Imported Upstream version 4.84 upstream/4.84
authorClinton Ebadi <clinton@unknownlamer.org>
Thu, 14 May 2015 03:12:14 +0000 (23:12 -0400)
committerClinton Ebadi <clinton@unknownlamer.org>
Thu, 14 May 2015 03:12:14 +0000 (23:12 -0400)
372 files changed:
.gitattributes [new file with mode: 0644]
.gitignore [new file with mode: 0644]
ABOUT [new file with mode: 0644]
ACKNOWLEDGMENTS [new file with mode: 0644]
CHANGES [new file with mode: 0644]
CONTRIBUTING [new file with mode: 0644]
LICENCE [new file with mode: 0644]
LICENSE.opendmarc [new file with mode: 0644]
Makefile [new file with mode: 0644]
NOTICE [new file with mode: 0644]
OS/Makefile-AIX [new file with mode: 0644]
OS/Makefile-BSDI [new file with mode: 0644]
OS/Makefile-Base [new file with mode: 0644]
OS/Makefile-CYGWIN [new file with mode: 0644]
OS/Makefile-DGUX [new file with mode: 0644]
OS/Makefile-Darwin [new file with mode: 0644]
OS/Makefile-Default [new file with mode: 0644]
OS/Makefile-DragonFly [new file with mode: 0644]
OS/Makefile-FreeBSD [new file with mode: 0644]
OS/Makefile-GNU [new file with mode: 0644]
OS/Makefile-GNUkFreeBSD [new file with mode: 0644]
OS/Makefile-GNUkNetBSD [new file with mode: 0644]
OS/Makefile-HI-OSF [new file with mode: 0644]
OS/Makefile-HI-UX [new file with mode: 0644]
OS/Makefile-HP-UX [new file with mode: 0644]
OS/Makefile-HP-UX-9 [new file with mode: 0644]
OS/Makefile-IRIX [new file with mode: 0644]
OS/Makefile-IRIX6 [new file with mode: 0644]
OS/Makefile-IRIX632 [new file with mode: 0644]
OS/Makefile-IRIX65 [new file with mode: 0644]
OS/Makefile-Linux [new file with mode: 0644]
OS/Makefile-NetBSD [new file with mode: 0644]
OS/Makefile-NetBSD-a.out [new file with mode: 0644]
OS/Makefile-OSF1 [new file with mode: 0644]
OS/Makefile-OpenBSD [new file with mode: 0644]
OS/Makefile-OpenUNIX [new file with mode: 0644]
OS/Makefile-QNX [new file with mode: 0644]
OS/Makefile-SCO [new file with mode: 0644]
OS/Makefile-SCO_SV [new file with mode: 0644]
OS/Makefile-SunOS4 [new file with mode: 0644]
OS/Makefile-SunOS5 [new file with mode: 0644]
OS/Makefile-SunOS5-hal [new file with mode: 0644]
OS/Makefile-ULTRIX [new file with mode: 0644]
OS/Makefile-UNIX_SV [new file with mode: 0644]
OS/Makefile-USG [new file with mode: 0644]
OS/Makefile-Unixware7 [new file with mode: 0644]
OS/Makefile-mips [new file with mode: 0644]
OS/eximon.conf-Default [new file with mode: 0644]
OS/os.Configuring [new file with mode: 0644]
OS/os.c-GNU [new file with mode: 0644]
OS/os.c-HI-OSF [new file with mode: 0644]
OS/os.c-IRIX [new file with mode: 0644]
OS/os.c-IRIX6 [new file with mode: 0644]
OS/os.c-IRIX632 [new file with mode: 0644]
OS/os.c-IRIX65 [new file with mode: 0644]
OS/os.c-Linux [new file with mode: 0644]
OS/os.c-OSF1 [new file with mode: 0644]
OS/os.c-cygwin [new file with mode: 0644]
OS/os.h-AIX [new file with mode: 0644]
OS/os.h-BSDI [new file with mode: 0644]
OS/os.h-DGUX [new file with mode: 0644]
OS/os.h-Darwin [new file with mode: 0644]
OS/os.h-DragonFly [new file with mode: 0644]
OS/os.h-FreeBSD [new file with mode: 0644]
OS/os.h-GNU [new file with mode: 0644]
OS/os.h-GNUkFreeBSD [new file with mode: 0644]
OS/os.h-GNUkNetBSD [new file with mode: 0644]
OS/os.h-HI-OSF [new file with mode: 0644]
OS/os.h-HI-UX [new file with mode: 0644]
OS/os.h-HP-UX [new file with mode: 0644]
OS/os.h-HP-UX-9 [new file with mode: 0644]
OS/os.h-IRIX [new file with mode: 0644]
OS/os.h-IRIX6 [new file with mode: 0644]
OS/os.h-IRIX632 [new file with mode: 0644]
OS/os.h-IRIX65 [new file with mode: 0644]
OS/os.h-Linux [new file with mode: 0644]
OS/os.h-NetBSD [new file with mode: 0644]
OS/os.h-NetBSD-a.out [new file with mode: 0644]
OS/os.h-OSF1 [new file with mode: 0644]
OS/os.h-OpenBSD [new file with mode: 0644]
OS/os.h-OpenUNIX [new file with mode: 0644]
OS/os.h-QNX [new file with mode: 0644]
OS/os.h-SCO [new file with mode: 0644]
OS/os.h-SCO_SV [new file with mode: 0644]
OS/os.h-SunOS4 [new file with mode: 0644]
OS/os.h-SunOS5 [new file with mode: 0644]
OS/os.h-SunOS5-hal [new file with mode: 0644]
OS/os.h-ULTRIX [new file with mode: 0644]
OS/os.h-UNIX_SV [new file with mode: 0644]
OS/os.h-USG [new file with mode: 0644]
OS/os.h-Unixware7 [new file with mode: 0644]
OS/os.h-cygwin [new file with mode: 0644]
OS/os.h-mips [new file with mode: 0644]
README [new file with mode: 0644]
README.DSN [new file with mode: 0644]
README.UPDATING [new file with mode: 0644]
doc/ChangeLog [new file with mode: 0644]
doc/Exim3.upgrade [new file with mode: 0644]
doc/Exim4.upgrade [new file with mode: 0644]
doc/GnuTLS-FAQ.txt [new file with mode: 0644]
doc/NewStuff [new file with mode: 0644]
doc/OptionLists.txt [new file with mode: 0644]
doc/README [new file with mode: 0644]
doc/README.SIEVE [new file with mode: 0644]
doc/dbm.discuss.txt [new file with mode: 0644]
doc/exim.8 [new file with mode: 0644]
doc/experimental-spec.txt [new file with mode: 0644]
doc/filter.txt [new file with mode: 0644]
doc/spec.txt [new file with mode: 0644]
exim_monitor/EDITME [new file with mode: 0644]
exim_monitor/em_StripChart.c [new file with mode: 0644]
exim_monitor/em_TextPop.c [new file with mode: 0644]
exim_monitor/em_globals.c [new file with mode: 0644]
exim_monitor/em_hdr.h [new file with mode: 0644]
exim_monitor/em_init.c [new file with mode: 0644]
exim_monitor/em_log.c [new file with mode: 0644]
exim_monitor/em_main.c [new file with mode: 0644]
exim_monitor/em_menu.c [new file with mode: 0644]
exim_monitor/em_queue.c [new file with mode: 0644]
exim_monitor/em_strip.c [new file with mode: 0644]
exim_monitor/em_text.c [new file with mode: 0644]
exim_monitor/em_version.c [new file with mode: 0644]
exim_monitor/em_xs.c [new file with mode: 0644]
scripts/Configure [new file with mode: 0755]
scripts/Configure-Makefile [new file with mode: 0755]
scripts/Configure-config.h [new file with mode: 0755]
scripts/Configure-eximon [new file with mode: 0755]
scripts/Configure-os.c [new file with mode: 0755]
scripts/Configure-os.h [new file with mode: 0755]
scripts/MakeLinks [new file with mode: 0755]
scripts/arch-type [new file with mode: 0755]
scripts/exim_install [new file with mode: 0755]
scripts/lookups-Makefile [new file with mode: 0755]
scripts/newer [new file with mode: 0755]
scripts/os-type [new file with mode: 0755]
scripts/reversion [new file with mode: 0755]
scripts/source_checks [new file with mode: 0644]
src/EDITME [new file with mode: 0644]
src/acl.c [new file with mode: 0644]
src/aliases.default [new file with mode: 0644]
src/auths/Makefile [new file with mode: 0644]
src/auths/README [new file with mode: 0644]
src/auths/auth-spa.c [new file with mode: 0644]
src/auths/auth-spa.h [new file with mode: 0644]
src/auths/b64decode.c [new file with mode: 0644]
src/auths/b64encode.c [new file with mode: 0644]
src/auths/call_pam.c [new file with mode: 0644]
src/auths/call_pwcheck.c [new file with mode: 0644]
src/auths/call_radius.c [new file with mode: 0644]
src/auths/check_serv_cond.c [new file with mode: 0644]
src/auths/cram_md5.c [new file with mode: 0644]
src/auths/cram_md5.h [new file with mode: 0644]
src/auths/cyrus_sasl.c [new file with mode: 0644]
src/auths/cyrus_sasl.h [new file with mode: 0644]
src/auths/dovecot.c [new file with mode: 0644]
src/auths/dovecot.h [new file with mode: 0644]
src/auths/get_data.c [new file with mode: 0644]
src/auths/get_no64_data.c [new file with mode: 0644]
src/auths/gsasl_exim.c [new file with mode: 0644]
src/auths/gsasl_exim.h [new file with mode: 0644]
src/auths/heimdal_gssapi.c [new file with mode: 0644]
src/auths/heimdal_gssapi.h [new file with mode: 0644]
src/auths/md5.c [new file with mode: 0644]
src/auths/plaintext.c [new file with mode: 0644]
src/auths/plaintext.h [new file with mode: 0644]
src/auths/pwcheck.c [new file with mode: 0644]
src/auths/pwcheck.h [new file with mode: 0644]
src/auths/sha1.c [new file with mode: 0644]
src/auths/spa.c [new file with mode: 0644]
src/auths/spa.h [new file with mode: 0644]
src/auths/xtextdecode.c [new file with mode: 0644]
src/auths/xtextencode.c [new file with mode: 0644]
src/bmi_spam.c [new file with mode: 0644]
src/bmi_spam.h [new file with mode: 0644]
src/buildconfig.c [new file with mode: 0644]
src/child.c [new file with mode: 0644]
src/cnumber.h [new file with mode: 0644]
src/config.h.defaults [new file with mode: 0644]
src/configure.default [new file with mode: 0644]
src/convert4r3.src [new file with mode: 0755]
src/convert4r4.src [new file with mode: 0755]
src/crypt16.c [new file with mode: 0644]
src/daemon.c [new file with mode: 0644]
src/dbfn.c [new file with mode: 0644]
src/dbfunctions.h [new file with mode: 0644]
src/dbstuff.h [new file with mode: 0644]
src/dcc.c [new file with mode: 0644]
src/dcc.h [new file with mode: 0644]
src/debug.c [new file with mode: 0644]
src/deliver.c [new file with mode: 0644]
src/demime.c [new file with mode: 0644]
src/demime.h [new file with mode: 0644]
src/directory.c [new file with mode: 0644]
src/dkim.c [new file with mode: 0644]
src/dkim.h [new file with mode: 0644]
src/dmarc.c [new file with mode: 0644]
src/dmarc.h [new file with mode: 0644]
src/dns.c [new file with mode: 0644]
src/drtables.c [new file with mode: 0644]
src/dummies.c [new file with mode: 0644]
src/enq.c [new file with mode: 0644]
src/exicyclog.src [new file with mode: 0644]
src/exigrep.src [new file with mode: 0644]
src/exim.c [new file with mode: 0644]
src/exim.h [new file with mode: 0644]
src/exim_checkaccess.src [new file with mode: 0755]
src/exim_dbmbuild.c [new file with mode: 0644]
src/exim_dbutil.c [new file with mode: 0644]
src/exim_lock.c [new file with mode: 0644]
src/eximon.src [new file with mode: 0644]
src/eximstats.src [new file with mode: 0644]
src/exinext.src [new file with mode: 0644]
src/exipick.src [new file with mode: 0644]
src/exiqgrep.src [new file with mode: 0644]
src/exiqsumm.src [new file with mode: 0644]
src/exiwhat.src [new file with mode: 0644]
src/expand.c [new file with mode: 0644]
src/filter.c [new file with mode: 0644]
src/filtertest.c [new file with mode: 0644]
src/functions.h [new file with mode: 0644]
src/globals.c [new file with mode: 0644]
src/globals.h [new file with mode: 0644]
src/header.c [new file with mode: 0644]
src/host.c [new file with mode: 0644]
src/ip.c [new file with mode: 0644]
src/local_scan.c [new file with mode: 0644]
src/local_scan.h [new file with mode: 0644]
src/log.c [new file with mode: 0644]
src/lookupapi.h [new file with mode: 0644]
src/lookups/Makefile [new file with mode: 0644]
src/lookups/README [new file with mode: 0644]
src/lookups/cdb.c [new file with mode: 0644]
src/lookups/dbmdb.c [new file with mode: 0644]
src/lookups/dnsdb.c [new file with mode: 0644]
src/lookups/dsearch.c [new file with mode: 0644]
src/lookups/ibase.c [new file with mode: 0644]
src/lookups/ldap.c [new file with mode: 0644]
src/lookups/ldap.h [new file with mode: 0644]
src/lookups/lf_check_file.c [new file with mode: 0644]
src/lookups/lf_functions.h [new file with mode: 0644]
src/lookups/lf_quote.c [new file with mode: 0644]
src/lookups/lf_sqlperform.c [new file with mode: 0644]
src/lookups/lsearch.c [new file with mode: 0644]
src/lookups/mysql.c [new file with mode: 0644]
src/lookups/nis.c [new file with mode: 0644]
src/lookups/nisplus.c [new file with mode: 0644]
src/lookups/oracle.c [new file with mode: 0644]
src/lookups/passwd.c [new file with mode: 0644]
src/lookups/pgsql.c [new file with mode: 0644]
src/lookups/redis.c [new file with mode: 0644]
src/lookups/spf.c [new file with mode: 0644]
src/lookups/sqlite.c [new file with mode: 0644]
src/lookups/testdb.c [new file with mode: 0644]
src/lookups/whoson.c [new file with mode: 0644]
src/lss.c [new file with mode: 0644]
src/macros.h [new file with mode: 0644]
src/malware.c [new file with mode: 0644]
src/match.c [new file with mode: 0644]
src/memcheck.h [new file with mode: 0644]
src/mime.c [new file with mode: 0644]
src/mime.h [new file with mode: 0644]
src/moan.c [new file with mode: 0644]
src/mytypes.h [new file with mode: 0644]
src/os.c [new file with mode: 0644]
src/osfunctions.h [new file with mode: 0644]
src/parse.c [new file with mode: 0644]
src/pdkim/Makefile [new file with mode: 0644]
src/pdkim/README [new file with mode: 0644]
src/pdkim/base64.c [new file with mode: 0644]
src/pdkim/base64.h [new file with mode: 0644]
src/pdkim/bignum.c [new file with mode: 0644]
src/pdkim/bignum.h [new file with mode: 0644]
src/pdkim/bn_mul.h [new file with mode: 0644]
src/pdkim/pdkim.c [new file with mode: 0644]
src/pdkim/pdkim.h [new file with mode: 0644]
src/pdkim/rsa.c [new file with mode: 0644]
src/pdkim/rsa.h [new file with mode: 0644]
src/pdkim/sha1.c [new file with mode: 0644]
src/pdkim/sha1.h [new file with mode: 0644]
src/pdkim/sha2.c [new file with mode: 0644]
src/pdkim/sha2.h [new file with mode: 0644]
src/perl.c [new file with mode: 0644]
src/queue.c [new file with mode: 0644]
src/rda.c [new file with mode: 0644]
src/readconf.c [new file with mode: 0644]
src/receive.c [new file with mode: 0644]
src/regex.c [new file with mode: 0644]
src/retry.c [new file with mode: 0644]
src/rewrite.c [new file with mode: 0644]
src/rfc2047.c [new file with mode: 0644]
src/route.c [new file with mode: 0644]
src/routers/Makefile [new file with mode: 0644]
src/routers/README [new file with mode: 0644]
src/routers/accept.c [new file with mode: 0644]
src/routers/accept.h [new file with mode: 0644]
src/routers/dnslookup.c [new file with mode: 0644]
src/routers/dnslookup.h [new file with mode: 0644]
src/routers/ipliteral.c [new file with mode: 0644]
src/routers/ipliteral.h [new file with mode: 0644]
src/routers/iplookup.c [new file with mode: 0644]
src/routers/iplookup.h [new file with mode: 0644]
src/routers/manualroute.c [new file with mode: 0644]
src/routers/manualroute.h [new file with mode: 0644]
src/routers/queryprogram.c [new file with mode: 0644]
src/routers/queryprogram.h [new file with mode: 0644]
src/routers/redirect.c [new file with mode: 0644]
src/routers/redirect.h [new file with mode: 0644]
src/routers/rf_change_domain.c [new file with mode: 0644]
src/routers/rf_expand_data.c [new file with mode: 0644]
src/routers/rf_functions.h [new file with mode: 0644]
src/routers/rf_get_errors_address.c [new file with mode: 0644]
src/routers/rf_get_munge_headers.c [new file with mode: 0644]
src/routers/rf_get_transport.c [new file with mode: 0644]
src/routers/rf_get_ugid.c [new file with mode: 0644]
src/routers/rf_lookup_hostlist.c [new file with mode: 0644]
src/routers/rf_queue_add.c [new file with mode: 0644]
src/routers/rf_self_action.c [new file with mode: 0644]
src/routers/rf_set_ugid.c [new file with mode: 0644]
src/search.c [new file with mode: 0644]
src/sieve.c [new file with mode: 0644]
src/smtp_in.c [new file with mode: 0644]
src/smtp_out.c [new file with mode: 0644]
src/spam.c [new file with mode: 0644]
src/spam.h [new file with mode: 0644]
src/spf.c [new file with mode: 0644]
src/spf.h [new file with mode: 0644]
src/spool_in.c [new file with mode: 0644]
src/spool_mbox.c [new file with mode: 0644]
src/spool_out.c [new file with mode: 0644]
src/srs.c [new file with mode: 0644]
src/srs.h [new file with mode: 0644]
src/std-crypto.c [new file with mode: 0644]
src/store.c [new file with mode: 0644]
src/store.h [new file with mode: 0644]
src/string.c [new file with mode: 0644]
src/structs.h [new file with mode: 0644]
src/tls-gnu.c [new file with mode: 0644]
src/tls-openssl.c [new file with mode: 0644]
src/tls.c [new file with mode: 0644]
src/tlscert-gnu.c [new file with mode: 0644]
src/tlscert-openssl.c [new file with mode: 0644]
src/tod.c [new file with mode: 0644]
src/transport-filter.src [new file with mode: 0644]
src/transport.c [new file with mode: 0644]
src/transports/Makefile [new file with mode: 0644]
src/transports/README [new file with mode: 0644]
src/transports/appendfile.c [new file with mode: 0644]
src/transports/appendfile.h [new file with mode: 0644]
src/transports/autoreply.c [new file with mode: 0644]
src/transports/autoreply.h [new file with mode: 0644]
src/transports/lmtp.c [new file with mode: 0644]
src/transports/lmtp.h [new file with mode: 0644]
src/transports/pipe.c [new file with mode: 0644]
src/transports/pipe.h [new file with mode: 0644]
src/transports/smtp.c [new file with mode: 0644]
src/transports/smtp.h [new file with mode: 0644]
src/transports/tf_maildir.c [new file with mode: 0644]
src/transports/tf_maildir.h [new file with mode: 0644]
src/tree.c [new file with mode: 0644]
src/valgrind.h [new file with mode: 0644]
src/verify.c [new file with mode: 0644]
src/version.c [new file with mode: 0644]
src/version.sh [new file with mode: 0644]
util/README [new file with mode: 0644]
util/cramtest.pl [new file with mode: 0755]
util/gen_pkcs3.c [new file with mode: 0644]
util/logargs.sh [new file with mode: 0755]
util/mkcdb.pl [new file with mode: 0755]
util/ocsp_fetch.pl [new file with mode: 0755]
util/proxy_protocol_client.pl [new file with mode: 0644]
util/ratelimit.pl [new file with mode: 0644]
util/unknownuser.sh [new file with mode: 0755]

diff --git a/.gitattributes b/.gitattributes
new file mode 100644 (file)
index 0000000..554385c
--- /dev/null
@@ -0,0 +1 @@
+ACKNOWLEDGMENTS         encoding=utf-8
diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..7839e97
--- /dev/null
@@ -0,0 +1,4 @@
+Local
+build-*
+tags
+cscope.out
diff --git a/ABOUT b/ABOUT
new file mode 100644 (file)
index 0000000..8d204b5
--- /dev/null
+++ b/ABOUT
@@ -0,0 +1,9 @@
+Exim repository: src
+--------------------
+
+This directory contains everything that is included in an Exim distribution
+tarball, with the exception of the doc directory and an empty Local directory.
+You can build Exim from the contents of this directory by adding a Local
+directory that contains appropriate configuration files.
+
+End
diff --git a/ACKNOWLEDGMENTS b/ACKNOWLEDGMENTS
new file mode 100644 (file)
index 0000000..1c4a934
--- /dev/null
@@ -0,0 +1,474 @@
+EXIM ACKNOWLEDGEMENTS
+
+This file is divided into two parts.  The first is the original list maintained
+by Exim's author, Philip Hazel, before he retired.  That has two sub-lists of
+contributors.  The second main part is an attempt to bring this up-to-date,
+using information from ChangeLog and git.
+
+Names may well occur more than once.
+
+There was a five year gap.  It is unlikely that this file is complete.
+If you contributed and are not listed, then *please* let us know.  Even if you
+don't much care, we want to acknowledge your help.  A contribution isn't just
+code, it includes reporting real bugs, helping with tracking problems down,
+documentation fixes and more.
+
+(Note that we have patches from folks in various countries and Latin1 is not
+ sufficient to handle all of their names acceptably.
+ This file should be in UTF-8).
+
+-Phil Pennock, pp The Exim Maintainers.
+
+============================8< cut here >8==============================
+
+I have not been very good at keeping a proper record of all the people who have
+sent in patches and other contributions to Exim. I am going to try to do better
+in the future by keeping a record in this file. First, I'll put a list of all
+those I can recover from the past; then I'll create a new list to which I'll
+add new contributors in future. Some regular contributors may appear in both.
+
+I'm going to record people who send in actual patches or who help in detailed
+ways. I'm not going to list people who just make a suggestion or report a
+bug. I hope that is a reasonable approach.
+
+If you should be on one of these lists and are not, please accept my apologies,
+and let me know! Any omissions are solely due to my incompetence. In
+particular, the "past" list has certainly lost the names of people who sent in
+relatively small patches.
+
+Philip Hazel
+
+Lists created: 20 November 2002
+Last updated (by PH):  22 August 2007
+
+THE OLD LIST
+
+Alan Barratt              First code for relay checking
+Malcolm Beattie           Interface to embedded Perl
+Philip Blundell           First support for IPv6
+Piete Brooks              Running the first live version
+                            Implementing multiple-system compilation
+Matthew Byng-Maddick      First code for dsearch lookup
+Steve Campbell            Extensions to eximstats
+                            Steve is now the maintainer of eximstats
+Brian Candler             LDAP support enhancement
+Petr Cech                 PostgreSQL interface
+Steve Clarke              Best way to find the load average in Linux
+Energis Ltd               Resources for the exim.org site
+Yann Golanski             Numerical hash function
+Jason Gunthorpe           IPv6 support (Linux)
+Michael Haardt            LDAP support enhancement
+Steve Haslam              First code for TLS
+Kjetil Torgrim Homme      Suggested patch for macro extensions
+John Horne                Proof-reading documentation (repeatedly)
+Pierre Humblet            Cygwin support
+Paul Kelly                MySQL interface
+                            First code for Oracle interface
+Ian Kirk                  Radius support
+Stuart Levy               Replacement for broken inet_ntoa() on IRIX
+Stuart Lynne              First code for LDAP
+Nigel Metheringham        Setting up the web site and mailing list
+                            Managing the web site and mailing list
+                            Interface to Berkeley DB
+                            Support for cdb
+                            Support for maildir
+Barry Pederson            LDAP support enhancement
+Marc Prud'hommeaux        SPA client authentication
+Alexander Sabourenkov     pwcheck daemon support
+Peter Savitch             LDAP support enhancement
+Robert Wal                whoson lookup
+Joachim Wieland           Researching strace and stolen subprocesses in Linux
+
+
+THE NEW LIST
+
+Alexander Alekseev        Use of function attribute checks in gcc
+Justo Alonso              Suggested patch for maildir++ maildirsize file support
+Anton Altaparmakov        Patches to get cyrus_sasl fully working
+Simon Arlott              Patch for $dnslist_matched.
+Claus Assmann             Example code for OpenSSL CRL support
+Warren Baker              Experimental Redis lookup.
+Robert Bannocks           Patch for LDAP reference problem on Solaris
+Ian Bell                  Analysis of a bug and an infelicity in clock tick code
+                          Patch for ${quote_local_part
+Peter Benie               A number mistakes found by analysing the code
+Johannes Berg             Suggested patch for authentication client $auth<n> support
+                          Suggested patch for acl_not_smtp_start
+Matt Bernstein            LMTP over socket
+                          Suggested patch for dnslists '&' feature
+Mike Bethune              Help with debugging an elusive ALRM signal bug
+Ard Biesheuvel            Lookup code for accessing an Interbase database
+Richard Birkett           Fix for empty -f address crash
+Dean Brooks               Fix for ratelimit per_rcpt in acl_not_smtp.
+Nick Burrett              Patch for CONFIGURE_FILE_USE_EUID in exicyclog
+Matthew Byng-Maddick      Patch for qualify_domain in redirect router
+                          Patch for ignore_target_hosts in ipliteral router
+                          The cyrus_sasl authenticator
+Steve Campbell            eximstats extensions and continued maintenance
+Brian Candler             Use h_errno for gethostbyname()
+                          Suggested patch for .ifdef etc
+                          Several minor fixes and suggestions
+Pete Carah                Patch for change to radiusclient API
+Oliver Cook               Suggested patch for exigrep & rejected messages
+                            Patch to add sender/host info to local_scan() rejects
+                            Suggested patch to add queue time to "Completed"
+Ted Cooper                Suggested patch for NOTQUIT ACL
+Jennifer Corley           Designing the new Exim logo
+John Dalbec               Patch for quota_warn_threshold bug
+Vivek Dasmohapatra        Suggested patch for CRL support
+Dennis Davis              Suggested server_condition for all authenticators
+Andrew Doran              Patch for NetBSD configuration files
+                          Patch for ifreq alignment and size problems
+Michael Deutschmann       Suggested patch for treating bind() failure like connect()
+                          Patch for $sender_data and $recipient_data
+                          Suggested patch for null address match lookup bug
+                          Suggested patch for verify = not_blind
+                          Patch for alternate TXT lookup in DNS lists
+Oliver Eikemeier          Patch to skip Received: if expansion is empty
+                          Patch for "eqi"
+Nico Erfurth              Fix for bug in ${readfile}
+                            Patch for router_home_directory
+                            Patch for ACL crash (try to test sender after ETRN)
+                            Suggested patch for lookup search bug
+                            Suggested patch for advertise_condition
+                            Patch for missing HELO in checkaccess
+                            Patch for raw headers
+                            Patch for lsearch lookups tidying
+                            Patch for .include_if_exists
+                            Patch for partial- not recognized in host list
+                            Lots more patches for bug fixes, enhancements, and
+                              code refactorings - too many to record details!
+Jochen Erwied             Fix for BDB 4.1 API
+Stefan Esser              Fix for DNS RR parsing bug
+Peter Evans               Suggested using modification time of "new" for time
+                            of "mailbox last read" for maildir
+Andrew Findlay            Patch to close writing end of ${readsocket
+Michael Fischer
+  v. Mollard              Suggested patch for exigrep -t option
+Kevin Fleming             Callout cache code
+                            Patch for authenticated_sender
+Tony Finch                Expansion extensions
+                            Timezone addition to log timestamps
+                            A number of useful code criticisms
+                            Timezone patch for exiwhat
+                            Patch for more daemon exiwhat information
+                            Patch for -dd
+                            Patch for mxh lookup type in dnsdb
+                            Patch for defer_foo in dndsb
+                            Patch for ${dlfunc
+                            Patch for $message_linecount
+                            ... and many more
+Graeme Fowler             Suggested patch for /noupdate with ratelimit
+Ian Freislich             Patch for spamd timeout problem
+Giuliano Gavazzi          Patches for OSX compilation
+Dominic Germain           Patch for exiqgrep MacOS X bug
+Oliver Gorwits            $load_average patch
+                          Patch for additional syslog facilities
+James Grinter             Suggested patches for header manipulation functions
+                            and recipient remove for local_scan() use
+Lukasz Grochal            Patch for saslauthd buglet
+Pavel Gulchouck           Diagnosis of return_path_on_delivery crash
+Michael Haardt            Tidies to make the code stricter
+                          Refactoring to allow for other filter types
+                          Suggested patch for appendfile "folder" extension
+                          Module to support Sieve (RFC 3028) filters and
+                            continued maintenance of same
+                          Patch for faster sort algorithm in queue.c
+                          Patch for LDAP timeout handling
+                            ... and several more
+Thomas Hager              Patch for saslauthd crash bug
+Richard Hall              Fix for file descriptor leak in redirection
+                          Fix for exiqsumm output corner case
+Jori Hamalainen           Patch to add features to exiqsumm
+                          Patch to speed up exigrep
+Steve Haslam              Lots of stuff, including
+                            HMAC computations
+                            Better error messages for BDB
+Sheldon Hearn             Suggested patch for smtp_accept_max_nonmail_hosts
+                          Fix for compile error with OpenSSL 0.9.8e
+Bryan Henderson           Patch to use RM_COMMAND everywhere during building
+Jakob Hirsch              Patch for % operator
+                          Patch for arbitrarily named ACL variables
+Magnus Holmgren           Patch for filter_prepend_home
+                          Patch for "h" flag in Domain Keys
+                          Patch for $sending_ip_address/$sending_port
+                          Patch for ${rfc2047d:
+                            ... and several more
+                          Lots of other maintenance support
+Kjetil Torgrim Homme      Patch for require_files problem on NFS file systems
+Tom Hughes                Suggested patch for $n bug in pipe command from filter
+Pierre Humblet            Continued Cygwin support
+Peter Ilieve              Suggested patch for lookup search bug
+John Jetmore              Writing and maintaining the 'exipick' utility
+                          Much helpful testing of the test suite & elsewhere
+                          Patch for -Mset
+                          Patch for TLS testing with -bh/-bhc/-bs
+                          Patch for exigrep -v functionality
+Bob Johannessen           Patch for Sieve envelope tests bug
+                          Patch for negative uid/gid bug
+Brad Jorsch               Patch for bitwise logical operators
+                          Patch for using "message" on acceptance
+                          Patch to add == and =& to dnslists
+Christian Kellner         Patch for LDAP dereferencing
+Alex Kiernan              Patches for libradius
+                          Diagnosis of milliwait clock-backwards bug
+                          Patch for BDB 4.3 API change
+Tom Kistner               SPA server code
+                          Writing and maintaining the content scanning
+                            extension (exiscan)
+Jürgen Kreileder          Fix for cyrus_sasl advertisement problem
+Friso Kuipers             Patch for GDBM problem
+Matthias Lederhofer       Diagnosing and patching obscure and subtle socket bug
+Chris Liddiard            Fix for bug in exiqsumm
+Chris Lightfoot           Patch for -restore-times in exim_lock
+Edgar Lovecraft           Patch for ${str2b64:
+Torsten Luettgert         Suggested patch for proper integer overflow detection
+Todd Lyons                Patch to add DMARC support using OpenDMARC libs/tools
+David Madole              Patch for SPA forced expansion failure bug
+Lars Mainka               Patch for OpenSSL crl collections
+Andrey Malyshev           Patch for $address_data after redirection bug
+Lionel Elie Mamane        Patch for IPv4/IPv6 listen() problem on USAGI Linux
+                          Patch for recognizing IPv6 "scoped addresses"
+                          Patch for callout caching bug
+Everton da Silva Marques  Suggested patch for SRV handling
+                          Suggested patch for SRV/MX lookup retry option
+Nikos Mavrogiannopoulos   GnuTLS proof of concept code
+                          Update to RSA and D-H parameter caching code
+Komar Maxim               Patch for check_rfc2047_length
+Andy Mell                 Fix for rejectlog regeneration bug
+Marc Merlin               Many suggestions and patches for callouts and
+                            SMTP error message features
+Andreas Metzler           Patch for message_id_header_domain
+                          Suggested patch for multi-config files in scripts bug
+                          GnuTLS non-existent parameter file bug fix
+Alex Miller               Suggested readline() patch
+                          Patch for LDAP_RES_SEARCH_REFERENCE handling
+                          Support for the DrWeb content scanner
+Arkadiusz Miskiewicz      Patch to add timeout to reads in malware.c
+Martin Mrazik             Patches for problems in the test suite
+Andreas Mueller           Patch for logging uncompleted SMTP transactions
+Pete Naylor               Patch for LDAP TCP connect timeout setting
+Alexander Newmann         Diagnosing and patching obscure and subtle socket bug
+Matthew Newton            Patch for exicyclog log location problem
+Marcin Owsiany            Diagnosis of a tricky timeout failure bug
+Andrey Panin              Dovecot authenticator
+Eric Parusel              Patch for tls_remember_esmtp
+Gaige Paulsen             Amended Darwin config files
+Richard Premdas           Patch for PAM buglet
+Jason Pyeron              Suggested patch for ignoring Sendmail's -O option
+Axel Rau                  Patch for Transport Post Delivery sql logging
+Mark Rigby-Jones          Patch for race condition during MBX locking
+Robert Roselius           Patch for OpenSSL workaround for bad clients
+Larry Rosenman            OpenUNIX config files
+Alexander Sabourenkov     Patch to add saslauthd daemon support
+                          Patch for MySQL non-data queries
+David Saez                Suggested patch for $sender_hostname lookup if needed
+                          Support for the clamd virus scanner
+                          Suggested patch for increased number of ACL variables
+Jonathan Sambrook         Suggested patch for expanding uid and gid lists
+Peter Savitch             Diagnosis of FPE bug when statvfs() fails on spool
+Harald Schueler           Patch for dn_expand() failure on truncated data
+Heiko Schlichting         Diagnosis of intermittent daemon crash bug
+Heiko Schlitterman        Proposed patch for +pid
+Stephan Schulz            Patch for $host_data caching error
+Lai Zit Seng              Patch for radiusclient 0.4.9 interface bugs
+Tony Sheen                Log files with datestamped names and auto rollover
+Martin Sluka              Patch for exigrep to include non-message lines
+Adam Stephens             Suggested patch for IGNOREQUOTA in LMTP
+Russell Stuart            Diagnosis of obscure batch multiple delivery bug
+Tamas Tevesz              Patch for crypt16() support
+Johan Thelmen             Support for the F-Secure virus scanner
+William Thompson          Suggested patch for acl_smtp_helo
+                          Suggested patch for nested ACL "drop" bug
+                          Suggested patch for continuation lines in file ACLs
+                          Patch for != support in DNS lists
+Adam Thornton             Patch for SMTP port expansion
+Daniel Tiefnig            Much helpful testing of the test suite
+Rein Tollevik             Patch to fix search cache missing tidyup
+Stefan Traby              Threaded Perl support
+Samuli Tuomola            OS files for QNX 6.2.0
+Dave Turner               Suggested patch for sender rewriting brokenness
+Steve Usher               Unbuffered I/O patch for Dovecot authentication
+Carlos Villegas           Suggested patch for "headers" in filter files
+Matthias Waffenschmidt    Patch for build-time Perl bug in configure script
+                          Queue run abandon log message tidy up
+Norihisa Washitake        Suggested patch for RFC 2047 header decoding
+Chris Webb                Patch for support of an SPF lookup method.
+Florian Weimer            Patch for minor format string issue
+                          Noticing the unwanted (and time-wasting) GnuTLS
+                            RSA_EXPORT code, and supplying a patch to remove it
+Joachim Wieland           Patches for PostgreSQL socket support and other
+                            PostgreSQL functionality
+                          Patch for hosts_avoid_esmtp
+Stephen Wilcox            Patch for ignore_enotdir problem
+Alain Williams            Suggested patch for exicyclog options
+                          PATCH for LDAP referrals option
+David Woodhouse           SQLite support proof of concept code
+                          control=freeze/no_tell basic code
+Erik ?                    patch to use select() instead of poll() on OS X
+****
+
+============================8< cut here >8==============================
+
+The Exim Maintainers Lists
+==========================
+
+We'll start with the Exim Maintainers, who are the people with commit
+access to the master git repository and a couple more folk; then we'll list
+known contributors since the lists above.  Then we list the folks who work
+to make Exim available on various operating systems as porters/packagers.
+
+For the Maintainers, we may list primary focus area.  All maintainers
+will have contributed to work outside those areas.  The maintainers'
+contributions are initialled in ChangeLog.  Changes from before maintainership
+should be listed as a contributor.
+
+For other contributors, we will attempt to track all contributions.  Note that
+the entries per-person were added initially by scanning back through the
+ChangeLog and git, so are not in chronological order.
+
+[ With names from all over the world, we need one sort order.  I've arbitrarily
+  decreed it to be "normal British address-book sort order, but based on family
+  name rather than whichever comes last and using whatever seems sanest for
+  sort order of characters which do not collate onto an English character",
+  which should handle the majority of cases.  If it is not adequate for some
+  situation, we'll resolve it then.
+  We leave out titles and honourifics, just names and handles. ]
+
+
+Maintainers
+-----------
+Steve Campbell            eximstats maintainer.
+Mike Cardwell             Exim webmaster.
+Tony Finch                Unbreaks lots of things. Ratelimit code.
+Graeme Fowler
+Michael Haardt            Maintains Sieve support, works on DKIM.
+Jeremy Harris
+Philip Hazel              Retired.
+                          Originating architect and author of the Exim project.
+John Jetmore
+Tom Kistner               DKIM. Content scanning. SPA.
+Todd Lyons
+Nigel Metheringham        Transitioning out of Default Victim status.
+Phil Pennock              Release Coordinator. Breaks lots of things.
+David Woodhouse           Dynamic modules. Security.
+
+
+Contributors
+------------
+Andrew Aitchison          Spotted cmdline AV scanner regression with -bmalware
+Simon Arlott              Code for outbound SSL-on-connect
+                          Patch implementing %M datestamping in log filenames
+                          Patch restoring SIGPIPE handler for child_open_uid
+                          Patch fixing NUL term/init of DKIM strings
+                          Patch fixing dnsdb TXT record handling for DKIM
+                          Patch speeding up DomainKeys signing
+Warren Baker              Found crash with MIME ACLs in non-SMTP local injection
+Dmitry Banschikov         Path to check for LDAP TLS initialisation errors
+René Berber               Pointed out mistake in build instructions for QNX
+Johannes Berg             Maintained dynamically loadable module code out-of-tree
+                          Patch expanding spamd_address if contains $
+Jasen Betts               Spotted lack of docs re bool{} on empty string 
+                            and typo fixes
+Wolfgang Breyha           DCC integration; expandable spamd_address
+                          Patch handling IPv6 addresses for SPF
+                          Patch fixing DKIM verification when signature header
+                            not prepended
+                          Unbroke Cyrus SASL auth after incorrect SSF addition
+                          Logging of 8bitmime reception
+David Brownlee            Patch improving local interface IP address detection
+Eugene Bujak              Security patch fixing buffer overflow in string_format
+Adam Ciarcinski           Patch for TLS-enabled LDAP (alternative to ldaps)
+Dennis Davis              Patches fixing compilation in older compilers
+                          Reported dynlookup framework build issues on Solaris
+Serge Demonchaux          Maintained dynamically loadable module code out-of-tree
+                          Patch fixing sign/unsigned and UTF mismatches
+Uwe Doering               Patch fixing DKIM multiple signature generation
+Maxim Dounin              Patch portability of accept() len
+Frank Elsner              Fixed build reliability by exporting LC_ALL=C
+Paul Fisher               Diagnosed smtp_cmd_buffer_size affecting GSSAPI SASL
+                            initial response, raised buffer size
+                          Patch adjusting connection_max_messages wait-DB usage
+Oliver Fleischmann        Patches fixing compilation in older compilers
+Julian Gilbey             Helped improve userforward local_part_suffix docs
+Richard Godbee            Patch fixing usage fprintf
+Steve Haslam              Maintained dynamically loadable module code out-of-tree
+Oliver Heesakkers         Debugged dynamic lookup build issues for LOOKUP_foo.
+Dmitry Isaikin            Spotted short writes to local files
+                          Patch for format string regression
+Alun Jones                Patch for NULL dereference in localhost_number
+Brad Jorsch               Patches fixing Resent-*: header handling
+John Hall                 Updated PCRE to 7.4 (when in-tree)
+Jeremy Harris             Patch to log authentication information in reject log
+                          Reported a ${extract error message typo
+Jakob Hirsch              Patch implementing freeze_signal on pipe transports
+                          Suggested X-Envelope-Sender: for content-scanning
+                          Patch fixing Base64 decode bugs
+John Horne                Patch adding $av_failed
+                          Patch escaping log text after lookup expansion defer
+                          Documentation fixes
+                          Pointed out ClamAV ExtendedDetectionInfo compat issue
+Regid Ichira              Documentation fixes
+Andreas M. Kirchwitz      Let /dev/null have normal permissions (4.73 fallout)
+J. Nick Koston            Patch adding force_command pipe transport option
+Roberto Lima              Patch letting exicyclog rotate paniclog
+Todd Lyons                Patch handling TAB in MAIL arguments
+Christof Meerwald         Provided insight & suggested patch for GnuTLS update
+Andreas Metzler           Patch upgrading PolarSSL (DKIM)
+                          Reported delivery logging problems (4.73 fallout)
+                          Patch to build without WITH_CONTENT_SCAN
+                          Patches fixing docs for max_rcpts, relay hosts/domains
+                          Documentation fixes
+Kirill Miazine            Multiple patches improving Dovecot authenticator
+Robert Millan             Wrote SPF Best Guess support
+Marcin MirosÅ‚aw           Running static analysis tools for us, catching issues
+Dirk Mueller              Patch extending use of our printf() compiler checking
+Andrey Oktyabrski         Patch fixing wide character breakage in rfc2047 coding
+                          Patch keeping SQL errors from being returned over SMTP
+Phil Pennock              Patch adding gnutls_compat_mode
+                          Patches adding bool{} and later bool_lax{}
+                          Patch for TLS library version reporting build/runtime
+                          Patch letting EXPN work under TLS
+                          More patches built up & applied when became maintainer
+Mark Daniel Reidel        Patch adding f-protd malware scanner support
+Steven A Reisman          Pointed out ${eval:x % 0} SIGFPE
+Todd Rinaldo              Patch fixing transport filter timeout
+Dan Rosenberg             Security notification & patch for hardlink attack on
+                            sticky mail directory
+                          Security notification of race condition in MBX locking
+Jay Rouman                Kept our copyright claim in the 21st century, not 11th
+                          Drew attention to SSL docs and epoch issue on 32bit
+Heiko Schlittermann       Patch making maildir_use_size_file expand
+                          Patch fixing maildir quota file races
+                          Patch fixing make parallelisation
+                          Updates to eximstats, exiwhat
+Janne Snabb               TLS extensive debugging & failure root cause analysis
+                          Added SPF record type support to dnsdb lookup
+Jan Srzednicki            Patch improving Dovecot authenticator
+                          Reported crash in Dovecot authenticator
+Samuel Thibault           Patch fixing IPv6 interface address detection on Hurd
+Martin Tscholak           Reported issue with TLS anonymous ciphersuites
+Stephen Usher             Patch fixing use of Oracle's LDAP libraries on Solaris
+Holger Weiß               Patch leting ${run} return more data than OS pipe
+                            buffer size
+Moritz Wilhelmy           Pointed out PCRE_PRERELEASE glitch
+Alain Williams            Patch supporting MySQL stored procedures
+Mark Zealey               Patch updating $message_linecount for maildir_tag
+                          Patch improving spamd server selection
+                          Patch to allow multiple TCP clamd servers
+
+
+Packagers
+---------
+Mark Baker                Debian, through Exim 3
+Hilko Bengen              Debian, Exim 4, current(*) maintenance
+Tim Cutts                 Debian, initial packaging
+Marc Haber                Debian, Exim 4, current(*) maintenance
+Steve Haslam              Debian, Exim 4
+Andreas Metzler           Debian, current(*) maintenance
+Christian Perrier         Debian, current(*) maintenance
+
+(*) Current as of our last information as of release: Exim 4.82
+
+
+# vim: set fileencoding=utf-8 expandtab :
diff --git a/CHANGES b/CHANGES
new file mode 100644 (file)
index 0000000..6f8fd3b
--- /dev/null
+++ b/CHANGES
@@ -0,0 +1,10 @@
+Change Information for Exim
+---------------------------
+
+Complete lists of all changes to the code, including bug fixes, are listed in
+doc/ChangeLog, and documentation for changes that have not yet made it
+into the manual is available in doc/NewStuff. The ftp site has a directory
+called ChangeLogs which contains individual ChangeLog and NewStuff files for
+each separate release.
+
+****
diff --git a/CONTRIBUTING b/CONTRIBUTING
new file mode 100644 (file)
index 0000000..a5f7809
--- /dev/null
@@ -0,0 +1,56 @@
+CONTRIBUTING TO EXIM
+====================
+
+Exim is an open-source project licensed under the GNU General Public License.
+At time of writing, all the developers work on Exim on a volunteer basis.
+We welcome patches and contributions.  There is no copyright assignment
+policy; if you offer a patch, it is assumed to be under the GPL, of whichever
+version the main developers see fit to use.
+
+Mistakes or inadequacies in the documentation are treated as bugs.  The main
+documentation is called "The Exim Specification" for a reason.  So if you
+can't code there are still places where your help will be very appreciated.
+
+General discussion, requests for help, and initial "is this a bug?" questions
+go to <exim-users@exim.org>.  Many suspected bugs turn out to not be bugs, so
+asking first is appreciated.
+
+Our main website is at http://www.exim.org/ and contains links to our wiki,
+where many frequent setups are walked through.  You will also find our
+bug-tracking system linked there.
+
+Development takes place in part on exim-users, when bugs or missing features
+are spotted based on feedback from people actually using the product.  In
+large part, discussion takes place on <exim-dev@exim.org>.  While you can use
+the bug-tracking system, everyone working on Exim, a mail transfer agent, is
+comfortable dealing with just email too, so you can use whichever you're most
+comfortable with.
+
+If you have an idea for a new feature, please do raise it on exim-users first.
+
+Our code is maintained in a Git repository.  The master repository, together
+with some others, can be found on http://git.exim.org/ and we welcome patches,
+whether of documentation or of code.  If you have a request for a new feature
+and can accompany it with working code, then it stands a much greater chance
+of being incorporated in a timely manner.
+
+If you're planning on working on a major new feature or redesign, please do
+talk to us first.
+
+We do not have a formal code-review process, but posted patches are subject to
+being reworked before being pulled in, or requests for modification made;
+we're a small enough pool of developers that we rely on the good judgement and
+discretion of the committer rather than formal process.
+
+We prefer new features to be accompanied by documentation patches, but if no
+new documentation is provided, we can write it and, in the process, perhaps
+uncover issues to work over with you.  Note that the PDF form of the
+documentation is faster to build than the TXT form.
+
+We do have a test harness and appreciate it if new features can be accompanied
+by new tests; if this is awkward for you, please do include sufficient
+description to allow someone else to write the test.
+
+
+-The Exim Maintainers
+ July 7th, 2010
diff --git a/LICENCE b/LICENCE
new file mode 100644 (file)
index 0000000..31d076d
--- /dev/null
+++ b/LICENCE
@@ -0,0 +1,340 @@
+                   GNU GENERAL PUBLIC LICENSE
+                      Version 2, June 1991
+
+ Copyright (C) 1989, 1991 Free Software Foundation, Inc.
+               51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
+ Everyone is permitted to copy and distribute verbatim copies
+ of this license document, but changing it is not allowed.
+
+                           Preamble
+
+  The licenses for most software are designed to take away your
+freedom to share and change it.  By contrast, the GNU General Public
+License is intended to guarantee your freedom to share and change free
+software--to make sure the software is free for all its users.  This
+General Public License applies to most of the Free Software
+Foundation's software and to any other program whose authors commit to
+using it.  (Some other Free Software Foundation software is covered by
+the GNU Library General Public License instead.)  You can apply it to
+your programs, too.
+
+  When we speak of free software, we are referring to freedom, not
+price.  Our General Public Licenses are designed to make sure that you
+have the freedom to distribute copies of free software (and charge for
+this service if you wish), that you receive source code or can get it
+if you want it, that you can change the software or use pieces of it
+in new free programs; and that you know you can do these things.
+
+  To protect your rights, we need to make restrictions that forbid
+anyone to deny you these rights or to ask you to surrender the rights.
+These restrictions translate to certain responsibilities for you if you
+distribute copies of the software, or if you modify it.
+
+  For example, if you distribute copies of such a program, whether
+gratis or for a fee, you must give the recipients all the rights that
+you have.  You must make sure that they, too, receive or can get the
+source code.  And you must show them these terms so they know their
+rights.
+
+  We protect your rights with two steps: (1) copyright the software, and
+(2) offer you this license which gives you legal permission to copy,
+distribute and/or modify the software.
+
+  Also, for each author's protection and ours, we want to make certain
+that everyone understands that there is no warranty for this free
+software.  If the software is modified by someone else and passed on, we
+want its recipients to know that what they have is not the original, so
+that any problems introduced by others will not reflect on the original
+authors' reputations.
+
+  Finally, any free program is threatened constantly by software
+patents.  We wish to avoid the danger that redistributors of a free
+program will individually obtain patent licenses, in effect making the
+program proprietary.  To prevent this, we have made it clear that any
+patent must be licensed for everyone's free use or not licensed at all.
+
+  The precise terms and conditions for copying, distribution and
+modification follow.
+\f
+                   GNU GENERAL PUBLIC LICENSE
+   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
+
+  0. This License applies to any program or other work which contains
+a notice placed by the copyright holder saying it may be distributed
+under the terms of this General Public License.  The "Program", below,
+refers to any such program or work, and a "work based on the Program"
+means either the Program or any derivative work under copyright law:
+that is to say, a work containing the Program or a portion of it,
+either verbatim or with modifications and/or translated into another
+language.  (Hereinafter, translation is included without limitation in
+the term "modification".)  Each licensee is addressed as "you".
+
+Activities other than copying, distribution and modification are not
+covered by this License; they are outside its scope.  The act of
+running the Program is not restricted, and the output from the Program
+is covered only if its contents constitute a work based on the
+Program (independent of having been made by running the Program).
+Whether that is true depends on what the Program does.
+
+  1. You may copy and distribute verbatim copies of the Program's
+source code as you receive it, in any medium, provided that you
+conspicuously and appropriately publish on each copy an appropriate
+copyright notice and disclaimer of warranty; keep intact all the
+notices that refer to this License and to the absence of any warranty;
+and give any other recipients of the Program a copy of this License
+along with the Program.
+
+You may charge a fee for the physical act of transferring a copy, and
+you may at your option offer warranty protection in exchange for a fee.
+
+  2. You may modify your copy or copies of the Program or any portion
+of it, thus forming a work based on the Program, and copy and
+distribute such modifications or work under the terms of Section 1
+above, provided that you also meet all of these conditions:
+
+    a) You must cause the modified files to carry prominent notices
+    stating that you changed the files and the date of any change.
+
+    b) You must cause any work that you distribute or publish, that in
+    whole or in part contains or is derived from the Program or any
+    part thereof, to be licensed as a whole at no charge to all third
+    parties under the terms of this License.
+
+    c) If the modified program normally reads commands interactively
+    when run, you must cause it, when started running for such
+    interactive use in the most ordinary way, to print or display an
+    announcement including an appropriate copyright notice and a
+    notice that there is no warranty (or else, saying that you provide
+    a warranty) and that users may redistribute the program under
+    these conditions, and telling the user how to view a copy of this
+    License.  (Exception: if the Program itself is interactive but
+    does not normally print such an announcement, your work based on
+    the Program is not required to print an announcement.)
+\f
+These requirements apply to the modified work as a whole.  If
+identifiable sections of that work are not derived from the Program,
+and can be reasonably considered independent and separate works in
+themselves, then this License, and its terms, do not apply to those
+sections when you distribute them as separate works.  But when you
+distribute the same sections as part of a whole which is a work based
+on the Program, the distribution of the whole must be on the terms of
+this License, whose permissions for other licensees extend to the
+entire whole, and thus to each and every part regardless of who wrote it.
+
+Thus, it is not the intent of this section to claim rights or contest
+your rights to work written entirely by you; rather, the intent is to
+exercise the right to control the distribution of derivative or
+collective works based on the Program.
+
+In addition, mere aggregation of another work not based on the Program
+with the Program (or with a work based on the Program) on a volume of
+a storage or distribution medium does not bring the other work under
+the scope of this License.
+
+  3. You may copy and distribute the Program (or a work based on it,
+under Section 2) in object code or executable form under the terms of
+Sections 1 and 2 above provided that you also do one of the following:
+
+    a) Accompany it with the complete corresponding machine-readable
+    source code, which must be distributed under the terms of Sections
+    1 and 2 above on a medium customarily used for software interchange; or,
+
+    b) Accompany it with a written offer, valid for at least three
+    years, to give any third party, for a charge no more than your
+    cost of physically performing source distribution, a complete
+    machine-readable copy of the corresponding source code, to be
+    distributed under the terms of Sections 1 and 2 above on a medium
+    customarily used for software interchange; or,
+
+    c) Accompany it with the information you received as to the offer
+    to distribute corresponding source code.  (This alternative is
+    allowed only for noncommercial distribution and only if you
+    received the program in object code or executable form with such
+    an offer, in accord with Subsection b above.)
+
+The source code for a work means the preferred form of the work for
+making modifications to it.  For an executable work, complete source
+code means all the source code for all modules it contains, plus any
+associated interface definition files, plus the scripts used to
+control compilation and installation of the executable.  However, as a
+special exception, the source code distributed need not include
+anything that is normally distributed (in either source or binary
+form) with the major components (compiler, kernel, and so on) of the
+operating system on which the executable runs, unless that component
+itself accompanies the executable.
+
+If distribution of executable or object code is made by offering
+access to copy from a designated place, then offering equivalent
+access to copy the source code from the same place counts as
+distribution of the source code, even though third parties are not
+compelled to copy the source along with the object code.
+\f
+  4. You may not copy, modify, sublicense, or distribute the Program
+except as expressly provided under this License.  Any attempt
+otherwise to copy, modify, sublicense or distribute the Program is
+void, and will automatically terminate your rights under this License.
+However, parties who have received copies, or rights, from you under
+this License will not have their licenses terminated so long as such
+parties remain in full compliance.
+
+  5. You are not required to accept this License, since you have not
+signed it.  However, nothing else grants you permission to modify or
+distribute the Program or its derivative works.  These actions are
+prohibited by law if you do not accept this License.  Therefore, by
+modifying or distributing the Program (or any work based on the
+Program), you indicate your acceptance of this License to do so, and
+all its terms and conditions for copying, distributing or modifying
+the Program or works based on it.
+
+  6. Each time you redistribute the Program (or any work based on the
+Program), the recipient automatically receives a license from the
+original licensor to copy, distribute or modify the Program subject to
+these terms and conditions.  You may not impose any further
+restrictions on the recipients' exercise of the rights granted herein.
+You are not responsible for enforcing compliance by third parties to
+this License.
+
+  7. If, as a consequence of a court judgment or allegation of patent
+infringement or for any other reason (not limited to patent issues),
+conditions are imposed on you (whether by court order, agreement or
+otherwise) that contradict the conditions of this License, they do not
+excuse you from the conditions of this License.  If you cannot
+distribute so as to satisfy simultaneously your obligations under this
+License and any other pertinent obligations, then as a consequence you
+may not distribute the Program at all.  For example, if a patent
+license would not permit royalty-free redistribution of the Program by
+all those who receive copies directly or indirectly through you, then
+the only way you could satisfy both it and this License would be to
+refrain entirely from distribution of the Program.
+
+If any portion of this section is held invalid or unenforceable under
+any particular circumstance, the balance of the section is intended to
+apply and the section as a whole is intended to apply in other
+circumstances.
+
+It is not the purpose of this section to induce you to infringe any
+patents or other property right claims or to contest validity of any
+such claims; this section has the sole purpose of protecting the
+integrity of the free software distribution system, which is
+implemented by public license practices.  Many people have made
+generous contributions to the wide range of software distributed
+through that system in reliance on consistent application of that
+system; it is up to the author/donor to decide if he or she is willing
+to distribute software through any other system and a licensee cannot
+impose that choice.
+
+This section is intended to make thoroughly clear what is believed to
+be a consequence of the rest of this License.
+\f
+  8. If the distribution and/or use of the Program is restricted in
+certain countries either by patents or by copyrighted interfaces, the
+original copyright holder who places the Program under this License
+may add an explicit geographical distribution limitation excluding
+those countries, so that distribution is permitted only in or among
+countries not thus excluded.  In such case, this License incorporates
+the limitation as if written in the body of this License.
+
+  9. The Free Software Foundation may publish revised and/or new versions
+of the General Public License from time to time.  Such new versions will
+be similar in spirit to the present version, but may differ in detail to
+address new problems or concerns.
+
+Each version is given a distinguishing version number.  If the Program
+specifies a version number of this License which applies to it and "any
+later version", you have the option of following the terms and conditions
+either of that version or of any later version published by the Free
+Software Foundation.  If the Program does not specify a version number of
+this License, you may choose any version ever published by the Free Software
+Foundation.
+
+  10. If you wish to incorporate parts of the Program into other free
+programs whose distribution conditions are different, write to the author
+to ask for permission.  For software which is copyrighted by the Free
+Software Foundation, write to the Free Software Foundation; we sometimes
+make exceptions for this.  Our decision will be guided by the two goals
+of preserving the free status of all derivatives of our free software and
+of promoting the sharing and reuse of software generally.
+
+                           NO WARRANTY
+
+  11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
+FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW.  EXCEPT WHEN
+OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
+PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
+OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS
+TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE
+PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
+REPAIR OR CORRECTION.
+
+  12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
+WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
+REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
+INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
+OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
+TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
+YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
+PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGES.
+
+                    END OF TERMS AND CONDITIONS
+\f
+           How to Apply These Terms to Your New Programs
+
+  If you develop a new program, and you want it to be of the greatest
+possible use to the public, the best way to achieve this is to make it
+free software which everyone can redistribute and change under these terms.
+
+  To do so, attach the following notices to the program.  It is safest
+to attach them to the start of each source file to most effectively
+convey the exclusion of warranty; and each file should have at least
+the "copyright" line and a pointer to where the full notice is found.
+
+    <one line to give the program's name and a brief idea of what it does.>
+    Copyright (C) <year>  <name of author>
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program; if not, write to the Free Software
+    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+
+
+Also add information on how to contact you by electronic and paper mail.
+
+If the program is interactive, make it output a short notice like this
+when it starts in an interactive mode:
+
+    Gnomovision version 69, Copyright (C) year name of author
+    Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
+    This is free software, and you are welcome to redistribute it
+    under certain conditions; type `show c' for details.
+
+The hypothetical commands `show w' and `show c' should show the appropriate
+parts of the General Public License.  Of course, the commands you use may
+be called something other than `show w' and `show c'; they could even be
+mouse-clicks or menu items--whatever suits your program.
+
+You should also get your employer (if you work as a programmer) or your
+school, if any, to sign a "copyright disclaimer" for the program, if
+necessary.  Here is a sample; alter the names:
+
+  Yoyodyne, Inc., hereby disclaims all copyright interest in the program
+  `Gnomovision' (which makes passes at compilers) written by James Hacker.
+
+  <signature of Ty Coon>, 1 April 1989
+  Ty Coon, President of Vice
+
+This General Public License does not permit incorporating your program into
+proprietary programs.  If your program is a subroutine library, you may
+consider it more useful to permit linking proprietary applications with the
+library.  If this is what you want to do, use the GNU Library General
+Public License instead of this License.
diff --git a/LICENSE.opendmarc b/LICENSE.opendmarc
new file mode 100644 (file)
index 0000000..e2ba06b
--- /dev/null
@@ -0,0 +1,29 @@
+Copyright (c) 2009, 2010, 2012, The Trusted Domain Project.
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+    * Redistributions of source code must retain the above copyright
+      notice, this list of conditions and the following disclaimer.
+    * Redistributions in binary form must reproduce the above copyright
+      notice, this list of conditions and the following disclaimer in the
+      documentation and/or other materials provided with the distribution.
+    * Neither the name of The Trusted Domain Project nor the names of its
+      contributors may be used to endorse or promote products derived from
+      this software without specific prior written permission.
+
+Portions of this project are also covered by the Sendmail Open Source
+License, available in this distribution in the file "LICENSE.Sendmail".
+See the copyright notice(s) in each file to determine whether or not it is
+covered by both licenses.
+
+THIS SOFTWARE IS PROVIDED BY THE OPENDKIM PROJECT ''AS IS'' AND ANY
+EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+DISCLAIMED. IN NO EVENT SHALL THE OPENDKIM PROJECT BE LIABLE FOR ANY
+DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
diff --git a/Makefile b/Makefile
new file mode 100644 (file)
index 0000000..99f4ab3
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,101 @@
+# Top-level makefile for Exim; handles creating a build directory with
+# appropriate links, and then creating and running the main makefile in that
+# directory.
+
+# Copyright (c) University of Cambridge, 1995 - 2014
+# See the file NOTICE for conditions of use and distribution.
+
+# IRIX make uses the shell that is in the SHELL variable, which often defaults
+# to csh, so put this in to make it use the Bourne shell. In systems where
+# /bin/sh is not a Bourne-compatible shell, this line will have to be edited,
+# or "make" must be called with a different SHELL= setting.
+
+SHELL=/bin/sh
+RM_COMMAND=/bin/rm
+
+# If a build name has not been specified by running this make file via a
+# command of the form "make build=xxxx", then determine the name of the
+# operating system and the machine architecture and use that. This does not
+# provide an override for the OS type and architecture type; they still have
+# to be used for the OS-specific files. To override them, you can set the
+# shell variables OSTYPE and ARCHTYPE when running make.
+
+buildname=$${build:-`$(SHELL) scripts/os-type`-`$(SHELL) scripts/arch-type`}
+
+# The default target checks for the existence of Local/Makefile, that the main
+# makefile is built and up-to-date, and then it runs it.
+
+all: Local/Makefile configure
+       @cd build-$(buildname); $(MAKE) SHELL=$(SHELL) $(MFLAGS)
+
+Local/Makefile:
+       @echo ""
+       @echo "*** Please create Local/Makefile by copying src/EDITME and making"
+       @echo "*** appropriate changes for your site."
+       @echo ""
+       @test ! -d Local && mkdir Local
+       @false
+
+# This is separated off so that "make build-directory" can be obeyed on
+# its own if necessary.
+
+build-directory:
+       @builddir=build-$(buildname); \
+       case "$$builddir" in *UnKnown*) exit 1;; esac; \
+       $(SHELL) -c "test -d $$builddir -a -r $$builddir/version.c || \
+         (mkdir $$builddir; cd $$builddir; $(SHELL) ../scripts/MakeLinks)";
+
+checks:
+       $(SHELL) scripts/source_checks
+
+# The "configure" target ensures that the build directory exists, then arranges
+# to build the main makefile from inside the build directory, by calling the
+# Configure-Makefile script. This does its own dependency checking because of
+# the optional files.
+
+configure: checks build-directory
+       @cd build-$(buildname); \
+         build=$(build) $(SHELL) ../scripts/Configure-Makefile
+
+# The "makefile" target forces a rebuild of the makefile (as opposed to
+# "configure", which doesn't force it).
+
+makefile: build-directory
+       @cd build-$(buildname); $(RM_COMMAND) -f Makefile; \
+         build=$(build) $(SHELL) ../scripts/Configure-Makefile
+
+# The installation commands are kept in a separate script, which expects
+# to be run from inside the build directory.
+
+install:        all
+               @cd build-$(buildname); \
+               build=$(build) $(SHELL) ../scripts/exim_install $(INSTALL_ARG)
+
+# Tidy-up targets
+
+clean:; @echo ""; echo '*** "make clean" just removes all .o and .a files'
+       @echo '*** Use "make makefile" to force a rebuild of the makefile'
+       @echo ""
+       cd build-$(buildname); \
+       $(RM_COMMAND) -f *.o lookups/*.o lookups/*.a auths/*.o auths/*.a \
+       routers/*.o routers/*.a transports/*.o transports/*.a \
+       pdkim/*.o pdkim/*.a
+
+clean_exim:; cd build-$(buildname); \
+        $(RM_COMMAND) -f *.o lookups/*.o lookups/*.a auths/*.o auths/*.a \
+       routers/*.o routers/*.a transports/*.o transports/*.a lookups/*.so
+
+distclean:; $(RM_COMMAND) -rf build-* cscope*
+
+cscope.files: FRC
+       echo "-q" > $@
+       echo "-p3" >> $@
+       find src Local OS -name "*.[cshyl]" -print \
+                   -o -name "os.h*" -print \
+                   -o -name "*akefile*" -print \
+                   -o -name EDITME -print >> $@
+       ls OS/* >> $@
+
+FRC:
+
+# End of top-level makefile
diff --git a/NOTICE b/NOTICE
new file mode 100644 (file)
index 0000000..b4a5c40
--- /dev/null
+++ b/NOTICE
@@ -0,0 +1,152 @@
+THE EXIM MAIL TRANSFER AGENT
+----------------------------
+
+Copyright (c) 2004 University of Cambridge
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+In addition, for the avoidance of any doubt, permission is granted to
+link this program with OpenSSL or any other library package and to
+(re)distribute the binaries produced as the result of such linking.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+
+UNSOLICITED EMAIL
+-----------------
+
+The use, supply or promotion of Exim for the purpose of sending bulk,
+unsolicited electronic mail is incompatible with the basic aims of the program,
+which revolve around the free provision of a service that enhances the quality
+of personal communications. The author of Exim regards indiscriminate
+mass-mailing as an antisocial, irresponsible abuse of the Internet.
+
+
+INCORPORATED CODE
+-----------------
+
+A number of pieces of external code are included in the Exim distribution.
+
+ .   Support for the cdb (Constant DataBase) lookup method is provided by code
+     contributed by Nigel Metheringham of Planet Online Ltd. which contains
+     the following statements:
+     _________________________________________________________________________
+
+     Copyright (c) 1998 Nigel Metheringham, Planet Online Ltd
+
+     This program is free software; you can redistribute it and/or modify it
+     under the terms of the GNU General Public License as published by the
+     Free Software Foundation; either version 2 of the License, or (at your
+     option) any later version.
+
+     This code implements Dan Bernstein's Constant DataBase (cdb) spec.
+     Information, the spec and sample code for cdb can be obtained from
+     http://www.pobox.com/~djb/cdb.html. This implementation borrows some code
+     from Dan Bernstein's implementation (which has no license restrictions
+     applied to it).
+     _________________________________________________________________________
+
+     The implementation is completely contained within the code of Exim. It
+     does not link against an external cdb library.
+
+ .   Client support for Microsoft's "Secure Password Authentication" is pro-
+     vided by code contributed by Marc Prud'hommeaux. Server support was
+     contributed by Tom Kistner. This includes code taken from the Samba
+     project, which is released under the Gnu GPL.
+
+
+ .   Support for calling the Cyrus "pwcheck" and "saslauthd" daemons is
+     provided by code taken from the Cyrus-SASL library and adapted by
+     Alexander S. Sabourenkov. The permission notice appears below, in
+     accordance with the conditions expressed therein.
+
+     _________________________________________________________________________
+
+     Copyright (c) 2001 Carnegie Mellon University. All rights reserved.
+
+     Redistribution and use in source and binary forms, with or without
+     modification, are permitted provided that the following conditions are
+     met:
+
+     1.   Redistributions of source code must retain the above copyright
+          notice, this list of conditions and the following disclaimer.
+
+     2.   Redistributions in binary form must reproduce the above copyright
+          notice, this list of conditions and the following disclaimer in the
+          documentation and/or other materials provided with the distribution.
+
+     3.   The name 'Carnegie Mellon University' must not be used to endorse or
+          promote products derived from this software without prior written
+          permission. For permission or any other legal details, please
+          contact
+
+            Office of Technology Transfer
+            Carnegie Mellon University
+            5000 Forbes Avenue
+            Pittsburgh, PA  15213-3890
+            (412) 268-4387, fax: (412) 268-7395
+            tech-transfer@andrew.cmu.edu
+
+     4.   Redistributions of any form whatsoever must retain the following
+          acknowledgment:
+             This product includes software developed by Computing Services at
+             Carnegie Mellon University (http://www.cmu.edu/computing/).
+
+     CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
+     SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
+     FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE FOR ANY
+     SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
+     RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
+     CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
+     CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+     _________________________________________________________________________
+
+
+ .   The Exim Monitor program, which is an X-Window application, includes
+     modified versions of the Athena StripChart and TextPop widgets. This code
+     is copyright by DEC and MIT, and their permission notice appears below,
+     in accordance with the conditions expressed therein.
+
+     _________________________________________________________________________
+
+     Copyright 1987, 1988 by Digital Equipment Corporation, Maynard,
+     Massachusetts, and the Massachusetts Institute of Technology, Cambridge,
+     Massachusetts.
+
+                                All Rights Reserved
+
+     Permission to use, copy, modify, and distribute this software and its
+     documentation for any purpose and without fee is hereby granted, provided
+     that the above copyright notice appear in all copies and that both that
+     copyright notice and this permission notice appear in supporting documen-
+     tation, and that the names of Digital or MIT not be used in advertising
+     or publicity pertaining to distribution of the software without specific,
+     written prior permission.
+
+     DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
+     ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
+     DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
+     ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
+     WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
+     ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
+     SOFTWARE.
+     _________________________________________________________________________
+
+
+ .   Some of the code to support the use of maildirsize files for maildir
+     deliveries is taken from the Courier Imapd source code. This code is
+     released under the GPL.
+     _________________________________________________________________________
+
+--
+Philip Hazel            University of Cambridge Computing Service,
diff --git a/OS/Makefile-AIX b/OS/Makefile-AIX
new file mode 100644 (file)
index 0000000..fc32aa2
--- /dev/null
@@ -0,0 +1,28 @@
+# Exim: OS-specific make file for AIX
+# Written by Nick Waterman (nick@cimio.co.uk)
+# Modified by PH following a message from Mike Meredith
+
+# Note that the output of uname -m is probably not what Philip expected,
+# so you might end up with more build-AIX-random_number directories than
+# you expected if you have too many AIX boxes, but it seems to work... I
+# blame IBM.
+
+# Note that nowadays you have to pay extra for a cc compiler with AIX!
+
+CC=gcc
+
+# This needs to be in here rather than os.h-AIX because of regexp stuff.
+# basically strchr is a #define, which means "extern char *strchr()"
+# ruins things. __STR31__ seems to get around this by magic. The AIX
+# include files are quite a confusing maze.
+# Mike M says this is not necessary any more; possibly this is related to
+# using gcc. Commented out by PH.
+#CFLAGS = -D__STR31__
+
+CFLAGS = -mcpu=power4 -maix64 -O3
+
+# Needed for vfork() and vfork() only?
+
+LIBS = -lbsd -lm
+
+# End
diff --git a/OS/Makefile-BSDI b/OS/Makefile-BSDI
new file mode 100644 (file)
index 0000000..d56aa9b
--- /dev/null
@@ -0,0 +1,21 @@
+# Exim: OS-specific make file for BSDI aka BSD/OS. Its antique link editor
+# cannot handle the TextPop overriding.
+
+CFLAGS=-O
+CHOWN_COMMAND=/usr/sbin/chown
+
+HAVE_SA_LEN=YES
+
+X11=/usr/X11
+XINCLUDE=-I$(X11)/include
+XLFLAGS=-L$(X11)/lib
+X11_LD_LIB=$(X11)/lib
+
+LIBS_EXIMON=-lSM -lICE -lipc -lm
+EXIMON_TEXTPOP=
+
+EXIWHAT_PS_ARG=-ax
+EXIWHAT_EGREP_ARG='/exim( |$$)'
+EXIWHAT_KILL_SIGNAL=-USR1
+
+# End
diff --git a/OS/Makefile-Base b/OS/Makefile-Base
new file mode 100644 (file)
index 0000000..87a8037
--- /dev/null
@@ -0,0 +1,756 @@
+# This file is the basis of the main makefile for Exim and friends. The
+# makefile at the top level arranges to build the main makefile by calling
+# scripts/Configure-Makefile from within the build directory. This
+# concatentates the configuration settings from Local/Makefile and other,
+# optional, Local/* files at the front of this file, to create Makefile in the
+# build directory.
+
+SHELL      = $(MAKE_SHELL)
+SCRIPTS    = ../scripts
+EDITME     = ../Local/Makefile
+EXIMON_EDITME = ../Local/eximon.conf
+
+# The compiler used for linking is normally the same as the compiler used for
+# compiling. However, by giving it a different name, we can override it from
+# the command line, and this is helpful for certain types of testing.
+
+LNCC = $(CC)
+
+# The compile commands can be very long. To make the output look better,
+# they are not normally echoed in full. To get full echoing, the caller
+# must set FULLECHO='' on the command line and call make with -e. We default
+# FULLECHO to '@' to suppress the full echo. Then define an abbreviation.
+
+FULLECHO = @
+FE       = $(FULLECHO)
+
+# The default target double-checks the existence of $(EDITME) and then arranges
+# to touch it if it exists and any of the optional configuration files, which
+# depend on the os or the architecture, have been altered. The same sub-target
+# does the same thing for the eximon configuration file if it exists. Then
+# there is a check that the Makefile (the one built from this file) is
+# up-to-date. Then the os-specific source files and the C configuration file
+# are set up, and finally it goes to the main Exim target.
+
+all:       allexim
+config:    $(EDITME) checklocalmake Makefile os.h os.c config.h version.h
+
+checklocalmake:
+       @if $(SHELL) $(SCRIPTS)/newer $(EDITME)-$(OSTYPE) $(EDITME) || \
+         $(SHELL) $(SCRIPTS)/newer $(EDITME)-$(ARCHTYPE) $(EDITME) || \
+         $(SHELL) $(SCRIPTS)/newer $(EDITME)-$(OSTYPE)-$(ARCHTYPE) $(EDITME); \
+       then \
+         touch $(EDITME); \
+       fi
+       @if $(SHELL) $(SCRIPTS)/newer $(EXIMON_EDITME)-$(OSTYPE) $(EXIMON_EDITME) || \
+         $(SHELL) $(SCRIPTS)/newer $(EXIMON_EDITME)-$(ARCHTYPE) $(EXIMON_EDITME) || \
+         $(SHELL) $(SCRIPTS)/newer $(EXIMON_EDITME)-$(OSTYPE)-$(ARCHTYPE) $(EXIMON_EDITME); \
+       then \
+         if [ -f $(EXIMON_EDITME) ]; then touch $(EXIMON_EDITME); fi \
+       fi
+
+$(EDITME):
+       @echo " "
+       @echo "*** Please create Local/Makefile by copying src/EDITME and making"
+       @echo "*** appropriate changes for your site."
+       @echo " "
+       @false
+
+$(EXIMON_EDITME):
+       @echo " "
+       @echo "*** Please create Local/eximon.conf by copying exim_monitor/EDITME and making"
+       @echo "*** appropriate changes for your site."
+       @echo " "
+       @test ! -d ../Local && mkdir ../Local
+       @false
+
+# Check that the local Makefile is up-to-date
+
+Makefile: ../OS/Makefile-Base ../OS/Makefile-Default \
+          $(SCRIPTS)/Configure $(SCRIPTS)/Configure-Makefile $(EDITME)
+       @echo " "
+       @echo "*** Makefile needs rebuilding"
+       @echo "*** Please run \"make makefile\" at top level"
+       @echo " "
+       @false
+
+# Build (link) the os.h file
+
+os.h:
+       $(SHELL) $(SCRIPTS)/Configure-os.h
+
+# Build the os.c file
+
+os.c:   ../src/os.c
+       $(SHELL) $(SCRIPTS)/Configure-os.c
+
+# Build the config.h file.
+
+config.h: Makefile buildconfig ../src/config.h.defaults $(EDITME)
+       $(SHELL) $(SCRIPTS)/Configure-config.h "$(MAKE)"
+
+
+# This target is recognized specially by GNU make. It records those targets
+# that do not correspond to files that are being built and which should
+# therefore always be run, even if the files exist. This shouldn't in fact be a
+# problem, but it does no harm. Other make programs will just ignore this.
+
+.PHONY: all config allexim buildauths buildlookups buildpdkim buildrouters \
+        buildtransports checklocalmake clean
+
+
+# This is the real default target for all the various exim binaries and
+# scripts, once the configuring stuff is done.
+
+allexim: $(EXIM_MONITOR) exicyclog exinext exiwhat \
+        exigrep eximstats exipick exiqgrep exiqsumm \
+        transport-filter.pl convert4r3 convert4r4 \
+        exim_checkaccess \
+        exim_dbmbuild exim_dumpdb exim_fixdb exim_tidydb exim_lock \
+        exim
+
+
+# Targets for special-purpose configuration header builders
+buildconfig: buildconfig.c
+       @echo "$(CC) buildconfig.c"
+       $(FE)$(CC) $(CFLAGS) $(INCLUDE) -o buildconfig buildconfig.c $(LIBS)
+
+
+# Target for the exicyclog utility script
+exicyclog: config ../src/exicyclog.src
+       @rm -f exicyclog
+       @sed \
+         -e "s?PROCESSED_FLAG?This file has been so processed.?"\
+         -e "/^# /p" \
+         -e "/^# /d" \
+         -e "s?CONFIGURE_FILE_USE_NODE?$(CONFIGURE_FILE_USE_NODE)?" \
+         -e "s?CONFIGURE_FILE_USE_EUID?$(CONFIGURE_FILE_USE_EUID)?" \
+         -e "s?CONFIGURE_FILE?$(CONFIGURE_FILE)?" \
+         -e "s?BIN_DIRECTORY?$(BIN_DIRECTORY)?" \
+         -e "s?EXICYCLOG_MAX?$(EXICYCLOG_MAX)?" \
+         -e "s?COMPRESS_COMMAND?$(COMPRESS_COMMAND)?" \
+         -e "s?COMPRESS_SUFFIX?$(COMPRESS_SUFFIX)?" \
+         -e "s?CHGRP_COMMAND?$(CHGRP_COMMAND)?" \
+         -e "s?CHMOD_COMMAND?$(CHMOD_COMMAND)?" \
+         -e "s?CHOWN_COMMAND?$(CHOWN_COMMAND)?" \
+         -e "s?MV_COMMAND?$(MV_COMMAND)?" \
+         -e "s?RM_COMMAND?$(RM_COMMAND)?" \
+         -e "s?TOUCH_COMMAND?$(TOUCH_COMMAND)?" \
+         ../src/exicyclog.src > exicyclog-t
+       @mv exicyclog-t exicyclog
+       @chmod a+x exicyclog
+       @echo ">>> exicyclog script built"
+
+# Target for the exinext utility script
+exinext: config ../src/exinext.src
+       @rm -f exinext
+       @sed \
+         -e "s?PROCESSED_FLAG?This file has been so processed.?"\
+         -e "/^# /p" \
+         -e "/^# /d" \
+         -e "s?CONFIGURE_FILE_USE_NODE?$(CONFIGURE_FILE_USE_NODE)?" \
+         -e "s?CONFIGURE_FILE?$(CONFIGURE_FILE)?" \
+         -e "s?BIN_DIRECTORY?$(BIN_DIRECTORY)?" \
+         ../src/exinext.src > exinext-t
+       @mv exinext-t exinext
+       @chmod a+x exinext
+       @echo ">>> exinext script built"
+
+# Target for the exiwhat utility script
+exiwhat: config ../src/exiwhat.src
+       @rm -f exiwhat
+       @sed \
+         -e "s?PROCESSED_FLAG?This file has been so processed.?"\
+         -e "/^# /p" \
+         -e "/^# /d" \
+         -e "s?CONFIGURE_FILE_USE_NODE?$(CONFIGURE_FILE_USE_NODE)?" \
+         -e "s?CONFIGURE_FILE?$(CONFIGURE_FILE)?" \
+         -e "s?BIN_DIRECTORY?$(BIN_DIRECTORY)?" \
+         -e "s?EXIWHAT_PS_CMD?$(EXIWHAT_PS_CMD)?" \
+         -e "s?EXIWHAT_PS_ARG?$(EXIWHAT_PS_ARG)?" \
+         -e "s?EXIWHAT_KILL_SIGNAL?$(EXIWHAT_KILL_SIGNAL)?" \
+         -e "s?EXIWHAT_EGREP_ARG?$(EXIWHAT_EGREP_ARG)?" \
+         -e "s?EXIWHAT_MULTIKILL_CMD?$(EXIWHAT_MULTIKILL_CMD)?" \
+         -e "s?EXIWHAT_MULTIKILL_ARG?$(EXIWHAT_MULTIKILL_ARG)?" \
+         ../src/exiwhat.src > exiwhat-t
+       @mv exiwhat-t exiwhat
+       @chmod a+x exiwhat
+       @echo ">>> exiwhat script built"
+
+# Target for the exim_checkaccess utility script
+exim_checkaccess: config ../src/exim_checkaccess.src
+       @rm -f exim_checkaccess
+       @sed \
+         -e "s?PROCESSED_FLAG?This file has been so processed.?"\
+         -e "/^# /p" \
+         -e "/^# /d" \
+         -e "s?CONFIGURE_FILE_USE_NODE?$(CONFIGURE_FILE_USE_NODE)?" \
+         -e "s?CONFIGURE_FILE?$(CONFIGURE_FILE)?" \
+         -e "s?BIN_DIRECTORY?$(BIN_DIRECTORY)?" \
+         -e "s?PERL_COMMAND?$(PERL_COMMAND)?" \
+         ../src/exim_checkaccess.src > exim_checkaccess-t
+       @mv exim_checkaccess-t exim_checkaccess
+       @chmod a+x exim_checkaccess
+       @echo ">>> exim_checkaccess script built"; echo ""
+
+# Target for the Exim monitor start-up script
+eximon: config ../src/eximon.src ../OS/eximon.conf-Default \
+          ../Local/eximon.conf
+       @rm -f eximon
+       $(SHELL) $(SCRIPTS)/Configure-eximon
+       @sed \
+         -e "s?PROCESSED_FLAG?This file has been so processed.?"\
+         -e "/^# /p" \
+         -e "/^# /d" \
+         -e "s?CONFIGURE_FILE_USE_NODE?$(CONFIGURE_FILE_USE_NODE)?" \
+         -e "s?CONFIGURE_FILE?$(CONFIGURE_FILE)?" \
+         -e "s?BIN_DIRECTORY?$(BIN_DIRECTORY)?" \
+         -e "s?BASENAME_COMMAND?$(BASENAME_COMMAND)?" \
+         -e "s?HOSTNAME_COMMAND?$(HOSTNAME_COMMAND)?" \
+         -e "s?X11_LD_LIBRARY?$(X11_LD_LIB)?" \
+         ../src/eximon.src >> eximon
+       @echo ">>> eximon script built"; echo ""
+
+# Targets for utilities; these are all Perl scripts that have to get the
+# location of Perl put in them. A few need other things as well.
+
+exigrep: Makefile ../src/exigrep.src
+       @rm -f exigrep
+       @sed \
+         -e "s?PROCESSED_FLAG?This file has been so processed.?"\
+         -e "/^# /p" \
+         -e "/^# /d" \
+         -e "s?PERL_COMMAND?$(PERL_COMMAND)?" \
+         -e "s?ZCAT_COMMAND?$(ZCAT_COMMAND)?" \
+          -e "s?COMPRESS_SUFFIX?$(COMPRESS_SUFFIX)?" \
+         ../src/exigrep.src > exigrep-t
+       @mv exigrep-t exigrep
+       @chmod a+x exigrep
+       @echo ">>> exigrep script built"
+
+eximstats: Makefile ../src/eximstats.src
+       @rm -f eximstats
+       @sed \
+         -e "s?PERL_COMMAND?$(PERL_COMMAND)?" \
+         ../src/eximstats.src > eximstats-t
+       @mv eximstats-t eximstats
+       @chmod a+x eximstats
+       @echo ">>> eximstats script built"
+
+exiqgrep: Makefile ../src/exiqgrep.src
+       @rm -f exiqgrep
+       @sed \
+         -e "s?PROCESSED_FLAG?This file has been so processed.?"\
+         -e "/^# /p" \
+         -e "/^# /d" \
+         -e "s?BIN_DIRECTORY?$(BIN_DIRECTORY)?" \
+         -e "s?PERL_COMMAND?$(PERL_COMMAND)?" \
+         ../src/exiqgrep.src > exiqgrep-t
+       @mv exiqgrep-t exiqgrep
+       @chmod a+x exiqgrep
+       @echo ">>> exiqgrep script built"
+
+exiqsumm: Makefile ../src/exiqsumm.src
+       @rm -f exiqsumm
+       @sed -e "s?PERL_COMMAND?$(PERL_COMMAND)?" \
+         ../src/exiqsumm.src > exiqsumm-t
+       @mv exiqsumm-t exiqsumm
+       @chmod a+x exiqsumm
+       @echo ">>> exiqsumm script built"
+
+exipick: Makefile ../src/exipick.src
+       @rm -f exipick
+       @sed -e "s?PERL_COMMAND?$(PERL_COMMAND)?" \
+         -e "s?SPOOL_DIRECTORY?$(SPOOL_DIRECTORY)?" \
+         ../src/exipick.src > exipick-t
+       @mv exipick-t exipick
+       @chmod a+x exipick
+       @echo ">>> exipick script built"
+
+transport-filter.pl: Makefile ../src/transport-filter.src
+       @rm -f transport-filter.pl
+       @sed -e "s?PERL_COMMAND?$(PERL_COMMAND)?" \
+         ../src/transport-filter.src > transport-filter.pl-t
+       @mv transport-filter.pl-t transport-filter.pl
+       @chmod a+x transport-filter.pl
+       @echo ">>> transport-filter.pl script built"
+
+convert4r3: Makefile ../src/convert4r3.src
+       @rm -f convert4r3
+       @sed -e "s?PERL_COMMAND?$(PERL_COMMAND)?" \
+         ../src/convert4r3.src > convert4r3-t
+       @mv convert4r3-t convert4r3
+       @chmod a+x convert4r3
+       @echo ">>> convert4r3 script built"
+
+convert4r4: Makefile ../src/convert4r4.src
+       @rm -f convert4r4
+       @sed -e "s?PERL_COMMAND?$(PERL_COMMAND)?" \
+         ../src/convert4r4.src > convert4r4-t
+       @mv convert4r4-t convert4r4
+       @chmod a+x convert4r4
+       @echo ">>> convert4r4 script built"
+
+
+# These are objects of optional features. They are always compiled, but
+# if the corresponding #defines are not set, they wind up empty and
+# are thrown away by the linker.
+
+OBJ_WITH_CONTENT_SCAN = malware.o mime.o regex.o spam.o spool_mbox.o
+OBJ_WITH_OLD_DEMIME = demime.o
+OBJ_EXPERIMENTAL = bmi_spam.o spf.o srs.o dcc.o dmarc.o
+
+# Targets for final binaries; the main one has a build number which is
+# updated each time. We don't bother with that for the auxiliaries.
+
+OBJ_LOOKUPS = lookups/lf_quote.o lookups/lf_check_file.o lookups/lf_sqlperform.o
+
+OBJ_EXIM = acl.o child.o crypt16.o daemon.o dbfn.o debug.o deliver.o \
+        directory.o dns.o drtables.o enq.o exim.o expand.o filter.o \
+        filtertest.o globals.o dkim.o \
+        header.o host.o ip.o log.o lss.o match.o moan.o \
+        os.o parse.o queue.o \
+        rda.o readconf.o receive.o retry.o rewrite.o rfc2047.o \
+        route.o search.o sieve.o smtp_in.o smtp_out.o spool_in.o spool_out.o \
+        std-crypto.o store.o string.o tls.o tod.o transport.o tree.o verify.o \
+        $(OBJ_LOOKUPS) \
+        local_scan.o $(EXIM_PERL) $(OBJ_WITH_CONTENT_SCAN) \
+        $(OBJ_WITH_OLD_DEMIME) $(OBJ_EXPERIMENTAL)
+
+exim:   buildlookups buildauths pdkim/pdkim.a \
+        buildrouters buildtransports \
+        $(OBJ_EXIM) version.o
+       @echo "$(LNCC) -o exim"
+       $(FE)$(PURIFY) $(LNCC) -o exim $(LFLAGS) $(OBJ_EXIM) version.o \
+         routers/routers.a transports/transports.a lookups/lookups.a \
+         auths/auths.a pdkim/pdkim.a \
+         $(LIBRESOLV) $(LIBS) $(LIBS_EXIM) $(IPV6_LIBS) $(EXTRALIBS) \
+         $(EXTRALIBS_EXIM) $(DBMLIB) $(LOOKUP_LIBS) $(AUTH_LIBS) \
+         $(PERL_LIBS) $(TLS_LIBS) $(PCRE_LIBS) $(LDFLAGS)
+       @if [ x"$(STRIP_COMMAND)" != x"" ]; then \
+         echo $(STRIP_COMMAND) exim; \
+         $(STRIP_COMMAND) exim; \
+       fi
+       $(EXIM_CHMOD)
+       @echo " "
+       @echo ">>> exim binary built"
+       @echo " "
+
+# The utility for dumping the contents of an exim database
+
+OBJ_DUMPDB = exim_dumpdb.o util-os.o util-store.o
+
+exim_dumpdb: $(OBJ_DUMPDB)
+       @echo "$(LNCC) -o exim_dumpdb"
+       $(FE)$(LNCC) $(CFLAGS) $(INCLUDE) -o exim_dumpdb $(LFLAGS) $(OBJ_DUMPDB) \
+         $(LIBS) $(EXTRALIBS) $(DBMLIB)
+       @if [ x"$(STRIP_COMMAND)" != x"" ]; then \
+         echo $(STRIP_COMMAND) exim_dumpdb; \
+         $(STRIP_COMMAND) exim_dumpdb; \
+       fi
+       @echo ">>> exim_dumpdb utility built"
+       @echo " "
+
+# The utility for interrogating/fixing the contents of an exim database
+
+OBJ_FIXDB = exim_fixdb.o util-os.o util-store.o
+
+exim_fixdb:  $(OBJ_FIXDB) buildauths
+       @echo "$(LNCC) -o exim_fixdb"
+       $(FE)$(LNCC) $(CFLAGS) $(INCLUDE) -o exim_fixdb $(LFLAGS) $(OBJ_FIXDB) \
+         auths/auths.a $(LIBS) $(EXTRALIBS) $(DBMLIB)
+       @if [ x"$(STRIP_COMMAND)" != x"" ]; then \
+         echo $(STRIP_COMMAND) exim_fixdb; \
+         $(STRIP_COMMAND) exim_fixdb; \
+       fi
+       @echo ">>> exim_fixdb utility built"
+       @echo " "
+
+# The utility for tidying the contents of an exim database
+
+OBJ_TIDYDB = exim_tidydb.o util-os.o util-store.o
+
+exim_tidydb: $(OBJ_TIDYDB)
+       @echo "$(LNCC) -o exim_tidydb"
+       $(FE)$(LNCC) $(CFLAGS) $(INCLUDE) -o exim_tidydb $(LFLAGS) $(OBJ_TIDYDB) \
+         $(LIBS) $(EXTRALIBS) $(DBMLIB)
+       @if [ x"$(STRIP_COMMAND)" != x"" ]; then \
+         echo $(STRIP_COMMAND) exim_tidydb; \
+         $(STRIP_COMMAND) exim_tidydb; \
+       fi
+       @echo ">>> exim_tidydb utility built"
+       @echo " "
+
+# The utility for building dbm files
+
+exim_dbmbuild: exim_dbmbuild.o
+       @echo "$(LNCC) -o exim_dbmbuild"
+       $(FE)$(LNCC) -o exim_dbmbuild $(LFLAGS) exim_dbmbuild.o \
+         $(LIBS) $(EXTRALIBS) $(DBMLIB)
+       @if [ x"$(STRIP_COMMAND)" != x"" ]; then \
+         echo $(STRIP_COMMAND) exim_dbmbuild; \
+         $(STRIP_COMMAND) exim_dbmbuild; \
+       fi
+       @echo ">>> exim_dbmbuild utility built"
+       @echo " "
+
+# The utility for locking a mailbox while messing around with it
+
+exim_lock: exim_lock.c
+       @echo "$(CC) exim_lock.c"
+       $(FE)$(CC) -c $(CFLAGS) $(INCLUDE) exim_lock.c
+       @echo "$(LNCC) -o exim_lock"
+       $(FE)$(LNCC) -o exim_lock $(LFLAGS) exim_lock.o  \
+         $(LIBS) $(EXTRALIBS)
+       @if [ x"$(STRIP_COMMAND)" != x"" ]; then \
+         echo $(STRIP_COMMAND) exim_lock; \
+         $(STRIP_COMMAND) exim_lock; \
+       fi
+       @echo ">>> exim_lock utility built"
+       @echo " "
+
+# The X-based Exim monitor program's binary part. There's a macro for cutting
+# out the modified TextPop module, because some antique link editors cannot
+# handle the fact that it is redefining things that are found later in the
+# Xaw library.
+
+# Object modules that are the unique Eximon modules
+
+MONBIN = em_StripChart.o $(EXIMON_TEXTPOP) em_globals.o em_init.o \
+       em_log.o em_main.o em_menu.o em_queue.o em_strip.o \
+       em_text.o em_xs.o
+
+# The complete modules list also includes some specially compiled versions of
+# code from the main Exim source tree.
+
+OBJ_MONBIN = util-spool_in.o util-store.o util-string.o tod.o tree.o $(MONBIN)
+
+eximon.bin: $(EXIMON_EDITME) eximon $(OBJ_MONBIN) \
+            ../exim_monitor/em_version.c
+       @echo "$(CC) exim_monitor/em_version.c"
+       $(FE)$(CC) -o em_version.o -c \
+         $(CFLAGS) $(XINCLUDE) -I. ../exim_monitor/em_version.c
+       @echo "$(LNCC) -o eximon.bin"
+       $(FE)$(PURIFY) $(LNCC) -o eximon.bin em_version.o $(LFLAGS) $(XLFLAGS) \
+       $(OBJ_MONBIN) -lXaw -lXmu -lXt -lXext -lX11 $(PCRE_LIBS) \
+         $(LIBS) $(LIBS_EXIMON) $(EXTRALIBS) $(EXTRALIBS_EXIMON) -lc
+       @if [ x"$(STRIP_COMMAND)" != x"" ]; then \
+         echo $(STRIP_COMMAND) eximon.bin; \
+         $(STRIP_COMMAND) eximon.bin; \
+       fi
+       @echo ">>> exim monitor binary built"
+       @echo " "
+
+
+# Compile step for most of the exim modules. HDRS is a list of headers
+# which cause everthing to be rebuilt. PHDRS is the same, for the use
+# of routers, transports, and authenticators. I can't find a way of doing this
+# in one. This list is overkill, but it doesn't really take much time to
+# rebuild Exim on a modern computer.
+
+HDRS  =    config.h    dbfunctions.h    dbstuff.h    exim.h    functions.h    globals.h    local_scan.h    macros.h    mytypes.h    structs.h
+PHDRS = ../config.h ../dbfunctions.h ../dbstuff.h ../exim.h ../functions.h ../globals.h ../local_scan.h ../macros.h ../mytypes.h ../structs.h
+
+.SUFFIXES: .o .c
+.c.o:;  @echo "$(CC) $*.c"
+       $(FE)$(CC) -c $(CFLAGS) -I. $(INCLUDE) $(IPV6_INCLUDE) $(TLS_INCLUDE) $*.c
+
+# Update Exim's version information and build the version object.
+
+version.h::
+       @../scripts/reversion
+
+cnumber.h: version.h
+
+version.o: $(HDRS) cnumber.h version.h version.c
+
+# This is the dummy module for use by test compiles of individual modules. It
+# contains functions such as log_write() that may be called from bits of Exim
+# in the tested code.
+
+dummies.o:       dummies.c
+
+# Compile instructions for perl.o for when EXIM_PERL is set
+
+perl.o:          $(HDRS) perl.c
+       @echo "$(PERL_CC) perl.c"
+       $(FE)$(PERL_CC) $(PERL_CCOPTS) $(CFLAGS) $(INCLUDE) -c perl.c
+
+# Compile instructions for the database utility modules
+
+exim_dumpdb.o:   $(HDRS) exim_dbutil.c
+       @echo "$(CC) -DEXIM_DUMPDB exim_dbutil.c"
+       $(FE)$(CC) -c $(CFLAGS) $(INCLUDE) \
+                                     -DCOMPILE_UTILITY \
+                                     -DEXIM_DUMPDB \
+                                     -o exim_dumpdb.o exim_dbutil.c
+
+exim_fixdb.o:    $(HDRS) exim_dbutil.c
+       @echo "$(CC) -DEXIM_FIXDB exim_dbutil.c"
+       $(FE)$(CC) -c $(CFLAGS) $(INCLUDE) \
+                                     -DCOMPILE_UTILITY \
+                                     -DEXIM_FIXDB \
+                                     -o exim_fixdb.o exim_dbutil.c
+
+exim_tidydb.o:   $(HDRS) exim_dbutil.c
+       @echo "$(CC) -DEXIM_TIDYDB exim_dbutil.c"
+       $(FE)$(CC) -c $(CFLAGS) $(INCLUDE) \
+                                     -DCOMPILE_UTILITY \
+                                     -DEXIM_TIDYDB \
+                                     -o exim_tidydb.o exim_dbutil.c
+
+# Compile instructions for exim_dbmbuild
+
+exim_dbmbuild.o: $(HDRS) exim_dbmbuild.c
+       @echo "$(CC) exim_dbmbuild.c"
+       $(FE)$(CC) -c $(CFLAGS) $(INCLUDE) -o exim_dbmbuild.o exim_dbmbuild.c
+
+# Utilities use special versions of some modules - typically with debugging
+# calls cut out.
+
+util-spool_in.o: $(HDRS) spool_in.c
+       @echo "$(CC) -DCOMPILE_UTILITY spool_in.c"
+       $(FE)$(CC) -c $(CFLAGS) $(INCLUDE) -DCOMPILE_UTILITY -o util-spool_in.o spool_in.c
+
+util-store.o:    $(HDRS) store.c
+       @echo "$(CC) -DCOMPILE_UTILITY store.c"
+       $(FE)$(CC) -c $(CFLAGS) $(INCLUDE) -DCOMPILE_UTILITY -o util-store.o store.c
+
+util-string.o:   $(HDRS) string.c
+       @echo "$(CC) -DCOMPILE_UTILITY string.c"
+       $(FE)$(CC) -c $(CFLAGS) $(INCLUDE) -DCOMPILE_UTILITY -o util-string.o string.c
+
+util-os.o:       $(HDRS) os.c
+       @echo "$(CC) -DCOMPILE_UTILITY os.c"
+       $(FE)$(CC) -c $(CFLAGS) $(INCLUDE) \
+                                     -DCOMPILE_UTILITY \
+                                     -DOS_LOAD_AVERAGE \
+                                     -DFIND_RUNNING_INTERFACES \
+                                     -o util-os.o os.c
+
+# The local scan module depends only on its own special header, and is compiled
+# from a source whose location is set by configuration.
+
+local_scan.o:    config local_scan.h ../$(LOCAL_SCAN_SOURCE)
+       @echo "$(CC) local_scan.c"
+       $(FE)$(CC) -c $(CFLAGS) -I. $(INCLUDE) -o local_scan.o ../$(LOCAL_SCAN_SOURCE)
+
+# Dependencies for the "ordinary" exim modules
+
+acl.o:           $(HDRS) acl.c
+child.o:         $(HDRS) child.c
+crypt16.o:       $(HDRS) crypt16.c
+daemon.o:        $(HDRS) daemon.c
+dbfn.o:          $(HDRS) dbfn.c
+debug.o:         $(HDRS) debug.c
+deliver.o:       $(HDRS) deliver.c
+directory.o:     $(HDRS) directory.c
+dns.o:           $(HDRS) dns.c
+enq.o:           $(HDRS) enq.c
+exim.o:          $(HDRS) exim.c
+expand.o:        $(HDRS) expand.c
+filter.o:        $(HDRS) filter.c
+filtertest.o:    $(HDRS) filtertest.c
+globals.o:       $(HDRS) globals.c
+header.o:        $(HDRS) header.c
+host.o:          $(HDRS) host.c
+ip.o:            $(HDRS) ip.c
+log.o:           $(HDRS) log.c
+lss.o:           $(HDRS) lss.c
+match.o:         $(HDRS) match.c
+moan.o:          $(HDRS) moan.c
+os.o:            $(HDRS) os.c
+parse.o:         $(HDRS) parse.c
+queue.o:         $(HDRS) queue.c
+rda.o:           $(HDRS) rda.c
+readconf.o:      $(HDRS) readconf.c
+receive.o:       $(HDRS) receive.c
+retry.o:         $(HDRS) retry.c
+rewrite.o:       $(HDRS) rewrite.c
+rfc2047.o:       $(HDRS) rfc2047.c
+route.o:         $(HDRS) route.c
+search.o:        $(HDRS) search.c
+sieve.o:         $(HDRS) sieve.c
+smtp_in.o:       $(HDRS) smtp_in.c
+smtp_out.o:      $(HDRS) smtp_out.c
+spool_in.o:      $(HDRS) spool_in.c
+spool_out.o:     $(HDRS) spool_out.c
+std-crypto.o:    $(HDRS) std-crypto.c
+store.o:         $(HDRS) store.c
+string.o:        $(HDRS) string.c
+tls.o:           $(HDRS) tls.c tls-gnu.c tlscert-gnu.c tls-openssl.c tlscert-openssl.c
+tod.o:           $(HDRS) tod.c
+transport.o:     $(HDRS) transport.c
+tree.o:          $(HDRS) tree.c
+verify.o:        $(HDRS) verify.c
+dkim.o:          $(HDRS) dkim.c
+
+# Dependencies for WITH_CONTENT_SCAN modules
+
+malware.o:       $(HDRS) malware.c
+mime.o:          $(HDRS) mime.c
+regex.o:         $(HDRS) regex.c
+spam.o:          $(HDRS) spam.c
+spool_mbox.o:    $(HDRS) spool_mbox.c
+
+
+# Dependencies for WITH_OLD_DEMIME modules
+
+demime.o:        $(HDRS) demime.c
+
+
+# Dependencies for EXPERIMENTAL_* modules
+
+bmi_spam.o:      $(HDRS) bmi_spam.c
+spf.o:           $(HDRS) spf.h spf.c
+srs.o:           $(HDRS) srs.h srs.c
+dcc.o:           $(HDRS) dcc.h dcc.c
+dmarc.o:         $(HDRS) dmarc.h dmarc.c
+
+# The module containing tables of available lookups, routers, auths, and
+# transports must be rebuilt if any of them are. However, because the makefiles
+# for the drivers are always run, we don't actually put the dependencies here,
+# because if we do, some version of "make" (e.g. IRIX) insist on rebuilding
+# drtables.o even though the .a files haven't in fact been updated. Instead
+# it is arranged that the lower-level makefiles remove drtables.o when they
+# rebuild the .a files.
+
+drtables.o:      $(HDRS) drtables.c
+
+# We depend upon object files built as part of building the lookups library
+# When using parallel make, we don't have the dependency to force building
+# in the sub-directory unless we force that dependency:
+
+$(OBJ_LOOKUPS):  buildlookups
+
+# The exim monitor's private modules - the sources live in a private
+# subdirectory. The final binary combines the private modules with some
+# modules from the main exim binary.
+
+em_StripChart.o: ../exim_monitor/em_StripChart.c
+em_TextPop.o:    ../exim_monitor/em_TextPop.c
+em_globals.o:    ../exim_monitor/em_globals.c ../exim_monitor/em_hdr.h
+em_init.o:       ../exim_monitor/em_init.c    ../exim_monitor/em_hdr.h
+em_log.o:        ../exim_monitor/em_log.c     ../exim_monitor/em_hdr.h
+em_main.o:       ../exim_monitor/em_main.c    ../exim_monitor/em_hdr.h
+em_menu.o:       ../exim_monitor/em_menu.c    ../exim_monitor/em_hdr.h
+em_queue.o:      ../exim_monitor/em_queue.c   ../exim_monitor/em_hdr.h
+em_strip.o:      ../exim_monitor/em_strip.c   ../exim_monitor/em_hdr.h
+em_text.o:       ../exim_monitor/em_text.c    ../exim_monitor/em_hdr.h
+em_xs.o:         ../exim_monitor/em_xs.c      ../exim_monitor/em_hdr.h
+em_version.o:    ../exim_monitor/em_version.c ../exim_monitor/em_hdr.h
+$(MONBIN): $(HDRS)
+                @echo "$(CC) exim_monitor/`echo $@ | sed 's/o$$/c/'`"
+                $(FE)$(CC) -o $@ -c $(CFLAGS) -I. -I../exim_monitor $(INCLUDE) $(XINCLUDE) \
+                  ../exim_monitor/`echo $@ | sed 's/o$$/c/'`
+
+
+# Targets for the various libraries that Exim uses.
+
+# The lookups library.
+
+buildlookups: config
+        @cd lookups && $(MAKE) SHELL=$(SHELL) AR="$(AR)" $(MFLAGS) CC="$(CC)" CFLAGS="$(CFLAGS)" \
+          CFLAGS_DYNAMIC="$(CFLAGS_DYNAMIC)" HDRS="../version.h $(PHDRS)" \
+          FE="$(FE)" RANLIB="$(RANLIB)" RM_COMMAND="$(RM_COMMAND)" \
+          INCLUDE="$(INCLUDE) $(IPV6_INCLUDE) $(TLS_INCLUDE) $(LOOKUP_INCLUDE)"
+        @echo " "
+
+# The routers library.
+
+buildrouters: config
+        @cd routers && $(MAKE) SHELL=$(SHELL) AR="$(AR)" $(MFLAGS) CC="$(CC)" CFLAGS="$(CFLAGS)" \
+          FE="$(FE)" RANLIB="$(RANLIB)" RM_COMMAND="$(RM_COMMAND)" HDRS="$(PHDRS)" \
+          INCLUDE="$(INCLUDE) $(IPV6_INCLUDE) $(TLS_INCLUDE)"
+        @echo " "
+
+# The transports library.
+
+buildtransports: config
+        @cd transports && $(MAKE) SHELL=$(SHELL) AR="$(AR)" $(MFLAGS) CC="$(CC)" CFLAGS="$(CFLAGS)" \
+          FE="$(FE)" RANLIB="$(RANLIB)" RM_COMMAND="$(RM_COMMAND)" HDRS="$(PHDRS)" \
+          INCLUDE="$(INCLUDE) $(IPV6_INCLUDE) $(TLS_INCLUDE)"
+        @echo " "
+
+# The library of authorization modules
+
+buildauths: config
+        @cd auths && $(MAKE) SHELL=$(SHELL) AR="$(AR)" $(MFLAGS) CC="$(CC)" CFLAGS="$(CFLAGS)" \
+          FE="$(FE)" RANLIB="$(RANLIB)" RM_COMMAND="$(RM_COMMAND)" HDRS="$(PHDRS)" \
+          INCLUDE="$(INCLUDE) $(IPV6_INCLUDE) $(TLS_INCLUDE)"
+        @echo " "
+
+# The PDKIM library
+
+buildpdkim: pdkim/pdkim.a
+pdkim/pdkim.a: config
+        @cd pdkim && $(MAKE) SHELL=$(SHELL) AR="$(AR)" $(MFLAGS) CC="$(CC)" CFLAGS="$(CFLAGS)" \
+          FE="$(FE)" RANLIB="$(RANLIB)" RM_COMMAND="$(RM_COMMAND)" HDRS="$(PHDRS)" \
+          INCLUDE="$(INCLUDE) $(IPV6_INCLUDE) $(TLS_INCLUDE)"
+        @echo " "
+
+# The "clean", "install", and "makefile" targets just pass themselves back to
+# the main Exim makefile. These targets will be obeyed only if "make" is obeyed
+# for them in the build directory.
+
+clean install makefile:; cd ..; $(MAKE) $(MFLAGS) build=$(build) $@
+
+# Targets for building stand-alone testing programs for basic testing of
+# some of the building blocks. These are not integrated with the makefile-
+# building targets. If you change something that is going to cause the
+# makefile to be rebuilt, you must run "make makefile" before running one
+# of these.
+
+# The testing programs use different versions of some modules - usually
+# with bits cut out that are not relevant to the test in hand. For those
+# that are used by several tests, we use a different name.
+
+sa-globals.o:    $(HDRS) globals.c
+       $(CC) -c $(CFLAGS) $(INCLUDE) -DSTAND_ALONE -o sa-globals.o globals.c
+
+sa-os.o:         $(HDRS) os.c
+       $(CC) -c $(CFLAGS) $(INCLUDE) \
+                                     -DFIND_RUNNING_INTERFACES \
+                                     -o sa-os.o os.c
+
+# These are the test targets themselves
+
+test_dbfn:   config.h dbfn.c dummies.o sa-globals.o sa-os.o store.o \
+              string.o tod.o version.o
+       $(CC) -c $(CFLAGS) $(INCLUDE) -DSTAND_ALONE dbfn.c
+       $(LNCC) -o test_dbfn $(LFLAGS) dbfn.o \
+         dummies.o sa-globals.o sa-os.o store.o string.o \
+         tod.o version.o $(LIBS) $(DBMLIB)
+       rm -f dbfn.o
+
+test_host:   config.h child.c host.c dns.c dummies.c sa-globals.o os.o \
+              store.o string.o tod.o tree.o
+       $(CC) -c $(CFLAGS) $(INCLUDE) -DSTAND_ALONE -DTEST_HOST child.c
+       $(CC) -c $(CFLAGS) $(INCLUDE) -DSTAND_ALONE -DTEST_HOST host.c
+       $(CC) -c $(CFLAGS) $(INCLUDE) -DSTAND_ALONE -DTEST_HOST dns.c
+       $(CC) -c $(CFLAGS) $(INCLUDE) -DSTAND_ALONE -DTEST_HOST dummies.c
+       $(LNCC) -o test_host $(LFLAGS) \
+         host.o child.o dns.o dummies.o sa-globals.o os.o store.o string.o \
+         tod.o tree.o $(LIBS) $(LIBRESOLV)
+       rm -f child.o dummies.o host.o dns.o
+
+test_os:     os.h os.c dummies.o sa-globals.o store.o string.o tod.o
+       $(CC) -c $(CFLAGS) $(INCLUDE) -DSTAND_ALONE os.c
+       $(LNCC) -o test_os $(LFLAGS) os.o dummies.o \
+         sa-globals.o store.o string.o tod.o $(LIBS)
+       rm -f os.o
+
+test_parse:  config.h parse.c dummies.o sa-globals.o \
+            store.o string.o tod.o version.o
+       $(CC) -c $(CFLAGS) $(INCLUDE) -DSTAND_ALONE parse.c
+       $(LNCC) -o test_parse $(LFLAGS) parse.o \
+         dummies.o sa-globals.o store.o string.o tod.o version.o
+       rm -f parse.o
+
+test_string: config.h string.c dummies.o sa-globals.o store.o tod.o
+       $(CC) -c $(CFLAGS) $(INCLUDE) -DSTAND_ALONE string.c
+       $(LNCC) -o test_string $(LFLAGS) -DSTAND_ALONE string.o \
+         dummies.o sa-globals.o store.o tod.o $(LIBS)
+       rm -f string.o
+
+# End
diff --git a/OS/Makefile-CYGWIN b/OS/Makefile-CYGWIN
new file mode 100644 (file)
index 0000000..774fa4f
--- /dev/null
@@ -0,0 +1,114 @@
+# OS-specific file for Cygwin.
+
+# This file provided by Pierre A. Humblet <Pierre.Humblet@ieee.org>
+
+HAVE_ICONV = yes
+CFLAGS= -g -Wall -O2
+LIBS= -lcrypt -lresolv
+LIBS_EXIM= -liconv
+EXIWHAT_PS_ARG=-as
+EXIWHAT_KILL_SIGNAL=-USR1
+EXIWHAT_EGREP_ARG='/(EXIM|exim)[0-9. -]*$$'
+
+DBMLIB=-lgdbm
+USE_GDBM=YES
+
+# Some OS add a suffix to executables
+EXE = .exe
+
+# To add a resource file with an icon
+LIBS_EXIM +=../Local/exim_res.o
+
+# To produce a linker map
+#LIBS_EXIM+=-Wl,-Map,Exim.Map
+
+
+##################################################
+# The following is normaly set in local/Makefile.
+# Makefile.cygwin provides defaults with which the
+# precompiled version is built
+##################################################
+
+BIN_DIRECTORY=/usr/bin
+CONFIGURE_FILE=/etc/exim.conf
+EXIM_USER=18   # This changes if user exim exists
+EXIM_GROUP=544 # Administrators
+SPOOL_DIRECTORY=/var/spool/exim
+LOG_FILE_PATH=/var/log/exim/exim_%s.log
+TIMEZONE_DEFAULT = ""
+
+AUTH_CRAM_MD5=yes
+AUTH_PLAINTEXT=yes
+AUTH_SPA=yes
+
+SUPPORT_TLS=yes
+TLS_LIBS=-lssl -lcrypto
+
+ROUTER_ACCEPT=yes
+ROUTER_DNSLOOKUP=yes
+ROUTER_IPLITERAL=yes
+ROUTER_MANUALROUTE=yes
+ROUTER_QUERYPROGRAM=yes
+ROUTER_REDIRECT=yes
+
+TRANSPORT_APPENDFILE=yes
+TRANSPORT_AUTOREPLY=yes
+TRANSPORT_PIPE=yes
+TRANSPORT_SMTP=yes
+
+SUPPORT_MAILDIR=yes
+SUPPORT_MAILSTORE=yes
+SUPPORT_MBX=yes
+
+LOOKUP_DBM=yes
+LOOKUP_LSEARCH=yes
+
+# LOOKUP_CDB=yes
+LOOKUP_DNSDB=yes
+LOOKUP_DSEARCH=yes
+LOOKUP_LDAP=yes
+# LOOKUP_MYSQL=yes
+# LOOKUP_NIS=yes
+# LOOKUP_NISPLUS=yes
+# LOOKUP_ORACLE=yes
+LOOKUP_PASSWD=yes
+# LOOKUP_PGSQL=yes
+# LOOKUP_WHOSON=yes
+
+LDAP_LIB_TYPE=OPENLDAP2
+LOOKUP_LIBS=-lldap -llber
+
+# WITH_OLD_DEMIME=yes
+WITH_CONTENT_SCAN=yes
+
+# It is important to define these variables but the values are always overridden
+CONFIGURE_OWNER=18
+CONFIGURE_GROUP=544
+
+EXICYCLOG_MAX=10
+
+COMPRESS_COMMAND=/usr/bin/gzip
+COMPRESS_SUFFIX=gz
+ZCAT_COMMAND=/usr/bin/zcat
+
+# EXIM_PERL=perl.o
+
+# Comment the two lines below if you do not have PAM, e.g. from
+# ftp://ftp.uni-erlangen.de/pub/pc/gnuwin32/cygwin/porters/Humblet_Pierre_A
+SUPPORT_PAM=yes
+CFLAGS += -DINCLUDE_PAM -I ../pam -I ../../pam
+
+APPENDFILE_MODE       = 0644     # default if no ntsec
+APPENDFILE_DIRECTORY_MODE = 0777
+APPENDFILE_LOCKFILE_MODE = 0666
+EXIMDB_DIRECTORY_MODE    = 0777
+EXIMDB_MODE              = 0666
+EXIMDB_LOCKFILE_MODE     = 0666
+INPUT_DIRECTORY_MODE  = 0777
+LOG_DIRECTORY_MODE    = 0777
+LOG_MODE              = 0666
+MSGLOG_DIRECTORY_MODE = 0777
+SPOOL_DIRECTORY_MODE  = 0777
+SPOOL_MODE            = 0666
+
+# End
diff --git a/OS/Makefile-DGUX b/OS/Makefile-DGUX
new file mode 100644 (file)
index 0000000..667c63f
--- /dev/null
@@ -0,0 +1,32 @@
+# Exim: OS-specific make file for DGUX
+#
+# Written by Ken Bailey (K.Bailey@rbgkew.org.uk) Feb 1998
+# on dgux R4.11MU04 generic AViiON mc88100
+# with no X
+
+# Minor tidies to remove settings that are actually the default,
+# in line with the style of other system files - PH.
+
+BASENAME_COMMAND=/bin/basename
+CHOWN_COMMAND=/bin/chown
+CHGRP_COMMAND=/bin/chgrp
+CHMOD_COMMAND=/bin/chmod
+
+# PERL
+# Perl is not necessary for running Exim itself, but some Perl utilities
+# are provided for processing the logs. Perl 5 is assumed.
+# DG ship perl version 4.036 in /bin/perl so need to use locally installed perl
+
+PERL_COMMAND=/usr/local/bin/perl
+
+# dg's version of gcc likes O2
+
+CFLAGS=-O2
+
+RANLIB=@true
+LIBS=-lsocket -lnsl -lm
+LIBRESOLV=-lresolv
+DBMLIB=-ldbm
+
+# End
+
diff --git a/OS/Makefile-Darwin b/OS/Makefile-Darwin
new file mode 100644 (file)
index 0000000..be0d952
--- /dev/null
@@ -0,0 +1,29 @@
+# Exim: OS-specific make file for Darwin (Mac OS X).
+
+CC=cc
+
+BASENAME_COMMAND=look_for_it
+CHOWN_COMMAND=/usr/sbin/chown
+CHMOD_COMMAND=/bin/chmod
+
+HAVE_SA_LEN=YES
+
+# Removed -DBIND_8_COMPAT for 4.61
+# CFLAGS=-O -no-cpp-precomp -DBIND_8_COMPAT
+
+CFLAGS=-O -no-cpp-precomp
+LIBRESOLV=-lresolv
+
+USE_DB = yes
+DBMLIB =
+
+X11=/usr/X11R6
+XINCLUDE=-I$(X11)/include
+XLFLAGS=-L$(X11)/lib
+X11_LD_LIB=$(X11)/lib
+
+EXIWHAT_PS_ARG=ax
+EXIWHAT_EGREP_ARG='/exim( |$$)'
+EXIWHAT_KILL_SIGNAL=-USR1
+
+# End
diff --git a/OS/Makefile-Default b/OS/Makefile-Default
new file mode 100644 (file)
index 0000000..60d5ea8
--- /dev/null
@@ -0,0 +1,329 @@
+##################################################
+#          The Exim mail transport agent         #
+##################################################
+
+# Generic default make file containing settings that relate to the OS or
+# to selectable features within the OS. The configuration options for Exim
+# itself live in Local/Makefile, which is constructed by editing src/EDITME.
+
+# These settings are basic defaults which may be overridden, either by the
+# generic OS-specific files, or by site-specific files. Do not edit this file.
+# Instead, edit or create suitable OS-specific and/or site specific files.
+# See the manual for details.
+
+
+# MAKE_SHELL contains the name of the shell to be used for executing commands
+# from the make files. Normally /bin/sh should be used.
+
+MAKE_SHELL=/bin/sh
+
+
+# BASENAME_COMMAND contains the path to the "basename" command, which varies
+# from OS to OS. This is used when building the Exim monitor script only. (See
+# also HOSTNAME_COMMAND.) If BASENAME_COMMAND is set to "look_for_it" then the
+# script checks for /usr/bin/basename and /bin/basename, and if neither is
+# found, it uses /usr/ucb/basename. This copes with Solaris 2 and Linux, both
+# of which come in different versions.
+
+BASENAME_COMMAND=/usr/bin/basename
+
+
+# If you set STRIP_COMMAND to the path of the "strip" command, it will be run
+# on every binary that is built. It is left unset by default, which leaves
+# the binaries unstripped.
+
+# STRIP_COMMAND=/usr/bin/strip
+
+
+# Some of the following commands live in different places in different OS. We
+# include them all here for generality.
+
+CHOWN_COMMAND=/usr/bin/chown
+CHGRP_COMMAND=/usr/bin/chgrp
+CHMOD_COMMAND=/usr/bin/chmod
+MV_COMMAND=/bin/mv
+RM_COMMAND=/bin/rm
+TOUCH_COMMAND=/usr/bin/touch
+
+
+# Some operating systems have different ways of building libraries of
+# functions. This macro defines the command to do this, defaulting to
+# the "ar" command with options "cq".
+
+AR=ar cq
+
+
+# Not all operating systems have the iconv() function. Those that do have
+#
+# HAVE_ICONV=yes
+#
+# in their OS-specific Makefiles. On those that don't it is possible to
+# install an independent implementation of iconv(). If you've done this,
+# add "HAVE_ICONV=yes" to your Local/Makefile.
+
+
+# Perl is not necessary for running Exim itself, except when EXIM_PERL
+# is set to cause Perl embedding. However, some Perl utilities are provided
+# for processing the logs. Perl 5 is assumed.
+
+PERL_COMMAND=/usr/bin/perl
+
+
+# CC contains the name of the C compiler to be used.
+
+CC=gcc
+
+
+# CFLAGS contains flags to be passed to the compiler. Nothing is defaulted
+# here; instead each OS-dependent Makefile contains a default setting.
+
+# CFLAGS=-O
+
+
+# LFLAGS contains flags to be passed to the link editor. Nothing is defaulted
+# here; instead each OS-dependent Makefile contains a default setting if one
+# is needed.
+
+# LFLAGS=
+
+
+# PCRE_LIBS contains the library to be linked for PCRE
+
+PCRE_LIBS=-lpcre
+
+
+# LIBS and EXTRALIBS contain library settings that are used on linking
+# commands to build binaries. The OS-dependent Makefile may contain a default
+# setting for LIBS, leaving EXTRALIBS available for adding further libraries
+# that are required for optional extras.
+
+# LIBS=
+# EXTRALIBS=
+
+
+# LIBS_EXIM and EXTRALIBS_EXIM contain library settings that are used
+# only when linking the Exim binary. They are not used for other binaries.
+# One possible use is for the TCP wrappers library.
+
+# LIBS_EXIM=
+# EXTRALIBS_EXIM=
+
+
+# LIBS_EXIMON and EXTRALIBS_EXIMON contain library settings that are
+# used only when linking the Exim monitor binary. They are not used for
+# other binaries.
+
+# LIBS_EXIMON=
+# EXTRALIBS_EXIMON=
+
+
+# The error name for quota exceeded varies among operating systems, and
+# even, unfortunately, in different versions of the same operating system.
+# EDQUOT was not in Sys V, but is in SPEC 1170, apparently. It was used
+# in SunOS4, but got taken out for SunOS5, where ENOSPC was given if a quota
+# was exceeded. However, it got put back into SunOS5 with a patch to 5.4 in
+# order to comply with SPEC 1170. Thus even different patch levels of the same
+# system (SunOS5) may use different numbers.
+#
+# If you don't have quotas or are not interested in handling quota errors
+# specially, just set this variable to 0. If it is not set, it defaults to
+# EDQUOT if that is defined for the OS; otherwise it defaults to ENOSPC.
+
+# ERRNO_QUOTA=EDQUOT
+
+
+# The exiwhat utility script finds all the processes running Exim, and sends
+# them a SIGUSR1 signal to get them to write their status to a file. There are
+# two ways in which this can be done:
+#
+# (1) If the OS has a command to find processes and signal them, that can be
+# used. Linux has "killall"; Solaris has "pkill". (Note: "killall" on Solaris
+# does something very different - and disastrous.) The following are set in the
+# OS-specific Makefiles for those OS where this can be done:
+
+# EXIWHAT_MULTIKILL_CMD=
+# EXIWHAT_MULTIKILL_ARG=
+
+# (2) For other operating systems, exiwhat calls the ps command and egreps the
+# output in order to find all the processes running Exim. The arguments for the
+# various commands needed to do this vary from OS to OS. These defaults work on
+# Solaris 2, HPUX, and IRIX. The OS-specific Makefiles have different versions
+# for other systems, and you can override with your own requirements in your
+# private Makefiles in the Local directory. The most commonly found
+# alternatives are -ax instead of -e for the ps argument, and / instead of a
+# blank before the name exim for the egrep argument on systems whose ps output
+# shows the full path name. The quotes for the egrep argument are specified
+# here so that leading white space can be used. This value should always be
+# given in single quotes.
+
+EXIWHAT_PS_CMD=/bin/ps
+EXIWHAT_PS_ARG=-e
+EXIWHAT_EGREP_ARG=' exim( |$$|-)'
+
+# For both kinds of exiwhat usage, the next setting specifies the signal that
+# is sent.
+
+EXIWHAT_KILL_SIGNAL=-USR1
+
+
+# IPv6 is gradually spreading more and more widely. Most operating systems seem
+# to support it nowadays. If you set this option, IPv6 support will be included
+# in the Exim binary. As well as the basic enabling option, there are
+# parameters for include and library directories that may be needed for IPv6 on
+# some systems, where the support is not yet in the standard library.
+
+# HAVE_IPV6=YES
+# IPV6_INCLUDE=-I /usr/ipv6/include
+# IPV6_LIBS=-L/usr/ipv6/libs -linet6
+
+# Exim uses the function getaddrinfo() for converting IPv6 addresses in text
+# form to binary. Apparently some operating systems do not support this, or not
+# correctly, and require the use of the function inet_pton() instead. The
+# following setting enables this. Note, however, the inet_pton() has reduced
+# functionality compared with getaddrinfo(). In particular, it does not
+# recognize the percent convention for identifying scopes (interfaces) that is
+# used by some operating systems.
+
+# IPV6_USE_INET_PTON=yes
+
+# Setting the next option brings in support for A6 DNS records for IPV6. These
+# were at one time expected to supplant AAAA records, but were eventually
+# rejected. The code remains in Exim, but has not been compiled or tested for
+# quite some time. Do not set this unless you know what you are doing.
+
+# SUPPORT_A6=yes
+
+
+# HOSTNAME_COMMAND contains the path to the "hostname" command, which varies
+# from OS to OS. This is used when building the Exim monitor script only. (See
+# also BASENAME_COMMAND.) If HOSTNAME_COMMAND is set to "look_for_it" then the
+# script checks for /usr/bin/hostname and /bin/hostname, and if neither is
+# found, it uses /usr/ucb/basename. This copes with Solaris 2, which comes in
+# different versions.
+
+HOSTNAME_COMMAND=/bin/hostname
+
+
+# INCLUDE contains arbitrary include parameters that you may need to use
+# when building exim. It is added to every compile command.
+
+# INCLUDE=-I /some/special/include-directory
+
+
+# Some OS require a separate library to be quoted when linking programs that
+# call name resolver functions. This can be set in LIBRESOLV, which is left
+# unset here, but is set is some of the OS-specific Makefiles.
+
+# LIBRESOLV=
+
+
+# Additional libraries and include directories may be required for some
+# lookup styles, e.g. LDAP or SQL. LOOKUP_LIBS is included only on the
+# command for linking Exim itself, not on any auxiliary programs. You
+# don't need to set LOOKUP_INCLUDE if the relevant directories are already
+# specified in INCLUDE.
+
+# LOOKUP_INCLUDE=-I /usr/local/ldap/include -I /usr/local/sql/include
+# LOOKUP_LIBS=-L/usr/local/lib -lldap -llber
+
+
+# RANLIB should be set to something that does nothing on systems that do not
+# have the ranlib command or do not need to run it on library files.
+
+RANLIB=ranlib
+
+
+# EXIM_CHMOD is available to specify a command that is automatically applied
+# to the Exim binary immediately it is compiled. (I find this useful when
+# building test versions.)
+
+EXIM_CHMOD=@true
+
+
+# LOCAL_SCAN_SOURCE defines the file in which the function local_scan() is
+# defined. This provides the administrator with a hook for including C code
+# for scanning incoming mails. The path that is defined must be relative to
+# the Exim distribution directory. For example
+
+# LOCAL_SCAN_SOURCE=Local/local_scan.c
+
+# The default setting points to a template function that doesn't actually do
+# any scanning, but just accepts the message.
+
+LOCAL_SCAN_SOURCE=src/local_scan.c
+
+# If you want to specify options for your local_scan() that can be set from
+# the main Exim configuration file, you need to uncomment the following line,
+# and then provide a table of options in your local_scan() source, as described
+# in the reference manual.
+
+# LOCAL_SCAN_HAS_OPTIONS=yes
+
+
+#############################################################################
+# The following are all concerned with configuring the way Exim handles its
+# database (hints) and other dbm files.
+
+# Some systems require a separate library to be supplied when linking programs
+# that make use of DBM library calls. This can be set in DBMLIB, which is unset
+# by default, but is set in some of the OS-specific Makefiles. Setting it in
+# your Local/Makefile will override any other setting.
+
+# DBMLIB=
+
+
+# When Exim is attempting to lock one of its database (hints) files, it
+# applies a timeout which can be altered here.
+
+# EXIMDB_LOCK_TIMEOUT=60
+
+
+# By default, Exim uses traditional ndbm function calls to handle its indexed
+# hints databases. On systems that have Berkeley db installed, this still
+# works via the compatibility interface. However, by defining USE_DB you can
+# make it use native db function calls.
+
+# USE_DB=YES
+
+# Similarly, if you are using gdbm, Exim will by default use the ndbm
+# compatibility interface. However, by defining USE_GDBM you can make it
+# use the native gdbm function calls.
+
+# USE_GDBM=YES
+
+
+#############################################################################
+# The following definitions are relevant only when compiling the Exim monitor
+# program, which requires an X11 display. See the varible EXIM_MONITOR in
+# src/EDITME for how to suppress this compilation.
+
+# X11 contains the location of the X11 libraries and include files.
+
+X11=/usr/X11R6
+
+# XINCLUDE contains options for header inclusion when compiling functions
+# that call X11 functions.
+
+XINCLUDE=-I$(X11)/include
+
+# XLFLAGS contains flags to be passed to the linker when linking the monitor.
+
+XLFLAGS=-L$(X11)/lib
+
+# X11_LD_LIB contains the name of the X11 library that is to be added to
+# LD_LIBRARY_PATH when running the monitor program.
+
+X11_LD_LIB=$(X11)/lib
+
+# A modified version of the Athena TextPop module is supplied with Exim. The
+# modification is to remove the "replace" part of the "search and replace"
+# operation because it isn't wanted. TextPop is only one of a number of
+# modules that make up the Text widget. Some antique link editors cannot handle
+# the case of a replacement module for one of a set of modules. To allow
+# the monitor to be linked in such cases, set the value of EXIMON_TEXTPOP
+# to be empty. The search operations will then contain a useless "replace"
+# option, which is untidy, but does no harm.
+
+EXIMON_TEXTPOP=em_TextPop.o
+
+# End
diff --git a/OS/Makefile-DragonFly b/OS/Makefile-DragonFly
new file mode 100644 (file)
index 0000000..c49c59f
--- /dev/null
@@ -0,0 +1,31 @@
+# Exim: OS-specific make file for DragonFly
+# There's no setting of CFLAGS here, to allow the system default
+# for "make" to be the default.
+
+CHOWN_COMMAND=/usr/sbin/chown
+CHMOD_COMMAND=/bin/chmod
+
+HAVE_SA_LEN=YES
+
+# crypt() is in a separate library
+LIBS=-lcrypt -lm
+
+# DragonFly always ships with Berkeley DB
+USE_DB=yes
+
+# X11 may be under /usr/pkg/xorg/ for example.
+# X11=/usr/X11R6
+X11=$(X11BASE)
+
+XINCLUDE=-I$(X11)/include
+XLFLAGS=-L$(X11)/lib
+XLFLAGS+=-Wl,-rpath,${X11BASE}/lib
+X11_LD_LIB=$(X11)/lib
+
+EXIWHAT_PS_ARG=-ax
+EXIWHAT_EGREP_ARG='/exim( |$$)'
+EXIWHAT_MULTIKILL_CMD='killall -m'
+EXIWHAT_MULTIKILL_ARG='^exim($$|-[0-9.]+-[0-9]+$$)'
+EXIWHAT_KILL_SIGNAL=-USR1
+
+# End
diff --git a/OS/Makefile-FreeBSD b/OS/Makefile-FreeBSD
new file mode 100644 (file)
index 0000000..ebb116b
--- /dev/null
@@ -0,0 +1,50 @@
+# Exim: OS-specific make file for FreeBSD
+# There's no setting of CFLAGS here, to allow the system default
+# for "make" to be the default.
+
+CHOWN_COMMAND=/usr/sbin/chown
+STRIP_COMMAND=/usr/bin/strip
+CHMOD_COMMAND=/bin/chmod
+
+HAVE_SA_LEN=YES
+
+# crypt() is in a separate library
+LIBS=-lcrypt -lm -lutil
+
+# Dynamicly loaded modules need to be built with -fPIC
+CFLAGS_DYNAMIC=-shared -rdynamic -fPIC
+
+# FreeBSD always ships with Berkeley DB
+USE_DB=yes
+
+# This code for building outside ports suggested by Richard Clayton
+.ifdef   X11BASE
+X11=${X11BASE}
+.elifdef LOCALBASE
+X11=$(LOCALBASE)
+.else
+X11=/usr/local
+.endif
+
+# nb: FreeBSD is entirely elf; objformat was removed prior to FreeBSD 7
+# http://www.freebsd.org/cgi/cvsweb.cgi/src/usr.bin/objformat/Attic/objformat.c
+# deleted Jan 2007.
+#
+# So if this fails, you're on an ancient unsupported FreeBSD release *and*
+# running GUI software, which seems both unusual and unwise.
+#
+# http://www.freebsd.org/doc/handbook/binary-formats.html suggests that the
+# switch to default to ELF came with FreeBSD 3.  elf(5) claims ELF support
+# introduced in FreeBSD 2.2.6.
+#
+XINCLUDE=-I$(X11)/include
+XLFLAGS=-L$(X11)/lib -Wl,-rpath,${X11}/lib
+X11_LD_LIB=$(X11)/lib
+
+EXIWHAT_PS_ARG=-ax
+EXIWHAT_EGREP_ARG='/exim( |$$)'
+EXIWHAT_MULTIKILL_CMD='killall -m'
+EXIWHAT_MULTIKILL_ARG='^exim($$|-[0-9.]+-[0-9]+$$)'
+EXIWHAT_KILL_SIGNAL=-USR1
+
+# End
diff --git a/OS/Makefile-GNU b/OS/Makefile-GNU
new file mode 100644 (file)
index 0000000..e464341
--- /dev/null
@@ -0,0 +1,29 @@
+# Exim: OS-specific make file for GNU and variants.
+
+HAVE_ICONV=yes
+
+BASENAME_COMMAND=look_for_it
+CHOWN_COMMAND=look_for_it
+CHGRP_COMMAND=look_for_it
+CHMOD_COMMAND=look_for_it
+
+CFLAGS ?= -O -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE
+
+DBMLIB = -ldb
+USE_DB = yes
+
+LIBS = -lnsl -lcrypt -lm
+LIBRESOLV = -lresolv
+
+X11=/usr/X11R6
+XINCLUDE=-I$(X11)/include
+XLFLAGS=-L$(X11)/lib
+X11_LD_LIB=$(X11)/lib
+
+EXIWHAT_PS_ARG=ax
+EXIWHAT_EGREP_ARG='/exim( |$$)'
+EXIWHAT_MULTIKILL_CMD=killall
+EXIWHAT_MULTIKILL_ARG=exim
+EXIWHAT_KILL_SIGNAL=-USR1
+
+# End
diff --git a/OS/Makefile-GNUkFreeBSD b/OS/Makefile-GNUkFreeBSD
new file mode 100644 (file)
index 0000000..8019281
--- /dev/null
@@ -0,0 +1,29 @@
+# Exim: OS-specific make file for GNU and variants.
+
+HAVE_ICONV=yes
+
+BASENAME_COMMAND=look_for_it
+CHOWN_COMMAND=look_for_it
+CHGRP_COMMAND=look_for_it
+CHMOD_COMMAND=look_for_it
+
+CFLAGS ?= -O -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE
+
+DBMLIB = -ldb
+USE_DB = yes
+
+LIBS = -lnsl -lcrypt -lm
+LIBRESOLV = -lresolv
+
+X11=/usr/X11R6
+XINCLUDE=-I$(X11)/include
+XLFLAGS=-L$(X11)/lib
+X11_LD_LIB=$(X11)/lib
+
+EXIWHAT_PS_ARG=ax
+EXIWHAT_EGREP_ARG='/exim( |$$)'
+EXIWHAT_MULTIKILL_CMD=killall
+EXIWHAT_MULTIKILL_ARG=exim4
+EXIWHAT_KILL_SIGNAL=-USR1
+
+# End
diff --git a/OS/Makefile-GNUkNetBSD b/OS/Makefile-GNUkNetBSD
new file mode 100644 (file)
index 0000000..8019281
--- /dev/null
@@ -0,0 +1,29 @@
+# Exim: OS-specific make file for GNU and variants.
+
+HAVE_ICONV=yes
+
+BASENAME_COMMAND=look_for_it
+CHOWN_COMMAND=look_for_it
+CHGRP_COMMAND=look_for_it
+CHMOD_COMMAND=look_for_it
+
+CFLAGS ?= -O -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE
+
+DBMLIB = -ldb
+USE_DB = yes
+
+LIBS = -lnsl -lcrypt -lm
+LIBRESOLV = -lresolv
+
+X11=/usr/X11R6
+XINCLUDE=-I$(X11)/include
+XLFLAGS=-L$(X11)/lib
+X11_LD_LIB=$(X11)/lib
+
+EXIWHAT_PS_ARG=ax
+EXIWHAT_EGREP_ARG='/exim( |$$)'
+EXIWHAT_MULTIKILL_CMD=killall
+EXIWHAT_MULTIKILL_ARG=exim4
+EXIWHAT_KILL_SIGNAL=-USR1
+
+# End
diff --git a/OS/Makefile-HI-OSF b/OS/Makefile-HI-OSF
new file mode 100644 (file)
index 0000000..da3d487
--- /dev/null
@@ -0,0 +1,8 @@
+# Exim: OS-specific make file for HI-OSF/1-MJ and HI-UX/MPP
+
+CC=cc
+CFLAGS=-O
+RANLIB=@true
+EXIWHAT_EGREP_ARG='/exim( |$$)'
+
+# End
diff --git a/OS/Makefile-HI-UX b/OS/Makefile-HI-UX
new file mode 100644 (file)
index 0000000..870ee84
--- /dev/null
@@ -0,0 +1,12 @@
+# Exim: OS-specific make file for HI-UX
+
+CC=cc -Aa -D_HIUX_SOURCE
+HAVE_SETRESUID=YES
+HAVE_SETEUID=NO
+XINCLUDE=-I/usr/include/X11R5
+XLFLAGS=-L/usr/lib/X11R5
+DBMLIB = -lndbm
+NEED_H_ERRNO=1
+RANLIB=@true
+
+# End
diff --git a/OS/Makefile-HP-UX b/OS/Makefile-HP-UX
new file mode 100644 (file)
index 0000000..073d67a
--- /dev/null
@@ -0,0 +1,25 @@
+# Exim: OS-specific make file for HP-UX later than 9
+
+# HP ANSI C compiler
+#CC=cc
+#CFLAGS=+O2 +Onolimit -z -D_XOPEN_SOURCE_EXTENDED
+# Users of the A.06.00 compiler might need to use +O1 rather than +O2 as
+# there have been some problems reported with this compiler with +O2 set.
+
+# gcc
+CFLAGS=-O -D_XOPEN_SOURCE_EXTENDED
+LDFLAGS=-Wl,-z
+LIBS=-lm
+
+BASENAME_COMMAND=/bin/basename
+HAVE_ICONV=yes
+HAVE_SETRESUID=YES
+HAVE_SETEUID=NO
+XINCLUDE=-I/usr/include/X11R6 -I/usr/contrib/X11R6/include
+XLFLAGS=-L/usr/lib/X11R6 -L/usr/contrib/X11R6/lib
+X11_LD_LIB=/usr/contrib/X11R6/lib
+EXIMON_TEXTPOP=
+DBMLIB=-lndbm
+RANLIB=@true
+
+# End
diff --git a/OS/Makefile-HP-UX-9 b/OS/Makefile-HP-UX-9
new file mode 100644 (file)
index 0000000..1530009
--- /dev/null
@@ -0,0 +1,15 @@
+# Exim: OS-specific make file for HP-UX 9
+
+CFLAGS=-O
+BASENAME_COMMAND=/bin/basename
+HAVE_ICONV=yes
+HAVE_SETRESUID=YES
+HAVE_SETEUID=NO
+XINCLUDE=-I/usr/include/X11R5
+XLFLAGS=-L/usr/lib/X11R5 -L/usr/contrib/X11R5/lib
+X11_LD_LIB=/usr/contrib/X11R5/lib
+EXIMON_TEXTPOP=
+DBMLIB=-lndbm
+RANLIB=@true
+
+# End
diff --git a/OS/Makefile-IRIX b/OS/Makefile-IRIX
new file mode 100644 (file)
index 0000000..7b95783
--- /dev/null
@@ -0,0 +1,12 @@
+# Exim: OS-specific make file for IRIX
+
+HAVE_ICONV=yes
+BASENAME_COMMAND=/sbin/basename
+HOSTNAME_COMMAND=/usr/bsd/hostname
+CFLAGS=-OPT:Olimit=1500
+LIBS=-lmld -lm
+XINCLUDE=-I/usr/include/X11
+vfork=fork
+RANLIB=@true
+
+# End
diff --git a/OS/Makefile-IRIX6 b/OS/Makefile-IRIX6
new file mode 100644 (file)
index 0000000..be01138
--- /dev/null
@@ -0,0 +1,13 @@
+# Exim: OS-specific make file for IRIX6 on 64-bit systems
+
+HAVE_ICONV=yes
+HOSTNAME_COMMAND=/usr/bsd/hostname
+CFLAGS=-O2 -n32 -OPT:Olimit=4000
+LFLAGS=-n32
+LIBS=-lelf -lm
+XINCLUDE=-I/usr/include/X11
+XLFLAGS=
+vfork=fork
+RANLIB=@true
+
+# End
diff --git a/OS/Makefile-IRIX632 b/OS/Makefile-IRIX632
new file mode 100644 (file)
index 0000000..b567fc6
--- /dev/null
@@ -0,0 +1,16 @@
+# Exim: OS-specific make file for IRIX 6 on 32-bit systems.
+# There seems to be some variation. The commented settings show
+# some alternatives.
+
+HAVE_ICONV=yes
+HOSTNAME_COMMAND=/usr/bsd/hostname
+#CFLAGS=-OPT:Olimit=1500 -32 -mips2
+CFLAGS=-32
+LFLAGS=-32
+#LIBS=-lmld
+LIBS=-lelf -lm
+XINCLUDE=-I/usr/include/X11
+vfork=fork
+RANLIB=@true
+
+# End
diff --git a/OS/Makefile-IRIX65 b/OS/Makefile-IRIX65
new file mode 100644 (file)
index 0000000..50e7745
--- /dev/null
@@ -0,0 +1,16 @@
+# Exim: OS-specific make file for IRIX 6.5
+
+HAVE_ICONV=yes
+HOSTNAME_COMMAND=/usr/bsd/hostname
+CC=cc
+CFLAGS=-O2 -OPT:Olimit=0
+# CFLAGS=-O2 # override with this (in your Local/Makefile) if using gcc
+LFLAGS=-Wl,-LD_MSG:off=85
+LFLAGS=
+# nlist has moved from libmld to libelf
+LIBS=-lelf -lm
+XINCLUDE=-I/usr/include/X11
+vfork=fork
+RANLIB=@true
+
+# End
diff --git a/OS/Makefile-Linux b/OS/Makefile-Linux
new file mode 100644 (file)
index 0000000..990f884
--- /dev/null
@@ -0,0 +1,36 @@
+# Exim: OS-specific make file for Linux. This is for modern Linuxes,
+# which use libc6.
+#
+# For Linux, we assume GNU Make; at time of writing, the only extension
+# used is ?= which is actually portable to other maintained Make variants,
+# just is not POSIX.
+
+HAVE_ICONV=yes
+
+BASENAME_COMMAND=look_for_it
+CHOWN_COMMAND=look_for_it
+CHGRP_COMMAND=look_for_it
+CHMOD_COMMAND=look_for_it
+
+# Preserve CFLAGS and CFLAGS_DYNAMIC from the caller/environment
+CFLAGS ?= -O -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE
+CFLAGS_DYNAMIC ?= -shared -rdynamic
+
+DBMLIB = -ldb
+USE_DB = yes
+
+LIBS = -lnsl -lcrypt -lm
+LIBRESOLV = -lresolv
+
+X11=/usr/X11R6
+XINCLUDE=-I$(X11)/include
+XLFLAGS=-L$(X11)/lib
+X11_LD_LIB=$(X11)/lib
+
+EXIWHAT_PS_ARG=ax
+EXIWHAT_EGREP_ARG='/exim( |$$)'
+EXIWHAT_MULTIKILL_CMD=killall
+EXIWHAT_MULTIKILL_ARG=exim
+EXIWHAT_KILL_SIGNAL=-USR1
+
+# End
diff --git a/OS/Makefile-NetBSD b/OS/Makefile-NetBSD
new file mode 100644 (file)
index 0000000..35d03a2
--- /dev/null
@@ -0,0 +1,27 @@
+# Exim: OS-specific make file for NetBSD (ELF object format)
+
+CHOWN_COMMAND=/usr/sbin/chown
+CHMOD_COMMAND=/bin/chmod
+
+CFLAGS ?= -O2
+
+HAVE_SA_LEN=YES
+HAVE_IPV6=YES
+LIBS=-lcrypt -lm
+
+X11=/usr/X11R6
+XINCLUDE=-I$(X11)/include
+XLFLAGS=-L$(X11)/lib
+X11_LD_LIB=$(X11)/lib
+
+EXIWHAT_PS_ARG=-ax
+EXIWHAT_EGREP_ARG='/exim( |$$)'
+EXIWHAT_KILL_SIGNAL=-USR1
+
+# NetBSD always ships with Berkeley DB
+USE_DB=yes
+
+# NetBSD ELF linker needs a -R flag.
+XLFLAGS+=-Wl,-R$(X11)/lib/
+
+# End
diff --git a/OS/Makefile-NetBSD-a.out b/OS/Makefile-NetBSD-a.out
new file mode 100644 (file)
index 0000000..e210efd
--- /dev/null
@@ -0,0 +1,24 @@
+# Exim: OS-specific make file for NetBSD (a.out/COFF object format)
+
+CHOWN_COMMAND=/usr/sbin/chown
+CHMOD_COMMAND=/bin/chmod
+
+CFLAGS ?= -O2
+
+HAVE_SA_LEN=YES
+HAVE_IPV6=YES
+LIBS=-lcrypt -lm
+
+X11=/usr/X11R6
+XINCLUDE=-I$(X11)/include
+XLFLAGS=-L$(X11)/lib
+X11_LD_LIB=$(X11)/lib
+
+EXIWHAT_PS_ARG=-ax
+EXIWHAT_EGREP_ARG='/exim( |$$)'
+EXIWHAT_KILL_SIGNAL=-USR1
+
+# NetBSD always ships with Berkeley DB
+USE_DB=yes
+
+# End
diff --git a/OS/Makefile-OSF1 b/OS/Makefile-OSF1
new file mode 100644 (file)
index 0000000..811ca07
--- /dev/null
@@ -0,0 +1,10 @@
+# Exim: OS-specific make file for OSF1
+
+CFLAGS=-O
+LIBS=-liconv -lm
+HAVE_CRYPT16=yes
+HAVE_ICONV=yes
+HOSTNAME_COMMAND=/usr/bin/hostname
+EXIWHAT_EGREP_ARG='/exim( |$$)'
+
+# End
diff --git a/OS/Makefile-OpenBSD b/OS/Makefile-OpenBSD
new file mode 100644 (file)
index 0000000..1022abb
--- /dev/null
@@ -0,0 +1,28 @@
+# Exim: OS-specific make file for OpenBSD
+
+CHOWN_COMMAND=/usr/sbin/chown
+CHGRP_COMMAND=/usr/sbin/chgrp
+CHMOD_COMMAND=/bin/chmod
+
+CFLAGS=-O2 -Wall
+
+LIBS=-lm
+
+HAVE_SA_LEN=YES
+
+X11=/usr/X11R6
+XINCLUDE=-I$(X11)/include
+XLFLAGS=-L$(X11)/lib
+
+EXIWHAT_MULTIKILL_CMD=pkill
+EXIWHAT_MULTIKILL_ARG='exim( |$$|-)'
+EXIWHAT_PS_ARG=-ax
+EXIWHAT_EGREP_ARG='/exim( |$$)'
+EXIWHAT_KILL_SIGNAL=-USR1
+
+HAVE_IPV6=YES
+
+# OpenBSD always ships with Berkeley DB
+USE_DB=yes
+
+# End
diff --git a/OS/Makefile-OpenUNIX b/OS/Makefile-OpenUNIX
new file mode 100644 (file)
index 0000000..e4d7261
--- /dev/null
@@ -0,0 +1,17 @@
+# Exim: OS-specific make file for OpenUNIX
+
+CC=/usr/bin/cc
+CFLAGS=-O -I/usr/local/include
+LFLAGS=-L/usr/local/lib
+
+LIBS=-lsocket -lnsl -lelf -lgen -lresolv -lm
+EXTRALIBS_EXIMON=-lICE -lSM
+
+RANLIB=@true
+ERRNO_QUOTA=0
+
+X11=/usr/lib/X11
+XINCLUDE=-I/usr/include/X11
+XLFLAGS=-L/usr/lib -L$(X11)/lib
+
+# End
diff --git a/OS/Makefile-QNX b/OS/Makefile-QNX
new file mode 100644 (file)
index 0000000..3cf81c4
--- /dev/null
@@ -0,0 +1,30 @@
+# Exim: OS-specific makefile for QNX
+
+BASENAME_COMMAND=/bin/basename
+MAKE_SHELL=/usr/bin/bash
+
+CHOWN_COMMAND=/bin/chown
+CHGRP_COMMAND=/bin/chgrp
+CHMOD_COMMAND=/bin/chmod
+HOSTNAME_COMMAND=/bin/hostname
+MV_COMMAND=/bin/mv
+PERL_COMMAND=/usr/bin/perl
+RM_COMMAND=/bin/rm
+
+AR=ar -rc
+
+CC=cc
+CFLAGS=-Otax
+LIBIDENTCFLAGS=
+
+RANLIB=@true
+DBMLIB=-ldb
+USE_DB=yes
+LIBS=-lsocket -lm
+
+X11=/usr/X11R6
+XINCLUDE=-I$(X11)/include
+XLFLAGS=-L$(X11)/lib
+X11_LD_LIB=$(X11)/lib
+
+# End
diff --git a/OS/Makefile-SCO b/OS/Makefile-SCO
new file mode 100644 (file)
index 0000000..baa61d8
--- /dev/null
@@ -0,0 +1,28 @@
+# Exim: OS-specific make file for SCO
+
+# It was reported that some versions of gcc (e.g. 2.8.1) require this to be
+# CFLAGS=-melf
+
+CFLAGS=-b elf
+
+RANLIB=@true
+DBMLIB=-lndbm
+ERRNO_QUOTA=0
+LIBS=-lsocket -lm
+HAVE_ICONV=yes
+
+X11=/usr/lib/X11
+XINCLUDE=-I/usr/include/X11
+XLFLAGS=-L/usr/lib -L$(X11)/lib
+X11_LD_LIB=$(X11)/lib
+
+# Changes from Frank Bernhardt (30/09/04)
+
+BASENAME_COMMAND=/bin/basename
+CHOWN_COMMAND=/bin/chown
+CHGRP_COMMAND=/bin/chgrp
+CHMOD_COMMAND=/bin/chmod
+HOSTNAME_COMMAND=/usr/bin/hostname
+TOUCH_COMMAND=/bin/touch
+
+# End
diff --git a/OS/Makefile-SCO_SV b/OS/Makefile-SCO_SV
new file mode 100644 (file)
index 0000000..249b81a
--- /dev/null
@@ -0,0 +1,34 @@
+# Exim: OS-specific make file for SCO_SV release 5 (tested on 5.0.5 & 5.0.5)
+#       (see the UNIX_SV files for SCO 4.2)
+# Supplied by: Tony Earnshaw <tonye@ilion.nl>
+
+# Note that 'gcc -melf -m486' applies to gcc 2.7.2 and higher;
+# 2.7.1 and SCO's SDK need '-belf'.
+
+# Removed -lwrap (PH 27/7/00) because not all systems have it
+
+CFLAGS=-melf -O3 -m486
+LFLAGS=-L/lib -L/usr/lib -L/usr/local/lib
+LIBS=-ltinfo -lsocket -lm
+
+HAVE_ICONV=yes
+
+RANLIB=@true
+DBMLIB=-lndbm
+ERRNO_QUOTA=0
+
+X11=/usr/lib/X11
+XINCLUDE=-I/usr/include/X11
+XLFLAGS=-L/usr/lib -L$(X11)/lib
+X11_LD_LIB=$(X11)/lib
+
+# Changes from Frank Bernhardt (30/9/04)
+
+BASENAME_COMMAND=/bin/basename
+CHOWN_COMMAND=/bin/chown
+CHGRP_COMMAND=/bin/chgrp
+CHMOD_COMMAND=/bin/chmod
+HOSTNAME_COMMAND=/usr/bin/hostname
+TOUCH_COMMAND=/bin/touch
+
+# End
diff --git a/OS/Makefile-SunOS4 b/OS/Makefile-SunOS4
new file mode 100644 (file)
index 0000000..c876998
--- /dev/null
@@ -0,0 +1,16 @@
+# Exim: OS-specific make file for SunOS4
+
+CFLAGS=-O
+
+CHOWN_COMMAND=/usr/etc/chown
+HOSTNAME_COMMAND=/usr/bin/hostname
+EXIT_FAILURE=1
+EXIT_SUCCESS=0
+LIBRESOLV=-lresolv
+XINCLUDE=-I/usr/include/X11
+
+EXIWHAT_PS_ARG=-ax
+EXIWHAT_EGREP_ARG='/exim( |$$)'
+EXIWHAT_KILL_SIGNAL=-30
+
+# End
diff --git a/OS/Makefile-SunOS5 b/OS/Makefile-SunOS5
new file mode 100644 (file)
index 0000000..e60a6c0
--- /dev/null
@@ -0,0 +1,22 @@
+# Exim: OS-specific make file for SunOS5
+
+CFLAGS=-O
+
+HAVE_ICONV=yes
+
+BASENAME_COMMAND=look_for_it
+HOSTNAME_COMMAND=look_for_it
+
+RANLIB=@true
+LIBS=-lsocket -lnsl -lkstat -lm
+LIBRESOLV=-lresolv
+
+EXIWHAT_MULTIKILL_CMD=pkill
+EXIWHAT_MULTIKILL_ARG='exim( |$$|-)'
+
+X11=/usr/openwin
+XINCLUDE=-I$(X11)/include
+XLFLAGS=-L$(X11)/lib -R$(X11)/lib
+X11LIB=$(X11)/lib
+
+# End
diff --git a/OS/Makefile-SunOS5-hal b/OS/Makefile-SunOS5-hal
new file mode 100644 (file)
index 0000000..05ea893
--- /dev/null
@@ -0,0 +1,18 @@
+# Exim: OS-specific make file for SunOS5 on a HAL
+
+# Note: The HAL runs a standard SunOS5 except that it has a 64 bit C
+# compiler called hcc.  To make things work pass the -KV7 flag to force
+# 32bit compilation - this is necessary to interwork with some libraries.
+
+CC=hcc
+CFLAGS=-O -KV7
+LIBIDENTCFLAGS="-KV7 -O -DHAVE_ANSIHEADERS"
+LIBIDENTNAME=sunos5
+RANLIB=@true
+LIBS=-lsocket -lnsl -lkstat -lm
+LIBRESOLV=-lresolv
+X11=/usr/X11R6
+XINCLUDE=-I$(X11)/include
+XLFLAGS=-L$(X11)/lib -R$(X11)/lib
+
+# End
diff --git a/OS/Makefile-ULTRIX b/OS/Makefile-ULTRIX
new file mode 100644 (file)
index 0000000..9e912b3
--- /dev/null
@@ -0,0 +1,18 @@
+# Exim: OS-specific make file for Ultrix
+
+MAKE_SHELL=/usr/bin/sh5
+
+CFLAGS=-O
+
+# This can either be /usr/include/X11 or /usr/include/mit depending on
+# the particular version of ULTRIX.
+
+XINCLUDE=-I/usr/include/X11 -I/usr/include/mit
+
+DBMLIB=-lgdbm
+
+EXIWHAT_PS_ARG=-ax
+EXIWHAT_EGREP_ARG='/exim( |$$)'
+EXIWHAT_KILL_SIGNAL=-USR1
+
+# End
diff --git a/OS/Makefile-UNIX_SV b/OS/Makefile-UNIX_SV
new file mode 100644 (file)
index 0000000..bfcfae1
--- /dev/null
@@ -0,0 +1,24 @@
+# Exim: OS-specific make file for SCO SVR4.2MP (and maybe Unixware)
+#
+#  *** Note that for SCO 5 the configuration file is called SCO_SV,
+#  *** and that Unixware7 has its own configuration. This is an old
+#  *** file that is retained for compatibility.
+#
+# Note that SCO does not include dbm/ndbm with their standard compiler
+# (it is available with /usr/ucb/cc, but that has bugs of its own). You
+# should install gcc and gdbm, then execute 'make install-compat' in the
+# gdbm source directory.
+
+CC=gcc -I/usr/local/include
+CFLAGS=-O
+
+RANLIB=@true
+DBMLIB=-lgdbm -L/usr/local/lib
+ERRNO_QUOTA=0
+LIBS=-lsocket -lelf -lgen -lnsl -lresolv -lm
+
+X11=/usr/lib/X11
+XINCLUDE=-I/usr/include/X11
+XLFLAGS=-L/usr/lib -L$(X11)/lib
+
+# End
diff --git a/OS/Makefile-USG b/OS/Makefile-USG
new file mode 100644 (file)
index 0000000..753a2d7
--- /dev/null
@@ -0,0 +1,33 @@
+# Exim: OS-specific make file for Unixware 2.x
+#
+# Note that Unixware does not include db/dbm/ndbm with their standard compiler
+# (it is available with /usr/ucb/cc, but that has bugs of its own). You
+# should install gcc and Berkeley DB (or another dbm library if you really
+# insist). If you use a different dbm library you will need to override
+# DBMLIB below.
+#
+# DB 1.85 and 2.x can be found at http://www.sleepycat.com/.
+# They have different characteristics. See the discussion of dbm libraries
+# in doc/dbm.discuss.txt in the Exim distribution.
+#
+# DB needs to be compiled with gcc and you need a 'cc' in your path
+# before the Unixware CC to compile it.
+#
+# Don't bother even starting to install exim on Unixware unless
+# you have installed gcc and use it for everything.
+
+CC=gcc -I/usr/local/include
+CFLAGS=-O
+
+RANLIB=@true
+DBMLIB=-ldb -L/usr/local/lib
+USE_DB=YES
+ERRNO_QUOTA=0
+LIBS=-lsocket -lelf -lgen -lnsl -lresolv -lm
+
+X11=/usr/lib/X11
+XINCLUDE=-I/usr/include/X11
+XLFLAGS=-L/usr/lib -L$(X11)/lib
+X11_LD_LIB=$(X11)/lib
+
+# End
diff --git a/OS/Makefile-Unixware7 b/OS/Makefile-Unixware7
new file mode 100644 (file)
index 0000000..88a8838
--- /dev/null
@@ -0,0 +1,32 @@
+# Exim: OS-specific make file for Unixware7
+# Based on information from James FitzGibbon <james@ehlo.com>
+
+# If you want to use libbind, you need to
+#     add -I/usr/local/bind/include to CFLAGS
+#     add -L/usr/local/bind/lib to LFLAGS
+#     remove -lresolv from LIBS
+#     add LOOKUP_LIBS=-lbind
+# The new settings should go in your Local/Makefile rather than here; then
+# they will be usable for subsequent Exim releases.
+
+CC=/usr/bin/cc
+CFLAGS=-O -I/usr/local/include
+LFLAGS=-L/usr/local/lib
+
+HAVE_ICONV=yes
+
+LIBS=-lsocket -lnsl -lelf -lgen -lresolv -lm
+
+# Removed on the advice of Larry Rosenman
+# EXTRALIBS=-lwrap
+
+EXTRALIBS_EXIMON=-lICE -lSM
+
+RANLIB=@true
+ERRNO_QUOTA=0
+
+X11=/usr/lib/X11
+XINCLUDE=-I/usr/include/X11
+XLFLAGS=-L/usr/lib -L$(X11)/lib
+
+# End
diff --git a/OS/Makefile-mips b/OS/Makefile-mips
new file mode 100644 (file)
index 0000000..ff33139
--- /dev/null
@@ -0,0 +1,16 @@
+# Exim: OS-specific make file for RiscOS4bsd
+
+HOSTNAME_COMMAND=/usr/ucb/hostname
+EXIT_FAILURE=1
+EXIT_SUCCESS=0
+LIBRESOLV=-lresolv
+LIBS=-liberty -lm
+XINCLUDE=-I/usr/X11R6/include
+
+CFLAGS=-O
+
+EXIWHAT_PS_ARG=-ax
+EXIWHAT_EGREP_ARG='/exim( |$$)'
+EXIWHAT_KILL_SIGNAL=-30
+
+# End
diff --git a/OS/eximon.conf-Default b/OS/eximon.conf-Default
new file mode 100644 (file)
index 0000000..2f874ef
--- /dev/null
@@ -0,0 +1,41 @@
+# Exim: Default settings for the eximon script which fires up the Exim monitor.
+# These can be overridden by OS-specific scripts and local installation
+# scripts, and also at run time by shell variables.
+
+# The name of the eximon binary, usually the same as the eximon script,
+# with .bin stuck on the end.
+
+EXIMON_BINARY="${EXIMON_BINARY-$0.bin}"
+
+# The remaining parameters are values likely to be changed to suit the
+# user's taste. They are documented in the EDITME file.
+
+WINDOW_TITLE=${EXIMON_WINDOW_TITLE-'"${hostname} eximon"'}
+
+ACTION_OUTPUT=${EXIMON_ACTION_OUTPUT-no}
+ACTION_QUEUE_UPDATE=${EXIMON_ACTION_QUEUE_UPDATE-yes}
+BODY_MAX=${EXIMON_BODY_MAX-20000}
+LOG_DEPTH=${EXIMON_LOG_DEPTH-300}
+LOG_WIDTH=${EXIMON_LOG_WIDTH-${EXIMON_WIDTH-950}}
+LOG_BUFFER=${EXIMON_LOG_BUFFER-20K}
+LOG_FONT=${EXIMON_LOG_FONT--misc-fixed-medium-r-normal-*-14-140-*-*-*-*-iso8859-1}
+LOG_STRIPCHARTS='/ <= /in/
+                 / => /out/
+                 / => .+ R=local/local/
+                 / => .+ T=[^ ]*smtp/smtp/'
+MENU_EVENT=${EXIMON_MENU_EVENT-'Shift<Btn1Down>'}
+MIN_HEIGHT=${EXIMON_MIN_HEIGHT-162}
+MIN_WIDTH=${EXIMON_MIN_WIDTH-103}
+QUEUE_DEPTH=${EXIMON_QUEUE_DEPTH-200}
+QUEUE_WIDTH=${EXIMON_QUEUE_WIDTH-${EXIMON_WIDTH-950}}
+QUEUE_FONT=${EXIMON_QUEUE_FONT-${LOG_FONT}}
+QUEUE_MAX_ADDRESSES=${EXIMON_QUEUE_MAX_ADDRESSES-10}
+QUEUE_INTERVAL=${EXIMON_QUEUE_INTERVAL-300}
+QUEUE_STRIPCHART_NAME=${EXIMON_QUEUE_STRIPCHART_NAME-queue}
+SIZE_STRIPCHART=${EXIMON_SIZE_STRIPCHART}
+SIZE_STRIPCHART_NAME=${EXIMON_SIZE_STRIPCHART_NAME}
+START_SMALL=${EXIMON_START_SMALL-no}
+STRIPCHART_INTERVAL=${EXIMON_STRIPCHART_INTERVAL-60}
+TEXT_DEPTH=${EXIMON_TEXT_DEPTH-200}
+
+# End
diff --git a/OS/os.Configuring b/OS/os.Configuring
new file mode 100644 (file)
index 0000000..465bc26
--- /dev/null
@@ -0,0 +1,215 @@
+Configuring Exim for different Operating Systems
+------------------------------------------------
+
+These notes describe the way in which Exim is configured at the C program level
+for different operating systems. The normal configuration options that apply
+independently of the operating system are specified by creating files in the
+Local directory, as described in chapter 4 of the manual.
+
+These notes cover the os.* files in the OS directory, and contain information
+for people who want to port the program to some new OS, or to modify the
+configuration for an existing port. If you are just wanting to compile Exim on
+a system that it already knows about, you do not need to read further unless
+there are problems.
+
+The os.c-<ostype> files
+-----------------------
+
+There may be an os.c-<ostype> file for each operating system, but for many of
+them it is not necessary. No error occurs is there isn't one. There is a
+generic file called os.c which contains code that is common to two or more OS
+for setting a restarting or a non-restarting signal, for computing the load
+average, and for finding all the network interface addresses. A few OS have
+their own individual code for one or more of these. When they do, the code is
+put into an os.c-<ostype> file, which also defines a macro such as
+OS_RESTARTING_SIGNAL (for example) to cut out the common code in the generic
+os.c.
+
+The os.h-<ostype> files
+-----------------------
+
+For each OS that Exim knows about, there is an os.h-<ostype> file, where
+<ostype> is the OS name. The relevant file is included as a C header file for
+all Exim compilation by pointing a symbolic link called os.h at it from the
+build directory. The settings are as follows:
+
+The select() function
+---------------------
+
+There is a difference in the data type for the second argument to the select()
+function in some OS. The macro SELECT_ARG2_TYPE can be used to define the type.
+If it is not defined in os.h, then it is defaulted to fs_set in exim.h.
+
+The dn_expand() function
+------------------------
+
+There is a difference in the data type for the fourth argument to the
+dn_expand() function in some OS. The macro DN_EXPAND_ARG4_TYPE can be used to
+define the type. If it is not defined in os.h, then it is defaulted to char *
+in exim.h.
+
+The h_errno variable
+--------------------
+
+If NEED_H_ERRNO is defined, then a definition of the form
+
+extern int h_errno
+
+is included in the compiled code of Exim.
+
+The strerror() function
+-----------------------
+
+Most systems provide the ANSI standard strerror() function; older systems may
+instead have an errlist[] variable in which to look up error texts. Defining
+STRERROR_FROM_ERRLIST causes Exim to build its own strerror() function that
+mimics the ANSI function by lookup up the error code in errlist.
+
+Truncating files
+----------------
+
+The fcntl() option for truncating the length of a file is called F_FREESP in
+most systems; in some, however, it is called O_TRUNC. Some os.h files define
+F_FREESP to be O_TRUNC for this reason.
+
+Finding local interfaces
+------------------------
+
+The SIOCGIFCONF ioctl for finding local interfaces behaves differently on BSD
+systems. It returns a vector of ifreq blocks containing sockaddr structures
+that can be longer than their sizeof definition, making the returned ifreq
+blocks longer than their sizeof definitions. BSD sockaddrs structures contain
+an sa_len field giving the actual size. To cope with difference, there is a
+macro called HAVE_SA_LEN. If it is defined, code that works on BSD systems is
+used. Otherwise, the objects returned by SIOCGIFCONF are assumed to be of
+length sizeof(struct ifreq).
+
+On some operating systems, the SIOCGIFCONF ioctl returns the IP addresses
+with the list of interfaces, and there is no need to call SIOCGIFADDR for each
+individual address. Mostly, making the second call does no harm, but on Linux
+when there are IP aliases, it causes things to go wrong. This also happens on
+BSDI and GNU Hurd. Therefore, there is now a macro to cut it out, called
+SIOCGIFCONF_GIVES_ADDR.
+
+Note that, if IPv6 support is configured, Exim cannot find the IPv6 addresses
+on local interfaces by itself. You need to set the local_interfaces option in
+this situation.
+
+Computing load averages
+-----------------------
+
+There are several different ways that load averages are computed. One-off code
+is put in the os.c-<ostype>, but several OS use similar methods, and these
+are coded in the generic os.c, using a number of parameters to make variations
+between OS.
+
+Sometimes the load average is not available to unprivileged callers. If
+LOAD_AVG_NEEDS_ROOT is set, Exim ensures that it is root before trying to
+obtain a load average value.
+
+(1) If HAVE_BSD_GETLOADAVG is defined, Exim uses a simple call to the
+getloadavg() function.
+
+(2) If HAVE_KSTAT is defined, Exim uses the kstat package as found in Solaris 2
+(but nowhere else as yet). It uses some supplementary definitions:
+
+  LOAD_AVG_KSTAT          the kstat to use
+  LOAD_AVG_KSTAT_MODULE   the module to access
+  LOAD_AVG_KSTAT_SYMBOL   the symbol containing the value we want
+  LOAD_AVG_KSTAT_FIELD    the field identity
+
+(3) If HAVE_DEV_KMEM is defined, Exim reads load average values from the
+/dev/kmem device. It uses some supplementary definitions:
+
+  LOAD_AVG_TYPE           the data type
+  LOAD_AVG_SYMBOL         the symbol to look up
+  KERNEL_PATH             the name of the kernel
+  FSCALE                  a scaling factor
+
+Sometimes FSCALE is defined in system headers so need not be defined in the
+os.h-<ostype> file.
+
+Glibc systems and IP options
+----------------------------
+
+The code for inspecting IP options is the same in all OS except for systems
+using glibc (e.g. Linux), which uses a different structure to return data from
+getsockopt(). To handle this, there is a macro called
+
+  GLIBC_IP_OPTIONS
+
+which should be set for Linux (in os.h-Linux) and any other operating system
+that uses glibc.
+
+Options for statvfs()
+---------------------
+
+The following settings apply to the compilation of the Exim monitor as well as
+to the main Exim binary.
+
+#undefine HAVE_STATFS
+
+Exim has options for checking the amount of space in the spool partition
+before accepting a message, and the monitor has the ability to display a
+stripchart of the percentage fullness of a particular disc partition, usually
+/var/spool/mail. The standard way of finding out the data is to call the
+statvfs() function, but some operating systems use statfs() and some may not
+have the ability at all. The Exim code uses STATVFS() for this function and
+this gets defined appropriately. HAVE_STATFS is defined before including the
+os.h file; undefining it suppresses the code for checking a partition in the
+main binary, and for monitoring disc partition in the monitor.
+
+When HAVE_STATFS is defined, the distinction between statvfs() and statfs() is
+made by checking HAVE_SYS_STATVFS_H. If it is defined, then sys/statvfs.h is
+included. Otherwise, STATVFS() is defined as a macro for statfs(), and some
+further includes are done, according to the following definitions:
+
+#define HAVE_SYS_MOUNT_H
+#define HAVE_VFS_H
+
+Each of those definitions causes the inclusion of the corresponding system
+header file in the Exim monitor compilation. For example, the first one causes
+
+#include <sys/mount.h>
+
+to be obeyed. Different systems may require different combinations of these
+headers.
+
+The sys/resource.h header
+-------------------------
+
+One OS does not have the sys/resource.h header. If NO_SYS_RESOURCE_H is defined
+in an os.h-<ostype> file, then the #include for this header is skipped in
+exim.h.
+
+Support for login_cap functions
+-------------------------------
+
+Some of the BSD systems support functions for controlling the resources that
+user processes can use (e.g. login_getpwclass). If HAVE_SETCLASSRESOURCES is
+defined, Exim supports this feature for running pipe deliveries, using the
+setclassresources() function.
+
+The crypt_h header
+------------------
+
+Some OS require crypt.h to be included to get a prototype for the crypt()
+function. This is needed only when compiling with AUTH support. If CRYPT_H is
+defined, then this header is included.
+
+mmap() support
+--------------
+
+The CDB support includes the option of handling file operations by using
+mmap()/munmap(). This gives a reasonable performance increase which will
+probably scale over multiple processes (since the files are mapped read-only
+shared). The vast majority of modern operating systems will support mmap
+(certainly in the simplified way that it is being used here). For example any
+BSD 4.x derived or POSIX compliant system will support it, as will pretty much
+any system using dynamically shared link libraries.
+
+If the OS is believed to support mmap() then the symbol HAVE_MMAP is defined.
+Not all systems that support mmap will have had their config files updated to
+reflect this. Currently Linux, Sun, BSD and SGI/mips systems have been updated.
+
+*** End ***
diff --git a/OS/os.c-GNU b/OS/os.c-GNU
new file mode 100644 (file)
index 0000000..e5d6ff6
--- /dev/null
@@ -0,0 +1,55 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* GNU-specific code. This is concatenated onto the generic src/os.c file.
+GNU/Hurd has approximately the same way to determine the load average as NeXT,
+so a variant of this could also be in the generic os.c file. See the GNU EMacs
+getloadavg.c file, from which this snippet was derived. getloadavg.c from Emacs
+is copyrighted by the FSF under the terms of the GPLv2 or any later version.
+Changes are hereby placed under the same license, as requested by the GPL. */
+
+#ifndef OS_LOAD_AVERAGE
+#define OS_LOAD_AVERAGE
+
+#include <mach.h>
+
+static processor_set_t default_set;
+static int getloadavg_initialized;
+
+int
+os_getloadavg (void)
+{
+host_t host;
+struct processor_set_basic_info info;
+unsigned info_count;
+
+if (!getloadavg_initialized)
+  {
+  if (processor_set_default (mach_host_self(), &default_set) == KERN_SUCCESS)
+    getloadavg_initialized = 1;
+  }
+
+if (getloadavg_initialized)
+  {
+  info_count = PROCESSOR_SET_BASIC_INFO_COUNT;
+  if (processor_set_info(default_set, PROCESSOR_SET_BASIC_INFO, &host,
+       (processor_set_info_t)&info, &info_count) != KERN_SUCCESS)
+    getloadavg_initialized = 0;
+  else
+    {
+    #if LOAD_SCALE == 1000
+    return info.load_average;
+    #else
+    return (int) (((double) info.load_average * 1000) / LOAD_SCALE));
+    #endif
+    }
+  }
+
+return -1;
+}
+#endif  /* OS_LOAD_AVERAGE */
+
+/* End of os.c-GNU */
diff --git a/OS/os.c-HI-OSF b/OS/os.c-HI-OSF
new file mode 100644 (file)
index 0000000..5e3d336
--- /dev/null
@@ -0,0 +1,35 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 2001 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* HI-OSF-specific code. This is concatenated onto the generic
+src/os.c file. OSF has an apparently unique way of getting the
+load average, so we provide a unique function here, and define
+OS_LOAD_AVERAGE to stop src/os.c trying to provide the function. */
+
+#ifndef OS_LOAD_AVERAGE
+#define OS_LOAD_AVERAGE
+
+#include <sys/table.h>
+
+int
+os_getloadavg(void)
+{
+double avg;
+struct tbl_loadavg load_avg;
+
+table (TBL_LOADAVG, 0, &load_avg, 1, sizeof (load_avg));
+
+avg = (load_avg.tl_lscale == 0)?
+  load_avg.tl_avenrun.d[0] :
+  (load_avg.tl_avenrun.l[0] / (double)load_avg.tl_lscale);
+
+return (int)(avg * 1000.0);
+}
+
+#endif  /* OS_LOAD_AVERAGE */
+
+/* End of os.c-HI-OSF */
diff --git a/OS/os.c-IRIX b/OS/os.c-IRIX
new file mode 100644 (file)
index 0000000..487091a
--- /dev/null
@@ -0,0 +1,118 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 2001 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Irix-specific code. This is concatenated onto the generic src/os.c file.
+Irix has a unique way of finding all the network interfaces, so we provide a
+unique function here, and define FIND_RUNNING_INTERFACES to stop src/os.c
+trying to provide the function. The macro may be set initially anyway, when
+compiling os. for utilities that don't want this function. */
+
+#ifndef FIND_RUNNING_INTERFACES
+#define FIND_RUNNING_INTERFACES
+
+/* This is the special form of the function using sysctl() which is the only
+form that returns all the aliases on IRIX systems. This code has its origins
+in a sample program that came from within SGI. */
+
+#include <sys/sysctl.h>
+#include <net/if_dl.h>
+#include <net/if_types.h>
+#include <net/soioctl.h>
+#include <net/route.h>
+
+#define ROUNDUP(a) ((a) > 0 ? (1 + (((a) - 1) | (sizeof(__uint64_t) -1))) \
+                    : sizeof(__uint64_t))
+#ifdef _HAVE_SA_LEN
+#define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
+#else
+#define ADVANCE(x, n) (x += ROUNDUP(_FAKE_SA_LEN_DST(n)))
+#endif
+
+
+ip_address_item *
+os_find_running_interfaces(void)
+{
+ip_address_item *yield = NULL;
+ip_address_item *last = NULL;
+ip_address_item *next;
+
+size_t needed;
+int mib[6];
+char *buf, *nextaddr, *lim;
+register struct if_msghdr *ifm;
+
+mib[0] = CTL_NET;
+mib[1] = PF_ROUTE;
+mib[2] = 0;
+mib[3] = 0;
+mib[4] = NET_RT_IFLIST;
+mib[5] = 0;
+
+/* Get an estimate of the amount of store needed, then get the store and
+get the data into it. Any error causes a panic death. */
+
+if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
+  log_write(0, LOG_PANIC_DIE, "iflist-sysctl-estimate failed: %s",
+    strerror(errno));
+
+buf = store_get(needed);
+
+if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
+  log_write(0, LOG_PANIC_DIE, "sysctl of ifnet list failed: %s",
+    strerror(errno));
+
+/* Now fish out the data for each interface */
+
+lim  = buf + needed;
+for (nextaddr = buf; nextaddr < lim; nextaddr += ifm->ifm_msglen)
+  {
+  ifm = (struct if_msghdr *)nextaddr;
+
+  if (ifm->ifm_type != RTM_IFINFO)
+    {
+    struct ifa_msghdr *ifam = (struct ifa_msghdr *)ifm;
+    struct sockaddr_in *mask = NULL, *addr = NULL;
+
+    if ((ifam->ifam_addrs & RTA_NETMASK) != 0)
+      mask = (struct sockaddr_in *)(ifam + 1);
+
+    if ((ifam->ifam_addrs & RTA_IFA) != 0)
+      {
+      char *cp = (char *)mask;
+      struct sockaddr *sa = (struct sockaddr *)mask;
+      ADVANCE(cp, sa);
+      addr = (struct sockaddr_in *)cp;
+      }
+
+    /* Create a data block for the address, fill in the data, and put it on
+    the chain. This data has to survive for ever, so use malloc. */
+
+    if (addr != NULL)
+      {
+      next = store_malloc(sizeof(ip_address_item));
+      next->next = NULL;
+      next->port = 0;
+      (void)host_ntoa(-1, addr, next->address, NULL);
+
+      if (yield == NULL) yield = last = next; else
+        {
+        last->next = next;
+        last = next;
+        }
+
+      DEBUG(D_interface) debug_printf("Actual local interface address is %s\n",
+        last->address);
+      }
+    }
+  }
+
+return yield;
+}
+
+#endif  /* FIND_RUNNING_INTERFACES */
+
+/* End of os.c-IRIX */
diff --git a/OS/os.c-IRIX6 b/OS/os.c-IRIX6
new file mode 100644 (file)
index 0000000..487091a
--- /dev/null
@@ -0,0 +1,118 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 2001 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Irix-specific code. This is concatenated onto the generic src/os.c file.
+Irix has a unique way of finding all the network interfaces, so we provide a
+unique function here, and define FIND_RUNNING_INTERFACES to stop src/os.c
+trying to provide the function. The macro may be set initially anyway, when
+compiling os. for utilities that don't want this function. */
+
+#ifndef FIND_RUNNING_INTERFACES
+#define FIND_RUNNING_INTERFACES
+
+/* This is the special form of the function using sysctl() which is the only
+form that returns all the aliases on IRIX systems. This code has its origins
+in a sample program that came from within SGI. */
+
+#include <sys/sysctl.h>
+#include <net/if_dl.h>
+#include <net/if_types.h>
+#include <net/soioctl.h>
+#include <net/route.h>
+
+#define ROUNDUP(a) ((a) > 0 ? (1 + (((a) - 1) | (sizeof(__uint64_t) -1))) \
+                    : sizeof(__uint64_t))
+#ifdef _HAVE_SA_LEN
+#define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
+#else
+#define ADVANCE(x, n) (x += ROUNDUP(_FAKE_SA_LEN_DST(n)))
+#endif
+
+
+ip_address_item *
+os_find_running_interfaces(void)
+{
+ip_address_item *yield = NULL;
+ip_address_item *last = NULL;
+ip_address_item *next;
+
+size_t needed;
+int mib[6];
+char *buf, *nextaddr, *lim;
+register struct if_msghdr *ifm;
+
+mib[0] = CTL_NET;
+mib[1] = PF_ROUTE;
+mib[2] = 0;
+mib[3] = 0;
+mib[4] = NET_RT_IFLIST;
+mib[5] = 0;
+
+/* Get an estimate of the amount of store needed, then get the store and
+get the data into it. Any error causes a panic death. */
+
+if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
+  log_write(0, LOG_PANIC_DIE, "iflist-sysctl-estimate failed: %s",
+    strerror(errno));
+
+buf = store_get(needed);
+
+if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
+  log_write(0, LOG_PANIC_DIE, "sysctl of ifnet list failed: %s",
+    strerror(errno));
+
+/* Now fish out the data for each interface */
+
+lim  = buf + needed;
+for (nextaddr = buf; nextaddr < lim; nextaddr += ifm->ifm_msglen)
+  {
+  ifm = (struct if_msghdr *)nextaddr;
+
+  if (ifm->ifm_type != RTM_IFINFO)
+    {
+    struct ifa_msghdr *ifam = (struct ifa_msghdr *)ifm;
+    struct sockaddr_in *mask = NULL, *addr = NULL;
+
+    if ((ifam->ifam_addrs & RTA_NETMASK) != 0)
+      mask = (struct sockaddr_in *)(ifam + 1);
+
+    if ((ifam->ifam_addrs & RTA_IFA) != 0)
+      {
+      char *cp = (char *)mask;
+      struct sockaddr *sa = (struct sockaddr *)mask;
+      ADVANCE(cp, sa);
+      addr = (struct sockaddr_in *)cp;
+      }
+
+    /* Create a data block for the address, fill in the data, and put it on
+    the chain. This data has to survive for ever, so use malloc. */
+
+    if (addr != NULL)
+      {
+      next = store_malloc(sizeof(ip_address_item));
+      next->next = NULL;
+      next->port = 0;
+      (void)host_ntoa(-1, addr, next->address, NULL);
+
+      if (yield == NULL) yield = last = next; else
+        {
+        last->next = next;
+        last = next;
+        }
+
+      DEBUG(D_interface) debug_printf("Actual local interface address is %s\n",
+        last->address);
+      }
+    }
+  }
+
+return yield;
+}
+
+#endif  /* FIND_RUNNING_INTERFACES */
+
+/* End of os.c-IRIX */
diff --git a/OS/os.c-IRIX632 b/OS/os.c-IRIX632
new file mode 100644 (file)
index 0000000..487091a
--- /dev/null
@@ -0,0 +1,118 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 2001 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Irix-specific code. This is concatenated onto the generic src/os.c file.
+Irix has a unique way of finding all the network interfaces, so we provide a
+unique function here, and define FIND_RUNNING_INTERFACES to stop src/os.c
+trying to provide the function. The macro may be set initially anyway, when
+compiling os. for utilities that don't want this function. */
+
+#ifndef FIND_RUNNING_INTERFACES
+#define FIND_RUNNING_INTERFACES
+
+/* This is the special form of the function using sysctl() which is the only
+form that returns all the aliases on IRIX systems. This code has its origins
+in a sample program that came from within SGI. */
+
+#include <sys/sysctl.h>
+#include <net/if_dl.h>
+#include <net/if_types.h>
+#include <net/soioctl.h>
+#include <net/route.h>
+
+#define ROUNDUP(a) ((a) > 0 ? (1 + (((a) - 1) | (sizeof(__uint64_t) -1))) \
+                    : sizeof(__uint64_t))
+#ifdef _HAVE_SA_LEN
+#define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
+#else
+#define ADVANCE(x, n) (x += ROUNDUP(_FAKE_SA_LEN_DST(n)))
+#endif
+
+
+ip_address_item *
+os_find_running_interfaces(void)
+{
+ip_address_item *yield = NULL;
+ip_address_item *last = NULL;
+ip_address_item *next;
+
+size_t needed;
+int mib[6];
+char *buf, *nextaddr, *lim;
+register struct if_msghdr *ifm;
+
+mib[0] = CTL_NET;
+mib[1] = PF_ROUTE;
+mib[2] = 0;
+mib[3] = 0;
+mib[4] = NET_RT_IFLIST;
+mib[5] = 0;
+
+/* Get an estimate of the amount of store needed, then get the store and
+get the data into it. Any error causes a panic death. */
+
+if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
+  log_write(0, LOG_PANIC_DIE, "iflist-sysctl-estimate failed: %s",
+    strerror(errno));
+
+buf = store_get(needed);
+
+if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
+  log_write(0, LOG_PANIC_DIE, "sysctl of ifnet list failed: %s",
+    strerror(errno));
+
+/* Now fish out the data for each interface */
+
+lim  = buf + needed;
+for (nextaddr = buf; nextaddr < lim; nextaddr += ifm->ifm_msglen)
+  {
+  ifm = (struct if_msghdr *)nextaddr;
+
+  if (ifm->ifm_type != RTM_IFINFO)
+    {
+    struct ifa_msghdr *ifam = (struct ifa_msghdr *)ifm;
+    struct sockaddr_in *mask = NULL, *addr = NULL;
+
+    if ((ifam->ifam_addrs & RTA_NETMASK) != 0)
+      mask = (struct sockaddr_in *)(ifam + 1);
+
+    if ((ifam->ifam_addrs & RTA_IFA) != 0)
+      {
+      char *cp = (char *)mask;
+      struct sockaddr *sa = (struct sockaddr *)mask;
+      ADVANCE(cp, sa);
+      addr = (struct sockaddr_in *)cp;
+      }
+
+    /* Create a data block for the address, fill in the data, and put it on
+    the chain. This data has to survive for ever, so use malloc. */
+
+    if (addr != NULL)
+      {
+      next = store_malloc(sizeof(ip_address_item));
+      next->next = NULL;
+      next->port = 0;
+      (void)host_ntoa(-1, addr, next->address, NULL);
+
+      if (yield == NULL) yield = last = next; else
+        {
+        last->next = next;
+        last = next;
+        }
+
+      DEBUG(D_interface) debug_printf("Actual local interface address is %s\n",
+        last->address);
+      }
+    }
+  }
+
+return yield;
+}
+
+#endif  /* FIND_RUNNING_INTERFACES */
+
+/* End of os.c-IRIX */
diff --git a/OS/os.c-IRIX65 b/OS/os.c-IRIX65
new file mode 100644 (file)
index 0000000..487091a
--- /dev/null
@@ -0,0 +1,118 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 2001 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Irix-specific code. This is concatenated onto the generic src/os.c file.
+Irix has a unique way of finding all the network interfaces, so we provide a
+unique function here, and define FIND_RUNNING_INTERFACES to stop src/os.c
+trying to provide the function. The macro may be set initially anyway, when
+compiling os. for utilities that don't want this function. */
+
+#ifndef FIND_RUNNING_INTERFACES
+#define FIND_RUNNING_INTERFACES
+
+/* This is the special form of the function using sysctl() which is the only
+form that returns all the aliases on IRIX systems. This code has its origins
+in a sample program that came from within SGI. */
+
+#include <sys/sysctl.h>
+#include <net/if_dl.h>
+#include <net/if_types.h>
+#include <net/soioctl.h>
+#include <net/route.h>
+
+#define ROUNDUP(a) ((a) > 0 ? (1 + (((a) - 1) | (sizeof(__uint64_t) -1))) \
+                    : sizeof(__uint64_t))
+#ifdef _HAVE_SA_LEN
+#define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
+#else
+#define ADVANCE(x, n) (x += ROUNDUP(_FAKE_SA_LEN_DST(n)))
+#endif
+
+
+ip_address_item *
+os_find_running_interfaces(void)
+{
+ip_address_item *yield = NULL;
+ip_address_item *last = NULL;
+ip_address_item *next;
+
+size_t needed;
+int mib[6];
+char *buf, *nextaddr, *lim;
+register struct if_msghdr *ifm;
+
+mib[0] = CTL_NET;
+mib[1] = PF_ROUTE;
+mib[2] = 0;
+mib[3] = 0;
+mib[4] = NET_RT_IFLIST;
+mib[5] = 0;
+
+/* Get an estimate of the amount of store needed, then get the store and
+get the data into it. Any error causes a panic death. */
+
+if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
+  log_write(0, LOG_PANIC_DIE, "iflist-sysctl-estimate failed: %s",
+    strerror(errno));
+
+buf = store_get(needed);
+
+if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
+  log_write(0, LOG_PANIC_DIE, "sysctl of ifnet list failed: %s",
+    strerror(errno));
+
+/* Now fish out the data for each interface */
+
+lim  = buf + needed;
+for (nextaddr = buf; nextaddr < lim; nextaddr += ifm->ifm_msglen)
+  {
+  ifm = (struct if_msghdr *)nextaddr;
+
+  if (ifm->ifm_type != RTM_IFINFO)
+    {
+    struct ifa_msghdr *ifam = (struct ifa_msghdr *)ifm;
+    struct sockaddr_in *mask = NULL, *addr = NULL;
+
+    if ((ifam->ifam_addrs & RTA_NETMASK) != 0)
+      mask = (struct sockaddr_in *)(ifam + 1);
+
+    if ((ifam->ifam_addrs & RTA_IFA) != 0)
+      {
+      char *cp = (char *)mask;
+      struct sockaddr *sa = (struct sockaddr *)mask;
+      ADVANCE(cp, sa);
+      addr = (struct sockaddr_in *)cp;
+      }
+
+    /* Create a data block for the address, fill in the data, and put it on
+    the chain. This data has to survive for ever, so use malloc. */
+
+    if (addr != NULL)
+      {
+      next = store_malloc(sizeof(ip_address_item));
+      next->next = NULL;
+      next->port = 0;
+      (void)host_ntoa(-1, addr, next->address, NULL);
+
+      if (yield == NULL) yield = last = next; else
+        {
+        last->next = next;
+        last = next;
+        }
+
+      DEBUG(D_interface) debug_printf("Actual local interface address is %s\n",
+        last->address);
+      }
+    }
+  }
+
+return yield;
+}
+
+#endif  /* FIND_RUNNING_INTERFACES */
+
+/* End of os.c-IRIX */
diff --git a/OS/os.c-Linux b/OS/os.c-Linux
new file mode 100644 (file)
index 0000000..df0dff9
--- /dev/null
@@ -0,0 +1,153 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1997 - 2001 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Linux-specific code. This is concatenated onto the generic
+src/os.c file. */
+
+
+/*************************************************
+*              Load average computation          *
+*************************************************/
+
+/*Linux has an apparently unique way of getting the load average, so we provide
+a unique function here, and define OS_LOAD_AVERAGE to stop src/os.c trying to
+provide the function. However, when compiling os.c for utilities, we may not
+want this at all, so check that it isn't set first. */
+
+#if !defined(OS_LOAD_AVERAGE) && defined(__linux__)
+#define OS_LOAD_AVERAGE
+
+/* Linux has 2 ways of returning load average:
+
+  (1) Do a read on /proc/loadavg
+  (2) Use the sysinfo library function and syscall
+
+The latter is simpler but in Linux 2.0 - 2.2 (and probably later releases) is
+exceptionally slow - 10-50ms per call is not unusual and about 100x slow the
+first method. This cripples high performance mail servers by increasing CPU
+utilisation by 3-5x.
+
+In Exim's very early days, it used the 1st method. Later, it switched to the
+2nd method. Now it tries the 1st method and falls back to the 2nd if /proc is
+unavailable. */
+
+#include <sys/sysinfo.h>
+
+static int
+linux_slow_getloadavg(void)
+{
+struct sysinfo s;
+double avg;
+if (sysinfo(&s) < 0) return -1;
+avg = (double) (s.loads[0]) / (1<<SI_LOAD_SHIFT);
+return (int)(avg * 1000.0);
+}
+
+int
+os_getloadavg(void)
+{
+char buffer[40];
+double avg;
+int count;
+int fd = open ("/proc/loadavg", O_RDONLY);
+if (fd == -1) return linux_slow_getloadavg();
+count = read (fd, buffer, sizeof(buffer));
+(void)close (fd);
+if (count <= 0) return linux_slow_getloadavg();
+count = sscanf (buffer, "%lf", &avg);
+if (count < 1) return linux_slow_getloadavg();
+return (int)(avg * 1000.0);
+}
+#endif  /* OS_LOAD_AVERAGE */
+
+
+
+
+
+/*************************************************
+*         Finding interface addresses            *
+*************************************************/
+
+/* This function is not required for utilities; we cut it out if
+FIND_RUNNING_INTERFACES is already defined. */
+
+#ifndef FIND_RUNNING_INTERFACES
+
+/* This code, contributed by Jason Gunthorpe, appears to be the current
+way of finding IPv6 interfaces in Linux. It first calls the common function in
+order to find IPv4 interfaces, then grobbles around to find the others. Jason
+said, "This is so horrible, don't look. Slightly ripped from net-tools
+ifconfig." It gets called by virtue of os_find_running_interfaces being defined
+as a macro for os_find_running_interfaces_linux in the os.h-Linux file. */
+
+ip_address_item *
+os_find_running_interfaces_linux(void)
+{
+ip_address_item *yield = NULL;
+
+#if HAVE_IPV6
+ip_address_item *last = NULL;
+ip_address_item  *next;
+char addr6p[8][5];
+unsigned int plen, scope, dad_status, if_idx;
+char devname[20+1];
+FILE *f;
+#endif
+
+yield = os_common_find_running_interfaces();
+
+#if HAVE_IPV6
+
+/* Open the /proc file; give up if we can't. */
+
+if ((f = fopen("/proc/net/if_inet6", "r")) == NULL) return yield;
+
+/* Pick out the data from within the file, and add it on to the chain */
+
+last = yield;
+if (last != NULL) while (last->next != NULL) last = last->next;
+
+while (fscanf(f, "%4s%4s%4s%4s%4s%4s%4s%4s %02x %02x %02x %02x %20s\n",
+             addr6p[0], addr6p[1], addr6p[2], addr6p[3],
+             addr6p[4], addr6p[5], addr6p[6], addr6p[7],
+             &if_idx, &plen, &scope, &dad_status, devname) != EOF)
+  {
+  struct sockaddr_in6 addr;
+
+  /* This data has to survive for ever, so use malloc. */
+
+  next = store_malloc(sizeof(ip_address_item));
+  next->next = NULL;
+  next->port = 0;
+  sprintf(CS next->address, "%s:%s:%s:%s:%s:%s:%s:%s",
+         addr6p[0], addr6p[1], addr6p[2], addr6p[3],
+         addr6p[4], addr6p[5], addr6p[6], addr6p[7]);
+
+  /* Normalize the representation */
+
+  inet_pton(AF_INET6, CS next->address, &addr.sin6_addr);
+  inet_ntop(AF_INET6, &addr.sin6_addr, CS next->address, sizeof(next->address));
+
+  if (yield == NULL) yield = last = next; else
+    {
+    last->next = next;
+    last = next;
+    }
+
+  DEBUG(D_interface)
+    debug_printf("Actual local interface address is %s (%s)\n", last->address,
+      devname);
+  }
+fclose(f);
+#endif  /* HAVE_IPV6 */
+
+return yield;
+}
+
+#endif  /* FIND_RUNNING_INTERFACES */
+
+/* End of os.c-Linux */
diff --git a/OS/os.c-OSF1 b/OS/os.c-OSF1
new file mode 100644 (file)
index 0000000..ad91b63
--- /dev/null
@@ -0,0 +1,36 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 2001 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* OSF1-specific code. This is concatenated onto the generic src/os.c file.
+OSF1 has an apparently unique way of getting the load average, so we provide a
+unique function here, and define OS_LOAD_AVERAGE to stop src/os.c trying to
+provide the function. The macro may be set initially anyway, when compiling os.
+for utilities that don't want this function. */
+
+#ifndef OS_LOAD_AVERAGE
+#define OS_LOAD_AVERAGE
+
+#include <sys/table.h>
+
+int
+os_getloadavg(void)
+{
+double avg;
+struct tbl_loadavg load_avg;
+
+table (TBL_LOADAVG, 0, &load_avg, 1, sizeof (load_avg));
+
+avg = (load_avg.tl_lscale == 0)?
+  load_avg.tl_avenrun.d[0] :
+  (load_avg.tl_avenrun.l[0] / (double)load_avg.tl_lscale);
+
+return (int)(avg * 1000.0);
+}
+
+#endif  /* OS_LOAD_AVERAGE */
+
+/* End of os.c-OSF1 */
diff --git a/OS/os.c-cygwin b/OS/os.c-cygwin
new file mode 100644 (file)
index 0000000..ea17a43
--- /dev/null
@@ -0,0 +1,862 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Cygwin-specific code. December 2002
+   This is concatenated onto the generic src/os.c file.
+
+   This code was supplied by Pierre A. Humblet <Pierre.Humblet@ieee.org>
+*/
+
+/* We need a special mkdir that
+   allows names starting with // */
+#undef mkdir
+int cygwin_mkdir( const char *path, mode_t mode )
+{
+  const char * p = path;
+  if (*p == '/') while(*(p+1) == '/') p++;
+  return mkdir(p, mode);
+}
+
+/* We have strsignal but cannot use #define
+   because types don't match */
+#define OS_STRSIGNAL /* src/os.c need not provide it */
+char * os_strsignal(int sig)
+{
+  return (char *) strsignal(sig);
+}
+
+#ifndef COMPILE_UTILITY /* Utilities don't need special code */
+#ifdef INCLUDE_MINIRES
+#include "../minires/minires.c"
+#include "../minires/os-interface.c"
+#endif
+
+#ifdef INCLUDE_PAM
+#include "../pam/pam.c"
+#endif
+
+unsigned int cygwin_WinVersion;
+
+/* Conflict between Windows definitions and others */
+#ifdef NOERROR
+#undef NOERROR
+#endif
+#ifdef DELETE
+#undef DELETE
+#endif
+
+#include <windows.h>
+#define EqualLuid(Luid1, Luid2) \
+  ((Luid1.LowPart == Luid2.LowPart) && (Luid1.HighPart == Luid2.HighPart))
+#include <sys/cygwin.h>
+
+/* Special static variables */
+static BOOL cygwin_debug = FALSE;
+static int privileged = 1; /* when not privileged, setuid = noop */
+
+#undef setuid
+int cygwin_setuid(uid_t uid )
+{
+  int res;
+  if (privileged <= 0) return 0;
+  else {
+    res = setuid(uid);
+    if (cygwin_debug)
+      fprintf(stderr, "setuid %lu %lu %d pid: %d\n",
+              uid, getuid(),res, getpid());
+  }
+  return res;
+}
+
+#undef setgid
+int cygwin_setgid(gid_t gid )
+{
+  int res;
+  if (privileged <= 0) return 0;
+  else {
+    res = setgid(gid);
+    if (cygwin_debug)
+      fprintf(stderr, "setgid %lu %lu %d pid: %d\n",
+              gid, getgid(), res, getpid());
+  }
+  return res;
+}
+
+/* Background processes run at lower priority */
+static void cygwin_setpriority()
+{
+  if (!SetPriorityClass(GetCurrentProcess(), BELOW_NORMAL_PRIORITY_CLASS))
+    SetPriorityClass(GetCurrentProcess(), IDLE_PRIORITY_CLASS);
+  return;
+}
+
+
+/* GetVersion()
+   MSB: 1 for 95/98/ME; Next 7: build number, except for 95/98/ME
+   Next byte: 0
+   Next byte: minor version of OS
+   Low  byte: major version of OS (3 or 4 for for NT, 5 for 2000 and XP) */
+#define VERSION_IS_58M(x) (x & 0x80000000) /* 95, 98, Me   */
+#define VERSION_IS_NT(x)  ((x & 0XFF) < 5) /* NT 4 or 3.51 */
+
+/*
+  Routine to find if process or thread is privileged
+*/
+
+enum {
+  CREATE_BIT = 1,
+  RESTORE_BIT = 2
+};
+
+static DWORD get_privileges ()
+{
+  char buffer[1024];
+  DWORD i, length;
+  HANDLE hToken = NULL;
+  PTOKEN_PRIVILEGES privs;
+  LUID cluid, rluid;
+  DWORD ret = 0;
+
+  privs = (PTOKEN_PRIVILEGES) buffer;
+
+  if (OpenProcessToken (GetCurrentProcess(), TOKEN_QUERY, &hToken)
+      && LookupPrivilegeValue (NULL, SE_CREATE_TOKEN_NAME, &cluid)
+      && LookupPrivilegeValue(NULL, SE_RESTORE_NAME, &rluid)
+      && (GetTokenInformation( hToken, TokenPrivileges,
+                               privs, sizeof (buffer), &length)
+          || (GetLastError () == ERROR_INSUFFICIENT_BUFFER
+              && (privs = (PTOKEN_PRIVILEGES) alloca (length))
+              && GetTokenInformation(hToken, TokenPrivileges,
+                                     privs, length, &length)))) {
+    for (i = 0; i < privs->PrivilegeCount; i++) {
+      if (EqualLuid(privs->Privileges[i].Luid, cluid))
+        ret |= CREATE_BIT;
+      else if (EqualLuid(privs->Privileges[i].Luid, rluid))
+        ret |= RESTORE_BIT;
+      else continue;
+      if (ret == (CREATE_BIT | RESTORE_BIT))
+        break;
+    }
+  }
+  else
+    fprintf(stderr, "has_create_token_privilege %ld\n", GetLastError());
+
+  if (hToken)
+    CloseHandle(hToken);
+
+  return ret;
+}
+
+/* We use a special routine to initialize
+    cygwin_init is called from the OS_INIT macro in main(). */
+
+void cygwin_init(int argc, char ** argv, void * rup,
+                 void * eup, void * egp, void * cup, void * cgp)
+{
+  int i;
+  uid_t myuid, systemuid;
+  gid_t mygid, adminsgid;
+  struct passwd * pwp;
+  char *cygenv, win32_path[MAX_PATH];
+  SID(1, SystemSid, SECURITY_LOCAL_SYSTEM_RID);
+  SID(2, AdminsSid, SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS);
+  DWORD priv_flags;
+
+  myuid = getuid();
+  mygid = getgid();
+  cygwin_WinVersion = GetVersion();
+  if ((cygenv = getenv("CYGWIN")) == NULL) cygenv = "";
+  /* Produce some debugging on stderr,
+     cannot yet use exim's debug functions.
+     Exim does not use -c and ignores -n.
+     Set lower priority for daemons */
+  for (i = 1; i < argc; i++) {
+    if (argv[i][0] == '-') {
+      if (argv[i][1] == 'c') {
+        argv[i][1] = 'n';  /* Replace -c by -n */
+        cygwin_debug = TRUE;
+        fprintf(stderr, "CYGWIN = \"%s\".", cygenv);
+        cygwin_conv_to_win32_path("/", win32_path);
+        fprintf(stderr, " Root / mapped to %s.\n", win32_path);
+      }
+      else if (argv[i][1] == 'b' && argv[i][2] == 'd')
+        cygwin_setpriority();
+    }
+  }
+  if (VERSION_IS_58M(cygwin_WinVersion)) {
+    * (uid_t *) rup = myuid;  /* Pretend we are root */
+    * (uid_t *) eup = myuid;  /* ... and exim */
+    * (gid_t *) egp = mygid;
+    return;
+  }
+  /* Nt/2000/XP
+     We initially set the exim uid & gid to those of the "real exim",
+       or to the root uid (SYSTEM) and exim gid (ADMINS),
+     If privileged, we setuid to those.
+     We always set the configure uid to the system uid.
+     We always set the root uid to the real uid
+       to avoid useless execs following forks.
+     If not privileged and unable to chown,
+       we set the exim uid to our uid.
+     If unprivileged, we fake all subsequent setuid. */
+
+  priv_flags = get_privileges ();
+  privileged = !!(priv_flags & CREATE_BIT);
+
+  /* Get the system and admins uid from their sids,
+     or use the default values from the Makefile. */
+  if ((systemuid = cygwin_internal(CW_GET_UID_FROM_SID, & SystemSid)) == -1)
+    systemuid = * (uid_t *) eup;
+  if ((adminsgid = cygwin_internal(CW_GET_GID_FROM_SID, & AdminsSid)) == -1)
+    adminsgid = * (gid_t *) egp;
+
+  if ((pwp = getpwnam("exim")) != NULL) {
+    * (uid_t *) eup = pwp->pw_uid;  /* Set it according to passwd */
+    * (gid_t *) egp = pwp->pw_gid;
+  }
+  else {
+    * (uid_t *) eup = systemuid;
+    * (gid_t *) egp = adminsgid;
+  }
+
+  /* Set the configuration uid and gid to the system uid and admins gid.
+     Note that exim uid is also accepted as owner of exim.conf. */
+  * (uid_t *) cup = systemuid;
+  * (gid_t *) cgp = adminsgid;
+
+  if (privileged) {             /* Can setuid */
+    if (cygwin_setgid(* (gid_t *) egp) /* Setuid to exim */
+        || cygwin_setuid(* (uid_t *) eup))
+      privileged = -1;          /* Problem... Perhaps not in 544 */
+  }
+
+  /* Pretend we are root to avoid useless execs.
+     We are limited by file access rights */
+  * (uid_t *) rup = getuid ();
+
+  /* If we have not setuid to exim and cannot chown,
+     set the exim uid to our uid to avoid chown failures */
+  if (privileged <= 0 && !(priv_flags & RESTORE_BIT))
+    * (uid_t *) eup = * (uid_t *) rup;
+
+  if (cygwin_debug) {
+    fprintf(stderr, "Starting uid %ld, gid %ld, ntsec %lu, privileged %d.\n",
+            myuid, mygid, cygwin_internal(CW_CHECK_NTSEC, NULL), privileged);
+    fprintf(stderr, "root_uid %ld, exim_uid %ld, exim_gid %ld, config_uid %ld, config_gid %ld.\n",
+            * (uid_t *) rup, * (uid_t *) eup, * (gid_t *) egp, * (uid_t *) cup, * (gid_t *) cgp);
+  }
+  return;
+}
+
+#ifndef OS_LOAD_AVERAGE /* Can be set on command line */
+#define OS_LOAD_AVERAGE /* src/os.c need not provide it */
+
+/*****************************************************************
+ *
+ Functions for average load measurements
+
+ There are two methods, which work only on NT.
+
+ The first one uses the HKEY_PERFORMANCE_DATA registry to
+ get performance data. It is complex but well documented
+ and works on all NT versions.
+
+ The second one uses NtQuerySystemInformation.
+ Its use is discouraged starting with WinXP.
+
+ Until 4.43, the Cygwin port of exim was using the first
+ method.
+
+*****************************************************************/
+#define PERF_METHOD2
+
+/* Structure to compute the load average efficiently */
+typedef struct {
+  DWORD Lock;
+  unsigned long long Time100ns;   /* Last measurement time */
+  unsigned long long IdleCount;   /* Latest cumulative idle time */
+  unsigned long long LastCounter; /* Last measurement counter */
+  unsigned long long PerfFreq;    /* Perf counter frequency */
+  int LastLoad;                   /* Last reported load, or -1 */
+#ifdef PERF_METHOD1
+  PPERF_DATA_BLOCK PerfData;      /* Pointer to a buffer to get the data */
+  DWORD BufferSize;               /* Size of PerfData */
+  LPSTR * NamesArray;             /* Temporary (malloc) buffer for index */
+#endif
+} cygwin_perf_t;
+
+static struct {
+   HANDLE handle;
+   pid_t pid;
+   cygwin_perf_t *perf;
+} cygwin_load = {NULL, 0, NULL};
+
+#ifdef PERF_METHOD1
+/*************************************************************
+ METHOD 1
+
+ Obtaining statistics in Windows is done at a low level by
+ calling registry functions, in particular the key
+ HKEY_PERFORMANCE_DATA on NT and successors.
+ Something equivalent exists on Win95, see Microsoft article
+ HOWTO: Access the Performance Registry Under Windows 95 (KB 174631)
+ but it is not implemented here.
+
+ The list of objects to be polled is specified in the string
+ passed to RegQueryValueEx in ReadStat() below.
+ On NT, all objects are polled even if info about only one is
+ required. This is fixed in Windows 2000. See articles
+ INFO: Perflib Calling Close Procedure in Windows 2000 (KB 270127)
+ INFO: Performance Data Changes Between Windows NT 4.0 and Windows
+ 2000 (KB 296523)
+
+ It is unclear to me how the counters are primarily identified.
+ Whether it's by name strings or by the offset of their strings
+ as mapped in X:\Winnt\system32\perfc009.dat [or equivalently as
+ reported by the registry functions in GetNameStrings( ) below].
+ Microsoft documentation seems to say that both methods should
+ work.
+
+ In the interest of speed and language independence, the main
+ code below relies on offsets. However if debug is enabled, the
+ code verifies that the names of the corresponding strings are
+ as expected.
+
+*****************************************************************/
+
+/* Object and counter indices and names */
+#define PROCESSOR_OBJECT_INDEX 238
+#define PROCESSOR_OBJECT_STRING "238"
+#define PROCESSOR_OBJECT_NAME "Processor"
+#define PROCESSOR_TIME_COUNTER 6
+#define PROCESSOR_TIME_NAME "% Processor Time"
+
+#define BYTEINCREMENT 800    /* Block to add to PerfData */
+
+/*****************************************************************
+ *
+ Macros to navigate through the performance data.
+
+ *****************************************************************/
+#define FirstObject(PerfData)\
+  ((PPERF_OBJECT_TYPE)((PBYTE)PerfData + PerfData->HeaderLength))
+#define NextObject(PerfObj)\
+  ((PPERF_OBJECT_TYPE)((PBYTE)PerfObj + PerfObj->TotalByteLength))
+#define ObjectCounterBlock(PerfObj)\
+  ((PPERF_COUNTER_BLOCK)(PBYTE)PerfObj + PerfObj->DefinitionLength )
+#define FirstInstance(PerfObj )\
+  ((PPERF_INSTANCE_DEFINITION)((PBYTE)PerfObj + PerfObj->DefinitionLength))
+#define InstanceCounterBlock(PerfInst)\
+  ((PPERF_COUNTER_BLOCK) ((PBYTE)PerfInst + PerfInst->ByteLength ))
+#define NextInstance(PerfInst )\
+  ((PPERF_INSTANCE_DEFINITION)((PBYTE)InstanceCounterBlock(PerfInst) + \
+        InstanceCounterBlock(PerfInst)->ByteLength) )
+#define FirstCounter(PerfObj)\
+  ((PPERF_COUNTER_DEFINITION) ((PBYTE)PerfObj + PerfObj->HeaderLength))
+#define NextCounter(PerfCntr)\
+  ((PPERF_COUNTER_DEFINITION)((PBYTE)PerfCntr + PerfCntr->ByteLength))
+
+/*****************************************************************
+ *
+ Load the counter and object names from the registry
+ to cygwin_load.perf->NameStrings
+ and index them in cygwin_load.perf->NamesArray
+
+ NameStrings seems to be taken from the file
+ X:\Winnt\system32\perfc009.dat
+
+ This is used only for name verification during initialization,
+ if DEBUG(D_load) is TRUE.
+
+*****************************************************************/
+static BOOL GetNameStrings( )
+{
+  HKEY hKeyPerflib;      // handle to registry key
+  DWORD dwArraySize;     // size for array
+  DWORD dwNamesSize;     // size for strings
+  LPSTR lpCurrentString; // pointer for enumerating data strings
+  DWORD dwCounter;       // current counter index
+  LONG  res;
+
+  /* Get the number of Counter items into dwArraySize. */
+  if ((res = RegOpenKeyEx( HKEY_LOCAL_MACHINE,
+                           "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Perflib",
+                           0,
+                           KEY_QUERY_VALUE, /* KEY_READ, */
+                           &hKeyPerflib))
+      != ERROR_SUCCESS) {
+    DEBUG(D_load) debug_printf("RegOpenKeyEx (1): error %ld (Windows)\n", res);
+    return FALSE;
+  }
+  dwNamesSize = sizeof(dwArraySize); /* Temporary reuse */
+  if ((res = RegQueryValueEx( hKeyPerflib,
+                              "Last Counter",
+                              NULL,
+                              NULL,
+                              (LPBYTE) &dwArraySize,
+                              &dwNamesSize ))
+      != ERROR_SUCCESS) {
+    DEBUG(D_load) debug_printf("RegQueryValueEx (1): error %ld (Windows)\n", res);
+    return FALSE;
+  }
+  RegCloseKey( hKeyPerflib );
+  /* Open the key containing the counter and object names. */
+  if ((res = RegOpenKeyEx( HKEY_LOCAL_MACHINE,
+                           "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Perflib\\009",
+                           0,
+                           KEY_READ,
+                           &hKeyPerflib))
+      != ERROR_SUCCESS) {
+    DEBUG(D_load) debug_printf("RegOpenKeyEx (2): error %ld (Windows)\n", res);
+    return FALSE;
+  }
+  /* Get the size of the Counter value in the key
+     and then read the value in the tail of NamesArray */
+  dwNamesSize = 0;
+  lpCurrentString = NULL;
+  while (1) {
+    res = RegQueryValueEx( hKeyPerflib,
+                           "Counter",
+                           NULL,
+                           NULL,
+                           (unsigned char *) lpCurrentString,
+                           &dwNamesSize);
+    if ((res == ERROR_SUCCESS) && /* Bug (NT 4.0): SUCCESS was returned on first call */
+        (cygwin_load.perf->NamesArray != NULL)) break;
+    if ((res == ERROR_SUCCESS) || /* but cygwin_load.perf->NamesArrays == NULL */
+        (res == ERROR_MORE_DATA)) {
+      /* Allocate memory BOTH for the names array and for the counter and object names */
+      if ((cygwin_load.perf->NamesArray =
+           (LPSTR *) malloc( (dwArraySize + 1) * sizeof(LPSTR) + dwNamesSize * sizeof(CHAR)))
+          != NULL) {
+        /* Point to area for the counter and object names */
+        lpCurrentString = (LPSTR) & cygwin_load.perf->NamesArray[dwArraySize + 1];
+        continue;
+      }
+      DEBUG(D_load) debug_printf("Malloc: errno %d (%s)\n", errno, strerror(errno));
+    }
+    else { /* Serious error */
+      DEBUG(D_load) debug_printf("RegQueryValueEx (2): error %ld (Windows)\n", res);
+    }
+    return FALSE;
+  }
+  RegCloseKey( hKeyPerflib );
+  /* Index the names into an array. */
+  while (*lpCurrentString) {
+    dwCounter = atol( lpCurrentString );
+    lpCurrentString += (lstrlen(lpCurrentString)+1);
+    cygwin_load.perf->NamesArray[dwCounter] = lpCurrentString;
+    lpCurrentString += (strlen(lpCurrentString)+1);
+  }
+  return TRUE;
+}
+
+/*****************************************************************
+ *
+ Find the value of the Processor Time counter
+
+*****************************************************************/
+static BOOL ReadTimeCtr(PPERF_OBJECT_TYPE PerfObj,
+                        PPERF_COUNTER_DEFINITION CurCntr,
+                        PPERF_COUNTER_BLOCK PtrToCntr,
+                        unsigned long long * TimePtr){
+  int j;
+  /* Scan all counters. */
+  for( j = 0; j < PerfObj->NumCounters; j++ ) {
+    if (CurCntr->CounterNameTitleIndex == PROCESSOR_TIME_COUNTER) {
+      /* Verify it is really the proc time counter */
+      if ((CurCntr->CounterType != PERF_100NSEC_TIMER_INV) || /* Wrong type */
+          ((cygwin_load.perf->NamesArray != NULL) &&                  /* Verify name */
+           (strcmp(cygwin_load.perf->NamesArray[CurCntr->CounterNameTitleIndex],
+                   PROCESSOR_TIME_NAME)))) {
+        log_write(0, LOG_MAIN|LOG_PANIC,
+                  "Incorrect Perf counter type or name %x %s",
+                  (unsigned) CurCntr->CounterType,
+                  cygwin_load.perf->NamesArray[CurCntr->CounterNameTitleIndex]);
+        return FALSE;
+      }
+      *TimePtr += *(unsigned long long int *) ((PBYTE) PtrToCntr + CurCntr->CounterOffset);
+      return TRUE; /* return TRUE as soon as we found the counter */
+    }
+    /* Get the next counter. */
+    CurCntr = NextCounter( CurCntr );
+  }
+  return FALSE;
+}
+
+/*****************************************************************
+ *
+ ReadStat()
+ Measures current Time100ns and IdleCount
+ Return TRUE if success.
+
+ *****************************************************************/
+static BOOL ReadStat(unsigned long long int *Time100nsPtr,
+                     unsigned long long int * IdleCountPtr)
+{
+  PPERF_OBJECT_TYPE PerfObj;
+  PPERF_INSTANCE_DEFINITION PerfInst;
+  PPERF_COUNTER_DEFINITION PerfCntr;
+  PPERF_COUNTER_BLOCK PtrToCntr;
+  DWORD i, k, res;
+
+  /* Get the performance data for the Processor object
+     There is no need to open a key.
+     We may need to blindly increase the buffer size.
+     BufferSize does not return info but may be changed */
+  while (1) {
+    DWORD BufferSize = cygwin_load.perf->BufferSize;
+    res = RegQueryValueEx( HKEY_PERFORMANCE_DATA,
+                           PROCESSOR_OBJECT_STRING,
+                           NULL,
+                           NULL,
+                           (LPBYTE) cygwin_load.perf->PerfData,
+                           &BufferSize );
+    if (res == ERROR_SUCCESS) break;
+    if (res == ERROR_MORE_DATA ) {
+      /* Increment if necessary to get a buffer that is big enough. */
+      cygwin_load.perf->BufferSize += BYTEINCREMENT;
+      if ((cygwin_load.perf->PerfData =
+           (PPERF_DATA_BLOCK) realloc( cygwin_load.perf->PerfData, cygwin_load.perf->BufferSize ))
+          != NULL) continue;
+      DEBUG(D_load) debug_printf("Malloc: errno %d (%s)\n", errno, strerror(errno));
+    }
+    else { /* Serious error */
+      DEBUG(D_load) debug_printf("RegQueryValueEx (3): error %ld (Windows)\n", res);
+    }
+    return FALSE;
+  }
+  /* Initialize the counters */
+  *Time100nsPtr = 0;
+  *IdleCountPtr = 0;
+  /* We should only have one object, but write general code just in case. */
+  PerfObj = FirstObject( cygwin_load.perf->PerfData );
+  for( i = 0; i < cygwin_load.perf->PerfData->NumObjectTypes; i++ ) {
+    /* We are only interested in the processor object */
+    if ( PerfObj->ObjectNameTitleIndex == PROCESSOR_OBJECT_INDEX) {
+      /* Possibly verify it is really the Processor object. */
+      if ((cygwin_load.perf->NamesArray != NULL) &&
+          (strcmp(cygwin_load.perf->NamesArray[PerfObj->ObjectNameTitleIndex],
+                  PROCESSOR_OBJECT_NAME))) {
+        log_write(0, LOG_MAIN|LOG_PANIC,
+                  "Incorrect Perf object name %s",
+                  cygwin_load.perf->NamesArray[PerfObj->ObjectNameTitleIndex]);
+        return FALSE;
+      }
+      /* Get the first counter */
+      PerfCntr = FirstCounter( PerfObj );
+      /* See if the object has instances.
+         It should, but write general code. */
+      if( PerfObj->NumInstances != PERF_NO_INSTANCES ) {
+        PerfInst = FirstInstance( PerfObj );
+        for( k = 0; k < PerfObj->NumInstances; k++ ) {
+          /* There can be several processors.
+             Accumulate both the Time100ns and the idle counter.
+             Starting with Win2000 there is an instance named "_Total".
+             Do not use it.     We only use instances with a single
+             character in the name.
+             If we examine the object names, we also look at the instance
+             names and their lengths and issue reports */
+          if ( cygwin_load.perf->NamesArray != NULL) {
+            CHAR ascii[30]; /* The name is in unicode */
+            wsprintf(ascii,"%.29lS",
+                     (char *)((PBYTE)PerfInst + PerfInst->NameOffset));
+            log_write(0, LOG_MAIN,
+                      "Perf: Found processor instance \"%s\", length %d",
+                      ascii, PerfInst->NameLength);
+            if ((PerfInst->NameLength != 4) &&
+                (strcmp(ascii, "_Total") != 0)) {
+              log_write(0, LOG_MAIN|LOG_PANIC,
+                        "Perf: WARNING: Unexpected processor instance name");
+              return FALSE;
+            }
+          }
+          if (PerfInst->NameLength == 4) {
+            *Time100nsPtr += cygwin_load.perf->PerfData->PerfTime100nSec.QuadPart;
+            PtrToCntr = InstanceCounterBlock(PerfInst);
+            if (! ReadTimeCtr(PerfObj, PerfCntr, PtrToCntr, IdleCountPtr)) {
+              return FALSE;
+            }
+          }
+          PerfInst = NextInstance( PerfInst );
+        }
+        return (*Time100nsPtr != 0); /* Something was read */
+      }
+      else { /* No instance, just the counter data */
+        *Time100nsPtr = cygwin_load.perf->PerfData->PerfTime100nSec.QuadPart;
+        PtrToCntr = ObjectCounterBlock(PerfObj);
+        return ReadTimeCtr(PerfObj, PerfCntr, PtrToCntr, IdleCountPtr);
+      }
+    }
+    PerfObj = NextObject( PerfObj );
+  }
+  return FALSE; /* Did not find the Processor object */
+}
+
+#elif defined(PERF_METHOD2)
+
+/*************************************************************
+  METHOD 2
+
+  Uses NtQuerySystemInformation.
+  This requires definitions that are not part of
+  standard include files.
+*************************************************************/
+#include <ntdef.h>
+
+typedef enum _SYSTEM_INFORMATION_CLASS
+{
+  SystemBasicInformation = 0,
+  SystemPerformanceInformation = 2,
+  SystemTimeOfDayInformation = 3,
+  SystemProcessesAndThreadsInformation = 5,
+  SystemProcessorTimes = 8,
+  SystemPagefileInformation = 18,
+  /* There are a lot more of these... */
+} SYSTEM_INFORMATION_CLASS;
+
+typedef struct _SYSTEM_BASIC_INFORMATION
+{
+  ULONG Unknown;
+  ULONG MaximumIncrement;
+  ULONG PhysicalPageSize;
+  ULONG NumberOfPhysicalPages;
+  ULONG LowestPhysicalPage;
+  ULONG HighestPhysicalPage;
+  ULONG AllocationGranularity;
+  ULONG LowestUserAddress;
+  ULONG HighestUserAddress;
+  ULONG ActiveProcessors;
+  UCHAR NumberProcessors;
+} SYSTEM_BASIC_INFORMATION, *PSYSTEM_BASIC_INFORMATION;
+
+typedef struct __attribute__ ((aligned (8))) _SYSTEM_PROCESSOR_TIMES
+{
+  LARGE_INTEGER IdleTime;
+  LARGE_INTEGER KernelTime;
+  LARGE_INTEGER UserTime;
+  LARGE_INTEGER DpcTime;
+  LARGE_INTEGER InterruptTime;
+  ULONG InterruptCount;
+} SYSTEM_PROCESSOR_TIMES, *PSYSTEM_PROCESSOR_TIMES;
+
+typedef NTSTATUS NTAPI (*NtQuerySystemInformation_t) (SYSTEM_INFORMATION_CLASS, PVOID, ULONG, PULONG);
+typedef ULONG NTAPI (*RtlNtStatusToDosError_t) (NTSTATUS);
+
+static NtQuerySystemInformation_t NtQuerySystemInformation;
+static RtlNtStatusToDosError_t RtlNtStatusToDosError;
+
+/*****************************************************************
+ *
+ LoadNtdll()
+ Load special functions from the NTDLL
+ Return TRUE if success.
+
+ *****************************************************************/
+
+static BOOL LoadNtdll()
+{
+  HINSTANCE hinstLib;
+
+  if ((hinstLib = LoadLibrary("NTDLL.DLL"))
+      && (NtQuerySystemInformation =
+          (NtQuerySystemInformation_t) GetProcAddress(hinstLib,
+                                                        "NtQuerySystemInformation"))
+      && (RtlNtStatusToDosError =
+          (RtlNtStatusToDosError_t) GetProcAddress(hinstLib,
+                                                     "RtlNtStatusToDosError")))
+    return TRUE;
+
+  DEBUG(D_load)
+    debug_printf("perf: load: %ld (Windows)\n", GetLastError());
+  return FALSE;
+}
+
+/*****************************************************************
+ *
+ ReadStat()
+ Measures current Time100ns and IdleCount
+ Return TRUE if success.
+
+ *****************************************************************/
+
+static BOOL ReadStat(unsigned long long int *Time100nsPtr,
+                     unsigned long long int *IdleCountPtr)
+{
+  NTSTATUS ret;
+  SYSTEM_BASIC_INFORMATION sbi;
+  PSYSTEM_PROCESSOR_TIMES spt;
+
+  *Time100nsPtr = *IdleCountPtr = 0;
+
+  if ((ret = NtQuerySystemInformation(SystemBasicInformation,
+                                      (PVOID) &sbi, sizeof sbi, NULL))
+      != STATUS_SUCCESS) {
+    DEBUG(D_load)
+      debug_printf("Perf: NtQuerySystemInformation: %lu (Windows)\n",
+                   RtlNtStatusToDosError(ret));
+  }
+  else if (!(spt = (PSYSTEM_PROCESSOR_TIMES) alloca(sizeof(spt[0]) * sbi.NumberProcessors))) {
+    DEBUG(D_load)
+      debug_printf("Perf: alloca: errno %d (%s)\n", errno, strerror(errno));
+  }
+  else if ((ret = NtQuerySystemInformation(SystemProcessorTimes, (PVOID) spt,
+                                           sizeof spt[0] * sbi.NumberProcessors, NULL))
+           != STATUS_SUCCESS) {
+    DEBUG(D_load)
+      debug_printf("Perf: NtQuerySystemInformation: %lu (Windows)\n",
+                   RtlNtStatusToDosError(ret));
+  }
+  else {
+    int i;
+    for (i = 0; i < sbi.NumberProcessors; i++) {
+      *Time100nsPtr += spt[i].KernelTime.QuadPart;;
+      *Time100nsPtr += spt[i].UserTime.QuadPart;
+      *IdleCountPtr += spt[i].IdleTime.QuadPart;
+    }
+    return TRUE;
+  }
+  return FALSE;
+}
+#endif /* PERF_METHODX */
+
+/*****************************************************************
+ *
+ InitLoadAvg()
+ Initialize the cygwin_load.perf structure.
+ and set cygwin_load.perf->Flag to TRUE if successful.
+ This is called the first time os_getloadavg is called
+ *****************************************************************/
+static void InitLoadAvg(cygwin_perf_t *this)
+{
+  BOOL success = TRUE;
+
+  /* Get perf frequency and counter */
+  QueryPerformanceFrequency((LARGE_INTEGER *)& this->PerfFreq);
+  QueryPerformanceCounter((LARGE_INTEGER *)& this->LastCounter);
+
+#ifdef PERF_METHOD1
+  DEBUG(D_load) {
+    /* Get the name strings through the registry
+       to verify that the object and counter numbers
+       have the names we expect */
+    success = GetNameStrings();
+  }
+#endif
+  /* Get initial values for Time100ns and IdleCount */
+  success = success
+            && ReadStat( & this->Time100ns,
+                         & this->IdleCount);
+  /* If success, set the Load to 0, else to -1 */
+  if (success) this->LastLoad = 0;
+  else {
+    log_write(0, LOG_MAIN, "Cannot obtain Load Average");
+    this->LastLoad = -1;
+  }
+#ifdef PERF_METHOD1
+  /* Free the buffer created for debug name verification */
+  if (this->NamesArray != NULL) {
+    free(this->NamesArray);
+    this->NamesArray = NULL;
+  }
+#endif
+}
+
+
+/*****************************************************************
+ *
+ os_getloadavg()
+
+ Return -1 if not available;
+ Return the previous value if less than AVERAGING sec old.
+ else return the processor load on a [0 - 1000] scale.
+
+ The first time we are called we initialize the counts
+ and return 0 or -1.
+ The initial load cannot be measured as we use the processor 100%
+*****************************************************************/
+static SECURITY_ATTRIBUTES sa = {sizeof (SECURITY_ATTRIBUTES), NULL, TRUE};
+#define AVERAGING 10
+
+int os_getloadavg()
+{
+  unsigned long long Time100ns, IdleCount, CurrCounter;
+  int value;
+  pid_t newpid;
+
+  /* New process.
+     Reload the dlls and the file mapping */
+  if ((newpid = getpid()) != cygwin_load.pid) {
+    BOOL new;
+    cygwin_load.pid = newpid;
+
+#ifdef PERF_METHOD2
+    if (!LoadNtdll()) {
+      log_write(0, LOG_MAIN, "Cannot obtain Load Average");
+      cygwin_load.perf = NULL;
+      return -1;
+    }
+#endif
+
+    if ((new = !cygwin_load.handle)) {
+      cygwin_load.handle = CreateFileMapping (INVALID_HANDLE_VALUE, &sa, PAGE_READWRITE,
+                                              0, sizeof(cygwin_perf_t), NULL);
+      DEBUG(D_load)
+        debug_printf("Perf: CreateFileMapping: handle %x\n", (unsigned) cygwin_load.handle);
+    }
+    cygwin_load.perf = (cygwin_perf_t *) MapViewOfFile (cygwin_load.handle,
+                                                        FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, 0);
+    DEBUG(D_load)
+      debug_printf("Perf: MapViewOfFile: addr %x\n", (unsigned) cygwin_load.perf);
+    if (new && cygwin_load.perf)
+      InitLoadAvg(cygwin_load.perf);
+  }
+
+  /* Check if initialized OK */
+  if (!cygwin_load.perf || cygwin_load.perf->LastLoad < 0)
+    return -1;
+
+  /* If we cannot get the lock, we return 0.
+     This is to prevent any lock-up possibility.
+     Finding a lock busy is unlikely, and giving up only
+     results in an immediate delivery .*/
+
+  if (InterlockedCompareExchange(&cygwin_load.perf->Lock, 1, 0)) {
+    DEBUG(D_load)
+      debug_printf("Perf: Lock busy\n");
+    return 0;
+  }
+
+    /* Get the current time (PerfCounter) */
+    QueryPerformanceCounter((LARGE_INTEGER *)& CurrCounter);
+    /* Calls closer than AVERAGING sec apart use the previous value */
+  if (CurrCounter - cygwin_load.perf->LastCounter >
+      AVERAGING * cygwin_load.perf->PerfFreq) {
+      /* Get Time100ns and IdleCount */
+      if (ReadStat( & Time100ns, & IdleCount)) { /* Success */
+        /* Return processor load on 1000 scale */
+      value = 1000 - ((1000 * (IdleCount - cygwin_load.perf->IdleCount)) /
+                      (Time100ns - cygwin_load.perf->Time100ns));
+      cygwin_load.perf->Time100ns = Time100ns;
+      cygwin_load.perf->IdleCount = IdleCount;
+      cygwin_load.perf->LastCounter = CurrCounter;
+      cygwin_load.perf->LastLoad = value;
+      DEBUG(D_load)
+        debug_printf("Perf: New load average %d\n", value);
+      }
+      else { /* Something bad happened.
+                Refuse to measure the load anymore
+                but don't bother releasing the buffer */
+        log_write(0, LOG_MAIN, "Cannot obtain Load Average");
+      cygwin_load.perf->LastLoad = -1;
+    }
+  }
+  else
+  DEBUG(D_load)
+      debug_printf("Perf: Old load average %d\n", cygwin_load.perf->LastLoad);
+  cygwin_load.perf->Lock = 0;
+  return cygwin_load.perf->LastLoad;
+}
+#endif /* OS_LOAD_AVERAGE */
+#endif /* COMPILE_UTILITY */
diff --git a/OS/os.h-AIX b/OS/os.h-AIX
new file mode 100644 (file)
index 0000000..f3a84f2
--- /dev/null
@@ -0,0 +1,23 @@
+/* Exim: OS-specific C header file for AIX */
+/* Written by Nick Waterman <nick@cimio.co.uk> */
+/* Modified by Philip Hazel with data from
+   Niels Provos <provos@wserver.physnet.uni-hamburg.de>
+   Juozas Simkevicius <juozas@omnitel.net> for load averages
+*/
+
+#define HAVE_DEV_KMEM
+#define LOAD_AVG_SYMBOL "avenrun"
+#define KERNEL_PATH     "/unix"
+#define LOAD_AVG_TYPE   int
+#define FSCALE          65536.0
+
+#define HAVE_SYS_VFS_H
+#define HAVE_SYS_STATFS_H
+
+/* Now tell AIX to emulate BSD as badly as it can. */
+
+#define _BSD 44
+
+typedef struct flock flock_t;
+
+/* End */
diff --git a/OS/os.h-BSDI b/OS/os.h-BSDI
new file mode 100644 (file)
index 0000000..cd91936
--- /dev/null
@@ -0,0 +1,11 @@
+/* Exim: OS-specific C header file for BSDI */
+
+#define HAVE_BSD_GETLOADAVG
+#define HAVE_SETCLASSRESOURCES
+#define HAVE_MMAP
+#define HAVE_SYS_MOUNT_H
+#define SIOCGIFCONF_GIVES_ADDR
+
+typedef struct flock flock_t;
+
+/* End */
diff --git a/OS/os.h-DGUX b/OS/os.h-DGUX
new file mode 100644 (file)
index 0000000..838ddd9
--- /dev/null
@@ -0,0 +1,25 @@
+/* Exim: OS-specific C header file for DGUX */
+
+/* Written by Ken Bailey (K.Bailey@rbgkew.org.uk) Feb 1998 */
+/* on dgux R4.11MU04 generic AViiON mc88100                */
+/* Modified Dec 1998 by PH after message from Ken.         */
+
+#define HAVE_SYS_STATVFS_H
+#define F_FAVAIL                 f_favail
+
+#define NO_SYSEXITS              /* DGUX doesn't ship sysexits.h */
+#define NO_IP_VAR_H              /* DGUX has no netinet/ip_var.h */
+
+#define os_strsignal             dg_strsignal
+#define OS_STRSIGNAL
+
+#define HAVE_MMAP
+
+/* The definition of ipoptions in netinet/in.h (masquerading as ip_opts) used
+in smtp_in.c is for Intel DG _IX86_ABI only. You may be able to get this to
+work on Intel DG but it's certainly easier to skip it on M88k. This means we
+forego the detection of some source-routing based IP attacks. */
+
+#define NO_IP_OPTIONS
+
+/* End */
diff --git a/OS/os.h-Darwin b/OS/os.h-Darwin
new file mode 100644 (file)
index 0000000..559003f
--- /dev/null
@@ -0,0 +1,45 @@
+/* Exim: OS-specific C header file for Darwin (Mac OS X) */
+
+/* #define CRYPT_H */  /* Apparently this isn't needed */
+
+#define HAVE_MMAP
+#define HAVE_SYS_MOUNT_H
+#define PAM_H_IN_PAM
+#define SIOCGIFCONF_GIVES_ADDR
+
+/* OSX 10.2 does not have poll.h, 10.3 does emulate it badly. */
+#define NO_POLL_H
+
+#define F_FREESP     O_TRUNC
+typedef struct flock flock_t;
+
+#define BASE_62 36  /* HFS+ aliases lower and upper cases in filenames.
+                               Consider reducing MAX_LOCALHOST_NUMBER */
+
+#ifndef        _BSD_SOCKLEN_T_
+#define _BSD_SOCKLEN_T_ int32_t                 /* socklen_t (duh) */
+#endif
+
+/* Settings for handling IP options. There's no netinet/ip_var.h. The IP
+option handling is in the style of the later GLIBCs but the GLIBC macros
+aren't set, so we invent a new one. */
+
+#define NO_IP_VAR_H
+#define DARWIN_IP_OPTIONS
+
+/* Need this for the DNS lookup code. Remember to remove if we get round to
+updating Exim to use the newer interface. */
+
+#define BIND_8_COMPAT
+
+/* It's not .so for dynamic libraries on Darwin. */
+#define DYNLIB_FN_EXT "dylib"
+
+/* We currently need some assistance getting OFF_T_FMT correct on MacOS */
+#ifdef OFF_T_FMT
+# undef OFF_T_FMT
+#endif
+#define OFF_T_FMT "%lld"
+#define LONGLONG_T long int
+
+/* End */
diff --git a/OS/os.h-DragonFly b/OS/os.h-DragonFly
new file mode 100644 (file)
index 0000000..669bb23
--- /dev/null
@@ -0,0 +1,10 @@
+/* Exim: OS-specific C header file for DragonFly */
+
+#define HAVE_BSD_GETLOADAVG
+#define HAVE_MMAP
+#define HAVE_SYS_MOUNT_H
+#define SIOCGIFCONF_GIVES_ADDR
+
+typedef struct flock flock_t;
+
+/* End */
diff --git a/OS/os.h-FreeBSD b/OS/os.h-FreeBSD
new file mode 100644 (file)
index 0000000..c5ed042
--- /dev/null
@@ -0,0 +1,13 @@
+/* Exim: OS-specific C header file for FreeBSD */
+
+#define HAVE_BSD_GETLOADAVG
+#define HAVE_SETCLASSRESOURCES
+#define HAVE_MMAP
+#define HAVE_SYS_MOUNT_H
+#define SIOCGIFCONF_GIVES_ADDR
+#define HAVE_SRANDOMDEV
+#define HAVE_ARC4RANDOM
+
+typedef struct flock flock_t;
+
+/* End */
diff --git a/OS/os.h-GNU b/OS/os.h-GNU
new file mode 100644 (file)
index 0000000..cc4da0e
--- /dev/null
@@ -0,0 +1,20 @@
+/* Exim: OS-specific C header file for GNU/Hurd */
+
+#define CRYPT_H
+#define GLIBC_IP_OPTIONS
+#define HAVE_BSD_GETLOADAVG
+#define HAVE_MMAP
+#define HAVE_SYS_VFS_H
+#define NO_IP_VAR_H
+#define SIG_IGN_WORKS
+#define SIOCGIFCONF_GIVES_ADDR
+
+#define F_FREESP     O_TRUNC
+typedef struct flock flock_t;
+
+#define os_strsignal strsignal
+#define OS_STRSIGNAL
+
+/* Hurd-specific bits below */
+
+/* End */
diff --git a/OS/os.h-GNUkFreeBSD b/OS/os.h-GNUkFreeBSD
new file mode 100644 (file)
index 0000000..e60690f
--- /dev/null
@@ -0,0 +1,22 @@
+/* Exim: OS-specific C header file for GNU/kFreeBSD */
+
+#define CRYPT_H
+#define GLIBC_IP_OPTIONS
+#define HAVE_MMAP
+#define HAVE_BSD_GETLOADAVG
+#define HAVE_SYS_VFS_H
+#define NO_IP_VAR_H
+#define SIG_IGN_WORKS
+
+#define F_FREESP     O_TRUNC
+typedef struct flock flock_t;
+
+#define os_strsignal strsignal
+#define OS_STRSIGNAL
+
+/* kFreeBSD-specific bits below */
+
+#define HAVE_SYS_MOUNT_H
+#define SIOCGIFCONF_GIVES_ADDR
+
+/* End */
diff --git a/OS/os.h-GNUkNetBSD b/OS/os.h-GNUkNetBSD
new file mode 100644 (file)
index 0000000..121f2d3
--- /dev/null
@@ -0,0 +1,22 @@
+/* Exim: OS-specific C header file for GNU/kNetBSD */
+
+#define CRYPT_H
+#define GLIBC_IP_OPTIONS
+#define HAVE_MMAP
+#define HAVE_BSD_GETLOADAVG
+#define HAVE_SYS_VFS_H
+#define NO_IP_VAR_H
+#define SIG_IGN_WORKS
+
+#define F_FREESP     O_TRUNC
+typedef struct flock flock_t;
+
+#define os_strsignal strsignal
+#define OS_STRSIGNAL
+
+/* kNetBSD-specific bits below */
+
+#define HAVE_SYS_MOUNT_H
+#define SIOCGIFCONF_GIVES_ADDR
+
+/* End */
diff --git a/OS/os.h-HI-OSF b/OS/os.h-HI-OSF
new file mode 100644 (file)
index 0000000..76bd429
--- /dev/null
@@ -0,0 +1,9 @@
+/* Exim: OS-specific C header file for HI-OSF/1-MJ and HI-UX/MPP */
+
+#define HAVE_SYS_MOUNT_H
+
+typedef struct flock           flock_t;
+#define F_FREESP               O_TRUNC
+#define DN_EXPAND_ARG4_TYPE    u_char *
+
+/* End */
diff --git a/OS/os.h-HI-UX b/OS/os.h-HI-UX
new file mode 100644 (file)
index 0000000..97b83ed
--- /dev/null
@@ -0,0 +1,18 @@
+/* Exim: OS-specific C header file for HI-UX */
+
+#define LOAD_AVG_NEEDS_ROOT
+#define HAVE_DEV_KMEM
+#define LOAD_AVG_TYPE       double
+#define LOAD_AVG_SYMBOL     "avenrun"
+#define KERNEL_PATH         "/HI-UX"
+#define FSCALE              1.0
+
+#define HAVE_SYS_VFS_H
+
+#define SELECT_ARG2_TYPE    int
+#define F_FREESP            O_TRUNC
+#define NEED_H_ERRNO        1
+
+typedef struct flock flock_t;
+
+/* End */
diff --git a/OS/os.h-HP-UX b/OS/os.h-HP-UX
new file mode 100644 (file)
index 0000000..87e4dfc
--- /dev/null
@@ -0,0 +1,27 @@
+/* Exim: OS-specific C header file for HP-UX versions greater than 9 */
+
+#define ICONV_ARG2_TYPE char **
+#define EXIM_SOCKLEN_T size_t
+
+#define LOAD_AVG_NEEDS_ROOT
+#define HAVE_DEV_KMEM
+#define LOAD_AVG_TYPE   double
+#define LOAD_AVG_SYMBOL "avenrun"
+#define KERNEL_PATH     "/stand/vmunix"
+#define FSCALE          1.0
+
+#define HAVE_SYS_STATVFS_H
+
+#define F_FREESP           O_TRUNC
+#define NEED_H_ERRNO       1
+
+typedef struct flock flock_t;
+
+typedef struct __res_state *res_state;
+
+#define LLONG_MIN LONG_LONG_MIN
+#define LLONG_MAX LONG_LONG_MAX
+
+#define strtoll(a,b,c) strtoimax(a,b,c)
+
+/* End */
diff --git a/OS/os.h-HP-UX-9 b/OS/os.h-HP-UX-9
new file mode 100644 (file)
index 0000000..dab965e
--- /dev/null
@@ -0,0 +1,20 @@
+/* Exim: OS-specific C header file for HP-UX version 9 */
+
+#define LOAD_AVG_NEEDS_ROOT
+#define HAVE_DEV_KMEM
+#define LOAD_AVG_TYPE   double
+#define LOAD_AVG_SYMBOL "avenrun"
+#define KERNEL_PATH     "/hp-ux"
+#define FSCALE          1.0
+
+#define HAVE_SYS_VFS_H
+
+#define SELECT_ARG2_TYPE   int
+#define F_FREESP           O_TRUNC
+#define NEED_H_ERRNO       1
+
+#define killpg(pgid,sig)   kill(-(pgid),sig)
+
+typedef struct flock flock_t;
+
+/* End */
diff --git a/OS/os.h-IRIX b/OS/os.h-IRIX
new file mode 100644 (file)
index 0000000..ac5a6b3
--- /dev/null
@@ -0,0 +1,20 @@
+/* Exim: OS-specific C header file for IRIX */
+
+#define DN_EXPAND_ARG4_TYPE  u_char *
+
+#define LOAD_AVG_NEEDS_ROOT
+#define HAVE_DEV_KMEM
+#define LOAD_AVG_TYPE   long
+#define LOAD_AVG_SYMBOL "avenrun"
+#define KERNEL_PATH     "/unix"
+#define FSCALE          1000.0
+
+#define HAVE_MMAP
+#define HAVE_SYS_STATVFS_H
+#define F_FAVAIL        f_favail
+#define vfork fork
+
+/* Other OS have "const" in here */
+#define ICONV_ARG2_TYPE char **
+
+/* End */
diff --git a/OS/os.h-IRIX6 b/OS/os.h-IRIX6
new file mode 100644 (file)
index 0000000..c41a234
--- /dev/null
@@ -0,0 +1,19 @@
+/* Exim: OS-specific C header file for IRIX */
+
+#define CRYPT_H
+#define LOAD_AVG_NEEDS_ROOT
+#define HAVE_DEV_KMEM
+#define LOAD_AVG_TYPE   long
+#define LOAD_AVG_SYMBOL "avenrun"
+#define KERNEL_PATH     "/unix"
+#define FSCALE          1000.0
+
+#define HAVE_MMAP
+#define HAVE_SYS_STATVFS_H
+#define F_FAVAIL        f_favail
+#define vfork fork
+
+/* Other OS have "const" in here */
+#define ICONV_ARG2_TYPE char **
+
+/* End */
diff --git a/OS/os.h-IRIX632 b/OS/os.h-IRIX632
new file mode 100644 (file)
index 0000000..0196931
--- /dev/null
@@ -0,0 +1,21 @@
+/* Exim: OS-specific C header file for IRIX */
+
+#define CRYPT_H
+#define DN_EXPAND_ARG4_TYPE  u_char *
+
+#define LOAD_AVG_NEEDS_ROOT
+#define HAVE_DEV_KMEM
+#define LOAD_AVG_TYPE   long
+#define LOAD_AVG_SYMBOL "avenrun"
+#define KERNEL_PATH     "/unix"
+#define FSCALE          1000.0
+
+#define HAVE_MMAP
+#define HAVE_SYS_STATVFS_H
+#define F_FAVAIL        f_favail
+#define vfork fork
+
+/* Other OS have "const" in here */
+#define ICONV_ARG2_TYPE char **
+
+/* End */
diff --git a/OS/os.h-IRIX65 b/OS/os.h-IRIX65
new file mode 100644 (file)
index 0000000..683c66a
--- /dev/null
@@ -0,0 +1,19 @@
+/* Exim: OS-specific C header file for IRIX 6.5 */
+
+#define CRYPT_H
+#define LOAD_AVG_NEEDS_ROOT
+#define HAVE_DEV_KMEM
+#define LOAD_AVG_TYPE   long
+#define LOAD_AVG_SYMBOL "avenrun"
+#define KERNEL_PATH     "/unix"
+#define FSCALE          1000.0
+
+#define HAVE_MMAP
+#define HAVE_SYS_STATVFS_H
+#define F_FAVAIL        f_favail
+#define vfork fork
+
+/* Other OS have "const" in here */
+#define ICONV_ARG2_TYPE char **
+
+/* End */
diff --git a/OS/os.h-Linux b/OS/os.h-Linux
new file mode 100644 (file)
index 0000000..3fead17
--- /dev/null
@@ -0,0 +1,72 @@
+/* Exim: OS-specific C header file for Linux */
+
+/* Some old systems we've received bug-reports for have a <limits.h> which
+does not pull in <features.h>.  Best to just pull it in now and have done
+with the issue. */
+
+#include <features.h>
+
+
+#define CRYPT_H
+#define GLIBC_IP_OPTIONS
+#define HAVE_MMAP
+#define HAVE_BSD_GETLOADAVG
+#define HAVE_SYS_STATVFS_H
+#define NO_IP_VAR_H
+#define SIG_IGN_WORKS
+
+/* When using the DKIM, setting HAVE_LINUX_SENDFILE can increase
+performance on outgoing mail a bit. Note: With older glibc versions
+this setting will conflict with the _FILE_OFFSET_BITS=64 setting
+defined as part of the Linux CFLAGS. */
+
+/* #define HAVE_LINUX_SENDFILE */
+
+#define F_FREESP     O_TRUNC
+typedef struct flock flock_t;
+
+#define os_strsignal strsignal
+#define OS_STRSIGNAL
+
+#if defined(__linux__) || defined(__FreeBSD_kernel__) || defined(__NetBSD_kernel__)
+#define SIOCGIFCONF_GIVES_ADDR
+#define HAVE_SYS_MOUNT_H
+#endif
+
+#if defined(__linux__)
+
+/* Some versions of Linux need explicit sync-ing of directories as well as
+files. This setting requests that. If the directory is on NFS, it may not
+be possible to sync it - in that case, Exim now should ignore the error. But
+if you have problems in that area, try undefining this. But be aware that you
+may be in a situation where files are not being properly "committed to stable
+storage" as quickly as Exim thinks they are. */
+
+#define NEED_SYNC_DIRECTORY
+
+/* Other OS have "const" in here */
+#define ICONV_ARG2_TYPE char **
+
+#define os_find_running_interfaces os_find_running_interfaces_linux
+
+/* Need a prototype for the Linux-specific function. The structure hasn't
+been defined yet, so we need to pre-declare it. */
+
+struct ip_address_item;
+extern struct ip_address_item *os_find_running_interfaces_linux(void);
+
+#endif /* __linux__ */
+
+/* Some folks running "unusual" setups with very old libc environments have
+found that _GNU_SOURCE=1 before <features.h> is not sufficient to define some
+constants needed for 64-bit arithmetic.  If you encounter build errors based
+on LLONG_MIN being undefined and various other escape hatches have not helped,
+then change the 0 to 1 in the next block. */
+
+#if 0
+# define LLONG_MIN LONG_LONG_MIN
+# define LLONG_MAX LONG_LONG_MAX
+#endif
+
+
+/* End */
diff --git a/OS/os.h-NetBSD b/OS/os.h-NetBSD
new file mode 100644 (file)
index 0000000..19a8ac0
--- /dev/null
@@ -0,0 +1,25 @@
+/* Exim: OS-specific C header file for NetBSD */
+
+#define HAVE_BSD_GETLOADAVG
+#define HAVE_GETIFADDRS
+#define HAVE_MMAP
+#define HAVE_SYS_MOUNT_H
+#define SIOCGIFCONF_GIVES_ADDR
+#define HAVE_ARC4RANDOM
+
+typedef struct flock flock_t;
+
+#define os_strsignal strsignal
+#define OS_STRSIGNAL
+
+#define os_get_dns_resolver_res __res_get_state
+#define os_put_dns_resolver_res(RP) __res_put_state(RP)
+#define OS_GET_DNS_RESOLVER_RES
+
+#include <sys/param.h>
+
+#if __NetBSD_Version__ >= 299000900
+#define HAVE_SYS_STATVFS_H
+#endif
+
+/* End */
diff --git a/OS/os.h-NetBSD-a.out b/OS/os.h-NetBSD-a.out
new file mode 100644 (file)
index 0000000..29a8fee
--- /dev/null
@@ -0,0 +1,5 @@
+/* Exim: OS-specific C header file for NetBSD (a.out binary format) */
+
+#include "../OS/os.h-NetBSD"     /* Same as for ELF format */
+
+/* End */
diff --git a/OS/os.h-OSF1 b/OS/os.h-OSF1
new file mode 100644 (file)
index 0000000..f04a5b7
--- /dev/null
@@ -0,0 +1,19 @@
+/* Exim: OS-specific C header file for OSF1 */
+
+#define HAVE_SYS_MOUNT_H
+#define HAVE_GETIPNODEBYNAME    1
+
+typedef struct flock flock_t;
+#define F_FREESP     O_TRUNC
+
+/* This was here for some time, but it seems that now (June 2005) things have
+changed. */
+/* #define EXIM_SOCKLEN_T    size_t */
+
+/* Still not "socklen_t", which is the most common setting */
+#define EXIM_SOCKLEN_T       int
+
+/* The default for this is "const char **" */
+#define ICONV_ARG2_TYPE      char **
+
+/* End */
diff --git a/OS/os.h-OpenBSD b/OS/os.h-OpenBSD
new file mode 100644 (file)
index 0000000..55bade6
--- /dev/null
@@ -0,0 +1,16 @@
+/* Exim: OS-specific C header file for OpenBSD */
+
+#define HAVE_BSD_GETLOADAVG
+#define HAVE_MMAP
+#define HAVE_SYS_MOUNT_H
+#define SIOCGIFCONF_GIVES_ADDR
+#define HAVE_ARC4RANDOM
+
+typedef struct flock flock_t;
+
+#define os_strsignal strsignal
+#define OS_STRSIGNAL
+
+typedef struct __res_state *res_state;
+
+/* End */
diff --git a/OS/os.h-OpenUNIX b/OS/os.h-OpenUNIX
new file mode 100644 (file)
index 0000000..90be8d5
--- /dev/null
@@ -0,0 +1,16 @@
+/* Exim: OS-specific C header file for OpenUNIX */
+
+#define NO_SYSEXITS
+
+#define LOAD_AVG_NEEDS_ROOT
+#define HAVE_DEV_KMEM
+#define LOAD_AVG_TYPE   short
+#define LOAD_AVG_SYMBOL "avenrun"
+#define KERNEL_PATH     "/stand/unix"
+#define FSCALE          256
+
+#define HAVE_SYS_STATVFS_H
+#define _SVID3
+#define NEED_H_ERRNO
+
+/* End */
diff --git a/OS/os.h-QNX b/OS/os.h-QNX
new file mode 100644 (file)
index 0000000..106b0a6
--- /dev/null
@@ -0,0 +1,21 @@
+/* Exim: OS-specific C header file for QNX */
+/* Modified for QNX 6.2.0 with diffs from Samuli Tuomola. */
+
+#include <sys/select.h>
+
+/* This include is wrapped in an ifdef so as to be skipped for QNXRTP, which
+doesn't have/need this header file. From Karsten P. Hoffmann. */
+
+#ifdef __QNX__
+#include <unix.h>
+#endif
+
+#undef HAVE_STATFS
+#undef HAVE_VFS_H
+#undef HAVE_SYS_MOUNT_H
+
+#define NO_SYSEXITS
+
+extern int h_errno;
+
+/* End */
diff --git a/OS/os.h-SCO b/OS/os.h-SCO
new file mode 100644 (file)
index 0000000..07d21bd
--- /dev/null
@@ -0,0 +1,18 @@
+/* Exim: OS-specific C header file for SCO */
+
+#define DN_EXPAND_ARG4_TYPE    u_char *
+
+#define LOAD_AVG_NEEDS_ROOT
+#define HAVE_DEV_KMEM
+#define LOAD_AVG_TYPE   short
+#define LOAD_AVG_SYMBOL "avenrun"
+#define KERNEL_PATH     "/unix"
+#define FSCALE          256
+#define EXIM_SOCKLEN_T  int
+
+#define HAVE_SYS_STATVFS_H
+#define F_FAVAIL        f_favail
+#define _SVID3
+#define NEED_H_ERRNO
+
+/* End */
diff --git a/OS/os.h-SCO_SV b/OS/os.h-SCO_SV
new file mode 100644 (file)
index 0000000..486fcbe
--- /dev/null
@@ -0,0 +1,16 @@
+/* Exim: OS-specific C header file for SCO_SV */
+
+#define LOAD_AVG_NEEDS_ROOT
+#define HAVE_DEV_KMEM
+#define LOAD_AVG_TYPE   short
+#define LOAD_AVG_SYMBOL "avenrun"
+#define KERNEL_PATH     "/unix"
+#define FSCALE          256
+#define EXIM_SOCKLEN_T  int
+
+#define HAVE_SYS_STATVFS_H
+#define F_FAVAIL        f_favail
+#define _SVID3
+#define NEED_H_ERRNO
+
+/* End */
diff --git a/OS/os.h-SunOS4 b/OS/os.h-SunOS4
new file mode 100644 (file)
index 0000000..b0deefc
--- /dev/null
@@ -0,0 +1,36 @@
+/* Exim: OS-specific C header file for SunOS4 */
+
+#define LOAD_AVG_NEEDS_ROOT
+#define HAVE_DEV_KMEM
+#define LOAD_AVG_TYPE   long
+#define LOAD_AVG_SYMBOL "_avenrun"
+#define KERNEL_PATH     "/vmunix"
+
+#define HAVE_MMAP
+#define HAVE_SYS_VFS_H
+
+#define F_FREESP     O_TRUNC
+#define EXIT_FAILURE 1
+#define EXIT_SUCCESS 0
+typedef struct flock flock_t;
+
+#define STRERROR_FROM_ERRLIST
+#define memmove(a, b, c) bcopy(b, a, c)
+#define strtoul(str, ptr, base) ((unsigned int)strtol((str),(ptr),(base)))
+
+extern char *strerror(int);
+extern int   sys_nerr;
+extern char *sys_errlist[];
+
+/* In ANSI C strtod() is defined in stdlib.h, but in SunOS4 it is defined in
+floatingpoint.h which is called from math.h, which Exim doesn't include. */
+
+extern double strtod(const char *, char **);
+
+/* SunOS4 seems to define getc, ungetc, feof and ferror as macros only, not
+as functions. We need to have them as assignable functions. Setting this
+flag causes this to get done in exim.h. */
+
+#define FUDGE_GETC_AND_FRIENDS
+
+/* End */
diff --git a/OS/os.h-SunOS5 b/OS/os.h-SunOS5
new file mode 100644 (file)
index 0000000..8bc0799
--- /dev/null
@@ -0,0 +1,31 @@
+/* Exim: OS-specific C header file for SunOS5 aka Solaris */
+
+#define CRYPT_H
+#define HAVE_MMAP
+#define HAVE_SYS_STATVFS_H
+#define F_FAVAIL                f_favail
+#define SIOCGIFCONF_GIVES_ADDR
+
+#define HAVE_GETIPNODEBYNAME    1
+#define HAVE_GETIPNODEBYADDR    1
+
+#define HAVE_KSTAT
+#define LOAD_AVG_KSTAT         "system_misc"
+#define LOAD_AVG_KSTAT_MODULE  "unix"
+#define LOAD_AVG_SYMBOL        "avenrun_1min"
+#define LOAD_AVG_FIELD          value.ui32
+
+#define os_strsignal            strsignal
+#define OS_STRSIGNAL
+
+/* This is needed for some early Solaris releases, but causes trouble
+in the current ones, so it is out by default. */
+
+/* #define EXIM_SOCKLEN_T       size_t */
+
+/* This is different from Linux and all other PAM implementations,
+it seems. */
+
+#define PAM_CONVERSE_ARG2_TYPE  struct pam_message
+
+/* End */
diff --git a/OS/os.h-SunOS5-hal b/OS/os.h-SunOS5-hal
new file mode 100644 (file)
index 0000000..044e09b
--- /dev/null
@@ -0,0 +1,11 @@
+/* Exim: OS-specific C header file for SunOS5 on HAL */
+
+#define HAVE_MMAP
+
+#define HAVE_KSTAT
+#define LOAD_AVG_KSTAT        "system_misc"
+#define LOAD_AVG_KSTAT_MODULE "unix"
+#define LOAD_AVG_SYMBOL       "avenrun_1min"
+#define LOAD_AVG_FIELD         value.ul
+
+/* End */
diff --git a/OS/os.h-ULTRIX b/OS/os.h-ULTRIX
new file mode 100644 (file)
index 0000000..9985af2
--- /dev/null
@@ -0,0 +1,15 @@
+/* Exim: OS-specific C header file for Ultrix */
+
+/* Well, it *does* have statfs(), but its structure is called something
+different, all the members have different names, and the function returns
+1 on success rather than 0. As this is for a minority function, and I think
+a minority operating system, easiest just to say "no" until someone asks. */
+
+#undef HAVE_STATFS
+
+#define F_FREESP     O_TRUNC
+#define NEED_H_ERRNO
+#define NO_OPENLOG
+typedef struct flock flock_t;
+
+/* End */
diff --git a/OS/os.h-UNIX_SV b/OS/os.h-UNIX_SV
new file mode 100644 (file)
index 0000000..9ad824a
--- /dev/null
@@ -0,0 +1,22 @@
+/* Exim: OS-specific C header file for SCO SVR4.2 (and maybe Unixware) */
+
+/**
+*** Note that for SCO 5 the configuration file is called SCO_SV,
+*** and that Unixware7 has its own configuration. This is an old
+*** file that is retained for compatibility.
+**/
+
+#define NO_SYSEXITS
+
+#define LOAD_AVG_NEEDS_ROOT
+#define HAVE_DEV_KMEM
+#define LOAD_AVG_TYPE   short
+#define LOAD_AVG_SYMBOL "avenrun"
+#define KERNEL_PATH     "/stand/unix"
+#define FSCALE          256
+
+#define HAVE_SYS_STATVFS_H
+#define _SVID3
+#define NEED_H_ERRNO
+
+/* End */
diff --git a/OS/os.h-USG b/OS/os.h-USG
new file mode 100644 (file)
index 0000000..1c780ee
--- /dev/null
@@ -0,0 +1,16 @@
+/* Exim: OS-specific C header file for Unixware 2.x */
+
+#define NO_SYSEXITS
+
+#define LOAD_AVG_NEEDS_ROOT
+#define HAVE_DEV_KMEM
+#define LOAD_AVG_TYPE   short
+#define LOAD_AVG_SYMBOL "avenrun"
+#define KERNEL_PATH     "/stand/unix"
+#define FSCALE          256
+
+#define HAVE_SYS_STATVFS_H
+#define _SVID3
+#define NEED_H_ERRNO
+
+/* End */
diff --git a/OS/os.h-Unixware7 b/OS/os.h-Unixware7
new file mode 100644 (file)
index 0000000..1592094
--- /dev/null
@@ -0,0 +1,19 @@
+/* Exim: OS-specific C header file for Unixware 7 */
+
+#define NO_SYSEXITS
+
+#define ICONV_ARG2_TYPE char **
+#define EXIM_SOCKLEN_T size_t
+
+#define LOAD_AVG_NEEDS_ROOT
+#define HAVE_DEV_KMEM
+#define LOAD_AVG_TYPE   short
+#define LOAD_AVG_SYMBOL "avenrun"
+#define KERNEL_PATH     "/stand/unix"
+#define FSCALE          256
+
+#define HAVE_SYS_STATVFS_H
+#define _SVID3
+#define NEED_H_ERRNO
+
+/* End */
diff --git a/OS/os.h-cygwin b/OS/os.h-cygwin
new file mode 100644 (file)
index 0000000..740300a
--- /dev/null
@@ -0,0 +1,51 @@
+/* Exim: OS-specific C header file for Cygwin */
+
+/* This code was supplied by Pierre A. Humblet <Pierre.Humblet@ieee.org> */
+
+/* Define the OS_INIT macro that we insert in exim.c:main()
+   to set the root and exim uid depending on the system */
+/* We use a special routine to initialize */
+void cygwin_init(int, char **, void *, void *, void *, void *, void *);
+#define OS_INIT\
+  cygwin_init(argc, (char **) argv, &root_uid, &exim_uid, &exim_gid, &config_uid, &config_gid);
+
+/* We need a special mkdir that
+   allows names starting with // */
+#include <sys/stat.h> /* Do not redefine mkdir in sys/stat.h */
+int cygwin_mkdir( const char *_path, mode_t __mode );
+#define mkdir cygwin_mkdir /* redefine mkdir elsewhere */
+
+/* Redefine the set*id calls to run when faking root */
+#include <unistd.h>   /* Do not redefine in unitsd.h */
+int cygwin_setuid(uid_t uid );
+int cygwin_setgid(gid_t gid );
+#define setuid cygwin_setuid
+#define setgid cygwin_setgid
+
+extern unsigned int cygwin_WinVersion;
+
+#define BASE_62 36  /* Windows aliases lower and upper cases in filenames.
+                       Consider reducing MAX_LOCALHOST_NUMBER */
+#define CRYPT_H
+#define HAVE_MMAP
+#define HAVE_SYS_VFS_H
+#define NO_IP_VAR_H
+#define NO_IP_OPTIONS
+#define F_FREESP     O_TRUNC
+/* Defining LOAD_AVG_NEEDS_ROOT causes an initial
+   call to os_getloadavg. In our case this is beneficial
+   because it initializes the counts */
+#define LOAD_AVG_NEEDS_ROOT
+
+typedef struct flock flock_t;
+
+/* Macro to define variable length SID structures */
+#define SID(n, name, sid...) \
+struct  { \
+  BYTE  Revision; \
+  BYTE  SubAuthorityCount; \
+  SID_IDENTIFIER_AUTHORITY IdentifierAuthority; \
+  DWORD SubAuthority[n]; \
+} name = { SID_REVISION, n, {SECURITY_NT_AUTHORITY}, {sid}}
+
+/* End */
diff --git a/OS/os.h-mips b/OS/os.h-mips
new file mode 100644 (file)
index 0000000..79f3ff2
--- /dev/null
@@ -0,0 +1,24 @@
+/* Exim: OS-specific C header file for RiscOS4bsd */
+
+#define LOAD_AVG_NEEDS_ROOT
+#define HAVE_DEV_KMEM
+#define LOAD_AVG_TYPE   long
+#define LOAD_AVG_SYMBOL "_avenrun"
+#define KERNEL_PATH     "/unix"
+
+#define HAVE_MMAP
+#define HAVE_SYS_VFS_H
+
+#define F_FREESP     O_TRUNC
+#define EXIT_FAILURE 1
+#define EXIT_SUCCESS 0
+typedef struct flock flock_t;
+
+#define STRERROR_FROM_ERRLIST
+#define memmove(a, b, c) bcopy(b, a, c)
+
+extern char *strerror(int);
+extern int   sys_nerr;
+extern char *sys_errlist[];
+
+/* End */
diff --git a/README b/README
new file mode 100644 (file)
index 0000000..652cee6
--- /dev/null
+++ b/README
@@ -0,0 +1,349 @@
+THE EXIM MAIL TRANSFER AGENT VERSION 4
+--------------------------------------
+
+Copyright (c) 1995 - 2012 University of Cambridge.
+See the file NOTICE for conditions of use and distribution.
+
+There is a book about Exim by Philip Hazel called "The Exim SMTP Mail Server",
+published by UIT Cambridge in May 2003. This is the official guide for Exim 4.
+The current edition covers release 4.10 and a few later extensions.
+
+The O'Reilly book about Exim ("Exim The Mail Transfer Agent" by Philip Hazel)
+covers Exim 3, which is now deprecated. Exim 4 has a large number of changes
+from Exim 3, though the basic structure and philosophy remains the same. The
+older book may be helpful for the background, but a lot of the detail has
+changed, so it is likely to be confusing to newcomers.
+
+There is a web site at http://www.exim.org; this contains details of the
+mailing list exim-users@exim.org.
+
+A copy of the Exim FAQ should be available from the same source that you used
+to obtain the Exim distribution. Additional formats for the documentation
+(PostScript, PDF, Texinfo, and HTML) should also be available there.
+
+
+EXIM DISTRIBUTION
+-----------------
+
+Unpacking the tar file should produce a single directory called exim-<version>,
+containing the following files and directories:
+
+ACKNOWLEDGMENTS  some acknowledgments
+CHANGES          a conventional file name; it indirects to some files in doc/
+LICENCE          the GNU General Public Licence
+Local/           an empty directory for local configuration files
+Makefile         top level Makefile
+NOTICE           notice about conditions of use
+OS/              directory containing OS-specific files
+README           this file
+README.UPDATING  special notes about updating from previous versions
+doc/             directory of documentation files
+exim_monitor/    directory of source files for the Exim monitor
+scripts/         directory of scripts used in the build process
+src/             directory of source files
+util/            directory of independent utilities
+
+Please see the documentation files for full instructions on how to build,
+install, and run Exim. For straightforward installations on operating systems
+to which Exim has already been ported, the building process is as follows:
+
+. Ensure that the top-level Exim directory (e.g. exim-4.80) is the current
+  directory (containing the files and directories listed above).
+
+. Edit the file called src/EDITME and put the result in a new file called
+  Local/Makefile. There are comments in src/EDITME telling you what the various
+  parameters are. You must at least provide values for BIN_DIRECTORY,
+  CONFIGURE_FILE, EXIM_USER and EXIM_GROUP (if EXIM_USER is numeric), and it is
+  recommended that SPOOL_DIRECTORY also be defined here if it is a fixed path.
+
+. There are a number of additional parameters whose defaults can also be
+  overridden by additions to Local/Makefile. The basic defaults are in
+  OS/Makefile-Default, but these settings are overridden for some operating
+  systems by values on OS/Makefile-<osname>. The most commonly-required change
+  is probably the setting of CC, which defines the command to run the C
+  compiler, and which defaults to gcc. To change it to cc, add the following
+  line to Local/Makefile:
+
+  CC=cc
+
+  If you are running the Berkeley DB package as your dbm library, then it is
+  worth putting USE_DB=yes in Local/Makefile, to get Exim to use the native
+  interface. This is the default for some operating systems. See
+  doc/dbm.discuss.txt for discussion on dbm libraries.
+
+. If you want to compile the Exim monitor, edit the file called
+  exim_monitor/EDITME and put the result in a file called Local/eximon.conf.
+  If you are not going to compile the Exim monitor, you should have commented
+  out the line starting EXIM_MONITOR= when creating Local/Makefile. There are
+  comments in exim_monitor/EDITME about the values set therein, but in this
+  case everything can be defaulted if you wish.
+
+. If your system is not POSIX compliant by default, then you might experience
+  fewer problems if you help point the build tools to the POSIX variants. For
+  instance, on Solaris:
+
+  PATH=/usr/xpg4/bin:$PATH make SHELL=/usr/xpg4/bin/sh
+
+. Type "make". This will determine what your machine's architecture and
+  operating system are, and create a build directory from those names (e.g.
+  "build-SunOS5-sparc"). Symbolic links are created from the build directory
+  to the source directory. A configured make file called <build-dir>/makefile
+  is then created, and "make" then goes on to use this to build various
+  binaries and scripts inside the build directory.
+
+. Type "make install", while running as root, to install the binaries,
+  scripts, and a default configuration file. To see what this command is
+  going to do before risking it, run "../scripts/exim_install -n" (not as
+  root) from within the build directory.
+
+. When you are ready to try running Exim, see the section entitled "Testing"
+  in the chapter called "Building and Installing Exim" in doc/spec.txt, or in
+  one of the other forms of the documentation.
+
+. Running the install script does NOT replace /usr/sbin/sendmail or
+  /usr/lib/sendmail with a link to Exim. That step you must perform by hand
+  when you are satisfied that Exim is running correctly.
+
+. Note that the default configuration refers to an alias file called
+  /etc/aliases. It used to be the case that every Unix had that file, because
+  it was the Sendmail default. These days, there are systems that don't have
+  /etc/aliases, so you might need to set it up. Your aliases should at least
+  include an alias for "postmaster".
+
+. Consider notifying users of the change of MTA. Exim has different
+  capabilities, and there are various operational differences, such as stricter
+  adherence to the RFCs than some MTAs, and differences in the text of
+  messages produced by various command-line options.
+
+. The default configuration file will use your host's fully qualified name (as
+  obtained from the uname() function) as the only local mail domain and as the
+  domain which is used to qualify unqualified local mail addresses. See the
+  comments in the default configuration file if you want to change these.
+
+The operating systems currently supported are: AIX, BSD/OS (aka BSDI), Darwin
+(Mac OS X), DGUX, FreeBSD, GNU/Hurd, GNU/Linux, HI-OSF (Hitachi), HP-UX, IRIX,
+MIPS RISCOS, NetBSD, OpenBSD, QNX, SCO, SCO SVR4.2 (aka UNIX-SV), Solaris (aka
+SunOS5), SunOS4, Tru64-Unix (formerly Digital Unix, formerly DEC-OSF1), Ultrix,
+and Unixware. However, code is not available for determining system load
+averages on Ultrix. There are also configuration files for compiling Exim in
+the Cygwin environment that can be installed on systems running Windows.
+However, the documentation supplied with the distribution does not contain any
+information about running Exim in the Cygwin environment.
+
+
+******* Modifying the building process ******
+
+Instructions for overriding the build-time options for Exim are given in the
+manual. You should never have to modify any of the supplied files; it should be
+possible to override everything that is necessary by creating suitable files in
+the Local directory. This means that you won't need to redo your modifications
+for the next release of Exim. If you find you can't avoid changing some other
+file, let me know and I'll see if I can find a way of making that unnecessary.
+
+Briefly, the building process concatenates a number of files in order to
+construct its working makefile. If <ostype> and <archtype> are the operating
+system and architecture types respectively, the files used are:
+
+  OS/Makefile-Default
+  OS/Makefile-<ostype>
+  Local/Makefile
+  Local/Makefile-<ostype>
+  Local/Makefile-<archtype>
+  Local/Makefile-<ostype>-<archtype>
+  OS/Makefile-Base
+
+Of the Local/* files, only Local/Makefile is required to exist; the rest are
+optional. Because of the way "make" works, values set in later files override
+values set in earlier ones. Thus you can set up general options that are
+overridden for specify operating systems and/or architectures if you wish.
+
+
+******* IMPORTANT FOR GNU/LINUX USERS *******
+
+Exim 4 won't work with some versions of Linux if you put its spool directory on
+an NFS partition. You get an error about "directory sync failed". This is
+because of a bug in Linux NFS. A fix has been promised in due course. It is in
+any case much better to put Exim's spool directory on local disc.
+
+If you get an error complaining about the lack of functions such as dbm_open()
+when building Exim, the problem is that it hasn't been able to find a DBM
+library. See the file doc/dbm.discuss.txt for a discussion about the various
+DBM libraries.
+
+Different versions of Linux come with different DBM libraries, stored in
+different places. As well as setting USE_DB=yes in Local/Makefile if Berkeley
+DB is in use, it may also be necessary to set a value in DBMLIB to specify the
+inclusion of the DBM library, for example: DBMLIB=-ldb or DBMLIB=-lgdbm.
+
+If you are using RedHat 7.0, which has DB3 as its DBM library, you need to
+install the db-devel package before building Exim. This will have a name like
+db3-devel-3.1.14-16.i386.rpm (but check which release of DB3 you have).
+
+The building scripts now distinguish between versions of Linux with the older
+libc5 and the more recent ones that use libc6. In the latter case, USE_DB and
+-ldb are the default settings, because DB is standard with libc6.
+
+It appears that with glibc-2.1.x (a minor libc upgrade), they have standardised
+on Berkeley DB2 (instead of DB1 in glibc-2.0.x). If you want to get DB1 back,
+you need to set
+
+  INCLUDE=-I/usr/include/db1
+  DBMLIB=-ldb1
+
+in your Local/Makefile. If you omit DBMLIB=-ldb1 Exim will link successfully
+using the DB1 compatibility interface to DB2, but it will expect the file
+format to be that of DB2, and so will not be able to read existing DB1 files.
+
+
+******* IMPORTANT FOR FREEBSD USERS *******
+
+On FreeBSD there is a file called /etc/mail/mailer.conf which selects what to
+run for various MTA calls. Instead of changing /usr/sbin/sendmail, you should
+edit this file instead, to read something like this:
+
+sendmail          /usr/exim/bin/exim
+send-mail         /usr/exim/bin/exim
+mailq             /usr/exim/bin/exim -bp
+newaliases        /usr/bin/true
+
+You will most probably need to add the line:
+
+daily_status_include_submit_mailq="NO"  # No separate 'submit' queue
+
+to /etc/periodic.conf. This stops FreeBSD running the command "mailq -Ac"
+(which Exim doesn't understand) to list a separate submit queue (which Exim
+doesn't have).
+
+If you are using FreeBSD prior to 3.0-RELEASE, and you are not using the ports
+mechanism to install Exim, then you should install the perl5 package
+(/usr/local/bin/perl) and use that instead of perl in the base system, which is
+perl4 up until 3.0-RELEASE. If you are using the ports mechanism, this is
+handled for you.
+
+If you are upgrading from version 2.11 of Exim or earlier, and you are using
+DBM files, and you did not previously have USE_DB=yes in your Local/Makefile,
+then you will either have to put USE_DB=no in your Local/Makefile or (better)
+rebuild your DBM data files. The default for FreeBSD has been changed to
+USE_DB=yes, since FreeBSD comes with Berkeley DB. However, using the native DB
+interface means that the data files no longer have the ".db" extension.
+
+
+
+******* IMPORTANT FOR Tru64 (aka Digital Unix aka DEC-OSF1) USERS *******
+
+The default compiler may not recognize ANSI C by default. You may have to set
+
+CC=cc
+CFLAGS=-std1
+
+in Local/Makefile in order to compile Exim. A user reported another small
+problem with this operating system: In the file /usr/include/net/if.h a
+semicolon was missing at the end of line 143.
+
+
+
+******* IMPORTANT FOR SCO USERS *******
+
+The building scripts assume the existence of the "ar" command, which is part of
+the Development System. However, it is also possible to use the "gar" command
+that is part of the GNU utilities that are distributed with the 5.0.7 release.
+If you have "gar" and not "ar" you should include
+
+AR=gar
+
+in your Local/Makefile.
+
+
+
+******* IMPORTANT FOR Unixware 2.x USERS *******
+
+Unixware does not include db/dbm/ndbm with its standard compiler (it is
+available with /usr/ucb/cc, but that has bugs of its own). You should install
+gcc and Berkeley DB (or another dbm library if you really insist). If you use a
+different dbm library you will need to override the default setting of DBMLIB.
+
+DB 1.85 and 2.x can be found at http://www.sleepycat.com/. They have different
+characteristics. See the discussion of dbm libraries in doc/dbm.discuss.txt. DB
+needs to be compiled with gcc and you need a 'cc' in your path before the
+Unixware CC to compile it.
+
+Don't bother even starting to install exim on Unixware unless you have
+installed gcc and use it for everything.
+
+
+******* IMPORTANT FOR SOLARIS 2.3 (SUNOS 5.3) USERS *******
+
+The file /usr/include/sysexits.h does not exist on Solaris 2.3 (and presumably
+earlier versions), though it is present in 2.4 and later versions. To compile
+Exim on Solaris 2.3 it is necessary to include the line
+
+CFLAGS=-O -DNO_SYSEXITS -DEX_TEMPFAIL=75
+
+in your Local/Makefile.
+
+
+******* IMPORTANT FOR IRIX USERS *******
+
+There are problems with some versions of gcc on IRIX, as a result of which all
+DNS lookups yield either 0.0.0.0 or 255.255.255.255. Releases of gcc after
+2.7.2.3 (which works ok) are affected. Specifically, 2.8.* is affected, as are
+the 2.95 series. From release 3.21 of Exim, a workaround for this problem
+should automatically be enabled when Exim is compiled on IRIX using gcc.
+
+As from version 2.03 there is IRIX-specific code in Exim to obtain a list of
+all the IP addresses on local interfaces, including alias addresses, because
+the standard code gives only non-alias addresses in IRIX. The code came from
+SGI, with the comment:
+
+"On 6.2 you need the libc patch to get the sysctl() stub and the networking
+kernel patch to get the support."
+
+It seems that this code doesn't work on at least some earlier versions of IRIX
+(e.g. IRIX 5.3). If you can't compile under IRIX and the problem appears to
+relate to sysctl(), try commenting or #ifdef-ing out all the code in the
+file OS/os.c-IRIX.
+
+
+******* IMPORTANT FOR HP-UX USERS *******
+
+There are two different sets of configuration files for HP-UX. Those ending in
+HP-UX-9 are used for HP-UX version 9, and have been tested on HP-UX version
+9.05. Those ending in HP-UX are for later releases, and have been tested on
+HP-UX version 11.00. If you are using a version of HP-UX between 9.05 and
+11.00, you may need to edit the file OS/os.h-HP-UX if you encounter problems
+building Exim.
+
+If you want to use the Sieve facility in Exim, the alias iso-8859-1 should be
+added to the alias definition for iso81 in /usr/lib/nls/iconv/config.iconv. You
+also need to add a new alias definition: "alias utf8 utf-8".
+
+
+******* IMPORTANT FOR QNX USERS *******
+
+1. Exim makes some assumptions about the shell in the makefiles. The "normal"
+   QNX shell (ksh) will not work. You need to install "bash", which can be
+   obtained from the QNX freeware on QUICS. Install it to /usr/local/bin/bash
+   Then you need to change the SHELL definition at the top of the main Makefile
+   to SHELL=/usr/local/bin/bash. The file OS/Makefile-QNX sets the variable
+   MAKE_SHELL to /usr/local/bin/bash. If you install bash in a different place,
+   you will need to set MAKE_SHELL in your Local/Makefile in order to override
+   this.
+
+2. For some strange reason make will fail at building "exim_dbmbuild" when
+   called the first time. However simply calling make a second time will solve
+   the problem. Alternatively, run "make makefile" and then "make".
+
+
+******* IMPORTANT FOR ULTRIX USERS *******
+
+You need to set SHELL explicitly in the make call when building on ULTRIX,
+that is, type "make SHELL=sh5".
+
+
+******* IMPORTANT FOR GNU/HURD USERS *******
+
+GNU/Hurd doesn't (at the time of writing, June 1999) have the ioctls for
+finding out the IP addresses of the local interfaces. You therefore have to set
+local_interfaces yourself. Otherwise it will treat only 127.0.0.1 as local.
+
+Philip Hazel
diff --git a/README.DSN b/README.DSN
new file mode 100644 (file)
index 0000000..68d1641
--- /dev/null
@@ -0,0 +1,141 @@
+Exim DSN Patch (4.82)
+---------------------
+
+This patch is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+This patch is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this patch; if not, write to the Free Software
+Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.
+
+Installation & Usage
+--------------------
+See docs/experimental-spec.txt
+
+Credits
+-------
+
+The original work for the patch was done by Philip Hazel in Exim 3
+
+The extract was taken and re-applied to Exim 4 by the following :-
+Phil Bingham   (phil.bingham@cwipapps.net)
+Steve Falla    (steve.falla@cwipapps.net)
+Ray Edah       (ray.edah@cwipapps.net)
+Andrew Johnson (andrew.johnson@cwippaps.net)
+Adrian Hungate (adrian.hungate@cwipapps.net)
+
+Now Primarily maintained by :-
+Andrew Johnson (andrew.johnson@cwippaps.net)
+
+Updated for 4.82, improved and submitted to
+http://bugs.exim.org/show_bug.cgi?id=118
+by :-
+Wolfgang Breyha (wbreyha@gmx.net)
+
+Contributions
+-------------
+Andrey J. Melnikoff (TEMHOTA) (temnota@kmv.ru) 
+
+
+ChangeLog
+---------
+14-Apr-2006 : Changed subject to "Delivery Status Notification"
+
+17-May-2006 : debug_printf in spool-in.c were not wrapped with #ifndef COMPILE_UTILITY
+              thanks to Andrey J. Melnikoff for this information
+
+12-Sep-2006 : Now supports Exim 4.63
+
+12-Sep-2006 : src/EDITME did not include the #define SUPPORT_DSN as stated
+              in the documentation, this has now been corrected
+              thanks to Robert Kehl for this information
+
+28-Jul-2008 : New version for exim 4.69 released.
+
+02-Jul-2010 : New version for exim 4.72 released.
+
+25-Apr-2014 : Version 1.4
+              *) fix ENVID and ORCPT addition in SMTP transport
+                *) p was not moved to the end of the string. new content
+                   added afterwards overwrites ENVID and/or ORCPT
+              *) change spool file format to be compatible with the
+                 extensible format of exim 4 by prepending new values and
+                 setting the extended bitmask accordingly
+                *) use SUPPORT_DSN_LEGACY=yes in Makefile to be able to read
+                   the legacy format of older patches until all messages are out of queue.
+              *) change "dsn" boolean toggle to "dsn_advertise_hosts" to
+                 be able to select who actually can use the extension
+              *) Add all RFC 3461 MUST fields to delivery-status section
+              *) convert xtext in ENVID
+              *) add all successful rcpts to ONE message instead of sending several messages
+
+26-Apr-2014 : Version 1.5
+              fixes:
+                *) fixed wrong order for ENVID
+                *) fixed wrong Final-Recipient value
+                *) af_ignore_failure is ignored for success reports
+                *) fixed DSN_LEGACY switch
+              improvements:
+                *) added MIME "failure" reports
+                  *) bounce_return_message is ignored (required by RFC)
+                  *) in case RET= is defined we honor these values
+                     otherwise bounce_return_body is honored.
+                  *) bounce_return_size_limit is always honored.
+                  *) message body intro and final text is ignored
+                  *) do not send report if DSN flags say NO
+                *) added MIME "delay" reports
+                  *) do not send report if DSN flags say NO
+                *) changed from SUPPORT_DSN to EXPERIMENTAL_DSN
+                *) updated documentation
+
+01-May-2014 : Version 1.6
+              fixes:
+                *) code cleanup
+                *) use text/rfc822-headers were applicable
+                *) fix NOTIFY=FAILURE
+
+              improvements:
+                *) do not truncated MIME messages
+                  *) if bounce_return_size_limit is smaller then the actual message
+                     only the header is returned
+                *) if bounce_return_body or bounce_return_size_limit prevents Exim
+                   from returning the requested (RET=FULL) body this fact is added
+                   as X-Exim-DSN-Information Header
+                  *) this also means that all of the last three parts of the "failure"
+                     template are not used anymore
+
+                *) dsn_process switch removed
+                  *) every router "processes" DSN by default
+                  *) there is no possibilty to "gag" DSN anymore since this violates RFC
+                *) dsn_lasthop switch added for routers
+                  *) if dsn_lasthop is set by a router it is handled as relaying to a
+                     non DSN aware relay. success mails are sent if Exim successfully 
+                     delivers the message.
+                *) redirect routers always "act" as if dsn_lasthop is set
+
+                *) address_item.dsn_aware changed from uschar to int for easier handling.
+
+02-May-2014 : fixes:
+                *) Reporting-MTA: use smtp_active_hostname instead of qualify_domain from
+                   original patch.
+
+20-May-2014 : fixes:
+                *) removed support for EXPERIMENTAL_DSN_LEGACY for codebase inclusion
+                *) fixed build of exim_monitor tree
+                *) fixed late declaration of dsn_all_lasthop
+
+-----------------
+
+Support for this patch up to 1.3 (limited though it is) will only be provided through the SourceForge
+project page (http://sourceforge.net/projects/eximdsn/)
+
+From 1.4 onward feel free to ask on the exim-users mailinglist or add comments to
+http://bugs.exim.org/show_bug.cgi?id=118
+
diff --git a/README.UPDATING b/README.UPDATING
new file mode 100644 (file)
index 0000000..590642f
--- /dev/null
@@ -0,0 +1,757 @@
+This document contains detailed information about incompatibilities that might
+be encountered when upgrading from one release of Exim to another. The
+information is in reverse order of release numbers. Mostly these are relatively
+small points, and the configuration file is normally upwards compatible, but
+there have been two big upheavals...
+
+
+**************************************************************************
+* There was a big reworking of the way mail routing works for release    *
+* 4.00. Previously used "directors" were abolished, and all routing is   *
+* now done by routers. Policy controls for incoming mail are now done by *
+* Access Control Lists instead of separate options. All this means that  *
+* pre-4.00 configuration files have to be massively converted. If you    *
+* are coming from a 3.xx release, please read the document in the file   *
+* doc/Exim4.upgrade, and allow some time to complete the upgrade.        *
+*                                                                        *
+* There was a big reworking of the way domain/host/net/address lists are *
+* handled at release 3.00. If you are coming from a pre-3.00 release, it *
+* might be easier to start again from a default configuration. Otherwise *
+* you need to read doc/Exim3.upgrade and do a double conversion of your  *
+* configuration file.                                                    *
+**************************************************************************
+
+
+The rest of this document contains information about changes in 4.xx releases
+that might affect a running system.
+
+
+Exim version 4.83
+-----------------
+
+ * SPF condition results renamed "permerror" and "temperror".  The old
+   names are still accepted for back-compatability, for this release.
+
+ * TLS details are now logged on rejects, subject to log selectors.
+
+ * Items in headers_remove lists must now have any embedded list-separators
+   doubled.
+
+ * Attempted use of the deprecated options "gnutls_require_kx" et. al.
+   now result in logged warning.
+
+
+Exim version 4.82
+-----------------
+
+ * New option gnutls_allow_auto_pkcs11 defaults false; if you have GnuTLS 2.12.0
+   or later and do want PKCS11 modules to be autoloaded, then set this option.
+
+ * A per-transport wait-<name> database is no longer updated if the transport
+   sets "connection_max_messages" to 1, as it can not be used and causes
+   unnecessary serialisation and load.  External tools tracking the state of
+   Exim by the hints databases may need modification to take this into account.
+
+ * The av_scanner option can now accept multiple clamd TCP targets, all other
+   setting limitations remain.
+
+
+Exim version 4.80
+-----------------
+
+ * BEWARE backwards-incompatible changes in SSL libraries, thus the version
+   bump.  See points below for details.
+   Also an LDAP data returned format change.
+
+ * The value of $tls_peerdn is now print-escaped when written to the spool file
+   in a -tls_peerdn line, and unescaped when read back in.  We received reports
+   of values with embedded newlines, which caused spool file corruption.
+
+   If you have a corrupt spool file and you wish to recover the contents after
+   upgrading, then lock the message, replace the new-lines that should be part
+   of the -tls_peerdn line with the two-character sequence \n and then unlock
+   the message.  No tool has been provided as we believe this is a rare
+   occurence.
+
+ * For OpenSSL, SSLv2 is now disabled by default.  (GnuTLS does not support
+   SSLv2).  RFC 6176 prohibits SSLv2 and some informal surveys suggest no
+   actual usage.  You can re-enable with the "openssl_options" Exim option,
+   in the main configuration section.  Note that supporting SSLv2 exposes
+   you to ciphersuite downgrade attacks.
+
+ * With OpenSSL 1.0.1+, Exim now supports TLS 1.1 and TLS 1.2.  If built
+   against 1.0.1a then you will get a warning message and the
+   "openssl_options" value will not parse "no_tlsv1_1": the value changes
+   incompatibly between 1.0.1a and 1.0.1b, because the value chosen for 1.0.1a
+   is infelicitous.  We advise avoiding 1.0.1a.
+
+   "openssl_options" gains "no_tlsv1_1", "no_tlsv1_2" and "no_compression".
+
+   COMPATIBILITY WARNING: The default value of "openssl_options" is no longer
+   "+dont_insert_empty_fragments".  We default to "+no_sslv2".
+   That old default was grandfathered in from before openssl_options became a
+   configuration option.
+   Empty fragments are inserted by default through TLS1.0, to partially defend
+   against certain attacks; TLS1.1+ change the protocol so that this is not
+   needed.  The DIEF SSL option was required for some old releases of mail
+   clients which did not gracefully handle the empty fragments, and was
+   initially set in Exim release 4.31 (see ChangeLog, item 37).
+
+   If you still have affected mail-clients, and you see SSL protocol failures
+   with this release of Exim, set:
+     openssl_options = +dont_insert_empty_fragments
+   in the main section of your Exim configuration file.  You're trading off
+   security for compatibility.  Exim is now defaulting to higher security and
+   rewarding more modern clients.
+
+   If the option tls_dhparams is set and the parameters loaded from the file
+   have a bit-count greater than the new option tls_dh_max_bits, then the file
+   will now be ignored.  If this affects you, raise the tls_dh_max_bits limit.
+   We suspect that most folks are using dated defaults and will not be affected.
+
+ * Ldap lookups returning multi-valued attributes now separate the attributes
+   with only a comma, not a comma-space sequence.  Also, an actual comma within
+   a returned attribute is doubled.  This makes it possible to parse the
+   attribute as a comma-separated list.  Note the distinction from multiple
+   attributes being returned, where each one is a name=value pair.
+
+   If you are currently splitting the results from LDAP upon a comma, then you
+   should check carefully to see if adjustments are needed.
+
+   This change lets cautious folks distinguish "comma used as separator for
+   joining values" from "comma inside the data".
+
+ * accept_8bitmime now defaults on, which is not RFC compliant but is better
+   suited to today's Internet.  See http://cr.yp.to/smtp/8bitmime.html for a
+   sane rationale.  Those who wish to be strictly RFC compliant, or know that
+   they need to talk to servers that are not 8-bit-clean, now need to take
+   explicit configuration action to default this option off.  This is not a
+   new option, you can safely force it off before upgrading, to decouple
+   configuration changes from the binary upgrade while remaining RFC compliant.
+
+ * The GnuTLS support has been mostly rewritten, to use APIs which don't cause
+   deprecation warnings in GnuTLS 2.12.x.  As part of this, these three options
+   are no longer supported:
+
+     gnutls_require_kx
+     gnutls_require_mac
+     gnutls_require_protocols
+
+   Their functionality is entirely subsumed into tls_require_ciphers.  In turn,
+   tls_require_ciphers is no longer an Exim list and is not parsed by Exim, but
+   is instead given to gnutls_priority_init(3), which expects a priority string;
+   this behaviour is much closer to the OpenSSL behaviour.  See:
+
+     http://www.gnutls.org/manual/html_node/Priority-Strings.html
+
+   for fuller documentation of the strings parsed.  The three gnutls_require_*
+   options are still parsed by Exim and, for this release, silently ignored.
+   A future release will add warnings, before a later still release removes
+   parsing entirely and the presence of the options will be a configuration
+   error.
+
+   Note that by default, GnuTLS will not accept RSA-MD5 signatures in chains.
+   A tls_require_ciphers value of NORMAL:%VERIFY_ALLOW_SIGN_RSA_MD5 may
+   re-enable support, but this is not supported by the Exim maintainers.
+   Our test suite no longer includes MD5-based certificates.
+
+   This rewrite means that Exim will continue to build against GnuTLS in the
+   future, brings Exim closer to other GnuTLS applications and lets us add
+   support for SNI and other features more readily.  We regret that it wasn't
+   feasible to retain the three dropped options.
+
+ * If built with TLS support, then Exim will now validate the value of
+   the main section tls_require_ciphers option at start-up.  Before, this
+   would cause a STARTTLS 4xx failure, now it causes a failure to start.
+   Running with a broken configuration which causes failures that may only
+   be left in the logs has been traded off for something more visible.  This
+   change makes an existing problem more prominent, but we do not believe
+   anyone would deliberately be running with an invalid tls_require_ciphers
+   option.
+
+   This also means that library linkage issues caused by conflicts of some
+   kind might take out the main daemon, not just the delivery or receiving
+   process.  Conceivably some folks might prefer to continue delivering
+   mail plaintext when their binary is broken in this way, if there is a
+   server that is a candidate to receive such mails that does not advertise
+   STARTTLS.  Note that Exim is typically a setuid root binary and given
+   broken linkage problems that cause segfaults, we feel it is safer to
+   fail completely.  (The check is not done as root, to ensure that problems
+   here are not made worse by the check).
+
+ * The "tls_dhparam" option has been updated, so that it can now specify a
+   path or an identifier for a standard DH prime from one of a few RFCs.
+   The default for OpenSSL is no longer to not use DH but instead to use
+   one of these standard primes.  The default for GnuTLS is no longer to use
+   a file in the spool directory, but to use that same standard prime.
+   The option is now used by GnuTLS too.  If it points to a path, then
+   GnuTLS will use that path, instead of a file in the spool directory;
+   GnuTLS will attempt to create it if it does not exist.
+
+   To preserve the previous behaviour of generating files in the spool
+   directory, set "tls_dhparam = historic".  Since prior releases of Exim
+   ignored tls_dhparam when using GnuTLS, this can safely be done before
+   the upgrade.
+
+
+
+Exim version 4.77
+-----------------
+
+ * GnuTLS will now attempt to use TLS 1.2 and TLS 1.1 before TLS 1.0 and SSL3,
+   if supported by your GnuTLS library.  Use the existing
+   "gnutls_require_protocols" option to downgrade this if that will be a
+   problem.  Prior to this release, supported values were "TLS1" and "SSL3",
+   so you should be able to update configuration prior to update.
+
+    [nb: gnutls_require_protocols removed in Exim 4.80, instead use
+         tls_require_ciphers to provide a priority string; see notes above]
+
+ * The match_<type>{string1}{string2} expansion conditions no longer subject
+   string2 to string expansion, unless Exim was built with the new
+   "EXPAND_LISTMATCH_RHS" option.  Too many people have inadvertently created
+   insecure configurations that way.  If you need the functionality and turn on
+   that build option, please let the developers know, and know why, so we can
+   try to provide a safer mechanism for you.
+
+   The match{}{} expansion condition (for regular expressions) is NOT affected.
+   For match_<type>{s1}{s2}, all list functionality is unchanged.  The only
+   change is that a '$' appearing in s2 will not trigger expansion, but instead
+   will be treated as a literal $ sign; the effect is very similar to having
+   wrapped s2 with \N...\N.  If s2 contains a named list and the list definition
+   uses $expansions then those _will_ be processed as normal.  It is only the
+   point at which s2 is read where expansion is inhibited.
+
+   If you are trying to test if two email addresses are equal, use eqi{s1}{s2}.
+   If you are testing if the address in s1 occurs in the list of items given
+   in s2, either use the new inlisti{s1}{s2} condition (added in 4.77) or use
+   the pre-existing forany{s2}{eqi{$item}{s1}} condition.
+
+
+Exim version 4.74
+-----------------
+
+ * The integrated support for dynamically loadable lookup modules has an ABI
+   change from the modules supported by some OS vendors through an unofficial
+   patch. Don't try to mix & match.
+
+ * Some parts of the build system are now beginning to assume that the host
+   environment is POSIX. If you're building on a system where POSIX tools are
+   not the default, you might have an easier time if you switch to the POSIX
+   tools.  Feel free to report non-POSIX issues as a request for a feature
+   enhancement, but if the POSIX variants are available then the fix will
+   probably just involve some coercion.  See the README instructions for
+   building on such hosts.
+
+
+Exim version 4.73
+-----------------
+
+ * The Exim run-time user can no longer be root; this was always
+   strongly discouraged, but is now prohibited both at build and
+   run-time.  If you need Exim to run routinely as root, you'll need to
+   patch the source and accept the risk.  Here be dragons.
+
+ * Exim will no longer accept a configuration file owned by the Exim
+   run-time user, unless that account is explicitly the value in
+   CONFIGURE_OWNER, which we discourage.  Exim now checks to ensure that
+   files are not writeable by other accounts.
+
+ * The ALT_CONFIG_ROOT_ONLY build option is no longer optional and is forced
+   on; the Exim user can, by default, no longer use -C/-D and retain privilege.
+   Two new build options mitigate this.
+
+    * TRUSTED_CONFIG_LIST defines a file containing a whitelist of config
+      files that are trusted to be selected by the Exim user; one per line.
+      This is the recommended approach going forward.
+
+    * WHITELIST_D_MACROS defines a colon-separated list of macro names which
+      the Exim run-time user may safely pass without dropping privileges.
+      Because changes to this involve a recompile, this is not the recommended
+      approach but may ease transition.  The values of the macros, when
+      overridden, are constrained to match this regex: ^[A-Za-z0-9_/.-]*$
+
+ * The system_filter_user option now defaults to the Exim run-time user,
+   rather than root.  You can still set it explicitly to root and this
+   can be done with prior versions too, letting you roll versions
+   without needing to change this configuration option.
+
+ * ClamAV must be at least version 0.95 unless WITH_OLD_CLAMAV_STREAM is
+   defined at build time.
+
+
+Exim version 4.70
+-----------------
+
+1. Experimental Yahoo! Domainkeys support has been dropped in this release.
+It has been superceded by a native implementation of its successor DKIM.
+
+2. Up to version 4.69, Exim came with an embedded version of the PCRE library.
+As of 4.70, this is no longer the case. To compile Exim, you will need PCRE
+installed. Most OS distributions have ready-made library and development
+packages.
+
+
+Exim version 4.68
+-----------------
+
+1. The internal implementation of the database keys that are used for ACL
+ratelimiting has been tidied up. This means that an update to 4.68 might cause
+Exim to "forget" previous rates that it had calculated, and reset them to zero.
+
+
+Exim version 4.64
+-----------------
+
+1. Callouts were setting the name used for EHLO/HELO from $smtp_active_
+hostname. This is wrong, because it relates to the incoming message (and
+probably the interface on which it is arriving) and not to the outgoing
+callout (which could be using a different interface). This has been
+changed to use the value of the helo_data option from the smtp transport
+instead - this is what is used when a message is actually being sent. If
+there is no remote transport (possible with a router that sets up host
+addresses), $smtp_active_hostname is used. This change is mentioned here in
+case somebody is relying on the use of $smtp_active_hostname.
+
+2. A bug has been fixed that might just possibly be something that is relied on
+in some configurations. In expansion items such as ${if >{xxx}{yyy}...} an
+empty string (that is {}) was being interpreted as if it was {0} and therefore
+treated as the number zero. From release 4.64, such strings cause an error
+because a decimal number, possibly followed by K or M, is required (as has
+always been documented).
+
+3. There has been a change to the GnuTLS support (ChangeLog/PH/20) to improve
+Exim's performance. Unfortunately, this has the side effect of being slightly
+non-upwards compatible for versions 4.50 and earlier. If you are upgrading from
+one of these earlier versions and you use GnuTLS, you must remove the file
+called gnutls-params in Exim's spool directory. If you don't do this, you will
+see this error:
+
+  TLS error on connection from ... (DH params import): Base64 decoding error.
+
+Removing the file causes Exim to recompute the relevant encryption parameters
+and cache them in the new format that was introduced for release 4.51 (May
+2005). If you are upgrading from release 4.51 or later, there should be no
+problem.
+
+
+Exim version 4.63
+-----------------
+
+When an SMTP error message is specified in a "message" modifier in an ACL, or
+in a :fail: or :defer: message in a redirect router, Exim now checks the start
+of the message for an SMTP error code. This consists of three digits followed
+by a space, optionally followed by an extended code of the form n.n.n, also
+followed by a space. If this is the case and the very first digit is the same
+as the default error code, the code from the message is used instead. If the
+very first digit is incorrect, a panic error is logged, and the default code is
+used. This is an incompatible change, but it is not expected to affect many (if
+any) configurations. It is possible to suppress the use of the supplied code in
+a redirect router by setting the smtp_error_code option false. In this case,
+any SMTP code is quietly ignored.
+
+
+Exim version 4.61
+-----------------
+
+1. The default number of ACL variables of each type has been increased to 20,
+and it's possible to compile Exim with more. You can safely upgrade to this
+release if you already have messages on the queue with saved ACL variable
+values. However, if you downgrade from this release with messages on the queue,
+any saved ACL values they may have will be lost.
+
+2. The default value for rfc1413_query_timeout has been changed from 30s to 5s.
+
+
+Exim version 4.54
+-----------------
+
+There was a problem with 4.52/TF/02 in that a "name=" option on control=
+submission terminated at the next slash, thereby not allowing for slashes in
+the name. This has been changed so that "name=" takes the rest of the string as
+its data. It must therefore be the last option.
+
+
+Version 4.53
+------------
+
+If you are using the experimental Domain Keys support, you must upgrade to
+at least libdomainkeys 0.67 in order to run this release of Exim.
+
+
+Version 4.51
+------------
+
+1. The format in which GnuTLS parameters are cached (in the file gnutls-params
+in the spool directory) has been changed. The new format can also be generated
+externally, so it is now possible to update the values from outside Exim. This
+has been implemented in an upwards, BUT NOT downwards, compatible manner.
+Upgrading should be seamless: when Exim finds that it cannot understand an
+existing cache file, it generates new parameters and writes them to the cache
+in the new format. If, however, you downgrade from 4.51 to a previous release,
+you MUST delete the gnutls-params file in the spool directory, because the
+older Exim will not recognize the new format.
+
+2. When doing a callout as part of verifying an address, Exim was not paying
+attention to any local part prefix or suffix that was matched by the router
+that accepted the address. It now behaves in the same way as it does for
+delivery: the affixes are removed from the local part unless
+rcpt_include_affixes is set on the transport. If you have a configuration that
+uses prefixes or suffixes on addresses that could be used for callouts, and you
+want the affixes to be retained, you must make sure that rcpt_include_affixes
+is set on the transport.
+
+3. Bounce and delay warning messages no longer contain details of delivery
+errors, except for explicit messages (e.g. generated by :fail:) and SMTP
+responses from remote hosts.
+
+
+Version 4.50
+------------
+
+The exicyclog script has been updated to use three-digit numbers in rotated log
+files if the maximum number to keep is greater than 99. If you are already
+keeping more than 99, there will be an incompatible change when you upgrade.
+You will probably want to rename your old log files to the new form before
+running the new exicyclog.
+
+
+Version 4.42
+------------
+
+RFC 3848 specifies standard names for the "with" phrase in Received: header
+lines when AUTH and/or TLS are in use. This is the "received protocol"
+field. Exim used to use "asmtp" for authenticated SMTP, without any
+indication (in the protocol name) for TLS use. Now it follows the RFC and
+uses "esmtpa" if the connection is authenticated, "esmtps" if it is
+encrypted, and "esmtpsa" if it is both encrypted and authenticated. These names
+appear in log lines as well as in Received: header lines.
+
+
+Version 4.34
+------------
+
+Change 4.31/2 gave problems to data ACLs and local_scan() functions that
+expected to see a Received: header. I have changed to yet another scheme. The
+Received: header is now generated after the body is received, but before the
+ACL or local_scan() is called. After they have run, the timestamp in the
+Received: header is updated.
+
+Thus, change (a) of 4.31/2 has been reversed, but change (b) is still true,
+which is lucky, since I decided it was a bug fix.
+
+
+Version 4.33
+------------
+
+If an expansion in a condition on a "warn" statement fails because a lookup
+defers, the "warn" statement is abandoned, and the next ACL statement is
+processed. Previously this caused the whole ACL to be aborted.
+
+
+Version 4.32
+------------
+
+Change 4.31/2 has been reversed, as it proved contentious. Recipient callout
+verification now uses <> in the MAIL command by default, as it did before. A
+new callout option, "use_sender", has been added to request the other
+behaviour.
+
+
+Version 4.31
+------------
+
+1. If you compile Exim to use GnuTLS, it now requires the use of release 1.0.0
+   or greater. The interface to the obsolete 0.8.x releases is no longer
+   supported. There is one externally visible change: the format for the
+   display of Distinguished Names now uses commas as a separator rather than a
+   slash. This is to comply with RFC 2253.
+
+2. When a message is received, the Received: header line is now generated when
+   reception is complete, instead of at the start of reception. For messages
+   that take a long time to come in, this changes the meaning of the timestamp.
+   There are several side-effects of this change:
+
+    (a) If a message is rejected by a DATA or non-SMTP ACL, or by local_scan(),
+        the logged header lines no longer include the local Received: line,
+        because it has not yet been created. If the message is a non-SMTP one,
+        and the error is processed by sending a message to the sender, the copy
+        of the original message that is returned does not have an added
+        Received: line.
+
+    (b) When a filter file is tested using -bf, no additional Received: header
+        is added to the test message. After some thought, I decided that this
+        is a bug fix.
+
+    The contents of $received_for are not affected by this change. This
+    variable still contains the single recipient of a message, copied after
+    addresses have been rewritten, but before local_scan() is run.
+
+2. Recipient callout verification, like sender verification, was using <> in
+   the MAIL FROM command. This isn't really the right thing, since the actual
+   sender may affect whether the remote host accepts the recipient or not. I
+   have changed it to use the actual sender in the callout; this means that
+   the cache record is now keyed on a recipient/sender pair, not just the
+   recipient address. There doesn't seem to be a real danger of callout loops,
+   since a callout by the remote host to check the sender would use <>.
+
+
+Version 4.30
+------------
+
+1. I have abolished timeout_DNS as an error that can be detected in retry
+   rules, because it has never worked. Despite the fact that it has been
+   documented since at least release 1.62, there was no code to support it.
+   If you have used it in your retry rules, you will now get a warning message
+   to the log and panic log. It is now treated as plain "timeout".
+
+2. After discussion on the mailing list, Exim no longer adds From:, Date:, or
+   Message-Id: header lines to messages that do not originate locally, that is,
+   messages that have an associated sending host address.
+
+3. When looking up a host name from an IP address, Exim now tries the DNS
+   first, and only if that fails does it use gethostbyaddr() (or equivalent).
+   This change was made because on some OS, not all the names are given for
+   addresses with multiple PTR records via the gethostbyaddr() interface. The
+   order of lookup can be changed by setting host_lookup_order.
+
+
+Version 4.23
+------------
+
+1. The new FIXED_NEVER_USERS build-time option creates a list of "never users"
+   that cannot be overridden. The default in the distributed EDITME is "root".
+   If for some reason you were (against advice) running deliveries as root, you
+   will have to ensure that FIXED_NEVER_USERS is not set in your
+   Local/Makefile.
+
+2. The ${quote: operator now quotes an empty string, which it did not before.
+
+3. Version 4.23 saves the contents of the ACL variables with the message, so
+   that they can be used later. If one of these variables contains a newline,
+   there will be a newline character in the spool that will not be interpreted
+   correctely by a previous version of Exim. (Exim ignores keyed spool file
+   items that it doesn't understand - precisely for this kind of problem - but
+   it expects them all to be on one line.)
+
+   So the bottom line is: if you have newlines in your ACL variables, you
+   cannot retreat from 4.23.
+
+
+Version 4.21
+------------
+
+1. The idea of the "warn" ACL verb is that it adds a header or writes to the
+   log only when "message" or "log_message" are set. However, if one of the
+   conditions was an address verification, or a call to a nested ACL, the
+   messages generated by the underlying test were being passed through. This
+   no longer happens. The underlying message is available in $acl_verify_
+   message for both "message" and "log_message" expansions, so it can be
+   passed through if needed.
+
+2. The way that the $h_ (and $header_) expansions work has been changed by the
+   addition of RFC 2047 decoding. See the main documentation (the NewStuff file
+   until release 4.30, then the manual) for full details. Briefly, there are
+   now three forms:
+
+     $rh_xxx: and $rheader_xxx: give the original content of the header
+       line(s), with no processing at all.
+
+     $bh_xxx: and $bheader_xxx: remove leading and trailing white space, and
+       then decode base64 or quoted-printable "words" within the header text,
+       but do not do charset translation.
+
+     $h_xxx: and $header_xxx: attempt to translate the $bh_ string to a
+       standard character set.
+
+   If you have previously been using $h_ expansions to access the raw
+   characters, you should change to $rh_ instead.
+
+3. When Exim creates an RFC 2047 encoded word in a header line, it labels it
+   with the default character set from the headers_charset option instead of
+   always using iso-8859-1.
+
+4. If TMPDIR is defined in Local/Makefile (default in src/EDITME is
+   TMPDIR="/tmp"), Exim checks for the presence of an environment variable
+   called TMPDIR, and if it finds it is different, it changes its value.
+
+5. Following a discussion on the list, the rules by which Exim recognises line
+   endings on incoming messages have been changed. The -dropcr and drop_cr
+   options are now no-ops, retained only for backwards compatibility. The
+   following line terminators are recognized: LF CRLF CR. However, special
+   processing applies to CR:
+
+   (i)  The sequence CR . CR does *not* terminate an incoming SMTP message,
+        nor a local message in the state where . is a terminator.
+
+   (ii) If a bare CR is encountered in a header line, an extra space is added
+        after the line terminator so as not to end the header. The reasoning
+        behind this is that bare CRs in header lines are most likely either
+        to be mistakes, or people trying to play silly games.
+
+6. The code for using daemon_smtp_port, local_interfaces, and the -oX options
+   has been reorganized. It is supposed to be backwards compatible, but it is
+   mentioned here just in case I've screwed up.
+
+
+
+Version 4.20
+------------
+
+1. I have tidied and re-organized the code that uses alarm() for imposing time
+   limits on various things. It shouldn't affect anything, but if you notice
+   processes getting stuck, it may be that I've broken something.
+
+2. The "arguments" log selector now also logs the current working directory
+   when Exim is called.
+
+3. An incompatible change has been made to the appendfile transport. This
+   affects the case when it is used for file deliveries that are set up by
+   .forward and filter files. Previously, any settings of the "file" or
+   "directory" options were ignored. It is hoped that, like the address_file
+   transport in the default configuration, these options were never in fact set
+   on such transports, because they were of no use.
+
+   Now, if either of these options is set, it is used. The path that is passed
+   by the router is in $address_file (this is not new), so it can be used as
+   part of a longer path, or modified in any other way that expansion permits.
+
+   If neither "file" nor "directory" is set, the behaviour is unchanged.
+
+4. Related to the above: in a filter, if a "save" command specifies a non-
+   absolute path, the value of $home/ is pre-pended. This no longer happens if
+   $home is unset or is set to an empty string.
+
+5. Multiple file deliveries from a filter or .forward file can never be
+   batched; the value of batch_max on the transport is ignored for file
+   deliveries. I'm assuming that nobody ever actually set batch_max on the
+   address_file transport - it would have had odd effects previously.
+
+6. DESTDIR is the more common variable that ROOT for use when installing
+   software under a different root filing system. The Exim install script now
+   recognizes DESTDIR first; if it is not set, ROOT is used.
+
+7. If DESTDIR is set when installing Exim, it no longer prepends its value to
+   the path of the system aliases file that appears in the default
+   configuration (when a default configuration is installed). If an aliases
+   file is actually created, its name *does* use the prefix.
+
+
+Version 4.14
+------------
+
+1. The default for the maximum number of unknown SMTP commands that Exim will
+accept before dropping a connection has been reduced from 5 to 3. However, you
+can now change the value by setting smtp_max_unknown_commands.
+
+2. The ${quote: operator has been changed so that it turns newline and carriage
+return characters into \n and \r, respectively.
+
+3. The file names used for maildir messages now include the microsecond time
+fraction as well as the time in seconds, to cope with systems where the process
+id can be re-used within the same second. The format is now
+
+  <time>.H<microsec>P<pid>.<host>
+
+This should be a compatible change, but is noted here just in case.
+
+4. The rules for creating message ids have changed, to cope with systems where
+the process id can be re-used within the same second. The format, however, is
+unchanged, so this should not cause any problems, except as noted in the next
+item.
+
+5. The maximum value for localhost_number has been reduced from 255 to 16, in
+order to implement the new message id rules. For operating systems that have
+case-insensitive file systems (Cygwin and Darwin), the limit is 10.
+
+6. verify = header_syntax was allowing unqualified addresses in all cases. Now
+it allows them only for locally generated messages and from hosts that match
+sender_unqualified_hosts or recipient_unqualified_hosts, respectively.
+
+7. For reasons lost in the mists of time, when a pipe transport was run, the
+environment variable MESSAGE_ID was set to the message ID preceded by 'E' (the
+form used in Message-ID: header lines). The 'E' has been removed.
+
+
+Version 4.11
+------------
+
+1. The handling of lines in the configuration file has changed. Previously,
+macro expansion was applied to logical lines, after continuations had been
+joined on. This meant that it could not be used in .include lines, which are
+handled as physical rather than logical lines. Macro expansion is now done on
+physical lines rather than logical lines. This means there are two
+incompatibilities:
+
+  (a) A macro that expands to # to turn a line into a comment now applies only
+      to the physical line where it appears. Previously, it would have caused
+      any following continuations also to be ignored.
+
+  (b) A macro name can no longer be split over the boundary between a line and
+      its continuation. Actually, this is more of a bug fix. :-)
+
+2. The -D command line option must now all be within one command line item.
+This makes it possible to use -D to set a macro to the empty string by commands
+such as
+
+  exim -DABC  ...
+  exim -DABC= ...
+
+Previously, these items would have moved on to the next item on the command
+line. To include spaces in a macro definition item, quotes must be used, in
+which case you can also have spaces after -D and surrounding the equals. For
+example:
+
+  exim '-D ABC = something' ...
+
+3. The way that addresses that redirect to themselves are handled has been
+changed, in order to fix an obscure bug. This should not cause any problems
+except in the case of wanting to go back from a 4.11 (or later) release to an
+earlier release. If there are undelivered messages on the spool that contain
+addresses which redirect to themselves, and the redirected addresses have
+already been delivered, you might get a duplicate delivery if you revert to an
+earlier Exim.
+
+4. The default way of looking up IP addresses for hosts in the manualroute and
+queryprogram routers has been changed. If "byname" or "bydns" is explicitly
+specified, there is no change, but if no method is specified, Exim now behaves
+as follows:
+
+  First, a DNS lookup is done. If this yields anything other than
+  HOST_NOT_FOUND, that result is used. Otherwise, Exim goes on to try a call to
+  getipnodebyname() (or gethostbyname() on older systems) and the result of the
+  lookup is the result of that call.
+
+This change has been made because it has been discovered that on some systems,
+if a DNS lookup called via getipnodebyname() times out, HOST_NOT_FOUND is
+returned instead of TRY_AGAIN. Thus, it is safest to try a DNS lookup directly
+first, and only if that gives a definite "no such host" to try the local
+function.
+
+5. In fixing the minor security problem with pid_file_path, I have removed some
+backwards-compatible (undocumented) code which was present to ease conversion
+from Exim 3. In Exim 4, pid_file_path is a literal; in Exim 3 it was allowed to
+contain "%s", which was replaced by the port number for daemons listening on
+non-standard ports. In Exim 4, such daemons do not write a pid file. The
+backwards compatibility feature was to replace "%s" by nothing if it occurred
+in an Exim 4 setting of pid_file_path. The bug was in this code. I have solved
+the problem by removing the backwards compatibility feature. Thus, if you still
+have "%s" somewhere in a setting of pid_file_path, you should remove it.
+
+6. There has been an extension to lsearch files. The keys in these files may
+now be quoted in order to allow for whitespace and colons in them. This means
+that if you were previously using keys that began with a doublequote, you will
+now have to wrap them with extra quotes and escape the internal quotes. The
+possibility that anybody is actually doing this seems extremely remote, but it
+is documented just in case.
+
+
+Version 4.10
+------------
+
+The build-time parameter EXIWHAT_KILL_ARG has been renamed EXIWHAT_KILL_SIGNAL
+to better reflect its function. The OS-specific files have been updated. Only
+if you have explicitly set this in your Makefile (highly unlikely) do you need
+to change anything.
+
+****
diff --git a/doc/ChangeLog b/doc/ChangeLog
new file mode 100644 (file)
index 0000000..bea6b1a
--- /dev/null
@@ -0,0 +1,6092 @@
+Change log file for Exim from version 4.21
+-------------------------------------------
+
+
+Exim version 4.84
+-----------------
+TL/01 Bugzilla 1506: Re-add a 'return NULL' to silence complaints from static
+      checkers that were complaining about end of non-void function with no
+      return.
+
+JH/01 Bug 1513: Fix parsing of quoted parameter values in MIME headers.
+      This was a regression intruduced in 4.83 by another bugfix.
+
+JH/02 Fix broken compilation when EXPERIMENTAL_DSN is enabled.
+
+TL/02 Bug 1509: Fix exipick for enhanced spoolfile specification used when
+      EXPERIMENTAL_DNS is enabled.  Fix from Wolfgang Breyha.
+
+
+Exim version 4.83
+-----------------
+
+TF/01 Correctly close the server side of TLS when forking for delivery.
+
+      When a message was received over SMTP with TLS, Exim failed to clear up
+      the incoming connection properly after forking off the child process to
+      deliver the message. In some situations the subsequent outgoing
+      delivery connection happened to have the same fd number as the incoming
+      connection previously had. Exim would try to use TLS and fail, logging
+      a "Bad file descriptor" error.
+
+TF/02 Portability fix for building lookup modules on Solaris when the xpg4
+      utilities have not been installed.
+
+JH/01 Fix memory-handling in use of acl as a conditional; avoid free of
+      temporary space as the ACL may create new global variables.
+
+TL/01 LDAP support uses per connection or global context settings, depending
+      upon the detected version of the libraries at build time.
+
+TL/02 Experimental Proxy Protocol support: allows a proxied SMTP connection
+      to extract and use the src ip:port in logging and expansions as if it
+      were a direct connection from the outside internet. PPv2 support was
+      updated based on HAProxy spec change in May 2014.
+
+JH/02 Add ${listextract {number}{list}{success}{fail}}.
+
+TL/03 Bugzilla 1433: Fix DMARC SEGV with specific From header contents.
+      Properly escape header and check for NULL return.
+
+PP/01 Continue incomplete 4.82 PP/19 by fixing docs too: use dns_dnssec_ok
+      not dns_use_dnssec.
+
+JH/03 Bugzilla 1157: support log_selector smtp_confirmation for lmtp.
+
+TL/04 Add verify = header_names_ascii check to reject email with non-ASCII
+      characters in header names, implemented as a verify condition.
+      Contributed by Michael Fischer v. Mollard.
+
+TL/05 Rename SPF condition results err_perm and err_temp to standardized
+      results permerror and temperror.  Previous values are deprecated but
+      still accepted.  In a future release, err_perm and err_temp will be
+      completely removed, which will be a backward incompatibility if the
+      ACL tests for either of these two old results. Patch contributed by
+      user bes-internal on the mailing list.
+
+JH/04 Add ${utf8clean:} operator. Contributed by Alex Rau.
+
+JH/05 Bugzilla 305: Log incoming-TLS details on rejects, subject to log
+      selectors, in both main and reject logs.
+
+JH/06 Log outbound-TLS and port details, subject to log selectors, for a
+      failed delivery.
+
+JH/07 Add malware type "sock" for talking to simple daemon.
+
+JH/08 Bugzilla 1371: Add tls_{,try_}verify_hosts to smtp transport.
+
+JH/09 Bugzilla 1431: Support (with limitations) headers_add/headers_remove in
+      routers/transports under cutthrough routing.
+
+JH/10 Bugzilla 1005: ACL "condition =" should accept values which are negative
+      numbers.  Touch up "bool" conditional to keep the same definition.
+
+TL/06 Remove duplicated language in spec file from 4.82 TL/16.
+
+JH/11 Add dnsdb tlsa lookup.  From Todd Lyons.
+
+JH/12 Expand items in router/transport headers_add or headers_remove lists
+      individually rather than the list as a whole.  Bug 1452.
+
+      Required for reasonable handling of multiple headers_ options when
+      they may be empty; requires that headers_remove items with embedded
+      colons must have them doubled (or the list-separator changed).
+
+TL/07 Add new dmarc expansion variable $dmarc_domain_policy to directly
+      view the policy declared in the DMARC record. Currently, $dmarc_status
+      is a combined value of both the record presence and the result of the
+      analysis.
+
+JH/13 Fix handling of $tls_cipher et.al. in (non-verify) transport.  Bug 1455.
+
+JH/14 New options dnssec_request_domains, dnssec_require_domains on the
+      dnslookup router and the smtp transport (applying to the forward
+      lookup).
+
+TL/08 Bugzilla 1453: New LDAP "SERVERS=" option allows admin to override list
+      of ldap servers used for a specific lookup.  Patch provided by Heiko
+      Schlichting.
+
+JH/18 New options dnssec_lax, dnssec_strict on dnsdb lookups.
+      New variable $lookup_dnssec_authenticated for observability.
+
+TL/09 Bugzilla 609: Add -C option to exiqgrep, specify which exim.conf to use.
+      Patch submitted by Lars Timman.
+
+JH/19 EXPERIMENTAL_OCSP support under GnuTLS.  Bug 1459.
+
+TL/10 Bugzilla 1454: New -oMm option to pass message reference to Exim.
+      Requires trusted mode and valid format message id, aborts otherwise.
+      Patch contributed by Heiko Schlichting.
+
+JH/20 New expansion variables tls_(in,out)_(our,peer)cert, and expansion item
+      certextract with support for various fields.  Bug 1358.
+
+JH/21 Observability of OCSP via variables tls_(in,out)_ocsp.  Stapling
+      is requested by default, modifiable by smtp transport option
+      hosts_request_ocsp.
+
+JH/22 Expansion operators ${md5:string} and ${sha1:string} can now
+      operate on certificate variables to give certificate fingerprints
+      Also new ${sha256:cert_variable}.
+
+JH/23 The PRDR feature is moved from being Experimental into the mainline.
+
+TL/11 Bug 1119: fix memory allocation in string_printing2().  Patch from
+      Christian Aistleitner.
+
+JH/24 The OCSP stapling feature is moved from Experimental into the mainline.
+
+TL/12 Bug 1444: Fix improper \r\n sequence handling when writing spool
+      file.  Patch from Wolfgang Breyha.
+
+JH/25 Expand the coverage of the delivery $host and $host_address to
+      client authenticators run in verify callout.  Bug 1476.
+
+JH/26 Port service names are now accepted for tls_on_connect_ports, to
+      align with daemon_smtp_ports.  Bug 72.
+
+TF/03 Fix udpsend. The ip_connectedsocket() function's socket type
+      support and error reporting did not work properly.
+
+TL/13 Bug 1495: Exiqgrep check if -C config file specified on cli exists
+      and is readable.  Patch from Andrew Colin Kissa.
+
+TL/14 Enhance documentation of ${run expansion and how it parses the
+      commandline after expansion, particularly in the case when an
+      unquoted variable expansion results in an empty value.
+
+JH/27 The TLS SNI feature was broken in 4.82.  Fix it.
+
+PP/02 Fix internal collision of T_APL on systems which support RFC3123
+      by renaming away from it.  Addresses GH issue 15, reported by
+      Jasper Wallace.
+
+JH/28 Fix parsing of MIME headers for parameters with quoted semicolons.
+
+TL/15 SECURITY: prevent double expansion in math comparison functions
+      (can expand unsanitized data). Not remotely exploitable.
+      CVE-2014-2972
+
+
+Exim version 4.82
+-----------------
+
+PP/01 Add -bI: framework, and -bI:sieve for querying sieve capabilities.
+
+PP/02 Make -n do something, by making it not do something.
+      When combined with -bP, the name of an option is not output.
+
+PP/03 Added tls_dh_min_bits SMTP transport driver option, only honoured
+      by GnuTLS.
+
+PP/04 First step towards DNSSEC, provide $sender_host_dnssec for
+      $sender_host_name and config options to manage this, and basic check
+      routines.
+
+PP/05 DSCP support for outbound connections and control modifier for inbound.
+
+PP/06 Cyrus SASL: set local and remote IP;port properties for driver.
+      (Only plugin which currently uses this is kerberos4, which nobody should
+      be using, but we should make it available and other future plugins might
+      conceivably use it, even though it would break NAT; stuff *should* be
+      using channel bindings instead).
+
+PP/07 Handle "exim -L <tag>" to indicate to use syslog with tag as the process
+      name; added for Sendmail compatibility; requires admin caller.
+      Handle -G as equivalent to "control = suppress_local_fixups" (we used to
+      just ignore it); requires trusted caller.
+      Also parse but ignore: -Ac -Am -X<logfile>
+      Bugzilla 1117.
+
+TL/01 Bugzilla 1258 - Refactor MAIL FROM optional args processing.
+
+TL/02 Add +smtp_confirmation as a default logging option.
+
+TL/03 Bugzilla 198 - Implement remove_header ACL modifier.
+      Patch by Magnus Holmgren from 2007-02-20.
+
+TL/04 Bugzilla 1281 - Spec typo.
+      Bugzilla 1283 - Spec typo.
+      Bugzilla 1290 - Spec grammar fixes.
+
+TL/05 Bugzilla 1285 - Spec omission, fix docbook errors for spec.txt creation.
+
+TL/06 Add Experimental DMARC support using libopendmarc libraries.
+
+TL/07 Fix an out of order global option causing a segfault.  Reported to dev
+      mailing list by by Dmitry Isaikin.
+
+JH/01 Bugzilla 1201 & 304 - New cutthrough-delivery feature, with TLS support.
+
+JH/02 Support "G" suffix to numbers in ${if comparisons.
+
+PP/08 Handle smtp transport tls_sni option forced-fail for OpenSSL.
+
+NM/01 Bugzilla 1197 - Spec typo
+      Bugzilla 1196 - Spec examples corrections
+
+JH/03 Add expansion operators ${listnamed:name} and ${listcount:string}
+
+PP/09 Add gnutls_allow_auto_pkcs11 option (was originally called
+      gnutls_enable_pkcs11, but renamed to more accurately indicate its
+      function.
+
+PP/10 Let Linux makefile inherit CFLAGS/CFLAGS_DYNAMIC.
+      Pulled from Debian 30_dontoverridecflags.dpatch by Andreas Metzler.
+
+JH/04 Add expansion item ${acl {name}{arg}...}, expansion condition
+      "acl {{name}{arg}...}", and optional args on acl condition
+      "acl = name arg..."
+
+JH/05 Permit multiple router/transport headers_add/remove lines.
+
+JH/06 Add dnsdb pseudo-lookup "a+" to do an "aaaa" + "a" combination.
+
+JH/07 Avoid using a waiting database for a single-message-only transport.
+      Performance patch from Paul Fisher.  Bugzilla 1262.
+
+JH/08 Strip leading/trailing newlines from add_header ACL modifier data.
+      Bugzilla 884.
+
+JH/09 Add $headers_added variable, with content from use of ACL modifier
+      add_header (but not yet added to the message).  Bugzilla 199.
+
+JH/10 Add 8bitmime log_selector, for 8bitmime status on the received line.
+      Pulled from Bugzilla 817 by Wolfgang Breyha.
+
+PP/11 SECURITY: protect DKIM DNS decoding from remote exploit.
+      CVE-2012-5671
+      (nb: this is the same fix as in Exim 4.80.1)
+
+JH/11 Add A= logging on delivery lines, and a client_set_id option on
+      authenticators.
+
+JH/12 Add optional authenticated_sender logging to A= and a log_selector
+      for control.
+
+PP/12 Unbreak server_set_id for NTLM/SPA auth, broken by 4.80 PP/29.
+
+PP/13 Dovecot auth: log better reason to rejectlog if Dovecot did not
+      advertise SMTP AUTH mechanism to us, instead of a generic
+      protocol violation error.  Also, make Exim more robust to bad
+      data from the Dovecot auth socket.
+
+TF/01 Fix ultimate retry timeouts for intermittently deliverable recipients.
+
+      When a queue runner is handling a message, Exim first routes the
+      recipient addresses, during which it prunes them based on the retry
+      hints database. After that it attempts to deliver the message to
+      any remaining recipients. It then updates the hints database using
+      the retry rules.
+
+      So if a recipient address works intermittently, it can get repeatedly
+      deferred at routing time. The retry hints record remains fresh so the
+      address never reaches the final cutoff time.
+
+      This is a fairly common occurrence when a user is bumping up against
+      their storage quota. Exim had some logic in its local delivery code
+      to deal with this. However it did not apply to per-recipient defers
+      in remote deliveries, e.g. over LMTP to a separate IMAP message store.
+
+      This change adds a proper retry rule check during routing so that the
+      final cutoff time is checked against the message's age. We only do
+      this check if there is an address retry record and there is not a
+      domain retry record; this implies that previous attempts to handle
+      the address had the retry_use_local_parts option turned on. We use
+      this as an approximation for the destination being like a local
+      delivery, as in LMTP.
+
+      I suspect this new check makes the old local delivery cutoff check
+      redundant, but I have not verified this so I left the code in place.
+
+TF/02 Correct gecos expansion when From: is a prefix of the username.
+
+      Test 0254 submits a message to Exim with the header
+
+        Resent-From: f
+
+      When I ran the test suite under the user fanf2, Exim expanded
+      the header to contain my full name, whereas it should have added
+      a Resent-Sender: header. It erroneously treats any prefix of the
+      username as equal to the username.
+
+      This change corrects that bug.
+
+GF/01 DCC debug and logging tidyup
+      Error conditions log to paniclog rather than rejectlog.
+      Debug lines prefixed by "DCC: " to remove any ambiguity.
+
+TF/03 Avoid unnecessary rebuilds of lookup-related code.
+
+PP/14 Fix OCSP reinitialisation in SNI handling for Exim/TLS as server.
+      Bug spotted by Jeremy Harris; was flawed since initial commit.
+      Would have resulted in OCSP responses post-SNI triggering an Exim
+      NULL dereference and crash.
+
+JH/13 Add $router_name and $transport_name variables.  Bugzilla 308.
+
+PP/15 Define SIOCGIFCONF_GIVES_ADDR for GNU Hurd.
+      Bug detection, analysis and fix by Samuel Thibault.
+      Bugzilla 1331, Debian bug #698092.
+
+SC/01 Update eximstats to watch out for senders sending 'HELO [IpAddr]'
+
+JH/14 SMTP PRDR (http://www.eric-a-hall.com/specs/draft-hall-prdr-00.txt).
+      Server implementation by Todd Lyons, client by JH.
+      Only enabled when compiled with EXPERIMENTAL_PRDR.  A new
+      config variable "prdr_enable" controls whether the server
+      advertises the facility.  If the client requests PRDR a new
+      acl_data_smtp_prdr ACL is called once for each recipient, after
+      the body content is received and before the acl_smtp_data ACL.
+      The client is controlled by bolth of: a hosts_try_prdr option
+      on the smtp transport, and the server advertisement.
+      Default client logging of deliveries and rejections involving
+      PRDR are flagged with the string "PRDR".
+
+PP/16 Fix problems caused by timeouts during quit ACLs trying to double
+      fclose().  Diagnosis by Todd Lyons.
+
+PP/17 Update configure.default to handle IPv6 localhost better.
+      Patch by Alain Williams (plus minor tweaks).
+      Bugzilla 880.
+
+PP/18 OpenSSL made graceful with empty tls_verify_certificates setting.
+      This is now consistent with GnuTLS, and is now documented: the
+      previous undocumented portable approach to treating the option as
+      unset was to force an expansion failure.  That still works, and
+      an empty string is now equivalent.
+
+PP/19 Renamed DNSSEC-enabling option to "dns_dnssec_ok", to make it
+      clearer that Exim is using the DO (DNSSEC OK) EDNS0 resolver flag,
+      not performing validation itself.
+
+PP/20 Added force_command boolean option to pipe transport.
+      Patch from Nick Koston, of cPanel Inc.
+
+JH/15 AUTH support on callouts (and hence cutthrough-deliveries).
+      Bugzilla 321, 823.
+
+TF/04 Added udpsend ACL modifer and hexquote expansion operator
+
+PP/21 Fix eximon continuous updating with timestamped log-files.
+      Broken in a format-string cleanup in 4.80, missed when I repaired the
+      other false fix of the same issue.
+      Report and fix from Heiko Schlichting.
+      Bugzilla 1363.
+
+PP/22 Guard LDAP TLS usage against Solaris LDAP variant.
+      Report from Prashanth Katuri.
+
+PP/23 Support safari_ecdhe_ecdsa_bug for openssl_options.
+      It's SecureTransport, so affects any MacOS clients which use the
+      system-integrated TLS libraries, including email clients.
+
+PP/24 Fix segfault from trying to fprintf() to a NULL stdio FILE* if
+      using a MIME ACL for non-SMTP local injection.
+      Report and assistance in diagnosis by Warren Baker.
+
+TL/08 Adjust exiqgrep to be case-insensitive for sender/receiver.
+
+JH/16 Fix comparisons for 64b.  Bugzilla 1385.
+
+TL/09 Add expansion variable $authenticated_fail_id to keep track of
+      last id that failed so it may be referenced in subsequent ACL's.
+
+TL/10 Bugzilla 1375 - Prevent TLS rebinding in ldap. Patch provided by
+      Alexander Miroch.
+
+TL/11 Bugzilla 1382 - Option ldap_require_cert overrides start_tls
+      ldap library initialization, allowing self-signed CA's to be
+      used. Also properly sets require_cert option later in code by
+      using NULL (global ldap config) instead of ldap handle (per
+      session). Bug diagnosis and testing by alxgomz.
+
+TL/12 Enhanced documentation in the ratelimit.pl script provided in
+      the src/util/ subdirectory.
+
+TL/13 Bug 1031 - Imported transport SQL logging patch from Axel Rau
+      renamed to Transport Post Delivery Action by Jeremy Harris, as
+      EXPERIMENTAL_TPDA.
+
+TL/14 Bugzilla 1217 - Redis lookup support has been added. It is only enabled
+      when Exim is compiled with EXPERIMENTAL_REDIS. A new config variable
+      redis_servers = needs to be configured which will be used by the redis
+      lookup.  Patch from Warren Baker, of The Packet Hub.
+
+TL/15 Fix exiqsumm summary for corner case. Patch provided by Richard Hall.
+
+TL/16 Bugzilla 1289 - Clarify host/ip processing when have errors looking up a
+      hostname or reverse DNS when processing a host list. Used suggestions
+      from multiple comments on this bug.
+
+TL/17 Bugzilla 1057 - Multiple clamd TCP targets patch from Mark Zealey.
+
+TL/18 Had previously added a -CONTINUE option to runtest in the test suite.
+      Missed a few lines, added it to make the runtest require no keyboard
+      interaction.
+
+TL/19 Bugzilla 1402 - Test 533 fails if any part of the path to the test suite
+      contains upper case chars. Make router use caseful_local_part.
+
+TL/20 Bugzilla 1400 - Add AVOID_GNUTLS_PKCS11 build option. Allows GnuTLS
+      support when GnuTLS has been built with p11-kit.
+
+
+Exim version 4.80.1
+-------------------
+
+PP/01 SECURITY: protect DKIM DNS decoding from remote exploit.
+      CVE-2012-5671
+      This, or similar/improved, will also be change PP/11 of 4.82.
+
+
+Exim version 4.80
+-----------------
+
+PP/01 Handle short writes when writing local log-files.
+      In practice, only affects FreeBSD (8 onwards).
+      Bugzilla 1053, with thanks to Dmitry Isaikin.
+
+NM/01 Bugzilla 949 - Documentation tweak
+
+NM/02 Bugzilla 1093 - eximstats DATA reject detection regexps
+      improved.
+
+NM/03 Bugzilla 1169 - primary_hostname spelling was incorrect in docs.
+
+PP/02 Implemented gsasl authenticator.
+
+PP/03 Implemented heimdal_gssapi authenticator with "server_keytab" option.
+
+PP/04 Local/Makefile support for (AUTH|LOOKUP)_*_PC=foo to use
+      `pkg-config foo` for cflags/libs.
+
+PP/05 Swapped $auth1/$auth2 for gsasl GSSAPI mechanism, to be more consistent
+      with rest of GSASL and with heimdal_gssapi.
+
+PP/06 Local/Makefile support for USE_(GNUTLS|OPENSSL)_PC=foo to use
+      `pkg-config foo` for cflags/libs for the TLS implementation.
+
+PP/07 New expansion variable $tls_bits; Cyrus SASL server connection
+      properties get this fed in as external SSF.  A number of robustness
+      and debugging improvements to the cyrus_sasl authenticator.
+
+PP/08 cyrus_sasl server now expands the server_realm option.
+
+PP/09 Bugzilla 1214 - Log authentication information in reject log.
+      Patch by Jeremy Harris.
+
+PP/10 Added dbmjz lookup type.
+
+PP/11 Let heimdal_gssapi authenticator take a SASL message without an authzid.
+
+PP/12 MAIL args handles TAB as well as SP, for better interop with
+      non-compliant senders.
+      Analysis and variant patch by Todd Lyons.
+
+NM/04 Bugzilla 1237 - fix cases where printf format usage not indicated
+      Bug report from Lars Müller <lars@samba.org> (via SUSE), 
+      Patch from Dirk Mueller <dmueller@suse.com>
+
+PP/13 tls_peerdn now print-escaped for spool files.
+      Observed some $tls_peerdn in wild which contained \n, which resulted
+      in spool file corruption.
+
+PP/14 TLS fixes for OpenSSL: support TLS 1.1 & 1.2; new "openssl_options"
+      values; set SSL_MODE_AUTO_RETRY so that OpenSSL will retry a read
+      or write after TLS renegotiation, which otherwise led to messages
+      "Got SSL error 2".
+
+TK/01 Bugzilla 1239 - fix DKIM verification when signature was not inserted
+      as a tracking header (ie: a signed header comes before the signature).
+      Patch from Wolfgang Breyha.
+
+JH/01 Bugzilla 660 - Multi-valued attributes from ldap now parseable as a
+      comma-sep list; embedded commas doubled.
+
+JH/02 Refactored ACL "verify =" logic to table-driven dispatch.
+
+PP/15 LDAP: Check for errors of TLS initialisation, to give correct
+      diagnostics.
+      Report and patch from Dmitry Banschikov.
+
+PP/16 Removed "dont_insert_empty_fragments" fron "openssl_options".
+      Removed SSL_clear() after SSL_new() which led to protocol negotiation
+      failures.  We appear to now support TLS1.1+ with Exim.
+
+PP/17 OpenSSL: new expansion var $tls_sni, which if used in tls_certificate
+      lets Exim select keys and certificates based upon TLS SNI from client.
+      Also option tls_sni on SMTP Transports.  Also clear $tls_bits correctly
+      before an outbound SMTP session.  New log_selector, +tls_sni.
+
+PP/18 Bugzilla 1122 - check localhost_number expansion for failure, avoid
+      NULL dereference.  Report and patch from Alun Jones.
+
+PP/19 DNS resolver init changes for NetBSD compatibility.  (Risk of breakage
+      on less well tested platforms).  Obviates NetBSD pkgsrc patch-ac.
+      Not seeing resolver debug output on NetBSD, but suspect this is a
+      resolver implementation change.
+
+PP/20 Revert part of NM/04, it broke log_path containing %D expansions.
+      Left warnings.  Added "eximon gdb" invocation mode.
+
+PP/21 Defaulting "accept_8bitmime" to true, not false.
+
+PP/22 Added -bw for inetd wait mode support.
+
+PP/23 Added PCRE_CONFIG=yes support to Makefile for using pcre-config to
+      locate the relevant includes and libraries.  Made this the default.
+
+PP/24 Fixed headers_only on smtp transports (was not sending trailing dot).
+      Bugzilla 1246, report and most of solution from Tomasz Kusy.
+
+JH/03 ${eval } now uses 64-bit and supports a "g" suffix (like to "k" and "m").
+      This may cause build issues on older platforms.
+
+PP/25 Revamped GnuTLS support, passing tls_require_ciphers to
+      gnutls_priority_init, ignoring Exim options gnutls_require_kx,
+      gnutls_require_mac & gnutls_require_protocols (no longer supported).
+      Added SNI support via GnuTLS too.
+      Made ${randint:..} supplier available, if using not-too-old GnuTLS.
+
+PP/26 Added EXPERIMENTAL_OCSP for OpenSSL.
+
+PP/27 Applied dnsdb SPF support patch from Janne Snabb.
+      Applied second patch from Janne, implementing suggestion to default
+      multiple-strings-in-record handling to match SPF spec.
+
+JH/04 Added expansion variable $tod_epoch_l for a higher-precision time.
+
+PP/28 Fix DCC dcc_header content corruption (stack memory referenced,
+      read-only, out of scope).
+      Patch from Wolfgang Breyha, report from Stuart Northfield.
+
+PP/29 Fix three issues highlighted by clang analyser static analysis.
+      Only crash-plausible issue would require the Cambridge-specific
+      iplookup router and a misconfiguration.
+      Report from Marcin MirosÅ‚aw.
+
+PP/30 Another attempt to deal with PCRE_PRERELEASE, this one less buggy.
+
+PP/31 %D in printf continues to cause issues (-Wformat=security), so for
+      now guard some of the printf checks behind WANT_DEEPER_PRINTF_CHECKS.
+      As part of this, removing so much warning spew let me fix some minor
+      real issues in debug logging.
+
+PP/32 GnuTLS was always using default tls_require_ciphers, due to a missing
+      assignment on my part.  Fixed.
+
+PP/33 Added tls_dh_max_bits option, defaulting to current hard-coded limit
+      of NSS, for GnuTLS/NSS interop.  Problem root cause diagnosis by
+      Janne Snabb (who went above and beyond: thank you).
+
+PP/34 Validate tls_require_ciphers on startup, since debugging an invalid
+      string otherwise requires a connection and a bunch more work and it's
+      relatively easy to get wrong.  Should also expose TLS library linkage
+      problems.
+
+PP/35 Pull in <features.h> on Linux, for some portability edge-cases of
+      64-bit ${eval} (JH/03).
+
+PP/36 Define _GNU_SOURCE in exim.h; it's needed for some releases of
+      GNU libc to support some of the 64-bit stuff, should not lead to
+      conflicts.  Defined before os.h is pulled in, so if a given platform
+      needs to override this, it can.
+
+PP/37 Unbreak Cyrus SASL auth: SSF retrieval was incorrect, Exim thought
+      protection layer was required, which is not implemented.
+      Bugzilla 1254, patch from Wolfgang Breyha.
+
+PP/38 Overhaul DH prime handling, supply RFC-specified DH primes as built
+      into Exim, default to IKE id 23 from RFC 5114 (2048 bit).  Make
+      tls_dhparam take prime identifiers.  Also unbreak combination of
+      OpenSSL+DH_params+TLSSNI.
+
+PP/39 Disable SSLv2 by default in OpenSSL support.
+
+
+Exim version 4.77
+-----------------
+
+PP/01 Solaris build fix for Oracle's LDAP libraries.
+      Bugzilla 1109, patch from Stephen Usher.
+
+TF/01 HP/UX build fix: avoid arithmetic on a void pointer.
+
+TK/01 DKIM Verification: Fix relaxed canon for empty headers w/o
+      whitespace trailer
+
+TF/02 Fix a couple more cases where we did not log the error message
+      when unlink() failed. See also change 4.74-TF/03.
+
+TF/03 Make the exiwhat support code safe for signals. Previously Exim might
+      lock up or crash if it happened to be inside a call to libc when it
+      got a SIGUSR1 from exiwhat.
+
+      The SIGUSR1 handler appends the current process status to the process
+      log which is later printed by exiwhat. It used to use the general
+      purpose logging code to do this, but several functions it calls are
+      not safe for signals.
+
+      The new output code in the SIGUSR1 handler is specific to the process
+      log, and simple enough that it's easy to inspect for signal safety.
+      Removing some special cases also simplifies the general logging code.
+      Removing the spurious timestamps from the process log simplifies
+      exiwhat.
+
+TF/04 Improved ratelimit ACL condition.
+
+      The /noupdate option has been deprecated in favour of /readonly which
+      has clearer semantics. The /leaky, /strict, and /readonly update modes
+      are mutually exclusive. The update mode is no longer included in the
+      database key; it just determines when the database is updated. (This
+      means that when you upgrde Exim will forget old rate measurements.)
+
+      Exim now checks that the per_* options are used with an update mode that
+      makes sense for the current ACL. For example, when Exim is processing a
+      message (e.g. acl_smtp_rcpt or acl_smtp_data, etc.) you can specify
+      per_mail/leaky or per_mail/strict; otherwise (e.g. in acl_smtp_helo) you
+      must specify per_mail/readonly. If you omit the update mode it defaults to
+      /leaky where that makes sense (as before) or /readonly where required.
+
+      The /noupdate option is now undocumented but still supported for
+      backwards compatibility. It is equivalent to /readonly except that in
+      ACLs where /readonly is required you may specify /leaky/noupdate or
+      /strict/noupdate which are treated the same as /readonly.
+
+      A useful new feature is the /count= option. This is a generalization
+      of the per_byte option, so that you can measure the throughput of other
+      aggregate values. For example, the per_byte option is now equivalent
+      to per_mail/count=${if >{0}{$message_size} {0} {$message_size} }.
+
+      The per_rcpt option has been generalized using the /count= mechanism
+      (though it's more complicated than the per_byte equivalence). When it is
+      used in acl_smtp_rcpt, the per_rcpt option adds recipients to the
+      measured rate one at a time; if it is used later (e.g. in acl_smtp_data)
+      or in a non-SMTP ACL it adds all the recipients in one go. (The latter
+      /count=$recipients_count behaviour used to work only in non-SMTP ACLs.)
+      Note that using per_rcpt with a non-readonly update mode in more than
+      one ACL will cause the recipients to be double-counted. (The per_mail
+      and per_byte options don't have this problem.)
+
+      The handling of very low rates has changed slightly. If the computed rate
+      is less than the event's count (usually one) then this event is the first
+      after a long gap. In this case the rate is set to the same as this event's
+      count, so that the first message of a spam run is counted properly.
+
+      The major new feature is a mechanism for counting the rate of unique
+      events. The new per_addr option counts the number of different
+      recipients that someone has sent messages to in the last time period. It
+      behaves like per_rcpt if all the recipient addresses are different, but
+      duplicate recipient addresses do not increase the measured rate. Like
+      the /count= option this is a general mechanism, so the per_addr option
+      is equivalent to per_rcpt/unique=$local_part@$domain. You can, for
+      example, measure the rate that a client uses different sender addresses
+      with the options per_mail/unique=$sender_address. There are further
+      details in the main documentation.
+
+TF/05 Removed obsolete $Cambridge$ CVS revision strings.
+
+TF/06 Removed a few PCRE remnants.
+
+TF/07 Automatically extract Exim's version number from tags in the git
+      repository when doing development or release builds.
+
+PP/02 Raise smtp_cmd_buffer_size to 16kB.
+      Bugzilla 879.  Patch from Paul Fisher.
+
+PP/03 Implement SSL-on-connect outbound with protocol=smtps on smtp transport.
+      Heavily based on revision 40f9a89a from Simon Arlott's tree.
+      Bugzilla 97.
+
+PP/04 Use .dylib instead of .so for dynamic library loading on MacOS.
+
+PP/05 Variable $av_failed, true if the AV scanner deferred.
+      Bugzilla 1078.  Patch from John Horne.
+
+PP/06 Stop make process more reliably on build failure.
+      Bugzilla 1087.  Patch from Heiko Schlittermann.
+
+PP/07 Make maildir_use_size_file an _expandable_ boolean.
+      Bugzilla 1089.  Patch from Heiko Schlittermann.
+
+PP/08 Handle ${run} returning more data than OS pipe buffer size.
+      Bugzilla 1131.  Patch from Holger Weiß.
+
+PP/09 Handle IPv6 addresses with SPF.
+      Bugzilla 860.  Patch from Wolfgang Breyha.
+
+PP/10 GnuTLS: support TLS 1.2 & 1.1.
+      Bugzilla 1156.
+      Use gnutls_certificate_verify_peers2() [patch from Andreas Metzler].
+      Bugzilla 1095.
+
+PP/11 match_* no longer expand right-hand-side by default.
+      New compile-time build option, EXPAND_LISTMATCH_RHS.
+      New expansion conditions, "inlist", "inlisti".
+
+PP/12 fix uninitialised greeting string from PP/03 (smtps client support).
+
+PP/13 shell and compiler warnings fixes for RC1-RC4 changes.
+
+PP/14 fix log_write() format string regression from TF/03.
+      Bugzilla 1152.  Patch from Dmitry Isaikin.
+
+
+Exim version 4.76
+-----------------
+
+PP/01 The new ldap_require_cert option would segfault if used.  Fixed.
+
+PP/02 Harmonised TLS library version reporting; only show if debugging.
+      Layout now matches that introduced for other libraries in 4.74 PP/03.
+
+PP/03 New openssl_options items: no_sslv2 no_sslv3 no_ticket no_tlsv1
+
+PP/04 New "dns_use_edns0" global option.
+
+PP/05 Don't segfault on misconfiguration of ref:name exim-user as uid.
+      Bugzilla 1098.
+
+PP/06 Extra paranoia around buffer usage at the STARTTLS transition.
+      nb: Exim is not vulnerable to http://www.kb.cert.org/vuls/id/555316
+
+TK/01 Updated PolarSSL code to 0.14.2.
+      Bugzilla 1097. Patch from Andreas Metzler.
+
+PP/07 Catch divide-by-zero in ${eval:...}.
+      Fixes bugzilla 1102.
+
+PP/08 Condition negation of bool{}/bool_lax{} did not negate.  Fixed.
+      Bugzilla 1104.
+
+TK/02 Bugzilla 1106: CVE-2011-1764 - DKIM log line was subject to a
+      format-string attack -- SECURITY: remote arbitrary code execution.
+
+TK/03 SECURITY - DKIM signature header parsing was double-expanded, second
+      time unintentionally subject to list matching rules, letting the header
+      cause arbitrary Exim lookups (of items which can occur in lists, *not*
+      arbitrary string expansion). This allowed for information disclosure.
+
+PP/09 Fix another SIGFPE (x86) in ${eval:...} expansion, this time related to
+      INT_MIN/-1 -- value coerced to INT_MAX.
+
+
+Exim version 4.75
+-----------------
+
+NM/01 Workround for PCRE version dependency in version reporting
+      Bugzilla 1073
+
+TF/01 Update valgrind.h and memcheck.h to copies from valgrind-3.6.0.
+      This fixes portability to compilers other than gcc, notably
+      Solaris CC and HP-UX CC. Fixes Bugzilla 1050.
+
+TF/02 Bugzilla 139: Avoid using the += operator in the modular lookup
+      makefiles for portability to HP-UX and POSIX correctness.
+
+PP/01 Permit LOOKUP_foo enabling on the make command-line.
+      Also via indented variable definition in the Makefile.
+      (Debugging by Oliver Heesakkers).
+
+PP/02 Restore caching of spamd results with expanded spamd_address.
+      Patch from author of expandable spamd_address patch, Wolfgang Breyha.
+
+PP/03 Build issue: lookups-Makefile now exports LC_ALL=C
+      Improves build reliability.  Fix from: Frank Elsner
+
+NM/02 Fix wide character breakage in the rfc2047 coding
+      Fixes bug 1064. Patch from Andrey N. Oktyabrski
+
+NM/03 Allow underscore in dnslist lookups
+      Fixes bug 1026. Patch from Graeme Fowler
+
+PP/04 Bugzilla 230: Support TLS-enabled LDAP (in addition to ldaps).
+      Code patches from Adam Ciarcinski of NetBSD.
+
+NM/04 Fixed exiqgrep to cope with mailq missing size issue
+      Fixes bug 943.
+
+PP/05 Bugzilla 1083: when lookup expansion defers, escape the output which
+      is logged, to avoid truncation. Patch from John Horne.
+
+PP/06 Bugzilla 1042: implement freeze_signal on pipe transports.
+      Patch from Jakob Hirsch.
+
+PP/07 Bugzilla 1061: restrict error messages sent over SMTP to not reveal
+      SQL string expansion failure details.
+      Patch from Andrey Oktyabrski.
+
+PP/08 Bugzilla 486: implement %M datestamping in log filenames.
+      Patch from Simon Arlott.
+
+PP/09 New lookups functionality failed to compile on old gcc which rejects
+      extern declarations in function scope.
+      Patch from Oliver Fleischmann
+
+PP/10 Use sig_atomic_t for flags set from signal handlers.
+      Check getgroups() return and improve debugging.
+      Fixed developed for diagnosis in bug 927 (which turned out to be
+      a kernel bug).
+
+PP/11 Bugzilla 1055: Update $message_linecount for maildir_tag.
+      Patch from Mark Zealey.
+
+PP/12 Bugzilla 1056: Improved spamd server selection.
+      Patch from Mark Zealey.
+
+PP/13 Bugzilla 1086: Deal with maildir quota file races.
+      Based on patch from Heiko Schlittermann.
+
+PP/14 Bugzilla 1019: DKIM multiple signature generation fix.
+      Patch from Uwe Doering, sign-off by Michael Haardt.
+
+NM/05 Fix to spam.c to accommodate older gcc versions which dislike
+      variable declaration deep within a block.  Bug and patch from
+      Dennis Davis.
+
+PP/15 lookups-Makefile IRIX compatibilty coercion.
+
+PP/16 Make DISABLE_DKIM build knob functional.
+
+NM/06 Bugzilla 968: child_open_uid: restore default SIGPIPE handler
+      Patch by Simon Arlott
+
+TF/03 Fix valgrind.h portability to C89 compilers that do not support
+      variable argument macros. Our copy now differs from upstream.
+
+
+Exim version 4.74
+-----------------
+
+TF/01 Failure to get a lock on a hints database can have serious
+      consequences so log it to the panic log.
+
+TF/02 Log LMTP confirmation messages in the same way as SMTP,
+      controlled using the smtp_confirmation log selector.
+
+TF/03 Include the error message when we fail to unlink a spool file.
+
+DW/01 Bugzilla 139: Support dynamically loaded lookups as modules.
+      With thanks to Steve Haslam, Johannes Berg & Serge Demonchaux
+      for maintaining out-of-tree patches for some time.
+
+PP/01 Bugzilla 139: Documentation and portability issues.
+      Avoid GNU Makefile-isms, let Exim continue to build on BSD.
+      Handle per-OS dynamic-module compilation flags.
+
+PP/02 Let /dev/null have normal permissions.
+      The 4.73 fixes were a little too stringent and complained about the
+      permissions on /dev/null.  Exempt it from some checks.
+      Reported by Andreas M. Kirchwitz.
+
+PP/03 Report version information for many libraries, including
+      Exim version information for dynamically loaded libraries.  Created
+      version.h, now support a version extension string for distributors
+      who patch heavily. Dynamic module ABI change.
+
+PP/04 CVE-2011-0017 - check return value of setuid/setgid. This is a
+      privilege escalation vulnerability whereby the Exim run-time user
+      can cause root to append content of the attacker's choosing to
+      arbitrary files.
+
+PP/05 Bugzilla 1041: merged DCC maintainer's fixes for return code.
+      (Wolfgang Breyha)
+
+PP/06 Bugzilla 1071: fix delivery logging with untrusted macros.
+      If dropping privileges for untrusted macros, we disabled normal logging
+      on the basis that it would fail; for the Exim run-time user, this is not
+      the case, and it resulted in successful deliveries going unlogged.
+      Fixed.  Reported by Andreas Metzler.
+
+
+Exim version 4.73
+-----------------
+
+PP/01 Date: & Message-Id: revert to normally being appended to a message,
+      only prepend for the Resent-* case.  Fixes regression introduced in
+      Exim 4.70 by NM/22 for Bugzilla 607.
+
+PP/02 Include check_rfc2047_length in configure.default because we're seeing
+      increasing numbers of administrators be bitten by this.
+
+JJ/01 Added DISABLE_DKIM and comment to src/EDITME
+
+PP/03 Bugzilla 994: added openssl_options main configuration option.
+
+PP/04 Bugzilla 995: provide better SSL diagnostics on failed reads.
+
+PP/05 Bugzilla 834: provide a permit_coredump option for pipe transports.
+
+PP/06 Adjust NTLM authentication to handle SASL Initial Response.
+
+PP/07 If TLS negotiated an anonymous cipher, we could end up with SSL but
+      without a peer certificate, leading to a segfault because of an
+      assumption that peers always have certificates.  Be a little more
+      paranoid.  Problem reported by Martin Tscholak.
+
+PP/08 Bugzilla 926: switch ClamAV to use the new zINSTREAM API for content
+      filtering; old API available if built with WITH_OLD_CLAMAV_STREAM=yes
+      NB: ClamAV planning to remove STREAM in "middle of 2010".
+      CL also introduces -bmalware, various -d+acl logging additions and
+      more caution in buffer sizes.
+
+PP/09 Implemented reverse_ip expansion operator.
+
+PP/10 Bugzilla 937: provide a "debug" ACL control.
+
+PP/11 Bugzilla 922: Documentation dusting, patch provided by John Horne.
+
+PP/12 Bugzilla 973: Implement --version.
+
+PP/13 Bugzilla 752: Refuse to build/run if Exim user is root/0.
+
+PP/14 Build without WITH_CONTENT_SCAN. Path from Andreas Metzler.
+
+PP/15 Bugzilla 816: support multiple condition rules on Routers.
+
+PP/16 Add bool_lax{} expansion operator and use that for combining multiple
+      condition rules, instead of bool{}.  Make both bool{} and bool_lax{}
+      ignore trailing whitespace.
+
+JJ/02 prevent non-panic DKIM error from being sent to paniclog
+
+JJ/03 added tcp_wrappers_daemon_name to allow host entries other than
+      "exim" to be used
+
+PP/17 Fix malware regression for cmdline scanner introduced in PP/08.
+      Notification from Dr Andrew Aitchison.
+
+PP/18 Change ClamAV response parsing to be more robust and to handle ClamAV's
+      ExtendedDetectionInfo response format.
+      Notification from John Horne.
+
+PP/19 OpenSSL 1.0.0a compatibility const-ness change, should be backwards
+      compatible.
+
+PP/20 Added a CONTRIBUTING file.  Fixed the documentation build to use http:
+      XSL and documented dependency on system catalogs, with examples of how
+      it normally works.
+
+DW/21 Added Valgrind hooks in store.c to help it capture out-of-bounds store
+      access.
+
+DW/22 Bugzilla 1044: CVE-2010-4345 - partial fix: restrict default behaviour
+      of CONFIGURE_OWNER and CONFIGURE_GROUP options to no longer allow a
+      configuration file which is writeable by the Exim user or group.
+
+DW/23 Bugzilla 1044: CVE-2010-4345 - part two: extend checks for writeability
+      of configuration files to cover files specified with the -C option if
+      they are going to be used with root privileges, not just the default
+      configuration file.
+
+DW/24 Bugzilla 1044: CVE-2010-4345 - part three: remove ALT_CONFIG_ROOT_ONLY
+      option (effectively making it always true).
+
+DW/25 Add TRUSTED_CONFIG_PREFIX_FILE option to allow alternative configuration
+      files to be used while preserving root privileges.
+
+DW/26 Set FD_CLOEXEC on SMTP sockets after forking in the daemon, to ensure
+      that rogue child processes cannot use them.
+
+PP/27 Bugzilla 1047: change the default for system_filter_user to be the Exim
+      run-time user, instead of root.
+
+PP/28 Add WHITELIST_D_MACROS option to let some macros be overridden by the
+      Exim run-time user without dropping privileges.
+
+DW/29 Remove use of va_copy() which breaks pre-C99 systems. Duplicate the
+      result string, instead of calling string_vformat() twice with the same
+      arguments.
+
+DW/30 Allow TRUSTED_CONFIG_PREFIX_FILE only for Exim or CONFIGURE_OWNER, not
+      for other users. Others should always drop root privileges if they use
+      -C on the command line, even for a whitelisted configure file.
+
+DW/31 Turn TRUSTED_CONFIG_PREFIX_FILE into TRUSTED_CONFIG_FILE. No prefixes.
+
+NM/01 Fixed bug #1002 - Message loss when using multiple deliveries
+
+
+Exim version 4.72
+-----------------
+
+JJ/01 installed exipick 20100104.1, adding $max_received_linelength,
+      $data_path, and $header_path variables; fixed documentation bugs and
+      typos
+
+JJ/02 installed exipick 20100222.0, added --input-dir and --finput to allow
+      exipick to access non-standard spools, including the "frozen" queue
+      (Finput)
+
+NM/01 Bugzilla 965: Support mysql stored procedures.
+      Patch from Alain Williams
+
+NM/02 Bugzilla 961: Spacing fix (syntax error) on Makefile directives for NetBSD
+
+NM/03 Bugzilla 955: Documentation fix for max_rcpts.
+      Patch from Andreas Metzler
+
+NM/04 Bugzilla 954: Fix for unknown responses from Dovecot authenticator.
+      Patch from Kirill Miazine
+
+NM/05 Bugzilla 671: Added umask to procmail example.
+
+JJ/03 installed exipick 20100323.0, fixing doc bug
+
+NM/06 Bugzilla 988: CVE-2010-2023 - prevent hardlink attack on sticky mail
+      directory.  Notification and patch from Dan Rosenberg.
+
+TK/01 PDKIM: Upgrade PolarSSL files to upstream version 0.12.1.
+
+TK/02 Improve log output when DKIM signing operation fails.
+
+MH/01 Treat the transport option dkim_domain as a colon separated
+      list, not as a single string, and sign the message with each element,
+      omitting multiple occurences of the same signer.
+
+NM/07 Null terminate DKIM strings, Null initialise DKIM variable
+      Bugzilla 985, 986.  Patch by Simon Arlott
+
+NM/08 Bugzilla 967. dnsdb DNS TXT record bug fix (DKIM-related)
+      Patch by Simon Arlott
+
+PP/01 Bugzilla 989: CVE-2010-2024 - work round race condition on
+      MBX locking.  Notification from Dan Rosenberg.
+
+
+Exim version 4.71
+-----------------
+
+TK/01 Bugzilla 912: Fix DKIM segfault on empty headers/body.
+
+NM/01 Bugzilla 913: Documentation fix for gnutls_* options.
+
+NM/02 Bugzilla 722: Documentation for randint.  Better randomness defaults.
+
+NM/03 Bugzilla 847: Enable DNSDB lookup by default.
+
+NM/04 Bugzilla 915: Flag broken perl installation during build.
+
+
+Exim version 4.70
+-----------------
+
+TK/01 Added patch by Johannes Berg that expands the main option
+      "spamd_address" if it starts with a dollar sign.
+
+TK/02 Write list of recipients to X-Envelope-Sender header when building
+      the mbox-format spool file for content scanning (suggested by Jakob
+      Hirsch).
+
+TK/03 Added patch by Wolfgang Breyha that adds experimental DCC
+      (http://www.dcc-servers.net/) support via dccifd. Activated by
+      setting EXPERIMENTAL_DCC=yes in Local/Makefile.
+
+TK/04 Bugzilla 673: Add f-protd malware scanner support. Patch submitted
+      by Mark Daniel Reidel <mr@df.eu>.
+
+NM/01 Bugzilla 657: Embedded PCRE removed from the exim source tree.
+      When building exim an external PCRE library is now needed -
+      PCRE is a system library on the majority of modern systems.
+      See entry on PCRE_LIBS in EDITME file.
+
+NM/02 Bugzilla 646: Removed unwanted C/R in Dovecot authenticator
+      conversation.  Added nologin parameter to request.
+      Patch contributed by Kirill Miazine.
+
+TF/01 Do not log submission mode rewrites if they do not change the address.
+
+TF/02 Bugzilla 662: Fix stack corruption before exec() in daemon.c.
+
+NM/03 Bugzilla 602: exicyclog now handles panic log, and creates empty
+      log files in place.  Contributed by Roberto Lima.
+
+NM/04 Bugzilla 667: Close socket used by dovecot authenticator.
+
+TF/03 Bugzilla 615: When checking the local_parts router precondition
+      after a local_part_suffix or local_part_prefix option, Exim now
+      does not use the address's named list lookup cache, since this
+      contains cached lookups for the whole local part.
+
+NM/05 Bugzilla 521: Integrated SPF Best Guess support contributed by
+      Robert Millan.  Documentation is in experimental-spec.txt.
+
+TF/04 Bugzilla 668: Fix parallel build (make -j).
+
+NM/05.2 Bugzilla 437: Prevent Maildir aux files being created with mode 000.
+
+NM/05.3 Bugzilla 598: Improvement to Dovecot authenticator handling.
+      Patch provided by Jan Srzednicki.
+
+TF/05 Leading white space used to be stripped from $spam_report which
+      wrecked the formatting. Now it is preserved.
+
+TF/06 Save $spam_score, $spam_bar, and $spam_report in spool files, so
+      that they are available at delivery time.
+
+TF/07 Fix the way ${extract is skipped in the untaken branch of a conditional.
+
+TF/08 TLS error reporting now respects the incoming_interface and
+      incoming_port log selectors.
+
+TF/09 Produce a more useful error message if an SMTP transport's hosts
+      setting expands to an empty string.
+
+NM/06 Bugzilla 744: EXPN did not work under TLS.
+      Patch provided by Phil Pennock.
+
+NM/07 Bugzilla 769: Extraneous comma in usage fprintf
+      Patch provided by Richard Godbee.
+
+NM/08 Fixed erroneous documentation references to smtp_notquit_acl to be
+      acl_smtp_notquit, added index entry.
+
+NM/09 Bugzilla 787: Potential buffer overflow in string_format.
+      Patch provided by Eugene Bujak.
+
+NM/10 Bugzilla 770: Problem on some platforms modifying the len parameter to
+      accept(). Patch provided by Maxim Dounin.
+
+NM/11 Bugzilla 749: Preserve old behaviour of blanks comparing equal to zero.
+      Patch provided by Phil Pennock.
+
+NM/12 Bugzilla 497: Correct behaviour of exiwhat when no config exists.
+
+NM/13 Bugzilla 590: Correct handling of Resent-Date headers.
+      Patch provided by Brad "anomie" Jorsch.
+
+NM/14 Bugzilla 622: Added timeout setting to transport filter.
+      Patch provided by Dean Brooks.
+
+TK/05 Add native DKIM support (does not depend on external libraries).
+
+NM/15 Bugzilla 854: Removed code that symlinks to pcre as its no longer useful.
+      Patch provided by Graeme Fowler.
+
+NM/16 Bugzilla 851: Documentation example syntax fix.
+
+NM/17 Changed NOTICE file to remove references to embedded PCRE.
+
+NM/18 Bugzilla 894: Fix issue with very long lines including comments in
+      lsearch.
+
+NM/19 Bugzilla 745: TLS version reporting.
+      Patch provided by Phil Pennock.
+
+NM/20 Bugzilla 167: bool: condition support.
+      Patch provided by Phil Pennock.
+
+NM/21 Bugzilla 665: gnutls_compat_mode to allow compatibility with broken
+      clients. Patch provided by Phil Pennock.
+
+NM/22 Bugzilla 607: prepend (not append) Resent-Message-ID and Resent-Date.
+      Patch provided by Brad "anomie" Jorsch.
+
+NM/23 Bugzilla 687: Fix misparses in eximstats.
+      Patch provided by Heiko Schlittermann.
+
+NM/24 Bugzilla 688: Fix exiwhat to handle log_selector = +pid.
+      Patch provided by Heiko Schlittermann.
+
+NM/25 Bugzilla 727: Use transport mode as default mode for maildirsize file.
+      plus update to original patch.
+
+NM/26 Bugzilla 799: Documentation correction for ratelimit.
+
+NM/27 Bugzilla 802: Improvements to local interface IP addr detection.
+      Patch provided by David Brownlee.
+
+NM/28 Bugzilla 807: Improvements to LMTP delivery logging.
+
+NM/29 Bugzilla 862, 866, 875: Documentation bugfixes.
+
+NM/30 Bugzilla 888: TLS documentation bugfixes.
+
+NM/31 Bugzilla 896: Dovecot buffer overrun fix.
+
+NM/32 Bugzilla 889: Change all instances of "expr" in shell scripts to "expr --"
+      Unlike the original bugzilla I have changed all shell scripts in src tree.
+
+NM/33 Bugzilla 898: Transport filter timeout fix.
+      Patch by Todd Rinaldo.
+
+NM/34 Bugzilla 901: Fix sign/unsigned and UTF mismatches.
+      Patch by Serge Demonchaux.
+
+NM/35 Bugzilla 39: Base64 decode bug fixes.
+      Patch by Jakob Hirsch.
+
+NM/36 Bugzilla 909: Correct connect() call in dcc code.
+
+NM/37 Bugzilla 910: Correct issue with relaxed/simple handling.
+
+NM/38 Bugzilla 908: Removed NetBSD3 support as no longer needed.
+
+NM/39 Bugzilla 911: Fixed MakeLinks build script.
+
+
+Exim version 4.69
+-----------------
+
+TK/01 Add preliminary DKIM support. Currently requires a forked version of
+      ALT-N's libdkim that I have put here:
+      http://duncanthrax.net/exim-experimental/
+
+      Note to Michael Haardt: I had to rename some vars in sieve.c. They
+      were called 'true' and it seems that C99 defines that as a reserved
+      keyword to be used with 'bool' variable types. That means you could
+      not include C99-style headers which use bools without triggering
+      build errors in sieve.c.
+
+NM/01 Bugzilla 592: --help option is handled incorrectly if exim is invoked
+      as mailq or other aliases.  Changed the --help handling significantly
+      to do whats expected.  exim_usage() emits usage/help information.
+
+SC/01 Added the -bylocaldomain option to eximstats.
+
+NM/02 Bugzilla 619: Defended against bad data coming back from gethostbyaddr.
+
+NM/03 Bugzilla 613: Documentation fix for acl_not_smtp.
+
+NM/04 Bugzilla 628: PCRE update to 7.4 (work done by John Hall).
+
+
+Exim version 4.68
+-----------------
+
+PH/01 Another patch from the Sieve maintainer.
+
+PH/02 When an IPv6 address is converted to a string for single-key lookup
+      in an address list (e.g. for an item such as "net24-dbm;/net/works"),
+      dots are used instead of colons so that keys in lsearch files need not
+      contain colons. This was done some time before quoting was made available
+      in lsearch files. However, iplsearch files do require colons in IPv6 keys
+      (notated using the quote facility) so as to distinguish them from IPv4
+      keys. This meant that lookups for IP addresses in host lists did not work
+      for iplsearch lookups.
+
+      This has been fixed by arranging for IPv6 addresses to be expressed with
+      colons if the lookup type is iplsearch. This is not incompatible, because
+      previously such lookups could never work.
+
+      The situation is now rather anomolous, since one *can* have colons in
+      ordinary lsearch keys. However, making the change in all cases is
+      incompatible and would probably break a number of configurations.
+
+TK/01 Change PRVS address formatting scheme to reflect latests BATV draft
+      version.
+
+MH/01 The "spam" ACL condition code contained a sscanf() call with a %s
+      conversion specification without a maximum field width, thereby enabling
+      a rogue spamd server to cause a buffer overflow. While nobody in their
+      right mind would setup Exim to query an untrusted spamd server, an
+      attacker that gains access to a server running spamd could potentially
+      exploit this vulnerability to run arbitrary code as the Exim user.
+
+TK/02 Bugzilla 502: Apply patch to make the SPF-Received: header use
+      $primary_hostname instead of what libspf2 thinks the hosts name is.
+
+MH/02 The dsearch lookup now uses lstat(2) instead of stat(2) to look for
+      a directory entry by the name of the lookup key. Previously, if a
+      symlink pointed to a non-existing file or a file in a directory that
+      Exim lacked permissions to read, a lookup for a key matching that
+      symlink would fail. Now it is enough that a matching directory entry
+      exists, symlink or not. (Bugzilla 503.)
+
+PH/03 The body_linecount and body_zerocount variables are now exported in the
+      local_scan API.
+
+PH/04 Added the $dnslist_matched variable.
+
+PH/05 Unset $tls_cipher and $tls_peerdn before making a connection as a client.
+      This means they are set thereafter only if the connection becomes
+      encrypted.
+
+PH/06 Added the client_condition to authenticators so that some can be skipped
+      by clients under certain conditions.
+
+PH/07 The error message for a badly-placed control=no_multiline_responses left
+      "_responses" off the end of the name.
+
+PH/08 Added -Mvc to output a copy of a message in RFC 2822 format.
+
+PH/09 Tidied the code for creating ratelimiting keys, creating them explicitly
+      (without spaces) instead of just copying the configuration text.
+
+PH/10 Added the /noupdate option to the ratelimit ACL condition.
+
+PH/11 Added $max_received_linelength.
+
+PH/12 Added +ignore_defer and +include_defer to host lists.
+
+PH/13 Installed PCRE version 7.2. This needed some changes because of the new
+      way in which PCRE > 7.0 is built.
+
+PH/14 Implemented queue_only_load_latch.
+
+PH/15 Removed an incorrect (int) cast when reading the value of SIZE in a
+      MAIL command. The effect was to mangle the value on 64-bit systems.
+
+PH/16 Another patch from the Sieve maintainer.
+
+PH/17 Added the NOTQUIT ACL, based on a patch from Ted Cooper.
+
+PH/18 If a system quota error occurred while trying to create the file for
+      a maildir delivery, the message "Mailbox is full" was not appended to the
+      bounce if the delivery eventually timed out. Change 4.67/27 below applied
+      only to a quota excession during the actual writing of the file.
+
+PH/19 It seems that peer DN values may contain newlines (and other non-printing
+      characters?) which causes problems in log lines. The DN values are now
+      passed through string_printing() before being added to log lines.
+
+PH/20 Added the "servers=" facility to MySQL and PostgreSQL lookups. (Oracle
+      and InterBase are left for another time.)
+
+PH/21 Added message_body_newlines option.
+
+PH/22 Guard against possible overflow in moan_check_errorcopy().
+
+PH/23 POSIX allows open() to be a macro; guard against that.
+
+PH/24 If the recipient of an error message contained an @ in the local part
+      (suitably quoted, of course), incorrect values were put in $domain and
+      $local_part during the evaluation of errors_copy.
+
+
+Exim version 4.67
+-----------------
+
+MH/01 Fix for bug #448, segfault in Dovecot authenticator when interface_address
+      is unset (happens when testing with -bh and -oMi isn't used). Thanks to
+      Jan Srzednicki.
+
+PH/01 Added a new log selector smtp_no_mail, to log SMTP sessions that do not
+      issue a MAIL command.
+
+PH/02 In an ACL statement such as
+
+        deny dnslists = X!=127.0.0.2 : X=127.0.0.2
+
+      if a client was not listed at all, or was listed with a value other than
+      127.0.0.2, in the X list, but was listed with 127.0.0.2 in the Y list,
+      the condition was not true (as it should be), so access was not denied.
+      The bug was that the ! inversion was incorrectly passed on to the second
+      item. This has been fixed.
+
+PH/03 Added additional dnslists conditions == and =& which are different from
+      = and & when the dns lookup returns more than one IP address.
+
+PH/04 Added gnutls_require_{kx,mac,protocols} to give more control over the
+      cipher suites used by GnuTLS. These options are ignored by OpenSSL.
+
+PH/05 After discussion on the list, added a compile time option ENABLE_DISABLE_
+      FSYNC, which compiles an option called disable_fsync that allows for
+      bypassing fsync(). The documentation is heavily laced with warnings.
+
+SC/01 Updated eximstats to collate all SpamAssassin rejects into one bucket.
+
+PH/06 Some tidies to the infrastructure of the Test Suite that is concerned
+      with the auxiliary C programs that it uses: (1) Arrange for BIND_8_COMPAT
+      to be defined when compiling on OSX (Darwin); (2) Tidies to the Makefile,
+      including adding "make clean"; (3) Added -fPIC when compiling the test
+      dynamically loaded module, to get rid of a warning.
+
+MH/02 Fix for bug #451, causing paniclog entries to be written if a bounce
+      message fails, move_frozen_messages = true and ignore_bounce_errors_after
+      = 0s. The bug is otherwise harmless.
+
+PH/07 There was a bug in the dovecot authenticator such that the value of
+      $auth1 could be overwritten, and so not correctly preserved, after a
+      successful authentication. This usually meant that the value preserved by
+      the server_setid option was incorrect.
+
+PH/08 Added $smtp_count_at_connection_start, deliberately with a long name.
+
+PH/09 Installed PCRE release 7.0.
+
+PH/10 The acl_not_smtp_start ACL was, contrary to the documentation, not being
+      run for batched SMTP input. It is now run at the start of every message
+      in the batch. While fixing this I discovered that the process information
+      (output by running exiwhat) was not always getting set for -bs and -bS
+      input. This is fixed, and it now also says "batched" for BSMTP.
+
+PH/11 Added control=no_pipelining.
+
+PH/12 Added $sending_ip_address and $sending_port (mostly Magnus Holmgren's
+      patch, slightly modified), and move the expansion of helo_data till after
+      the connection is made in the smtp transport (so it can use these
+      values).
+
+PH/13 Added ${rfc2047d: to decoded RFC 2047 strings.
+
+PH/14 Added log_selector = +pid.
+
+PH/15 Flush SMTP output before delaying, unless control=no_delay_flush is set.
+
+PH/16 Add ${if forany and ${if forall.
+
+PH/17 Added dsn_from option to vary the From: line in DSNs.
+
+PH/18 Flush SMTP output before performing a callout, unless control =
+      no_callout_flush is set.
+
+PH/19 Change 4.64/PH/36 introduced a bug: when address_retry_include_sender
+      was true (the default) a successful delivery failed to delete the retry
+      item, thus causing premature timeout of the address. The bug is now
+      fixed.
+
+PH/20 Added hosts_avoid_pipelining to the smtp transport.
+
+PH/21 Long custom messages for fakedefer and fakereject are now split up
+      into multiline reponses in the same way that messages for "deny" and
+      other ACL rejections are.
+
+PH/22 Applied Jori Hamalainen's speed-up changes and typo fixes to exigrep,
+      with slight modification.
+
+PH/23 Applied sieve patches from the maintainer "tracking the latest notify
+      draft, changing the syntax and factoring some duplicate code".
+
+PH/24 When the log selector "outgoing_port" was set, the port was shown as -1
+      for deliveries of the second and subsequent messages over the same SMTP
+      connection.
+
+PH/25 Applied Magnus Holmgren's patch for ${addresses, ${map, ${filter, and
+      ${reduce, with only minor "tidies".
+
+SC/02 Applied Daniel Tiefnig's patch to improve the '($parent) =' pattern match.
+
+PH/26 Added a "continue" ACL modifier that does nothing, for the benefit of its
+      expansion side effects.
+
+PH/27 When a message times out after an over-quota error from an Exim-imposed
+      quota, the bounce message says "mailbox is full". This message was not
+      being given when it was a system quota that was exceeded. It now should
+      be the same.
+
+MH/03 Made $recipients available in local_scan(). local_scan() already has
+      better access to the recipient list through recipients_list[], but
+      $recipients can be useful in postmaster-provided expansion strings.
+
+PH/28 The $smtp_command and $smtp_command_argument variables were not correct
+      in the case of a MAIL command with additional options following the
+      address, for example: MAIL FROM:<foo@bar> SIZE=1234. The option settings
+      were accidentally chopped off.
+
+PH/29 SMTP synchronization checks are implemented when a command is read -
+      there is a check that no more input is waiting when there shouldn't be
+      any. However, for some commands, a delay in an ACL can mean that it is
+      some time before the response is written. In this time, more input might
+      arrive, invalidly. So now there are extra checks after an ACL has run for
+      HELO/EHLO and after the predata ACL, and likewise for MAIL and RCPT when
+      pipelining has not been advertised.
+
+PH/30 MH's patch to allow iscntrl() characters to be list separators.
+
+PH/31 Unlike :fail:, a custom message specified with :defer: was not being
+      returned in the SMTP response when smtp_return_error_details was false.
+      This has been fixed.
+
+PH/32 Change the Dovecot authenticator to use read() and write() on the socket
+      instead of the C I/O that was originally supplied, because problems were
+      reported on Solaris.
+
+PH/33 Compile failed with OpenSSL 0.9.8e. This was due to a coding error in
+      Exim which did not show up earlier: it was assuming that a call to
+      SSL_CTX_set_info_callback() might give an error value. In fact, there is
+      no error. In previous releases of OpenSSL, SSL_CTX_set_info_callback()
+      was a macro that became an assignment, so it seemed to work. This has
+      changed to a proper function call with a void return, hence the compile
+      error. Exim's code has been fixed.
+
+PH/34 Change HDA_SIZE in oracle.c from 256 to 512. This is needed for 64-bit
+      cpus.
+
+PH/35 Applied a patch from the Sieve maintainer which fixes a bug in "notify".
+
+PH/36 Applied John Jetmore's patch to add -v functionality to exigrep.
+
+PH/37 If a message is not accepted after it has had an id assigned (e.g.
+      because it turns out to be too big or there is a timeout) there is no
+      "Completed" line in the log. When some messages of this type were
+      selected by exigrep, they were listed as "not completed". Others were
+      picked up by some special patterns. I have improved the selection
+      criteria to be more general.
+
+PH/38 The host_find_failed option in the manualroute router can now be set
+      to "ignore", to completely ignore a host whose IP address cannot be
+      found. If all hosts are ignored, the behaviour is controlled by the new
+      host_all_ignored option.
+
+PH/39 In a list of hosts for manualroute, if one item (either because of multi-
+      homing or because of multiple MX records with /mx) generated more than
+      one IP address, and the following item turned out to be the local host,
+      all the secondary addresses of the first item were incorrectly removed
+      from the list, along with the local host and any following hosts (which
+      is what is supposed to happen).
+
+PH/40 When Exim receives a message, it writes the login name, uid, and gid of
+      whoever called Exim into the -H file. In the case of the daemon it was
+      behaving confusingly. When first started, it used values for whoever
+      started the daemon, but after a SIGHUP it used the Exim user (because it
+      calls itself on a restart). I have changed the code so that it now always
+      uses the Exim user.
+
+PH/41 (Following a suggestion from Tony Finch) If all the RCPT commands in a
+      message are rejected with the same error (e.g. no authentication or bad
+      sender address), and a DATA command is nevertheless sent (as can happen
+      with PIPELINING or a stupid MUA), the error message that was given to the
+      RCPT commands is included in the rejection of the DATA command. This is
+      intended to be helpful for MUAs that show only the final error to their
+      users.
+
+PH/42 Another patch from the Sieve maintainer.
+
+SC/02 Eximstats - Differentiate between permanent and temporary rejects.
+      Eximstats - Fixed some broken HTML links and added missing column headers
+                  (Jez Hancock).
+      Eximstats - Fixed Grand Total Summary Domains, Edomains, and Email
+                  columns for Rejects, Temp Rejects, Ham, and Spam rows.
+
+SC/03 Eximstats - V1.58 Fix to get <> and blackhole to show in edomain tables.
+
+PH/43 Yet another patch from the Sieve maintainer.
+
+PH/44 I found a way to check for a TCP/IP connection going away before sending
+      the response to the final '.' that terminates a message, but only in the
+      case where the client has not sent further data following the '.'
+      (unfortunately, this is allowed). However, in many cases there won't be
+      any further data because there won't be any more messages to send. A call
+      to select() can be used: if it shows that the input is "ready", there is
+      either input waiting, or the socket has been closed. An attempt to read
+      the next input character can distinguish the two cases. Previously, Exim
+      would have sent an OK response which the client would never have see.
+      This could lead to message repetition. This fix should cure that, at
+      least in a lot of common cases.
+
+PH/45 Do not advertise STARTTLS in response to HELP unless it would be
+      advertised in response to EHLO.
+
+
+Exim version 4.66
+-----------------
+
+PH/01 Two more bugs that were introduced by 4.64/PH/07, in addition to the one
+      fixed by 4.65/MH/01 (is this a record?) are fixed:
+
+      (i)  An empty string was always treated as zero by the numeric comparison
+           operators. This behaviour has been restored.
+
+      (ii) It is documented that the numeric comparison operators always treat
+           their arguments as decimal numbers. This was broken in that numbers
+           starting with 0 were being interpreted as octal.
+
+      While fixing these problems I realized that there was another issue that
+      hadn't been noticed. Values of message_size_limit (both the global option
+      and the transport option) were treated as octal if they started with 0.
+      The documentation was vague. These values are now always treated as
+      decimal, and I will make that clear in the documentation.
+
+
+Exim version 4.65
+-----------------
+
+TK/01 Disable default definition of HAVE_LINUX_SENDFILE. Clashes with
+      Linux large file support (_FILE_OFFSET_BITS=64) on older glibc
+      versions. (#438)
+
+MH/01 Don't check that the operands of numeric comparison operators are
+      integers when their expansion is in "skipping" mode (fixes bug
+      introduced by 4.64-PH/07).
+
+PH/01 If a system filter or a router generates more than SHRT_MAX (32767)
+      child addresses, Exim now panics and dies. Previously, because the count
+      is held in a short int, deliveries were likely to be lost. As such a
+      large number of recipients for a single message is ridiculous
+      (performance will be very, very poor), I have chosen to impose a limit
+      rather than extend the field.
+
+
+Exim version 4.64
+-----------------
+
+TK/01 Bugzilla #401. Fix DK spooling code so that it can overwrite a
+      leftover -K file (the existence of which was triggered by #402).
+      While we were at it, introduced process PID as part of the -K
+      filename. This should rule out race conditions when creating
+      these files.
+
+TK/02 Bugzilla #402. Apply patch from Simon Arlott, speeding up DK signing
+      processing considerably. Previous code took too long for large mails,
+      triggering a timeout which in turn triggers #401.
+
+TK/03 Introduced HAVE_LINUX_SENDFILE to os.h-Linux. Currently only used
+      in the DK code in transports.c. sendfile() is not really portable,
+      hence the _LINUX specificness.
+
+TF/01 In the add_headers option to the mail command in an Exim filter,
+      there was a bug that Exim would claim a syntax error in any
+      header after the first one which had an odd number of characters
+      in the field name.
+
+PH/01 If a server that rejects MAIL FROM:<> was the target of a sender
+      callout verification, Exim cached a "reject" for the entire domain. This
+      is correct for most verifications, but it is not correct for a recipient
+      verification with use_sender or use_postmaster set, because in that case
+      the callout does not use MAIL FROM:<>. Exim now distinguishes the special
+      case of MAIL FROM:<> rejection from other early rejections (e.g.
+      rejection of HELO). When verifying a recipient using a non-null MAIL
+      address, the cache is ignored if it shows MAIL FROM:<> rejection.
+      Whatever the result of the callout, the value of the domain cache is
+      left unchanged (for any other kind of callout, getting as far as trying
+      RCPT means that the domain itself is ok).
+
+PH/02 Tidied a number of unused variable and signed/unsigned warnings that
+      gcc 4.1.1 threw up.
+
+PH/03 On Solaris, an unexpectedly close socket (dropped connection) can
+      manifest itself as EPIPE rather than ECONNECT. When tidying away a
+      session, the daemon ignores ECONNECT errors and logs others; it now
+      ignores EPIPE as well.
+
+PH/04 Applied Nico Erfurth's refactoring patch to tidy up mime.c
+      (quoted-printable decoding).
+
+PH/05 Applied Nico Erfurth's refactoring patch to tidy up spool_mbox.c, and
+      later the small subsequent patch to fix an introduced bug.
+
+PH/06 Installed the latest Cygwin Makefile from the Cygwin maintainer.
+
+PH/07 There was no check for overflow in expansions such as ${if >{1}{4096M}}.
+
+PH/08 An error is now given if message_size_limit is specified negative.
+
+PH/09 Applied and tidied up Jakob Hirsch's patch for allowing ACL variables
+      to be given (somewhat) arbitrary names.
+
+JJ/01 exipick 20060919.0, allow for arbitrary acl_ variables introduced
+      in 4.64-PH/09.
+
+JJ/02 exipick 20060919.0, --show-vars args can now be regular expressions,
+      miscellaneous code fixes
+
+PH/10 Added the log_reject_target ACL modifier to specify where to log
+      rejections.
+
+PH/11 Callouts were setting the name used for EHLO/HELO from $smtp_active_
+      hostname. This is wrong, because it relates to the incoming message (and
+      probably the interface on which it is arriving) and not to the outgoing
+      callout (which could be using a different interface). This has been
+      changed to use the value of the helo_data option from the smtp transport
+      instead - this is what is used when a message is actually being sent. If
+      there is no remote transport (possible with a router that sets up host
+      addresses), $smtp_active_hostname is used.
+
+PH/12 Installed Andrey Panin's patch to add a dovecot authenticator. Various
+      tweaks were necessary in order to get it to work (see also 21 below):
+      (a) The code assumed that strncpy() returns a negative number on buffer
+          overflow, which isn't the case. Replaced with Exim's string_format()
+          function.
+      (b) There were several signed/unsigned issues. I just did the minimum
+          hacking in of casts. There is scope for a larger refactoring.
+      (c) The code used strcasecmp() which is not a standard C function.
+          Replaced with Exim's strcmpic() function.
+      (d) The code set only $1; it now sets $auth1 as well.
+      (e) A simple test gave the error "authentication client didn't specify
+          service in request". It would seem that Dovecot has changed its
+          interface. Fortunately there's a specification; I followed it and
+          changed what the client sends and it appears to be working now.
+
+PH/13 Added $message_headers_raw to provide the headers without RFC 2047
+      decoding.
+
+PH/14 Corrected misleading output from -bv when -v was also used. Suppose the
+      address A is aliased to B and C, where B exists and C does not. Without
+      -v the output is "A verified" because verification stops after a
+      successful redirection if more than one address is generated. However,
+      with -v the child addresses are also verified. Exim was outputting "A
+      failed to verify" and then showing the successful verification for C,
+      with its parentage. It now outputs "B failed to verify", showing B's
+      parentage before showing the successful verification of C.
+
+PH/15 Applied Michael Deutschmann's patch to allow DNS black list processing to
+      look up a TXT record in a specific list after matching in a combined
+      list.
+
+PH/16 It seems that the options setting for the resolver (RES_DEFNAMES and
+      RES_DNSRCH) can affect the behaviour of gethostbyname() and friends when
+      they consult the DNS. I had assumed they would set it the way they
+      wanted; and indeed my experiments on Linux seem to show that in some
+      cases they do (I could influence IPv6 lookups but not IPv4 lookups).
+      To be on the safe side, however, I have now made the interface to
+      host_find_byname() similar to host_find_bydns(), with an argument
+      containing the DNS resolver options. The host_find_byname() function now
+      sets these options at its start, just as host_find_bydns() does. The smtp
+      transport options dns_qualify_single and dns_search_parents are passed to
+      host_find_byname() when gethostbyname=TRUE in this transport. Other uses
+      of host_find_byname() use the default settings of RES_DEFNAMES
+      (qualify_single) but not RES_DNSRCH (search_parents).
+
+PH/17 Applied (a modified version of) Nico Erfurth's patch to make
+      spool_read_header() do less string testing, by means of a preliminary
+      switch on the second character of optional "-foo" lines. (This is
+      overdue, caused by the large number of possibilities that now exist.
+      Originally there were few.) While I was there, I also converted the
+      str(n)cmp tests so they don't re-test the leading "-" and the first
+      character, in the hope this might squeeze out yet more improvement.
+
+PH/18 Two problems with "group" syntax in header lines when verifying: (1) The
+      flag allowing group syntax was set by the header_syntax check but not
+      turned off, possible causing trouble later; (2) The flag was not being
+      set at all for the header_verify test, causing "group"-style headers to
+      be rejected. I have now set it in this case, and also caused header_
+      verify to ignore an empty address taken from a group. While doing this, I
+      came across some other cases where the code for allowing group syntax
+      while scanning a header line wasn't quite right (mostly, not resetting
+      the flag correctly in the right place). These bugs could have caused
+      trouble for malformed header lines. I hope it is now all correct.
+
+PH/19 The functions {pwcheck,saslauthd}_verify_password() are always called
+      with the "reply" argument non-NULL. The code, however (which originally
+      came from elsewhere) had *some* tests for NULL when it wrote to *reply,
+      but it didn't always do it. This confused somebody who was copying the
+      code for some other use. I have removed all the tests.
+
+PH/20 It was discovered that the GnuTLS code had support for RSA_EXPORT, a
+      feature that was used to support insecure browsers during the U.S. crypto
+      embargo. It requires special client support, and Exim is probably the
+      only MTA that supported it -- and would never use it because real RSA is
+      always available. This code has been removed, because it had the bad
+      effect of slowing Exim down by computing (never used) parameters for the
+      RSA_EXPORT functionality.
+
+PH/21 On the advice of Timo Sirainen, added a check to the dovecot
+      authenticator to fail if there's a tab character in the incoming data
+      (there should never be unless someone is messing about, as it's supposed
+      to be base64-encoded). Also added, on Timo's advice, the "secured" option
+      if the connection is using TLS or if the remote IP is the same as the
+      local IP, and the "valid-client-cert option" if a client certificate has
+      been verified.
+
+PH/22 As suggested by Dennis Davis, added a server_condition option to *all*
+      authenticators. This can be used for authorization after authentication
+      succeeds. (In the case of plaintext, it servers for both authentication
+      and authorization.)
+
+PH/23 Testing for tls_required and lost_connection in a retry rule didn't work
+      if any retry times were supplied.
+
+PH/24 Exim crashed if verify=helo was activated during an incoming -bs
+      connection, where there is no client IP address to check. In this
+      situation, the verify now always succeeds.
+
+PH/25 Applied John Jetmore's -Mset patch.
+
+PH/26 Added -bem to be like -Mset, but loading a message from a file.
+
+PH/27 In a string expansion for a processed (not raw) header when multiple
+      headers of the same name were present, leading whitespace was being
+      removed from all of them, but trailing whitespace was being removed only
+      from the last one. Now trailing whitespace is removed from each header
+      before concatenation. Completely empty headers in a concatenation (as
+      before) are ignored.
+
+PH/28 Fixed bug in backwards-compatibility feature of PH/09 (thanks to John
+      Jetmore). It would have mis-read ACL variables from pre-4.61 spool files.
+
+PH/29 [Removed. This was a change that I later backed out, and forgot to
+      correct the ChangeLog entry (that I had efficiently created) before
+      committing the later change.]
+
+PH/30 Exim was sometimes attempting to deliver messages that had suffered
+      address errors (4xx response to RCPT) over the same connection as other
+      messages routed to the same hosts. Such deliveries are always "forced",
+      so retry times are not inspected. This resulted in far too many retries
+      for the affected addresses. The effect occurred only when there were more
+      hosts than the hosts_max_try setting in the smtp transport when it had
+      the 4xx errors. Those hosts that it had tried were not added to the list
+      of hosts for which the message was waiting, so if all were tried, there
+      was no problem. Two fixes have been applied:
+
+      (i)  If there are any address or message errors in an SMTP delivery, none
+           of the hosts (tried or untried) are now added to the list of hosts
+           for which the message is waiting, so the message should not be a
+           candidate for sending over the same connection that was used for a
+           successful delivery of some other message. This seems entirely
+           reasonable: after all the message is NOT "waiting for some host".
+           This is so "obvious" that I'm not sure why it wasn't done
+           previously. Hope I haven't missed anything, but it can't do any
+           harm, as the worst effect is to miss an optimization.
+
+      (ii) If, despite (i), such a delivery is accidentally attempted, the
+           routing retry time is respected, so at least it doesn't keep
+           hammering the server.
+
+PH/31 Installed Andrew Findlay's patch to close the writing end of the socket
+      in ${readsocket because some servers need this prod.
+
+PH/32 Added some extra debug output when updating a wait-xxx database.
+
+PH/33 The hint "could be header name not terminated by colon", which has been
+      given for certain expansion errors for a long time, was not being given
+      for the ${if def:h_colon_omitted{...  case.
+
+PH/34 The spec says: "With one important exception, whenever a domain list is
+      being scanned, $domain contains the subject domain." There was at least
+      one case where this was not true.
+
+PH/35 The error "getsockname() failed: connection reset by peer" was being
+      written to the panic log as well as the main log, but it isn't really
+      panic-worthy as it just means the connection died rather early on. I have
+      removed the panic log writing for the ECONNRESET error when getsockname()
+      fails.
+
+PH/36 After a 4xx response to a RCPT error, that address was delayed (in queue
+      runs only) independently of the message's sender address. This meant
+      that, if the 4xx error was in fact related to the sender, a different
+      message to the same recipient with a different sender could confuse
+      things. In particualar, this can happen when sending to a greylisting
+      server, but other circumstances could also provoke similar problems.
+      I have changed the default so that the retry time for these errors is now
+      based a combination of the sender and recipient addresses. This change
+      can be overridden by setting address_retry_include_sender=false in the
+      smtp transport.
+
+PH/37 For LMTP over TCP/IP (the smtp transport), error responses from the
+      remote server are returned as part of bounce messages. This was not
+      happening for LMTP over a pipe (the lmtp transport), but now it is the
+      same for both kinds of LMTP.
+
+PH/38 Despite being documented as not happening, Exim was rewriting addresses
+      in header lines that were in fact CNAMEs. This is no longer the case.
+
+PH/39 If -R or -S was given with -q<time>, the effect of -R or -S was ignored,
+      and queue runs started by the daemon processed all messages. This has
+      been fixed so that -R and -S can now usefully be given with -q<time>.
+
+PH/40 Import PCRE release 6.7 (fixes some bugs).
+
+PH/41 Add bitwise logical operations to eval (courtesy Brad Jorsch).
+
+PH/42 Give an error if -q is specified more than once.
+
+PH/43 Renamed the variables $interface_address and $interface_port as
+      $received_ip_address and $received_port, to make it clear that these
+      values apply to message reception, and not to the outgoing interface when
+      a message is delivered. (The old names remain recognized, of course.)
+
+PH/44 There was no timeout on the connect() call when using a Unix domain
+      socket in the ${readsocket expansion. There now is.
+
+PH/45 Applied a modified version of Brad Jorsch's patch to allow "message" to
+      be meaningful with "accept".
+
+SC/01 Eximstats V1.43
+     Bug fix for V1.42 with -h0 specified. Spotted by Chris Lear.
+
+SC/02 Eximstats V1.44
+      Use a glob alias rather than an array ref in the generated
+      parser. This improves both readability and performance.
+
+SC/03 Eximstats V1.45 (Marco Gaiarin / Steve Campbell)
+      Collect SpamAssassin and rejection statistics.
+      Don't display local sender or destination tables unless
+      there is data to show.
+      Added average volumes into the top table text output.
+
+SC/04 Eximstats V1.46
+      Collect data on the number of addresses (recipients)
+      as well as the number of messages.
+
+SC/05 Eximstats V1.47
+      Added 'Message too big' to the list of mail rejection
+      reasons (thanks to Marco Gaiarin).
+
+SC/06 Eximstats V1.48
+      Mainlog lines which have GMT offsets and are too short to
+      have a flag are now skipped.
+
+SC/07 Eximstats V1.49 (Alain Williams)
+      Added the -emptyok flag.
+
+SC/08 Eximstats V1.50
+      Fixes for obtaining the IP address from reject messages.
+
+JJ/03 exipick.20061117.2, made header handling as similar to exim as possible
+      (added [br]h_ prefixes, implemented RFC2047 decoding.  Fixed
+      whitesspace changes from 4.64-PH/27
+
+JJ/04 exipick.20061117.2, fixed format and added $message_headers_raw to
+      match 4.64-PH/13
+
+JJ/05 exipick.20061117.2, bug fixes (error out sooner when invalid criteria
+      are found, allow negative numbers in numeric criteria)
+
+JJ/06 exipick.20061117.2, added new $message_body_missing variable
+
+JJ/07 exipick.20061117.2, added $received_ip_address and $received_port
+      to match changes made in 4.64-PH/43
+
+PH/46 Applied Jori Hamalainen's patch to add features to exiqsumm.
+
+PH/47 Put in an explicit test for a DNS lookup of an address record where the
+      "domain" is actually an IP address, and force a failure. This locks out
+      those revolvers/nameservers that support "A-for-A" lookups, in
+      contravention of the specifications.
+
+PH/48 When a host name was looked up from an IP address, and the subsequent
+      forward lookup of the name timed out, the host name was left in
+      $sender_host_name, contrary to the specification.
+
+PH/49 Although default lookup types such as lsearch* or cdb*@ have always been
+      restricted to single-key lookups, Exim was not diagnosing an error if
+      * or *@ was used with a query-style lookup.
+
+PH/50 Increased the value of DH_BITS in tls-gnu.c from 768 to 1024.
+
+MH/01 local_scan ABI version incremented to 1.1. It should have been updated
+      long ago, but noone interested enough thought of it. Let's just say that
+      the "1.1" means that there are some new functions that weren't there at
+      some point in the past.
+
+PH/51 Error processing for expansion failure of helo_data from an smtp
+      transport during callout processing was broken.
+
+PH/52 Applied John Jetmore's patch to allow tls-on-connect and STARTTLS to be
+      tested/used via the -bh/-bhc/-bs options.
+
+PH/53 Added missing "#include <time.h>" to pcre/pcretest.c (this was a PCRE
+      bug, fixed in subsequent PCRE releases).
+
+PH/54 Applied Robert Bannocks' patch to avoid a problem with references that
+      arises when using the Solaris LDAP libraries (but not with OpenLDAP).
+
+PH/55 Check for a ridiculously long file name in exim_dbmbuild.
+
+
+Exim version 4.63
+-----------------
+
+SC/01 Use a glob alias rather than an array ref in eximstats generated
+      parser. This improves both readability and performance.
+
+SC/02 Collect SpamAssassin and rejection statistics in eximstats.
+      Don't display local sender or destination tables in eximstats unless
+      there is data to show.
+      Added average volumes into the eximstats top table text output.
+
+SC/03 Collect data on the number of addresses (recipients) as well
+      as the number of messages in eximstats.
+
+TF/01 Correct an error in the documentation for the redirect router. Exim
+      does (usually) call initgroups() when daemonizing.
+
+TF/02 Call initgroups() when dropping privilege in exim.c, so that Exim runs
+      with consistent privilege compared to when running as a daemon.
+
+TF/03 Note in the spec that $authenticated_id is not set for local
+      submissions from trusted users.
+
+TF/04 The ratelimit per_rcpt option now works correctly in acl_not_smtp.
+      Thanks to Dean Brooks <dean@iglou.com> for the patch.
+
+TF/05 Make it easier to get SMTP authentication and TLS/SSL support working
+      by adding some example configuration directives to the default
+      configuration file. A little bit of work is required to uncomment the
+      directives and define how usernames and passwords are checked, but
+      there is now a framework to start from.
+
+PH/01 Added #define LDAP_DEPRECATED 1 to ldap.c because some of the "old"
+      functions that Exim currently uses aren't defined in ldap.h for OpenLDAP
+      without this. I don't know how relevant this is to other LDAP libraries.
+
+PH/02 Add the verb name to the "unknown ACL verb" error.
+
+PH/03 Magnus Holmgren's patch for filter_prepend_home.
+
+PH/03 Fixed Bugzilla #101: macro definition between ACLs doesn't work.
+
+PH/04 Applied Magnus Holmgren's patch to fix Bugzilla #98: transport's home
+      directory not expanded when it should be if an expanded home directory
+      was set for the address (which is overridden by the transport).
+
+PH/05 Applied Alex Kiernan's patch to fix Bugzilla #99: a problem with
+      libradius.
+
+PH/06 Added acl_not_smtp_start, based on Johannes Berg's patch, and set the
+      bit to forbid control=suppress_local_fixups in the acl_not_smtp ACL,
+      because it is too late at that time, and has no effect.
+
+PH/07 Changed ${quote_pgsql to quote ' as '' instead of \' because of a
+      security issue with \' (bugzilla #107). I could not use the
+      PQescapeStringConn() function, because it needs a PGconn value as one of
+      its arguments.
+
+PH/08 When testing addresses using -bt, indicate those final addresses that
+      are duplicates that would not cause an additional delivery. At least one
+      person was confused, thinking that -bt output corresponded to deliveries.
+      (Suppressing duplicates isn't a good idea as you lose the information
+      about possibly different redirections that led to the duplicates.)
+
+PH/09 Applied patch from Erik to use select() instead of poll() in spam.c on
+      systems where poll() doesn't work, in particular OS X.
+
+PH/10 Added more information to debugging output for retry time not reached.
+
+PH/11 Applied patch from Arkadiusz Miskiewicz to apply a timeout to read
+      operations in malware.c.
+
+PH/12 Applied patch from Magnus Holmgren to include the "h" tag in Domain Keys
+      signatures.
+
+PH/13 If write_rejectlog was set false when logging was sent to syslog with
+      syslog_duplication set false, log lines that would normally be written
+      both the the main log and to the reject log were not written to syslog at
+      all.
+
+PH/14 In the default configuration, change the use of "message" in ACL warn
+      statements to "add_header".
+
+PH/15 Diagnose a filter syntax error for "seen", "unseen", or "noerror" if not
+      not followed by a command (e.g. "seen endif").
+
+PH/16 Recognize SMTP codes at the start of "message" in ACLs and after :fail:
+      and :defer: in a redirect router. Add forbid_smtp_code to suppress the
+      latter.
+
+PH/17 Added extra conditions to the default value of delay_warning_condition
+      so that it is now:
+
+        ${if or { \
+            { !eq{$h_list-id:$h_list-post:$h_list-subscribe:}{} } \
+            { match{$h_precedence:}{(?i)bulk|list|junk} } \
+            { match{$h_auto-submitted:}{(?i)auto-generated|auto-replied} } \
+            }{no}{yes}}
+
+      The Auto-Submitted: and various List- headers are standardised, whereas I
+      don't think Precedence: ever was.
+
+PH/18 Refactored debugging code in route_finduser() to show more information,
+      in particular, the error code if getpwnam() issues one.
+
+PH/19 Added PQsetClientEncoding(conn, "SQL_ASCII") to the pgsql code module.
+      This is apparently needed in addition to the PH/07 change above to avoid
+      any possible encoding problems.
+
+PH/20 Perl can change the locale. Exim was resetting it after a ${perl call,
+      but not after initializing Perl.
+
+PH/21 Added a call to PQsetNoticeProcessor() to catch pgsql "notices" and
+      output them only if debugging. By default they are written stderr,
+      apparently, which is not desirable.
+
+PH/22 Added Alain Williams' LDAP patch to support setting REFERRALS=off on
+      queries.
+
+JJ/01 exipick: added --reverse (and -R synonym), --random, --size, --sort and
+      --not options
+
+JJ/02 exipick: rewrote --help documentation to hopefully make more clear.
+
+PH/23 Made -oMaa and -oMt work with -bh and -bs to pretend the connection is
+      authenticated or an ident call has been made. Suppress the default
+      values for $authenticated_id and $authenticated_sender (but permit -oMai
+      and -oMas) when testing with -bh.
+
+PH/24 Re-jigged the order of the tests in the default configuration so that the
+      tests for valid domains and recipients precede the DNS black list and CSA
+      tests, on the grounds that those ones are more expensive.
+
+PH/25 Exim was not testing for a space following SMTP commands such as EHLO
+      that require one. Thus, EHLORHUBARB was interpreted as a valid command.
+      This bug exists in every version of Exim that I still have, right back to
+      0.12.
+
+PH/26 (n)wildlsearch lookups are documented as being done case-insensitively.
+      However, an attempt to turn on case-sensitivity in a regex key by
+      including (?-i) didn't work because the subject string was already
+      lowercased, and the effects were non-intuitive. It turns out that a
+      one-line patch can be used to allow (?-i) to work as expected.
+
+
+Exim version 4.62
+-----------------
+
+TF/01 Fix the add_header change below (4.61 PH/55) which had a bug that (amongst
+      other effects) broke the use of negated acl sub-conditions.
+
+PH/01 ${readsocket now supports Internet domain sockets (modified John Jetmore
+      patch).
+
+PH/02 When tcp-wrappers is called from Exim, it returns only "deny" or "allow".
+      "Deny" causes Exim to reject the incoming connection with a 554 error.
+      Unfortunately, if there is a major crisis, such as a disk failure,
+      tcp-wrappers gives "deny", whereas what one would like would be some
+      kind of temporary error. A kludge has been added to help with this.
+      Before calling hosts_ctl(), errno is set zero. If the result is "deny", a
+      554 error is used if errno is still zero or contains ENOENT (which occurs
+      if either of the /etc/hosts.{allow,deny} files is missing). Otherwise, a
+      451 error is used.
+
+PH/03 Add -lutil to the default FreeBSD LIBS setting.
+
+PH/04 Change PH/19 for 4.61 was too wide. It should not be applied to host
+      errors. Otherwise a message that provokes a temporary error (when other
+      messages do not) can cause a whole host to time out.
+
+PH/05 Batch deliveries by appendfile and pipe transports did not work when the
+      addresses were routed directly to files or pipes from a redirect router.
+      File deliveries just didn't batch; pipe deliveries might have suffered
+      odd errors.
+
+PH/06 A failure to get a lock for a hints database would erroneously always say
+      "Failed to get write lock", even when it was really a read lock.
+
+PH/07 The appendfile transport was creating MBX lock files with a fixed mode
+      of 0600. This has been changed to use the value of the lockfile_mode
+      option (which defaults to 0600).
+
+PH/08 Applied small patch from the Sieve maintainer.
+
+PH/09 If maildir_quota_directory_regex was set to exclude (say) the .Trash
+      folder from quota calculations, a direct delivery into this folder messed
+      up the contents of the maildirsize file. This was because the regex was
+      used only to exclude .Trash (or whatever) when the size of the mailbox
+      was calculated. There was no check that a delivery was happening into an
+      excluded directory. This bug has been fixed by ignoring all quota
+      processing for deliveries into excluded directories.
+
+PH/10 Added the maildirfolder_create_regex option to appendfile.
+
+
+Exim version 4.61
+-----------------
+
+PH/01 The code for finding all the local interface addresses on a FreeBSD
+      system running IPv6 was broken. This may well have applied to all BSD
+      systems, as well as to others that have similar system calls. The broken
+      code found IPv4 interfaces correctly, but gave incorrect values for the
+      IPv6 interfaces. In particular, ::1 was not found. The effect in Exim was
+      that it would not match correctly against @[] and not recognize the IPv6
+      addresses as local.
+
+PH/02 The ipliteral router was not recognizing addresses of the form user@
+      [ipv6:....] because it didn't know about the "ipv6:" prefix.
+
+PH/03 Added disable_ipv6.
+
+PH/04 Changed $reply_address to use the raw form of the headers instead of the
+      decoded form, because it is most often used to construct To: headers
+      lines in autoreplies, and the decoded form may well be syntactically
+      invalid. However, $reply_address has leading white space removed, and all
+      newlines turned into spaces so that the autoreply transport does not
+      grumble.
+
+PH/05 If group was specified without a user on a router, and no group or user
+      was specified on a transport, the group from the router was ignored.
+
+PH/06 Increased the number of ACL variables to 20 of each type, and arranged
+      for visible compile-time settings that can be used to change these
+      numbers, for those that want even more. Backwards compatibility with old
+      spool files has been maintained. However, going back to a previous Exim
+      release will lost any variables that are in spool files.
+
+PH/07 Two small changes when running in the test harness: increase delay when
+      passing a TCP/IP connection to a new process, in case the original
+      process has to generate a bounce, and remove special handling of
+      127.0.0.2 (sic), which is no longer necessary.
+
+PH/08 Changed debug output of dbfn_open() flags from numbers to names, so as to
+      be the same on different OS.
+
+PH/09 Moved a debug statement in filter processing to avoid a race problem when
+      testing.
+
+JJ/01 exipick: fixed bug where -b (brief) output option showed "Vars:"
+      whether --show-vars was specified or not
+
+JJ/02 exipick: Added support for new ACL variable spool format introduced
+      in 4.61-PH/06
+
+PH/10 Fixed another bug related to PH/04 above: if an incoming message had a
+      syntactically invalid From: or Reply-to: line, and a filter used this to
+      generate an autoreply, and therefore failed to obtain an address for the
+      autoreply, Exim could try to deliver to a non-existent relative file
+      name, causing unrelated and misleading errors. What now happens is that
+      it logs this as a hard delivery error, but does not attempt to create a
+      bounce message.
+
+PH/11 The exinext utility has a -C option for testing purposes, but although
+      the given file was scanned by exinext itself; it wasn't being passed on
+      when Exim was called.
+
+PH/12 In the smtp transport, treat an explicit ECONNRESET error the same as
+      an end-of-file indication when reading a command response.
+
+PH/13 Domain literals for IPv6 were not recognized unless IPv6 support was
+      compiled. In many other places in Exim, IPv6 addresses are always
+      recognized, so I have changed this. It also means that IPv4 domain
+      literals of the form [IPV4:n.n.n.n] are now always recognized.
+
+PH/14 When a uid/gid is specified for the queryprogram router, it cannot be
+      used if the router is not running as root, for example, when verifying at
+      ACL time, or when using -bh. The debugging output from this situation was
+      non-existent - all you got was a failure to exec. I have made two
+      changes:
+
+      (a) Failures to set uid/gid, the current directory, or a process leader
+          in a subprocess such as that created by queryprogram now generate
+          suitable debugging ouput when -d is set.
+
+      (b) The queryprogram router detects when it is not running as root,
+          outputs suitable debugging information if -d is set, and then runs
+          the subprocess without attempting to change uid/gid.
+
+PH/15 Minor change to Makefile for building test_host (undocumented testing
+      feature).
+
+PH/16 As discussed on the list in Nov/Dec: Exim no longer looks at the
+      additional section of a DNS packet that returns MX or SRV records.
+      Instead, it always explicitly searches for A/AAAA records. This avoids
+      major problems that occur when a DNS server includes only records of one
+      type (A or AAAA) in an MX/SRV packet. A byproduct of this change has
+      fixed another bug: if SRV records were looked up and the corresponding
+      address records were *not* found in the additional section, the port
+      values from the SRV records were lost.
+
+PH/17 If a delivery to a pipe, file, or autoreply was deferred, Exim was not
+      using the correct key (the original address) when searching the retry
+      rules in order to find which one to use for generating the retry hint.
+
+PH/18 If quota_warn_message contains a From: header, Exim now refrains from
+      adding the default one. Similarly, if it contains a Reply-To: header, the
+      errors_reply_to option, if set, is not used.
+
+PH/19 When calculating a retry time, Exim used to measure the "time since
+      failure" by looking at the "first failed" field in the retry record. Now
+      it does not use this if it is later than than the arrival time of the
+      message. Instead it uses the arrival time. This makes for better
+      behaviour in cases where some deliveries succeed, thus re-setting the
+      "first failed" field. An example is a quota failure for a huge message
+      when small messages continue to be delivered. Without this change, the
+      "time since failure" will always be short, possible causing more frequent
+      delivery attempts for the huge message than are intended.
+      [Note: This change was subsequently modified - see PH/04 for 4.62.]
+
+PH/20 Added $auth1, $auth2, $auth3 to contain authentication data (as well as
+      $1, $2, $3) because the numerical variables can be reset during some
+      expansion items (e.g. "match"), thereby losing the authentication data.
+
+PH/21 Make -bV show the size of off_t variables so that the test suite can
+      decide whether to run tests for quotas > 2G.
+
+PH/22 Test the values given for quota, quota_filecount, quota_warn_threshold,
+      mailbox_size, and mailbox_filecount in the appendfile transport. If a
+      filecount value is greater than 2G or if a quota value is greater than 2G
+      on a system where the size of off_t is not greater than 4, a panic error
+      is given.
+
+PH/23 When a malformed item such as 1.2.3/24 appears in a host list, it can
+      never match. The debug and -bh output now contains an explicit error
+      message indicating a malformed IPv4 address or mask.
+
+PH/24 An host item such as 1.2.3.4/abc was being treated as the IP address
+      1.2.3.4 without a mask. Now it is not recognized as an IP address, and
+      PH/23 above applies.
+
+PH/25 Do not write to syslog when running in the test harness. The only
+      occasion when this arises is a failure to open the main or panic logs
+      (for which there is an explicit test).
+
+PH/26 Added the /no_tell option to "control=freeze".
+
+PH/27 If a host name lookup failed very early in a connection, for example, if
+      the IP address matched host_lookup and the reverse lookup yielded a name
+      that did not have a forward lookup, an error message of the form "no IP
+      address found for host xxx.xxx.xxx (during SMTP connection from NULL)"
+      could be logged. Now it outputs the IP address instead of "NULL".
+
+PH/28 An enabling patch from MH: add new function child_open_exim2() which
+      allows the sender and the authenticated sender to be set when
+      submitting a message from within Exim. Since child_open_exim() is
+      documented for local_scan(), the new function should be too.
+
+PH/29 In GnuTLS, a forced expansion failure for tls_privatekey was not being
+      ignored. In both GnuTLS and OpenSSL, an expansion of tls_privatekey that
+      results in an empty string is now treated as unset.
+
+PH/30 Fix eximon buffer overflow bug (Bugzilla #73).
+
+PH/31 Added sender_verify_fail logging option.
+
+PH/32 In November 2003, the code in Exim that added an empty Bcc: header when
+      needed by RFC 822 but not by RFC 2822 was commented out. I have now
+      tidied the source and removed it altogether.
+
+PH/33 When a queue run was abandoned because the load average was too high, a
+      log line was always written; now it is written only if the queue_run log
+      selector is set. In addition, the log line for abandonment now contains
+      information about the queue run such as the pid. This is always present
+      in "start" and "stop" lines but was omitted from the "abandon" line.
+
+PH/34 Omit spaces between a header name and the colon in the error message that
+      is given when verify = headers_syntax fails (if there are lots of them,
+      the message gets confusing).
+
+PH/35 Change the default for dns_check_names_pattern to allow slashes within
+      names, as there are now some PTR records that contain slashes. This check
+      is only to protect against broken name servers that fall over on strange
+      characters, so the fact that it applies to all lookups doesn't matter.
+
+PH/36 Now that the new test suite is complete, we can remove some of the
+      special code in Exim that was needed for the old test suite. For example,
+      sorting DNS records because real resolvers return them in an arbitrary
+      order. The new test suite's fake resolver always returns records in the
+      same order.
+
+PH/37 When running in the test harness, use -odi for submitted messages (e.g.
+      bounces) except when queue_only is set, to avoid logging races between
+      the different processes.
+
+PH/38 Panic-die if .include specifies a non-absolute path.
+
+PH/39 A tweak to the "H" retry rule from its user.
+
+JJ/03 exipick: Removed parentheses from 'next' and 'last' calls that specified
+      a label.  They prevented compilation on older perls.
+
+JJ/04 exipick: Refactored code to prevent implicit split to @_ which caused
+      a warning to be raised on newish perls.
+
+JJ/05 exipick: Fixed bug where -bpc always showed a count of all messages
+      on queue.  Changes to match documented behaviour of showing count of
+      messages matching specified criteria.
+
+PH/40 Changed the default ident timeout from 30s to 5s.
+
+PH/41 Added support for the use of login_cap features, on those BSD systems
+      that have them, for controlling the resources used by pipe deliveries.
+
+PH/42 The content-scanning code uses fopen() to create files in which to put
+      message data. Previously it was not paying any attention to the mode of
+      the files. Exim runs with umask(0) because the rest of the code creates
+      files with open(), and sets the required mode explicitly. Thus, these
+      files were ending up world-writeable. This was not a big issue, because,
+      being within the spool directory, they were not world-accessible. I have
+      created a function called modefopen, which takes an additional mode
+      argument. It sets umask(777), creates the file, chmods it to the required
+      mode, then resets the umask. All the relevant calls to fopen() in the
+      content scanning code have been changed to use this function.
+
+PH/43 If retry_interval_max is set greater than 24 hours, it is quietly reset
+      to 24 hours. This avoids potential overflow problems when processing G
+      and H retry rules. I suspect nobody ever tinkers with this value.
+
+PH/44 Added STRIP_COMMAND=/usr/bin/strip to the FreeBSD Makefile.
+
+PH/45 When the plaintext authenticator is running as a client, the server's
+      challenges are checked to ensure they are valid base64 strings. By
+      default, the authentication attempt is cancelled if an invalid string is
+      received. Setting client_ignore_invalid_base64 true ignores these errors.
+      The decoded challenge strings are now placed in $auth1, $auth2, etc. as
+      they are received. Thus, the responses can be made to depend on the
+      challenges. If an invalid string is ignored, an empty string is placed in
+      the variable.
+
+PH/46 Messages that are created by the autoreply transport now contains a
+      References: header, in accordance with RFCs 2822 and 3834.
+
+PH/47 Added authenticated_sender_force to the smtp transport.
+
+PH/48 The ${prvs expansion was broken on systems where time_t was long long.
+
+PH/49 Installed latest patch from the Sieve maintainer.
+
+PH/50 When an Exim quota was set without a file count quota, and mailbox_size
+      was also set, the appendfile transport was unnecessarily scanning a
+      directory of message files (e.g. for maildir delivery) to find the count
+      of files (along with the size), even though it did not need this
+      information. It now does the scan only if it needs to find either the
+      size of the count of files.
+
+PH/51 Added ${time_eval: to convert Exim time strings into seconds.
+
+PH/52 Two bugs concerned with error handling when the smtp transport is
+      used in LMTP mode:
+
+      (i) Exim was not creating retry information for temporary errors given
+      for individual recipients after the DATA command when the smtp transport
+      was used in LMTP mode. This meant that they could be retried too
+      frequently, and not timed out correctly.
+
+      (ii) Exim was setting the flag that allows error details to be returned
+      for LMTP errors on RCPT commands, but not for LMTP errors for individual
+      recipients that were returned after the DATA command.
+
+PH/53 This is related to PH/52, but is more general: for any failing address,
+      when detailed error information was permitted to be returned to the
+      sender, but the error was temporary, then after the final timeout, only
+      "retry timeout exceeded" was returned. Now it returns the full error as
+      well as "retry timeout exceeded".
+
+PH/54 Added control=allow_auth_unadvertised, as it seems there are clients that
+      do this, and (what is worse) MTAs that accept it.
+
+PH/55 Added the add_header modified to ACLs. The use of "message" with "warn"
+      will now be deprecated.
+
+PH/56 New os.c-cygwin from the Cygwin maintainer.
+
+JJ/06 exipick: added --unsorted option to allow unsorted output in all output
+      formats (previously only available in exim formats via -bpr, -bpru,
+      and -bpra.  Now also available in native and exiqgrep formats)
+
+JJ/07 exipick: added --freeze and --thaw options to allow faster interaction
+      with very large, slow to parse queues
+
+JJ/08 exipick: added ! as generic prefix to negate any criteria format
+
+JJ/09 exipick: miscellaneous performance enhancements (~24% improvements)
+
+PH/57 Tidies in SMTP dialogue display in debug output: (i) It was not showing
+      responses to authentication challenges, though it was showing the
+      challenges; (ii) I've removed the CR characters from the debug output for
+      SMTP output lines.
+
+PH/58 Allow for the insertion of a newline as well as a space when a string
+      is turned into more than one encoded-word during RFC 2047 encoding. The
+      Sieve code now uses this.
+
+PH/59 Added the following errors that can be detected in retry rules: mail_4xx,
+      data_4xx, lost_connection, tls_required.
+
+PH/60 When a VRFY deferred or FAILED, the log message rather than the user
+      message was being sent as an SMTP response.
+
+PH/61 Add -l and -k options to exicyclog.
+
+PH/62 When verifying, if an address was redirected to one new address, so that
+      verification continued, and the new address failed or deferred after
+      having set something in $address_data, the value of $address_data was not
+      passed back to the ACL. This was different to the case when no
+      redirection occurred. The value is now passed back in both cases.
+
+PH/63 Changed the macro HAVE_LOGIN_CAP (see PH/41 for this release above) to
+      HAVE_SETCLASSRESOURCES because there are different APIs in use that all
+      use login_cap.h, so on its own it isn't the distinguishing feature. The
+      new name refers directly to the setclassresources() function.
+
+PH/65 Added configuration files for NetBSD3.
+
+PH/66 Updated OS/Makefile-HP-UX for gcc 4.1.0 with HP-UX 11.
+
+PH/67 Fixed minor infelicity in the sorting of addresses to ensure that IPv6
+      is preferred over IPv4.
+
+PH/68 The bounce_return_message and bounce_return_body options were not being
+      honoured for bounces generated during the reception of non-SMTP messages.
+      In particular, this applied to messages rejected by the ACL. This bug has
+      been fixed. However, if bounce_return_message is true and bounce_return_
+      body is false, the headers that are returned for a non-SMTP message
+      include only those that have been read before the error was detected.
+      (In the case of an ACL rejection, they have all been read.)
+
+PH/69 The HTML version of the specification is now built in a directory called
+      spec_html instead of spec.html, because the latter looks like a path with
+      a MIME-type, and this confuses some software.
+
+PH/70 Catch two compiler warnings in sieve.c.
+
+PH/71 Fixed an obscure and subtle bug (thanks Alexander & Matthias). The
+      function verify_get_ident() calls ip_connect() to connect a socket, but
+      if the "connect()" function timed out, ip_connect() used to close the
+      socket. However, verify_get_ident() also closes the socket later, and in
+      between Exim writes to the log, which may get opened at this point. When
+      the socket was closed in ip_connect(), the log could get the same file
+      descriptor number as the socket. This naturally causes chaos. The fix is
+      not to close the socket in ip_connect(); the socket should be closed by
+      the function that creates it. There was only one place in the code where
+      this was missing, in the iplookup router, which I don't think anybody now
+      uses, but I've fixed it anyway.
+
+PH/72 Make dns_again_means_nonexist apply to lookups using gethostbyname() as
+      well as to direct DNS lookups. Otherwise the handling of names in host
+      lists is inconsistent and therefore confusing.
+
+
+Exim version 4.60
+-----------------
+
+PH/01 Two changes to the default runtime configuration:
+
+      (1) Move the checks for relay_from_hosts and authenticated clients from
+          after to before the (commented out) DNS black list checks.
+
+      (2) Add control=submission to the relay_from_hosts and authenticated
+          clients checks, on the grounds that messages accepted by these
+          statements are most likely to be submissions.
+
+PH/02 Several tidies to the handling of ${prvs and ${prvscheck:
+
+      (1) Generate an error if the third argument for the ${prvs expansion is
+          not a single digit.
+
+      (2) Treat a missing third argument of ${prvscheck as if it were an empty
+          string.
+
+      (3) Reset the variables that are obtained from the first argument of
+          ${prvscheck and used in the second argument before leaving the code,
+          because their memory is reclaimed, so using them afterwards may do
+          silly things.
+
+      (4) Tidy up the code for expanding the arguments of ${prvscheck one by
+          one (it's much easier than Tom thought :-).
+
+      (5) Because of (4), we can now allow for the use of $prvscheck_result
+          inside the third argument.
+
+PH/03 For some reason, the default setting of PATH when running a command from
+      a pipe transport was just "/usr/bin". I have changed it to
+      "/bin:/usr/bin".
+
+PH/04 SUPPORT_TRANSLATE_IP_ADDRESS and MOVE_FROZEN_MESSAGES did not cause
+      anything to be listed in the output from -bV.
+
+PH/05 When a filter generated an autoreply, the entire To: header line was
+      quoted in the delivery log line, like this:
+
+        => >A.N.Other <ano@some.domain> <original@ddress> ...
+
+      This has been changed so that it extracts the operative address. There
+      may be more than one such address. If so, they are comma-separated, like
+      this:
+
+        => >ano@some.domain,ona@other.domain <original@ddress> ...
+
+PH/06 When a client host used a correct literal IP address in a HELO or EHLO
+      command, (for example, EHLO [1.2.3.4]) and the client's IP address was
+      not being looked up in the rDNS to get a host name, Exim was showing the
+      IP address twice in Received: lines, even though the IP addresses were
+      identical. For example:
+
+        Received: from [1.2.3.4] (helo=[1.2.3.4])
+
+      However, if the real host name was known, it was omitting the HELO data
+      if it matched the actual IP address. This has been tidied up so that it
+      doesn't show the same IP address twice.
+
+PH/07 When both +timestamp and +memory debugging was on, the value given by
+      $tod_xxx expansions could be wrong, because the tod_stamp() function was
+      called by the debug printing, thereby overwriting the timestamp buffer.
+      Debugging no longer uses the tod_stamp() function when +timestamp is set.
+
+PH/08 When the original message was included in an autoreply transport, it
+      always said "this is a copy of the message, including all the headers",
+      even if body_only or headers_only was set. It now gives an appropriate
+      message.
+
+PH/09 Applied a patch from the Sieve maintainer which:
+
+      o  fixes some comments
+      o  adds the (disabled) notify extension core
+      o  adds some debug output for the result of if/elsif tests
+      o  points to the current vacation draft in the documentation
+         and documents the missing references header update
+
+      and most important:
+
+      o  fixes a bug in processing the envelope test (when testing
+         multiple envelope elements, the last element determinted the
+         result)
+
+PH/10 Exim was violating RFC 3834 ("Recommendations for Automatic Responses to
+      Electronic Mail") by including:
+
+        Auto-submitted: auto-generated
+
+      in the messages that it generates (bounce messages and others, such as
+      warnings). In the case of bounce messages for non-SMTP mesages, there was
+      also a typo: it was using "Auto_submitted" (underscore instead of
+      hyphen). Since every message generated by Exim is necessarily in response
+      to another message, thes have all been changed to:
+
+        Auto-Submitted: auto-replied
+
+      in accordance with these statements in the RFC:
+
+        The auto-replied keyword:
+
+        -  SHOULD be used on messages sent in direct response to another
+           message by an automatic process,
+
+        -  MUST NOT be used on manually-generated messages,
+
+        -  MAY be used on Delivery Status Notifications (DSNs) and Message
+           Disposition Notifications (MDNs),
+
+        -  MUST NOT be used on messages generated by automatic or periodic
+           processes, except for messages which are automatic responses to
+           other messages.
+
+PH/11 Added "${if def:sender_address {(envelope-from <$sender_address>)\n\t}}"
+      to the default Received: header definition.
+
+PH/12 Added log selector acl_warn_skipped (default on).
+
+PH/13 After a successful wildlsearch lookup, discard the values of numeric
+      variables because (a) they are in the wrong storage pool and (b) even if
+      they were copied, it wouldn't work properly because of the caching.
+
+PH/14 Add check_rfc2047_length to disable enforcement of RFC 2047 length
+      checking when decoding. Apparently there are clients that generate
+      overlong encoded strings. Why am I not surprised?
+
+PH/15 If the first argument of "${if match_address" was not empty, but did not
+      contain an "@" character, Exim crashed. Now it writes a panic log message
+      and treats the condition as false.
+
+PH/16 In autoreply, treat an empty string for "once" the same as unset.
+
+PH/17 A further patch from the Sieve maintainer: "Introduce the new Sieve
+      extension "envelope-auth".  The code is finished and in agreement with
+      other implementations, but there is no documentation so far and in fact,
+      nobody wrote the draft yet.  This extension is currently #undef'ed, thus
+      not changing the active code.
+
+      Print executed "if" and "elsif" statements when debugging is used. This
+      helps a great deal to understand what a filter does.
+
+      Document more things not specified clearly in RFC3028.  I had all this
+      sorted out, when out of a sudden new issues came to my mind.  Oops."
+
+PH/18 Exim was not recognizing the "net-" search type prefix in match_ip lists
+      (Bugzilla #53).
+
+PH/19 Exim expands the IPv6 address given to -bh to its full non-abbreviated
+      canonical form (as documented). However, after a host name lookup from
+      the IP address, check_host() was doing a simple string comparison with
+      addresses acquired from the DNS when checking that the found name did
+      have the original IP as one of its addresses. Since any found IPv6
+      addresses are likely to be in abbreviated form, the comparison could
+      fail. Luckily, there already exists a function for doing the comparison
+      by converting both addresses to binary, so now that is used instead of
+      the text comparison.
+
+PH/20 There was another similar case to PH/19, when a complete host name was
+      given in a host list; looking up its IP address could give an abbreviated
+      form, whereas the current host's name might or might not be abbreviated.
+      The same fix has been applied.
+
+
+Exim version 4.54
+-----------------
+
+PH/01 The ${base62: operator adjusted itself to base 36 when BASE_62 was
+      set to 36 (for Darwin and Cygwin), but the ${base62d: operator did not.
+      It now does.
+
+PH/02 Two minor problems detected in Cygwin: the os.{c,h} files had lost */ on
+      the CVS lines, and there was a missing #if HAVE_IPV6 in host.c.
+
+PH/03 Typo: missing ".o" in src/pcre/Makefile.
+
+PH/04 Tighten up "personal" tests: Instead of testing for any "List-"
+      header line, restrict the check to what is listed in RFCs 2369 and 2929.
+      Also, for "Auto-Submitted", treat anything other than "no" as
+      non-personal, in accordance with RFC 3834. (Previously it treated
+      anything starting "auto-" as non-personal.)
+
+TF/01 The control=submission/name=... option had a problem with syntax
+      errors if the name included a slash character. The /name= option
+      now slurps the rest of the string, so it can include any characters
+      but it must come last in the list of options (after /sender_retain
+      or /domain=).
+
+PH/05 Some modifications to the interface to the fake nameserver for the new
+      testing suite.
+
+
+
+Exim version 4.53
+-----------------
+
+TK/01 Added the "success_on_redirect" address verification option. See
+      NewStuff for rationale and an example.
+
+PH/01 Added support for SQLite, basic code supplied by David Woodhouse.
+
+PH/02 Patch to exigrep to allow it to work on syslog lines.
+
+PH/03 When creating an mbox file for a virus/spam scan, use fseek() instead of
+      fread() to skip over the body file's header line, because in Cygwin the
+      header line is locked and is inaccessible.
+
+PH/04 Added $message_exim_id, ultimately to replace $message_id (they will both
+      co-exist for some time) to make it clear that it is the Exim ID that is
+      referenced, not the Message-ID: header line.
+
+PH/05 Replaced all Tom's calls to snprintf() with calls to the internal
+      string_format() function, because snprintf() does not exist on all
+      operating systems.
+
+PH/06 The use of forbid_filter_existstest now also locks out the use of the
+      ${stat: expansion item.
+
+PH/07 Changed "SMTP protocol violation: synchronization error" into "SMTP
+      protocol synchronization error", to keep the pedants happy.
+
+PH/08 Arrange for USE_INET_NTOA_FIX to be set in config.h for AIX systems as
+      well as for IRIX systems, when gcc is being used. See the host.c source
+      file for comments.
+
+PH/09 Installed latest Cygwin configuration files from the Cygwin maintainer.
+
+PH/10 Named domain lists were not working if used in a queue_smtp_domains
+      setting.
+
+PH/11 Added support for the IGNOREQUOTA extension to LMTP, both to the lmtp
+      transport and to the smtp transport in LMTP mode.
+
+TK/02 Remove one case of BASE64 error detection FTTB (undocumented anyway).
+
+PH/12 There was a missing call to search_tidyup() before the fork() in rda.c to
+      run a filter in a subprocess. This could lead to confusion in subsequent
+      lookups in the parent process. There should also be a search_tidyup() at
+      the end of the subprocess.
+
+PH/13 Previously, if "verify = helo" was set in an ACL, the condition was true
+      only if the host matched helo_try_verify_hosts, which caused the
+      verification to occur when the EHLO/HELO command was issued. The ACL just
+      tested the remembered result. Now, if a previous verification attempt has
+      not happened, "verify = helo" does it there and then.
+
+JJ/01 exipick: added $message_exim_id variable (see 4.53-PH/04)
+
+TK/03 Fix log output including CR from clamd.
+
+PH/14 A reference to $reply_address when Reply-to: was empty and From: did not
+      exist provoked a memory error which could cause a segfault.
+
+PH/15 Installed PCRE 6.2
+
+PH/17 Defined BIND_8_COMPAT in the Darwin os.h file.
+
+PH/18 Reversed 4.52/PH/17 because the HP-UX user found it wasn't the cause
+      of the problem. Specifically, suggested +O2 rather than +O1 for the
+      HP-UX compiler.
+
+PH/19 Added sqlite_lock_timeout option (David Woodhouse's patch).
+
+PH/20 If a delivery was routed to a non-standard port by means of an SRV
+      record, the port was not correctly logged when the outgoing_port log
+      selector was set (it logged the transort's default port).
+
+PH/21 Added support for host-specific ports to manualroute, queryprogram,
+      fallback_hosts, and "hosts" in the smtp transport.
+
+PH/22 If the log selector "outgoing_port" is set, the port is now also given on
+      host errors such as "Connection refused".
+
+PH/23 Applied a patch to fix problems with exim-4.52 while doing radius
+      authentication with radiusclient 0.4.9:
+
+      - Error returned from rc_read_config was caught wrongly
+      - Username/password not passed on to radius server due to wrong length.
+
+      The presumption is that some radiusclient API changes for 4.51/PH/17
+      were not taken care of correctly. The code is still untested by me (my
+      Linux distribution still has 0.3.2 of radiusclient), but it was
+      contributed by a Radius user.
+
+PH/24 When doing a callout, the value of $domain wasn't set correctly when
+      expanding the "port" option of the smtp transport.
+
+TK/04 MIME ACL: Fix buffer underrun that occurs when EOF condition is met
+      while reading a MIME header. Thanks to Tom Hughes for a patch.
+
+PH/24 Include config.h inside local_scan.h so that configuration settings are
+      available.
+
+PH/25 Make $smtp_command_argument available after all SMTP commands. This means
+      that in an ACL for RCPT (for example), you can examine exactly what was
+      received.
+
+PH/26 Exim was recognizing IPv6 addresses of the form [IPv6:....] in EHLO
+      commands, but it was not correctly comparing the address with the actual
+      client host address. Thus, it would show the EHLO address in Received:
+      header lines when this was not necessary.
+
+PH/27 Added the % operator to ${eval:}.
+
+PH/28 Exim tries to create and chdir to its spool directory when it starts;
+      it should be ignoring failures (because with -C, for example, it has lost
+      privilege). It wasn't ignoring creation failures other than "already
+      exists".
+
+PH/29 Added "crypteq" to the list of supported features that Exim outputs when
+      -bV or -d is used.
+
+PH/30 Fixed (presumably very longstanding) bug in exim_dbmbuild: if it failed
+      because an input line was too long, either on its own, or by virtue of
+      too many continuations, the temporary file was not being removed, and the
+      return code was incorrect.
+
+PH/31 Missing "BOOL" in function definition in filtertest.c.
+
+PH/32 Applied Sieve patches from the maintainer.
+
+TK/05 Domainkeys: Accomodate for a minor API change in libdomainkeys 0.67.
+
+PH/33 Added "verify = not_blind".
+
+PH/34 There are settings for CHOWN_COMMAND and MV_COMMAND that can be used in
+      Local/Makefile (with some defaults set). These are used in built scripts
+      such as exicyclog, but they have never been used in the exim_install
+      script (though there are many overriding facilities there). I have
+      arranged that the exim_install script now takes note of these two
+      settings.
+
+PH/35 Installed configuration files for Dragonfly.
+
+PH/36 When a locally submitted message by a trusted user did not contain a
+      From: header, and the sender address was obtained from -f or from an SMTP
+      MAIL command, and the trusted user did not use -F to supply a sender
+      name, $originator_name was incorrectly used when constructing a From:
+      header. Furthermore, $originator_name was used for submission mode
+      messages from external hosts without From: headers in a similar way,
+      which is clearly wrong.
+
+PH/37 Added control=suppress_local_fixups.
+
+PH/38 When log_selector = +received_sender was set, and the addition of the
+      sender made the log line's construction buffer exactly full, or one byte
+      less than full, an overflow happened when the terminating "\n" was
+      subsequently added.
+
+PH/39 Added a new log selector, "unknown_in_list", which provokes a log entry
+      when the result of a list match is failure because a DNS lookup failed.
+
+PH/40 RM_COMMAND is now used in the building process.
+
+PH/41 Added a "distclean" target to the top-level Makefile; it deletes all
+      the "build-* directories that it finds.
+
+PH/42 (But a TF fix): In a domain list, Exim incorrectly matched @[] if the IP
+      address in a domain literal was a prefix of an interface address.
+
+PH/43 (Again a TF fix): In the dnslookup router, do not apply widen_domains
+      when verifying a sender address, unless rewrite_headers is false.
+
+PH/44 Wrote a long comment about why errors_to addresses are verified as
+      recipients, not senders.
+
+TF/01 Add missing LIBS=-lm to OS/Makefile-OpenBSD which was overlooked when
+      the ratelimit ACL was added.
+
+PH/45 Added $smtp_command for the full command (cf $smtp_command_argument).
+
+PH/46 Added extra information about PostgreSQL errors to the error string.
+
+PH/47 Added an interface to a fake DNS resolver for use by the new test suite,
+      avoiding the need to install special zones in a real server. This is
+      backwards compatible; if it can't find the fake resolver, it drops back.
+      Thus, both old and new test suites can be run.
+
+TF/02 Added util/ratelimit.pl
+
+TF/03 Minor fix to the ratelimit code to improve its behaviour in case the
+      clock is set back in time.
+
+TF/04 Fix the ratelimit support in exim_fixdb. Patch provided by Brian
+      Candler <B.Candler@pobox.com>.
+
+TF/05 The fix for PH/43 was not completely correct; widen_domains is always
+      OK for addresses that are the result of redirections.
+
+PH/48 A number of further additions for the benefit of the new test suite,
+      including a fake gethostbyname() that interfaces to the fake DNS resolver
+      (see PH/47 above).
+
+TF/06 The fix for widen_domains has also been applied to qualify_single and
+      search_parents which are the other dnslookup options that can cause
+      header rewrites.
+
+PH/49 Michael Haardt's randomized retrying, but as a separate retry parameter
+      type ("H").
+
+PH/50 Make never_users, trusted_users, admin_groups, trusted_groups expandable.
+
+TF/07 Exim produced the error message "an SRV record indicated no SMTP
+      service" if it encountered an MX record with an empty target hostname.
+      The message is now "an MX or SRV record indicated no SMTP service".
+
+TF/08 Change PH/13 introduced the possibility that verify=helo may defer,
+      if the DNS of the sending site is misconfigured. This is quite a
+      common situation. This change restores the behaviour of treating a
+      helo verification defer as a failure.
+
+PH/51 If self=fail was set on a router, the bounce message did not include the
+      actual error message.
+
+
+Exim version 4.52
+-----------------
+
+TF/01 Added support for Client SMTP Authorization. See NewStuff for details.
+
+PH/01 When a transport filter timed out in a pipe delivery, and the pipe
+      command itself ended in error, the underlying message about the transport
+      filter timeout was being overwritten with the pipe command error. Now the
+      underlying error message should be appended to the second error message.
+
+TK/01 Fix poll() being unavailable on Mac OSX 10.2.
+
+PH/02 Reduce the amount of output that "make" produces by default. Full output
+      can still be requested.
+
+PH/03 The warning log line about a condition test deferring for a "warn" verb
+      was being output only once per connection, rather than after each
+      occurrence (because it was using the same function as for successful
+      "warn" verbs). This seems wrong, so I have changed it.
+
+TF/02 Two buglets in acl.c which caused Exim to read a few bytes of memory that
+      it should not have, which might have caused a crash in the right
+      circumstances, but probably never did.
+
+PH/04 Installed a modified version of Tony Finch's patch to make submission
+      mode fix the return path as well as the Sender: header line, and to
+      add a /name= option so that you can make the user's friendly name appear
+      in the header line.
+
+TF/03 Added the control = fakedefer ACL modifier.
+
+TF/04 Added the ratelimit ACL condition. See NewStuff for details. Thanks to
+      Mark Lowes for thorough testing.
+
+TK/02 Rewrote SPF support to work with libspf2 versions >1.2.0.
+
+TK/03 Merged latest SRS patch from Miles Wilton.
+
+PH/05 There's a shambles in IRIX6 - it defines EX_OK in unistd.h which conflicts
+      with the definition in sysexits.h (which is #included earlier).
+      Fortunately, Exim does not actually use EX_OK. The code used to try to
+      preserve the sysexits.h value, by assumimg that macro definitions were
+      scanned for macro replacements. I have been disabused of this notion,
+      so now the code just undefines EX_OK before #including unistd.h.
+
+PH/06 There is a timeout for writing blocks of data, set by, e.g. data_timeout
+      in the smtp transport. When a block could not be written in a single
+      write() function, the timeout was being re-applied to each part-write.
+      This seems wrong - if the receiver was accepting one byte at a time it
+      would take for ever. The timeout is now adjusted when this happens. It
+      doesn't have to be particularly precise.
+
+TK/04 Added simple SPF lookup method in EXPERIMENTAL_SPF. See NewStuff for
+      details. Thanks to Chris Webb <chris@arachsys.com> for the patch!
+
+PH/07 Added "fullpostmaster" verify option, which does a check to <postmaster>
+      without a domain if the check to <postmaster@domain> fails.
+
+SC/01 Eximstats: added -xls and the ability to specify output files
+     (patch written by Frank Heydlauf).
+
+SC/02 Eximstats: use FileHandles for outputing results.
+
+SC/03 Eximstats: allow any combination of xls, txt, and html output.
+
+SC/04 Eximstats: fixed display of large numbers with -nvr option
+
+SC/05 Eximstats: fixed merging of reports with empty tables.
+
+SC/06 Eximstats: added the -include_original_destination flag
+
+SC/07 Eximstats: removed tabs and trailing whitespace.
+
+TK/05 Malware: Improve on aveserver error handling. Patch from Alex Miller.
+
+TK/06 MBOX spool code: Add real "From " MBOX separator line
+      so the .eml file is really in mbox format (even though
+      most programs do not really care). Patch from Alex Miller.
+
+TK/07 MBOX spool code: Add X-Envelope-From: and X-Envelope-To: headers.
+      The latter is generated from $received_to and is only set if the
+      message has one envelope recipient. SA can use these headers,
+      obviously out-of-the-box. Patch from Alex Miller.
+
+PH/08 The ${def test on a variable was returning false if the variable's
+      value was "0", contrary to what the specification has always said!
+      The result should be true unless the variable is empty.
+
+PH/09 The syntax error of a character other than { following "${if
+      def:variable_name" (after optional whitespace) was not being diagnosed.
+      An expansion such as ${if def:sender_ident:{xxx}{yyy}} in which an
+      accidental colon was present, for example, could give incorrect results.
+
+PH/10 Tidied the code in a number of places where the st_size field of a stat()
+      result is used (not including appendfile, where other changes are about
+      to be made).
+
+PH/11 Upgraded appendfile so that quotas larger than 2G are now supported.
+      This involved changing a lot of size variables from int to off_t. It
+      should work with maildirs and everything.
+
+TK/08 Apply fix provided by Michael Haardt to prevent deadlock in case of
+      spamd dying while we are connected to it.
+
+TF/05 Fixed a ${extract error message typo reported by Jeremy Harris
+      <jgh@wizmail.org>
+
+PH/12 Applied Alex Kiernan's patch for the API change for the error callback
+      function for BDB 4.3.
+
+PH/13 Changed auto_thaw such that it does not apply to bounce messages.
+
+PH/14 Imported PCRE 6.0; this was more than just a trivial operation because
+      the sources for PCRE have been re-arranged and more files are now
+      involved.
+
+PH/15 The code I had for printing potentially long long variables in PH/11
+      above was not the best (it lost precision). The length of off_t variables
+      is now inspected at build time, and an appropriate printing format (%ld
+      or %lld) is chosen and #defined by OFF_T_FMT. We also define LONGLONG_T
+      to be "long long int" or "long int". This is needed for the internal
+      formatting function string_vformat().
+
+PH/16 Applied Matthew Newton's patch to exicyclog: "If log_file_path is set in
+      the configuration file to be ":syslog", then the script "guesses" where
+      the logs files are, rather than using the compiled in default. In our
+      case the guess is not the same as the compiled default, so the script
+      suddenly stopped working when I started to use syslog. The patch checks
+      to see if log_file_path is "". If so, it attempts to read it from exim
+      with no configuration file to get the compiled in version, before it
+      falls back to the previous guessing code."
+
+TK/09 Added "prvs" and "prvscheck" expansion items. These help a lot with
+      implementing BATV in an Exim configuration. See NewStuff for the gory
+      details.
+
+PH/17 Applied Michael Haardt's patch for HP-UX, affecting only the os.h and
+      Makefile that are specific to HP-UX.
+
+PH/18 If the "use_postmaster" option was set for a recipient callout together
+      with the "random" option, the postmaster address was used as the MAIL
+      FROM address for the random test, but not for the subsequent recipient
+      test. It is now used for both.
+
+PH/19 Applied Michael Haardt's patch to update Sieve to RFC3028bis. "The
+      patch removes a few documentation additions to RFC 3028, because the
+      latest draft now contains them. It adds the new en;ascii-case comparator
+      and a new error check for 8bit text in MIME parts. Comparator and
+      require names are now matched exactly. I enabled the subaddress
+      extension, but it is not well tested yet (read: it works for me)."
+
+PH/20 Added macros for time_t as for off_t (see PH/15 above) and used them to
+      rework some of the code of TK/09 above to avoid the hardwired use of
+      "%lld" and "long long". Replaced the call to snprintf() with a call to
+      string_vformat().
+
+PH/21 Added some other messages to those in 4.51/PH/42, namely "All relevant MX
+      records point to non-existent hosts", "retry timeout exceeded", and
+      "retry time not reached for any host after a long failure period".
+
+PH/22 Fixed some oversights/typos causing bugs when Exim is compiled with
+      experimental DomainKeys support:
+
+      (1) The filter variables $n0-$n9 and $sn0-$sn9 were broken.
+      (2) On an error such as an illegally used "control", the wrong name for
+          the control was given.
+
+      These problems did NOT occur unless DomainKeys support was compiled.
+
+PH/23 Added daemon_startup_retries and daemon_startup_sleep.
+
+PH/24 Added ${if match_ip condition.
+
+PH/25 Put debug statements on either side of calls to EXIM_DBOPEN() for hints
+      databases so that it will be absolutely obvious if a crash occurs in the
+      DB library. This is a regular occurrence (often caused by mis-matched
+      db.h files).
+
+PH/26 Insert a lot of missing (void) casts for functions such as chown(),
+      chmod(), fcntl(), sscanf(), and other functions from stdio.h. These were
+      picked up on a user's system that detects such things. There doesn't seem
+      to be a gcc warning option for this - only an attribute that has to be
+      put on the function's prototype. It seems that in Fedora Core 4 they have
+      set this on a number of new functions. No doubt there will be more in due
+      course.
+
+PH/27 If a dnslookup or manualroute router is set with verify=only, it need not
+      specify a transport. However, if an address that was verified by such a
+      router was the subject of a callout, Exim crashed because it tried to
+      read the rcpt_include_affixes from the non-existent transport. Now it
+      just assumes that the setting of that option is false. This bug was
+      introduced by 4.51/PH/31.
+
+PH/28 Changed -d+all to exclude +memory, because that information is very
+      rarely of interest, but it makes the output a lot bigger. People tend to
+      do -d+all out of habit.
+
+PH/29 Removed support for the Linux-libc5 build, as it is obsolete and the
+      code in os-type was giving problems when libc.so lives in lib64, like on
+      x86_64 Fedora Core.
+
+PH/30 Exim's DNS code uses the original T_xxx names for DNS record times. These
+      aren't the modern standard, and it seems that some systems' include files
+      don't always have them. Exim was already checking for some of the newer
+      ones like T_AAAA, and defining it itself. I've added checks for all the
+      record types that Exim uses.
+
+PH/31 When using GnuTLS, if the parameters cache file did not exist, Exim was
+      not automatically generating a new one, as it is supposed to. This
+      prevented TLS from working. If the file did exist, but contained invalid
+      data, a new version was generated, as expected. It was only the case of a
+      non-existent file that was broken.
+
+TK/10 Domainkeys: Fix a bug in verification that caused a crash in conjunction
+      with a change in libdomainkeys > 0.64.
+
+TK/11 Domainkeys: Change the logic how the "testing" policy flag is retrieved
+      from DNS. If the selector record carries the flag, it now has
+      precedence over the domain-wide flag.
+
+TK/12 Cleared some compiler warnings related to SPF, SRS and DK code.
+
+PH/32 In mua_wrapper mode, if an smtp transport configuration error (such as
+      the use of a port name that isn't defined in /etc/services) occurred, the
+      message was deferred as in a normal delivery, and thus remained on the
+      spool, instead of being failed because of the mua_wrapper setting. This
+      is now fixed, and I tidied up some of the mua_wrapper messages at the
+      same time.
+
+SC/08 Eximstats: whilst parsing the mainlog(s), store information about
+      the messages in a hash of arrays rather than using individual hashes.
+      This is a bit cleaner and results in dramatic memory savings, albeit
+      at a slight CPU cost.
+
+SC/09 Eximstats: added the -show_rt<list> and the -show_dt<list> flags
+      as requested by Marc Sherman.
+
+SC/10 Eximstats: added histograms for user specified patterns as requested
+      by Marc Sherman.
+
+SC/11 Eximstats: v1.43 - bugfix for pattern histograms with -h0 specified.
+
+PH/33 Patch from the Cygwin maintainer to add "b" to all occurences of
+      fopen() in the content-scanning modules that did not already have it.
+
+
+Exim version 4.51
+-----------------
+
+TK/01 Added Yahoo DomainKeys support via libdomainkeys. See
+      doc/experimental-spec.txt for details. (http://domainkeys.sf.net)
+
+TK/02 Fix ACL "control" statement not being available in MIME ACL.
+
+TK/03 Fix ACL "regex" condition not being available in MIME ACL.
+
+PH/01 Installed a patch from the Sieve maintainer that allows -bf to be used
+      to test Sieve filters that use "vacation".
+
+PH/02 Installed a slightly modified version of Nikos Mavrogiannopoulos' patch
+      that changes the way the GnuTLS parameters are stored in the cache file.
+      The new format can be generated externally. For backward compatibility,
+      if the data in the cache doesn't make sense, Exim assumes it has read an
+      old-format file, and it generates new data and writes a new file. This
+      means that you can't go back to an older release without removing the
+      file.
+
+PH/03 A redirect router that has both "unseen" and "one_time" set does not
+      work if there are any delivery delays because "one_time" forces the
+      parent to be marked "delivered", so its unseen clone is never tried
+      again. For this reason, Exim now forbids the simultaneous setting of
+      these two options.
+
+PH/04 Change 4.11/85 fixed an obscure bug concerned with addresses that are
+      redirected to themselves ("homonym" addresses). Read the long ChangeLog
+      entry if you want to know the details. The fix, however, neglected to
+      consider the case when local delivery batching is involved. The test for
+      "previously delivered" was not happening when checking to see if an
+      address could be batched with a previous (undelivered) one; under
+      certain circumstances this could lead to multiple deliveries to the same
+      address.
+
+PH/05 Renamed the macro SOCKLEN_T as EXIM_SOCKLEN_T because AIX uses SOCKLEN_T
+      in its include files, and this causes problems building Exim.
+
+PH/06 A number of "verify =" ACL conditions have no options (e.g. verify =
+      header_syntax) but Exim was just ignoring anything given after a slash.
+      In particular, this caused confusion with an attempt to use "verify =
+      reverse_host_lookup/defer_ok". An error is now given when options are
+      supplied for verify items that do not have them. (Maybe reverse_host_
+      lookup should have a defer_ok option, but that's a different point.)
+
+PH/07 Increase the size of the buffer for incoming SMTP commands from 512 (as
+      defined by RFC 821) to 2048, because there were problems with some AUTH
+      commands, and RFC 1869 says the size should be increased for extended
+      SMTP commands that take arguments.
+
+PH/08 Added ${dlfunc dynamically loaded function for expansion (code from Tony
+      Finch).
+
+PH/09 Previously, an attempt to use ${perl when it wasn't compiled gave an
+      "unknown" error; now it says that the functionality isn't in the binary.
+
+PH/10 Added a nasty fudge to try to recognize and flatten LDAP passwords in
+      an address' error message when a string expansion fails (syntax or
+      whatever). Otherwise the password may appear in the log. Following change
+      PH/42 below, there is no longer a chance of it appearing in a bounce
+      message.
+
+PH/11 Installed exipick version 20050225.0 from John Jetmore.
+
+PH/12 If the last host in a fallback_hosts list was multihomed, only the first
+      of its addresses was ever tried. (Bugzilla bug #2.)
+
+PH/13 If "headers_add" in a transport didn't end in a newline, Exim printed
+      the result incorrectly in the debug output. (It correctly added a newline
+      to what was transported.)
+
+TF/01 Added $received_time.
+
+PH/14 Modified the default configuration to add an acl_smtp_data ACL, with
+      commented out examples of how to interface to a virus scanner and to
+      SpamAssassin. Also added commented examples of av_scanner and
+      spamd_address settings.
+
+PH/15 Further to TK/02 and TK/03 above, tidied up the tables of what conditions
+      and controls are allowed in which ACLs. There were a couple of minor
+      errors. Some of the entries in the conditions table (which is a table of
+      where they are NOT allowed) were getting very unwieldy; rewrote them as a
+      negation of where the condition IS allowed.
+
+PH/16 Installed updated OS/os.c-cygwin from the Cygwin maintainer.
+
+PH/17 The API for radiusclient changed at release 0.4.0. Unfortunately, the
+      header file does not have a version number, so I've had to invent a new
+      value for RADIUS_LIB_TYPE, namely "RADIUSCLIENTNEW" to request the new
+      API. The code is untested by me (my Linux distribution still has 0.3.2 of
+      radiusclient), but it was contributed by a Radius user.
+
+PH/18 Installed Lars Mainka's patch for the support of CRL collections in
+      files or directories, for OpenSSL.
+
+PH/19 When an Exim process that is running as root has to create an Exim log
+      file, it does so in a subprocess that runs as exim:exim so as to get the
+      ownership right at creation (otherwise, other Exim processes might see
+      the file with the wrong ownership). There was no test for failure of this
+      fork() call, which would lead to the process getting stuck as it waited
+      for a non-existent subprocess. Forks do occasionally fail when resources
+      run out. I reviewed all the other calls to fork(); they all seem to check
+      for failure.
+
+PH/20 When checking for unexpected SMTP input at connect time (before writing
+      the banner), Exim was not dealing correctly with a non-positive return
+      from the read() function. If the client had disconnected by this time,
+      the result was a log entry for a synchronization error with an empty
+      string after "input=" when read() returned zero. If read() returned -1
+      (an event I could not check), uninitialized data bytes were printed.
+      There were reports of junk text (parts of files, etc) appearing after
+      "input=".
+
+PH/21 Added acl_not_smtp_mime to allow for MIME scanning for non-SMTP messages.
+
+PH/22 Added support for macro redefinition, and (re)definition in between
+      driver and ACL definitions.
+
+PH/23 The cyrus_sasl authenticator was expanding server_hostname, but then
+      forgetting to use the resulting value; it was using the unexpanded value.
+
+PH/24 The cyrus_sasl authenticator was advertising mechanisms for which it
+      hadn't been configured. The fix is from Juergen Kreileder, who
+      understands it better than I do:
+
+      "Here's what I see happening with three configured cyrus_sasl
+      authenticators configured (plain, login, cram-md5):
+
+      On startup auth_cyrus_sasl_init() gets called for each of these.
+      This means three calls to sasl_listmech() without a specified mech_list.
+      => SASL tests which mechs of all available mechs actually work
+      => three warnings about OTP not working
+      => the returned list contains: plain, login, cram-md5, digest-md5, ...
+
+      With the patch, sasl_listmech() also gets called three times.  But now
+      SASL's mech_list option is set to the server_mech specified in the the
+      authenticator.  Or in other words, the answer from sasl_listmech()
+      gets limited to just the mech you're testing for (which is different
+      for each call.)
+      => the return list contains just 'plain' or 'login', 'cram-md5' or
+      nothing depending on the value of ob->server_mech.
+
+      I've just tested the patch: Authentication still works fine,
+      unavailable mechs specified in the exim configuration are still
+      caught, and the auth.log warnings about OTP are gone."
+
+PH/25 When debugging is enabled, the contents of the command line are added
+      to the debugging output, even when log_selector=+arguments is not
+      specified.
+
+PH/26 Change scripts/os-type so that when "uname -s" returns just "GNU", the
+      answer is "GNU", and only if the return is "GNU/something" is the answer
+      "Linux".
+
+PH/27 $acl_verify_message is now set immediately after the failure of a
+      verification in an ACL, and so is available in subsequent modifiers. In
+      particular, the message can be preserved by coding like this:
+
+         warn  !verify = sender
+               set acl_m0 = $acl_verify_message
+
+      Previously, $acl_verify_message was set only while expanding "message"
+      and "log_message" when a very denied access.
+
+PH/28 Modified OS/os.c-Linux with
+
+        -#ifndef OS_LOAD_AVERAGE
+        +#if !defined(OS_LOAD_AVERAGE) && defined(__linux__)
+
+      to make Exim compile on kfreebsd-gnu. (I'm totally confused about the
+      nomenclature these days.)
+
+PH/29 Installed patch from the Sieve maintainer that adds the options
+      sieve_useraddress and sieve_subaddress to the redirect router.
+
+PH/30 In these circumstances:
+        . Two addresses routed to the same list of hosts;
+        . First host does not offer TLS;
+        . First host accepts first address;
+        . First host gives temporary error to second address;
+        . Second host offers TLS and a TLS session is established;
+        . Second host accepts second address.
+      Exim incorrectly logged both deliveries with the TLS parameters (cipher
+      and peerdn, if requested) that were in fact used only for the second
+      address.
+
+PH/31 When doing a callout as part of verifying an address, Exim was not paying
+      attention to any local part prefix or suffix that was matched by the
+      router that accepted the address. It now behaves in the same way as it
+      does for delivery: the affixes are removed from the local part unless
+      rcpt_include_affixes is set on the transport.
+
+PH/32 Add the sender address, as F=<...>, to the log line when logging a
+      timeout during the DATA phase of an incoming message.
+
+PH/33 Sieve envelope tests were broken for match types other than :is. I have
+      applied a patch sanctioned by the Sieve maintainer.
+
+PH/34 Change 4.50/80 broke Exim in that it could no longer handle cases where
+      the uid or gid is negative. A case of a negative gid caused this to be
+      noticed. The fix allows for either to be negative.
+
+PH/35 ACL_WHERE_MIME is now declared unconditionally, to avoid too much code
+      clutter, but the tables that are indexed by ACL_WHERE_xxx values had been
+      overlooked.
+
+PH/36 The change PH/12 above was broken. Fixed it.
+
+PH/37 Exim used to check for duplicate addresses in the middle of routing, on
+      the grounds that routing the same address twice would always produce the
+      same answer. This might have been true once, but it is certainly no
+      longer true now. Routing a child address may depend on the previous
+      routing that produced that child. Some complicated redirection strategies
+      went wrong when messages had multiple recipients, and made Exim's
+      behaviour dependent on the order in which the addresses were given.
+
+      I have moved the duplicate checking until after the routing is complete.
+      Exim scans the addresses that are assigned to local and remote
+      transports, and removes any duplicates. This means that more work will be
+      done, as duplicates will always all be routed, but duplicates are
+      presumably rare, so I don't expect this is of any significance.
+
+      For deliveries to pipes, files, and autoreplies, the duplicate checking
+      still happens during the routing process, since they are not going to be
+      routed further.
+
+PH/38 Installed a patch from Ian Freislich, with the agreement of Tom Kistner.
+      It corrects a timeout issue with spamd. This is Ian's comment: "The
+      background is that sometimes spamd either never reads data from a
+      connection it has accepted, or it never writes response data. The exiscan
+      spam.[ch] uses a 3600 second timeout on spamd socket reads, further, it
+      blindly assumes that writes won't block so it may never time out."
+
+PH/39 Allow G after quota size as well as K and M.
+
+PH/40 The value set for $authenticated_id in an authenticator may not contain
+      binary zeroes or newlines because the value is written to log lines and
+      to spool files. There was no check on this. Now the value is run through
+      the string_printing() function so that such characters are converted to
+      printable escape sequences.
+
+PH/41 $message_linecount is a new variable that contains the total number of
+      lines in the message. Compare $body_linecount, which is the count for the
+      body only.
+
+PH/42 Exim no longer gives details of delivery errors for specific addresses in
+      bounce and delay warning messages, except in certain special cases, which
+      are as follows:
+
+      (a) An SMTP error message from a remote host;
+      (b) A message specified in a :fail: redirection;
+      (c) A message specified in a "fail" command in a system filter;
+      (d) A message specified in a FAIL return from the queryprogram router;
+      (e) A message specified by the cannot_route_message router option.
+
+      In these cases only, Exim does include the error details in bounce and
+      warning messages. There are also a few cases where bland messages such
+      as "unrouteable address" or "local delivery error" are given.
+
+PH/43 $value is now also set for the "else" part of a ${run expansion.
+
+PH/44 Applied patch from the Sieve maintainer: "The vacation draft is still
+      being worked on, but at least Exim now implements the latest version to
+      play with."
+
+PH/45 In a pipe transport, although a timeout while waiting for the pipe
+      process to complete was treated as a delivery failure, a timeout while
+      writing the message to the pipe was logged, but erroneously treated as a
+      successful delivery. Such timeouts include transport filter timeouts. For
+      consistency with the overall process timeout, these timeouts are now
+      treated as errors, giving rise to delivery failures by default. However,
+      there is now a new Boolean option for the pipe transport called
+      timeout_defer, which, if set TRUE, converts the failures into defers for
+      both kinds of timeout. A transport filter timeout is now identified in
+      the log output.
+
+PH/46 The "scripts/Configure-config.h" script calls "make" at one point. On
+      systems where "make" and "gmake" are different, calling "gmake" at top
+      level broke things. I've arranged for the value of $(MAKE) to be passed
+      from the Makefile to this script so that it can call the same version of
+      "make".
+
+
+A note about Exim versions 4.44 and 4.50
+----------------------------------------
+
+Exim 4.50 was meant to be the next release after 4.43. It contains a lot of
+changes of various kinds. As a consequence, a big documentation update was
+needed. This delayed the release for rather longer than seemed good, especially
+in the light of a couple of (minor) security issues. Therefore, the changes
+that fixed bugs were backported into 4.43, to create a 4.44 maintenance
+release. So 4.44 and 4.50 are in effect two different branches that both start
+from 4.43.
+
+I have left the 4.50 change log unchanged; it contains all the changes since
+4.43. The change log for 4.44 is below; many of its items are identical to
+those for 4.50. This seems to be the most sensible way to preserve the
+historical information.
+
+
+Exim version 4.50
+-----------------
+
+ 1. Minor wording change to the doc/README.SIEVE file.
+
+ 2. Change 4.43/35 introduced a bug: if quota_filecount was set, the
+    computation of the current number of files was incorrect.
+
+ 3. Closing a stable door: arrange to panic-die if setitimer() ever fails. The
+    bug fixed in 4.43/37 would have been diagnosed quickly if this had been in
+    place.
+
+ 4. Give more explanation in the error message when the command for a transport
+    filter fails to execute.
+
+ 5. There are several places where Exim runs a non-Exim command in a
+    subprocess. The SIGUSR1 signal should be disabled for these processes. This
+    was being done only for the command run by the queryprogram router. It is
+    now done for all such subprocesses. The other cases are: ${run, transport
+    filters, and the commands run by the lmtp and pipe transports.
+
+ 6. Added CONFIGURE_GROUP build-time option.
+
+ 7. Some older OS have a limit of 256 on the maximum number of file
+    descriptors. Exim was using setrlimit() to set 1000 as a large value
+    unlikely to be exceeded. Change 4.43/17 caused a lot of logging on these
+    systems. I've change it so that if it can't get 1000, it tries for 256.
+
+ 8. "control=submission" was allowed, but had no effect, in a DATA ACL. This
+    was an oversight, and furthermore, ever since the addition of extra
+    controls (e.g. 4.43/32), the checks on when to allow different forms of
+    "control" were broken. There should now be diagnostics for all cases when a
+    control that does not make sense is encountered.
+
+ 9. Added the /retain_sender option to "control=submission".
+
+10. $recipients is now available in the predata ACL (oversight).
+
+11. Tidy the search cache before the fork to do a delivery from a message
+    received from the command line. Otherwise the child will trigger a lookup
+    failure and thereby defer the delivery if it tries to use (for example) a
+    cached ldap connection that the parent has called unbind on.
+
+12. If verify=recipient was followed by verify=sender in a RCPT ACL, the value
+    of $address_data from the recipient verification was clobbered by the
+    sender verification.
+
+13. The value of address_data from a sender verification is now available in
+    $sender_address_data in subsequent conditions in the ACL statement.
+
+14. Added forbid_sieve_filter and forbid_exim_filter to the redirect router.
+
+15. Added a new option "connect=<time>" to callout options, to set a different
+    connection timeout.
+
+16. If FIXED_NEVER_USERS was defined, but empty, Exim was assuming the uid 0
+    was its contents. (It was OK if the option was not defined at all.)
+
+17. A "Completed" log line is now written for messages that are removed from
+    the spool by the -Mrm option.
+
+18. New variables $sender_verify_failure and $recipient_verify_failure contain
+    information about exactly what failed.
+
+19. Added -dd to debug only the daemon process.
+
+20. Incorporated Michael Haardt's patch to ldap.c for improving the way it
+    handles timeouts, both on the server side and network timeouts. Renamed the
+    CONNECT parameter as NETTIMEOUT (but kept the old name for compatibility).
+
+21. The rare case of EHLO->STARTTLS->HELO was setting the protocol to "smtp".
+    It is now set to "smtps".
+
+22. $host_address is now set to the target address during the checking of
+    ignore_target_hosts.
+
+23. When checking ignore_target_hosts for an ipliteral router, no host name was
+    being passed; this would have caused $sender_host_name to have been used if
+    matching the list had actually called for a host name (not very likely,
+    since this list is usually IP addresses). A host name is now passed as
+    "[x.x.x.x]".
+
+24. Changed the calls that set up the SIGCHLD handler in the daemon to use the
+    code that specifies a non-restarting handler (typically sigaction() in
+    modern systems) in an attempt to fix a rare and obscure crash bug.
+
+25. Narrowed the window for a race in the daemon that could cause it to ignore
+    SIGCHLD signals. This is not a major problem, because they are used only to
+    wake it up if nothing else does.
+
+26. A malformed maildirsize file could cause Exim to calculate negative values
+    for the mailbox size or file count. Odd effects could occur as a result.
+    The maildirsize information is now recalculated if the size or filecount
+    end up negative.
+
+27. Added HAVE_SYS_STATVFS_H to the os.h file for Linux, as it has had this
+    support for a long time. Removed HAVE_SYS_VFS_H.
+
+28. Installed the latest version of exipick from John Jetmore.
+
+29. In an address list, if the pattern was not a regular expression, an empty
+    subject address (from a bounce message) matched only if the pattern was an
+    empty string. Non-empty patterns were not even tested. This was the wrong
+    because it is perfectly reasonable to use an empty address as part of a
+    database query. An empty address is now tested by patterns that are
+    lookups. However, all the other forms of pattern expect the subject to
+    contain a local part and a domain, and therefore, for them, an empty
+    address still always fails if the pattern is not itself empty.
+
+30. Exim went into a mad DNS loop when attempting to do a callout where the
+    host was specified on an smtp transport, and looking it up yielded more
+    than one IP address.
+
+31. Re-factored the code for checking spool and log partition space into a
+    function that finds that data and another that does the check. The former
+    is then used to implement four new variables: $spool_space, $log_space,
+    $spool_inodes, and $log_inodes.
+
+32. The RFC2047 encoding function was originally intended for short strings
+    such as real names; it was not keeping to the 75-character limit for
+    encoded words that the RFC imposes. It now respects the limit, and
+    generates multiple encoded words if necessary. To be on the safe side, I
+    have increased the buffer size for the ${rfc2047: expansion operator from
+    1024 to 2048 bytes.
+
+33. It is now permitted to omit both strings after an "if" condition; if the
+    condition is true, the result is "true". As before, when the second string
+    is omitted, a false condition yields an empty string. This makes it less
+    cumbersome to write custom ACL and router conditions.
+
+34. Failure to deliver a bounce message always caused it to be frozen, even if
+    there was an errors_to setting on the router. The errors_to setting is now
+    respected.
+
+35. If an IPv6 address is given for -bh or -bhc, it is now converted to the
+    canonical form (fully expanded) before being placed in
+    $sender_host_address.
+
+36. The table in the code that translates DNS record types into text (T_A to
+    "A" for instance) was missing entries for NS and CNAME. It is just possible
+    that this could have caused confusion if both these types were looked up
+    for the same domain, because the text type is used as part of Exim's
+    per-process caching. But the chance of anyone hitting this buglet seems
+    very small.
+
+37. The dnsdb lookup has been extended in a number of ways.
+
+    (1) There is a new type, "zns", which walks up the domain tree until it
+        finds some nameserver records. It should be used with care.
+
+    (2) There is a new type, "mxh", which is like "mx" except that it returns
+        just the host names, not the priorities.
+
+    (3) It is now possible to give a list of domains (or IP addresses) to be
+        looked up. The behaviour when one of the lookups defers can be
+        controlled by a keyword.
+
+    (4) It is now possible to specify the separator character for use when
+        multiple records are returned.
+
+38. The dnslists ACL condition has been extended: it is now possible to supply
+    a list of IP addresses and/or domains to be looked up in a particular DNS
+    domain.
+
+39. Added log_selector=+queue_time_overall.
+
+40. When running the queue in the test harness, wait just a tad after forking a
+    delivery process, to get repeatability of debugging output.
+
+41. Include certificate and key file names in error message when GnuTLS fails
+    to set them up, because the GnuTLS error message doesn't include the name
+    of the failing file when there is a problem reading it.
+
+42. Allow both -bf and -bF in the same test run.
+
+43. Did the same fix as 41 above for OpenSSL, which had the same infelicity.
+
+44. The "Exiscan patch" is now merged into the mainline Exim source.
+
+45. Sometimes the final signoff response after QUIT could fail to get
+    transmitted in the non-TLS case. Testing !tls_active instead of tls_active
+    < 0 before doing a fflush(). This bug looks as though it goes back to the
+    introduction of TLS in release 3.20, but "sometimes" must have been rare
+    because the tests only now provoked it.
+
+46. Reset the locale to "C" after calling embedded Perl, in case it was changed
+    (this can affect the format of dates).
+
+47. exim_tidydb, when checking for the continued existence of a message for
+    which it has found a message-specific retry record, was not finding
+    messages that were in split spool directories. Consequently, it was
+    deleting retry records that should have stayed in existence.
+
+48. Steve fixed some bugs in eximstats.
+
+49. The SPA authentication driver was not abandoning authentication and moving
+    on to the next authenticator when an expansion was forced to fail,
+    contradicting the general specification for all authenticators. Instead it
+    was generating a temporary error. It now behaves as specified.
+
+50. The default ordering of permitted cipher suites for GnuTLS was pessimal
+    (the order specifies the preference for clients). The order is now AES256,
+    AES128, 3DES, ARCFOUR128.
+
+51. Small patch to Sieve code - explicitly set From: when generating an
+    autoreply.
+
+52. Exim crashed if a remote delivery caused a very long error message to be
+    recorded - for instance if somebody sent an entire SpamAssassin report back
+    as a large number of 550 error lines. This bug was coincidentally fixed by
+    increasing the size of one of Exim's internal buffers (big_buffer) that
+    happened as part of the Exiscan merge. However, to be on the safe side, I
+    have made the code more robust (and fixed the comments that describe what
+    is going on).
+
+53. Now that there can be additional text after "Completed" in log lines (if
+    the queue_time_overall log selector is set), a one-byte patch to exigrep
+    was needed to allow it to recognize "Completed" as not the last thing in
+    the line.
+
+54. The LDAP lookup was not handling a return of LDAP_RES_SEARCH_REFERENCE. A
+    patch that reportedly fixes this has been added. I am not expert enough to
+    create a test for it. This is what the patch creator wrote:
+
+      "I found a little strange behaviour of ldap code when working with
+      Windows 2003 AD Domain, where users was placed in more than one
+      Organization Units. When I tried to give exim partial DN, the exit code
+      of ldap_search was unknown to exim because of LDAP_RES_SEARCH_REFERENCE.
+      But simultaneously result of request was absolutely normal ldap result,
+      so I produce this patch..."
+
+    Later: it seems that not all versions of LDAP support LDAP_RES_SEARCH_
+    REFERENCE, so I have modified the code to exclude the patch when that macro
+    is not defined.
+
+55. Some experimental protocols are using DNS PTR records for new purposes. The
+    keys for these records are domain names, not reversed IP addresses. The
+    dnsdb PTR lookup now tests whether its key is an IP address. If not, it
+    leaves it alone. Component reversal etc. now happens only for IP addresses.
+    CAN-2005-0021
+
+56. Improve error message when ldap_search() fails in OpenLDAP or Solaris LDAP.
+
+57. Double the size of the debug message buffer (to 2048) so that more of very
+    long debug lines gets shown.
+
+58. The exicyclog utility now does better if the number of log files to keep
+    exceeds 99. In this case, it numbers them 001, 002 ... instead of 01, 02...
+
+59. Two changes related to the smtp_active_hostname option:
+
+      (1) $smtp_active_hostname is now available as a variable.
+      (2) The default for smtp_banner uses $smtp_active_hostname instead
+          of $primary_hostname.
+
+60. The host_aton() function is supposed to be passed a string that is known
+    to be a valid IP address. However, in the case of IPv6 addresses, it was
+    not checking this. This is a hostage to fortune. Exim now panics and dies
+    if the condition is not met. A case was found where this could be provoked
+    from a dnsdb PTR lookup with an IPv6 address that had more than 8
+    components; fortuitously, this particular loophole had already been fixed
+    by change 4.50/55 above.
+
+    If there are any other similar loopholes, the new check in host_aton()
+    itself should stop them being exploited. The report I received stated that
+    data on the command line could provoke the exploit when Exim was running as
+    exim, but did not say which command line option was involved. All I could
+    find was the use of -be with a bad dnsdb PTR lookup, and in that case it is
+    running as the user.
+    CAN-2005-0021
+
+61. There was a buffer overflow vulnerability in the SPA authentication code
+    (which came originally from the Samba project). I have added a test to the
+    spa_base64_to_bits() function which I hope fixes it.
+    CAN-2005-0022
+
+62. Configuration update for GNU/Hurd and variations. Updated Makefile-GNU and
+    os.h-GNU, and added configuration files for GNUkFreeBSD and GNUkNetBSD.
+
+63. The daemon start-up calls getloadavg() while still root for those OS that
+    need the first call to be done as root, but it missed one case: when
+    deliver_queue_load_max is set with deliver_drop_privilege. This is
+    necessary for the benefit of the queue runner, because there is no re-exec
+    when deliver_drop_privilege is set.
+
+64. A call to exiwhat cut short delays set up by "delay" modifiers in ACLs.
+    This has been fixed.
+
+65. Caching of lookup data for "hosts =" ACL conditions, when a named host list
+    was in use, was not putting the data itself into the right store pool;
+    consequently, it could be overwritten for a subsequent message in the same
+    SMTP connection. (Fix 4.40/11 dealt with the non-cache case, but overlooked
+    the caching.)
+
+66. Added hosts_max_try_hardlimit to the smtp transport, default 50.
+
+67. The string_is_ip_address() function returns 0, 4, or 6, for "no an IP
+    address", "IPv4 address", and "IPv6 address", respectively. Some calls of
+    the function were treating the return as a boolean value, which happened to
+    work because 0=false and not-0=true, but is not correct code.
+
+68. The host_aton() function was not handling scoped IPv6 addresses (those
+    with, for example, "%eth0" on the end) correctly.
+
+69. Fixed some compiler warnings in acl.c for the bitmaps specified with
+    negated items (that is, ~something) in unsigned ints. Some compilers
+    apparently mutter when there is no cast.
+
+70. If an address verification called from an ACL failed, and did not produce a
+    user-specific message (i.e. there was only a "system" message), nothing was
+    put in $acl_verify_message. In this situation, it now puts the system
+    message there.
+
+71. Change 4.23/11 added synchronization checking at the start of an SMTP
+    session; change 4.31/43 added the unwanted input to the log line - except
+    that it did not do this in the start of session case. It now does.
+
+72. After a timeout in a callout SMTP session, Exim still sent a QUIT command.
+    This is wrong and can cause the other end to generate a synchronization
+    error if it is another Exim or anything else that does the synchronization
+    check. A QUIT command is no longer sent after a timeout.
+
+73. $host_lookup_deferred has been added, to make it easier to detect DEFERs
+    during host lookups.
+
+74. The defer_ok option of callout verification was not working if it was used
+    when verifying addresses in header lines, that is, for this case:
+
+      verify = header_sender/callout=defer_ok
+
+75. A backgrounded daemon closed stdin/stdout/stderr on entry; this meant that
+    those file descriptors could be used for SMTP connections. If anything
+    wrote to stderr (the example that came up was "warn" in embedded Perl), it
+    could be sent to the SMTP client, causing chaos. The daemon now opens
+    stdin, stdout, and stderr to /dev/null when it puts itself into the
+    background.
+
+76. Arrange for output from Perl's "warn" command to be written to Exim's main
+    log by default. The user can override this with suitable Perl magic.
+
+77. The use of log_message on a "discard" ACL verb, which is supposed to add to
+    the log message when discard triggers, was not working for the DATA ACL or
+    for the non-SMTP ACL.
+
+78. Error message wording change in sieve.c.
+
+79. If smtp_accept_max_per_host was set, the number of connections could be
+    restricted to fewer than expected, because the daemon was trying to set up
+    a new connection before checking whether the processes handling previous
+    connections had finished. The check for completed processes is now done
+    earlier. On busy systems, this bug wouldn't be noticed because something
+    else would have woken the daemon, and it would have reaped the completed
+    process earlier.
+
+80. If a message was submitted locally by a user whose login name contained one
+    or more spaces (ugh!), the spool file that Exim wrote was not re-readable.
+    It caused a spool format error. I have fixed the spool reading code. A
+    related problem was that the "from" clause in the Received: line became
+    illegal because of the space(s). It is now covered by ${quote_local_part.
+
+81. Included the latest eximstats from Steve (adds average sizes to HTML Top
+    tables).
+
+82. Updated OS/Makefile-AIX as per message from Mike Meredith.
+
+83. Patch from Sieve maintainer to fix unterminated string problem in
+    "vacation" handling.
+
+84. Some minor changes to the Linux configuration files to help with other
+    OS variants using glibc.
+
+85. One more patch for Sieve to update vacation handling to latest spec.
+
+
+----------------------------------------------------
+See the note above about the 4.44 and 4.50 releases.
+----------------------------------------------------
+
+
+Exim version 4.44
+-----------------
+
+ 1. Change 4.43/35 introduced a bug that caused file counts to be
+    incorrectly computed when quota_filecount was set in an appendfile
+    transport
+
+ 2. Closing a stable door: arrange to panic-die if setitimer() ever fails. The
+    bug fixed in 4.43/37 would have been diagnosed quickly if this had been in
+    place.
+
+ 3. Give more explanation in the error message when the command for a transport
+    filter fails to execute.
+
+ 4. There are several places where Exim runs a non-Exim command in a
+    subprocess. The SIGUSR1 signal should be disabled for these processes. This
+    was being done only for the command run by the queryprogram router. It is
+    now done for all such subprocesses. The other cases are: ${run, transport
+    filters, and the commands run by the lmtp and pipe transports.
+
+ 5. Some older OS have a limit of 256 on the maximum number of file
+    descriptors. Exim was using setrlimit() to set 1000 as a large value
+    unlikely to be exceeded. Change 4.43/17 caused a lot of logging on these
+    systems. I've change it so that if it can't get 1000, it tries for 256.
+
+ 6. "control=submission" was allowed, but had no effect, in a DATA ACL. This
+    was an oversight, and furthermore, ever since the addition of extra
+    controls (e.g. 4.43/32), the checks on when to allow different forms of
+    "control" were broken. There should now be diagnostics for all cases when a
+    control that does not make sense is encountered.
+
+ 7. $recipients is now available in the predata ACL (oversight).
+
+ 8. Tidy the search cache before the fork to do a delivery from a message
+    received from the command line. Otherwise the child will trigger a lookup
+    failure and thereby defer the delivery if it tries to use (for example) a
+    cached ldap connection that the parent has called unbind on.
+
+ 9. If verify=recipient was followed by verify=sender in a RCPT ACL, the value
+    of $address_data from the recipient verification was clobbered by the
+    sender verification.
+
+10. If FIXED_NEVER_USERS was defined, but empty, Exim was assuming the uid 0
+    was its contents. (It was OK if the option was not defined at all.)
+
+11. A "Completed" log line is now written for messages that are removed from
+    the spool by the -Mrm option.
+
+12. $host_address is now set to the target address during the checking of
+    ignore_target_hosts.
+
+13. When checking ignore_target_hosts for an ipliteral router, no host name was
+    being passed; this would have caused $sender_host_name to have been used if
+    matching the list had actually called for a host name (not very likely,
+    since this list is usually IP addresses). A host name is now passed as
+    "[x.x.x.x]".
+
+14. Changed the calls that set up the SIGCHLD handler in the daemon to use the
+    code that specifies a non-restarting handler (typically sigaction() in
+    modern systems) in an attempt to fix a rare and obscure crash bug.
+
+15. Narrowed the window for a race in the daemon that could cause it to ignore
+    SIGCHLD signals. This is not a major problem, because they are used only to
+    wake it up if nothing else does.
+
+16. A malformed maildirsize file could cause Exim to calculate negative values
+    for the mailbox size or file count. Odd effects could occur as a result.
+    The maildirsize information is now recalculated if the size or filecount
+    end up negative.
+
+17. Added HAVE_SYS_STATVFS_H to the os.h file for Linux, as it has had this
+    support for a long time. Removed HAVE_SYS_VFS_H.
+
+18. Updated exipick to current release from John Jetmore.
+
+19. Allow an empty sender to be matched against a lookup in an address list.
+    Previously the only cases considered were a regular expression, or an
+    empty pattern.
+
+20. Exim went into a mad DNS lookup loop when doing a callout where the
+    host was specified on the transport, if the DNS lookup yielded more than
+    one IP address.
+
+21. The RFC2047 encoding function was originally intended for short strings
+    such as real names; it was not keeping to the 75-character limit for
+    encoded words that the RFC imposes. It now respects the limit, and
+    generates multiple encoded words if necessary. To be on the safe side, I
+    have increased the buffer size for the ${rfc2047: expansion operator from
+    1024 to 2048 bytes.
+
+22. Failure to deliver a bounce message always caused it to be frozen, even if
+    there was an errors_to setting on the router. The errors_to setting is now
+    respected.
+
+23. If an IPv6 address is given for -bh or -bhc, it is now converted to the
+    canonical form (fully expanded) before being placed in
+    $sender_host_address.
+
+24. Updated eximstats to version 1.33
+
+25. Include certificate and key file names in error message when GnuTLS fails
+    to set them up, because the GnuTLS error message doesn't include the name
+    of the failing file when there is a problem reading it.
+
+26. Expand error message when OpenSSL has problems setting up cert/key files.
+    As per change 25.
+
+27. Reset the locale to "C" after calling embedded Perl, in case it was changed
+    (this can affect the format of dates).
+
+28. exim_tidydb, when checking for the continued existence of a message for
+    which it has found a message-specific retry record, was not finding
+    messages that were in split spool directories. Consequently, it was
+    deleting retry records that should have stayed in existence.
+
+29. eximstats updated to version 1.35
+    1.34 - allow eximstats to parse syslog lines as well as mainlog lines
+    1.35 - bugfix such that pie charts by volume are generated correctly
+
+30. The SPA authentication driver was not abandoning authentication and moving
+    on to the next authenticator when an expansion was forced to fail,
+    contradicting the general specification for all authenticators. Instead it
+    was generating a temporary error. It now behaves as specified.
+
+31. The default ordering of permitted cipher suites for GnuTLS was pessimal
+    (the order specifies the preference for clients). The order is now AES256,
+    AES128, 3DES, ARCFOUR128.
+
+31. Small patch to Sieve code - explicitly set From: when generating an
+    autoreply.
+
+32. Exim crashed if a remote delivery caused a very long error message to be
+    recorded - for instance if somebody sent an entire SpamAssassin report back
+    as a large number of 550 error lines. This bug was coincidentally fixed by
+    increasing the size of one of Exim's internal buffers (big_buffer) that
+    happened as part of the Exiscan merge. However, to be on the safe side, I
+    have made the code more robust (and fixed the comments that describe what
+    is going on).
+
+33. Some experimental protocols are using DNS PTR records for new purposes. The
+    keys for these records are domain names, not reversed IP addresses. The
+    dnsdb PTR lookup now tests whether its key is an IP address. If not, it
+    leaves it alone. Component reversal etc. now happens only for IP addresses.
+    CAN-2005-0021
+
+34. The host_aton() function is supposed to be passed a string that is known
+    to be a valid IP address. However, in the case of IPv6 addresses, it was
+    not checking this. This is a hostage to fortune. Exim now panics and dies
+    if the condition is not met. A case was found where this could be provoked
+    from a dnsdb PTR lookup with an IPv6 address that had more than 8
+    components; fortuitously, this particular loophole had already been fixed
+    by change 4.50/55 or 4.44/33 above.
+
+    If there are any other similar loopholes, the new check in host_aton()
+    itself should stop them being exploited. The report I received stated that
+    data on the command line could provoke the exploit when Exim was running as
+    exim, but did not say which command line option was involved. All I could
+    find was the use of -be with a bad dnsdb PTR lookup, and in that case it is
+    running as the user.
+    CAN-2005-0021
+
+35. There was a buffer overflow vulnerability in the SPA authentication code
+    (which came originally from the Samba project). I have added a test to the
+    spa_base64_to_bits() function which I hope fixes it.
+    CAN-2005-0022
+
+36. The daemon start-up calls getloadavg() while still root for those OS that
+    need the first call to be done as root, but it missed one case: when
+    deliver_queue_load_max is set with deliver_drop_privilege. This is
+    necessary for the benefit of the queue runner, because there is no re-exec
+    when deliver_drop_privilege is set.
+
+37. Caching of lookup data for "hosts =" ACL conditions, when a named host list
+    was in use, was not putting the data itself into the right store pool;
+    consequently, it could be overwritten for a subsequent message in the same
+    SMTP connection. (Fix 4.40/11 dealt with the non-cache case, but overlooked
+    the caching.)
+
+38. Sometimes the final signoff response after QUIT could fail to get
+    transmitted in the non-TLS case. Testing !tls_active instead of tls_active
+    < 0 before doing a fflush(). This bug looks as though it goes back to the
+    introduction of TLS in release 3.20, but "sometimes" must have been rare
+    because the tests only now provoked it.
+
+
+Exim version 4.43
+-----------------
+
+ 1. Fixed a longstanding but relatively impotent bug: a long time ago, before
+    PIPELINING, the function smtp_write_command() used to return TRUE or FALSE.
+    Now it returns an integer. A number of calls were still expecting a T/F
+    return. Fortuitously, in all cases, the tests worked in OK situations,
+    which is the norm. However, things would have gone wrong on any write
+    failures on the smtp file descriptor. This function is used when sending
+    messages over SMTP and also when doing verify callouts.
+
+ 2. When Exim is called to do synchronous delivery of a locally submitted
+    message (the -odf or -odi options), it no longer closes stderr before doing
+    the delivery.
+
+ 3. Implemented the mua_wrapper option.
+
+ 4. Implemented mx_fail_domains and srv_fail_domains for the dnslookup router.
+
+ 5. Implemented the functions header_remove(), header_testname(),
+    header_add_at_position(), and receive_remove_recipient(), and exported them
+    to local_scan().
+
+ 6. If an ACL "warn" statement specified the addition of headers, Exim already
+    inserted X-ACL-Warn: at the start if there was no header name. However, it
+    was not making this test for the second and subsequent header lines if
+    there were newlines in the string. This meant that an invalid header could
+    be inserted if Exim was badly configured.
+
+ 7. Allow an ACL "warn" statement to add header lines at the start or after all
+    the Received: headers, as well as at the end.
+
+ 8. Added the rcpt_4xx retry error code.
+
+ 9. Added postmaster_mailfrom=xxx to callout verification option.
+
+10. Added mailfrom=xxxx to the callout verification option, for verify=
+    header_sender only.
+
+11. ${substr_1_:xxxx} and ${substr__3:xxxx} are now diagnosed as syntax errors
+    (they previously behaved as ${substr_1_0:xxxx} and ${substr:_0_3:xxxx}).
+
+12. Inserted some casts to stop certain compilers warning when using pointer
+    differences as field lengths or precisions in printf-type calls (mostly
+    affecting debugging statements).
+
+13. Added optional readline() support for -be (dynamically loaded).
+
+14. Obscure bug fix: if a message error (e.g. 4xx to MAIL) happened within the
+    same clock tick as a message's arrival, so that its received time was the
+    same as the "first fail" time on the retry record, and that message
+    remained on the queue past the ultimate address timeout, every queue runner
+    would try a delivery (because it was past the ultimate address timeout) but
+    after another failure, the ultimate address timeout, which should have then
+    bounced the address, did not kick in. This was a "< instead of <=" error;
+    in most cases the first failure would have been in the next clock tick
+    after the received time, and all would be well.
+
+15. The special items beginning with @ in domain lists (e.g. @mx_any) were not
+    being recognized when the domain list was tested by the match_domain
+    condition in an expansion string.
+
+16. Added the ${str2b64: operator.
+
+17. Exim was always calling setrlimit() to set a large limit for the number of
+    processes, without checking whether the existing limit was already
+    adequate. (It did check for the limit on file descriptors.) Furthermore,
+    errors from getrlimit() and setrlimit() were being ignored. Now they are
+    logged to the main and panic logs, but Exim does carry on, to try to do its
+    job under whatever limits there are.
+
+18. Imported PCRE 5.0.
+
+19. Trivial typo in log message " temporarily refused connection" (the leading
+    space).
+
+20. If the log selector return_path_on_delivery was set and an address was
+    redirected to /dev/null, the delivery process crashed because it assumed
+    that a return path would always be set for a "successful" delivery. In this
+    case, the whole delivery is bypassed as an optimization, and therefore no
+    return path is set.
+
+21. Internal re-arrangement: the function for sending a challenge and reading
+    a response while authentication was assuming a zero-terminated challenge
+    string. It's now changed to take a pointer and a length, to allow for
+    binary data in such strings.
+
+22. Added the cyrus_sasl authenticator (code supplied by MBM).
+
+23. Exim was not respecting finduser_retries when seeking the login of the
+    uid under which it was called; it was always trying 10 times. (The default
+    setting of finduser_retries is zero.) Also, it was sleeping after the final
+    failure, which is pointless.
+
+24. Implemented tls_on_connect_ports.
+
+25. Implemented acl_smtp_predata.
+
+26. If the domain in control=submission is set empty, Exim assumes that the
+    authenticated id is a complete email address when it generates From: or
+    Sender: header lines.
+
+27. Added "#define SOCKLEN_T int" to OS/os.h-SCO and OS/os.h-SCO_SV. Also added
+    definitions to OS/Makefile-SCO and OS/Makefile-SCO_SV that put basename,
+    chown and chgrp in /bin and hostname in /usr/bin.
+
+28. Exim was keeping the "process log" file open after each use, just as it
+    does for the main log. This opens the possibility of it remaining open for
+    long periods when the USR1 signal hits a daemon. Occasional processlog
+    errors were reported, that could have been caused by this. Anyway, it seems
+    much more sensible not to leave this file open at all, so that is what now
+    happens.
+
+29. The long-running daemon process does not normally write to the log once it
+    has entered its main loop, and it closes the log before doing so. This is
+    so that log files can straightforwardly be renamed and moved. However,
+    there are a couple of unusual error situations where the daemon does write
+    log entries, and I had neglected to close the log afterwards.
+
+30. The text of an SMTP error response that was received during a remote
+    delivery was being truncated at 512 bytes. This is too short for some of
+    the long messages that one sometimes sees. I've increased the limit to
+    1024.
+
+31. It is now possible to make retry rules that apply only when a message has a
+    specific sender, in particular, an empty sender.
+
+32. Added "control = enforce_sync" and "control = no_enforce_sync". This makes
+    it possible to be selective about when SMTP synchronization is enforced.
+
+33. Added "control = caseful_local_part" and "control = "caselower_local_part".
+
+32. Implemented hosts_connection_nolog.
+
+33. Added an ACL for QUIT.
+
+34. Setting "delay_warning=" to disable warnings was not working; it gave a
+    syntax error.
+
+35. Added mailbox_size and mailbox_filecount to appendfile.
+
+36. Added control = no_multiline_responses to ACLs.
+
+37. There was a bug in the logic of the code that waits for the clock to tick
+    in the case where the clock went backwards by a substantial amount such
+    that the microsecond fraction of "now" was more than the microsecond
+    fraction of "then" (but the whole seconds number was less).
+
+38. Added support for the libradius Radius client library this is found on
+    FreeBSD (previously only the radiusclient library was supported).
+
+
+Exim version 4.42
+-----------------
+
+ 1. When certain lookups returned multiple values in the form name=value, the
+    quoting of the values was not always being done properly. Specifically:
+    (a) If the value started with a double quote, but contained no whitespace,
+        it was not quoted.
+    (b) If the value contained whitespace other than a space character (i.e.
+        tabs or newlines or carriage returns) it was not quoted.
+    This fix has been applied to the mysql and pgsql lookups by writing a
+    separate quoting function and calling it from the lookup code. The fix
+    should probably also be applied to nisplus, ibase and oracle lookups, but
+    since I cannot test any of those, I have not disturbed their existing code.
+
+ 2. A hit in the callout cache for a specific address caused a log line with no
+    reason for rejecting RCPT. Now it says "Previous (cached) callout
+    verification failure".
+
+ 3. There was an off-by-one bug in the queryprogram router. An over-long
+    return line was truncated at 256 instead of 255 characters, thereby
+    overflowing its buffer with the terminating zero. As well as fixing this, I
+    have increased the buffer size to 1024 (and made a note to document this).
+
+ 4. If an interrupt, such as the USR1 signal that is send by exiwhat, arrives
+    when Exim is waiting for an SMTP response from a remote server, Exim
+    restarts its select() call on the socket, thereby resetting its timeout.
+    This is not a problem when such interrupts are rare. Somebody set up a cron
+    job to run exiwhat every 2 minutes, which is less than the normal select()
+    timeout (5 or 10 minutes). This meant that the select() timeout never
+    kicked in because it was always reset. I have fixed this by comparing the
+    time when an interrupt arrives with the time at the start of the first call
+    to select(). If more time than the timeout has elapsed, the interrupt is
+    treated as a timeout.
+
+ 5. Some internal re-factoring in preparation for the addition of Sieve
+    extensions (by MH). In particular, the "personal" test is moved to a
+    separate function, and given an option for scanning Cc: and Bcc: (which is
+    not set for Exim filters).
+
+ 6. When Exim created an email address using the login of the caller as the
+    local part (e.g. when creating a From: or Sender: header line), it was not
+    quoting the local part when it contained special characters such as @.
+
+ 7. Installed new OpenBSD configuration files.
+
+ 8. Reworded some messages for syntax errors in "and" and "or" conditions to
+    try to make them clearer.
+
+ 9. Callout options, other than the timeout value, were being ignored when
+    verifying sender addresses in header lines. For example, when using
+
+      verify = header_sender/callout=no_cache
+
+    the cache was (incorrectly) being used.
+
+10. Added a missing instance of ${EXE} to the exim_install script; this affects
+    only the Cygwin environment.
+
+11. When return_path_on_delivery was set as a log selector, if different remote
+    addresses in the same message used different return paths and parallel
+    remote delivery occurred, the wrong values would sometimes be logged.
+    (Whenever a remote delivery process finished, the return path value from
+    the most recently started remote delivery process was logged.)
+
+12. RFC 3848 specifies standard names for the "with" phrase in Received: header
+    lines when AUTH and/or TLS are in use. This is the "received protocol"
+    field. Exim used to use "asmtp" for authenticated SMTP, without any
+    indication (in the protocol name) for TLS use. Now it follows the RFC and
+    uses "esmtpa" if the connection is authenticated, "esmtps" if it is
+    encrypted, and "esmtpsa" if it is both encrypted and authenticated. These
+    names appear in log lines as well as in Received: header lines.
+
+13. Installed MH's patches for Sieve to add the "copy" and "vacation"
+    extensions, and comparison tests, and to fix some bugs.
+
+14. Changes to the "personal" filter test:
+
+    (1) The test was buggy in that it was just doing the equivalent of
+    "contains" tests on header lines. For example, if a user's address was
+    anne@some.where, the "personal" test would incorrectly be true for
+
+      To: susanne@some.where
+
+    This test is now done by extracting each address from the header in turn,
+    and checking the entire address. Other tests that are part of "personal"
+    are now done using regular expressions (for example, to check local parts
+    of addresses in From: header lines).
+
+    (2) The list of non-personal local parts in From: addresses has been
+    extended to include "listserv", "majordomo", "*-request", and "owner-*",
+    taken from the Sieve specification recommendations.
+
+    (3) If the message contains any header line starting with "List-" it is
+    treated as non-personal.
+
+    (4) The test for "circular" in the Subject: header line has been removed
+    because it now seems ill-conceived.
+
+15. Minor typos in src/EDITME comments corrected.
+
+16. Installed latest exipick from John Jetmore.
+
+17. If headers_add on a router specified a text string that was too long for
+    string_sprintf() - that is, longer than 8192 bytes - Exim panicked. The use
+    of string_sprintf() is now avoided.
+
+18. $message_body_size was not set (it was always zero) when running the DATA
+    ACL and the local_scan() function.
+
+19. For the "mail" command in an Exim filter, no default was being set for
+    the once_repeat time, causing a random time value to be used if "once" was
+    specified. (If the value happened to be <= 0, no repeat happened.) The
+    default is now 0s, meaning "never repeat". The "vacation" command was OK
+    (its default is 7d). It's somewhat surprising nobody ever noticed this bug
+    (I found it when inspecting the code).
+
+20. There is now an overall timeout for performing a callout verification. It
+    defaults to 4 times the callout timeout, which applies to individual SMTP
+    commands during the callout. The overall timeout applies when there is more
+    than one host that can be tried. The timeout is checked before trying the
+    next host. This prevents very long delays if there are a large number of
+    hosts and all are timing out (e.g. when the network connections are timing
+    out). The value of the overall timeout can be changed by specifying an
+    additional sub-option for "callout", called "maxwait". For example:
+
+      verify = sender/callout=5s,maxwait=20s
+
+21. Add O_APPEND to the open() call for maildirsize files (Exim already seeks
+    to the end before writing, but this should make it even safer).
+
+22. Exim was forgetting that it had advertised PIPELINING for the second and
+    subsequent messages on an SMTP connection. It was also not resetting its
+    memory on STARTTLS and an internal HELO.
+
+23. When Exim logs an SMTP synchronization error within a session, it now
+    records whether PIPELINING has been advertised or not.
+
+24. Added 3 instances of "(long int)" casts to time_t variables that were being
+    formatted using %ld, because on OpenBSD (and perhaps others), time_t is int
+    rather than long int.
+
+25. Installed the latest Cygwin configuration files from the Cygwin maintainer.
+
+26. Added the never_mail option to autoreply.
+
+
+Exim version 4.41
+-----------------
+
+ 1. A reorganization of the code in order to implement 4.40/8 caused a daemon
+    crash if the getsockname() call failed; this can happen if a connection is
+    closed very soon after it is established. The problem was simply in the
+    order in which certain operations were done, causing Exim to try to write
+    to the SMTP stream before it had set up the file descriptor. The bug has
+    been fixed by making things happen in the correct order.
+
+
+Exim version 4.40
+-----------------
+
+ 1. If "drop" was used in a DATA ACL, the SMTP output buffer was not flushed
+    before the connection was closed, thus losing the rejection response.
+
+ 2. Commented out the definition of SOCKLEN_T in os.h-SunOS5. It is needed for
+    some early Solaris releases, but causes trouble in current releases where
+    socklen_t is defined.
+
+ 3. When std{in,out,err} are closed, re-open them to /dev/null so that they
+    always exist.
+
+ 4. Minor refactoring of os.c-Linux to avoid compiler warning when IPv6 is not
+    configured.
+
+ 5. Refactoring in expand.c to improve memory usage. Pre-allocate a block so
+    that releasing the top of it at the end releases what was used for sub-
+    expansions (unless the block got too big). However, discard this block if
+    the first thing is a variable or header, so that we can use its block when
+    it is dynamic (useful for very large $message_headers, for example).
+
+ 6. Lookups now cache *every* query, not just the most recent. A new, separate
+    store pool is used for this. It can be recovered when all lookup caches are
+    flushed. Lookups now release memory at the end of their result strings.
+    This has involved some general refactoring of the lookup sources.
+
+ 7. Some code has been added to the store_xxx() functions to reduce the amount
+    of flapping under certain conditions.
+
+ 8. log_incoming_interface used to affect only the <= reception log lines. Now
+    it causes the local interface and port to be added to several more SMTP log
+    lines, for example "SMTP connection from", and rejection lines.
+
+ 9. The Sieve author supplied some patches for the doc/README.SIEVE file.
+
+10. Added a conditional definition of _BSD_SOCKLEN_T to os.h-Darwin.
+
+11. If $host_data was set by virtue of a hosts lookup in an ACL, its value
+    could be overwritten at the end of the current message (or the start of a
+    new message if it was set in a HELO ACL). The value is now preserved for
+    the duration of the SMTP connection.
+
+12. If a transport had a headers_rewrite setting, and a matching header line
+    contained an unqualified address, that address was qualified, even if it
+    did not match any rewriting rules. The underlying bug was that the values
+    of the flags that permit the existence of unqualified sender and recipient
+    addresses in header lines (set by {sender,recipient}_unqualified_hosts for
+    non-local messages, and by -bnq for local messages) were not being
+    preserved with the message after it was received.
+
+13. When Exim was logging an SMTP synchronization error, it could sometimes log
+    "next input=" as part of the text comprising the host identity instead of
+    the correct text. The code was using the same buffer for two different
+    strings. However, depending on which order the printing function evaluated
+    its arguments, the bug did not always show up. Under Linux, for example, my
+    test suite worked just fine.
+
+14. Exigrep contained a use of Perl's "our" scoping after change 4.31/70. This
+    doesn't work with some older versions of Perl. It has been changed to "my",
+    which in any case is probably the better facility to use.
+
+15. A really picky compiler found some instances of statements for creating
+    error messages that either had too many or two few arguments for the format
+    string.
+
+16. The size of the buffer for calls to the DNS resolver has been increased
+    from 1024 to 2048. A larger buffer is needed when performing PTR lookups
+    for addresses that have a lot of PTR records. This alleviates a problem; it
+    does not fully solve it.
+
+17. A dnsdb lookup for PTR records that receives more data than will fit in the
+    buffer now truncates the list and logs the incident, which is the same
+    action as happens when Exim is looking up a host name and its aliases.
+    Previously in this situation something unpredictable would happen;
+    sometimes it was "internal error: store_reset failed".
+
+18. If a server dropped the connection unexpectedly when an Exim client was
+    using GnuTLS and trying to read a response, the client delivery process
+    crashed while trying to generate an error log message.
+
+19. If a "warn" verb in an ACL added multiple headers to a message in a single
+    string, for example:
+
+      warn message = H1: something\nH2: something
+
+    the text was added as a single header line from Exim's point of view
+    though it ended up OK in the delivered message. However, searching for the
+    second and subsequent header lines using $h_h2: did not work. This has been
+    fixed. Similarly, if a system filter added multiple headers in this way,
+    the routers could not see them.
+
+20. Expanded the error message when iplsearch is called with an invalid key to
+    suggest using net-iplsearch in a host list.
+
+21. When running tests using -bh, any delays imposed by "delay" modifiers in
+    ACLs are no longer actually imposed (and a message to that effect is
+    output).
+
+22. If a "gecos" field in a passwd entry contained escaped characters, in
+    particular, if it contained a \" sequence, Exim got it wrong when building
+    a From: or a Sender: header from that name. A second bug also caused
+    incorrect handling when an unquoted " was present following a character
+    that needed quoting.
+
+23. "{crypt}" as a password encryption mechanism for a "crypteq" expansion item
+    was not being matched caselessly.
+
+24. Arranged for all hyphens in the exim.8 source to be escaped with
+    backslashes.
+
+25. Change 16 of 4.32, which reversed 71 or 4.31 didn't quite do the job
+    properly. Recipient callout cache records were still being keyed to include
+    the sender, even when use_sender was set false. This led to far more
+    callouts that were necessary. The sender is no longer included in the key
+    when use_sender is false.
+
+26. Added "control = submission" modifier to ACLs.
+
+27. Added the ${base62d: operator to decode base 62 numbers.
+
+28. dnsdb lookups can now access SRV records.
+
+29. CONFIGURE_OWNER can be set at build time to define an alternative owner for
+    the configuration file.
+
+30. The debug message "delivering xxxxxx-xxxxxx-xx" is now output in verbose
+    (-v) mode. This makes the output for a verbose queue run more intelligible.
+
+31. Added a use_postmaster feature to recipient callouts.
+
+32. Added the $body_zerocount variable, containing the number of binary zero
+    bytes in the message body.
+
+33. The time of last modification of the "new" subdirectory is now used as the
+    "mailbox time last read" when there is a quota error for a maildir
+    delivery.
+
+34. Added string comparison operators lt, lti, le, lei, gt, gti, ge, gei.
+
+35. Added +ignore_unknown as a special item in host lists.
+
+36. Code for decoding IPv6 addresses in host lists is now included, even if
+    IPv6 support is not being compiled. This fixes a bug in which an IPv6
+    address was recognized as an IP address, but was then not correctly decoded
+    into binary, causing unexpected and incorrect effects when compared with
+    another IP address.
+
+
+Exim version 4.34
+-----------------
+
+ 1. Very minor rewording of debugging text in manualroute to say "list of
+    hosts" instead of "hostlist".
+
+ 2. If verify=header_syntax was set, and a header line with an unqualified
+    address (no domain) and a large number of spaces between the end of the
+    name and the colon was received, the reception process suffered a buffer
+    overflow, and (when I tested it) crashed. This was caused by some obsolete
+    code that should have been removed. The fix is to remove it!
+
+ 3. When running in the test harness, delay a bit after writing a bounce
+    message to get a bit more predictability in the log output.
+
+ 4. Added a call to search_tidyup() just before forking a reception process. In
+    theory, someone could use a lookup in the expansion of smtp_accept_max_
+    per_host which, without the tidyup, could leave open a database connection.
+
+ 5. Added the variables $recipient_data and $sender_data which get set from a
+    lookup success in an ACL "recipients" or "senders" condition, or a router
+    "senders" option, similar to $domain_data and $local_part_data.
+
+ 6. Moved the writing of debug_print from before to after the "senders" test
+    for routers.
+
+ 7. Change 4.31/66 (moving the time when the Received: is generated) caused
+    problems for message scanning, either using a data ACL, or using
+    local_scan() because the Received: header was not generated till after they
+    were called (in order to set the time as the time of reception completion).
+    I have revised the way this works. The header is now generated after the
+    body is received, but before the ACL or local_scan() are called. After they
+    are run, the timestamp in the header is updated.
+
+
+Exim version 4.33
+-----------------
+
+ 1. Change 4.24/6 introduced a bug because the SIGALRM handler was disabled
+    before starting a queue runner without re-exec. This happened only when
+    deliver_drop_privilege was set or when the Exim user was set to root. The
+    effect of the bug was that timeouts during subsequent deliveries caused
+    crashes instead of being properly handled. The handler is now left at its
+    default (and expected) setting.
+
+ 2. The other case in which a daemon avoids a re-exec is to deliver an incoming
+    message, again when deliver_drop_privilege is set or Exim is run as root.
+    The bug described in (1) was not present in this case, but the tidying up
+    of the other signals was missing. I have made the two cases consistent.
+
+ 3. The ignore_target_hosts setting on a manualroute router was being ignored
+    for hosts that were looked up using the /MX notation.
+
+ 4. Added /ignore=<ip list> feature to @mx_any, @mx_primary, and @mx_secondary
+    in domain lists.
+
+ 5. Change 4.31/55 was buggy, and broke when there was a rewriting rule that
+    operated on the sender address. After changing the $sender_address to <>
+    for the sender address verify, Exim was re-instated it as the original
+    (before rewriting) address, but remembering that it had rewritten it, so it
+    wasn't rewriting it again. This bug also had the effect of breaking the
+    sender address verification caching when the sender address was rewritten.
+
+ 6. The ignore_target_hosts option was being ignored by the ipliteral router.
+    This has been changed so that if the ip literal address matches
+    ignore_target_hosts, the router declines.
+
+ 7. Added expansion conditions match_domain, match_address, and match_local_
+    part (NOT match_host).
+
+ 8. The placeholder for the Received: header didn't have a length field set.
+
+ 9. Added code to Exim itself and to exim_lock to test for a specific race
+    condition that could lead to file corruption when using MBX delivery. The
+    issue is with the lockfile that is created in /tmp. If this file is removed
+    after a process has opened it but before that process has acquired a lock,
+    there is the potential for a second process to recreate the file and also
+    acquire a lock. This could lead to two Exim processes writing to the file
+    at the same time. The added code performs the same test as UW imapd; it
+    checks after acquiring the lock that its file descriptor still refers to
+    the same named file.
+
+10. The buffer for building added header lines was of fixed size, 8192 bytes.
+    It is now parameterized by HEADER_ADD_BUFFER_SIZE and this can be adjusted
+    when Exim is built.
+
+11. Added the smtp_active_hostname option. If used, this will typically be made
+    to depend on the incoming interface address. Because $interface_address is
+    not set up until the daemon has forked a reception process, error responses
+    that can happen earlier (such as "too many connections") no longer contain
+    a host name.
+
+12. If an expansion in a condition on a "warn" statement fails because a lookup
+    defers, the "warn" statement is abandoned, and the next ACL statement is
+    processed. Previously this caused the whole ACL to be aborted.
+
+13. Added the iplsearch lookup type.
+
+14. Added ident_timeout as a log selector.
+
+15. Added tls_certificate_verified as a log selector.
+
+16. Added a global option tls_require_ciphers (compare the smtp transport
+    option of the same name). This controls incoming TLS connections.
+
+17. I finally figured out how to make tls_require_ciphers do a similar thing
+    in GNUtls to what it does in OpenSSL, that is, set up an appropriate list
+    before starting the TLS session.
+
+18. Tabs are now shown as \t in -bP output.
+
+19. If the log selector return_path_on_delivery was set, Exim crashed when
+    bouncing a message because it had too many Received: header lines.
+
+20. If two routers both had headers_remove settings, and the first one included
+    a superfluous trailing colon, the final name in the first list and the
+    first name in the second list were incorrectly joined into one item (with a
+    colon in the middle).
+
+
+Exim version 4.32
+-----------------
+
+ 1. Added -C and -D options to the exinext utility, mainly to make it easier
+    to include in the automated testing, but these could be helpful when
+    multiple configurations are in use.
+
+ 2. The exinext utility was not formatting the output nicely when there was
+    an alternate port involved in the retry record key, nor when there was a
+    message id as well (for retries that were specific to a specific message
+    and a specific host). It was also confused by IPv6 addresses, because of
+    the additional colons they contain. I have fixed the IPv4 problem, and
+    patched it up to do a reasonable job for IPv6.
+
+ 3. When there is an error after a MAIL, RCPT, or DATA SMTP command during
+    delivery, the log line now contains "pipelined" if PIPELINING was used.
+
+ 4. An SMTP transport process used to panic and die if the bind() call to set
+    an explicit outgoing interface failed. This has been changed; it is now
+    treated in the same way as a connect() failure.
+
+ 5. A reference to $sender_host_name in the part of a conditional expansion
+    that was being skipped was still causing a DNS lookup. This no longer
+    occurs.
+
+ 6. The def: expansion condition was not recognizing references to header lines
+    that used bh_ and bheader_.
+
+ 7. Added the _cache feature to named lists.
+
+ 8. The code for checking quota_filecount in the appendfile transport was
+    allowing one more file than it should have been.
+
+ 9. For compatibility with Sendmail, the command line option
+
+      -prval:sval
+
+    is equivalent to
+
+      -oMr rval -oMs sval
+
+    and sets the incoming protocol and host name (for trusted callers). The
+    host name and its colon can be omitted when only the protocol is to be set.
+    Note the Exim already has two private options, -pd and -ps, that refer to
+    embedded Perl. It is therefore impossible to set a protocol value of "d" or
+    "s", but I don't think that's a major issue.
+
+10. A number of refactoring changes to the code, none of which should affect
+    Exim's behaviour:
+
+    (a) The number of logging options was getting close to filling up the
+    32-bit word that was used as a bit map. I have split them into two classes:
+    those that are passed in the argument to log_write(), and those that are
+    only ever tested independently outside of that function. These are now in
+    separate 32-bit words, so there is plenty of room for expansion again.
+    There is no change in the user interface or the logging behaviour.
+
+    (b) When building, for example, log lines, the code previously used a
+    macro that called string_cat() twice, in order to add two strings. This is
+    not really sufficiently general. Furthermore, there was one instance where
+    it was actually wrong because one of the argument was used twice, and in
+    one call a function was used. (As it happened, calling the function twice
+    did not affect the overall behaviour.) The macro has been replaced by a
+    function that can join an arbitrary number of extra strings onto a growing
+    string.
+
+    (c) The code for expansion conditions now uses a table and a binary chop
+    instead of a serial search (which was left over from when there were very
+    few conditions). Also, it now recognizes conditions like "pam" even when
+    the relevant support is not compiled in: a suitably worded error message is
+    given if an attempt is made to use such a condition.
+
+11. Added ${time_interval:xxxxx}.
+
+12. A bug was causing one of the ddress fields not to be passed back correctly
+    from remote delivery subprocesses. The field in question was not being
+    subsequently used, so this caused to problems in practice.
+
+13. Added new log selectors queue_time and deliver_time.
+
+14. Might have fixed a bug in maildirsizefile handling that threw up
+    "unexpected character" debug warnings, and recalculated the data
+    unnecessarily. In any case, I expanded the warning message to give more
+    information.
+
+15. Added the message "Restricted characters in address" to the statements in
+    the default ACL that block characters like @ and % in local parts.
+
+16. Change 71 for release 4.31 proved to be much less benign that I imagined.
+    Three changes have been made:
+
+    (a) There was a serious bug; a negative response to MAIL caused the whole
+        recipient domain to be cached as invalid, thereby blocking all messages
+        to all local parts at the same domain, from all senders. This bug has
+        been fixed. The domain is no longer cached after a negative response to
+        MAIL if the sender used is not empty.
+
+    (b) The default behaviour of using MAIL FROM:<> for recipient callouts has
+        been restored.
+
+    (c) A new callout option, "use_sender" has been added for people who want
+        the modified behaviour.
+
+
+Exim version 4.31
+-----------------
+
+ 1. Removed "EXTRALIBS=-lwrap" from OS/Makefile-Unixware7 on the advice of
+    Larry Rosenman.
+
+ 2. Removed "LIBS = -lresolv" from OS/Makefile-Darwin as it is not needed, and
+    indeed breaks things for older releases.
+
+ 3. Added additional logging to the case where there is a problem reading data
+    from a filter that is running in a subprocess using a pipe, in order to
+    try to track down a specific problem.
+
+ 4. Testing facility fudge: when running in the test harness and attempting
+    to connect to 10.x.x.x (expecting a connection timeout) I'm now sometimes
+    getting "No route to host". Convert this to a timeout.
+
+ 5. Define ICONV_ARG2_TYPE as "char **" for Unixware7 to avoid compiler
+    warning.
+
+ 6. Some OS don't have socklen_t but use size_t instead. This affects the
+    fifth argument of getsockopt() amongst other things. This is now
+    configurable by a macro called SOCKLEN_T which defaults to socklen_t, but
+    can be set for individual OS. I have set it for SunOS5, OSF1, and
+    Unixware7. Current versions of SunOS5 (aka Solaris) do have socklen_t, but
+    some earlier ones do not.
+
+ 7. Change 4.30/15 was not doing the test caselessly.
+
+ 8. The standard form for an IPv6 address literal was being rejected by address
+    parsing in, for example, MAIL and RCPT commands. An example of this kind of
+    address is [IPv6:2002:c1ed:8229:10:202:2dff:fe07:a42a]. Exim now accepts
+    this, as well as the form without the "IPv6" on the front (but only when
+    address literals are enabled, of course).
+
+ 9. Added some casts to avoid compiler warnings in OS/os.c-Linux.
+
+10. Exim crashed if a message with an empty sender address specified by -f
+    encountered a router with an errors_to setting. This could be provoked only
+    by a command such as
+
+      exim -f "" ...
+
+    where an empty string was supplied; "<>" did not hit this bug.
+
+11. Installed PCRE release 4.5.
+
+12. If EHLO/HELO was rejected by an ACL, the value of $sender_helo_name
+    remained set. It is now erased.
+
+13. exiqgrep wasn't working on MacOS X because it didn't correctly compute
+    times from message ids (which are base 36 rather than the normal 62).
+
+14. "Expected" SMTP protocol errors that can arise when PIPELINING is in use
+    were being counted as actual protocol errors, and logged if the log
+    selector +smtp_protocol_error was set. One cannot be perfect in this test,
+    but now, if PIPELINING has been advertised, RCPT following a rejected MAIL,
+    and DATA following a set of rejected RCPTs do not count as protocol errors.
+    In other words, Exim assumes they were pipelined, though this may not
+    actually be the case. Of course, in all cases the client gets an
+    appropriate error code.
+
+15. If a lookup fails in an ACL condition, a message about the failure may
+    be available; it is used if testing the ACL cannot continue, because most
+    such messages specify what the cause of the deferral is. However, some
+    messages (e.g. "MYSQL: no data found") do not cause a defer. There was bug
+    that caused an old message to be retained and used if a later statement
+    caused a defer, replacing the real cause of the deferral.
+
+16. If an IP address had so many PTR records that the DNS lookup buffer
+    was not large enough to hold them, Exim could crash while trying to process
+    the truncated data. It now detects and logs this case.
+
+17. Further to 4.21/58, another change has been made: if (and only if) the
+    first line of a message (the first header line) ends with CRLF, a bare LF
+    in a subsequent header line has a space inserted after it, so as not to
+    terminate the header.
+
+18. Refactoring: tidied an ugly bit of code in appendfile that copied data
+    unnecessarily, used atoi() instead of strtol(), and didn't check the
+    termination when getting file sizes from file names by regex.
+
+19. Completely re-implemented the support for maildirsize files, in the light
+    of a number of problems with the previous contributed implementation
+    (4.30/29). In particular:
+
+    . If the quota is zero, the maildirsize file is maintained, but no quota is
+      imposed.
+
+    . If the maildir directory does not exist, it is created before any attempt
+      to write a maildirsize file.
+
+    . The quota value in the file is just a cache; if the quota is changed in
+      the transport, the new value overrides.
+
+    . A regular expression is available for excluding directories from the
+      count.
+
+20. The autoreply transport checks the characters in options that define the
+    message's headers; it allows continued headers, but it was checking with
+    isspace() after an embedded newline instead of explicitly looking for a
+    space or a tab.
+
+21. If all the "regular" hosts to which an address was routed had passed their
+    expiry times, and had not reached their retry times, the address was
+    bounced, even if fallback hosts were defined. Now Exim should go on to try
+    the fallback hosts.
+
+22. Increased buffer sizes in the callout code from 1024 to 4096 to match the
+    equivalent code in the SMTP transport. Some hosts send humungous responses
+    to HELO/EHLO, more than 1024 it seems.
+
+23. Refactoring: code in filter.c used (void *) for "any old type" but this
+    gives compiler warnings in some environments. I've now done it "properly",
+    using a union.
+
+24. The replacement for inet_ntoa() that is used with gcc on IRIX systems
+    (because of problems with the built-in one) was declared to return uschar *
+    instead of char *, causing compiler failure.
+
+25. Fixed a file descriptor leak when processing alias/forward files.
+
+26. Fixed a minor format string issue in dbfn.c.
+
+27. Typo in exim.c: ("dmbnz" for "dbmnz").
+
+28. If a filter file refered to $h_xxx or $message_headers, and the headers
+    contained RFC 2047 "words", Exim's memory could, under certain conditions,
+    become corrupted.
+
+29. When a sender address is verified, it is cached, to save repeating the test
+    when there is more than one recipient in a message. However, when the
+    verification involves a callout, it is possible for different callout
+    options to be set for different recipients. It is too complicated to keep
+    track of this in the cache, so now Exim always runs a verification when a
+    callout is required, relying on the callout cache for the optimization.
+    The overhead is duplication of the address routing, but this should not be
+    too great.
+
+30. Fixed a bug in callout caching. If a RCPT command caused the sender address
+    to be verified with callout=postmaster, and the main callout worked but the
+    postmaster check failed, the verification correctly failed. However, if a
+    subsequent RCPT command asked for sender verification *without* the
+    postmaster check, incorrect caching caused this verification also to fail,
+    incorrectly.
+
+31. Exim caches DNS lookup failures so as to avoid multiple timeouts; however,
+    it was not caching the DNS options (qualify_single, search_parents) that
+    were used when the lookup failed. A subsequent lookup with different
+    options therefore always gave the same answer, though there were cases
+    where it should not have. (Example: a "domains = !$mx_any" option on a
+    dnslookup router: the "domains" option is always processed without any
+    widening, but the router might have qualify_single set.) Now Exim uses the
+    cached value only when the same options are set.
+
+32. Added John Jetmore's "exipick" utility to the distribution.
+
+33. GnuTLS: When an attempt to start a TLS session fails for any reason other
+    than a timeout (e.g. a certificate is required, and is not provided), an
+    Exim server now closes the connection immediately. Previously it waited for
+    the client to close - but if the client is SSL, it seems that they each
+    wait for each other, leading to a delay before one of them times out.
+
+34: GnuTLS: Updated the code to use the new GnuTLS 1.0.0 API. I have not
+    maintained 0.8.x compatibility because I don't think many are using it, and
+    it is clearly obsolete.
+
+35. Added TLS support for CRLs: a tls_crl global option and one for the smtp
+    transport.
+
+36. OpenSSL: $tls_certificate_verified was being set to 1 even if the
+    client certificate was expired. A simple patch fixes this, though I don't
+    understand the full logic of why the verify callback is called multiple
+    times.
+
+37. OpenSSL: a patch from Robert Roselius: "Enable client-bug workaround.
+    Versions of OpenSSL as of 0.9.6d include a 'CBC countermeasure' feature,
+    which causes problems with some clients (such as the Certicom SSL Plus
+    library used by Eudora). This option, SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS,
+    disables the coutermeasure allowing Eudora to connect."
+
+38. Exim was not checking that a write() to a log file succeeded. This could
+    lead to Bad Things if a log got too big, in particular if it hit a file
+    size limit. Exim now panics and dies if it cannot write to a log file, just
+    as it does if it cannot open a log file.
+
+39. Modified OS/Makefile-Linux so that it now contains
+
+      CFLAGS=-O -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE
+
+    The two -D definitions ensure that Exim is compiled with large file
+    support, which makes it possible to handle log files that are bigger than
+    2^31.
+
+40. Fixed a subtle caching bug: if (in an ACL or a set of routers, for
+    instance) a domain was checked against a named list that involved a lookup,
+    causing $domain_data to be set, then another domain was checked against the
+    same list, then the first domain was re-checked, the value of $domain_data
+    after the final check could be wrong. In particular, if the second check
+    failed, it could be set empty. This bug probably also applied to
+    $localpart_data.
+
+41. The strip_trailing_dot option was not being applied to the address given
+    with the -f command-line option.
+
+42. The code for reading a message's header from the spool was incrementing
+    $received_count, but never initializing it. This meant that the value was
+    incorrect (doubled) while delivering a message in the same process in which
+    it was received. In the most common configuration of Exim, this never
+    happens - a fresh exec is done - but it can happen when
+    deliver_drop_privilege is set.
+
+43. When Exim logs an SMTP synchronization error - client data sent too soon -
+    it now includes up to 150 characters of the unexpected data in the log
+    line.
+
+44. The exim_dbmbuild utility uses fixed size buffers for reading input lines
+    and building data strings. The size of both of these buffers was 10 000
+    bytes - far larger than anybody would *ever* want, thought I. Needless to
+    say, somebody hit the limit. I have increased the maximum line length to
+    20 000 and the maximum data length of concatenated lines to 100 000. I have
+    also fixed two bugs, because there was no checking on these buffers. Tsk,
+    tsk. Now exim_dbmbuild gives a message and exits with an error code if a
+    buffer is too small.
+
+45. The exim_dbmbuild utility did not support quoted keys, as Exim does in
+    lsearch lookups. Now it does.
+
+46. When parsing a route_list item in a manualroute router, a fixed-length
+    buffer was used for the list of hosts. I made this 1024 bytes long,
+    thinking that nobody would ever have a list of hosts that long. Wrong.
+    Somebody had a whole pile of complicated expansion conditions, and the
+    string was silently truncated, leading to an expansion error. It turns out
+    that it is easier to change to an unlimited length (owing to other changes
+    that have happened since this code was originally written) than to build
+    structure for giving a limitation error. The length of the item that
+    expands into the list of hosts is now unlimited.
+
+47. The lsearch lookup could not handle data where the length of text line was
+    more than 4095 characters. Such lines were truncated, leading to shortened
+    data being returned. It should now handle lines of any length.
+
+48. Minor wording revision: "cannot test xxx in yyy ACL" becomes "cannot test
+    xxx condition in yyy ACL" (e.g. "cannot test domains condition in DATA
+    ACL").
+
+49. Cosmetic tidy to scripts like exicyclog that are generated by globally
+    replacing strings such as BIN_DIRECTORY in a source file: the replacement
+    no longer happens in comment lines. A list of replacements is now placed
+    at the head of all of the source files, except those whose only change is
+    to replace PERL_COMMAND in the very first #! line.
+
+50. Replaced the slow insertion sort in queue.c, for sorting the list of
+    messages on the queue, with a bottom-up merge sort, using code contributed
+    by Michael Haardt. This should make operations like -bp somewhat faster on
+    large queues. It won't affect queue runners, except when queue_run_in_order
+    is set.
+
+51. Installed eximstats 1.31 in the distribution.
+
+52. Added support for SRV lookups to the dnslookup router.
+
+53. If an ACL referred to $message_body or $message_body_end, the value was not
+    reset for any messages that followed in the same SMTP session.
+
+54. The store-handling optimization for building very long strings was not
+    differentiating between the different store pools. I don't think this
+    actually made any difference in practice, but I've tidied it.
+
+55. While running the routers to verify a sender address, $sender_address
+    was still set to the sender address. This is wrong, because when routing to
+    send a bounce to the sender, it would be empty. Therefore, I have changed
+    it so that, while verifying a sender address, $sender_address is set to <>.
+    (There is no change to what happens when verifying a recipient address.)
+
+56. After finding MX (or SRV) records, Exim was doing a DNS lookup for the
+    target A or AAAA records (if not already returned) without resetting the
+    qualify_single or search_parents options of the DNS resolver. These are
+    inappropriate in this case because the targets of MX and SRV records must
+    be FQDNs. A broken DNS record could cause trouble if it happened to have a
+    target that, when qualified, matched something in the local domain. These
+    two options are now turned off when doing these lookups.
+
+57. It seems that at least some releases of Reiserfs (which does not have the
+    concept of a fixed number of inodes) returns zero and not -1 for the
+    number of available inodes. This interacted badly with check_spool_inodes,
+    which assumed that -1 was the "no such thing" setting. What I have done is
+    to check that the total number of inodes is greater than zero before doing
+    the test of how many are available.
+
+58. When a "warn" ACL statement has a log_message modifier, the message is
+    remembered, and not repeated. This is to avoid a lot of repetition when a
+    message has many recipients that cause the same warning to be written.
+    Howewer, Exim was preserving the list of already written lines for an
+    entire SMTP session, which doesn't seem right. The memory is now reset if a
+    new message is started.
+
+59. The "rewrite" debugging flag was not showing the result of rewriting in the
+    debugging output unless log_rewrite was also set.
+
+60. Avoid a compiler warning on 64-bit systems in dsearch.c by avoiding the use
+    of (int)(handle) when we know that handle contains (void *)(-1).
+
+61. The Exim daemon panic-logs an error return when it closes the incoming
+    connection. However "connection reset by peer" seems to be common, and
+    isn't really an error worthy of noting specially, so that particular error
+    is no long logged.
+
+62. When Exim is trying to find all the local interfaces, it used to panic and
+    die if the ioctl to get the interface flags failed. However, it seems that
+    on at least one OS (Solaris 9) it is possible to have an interface that is
+    included in the list of interfaces, but for which you get a failure error
+    for this call. This happens when the interface is not "plumbed" into a
+    protocol (i.e. neither IPv4 nor IPv6). I've changed the code so that a
+    failure of the "get flags" call assumes that the interface is down.
+
+63. Added a ${eval10: operator, which assumes all numbers are decimal. This
+    makes life easier for people who are doing arithmetic on fields extracted
+    from dates, where you often get leading zeros that should not be
+    interpreted as octal.
+
+64. Added qualify_domain to the redirect router, to override the global
+    setting.
+
+65. If a pathologically long header line contained very many addresses (the
+    report of this problem mentioned 10 000) and each of them was rewritten,
+    Exim could use up a very large amount of memory. (It kept on making new
+    copies of the header line as it rewrote, and never released the old ones.)
+    At the expense of a bit more processing, the header rewriting function has
+    been changed so that it no longer eats memory in this way.
+
+66. The generation of the Received: header has been moved from the time that a
+    message starts to be received, to the time that it finishes. The timestamp
+    in the Received: header should now be very close to that of the <= log
+    line. There are two side-effects of this change:
+
+    (a) If a message is rejected by a DATA or non-SMTP ACL or local_scan(), the
+        logged header lines no longer include the local Received: line, because
+        it has not yet been created. The same applies to a copy of the message
+        that is returned to a non-SMTP sender when a message is rejected.
+
+    (b) When a filter file is tested using -bf, no additional Received: header
+        is added to the test message. After some thought, I decided that this
+        is a bug fix.
+
+    This change does not affect the value of $received_for. It is still set
+    after address rewriting, but before local_scan() is called.
+
+67. Installed the latest Cygwin-specific files from the Cygwin maintainer.
+
+68. GnuTLS: If an empty file is specified for tls_verify_certificates, GnuTLS
+    gave an unhelpful panic error message, and a defer error. I have managed to
+    change this behaviour so that it now rejects any supplied certificate,
+    which seems right, as the list of acceptable certificates is empty.
+
+69. OpenSSL: If an empty file is specified for tls_verify_certificates, OpenSSL
+    gave an unhelpful defer error. I have not managed to make this reject any
+    supplied certificates, but the error message it gives is "no certificate
+    supplied", which is not helpful.
+
+70. exigrep's output now also includes lines that are not associated with any
+    message, but which match the given pattern. Implemented by a patch from
+    Martin Sluka, which also tidied up the Perl a bit.
+
+71. Recipient callout verification, like sender verification, was using <> in
+    the MAIL FROM command. This isn't really the right thing, since the actual
+    sender may affect whether the remote host accepts the recipient or not. I
+    have changed it to use the actual sender in the callout; this means that
+    the cache record is now keyed on a recipient/sender pair, not just the
+    recipient address. There doesn't seem to be a real danger of callout loops,
+    since a callout by the remote host to check the sender would use <>.
+    [SEE ABOVE: changed after hitting problems.]
+
+72. Exim treats illegal SMTP error codes that do not begin with 4 or 5 as
+    temporary errors. However, in the case of such a code being given after
+    the end of a data transmission (i.e. after ".") Exim was failing to write
+    a retry record for the message. (Yes, there was some broken host that was
+    actually sending 8xx at this point.)
+
+73. An unknown lookup type in a host list could cause Exim to panic-die when
+    the list was checked. (An example that provoked this was putting <; in the
+    middle of a list instead of at the start.) If this happened during a DATA
+    ACL check, a -D file could be left lying around. This kind of configuration
+    error no longer causes Exim to die; instead it causes a defer errror. The
+    incident is still logged to the main and panic logs.
+
+74. Buglet left over from Exim 3 conversion. The message "too many messages
+    in one connection" was written to the rejectlog but not the mainlog, except
+    when address rewriting (yes!) was being logged.
+
+75. Added write_rejectlog option.
+
+76. When a system filter was run not as root (that is, when system_filter_user
+    was set), the values of the $n variables were not being returned to the
+    main process; thus, they were not subsequently available in the $sn
+    variables.
+
+77. Added +return_path_on_delivery log selector.
+
+78. A connection timeout was being treated differently from recipients deferred
+    when testing hosts_max_try with a message that was older than the host's
+    retry timeout. (The host should not be counted, thus allowing all hosts to
+    be tried at least once before bouncing.) This may have been the cause of an
+    occasionally reported bug whereby a message would remain on the queue
+    longer than the retry timeout, but would be bounced if a delivery was
+    forced. I say "may" because I never totally pinned down the problem;
+    setting up timeout/retry tests is difficult. See also the next item.
+
+79. The ultimate address timeout was not being applied to errors that involved
+    a combination of host plus message (for example, a timeout on a MAIL
+    command). When an address resolved to a number of possible hosts, and they
+    were not all tried for each delivery (e.g. because of hosts_max_try), a
+    message could remain on the queue longer than the retry timeout.
+
+80. Sieve bug: "stop" inside "elsif" was broken. Applied a patch from Michael
+    Haardt.
+
+81. Fixed an obscure SMTP outgoing bug which required at least the following
+    conditions: (a) there was another message waiting for the same server;
+    (b) the server returned 5xx to all RCPT commands in the first message so
+    that the message was not completed; (c) the server dropped the connection
+    or gave a negative response to the RSET that Exim sends to abort the
+    transaction. The observed case was a dropped connection after DATA that had
+    been sent in pipelining mode. That is, the server had advertised PIPELINING
+    but was not implementing it correctly. The effect of the bug was incorrect
+    behaviour, such as trying another host, and this could lead to a crash.
+
+
+Exim version 4.30
+-----------------
+
+ 1. The 3rd arguments to getsockname(), getpeername(), and accept() in exim.c
+    and daemon.c were passed as pointers to ints; they should have been
+    pointers to socklen_t variables (which are typically unsigned ints).
+
+ 2. Some signed/unsigned type warnings in the os.c file for Linux have been
+    fixed.
+
+ 3. Fixed a really odd bug that affected only the testing scheme; patching a
+    certain fixed string in the binary changed the value of another string that
+    happened to be identical to the end of the original first string.
+
+ 4. When gethostbyname() (or equivalent) is passed an IP address as a "host
+    name", it returns that address as the IP address. On some operating
+    systems (e.g. Solaris), it also passes back the IP address string as the
+    "host name". However, on others (e.g. Linux), it passes back an empty
+    string. Exim wasn't checking for this, and was changing the host name to an
+    empty string, assuming it had been canonicized.
+
+ 5. Although rare, it is permitted to have more than one PTR record for a given
+    IP address. I thought that gethostbyaddr() or getipnodebyaddr() always gave
+    all the names associated with an address, because they do in Solaris.
+    However, it seems that they do not in Linux for data that comes from the
+    DNS. If an address in /etc/hosts has multiple names, they _are_ all given.
+    I found this out when I moved to a new Linux workstation and tried to run
+    the Exim test suite.
+
+    To get round this problem I have changed the code so that it now does its
+    own call to the DNS to look up PTR records when searching for a host name.
+    If nothing can be found in the DNS, it tries gethostbyaddr(), so that
+    addresses that are only in /etc/hosts are still found.
+
+    This behaviour is, however, controlled by an option called host_lookup_
+    order, which defaults to "bydns:byaddr". If people want to use the other
+    order, or indeed, just use one or the other means of lookup, they can
+    specify it in this variable.
+
+ 6. If a PTR record yields an empty name, Exim treats it as non-existent. In
+    some operating systems, this comes back from gethostbyaddr() as an empty
+    string, and this is what Exim used to test for. However, it seems that in
+    other systems, "." is yielded. Exim now tests for this case too.
+
+ 7. The values of check_spool_space and check_log_space are now held internally
+    as a number of kilobytes instead of an absolute number of bytes. If a
+    numbers is specified without 'K' or 'M', it is rounded up to the nearest
+    kilobyte. This means that much larger values can be stored.
+
+ 8. Exim monitor: an attempt to get the action menu when not actually pointing
+    at a message produces an empty menu entitled "No message selected". This
+    works on Solaris (OpenWindows). However, XFree86 does not like a menu with
+    no entries in it ("Shell widget menu has zero width and/or height"). So I
+    have added a single, blank menu entry in this case.
+
+ 9. Added ${quote_local_part.
+
+10. MIME decoding is now applied to the contents of Subject: header lines when
+    they are logged.
+
+11. Now that a reference to $sender_host_address automatically causes a reverse
+    lookup to occur if necessary (4.13/18), there is no need to arrange for a
+    host lookup before query-style lookups in lists that might use this
+    variable. This has therefore been abolished, and the "net-" prefix is no
+    longer necessary for query-style lookups.
+
+12. The Makefile for SCO_SV contained a setting of LDFLAGS. This appears to
+    have been a typo for LFLAGS, so it has been changed.
+
+13. The install script calls Exim with "-C /dev/null" in order to find the
+    version number. If ALT_CONFIG_PREFIX was set, this caused an error message
+    to be output. Howeve, since Exim outputs its version number before the
+    error, it didn't break the script. It just looked ugly. I fixed this by
+    always allowing "-C /dev/null" if the caller is root.
+
+14. Ignore overlarge ACL variable number when reading spool file - insurance
+    against a later release with more variables having written the file.
+
+15. The standard form for an IPv6 address literal was being rejected by EHLO.
+    Example: [IPv6:2002:c1ed:8229:10:202:2dff:fe07:a42a]. Exim now accepts
+    this, as well as the form without the "IPv6" on the front.
+
+16. Added CHOWN_COMMAND=/usr/sbin/chown and LIBS=-lresolv to the
+    OS/Makefile-Darwin file.
+
+17. Fixed typo in lookups/ldap.c: D_LOOKUP should be D_lookup. This applied
+    only to LDAP libraries that do not have LDAP_OPT_DEREF.
+
+18. After change 4.21/52, "%ld" was used to format the contents of the $inode
+    variable. However, some OS use ints for inodes. I've added cast to long int
+    to get rid of the compiler warning.
+
+19. I had forgotten to lock out "/../" in configuration file names when
+    ALT_CONFIG_PREFIX was set.
+
+20. Routers used for verification do not need to specify transports. However,
+    if such a router generated a host list, and callout was configured, Exim
+    crashed, because it could not find a port number from the (non-existent)
+    transport. It now assumes port 25 in this circumstance.
+
+21. Added the -t option to exigrep.
+
+22. If LOOKUP_LSEARCH is defined, all three linear search methods (lsearch,
+    wildlsearch, nwildlsearch) are compiled. LOOKUP_WILDLSEARCH and LOOKUP_
+    NWILDLSEARCH are now obsolete, but retained for compatibility. If either of
+    them is set, LOOKUP_LSEARCH is forced.
+
+23. "exim -bV" now outputs a list of lookups that are included in the binary.
+
+24. Added sender and host information to the "rejected by local_scan()" log
+    line; previously there was no indication of these.
+
+25. Added .include_if_exists.
+
+26. Change 3.952/11 added an explicit directory sync on top of a file sync for
+    Linux. It turns out that not all file systems support this. Apparently some
+    versions of NFS do not. (It's rare to put Exim's spool on NFS, but people
+    do it.) To cope with this, the error EINVAL, which means that sync-ing is
+    not supported on the file descriptor, is now ignored when Exim is trying to
+    sync a directory. This applies only to Linux.
+
+27. Added -DBIND_8_COMPAT to the CLFAGS setting for Darwin.
+
+28. In Darwin (MacOS X), the PAM headers are in /usr/include/pam and not in
+    /usr/include/security. There's now a flag in OS/os.h-Darwin to cope with
+    this.
+
+29. Added support for maildirsize files from supplied patch (modified a bit).
+
+30. The use of :fail: followed by an empty string could lead Exim to respond to
+    sender verification failures with (e.g.):
+
+      550 Verification failed for <xxx>
+      550 Sender verify failed
+
+    where the first response line was missing the '-' that indicates it is not
+    the final line of the response.
+
+31. The loop for finding the name of the user that called Exim had a hardwired
+    limit of 10; it now uses the value of finduser_retries, which is used for
+    all other user lookups.
+
+32. Added $received_count variable, available in data and not_smtp ACLs, and at
+    delivery time.
+
+33. Exim was neglecting to zero errno before one call of strtol() when
+    expanding a string and expecting an integer value. On some systems this
+    resulted in spurious "integer overflow" errors. Also, it was casting the
+    result into an int without checking.
+
+34. Testing for a connection timeout using "timeout_connect" in the retry rules
+    did not work. The code looks as if it has *never* worked, though it appears
+    to have been documented since at least releast 1.62. I have made it work.
+
+35. The "timeout_DNS" error in retry rules, also documented since at least
+    1.62, also never worked. As it isn't clear exactly what this means, and
+    clearly it isn't a major issue, I have abolished the feature by treating it
+    as "timeout", and writing a warning to the main and panic logs.
+
+36. The display of retry rules for -brt wasn't always showing the error code
+    correctly.
+
+37. Added new error conditions to retry rules: timeout_A, timeout_MX,
+    timeout_connect_A, timeout_connect_MX.
+
+38. Rewriting the envelope sender at SMTP time did not allow it to be rewritten
+    to the empty sender.
+
+39. The daemon was not analysing the content of -oX till after it had closed
+    stderr and disconnected from the controlling terminal. This meant that any
+    syntax errors were only noted on the panic log, and the return code from
+    the command was 0. By re-arranging the code a little, I've made the
+    decoding happen first, so such errors now appear on stderr, and the return
+    code is 1. However, the actual setting up of the sockets still happens in
+    the disconnected process, so errors there are still only recorded on the
+    panic log.
+
+40. A daemon listener on a wildcard IPv6 socket that also accepts IPv4
+    connections (as happens on some IP stacks) was logged at start up time as
+    just listening for IPv6. It now logs "IPv6 with IPv4". This differentiates
+    it from "IPv6 and IPv4", which means that two separate sockets are being
+    used.
+
+41. The debug output for gethostbyname2() or getipnodebyname() failures now
+    says whether AF_INET or AF_INET6 was passed as an argument.
+
+42. Exiwhat output was messed up when time zones were included in log
+    timestamps.
+
+43. Exiwhat now gives more information about the daemon's listening ports,
+    and whether -tls-on-connect was used.
+
+44. The "port" option of the smtp transport is now expanded.
+
+45. A "message" modifier in a "warn" statement in a non-message ACL was being
+    silently ignored. Now an error message is written to the main and panic
+    logs.
+
+46. There's a new ACL modifier called "logwrite" which writes to a log file
+    as soon as it is encountered.
+
+47. Added $local_user_uid and $local_user_gid at routing time.
+
+48. Exim crashed when trying to verify a sender address that was being
+    rewritten to "<>".
+
+49. Exim was recognizing only a space character after ".include". It now also
+    recognizes a tab character.
+
+50. Fixed several bugs in the Perl script that creates the exim.8 man page by
+    extracting the relevant information from the specification. The man page no
+    longer contains scrambled data for the -d option, and I've added a section
+    at the front about calling Exim under different names.
+
+51. Added "extra_headers" argument to the "mail" command in filter files.
+
+52. Redirecting mail to an unqualified address in a Sieve filter caused Exim to
+    crash.
+
+53. Installed eximstats 1.29.
+
+54. Added transport_filter_timeout as a generic transport option.
+
+55. Exim no longer adds an empty Bcc: header to messages that have no To: or
+    Cc: header lines. This was required by RFC 822, but it not required by RFC
+    2822.
+
+56. Exim used to add From:, Date:, and Message-Id: header lines to any
+    incoming messages that did not have them. Now it does so only if the
+    message originates locally, that is, if there is no associated remote host
+    address. When Resent- header lines are present, this applies to the Resent-
+    lines rather than the non-Resent- lines.
+
+57. Drop incoming SMTP connection after too many syntax or protocol errors. The
+    limit is controlled by smtp_max_synprot_errors, defaulting to 3.
+
+58. Messages for configuration errors now include the name of the main
+    configuration file - useful now that there may be more than one file in a
+    list (.included file names were always shown).
+
+59. Change 4.21/82 (run initgroups() when starting the daemon) causes problems
+    for those rare installations that do not start the daemon as root or run it
+    setuid root. I've cut out the call to initgroups() if the daemon is not
+    root at that time.
+
+60. The Exim user and group can now be bound into the binary as text strings
+    that are looked up at the start of Exim's processing.
+
+61. Applied a small patch for the Interbase code, supplied by Ard Biesheuvel.
+
+62. Added $mailstore_basename variable.
+
+63. Installed patch to sieve.c from Michael Haardt.
+
+64. When Exim failed to open the panic log after failing to open the main log,
+    the original message it was trying to log was written to stderr and debug
+    output, but if they were not available (the usual case in production), it
+    was lost. Now it is written to syslog before the two lines that record the
+    failures to open the logs.
+
+65. Users' Exim filters run in subprocesses under the user's uid. It is
+    possible for a "deliver" command or an alias in a "personal" command to
+    provoke an address rewrite. If logging of address rewriting is configured,
+    this fails because the process is not running as root or exim. There may be
+    a better way of dealing with this, but for the moment (because 4.30 needs
+    to be released), I have disabled address rewrite logging when running a
+    filter in a non-root, non-exim process.
+
+
+Exim version 4.24
+-----------------
+
+ 1. The buildconfig auxiliary program wasn't quoting the value set for
+    HEADERS_CHARSET. This caused a compilation error complaining that 'ISO' was
+    not defined. This bug was masked in 4.22 by the effect that was fixed in
+    change 4.23/1.
+
+ 2. Some messages that were rejected after a message id was allocated were
+    shown as "incomplete" by exigrep. It no longer does this for messages that
+    are rejected by local_scan() or the DATA or non-SMTP ACLs.
+
+ 3. If a Message-ID: header used a domain literal in the ID, and Exim did not
+    have allow_domain_literals set, the ID did not get logged in the <= line.
+    Domain literals are now always recognized in Message-ID: header lines.
+
+ 4. The first argument for a ${extract expansion item is the key name or field
+    number. Leading and trailing spaces in this item were not being ignored,
+    causing some misleading effects.
+
+ 5. When deliver_drop_privilege was set, single queue runner processes started
+    manually (i.e. by the command "exim -q") or by the daemon (which uses the
+    same command in the process it spins off) were not dropping privilege.
+
+ 6. When the daemon running as "exim" started a queue runner, it always
+    re-executed Exim in the spun-off process. This is a waste of effort when
+    deliver_drop_privilege is set. The new process now just calls the
+    queue-runner function directly.
+
+
+Exim version 4.23
+-----------------
+
+ 1. Typo in the src/EDITME file: it referred to HEADERS_DECODE_TO instead of
+    HEADERS_CHARSET.
+
+ 2. Change 4.21/73 introduced a bug. The pid file path set by -oP was being
+    ignored. Though the use of -oP was forcing the writing of a pid file, it
+    was always written to the default place.
+
+ 3. If the message "no IP address found for host xxxx" is generated during
+    incoming verification, it is now followed by identification of the incoming
+    connection (so you can more easily find what provoked it).
+
+ 4. Bug fix for Sieve filters: "stop" inside a block was not working properly.
+
+ 5. Added some features to "harden" Exim a bit more against certain attacks:
+
+    (a) There is now a build-time option called FIXED_NEVER_USERS that can
+        be put in Local/Makefile. This is like the never_users runtime option,
+        but it cannot be overridden. The default setting is "root".
+
+    (b) If ALT_CONFIG_PREFIX is defined in Local/Makefile, it specifies a
+        prefix string with which any file named in a -C command line option
+        must start.
+
+    (c) If ALT_CONFIG_ROOT_ONLY is defined in Local/Makefile, root privilege
+        is retained for -C and -D only if the caller of Exim is root. Without
+        it, the exim user may also use -C and -D and retain privilege.
+
+    (d) If DISABLE_D_OPTION is defined in Local/Makefile, the use of the -D
+        command line option is disabled.
+
+ 6. Macro names set by the -D option must start with an upper case letter, just
+    like macro names defined in the configuration file.
+
+ 7. Added "dereference=" facility to LDAP.
+
+ 8. Two instances of the typo "uknown" in the source files are fixed.
+
+ 9. If a PERL_COMMAND setting in Local/Makefile was not at the start of a line,
+    the Configure-Makefile script screwed up while processing it.
+
+10. Incorporated PCRE 4.4.
+
+11. The SMTP synchronization check was not operating right at the start of an
+    SMTP session. For example, it could not catch a HELO sent before the client
+    waited for the greeting. There is now a check for outstanding input at the
+    point when the greeting is written. Because of the duplex, asynchronous
+    nature of TCP/IP, it cannot be perfect - the incorrect input may be on its
+    way, but not yet received, when the check is performed.
+
+12. Added tcp_nodelay to make it possible to turn of the setting of TCP_NODELAY
+    on TCP/IP sockets, because this apparently causes some broken clients to
+    timeout.
+
+13. Installed revised OS/Makefile-CYGWIN and OS/os.c-cygwin (the .h file was
+    unchanged) from the Cygwin maintainer.
+
+14. The code for -bV that shows what is in the binary showed "mbx" when maildir
+    was supported instead of testing for mbx. Effectively a typo.
+
+15. The spa authenticator server code was not checking that the input it
+    received was valid base64.
+
+16. The debug output line for the "set" modifier in ACLs was not showing the
+    name of the variable that was being set.
+
+17. Code tidy: the variable type "vtype_string" was never used. Removed it.
+
+18. Previously, a reference to $sender_host_name did not cause a DNS reverse
+    lookup on its own. Something else was needed to trigger the lookup. For
+    example, a match in host_lookup or the need for a host name in a host list.
+    Now, if $sender_host_name is referenced and the host name has not yet been
+    looked up, a lookup is performed. If the lookup fails, the variable remains
+    empty, and $host_lookup_failed is set to "1".
+
+19. Added "eqi" as a case-independent comparison operator.
+
+20. The saslauthd authentication condition could segfault if neither service
+    nor realm was specified.
+
+21. If an overflowing value such as "2048M" was set for message_size_limit, the
+    error message that was logged was misleading, and incoming SMTP
+    connections were dropped. The message is now more accurate, and temporary
+    errors are given to SMTP connections.
+
+22. In some error situations (such as 21 above) Exim rejects all SMTP commands
+    (except RSET) with a 421 error, until QUIT is received. However, it was
+    failing to send a response to QUIT.
+
+23. The HELO ACL was being run before the code for helo_try_verify_hosts,
+    which made it impossible to use "verify = helo" in the HELO ACL. The HELO
+    ACL is now run after the helo_try_verify_hosts code.
+
+24. "{MD5}" and "{SHA1}" are now recognized as equivalent to "{md5"} and
+    "{sha1}" in the "crypteq" expansion condition (in fact the comparison is
+    case-independent, so other case variants are also recognized). Apparently
+    some systems use these upper case variants.
+
+25. If more than two messages were waiting for the same host, and a transport
+    filter was specified for the transport, Exim sent two messages over the
+    same TCP/IP connection, and then failed with "socket operation on non-
+    socket" when it tried to send the third.
+
+26. Added Exim::debug_write and Exim::log_write for embedded Perl use.
+
+27. The extern definition of crypt16() in expand.c was not being excluded when
+    the OS had its own crypt16() function.
+
+28. Added bounce_return_body as a new option, and bounce_return_size_limit
+    as a preferred synonym for return_size_limit, both as an option and as an
+    expansion variable.
+
+29. Added LIBS=-liconv to OS/Makefile-OSF1.
+
+30. Changed the default configuration ACL to relax the local part checking rule
+    for addresses that are not in any local domains. For these addresses,
+    slashes and pipe symbols are allowed within local parts, but the sequence
+    /../ is explicitly forbidden.
+
+31. SPA server authentication was not clearing the challenge buffer before
+    using it.
+
+32. log_message in a "warn" ACL statement was writing to the reject log as
+    well as to the main log, which contradicts the documentation and doesn't
+    seem right (because no rejection is happening). So I have stopped it.
+
+33. Added Ard Biesheuvel's lookup code for accessing an Interbase database.
+    However, I am unable to do any testing of this.
+
+34. Fixed an infelicity in the appendfile transport. When checking directories
+    for a mailbox, to see if any needed to be created, it was accidentally
+    using path names with one or more superfluous leading slashes; tracing
+    would show up entries such as stat("///home/ph10", 0xFFBEEA48).
+
+35. If log_message is set on a "discard" verb in a MAIL or RCPT ACL, its
+    contents are added to the log line that is written for every discarded
+    recipient. (Previously a log_message setting was ignored.)
+
+36. The ${quote: operator now quotes the string if it is empty.
+
+37. The install script runs exim in order to find its version number. If for
+    some reason other than non-existence or emptiness, which it checks, it
+    could not run './exim', it was installing it with an empty version number,
+    i.e. as "exim-". This error state is now caught, and the installation is
+    aborted.
+
+38. An argument was missing from the function that creates an error message
+    when Exim fails to connect to the socket for saslauthd authentication.
+    This could cause Exim to crash, or give a corrupted message.
+
+39. Added isip, isip4, and isip6 to ${if conditions.
+
+40. The ACL variables $acl_xx are now saved with the message, and can be
+    accessed later in routers, transports, and filters.
+
+41. The new lookup type nwildlsearch is like wildlsearch, except that the key
+    strings in the file are not string-expanded.
+
+42. If a MAIL command specified a SIZE value that was too large to fit into an
+    int variable, the check against message_size_limit failed. Such values are
+    now forced to INT_MAX, which is around 2Gb for a 32-bit variable. Maybe one
+    day this will have to be increased, but I don't think I want to be around
+    when emails are that large.
+
+
+
+Exim version 4.22
+-----------------
+
+ 1. Removed HAVE_ICONV=yes from OS/Makefile-FreeBSD, since it seems that
+    iconv() is not standard in FreeBSD.
+
+ 2. Change 4.21/17 was buggy and could cause stack overwriting on a system with
+    IPv6 enabled. The observed symptom was a segmentation fault on return from
+    the function os_common_find_running_interfaces() in src/os.c.
+
+ 3. In the check_special_case() function in daemon.c I had used "errno" as an
+    argument name, which causes warnings on some systems. This was basically a
+    typo, since it was named "eno" in the comments!
+
+ 4. The code that waits for the clock to tick (at a resolution of some fraction
+    of a second) so as to ensure message-id uniqueness was always waiting for
+    at least one whole tick, when it could have waited for less. [This is
+    almost certainly not relevant at current processor speeds, where it is
+    unlikely to ever wait at all. But we try to future-proof.]
+
+ 5. The function that sleeps for a time interval that includes fractions of a
+    second contained a race. It did not block SIGALRM between setting the
+    timer, and suspending (a couple of lines later). If the interval was short
+    and the sigsuspend() was delayed until after it had expired, the suspension
+    never ended. On busy systems this could lead to processes getting stuck for
+    ever.
+
+ 6. Some uncommon configurations may cause a lookup to happen in a queue runner
+    process, before it forks any delivery processes. The open lookup caching
+    mechanism meant that the open file or database connection was passed into
+    the delivery process. The problem was that delivery processes always tidy
+    up cached lookup data. This could cause a problem for the next delivery
+    process started by the queue runner, because the external queue runner
+    process does not know about the closure. So the next delivery process
+    still has data in the lookup cache. In the case of a file lookup, there was
+    no problem because closing a file descriptor in a subprocess doesn't affect
+    the parent. However, if the lookup was caching a connection to a database,
+    the connection was closed, and the second delivery process was likely to
+    see errors such as "PGSQL: query failed: server closed the connection
+    unexpectedly". The problem has been fixed by closing all cached lookups
+    in a queue runner before running a delivery process.
+
+ 7. Compiler warning on Linux for the second argument of iconv(), which doesn't
+    seem to have the "const" qualifier which it has on other OS. I've
+    parameterised it.
+
+ 8. Change 4.21/2 was too strict. It is only if there are two authenticators
+    *of the same type* (client or server) with the same public name that an
+    error should be diagnosed.
+
+ 9. When Exim looked up a host name for an IP address, but failed to find the
+    original IP address when looking up the host name (a safety check), it
+    output the message "<ip address> does not match any IP for NULL", which was
+    confusing, to say the least. The bug was that the host name should have
+    appeared instead of "NULL".
+
+10. Since release 3.03, if Exim is called by a uid other than root or the Exim
+    user that is built into the binary, and the -C or -D options is used, root
+    privilege is dropped before the configuration file is read. In addition,
+    logging is switched to stderr instead of the normal log files. If the
+    configuration then re-defines the Exim user, the unprivileged environment
+    is probably not what is expected, so Exim logs a panic warning message (but
+    proceeds).
+
+    However, if deliver_drop_privilege is set, the unprivileged state may well
+    be exactly what is intended, so the warning has been cut out in that case,
+    and Exim is allowed to try to write to its normal log files.
+
+
+Exim version 4.21
+-----------------
+
+ 1. smtp_return_error_details was not giving details for temporary sender
+    or receiver verification errors.
+
+ 2. Diagnose a configuration error if two authenticators have the same public
+    name.
+
+ 3. Exim used not to create the message log file for a message until the first
+    delivery attempt. This could be confusing when incoming messages were held
+    for policy or load reasons. The message log file is now created at the time
+    the message is received, and an initial "Received" line is written to it.
+
+ 4. The automatically generated man page for command line options had a minor
+    bug that caused no ill effects; however, a more serious problem was that
+    the procedure for building the man page automatically didn't always
+    operate. Consequently, release 4.20 contains an out-of-date version. This
+    shouldn't happen again.
+
+ 5. When building Exim with embedded Perl support, the script that builds the
+    Makefile was calling 'perl' to find its compile-time parameters, ignoring
+    any setting of PERL_COMMAND in Local/Makefile. This is now fixed.
+
+ 6. The freeze_tell option was not being used for messages that were frozen on
+    arrival, either by an ACL or by local_scan().
+
+ 7. Added the smtp_incomplete_transaction log selector.
+
+ 8. After STARTTLS, Exim was not forgetting that it had advertised AUTH, so it
+    was accepting AUTH without a new EHLO.
+
+ 9. Added tls_remember_esmtp to cope with YAEB. This allows AUTH and other
+    ESMTP extensions after STARTTLS without a new EHLO, in contravention of the
+    RFC.
+
+10. Logging of TCP/IP connections (when configured) now happens in the main
+    daemon process instead of the child process, so that the TCP/IP connection
+    count is more accurate (but it can never be perfect).
+
+11. The use of "drop" in a nested ACL was not being handled correctly in the
+    outer ACL. Now, if condition failure induced by the nested "drop" causes
+    the outer ACL verb to deny access ("accept" or "discard" after "endpass",
+    or "require"), the connection is dropped.
+
+12. Similarly, "discard" in a nested ACL wasn't being handled. A nested ACL
+    that yield "discard" can now be used with an "accept" or a "discard" verb,
+    but an error is generated for any others (because I can't see a useful way
+    to define what should happen).
+
+13. When an ACL is read dynamically from a file (or anywhere else), the lines
+    are now processed in the same way as lines in the Exim configuration file.
+    In particular, continuation lines are supported.
+
+14. Added the "dnslists = a.b.c!=n.n.n.n" feature.
+
+15. Added -ti meaning -t -i.
+
+16. Check for letters, digits, hyphens, and dots in the names of dnslist
+    domains, and warn by logging if others are found.
+
+17. At least on BSD, alignment is not guarenteed for the array of ifreq's
+    returned from GIFCONF when Exim is trying to find the list of interfaces on
+    a host. The code in os.c has been modified to copy each ifreq to an aligned
+    structure in all cases.
+
+    Also, in some cases, the returned ifreq's were being copied to a 'struct
+    ifreq' on the stack, which was subsequently passed to host_ntoa(). That
+    means the last couple of bytes of an IPv6 address could be chopped if the
+    ifreq contained only a normal sockaddr (14 bytes storage).
+
+18. Named domain lists were not supported in the hosts_treat_as_local option.
+    An entry such as +xxxx was not recognized, and was treated as a literal
+    domain name.
+
+19. Ensure that header lines added by a DATA ACL are included in the reject log
+    if the ACL subsequently rejects the message.
+
+20. Upgrade the cramtest.pl utility script to use Digest::MD5 instead of just
+    MD5 (which is deprecated).
+
+21. When testing a filter file using -bf, Exim was writing a message when it
+    took the sender from a "From " line in the message, but it was not doing so
+    when it took $return_path from a Return-Path: header line. It now does.
+
+22. If the contents of a "message" modifier for a "warn" ACL verb do not begin
+    with a valid header line field name (a series of printing characters
+    terminated by a colon, Exim now inserts X-ACL-Warn: at the beginning.
+
+23. Changed "disc" in the source to "disk" to conform to the documentation and
+    the book and for uniformity.
+
+24. Ignore Sendmail's -Ooption=value command line item.
+
+25. When execve() failed while trying to run a command in a pipe transport,
+    Exim was returning EX_UNAVAILBLE (69) from the subprocess. However, this
+    could be confused with a return value of 69 from the command itself. This
+    has been changed to 127, the value the shell returns if it is asked to run
+    a non-existent command. The wording for the related log line suggests a
+    non-existent command as the problem.
+
+26. If received_header_text expands to an empty string, do not add a Received:
+    header line to the message. (Well, it adds a token one on the spool, but
+    marks it "old" so that it doesn't get used or transmitted.)
+
+27. Installed eximstats 1.28 (addition of -nt option).
+
+28. There was no check for failure on the call to getsockname() in the daemon
+    code. This can fail if there is a shortage of resources on the system, with
+    ENOMEM, for example. A temporary error is now given on failure.
+
+29. Contrary to the C standard, it seems that in some environments, the
+    equivalent of setlocale(LC_ALL, "C") is not obeyed at the start of a C
+    program. Exim now does this explicitly; it affects the formatting of
+    timestamps using strftime().
+
+30. If exiqsumm was given junk data, it threw up some uninitialized variable
+    complaints. I've now initialized all the variables, to avoid this.
+
+32. Header lines added by a system filter were not being "seen" during
+    transport-time rewrites.
+
+33. The info_callback() function passed to OpenSSL is set up with type void
+    (*)(SSL *, int, int), as described somewhere. However, when calling the
+    function (actually a macro) that sets it up, the type void(*)() is
+    expected. I've put in a cast to prevent warnings from picky compilers.
+
+34. If a DNS black list lookup found a CNAME record, but there were no A
+    records associated with the domain it pointed at, Exim crashed.
+
+35. If a DNS black list lookup returned more than one A record, Exim ignored
+    all but the first. It now scans all returned addresses if a particular IP
+    value is being sought. In this situation, the contents of the
+    $dnslist_value variable are a list of all the addresses, separated by a
+    comma and a space.
+
+36. Tightened up the rules for host name lookups using reverse DNS. Exim used
+    to accept a host name and all its aliases if the forward lookup for any of
+    them yielded the IP address of the incoming connection. Now it accepts only
+    those names whose forward lookup yields the correct IP address. Any other
+    names are discarded. This closes a loophole whereby a rogue DNS
+    administrator could create reverse DNS records to break through a
+    wildcarded host restriction in an ACL.
+
+37. If a user filter or a system filter that ran in a subprocess used any of
+    the numerical variables ($1, $2 etc), or $thisaddress, in a pipe command,
+    the wrong values were passed to the pipe command ($thisaddress had the
+    value of $0, $0 had the value of $1, etc). This bug was introduced by
+    change 4.11/101, and not discovered because I wrote an inadequate test. :-(
+
+38. Improved the line breaking for long SMTP error messages from ACLs.
+    Previously, if there was no break point between 40 and 75 characters, Exim
+    left the rest of the message alone. Two changes have been made: (a) I've
+    reduced the minimum length to 35 characters; (b) if it can't find a break
+    point between 35 and 75 characters, it looks ahead and uses the first one
+    that it finds. This may give the occasional overlong line, but at least the
+    remaining text gets split now.
+
+39. Change 82 of 4.11 was unimaginative. It assumed the limit on the number of
+    file descriptors might be low, and that setting 1000 would always raise it.
+    It turns out that in some environments, the limit is already over 1000 and
+    that lowering it causes trouble. So now Exim takes care not to decrease it.
+
+40. When delivering a message, the value of $return_path is set to $sender_
+    address at the start of routing (routers may change the value). By an
+    oversight, this default was not being set up when an address was tested by
+    -bt or -bv, which affected the outcome if any router or filter referred to
+    $return_path.
+
+41. The idea of the "warn" ACL verb is that it adds a header or writes to the
+    log only when "message" or "log_message" are set. However, if one of the
+    conditions was an address verification, or a call to a nested ACL, the
+    messages generated by the underlying test were being passed through. This
+    no longer happens. The underlying message is available in $acl_verify_
+    message for both "message" and "log_message" expansions, so it can be
+    passed through if needed.
+
+42. Added RFC 2047 interpretation of header lines for $h_ expansions, with a
+    new expansion $bh_ to give the encoded byte string without charset
+    translation. Translation happens only if iconv() is available; HAVE_ICONV
+    indicates this at build time. HEADERS_CHARSET gives the charset to
+    translate to; headers_charset can change it in the configuration, and
+    "headers charset" can change it in an individual filter file.
+
+43. Now that we have a default RFC 2047 charset (see above), the code in Exim
+    that creates RFC 2047 encoded "words" labels them as that charset instead
+    of always using iso-8859-1. The cases are (i) the explicit ${rfc2047:
+    expansion operator; (ii) when Exim creates a From: line for a local
+    message; (iii) when a header line is rewritten to include a "phrase" part.
+
+44. Nasty bug in exiqsumm: the regex to skip already-delivered addresses was
+    buggy, causing it to skip the first lines of messages whose message ID
+    ended in 'D'. This would not have bitten before Exim release 4.14, because
+    message IDs were unlikely to end in 'D' before then. The effect was to have
+    incorrect size information for certain domains.
+
+45. #include "config.h" was missing at the start of the crypt16.c module. This
+    caused trouble on Tru64 (aka OSF1) systems, because HAVE_CRYPT16 was not
+    noticed.
+
+46. If there was a timeout during a "random" callout check, Exim treated it as
+    a failure of the random address, and carried on sending RSET and the real
+    address. If the delay was just some slowness somewhere, the response to the
+    original RCPT would be taken as a response to RSET and so on, causing
+    mayhem of various kinds.
+
+47. Change 50 for 4.20 was a heap of junk. I don't know what I was thinking
+    when I implemented it. It didn't allow for the fact that some option values
+    may legitimatetly be negative (e.g. size_addition), and it didn't even do
+    the right test for positive values.
+
+48. Domain names in DNS records are case-independent. Exim always looks them up
+    in lower case. Some resolvers return domain names in exactly the case they
+    appear in the zone file, that is, they may contain uppercase letters. Not
+    all resolvers do this - some return always lower case. Exim was treating a
+    change of case by a resolver as a change of domain, similar to a widening
+    of a domain abbreviation. This triggered its re-routing code and so it was
+    trying to route what was effectively the same domain again. This normally
+    caused routing to fail (because the router wouldn't handle the domain
+    twice). Now Exim checks for this case specially, and just changes the
+    casing of the domain that it ultimately uses when it transmits the message
+    envelope.
+
+49. Added Sieve (RFC 3028) support, courtesy of Michael Haardt's contributed
+    module.
+
+50. If a filter generated a file delivery with a non-absolute name (possible if
+    no home directory exists for the router), the forbid_file option was not
+    forbidding it.
+
+51. Added '&' feature to dnslists, to provide bit mask matching in addition to
+    the existing equality matching.
+
+52. Exim was using ints instead of ino_t variables in some places where it was
+    dealing with inode numbers.
+
+53. If TMPDIR is defined in Local/Makefile (default in src/EDITME is
+    TMPDIR="/tmp"), Exim checks for the presence of an environment variable
+    called TMPDIR, and if it finds it is different, it changes its value.
+
+54. The smtp_printf() function is now made available to local_scan() so
+    additional output lines can be written before returning. There is also an
+    smtp_fflush() function to enable the detection of a dropped connection.
+    The variables smtp_input and smtp_batched_input are exported to
+    local_scan().
+
+55. Changed the default runtime configuration: the message "Unknown user"
+    has been removed from the ACL, and instead placed on the localuser router,
+    using the cannot_route_message feature. This means that any verification
+    failures that generate their own messages won't get overridden. Similarly,
+    the "Unrouteable address" message that was in the ACL for unverifiable
+    relay addresses has also been removed.
+
+56. Added hosts_avoid_esmtp to the smtp transport.
+
+57. The exicyclog script was not checking for the esoteric option
+    CONFIGURE_FILE_USE_EUID in the Local/Makefile. It now does this, but it
+    will work only if exicyclog is run under the appropriate euid.
+
+58. Following a discussion on the list, the rules by which Exim recognises line
+    endings on incoming messages have been changed. The -dropcr and drop_cr
+    options are now no-ops, retained only for backwards compatibility. The
+    following line terminators are recognized: LF CRLF CR. However, special
+    processing applies to CR:
+
+    (i)  The sequence CR . CR does *not* terminate an incoming SMTP message,
+         nor a local message in the state where . is a terminator.
+
+    (ii) If a bare CR is encountered in a header line, an extra space is added
+         after the line terminator so as not to end the header. The reasoning
+         behind this is that bare CRs in header lines are most likely either
+         to be mistakes, or people trying to play silly games.
+
+59. The size of a message, as listed by "-bp" or in the Exim monitor window,
+    was being incorrectly given as 18 bytes larger than it should have been.
+    This is a VOB (very old bug).
+
+60. This may never have affected anything current, but just in case it has:
+    When the local host is found other than at the start of a list of hosts,
+    the local host, those with the same MX, and any that follow, are discarded.
+    When the list in question was part of a longer list of hosts, the following
+    hosts (not currently being processed) were also being discarded. This no
+    longer happens. I'm not sure if this situation could ever has previously
+    arisen.
+
+61. Added the "/MX" feature to lists of hosts in the manualroute and query
+    program routers.
+
+62. Whenever Exim generates a new message, it now adds an Auto-Submitted:
+    header. This is something that is recommended in a new Internet Draft, and
+    is something that is documented as being done by Sendmail. There are two
+    possible values. For messages generated by the autoreply transport, Exim
+    adds:
+
+      Auto-Submitted: auto-replied
+
+    whereas for all other generated messages (e.g. bounces) it adds
+
+      Auto-Submitted: auto-generated
+
+63. The "personal" condition in filters now includes a test for the
+    Auto-Submitted: header. If it contains the string "auto-" the message it
+    not considered personal.
+
+64. Added rcpt_include_affixes as a generic transport option.
+
+65. Added queue_only_override (default true).
+
+66. Added the syslog_duplication option.
+
+67. If what should have been the first header line of a message consisted of
+    a space followed by a colon, Exim was mis-interpreting it as a header line.
+    It isn't of course - it is syntactically invalid and should therefore be
+    treated as the start of the message body. The misbehaviour could have
+    caused a number of strange effects, including loss of data in subsequent
+    header lines, and spool format errors.
+
+68. Formerly, the AUTH parameter on a MAIL command was trusted only if the
+    client host had authenticated. This control can now be exercised by an ACL
+    for more flexibility.
+
+69. By default, callouts do not happen when testing with -bh. There is now a
+    variant, -bhc, which does actually run the callout code, including
+    consulting and updating the callout cache.
+
+70. Added support for saslauthd authentication, courtesy of Alexander
+    Sabourenkov.
+
+71. If statvfs() failed on the spool or log directories while checking their
+    size for availability, Exim confusingly gave the error "space shortage".
+    Furthermore, in debugging mode it crashed with a floating point exception.
+    These checks are done if check_{spool,log}_{space,inodes} are set, and when
+    an SMTP message arrives with SIZE= on the MAIL command. As this is a really
+    serious problem, Exim now writes to the main and panic logs when this
+    happens, with details of the failure. It then refuses to accept the
+    incoming message, giving the message "spool directory problem" or "log
+    directory problem" with a 421 code for SMTP messages.
+
+72. When Exim is about to re-exec itself, it ensures that the file descriptors
+    0, 1, and 2 exist, because some OS complain for execs without them (see
+    ChangeLog 4.05/30). If necessary, Exim opens /dev/null to use for these
+    descriptors. However, the code omitted to check that the open succeeded,
+    causing mysterious errors if for some reason the permissions on /dev/null
+    got screwed. Now Exim writes a message to the main and panic logs, and
+    bombs out if it can't open /dev/null.
+
+73. Re-vamped the way daemon_smtp_port, local_interfaces, and -oX work and
+    interact so that it is all more flexible. It is supposed to remain
+    backwards compatible. Also added extra_local_interfaces.
+
+74. Invalid data sent to a SPA (NTLM) server authenticator could cause the code
+    to bomb out with an assertion failure - to the client this appears as a
+    connection drop. This problem occurs in the part of the code that was taken
+    from the Samba project. Fortunately, the assertion is in a very simple
+    function, so I have fixed this by reproducing the function inline in the
+    one place where it is called, and arranging for authentication to fail
+    instead of killing the process with assert().
+
+75. The SPA client code was not working when the server requested OEM rather
+    than Unicode encoding.
+
+76. Added code to make require_files with a specific uid setting more usable in
+    the case where statting the file as root fails - usually a non-root-mounted
+    NFS file system. When this happens and the failure is EACCES, Exim now
+    forks a subprocess and does the per-uid checking as the relevant uid.
+
+77. Added process_log_path.
+
+78. If log_file_path was not explicitly set, a setting of check_log_space or
+    check_log_inodes was ignored.
+
+79. If a space check for the spool or log partitions fails, the incident is now
+    logged. Of course, in the latter case the data may get lost...
+
+80. Added the %p formatting code to string_format() so that it can be used to
+    print addresses in debug_print(). Adjusted all the address printing in the
+    debugging in store.c to use %p rather than %d.
+
+81. There was a concern that a line of code in smtp_in.c could overflow a
+    buffer if a HELO/EHLO command was given followed by 500 or so spaces. As
+    initially expressed, the concern was not well-founded, because trailing
+    spaces are removed early. However, if the trailing spaces were followed by
+    a NULL, they did not get removed, so the overflow was possible. Two fixes
+    were applied:
+
+    (a) I re-wrote the offending code in a cleaner fashion.
+    (b) If an incoming SMTP command contains a NULL character, it is rejected
+        as invalid.
+
+82. When Exim changes uid/gid to the Exim user at daemon start time, it now
+    runs initgroups(), so that if the Exim user is in any additional groups,
+    they will be used during message reception.
+
+
+Exim version 4.20
+-----------------
+
+The change log for 4.20 and earlier releases has been archived.
+
+****
diff --git a/doc/Exim3.upgrade b/doc/Exim3.upgrade
new file mode 100644 (file)
index 0000000..5c5024a
--- /dev/null
@@ -0,0 +1,671 @@
+This document contains information about upgrading Exim to the last of the 3.xx
+releases. It is provided to help anybody who is upgrading to release 4.xx from
+a release that is earlier than 3.33. It goes back as far as release 2.12. If
+you are upgrading to release 4.xx from an even earlier release, it is probably
+best to start again from the default configuration.
+
+
+Upgrading from release 3.16
+---------------------------
+
+1. The way LDAP returns values for multiple attributes has been changed to be
+the same as the NIS+ lookup.
+
+If you specify multiple attributes, they are returned as space-separated
+strings, quoted if necessary.
+
+e.g.   ldap:///o=base?attr1,attr2?sub?(uid=fred)
+
+       used to give:    attr1=value one, attr2=value2
+       now gives:       attr1="value one" attr2=value2
+
+If you don't specify any attributes in the search, you now get them in
+the tagged format as well.
+
+e.g.   ldap:///o=base??sub?(uid=fred)
+
+       used to give:    top, value one, value2
+       now gives:       objectClass=top attr1="value one" attr2=value2
+
+The reason for these changes is so that the results can be safely parsed -
+in fact, the existing ${extract{key}{val}} function does this nicely.
+This in turn allows a single LDAP query to be reused - one query can return
+the destination delivery address, the quota, and so forth.
+
+This is NOT a backwards compatible change, so there is a compile-time option
+to reverse it in the src/lookups/ldap.c module, for use in emergency. But it is
+not thought that the old behaviour was particularly useful as it stood, because
+a field that contained ',' or '=' would make the result unparseable.
+
+In the common case where you explicitly ask for a single attribute in your
+LDAP query, the behaviour is unchanged - the result is not quoted, and if there
+are multiple values they are comma-separated.
+
+2. The hosts_max_try option in the smtp transport limits the number of IP
+addresses that will actually be tried during one delivery attempt. The default
+is 5. Previously, all available addresses were tried.
+
+3. The extension of the "extract" expansion item has resulted in a change to
+the way Exim decides between the keyed form and the numeric form. If the first
+argument consists entirely of digits, the numeric form is assumed. This means
+that it is impossible to have keys that are digit strings, without manipulating
+the data first (e.g. by using ${sg} to add a letter to each key).
+
+
+Upgrading from release 3.15
+---------------------------
+
+1. The handling of "freeze" and "fail" in system filter files has changed.
+Previously, any deliveries set up by a filter that ended with "freeze" or
+"fail" were discarded. This no longer happens; such deliveries are honoured.
+A consequence of this is that first_delivery becomes false after freezing in a
+system filter; previously it remained true until a real delivery attempt
+happened.
+
+
+Upgrading from release 3.13
+---------------------------
+
+1. The handling of maildir_tag has been changed (see NewStuff). There are two
+small incompatibilities: (a) Exim now inserts a leading colon only if the
+string begins with an alphanumeric character. So if you were using a string
+starting with a special character, you will have to add the leading colon to
+it to remain compatible. (b) The expansion of maildir_tag now happens after the
+file has been written, and $message_size is updated to the correct file size
+before the expansion. The tag is not used on the temporary file (it was
+previously).
+
+2. The handling of Exim's configuration has changed in two ways:
+
+  (a) Any line may be continued by ending it with a backslash. Trailing white
+  space after the backslash, and leading white space on continuation lines is
+  ignored. This means that quotes are no longer needed just to make it possible
+  to continue an option setting. The difference between quoted and non-quoted
+  strings is that quoted strings are processed for internal backslashed items
+  such as \n. The only possible incompatibility of this change is if any
+  existing configuration has a non-quoted line ended in backslash, which seems
+  a very remote possibility.
+
+  (b) All lists, with the exception of log_file_path, can now use a different
+  character to colon as the separator. This is specified by starting the list
+  with <x where x is any punctuation character. For example:
+
+    local_interfaces = <; 127.0.0.1 ; ::1
+
+  The new feature is provided to make life easier with IPv6 addresses. It is
+  recommended that its use be confined to circumstances where it really is
+  needed, and that colon be used in most cases. I don't believe this change
+  is incompatible, because I don't think any list item can legitimately begin
+  with a '<' character.
+
+3. Previously, Exim took no action to ensure that the timestamps in its log
+files were "wall clock time". If the TZ environment variable was set when Exim
+was called, it could cause strange times to be logged. For the majority of
+operating systems, I have been able to fix this problem by deleting the entire
+environment. However, this doesn't work in some systems, and a macro called
+HANDS_OFF_ENVIRONMENT is defined in their OS/os.h files to suppress the action.
+These OS are: AIX, DGUX, HP-UX, IRIX, and SCO, and their behaviour should be
+unchanged from previous releases. On any other OS, if you find you are getting
+weird timestamps, it may be that your OS needs HANDS_OFF_ENVIRONMENT.
+
+4. As a result of the change described in 3, there may be some cases where Exim
+runs an external program that previously got passed the environment, and now do
+not. This does *not* apply to the pipe transport, where the environment has
+always been set up specifically, as described in the manual.
+
+5. The way in which Exim scans its queue when split_spool_directory is set has
+changed, but this shouldn't make any noticeable difference. See doc/NewStuff
+for defails.
+
+
+Upgrading from release 3.03
+---------------------------
+
+The from_hack option in the appendfile and pipe transports has been replace by
+two string options, check_string and escape_string. If your configuration
+contains any references to from_hack they should be replaced. Exim continues to
+recognize from_hack as a transitional measure. If no_from_hack is specified in
+an appendfile transport, the two new options are forced to be unset. Otherwise
+the setting of from_hack is ignored.
+
+
+Upgrading from release 3.02
+---------------------------
+
+The exim_dbmbuild utility has been changed to write a warning to stderr on
+encountering a duplicate key, and to return a value of 1. Formerly, it ignored
+all but the last of a set of duplicates; now it ignores all but the first, to
+make dbm-searched files behave the same way as lsearch-searched files. However,
+there is an option -lastdup which makes it behave as before. The -nowarn option
+suppresses the individual warnings, but the number of duplicates is always
+listed on stdout at the end.
+
+
+Updating from a release prior to 3.00
+-------------------------------------
+
+Prior to release 3.00 a lot of options which contained lists of various kinds
+came in groups such as sender_accept, sender_reject, sender_reject_except. This
+style of configuration has been abolished. Instead, it is now possible to put
+negative entries in such lists, so that a single option is all that is
+required. In addition to this, net lists have been abolished, and instead,
+host lists can now contain items that specify networks as well as hosts. The
+names of some of these options have also been changed.
+
+As a result of these changes, most configuration files used for earlier
+versions of Exim need to be changed. The opportunity has therefore been taken
+to remove a number of other obsolete features and options.
+
+A Perl script is built in the file util/convert4r3 to assist in updating Exim
+configuration files. It reads a configuration file on the standard input,
+writes a modified file on the standard output, and writes comments about what
+it has done to the standard error file. It assumes that the input is a valid
+Exim configuration file. A typical call to the conversion script might be
+
+  util/convert4r3  </opt/exim/configure  >/opt/exim/configure.new
+
+The way the script merges an accept/reject/reject_except triple into a single
+accept option is to put the reject_except list first, followed by the reject
+list with every item negated, followed by the accept list. For example, if an
+old configuration file contains
+
+  sender_host_accept_relay        = *.c.d : e.f.g
+  sender_host_reject_relay        = *.b.c.d
+  sender_host_reject_relay_except = a.b.c.d
+
+the new configuration will contain
+
+  host_accept_relay = a.b.c.d : ! *.b.c.d : *.c.d : e.f.g
+
+The same ordering is used to merge a triple into a reject option, but this time
+the first and third sublists are negated. For example, if an old configuration
+file contains
+
+  sender_host_accept        = *.c.d : e.f.g
+  sender_host_reject        = *.b.c.d
+  sender_host_reject_except = a.b.c.d
+
+the new configuration file will contain
+
+  host_reject = ! a.b.c.d : *.b.c.d : ! *.c.d : ! e.f.g : *
+
+The output file should be checked before trying to use it. Each option change
+is preceded by an identifying comment. There are several specific things that
+you should look out for when checking:
+
+(1) If you are using macros to contain lists of items, and these have to be
+    negated in the new world, convert4r3 won't get it right. For example, if
+    the old configuration contains
+
+      ACCEPTHOSTS = *.c.d : e.f.g
+      sender_host_reject = ACCEPTHOSTS
+
+    then the rewritten configuration will be
+
+      ACCEPTHOSTS = *.c.d : e.f.g
+      host_reject = !ACCEPTHOSTS
+
+    but because this is just textual macro handling, that is equivalent to
+
+      host_reject = !*.c.d : e.f.g
+
+    which is not the correct translation, because the second item is not
+    negated. There is unfortunately no easy way to use a macro to provide a
+    list of things that are sometimes negated.
+
+(2) The conversion adds some settings of file_transport, pipe_transport, and
+    reply_transport to aliasfile and forwardfile directors. This is done
+    because the global implicit defaults for these options have been removed.
+    The default configuration now contains explicit settings, so convert4r3
+    makes these additions to be compatible with that. If your aliasfile and
+    forwardfile directors do not make use of the pipe, file, or autoreply
+    facilities, you can remove these new settings.
+
+(3) If you are using +allow_unknown in a host list which also has an exception
+    list, you may need to move +allow_unknown in the new configuration. For
+    example, if the old configuration contains
+
+      sender_host_reject = +allow_unknown : *.b.c
+      sender_host_reject_except = *.a.b.c
+
+    then the rewritten configuration will be
+
+      host_reject = ! *.a.b.c : +allow_unknown : *.b.c
+
+    Because the negated item contains a wild card, the reverse lookup for the
+    host name will occur before +allow_unknown is encountered, and therefore
+    +allow_unknown will have no effect. It should be moved to the start of the
+    list.
+
+One way of upgrading Exim from a pre-3.00 release to a post-3.00 release is as
+follows:
+
+1. Suppose your configuration file is called /opt/exim/configure, and you want
+   to continue with this name after upgrading. The first thing to do is to make
+   another copy of this file called, say, /opt/exim/configure.pre-3.00.
+
+2. Rebuild your existing Exim to use the copy of the configuration file instead
+   of the standard file. Install this version of Exim under a special name such
+   as exim-2.12, and point a symbolic link called "exim" at it. Then HUP your
+   daemon. You can check on the name of the configuration file by running
+
+     exim -bP configure_file
+
+   Ensure that everything is running smoothly.
+
+3. Build the new release, configured to use the standard configuration file.
+
+4. Use the convert4r3 utility to upgrade your configuration file for the new
+   release. After running it, check the file by hand.
+
+5. If any of the options that convert4r3 rewrote contained regular expressions
+   that had backslashes in them, and were not previously in quotes, they will
+   need modification if convert4r3 has put them into quotes. Either re-arrange
+   the option to remove the quoting, or escape each backslash. For example, if
+   you had
+
+     sender_reject_recipients = ^\d{8}@
+     sender_reject_except = ^\d{8}@x.y.z
+
+   convert4r3 will have combined the two settings into
+
+     sender_reject_recipients = "! ^\d{8}@x.y.z : \
+        ^\d{8}@"
+
+   This must be changed to
+
+     sender_reject_recipients = ! ^\d{8}@x.y.z : ^\d{8}@
+   or
+     sender_reject_recipients = "! ^\\d{8}@x.y.z : ^\\d{8}@"
+
+   In the second case, the quoted string could of course still be split
+   over several lines.
+
+6. If your configuration refers to any external lists of networks, check them
+   to ensure that all the masks are in the single-number form, because Exim no
+   longer recognizes the dotted quad form of mask. For example, if an item in
+   a netlist file is
+
+      131.111.8.0/255.255.255.0
+
+   you must change it to
+
+      131.111.8.0/24
+
+   Otherwise Exim will not recognize it as a masked IP address, and will treat
+   it as a host name. The convert4r3 utility makes this conversion for networks
+   that are mentioned inline in the configuration, but it does not handle
+   referenced files.
+
+7. Check the newly-built Exim as much as possible without installing; you can,
+   for example, use a command such as
+
+     ./exim -bV
+
+   in the build directory to test that it successfully reads the new
+   configuration file. You can also do tests using -bt and -bh.
+
+8. Install the new release under a special name such as exim-3.00.
+
+9. You can then easily change between the new and old releases simply by moving
+   the symbolic link and HUPping your daemon.
+
+
+Details of syntax changes at 3.00
+=================================
+
+1. A bare file name without a preceding search type may appear in a domain
+list; this causes each line of the file to be read and processed as if it were
+an item in the list, except that it cannot itself be a bare file name (that is,
+this facility cannot be used recursively). Wild cards and regular expressions
+may be used in the lines of the file just as in the main list.
+For example, if
+
+  local_domains = /etc/local-domains
+
+then the file could contain lines like
+
+  *.mydomain.com
+
+This is different to an lsearch file, which operates like any other lookup type
+and does an exact search for the key. If a # character appears anywhere in a
+line of the file, it and all following characters are ignored. Blank lines are
+also ignored.
+
+2. Any item in a domain list (including a bare file name) can be preceded by an
+exclamation mark character, to indicate negation. White space after the ! is
+ignored. If the domain matches the rest of the item, it is *not* in the set of
+domains that the option is defining. If the end of the list is reached, the
+domain is accepted if the last item was a negative one, but not if it was a
+positive one. If ! precedes a bare file name, then all items in the file are
+negated, unless they are preceded by another exclamation mark. For example:
+
+  relay_domains = !a.b.c : *.b.c
+
+sets up a.b.c as an exception to the more general item *.b.c, because lists are
+processed from left to right. If the domain that is being checked matches
+neither a.b.c nor *.b.c, then it is not accepted as a relay domain, because the
+last item in the list is a positive item. However, if the option were just
+
+  relay_domains = !a.b.c
+
+then all domains other than a.b.c would be relay domains, because the last item
+in the list is a negative item. In effect, a list that ends with a negative
+item has ": *" appended to it.
+
+3. Negation and bare file names are available as above in lists of local parts
+(e.g. in local_parts options) and complete addresses (address lists). For the
+special "@@" lookup form in address lists, negation also can be used in the
+list of local parts that is looked up for the domain. For example, with
+
+  sender_reject_recipients = @@dbm;/etc/reject-by-domain
+
+the file could contain lines like this:
+
+  baddomain.com:  !postmaster : !hostmaster : *
+
+If a local part that actually begins with ! is required, it has to be specified
+using a regular expression. Because local parts may legitimately contain #
+characters, a comment in the file is recognized only if # is followed by white
+space or the end of the line.
+
+4. Host lists may now contain network items, as in the former net list options,
+which have all been abolished. The only form of network masking is the /n
+variety. Negation and bare file names can appear in host lists, and there is a
+new type of item which allows masked network numbers to be used as keys in
+lookups, thus making it possible to used DBM files for faster checking when the
+list of networks is large.
+
+The complete list of types of item which can now appear in a host list is:
+
+. An item may be a bare file name; each line of the file may take the form of
+  any of the items below, but it may not itself be another bare file name. If
+  the file name is preceded by ! then all items in the file are negated, unless
+  they are preceded by another exclamation mark. Comments in the file are
+  introduced by # and blank lines are ignored.
+
+. If the entire item is "*" it matches any host.
+
+. If the item is in the form of an IP address, it is matched against the IP
+  address of the incoming call.
+
+. If the item is in the form of an IP address followed by a slash and a mask
+  length (e.g. 131.111.0.0/16) then it is matched against the IP address of the
+  incoming call, subject to the mask.
+
+. If the item is of the form "net<number>-<search-type>;<search-data>", for
+  example:
+
+    net24-dbm;/networks.db
+
+  then the IP address of the incoming call is masked using <number> as the mask
+  length; a textual string is then constructed from the masked value, followed
+  by the mask, and this is then used as the key for the lookup. For example, if
+  the incoming IP address is 192.152.34.6 then the key that is looked up for
+  the above example is "192.152.34.0/24".
+
+. If the entire item is "@" the primary host name is used as the the match
+  item, and the following applies:
+
+. If the item is a plain domain name, then a forward DNS lookup is done on that
+  name to find its IP address(es), and the result is compared with the IP
+  address of the incoming call.
+
+The remaining items require the host name to be obtained by a reverse DNS
+lookup. If the lookup fails, Exim takes a hard line by default and access is
+not permitted. If the list is an "accept" list, Exim behaves as if the current
+host is not in the set defined by the list, whereas if it is a "reject" list,
+it behaves as if it is.
+
+To change this behaviour, the special item "+allow_unknown" may appear in the
+list (at top level - it is not recognized in an indirected file); if any
+subsequent items require a host name, and the reverse DNS lookup fails, Exim
+permits the access, that is, its behaviour is the opposite to the default.
+
+. If the item starts with "*" then the remainder of the item must match the end
+  of the host name. For example, *.b.c matches all hosts whose names end in
+  .b.c. This special simple form is provided because this is a very common
+  requirement. Other kinds of wildcarding require the use of a regular
+  expression.
+
+. If the item starts with "^" then it is taken to be a regular expression which
+  is matched against the host name. For example, ^(a|b)\.c\.d$ matches either
+  of the two hosts a.c.d or b.c.d. If the option string in which this occurs is
+  given in quotes, then the backslash characters must be doubled, because they
+  are significant in quoted strings. The following two settings are exactly
+  equivalent:
+
+    host_accept = ^(a|b)\.c\.d$
+    host_accept = "^(a|b)\\.c\\.d$"
+
+. If the item is of the form <search-type>;<filename or query>, for example
+
+    dbm;/host/accept/list
+
+  then the host name is looked up using the search type and file name or query
+  (as appropriate). The actual data that is looked up is not used.
+
+5. Early versions of Exim required commas and semicolons to terminate option
+settings in drivers. This hasn't been the case for quite some time. The code to
+handle them has now been removed.
+
+
+Details of option changes at 3.00
+=================================
+
+Main options
+------------
+
+  * address_directory_transport, address_directory2_transport,
+    address_file_transport, address_pipe_transport, and address_reply_transport
+    have been abolished as obsolete. The aliasfile and forwardfile directors
+    have been able for some time to set the transports they want to use for
+    these special kinds of delivery; there seems little need for global
+    defaults. The default configuration has been altered to add settings for
+    file_transport and pipe_transport to the aliasfile and forwardfile
+    directors, and to add reply_transport to forwardfile.
+
+  * check_dns_names, a deprecated synonym for dns_check_names, has been
+    abolished.
+
+  * helo_accept_junk_nets is abolished; nets can now appear in
+    helo_accept_junk_hosts.
+
+  * helo_verify_except_hosts and helo_verify_except_nets have been abolished,
+    and helo_verify has been changed from a boolean to a host list, listing
+    those hosts for which HELO verification is required.
+
+  * the obsolete option helo_verify_nets (a synonym for host_lookup_nets) has
+    been abolished. Note that host_lookup_nets itself has been replaced by
+    host_lookup.
+
+  * hold_domains_except has been abolished. Use negated items in hold_domains.
+
+  * host_lookup_nets has been replaced by host_lookup, which can contain hosts
+    and nets.
+
+  * ignore_fromline_nets has been replaced by ignore_fromline_hosts.
+
+  * If message_filter is set and the filter generates any deliveries to files,
+    pipes, or any autoreplies, then the appropriate message_filter_*_transport
+    options must be set to define the transports, following the abolition of
+    the global defaults (see above).
+
+  * queue_remote and queue_remote_except have been abolished and replaced by
+    queue_remote_domains, which lists those domains that should be queued. The
+    effect of queue_remote=true is now obtained by queue_remote_domains=*.
+
+  * queue_smtp and queue_smtp_except have been abolished and replaced by
+    queue_smtp_domains, which lists those domains that should be queued after
+    routing. The effect of queue_smtp=true is now obtained by
+    queue_smtp_domains=*.
+
+  * rbl_except_nets has been abolished and replaced by rbl_hosts, which can
+    contain hosts and nets. This defaults to "*" and defines the set of hosts
+    for which RBL checking is done.
+
+  * receiver_unqualified_nets is abolished; nets can now appear in
+    receiver_unqualified_hosts.
+
+  * receiver_verify_except_hosts and receiver_verify_except_nets have been
+    abolished and replaced by receiver_verify_hosts, which defaults to "*".
+    This is used, however, only when receiver_verify is set - together with the
+    other conditions (receiver_verify_addresses, receiver_verify_senders).
+
+  * receiver_verify_senders_except has been abolished; the functionality is now
+    available by using negation in receiver_verify_senders.
+
+  * rfc1413_except_hosts and rfc1413_except_nets have been abolished, and
+    replaced by rfc1413_hosts, which defaults to "*".
+
+  * sender_accept, sender_accept_recipients and sender_reject_except have
+    been abolished; the functionality is now available via sender_reject and
+    sender_reject_recipients.
+
+  * sender_host_accept, sender_net_accept, sender_host_reject,
+    sender_net_reject, sender_host_reject_except, sender_net_reject_except,
+    sender_host_reject_recipients and sender_net_reject_recipients
+    have all been abolished, and replaced by the options host_reject and
+    host_reject_recipients.
+
+  * sender_host_accept_relay, sender_net_accept_relay,
+    sender_host_reject_relay, sender_host_reject_relay_except,
+    sender_net_reject_relay, and sender_net_reject_relay_except are abolished,
+    and replaced by host_accept_relay. This defaults unset, and this means that
+    all relaying is now by default locked out in the Exim binary. Previously,
+    if no relaying options were set, relaying was permitted.
+
+  * sender_unqualified_nets has been abolished; nets can now appear in
+    sender_unqualified_hosts.
+
+  * sender_verify_except_hosts and sender_verify_except_nets have been
+    abolished and replaced by sender_verify_hosts, which defaults to "*". This
+    is used, however, only when sender_verify is set (to make it similar to
+    receiver_verify, even though there aren't at present any other conditions.)
+
+  * sender_verify_log_details has been abolished. This was a little-used
+    debugging option.
+
+  * smtp_etrn_nets has been abolished; nets can now appear in smtp_etrn_hosts.
+
+  * smtp_expn_nets has been abolished; nets can now appear in smtp_expn_hosts.
+
+  * smtp_log_connections, a deprecated synonym for log_smtp_connections, has
+    been abolished.
+
+  * smtp_reserve_nets is abolished; nets can now appear in smtp_reserve_hosts.
+
+Generic director and router options
+-----------------------------------
+
+  * except_domains, except_local_parts, and except_senders have been abolished.
+    Use negated items in domains, local_parts, and senders instead, for
+    example, replace
+
+      except_domains = a.b.c
+
+    with
+
+      domains = !a.b.c
+
+    If you already have a domains setting, add any negative items to the front
+    of it.
+
+The aliasfile director
+----------------------
+
+  * The option "directory", an obsolete synonym for home_directory, has been
+    abolished.
+
+The forwardfile director
+------------------------
+
+  * The option "directory", an obsolete synonym for file_directory, has been
+    abolished.
+
+  * The option forbid_filter_log, an obsolete synonym for
+    forbid_filter_logwrite, has been abolished.
+
+The localuser director
+----------------------
+
+  * The option "directory", an obsolete synonym for match_directory, has been
+    abolished.
+
+The lookuphost router
+---------------------
+
+  * mx_domains_except and its obsolete old name non_mx_domains have been
+    abolished. Use negated items in mx_domains.
+
+The pipe transport
+------------------
+
+  * The option "directory", an obsolete synonym for home_directory, has been
+    abolished.
+
+The smtp transport
+------------------
+
+  * mx_domains_except and its obsolete old name non_mx_domains have been
+    abolished. Use negated items in mx_domains.
+
+  * serialize_nets has been abolished; nets may now appear in serialize_hosts.
+
+
+Other items relevant to upgrading from Exim 2.12
+================================================
+
+1. RFC 2505 (Anti-Spam Recommendations for SMTP MTAs) recommends that the
+checking of addresses for spam blocks should be done entirely caselessly.
+Previously, although Exim retained the case of the local part, in accordance
+with the RFC 821 rule that local parts are case sensitive, some of the string
+comparisons were nevertheless done caselessly, but file lookups used the
+unmodified address.
+
+The way addresses are compared with options whose values are address lists has
+been changed. At the start of the comparison, both the local part and the
+domain are now forced to lower case, and any comparisons that are done with
+in-line strings are done caselessly. For example,
+
+  sender_reject = A@b.c
+
+rejects both A@b.c and a@b.c. Any lookups that occur use lowercased strings as
+their keys. If the @@ lookup facility is used, the lookup is done on the lower
+cased domain name, but any subsequent string comparisons on local parts are
+done caselessly.
+
+To retain possibility of caseful matching, the pseudo-item "+caseful" can
+appear in an address list. It causes any subsequent items to do caseful matches
+on local parts. The domain, however, remains lower cased.
+
+2. The handling of incoming batched SMTP has been re-worked so as to behave in
+a more useful way in cases of error:
+
+  (i)   The option sender_verify_batch now defaults false.
+  (ii)  EOF is no longer interpreted as end-of-message; the "." line must be
+        present.
+  (iii) Exim stops immediately in cases of error, writing information to stdout
+        and stderr, and setting the return code to 1 if some messages have been
+        accepted, and 2 otherwise.
+
+3. The first message delivered by -R, and all messages delivered by -Rf and -qf
+are "forced" in the sense that retry information is over-ridden. Previously,
+Exim also forcibly thawed any of these messages that was frozen. This no longer
+happens. Additional options -Rff and -qff have been implemented to force
+thawing as well as delivery.
+
+4. When recipients are being rejected because the sending host is in an RBL
+list, Exim used just to show the RBL text, if any, as part of the rejection
+response. Now, if prohibition_message is set, it expands that string instead,
+with the RBL message available in $rbl_text, and $prohibition_reason set to
+"rbl_reject".
+
+5. When a trusted caller passed a message to Exim, it used to check the From:
+header against the caller's login (even though the caller was trusted) unless
+the -f option had been used to supply a different sender. This has been changed
+so that From: is never checked if the caller is trusted.
+
+Philip Hazel
+May 1999
+
diff --git a/doc/Exim4.upgrade b/doc/Exim4.upgrade
new file mode 100644 (file)
index 0000000..a97d41f
--- /dev/null
@@ -0,0 +1,1730 @@
+Upgrading Exim from Release 3.33 to 4.xx
+----------------------------------------
+
+Exim 4.00 represents the largest upheaval in Exim's history. There are a lot of
+changes to the way some parts of Exim work, and a lot of incompatible changes
+to the run time configuration file.
+
+This document is in two parts. The first part contains instructions and
+suggestions for how you might go about performing the upgrade. The second part
+is a brief list of all the changes that have taken place. For full details of
+all the new features, please consult the current version of the reference
+manual.
+
+
+HOW TO UPGRADE YOUR EXIM
+------------------------
+
+When you compile Exim 4, a Perl script called convert4r4 is built in the build
+directory. It is not installed by the install script, because it is likely that
+you will run it only once.
+
+This script is provided to assist in updating Exim configuration files. It
+reads an Exim 3 configuration file on the standard input, and writes a modified
+file on the standard output. It also writes comments about what it has done to
+the standard error file. It assumes that the input is a valid Exim 3
+configuration file. A typical call to the conversion script might be
+
+  ./convert4r4  </etc/exim/configure  >/etc/exim/configure.new
+
+The output file MUST be checked and tested before trying to use it on a live
+system. The conversion script is just an aid which does a lot of the "grunt
+work". It does not guarantee to produce an Exim 4 configuration that behaves
+exactly the same as the Exim 3 configuration it reads.
+
+Each option change in the new file is preceded by an identifying comment. In
+fact, the conversion script tends to make quite a mess of your configuration,
+and you should expect to go through it afterwards and tidy it up by hand.
+
+Unless you are running a very straightforward configuration, the automatic
+conversion is likely to generate a non-optimal configuration. You should not
+only check it thoroughly, but also run as many tests as you can, to ensure that
+it is working as you expect. In particular, you should test address routing,
+using -bt and -bv, and the policy controls, using -bh. If possible, you should
+also do some live tests (i.e. send and receive some messages) before putting
+Exim 4 into service.
+
+If you have a very complicated configuration, it is possible that convert4r4
+will break it in some situations, which is why thorough testing is strongly
+recommended.
+
+                   *********************************
+                   ***** You Have Been Warned ******
+                   *********************************
+
+
+HOW TO MOVE FROM AN EXIM 3 RELEASE TO AN EXIM 4 RELEASE
+-------------------------------------------------------
+
+One way of upgrading to Exim 4 from a version 3 release is as follows:
+
+1. Suppose your run time configuration file is called /usr/exim/configure, and
+   you want to continue with this name after upgrading. The first thing to do
+   is to make another copy of this file called, say, /usr/exim/configure.r3.
+
+2. Rebuild your existing Exim to use the copy of the run time configuration
+   file instead of the standard file. Install this version of Exim and HUP your
+   daemon. You can check on the name of the configuration file by running
+
+     exim -bP configure_file
+
+   Ensure that everything is running smoothly. You now have something you can
+   fall back to. IMPORTANT: when you do this re-install, you should also
+   re-install the utilities because four of them (exicyclog, eximon, exinext,
+   and exiwhat) also refer to the configuration file.
+
+3. Build the new release, configured to use the standard configuration file.
+
+4. Use the convert4r4 utility to upgrade your configuration file for the new
+   release. After running the conversion utility, check the file by hand, and
+   tidy it up.
+
+5. Test, test, test! And test some more!
+
+6. You can run complete tests, including actual deliveries, from an uninstalled
+   binary, but you have to tell it where it is, so that any re-executions can
+   be done. You can do this by temporarily inserting a setting such as
+
+   exim_path = /source/exim/exim-4.00/build-SunOS5-5.8-sparc/exim
+
+   into the run time configuration. If you want to, you can also insert
+   settings for spool_directory and log_file_path to divert those away from
+   their normal places. Remember to remove these temporary settings when you
+   eventually install the binary for real.
+
+7. The new installation script installs the new release as exim-4.00-1, and
+   set a symbolic link called "exim" to point to it. The old version of Exim
+   will be renamed to something like exim-3.33-1.
+
+8. You can now easily change between the new and old releases simply by moving
+   the symbolic link and HUPping your daemon. The format of message files on
+   Exim's spool has _not_ changed, so there should be no problem in changing
+   between releases while there are messages on the queue.
+
+9. HOWEVER: If you do change back and forth between releases, you must also
+   change the utilities exicyclog, eximon, exinext, and exiwhat if you are
+   going to use them. Installing Exim 4 will have left the old versions with a
+   .O suffix. It might be helpful to rename these so that you don't lose them.
+
+
+WHAT HAS NOT CHANGED IN EXIM 4.00
+---------------------------------
+
+The basic overall philosophy, design, and process structure has not changed.
+The format of spool files is the same. The transports have had only minor
+modifications. The command line options remain the same, with a couple of
+additions.
+
+The general run time configuration approach has not changed, but the actual
+details of the configuration file are different.
+
+The Exim monitor has not changed, and there have been only very minor changes
+to other Exim utilities.
+
+
+WHAT HAS CHANGED IN EXIM 4.00
+-----------------------------
+
+The rest of this document lists the very many changes that have taken place.
+I'm going to give only brief details here, because this part of the document is
+intended as a way of alerting you to areas of difference. The reference manual
+describes how the new features work in detail.
+
+
+Named domain, host, address, and local part lists
+-------------------------------------------------
+
+A new feature makes it possible to give names to lists of domains, hosts,
+addresses, and local parts. The syntax used is
+
+  domainlist    <name> = <a domain list>
+  hostlist      <name> = <a host list>
+  addresslist   <name> = <an address list>
+  localpartlist <name> = <a list of local parts>
+
+For example:
+
+  domainlist  local_domains = *.my.domain
+  addresslist bad_senders = cdb;/etc/badsenders
+
+These lists are referenced in options by giving the name preceded by a + sign.
+For example, in a router you might have
+
+  domains = +local_domains
+
+At first sight, these lists might seem to be the same as macros, but they are
+not. Macros are just textual substitutions. If you write
+
+  ALIST = host1 : host2
+  auth_advertise_hosts = !ALIST
+
+it probably won't do what you want, because that is exactly the same as
+
+  auth_advertise_hosts = !host1 : host2
+
+Notice that the second host name is not negated. However, if you use a host
+list, and write
+
+  hostlist alist = host1 : host2
+  auth_advertise_hosts = ! +alist
+
+the negation applies to the whole list, and so that is equivalent to
+
+  auth_advertise_hosts = !host1 : !host2
+
+These named lists also have a performance advantage. When Exim is routing an
+address or checking an incoming message, it caches the result of tests on the
+lists. So, if you have a setting such as
+
+  domains = +local_domains
+
+on several of your routers, the actual test is done only for the first one.
+However, this caching works only if there are no expansions within the list
+itself or any sublists that it references. In other words, caching happens only
+if the list is known to be the same each time it is referenced.
+
+By default, there may be up to 16 named lists of each type. This limit can be
+extended by changing a compile-time variable.
+
+The use of domain and host lists is recommended for concepts such as local
+domains, relay domains, and relay hosts. The default configuration is set up
+like this.
+
+
+Processing of domain, host, local part, and address lists
+---------------------------------------------------------
+
+The handling of these lists is now more uniform. Every list is expanded as a
+single string before it is used. (In Exim 3, some options were expanded and
+some weren't, and query-style lookup items were then re-expanded.)
+
+If an expansion is forced to fail, Exim behaves as if the item has not been
+found in the list.
+
+The confusing $key variable has been abolished. When processing a domain list,
+$domain contains the relevant domain and can be used in generating database
+queries. Other appropriate variables are set when processing other kinds of
+list; $sender_host and $sender_host_address for checking incoming hosts and
+$host and $host_address for checking outgoing hosts.
+
+Note that this means that any \ and $ characters in regular expressions must be
+escaped if they appear in one of these lists. The new expansion feature to turn
+off expansion (\N ... \N) which is described below can be helpful here.
+
+IMPORTANT: The details of the processing of address lists has been revised. In
+particular, the handling of the case of an item that is a single-key lookup has
+changed. It no longer looks up the domain on its own before looking up the
+complete address. You need to supply an explicit "*@" before the lookup if you
+want just the domain to be looked up. Please check the manual for full details.
+
+If an item in a host list is the empty string, it matches only when no host is
+defined. If used when checking an incoming message, it matches only when the
+message is arriving by SMTP on the standard input from a local process (using
+-bs). This provides a way of distinguishing between SMTP mail from local
+processes and from remote hosts.
+
+The +allow_unknown and +warn_unknown settings for host lists have been replaced
+by a single item, +include_unknown. By default, failure to find a host name
+when needed causes Exim to behave as if the host does not match the list, but
+if +include_unknown is set, the opposite behaviour happens. Whenever
++include_unknown is invoked, the incident is logged.
+
+
+Merge of Directors and Routers
+------------------------------
+
+There are no longer any directors in Exim 4. There are just routers. All
+addresses are passed to a single list of routers which typically makes use of
+the "domains" option to choose which way to handle specific groups of domains.
+
+A consequence of this is that the code no longer contains any concept of "local
+domains". However, a typical configuration will probably define a named domain
+list (see above) called local_domains, and use it to control routing something
+like this:
+
+  route_remote:
+    driver = dnslookup
+    domains = ! +local_domains
+    transport = remote_smtp
+    no_more
+
+  system_aliases:
+    ....
+
+The first router does DNS routing for all domains that are not in the named
+list of local domains, and no_more ensures that it is the last router for those
+domains. All other domains fall through to the system_aliases and subsequent
+routers. For a complete configuration example, look at the default
+configuration file in src/configure.default.
+
+
+Router Actions
+--------------
+
+The concept of how the routers work is as follows:
+
+A number of pre-conditions are tested (details below). If any of them fails,
+control is passed to the next router. We say "the router is skipped". Otherwise
+the router is run, and can yield one of several different results:
+
+. accept: The router accepts the address, and either queues it for a transport,
+or generates one or more "child" addresses. Processing the original address
+ceases, unless "unseen" is set on the router, in which case the address is
+passed to the next router. Processing of any child addresses starts with the
+first router by default, or at the router defined by redirect_router if it is
+set. This may be any router in the list.
+
+. decline: The router declines to accept the address because it does not
+recognize it at all. The address is passed to the next router, unless no_more
+is set, in which case the address fails.
+
+. pass: The router recognizes the address, but cannot handle it itself. It
+requests that the address be passed to another router. This overrides no_more.
+By default the address is passed to the next router, but this can be changed by
+setting pass_router. However, in this case (unlike redirect_router) the named
+router must be below the current router (to avoid loops).
+
+. fail: The router determines that the address should fail, and queues it for
+the generation of a bounce message. There is no further processing of the
+original address, unless "unseen" is set.
+
+. defer: The router cannot handle the address at the present time. (For
+example, a database may be offline.) No further processing of the address
+happens in this delivery attempt. It is tried again next time.
+
+. error: There is some error in the router (for example, a syntax error in
+its configuration). The action is as for defer.
+
+
+Router pre-conditions
+---------------------
+
+In Exim 3 there are some strange interactions between the generic options that
+test things before running a director or router and the no_more test that
+happens afterwards.
+
+In Exim 4 it is all more straightforward. If any of the pre-condition tests
+fail, the router is skipped and control passes to the next router. The no_more
+option has an effect only if the router is actually run - that is, if all the
+pre-condition tests succeed. The order in which these tests are run is:
+
+  verify status, expn status, domains, local_parts, check_local_user
+
+If all those match, the debug_print string is output when debugging. Exim then
+goes on to test
+
+  senders, require_files, condition
+
+Note that require_files comes near the end of the list, so you cannot use it to
+check for the existence of a file in which to lookup up a domain, local part,
+or sender. However, as these options are all expanded, you can use the "exists"
+expansion condition to make such tests. The require_files option is intended
+for checking files that the router may be going to use internally, or which are
+needed by a specific transport (e.g. .procmailrc).
+
+In Exim 4, local part prefixes and suffixes are recognized and removed before
+any of the other pre-condition tests are done (in Exim 3 they were removed
+afterwards). Note that this means that the local_parts option now tests the
+local part without its prefix or suffix.
+
+If you want to use local parts that include any affixes in a pre-condition
+test, you can do so by using a "condition" option that uses the variables
+$local_part, $local_part_prefix, and $local_part_suffix as necessary.
+
+
+A New Set of Routers
+--------------------
+
+The two sets of routers and directors of Exim 3 have been replaced by a single
+set of routers for Exim 4. These are as follows:
+
+. accept        Always accepts an address. It has no private options.
+
+. dnslookup     Routes by DNS lookup (descended from lookuphost).
+
+. ipliteral     Routes IP literal addresses (unchanged).
+
+. iplookup      Special-purpose lookup router (unchanged).
+
+. manualroute   Routes domains from explicit data (descended from domainlist).
+
+. queryprogram  Routes addresses by running a program (detail changed).
+
+. redirect      Redirects addresses; handles all the functions previously
+                supported by aliasfile, forwardfile, and smartuser without
+                a transport.
+
+
+Saving duplication of effort while routing
+------------------------------------------
+
+Early versions of Exim used to copy the routing of one address for all other
+addresses in the same domain, thereby possibly saving some repeated DNS
+lookups. This feature was removed for release 2.12, after the possibility of
+varying the router actions according to the local part (the local_parts option)
+was added. (In fact, the use of $local_part could have broken it earlier.)
+
+For Exim 4, I have added an option called same_domain_copy_routing to the
+dnslookup and manualroute routers. When one of these routers routes an address
+to a remote transport and this option is set, any other addresses in the
+message that have the same domain are automatically given the same routing, but
+only if the router does not set headers_add or headers_remove, and does not
+`widen' the domain during the routing.
+
+
+Generic Router Options
+----------------------
+
+. The global locally_caseless option is replaced by a generic router option
+  called caseful_local_part. By default, routers handle local parts caselessly.
+
+. check_local_user is now a generic option that is needed to check for a local
+  account. Typically used on redirect (for user's forward files) and on accept
+  (for local deliveries).
+
+. The setting self=local has been removed (since there's no concept of local
+  domains in the code). The same kind of effect can be achieved by using
+  self=reroute or self=pass.
+
+. expn is now a generic router option.
+
+. local_part_prefix and local_part_suffix are now generic router options,
+  replacing prefix and suffix on directors.
+
+. Exim 3 has two logging styles for delivery, depending on whether the domain
+  is a local domain or not. For local domains, the address is given just as the
+  local part - this makes these deliveries easier to spot in the log. In Exim 4
+  there's no concept of local domains, so this functionality cannot be
+  automatic. Instead, there's a generic router option called log_as_local which
+  requests "local-style" logging. This option defaults on for the "accept"
+  router, and off for all the others.
+
+. There's an option called retry_use_local_part which is the default for any
+  router that has check_local_user set, and it applies to routing delays. (The
+  same option for transports applies to transport delays.)
+
+. transport_home_directory and transport_current_directory are new generic
+  options on all routers. They set up default values for home_directory and
+  current_directory on the transport to which they route an address. Any
+  settings in the transport override.
+
+. If transport_home_directory is not set, but check_local_user is set, the
+  user's home directory is used as a default value.
+
+. The special fudge that exists in Exim 3 for handling home_directory settings
+  in forwardfile directors is not needed in Exim 4. It has therefore been
+  removed.
+
+. The new_director option in Exim 3 allows the direction of redirected
+  addresses to start at a given director, instead of the first one. In Exim 4,
+  this option is now called redirect_router. The option is used when a redirect
+  router succeeds, and when a queryprogram router returns a "redirect"
+  response.
+
+. There is a new option called pass_router, which specifies the router to go to
+  when a router "passes" on an address. The named router must follow the
+  current router (to avoid routing loops). Note: if a router declines, control
+  always passes to the next router, unless no_more is set.
+
+. There is a new router option called address_data. This is set to a string
+  which is expanded just before the router is run, that is, after all the
+  pre-tests have succeeded. If the expansion is forced to fail, the router
+  declines. Other expansion failures cause delivery of the address to be
+  deferred.
+
+  When the expansion succeeds, the value is retained with the address, and can
+  be accessed using the variable $address_data. Even if the router declines or
+  passes, the value remains with the address, though it can be changed by
+  another address_data setting on a subsequent router. If a router generates
+  child addresses, the value of $address_data propagates to them.
+
+  The idea of address_data is that you can use it to look up a lot of data for
+  the address once, then then pick out parts of the data later. For example,
+  you could use an LDAP lookup to return a string of the form
+
+    uid=1234 gid=5678 mailbox=/mail/xyz forward=/home/xyz/.forward
+
+  In the transport you could then pick out the mailbox by a setting such as
+
+    file = ${extract{mailbox}{$address_data}}
+
+  This makes the configuration file less messy, and also reduces the number of
+  lookups. (Exim does cache the most recent lookup, but there may be several
+  addresses with different lookups.)
+
+. When a transport is run for several addresses simultaneously, the values of
+  $address_data, $local_part_data, and $domain_data are taken from the first
+  address that the transport handles. However, the order in which multiple
+  addresses are processed is not defined. You therefore need to be careful if
+  you want to use these variables with multiple addresses. The smtp transport
+  is the only one which by default handles multiple addresses.
+
+. When an address is routed by a router with the "unseen" option set, a "clone"
+  address is created, and it starts being routed at the next router. (This is
+  what people expect. In Exim 3 it starts at the top - in simple cases that has
+  the same effect because of the anti-looping rule, but if aliases are involved
+  it sometimes doesn't do what you want.)
+
+. The way that require_files works has been changed. Each item in the list is
+  now separately expanded as the test proceeds. The use of leading ! and +
+  characters is unchanged. However, user and group checking is done differently.
+  Previously, seteuid() was used, but seteuid() is no longer used in Exim (see
+  "Security" below). Instead, Exim now scans along the components of the file
+  path and checks the access for the given uid and gid. It expects "x" access
+  on directories and "r" on the final file. This means that file access control
+  lists (on those operating systems that have them) are ignored.
+
+
+Other Consequences of the Director/Router Merge
+-----------------------------------------------
+
+. The -odqr option is abolished, as there is no inbuilt concept of remote
+  domains.
+
+. The -odqs option is equivalent to queue_smtp_domains = *.
+
+. queue_remote_domains is renamed queue_domains, and applies to any domain.
+
+. The -ql option now suppresses remote delivery; routing always happens.
+
+. The "remote" facility of queue_only_file has been removed.
+
+. The match_directory option for forwardfile and localuser has been entirely
+  abolished. Its function can be achieved using the "condition" option in
+  conjunction with check_local_user.
+
+. When an address is being verified, if it is redirected to a single new
+  address, verification continues with that address. If it is redirected to
+  more than one address, verification ceases with a success result. (In Exim 3,
+  this applied only to aliasing, not to forwarding.)
+
+
+The dnslookup router
+--------------------
+
+This router replaces the lookuphost router of Exim 3. It is much the same,
+except that the "gethostbyname" option has been removed. It now does only DNS
+routing - hence the change of name. Routing using gethostbyname() can be done
+by the manualroute router.
+
+
+The manualroute router
+----------------------
+
+This is the new name for the domainlist router, supposedly to make its function
+clearer and to avoid confusion with the "domainlist" that is used to set up
+named domain lists. Several things have been removed and reorganized.
+
+. The old search mechanism (route_file, route_query, route_queries,
+  search_type) have been removed. Instead there is a new option called
+  route_data, which is an expanded string. It should expand to a single routing
+  entry. If the expansion ends up empty (or is forced to fail), the router
+  declines. The route_list option still exists, for convenient listing of a few
+  inline routes.
+
+. There is no longer any MX processing function in this router. The keywords
+  bydns_mx and bydns_a have been removed, leaving just
+
+    bydns  => find IP addresses from address records in the DNS
+    byname => find IP addresses by calling gethostbyname()
+
+  The default lookup type is "byname", and this can be omitted from a route
+  data line. If an IP address is given, both "byname" and "bydns" are ignored
+  (so typically you omit this field).
+
+. The qualify_single and search_parents options have also been removed.
+
+. A transport is always required to be set, unless verify_only is set.
+
+. The host_find_failed option can be set to "decline", to cause the router to
+  decline if it can't find an IP address for a listed host.
+
+. If manualroute routes to a local transport, there is no need to specify
+  byname or bydns in the routing data. Any supplied host list is passed as a
+  string in $host, but $host_address is unset.
+
+
+The queryprogram router
+-----------------------
+
+This router has been re-designed:
+
+. You must now specify a user and group for the program to be run using
+  command_user and (if necessary) command_group. It no longer defaults to
+  "nobody". These options are expanded.
+
+. The command is now split up and each argument expanded separately, as happens
+  for the pipe transport. The command name is also expanded.
+
+. The return value "forcefail" has been renamed "fail", and it causes delivery
+  to fail. (The original usage of "fail" meaning "decline" has finally been
+  removed.)
+
+. The $route_option variable, which queryprogram used to be able to set has
+  been abolished. A facility to set the new $address_data variable replaces it.
+
+. The string returned from queryprogram must now be one of:
+
+  DECLINE
+  FAIL text
+  DEFER text
+  PASS
+  FREEZE text
+  REDIRECT text
+  ACCEPT TRANSPORT=transport HOSTS=host list LOOKUP=byname|bydns DATA=text
+
+The text returned for "redirect" is a list of new addresses. The text for FAIL
+is returned in the SMTP dialogue when the router is run as part of address
+verification. It is also logged. The text for DEFER and FREEZE is just logged.
+
+The data items in the "accept" return can be given in any order, and all are
+optional. If no transport is included in the "accept" return, the router's
+default transport is used. The host list and lookup type are needed only if the
+transport is an smtp transport that does not itself have a host list. The
+default lookup type is "byname". If the "data" field is set, its value is
+placed in the $address_data variable.
+
+
+The redirect router
+-------------------
+
+This router replaces forwardfile, appendfile, and the use of smartuser without
+a transport. It has two mutually exclusive options for specifying the data that
+it uses. If "file" is set, the data is taken from a file. Otherwise "data" must
+be set, and the data is the expanded value of that option.
+
+The data may be an alias list, possibly including special entries such as
+:fail:, or it may be a list of filtering instructions.
+
+If "file" is set, but the file does not exist or is empty, or its contents have
+no effect (entirely comments, or a filter that does nothing), the router
+declines. This also happens if the expansion of "file" is forced to fail. Any
+other expansion failure causes the router to defer.
+
+Ownership of the file is checked if check_local_user is set or if owners is
+set, unless check_owner is explicitly set false.
+
+Likewise, the group is checked if owngroups is set, or if check_local_user is
+set and a modemask not containing 020 is set, unless check_group is explicitly
+set false.
+
+If "data" is set, a forced expansion causes the router to decline. This also
+happens if "data" is an empty string or a string that causes nothing to be
+generated and no action to be taken.
+
+Because "data" is now used for traditional /etc/aliases lookups, an empty alias
+no longer gives an error. It behaves in the same way as :unknown: (which is
+still recognized, but ignored).
+
+. If no_repeat_use is set, the router is skipped if _any_ ancestor of the
+  current address was routed by this router. This pre-test happens before any
+  of the others. (Contrast the default loop avoidance logic, which skips a
+  router if an ancestor with the same local part was routed by the router.)
+
+. If include_directory is set, :include: files are constrained to this
+  directory.
+
+. When an address is redirected to a file or a pipe, $address_file or
+  $address_pipe (as appropriate) is set when expanding the value of
+  file_transport or directory_transport.
+
+. There are new options forbid_filter_readfile and forbid_filter_run to lock
+  out the use of the new ${readfile and ${run expansion items in filters.
+
+. If one_time is set, forbid_pipe, forbid_file, and forbid_filter_reply are
+  forced to be true, and headers_add and headers_remove are forbidden.
+
+
+Generic transport options
+-------------------------
+
+. All remote deliveries are now done in subprocesses running with specified
+  UIDs and GIDs. (Formerly, only remote parallel deliveries were done in
+  subprocesses.) As a result, user and group are now generic options that can
+  be used on all transports. The default for both local and remote transports
+  is to run as the Exim user and group. For remote transports, this should not
+  normally be changed, but if it is, the user or group should be able to access
+  the hints databases, though failure to open a hints database is always
+  ignored.
+
+  If it turns out that a transport user is in the never_users list, Exim now
+  defers delivery and writes to the panic log. (Previously it just ran the
+  delivery as "nobody".) Because subprocesses (usually running as "exim")
+  are now always used for remote deliveries, you should *not* include "exim" in
+  the never_users list.
+
+. initgroups is now also a generic transport option.
+
+. home_directory and current_directory are generic options on all transports,
+  though some transports (e.g. smtp) make no use of them. If they are unset,
+  values supplied by the router are used.
+
+. The message_size_limit option is now expanded, which makes it possible to
+  have different limits for different hosts, for example.
+
+
+Multiple (batch) deliveries in the appendfile, lmtp, and pipe transports
+------------------------------------------------------------------------
+
+The options controlling batch deliveries, including BSMTP, were a mess, and
+have been reworked.
+
+. The batch option has been removed from all three transports, and the bsmtp
+  and bsmtp_helo options have been removed from appendfile and pipe.
+
+. The batch_max option defaults to 1 in all three transports.
+
+. A new option called use_bsmtp has been added to appendfile and pipe. When
+  set, the message is delivered in BSMTP format. If you want to have a HELO
+  line at the start of the message, you can configure this by making use of the
+  message_prefix option. You must include the terminating newline.
+
+. A new option called batch_id has been added to all three transports.
+
+Batching is now achieved by setting batch_max to a value greater than 1. This
+is recommended for lmtp. When multiple addresses are routed to the same
+transport that has a batch_max value greater than one, the addresses are
+delivered in a batch, subject to certain conditions:
+
+. If any of the transport's options contain a reference to "$local_part", no
+  batching is possible.
+
+. If any of the transport's options contain a reference to "$domain", only
+  addresses with the same domain are batched.
+
+. If batch_id is set, it is expanded for each address, and only those addresses
+  with the same expanded value are batched.
+
+. Batched addresses must also have the same errors address (where to send
+  delivery errors), the same header additions and removals, the same user and
+  group for the transport, and if a host list is present, the first host must
+  be the same.
+
+
+The appendfile transport
+------------------------
+
+. The prefix and suffix options have been renamed message_prefix and
+  message_suffix to avoid confusion with address affixes. The default values,
+  which are suitable for mbox deliveries, now apply only if "file" is set and
+  use_bsmtp is not set. Otherwise, the default values for these options are
+  unset. They can, of course, always be overridden.
+
+. If "directory" is set (which means that "file" is not set), the check_string
+  and escape_string options now default unset.
+
+. The require_lockfile options has been abolished. If use_lockfile is set, a
+  lock file is always required.
+
+. The quota_filecount option is now expanded.
+
+. The create_file option now also applies when delivering into an individual
+  file in a given directory, as well as when appending to a single file. In the
+  case of maildir delivery, the restriction applies to the top directory of the
+  maildir folder.
+
+. There's a new option called directory_file which is expanded to form the
+  final leaf name of files when "directory" is set, but neither maildir nor
+  mailstore is set. The default is "q${base62:$tod_epoch}-$inode", which
+  reproduces the old fixed value. The variable $inode is available only when
+  expanding this new option.
+
+
+The pipe transport
+------------------
+
+. The prefix and suffix options have been renamed message_prefix and
+  message_suffix to avoid confusion with address affixes. The default values
+  that are suitable for vacation deliveries now apply only if use_bsmtp is not
+  set. Otherwise the default values for these options are unset. They can, of
+  course, always be overridden.
+
+
+The smtp transport
+------------------
+
+. The badly-named batch_max option is now called connection_max_messages.
+
+. If hosts_randomize is set, it now affects host lists that come from a router
+  as well as the contents of the "hosts" option, but only if the hosts were not
+  obtained from MX records. Typically, such lists come from the manualroute
+  router. This change means that the router can provide the same host list for
+  multiple addresses - causing them all to be sent to the transport at once.
+  Randomizing is then done each time the transport is called. (If you set
+  hosts_randomize on the router, the randomizing happens for each address.)
+
+. The way that smtp operates when there are multiple addresses to be sent to
+  the same host is now different. Previously, the transport was called many
+  times, with a maximum of max_rcpt addresses per call. Each call made a new
+  connection to the host. When remote_max_parallel = 1, all the addresses are
+  now passed to the transport at once. It makes a single TCP/IP call, but may
+  send multiple copies of the message, each with no more than max_rcpt
+  recipients.
+
+  When remote_max_parallel is greater than 1, a heuristic is used. The number
+  of addresses passed to a single call of the transport is limited to
+
+    (the total number of recipients) / (the value of remote_max_parallel)
+
+  so, for example, if there are 100 recipients and remote_max_parallel is 2, no
+  more than 50 are passed in one call, even if max_rcpt is 100. (The idea is
+  that the other 50 will be passed to another call running in parallel.)
+
+  There is an option of the smtp transport called connection_max_messages
+  which limits the number of messages, or copies of a message, that Exim sends
+  down a single TCP/IP connection. This applies both to this mechanism for
+  multiple copies of a single message, and the re-use of a TCP/IP connection
+  for sending other messages destined for the same host, after a delivery
+  delay. The default value is 500.
+
+. The "interface" option is now expanded. If the result is a forced failure or
+  an empty string, it is ignored. Otherwise, the result must be a list of IP
+  addresses. The first one of the correct type (IPv4 or IPv6) for the outgoing
+  connection is used. If there isn't one of the correct type, the option is
+  ignored.
+
+. At the start of running the transport, the value of $host is taken from the
+  first host in a multi-host list. However, just before the transport connects
+  to a host, the value is changed to refer to that particular host. (This
+  applies to $host_address as well.) This means that options such as helo_data
+  and the tls_options can be made host-specific.
+
+. The tls_verify_ciphers option has been renamed tls_require_ciphers, in order
+  to leave the word "verify" as something that refers to the verification of
+  certificates.
+
+. The resolution of hosts and fallback_hosts used to look up MX records. This
+  was some kind of ancient silliness that I recently noticed. These are
+  definitely hosts, not mail domains. Exim 4 just looks up address records.
+  As a consequence of this, the mx_domains option of the smtp transport is
+  removed.
+
+. The authenticate_hosts option has been renamed as hosts_try_auth. A new
+  option called hosts_require_auth has been added; if authentication fails for
+  one of these hosts, Exim does _not_ try to send unauthenticated. It defers
+  instead. The deferal error is detectable in the retry rules, so this can be
+  turned into a hard failure if required.
+
+
+The System Filter
+-----------------
+
+. The system filter options that were called message_filter_xxx have all been
+  renamed as system_filter_xxx.
+
+. The value of system_filter is expanded.
+
+. message_filter_directory_transport and message_filter_file_transport are now
+  both expanded before use. If the filter set up any file or pipe deliveries,
+  $address_file and $address_pipe are set as appropriate while doing the
+  expansions.
+
+. message_filter_directory2_transport has been removed. The effect of using
+  different directory-style transports can be achieved by specifying a suitable
+  expansion string to system_filter_directory_transport.
+
+. When a system filter added recipients to a message, Exim 3 added an
+  X-Envelope-To: header, listing the real recipients (up to 100). This has been
+  abolished because you can do this kind of thing using "headers_add" nowadays.
+
+. The "fail" command has been extended to allow for two different messages, one
+  for Exim's log and the other to be returned to the sender. The syntax is
+
+    fail "<<log message>>user message"
+
+  That is, if the first two characters of the message are "<<" the following
+  text, up to ">>", is written to the log, and the remainder is returned to the
+  user. If there is no log message, the user message is logged. The motivation
+  for this feature was to reduce the amount of text logged, while being able to
+  send quite long (maybe even multi-line) messages back to the sender.
+
+
+Changes to Lookups
+------------------
+
+. Oracle support is available. It works like the mysql and pgsql support,
+  except that there is no "database name" involved, and the "host name" field
+  is used for what is called "service name" in Oracle. This often looks like a
+  host name. Also, semicolons are not used at the end of an SQL query for
+  Oracle.
+
+. There's a new single-key lookup type called dsearch. It searches a directory
+  for a file whose name matches the key. The result of a successful search is
+  the key. One possible use of this could be for recognizing virtual domains.
+  If each domain is represented by a file whose name is the domain name, you
+  needn't make a separate list of the domains. You could test for them in an
+  ACL (see below), for example, by a line like this
+
+     accept domains = dsearch;/etc/virtual/domains
+
+. The format of LDAP output has been changed for cases where multiple
+  attributes are requested. The data for each attribute is now always quoted.
+  Within the quotes, the quote character, backslash, and newline are escaped
+  with backslashes and commas are used to separate multiple values for the
+  attribute. Thus, the string in quotes takes the same form as the output when
+  a single attribute is requested. If multiple entries are found, their data is
+  still separated by a newline.
+
+. There's a new expansion condition called ldapauth which exists so that the
+  LDAP authentication mechanism can be used for user authentication. It is
+  described under "string expansion" below.
+
+. Exim now supports ldaps:// URLs as well as ldap:// URLs. The former do LDAP
+  over TLS (i.e. encrypted) connections.
+
+. There is now support for the "whoson" mechanism for doing "POP-before-SMTP"
+  authentication. This is provided by new query-style lookup type called
+  "whoson", with queries that consist of IP addresses. For example, in an ACL
+  you can write
+
+    require condition = ${lookup whoson {$sender_host_address}{yes}{no}}
+
+
+Special items in domain and host lists
+--------------------------------------
+
+. In a domain list, the special item @ matches the primary host name, and the
+  special item @[] matches any local interface address enclosed in square
+  brackets (as in domain literal email addresses). The special item @mx_any
+  matches any domain that has an MX record pointing to the local host. The
+  special items @mx_primary and @mx_secondary are similar, except that the
+  first matches only when the primary MX is to the local host, and the second
+  only when the primary MX is not the local host, but a secondary MX is.
+
+. In a host list, the special item @ matches the primary host name, and the
+  special item @[] matches any local interface address (not in brackets).
+
+
+Access Control Lists (ACLs)
+---------------------------
+
+All the policy control options for incoming messages have been replaced by
+Access Control Lists (ACLs). These give more flexibility to the sysadmin, and
+allow the order of testing to be specified. For example, using an ACL, it is
+possible to specify "accept if authenticated, even if from an RBL host, but
+otherwise deny if from an RBL host", which is not possible in Exim 3.
+
+ACLs are defined in a new part of the configuration file, and given names.
+Which ones to run are controlled by a new set of options that are placed in the
+main part of the configuration.
+
+  acl_smtp_auth   specifies the ACL to run when AUTH is received
+  acl_smtp_data   specifies the ACL to run after a message has been received
+  acl_smtp_etrn   specifies the ACL to run when ETRN is received
+  acl_smtp_expn   specifies the ACL to run when EXPN is received
+  acl_smtp_rcpt   specifies the ACL to run when RCPT is received
+  acl_smtp_vrfy   specifies the ACL to run when VRFY is received
+
+The default actions vary. If acl_smtp_auth is not defined, AUTH is always
+accepted (and an attempt is made to authenticate the session). If acl_smtp_data
+is not defined, no checks are done after a message has been received, and it is
+always accepted at that point.
+
+However, if any of the others are not defined, the relevant SMTP command is
+rejected. In particular, this means that acl_smtp_rcpt must be defined in order
+to receive any messages over an SMTP connection. The default configuration file
+contains a suitable default for this.
+
+ACLs can be provided in line, or in files, or looked up from databases. One ACL
+can call another in a subroutine-like manner. String expansion is used, and
+which ACL to run can be varied according to sender host or any other criterion
+that a string expansion can test.
+
+This is not the place to give a full specification of ACLs, but here is a
+typical example for checking RCPT commands, taken from the default
+configuration. The tests are performed in order.
+
+acl_check_rcpt:
+  # Accept if source is local SMTP (i.e. not over TCP/IP - undefined host)
+  accept  hosts = :
+
+  # Deny if the local part contains @ or % or /
+  deny    local_parts   = ^.*[@%/]
+
+  # Accept mail to postmaster in any local domain, regardless of the source,
+  # and without verifying the sender.
+  accept  domains       = +local_domains
+          local_parts   = postmaster
+
+  # Deny unless the sender address can be verified.
+  require verify        = sender
+
+  # Accept if the address is in a local domain, but only if the recipient can
+  # be verified. Otherwise deny. The "endpass" line is the border between
+  # passing on to the next ACL statement (if tests above it fail) or denying
+  # access (if tests below it fail).
+  accept  domains       = +local_domains
+          endpass
+          message       = unknown user
+          verify        = recipient
+
+  # We get here only for non-local domains. Accept if the message arrived over
+  # an authenticated connection, from any host. These messages are usually from
+  # MUAs, so recipient verification is omitted.
+  accept  authenticated = *
+
+  # Reaching the end of the ACL causes a "deny", but we might as well give
+  # an explicit message.
+  deny    message       = relay not permitted
+
+The following options have been abolished as a consequence of the introduction
+of ACLs:
+
+auth_hosts, auth_over_tls_hosts, headers_checks_fail, headers_check_syntax,
+headers_sender_verify, headers_sender_verify_errmsg, host_accept_relay,
+host_auth_accept_relay, host_reject_recipients, prohibition_message,
+rbl_domains, rbl_hosts, rbl_log_headers, rbl_log_rcpt_count,
+rbl_reject_recipients, rbl_warn_header, receiver_try_verify, receiver_verify,
+receiver_verify_addresses, receiver_verify_hosts, receiver_verify_senders,
+recipients_reject_except, recipients_reject_except_senders, relay_domains,
+relay_domains_include_local_mx, relay_match_host_or_sender,
+sender_address_relay, sender_address_relay_hosts, sender_reject,
+sender_reject_recipients, sender_try_verify, sender_verify,
+sender_verify_batch, sender_verify_hosts, sender_verify_fixup,
+sender_verify_hosts_callback, sender_verify_callback_domains,
+sender_verify_callback_timeout, sender_verify_max_retry_rate,
+sender_verify_reject, smtp_etrn_hosts, smtp_expn_hosts. smtp_verify, tls_hosts.
+
+The variable $prohibition_reason has been abolished.
+
+The host_reject option has been retained, but with its name changed to
+host_reject_connection, to emphasize that it causes a rejection at connection
+time. I've left it available just in case it is needed - but its use is not
+recommended in normal circumstances.
+
+
+Other Incoming SMTP Session Controls
+------------------------------------
+
+. The option smtp_accept_max_per_connection (default 1000) limits the number of
+  messages accepted over a single SMTP connection. This is a safety catch in
+  case some sender goes mad (incidents of this kind have been seen). After the
+  limit is reached, a 421 response is given to MAIL commands.
+
+. Some sites find it helpful to be able to limit the rate at which certain
+  hosts can send them messages, and the rate at which an individual message can
+  specify recipients. There are now options for controlling these two different
+  rates.
+
+  Rate limiting applies only to those hosts that match smtp_ratelimit_hosts,
+  whose value is a host list. When a host matches, one or both of the options
+  smtp_ratelimit_mail and smtp_ratelimit_rcpt may be set. They apply to the
+  rate of acceptance of MAIL and RCPT commands in a single SMTP session,
+  respectively.
+
+  The value of each option is a set of four comma-separated values:
+
+    1. A threshold, before which there is no rate limiting.
+    2. An initial time delay. Unlike other times in Exim, fractions are allowed
+       here.
+    3. A factor by which to increase the delay each time.
+    4. A maximum value for the delay.
+
+  For example, these settings have been used successfully at the site which
+  first suggested this feature, for controlling mail from their customers:
+
+    smtp_ratelimit_mail = 2, 0.5s, 1.05, 4m
+    smtp_ratelimit_rcpt = 4, 0.25s, 1.015, 4m
+
+. The default value for smtp_connect_backlog has been increased to 20.
+
+. The SMTP protocol specification requires the client to wait for a response
+  from the server at certain points in the dialogue. (Without PIPELINING these
+  are after every command; with PIPELINING they are fewer, but still exist.)
+  Some spamming sites send out a complete set of SMTP commands without waiting
+  for any response. Exim 4 protects against this by rejecting messages if the
+  client has sent further input when it should not have. The error response
+  "554 SMTP synchronization error" is sent, and the connection is dropped.
+
+  This check is controlled by smtp_enforce_sync, which is true by default.
+
+. helo_strict_syntax has been abolished. The default is now to enforce strict
+  domain syntax for HELO/EHLO arguments. You can use helo_accept_junk_hosts if
+  you want to avoid this.
+
+. There's a new option called helo_lookup_domains. If the domain given in a
+  HELO or EHLO command matches this list, a reverse lookup is done in order to
+  establish the host's true name. The default setting is
+
+    helo_lookup_domains = @ : @[]
+
+  That is, a lookup is forced if the client host gives the server's name or
+  [one of its IP addresses] in HELO or EHLO. (In Exim 3 this happened
+  automatically and was not configurable.)
+
+. The value of the global message_size_limit option is now expanded. For
+  locally submitted messages this happens at the start of message reception.
+  For messages from remote hosts, the expansion is done just after the host
+  connects, so that the value can depend on the host.
+
+
+Handling of Resent- Fields
+--------------------------
+
+RFC 2822 makes it clear that Resent- fields are purely informational. Exim used
+to make use of Resent-Reply-To: which does not actually exist, and it also used
+to use the last set of resent- fields for all the address fields it recognized.
+
+In Exim 4, resent- headers are dealt with as follows:
+
+. A Resent-From: header that just contains the login id as the address is
+  automatically rewritten in the same way as From: is (using qualify domain,
+  and user name from the passwd data).
+
+. If there's a rewrite rule for a header, it is also applied to resent- headers
+  of the same type. For example, a rule that rewrites From: headers also
+  rewrites Resent-From: headers.
+
+. For local messages, if Sender: is being removed on input, Resent-Sender: is
+  also removed.
+
+. If there are any resent- headers but no Resent-Date: or Resent-From: they are
+  added.
+
+. The logic for adding Sender: is now duplicated for Resent-Sender.
+
+. If there's no Resent-Message-Id: one is created, and it is the
+  Resent-Message-Id: which is included in the log line.
+
+
+Authentication
+--------------
+
+. The auth_hosts option has been abolished; this functionality is now
+  controlled by ACLs.
+
+. The auth_always_advertise option has been abolished because it depended on
+  auth_hosts and host_auth_accept_relay, both of which are no more. In its
+  place there is a new option called auth_advertise_hosts, whose default value
+  is *, meaning "advertise AUTH to all".
+
+. The value of server_setid is now used when logging failed authentication
+  attempts.
+
+. The -oMaa option allows trusted users to set the value of
+  $sender_host_authenticated (the authenticator name). This is normally used in
+  conjunction with -oMa.
+
+
+Encryption
+----------
+
+. Because tls_hosts is no more, tls_advertise_hosts is now the only means of
+  controlling the advertisement of STARTTLS (previously, tls_hosts overrode).
+
+. The global option tls_verify_ciphers has been abolished. There are now
+  facilities for checking which cipher is in use in ACLs.
+
+. There's a new option called tls_try_verify_hosts. Like tls_verify_hosts, this
+  causes the server to request a certificate from a client, and it verifies the
+  certificate that it receives. However, unlike tls_verify_hosts, Exim
+  continues with the SMTP connection (encrypted) if a client certificate is not
+  received, or if the certificate does not verify. This state can be detected
+  in an ACL, which makes it possible to implement policies such as "accept for
+  relay only if a verified certificate has been received but accept for local
+  delivery if encrypted, even without a verified certificate".
+
+  A match in tls_verify_hosts overrides tls_try_verify_hosts.
+
+
+The Daemon
+----------
+
+. local_interfaces can now specify a port number with each address, thus
+  allowing a single Exim daemon to listen on multiple ports. The format of each
+  address is either [aaaa]:ppp or aaaa.ppp where aaaa is an IP address and ppp
+  is a port number. For example:
+
+    local_interfaces = 192.168.3.4.25 : 192.168.3.4.26
+
+  If an address is listed without a port, the setting of daemon_smtp_port, or
+  the value of the -oX option, is the default.
+
+. The -oX option can now override local_interfaces. That is, it can supply IP
+  addresses as well as just a port. It is interpreted in this way if its value
+  contains any of the characters . : or []. For example:
+
+  exim -bd -oX 10.9.8.7:10.11.12.13.2525
+
+  The format of the string is identical to the format recognized by the
+  local_interfaces option.
+
+. The way the daemon wrote PID files was overly complicated and messy. It no
+  longer tries to be clever. A PID file is written if, and only if, -bd is used
+  and -oX is _not_ used. In other words, only if the daemon is started with its
+  standard options. There is only one PID file. If pid_file_path is unset, it
+  is exim-daemon.pid in Exim's spool directory. Otherwise the value of
+  pid_file_path is used. For backwards compatibility, "%s" in this value is
+  replaced by an empty string.
+
+
+Logging
+-------
+
+The log_level option and all the various independent logging control options
+have been abolished. In their place there is a single option called
+log_selector. It takes a string argument composed of names preceded by + or -
+characters. These turn on or off the logging of different things. For example:
+
+  log_selector = +arguments -retry_defer
+
+The optional logging items (defaults marked *) are:
+
+  address_rewrite             address rewriting
+  all_parents                 all parents in => lines
+  arguments                   exim arguments
+ *connection_reject           connection rejections
+ *delay_delivery              immediate delivery delayed (message queued)
+  delivery_size               add S=nnn to delivery lines
+ *dnslist_defer               defers of DNS list (aka RBL) lookups
+  incoming_interface          incoming interface on <= lines
+  incoming_port               incoming port on <= lines
+ *lost_incoming_connection    as it says (includes timeouts)
+ *queue_run                   start and end queue runs
+  received_sender             sender on <= lines
+  received_recipients         recipients on <= lines
+ *retry_defer                 "retry time not reached"
+  sender_on_delivery          add sender to => lines
+ *size_reject                 rejection because too big
+ *skip_delivery               "message is frozen"
+  smtp_confirmation           SMTP confirmation on <= lines
+  smtp_connection             SMTP connections
+  smtp_protocol_error         SMTP protocol errors
+  smtp_syntax_error           SMTP syntax errors
+  subject                     contents of Subject: on <= lines
+ *tls_cipher                  TLS cipher on <= lines
+  tls_peerdn                  TLS peer DN on <= lines
+
+  all                         all of the above
+
+"retry time not reached" is always omitted from individual message logs after
+the first delivery attempt.
+
+The log line "error message sent to" has been abolished, because the R= item on
+the incoming message line gives the relationship between the original message
+and the bounce.
+
+The logging options that have been abolished are: log_all_parents,
+log_arguments, log_incoming_port, log_interface, log_ip_options,
+log_level, log_queue_run_level, log_received_sender, log_received_rceipients,
+log_rewrites, log_sender_on_delivery, log_smtp_confirmation,
+log_smtp_connections, log_smtp_syntax_errors, log_subject, tls_log_cipher,
+tls_log_peerdn.
+
+
+Debugging
+---------
+
+The debug_level option has been removed. The -dm option has been removed. The
+-df option has also be removed, along with its related build-time option
+STDERR_FILE. (To debug inetd usage, an auxiliary script should be used.)
+
+The -d option has been reworked. It no longer takes a debug level number
+argument, but instead takes a list of debugging names, each preceded by + or -
+to turn on or off individual sets of debugging messages.
+
+. The -v option now shows just the SMTP dialog and any log lines.
+
+. -d with no argument gives a lot of standard debugging data. This is in effect
+  the equivalent of the old -d9, the thing you ask people to set for an initial
+  debugging test.
+
+. -d+x      adds debugging option x to the default set
+  -d-x      removes debugging option x from the default set
+  -d-all+x  leaves only debugging option x
+
+The available debugging names are:
+
+  acl            ACL interpretation
+  auth           authenticators
+  deliver        general delivery logic
+  dns            DNS lookups (see also resolver)
+  dnsbl          DNS black list (aka RBL) code
+  exec           arguments for execv() calls
+  filter         filter handling
+  hints_lookup   hints data lookups
+  host_lookup    all types of name->IP address handling
+  ident          ident lookup
+  interface      lists of local interfaces
+  lists          matching things in lists
+  load           system load checks
+  lookup         general lookup code and all lookups
+  memory         memory handling (replaces the old -dm)
+  process_info   setting info for the process log
+  queue_run      queue runs
+  receive        general message reception logic
+  resolver       turn on the DNS resolver's debugging output; goes to stdout
+  retry          retry handling
+  rewrite        rewriting
+  route          address routing
+  tls            TLS logic
+  transport      transports
+  uid            changes of uid/gid and looking up uid/gid
+  verify         address verification logic
+
+  all            all of the above, and also -v
+
+The default (-d with no argument) includes all of the above, plus -v, with the
+exception of filter, interface, load, memory, and resolver. Some debugging
+output always happens unconditionally whenever any debugging is selected. This
+includes some initial output and every log line.
+
+-d without any value was previously allowed for non-admin users because it used
+to be synonymous with -v. In Exim 4, non-admin users may use -v, but not -d.
+
+If the debug_print option is set in any driver, it produces output whenever any
+debugging is selected, or if -v is used.
+
+
+Local Scan Function
+-------------------
+
+For customized message scanning, you can now supply a C function that is linked
+into the Exim binary. The function is called local_scan(), and it is called
+when Exim has received a message, but has not yet sent a final
+acknowledgement to the sender. This applies to all messages, whether local or
+remote, SMTP or not.
+
+From within your function you can inspect the message, change the recipients,
+add or remove headers, and tell Exim whether to accept or reject the message.
+
+The manual contains the specification of the API for this function.
+
+
+String Expansion
+----------------
+
+. The lookup feature that allowed for subkeys using the syntax
+
+    ${lookup {key:subkey} type {data...
+
+  has been abolished (a) because the effect can be achieved using ${extract,
+  and (b) because in non-lsearch lookups, a colon can be a valid character in a
+  key.
+
+. When a string key is used in a ${extract expansion item, it is now handled
+  case-insensitively.
+
+. A new expansion variable called $tod_epoch gives the time as a single decimal
+  number representing the number of seconds from the start of the Unix epoch.
+
+. There's a new expansion operator that can turn numbers into base 62, for
+  example, ${base62:$tod_epoch}.
+
+. ${extract{number} now recognizes a negative number as a request to count
+  fields from the right.
+
+. There's a new expansion feature for reading files:
+
+    ${readfile{/some/file}{eolstring}}
+
+  The contents of the file replace the item. If {eolstring} is present (it's
+  optional) any newlines in the file are replaced by that string.
+
+. There's a new expansion feature for running commands:
+
+    ${run{comand args}{yes}{no}}
+
+  Like all the other conditional items, the {yes} and {no} strings are
+  optional. Omitting both is equivalent to {$value}. The standard output of the
+  command is put into $value if the command succeeds (returns a zero code). The
+  value of the code itself is put into $runrc, and this remains set afterwards,
+  so in a filter file you can do things like
+
+    if "${run{x y z}{}}$runrc" is 1 then ...
+    elsif $runrc is 2 then ...
+
+  As in other command executions from Exim, a shell is not used by default.
+  If you want a shell, you must explicitly code it.
+
+. The redirect router has options for forbidding ${readfile and ${run in
+  filters.
+
+. A feature is provided to suppress expansion of part of a string. Any
+  characters between two occurrences of \N are copied to the output string
+  verbatim. This is particularly useful for protecting regular expressions from
+  unwanted expansion effects. For example:
+
+    queue_smtp_domains = ! \N^ten-\d+\.testing\.com$\N
+
+  Without \N the \ and $ characters in the regex would have to be escaped.
+
+. Radius authentication is supported in a similar way to PAM. You must set
+  RADIUS_CONFIG_FILE in Local/Makefile to specify the location of the Radius
+  client configuration file. Then you can use expansions such as
+
+    server_condition = ${if radius{arguments}{yes}{no}}
+
+. User authentication can now also be done by attempting to bind to an LDAP
+  server. The syntax is again similar to PAM and Radius.
+
+    server_condition = ${if ldapauth{ldap query}{yes}{no}}
+
+  A user and password are required to be supplied with the query. No actual
+  data is looked up; Exim just does a bind to the LDAP server and sets the
+  condition according to the result. Here's an example of an SMTP
+  authenticator:
+
+    login:
+      driver = plaintext
+      public_name = LOGIN
+      server_prompts = "Username:: : Password::"
+      server_condition = ${if ldapauth \
+        {user="uid=${quote_ldap:$1},ou=people,o=example.org" pass="$2" \
+        ldap://ldap.example.org/}{yes}{no}}
+      server_set_id = uid=$1,ou=people,o=example.org
+
+
+
+Security
+--------
+
+Exim 3 could be run in a variety of ways as far as security was concerned. This
+has all been simplified in Exim 4. The security-conscious might like to know
+that it no longer makes any use of the seteuid() function.
+
+. A UID and GID are required to be specified when Exim is compiled. They can be
+  now specified by name as well as by number, so the relevant options are now
+  called EXIM_USER and EXIM_GROUP. If you really feel you have to run Exim as
+  root, you can specify root here, but it is not recommended.
+
+. The "security" option has been abolished. Exim always releases its root
+  privilege when it can. In a conventional configuration, that means when it is
+  receiving a message, when it is delivering a message, when it is running a
+  queryprogram router, and when it is obeying users' filter files (and system
+  filters if it has been given a user for that purpose).
+
+. One important change is that Exim 4 runs as root while routing addresses for
+  delivery. Exim 3 used seteuid() to give up privilege temporarily while
+  routing. Apart from the unliked use of seteuid(), this sometimes gave rise to
+  permissions problems on configuration files.
+
+. However, Exim still runs as the Exim user while receiving messages, and
+  therefore while using the routing logic for verifying at SMTP time.
+
+. There is a new option called deliver_drop_privilege. If this is set, Exim
+  gives up its privilege right at the start of a delivery process, and runs the
+  entire delivery as the Exim user. This is the same action that used to be
+  requested by setting security=unprivileged.
+
+
+Hints Databases
+---------------
+
+. A single "misc" hints database is now used for ETRN and host serialization.
+  There have been appropriate consequential changes to the utilities for
+  managing the hints.
+
+. The exim_tidydb -f option has been abolished. A full tidy is now always done
+  (it hasn't proved to be very expensive).
+
+
+The run time Configuration File
+------------------------------
+
+. The format of the configuration file has changed. Instead of using "end" to
+  terminate sections, it now uses "begin <name>" to start sections. This means
+  that the sections, apart from the first, may appear in any order.
+
+. You can now include other files inside Exim run time configuration files, by
+  using this syntax:
+
+  .include <file name>
+
+. Quotes round the file name are optional. Includes may be nested to any depth,
+  but remember that Exim reads its configuration file often. The processing of
+  .include happens early, at a physical line level, so, like comment lines, it
+  can be used in the middle of an options setting, for example:
+
+  hosts_lookup = a.b.c \
+                 .include /some/file
+
+  Include processing happens _before_ macro processing. Its effect is simply to
+  process the lines of the file as if they occurred inline where the .include
+  appears.
+
+. A macro at the start of a configuration line can now turn the line into an
+  empty line or a comment line. This applies to _logical_ input lines, that is,
+  after any concatenations have been done.
+
+
+Format of spool files
+---------------------
+
+. -local_scan is used in spool files to record the value of $local_scan_data,
+  the string returned from the locally-provided local_scan() function.
+
+
+Renamed Options
+---------------
+
+Some options have been renamed, to make their function clearer, or for
+consistency.
+
+. receiver_unqualified_hosts has been renamed as recipient_unqualified_hosts.
+  I'm going to use "recipient" everywhere in future.
+
+. helo_verify has become helo_verify_hosts.
+
+. remote_sort has become remote_sort_domains.
+
+. In the appendfile and pipe transports, "prefix" and "suffix" have become
+  "message_prefix" and "message_suffix". In the generic router options,
+  "prefix" and "suffix" have become "local_part_prefix" and "local_part_suffix".
+
+
+Miscellaneous
+-------------
+
+. ETRN serialization now uses a double fork, so that an Exim process (detached
+  from the original input process) can wait for the command to finish. This
+  means that it works whatever command ETRN causes to run. (Previously it
+  worked only if ETRN ran "exim -Rxxx".)
+
+. For incoming messages, the server's port number is preserved, and is
+  available in $interface_port. The privileged option -oMi can be used to
+  set this value.
+
+. The -Mmd option (to mark addresses delivered) now operates in a
+  case-sensitive manner.
+
+. Checks for duplicate deliveries are now case-sensitive in the local part.
+
+. The number of situations where Exim panics has been reduced. For example,
+  expansion failures for the "domains" or "local_parts" options in a router now
+  cause deferral instead of a panic.
+
+. EXPN no longer attempts to distinguish local and remote addresses (but you
+  can cause it to be rejected for certain arguments in the ACL).
+
+. accept_timeout has been renamed as receive_timeout, to match
+  smtp_receive_timeout.
+
+. The ability to check an ident value as part of an item in a host list has
+  been removed.
+
+. The reject log shows a message's headers only if the rejection happens after
+  the SMTP DATA command (because they aren't available for earlier checks). The
+  sender, and up to five recipients are listed in Envelope-from: and
+  Envelope-to: header lines. After the headers, a line of separator characters
+  is output. Separators are no longer used for other reject log entries.
+
+. Because header checks are now done as part of ACLs, they now apply only to
+  SMTP input.
+
+. The port number on SMTP connections is now logged in the format [aaaa]:ppp
+  where aaaa is an IP address and ppp is a port, instead of in the format
+  [aaaa.ppp] because the former format causes some software to complain about
+  bad IP addresses.
+
+. The -oMa and -oMi options can now use the [aaaa]:ppp notation to set a port
+  number, but they still also recognize the aaaa.ppp notation.
+
+. The build-time option HAVE_AUTH is abolished. Exim automatically includes
+  authentication code if any authenticators are configured.
+
+. The nobody_user and nobody_group options have been abolished.
+
+. The $message_precedence variable has been abolished. The value is now
+  available as $h_precedence:.
+
+. There's a new utility script called exim_checkaccess which packages up a call
+  to Exim with the -bh option, for access control checking. The syntax is
+
+    exim_checkaccess <IP address> <email address> [exim options]
+
+  It runs "exim -bh <IP address>", does the SMTP dialogue, tests the result and
+  outputs either "accepted" or "Rejected" and the SMTP response to the RCPT TO
+  command. The sender is <> by default, but can be changed by the use of the
+  -f option.
+
+. The default state of Exim is now to forbid domain literals. For this reason,
+  the option that changes this has been renamed as allow_domain_literals.
+
+. The dns_check_names boolean option has been abolished. Checking is now turned
+  off by unsetting dns_check_names_pattern.
+
+. The errors_address and freeze_tell_mailmaster options have been abolished. In
+  their place there is a new option called freeze_tell, which can be set to a
+  list of addresses. A message is sent to these addresses whenever a message is
+  frozen - with the exception of failed bounce messages (this is not changed).
+
+. The message_size_limit_count_recipients option has been abolished on the
+  grounds that it was a failed experiment.
+
+. The very-special-purpose X rewrite flag has been abolished. The facility it
+  provided can now be done using the features of ACLs.
+
+. The timestamps_utc option has been abolished. The facility is now provided by
+  setting timezone = utc.
+
+. The value of remote_max_parallel now defaults to 2.
+
+. ignore_errmsg_errors has been abolished. The effect can be achieved by
+  setting ignore_bounce_errors_after = 0s. This option has been renamed from
+  ignore_errmsg_errors_after to make its function clearer. The default value
+  for ignore_bounce_errors_after is now 10w (10 weeks - i.e. likely to be
+  longer than any other timeouts, thereby disabling the facility).
+
+. The default for message_size_limit is now 50M as a guard against DoS attacks.
+
+. The -qi option does only initial (first time) deliveries. This can be helpful
+  if you are injecting message onto the queue using -odq and want a queue
+  runner just to process new messages. You can also use -qqi if you want.
+
+. Rewriting and retry patterns are now anything that can be single address list
+  items. They are processed by the same code, and are therefore expanded before
+  the matching takes place. Regular expressions must be suitably quoted. These
+  patterns may now be enclosed in double quotes so that white space may be
+  included. Normal quote processing applies.
+
+. Some scripts were built in the util directory, which was a mistake, because
+  they might be different for different platforms. Everything that is built is
+  now built in the build directory. The util directory just contains a couple
+  of scripts that are not modified at build time.
+
+. The installation script now installs the Exim binary as exim-v.vv-bb (where
+  v.vv is the version number and bb is the build number), and points a symbolic
+  link called "exim" to this binary. It does this in an atomic way so that
+  there is no time when "exim" is non-existent. The script is clever enough to
+  cope with an existing non-symbolic-link binary, converting it to the new
+  scheme automatically (and atomically).
+
+. When installing utilities, Exim now uses cp instead of mv to add .O to the
+  old ones, in order to preserve the permissions.
+
+. If the installation script is installing the default configuration, and
+  /etc/aliases does not exist, the script installs a default version. This does
+  not actually contain any aliases, but it does contain comments about ones
+  that should be created. A warning is output to the user.
+
+. A delay warning message is not sent if all the addresses in a message get a
+  "retry time not reached" error. Exim waits until a delivery is actually
+  attempted, so as to be able to give a more informative message.
+
+. The existence of the three options deliver_load_max, queue_only_load, and
+  deliver_queue_load_max was confusing, because their function overlapped. The
+  first of them has been abolished. We are left with
+
+    queue_only_load         no immediate delivery if load is high when
+                            message arrives
+    deliver_queue_load_max  no queued delivery if load is too high
+
+. The ability to edit message bodies (-Meb and the Eximon menu item) has been
+  removed, on the grounds that it is bad practice to do this.
+
+. Eximstats is now Steve Campbell's patched version, which displays sizes in K
+  and M and G, and can optionally generate HTML.
+
+. If bounce_sender_authentication is set to an email address, this address is
+  used in an AUTH option of the MAIL command when sending bounce messages, if
+  authentication is being used. For example, if you set
+
+    bounce_sender_authentication = mailer-daemon@your.domain
+
+  a bounce message will be sent over an authenticated connection using
+
+    MAIL FROM:<> AUTH=mailer-daemon@your.domain
+
+. untrusted_set_sender has changed from a boolean to an address pattern. It
+  permits untrusted users to set sender addresses that match the pattern. Like
+  all address patterns, it is expanded. The identity of the user is in
+  $sender_ident, so you can, for example, restrict users to setting senders
+  that start with their login ids by setting
+
+    untrusted_set_sender = ^$sender_ident-
+
+  The effect of the previous boolean can be achieved by setting the value to *.
+  This option applies to all forms of local input.
+
+. The always_bcc option has been abolished. If an incoming message has no To:
+  or Cc: headers, Exim now always adds an empty Bcc: line. This makes the
+  message valid for RFC 822 (sic). In time, this can be removed, because RFC
+  2822 does not require there to be a recipient header.
+
+. ACTION_OUTPUT=no is now the default in the Exim monitor.
+
+. dns_ipv4_lookup has changed from a boolean into a domain list, and it now
+  applies only to those domains. Setting this option does not stop Exim from
+  making IPv6 calls: if an MX lookup returns AAAA records, Exim will use them.
+  What it does is to stop Exim looking for AAAA records explicitly.
+
+. The -G option is ignored (another Sendmail thing).
+
+. If no_bounce_return_message is set, the original message is not included in
+  bounce messages. If you want to include additional information in the bounce
+  message itself, you can use the existing errmsg_file and errmsg_text
+  facilities.
+
+. -bdf runs the daemon in the foreground (i.e. not detached from the terminal),
+  even when no debugging is requested.
+
+. Options for changing Exim's behaviour on receiving IPv4 options have been
+  abolished. Exim now always refuses calls that set these options, and logs the
+  incident. The abolished options are kill_ip_options, log_ip_options, and
+  refuse_ip_options.
+
+. The pattern for each errors_copy entry is now matched as an item in an
+  address list.
+
+. A number of options and variables that used the word "errmsg" have been
+  changed to use "bounce" instead, because it seems that "bounce message" is
+  now a reasonably well-understood term. I used it in the book and am now using
+  it in the manual; it's a lot less cumbersome than "delivery error
+  notification message". The changes are:
+
+  $errmsg_recipient          => $bounce_recipient
+  errmsg_file                => bounce_message_file
+  errmsg_text                => bounce_message_text
+  ignore_errmsg_errors_after => ignore_bounce_errors_after
+
+  For consistency, warnmsg_file has been changed to warn_message_file. However,
+  the two variables $warnmsg_delay and $warnmsg_recipients are unchanged.
+
+  The hide_child_in_errmsg option has not changed, because it applies to both
+  bounce and delay warning messages.
+
+. smtp_accept_max_per_host is now an expanded string, so it can be varied on
+  a per-host basis. However, because this test happens in the daemon before it
+  forks, the expansion should be kept as simple as possible (e.g. just inline
+  tests of $sender_host_address).
+
+. The retry rules can now recognize the error "auth_failed", which happens when
+  authentication is required, but cannot be done.
+
+. There's a new option called local_sender_retain which can be set if
+  no_local_from_check is set. It causes Sender: headers to be retained in
+  locally-submitted messages.
+
+. The -dropcr command line option now turns CRLF into LF, and leaves isolated
+  CRs alone. Previously it simply dropped _all_ CR characters. There is now
+  also a drop_cr main option which, if turned on, assumes -dropcr for all
+  non-SMTP input.
+
+
+Removal of Obsolete Things
+--------------------------
+
+. The obsolete values "fail_soft" and "fail_hard" for the "self" option have
+  been removed.
+
+. The obsolete "log" command has been removed from the filter language.
+
+. "service" was an obsolete synonym for "port" when specifying IP port numbers.
+  It has been removed.
+
+. The obsolete option collapse_source_routes has been removed. It has done
+  nothing since release 3.10.
+
+. The obsolete from_hack option in appendfile and pipe transports has been
+  removed.
+
+. The obsolete ipv4_address_lookup has been abolished (dns_ipv4_lookup has been
+  a synonym for some time, but it's changed - see above).
+
+. The obsolete generic transport options add_headers and remove_headers have
+  been abolished. The new names, headers_add and headers_remove, have been
+  available for some time.
+
+Philip Hazel
+February 2002
diff --git a/doc/GnuTLS-FAQ.txt b/doc/GnuTLS-FAQ.txt
new file mode 100644 (file)
index 0000000..8970875
--- /dev/null
@@ -0,0 +1,410 @@
+Using Exim 4.80+ with GnuTLS
+============================
+
+(1) I'm having problems building with GnuTLS 1, why?
+(2) What changed?  Why?
+(3) I'm seeing:
+    "(gnutls_handshake): A TLS packet with unexpected length was received"
+    Why?
+(4) What's the deal with MD5?
+(5) What happened to gnutls_require_kx / gnutls_require_mac /
+    gnutls_require_protocols?
+(6) What's the deal with tls_dh_max_bits?  What's DH?
+(7) What's a Priority String?
+(8) How do I use tls_require_ciphers?
+(9) How do I test STARTTLS support?
+
+
+
+(1): I'm having problems building with GnuTLS 1, why?
+-----------------------------------------------------
+
+GnuTLS's library interface has changed and Exim uses the more current
+interface.  Since GnuTLS is security critical code, you should probably update
+to a supported release.
+
+If updating GnuTLS is not an option, then build Exim against OpenSSL instead.
+
+If neither is an option, then you might build Exim with the rule
+"SUPPORT_TLS=yes" commented out in "Local/Makefile", so that your Exim build
+no longer has TLS support.
+
+If you need to keep TLS support, and you can't use OpenSSL, then you'll have
+to update the GnuTLS you have installed.  Sorry.
+
+We've tested the build of Exim back as far as GnuTLS 2.8.x; most development
+work is done with 2.12 and tested on 2.10 and 3.x.
+
+If you have to pick a version to upgrade to, use GnuTLS 3.x if available.  The
+GnuTLS developers took advantage of the version bump to add an error code
+return value which makes debugging some problems a lot easier.
+
+
+
+(2): What changed?  Why?
+------------------------
+
+The GnuTLS provider integration in Exim was overhauled, rewritten but with
+some copy/paste, because building Exim against more current releases of GnuTLS
+was issuing deprecation warnings from the compiler.
+
+When a library provider marks up the include files so that some function calls
+will cause the compiler/linker to emit deprecation warnings, it's time to pay
+serious attention.  A future release might not work at all.  Using the new
+APIs may mean that Exim will *stop* working with older releases of GnuTLS.
+The GnuTLS support in Exim was overhauled in Exim 4.80.  In prior releases,
+Exim hard-coded a lot of algorithms and constrained what could happen.  In
+Exim 4.79, we added to the hard-coded list just enough to let TLSv1.1 and
+TLSv1.2 be negotiated, but not actually support the mandatory algorithms of
+those protocol versions.  When Exim's GnuTLS integration was originally
+written, there was no other choice than to make Exim responsible for a lot of
+this.  In the meantime, GnuTLS has improved.
+
+With the rewrite, we started using the current API and leaving a lot more
+responsibility for TLS decisions to the library.
+
+The GnuTLS developers added "priority strings" (see Q7), which provide an
+interface exposed to the configuration file for a lot of the tuning.
+
+The GnuTLS policy is to no longer support MD5 in certificates.  Exim had
+previously been immune to this policy, but no longer.  See Q4.
+
+
+
+(3): I'm seeing "A TLS packet with unexpected length was received".  Why?
+-------------------------------------------------------------------------
+
+The most likely reason is that the client dropped the connection during
+handshake, because their library disliked some aspect of the negotiation.
+
+In GnuTLS 2, an EOF on the connection is reported with an error code for
+packets being too large, and the above is the string returned by the library
+for that error code.  In GnuTLS 3, there's a specific error code for EOF and
+the diagnostic will be less confusing.
+
+Most likely cause is an MD5 hash used in a certificate.  See Q4 below.
+Alternatively, the client dislikes the size of the Diffie-Hellman prime
+offered by the server; if lowering the value of the "tls_dh_max_bits" Exim
+option fixes the problem, this was the cause.  See Q6.
+
+
+
+(4): What's the deal with MD5?
+------------------------------
+
+MD5 is a hash algorithm.  Hash algorithms are used to reduce a lot of data
+down to a fairly short value, which is supposed to be extremely hard to
+manipulate to get a value of someone's choosing.  Signatures, used to attest
+to identity or integrity, rely upon this manipulation being effectively
+impossible, because the signature is the result of math upon the hash result.
+Without hash algorithms, signatures would be longer than the text being
+signed.
+
+MD5 was once very popular.  It still is far too popular.  Real world attacks
+have been proven possible against MD5.  Including an attack against PKI
+(Public Key Infrastructure) certificates used for SSL/TLS.  In that attack,
+the attackers got a certificate for one identity but were able to then publish
+a certificate with the same signature but a different identity.  This
+undermines the whole purpose of having certificates.
+
+So GnuTLS stopped trusting any certificate with an MD5-based hash used in it.
+The world has been hurriedly moving away from MD5 in certificates for a while.
+If you still have such a certificate, you should move too.
+
+If you paid someone for your certificate, they should be willing to reissue
+the certificate with a different algorithm, for no extra money.  If they try
+to charge money to replace their defective product, buy from someone else
+instead.  Part of the reason for paying money on a recurring basis is to cover
+the ongoing costs of proving a trust relationship, such as providing
+revocation protocols.  This is just another of those ongoing costs you have
+already paid for.
+
+
+
+(5): ... gnutls_require_kx / gnutls_require_mac / gnutls_require_protocols?
+---------------------------------------------------------------------------
+
+These Exim options were used to provide fine-grained control over the TLS
+negotiation performed by GnuTLS.  They required explicit protocol knowledge
+from Exim, which vastly limited what GnuTLS could do and involved the Exim
+maintainers in decisions which aren't part of their professional areas of
+expertise.  The need for Exim to be able to do this went away when GnuTLS
+introduced Priority Strings (see Q7).
+
+If you were using these options before, then you're already an expert user and
+should be able to easily craft a priority string to accomplish your goals.
+Set the Exim "tls_require_ciphers" value accordingly.  There is a main section
+option of this name, used for Exim receiving inbound connections, and an SMTP
+driver transport option of this name, used for Exim establishing outbound
+connections.
+
+
+
+(6): What's the deal with tls_dh_max_bits?  What's DH?
+------------------------------------------------------
+
+You can avoid all of the tls_dh_max_bits issues if you leave "tls_dhparam"
+unset, so that you get one of the standard built-in primes used for DH.
+
+
+DH, Diffie-Hellman (or Diffie-Hellman-Merkle, or something naming Williamson)
+is the common name for a way for two parties to a communication stream to
+exchange some private random data so that both end up with a shared secret
+which no eavesdropper can get.  It does not provide for proof of the identity
+of either party, so on its own is subject to man-in-the-middle attacks, but is
+often combined with systems which do provide such proof, improving them by
+separating the session key (the shared secret) from the long-term identity,
+and so protecting past communications from a break of the long-term identity.
+
+To do this, the server sends to the client a very large prime number; this is
+in the clear, an attacker can see it.  This is not a problem; it's so not a
+problem, that there are standard named primes which applications can use, and
+which Exim now supports.
+
+The size of the prime number affects how difficult it is to break apart the
+shared secret and decrypt the data.  As time passes, the size required to
+provide protection against an adversary climbs: computers get more powerful,
+mathematical advances are made, and so on.
+
+Estimates of the size needed are published as recommendations by various
+groups; a good summary of sizes currently recommended, for various
+cryptographic primitives, is available at:
+
+  http://www.keylength.com/en/3/
+
+The GnuTLS folks think the ECRYPT II advice is good.  They know far more of
+such matters than the Exim folks, we just say "er, what they said".
+
+One of the new pieces of the GnuTLS API is a means for an application to ask
+it for guidance and advice on how large some numbers should be.  This is not
+entirely internal to GnuTLS, since generating the numbers is slow, an
+application might want to use a standard prime, etc.  So, in an attempt to get
+away from being involved in cryptographic policy, and to get rid of a
+hard-coded "1024" in Exim's source-code, we switched to asking GnuTLS how many
+bits should be in the prime number generated for use for Diffie-Hellman.  We
+then give this number straight back to GnuTLS when generating a DH prime.
+We can ask for various sizes, and did not expose this to the administrator but
+instead just asked for "NORMAL" protection.
+Literally:
+
+ dh_bits = gnutls_sec_param_to_pk_bits(GNUTLS_PK_DH, GNUTLS_SEC_PARAM_NORMAL);
+
+This API is only available as of GnuTLS 2.12.  Prior to that release, we stuck
+with the old value, for compatibility, so "1024" is still hard-coded.
+Reviewing the page above, you'll see that this is described as "Short-term
+protection against medium organizations, medium-term protection against small
+organizations."
+
+So if you are using an old release of GnuTLS, you can either add to
+Local/Makefile a different value of "EXIM_SERVER_DH_BITS_PRE2_12" or accept
+that your protection might not be adequate to your needs.  We advise updating
+to a more current GnuTLS release and rebuilding Exim against that.
+
+Unfortunately, some TLS libraries have the client side bound how large a DH
+prime they will accept from the server.  The larger the number, the more
+computation required to work with it and the slower that things get.  So they
+pick what they believe to be reasonable upper bounds, and then typically
+forget about it for several years.
+
+Worse, in TLS the DH negotiation happens after a ciphersuite has been chosen,
+so if the client dislikes the value then a different ciphersuite avoiding DH
+can not be negotiated!  The client typically drops the connection, resulting
+in errors to the user and errors in the Exim logs.  With GnuTLS 3, you'll see
+the EOF (End-Of-File) error message in Exim's logs, reported as being part of
+"gnutls_handshake", but with GnuTLS 2 you'll see a log message about a packet
+with an unexpected size.  Unless the client software is written intelligently
+enough to be able to adapt and reconnect forbidding DH, the client will never
+be able to negotiate TLS.
+
+This time around, we discovered that the NSS library used by various Mozilla
+products, Chrome, etc, and most particularly by the Thunderbird mail client,
+has the lowest cap.  In fact, prior to recent updates, their upper limit was
+lower than the value returned by GnuTLS for "NORMAL".  The most recent NSS
+library release raises this, but the most recent Thunderbird release still has
+the old limit.
+
+So Exim had to get involved in cryptography policy decisions again.  We added
+the "tls_dh_max_bits" global option, to set a number used in both OpenSSL and
+GnuTLS bindings for Exim.  In GnuTLS, it clamps the value returned by
+gnutls_sec_param_to_pk_bits(), so that if the returned value is larger than
+tls_dh_max_bits then tls_dh_max_bits would be used instead.
+
+Our policy decision was to default the value of tls_dh_max_bits to the maximum
+supported in the most recent Thunderbird release, and to make this an
+administrator-available option so that administrators can choose to trade off
+security versus compatibility by raising it.
+
+A future release of Exim may even let the administrator tell GnuTLS to ask for
+more or less than "NORMAL".
+
+To add to the fun, the size of the prime returned by GnuTLS when we call
+gnutls_dh_params_generate2() is not limited to be the requested size.  GnuTLS
+has a tendency to overshoot.  2237 bit primes are common when 2236 is
+requested, and higher still have been observed.  Further, there is no API to
+ask how large the prime bundled up inside the parameter is; the most we can do
+is ask how large the DH prime used in an active TLS session is.  Since we're
+not able to use GnuTLS API calls (and exporting to PKCS3 and then calling
+OpenSSL routines would be undiplomatic, plus add a library dependency), we're
+left with no way to actually know the size of the freshly generated DH prime.
+
+Thus we check if the the value returned is at least 10 more than the minimum
+we'll accept as a client (EXIM_CLIENT_DH_MIN_BITS, see below, defaults to
+1024) and if it is, we subtract 10.  Then we reluctantly deploy a strategy
+called "hope".  This is not guaranteed to be successful; in the first code
+pass on this logic, we subtracted 3, asked for 2233 bits and got 2240 in the
+first test.
+
+If you see Thunderbird clients still failing, then as a user who can see into
+Exim's spool directory, run:
+
+$ openssl dhparam -noout -text -in /path/to/spool/gnutls-params-2236 | head
+
+Ideally, the first line will read "PKCS#3 DH Parameters: (2236 bit)".  If the
+count is more than 2236, then remove the file and let Exim regenerate it, or
+generate one yourself and move it into place.  Ideally use "openssl dhparam"
+to generate it, and then wait a very long time; at least this way, the size
+will be correct.
+
+The use of "hope" as a strategy was felt to be unacceptable as a default, so
+late in the RC series for 4.80, the whole issue was side-stepped.  The primes
+used for DH are publicly revealed; moreover, there are selection criteria for
+what makes a "good" DH prime.  As it happens, there are *standard* primes
+which can be used, and are specified to be used for certain protocols.  So
+these primes were built into Exim, and by default exim now uses a 2048 bit
+prime from section 2.2 of RFC 5114.
+
+
+A TLS client does not get to choose the DH prime used, but can choose a
+minimum acceptable value.  For Exim, this is a compile-time constant called
+"EXIM_CLIENT_DH_MIN_BITS" of 1024, which can be overruled in "Local/Makefile".
+
+
+
+(7): What's a Priority String?
+------------------------------
+
+A priority string is a way for a user of GnuTLS to tell GnuTLS how it should
+make decisions about what to do in TLS; it includes which algorithms to make
+available for various roles, what compatibility trade-offs to make, which
+features to enable or disable.
+
+It is exposed to the Mail Administrator in Exim's configuration file as the
+"tls_require_ciphers" option, which exists as a main section option for use in
+Exim as a server, accepting connections, and as an option on Transports using
+the SMTP driver, for use in Exim as a client.  The main section option is
+*not* the default for the transport option, they are entirely independent.
+For both, the default value used by Exim is the string "NORMAL".  (This is not
+the same NORMAL as for DH prime bit size selection in Q6, but a different
+NORMAL.)  See Q8.
+
+The current documentation, for the most recent release of GnuTLS, is available
+online at:
+
+  http://www.gnutls.org/manual/html_node/Priority-Strings.html
+
+Beware that if you are not using the most recent GnuTLS release then this
+documentation will be wrong for you!  You should find the "info" documentation
+which came with GnuTLS to review the available options.  It's under "The TLS
+Handshake Protocol".
+
+$ pinfo --node="Priority Strings" gnutls
+
+(This author is unable to persuade the "info" command-line tool to jump
+straight to the required node, but "pinfo" works.)
+
+To trade off some security for more compatibility, you might set a value of
+"NORMAL:%COMPAT".  See the documentation for more, including lowering security
+even further for more security, forcing clients to use the server's protocol
+suite, and ways to force selection of particular algorithms.
+
+
+
+(8): How do I use tls_require_ciphers?
+--------------------------------------
+
+This is the name of two options in Exim.  One is a main section option, used
+by Exim as a server when a client initiates SSL/TLS negotiation, the other is
+an option on transports which use "driver = smtp", used when Exim initiates
+SSL/TLS as a client talking to a remote server.
+
+The option is expanded and so can take advantage of any variables which have
+been set.  This includes the IP address of the remote side, the port upon
+which a connection was accepted (when a server), and more.  Currently it does
+not have access to $tls_sni, whether as a client or as a server.
+
+This example, for the main section's option, will let the library defaults be
+permitted on the MX port, where there's probably no identity verification
+anyway, and lowers security further by increasing compatibility; but this ups
+the ante on the submission ports where the administrator might have some
+influence on the choice of clients used:
+
+tls_require_ciphers = ${if =={$received_port}{25}\
+                           {NORMAL:%COMPAT}\
+                           {SECURE128}}
+
+Note that during Exim start-up, when this option is sanity-checked, there will
+be no value of $received_port.  In the above example, the checked value will
+thus be "SECURE128".  Be careful to ensure that it always expands safely.
+
+
+
+(9): How do I test STARTTLS support?
+------------------------------------
+
+The best command-line client for debugging specifically SSL/TLS which this
+author has encountered is part of the GnuTLS suite, and is called
+"gnutls-cli".  It's best because it's the only interactive tool which lets the
+user start TLS handshake exactly when they wish, so can choose to use the
+STARTTLS command.
+
+$ gnutls-cli --starttls --crlf --port 587 mail.example.org
+
+After EHLO, to see the capabilities, enter STARTTLS, wait for the response,
+then send EOF.  Typically that's done by typing Ctrl-D at the start of a line.
+The "gnutls-cli" tool will take over, set up TLS (or fail) and by the time it
+returns to await more user input, you're using a secure connection and should
+type your second EHLO.
+
+The "--x509cafile" option may be helpful for checking certificates and
+"--priority" to pass a priority string to the client tool for configuring it.
+
+The --crlf is for strict protocol correctness, but Exim doesn't really need
+it, so "gnutls-cli -s -p 587 mail.example.org" is shorter.
+
+
+For debugging SMTP as a whole, we recommend swaks, "Swiss Army Knife SMTP", by
+John Jetmore (one of the Exim Maintainers).  This has some TLS tuning options;
+it can be found at:
+
+  http://www.jetmore.org/john/code/swaks/
+
+
+For OpenSSL, the "openssl s_client" command helps; you can either set up Exim
+with a listening port which is SSL-on-connect or tell s_client to use
+STARTTLS.
+
+For the former, use the "tls_on_connect_ports" option and the
+"daemon_smtp_ports" option.  Most clients for SSL-on-connect use the port
+which was briefly registered with IANA for this purpose, 465.  So you would
+set something like:
+
+  daemon_smtp_ports = 25 : 465 : 587
+  tls_on_connect_ports = 465
+
+To use s_client with STARTTLS support, use "-starttls smtp" on the
+command-line.  Beware that older versions of OpenSSL did not wait for the SMTP
+banner before sending EHLO, which will fall afoul of the protocol
+synchronisation checks in Exim (used to trip up pump-and-dump spammers); also
+you will not get control of the session until TLS is established.  That said,
+this tool provides more tuning hooks for adjusting how TLS will be set up than
+most.
+
+*BEWARE* that by default, s_client will take any line starting with a capital
+letter "R" to be a request to initiate TLS renegotiation with the server and
+the line will not be sent.  This may trip up "RCPT TO:<someone@example.org>"
+lines in SMTP.  SMTP is not case-sensitive, so type "rcpt to" instead.
+Alternatively, invoke s_client with the "-ign_eof" option to disable this
+R-filtering and a few other features.
+
+
+# END OF FAQ
diff --git a/doc/NewStuff b/doc/NewStuff
new file mode 100644 (file)
index 0000000..1b7ad35
--- /dev/null
@@ -0,0 +1,973 @@
+New Features in Exim
+--------------------
+
+This file contains descriptions of new features that have been added to Exim.
+Before a formal release, there may be quite a lot of detail so that people can
+test from the snapshots or the CVS before the documentation is updated. Once
+the documentation is updated, this file is reduced to a short list.
+
+Version 4.84
+------------
+
+
+Version 4.83
+------------
+
+ 1. If built with the EXPERIMENTAL_PROXY feature enabled, Exim can be
+    configured to expect an initial header from a proxy that will make the
+    actual external source IP:host be used in exim instead of the IP of the
+    proxy that is connecting to it.
+
+ 2. New verify option header_names_ascii, which will check to make sure
+    there are no non-ASCII characters in header names.  Exim itself handles
+    those non-ASCII characters, but downstream apps may not, so Exim can
+    detect and reject if those characters are present.
+
+ 3. New expansion operator ${utf8clean:string} to replace malformed UTF8
+    codepoints with valid ones.
+
+ 4. New malware type "sock".  Talks over a Unix or TCP socket, sending one
+    command line and matching a regex against the return data for trigger
+    and a second regex to extract malware_name.  The mail spoofile name can
+    be included in the command line.
+
+ 5. The smtp transport now supports options "tls_verify_hosts" and
+    "tls_try_verify_hosts".  If either is set the certificate verification
+    is split from the encryption operation. The default remains that a failed
+    verification cancels the encryption.
+
+ 6. New SERVERS override of default ldap server list.  In the ACLs, an ldap
+    lookup can now set a list of servers to use that is different from the
+    default list.
+
+ 7. New command-line option -C for exiqgrep to specify alternate exim.conf
+    file when searching the queue.
+
+ 8. OCSP now supports GnuTLS also, if you have version 3.1.3 or later of that.
+
+ 9. Support for DNSSEC on outbound connections.
+
+10. New variables "tls_(in,out)_(our,peer)cert" and expansion item
+    "certextract" to extract fields from them. Hash operators md5 and sha1
+    work over them for generating fingerprints, and a new sha256 operator
+    for them added.
+
+11. PRDR is now supported dy default.
+
+12. OCSP stapling is now supported by default.
+
+13. If built with the EXPERIMENTAL_DSN feature enabled, Exim will output
+    Delivery Status Notification messages in MIME format, and negociate
+    DSN features per RFC 3461.
+
+
+Version 4.82
+------------
+
+ 1. New command-line option -bI:sieve will list all supported sieve extensions
+    of this Exim build on standard output, one per line.
+    ManageSieve (RFC 5804) providers managing scripts for use by Exim should
+    query this to establish the correct list to include in the protocol's
+    SIEVE capability line.
+
+ 2. If the -n option is combined with the -bP option, then the name of an
+    emitted option is not output, only the value (if visible to you).
+    For instance, "exim -n -bP pid_file_path" should just emit a pathname
+    followed by a newline, and no other text.
+
+ 3. When built with SUPPORT_TLS and USE_GNUTLS, the SMTP transport driver now
+    has a "tls_dh_min_bits" option, to set the minimum acceptable number of
+    bits in the Diffie-Hellman prime offered by a server (in DH ciphersuites)
+    acceptable for security.  (Option accepted but ignored if using OpenSSL).
+    Defaults to 1024, the old value.  May be lowered only to 512, or raised as
+    far as you like.  Raising this may hinder TLS interoperability with other
+    sites and is not currently recommended.  Lowering this will permit you to
+    establish a TLS session which is not as secure as you might like.
+
+    Unless you really know what you are doing, leave it alone.
+
+ 4. If not built with DISABLE_DNSSEC, Exim now has the main option
+    dns_dnssec_ok; if set to 1 then Exim will initialise the resolver library
+    to send the DO flag to your recursive resolver.  If you have a recursive
+    resolver, which can set the Authenticated Data (AD) flag in results, Exim
+    can now detect this.  Exim does not perform validation itself, instead
+    relying upon a trusted path to the resolver.
+
+    Current status: work-in-progress; $sender_host_dnssec variable added.
+
+ 5. DSCP support for outbound connections: on a transport using the smtp driver,
+    set "dscp = ef", for instance, to cause the connections to have the relevant
+    DSCP (IPv4 TOS or IPv6 TCLASS) value in the header.
+
+    Similarly for inbound connections, there is a new control modifier, dscp,
+    so "warn control = dscp/ef" in the connect ACL, or after authentication.
+
+    Supported values depend upon system libraries.  "exim -bI:dscp" to list the
+    ones Exim knows of.  You can also set a raw number 0..0x3F.
+
+ 6. The -G command-line flag is no longer ignored; it is now equivalent to an
+    ACL setting "control = suppress_local_fixups".  The -L command-line flag
+    is now accepted and forces use of syslog, with the provided tag as the
+    process name.  A few other flags used by Sendmail are now accepted and
+    ignored.
+
+ 7. New cutthrough routing feature.  Requested by a "control = cutthrough_delivery"
+    ACL modifier; works for single-recipient mails which are recieved on and
+    deliverable via SMTP.  Using the connection made for a recipient verify,
+    if requested before the verify, or a new one made for the purpose while
+    the inbound connection is still active.  The bulk of the mail item is copied
+    direct from the inbound socket to the outbound (as well as the spool file).
+    When the source notifies the end of data, the data acceptance by the destination
+    is negociated before the acceptance is sent to the source.  If the destination
+    does not accept the mail item, for example due to content-scanning, the item
+    is not accepted from the source and therefore there is no need to generate
+    a bounce mail.  This is of benefit when providing a secondary-MX service.
+    The downside is that delays are under the control of the ultimate destination
+    system not your own.
+
+    The Recieved-by: header on items delivered by cutthrough is generated
+    early in reception rather than at the end; this will affect any timestamp
+    included.  The log line showing delivery is recorded before that showing
+    reception; it uses a new ">>" tag instead of "=>".
+
+    To support the feature, verify-callout connections can now use ESMTP and TLS.
+    The usual smtp transport options are honoured, plus a (new, default everything)
+    hosts_verify_avoid_tls.
+
+    New variable families named tls_in_cipher, tls_out_cipher etc. are introduced
+    for specific access to the information for each connection.  The old names
+    are present for now but deprecated.
+
+    Not yet supported: IGNOREQUOTA, SIZE, PIPELINING.
+
+ 8. New expansion operators ${listnamed:name} to get the content of a named list
+    and ${listcount:string} to count the items in a list.
+
+ 9. New global option "gnutls_allow_auto_pkcs11", defaults false.  The GnuTLS
+    rewrite in 4.80 combines with GnuTLS 2.12.0 or later, to autoload PKCS11
+    modules.  For some situations this is desirable, but we expect admin in
+    those situations to know they want the feature.  More commonly, it means
+    that GUI user modules get loaded and are broken by the setuid Exim being
+    unable to access files specified in environment variables and passed
+    through, thus breakage.  So we explicitly inhibit the PKCS11 initialisation
+    unless this new option is set.
+
+    Some older OS's with earlier versions of GnuTLS might not have pkcs11 ability,
+    so have also added a build option which can be used to build Exim with GnuTLS
+    but without trying to use any kind of PKCS11 support.  Uncomment this in the
+    Local/Makefile:
+
+    AVOID_GNUTLS_PKCS11=yes
+
+10. The "acl = name" condition on an ACL now supports optional arguments.
+    New expansion item "${acl {name}{arg}...}" and expansion condition
+    "acl {{name}{arg}...}" are added.  In all cases up to nine arguments
+    can be used, appearing in $acl_arg1 to $acl_arg9 for the called ACL.
+    Variable $acl_narg contains the number of arguments.  If the ACL sets
+    a "message =" value this becomes the result of the expansion item,
+    or the value of $value for the expansion condition.  If the ACL returns
+    accept the expansion condition is true; if reject, false.  A defer
+    return results in a forced fail.
+
+11. Routers and transports can now have multiple headers_add and headers_remove
+    option lines.  The concatenated list is used.
+
+12. New ACL modifier "remove_header" can remove headers before message gets
+    handled by routers/transports.
+
+13. New dnsdb lookup pseudo-type "a+".  A sequence of "a6" (if configured),
+    "aaaa" and "a" lookups is done and the full set of results returned.
+
+14. New expansion variable $headers_added with content from ACL add_header
+    modifier (but not yet added to messsage).
+
+15. New 8bitmime status logging option for received messages.  Log field "M8S".
+
+16. New authenticated_sender logging option, adding to log field "A".
+
+17. New expansion variables $router_name and $transport_name.  Useful
+    particularly for debug_print as -bt commandline option does not
+    require privilege whereas -d does.
+
+18. If built with EXPERIMENTAL_PRDR, per-recipient data responses per a
+    proposed extension to SMTP from Eric Hall.
+
+19. The pipe transport has gained the force_command option, to allow
+    decorating commands from user .forward pipe aliases with prefix
+    wrappers, for instance.
+
+20. Callout connections can now AUTH; the same controls as normal delivery
+    connections apply.
+
+21. Support for DMARC, using opendmarc libs, can be enabled. It adds new
+    options: dmarc_forensic_sender, dmarc_history_file, and dmarc_tld_file.
+    It adds new expansion variables $dmarc_ar_header, $dmarc_status,
+    $dmarc_status_text, and $dmarc_used_domain.  It adds a new acl modifier
+    dmarc_status.  It adds new control flags dmarc_disable_verify and
+    dmarc_enable_forensic.
+
+22. Add expansion variable $authenticated_fail_id, which is the username
+    provided to the authentication method which failed.  It is available
+    for use in subsequent ACL processing (typically quit or notquit ACLs).
+
+23. New ACL modifer "udpsend" can construct a UDP packet to send to a given
+    UDP host and port.
+
+24. New ${hexquote:..string..} expansion operator converts non-printable
+    characters in the string to \xNN form.
+
+25. Experimental TPDA (Transport Post Delivery Action) function added.
+    Patch provided by Axel Rau.
+
+26. Experimental Redis lookup added. Patch provided by Warren Baker.
+
+
+Version 4.80
+------------
+
+ 1. New authenticator driver, "gsasl".  Server-only (at present).
+    This is a SASL interface, licensed under GPL, which can be found at
+    http://www.gnu.org/software/gsasl/.
+    This system does not provide sources of data for authentication, so
+    careful use needs to be made of the conditions in Exim.
+
+ 2. New authenticator driver, "heimdal_gssapi".  Server-only.
+    A replacement for using cyrus_sasl with Heimdal, now that $KRB5_KTNAME
+    is no longer honoured for setuid programs by Heimdal.  Use the
+    "server_keytab" option to point to the keytab.
+
+ 3. The "pkg-config" system can now be used when building Exim to reference
+    cflags and library information for lookups and authenticators, rather
+    than having to update "CFLAGS", "AUTH_LIBS", "LOOKUP_INCLUDE" and
+    "LOOKUP_LIBS" directly.  Similarly for handling the TLS library support
+    without adjusting "TLS_INCLUDE" and "TLS_LIBS".
+
+    In addition, setting PCRE_CONFIG=yes will query the pcre-config tool to
+    find the headers and libraries for PCRE.
+
+ 4. New expansion variable $tls_bits.
+
+ 5. New lookup type, "dbmjz".  Key is an Exim list, the elements of which will
+    be joined together with ASCII NUL characters to construct the key to pass
+    into the DBM library.  Can be used with gsasl to access sasldb2 files as
+    used by Cyrus SASL.
+
+ 6. OpenSSL now supports TLS1.1 and TLS1.2 with OpenSSL 1.0.1.
+
+    Avoid release 1.0.1a if you can.  Note that the default value of
+    "openssl_options" is no longer "+dont_insert_empty_fragments", as that
+    increased susceptibility to attack.  This may still have interoperability
+    implications for very old clients (see version 4.31 change 37) but
+    administrators can choose to make the trade-off themselves and restore
+    compatibility at the cost of session security.
+
+ 7. Use of the new expansion variable $tls_sni in the main configuration option
+    tls_certificate will cause Exim to re-expand the option, if the client
+    sends the TLS Server Name Indication extension, to permit choosing a
+    different certificate; tls_privatekey will also be re-expanded.  You must
+    still set these options to expand to valid files when $tls_sni is not set.
+
+    The SMTP Transport has gained the option tls_sni, which will set a hostname
+    for outbound TLS sessions, and set $tls_sni too.
+
+    A new log_selector, +tls_sni, has been added, to log received SNI values
+    for Exim as a server.
+
+ 8. The existing "accept_8bitmime" option now defaults to true.  This means
+    that Exim is deliberately not strictly RFC compliant.  We're following
+    Dan Bernstein's advice in http://cr.yp.to/smtp/8bitmime.html by default.
+    Those who disagree, or know that they are talking to mail servers that,
+    even today, are not 8-bit clean, need to turn off this option.
+
+ 9. Exim can now be started with -bw (with an optional timeout, given as
+    -bw<timespec>).  With this, stdin at startup is a socket that is
+    already listening for connections.  This has a more modern name of
+    "socket activation", but forcing the activated socket to fd 0.  We're
+    interested in adding more support for modern variants.
+
+10. ${eval } now uses 64-bit values on supporting platforms.  A new "G" suffix
+    for numbers indicates multiplication by 1024^3.
+
+11. The GnuTLS support has been revamped; the three options gnutls_require_kx,
+    gnutls_require_mac & gnutls_require_protocols are no longer supported.
+    tls_require_ciphers is now parsed by gnutls_priority_init(3) as a priority
+    string, documentation for which is at:
+    http://www.gnutls.org/manual/html_node/Priority-Strings.html
+
+    SNI support has been added to Exim's GnuTLS integration too.
+
+    For sufficiently recent GnuTLS libraries, ${randint:..} will now use
+    gnutls_rnd(), asking for GNUTLS_RND_NONCE level randomness.
+
+12. With OpenSSL, if built with EXPERIMENTAL_OCSP, a new option tls_ocsp_file
+    is now available.  If the contents of the file are valid, then Exim will
+    send that back in response to a TLS status request; this is OCSP Stapling.
+    Exim will not maintain the contents of the file in any way: administrators
+    are responsible for ensuring that it is up-to-date.
+
+    See "experimental-spec.txt" for more details.
+
+13. ${lookup dnsdb{ }} supports now SPF record types. They are handled
+    identically to TXT record lookups.
+
+14. New expansion variable $tod_epoch_l for higher-precision time.
+
+15. New global option tls_dh_max_bits, defaulting to current value of NSS
+    hard-coded limit of DH ephemeral bits, to fix interop problems caused by
+    GnuTLS 2.12 library recommending a bit count higher than NSS supports.
+
+16. tls_dhparam now used by both OpenSSL and GnuTLS, can be path or identifier.
+    Option can now be a path or an identifier for a standard prime.
+    If unset, we use the DH prime from section 2.2 of RFC 5114, "ike23".
+    Set to "historic" to get the old GnuTLS behaviour of auto-generated DH
+    primes.
+
+17. SSLv2 now disabled by default in OpenSSL.  (Never supported by GnuTLS).
+    Use "openssl_options -no_sslv2" to re-enable support, if your OpenSSL
+    install was not built with OPENSSL_NO_SSL2 ("no-ssl2").
+
+
+Version 4.77
+------------
+
+ 1. New options for the ratelimit ACL condition: /count= and /unique=.
+    The /noupdate option has been replaced by a /readonly option.
+
+ 2. The SMTP transport's protocol option may now be set to "smtps", to
+    use SSL-on-connect outbound.
+
+ 3. New variable $av_failed, set true if the AV scanner deferred; ie, when
+    there is a problem talking to the AV scanner, or the AV scanner running.
+
+ 4. New expansion conditions, "inlist" and "inlisti", which take simple lists
+    and check if the search item is a member of the list.  This does not
+    support named lists, but does subject the list part to string expansion.
+
+ 5. Unless the new EXPAND_LISTMATCH_RHS build option is set when Exim was
+    built, Exim no longer performs string expansion on the second string of
+    the match_* expansion conditions: "match_address", "match_domain",
+    "match_ip" & "match_local_part".  Named lists can still be used.
+
+
+Version 4.76
+------------
+
+ 1. The global option "dns_use_edns0" may be set to coerce EDNS0 usage on
+    or off in the resolver library.
+
+
+Version 4.75
+------------
+
+ 1. In addition to the existing LDAP and LDAP/SSL ("ldaps") support, there
+    is now LDAP/TLS support, given sufficiently modern OpenLDAP client
+    libraries.  The following global options have been added in support of
+    this: ldap_ca_cert_dir, ldap_ca_cert_file, ldap_cert_file, ldap_cert_key,
+    ldap_cipher_suite, ldap_require_cert, ldap_start_tls.
+
+ 2. The pipe transport now takes a boolean option, "freeze_signal", default
+    false.  When true, if the external delivery command exits on a signal then
+    Exim will freeze the message in the queue, instead of generating a bounce.
+
+ 3. Log filenames may now use %M as an escape, instead of %D (still available).
+    The %M pattern expands to yyyymm, providing month-level resolution.
+
+ 4. The $message_linecount variable is now updated for the maildir_tag option,
+    in the same way as $message_size, to reflect the real number of lines,
+    including any header additions or removals from transport.
+
+ 5. When contacting a pool of SpamAssassin servers configured in spamd_address,
+    Exim now selects entries randomly, to better scale in a cluster setup.
+
+
+Version 4.74
+------------
+
+ 1. SECURITY FIX: privilege escalation flaw fixed. On Linux (and only Linux)
+    the flaw permitted the Exim run-time user to cause root to append to
+    arbitrary files of the attacker's choosing, with the content based
+    on content supplied by the attacker.
+
+ 2. Exim now supports loading some lookup types at run-time, using your
+    platform's dlopen() functionality.  This has limited platform support
+    and the intention is not to support every variant, it's limited to
+    dlopen().  This permits the main Exim binary to not be linked against
+    all the libraries needed for all the lookup types.
+
+
+Version 4.73
+------------
+
+ NOTE: this version is not guaranteed backwards-compatible, please read the
+       items below carefully
+
+ 1. A new main configuration option, "openssl_options", is available if Exim
+    is built with SSL support provided by OpenSSL.  The option allows
+    administrators to specify OpenSSL options to be used on connections;
+    typically this is to set bug compatibility features which the OpenSSL
+    developers have not enabled by default.  There may be security
+    consequences for certain options, so these should not be changed
+    frivolously.
+
+ 2. A new pipe transport option, "permit_coredumps", may help with problem
+    diagnosis in some scenarios.  Note that Exim is typically installed as
+    a setuid binary, which on most OSes will inhibit coredumps by default,
+    so that safety mechanism would have to be overridden for this option to
+    be able to take effect.
+
+ 3. ClamAV 0.95 is now required for ClamAV support in Exim, unless
+    Local/Makefile sets: WITH_OLD_CLAMAV_STREAM=yes
+    Note that this switches Exim to use a new API ("INSTREAM") and a future
+    release of ClamAV will remove support for the old API ("STREAM").
+
+    The av_scanner option, when set to "clamd", now takes an optional third
+    part, "local", which causes Exim to pass a filename to ClamAV instead of
+    the file content.  This is the same behaviour as when clamd is pointed at
+    a Unix-domain socket.  For example:
+
+      av_scanner = clamd:192.0.2.3 1234:local
+
+    ClamAV's ExtendedDetectionInfo response format is now handled.
+
+ 4. There is now a -bmalware option, restricted to admin users.  This option
+    takes one parameter, a filename, and scans that file with Exim's
+    malware-scanning framework.  This is intended purely as a debugging aid
+    to ensure that Exim's scanning is working, not to replace other tools.
+    Note that the ACL framework is not invoked, so if av_scanner references
+    ACL variables without a fallback then this will fail.
+
+ 5. There is a new expansion operator, "reverse_ip", which will reverse IP
+    addresses; IPv4 into dotted quad, IPv6 into dotted nibble.  Examples:
+
+      ${reverse_ip:192.0.2.4}
+       -> 4.2.0.192
+      ${reverse_ip:2001:0db8:c42:9:1:abcd:192.0.2.3}
+       -> 3.0.2.0.0.0.0.c.d.c.b.a.1.0.0.0.9.0.0.0.2.4.c.0.8.b.d.0.1.0.0.2
+
+ 6. There is a new ACL control called "debug", to enable debug logging.
+    This allows selective logging of certain incoming transactions within
+    production environments, with some care.  It takes two options, "tag"
+    and "opts"; "tag" is included in the filename of the log and "opts"
+    is used as per the -d<options> command-line option.  Examples, which
+    don't all make sense in all contexts:
+
+      control = debug
+      control = debug/tag=.$sender_host_address
+      control = debug/opts=+expand+acl
+      control = debug/tag=.$message_exim_id/opts=+expand
+
+ 7. It has always been implicit in the design and the documentation that
+    "the Exim user" is not root.  src/EDITME said that using root was
+    "very strongly discouraged".  This is not enough to keep people from
+    shooting themselves in the foot in days when many don't configure Exim
+    themselves but via package build managers.  The security consequences of
+    running various bits of network code are severe if there should be bugs in
+    them.  As such, the Exim user may no longer be root.  If configured
+    statically, Exim will refuse to build.  If configured as ref:user then Exim
+    will exit shortly after start-up.  If you must shoot yourself in the foot,
+    then henceforth you will have to maintain your own local patches to strip
+    the safeties off.
+
+ 8. There is a new expansion condition, bool_lax{}.  Where bool{} uses the ACL
+    condition logic to determine truth/failure and will fail to expand many
+    strings, bool_lax{} uses the router condition logic, where most strings
+    do evaluate true.
+    Note: bool{00} is false, bool_lax{00} is true.
+
+ 9. Routers now support multiple "condition" tests.
+
+10. There is now a runtime configuration option "tcp_wrappers_daemon_name".
+    Setting this allows an admin to define which entry in the tcpwrappers
+    config file will be used to control access to the daemon.  This option
+    is only available when Exim is built with USE_TCP_WRAPPERS.  The
+    default value is set at build time using the TCP_WRAPPERS_DAEMON_NAME
+    build option.
+
+11. [POSSIBLE CONFIG BREAKAGE] The default value for system_filter_user is now
+    the Exim run-time user, instead of root.
+
+12. [POSSIBLE CONFIG BREAKAGE] ALT_CONFIG_ROOT_ONLY is no longer optional and
+    is forced on.  This is mitigated by the new build option
+    TRUSTED_CONFIG_LIST which defines a list of configuration files which
+    are trusted; one per line. If a config file is owned by root and matches
+    a pathname in the list, then it may be invoked by the Exim build-time
+    user without Exim relinquishing root privileges.
+
+13. [POSSIBLE CONFIG BREAKAGE] The Exim user is no longer automatically
+    trusted to supply -D<Macro[=Value]> overrides on the command-line.  Going
+    forward, we recommend using TRUSTED_CONFIG_LIST with shim configs that
+    include the main config.  As a transition mechanism, we are temporarily
+    providing a work-around: the new build option WHITELIST_D_MACROS provides
+    a colon-separated list of macro names which may be overridden by the Exim
+    run-time user.  The values of these macros are constrained to the regex
+    ^[A-Za-z0-9_/.-]*$ (which explicitly does allow for empty values).
+
+
+Version 4.72
+------------
+
+ 1. TWO SECURITY FIXES: one relating to mail-spools which are globally
+    writable, the other to locking of MBX folders (not mbox).
+
+ 2. MySQL stored procedures are now supported.
+
+ 3. The dkim_domain transport option is now a list, not a single string, and
+    messages will be signed for each element in the list (discarding
+    duplicates).
+
+ 4. The 4.70 release unexpectedly changed the behaviour of dnsdb TXT lookups
+    in the presence of multiple character strings within the RR. Prior to 4.70,
+    only the first string would be returned.  The dnsdb lookup now, by default,
+    preserves the pre-4.70 semantics, but also now takes an extended output
+    separator specification.  The separator can be followed by a semicolon, to
+    concatenate the individual text strings together with no join character,
+    or by a comma and a second separator character, in which case the text
+    strings within a TXT record are joined on that second character.
+    Administrators are reminded that DNS provides no ordering guarantees
+    between multiple records in an RRset.  For example:
+
+      foo.example.  IN TXT "a" "b" "c"
+      foo.example.  IN TXT "d" "e" "f"
+
+      ${lookup dnsdb{>/ txt=foo.example}}   -> "a/d"
+      ${lookup dnsdb{>/; txt=foo.example}}  -> "def/abc"
+      ${lookup dnsdb{>/,+ txt=foo.example}} -> "a+b+c/d+e+f"
+
+
+Version 4.70 / 4.71
+-------------------
+
+ 1. Native DKIM support without an external library.
+    (Note that if no action to prevent it is taken, a straight upgrade will
+    result in DKIM verification of all signed incoming emails.  See spec
+    for details on conditionally disabling)
+
+ 2. Experimental DCC support via dccifd (contributed by Wolfgang Breyha).
+
+ 3. There is now a bool{} expansion condition which maps certain strings to
+    true/false condition values (most likely of use in conjunction with the
+    and{} expansion operator).
+
+ 4. The $spam_score, $spam_bar and $spam_report variables are now available
+    at delivery time.
+
+ 5. exim -bP now supports "macros", "macro_list" or "macro MACRO_NAME" as
+    options, provided that Exim is invoked by an admin_user.
+
+ 6. There is a new option gnutls_compat_mode, when linked against GnuTLS,
+    which increases compatibility with older clients at the cost of decreased
+    security.  Don't set this unless you need to support such clients.
+
+ 7. There is a new expansion operator, ${randint:...} which will produce a
+    "random" number less than the supplied integer.  This randomness is
+    not guaranteed to be cryptographically strong, but depending upon how
+    Exim was built may be better than the most naive schemes.
+
+ 8. Exim now explicitly ensures that SHA256 is available when linked against
+    OpenSSL.
+
+ 9. The transport_filter_timeout option now applies to SMTP transports too.
+
+
+Version 4.69
+------------
+
+ 1. Preliminary DKIM support in Experimental.
+
+
+Version 4.68
+------------
+
+ 1. The body_linecount and body_zerocount C variables are now exported in the
+    local_scan API.
+
+ 2. When a dnslists lookup succeeds, the key that was looked up is now placed
+    in $dnslist_matched. When the key is an IP address, it is not reversed in
+    this variable (though it is, of course, in the actual lookup). In simple
+    cases, for example:
+
+      deny dnslists = spamhaus.example
+
+    the key is also available in another variable (in this case,
+    $sender_host_address). In more complicated cases, however, this is not
+    true. For example, using a data lookup might generate a dnslists lookup
+    like this:
+
+      deny dnslists = spamhaus.example/<|192.168.1.2|192.168.6.7|...
+
+    If this condition succeeds, the value in $dnslist_matched might be
+    192.168.6.7 (for example).
+
+ 3. Authenticators now have a client_condition option. When Exim is running as
+    a client, it skips an authenticator whose client_condition expansion yields
+    "0", "no", or "false". This can be used, for example, to skip plain text
+    authenticators when the connection is not encrypted by a setting such as:
+
+      client_condition = ${if !eq{$tls_cipher}{}}
+
+    Note that the 4.67 documentation states that $tls_cipher contains the
+    cipher used for incoming messages. In fact, during SMTP delivery, it
+    contains the cipher used for the delivery. The same is true for
+    $tls_peerdn.
+
+ 4. There is now a -Mvc <message-id> option, which outputs a copy of the
+    message to the standard output, in RFC 2822 format. The option can be used
+    only by an admin user.
+
+ 5. There is now a /noupdate option for the ratelimit ACL condition. It
+    computes the rate and checks the limit as normal, but it does not update
+    the saved data. This means that, in relevant ACLs, it is possible to lookup
+    the existence of a specified (or auto-generated) ratelimit key without
+    incrementing the ratelimit counter for that key.
+
+    In order for this to be useful, another ACL entry must set the rate
+    for the same key somewhere (otherwise it will always be zero).
+
+    Example:
+
+    acl_check_connect:
+      # Read the rate; if it doesn't exist or is below the maximum
+      # we update it below
+      deny ratelimit = 100 / 5m / strict / noupdate
+           log_message = RATE: $sender_rate / $sender_rate_period \
+                         (max $sender_rate_limit)
+
+      [... some other logic and tests...]
+
+      warn ratelimit = 100 / 5m / strict / per_cmd
+           log_message = RATE UPDATE: $sender_rate / $sender_rate_period \
+                         (max $sender_rate_limit)
+           condition = ${if le{$sender_rate}{$sender_rate_limit}}
+
+      accept
+
+ 6. The variable $max_received_linelength contains the number of bytes in the
+    longest line that was received as part of the message, not counting the
+    line termination character(s).
+
+ 7. Host lists can now include +ignore_defer and +include_defer, analagous to
+    +ignore_unknown and +include_unknown. These options should be used with
+    care, probably only in non-critical host lists such as whitelists.
+
+ 8. There's a new option called queue_only_load_latch, which defaults true.
+    If set false when queue_only_load is greater than zero, Exim re-evaluates
+    the load for each incoming message in an SMTP session. Otherwise, once one
+    message is queued, the remainder are also.
+
+ 9. There is a new ACL, specified by acl_smtp_notquit, which is run in most
+    cases when an SMTP session ends without sending QUIT. However, when Exim
+    itself is is bad trouble, such as being unable to write to its log files,
+    this ACL is not run, because it might try to do things (such as write to
+    log files) that make the situation even worse.
+
+    Like the QUIT ACL, this new ACL is provided to make it possible to gather
+    statistics. Whatever it returns (accept or deny) is immaterial. The "delay"
+    modifier is forbidden in this ACL.
+
+    When the NOTQUIT ACL is running, the variable $smtp_notquit_reason is set
+    to a string that indicates the reason for the termination of the SMTP
+    connection. The possible values are:
+
+      acl-drop                 Another ACL issued a "drop" command
+      bad-commands             Too many unknown or non-mail commands
+      command-timeout          Timeout while reading SMTP commands
+      connection-lost          The SMTP connection has been lost
+      data-timeout             Timeout while reading message data
+      local-scan-error         The local_scan() function crashed
+      local-scan-timeout       The local_scan() function timed out
+      signal-exit              SIGTERM or SIGINT
+      synchronization-error    SMTP synchronization error
+      tls-failed               TLS failed to start
+
+    In most cases when an SMTP connection is closed without having received
+    QUIT, Exim sends an SMTP response message before actually closing the
+    connection. With the exception of acl-drop, the default message can be
+    overridden by the "message" modifier in the NOTQUIT ACL. In the case of a
+    "drop" verb in another ACL, it is the message from the other ACL that is
+    used.
+
+10. For MySQL and PostgreSQL lookups, it is now possible to specify a list of
+    servers with individual queries. This is done by starting the query with
+    "servers=x:y:z;", where each item in the list may take one of two forms:
+
+    (1) If it is just a host name, the appropriate global option (mysql_servers
+        or pgsql_servers) is searched for a host of the same name, and the
+        remaining parameters (database, user, password) are taken from there.
+
+    (2) If it contains any slashes, it is taken as a complete parameter set.
+
+    The list of servers is used in exactly the same was as the global list.
+    Once a connection to a server has happened and a query has been
+    successfully executed, processing of the lookup ceases.
+
+    This feature is intended for use in master/slave situations where updates
+    are occurring, and one wants to update a master rather than a slave. If the
+    masters are in the list for reading, you might have:
+
+      mysql_servers = slave1/db/name/pw:slave2/db/name/pw:master/db/name/pw
+
+    In an updating lookup, you could then write
+
+      ${lookup mysql{servers=master; UPDATE ...}
+
+    If, on the other hand, the master is not to be used for reading lookups:
+
+      pgsql_servers = slave1/db/name/pw:slave2/db/name/pw
+
+    you can still update the master by
+
+      ${lookup pgsql{servers=master/db/name/pw; UPDATE ...}
+
+11. The message_body_newlines option (default FALSE, for backwards
+    compatibility) can be used to control whether newlines are present in
+    $message_body and $message_body_end. If it is FALSE, they are replaced by
+    spaces.
+
+
+Version 4.67
+------------
+
+ 1. There is a new log selector called smtp_no_mail, which is not included in
+    the default setting. When it is set, a line is written to the main log
+    whenever an accepted SMTP connection terminates without having issued a
+    MAIL command.
+
+ 2. When an item in a dnslists list is followed by = and & and a list of IP
+    addresses, the behaviour was not clear when the lookup returned more than
+    one IP address. This has been solved by the addition of == and =& for "all"
+    rather than the default "any" matching.
+
+ 3. Up till now, the only control over which cipher suites GnuTLS uses has been
+    for the cipher algorithms. New options have been added to allow some of the
+    other parameters to be varied.
+
+ 4. There is a new compile-time option called ENABLE_DISABLE_FSYNC. When it is
+    set, Exim compiles a runtime option called disable_fsync.
+
+ 5. There is a new variable called $smtp_count_at_connection_start.
+
+ 6. There's a new control called no_pipelining.
+
+ 7. There are two new variables called $sending_ip_address and $sending_port.
+    These are set whenever an SMTP connection to another host has been set up.
+
+ 8. The expansion of the helo_data option in the smtp transport now happens
+    after the connection to the server has been made.
+
+ 9. There is a new expansion operator ${rfc2047d: that decodes strings that
+    are encoded as per RFC 2047.
+
+10. There is a new log selector called "pid", which causes the current process
+    id to be added to every log line, in square brackets, immediately after the
+    time and date.
+
+11. Exim has been modified so that it flushes SMTP output before implementing
+    a delay in an ACL. It also flushes the output before performing a callout,
+    as this can take a substantial time. These behaviours can be disabled by
+    obeying control = no_delay_flush or control = no_callout_flush,
+    respectively, at some earlier stage of the connection.
+
+12. There are two new expansion conditions that iterate over a list. They are
+    called forany and forall.
+
+13. There's a new global option called dsn_from that can be used to vary the
+    contents of From: lines in bounces and other automatically generated
+    messages ("delivery status notifications" - hence the name of the option).
+
+14. The smtp transport has a new option called hosts_avoid_pipelining.
+
+15. By default, exigrep does case-insensitive matches. There is now a -I option
+    that makes it case-sensitive.
+
+16. A number of new features ("addresses", "map", "filter", and "reduce") have
+    been added to string expansions to make it easier to process lists of
+    items, typically addresses.
+
+17. There's a new ACL modifier called "continue". It does nothing of itself,
+    and processing of the ACL always continues with the next condition or
+    modifier. It is provided so that the side effects of expanding its argument
+    can be used.
+
+18. It is now possible to use newline and other control characters (those with
+    values less than 32, plus DEL) as separators in lists.
+
+19. The exigrep utility now has a -v option, which inverts the matching
+    condition.
+
+20. The host_find_failed option in the manualroute router can now be set to
+    "ignore".
+
+
+Version 4.66
+------------
+
+No new features were added to 4.66.
+
+
+Version 4.65
+------------
+
+No new features were added to 4.65.
+
+
+Version 4.64
+------------
+
+ 1. ACL variables can now be given arbitrary names, as long as they start with
+    "acl_c" or "acl_m" (for connection variables and message variables), are at
+    least six characters long, with the sixth character being either a digit or
+    an underscore.
+
+ 2. There is a new ACL modifier called log_reject_target. It makes it possible
+    to specify which logs are used for messages about ACL rejections.
+
+ 3. There is a new authenticator called "dovecot". This is an interface to the
+    authentication facility of the Dovecot POP/IMAP server, which can support a
+    number of authentication methods.
+
+ 4. The variable $message_headers_raw provides a concatenation of all the
+    messages's headers without any decoding. This is in contrast to
+    $message_headers, which does RFC2047 decoding on the header contents.
+
+ 5. In a DNS black list, if two domain names, comma-separated, are given, the
+    second is used first to do an initial check, making use of any IP value
+    restrictions that are set. If there is a match, the first domain is used,
+    without any IP value restrictions, to get the TXT record.
+
+ 6. All authenticators now have a server_condition option.
+
+ 7. There is a new command-line option called -Mset. It is useful only in
+    conjunction with -be (that is, when testing string expansions). It must be
+    followed by a message id; Exim loads the given message from its spool
+    before doing the expansions.
+
+ 8. Another similar new command-line option is called -bem. It operates like
+    -be except that it must be followed by the name of a file that contains a
+    message.
+
+ 9. When an address is delayed because of a 4xx response to a RCPT command, it
+    is now the combination of sender and recipient that is delayed in
+    subsequent queue runs until its retry time is reached.
+
+10. Unary negation and the bitwise logical operators and, or, xor, not, and
+    shift, have been added to the eval: and eval10: expansion items.
+
+11. The variables $interface_address and $interface_port have been renamed
+    as $received_ip_address and $received_port, to make it clear that they
+    relate to message reception rather than delivery. (The old names remain
+    available for compatibility.)
+
+12. The "message" modifier can now be used on "accept" and "discard" acl verbs
+    to vary the message that is sent when an SMTP command is accepted.
+
+
+Version 4.63
+------------
+
+1. There is a new Boolean option called filter_prepend_home for the redirect
+   router.
+
+2. There is a new acl, set by acl_not_smtp_start, which is run right at the
+   start of receiving a non-SMTP message, before any of the message has been
+   read.
+
+3. When an SMTP error message is specified in a "message" modifier in an ACL,
+   or in a :fail: or :defer: message in a redirect router, Exim now checks the
+   start of the message for an SMTP error code.
+
+4. There is a new parameter for LDAP lookups called "referrals", which takes
+   one of the settings "follow" (the default) or "nofollow".
+
+5. Version 20070721.2 of exipick now included, offering these new options:
+    --reverse
+        After all other sorting options have bee processed, reverse order
+        before displaying messages (-R is synonym).
+    --random
+        Randomize order of matching messages before displaying.
+    --size
+        Instead of displaying the matching messages, display the sum
+        of their sizes.
+    --sort <variable>[,<variable>...]
+        Before displaying matching messages, sort the messages according to
+        each messages value for each variable.
+    --not
+        Negate the value for every test (returns inverse output from the
+        same criteria without --not).
+
+
+Version 4.62
+------------
+
+1. The ${readsocket expansion item now supports Internet domain sockets as well
+   as Unix domain sockets. If the first argument begins "inet:", it must be of
+   the form "inet:host:port". The port is mandatory; it may be a number or the
+   name of a TCP port in /etc/services. The host may be a name, or it may be an
+   IP address. An ip address may optionally be enclosed in square brackets.
+   This is best for IPv6 addresses. For example:
+
+     ${readsocket{inet:[::1]:1234}{<request data>}...
+
+   Only a single host name may be given, but if looking it up yield more than
+   one IP address, they are each tried in turn until a connection is made. Once
+   a connection has been made, the behaviour is as for ${readsocket with a Unix
+   domain socket.
+
+2. If a redirect router sets up file or pipe deliveries for more than one
+   incoming address, and the relevant transport has batch_max set greater than
+   one, a batch delivery now occurs.
+
+3. The appendfile transport has a new option called maildirfolder_create_regex.
+   Its value is a regular expression. For a maildir delivery, this is matched
+   against the maildir directory; if it matches, Exim ensures that a
+   maildirfolder file is created alongside the new, cur, and tmp directories.
+
+
+Version 4.61
+------------
+
+The documentation is up-to-date for the 4.61 release. Major new features since
+the 4.60 release are:
+
+. An option called disable_ipv6, to disable the use of IPv6 completely.
+
+. An increase in the number of ACL variables to 20 of each type.
+
+. A change to use $auth1, $auth2, and $auth3 in authenticators instead of $1,
+  $2, $3, (though those are still set) because the numeric variables get used
+  for other things in complicated expansions.
+
+. The default for rfc1413_query_timeout has been changed from 30s to 5s.
+
+. It is possible to use setclassresources() on some BSD OS to control the
+  resources used in pipe deliveries.
+
+. A new ACL modifier called add_header, which can be used with any verb.
+
+. More errors are detectable in retry rules.
+
+There are a number of other additions too.
+
+
+Version 4.60
+------------
+
+The documentation is up-to-date for the 4.60 release. Major new features since
+the 4.50 release are:
+
+. Support for SQLite.
+
+. Support for IGNOREQUOTA in LMTP.
+
+. Extensions to the "submission mode" features.
+
+. Support for Client SMTP Authorization (CSA).
+
+. Support for ratelimiting hosts and users.
+
+. New expansion items to help with the BATV "prvs" scheme.
+
+. A "match_ip" condition, that matches an IP address against a list.
+
+There are many more minor changes.
+
+****
diff --git a/doc/OptionLists.txt b/doc/OptionLists.txt
new file mode 100644 (file)
index 0000000..ef61956
--- /dev/null
@@ -0,0 +1,1043 @@
+LISTS OF EXIM OPTIONS
+---------------------
+
+This file contains complete lists of four kinds of Exim option:
+
+  1. Those that can appear in the run time configuration file;
+  2. Those that can be used on the command line;
+  3. Those that can appear in the build time configuration (Local/Makefile);
+  4. Those that can appear in the build time configuration for the Exim monitor
+     (Local/eximon.conf).
+
+This file was last updated for Exim release 4.72.
+
+
+1. RUN TIME OPTIONS
+-------------------
+
+As Exim has developed, new options have been added at each major release. For
+the most part, backwards compatibility has been maintained, and obsolete
+options continue to be recognized. However, incompatible changes took place at
+releases 3.00, and 4.00. In both cases, several groups of options were
+amalgamated into new, extended options, and obsolete options were removed.
+
+The table below contains a complete list of all Exim's current options, along
+with their types, default values, and where they can be used. String options
+that are expanded before use are marked with *. Host lists, domain lists, and
+address lists are always expanded. In some cases the defaults are not fixed
+values, or not short enough to fit in the table. These are indicated by +. Some
+other default values are determined when the Exim binary is compiled; these are
+indicated by ++.
+
+For options that are specific to a particular driver, the fourth column
+contains the driver name, for example, appendfile. Otherwise, it contains
+
+ . `main' for options that appear in the main section of Exim's configuration
+    file;
+
+ . `authenticators' for generic options that apply to any authenticator;
+
+ . `routers' for generic options that apply to any router;
+
+ . `transports' for generic options that apply to any transport.
+
+The fifth column contains the version of Exim in which the option was added, or
+substantially changed. Where no number is given, the option has been in Exim
+since the very early releases. The routers were completely reorganised for
+release 4.00, and so no router options are shown as earlier than 4.00, though
+in fact some of them were inherited from earlier versions.
+
+-----------------------------------------------------------------------------------------
+accept_8bitmime                      boolean         true          main              1.60 changed to true in 4.80
+acl_not_smtp                         string*         unset         main              4.11
+acl_not_smtp_mime                    string*         unset         main              4.51 with content scan
+acl_smtp_auth                        string*         unset         main              4.00
+acl_smtp_connect                     string*         unset         main              4.11
+acl_smtp_data                        string*         unset         main              4.00
+acl_smtp_data_prdr                   string*         unset         main              4.82 with expreimental_prdr
+acl_smtp_dkim                        string*         unset         main              4.70 unless disable_dkim
+acl_smtp_etrn                        string*         unset         main              4.00
+acl_smtp_expn                        string*         unset         main              4.00
+acl_smtp_helo                        string*         unset         main              4.20
+acl_smtp_mail                        string*         unset         main              4.11
+acl_smtp_mailauth                    string*         unset         main              4.21
+acl_smtp_mime                        string*         unset         main              4.50 with content scan
+acl_smtp_predata                     string*         unset         main              4.43
+acl_smtp_quit                        string*         unset         main              4.43
+acl_smtp_rcpt                        string*         unset         main              4.00
+acl_smtp_starttls                    string*         unset         main              4.11
+acl_smtp_vrfy                        string*         unset         main              4.00
+address_data                         string*         unset         routers           4.00
+address_retry_include_sender         boolean         true          smtp              4.64
+address_test                         boolean         true          routers           4.14
+admin_groups                         string list     unset         main              3.02
+allow_domain_literals                boolean         false         main              4.00 replacing forbid_domain_literals
+allow_commands                       string list*    unset         pipe              1.89
+allow_defer                          boolean         false         redirect          4.00
+allow_fail                           boolean         false         redirect          4.00
+allow_filter                         boolean         false         redirect          4.00
+allow_freeze                         boolean         false         redirect          4.00
+allow_fifo                           boolean         false         appendfile        3.13
+allow_localhost                      boolean         false         smtp              1.73
+allow_mx_to_ip                       boolean         false         main              3.14
+allow_symlink                        boolean         false         appendfile
+allow_utf8_domains                   boolean         false         main              4.14
+auth_advertise_hosts                 host list       "*"           main              4.00
+authenticated_sender                 string*         unset         smtp              4.14
+authenticated_sender_force           boolean         false         smtp              4.61
+authenticate_hosts                   host list       unset         smtp              3.13
+auto_thaw                            time            0s            main
+av_scanner                           string*         +             main              4.50 with content scan
+batch_id                             string          unset         appendfile        4.00
+                                                     unset         lmtp              4.00
+                                                     unset         pipe              4.00
+batch_max                            integer         100           appendfile
+                                                     100           lmtp              3.20
+                                                     100           pipe
+bcc                                  string*         unset         autoreply
+bi_command                           string          unset         main
+body_only                            boolean         false         transports        2.05
+bounce_message_file                  string          unset         main              4.00
+bounce_message_text                  string          unset         main              4.00
+bounce_return_body                   boolean         true          main              4.23
+bounce_return_message                boolean         true          main              4.00
+bounce_return_size_limit             int             100K          main              4.23 better name for return_size_limit
+bounce_sender_authentication         string          unset         main              4.00
+callout_domain_negative_expire       time            3h            main              4.11
+callout_domain_positive_expire       time            7d            main              4.11
+callout_negative_expire              time            2h            main              4.11
+callout_positive_expire              time            24h           main              4.11
+callout_random_local_part            string*         +             main              4.11
+cannot_route_message                 string*         unset         routers           4.11
+caseful_local_part                   boolean         false         routers           4.00
+cc                                   string*         unset         autoreply
+check_ancestor                       boolean         false         redirect          4.00
+check_group                          boolean         false         appendfile
+                                                     +             redirect          4.00
+check_local_user                     boolean         false         routers           4.00
+check_log_inodes                     integer         0             main
+check_log_space                      integer         0             main
+check_owner                          boolean         true          appendfile        3.14
+                                                     +             redirect          4.00
+check_rfc2047_length                 boolean         true          main              4.60
+check_secondary_mx                   boolean         false         dnslookup         4.00
+check_spool_inodes                   integer         0             main
+check_spool_space                    integer         0             main
+check_string                         string          "From "       appendfile        3.03
+                                                     unset         pipe              3.03
+check_srv                            string*         unset         dnslookup         4.31
+client_condition                     string*         unset         authenticators    4.68
+client_ignore_invalid_base64         boolean         false         plaintext         4.61
+client_name                          string*         +             cram_md5          3.10
+client_secret                        string*         unset         cram_md5          3.10
+client_send                          string*         unset         plaintext         3.10
+command                              string*         unset         lmtp              3.20
+                                                     unset         pipe
+                                                     unset         queryprogram      4.00
+command_group                        string          unset         queryprogram      4.00
+command_timeout                      time            5m            smtp
+command_user                         string          unset         queryprogram      4.00
+condition                            string*         unset         routers           4.00
+connect_timeout                      time            0s            smtp              1.60
+connection_max_messages              integer         500           smtp              4.00 replaces batch_max
+create_directory                     boolean         true          appendfile
+create_file                          string          "anywhere"    appendfile
+current_directory                    string          unset         transports        4.00
+                                                     unset         queryprogram      4.00
+daemon_smtp_ports                    string          unset         main              1.75  pluralised in 4.21
+daemon_startup_retries               int             9             main              4.52
+daemon_startup_sleep                 time            30s           main              4.52
+data                                 string          unset         redirect          4.00
+data_timeout                         time            5m            smtp
+debug_print                          string*         unset         authenticators    4.00
+                                                     unset         routers           4.00
+                                                     unset         transports        2.00
+delay_after_cutoff                   boolean         true          smtp
+delay_warning                        time list       24h           main
+delay_warning_condition              string*         +             main              1.73
+deliver_drop_privilege               boolean         false         main              4.00
+deliver_queue_load_max               fixed-point     unset         main              1.70
+delivery_date_add                    boolean         false         transports
+delivery_date_remove                 boolean         true          main
+dkim_canon                           string*         unset         smtp              4.70
+dkim_domain                          string list*    unset         smtp              4.72
+dkim_private_key                     string*         unset         smtp              4.70
+dkim_selector                        string*         unset         smtp              4.70
+dkim_sign_headers                    string*         (RFC4871)     smtp              4.70
+dkim_strict                          string*         unset         smtp              4.70
+dkim_verify_signers                  string*         $dkim_signers main              4.70
+directory                            string*         unset         appendfile
+directory_file                       string*         +             appendfile
+directory_mode                       octal-integer   0700          appendfile
+directory_transport                  string*         unset         redirect          4.00
+disable_ipv6                         boolean         false         main              4.61
+disable_logging                      boolean         false         routers           4.11
+                                                     false         transports        4.11
+dmarc_forensic_sender                string          unset         main              4.82 if experimental_dmarc
+dmarc_history_file                   string          unset         main              4.82 if experimental_dmarc
+dmarc_tld_file                       string          unset         main              4.82 if experimental_dmarc
+dns_again_means_nonexist             domain list     unset         main              1.89
+dns_check_names_pattern              string          +             main              2.11
+dns_csa_search_limit                 integer         5             main              4.60
+dns_csa_use_reverse                  boolean         true          main              4.60
+dns_dnssec_ok                        integer         -1            main              4.82
+dns_ipv4_lookup                      boolean         false         main              3.20
+dns_qualify_single                   boolean         true          smtp
+dns_retrans                          time            0s            main              1.60
+dns_retry                            integer         0             main              1.60
+dns_search_parents                   boolean         false         smtp
+dns_use_edns0                        integer         -1            main              4.76
+domains                              domain list     unset         routers           4.00
+driver                               string          unset         authenticators
+                                                     unset         routers           4.00
+                                                     unset         transports
+drop_cr                              boolean         false         main              4.00 became a no-op in 4.21
+dscp                                 string          unset         smtp              4.82
+dsn_from                             string*         +             main              4.67
+envelope_to_add                      boolean         false         transports
+envelope_to_remove                   boolean         true          main
+environment                          string*         unset         pipe              2.95
+errors_copy                          string list*    unset         main
+errors_reply_to                      string          unset         main
+errors_to                            string*         unset         routers           4.00
+escape_string                        string          ">From "      appendfile        3.03
+                                                     unset         pipe              3.03
+exim_group                           string          ++            main
+exim_path                            string          ++            main
+exim_user                            string          ++            main
+expn                                 boolean         true          routers
+extra_local_interfaces               string          unset         main              4.21
+extract_addresses_remove_arguments   boolean         true          main              1.92
+fail_verify                          boolean         false         routers
+fail_verify_recipient                boolean         false         routers           4.00
+fail_verify_sender                   boolean         false         routers           4.00
+fallback_hosts                       string list     unset         routers           4.00
+                                                     unset         smtp
+file                                 string*         unset         appendfile
+                                                     unset         autoreply
+                                                     unset         redirect          4.00
+file_expand                          boolean         false         autoreply
+file_format                          string          unset         appendfile        3.03
+file_must_exist                      boolean         false         appendfile
+file_optional                        boolean         false         autoreply
+file_transport                       string*         unset         redirect          4.00
+filter_prepend_home                  boolean         true          redirect          4.63
+final_timeout                        time            10m           smtp
+finduser_retries                     integer         0             main
+forbid_blackhole                     boolean         false         redirect          4.00
+forbid_exim_filter                   boolean         false         redirect          4.44
+forbid_fail                          boolean         false         redirect          4.00
+forbid_file                          boolean         false         redirect          4.00
+forbid_filter_existstest             boolean         false         redirect          4.00
+forbid_filter_logwrite               boolean         false         redirect          4.00
+forbid_filter_lookup                 boolean         false         redirect          4.00
+forbid_filter_perl                   boolean         false         redirect          4.00
+forbid_filter_readfile               boolean         false         redirect          4.00
+forbid_filter_readsocket             boolean         false         redirect          4.11
+forbid_filter_reply                  boolean         false         redirect          4.00
+forbid_filter_run                    boolean         false         redirect          4.00
+forbid_include                       boolean         false         redirect          4.00
+forbid_pipe                          boolean         false         redirect          4.00
+forbid_sieve_filter                  boolean         false         redirect          4.44
+forbid_smtp_code                     boolean         false         redirect          4.63
+force_command                        boolean         false         pipe              4.82
+freeze_exec_fail                     boolean         false         pipe              1.89
+freeze_signal                        boolean         false         pipe              4.75
+freeze_tell                          boolean         false         main              4.00 replaces freeze_tell_mailmaster
+from                                 string*         unset         autoreply
+gecos_name                           string*         unset         main
+gecos_pattern                        string          unset         main
+gethostbyname                        boolean         false         smtp
+gnutls_allow_auto_pkcs11             boolean         false         main              4.82
+gnutls_compat_mode                   boolean         unset         main              4.70
+gnutls_require_kx                    string*         unset         main              4.67 deprecated, warns
+                                     string*         unset         smtp              4.67 deprecated, warns
+gnutls_require_mac                   string*         unset         main              4.67 deprecated, warns
+                                     string*         unset         smtp              4.67 deprecated, warns
+gnutls_require_protocols             string*         unset         main              4.67 deprecated, warns
+                                     string*         unset         smtp              4.67 deprecated, warns
+group                                string          +             routers           4.00
+                                                     unset         transports        4.00 replaces local option in some transports
+header_line_maxsize                  integer         0 (unset)     main              4.14
+header_maxsize                       integer         1M            main              4.14
+headers                              string*         unset         autoreply
+headers_add                          string*         unset         routers           4.00
+                                                     unset         transports
+headers_charset                      string          ++            main              4.21
+headers_only                         boolean         false         transports        2.05
+headers_remove                       string*         unset         routers           4.00
+                                                     unset         transports
+headers_rewrite                      string          unset         transports        3.20
+helo_accept_junk_hosts               host list       unset         main              3.00
+helo_allow_chars                     string          ""            main              4.02
+helo_lookup_domains                  domain list     unset         main              4.00
+helo_data                            string*   "$primary_hostname" smtp              3.21
+helo_try_verify_hosts                host list       unset         main              4.00
+helo_verify_hosts                    host list       unset         main              1.73
+hide_child_in_errmsg                                 false         redirect          4.00
+hold_domains                         domain list     unset         main              1.70
+home_directory                       string*         unset         transports        4.00 replaces individual options
+host_all_ignored                     string          "defer"       manualroute       4.67
+host_find_failed                     string          "freeze"      manualroute       4.00
+host_lookup                          host list       unset         main              3.00
+host_lookup_order                    string list  "bydns:byaddr"   main              4.30
+host_reject_connection               host list       unset         main              4.00
+hosts                                string          unset         iplookup          4.00
+                                     string list*    unset         smtp
+hosts_avoid_esmtp                    host list       unset         smtp              4.21
+hosts_avoid_pipelining               host list       unset         smtp              4.67
+hosts_avoid_tls                      host list       unset         smtp              3.20
+hosts_connection_nolog               host list       unset         main              4.43
+hosts_max_try                        integer         5             smtp              3.20
+hosts_max_try_hardlimit              integer         50            smtp              4.50
+hosts_nopass_tls                     host list       unset         smtp              4.00
+hosts_override                       boolean         false         smtp              2.11
+hosts_randomize                      boolean         false         manualroute       4.00
+                                                     false         smtp              3.14
+hosts_require_auth                   host list       unset         smtp              4.00
+hosts_require_ocsp                   host list       unset         smtp              4.82 if experimental_ocsp
+hosts_require_tls                    host list       unset         smtp              3.20
+hosts_treat_as_local                 domain list     unset         main              1.95
+hosts_try_auth                       host list       unset         smtp              4.00
+hosts_try_prdr                       host list       unset         smtp              4.82 if experimental_prdr
+ibase_servers                        string          unset         main              4.23
+ignore_bounce_errors_after           time            0s            main              4.00
+ignore_eacces                        boolean         false         redirect          4.00
+ignore_enotdir                       boolean         false         redirect          4.00
+ignore_fromline_hosts                host list       unset         main
+ignore_fromline_local                boolean         false         main              2.05
+ignore_status                        boolean         false         pipe
+ignore_target_hosts                  host list       unset         routers           4.00
+include_directory                    string          unset         redirect          4.00
+initgroups                                           false         routers           4.00
+interface                            string list     unset         smtp              1.70
+keep_malformed                       time            4d            main
+keepalive                            boolean         true          smtp              2.05
+ldap_default_servers                 string list     unset         main              3.02
+ldap_version                         int             2 or 3        main              4.14
+local_from_check                     boolean         true          main              3.14
+local_from_prefix                    string          unset         main              3.14
+local_from_suffix                    string          unset         main              3.14
+local_interfaces                     string list     unset         main              1.60
+local_part_prefix                    string          unset         routers           4.00 replaces prefix
+local_part_prefix_optional           boolean         unset         routers           4.00 replaces prefix_optional
+local_part_suffix                    string          unset         routers           4.00 replaces suffix
+local_part_suffix_optional           boolean         unset         routers           4.00 replaces suffix_optional
+local_parts                          string list*    unset         routers           4.00
+local_scan_timeout                   time            5m            main              4.03
+local_sender_retain                  boolean         false         main              4.00
+localhost_number                     string          unset         main
+lock_fcntl_timeout                   time            0s            appendfile        3.14
+lock_flock_timeout                   time            0s            appendfile        4.11
+lock_interval                        time            3s            appendfile
+lock_retries                         integer         10            appendfile
+lockfile_mode                        octal-integer   0600          appendfile
+lockfile_timeout                     time            30m           appendfile
+log                                  string*         unset         autoreply
+log_as_local                         boolean         +             routers           4.00
+log_file_path                        string list     ++            main
+log_defer_output                     boolean         false         pipe              1.89
+log_fail_output                      boolean         false         pipe              1.60
+log_output                           boolean         false         pipe              1.60
+log_selector                         string          unset         main              4.00
+log_timezone                         boolean         false         main              4.11
+lookup_open_max                      integer         25            main              2.05
+mailbox_filecount                    string*         unset         appendfile        4.43
+mailbox_size                         string*         unset         appendfile        4.43
+maildir_format                       boolean         false         appendfile        1.70
+maildir_retries                      integer         10            appendfile        1.70
+maildir_tag                          string*         unset         appendfile        1.92
+maildir_use_size_file                boolean*        false         appendfile        4.30 expanded in 4.77
+maildirfolder_create_regex           string          unset         appendfile        4.62
+mailstore_format                     boolean         false         appendfile        2.00
+mailstore_prefix                     string*         unset         appendfile        2.00
+mailstore_suffix                     string*         unset         appendfile        2.00
+match_directory                      string*         unset         localuser
+max_output                           integer         20K           pipe
+max_rcpt                             integer         100           smtp              1.60
+max_user_name_length                 integer         0             main
+mbx_format                           boolean         false         appendfile        2.10
+message_body_newlines                boolean         false         main              4.68
+message_body_visible                 integer         500           main
+message_id_header_domain             string*         unset         main              4.11
+message_id_header_text               string*         unset         main
+message_logs                         boolean         true          main              4.10
+message_prefix                       string*         +             appendfile        4.00 replaces prefix
+                                     string*         unset         pipe              4.00 replaces prefix
+message_size_limit                   integer         50M           main
+                                                     0             transports        2.05
+message_suffix                       string*         +             appendfile        4.00 replaces suffix
+                                     string*         unset         pipe              4.00 replaces suffix
+mode                                 octal-integer   0600          appendfile
+                                                     0600          autoreply
+mode_fail_narrower                   boolean         true          appendfile        1.70
+modemask                             octal-integer   022           redirect          4.00
+more                                 boolean         true          routers           4.00
+move_frozen_messages                 boolean         false         main              3.09
+multi_domain                         boolean         true          smtp
+mx_domains                           domain list     unset         dnslookup         4.00
+mx_fail_domains                      domain list     unset         dnslookup         4.43
+mysql_servers                        string list     unset         main              3.03
+never_users                          string list     unset         main
+notify_comsat                        boolean         false         appendfile
+once                                 string*         unset         autoreply
+once_file_size                       integer         0             autoreply         3.20
+once_repeat                          time            0s            autoreply         2.95
+one_time                             boolean         false         redirect          4.00
+openssl_options                      string          +no_sslv2     main              4.73 default changed in 4.80
+optional                             boolean         false         iplookup          4.00
+oracle_servers                       string          unset         main              4.00
+owners                               string list     unset         redirect          4.00
+owngroups                            string list     unset         redirect          4.00
+pass_on_timeout                      boolean         false         routers           4.00
+pass_router                          string          unset         routers           4.00
+path                                 string          "/usr/bin"    pipe
+percent_hack_domains                 domain list     unset         main
+perl_at_start                        boolean         false         main              2.10
+perl_startup                         string          unset         main              2.10
+permit_coredump                      boolean         false         pipe              4.73
+pgsql_servers                        string list     unset         main              3.14
+pid_file_path                        string          ++            main
+pipe_as_creator                      boolean         false         pipe
+pipe_transport                       string*         unset         redirect          4.00
+pipelining_advertise_hosts           host list       "*"           main              4.14
+port                                 integer         0             iplookup          4.00
+                                     string          "smtp"        smtp
+preserve_message_logs                boolean         false         main
+prdr_enable                          boolean         false         main              4.82 if experimental_prdr
+primary_hostname                     string          +             main
+print_topbitchars                    boolean         false         main              1.89
+process_log_path                     string          unset         main              4.21
+prod_requires_admin                  boolean         true          main              1.70
+protocol                             string          "udp"         iplookup          4.00
+                                     string          "smtp"        smtp              3.20
+public_name                          string          unset         authenticators    3.10
+qualify_domain                       string          +             main
+                                     string*         unset         redirect          4.31
+qualify_preserve_domain              boolean         false         redirect          4.00
+qualify_recipient                    string          +             main
+qualify_single                       boolean         true          dnslookup         4.00
+query                                string*         +             iplookup          4.00
+queue_domains                        domain list     unset         main              4.00
+queue_list_requires_admin            boolean         true          main              1.95
+queue_only                           boolean         false         main
+queue_only_file                      string          unset         main              2.05
+queue_only_load                      fixed-point     unset         main
+queue_only_load_latch                boolean         true          main              4.68
+queue_only_override                  boolean         true          main              4.21
+queue_run_in_order                   boolean         false         main              1.70
+queue_run_max                        integer         5             main
+queue_smtp_domains                   domain list     unset         main
+quota                                string*         unset         appendfile        1.60
+quota_directory                      string*         unset         appendfile        4.11
+quota_filecount                      integer         0             appendfile        2.05
+quota_is_inclusive                   boolean         true          appendfile        3.20
+quota_size_regex                     string          unset         appendfile        3.14
+quota_warn_message                   string*         +             appendfile        2.10
+quota_warn_threshold                 string*         0             appendfile        2.10
+rcpt_include_affixes                 boolean         false         transports        4.21
+receive_timeout                      time            0s            main              4.00 replacing accept_timeout
+received_header_text                 string*         +             main
+received_headers_max                 integer         30            main
+recipient_unqualified_hosts          host list       unset         main              4.00 replacing receiver_unqualified_hosts
+recipients_max                       integer         0             main              1.60
+recipients_max_reject                boolean         false         main              1.70
+redirect_router                      string          unset         routers           4.00
+remote_max_parallel                  integer         1             main
+remote_sort_domains                  domain list     unset         main              4.00 replacing remote_sort
+repeat_use                           boolean         true          redirect          4.00
+reply_to                             string*         unset         autoreply         2.05
+reply_transport                      string*         unset         redirect          4.00
+require_files                        string list*    unset         routers           4.00
+reroute                              string*         unset         iplookup          4.00
+response_pattern                     string          unset         iplookup          4.00
+restrict_to_path                     boolean         false         pipe
+retry_data_expire                    time            7d            main              3.03
+retry_include_ip_address             boolean         true          smtp              1.92
+retry_interval_max                   time            24h           main
+retry_use_local_part                 boolean         +             routers           4.00
+                                                     +             transports        4.00 replacing individual options
+return_fail_output                   boolean         false         pipe              1.60
+return_message                       boolean         false         autoreply
+return_output                        boolean         false         pipe
+return_path                          string*         unset         transports        2.05
+return_path_add                      boolean         false         transports
+return_path_remove                   boolean         true          main
+return_size_limit                    integer         100K          main                   renamed bounce_return_size_limit in 4.23
+rewrite                              boolean         true          redirect          4.00
+rewrite_headers                      boolean         true          dnslookup         4.00
+rfc1413_hosts                        host list       *             main
+rfc1413_query_timeout                time            5s            main
+router_home_directory                string*         unset         routers           4.11
+route_data                           string*         unset         manualroute       4.00
+route_list                           string list     unset         manualroute       4.00
+same_domain_copy_routing             boolean         false         dnslookup         4.00
+search_parents                       boolean         false         dnslookup         4.00
+self                                 string          "freeze"      routers           4.00
+sender_unqualified_hosts             host list       unset         main
+senders                              address list    unset         routers           4.00
+serialize_hosts                      host list       unset         smtp              1.60
+server_advertise_condition           string*         unset         authenticators    4.14
+server_channelbinding                bool            false         gsasl             4.80
+server_condition                     string*         unset         authenticators    3.10 (plaintext) 4.64 (others)
+server_hostname                      string*   "$primary_hostname" cyrus_sasl,gsasl,heimdal_gssapi (cyrus-only) 4.80 (others)
+server_keytab                        string*         unset         heimdal_gssapi    4.80
+server_mail_auth_condition           string*         unset         authenticators    3.22
+server_mech                          string          public_name   cyrus_sasl,gsasl  4.43 (cyrus-only) 4.80 (others)
+server_password                      string          unset         gsasl             4.80
+server_prompts                       string*         unset         plaintext         3.10
+server_realm                         string          unset         cyrus_sasl,gsasl  4.43 (cyrus-only) 4.80 (others)
+server_scram_iter                    string*         unset         gsasl             4.80
+server_scram_salt                    string*         unset         gsasl             4.80
+server_secret                        string*         unset         cram_md5          3.10
+server_service                       string          "smtp"  cyrus_sasl,gsasl,heimdal_gssapi  (cyrus-only) 4.80 (others)
+server_set_id                        string*         unset         authenticators    3.10
+shadow_condition                     string*         unset         transports
+shadow_transport                     string          unset         transports
+size_addition                        integer         1024          smtp              1.91
+skip_syntax_errors                   boolean         false         redirect          4.00
+smtp_accept_keepalive                boolean         true          main
+smtp_accept_max                      integer         20            main
+smtp_accept_max_nonmail              integer         10            main              4.11
+smtp_accept_max_nonmail_hosts        host list       *             main              4.14
+smtp_accept_max_per_connection       integer         1000          main              4.00
+smtp_accept_max_per_host             integer         0             main              2.05
+smtp_accept_queue                    integer         0             main
+smtp_accept_queue_per_connection     integer         10            main              2.03
+smtp_accept_reserve                  integer         0             main
+smtp_active_hostname                 string*         unset         main              4.33
+smtp_banner                          string*         +             main
+smtp_check_spool_space               boolean         true          main              2.10
+smtp_connect_backlog                 integer         5             main
+smtp_enforce_sync                    boolean         true          main              4.00
+smtp_etrn_command                    string*         unset         main              1.92
+smtp_etrn_serialize                  boolean         true          main              1.89
+smtp_load_reserve                    fixed-point     unset         main
+smtp_max_synprot_errors              integer         3             main              4.30
+smtp_max_unknown_commands            integer         3             main              4.14
+smtp_ratelimit_hosts                 host list       unset         main              4.00
+smtp_ratelimit_mail                  string          unset         main              4.00
+smtp_ratelimit_rcpt                  string          unset         main              4.00
+smtp_receive_timeout                 time            5m            main
+smtp_reserve_hosts                   host list       unset         main
+smtp_return_error_details            boolean         false         main              4.11
+socket                               string*         unset         lmtp              4.11
+spamd_address                        string*         +             main              4.50 with content scan
+split_spool_directory                boolean         false         main              1.70
+spool_directory                      string          ++            main
+sqlite_lock_timeout                  time            5s            main              4.53
+strict_acl_vars                      boolean         false         main              4.64
+srv_fail_domains                     domain list     unset         dnslookup         4.43
+strip_excess_angle_brackets          boolean         false         main
+strip_trailing_dot                   boolean         false         main
+subject                              string*         unset         autoreply
+syntax_errors_text                   string*         unset         redirect          4.00
+syntax_errors_to                     string          unset         redirect          4.00
+syslog_duplication                   boolean         true          main              4.21
+syslog_facility                      string          unset         main              4.20
+syslog_processname                   string          "exim"        main              4.20
+syslog_timestamp                     boolean         true          main              4.00
+system_filter                        string          unset         main              4.00 replacing message_filter
+system_filter_directory_transport    string          unset         main              4.00 replacing message_filter
+system_filter_file_transport         string          unset         main              4.00 replacing message_filter
+system_filter_group                  string          unset         main              4.00 replacing message_filter
+system_filter_pipe_transport         string          unset         main              4.00 replacing message_filter
+system_filter_reply_transport        string          unset         main              4.00 replacing message_filter
+system_filter_user                   string          unset         main              4.00 replacing message_filter
+tcp_nodelay                          boolean         true          main              4.23
+                                                     true          smtp              4.23
+tcp_wrappers_daemon_name             string*         ++            main              4.73
+temp_errors                          string list     +             pipe              1.95
+text                                 string*         unset         autoreply
+timeout                              time            5m            lmtp              3.20
+                                                     1h            pipe
+                                                     1h            queryprogram      4.00
+                                                     5s            iplookup          4.00
+timeout_defer                        boolean         false         pipe              4.51
+timeout_frozen_after                 time            0s            main              3.20
+timezone                             string          +             main              3.15
+tls_advertise_hosts                  host list       *             main              3.20
+tls_certificate                      string*         unset         main              3.20
+                                                     unset         smtp              3.20
+tls_dh_max_bits                      integer         2236          main              4.80
+tls_dh_min_bits                      integer         1024          smtp              4.82
+tls_dhparam                          string*         unset         main              3.20
+tls_ocsp_file                        string*         unset         main              4.80 if experimental_ocsp
+tls_on_connect_ports                 string          unset         main              4.43
+tls_privatekey                       string*         unset         main              3.20
+                                                     unset         smtp              3.20
+tls_remember_emstp                   boolean         false         main              4.21
+tls_require_ciphers                  string*         unset         smtp              4.00 replaces tls_verify_ciphers
+                                     string*         unset         main              4.33
+tls_sni                              string*         unset         main              4.80
+tls_tempfail_tryclear                boolean         true          smtp              4.05
+tls_try_verify_hosts                 host list       unset         main              4.00
+tls_verify_certificates              string*         unset         main              3.20
+                                                     unset         smtp              3.20
+tls_verify_hosts                     host list       unset         main              3.20
+to                                   string*         unset         autoreply
+translate_ip_address                 string          unset         routers           4.00
+transport                            string*         unset         routers           4.00
+transport_current_directory          string          unset         routers           4.00
+transport_home_directory             string          unset         routers           4.00
+transport_filter                     string          unset         transports
+transport_filter_timeout             time            5m            transports        4.30
+trusted_groups                       string list     unset         main
+trusted_users                        string list     unset         main
+umask                                octal-integer   022           pipe
+unknown_login                        string          unset         main
+unknown_username                     string          unset         main
+unseen                               boolean         false         routers           4.00
+untrusted_set_sender                 boolean         false         main              3.20
+use_bsmtp                            boolean         false         appendfile        4.00
+                                                     false         pipe              4.00
+use_crlf                             boolean         false         appendfile        1.89
+                                                     false         pipe              1.89
+use_fcntl_lock                       boolean         +             appendfile        1.70
+use_flock_lock                       boolean         +             appendfile        4.11
+use_lockfile                         boolean         +             appendfile
+use_mbx_lock                         boolean         +             appendfile        2.10
+use_shell                            boolean         false         pipe              1.70
+user                                 string          +             routers           4.00
+                                                     unset         transports        4.00 replaces individual options
+uucp_from_pattern                    string          +             main              1.75
+uucp_from_sender                     string*         "$1"          main              1.75
+verify                               boolean         true          routers           4.00
+verify_only                          boolean         false         routers           4.00
+verify_recipient                     boolean         true          routers           4.00
+verify_sender                        boolean         true          routers           4.00
+warn_message_file                    string          unset         main              4.00
+widen_domains                        string list     unset         dnslookup         4.00
+write_rejectlog                      boolean         true          main              4.31
+
+
+
+2. COMMAND LINE OPTIONS
+-----------------------
+
+The table below contains a complete list of all Exim's command line options.
+Those marked with # are available only to trusted users, those marked with +
+are available only to admin users, and those marked with * exist only to
+provide compatibility with Sendmail.
+
+--               Terminate options
+--help           Give a little help (not a lot)
+--version        Give version information (alias for -bV)
+-B             * Ignored
+-bd            + Start daemon
+-bdf           + Start daemon and run it in the foreground
+-be              Test string expansion
+-bem             Test string expansion, read test message first
+-bF              Test system filter file
+-bf              Test user filter file
+-bfd             Set domain for filter testing
+-bfl             Set local part for filter testing
+-bfp             Set local part prefix for filter testing
+-bfs             Set local part suffix for filter testing
+-bh              Test incoming SMTP call, omitting callouts
+-bhc             Test incoming SMTP call, with callouts
+-bi            * Run <command>bi_command</command>
+-bI:help         Show list of accepted -bI:<tag> options
+-bm              Accept message on standard input
+-bmalware      + Invoke configured malware scanning against supplied filename
+-bnq             Don't qualify addresses in locally submitted messages
+-bP              Show configuration option settings
+-bp            + List the queue
+-bpa           + ... with generated addresses as well
+-bpc           + ... but just show a count of messages
+-bpr           + ... do not sort
+-bpra          + ... with generated addresses, unsorted
+-bpru          + ... only undelivered addresses, unsorted
+-bpu           + ... only undelivered addresses
+-brt             Test retry rules
+-brw             Test rewriting rules
+-bS              Read batch SMTP on standard input
+-bs              Run SMTP on standard input and output
+-bt              Test address directing and routing
+-bV              Verify version number
+-bv              Test recipient address verification
+-bvs             Test sender address verification
+-bw            + Inetd wait mode
+-C             + Use alternate configuration file
+-D             + Define macro for configuration file
+-d             + Turn on debugging output
+-dd            + Ditto, but if a daemon, not for subprocesses
+-dropcr          Remove CR character in input: became a no-op in 4.21
+-E               Internal use only
+-ex            * Synonym for -oex (for several different x)
+-F               Set calling user name
+-f             # Set calling user address
+-G             * Ignored
+-h             * Ignored
+-i               Dot does not terminate message
+-M             + Force deliver specific message
+-Mar           + Add recipient to message
+-MC              Internal use only
+-MCA             Internal use only
+-MCP             Internal use only
+-MCQ             Internal use only
+-MCS             Internal use only
+-MCT             Internal use only
+-Mc            + Deliver specific message
+-Mes           + Edit message sender
+-Mf            + Freeze message(s)
+-Mg            + Give up (bounce) message(s)
+-Mmad          + Mark all recipients delivered
+-Mmd           + Mark one recipient delivered
+-Mrm           + Remove message(s) (no bounce)
+-Mset          + Load message's variables when testing with -be
+-Mt            + Thaw message(s)
+-Mvb           + View message body
+-Mvc           + View entire message in RFC 2822 format
+-Mvh           + View message header
+-Mvl           + View message log
+-m             * Ignored
+-N             + Deliver without transporting
+-n             * Ignored
+-O             * Ignored
+-oA            * Supply argument for <option>-bi</option>
+-oB              Set max messages down one connection
+-odb             Background delivery
+-odf             Foreground delivery
+-odi             Foreground delivery
+-odq             Queue message; do not deliver
+-odqs            ... do not do SMTP deliveries
+-oee             Error sent by mail; zero return code
+-oem             Error sent by mail; non-zero return code
+-oep             Error written to standard error stream
+-oeq           * Error written to standard error stream
+-oew           * Error sent by mail; non-zero return code
+-oi              Dot does not terminate message
+-oitrue        * Dot does not terminate message
+-oMa           # Supply host address
+-oMaa          # Supply authenticator name
+-oMai          # Supply authenticated id
+-oMas          # Supply authenticated sender
+-oMi           # Supply interface address
+-oMm           # Supply message reference
+-oMr           # Supply protocol name
+-oMs           # Supply host name
+-oMt           # Supply ident string
+-om            * Ignored
+-oo            * Ignored
+-oP            * Specify path for daemon's pid file
+-or              Timeout non-SMTP messages
+-os              Timeout for SMTP messages
+-ov            * Verbose; same as -v
+-oX              Alternative port for daemon
+-pd              Delay Perl interpreter start
+-ps              Do not delay Perl interpreter start
+-p<r>:<s>      * Same as -oMr <r> -oMs <s>
+-q             + Run the queue                    )
+-qf            + ... force delivery               ) Other combinations are
+-qff           + ... and include frozen messages  ) possible. The syntax is
+-qi            + ... initial deliveries only      )
+-ql            + ... local deliveries only        )   -q[q][f][f][i|l][time]
+-qq            + Two-stage queue run              )
+-qR            * Same as -R
+-qS            * Same as -S
+-R               Select by recipient in queue run
+-Rf              ... with forcing
+-Rff             ... and frozen messages
+-Rr              ... using regular expression
+-Rrf             ... with forcing
+-Rrff            ... and frozen messages
+-r             * Synonym for -f
+-S               Select by sender in queue run
+-Sf              ... with forcing
+-Sff             ... and frozen messages
+-Sr              ... using regular expression
+-Srf             ... with forcing
+-Srff            ... and frozen messages
+-Tqt           * Used by Exim test suite; not recognized in normal use
+-t               Take recipients from header lines
+-ti            * Same as -t -i
+-tls-on-connect  Do TLS on startup (for legacy clients)
+-U             * Ignored
+-v               Verbose - shows SMTP dialogue and other delivery info
+-x               Ignored (AIX compatibility)
+
+
+3. BUILD TIME OPTIONS FOR EXIM
+------------------------------
+
+The table below contains a "mostly" complete list of options that can be
+set in Local/Makefile when building Exim.  The only items not included are
+those whose names can be trivially derived from rules stated below.
+
+More information about individual options can be found in src/EDITME and
+OS/Makefile-Default.
+
+The second column below gives the type of option:
+
+ . `system' means the option is concerned with the operating system;
+
+ . `driver' means the option selects a driver to be included in the binary;
+
+ . `lookup' means the option selects a lookuptype to be included in the binary;
+
+ . `mandatory' means the option is required to be supplied;
+
+ . `recommended' means the option is recommended to be supplied;
+
+ . `optional' means what it says;
+
+Those marked with * are specialized and are unlikely to be required in most
+installations. Those that are marked with ** are commonly set in OS-specific
+Makefiles. If you use any of these in your Local/Makefile, you may need to
+reproduce some of the OS-specific settings. For example, in the Makefile for
+Solaris (which is actually called OS/Makefile-SunOS5), there is
+
+  LIBS=-lsocket -lnsl -lkstat
+
+If you use LIBS to add extra libraries, you must also include the OS ones in
+your setting. It is better, in this particular case, to use EXTRALIBS, which is
+empty by default, and is provided for just this reason. Of course, if you do
+actually want to modify a setting from the OS-specific file, there is nothing
+to stop you overriding it in your Local/Makefile.
+
+When building Exim with dynamically loaded lookup support, for "LOOKUP_FOO",
+you can define "LOOKUP_FOO_INCLUDE" and "LOOKUP_FOO_LIBS", for includes and
+libraries specific only to that module.  These rules are only used where the
+relevant lookup is a module.  These options are not explicitly listed below.
+
+Variables with names endined `_PC' are used for pkg-config integration; setting
+the value to the name of a pkg-config package will cause Exim's build system to
+query the --cflags and --libs for the given name, when building anything
+dependent upon the component associated with the `_PC' variable.  For lookups,
+if the lookup is dynamically loaded, then this *replaces* the use of the
+corresponding _INCLUDE and _LIBS options, which will be ignored.  If the lookup
+is statically loaded into Exim, the results will be inserted into the
+"LOOKUP_INCLUDE" and "LOOKUP_LIBS" options.  For authenticator, the results
+will be inserted into the "CFLAGS" and "AUTH_LIBS" options.  These options are
+only listed below for the TLS implementation cases.
+
+
+Option                       Type         Description
+------------------------------------------------------------------------------
+
+ALT_CONFIG_PREFIX            optional     restricts location of -C files
+APPENDFILE_MODE              optional*
+APPENDFILE_DIRECTORY_MODE    optional*
+APPENDFILE_LOCKFILE_MODE     optional*
+AR                           system       command to build a library
+AUTH_CRAM_MD5                driver       include cram_md5 authenticator
+AUTH_CYRUS_SASL              driver       include Cyrus SASL authenticator
+AUTH_GSASL                   driver       include GNU SASL authenticator
+AUTH_HEIMDAL_GSSAPI          driver       include Heimdal GSSAPI authenticator
+AUTH_LIBS                    system       library linkage for authenticators
+AUTH_PLAINTEXT               driver       include plaintext authenticator
+AUTH_SPA                     driver       include SPA (NTLM) authenticator
+AUTH_VARS=3                  optional*    number of $auth variables
+BASENAME_COMMAND             system**     path to basename
+BASE_62=62                   optional*    not normally changed for Unix
+BIN_DIRECTORY                mandatory    Exim binary directory
+CC                           system**     C compiler
+CFLAGS                       system**     flags for C compiler
+CFLAGS_DYNAMIC               system**     extra flags needed for .so modules
+CHGRP_COMMAND                system**     path to chgrp
+CHOWN_COMMAND                system**     path to chown
+COMPRESS_COMMAND             system       path to a compress command
+COMPRESS_SUFFIX              system       suffix added to compressed files
+CONFIGURE_FILE               mandatory    Exim runtime configuration file
+CONFIGURE_FILE_USE_EUID      optional*
+CONFIGURE_FILE_USE_NODE      optional*
+CONFIGURE_GROUP              optional*    alternate group for configuration file
+CONFIGURE_OWNER              optional*    alternate owner for configuration file
+CYRUS_PWCHECK_SOCKET         optional     socket for pwcheck daemon
+DBMLIB                       optional**   location of DBM library
+DB_DIRECTORY_MODE            optional*    mode for hints directory
+DB_LOCKFILE_MODE             optional*    mode for hints lock files
+DB_LOCK_TIMEOUT              optional*    timeout for hints lock files
+DB_MODE                      optional*    mode for hints files
+DEFAULT_CRYPT                optional     default crypt() function
+DELIVER_IN_BUFFER_SIZE       optional*
+DELIVER_OUT_BUFFER_SIZE      optional*
+DISABLE_DKIM                 optional     disables DKIM support
+DISABLE_DNSSEC               optional     disables attempts to use DNSSEC
+DISABLE_D_OPTION             optional     disables -D option
+ERRNO_QUOTA                  optional*    error code for system quota failures
+EXICYCLOG_MAX                optional     number of old log files to keep
+EXIMDB_DIRECTORY_MODE        optional*    for hints database directory
+EXIMDB_LOCKFILE_MODE         optional*    for hints lock files
+EXIMDB_MODE                  optional*    mode for hints files
+EXIMON_TEXTPOP               system**
+EXIM_CHMOD                   optional*
+EXIM_GROUP                   mandatory    group to use for Exim
+EXIM_MONITOR                 optional     set to eximon.bin to compile
+EXIM_PERL                    optional
+EXIM_USER                    mandatory    user to use for Exim
+EXIWHAT_EGREP_ARG            system**     to find Exim processes from ps
+EXIWHAT_KILL_SIGNAL          system**     -SIGUSER1 or numerical equivalent
+EXIWHAT_MULTIKILL_CMD        system**
+EXIWHAT_MULTIKILL_ARG        system**
+EXIWHAT_PS_ARG               system**     to list all processes
+EXIWHAT_PS_CMD               system**     path to ps command
+EXPAND_LISTMATCH_RHS         optional*    restore pre-4.77 match_*{}{} behaviour
+EXTRALIBS                    system       additional libraries
+EXTRALIBS_EXIM               system       additional libraries for Exim only
+EXTRALIBS_EXIMON             system       additional libraries for the monitor
+FIXED_NEVER_USERS            optional     can't override at runtime
+HAVE_ICONV                   system       the iconv() function is available
+HAVE_IPV6                    system       include IPv6 support
+HEADERS_CHARSET              optional     charset for decoded header lines
+HEADER_ADD_BUFFER_SIZE       optional*    buffer for header_add()
+HEADER_MAXSIZE               optional*    max memory for message header
+HOSTNAME_COMMAND             system**     path to hostname command
+INCLUDE                      system       path to include files
+INFO_DIRECTORY               optional     directory for Info documentation
+INPUT_DIRECTORY_MODE         optional     mode for input directory
+IPV6_INCLUDE                 system       additional includes for IPv6
+IPV6_LIBS                    system       additional libraries for IPv6
+LDAP_LIB_TYPE                optional     type of LDAP library
+LFLAGS                       system**     link editor flags
+LIBIDENTCFLAGS               system       C flags when compiling libident
+LIBIDENTNAME                 system       name for libident library
+LIBRESOLV                    system**     library for DNS resolver
+LIBS                         system**     additional libraries
+LIBS_EXIM                    system**     additional libraries for Exim ony
+LIBS_EXIMON                  system**     additional libraries for monitor
+LOCAL_SCAN_SOURCE            optional     location of local_scan() source
+LOG_DIRECTORY_MODE           optional     mode for log directory
+LOG_FILE_PATH                optional     path to log files
+LOG_MODE                     optional     mode for log files
+LOOKUP_CDB                   lookup       include cdb lookup
+LOOKUP_DBM                   lookup       include dbm lookup
+LOOKUP_DNSDB                 lookup       include dnsdb lookup
+LOOKUP_DSEARCH               lookup       include dsearch lookup
+LOOKUP_INCLUDE               lookup       include files for lookups
+LOOKUP_LDAP                  lookup       include ldap lookup
+LOOKUP_LIBS                  lookup       include libraries for lookups
+LOOKUP_LSEARCH               lookup       include all lsearch lookups
+LOOKUP_MODULE_DIR            optional*    where dynamic lookup modules live
+LOOKUP_MYSQL                 lookup       include mysql lookup
+LOOKUP_NIS                   lookup       include nis lookup
+LOOKUP_NISPLUS               lookup       include nisplus lookup
+LOOKUP_ORACLE                lookup       include oracle lookup
+LOOKUP_PGSQL                 lookup       include pgsql lookup
+LOOKUP_TESTDB                lookup*
+LOOKUP_WHOSON                lookup       include whoson lookup
+LOOKUP_*_INCLUDE             optional*    include files for dynamic module
+LOOKUP_*_LIBS                optional*    libraries for dynamic module
+MAKE_SHELL                   optional*    shell to use for make
+MAX_FILTER_SIZE              optional*    max file size for filter files
+MAX_INCLUDE_SIZE             optional*    max file size for :include: files
+MAX_LOCALHOST_NUMBER=256     optional*    for when localhost_number is set
+MAX_NAMED_LIST               optional*    max named lists of a given type
+MAX_INTERFACES               system       maximum network interfaces
+MSGLOG_DIRECTORY_MODE        optional*    mode for message log directory
+MV_COMMAND                   system       path to mv command
+NO_SYMLINK                   optional     install doesn't make 'exim" symlink
+PCRE_CONFIG                  system*      use pcre-config for PCRE support
+PCRE_LIBS                    system*      library for using PCRE
+PERL_CC                      system*      compiler for Perl interface code
+PERL_CCOPTS                  system*      flags for same
+PERL_COMMAND                 system       path to Perl
+PERL_LIBS                    system*      library for compiling Perl interface
+PID_FILE_PATH                optional     path to daemon's pid file
+RADIUS_CONFIG_FILE           optional     path to Radius config file
+RADIUS_LIB_TYPE              optional     type of RADIUS library
+RANLIB                       system**     path to ranlib command
+RM_COMMAND                   system       path to rm command
+ROUTER_ACCEPT                driver       include accept router
+ROUTER_DNSLOOKUP             driver       include dnslookup router
+ROUTER_MANUALROUTE           driver       include manualroute router
+ROUTER_IPLITERAL             driver       include ipliteral router
+ROUTER_IPLOOKUP              driver       include iplookup router
+ROUTER_REDIRECT              driver       include redirect router
+ROUTER_QUERYPROGRAM          driver       include queryprogram router
+SPOOL_DIRECTORY              recommended  path to spool directory
+SPOOL_DIRECTORY_MODE         optional     mode of spool directory
+SPOOL_MODE                   optional     mode of spool files
+STRING_SPRINTF_BUFFER_SIZE   optional*    buffer for string_sprintf()
+STRIP_COMMAND                optional*    can be used to strp binaries
+SUPPORT_A6                   optional*    support A6 DNS records
+SUPPORT_CRYPTEQ              optional     support crypteq (if no auths)
+SUPPORT_MAILDIR              optional     support for maildir delivery
+SUPPORT_MAILSTORE            optional     support for mailstore delivery
+SUPPORT_MBX                  optional     support for MBX delivery
+SUPPORT_MOVE_FROZEN_MESSAGES optional*    support for frozen message moving
+SUPPORT_PAM                  optional     support for PAM authentication
+SUPPORT_TLS                  optional     support for TLS encryption over SMTP
+SUPPORT_TRANSLATE_IP_ADDRESS optional*    support for address translation
+SYSLOG_LOG_PID               optional     add pid to syslog lines
+SYSLOG_LONG_LINES            optional     do not split long syslog lines
+SYSTEM_ALIASES_FILE          optional     defaults to /etc/aliases
+TCP_WRAPPERS_DAEMON_NAME     system*      daemon name used by tcpwrappers library
+TIMEZONE_DEFAULT             optional     default for timezone option
+TLS_INCLUDE                  optional     path to include files for TLS
+TLS_LIBS                     optional     additional libraries for TLS
+TMPDIR                       system       value for TMPDIR environment variable
+TRANSPORT_APPENDFILE         driver       include appendfile transport
+TRANSPORT_AUTOREPLY          driver       include autoreply transport
+TRANSPORT_LMTP               driver       include lmtp transport
+TRANSPORT_PIPE               driver       include pipe transport
+TRANSPORT_SMTP               driver       include smtp transport
+TRUSTED_CONFIG_LIST          optional     config files safe to retain privileges
+USE_DB                       system**     use native DB interface
+USE_GNUTLS                   optional     use GnuTLS instead of OpenSSL
+USE_GNUTLS_PC                optional     probably "gnutls"
+USE_OPENSSL_PC               optional     probably "openssl"
+USE_READLINE                 optional     try to load libreadline for -be
+USE_TCP_WRAPPERS             system       link with tcpwrappers
+USE_TDB                      optional     use the tdb DB interface
+WHITELIST_D_MACROS           optional     -Dmacros safe to retain privileges with
+WITH_OLD_CLAMAV_STREAM       optional*    use ClamAV API from before ClamAV 0.95
+X11                          system**     X11 base directory
+X11_LD_LIB                   system**     X11 link library
+XINCLUDE                     system**     X11 include directory
+XLFLAGS                      system**     X11 link time flags
+ZCAT_COMMAND                 system       path to zcat command
+
+
+4. BUILD TIME OPTIONS FOR EXIMON
+--------------------------------
+
+The table below contains a complete list of options that can be set in
+Local/eximon.conf when building the Exim monitor. Where the default is shown as
+** it means that the text string is too long to fit in the table and is instead
+given below. A blank default means that there is no default value.
+
+ACTION_OUTPUT=no                          show output for every action
+ACTION_QUEUE_UPDATE=yes                   update queue display after actions
+BODY_MAX=20000                            maximum body display
+DOMAIN=                                   domain to strip from window title
+LOG_BUFFER=20K                            buffer for log tail
+LOG_DEPTH=300                             depth of log subwindow
+LOG_FONT=**                               font for log display
+LOG_STRIPCHARTS=**                        patterns for stripcharts
+LOG_WIDTH=950                             width of log subwindow
+MENU_EVENT='Shift<Btn1Down>'              keypress for menu
+MIN_HEIGHT=162                            minimum window height
+MIN_WIDTH=103                             minimum window width
+QUALIFY_DOMAIN=                           local domain to strip from addresses
+QUEUE_DEPTH=200                           depth of queue subwindow
+QUEUE_FONT=$LOG_FONT                      font for queue display
+QUEUE_INTERVAL=300                        queue refresh interval
+QUEUE_MAX_ADDRESSES=10                    max addresses to show in queue
+QUEUE_STRIPCHART_NAME=queue               name for queue stripchart
+QUEUE_WIDTH=950                           width of queue subwindow
+SIZE_STRIPCHART=                          request partition size stripchart
+SIZE_STRIPCHART_NAME=space                name for size stripchart
+START_SMALL=no                            if yes, start with small window
+STRIPCHART_INTERVAL=60                    stripchart refresh interval
+TEXT_DEPTH=200                            depth of text windows
+WINDOW_TITLE="${hostname} eximon"         window title
+
+The default for LOG_FONT is
+
+  LOG_FONT=-misc-fixed-medium-r-normal-*-14-140-*-*-*-*-iso8859-1
+
+and the default for LOG_STRIPCHARTS is
+
+  LOG_STRIPCHARTS='/ <= /in/
+                   / => /out/
+                   / => .+ R=local/local/
+                   / => .+ T=[^ ]*smtp/smtp/'
+
+That is, there are four stripcharts, named in, out, local, and smtp. The first
+counts message arrivals, the second counts all deliveries, the third counts
+deliveries where the router's name starts with "local", and the fourth counts
+deliveries where the transport name contains "smtp".
+
+**** End of OptionLists ****
diff --git a/doc/README b/doc/README
new file mode 100644 (file)
index 0000000..a019ab8
--- /dev/null
@@ -0,0 +1,65 @@
+Exim Documentation
+------------------
+
+This directory should contain the following files:
+
+  ChangeLog        most recent log of all changes to Exim
+  NewStuff         features that haven't made it to the manual yet,
+                   and/or a list of newly-added functionality
+  OptionLists.txt  lists of all Exim's options
+  README           this document
+  README.SIEVE     notes on the Sieve support
+  Exim3.upgrade    information about upgrading from release 2.12 to 3.00
+  Exim4.upgrade    information about upgrading from release 3.33 to 4.00
+  dbm.discuss.txt  discussion of DBM libraries
+  filter.txt       specification of filter file contents
+  spec.txt         main specification of Exim
+  experimental-spec.txt  specification of experimental features
+
+
+PostScript
+----------
+
+The Exim specifications are also available in PostScript. This is not included
+in the main distribution because not everyone wants it. Wherever you got this
+distribution from should also have carried another file called
+exim-postscript-<version>.tar.gz which contains the PostScript documentation.
+When de-tarred it creates a directory called exim-postscript-<version> into
+which it places the files doc/filter.ps and doc/spec.ps.
+
+
+HTML
+----
+
+A conversion from the original sources into HTML is done to create the online
+documentation at http://www.exim.org. This set of files is also available for
+installation on other servers. Wherever you got this distribution from should
+also have carried another file called exim-html-<version>.tar.gz which contains
+the HTML documentation. When de-tarred it creates a directory called
+exim-html-<version> into which it places a directory called doc/html containing
+the set of HTML files. The kick off point is the traditional index.html.
+
+
+PDF
+---
+
+The Exim specifications are available in Portable Document Format. Wherever you
+got this distribution from should also have carried another file called
+exim-pdf-<version>.tar.gz which contains the PDF documentation. When de-tarred
+it creates a directory called exim-pdf-<version> into which it places the files
+doc/filter.pdf and doc/spec.pdf.
+
+
+TeXinfo
+-------
+
+A version of the documentation that has been converted to TeXinfo format is
+available in the distribution file exim-texinfo-<version>.gz. When de-tarred it
+creates a directory called exim-texinfo-<version> into which it places the
+files doc/filter.texinfo and doc/spec.texinfo.
+
+The conversion process is automatic, so the result isn't as nice as
+hand-maintained TeXinfo files would be, and some information is lost; for
+example, TeXinfo does not support the use of change bars.
+
+-- End --
diff --git a/doc/README.SIEVE b/doc/README.SIEVE
new file mode 100644 (file)
index 0000000..9b22745
--- /dev/null
@@ -0,0 +1,343 @@
+              Notes on the Sieve implementation for Exim
+
+Exim Filter Versus Sieve Filter
+
+Exim supports two incompatible filters: The traditional Exim filter and
+the Sieve filter. Since Sieve is a extensible language, it is important
+to understand "Sieve" in this context as "the specific implementation
+of Sieve for Exim".
+
+The Exim filter contains more features, such as variable expansion, and
+better integration with the host environment, like external processes
+and pipes.
+
+Sieve is a standard for interoperable filters, defined in RFC 5228,
+with multiple implementations around. If interoperability is important,
+then there is no way around it.
+
+
+Exim Implementation
+
+The Exim Sieve implementation offers the core as defined by RFC 5228,
+the "encoded-character" extension (RFC 5228), the "envelope" test (RFC
+5228), the "fileinto" action (5228), the "copy" parameter (RFC 3894), the
+"vacation" action (5230), the "notify" action (draft-ietf-sieve-notify-12)
+with mailto URIs (draft-ietf-sieve-notify-mailto-05), the
+"i;ascii-numeric" comparator (RFC 2244) and the subaddress parameter
+(RFC 5233).
+
+The Sieve filter is integrated in Exim and works very similar to the
+Exim filter: Sieve scripts are recognized by the first line containing
+"# sieve filter".  When using "keep" or "fileinto" to save a mail into a
+folder, the resulting string is available as the variable $address_file
+in the transport that stores it.  The following routers and transport
+show a typical use of Sieve:
+
+begin routers
+
+localuser_verify:
+  driver = accept
+  domains = +localdomains
+  local_part_suffix = "-*"
+  local_part_suffix_optional
+  check_local_user
+  require_files = $home/.forward
+  verify_only = true
+
+localuser_deliver:
+  driver = redirect
+  domains = +localdomains
+  local_part_suffix = "-*"
+  local_part_suffix_optional
+  sieve_subaddress = "${sg{$local_part_suffix}{^-}{}}"
+  sieve_useraddress = "$local_part"
+  check_local_user
+  require_files = $home/.forward
+  file = $home/.forward
+  check_ancestor
+  allow_filter
+  file_transport = localuser
+  reply_transport = vacation
+  sieve_vacation_directory = $home/mail/vacation
+  verify = false
+
+begin transports
+
+localuser:
+  driver = appendfile
+  file = ${if eq{$address_file}{inbox} \
+              {/var/mail/$local_part} \
+              {${if eq{${substr_0_1:$address_file}}{/} \
+                    {$address_file} \
+                    {$home/mail/$address_file} \
+              }} \
+         }
+  delivery_date_add
+  envelope_to_add
+  return_path_add
+  mode = 0600
+
+vacation:
+  driver = autoreply
+
+Absolute files are stored where specified, relative files are stored
+relative to $home/mail and "inbox" goes to the standard mailbox location.
+To enable "vacation", sieve_vacation_directory is set to the directory
+where vacation databases are held (don't put anything else in that
+directory) and point reply_transport to an autoreply transport.
+Setting the Sieve useraddress and subaddress allows to use the subaddress
+extension.
+
+
+RFC Compliance
+
+Exim requires the first line to be "# sieve filter".  Of course the RFC
+does not enforce that line.  Don't expect examples to work without adding
+it, though.
+
+RFC 5228 requires using CRLF to terminate the end of a line.
+The rationale was that CRLF is universally used in network protocols
+to mark the end of the line.  This implementation does not embed Sieve
+in a network protocol, but uses Sieve scripts as part of the Exim MTA.
+Since all parts of Exim use \n as newline character, this implementation
+does, too.  You can change this by defining the macro RFC_EOL at compile
+time to enforce CRLF being used.
+
+The folder specified by "fileinto" must not contain the character
+sequence ".." to avoid security problems.  RFC 5228 does not specify the
+syntax of folders apart from keep being equivalent to fileinto "INBOX".
+This implementation uses "inbox" instead.
+
+Sieve script errors currently cause that messages are silently filed into
+"inbox".  RFC 5228 requires that the user is notified of that condition.
+This may be implemented in future by adding a header line to mails that
+are filed into "inbox" due to an error in the filter.
+
+The automatic replies generated by "vacation" do not contain an updated
+"references" header field.
+
+
+Semantics Of Keep
+
+The keep command is equivalent to fileinto "inbox": It saves the
+message and resets the implicit keep flag.  It does not set the
+implicit keep flag; there is no command to set it once it has
+been reset.
+
+
+Semantics Of Fileinto
+
+RFC 5228 does not specify if "fileinto" tries to create a mail folder,
+in case it does not exist.  This implementation allows to configure
+that aspect using the appendfile transport options "create_directory",
+"create_file" and "file_must_exist".  See the appendfile transport in
+the Exim specification for details.
+
+
+Allof And Anyof Test
+
+RFC 5228 does not specify if these tests use shortcut/lazy evaluation.
+Exim uses shortcut evaluation.
+
+
+Action Reordering
+
+RFC 5228 does not specify if actions may be executed out of order.
+Exim may execute them out of order, e.g. messages may be filed to
+folders or forwarded in a different order than specified, because
+those actions only setup delivery, but do not execute it themselves.
+
+
+Sieve Syntax And Semantics
+
+RFC 5228 uses a generic grammar as syntax for commands and tests and
+performs many checks during semantic analysis.  Syntax is specified
+by grammar rules, semantics by natural language.  The intention is to
+provide a framework for the syntax that describes current commands as
+well as future extensions, and describing commands by semantics.
+
+The following replacement for section 8.2 gives a grammar for specific
+commands of this implementation, thus removing most of the semantic
+analysis.  Since the parser can not parse unsupported extensions, the
+result is strict error checking of any executed and not executed code
+until "stop" is executed or the end of the script is reached.
+
+8.2. Grammar
+
+The grammar is specified in ABNF with two extensions to describe tagged
+arguments that can be reordered and grammar extensions: { } denotes a
+sequence of symbols that may appear in any order.  Example:
+
+  options = a b c
+  start   = { options }
+
+is equivalent to:
+
+  start   =  ( a b c ) / ( a c b ) / ( b a c ) / ( b c a ) / ( c a b ) / ( c b a )
+
+The symbol =) is used to append to a rule:
+
+  start =  a
+  start =) b
+
+is equivalent to
+
+  start =  a b
+
+The basic Sieve commands are specified using the following grammar, which
+language is a subset of the generic grammar above.  The start symbol is
+"start".
+
+  address-part     =  ":localpart" / ":domain" / ":all"
+  comparator       =  ":comparator" string
+  match-type       =  ":is" / ":contains" / ":matches"
+  string           =  quoted-string / multi-line
+  string-list      =  "[" string *("," string) "]" / string
+  address-test     =  "address" { [address-part] [comparator] [match-type] }
+                      string-list string-list
+  test-list        =  "(" test *("," test) ")"
+  allof-test       =  "allof" test-list
+  anyof-test       =  "anyof" test-list
+  exists-test      =  "exists" string-list
+  false-test       =  "false"
+  true=test        =  "true"
+  header-test      =  "header" { [comparator] [match-type] }
+                      string-list string-list
+  not-test         =  "not" test
+  relop            =  ":over" / ":under"
+  size-test        =  "size" relop number
+  block            =  "{" commands "}"
+  if-command       =  "if" test block *( "elsif" test block ) [ "else" block ]
+  stop-command     =  "stop" { stop-options } ";"
+  stop-options     =
+  keep-command     =  "keep" { keep-options } ";"
+  keep-options     =
+  discard-command  =  "discard" { discard-options } ";"
+  discard-options  =
+  redirect-command =  "redirect" { redirect-options } string ";"
+  redirect-options =
+  require-command  =  "require" { require-options } string-list ";"
+  require-options  =
+  test             =  address-test / allof-test / anyof-test / exists-test
+                      / false-test / true-test / header-test / not-test
+                      / size-test
+  command          =  if-command / stop-command / keep-command
+                      / discard-command / redirect-command
+  commands         =  *command
+  start            =  *require-command commands
+
+The extensions "envelope" and "fileinto" are specified using the following
+grammar extension.
+
+  envelope-test    =  "envelope" { [comparator] [address-part] [match-type] }
+                      string-list string-list
+  test             =/ envelope-test
+
+  fileinto-command =  "fileinto" { fileinto-options } string ";"
+  fileinto-options =
+  command          =/ fileinto-command
+
+The extension "copy" is specified as:
+
+  fileinto-options =) ":copy"
+  redirect-options =) ":copy"
+
+
+The i;ascii-numeric Comparator
+
+RFC 2244 describes this comparator and specifies that non-numeric strings
+are considered equal with an ordinal value higher than any numeric string.
+Although not stated explicitly, this includes the empty string.  A range
+of at least 2^31 is required.  This implementation does not limit the
+range, because it does not convert numbers to binary representation
+before comparing them.
+
+
+The vacation extension
+
+The extension "vacation" is specified using the following grammar
+extension.
+
+  vacation-command =  "vacation" { vacation-options } <reason: string>
+  vacation-options =  [":days" number]
+                      [":subject" string]
+                      [":from" string]
+                      [":addresses" string-list]
+                      [":mime"]
+                      [":handle" string]
+  command          =/ vacation-command
+
+
+Semantics Of ":mime"
+
+The draft does not specify how strings using MIME entities are used
+to compose messages.  As a result, different implementations generate
+different mails.  The Exim Sieve implementation splits the reason into
+header and body.  It adds the header to the mail header and uses the body
+as mail body.  Be aware, that other imlementations compose a multipart
+structure with the reason as only part.  Both conform to the specification
+(or lack thereof).
+
+
+Semantics Of Not Using ":mime"
+
+Sieve scripts are written in UTF-8, so is the reason string in this
+case.  This implementation adds MIME headers to indicate that.  This
+is not required by the vacation draft, which does not specify how
+the UTF-8 reason is processed to compose the resulting message.
+
+
+Default Subject
+
+RFC 5230 specifies that the default message subject is "Auto: " plus
+the old subject.  Using this subject is dangerous, because many mailing
+lists verify addresses by sending a secret key in the subject of a
+message, asking to reply to the message for confirmation.  Using the
+default vacation subject confirms any subscription request of this kind,
+allowing to subscribe a third party to any mailing list, either to annoy
+the user or to declare spam as legitimate mail by proving to use opt-in.
+
+
+Rate Limiting Responses
+
+In absence of a handle, this implementation hashes the reason,
+":subject" option, ":mime" option and ":from" option and uses the hex
+string representation as filename within the "sieve_vacation_directory"
+to store the recipient addresses for this vacation parameter set.
+
+The draft specifies that sites may define a minimum ":days" value than 1.
+This implementation uses 1.  The maximum value MUST greater than 7,
+and SHOULD be greater than 30.  This implementation uses a maximum of 31.
+
+Vacation recipient address databases older than 31 days are automatically
+removed.  Users do not have to remove them manually when modifying their
+scripts.  Don't put anything but vacation databases in that directory
+or you risk that it will be removed, too!
+
+
+Global Reply Address Blacklist
+
+The draft requires that each implementation offers a global black list
+of addresses that will never be replied to.  Exim offers this as option
+"never_mail" in the autoreply transport.
+
+
+The enotify extension
+
+The extension "enotify" is specified using the following grammar
+extension.
+
+  notify-command =  "notify" { notify-options } <method: string>
+  notify-options =  [":from" string]
+                    [":importance" <"1" / "2" / "3">]
+                    [":options" 1*(string-list / number)]
+                    [":message" string]
+
+  command          =/ notify-command
+
+  valid_notify_method = "valid_notify_method"
+                        <notification-uris: string-list>
+
+  test             =/ valid_notify_method
+
+Only the mailto URI scheme is implemented.
diff --git a/doc/dbm.discuss.txt b/doc/dbm.discuss.txt
new file mode 100644 (file)
index 0000000..e82987b
--- /dev/null
@@ -0,0 +1,320 @@
+DBM Libraries for use with Exim
+-------------------------------
+
+Background
+----------
+
+Exim uses direct-access (so-called "dbm") files for a number of different
+purposes. These are files arranged so that the data they contain is indexed and
+can quickly be looked up by quoting an appropriate key. They are used as
+follows:
+
+1. Exim keeps its "hints" databases in dbm files.
+
+2. The configuration can specify that certain things (e.g. aliases) be looked
+   up in dbm files.
+
+3. The configuration can contain expansion strings that involve lookups in dbm
+   files.
+
+4. The filter commands "mail" and "vacation" have a facility for replying only
+   once to each incoming address. The record of which addresses have already
+   received replies may be kept in a dbm file, depending on the configuration
+   option once_file_size.
+
+The runtime configuration can be set up without specifying 2 or 3, but Exim
+always requires the availability of a dbm library, for 1 (and 4 if configured
+that way).
+
+
+DBM Libraries
+-------------
+
+The original library that provided the dbm facility in Unix was called "dbm".
+This seems to have been superseded quite some time ago by a new version called
+"ndbm" which permits several dbm files to be open at once. Several operating
+systems, including those from Sun, contain ndbm as standard.
+
+A number of alternative libraries also exist, the most common of which seems to
+be Berkeley DB (just called DB hereinafter). Release 1.85 was around for
+some time, and various releases 2.x began to appear towards the end of 1997. In
+November 1999, version 3.0 was released, and the ending of support for 2.7.7,
+the last 2.x release, was announced for November 2000. (Support for 1.85 has
+already ceased.) There were further 3.x releases, but by the end of 2001, the
+current release was 4.0.14.
+
+There are major differences in implementation and interface between the DB 1.x
+and 2.x/3.x/4.x releases, and they are best considered as two independent dbm
+libraries. Changes to the API were made for 3.0 and again for 3.1.
+
+Another DBM library is the GNU library, gdbm, though this does not seem to be
+very widespread.
+
+Yet another dbm library is tdb (Trivial Data Base) which has come out of the
+Samba project. The first releases seem to have been in mid-2000.
+
+Some older Linux releases contain gdbm as standard, while others contain no dbm
+library. More recent releases contain DB 1.85 or 2.x or later, and presumably
+will track the development of the DB library. Some BSD versions of Unix include
+DB 1.85 or later. All of the non-ndbm libraries except tdb contain
+compatibility interfaces so that programs written to call the ndbm functions
+should, in theory, work with them, but there are some potential pitfalls which
+have caught out Exim users in the past.
+
+Exim has been tested with ndbm, gdbm, DB 1.85, DB 2.x, DB 3.1, DB 4.0.14, and
+tdb 1.0.2, in various different modes in some cases, and is believed to work
+with all of them if it and they are properly configured.
+
+I have considered the possibility of calling different dbm libraries for
+different functions from a single Exim binary. However, because all bar one of
+the libraries provide ndbm compatibility interfaces (and therefore the same
+function names) it would require a lot of complicated, error-prone trickery to
+achieve this. Exim therefore uses a single library for all its dbm activities.
+
+However, Exim does also support cdb (Constant Data Base), an efficient file
+arrangement for indexed data that does not change incrementally (for example,
+alias files). This is independent of any dbm library and can be used alongside
+any of them.
+
+
+Locking
+-------
+
+The configuration option EXIMDB_LOCK_TIMEOUT controls how long Exim waits to
+get a lock on a hints database. From version 1.80 onwards, Exim does not
+attempt to take out a lock on an actual database file (this caused problems in
+the past). Instead, it takes out an fcntl() lock on a separate file whose name
+ends in ".lockfile". This ensures that Exim has exclusive access to the
+database before even attempting to open it. Exim creates the lock file the
+first time it needs it. It should never be removed.
+
+
+Main Pitfall
+------------
+
+The OS-specific configuration files that are used to build Exim specify the use
+of Berkeley DB on those systems where it is known to be standard. In the
+absence of any special configuration options, Exim uses the ndbm set of
+functions to control its dbm databases. This should work with any of the dbm
+libraries because those that are not ndbm have compatibility interfaces.
+However, there is one awful pitfall:
+
+Exim #includes a header file called ndbm.h which defines the functions and the
+interface data block; gdbm and DB 1.x provide their own versions of this header
+file, later DB versions do not. If it should happen that the wrong version of
+nbdm.h is seen by Exim, it may compile without error, but fail to operate
+correctly at runtime.
+
+This situation can easily arise when more than one dbm library is installed on
+a single host. For example, if you decide to use DB 1.x on a system where gdbm
+is the standard library, unless you are careful in setting up the include
+directories for Exim, it may see gdbm's ndbm.h file instead of DB's. The
+situation is even worse with later versions of DB, which do not provide an
+ndbm.h file at all.
+
+One way out of this for gdbm and any of the versions of DB is to configure Exim
+to call the DBM library in its native mode instead of via the ndbm
+compatibility interface, thus avoiding the use of ndbm.h. This is done by
+setting the USE_DB configuration option if you are using Berkeley DB, or
+USE_GDBM if you are using gdbm. This is the recommended approach.
+
+
+NDBM
+----
+
+The ndbm library holds its data in two files, with extensions .dir and .pag.
+This makes atomic updating of, for example, alias files, difficult, because
+simple renaming cannot be used without some risk. However, if your system has
+ndbm installed, Exim should compile and run without any problems.
+
+
+GDBM
+----
+
+The gdbm library, when called via the ndbm compatibility interface, makes two
+hard links to a single file, with extensions .dir and .pag. As mentioned above,
+gdbm provides its own version of the ndbm.h header, and you must ensure that
+this is seen by Exim rather than any other version. This is not likely to be a
+problem if gdbm is the only dbm library on your system.
+
+If gdbm is called via the native interface (by setting USE_GDBM in your
+Local/Makefile), it uses a single file, with no extension on the name, and the
+ndbm.h header is not required.
+
+The gdbm library does its own locking of the single file that it uses. From
+version 1.80 onwards, Exim locks on an entirely separate file before accessing
+a hints database, so gdbm's locking should always succeed.
+
+
+Berkeley DB 1.8x
+----------------
+
+1.85 was the most widespread DB 1.x release; there is also a 1.86 bug-fix
+release, but the belief is that the bugs it fixes will not affect Exim.
+However, maintenance for 1.x releases has been phased out.
+
+This dbm library can be called by Exim in one of two ways: via the ndbm
+compatibility interface, or via its own native interface. There are two
+advantages to doing the latter: (1) you don't run the risk of Exim's seeing the
+"wrong" version of the ndbm.h header, as described above, and (2) the
+performace is better. It is therefore recommended that you set USE_DB=yes in an
+appropriate Local/Makefile-xxx file. (If you are compiling for just one OS, it
+can go in Local/Makefile itself.)
+
+When called via the compatibility interface, DB 1.x creates a single file with
+a .db extension. When called via its native interface, no extension is added to
+the file name handed to it.
+
+DB 1.x does not do any locking of its own.
+
+
+Berkeley DB 2.x
+---------------
+
+DB 2.x was released in 1997. It is a major re-implementation and its native
+interface is incompatible with DB 1.x, though a compatibility interface was
+introduced in DB 2.1.0, and there is also an ndbm.h compatibility interface.
+
+Like 1.x, it can be called from Exim via the ndbm compatibility interface or
+via its native interface, and once again setting USE_DB in order to get the
+native interface is recommended. If USE_DB is *not* set, then you will have to
+provide a suitable version of ndbm.h, because one does not come with the DB 2.x
+distribution. A suitable version is:
+
+  /*************************************************
+  *              ndbm.h header for DB 2.x          *
+  *************************************************/
+
+  /* This header should replace any other version of ndbm.h when Berkeley DB
+  version 2.x is in use via the ndbm compatibility interface. Otherwise, any
+  extant version of ndbm.h may cause programs to misbehave. There doesn't seem
+  to be a version of ndbm.h supplied with DB 2.x, so I made this for myself.
+
+  Philip Hazel 12/Jun/97
+  */
+
+  #define DB_DBM_HSEARCH
+  #include <db.h>
+
+  /* End */
+
+When called via the compatibility interface, DB 2.x creates a single file with
+a .db extension. When called via its native interface, no extension is added to
+the file name handed to it.
+
+DB 2.x does not do any automatic locking of its own; it does have a set of
+functions for various forms of locking, but Exim does not use them.
+
+
+Berkeley DB 3.x
+---------------
+
+DB 3.0 was released in November 1999 and 3.1 in June 2000. The 3.x series is a
+development of the 2.x series and the above comments apply. Exim can
+automatically distinguish between the different versions, so it copes with the
+changes to the API without needing any special configuration.
+
+When Exim creates a DBM file using DB 3.x (e.g. when creating one of its hints
+databases), it specified the "hash" format. However, when it opens a DB 3 file
+for reading only, it specifies "unknown". This means that it can read DB 3
+files in other formats that are created by other programs.
+
+
+Berkeley DB 4.x
+---------------
+
+The 4.x series is a developement of the 2.x and 3.x series, and the above
+comments apply.
+
+
+tdb
+---
+
+tdb 1.0.2 was released in September 2000. Its origin is the database functions
+that are used by the Samba project.
+
+
+
+Testing Exim's dbm handling
+---------------------------
+
+Because there have been problems with dbm file locking in the past, I built
+some testing code for Exim's dbm functions. This is very much a lash-up, but it
+is documented here so that anybody who wants to check that their configuration
+is locking properly can do so. Now that Exim does the locking on an entirely
+separate file, locking problems are much less likely, but this code still
+exists, just in case. Proceed as follows:
+
+. Build Exim in the normal way. This ensures that all the makesfiles etc. get
+  set up.
+
+. From within the build directory, obey "make test_dbfn". This makes a binary
+  file called test_dbfn. If you are experimenting with different configurations
+  you *must* do "make makefile" after changing anything, before obeying "make
+  test_dbfn" again, because the make target for test_dbfn isn't integrated
+  with the making of the makefile.
+
+. Identify a scratch directory where you have write access. Create a sub-
+  directory called "db" in the scratch directory.
+
+. Type the command "test_dbfn <scratch-directory>". This will output some
+  general information such as
+
+    Exim's db functions tester: interface type is db (v2)
+    DBM library: Berkeley DB: Sleepycat Software: DB 2.1.0: (6/13/97)
+    USE_DB is defined
+
+  It then says
+
+    Test the functions
+    >
+
+. At this point you can type commands to open a dbm file and read and write
+  data in it. First type the command "open <name>", e.g. "open junk". The
+  response should look like this
+
+    opened DB file <scratch-directory>/db/junk: flags=102
+    Locked
+    opened 0
+    >
+
+  The tester will have created a dbm file within the db directory of the
+  scratch directory. It will also have created a file with the extension
+  ".lockfile" in the same directory. Unlike Exim itself, it will not create
+  the db directory for itself if it does not exist.
+
+. To test the locking, don't type anything more for the moment. You now need to
+  set up another process running the same test_dbfn command, e.g. from a
+  different logon to the same host. This time, when you attempt to open the
+  file it should fail after a minute with a timeout error because it is
+  already in use.
+
+. If the second process doesn't produce any error message, but gets back to the
+  > prompt, then the locking is not working properly.
+
+. You can check that the second process gets the lock when the first process
+  releases it by exiting from the first process with ^D, q, or quit; or by
+  typing the command "close".
+
+. There are some other commands available that are not related to locking:
+
+     write <key> <data>
+     e.g.
+     write abcde the quick brown fox
+
+  writes a record to the database,
+
+     read <key>
+     delete <key>
+
+  read and delete a record, respectively, and
+
+     scan
+
+  scans the entire database. Note that the database is purely for testing the
+  dbm functions. It is *not* one of Exim's regular databases, and you should
+  not try running this testing program on any of Exim's real database
+  files.
+
+Philip Hazel
+Last update: June 2002
diff --git a/doc/exim.8 b/doc/exim.8
new file mode 100644 (file)
index 0000000..0fccbc7
--- /dev/null
@@ -0,0 +1,1642 @@
+.TH EXIM 8
+.SH NAME
+exim \- a Mail Transfer Agent
+.SH SYNOPSIS
+.nf
+.B exim [options] arguments ...
+.B mailq [options] arguments ...
+.B rsmtp [options] arguments ...
+.B rmail [options] arguments ...
+.B runq [options] arguments ...
+.B newaliases [options] arguments ...
+.fi
+.
+.SH DESCRIPTION
+.rs
+.sp
+Exim is a mail transfer agent (MTA) developed at the University of Cambridge.
+It is a large program with very many facilities. For a full specification, see
+the reference manual. This man page contains only a description of the command
+line options. It has been automatically generated from the reference manual
+source, hopefully without too much mangling.
+.P
+Like other MTAs, Exim replaces Sendmail, and is normally called by user agents
+(MUAs) using the path \fI/usr/sbin/sendmail\fP when they submit messages for
+delivery (some operating systems use \fI/usr/lib/sendmail\fP). This path is
+normally set up as a symbolic link to the Exim binary. It may also be used by
+boot scripts to start the Exim daemon. Many of Exim's command line options are
+compatible with Sendmail so that it can act as a drop-in replacement.
+.
+.SH "DEFAULT ACTION"
+.rs
+.sp
+If no options are present that require a specific action (such as starting the
+daemon or a queue runner, testing an address, receiving a message in a specific
+format, or listing the queue), and there are no arguments on the command line,
+Exim outputs a brief message about itself and exits.
+.sp
+However, if there is at least one command line argument, \fB-bm\fR (accept a
+local message on the standard input, with the arguments specifying the
+recipients) is assumed. Thus, for example, if Exim is installed in
+\fI/usr/sbin\fP, you can send a message from the command line like this:
+.sp
+  /usr/sbin/exim -i <recipient-address(es)>
+  <message content, including all the header lines>
+  CTRL-D
+.sp
+The \fB-i\fP option prevents a line containing just a dot from terminating
+the message. Only an end-of-file (generated by typing CTRL-D if the input is
+from a terminal) does so.
+.
+.SH "SETTING OPTIONS BY PROGRAM NAME"
+.rs
+.sp
+If an Exim binary is called using one of the names listed in this section
+(typically via a symbolic link), certain options are assumed.
+.TP
+\fBmailq\fR
+Behave as if the option \fB\-bp\fP were present before any other options.
+The \fB\-bp\fP option requests a listing of the contents of the mail queue
+on the standard output.
+.TP
+\fBrsmtp\fR
+Behaves as if the option \fB\-bS\fP were present before any other options,
+for compatibility with Smail. The \fB\-bS\fP option is used for reading in a
+number of messages in batched SMTP format.
+.TP
+\fBrmail\fR
+Behave as if the \fB\-i\fP and \fB\-oee\fP options were present before
+any other options, for compatibility with Smail. The name \fBrmail\fR is used
+as an interface by some UUCP systems. The \fB\-i\fP option specifies that a
+dot on a line by itself does not terminate a non\-SMTP message; \fB\-oee\fP
+requests that errors detected in non\-SMTP messages be reported by emailing
+the sender.
+.TP
+\fBrunq\fR
+Behave as if the option \fB\-q\fP were present before any other options, for
+compatibility with Smail. The \fB\-q\fP option causes a single queue runner
+process to be started. It processes the queue once, then exits.
+.TP
+\fBnewaliases\fR
+Behave as if the option \fB\-bi\fP were present before any other options,
+for compatibility with Sendmail. This option is used for rebuilding Sendmail's
+alias file. Exim does not have the concept of a single alias file, but can be
+configured to run a specified command if called with the \fB\-bi\fP option.
+.
+.SH "OPTIONS"
+.rs
+.TP 10
+\fB\-\-\fP
+This is a pseudo\-option whose only purpose is to terminate the options and
+therefore to cause subsequent command line items to be treated as arguments
+rather than options, even if they begin with hyphens.
+.TP 10
+\fB\-\-help\fP
+This option causes Exim to output a few sentences stating what it is.
+The same output is generated if the Exim binary is called with no options and
+no arguments.
+.TP 10
+\fB\-\-version\fP
+This option is an alias for \fB\-bV\fP and causes version information to be
+displayed.
+.TP 10
+\fB\-Ac\fP
+\fB\-Am\fP
+These options are used by Sendmail for selecting configuration files and are
+ignored by Exim.
+.TP 10
+\fB\-B\fP<\fItype\fP>
+This is a Sendmail option for selecting 7 or 8 bit processing. Exim is 8\-bit
+clean; it ignores this option.
+.TP 10
+\fB\-bd\fP
+This option runs Exim as a daemon, awaiting incoming SMTP connections. Usually
+the \fB\-bd\fP option is combined with the \fB\-q\fP<\fItime\fP> option, to specify
+that the daemon should also initiate periodic queue runs.
+.sp
+The \fB\-bd\fP option can be used only by an admin user. If either of the \fB\-d\fP
+(debugging) or \fB\-v\fP (verifying) options are set, the daemon does not
+disconnect from the controlling terminal. When running this way, it can be
+stopped by pressing ctrl\-C.
+.sp
+By default, Exim listens for incoming connections to the standard SMTP port on
+all the host's running interfaces. However, it is possible to listen on other
+ports, on multiple ports, and only on specific interfaces.
+.sp
+When a listening daemon
+is started without the use of \fB\-oX\fP (that is, without overriding the normal
+configuration), it writes its process id to a file called exim\-daemon.pid
+in Exim's spool directory. This location can be overridden by setting
+PID_FILE_PATH in Local/Makefile. The file is written while Exim is still
+running as root.
+.sp
+When \fB\-oX\fP is used on the command line to start a listening daemon, the
+process id is not written to the normal pid file path. However, \fB\-oP\fP can be
+used to specify a path on the command line if a pid file is required.
+.sp
+The SIGHUP signal
+can be used to cause the daemon to re\-execute itself. This should be done
+whenever Exim's configuration file, or any file that is incorporated into it by
+means of the \fB.include\fP facility, is changed, and also whenever a new version
+of Exim is installed. It is not necessary to do this when other files that are
+referenced from the configuration (for example, alias files) are changed,
+because these are reread each time they are used.
+.TP 10
+\fB\-bdf\fP
+This option has the same effect as \fB\-bd\fP except that it never disconnects
+from the controlling terminal, even when no debugging is specified.
+.TP 10
+\fB\-be\fP
+Run Exim in expansion testing mode. Exim discards its root privilege, to
+prevent ordinary users from using this mode to read otherwise inaccessible
+files. If no arguments are given, Exim runs interactively, prompting for lines
+of data. Otherwise, it processes each argument in turn.
+.sp
+If Exim was built with USE_READLINE=yes in Local/Makefile, it tries
+to load the \fBlibreadline\fP library dynamically whenever the \fB\-be\fP option is
+used without command line arguments. If successful, it uses the readline()
+function, which provides extensive line\-editing facilities, for reading the
+test data. A line history is supported.
+.sp
+Long expansion expressions can be split over several lines by using backslash
+continuations. As in Exim's run time configuration, white space at the start of
+continuation lines is ignored. Each argument or data line is passed through the
+string expansion mechanism, and the result is output. Variable values from the
+configuration file (for example, \fI$qualify_domain\fP) are available, but no
+message\-specific values (such as \fI$sender_domain\fP) are set, because no message
+is being processed (but see \fB\-bem\fP and \fB\-Mset\fP).
+.sp
+\fBNote\fP: If you use this mechanism to test lookups, and you change the data
+files or databases you are using, you must exit and restart Exim before trying
+the same lookup again. Otherwise, because each Exim process caches the results
+of lookups, you will just get the same result as before.
+.TP 10
+\fB\-bem\fP <\fIfilename\fP>
+This option operates like \fB\-be\fP except that it must be followed by the name
+of a file. For example:
+.sp
+  exim \-bem /tmp/testmessage
+.sp
+The file is read as a message (as if receiving a locally\-submitted non\-SMTP
+message) before any of the test expansions are done. Thus, message\-specific
+variables such as \fI$message_size\fP and \fI$header_from:\fP are available. However,
+no \fIReceived:\fP header is added to the message. If the \fB\-t\fP option is set,
+recipients are read from the headers in the normal way, and are shown in the
+\fI$recipients\fP variable. Note that recipients cannot be given on the command
+line, because further arguments are taken as strings to expand (just like
+\fB\-be\fP).
+.TP 10
+\fB\-bF\fP <\fIfilename\fP>
+This option is the same as \fB\-bf\fP except that it assumes that the filter being
+tested is a system filter. The additional commands that are available only in
+system filters are recognized.
+.TP 10
+\fB\-bf\fP <\fIfilename\fP>
+This option runs Exim in user filter testing mode; the file is the filter file
+to be tested, and a test message must be supplied on the standard input. If
+there are no message\-dependent tests in the filter, an empty file can be
+supplied.
+.sp
+If you want to test a system filter file, use \fB\-bF\fP instead of \fB\-bf\fP. You
+can use both \fB\-bF\fP and \fB\-bf\fP on the same command, in order to test a system
+filter and a user filter in the same run. For example:
+.sp
+  exim \-bF /system/filter \-bf /user/filter </test/message
+.sp
+This is helpful when the system filter adds header lines or sets filter
+variables that are used by the user filter.
+.sp
+If the test filter file does not begin with one of the special lines
+.sp
+  # Exim filter
+  # Sieve filter
+.sp
+it is taken to be a normal .forward file, and is tested for validity under
+that interpretation.
+.sp
+The result of an Exim command that uses \fB\-bf\fP, provided no errors are
+detected, is a list of the actions that Exim would try to take if presented
+with the message for real. More details of filter testing are given in the
+separate document entitled \fIExim's interfaces to mail filtering\fP.
+.sp
+When testing a filter file,
+the envelope sender can be set by the \fB\-f\fP option,
+or by a "From " line at the start of the test message. Various parameters
+that would normally be taken from the envelope recipient address of the message
+can be set by means of additional command line options (see the next four
+options).
+.TP 10
+\fB\-bfd\fP <\fIdomain\fP>
+This sets the domain of the recipient address when a filter file is being
+tested by means of the \fB\-bf\fP option. The default is the value of
+\fI$qualify_domain\fP.
+.TP 10
+\fB\-bfl\fP <\fIlocal part\fP>
+This sets the local part of the recipient address when a filter file is being
+tested by means of the \fB\-bf\fP option. The default is the username of the
+process that calls Exim. A local part should be specified with any prefix or
+suffix stripped, because that is how it appears to the filter when a message is
+actually being delivered.
+.TP 10
+\fB\-bfp\fP <\fIprefix\fP>
+This sets the prefix of the local part of the recipient address when a filter
+file is being tested by means of the \fB\-bf\fP option. The default is an empty
+prefix.
+.TP 10
+\fB\-bfs\fP <\fIsuffix\fP>
+This sets the suffix of the local part of the recipient address when a filter
+file is being tested by means of the \fB\-bf\fP option. The default is an empty
+suffix.
+.TP 10
+\fB\-bh\fP <\fIIP address\fP>
+This option runs a fake SMTP session as if from the given IP address, using the
+standard input and output. The IP address may include a port number at the end,
+after a full stop. For example:
+.sp
+  exim \-bh 10.9.8.7.1234
+  exim \-bh fe80::a00:20ff:fe86:a061.5678
+.sp
+When an IPv6 address is given, it is converted into canonical form. In the case
+of the second example above, the value of \fI$sender_host_address\fP after
+conversion to the canonical form is
+fe80:0000:0000:0a00:20ff:fe86:a061.5678.
+.sp
+Comments as to what is going on are written to the standard error file. These
+include lines beginning with "LOG" for anything that would have been logged.
+This facility is provided for testing configuration options for incoming
+messages, to make sure they implement the required policy. For example, you can
+test your relay controls using \fB\-bh\fP.
+.sp
+\fBWarning 1\fP:
+You can test features of the configuration that rely on ident (RFC 1413)
+information by using the \fB\-oMt\fP option. However, Exim cannot actually perform
+an ident callout when testing using \fB\-bh\fP because there is no incoming SMTP
+connection.
+.sp
+\fBWarning 2\fP: Address verification callouts
+are also skipped when testing using \fB\-bh\fP. If you want these callouts to
+occur, use \fB\-bhc\fP instead.
+.sp
+Messages supplied during the testing session are discarded, and nothing is
+written to any of the real log files. There may be pauses when DNS (and other)
+lookups are taking place, and of course these may time out. The \fB\-oMi\fP option
+can be used to specify a specific IP interface and port if this is important,
+and \fB\-oMaa\fP and \fB\-oMai\fP can be used to set parameters as if the SMTP
+session were authenticated.
+.sp
+The \fIexim_checkaccess\fP utility is a "packaged" version of \fB\-bh\fP whose
+output just states whether a given recipient address from a given host is
+acceptable or not.
+.sp
+Features such as authentication and encryption, where the client input is not
+plain text, cannot easily be tested with \fB\-bh\fP. Instead, you should use a
+specialized SMTP test program such as
+\fBswaks\fP.
+.TP 10
+\fB\-bhc\fP <\fIIP address\fP>
+This option operates in the same way as \fB\-bh\fP, except that address
+verification callouts are performed if required. This includes consulting and
+updating the callout cache database.
+.TP 10
+\fB\-bi\fP
+Sendmail interprets the \fB\-bi\fP option as a request to rebuild its alias file.
+Exim does not have the concept of a single alias file, and so it cannot mimic
+this behaviour. However, calls to /usr/lib/sendmail with the \fB\-bi\fP option
+tend to appear in various scripts such as NIS make files, so the option must be
+recognized.
+.sp
+If \fB\-bi\fP is encountered, the command specified by the \fBbi_command\fP
+configuration option is run, under the uid and gid of the caller of Exim. If
+the \fB\-oA\fP option is used, its value is passed to the command as an argument.
+The command set by \fBbi_command\fP may not contain arguments. The command can
+use the \fIexim_dbmbuild\fP utility, or some other means, to rebuild alias files
+if this is required. If the \fBbi_command\fP option is not set, calling Exim with
+\fB\-bi\fP is a no\-op.
+.TP 10
+\fB\-bI:help\fP
+We shall provide various options starting \-bI: for querying Exim for
+information.  The output of many of these will be intended for machine
+consumption.  This one is not.  The \fB\-bI:help\fP option asks Exim for a
+synopsis of supported options beginning \-bI:.  Use of any of these
+options shall cause Exim to exit after producing the requested output.
+.TP 10
+\fB\-bI:dscp\fP
+This option causes Exim to emit an alphabetically sorted list of all
+recognised DSCP names.
+.TP 10
+\fB\-bI:sieve\fP
+This option causes Exim to emit an alphabetically sorted list of all supported
+Sieve protocol extensions on stdout, one per line.  This is anticipated to be
+useful for ManageSieve (RFC 5804) implementations, in providing that protocol's
+SIEVE capability response line.  As the precise list may depend upon
+compile\-time build options, which this option will adapt to, this is the only
+way to guarantee a correct response.
+.TP 10
+\fB\-bm\fP
+This option runs an Exim receiving process that accepts an incoming,
+locally\-generated message on the standard input. The recipients are given as the
+command arguments (except when \fB\-t\fP is also present \- see below). Each
+argument can be a comma\-separated list of RFC 2822 addresses. This is the
+default option for selecting the overall action of an Exim call; it is assumed
+if no other conflicting option is present.
+.sp
+If any addresses in the message are unqualified (have no domain), they are
+qualified by the values of the \fBqualify_domain\fP or \fBqualify_recipient\fP
+options, as appropriate. The \fB\-bnq\fP option (see below) provides a way of
+suppressing this for special cases.
+.sp
+Policy checks on the contents of local messages can be enforced by means of
+the non\-SMTP ACL.
+.sp
+The return code is zero if the message is successfully accepted. Otherwise, the
+action is controlled by the \fB\-oe\fP\fIx\fP option setting \- see below.
+.sp
+The format
+of the message must be as defined in RFC 2822, except that, for
+compatibility with Sendmail and Smail, a line in one of the forms
+.sp
+  From sender Fri Jan  5 12:55 GMT 1997
+  From sender Fri, 5 Jan 97 12:55:01
+.sp
+(with the weekday optional, and possibly with additional text after the date)
+is permitted to appear at the start of the message. There appears to be no
+authoritative specification of the format of this line. Exim recognizes it by
+matching against the regular expression defined by the \fBuucp_from_pattern\fP
+option, which can be changed if necessary.
+.sp
+The specified sender is treated as if it were given as the argument to the
+\fB\-f\fP option, but if a \fB\-f\fP option is also present, its argument is used in
+preference to the address taken from the message. The caller of Exim must be a
+trusted user for the sender of a message to be set in this way.
+.TP 10
+\fB\-bmalware\fP <\fIfilename\fP>
+This debugging option causes Exim to scan the given file,
+using the malware scanning framework.  The option of \fBav_scanner\fP influences
+this option, so if \fBav_scanner\fP's value is dependent upon an expansion then
+the expansion should have defaults which apply to this invocation.  ACLs are
+not invoked, so if \fBav_scanner\fP references an ACL variable then that variable
+will never be populated and \fB\-bmalware\fP will fail.
+.sp
+Exim will have changed working directory before resolving the filename, so
+using fully qualified pathnames is advisable.  Exim will be running as the Exim
+user when it tries to open the file, rather than as the invoking user.
+This option requires admin privileges.
+.sp
+The \fB\-bmalware\fP option will not be extended to be more generally useful,
+there are better tools for file\-scanning.  This option exists to help
+administrators verify their Exim and AV scanner configuration.
+.TP 10
+\fB\-bnq\fP
+By default, Exim automatically qualifies unqualified addresses (those
+without domains) that appear in messages that are submitted locally (that
+is, not over TCP/IP). This qualification applies both to addresses in
+envelopes, and addresses in header lines. Sender addresses are qualified using
+\fBqualify_domain\fP, and recipient addresses using \fBqualify_recipient\fP (which
+defaults to the value of \fBqualify_domain\fP).
+.sp
+Sometimes, qualification is not wanted. For example, if \fB\-bS\fP (batch SMTP) is
+being used to re\-submit messages that originally came from remote hosts after
+content scanning, you probably do not want to qualify unqualified addresses in
+header lines. (Such lines will be present only if you have not enabled a header
+syntax check in the appropriate ACL.)
+.sp
+The \fB\-bnq\fP option suppresses all qualification of unqualified addresses in
+messages that originate on the local host. When this is used, unqualified
+addresses in the envelope provoke errors (causing message rejection) and
+unqualified addresses in header lines are left alone.
+.TP 10
+\fB\-bP\fP
+If this option is given with no arguments, it causes the values of all Exim's
+main configuration options to be written to the standard output. The values
+of one or more specific options can be requested by giving their names as
+arguments, for example:
+.sp
+  exim \-bP qualify_domain hold_domains
+.sp
+However, any option setting that is preceded by the word "hide" in the
+configuration file is not shown in full, except to an admin user. For other
+users, the output is as in this example:
+.sp
+  mysql_servers = <value not displayable>
+.sp
+If \fBconfigure_file\fP is given as an argument, the name of the run time
+configuration file is output.
+If a list of configuration files was supplied, the value that is output here
+is the name of the file that was actually used.
+.sp
+If the \fB\-n\fP flag is given, then for most modes of \fB\-bP\fP operation the
+name will not be output.
+.sp
+If \fBlog_file_path\fP or \fBpid_file_path\fP are given, the names of the
+directories where log files and daemon pid files are written are output,
+respectively. If these values are unset, log files are written in a
+sub\-directory of the spool directory called \fBlog\fP, and the pid file is
+written directly into the spool directory.
+.sp
+If \fB\-bP\fP is followed by a name preceded by +, for example,
+.sp
+  exim \-bP +local_domains
+.sp
+it searches for a matching named list of any type (domain, host, address, or
+local part) and outputs what it finds.
+.sp
+If one of the words \fBrouter\fP, \fBtransport\fP, or \fBauthenticator\fP is given,
+followed by the name of an appropriate driver instance, the option settings for
+that driver are output. For example:
+.sp
+  exim \-bP transport local_delivery
+.sp
+The generic driver options are output first, followed by the driver's private
+options. A list of the names of drivers of a particular type can be obtained by
+using one of the words \fBrouter_list\fP, \fBtransport_list\fP, or
+\fBauthenticator_list\fP, and a complete list of all drivers with their option
+settings can be obtained by using \fBrouters\fP, \fBtransports\fP, or
+\fBauthenticators\fP.
+.sp
+If invoked by an admin user, then \fBmacro\fP, \fBmacro_list\fP and \fBmacros\fP
+are available, similarly to the drivers.  Because macros are sometimes used
+for storing passwords, this option is restricted.
+The output format is one item per line.
+.TP 10
+\fB\-bp\fP
+This option requests a listing of the contents of the mail queue on the
+standard output. If the \fB\-bp\fP option is followed by a list of message ids,
+just those messages are listed. By default, this option can be used only by an
+admin user. However, the \fBqueue_list_requires_admin\fP option can be set false
+to allow any user to see the queue.
+.sp
+Each message on the queue is displayed as in the following example:
+.sp
+  25m  2.9K 0t5C6f\-0000c8\-00 <alice@wonderland.fict.example>
+            red.king@looking\-glass.fict.example
+            <other addresses>
+.sp
+The first line contains the length of time the message has been on the queue
+(in this case 25 minutes), the size of the message (2.9K), the unique local
+identifier for the message, and the message sender, as contained in the
+envelope. For bounce messages, the sender address is empty, and appears as
+"<>". If the message was submitted locally by an untrusted user who overrode
+the default sender address, the user's login name is shown in parentheses
+before the sender address.
+.sp
+If the message is frozen (attempts to deliver it are suspended) then the text
+"*** frozen ***" is displayed at the end of this line.
+.sp
+The recipients of the message (taken from the envelope, not the headers) are
+displayed on subsequent lines. Those addresses to which the message has already
+been delivered are marked with the letter D. If an original address gets
+expanded into several addresses via an alias or forward file, the original is
+displayed with a D only when deliveries for all of its child addresses are
+complete.
+.TP 10
+\fB\-bpa\fP
+This option operates like \fB\-bp\fP, but in addition it shows delivered addresses
+that were generated from the original top level address(es) in each message by
+alias or forwarding operations. These addresses are flagged with "+D" instead
+of just "D".
+.TP 10
+\fB\-bpc\fP
+This option counts the number of messages on the queue, and writes the total
+to the standard output. It is restricted to admin users, unless
+\fBqueue_list_requires_admin\fP is set false.
+.TP 10
+\fB\-bpr\fP
+This option operates like \fB\-bp\fP, but the output is not sorted into
+chronological order of message arrival. This can speed it up when there are
+lots of messages on the queue, and is particularly useful if the output is
+going to be post\-processed in a way that doesn't need the sorting.
+.TP 10
+\fB\-bpra\fP
+This option is a combination of \fB\-bpr\fP and \fB\-bpa\fP.
+.TP 10
+\fB\-bpru\fP
+This option is a combination of \fB\-bpr\fP and \fB\-bpu\fP.
+.TP 10
+\fB\-bpu\fP
+This option operates like \fB\-bp\fP but shows only undelivered top\-level
+addresses for each message displayed. Addresses generated by aliasing or
+forwarding are not shown, unless the message was deferred after processing by a
+router with the \fBone_time\fP option set.
+.TP 10
+\fB\-brt\fP
+This option is for testing retry rules, and it must be followed by up to three
+arguments. It causes Exim to look for a retry rule that matches the values
+and to write it to the standard output. For example:
+.sp
+  exim \-brt bach.comp.mus.example
+  Retry rule: *.comp.mus.example  F,2h,15m; F,4d,30m;
+.sp
+ The first
+argument, which is required, can be a complete address in the form
+\fIlocal_part@domain\fP, or it can be just a domain name. If the second argument
+contains a dot, it is interpreted as an optional second domain name; if no
+retry rule is found for the first argument, the second is tried. This ties in
+with Exim's behaviour when looking for retry rules for remote hosts \- if no
+rule is found that matches the host, one that matches the mail domain is
+sought. Finally, an argument that is the name of a specific delivery error, as
+used in setting up retry rules, can be given. For example:
+.sp
+  exim \-brt haydn.comp.mus.example quota_3d
+  Retry rule: *@haydn.comp.mus.example quota_3d  F,1h,15m
+.TP 10
+\fB\-brw\fP
+This option is for testing address rewriting rules, and it must be followed by
+a single argument, consisting of either a local part without a domain, or a
+complete address with a fully qualified domain. Exim outputs how this address
+would be rewritten for each possible place it might appear.
+.TP 10
+\fB\-bS\fP
+This option is used for batched SMTP input, which is an alternative interface
+for non\-interactive local message submission. A number of messages can be
+submitted in a single run. However, despite its name, this is not really SMTP
+input. Exim reads each message's envelope from SMTP commands on the standard
+input, but generates no responses. If the caller is trusted, or
+\fBuntrusted_set_sender\fP is set, the senders in the SMTP MAIL commands are
+believed; otherwise the sender is always the caller of Exim.
+.sp
+The message itself is read from the standard input, in SMTP format (leading
+dots doubled), terminated by a line containing just a single dot. An error is
+provoked if the terminating dot is missing. A further message may then follow.
+.sp
+As for other local message submissions, the contents of incoming batch SMTP
+messages can be checked using the non\-SMTP ACL.
+Unqualified addresses are automatically qualified using \fBqualify_domain\fP and
+\fBqualify_recipient\fP, as appropriate, unless the \fB\-bnq\fP option is used.
+.sp
+Some other SMTP commands are recognized in the input. HELO and EHLO act
+as RSET; VRFY, EXPN, ETRN, and HELP act as NOOP;
+QUIT quits, ignoring the rest of the standard input.
+.sp
+If any error is encountered, reports are written to the standard output and
+error streams, and Exim gives up immediately. The return code is 0 if no error
+was detected; it is 1 if one or more messages were accepted before the error
+was detected; otherwise it is 2.
+.sp
+.TP 10
+\fB\-bs\fP
+This option causes Exim to accept one or more messages by reading SMTP commands
+on the standard input, and producing SMTP replies on the standard output. SMTP
+policy controls, as defined in ACLs are applied.
+Some user agents use this interface as a way of passing locally\-generated
+messages to the MTA.
+.sp
+In
+this usage, if the caller of Exim is trusted, or \fBuntrusted_set_sender\fP is
+set, the senders of messages are taken from the SMTP MAIL commands.
+Otherwise the content of these commands is ignored and the sender is set up as
+the calling user. Unqualified addresses are automatically qualified using
+\fBqualify_domain\fP and \fBqualify_recipient\fP, as appropriate, unless the
+\fB\-bnq\fP option is used.
+.sp
+The
+\fB\-bs\fP option is also used to run Exim from \fIinetd\fP, as an alternative to
+using a listening daemon. Exim can distinguish the two cases by checking
+whether the standard input is a TCP/IP socket. When Exim is called from
+\fIinetd\fP, the source of the mail is assumed to be remote, and the comments
+above concerning senders and qualification do not apply. In this situation,
+Exim behaves in exactly the same way as it does when receiving a message via
+the listening daemon.
+.TP 10
+\fB\-bt\fP
+This option runs Exim in address testing mode, in which each argument is taken
+as a recipient address to be tested for deliverability. The results are
+written to the standard output. If a test fails, and the caller is not an admin
+user, no details of the failure are output, because these might contain
+sensitive information such as usernames and passwords for database lookups.
+.sp
+If no arguments are given, Exim runs in an interactive manner, prompting with a
+right angle bracket for addresses to be tested.
+.sp
+Unlike the \fB\-be\fP test option, you cannot arrange for Exim to use the
+readline() function, because it is running as \fIroot\fP and there are
+security issues.
+.sp
+Each address is handled as if it were the recipient address of a message
+(compare the \fB\-bv\fP option). It is passed to the routers and the result is
+written to the standard output. However, any router that has
+\fBno_address_test\fP set is bypassed. This can make \fB\-bt\fP easier to use for
+genuine routing tests if your first router passes everything to a scanner
+program.
+.sp
+The return code is 2 if any address failed outright; it is 1 if no address
+failed outright but at least one could not be resolved for some reason. Return
+code 0 is given only when all addresses succeed.
+.sp
+\fBNote\fP: When actually delivering a message, Exim removes duplicate recipient
+addresses after routing is complete, so that only one delivery takes place.
+This does not happen when testing with \fB\-bt\fP; the full results of routing are
+always shown.
+.sp
+\fBWarning\fP: \fB\-bt\fP can only do relatively simple testing. If any of the
+routers in the configuration makes any tests on the sender address of a
+message,
+you can use the \fB\-f\fP option to set an appropriate sender when running
+\fB\-bt\fP tests. Without it, the sender is assumed to be the calling user at the
+default qualifying domain. However, if you have set up (for example) routers
+whose behaviour depends on the contents of an incoming message, you cannot test
+those conditions using \fB\-bt\fP. The \fB\-N\fP option provides a possible way of
+doing such tests.
+.TP 10
+\fB\-bV\fP
+This option causes Exim to write the current version number, compilation
+number, and compilation date of the \fIexim\fP binary to the standard output.
+It also lists the DBM library that is being used, the optional modules (such as
+specific lookup types), the drivers that are included in the binary, and the
+name of the run time configuration file that is in use.
+.sp
+As part of its operation, \fB\-bV\fP causes Exim to read and syntax check its
+configuration file. However, this is a static check only. It cannot check
+values that are to be expanded. For example, although a misspelt ACL verb is
+detected, an error in the verb's arguments is not. You cannot rely on \fB\-bV\fP
+alone to discover (for example) all the typos in the configuration; some
+realistic testing is needed. The \fB\-bh\fP and \fB\-N\fP options provide more
+dynamic testing facilities.
+.TP 10
+\fB\-bv\fP
+This option runs Exim in address verification mode, in which each argument is
+taken as a recipient address to be verified by the routers. (This does
+not involve any verification callouts). During normal operation, verification
+happens mostly as a consequence processing a \fBverify\fP condition in an ACL. If you want to test an entire ACL, possibly
+including callouts, see the \fB\-bh\fP and \fB\-bhc\fP options.
+.sp
+If verification fails, and the caller is not an admin user, no details of the
+failure are output, because these might contain sensitive information such as
+usernames and passwords for database lookups.
+.sp
+If no arguments are given, Exim runs in an interactive manner, prompting with a
+right angle bracket for addresses to be verified.
+.sp
+Unlike the \fB\-be\fP test option, you cannot arrange for Exim to use the
+readline() function, because it is running as \fIexim\fP and there are
+security issues.
+.sp
+Verification differs from address testing (the \fB\-bt\fP option) in that routers
+that have \fBno_verify\fP set are skipped, and if the address is accepted by a
+router that has \fBfail_verify\fP set, verification fails. The address is
+verified as a recipient if \fB\-bv\fP is used; to test verification for a sender
+address, \fB\-bvs\fP should be used.
+.sp
+If the \fB\-v\fP option is not set, the output consists of a single line for each
+address, stating whether it was verified or not, and giving a reason in the
+latter case. Without \fB\-v\fP, generating more than one address by redirection
+causes verification to end successfully, without considering the generated
+addresses. However, if just one address is generated, processing continues,
+and the generated address must verify successfully for the overall verification
+to succeed.
+.sp
+When \fB\-v\fP is set, more details are given of how the address has been handled,
+and in the case of address redirection, all the generated addresses are also
+considered. Verification may succeed for some and fail for others.
+.sp
+The
+return code is 2 if any address failed outright; it is 1 if no address
+failed outright but at least one could not be resolved for some reason. Return
+code 0 is given only when all addresses succeed.
+.sp
+If any of the routers in the configuration makes any tests on the sender
+address of a message, you should use the \fB\-f\fP option to set an appropriate
+sender when running \fB\-bv\fP tests. Without it, the sender is assumed to be the
+calling user at the default qualifying domain.
+.TP 10
+\fB\-bvs\fP
+This option acts like \fB\-bv\fP, but verifies the address as a sender rather
+than a recipient address. This affects any rewriting and qualification that
+might happen.
+.TP 10
+\fB\-bw\fP
+This option runs Exim as a daemon, awaiting incoming SMTP connections,
+similarly to the \fB\-bd\fP option.  All port specifications on the command\-line
+and in the configuration file are ignored.  Queue\-running may not be specified.
+.sp
+In this mode, Exim expects to be passed a socket as fd 0 (stdin) which is
+listening for connections.  This permits the system to start up and have
+inetd (or equivalent) listen on the SMTP ports, starting an Exim daemon for
+each port only when the first connection is received.
+.sp
+If the option is given as \fB\-bw\fP<\fItime\fP> then the time is a timeout, after
+which the daemon will exit, which should cause inetd to listen once more.
+.TP 10
+\fB\-C\fP <\fIfilelist\fP>
+This option causes Exim to find the run time configuration file from the given
+list instead of from the list specified by the CONFIGURE_FILE
+compile\-time setting. Usually, the list will consist of just a single file
+name, but it can be a colon\-separated list of names. In this case, the first
+file that exists is used. Failure to open an existing file stops Exim from
+proceeding any further along the list, and an error is generated.
+.sp
+When this option is used by a caller other than root, and the list is different
+from the compiled\-in list, Exim gives up its root privilege immediately, and
+runs with the real and effective uid and gid set to those of the caller.
+However, if a TRUSTED_CONFIG_LIST file is defined in Local/Makefile, that
+file contains a list of full pathnames, one per line, for configuration files
+which are trusted. Root privilege is retained for any configuration file so
+listed, as long as the caller is the Exim user (or the user specified in the
+CONFIGURE_OWNER option, if any), and as long as the configuration file is
+not writeable by inappropriate users or groups.
+.sp
+Leaving TRUSTED_CONFIG_LIST unset precludes the possibility of testing a
+configuration using \fB\-C\fP right through message reception and delivery,
+even if the caller is root. The reception works, but by that time, Exim is
+running as the Exim user, so when it re\-executes to regain privilege for the
+delivery, the use of \fB\-C\fP causes privilege to be lost. However, root can
+test reception and delivery using two separate commands (one to put a message
+on the queue, using \fB\-odq\fP, and another to do the delivery, using \fB\-M\fP).
+.sp
+If ALT_CONFIG_PREFIX is defined in Local/Makefile, it specifies a
+prefix string with which any file named in a \fB\-C\fP command line option
+must start. In addition, the file name must not contain the sequence /../.
+However, if the value of the \fB\-C\fP option is identical to the value of
+CONFIGURE_FILE in Local/Makefile, Exim ignores \fB\-C\fP and proceeds as
+usual. There is no default setting for ALT_CONFIG_PREFIX; when it is
+unset, any file name can be used with \fB\-C\fP.
+.sp
+ALT_CONFIG_PREFIX can be used to confine alternative configuration files
+to a directory to which only root has access. This prevents someone who has
+broken into the Exim account from running a privileged Exim with an arbitrary
+configuration file.
+.sp
+The \fB\-C\fP facility is useful for ensuring that configuration files are
+syntactically correct, but cannot be used for test deliveries, unless the
+caller is privileged, or unless it is an exotic configuration that does not
+require privilege. No check is made on the owner or group of the files
+specified by this option.
+.TP 10
+\fB\-D\fP<\fImacro\fP>=<\fIvalue\fP>
+This option can be used to override macro definitions in the configuration file. However, like \fB\-C\fP, if it is used by an
+unprivileged caller, it causes Exim to give up its root privilege.
+If DISABLE_D_OPTION is defined in Local/Makefile, the use of \fB\-D\fP is
+completely disabled, and its use causes an immediate error exit.
+.sp
+If WHITELIST_D_MACROS is defined in Local/Makefile then it should be a
+colon\-separated list of macros which are considered safe and, if \fB\-D\fP only
+supplies macros from this list, and the values are acceptable, then Exim will
+not give up root privilege if the caller is root, the Exim run\-time user, or
+the CONFIGURE_OWNER, if set.  This is a transition mechanism and is expected
+to be removed in the future.  Acceptable values for the macros satisfy the
+regexp: ^[A\-Za\-z0\-9_/.\-]*$
+.sp
+The entire option (including equals sign if present) must all be within one
+command line item. \fB\-D\fP can be used to set the value of a macro to the empty
+string, in which case the equals sign is optional. These two commands are
+synonymous:
+.sp
+  exim \-DABC  ...
+  exim \-DABC= ...
+.sp
+To include spaces in a macro definition item, quotes must be used. If you use
+quotes, spaces are permitted around the macro name and the equals sign. For
+example:
+.sp
+  exim '\-D ABC = something' ...
+.sp
+\fB\-D\fP may be repeated up to 10 times on a command line.
+.TP 10
+\fB\-d\fP<\fIdebug options\fP>
+This option causes debugging information to be written to the standard
+error stream. It is restricted to admin users because debugging output may show
+database queries that contain password information. Also, the details of users'
+filter files should be protected. If a non\-admin user uses \fB\-d\fP, Exim
+writes an error message to the standard error stream and exits with a non\-zero
+return code.
+.sp
+When \fB\-d\fP is used, \fB\-v\fP is assumed. If \fB\-d\fP is given on its own, a lot of
+standard debugging data is output. This can be reduced, or increased to include
+some more rarely needed information, by directly following \fB\-d\fP with a string
+made up of names preceded by plus or minus characters. These add or remove sets
+of debugging data, respectively. For example, \fB\-d+filter\fP adds filter
+debugging, whereas \fB\-d\-all+filter\fP selects only filter debugging. Note that
+no spaces are allowed in the debug setting. The available debugging categories
+are:
+.sp
+  acl             ACL interpretation
+  auth            authenticators
+  deliver         general delivery logic
+  dns             DNS lookups (see also resolver)
+  dnsbl           DNS black list (aka RBL) code
+  exec            arguments for execv() calls
+  expand          detailed debugging for string expansions
+  filter          filter handling
+  hints_lookup    hints data lookups
+  host_lookup     all types of name\-to\-IP address handling
+  ident           ident lookup
+  interface       lists of local interfaces
+  lists           matching things in lists
+  load            system load checks
+  local_scan      can be used by local_scan()
+  lookup          general lookup code and all lookups
+  memory          memory handling
+  pid             add pid to debug output lines
+  process_info    setting info for the process log
+  queue_run       queue runs
+  receive         general message reception logic
+  resolver        turn on the DNS resolver's debugging output
+  retry           retry handling
+  rewrite         address rewriting
+  route           address routing
+  timestamp       add timestamp to debug output lines
+  tls             TLS logic
+  transport       transports
+  uid             changes of uid/gid and looking up uid/gid
+  verify          address verification logic
+  all             almost all of the above (see below), and also \fB\-v\fP
+.sp
+The all option excludes memory when used as +all, but includes it
+for \-all. The reason for this is that +all is something that people
+tend to use when generating debug output for Exim maintainers. If +memory
+is included, an awful lot of output that is very rarely of interest is
+generated, so it now has to be explicitly requested. However, \-all does
+turn everything off.
+.sp
+The resolver option produces output only if the DNS resolver was compiled
+with DEBUG enabled. This is not the case in some operating systems. Also,
+unfortunately, debugging output from the DNS resolver is written to stdout
+rather than stderr.
+.sp
+The default (\fB\-d\fP with no argument) omits expand, filter,
+interface, load, memory, pid, resolver, and timestamp.
+However, the pid selector is forced when debugging is turned on for a
+daemon, which then passes it on to any re\-executed Exims. Exim also
+automatically adds the pid to debug lines when several remote deliveries are
+run in parallel.
+.sp
+The timestamp selector causes the current time to be inserted at the start
+of all debug output lines. This can be useful when trying to track down delays
+in processing.
+.sp
+If the \fBdebug_print\fP option is set in any driver, it produces output whenever
+any debugging is selected, or if \fB\-v\fP is used.
+.TP 10
+\fB\-dd\fP<\fIdebug options\fP>
+This option behaves exactly like \fB\-d\fP except when used on a command that
+starts a daemon process. In that case, debugging is turned off for the
+subprocesses that the daemon creates. Thus, it is useful for monitoring the
+behaviour of the daemon without creating as much output as full debugging does.
+.TP 10
+\fB\-dropcr\fP
+This is an obsolete option that is now a no\-op. It used to affect the way Exim
+handled CR and LF characters in incoming messages.
+.TP 10
+\fB\-E\fP
+This option specifies that an incoming message is a locally\-generated delivery
+failure report. It is used internally by Exim when handling delivery failures
+and is not intended for external use. Its only effect is to stop Exim
+generating certain messages to the postmaster, as otherwise message cascades
+could occur in some situations. As part of the same option, a message id may
+follow the characters \fB\-E\fP. If it does, the log entry for the receipt of the
+new message contains the id, following "R=", as a cross\-reference.
+.TP 10
+\fB\-e\fP\fIx\fP
+There are a number of Sendmail options starting with \fB\-oe\fP which seem to be
+called by various programs without the leading \fBo\fP in the option. For
+example, the \fBvacation\fP program uses \fB\-eq\fP. Exim treats all options of the
+form \fB\-e\fP\fIx\fP as synonymous with the corresponding \fB\-oe\fP\fIx\fP options.
+.TP 10
+\fB\-F\fP <\fIstring\fP>
+This option sets the sender's full name for use when a locally\-generated
+message is being accepted. In the absence of this option, the user's \fIgecos\fP
+entry from the password data is used. As users are generally permitted to alter
+their \fIgecos\fP entries, no security considerations are involved. White space
+between \fB\-F\fP and the <\fIstring\fP> is optional.
+.TP 10
+\fB\-f\fP <\fIaddress\fP>
+This option sets the address of the envelope sender of a locally\-generated
+message (also known as the return path). The option can normally be used only
+by a trusted user, but \fBuntrusted_set_sender\fP can be set to allow untrusted
+users to use it.
+.sp
+Processes running as root or the Exim user are always trusted. Other
+trusted users are defined by the \fBtrusted_users\fP or \fBtrusted_groups\fP
+options. In the absence of \fB\-f\fP, or if the caller is not trusted, the sender
+of a local message is set to the caller's login name at the default qualify
+domain.
+.sp
+There is one exception to the restriction on the use of \fB\-f\fP: an empty sender
+can be specified by any user, trusted or not, to create a message that can
+never provoke a bounce. An empty sender can be specified either as an empty
+string, or as a pair of angle brackets with nothing between them, as in these
+examples of shell commands:
+.sp
+  exim \-f '<>' user@domain
+  exim \-f "" user@domain
+.sp
+In addition, the use of \fB\-f\fP is not restricted when testing a filter file
+with \fB\-bf\fP or when testing or verifying addresses using the \fB\-bt\fP or
+\fB\-bv\fP options.
+.sp
+Allowing untrusted users to change the sender address does not of itself make
+it possible to send anonymous mail. Exim still checks that the \fIFrom:\fP header
+refers to the local user, and if it does not, it adds a \fISender:\fP header,
+though this can be overridden by setting \fBno_local_from_check\fP.
+.sp
+White
+space between \fB\-f\fP and the <\fIaddress\fP> is optional (that is, they can be
+given as two arguments or one combined argument). The sender of a
+locally\-generated message can also be set (when permitted) by an initial
+"From " line in the message \- see the description of \fB\-bm\fP above \- but
+if \fB\-f\fP is also present, it overrides "From ".
+.TP 10
+\fB\-G\fP
+This option is equivalent to an ACL applying:
+.sp
+  control = suppress_local_fixups
+.sp
+for every message received.  Note that Sendmail will complain about such
+bad formatting, where Exim silently just does not fix it up.  This may change
+in future.
+.sp
+As this affects audit information, the caller must be a trusted user to use
+this option.
+.TP 10
+\fB\-h\fP <\fInumber\fP>
+This option is accepted for compatibility with Sendmail, but has no effect. (In
+Sendmail it overrides the "hop count" obtained by counting \fIReceived:\fP
+headers.)
+.TP 10
+\fB\-i\fP
+This option, which has the same effect as \fB\-oi\fP, specifies that a dot on a
+line by itself should not terminate an incoming, non\-SMTP message. I can find
+no documentation for this option in Solaris 2.4 Sendmail, but the \fImailx\fP
+command in Solaris 2.4 uses it. See also \fB\-ti\fP.
+.TP 10
+\fB\-L\fP <\fItag\fP>
+This option is equivalent to setting \fBsyslog_processname\fP in the config
+file and setting \fBlog_file_path\fP to syslog.
+Its use is restricted to administrators.  The configuration file has to be
+read and parsed, to determine access rights, before this is set and takes
+effect, so early configuration file errors will not honour this flag.
+.sp
+The tag should not be longer than 32 characters.
+.TP 10
+\fB\-M\fP <\fImessage id\fP> <\fImessage id\fP> ...
+This option requests Exim to run a delivery attempt on each message in turn. If
+any of the messages are frozen, they are automatically thawed before the
+delivery attempt. The settings of \fBqueue_domains\fP, \fBqueue_smtp_domains\fP,
+and \fBhold_domains\fP are ignored.
+.sp
+Retry
+hints for any of the addresses are overridden \- Exim tries to deliver even if
+the normal retry time has not yet been reached. This option requires the caller
+to be an admin user. However, there is an option called \fBprod_requires_admin\fP
+which can be set false to relax this restriction (and also the same requirement
+for the \fB\-q\fP, \fB\-R\fP, and \fB\-S\fP options).
+.sp
+The deliveries happen synchronously, that is, the original Exim process does
+not terminate until all the delivery attempts have finished. No output is
+produced unless there is a serious error. If you want to see what is happening,
+use the \fB\-v\fP option as well, or inspect Exim's main log.
+.TP 10
+\fB\-Mar\fP <\fImessage id\fP> <\fIaddress\fP> <\fIaddress\fP> ...
+This option requests Exim to add the addresses to the list of recipients of the
+message ("ar" for "add recipients"). The first argument must be a message
+id, and the remaining ones must be email addresses. However, if the message is
+active (in the middle of a delivery attempt), it is not altered. This option
+can be used only by an admin user.
+.TP 10
+\fB\-MC\fP <\fItransport\fP> <\fIhostname\fP> <\fIsequence number\fP> <\fImessage id\fP>
+This option is not intended for use by external callers. It is used internally
+by Exim to invoke another instance of itself to deliver a waiting message using
+an existing SMTP connection, which is passed as the standard input. This must be the final option, and the caller
+must be root or the Exim user in order to use it.
+.TP 10
+\fB\-MCA\fP
+This option is not intended for use by external callers. It is used internally
+by Exim in conjunction with the \fB\-MC\fP option. It signifies that the
+connection to the remote host has been authenticated.
+.TP 10
+\fB\-MCP\fP
+This option is not intended for use by external callers. It is used internally
+by Exim in conjunction with the \fB\-MC\fP option. It signifies that the server to
+which Exim is connected supports pipelining.
+.TP 10
+\fB\-MCQ\fP <\fIprocess id\fP> <\fIpipe fd\fP>
+This option is not intended for use by external callers. It is used internally
+by Exim in conjunction with the \fB\-MC\fP option when the original delivery was
+started by a queue runner. It passes on the process id of the queue runner,
+together with the file descriptor number of an open pipe. Closure of the pipe
+signals the final completion of the sequence of processes that are passing
+messages through the same SMTP connection.
+.TP 10
+\fB\-MCS\fP
+This option is not intended for use by external callers. It is used internally
+by Exim in conjunction with the \fB\-MC\fP option, and passes on the fact that the
+SMTP SIZE option should be used on messages delivered down the existing
+connection.
+.TP 10
+\fB\-MCT\fP
+This option is not intended for use by external callers. It is used internally
+by Exim in conjunction with the \fB\-MC\fP option, and passes on the fact that the
+host to which Exim is connected supports TLS encryption.
+.TP 10
+\fB\-Mc\fP <\fImessage id\fP> <\fImessage id\fP> ...
+This option requests Exim to run a delivery attempt on each message in turn,
+but unlike the \fB\-M\fP option, it does check for retry hints, and respects any
+that are found. This option is not very useful to external callers. It is
+provided mainly for internal use by Exim when it needs to re\-invoke itself in
+order to regain root privilege for a delivery.
+However, \fB\-Mc\fP can be useful when testing, in order to run a delivery that
+respects retry times and other options such as \fBhold_domains\fP that are
+overridden when \fB\-M\fP is used. Such a delivery does not count as a queue run.
+If you want to run a specific delivery as if in a queue run, you should use
+\fB\-q\fP with a message id argument. A distinction between queue run deliveries
+and other deliveries is made in one or two places.
+.TP 10
+\fB\-Mes\fP <\fImessage id\fP> <\fIaddress\fP>
+This option requests Exim to change the sender address in the message to the
+given address, which must be a fully qualified address or "<>" ("es" for
+"edit sender"). There must be exactly two arguments. The first argument must
+be a message id, and the second one an email address. However, if the message
+is active (in the middle of a delivery attempt), its status is not altered.
+This option can be used only by an admin user.
+.TP 10
+\fB\-Mf\fP <\fImessage id\fP> <\fImessage id\fP> ...
+This option requests Exim to mark each listed message as "frozen". This
+prevents any delivery attempts taking place until the message is "thawed",
+either manually or as a result of the \fBauto_thaw\fP configuration option.
+However, if any of the messages are active (in the middle of a delivery
+attempt), their status is not altered. This option can be used only by an admin
+user.
+.TP 10
+\fB\-Mg\fP <\fImessage id\fP> <\fImessage id\fP> ...
+This option requests Exim to give up trying to deliver the listed messages,
+including any that are frozen. However, if any of the messages are active,
+their status is not altered. For non\-bounce messages, a delivery error message
+is sent to the sender, containing the text "cancelled by administrator".
+Bounce messages are just discarded. This option can be used only by an admin
+user.
+.TP 10
+\fB\-Mmad\fP <\fImessage id\fP> <\fImessage id\fP> ...
+This option requests Exim to mark all the recipient addresses in the messages
+as already delivered ("mad" for "mark all delivered"). However, if any
+message is active (in the middle of a delivery attempt), its status is not
+altered. This option can be used only by an admin user.
+.TP 10
+\fB\-Mmd\fP <\fImessage id\fP> <\fIaddress\fP> <\fIaddress\fP> ...
+This option requests Exim to mark the given addresses as already delivered
+("md" for "mark delivered"). The first argument must be a message id, and
+the remaining ones must be email addresses. These are matched to recipient
+addresses in the message in a case\-sensitive manner. If the message is active
+(in the middle of a delivery attempt), its status is not altered. This option
+can be used only by an admin user.
+.TP 10
+\fB\-Mrm\fP <\fImessage id\fP> <\fImessage id\fP> ...
+This option requests Exim to remove the given messages from the queue. No
+bounce messages are sent; each message is simply forgotten. However, if any of
+the messages are active, their status is not altered. This option can be used
+only by an admin user or by the user who originally caused the message to be
+placed on the queue.
+.TP 10
+\fB\-Mset\fP <\fImessage id\fP>
+This option is useful only in conjunction with \fB\-be\fP (that is, when testing
+string expansions). Exim loads the given message from its spool before doing
+the test expansions, thus setting message\-specific variables such as
+\fI$message_size\fP and the header variables. The \fI$recipients\fP variable is made
+available. This feature is provided to make it easier to test expansions that
+make use of these variables. However, this option can be used only by an admin
+user. See also \fB\-bem\fP.
+.TP 10
+\fB\-Mt\fP <\fImessage id\fP> <\fImessage id\fP> ...
+This option requests Exim to "thaw" any of the listed messages that are
+"frozen", so that delivery attempts can resume. However, if any of the
+messages are active, their status is not altered. This option can be used only
+by an admin user.
+.TP 10
+\fB\-Mvb\fP <\fImessage id\fP>
+This option causes the contents of the message body (\-D) spool file to be
+written to the standard output. This option can be used only by an admin user.
+.TP 10
+\fB\-Mvc\fP <\fImessage id\fP>
+This option causes a copy of the complete message (header lines plus body) to
+be written to the standard output in RFC 2822 format. This option can be used
+only by an admin user.
+.TP 10
+\fB\-Mvh\fP <\fImessage id\fP>
+This option causes the contents of the message headers (\-H) spool file to be
+written to the standard output. This option can be used only by an admin user.
+.TP 10
+\fB\-Mvl\fP <\fImessage id\fP>
+This option causes the contents of the message log spool file to be written to
+the standard output. This option can be used only by an admin user.
+.TP 10
+\fB\-m\fP
+This is apparently a synonym for \fB\-om\fP that is accepted by Sendmail, so Exim
+treats it that way too.
+.TP 10
+\fB\-N\fP
+This is a debugging option that inhibits delivery of a message at the transport
+level. It implies \fB\-v\fP. Exim goes through many of the motions of delivery \-
+it just doesn't actually transport the message, but instead behaves as if it
+had successfully done so. However, it does not make any updates to the retry
+database, and the log entries for deliveries are flagged with "*>" rather
+than "=>".
+.sp
+Because \fB\-N\fP discards any message to which it applies, only root or the Exim
+user are allowed to use it with \fB\-bd\fP, \fB\-q\fP, \fB\-R\fP or \fB\-M\fP. In other
+words, an ordinary user can use it only when supplying an incoming message to
+which it will apply. Although transportation never fails when \fB\-N\fP is set, an
+address may be deferred because of a configuration problem on a transport, or a
+routing problem. Once \fB\-N\fP has been used for a delivery attempt, it sticks to
+the message, and applies to any subsequent delivery attempts that may happen
+for that message.
+.TP 10
+\fB\-n\fP
+This option is interpreted by Sendmail to mean "no aliasing".
+For normal modes of operation, it is ignored by Exim.
+When combined with \fB\-bP\fP it suppresses the name of an option from being output.
+.TP 10
+\fB\-O\fP <\fIdata\fP>
+This option is interpreted by Sendmail to mean set option. It is ignored by
+Exim.
+.TP 10
+\fB\-oA\fP <\fIfile name\fP>
+This option is used by Sendmail in conjunction with \fB\-bi\fP to specify an
+alternative alias file name. Exim handles \fB\-bi\fP differently; see the
+description above.
+.TP 10
+\fB\-oB\fP <\fIn\fP>
+This is a debugging option which limits the maximum number of messages that can
+be delivered down one SMTP connection, overriding the value set in any smtp
+transport. If <\fIn\fP> is omitted, the limit is set to 1.
+.TP 10
+\fB\-odb\fP
+This option applies to all modes in which Exim accepts incoming messages,
+including the listening daemon. It requests "background" delivery of such
+messages, which means that the accepting process automatically starts a
+delivery process for each message received, but does not wait for the delivery
+processes to finish.
+.sp
+When all the messages have been received, the reception process exits,
+leaving the delivery processes to finish in their own time. The standard output
+and error streams are closed at the start of each delivery process.
+This is the default action if none of the \fB\-od\fP options are present.
+.sp
+If one of the queueing options in the configuration file
+(\fBqueue_only\fP or \fBqueue_only_file\fP, for example) is in effect, \fB\-odb\fP
+overrides it if \fBqueue_only_override\fP is set true, which is the default
+setting. If \fBqueue_only_override\fP is set false, \fB\-odb\fP has no effect.
+.TP 10
+\fB\-odf\fP
+This option requests "foreground" (synchronous) delivery when Exim has
+accepted a locally\-generated message. (For the daemon it is exactly the same as
+\fB\-odb\fP.) A delivery process is automatically started to deliver the message,
+and Exim waits for it to complete before proceeding.
+.sp
+The original Exim reception process does not finish until the delivery
+process for the final message has ended. The standard error stream is left open
+during deliveries.
+.sp
+However, like \fB\-odb\fP, this option has no effect if \fBqueue_only_override\fP is
+false and one of the queueing options in the configuration file is in effect.
+.sp
+If there is a temporary delivery error during foreground delivery, the
+message is left on the queue for later delivery, and the original reception
+process exits.
+.TP 10
+\fB\-odi\fP
+This option is synonymous with \fB\-odf\fP. It is provided for compatibility with
+Sendmail.
+.TP 10
+\fB\-odq\fP
+This option applies to all modes in which Exim accepts incoming messages,
+including the listening daemon. It specifies that the accepting process should
+not automatically start a delivery process for each message received. Messages
+are placed on the queue, and remain there until a subsequent queue runner
+process encounters them. There are several configuration options (such as
+\fBqueue_only\fP) that can be used to queue incoming messages under certain
+conditions. This option overrides all of them and also \fB\-odqs\fP. It always
+forces queueing.
+.TP 10
+\fB\-odqs\fP
+This option is a hybrid between \fB\-odb\fP/\fB\-odi\fP and \fB\-odq\fP.
+However, like \fB\-odb\fP and \fB\-odi\fP, this option has no effect if
+\fBqueue_only_override\fP is false and one of the queueing options in the
+configuration file is in effect.
+.sp
+When \fB\-odqs\fP does operate, a delivery process is started for each incoming
+message, in the background by default, but in the foreground if \fB\-odi\fP is
+also present. The recipient addresses are routed, and local deliveries are done
+in the normal way. However, if any SMTP deliveries are required, they are not
+done at this time, so the message remains on the queue until a subsequent queue
+runner process encounters it. Because routing was done, Exim knows which
+messages are waiting for which hosts, and so a number of messages for the same
+host can be sent in a single SMTP connection. The \fBqueue_smtp_domains\fP
+configuration option has the same effect for specific domains. See also the
+\fB\-qq\fP option.
+.TP 10
+\fB\-oee\fP
+If an error is detected while a non\-SMTP message is being received (for
+example, a malformed address), the error is reported to the sender in a mail
+message.
+.sp
+Provided
+this error message is successfully sent, the Exim receiving process
+exits with a return code of zero. If not, the return code is 2 if the problem
+is that the original message has no recipients, or 1 for any other error.
+This is the default \fB\-oe\fP\fIx\fP option if Exim is called as \fIrmail\fP.
+.TP 10
+\fB\-oem\fP
+This is the same as \fB\-oee\fP, except that Exim always exits with a non\-zero
+return code, whether or not the error message was successfully sent.
+This is the default \fB\-oe\fP\fIx\fP option, unless Exim is called as \fIrmail\fP.
+.TP 10
+\fB\-oep\fP
+If an error is detected while a non\-SMTP message is being received, the
+error is reported by writing a message to the standard error file (stderr).
+The return code is 1 for all errors.
+.TP 10
+\fB\-oeq\fP
+This option is supported for compatibility with Sendmail, but has the same
+effect as \fB\-oep\fP.
+.TP 10
+\fB\-oew\fP
+This option is supported for compatibility with Sendmail, but has the same
+effect as \fB\-oem\fP.
+.TP 10
+\fB\-oi\fP
+This option, which has the same effect as \fB\-i\fP, specifies that a dot on a
+line by itself should not terminate an incoming, non\-SMTP message. Otherwise, a
+single dot does terminate, though Exim does no special processing for other
+lines that start with a dot. This option is set by default if Exim is called as
+\fIrmail\fP. See also \fB\-ti\fP.
+.TP 10
+\fB\-oitrue\fP
+This option is treated as synonymous with \fB\-oi\fP.
+.TP 10
+\fB\-oMa\fP <\fIhost address\fP>
+A number of options starting with \fB\-oM\fP can be used to set values associated
+with remote hosts on locally\-submitted messages (that is, messages not received
+over TCP/IP). These options can be used by any caller in conjunction with the
+\fB\-bh\fP, \fB\-be\fP, \fB\-bf\fP, \fB\-bF\fP, \fB\-bt\fP, or \fB\-bv\fP testing options. In
+other circumstances, they are ignored unless the caller is trusted.
+.sp
+The \fB\-oMa\fP option sets the sender host address. This may include a port
+number at the end, after a full stop (period). For example:
+.sp
+  exim \-bs \-oMa 10.9.8.7.1234
+.sp
+An alternative syntax is to enclose the IP address in square brackets,
+followed by a colon and the port number:
+.sp
+  exim \-bs \-oMa [10.9.8.7]:1234
+.sp
+The IP address is placed in the \fI$sender_host_address\fP variable, and the
+port, if present, in \fI$sender_host_port\fP. If both \fB\-oMa\fP and \fB\-bh\fP
+are present on the command line, the sender host IP address is taken from
+whichever one is last.
+.TP 10
+\fB\-oMaa\fP <\fIname\fP>
+See \fB\-oMa\fP above for general remarks about the \fB\-oM\fP options. The \fB\-oMaa\fP
+option sets the value of \fI$sender_host_authenticated\fP (the authenticator
+name).
+This option can be used with \fB\-bh\fP and \fB\-bs\fP to set up an
+authenticated SMTP session without actually using the SMTP AUTH command.
+.TP 10
+\fB\-oMai\fP <\fIstring\fP>
+See \fB\-oMa\fP above for general remarks about the \fB\-oM\fP options. The \fB\-oMai\fP
+option sets the value of \fI$authenticated_id\fP (the id that was authenticated).
+This overrides the default value (the caller's login id, except with \fB\-bh\fP,
+where there is no default) for messages from local sources.
+.TP 10
+\fB\-oMas\fP <\fIaddress\fP>
+See \fB\-oMa\fP above for general remarks about the \fB\-oM\fP options. The \fB\-oMas\fP
+option sets the authenticated sender value in \fI$authenticated_sender\fP. It
+overrides the sender address that is created from the caller's login id for
+messages from local sources, except when \fB\-bh\fP is used, when there is no
+default. For both \fB\-bh\fP and \fB\-bs\fP, an authenticated sender that is
+specified on a MAIL command overrides this value.
+.TP 10
+\fB\-oMi\fP <\fIinterface address\fP>
+See \fB\-oMa\fP above for general remarks about the \fB\-oM\fP options. The \fB\-oMi\fP
+option sets the IP interface address value. A port number may be included,
+using the same syntax as for \fB\-oMa\fP. The interface address is placed in
+\fI$received_ip_address\fP and the port number, if present, in \fI$received_port\fP.
+.TP 10
+\fB\-oMm\fP <\fImessage reference\fP>
+See \fB\-oMa\fP above for general remarks about the \fB\-oM\fP options. The \fB\-oMm\fP
+option sets the message reference, e.g. message\-id, and is logged during
+delivery. This is useful when some kind of audit trail is required to tie
+messages together. The format of the message reference is checked and will
+abort if the format is invalid. The option will only be accepted if exim is
+running in trusted mode, not as any regular user.
+.sp
+The best example of a message reference is when Exim sends a bounce message.
+The message reference is the message\-id of the original message for which Exim
+is sending the bounce.
+.TP 10
+\fB\-oMr\fP <\fIprotocol name\fP>
+See \fB\-oMa\fP above for general remarks about the \fB\-oM\fP options. The \fB\-oMr\fP
+option sets the received protocol value that is stored in
+\fI$received_protocol\fP. However, it does not apply (and is ignored) when \fB\-bh\fP
+or \fB\-bs\fP is used. For \fB\-bh\fP, the protocol is forced to one of the standard
+SMTP protocol names. For \fB\-bs\fP, the protocol is always "local\-" followed by
+one of those same names. For \fB\-bS\fP (batched SMTP) however, the protocol can
+be set by \fB\-oMr\fP.
+.TP 10
+\fB\-oMs\fP <\fIhost name\fP>
+See \fB\-oMa\fP above for general remarks about the \fB\-oM\fP options. The \fB\-oMs\fP
+option sets the sender host name in \fI$sender_host_name\fP. When this option is
+present, Exim does not attempt to look up a host name from an IP address; it
+uses the name it is given.
+.TP 10
+\fB\-oMt\fP <\fIident string\fP>
+See \fB\-oMa\fP above for general remarks about the \fB\-oM\fP options. The \fB\-oMt\fP
+option sets the sender ident value in \fI$sender_ident\fP. The default setting for
+local callers is the login id of the calling process, except when \fB\-bh\fP is
+used, when there is no default.
+.TP 10
+\fB\-om\fP
+In Sendmail, this option means "me too", indicating that the sender of a
+message should receive a copy of the message if the sender appears in an alias
+expansion. Exim always does this, so the option does nothing.
+.TP 10
+\fB\-oo\fP
+This option is ignored. In Sendmail it specifies "old style headers",
+whatever that means.
+.TP 10
+\fB\-oP\fP <\fIpath\fP>
+This option is useful only in conjunction with \fB\-bd\fP or \fB\-q\fP with a time
+value. The option specifies the file to which the process id of the daemon is
+written. When \fB\-oX\fP is used with \fB\-bd\fP, or when \fB\-q\fP with a time is used
+without \fB\-bd\fP, this is the only way of causing Exim to write a pid file,
+because in those cases, the normal pid file is not used.
+.TP 10
+\fB\-or\fP <\fItime\fP>
+This option sets a timeout value for incoming non\-SMTP messages. If it is not
+set, Exim will wait forever for the standard input. The value can also be set
+by the \fBreceive_timeout\fP option.
+.TP 10
+\fB\-os\fP <\fItime\fP>
+This option sets a timeout value for incoming SMTP messages. The timeout
+applies to each SMTP command and block of data. The value can also be set by
+the \fBsmtp_receive_timeout\fP option; it defaults to 5 minutes.
+.TP 10
+\fB\-ov\fP
+This option has exactly the same effect as \fB\-v\fP.
+.TP 10
+\fB\-oX\fP <\fInumber or string\fP>
+This option is relevant only when the \fB\-bd\fP (start listening daemon) option
+is also given. It controls which ports and interfaces the daemon uses. When \fB\-oX\fP is used to start a daemon, no pid
+file is written unless \fB\-oP\fP is also present to specify a pid file name.
+.TP 10
+\fB\-pd\fP
+This option applies when an embedded Perl interpreter is linked with Exim. It overrides the setting of the \fBperl_at_start\fP
+option, forcing the starting of the interpreter to be delayed until it is
+needed.
+.TP 10
+\fB\-ps\fP
+This option applies when an embedded Perl interpreter is linked with Exim. It overrides the setting of the \fBperl_at_start\fP
+option, forcing the starting of the interpreter to occur as soon as Exim is
+started.
+.TP 10
+\fB\-p\fP<\fIrval\fP>:<\fIsval\fP>
+For compatibility with Sendmail, this option is equivalent to
+.sp
+  \-oMr <\fIrval\fP> \-oMs <\fIsval\fP>
+.sp
+It sets the incoming protocol and host name (for trusted callers). The
+host name and its colon can be omitted when only the protocol is to be set.
+Note the Exim already has two private options, \fB\-pd\fP and \fB\-ps\fP, that refer
+to embedded Perl. It is therefore impossible to set a protocol value of d
+or s using this option (but that does not seem a real limitation).
+.TP 10
+\fB\-q\fP
+This option is normally restricted to admin users. However, there is a
+configuration option called \fBprod_requires_admin\fP which can be set false to
+relax this restriction (and also the same requirement for the \fB\-M\fP, \fB\-R\fP,
+and \fB\-S\fP options).
+.sp
+The \fB\-q\fP option starts one queue runner process. This scans the queue of
+waiting messages, and runs a delivery process for each one in turn. It waits
+for each delivery process to finish before starting the next one. A delivery
+process may not actually do any deliveries if the retry times for the addresses
+have not been reached. Use \fB\-qf\fP (see below) if you want to override this.
+.sp
+If
+the delivery process spawns other processes to deliver other messages down
+passed SMTP connections, the queue runner waits for these to finish before
+proceeding.
+.sp
+When all the queued messages have been considered, the original queue runner
+process terminates. In other words, a single pass is made over the waiting
+mail, one message at a time. Use \fB\-q\fP with a time (see below) if you want
+this to be repeated periodically.
+.sp
+Exim processes the waiting messages in an unpredictable order. It isn't very
+random, but it is likely to be different each time, which is all that matters.
+If one particular message screws up a remote MTA, other messages to the same
+MTA have a chance of getting through if they get tried first.
+.sp
+It is possible to cause the messages to be processed in lexical message id
+order, which is essentially the order in which they arrived, by setting the
+\fBqueue_run_in_order\fP option, but this is not recommended for normal use.
+.TP 10
+\fB\-q\fP<\fIqflags\fP>
+The \fB\-q\fP option may be followed by one or more flag letters that change its
+behaviour. They are all optional, but if more than one is present, they must
+appear in the correct order. Each flag is described in a separate item below.
+.TP 10
+\fB\-qq...\fP
+An option starting with \fB\-qq\fP requests a two\-stage queue run. In the first
+stage, the queue is scanned as if the \fBqueue_smtp_domains\fP option matched
+every domain. Addresses are routed, local deliveries happen, but no remote
+transports are run.
+.sp
+The hints database that remembers which messages are waiting for specific hosts
+is updated, as if delivery to those hosts had been deferred. After this is
+complete, a second, normal queue scan happens, with routing and delivery taking
+place as normal. Messages that are routed to the same host should mostly be
+delivered down a single SMTP
+connection because of the hints that were set up during the first queue scan.
+This option may be useful for hosts that are connected to the Internet
+intermittently.
+.TP 10
+\fB\-q[q]i...\fP
+If the \fIi\fP flag is present, the queue runner runs delivery processes only for
+those messages that haven't previously been tried. (\fIi\fP stands for "initial
+delivery".) This can be helpful if you are putting messages on the queue using
+\fB\-odq\fP and want a queue runner just to process the new messages.
+.TP 10
+\fB\-q[q][i]f...\fP
+If one \fIf\fP flag is present, a delivery attempt is forced for each non\-frozen
+message, whereas without \fIf\fP only those non\-frozen addresses that have passed
+their retry times are tried.
+.TP 10
+\fB\-q[q][i]ff...\fP
+If \fIff\fP is present, a delivery attempt is forced for every message, whether
+frozen or not.
+.TP 10
+\fB\-q[q][i][f[f]]l\fP
+The \fIl\fP (the letter "ell") flag specifies that only local deliveries are to
+be done. If a message requires any remote deliveries, it remains on the queue
+for later delivery.
+.TP 10
+\fB\-q\fP<\fIqflags\fP> <\fIstart id\fP> <\fIend id\fP>
+When scanning the queue, Exim can be made to skip over messages whose ids are
+lexically less than a given value by following the \fB\-q\fP option with a
+starting message id. For example:
+.sp
+  exim \-q 0t5C6f\-0000c8\-00
+.sp
+Messages that arrived earlier than 0t5C6f\-0000c8\-00 are not inspected. If a
+second message id is given, messages whose ids are lexically greater than it
+are also skipped. If the same id is given twice, for example,
+.sp
+  exim \-q 0t5C6f\-0000c8\-00 0t5C6f\-0000c8\-00
+.sp
+just one delivery process is started, for that message. This differs from
+\fB\-M\fP in that retry data is respected, and it also differs from \fB\-Mc\fP in
+that it counts as a delivery from a queue run. Note that the selection
+mechanism does not affect the order in which the messages are scanned. There
+are also other ways of selecting specific sets of messages for delivery in a
+queue run \- see \fB\-R\fP and \fB\-S\fP.
+.TP 10
+\fB\-q\fP<\fIqflags\fP><\fItime\fP>
+When a time value is present, the \fB\-q\fP option causes Exim to run as a daemon,
+starting a queue runner process at intervals specified by the given time value. This form of the
+\fB\-q\fP option is commonly combined with the \fB\-bd\fP option, in which case a
+single daemon process handles both functions. A common way of starting up a
+combined daemon at system boot time is to use a command such as
+.sp
+  /usr/exim/bin/exim \-bd \-q30m
+.sp
+Such a daemon listens for incoming SMTP calls, and also starts a queue runner
+process every 30 minutes.
+.sp
+When a daemon is started by \fB\-q\fP with a time value, but without \fB\-bd\fP, no
+pid file is written unless one is explicitly requested by the \fB\-oP\fP option.
+.TP 10
+\fB\-qR\fP<\fIrsflags\fP> <\fIstring\fP>
+This option is synonymous with \fB\-R\fP. It is provided for Sendmail
+compatibility.
+.TP 10
+\fB\-qS\fP<\fIrsflags\fP> <\fIstring\fP>
+This option is synonymous with \fB\-S\fP.
+.TP 10
+\fB\-R\fP<\fIrsflags\fP> <\fIstring\fP>
+The <\fIrsflags\fP> may be empty, in which case the white space before the string
+is optional, unless the string is \fIf\fP, \fIff\fP, \fIr\fP, \fIrf\fP, or \fIrff\fP,
+which are the possible values for <\fIrsflags\fP>. White space is required if
+<\fIrsflags\fP> is not empty.
+.sp
+This option is similar to \fB\-q\fP with no time value, that is, it causes Exim to
+perform a single queue run, except that, when scanning the messages on the
+queue, Exim processes only those that have at least one undelivered recipient
+address containing the given string, which is checked in a case\-independent
+way. If the <\fIrsflags\fP> start with \fIr\fP, <\fIstring\fP> is interpreted as a
+regular expression; otherwise it is a literal string.
+.sp
+If you want to do periodic queue runs for messages with specific recipients,
+you can combine \fB\-R\fP with \fB\-q\fP and a time value. For example:
+.sp
+  exim \-q25m \-R @special.domain.example
+.sp
+This example does a queue run for messages with recipients in the given domain
+every 25 minutes. Any additional flags that are specified with \fB\-q\fP are
+applied to each queue run.
+.sp
+Once a message is selected for delivery by this mechanism, all its addresses
+are processed. For the first selected message, Exim overrides any retry
+information and forces a delivery attempt for each undelivered address. This
+means that if delivery of any address in the first message is successful, any
+existing retry information is deleted, and so delivery attempts for that
+address in subsequently selected messages (which are processed without forcing)
+will run. However, if delivery of any address does not succeed, the retry
+information is updated, and in subsequently selected messages, the failing
+address will be skipped.
+.sp
+If the <\fIrsflags\fP> contain \fIf\fP or \fIff\fP, the delivery forcing applies to
+all selected messages, not just the first; frozen messages are included when
+\fIff\fP is present.
+.sp
+The \fB\-R\fP option makes it straightforward to initiate delivery of all messages
+to a given domain after a host has been down for some time. When the SMTP
+command ETRN is accepted by its ACL, its default
+effect is to run Exim with the \fB\-R\fP option, but it can be configured to run
+an arbitrary command instead.
+.TP 10
+\fB\-r\fP
+This is a documented (for Sendmail) obsolete alternative name for \fB\-f\fP.
+.TP 10
+\fB\-S\fP<\fIrsflags\fP> <\fIstring\fP>
+This option acts like \fB\-R\fP except that it checks the string against each
+message's sender instead of against the recipients. If \fB\-R\fP is also set, both
+conditions must be met for a message to be selected. If either of the options
+has \fIf\fP or \fIff\fP in its flags, the associated action is taken.
+.TP 10
+\fB\-Tqt\fP <\fItimes\fP>
+This is an option that is exclusively for use by the Exim testing suite. It is not
+recognized when Exim is run normally. It allows for the setting up of explicit
+"queue times" so that various warning/retry features can be tested.
+.TP 10
+\fB\-t\fP
+When Exim is receiving a locally\-generated, non\-SMTP message on its standard
+input, the \fB\-t\fP option causes the recipients of the message to be obtained
+from the \fITo:\fP, \fICc:\fP, and \fIBcc:\fP header lines in the message instead of
+from the command arguments. The addresses are extracted before any rewriting
+takes place and the \fIBcc:\fP header line, if present, is then removed.
+.sp
+If the command has any arguments, they specify addresses to which the message
+is \fInot\fP to be delivered. That is, the argument addresses are removed from
+the recipients list obtained from the headers. This is compatible with Smail 3
+and in accordance with the documented behaviour of several versions of
+Sendmail, as described in man pages on a number of operating systems (e.g.
+Solaris 8, IRIX 6.5, HP\-UX 11). However, some versions of Sendmail \fIadd\fP
+argument addresses to those obtained from the headers, and the O'Reilly
+Sendmail book documents it that way. Exim can be made to add argument addresses
+instead of subtracting them by setting the option
+\fBextract_addresses_remove_arguments\fP false.
+.sp
+If there are any \fBResent\-\fP header lines in the message, Exim extracts
+recipients from all \fIResent\-To:\fP, \fIResent\-Cc:\fP, and \fIResent\-Bcc:\fP header
+lines instead of from \fITo:\fP, \fICc:\fP, and \fIBcc:\fP. This is for compatibility
+with Sendmail and other MTAs. (Prior to release 4.20, Exim gave an error if
+\fB\-t\fP was used in conjunction with \fBResent\-\fP header lines.)
+.sp
+RFC 2822 talks about different sets of \fBResent\-\fP header lines (for when a
+message is resent several times). The RFC also specifies that they should be
+added at the front of the message, and separated by \fIReceived:\fP lines. It is
+not at all clear how \fB\-t\fP should operate in the present of multiple sets,
+nor indeed exactly what constitutes a "set".
+In practice, it seems that MUAs do not follow the RFC. The \fBResent\-\fP lines
+are often added at the end of the header, and if a message is resent more than
+once, it is common for the original set of \fBResent\-\fP headers to be renamed as
+\fBX\-Resent\-\fP when a new set is added. This removes any possible ambiguity.
+.TP 10
+\fB\-ti\fP
+This option is exactly equivalent to \fB\-t\fP \fB\-i\fP. It is provided for
+compatibility with Sendmail.
+.TP 10
+\fB\-tls\-on\-connect\fP
+This option is available when Exim is compiled with TLS support. It forces all
+incoming SMTP connections to behave as if the incoming port is listed in the
+\fBtls_on_connect_ports\fP option.
+.TP 10
+\fB\-U\fP
+Sendmail uses this option for "initial message submission", and its
+documentation states that in future releases, it may complain about
+syntactically invalid messages rather than fixing them when this flag is not
+set. Exim ignores this option.
+.TP 10
+\fB\-v\fP
+This option causes Exim to write information to the standard error stream,
+describing what it is doing. In particular, it shows the log lines for
+receiving and delivering a message, and if an SMTP connection is made, the SMTP
+dialogue is shown. Some of the log lines shown may not actually be written to
+the log if the setting of \fBlog_selector\fP discards them. Any relevant
+selectors are shown with each log line. If none are shown, the logging is
+unconditional.
+.TP 10
+\fB\-x\fP
+AIX uses \fB\-x\fP for a private purpose ("mail from a local mail program has
+National Language Support extended characters in the body of the mail item").
+It sets \fB\-x\fP when calling the MTA from its \fBmail\fP command. Exim ignores
+this option.
+.TP 10
+\fB\-X\fP <\fIlogfile\fP>
+This option is interpreted by Sendmail to cause debug information to be sent
+to the named file.  It is ignored by Exim.
+.sp
+.
+.SH "SEE ALSO"
+.rs
+.sp
+The full Exim specification, the Exim book, and the Exim wiki.
diff --git a/doc/experimental-spec.txt b/doc/experimental-spec.txt
new file mode 100644 (file)
index 0000000..6657f63
--- /dev/null
@@ -0,0 +1,1154 @@
+From time to time, experimental features may be added to Exim.
+While a feature  is experimental, there  will be a  build-time
+option whose name starts  "EXPERIMENTAL_" that must be  set in
+order to include the  feature. This file contains  information
+about experimental  features, all  of which  are unstable and
+liable to incompatible change.
+
+
+Brightmail AntiSpam (BMI) suppport
+--------------------------------------------------------------
+
+Brightmail  AntiSpam  is  a  commercial  package.  Please  see
+http://www.brightmail.com    for    more    information     on
+the product. For  the sake of  clarity, we'll refer  to it  as
+"BMI" from now on.
+
+
+0) BMI concept and implementation overview
+
+In  contrast  to   how  spam-scanning  with   SpamAssassin  is
+implemented  in  exiscan-acl,  BMI  is  more  suited  for  per
+-recipient  scanning of  messages. However,  each messages  is
+scanned  only  once,  but  multiple  "verdicts"  for  multiple
+recipients can be  returned from the  BMI server. The  exiscan
+implementation  passes  the  message to  the  BMI  server just
+before accepting it.  It then adds  the retrieved verdicts  to
+the messages header file in the spool. These verdicts can then
+be  queried  in  routers,  where  operation  is  per-recipient
+instead  of per-message.  To use  BMI, you  need to  take the
+following steps:
+
+  1) Compile Exim with BMI support
+  2) Set up main BMI options (top section of Exim config file)
+  3) Set up ACL control statement (ACL section of the config
+     file)
+  4) Set up your routers to use BMI verdicts (routers section
+     of the config file).
+  5) (Optional) Set up per-recipient opt-in information.
+
+These four steps are explained in more details below.
+
+1) Adding support for BMI at compile time
+
+  To compile with BMI support,  you need to link Exim  against
+  the   Brighmail  client   SDK,  consisting   of  a   library
+  (libbmiclient_single.so)  and  a  header  file  (bmi_api.h).
+  You'll also need to explicitly set a flag in the Makefile to
+  include BMI support in the Exim binary. Both can be achieved
+  with  these lines in Local/Makefile:
+
+  EXPERIMENTAL_BRIGHTMAIL=yes
+  CFLAGS=-I/path/to/the/dir/with/the/includefile
+  EXTRALIBS_EXIM=-L/path/to/the/dir/with/the/library -lbmiclient_single
+
+  If  you use  other CFLAGS  or EXTRALIBS_EXIM  settings then
+  merge the content of these lines with them.
+
+  Note for BMI6.x users: You'll also have to add -lxml2_single
+  to the EXTRALIBS_EXIM line. Users of 5.5x do not need to  do
+  this.
+
+  You    should     also    include     the    location     of
+  libbmiclient_single.so in your dynamic linker  configuration
+  file   (usually   /etc/ld.so.conf)   and   run    "ldconfig"
+  afterwards, or  else the  produced Exim  binary will  not be
+  able to find the library file.
+
+
+2) Setting up BMI support in the Exim main configuration
+
+  To enable BMI  support in the  main Exim configuration,  you
+  should set the path to the main BMI configuration file  with
+  the "bmi_config_file" option, like this:
+
+  bmi_config_file = /opt/brightmail/etc/brightmail.cfg
+
+  This must go into section 1 of Exim's configuration file (You
+  can  put it  right on  top). If  you omit  this option,  it
+  defaults to /opt/brightmail/etc/brightmail.cfg.
+
+  Note for BMI6.x users: This  file is in XML format  in V6.xx
+  and its  name is  /opt/brightmail/etc/bmiconfig.xml. So  BMI
+  6.x users MUST set the bmi_config_file option.
+
+
+3) Set up ACL control statement
+
+  To  optimize performance,  it makes  sense only  to process
+  messages coming from remote, untrusted sources with the  BMI
+  server.  To set  up a  messages for  processing by  the BMI
+  server, you MUST set the "bmi_run" control statement in  any
+  ACL for an incoming message.  You will typically do this  in
+  an "accept"  block in  the "acl_check_rcpt"  ACL. You should
+  use the "accept" block(s)  that accept messages from  remote
+  servers for your own domain(s). Here is an example that uses
+  the "accept" blocks from Exim's default configuration file:
+
+
+  accept  domains       = +local_domains
+          endpass
+          verify        = recipient
+          control       = bmi_run
+
+  accept  domains       = +relay_to_domains
+          endpass
+          verify        = recipient
+          control       = bmi_run
+
+  If bmi_run  is not  set in  any ACL  during reception of the
+  message, it will NOT be passed to the BMI server.
+
+
+4) Setting up routers to use BMI verdicts
+
+  When a message has been  run through the BMI server,  one or
+  more "verdicts" are  present. Different recipients  can have
+  different verdicts. Each  recipient is treated  individually
+  during routing, so you  can query the verdicts  by recipient
+  at  that stage.  From Exim's  view, a  verdict can  have the
+  following outcomes:
+
+  o deliver the message normally
+  o deliver the message to an alternate location
+  o do not deliver the message
+
+  To query  the verdict  for a  recipient, the  implementation
+  offers the following tools:
+
+
+  - Boolean router  preconditions. These  can be  used in  any
+    router. For a simple  implementation of BMI, these  may be
+    all  that  you  need.  The  following  preconditions   are
+    available:
+
+    o bmi_deliver_default
+
+      This  precondition  is  TRUE  if  the  verdict  for  the
+      recipient is  to deliver  the message  normally. If  the
+      message has not been  processed by the BMI  server, this
+      variable defaults to TRUE.
+
+    o bmi_deliver_alternate
+
+      This  precondition  is  TRUE  if  the  verdict  for  the
+      recipient  is to  deliver the  message to  an alternate
+      location.  You  can  get the  location  string  from the
+      $bmi_alt_location expansion variable if you need it. See
+      further below. If the message has not been processed  by
+      the BMI server, this variable defaults to FALSE.
+
+    o bmi_dont_deliver
+
+      This  precondition  is  TRUE  if  the  verdict  for  the
+      recipient  is  NOT  to   deliver  the  message  to   the
+      recipient. You will typically use this precondition in a
+      top-level blackhole router, like this:
+
+        # don't deliver messages handled by the BMI server
+        bmi_blackhole:
+          driver = redirect
+          bmi_dont_deliver
+          data = :blackhole:
+
+      This router should be on top of all others, so  messages
+      that should not be delivered do not reach other  routers
+      at all. If   the  message  has  not  been  processed  by
+      the  BMI server, this variable defaults to FALSE.
+
+
+  - A list router  precondition to query  if rules "fired"  on
+    the message for the recipient. Its name is "bmi_rule". You
+    use  it  by  passing it  a  colon-separated  list of  rule
+    numbers. You can use this condition to route messages that
+    matched specific rules. Here is an example:
+
+      # special router for BMI rule #5, #8 and #11
+      bmi_rule_redirect:
+        driver = redirect
+        bmi_rule = 5:8:11
+        data = postmaster@mydomain.com
+
+
+  - Expansion variables. Several  expansion variables are  set
+    during  routing.  You  can  use  them  in  custom   router
+    conditions,  for  example.  The  following  variables  are
+    available:
+
+    o $bmi_base64_verdict
+
+      This variable  will contain  the BASE64  encoded verdict
+      for the recipient being routed. You can use it to add  a
+      header to messages for tracking purposes, for example:
+
+      localuser:
+        driver = accept
+        check_local_user
+        headers_add = X-Brightmail-Verdict: $bmi_base64_verdict
+        transport = local_delivery
+
+      If there is no verdict available for the recipient being
+      routed, this variable contains the empty string.
+
+    o $bmi_base64_tracker_verdict
+
+      This variable  will contain  a BASE64  encoded subset of
+      the  verdict  information  concerning  the  "rules" that
+      fired  on the  message. You  can add  this string  to a
+      header, commonly named "X-Brightmail-Tracker". Example:
+
+      localuser:
+        driver = accept
+        check_local_user
+        headers_add = X-Brightmail-Tracker: $bmi_base64_tracker_verdict
+        transport = local_delivery
+
+      If there is no verdict available for the recipient being
+      routed, this variable contains the empty string.
+
+    o $bmi_alt_location
+
+      If  the  verdict  is  to  redirect  the  message  to  an
+      alternate  location,  this  variable  will  contain  the
+      alternate location string returned by the BMI server. In
+      its default configuration, this is a header-like  string
+      that can be added to the message with "headers_add".  If
+      there is  no verdict  available for  the recipient being
+      routed, or if the  message is to be  delivered normally,
+      this variable contains the empty string.
+
+    o $bmi_deliver
+
+      This is an additional integer variable that can be  used
+      to query if the message should be delivered at all.  You
+      should use router preconditions instead if possible.
+
+      $bmi_deliver is '0': the message should NOT be delivered.
+      $bmi_deliver is '1': the message should be delivered.
+
+
+  IMPORTANT NOTE: Verdict inheritance.
+  The  message  is passed  to  the BMI  server  during message
+  reception,  using the  target addresses  from the  RCPT TO:
+  commands in the SMTP transaction. If recipients get expanded
+  or re-written (for example by aliasing), the new address(es)
+  inherit the  verdict from  the original  address. This means
+  that verdicts also apply to all "child" addresses  generated
+  from top-level addresses that were sent to the BMI server.
+
+
+5) Using per-recipient opt-in information (Optional)
+
+  The  BMI server  features multiple  scanning "profiles"  for
+  individual recipients.  These are  usually stored  in a LDAP
+  server and are  queried by the  BMI server itself.  However,
+  you can also  pass opt-in data  for each recipient  from the
+  MTA to the  BMI server. This  is particularly useful  if you
+  already look  up recipient  data in  Exim anyway  (which can
+  also be  stored in  a SQL  database or  other source).  This
+  implementation enables you  to pass opt-in  data to the  BMI
+  server  in  the  RCPT   ACL.  This  works  by   setting  the
+  'bmi_optin' modifier in  a block of  that ACL. If  should be
+  set to a list  of comma-separated strings that  identify the
+  features which the BMI server should use for that particular
+  recipient. Ideally, you  would use the  'bmi_optin' modifier
+  in the same  ACL block where  you set the  'bmi_run' control
+  flag. Here is an example that will pull opt-in data for each
+  recipient      from       a      flat       file      called
+  '/etc/exim/bmi_optin_data'.
+
+  The file format:
+
+    user1@mydomain.com: <OPTIN STRING1>:<OPTIN STRING2>
+    user2@thatdomain.com: <OPTIN STRING3>
+
+
+  The example:
+
+    accept  domains       = +relay_to_domains
+            endpass
+            verify        = recipient
+            bmi_optin     = ${lookup{$local_part@$domain}lsearch{/etc/exim/bmi_optin_data}}
+            control       = bmi_run
+
+  Of course,  you can  also use  any other  lookup method that
+  Exim supports, including LDAP, Postgres, MySQL, Oracle etc.,
+  as long as  the result is  a list of  colon-separated opt-in
+  strings.
+
+  For a list of available opt-in strings, please contact  your
+  Brightmail representative.
+
+
+
+
+Sender Policy Framework (SPF) support
+--------------------------------------------------------------
+
+To learn  more  about  SPF, visit   http://www.openspf.org. This
+document does   not explain  the SPF  fundamentals, you should
+read and understand the implications of deploying SPF on  your
+system before doing so.
+
+SPF support is added via the libspf2 library. Visit
+
+  http://www.libspf2.org/
+
+to obtain  a copy,  then compile  and install  it. By default,
+this will  put headers  in /usr/local/include  and the  static
+library in /usr/local/lib.
+
+To compile Exim with SPF support, set these additional flags in
+Local/Makefile:
+
+EXPERIMENTAL_SPF=yes
+CFLAGS=-DSPF -I/usr/local/include
+EXTRALIBS_EXIM=-L/usr/local/lib -lspf2
+
+This assumes   that the   libspf2 files   are installed  in
+their default locations.
+
+You can now run SPF checks in incoming SMTP by using the "spf"
+ACL condition  in either  the MAIL,  RCPT or  DATA ACLs.  When
+using it in the RCPT ACL, you can make the checks dependent on
+the RCPT  address (or  domain), so  you can  check SPF records
+only  for   certain  target   domains.  This   gives  you  the
+possibility  to opt-out  certain customers  that do  not want
+their mail to be subject to SPF checking.
+
+The spf condition  takes a list  of strings on  its right-hand
+side. These strings describe the outcome of the SPF check  for
+which the spf condition should succeed. Valid strings are:
+
+  o pass      The SPF check passed, the sending host
+              is positively verified by SPF.
+  o fail      The SPF check failed, the sending host
+              is NOT allowed to send mail for the domain
+              in the envelope-from address.
+  o softfail  The SPF check failed, but the queried
+              domain can't absolutely confirm that this
+              is a forgery.
+  o none      The queried domain does not publish SPF
+              records.
+  o neutral   The SPF check returned a "neutral" state.
+              This means the queried domain has published
+              a SPF record, but wants to allow outside
+              servers to send mail under its domain as well.
+              This should be treated like "none".
+  o permerror This indicates a syntax error in the SPF
+              record of the queried domain. You may deny
+              messages when this occurs. (Changed in 4.83)
+  o temperror This indicates a temporary error during all
+              processing, including Exim's SPF processing.
+              You may defer messages when this occurs.
+              (Changed in 4.83)
+  o err_temp  Same as permerror, deprecated in 4.83, will be
+              removed in a future release.
+  o err_perm  Same as temperror, deprecated in 4.83, will be
+              removed in a future release.
+
+You can prefix each string with an exclamation mark to  invert
+its meaning,  for example  "!fail" will  match all  results but
+"fail".  The  string  list is  evaluated  left-to-right,  in a
+short-circuit fashion.  When a  string matches  the outcome of
+the SPF check, the condition  succeeds. If none of the  listed
+strings matches the  outcome of the  SPF check, the  condition
+fails.
+
+Here is an example to fail forgery attempts from domains that
+publish SPF records:
+
+/* -----------------
+deny message = $sender_host_address is not allowed to send mail from ${if def:sender_address_domain {$sender_address_domain}{$sender_helo_name}}.  \
+              Please see http://www.openspf.org/Why?scope=${if def:sender_address_domain {mfrom}{helo}};identity=${if def:sender_address_domain {$sender_address}{$sender_helo_name}};ip=$sender_host_address
+     spf = fail
+--------------------- */
+
+You can also give special treatment to specific domains:
+
+/* -----------------
+deny message = AOL sender, but not from AOL-approved relay.
+     sender_domains = aol.com
+     spf = fail:neutral
+--------------------- */
+
+Explanation: AOL  publishes SPF  records, but  is liberal  and
+still allows  non-approved relays  to send  mail from aol.com.
+This will result in a "neutral" state, while mail from genuine
+AOL servers  will result  in "pass".  The example  above takes
+this into account and  treats "neutral" like "fail",  but only
+for aol.com. Please note that this violates the SPF draft.
+
+When the spf condition has run, it sets up several expansion
+variables.
+
+  $spf_header_comment
+  This contains a human-readable string describing the outcome
+  of the SPF check. You can add it to a custom header or use
+  it for logging purposes.
+
+  $spf_received
+  This contains a complete Received-SPF: header that can be
+  added to the message. Please note that according to the SPF
+  draft, this header must be added at the top of the header
+  list. Please see section 10 on how you can do this.
+
+  Note: in case of "Best-guess" (see below), the convention is
+  to put this string in a header called X-SPF-Guess: instead.
+
+  $spf_result
+  This contains the outcome of the SPF check in string form,
+  one of pass, fail, softfail, none, neutral, permerror or
+  temperror.
+
+  $spf_smtp_comment
+  This contains a string that can be used in a SMTP response
+  to the calling party. Useful for "fail".
+
+In addition to SPF, you can also perform checks for so-called
+"Best-guess".  Strictly speaking, "Best-guess" is not standard
+SPF, but it is supported by the same framework that enables SPF
+capability.  Refer to http://www.openspf.org/FAQ/Best_guess_record
+for a description of what it means.
+
+To access this feature, simply use the spf_guess condition in place
+of the spf one.  For example:
+
+/* -----------------
+deny message = $sender_host_address doesn't look trustworthy to me
+     spf_guess = fail
+--------------------- */
+
+In case you decide to reject messages based on this check, you
+should note that although it uses the same framework, "Best-guess"
+is NOT SPF, and therefore you should not mention SPF at all in your
+reject message.
+
+When the spf_guess condition has run, it sets up the same expansion
+variables as when spf condition is run, described above.
+
+Additionally, since Best-guess is not standardized, you may redefine
+what "Best-guess" means to you by redefining spf_guess variable in
+global config.  For example, the following:
+
+/* -----------------
+spf_guess = v=spf1 a/16 mx/16 ptr ?all
+--------------------- */
+
+would relax host matching rules to a broader network range.
+
+
+SRS (Sender Rewriting Scheme) Support
+--------------------------------------------------------------
+
+Exiscan  currently  includes SRS  support  via Miles  Wilton's
+libsrs_alt library. The current version of the supported
+library is 0.5.
+
+In order to  use SRS, you  must get a  copy of libsrs_alt from
+
+http://srs.mirtol.com/
+
+Unpack the tarball, then refer to MTAs/README.EXIM
+to proceed. You need to set
+
+EXPERIMENTAL_SRS=yes
+
+in your Local/Makefile.
+
+
+DCC Support
+--------------------------------------------------------------
+
+*) Building exim
+
+In order to build exim with DCC support add
+
+EXPERIMENTAL_DCC=yes
+
+to your Makefile. (Re-)build/install exim. exim -d should show
+EXPERIMENTAL_DCC under "Support for".
+
+
+*) Configuration
+
+In the main section of exim.cf add at least
+  dccifd_address = /usr/local/dcc/var/dccifd
+or
+  dccifd_address = <ip> <port>
+
+In the DATA ACL you can use the new condition
+        dcc = *
+
+After that "$dcc_header" contains the X-DCC-Header.
+
+Return values are:
+  fail    for overall "R", "G" from dccifd
+  defer   for overall "T" from dccifd
+  accept  for overall "A", "S" from dccifd
+
+dcc = */defer_ok works as for spamd.
+
+The "$dcc_result" variable contains the overall result from DCC
+answer.  There will an X-DCC: header added to the mail.
+
+Usually you'll use
+  defer   !dcc = *
+to greylist with DCC.
+
+If you set, in the main section,
+  dcc_direct_add_header = true
+then the dcc header will be added "in deep" and if the spool
+file was already written it gets removed. This forces Exim to
+write it again if needed.  This helps to get the DCC Header
+through to eg. SpamAssassin.
+
+If you want to pass even more headers in the middle of the
+DATA stage you can set
+  $acl_m_dcc_add_header
+to tell the DCC routines to add more information; eg, you might set
+this to some results from ClamAV.  Be careful.  Header syntax is
+not checked and is added "as is".
+
+In case you've troubles with sites sending the same queue items from several
+hosts and fail to get through greylisting you can use
+$acl_m_dcc_override_client_ip
+
+Setting $acl_m_dcc_override_client_ip to an IP address overrides the default
+of $sender_host_address. eg. use the following ACL in DATA stage:
+
+  warn    set acl_m_dcc_override_client_ip = \
+    ${lookup{$sender_helo_name}nwildlsearch{/etc/mail/multipleip_sites}{$value}{}}
+          condition = ${if def:acl_m_dcc_override_client_ip}
+          log_message = dbg: acl_m_dcc_override_client_ip set to \
+                        $acl_m_dcc_override_client_ip
+
+Then set something like
+# cat /etc/mail/multipleip_sites
+mout-xforward.gmx.net           82.165.159.12
+mout.gmx.net                    212.227.15.16
+
+Use a reasonable IP. eg. one the sending cluster acutally uses.
+
+DMARC Support
+--------------------------------------------------------------
+
+DMARC combines feedback from SPF, DKIM, and header From: in order
+to attempt to provide better indicators of the authenticity of an
+email.  This document does not explain the fundamentals, you
+should read and understand how it works by visiting the website at
+http://www.dmarc.org/.
+
+DMARC support is added via the libopendmarc library.  Visit:
+
+  http://sourceforge.net/projects/opendmarc/
+
+to obtain a copy, or find it in your favorite rpm package
+repository.  If building from source, this description assumes
+that headers will be in /usr/local/include, and that the libraries
+are in /usr/local/lib.
+
+1. To compile Exim with DMARC support, you must first enable SPF.
+Please read the above section on enabling the EXPERIMENTAL_SPF
+feature.  You must also have DKIM support, so you cannot set the
+DISABLE_DKIM feature.  Once both of those conditions have been met
+you can enable DMARC in Local/Makefile:
+
+EXPERIMENTAL_DMARC=yes
+LDFLAGS += -lopendmarc
+# CFLAGS += -I/usr/local/include
+# LDFLAGS += -L/usr/local/lib
+
+The first line sets the feature to include the correct code, and
+the second line says to link the libopendmarc libraries into the
+exim binary.  The commented out lines should be uncommented if you
+built opendmarc from source and installed in the default location.
+Adjust the paths if you installed them elsewhere, but you do not
+need to uncomment them if an rpm (or you) installed them in the
+package controlled locations (/usr/include and /usr/lib).
+
+
+2. Use the following global settings to configure DMARC:
+
+Required:
+dmarc_tld_file      Defines the location of a text file of valid
+                    top level domains the opendmarc library uses
+                    during domain parsing. Maintained by Mozilla,
+                    the most current version can be downloaded
+                    from a link at http://publicsuffix.org/list/.
+
+Optional:
+dmarc_history_file  Defines the location of a file to log results
+                    of dmarc verification on inbound emails. The
+                    contents are importable by the opendmarc tools
+                    which will manage the data, send out DMARC
+                    reports, and expire the data. Make sure the
+                    directory of this file is writable by the user
+                    exim runs as.
+
+dmarc_forensic_sender The email address to use when sending a
+                    forensic report detailing alignment failures
+                    if a sender domain's dmarc record specifies it
+                    and you have configured Exim to send them.
+                    Default: do-not-reply@$default_hostname
+
+
+3. By default, the DMARC processing will run for any remote,
+non-authenticated user.  It makes sense to only verify DMARC
+status of messages coming from remote, untrusted sources.  You can
+use standard conditions such as hosts, senders, etc, to decide that
+DMARC verification should *not* be performed for them and disable
+DMARC with a control setting:
+
+  control = dmarc_disable_verify
+
+A DMARC record can also specify a "forensic address", which gives
+exim an email address to submit reports about failed alignment.
+Exim does not do this by default because in certain conditions it
+results in unintended information leakage (what lists a user might
+be subscribed to, etc).  You must configure exim to submit forensic
+reports to the owner of the domain.  If the DMARC record contains a
+forensic address and you specify the control statement below, then
+exim will send these forensic emails.  It's also advised that you
+configure a dmarc_forensic_sender because the default sender address
+construction might be inadequate.
+
+  control = dmarc_forensic_enable
+
+(AGAIN: You can choose not to send these forensic reports by simply
+not putting the dmarc_forensic_enable control line at any point in
+your exim config.  If you don't tell it to send them, it will not
+send them.)
+
+There are no options to either control.  Both must appear before
+the DATA acl.
+
+
+4. You can now run DMARC checks in incoming SMTP by using the
+"dmarc_status" ACL condition in the DATA ACL.  You are required to
+call the spf condition first in the ACLs, then the "dmarc_status"
+condition.  Putting this condition in the ACLs is required in order
+for a DMARC check to actually occur.  All of the variables are set
+up before the DATA ACL, but there is no actual DMARC check that
+occurs until a "dmarc_status" condition is encountered in the ACLs.
+
+The dmarc_status condition takes a list of strings on its
+right-hand side.  These strings describe recommended action based
+on the DMARC check.  To understand what the policy recommendations
+mean, refer to the DMARC website above.  Valid strings are:
+
+  o accept      The DMARC check passed and the library recommends
+                accepting the email.
+  o reject      The DMARC check failed and the library recommends
+                rejecting the email.
+  o quarantine  The DMARC check failed and the library recommends
+                keeping it for further inspection.
+  o none        The DMARC check passed and the library recommends
+                no specific action, neutral.
+  o norecord    No policy section in the DMARC record for this
+                sender domain.
+  o nofrom      Unable to determine the domain of the sender.
+  o temperror   Library error or dns error.
+  o off         The DMARC check was disabled for this email.
+
+You can prefix each string with an exclamation mark to invert its
+meaning, for example "!accept" will match all results but
+"accept".  The string list is evaluated left-to-right in a
+short-circuit fashion.  When a string matches the outcome of the
+DMARC check, the condition succeeds.  If none of the listed
+strings matches the outcome of the DMARC check, the condition
+fails.
+
+Of course, you can also use any other lookup method that Exim
+supports, including LDAP, Postgres, MySQL, etc, as long as the
+result is a list of colon-separated strings.
+
+Several expansion variables are set before the DATA ACL is
+processed, and you can use them in this ACL.  The following
+expansion variables are available:
+
+  o $dmarc_status
+    This is a one word status indicating what the DMARC library
+    thinks of the email.  It is a combination of the results of
+    DMARC record lookup and the SPF/DKIM/DMARC processing results
+    (if a DMARC record was found).  The actual policy declared
+    in the DMARC record is in a separate expansion variable.
+
+  o $dmarc_status_text
+    This is a slightly longer, human readable status.
+
+  o $dmarc_used_domain
+    This is the domain which DMARC used to look up the DMARC
+    policy record.
+
+  o $dmarc_domain_policy
+    This is the policy declared in the DMARC record.  Valid values
+    are "none", "reject" and "quarantine".  It is blank when there
+    is any error, including no DMARC record.
+
+  o $dmarc_ar_header
+    This is the entire Authentication-Results header which you can
+    add using an add_header modifier.
+
+
+5. How to enable DMARC advanced operation:
+By default, Exim's DMARC configuration is intended to be
+non-intrusive and conservative.  To facilitate this, Exim will not
+create any type of logging files without explicit configuration by
+you, the admin.  Nor will Exim send out any emails/reports about
+DMARC issues without explicit configuration by you, the admin (other
+than typical bounce messages that may come about due to ACL
+processing or failure delivery issues).
+
+In order to log statistics suitable to be imported by the opendmarc
+tools, you need to:
+a. Configure the global setting dmarc_history_file.
+b. Configure cron jobs to call the appropriate opendmarc history
+   import scripts and truncating the dmarc_history_file.
+
+In order to send forensic reports, you need to:
+a. Configure the global setting dmarc_forensic_sender.
+b. Configure, somewhere before the DATA ACL, the control option to
+   enable sending DMARC forensic reports.
+
+
+6. Example usage:
+(RCPT ACL)
+  warn    domains        = +local_domains
+          hosts          = +local_hosts
+          control        = dmarc_disable_verify
+
+  warn    !domains       = +screwed_up_dmarc_records
+          control        = dmarc_enable_forensic
+
+  warn    condition      = (lookup if destined to mailing list)
+          set acl_m_mailing_list = 1
+
+(DATA ACL)
+  warn    dmarc_status   = accept : none : off
+          !authenticated = *
+          log_message    = DMARC DEBUG: $dmarc_status $dmarc_used_domain
+          add_header     = $dmarc_ar_header
+
+  warn    dmarc_status   = !accept
+          !authenticated = *
+          log_message    = DMARC DEBUG: '$dmarc_status' for $dmarc_used_domain
+
+  warn    dmarc_status   = quarantine
+          !authenticated = *
+          set $acl_m_quarantine = 1
+          # Do something in a transport with this flag variable
+
+  deny    condition      = ${if eq{$dmarc_domain_policy}{reject}}
+          condition      = ${if eq{$acl_m_mailing_list}{1}}
+          message        = Messages from $dmarc_used_domain break mailing lists
+
+  deny    dmarc_status   = reject
+          !authenticated = *
+          message        = Message from $domain_used_domain failed sender's DMARC policy, REJECT
+
+
+
+Transport post-delivery actions
+--------------------------------------------------------------
+
+An arbitrary per-transport string can be expanded on successful delivery,
+and (for SMTP transports) a second string on deferrals caused by a host error.
+This feature may be used, for example, to write exim internal log information
+(not available otherwise) into a database.
+
+In order to use the feature, you must set
+
+EXPERIMENTAL_TPDA=yes
+
+in your Local/Makefile
+
+and define the expandable strings in the runtime config file, to
+be executed at end of delivery.
+
+Additionally, there are 6 more variables, available at end of
+delivery:
+
+tpda_delivery_ip             IP of host, which has accepted delivery
+tpda_delivery_port           Port of remote host which has accepted delivery
+tpda_delivery_fqdn           FQDN of host, which has accepted delivery
+tpda_delivery_local_part     local part of address being delivered
+tpda_delivery_domain         domain part of address being delivered
+tpda_delivery_confirmation   SMTP confirmation message
+
+In case of a deferral caused by a host-error:
+tpda_defer_errno             Error number
+tpda_defer_errstr            Error string possibly containing more details
+
+The $router_name and $transport_name variables are also usable.
+
+
+To take action after successful deliveries, set the following option
+on any transport of interest.
+
+tpda_delivery_action
+
+An example might look like:
+
+tpda_delivery_action = \
+${lookup pgsql {SELECT * FROM record_Delivery( \
+    '${quote_pgsql:$sender_address_domain}',\
+    '${quote_pgsql:${lc:$sender_address_local_part}}', \
+    '${quote_pgsql:$tpda_delivery_domain}', \
+    '${quote_pgsql:${lc:$tpda_delivery_local_part}}', \
+    '${quote_pgsql:$tpda_delivery_ip}', \
+    '${quote_pgsql:${lc:$tpda_delivery_fqdn}}', \
+    '${quote_pgsql:$message_exim_id}')}}
+
+The string is expanded after the delivery completes and any
+side-effects will happen.  The result is then discarded.
+Note that for complex operations an ACL expansion can be used.
+
+
+In order to log host deferrals, add the following option to an SMTP
+transport:
+
+tpda_host_defer_action
+
+This is a private option of the SMTP transport. It is intended to
+log failures of remote hosts. It is executed only when exim has
+attempted to deliver a message to a remote host and failed due to
+an error which doesn't seem to be related to the individual
+message, sender, or recipient address.
+See section 47.2 of the exim documentation for more details on how
+this is determined.
+
+Example:
+
+tpda_host_defer_action = \
+${lookup mysql {insert into delivlog set \
+    msgid = '${quote_mysql:$message_exim_id}', \
+    senderlp = '${quote_mysql:${lc:$sender_address_local_part}}', \
+    senderdom = '${quote_mysql:$sender_address_domain}', \
+    delivlp = '${quote_mysql:${lc:$tpda_delivery_local_part}}', \
+    delivdom = '${quote_mysql:$tpda_delivery_domain}', \
+    delivip = '${quote_mysql:$tpda_delivery_ip}', \
+    delivport = '${quote_mysql:$tpda_delivery_port}', \
+    delivfqdn = '${quote_mysql:$tpda_delivery_fqdn}', \
+    deliverrno = '${quote_mysql:$tpda_defer_errno}', \
+    deliverrstr = '${quote_mysql:$tpda_defer_errstr}' \
+    }}
+
+
+Redis Lookup
+--------------------------------------------------------------
+
+Redis is open source advanced key-value data store. This document
+does not explain the fundamentals, you should read and understand how
+it works by visiting the website at http://www.redis.io/.
+
+Redis lookup support is added via the hiredis library.  Visit:
+
+  https://github.com/redis/hiredis
+
+to obtain a copy, or find it in your operating systems package repository.
+If building from source, this description assumes that headers will be in
+/usr/local/include, and that the libraries are in /usr/local/lib.
+
+1. In order to build exim with Redis lookup support add
+
+EXPERIMENTAL_REDIS=yes
+
+to your Local/Makefile. (Re-)build/install exim. exim -d should show
+Experimental_Redis in the line "Support for:".
+
+EXPERIMENTAL_REDIS=yes
+LDFLAGS += -lhiredis
+# CFLAGS += -I/usr/local/include
+# LDFLAGS += -L/usr/local/lib
+
+The first line sets the feature to include the correct code, and
+the second line says to link the hiredis libraries into the
+exim binary.  The commented out lines should be uncommented if you
+built hiredis from source and installed in the default location.
+Adjust the paths if you installed them elsewhere, but you do not
+need to uncomment them if an rpm (or you) installed them in the
+package controlled locations (/usr/include and /usr/lib).
+
+
+2. Use the following global settings to configure Redis lookup support:
+
+Required:
+redis_servers       This option provides a list of Redis servers
+                    and associated connection data, to be used in
+                    conjunction with redis lookups. The option is
+                    only available if Exim is configured with Redis
+                    support.
+
+For example:
+
+redis_servers = 127.0.0.1/10/ - using database 10 with no password
+redis_servers = 127.0.0.1//password - to make use of the default database of 0 with a password
+redis_servers = 127.0.0.1// - for default database of 0 with no password
+
+3. Once you have the Redis servers defined you can then make use of the
+experimental Redis lookup by specifying ${lookup redis{}} in a lookup query.
+
+4. Example usage:
+
+(Host List)
+hostlist relay_from_ips = <\n ${lookup redis{SMEMBERS relay_from_ips}}
+
+Where relay_from_ips is a Redis set which contains entries such as "192.168.0.0/24" "10.0.0.0/8" and so on.
+The result set is returned as
+192.168.0.0/24
+10.0.0.0/8
+..
+.
+
+(Domain list)
+domainlist virtual_domains = ${lookup redis {HGET $domain domain}}
+
+Where $domain is a hash which includes the key 'domain' and the value '$domain'.
+
+(Adding or updating an existing key)
+set acl_c_spammer = ${if eq{${lookup redis{SPAMMER_SET}}}{OK}}
+
+Where SPAMMER_SET is a macro and it is defined as
+
+"SET SPAMMER <some_value>"
+
+(Getting a value from Redis)
+
+set acl_c_spam_host = ${lookup redis{GET...}}
+
+
+Proxy Protocol Support
+--------------------------------------------------------------
+
+Exim now has Experimental "Proxy Protocol" support.  It was built on
+specifications from:
+http://haproxy.1wt.eu/download/1.5/doc/proxy-protocol.txt
+Above URL revised May 2014 to change version 2 spec:
+http://git.1wt.eu/web?p=haproxy.git;a=commitdiff;h=afb768340c9d7e50d8e
+
+The purpose of this function is so that an application load balancer,
+such as HAProxy, can sit in front of several Exim servers and Exim
+will log the IP that is connecting to the proxy server instead of
+the IP of the proxy server when it connects to Exim.  It resets the
+$sender_address_host and $sender_address_port to the IP:port of the
+connection to the proxy.  It also re-queries the DNS information for
+this new IP address so that the original sender's hostname and IP
+get logged in the Exim logfile.  There is no logging if a host passes or
+fails Proxy Protocol negotiation, but it can easily be determined and
+recorded in an ACL (example is below).
+
+1. To compile Exim with Proxy Protocol support, put this in
+Local/Makefile:
+
+EXPERIMENTAL_PROXY=yes
+
+2. Global configuration settings:
+
+proxy_required_hosts = HOSTLIST
+
+The proxy_required_hosts option will require any IP in that hostlist
+to use Proxy Protocol. The specification of Proxy Protocol is very
+strict, and if proxy negotiation fails, Exim will not allow any SMTP
+command other than QUIT. (See end of this section for an example.)
+The option is expanded when used, so it can be a hostlist as well as
+string of IP addresses.  Since it is expanded, specifying an alternate
+separator is supported for ease of use with IPv6 addresses.
+
+To log the IP of the proxy in the incoming logline, add:
+  log_selector = +proxy
+
+A default incoming logline (wrapped for appearance) will look like this:
+
+  2013-11-04 09:25:06 1VdNti-0001OY-1V <= me@example.net
+  H=mail.example.net [1.2.3.4] P=esmtp S=433
+
+With the log selector enabled, an email that was proxied through a
+Proxy Protocol server at 192.168.1.2 will look like this:
+
+  2013-11-04 09:25:06 1VdNti-0001OY-1V <= me@example.net
+  H=mail.example.net [1.2.3.4] P=esmtp PRX=192.168.1.2 S=433
+
+3. In the ACL's the following expansion variables are available.
+
+proxy_host_address   The (internal) src IP of the proxy server
+                     making the connection to the Exim server.
+proxy_host_port      The (internal) src port the proxy server is
+                     using to connect to the Exim server.
+proxy_target_address The dest (public) IP of the remote host to
+                     the proxy server.
+proxy_target_port    The dest port the remote host is using to
+                     connect to the proxy server.
+proxy_session        Boolean, yes/no, the connected host is required
+                     to use Proxy Protocol.
+
+There is no expansion for a failed proxy session, however you can detect
+it by checking if $proxy_session is true but $proxy_host is empty.  As
+an example, in my connect ACL, I have:
+
+  warn    condition      = ${if and{ {bool{$proxy_session}} \
+                                     {eq{$proxy_host_address}{}} } }
+          log_message    = Failed required proxy protocol negotiation \
+                           from $sender_host_name [$sender_host_address]
+
+  warn    condition      = ${if and{ {bool{$proxy_session}} \
+                                     {!eq{$proxy_host_address}{}} } }
+          # But don't log health probes from the proxy itself
+          condition      = ${if eq{$proxy_host_address}{$sender_host_address} \
+                                {false}{true}}
+          log_message    = Successfully proxied from $sender_host_name \
+                           [$sender_host_address] through proxy protocol \
+                           host $proxy_host_address
+
+  # Possibly more clear
+  warn logwrite = Remote Source Address: $sender_host_address:$sender_host_port
+       logwrite = Proxy Target Address: $proxy_target_address:$proxy_target_port
+       logwrite = Proxy Internal Address: $proxy_host_address:$proxy_host_port
+       logwrite = Internal Server Address: $received_ip_address:$received_port
+
+
+4. Recommended ACL additions:
+   - Since the real connections are all coming from your proxy, and the
+     per host connection tracking is done before Proxy Protocol is
+     evaluated, smtp_accept_max_per_host must be set high enough to
+     handle all of the parallel volume you expect per inbound proxy.
+   - With the smtp_accept_max_per_host set so high, you lose the ability
+     to protect your server from massive numbers of inbound connections
+     from one IP.  In order to prevent your server from being DOS'd, you
+     need to add a per connection ratelimit to your connect ACL.  I
+     suggest something like this:
+
+  # Set max number of connections per host
+  LIMIT   = 5
+  # Or do some kind of IP lookup in a flat file or database
+  # LIMIT = ${lookup{$sender_host_address}iplsearch{/etc/exim/proxy_limits}}
+
+  defer   message        = Too many connections from this IP right now
+          ratelimit      = LIMIT / 5s / per_conn / strict
+
+
+5. Runtime issues to be aware of:
+   - The proxy has 3 seconds (hard-coded in the source code) to send the
+     required Proxy Protocol header after it connects.  If it does not,
+     the response to any commands will be:
+     "503 Command refused, required Proxy negotiation failed"
+   - If the incoming connection is configured in Exim to be a Proxy
+     Protocol host, but the proxy is not sending the header, the banner
+     does not get sent until the timeout occurs.  If the sending host
+     sent any input (before the banner), this causes a standard Exim
+     synchronization error (i.e. trying to pipeline before PIPELINING
+     was advertised).
+   - This is not advised, but is mentioned for completeness if you have
+     a specific internal configuration that you want this:  If the Exim
+     server only has an internal IP address and no other machines in your
+     organization will connect to it to try to send email, you may
+     simply set the hostlist to "*", however, this will prevent local
+     mail programs from working because that would require mail from
+     localhost to use Proxy Protocol.  Again, not advised!
+
+6. Example of a refused connection because the Proxy Protocol header was
+not sent from a host configured to use Proxy Protocol.  In the example,
+the 3 second timeout occurred (when a Proxy Protocol banner should have
+been sent), the banner was displayed to the user, but all commands are
+rejected except for QUIT:
+
+# nc mail.example.net 25
+220-mail.example.net, ESMTP Exim 4.82+proxy, Mon, 04 Nov 2013 10:45:59
+220 -0800 RFC's enforced
+EHLO localhost
+503 Command refused, required Proxy negotiation failed
+QUIT
+221 mail.example.net closing connection
+
+
+DSN Support
+--------------------------------------------------------------
+
+DSN Support tries to add RFC 3461 support to Exim. It adds support for
+*) the additional parameters for MAIL FROM and RCPT TO
+*) RFC complient MIME DSN messages for all of
+   success, failure and delay notifications
+*) dsn_advertise_hosts main option to select which hosts are able
+   to use the extension
+*) dsn_lasthop router switch to end DSN processing
+
+In case of failure reports this means that the last three parts, the message body
+intro, size info and final text, of the defined template are ignored since there is no
+logical place to put them in the MIME message.
+
+All the other changes are made without changing any defaults
+
+Building exim:
+--------------
+
+Define
+EXPERIMENTAL_DSN=YES
+in your Local/Makefile.
+
+Configuration:
+--------------
+All DSNs are sent in MIME format if you built exim with EXPERIMENTAL_DSN=YES
+No option needed to activate it, and no way to turn it off.
+
+Failure and delay DSNs are triggered as usual except a sender used NOTIFY=...
+to prevent them.
+
+Support for Success DSNs is added and activated by NOTIFY=SUCCESS by clients.
+
+Add
+dsn_advertise_hosts = *
+or a more restrictive host_list to announce DSN in EHLO answers
+
+Those hosts can then use NOTIFY,ENVID,RET,ORCPT options.
+
+If a message is relayed to a DSN aware host without changing the envelope
+recipient the options are passed along and no success DSN is generated.
+
+A redirect router will always trigger a success DSN if requested and the DSN
+options are not passed any further.
+
+A success DSN always contains the recipient address as submitted by the
+client as required by RFC. Rewritten addresses are never exposed.
+
+If you used DSN patch up to 1.3 before remove all "dsn_process" switches from
+your routers since you don't need them anymore. There is no way to "gag"
+success DSNs anymore. Announcing DSN means answering as requested.
+
+You can prevent Exim from passing DSN options along to other DSN aware hosts by defining
+dsn_lasthop
+in a router. Exim will then send the success DSN himself if requested as if
+the next hop does not support DSN.
+Adding it to a redirect router makes no difference.
+
+Certificate name checking
+--------------------------------------------------------------
+The X509 certificates used for TLS are supposed be verified
+that they are owned by the expected host.  The coding of TLS
+support to date has not made these checks.
+
+If built with EXPERIMENTAL_CERTNAMES defined, code is
+included to do so, and a new smtp transport option
+"tls_verify_cert_hostname" supported which takes a list of
+names for which the checks must be made.  The host must
+also be in "tls_verify_hosts".
+
+Both Subject and Subject-Alternate-Name certificate fields
+are supported, as are wildcard certificates (limited to
+a single wildcard being the initial component of a 3-or-more
+component FQDN).
+
+
+
+--------------------------------------------------------------
+End of file
+--------------------------------------------------------------
diff --git a/doc/filter.txt b/doc/filter.txt
new file mode 100644 (file)
index 0000000..6d8544e
--- /dev/null
@@ -0,0 +1,1726 @@
+Exim's interfaces to mail filtering
+
+Philip Hazel
+
+Copyright (c) 2014 University of Cambridge
+
++-----------------------------------------------------------------------------+
++-------------------------------------+--------------------------------+------+
+|Revision 4.84                        |11 Aug 2014                     |PH    |
++-------------------------------------+--------------------------------+------+
+-------------------------------------------------------------------------------
+
+TABLE OF CONTENTS
+
+1. Forwarding and filtering in Exim
+
+    1.1. Introduction
+    1.2. Filter operation
+    1.3. Testing a new filter file
+    1.4. Installing a filter file
+    1.5. Testing an installed filter file
+    1.6. Details of filtering commands
+
+2. Sieve filter files
+
+    2.1. Recognition of Sieve filters
+    2.2. Saving to specified folders
+    2.3. Strings containing header names
+    2.4. Exists test with empty list of headers
+    2.5. Header test with invalid MIME encoding in header
+    2.6. Address test for multiple addresses per header
+    2.7. Semantics of keep
+    2.8. Semantics of fileinto
+    2.9. Semantics of redirect
+    2.10. String arguments
+    2.11. Number units
+    2.12. RFC compliance
+
+3. Exim filter files
+
+    3.1. Format of Exim filter files
+    3.2. Data values in filter commands
+    3.3. String expansion
+    3.4. Some useful general variables
+    3.5. Header variables
+    3.6. User variables
+    3.7. Current directory
+    3.8. Significant deliveries
+    3.9. Filter commands
+    3.10. The add command
+    3.11. The deliver command
+    3.12. The save command
+    3.13. The pipe command
+    3.14. Mail commands
+    3.15. Logging commands
+    3.16. The finish command
+    3.17. The testprint command
+    3.18. The fail command
+    3.19. The freeze command
+    3.20. The headers command
+    3.21. Obeying commands conditionally
+    3.22. String testing conditions
+    3.23. Numeric testing conditions
+    3.24. Testing for significant deliveries
+    3.25. Testing for error messages
+    3.26. Testing a list of addresses
+    3.27. Testing for personal mail
+    3.28. Alias addresses for the personal condition
+    3.29. Details of the personal condition
+    3.30. Testing delivery status
+    3.31. Multiple personal mailboxes
+    3.32. Ignoring delivery errors
+    3.33. Examples of Exim filter commands
+
+
+
+===============================================================================
+1. FORWARDING AND FILTERING IN EXIM
+
+This document describes the user interfaces to Exim's in-built mail filtering
+facilities, and is copyright (c) University of Cambridge 2014. It corresponds
+to Exim version 4.84.
+
+
+1.1 Introduction
+----------------
+
+Most Unix mail transfer agents (programs that deliver mail) permit individual
+users to specify automatic forwarding of their mail, usually by placing a list
+of forwarding addresses in a file called .forward in their home directories.
+Exim extends this facility by allowing the forwarding instructions to be a set
+of rules rather than just a list of addresses, in effect providing ".forward
+with conditions". Operating the set of rules is called filtering, and the file
+that contains them is called a filter file.
+
+Exim supports two different kinds of filter file. An Exim filter contains
+instructions in a format that is unique to Exim. A Sieve filter contains
+instructions in the Sieve format that is defined by RFC 3028. As this is a
+standard format, Sieve filter files may already be familiar to some users.
+Sieve files should also be portable between different environments. However,
+the Exim filtering facility contains more features (such as variable
+expansion), and better integration with the host environment (such as the use
+of external processes and pipes).
+
+The choice of which kind of filter to use can be left to the end-user, provided
+that the system administrator has configured Exim appropriately for both kinds
+of filter. However, if interoperability is important, Sieve is the only choice.
+
+The ability to use filtering or traditional forwarding has to be enabled by the
+system administrator, and some of the individual facilities can be separately
+enabled or disabled. A local document should be provided to describe exactly
+what has been enabled. In the absence of this, consult your system
+administrator.
+
+This document describes how to use a filter file and the format of its
+contents. It is intended for use by end-users. Both Sieve filters and Exim
+filters are covered. However, for Sieve filters, only issues that relate to the
+Exim implementation are discussed, since Sieve itself is described elsewhere.
+
+The contents of traditional .forward files are not described here. They
+normally contain just a list of addresses, file names, or pipe commands,
+separated by commas or newlines, but other types of item are also available.
+The full details can be found in the chapter on the redirect router in the Exim
+specification, which also describes how the system administrator can set up and
+control the use of filtering.
+
+
+1.2 Filter operation
+--------------------
+
+It is important to realize that, in Exim, no deliveries are actually made while
+a filter or traditional .forward file is being processed. Running a filter or
+processing a traditional .forward file sets up future delivery operations, but
+does not carry them out.
+
+The result of filter or .forward file processing is a list of destinations to
+which a message should be delivered. The deliveries themselves take place
+later, along with all other deliveries for the message. This means that it is
+not possible to test for successful deliveries while filtering. It also means
+that any duplicate addresses that are generated are dropped, because Exim never
+delivers the same message to the same address more than once.
+
+
+1.3 Testing a new filter file
+-----------------------------
+
+Filter files, especially the more complicated ones, should always be tested, as
+it is easy to make mistakes. Exim provides a facility for preliminary testing
+of a filter file before installing it. This tests the syntax of the file and
+its basic operation, and can also be used with traditional .forward files.
+
+Because a filter can do tests on the content of messages, a test message is
+required. Suppose you have a new filter file called myfilter and a test message
+in a file called test-message. Assuming that Exim is installed with the
+conventional path name /usr/sbin/sendmail (some operating systems use /usr/lib/
+sendmail), the following command can be used:
+
+/usr/sbin/sendmail -bf myfilter <test-message
+
+The -bf option tells Exim that the following item on the command line is the
+name of a filter file that is to be tested. There is also a -bF option, which
+is similar, but which is used for testing system filter files, as opposed to
+user filter files, and which is therefore of use only to the system
+administrator.
+
+The test message is supplied on the standard input. If there are no
+message-dependent tests in the filter, an empty file (/dev/null) can be used. A
+supplied message must start with header lines or the "From " message separator
+line that is found in many multi-message folder files. Note that blank lines at
+the start terminate the header lines. A warning is given if no header lines are
+read.
+
+The result of running this command, provided no errors are detected in the
+filter file, is a list of the actions that Exim would try to take if presented
+with the message for real. For example, for an Exim filter, the output
+
+Deliver message to: gulliver@lilliput.fict.example
+Save message to: /home/lemuel/mail/archive
+
+means that one copy of the message would be sent to 
+gulliver@lilliput.fict.example, and another would be added to the file /home/
+lemuel/mail/archive, if all went well.
+
+The actions themselves are not attempted while testing a filter file in this
+way; there is no check, for example, that any forwarding addresses are valid.
+For an Exim filter, if you want to know why a particular action is being taken,
+add the -v option to the command. This causes Exim to output the results of any
+conditional tests and to indent its output according to the depth of nesting of
+if commands. Further additional output from a filter test can be generated by
+the testprint command, which is described below.
+
+When Exim is outputting a list of the actions it would take, if any text
+strings are included in the output, non-printing characters therein are
+converted to escape sequences. In particular, if any text string contains a
+newline character, this is shown as "\n" in the testing output.
+
+When testing a filter in this way, Exim makes up an "envelope" for the message.
+The recipient is by default the user running the command, and so is the sender,
+but the command can be run with the -f option to supply a different sender. For
+example,
+
+/usr/sbin/sendmail -bf myfilter \
+   -f islington@never.where <test-message
+
+Alternatively, if the -f option is not used, but the first line of the supplied
+message is a "From " separator from a message folder file (not the same thing
+as a From: header line), the sender is taken from there. If -f is present, the
+contents of any "From " line are ignored.
+
+The "return path" is the same as the envelope sender, unless the message
+contains a Return-path: header, in which case it is taken from there. You need
+not worry about any of this unless you want to test out features of a filter
+file that rely on the sender address or the return path.
+
+It is possible to change the envelope recipient by specifying further options.
+The -bfd option changes the domain of the recipient address, while the -bfl
+option changes the "local part", that is, the part before the @ sign. An
+adviser could make use of these to test someone else's filter file.
+
+The -bfp and -bfs options specify the prefix or suffix for the local part.
+These are relevant only when support for multiple personal mailboxes is
+implemented; see the description in section 3.31 below.
+
+
+1.4 Installing a filter file
+----------------------------
+
+A filter file is normally installed under the name .forward in your home
+directory - it is distinguished from a conventional .forward file by its first
+line (described below). However, the file name is configurable, and some system
+administrators may choose to use some different name or location for filter
+files.
+
+
+1.5 Testing an installed filter file
+------------------------------------
+
+Testing a filter file before installation cannot find every potential problem;
+for example, it does not actually run commands to which messages are piped.
+Some "live" tests should therefore also be done once a filter is installed.
+
+If at all possible, test your filter file by sending messages from some other
+account. If you send a message to yourself from the filtered account, and
+delivery fails, the error message will be sent back to the same account, which
+may cause another delivery failure. It won't cause an infinite sequence of such
+messages, because delivery failure messages do not themselves generate further
+messages. However, it does mean that the failure won't be returned to you, and
+also that the postmaster will have to investigate the stuck message.
+
+If you have to test an Exim filter from the same account, a sensible precaution
+is to include the line
+
+if error_message then finish endif
+
+as the first filter command, at least while testing. This causes filtering to
+be abandoned for a delivery failure message, and since no destinations are
+generated, the message goes on to be delivered to the original address. Unless
+there is a good reason for not doing so, it is recommended that the above test
+be left in all Exim filter files. (This does not apply to Sieve files.)
+
+
+1.6 Details of filtering commands
+---------------------------------
+
+The filtering commands for Sieve and Exim filters are completely different in
+syntax and semantics. The Sieve mechanism is defined in RFC 3028; in the next
+chapter we describe how it is integrated into Exim. The subsequent chapter
+covers Exim filtering commands in detail.
+
+
+
+===============================================================================
+2. SIEVE FILTER FILES
+
+The code for Sieve filtering in Exim was contributed by Michael Haardt, and
+most of the content of this chapter is taken from the notes he provided. Since
+Sieve is an extensible language, it is important to understand "Sieve" in this
+context as "the specific implementation of Sieve for Exim".
+
+This chapter does not contain a description of Sieve, since that can be found
+in RFC 3028, which should be read in conjunction with these notes.
+
+The Exim Sieve implementation offers the core as defined by RFC 3028,
+comparison tests, the subaddress parameter, the copy, envelope, fileinto, 
+notify, and vacation extensions, but not the reject extension. Exim does not
+support message delivery notifications (MDNs), so adding it just to the Sieve
+filter (as required for reject) makes little sense.
+
+In order for Sieve to work properly in Exim, the system administrator needs to
+make some adjustments to the Exim configuration. These are described in the
+chapter on the redirect router in the full Exim specification.
+
+
+2.1 Recognition of Sieve filters
+--------------------------------
+
+A filter file is interpreted as a Sieve filter if its first line is
+
+# Sieve filter
+
+This is what distinguishes it from a conventional .forward file or an Exim
+filter file.
+
+
+2.2 Saving to specified folders
+-------------------------------
+
+If the system administrator has set things up as suggested in the Exim
+specification, and you use keep or fileinto to save a mail into a folder,
+absolute files are stored where specified, relative files are stored relative
+to $home, and inbox goes to the standard mailbox location.
+
+
+2.3 Strings containing header names
+-----------------------------------
+
+RFC 3028 does not specify what happens if a string denoting a header field does
+not contain a valid header name, for example, it contains a colon. This
+implementation generates an error instead of ignoring the header field in order
+to ease script debugging, which fits in with the common picture of Sieve.
+
+
+2.4 Exists test with empty list of headers
+------------------------------------------
+
+The exists test succeeds only if all the specified headers exist. RFC 3028 does
+not explicitly specify what happens on an empty list of headers. This
+implementation evaluates that condition as true, interpreting the RFC in a
+strict sense.
+
+
+2.5 Header test with invalid MIME encoding in header
+----------------------------------------------------
+
+Some MUAs process invalid base64 encoded data, generating junk. Others ignore
+junk after seeing an equal sign in base64 encoded data. RFC 2047 does not
+specify how to react in this case, other than stating that a client must not
+forbid to process a message for that reason. RFC 2045 specifies that invalid
+data should be ignored (apparently looking at end of line characters). It also
+specifies that invalid data may lead to rejecting messages containing them (and
+there it appears to talk about true encoding violations), which is a clear
+contradiction to ignoring them.
+
+RFC 3028 does not specify how to process incorrect MIME words. This
+implementation treats them literally, as it does if the word is correct but its
+character set cannot be converted to UTF-8.
+
+
+2.6 Address test for multiple addresses per header
+--------------------------------------------------
+
+A header may contain multiple addresses. RFC 3028 does not explicitly specify
+how to deal with them, but since the address test checks if anything matches
+anything else, matching one address suffices to satisfy the condition. That
+makes it impossible to test if a header contains a certain set of addresses and
+no more, but it is more logical than letting the test fail if the header
+contains an additional address besides the one the test checks for.
+
+
+2.7 Semantics of keep
+---------------------
+
+The keep command is equivalent to
+
+fileinto "inbox";
+
+It saves the message and resets the implicit keep flag. It does not set the
+implicit keep flag; there is no command to set it once it has been reset.
+
+
+2.8 Semantics of fileinto
+-------------------------
+
+RFC 3028 does not specify whether fileinto should try to create a mail folder
+if it does not exist. This implementation allows the sysadmin to configure that
+aspect using the appendfile transport options create_directory, create_file,
+and file_must_exist. See the appendfile transport in the Exim specification for
+details.
+
+
+2.9 Semantics of redirect
+-------------------------
+
+Sieve scripts are supposed to be interoperable between servers, so this
+implementation does not allow mail to be redirected to unqualified addresses,
+because the domain would depend on the system being used. On systems with
+virtual mail domains, the default domain is probably not what the user expects
+it to be.
+
+
+2.10 String arguments
+---------------------
+
+There has been confusion if the string arguments to require are to be matched
+case-sensitively or not. This implementation matches them with the match type 
+:is (default, see section 2.7.1 of the RFC) and the comparator i;ascii-casemap
+(default, see section 2.7.3 of the RFC). The RFC defines the command defaults
+clearly, so any different implementations violate RFC 3028. The same is valid
+for comparator names, also specified as strings.
+
+
+2.11 Number units
+-----------------
+
+There is a mistake in RFC 3028: the suffix G denotes gibi-, not tebibyte. The
+mistake is obvious, because RFC 3028 specifies G to denote 2^30 (which is gibi,
+not tebi), and that is what this implementation uses as the scaling factor for
+the suffix G.
+
+
+2.12 RFC compliance
+-------------------
+
+Exim requires the first line of a Sieve filter to be
+
+# Sieve filter
+
+Of course the RFC does not specify that line. Do not expect examples to work
+without adding it, though.
+
+RFC 3028 requires the use of CRLF to terminate a line. The rationale was that
+CRLF is universally used in network protocols to mark the end of the line. This
+implementation does not embed Sieve in a network protocol, but uses Sieve
+scripts as part of the Exim MTA. Since all parts of Exim use LF as the newline
+character, this implementation does, too, by default, though the system
+administrator may choose (at Exim compile time) to use CRLF instead.
+
+Exim violates RFC 2822, section 3.6.8, by accepting 8-bit header names, so this
+implementation repeats this violation to stay consistent with Exim. This is in
+preparation for UTF-8 data.
+
+Sieve scripts cannot contain NUL characters in strings, but mail headers could
+contain MIME encoded NUL characters, which could never be matched by Sieve
+scripts using exact comparisons. For that reason, this implementation extends
+the Sieve quoted string syntax with \0 to describe a NUL character, violating \
+0 being the same as 0 in RFC 3028. Even without using \0, the following tests
+are all true in this implementation. Implementations that use C-style strings
+will only evaluate the first test as true.
+
+Subject: =?iso-8859-1?q?abc=00def
+
+header :contains "Subject" ["abc"]
+header :contains "Subject" ["def"]
+header :matches "Subject" ["abc?def"]
+
+Note that by considering Sieve to be an MUA, RFC 2047 can be interpreted in a
+way that NUL characters truncating strings is allowed for Sieve
+implementations, although not recommended. It is further allowed to use encoded
+NUL characters in headers, but that's not recommended either. The above example
+shows why.
+
+RFC 3028 states that if an implementation fails to convert a character set to
+UTF-8, two strings cannot be equal if one contains octets greater than 127.
+Assuming that all unknown character sets are one-byte character sets with the
+lower 128 octets being US-ASCII is not sound, so this implementation violates
+RFC 3028 and treats such MIME words literally. That way at least something
+could be matched.
+
+The folder specified by fileinto must not contain the character sequence ".."
+to avoid security problems. RFC 3028 does not specify the syntax of folders
+apart from keep being equivalent to
+
+fileinto "INBOX";
+
+This implementation uses inbox instead.
+
+Sieve script errors currently cause messages to be silently filed into inbox.
+RFC 3028 requires that the user is notified of that condition. This may be
+implemented in the future by adding a header line to mails that are filed into
+inbox due to an error in the filter.
+
+
+
+===============================================================================
+3. EXIM FILTER FILES
+
+This chapter contains a full description of the contents of Exim filter files.
+
+
+3.1 Format of Exim filter files
+-------------------------------
+
+Apart from leading white space, the first text in an Exim filter file must be
+
+# Exim filter
+
+This is what distinguishes it from a conventional .forward file or a Sieve
+filter file. If the file does not have this initial line (or the equivalent for
+a Sieve filter), it is treated as a conventional .forward file, both when
+delivering mail and when using the -bf testing mechanism. The white space in
+the line is optional, and any capitalization may be used. Further text on the
+same line is treated as a comment. For example, you could have
+
+#   Exim filter   <<== do not edit or remove this line!
+
+The remainder of the file is a sequence of filtering commands, which consist of
+keywords and data values. For example, in the command
+
+deliver gulliver@lilliput.fict.example
+
+the keyword is "deliver" and the data value is
+"gulliver@lilliput.fict.example". White space or line breaks separate the
+components of a command, except in the case of conditions for the if command,
+where round brackets (parentheses) also act as separators. Complete commands
+are separated from each other by white space or line breaks; there are no
+special terminators. Thus, several commands may appear on one line, or one
+command may be spread over a number of lines.
+
+If the character # follows a separator anywhere in a command, everything from #
+up to the next newline is ignored. This provides a way of including comments in
+a filter file.
+
+
+3.2 Data values in filter commands
+----------------------------------
+
+There are two ways in which a data value can be input:
+
+  * If the text contains no white space, it can be typed verbatim. However, if
+    it is part of a condition, it must also be free of round brackets
+    (parentheses), as these are used for grouping in conditions.
+
+  * Otherwise, text must be enclosed in double quotation marks. In this case,
+    the character \ (backslash) is treated as an "escape character" within the
+    string, causing the following character or characters to be treated
+    specially:
+
+    \n   is replaced by a newline
+    \r   is replaced by a carriage return
+    \t   is replaced by a tab
+
+Backslash followed by up to three octal digits is replaced by the character
+specified by those digits, and "\x" followed by up to two hexadecimal digits is
+treated similarly. Backslash followed by any other character is replaced by the
+second character, so that in particular, "\"" becomes """ and "\\" becomes "\".
+A data item enclosed in double quotes can be continued onto the next line by
+ending the first line with a backslash. Any leading white space at the start of
+the continuation line is ignored.
+
+In addition to the escape character processing that occurs when strings are
+enclosed in quotes, most data values are also subject to string expansion (as
+described in the next section), in which case the characters "$" and "\" are
+also significant. This means that if a single backslash is actually required in
+such a string, and the string is also quoted, "\\\\" has to be entered.
+
+The maximum permitted length of a data string, before expansion, is 1024
+characters.
+
+
+3.3 String expansion
+--------------------
+
+Most data values are expanded before use. Expansion consists of replacing
+substrings beginning with "$" with other text. The full expansion facilities
+available in Exim are extensive. If you want to know everything that Exim can
+do with strings, you should consult the chapter on string expansion in the Exim
+documentation.
+
+In filter files, by far the most common use of string expansion is the
+substitution of the contents of a variable. For example, the substring
+
+$reply_address
+
+is replaced by the address to which replies to the message should be sent. If
+such a variable name is followed by a letter or digit or underscore, it must be
+enclosed in curly brackets (braces), for example,
+
+${reply_address}
+
+If a "$" character is actually required in an expanded string, it must be
+escaped with a backslash, and because backslash is also an escape character in
+quoted input strings, it must be doubled in that case. The following two
+examples illustrate two different ways of testing for a "$" character in a
+message:
+
+if $message_body contains \$ then ...
+if $message_body contains "\\$" then ...
+
+You can prevent part of a string from being expanded by enclosing it between
+two occurrences of "\N". For example,
+
+if $message_body contains \N$$$$\N then ...
+
+tests for a run of four dollar characters.
+
+
+3.4 Some useful general variables
+---------------------------------
+
+A complete list of the available variables is given in the Exim documentation.
+This shortened list contains the ones that are most likely to be useful in
+personal filter files:
+
+$body_linecount: The number of lines in the body of the message.
+
+$body_zerocount: The number of binary zero characters in the body of the
+message.
+
+$home: In conventional configurations, this variable normally contains the
+user's home directory. The system administrator can, however, change this.
+
+$local_part: The part of the email address that precedes the @ sign - normally
+the user's login name. If support for multiple personal mailboxes is enabled
+(see section 3.31 below) and a prefix or suffix for the local part was
+recognized, it is removed from the string in this variable.
+
+$local_part_prefix: If support for multiple personal mailboxes is enabled (see
+section 3.31 below), and a local part prefix was recognized, this variable
+contains the prefix. Otherwise it contains an empty string.
+
+$local_part_suffix: If support for multiple personal mailboxes is enabled (see
+section 3.31 below), and a local part suffix was recognized, this variable
+contains the suffix. Otherwise it contains an empty string.
+
+$message_body: The initial portion of the body of the message. By default, up
+to 500 characters are read into this variable, but the system administrator can
+configure this to some other value. Newlines in the body are converted into
+single spaces.
+
+$message_body_end: The final portion of the body of the message, formatted and
+limited in the same way as $message_body.
+
+$message_body_size: The size of the body of the message, in bytes.
+
+$message_exim_id: The message's local identification string, which is unique
+for each message handled by a single host.
+
+$message_headers: The header lines of the message, concatenated into a single
+string, with newline characters between them.
+
+$message_size: The size of the entire message, in bytes.
+
+$original_local_part: When an address that arrived with the message is being
+processed, this contains the same value as the variable $local_part. However,
+if an address generated by an alias, forward, or filter file is being
+processed, this variable contains the local part of the original address.
+
+$reply_address: The contents of the Reply-to: header, if the message has one;
+otherwise the contents of the From: header. It is the address to which normal
+replies to the message should be sent.
+
+$return_path: The return path - that is, the sender field that will be
+transmitted as part of the message's envelope if the message is sent to another
+host. This is the address to which delivery errors are sent. In many cases,
+this variable has the same value as $sender_address, but if, for example, an
+incoming message to a mailing list has been expanded, $return_path may have
+been changed to contain the address of the list maintainer.
+
+$sender_address: The sender address that was received in the envelope of the
+message. This is not necessarily the same as the contents of the From: or 
+Sender: header lines. For delivery error messages ("bounce messages") there is
+no sender address, and this variable is empty.
+
+$tod_full: A full version of the time and date, for example: Wed, 18 Oct 1995
+09:51:40 +0100. The timezone is always given as a numerical offset from GMT.
+
+$tod_log: The time and date in the format used for writing Exim's log files,
+without the timezone, for example: 1995-10-12 15:32:29.
+
+$tod_zone: The local timezone offset, for example: +0100.
+
+
+3.5 Header variables
+--------------------
+
+There is a special set of expansion variables containing the header lines of
+the message being processed. These variables have names beginning with $header_
+followed by the name of the header line, terminated by a colon. For example,
+
+$header_from:
+$header_subject:
+
+The whole item, including the terminating colon, is replaced by the contents of
+the message header line. If there is more than one header line with the same
+name, their contents are concatenated. For header lines whose data consists of
+a list of addresses (for example, From: and To:), a comma and newline is
+inserted between each set of data. For all other header lines, just a newline
+is used.
+
+Leading and trailing white space is removed from header line data, and if there
+are any MIME "words" that are encoded as defined by RFC 2047 (because they
+contain non-ASCII characters), they are decoded and translated, if possible, to
+a local character set. Translation is attempted only on operating systems that
+have the iconv() function. This makes the header line look the same as it would
+when displayed by an MUA. The default character set is ISO-8859-1, but this can
+be changed by means of the headers command (see below).
+
+If you want to see the actual characters that make up a header line, you can
+specify $rheader_ instead of $header_. This inserts the "raw" header line,
+unmodified.
+
+There is also an intermediate form, requested by $bheader_, which removes
+leading and trailing space and decodes MIME "words", but does not do any
+character translation. If an attempt to decode what looks superficially like a
+MIME "word" fails, the raw string is returned. If decoding produces a binary
+zero character, it is replaced by a question mark.
+
+The capitalization of the name following $header_ is not significant. Because
+any printing character except colon may appear in the name of a message's
+header (this is a requirement of RFC 2822, the document that describes the
+format of a mail message) curly brackets must not be used in this case, as they
+will be taken as part of the header name. Two shortcuts are allowed in naming
+header variables:
+
+  * The initiating $header_, $rheader_, or $bheader_ can be abbreviated to $h_,
+    $rh_, or $bh_, respectively.
+
+  * The terminating colon can be omitted if the next character is white space.
+    The white space character is retained in the expanded string. However, this
+    is not recommended, because it makes it easy to forget the colon when it
+    really is needed.
+
+If the message does not contain a header of the given name, an empty string is
+substituted. Thus it is important to spell the names of headers correctly. Do
+not use $header_Reply_to when you really mean $header_Reply-to.
+
+
+3.6 User variables
+------------------
+
+There are ten user variables with names $n0 - $n9 that can be incremented by
+the add command (see section 3.10). These can be used for "scoring" messages in
+various ways. If Exim is configured to run a "system filter" on every message,
+the values left in these variables are copied into the variables $sn0 - $sn9 at
+the end of the system filter, thus making them available to users' filter
+files. How these values are used is entirely up to the individual installation.
+
+
+3.7 Current directory
+---------------------
+
+The contents of your filter file should not make any assumptions about the
+current directory. It is best to use absolute paths for file names; you can
+normally make use of the $home variable to refer to your home directory. The 
+save command automatically inserts $home at the start of non-absolute paths.
+
+
+3.8 Significant deliveries
+--------------------------
+
+When in the course of delivery a message is processed by a filter file, what
+happens next, that is, after the filter file has been processed, depends on
+whether or not the filter sets up any significant deliveries. If at least one
+significant delivery is set up, the filter is considered to have handled the
+entire delivery arrangements for the current address, and no further processing
+of the address takes place. If, however, no significant deliveries are set up,
+Exim continues processing the current address as if there were no filter file,
+and typically sets up a delivery of a copy of the message into a local mailbox.
+In particular, this happens in the special case of a filter file containing
+only comments.
+
+The delivery commands deliver, save, and pipe are by default significant.
+However, if such a command is preceded by the word "unseen", its delivery is
+not considered to be significant. In contrast, other commands such as mail and 
+vacation do not set up significant deliveries unless preceded by the word
+"seen". The following example commands set up significant deliveries:
+
+deliver jack@beanstalk.example
+pipe $home/bin/mymailscript
+seen mail subject "message discarded"
+seen finish
+
+The following example commands do not set up significant deliveries:
+
+unseen deliver jack@beanstalk.example
+unseen pipe $home/bin/mymailscript
+mail subject "message discarded"
+finish
+
+
+3.9 Filter commands
+-------------------
+
+The filter commands that are described in subsequent sections are listed below,
+with the section in which they are described in brackets:
+
+add         increment a user variable (section 3.10)
+deliver     deliver to an email address (section 3.11)
+fail        force delivery failure (sysadmin use) (section 3.18)
+finish      end processing (section 3.16)
+freeze      freeze message (sysadmin use) (section 3.19)
+headers     set the header character set (section 3.20)
+if          test condition(s) (section 3.21)
+logfile     define log file (section 3.15)
+logwrite    write to log file (section 3.15)
+mail        send a reply message (section 3.14)
+pipe        pipe to a command (section 3.13)
+save        save to a file (section 3.12)
+testprint   print while testing (section 3.17)
+vacation    tailored form of mail (section 3.14)
+
+The headers command has additional parameters that can be used only in a system
+filter. The fail and freeze commands are available only when Exim's filtering
+facilities are being used as a system filter, and are therefore usable only by
+the system administrator and not by ordinary users. They are mentioned only
+briefly in this document; for more information, see the main Exim
+specification.
+
+
+3.10 The add command
+--------------------
+
+     add <number> to <user variable>
+e.g. add 2 to n3
+
+There are 10 user variables of this type, with names $n0 - $n9. Their values
+can be obtained by the normal expansion syntax (for example $n3) in other
+commands. At the start of filtering, these variables all contain zero. Both
+arguments of the add command are expanded before use, making it possible to add
+variables to each other. Subtraction can be obtained by adding negative
+numbers.
+
+
+3.11 The deliver command
+------------------------
+
+     deliver <mail address>
+e.g. deliver "Dr Livingstone <David@somewhere.africa.example>"
+
+This command provides a forwarding operation. The delivery that it sets up is
+significant unless the command is preceded by "unseen" (see section 3.8). The
+message is sent on to the given address, exactly as happens if the address had
+appeared in a traditional .forward file. If you want to deliver the message to
+a number of different addresses, you can use more than one deliver command
+(each one may have only one address). However, duplicate addresses are
+discarded.
+
+To deliver a copy of the message to your normal mailbox, your login name can be
+given as the address. Once an address has been processed by the filtering
+mechanism, an identical generated address will not be so processed again, so
+doing this does not cause a loop.
+
+However, if you have a mail alias, you should not refer to it here. For
+example, if the mail address L.Gulliver is aliased to lg303 then all references
+in Gulliver's .forward file should be to lg303. A reference to the alias will
+not work for messages that are addressed to that alias, since, like .forward
+file processing, aliasing is performed only once on an address, in order to
+avoid looping.
+
+Following the new address, an optional second address, preceded by "errors_to"
+may appear. This changes the address to which delivery errors on the forwarded
+message will be sent. Instead of going to the message's original sender, they
+go to this new address. For ordinary users, the only value that is permitted
+for this address is the user whose filter file is being processed. For example,
+the user lg303 whose mailbox is in the domain lilliput.example could have a
+filter file that contains
+
+deliver jon@elsewhere.example errors_to lg303@lilliput.example
+
+Clearly, using this feature makes sense only in situations where not all
+messages are being forwarded. In particular, bounce messages must not be
+forwarded in this way, as this is likely to create a mail loop if something
+goes wrong.
+
+
+3.12 The save command
+---------------------
+
+     save <file name>
+e.g. save $home/mail/bookfolder
+
+This command specifies that a copy of the message is to be appended to the
+given file (that is, the file is to be used as a mail folder). The delivery
+that save sets up is significant unless the command is preceded by "unseen"
+(see section 3.8).
+
+More than one save command may be obeyed; each one causes a copy of the message
+to be written to its argument file, provided they are different (duplicate save
+commands are ignored).
+
+If the file name does not start with a / character, the contents of the $home
+variable are prepended, unless it is empty, or the system administrator has
+disabled this feature. In conventional configurations, this variable is
+normally set in a user filter to the user's home directory, but the system
+administrator may set it to some other path. In some configurations, $home may
+be unset, or prepending may be disabled, in which case a non-absolute path name
+may be generated. Such configurations convert this to an absolute path when the
+delivery takes place. In a system filter, $home is never set.
+
+The user must of course have permission to write to the file, and the writing
+of the file takes place in a process that is running as the user, under the
+user's primary group. Any secondary groups to which the user may belong are not
+normally taken into account, though the system administrator can configure Exim
+to set them up. In addition, the ability to use this command at all is
+controlled by the system administrator - it may be forbidden on some systems.
+
+An optional mode value may be given after the file name. The value for the mode
+is interpreted as an octal number, even if it does not begin with a zero. For
+example:
+
+save /some/folder 640
+
+This makes it possible for users to override the system-wide mode setting for
+file deliveries, which is normally 600. If an existing file does not have the
+correct mode, it is changed.
+
+An alternative form of delivery may be enabled on your system, in which each
+message is delivered into a new file in a given directory. If this is the case,
+this functionality can be requested by giving the directory name terminated by
+a slash after the save command, for example
+
+save separated/messages/
+
+There are several different formats for such deliveries; check with your system
+administrator or local documentation to find out which (if any) are available
+on your system. If this functionality is not enabled, the use of a path name
+ending in a slash causes an error.
+
+
+3.13 The pipe command
+---------------------
+
+     pipe <command>
+e.g. pipe "$home/bin/countmail $sender_address"
+
+This command specifies that the message is to be delivered to the specified
+command using a pipe. The delivery that it sets up is significant unless the
+command is preceded by "unseen" (see section 3.8). Remember, however, that no
+deliveries are done while the filter is being processed. All deliveries happen
+later on. Therefore, the result of running the pipe is not available to the
+filter.
+
+When the deliveries are done, a separate process is run, and a copy of the
+message is passed on its standard input. The process runs as the user, under
+the user's primary group. Any secondary groups to which the user may belong are
+not normally taken into account, though the system administrator can configure
+Exim to set them up. More than one pipe command may appear; each one causes a
+copy of the message to be written to its argument pipe, provided they are
+different (duplicate pipe commands are ignored).
+
+When the time comes to transport the message, the command supplied to pipe is
+split up by Exim into a command name and a number of arguments. These are
+delimited by white space except for arguments enclosed in double quotes, in
+which case backslash is interpreted as an escape, or in single quotes, in which
+case no escaping is recognized. Note that as the whole command is normally
+supplied in double quotes, a second level of quoting is required for internal
+double quotes. For example:
+
+pipe "$home/myscript \"size is $message_size\""
+
+String expansion is performed on the separate components after the line has
+been split up, and the command is then run directly by Exim; it is not run
+under a shell. Therefore, substitution cannot change the number of arguments,
+nor can quotes, backslashes or other shell metacharacters in variables cause
+confusion.
+
+Documentation for some programs that are normally run via this kind of pipe
+often suggest that the command should start with
+
+IFS=" "
+
+This is a shell command, and should not be present in Exim filter files, since
+it does not normally run the command under a shell.
+
+However, there is an option that the administrator can set to cause a shell to
+be used. In this case, the entire command is expanded as a single string and
+passed to the shell for interpretation. It is recommended that this be avoided
+if at all possible, since it can lead to problems when inserted variables
+contain shell metacharacters.
+
+The default PATH set up for the command is determined by the system
+administrator, usually containing at least /bin and /usr/bin so that common
+commands are available without having to specify an absolute file name.
+However, it is possible for the system administrator to restrict the pipe
+facility so that the command name must not contain any / characters, and must
+be found in one of the directories in the configured PATH. It is also possible
+for the system administrator to lock out the use of the pipe command
+altogether.
+
+When the command is run, a number of environment variables are set up. The
+complete list for pipe deliveries may be found in the Exim reference manual.
+Those that may be useful for pipe deliveries from user filter files are:
+
+DOMAIN               the domain of the address
+HOME                 your home directory
+LOCAL_PART           see below
+LOCAL_PART_PREFIX    see below
+LOCAL_PART_SUFFIX    see below
+LOGNAME              your login name
+MESSAGE_ID           the unique id of the message
+PATH                 the command search path
+RECIPIENT            the complete recipient address
+SENDER               the sender of the message
+SHELL                /bin/sh
+USER                 see below
+
+LOCAL_PART, LOGNAME, and USER are all set to the same value, namely, your login
+id. LOCAL_PART_PREFIX and LOCAL_PART_SUFFIX may be set if Exim is configured to
+recognize prefixes or suffixes in the local parts of addresses. For example, a
+message addressed to pat-suf2@domain.example may cause the filter for user pat
+to be run. If this sets up a pipe delivery, LOCAL_PART_SUFFIX is "-suf2" when
+the pipe command runs. The system administrator has to configure Exim specially
+for this feature to be available.
+
+If you run a command that is a shell script, be very careful in your use of
+data from the incoming message in the commands in your script. RFC 2822 is very
+generous in the characters that are permitted to appear in mail addresses, and
+in particular, an address may begin with a vertical bar or a slash. For this
+reason you should always use quotes round any arguments that involve data from
+the message, like this:
+
+/some/command '$SENDER'
+
+so that inserted shell meta-characters do not cause unwanted effects.
+
+Remember that, as was explained earlier, the pipe command is not run at the
+time the filter file is interpreted. The filter just defines what deliveries
+are required for one particular addressee of a message. The deliveries
+themselves happen later, once Exim has decided everything that needs to be done
+for the message.
+
+A consequence of this is that you cannot inspect the return code from the pipe
+command from within the filter. Nevertheless, the code returned by the command
+is important, because Exim uses it to decide whether the delivery has succeeded
+or failed.
+
+The command should return a zero completion code if all has gone well. Most
+non-zero codes are treated by Exim as indicating a failure of the pipe. This is
+treated as a delivery failure, causing the message to be returned to its
+sender. However, there are some completion codes that are treated as temporary
+errors. The message remains on Exim's spool disk, and the delivery is tried
+again later, though it will ultimately time out if the delivery failures go on
+too long. The completion codes to which this applies can be specified by the
+system administrator; the default values are 73 and 75.
+
+The pipe command should not normally write anything to its standard output or
+standard error file descriptors. If it does, whatever is written is normally
+returned to the sender of the message as a delivery error, though this action
+can be varied by the system administrator.
+
+
+3.14 Mail commands
+------------------
+
+There are two commands that cause the creation of a new mail message, neither
+of which count as a significant delivery unless the command is preceded by the
+word "seen" (see section 3.8). This is a powerful facility, but it should be
+used with care, because of the danger of creating infinite sequences of
+messages. The system administrator can forbid the use of these commands
+altogether.
+
+To help prevent runaway message sequences, these commands have no effect when
+the incoming message is a bounce (delivery error) message, and messages sent by
+this means are treated as if they were reporting delivery errors. Thus, they
+should never themselves cause a bounce message to be returned. The basic
+mail-sending command is
+
+mail [to <address-list>]
+     [cc <address-list>]
+     [bcc <address-list>]
+     [from <address>]
+     [reply_to <address>]
+     [subject <text>]
+     [extra_headers <text>]
+     [text <text>]
+     [[expand] file <filename>]
+     [return message]
+     [log <log file name>]
+     [once <note file name>]
+     [once_repeat <time interval>]
+e.g. mail text "Your message about $h_subject: has been received"
+
+Each <address-list> can contain a number of addresses, separated by commas, in
+the format of a To: or Cc: header line. In fact, the text you supply here is
+copied exactly into the appropriate header line. It may contain additional
+information as well as email addresses. For example:
+
+mail to "Julius Caesar <jc@rome.example>, \
+         <ma@rome.example> (Mark A.)"
+
+Similarly, the texts supplied for from and reply_to are copied into their
+respective header lines.
+
+As a convenience for use in one common case, there is also a command called 
+vacation. It behaves in the same way as mail, except that the defaults for the 
+subject, file, log, once, and once_repeat options are
+
+subject "On vacation"
+expand file .vacation.msg
+log  .vacation.log
+once .vacation
+once_repeat 7d
+
+respectively. These are the same file names and repeat period used by the
+traditional Unix vacation command. The defaults can be overridden by explicit
+settings, but if a file name is given its contents are expanded only if
+explicitly requested.
+
+Warning: The vacation command should always be used conditionally, subject to
+at least the personal condition (see section 3.27 below) so as not to send
+automatic replies to non-personal messages from mailing lists or elsewhere.
+Sending an automatic response to a mailing list or a mailing list manager is an
+Internet Sin.
+
+For both commands, the key/value argument pairs can appear in any order. At
+least one of text or file must appear (except with vacation, where there is a
+default for file); if both are present, the text string appears first in the
+message. If expand precedes file, each line of the file is subject to string
+expansion before it is included in the message.
+
+Several lines of text can be supplied to text by including the escape sequence
+"\n" in the string wherever a newline is required. If the command is output
+during filter file testing, newlines in the text are shown as "\n".
+
+Note that the keyword for creating a Reply-To: header is reply_to, because Exim
+keywords may contain underscores, but not hyphens. If the from keyword is
+present and the given address does not match the user who owns the forward
+file, Exim normally adds a Sender: header to the message, though it can be
+configured not to do this.
+
+The extra_headers keyword allows you to add custom header lines to the message.
+The text supplied must be one or more syntactically valid RFC 2822 header
+lines. You can use "\n" within quoted text to specify newlines between headers,
+and also to define continued header lines. For example:
+
+extra_headers "h1: first\nh2: second\n continued\nh3: third"
+
+No newline should appear at the end of the final header line.
+
+If no to argument appears, the message is sent to the address in the
+$reply_address variable (see section 3.3 above). An In-Reply-To: header is
+automatically included in the created message, giving a reference to the
+message identification of the incoming message.
+
+If return message is specified, the incoming message that caused the filter
+file to be run is added to the end of the message, subject to a maximum size
+limitation.
+
+If a log file is specified, a line is added to it for each message sent.
+
+If a once file is specified, it is used to hold a database for remembering who
+has received a message, and no more than one message is ever sent to any
+particular address, unless once_repeat is set. This specifies a time interval
+after which another copy of the message is sent. The interval is specified as a
+sequence of numbers, each followed by the initial letter of one of "seconds",
+"minutes", "hours", "days", or "weeks". For example,
+
+once_repeat 5d4h
+
+causes a new message to be sent if at least 5 days and 4 hours have elapsed
+since the last one was sent. There must be no white space in a time interval.
+
+Commonly, the file name specified for once is used as the base name for
+direct-access (DBM) file operations. There are a number of different DBM
+libraries in existence. Some operating systems provide one as a default, but
+even in this case a different one may have been used when building Exim. With
+some DBM libraries, specifying once results in two files being created, with
+the suffixes .dir and .pag being added to the given name. With some others a
+single file with the suffix .db is used, or the name is used unchanged.
+
+Using a DBM file for implementing the once feature means that the file grows as
+large as necessary. This is not usually a problem, but some system
+administrators want to put a limit on it. The facility can be configured not to
+use a DBM file, but instead, to use a regular file with a maximum size. The
+data in such a file is searched sequentially, and if the file fills up, the
+oldest entry is deleted to make way for a new one. This means that some
+correspondents may receive a second copy of the message after an unpredictable
+interval. Consult your local information to see if your system is configured
+this way.
+
+More than one mail or vacation command may be obeyed in a single filter run;
+they are all honoured, even when they are to the same recipient.
+
+
+3.15 Logging commands
+---------------------
+
+A log can be kept of actions taken by a filter file. This facility is normally
+available in conventional configurations, but there are some situations where
+it might not be. Also, the system administrator may choose to disable it. Check
+your local information if in doubt.
+
+Logging takes place while the filter file is being interpreted. It does not
+queue up for later like the delivery commands. The reason for this is so that a
+log file need be opened only once for several write operations. There are two
+commands, neither of which constitutes a significant delivery. The first
+defines a file to which logging output is subsequently written:
+
+     logfile <file name>
+e.g. logfile $home/filter.log
+
+The file name must be fully qualified. You can use $home, as in this example,
+to refer to your home directory. The file name may optionally be followed by a
+mode for the file, which is used if the file has to be created. For example,
+
+logfile $home/filter.log 0644
+
+The number is interpreted as octal, even if it does not begin with a zero. The
+default for the mode is 600. It is suggested that the logfile command normally
+appear as the first command in a filter file. Once a log file has been obeyed,
+the logwrite command can be used to write to it:
+
+     logwrite "<some text string>"
+e.g. logwrite "$tod_log $message_id processed"
+
+It is possible to have more than one logfile command, to specify writing to
+different log files in different circumstances. Writing takes place at the end
+of the file, and a newline character is added to the end of each string if
+there isn't one already there. Newlines can be put in the middle of the string
+by using the "\n" escape sequence. Lines from simultaneous deliveries may get
+interleaved in the file, as there is no interlocking, so you should plan your
+logging with this in mind. However, data should not get lost.
+
+
+3.16 The finish command
+-----------------------
+
+The command finish, which has no arguments, causes Exim to stop interpreting
+the filter file. This is not a significant action unless preceded by "seen". A
+filter file containing only "seen finish" is a black hole.
+
+
+3.17 The testprint command
+--------------------------
+
+It is sometimes helpful to be able to print out the values of variables when
+testing filter files. The command
+
+     testprint <text>
+e.g. testprint "home=$home reply_address=$reply_address"
+
+does nothing when mail is being delivered. However, when the filtering code is
+being tested by means of the -bf option (see section 1.3 above), the value of
+the string is written to the standard output.
+
+
+3.18 The fail command
+---------------------
+
+When Exim's filtering facilities are being used as a system filter, the fail
+command is available, to force delivery failure. Because this command is
+normally usable only by the system administrator, and not enabled for use by
+ordinary users, it is described in more detail in the main Exim specification
+rather than in this document.
+
+
+3.19 The freeze command
+-----------------------
+
+When Exim's filtering facilities are being used as a system filter, the freeze
+command is available, to freeze a message on the queue. Because this command is
+normally usable only by the system administrator, and not enabled for use by
+ordinary users, it is described in more detail in the main Exim specification
+rather than in this document.
+
+
+3.20 The headers command
+------------------------
+
+The headers command can be used to change the target character set that is used
+when translating the contents of encoded header lines for insertion by the
+$header_ mechanism (see section 3.5 above). The default can be set in the Exim
+configuration; if not specified, ISO-8859-1 is used. The only currently
+supported format for the headers command in user filters is as in this example:
+
+headers charset "UTF-8"
+
+That is, headers is followed by the word "charset" and then the name of a
+character set. This particular example would be useful if you wanted to compare
+the contents of a header to a UTF-8 string.
+
+In system filter files, the headers command can be used to add or remove header
+lines from the message. These features are described in the main Exim
+specification.
+
+
+3.21 Obeying commands conditionally
+-----------------------------------
+
+Most of the power of filtering comes from the ability to test conditions and
+obey different commands depending on the outcome. The if command is used to
+specify conditional execution, and its general form is
+
+if    <condition>
+then  <commands>
+elif  <condition>
+then  <commands>
+else  <commands>
+endif
+
+There may be any number of elif and then sections (including none) and the else
+section is also optional. Any number of commands, including nested if commands,
+may appear in any of the <commands> sections.
+
+Conditions can be combined by using the words and and or, and round brackets
+(parentheses) can be used to specify how several conditions are to combine.
+Without brackets, and is more binding than or. For example:
+
+if
+$h_subject: contains "Make money" or
+$h_precedence: is "junk" or
+($h_sender: matches ^\\d{8}@ and not personal) or
+$message_body contains "this is not spam"
+then
+seen finish
+endif
+
+A condition can be preceded by not to negate it, and there are also some
+negative forms of condition that are more English-like.
+
+
+3.22 String testing conditions
+------------------------------
+
+There are a number of conditions that operate on text strings, using the words
+"begins", "ends", "is", "contains" and "matches". If you want to apply the same
+test to more than one header line, you can easily concatenate them into a
+single string for testing, as in this example:
+
+if "$h_to:, $h_cc:" contains me@domain.example then ...
+
+If a string-testing condition name is written in lower case, the testing of
+letters is done without regard to case; if it is written in upper case (for
+example, "CONTAINS"), the case of letters is taken into account.
+
+     <text1> begins <text2>
+     <text1> does not begin <text2>
+e.g. $header_from: begins "Friend@"
+
+A "begins" test checks for the presence of the second string at the start of
+the first, both strings having been expanded.
+
+     <text1> ends <text2>
+     <text1> does not end <text2>
+e.g. $header_from: ends "public.com.example"
+
+An "ends" test checks for the presence of the second string at the end of the
+first, both strings having been expanded.
+
+     <text1> is <text2>
+     <text1> is not <text2>
+e.g. $local_part_suffix is "-foo"
+
+An "is" test does an exact match between the strings, having first expanded
+both strings.
+
+     <text1> contains <text2>
+     <text1> does not contain <text2>
+e.g. $header_subject: contains "evolution"
+
+A "contains" test does a partial string match, having expanded both strings.
+
+     <text1> matches <text2>
+     <text1> does not match <text2>
+e.g. $sender_address matches "(bill|john)@"
+
+For a "matches" test, after expansion of both strings, the second one is
+interpreted as a regular expression. Exim uses the PCRE regular expression
+library, which provides regular expressions that are compatible with Perl.
+
+The match succeeds if the regular expression matches any part of the first
+string. If you want a regular expression to match only at the start or end of
+the subject string, you must encode that requirement explicitly, using the "^"
+or "$" metacharacters. The above example, which is not so constrained, matches
+all these addresses:
+
+bill@test.example
+john@some.example
+spoonbill@example.com
+littlejohn@example.com
+
+To match only the first two, you could use this:
+
+if $sender_address matches "^(bill|john)@" then ...
+
+Care must be taken if you need a backslash in a regular expression, because
+backslashes are interpreted as escape characters both by the string expansion
+code and by Exim's normal processing of strings in quotes. For example, if you
+want to test the sender address for a domain ending in .com the regular
+expression is
+
+\.com$
+
+The backslash and dollar sign in that expression have to be escaped when used
+in a filter command, as otherwise they would be interpreted by the expansion
+code. Thus, what you actually write is
+
+if $sender_address matches \\.com\$
+
+An alternative way of handling this is to make use of the "\N" expansion flag
+for suppressing expansion:
+
+if $sender_address matches \N\.com$\N
+
+Everything between the two occurrences of "\N" is copied without change by the
+string expander (and in fact you do not need the final one, because it is at
+the end of the string). If the regular expression is given in quotes (mandatory
+only if it contains white space) you have to write either
+
+if $sender_address matches "\\\\.com\\$"
+
+or
+
+if $sender_address matches "\\N\\.com$\\N"
+
+If the regular expression contains bracketed sub-expressions, numeric variable
+substitutions such as $1 can be used in the subsequent actions after a
+successful match. If the match fails, the values of the numeric variables
+remain unchanged. Previous values are not restored after endif. In other words,
+only one set of values is ever available. If the condition contains several
+sub-conditions connected by and or or, it is the strings extracted from the
+last successful match that are available in subsequent actions. Numeric
+variables from any one sub-condition are also available for use in subsequent
+sub-conditions, because string expansion of a condition occurs just before it
+is tested.
+
+
+3.23 Numeric testing conditions
+-------------------------------
+
+The following conditions are available for performing numerical tests:
+
+     <number1> is above <number2>
+     <number1> is not above <number2>
+     <number1> is below <number2>
+     <number1> is not below <number2>
+e.g. $message_size is not above 10k
+
+The <number> arguments must expand to strings of digits, optionally followed by
+one of the letters K or M (upper case or lower case) which cause multiplication
+by 1024 and 1024x1024 respectively.
+
+
+3.24 Testing for significant deliveries
+---------------------------------------
+
+You can use the delivered condition to test whether or not any previously
+obeyed filter commands have set up a significant delivery. For example:
+
+if not delivered then save mail/anomalous endif
+
+"Delivered" is perhaps a poor choice of name for this condition, because the
+message has not actually been delivered; rather, a delivery has been set up for
+later processing.
+
+
+3.25 Testing for error messages
+-------------------------------
+
+The condition error_message is true if the incoming message is a bounce (mail
+delivery error) message. Putting the command
+
+if error_message then finish endif
+
+at the head of your filter file is a useful insurance against things going
+wrong in such a way that you cannot receive delivery error reports. Note: 
+error_message is a condition, not an expansion variable, and therefore is not
+preceded by "$".
+
+
+3.26 Testing a list of addresses
+--------------------------------
+
+There is a facility for looping through a list of addresses and applying a
+condition to each of them. It takes the form
+
+foranyaddress <string> (<condition>)
+
+where <string> is interpreted as a list of RFC 2822 addresses, as in a typical
+header line, and <condition> is any valid filter condition or combination of
+conditions. The "group" syntax that is defined for certain header lines that
+contain addresses is supported.
+
+The parentheses surrounding the condition are mandatory, to delimit it from
+possible further sub-conditions of the enclosing if command. Within the
+condition, the expansion variable $thisaddress is set to the non-comment
+portion of each of the addresses in the string in turn. For example, if the
+string is
+
+B.Simpson <bart@sfld.example>, lisa@sfld.example (his sister)
+
+then $thisaddress would take on the values "bart@sfld.example" and
+"lisa@sfld.example" in turn.
+
+If there are no valid addresses in the list, the whole condition is false. If
+the internal condition is true for any one address, the overall condition is
+true and the loop ends. If the internal condition is false for all addresses in
+the list, the overall condition is false. This example tests for the presence
+of an eight-digit local part in any address in a To: header:
+
+if foranyaddress $h_to: ( $thisaddress matches ^\\d{8}@ ) then ...
+
+When the overall condition is true, the value of $thisaddress in the commands
+that follow then is the last value it took on inside the loop. At the end of
+the if command, the value of $thisaddress is reset to what it was before. It is
+best to avoid the use of multiple occurrences of foranyaddress, nested or
+otherwise, in a single if command, if the value of $thisaddress is to be used
+afterwards, because it isn't always clear what the value will be. Nested if
+commands should be used instead.
+
+Header lines can be joined together if a check is to be applied to more than
+one of them. For example:
+
+if foranyaddress $h_to:,$h_cc: ....
+
+This scans through the addresses in both the To: and the Cc: headers.
+
+
+3.27 Testing for personal mail
+------------------------------
+
+A common requirement is to distinguish between incoming personal mail and mail
+from a mailing list, or from a robot or other automatic process (for example, a
+bounce message). In particular, this test is normally required for "vacation
+messages".
+
+The personal condition checks that the message is not a bounce message and that
+the current user's email address appears in the To: header. It also checks that
+the sender is not the current user or one of a number of common daemons, and
+that there are no header lines starting List- in the message. Finally, it
+checks the content of the Precedence: header line, if there is one.
+
+You should always use the personal condition when generating automatic
+responses. This example shows the use of personal in a filter file that is
+sending out vacation messages:
+
+if personal then
+mail to $reply_address
+subject "I am on holiday"
+file $home/vacation/message
+once $home/vacation/once
+once_repeat 10d
+endif
+
+It is tempting, when writing commands like the above, to quote the original
+subject in the reply. For example:
+
+subject "Re: $h_subject:"
+
+There is a danger in doing this, however. It may allow a third party to
+subscribe you to an opt-in mailing list, provided that the list accepts bounce
+messages as subscription confirmations. (Messages sent from filters are always
+sent as bounce messages.) Well-managed lists require a non-bounce message to
+confirm a subscription, so the danger is relatively small.
+
+If prefixes or suffixes are in use for local parts - something which depends on
+the configuration of Exim (see section 3.31 below) - the tests for the current
+user are done with the full address (including the prefix and suffix, if any)
+as well as with the prefix and suffix removed. If the system is configured to
+rewrite local parts of mail addresses, for example, to rewrite "dag46" as
+"Dirk.Gently", the rewritten form of the address is also used in the tests.
+
+
+3.28 Alias addresses for the personal condition
+-----------------------------------------------
+
+It is quite common for people who have mail accounts on a number of different
+systems to forward all their mail to one system, and in this case a check for
+personal mail should test all their various mail addresses. To allow for this,
+the personal condition keyword can be followed by
+
+alias <address>
+
+any number of times, for example:
+
+if personal alias smith@else.where.example
+            alias jones@other.place.example
+then ...
+
+The alias addresses are treated as alternatives to the current user's email
+address when testing the contents of header lines.
+
+
+3.29 Details of the personal condition
+--------------------------------------
+
+The basic personal test is roughly equivalent to the following:
+
+not error_message and
+$message_headers does not contain "\nList-Id:" and
+$message_headers does not contain "\nList-Help:" and
+$message_headers does not contain "\nList-Subscribe:" and
+$message_headers does not contain "\nList-Unsubscribe:" and
+$message_headers does not contain "\nList-Post:" and
+$message_headers does not contain "\nList-Owner:" and
+$message_headers does not contain "\nList-Archive:" and
+(
+"${if def:h_auto-submitted:{present}{absent}}" is "absent" or
+$header_auto-submitted: is "no"
+) and
+$header_precedence: does not contain "bulk" and
+$header_precedence: does not contain "list" and
+$header_precedence: does not contain "junk" and
+foranyaddress $header_to:
+( $thisaddress contains "$local_part$domain" ) and
+not foranyaddress $header_from:
+(
+$thisaddress contains "$local_part@$domain" or
+$thisaddress contains "server@" or
+$thisaddress contains "daemon@" or
+$thisaddress contains "root@" or
+$thisaddress contains "listserv@" or
+$thisaddress contains "majordomo@" or
+$thisaddress contains "-request@" or
+$thisaddress matches  "^owner-[^@]+@"
+)
+
+The variable $local_part contains the local part of the mail address of the
+user whose filter file is being run - it is normally your login id. The $domain
+variable contains the mail domain. As explained above, if aliases or rewriting
+are defined, or if prefixes or suffixes are in use, the tests for the current
+user are also done with alternative addresses.
+
+
+3.30 Testing delivery status
+----------------------------
+
+There are two conditions that are intended mainly for use in system filter
+files, but which are available in users' filter files as well. The condition 
+first_delivery is true if this is the first process that is attempting to
+deliver the message, and false otherwise. This indicator is not reset until the
+first delivery process successfully terminates; if there is a crash or a power
+failure (for example), the next delivery attempt is also a "first delivery".
+
+In a user filter file first_delivery will be false if there was previously an
+error in the filter, or if a delivery for the user failed owing to, for
+example, a quota error, or if forwarding to a remote address was deferred for
+some reason.
+
+The condition manually_thawed is true if the message was "frozen" for some
+reason, and was subsequently released by the system administrator. It is
+unlikely to be of use in users' filter files.
+
+
+3.31 Multiple personal mailboxes
+--------------------------------
+
+The system administrator can configure Exim so that users can set up variants
+on their email addresses and handle them separately. Consult your system
+administrator or local documentation to see if this facility is enabled on your
+system, and if so, what the details are.
+
+The facility involves the use of a prefix or a suffix on an email address. For
+example, all mail addressed to lg303-<something> would be the property of user 
+lg303, who could determine how it was to be handled, depending on the value of
+<something>.
+
+There are two possible ways in which this can be set up. The first possibility
+is the use of multiple .forward files. In this case, mail to lg303-foo, for
+example, is handled by looking for a file called .forward-foo in lg303's home
+directory. If such a file does not exist, delivery fails and the message is
+returned to its sender.
+
+The alternative approach is to pass all messages through a single .forward
+file, which must be a filter file so that it can distinguish between the
+different cases by referencing the variables $local_part_prefix or
+$local_part_suffix, as in the final example in section 3.33 below.
+
+It is possible to configure Exim to support both schemes at once. In this case,
+a specific .forward-foo file is first sought; if it is not found, the basic
+.forward file is used.
+
+The personal test (see section 3.27) includes prefixes and suffixes in its
+checking.
+
+
+3.32 Ignoring delivery errors
+-----------------------------
+
+As was explained above, filtering just sets up addresses for delivery - no
+deliveries are actually done while a filter file is active. If any of the
+generated addresses subsequently suffers a delivery failure, an error message
+is generated in the normal way. However, if a filter command that sets up a
+delivery is preceded by the word "noerror", errors for that delivery, and any
+deliveries consequent on it (that is, from alias, forwarding, or filter files
+it invokes) are ignored.
+
+
+3.33 Examples of Exim filter commands
+-------------------------------------
+
+Simple forwarding:
+
+# Exim filter
+deliver baggins@rivendell.middle-earth.example
+
+Vacation handling using traditional means, assuming that the .vacation.msg and
+other files have been set up in your home directory:
+
+# Exim filter
+unseen pipe "/usr/ucb/vacation \"$local_part\""
+
+Vacation handling inside Exim, having first created a file called .vacation.msg
+in your home directory:
+
+# Exim filter
+if personal then vacation endif
+
+File some messages by subject:
+
+# Exim filter
+if $header_subject: contains "empire" or
+$header_subject: contains "foundation"
+then
+save $home/mail/f+e
+endif
+
+Save all non-urgent messages by weekday:
+
+# Exim filter
+if $header_subject: does not contain "urgent" and
+$tod_full matches "^(...),"
+then
+save $home/mail/$1
+endif
+
+Throw away all mail from one site, except from postmaster:
+
+# Exim filter
+if $reply_address contains "@spam.site.example" and
+$reply_address does not contain "postmaster@"
+then
+seen finish
+endif
+
+Handle multiple personal mailboxes:
+
+# Exim filter
+if $local_part_suffix is "-foo"
+then
+save $home/mail/foo
+elif $local_part_suffix is "-bar"
+then
+save $home/mail/bar
+endif
+
diff --git a/doc/spec.txt b/doc/spec.txt
new file mode 100644 (file)
index 0000000..e400286
--- /dev/null
@@ -0,0 +1,34896 @@
+Specification of the Exim Mail Transfer Agent
+
+Exim Maintainers
+
+Copyright (c) 2014 University of Cambridge
+
++-----------------------------------------------------------------------------+
++-------------------------------------+--------------------------------+------+
+|Revision 4.84                        |11 Aug 2014                     |EM    |
++-------------------------------------+--------------------------------+------+
+-------------------------------------------------------------------------------
+
+TABLE OF CONTENTS
+
+1. Introduction
+
+    1.1. Exim documentation
+    1.2. FTP and web sites
+    1.3. Mailing lists
+    1.4. Exim training
+    1.5. Bug reports
+    1.6. Where to find the Exim distribution
+    1.7. Limitations
+    1.8. Run time configuration
+    1.9. Calling interface
+    1.10. Terminology
+
+2. Incorporated code
+3. How Exim receives and delivers mail
+
+    3.1. Overall philosophy
+    3.2. Policy control
+    3.3. User filters
+    3.4. Message identification
+    3.5. Receiving mail
+    3.6. Handling an incoming message
+    3.7. Life of a message
+    3.8. Processing an address for delivery
+    3.9. Processing an address for verification
+    3.10. Running an individual router
+    3.11. Duplicate addresses
+    3.12. Router preconditions
+    3.13. Delivery in detail
+    3.14. Retry mechanism
+    3.15. Temporary delivery failure
+    3.16. Permanent delivery failure
+    3.17. Failures to deliver bounce messages
+
+4. Building and installing Exim
+
+    4.1. Unpacking
+    4.2. Multiple machine architectures and operating systems
+    4.3. PCRE library
+    4.4. DBM libraries
+    4.5. Pre-building configuration
+    4.6. Support for iconv()
+    4.7. Including TLS/SSL encryption support
+    4.8. Use of tcpwrappers
+    4.9. Including support for IPv6
+    4.10. Dynamically loaded lookup module support
+    4.11. The building process
+    4.12. Output from "make"
+    4.13. Overriding build-time options for Exim
+    4.14. OS-specific header files
+    4.15. Overriding build-time options for the monitor
+    4.16. Installing Exim binaries and scripts
+    4.17. Installing info documentation
+    4.18. Setting up the spool directory
+    4.19. Testing
+    4.20. Replacing another MTA with Exim
+    4.21. Upgrading Exim
+    4.22. Stopping the Exim daemon on Solaris
+
+5. The Exim command line
+
+    5.1. Setting options by program name
+    5.2. Trusted and admin users
+    5.3. Command line options
+
+6. The Exim run time configuration file
+
+    6.1. Using a different configuration file
+    6.2. Configuration file format
+    6.3. File inclusions in the configuration file
+    6.4. Macros in the configuration file
+    6.5. Macro substitution
+    6.6. Redefining macros
+    6.7. Overriding macro values
+    6.8. Example of macro usage
+    6.9. Conditional skips in the configuration file
+    6.10. Common option syntax
+    6.11. Boolean options
+    6.12. Integer values
+    6.13. Octal integer values
+    6.14. Fixed point numbers
+    6.15. Time intervals
+    6.16. String values
+    6.17. Expanded strings
+    6.18. User and group names
+    6.19. List construction
+    6.20. Changing list separators
+    6.21. Empty items in lists
+    6.22. Format of driver configurations
+
+7. The default configuration file
+
+    7.1. Main configuration settings
+    7.2. ACL configuration
+    7.3. Router configuration
+    7.4. Transport configuration
+    7.5. Default retry rule
+    7.6. Rewriting configuration
+    7.7. Authenticators configuration
+
+8. Regular expressions
+9. File and database lookups
+
+    9.1. Examples of different lookup syntax
+    9.2. Lookup types
+    9.3. Single-key lookup types
+    9.4. Query-style lookup types
+    9.5. Temporary errors in lookups
+    9.6. Default values in single-key lookups
+    9.7. Partial matching in single-key lookups
+    9.8. Lookup caching
+    9.9. Quoting lookup data
+    9.10. More about dnsdb
+    9.11. Pseudo dnsdb record types
+    9.12. Multiple dnsdb lookups
+    9.13. More about LDAP
+    9.14. Format of LDAP queries
+    9.15. LDAP quoting
+    9.16. LDAP connections
+    9.17. LDAP authentication and control information
+    9.18. Format of data returned by LDAP
+    9.19. More about NIS+
+    9.20. SQL lookups
+    9.21. More about MySQL, PostgreSQL, Oracle, and InterBase
+    9.22. Specifying the server in the query
+    9.23. Special MySQL features
+    9.24. Special PostgreSQL features
+    9.25. More about SQLite
+
+10. Domain, host, address, and local part lists
+
+    10.1. Expansion of lists
+    10.2. Negated items in lists
+    10.3. File names in lists
+    10.4. An lsearch file is not an out-of-line list
+    10.5. Named lists
+    10.6. Named lists compared with macros
+    10.7. Named list caching
+    10.8. Domain lists
+    10.9. Host lists
+    10.10. Special host list patterns
+    10.11. Host list patterns that match by IP address
+    10.12. Host list patterns for single-key lookups by host address
+    10.13. Host list patterns that match by host name
+    10.14. Behaviour when an IP address or name cannot be found
+    10.15. Mixing wildcarded host names and addresses in host lists
+    10.16. Temporary DNS errors when looking up host information
+    10.17. Host list patterns for single-key lookups by host name
+    10.18. Host list patterns for query-style lookups
+    10.19. Address lists
+    10.20. Case of letters in address lists
+    10.21. Local part lists
+
+11. String expansions
+
+    11.1. Literal text in expanded strings
+    11.2. Character escape sequences in expanded strings
+    11.3. Testing string expansions
+    11.4. Forced expansion failure
+    11.5. Expansion items
+    11.6. Expansion operators
+    11.7. Expansion conditions
+    11.8. Combining expansion conditions
+    11.9. Expansion variables
+
+12. Embedded Perl
+
+    12.1. Setting up so Perl can be used
+    12.2. Calling Perl subroutines
+    12.3. Calling Exim functions from Perl
+    12.4. Use of standard output and error by Perl
+
+13. Starting the daemon and the use of network interfaces
+
+    13.1. Starting a listening daemon
+    13.2. Special IP listening addresses
+    13.3. Overriding local_interfaces and daemon_smtp_ports
+    13.4. Support for the obsolete SSMTP (or SMTPS) protocol
+    13.5. IPv6 address scopes
+    13.6. Disabling IPv6
+    13.7. Examples of starting a listening daemon
+    13.8. Recognizing the local host
+    13.9. Delivering to a remote host
+
+14. Main configuration
+
+    14.1. Miscellaneous
+    14.2. Exim parameters
+    14.3. Privilege controls
+    14.4. Logging
+    14.5. Frozen messages
+    14.6. Data lookups
+    14.7. Message ids
+    14.8. Embedded Perl Startup
+    14.9. Daemon
+    14.10. Resource control
+    14.11. Policy controls
+    14.12. Callout cache
+    14.13. TLS
+    14.14. Local user handling
+    14.15. All incoming messages (SMTP and non-SMTP)
+    14.16. Non-SMTP incoming messages
+    14.17. Incoming SMTP messages
+    14.18. SMTP extensions
+    14.19. Processing messages
+    14.20. System filter
+    14.21. Routing and delivery
+    14.22. Bounce and warning messages
+    14.23. Alphabetical list of main options
+
+15. Generic options for routers
+16. The accept router
+17. The dnslookup router
+
+    17.1. Problems with DNS lookups
+    17.2. Declining addresses by dnslookup
+    17.3. Private options for dnslookup
+    17.4. Effect of qualify_single and search_parents
+
+18. The ipliteral router
+19. The iplookup router
+20. The manualroute router
+
+    20.1. Private options for manualroute
+    20.2. Routing rules in route_list
+    20.3. Routing rules in route_data
+    20.4. Format of the list of hosts
+    20.5. Format of one host item
+    20.6. How the list of hosts is used
+    20.7. How the options are used
+    20.8. Manualroute examples
+
+21. The queryprogram router
+22. The redirect router
+
+    22.1. Redirection data
+    22.2. Forward files and address verification
+    22.3. Interpreting redirection data
+    22.4. Items in a non-filter redirection list
+    22.5. Redirecting to a local mailbox
+    22.6. Special items in redirection lists
+    22.7. Duplicate addresses
+    22.8. Repeated redirection expansion
+    22.9. Errors in redirection lists
+    22.10. Private options for the redirect router
+
+23. Environment for running local transports
+
+    23.1. Concurrent deliveries
+    23.2. Uids and gids
+    23.3. Current and home directories
+    23.4. Expansion variables derived from the address
+
+24. Generic options for transports
+25. Address batching in local transports
+26. The appendfile transport
+
+    26.1. The file and directory options
+    26.2. Private options for appendfile
+    26.3. Operational details for appending
+    26.4. Operational details for delivery to a new file
+    26.5. Maildir delivery
+    26.6. Using tags to record message sizes
+    26.7. Using a maildirsize file
+    26.8. Mailstore delivery
+    26.9. Non-special new file delivery
+
+27. The autoreply transport
+
+    27.1. Private options for autoreply
+
+28. The lmtp transport
+29. The pipe transport
+
+    29.1. Concurrent delivery
+    29.2. Returned status and data
+    29.3. How the command is run
+    29.4. Environment variables
+    29.5. Private options for pipe
+    29.6. Using an external local delivery agent
+
+30. The smtp transport
+
+    30.1. Multiple messages on a single connection
+    30.2. Use of the $host and $host_address variables
+    30.3. Use of $tls_cipher and $tls_peerdn
+    30.4. Private options for smtp
+    30.5. How the limits for the number of hosts to try are used
+
+31. Address rewriting
+
+    31.1. Explicitly configured address rewriting
+    31.2. When does rewriting happen?
+    31.3. Testing the rewriting rules that apply on input
+    31.4. Rewriting rules
+    31.5. Rewriting patterns
+    31.6. Rewriting replacements
+    31.7. Rewriting flags
+    31.8. Flags specifying which headers and envelope addresses to rewrite
+    31.9. The SMTP-time rewriting flag
+    31.10. Flags controlling the rewriting process
+    31.11. Rewriting examples
+
+32. Retry configuration
+
+    32.1. Changing retry rules
+    32.2. Format of retry rules
+    32.3. Choosing which retry rule to use for address errors
+    32.4. Choosing which retry rule to use for host and message errors
+    32.5. Retry rules for specific errors
+    32.6. Retry rules for specified senders
+    32.7. Retry parameters
+    32.8. Retry rule examples
+    32.9. Timeout of retry data
+    32.10. Long-term failures
+    32.11. Deliveries that work intermittently
+
+33. SMTP authentication
+
+    33.1. Generic options for authenticators
+    33.2. The AUTH parameter on MAIL commands
+    33.3. Authentication on an Exim server
+    33.4. Testing server authentication
+    33.5. Authentication by an Exim client
+
+34. The plaintext authenticator
+
+    34.1. Plaintext options
+    34.2. Using plaintext in a server
+    34.3. The PLAIN authentication mechanism
+    34.4. The LOGIN authentication mechanism
+    34.5. Support for different kinds of authentication
+    34.6. Using plaintext in a client
+
+35. The cram_md5 authenticator
+
+    35.1. Using cram_md5 as a server
+    35.2. Using cram_md5 as a client
+
+36. The cyrus_sasl authenticator
+
+    36.1. Using cyrus_sasl as a server
+
+37. The dovecot authenticator
+38. The gsasl authenticator
+
+    38.1. gsasl auth variables
+
+39. The heimdal_gssapi authenticator
+
+    39.1. heimdal_gssapi auth variables
+
+40. The spa authenticator
+
+    40.1. Using spa as a server
+    40.2. Using spa as a client
+
+41. Encrypted SMTP connections using TLS/SSL
+
+    41.1. Support for the legacy "ssmtp" (aka "smtps") protocol
+    41.2. OpenSSL vs GnuTLS
+    41.3. GnuTLS parameter computation
+    41.4. Requiring specific ciphers in OpenSSL
+    41.5. Requiring specific ciphers or other parameters in GnuTLS
+    41.6. Configuring an Exim server to use TLS
+    41.7. Requesting and verifying client certificates
+    41.8. Revoked certificates
+    41.9. Configuring an Exim client to use TLS
+    41.10. Use of TLS Server Name Indication
+    41.11. Multiple messages on the same encrypted TCP/IP connection
+    41.12. Certificates and all that
+    41.13. Certificate chains
+    41.14. Self-signed certificates
+
+42. Access control lists
+
+    42.1. Testing ACLs
+    42.2. Specifying when ACLs are used
+    42.3. The non-SMTP ACLs
+    42.4. The SMTP connect ACL
+    42.5. The EHLO/HELO ACL
+    42.6. The DATA ACLs
+    42.7. The SMTP DKIM ACL
+    42.8. The SMTP MIME ACL
+    42.9. The SMTP PRDR ACL
+    42.10. The QUIT ACL
+    42.11. The not-QUIT ACL
+    42.12. Finding an ACL to use
+    42.13. ACL return codes
+    42.14. Unset ACL options
+    42.15. Data for message ACLs
+    42.16. Data for non-message ACLs
+    42.17. Format of an ACL
+    42.18. ACL verbs
+    42.19. ACL variables
+    42.20. Condition and modifier processing
+    42.21. ACL modifiers
+    42.22. Use of the control modifier
+    42.23. Summary of message fixup control
+    42.24. Adding header lines in ACLs
+    42.25. Removing header lines in ACLs
+    42.26. ACL conditions
+    42.27. Using DNS lists
+    42.28. Specifying the IP address for a DNS list lookup
+    42.29. DNS lists keyed on domain names
+    42.30. Multiple explicit keys for a DNS list
+    42.31. Data returned by DNS lists
+    42.32. Variables set from DNS lists
+    42.33. Additional matching conditions for DNS lists
+    42.34. Negated DNS matching conditions
+    42.35. Handling multiple DNS records from a DNS list
+    42.36. Detailed information from merged DNS lists
+    42.37. DNS lists and IPv6
+    42.38. Rate limiting incoming messages
+    42.39. Ratelimit options for what is being measured
+    42.40. Ratelimit update modes
+    42.41. Ratelimit options for handling fast clients
+    42.42. Limiting the rate of different events
+    42.43. Using rate limiting
+    42.44. Address verification
+    42.45. Callout verification
+    42.46. Additional parameters for callouts
+    42.47. Callout caching
+    42.48. Sender address verification reporting
+    42.49. Redirection while verifying
+    42.50. Client SMTP authorization (CSA)
+    42.51. Bounce address tag validation
+    42.52. Using an ACL to control relaying
+    42.53. Checking a relay configuration
+
+43. Content scanning at ACL time
+
+    43.1. Scanning for viruses
+    43.2. Scanning with SpamAssassin
+    43.3. Calling SpamAssassin from an Exim ACL
+    43.4. Scanning MIME parts
+    43.5. Scanning with regular expressions
+    43.6. The demime condition
+
+44. Adding a local scan function to Exim
+
+    44.1. Building Exim to use a local scan function
+    44.2. API for local_scan()
+    44.3. Configuration options for local_scan()
+    44.4. Available Exim variables
+    44.5. Structure of header lines
+    44.6. Structure of recipient items
+    44.7. Available Exim functions
+    44.8. More about Exim's memory handling
+
+45. System-wide message filtering
+
+    45.1. Specifying a system filter
+    45.2. Testing a system filter
+    45.3. Contents of a system filter
+    45.4. Additional variable for system filters
+    45.5. Defer, freeze, and fail commands for system filters
+    45.6. Adding and removing headers in a system filter
+    45.7. Setting an errors address in a system filter
+    45.8. Per-address filtering
+
+46. Message processing
+
+    46.1. Submission mode for non-local messages
+    46.2. Line endings
+    46.3. Unqualified addresses
+    46.4. The UUCP From line
+    46.5. Resent- header lines
+    46.6. The Auto-Submitted: header line
+    46.7. The Bcc: header line
+    46.8. The Date: header line
+    46.9. The Delivery-date: header line
+    46.10. The Envelope-to: header line
+    46.11. The From: header line
+    46.12. The Message-ID: header line
+    46.13. The Received: header line
+    46.14. The References: header line
+    46.15. The Return-path: header line
+    46.16. The Sender: header line
+    46.17. Adding and removing header lines in routers and transports
+    46.18. Constructed addresses
+    46.19. Case of local parts
+    46.20. Dots in local parts
+    46.21. Rewriting addresses
+
+47. SMTP processing
+
+    47.1. Outgoing SMTP and LMTP over TCP/IP
+    47.2. Errors in outgoing SMTP
+    47.3. Incoming SMTP messages over TCP/IP
+    47.4. Unrecognized SMTP commands
+    47.5. Syntax and protocol errors in SMTP commands
+    47.6. Use of non-mail SMTP commands
+    47.7. The VRFY and EXPN commands
+    47.8. The ETRN command
+    47.9. Incoming local SMTP
+    47.10. Outgoing batched SMTP
+    47.11. Incoming batched SMTP
+
+48. Customizing bounce and warning messages
+
+    48.1. Customizing bounce messages
+    48.2. Customizing warning messages
+
+49. Some common configuration settings
+
+    49.1. Sending mail to a smart host
+    49.2. Using Exim to handle mailing lists
+    49.3. Syntax errors in mailing lists
+    49.4. Re-expansion of mailing lists
+    49.5. Closed mailing lists
+    49.6. Variable Envelope Return Paths (VERP)
+    49.7. Virtual domains
+    49.8. Multiple user mailboxes
+    49.9. Simplified vacation processing
+    49.10. Taking copies of mail
+    49.11. Intermittently connected hosts
+    49.12. Exim on the upstream server host
+    49.13. Exim on the intermittently connected client host
+
+50. Using Exim as a non-queueing client
+51. Log files
+
+    51.1. Where the logs are written
+    51.2. Logging to local files that are periodically "cycled"
+    51.3. Datestamped log files
+    51.4. Logging to syslog
+    51.5. Log line flags
+    51.6. Logging message reception
+    51.7. Logging deliveries
+    51.8. Discarded deliveries
+    51.9. Deferred deliveries
+    51.10. Delivery failures
+    51.11. Fake deliveries
+    51.12. Completion
+    51.13. Summary of Fields in Log Lines
+    51.14. Other log entries
+    51.15. Reducing or increasing what is logged
+    51.16. Message log
+
+52. Exim utilities
+
+    52.1. Finding out what Exim processes are doing (exiwhat)
+    52.2. Selective queue listing (exiqgrep)
+    52.3. Summarizing the queue (exiqsumm)
+    52.4. Extracting specific information from the log (exigrep)
+    52.5. Selecting messages by various criteria (exipick)
+    52.6. Cycling log files (exicyclog)
+    52.7. Mail statistics (eximstats)
+    52.8. Checking access policy (exim_checkaccess)
+    52.9. Making DBM files (exim_dbmbuild)
+    52.10. Finding individual retry times (exinext)
+    52.11. Hints database maintenance
+    52.12. exim_dumpdb
+    52.13. exim_tidydb
+    52.14. exim_fixdb
+    52.15. Mailbox maintenance (exim_lock)
+
+53. The Exim monitor
+
+    53.1. Running the monitor
+    53.2. The stripcharts
+    53.3. Main action buttons
+    53.4. The log display
+    53.5. The queue display
+    53.6. The queue menu
+
+54. Security considerations
+
+    54.1. Building a more "hardened" Exim
+    54.2. Root privilege
+    54.3. Running Exim without privilege
+    54.4. Delivering to local files
+    54.5. Running local commands
+    54.6. Trust in configuration data
+    54.7. IPv4 source routing
+    54.8. The VRFY, EXPN, and ETRN commands in SMTP
+    54.9. Privileged users
+    54.10. Spool files
+    54.11. Use of argv[0]
+    54.12. Use of %f formatting
+    54.13. Embedded Exim path
+    54.14. Dynamic module directory
+    54.15. Use of sprintf()
+    54.16. Use of debug_printf() and log_write()
+    54.17. Use of strcat() and strcpy()
+
+55. Format of spool files
+
+    55.1. Format of the -H file
+
+56. Support for DKIM (DomainKeys Identified Mail)
+
+    56.1. Signing outgoing messages
+    56.2. Verifying DKIM signatures in incoming mail
+
+57. Adding new drivers or lookup types
+
+
+
+===============================================================================
+1. INTRODUCTION
+
+Exim is a mail transfer agent (MTA) for hosts that are running Unix or
+Unix-like operating systems. It was designed on the assumption that it would be
+run on hosts that are permanently connected to the Internet. However, it can be
+used on intermittently connected hosts with suitable configuration adjustments.
+
+Configuration files currently exist for the following operating systems: AIX,
+BSD/OS (aka BSDI), Darwin (Mac OS X), DGUX, Dragonfly, FreeBSD, GNU/Hurd, GNU/
+Linux, HI-OSF (Hitachi), HI-UX, HP-UX, IRIX, MIPS RISCOS, NetBSD, OpenBSD,
+OpenUNIX, QNX, SCO, SCO SVR4.2 (aka UNIX-SV), Solaris (aka SunOS5), SunOS4,
+Tru64-Unix (formerly Digital UNIX, formerly DEC-OSF1), Ultrix, and Unixware.
+Some of these operating systems are no longer current and cannot easily be
+tested, so the configuration files may no longer work in practice.
+
+There are also configuration files for compiling Exim in the Cygwin environment
+that can be installed on systems running Windows. However, this document does
+not contain any information about running Exim in the Cygwin environment.
+
+The terms and conditions for the use and distribution of Exim are contained in
+the file NOTICE. Exim is distributed under the terms of the GNU General Public
+Licence, a copy of which may be found in the file LICENCE.
+
+The use, supply or promotion of Exim for the purpose of sending bulk,
+unsolicited electronic mail is incompatible with the basic aims of the program,
+which revolve around the free provision of a service that enhances the quality
+of personal communications. The author of Exim regards indiscriminate
+mass-mailing as an antisocial, irresponsible abuse of the Internet.
+
+Exim owes a great deal to Smail 3 and its author, Ron Karr. Without the
+experience of running and working on the Smail 3 code, I could never have
+contemplated starting to write a new MTA. Many of the ideas and user interfaces
+were originally taken from Smail 3, though the actual code of Exim is entirely
+new, and has developed far beyond the initial concept.
+
+Many people, both in Cambridge and around the world, have contributed to the
+development and the testing of Exim, and to porting it to various operating
+systems. I am grateful to them all. The distribution now contains a file called
+ACKNOWLEDGMENTS, in which I have started recording the names of contributors.
+
+
+1.1 Exim documentation
+----------------------
+
+This edition of the Exim specification applies to version 4.84 of Exim.
+Substantive changes from the 4.83 edition are marked in some renditions of the
+document; this paragraph is so marked if the rendition is capable of showing a
+change indicator.
+
+This document is very much a reference manual; it is not a tutorial. The reader
+is expected to have some familiarity with the SMTP mail transfer protocol and
+with general Unix system administration. Although there are some discussions
+and examples in places, the information is mostly organized in a way that makes
+it easy to look up, rather than in a natural order for sequential reading.
+Furthermore, the manual aims to cover every aspect of Exim in detail, including
+a number of rarely-used, special-purpose features that are unlikely to be of
+very wide interest.
+
+An "easier" discussion of Exim which provides more in-depth explanatory,
+introductory, and tutorial material can be found in a book entitled The Exim
+SMTP Mail Server (second edition, 2007), published by UIT Cambridge (http://
+www.uit.co.uk/exim-book/).
+
+This book also contains a chapter that gives a general introduction to SMTP and
+Internet mail. Inevitably, however, the book is unlikely to be fully up-to-date
+with the latest release of Exim. (Note that the earlier book about Exim,
+published by O'Reilly, covers Exim 3, and many things have changed in Exim 4.)
+
+If you are using a Debian distribution of Exim, you will find information about
+Debian-specific features in the file /usr/share/doc/exim4-base/README.Debian.
+The command man update-exim.conf is another source of Debian-specific
+information.
+
+As the program develops, there may be features in newer versions that have not
+yet made it into this document, which is updated only when the most significant
+digit of the fractional part of the version number changes. Specifications of
+new features that are not yet in this manual are placed in the file doc/
+NewStuff in the Exim distribution.
+
+Some features may be classified as "experimental". These may change
+incompatibly while they are developing, or even be withdrawn. For this reason,
+they are not documented in this manual. Information about experimental features
+can be found in the file doc/experimental.txt.
+
+All changes to the program (whether new features, bug fixes, or other kinds of
+change) are noted briefly in the file called doc/ChangeLog.
+
+This specification itself is available as an ASCII file in doc/spec.txt so that
+it can easily be searched with a text editor. Other files in the doc directory
+are:
+
+OptionLists.txt  list of all options in alphabetical order
+dbm.discuss.txt  discussion about DBM libraries
+exim.8           a man page of Exim's command line options
+experimental.txt documentation of experimental features
+filter.txt       specification of the filter language
+Exim3.upgrade    upgrade notes from release 2 to release 3
+Exim4.upgrade    upgrade notes from release 3 to release 4
+
+The main specification and the specification of the filtering language are also
+available in other formats (HTML, PostScript, PDF, and Texinfo). Section 1.6
+below tells you how to get hold of these.
+
+
+1.2 FTP and web sites
+---------------------
+
+The primary site for Exim source distributions is currently the University of
+Cambridge's FTP site, whose contents are described in Where to find the Exim
+distribution below. In addition, there is a web site and an FTP site at 
+exim.org. These are now also hosted at the University of Cambridge. The 
+exim.org site was previously hosted for a number of years by Energis Squared,
+formerly Planet Online Ltd, whose support I gratefully acknowledge.
+
+As well as Exim distribution tar files, the Exim web site contains a number of
+differently formatted versions of the documentation. A recent addition to the
+online information is the Exim wiki (http://wiki.exim.org), which contains what
+used to be a separate FAQ, as well as various other examples, tips, and
+know-how that have been contributed by Exim users.
+
+An Exim Bugzilla exists at http://bugs.exim.org. You can use this to report
+bugs, and also to add items to the wish list. Please search first to check that
+you are not duplicating a previous entry.
+
+
+1.3 Mailing lists
+-----------------
+
+The following Exim mailing lists exist:
+
+exim-announce@exim.org Moderated, low volume announcements list
+exim-users@exim.org    General discussion list
+exim-dev@exim.org      Discussion of bugs, enhancements, etc.
+exim-cvs@exim.org      Automated commit messages from the VCS
+
+You can subscribe to these lists, change your existing subscriptions, and view
+or search the archives via the mailing lists link on the Exim home page. If you
+are using a Debian distribution of Exim, you may wish to subscribe to the
+Debian-specific mailing list pkg-exim4-users@lists.alioth.debian.org via this
+web page:
+
+http://lists.alioth.debian.org/mailman/listinfo/pkg-exim4-users
+
+Please ask Debian-specific questions on this list and not on the general Exim
+lists.
+
+
+1.4 Exim training
+-----------------
+
+Training courses in Cambridge (UK) used to be run annually by the author of
+Exim, before he retired. At the time of writing, there are no plans to run
+further Exim courses in Cambridge. However, if that changes, relevant
+information will be posted at http://www-tus.csx.cam.ac.uk/courses/exim/.
+
+
+1.5 Bug reports
+---------------
+
+Reports of obvious bugs can be emailed to bugs@exim.org or reported via the
+Bugzilla (http://bugs.exim.org). However, if you are unsure whether some
+behaviour is a bug or not, the best thing to do is to post a message to the 
+exim-dev mailing list and have it discussed.
+
+
+1.6 Where to find the Exim distribution
+---------------------------------------
+
+The master ftp site for the Exim distribution is
+
+ftp://ftp.csx.cam.ac.uk/pub/software/email/exim
+
+This is mirrored by
+
+ftp://ftp.exim.org/pub/exim
+
+The file references that follow are relative to the exim directories at these
+sites. There are now quite a number of independent mirror sites around the
+world. Those that I know about are listed in the file called Mirrors.
+
+Within the exim directory there are subdirectories called exim3 (for previous
+Exim 3 distributions), exim4 (for the latest Exim 4 distributions), and Testing
+for testing versions. In the exim4 subdirectory, the current release can always
+be found in files called
+
+exim-n.nn.tar.gz
+exim-n.nn.tar.bz2
+
+where n.nn is the highest such version number in the directory. The two files
+contain identical data; the only difference is the type of compression. The
+.bz2 file is usually a lot smaller than the .gz file.
+
+The distributions will be PGP signed by an individual key of the Release
+Coordinator. This key will have a uid containing an email address in the 
+exim.org domain and will have signatures from other people, including other
+Exim maintainers. We expect that the key will be in the "strong set" of PGP
+keys. There should be a trust path to that key from Nigel Metheringham's PGP
+key, a version of which can be found in the release directory in the file
+nigel-pubkey.asc. All keys used will be available in public keyserver pools,
+such as pool.sks-keyservers.net.
+
+At time of last update, releases were being made by Phil Pennock and signed
+with key 0x403043153903637F, although that key is expected to be replaced in
+2013. A trust path from Nigel's key to Phil's can be observed at https://
+www.security.spodhuis.org/exim-trustpath.
+
+Releases have also been authorized to be performed by Todd Lyons who signs with
+key 0xC4F4F94804D29EBA. A direct trust path exists between previous RE Phil
+Pennock and Todd Lyons through a common associate.
+
+The signatures for the tar bundles are in:
+
+exim-n.nn.tar.gz.asc
+exim-n.nn.tar.bz2.asc
+
+For each released version, the log of changes is made separately available in a
+separate file in the directory ChangeLogs so that it is possible to find out
+what has changed without having to download the entire distribution.
+
+The main distribution contains ASCII versions of this specification and other
+documentation; other formats of the documents are available in separate files
+inside the exim4 directory of the FTP site:
+
+exim-html-n.nn.tar.gz
+exim-pdf-n.nn.tar.gz
+exim-postscript-n.nn.tar.gz
+exim-texinfo-n.nn.tar.gz
+
+These tar files contain only the doc directory, not the complete distribution,
+and are also available in .bz2 as well as .gz forms.
+
+
+1.7 Limitations
+---------------
+
+  * Exim is designed for use as an Internet MTA, and therefore handles
+    addresses in RFC 2822 domain format only. It cannot handle UUCP "bang
+    paths", though simple two-component bang paths can be converted by a
+    straightforward rewriting configuration. This restriction does not prevent
+    Exim from being interfaced to UUCP as a transport mechanism, provided that
+    domain addresses are used.
+
+  * Exim insists that every address it handles has a domain attached. For
+    incoming local messages, domainless addresses are automatically qualified
+    with a configured domain value. Configuration options specify from which
+    remote systems unqualified addresses are acceptable. These are then
+    qualified on arrival.
+
+  * The only external transport mechanisms that are currently implemented are
+    SMTP and LMTP over a TCP/IP network (including support for IPv6). However,
+    a pipe transport is available, and there are facilities for writing
+    messages to files and pipes, optionally in batched SMTP format; these
+    facilities can be used to send messages to other transport mechanisms such
+    as UUCP, provided they can handle domain-style addresses. Batched SMTP
+    input is also catered for.
+
+  * Exim is not designed for storing mail for dial-in hosts. When the volumes
+    of such mail are large, it is better to get the messages "delivered" into
+    files (that is, off Exim's queue) and subsequently passed on to the dial-in
+    hosts by other means.
+
+  * Although Exim does have basic facilities for scanning incoming messages,
+    these are not comprehensive enough to do full virus or spam scanning. Such
+    operations are best carried out using additional specialized software
+    packages. If you compile Exim with the content-scanning extension,
+    straightforward interfaces to a number of common scanners are provided.
+
+
+1.8 Run time configuration
+--------------------------
+
+Exim's run time configuration is held in a single text file that is divided
+into a number of sections. The entries in this file consist of keywords and
+values, in the style of Smail 3 configuration files. A default configuration
+file which is suitable for simple online installations is provided in the
+distribution, and is described in chapter 7 below.
+
+
+1.9 Calling interface
+---------------------
+
+Like many MTAs, Exim has adopted the Sendmail command line interface so that it
+can be a straight replacement for /usr/lib/sendmail or /usr/sbin/sendmail when
+sending mail, but you do not need to know anything about Sendmail in order to
+run Exim. For actions other than sending messages, Sendmail-compatible options
+also exist, but those that produce output (for example, -bp, which lists the
+messages on the queue) do so in Exim's own format. There are also some
+additional options that are compatible with Smail 3, and some further options
+that are new to Exim. Chapter 5 documents all Exim's command line options. This
+information is automatically made into the man page that forms part of the Exim
+distribution.
+
+Control of messages on the queue can be done via certain privileged command
+line options. There is also an optional monitor program called eximon, which
+displays current information in an X window, and which contains a menu
+interface to Exim's command line administration options.
+
+
+1.10 Terminology
+----------------
+
+The body of a message is the actual data that the sender wants to transmit. It
+is the last part of a message, and is separated from the header (see below) by
+a blank line.
+
+When a message cannot be delivered, it is normally returned to the sender in a
+delivery failure message or a "non-delivery report" (NDR). The term bounce is
+commonly used for this action, and the error reports are often called bounce
+messages. This is a convenient shorthand for "delivery failure error report".
+Such messages have an empty sender address in the message's envelope (see
+below) to ensure that they cannot themselves give rise to further bounce
+messages.
+
+The term default appears frequently in this manual. It is used to qualify a
+value which is used in the absence of any setting in the configuration. It may
+also qualify an action which is taken unless a configuration setting specifies
+otherwise.
+
+The term defer is used when the delivery of a message to a specific destination
+cannot immediately take place for some reason (a remote host may be down, or a
+user's local mailbox may be full). Such deliveries are deferred until a later
+time.
+
+The word domain is sometimes used to mean all but the first component of a
+host's name. It is not used in that sense here, where it normally refers to the
+part of an email address following the @ sign.
+
+A message in transit has an associated envelope, as well as a header and a
+body. The envelope contains a sender address (to which bounce messages should
+be delivered), and any number of recipient addresses. References to the sender
+or the recipients of a message usually mean the addresses in the envelope. An
+MTA uses these addresses for delivery, and for returning bounce messages, not
+the addresses that appear in the header lines.
+
+The header of a message is the first part of a message's text, consisting of a
+number of lines, each of which has a name such as From:, To:, Subject:, etc.
+Long header lines can be split over several text lines by indenting the
+continuations. The header is separated from the body by a blank line.
+
+The term local part, which is taken from RFC 2822, is used to refer to that
+part of an email address that precedes the @ sign. The part that follows the @
+sign is called the domain or mail domain.
+
+The terms local delivery and remote delivery are used to distinguish delivery
+to a file or a pipe on the local host from delivery by SMTP over TCP/IP to
+another host. As far as Exim is concerned, all hosts other than the host it is
+running on are remote.
+
+Return path is another name that is used for the sender address in a message's
+envelope.
+
+The term queue is used to refer to the set of messages awaiting delivery,
+because this term is in widespread use in the context of MTAs. However, in
+Exim's case the reality is more like a pool than a queue, because there is
+normally no ordering of waiting messages.
+
+The term queue runner is used to describe a process that scans the queue and
+attempts to deliver those messages whose retry times have come. This term is
+used by other MTAs, and also relates to the command runq, but in Exim the
+waiting messages are normally processed in an unpredictable order.
+
+The term spool directory is used for a directory in which Exim keeps the
+messages on its queue - that is, those that it is in the process of delivering.
+This should not be confused with the directory in which local mailboxes are
+stored, which is called a "spool directory" by some people. In the Exim
+documentation, "spool" is always used in the first sense.
+
+
+
+===============================================================================
+2. INCORPORATED CODE
+
+A number of pieces of external code are included in the Exim distribution.
+
+  * Regular expressions are supported in the main Exim program and in the Exim
+    monitor using the freely-distributable PCRE library, copyright (c)
+    University of Cambridge. The source to PCRE is no longer shipped with Exim,
+    so you will need to use the version of PCRE shipped with your system, or
+    obtain and install the full version of the library from ftp://
+    ftp.csx.cam.ac.uk/pub/software/programming/pcre.
+
+  * Support for the cdb (Constant DataBase) lookup method is provided by code
+    contributed by Nigel Metheringham of (at the time he contributed it) Planet
+    Online Ltd. The implementation is completely contained within the code of
+    Exim. It does not link against an external cdb library. The code contains
+    the following statements:
+
+        Copyright (c) 1998 Nigel Metheringham, Planet Online Ltd
+
+        This program is free software; you can redistribute it and/or modify it
+        under the terms of the GNU General Public License as published by the
+        Free Software Foundation; either version 2 of the License, or (at your
+        option) any later version. This code implements Dan Bernstein's
+        Constant DataBase (cdb) spec. Information, the spec and sample code for
+        cdb can be obtained from http://www.pobox.com/~djb/cdb.html. This
+        implementation borrows some code from Dan Bernstein's implementation
+        (which has no license restrictions applied to it).
+
+  * Client support for Microsoft's Secure Password Authentication is provided
+    by code contributed by Marc Prud'hommeaux. Server support was contributed
+    by Tom Kistner. This includes code taken from the Samba project, which is
+    released under the Gnu GPL.
+
+  * Support for calling the Cyrus pwcheck and saslauthd daemons is provided by
+    code taken from the Cyrus-SASL library and adapted by Alexander S.
+    Sabourenkov. The permission notice appears below, in accordance with the
+    conditions expressed therein.
+
+        Copyright (c) 2001 Carnegie Mellon University. All rights reserved.
+
+        Redistribution and use in source and binary forms, with or without
+        modification, are permitted provided that the following conditions are
+        met:
+
+         1. Redistributions of source code must retain the above copyright
+            notice, this list of conditions and the following disclaimer.
+
+         2. Redistributions in binary form must reproduce the above copyright
+            notice, this list of conditions and the following disclaimer in the
+            documentation and/or other materials provided with the
+            distribution.
+
+         3. The name "Carnegie Mellon University" must not be used to endorse
+            or promote products derived from this software without prior
+            written permission. For permission or any other legal details,
+            please contact
+
+                          Office of Technology Transfer
+                          Carnegie Mellon University
+                          5000 Forbes Avenue
+                          Pittsburgh, PA  15213-3890
+                          (412) 268-4387, fax: (412) 268-7395
+                          tech-transfer@andrew.cmu.edu
+
+         4. Redistributions of any form whatsoever must retain the following
+            acknowledgment:
+
+            "This product includes software developed by Computing Services at
+            Carnegie Mellon University (http://www.cmu.edu/computing/."
+
+            CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
+            THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+            AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
+            FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+            WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
+            AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
+            OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
+            SOFTWARE.
+
+  * The Exim Monitor program, which is an X-Window application, includes
+    modified versions of the Athena StripChart and TextPop widgets. This code
+    is copyright by DEC and MIT, and their permission notice appears below, in
+    accordance with the conditions expressed therein.
+
+        Copyright 1987, 1988 by Digital Equipment Corporation, Maynard,
+        Massachusetts, and the Massachusetts Institute of Technology,
+        Cambridge, Massachusetts.
+
+        All Rights Reserved
+
+        Permission to use, copy, modify, and distribute this software and its
+        documentation for any purpose and without fee is hereby granted,
+        provided that the above copyright notice appear in all copies and that
+        both that copyright notice and this permission notice appear in
+        supporting documentation, and that the names of Digital or MIT not be
+        used in advertising or publicity pertaining to distribution of the
+        software without specific, written prior permission.
+
+        DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
+        INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
+        EVENT SHALL DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR
+        CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
+        USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
+        OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+        PERFORMANCE OF THIS SOFTWARE.
+
+  * The DMARC implementation uses the OpenDMARC library which is Copyrighted by
+    The Trusted Domain Project. Portions of Exim source which use OpenDMARC
+    derived code are indicated in the respective source files. The full
+    OpenDMARC license is provided in the LICENSE.opendmarc file contained in
+    the distributed source code.
+
+  * Many people have contributed code fragments, some large, some small, that
+    were not covered by any specific licence requirements. It is assumed that
+    the contributors are happy to see their code incorporated into Exim under
+    the GPL.
+
+
+
+===============================================================================
+3. HOW EXIM RECEIVES AND DELIVERS MAIL
+
+
+3.1 Overall philosophy
+----------------------
+
+Exim is designed to work efficiently on systems that are permanently connected
+to the Internet and are handling a general mix of mail. In such circumstances,
+most messages can be delivered immediately. Consequently, Exim does not
+maintain independent queues of messages for specific domains or hosts, though
+it does try to send several messages in a single SMTP connection after a host
+has been down, and it also maintains per-host retry information.
+
+
+3.2 Policy control
+------------------
+
+Policy controls are now an important feature of MTAs that are connected to the
+Internet. Perhaps their most important job is to stop MTAs being abused as
+"open relays" by misguided individuals who send out vast amounts of unsolicited
+junk, and want to disguise its source. Exim provides flexible facilities for
+specifying policy controls on incoming mail:
+
+  * Exim 4 (unlike previous versions of Exim) implements policy controls on
+    incoming mail by means of Access Control Lists (ACLs). Each list is a
+    series of statements that may either grant or deny access. ACLs can be used
+    at several places in the SMTP dialogue while receiving a message from a
+    remote host. However, the most common places are after each RCPT command,
+    and at the very end of the message. The sysadmin can specify conditions for
+    accepting or rejecting individual recipients or the entire message,
+    respectively, at these two points (see chapter 42). Denial of access
+    results in an SMTP error code.
+
+  * An ACL is also available for locally generated, non-SMTP messages. In this
+    case, the only available actions are to accept or deny the entire message.
+
+  * When Exim is compiled with the content-scanning extension, facilities are
+    provided in the ACL mechanism for passing the message to external virus and
+    /or spam scanning software. The result of such a scan is passed back to the
+    ACL, which can then use it to decide what to do with the message.
+
+  * When a message has been received, either from a remote host or from the
+    local host, but before the final acknowledgment has been sent, a locally
+    supplied C function called local_scan() can be run to inspect the message
+    and decide whether to accept it or not (see chapter 44). If the message is
+    accepted, the list of recipients can be modified by the function.
+
+  * Using the local_scan() mechanism is another way of calling external scanner
+    software. The SA-Exim add-on package works this way. It does not require
+    Exim to be compiled with the content-scanning extension.
+
+  * After a message has been accepted, a further checking mechanism is
+    available in the form of the system filter (see chapter 45). This runs at
+    the start of every delivery process.
+
+
+3.3 User filters
+----------------
+
+In a conventional Exim configuration, users are able to run private filters by
+setting up appropriate .forward files in their home directories. See chapter 22
+(about the redirect router) for the configuration needed to support this, and
+the separate document entitled Exim's interfaces to mail filtering for user
+details. Two different kinds of filtering are available:
+
+  * Sieve filters are written in the standard filtering language that is
+    defined by RFC 3028.
+
+  * Exim filters are written in a syntax that is unique to Exim, but which is
+    more powerful than Sieve, which it pre-dates.
+
+User filters are run as part of the routing process, described below.
+
+
+3.4 Message identification
+--------------------------
+
+Every message handled by Exim is given a message id which is sixteen characters
+long. It is divided into three parts, separated by hyphens, for example
+"16VDhn-0001bo-D3". Each part is a sequence of letters and digits, normally
+encoding numbers in base 62. However, in the Darwin operating system (Mac OS X)
+and when Exim is compiled to run under Cygwin, base 36 (avoiding the use of
+lower case letters) is used instead, because the message id is used to
+construct file names, and the names of files in those systems are not always
+case-sensitive.
+
+The detail of the contents of the message id have changed as Exim has evolved.
+Earlier versions relied on the operating system not re-using a process id (pid)
+within one second. On modern operating systems, this assumption can no longer
+be made, so the algorithm had to be changed. To retain backward compatibility,
+the format of the message id was retained, which is why the following rules are
+somewhat eccentric:
+
+  * The first six characters of the message id are the time at which the
+    message started to be received, to a granularity of one second. That is,
+    this field contains the number of seconds since the start of the epoch (the
+    normal Unix way of representing the date and time of day).
+
+  * After the first hyphen, the next six characters are the id of the process
+    that received the message.
+
+  * There are two different possibilities for the final two characters:
+
+     1. If localhost_number is not set, this value is the fractional part of
+        the time of reception, normally in units of 1/2000 of a second, but for
+        systems that must use base 36 instead of base 62 (because of
+        case-insensitive file systems), the units are 1/1000 of a second.
+
+     2. If localhost_number is set, it is multiplied by 200 (100) and added to
+        the fractional part of the time, which in this case is in units of 1/
+        200 (1/100) of a second.
+
+After a message has been received, Exim waits for the clock to tick at the
+appropriate resolution before proceeding, so that if another message is
+received by the same process, or by another process with the same (re-used)
+pid, it is guaranteed that the time will be different. In most cases, the clock
+will already have ticked while the message was being received.
+
+
+3.5 Receiving mail
+------------------
+
+The only way Exim can receive mail from another host is using SMTP over TCP/IP,
+in which case the sender and recipient addresses are transferred using SMTP
+commands. However, from a locally running process (such as a user's MUA), there
+are several possibilities:
+
+  * If the process runs Exim with the -bm option, the message is read
+    non-interactively (usually via a pipe), with the recipients taken from the
+    command line, or from the body of the message if -t is also used.
+
+  * If the process runs Exim with the -bS option, the message is also read
+    non-interactively, but in this case the recipients are listed at the start
+    of the message in a series of SMTP RCPT commands, terminated by a DATA
+    command. This is so-called "batch SMTP" format, but it isn't really SMTP.
+    The SMTP commands are just another way of passing envelope addresses in a
+    non-interactive submission.
+
+  * If the process runs Exim with the -bs option, the message is read
+    interactively, using the SMTP protocol. A two-way pipe is normally used for
+    passing data between the local process and the Exim process. This is "real"
+    SMTP and is handled in the same way as SMTP over TCP/IP. For example, the
+    ACLs for SMTP commands are used for this form of submission.
+
+  * A local process may also make a TCP/IP call to the host's loopback address
+    (127.0.0.1) or any other of its IP addresses. When receiving messages, Exim
+    does not treat the loopback address specially. It treats all such
+    connections in the same way as connections from other hosts.
+
+In the three cases that do not involve TCP/IP, the sender address is
+constructed from the login name of the user that called Exim and a default
+qualification domain (which can be set by the qualify_domain configuration
+option). For local or batch SMTP, a sender address that is passed using the
+SMTP MAIL command is ignored. However, the system administrator may allow
+certain users ("trusted users") to specify a different sender address
+unconditionally, or all users to specify certain forms of different sender
+address. The -f option or the SMTP MAIL command is used to specify these
+different addresses. See section 5.2 for details of trusted users, and the 
+untrusted_set_sender option for a way of allowing untrusted users to change
+sender addresses.
+
+Messages received by either of the non-interactive mechanisms are subject to
+checking by the non-SMTP ACL, if one is defined. Messages received using SMTP
+(either over TCP/IP, or interacting with a local process) can be checked by a
+number of ACLs that operate at different times during the SMTP session. Either
+individual recipients, or the entire message, can be rejected if local policy
+requirements are not met. The local_scan() function (see chapter 44) is run for
+all incoming messages.
+
+Exim can be configured not to start a delivery process when a message is
+received; this can be unconditional, or depend on the number of incoming SMTP
+connections or the system load. In these situations, new messages wait on the
+queue until a queue runner process picks them up. However, in standard
+configurations under normal conditions, delivery is started as soon as a
+message is received.
+
+
+3.6 Handling an incoming message
+--------------------------------
+
+When Exim accepts a message, it writes two files in its spool directory. The
+first contains the envelope information, the current status of the message, and
+the header lines, and the second contains the body of the message. The names of
+the two spool files consist of the message id, followed by "-H" for the file
+containing the envelope and header, and "-D" for the data file.
+
+By default all these message files are held in a single directory called input
+inside the general Exim spool directory. Some operating systems do not perform
+very well if the number of files in a directory gets large; to improve
+performance in such cases, the split_spool_directory option can be used. This
+causes Exim to split up the input files into 62 sub-directories whose names are
+single letters or digits. When this is done, the queue is processed one
+sub-directory at a time instead of all at once, which can improve overall
+performance even when there are not enough files in each directory to affect
+file system performance.
+
+The envelope information consists of the address of the message's sender and
+the addresses of the recipients. This information is entirely separate from any
+addresses contained in the header lines. The status of the message includes a
+list of recipients who have already received the message. The format of the
+first spool file is described in chapter 55.
+
+Address rewriting that is specified in the rewrite section of the configuration
+(see chapter 31) is done once and for all on incoming addresses, both in the
+header lines and the envelope, at the time the message is accepted. If during
+the course of delivery additional addresses are generated (for example, via
+aliasing), these new addresses are rewritten as soon as they are generated. At
+the time a message is actually delivered (transported) further rewriting can
+take place; because this is a transport option, it can be different for
+different forms of delivery. It is also possible to specify the addition or
+removal of certain header lines at the time the message is delivered (see
+chapters 15 and 24).
+
+
+3.7 Life of a message
+---------------------
+
+A message remains in the spool directory until it is completely delivered to
+its recipients or to an error address, or until it is deleted by an
+administrator or by the user who originally created it. In cases when delivery
+cannot proceed - for example, when a message can neither be delivered to its
+recipients nor returned to its sender, the message is marked "frozen" on the
+spool, and no more deliveries are attempted.
+
+An administrator can "thaw" such messages when the problem has been corrected,
+and can also freeze individual messages by hand if necessary. In addition, an
+administrator can force a delivery error, causing a bounce message to be sent.
+
+There are options called ignore_bounce_errors_after and timeout_frozen_after,
+which discard frozen messages after a certain time. The first applies only to
+frozen bounces, the second to any frozen messages.
+
+While Exim is working on a message, it writes information about each delivery
+attempt to its main log file. This includes successful, unsuccessful, and
+delayed deliveries for each recipient (see chapter 51). The log lines are also
+written to a separate message log file for each message. These logs are solely
+for the benefit of the administrator, and are normally deleted along with the
+spool files when processing of a message is complete. The use of individual
+message logs can be disabled by setting no_message_logs; this might give an
+improvement in performance on very busy systems.
+
+All the information Exim itself needs to set up a delivery is kept in the first
+spool file, along with the header lines. When a successful delivery occurs, the
+address is immediately written at the end of a journal file, whose name is the
+message id followed by "-J". At the end of a delivery run, if there are some
+addresses left to be tried again later, the first spool file (the "-H" file) is
+updated to indicate which these are, and the journal file is then deleted.
+Updating the spool file is done by writing a new file and renaming it, to
+minimize the possibility of data loss.
+
+Should the system or the program crash after a successful delivery but before
+the spool file has been updated, the journal is left lying around. The next
+time Exim attempts to deliver the message, it reads the journal file and
+updates the spool file before proceeding. This minimizes the chances of double
+deliveries caused by crashes.
+
+
+3.8 Processing an address for delivery
+--------------------------------------
+
+The main delivery processing elements of Exim are called routers and transports
+, and collectively these are known as drivers. Code for a number of them is
+provided in the source distribution, and compile-time options specify which
+ones are included in the binary. Run time options specify which ones are
+actually used for delivering messages.
+
+Each driver that is specified in the run time configuration is an instance of
+that particular driver type. Multiple instances are allowed; for example, you
+can set up several different smtp transports, each with different option values
+that might specify different ports or different timeouts. Each instance has its
+own identifying name. In what follows we will normally use the instance name
+when discussing one particular instance (that is, one specific configuration of
+the driver), and the generic driver name when discussing the driver's features
+in general.
+
+A router is a driver that operates on an address, either determining how its
+delivery should happen, by assigning it to a specific transport, or converting
+the address into one or more new addresses (for example, via an alias file). A
+router may also explicitly choose to fail an address, causing it to be bounced.
+
+A transport is a driver that transmits a copy of the message from Exim's spool
+to some destination. There are two kinds of transport: for a local transport,
+the destination is a file or a pipe on the local host, whereas for a remote
+transport the destination is some other host. A message is passed to a specific
+transport as a result of successful routing. If a message has several
+recipients, it may be passed to a number of different transports.
+
+An address is processed by passing it to each configured router instance in
+turn, subject to certain preconditions, until a router accepts the address or
+specifies that it should be bounced. We will describe this process in more
+detail shortly. First, as a simple example, we consider how each recipient
+address in a message is processed in a small configuration of three routers.
+
+To make this a more concrete example, it is described in terms of some actual
+routers, but remember, this is only an example. You can configure Exim's
+routers in many different ways, and there may be any number of routers in a
+configuration.
+
+The first router that is specified in a configuration is often one that handles
+addresses in domains that are not recognized specially by the local host. These
+are typically addresses for arbitrary domains on the Internet. A precondition
+is set up which looks for the special domains known to the host (for example,
+its own domain name), and the router is run for addresses that do not match.
+Typically, this is a router that looks up domains in the DNS in order to find
+the hosts to which this address routes. If it succeeds, the address is assigned
+to a suitable SMTP transport; if it does not succeed, the router is configured
+to fail the address.
+
+The second router is reached only when the domain is recognized as one that
+"belongs" to the local host. This router does redirection - also known as
+aliasing and forwarding. When it generates one or more new addresses from the
+original, each of them is routed independently from the start. Otherwise, the
+router may cause an address to fail, or it may simply decline to handle the
+address, in which case the address is passed to the next router.
+
+The final router in many configurations is one that checks to see if the
+address belongs to a local mailbox. The precondition may involve a check to see
+if the local part is the name of a login account, or it may look up the local
+part in a file or a database. If its preconditions are not met, or if the
+router declines, we have reached the end of the routers. When this happens, the
+address is bounced.
+
+
+3.9 Processing an address for verification
+------------------------------------------
+
+As well as being used to decide how to deliver to an address, Exim's routers
+are also used for address verification. Verification can be requested as one of
+the checks to be performed in an ACL for incoming messages, on both sender and
+recipient addresses, and it can be tested using the -bv and -bvs command line
+options.
+
+When an address is being verified, the routers are run in "verify mode". This
+does not affect the way the routers work, but it is a state that can be
+detected. By this means, a router can be skipped or made to behave differently
+when verifying. A common example is a configuration in which the first router
+sends all messages to a message-scanning program, unless they have been
+previously scanned. Thus, the first router accepts all addresses without any
+checking, making it useless for verifying. Normally, the no_verify option would
+be set for such a router, causing it to be skipped in verify mode.
+
+
+3.10 Running an individual router
+---------------------------------
+
+As explained in the example above, a number of preconditions are checked before
+running a router. If any are not met, the router is skipped, and the address is
+passed to the next router. When all the preconditions on a router are met, the
+router is run. What happens next depends on the outcome, which is one of the
+following:
+
+  * accept: The router accepts the address, and either assigns it to a
+    transport, or generates one or more "child" addresses. Processing the
+    original address ceases, unless the unseen option is set on the router.
+    This option can be used to set up multiple deliveries with different
+    routing (for example, for keeping archive copies of messages). When unseen
+    is set, the address is passed to the next router. Normally, however, an 
+    accept return marks the end of routing.
+
+    Any child addresses generated by the router are processed independently,
+    starting with the first router by default. It is possible to change this by
+    setting the redirect_router option to specify which router to start at for
+    child addresses. Unlike pass_router (see below) the router specified by 
+    redirect_router may be anywhere in the router configuration.
+
+  * pass: The router recognizes the address, but cannot handle it itself. It
+    requests that the address be passed to another router. By default the
+    address is passed to the next router, but this can be changed by setting
+    the pass_router option. However, (unlike redirect_router) the named router
+    must be below the current router (to avoid loops).
+
+  * decline: The router declines to accept the address because it does not
+    recognize it at all. By default, the address is passed to the next router,
+    but this can be prevented by setting the no_more option. When no_more is
+    set, all the remaining routers are skipped. In effect, no_more converts 
+    decline into fail.
+
+  * fail: The router determines that the address should fail, and queues it for
+    the generation of a bounce message. There is no further processing of the
+    original address unless unseen is set on the router.
+
+  * defer: The router cannot handle the address at the present time. (A
+    database may be offline, or a DNS lookup may have timed out.) No further
+    processing of the address happens in this delivery attempt. It is tried
+    again next time the message is considered for delivery.
+
+  * error: There is some error in the router (for example, a syntax error in
+    its configuration). The action is as for defer.
+
+If an address reaches the end of the routers without having been accepted by
+any of them, it is bounced as unrouteable. The default error message in this
+situation is "unrouteable address", but you can set your own message by making
+use of the cannot_route_message option. This can be set for any router; the
+value from the last router that "saw" the address is used.
+
+Sometimes while routing you want to fail a delivery when some conditions are
+met but others are not, instead of passing the address on for further routing.
+You can do this by having a second router that explicitly fails the delivery
+when the relevant conditions are met. The redirect router has a "fail" facility
+for this purpose.
+
+
+3.11 Duplicate addresses
+------------------------
+
+Once routing is complete, Exim scans the addresses that are assigned to local
+and remote transports, and discards any duplicates that it finds. During this
+check, local parts are treated as case-sensitive. This happens only when
+actually delivering a message; when testing routers with -bt, all the routed
+addresses are shown.
+
+
+3.12 Router preconditions
+-------------------------
+
+The preconditions that are tested for each router are listed below, in the
+order in which they are tested. The individual configuration options are
+described in more detail in chapter 15.
+
+  * The local_part_prefix and local_part_suffix options can specify that the
+    local parts handled by the router may or must have certain prefixes and/or
+    suffixes. If a mandatory affix (prefix or suffix) is not present, the
+    router is skipped. These conditions are tested first. When an affix is
+    present, it is removed from the local part before further processing,
+    including the evaluation of any other conditions.
+
+  * Routers can be designated for use only when not verifying an address, that
+    is, only when routing it for delivery (or testing its delivery routing). If
+    the verify option is set false, the router is skipped when Exim is
+    verifying an address. Setting the verify option actually sets two options, 
+    verify_sender and verify_recipient, which independently control the use of
+    the router for sender and recipient verification. You can set these options
+    directly if you want a router to be used for only one type of verification.
+    Note that cutthrough delivery is classed as a recipient verification for
+    this purpose.
+
+  * If the address_test option is set false, the router is skipped when Exim is
+    run with the -bt option to test an address routing. This can be helpful
+    when the first router sends all new messages to a scanner of some sort; it
+    makes it possible to use -bt to test subsequent delivery routing without
+    having to simulate the effect of the scanner.
+
+  * Routers can be designated for use only when verifying an address, as
+    opposed to routing it for delivery. The verify_only option controls this.
+    Again, cutthrough delivery counts as a verification.
+
+  * Individual routers can be explicitly skipped when running the routers to
+    check an address given in the SMTP EXPN command (see the expn option).
+
+  * If the domains option is set, the domain of the address must be in the set
+    of domains that it defines.
+
+  * If the local_parts option is set, the local part of the address must be in
+    the set of local parts that it defines. If local_part_prefix or 
+    local_part_suffix is in use, the prefix or suffix is removed from the local
+    part before this check. If you want to do precondition tests on local parts
+    that include affixes, you can do so by using a condition option (see below)
+    that uses the variables $local_part, $local_part_prefix, and
+    $local_part_suffix as necessary.
+
+  * If the check_local_user option is set, the local part must be the name of
+    an account on the local host. If this check succeeds, the uid and gid of
+    the local user are placed in $local_user_uid and $local_user_gid and the
+    user's home directory is placed in $home; these values can be used in the
+    remaining preconditions.
+
+  * If the router_home_directory option is set, it is expanded at this point,
+    because it overrides the value of $home. If this expansion were left till
+    later, the value of $home as set by check_local_user would be used in
+    subsequent tests. Having two different values of $home in the same router
+    could lead to confusion.
+
+  * If the senders option is set, the envelope sender address must be in the
+    set of addresses that it defines.
+
+  * If the require_files option is set, the existence or non-existence of
+    specified files is tested.
+
+  * If the condition option is set, it is evaluated and tested. This option
+    uses an expanded string to allow you to set up your own custom
+    preconditions. Expanded strings are described in chapter 11.
+
+Note that require_files comes near the end of the list, so you cannot use it to
+check for the existence of a file in which to lookup up a domain, local part,
+or sender. However, as these options are all expanded, you can use the exists
+expansion condition to make such tests within each condition. The require_files
+option is intended for checking files that the router may be going to use
+internally, or which are needed by a specific transport (for example,
+.procmailrc).
+
+
+3.13 Delivery in detail
+-----------------------
+
+When a message is to be delivered, the sequence of events is as follows:
+
+  * If a system-wide filter file is specified, the message is passed to it. The
+    filter may add recipients to the message, replace the recipients, discard
+    the message, cause a new message to be generated, or cause the message
+    delivery to fail. The format of the system filter file is the same as for
+    Exim user filter files, described in the separate document entitled Exim's
+    interfaces to mail filtering. (Note: Sieve cannot be used for system filter
+    files.)
+
+    Some additional features are available in system filters - see chapter 45
+    for details. Note that a message is passed to the system filter only once
+    per delivery attempt, however many recipients it has. However, if there are
+    several delivery attempts because one or more addresses could not be
+    immediately delivered, the system filter is run each time. The filter
+    condition first_delivery can be used to detect the first run of the system
+    filter.
+
+  * Each recipient address is offered to each configured router in turn,
+    subject to its preconditions, until one is able to handle it. If no router
+    can handle the address, that is, if they all decline, the address is
+    failed. Because routers can be targeted at particular domains, several
+    locally handled domains can be processed entirely independently of each
+    other.
+
+  * A router that accepts an address may assign it to a local or a remote
+    transport. However, the transport is not run at this time. Instead, the
+    address is placed on a list for the particular transport, which will be run
+    later. Alternatively, the router may generate one or more new addresses
+    (typically from alias, forward, or filter files). New addresses are fed
+    back into this process from the top, but in order to avoid loops, a router
+    ignores any address which has an identically-named ancestor that was
+    processed by itself.
+
+  * When all the routing has been done, addresses that have been successfully
+    handled are passed to their assigned transports. When local transports are
+    doing real local deliveries, they handle only one address at a time, but if
+    a local transport is being used as a pseudo-remote transport (for example,
+    to collect batched SMTP messages for transmission by some other means)
+    multiple addresses can be handled. Remote transports can always handle more
+    than one address at a time, but can be configured not to do so, or to
+    restrict multiple addresses to the same domain.
+
+  * Each local delivery to a file or a pipe runs in a separate process under a
+    non-privileged uid, and these deliveries are run one at a time. Remote
+    deliveries also run in separate processes, normally under a uid that is
+    private to Exim ("the Exim user"), but in this case, several remote
+    deliveries can be run in parallel. The maximum number of simultaneous
+    remote deliveries for any one message is set by the remote_max_parallel
+    option. The order in which deliveries are done is not defined, except that
+    all local deliveries happen before any remote deliveries.
+
+  * When it encounters a local delivery during a queue run, Exim checks its
+    retry database to see if there has been a previous temporary delivery
+    failure for the address before running the local transport. If there was a
+    previous failure, Exim does not attempt a new delivery until the retry time
+    for the address is reached. However, this happens only for delivery
+    attempts that are part of a queue run. Local deliveries are always
+    attempted when delivery immediately follows message reception, even if
+    retry times are set for them. This makes for better behaviour if one
+    particular message is causing problems (for example, causing quota
+    overflow, or provoking an error in a filter file).
+
+  * Remote transports do their own retry handling, since an address may be
+    deliverable to one of a number of hosts, each of which may have a different
+    retry time. If there have been previous temporary failures and no host has
+    reached its retry time, no delivery is attempted, whether in a queue run or
+    not. See chapter 32 for details of retry strategies.
+
+  * If there were any permanent errors, a bounce message is returned to an
+    appropriate address (the sender in the common case), with details of the
+    error for each failing address. Exim can be configured to send copies of
+    bounce messages to other addresses.
+
+  * If one or more addresses suffered a temporary failure, the message is left
+    on the queue, to be tried again later. Delivery of these addresses is said
+    to be deferred.
+
+  * When all the recipient addresses have either been delivered or bounced,
+    handling of the message is complete. The spool files and message log are
+    deleted, though the message log can optionally be preserved if required.
+
+
+3.14 Retry mechanism
+--------------------
+
+Exim's mechanism for retrying messages that fail to get delivered at the first
+attempt is the queue runner process. You must either run an Exim daemon that
+uses the -q option with a time interval to start queue runners at regular
+intervals, or use some other means (such as cron) to start them. If you do not
+arrange for queue runners to be run, messages that fail temporarily at the
+first attempt will remain on your queue for ever. A queue runner process works
+its way through the queue, one message at a time, trying each delivery that has
+passed its retry time. You can run several queue runners at once.
+
+Exim uses a set of configured rules to determine when next to retry the failing
+address (see chapter 32). These rules also specify when Exim should give up
+trying to deliver to the address, at which point it generates a bounce message.
+If no retry rules are set for a particular host, address, and error
+combination, no retries are attempted, and temporary errors are treated as
+permanent.
+
+
+3.15 Temporary delivery failure
+-------------------------------
+
+There are many reasons why a message may not be immediately deliverable to a
+particular address. Failure to connect to a remote machine (because it, or the
+connection to it, is down) is one of the most common. Temporary failures may be
+detected during routing as well as during the transport stage of delivery.
+Local deliveries may be delayed if NFS files are unavailable, or if a mailbox
+is on a file system where the user is over quota. Exim can be configured to
+impose its own quotas on local mailboxes; where system quotas are set they will
+also apply.
+
+If a host is unreachable for a period of time, a number of messages may be
+waiting for it by the time it recovers, and sending them in a single SMTP
+connection is clearly beneficial. Whenever a delivery to a remote host is
+deferred, Exim makes a note in its hints database, and whenever a successful
+SMTP delivery has happened, it looks to see if any other messages are waiting
+for the same host. If any are found, they are sent over the same SMTP
+connection, subject to a configuration limit as to the maximum number in any
+one connection.
+
+
+3.16 Permanent delivery failure
+-------------------------------
+
+When a message cannot be delivered to some or all of its intended recipients, a
+bounce message is generated. Temporary delivery failures turn into permanent
+errors when their timeout expires. All the addresses that fail in a given
+delivery attempt are listed in a single message. If the original message has
+many recipients, it is possible for some addresses to fail in one delivery
+attempt and others to fail subsequently, giving rise to more than one bounce
+message. The wording of bounce messages can be customized by the administrator.
+See chapter 48 for details.
+
+Bounce messages contain an X-Failed-Recipients: header line that lists the
+failed addresses, for the benefit of programs that try to analyse such messages
+automatically.
+
+A bounce message is normally sent to the sender of the original message, as
+obtained from the message's envelope. For incoming SMTP messages, this is the
+address given in the MAIL command. However, when an address is expanded via a
+forward or alias file, an alternative address can be specified for delivery
+failures of the generated addresses. For a mailing list expansion (see section
+49.2) it is common to direct bounce messages to the manager of the list.
+
+
+3.17 Failures to deliver bounce messages
+----------------------------------------
+
+If a bounce message (either locally generated or received from a remote host)
+itself suffers a permanent delivery failure, the message is left on the queue,
+but it is frozen, awaiting the attention of an administrator. There are options
+that can be used to make Exim discard such failed messages, or to keep them for
+only a short time (see timeout_frozen_after and ignore_bounce_errors_after).
+
+
+
+===============================================================================
+4. BUILDING AND INSTALLING EXIM
+
+
+4.1 Unpacking
+-------------
+
+Exim is distributed as a gzipped or bzipped tar file which, when unpacked,
+creates a directory with the name of the current release (for example,
+exim-4.84) into which the following files are placed:
+
+    ACKNOWLEDGMENTS contains some acknowledgments
+    CHANGES         contains a reference to where changes are documented
+    LICENCE         the GNU General Public Licence
+    Makefile        top-level make file
+    NOTICE          conditions for the use of Exim
+    README          list of files, directories and simple build instructions
+
+Other files whose names begin with README may also be present. The following
+subdirectories are created:
+
+    Local        an empty directory for local configuration files
+    OS           OS-specific files
+    doc          documentation files
+    exim_monitor source files for the Exim monitor
+    scripts      scripts used in the build process
+    src          remaining source files
+    util         independent utilities
+
+The main utility programs are contained in the src directory, and are built
+with the Exim binary. The util directory contains a few optional scripts that
+may be useful to some sites.
+
+
+4.2 Multiple machine architectures and operating systems
+--------------------------------------------------------
+
+The building process for Exim is arranged to make it easy to build binaries for
+a number of different architectures and operating systems from the same set of
+source files. Compilation does not take place in the src directory. Instead, a 
+build directory is created for each architecture and operating system. Symbolic
+links to the sources are installed in this directory, which is where the actual
+building takes place. In most cases, Exim can discover the machine architecture
+and operating system for itself, but the defaults can be overridden if
+necessary.
+
+
+4.3 PCRE library
+----------------
+
+Exim no longer has an embedded PCRE library as the vast majority of modern
+systems include PCRE as a system library, although you may need to install the
+PCRE or PCRE development package for your operating system. If your system has
+a normal PCRE installation the Exim build process will need no further
+configuration. If the library or the headers are in an unusual location you
+will need to either set the PCRE_LIBS and INCLUDE directives appropriately, or
+set PCRE_CONFIG=yes to use the installed pcre-config command. If your operating
+system has no PCRE support then you will need to obtain and build the current
+PCRE from ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/. More
+information on PCRE is available at http://www.pcre.org/.
+
+
+4.4 DBM libraries
+-----------------
+
+Even if you do not use any DBM files in your configuration, Exim still needs a
+DBM library in order to operate, because it uses indexed files for its hints
+databases. Unfortunately, there are a number of DBM libraries in existence, and
+different operating systems often have different ones installed.
+
+If you are using Solaris, IRIX, one of the modern BSD systems, or a modern
+Linux distribution, the DBM configuration should happen automatically, and you
+may be able to ignore this section. Otherwise, you may have to learn more than
+you would like about DBM libraries from what follows.
+
+Licensed versions of Unix normally contain a library of DBM functions operating
+via the ndbm interface, and this is what Exim expects by default. Free versions
+of Unix seem to vary in what they contain as standard. In particular, some
+early versions of Linux have no default DBM library, and different distributors
+have chosen to bundle different libraries with their packaged versions.
+However, the more recent releases seem to have standardized on the Berkeley DB
+library.
+
+Different DBM libraries have different conventions for naming the files they
+use. When a program opens a file called dbmfile, there are several
+possibilities:
+
+ 1. A traditional ndbm implementation, such as that supplied as part of
+    Solaris, operates on two files called dbmfile.dir and dbmfile.pag.
+
+ 2. The GNU library, gdbm, operates on a single file. If used via its ndbm
+    compatibility interface it makes two different hard links to it with names
+    dbmfile.dir and dbmfile.pag, but if used via its native interface, the file
+    name is used unmodified.
+
+ 3. The Berkeley DB package, if called via its ndbm compatibility interface,
+    operates on a single file called dbmfile.db, but otherwise looks to the
+    programmer exactly the same as the traditional ndbm implementation.
+
+ 4. If the Berkeley package is used in its native mode, it operates on a single
+    file called dbmfile; the programmer's interface is somewhat different to
+    the traditional ndbm interface.
+
+ 5. To complicate things further, there are several very different versions of
+    the Berkeley DB package. Version 1.85 was stable for a very long time,
+    releases 2.x and 3.x were current for a while, but the latest versions are
+    now numbered 4.x. Maintenance of some of the earlier releases has ceased.
+    All versions of Berkeley DB can be obtained from http://www.sleepycat.com/.
+
+ 6. Yet another DBM library, called tdb, is available from http://
+    download.sourceforge.net/tdb. It has its own interface, and also operates
+    on a single file.
+
+Exim and its utilities can be compiled to use any of these interfaces. In order
+to use any version of the Berkeley DB package in native mode, you must set
+USE_DB in an appropriate configuration file (typically Local/Makefile). For
+example:
+
+USE_DB=yes
+
+Similarly, for gdbm you set USE_GDBM, and for tdb you set USE_TDB. An error is
+diagnosed if you set more than one of these.
+
+At the lowest level, the build-time configuration sets none of these options,
+thereby assuming an interface of type (1). However, some operating system
+configuration files (for example, those for the BSD operating systems and
+Linux) assume type (4) by setting USE_DB as their default, and the
+configuration files for Cygwin set USE_GDBM. Anything you set in Local/Makefile
+, however, overrides these system defaults.
+
+As well as setting USE_DB, USE_GDBM, or USE_TDB, it may also be necessary to
+set DBMLIB, to cause inclusion of the appropriate library, as in one of these
+lines:
+
+DBMLIB = -ldb
+DBMLIB = -ltdb
+
+Settings like that will work if the DBM library is installed in the standard
+place. Sometimes it is not, and the library's header file may also not be in
+the default path. You may need to set INCLUDE to specify where the header file
+is, and to specify the path to the library more fully in DBMLIB, as in this
+example:
+
+INCLUDE=-I/usr/local/include/db-4.1
+DBMLIB=/usr/local/lib/db-4.1/libdb.a
+
+There is further detailed discussion about the various DBM libraries in the
+file doc/dbm.discuss.txt in the Exim distribution.
+
+
+4.5 Pre-building configuration
+------------------------------
+
+Before building Exim, a local configuration file that specifies options
+independent of any operating system has to be created with the name Local/
+Makefile. A template for this file is supplied as the file src/EDITME, and it
+contains full descriptions of all the option settings therein. These
+descriptions are therefore not repeated here. If you are building Exim for the
+first time, the simplest thing to do is to copy src/EDITME to Local/Makefile,
+then read it and edit it appropriately.
+
+There are three settings that you must supply, because Exim will not build
+without them. They are the location of the run time configuration file
+(CONFIGURE_FILE), the directory in which Exim binaries will be installed
+(BIN_DIRECTORY), and the identity of the Exim user (EXIM_USER and maybe
+EXIM_GROUP as well). The value of CONFIGURE_FILE can in fact be a
+colon-separated list of file names; Exim uses the first of them that exists.
+
+There are a few other parameters that can be specified either at build time or
+at run time, to enable the same binary to be used on a number of different
+machines. However, if the locations of Exim's spool directory and log file
+directory (if not within the spool directory) are fixed, it is recommended that
+you specify them in Local/Makefile instead of at run time, so that errors
+detected early in Exim's execution (such as a malformed configuration file) can
+be logged.
+
+Exim's interfaces for calling virus and spam scanning software directly from
+access control lists are not compiled by default. If you want to include these
+facilities, you need to set
+
+WITH_CONTENT_SCAN=yes
+
+in your Local/Makefile. For details of the facilities themselves, see chapter
+43.
+
+If you are going to build the Exim monitor, a similar configuration process is
+required. The file exim_monitor/EDITME must be edited appropriately for your
+installation and saved under the name Local/eximon.conf. If you are happy with
+the default settings described in exim_monitor/EDITME, Local/eximon.conf can be
+empty, but it must exist.
+
+This is all the configuration that is needed in straightforward cases for known
+operating systems. However, the building process is set up so that it is easy
+to override options that are set by default or by operating-system-specific
+configuration files, for example to change the name of the C compiler, which
+defaults to gcc. See section 4.13 below for details of how to do this.
+
+
+4.6 Support for iconv()
+-----------------------
+
+The contents of header lines in messages may be encoded according to the rules
+described RFC 2047. This makes it possible to transmit characters that are not
+in the ASCII character set, and to label them as being in a particular
+character set. When Exim is inspecting header lines by means of the $h_
+mechanism, it decodes them, and translates them into a specified character set
+(default ISO-8859-1). The translation is possible only if the operating system
+supports the iconv() function.
+
+However, some of the operating systems that supply iconv() do not support very
+many conversions. The GNU libiconv library (available from http://www.gnu.org/
+software/libiconv/) can be installed on such systems to remedy this deficiency,
+as well as on systems that do not supply iconv() at all. After installing 
+libiconv, you should add
+
+HAVE_ICONV=yes
+
+to your Local/Makefile and rebuild Exim.
+
+
+4.7 Including TLS/SSL encryption support
+----------------------------------------
+
+Exim can be built to support encrypted SMTP connections, using the STARTTLS
+command as per RFC 2487. It can also support legacy clients that expect to
+start a TLS session immediately on connection to a non-standard port (see the 
+tls_on_connect_ports runtime option and the -tls-on-connect command line
+option).
+
+If you want to build Exim with TLS support, you must first install either the
+OpenSSL or GnuTLS library. There is no cryptographic code in Exim itself for
+implementing SSL.
+
+If OpenSSL is installed, you should set
+
+SUPPORT_TLS=yes
+TLS_LIBS=-lssl -lcrypto
+
+in Local/Makefile. You may also need to specify the locations of the OpenSSL
+library and include files. For example:
+
+SUPPORT_TLS=yes
+TLS_LIBS=-L/usr/local/openssl/lib -lssl -lcrypto
+TLS_INCLUDE=-I/usr/local/openssl/include/
+
+If you have pkg-config available, then instead you can just use:
+
+SUPPORT_TLS=yes
+USE_OPENSSL_PC=openssl
+
+If GnuTLS is installed, you should set
+
+SUPPORT_TLS=yes
+USE_GNUTLS=yes
+TLS_LIBS=-lgnutls -ltasn1 -lgcrypt
+
+in Local/Makefile, and again you may need to specify the locations of the
+library and include files. For example:
+
+SUPPORT_TLS=yes
+USE_GNUTLS=yes
+TLS_LIBS=-L/usr/gnu/lib -lgnutls -ltasn1 -lgcrypt
+TLS_INCLUDE=-I/usr/gnu/include
+
+If you have pkg-config available, then instead you can just use:
+
+SUPPORT_TLS=yes
+USE_GNUTLS=yes
+USE_GNUTLS_PC=gnutls
+
+You do not need to set TLS_INCLUDE if the relevant directory is already
+specified in INCLUDE. Details of how to configure Exim to make use of TLS are
+given in chapter 41.
+
+
+4.8 Use of tcpwrappers
+----------------------
+
+Exim can be linked with the tcpwrappers library in order to check incoming SMTP
+calls using the tcpwrappers control files. This may be a convenient alternative
+to Exim's own checking facilities for installations that are already making use
+of tcpwrappers for other purposes. To do this, you should set USE_TCP_WRAPPERS
+in Local/Makefile, arrange for the file tcpd.h to be available at compile time,
+and also ensure that the library libwrap.a is available at link time, typically
+by including -lwrap in EXTRALIBS_EXIM. For example, if tcpwrappers is installed
+in /usr/local, you might have
+
+USE_TCP_WRAPPERS=yes
+CFLAGS=-O -I/usr/local/include
+EXTRALIBS_EXIM=-L/usr/local/lib -lwrap
+
+in Local/Makefile. The daemon name to use in the tcpwrappers control files is
+"exim". For example, the line
+
+exim : LOCAL  192.168.1.  .friendly.domain.example
+
+in your /etc/hosts.allow file allows connections from the local host, from the
+subnet 192.168.1.0/24, and from all hosts in friendly.domain.example. All other
+connections are denied. The daemon name used by tcpwrappers can be changed at
+build time by setting TCP_WRAPPERS_DAEMON_NAME in Local/Makefile, or by setting
+tcp_wrappers_daemon_name in the configure file. Consult the tcpwrappers
+documentation for further details.
+
+
+4.9 Including support for IPv6
+------------------------------
+
+Exim contains code for use on systems that have IPv6 support. Setting
+"HAVE_IPV6=YES" in Local/Makefile causes the IPv6 code to be included; it may
+also be necessary to set IPV6_INCLUDE and IPV6_LIBS on systems where the IPv6
+support is not fully integrated into the normal include and library files.
+
+Two different types of DNS record for handling IPv6 addresses have been
+defined. AAAA records (analogous to A records for IPv4) are in use, and are
+currently seen as the mainstream. Another record type called A6 was proposed as
+better than AAAA because it had more flexibility. However, it was felt to be
+over-complex, and its status was reduced to "experimental". It is not known if
+anyone is actually using A6 records. Exim has support for A6 records, but this
+is included only if you set "SUPPORT_A6=YES" in Local/Makefile. The support has
+not been tested for some time.
+
+
+4.10 Dynamically loaded lookup module support
+---------------------------------------------
+
+On some platforms, Exim supports not compiling all lookup types directly into
+the main binary, instead putting some into external modules which can be loaded
+on demand. This permits packagers to build Exim with support for lookups with
+extensive library dependencies without requiring all users to install all of
+those dependencies. Most, but not all, lookup types can be built this way.
+
+Set "LOOKUP_MODULE_DIR" to the directory into which the modules will be
+installed; Exim will only load modules from that directory, as a security
+measure. You will need to set "CFLAGS_DYNAMIC" if not already defined for your
+OS; see OS/Makefile-Linux for an example. Some other requirements for adjusting
+"EXTRALIBS" may also be necessary, see src/EDITME for details.
+
+Then, for each module to be loaded dynamically, define the relevant "LOOKUP_"<
+lookup_type> flags to have the value "2" instead of "yes". For example, this
+will build in lsearch but load sqlite and mysql support on demand:
+
+LOOKUP_LSEARCH=yes
+LOOKUP_SQLITE=2
+LOOKUP_MYSQL=2
+
+
+4.11 The building process
+-------------------------
+
+Once Local/Makefile (and Local/eximon.conf, if required) have been created, run
+make at the top level. It determines the architecture and operating system
+types, and creates a build directory if one does not exist. For example, on a
+Sun system running Solaris 8, the directory build-SunOS5-5.8-sparc is created. 
+Symbolic links to relevant source files are installed in the build directory.
+
+Warning: The -j (parallel) flag must not be used with make; the building
+process fails if it is set.
+
+If this is the first time make has been run, it calls a script that builds a
+make file inside the build directory, using the configuration files from the
+Local directory. The new make file is then passed to another instance of make.
+This does the real work, building a number of utility scripts, and then
+compiling and linking the binaries for the Exim monitor (if configured), a
+number of utility programs, and finally Exim itself. The command "make
+makefile" can be used to force a rebuild of the make file in the build
+directory, should this ever be necessary.
+
+If you have problems building Exim, check for any comments there may be in the
+README file concerning your operating system, and also take a look at the FAQ,
+where some common problems are covered.
+
+
+4.12 Output from "make"
+-----------------------
+
+The output produced by the make process for compile lines is often very
+unreadable, because these lines can be very long. For this reason, the normal
+output is suppressed by default, and instead output similar to that which
+appears when compiling the 2.6 Linux kernel is generated: just a short line for
+each module that is being compiled or linked. However, it is still possible to
+get the full output, by calling make like this:
+
+FULLECHO='' make -e
+
+The value of FULLECHO defaults to "@", the flag character that suppresses
+command reflection in make. When you ask for the full output, it is given in
+addition to the short output.
+
+
+4.13 Overriding build-time options for Exim
+-------------------------------------------
+
+The main make file that is created at the beginning of the building process
+consists of the concatenation of a number of files which set configuration
+values, followed by a fixed set of make instructions. If a value is set more
+than once, the last setting overrides any previous ones. This provides a
+convenient way of overriding defaults. The files that are concatenated are, in
+order:
+
+OS/Makefile-Default
+OS/Makefile-<ostype>
+Local/Makefile
+Local/Makefile-<ostype>
+Local/Makefile-<archtype>
+Local/Makefile-<ostype>-<archtype>
+OS/Makefile-Base
+
+where <ostype> is the operating system type and <archtype> is the architecture
+type. Local/Makefile is required to exist, and the building process fails if it
+is absent. The other three Local files are optional, and are often not needed.
+
+The values used for <ostype> and <archtype> are obtained from scripts called
+scripts/os-type and scripts/arch-type respectively. If either of the
+environment variables EXIM_OSTYPE or EXIM_ARCHTYPE is set, their values are
+used, thereby providing a means of forcing particular settings. Otherwise, the
+scripts try to get values from the uname command. If this fails, the shell
+variables OSTYPE and ARCHTYPE are inspected. A number of ad hoc transformations
+are then applied, to produce the standard names that Exim expects. You can run
+these scripts directly from the shell in order to find out what values are
+being used on your system.
+
+OS/Makefile-Default contains comments about the variables that are set therein.
+Some (but not all) are mentioned below. If there is something that needs
+changing, review the contents of this file and the contents of the make file
+for your operating system (OS/Makefile-<ostype>) to see what the default values
+are.
+
+If you need to change any of the values that are set in OS/Makefile-Default or
+in OS/Makefile-<ostype>, or to add any new definitions, you do not need to
+change the original files. Instead, you should make the changes by putting the
+new values in an appropriate Local file. For example, when building Exim in
+many releases of the Tru64-Unix (formerly Digital UNIX, formerly DEC-OSF1)
+operating system, it is necessary to specify that the C compiler is called cc
+rather than gcc. Also, the compiler must be called with the option -std1, to
+make it recognize some of the features of Standard C that Exim uses. (Most
+other compilers recognize Standard C by default.) To do this, you should create
+a file called Local/Makefile-OSF1 containing the lines
+
+CC=cc
+CFLAGS=-std1
+
+If you are compiling for just one operating system, it may be easier to put
+these lines directly into Local/Makefile.
+
+Keeping all your local configuration settings separate from the distributed
+files makes it easy to transfer them to new versions of Exim simply by copying
+the contents of the Local directory.
+
+Exim contains support for doing LDAP, NIS, NIS+, and other kinds of file
+lookup, but not all systems have these components installed, so the default is
+not to include the relevant code in the binary. All the different kinds of file
+and database lookup that Exim supports are implemented as separate code modules
+which are included only if the relevant compile-time options are set. In the
+case of LDAP, NIS, and NIS+, the settings for Local/Makefile are:
+
+LOOKUP_LDAP=yes
+LOOKUP_NIS=yes
+LOOKUP_NISPLUS=yes
+
+and similar settings apply to the other lookup types. They are all listed in
+src/EDITME. In many cases the relevant include files and interface libraries
+need to be installed before compiling Exim. However, there are some optional
+lookup types (such as cdb) for which the code is entirely contained within
+Exim, and no external include files or libraries are required. When a lookup
+type is not included in the binary, attempts to configure Exim to use it cause
+run time configuration errors.
+
+Many systems now use a tool called pkg-config to encapsulate information about
+how to compile against a library; Exim has some initial support for being able
+to use pkg-config for lookups and authenticators. For any given makefile
+variable which starts "LOOKUP_" or "AUTH_", you can add a new variable with the
+"_PC" suffix in the name and assign as the value the name of the package to be
+queried. The results of querying via the pkg-config command will be added to
+the appropriate Makefile variables with "+=" directives, so your version of 
+make will need to support that syntax. For instance:
+
+LOOKUP_SQLITE=yes
+LOOKUP_SQLITE_PC=sqlite3
+AUTH_GSASL=yes
+AUTH_GSASL_PC=libgsasl
+AUTH_HEIMDAL_GSSAPI=yes
+AUTH_HEIMDAL_GSSAPI_PC=heimdal-gssapi
+
+Exim can be linked with an embedded Perl interpreter, allowing Perl subroutines
+to be called during string expansion. To enable this facility,
+
+EXIM_PERL=perl.o
+
+must be defined in Local/Makefile. Details of this facility are given in
+chapter 12.
+
+The location of the X11 libraries is something that varies a lot between
+operating systems, and there may be different versions of X11 to cope with.
+Exim itself makes no use of X11, but if you are compiling the Exim monitor, the
+X11 libraries must be available. The following three variables are set in OS/
+Makefile-Default:
+
+X11=/usr/X11R6
+XINCLUDE=-I$(X11)/include
+XLFLAGS=-L$(X11)/lib
+
+These are overridden in some of the operating-system configuration files. For
+example, in OS/Makefile-SunOS5 there is
+
+X11=/usr/openwin
+XINCLUDE=-I$(X11)/include
+XLFLAGS=-L$(X11)/lib -R$(X11)/lib
+
+If you need to override the default setting for your operating system, place a
+definition of all three of these variables into your Local/Makefile-<ostype>
+file.
+
+If you need to add any extra libraries to the link steps, these can be put in a
+variable called EXTRALIBS, which appears in all the link commands, but by
+default is not defined. In contrast, EXTRALIBS_EXIM is used only on the command
+for linking the main Exim binary, and not for any associated utilities.
+
+There is also DBMLIB, which appears in the link commands for binaries that use
+DBM functions (see also section 4.4). Finally, there is EXTRALIBS_EXIMON, which
+appears only in the link step for the Exim monitor binary, and which can be
+used, for example, to include additional X11 libraries.
+
+The make file copes with rebuilding Exim correctly if any of the configuration
+files are edited. However, if an optional configuration file is deleted, it is
+necessary to touch the associated non-optional file (that is, Local/Makefile or
+Local/eximon.conf) before rebuilding.
+
+
+4.14 OS-specific header files
+-----------------------------
+
+The OS directory contains a number of files with names of the form os.h-
+<ostype>. These are system-specific C header files that should not normally
+need to be changed. There is a list of macro settings that are recognized in
+the file OS/os.configuring, which should be consulted if you are porting Exim
+to a new operating system.
+
+
+4.15 Overriding build-time options for the monitor
+--------------------------------------------------
+
+A similar process is used for overriding things when building the Exim monitor,
+where the files that are involved are
+
+OS/eximon.conf-Default
+OS/eximon.conf-<ostype>
+Local/eximon.conf
+Local/eximon.conf-<ostype>
+Local/eximon.conf-<archtype>
+Local/eximon.conf-<ostype>-<archtype>
+
+As with Exim itself, the final three files need not exist, and in this case the
+OS/eximon.conf-<ostype> file is also optional. The default values in OS/
+eximon.conf-Default can be overridden dynamically by setting environment
+variables of the same name, preceded by EXIMON_. For example, setting
+EXIMON_LOG_DEPTH in the environment overrides the value of LOG_DEPTH at run
+time.
+
+
+4.16 Installing Exim binaries and scripts
+-----------------------------------------
+
+The command "make install" runs the exim_install script with no arguments. The
+script copies binaries and utility scripts into the directory whose name is
+specified by the BIN_DIRECTORY setting in Local/Makefile. The install script
+copies files only if they are newer than the files they are going to replace.
+The Exim binary is required to be owned by root and have the setuid bit set,
+for normal configurations. Therefore, you must run "make install" as root so
+that it can set up the Exim binary in this way. However, in some special
+situations (for example, if a host is doing no local deliveries) it may be
+possible to run Exim without making the binary setuid root (see chapter 54 for
+details).
+
+Exim's run time configuration file is named by the CONFIGURE_FILE setting in
+Local/Makefile. If this names a single file, and the file does not exist, the
+default configuration file src/configure.default is copied there by the
+installation script. If a run time configuration file already exists, it is
+left alone. If CONFIGURE_FILE is a colon-separated list, naming several
+alternative files, no default is installed.
+
+One change is made to the default configuration file when it is installed: the
+default configuration contains a router that references a system aliases file.
+The path to this file is set to the value specified by SYSTEM_ALIASES_FILE in
+Local/Makefile (/etc/aliases by default). If the system aliases file does not
+exist, the installation script creates it, and outputs a comment to the user.
+
+The created file contains no aliases, but it does contain comments about the
+aliases a site should normally have. Mail aliases have traditionally been kept
+in /etc/aliases. However, some operating systems are now using /etc/mail/
+aliases. You should check if yours is one of these, and change Exim's
+configuration if necessary.
+
+The default configuration uses the local host's name as the only local domain,
+and is set up to do local deliveries into the shared directory /var/mail,
+running as the local user. System aliases and .forward files in users' home
+directories are supported, but no NIS or NIS+ support is configured. Domains
+other than the name of the local host are routed using the DNS, with delivery
+over SMTP.
+
+It is possible to install Exim for special purposes (such as building a binary
+distribution) in a private part of the file system. You can do this by a
+command such as
+
+make DESTDIR=/some/directory/ install
+
+This has the effect of pre-pending the specified directory to all the file
+paths, except the name of the system aliases file that appears in the default
+configuration. (If a default alias file is created, its name is modified.) For
+backwards compatibility, ROOT is used if DESTDIR is not set, but this usage is
+deprecated.
+
+Running make install does not copy the Exim 4 conversion script convert4r4. You
+will probably run this only once if you are upgrading from Exim 3. None of the
+documentation files in the doc directory are copied, except for the info files
+when you have set INFO_DIRECTORY, as described in section 4.17 below.
+
+For the utility programs, old versions are renamed by adding the suffix .O to
+their names. The Exim binary itself, however, is handled differently. It is
+installed under a name that includes the version number and the compile number,
+for example exim-4.84-1. The script then arranges for a symbolic link called
+exim to point to the binary. If you are updating a previous version of Exim,
+the script takes care to ensure that the name exim is never absent from the
+directory (as seen by other processes).
+
+If you want to see what the make install will do before running it for real,
+you can pass the -n option to the installation script by this command:
+
+make INSTALL_ARG=-n install
+
+The contents of the variable INSTALL_ARG are passed to the installation script.
+You do not need to be root to run this test. Alternatively, you can run the
+installation script directly, but this must be from within the build directory.
+For example, from the top-level Exim directory you could use this command:
+
+(cd build-SunOS5-5.5.1-sparc; ../scripts/exim_install -n)
+
+There are two other options that can be supplied to the installation script.
+
+  * -no_chown bypasses the call to change the owner of the installed binary to
+    root, and the call to make it a setuid binary.
+
+  * -no_symlink bypasses the setting up of the symbolic link exim to the
+    installed binary.
+
+INSTALL_ARG can be used to pass these options to the script. For example:
+
+make INSTALL_ARG=-no_symlink install
+
+The installation script can also be given arguments specifying which files are
+to be copied. For example, to install just the Exim binary, and nothing else,
+without creating the symbolic link, you could use:
+
+make INSTALL_ARG='-no_symlink exim' install
+
+
+4.17 Installing info documentation
+----------------------------------
+
+Not all systems use the GNU info system for documentation, and for this reason,
+the Texinfo source of Exim's documentation is not included in the main
+distribution. Instead it is available separately from the ftp site (see section
+1.6).
+
+If you have defined INFO_DIRECTORY in Local/Makefile and the Texinfo source of
+the documentation is found in the source tree, running "make install"
+automatically builds the info files and installs them.
+
+
+4.18 Setting up the spool directory
+-----------------------------------
+
+When it starts up, Exim tries to create its spool directory if it does not
+exist. The Exim uid and gid are used for the owner and group of the spool
+directory. Sub-directories are automatically created in the spool directory as
+necessary.
+
+
+4.19 Testing
+------------
+
+Having installed Exim, you can check that the run time configuration file is
+syntactically valid by running the following command, which assumes that the
+Exim binary directory is within your PATH environment variable:
+
+exim -bV
+
+If there are any errors in the configuration file, Exim outputs error messages.
+Otherwise it outputs the version number and build date, the DBM library that is
+being used, and information about which drivers and other optional code modules
+are included in the binary. Some simple routing tests can be done by using the
+address testing option. For example,
+
+exim -bt <local username>
+
+should verify that it recognizes a local mailbox, and
+
+exim -bt <remote address>
+
+a remote one. Then try getting it to deliver mail, both locally and remotely.
+This can be done by passing messages directly to Exim, without going through a
+user agent. For example:
+
+exim -v postmaster@your.domain.example
+From: user@your.domain.example
+To: postmaster@your.domain.example
+Subject: Testing Exim
+
+This is a test message.
+^D
+
+The -v option causes Exim to output some verification of what it is doing. In
+this case you should see copies of three log lines, one for the message's
+arrival, one for its delivery, and one containing "Completed".
+
+If you encounter problems, look at Exim's log files (mainlog and paniclog) to
+see if there is any relevant information there. Another source of information
+is running Exim with debugging turned on, by specifying the -d option. If a
+message is stuck on Exim's spool, you can force a delivery with debugging
+turned on by a command of the form
+
+exim -d -M <exim-message-id>
+
+You must be root or an "admin user" in order to do this. The -d option produces
+rather a lot of output, but you can cut this down to specific areas. For
+example, if you use -d-all+route only the debugging information relevant to
+routing is included. (See the -d option in chapter 5 for more details.)
+
+One specific problem that has shown up on some sites is the inability to do
+local deliveries into a shared mailbox directory, because it does not have the
+"sticky bit" set on it. By default, Exim tries to create a lock file before
+writing to a mailbox file, and if it cannot create the lock file, the delivery
+is deferred. You can get round this either by setting the "sticky bit" on the
+directory, or by setting a specific group for local deliveries and allowing
+that group to create files in the directory (see the comments above the 
+local_delivery transport in the default configuration file). Another approach
+is to configure Exim not to use lock files, but just to rely on fcntl() locking
+instead. However, you should do this only if all user agents also use fcntl()
+locking. For further discussion of locking issues, see chapter 26.
+
+One thing that cannot be tested on a system that is already running an MTA is
+the receipt of incoming SMTP mail on the standard SMTP port. However, the -oX
+option can be used to run an Exim daemon that listens on some other port, or 
+inetd can be used to do this. The -bh option and the exim_checkaccess utility
+can be used to check out policy controls on incoming SMTP mail.
+
+Testing a new version on a system that is already running Exim can most easily
+be done by building a binary with a different CONFIGURE_FILE setting. From
+within the run time configuration, all other file and directory names that Exim
+uses can be altered, in order to keep it entirely clear of the production
+version.
+
+
+4.20 Replacing another MTA with Exim
+------------------------------------
+
+Building and installing Exim for the first time does not of itself put it in
+general use. The name by which the system's MTA is called by mail user agents
+is either /usr/sbin/sendmail, or /usr/lib/sendmail (depending on the operating
+system), and it is necessary to make this name point to the exim binary in
+order to get the user agents to pass messages to Exim. This is normally done by
+renaming any existing file and making /usr/sbin/sendmail or /usr/lib/sendmail a
+symbolic link to the exim binary. It is a good idea to remove any setuid
+privilege and executable status from the old MTA. It is then necessary to stop
+and restart the mailer daemon, if one is running.
+
+Some operating systems have introduced alternative ways of switching MTAs. For
+example, if you are running FreeBSD, you need to edit the file /etc/mail/
+mailer.conf instead of setting up a symbolic link as just described. A typical
+example of the contents of this file for running Exim is as follows:
+
+sendmail            /usr/exim/bin/exim
+send-mail           /usr/exim/bin/exim
+mailq               /usr/exim/bin/exim -bp
+newaliases          /usr/bin/true
+
+Once you have set up the symbolic link, or edited /etc/mail/mailer.conf, your
+Exim installation is "live". Check it by sending a message from your favourite
+user agent.
+
+You should consider what to tell your users about the change of MTA. Exim may
+have different capabilities to what was previously running, and there are
+various operational differences such as the text of messages produced by
+command line options and in bounce messages. If you allow your users to make
+use of Exim's filtering capabilities, you should make the document entitled 
+Exim's interface to mail filtering available to them.
+
+
+4.21 Upgrading Exim
+-------------------
+
+If you are already running Exim on your host, building and installing a new
+version automatically makes it available to MUAs, or any other programs that
+call the MTA directly. However, if you are running an Exim daemon, you do need
+to send it a HUP signal, to make it re-execute itself, and thereby pick up the
+new binary. You do not need to stop processing mail in order to install a new
+version of Exim. The install script does not modify an existing runtime
+configuration file.
+
+
+4.22 Stopping the Exim daemon on Solaris
+----------------------------------------
+
+The standard command for stopping the mailer daemon on Solaris is
+
+/etc/init.d/sendmail stop
+
+If /usr/lib/sendmail has been turned into a symbolic link, this script fails to
+stop Exim because it uses the command ps -e and greps the output for the text
+"sendmail"; this is not present because the actual program name (that is,
+"exim") is given by the ps command with these options. A solution is to replace
+the line that finds the process id with something like
+
+pid=`cat /var/spool/exim/exim-daemon.pid`
+
+to obtain the daemon's pid directly from the file that Exim saves it in.
+
+Note, however, that stopping the daemon does not "stop Exim". Messages can
+still be received from local processes, and if automatic delivery is configured
+(the normal case), deliveries will still occur.
+
+
+
+===============================================================================
+5. THE EXIM COMMAND LINE
+
+Exim's command line takes the standard Unix form of a sequence of options, each
+starting with a hyphen character, followed by a number of arguments. The
+options are compatible with the main options of Sendmail, and there are also
+some additional options, some of which are compatible with Smail 3. Certain
+combinations of options do not make sense, and provoke an error if used. The
+form of the arguments depends on which options are set.
+
+
+5.1 Setting options by program name
+-----------------------------------
+
+If Exim is called under the name mailq, it behaves as if the option -bp were
+present before any other options. The -bp option requests a listing of the
+contents of the mail queue on the standard output. This feature is for
+compatibility with some systems that contain a command of that name in one of
+the standard libraries, symbolically linked to /usr/sbin/sendmail or /usr/lib/
+sendmail.
+
+If Exim is called under the name rsmtp it behaves as if the option -bS were
+present before any other options, for compatibility with Smail. The -bS option
+is used for reading in a number of messages in batched SMTP format.
+
+If Exim is called under the name rmail it behaves as if the -i and -oee options
+were present before any other options, for compatibility with Smail. The name 
+rmail is used as an interface by some UUCP systems.
+
+If Exim is called under the name runq it behaves as if the option -q were
+present before any other options, for compatibility with Smail. The -q option
+causes a single queue runner process to be started.
+
+If Exim is called under the name newaliases it behaves as if the option -bi
+were present before any other options, for compatibility with Sendmail. This
+option is used for rebuilding Sendmail's alias file. Exim does not have the
+concept of a single alias file, but can be configured to run a given command if
+called with the -bi option.
+
+
+5.2 Trusted and admin users
+---------------------------
+
+Some Exim options are available only to trusted users and others are available
+only to admin users. In the description below, the phrases "Exim user" and
+"Exim group" mean the user and group defined by EXIM_USER and EXIM_GROUP in
+Local/Makefile or set by the exim_user and exim_group options. These do not
+necessarily have to use the name "exim".
+
+  * The trusted users are root, the Exim user, any user listed in the 
+    trusted_users configuration option, and any user whose current group or any
+    supplementary group is one of those listed in the trusted_groups
+    configuration option. Note that the Exim group is not automatically
+    trusted.
+
+    Trusted users are always permitted to use the -f option or a leading
+    "From " line to specify the envelope sender of a message that is passed to
+    Exim through the local interface (see the -bm and -f options below). See
+    the untrusted_set_sender option for a way of permitting non-trusted users
+    to set envelope senders.
+
+    For a trusted user, there is never any check on the contents of the From:
+    header line, and a Sender: line is never added. Furthermore, any existing 
+    Sender: line in incoming local (non-TCP/IP) messages is not removed.
+
+    Trusted users may also specify a host name, host address, interface
+    address, protocol name, ident value, and authentication data when
+    submitting a message locally. Thus, they are able to insert messages into
+    Exim's queue locally that have the characteristics of messages received
+    from a remote host. Untrusted users may in some circumstances use -f, but
+    can never set the other values that are available to trusted users.
+
+  * The admin users are root, the Exim user, and any user that is a member of
+    the Exim group or of any group listed in the admin_groups configuration
+    option. The current group does not have to be one of these groups.
+
+    Admin users are permitted to list the queue, and to carry out certain
+    operations on messages, for example, to force delivery failures. It is also
+    necessary to be an admin user in order to see the full information provided
+    by the Exim monitor, and full debugging output.
+
+    By default, the use of the -M, -q, -R, and -S options to cause Exim to
+    attempt delivery of messages on its queue is restricted to admin users.
+    However, this restriction can be relaxed by setting the prod_requires_admin
+    option false (that is, specifying no_prod_requires_admin).
+
+    Similarly, the use of the -bp option to list all the messages in the queue
+    is restricted to admin users unless queue_list_requires_admin is set false.
+
+Warning: If you configure your system so that admin users are able to edit
+Exim's configuration file, you are giving those users an easy way of getting
+root. There is further discussion of this issue at the start of chapter 6.
+
+
+5.3 Command line options
+------------------------
+
+Exim's command line options are described in alphabetical order below. If none
+of the options that specifies a specific action (such as starting the daemon or
+a queue runner, or testing an address, or receiving a message in a specific
+format, or listing the queue) are present, and there is at least one argument
+on the command line, -bm (accept a local message on the standard input, with
+the arguments specifying the recipients) is assumed. Otherwise, Exim outputs a
+brief message about itself and exits.
+
+--
+
+    This is a pseudo-option whose only purpose is to terminate the options and
+    therefore to cause subsequent command line items to be treated as arguments
+    rather than options, even if they begin with hyphens.
+
+--help
+
+    This option causes Exim to output a few sentences stating what it is. The
+    same output is generated if the Exim binary is called with no options and
+    no arguments.
+
+--version
+
+    This option is an alias for -bV and causes version information to be
+    displayed.
+
+-Ac, -Am
+
+    These options are used by Sendmail for selecting configuration files and
+    are ignored by Exim.
+
+-B<type>
+
+    This is a Sendmail option for selecting 7 or 8 bit processing. Exim is
+    8-bit clean; it ignores this option.
+
+-bd
+
+    This option runs Exim as a daemon, awaiting incoming SMTP connections.
+    Usually the -bd option is combined with the -q<time> option, to specify
+    that the daemon should also initiate periodic queue runs.
+
+    The -bd option can be used only by an admin user. If either of the -d
+    (debugging) or -v (verifying) options are set, the daemon does not
+    disconnect from the controlling terminal. When running this way, it can be
+    stopped by pressing ctrl-C.
+
+    By default, Exim listens for incoming connections to the standard SMTP port
+    on all the host's running interfaces. However, it is possible to listen on
+    other ports, on multiple ports, and only on specific interfaces. Chapter 13
+    contains a description of the options that control this.
+
+    When a listening daemon is started without the use of -oX (that is, without
+    overriding the normal configuration), it writes its process id to a file
+    called exim-daemon.pid in Exim's spool directory. This location can be
+    overridden by setting PID_FILE_PATH in Local/Makefile. The file is written
+    while Exim is still running as root.
+
+    When -oX is used on the command line to start a listening daemon, the
+    process id is not written to the normal pid file path. However, -oP can be
+    used to specify a path on the command line if a pid file is required.
+
+    The SIGHUP signal can be used to cause the daemon to re-execute itself.
+    This should be done whenever Exim's configuration file, or any file that is
+    incorporated into it by means of the .include facility, is changed, and
+    also whenever a new version of Exim is installed. It is not necessary to do
+    this when other files that are referenced from the configuration (for
+    example, alias files) are changed, because these are reread each time they
+    are used.
+
+-bdf
+
+    This option has the same effect as -bd except that it never disconnects
+    from the controlling terminal, even when no debugging is specified.
+
+-be
+
+    Run Exim in expansion testing mode. Exim discards its root privilege, to
+    prevent ordinary users from using this mode to read otherwise inaccessible
+    files. If no arguments are given, Exim runs interactively, prompting for
+    lines of data. Otherwise, it processes each argument in turn.
+
+    If Exim was built with USE_READLINE=yes in Local/Makefile, it tries to load
+    the libreadline library dynamically whenever the -be option is used without
+    command line arguments. If successful, it uses the readline() function,
+    which provides extensive line-editing facilities, for reading the test
+    data. A line history is supported.
+
+    Long expansion expressions can be split over several lines by using
+    backslash continuations. As in Exim's run time configuration, white space
+    at the start of continuation lines is ignored. Each argument or data line
+    is passed through the string expansion mechanism, and the result is output.
+    Variable values from the configuration file (for example, $qualify_domain)
+    are available, but no message-specific values (such as $sender_domain) are
+    set, because no message is being processed (but see -bem and -Mset).
+
+    Note: If you use this mechanism to test lookups, and you change the data
+    files or databases you are using, you must exit and restart Exim before
+    trying the same lookup again. Otherwise, because each Exim process caches
+    the results of lookups, you will just get the same result as before.
+
+-bem <filename>
+
+    This option operates like -be except that it must be followed by the name
+    of a file. For example:
+
+    exim -bem /tmp/testmessage
+
+    The file is read as a message (as if receiving a locally-submitted non-SMTP
+    message) before any of the test expansions are done. Thus, message-specific
+    variables such as $message_size and $header_from: are available. However,
+    no Received: header is added to the message. If the -t option is set,
+    recipients are read from the headers in the normal way, and are shown in
+    the $recipients variable. Note that recipients cannot be given on the
+    command line, because further arguments are taken as strings to expand
+    (just like -be).
+
+-bF <filename>
+
+    This option is the same as -bf except that it assumes that the filter being
+    tested is a system filter. The additional commands that are available only
+    in system filters are recognized.
+
+-bf <filename>
+
+    This option runs Exim in user filter testing mode; the file is the filter
+    file to be tested, and a test message must be supplied on the standard
+    input. If there are no message-dependent tests in the filter, an empty file
+    can be supplied.
+
+    If you want to test a system filter file, use -bF instead of -bf. You can
+    use both -bF and -bf on the same command, in order to test a system filter
+    and a user filter in the same run. For example:
+
+    exim -bF /system/filter -bf /user/filter </test/message
+
+    This is helpful when the system filter adds header lines or sets filter
+    variables that are used by the user filter.
+
+    If the test filter file does not begin with one of the special lines
+
+    # Exim filter
+    # Sieve filter
+
+    it is taken to be a normal .forward file, and is tested for validity under
+    that interpretation. See sections 22.4 to 22.6 for a description of the
+    possible contents of non-filter redirection lists.
+
+    The result of an Exim command that uses -bf, provided no errors are
+    detected, is a list of the actions that Exim would try to take if presented
+    with the message for real. More details of filter testing are given in the
+    separate document entitled Exim's interfaces to mail filtering.
+
+    When testing a filter file, the envelope sender can be set by the -f
+    option, or by a "From " line at the start of the test message. Various
+    parameters that would normally be taken from the envelope recipient address
+    of the message can be set by means of additional command line options (see
+    the next four options).
+
+-bfd <domain>
+
+    This sets the domain of the recipient address when a filter file is being
+    tested by means of the -bf option. The default is the value of
+    $qualify_domain.
+
+-bfl <local part>
+
+    This sets the local part of the recipient address when a filter file is
+    being tested by means of the -bf option. The default is the username of the
+    process that calls Exim. A local part should be specified with any prefix
+    or suffix stripped, because that is how it appears to the filter when a
+    message is actually being delivered.
+
+-bfp <prefix>
+
+    This sets the prefix of the local part of the recipient address when a
+    filter file is being tested by means of the -bf option. The default is an
+    empty prefix.
+
+-bfs <suffix>
+
+    This sets the suffix of the local part of the recipient address when a
+    filter file is being tested by means of the -bf option. The default is an
+    empty suffix.
+
+-bh <IP address>
+
+    This option runs a fake SMTP session as if from the given IP address, using
+    the standard input and output. The IP address may include a port number at
+    the end, after a full stop. For example:
+
+    exim -bh 10.9.8.7.1234
+    exim -bh fe80::a00:20ff:fe86:a061.5678
+
+    When an IPv6 address is given, it is converted into canonical form. In the
+    case of the second example above, the value of $sender_host_address after
+    conversion to the canonical form is
+    "fe80:0000:0000:0a00:20ff:fe86:a061.5678".
+
+    Comments as to what is going on are written to the standard error file.
+    These include lines beginning with "LOG" for anything that would have been
+    logged. This facility is provided for testing configuration options for
+    incoming messages, to make sure they implement the required policy. For
+    example, you can test your relay controls using -bh.
+
+    Warning 1: You can test features of the configuration that rely on ident
+    (RFC 1413) information by using the -oMt option. However, Exim cannot
+    actually perform an ident callout when testing using -bh because there is
+    no incoming SMTP connection.
+
+    Warning 2: Address verification callouts (see section 42.45) are also
+    skipped when testing using -bh. If you want these callouts to occur, use 
+    -bhc instead.
+
+    Messages supplied during the testing session are discarded, and nothing is
+    written to any of the real log files. There may be pauses when DNS (and
+    other) lookups are taking place, and of course these may time out. The -oMi
+    option can be used to specify a specific IP interface and port if this is
+    important, and -oMaa and -oMai can be used to set parameters as if the SMTP
+    session were authenticated.
+
+    The exim_checkaccess utility is a "packaged" version of -bh whose output
+    just states whether a given recipient address from a given host is
+    acceptable or not. See section 52.8.
+
+    Features such as authentication and encryption, where the client input is
+    not plain text, cannot easily be tested with -bh. Instead, you should use a
+    specialized SMTP test program such as swaks.
+
+-bhc <IP address>
+
+    This option operates in the same way as -bh, except that address
+    verification callouts are performed if required. This includes consulting
+    and updating the callout cache database.
+
+-bi
+
+    Sendmail interprets the -bi option as a request to rebuild its alias file.
+    Exim does not have the concept of a single alias file, and so it cannot
+    mimic this behaviour. However, calls to /usr/lib/sendmail with the -bi
+    option tend to appear in various scripts such as NIS make files, so the
+    option must be recognized.
+
+    If -bi is encountered, the command specified by the bi_command
+    configuration option is run, under the uid and gid of the caller of Exim.
+    If the -oA option is used, its value is passed to the command as an
+    argument. The command set by bi_command may not contain arguments. The
+    command can use the exim_dbmbuild utility, or some other means, to rebuild
+    alias files if this is required. If the bi_command option is not set,
+    calling Exim with -bi is a no-op.
+
+-bI:help
+
+    We shall provide various options starting "-bI:" for querying Exim for
+    information. The output of many of these will be intended for machine
+    consumption. This one is not. The -bI:help option asks Exim for a synopsis
+    of supported options beginning "-bI:". Use of any of these options shall
+    cause Exim to exit after producing the requested output.
+
+-bI:dscp
+
+    This option causes Exim to emit an alphabetically sorted list of all
+    recognised DSCP names.
+
+-bI:sieve
+
+    This option causes Exim to emit an alphabetically sorted list of all
+    supported Sieve protocol extensions on stdout, one per line. This is
+    anticipated to be useful for ManageSieve (RFC 5804) implementations, in
+    providing that protocol's "SIEVE" capability response line. As the precise
+    list may depend upon compile-time build options, which this option will
+    adapt to, this is the only way to guarantee a correct response.
+
+-bm
+
+    This option runs an Exim receiving process that accepts an incoming,
+    locally-generated message on the standard input. The recipients are given
+    as the command arguments (except when -t is also present - see below). Each
+    argument can be a comma-separated list of RFC 2822 addresses. This is the
+    default option for selecting the overall action of an Exim call; it is
+    assumed if no other conflicting option is present.
+
+    If any addresses in the message are unqualified (have no domain), they are
+    qualified by the values of the qualify_domain or qualify_recipient options,
+    as appropriate. The -bnq option (see below) provides a way of suppressing
+    this for special cases.
+
+    Policy checks on the contents of local messages can be enforced by means of
+    the non-SMTP ACL. See chapter 42 for details.
+
+    The return code is zero if the message is successfully accepted. Otherwise,
+    the action is controlled by the -oex option setting - see below.
+
+    The format of the message must be as defined in RFC 2822, except that, for
+    compatibility with Sendmail and Smail, a line in one of the forms
+
+    From sender Fri Jan  5 12:55 GMT 1997
+    From sender Fri, 5 Jan 97 12:55:01
+
+    (with the weekday optional, and possibly with additional text after the
+    date) is permitted to appear at the start of the message. There appears to
+    be no authoritative specification of the format of this line. Exim
+    recognizes it by matching against the regular expression defined by the 
+    uucp_from_pattern option, which can be changed if necessary.
+
+    The specified sender is treated as if it were given as the argument to the 
+    -f option, but if a -f option is also present, its argument is used in
+    preference to the address taken from the message. The caller of Exim must
+    be a trusted user for the sender of a message to be set in this way.
+
+-bmalware <filename>
+
+    This debugging option causes Exim to scan the given file, using the malware
+    scanning framework. The option of av_scanner influences this option, so if 
+    av_scanner's value is dependent upon an expansion then the expansion should
+    have defaults which apply to this invocation. ACLs are not invoked, so if 
+    av_scanner references an ACL variable then that variable will never be
+    populated and -bmalware will fail.
+
+    Exim will have changed working directory before resolving the filename, so
+    using fully qualified pathnames is advisable. Exim will be running as the
+    Exim user when it tries to open the file, rather than as the invoking user.
+    This option requires admin privileges.
+
+    The -bmalware option will not be extended to be more generally useful,
+    there are better tools for file-scanning. This option exists to help
+    administrators verify their Exim and AV scanner configuration.
+
+-bnq
+
+    By default, Exim automatically qualifies unqualified addresses (those
+    without domains) that appear in messages that are submitted locally (that
+    is, not over TCP/IP). This qualification applies both to addresses in
+    envelopes, and addresses in header lines. Sender addresses are qualified
+    using qualify_domain, and recipient addresses using qualify_recipient
+    (which defaults to the value of qualify_domain).
+
+    Sometimes, qualification is not wanted. For example, if -bS (batch SMTP) is
+    being used to re-submit messages that originally came from remote hosts
+    after content scanning, you probably do not want to qualify unqualified
+    addresses in header lines. (Such lines will be present only if you have not
+    enabled a header syntax check in the appropriate ACL.)
+
+    The -bnq option suppresses all qualification of unqualified addresses in
+    messages that originate on the local host. When this is used, unqualified
+    addresses in the envelope provoke errors (causing message rejection) and
+    unqualified addresses in header lines are left alone.
+
+-bP
+
+    If this option is given with no arguments, it causes the values of all
+    Exim's main configuration options to be written to the standard output. The
+    values of one or more specific options can be requested by giving their
+    names as arguments, for example:
+
+    exim -bP qualify_domain hold_domains
+
+    However, any option setting that is preceded by the word "hide" in the
+    configuration file is not shown in full, except to an admin user. For other
+    users, the output is as in this example:
+
+    mysql_servers = <value not displayable>
+
+    If configure_file is given as an argument, the name of the run time
+    configuration file is output. If a list of configuration files was
+    supplied, the value that is output here is the name of the file that was
+    actually used.
+
+    If the -n flag is given, then for most modes of -bP operation the name will
+    not be output.
+
+    If log_file_path or pid_file_path are given, the names of the directories
+    where log files and daemon pid files are written are output, respectively.
+    If these values are unset, log files are written in a sub-directory of the
+    spool directory called log, and the pid file is written directly into the
+    spool directory.
+
+    If -bP is followed by a name preceded by "+", for example,
+
+    exim -bP +local_domains
+
+    it searches for a matching named list of any type (domain, host, address,
+    or local part) and outputs what it finds.
+
+    If one of the words router, transport, or authenticator is given, followed
+    by the name of an appropriate driver instance, the option settings for that
+    driver are output. For example:
+
+    exim -bP transport local_delivery
+
+    The generic driver options are output first, followed by the driver's
+    private options. A list of the names of drivers of a particular type can be
+    obtained by using one of the words router_list, transport_list, or 
+    authenticator_list, and a complete list of all drivers with their option
+    settings can be obtained by using routers, transports, or authenticators.
+
+    If invoked by an admin user, then macro, macro_list and macros are
+    available, similarly to the drivers. Because macros are sometimes used for
+    storing passwords, this option is restricted. The output format is one item
+    per line.
+
+-bp
+
+    This option requests a listing of the contents of the mail queue on the
+    standard output. If the -bp option is followed by a list of message ids,
+    just those messages are listed. By default, this option can be used only by
+    an admin user. However, the queue_list_requires_admin option can be set
+    false to allow any user to see the queue.
+
+    Each message on the queue is displayed as in the following example:
+
+    25m  2.9K 0t5C6f-0000c8-00 <alice@wonderland.fict.example>
+              red.king@looking-glass.fict.example
+              <other addresses>
+
+    The first line contains the length of time the message has been on the
+    queue (in this case 25 minutes), the size of the message (2.9K), the unique
+    local identifier for the message, and the message sender, as contained in
+    the envelope. For bounce messages, the sender address is empty, and appears
+    as "<>". If the message was submitted locally by an untrusted user who
+    overrode the default sender address, the user's login name is shown in
+    parentheses before the sender address.
+
+    If the message is frozen (attempts to deliver it are suspended) then the
+    text "*** frozen ***" is displayed at the end of this line.
+
+    The recipients of the message (taken from the envelope, not the headers)
+    are displayed on subsequent lines. Those addresses to which the message has
+    already been delivered are marked with the letter D. If an original address
+    gets expanded into several addresses via an alias or forward file, the
+    original is displayed with a D only when deliveries for all of its child
+    addresses are complete.
+
+-bpa
+
+    This option operates like -bp, but in addition it shows delivered addresses
+    that were generated from the original top level address(es) in each message
+    by alias or forwarding operations. These addresses are flagged with "+D"
+    instead of just "D".
+
+-bpc
+
+    This option counts the number of messages on the queue, and writes the
+    total to the standard output. It is restricted to admin users, unless 
+    queue_list_requires_admin is set false.
+
+-bpr
+
+    This option operates like -bp, but the output is not sorted into
+    chronological order of message arrival. This can speed it up when there are
+    lots of messages on the queue, and is particularly useful if the output is
+    going to be post-processed in a way that doesn't need the sorting.
+
+-bpra
+
+    This option is a combination of -bpr and -bpa.
+
+-bpru
+
+    This option is a combination of -bpr and -bpu.
+
+-bpu
+
+    This option operates like -bp but shows only undelivered top-level
+    addresses for each message displayed. Addresses generated by aliasing or
+    forwarding are not shown, unless the message was deferred after processing
+    by a router with the one_time option set.
+
+-brt
+
+    This option is for testing retry rules, and it must be followed by up to
+    three arguments. It causes Exim to look for a retry rule that matches the
+    values and to write it to the standard output. For example:
+
+    exim -brt bach.comp.mus.example
+    Retry rule: *.comp.mus.example  F,2h,15m; F,4d,30m;
+
+    See chapter 32 for a description of Exim's retry rules. The first argument,
+    which is required, can be a complete address in the form local_part@domain,
+    or it can be just a domain name. If the second argument contains a dot, it
+    is interpreted as an optional second domain name; if no retry rule is found
+    for the first argument, the second is tried. This ties in with Exim's
+    behaviour when looking for retry rules for remote hosts - if no rule is
+    found that matches the host, one that matches the mail domain is sought.
+    Finally, an argument that is the name of a specific delivery error, as used
+    in setting up retry rules, can be given. For example:
+
+    exim -brt haydn.comp.mus.example quota_3d
+    Retry rule: *@haydn.comp.mus.example quota_3d  F,1h,15m
+
+-brw
+
+    This option is for testing address rewriting rules, and it must be followed
+    by a single argument, consisting of either a local part without a domain,
+    or a complete address with a fully qualified domain. Exim outputs how this
+    address would be rewritten for each possible place it might appear. See
+    chapter 31 for further details.
+
+-bS
+
+    This option is used for batched SMTP input, which is an alternative
+    interface for non-interactive local message submission. A number of
+    messages can be submitted in a single run. However, despite its name, this
+    is not really SMTP input. Exim reads each message's envelope from SMTP
+    commands on the standard input, but generates no responses. If the caller
+    is trusted, or untrusted_set_sender is set, the senders in the SMTP MAIL
+    commands are believed; otherwise the sender is always the caller of Exim.
+
+    The message itself is read from the standard input, in SMTP format (leading
+    dots doubled), terminated by a line containing just a single dot. An error
+    is provoked if the terminating dot is missing. A further message may then
+    follow.
+
+    As for other local message submissions, the contents of incoming batch SMTP
+    messages can be checked using the non-SMTP ACL (see chapter 42).
+    Unqualified addresses are automatically qualified using qualify_domain and 
+    qualify_recipient, as appropriate, unless the -bnq option is used.
+
+    Some other SMTP commands are recognized in the input. HELO and EHLO act as
+    RSET; VRFY, EXPN, ETRN, and HELP act as NOOP; QUIT quits, ignoring the rest
+    of the standard input.
+
+    If any error is encountered, reports are written to the standard output and
+    error streams, and Exim gives up immediately. The return code is 0 if no
+    error was detected; it is 1 if one or more messages were accepted before
+    the error was detected; otherwise it is 2.
+
+    More details of input using batched SMTP are given in section 47.11.
+
+-bs
+
+    This option causes Exim to accept one or more messages by reading SMTP
+    commands on the standard input, and producing SMTP replies on the standard
+    output. SMTP policy controls, as defined in ACLs (see chapter 42) are
+    applied. Some user agents use this interface as a way of passing
+    locally-generated messages to the MTA.
+
+    In this usage, if the caller of Exim is trusted, or untrusted_set_sender is
+    set, the senders of messages are taken from the SMTP MAIL commands.
+    Otherwise the content of these commands is ignored and the sender is set up
+    as the calling user. Unqualified addresses are automatically qualified
+    using qualify_domain and qualify_recipient, as appropriate, unless the -bnq
+    option is used.
+
+    The -bs option is also used to run Exim from inetd, as an alternative to
+    using a listening daemon. Exim can distinguish the two cases by checking
+    whether the standard input is a TCP/IP socket. When Exim is called from 
+    inetd, the source of the mail is assumed to be remote, and the comments
+    above concerning senders and qualification do not apply. In this situation,
+    Exim behaves in exactly the same way as it does when receiving a message
+    via the listening daemon.
+
+-bt
+
+    This option runs Exim in address testing mode, in which each argument is
+    taken as a recipient address to be tested for deliverability. The results
+    are written to the standard output. If a test fails, and the caller is not
+    an admin user, no details of the failure are output, because these might
+    contain sensitive information such as usernames and passwords for database
+    lookups.
+
+    If no arguments are given, Exim runs in an interactive manner, prompting
+    with a right angle bracket for addresses to be tested.
+
+    Unlike the -be test option, you cannot arrange for Exim to use the readline
+    () function, because it is running as root and there are security issues.
+
+    Each address is handled as if it were the recipient address of a message
+    (compare the -bv option). It is passed to the routers and the result is
+    written to the standard output. However, any router that has 
+    no_address_test set is bypassed. This can make -bt easier to use for
+    genuine routing tests if your first router passes everything to a scanner
+    program.
+
+    The return code is 2 if any address failed outright; it is 1 if no address
+    failed outright but at least one could not be resolved for some reason.
+    Return code 0 is given only when all addresses succeed.
+
+    Note: When actually delivering a message, Exim removes duplicate recipient
+    addresses after routing is complete, so that only one delivery takes place.
+    This does not happen when testing with -bt; the full results of routing are
+    always shown.
+
+    Warning: -bt can only do relatively simple testing. If any of the routers
+    in the configuration makes any tests on the sender address of a message, 
+    you can use the -f option to set an appropriate sender when running -bt
+    tests. Without it, the sender is assumed to be the calling user at the
+    default qualifying domain. However, if you have set up (for example)
+    routers whose behaviour depends on the contents of an incoming message, you
+    cannot test those conditions using -bt. The -N option provides a possible
+    way of doing such tests.
+
+-bV
+
+    This option causes Exim to write the current version number, compilation
+    number, and compilation date of the exim binary to the standard output. It
+    also lists the DBM library that is being used, the optional modules (such
+    as specific lookup types), the drivers that are included in the binary, and
+    the name of the run time configuration file that is in use.
+
+    As part of its operation, -bV causes Exim to read and syntax check its
+    configuration file. However, this is a static check only. It cannot check
+    values that are to be expanded. For example, although a misspelt ACL verb
+    is detected, an error in the verb's arguments is not. You cannot rely on 
+    -bV alone to discover (for example) all the typos in the configuration;
+    some realistic testing is needed. The -bh and -N options provide more
+    dynamic testing facilities.
+
+-bv
+
+    This option runs Exim in address verification mode, in which each argument
+    is taken as a recipient address to be verified by the routers. (This does
+    not involve any verification callouts). During normal operation,
+    verification happens mostly as a consequence processing a verify condition
+    in an ACL (see chapter 42). If you want to test an entire ACL, possibly
+    including callouts, see the -bh and -bhc options.
+
+    If verification fails, and the caller is not an admin user, no details of
+    the failure are output, because these might contain sensitive information
+    such as usernames and passwords for database lookups.
+
+    If no arguments are given, Exim runs in an interactive manner, prompting
+    with a right angle bracket for addresses to be verified.
+
+    Unlike the -be test option, you cannot arrange for Exim to use the readline
+    () function, because it is running as exim and there are security issues.
+
+    Verification differs from address testing (the -bt option) in that routers
+    that have no_verify set are skipped, and if the address is accepted by a
+    router that has fail_verify set, verification fails. The address is
+    verified as a recipient if -bv is used; to test verification for a sender
+    address, -bvs should be used.
+
+    If the -v option is not set, the output consists of a single line for each
+    address, stating whether it was verified or not, and giving a reason in the
+    latter case. Without -v, generating more than one address by redirection
+    causes verification to end successfully, without considering the generated
+    addresses. However, if just one address is generated, processing continues,
+    and the generated address must verify successfully for the overall
+    verification to succeed.
+
+    When -v is set, more details are given of how the address has been handled,
+    and in the case of address redirection, all the generated addresses are
+    also considered. Verification may succeed for some and fail for others.
+
+    The return code is 2 if any address failed outright; it is 1 if no address
+    failed outright but at least one could not be resolved for some reason.
+    Return code 0 is given only when all addresses succeed.
+
+    If any of the routers in the configuration makes any tests on the sender
+    address of a message, you should use the -f option to set an appropriate
+    sender when running -bv tests. Without it, the sender is assumed to be the
+    calling user at the default qualifying domain.
+
+-bvs
+
+    This option acts like -bv, but verifies the address as a sender rather than
+    a recipient address. This affects any rewriting and qualification that
+    might happen.
+
+-bw
+
+    This option runs Exim as a daemon, awaiting incoming SMTP connections,
+    similarly to the -bd option. All port specifications on the command-line
+    and in the configuration file are ignored. Queue-running may not be
+    specified.
+
+    In this mode, Exim expects to be passed a socket as fd 0 (stdin) which is
+    listening for connections. This permits the system to start up and have
+    inetd (or equivalent) listen on the SMTP ports, starting an Exim daemon for
+    each port only when the first connection is received.
+
+    If the option is given as -bw<time> then the time is a timeout, after which
+    the daemon will exit, which should cause inetd to listen once more.
+
+-C <filelist>
+
+    This option causes Exim to find the run time configuration file from the
+    given list instead of from the list specified by the CONFIGURE_FILE
+    compile-time setting. Usually, the list will consist of just a single file
+    name, but it can be a colon-separated list of names. In this case, the
+    first file that exists is used. Failure to open an existing file stops Exim
+    from proceeding any further along the list, and an error is generated.
+
+    When this option is used by a caller other than root, and the list is
+    different from the compiled-in list, Exim gives up its root privilege
+    immediately, and runs with the real and effective uid and gid set to those
+    of the caller. However, if a TRUSTED_CONFIG_LIST file is defined in Local/
+    Makefile, that file contains a list of full pathnames, one per line, for
+    configuration files which are trusted. Root privilege is retained for any
+    configuration file so listed, as long as the caller is the Exim user (or
+    the user specified in the CONFIGURE_OWNER option, if any), and as long as
+    the configuration file is not writeable by inappropriate users or groups.
+
+    Leaving TRUSTED_CONFIG_LIST unset precludes the possibility of testing a
+    configuration using -C right through message reception and delivery, even
+    if the caller is root. The reception works, but by that time, Exim is
+    running as the Exim user, so when it re-executes to regain privilege for
+    the delivery, the use of -C causes privilege to be lost. However, root can
+    test reception and delivery using two separate commands (one to put a
+    message on the queue, using -odq, and another to do the delivery, using -M
+    ).
+
+    If ALT_CONFIG_PREFIX is defined in Local/Makefile, it specifies a prefix
+    string with which any file named in a -C command line option must start. In
+    addition, the file name must not contain the sequence "/../". However, if
+    the value of the -C option is identical to the value of CONFIGURE_FILE in
+    Local/Makefile, Exim ignores -C and proceeds as usual. There is no default
+    setting for ALT_CONFIG_PREFIX; when it is unset, any file name can be used
+    with -C.
+
+    ALT_CONFIG_PREFIX can be used to confine alternative configuration files to
+    a directory to which only root has access. This prevents someone who has
+    broken into the Exim account from running a privileged Exim with an
+    arbitrary configuration file.
+
+    The -C facility is useful for ensuring that configuration files are
+    syntactically correct, but cannot be used for test deliveries, unless the
+    caller is privileged, or unless it is an exotic configuration that does not
+    require privilege. No check is made on the owner or group of the files
+    specified by this option.
+
+-D<macro>=<value>
+
+    This option can be used to override macro definitions in the configuration
+    file (see section 6.4). However, like -C, if it is used by an unprivileged
+    caller, it causes Exim to give up its root privilege. If DISABLE_D_OPTION
+    is defined in Local/Makefile, the use of -D is completely disabled, and its
+    use causes an immediate error exit.
+
+    If WHITELIST_D_MACROS is defined in Local/Makefile then it should be a
+    colon-separated list of macros which are considered safe and, if -D only
+    supplies macros from this list, and the values are acceptable, then Exim
+    will not give up root privilege if the caller is root, the Exim run-time
+    user, or the CONFIGURE_OWNER, if set. This is a transition mechanism and is
+    expected to be removed in the future. Acceptable values for the macros
+    satisfy the regexp: "^[A-Za-z0-9_/.-]*$"
+
+    The entire option (including equals sign if present) must all be within one
+    command line item. -D can be used to set the value of a macro to the empty
+    string, in which case the equals sign is optional. These two commands are
+    synonymous:
+
+    exim -DABC  ...
+    exim -DABC= ...
+
+    To include spaces in a macro definition item, quotes must be used. If you
+    use quotes, spaces are permitted around the macro name and the equals sign.
+    For example:
+
+    exim '-D ABC = something' ...
+
+    -D may be repeated up to 10 times on a command line.
+
+-d<debug options>
+
+    This option causes debugging information to be written to the standard
+    error stream. It is restricted to admin users because debugging output may
+    show database queries that contain password information. Also, the details
+    of users' filter files should be protected. If a non-admin user uses -d,
+    Exim writes an error message to the standard error stream and exits with a
+    non-zero return code.
+
+    When -d is used, -v is assumed. If -d is given on its own, a lot of
+    standard debugging data is output. This can be reduced, or increased to
+    include some more rarely needed information, by directly following -d with
+    a string made up of names preceded by plus or minus characters. These add
+    or remove sets of debugging data, respectively. For example, -d+filter adds
+    filter debugging, whereas -d-all+filter selects only filter debugging. Note
+    that no spaces are allowed in the debug setting. The available debugging
+    categories are:
+
+    acl             ACL interpretation
+    auth            authenticators
+    deliver         general delivery logic
+    dns             DNS lookups (see also resolver)
+    dnsbl           DNS black list (aka RBL) code
+    exec            arguments for execv() calls
+    expand          detailed debugging for string expansions
+    filter          filter handling
+    hints_lookup    hints data lookups
+    host_lookup     all types of name-to-IP address handling
+    ident           ident lookup
+    interface       lists of local interfaces
+    lists           matching things in lists
+    load            system load checks
+    local_scan      can be used by local_scan() (see chapter 44)
+    lookup          general lookup code and all lookups
+    memory          memory handling
+    pid             add pid to debug output lines
+    process_info    setting info for the process log
+    queue_run       queue runs
+    receive         general message reception logic
+    resolver        turn on the DNS resolver's debugging output
+    retry           retry handling
+    rewrite         address rewriting
+    route           address routing
+    timestamp       add timestamp to debug output lines
+    tls             TLS logic
+    transport       transports
+    uid             changes of uid/gid and looking up uid/gid
+    verify          address verification logic
+    all             almost all of the above (see below), and also -v
+
+    The "all" option excludes "memory" when used as "+all", but includes it for
+    "-all". The reason for this is that "+all" is something that people tend to
+    use when generating debug output for Exim maintainers. If "+memory" is
+    included, an awful lot of output that is very rarely of interest is
+    generated, so it now has to be explicitly requested. However, "-all" does
+    turn everything off.
+
+    The "resolver" option produces output only if the DNS resolver was compiled
+    with DEBUG enabled. This is not the case in some operating systems. Also,
+    unfortunately, debugging output from the DNS resolver is written to stdout
+    rather than stderr.
+
+    The default (-d with no argument) omits "expand", "filter", "interface",
+    "load", "memory", "pid", "resolver", and "timestamp". However, the "pid"
+    selector is forced when debugging is turned on for a daemon, which then
+    passes it on to any re-executed Exims. Exim also automatically adds the pid
+    to debug lines when several remote deliveries are run in parallel.
+
+    The "timestamp" selector causes the current time to be inserted at the
+    start of all debug output lines. This can be useful when trying to track
+    down delays in processing.
+
+    If the debug_print option is set in any driver, it produces output whenever
+    any debugging is selected, or if -v is used.
+
+-dd<debug options>
+
+    This option behaves exactly like -d except when used on a command that
+    starts a daemon process. In that case, debugging is turned off for the
+    subprocesses that the daemon creates. Thus, it is useful for monitoring the
+    behaviour of the daemon without creating as much output as full debugging
+    does.
+
+-dropcr
+
+    This is an obsolete option that is now a no-op. It used to affect the way
+    Exim handled CR and LF characters in incoming messages. What happens now is
+    described in section 46.2.
+
+-E
+
+    This option specifies that an incoming message is a locally-generated
+    delivery failure report. It is used internally by Exim when handling
+    delivery failures and is not intended for external use. Its only effect is
+    to stop Exim generating certain messages to the postmaster, as otherwise
+    message cascades could occur in some situations. As part of the same
+    option, a message id may follow the characters -E. If it does, the log
+    entry for the receipt of the new message contains the id, following "R=",
+    as a cross-reference.
+
+-ex
+
+    There are a number of Sendmail options starting with -oe which seem to be
+    called by various programs without the leading o in the option. For
+    example, the vacation program uses -eq. Exim treats all options of the form
+    -ex as synonymous with the corresponding -oex options.
+
+-F <string>
+
+    This option sets the sender's full name for use when a locally-generated
+    message is being accepted. In the absence of this option, the user's gecos
+    entry from the password data is used. As users are generally permitted to
+    alter their gecos entries, no security considerations are involved. White
+    space between -F and the <string> is optional.
+
+-f <address>
+
+    This option sets the address of the envelope sender of a locally-generated
+    message (also known as the return path). The option can normally be used
+    only by a trusted user, but untrusted_set_sender can be set to allow
+    untrusted users to use it.
+
+    Processes running as root or the Exim user are always trusted. Other
+    trusted users are defined by the trusted_users or trusted_groups options.
+    In the absence of -f, or if the caller is not trusted, the sender of a
+    local message is set to the caller's login name at the default qualify
+    domain.
+
+    There is one exception to the restriction on the use of -f: an empty sender
+    can be specified by any user, trusted or not, to create a message that can
+    never provoke a bounce. An empty sender can be specified either as an empty
+    string, or as a pair of angle brackets with nothing between them, as in
+    these examples of shell commands:
+
+    exim -f '<>' user@domain
+    exim -f "" user@domain
+
+    In addition, the use of -f is not restricted when testing a filter file
+    with -bf or when testing or verifying addresses using the -bt or -bv
+    options.
+
+    Allowing untrusted users to change the sender address does not of itself
+    make it possible to send anonymous mail. Exim still checks that the From:
+    header refers to the local user, and if it does not, it adds a Sender:
+    header, though this can be overridden by setting no_local_from_check.
+
+    White space between -f and the <address> is optional (that is, they can be
+    given as two arguments or one combined argument). The sender of a
+    locally-generated message can also be set (when permitted) by an initial
+    "From " line in the message - see the description of -bm above - but if -f
+    is also present, it overrides "From ".
+
+-G
+
+    This option is equivalent to an ACL applying:
+
+    control = suppress_local_fixups
+
+    for every message received. Note that Sendmail will complain about such bad
+    formatting, where Exim silently just does not fix it up. This may change in
+    future.
+
+    As this affects audit information, the caller must be a trusted user to use
+    this option.
+
+-h <number>
+
+    This option is accepted for compatibility with Sendmail, but has no effect.
+    (In Sendmail it overrides the "hop count" obtained by counting Received:
+    headers.)
+
+-i
+
+    This option, which has the same effect as -oi, specifies that a dot on a
+    line by itself should not terminate an incoming, non-SMTP message. I can
+    find no documentation for this option in Solaris 2.4 Sendmail, but the 
+    mailx command in Solaris 2.4 uses it. See also -ti.
+
+-L <tag>
+
+    This option is equivalent to setting syslog_processname in the config file
+    and setting log_file_path to "syslog". Its use is restricted to
+    administrators. The configuration file has to be read and parsed, to
+    determine access rights, before this is set and takes effect, so early
+    configuration file errors will not honour this flag.
+
+    The tag should not be longer than 32 characters.
+
+-M <message id> <message id> ...
+
+    This option requests Exim to run a delivery attempt on each message in
+    turn. If any of the messages are frozen, they are automatically thawed
+    before the delivery attempt. The settings of queue_domains, 
+    queue_smtp_domains, and hold_domains are ignored.
+
+    Retry hints for any of the addresses are overridden - Exim tries to deliver
+    even if the normal retry time has not yet been reached. This option
+    requires the caller to be an admin user. However, there is an option called
+    prod_requires_admin which can be set false to relax this restriction (and
+    also the same requirement for the -q, -R, and -S options).
+
+    The deliveries happen synchronously, that is, the original Exim process
+    does not terminate until all the delivery attempts have finished. No output
+    is produced unless there is a serious error. If you want to see what is
+    happening, use the -v option as well, or inspect Exim's main log.
+
+-Mar <message id> <address> <address> ...
+
+    This option requests Exim to add the addresses to the list of recipients of
+    the message ("ar" for "add recipients"). The first argument must be a
+    message id, and the remaining ones must be email addresses. However, if the
+    message is active (in the middle of a delivery attempt), it is not altered.
+    This option can be used only by an admin user.
+
+-MC <transport> <hostname> <sequence number> <message id>
+
+    This option is not intended for use by external callers. It is used
+    internally by Exim to invoke another instance of itself to deliver a
+    waiting message using an existing SMTP connection, which is passed as the
+    standard input. Details are given in chapter 47. This must be the final
+    option, and the caller must be root or the Exim user in order to use it.
+
+-MCA
+
+    This option is not intended for use by external callers. It is used
+    internally by Exim in conjunction with the -MC option. It signifies that
+    the connection to the remote host has been authenticated.
+
+-MCP
+
+    This option is not intended for use by external callers. It is used
+    internally by Exim in conjunction with the -MC option. It signifies that
+    the server to which Exim is connected supports pipelining.
+
+-MCQ <process id> <pipe fd>
+
+    This option is not intended for use by external callers. It is used
+    internally by Exim in conjunction with the -MC option when the original
+    delivery was started by a queue runner. It passes on the process id of the
+    queue runner, together with the file descriptor number of an open pipe.
+    Closure of the pipe signals the final completion of the sequence of
+    processes that are passing messages through the same SMTP connection.
+
+-MCS
+
+    This option is not intended for use by external callers. It is used
+    internally by Exim in conjunction with the -MC option, and passes on the
+    fact that the SMTP SIZE option should be used on messages delivered down
+    the existing connection.
+
+-MCT
+
+    This option is not intended for use by external callers. It is used
+    internally by Exim in conjunction with the -MC option, and passes on the
+    fact that the host to which Exim is connected supports TLS encryption.
+
+-Mc <message id> <message id> ...
+
+    This option requests Exim to run a delivery attempt on each message in
+    turn, but unlike the -M option, it does check for retry hints, and respects
+    any that are found. This option is not very useful to external callers. It
+    is provided mainly for internal use by Exim when it needs to re-invoke
+    itself in order to regain root privilege for a delivery (see chapter 54).
+    However, -Mc can be useful when testing, in order to run a delivery that
+    respects retry times and other options such as hold_domains that are
+    overridden when -M is used. Such a delivery does not count as a queue run.
+    If you want to run a specific delivery as if in a queue run, you should use
+    -q with a message id argument. A distinction between queue run deliveries
+    and other deliveries is made in one or two places.
+
+-Mes <message id> <address>
+
+    This option requests Exim to change the sender address in the message to
+    the given address, which must be a fully qualified address or "<>" ("es"
+    for "edit sender"). There must be exactly two arguments. The first argument
+    must be a message id, and the second one an email address. However, if the
+    message is active (in the middle of a delivery attempt), its status is not
+    altered. This option can be used only by an admin user.
+
+-Mf <message id> <message id> ...
+
+    This option requests Exim to mark each listed message as "frozen". This
+    prevents any delivery attempts taking place until the message is "thawed",
+    either manually or as a result of the auto_thaw configuration option.
+    However, if any of the messages are active (in the middle of a delivery
+    attempt), their status is not altered. This option can be used only by an
+    admin user.
+
+-Mg <message id> <message id> ...
+
+    This option requests Exim to give up trying to deliver the listed messages,
+    including any that are frozen. However, if any of the messages are active,
+    their status is not altered. For non-bounce messages, a delivery error
+    message is sent to the sender, containing the text "cancelled by
+    administrator". Bounce messages are just discarded. This option can be used
+    only by an admin user.
+
+-Mmad <message id> <message id> ...
+
+    This option requests Exim to mark all the recipient addresses in the
+    messages as already delivered ("mad" for "mark all delivered"). However, if
+    any message is active (in the middle of a delivery attempt), its status is
+    not altered. This option can be used only by an admin user.
+
+-Mmd <message id> <address> <address> ...
+
+    This option requests Exim to mark the given addresses as already delivered
+    ("md" for "mark delivered"). The first argument must be a message id, and
+    the remaining ones must be email addresses. These are matched to recipient
+    addresses in the message in a case-sensitive manner. If the message is
+    active (in the middle of a delivery attempt), its status is not altered.
+    This option can be used only by an admin user.
+
+-Mrm <message id> <message id> ...
+
+    This option requests Exim to remove the given messages from the queue. No
+    bounce messages are sent; each message is simply forgotten. However, if any
+    of the messages are active, their status is not altered. This option can be
+    used only by an admin user or by the user who originally caused the message
+    to be placed on the queue.
+
+-Mset <message id>
+
+    This option is useful only in conjunction with -be (that is, when testing
+    string expansions). Exim loads the given message from its spool before
+    doing the test expansions, thus setting message-specific variables such as
+    $message_size and the header variables. The $recipients variable is made
+    available. This feature is provided to make it easier to test expansions
+    that make use of these variables. However, this option can be used only by
+    an admin user. See also -bem.
+
+-Mt <message id> <message id> ...
+
+    This option requests Exim to "thaw" any of the listed messages that are
+    "frozen", so that delivery attempts can resume. However, if any of the
+    messages are active, their status is not altered. This option can be used
+    only by an admin user.
+
+-Mvb <message id>
+
+    This option causes the contents of the message body (-D) spool file to be
+    written to the standard output. This option can be used only by an admin
+    user.
+
+-Mvc <message id>
+
+    This option causes a copy of the complete message (header lines plus body)
+    to be written to the standard output in RFC 2822 format. This option can be
+    used only by an admin user.
+
+-Mvh <message id>
+
+    This option causes the contents of the message headers (-H) spool file to
+    be written to the standard output. This option can be used only by an admin
+    user.
+
+-Mvl <message id>
+
+    This option causes the contents of the message log spool file to be written
+    to the standard output. This option can be used only by an admin user.
+
+-m
+
+    This is apparently a synonym for -om that is accepted by Sendmail, so Exim
+    treats it that way too.
+
+-N
+
+    This is a debugging option that inhibits delivery of a message at the
+    transport level. It implies -v. Exim goes through many of the motions of
+    delivery - it just doesn't actually transport the message, but instead
+    behaves as if it had successfully done so. However, it does not make any
+    updates to the retry database, and the log entries for deliveries are
+    flagged with "*>" rather than "=>".
+
+    Because -N discards any message to which it applies, only root or the Exim
+    user are allowed to use it with -bd, -q, -R or -M. In other words, an
+    ordinary user can use it only when supplying an incoming message to which
+    it will apply. Although transportation never fails when -N is set, an
+    address may be deferred because of a configuration problem on a transport,
+    or a routing problem. Once -N has been used for a delivery attempt, it
+    sticks to the message, and applies to any subsequent delivery attempts that
+    may happen for that message.
+
+-n
+
+    This option is interpreted by Sendmail to mean "no aliasing". For normal
+    modes of operation, it is ignored by Exim. When combined with -bP it
+    suppresses the name of an option from being output.
+
+-O <data>
+
+    This option is interpreted by Sendmail to mean "set option". It is ignored
+    by Exim.
+
+-oA <file name>
+
+    This option is used by Sendmail in conjunction with -bi to specify an
+    alternative alias file name. Exim handles -bi differently; see the
+    description above.
+
+-oB <n>
+
+    This is a debugging option which limits the maximum number of messages that
+    can be delivered down one SMTP connection, overriding the value set in any 
+    smtp transport. If <n> is omitted, the limit is set to 1.
+
+-odb
+
+    This option applies to all modes in which Exim accepts incoming messages,
+    including the listening daemon. It requests "background" delivery of such
+    messages, which means that the accepting process automatically starts a
+    delivery process for each message received, but does not wait for the
+    delivery processes to finish.
+
+    When all the messages have been received, the reception process exits,
+    leaving the delivery processes to finish in their own time. The standard
+    output and error streams are closed at the start of each delivery process.
+    This is the default action if none of the -od options are present.
+
+    If one of the queueing options in the configuration file (queue_only or 
+    queue_only_file, for example) is in effect, -odb overrides it if 
+    queue_only_override is set true, which is the default setting. If 
+    queue_only_override is set false, -odb has no effect.
+
+-odf
+
+    This option requests "foreground" (synchronous) delivery when Exim has
+    accepted a locally-generated message. (For the daemon it is exactly the
+    same as -odb.) A delivery process is automatically started to deliver the
+    message, and Exim waits for it to complete before proceeding.
+
+    The original Exim reception process does not finish until the delivery
+    process for the final message has ended. The standard error stream is left
+    open during deliveries.
+
+    However, like -odb, this option has no effect if queue_only_override is
+    false and one of the queueing options in the configuration file is in
+    effect.
+
+    If there is a temporary delivery error during foreground delivery, the
+    message is left on the queue for later delivery, and the original reception
+    process exits. See chapter 50 for a way of setting up a restricted
+    configuration that never queues messages.
+
+-odi
+
+    This option is synonymous with -odf. It is provided for compatibility with
+    Sendmail.
+
+-odq
+
+    This option applies to all modes in which Exim accepts incoming messages,
+    including the listening daemon. It specifies that the accepting process
+    should not automatically start a delivery process for each message
+    received. Messages are placed on the queue, and remain there until a
+    subsequent queue runner process encounters them. There are several
+    configuration options (such as queue_only) that can be used to queue
+    incoming messages under certain conditions. This option overrides all of
+    them and also -odqs. It always forces queueing.
+
+-odqs
+
+    This option is a hybrid between -odb/-odi and -odq. However, like -odb and 
+    -odi, this option has no effect if queue_only_override is false and one of
+    the queueing options in the configuration file is in effect.
+
+    When -odqs does operate, a delivery process is started for each incoming
+    message, in the background by default, but in the foreground if -odi is
+    also present. The recipient addresses are routed, and local deliveries are
+    done in the normal way. However, if any SMTP deliveries are required, they
+    are not done at this time, so the message remains on the queue until a
+    subsequent queue runner process encounters it. Because routing was done,
+    Exim knows which messages are waiting for which hosts, and so a number of
+    messages for the same host can be sent in a single SMTP connection. The 
+    queue_smtp_domains configuration option has the same effect for specific
+    domains. See also the -qq option.
+
+-oee
+
+    If an error is detected while a non-SMTP message is being received (for
+    example, a malformed address), the error is reported to the sender in a
+    mail message.
+
+    Provided this error message is successfully sent, the Exim receiving
+    process exits with a return code of zero. If not, the return code is 2 if
+    the problem is that the original message has no recipients, or 1 for any
+    other error. This is the default -oex option if Exim is called as rmail.
+
+-oem
+
+    This is the same as -oee, except that Exim always exits with a non-zero
+    return code, whether or not the error message was successfully sent. This
+    is the default -oex option, unless Exim is called as rmail.
+
+-oep
+
+    If an error is detected while a non-SMTP message is being received, the
+    error is reported by writing a message to the standard error file (stderr).
+    The return code is 1 for all errors.
+
+-oeq
+
+    This option is supported for compatibility with Sendmail, but has the same
+    effect as -oep.
+
+-oew
+
+    This option is supported for compatibility with Sendmail, but has the same
+    effect as -oem.
+
+-oi
+
+    This option, which has the same effect as -i, specifies that a dot on a
+    line by itself should not terminate an incoming, non-SMTP message.
+    Otherwise, a single dot does terminate, though Exim does no special
+    processing for other lines that start with a dot. This option is set by
+    default if Exim is called as rmail. See also -ti.
+
+-oitrue
+
+    This option is treated as synonymous with -oi.
+
+-oMa <host address>
+
+    A number of options starting with -oM can be used to set values associated
+    with remote hosts on locally-submitted messages (that is, messages not
+    received over TCP/IP). These options can be used by any caller in
+    conjunction with the -bh, -be, -bf, -bF, -bt, or -bv testing options. In
+    other circumstances, they are ignored unless the caller is trusted.
+
+    The -oMa option sets the sender host address. This may include a port
+    number at the end, after a full stop (period). For example:
+
+    exim -bs -oMa 10.9.8.7.1234
+
+    An alternative syntax is to enclose the IP address in square brackets,
+    followed by a colon and the port number:
+
+    exim -bs -oMa [10.9.8.7]:1234
+
+    The IP address is placed in the $sender_host_address variable, and the
+    port, if present, in $sender_host_port. If both -oMa and -bh are present on
+    the command line, the sender host IP address is taken from whichever one is
+    last.
+
+-oMaa <name>
+
+    See -oMa above for general remarks about the -oM options. The -oMaa option
+    sets the value of $sender_host_authenticated (the authenticator name). See
+    chapter 33 for a discussion of SMTP authentication. This option can be used
+    with -bh and -bs to set up an authenticated SMTP session without actually
+    using the SMTP AUTH command.
+
+-oMai <string>
+
+    See -oMa above for general remarks about the -oM options. The -oMai option
+    sets the value of $authenticated_id (the id that was authenticated). This
+    overrides the default value (the caller's login id, except with -bh, where
+    there is no default) for messages from local sources. See chapter 33 for a
+    discussion of authenticated ids.
+
+-oMas <address>
+
+    See -oMa above for general remarks about the -oM options. The -oMas option
+    sets the authenticated sender value in $authenticated_sender. It overrides
+    the sender address that is created from the caller's login id for messages
+    from local sources, except when -bh is used, when there is no default. For
+    both -bh and -bs, an authenticated sender that is specified on a MAIL
+    command overrides this value. See chapter 33 for a discussion of
+    authenticated senders.
+
+-oMi <interface address>
+
+    See -oMa above for general remarks about the -oM options. The -oMi option
+    sets the IP interface address value. A port number may be included, using
+    the same syntax as for -oMa. The interface address is placed in
+    $received_ip_address and the port number, if present, in $received_port.
+
+-oMm <message reference>
+
+    See -oMa above for general remarks about the -oM options. The -oMm option
+    sets the message reference, e.g. message-id, and is logged during delivery.
+    This is useful when some kind of audit trail is required to tie messages
+    together. The format of the message reference is checked and will abort if
+    the format is invalid. The option will only be accepted if exim is running
+    in trusted mode, not as any regular user.
+
+    The best example of a message reference is when Exim sends a bounce
+    message. The message reference is the message-id of the original message
+    for which Exim is sending the bounce.
+
+-oMr <protocol name>
+
+    See -oMa above for general remarks about the -oM options. The -oMr option
+    sets the received protocol value that is stored in $received_protocol.
+    However, it does not apply (and is ignored) when -bh or -bs is used. For 
+    -bh, the protocol is forced to one of the standard SMTP protocol names (see
+    the description of $received_protocol in section 11.9). For -bs, the
+    protocol is always "local-" followed by one of those same names. For -bS
+    (batched SMTP) however, the protocol can be set by -oMr.
+
+-oMs <host name>
+
+    See -oMa above for general remarks about the -oM options. The -oMs option
+    sets the sender host name in $sender_host_name. When this option is
+    present, Exim does not attempt to look up a host name from an IP address;
+    it uses the name it is given.
+
+-oMt <ident string>
+
+    See -oMa above for general remarks about the -oM options. The -oMt option
+    sets the sender ident value in $sender_ident. The default setting for local
+    callers is the login id of the calling process, except when -bh is used,
+    when there is no default.
+
+-om
+
+    In Sendmail, this option means "me too", indicating that the sender of a
+    message should receive a copy of the message if the sender appears in an
+    alias expansion. Exim always does this, so the option does nothing.
+
+-oo
+
+    This option is ignored. In Sendmail it specifies "old style headers",
+    whatever that means.
+
+-oP <path>
+
+    This option is useful only in conjunction with -bd or -q with a time value.
+    The option specifies the file to which the process id of the daemon is
+    written. When -oX is used with -bd, or when -q with a time is used without 
+    -bd, this is the only way of causing Exim to write a pid file, because in
+    those cases, the normal pid file is not used.
+
+-or <time>
+
+    This option sets a timeout value for incoming non-SMTP messages. If it is
+    not set, Exim will wait forever for the standard input. The value can also
+    be set by the receive_timeout option. The format used for specifying times
+    is described in section 6.15.
+
+-os <time>
+
+    This option sets a timeout value for incoming SMTP messages. The timeout
+    applies to each SMTP command and block of data. The value can also be set
+    by the smtp_receive_timeout option; it defaults to 5 minutes. The format
+    used for specifying times is described in section 6.15.
+
+-ov
+
+    This option has exactly the same effect as -v.
+
+-oX <number or string>
+
+    This option is relevant only when the -bd (start listening daemon) option
+    is also given. It controls which ports and interfaces the daemon uses.
+    Details of the syntax, and how it interacts with configuration file
+    options, are given in chapter 13. When -oX is used to start a daemon, no
+    pid file is written unless -oP is also present to specify a pid file name.
+
+-pd
+
+    This option applies when an embedded Perl interpreter is linked with Exim
+    (see chapter 12). It overrides the setting of the perl_at_start option,
+    forcing the starting of the interpreter to be delayed until it is needed.
+
+-ps
+
+    This option applies when an embedded Perl interpreter is linked with Exim
+    (see chapter 12). It overrides the setting of the perl_at_start option,
+    forcing the starting of the interpreter to occur as soon as Exim is
+    started.
+
+-p<rval>:<sval>
+
+    For compatibility with Sendmail, this option is equivalent to
+
+    -oMr <rval> -oMs <sval>
+
+    It sets the incoming protocol and host name (for trusted callers). The host
+    name and its colon can be omitted when only the protocol is to be set. Note
+    the Exim already has two private options, -pd and -ps, that refer to
+    embedded Perl. It is therefore impossible to set a protocol value of "d" or
+    "s" using this option (but that does not seem a real limitation).
+
+-q
+
+    This option is normally restricted to admin users. However, there is a
+    configuration option called prod_requires_admin which can be set false to
+    relax this restriction (and also the same requirement for the -M, -R, and 
+    -S options).
+
+    The -q option starts one queue runner process. This scans the queue of
+    waiting messages, and runs a delivery process for each one in turn. It
+    waits for each delivery process to finish before starting the next one. A
+    delivery process may not actually do any deliveries if the retry times for
+    the addresses have not been reached. Use -qf (see below) if you want to
+    override this.
+
+    If the delivery process spawns other processes to deliver other messages
+    down passed SMTP connections, the queue runner waits for these to finish
+    before proceeding.
+
+    When all the queued messages have been considered, the original queue
+    runner process terminates. In other words, a single pass is made over the
+    waiting mail, one message at a time. Use -q with a time (see below) if you
+    want this to be repeated periodically.
+
+    Exim processes the waiting messages in an unpredictable order. It isn't
+    very random, but it is likely to be different each time, which is all that
+    matters. If one particular message screws up a remote MTA, other messages
+    to the same MTA have a chance of getting through if they get tried first.
+
+    It is possible to cause the messages to be processed in lexical message id
+    order, which is essentially the order in which they arrived, by setting the
+    queue_run_in_order option, but this is not recommended for normal use.
+
+-q<qflags>
+
+    The -q option may be followed by one or more flag letters that change its
+    behaviour. They are all optional, but if more than one is present, they
+    must appear in the correct order. Each flag is described in a separate item
+    below.
+
+-qq...
+
+    An option starting with -qq requests a two-stage queue run. In the first
+    stage, the queue is scanned as if the queue_smtp_domains option matched
+    every domain. Addresses are routed, local deliveries happen, but no remote
+    transports are run.
+
+    The hints database that remembers which messages are waiting for specific
+    hosts is updated, as if delivery to those hosts had been deferred. After
+    this is complete, a second, normal queue scan happens, with routing and
+    delivery taking place as normal. Messages that are routed to the same host
+    should mostly be delivered down a single SMTP connection because of the
+    hints that were set up during the first queue scan. This option may be
+    useful for hosts that are connected to the Internet intermittently.
+
+-q[q]i...
+
+    If the i flag is present, the queue runner runs delivery processes only for
+    those messages that haven't previously been tried. (i stands for "initial
+    delivery".) This can be helpful if you are putting messages on the queue
+    using -odq and want a queue runner just to process the new messages.
+
+-q[q][i]f...
+
+    If one f flag is present, a delivery attempt is forced for each non-frozen
+    message, whereas without f only those non-frozen addresses that have passed
+    their retry times are tried.
+
+-q[q][i]ff...
+
+    If ff is present, a delivery attempt is forced for every message, whether
+    frozen or not.
+
+-q[q][i][f[f]]l
+
+    The l (the letter "ell") flag specifies that only local deliveries are to
+    be done. If a message requires any remote deliveries, it remains on the
+    queue for later delivery.
+
+-q<qflags> <start id> <end id>
+
+    When scanning the queue, Exim can be made to skip over messages whose ids
+    are lexically less than a given value by following the -q option with a
+    starting message id. For example:
+
+    exim -q 0t5C6f-0000c8-00
+
+    Messages that arrived earlier than "0t5C6f-0000c8-00" are not inspected. If
+    a second message id is given, messages whose ids are lexically greater than
+    it are also skipped. If the same id is given twice, for example,
+
+    exim -q 0t5C6f-0000c8-00 0t5C6f-0000c8-00
+
+    just one delivery process is started, for that message. This differs from 
+    -M in that retry data is respected, and it also differs from -Mc in that it
+    counts as a delivery from a queue run. Note that the selection mechanism
+    does not affect the order in which the messages are scanned. There are also
+    other ways of selecting specific sets of messages for delivery in a queue
+    run - see -R and -S.
+
+-q<qflags><time>
+
+    When a time value is present, the -q option causes Exim to run as a daemon,
+    starting a queue runner process at intervals specified by the given time
+    value (whose format is described in section 6.15). This form of the -q
+    option is commonly combined with the -bd option, in which case a single
+    daemon process handles both functions. A common way of starting up a
+    combined daemon at system boot time is to use a command such as
+
+    /usr/exim/bin/exim -bd -q30m
+
+    Such a daemon listens for incoming SMTP calls, and also starts a queue
+    runner process every 30 minutes.
+
+    When a daemon is started by -q with a time value, but without -bd, no pid
+    file is written unless one is explicitly requested by the -oP option.
+
+-qR<rsflags> <string>
+
+    This option is synonymous with -R. It is provided for Sendmail
+    compatibility.
+
+-qS<rsflags> <string>
+
+    This option is synonymous with -S.
+
+-R<rsflags> <string>
+
+    The <rsflags> may be empty, in which case the white space before the string
+    is optional, unless the string is f, ff, r, rf, or rff, which are the
+    possible values for <rsflags>. White space is required if <rsflags> is not
+    empty.
+
+    This option is similar to -q with no time value, that is, it causes Exim to
+    perform a single queue run, except that, when scanning the messages on the
+    queue, Exim processes only those that have at least one undelivered
+    recipient address containing the given string, which is checked in a
+    case-independent way. If the <rsflags> start with r, <string> is
+    interpreted as a regular expression; otherwise it is a literal string.
+
+    If you want to do periodic queue runs for messages with specific
+    recipients, you can combine -R with -q and a time value. For example:
+
+    exim -q25m -R @special.domain.example
+
+    This example does a queue run for messages with recipients in the given
+    domain every 25 minutes. Any additional flags that are specified with -q
+    are applied to each queue run.
+
+    Once a message is selected for delivery by this mechanism, all its
+    addresses are processed. For the first selected message, Exim overrides any
+    retry information and forces a delivery attempt for each undelivered
+    address. This means that if delivery of any address in the first message is
+    successful, any existing retry information is deleted, and so delivery
+    attempts for that address in subsequently selected messages (which are
+    processed without forcing) will run. However, if delivery of any address
+    does not succeed, the retry information is updated, and in subsequently
+    selected messages, the failing address will be skipped.
+
+    If the <rsflags> contain f or ff, the delivery forcing applies to all
+    selected messages, not just the first; frozen messages are included when ff
+    is present.
+
+    The -R option makes it straightforward to initiate delivery of all messages
+    to a given domain after a host has been down for some time. When the SMTP
+    command ETRN is accepted by its ACL (see chapter 42), its default effect is
+    to run Exim with the -R option, but it can be configured to run an
+    arbitrary command instead.
+
+-r
+
+    This is a documented (for Sendmail) obsolete alternative name for -f.
+
+-S<rsflags> <string>
+
+    This option acts like -R except that it checks the string against each
+    message's sender instead of against the recipients. If -R is also set, both
+    conditions must be met for a message to be selected. If either of the
+    options has f or ff in its flags, the associated action is taken.
+
+-Tqt <times>
+
+    This is an option that is exclusively for use by the Exim testing suite. It
+    is not recognized when Exim is run normally. It allows for the setting up
+    of explicit "queue times" so that various warning/retry features can be
+    tested.
+
+-t
+
+    When Exim is receiving a locally-generated, non-SMTP message on its
+    standard input, the -t option causes the recipients of the message to be
+    obtained from the To:, Cc:, and Bcc: header lines in the message instead of
+    from the command arguments. The addresses are extracted before any
+    rewriting takes place and the Bcc: header line, if present, is then
+    removed.
+
+    If the command has any arguments, they specify addresses to which the
+    message is not to be delivered. That is, the argument addresses are removed
+    from the recipients list obtained from the headers. This is compatible with
+    Smail 3 and in accordance with the documented behaviour of several versions
+    of Sendmail, as described in man pages on a number of operating systems
+    (e.g. Solaris 8, IRIX 6.5, HP-UX 11). However, some versions of Sendmail 
+    add argument addresses to those obtained from the headers, and the O'Reilly
+    Sendmail book documents it that way. Exim can be made to add argument
+    addresses instead of subtracting them by setting the option 
+    extract_addresses_remove_arguments false.
+
+    If there are any Resent- header lines in the message, Exim extracts
+    recipients from all Resent-To:, Resent-Cc:, and Resent-Bcc: header lines
+    instead of from To:, Cc:, and Bcc:. This is for compatibility with Sendmail
+    and other MTAs. (Prior to release 4.20, Exim gave an error if -t was used
+    in conjunction with Resent- header lines.)
+
+    RFC 2822 talks about different sets of Resent- header lines (for when a
+    message is resent several times). The RFC also specifies that they should
+    be added at the front of the message, and separated by Received: lines. It
+    is not at all clear how -t should operate in the present of multiple sets,
+    nor indeed exactly what constitutes a "set". In practice, it seems that
+    MUAs do not follow the RFC. The Resent- lines are often added at the end of
+    the header, and if a message is resent more than once, it is common for the
+    original set of Resent- headers to be renamed as X-Resent- when a new set
+    is added. This removes any possible ambiguity.
+
+-ti
+
+    This option is exactly equivalent to -t -i. It is provided for
+    compatibility with Sendmail.
+
+-tls-on-connect
+
+    This option is available when Exim is compiled with TLS support. It forces
+    all incoming SMTP connections to behave as if the incoming port is listed
+    in the tls_on_connect_ports option. See section 13.4 and chapter 41 for
+    further details.
+
+-U
+
+    Sendmail uses this option for "initial message submission", and its
+    documentation states that in future releases, it may complain about
+    syntactically invalid messages rather than fixing them when this flag is
+    not set. Exim ignores this option.
+
+-v
+
+    This option causes Exim to write information to the standard error stream,
+    describing what it is doing. In particular, it shows the log lines for
+    receiving and delivering a message, and if an SMTP connection is made, the
+    SMTP dialogue is shown. Some of the log lines shown may not actually be
+    written to the log if the setting of log_selector discards them. Any
+    relevant selectors are shown with each log line. If none are shown, the
+    logging is unconditional.
+
+-x
+
+    AIX uses -x for a private purpose ("mail from a local mail program has
+    National Language Support extended characters in the body of the mail
+    item"). It sets -x when calling the MTA from its mail command. Exim ignores
+    this option.
+
+-X <logfile>
+
+    This option is interpreted by Sendmail to cause debug information to be
+    sent to the named file. It is ignored by Exim.
+
+
+
+===============================================================================
+6. THE EXIM RUN TIME CONFIGURATION FILE
+
+Exim uses a single run time configuration file that is read whenever an Exim
+binary is executed. Note that in normal operation, this happens frequently,
+because Exim is designed to operate in a distributed manner, without central
+control.
+
+If a syntax error is detected while reading the configuration file, Exim writes
+a message on the standard error, and exits with a non-zero return code. The
+message is also written to the panic log. Note: Only simple syntax errors can
+be detected at this time. The values of any expanded options are not checked
+until the expansion happens, even when the expansion does not actually alter
+the string.
+
+The name of the configuration file is compiled into the binary for security
+reasons, and is specified by the CONFIGURE_FILE compilation option. In most
+configurations, this specifies a single file. However, it is permitted to give
+a colon-separated list of file names, in which case Exim uses the first
+existing file in the list.
+
+The run time configuration file must be owned by root or by the user that is
+specified at compile time by the CONFIGURE_OWNER option (if set). The
+configuration file must not be world-writeable, or group-writeable unless its
+group is the root group or the one specified at compile time by the
+CONFIGURE_GROUP option.
+
+Warning: In a conventional configuration, where the Exim binary is setuid to
+root, anybody who is able to edit the run time configuration file has an easy
+way to run commands as root. If you specify a user or group in the
+CONFIGURE_OWNER or CONFIGURE_GROUP options, then that user and/or any users who
+are members of that group will trivially be able to obtain root privileges.
+
+Up to Exim version 4.72, the run time configuration file was also permitted to
+be writeable by the Exim user and/or group. That has been changed in Exim 4.73
+since it offered a simple privilege escalation for any attacker who managed to
+compromise the Exim user account.
+
+A default configuration file, which will work correctly in simple situations,
+is provided in the file src/configure.default. If CONFIGURE_FILE defines just
+one file name, the installation process copies the default configuration to a
+new file of that name if it did not previously exist. If CONFIGURE_FILE is a
+list, no default is automatically installed. Chapter 7 is a "walk-through"
+discussion of the default configuration.
+
+
+6.1 Using a different configuration file
+----------------------------------------
+
+A one-off alternate configuration can be specified by the -C command line
+option, which may specify a single file or a list of files. However, when -C is
+used, Exim gives up its root privilege, unless called by root (or unless the
+argument for -C is identical to the built-in value from CONFIGURE_FILE), or is
+listed in the TRUSTED_CONFIG_LIST file and the caller is the Exim user or the
+user specified in the CONFIGURE_OWNER setting. -C is useful mainly for checking
+the syntax of configuration files before installing them. No owner or group
+checks are done on a configuration file specified by -C, if root privilege has
+been dropped.
+
+Even the Exim user is not trusted to specify an arbitrary configuration file
+with the -C option to be used with root privileges, unless that file is listed
+in the TRUSTED_CONFIG_LIST file. This locks out the possibility of testing a
+configuration using -C right through message reception and delivery, even if
+the caller is root. The reception works, but by that time, Exim is running as
+the Exim user, so when it re-execs to regain privilege for the delivery, the
+use of -C causes privilege to be lost. However, root can test reception and
+delivery using two separate commands (one to put a message on the queue, using 
+-odq, and another to do the delivery, using -M).
+
+If ALT_CONFIG_PREFIX is defined in Local/Makefile, it specifies a prefix string
+with which any file named in a -C command line option must start. In addition,
+the file name must not contain the sequence "/../". There is no default setting
+for ALT_CONFIG_PREFIX; when it is unset, any file name can be used with -C.
+
+One-off changes to a configuration can be specified by the -D command line
+option, which defines and overrides values for macros used inside the
+configuration file. However, like -C, the use of this option by a
+non-privileged user causes Exim to discard its root privilege. If
+DISABLE_D_OPTION is defined in Local/Makefile, the use of -D is completely
+disabled, and its use causes an immediate error exit.
+
+The WHITELIST_D_MACROS option in Local/Makefile permits the binary builder to
+declare certain macro names trusted, such that root privilege will not
+necessarily be discarded. WHITELIST_D_MACROS defines a colon-separated list of
+macros which are considered safe and, if -D only supplies macros from this
+list, and the values are acceptable, then Exim will not give up root privilege
+if the caller is root, the Exim run-time user, or the CONFIGURE_OWNER, if set.
+This is a transition mechanism and is expected to be removed in the future.
+Acceptable values for the macros satisfy the regexp: "^[A-Za-z0-9_/.-]*$"
+
+Some sites may wish to use the same Exim binary on different machines that
+share a file system, but to use different configuration files on each machine.
+If CONFIGURE_FILE_USE_NODE is defined in Local/Makefile, Exim first looks for a
+file whose name is the configuration file name followed by a dot and the
+machine's node name, as obtained from the uname() function. If this file does
+not exist, the standard name is tried. This processing occurs for each file
+name in the list given by CONFIGURE_FILE or -C.
+
+In some esoteric situations different versions of Exim may be run under
+different effective uids and the CONFIGURE_FILE_USE_EUID is defined to help
+with this. See the comments in src/EDITME for details.
+
+
+6.2 Configuration file format
+-----------------------------
+
+Exim's configuration file is divided into a number of different parts. General
+option settings must always appear at the start of the file. The other parts
+are all optional, and may appear in any order. Each part other than the first
+is introduced by the word "begin" followed by the name of the part. The
+optional parts are:
+
+  * ACL: Access control lists for controlling incoming SMTP mail (see chapter
+    42).
+
+  * authenticators: Configuration settings for the authenticator drivers. These
+    are concerned with the SMTP AUTH command (see chapter 33).
+
+  * routers: Configuration settings for the router drivers. Routers process
+    addresses and determine how the message is to be delivered (see chapters 15
+    -22).
+
+  * transports: Configuration settings for the transport drivers. Transports
+    define mechanisms for copying messages to destinations (see chapters 24-30
+    ).
+
+  * retry: Retry rules, for use when a message cannot be delivered immediately.
+    If there is no retry section, or if it is empty (that is, no retry rules
+    are defined), Exim will not retry deliveries. In this situation, temporary
+    errors are treated the same as permanent errors. Retry rules are discussed
+    in chapter 32.
+
+  * rewrite: Global address rewriting rules, for use when a message arrives and
+    when new addresses are generated during delivery. Rewriting is discussed in
+    chapter 31.
+
+  * local_scan: Private options for the local_scan() function. If you want to
+    use this feature, you must set
+
+    LOCAL_SCAN_HAS_OPTIONS=yes
+
+    in Local/Makefile before building Exim. Details of the local_scan()
+    facility are given in chapter 44.
+
+Leading and trailing white space in configuration lines is always ignored.
+
+Blank lines in the file, and lines starting with a # character (ignoring
+leading white space) are treated as comments and are ignored. Note: A #
+character other than at the beginning of a line is not treated specially, and
+does not introduce a comment.
+
+Any non-comment line can be continued by ending it with a backslash. Note that
+the general rule for white space means that trailing white space after the
+backslash and leading white space at the start of continuation lines is
+ignored. Comment lines beginning with # (but not empty lines) may appear in the
+middle of a sequence of continuation lines.
+
+A convenient way to create a configuration file is to start from the default,
+which is supplied in src/configure.default, and add, delete, or change settings
+as required.
+
+The ACLs, retry rules, and rewriting rules have their own syntax which is
+described in chapters 42, 32, and 31, respectively. The other parts of the
+configuration file have some syntactic items in common, and these are described
+below, from section 6.10 onwards. Before that, the inclusion, macro, and
+conditional facilities are described.
+
+
+6.3 File inclusions in the configuration file
+---------------------------------------------
+
+You can include other files inside Exim's run time configuration file by using
+this syntax:
+
+.include <file name>
+.include_if_exists <file name>
+
+on a line by itself. Double quotes round the file name are optional. If you use
+the first form, a configuration error occurs if the file does not exist; the
+second form does nothing for non-existent files. In all cases, an absolute file
+name is required.
+
+Includes may be nested to any depth, but remember that Exim reads its
+configuration file often, so it is a good idea to keep them to a minimum. If
+you change the contents of an included file, you must HUP the daemon, because
+an included file is read only when the configuration itself is read.
+
+The processing of inclusions happens early, at a physical line level, so, like
+comment lines, an inclusion can be used in the middle of an option setting, for
+example:
+
+hosts_lookup = a.b.c \
+               .include /some/file
+
+Include processing happens after macro processing (see below). Its effect is to
+process the lines of the included file as if they occurred inline where the
+inclusion appears.
+
+
+6.4 Macros in the configuration file
+------------------------------------
+
+If a line in the main part of the configuration (that is, before the first
+"begin" line) begins with an upper case letter, it is taken as a macro
+definition, and must be of the form
+
+<name> = <rest of line>
+
+The name must consist of letters, digits, and underscores, and need not all be
+in upper case, though that is recommended. The rest of the line, including any
+continuations, is the replacement text, and has leading and trailing white
+space removed. Quotes are not removed. The replacement text can never end with
+a backslash character, but this doesn't seem to be a serious limitation.
+
+Macros may also be defined between router, transport, authenticator, or ACL
+definitions. They may not, however, be defined within an individual driver or
+ACL, or in the local_scan, retry, or rewrite sections of the configuration.
+
+
+6.5 Macro substitution
+----------------------
+
+Once a macro is defined, all subsequent lines in the file (and any included
+files) are scanned for the macro name; if there are several macros, the line is
+scanned for each in turn, in the order in which the macros are defined. The
+replacement text is not re-scanned for the current macro, though it is scanned
+for subsequently defined macros. For this reason, a macro name may not contain
+the name of a previously defined macro as a substring. You could, for example,
+define
+
+ABCD_XYZ = <something>
+ABCD = <something else>
+
+but putting the definitions in the opposite order would provoke a configuration
+error. Macro expansion is applied to individual physical lines from the file,
+before checking for line continuation or file inclusion (see above). If a line
+consists solely of a macro name, and the expansion of the macro is empty, the
+line is ignored. A macro at the start of a line may turn the line into a
+comment line or a ".include" line.
+
+
+6.6 Redefining macros
+---------------------
+
+Once defined, the value of a macro can be redefined later in the configuration
+(or in an included file). Redefinition is specified by using == instead of =.
+For example:
+
+MAC =  initial value
+...
+MAC == updated value
+
+Redefinition does not alter the order in which the macros are applied to the
+subsequent lines of the configuration file. It is still the same order in which
+the macros were originally defined. All that changes is the macro's value.
+Redefinition makes it possible to accumulate values. For example:
+
+MAC =  initial value
+...
+MAC == MAC and something added
+
+This can be helpful in situations where the configuration file is built from a
+number of other files.
+
+
+6.7 Overriding macro values
+---------------------------
+
+The values set for macros in the configuration file can be overridden by the -D
+command line option, but Exim gives up its root privilege when -D is used,
+unless called by root or the Exim user. A definition on the command line using
+the -D option causes all definitions and redefinitions within the file to be
+ignored.
+
+
+6.8 Example of macro usage
+--------------------------
+
+As an example of macro usage, consider a configuration where aliases are looked
+up in a MySQL database. It helps to keep the file less cluttered if long
+strings such as SQL statements are defined separately as macros, for example:
+
+ALIAS_QUERY = select mailbox from user where \
+              login='${quote_mysql:$local_part}';
+
+This can then be used in a redirect router setting like this:
+
+data = ${lookup mysql{ALIAS_QUERY}}
+
+In earlier versions of Exim macros were sometimes used for domain, host, or
+address lists. In Exim 4 these are handled better by named lists - see section
+10.5.
+
+
+6.9 Conditional skips in the configuration file
+-----------------------------------------------
+
+You can use the directives ".ifdef", ".ifndef", ".elifdef", ".elifndef",
+".else", and ".endif" to dynamically include or exclude portions of the
+configuration file. The processing happens whenever the file is read (that is,
+when an Exim binary starts to run).
+
+The implementation is very simple. Instances of the first four directives must
+be followed by text that includes the names of one or macros. The condition
+that is tested is whether or not any macro substitution has taken place in the
+line. Thus:
+
+.ifdef AAA
+message_size_limit = 50M
+.else
+message_size_limit = 100M
+.endif
+
+sets a message size limit of 50M if the macro "AAA" is defined, and 100M
+otherwise. If there is more than one macro named on the line, the condition is
+true if any of them are defined. That is, it is an "or" condition. To obtain an
+"and" condition, you need to use nested ".ifdef"s.
+
+Although you can use a macro expansion to generate one of these directives, it
+is not very useful, because the condition "there was a macro substitution in
+this line" will always be true.
+
+Text following ".else" and ".endif" is ignored, and can be used as comment to
+clarify complicated nestings.
+
+
+6.10 Common option syntax
+-------------------------
+
+For the main set of options, driver options, and local_scan() options, each
+setting is on a line by itself, and starts with a name consisting of lower-case
+letters and underscores. Many options require a data value, and in these cases
+the name must be followed by an equals sign (with optional white space) and
+then the value. For example:
+
+qualify_domain = mydomain.example.com
+
+Some option settings may contain sensitive data, for example, passwords for
+accessing databases. To stop non-admin users from using the -bP command line
+option to read these values, you can precede the option settings with the word
+"hide". For example:
+
+hide mysql_servers = localhost/users/admin/secret-password
+
+For non-admin users, such options are displayed like this:
+
+mysql_servers = <value not displayable>
+
+If "hide" is used on a driver option, it hides the value of that option on all
+instances of the same driver.
+
+The following sections describe the syntax used for the different data types
+that are found in option settings.
+
+
+6.11 Boolean options
+--------------------
+
+Options whose type is given as boolean are on/off switches. There are two
+different ways of specifying such options: with and without a data value. If
+the option name is specified on its own without data, the switch is turned on;
+if it is preceded by "no_" or "not_" the switch is turned off. However, boolean
+options may be followed by an equals sign and one of the words "true", "false",
+"yes", or "no", as an alternative syntax. For example, the following two
+settings have exactly the same effect:
+
+queue_only
+queue_only = true
+
+The following two lines also have the same (opposite) effect:
+
+no_queue_only
+queue_only = false
+
+You can use whichever syntax you prefer.
+
+
+6.12 Integer values
+-------------------
+
+If an option's type is given as "integer", the value can be given in decimal,
+hexadecimal, or octal. If it starts with a digit greater than zero, a decimal
+number is assumed. Otherwise, it is treated as an octal number unless it starts
+with the characters "0x", in which case the remainder is interpreted as a
+hexadecimal number.
+
+If an integer value is followed by the letter K, it is multiplied by 1024; if
+it is followed by the letter M, it is multiplied by 1024x1024. When the values
+of integer option settings are output, values which are an exact multiple of
+1024 or 1024x1024 are sometimes, but not always, printed using the letters K
+and M. The printing style is independent of the actual input format that was
+used.
+
+
+6.13 Octal integer values
+-------------------------
+
+If an option's type is given as "octal integer", its value is always
+interpreted as an octal number, whether or not it starts with the digit zero.
+Such options are always output in octal.
+
+
+6.14 Fixed point numbers
+------------------------
+
+If an option's type is given as "fixed-point", its value must be a decimal
+integer, optionally followed by a decimal point and up to three further digits.
+
+
+6.15 Time intervals
+-------------------
+
+A time interval is specified as a sequence of numbers, each followed by one of
+the following letters, with no intervening white space:
+
+    s seconds
+    m minutes
+    h hours
+    d days
+    w weeks
+
+For example, "3h50m" specifies 3 hours and 50 minutes. The values of time
+intervals are output in the same format. Exim does not restrict the values; it
+is perfectly acceptable, for example, to specify "90m" instead of "1h30m".
+
+
+6.16 String values
+------------------
+
+If an option's type is specified as "string", the value can be specified with
+or without double-quotes. If it does not start with a double-quote, the value
+consists of the remainder of the line plus any continuation lines, starting at
+the first character after any leading white space, with trailing white space
+removed, and with no interpretation of the characters in the string. Because
+Exim removes comment lines (those beginning with #) at an early stage, they can
+appear in the middle of a multi-line string. The following two settings are
+therefore equivalent:
+
+trusted_users = uucp:mail
+trusted_users = uucp:\
+                # This comment line is ignored
+                mail
+
+If a string does start with a double-quote, it must end with a closing
+double-quote, and any backslash characters other than those used for line
+continuation are interpreted as escape characters, as follows:
+
+    "\\"              single backslash
+    "\n"              newline
+    "\r"              carriage return
+    "\t"              tab
+    "\"<octal digits> up to 3 octal digits specify one character
+    "\x"<hex digits>  up to 2 hexadecimal digits specify one character
+
+If a backslash is followed by some other character, including a double-quote
+character, that character replaces the pair.
+
+Quoting is necessary only if you want to make use of the backslash escapes to
+insert special characters, or if you need to specify a value with leading or
+trailing spaces. These cases are rare, so quoting is almost never needed in
+current versions of Exim. In versions of Exim before 3.14, quoting was required
+in order to continue lines, so you may come across older configuration files
+and examples that apparently quote unnecessarily.
+
+
+6.17 Expanded strings
+---------------------
+
+Some strings in the configuration file are subjected to string expansion, by
+which means various parts of the string may be changed according to the
+circumstances (see chapter 11). The input syntax for such strings is as just
+described; in particular, the handling of backslashes in quoted strings is done
+as part of the input process, before expansion takes place. However, backslash
+is also an escape character for the expander, so any backslashes that are
+required for that reason must be doubled if they are within a quoted
+configuration string.
+
+
+6.18 User and group names
+-------------------------
+
+User and group names are specified as strings, using the syntax described
+above, but the strings are interpreted specially. A user or group name must
+either consist entirely of digits, or be a name that can be looked up using the
+getpwnam() or getgrnam() function, as appropriate.
+
+
+6.19 List construction
+----------------------
+
+The data for some configuration options is a list of items, with colon as the
+default separator. Many of these options are shown with type "string list" in
+the descriptions later in this document. Others are listed as "domain list",
+"host list", "address list", or "local part list". Syntactically, they are all
+the same; however, those other than "string list" are subject to particular
+kinds of interpretation, as described in chapter 10.
+
+In all these cases, the entire list is treated as a single string as far as the
+input syntax is concerned. The trusted_users setting in section 6.16 above is
+an example. If a colon is actually needed in an item in a list, it must be
+entered as two colons. Leading and trailing white space on each item in a list
+is ignored. This makes it possible to include items that start with a colon,
+and in particular, certain forms of IPv6 address. For example, the list
+
+local_interfaces = 127.0.0.1 : ::::1
+
+contains two IP addresses, the IPv4 address 127.0.0.1 and the IPv6 address ::1.
+
+Note: Although leading and trailing white space is ignored in individual list
+items, it is not ignored when parsing the list. The space after the first colon
+in the example above is necessary. If it were not there, the list would be
+interpreted as the two items 127.0.0.1:: and 1.
+
+
+6.20 Changing list separators
+-----------------------------
+
+Doubling colons in IPv6 addresses is an unwelcome chore, so a mechanism was
+introduced to allow the separator character to be changed. If a list begins
+with a left angle bracket, followed by any punctuation character, that
+character is used instead of colon as the list separator. For example, the list
+above can be rewritten to use a semicolon separator like this:
+
+local_interfaces = <; 127.0.0.1 ; ::1
+
+This facility applies to all lists, with the exception of the list in 
+log_file_path. It is recommended that the use of non-colon separators be
+confined to circumstances where they really are needed.
+
+It is also possible to use newline and other control characters (those with
+code values less than 32, plus DEL) as separators in lists. Such separators
+must be provided literally at the time the list is processed. For options that
+are string-expanded, you can write the separator using a normal escape
+sequence. This will be processed by the expander before the string is
+interpreted as a list. For example, if a newline-separated list of domains is
+generated by a lookup, you can process it directly by a line such as this:
+
+domains = <\n ${lookup mysql{.....}}
+
+This avoids having to change the list separator in such data. You are unlikely
+to want to use a control character as a separator in an option that is not
+expanded, because the value is literal text. However, it can be done by giving
+the value in quotes. For example:
+
+local_interfaces = "<\n 127.0.0.1 \n ::1"
+
+Unlike printing character separators, which can be included in list items by
+doubling, it is not possible to include a control character as data when it is
+set as the separator. Two such characters in succession are interpreted as
+enclosing an empty list item.
+
+
+6.21 Empty items in lists
+-------------------------
+
+An empty item at the end of a list is always ignored. In other words, trailing
+separator characters are ignored. Thus, the list in
+
+senders = user@domain :
+
+contains only a single item. If you want to include an empty string as one item
+in a list, it must not be the last item. For example, this list contains three
+items, the second of which is empty:
+
+senders = user1@domain : : user2@domain
+
+Note: There must be white space between the two colons, as otherwise they are
+interpreted as representing a single colon data character (and the list would
+then contain just one item). If you want to specify a list that contains just
+one, empty item, you can do it as in this example:
+
+senders = :
+
+In this case, the first item is empty, and the second is discarded because it
+is at the end of the list.
+
+
+6.22 Format of driver configurations
+------------------------------------
+
+There are separate parts in the configuration for defining routers, transports,
+and authenticators. In each part, you are defining a number of driver
+instances, each with its own set of options. Each driver instance is defined by
+a sequence of lines like this:
+
+<instance name>:
+  <option>
+  ...
+  <option>
+
+In the following example, the instance name is localuser, and it is followed by
+three options settings:
+
+localuser:
+  driver = accept
+  check_local_user
+  transport = local_delivery
+
+For each driver instance, you specify which Exim code module it uses - by the
+setting of the driver option - and (optionally) some configuration settings.
+For example, in the case of transports, if you want a transport to deliver with
+SMTP you would use the smtp driver; if you want to deliver to a local file you
+would use the appendfile driver. Each of the drivers is described in detail in
+its own separate chapter later in this manual.
+
+You can have several routers, transports, or authenticators that are based on
+the same underlying driver (each must have a different instance name).
+
+The order in which routers are defined is important, because addresses are
+passed to individual routers one by one, in order. The order in which
+transports are defined does not matter at all. The order in which
+authenticators are defined is used only when Exim, as a client, is searching
+them to find one that matches an authentication mechanism offered by the
+server.
+
+Within a driver instance definition, there are two kinds of option: generic and
+private. The generic options are those that apply to all drivers of the same
+type (that is, all routers, all transports or all authenticators). The driver
+option is a generic option that must appear in every definition. The private
+options are special for each driver, and none need appear, because they all
+have default values.
+
+The options may appear in any order, except that the driver option must precede
+any private options, since these depend on the particular driver. For this
+reason, it is recommended that driver always be the first option.
+
+Driver instance names, which are used for reference in log entries and
+elsewhere, can be any sequence of letters, digits, and underscores (starting
+with a letter) and must be unique among drivers of the same type. A router and
+a transport (for example) can each have the same name, but no two router
+instances can have the same name. The name of a driver instance should not be
+confused with the name of the underlying driver module. For example, the
+configuration lines:
+
+remote_smtp:
+  driver = smtp
+
+create an instance of the smtp transport driver whose name is remote_smtp. The
+same driver code can be used more than once, with different instance names and
+different option settings each time. A second instance of the smtp transport,
+with different options, might be defined thus:
+
+special_smtp:
+  driver = smtp
+  port = 1234
+  command_timeout = 10s
+
+The names remote_smtp and special_smtp would be used to reference these
+transport instances from routers, and these names would appear in log lines.
+
+Comment lines may be present in the middle of driver specifications. The full
+list of option settings for any particular driver instance, including all the
+defaulted values, can be extracted by making use of the -bP command line
+option.
+
+
+
+===============================================================================
+7. THE DEFAULT CONFIGURATION FILE
+
+The default configuration file supplied with Exim as src/configure.default is
+sufficient for a host with simple mail requirements. As an introduction to the
+way Exim is configured, this chapter "walks through" the default configuration,
+giving brief explanations of the settings. Detailed descriptions of the options
+are given in subsequent chapters. The default configuration file itself
+contains extensive comments about ways you might want to modify the initial
+settings. However, note that there are many options that are not mentioned at
+all in the default configuration.
+
+
+7.1 Main configuration settings
+-------------------------------
+
+The main (global) configuration option settings must always come first in the
+file. The first thing you'll see in the file, after some initial comments, is
+the line
+
+# primary_hostname =
+
+This is a commented-out setting of the primary_hostname option. Exim needs to
+know the official, fully qualified name of your host, and this is where you can
+specify it. However, in most cases you do not need to set this option. When it
+is unset, Exim uses the uname() system function to obtain the host name.
+
+The first three non-comment configuration lines are as follows:
+
+domainlist local_domains    = @
+domainlist relay_to_domains =
+hostlist   relay_from_hosts = 127.0.0.1
+
+These are not, in fact, option settings. They are definitions of two named
+domain lists and one named host list. Exim allows you to give names to lists of
+domains, hosts, and email addresses, in order to make it easier to manage the
+configuration file (see section 10.5).
+
+The first line defines a domain list called local_domains; this is used later
+in the configuration to identify domains that are to be delivered on the local
+host.
+
+There is just one item in this list, the string "@". This is a special form of
+entry which means "the name of the local host". Thus, if the local host is
+called a.host.example, mail to any.user@a.host.example is expected to be
+delivered locally. Because the local host's name is referenced indirectly, the
+same configuration file can be used on different hosts.
+
+The second line defines a domain list called relay_to_domains, but the list
+itself is empty. Later in the configuration we will come to the part that
+controls mail relaying through the local host; it allows relaying to any
+domains in this list. By default, therefore, no relaying on the basis of a mail
+domain is permitted.
+
+The third line defines a host list called relay_from_hosts. This list is used
+later in the configuration to permit relaying from any host or IP address that
+matches the list. The default contains just the IP address of the IPv4 loopback
+interface, which means that processes on the local host are able to submit mail
+for relaying by sending it over TCP/IP to that interface. No other hosts are
+permitted to submit messages for relaying.
+
+Just to be sure there's no misunderstanding: at this point in the configuration
+we aren't actually setting up any controls. We are just defining some domains
+and hosts that will be used in the controls that are specified later.
+
+The next two configuration lines are genuine option settings:
+
+acl_smtp_rcpt = acl_check_rcpt
+acl_smtp_data = acl_check_data
+
+These options specify Access Control Lists (ACLs) that are to be used during an
+incoming SMTP session for every recipient of a message (every RCPT command),
+and after the contents of the message have been received, respectively. The
+names of the lists are acl_check_rcpt and acl_check_data, and we will come to
+their definitions below, in the ACL section of the configuration. The RCPT ACL
+controls which recipients are accepted for an incoming message - if a
+configuration does not provide an ACL to check recipients, no SMTP mail can be
+accepted. The DATA ACL allows the contents of a message to be checked.
+
+Two commented-out option settings are next:
+
+# av_scanner = clamd:/tmp/clamd
+# spamd_address = 127.0.0.1 783
+
+These are example settings that can be used when Exim is compiled with the
+content-scanning extension. The first specifies the interface to the virus
+scanner, and the second specifies the interface to SpamAssassin. Further
+details are given in chapter 43.
+
+Three more commented-out option settings follow:
+
+# tls_advertise_hosts = *
+# tls_certificate = /etc/ssl/exim.crt
+# tls_privatekey = /etc/ssl/exim.pem
+
+These are example settings that can be used when Exim is compiled with support
+for TLS (aka SSL) as described in section 4.7. The first one specifies the list
+of clients that are allowed to use TLS when connecting to this server; in this
+case the wildcard means all clients. The other options specify where Exim
+should find its TLS certificate and private key, which together prove the
+server's identity to any clients that connect. More details are given in
+chapter 41.
+
+Another two commented-out option settings follow:
+
+# daemon_smtp_ports = 25 : 465 : 587
+# tls_on_connect_ports = 465
+
+These options provide better support for roaming users who wish to use this
+server for message submission. They are not much use unless you have turned on
+TLS (as described in the previous paragraph) and authentication (about which
+more in section 7.7). The usual SMTP port 25 is often blocked on end-user
+networks, so RFC 4409 specifies that message submission should use port 587
+instead. However some software (notably Microsoft Outlook) cannot be configured
+to use port 587 correctly, so these settings also enable the non-standard
+"smtps" (aka "ssmtp") port 465 (see section 13.4).
+
+Two more commented-out options settings follow:
+
+# qualify_domain =
+# qualify_recipient =
+
+The first of these specifies a domain that Exim uses when it constructs a
+complete email address from a local login name. This is often needed when Exim
+receives a message from a local process. If you do not set qualify_domain, the
+value of primary_hostname is used. If you set both of these options, you can
+have different qualification domains for sender and recipient addresses. If you
+set only the first one, its value is used in both cases.
+
+The following line must be uncommented if you want Exim to recognize addresses
+of the form user@[10.11.12.13] that is, with a "domain literal" (an IP address
+within square brackets) instead of a named domain.
+
+# allow_domain_literals
+
+The RFCs still require this form, but many people think that in the modern
+Internet it makes little sense to permit mail to be sent to specific hosts by
+quoting their IP addresses. This ancient format has been used by people who try
+to abuse hosts by using them for unwanted relaying. However, some people
+believe there are circumstances (for example, messages addressed to postmaster)
+where domain literals are still useful.
+
+The next configuration line is a kind of trigger guard:
+
+never_users = root
+
+It specifies that no delivery must ever be run as the root user. The normal
+convention is to set up root as an alias for the system administrator. This
+setting is a guard against slips in the configuration. The list of users
+specified by never_users is not, however, the complete list; the build-time
+configuration in Local/Makefile has an option called FIXED_NEVER_USERS
+specifying a list that cannot be overridden. The contents of never_users are
+added to this list. By default FIXED_NEVER_USERS also specifies root.
+
+When a remote host connects to Exim in order to send mail, the only information
+Exim has about the host's identity is its IP address. The next configuration
+line,
+
+host_lookup = *
+
+specifies that Exim should do a reverse DNS lookup on all incoming connections,
+in order to get a host name. This improves the quality of the logging
+information, but if you feel it is too expensive, you can remove it entirely,
+or restrict the lookup to hosts on "nearby" networks. Note that it is not
+always possible to find a host name from an IP address, because not all DNS
+reverse zones are maintained, and sometimes DNS servers are unreachable.
+
+The next two lines are concerned with ident callbacks, as defined by RFC 1413
+(hence their names):
+
+rfc1413_hosts = *
+rfc1413_query_timeout = 0s
+
+These settings cause Exim to avoid ident callbacks for all incoming SMTP calls.
+Few hosts offer RFC1413 service these days; calls have to be terminated by a
+timeout and this needlessly delays the startup of an incoming SMTP connection.
+If you have hosts for which you trust RFC1413 and need this information, you
+can change this.
+
+This line enables an efficiency SMTP option. It is negociated by clients and
+not expected to cause problems but can be disabled if needed.
+
+prdr_enable = true
+
+When Exim receives messages over SMTP connections, it expects all addresses to
+be fully qualified with a domain, as required by the SMTP definition. However,
+if you are running a server to which simple clients submit messages, you may
+find that they send unqualified addresses. The two commented-out options:
+
+# sender_unqualified_hosts =
+# recipient_unqualified_hosts =
+
+show how you can specify hosts that are permitted to send unqualified sender
+and recipient addresses, respectively.
+
+The percent_hack_domains option is also commented out:
+
+# percent_hack_domains =
+
+It provides a list of domains for which the "percent hack" is to operate. This
+is an almost obsolete form of explicit email routing. If you do not know
+anything about it, you can safely ignore this topic.
+
+The last two settings in the main part of the default configuration are
+concerned with messages that have been "frozen" on Exim's queue. When a message
+is frozen, Exim no longer continues to try to deliver it. Freezing occurs when
+a bounce message encounters a permanent failure because the sender address of
+the original message that caused the bounce is invalid, so the bounce cannot be
+delivered. This is probably the most common case, but there are also other
+conditions that cause freezing, and frozen messages are not always bounce
+messages.
+
+ignore_bounce_errors_after = 2d
+timeout_frozen_after = 7d
+
+The first of these options specifies that failing bounce messages are to be
+discarded after 2 days on the queue. The second specifies that any frozen
+message (whether a bounce message or not) is to be timed out (and discarded)
+after a week. In this configuration, the first setting ensures that no failing
+bounce message ever lasts a week.
+
+
+7.2 ACL configuration
+---------------------
+
+In the default configuration, the ACL section follows the main configuration.
+It starts with the line
+
+begin acl
+
+and it contains the definitions of two ACLs, called acl_check_rcpt and 
+acl_check_data, that were referenced in the settings of acl_smtp_rcpt and 
+acl_smtp_data above.
+
+The first ACL is used for every RCPT command in an incoming SMTP message. Each
+RCPT command specifies one of the message's recipients. The ACL statements are
+considered in order, until the recipient address is either accepted or
+rejected. The RCPT command is then accepted or rejected, according to the
+result of the ACL processing.
+
+acl_check_rcpt:
+
+This line, consisting of a name terminated by a colon, marks the start of the
+ACL, and names it.
+
+accept  hosts = :
+
+This ACL statement accepts the recipient if the sending host matches the list.
+But what does that strange list mean? It doesn't actually contain any host
+names or IP addresses. The presence of the colon puts an empty item in the
+list; Exim matches this only if the incoming message did not come from a remote
+host, because in that case, the remote hostname is empty. The colon is
+important. Without it, the list itself is empty, and can never match anything.
+
+What this statement is doing is to accept unconditionally all recipients in
+messages that are submitted by SMTP from local processes using the standard
+input and output (that is, not using TCP/IP). A number of MUAs operate in this
+manner.
+
+deny    message       = Restricted characters in address
+        domains       = +local_domains
+        local_parts   = ^[.] : ^.*[@%!/|]
+
+deny    message       = Restricted characters in address
+        domains       = !+local_domains
+        local_parts   = ^[./|] : ^.*[@%!] : ^.*/\\.\\./
+
+These statements are concerned with local parts that contain any of the
+characters "@", "%", "!", "/", "|", or dots in unusual places. Although these
+characters are entirely legal in local parts (in the case of "@" and leading
+dots, only if correctly quoted), they do not commonly occur in Internet mail
+addresses.
+
+The first three have in the past been associated with explicitly routed
+addresses (percent is still sometimes used - see the percent_hack_domains
+option). Addresses containing these characters are regularly tried by spammers
+in an attempt to bypass relaying restrictions, and also by open relay testing
+programs. Unless you really need them it is safest to reject these characters
+at this early stage. This configuration is heavy-handed in rejecting these
+characters for all messages it accepts from remote hosts. This is a deliberate
+policy of being as safe as possible.
+
+The first rule above is stricter, and is applied to messages that are addressed
+to one of the local domains handled by this host. This is implemented by the
+first condition, which restricts it to domains that are listed in the 
+local_domains domain list. The "+" character is used to indicate a reference to
+a named list. In this configuration, there is just one domain in local_domains,
+but in general there may be many.
+
+The second condition on the first statement uses two regular expressions to
+block local parts that begin with a dot or contain "@", "%", "!", "/", or "|".
+If you have local accounts that include these characters, you will have to
+modify this rule.
+
+Empty components (two dots in a row) are not valid in RFC 2822, but Exim allows
+them because they have been encountered in practice. (Consider the common
+convention of local parts constructed as "
+first-initial.second-initial.family-name" when applied to someone like the
+author of Exim, who has no second initial.) However, a local part starting with
+a dot or containing "/../" can cause trouble if it is used as part of a file
+name (for example, for a mailing list). This is also true for local parts that
+contain slashes. A pipe symbol can also be troublesome if the local part is
+incorporated unthinkingly into a shell command line.
+
+The second rule above applies to all other domains, and is less strict. This
+allows your own users to send outgoing messages to sites that use slashes and
+vertical bars in their local parts. It blocks local parts that begin with a
+dot, slash, or vertical bar, but allows these characters within the local part.
+However, the sequence "/../" is barred. The use of "@", "%", and "!" is
+blocked, as before. The motivation here is to prevent your users (or your
+users' viruses) from mounting certain kinds of attack on remote sites.
+
+accept  local_parts   = postmaster
+        domains       = +local_domains
+
+This statement, which has two conditions, accepts an incoming address if the
+local part is postmaster and the domain is one of those listed in the 
+local_domains domain list. The "+" character is used to indicate a reference to
+a named list. In this configuration, there is just one domain in local_domains,
+but in general there may be many.
+
+The presence of this statement means that mail to postmaster is never blocked
+by any of the subsequent tests. This can be helpful while sorting out problems
+in cases where the subsequent tests are incorrectly denying access.
+
+require verify        = sender
+
+This statement requires the sender address to be verified before any subsequent
+ACL statement can be used. If verification fails, the incoming recipient
+address is refused. Verification consists of trying to route the address, to
+see if a bounce message could be delivered to it. In the case of remote
+addresses, basic verification checks only the domain, but callouts can be used
+for more verification if required. Section 42.44 discusses the details of
+address verification.
+
+accept  hosts         = +relay_from_hosts
+        control       = submission
+
+This statement accepts the address if the message is coming from one of the
+hosts that are defined as being allowed to relay through this host. Recipient
+verification is omitted here, because in many cases the clients are dumb MUAs
+that do not cope well with SMTP error responses. For the same reason, the
+second line specifies "submission mode" for messages that are accepted. This is
+described in detail in section 46.1; it causes Exim to fix messages that are
+deficient in some way, for example, because they lack a Date: header line. If
+you are actually relaying out from MTAs, you should probably add recipient
+verification here, and disable submission mode.
+
+accept  authenticated = *
+        control       = submission
+
+This statement accepts the address if the client host has authenticated itself.
+Submission mode is again specified, on the grounds that such messages are most
+likely to come from MUAs. The default configuration does not define any
+authenticators, though it does include some nearly complete commented-out
+examples described in 7.7. This means that no client can in fact authenticate
+until you complete the authenticator definitions.
+
+require message = relay not permitted
+        domains = +local_domains : +relay_to_domains
+
+This statement rejects the address if its domain is neither a local domain nor
+one of the domains for which this host is a relay.
+
+require verify = recipient
+
+This statement requires the recipient address to be verified; if verification
+fails, the address is rejected.
+
+# deny    message     = rejected because $sender_host_address \
+#                       is in a black list at $dnslist_domain\n\
+#                       $dnslist_text
+#         dnslists    = black.list.example
+#
+# warn    dnslists    = black.list.example
+#         add_header  = X-Warning: $sender_host_address is in \
+#                       a black list at $dnslist_domain
+#         log_message = found in $dnslist_domain
+
+These commented-out lines are examples of how you could configure Exim to check
+sending hosts against a DNS black list. The first statement rejects messages
+from blacklisted hosts, whereas the second just inserts a warning header line.
+
+# require verify = csa
+
+This commented-out line is an example of how you could turn on client SMTP
+authorization (CSA) checking. Such checks do DNS lookups for special SRV
+records.
+
+accept
+
+The final statement in the first ACL unconditionally accepts any recipient
+address that has successfully passed all the previous tests.
+
+acl_check_data:
+
+This line marks the start of the second ACL, and names it. Most of the contents
+of this ACL are commented out:
+
+# deny    malware   = *
+#         message   = This message contains a virus \
+#                     ($malware_name).
+
+These lines are examples of how to arrange for messages to be scanned for
+viruses when Exim has been compiled with the content-scanning extension, and a
+suitable virus scanner is installed. If the message is found to contain a
+virus, it is rejected with the given custom error message.
+
+# warn    spam      = nobody
+#         message   = X-Spam_score: $spam_score\n\
+#                     X-Spam_score_int: $spam_score_int\n\
+#                     X-Spam_bar: $spam_bar\n\
+#                     X-Spam_report: $spam_report
+
+These lines are an example of how to arrange for messages to be scanned by
+SpamAssassin when Exim has been compiled with the content-scanning extension,
+and SpamAssassin has been installed. The SpamAssassin check is run with
+"nobody" as its user parameter, and the results are added to the message as a
+series of extra header line. In this case, the message is not rejected,
+whatever the spam score.
+
+accept
+
+This final line in the DATA ACL accepts the message unconditionally.
+
+
+7.3 Router configuration
+------------------------
+
+The router configuration comes next in the default configuration, introduced by
+the line
+
+begin routers
+
+Routers are the modules in Exim that make decisions about where to send
+messages. An address is passed to each router in turn, until it is either
+accepted, or failed. This means that the order in which you define the routers
+matters. Each router is fully described in its own chapter later in this
+manual. Here we give only brief overviews.
+
+# domain_literal:
+#   driver = ipliteral
+#   domains = !+local_domains
+#   transport = remote_smtp
+
+This router is commented out because the majority of sites do not want to
+support domain literal addresses (those of the form user@[10.9.8.7]). If you
+uncomment this router, you also need to uncomment the setting of 
+allow_domain_literals in the main part of the configuration.
+
+dnslookup:
+  driver = dnslookup
+  domains = ! +local_domains
+  transport = remote_smtp
+  ignore_target_hosts = 0.0.0.0 : 127.0.0.0/8
+  no_more
+
+The first uncommented router handles addresses that do not involve any local
+domains. This is specified by the line
+
+domains = ! +local_domains
+
+The domains option lists the domains to which this router applies, but the
+exclamation mark is a negation sign, so the router is used only for domains
+that are not in the domain list called local_domains (which was defined at the
+start of the configuration). The plus sign before local_domains indicates that
+it is referring to a named list. Addresses in other domains are passed on to
+the following routers.
+
+The name of the router driver is dnslookup, and is specified by the driver
+option. Do not be confused by the fact that the name of this router instance is
+the same as the name of the driver. The instance name is arbitrary, but the
+name set in the driver option must be one of the driver modules that is in the
+Exim binary.
+
+The dnslookup router routes addresses by looking up their domains in the DNS in
+order to obtain a list of hosts to which the address is routed. If the router
+succeeds, the address is queued for the remote_smtp transport, as specified by
+the transport option. If the router does not find the domain in the DNS, no
+further routers are tried because of the no_more setting, so the address fails
+and is bounced.
+
+The ignore_target_hosts option specifies a list of IP addresses that are to be
+entirely ignored. This option is present because a number of cases have been
+encountered where MX records in the DNS point to host names whose IP addresses
+are 0.0.0.0 or are in the 127 subnet (typically 127.0.0.1). Completely ignoring
+these IP addresses causes Exim to fail to route the email address, so it
+bounces. Otherwise, Exim would log a routing problem, and continue to try to
+deliver the message periodically until the address timed out.
+
+system_aliases:
+  driver = redirect
+  allow_fail
+  allow_defer
+  data = ${lookup{$local_part}lsearch{/etc/aliases}}
+# user = exim
+  file_transport = address_file
+  pipe_transport = address_pipe
+
+Control reaches this and subsequent routers only for addresses in the local
+domains. This router checks to see whether the local part is defined as an
+alias in the /etc/aliases file, and if so, redirects it according to the data
+that it looks up from that file. If no data is found for the local part, the
+value of the data option is empty, causing the address to be passed to the next
+router.
+
+/etc/aliases is a conventional name for the system aliases file that is often
+used. That is why it is referenced by from the default configuration file.
+However, you can change this by setting SYSTEM_ALIASES_FILE in Local/Makefile
+before building Exim.
+
+userforward:
+  driver = redirect
+  check_local_user
+# local_part_suffix = +* : -*
+# local_part_suffix_optional
+  file = $home/.forward
+# allow_filter
+  no_verify
+  no_expn
+  check_ancestor
+  file_transport = address_file
+  pipe_transport = address_pipe
+  reply_transport = address_reply
+
+This is the most complicated router in the default configuration. It is another
+redirection router, but this time it is looking for forwarding data set up by
+individual users. The check_local_user setting specifies a check that the local
+part of the address is the login name of a local user. If it is not, the router
+is skipped. The two commented options that follow check_local_user, namely:
+
+# local_part_suffix = +* : -*
+# local_part_suffix_optional
+
+show how you can specify the recognition of local part suffixes. If the first
+is uncommented, a suffix beginning with either a plus or a minus sign, followed
+by any sequence of characters, is removed from the local part and placed in the
+variable $local_part_suffix. The second suffix option specifies that the
+presence of a suffix in the local part is optional. When a suffix is present,
+the check for a local login uses the local part with the suffix removed.
+
+When a local user account is found, the file called .forward in the user's home
+directory is consulted. If it does not exist, or is empty, the router declines.
+Otherwise, the contents of .forward are interpreted as redirection data (see
+chapter 22 for more details).
+
+Traditional .forward files contain just a list of addresses, pipes, or files.
+Exim supports this by default. However, if allow_filter is set (it is commented
+out by default), the contents of the file are interpreted as a set of Exim or
+Sieve filtering instructions, provided the file begins with "#Exim filter" or "
+#Sieve filter", respectively. User filtering is discussed in the separate
+document entitled Exim's interfaces to mail filtering.
+
+The no_verify and no_expn options mean that this router is skipped when
+verifying addresses, or when running as a consequence of an SMTP EXPN command.
+There are two reasons for doing this:
+
+ 1. Whether or not a local user has a .forward file is not really relevant when
+    checking an address for validity; it makes sense not to waste resources
+    doing unnecessary work.
+
+ 2. More importantly, when Exim is verifying addresses or handling an EXPN
+    command during an SMTP session, it is running as the Exim user, not as
+    root. The group is the Exim group, and no additional groups are set up. It
+    may therefore not be possible for Exim to read users' .forward files at
+    this time.
+
+The setting of check_ancestor prevents the router from generating a new address
+that is the same as any previous address that was redirected. (This works round
+a problem concerning a bad interaction between aliasing and forwarding - see
+section 22.5).
+
+The final three option settings specify the transports that are to be used when
+forwarding generates a direct delivery to a file, or to a pipe, or sets up an
+auto-reply, respectively. For example, if a .forward file contains
+
+a.nother@elsewhere.example, /home/spqr/archive
+
+the delivery to /home/spqr/archive is done by running the address_file
+transport.
+
+localuser:
+  driver = accept
+  check_local_user
+# local_part_suffix = +* : -*
+# local_part_suffix_optional
+  transport = local_delivery
+
+The final router sets up delivery into local mailboxes, provided that the local
+part is the name of a local login, by accepting the address and assigning it to
+the local_delivery transport. Otherwise, we have reached the end of the
+routers, so the address is bounced. The commented suffix settings fulfil the
+same purpose as they do for the userforward router.
+
+
+7.4 Transport configuration
+---------------------------
+
+Transports define mechanisms for actually delivering messages. They operate
+only when referenced from routers, so the order in which they are defined does
+not matter. The transports section of the configuration starts with
+
+begin transports
+
+One remote transport and four local transports are defined.
+
+remote_smtp:
+  driver = smtp
+  hosts_try_prdr = *
+
+This transport is used for delivering messages over SMTP connections. The list
+of remote hosts comes from the router. The hosts_try_prdr option enables an
+efficiency SMTP option. It is negotiated between client and server and not
+expected to cause problems but can be disabled if needed. All other options are
+defaulted.
+
+local_delivery:
+  driver = appendfile
+  file = /var/mail/$local_part
+  delivery_date_add
+  envelope_to_add
+  return_path_add
+# group = mail
+# mode = 0660
+
+This appendfile transport is used for local delivery to user mailboxes in
+traditional BSD mailbox format. By default it runs under the uid and gid of the
+local user, which requires the sticky bit to be set on the /var/mail directory.
+Some systems use the alternative approach of running mail deliveries under a
+particular group instead of using the sticky bit. The commented options show
+how this can be done.
+
+Exim adds three headers to the message as it delivers it: Delivery-date:, 
+Envelope-to: and Return-path:. This action is requested by the three
+similarly-named options above.
+
+address_pipe:
+  driver = pipe
+  return_output
+
+This transport is used for handling deliveries to pipes that are generated by
+redirection (aliasing or users' .forward files). The return_output option
+specifies that any output generated by the pipe is to be returned to the
+sender.
+
+address_file:
+  driver = appendfile
+  delivery_date_add
+  envelope_to_add
+  return_path_add
+
+This transport is used for handling deliveries to files that are generated by
+redirection. The name of the file is not specified in this instance of 
+appendfile, because it comes from the redirect router.
+
+address_reply:
+  driver = autoreply
+
+This transport is used for handling automatic replies generated by users'
+filter files.
+
+
+7.5 Default retry rule
+----------------------
+
+The retry section of the configuration file contains rules which affect the way
+Exim retries deliveries that cannot be completed at the first attempt. It is
+introduced by the line
+
+begin retry
+
+In the default configuration, there is just one rule, which applies to all
+errors:
+
+*   *   F,2h,15m; G,16h,1h,1.5; F,4d,6h
+
+This causes any temporarily failing address to be retried every 15 minutes for
+2 hours, then at intervals starting at one hour and increasing by a factor of
+1.5 until 16 hours have passed, then every 6 hours up to 4 days. If an address
+is not delivered after 4 days of temporary failure, it is bounced.
+
+If the retry section is removed from the configuration, or is empty (that is,
+if no retry rules are defined), Exim will not retry deliveries. This turns
+temporary errors into permanent errors.
+
+
+7.6 Rewriting configuration
+---------------------------
+
+The rewriting section of the configuration, introduced by
+
+begin rewrite
+
+contains rules for rewriting addresses in messages as they arrive. There are no
+rewriting rules in the default configuration file.
+
+
+7.7 Authenticators configuration
+--------------------------------
+
+The authenticators section of the configuration, introduced by
+
+begin authenticators
+
+defines mechanisms for the use of the SMTP AUTH command. The default
+configuration file contains two commented-out example authenticators which
+support plaintext username/password authentication using the standard PLAIN
+mechanism and the traditional but non-standard LOGIN mechanism, with Exim
+acting as the server. PLAIN and LOGIN are enough to support most MUA software.
+
+The example PLAIN authenticator looks like this:
+
+#PLAIN:
+#  driver                  = plaintext
+#  server_set_id           = $auth2
+#  server_prompts          = :
+#  server_condition        = Authentication is not yet configured
+#  server_advertise_condition = ${if def:tls_in_cipher }
+
+And the example LOGIN authenticator looks like this:
+
+#LOGIN:
+#  driver                  = plaintext
+#  server_set_id           = $auth1
+#  server_prompts          = <| Username: | Password:
+#  server_condition        = Authentication is not yet configured
+#  server_advertise_condition = ${if def:tls_in_cipher }
+
+The server_set_id option makes Exim remember the authenticated username in
+$authenticated_id, which can be used later in ACLs or routers. The 
+server_prompts option configures the plaintext authenticator so that it
+implements the details of the specific authentication mechanism, i.e. PLAIN or
+LOGIN. The server_advertise_condition setting controls when Exim offers
+authentication to clients; in the examples, this is only when TLS or SSL has
+been started, so to enable the authenticators you also need to add support for
+TLS as described in section 7.1.
+
+The server_condition setting defines how to verify that the username and
+password are correct. In the examples it just produces an error message. To
+make the authenticators work, you can use a string expansion expression like
+one of the examples in chapter 34.
+
+Beware that the sequence of the parameters to PLAIN and LOGIN differ; the
+usercode and password are in different positions. Chapter 34 covers both.
+
+
+
+===============================================================================
+8. REGULAR EXPRESSIONS
+
+Exim supports the use of regular expressions in many of its options. It uses
+the PCRE regular expression library; this provides regular expression matching
+that is compatible with Perl 5. The syntax and semantics of regular expressions
+is discussed in many Perl reference books, and also in Jeffrey Friedl's 
+Mastering Regular Expressions, which is published by O'Reilly (see http://
+www.oreilly.com/catalog/regex2/).
+
+The documentation for the syntax and semantics of the regular expressions that
+are supported by PCRE is included in the PCRE distribution, and no further
+description is included here. The PCRE functions are called from Exim using the
+default option settings (that is, with no PCRE options set), except that the
+PCRE_CASELESS option is set when the matching is required to be
+case-insensitive.
+
+In most cases, when a regular expression is required in an Exim configuration,
+it has to start with a circumflex, in order to distinguish it from plain text
+or an "ends with" wildcard. In this example of a configuration setting, the
+second item in the colon-separated list is a regular expression.
+
+domains = a.b.c : ^\\d{3} : *.y.z : ...
+
+The doubling of the backslash is required because of string expansion that
+precedes interpretation - see section 11.1 for more discussion of this issue,
+and a way of avoiding the need for doubling backslashes. The regular expression
+that is eventually used in this example contains just one backslash. The
+circumflex is included in the regular expression, and has the normal effect of
+"anchoring" it to the start of the string that is being matched.
+
+There are, however, two cases where a circumflex is not required for the
+recognition of a regular expression: these are the match condition in a string
+expansion, and the matches condition in an Exim filter file. In these cases,
+the relevant string is always treated as a regular expression; if it does not
+start with a circumflex, the expression is not anchored, and can match anywhere
+in the subject string.
+
+In all cases, if you want a regular expression to match at the end of a string,
+you must code the $ metacharacter to indicate this. For example:
+
+domains = ^\\d{3}\\.example
+
+matches the domain 123.example, but it also matches 123.example.com. You need
+to use:
+
+domains = ^\\d{3}\\.example\$
+
+if you want example to be the top-level domain. The backslash before the $ is
+needed because string expansion also interprets dollar characters.
+
+
+
+===============================================================================
+9. FILE AND DATABASE LOOKUPS
+
+Exim can be configured to look up data in files or databases as it processes
+messages. Two different kinds of syntax are used:
+
+ 1. A string that is to be expanded may contain explicit lookup requests. These
+    cause parts of the string to be replaced by data that is obtained from the
+    lookup. Lookups of this type are conditional expansion items. Different
+    results can be defined for the cases of lookup success and failure. See
+    chapter 11, where string expansions are described in detail.
+
+ 2. Lists of domains, hosts, and email addresses can contain lookup requests as
+    a way of avoiding excessively long linear lists. In this case, the data
+    that is returned by the lookup is often (but not always) discarded; whether
+    the lookup succeeds or fails is what really counts. These kinds of list are
+    described in chapter 10.
+
+String expansions, lists, and lookups interact with each other in such a way
+that there is no order in which to describe any one of them that does not
+involve references to the others. Each of these three chapters makes more sense
+if you have read the other two first. If you are reading this for the first
+time, be aware that some of it will make a lot more sense after you have read
+chapters 10 and 11.
+
+
+9.1 Examples of different lookup syntax
+---------------------------------------
+
+It is easy to confuse the two different kinds of lookup, especially as the
+lists that may contain the second kind are always expanded before being
+processed as lists. Therefore, they may also contain lookups of the first kind.
+Be careful to distinguish between the following two examples:
+
+domains = ${lookup{$sender_host_address}lsearch{/some/file}}
+domains = lsearch;/some/file
+
+The first uses a string expansion, the result of which must be a domain list.
+No strings have been specified for a successful or a failing lookup; the
+defaults in this case are the looked-up data and an empty string, respectively.
+The expansion takes place before the string is processed as a list, and the
+file that is searched could contain lines like this:
+
+192.168.3.4: domain1:domain2:...
+192.168.1.9: domain3:domain4:...
+
+When the lookup succeeds, the result of the expansion is a list of domains (and
+possibly other types of item that are allowed in domain lists).
+
+In the second example, the lookup is a single item in a domain list. It causes
+Exim to use a lookup to see if the domain that is being processed can be found
+in the file. The file could contains lines like this:
+
+domain1:
+domain2:
+
+Any data that follows the keys is not relevant when checking that the domain
+matches the list item.
+
+It is possible, though no doubt confusing, to use both kinds of lookup at once.
+Consider a file containing lines like this:
+
+192.168.5.6: lsearch;/another/file
+
+If the value of $sender_host_address is 192.168.5.6, expansion of the first 
+domains setting above generates the second setting, which therefore causes a
+second lookup to occur.
+
+The rest of this chapter describes the different lookup types that are
+available. Any of them can be used in any part of the configuration where a
+lookup is permitted.
+
+
+9.2 Lookup types
+----------------
+
+Two different types of data lookup are implemented:
+
+  * The single-key type requires the specification of a file in which to look,
+    and a single key to search for. The key must be a non-empty string for the
+    lookup to succeed. The lookup type determines how the file is searched.
+
+  * The query-style type accepts a generalized database query. No particular
+    key value is assumed by Exim for query-style lookups. You can use whichever
+    Exim variables you need to construct the database query.
+
+The code for each lookup type is in a separate source file that is included in
+the binary of Exim only if the corresponding compile-time option is set. The
+default settings in src/EDITME are:
+
+LOOKUP_DBM=yes
+LOOKUP_LSEARCH=yes
+
+which means that only linear searching and DBM lookups are included by default.
+For some types of lookup (e.g. SQL databases), you need to install appropriate
+libraries and header files before building Exim.
+
+
+9.3 Single-key lookup types
+---------------------------
+
+The following single-key lookup types are implemented:
+
+  * cdb: The given file is searched as a Constant DataBase file, using the key
+    string without a terminating binary zero. The cdb format is designed for
+    indexed files that are read frequently and never updated, except by total
+    re-creation. As such, it is particularly suitable for large files
+    containing aliases or other indexed data referenced by an MTA. Information
+    about cdb can be found in several places:
+
+    http://www.pobox.com/~djb/cdb.html
+    ftp://ftp.corpit.ru/pub/tinycdb/
+    http://packages.debian.org/stable/utils/freecdb.html
+
+    A cdb distribution is not needed in order to build Exim with cdb support,
+    because the code for reading cdb files is included directly in Exim itself.
+    However, no means of building or testing cdb files is provided with Exim,
+    so you need to obtain a cdb distribution in order to do this.
+
+  * dbm: Calls to DBM library functions are used to extract data from the given
+    DBM file by looking up the record with the given key. A terminating binary
+    zero is included in the key that is passed to the DBM library. See section
+    4.4 for a discussion of DBM libraries.
+
+    For all versions of Berkeley DB, Exim uses the DB_HASH style of database
+    when building DBM files using the exim_dbmbuild utility. However, when
+    using Berkeley DB versions 3 or 4, it opens existing databases for reading
+    with the DB_UNKNOWN option. This enables it to handle any of the types of
+    database that the library supports, and can be useful for accessing DBM
+    files created by other applications. (For earlier DB versions, DB_HASH is
+    always used.)
+
+  * dbmjz: This is the same as dbm, except that the lookup key is interpreted
+    as an Exim list; the elements of the list are joined together with ASCII
+    NUL characters to form the lookup key. An example usage would be to
+    authenticate incoming SMTP calls using the passwords from Cyrus SASL's /etc
+    /sasldb2 file with the gsasl authenticator or Exim's own cram_md5
+    authenticator.
+
+  * dbmnz: This is the same as dbm, except that a terminating binary zero is
+    not included in the key that is passed to the DBM library. You may need
+    this if you want to look up data in files that are created by or shared
+    with some other application that does not use terminating zeros. For
+    example, you need to use dbmnz rather than dbm if you want to authenticate
+    incoming SMTP calls using the passwords from Courier's /etc/
+    userdbshadow.dat file. Exim's utility program for creating DBM files (
+    exim_dbmbuild) includes the zeros by default, but has an option to omit
+    them (see section 52.9).
+
+  * dsearch: The given file must be a directory; this is searched for an entry
+    whose name is the key by calling the lstat() function. The key may not
+    contain any forward slash characters. If lstat() succeeds, the result of
+    the lookup is the name of the entry, which may be a file, directory,
+    symbolic link, or any other kind of directory entry. An example of how this
+    lookup can be used to support virtual domains is given in section 49.7.
+
+  * iplsearch: The given file is a text file containing keys and data. A key is
+    terminated by a colon or white space or the end of the line. The keys in
+    the file must be IP addresses, or IP addresses with CIDR masks. Keys that
+    involve IPv6 addresses must be enclosed in quotes to prevent the first
+    internal colon being interpreted as a key terminator. For example:
+
+    1.2.3.4:           data for 1.2.3.4
+    192.168.0.0/16:    data for 192.168.0.0/16
+    "abcd::cdab":      data for abcd::cdab
+    "abcd:abcd::/32"   data for abcd:abcd::/32
+
+    The key for an iplsearch lookup must be an IP address (without a mask). The
+    file is searched linearly, using the CIDR masks where present, until a
+    matching key is found. The first key that matches is used; there is no
+    attempt to find a "best" match. Apart from the way the keys are matched,
+    the processing for iplsearch is the same as for lsearch.
+
+    Warning 1: Unlike most other single-key lookup types, a file of data for 
+    iplsearch can not be turned into a DBM or cdb file, because those lookup
+    types support only literal keys.
+
+    Warning 2: In a host list, you must always use net-iplsearch so that the
+    implicit key is the host's IP address rather than its name (see section
+    10.12).
+
+  * lsearch: The given file is a text file that is searched linearly for a line
+    beginning with the search key, terminated by a colon or white space or the
+    end of the line. The search is case-insensitive; that is, upper and lower
+    case letters are treated as the same. The first occurrence of the key that
+    is found in the file is used.
+
+    White space between the key and the colon is permitted. The remainder of
+    the line, with leading and trailing white space removed, is the data. This
+    can be continued onto subsequent lines by starting them with any amount of
+    white space, but only a single space character is included in the data at
+    such a junction. If the data begins with a colon, the key must be
+    terminated by a colon, for example:
+
+    baduser:  :fail:
+
+    Empty lines and lines beginning with # are ignored, even if they occur in
+    the middle of an item. This is the traditional textual format of alias
+    files. Note that the keys in an lsearch file are literal strings. There is
+    no wildcarding of any kind.
+
+    In most lsearch files, keys are not required to contain colons or #
+    characters, or white space. However, if you need this feature, it is
+    available. If a key begins with a doublequote character, it is terminated
+    only by a matching quote (or end of line), and the normal escaping rules
+    apply to its contents (see section 6.16). An optional colon is permitted
+    after quoted keys (exactly as for unquoted keys). There is no special
+    handling of quotes for the data part of an lsearch line.
+
+  * nis: The given file is the name of a NIS map, and a NIS lookup is done with
+    the given key, without a terminating binary zero. There is a variant called
+    nis0 which does include the terminating binary zero in the key. This is
+    reportedly needed for Sun-style alias files. Exim does not recognize NIS
+    aliases; the full map names must be used.
+
+  * wildlsearch or nwildlsearch: These search a file linearly, like lsearch,
+    but instead of being interpreted as a literal string, each key in the file
+    may be wildcarded. The difference between these two lookup types is that
+    for wildlsearch, each key in the file is string-expanded before being used,
+    whereas for nwildlsearch, no expansion takes place.
+
+    Like lsearch, the testing is done case-insensitively. However, keys in the
+    file that are regular expressions can be made case-sensitive by the use of
+    "(-i)" within the pattern. The following forms of wildcard are recognized:
+
+     1. The string may begin with an asterisk to mean "ends with". For example:
+
+            *.a.b.c       data for anything.a.b.c
+            *fish         data for anythingfish
+
+     2. The string may begin with a circumflex to indicate a regular
+        expression. For example, for wildlsearch:
+
+            ^\N\d+\.a\.b\N    data for <digits>.a.b
+
+        Note the use of "\N" to disable expansion of the contents of the
+        regular expression. If you are using nwildlsearch, where the keys are
+        not string-expanded, the equivalent entry is:
+
+            ^\d+\.a\.b        data for <digits>.a.b
+
+        The case-insensitive flag is set at the start of compiling the regular
+        expression, but it can be turned off by using "(-i)" at an appropriate
+        point. For example, to make the entire pattern case-sensitive:
+
+            ^(?-i)\d+\.a\.b        data for <digits>.a.b
+
+        If the regular expression contains white space or colon characters, you
+        must either quote it (see lsearch above), or represent these characters
+        in other ways. For example, "\s" can be used for white space and "\x3A"
+        for a colon. This may be easier than quoting, because if you quote, you
+        have to escape all the backslashes inside the quotes.
+
+        Note: It is not possible to capture substrings in a regular expression
+        match for later use, because the results of all lookups are cached. If
+        a lookup is repeated, the result is taken from the cache, and no actual
+        pattern matching takes place. The values of all the numeric variables
+        are unset after a (n)wildlsearch match.
+
+     3. Although I cannot see it being of much use, the general matching
+        function that is used to implement (n)wildlsearch means that the string
+        may begin with a lookup name terminated by a semicolon, and followed by
+        lookup data. For example:
+
+            cdb;/some/file  data for keys that match the file
+
+        The data that is obtained from the nested lookup is discarded.
+
+    Keys that do not match any of these patterns are interpreted literally. The
+    continuation rules for the data are the same as for lsearch, and keys may
+    be followed by optional colons.
+
+    Warning: Unlike most other single-key lookup types, a file of data for (n)
+    wildlsearch can not be turned into a DBM or cdb file, because those lookup
+    types support only literal keys.
+
+
+9.4 Query-style lookup types
+----------------------------
+
+The supported query-style lookup types are listed below. Further details about
+many of them are given in later sections.
+
+  * dnsdb: This does a DNS search for one or more records whose domain names
+    are given in the supplied query. The resulting data is the contents of the
+    records. See section 9.10.
+
+  * ibase: This does a lookup in an InterBase database.
+
+  * ldap: This does an LDAP lookup using a query in the form of a URL, and
+    returns attributes from a single entry. There is a variant called ldapm
+    that permits values from multiple entries to be returned. A third variant
+    called ldapdn returns the Distinguished Name of a single entry instead of
+    any attribute values. See section 9.13.
+
+  * mysql: The format of the query is an SQL statement that is passed to a
+    MySQL database. See section 9.20.
+
+  * nisplus: This does a NIS+ lookup using a query that can specify the name of
+    the field to be returned. See section 9.19.
+
+  * oracle: The format of the query is an SQL statement that is passed to an
+    Oracle database. See section 9.20.
+
+  * passwd is a query-style lookup with queries that are just user names. The
+    lookup calls getpwnam() to interrogate the system password data, and on
+    success, the result string is the same as you would get from an lsearch
+    lookup on a traditional /etc/passwd file, though with "*" for the password
+    value. For example:
+
+    *:42:42:King Rat:/home/kr:/bin/bash
+
+  * pgsql: The format of the query is an SQL statement that is passed to a
+    PostgreSQL database. See section 9.20.
+
+  * sqlite: The format of the query is a file name followed by an SQL statement
+    that is passed to an SQLite database. See section 9.25.
+
+  * testdb: This is a lookup type that is used for testing Exim. It is not
+    likely to be useful in normal operation.
+
+  * whoson: Whoson (http://whoson.sourceforge.net) is a protocol that allows a
+    server to check whether a particular (dynamically allocated) IP address is
+    currently allocated to a known (trusted) user and, optionally, to obtain
+    the identity of the said user. For SMTP servers, Whoson was popular at one
+    time for "POP before SMTP" authentication, but that approach has been
+    superseded by SMTP authentication. In Exim, Whoson can be used to implement
+    "POP before SMTP" checking using ACL statements such as
+
+    require condition = \
+      ${lookup whoson {$sender_host_address}{yes}{no}}
+
+    The query consists of a single IP address. The value returned is the name
+    of the authenticated user, which is stored in the variable $value. However,
+    in this example, the data in $value is not used; the result of the lookup
+    is one of the fixed strings "yes" or "no".
+
+
+9.5 Temporary errors in lookups
+-------------------------------
+
+Lookup functions can return temporary error codes if the lookup cannot be
+completed. For example, an SQL or LDAP database might be unavailable. For this
+reason, it is not advisable to use a lookup that might do this for critical
+options such as a list of local domains.
+
+When a lookup cannot be completed in a router or transport, delivery of the
+message (to the relevant address) is deferred, as for any other temporary
+error. In other circumstances Exim may assume the lookup has failed, or may
+give up altogether.
+
+
+9.6 Default values in single-key lookups
+----------------------------------------
+
+In this context, a "default value" is a value specified by the administrator
+that is to be used if a lookup fails.
+
+Note: This section applies only to single-key lookups. For query-style lookups,
+the facilities of the query language must be used. An attempt to specify a
+default for a query-style lookup provokes an error.
+
+If "*" is added to a single-key lookup type (for example, lsearch*) and the
+initial lookup fails, the key "*" is looked up in the file to provide a default
+value. See also the section on partial matching below.
+
+Alternatively, if "*@" is added to a single-key lookup type (for example dbm*@)
+then, if the initial lookup fails and the key contains an @ character, a second
+lookup is done with everything before the last @ replaced by *. This makes it
+possible to provide per-domain defaults in alias files that include the domains
+in the keys. If the second lookup fails (or doesn't take place because there is
+no @ in the key), "*" is looked up. For example, a redirect router might
+contain:
+
+data = ${lookup{$local_part@$domain}lsearch*@{/etc/mix-aliases}}
+
+Suppose the address that is being processed is jane@eyre.example. Exim looks up
+these keys, in this order:
+
+jane@eyre.example
+*@eyre.example
+*
+
+The data is taken from whichever key it finds first. Note: In an lsearch file,
+this does not mean the first of these keys in the file. A complete scan is done
+for each key, and only if it is not found at all does Exim move on to try the
+next key.
+
+
+9.7 Partial matching in single-key lookups
+------------------------------------------
+
+The normal operation of a single-key lookup is to search the file for an exact
+match with the given key. However, in a number of situations where domains are
+being looked up, it is useful to be able to do partial matching. In this case,
+information in the file that has a key starting with "*." is matched by any
+domain that ends with the components that follow the full stop. For example, if
+a key in a DBM file is
+
+*.dates.fict.example
+
+then when partial matching is enabled this is matched by (amongst others) 
+2001.dates.fict.example and 1984.dates.fict.example. It is also matched by 
+dates.fict.example, if that does not appear as a separate key in the file.
+
+Note: Partial matching is not available for query-style lookups. It is also not
+available for any lookup items in address lists (see section 10.19).
+
+Partial matching is implemented by doing a series of separate lookups using
+keys constructed by modifying the original subject key. This means that it can
+be used with any of the single-key lookup types, provided that partial matching
+keys beginning with a special prefix (default "*.") are included in the data
+file. Keys in the file that do not begin with the prefix are matched only by
+unmodified subject keys when partial matching is in use.
+
+Partial matching is requested by adding the string "partial-" to the front of
+the name of a single-key lookup type, for example, partial-dbm. When this is
+done, the subject key is first looked up unmodified; if that fails, "*." is
+added at the start of the subject key, and it is looked up again. If that
+fails, further lookups are tried with dot-separated components removed from the
+start of the subject key, one-by-one, and "*." added on the front of what
+remains.
+
+A minimum number of two non-* components are required. This can be adjusted by
+including a number before the hyphen in the search type. For example, 
+partial3-lsearch specifies a minimum of three non-* components in the modified
+keys. Omitting the number is equivalent to "partial2-". If the subject key is 
+2250.dates.fict.example then the following keys are looked up when the minimum
+number of non-* components is two:
+
+2250.dates.fict.example
+*.2250.dates.fict.example
+*.dates.fict.example
+*.fict.example
+
+As soon as one key in the sequence is successfully looked up, the lookup
+finishes.
+
+The use of "*." as the partial matching prefix is a default that can be
+changed. The motivation for this feature is to allow Exim to operate with file
+formats that are used by other MTAs. A different prefix can be supplied in
+parentheses instead of the hyphen after "partial". For example:
+
+domains = partial(.)lsearch;/some/file
+
+In this example, if the domain is a.b.c, the sequence of lookups is "a.b.c",
+".a.b.c", and ".b.c" (the default minimum of 2 non-wild components is
+unchanged). The prefix may consist of any punctuation characters other than a
+closing parenthesis. It may be empty, for example:
+
+domains = partial1()cdb;/some/file
+
+For this example, if the domain is a.b.c, the sequence of lookups is "a.b.c",
+"b.c", and "c".
+
+If "partial0" is specified, what happens at the end (when the lookup with just
+one non-wild component has failed, and the original key is shortened right down
+to the null string) depends on the prefix:
+
+  * If the prefix has zero length, the whole lookup fails.
+
+  * If the prefix has length 1, a lookup for just the prefix is done. For
+    example, the final lookup for "partial0(.)" is for "." alone.
+
+  * Otherwise, if the prefix ends in a dot, the dot is removed, and the
+    remainder is looked up. With the default prefix, therefore, the final
+    lookup is for "*" on its own.
+
+  * Otherwise, the whole prefix is looked up.
+
+If the search type ends in "*" or "*@" (see section 9.6 above), the search for
+an ultimate default that this implies happens after all partial lookups have
+failed. If "partial0" is specified, adding "*" to the search type has no effect
+with the default prefix, because the "*" key is already included in the
+sequence of partial lookups. However, there might be a use for lookup types
+such as "partial0(.)lsearch*".
+
+The use of "*" in lookup partial matching differs from its use as a wildcard in
+domain lists and the like. Partial matching works only in terms of
+dot-separated components; a key such as "*fict.example" in a database file is
+useless, because the asterisk in a partial matching subject key is always
+followed by a dot.
+
+
+9.8 Lookup caching
+------------------
+
+Exim caches all lookup results in order to avoid needless repetition of
+lookups. However, because (apart from the daemon) Exim operates as a collection
+of independent, short-lived processes, this caching applies only within a
+single Exim process. There is no inter-process lookup caching facility.
+
+For single-key lookups, Exim keeps the relevant files open in case there is
+another lookup that needs them. In some types of configuration this can lead to
+many files being kept open for messages with many recipients. To avoid hitting
+the operating system limit on the number of simultaneously open files, Exim
+closes the least recently used file when it needs to open more files than its
+own internal limit, which can be changed via the lookup_open_max option.
+
+The single-key lookup files are closed and the lookup caches are flushed at
+strategic points during delivery - for example, after all routing is complete.
+
+
+9.9 Quoting lookup data
+-----------------------
+
+When data from an incoming message is included in a query-style lookup, there
+is the possibility of special characters in the data messing up the syntax of
+the query. For example, a NIS+ query that contains
+
+[name=$local_part]
+
+will be broken if the local part happens to contain a closing square bracket.
+For NIS+, data can be enclosed in double quotes like this:
+
+[name="$local_part"]
+
+but this still leaves the problem of a double quote in the data. The rule for
+NIS+ is that double quotes must be doubled. Other lookup types have different
+rules, and to cope with the differing requirements, an expansion operator of
+the following form is provided:
+
+${quote_<lookup-type>:<string>}
+
+For example, the safest way to write the NIS+ query is
+
+[name="${quote_nisplus:$local_part}"]
+
+See chapter 11 for full coverage of string expansions. The quote operator can
+be used for all lookup types, but has no effect for single-key lookups, since
+no quoting is ever needed in their key strings.
+
+
+9.10 More about dnsdb
+---------------------
+
+The dnsdb lookup type uses the DNS as its database. A simple query consists of
+a record type and a domain name, separated by an equals sign. For example, an
+expansion string could contain:
+
+${lookup dnsdb{mx=a.b.example}{$value}fail}
+
+If the lookup succeeds, the result is placed in $value, which in this case is
+used on its own as the result. If the lookup does not succeed, the "fail"
+keyword causes a forced expansion failure - see section 11.4 for an explanation
+of what this means.
+
+The supported DNS record types are A, CNAME, MX, NS, PTR, SPF, SRV, TLSA and
+TXT, and, when Exim is compiled with IPv6 support, AAAA (and A6 if that is also
+configured). If no type is given, TXT is assumed. When the type is PTR, the
+data can be an IP address, written as normal; inversion and the addition of 
+in-addr.arpa or ip6.arpa happens automatically. For example:
+
+${lookup dnsdb{ptr=192.168.4.5}{$value}fail}
+
+If the data for a PTR record is not a syntactically valid IP address, it is not
+altered and nothing is added.
+
+For an MX lookup, both the preference value and the host name are returned for
+each record, separated by a space. For an SRV lookup, the priority, weight,
+port, and host name are returned for each record, separated by spaces.
+
+For any record type, if multiple records are found (or, for A6 lookups, if a
+single record leads to multiple addresses), the data is returned as a
+concatenation, with newline as the default separator. The order, of course,
+depends on the DNS resolver. You can specify a different separator character
+between multiple records by putting a right angle-bracket followed immediately
+by the new separator at the start of the query. For example:
+
+${lookup dnsdb{>: a=host1.example}}
+
+It is permitted to specify a space as the separator character. Further white
+space is ignored.
+
+For TXT records with multiple items of data, only the first item is returned,
+unless a separator for them is specified using a comma after the separator
+character followed immediately by the TXT record item separator. To concatenate
+items without a separator, use a semicolon instead. For SPF records the default
+behaviour is to concatenate multiple items without using a separator.
+
+${lookup dnsdb{>\n,: txt=a.b.example}}
+${lookup dnsdb{>\n; txt=a.b.example}}
+${lookup dnsdb{spf=example.org}}
+
+It is permitted to specify a space as the separator character. Further white
+space is ignored.
+
+
+9.11 Pseudo dnsdb record types
+------------------------------
+
+By default, both the preference value and the host name are returned for each
+MX record, separated by a space. If you want only host names, you can use the
+pseudo-type MXH:
+
+${lookup dnsdb{mxh=a.b.example}}
+
+In this case, the preference values are omitted, and just the host names are
+returned.
+
+Another pseudo-type is ZNS (for "zone NS"). It performs a lookup for NS records
+on the given domain, but if none are found, it removes the first component of
+the domain name, and tries again. This process continues until NS records are
+found or there are no more components left (or there is a DNS error). In other
+words, it may return the name servers for a top-level domain, but it never
+returns the root name servers. If there are no NS records for the top-level
+domain, the lookup fails. Consider these examples:
+
+${lookup dnsdb{zns=xxx.quercite.com}}
+${lookup dnsdb{zns=xxx.edu}}
+
+Assuming that in each case there are no NS records for the full domain name,
+the first returns the name servers for quercite.com, and the second returns the
+name servers for edu.
+
+You should be careful about how you use this lookup because, unless the
+top-level domain does not exist, the lookup always returns some host names. The
+sort of use to which this might be put is for seeing if the name servers for a
+given domain are on a blacklist. You can probably assume that the name servers
+for the high-level domains such as com or co.uk are not going to be on such a
+list.
+
+A third pseudo-type is CSA (Client SMTP Authorization). This looks up SRV
+records according to the CSA rules, which are described in section 42.50.
+Although dnsdb supports SRV lookups directly, this is not sufficient because of
+the extra parent domain search behaviour of CSA. The result of a successful
+lookup such as:
+
+${lookup dnsdb {csa=$sender_helo_name}}
+
+has two space-separated fields: an authorization code and a target host name.
+The authorization code can be "Y" for yes, "N" for no, "X" for explicit
+authorization required but absent, or "?" for unknown.
+
+The pseudo-type A+ performs an A6 lookup (if configured) followed by an AAAA
+and then an A lookup. All results are returned; defer processing (see below) is
+handled separately for each lookup. Example:
+
+${lookup dnsdb {>; a+=$sender_helo_name}}
+
+
+9.12 Multiple dnsdb lookups
+---------------------------
+
+In the previous sections, dnsdb lookups for a single domain are described.
+However, you can specify a list of domains or IP addresses in a single dnsdb
+lookup. The list is specified in the normal Exim way, with colon as the default
+separator, but with the ability to change this. For example:
+
+${lookup dnsdb{one.domain.com:two.domain.com}}
+${lookup dnsdb{a=one.host.com:two.host.com}}
+${lookup dnsdb{ptr = <; 1.2.3.4 ; 4.5.6.8}}
+
+In order to retain backwards compatibility, there is one special case: if the
+lookup type is PTR and no change of separator is specified, Exim looks to see
+if the rest of the string is precisely one IPv6 address. In this case, it does
+not treat it as a list.
+
+The data from each lookup is concatenated, with newline separators by default,
+in the same way that multiple DNS records for a single item are handled. A
+different separator can be specified, as described above.
+
+Modifiers for dnsdb lookups are givien by optional keywords, each followed by a
+comma, that may appear before the record type.
+
+The dnsdb lookup fails only if all the DNS lookups fail. If there is a
+temporary DNS error for any of them, the behaviour is controlled by a
+defer-option modifier. The possible keywords are "defer_strict", "defer_never",
+and "defer_lax". With "strict" behaviour, any temporary DNS error causes the
+whole lookup to defer. With "never" behaviour, a temporary DNS error is
+ignored, and the behaviour is as if the DNS lookup failed to find anything.
+With "lax" behaviour, all the queries are attempted, but a temporary DNS error
+causes the whole lookup to defer only if none of the other lookups succeed. The
+default is "lax", so the following lookups are equivalent:
+
+${lookup dnsdb{defer_lax,a=one.host.com:two.host.com}}
+${lookup dnsdb{a=one.host.com:two.host.com}}
+
+Thus, in the default case, as long as at least one of the DNS lookups yields
+some data, the lookup succeeds.
+
+Use of DNSSEC is controlled by a dnssec modifier. The possible keywords are
+"dnssec_strict", "dnssec_lax", and "dnssec_never". With "strict" or "lax"
+DNSSEC information is requested with the lookup. With "strict" a response from
+the DNS resolver that is not labelled as authenticated data is treated as
+equivalent to a temporary DNS error. The default is "never".
+
+See also the $lookup_dnssec_authenticated variable.
+
+
+9.13 More about LDAP
+--------------------
+
+The original LDAP implementation came from the University of Michigan; this has
+become "Open LDAP", and there are now two different releases. Another
+implementation comes from Netscape, and Solaris 7 and subsequent releases
+contain inbuilt LDAP support. Unfortunately, though these are all compatible at
+the lookup function level, their error handling is different. For this reason
+it is necessary to set a compile-time variable when building Exim with LDAP, to
+indicate which LDAP library is in use. One of the following should appear in
+your Local/Makefile:
+
+LDAP_LIB_TYPE=UMICHIGAN
+LDAP_LIB_TYPE=OPENLDAP1
+LDAP_LIB_TYPE=OPENLDAP2
+LDAP_LIB_TYPE=NETSCAPE
+LDAP_LIB_TYPE=SOLARIS
+
+If LDAP_LIB_TYPE is not set, Exim assumes "OPENLDAP1", which has the same
+interface as the University of Michigan version.
+
+There are three LDAP lookup types in Exim. These behave slightly differently in
+the way they handle the results of a query:
+
+  * ldap requires the result to contain just one entry; if there are more, it
+    gives an error.
+
+  * ldapdn also requires the result to contain just one entry, but it is the
+    Distinguished Name that is returned rather than any attribute values.
+
+  * ldapm permits the result to contain more than one entry; the attributes
+    from all of them are returned.
+
+For ldap and ldapm, if a query finds only entries with no attributes, Exim
+behaves as if the entry did not exist, and the lookup fails. The format of the
+data returned by a successful lookup is described in the next section. First we
+explain how LDAP queries are coded.
+
+
+9.14 Format of LDAP queries
+---------------------------
+
+An LDAP query takes the form of a URL as defined in RFC 2255. For example, in
+the configuration of a redirect router one might have this setting:
+
+data = ${lookup ldap \
+  {ldap:///cn=$local_part,o=University%20of%20Cambridge,\
+  c=UK?mailbox?base?}}
+
+The URL may begin with "ldap" or "ldaps" if your LDAP library supports secure
+(encrypted) LDAP connections. The second of these ensures that an encrypted TLS
+connection is used.
+
+With sufficiently modern LDAP libraries, Exim supports forcing TLS over regular
+LDAP connections, rather than the SSL-on-connect "ldaps". See the 
+ldap_start_tls option.
+
+Starting with Exim 4.83, the initialization of LDAP with TLS is more tightly
+controlled. Every part of the TLS configuration can be configured by settings
+in exim.conf. Depending on the version of the client libraries installed on
+your system, some of the initialization may have required setting options in /
+etc/ldap.conf or ~/.ldaprc to get TLS working with self-signed certificates.
+This revealed a nuance where the current UID that exim was running as could
+affect which config files it read. With Exim 4.83, these methods become
+optional, only taking effect if not specifically set in exim.conf.
+
+
+9.15 LDAP quoting
+-----------------
+
+Two levels of quoting are required in LDAP queries, the first for LDAP itself
+and the second because the LDAP query is represented as a URL. Furthermore,
+within an LDAP query, two different kinds of quoting are required. For this
+reason, there are two different LDAP-specific quoting operators.
+
+The quote_ldap operator is designed for use on strings that are part of filter
+specifications. Conceptually, it first does the following conversions on the
+string:
+
+*   =>   \2A
+(   =>   \28
+)   =>   \29
+\   =>   \5C
+
+in accordance with RFC 2254. The resulting string is then quoted according to
+the rules for URLs, that is, all non-alphanumeric characters except
+
+! $ ' - . _ ( ) * +
+
+are converted to their hex values, preceded by a percent sign. For example:
+
+${quote_ldap: a(bc)*, a<yz>; }
+
+yields
+
+%20a%5C28bc%5C29%5C2A%2C%20a%3Cyz%3E%3B%20
+
+Removing the URL quoting, this is (with a leading and a trailing space):
+
+a\28bc\29\2A, a<yz>;
+
+The quote_ldap_dn operator is designed for use on strings that are part of base
+DN specifications in queries. Conceptually, it first converts the string by
+inserting a backslash in front of any of the following characters:
+
+, + " \ < > ;
+
+It also inserts a backslash before any leading spaces or # characters, and
+before any trailing spaces. (These rules are in RFC 2253.) The resulting string
+is then quoted according to the rules for URLs. For example:
+
+${quote_ldap_dn: a(bc)*, a<yz>; }
+
+yields
+
+%5C%20a(bc)*%5C%2C%20a%5C%3Cyz%5C%3E%5C%3B%5C%20
+
+Removing the URL quoting, this is (with a trailing space):
+
+\ a(bc)*\, a\<yz\>\;\
+
+There are some further comments about quoting in the section on LDAP
+authentication below.
+
+
+9.16 LDAP connections
+---------------------
+
+The connection to an LDAP server may either be over TCP/IP, or, when OpenLDAP
+is in use, via a Unix domain socket. The example given above does not specify
+an LDAP server. A server that is reached by TCP/IP can be specified in a query
+by starting it with
+
+ldap://<hostname>:<port>/...
+
+If the port (and preceding colon) are omitted, the standard LDAP port (389) is
+used. When no server is specified in a query, a list of default servers is
+taken from the ldap_default_servers configuration option. This supplies a
+colon-separated list of servers which are tried in turn until one successfully
+handles a query, or there is a serious error. Successful handling either
+returns the requested data, or indicates that it does not exist. Serious errors
+are syntactical, or multiple values when only a single value is expected.
+Errors which cause the next server to be tried are connection failures, bind
+failures, and timeouts.
+
+For each server name in the list, a port number can be given. The standard way
+of specifying a host and port is to use a colon separator (RFC 1738). Because 
+ldap_default_servers is a colon-separated list, such colons have to be doubled.
+For example
+
+ldap_default_servers = ldap1.example.com::145:ldap2.example.com
+
+If ldap_default_servers is unset, a URL with no server name is passed to the
+LDAP library with no server name, and the library's default (normally the local
+host) is used.
+
+If you are using the OpenLDAP library, you can connect to an LDAP server using
+a Unix domain socket instead of a TCP/IP connection. This is specified by using
+"ldapi" instead of "ldap" in LDAP queries. What follows here applies only to
+OpenLDAP. If Exim is compiled with a different LDAP library, this feature is
+not available.
+
+For this type of connection, instead of a host name for the server, a pathname
+for the socket is required, and the port number is not relevant. The pathname
+can be specified either as an item in ldap_default_servers, or inline in the
+query. In the former case, you can have settings such as
+
+ldap_default_servers = /tmp/ldap.sock : backup.ldap.your.domain
+
+When the pathname is given in the query, you have to escape the slashes as
+"%2F" to fit in with the LDAP URL syntax. For example:
+
+${lookup ldap {ldapi://%2Ftmp%2Fldap.sock/o=...
+
+When Exim processes an LDAP lookup and finds that the "hostname" is really a
+pathname, it uses the Unix domain socket code, even if the query actually
+specifies "ldap" or "ldaps". In particular, no encryption is used for a socket
+connection. This behaviour means that you can use a setting of 
+ldap_default_servers such as in the example above with traditional "ldap" or
+"ldaps" queries, and it will work. First, Exim tries a connection via the Unix
+domain socket; if that fails, it tries a TCP/IP connection to the backup host.
+
+If an explicit "ldapi" type is given in a query when a host name is specified,
+an error is diagnosed. However, if there are more items in ldap_default_servers
+, they are tried. In other words:
+
+  * Using a pathname with "ldap" or "ldaps" forces the use of the Unix domain
+    interface.
+
+  * Using "ldapi" with a host name causes an error.
+
+Using "ldapi" with no host or path in the query, and no setting of 
+ldap_default_servers, does whatever the library does by default.
+
+
+9.17 LDAP authentication and control information
+------------------------------------------------
+
+The LDAP URL syntax provides no way of passing authentication and other control
+information to the server. To make this possible, the URL in an LDAP query may
+be preceded by any number of <name>=<value> settings, separated by spaces. If a
+value contains spaces it must be enclosed in double quotes, and when double
+quotes are used, backslash is interpreted in the usual way inside them. The
+following names are recognized:
+
+DEREFERENCE  set the dereferencing parameter
+NETTIME      set a timeout for a network operation
+USER         set the DN, for authenticating the LDAP bind
+PASS         set the password, likewise
+REFERRALS    set the referrals parameter
+SERVERS      set alternate server list for this query only
+SIZE         set the limit for the number of entries returned
+TIME         set the maximum waiting time for a query
+
+The value of the DEREFERENCE parameter must be one of the words "never",
+"searching", "finding", or "always". The value of the REFERRALS parameter must
+be "follow" (the default) or "nofollow". The latter stops the LDAP library from
+trying to follow referrals issued by the LDAP server.
+
+The name CONNECT is an obsolete name for NETTIME, retained for backwards
+compatibility. This timeout (specified as a number of seconds) is enforced from
+the client end for operations that can be carried out over a network.
+Specifically, it applies to network connections and calls to the ldap_result()
+function. If the value is greater than zero, it is used if
+LDAP_OPT_NETWORK_TIMEOUT is defined in the LDAP headers (OpenLDAP), or if
+LDAP_X_OPT_CONNECT_TIMEOUT is defined in the LDAP headers (Netscape SDK 4.1). A
+value of zero forces an explicit setting of "no timeout" for Netscape SDK; for
+OpenLDAP no action is taken.
+
+The TIME parameter (also a number of seconds) is passed to the server to set a
+server-side limit on the time taken to complete a search.
+
+The SERVERS parameter allows you to specify an alternate list of ldap servers
+to use for an individual lookup. The global ldap_servers option provides a
+default list of ldap servers, and a single lookup can specify a single ldap
+server to use. But when you need to do a lookup with a list of servers that is
+different than the default list (maybe different order, maybe a completely
+different set of servers), the SERVERS parameter allows you to specify this
+alternate list.
+
+Here is an example of an LDAP query in an Exim lookup that uses some of these
+values. This is a single line, folded to fit on the page:
+
+${lookup ldap
+  {user="cn=manager,o=University of Cambridge,c=UK" pass=secret
+  ldap:///o=University%20of%20Cambridge,c=UK?sn?sub?(cn=foo)}
+  {$value}fail}
+
+The encoding of spaces as "%20" is a URL thing which should not be done for any
+of the auxiliary data. Exim configuration settings that include lookups which
+contain password information should be preceded by "hide" to prevent non-admin
+users from using the -bP option to see their values.
+
+The auxiliary data items may be given in any order. The default is no
+connection timeout (the system timeout is used), no user or password, no limit
+on the number of entries returned, and no time limit on queries.
+
+When a DN is quoted in the USER= setting for LDAP authentication, Exim removes
+any URL quoting that it may contain before passing it LDAP. Apparently some
+libraries do this for themselves, but some do not. Removing the URL quoting has
+two advantages:
+
+  * It makes it possible to use the same quote_ldap_dn expansion for USER= DNs
+    as with DNs inside actual queries.
+
+  * It permits spaces inside USER= DNs.
+
+For example, a setting such as
+
+USER=cn=${quote_ldap_dn:$1}
+
+should work even if $1 contains spaces.
+
+Expanded data for the PASS= value should be quoted using the quote expansion
+operator, rather than the LDAP quote operators. The only reason this field
+needs quoting is to ensure that it conforms to the Exim syntax, which does not
+allow unquoted spaces. For example:
+
+PASS=${quote:$3}
+
+The LDAP authentication mechanism can be used to check passwords as part of
+SMTP authentication. See the ldapauth expansion string condition in chapter 11.
+
+
+9.18 Format of data returned by LDAP
+------------------------------------
+
+The ldapdn lookup type returns the Distinguished Name from a single entry as a
+sequence of values, for example
+
+cn=manager, o=University of Cambridge, c=UK
+
+The ldap lookup type generates an error if more than one entry matches the
+search filter, whereas ldapm permits this case, and inserts a newline in the
+result between the data from different entries. It is possible for multiple
+values to be returned for both ldap and ldapm, but in the former case you know
+that whatever values are returned all came from a single entry in the
+directory.
+
+In the common case where you specify a single attribute in your LDAP query, the
+result is not quoted, and does not contain the attribute name. If the attribute
+has multiple values, they are separated by commas.
+
+If you specify multiple attributes, the result contains space-separated, quoted
+strings, each preceded by the attribute name and an equals sign. Within the
+quotes, the quote character, backslash, and newline are escaped with
+backslashes, and commas are used to separate multiple values for the attribute.
+Apart from the escaping, the string within quotes takes the same form as the
+output when a single attribute is requested. Specifying no attributes is the
+same as specifying all of an entry's attributes.
+
+Here are some examples of the output format. The first line of each pair is an
+LDAP query, and the second is the data that is returned. The attribute called 
+attr1 has two values, whereas attr2 has only one value:
+
+ldap:///o=base?attr1?sub?(uid=fred)
+value1.1, value1.2
+
+ldap:///o=base?attr2?sub?(uid=fred)
+value two
+
+ldap:///o=base?attr1,attr2?sub?(uid=fred)
+attr1="value1.1, value1.2" attr2="value two"
+
+ldap:///o=base??sub?(uid=fred)
+objectClass="top" attr1="value1.1, value1.2" attr2="value two"
+
+The extract operator in string expansions can be used to pick out individual
+fields from data that consists of key=value pairs. You can make use of Exim's 
+-be option to run expansion tests and thereby check the results of LDAP
+lookups.
+
+
+9.19 More about NIS+
+--------------------
+
+NIS+ queries consist of a NIS+ indexed name followed by an optional colon and
+field name. If this is given, the result of a successful query is the contents
+of the named field; otherwise the result consists of a concatenation of 
+field-name=field-value pairs, separated by spaces. Empty values and values
+containing spaces are quoted. For example, the query
+
+[name=mg1456],passwd.org_dir
+
+might return the string
+
+name=mg1456 passwd="" uid=999 gid=999 gcos="Martin Guerre"
+home=/home/mg1456 shell=/bin/bash shadow=""
+
+(split over two lines here to fit on the page), whereas
+
+[name=mg1456],passwd.org_dir:gcos
+
+would just return
+
+Martin Guerre
+
+with no quotes. A NIS+ lookup fails if NIS+ returns more than one table entry
+for the given indexed key. The effect of the quote_nisplus expansion operator
+is to double any quote characters within the text.
+
+
+9.20 SQL lookups
+----------------
+
+Exim can support lookups in InterBase, MySQL, Oracle, PostgreSQL, and SQLite
+databases. Queries for these databases contain SQL statements, so an example
+might be
+
+${lookup mysql{select mailbox from users where id='userx'}\
+  {$value}fail}
+
+If the result of the query contains more than one field, the data for each
+field in the row is returned, preceded by its name, so the result of
+
+${lookup pgsql{select home,name from users where id='userx'}\
+  {$value}}
+
+might be
+
+home=/home/userx name="Mister X"
+
+Empty values and values containing spaces are double quoted, with embedded
+quotes escaped by a backslash. If the result of the query contains just one
+field, the value is passed back verbatim, without a field name, for example:
+
+Mister X
+
+If the result of the query yields more than one row, it is all concatenated,
+with a newline between the data for each row.
+
+
+9.21 More about MySQL, PostgreSQL, Oracle, and InterBase
+--------------------------------------------------------
+
+If any MySQL, PostgreSQL, Oracle, or InterBase lookups are used, the 
+mysql_servers, pgsql_servers, oracle_servers, or ibase_servers option (as
+appropriate) must be set to a colon-separated list of server information. (For
+MySQL and PostgreSQL only, the global option need not be set if all queries
+contain their own server information - see section 9.22.) Each item in the list
+is a slash-separated list of four items: host name, database name, user name,
+and password. In the case of Oracle, the host name field is used for the
+"service name", and the database name field is not used and should be empty.
+For example:
+
+hide oracle_servers = oracle.plc.example//userx/abcdwxyz
+
+Because password data is sensitive, you should always precede the setting with
+"hide", to prevent non-admin users from obtaining the setting via the -bP
+option. Here is an example where two MySQL servers are listed:
+
+hide mysql_servers = localhost/users/root/secret:\
+                     otherhost/users/root/othersecret
+
+For MySQL and PostgreSQL, a host may be specified as <name>:<port> but because
+this is a colon-separated list, the colon has to be doubled. For each query,
+these parameter groups are tried in order until a connection is made and a
+query is successfully processed. The result of a query may be that no data is
+found, but that is still a successful query. In other words, the list of
+servers provides a backup facility, not a list of different places to look.
+
+The quote_mysql, quote_pgsql, and quote_oracle expansion operators convert
+newline, tab, carriage return, and backspace to \n, \t, \r, and \b
+respectively, and the characters single-quote, double-quote, and backslash
+itself are escaped with backslashes. The quote_pgsql expansion operator, in
+addition, escapes the percent and underscore characters. This cannot be done
+for MySQL because these escapes are not recognized in contexts where these
+characters are not special.
+
+
+9.22 Specifying the server in the query
+---------------------------------------
+
+For MySQL and PostgreSQL lookups (but not currently for Oracle and InterBase),
+it is possible to specify a list of servers with an individual query. This is
+done by starting the query with
+
+servers=server1:server2:server3:...;
+
+Each item in the list may take one of two forms:
+
+ 1. If it contains no slashes it is assumed to be just a host name. The
+    appropriate global option (mysql_servers or pgsql_servers) is searched for
+    a host of the same name, and the remaining parameters (database, user,
+    password) are taken from there.
+
+ 2. If it contains any slashes, it is taken as a complete parameter set.
+
+The list of servers is used in exactly the same way as the global list. Once a
+connection to a server has happened and a query has been successfully executed,
+processing of the lookup ceases.
+
+This feature is intended for use in master/slave situations where updates are
+occurring and you want to update the master rather than a slave. If the master
+is in the list as a backup for reading, you might have a global setting like
+this:
+
+mysql_servers = slave1/db/name/pw:\
+                slave2/db/name/pw:\
+                master/db/name/pw
+
+In an updating lookup, you could then write:
+
+${lookup mysql{servers=master; UPDATE ...} }
+
+That query would then be sent only to the master server. If, on the other hand,
+the master is not to be used for reading, and so is not present in the global
+option, you can still update it by a query of this form:
+
+${lookup pgsql{servers=master/db/name/pw; UPDATE ...} }
+
+
+9.23 Special MySQL features
+---------------------------
+
+For MySQL, an empty host name or the use of "localhost" in mysql_servers causes
+a connection to the server on the local host by means of a Unix domain socket.
+An alternate socket can be specified in parentheses. The full syntax of each
+item in mysql_servers is:
+
+<hostname>::<port>(<socket name>)/<database>/<user>/<password>
+
+Any of the three sub-parts of the first field can be omitted. For normal use on
+the local host it can be left blank or set to just "localhost".
+
+No database need be supplied - but if it is absent here, it must be given in
+the queries.
+
+If a MySQL query is issued that does not request any data (an insert, update,
+or delete command), the result of the lookup is the number of rows affected.
+
+Warning: This can be misleading. If an update does not actually change anything
+(for example, setting a field to the value it already has), the result is zero
+because no rows are affected.
+
+
+9.24 Special PostgreSQL features
+--------------------------------
+
+PostgreSQL lookups can also use Unix domain socket connections to the database.
+This is usually faster and costs less CPU time than a TCP/IP connection.
+However it can be used only if the mail server runs on the same machine as the
+database server. A configuration line for PostgreSQL via Unix domain sockets
+looks like this:
+
+hide pgsql_servers = (/tmp/.s.PGSQL.5432)/db/user/password : ...
+
+In other words, instead of supplying a host name, a path to the socket is
+given. The path name is enclosed in parentheses so that its slashes aren't
+visually confused with the delimiters for the other server parameters.
+
+If a PostgreSQL query is issued that does not request any data (an insert,
+update, or delete command), the result of the lookup is the number of rows
+affected.
+
+
+9.25 More about SQLite
+----------------------
+
+SQLite is different to the other SQL lookups because a file name is required in
+addition to the SQL query. An SQLite database is a single file, and there is no
+daemon as in the other SQL databases. The interface to Exim requires the name
+of the file, as an absolute path, to be given at the start of the query. It is
+separated from the query by white space. This means that the path name cannot
+contain white space. Here is a lookup expansion example:
+
+${lookup sqlite {/some/thing/sqlitedb \
+  select name from aliases where id='userx';}}
+
+In a list, the syntax is similar. For example:
+
+domainlist relay_to_domains = sqlite;/some/thing/sqlitedb \
+   select * from relays where ip='$sender_host_address';
+
+The only character affected by the quote_sqlite operator is a single quote,
+which it doubles.
+
+The SQLite library handles multiple simultaneous accesses to the database
+internally. Multiple readers are permitted, but only one process can update at
+once. Attempts to access the database while it is being updated are rejected
+after a timeout period, during which the SQLite library waits for the lock to
+be released. In Exim, the default timeout is set to 5 seconds, but it can be
+changed by means of the sqlite_lock_timeout option.
+
+
+
+===============================================================================
+10. DOMAIN, HOST, ADDRESS, AND LOCAL PART LISTS
+
+A number of Exim configuration options contain lists of domains, hosts, email
+addresses, or local parts. For example, the hold_domains option contains a list
+of domains whose delivery is currently suspended. These lists are also used as
+data in ACL statements (see chapter 42), and as arguments to expansion
+conditions such as match_domain.
+
+Each item in one of these lists is a pattern to be matched against a domain,
+host, email address, or local part, respectively. In the sections below, the
+different types of pattern for each case are described, but first we cover some
+general facilities that apply to all four kinds of list.
+
+
+10.1 Expansion of lists
+-----------------------
+
+Each list is expanded as a single string before it is used. The result of
+expansion must be a list, possibly containing empty items, which is split up
+into separate items for matching. By default, colon is the separator character,
+but this can be varied if necessary. See sections 6.19 and 6.21 for details of
+the list syntax; the second of these discusses the way to specify empty list
+items.
+
+If the string expansion is forced to fail, Exim behaves as if the item it is
+testing (domain, host, address, or local part) is not in the list. Other
+expansion failures cause temporary errors.
+
+If an item in a list is a regular expression, backslashes, dollars and possibly
+other special characters in the expression must be protected against
+misinterpretation by the string expander. The easiest way to do this is to use
+the "\N" expansion feature to indicate that the contents of the regular
+expression should not be expanded. For example, in an ACL you might have:
+
+deny senders = \N^\d{8}\w@.*\.baddomain\.example$\N : \
+               ${lookup{$domain}lsearch{/badsenders/bydomain}}
+
+The first item is a regular expression that is protected from expansion by "\
+N", whereas the second uses the expansion to obtain a list of unwanted senders
+based on the receiving domain.
+
+
+10.2 Negated items in lists
+---------------------------
+
+Items in a list may be positive or negative. Negative items are indicated by a
+leading exclamation mark, which may be followed by optional white space. A list
+defines a set of items (domains, etc). When Exim processes one of these lists,
+it is trying to find out whether a domain, host, address, or local part
+(respectively) is in the set that is defined by the list. It works like this:
+
+The list is scanned from left to right. If a positive item is matched, the
+subject that is being checked is in the set; if a negative item is matched, the
+subject is not in the set. If the end of the list is reached without the
+subject having matched any of the patterns, it is in the set if the last item
+was a negative one, but not if it was a positive one. For example, the list in
+
+domainlist relay_to_domains = !a.b.c : *.b.c
+
+matches any domain ending in .b.c except for a.b.c. Domains that match neither 
+a.b.c nor *.b.c do not match, because the last item in the list is positive.
+However, if the setting were
+
+domainlist relay_to_domains = !a.b.c
+
+then all domains other than a.b.c would match because the last item in the list
+is negative. In other words, a list that ends with a negative item behaves as
+if it had an extra item ":*" on the end.
+
+Another way of thinking about positive and negative items in lists is to read
+the connector as "or" after a positive item and as "and" after a negative item.
+
+
+10.3 File names in lists
+------------------------
+
+If an item in a domain, host, address, or local part list is an absolute file
+name (beginning with a slash character), each line of the file is read and
+processed as if it were an independent item in the list, except that further
+file names are not allowed, and no expansion of the data from the file takes
+place. Empty lines in the file are ignored, and the file may also contain
+comment lines:
+
+  * For domain and host lists, if a # character appears anywhere in a line of
+    the file, it and all following characters are ignored.
+
+  * Because local parts may legitimately contain # characters, a comment in an
+    address list or local part list file is recognized only if # is preceded by
+    white space or the start of the line. For example:
+
+    not#comment@x.y.z   # but this is a comment
+
+Putting a file name in a list has the same effect as inserting each line of the
+file as an item in the list (blank lines and comments excepted). However, there
+is one important difference: the file is read each time the list is processed,
+so if its contents vary over time, Exim's behaviour changes.
+
+If a file name is preceded by an exclamation mark, the sense of any match
+within the file is inverted. For example, if
+
+hold_domains = !/etc/nohold-domains
+
+and the file contains the lines
+
+!a.b.c
+*.b.c
+
+then a.b.c is in the set of domains defined by hold_domains, whereas any domain
+matching "*.b.c" is not.
+
+
+10.4 An lsearch file is not an out-of-line list
+-----------------------------------------------
+
+As will be described in the sections that follow, lookups can be used in lists
+to provide indexed methods of checking list membership. There has been some
+confusion about the way lsearch lookups work in lists. Because an lsearch file
+contains plain text and is scanned sequentially, it is sometimes thought that
+it is allowed to contain wild cards and other kinds of non-constant pattern.
+This is not the case. The keys in an lsearch file are always fixed strings,
+just as for any other single-key lookup type.
+
+If you want to use a file to contain wild-card patterns that form part of a
+list, just give the file name on its own, without a search type, as described
+in the previous section. You could also use the wildlsearch or nwildlsearch,
+but there is no advantage in doing this.
+
+
+10.5 Named lists
+----------------
+
+A list of domains, hosts, email addresses, or local parts can be given a name
+which is then used to refer to the list elsewhere in the configuration. This is
+particularly convenient if the same list is required in several different
+places. It also allows lists to be given meaningful names, which can improve
+the readability of the configuration. For example, it is conventional to define
+a domain list called local_domains for all the domains that are handled locally
+on a host, using a configuration line such as
+
+domainlist local_domains = localhost:my.dom.example
+
+Named lists are referenced by giving their name preceded by a plus sign, so,
+for example, a router that is intended to handle local domains would be
+configured with the line
+
+domains = +local_domains
+
+The first router in a configuration is often one that handles all domains
+except the local ones, using a configuration with a negated item like this:
+
+dnslookup:
+  driver = dnslookup
+  domains = ! +local_domains
+  transport = remote_smtp
+  no_more
+
+The four kinds of named list are created by configuration lines starting with
+the words domainlist, hostlist, addresslist, or localpartlist, respectively.
+Then there follows the name that you are defining, followed by an equals sign
+and the list itself. For example:
+
+hostlist    relay_from_hosts = 192.168.23.0/24 : my.friend.example
+addresslist bad_senders = cdb;/etc/badsenders
+
+A named list may refer to other named lists:
+
+domainlist  dom1 = first.example : second.example
+domainlist  dom2 = +dom1 : third.example
+domainlist  dom3 = fourth.example : +dom2 : fifth.example
+
+Warning: If the last item in a referenced list is a negative one, the effect
+may not be what you intended, because the negation does not propagate out to
+the higher level. For example, consider:
+
+domainlist  dom1 = !a.b
+domainlist  dom2 = +dom1 : *.b
+
+The second list specifies "either in the dom1 list or *.b". The first list
+specifies just "not a.b", so the domain x.y matches it. That means it matches
+the second list as well. The effect is not the same as
+
+domainlist  dom2 = !a.b : *.b
+
+where x.y does not match. It's best to avoid negation altogether in referenced
+lists if you can.
+
+Named lists may have a performance advantage. When Exim is routing an address
+or checking an incoming message, it caches the result of tests on named lists.
+So, if you have a setting such as
+
+domains = +local_domains
+
+on several of your routers or in several ACL statements, the actual test is
+done only for the first one. However, the caching works only if there are no
+expansions within the list itself or any sublists that it references. In other
+words, caching happens only for lists that are known to be the same each time
+they are referenced.
+
+By default, there may be up to 16 named lists of each type. This limit can be
+extended by changing a compile-time variable. The use of domain and host lists
+is recommended for concepts such as local domains, relay domains, and relay
+hosts. The default configuration is set up like this.
+
+
+10.6 Named lists compared with macros
+-------------------------------------
+
+At first sight, named lists might seem to be no different from macros in the
+configuration file. However, macros are just textual substitutions. If you
+write
+
+ALIST = host1 : host2
+auth_advertise_hosts = !ALIST
+
+it probably won't do what you want, because that is exactly the same as
+
+auth_advertise_hosts = !host1 : host2
+
+Notice that the second host name is not negated. However, if you use a host
+list, and write
+
+hostlist alist = host1 : host2
+auth_advertise_hosts = ! +alist
+
+the negation applies to the whole list, and so that is equivalent to
+
+auth_advertise_hosts = !host1 : !host2
+
+
+10.7 Named list caching
+-----------------------
+
+While processing a message, Exim caches the result of checking a named list if
+it is sure that the list is the same each time. In practice, this means that
+the cache operates only if the list contains no $ characters, which guarantees
+that it will not change when it is expanded. Sometimes, however, you may have
+an expanded list that you know will be the same each time within a given
+message. For example:
+
+domainlist special_domains = \
+           ${lookup{$sender_host_address}cdb{/some/file}}
+
+This provides a list of domains that depends only on the sending host's IP
+address. If this domain list is referenced a number of times (for example, in
+several ACL lines, or in several routers) the result of the check is not cached
+by default, because Exim does not know that it is going to be the same list
+each time.
+
+By appending "_cache" to "domainlist" you can tell Exim to go ahead and cache
+the result anyway. For example:
+
+domainlist_cache special_domains = ${lookup{...
+
+If you do this, you should be absolutely sure that caching is going to do the
+right thing in all cases. When in doubt, leave it out.
+
+
+10.8 Domain lists
+-----------------
+
+Domain lists contain patterns that are to be matched against a mail domain. The
+following types of item may appear in domain lists:
+
+  * If a pattern consists of a single @ character, it matches the local host
+    name, as set by the primary_hostname option (or defaulted). This makes it
+    possible to use the same configuration file on several different hosts that
+    differ only in their names.
+
+  * If a pattern consists of the string "@[]" it matches an IP address enclosed
+    in square brackets (as in an email address that contains a domain literal),
+    but only if that IP address is recognized as local for email routing
+    purposes. The local_interfaces and extra_local_interfaces options can be
+    used to control which of a host's several IP addresses are treated as
+    local. In today's Internet, the use of domain literals is controversial.
+
+  * If a pattern consists of the string "@mx_any" it matches any domain that
+    has an MX record pointing to the local host or to any host that is listed
+    in hosts_treat_as_local. The items "@mx_primary" and "@mx_secondary" are
+    similar, except that the first matches only when a primary MX target is the
+    local host, and the second only when no primary MX target is the local
+    host, but a secondary MX target is. "Primary" means an MX record with the
+    lowest preference value - there may of course be more than one of them.
+
+    The MX lookup that takes place when matching a pattern of this type is
+    performed with the resolver options for widening names turned off. Thus,
+    for example, a single-component domain will not be expanded by adding the
+    resolver's default domain. See the qualify_single and search_parents
+    options of the dnslookup router for a discussion of domain widening.
+
+    Sometimes you may want to ignore certain IP addresses when using one of
+    these patterns. You can specify this by following the pattern with "/ignore
+    ="<ip list>, where <ip list> is a list of IP addresses. These addresses are
+    ignored when processing the pattern (compare the ignore_target_hosts option
+    on a router). For example:
+
+    domains = @mx_any/ignore=127.0.0.1
+
+    This example matches any domain that has an MX record pointing to one of
+    the local host's IP addresses other than 127.0.0.1.
+
+    The list of IP addresses is in fact processed by the same code that
+    processes host lists, so it may contain CIDR-coded network specifications
+    and it may also contain negative items.
+
+    Because the list of IP addresses is a sublist within a domain list, you
+    have to be careful about delimiters if there is more than one address. Like
+    any other list, the default delimiter can be changed. Thus, you might have:
+
+    domains = @mx_any/ignore=<;127.0.0.1;0.0.0.0 : \
+              an.other.domain : ...
+
+    so that the sublist uses semicolons for delimiters. When IPv6 addresses are
+    involved, it is easiest to change the delimiter for the main list as well:
+
+    domains = <? @mx_any/ignore=<;127.0.0.1;::1 ? \
+              an.other.domain ? ...
+
+  * If a pattern starts with an asterisk, the remaining characters of the
+    pattern are compared with the terminating characters of the domain. The use
+    of "*" in domain lists differs from its use in partial matching lookups. In
+    a domain list, the character following the asterisk need not be a dot,
+    whereas partial matching works only in terms of dot-separated components.
+    For example, a domain list item such as "*key.ex" matches donkey.ex as well
+    as cipher.key.ex.
+
+  * If a pattern starts with a circumflex character, it is treated as a regular
+    expression, and matched against the domain using a regular expression
+    matching function. The circumflex is treated as part of the regular
+    expression. Email domains are case-independent, so this regular expression
+    match is by default case-independent, but you can make it case-dependent by
+    starting it with "(?-i)". References to descriptions of the syntax of
+    regular expressions are given in chapter 8.
+
+    Warning: Because domain lists are expanded before being processed, you must
+    escape any backslash and dollar characters in the regular expression, or
+    use the special "\N" sequence (see chapter 11) to specify that it is not to
+    be expanded (unless you really do want to build a regular expression by
+    expansion, of course).
+
+  * If a pattern starts with the name of a single-key lookup type followed by a
+    semicolon (for example, "dbm;" or "lsearch;"), the remainder of the pattern
+    must be a file name in a suitable format for the lookup type. For example,
+    for "cdb;" it must be an absolute path:
+
+    domains = cdb;/etc/mail/local_domains.cdb
+
+    The appropriate type of lookup is done on the file using the domain name as
+    the key. In most cases, the data that is looked up is not used; Exim is
+    interested only in whether or not the key is present in the file. However,
+    when a lookup is used for the domains option on a router or a domains
+    condition in an ACL statement, the data is preserved in the $domain_data
+    variable and can be referred to in other router options or other statements
+    in the same ACL.
+
+  * Any of the single-key lookup type names may be preceded by "partial"<n>"-",
+    where the <n> is optional, for example,
+
+    domains = partial-dbm;/partial/domains
+
+    This causes partial matching logic to be invoked; a description of how this
+    works is given in section 9.7.
+
+  * Any of the single-key lookup types may be followed by an asterisk. This
+    causes a default lookup for a key consisting of a single asterisk to be
+    done if the original lookup fails. This is not a useful feature when using
+    a domain list to select particular domains (because any domain would
+    match), but it might have value if the result of the lookup is being used
+    via the $domain_data expansion variable.
+
+  * If the pattern starts with the name of a query-style lookup type followed
+    by a semicolon (for example, "nisplus;" or "ldap;"), the remainder of the
+    pattern must be an appropriate query for the lookup type, as described in
+    chapter 9. For example:
+
+    hold_domains = mysql;select domain from holdlist \
+      where domain = '${quote_mysql:$domain}';
+
+    In most cases, the data that is looked up is not used (so for an SQL query,
+    for example, it doesn't matter what field you select). Exim is interested
+    only in whether or not the query succeeds. However, when a lookup is used
+    for the domains option on a router, the data is preserved in the
+    $domain_data variable and can be referred to in other options.
+
+  * If none of the above cases apply, a caseless textual comparison is made
+    between the pattern and the domain.
+
+Here is an example that uses several different kinds of pattern:
+
+domainlist funny_domains = \
+  @ : \
+  lib.unseen.edu : \
+  *.foundation.fict.example : \
+  \N^[1-2]\d{3}\.fict\.example$\N : \
+  partial-dbm;/opt/data/penguin/book : \
+  nis;domains.byname : \
+  nisplus;[name=$domain,status=local],domains.org_dir
+
+There are obvious processing trade-offs among the various matching modes. Using
+an asterisk is faster than a regular expression, and listing a few names
+explicitly probably is too. The use of a file or database lookup is expensive,
+but may be the only option if hundreds of names are required. Because the
+patterns are tested in order, it makes sense to put the most commonly matched
+patterns earlier.
+
+
+10.9 Host lists
+---------------
+
+Host lists are used to control what remote hosts are allowed to do. For
+example, some hosts may be allowed to use the local host as a relay, and some
+may be permitted to use the SMTP ETRN command. Hosts can be identified in two
+different ways, by name or by IP address. In a host list, some types of pattern
+are matched to a host name, and some are matched to an IP address. You need to
+be particularly careful with this when single-key lookups are involved, to
+ensure that the right value is being used as the key.
+
+
+10.10 Special host list patterns
+--------------------------------
+
+If a host list item is the empty string, it matches only when no remote host is
+involved. This is the case when a message is being received from a local
+process using SMTP on the standard input, that is, when a TCP/IP connection is
+not used.
+
+The special pattern "*" in a host list matches any host or no host. Neither the
+IP address nor the name is actually inspected.
+
+
+10.11 Host list patterns that match by IP address
+-------------------------------------------------
+
+If an IPv4 host calls an IPv6 host and the call is accepted on an IPv6 socket,
+the incoming address actually appears in the IPv6 host as "::ffff:"<v4address>.
+When such an address is tested against a host list, it is converted into a
+traditional IPv4 address first. (Not all operating systems accept IPv4 calls on
+IPv6 sockets, as there have been some security concerns.)
+
+The following types of pattern in a host list check the remote host by
+inspecting its IP address:
+
+  * If the pattern is a plain domain name (not a regular expression, not
+    starting with *, not a lookup of any kind), Exim calls the operating system
+    function to find the associated IP address(es). Exim uses the newer
+    getipnodebyname() function when available, otherwise gethostbyname(). This
+    typically causes a forward DNS lookup of the name. The result is compared
+    with the IP address of the subject host.
+
+    If there is a temporary problem (such as a DNS timeout) with the host name
+    lookup, a temporary error occurs. For example, if the list is being used in
+    an ACL condition, the ACL gives a "defer" response, usually leading to a
+    temporary SMTP error code. If no IP address can be found for the host name,
+    what happens is described in section 10.14 below.
+
+  * If the pattern is "@", the primary host name is substituted and used as a
+    domain name, as just described.
+
+  * If the pattern is an IP address, it is matched against the IP address of
+    the subject host. IPv4 addresses are given in the normal "dotted-quad"
+    notation. IPv6 addresses can be given in colon-separated format, but the
+    colons have to be doubled so as not to be taken as item separators when the
+    default list separator is used. IPv6 addresses are recognized even when
+    Exim is compiled without IPv6 support. This means that if they appear in a
+    host list on an IPv4-only host, Exim will not treat them as host names.
+    They are just addresses that can never match a client host.
+
+  * If the pattern is "@[]", it matches the IP address of any IP interface on
+    the local host. For example, if the local host is an IPv4 host with one
+    interface address 10.45.23.56, these two ACL statements have the same
+    effect:
+
+    accept hosts = 127.0.0.1 : 10.45.23.56
+    accept hosts = @[]
+
+  * If the pattern is an IP address followed by a slash and a mask length (for
+    example 10.11.42.0/24), it is matched against the IP address of the subject
+    host under the given mask. This allows, an entire network of hosts to be
+    included (or excluded) by a single item. The mask uses CIDR notation; it
+    specifies the number of address bits that must match, starting from the
+    most significant end of the address.
+
+    Note: The mask is not a count of addresses, nor is it the high number of a
+    range of addresses. It is the number of bits in the network portion of the
+    address. The above example specifies a 24-bit netmask, so it matches all
+    256 addresses in the 10.11.42.0 network. An item such as
+
+    192.168.23.236/31
+
+    matches just two addresses, 192.168.23.236 and 192.168.23.237. A mask value
+    of 32 for an IPv4 address is the same as no mask at all; just a single
+    address matches.
+
+    Here is another example which shows an IPv4 and an IPv6 network:
+
+    recipient_unqualified_hosts = 192.168.0.0/16: \
+                                  3ffe::ffff::836f::::/48
+
+    The doubling of list separator characters applies only when these items
+    appear inline in a host list. It is not required when indirecting via a
+    file. For example:
+
+    recipient_unqualified_hosts = /opt/exim/unqualnets
+
+    could make use of a file containing
+
+    172.16.0.0/12
+    3ffe:ffff:836f::/48
+
+    to have exactly the same effect as the previous example. When listing IPv6
+    addresses inline, it is usually more convenient to use the facility for
+    changing separator characters. This list contains the same two networks:
+
+    recipient_unqualified_hosts = <; 172.16.0.0/12; \
+                                     3ffe:ffff:836f::/48
+
+    The separator is changed to semicolon by the leading "<;" at the start of
+    the list.
+
+
+10.12 Host list patterns for single-key lookups by host address
+---------------------------------------------------------------
+
+When a host is to be identified by a single-key lookup of its complete IP
+address, the pattern takes this form:
+
+net-<single-key-search-type>;<search-data>
+
+For example:
+
+hosts_lookup = net-cdb;/hosts-by-ip.db
+
+The text form of the IP address of the subject host is used as the lookup key.
+IPv6 addresses are converted to an unabbreviated form, using lower case
+letters, with dots as separators because colon is the key terminator in lsearch
+files. [Colons can in fact be used in keys in lsearch files by quoting the
+keys, but this is a facility that was added later.] The data returned by the
+lookup is not used.
+
+Single-key lookups can also be performed using masked IP addresses, using
+patterns of this form:
+
+net<number>-<single-key-search-type>;<search-data>
+
+For example:
+
+net24-dbm;/networks.db
+
+The IP address of the subject host is masked using <number> as the mask length.
+A textual string is constructed from the masked value, followed by the mask,
+and this is used as the lookup key. For example, if the host's IP address is
+192.168.34.6, the key that is looked up for the above example is "192.168.34.0/
+24".
+
+When an IPv6 address is converted to a string, dots are normally used instead
+of colons, so that keys in lsearch files need not contain colons (which
+terminate lsearch keys). This was implemented some time before the ability to
+quote keys was made available in lsearch files. However, the more recently
+implemented iplsearch files do require colons in IPv6 keys (notated using the
+quoting facility) so as to distinguish them from IPv4 keys. For this reason,
+when the lookup type is iplsearch, IPv6 addresses are converted using colons
+and not dots. In all cases, full, unabbreviated IPv6 addresses are always used.
+
+Ideally, it would be nice to tidy up this anomalous situation by changing to
+colons in all cases, given that quoting is now available for lsearch. However,
+this would be an incompatible change that might break some existing
+configurations.
+
+Warning: Specifying net32- (for an IPv4 address) or net128- (for an IPv6
+address) is not the same as specifying just net- without a number. In the
+former case the key strings include the mask value, whereas in the latter case
+the IP address is used on its own.
+
+
+10.13 Host list patterns that match by host name
+------------------------------------------------
+
+There are several types of pattern that require Exim to know the name of the
+remote host. These are either wildcard patterns or lookups by name. (If a
+complete hostname is given without any wildcarding, it is used to find an IP
+address to match against, as described in section 10.11 above.)
+
+If the remote host name is not already known when Exim encounters one of these
+patterns, it has to be found from the IP address. Although many sites on the
+Internet are conscientious about maintaining reverse DNS data for their hosts,
+there are also many that do not do this. Consequently, a name cannot always be
+found, and this may lead to unwanted effects. Take care when configuring host
+lists with wildcarded name patterns. Consider what will happen if a name cannot
+be found.
+
+Because of the problems of determining host names from IP addresses, matching
+against host names is not as common as matching against IP addresses.
+
+By default, in order to find a host name, Exim first does a reverse DNS lookup;
+if no name is found in the DNS, the system function (gethostbyaddr() or
+getipnodebyaddr() if available) is tried. The order in which these lookups are
+done can be changed by setting the host_lookup_order option. For security, once
+Exim has found one or more names, it looks up the IP addresses for these names
+and compares them with the IP address that it started with. Only those names
+whose IP addresses match are accepted. Any other names are discarded. If no
+names are left, Exim behaves as if the host name cannot be found. In the most
+common case there is only one name and one IP address.
+
+There are some options that control what happens if a host name cannot be
+found. These are described in section 10.14 below.
+
+As a result of aliasing, hosts may have more than one name. When processing any
+of the following types of pattern, all the host's names are checked:
+
+  * If a pattern starts with "*" the remainder of the item must match the end
+    of the host name. For example, "*.b.c" matches all hosts whose names end in
+    .b.c. This special simple form is provided because this is a very common
+    requirement. Other kinds of wildcarding require the use of a regular
+    expression.
+
+  * If the item starts with "^" it is taken to be a regular expression which is
+    matched against the host name. Host names are case-independent, so this
+    regular expression match is by default case-independent, but you can make
+    it case-dependent by starting it with "(?-i)". References to descriptions
+    of the syntax of regular expressions are given in chapter 8. For example,
+
+    ^(a|b)\.c\.d$
+
+    is a regular expression that matches either of the two hosts a.c.d or b.c.d
+    . When a regular expression is used in a host list, you must take care that
+    backslash and dollar characters are not misinterpreted as part of the
+    string expansion. The simplest way to do this is to use "\N" to mark that
+    part of the string as non-expandable. For example:
+
+    sender_unqualified_hosts = \N^(a|b)\.c\.d$\N : ....
+
+    Warning: If you want to match a complete host name, you must include the
+    "$" terminating metacharacter in the regular expression, as in the above
+    example. Without it, a match at the start of the host name is all that is
+    required.
+
+
+10.14 Behaviour when an IP address or name cannot be found
+----------------------------------------------------------
+
+While processing a host list, Exim may need to look up an IP address from a
+name (see section 10.11), or it may need to look up a host name from an IP
+address (see section 10.13). In either case, the behaviour when it fails to
+find the information it is seeking is the same.
+
+Note: This section applies to permanent lookup failures. It does not apply to
+temporary DNS errors, whose handling is described in the next section.
+
+Exim parses a host list from left to right. If it encounters a permanent lookup
+failure in any item in the host list before it has found a match, Exim treats
+it as a failure and the default behavior is as if the host does not match the
+list. This may not always be what you want to happen. To change Exim's
+behaviour, the special items "+include_unknown" or "+ignore_unknown" may appear
+in the list (at top level - they are not recognized in an indirected file).
+
+  * If any item that follows "+include_unknown" requires information that
+    cannot found, Exim behaves as if the host does match the list. For example,
+
+    host_reject_connection = +include_unknown:*.enemy.ex
+
+    rejects connections from any host whose name matches "*.enemy.ex", and also
+    any hosts whose name it cannot find.
+
+  * If any item that follows "+ignore_unknown" requires information that cannot
+    be found, Exim ignores that item and proceeds to the rest of the list. For
+    example:
+
+    accept hosts = +ignore_unknown : friend.example : \
+                   192.168.4.5
+
+    accepts from any host whose name is friend.example and from 192.168.4.5,
+    whether or not its host name can be found. Without "+ignore_unknown", if no
+    name can be found for 192.168.4.5, it is rejected.
+
+Both "+include_unknown" and "+ignore_unknown" may appear in the same list. The
+effect of each one lasts until the next, or until the end of the list.
+
+
+10.15 Mixing wildcarded host names and addresses in host lists
+--------------------------------------------------------------
+
+This section explains the host/ip processing logic with the same concepts as
+the previous section, but specifically addresses what happens when a wildcarded
+hostname is one of the items in the hostlist.
+
+  * If you have name lookups or wildcarded host names and IP addresses in the
+    same host list, you should normally put the IP addresses first. For
+    example, in an ACL you could have:
+
+    accept hosts = 10.9.8.7 : *.friend.example
+
+    The reason you normally would order it this way lies in the left-to-right
+    way that Exim processes lists. It can test IP addresses without doing any
+    DNS lookups, but when it reaches an item that requires a host name, it
+    fails if it cannot find a host name to compare with the pattern. If the
+    above list is given in the opposite order, the accept statement fails for a
+    host whose name cannot be found, even if its IP address is 10.9.8.7.
+
+  * If you really do want to do the name check first, and still recognize the
+    IP address, you can rewrite the ACL like this:
+
+    accept hosts = *.friend.example
+    accept hosts = 10.9.8.7
+
+    If the first accept fails, Exim goes on to try the second one. See chapter
+    42 for details of ACLs. Alternatively, you can use "+ignore_unknown", which
+    was discussed in depth in the first example in this section.
+
+
+10.16 Temporary DNS errors when looking up host information
+-----------------------------------------------------------
+
+A temporary DNS lookup failure normally causes a defer action (except when 
+dns_again_means_nonexist converts it into a permanent error). However, host
+lists can include "+ignore_defer" and "+include_defer", analagous to
+"+ignore_unknown" and "+include_unknown", as described in the previous section.
+These options should be used with care, probably only in non-critical host
+lists such as whitelists.
+
+
+10.17 Host list patterns for single-key lookups by host name
+------------------------------------------------------------
+
+If a pattern is of the form
+
+<single-key-search-type>;<search-data>
+
+for example
+
+dbm;/host/accept/list
+
+a single-key lookup is performed, using the host name as its key. If the lookup
+succeeds, the host matches the item. The actual data that is looked up is not
+used.
+
+Reminder: With this kind of pattern, you must have host names as keys in the
+file, not IP addresses. If you want to do lookups based on IP addresses, you
+must precede the search type with "net-" (see section 10.12). There is,
+however, no reason why you could not use two items in the same list, one doing
+an address lookup and one doing a name lookup, both using the same file.
+
+
+10.18 Host list patterns for query-style lookups
+------------------------------------------------
+
+If a pattern is of the form
+
+<query-style-search-type>;<query>
+
+the query is obeyed, and if it succeeds, the host matches the item. The actual
+data that is looked up is not used. The variables $sender_host_address and
+$sender_host_name can be used in the query. For example:
+
+hosts_lookup = pgsql;\
+  select ip from hostlist where ip='$sender_host_address'
+
+The value of $sender_host_address for an IPv6 address contains colons. You can
+use the sg expansion item to change this if you need to. If you want to use
+masked IP addresses in database queries, you can use the mask expansion
+operator.
+
+If the query contains a reference to $sender_host_name, Exim automatically
+looks up the host name if it has not already done so. (See section 10.13 for
+comments on finding host names.)
+
+Historical note: prior to release 4.30, Exim would always attempt to find a
+host name before running the query, unless the search type was preceded by
+"net-". This is no longer the case. For backwards compatibility, "net-" is
+still recognized for query-style lookups, but its presence or absence has no
+effect. (Of course, for single-key lookups, "net-" is important. See section
+10.12.)
+
+
+10.19 Address lists
+-------------------
+
+Address lists contain patterns that are matched against mail addresses. There
+is one special case to be considered: the sender address of a bounce message is
+always empty. You can test for this by providing an empty item in an address
+list. For example, you can set up a router to process bounce messages by using
+this option setting:
+
+senders = :
+
+The presence of the colon creates an empty item. If you do not provide any
+data, the list is empty and matches nothing. The empty sender can also be
+detected by a regular expression that matches an empty string, and by a
+query-style lookup that succeeds when $sender_address is empty.
+
+Non-empty items in an address list can be straightforward email addresses. For
+example:
+
+senders = jbc@askone.example : hs@anacreon.example
+
+A certain amount of wildcarding is permitted. If a pattern contains an @
+character, but is not a regular expression and does not begin with a
+semicolon-terminated lookup type (described below), the local part of the
+subject address is compared with the local part of the pattern, which may start
+with an asterisk. If the local parts match, the domain is checked in exactly
+the same way as for a pattern in a domain list. For example, the domain can be
+wildcarded, refer to a named list, or be a lookup:
+
+deny senders = *@*.spamming.site:\
+               *@+hostile_domains:\
+               bozo@partial-lsearch;/list/of/dodgy/sites:\
+               *@dbm;/bad/domains.db
+
+If a local part that begins with an exclamation mark is required, it has to be
+specified using a regular expression, because otherwise the exclamation mark is
+treated as a sign of negation, as is standard in lists.
+
+If a non-empty pattern that is not a regular expression or a lookup does not
+contain an @ character, it is matched against the domain part of the subject
+address. The only two formats that are recognized this way are a literal
+domain, or a domain pattern that starts with *. In both these cases, the effect
+is the same as if "*@" preceded the pattern. For example:
+
+deny senders = enemy.domain : *.enemy.domain
+
+The following kinds of more complicated address list pattern can match any
+address, including the empty address that is characteristic of bounce message
+senders:
+
+  * If (after expansion) a pattern starts with "^", a regular expression match
+    is done against the complete address, with the pattern as the regular
+    expression. You must take care that backslash and dollar characters are not
+    misinterpreted as part of the string expansion. The simplest way to do this
+    is to use "\N" to mark that part of the string as non-expandable. For
+    example:
+
+    deny senders = \N^.*this.*@example\.com$\N : \
+                   \N^\d{8}.+@spamhaus.example$\N : ...
+
+    The "\N" sequences are removed by the expansion, so these items do indeed
+    start with "^" by the time they are being interpreted as address patterns.
+
+  * Complete addresses can be looked up by using a pattern that starts with a
+    lookup type terminated by a semicolon, followed by the data for the lookup.
+    For example:
+
+    deny senders = cdb;/etc/blocked.senders : \
+      mysql;select address from blocked where \
+      address='${quote_mysql:$sender_address}'
+
+    Both query-style and single-key lookup types can be used. For a single-key
+    lookup type, Exim uses the complete address as the key. However, empty keys
+    are not supported for single-key lookups, so a match against the empty
+    address always fails. This restriction does not apply to query-style
+    lookups.
+
+    Partial matching for single-key lookups (section 9.7) cannot be used, and
+    is ignored if specified, with an entry being written to the panic log. 
+    However, you can configure lookup defaults, as described in section 9.6,
+    but this is useful only for the "*@" type of default. For example, with
+    this lookup:
+
+    accept senders = lsearch*@;/some/file
+
+    the file could contains lines like this:
+
+    user1@domain1.example
+    *@domain2.example
+
+    and for the sender address nimrod@jaeger.example, the sequence of keys that
+    are tried is:
+
+    nimrod@jaeger.example
+    *@jaeger.example
+    *
+
+    Warning 1: Do not include a line keyed by "*" in the file, because that
+    would mean that every address matches, thus rendering the test useless.
+
+    Warning 2: Do not confuse these two kinds of item:
+
+    deny recipients = dbm*@;/some/file
+    deny recipients = *@dbm;/some/file
+
+    The first does a whole address lookup, with defaulting, as just described,
+    because it starts with a lookup type. The second matches the local part and
+    domain independently, as described in a bullet point below.
+
+The following kinds of address list pattern can match only non-empty addresses.
+If the subject address is empty, a match against any of these pattern types
+always fails.
+
+  * If a pattern starts with "@@" followed by a single-key lookup item (for
+    example, "@@lsearch;/some/file"), the address that is being checked is
+    split into a local part and a domain. The domain is looked up in the file.
+    If it is not found, there is no match. If it is found, the data that is
+    looked up from the file is treated as a colon-separated list of local part
+    patterns, each of which is matched against the subject local part in turn.
+
+    The lookup may be a partial one, and/or one involving a search for a
+    default keyed by "*" (see section 9.6). The local part patterns that are
+    looked up can be regular expressions or begin with "*", or even be further
+    lookups. They may also be independently negated. For example, with
+
+    deny senders = @@dbm;/etc/reject-by-domain
+
+    the data from which the DBM file is built could contain lines like
+
+    baddomain.com:  !postmaster : *
+
+    to reject all senders except postmaster from that domain.
+
+    If a local part that actually begins with an exclamation mark is required,
+    it has to be specified using a regular expression. In lsearch files, an
+    entry may be split over several lines by indenting the second and
+    subsequent lines, but the separating colon must still be included at line
+    breaks. White space surrounding the colons is ignored. For example:
+
+    aol.com:  spammer1 : spammer2 : ^[0-9]+$ :
+      spammer3 : spammer4
+
+    As in all colon-separated lists in Exim, a colon can be included in an item
+    by doubling.
+
+    If the last item in the list starts with a right angle-bracket, the
+    remainder of the item is taken as a new key to look up in order to obtain a
+    continuation list of local parts. The new key can be any sequence of
+    characters. Thus one might have entries like
+
+    aol.com: spammer1 : spammer 2 : >*
+    xyz.com: spammer3 : >*
+    *:       ^\d{8}$
+
+    in a file that was searched with @@dbm*, to specify a match for 8-digit
+    local parts for all domains, in addition to the specific local parts listed
+    for each domain. Of course, using this feature costs another lookup each
+    time a chain is followed, but the effort needed to maintain the data is
+    reduced.
+
+    It is possible to construct loops using this facility, and in order to
+    catch them, the chains may be no more than fifty items long.
+
+  * The @@<lookup> style of item can also be used with a query-style lookup,
+    but in this case, the chaining facility is not available. The lookup can
+    only return a single list of local parts.
+
+Warning: There is an important difference between the address list items in
+these two examples:
+
+senders = +my_list
+senders = *@+my_list
+
+In the first one, "my_list" is a named address list, whereas in the second
+example it is a named domain list.
+
+
+10.20 Case of letters in address lists
+--------------------------------------
+
+Domains in email addresses are always handled caselessly, but for local parts
+case may be significant on some systems (see caseful_local_part for how Exim
+deals with this when routing addresses). However, RFC 2505 (Anti-Spam
+Recommendations for SMTP MTAs) suggests that matching of addresses to blocking
+lists should be done in a case-independent manner. Since most address lists in
+Exim are used for this kind of control, Exim attempts to do this by default.
+
+The domain portion of an address is always lowercased before matching it to an
+address list. The local part is lowercased by default, and any string
+comparisons that take place are done caselessly. This means that the data in
+the address list itself, in files included as plain file names, and in any file
+that is looked up using the "@@" mechanism, can be in any case. However, the
+keys in files that are looked up by a search type other than lsearch (which
+works caselessly) must be in lower case, because these lookups are not
+case-independent.
+
+To allow for the possibility of caseful address list matching, if an item in an
+address list is the string "+caseful", the original case of the local part is
+restored for any comparisons that follow, and string comparisons are no longer
+case-independent. This does not affect the domain, which remains in lower case.
+However, although independent matches on the domain alone are still performed
+caselessly, regular expressions that match against an entire address become
+case-sensitive after "+caseful" has been seen.
+
+
+10.21 Local part lists
+----------------------
+
+Case-sensitivity in local part lists is handled in the same way as for address
+lists, as just described. The "+caseful" item can be used if required. In a
+setting of the local_parts option in a router with caseful_local_part set
+false, the subject is lowercased and the matching is initially
+case-insensitive. In this case, "+caseful" will restore case-sensitive matching
+in the local part list, but not elsewhere in the router. If caseful_local_part
+is set true in a router, matching in the local_parts option is case-sensitive
+from the start.
+
+If a local part list is indirected to a file (see section 10.3), comments are
+handled in the same way as address lists - they are recognized only if the # is
+preceded by white space or the start of the line. Otherwise, local part lists
+are matched in the same way as domain lists, except that the special items that
+refer to the local host ("@", "@[]", "@mx_any", "@mx_primary", and
+"@mx_secondary") are not recognized. Refer to section 10.8 for details of the
+other available item types.
+
+
+
+===============================================================================
+11. STRING EXPANSIONS
+
+Many strings in Exim's run time configuration are expanded before use. Some of
+them are expanded every time they are used; others are expanded only once.
+
+When a string is being expanded it is copied verbatim from left to right except
+when a dollar or backslash character is encountered. A dollar specifies the
+start of a portion of the string that is interpreted and replaced as described
+below in section 11.5 onwards. Backslash is used as an escape character, as
+described in the following section.
+
+Whether a string is expanded depends upon the context. Usually this is solely
+dependent upon the option for which a value is sought; in this documentation,
+options for which string expansion is performed are marked with * after the
+data type. ACL rules always expand strings. A couple of expansion conditions do
+not expand some of the brace-delimited branches, for security reasons.
+
+
+11.1 Literal text in expanded strings
+-------------------------------------
+
+An uninterpreted dollar can be included in an expanded string by putting a
+backslash in front of it. A backslash can be used to prevent any special
+character being treated specially in an expansion, including backslash itself.
+If the string appears in quotes in the configuration file, two backslashes are
+required because the quotes themselves cause interpretation of backslashes when
+the string is read in (see section 6.16).
+
+A portion of the string can specified as non-expandable by placing it between
+two occurrences of "\N". This is particularly useful for protecting regular
+expressions, which often contain backslashes and dollar signs. For example:
+
+deny senders = \N^\d{8}[a-z]@some\.site\.example$\N
+
+On encountering the first "\N", the expander copies subsequent characters
+without interpretation until it reaches the next "\N" or the end of the string.
+
+
+11.2 Character escape sequences in expanded strings
+---------------------------------------------------
+
+A backslash followed by one of the letters "n", "r", or "t" in an expanded
+string is recognized as an escape sequence for the character newline, carriage
+return, or tab, respectively. A backslash followed by up to three octal digits
+is recognized as an octal encoding for a single character, and a backslash
+followed by "x" and up to two hexadecimal digits is a hexadecimal encoding.
+
+These escape sequences are also recognized in quoted strings when they are read
+in. Their interpretation in expansions as well is useful for unquoted strings,
+and for other cases such as looked-up strings that are then expanded.
+
+
+11.3 Testing string expansions
+------------------------------
+
+Many expansions can be tested by calling Exim with the -be option. This takes
+the command arguments, or lines from the standard input if there are no
+arguments, runs them through the string expansion code, and writes the results
+to the standard output. Variables based on configuration values are set up, but
+since no message is being processed, variables such as $local_part have no
+value. Nevertheless the -be option can be useful for checking out file and
+database lookups, and the use of expansion operators such as sg, substr and 
+nhash.
+
+Exim gives up its root privilege when it is called with the -be option, and
+instead runs under the uid and gid it was called with, to prevent users from
+using -be for reading files to which they do not have access.
+
+If you want to test expansions that include variables whose values are taken
+from a message, there are two other options that can be used. The -bem option
+is like -be except that it is followed by a file name. The file is read as a
+message before doing the test expansions. For example:
+
+exim -bem /tmp/test.message '$h_subject:'
+
+The -Mset option is used in conjunction with -be and is followed by an Exim
+message identifier. For example:
+
+exim -be -Mset 1GrA8W-0004WS-LQ '$recipients'
+
+This loads the message from Exim's spool before doing the test expansions, and
+is therefore restricted to admin users.
+
+
+11.4 Forced expansion failure
+-----------------------------
+
+A number of expansions that are described in the following section have
+alternative "true" and "false" substrings, enclosed in brace characters (which
+are sometimes called "curly brackets"). Which of the two strings is used
+depends on some condition that is evaluated as part of the expansion. If,
+instead of a "false" substring, the word "fail" is used (not in braces), the
+entire string expansion fails in a way that can be detected by the code that
+requested the expansion. This is called "forced expansion failure", and its
+consequences depend on the circumstances. In some cases it is no different from
+any other expansion failure, but in others a different action may be taken.
+Such variations are mentioned in the documentation of the option that is being
+expanded.
+
+
+11.5 Expansion items
+--------------------
+
+The following items are recognized in expanded strings. White space may be used
+between sub-items that are keywords or substrings enclosed in braces inside an
+outer set of braces, to improve readability. Warning: Within braces, white
+space is significant.
+
+$<variable name> or ${<variable name>}
+
+    Substitute the contents of the named variable, for example:
+
+    $local_part
+    ${domain}
+
+    The second form can be used to separate the name from subsequent
+    alphanumeric characters. This form (using braces) is available only for
+    variables; it does not apply to message headers. The names of the variables
+    are given in section 11.9 below. If the name of a non-existent variable is
+    given, the expansion fails.
+
+${<op>:<string>}
+
+    The string is first itself expanded, and then the operation specified by <
+    op> is applied to it. For example:
+
+    ${lc:$local_part}
+
+    The string starts with the first character after the colon, which may be
+    leading white space. A list of operators is given in section 11.6 below.
+    The operator notation is used for simple expansion items that have just one
+    argument, because it reduces the number of braces and therefore makes the
+    string easier to understand.
+
+$bheader_<header name>: or $bh_<header name>:
+
+    This item inserts "basic" header lines. It is described with the header
+    expansion item below.
+
+${acl{<name>}{<arg>}...}
+
+    The name and zero to nine argument strings are first expanded separately.
+    The expanded arguments are assigned to the variables $acl_arg1 to $acl_arg9
+    in order. Any unused are made empty. The variable $acl_narg is set to the
+    number of arguments. The named ACL (see chapter 42) is called and may use
+    the variables; if another acl expansion is used the values are restored
+    after it returns. If the ACL sets a value using a "message =" modifier and
+    returns accept or deny, the value becomes the result of the expansion. If
+    no message is set and the ACL returns accept or deny the expansion result
+    is an empty string. If the ACL returns defer the result is a forced-fail.
+    Otherwise the expansion fails.
+
+${certextract{<field>}{<certificate>}{<string2>}{<string3>}}
+
+    The <certificate> must be a variable of type certificate. The field name is
+    expanded and used to retrive the relevant field from the certificate.
+    Supported fields are:
+
+    version        
+    serial_number  
+    subject         RFC4514 DN
+    issuer          RFC4514 DN
+    notbefore       time
+    notafter        time
+    sig_algorithm  
+    signature      
+    subj_altname    tagged list
+    ocsp_uri        list
+    crl_uri         list
+
+    If the field is found, <string2> is expanded, and replaces the whole item;
+    otherwise <string3> is used. During the expansion of <string2> the variable
+    $value contains the value that has been extracted. Afterwards, it is
+    restored to any previous value it might have had.
+
+    If {<string3>} is omitted, the item is replaced by an empty string if the
+    key is not found. If {<string2>} is also omitted, the value that was
+    extracted is used.
+
+    Some field names take optional modifiers, appended and separated by commas.
+
+    The field selectors marked as "RFC4514" above output a Distinguished Name
+    string which is not quite parseable by Exim as a comma-separated tagged
+    list (the exceptions being elements containin commas). RDN elements of a
+    single type may be selected by a modifier of the type label; if so the
+    expansion result is a list (newline-separated by default). The separator
+    may be changed by another modifer of a right angle-bracket followed
+    immediately by the new separator. Recognised RDN type labels include "CN",
+    "O", "OU" and "DC".
+
+    The field selectors marked as "time" above may output a number of seconds
+    since epoch if the modifier "int" is used.
+
+    The field selectors marked as "list" above return a list, newline-separated
+    by default, (embedded separator characters in elements are doubled). The
+    separator may be changed by a modifier of a right angle-bracket followed
+    immediately by the new separator.
+
+    The field selectors marked as "tagged" above prefix each list element with
+    a type string and an equals sign. Elements of only one type may be selected
+    by a modifier which is one of "dns", "uri" or "mail"; if so the elenment
+    tags are omitted.
+
+    If not otherwise noted field values are presented in human-readable form.
+
+${dlfunc{<file>}{<function>}{<arg>}{<arg>}...}
+
+    This expansion dynamically loads and then calls a locally-written C
+    function. This functionality is available only if Exim is compiled with
+
+    EXPAND_DLFUNC=yes
+
+    set in Local/Makefile. Once loaded, Exim remembers the dynamically loaded
+    object so that it doesn't reload the same object file in the same Exim
+    process (but of course Exim does start new processes frequently).
+
+    There may be from zero to eight arguments to the function. When compiling a
+    local function that is to be called in this way, local_scan.h should be
+    included. The Exim variables and functions that are defined by that API are
+    also available for dynamically loaded functions. The function itself must
+    have the following type:
+
+    int dlfunction(uschar **yield, int argc, uschar *argv[])
+
+    Where "uschar" is a typedef for "unsigned char" in local_scan.h. The
+    function should return one of the following values:
+
+    "OK": Success. The string that is placed in the variable yield is put into
+    the expanded string that is being built.
+
+    "FAIL": A non-forced expansion failure occurs, with the error message taken
+    from yield, if it is set.
+
+    "FAIL_FORCED": A forced expansion failure occurs, with the error message
+    taken from yield if it is set.
+
+    "ERROR": Same as "FAIL", except that a panic log entry is written.
+
+    When compiling a function that is to be used in this way with gcc, you need
+    to add -shared to the gcc command. Also, in the Exim build-time
+    configuration, you must add -export-dynamic to EXTRALIBS.
+
+${extract{<key>}{<string1>}{<string2>}{<string3>}}
+
+    The key and <string1> are first expanded separately. Leading and trailing
+    white space is removed from the key (but not from any of the strings). The
+    key must not consist entirely of digits. The expanded <string1> must be of
+    the form:
+
+    <key1> = <value1>  <key2> = <value2> ...
+
+    where the equals signs and spaces (but not both) are optional. If any of
+    the values contain white space, they must be enclosed in double quotes, and
+    any values that are enclosed in double quotes are subject to escape
+    processing as described in section 6.16. The expanded <string1> is searched
+    for the value that corresponds to the key. The search is case-insensitive.
+    If the key is found, <string2> is expanded, and replaces the whole item;
+    otherwise <string3> is used. During the expansion of <string2> the variable
+    $value contains the value that has been extracted. Afterwards, it is
+    restored to any previous value it might have had.
+
+    If {<string3>} is omitted, the item is replaced by an empty string if the
+    key is not found. If {<string2>} is also omitted, the value that was
+    extracted is used. Thus, for example, these two expansions are identical,
+    and yield "2001":
+
+    ${extract{gid}{uid=1984 gid=2001}}
+    ${extract{gid}{uid=1984 gid=2001}{$value}}
+
+    Instead of {<string3>} the word "fail" (not in curly brackets) can appear,
+    for example:
+
+    ${extract{Z}{A=... B=...}{$value} fail }
+
+    This forces an expansion failure (see section 11.4); {<string2>} must be
+    present for "fail" to be recognized.
+
+${extract{<number>}{<separators>}{<string1>}{<string2>}{<string3>}}
+
+    The <number> argument must consist entirely of decimal digits, apart from
+    leading and trailing white space, which is ignored. This is what
+    distinguishes this form of extract from the previous kind. It behaves in
+    the same way, except that, instead of extracting a named field, it extracts
+    from <string1> the field whose number is given as the first argument. You
+    can use $value in <string2> or "fail" instead of <string3> as before.
+
+    The fields in the string are separated by any one of the characters in the
+    separator string. These may include space or tab characters. The first
+    field is numbered one. If the number is negative, the fields are counted
+    from the end of the string, with the rightmost one numbered -1. If the
+    number given is zero, the entire string is returned. If the modulus of the
+    number is greater than the number of fields in the string, the result is
+    the expansion of <string3>, or the empty string if <string3> is not
+    provided. For example:
+
+    ${extract{2}{:}{x:42:99:& Mailer::/bin/bash}}
+
+    yields "42", and
+
+    ${extract{-4}{:}{x:42:99:& Mailer::/bin/bash}}
+
+    yields "99". Two successive separators mean that the field between them is
+    empty (for example, the fifth field above).
+
+${filter{<string>}{<condition>}}
+
+    After expansion, <string> is interpreted as a list, colon-separated by
+    default, but the separator can be changed in the usual way. For each item
+    in this list, its value is place in $item, and then the condition is
+    evaluated. If the condition is true, $item is added to the output as an
+    item in a new list; if the condition is false, the item is discarded. The
+    separator used for the output list is the same as the one used for the
+    input, but a separator setting is not included in the output. For example:
+
+    ${filter{a:b:c}{!eq{$item}{b}}
+
+    yields "a:c". At the end of the expansion, the value of $item is restored
+    to what it was before. See also the map and reduce expansion items.
+
+${hash{<string1>}{<string2>}{<string3>}}
+
+    This is a textual hashing function, and was the first to be implemented in
+    early versions of Exim. In current releases, there are other hashing
+    functions (numeric, MD5, and SHA-1), which are described below.
+
+    The first two strings, after expansion, must be numbers. Call them <m> and
+    <n>. If you are using fixed values for these numbers, that is, if <string1>
+    and <string2> do not change when they are expanded, you can use the simpler
+    operator notation that avoids some of the braces:
+
+    ${hash_<n>_<m>:<string>}
+
+    The second number is optional (in both notations). If <n> is greater than
+    or equal to the length of the string, the expansion item returns the
+    string. Otherwise it computes a new string of length <n> by applying a
+    hashing function to the string. The new string consists of characters taken
+    from the first <m> characters of the string
+
+    abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQWRSTUVWXYZ0123456789
+
+    If <m> is not present the value 26 is used, so that only lower case letters
+    appear. For example:
+
+    $hash{3}{monty}}              yields  jmg
+    $hash{5}{monty}}              yields  monty
+    $hash{4}{62}{monty python}}   yields  fbWx
+
+$header_<header name>: or $h_<header name>:, $bheader_<header name>: or $bh_<
+    header name>:, $rheader_<header name>: or $rh_<header name>:
+
+    Substitute the contents of the named message header line, for example
+
+    $header_reply-to:
+
+    The newline that terminates a header line is not included in the expansion,
+    but internal newlines (caused by splitting the header line over several
+    physical lines) may be present.
+
+    The difference between rheader, bheader, and header is in the way the data
+    in the header line is interpreted.
+
+      * rheader gives the original "raw" content of the header line, with no
+        processing at all, and without the removal of leading and trailing
+        white space.
+
+      * bheader removes leading and trailing white space, and then decodes
+        base64 or quoted-printable MIME "words" within the header text, but
+        does no character set translation. If decoding of what looks
+        superficially like a MIME "word" fails, the raw string is returned. If
+        decoding produces a binary zero character, it is replaced by a question
+        mark - this is what Exim does for binary zeros that are actually
+        received in header lines.
+
+      * header tries to translate the string as decoded by bheader to a
+        standard character set. This is an attempt to produce the same string
+        as would be displayed on a user's MUA. If translation fails, the 
+        bheader string is returned. Translation is attempted only on operating
+        systems that support the iconv() function. This is indicated by the
+        compile-time macro HAVE_ICONV in a system Makefile or in Local/Makefile
+        .
+
+    In a filter file, the target character set for header can be specified by a
+    command of the following form:
+
+    headers charset "UTF-8"
+
+    This command affects all references to $h_ (or $header_) expansions in
+    subsequently obeyed filter commands. In the absence of this command, the
+    target character set in a filter is taken from the setting of the 
+    headers_charset option in the runtime configuration. The value of this
+    option defaults to the value of HEADERS_CHARSET in Local/Makefile. The
+    ultimate default is ISO-8859-1.
+
+    Header names follow the syntax of RFC 2822, which states that they may
+    contain any printing characters except space and colon. Consequently, curly
+    brackets do not terminate header names, and should not be used to enclose
+    them as if they were variables. Attempting to do so causes a syntax error.
+
+    Only header lines that are common to all copies of a message are visible to
+    this mechanism. These are the original header lines that are received with
+    the message, and any that are added by an ACL statement or by a system
+    filter. Header lines that are added to a particular copy of a message by a
+    router or transport are not accessible.
+
+    For incoming SMTP messages, no header lines are visible in ACLs that are
+    obeyed before the DATA ACL, because the header structure is not set up
+    until the message is received. Header lines that are added in a RCPT ACL
+    (for example) are saved until the message's incoming header lines are
+    available, at which point they are added. When a DATA ACL is running,
+    however, header lines added by earlier ACLs are visible.
+
+    Upper case and lower case letters are synonymous in header names. If the
+    following character is white space, the terminating colon may be omitted,
+    but this is not recommended, because you may then forget it when it is
+    needed. When white space terminates the header name, it is included in the
+    expanded string. If the message does not contain the given header, the
+    expansion item is replaced by an empty string. (See the def condition in
+    section 11.7 for a means of testing for the existence of a header.)
+
+    If there is more than one header with the same name, they are all
+    concatenated to form the substitution string, up to a maximum length of
+    64K. Unless rheader is being used, leading and trailing white space is
+    removed from each header before concatenation, and a completely empty
+    header is ignored. A newline character is then inserted between non-empty
+    headers, but there is no newline at the very end. For the header and 
+    bheader expansion, for those headers that contain lists of addresses, a
+    comma is also inserted at the junctions between headers. This does not
+    happen for the rheader expansion.
+
+${hmac{<hashname>}{<secret>}{<string>}}
+
+    This function uses cryptographic hashing (either MD5 or SHA-1) to convert a
+    shared secret and some text into a message authentication code, as
+    specified in RFC 2104. This differs from "${md5:secret_text...}" or "$
+    {sha1:secret_text...}" in that the hmac step adds a signature to the
+    cryptographic hash, allowing for authentication that is not possible with
+    MD5 or SHA-1 alone. The hash name must expand to either "md5" or "sha1" at
+    present. For example:
+
+    ${hmac{md5}{somesecret}{$primary_hostname $tod_log}}
+
+    For the hostname mail.example.com and time 2002-10-17 11:30:59, this
+    produces:
+
+    dd97e3ba5d1a61b5006108f8c8252953
+
+    As an example of how this might be used, you might put in the main part of
+    an Exim configuration:
+
+    SPAMSCAN_SECRET=cohgheeLei2thahw
+
+    In a router or a transport you could then have:
+
+    headers_add = \
+      X-Spam-Scanned: ${primary_hostname} ${message_exim_id} \
+      ${hmac{md5}{SPAMSCAN_SECRET}\
+      {${primary_hostname},${message_exim_id},$h_message-id:}}
+
+    Then given a message, you can check where it was scanned by looking at the 
+    X-Spam-Scanned: header line. If you know the secret, you can check that
+    this header line is authentic by recomputing the authentication code from
+    the host name, message ID and the Message-id: header line. This can be done
+    using Exim's -be option, or by other means, for example by using the 
+    hmac_md5_hex() function in Perl.
+
+${if <condition> {<string1>}{<string2>}}
+
+    If <condition> is true, <string1> is expanded and replaces the whole item;
+    otherwise <string2> is used. The available conditions are described in
+    section 11.7 below. For example:
+
+    ${if eq {$local_part}{postmaster} {yes}{no} }
+
+    The second string need not be present; if it is not and the condition is
+    not true, the item is replaced with nothing. Alternatively, the word "fail"
+    may be present instead of the second string (without any curly brackets).
+    In this case, the expansion is forced to fail if the condition is not true
+    (see section 11.4).
+
+    If both strings are omitted, the result is the string "true" if the
+    condition is true, and the empty string if the condition is false. This
+    makes it less cumbersome to write custom ACL and router conditions. For
+    example, instead of
+
+    condition = ${if >{$acl_m4}{3}{true}{false}}
+
+    you can use
+
+    condition = ${if >{$acl_m4}{3}}
+
+${length{<string1>}{<string2>}}
+
+    The length item is used to extract the initial portion of a string. Both
+    strings are expanded, and the first one must yield a number, <n>, say. If
+    you are using a fixed value for the number, that is, if <string1> does not
+    change when expanded, you can use the simpler operator notation that avoids
+    some of the braces:
+
+    ${length_<n>:<string>}
+
+    The result of this item is either the first <n> characters or the whole of
+    <string2>, whichever is the shorter. Do not confuse length with strlen,
+    which gives the length of a string.
+
+${listextract{<number>}{<string1>}{<string2>}{<string3>}}
+
+    The <number> argument must consist entirely of decimal digits, apart from
+    an optional leading minus, and leading and trailing white space (which is
+    ignored).
+
+    After expansion, <string1> is interpreted as a list, colon-separated by
+    default, but the separator can be changed in the usual way.
+
+    The first field of the list is numbered one. If the number is negative, the
+    fields are counted from the end of the list, with the rightmost one
+    numbered -1. The numbered element of the list is extracted and placed in
+    $value, then <string2> is expanded as the result.
+
+    If the modulus of the number is zero or greater than the number of fields
+    in the string, the result is the expansion of <string3>.
+
+    For example:
+
+    ${listextract{2}{x:42:99}}
+
+    yields "42", and
+
+    ${listextract{-3}{<, x,42,99,& Mailer,,/bin/bash}{result: $value}}
+
+    yields "result: 99".
+
+    If {<string3>} is omitted, an empty string is used for string3. If {<
+    string2>} is also omitted, the value that was extracted is used. You can
+    use "fail" instead of {<string3>} as in a string extract.
+
+${lookup{<key>} <search type> {<file>} {<string1>} {<string2>}}
+
+    This is the first of one of two different types of lookup item, which are
+    both described in the next item.
+
+${lookup <search type> {<query>} {<string1>} {<string2>}}
+
+    The two forms of lookup item specify data lookups in files and databases,
+    as discussed in chapter 9. The first form is used for single-key lookups,
+    and the second is used for query-style lookups. The <key>, <file>, and <
+    query> strings are expanded before use.
+
+    If there is any white space in a lookup item which is part of a filter
+    command, a retry or rewrite rule, a routing rule for the manualroute
+    router, or any other place where white space is significant, the lookup
+    item must be enclosed in double quotes. The use of data lookups in users'
+    filter files may be locked out by the system administrator.
+
+    If the lookup succeeds, <string1> is expanded and replaces the entire item.
+    During its expansion, the variable $value contains the data returned by the
+    lookup. Afterwards it reverts to the value it had previously (at the outer
+    level it is empty). If the lookup fails, <string2> is expanded and replaces
+    the entire item. If {<string2>} is omitted, the replacement is the empty
+    string on failure. If <string2> is provided, it can itself be a nested
+    lookup, thus providing a mechanism for looking up a default value when the
+    original lookup fails.
+
+    If a nested lookup is used as part of <string1>, $value contains the data
+    for the outer lookup while the parameters of the second lookup are
+    expanded, and also while <string2> of the second lookup is expanded, should
+    the second lookup fail. Instead of {<string2>} the word "fail" can appear,
+    and in this case, if the lookup fails, the entire expansion is forced to
+    fail (see section 11.4). If both {<string1>} and {<string2>} are omitted,
+    the result is the looked up value in the case of a successful lookup, and
+    nothing in the case of failure.
+
+    For single-key lookups, the string "partial" is permitted to precede the
+    search type in order to do partial matching, and * or *@ may follow a
+    search type to request default lookups if the key does not match (see
+    sections 9.6 and 9.7 for details).
+
+    If a partial search is used, the variables $1 and $2 contain the wild and
+    non-wild parts of the key during the expansion of the replacement text.
+    They return to their previous values at the end of the lookup item.
+
+    This example looks up the postmaster alias in the conventional alias file:
+
+    ${lookup {postmaster} lsearch {/etc/aliases} {$value}}
+
+    This example uses NIS+ to look up the full name of the user corresponding
+    to the local part of an address, forcing the expansion to fail if it is not
+    found:
+
+    ${lookup nisplus {[name=$local_part],passwd.org_dir:gcos} \
+      {$value}fail}
+
+${map{<string1>}{<string2>}}
+
+    After expansion, <string1> is interpreted as a list, colon-separated by
+    default, but the separator can be changed in the usual way. For each item
+    in this list, its value is place in $item, and then <string2> is expanded
+    and added to the output as an item in a new list. The separator used for
+    the output list is the same as the one used for the input, but a separator
+    setting is not included in the output. For example:
+
+    ${map{a:b:c}{[$item]}} ${map{<- x-y-z}{($item)}}
+
+    expands to "[a]:[b]:[c] (x)-(y)-(z)". At the end of the expansion, the
+    value of $item is restored to what it was before. See also the filter and 
+    reduce expansion items.
+
+${nhash{<string1>}{<string2>}{<string3>}}
+
+    The three strings are expanded; the first two must yield numbers. Call them
+    <n> and <m>. If you are using fixed values for these numbers, that is, if <
+    string1> and <string2> do not change when they are expanded, you can use
+    the simpler operator notation that avoids some of the braces:
+
+    ${nhash_<n>_<m>:<string>}
+
+    The second number is optional (in both notations). If there is only one
+    number, the result is a number in the range 0-<n>-1. Otherwise, the string
+    is processed by a div/mod hash function that returns two numbers, separated
+    by a slash, in the ranges 0 to <n>-1 and 0 to <m>-1, respectively. For
+    example,
+
+    ${nhash{8}{64}{supercalifragilisticexpialidocious}}
+
+    returns the string "6/33".
+
+${perl{<subroutine>}{<arg>}{<arg>}...}
+
+    This item is available only if Exim has been built to include an embedded
+    Perl interpreter. The subroutine name and the arguments are first
+    separately expanded, and then the Perl subroutine is called with those
+    arguments. No additional arguments need be given; the maximum number
+    permitted, including the name of the subroutine, is nine.
+
+    The return value of the subroutine is inserted into the expanded string,
+    unless the return value is undef. In that case, the expansion fails in the
+    same way as an explicit "fail" on a lookup item. The return value is a
+    scalar. Whatever you return is evaluated in a scalar context. For example,
+    if you return the name of a Perl vector, the return value is the size of
+    the vector, not its contents.
+
+    If the subroutine exits by calling Perl's die function, the expansion fails
+    with the error message that was passed to die. More details of the embedded
+    Perl facility are given in chapter 12.
+
+    The redirect router has an option called forbid_filter_perl which locks out
+    the use of this expansion item in filter files.
+
+${prvs{<address>}{<secret>}{<keynumber>}}
+
+    The first argument is a complete email address and the second is secret
+    keystring. The third argument, specifying a key number, is optional. If
+    absent, it defaults to 0. The result of the expansion is a prvs-signed
+    email address, to be typically used with the return_path option on an smtp
+    transport as part of a bounce address tag validation (BATV) scheme. For
+    more discussion and an example, see section 42.51.
+
+${prvscheck{<address>}{<secret>}{<string>}}
+
+    This expansion item is the complement of the prvs item. It is used for
+    checking prvs-signed addresses. If the expansion of the first argument does
+    not yield a syntactically valid prvs-signed address, the whole item expands
+    to the empty string. When the first argument does expand to a syntactically
+    valid prvs-signed address, the second argument is expanded, with the
+    prvs-decoded version of the address and the key number extracted from the
+    address in the variables $prvscheck_address and $prvscheck_keynum,
+    respectively.
+
+    These two variables can be used in the expansion of the second argument to
+    retrieve the secret. The validity of the prvs-signed address is then
+    checked against the secret. The result is stored in the variable
+    $prvscheck_result, which is empty for failure or "1" for success.
+
+    The third argument is optional; if it is missing, it defaults to an empty
+    string. This argument is now expanded. If the result is an empty string,
+    the result of the expansion is the decoded version of the address. This is
+    the case whether or not the signature was valid. Otherwise, the result of
+    the expansion is the expansion of the third argument.
+
+    All three variables can be used in the expansion of the third argument.
+    However, once the expansion is complete, only $prvscheck_result remains
+    set. For more discussion and an example, see section 42.51.
+
+${readfile{<file name>}{<eol string>}}
+
+    The file name and end-of-line string are first expanded separately. The
+    file is then read, and its contents replace the entire item. All newline
+    characters in the file are replaced by the end-of-line string if it is
+    present. Otherwise, newlines are left in the string. String expansion is
+    not applied to the contents of the file. If you want this, you must wrap
+    the item in an expand operator. If the file cannot be read, the string
+    expansion fails.
+
+    The redirect router has an option called forbid_filter_readfile which locks
+    out the use of this expansion item in filter files.
+
+${readsocket{<name>}{<request>}{<timeout>}{<eol string>}{<fail string>}}
+
+    This item inserts data from a Unix domain or Internet socket into the
+    expanded string. The minimal way of using it uses just two arguments, as in
+    these examples:
+
+    ${readsocket{/socket/name}{request string}}
+    ${readsocket{inet:some.host:1234}{request string}}
+
+    For a Unix domain socket, the first substring must be the path to the
+    socket. For an Internet socket, the first substring must contain "inet:"
+    followed by a host name or IP address, followed by a colon and a port,
+    which can be a number or the name of a TCP port in /etc/services. An IP
+    address may optionally be enclosed in square brackets. This is best for
+    IPv6 addresses. For example:
+
+    ${readsocket{inet:[::1]:1234}{request string}}
+
+    Only a single host name may be given, but if looking it up yields more than
+    one IP address, they are each tried in turn until a connection is made. For
+    both kinds of socket, Exim makes a connection, writes the request string
+    (unless it is an empty string) and reads from the socket until an
+    end-of-file is read. A timeout of 5 seconds is applied. Additional,
+    optional arguments extend what can be done. Firstly, you can vary the
+    timeout. For example:
+
+    ${readsocket{/socket/name}{request string}{3s}}
+
+    A fourth argument allows you to change any newlines that are in the data
+    that is read, in the same way as for readfile (see above). This example
+    turns them into spaces:
+
+    ${readsocket{inet:127.0.0.1:3294}{request string}{3s}{ }}
+
+    As with all expansions, the substrings are expanded before the processing
+    happens. Errors in these sub-expansions cause the expansion to fail. In
+    addition, the following errors can occur:
+
+      * Failure to create a socket file descriptor;
+
+      * Failure to connect the socket;
+
+      * Failure to write the request string;
+
+      * Timeout on reading from the socket.
+
+    By default, any of these errors causes the expansion to fail. However, if
+    you supply a fifth substring, it is expanded and used when any of the above
+    errors occurs. For example:
+
+    ${readsocket{/socket/name}{request string}{3s}{\n}\
+      {socket failure}}
+
+    You can test for the existence of a Unix domain socket by wrapping this
+    expansion in "${if exists", but there is a race condition between that test
+    and the actual opening of the socket, so it is safer to use the fifth
+    argument if you want to be absolutely sure of avoiding an expansion error
+    for a non-existent Unix domain socket, or a failure to connect to an
+    Internet socket.
+
+    The redirect router has an option called forbid_filter_readsocket which
+    locks out the use of this expansion item in filter files.
+
+${reduce{<string1>}{<string2>}{<string3>}}
+
+    This operation reduces a list to a single, scalar string. After expansion,
+    <string1> is interpreted as a list, colon-separated by default, but the
+    separator can be changed in the usual way. Then <string2> is expanded and
+    assigned to the $value variable. After this, each item in the <string1>
+    list is assigned to $item in turn, and <string3> is expanded for each of
+    them. The result of that expansion is assigned to $value before the next
+    iteration. When the end of the list is reached, the final value of $value
+    is added to the expansion output. The reduce expansion item can be used in
+    a number of ways. For example, to add up a list of numbers:
+
+    ${reduce {<, 1,2,3}{0}{${eval:$value+$item}}}
+
+    The result of that expansion would be "6". The maximum of a list of numbers
+    can be found:
+
+    ${reduce {3:0:9:4:6}{0}{${if >{$item}{$value}{$item}{$value}}}}
+
+    At the end of a reduce expansion, the values of $item and $value are
+    restored to what they were before. See also the filter and map expansion
+    items.
+
+$rheader_<header name>: or $rh_<header name>:
+
+    This item inserts "raw" header lines. It is described with the header
+    expansion item above.
+
+${run{<command> <args>}{<string1>}{<string2>}}
+
+    The command and its arguments are first expanded as one string. The string
+    is split apart into individual arguments by spaces, and then the command is
+    run in a separate process, but under the same uid and gid. As in other
+    command executions from Exim, a shell is not used by default. If the
+    command requires a shell, you must explicitly code it.
+
+    Since the arguments are split by spaces, when there is a variable expansion
+    which has an empty result, it will cause the situation that the argument
+    will simply be omitted when the program is actually executed by Exim. If
+    the script/program requires a specific number of arguments and the expanded
+    variable could possibly result in this empty expansion, the variable must
+    be quoted. This is more difficult if the expanded variable itself could
+    result in a string containing quotes, because it would interfere with the
+    quotes around the command arguments. A possible guard against this is to
+    wrap the variable in the sg operator to change any quote marks to some
+    other character.
+
+    The standard input for the command exists, but is empty. The standard
+    output and standard error are set to the same file descriptor. If the
+    command succeeds (gives a zero return code) <string1> is expanded and
+    replaces the entire item; during this expansion, the standard output/error
+    from the command is in the variable $value. If the command fails, <string2
+    >, if present, is expanded and used. Once again, during the expansion, the
+    standard output/error from the command is in the variable $value.
+
+    If <string2> is absent, the result is empty. Alternatively, <string2> can
+    be the word "fail" (not in braces) to force expansion failure if the
+    command does not succeed. If both strings are omitted, the result is
+    contents of the standard output/error on success, and nothing on failure.
+
+    The standard output/error of the command is put in the variable $value. In
+    this ACL example, the output of a command is logged for the admin to
+    troubleshoot:
+
+    warn  condition    = ${run{/usr/bin/id}{yes}{no}}
+          log_message  = Output of id: $value
+
+    If the command requires shell idioms, such as the > redirect operator, the
+    shell must be invoked directly, such as with:
+
+    ${run{/bin/bash -c "/usr/bin/id >/tmp/id"}{yes}{yes}}
+
+    The return code from the command is put in the variable $runrc, and this
+    remains set afterwards, so in a filter file you can do things like this:
+
+    if "${run{x y z}{}}$runrc" is 1 then ...
+      elif $runrc is 2 then ...
+      ...
+    endif
+
+    If execution of the command fails (for example, the command does not
+    exist), the return code is 127 - the same code that shells use for
+    non-existent commands.
+
+    Warning: In a router or transport, you cannot assume the order in which
+    option values are expanded, except for those preconditions whose order of
+    testing is documented. Therefore, you cannot reliably expect to set $runrc
+    by the expansion of one option, and use it in another.
+
+    The redirect router has an option called forbid_filter_run which locks out
+    the use of this expansion item in filter files.
+
+${sg{<subject>}{<regex>}{<replacement>}}
+
+    This item works like Perl's substitution operator (s) with the global (/g)
+    option; hence its name. However, unlike the Perl equivalent, Exim does not
+    modify the subject string; instead it returns the modified string for
+    insertion into the overall expansion. The item takes three arguments: the
+    subject string, a regular expression, and a substitution string. For
+    example:
+
+    ${sg{abcdefabcdef}{abc}{xyz}}
+
+    yields "xyzdefxyzdef". Because all three arguments are expanded before use,
+    if any $ or \ characters are required in the regular expression or in the
+    substitution string, they have to be escaped. For example:
+
+    ${sg{abcdef}{^(...)(...)\$}{\$2\$1}}
+
+    yields "defabc", and
+
+    ${sg{1=A 4=D 3=C}{\N(\d+)=\N}{K\$1=}}
+
+    yields "K1=A K4=D K3=C". Note the use of "\N" to protect the contents of
+    the regular expression from string expansion.
+
+${substr{<string1>}{<string2>}{<string3>}}
+
+    The three strings are expanded; the first two must yield numbers. Call them
+    <n> and <m>. If you are using fixed values for these numbers, that is, if <
+    string1> and <string2> do not change when they are expanded, you can use
+    the simpler operator notation that avoids some of the braces:
+
+    ${substr_<n>_<m>:<string>}
+
+    The second number is optional (in both notations). If it is absent in the
+    simpler format, the preceding underscore must also be omitted.
+
+    The substr item can be used to extract more general substrings than length.
+    The first number, <n>, is a starting offset, and <m> is the length
+    required. For example
+
+    ${substr{3}{2}{$local_part}}
+
+    If the starting offset is greater than the string length the result is the
+    null string; if the length plus starting offset is greater than the string
+    length, the result is the right-hand part of the string, starting from the
+    given offset. The first character in the string has offset zero.
+
+    The substr expansion item can take negative offset values to count from the
+    right-hand end of its operand. The last character is offset -1, the
+    second-last is offset -2, and so on. Thus, for example,
+
+    ${substr{-5}{2}{1234567}}
+
+    yields "34". If the absolute value of a negative offset is greater than the
+    length of the string, the substring starts at the beginning of the string,
+    and the length is reduced by the amount of overshoot. Thus, for example,
+
+    ${substr{-5}{2}{12}}
+
+    yields an empty string, but
+
+    ${substr{-3}{2}{12}}
+
+    yields "1".
+
+    When the second number is omitted from substr, the remainder of the string
+    is taken if the offset is positive. If it is negative, all characters in
+    the string preceding the offset point are taken. For example, an offset of
+    -1 and no length, as in these semantically identical examples:
+
+    ${substr_-1:abcde}
+    ${substr{-1}{abcde}}
+
+    yields all but the last character of the string, that is, "abcd".
+
+${tr{<subject>}{<characters>}{<replacements>}}
+
+    This item does single-character translation on its subject string. The
+    second argument is a list of characters to be translated in the subject
+    string. Each matching character is replaced by the corresponding character
+    from the replacement list. For example
+
+    ${tr{abcdea}{ac}{13}}
+
+    yields "1b3de1". If there are duplicates in the second character string,
+    the last occurrence is used. If the third string is shorter than the
+    second, its last character is replicated. However, if it is empty, no
+    translation takes place.
+
+
+11.6 Expansion operators
+------------------------
+
+For expansion items that perform transformations on a single argument string,
+the "operator" notation is used because it is simpler and uses fewer braces.
+The substring is first expanded before the operation is applied to it. The
+following operations can be performed:
+
+${address:<string>}
+
+    The string is interpreted as an RFC 2822 address, as it might appear in a
+    header line, and the effective address is extracted from it. If the string
+    does not parse successfully, the result is empty.
+
+${addresses:<string>}
+
+    The string (after expansion) is interpreted as a list of addresses in RFC
+    2822 format, such as can be found in a To: or Cc: header line. The
+    operative address (local-part@domain) is extracted from each item, and the
+    result of the expansion is a colon-separated list, with appropriate
+    doubling of colons should any happen to be present in the email addresses.
+    Syntactically invalid RFC2822 address items are omitted from the output.
+
+    It is possible to specify a character other than colon for the output
+    separator by starting the string with > followed by the new separator
+    character. For example:
+
+    ${addresses:>& Chief <ceo@up.stairs>, sec@base.ment (dogsbody)}
+
+    expands to "ceo@up.stairs&sec@base.ment". Compare the address (singular)
+    expansion item, which extracts the working address from a single RFC2822
+    address. See the filter, map, and reduce items for ways of processing
+    lists.
+
+    To clarify "list of addresses in RFC 2822 format" mentioned above, Exim
+    follows a strict interpretation of header line formatting. Exim parses the
+    bare, unquoted portion of an email address and if it finds a comma, treats
+    it as an email address seperator. For the example header line:
+
+    From: =?iso-8859-2?Q?Last=2C_First?= <user@example.com>
+
+    The first example below demonstrates that Q-encoded email addresses are
+    parsed properly if it is given the raw header (in this example,
+    "$rheader_from:"). It does not see the comma because it's still encoded as
+    "=2C". The second example below is passed the contents of "$header_from:",
+    meaning it gets de-mimed. Exim sees the decoded "," so it treats it as two
+    email addresses. The third example shows that the presence of a comma is
+    skipped when it is quoted.
+
+    # exim -be '${addresses:From: \
+    =?iso-8859-2?Q?Last=2C_First?= <user@example.com>}'
+    user@example.com
+    # exim -be '${addresses:From: Last, First <user@example.com>}'
+    Last:user@example.com
+    # exim -be '${addresses:From: "Last, First" <user@example.com>}'
+    user@example.com
+
+${base62:<digits>}
+
+    The string must consist entirely of decimal digits. The number is converted
+    to base 62 and output as a string of six characters, including leading
+    zeros. In the few operating environments where Exim uses base 36 instead of
+    base 62 for its message identifiers (because those systems do not have
+    case-sensitive file names), base 36 is used by this operator, despite its
+    name. Note: Just to be absolutely clear: this is not base64 encoding.
+
+${base62d:<base-62 digits>}
+
+    The string must consist entirely of base-62 digits, or, in operating
+    environments where Exim uses base 36 instead of base 62 for its message
+    identifiers, base-36 digits. The number is converted to decimal and output
+    as a string.
+
+${domain:<string>}
+
+    The string is interpreted as an RFC 2822 address and the domain is
+    extracted from it. If the string does not parse successfully, the result is
+    empty.
+
+${escape:<string>}
+
+    If the string contains any non-printing characters, they are converted to
+    escape sequences starting with a backslash. Whether characters with the
+    most significant bit set (so-called "8-bit characters") count as printing
+    or not is controlled by the print_topbitchars option.
+
+${eval:<string>} and ${eval10:<string>}
+
+    These items supports simple arithmetic and bitwise logical operations in
+    expansion strings. The string (after expansion) must be a conventional
+    arithmetic expression, but it is limited to basic arithmetic operators,
+    bitwise logical operators, and parentheses. All operations are carried out
+    using integer arithmetic. The operator priorities are as follows (the same
+    as in the C programming language):
+
+        highest: not (~), negate (-)
+                 multiply (*), divide (/), remainder (%)
+                 plus (+), minus (-)
+                 shift-left (<<), shift-right (>>)
+                 and (&)
+                 xor (^)
+        lowest:  or (|)
+
+    Binary operators with the same priority are evaluated from left to right.
+    White space is permitted before or after operators.
+
+    For eval, numbers may be decimal, octal (starting with "0") or hexadecimal
+    (starting with "0x"). For eval10, all numbers are taken as decimal, even if
+    they start with a leading zero; hexadecimal numbers are not permitted. This
+    can be useful when processing numbers extracted from dates or times, which
+    often do have leading zeros.
+
+    A number may be followed by "K", "M" or "G" to multiply it by 1024,
+    1024*1024 or 1024*1024*1024, respectively. Negative numbers are supported.
+    The result of the computation is a decimal representation of the answer
+    (without "K", "M" or "G"). For example:
+
+    ${eval:1+1}              yields 2
+    ${eval:1+2*3}            yields 7
+    ${eval:(1+2)*3}          yields 9
+    ${eval:2+42%5}           yields 4
+    ${eval:0xc&5}            yields 4
+    ${eval:0xc|5}            yields 13
+    ${eval:0xc^5}            yields 9
+    ${eval:0xc>>1}           yields 6
+    ${eval:0xc<<1}           yields 24
+    ${eval:~255&0x1234}      yields 4608
+    ${eval:-(~255&0x1234)}   yields -4608
+
+    As a more realistic example, in an ACL you might have
+
+    deny   message = Too many bad recipients
+           condition =                    \
+             ${if and {                   \
+               {>{$rcpt_count}{10}}       \
+               {                          \
+               <                          \
+                 {$recipients_count}      \
+                 {${eval:$rcpt_count/2}}  \
+               }                          \
+             }{yes}{no}}
+
+    The condition is true if there have been more than 10 RCPT commands and
+    fewer than half of them have resulted in a valid recipient.
+
+${expand:<string>}
+
+    The expand operator causes a string to be expanded for a second time. For
+    example,
+
+    ${expand:${lookup{$domain}dbm{/some/file}{$value}}}
+
+    first looks up a string in a file while expanding the operand for expand,
+    and then re-expands what it has found.
+
+${from_utf8:<string>}
+
+    The world is slowly moving towards Unicode, although there are no standards
+    for email yet. However, other applications (including some databases) are
+    starting to store data in Unicode, using UTF-8 encoding. This operator
+    converts from a UTF-8 string to an ISO-8859-1 string. UTF-8 code values
+    greater than 255 are converted to underscores. The input must be a valid
+    UTF-8 string. If it is not, the result is an undefined sequence of bytes.
+
+    Unicode code points with values less than 256 are compatible with ASCII and
+    ISO-8859-1 (also known as Latin-1). For example, character 169 is the
+    copyright symbol in both cases, though the way it is encoded is different.
+    In UTF-8, more than one byte is needed for characters with code values
+    greater than 127, whereas ISO-8859-1 is a single-byte encoding (but thereby
+    limited to 256 characters). This makes translation from UTF-8 to ISO-8859-1
+    straightforward.
+
+${hash_<n>_<m>:<string>}
+
+    The hash operator is a simpler interface to the hashing function that can
+    be used when the two parameters are fixed numbers (as opposed to strings
+    that change when expanded). The effect is the same as
+
+    ${hash{<n>}{<m>}{<string>}}
+
+    See the description of the general hash item above for details. The
+    abbreviation h can be used when hash is used as an operator.
+
+${hex2b64:<hexstring>}
+
+    This operator converts a hex string into one that is base64 encoded. This
+    can be useful for processing the output of the MD5 and SHA-1 hashing
+    functions.
+
+${hexquote:<string>}
+
+    This operator converts non-printable characters in a string into a hex
+    escape form. Byte values between 33 (!) and 126 (~) inclusive are left as
+    is, and other byte values are converted to "\xNN", for example a byte value
+    127 is converted to "\x7f".
+
+${lc:<string>}
+
+    This forces the letters in the string into lower-case, for example:
+
+    ${lc:$local_part}
+
+${length_<number>:<string>}
+
+    The length operator is a simpler interface to the length function that can
+    be used when the parameter is a fixed number (as opposed to a string that
+    changes when expanded). The effect is the same as
+
+    ${length{<number>}{<string>}}
+
+    See the description of the general length item above for details. Note that
+    length is not the same as strlen. The abbreviation l can be used when 
+    length is used as an operator.
+
+${listcount:<string>}
+
+    The string is interpreted as a list and the number of items is returned.
+
+${listnamed:<name>} and ${listnamed_<type>:<name>}
+
+    The name is interpreted as a named list and the content of the list is
+    returned, expanding any referenced lists, re-quoting as needed for
+    colon-separation. If the optional type is given it must be one of "a", "d",
+    "h" or "l" and selects address-, domain-, host- or localpart- lists to
+    search among respectively. Otherwise all types are searched in an undefined
+    order and the first matching list is returned.
+
+${local_part:<string>}
+
+    The string is interpreted as an RFC 2822 address and the local part is
+    extracted from it. If the string does not parse successfully, the result is
+    empty.
+
+${mask:<IP address>/<bit count>}
+
+    If the form of the string to be operated on is not an IP address followed
+    by a slash and an integer (that is, a network address in CIDR notation),
+    the expansion fails. Otherwise, this operator converts the IP address to
+    binary, masks off the least significant bits according to the bit count,
+    and converts the result back to text, with mask appended. For example,
+
+    ${mask:10.111.131.206/28}
+
+    returns the string "10.111.131.192/28". Since this operation is expected to
+    be mostly used for looking up masked addresses in files, the result for an
+    IPv6 address uses dots to separate components instead of colons, because
+    colon terminates a key string in lsearch files. So, for example,
+
+    ${mask:3ffe:ffff:836f:0a00:000a:0800:200a:c031/99}
+
+    returns the string
+
+    3ffe.ffff.836f.0a00.000a.0800.2000.0000/99
+
+    Letters in IPv6 addresses are always output in lower case.
+
+${md5:<string>}
+
+    The md5 operator computes the MD5 hash value of the string, and returns it
+    as a 32-digit hexadecimal number, in which any letters are in lower case.
+
+${nhash_<n>_<m>:<string>}
+
+    The nhash operator is a simpler interface to the numeric hashing function
+    that can be used when the two parameters are fixed numbers (as opposed to
+    strings that change when expanded). The effect is the same as
+
+    ${nhash{<n>}{<m>}{<string>}}
+
+    See the description of the general nhash item above for details.
+
+${quote:<string>}
+
+    The quote operator puts its argument into double quotes if it is an empty
+    string or contains anything other than letters, digits, underscores, dots,
+    and hyphens. Any occurrences of double quotes and backslashes are escaped
+    with a backslash. Newlines and carriage returns are converted to "\n" and "
+    \r", respectively For example,
+
+    ${quote:ab"*"cd}
+
+    becomes
+
+    "ab\"*\"cd"
+
+    The place where this is useful is when the argument is a substitution from
+    a variable or a message header.
+
+${quote_local_part:<string>}
+
+    This operator is like quote, except that it quotes the string only if
+    required to do so by the rules of RFC 2822 for quoting local parts. For
+    example, a plus sign would not cause quoting (but it would for quote). If
+    you are creating a new email address from the contents of $local_part (or
+    any other unknown data), you should always use this operator.
+
+${quote_<lookup-type>:<string>}
+
+    This operator applies lookup-specific quoting rules to the string. Each
+    query-style lookup type has its own quoting rules which are described with
+    the lookups in chapter 9. For example,
+
+    ${quote_ldap:two * two}
+
+    returns
+
+    two%20%5C2A%20two
+
+    For single-key lookup types, no quoting is ever necessary and this operator
+    yields an unchanged string.
+
+${randint:<n>}
+
+    This operator returns a somewhat random number which is less than the
+    supplied number and is at least 0. The quality of this randomness depends
+    on how Exim was built; the values are not suitable for keying material. If
+    Exim is linked against OpenSSL then RAND_pseudo_bytes() is used. If Exim is
+    linked against GnuTLS then gnutls_rnd(GNUTLS_RND_NONCE) is used, for
+    versions of GnuTLS with that function. Otherwise, the implementation may be
+    arc4random(), random() seeded by srandomdev() or srandom(), or a custom
+    implementation even weaker than random().
+
+${reverse_ip:<ipaddr>}
+
+    This operator reverses an IP address; for IPv4 addresses, the result is in
+    dotted-quad decimal form, while for IPv6 addreses the result is in
+    dotted-nibble hexadecimal form. In both cases, this is the "natural" form
+    for DNS. For example,
+
+    ${reverse_ip:192.0.2.4}
+    ${reverse_ip:2001:0db8:c42:9:1:abcd:192.0.2.127}
+
+    returns
+
+    4.2.0.192
+    f.7.2.0.0.0.0.c.d.c.b.a.1.0.0.0.9.0.0.0.2.4.c.0.8.b.d.0.1.0.0.2
+
+${rfc2047:<string>}
+
+    This operator encodes text according to the rules of RFC 2047. This is an
+    encoding that is used in header lines to encode non-ASCII characters. It is
+    assumed that the input string is in the encoding specified by the 
+    headers_charset option, which defaults to ISO-8859-1. If the string
+    contains only characters in the range 33-126, and no instances of the
+    characters
+
+    ? = ( ) < > @ , ; : \ " . [ ] _
+
+    it is not modified. Otherwise, the result is the RFC 2047 encoding of the
+    string, using as many "encoded words" as necessary to encode all the
+    characters.
+
+${rfc2047d:<string>}
+
+    This operator decodes strings that are encoded as per RFC 2047. Binary zero
+    bytes are replaced by question marks. Characters are converted into the
+    character set defined by headers_charset. Overlong RFC 2047 "words" are not
+    recognized unless check_rfc2047_length is set false.
+
+    Note: If you use $header_xxx: (or $h_xxx:) to access a header line, RFC
+    2047 decoding is done automatically. You do not need to use this operator
+    as well.
+
+${rxquote:<string>}
+
+    The rxquote operator inserts a backslash before any non-alphanumeric
+    characters in its argument. This is useful when substituting the values of
+    variables or headers inside regular expressions.
+
+${sha1:<string>}
+
+    The sha1 operator computes the SHA-1 hash value of the string, and returns
+    it as a 40-digit hexadecimal number, in which any letters are in upper
+    case.
+
+${sha256:<certificate>}
+
+    The sha256 operator computes the SHA-256 hash fingerprint of the
+    certificate, and returns it as a 64-digit hexadecimal number, in which any
+    letters are in upper case. Only arguments which are a single variable of
+    certificate type are supported.
+
+${stat:<string>}
+
+    The string, after expansion, must be a file path. A call to the stat()
+    function is made for this path. If stat() fails, an error occurs and the
+    expansion fails. If it succeeds, the data from the stat replaces the item,
+    as a series of <name>=<value> pairs, where the values are all numerical,
+    except for the value of "smode". The names are: "mode" (giving the mode as
+    a 4-digit octal number), "smode" (giving the mode in symbolic format as a
+    10-character string, as for the ls command), "inode", "device", "links",
+    "uid", "gid", "size", "atime", "mtime", and "ctime". You can extract
+    individual fields using the extract expansion item.
+
+    The use of the stat expansion in users' filter files can be locked out by
+    the system administrator. Warning: The file size may be incorrect on 32-bit
+    systems for files larger than 2GB.
+
+${str2b64:<string>}
+
+    This operator converts a string into one that is base64 encoded.
+
+${strlen:<string>}
+
+    The item is replace by the length of the expanded string, expressed as a
+    decimal number. Note: Do not confuse strlen with length.
+
+${substr_<start>_<length>:<string>}
+
+    The substr operator is a simpler interface to the substr function that can
+    be used when the two parameters are fixed numbers (as opposed to strings
+    that change when expanded). The effect is the same as
+
+    ${substr{<start>}{<length>}{<string>}}
+
+    See the description of the general substr item above for details. The
+    abbreviation s can be used when substr is used as an operator.
+
+${time_eval:<string>}
+
+    This item converts an Exim time interval such as "2d4h5m" into a number of
+    seconds.
+
+${time_interval:<string>}
+
+    The argument (after sub-expansion) must be a sequence of decimal digits
+    that represents an interval of time as a number of seconds. It is converted
+    into a number of larger units and output in Exim's normal time format, for
+    example, "1w3d4h2m6s".
+
+${uc:<string>}
+
+    This forces the letters in the string into upper-case.
+
+${utf8clean:<string>}
+
+    This replaces any invalid utf-8 sequence in the string by the character "?
+    ".
+
+
+11.7 Expansion conditions
+-------------------------
+
+The following conditions are available for testing by the ${if construct while
+expanding strings:
+
+!<condition>
+
+    Preceding any condition with an exclamation mark negates the result of the
+    condition.
+
+<symbolic operator> {<string1>}{<string2>}
+
+    There are a number of symbolic operators for doing numeric comparisons.
+    They are:
+
+    =      equal
+    ==     equal
+    >      greater
+    >=     greater or equal
+    <      less
+    <=     less or equal
+
+    For example:
+
+    ${if >{$message_size}{10M} ...
+
+    Note that the general negation operator provides for inequality testing.
+    The two strings must take the form of optionally signed decimal integers,
+    optionally followed by one of the letters "K", "M" or "G" (in either upper
+    or lower case), signifying multiplication by 1024, 1024*1024 or
+    1024*1024*1024, respectively. As a special case, the numerical value of an
+    empty string is taken as zero.
+
+    In all cases, a relative comparator OP is testing if <string1> OP <string2
+    >; the above example is checking if $message_size is larger than 10M, not
+    if 10M is larger than $message_size.
+
+acl {{<name>}{<arg1>}{<arg2>}...}
+
+    The name and zero to nine argument strings are first expanded separately.
+    The expanded arguments are assigned to the variables $acl_arg1 to $acl_arg9
+    in order. Any unused are made empty. The variable $acl_narg is set to the
+    number of arguments. The named ACL (see chapter 42) is called and may use
+    the variables; if another acl expansion is used the values are restored
+    after it returns. If the ACL sets a value using a "message =" modifier the
+    variable $value becomes the result of the expansion, otherwise it is empty.
+    If the ACL returns accept the condition is true; if deny, false. If the ACL
+    returns defer the result is a forced-fail.
+
+bool {<string>}
+
+    This condition turns a string holding a true or false representation into a
+    boolean state. It parses "true", "false", "yes" and "no"
+    (case-insensitively); also integer numbers map to true if non-zero, false
+    if zero. An empty string is treated as false. Leading and trailing
+    whitespace is ignored; thus a string consisting only of whitespace is
+    false. All other string values will result in expansion failure.
+
+    When combined with ACL variables, this expansion condition will let you
+    make decisions in one place and act on those decisions in another place.
+    For example:
+
+    ${if bool{$acl_m_privileged_sender} ...
+
+bool_lax {<string>}
+
+    Like bool, this condition turns a string into a boolean state. But where 
+    bool accepts a strict set of strings, bool_lax uses the same loose
+    definition that the Router condition option uses. The empty string and the
+    values "false", "no" and "0" map to false, all others map to true. Leading
+    and trailing whitespace is ignored.
+
+    Note that where "bool{00}" is false, "bool_lax{00}" is true.
+
+crypteq {<string1>}{<string2>}
+
+    This condition is included in the Exim binary if it is built to support any
+    authentication mechanisms (see chapter 33). Otherwise, it is necessary to
+    define SUPPORT_CRYPTEQ in Local/Makefile to get crypteq included in the
+    binary.
+
+    The crypteq condition has two arguments. The first is encrypted and
+    compared against the second, which is already encrypted. The second string
+    may be in the LDAP form for storing encrypted strings, which starts with
+    the encryption type in curly brackets, followed by the data. If the second
+    string does not begin with "{" it is assumed to be encrypted with crypt()
+    or crypt16() (see below), since such strings cannot begin with "{".
+    Typically this will be a field from a password file. An example of an
+    encrypted string in LDAP form is:
+
+    {md5}CY9rzUYh03PK3k6DJie09g==
+
+    If such a string appears directly in an expansion, the curly brackets have
+    to be quoted, because they are part of the expansion syntax. For example:
+
+    ${if crypteq {test}{\{md5\}CY9rzUYh03PK3k6DJie09g==}{yes}{no}}
+
+    The following encryption types (whose names are matched case-independently)
+    are supported:
+
+      * {md5} computes the MD5 digest of the first string, and expresses this
+        as printable characters to compare with the remainder of the second
+        string. If the length of the comparison string is 24, Exim assumes that
+        it is base64 encoded (as in the above example). If the length is 32,
+        Exim assumes that it is a hexadecimal encoding of the MD5 digest. If
+        the length not 24 or 32, the comparison fails.
+
+      * {sha1} computes the SHA-1 digest of the first string, and expresses
+        this as printable characters to compare with the remainder of the
+        second string. If the length of the comparison string is 28, Exim
+        assumes that it is base64 encoded. If the length is 40, Exim assumes
+        that it is a hexadecimal encoding of the SHA-1 digest. If the length is
+        not 28 or 40, the comparison fails.
+
+      * {crypt} calls the crypt() function, which traditionally used to use
+        only the first eight characters of the password. However, in modern
+        operating systems this is no longer true, and in many cases the entire
+        password is used, whatever its length.
+
+      * {crypt16} calls the crypt16() function, which was originally created to
+        use up to 16 characters of the password in some operating systems.
+        Again, in modern operating systems, more characters may be used.
+
+    Exim has its own version of crypt16(), which is just a double call to crypt
+    (). For operating systems that have their own version, setting HAVE_CRYPT16
+    in Local/Makefile when building Exim causes it to use the operating system
+    version instead of its own. This option is set by default in the
+    OS-dependent Makefile for those operating systems that are known to support
+    crypt16().
+
+    Some years after Exim's crypt16() was implemented, a user discovered that
+    it was not using the same algorithm as some operating systems' versions. It
+    turns out that as well as crypt16() there is a function called bigcrypt()
+    in some operating systems. This may or may not use the same algorithm, and
+    both of them may be different to Exim's built-in crypt16().
+
+    However, since there is now a move away from the traditional crypt()
+    functions towards using SHA1 and other algorithms, tidying up this area of
+    Exim is seen as very low priority.
+
+    If you do not put a encryption type (in curly brackets) in a crypteq
+    comparison, the default is usually either "{crypt}" or "{crypt16}", as
+    determined by the setting of DEFAULT_CRYPT in Local/Makefile. The default
+    default is "{crypt}". Whatever the default, you can always use either
+    function by specifying it explicitly in curly brackets.
+
+def:<variable name>
+
+    The def condition must be followed by the name of one of the expansion
+    variables defined in section 11.9. The condition is true if the variable
+    does not contain the empty string. For example:
+
+    ${if def:sender_ident {from $sender_ident}}
+
+    Note that the variable name is given without a leading $ character. If the
+    variable does not exist, the expansion fails.
+
+def:header_<header name>:  or  def:h_<header name>:
+
+    This condition is true if a message is being processed and the named header
+    exists in the message. For example,
+
+    ${if def:header_reply-to:{$h_reply-to:}{$h_from:}}
+
+    Note: No $ appears before header_ or h_ in the condition, and the header
+    name must be terminated by a colon if white space does not follow.
+
+eq {<string1>}{<string2>}, eqi {<string1>}{<string2>}
+
+    The two substrings are first expanded. The condition is true if the two
+    resulting strings are identical. For eq the comparison includes the case of
+    letters, whereas for eqi the comparison is case-independent.
+
+exists {<file name>}
+
+    The substring is first expanded and then interpreted as an absolute path.
+    The condition is true if the named file (or directory) exists. The
+    existence test is done by calling the stat() function. The use of the 
+    exists test in users' filter files may be locked out by the system
+    administrator.
+
+first_delivery
+
+    This condition, which has no data, is true during a message's first
+    delivery attempt. It is false during any subsequent delivery attempts.
+
+forall{<a list>}{<a condition>}, forany{<a list>}{<a condition>}
+
+    These conditions iterate over a list. The first argument is expanded to
+    form the list. By default, the list separator is a colon, but it can be
+    changed by the normal method. The second argument is interpreted as a
+    condition that is to be applied to each item in the list in turn. During
+    the interpretation of the condition, the current list item is placed in a
+    variable called $item.
+
+      * For forany, interpretation stops if the condition is true for any item,
+        and the result of the whole condition is true. If the condition is
+        false for all items in the list, the overall condition is false.
+
+      * For forall, interpretation stops if the condition is false for any
+        item, and the result of the whole condition is false. If the condition
+        is true for all items in the list, the overall condition is true.
+
+    Note that negation of forany means that the condition must be false for all
+    items for the overall condition to succeed, and negation of forall means
+    that the condition must be false for at least one item. In this example,
+    the list separator is changed to a comma:
+
+    ${if forany{<, $recipients}{match{$item}{^user3@}}{yes}{no}}
+
+    The value of $item is saved and restored while forany or forall is being
+    processed, to enable these expansion items to be nested.
+
+    To scan a named list, expand it with the listnamed operator.
+
+ge {<string1>}{<string2>}, gei {<string1>}{<string2>}
+
+    The two substrings are first expanded. The condition is true if the first
+    string is lexically greater than or equal to the second string. For ge the
+    comparison includes the case of letters, whereas for gei the comparison is
+    case-independent.
+
+gt {<string1>}{<string2>}, gti {<string1>}{<string2>}
+
+    The two substrings are first expanded. The condition is true if the first
+    string is lexically greater than the second string. For gt the comparison
+    includes the case of letters, whereas for gti the comparison is
+    case-independent.
+
+inlist {<string1>}{<string2>}, inlisti {<string1>}{<string2>}
+
+    Both strings are expanded; the second string is treated as a list of simple
+    strings; if the first string is a member of the second, then the condition
+    is true.
+
+    These are simpler to use versions of the more powerful forany condition.
+    Examples, and the forany equivalents:
+
+    ${if inlist{needle}{foo:needle:bar}}
+      ${if forany{foo:needle:bar}{eq{$item}{needle}}}
+    ${if inlisti{Needle}{fOo:NeeDLE:bAr}}
+      ${if forany{fOo:NeeDLE:bAr}{eqi{$item}{Needle}}}
+
+isip {<string>}, isip4 {<string>}, isip6 {<string>}
+
+    The substring is first expanded, and then tested to see if it has the form
+    of an IP address. Both IPv4 and IPv6 addresses are valid for isip, whereas 
+    isip4 and isip6 test specifically for IPv4 or IPv6 addresses.
+
+    For an IPv4 address, the test is for four dot-separated components, each of
+    which consists of from one to three digits. For an IPv6 address, up to
+    eight colon-separated components are permitted, each containing from one to
+    four hexadecimal digits. There may be fewer than eight components if an
+    empty component (adjacent colons) is present. Only one empty component is
+    permitted.
+
+    Note: The checks are just on the form of the address; actual numerical
+    values are not considered. Thus, for example, 999.999.999.999 passes the
+    IPv4 check. The main use of these tests is to distinguish between IP
+    addresses and host names, or between IPv4 and IPv6 addresses. For example,
+    you could use
+
+    ${if isip4{$sender_host_address}...
+
+    to test which IP version an incoming SMTP connection is using.
+
+ldapauth {<ldap query>}
+
+    This condition supports user authentication using LDAP. See section 9.13
+    for details of how to use LDAP in lookups and the syntax of queries. For
+    this use, the query must contain a user name and password. The query itself
+    is not used, and can be empty. The condition is true if the password is not
+    empty, and the user name and password are accepted by the LDAP server. An
+    empty password is rejected without calling LDAP because LDAP binds with an
+    empty password are considered anonymous regardless of the username, and
+    will succeed in most configurations. See chapter 33 for details of SMTP
+    authentication, and chapter 34 for an example of how this can be used.
+
+le {<string1>}{<string2>}, lei {<string1>}{<string2>}
+
+    The two substrings are first expanded. The condition is true if the first
+    string is lexically less than or equal to the second string. For le the
+    comparison includes the case of letters, whereas for lei the comparison is
+    case-independent.
+
+lt {<string1>}{<string2>}, lti {<string1>}{<string2>}
+
+    The two substrings are first expanded. The condition is true if the first
+    string is lexically less than the second string. For lt the comparison
+    includes the case of letters, whereas for lti the comparison is
+    case-independent.
+
+match {<string1>}{<string2>}
+
+    The two substrings are first expanded. The second is then treated as a
+    regular expression and applied to the first. Because of the pre-expansion,
+    if the regular expression contains dollar, or backslash characters, they
+    must be escaped. Care must also be taken if the regular expression contains
+    braces (curly brackets). A closing brace must be escaped so that it is not
+    taken as a premature termination of <string2>. The easiest approach is to
+    use the "\N" feature to disable expansion of the regular expression. For
+    example,
+
+    ${if match {$local_part}{\N^\d{3}\N} ...
+
+    If the whole expansion string is in double quotes, further escaping of
+    backslashes is also required.
+
+    The condition is true if the regular expression match succeeds. The regular
+    expression is not required to begin with a circumflex metacharacter, but if
+    there is no circumflex, the expression is not anchored, and it may match
+    anywhere in the subject, not just at the start. If you want the pattern to
+    match at the end of the subject, you must include the "$" metacharacter at
+    an appropriate point.
+
+    At the start of an if expansion the values of the numeric variable
+    substitutions $1 etc. are remembered. Obeying a match condition that
+    succeeds causes them to be reset to the substrings of that condition and
+    they will have these values during the expansion of the success string. At
+    the end of the if expansion, the previous values are restored. After
+    testing a combination of conditions using or, the subsequent values of the
+    numeric variables are those of the condition that succeeded.
+
+match_address {<string1>}{<string2>}
+
+    See match_local_part.
+
+match_domain {<string1>}{<string2>}
+
+    See match_local_part.
+
+match_ip {<string1>}{<string2>}
+
+    This condition matches an IP address to a list of IP address patterns. It
+    must be followed by two argument strings. The first (after expansion) must
+    be an IP address or an empty string. The second (not expanded) is a
+    restricted host list that can match only an IP address, not a host name.
+    For example:
+
+    ${if match_ip{$sender_host_address}{1.2.3.4:5.6.7.8}{...}{...}}
+
+    The specific types of host list item that are permitted in the list are:
+
+      * An IP address, optionally with a CIDR mask.
+
+      * A single asterisk, which matches any IP address.
+
+      * An empty item, which matches only if the IP address is empty. This
+        could be useful for testing for a locally submitted message or one from
+        specific hosts in a single test such as
+
+          ${if match_ip{$sender_host_address}{:4.3.2.1:...}{...}{...}}
+
+        where the first item in the list is the empty string.
+
+      * The item @[] matches any of the local host's interface addresses.
+
+      * Single-key lookups are assumed to be like "net-" style lookups in host
+        lists, even if "net-" is not specified. There is never any attempt to
+        turn the IP address into a host name. The most common type of linear
+        search for match_ip is likely to be iplsearch, in which the file can
+        contain CIDR masks. For example:
+
+          ${if match_ip{$sender_host_address}{iplsearch;/some/file}...
+
+        It is of course possible to use other kinds of lookup, and in such a
+        case, you do need to specify the "net-" prefix if you want to specify a
+        specific address mask, for example:
+
+          ${if match_ip{$sender_host_address}{net24-dbm;/some/file}...
+
+        However, unless you are combining a match_ip condition with others, it
+        is just as easy to use the fact that a lookup is itself a condition,
+        and write:
+
+          ${lookup{${mask:$sender_host_address/24}}dbm{/a/file}...
+
+    Note that <string2> is not itself subject to string expansion, unless Exim
+    was built with the EXPAND_LISTMATCH_RHS option.
+
+    Consult section 10.11 for further details of these patterns.
+
+match_local_part {<string1>}{<string2>}
+
+    This condition, together with match_address and match_domain, make it
+    possible to test domain, address, and local part lists within expansions.
+    Each condition requires two arguments: an item and a list to match. A
+    trivial example is:
+
+    ${if match_domain{a.b.c}{x.y.z:a.b.c:p.q.r}{yes}{no}}
+
+    In each case, the second argument may contain any of the allowable items
+    for a list of the appropriate type. Also, because the second argument
+    (after expansion) is a standard form of list, it is possible to refer to a
+    named list. Thus, you can use conditions like this:
+
+    ${if match_domain{$domain}{+local_domains}{...
+
+    For address lists, the matching starts off caselessly, but the "+caseful"
+    item can be used, as in all address lists, to cause subsequent items to
+    have their local parts matched casefully. Domains are always matched
+    caselessly.
+
+    Note that <string2> is not itself subject to string expansion, unless Exim
+    was built with the EXPAND_LISTMATCH_RHS option.
+
+    Note: Host lists are not supported in this way. This is because hosts have
+    two identities: a name and an IP address, and it is not clear how to
+    specify cleanly how such a test would work. However, IP addresses can be
+    matched using match_ip.
+
+pam {<string1>:<string2>:...}
+
+    Pluggable Authentication Modules (http://www.kernel.org/pub/linux/libs/pam/
+    ) are a facility that is available in the latest releases of Solaris and in
+    some GNU/Linux distributions. The Exim support, which is intended for use
+    in conjunction with the SMTP AUTH command, is available only if Exim is
+    compiled with
+
+    SUPPORT_PAM=yes
+
+    in Local/Makefile. You probably need to add -lpam to EXTRALIBS, and in some
+    releases of GNU/Linux -ldl is also needed.
+
+    The argument string is first expanded, and the result must be a
+    colon-separated list of strings. Leading and trailing white space is
+    ignored. The PAM module is initialized with the service name "exim" and the
+    user name taken from the first item in the colon-separated data string (<
+    string1>). The remaining items in the data string are passed over in
+    response to requests from the authentication function. In the simple case
+    there will only be one request, for a password, so the data consists of
+    just two strings.
+
+    There can be problems if any of the strings are permitted to contain colon
+    characters. In the usual way, these have to be doubled to avoid being taken
+    as separators. If the data is being inserted from a variable, the sg
+    expansion item can be used to double any existing colons. For example, the
+    configuration of a LOGIN authenticator might contain this setting:
+
+    server_condition = ${if pam{$auth1:${sg{$auth2}{:}{::}}}}
+
+    For a PLAIN authenticator you could use:
+
+    server_condition = ${if pam{$auth2:${sg{$auth3}{:}{::}}}}
+
+    In some operating systems, PAM authentication can be done only from a
+    process running as root. Since Exim is running as the Exim user when
+    receiving messages, this means that PAM cannot be used directly in those
+    systems. A patched version of the pam_unix module that comes with the Linux
+    PAM package is available from http://www.e-admin.de/pam_exim/. The patched
+    module allows one special uid/gid combination, in addition to root, to
+    authenticate. If you build the patched module to allow the Exim user and
+    group, PAM can then be used from an Exim authenticator.
+
+pwcheck {<string1>:<string2>}
+
+    This condition supports user authentication using the Cyrus pwcheck daemon.
+    This is one way of making it possible for passwords to be checked by a
+    process that is not running as root. Note: The use of pwcheck is now
+    deprecated. Its replacement is saslauthd (see below).
+
+    The pwcheck support is not included in Exim by default. You need to specify
+    the location of the pwcheck daemon's socket in Local/Makefile before
+    building Exim. For example:
+
+    CYRUS_PWCHECK_SOCKET=/var/pwcheck/pwcheck
+
+    You do not need to install the full Cyrus software suite in order to use
+    the pwcheck daemon. You can compile and install just the daemon alone from
+    the Cyrus SASL library. Ensure that exim is the only user that has access
+    to the /var/pwcheck directory.
+
+    The pwcheck condition takes one argument, which must be the user name and
+    password, separated by a colon. For example, in a LOGIN authenticator
+    configuration, you might have this:
+
+    server_condition = ${if pwcheck{$auth1:$auth2}}
+
+    Again, for a PLAIN authenticator configuration, this would be:
+
+    server_condition = ${if pwcheck{$auth2:$auth3}}
+
+queue_running
+
+    This condition, which has no data, is true during delivery attempts that
+    are initiated by queue runner processes, and false otherwise.
+
+radius {<authentication string>}
+
+    Radius authentication (RFC 2865) is supported in a similar way to PAM. You
+    must set RADIUS_CONFIG_FILE in Local/Makefile to specify the location of
+    the Radius client configuration file in order to build Exim with Radius
+    support.
+
+    With just that one setting, Exim expects to be linked with the radiusclient
+    library, using the original API. If you are using release 0.4.0 or later of
+    this library, you need to set
+
+    RADIUS_LIB_TYPE=RADIUSCLIENTNEW
+
+    in Local/Makefile when building Exim. You can also link Exim with the 
+    libradius library that comes with FreeBSD. To do this, set
+
+    RADIUS_LIB_TYPE=RADLIB
+
+    in Local/Makefile, in addition to setting RADIUS_CONFIGURE_FILE. You may
+    also have to supply a suitable setting in EXTRALIBS so that the Radius
+    library can be found when Exim is linked.
+
+    The string specified by RADIUS_CONFIG_FILE is expanded and passed to the
+    Radius client library, which calls the Radius server. The condition is true
+    if the authentication is successful. For example:
+
+    server_condition = ${if radius{<arguments>}}
+
+saslauthd {{<user>}{<password>}{<service>}{<realm>}}
+
+    This condition supports user authentication using the Cyrus saslauthd
+    daemon. This replaces the older pwcheck daemon, which is now deprecated.
+    Using this daemon is one way of making it possible for passwords to be
+    checked by a process that is not running as root.
+
+    The saslauthd support is not included in Exim by default. You need to
+    specify the location of the saslauthd daemon's socket in Local/Makefile
+    before building Exim. For example:
+
+    CYRUS_SASLAUTHD_SOCKET=/var/state/saslauthd/mux
+
+    You do not need to install the full Cyrus software suite in order to use
+    the saslauthd daemon. You can compile and install just the daemon alone
+    from the Cyrus SASL library.
+
+    Up to four arguments can be supplied to the saslauthd condition, but only
+    two are mandatory. For example:
+
+    server_condition = ${if saslauthd{{$auth1}{$auth2}}}
+
+    The service and the realm are optional (which is why the arguments are
+    enclosed in their own set of braces). For details of the meaning of the
+    service and realm, and how to run the daemon, consult the Cyrus
+    documentation.
+
+
+11.8 Combining expansion conditions
+-----------------------------------
+
+Several conditions can be tested at once by combining them using the and and or
+combination conditions. Note that and and or are complete conditions on their
+own, and precede their lists of sub-conditions. Each sub-condition must be
+enclosed in braces within the overall braces that contain the list. No
+repetition of if is used.
+
+or {{<cond1>}{<cond2>}...}
+
+    The sub-conditions are evaluated from left to right. The condition is true
+    if any one of the sub-conditions is true. For example,
+
+    ${if or {{eq{$local_part}{spqr}}{eq{$domain}{testing.com}}}...
+
+    When a true sub-condition is found, the following ones are parsed but not
+    evaluated. If there are several "match" sub-conditions the values of the
+    numeric variables afterwards are taken from the first one that succeeds.
+
+and {{<cond1>}{<cond2>}...}
+
+    The sub-conditions are evaluated from left to right. The condition is true
+    if all of the sub-conditions are true. If there are several "match"
+    sub-conditions, the values of the numeric variables afterwards are taken
+    from the last one. When a false sub-condition is found, the following ones
+    are parsed but not evaluated.
+
+
+11.9 Expansion variables
+------------------------
+
+This section contains an alphabetical list of all the expansion variables. Some
+of them are available only when Exim is compiled with specific options such as
+support for TLS or the content scanning extension.
+
+$0, $1, etc
+
+    When a match expansion condition succeeds, these variables contain the
+    captured substrings identified by the regular expression during subsequent
+    processing of the success string of the containing if expansion item.
+    However, they do not retain their values afterwards; in fact, their
+    previous values are restored at the end of processing an if item. The
+    numerical variables may also be set externally by some other matching
+    process which precedes the expansion of the string. For example, the
+    commands available in Exim filter files include an if command with its own
+    regular expression matching condition.
+
+$acl_arg1, $acl_arg2, etc
+
+    Within an acl condition, expansion condition or expansion item any
+    arguments are copied to these variables, any unused variables being made
+    empty.
+
+$acl_c...
+
+    Values can be placed in these variables by the set modifier in an ACL. They
+    can be given any name that starts with $acl_c and is at least six
+    characters long, but the sixth character must be either a digit or an
+    underscore. For example: $acl_c5, $acl_c_mycount. The values of the
+    $acl_c... variables persist throughout the lifetime of an SMTP connection.
+    They can be used to pass information between ACLs and between different
+    invocations of the same ACL. When a message is received, the values of
+    these variables are saved with the message, and can be accessed by filters,
+    routers, and transports during subsequent delivery.
+
+$acl_m...
+
+    These variables are like the $acl_c... variables, except that their values
+    are reset after a message has been received. Thus, if several messages are
+    received in one SMTP connection, $acl_m... values are not passed on from
+    one message to the next, as $acl_c... values are. The $acl_m... variables
+    are also reset by MAIL, RSET, EHLO, HELO, and after starting a TLS session.
+    When a message is received, the values of these variables are saved with
+    the message, and can be accessed by filters, routers, and transports during
+    subsequent delivery.
+
+$acl_narg
+
+    Within an acl condition, expansion condition or expansion item this
+    variable has the number of arguments.
+
+$acl_verify_message
+
+    After an address verification has failed, this variable contains the
+    failure message. It retains its value for use in subsequent modifiers. The
+    message can be preserved by coding like this:
+
+    warn !verify = sender
+         set acl_m0 = $acl_verify_message
+
+    You can use $acl_verify_message during the expansion of the message or 
+    log_message modifiers, to include information about the verification
+    failure.
+
+$address_data
+
+    This variable is set by means of the address_data option in routers. The
+    value then remains with the address while it is processed by subsequent
+    routers and eventually a transport. If the transport is handling multiple
+    addresses, the value from the first address is used. See chapter 15 for
+    more details. Note: The contents of $address_data are visible in user
+    filter files.
+
+    If $address_data is set when the routers are called from an ACL to verify a
+    recipient address, the final value is still in the variable for subsequent
+    conditions and modifiers of the ACL statement. If routing the address
+    caused it to be redirected to just one address, the child address is also
+    routed as part of the verification, and in this case the final value of
+    $address_data is from the child's routing.
+
+    If $address_data is set when the routers are called from an ACL to verify a
+    sender address, the final value is also preserved, but this time in
+    $sender_address_data, to distinguish it from data from a recipient address.
+
+    In both cases (recipient and sender verification), the value does not
+    persist after the end of the current ACL statement. If you want to preserve
+    these values for longer, you can save them in ACL variables.
+
+$address_file
+
+    When, as a result of aliasing, forwarding, or filtering, a message is
+    directed to a specific file, this variable holds the name of the file when
+    the transport is running. At other times, the variable is empty. For
+    example, using the default configuration, if user r2d2 has a .forward file
+    containing
+
+    /home/r2d2/savemail
+
+    then when the address_file transport is running, $address_file contains the
+    text string "/home/r2d2/savemail". For Sieve filters, the value may be
+    "inbox" or a relative folder name. It is then up to the transport
+    configuration to generate an appropriate absolute path to the relevant
+    file.
+
+$address_pipe
+
+    When, as a result of aliasing or forwarding, a message is directed to a
+    pipe, this variable holds the pipe command when the transport is running.
+
+$auth1 - $auth3
+
+    These variables are used in SMTP authenticators (see chapters 34-40).
+    Elsewhere, they are empty.
+
+$authenticated_id
+
+    When a server successfully authenticates a client it may be configured to
+    preserve some of the authentication information in the variable
+    $authenticated_id (see chapter 33). For example, a user/password
+    authenticator configuration might preserve the user name for use in the
+    routers. Note that this is not the same information that is saved in
+    $sender_host_authenticated. When a message is submitted locally (that is,
+    not over a TCP connection) the value of $authenticated_id is normally the
+    login name of the calling process. However, a trusted user can override
+    this by means of the -oMai command line option.
+
+$authenticated_fail_id
+
+    When an authentication attempt fails, the variable $authenticated_fail_id
+    will contain the failed authentication id. If more than one authentication
+    id is attempted, it will contain only the last one. The variable is
+    available for processing in the ACL's, generally the quit or notquit ACL. A
+    message to a local recipient could still be accepted without requiring
+    authentication, which means this variable could also be visible in all of
+    the ACL's as well.
+
+$authenticated_sender
+
+    When acting as a server, Exim takes note of the AUTH= parameter on an
+    incoming SMTP MAIL command if it believes the sender is sufficiently
+    trusted, as described in section 33.2. Unless the data is the string "<>",
+    it is set as the authenticated sender of the message, and the value is
+    available during delivery in the $authenticated_sender variable. If the
+    sender is not trusted, Exim accepts the syntax of AUTH=, but ignores the
+    data.
+
+    When a message is submitted locally (that is, not over a TCP connection),
+    the value of $authenticated_sender is an address constructed from the login
+    name of the calling process and $qualify_domain, except that a trusted user
+    can override this by means of the -oMas command line option.
+
+$authentication_failed
+
+    This variable is set to "1" in an Exim server if a client issues an AUTH
+    command that does not succeed. Otherwise it is set to "0". This makes it
+    possible to distinguish between "did not try to authenticate" (
+    $sender_host_authenticated is empty and $authentication_failed is set to
+    "0") and "tried to authenticate but failed" ($sender_host_authenticated is
+    empty and $authentication_failed is set to "1"). Failure includes any
+    negative response to an AUTH command, including (for example) an attempt to
+    use an undefined mechanism.
+
+$av_failed
+
+    This variable is available when Exim is compiled with the content-scanning
+    extension. It is set to "0" by default, but will be set to "1" if any
+    problem occurs with the virus scanner (specified by av_scanner) during the
+    ACL malware condition.
+
+$body_linecount
+
+    When a message is being received or delivered, this variable contains the
+    number of lines in the message's body. See also $message_linecount.
+
+$body_zerocount
+
+    When a message is being received or delivered, this variable contains the
+    number of binary zero bytes (ASCII NULs) in the message's body.
+
+$bounce_recipient
+
+    This is set to the recipient address of a bounce message while Exim is
+    creating it. It is useful if a customized bounce message text file is in
+    use (see chapter 48).
+
+$bounce_return_size_limit
+
+    This contains the value set in the bounce_return_size_limit option, rounded
+    up to a multiple of 1000. It is useful when a customized error message text
+    file is in use (see chapter 48).
+
+$caller_gid
+
+    The real group id under which the process that called Exim was running.
+    This is not the same as the group id of the originator of a message (see
+    $originator_gid). If Exim re-execs itself, this variable in the new
+    incarnation normally contains the Exim gid.
+
+$caller_uid
+
+    The real user id under which the process that called Exim was running. This
+    is not the same as the user id of the originator of a message (see
+    $originator_uid). If Exim re-execs itself, this variable in the new
+    incarnation normally contains the Exim uid.
+
+$compile_date
+
+    The date on which the Exim binary was compiled.
+
+$compile_number
+
+    The building process for Exim keeps a count of the number of times it has
+    been compiled. This serves to distinguish different compilations of the
+    same version of the program.
+
+$demime_errorlevel
+
+    This variable is available when Exim is compiled with the content-scanning
+    extension and the obsolete demime condition. For details, see section 43.6.
+
+$demime_reason
+
+    This variable is available when Exim is compiled with the content-scanning
+    extension and the obsolete demime condition. For details, see section 43.6.
+
+$dnslist_domain, $dnslist_matched, $dnslist_text, $dnslist_value
+
+    When a DNS (black) list lookup succeeds, these variables are set to contain
+    the following data from the lookup: the list's domain name, the key that
+    was looked up, the contents of any associated TXT record, and the value
+    from the main A record. See section 42.32 for more details.
+
+$domain
+
+    When an address is being routed, or delivered on its own, this variable
+    contains the domain. Uppercase letters in the domain are converted into
+    lower case for $domain.
+
+    Global address rewriting happens when a message is received, so the value
+    of $domain during routing and delivery is the value after rewriting.
+    $domain is set during user filtering, but not during system filtering,
+    because a message may have many recipients and the system filter is called
+    just once.
+
+    When more than one address is being delivered at once (for example, several
+    RCPT commands in one SMTP delivery), $domain is set only if they all have
+    the same domain. Transports can be restricted to handling only one domain
+    at a time if the value of $domain is required at transport time - this is
+    the default for local transports. For further details of the environment in
+    which local transports are run, see chapter 23.
+
+    At the end of a delivery, if all deferred addresses have the same domain,
+    it is set in $domain during the expansion of delay_warning_condition.
+
+    The $domain variable is also used in some other circumstances:
+
+      * When an ACL is running for a RCPT command, $domain contains the domain
+        of the recipient address. The domain of the sender address is in
+        $sender_address_domain at both MAIL time and at RCPT time. $domain is
+        not normally set during the running of the MAIL ACL. However, if the
+        sender address is verified with a callout during the MAIL ACL, the
+        sender domain is placed in $domain during the expansions of hosts, 
+        interface, and port in the smtp transport.
+
+      * When a rewrite item is being processed (see chapter 31), $domain
+        contains the domain portion of the address that is being rewritten; it
+        can be used in the expansion of the replacement address, for example,
+        to rewrite domains by file lookup.
+
+      * With one important exception, whenever a domain list is being scanned,
+        $domain contains the subject domain. Exception: When a domain list in a
+        sender_domains condition in an ACL is being processed, the subject
+        domain is in $sender_address_domain and not in $domain. It works this
+        way so that, in a RCPT ACL, the sender domain list can be dependent on
+        the recipient domain (which is what is in $domain at this time).
+
+      * When the smtp_etrn_command option is being expanded, $domain contains
+        the complete argument of the ETRN command (see section 47.8).
+
+$domain_data
+
+    When the domains option on a router matches a domain by means of a lookup,
+    the data read by the lookup is available during the running of the router
+    as $domain_data. In addition, if the driver routes the address to a
+    transport, the value is available in that transport. If the transport is
+    handling multiple addresses, the value from the first address is used.
+
+    $domain_data is also set when the domains condition in an ACL matches a
+    domain by means of a lookup. The data read by the lookup is available
+    during the rest of the ACL statement. In all other situations, this
+    variable expands to nothing.
+
+$exim_gid
+
+    This variable contains the numerical value of the Exim group id.
+
+$exim_path
+
+    This variable contains the path to the Exim binary.
+
+$exim_uid
+
+    This variable contains the numerical value of the Exim user id.
+
+$found_extension
+
+    This variable is available when Exim is compiled with the content-scanning
+    extension and the obsolete demime condition. For details, see section 43.6.
+
+$header_<name>
+
+    This is not strictly an expansion variable. It is expansion syntax for
+    inserting the message header line with the given name. Note that the name
+    must be terminated by colon or white space, because it may contain a wide
+    variety of characters. Note also that braces must not be used.
+
+$headers_added
+
+    Within an ACL this variable contains the headers added so far by the ACL
+    modifier add_header (section 42.24). The headers are a newline-separated
+    list.
+
+$home
+
+    When the check_local_user option is set for a router, the user's home
+    directory is placed in $home when the check succeeds. In particular, this
+    means it is set during the running of users' filter files. A router may
+    also explicitly set a home directory for use by a transport; this can be
+    overridden by a setting on the transport itself.
+
+    When running a filter test via the -bf option, $home is set to the value of
+    the environment variable HOME.
+
+$host
+
+    If a router assigns an address to a transport (any transport), and passes a
+    list of hosts with the address, the value of $host when the transport
+    starts to run is the name of the first host on the list. Note that this
+    applies both to local and remote transports.
+
+    For the smtp transport, if there is more than one host, the value of $host
+    changes as the transport works its way through the list. In particular,
+    when the smtp transport is expanding its options for encryption using TLS,
+    or for specifying a transport filter (see chapter 24), $host contains the
+    name of the host to which it is connected.
+
+    When used in the client part of an authenticator configuration (see chapter
+    33), $host contains the name of the server to which the client is
+    connected.
+
+$host_address
+
+    This variable is set to the remote host's IP address whenever $host is set
+    for a remote connection. It is also set to the IP address that is being
+    checked when the ignore_target_hosts option is being processed.
+
+$host_data
+
+    If a hosts condition in an ACL is satisfied by means of a lookup, the
+    result of the lookup is made available in the $host_data variable. This
+    allows you, for example, to do things like this:
+
+    deny  hosts = net-lsearch;/some/file
+    message = $host_data
+
+$host_lookup_deferred
+
+    This variable normally contains "0", as does $host_lookup_failed. When a
+    message comes from a remote host and there is an attempt to look up the
+    host's name from its IP address, and the attempt is not successful, one of
+    these variables is set to "1".
+
+      * If the lookup receives a definite negative response (for example, a DNS
+        lookup succeeded, but no records were found), $host_lookup_failed is
+        set to "1".
+
+      * If there is any kind of problem during the lookup, such that Exim
+        cannot tell whether or not the host name is defined (for example, a
+        timeout for a DNS lookup), $host_lookup_deferred is set to "1".
+
+    Looking up a host's name from its IP address consists of more than just a
+    single reverse lookup. Exim checks that a forward lookup of at least one of
+    the names it receives from a reverse lookup yields the original IP address.
+    If this is not the case, Exim does not accept the looked up name(s), and
+    $host_lookup_failed is set to "1". Thus, being able to find a name from an
+    IP address (for example, the existence of a PTR record in the DNS) is not
+    sufficient on its own for the success of a host name lookup. If the reverse
+    lookup succeeds, but there is a lookup problem such as a timeout when
+    checking the result, the name is not accepted, and $host_lookup_deferred is
+    set to "1". See also $sender_host_name.
+
+$host_lookup_failed
+
+    See $host_lookup_deferred.
+
+$inode
+
+    The only time this variable is set is while expanding the directory_file
+    option in the appendfile transport. The variable contains the inode number
+    of the temporary file which is about to be renamed. It can be used to
+    construct a unique name for the file.
+
+$interface_address
+
+    This is an obsolete name for $received_ip_address.
+
+$interface_port
+
+    This is an obsolete name for $received_port.
+
+$item
+
+    This variable is used during the expansion of forall and forany conditions
+    (see section 11.7), and filter, map, and reduce items (see section 11.7).
+    In other circumstances, it is empty.
+
+$ldap_dn
+
+    This variable, which is available only when Exim is compiled with LDAP
+    support, contains the DN from the last entry in the most recently
+    successful LDAP lookup.
+
+$load_average
+
+    This variable contains the system load average, multiplied by 1000 so that
+    it is an integer. For example, if the load average is 0.21, the value of
+    the variable is 210. The value is recomputed every time the variable is
+    referenced.
+
+$local_part
+
+    When an address is being routed, or delivered on its own, this variable
+    contains the local part. When a number of addresses are being delivered
+    together (for example, multiple RCPT commands in an SMTP session),
+    $local_part is not set.
+
+    Global address rewriting happens when a message is received, so the value
+    of $local_part during routing and delivery is the value after rewriting.
+    $local_part is set during user filtering, but not during system filtering,
+    because a message may have many recipients and the system filter is called
+    just once.
+
+    If a local part prefix or suffix has been recognized, it is not included in
+    the value of $local_part during routing and subsequent delivery. The values
+    of any prefix or suffix are in $local_part_prefix and $local_part_suffix,
+    respectively.
+
+    When a message is being delivered to a file, pipe, or autoreply transport
+    as a result of aliasing or forwarding, $local_part is set to the local part
+    of the parent address, not to the file name or command (see $address_file
+    and $address_pipe).
+
+    When an ACL is running for a RCPT command, $local_part contains the local
+    part of the recipient address.
+
+    When a rewrite item is being processed (see chapter 31), $local_part
+    contains the local part of the address that is being rewritten; it can be
+    used in the expansion of the replacement address, for example.
+
+    In all cases, all quoting is removed from the local part. For example, for
+    both the addresses
+
+    "abc:xyz"@test.example
+    abc\:xyz@test.example
+
+    the value of $local_part is
+
+    abc:xyz
+
+    If you use $local_part to create another address, you should always wrap it
+    inside a quoting operator. For example, in a redirect router you could
+    have:
+
+    data = ${quote_local_part:$local_part}@new.domain.example
+
+    Note: The value of $local_part is normally lower cased. If you want to
+    process local parts in a case-dependent manner in a router, you can set the
+    caseful_local_part option (see chapter 15).
+
+$local_part_data
+
+    When the local_parts option on a router matches a local part by means of a
+    lookup, the data read by the lookup is available during the running of the
+    router as $local_part_data. In addition, if the driver routes the address
+    to a transport, the value is available in that transport. If the transport
+    is handling multiple addresses, the value from the first address is used.
+
+    $local_part_data is also set when the local_parts condition in an ACL
+    matches a local part by means of a lookup. The data read by the lookup is
+    available during the rest of the ACL statement. In all other situations,
+    this variable expands to nothing.
+
+$local_part_prefix
+
+    When an address is being routed or delivered, and a specific prefix for the
+    local part was recognized, it is available in this variable, having been
+    removed from $local_part.
+
+$local_part_suffix
+
+    When an address is being routed or delivered, and a specific suffix for the
+    local part was recognized, it is available in this variable, having been
+    removed from $local_part.
+
+$local_scan_data
+
+    This variable contains the text returned by the local_scan() function when
+    a message is received. See chapter 44 for more details.
+
+$local_user_gid
+
+    See $local_user_uid.
+
+$local_user_uid
+
+    This variable and $local_user_gid are set to the uid and gid after the 
+    check_local_user router precondition succeeds. This means that their values
+    are available for the remaining preconditions (senders, require_files, and 
+    condition), for the address_data expansion, and for any router-specific
+    expansions. At all other times, the values in these variables are "(uid_t)
+    (-1)" and "(gid_t)(-1)", respectively.
+
+$localhost_number
+
+    This contains the expanded value of the localhost_number option. The
+    expansion happens after the main options have been read.
+
+$log_inodes
+
+    The number of free inodes in the disk partition where Exim's log files are
+    being written. The value is recalculated whenever the variable is
+    referenced. If the relevant file system does not have the concept of
+    inodes, the value of is -1. See also the check_log_inodes option.
+
+$log_space
+
+    The amount of free space (as a number of kilobytes) in the disk partition
+    where Exim's log files are being written. The value is recalculated
+    whenever the variable is referenced. If the operating system does not have
+    the ability to find the amount of free space (only true for experimental
+    systems), the space value is -1. See also the check_log_space option.
+
+$lookup_dnssec_authenticated
+
+    This variable is set after a DNS lookup done by a dnsdb lookup expansion,
+    dnslookup router or smtp transport. It will be empty if DNSSEC was not
+    requested, "no" if the result was not labelled as authenticated data and
+    "yes" if it was.
+
+$mailstore_basename
+
+    This variable is set only when doing deliveries in "mailstore" format in
+    the appendfile transport. During the expansion of the mailstore_prefix, 
+    mailstore_suffix, message_prefix, and message_suffix options, it contains
+    the basename of the files that are being written, that is, the name without
+    the ".tmp", ".env", or ".msg" suffix. At all other times, this variable is
+    empty.
+
+$malware_name
+
+    This variable is available when Exim is compiled with the content-scanning
+    extension. It is set to the name of the virus that was found when the ACL 
+    malware condition is true (see section 43.1).
+
+$max_received_linelength
+
+    This variable contains the number of bytes in the longest line that was
+    received as part of the message, not counting the line termination
+    character(s).
+
+$message_age
+
+    This variable is set at the start of a delivery attempt to contain the
+    number of seconds since the message was received. It does not change during
+    a single delivery attempt.
+
+$message_body
+
+    This variable contains the initial portion of a message's body while it is
+    being delivered, and is intended mainly for use in filter files. The
+    maximum number of characters of the body that are put into the variable is
+    set by the message_body_visible configuration option; the default is 500.
+
+    By default, newlines are converted into spaces in $message_body, to make it
+    easier to search for phrases that might be split over a line break.
+    However, this can be disabled by setting message_body_newlines to be true.
+    Binary zeros are always converted into spaces.
+
+$message_body_end
+
+    This variable contains the final portion of a message's body while it is
+    being delivered. The format and maximum size are as for $message_body.
+
+$message_body_size
+
+    When a message is being delivered, this variable contains the size of the
+    body in bytes. The count starts from the character after the blank line
+    that separates the body from the header. Newlines are included in the
+    count. See also $message_size, $body_linecount, and $body_zerocount.
+
+$message_exim_id
+
+    When a message is being received or delivered, this variable contains the
+    unique message id that is generated and used by Exim to identify the
+    message. An id is not created for a message until after its header has been
+    successfully received. Note: This is not the contents of the Message-ID:
+    header line; it is the local id that Exim assigns to the message, for
+    example: "1BXTIK-0001yO-VA".
+
+$message_headers
+
+    This variable contains a concatenation of all the header lines when a
+    message is being processed, except for lines added by routers or
+    transports. The header lines are separated by newline characters. Their
+    contents are decoded in the same way as a header line that is inserted by 
+    bheader.
+
+$message_headers_raw
+
+    This variable is like $message_headers except that no processing of the
+    contents of header lines is done.
+
+$message_id
+
+    This is an old name for $message_exim_id, which is now deprecated.
+
+$message_linecount
+
+    This variable contains the total number of lines in the header and body of
+    the message. Compare $body_linecount, which is the count for the body only.
+    During the DATA and content-scanning ACLs, $message_linecount contains the
+    number of lines received. Before delivery happens (that is, before filters,
+    routers, and transports run) the count is increased to include the 
+    Received: header line that Exim standardly adds, and also any other header
+    lines that are added by ACLs. The blank line that separates the message
+    header from the body is not counted.
+
+    As with the special case of $message_size, during the expansion of the
+    appendfile transport's maildir_tag option in maildir format, the value of
+    $message_linecount is the precise size of the number of newlines in the
+    file that has been written (minus one for the blank line between the header
+    and the body).
+
+    Here is an example of the use of this variable in a DATA ACL:
+
+    deny message   = Too many lines in message header
+         condition = \
+          ${if <{250}{${eval:$message_linecount - $body_linecount}}}
+
+    In the MAIL and RCPT ACLs, the value is zero because at that stage the
+    message has not yet been received.
+
+$message_size
+
+    When a message is being processed, this variable contains its size in
+    bytes. In most cases, the size includes those headers that were received
+    with the message, but not those (such as Envelope-to:) that are added to
+    individual deliveries as they are written. However, there is one special
+    case: during the expansion of the maildir_tag option in the appendfile
+    transport while doing a delivery in maildir format, the value of
+    $message_size is the precise size of the file that has been written. See
+    also $message_body_size, $body_linecount, and $body_zerocount.
+
+    While running a per message ACL (mail/rcpt/predata), $message_size contains
+    the size supplied on the MAIL command, or -1 if no size was given. The
+    value may not, of course, be truthful.
+
+$mime_xxx
+
+    A number of variables whose names start with $mime are available when Exim
+    is compiled with the content-scanning extension. For details, see section
+    43.4.
+
+$n0 - $n9
+
+    These variables are counters that can be incremented by means of the add
+    command in filter files.
+
+$original_domain
+
+    When a top-level address is being processed for delivery, this contains the
+    same value as $domain. However, if a "child" address (for example,
+    generated by an alias, forward, or filter file) is being processed, this
+    variable contains the domain of the original address (lower cased). This
+    differs from $parent_domain only when there is more than one level of
+    aliasing or forwarding. When more than one address is being delivered in a
+    single transport run, $original_domain is not set.
+
+    If a new address is created by means of a deliver command in a system
+    filter, it is set up with an artificial "parent" address. This has the
+    local part system-filter and the default qualify domain.
+
+$original_local_part
+
+    When a top-level address is being processed for delivery, this contains the
+    same value as $local_part, unless a prefix or suffix was removed from the
+    local part, because $original_local_part always contains the full local
+    part. When a "child" address (for example, generated by an alias, forward,
+    or filter file) is being processed, this variable contains the full local
+    part of the original address.
+
+    If the router that did the redirection processed the local part
+    case-insensitively, the value in $original_local_part is in lower case.
+    This variable differs from $parent_local_part only when there is more than
+    one level of aliasing or forwarding. When more than one address is being
+    delivered in a single transport run, $original_local_part is not set.
+
+    If a new address is created by means of a deliver command in a system
+    filter, it is set up with an artificial "parent" address. This has the
+    local part system-filter and the default qualify domain.
+
+$originator_gid
+
+    This variable contains the value of $caller_gid that was set when the
+    message was received. For messages received via the command line, this is
+    the gid of the sending user. For messages received by SMTP over TCP/IP,
+    this is normally the gid of the Exim user.
+
+$originator_uid
+
+    The value of $caller_uid that was set when the message was received. For
+    messages received via the command line, this is the uid of the sending
+    user. For messages received by SMTP over TCP/IP, this is normally the uid
+    of the Exim user.
+
+$parent_domain
+
+    This variable is similar to $original_domain (see above), except that it
+    refers to the immediately preceding parent address.
+
+$parent_local_part
+
+    This variable is similar to $original_local_part (see above), except that
+    it refers to the immediately preceding parent address.
+
+$pid
+
+    This variable contains the current process id.
+
+$pipe_addresses
+
+    This is not an expansion variable, but is mentioned here because the string
+    "$pipe_addresses" is handled specially in the command specification for the
+    pipe transport (chapter 29) and in transport filters (described under 
+    transport_filter in chapter 24). It cannot be used in general expansion
+    strings, and provokes an "unknown variable" error if encountered.
+
+$primary_hostname
+
+    This variable contains the value set by primary_hostname in the
+    configuration file, or read by the uname() function. If uname() returns a
+    single-component name, Exim calls gethostbyname() (or getipnodebyname()
+    where available) in an attempt to acquire a fully qualified host name. See
+    also $smtp_active_hostname.
+
+$prvscheck_address
+
+    This variable is used in conjunction with the prvscheck expansion item,
+    which is described in sections 11.5 and 42.51.
+
+$prvscheck_keynum
+
+    This variable is used in conjunction with the prvscheck expansion item,
+    which is described in sections 11.5 and 42.51.
+
+$prvscheck_result
+
+    This variable is used in conjunction with the prvscheck expansion item,
+    which is described in sections 11.5 and 42.51.
+
+$qualify_domain
+
+    The value set for the qualify_domain option in the configuration file.
+
+$qualify_recipient
+
+    The value set for the qualify_recipient option in the configuration file,
+    or if not set, the value of $qualify_domain.
+
+$rcpt_count
+
+    When a message is being received by SMTP, this variable contains the number
+    of RCPT commands received for the current message. If this variable is used
+    in a RCPT ACL, its value includes the current command.
+
+$rcpt_defer_count
+
+    When a message is being received by SMTP, this variable contains the number
+    of RCPT commands in the current message that have previously been rejected
+    with a temporary (4xx) response.
+
+$rcpt_fail_count
+
+    When a message is being received by SMTP, this variable contains the number
+    of RCPT commands in the current message that have previously been rejected
+    with a permanent (5xx) response.
+
+$received_count
+
+    This variable contains the number of Received: header lines in the message,
+    including the one added by Exim (so its value is always greater than zero).
+    It is available in the DATA ACL, the non-SMTP ACL, and while routing and
+    delivering.
+
+$received_for
+
+    If there is only a single recipient address in an incoming message, this
+    variable contains that address when the Received: header line is being
+    built. The value is copied after recipient rewriting has happened, but
+    before the local_scan() function is run.
+
+$received_ip_address
+
+    As soon as an Exim server starts processing an incoming TCP/IP connection,
+    this variable is set to the address of the local IP interface, and
+    $received_port is set to the local port number. (The remote IP address and
+    port are in $sender_host_address and $sender_host_port.) When testing with 
+    -bh, the port value is -1 unless it has been set using the -oMi command
+    line option.
+
+    As well as being useful in ACLs (including the "connect" ACL), these
+    variable could be used, for example, to make the file name for a TLS
+    certificate depend on which interface and/or port is being used for the
+    incoming connection. The values of $received_ip_address and $received_port
+    are saved with any messages that are received, thus making these variables
+    available at delivery time.
+
+    Note: There are no equivalent variables for outgoing connections, because
+    the values are unknown (unless they are explicitly set by options of the 
+    smtp transport).
+
+$received_port
+
+    See $received_ip_address.
+
+$received_protocol
+
+    When a message is being processed, this variable contains the name of the
+    protocol by which it was received. Most of the names used by Exim are
+    defined by RFCs 821, 2821, and 3848. They start with "smtp" (the client
+    used HELO) or "esmtp" (the client used EHLO). This can be followed by "s"
+    for secure (encrypted) and/or "a" for authenticated. Thus, for example, if
+    the protocol is set to "esmtpsa", the message was received over an
+    encrypted SMTP connection and the client was successfully authenticated.
+
+    Exim uses the protocol name "smtps" for the case when encryption is
+    automatically set up on connection without the use of STARTTLS (see 
+    tls_on_connect_ports), and the client uses HELO to initiate the encrypted
+    SMTP session. The name "smtps" is also used for the rare situation where
+    the client initially uses EHLO, sets up an encrypted connection using
+    STARTTLS, and then uses HELO afterwards.
+
+    The -oMr option provides a way of specifying a custom protocol name for
+    messages that are injected locally by trusted callers. This is commonly
+    used to identify messages that are being re-injected after some kind of
+    scanning.
+
+$received_time
+
+    This variable contains the date and time when the current message was
+    received, as a number of seconds since the start of the Unix epoch.
+
+$recipient_data
+
+    This variable is set after an indexing lookup success in an ACL recipients
+    condition. It contains the data from the lookup, and the value remains set
+    until the next recipients test. Thus, you can do things like this:
+
+    require recipients  = cdb*@;/some/file
+    deny    some further test involving $recipient_data
+
+    Warning: This variable is set only when a lookup is used as an indexing
+    method in the address list, using the semicolon syntax as in the example
+    above. The variable is not set for a lookup that is used as part of the
+    string expansion that all such lists undergo before being interpreted.
+
+$recipient_verify_failure
+
+    In an ACL, when a recipient verification fails, this variable contains
+    information about the failure. It is set to one of the following words:
+
+      * "qualify": The address was unqualified (no domain), and the message was
+        neither local nor came from an exempted host.
+
+      * "route": Routing failed.
+
+      * "mail": Routing succeeded, and a callout was attempted; rejection
+        occurred at or before the MAIL command (that is, on initial connection,
+        HELO, or MAIL).
+
+      * "recipient": The RCPT command in a callout was rejected.
+
+      * "postmaster": The postmaster check in a callout was rejected.
+
+    The main use of this variable is expected to be to distinguish between
+    rejections of MAIL and rejections of RCPT.
+
+$recipients
+
+    This variable contains a list of envelope recipients for a message. A comma
+    and a space separate the addresses in the replacement text. However, the
+    variable is not generally available, to prevent exposure of Bcc recipients
+    in unprivileged users' filter files. You can use $recipients only in these
+    cases:
+
+     1. In a system filter file.
+
+     2. In the ACLs associated with the DATA command and with non-SMTP
+        messages, that is, the ACLs defined by acl_smtp_predata, acl_smtp_data,
+        acl_smtp_mime, acl_not_smtp_start, acl_not_smtp, and acl_not_smtp_mime.
+
+     3. From within a local_scan() function.
+
+$recipients_count
+
+    When a message is being processed, this variable contains the number of
+    envelope recipients that came with the message. Duplicates are not excluded
+    from the count. While a message is being received over SMTP, the number
+    increases for each accepted recipient. It can be referenced in an ACL.
+
+$regex_match_string
+
+    This variable is set to contain the matching regular expression after a 
+    regex ACL condition has matched (see section 43.5).
+
+$reply_address
+
+    When a message is being processed, this variable contains the contents of
+    the Reply-To: header line if one exists and it is not empty, or otherwise
+    the contents of the From: header line. Apart from the removal of leading
+    white space, the value is not processed in any way. In particular, no RFC
+    2047 decoding or character code translation takes place.
+
+$return_path
+
+    When a message is being delivered, this variable contains the return path -
+    the sender field that will be sent as part of the envelope. It is not
+    enclosed in <> characters. At the start of routing an address, $return_path
+    has the same value as $sender_address, but if, for example, an incoming
+    message to a mailing list has been expanded by a router which specifies a
+    different address for bounce messages, $return_path subsequently contains
+    the new bounce address, whereas $sender_address always contains the
+    original sender address that was received with the message. In other words,
+    $sender_address contains the incoming envelope sender, and $return_path
+    contains the outgoing envelope sender.
+
+$return_size_limit
+
+    This is an obsolete name for $bounce_return_size_limit.
+
+$router_name
+
+    During the running of a router this variable contains its name.
+
+$runrc
+
+    This variable contains the return code from a command that is run by the $
+    {run...} expansion item. Warning: In a router or transport, you cannot
+    assume the order in which option values are expanded, except for those
+    preconditions whose order of testing is documented. Therefore, you cannot
+    reliably expect to set $runrc by the expansion of one option, and use it in
+    another.
+
+$self_hostname
+
+    When an address is routed to a supposedly remote host that turns out to be
+    the local host, what happens is controlled by the self generic router
+    option. One of its values causes the address to be passed to another
+    router. When this happens, $self_hostname is set to the name of the local
+    host that the original router encountered. In other circumstances its
+    contents are null.
+
+$sender_address
+
+    When a message is being processed, this variable contains the sender's
+    address that was received in the message's envelope. The case of letters in
+    the address is retained, in both the local part and the domain. For bounce
+    messages, the value of this variable is the empty string. See also
+    $return_path.
+
+$sender_address_data
+
+    If $address_data is set when the routers are called from an ACL to verify a
+    sender address, the final value is preserved in $sender_address_data, to
+    distinguish it from data from a recipient address. The value does not
+    persist after the end of the current ACL statement. If you want to preserve
+    it for longer, you can save it in an ACL variable.
+
+$sender_address_domain
+
+    The domain portion of $sender_address.
+
+$sender_address_local_part
+
+    The local part portion of $sender_address.
+
+$sender_data
+
+    This variable is set after a lookup success in an ACL senders condition or
+    in a router senders option. It contains the data from the lookup, and the
+    value remains set until the next senders test. Thus, you can do things like
+    this:
+
+    require senders      = cdb*@;/some/file
+    deny    some further test involving $sender_data
+
+    Warning: This variable is set only when a lookup is used as an indexing
+    method in the address list, using the semicolon syntax as in the example
+    above. The variable is not set for a lookup that is used as part of the
+    string expansion that all such lists undergo before being interpreted.
+
+$sender_fullhost
+
+    When a message is received from a remote host, this variable contains the
+    host name and IP address in a single string. It ends with the IP address in
+    square brackets, followed by a colon and a port number if the logging of
+    ports is enabled. The format of the rest of the string depends on whether
+    the host issued a HELO or EHLO SMTP command, and whether the host name was
+    verified by looking up its IP address. (Looking up the IP address can be
+    forced by the host_lookup option, independent of verification.) A plain
+    host name at the start of the string is a verified host name; if this is
+    not present, verification either failed or was not requested. A host name
+    in parentheses is the argument of a HELO or EHLO command. This is omitted
+    if it is identical to the verified host name or to the host's IP address in
+    square brackets.
+
+$sender_helo_name
+
+    When a message is received from a remote host that has issued a HELO or
+    EHLO command, the argument of that command is placed in this variable. It
+    is also set if HELO or EHLO is used when a message is received using SMTP
+    locally via the -bs or -bS options.
+
+$sender_host_address
+
+    When a message is received from a remote host, this variable contains that
+    host's IP address. For locally submitted messages, it is empty.
+
+$sender_host_authenticated
+
+    This variable contains the name (not the public name) of the authenticator
+    driver that successfully authenticated the client from which the message
+    was received. It is empty if there was no successful authentication. See
+    also $authenticated_id.
+
+$sender_host_dnssec
+
+    If an attempt to populate $sender_host_name has been made (by reference, 
+    hosts_lookup or otherwise) then this boolean will have been set true if,
+    and only if, the resolver library states that the reverse DNS was
+    authenticated data. At all other times, this variable is false.
+
+    It is likely that you will need to coerce DNSSEC support on in the resolver
+    library, by setting:
+
+    dns_dnssec_ok = 1
+
+    Exim does not perform DNSSEC validation itself, instead leaving that to a
+    validating resolver (eg, unbound, or bind with suitable configuration).
+
+    Exim does not (currently) check to see if the forward DNS was also secured
+    with DNSSEC, only the reverse DNS.
+
+    If you have changed host_lookup_order so that "bydns" is not the first
+    mechanism in the list, then this variable will be false.
+
+$sender_host_name
+
+    When a message is received from a remote host, this variable contains the
+    host's name as obtained by looking up its IP address. For messages received
+    by other means, this variable is empty.
+
+    If the host name has not previously been looked up, a reference to
+    $sender_host_name triggers a lookup (for messages from remote hosts). A
+    looked up name is accepted only if it leads back to the original IP address
+    via a forward lookup. If either the reverse or the forward lookup fails to
+    find any data, or if the forward lookup does not yield the original IP
+    address, $sender_host_name remains empty, and $host_lookup_failed is set to
+    "1".
+
+    However, if either of the lookups cannot be completed (for example, there
+    is a DNS timeout), $host_lookup_deferred is set to "1", and
+    $host_lookup_failed remains set to "0".
+
+    Once $host_lookup_failed is set to "1", Exim does not try to look up the
+    host name again if there is a subsequent reference to $sender_host_name in
+    the same Exim process, but it does try again if $host_lookup_deferred is
+    set to "1".
+
+    Exim does not automatically look up every calling host's name. If you want
+    maximum efficiency, you should arrange your configuration so that it avoids
+    these lookups altogether. The lookup happens only if one or more of the
+    following are true:
+
+      * A string containing $sender_host_name is expanded.
+
+      * The calling host matches the list in host_lookup. In the default
+        configuration, this option is set to *, so it must be changed if
+        lookups are to be avoided. (In the code, the default for host_lookup is
+        unset.)
+
+      * Exim needs the host name in order to test an item in a host list. The
+        items that require this are described in sections 10.13 and 10.17.
+
+      * The calling host matches helo_try_verify_hosts or helo_verify_hosts. In
+        this case, the host name is required to compare with the name quoted in
+        any EHLO or HELO commands that the client issues.
+
+      * The remote host issues a EHLO or HELO command that quotes one of the
+        domains in helo_lookup_domains. The default value of this option is
+
+          helo_lookup_domains = @ : @[]
+
+        which causes a lookup if a remote host (incorrectly) gives the server's
+        name or IP address in an EHLO or HELO command.
+
+$sender_host_port
+
+    When a message is received from a remote host, this variable contains the
+    port number that was used on the remote host.
+
+$sender_ident
+
+    When a message is received from a remote host, this variable contains the
+    identification received in response to an RFC 1413 request. When a message
+    has been received locally, this variable contains the login name of the
+    user that called Exim.
+
+$sender_rate_xxx
+
+    A number of variables whose names begin $sender_rate_ are set as part of
+    the ratelimit ACL condition. Details are given in section 42.38.
+
+$sender_rcvhost
+
+    This is provided specifically for use in Received: headers. It starts with
+    either the verified host name (as obtained from a reverse DNS lookup) or,
+    if there is no verified host name, the IP address in square brackets. After
+    that there may be text in parentheses. When the first item is a verified
+    host name, the first thing in the parentheses is the IP address in square
+    brackets, followed by a colon and a port number if port logging is enabled.
+    When the first item is an IP address, the port is recorded as "port=xxxx"
+    inside the parentheses.
+
+    There may also be items of the form "helo=xxxx" if HELO or EHLO was used
+    and its argument was not identical to the real host name or IP address, and
+    "ident=xxxx" if an RFC 1413 ident string is available. If all three items
+    are present in the parentheses, a newline and tab are inserted into the
+    string, to improve the formatting of the Received: header.
+
+$sender_verify_failure
+
+    In an ACL, when a sender verification fails, this variable contains
+    information about the failure. The details are the same as for
+    $recipient_verify_failure.
+
+$sending_ip_address
+
+    This variable is set whenever an outgoing SMTP connection to another host
+    has been set up. It contains the IP address of the local interface that is
+    being used. This is useful if a host that has more than one IP address
+    wants to take on different personalities depending on which one is being
+    used. For incoming connections, see $received_ip_address.
+
+$sending_port
+
+    This variable is set whenever an outgoing SMTP connection to another host
+    has been set up. It contains the local port that is being used. For
+    incoming connections, see $received_port.
+
+$smtp_active_hostname
+
+    During an incoming SMTP session, this variable contains the value of the
+    active host name, as specified by the smtp_active_hostname option. The
+    value of $smtp_active_hostname is saved with any message that is received,
+    so its value can be consulted during routing and delivery.
+
+$smtp_command
+
+    During the processing of an incoming SMTP command, this variable contains
+    the entire command. This makes it possible to distinguish between HELO and
+    EHLO in the HELO ACL, and also to distinguish between commands such as
+    these:
+
+    MAIL FROM:<>
+    MAIL FROM: <>
+
+    For a MAIL command, extra parameters such as SIZE can be inspected. For a
+    RCPT command, the address in $smtp_command is the original address before
+    any rewriting, whereas the values in $local_part and $domain are taken from
+    the address after SMTP-time rewriting.
+
+$smtp_command_argument
+
+    While an ACL is running to check an SMTP command, this variable contains
+    the argument, that is, the text that follows the command name, with leading
+    white space removed. Following the introduction of $smtp_command, this
+    variable is somewhat redundant, but is retained for backwards
+    compatibility.
+
+$smtp_count_at_connection_start
+
+    This variable is set greater than zero only in processes spawned by the
+    Exim daemon for handling incoming SMTP connections. The name is
+    deliberately long, in order to emphasize what the contents are. When the
+    daemon accepts a new connection, it increments this variable. A copy of the
+    variable is passed to the child process that handles the connection, but
+    its value is fixed, and never changes. It is only an approximation of how
+    many incoming connections there actually are, because many other
+    connections may come and go while a single connection is being processed.
+    When a child process terminates, the daemon decrements its copy of the
+    variable.
+
+$sn0 - $sn9
+
+    These variables are copies of the values of the $n0 - $n9 accumulators that
+    were current at the end of the system filter file. This allows a system
+    filter file to set values that can be tested in users' filter files. For
+    example, a system filter could set a value indicating how likely it is that
+    a message is junk mail.
+
+$spam_xxx
+
+    A number of variables whose names start with $spam are available when Exim
+    is compiled with the content-scanning extension. For details, see section
+    43.2.
+
+$spool_directory
+
+    The name of Exim's spool directory.
+
+$spool_inodes
+
+    The number of free inodes in the disk partition where Exim's spool files
+    are being written. The value is recalculated whenever the variable is
+    referenced. If the relevant file system does not have the concept of
+    inodes, the value of is -1. See also the check_spool_inodes option.
+
+$spool_space
+
+    The amount of free space (as a number of kilobytes) in the disk partition
+    where Exim's spool files are being written. The value is recalculated
+    whenever the variable is referenced. If the operating system does not have
+    the ability to find the amount of free space (only true for experimental
+    systems), the space value is -1. For example, to check in an ACL that there
+    is at least 50 megabytes free on the spool, you could write:
+
+    condition = ${if > {$spool_space}{50000}}
+
+    See also the check_spool_space option.
+
+$thisaddress
+
+    This variable is set only during the processing of the foranyaddress
+    command in a filter file. Its use is explained in the description of that
+    command, which can be found in the separate document entitled Exim's
+    interfaces to mail filtering.
+
+$tls_in_bits
+
+    Contains an approximation of the TLS cipher's bit-strength on the inbound
+    connection; the meaning of this depends upon the TLS implementation used.
+    If TLS has not been negotiated, the value will be 0. The value of this is
+    automatically fed into the Cyrus SASL authenticator when acting as a
+    server, to specify the "external SSF" (a SASL term).
+
+    The deprecated $tls_bits variable refers to the inbound side except when
+    used in the context of an outbound SMTP delivery, when it refers to the
+    outbound.
+
+$tls_out_bits
+
+    Contains an approximation of the TLS cipher's bit-strength on an outbound
+    SMTP connection; the meaning of this depends upon the TLS implementation
+    used. If TLS has not been negotiated, the value will be 0.
+
+$tls_in_ourcert
+
+    This variable refers to the certificate presented to the peer of an inbound
+    connection when the message was received. It is only useful as the argument
+    of a certextract expansion item, md5 or sha1 operator, or a def condition.
+
+$tls_in_peercert
+
+    This variable refers to the certificate presented by the peer of an inbound
+    connection when the message was received. It is only useful as the argument
+    of a certextract expansion item, md5 or sha1 operator, or a def condition.
+
+$tls_out_ourcert
+
+    This variable refers to the certificate presented to the peer of an
+    outbound connection. It is only useful as the argument of a certextract
+    expansion item, md5 or sha1 operator, or a def condition.
+
+$tls_out_peercert
+
+    This variable refers to the certificate presented by the peer of an
+    outbound connection. It is only useful as the argument of a certextract
+    expansion item, md5 or sha1 operator, or a def condition.
+
+$tls_in_certificate_verified
+
+    This variable is set to "1" if a TLS certificate was verified when the
+    message was received, and "0" otherwise.
+
+    The deprecated $tls_certificate_verfied variable refers to the inbound side
+    except when used in the context of an outbound SMTP delivery, when it
+    refers to the outbound.
+
+$tls_out_certificate_verified
+
+    This variable is set to "1" if a TLS certificate was verified when an
+    outbound SMTP connection was made, and "0" otherwise.
+
+$tls_in_cipher
+
+    When a message is received from a remote host over an encrypted SMTP
+    connection, this variable is set to the cipher suite that was negotiated,
+    for example DES-CBC3-SHA. In other circumstances, in particular, for
+    message received over unencrypted connections, the variable is empty.
+    Testing $tls_cipher for emptiness is one way of distinguishing between
+    encrypted and non-encrypted connections during ACL processing.
+
+    The deprecated $tls_cipher variable is the same as $tls_in_cipher during
+    message reception, but in the context of an outward SMTP delivery taking
+    place via the smtp transport becomes the same as $tls_out_cipher.
+
+$tls_out_cipher
+
+    This variable is cleared before any outgoing SMTP connection is made, and
+    then set to the outgoing cipher suite if one is negotiated. See chapter 41
+    for details of TLS support and chapter 30 for details of the smtp
+    transport.
+
+$tls_in_ocsp
+
+    When a message is received from a remote client connection the result of
+    any OCSP request from the client is encoded in this variable:
+
+    0 OCSP proof was not requested (default value)
+    1 No response to request
+    2 Response not verified
+    3 Verification failed
+    4 Verification succeeded
+
+$tls_out_ocsp
+
+    When a message is sent to a remote host connection the result of any OCSP
+    request made is encoded in this variable. See $tls_in_ocsp for values.
+
+$tls_in_peerdn
+
+    When a message is received from a remote host over an encrypted SMTP
+    connection, and Exim is configured to request a certificate from the
+    client, the value of the Distinguished Name of the certificate is made
+    available in the $tls_in_peerdn during subsequent processing.
+
+    The deprecated $tls_peerdn variable refers to the inbound side except when
+    used in the context of an outbound SMTP delivery, when it refers to the
+    outbound.
+
+$tls_out_peerdn
+
+    When a message is being delivered to a remote host over an encrypted SMTP
+    connection, and Exim is configured to request a certificate from the
+    server, the value of the Distinguished Name of the certificate is made
+    available in the $tls_out_peerdn during subsequent processing.
+
+$tls_in_sni
+
+    When a TLS session is being established, if the client sends the Server
+    Name Indication extension, the value will be placed in this variable. If
+    the variable appears in tls_certificate then this option and some others,
+    described in 41.10, will be re-expanded early in the TLS session, to permit
+    a different certificate to be presented (and optionally a different key to
+    be used) to the client, based upon the value of the SNI extension.
+
+    The deprecated $tls_sni variable refers to the inbound side except when
+    used in the context of an outbound SMTP delivery, when it refers to the
+    outbound.
+
+$tls_out_sni
+
+    During outbound SMTP deliveries, this variable reflects the value of the 
+    tls_sni option on the transport.
+
+$tod_bsdinbox
+
+    The time of day and the date, in the format required for BSD-style mailbox
+    files, for example: Thu Oct 17 17:14:09 1995.
+
+$tod_epoch
+
+    The time and date as a number of seconds since the start of the Unix epoch.
+
+$tod_epoch_l
+
+    The time and date as a number of microseconds since the start of the Unix
+    epoch.
+
+$tod_full
+
+    A full version of the time and date, for example: Wed, 16 Oct 1995 09:51:40
+    +0100. The timezone is always given as a numerical offset from UTC, with
+    positive values used for timezones that are ahead (east) of UTC, and
+    negative values for those that are behind (west).
+
+$tod_log
+
+    The time and date in the format used for writing Exim's log files, for
+    example: 1995-10-12 15:32:29, but without a timezone.
+
+$tod_logfile
+
+    This variable contains the date in the format yyyymmdd. This is the format
+    that is used for datestamping log files when log_file_path contains the
+    "%D" flag.
+
+$tod_zone
+
+    This variable contains the numerical value of the local timezone, for
+    example: -0500.
+
+$tod_zulu
+
+    This variable contains the UTC date and time in "Zulu" format, as specified
+    by ISO 8601, for example: 20030221154023Z.
+
+$transport_name
+
+    During the running of a transport, this variable contains its name.
+
+$value
+
+    This variable contains the result of an expansion lookup, extraction
+    operation, or external command, as described above. It is also used during
+    a reduce expansion.
+
+$version_number
+
+    The version number of Exim.
+
+$warn_message_delay
+
+    This variable is set only during the creation of a message warning about a
+    delivery delay. Details of its use are explained in section 48.2.
+
+$warn_message_recipients
+
+    This variable is set only during the creation of a message warning about a
+    delivery delay. Details of its use are explained in section 48.2.
+
+
+
+===============================================================================
+12. EMBEDDED PERL
+
+Exim can be built to include an embedded Perl interpreter. When this is done,
+Perl subroutines can be called as part of the string expansion process. To make
+use of the Perl support, you need version 5.004 or later of Perl installed on
+your system. To include the embedded interpreter in the Exim binary, include
+the line
+
+EXIM_PERL = perl.o
+
+in your Local/Makefile and then build Exim in the normal way.
+
+
+12.1 Setting up so Perl can be used
+-----------------------------------
+
+Access to Perl subroutines is via a global configuration option called 
+perl_startup and an expansion string operator ${perl ...}. If there is no 
+perl_startup option in the Exim configuration file then no Perl interpreter is
+started and there is almost no overhead for Exim (since none of the Perl
+library will be paged in unless used). If there is a perl_startup option then
+the associated value is taken to be Perl code which is executed in a newly
+created Perl interpreter.
+
+The value of perl_startup is not expanded in the Exim sense, so you do not need
+backslashes before any characters to escape special meanings. The option should
+usually be something like
+
+perl_startup = do '/etc/exim.pl'
+
+where /etc/exim.pl is Perl code which defines any subroutines you want to use
+from Exim. Exim can be configured either to start up a Perl interpreter as soon
+as it is entered, or to wait until the first time it is needed. Starting the
+interpreter at the beginning ensures that it is done while Exim still has its
+setuid privilege, but can impose an unnecessary overhead if Perl is not in fact
+used in a particular run. Also, note that this does not mean that Exim is
+necessarily running as root when Perl is called at a later time. By default,
+the interpreter is started only when it is needed, but this can be changed in
+two ways:
+
+  * Setting perl_at_start (a boolean option) in the configuration requests a
+    startup when Exim is entered.
+
+  * The command line option -ps also requests a startup when Exim is entered,
+    overriding the setting of perl_at_start.
+
+There is also a command line option -pd (for delay) which suppresses the
+initial startup, even if perl_at_start is set.
+
+
+12.2 Calling Perl subroutines
+-----------------------------
+
+When the configuration file includes a perl_startup option you can make use of
+the string expansion item to call the Perl subroutines that are defined by the 
+perl_startup code. The operator is used in any of the following forms:
+
+${perl{foo}}
+${perl{foo}{argument}}
+${perl{foo}{argument1}{argument2} ... }
+
+which calls the subroutine foo with the given arguments. A maximum of eight
+arguments may be passed. Passing more than this results in an expansion failure
+with an error message of the form
+
+Too many arguments passed to Perl subroutine "foo" (max is 8)
+
+The return value of the Perl subroutine is evaluated in a scalar context before
+it is passed back to Exim to be inserted into the expanded string. If the
+return value is undef, the expansion is forced to fail in the same way as an
+explicit "fail" on an if or lookup item. If the subroutine aborts by obeying
+Perl's die function, the expansion fails with the error message that was passed
+to die.
+
+
+12.3 Calling Exim functions from Perl
+-------------------------------------
+
+Within any Perl code called from Exim, the function Exim::expand_string() is
+available to call back into Exim's string expansion function. For example, the
+Perl code
+
+my $lp = Exim::expand_string('$local_part');
+
+makes the current Exim $local_part available in the Perl variable $lp. Note
+those are single quotes and not double quotes to protect against $local_part
+being interpolated as a Perl variable.
+
+If the string expansion is forced to fail by a "fail" item, the result of 
+Exim::expand_string() is undef. If there is a syntax error in the expansion
+string, the Perl call from the original expansion string fails with an
+appropriate error message, in the same way as if die were used.
+
+Two other Exim functions are available for use from within Perl code. 
+Exim::debug_write() writes a string to the standard error stream if Exim's
+debugging is enabled. If you want a newline at the end, you must supply it. 
+Exim::log_write() writes a string to Exim's main log, adding a leading
+timestamp. In this case, you should not supply a terminating newline.
+
+
+12.4 Use of standard output and error by Perl
+---------------------------------------------
+
+You should not write to the standard error or output streams from within your
+Perl code, as it is not defined how these are set up. In versions of Exim
+before 4.50, it is possible for the standard output or error to refer to the
+SMTP connection during message reception via the daemon. Writing to this stream
+is certain to cause chaos. From Exim 4.50 onwards, the standard output and
+error streams are connected to /dev/null in the daemon. The chaos is avoided,
+but the output is lost.
+
+The Perl warn statement writes to the standard error stream by default. Calls
+to warn may be embedded in Perl modules that you use, but over which you have
+no control. When Exim starts up the Perl interpreter, it arranges for output
+from the warn statement to be written to the Exim main log. You can change this
+by including appropriate Perl magic somewhere in your Perl code. For example,
+to discard warn output completely, you need this:
+
+$SIG{__WARN__} = sub { };
+
+Whenever a warn is obeyed, the anonymous subroutine is called. In this example,
+the code for the subroutine is empty, so it does nothing, but you can include
+any Perl code that you like. The text of the warn message is passed as the
+first subroutine argument.
+
+
+
+===============================================================================
+13. STARTING THE DAEMON AND THE USE OF NETWORK INTERFACES
+
+A host that is connected to a TCP/IP network may have one or more physical
+hardware network interfaces. Each of these interfaces may be configured as one
+or more "logical" interfaces, which are the entities that a program actually
+works with. Each of these logical interfaces is associated with an IP address.
+In addition, TCP/IP software supports "loopback" interfaces (127.0.0.1 in IPv4
+and ::1 in IPv6), which do not use any physical hardware. Exim requires
+knowledge about the host's interfaces for use in three different circumstances:
+
+ 1. When a listening daemon is started, Exim needs to know which interfaces and
+    ports to listen on.
+
+ 2. When Exim is routing an address, it needs to know which IP addresses are
+    associated with local interfaces. This is required for the correct
+    processing of MX lists by removing the local host and others with the same
+    or higher priority values. Also, Exim needs to detect cases when an address
+    is routed to an IP address that in fact belongs to the local host. Unless
+    the self router option or the allow_localhost option of the smtp transport
+    is set (as appropriate), this is treated as an error situation.
+
+ 3. When Exim connects to a remote host, it may need to know which interface to
+    use for the outgoing connection.
+
+Exim's default behaviour is likely to be appropriate in the vast majority of
+cases. If your host has only one interface, and you want all its IP addresses
+to be treated in the same way, and you are using only the standard SMTP port,
+you should not need to take any special action. The rest of this chapter does
+not apply to you.
+
+In a more complicated situation you may want to listen only on certain
+interfaces, or on different ports, and for this reason there are a number of
+options that can be used to influence Exim's behaviour. The rest of this
+chapter describes how they operate.
+
+When a message is received over TCP/IP, the interface and port that were
+actually used are set in $received_ip_address and $received_port.
+
+
+13.1 Starting a listening daemon
+--------------------------------
+
+When a listening daemon is started (by means of the -bd command line option),
+the interfaces and ports on which it listens are controlled by the following
+options:
+
+  * daemon_smtp_ports contains a list of default ports or service names. (For
+    backward compatibility, this option can also be specified in the singular.)
+
+  * local_interfaces contains list of interface IP addresses on which to
+    listen. Each item may optionally also specify a port.
+
+The default list separator in both cases is a colon, but this can be changed as
+described in section 6.19. When IPv6 addresses are involved, it is usually best
+to change the separator to avoid having to double all the colons. For example:
+
+local_interfaces = <; 127.0.0.1 ; \
+                      192.168.23.65 ; \
+                      ::1 ; \
+                      3ffe:ffff:836f::fe86:a061
+
+There are two different formats for specifying a port along with an IP address
+in local_interfaces:
+
+ 1. The port is added onto the address with a dot separator. For example, to
+    listen on port 1234 on two different IP addresses:
+
+    local_interfaces = <; 192.168.23.65.1234 ; \
+                          3ffe:ffff:836f::fe86:a061.1234
+
+ 2. The IP address is enclosed in square brackets, and the port is added with a
+    colon separator, for example:
+
+    local_interfaces = <; [192.168.23.65]:1234 ; \
+                          [3ffe:ffff:836f::fe86:a061]:1234
+
+When a port is not specified, the value of daemon_smtp_ports is used. The
+default setting contains just one port:
+
+daemon_smtp_ports = smtp
+
+If more than one port is listed, each interface that does not have its own port
+specified listens on all of them. Ports that are listed in daemon_smtp_ports
+can be identified either by name (defined in /etc/services) or by number.
+However, when ports are given with individual IP addresses in local_interfaces,
+only numbers (not names) can be used.
+
+
+13.2 Special IP listening addresses
+-----------------------------------
+
+The addresses 0.0.0.0 and ::0 are treated specially. They are interpreted as
+"all IPv4 interfaces" and "all IPv6 interfaces", respectively. In each case,
+Exim tells the TCP/IP stack to "listen on all IPvx interfaces" instead of
+setting up separate listening sockets for each interface. The default value of 
+local_interfaces is
+
+local_interfaces = 0.0.0.0
+
+when Exim is built without IPv6 support; otherwise it is:
+
+local_interfaces = <; ::0 ; 0.0.0.0
+
+Thus, by default, Exim listens on all available interfaces, on the SMTP port.
+
+
+13.3 Overriding local_interfaces and daemon_smtp_ports
+------------------------------------------------------
+
+The -oX command line option can be used to override the values of 
+daemon_smtp_ports and/or local_interfaces for a particular daemon instance.
+Another way of doing this would be to use macros and the -D option. However, 
+-oX can be used by any admin user, whereas modification of the runtime
+configuration by -D is allowed only when the caller is root or exim.
+
+The value of -oX is a list of items. The default colon separator can be changed
+in the usual way if required. If there are any items that do not contain dots
+or colons (that is, are not IP addresses), the value of daemon_smtp_ports is
+replaced by the list of those items. If there are any items that do contain
+dots or colons, the value of local_interfaces is replaced by those items. Thus,
+for example,
+
+-oX 1225
+
+overrides daemon_smtp_ports, but leaves local_interfaces unchanged, whereas
+
+-oX 192.168.34.5.1125
+
+overrides local_interfaces, leaving daemon_smtp_ports unchanged. (However,
+since local_interfaces now contains no items without ports, the value of 
+daemon_smtp_ports is no longer relevant in this example.)
+
+
+13.4 Support for the obsolete SSMTP (or SMTPS) protocol
+-------------------------------------------------------
+
+Exim supports the obsolete SSMTP protocol (also known as SMTPS) that was used
+before the STARTTLS command was standardized for SMTP. Some legacy clients
+still use this protocol. If the tls_on_connect_ports option is set to a list of
+port numbers or service names, connections to those ports must use SSMTP. The
+most common use of this option is expected to be
+
+tls_on_connect_ports = 465
+
+because 465 is the usual port number used by the legacy clients. There is also
+a command line option -tls-on-connect, which forces all ports to behave in this
+way when a daemon is started.
+
+Warning: Setting tls_on_connect_ports does not of itself cause the daemon to
+listen on those ports. You must still specify them in daemon_smtp_ports, 
+local_interfaces, or the -oX option. (This is because tls_on_connect_ports
+applies to inetd connections as well as to connections via the daemon.)
+
+
+13.5 IPv6 address scopes
+------------------------
+
+IPv6 addresses have "scopes", and a host with multiple hardware interfaces can,
+in principle, have the same link-local IPv6 address on different interfaces.
+Thus, additional information is needed, over and above the IP address, to
+distinguish individual interfaces. A convention of using a percent sign
+followed by something (often the interface name) has been adopted in some
+cases, leading to addresses like this:
+
+fe80::202:b3ff:fe03:45c1%eth0
+
+To accommodate this usage, a percent sign followed by an arbitrary string is
+allowed at the end of an IPv6 address. By default, Exim calls getaddrinfo() to
+convert a textual IPv6 address for actual use. This function recognizes the
+percent convention in operating systems that support it, and it processes the
+address appropriately. Unfortunately, some older libraries have problems with
+getaddrinfo(). If
+
+IPV6_USE_INET_PTON=yes
+
+is set in Local/Makefile (or an OS-dependent Makefile) when Exim is built, Exim
+uses inet_pton() to convert a textual IPv6 address for actual use, instead of
+getaddrinfo(). (Before version 4.14, it always used this function.) Of course,
+this means that the additional functionality of getaddrinfo() - recognizing
+scoped addresses - is lost.
+
+
+13.6 Disabling IPv6
+-------------------
+
+Sometimes it happens that an Exim binary that was compiled with IPv6 support is
+run on a host whose kernel does not support IPv6. The binary will fall back to
+using IPv4, but it may waste resources looking up AAAA records, and trying to
+connect to IPv6 addresses, causing delays to mail delivery. If you set the 
+disable_ipv6 option true, even if the Exim binary has IPv6 support, no IPv6
+activities take place. AAAA records are never looked up, and any IPv6 addresses
+that are listed in local_interfaces, data for the manualroute router, etc. are
+ignored. If IP literals are enabled, the ipliteral router declines to handle
+IPv6 literal addresses.
+
+On the other hand, when IPv6 is in use, there may be times when you want to
+disable it for certain hosts or domains. You can use the dns_ipv4_lookup option
+to globally suppress the lookup of AAAA records for specified domains, and you
+can use the ignore_target_hosts generic router option to ignore IPv6 addresses
+in an individual router.
+
+
+13.7 Examples of starting a listening daemon
+--------------------------------------------
+
+The default case in an IPv6 environment is
+
+daemon_smtp_ports = smtp
+local_interfaces = <; ::0 ; 0.0.0.0
+
+This specifies listening on the smtp port on all IPv6 and IPv4 interfaces.
+Either one or two sockets may be used, depending on the characteristics of the
+TCP/IP stack. (This is complicated and messy; for more information, read the
+comments in the daemon.c source file.)
+
+To specify listening on ports 25 and 26 on all interfaces:
+
+daemon_smtp_ports = 25 : 26
+
+(leaving local_interfaces at the default setting) or, more explicitly:
+
+local_interfaces = <; ::0.25     ; ::0.26 \
+                      0.0.0.0.25 ; 0.0.0.0.26
+
+To listen on the default port on all IPv4 interfaces, and on port 26 on the
+IPv4 loopback address only:
+
+local_interfaces = 0.0.0.0 : 127.0.0.1.26
+
+To specify listening on the default port on specific interfaces only:
+
+local_interfaces = 10.0.0.67 : 192.168.34.67
+
+Warning: Such a setting excludes listening on the loopback interfaces.
+
+
+13.8 Recognizing the local host
+-------------------------------
+
+The local_interfaces option is also used when Exim needs to determine whether
+or not an IP address refers to the local host. That is, the IP addresses of all
+the interfaces on which a daemon is listening are always treated as local.
+
+For this usage, port numbers in local_interfaces are ignored. If either of the
+items 0.0.0.0 or ::0 are encountered, Exim gets a complete list of available
+interfaces from the operating system, and extracts the relevant (that is, IPv4
+or IPv6) addresses to use for checking.
+
+Some systems set up large numbers of virtual interfaces in order to provide
+many virtual web servers. In this situation, you may want to listen for email
+on only a few of the available interfaces, but nevertheless treat all
+interfaces as local when routing. You can do this by setting 
+extra_local_interfaces to a list of IP addresses, possibly including the "all"
+wildcard values. These addresses are recognized as local, but are not used for
+listening. Consider this example:
+
+local_interfaces = <; 127.0.0.1 ; ::1 ; \
+                      192.168.53.235 ; \
+                      3ffe:2101:12:1:a00:20ff:fe86:a061
+
+extra_local_interfaces = <; ::0 ; 0.0.0.0
+
+The daemon listens on the loopback interfaces and just one IPv4 and one IPv6
+address, but all available interface addresses are treated as local when Exim
+is routing.
+
+In some environments the local host name may be in an MX list, but with an IP
+address that is not assigned to any local interface. In other cases it may be
+desirable to treat other host names as if they referred to the local host. Both
+these cases can be handled by setting the hosts_treat_as_local option. This
+contains host names rather than IP addresses. When a host is referenced during
+routing, either via an MX record or directly, it is treated as the local host
+if its name matches hosts_treat_as_local, or if any of its IP addresses match 
+local_interfaces or extra_local_interfaces.
+
+
+13.9 Delivering to a remote host
+--------------------------------
+
+Delivery to a remote host is handled by the smtp transport. By default, it
+allows the system's TCP/IP functions to choose which interface to use (if there
+is more than one) when connecting to a remote host. However, the interface
+option can be set to specify which interface is used. See the description of
+the smtp transport in chapter 30 for more details.
+
+
+
+===============================================================================
+14. MAIN CONFIGURATION
+
+The first part of the run time configuration file contains three types of item:
+
+  * Macro definitions: These lines start with an upper case letter. See section
+    6.4 for details of macro processing.
+
+  * Named list definitions: These lines start with one of the words
+    "domainlist", "hostlist", "addresslist", or "localpartlist". Their use is
+    described in section 10.5.
+
+  * Main configuration settings: Each setting occupies one line of the file
+    (with possible continuations). If any setting is preceded by the word
+    "hide", the -bP command line option displays its value to admin users only.
+    See section 6.10 for a description of the syntax of these option settings.
+
+This chapter specifies all the main configuration options, along with their
+types and default values. For ease of finding a particular option, they appear
+in alphabetical order in section 14.23 below. However, because there are now so
+many options, they are first listed briefly in functional groups, as an aid to
+finding the name of the option you are looking for. Some options are listed in
+more than one group.
+
+
+14.1 Miscellaneous
+------------------
+
+bi_command            to run for -bi command line option
+disable_ipv6          do no IPv6 processing
+keep_malformed        for broken files - should not happen
+localhost_number      for unique message ids in clusters
+message_body_newlines retain newlines in $message_body
+message_body_visible  how much to show in $message_body
+mua_wrapper           run in "MUA wrapper" mode
+print_topbitchars     top-bit characters are printing
+timezone              force time zone
+
+
+14.2 Exim parameters
+--------------------
+
+exim_group            override compiled-in value
+exim_path             override compiled-in value
+exim_user             override compiled-in value
+primary_hostname      default from uname()
+split_spool_directory use multiple directories
+spool_directory       override compiled-in value
+
+
+14.3 Privilege controls
+-----------------------
+
+admin_groups              groups that are Exim admin users
+deliver_drop_privilege    drop root for delivery processes
+local_from_check          insert Sender: if necessary
+local_from_prefix         for testing From: for local sender
+local_from_suffix         for testing From: for local sender
+local_sender_retain       keep Sender: from untrusted user
+never_users               do not run deliveries as these
+prod_requires_admin       forced delivery requires admin user
+queue_list_requires_admin queue listing requires admin user
+trusted_groups            groups that are trusted
+trusted_users             users that are trusted
+
+
+14.4 Logging
+------------
+
+hosts_connection_nolog exemption from connect logging
+log_file_path          override compiled-in value
+log_selector           set/unset optional logging
+log_timezone           add timezone to log lines
+message_logs           create per-message logs
+preserve_message_logs  after message completion
+process_log_path       for SIGUSR1 and exiwhat
+syslog_duplication     controls duplicate log lines on syslog
+syslog_facility        set syslog "facility" field
+syslog_processname     set syslog "ident" field
+syslog_timestamp       timestamp syslog lines
+write_rejectlog        control use of message log
+
+
+14.5 Frozen messages
+--------------------
+
+auto_thaw            sets time for retrying frozen messages
+freeze_tell          send message when freezing
+move_frozen_messages to another directory
+timeout_frozen_after keep frozen messages only so long
+
+
+14.6 Data lookups
+-----------------
+
+ibase_servers        InterBase servers
+ldap_ca_cert_dir     dir of CA certs to verify LDAP server's
+ldap_ca_cert_file    file of CA certs to verify LDAP server's
+ldap_cert_file       client cert file for LDAP
+ldap_cert_key        client key file for LDAP
+ldap_cipher_suite    TLS negotiation preference control
+ldap_default_servers used if no server in query
+ldap_require_cert    action to take without LDAP server cert
+ldap_start_tls       require TLS within LDAP
+ldap_version         set protocol version
+lookup_open_max      lookup files held open
+mysql_servers        default MySQL servers
+oracle_servers       Oracle servers
+pgsql_servers        default PostgreSQL servers
+sqlite_lock_timeout  as it says
+
+
+14.7 Message ids
+----------------
+
+message_id_header_domain used to build Message-ID: header
+message_id_header_text   ditto
+
+
+14.8 Embedded Perl Startup
+--------------------------
+
+perl_at_start always start the interpreter
+perl_startup  code to obey when starting Perl
+
+
+14.9 Daemon
+-----------
+
+daemon_smtp_ports      default ports
+daemon_startup_retries number of times to retry
+daemon_startup_sleep   time to sleep between tries
+extra_local_interfaces not necessarily listened on
+local_interfaces       on which to listen, with optional ports
+pid_file_path          override compiled-in value
+queue_run_max          maximum simultaneous queue runners
+
+
+14.10 Resource control
+----------------------
+
+check_log_inodes                 before accepting a message
+check_log_space                  before accepting a message
+check_spool_inodes               before accepting a message
+check_spool_space                before accepting a message
+deliver_queue_load_max           no queue deliveries if load high
+queue_only_load                  queue incoming if load high
+queue_only_load_latch            don't re-evaluate load for each message
+queue_run_max                    maximum simultaneous queue runners
+remote_max_parallel              parallel SMTP delivery per message
+smtp_accept_max                  simultaneous incoming connections
+smtp_accept_max_nonmail          non-mail commands
+smtp_accept_max_nonmail_hosts    hosts to which the limit applies
+smtp_accept_max_per_connection   messages per connection
+smtp_accept_max_per_host         connections from one host
+smtp_accept_queue                queue mail if more connections
+smtp_accept_queue_per_connection queue if more messages per connection
+smtp_accept_reserve              only reserve hosts if more connections
+smtp_check_spool_space           from SIZE on MAIL command
+smtp_connect_backlog             passed to TCP/IP stack
+smtp_load_reserve                SMTP from reserved hosts if load high
+smtp_reserve_hosts               these are the reserve hosts
+
+
+14.11 Policy controls
+---------------------
+
+acl_not_smtp           ACL for non-SMTP messages
+acl_not_smtp_mime      ACL for non-SMTP MIME parts
+acl_not_smtp_start     ACL for start of non-SMTP message
+acl_smtp_auth          ACL for AUTH
+acl_smtp_connect       ACL for connection
+acl_smtp_data          ACL for DATA
+acl_smtp_data_prdr     ACL for DATA, per-recipient
+acl_smtp_dkim          ACL for DKIM verification
+acl_smtp_etrn          ACL for ETRN
+acl_smtp_expn          ACL for EXPN
+acl_smtp_helo          ACL for EHLO or HELO
+acl_smtp_mail          ACL for MAIL
+acl_smtp_mailauth      ACL for AUTH on MAIL command
+acl_smtp_mime          ACL for MIME parts
+acl_smtp_predata       ACL for start of data
+acl_smtp_quit          ACL for QUIT
+acl_smtp_rcpt          ACL for RCPT
+acl_smtp_starttls      ACL for STARTTLS
+acl_smtp_vrfy          ACL for VRFY
+av_scanner             specify virus scanner
+check_rfc2047_length   check length of RFC 2047 "encoded words"
+dns_csa_search_limit   control CSA parent search depth
+dns_csa_use_reverse    en/disable CSA IP reverse search
+header_maxsize         total size of message header
+header_line_maxsize    individual header line limit
+helo_accept_junk_hosts allow syntactic junk from these hosts
+helo_allow_chars       allow illegal chars in HELO names
+helo_lookup_domains    lookup hostname for these HELO names
+helo_try_verify_hosts  HELO soft-checked for these hosts
+helo_verify_hosts      HELO hard-checked for these hosts
+host_lookup            host name looked up for these hosts
+host_lookup_order      order of DNS and local name lookups
+host_reject_connection reject connection from these hosts
+hosts_treat_as_local   useful in some cluster configurations
+local_scan_timeout     timeout for local_scan()
+message_size_limit     for all messages
+percent_hack_domains   recognize %-hack for these domains
+spamd_address          set interface to SpamAssassin
+strict_acl_vars        object to unset ACL variables
+
+
+14.12 Callout cache
+-------------------
+
+callout_domain_negative_expire timeout for negative domain cache item
+callout_domain_positive_expire timeout for positive domain cache item
+callout_negative_expire        timeout for negative address cache item
+callout_positive_expire        timeout for positive address cache item
+callout_random_local_part      string to use for "random" testing
+
+
+14.13 TLS
+---------
+
+gnutls_compat_mode       use GnuTLS compatibility mode
+gnutls_allow_auto_pkcs11 allow GnuTLS to autoload PKCS11 modules
+openssl_options          adjust OpenSSL compatibility options
+tls_advertise_hosts      advertise TLS to these hosts
+tls_certificate          location of server certificate
+tls_crl                  certificate revocation list
+tls_dh_max_bits          clamp D-H bit count suggestion
+tls_dhparam              DH parameters for server
+tls_ocsp_file            location of server certificate status proof
+tls_on_connect_ports     specify SSMTP (SMTPS) ports
+tls_privatekey           location of server private key
+tls_remember_esmtp       don't reset after starting TLS
+tls_require_ciphers      specify acceptable ciphers
+tls_try_verify_hosts     try to verify client certificate
+tls_verify_certificates  expected client certificates
+tls_verify_hosts         insist on client certificate verify
+
+
+14.14 Local user handling
+-------------------------
+
+finduser_retries    useful in NIS environments
+gecos_name          used when creating Sender:
+gecos_pattern       ditto
+max_username_length for systems that truncate
+unknown_login       used when no login name found
+unknown_username    ditto
+uucp_from_pattern   for recognizing "From " lines
+uucp_from_sender    ditto
+
+
+14.15 All incoming messages (SMTP and non-SMTP)
+-----------------------------------------------
+
+header_maxsize        total size of message header
+header_line_maxsize   individual header line limit
+message_size_limit    applies to all messages
+percent_hack_domains  recognize %-hack for these domains
+received_header_text  expanded to make Received:
+received_headers_max  for mail loop detection
+recipients_max        limit per message
+recipients_max_reject permanently reject excess recipients
+
+
+14.16 Non-SMTP incoming messages
+--------------------------------
+
+receive_timeout for non-SMTP messages
+
+
+14.17 Incoming SMTP messages
+----------------------------
+
+See also the Policy controls section above.
+
+host_lookup                      host name looked up for these hosts
+host_lookup_order                order of DNS and local name lookups
+recipient_unqualified_hosts      may send unqualified recipients
+rfc1413_hosts                    make ident calls to these hosts
+rfc1413_query_timeout            zero disables ident calls
+sender_unqualified_hosts         may send unqualified senders
+smtp_accept_keepalive            some TCP/IP magic
+smtp_accept_max                  simultaneous incoming connections
+smtp_accept_max_nonmail          non-mail commands
+smtp_accept_max_nonmail_hosts    hosts to which the limit applies
+smtp_accept_max_per_connection   messages per connection
+smtp_accept_max_per_host         connections from one host
+smtp_accept_queue                queue mail if more connections
+smtp_accept_queue_per_connection queue if more messages per connection
+smtp_accept_reserve              only reserve hosts if more connections
+smtp_active_hostname             host name to use in messages
+smtp_banner                      text for welcome banner
+smtp_check_spool_space           from SIZE on MAIL command
+smtp_connect_backlog             passed to TCP/IP stack
+smtp_enforce_sync                of SMTP command/responses
+smtp_etrn_command                what to run for ETRN
+smtp_etrn_serialize              only one at once
+smtp_load_reserve                only reserve hosts if this load
+smtp_max_unknown_commands        before dropping connection
+smtp_ratelimit_hosts             apply ratelimiting to these hosts
+smtp_ratelimit_mail              ratelimit for MAIL commands
+smtp_ratelimit_rcpt              ratelimit for RCPT commands
+smtp_receive_timeout             per command or data line
+smtp_reserve_hosts               these are the reserve hosts
+smtp_return_error_details        give detail on rejections
+
+
+14.18 SMTP extensions
+---------------------
+
+accept_8bitmime            advertise 8BITMIME
+auth_advertise_hosts       advertise AUTH to these hosts
+ignore_fromline_hosts      allow "From " from these hosts
+ignore_fromline_local      allow "From " from local SMTP
+pipelining_advertise_hosts advertise pipelining to these hosts
+prdr_enable                advertise PRDR to all hosts
+tls_advertise_hosts        advertise TLS to these hosts
+
+
+14.19 Processing messages
+-------------------------
+
+allow_domain_literals              recognize domain literal syntax
+allow_mx_to_ip                     allow MX to point to IP address
+allow_utf8_domains                 in addresses
+check_rfc2047_length               check length of RFC 2047 "encoded words"
+delivery_date_remove               from incoming messages
+envelope_to_remove                 from incoming messages
+extract_addresses_remove_arguments affects -t processing
+headers_charset                    default for translations
+qualify_domain                     default for senders
+qualify_recipient                  default for recipients
+return_path_remove                 from incoming messages
+strip_excess_angle_brackets        in addresses
+strip_trailing_dot                 at end of addresses
+untrusted_set_sender               untrusted can set envelope sender
+
+
+14.20 System filter
+-------------------
+
+system_filter                     locate system filter
+system_filter_directory_transport transport for delivery to a directory
+system_filter_file_transport      transport for delivery to a file
+system_filter_group               group for filter running
+system_filter_pipe_transport      transport for delivery to a pipe
+system_filter_reply_transport     transport for autoreply delivery
+system_filter_user                user for filter running
+
+
+14.21 Routing and delivery
+--------------------------
+
+disable_ipv6             do no IPv6 processing
+dns_again_means_nonexist for broken domains
+dns_check_names_pattern  pre-DNS syntax check
+dns_dnssec_ok            parameter for resolver
+dns_ipv4_lookup          only v4 lookup for these domains
+dns_retrans              parameter for resolver
+dns_retry                parameter for resolver
+dns_use_edns0            parameter for resolver
+hold_domains             hold delivery for these domains
+local_interfaces         for routing checks
+queue_domains            no immediate delivery for these
+queue_only               no immediate delivery at all
+queue_only_file          no immediate delivery if file exists
+queue_only_load          no immediate delivery if load is high
+queue_only_load_latch    don't re-evaluate load for each message
+queue_only_override      allow command line to override
+queue_run_in_order       order of arrival
+queue_run_max            of simultaneous queue runners
+queue_smtp_domains       no immediate SMTP delivery for these
+remote_max_parallel      parallel SMTP delivery per message
+remote_sort_domains      order of remote deliveries
+retry_data_expire        timeout for retry data
+retry_interval_max       safety net for retry rules
+
+
+14.22 Bounce and warning messages
+---------------------------------
+
+bounce_message_file          content of bounce
+bounce_message_text          content of bounce
+bounce_return_body           include body if returning message
+bounce_return_message        include original message in bounce
+bounce_return_size_limit     limit on returned message
+bounce_sender_authentication send authenticated sender with bounce
+dsn_from                     set From: contents in bounces
+errors_copy                  copy bounce messages
+errors_reply_to              Reply-to: in bounces
+delay_warning                time schedule
+delay_warning_condition      condition for warning messages
+ignore_bounce_errors_after   discard undeliverable bounces
+smtp_return_error_details    give detail on rejections
+warn_message_file            content of warning message
+
+
+14.23 Alphabetical list of main options
+---------------------------------------
+
+Those options that undergo string expansion before use are marked with *.
+
++---------------+---------+-------------+-------------+
+|accept_8bitmime|Use: main|Type: boolean|Default: true|
++---------------+---------+-------------+-------------+
+
+This option causes Exim to send 8BITMIME in its response to an SMTP EHLO
+command, and to accept the BODY= parameter on MAIL commands. However, though
+Exim is 8-bit clean, it is not a protocol converter, and it takes no steps to
+do anything special with messages received by this route.
+
+Historically Exim kept this option off by default, but the maintainers feel
+that in today's Internet, this causes more problems than it solves. It now
+defaults to true. A more detailed analysis of the issues is provided by Dan
+Bernstein:
+
+http://cr.yp.to/smtp/8bitmime.html
+
+To log received 8BITMIME status use
+
+log_selector = +8bitmime
+
++------------+---------+-------------+--------------+
+|acl_not_smtp|Use: main|Type: string*|Default: unset|
++------------+---------+-------------+--------------+
+
+This option defines the ACL that is run when a non-SMTP message has been read
+and is on the point of being accepted. See chapter 42 for further details.
+
++-----------------+---------+-------------+--------------+
+|acl_not_smtp_mime|Use: main|Type: string*|Default: unset|
++-----------------+---------+-------------+--------------+
+
+This option defines the ACL that is run for individual MIME parts of non-SMTP
+messages. It operates in exactly the same way as acl_smtp_mime operates for
+SMTP messages.
+
++------------------+---------+-------------+--------------+
+|acl_not_smtp_start|Use: main|Type: string*|Default: unset|
++------------------+---------+-------------+--------------+
+
+This option defines the ACL that is run before Exim starts reading a non-SMTP
+message. See chapter 42 for further details.
+
++-------------+---------+-------------+--------------+
+|acl_smtp_auth|Use: main|Type: string*|Default: unset|
++-------------+---------+-------------+--------------+
+
+This option defines the ACL that is run when an SMTP AUTH command is received.
+See chapter 42 for further details.
+
++----------------+---------+-------------+--------------+
+|acl_smtp_connect|Use: main|Type: string*|Default: unset|
++----------------+---------+-------------+--------------+
+
+This option defines the ACL that is run when an SMTP connection is received.
+See chapter 42 for further details.
+
++-------------+---------+-------------+--------------+
+|acl_smtp_data|Use: main|Type: string*|Default: unset|
++-------------+---------+-------------+--------------+
+
+This option defines the ACL that is run after an SMTP DATA command has been
+processed and the message itself has been received, but before the final
+acknowledgment is sent. See chapter 42 for further details.
+
++------------------+---------+-------------+--------------+
+|acl_smtp_data_prdr|Use: main|Type: string*|Default: unset|
++------------------+---------+-------------+--------------+
+
+This option defines the ACL that, if the PRDR feature has been negotiated, is
+run for each recipient after an SMTP DATA command has been processed and the
+message itself has been received, but before the acknowledgment is sent. See
+chapter 42 for further details.
+
++-------------+---------+-------------+--------------+
+|acl_smtp_etrn|Use: main|Type: string*|Default: unset|
++-------------+---------+-------------+--------------+
+
+This option defines the ACL that is run when an SMTP ETRN command is received.
+See chapter 42 for further details.
+
++-------------+---------+-------------+--------------+
+|acl_smtp_expn|Use: main|Type: string*|Default: unset|
++-------------+---------+-------------+--------------+
+
+This option defines the ACL that is run when an SMTP EXPN command is received.
+See chapter 42 for further details.
+
++-------------+---------+-------------+--------------+
+|acl_smtp_helo|Use: main|Type: string*|Default: unset|
++-------------+---------+-------------+--------------+
+
+This option defines the ACL that is run when an SMTP EHLO or HELO command is
+received. See chapter 42 for further details.
+
++-------------+---------+-------------+--------------+
+|acl_smtp_mail|Use: main|Type: string*|Default: unset|
++-------------+---------+-------------+--------------+
+
+This option defines the ACL that is run when an SMTP MAIL command is received.
+See chapter 42 for further details.
+
++-----------------+---------+-------------+--------------+
+|acl_smtp_mailauth|Use: main|Type: string*|Default: unset|
++-----------------+---------+-------------+--------------+
+
+This option defines the ACL that is run when there is an AUTH parameter on a
+MAIL command. See chapter 42 for details of ACLs, and chapter 33 for details of
+authentication.
+
++-------------+---------+-------------+--------------+
+|acl_smtp_mime|Use: main|Type: string*|Default: unset|
++-------------+---------+-------------+--------------+
+
+This option is available when Exim is built with the content-scanning
+extension. It defines the ACL that is run for each MIME part in a message. See
+section 43.4 for details.
+
++----------------+---------+-------------+--------------+
+|acl_smtp_predata|Use: main|Type: string*|Default: unset|
++----------------+---------+-------------+--------------+
+
+This option defines the ACL that is run when an SMTP DATA command is received,
+before the message itself is received. See chapter 42 for further details.
+
++-------------+---------+-------------+--------------+
+|acl_smtp_quit|Use: main|Type: string*|Default: unset|
++-------------+---------+-------------+--------------+
+
+This option defines the ACL that is run when an SMTP QUIT command is received.
+See chapter 42 for further details.
+
++-------------+---------+-------------+--------------+
+|acl_smtp_rcpt|Use: main|Type: string*|Default: unset|
++-------------+---------+-------------+--------------+
+
+This option defines the ACL that is run when an SMTP RCPT command is received.
+See chapter 42 for further details.
+
++-----------------+---------+-------------+--------------+
+|acl_smtp_starttls|Use: main|Type: string*|Default: unset|
++-----------------+---------+-------------+--------------+
+
+This option defines the ACL that is run when an SMTP STARTTLS command is
+received. See chapter 42 for further details.
+
++-------------+---------+-------------+--------------+
+|acl_smtp_vrfy|Use: main|Type: string*|Default: unset|
++-------------+---------+-------------+--------------+
+
+This option defines the ACL that is run when an SMTP VRFY command is received.
+See chapter 42 for further details.
+
++------------+---------+------------------+--------------+
+|admin_groups|Use: main|Type: string list*|Default: unset|
++------------+---------+------------------+--------------+
+
+This option is expanded just once, at the start of Exim's processing. If the
+current group or any of the supplementary groups of an Exim caller is in this
+colon-separated list, the caller has admin privileges. If all your system
+programmers are in a specific group, for example, you can give them all Exim
+admin privileges by putting that group in admin_groups. However, this does not
+permit them to read Exim's spool files (whose group owner is the Exim gid). To
+permit this, you have to add individuals to the Exim group.
+
++---------------------+---------+-------------+--------------+
+|allow_domain_literals|Use: main|Type: boolean|Default: false|
++---------------------+---------+-------------+--------------+
+
+If this option is set, the RFC 2822 domain literal format is permitted in email
+addresses. The option is not set by default, because the domain literal format
+is not normally required these days, and few people know about it. It has,
+however, been exploited by mail abusers.
+
+Unfortunately, it seems that some DNS black list maintainers are using this
+format to report black listing to postmasters. If you want to accept messages
+addressed to your hosts by IP address, you need to set allow_domain_literals
+true, and also to add "@[]" to the list of local domains (defined in the named
+domain list local_domains in the default configuration). This "magic string"
+matches the domain literal form of all the local host's IP addresses.
+
++--------------+---------+-------------+--------------+
+|allow_mx_to_ip|Use: main|Type: boolean|Default: false|
++--------------+---------+-------------+--------------+
+
+It appears that more and more DNS zone administrators are breaking the rules
+and putting domain names that look like IP addresses on the right hand side of
+MX records. Exim follows the rules and rejects this, giving an error message
+that explains the mis-configuration. However, some other MTAs support this
+practice, so to avoid "Why can't Exim do this?" complaints, allow_mx_to_ip
+exists, in order to enable this heinous activity. It is not recommended, except
+when you have no other choice.
+
++------------------+---------+-------------+--------------+
+|allow_utf8_domains|Use: main|Type: boolean|Default: false|
++------------------+---------+-------------+--------------+
+
+Lots of discussion is going on about internationalized domain names. One camp
+is strongly in favour of just using UTF-8 characters, and it seems that at
+least two other MTAs permit this. This option allows Exim users to experiment
+if they wish.
+
+If it is set true, Exim's domain parsing function allows valid UTF-8
+multicharacters to appear in domain name components, in addition to letters,
+digits, and hyphens. However, just setting this option is not enough; if you
+want to look up these domain names in the DNS, you must also adjust the value
+of dns_check_names_pattern to match the extended form. A suitable setting is:
+
+dns_check_names_pattern = (?i)^(?>(?(1)\.|())[a-z0-9\xc0-\xff]\
+  (?>[-a-z0-9\x80-\xff]*[a-z0-9\x80-\xbf])?)+$
+
+Alternatively, you can just disable this feature by setting
+
+dns_check_names_pattern =
+
+That is, set the option to an empty string so that no check is done.
+
++--------------------+---------+----------------+----------+
+|auth_advertise_hosts|Use: main|Type: host list*|Default: *|
++--------------------+---------+----------------+----------+
+
+If any server authentication mechanisms are configured, Exim advertises them in
+response to an EHLO command only if the calling host matches this list.
+Otherwise, Exim does not advertise AUTH. Exim does not accept AUTH commands
+from clients to which it has not advertised the availability of AUTH. The
+advertising of individual authentication mechanisms can be controlled by the
+use of the server_advertise_condition generic authenticator option on the
+individual authenticators. See chapter 33 for further details.
+
+Certain mail clients (for example, Netscape) require the user to provide a name
+and password for authentication if AUTH is advertised, even though it may not
+be needed (the host may accept messages from hosts on its local LAN without
+authentication, for example). The auth_advertise_hosts option can be used to
+make these clients more friendly by excluding them from the set of hosts to
+which Exim advertises AUTH.
+
+If you want to advertise the availability of AUTH only when the connection is
+encrypted using TLS, you can make use of the fact that the value of this option
+is expanded, with a setting like this:
+
+auth_advertise_hosts = ${if eq{$tls_in_cipher}{}{}{*}}
+
+If $tls_in_cipher is empty, the session is not encrypted, and the result of the
+expansion is empty, thus matching no hosts. Otherwise, the result of the
+expansion is *, which matches all hosts.
+
++---------+---------+----------+-----------+
+|auto_thaw|Use: main|Type: time|Default: 0s|
++---------+---------+----------+-----------+
+
+If this option is set to a time greater than zero, a queue runner will try a
+new delivery attempt on any frozen message, other than a bounce message, if
+this much time has passed since it was frozen. This may result in the message
+being re-frozen if nothing has changed since the last attempt. It is a way of
+saying "keep on trying, even though there are big problems".
+
+Note: This is an old option, which predates timeout_frozen_after and 
+ignore_bounce_errors_after. It is retained for compatibility, but it is not
+thought to be very useful any more, and its use should probably be avoided.
+
++----------+---------+------------+------------------+
+|av_scanner|Use: main|Type: string|Default: see below|
++----------+---------+------------+------------------+
+
+This option is available if Exim is built with the content-scanning extension.
+It specifies which anti-virus scanner to use. The default value is:
+
+sophie:/var/run/sophie
+
+If the value of av_scanner starts with a dollar character, it is expanded
+before use. See section 43.1 for further details.
+
++----------+---------+------------+--------------+
+|bi_command|Use: main|Type: string|Default: unset|
++----------+---------+------------+--------------+
+
+This option supplies the name of a command that is run when Exim is called with
+the -bi option (see chapter 5). The string value is just the command name, it
+is not a complete command line. If an argument is required, it must come from
+the -oA command line option.
+
++-------------------+---------+------------+--------------+
+|bounce_message_file|Use: main|Type: string|Default: unset|
++-------------------+---------+------------+--------------+
+
+This option defines a template file containing paragraphs of text to be used
+for constructing bounce messages. Details of the file's contents are given in
+chapter 48. See also warn_message_file.
+
++-------------------+---------+------------+--------------+
+|bounce_message_text|Use: main|Type: string|Default: unset|
++-------------------+---------+------------+--------------+
+
+When this option is set, its contents are included in the default bounce
+message immediately after "This message was created automatically by mail
+delivery software." It is not used if bounce_message_file is set.
+
++------------------+---------+-------------+-------------+
+|bounce_return_body|Use: main|Type: boolean|Default: true|
++------------------+---------+-------------+-------------+
+
+This option controls whether the body of an incoming message is included in a
+bounce message when bounce_return_message is true. The default setting causes
+the entire message, both header and body, to be returned (subject to the value
+of bounce_return_size_limit). If this option is false, only the message header
+is included. In the case of a non-SMTP message containing an error that is
+detected during reception, only those header lines preceding the point at which
+the error was detected are returned.
+
++---------------------+---------+-------------+-------------+
+|bounce_return_message|Use: main|Type: boolean|Default: true|
++---------------------+---------+-------------+-------------+
+
+If this option is set false, none of the original message is included in bounce
+messages generated by Exim. See also bounce_return_size_limit and 
+bounce_return_body.
+
++------------------------+---------+-------------+-------------+
+|bounce_return_size_limit|Use: main|Type: integer|Default: 100K|
++------------------------+---------+-------------+-------------+
+
+This option sets a limit in bytes on the size of messages that are returned to
+senders as part of bounce messages when bounce_return_message is true. The
+limit should be less than the value of the global message_size_limit and of any
+message_size_limit settings on transports, to allow for the bounce text that
+Exim generates. If this option is set to zero there is no limit.
+
+When the body of any message that is to be included in a bounce message is
+greater than the limit, it is truncated, and a comment pointing this out is
+added at the top. The actual cutoff may be greater than the value given, owing
+to the use of buffering for transferring the message in chunks (typically 8K in
+size). The idea is to save bandwidth on those undeliverable 15-megabyte
+messages.
+
++----------------------------+---------+------------+--------------+
+|bounce_sender_authentication|Use: main|Type: string|Default: unset|
++----------------------------+---------+------------+--------------+
+
+This option provides an authenticated sender address that is sent with any
+bounce messages generated by Exim that are sent over an authenticated SMTP
+connection. A typical setting might be:
+
+bounce_sender_authentication = mailer-daemon@my.domain.example
+
+which would cause bounce messages to be sent using the SMTP command:
+
+MAIL FROM:<> AUTH=mailer-daemon@my.domain.example
+
+The value of bounce_sender_authentication must always be a complete email
+address.
+
++------------------------------+---------+----------+-----------+
+|callout_domain_negative_expire|Use: main|Type: time|Default: 3h|
++------------------------------+---------+----------+-----------+
+
+This option specifies the expiry time for negative callout cache data for a
+domain. See section 42.45 for details of callout verification, and section
+42.47 for details of the caching.
+
++------------------------------+---------+----------+-----------+
+|callout_domain_positive_expire|Use: main|Type: time|Default: 7d|
++------------------------------+---------+----------+-----------+
+
+This option specifies the expiry time for positive callout cache data for a
+domain. See section 42.45 for details of callout verification, and section
+42.47 for details of the caching.
+
++-----------------------+---------+----------+-----------+
+|callout_negative_expire|Use: main|Type: time|Default: 2h|
++-----------------------+---------+----------+-----------+
+
+This option specifies the expiry time for negative callout cache data for an
+address. See section 42.45 for details of callout verification, and section
+42.47 for details of the caching.
+
++-----------------------+---------+----------+------------+
+|callout_positive_expire|Use: main|Type: time|Default: 24h|
++-----------------------+---------+----------+------------+
+
+This option specifies the expiry time for positive callout cache data for an
+address. See section 42.45 for details of callout verification, and section
+42.47 for details of the caching.
+
++-------------------------+---------+-------------+------------------+
+|callout_random_local_part|Use: main|Type: string*|Default: see below|
++-------------------------+---------+-------------+------------------+
+
+This option defines the "random" local part that can be used as part of callout
+verification. The default value is
+
+$primary_hostname-$tod_epoch-testing
+
+See section 42.46 for details of how this value is used.
+
++----------------+---------+-------------+----------+
+|check_log_inodes|Use: main|Type: integer|Default: 0|
++----------------+---------+-------------+----------+
+
+See check_spool_space below.
+
++---------------+---------+-------------+----------+
+|check_log_space|Use: main|Type: integer|Default: 0|
++---------------+---------+-------------+----------+
+
+See check_spool_space below.
+
++--------------------+---------+-------------+-------------+
+|check_rfc2047_length|Use: main|Type: boolean|Default: true|
++--------------------+---------+-------------+-------------+
+
+RFC 2047 defines a way of encoding non-ASCII characters in headers using a
+system of "encoded words". The RFC specifies a maximum length for an encoded
+word; strings to be encoded that exceed this length are supposed to use
+multiple encoded words. By default, Exim does not recognize encoded words that
+exceed the maximum length. However, it seems that some software, in violation
+of the RFC, generates overlong encoded words. If check_rfc2047_length is set
+false, Exim recognizes encoded words of any length.
+
++------------------+---------+-------------+----------+
+|check_spool_inodes|Use: main|Type: integer|Default: 0|
++------------------+---------+-------------+----------+
+
+See check_spool_space below.
+
++-----------------+---------+-------------+----------+
+|check_spool_space|Use: main|Type: integer|Default: 0|
++-----------------+---------+-------------+----------+
+
+The four check_... options allow for checking of disk resources before a
+message is accepted.
+
+When any of these options are set, they apply to all incoming messages. If you
+want to apply different checks to different kinds of message, you can do so by
+testing the variables $log_inodes, $log_space, $spool_inodes, and $spool_space
+in an ACL with appropriate additional conditions.
+
+check_spool_space and check_spool_inodes check the spool partition if either
+value is greater than zero, for example:
+
+check_spool_space = 10M
+check_spool_inodes = 100
+
+The spool partition is the one that contains the directory defined by
+SPOOL_DIRECTORY in Local/Makefile. It is used for holding messages in transit.
+
+check_log_space and check_log_inodes check the partition in which log files are
+written if either is greater than zero. These should be set only if 
+log_file_path and spool_directory refer to different partitions.
+
+If there is less space or fewer inodes than requested, Exim refuses to accept
+incoming mail. In the case of SMTP input this is done by giving a 452 temporary
+error response to the MAIL command. If ESMTP is in use and there was a SIZE
+parameter on the MAIL command, its value is added to the check_spool_space
+value, and the check is performed even if check_spool_space is zero, unless 
+no_smtp_check_spool_space is set.
+
+The values for check_spool_space and check_log_space are held as a number of
+kilobytes. If a non-multiple of 1024 is specified, it is rounded up.
+
+For non-SMTP input and for batched SMTP input, the test is done at start-up; on
+failure a message is written to stderr and Exim exits with a non-zero code, as
+it obviously cannot send an error message of any kind.
+
++-----------------+---------+------------+---------------+
+|daemon_smtp_ports|Use: main|Type: string|Default: "smtp"|
++-----------------+---------+------------+---------------+
+
+This option specifies one or more default SMTP ports on which the Exim daemon
+listens. See chapter 13 for details of how it is used. For backward
+compatibility, daemon_smtp_port (singular) is a synonym.
+
++----------------------+---------+-------------+----------+
+|daemon_startup_retries|Use: main|Type: integer|Default: 9|
++----------------------+---------+-------------+----------+
+
+This option, along with daemon_startup_sleep, controls the retrying done by the
+daemon at startup when it cannot immediately bind a listening socket (typically
+because the socket is already in use): daemon_startup_retries defines the
+number of retries after the first failure, and daemon_startup_sleep defines the
+length of time to wait between retries.
+
++--------------------+---------+----------+------------+
+|daemon_startup_sleep|Use: main|Type: time|Default: 30s|
++--------------------+---------+----------+------------+
+
+See daemon_startup_retries.
+
++-------------+---------+---------------+------------+
+|delay_warning|Use: main|Type: time list|Default: 24h|
++-------------+---------+---------------+------------+
+
+When a message is delayed, Exim sends a warning message to the sender at
+intervals specified by this option. The data is a colon-separated list of times
+after which to send warning messages. If the value of the option is an empty
+string or a zero time, no warnings are sent. Up to 10 times may be given. If a
+message has been on the queue for longer than the last time, the last interval
+between the times is used to compute subsequent warning times. For example,
+with
+
+delay_warning = 4h:8h:24h
+
+the first message is sent after 4 hours, the second after 8 hours, and the
+third one after 24 hours. After that, messages are sent every 16 hours, because
+that is the interval between the last two times on the list. If you set just
+one time, it specifies the repeat interval. For example, with:
+
+delay_warning = 6h
+
+messages are repeated every six hours. To stop warnings after a given time, set
+a very large time at the end of the list. For example:
+
+delay_warning = 2h:12h:99d
+
+Note that the option is only evaluated at the time a delivery attempt fails,
+which depends on retry and queue-runner configuration. Typically retries will
+be configured more frequently than warning messages.
+
++-----------------------+---------+-------------+------------------+
+|delay_warning_condition|Use: main|Type: string*|Default: see below|
++-----------------------+---------+-------------+------------------+
+
+The string is expanded at the time a warning message might be sent. If all the
+deferred addresses have the same domain, it is set in $domain during the
+expansion. Otherwise $domain is empty. If the result of the expansion is a
+forced failure, an empty string, or a string matching any of "0", "no" or
+"false" (the comparison being done caselessly) then the warning message is not
+sent. The default is:
+
+delay_warning_condition = ${if or {\
+  { !eq{$h_list-id:$h_list-post:$h_list-subscribe:}{} }\
+  { match{$h_precedence:}{(?i)bulk|list|junk} }\
+  { match{$h_auto-submitted:}{(?i)auto-generated|auto-replied} }\
+  } {no}{yes}}
+
+This suppresses the sending of warnings for messages that contain List-ID:, 
+List-Post:, or List-Subscribe: headers, or have "bulk", "list" or "junk" in a 
+Precedence: header, or have "auto-generated" or "auto-replied" in an 
+Auto-Submitted: header.
+
++----------------------+---------+-------------+--------------+
+|deliver_drop_privilege|Use: main|Type: boolean|Default: false|
++----------------------+---------+-------------+--------------+
+
+If this option is set true, Exim drops its root privilege at the start of a
+delivery process, and runs as the Exim user throughout. This severely restricts
+the kinds of local delivery that are possible, but is viable in certain types
+of configuration. There is a discussion about the use of root privilege in
+chapter 54.
+
++----------------------+---------+-----------------+--------------+
+|deliver_queue_load_max|Use: main|Type: fixed-point|Default: unset|
++----------------------+---------+-----------------+--------------+
+
+When this option is set, a queue run is abandoned if the system load average
+becomes greater than the value of the option. The option has no effect on
+ancient operating systems on which Exim cannot determine the load average. See
+also queue_only_load and smtp_load_reserve.
+
++--------------------+---------+-------------+-------------+
+|delivery_date_remove|Use: main|Type: boolean|Default: true|
++--------------------+---------+-------------+-------------+
+
+Exim's transports have an option for adding a Delivery-date: header to a
+message when it is delivered, in exactly the same way as Return-path: is
+handled. Delivery-date: records the actual time of delivery. Such headers
+should not be present in incoming messages, and this option causes them to be
+removed at the time the message is received, to avoid any problems that might
+occur when a delivered message is subsequently sent on to some other recipient.
+
++-------------+---------+-------------+--------------+
+|disable_fsync|Use: main|Type: boolean|Default: false|
++-------------+---------+-------------+--------------+
+
+This option is available only if Exim was built with the compile-time option
+ENABLE_DISABLE_FSYNC. When this is not set, a reference to disable_fsync in a
+runtime configuration generates an "unknown option" error. You should not build
+Exim with ENABLE_DISABLE_FSYNC or set disable_fsync unless you really, really,
+really understand what you are doing. No pre-compiled distributions of Exim
+should ever make this option available.
+
+When disable_fsync is set true, Exim no longer calls fsync() to force updated
+files' data to be written to disc before continuing. Unexpected events such as
+crashes and power outages may cause data to be lost or scrambled. Here be
+Dragons. Beware.
+
++------------+---------+-------------+--------------+
+|disable_ipv6|Use: main|Type: boolean|Default: false|
++------------+---------+-------------+--------------+
+
+If this option is set true, even if the Exim binary has IPv6 support, no IPv6
+activities take place. AAAA records are never looked up, and any IPv6 addresses
+that are listed in local_interfaces, data for the manualroute router, etc. are
+ignored. If IP literals are enabled, the ipliteral router declines to handle
+IPv6 literal addresses.
+
++------------------------+---------+------------------+--------------+
+|dns_again_means_nonexist|Use: main|Type: domain list*|Default: unset|
++------------------------+---------+------------------+--------------+
+
+DNS lookups give a "try again" response for the DNS errors "non-authoritative
+host not found" and "SERVERFAIL". This can cause Exim to keep trying to deliver
+a message, or to give repeated temporary errors to incoming mail. Sometimes the
+effect is caused by a badly set up name server and may persist for a long time.
+If a domain which exhibits this problem matches anything in 
+dns_again_means_nonexist, it is treated as if it did not exist. This option
+should be used with care. You can make it apply to reverse lookups by a setting
+such as this:
+
+dns_again_means_nonexist = *.in-addr.arpa
+
+This option applies to all DNS lookups that Exim does. It also applies when the
+gethostbyname() or getipnodebyname() functions give temporary errors, since
+these are most likely to be caused by DNS lookup problems. The dnslookup router
+has some options of its own for controlling what happens when lookups for MX or
+SRV records give temporary errors. These more specific options are applied
+after this global option.
+
++-----------------------+---------+------------+------------------+
+|dns_check_names_pattern|Use: main|Type: string|Default: see below|
++-----------------------+---------+------------+------------------+
+
+When this option is set to a non-empty string, it causes Exim to check domain
+names for characters that are not allowed in host names before handing them to
+the DNS resolver, because some resolvers give temporary errors for names that
+contain unusual characters. If a domain name contains any unwanted characters,
+a "not found" result is forced, and the resolver is not called. The check is
+done by matching the domain name against a regular expression, which is the
+value of this option. The default pattern is
+
+dns_check_names_pattern = \
+  (?i)^(?>(?(1)\.|())[^\W_](?>[a-z0-9/-]*[^\W_])?)+$
+
+which permits only letters, digits, slashes, and hyphens in components, but
+they must start and end with a letter or digit. Slashes are not, in fact,
+permitted in host names, but they are found in certain NS records (which can be
+accessed in Exim by using a dnsdb lookup). If you set allow_utf8_domains, you
+must modify this pattern, or set the option to an empty string.
+
++--------------------+---------+-------------+----------+
+|dns_csa_search_limit|Use: main|Type: integer|Default: 5|
++--------------------+---------+-------------+----------+
+
+This option controls the depth of parental searching for CSA SRV records in the
+DNS, as described in more detail in section 42.50.
+
++-------------------+---------+-------------+-------------+
+|dns_csa_use_reverse|Use: main|Type: boolean|Default: true|
++-------------------+---------+-------------+-------------+
+
+This option controls whether or not an IP address, given as a CSA domain, is
+reversed and looked up in the reverse DNS, as described in more detail in
+section 42.50.
+
++-------------+---------+-------------+-----------+
+|dns_dnssec_ok|Use: main|Type: integer|Default: -1|
++-------------+---------+-------------+-----------+
+
+If this option is set to a non-negative number then Exim will initialise the
+DNS resolver library to either use or not use DNSSEC, overriding the system
+default. A value of 0 coerces DNSSEC off, a value of 1 coerces DNSSEC on.
+
+If the resolver library does not support DNSSEC then this option has no effect.
+
++---------------+---------+------------------+--------------+
+|dns_ipv4_lookup|Use: main|Type: domain list*|Default: unset|
++---------------+---------+------------------+--------------+
+
+When Exim is compiled with IPv6 support and disable_ipv6 is not set, it looks
+for IPv6 address records (AAAA records) as well as IPv4 address records (A
+records) when trying to find IP addresses for hosts, unless the host's domain
+matches this list.
+
+This is a fudge to help with name servers that give big delays or otherwise do
+not work for the AAAA record type. In due course, when the world's name servers
+have all been upgraded, there should be no need for this option.
+
++-----------+---------+----------+-----------+
+|dns_retrans|Use: main|Type: time|Default: 0s|
++-----------+---------+----------+-----------+
+
+The options dns_retrans and dns_retry can be used to set the retransmission and
+retry parameters for DNS lookups. Values of zero (the defaults) leave the
+system default settings unchanged. The first value is the time between retries,
+and the second is the number of retries. It isn't totally clear exactly how
+these settings affect the total time a DNS lookup may take. I haven't found any
+documentation about timeouts on DNS lookups; these parameter values are
+available in the external resolver interface structure, but nowhere does it
+seem to describe how they are used or what you might want to set in them.
+
++---------+---------+-------------+----------+
+|dns_retry|Use: main|Type: integer|Default: 0|
++---------+---------+-------------+----------+
+
+See dns_retrans above.
+
++-------------+---------+-------------+-----------+
+|dns_use_edns0|Use: main|Type: integer|Default: -1|
++-------------+---------+-------------+-----------+
+
+If this option is set to a non-negative number then Exim will initialise the
+DNS resolver library to either use or not use EDNS0 extensions, overriding the
+system default. A value of 0 coerces EDNS0 off, a value of 1 coerces EDNS0 on.
+
+If the resolver library does not support EDNS0 then this option has no effect.
+
++-------+---------+-------------+--------------+
+|drop_cr|Use: main|Type: boolean|Default: false|
++-------+---------+-------------+--------------+
+
+This is an obsolete option that is now a no-op. It used to affect the way Exim
+handled CR and LF characters in incoming messages. What happens now is
+described in section 46.2.
+
++--------+---------+-------------+------------------+
+|dsn_from|Use: main|Type: string*|Default: see below|
++--------+---------+-------------+------------------+
+
+This option can be used to vary the contents of From: header lines in bounces
+and other automatically generated messages ("Delivery Status Notifications" -
+hence the name of the option). The default setting is:
+
+dsn_from = Mail Delivery System <Mailer-Daemon@$qualify_domain>
+
+The value is expanded every time it is needed. If the expansion fails, a panic
+is logged, and the default value is used.
+
++------------------+---------+-------------+-------------+
+|envelope_to_remove|Use: main|Type: boolean|Default: true|
++------------------+---------+-------------+-------------+
+
+Exim's transports have an option for adding an Envelope-to: header to a message
+when it is delivered, in exactly the same way as Return-path: is handled. 
+Envelope-to: records the original recipient address from the messages's
+envelope that caused the delivery to happen. Such headers should not be present
+in incoming messages, and this option causes them to be removed at the time the
+message is received, to avoid any problems that might occur when a delivered
+message is subsequently sent on to some other recipient.
+
++-----------+---------+------------------+--------------+
+|errors_copy|Use: main|Type: string list*|Default: unset|
++-----------+---------+------------------+--------------+
+
+Setting this option causes Exim to send bcc copies of bounce messages that it
+generates to other addresses. Note: This does not apply to bounce messages
+coming from elsewhere. The value of the option is a colon-separated list of
+items. Each item consists of a pattern, terminated by white space, followed by
+a comma-separated list of email addresses. If a pattern contains spaces, it
+must be enclosed in double quotes.
+
+Each pattern is processed in the same way as a single item in an address list
+(see section 10.19). When a pattern matches the recipient of the bounce
+message, the message is copied to the addresses on the list. The items are
+scanned in order, and once a matching one is found, no further items are
+examined. For example:
+
+errors_copy = spqr@mydomain   postmaster@mydomain.example :\
+              rqps@mydomain   hostmaster@mydomain.example,\
+                              postmaster@mydomain.example
+
+The address list is expanded before use. The expansion variables $local_part
+and $domain are set from the original recipient of the error message, and if
+there was any wildcard matching in the pattern, the expansion variables $0, $1,
+etc. are set in the normal way.
+
++---------------+---------+------------+--------------+
+|errors_reply_to|Use: main|Type: string|Default: unset|
++---------------+---------+------------+--------------+
+
+By default, Exim's bounce and delivery warning messages contain the header line
+
+From: Mail Delivery System <Mailer-Daemon@qualify-domain>
+
+where qualify-domain is the value of the qualify_domain option. A warning
+message that is generated by the quota_warn_message option in an appendfile
+transport may contain its own From: header line that overrides the default.
+
+Experience shows that people reply to bounce messages. If the errors_reply_to
+option is set, a Reply-To: header is added to bounce and warning messages. For
+example:
+
+errors_reply_to = postmaster@my.domain.example
+
+The value of the option is not expanded. It must specify a valid RFC 2822
+address. However, if a warning message that is generated by the 
+quota_warn_message option in an appendfile transport contain its own Reply-To:
+header line, the value of the errors_reply_to option is not used.
+
++----------+---------+------------+--------------------------------+
+|exim_group|Use: main|Type: string|Default: compile-time configured|
++----------+---------+------------+--------------------------------+
+
+This option changes the gid under which Exim runs when it gives up root
+privilege. The default value is compiled into the binary. The value of this
+option is used only when exim_user is also set. Unless it consists entirely of
+digits, the string is looked up using getgrnam(), and failure causes a
+configuration error. See chapter 54 for a discussion of security issues.
+
++---------+---------+------------+------------------+
+|exim_path|Use: main|Type: string|Default: see below|
++---------+---------+------------+------------------+
+
+This option specifies the path name of the Exim binary, which is used when Exim
+needs to re-exec itself. The default is set up to point to the file exim in the
+directory configured at compile time by the BIN_DIRECTORY setting. It is
+necessary to change exim_path if, exceptionally, Exim is run from some other
+place. Warning: Do not use a macro to define the value of this option, because
+you will break those Exim utilities that scan the configuration file to find
+where the binary is. (They then use the -bP option to extract option settings
+such as the value of spool_directory.)
+
++---------+---------+------------+--------------------------------+
+|exim_user|Use: main|Type: string|Default: compile-time configured|
++---------+---------+------------+--------------------------------+
+
+This option changes the uid under which Exim runs when it gives up root
+privilege. The default value is compiled into the binary. Ownership of the run
+time configuration file and the use of the -C and -D command line options is
+checked against the values in the binary, not what is set here.
+
+Unless it consists entirely of digits, the string is looked up using getpwnam()
+, and failure causes a configuration error. If exim_group is not also supplied,
+the gid is taken from the result of getpwnam() if it is used. See chapter 54
+for a discussion of security issues.
+
++----------------------+---------+-----------------+--------------+
+|extra_local_interfaces|Use: main|Type: string list|Default: unset|
++----------------------+---------+-----------------+--------------+
+
+This option defines network interfaces that are to be considered local when
+routing, but which are not used for listening by the daemon. See section 13.8
+for details.
+
++-------------------------------------+---------+-------------+-------------+
+|extract_addresses_remove_   arguments|Use: main|Type: boolean|Default: true|
++-------------------------------------+---------+-------------+-------------+
+
+According to some Sendmail documentation (Sun, IRIX, HP-UX), if any addresses
+are present on the command line when the -t option is used to build an envelope
+from a message's To:, Cc: and Bcc: headers, the command line addresses are
+removed from the recipients list. This is also how Smail behaves. However,
+other Sendmail documentation (the O'Reilly book) states that command line
+addresses are added to those obtained from the header lines. When 
+extract_addresses_remove_arguments is true (the default), Exim subtracts
+argument headers. If it is set false, Exim adds rather than removes argument
+addresses.
+
++----------------+---------+-------------+----------+
+|finduser_retries|Use: main|Type: integer|Default: 0|
++----------------+---------+-------------+----------+
+
+On systems running NIS or other schemes in which user and group information is
+distributed from a remote system, there can be times when getpwnam() and
+related functions fail, even when given valid data, because things time out.
+Unfortunately these failures cannot be distinguished from genuine "not found"
+errors. If finduser_retries is set greater than zero, Exim will try that many
+extra times to find a user or a group, waiting for one second between retries.
+
+You should not set this option greater than zero if your user information is in
+a traditional /etc/passwd file, because it will cause Exim needlessly to search
+the file multiple times for non-existent users, and also cause delay.
+
++-----------+---------+----------------------------------+--------------+
+|freeze_tell|Use: main|Type: string list, comma separated|Default: unset|
++-----------+---------+----------------------------------+--------------+
+
+On encountering certain errors, or when configured to do so in a system filter,
+ACL, or special router, Exim freezes a message. This means that no further
+delivery attempts take place until an administrator thaws the message, or the 
+auto_thaw, ignore_bounce_errors_after, or timeout_frozen_after feature cause it
+to be processed. If freeze_tell is set, Exim generates a warning message
+whenever it freezes something, unless the message it is freezing is a
+locally-generated bounce message. (Without this exception there is the
+possibility of looping.) The warning message is sent to the addresses supplied
+as the comma-separated value of this option. If several of the message's
+addresses cause freezing, only a single message is sent. If the freezing was
+automatic, the reason(s) for freezing can be found in the message log. If you
+configure freezing in a filter or ACL, you must arrange for any logging that
+you require.
+
++----------+---------+-------------+--------------+
+|gecos_name|Use: main|Type: string*|Default: unset|
++----------+---------+-------------+--------------+
+
+Some operating systems, notably HP-UX, use the "gecos" field in the system
+password file to hold other information in addition to users' real names. Exim
+looks up this field for use when it is creating Sender: or From: headers. If
+either gecos_pattern or gecos_name are unset, the contents of the field are
+used unchanged, except that, if an ampersand is encountered, it is replaced by
+the user's login name with the first character forced to upper case, since this
+is a convention that is observed on many systems.
+
+When these options are set, gecos_pattern is treated as a regular expression
+that is to be applied to the field (again with & replaced by the login name),
+and if it matches, gecos_name is expanded and used as the user's name.
+
+Numeric variables such as $1, $2, etc. can be used in the expansion to pick up
+sub-fields that were matched by the pattern. In HP-UX, where the user's name
+terminates at the first comma, the following can be used:
+
+gecos_pattern = ([^,]*)
+gecos_name = $1
+
++-------------+---------+------------+--------------+
+|gecos_pattern|Use: main|Type: string|Default: unset|
++-------------+---------+------------+--------------+
+
+See gecos_name above.
+
++------------------+---------+-------------+--------------+
+|gnutls_compat_mode|Use: main|Type: boolean|Default: unset|
++------------------+---------+-------------+--------------+
+
+This option controls whether GnuTLS is used in compatibility mode in an Exim
+server. This reduces security slightly, but improves interworking with older
+implementations of TLS.
+
+option gnutls_allow_auto_pkcs11 main boolean unset This option will let GnuTLS
+(2.12.0 or later) autoload PKCS11 modules with the p11-kit configuration files
+in /etc/pkcs11/modules/.
+
+See http://www.gnutls.org/manual/gnutls.html#Smart-cards-and-HSMs for
+documentation.
+
++---------------+---------+------------+------------------+
+|headers_charset|Use: main|Type: string|Default: see below|
++---------------+---------+------------+------------------+
+
+This option sets a default character set for translating from encoded MIME
+"words" in header lines, when referenced by an $h_xxx expansion item. The
+default is the value of HEADERS_CHARSET in Local/Makefile. The ultimate default
+is ISO-8859-1. For more details see the description of header insertions in
+section 11.5.
+
++--------------+---------+-------------+------------------+
+|header_maxsize|Use: main|Type: integer|Default: see below|
++--------------+---------+-------------+------------------+
+
+This option controls the overall maximum size of a message's header section.
+The default is the value of HEADER_MAXSIZE in Local/Makefile; the default for
+that is 1M. Messages with larger header sections are rejected.
+
++-------------------+---------+-------------+----------+
+|header_line_maxsize|Use: main|Type: integer|Default: 0|
++-------------------+---------+-------------+----------+
+
+This option limits the length of any individual header line in a message, after
+all the continuations have been joined together. Messages with individual
+header lines that are longer than the limit are rejected. The default value of
+zero means "no limit".
+
++----------------------+---------+----------------+--------------+
+|helo_accept_junk_hosts|Use: main|Type: host list*|Default: unset|
++----------------------+---------+----------------+--------------+
+
+Exim checks the syntax of HELO and EHLO commands for incoming SMTP mail, and
+gives an error response for invalid data. Unfortunately, there are some SMTP
+clients that send syntactic junk. They can be accommodated by setting this
+option. Note that this is a syntax check only. See helo_verify_hosts if you
+want to do semantic checking. See also helo_allow_chars for a way of extending
+the permitted character set.
+
++----------------+---------+------------+--------------+
+|helo_allow_chars|Use: main|Type: string|Default: unset|
++----------------+---------+------------+--------------+
+
+This option can be set to a string of rogue characters that are permitted in
+all EHLO and HELO names in addition to the standard letters, digits, hyphens,
+and dots. If you really must allow underscores, you can set
+
+helo_allow_chars = _
+
+Note that the value is one string, not a list.
+
++-------------------+---------+------------------+----------------+
+|helo_lookup_domains|Use: main|Type: domain list*|Default: "@:@[]"|
++-------------------+---------+------------------+----------------+
+
+If the domain given by a client in a HELO or EHLO command matches this list, a
+reverse lookup is done in order to establish the host's true name. The default
+forces a lookup if the client host gives the server's name or any of its IP
+addresses (in brackets), something that broken clients have been seen to do.
+
++---------------------+---------+----------------+--------------+
+|helo_try_verify_hosts|Use: main|Type: host list*|Default: unset|
++---------------------+---------+----------------+--------------+
+
+By default, Exim just checks the syntax of HELO and EHLO commands (see 
+helo_accept_junk_hosts and helo_allow_chars). However, some sites like to do
+more extensive checking of the data supplied by these commands. The ACL
+condition "verify = helo" is provided to make this possible. Formerly, it was
+necessary also to set this option (helo_try_verify_hosts) to force the check to
+occur. From release 4.53 onwards, this is no longer necessary. If the check has
+not been done before "verify = helo" is encountered, it is done at that time.
+Consequently, this option is obsolete. Its specification is retained here for
+backwards compatibility.
+
+When an EHLO or HELO command is received, if the calling host matches 
+helo_try_verify_hosts, Exim checks that the host name given in the HELO or EHLO
+command either:
+
+  * is an IP literal matching the calling address of the host, or
+
+  * matches the host name that Exim obtains by doing a reverse lookup of the
+    calling host address, or
+
+  * when looked up using gethostbyname() (or getipnodebyname() when available)
+    yields the calling host address.
+
+However, the EHLO or HELO command is not rejected if any of the checks fail.
+Processing continues, but the result of the check is remembered, and can be
+detected later in an ACL by the "verify = helo" condition.
+
++-----------------+---------+----------------+--------------+
+|helo_verify_hosts|Use: main|Type: host list*|Default: unset|
++-----------------+---------+----------------+--------------+
+
+Like helo_try_verify_hosts, this option is obsolete, and retained only for
+backwards compatibility. For hosts that match this option, Exim checks the host
+name given in the HELO or EHLO in the same way as for helo_try_verify_hosts. If
+the check fails, the HELO or EHLO command is rejected with a 550 error, and
+entries are written to the main and reject logs. If a MAIL command is received
+before EHLO or HELO, it is rejected with a 503 error.
+
++------------+---------+------------------+--------------+
+|hold_domains|Use: main|Type: domain list*|Default: unset|
++------------+---------+------------------+--------------+
+
+This option allows mail for particular domains to be held on the queue
+manually. The option is overridden if a message delivery is forced with the -M,
+-qf, -Rf or -Sf options, and also while testing or verifying addresses using 
+-bt or -bv. Otherwise, if a domain matches an item in hold_domains, no routing
+or delivery for that address is done, and it is deferred every time the message
+is looked at.
+
+This option is intended as a temporary operational measure for delaying the
+delivery of mail while some problem is being sorted out, or some new
+configuration tested. If you just want to delay the processing of some domains
+until a queue run occurs, you should use queue_domains or queue_smtp_domains,
+not hold_domains.
+
+A setting of hold_domains does not override Exim's code for removing messages
+from the queue if they have been there longer than the longest retry time in
+any retry rule. If you want to hold messages for longer than the normal retry
+times, insert a dummy retry rule with a long retry time.
+
++-----------+---------+----------------+--------------+
+|host_lookup|Use: main|Type: host list*|Default: unset|
++-----------+---------+----------------+--------------+
+
+Exim does not look up the name of a calling host from its IP address unless it
+is required to compare against some host list, or the host matches 
+helo_try_verify_hosts or helo_verify_hosts, or the host matches this option
+(which normally contains IP addresses rather than host names). The default
+configuration file contains
+
+host_lookup = *
+
+which causes a lookup to happen for all hosts. If the expense of these lookups
+is felt to be too great, the setting can be changed or removed.
+
+After a successful reverse lookup, Exim does a forward lookup on the name it
+has obtained, to verify that it yields the IP address that it started with. If
+this check fails, Exim behaves as if the name lookup failed.
+
+After any kind of failure, the host name (in $sender_host_name) remains unset,
+and $host_lookup_failed is set to the string "1". See also 
+dns_again_means_nonexist, helo_lookup_domains, and "verify =
+reverse_host_lookup" in ACLs.
+
++-----------------+---------+-----------------+-----------------------+
+|host_lookup_order|Use: main|Type: string list|Default: "bydns:byaddr"|
++-----------------+---------+-----------------+-----------------------+
+
+This option specifies the order of different lookup methods when Exim is trying
+to find a host name from an IP address. The default is to do a DNS lookup
+first, and then to try a local lookup (using gethostbyaddr() or equivalent) if
+that fails. You can change the order of these lookups, or omit one entirely, if
+you want.
+
+Warning: The "byaddr" method does not always yield aliases when there are
+multiple PTR records in the DNS and the IP address is not listed in /etc/hosts.
+Different operating systems give different results in this case. That is why
+the default tries a DNS lookup first.
+
++----------------------+---------+----------------+--------------+
+|host_reject_connection|Use: main|Type: host list*|Default: unset|
++----------------------+---------+----------------+--------------+
+
+If this option is set, incoming SMTP calls from the hosts listed are rejected
+as soon as the connection is made. This option is obsolete, and retained only
+for backward compatibility, because nowadays the ACL specified by 
+acl_smtp_connect can also reject incoming connections immediately.
+
+The ability to give an immediate rejection (either by this option or using an
+ACL) is provided for use in unusual cases. Many hosts will just try again,
+sometimes without much delay. Normally, it is better to use an ACL to reject
+incoming messages at a later stage, such as after RCPT commands. See chapter 42
+.
+
++----------------------+---------+----------------+--------------+
+|hosts_connection_nolog|Use: main|Type: host list*|Default: unset|
++----------------------+---------+----------------+--------------+
+
+This option defines a list of hosts for which connection logging does not
+happen, even though the smtp_connection log selector is set. For example, you
+might want not to log SMTP connections from local processes, or from 127.0.0.1,
+or from your local LAN. This option is consulted in the main loop of the
+daemon; you should therefore strive to restrict its value to a short inline
+list of IP addresses and networks. To disable logging SMTP connections from
+local processes, you must create a host list with an empty item. For example:
+
+hosts_connection_nolog = :
+
+If the smtp_connection log selector is not set, this option has no effect.
+
++--------------------+---------+------------------+--------------+
+|hosts_treat_as_local|Use: main|Type: domain list*|Default: unset|
++--------------------+---------+------------------+--------------+
+
+If this option is set, any host names that match the domain list are treated as
+if they were the local host when Exim is scanning host lists obtained from MX
+records or other sources. Note that the value of this option is a domain list,
+not a host list, because it is always used to check host names, not IP
+addresses.
+
+This option also applies when Exim is matching the special items "@mx_any",
+"@mx_primary", and "@mx_secondary" in a domain list (see section 10.8), and
+when checking the hosts option in the smtp transport for the local host (see
+the allow_localhost option in that transport). See also local_interfaces, 
+extra_local_interfaces, and chapter 13, which contains a discussion about local
+network interfaces and recognizing the local host.
+
++-------------+---------+-----------------+--------------+
+|ibase_servers|Use: main|Type: string list|Default: unset|
++-------------+---------+-----------------+--------------+
+
+This option provides a list of InterBase servers and associated connection
+data, to be used in conjunction with ibase lookups (see section 9.21). The
+option is available only if Exim has been built with InterBase support.
+
++--------------------------+---------+----------+------------+
+|ignore_bounce_errors_after|Use: main|Type: time|Default: 10w|
++--------------------------+---------+----------+------------+
+
+This option affects the processing of bounce messages that cannot be delivered,
+that is, those that suffer a permanent delivery failure. (Bounce messages that
+suffer temporary delivery failures are of course retried in the usual way.)
+
+After a permanent delivery failure, bounce messages are frozen, because there
+is no sender to whom they can be returned. When a frozen bounce message has
+been on the queue for more than the given time, it is unfrozen at the next
+queue run, and a further delivery is attempted. If delivery fails again, the
+bounce message is discarded. This makes it possible to keep failed bounce
+messages around for a shorter time than the normal maximum retry time for
+frozen messages. For example,
+
+ignore_bounce_errors_after = 12h
+
+retries failed bounce message deliveries after 12 hours, discarding any further
+failures. If the value of this option is set to a zero time period, bounce
+failures are discarded immediately. Setting a very long time (as in the default
+value) has the effect of disabling this option. For ways of automatically
+dealing with other kinds of frozen message, see auto_thaw and 
+timeout_frozen_after.
+
++---------------------+---------+----------------+--------------+
+|ignore_fromline_hosts|Use: main|Type: host list*|Default: unset|
++---------------------+---------+----------------+--------------+
+
+Some broken SMTP clients insist on sending a UUCP-like "From " line before the
+headers of a message. By default this is treated as the start of the message's
+body, which means that any following headers are not recognized as such. Exim
+can be made to ignore it by setting ignore_fromline_hosts to match those hosts
+that insist on sending it. If the sender is actually a local process rather
+than a remote host, and is using -bs to inject the messages, 
+ignore_fromline_local must be set to achieve this effect.
+
++---------------------+---------+-------------+--------------+
+|ignore_fromline_local|Use: main|Type: boolean|Default: false|
++---------------------+---------+-------------+--------------+
+
+See ignore_fromline_hosts above.
+
++--------------+---------+----------+-----------+
+|keep_malformed|Use: main|Type: time|Default: 4d|
++--------------+---------+----------+-----------+
+
+This option specifies the length of time to keep messages whose spool files
+have been corrupted in some way. This should, of course, never happen. At the
+next attempt to deliver such a message, it gets removed. The incident is
+logged.
+
++----------------+---------+------------+--------------+
+|ldap_ca_cert_dir|Use: main|Type: string|Default: unset|
++----------------+---------+------------+--------------+
+
+This option indicates which directory contains CA certificates for verifying a
+TLS certificate presented by an LDAP server. While Exim does not provide a
+default value, your SSL library may. Analogous to tls_verify_certificates but
+as a client-side option for LDAP and constrained to be a directory.
+
++-----------------+---------+------------+--------------+
+|ldap_ca_cert_file|Use: main|Type: string|Default: unset|
++-----------------+---------+------------+--------------+
+
+This option indicates which file contains CA certificates for verifying a TLS
+certificate presented by an LDAP server. While Exim does not provide a default
+value, your SSL library may. Analogous to tls_verify_certificates but as a
+client-side option for LDAP and constrained to be a file.
+
++--------------+---------+------------+--------------+
+|ldap_cert_file|Use: main|Type: string|Default: unset|
++--------------+---------+------------+--------------+
+
+This option indicates which file contains an TLS client certificate which Exim
+should present to the LDAP server during TLS negotiation. Should be used
+together with ldap_cert_key.
+
++-------------+---------+------------+--------------+
+|ldap_cert_key|Use: main|Type: string|Default: unset|
++-------------+---------+------------+--------------+
+
+This option indicates which file contains the secret/private key to use to
+prove identity to the LDAP server during TLS negotiation. Should be used
+together with ldap_cert_file, which contains the identity to be proven.
+
++-----------------+---------+------------+--------------+
+|ldap_cipher_suite|Use: main|Type: string|Default: unset|
++-----------------+---------+------------+--------------+
+
+This controls the TLS cipher-suite negotiation during TLS negotiation with the
+LDAP server. See 41.4 for more details of the format of cipher-suite options
+with OpenSSL (as used by LDAP client libraries).
+
++--------------------+---------+-----------------+--------------+
+|ldap_default_servers|Use: main|Type: string list|Default: unset|
++--------------------+---------+-----------------+--------------+
+
+This option provides a list of LDAP servers which are tried in turn when an
+LDAP query does not contain a server. See section 9.14 for details of LDAP
+queries. This option is available only when Exim has been built with LDAP
+support.
+
++-----------------+---------+------------+---------------+
+|ldap_require_cert|Use: main|Type: string|Default: unset.|
++-----------------+---------+------------+---------------+
+
+This should be one of the values "hard", "demand", "allow", "try" or "never". A
+value other than one of these is interpreted as "never". See the entry
+"TLS_REQCERT" in your system man page for ldap.conf(5). Although Exim does not
+set a default, the LDAP library probably defaults to hard/demand.
+
++--------------+---------+-------------+--------------+
+|ldap_start_tls|Use: main|Type: boolean|Default: false|
++--------------+---------+-------------+--------------+
+
+If set, Exim will attempt to negotiate TLS with the LDAP server when connecting
+on a regular LDAP port. This is the LDAP equivalent of SMTP's "STARTTLS". This
+is distinct from using "ldaps", which is the LDAP form of SSL-on-connect. In
+the event of failure to negotiate TLS, the action taken is controlled by 
+ldap_require_cert.
+
++------------+---------+-------------+--------------+
+|ldap_version|Use: main|Type: integer|Default: unset|
++------------+---------+-------------+--------------+
+
+This option can be used to force Exim to set a specific protocol version for
+LDAP. If it option is unset, it is shown by the -bP command line option as -1.
+When this is the case, the default is 3 if LDAP_VERSION3 is defined in the LDAP
+headers; otherwise it is 2. This option is available only when Exim has been
+built with LDAP support.
+
++----------------+---------+-------------+-------------+
+|local_from_check|Use: main|Type: boolean|Default: true|
++----------------+---------+-------------+-------------+
+
+When a message is submitted locally (that is, not over a TCP/IP connection) by
+an untrusted user, Exim removes any existing Sender: header line, and checks
+that the From: header line matches the login of the calling user and the domain
+specified by qualify_domain.
+
+Note: An unqualified address (no domain) in the From: header in a locally
+submitted message is automatically qualified by Exim, unless the -bnq command
+line option is used.
+
+You can use local_from_prefix and local_from_suffix to permit affixes on the
+local part. If the From: header line does not match, Exim adds a Sender: header
+with an address constructed from the calling user's login and the default
+qualify domain.
+
+If local_from_check is set false, the From: header check is disabled, and no 
+Sender: header is ever added. If, in addition, you want to retain Sender:
+header lines supplied by untrusted users, you must also set local_sender_retain
+to be true.
+
+These options affect only the header lines in the message. The envelope sender
+is still forced to be the login id at the qualify domain unless 
+untrusted_set_sender permits the user to supply an envelope sender.
+
+For messages received over TCP/IP, an ACL can specify "submission mode" to
+request similar header line checking. See section 46.16, which has more details
+about Sender: processing.
+
++-----------------+---------+------------+--------------+
+|local_from_prefix|Use: main|Type: string|Default: unset|
++-----------------+---------+------------+--------------+
+
+When Exim checks the From: header line of locally submitted messages for
+matching the login id (see local_from_check above), it can be configured to
+ignore certain prefixes and suffixes in the local part of the address. This is
+done by setting local_from_prefix and/or local_from_suffix to appropriate
+lists, in the same form as the local_part_prefix and local_part_suffix router
+options (see chapter 15). For example, if
+
+local_from_prefix = *-
+
+is set, a From: line containing
+
+From: anything-user@your.domain.example
+
+will not cause a Sender: header to be added if user@your.domain.example matches
+the actual sender address that is constructed from the login name and qualify
+domain.
+
++-----------------+---------+------------+--------------+
+|local_from_suffix|Use: main|Type: string|Default: unset|
++-----------------+---------+------------+--------------+
+
+See local_from_prefix above.
+
++----------------+---------+-----------------+------------------+
+|local_interfaces|Use: main|Type: string list|Default: see below|
++----------------+---------+-----------------+------------------+
+
+This option controls which network interfaces are used by the daemon for
+listening; they are also used to identify the local host when routing. Chapter
+13 contains a full description of this option and the related options 
+daemon_smtp_ports, extra_local_interfaces, hosts_treat_as_local, and 
+tls_on_connect_ports. The default value for local_interfaces is
+
+local_interfaces = 0.0.0.0
+
+when Exim is built without IPv6 support; otherwise it is
+
+local_interfaces = <; ::0 ; 0.0.0.0
+
++------------------+---------+----------+-----------+
+|local_scan_timeout|Use: main|Type: time|Default: 5m|
++------------------+---------+----------+-----------+
+
+This timeout applies to the local_scan() function (see chapter 44). Zero means
+"no timeout". If the timeout is exceeded, the incoming message is rejected with
+a temporary error if it is an SMTP message. For a non-SMTP message, the message
+is dropped and Exim ends with a non-zero code. The incident is logged on the
+main and reject logs.
+
++-------------------+---------+-------------+--------------+
+|local_sender_retain|Use: main|Type: boolean|Default: false|
++-------------------+---------+-------------+--------------+
+
+When a message is submitted locally (that is, not over a TCP/IP connection) by
+an untrusted user, Exim removes any existing Sender: header line. If you do not
+want this to happen, you must set local_sender_retain, and you must also set 
+local_from_check to be false (Exim will complain if you do not). See also the
+ACL modifier "control = suppress_local_fixups". Section 46.16 has more details
+about Sender: processing.
+
++----------------+---------+-------------+--------------+
+|localhost_number|Use: main|Type: string*|Default: unset|
++----------------+---------+-------------+--------------+
+
+Exim's message ids are normally unique only within the local host. If
+uniqueness among a set of hosts is required, each host must set a different
+value for the localhost_number option. The string is expanded immediately after
+reading the configuration file (so that a number can be computed from the host
+name, for example) and the result of the expansion must be a number in the
+range 0-16 (or 0-10 on operating systems with case-insensitive file systems).
+This is available in subsequent string expansions via the variable
+$localhost_number. When localhost_number is set, the final two characters of
+the message id, instead of just being a fractional part of the time, are
+computed from the time and the local host number as described in section 3.4.
+
++-------------+---------+------------------+----------------------------+
+|log_file_path|Use: main|Type: string list*|Default: set at compile time|
++-------------+---------+------------------+----------------------------+
+
+This option sets the path which is used to determine the names of Exim's log
+files, or indicates that logging is to be to syslog, or both. It is expanded
+when Exim is entered, so it can, for example, contain a reference to the host
+name. If no specific path is set for the log files at compile or run time, they
+are written in a sub-directory called log in Exim's spool directory. Chapter 51
+contains further details about Exim's logging, and section 51.1 describes how
+the contents of log_file_path are used. If this string is fixed at your
+installation (contains no expansion variables) it is recommended that you do
+not set this option in the configuration file, but instead supply the path
+using LOG_FILE_PATH in Local/Makefile so that it is available to Exim for
+logging errors detected early on - in particular, failure to read the
+configuration file.
+
++------------+---------+------------+--------------+
+|log_selector|Use: main|Type: string|Default: unset|
++------------+---------+------------+--------------+
+
+This option can be used to reduce or increase the number of things that Exim
+writes to its log files. Its argument is made up of names preceded by plus or
+minus characters. For example:
+
+log_selector = +arguments -retry_defer
+
+A list of possible names and what they control is given in the chapter on
+logging, in section 51.15.
+
++------------+---------+-------------+--------------+
+|log_timezone|Use: main|Type: boolean|Default: false|
++------------+---------+-------------+--------------+
+
+By default, the timestamps on log lines are in local time without the timezone.
+This means that if your timezone changes twice a year, the timestamps in log
+lines are ambiguous for an hour when the clocks go back. One way of avoiding
+this problem is to set the timezone to UTC. An alternative is to set 
+log_timezone true. This turns on the addition of the timezone offset to
+timestamps in log lines. Turning on this option can add quite a lot to the size
+of log files because each line is extended by 6 characters. Note that the
+$tod_log variable contains the log timestamp without the zone, but there is
+another variable called $tod_zone that contains just the timezone offset.
+
++---------------+---------+-------------+-----------+
+|lookup_open_max|Use: main|Type: integer|Default: 25|
++---------------+---------+-------------+-----------+
+
+This option limits the number of simultaneously open files for single-key
+lookups that use regular files (that is, lsearch, dbm, and cdb). Exim normally
+keeps these files open during routing, because often the same file is required
+several times. If the limit is reached, Exim closes the least recently used
+file. Note that if you are using the ndbm library, it actually opens two files
+for each logical DBM database, though it still counts as one for the purposes
+of lookup_open_max. If you are getting "too many open files" errors with NDBM,
+you need to reduce the value of lookup_open_max.
+
++-------------------+---------+-------------+----------+
+|max_username_length|Use: main|Type: integer|Default: 0|
++-------------------+---------+-------------+----------+
+
+Some operating systems are broken in that they truncate long arguments to
+getpwnam() to eight characters, instead of returning "no such user". If this
+option is set greater than zero, any attempt to call getpwnam() with an
+argument that is longer behaves as if getpwnam() failed.
+
++---------------------+---------+----------+--------------+
+|message_body_newlines|Use: main|Type: bool|Default: false|
++---------------------+---------+----------+--------------+
+
+By default, newlines in the message body are replaced by spaces when setting
+the $message_body and $message_body_end expansion variables. If this option is
+set true, this no longer happens.
+
++--------------------+---------+-------------+------------+
+|message_body_visible|Use: main|Type: integer|Default: 500|
++--------------------+---------+-------------+------------+
+
+This option specifies how much of a message's body is to be included in the
+$message_body and $message_body_end expansion variables.
+
++------------------------+---------+-------------+--------------+
+|message_id_header_domain|Use: main|Type: string*|Default: unset|
++------------------------+---------+-------------+--------------+
+
+If this option is set, the string is expanded and used as the right hand side
+(domain) of the Message-ID: header that Exim creates if a locally-originated
+incoming message does not have one. "Locally-originated" means "not received
+over TCP/IP." Otherwise, the primary host name is used. Only letters, digits,
+dot and hyphen are accepted; any other characters are replaced by hyphens. If
+the expansion is forced to fail, or if the result is an empty string, the
+option is ignored.
+
++----------------------+---------+-------------+--------------+
+|message_id_header_text|Use: main|Type: string*|Default: unset|
++----------------------+---------+-------------+--------------+
+
+If this variable is set, the string is expanded and used to augment the text of
+the Message-id: header that Exim creates if a locally-originated incoming
+message does not have one. The text of this header is required by RFC 2822 to
+take the form of an address. By default, Exim uses its internal message id as
+the local part, and the primary host name as the domain. If this option is set,
+it is expanded, and provided the expansion is not forced to fail, and does not
+yield an empty string, the result is inserted into the header immediately
+before the @, separated from the internal message id by a dot. Any characters
+that are illegal in an address are automatically converted into hyphens. This
+means that variables such as $tod_log can be used, because the spaces and
+colons will become hyphens.
+
++------------+---------+-------------+-------------+
+|message_logs|Use: main|Type: boolean|Default: true|
++------------+---------+-------------+-------------+
+
+If this option is turned off, per-message log files are not created in the
+msglog spool sub-directory. This reduces the amount of disk I/O required by
+Exim, by reducing the number of files involved in handling a message from a
+minimum of four (header spool file, body spool file, delivery journal, and
+per-message log) to three. The other major I/O activity is Exim's main log,
+which is not affected by this option.
+
++------------------+---------+-------------+------------+
+|message_size_limit|Use: main|Type: string*|Default: 50M|
++------------------+---------+-------------+------------+
+
+This option limits the maximum size of message that Exim will process. The
+value is expanded for each incoming connection so, for example, it can be made
+to depend on the IP address of the remote host for messages arriving via TCP/
+IP. After expansion, the value must be a sequence of decimal digits, optionally
+followed by K or M.
+
+Note: This limit cannot be made to depend on a message's sender or any other
+properties of an individual message, because it has to be advertised in the
+server's response to EHLO. String expansion failure causes a temporary error. A
+value of zero means no limit, but its use is not recommended. See also 
+bounce_return_size_limit.
+
+Incoming SMTP messages are failed with a 552 error if the limit is exceeded;
+locally-generated messages either get a stderr message or a delivery failure
+message to the sender, depending on the -oe setting. Rejection of an oversized
+message is logged in both the main and the reject logs. See also the generic
+transport option message_size_limit, which limits the size of message that an
+individual transport can process.
+
+If you use a virus-scanner and set this option to to a value larger than the
+maximum size that your virus-scanner is configured to support, you may get
+failures triggered by large mails. The right size to configure for the
+virus-scanner depends upon what data is passed and the options in use but it's
+probably safest to just set it to a little larger than this value. Eg, with a
+default Exim message size of 50M and a default ClamAV StreamMaxLength of 10M,
+some problems may result.
+
+A value of 0 will disable size limit checking; Exim will still advertise the
+SIZE extension in an EHLO response, but without a limit, so as to permit SMTP
+clients to still indicate the message size along with the MAIL verb.
+
++--------------------+---------+-------------+--------------+
+|move_frozen_messages|Use: main|Type: boolean|Default: false|
++--------------------+---------+-------------+--------------+
+
+This option, which is available only if Exim has been built with the setting
+
+SUPPORT_MOVE_FROZEN_MESSAGES=yes
+
+in Local/Makefile, causes frozen messages and their message logs to be moved
+from the input and msglog directories on the spool to Finput and Fmsglog,
+respectively. There is currently no support in Exim or the standard utilities
+for handling such moved messages, and they do not show up in lists generated by
+-bp or by the Exim monitor.
+
++-----------+---------+-------------+--------------+
+|mua_wrapper|Use: main|Type: boolean|Default: false|
++-----------+---------+-------------+--------------+
+
+Setting this option true causes Exim to run in a very restrictive mode in which
+it passes messages synchronously to a smart host. Chapter 50 contains a full
+description of this facility.
+
++-------------+---------+-----------------+--------------+
+|mysql_servers|Use: main|Type: string list|Default: unset|
++-------------+---------+-----------------+--------------+
+
+This option provides a list of MySQL servers and associated connection data, to
+be used in conjunction with mysql lookups (see section 9.21). The option is
+available only if Exim has been built with MySQL support.
+
++-----------+---------+------------------+--------------+
+|never_users|Use: main|Type: string list*|Default: unset|
++-----------+---------+------------------+--------------+
+
+This option is expanded just once, at the start of Exim's processing. Local
+message deliveries are normally run in processes that are setuid to the
+recipient, and remote deliveries are normally run under Exim's own uid and gid.
+It is usually desirable to prevent any deliveries from running as root, as a
+safety precaution.
+
+When Exim is built, an option called FIXED_NEVER_USERS can be set to a list of
+users that must not be used for local deliveries. This list is fixed in the
+binary and cannot be overridden by the configuration file. By default, it
+contains just the single user name "root". The never_users runtime option can
+be used to add more users to the fixed list.
+
+If a message is to be delivered as one of the users on the fixed list or the 
+never_users list, an error occurs, and delivery is deferred. A common example
+is
+
+never_users = root:daemon:bin
+
+Including root is redundant if it is also on the fixed list, but it does no
+harm. This option overrides the pipe_as_creator option of the pipe transport
+driver.
+
++---------------+---------+-----------------+------------------+
+|openssl_options|Use: main|Type: string list|Default: +no_sslv2|
++---------------+---------+-----------------+------------------+
+
+This option allows an administrator to adjust the SSL options applied by
+OpenSSL to connections. It is given as a space-separated list of items, each
+one to be +added or -subtracted from the current value.
+
+This option is only available if Exim is built against OpenSSL. The values
+available for this option vary according to the age of your OpenSSL install.
+The "all" value controls a subset of flags which are available, typically the
+bug workaround options. The SSL_CTX_set_options man page will list the values
+known on your system and Exim should support all the "bug workaround" options
+and many of the "modifying" options. The Exim names lose the leading "SSL_OP_"
+and are lower-cased.
+
+Note that adjusting the options can have severe impact upon the security of SSL
+as used by Exim. It is possible to disable safety checks and shoot yourself in
+the foot in various unpleasant ways. This option should not be adjusted
+lightly. An unrecognised item will be detected at startup, by invoking Exim
+with the -bV flag.
+
+Historical note: prior to release 4.80, Exim defaulted this value to
+"+dont_insert_empty_fragments", which may still be needed for compatibility
+with some clients, but which lowers security by increasing exposure to some now
+infamous attacks.
+
+An example:
+
+# Make both old MS and old Eudora happy:
+openssl_options = -all +microsoft_big_sslv3_buffer \
+                       +dont_insert_empty_fragments
+
+Possible options may include:
+
+  * "all"
+
+  * "allow_unsafe_legacy_renegotiation"
+
+  * "cipher_server_preference"
+
+  * "dont_insert_empty_fragments"
+
+  * "ephemeral_rsa"
+
+  * "legacy_server_connect"
+
+  * "microsoft_big_sslv3_buffer"
+
+  * "microsoft_sess_id_bug"
+
+  * "msie_sslv2_rsa_padding"
+
+  * "netscape_challenge_bug"
+
+  * "netscape_reuse_cipher_change_bug"
+
+  * "no_compression"
+
+  * "no_session_resumption_on_renegotiation"
+
+  * "no_sslv2"
+
+  * "no_sslv3"
+
+  * "no_ticket"
+
+  * "no_tlsv1"
+
+  * "no_tlsv1_1"
+
+  * "no_tlsv1_2"
+
+  * "safari_ecdhe_ecdsa_bug"
+
+  * "single_dh_use"
+
+  * "single_ecdh_use"
+
+  * "ssleay_080_client_dh_bug"
+
+  * "sslref2_reuse_cert_type_bug"
+
+  * "tls_block_padding_bug"
+
+  * "tls_d5_bug"
+
+  * "tls_rollback_bug"
+
+As an aside, the "safari_ecdhe_ecdsa_bug" item is a misnomer and affects all
+clients connecting using the MacOS SecureTransport TLS facility prior to MacOS
+10.8.4, including email clients. If you see old MacOS clients failing to
+negotiate TLS then this option value might help, provided that your OpenSSL
+release is new enough to contain this work-around. This may be a situation
+where you have to upgrade OpenSSL to get buggy clients working.
+
++--------------+---------+-----------------+--------------+
+|oracle_servers|Use: main|Type: string list|Default: unset|
++--------------+---------+-----------------+--------------+
+
+This option provides a list of Oracle servers and associated connection data,
+to be used in conjunction with oracle lookups (see section 9.21). The option is
+available only if Exim has been built with Oracle support.
+
++--------------------+---------+------------------+--------------+
+|percent_hack_domains|Use: main|Type: domain list*|Default: unset|
++--------------------+---------+------------------+--------------+
+
+The "percent hack" is the convention whereby a local part containing a percent
+sign is re-interpreted as a new email address, with the percent replaced by @.
+This is sometimes called "source routing", though that term is also applied to
+RFC 2822 addresses that begin with an @ character. If this option is set, Exim
+implements the percent facility for those domains listed, but no others. This
+happens before an incoming SMTP address is tested against an ACL.
+
+Warning: The "percent hack" has often been abused by people who are trying to
+get round relaying restrictions. For this reason, it is best avoided if at all
+possible. Unfortunately, a number of less security-conscious MTAs implement it
+unconditionally. If you are running Exim on a gateway host, and routing mail
+through to internal MTAs without processing the local parts, it is a good idea
+to reject recipient addresses with percent characters in their local parts.
+Exim's default configuration does this.
+
++-------------+---------+-------------+--------------+
+|perl_at_start|Use: main|Type: boolean|Default: false|
++-------------+---------+-------------+--------------+
+
+This option is available only when Exim is built with an embedded Perl
+interpreter. See chapter 12 for details of its use.
+
++------------+---------+------------+--------------+
+|perl_startup|Use: main|Type: string|Default: unset|
++------------+---------+------------+--------------+
+
+This option is available only when Exim is built with an embedded Perl
+interpreter. See chapter 12 for details of its use.
+
++-------------+---------+-----------------+--------------+
+|pgsql_servers|Use: main|Type: string list|Default: unset|
++-------------+---------+-----------------+--------------+
+
+This option provides a list of PostgreSQL servers and associated connection
+data, to be used in conjunction with pgsql lookups (see section 9.21). The
+option is available only if Exim has been built with PostgreSQL support.
+
++-------------+---------+-------------+----------------------------+
+|pid_file_path|Use: main|Type: string*|Default: set at compile time|
++-------------+---------+-------------+----------------------------+
+
+This option sets the name of the file to which the Exim daemon writes its
+process id. The string is expanded, so it can contain, for example, references
+to the host name:
+
+pid_file_path = /var/log/$primary_hostname/exim.pid
+
+If no path is set, the pid is written to the file exim-daemon.pid in Exim's
+spool directory. The value set by the option can be overridden by the -oP
+command line option. A pid file is not written if a "non-standard" daemon is
+run by means of the -oX option, unless a path is explicitly supplied by -oP.
+
++--------------------------+---------+----------------+----------+
+|pipelining_advertise_hosts|Use: main|Type: host list*|Default: *|
++--------------------------+---------+----------------+----------+
+
+This option can be used to suppress the advertisement of the SMTP PIPELINING
+extension to specific hosts. See also the no_pipelining control in section
+42.22. When PIPELINING is not advertised and smtp_enforce_sync is true, an Exim
+server enforces strict synchronization for each SMTP command and response. When
+PIPELINING is advertised, Exim assumes that clients will use it; "out of order"
+commands that are "expected" do not count as protocol errors (see 
+smtp_max_synprot_errors).
+
++-----------+---------+-------------+--------------+
+|prdr_enable|Use: main|Type: boolean|Default: false|
++-----------+---------+-------------+--------------+
+
+This option can be used to enable the Per-Recipient Data Response extension to
+SMTP, defined by Eric Hall. If the option is set, PRDR is advertised by Exim
+when operating as a server. If the client requests PRDR, and more than one
+recipient, for a message an additional ACL is called for each recipient after
+the message content is recieved. See section 42.9.
+
++---------------------+---------+-------------+--------------+
+|preserve_message_logs|Use: main|Type: boolean|Default: false|
++---------------------+---------+-------------+--------------+
+
+If this option is set, message log files are not deleted when messages are
+completed. Instead, they are moved to a sub-directory of the spool directory
+called msglog.OLD, where they remain available for statistical or debugging
+purposes. This is a dangerous option to set on systems with any appreciable
+volume of mail. Use with care!
+
++----------------+---------+------------+------------------+
+|primary_hostname|Use: main|Type: string|Default: see below|
++----------------+---------+------------+------------------+
+
+This specifies the name of the current host. It is used in the default EHLO or
+HELO command for outgoing SMTP messages (changeable via the helo_data option in
+the smtp transport), and as the default for qualify_domain. The value is also
+used by default in some SMTP response messages from an Exim server. This can be
+changed dynamically by setting smtp_active_hostname.
+
+If primary_hostname is not set, Exim calls uname() to find the host name. If
+this fails, Exim panics and dies. If the name returned by uname() contains only
+one component, Exim passes it to gethostbyname() (or getipnodebyname() when
+available) in order to obtain the fully qualified version. The variable
+$primary_hostname contains the host name, whether set explicitly by this
+option, or defaulted.
+
++-----------------+---------+-------------+--------------+
+|print_topbitchars|Use: main|Type: boolean|Default: false|
++-----------------+---------+-------------+--------------+
+
+By default, Exim considers only those characters whose codes lie in the range
+32-126 to be printing characters. In a number of circumstances (for example,
+when writing log entries) non-printing characters are converted into escape
+sequences, primarily to avoid messing up the layout. If print_topbitchars is
+set, code values of 128 and above are also considered to be printing
+characters.
+
+This option also affects the header syntax checks performed by the autoreply
+transport, and whether Exim uses RFC 2047 encoding of the user's full name when
+constructing From: and Sender: addresses (as described in section 46.18).
+Setting this option can cause Exim to generate eight bit message headers that
+do not conform to the standards.
+
++----------------+---------+------------+--------------+
+|process_log_path|Use: main|Type: string|Default: unset|
++----------------+---------+------------+--------------+
+
+This option sets the name of the file to which an Exim process writes its
+"process log" when sent a USR1 signal. This is used by the exiwhat utility
+script. If this option is unset, the file called exim-process.info in Exim's
+spool directory is used. The ability to specify the name explicitly can be
+useful in environments where two different Exims are running, using different
+spool directories.
+
++-------------------+---------+-------------+-------------+
+|prod_requires_admin|Use: main|Type: boolean|Default: true|
++-------------------+---------+-------------+-------------+
+
+The -M, -R, and -q command-line options require the caller to be an admin user
+unless prod_requires_admin is set false. See also queue_list_requires_admin.
+
++--------------+---------+------------+------------------+
+|qualify_domain|Use: main|Type: string|Default: see below|
++--------------+---------+------------+------------------+
+
+This option specifies the domain name that is added to any envelope sender
+addresses that do not have a domain qualification. It also applies to recipient
+addresses if qualify_recipient is not set. Unqualified addresses are accepted
+by default only for locally-generated messages. Qualification is also applied
+to addresses in header lines such as From: and To: for locally-generated
+messages, unless the -bnq command line option is used.
+
+Messages from external sources must always contain fully qualified addresses,
+unless the sending host matches sender_unqualified_hosts or 
+recipient_unqualified_hosts (as appropriate), in which case incoming addresses
+are qualified with qualify_domain or qualify_recipient as necessary.
+Internally, Exim always works with fully qualified envelope addresses. If 
+qualify_domain is not set, it defaults to the primary_hostname value.
+
++-----------------+---------+------------+------------------+
+|qualify_recipient|Use: main|Type: string|Default: see below|
++-----------------+---------+------------+------------------+
+
+This option allows you to specify a different domain for qualifying recipient
+addresses to the one that is used for senders. See qualify_domain above.
+
++-------------+---------+------------------+--------------+
+|queue_domains|Use: main|Type: domain list*|Default: unset|
++-------------+---------+------------------+--------------+
+
+This option lists domains for which immediate delivery is not required. A
+delivery process is started whenever a message is received, but only those
+domains that do not match are processed. All other deliveries wait until the
+next queue run. See also hold_domains and queue_smtp_domains.
+
++-------------------------+---------+-------------+-------------+
+|queue_list_requires_admin|Use: main|Type: boolean|Default: true|
++-------------------------+---------+-------------+-------------+
+
+The -bp command-line option, which lists the messages that are on the queue,
+requires the caller to be an admin user unless queue_list_requires_admin is set
+false. See also prod_requires_admin.
+
++----------+---------+-------------+--------------+
+|queue_only|Use: main|Type: boolean|Default: false|
++----------+---------+-------------+--------------+
+
+If queue_only is set, a delivery process is not automatically started whenever
+a message is received. Instead, the message waits on the queue for the next
+queue run. Even if queue_only is false, incoming messages may not get delivered
+immediately when certain conditions (such as heavy load) occur.
+
+The -odq command line has the same effect as queue_only. The -odb and -odi
+command line options override queue_only unless queue_only_override is set
+false. See also queue_only_file, queue_only_load, and smtp_accept_queue.
+
++---------------+---------+------------+--------------+
+|queue_only_file|Use: main|Type: string|Default: unset|
++---------------+---------+------------+--------------+
+
+This option can be set to a colon-separated list of absolute path names, each
+one optionally preceded by "smtp". When Exim is receiving a message, it tests
+for the existence of each listed path using a call to stat(). For each path
+that exists, the corresponding queueing option is set. For paths with no
+prefix, queue_only is set; for paths prefixed by "smtp", queue_smtp_domains is
+set to match all domains. So, for example,
+
+queue_only_file = smtp/some/file
+
+causes Exim to behave as if queue_smtp_domains were set to "*" whenever /some/
+file exists.
+
++---------------+---------+-----------------+--------------+
+|queue_only_load|Use: main|Type: fixed-point|Default: unset|
++---------------+---------+-----------------+--------------+
+
+If the system load average is higher than this value, incoming messages from
+all sources are queued, and no automatic deliveries are started. If this
+happens during local or remote SMTP input, all subsequent messages received on
+the same SMTP connection are queued by default, whatever happens to the load in
+the meantime, but this can be changed by setting queue_only_load_latch false.
+
+Deliveries will subsequently be performed by queue runner processes. This
+option has no effect on ancient operating systems on which Exim cannot
+determine the load average. See also deliver_queue_load_max and 
+smtp_load_reserve.
+
++---------------------+---------+-------------+-------------+
+|queue_only_load_latch|Use: main|Type: boolean|Default: true|
++---------------------+---------+-------------+-------------+
+
+When this option is true (the default), once one message has been queued
+because the load average is higher than the value set by queue_only_load, all
+subsequent messages received on the same SMTP connection are also queued. This
+is a deliberate choice; even though the load average may fall below the
+threshold, it doesn't seem right to deliver later messages on the same
+connection when not delivering earlier ones. However, there are special
+circumstances such as very long-lived connections from scanning appliances
+where this is not the best strategy. In such cases, queue_only_load_latch
+should be set false. This causes the value of the load average to be
+re-evaluated for each message.
+
++-------------------+---------+-------------+-------------+
+|queue_only_override|Use: main|Type: boolean|Default: true|
++-------------------+---------+-------------+-------------+
+
+When this option is true, the -odx command line options override the setting of
+queue_only or queue_only_file in the configuration file. If queue_only_override
+is set false, the -odx options cannot be used to override; they are accepted,
+but ignored.
+
++------------------+---------+-------------+--------------+
+|queue_run_in_order|Use: main|Type: boolean|Default: false|
++------------------+---------+-------------+--------------+
+
+If this option is set, queue runs happen in order of message arrival instead of
+in an arbitrary order. For this to happen, a complete list of the entire queue
+must be set up before the deliveries start. When the queue is all held in a
+single directory (the default), a single list is created for both the ordered
+and the non-ordered cases. However, if split_spool_directory is set, a single
+list is not created when queue_run_in_order is false. In this case, the
+sub-directories are processed one at a time (in a random order), and this
+avoids setting up one huge list for the whole queue. Thus, setting 
+queue_run_in_order with split_spool_directory may degrade performance when the
+queue is large, because of the extra work in setting up the single, large list.
+In most situations, queue_run_in_order should not be set.
+
++-------------+---------+-------------+----------+
+|queue_run_max|Use: main|Type: integer|Default: 5|
++-------------+---------+-------------+----------+
+
+This controls the maximum number of queue runner processes that an Exim daemon
+can run simultaneously. This does not mean that it starts them all at once, but
+rather that if the maximum number are still running when the time comes to
+start another one, it refrains from starting another one. This can happen with
+very large queues and/or very sluggish deliveries. This option does not,
+however, interlock with other processes, so additional queue runners can be
+started by other means, or by killing and restarting the daemon.
+
+Setting this option to zero does not suppress queue runs; rather, it disables
+the limit, allowing any number of simultaneous queue runner processes to be
+run. If you do not want queue runs to occur, omit the -qxx setting on the
+daemon's command line.
+
++------------------+---------+------------------+--------------+
+|queue_smtp_domains|Use: main|Type: domain list*|Default: unset|
++------------------+---------+------------------+--------------+
+
+When this option is set, a delivery process is started whenever a message is
+received, routing is performed, and local deliveries take place. However, if
+any SMTP deliveries are required for domains that match queue_smtp_domains,
+they are not immediately delivered, but instead the message waits on the queue
+for the next queue run. Since routing of the message has taken place, Exim
+knows to which remote hosts it must be delivered, and so when the queue run
+happens, multiple messages for the same host are delivered over a single SMTP
+connection. The -odqs command line option causes all SMTP deliveries to be
+queued in this way, and is equivalent to setting queue_smtp_domains to "*". See
+also hold_domains and queue_domains.
+
++---------------+---------+----------+-----------+
+|receive_timeout|Use: main|Type: time|Default: 0s|
++---------------+---------+----------+-----------+
+
+This option sets the timeout for accepting a non-SMTP message, that is, the
+maximum time that Exim waits when reading a message on the standard input. If
+the value is zero, it will wait for ever. This setting is overridden by the -or
+command line option. The timeout for incoming SMTP messages is controlled by 
+smtp_receive_timeout.
+
++--------------------+---------+-------------+------------------+
+|received_header_text|Use: main|Type: string*|Default: see below|
++--------------------+---------+-------------+------------------+
+
+This string defines the contents of the Received: message header that is added
+to each message, except for the timestamp, which is automatically added on at
+the end (preceded by a semicolon). The string is expanded each time it is used.
+If the expansion yields an empty string, no Received: header line is added to
+the message. Otherwise, the string should start with the text "Received:" and
+conform to the RFC 2822 specification for Received: header lines. The default
+setting is:
+
+received_header_text = Received: \
+  ${if def:sender_rcvhost {from $sender_rcvhost\n\t}\
+  {${if def:sender_ident \
+  {from ${quote_local_part:$sender_ident} }}\
+  ${if def:sender_helo_name {(helo=$sender_helo_name)\n\t}}}}\
+  by $primary_hostname \
+  ${if def:received_protocol {with $received_protocol}} \
+  ${if def:tls_in_cipher {($tls_in_cipher)\n\t}}\
+  (Exim $version_number)\n\t\
+  ${if def:sender_address \
+  {(envelope-from <$sender_address>)\n\t}}\
+  id $message_exim_id\
+  ${if def:received_for {\n\tfor $received_for}}
+
+The reference to the TLS cipher is omitted when Exim is built without TLS
+support. The use of conditional expansions ensures that this works for both
+locally generated messages and messages received from remote hosts, giving
+header lines such as the following:
+
+Received: from scrooge.carol.example ([192.168.12.25] ident=root)
+by marley.carol.example with esmtp (Exim 4.00)
+(envelope-from <bob@carol.example>)
+id 16IOWa-00019l-00
+for chas@dickens.example; Tue, 25 Dec 2001 14:43:44 +0000
+Received: by scrooge.carol.example with local (Exim 4.00)
+id 16IOWW-000083-00; Tue, 25 Dec 2001 14:43:41 +0000
+
+Until the body of the message has been received, the timestamp is the time when
+the message started to be received. Once the body has arrived, and all policy
+checks have taken place, the timestamp is updated to the time at which the
+message was accepted.
+
++--------------------+---------+-------------+-----------+
+|received_headers_max|Use: main|Type: integer|Default: 30|
++--------------------+---------+-------------+-----------+
+
+When a message is to be delivered, the number of Received: headers is counted,
+and if it is greater than this parameter, a mail loop is assumed to have
+occurred, the delivery is abandoned, and an error message is generated. This
+applies to both local and remote deliveries.
+
++---------------------------+---------+----------------+--------------+
+|recipient_unqualified_hosts|Use: main|Type: host list*|Default: unset|
++---------------------------+---------+----------------+--------------+
+
+This option lists those hosts from which Exim is prepared to accept unqualified
+recipient addresses in message envelopes. The addresses are made fully
+qualified by the addition of the qualify_recipient value. This option also
+affects message header lines. Exim does not reject unqualified recipient
+addresses in headers, but it qualifies them only if the message came from a
+host that matches recipient_unqualified_hosts, or if the message was submitted
+locally (not using TCP/IP), and the -bnq option was not set.
+
++--------------+---------+-------------+----------+
+|recipients_max|Use: main|Type: integer|Default: 0|
++--------------+---------+-------------+----------+
+
+If this option is set greater than zero, it specifies the maximum number of
+original recipients for any message. Additional recipients that are generated
+by aliasing or forwarding do not count. SMTP messages get a 452 response for
+all recipients over the limit; earlier recipients are delivered as normal.
+Non-SMTP messages with too many recipients are failed, and no deliveries are
+done.
+
+Note: The RFCs specify that an SMTP server should accept at least 100 RCPT
+commands in a single message.
+
++---------------------+---------+-------------+--------------+
+|recipients_max_reject|Use: main|Type: boolean|Default: false|
++---------------------+---------+-------------+--------------+
+
+If this option is set true, Exim rejects SMTP messages containing too many
+recipients by giving 552 errors to the surplus RCPT commands, and a 554 error
+to the eventual DATA command. Otherwise (the default) it gives a 452 error to
+the surplus RCPT commands and accepts the message on behalf of the initial set
+of recipients. The remote server should then re-send the message for the
+remaining recipients at a later time.
+
++-------------------+---------+-------------+----------+
+|remote_max_parallel|Use: main|Type: integer|Default: 2|
++-------------------+---------+-------------+----------+
+
+This option controls parallel delivery of one message to a number of remote
+hosts. If the value is less than 2, parallel delivery is disabled, and Exim
+does all the remote deliveries for a message one by one. Otherwise, if a single
+message has to be delivered to more than one remote host, or if several copies
+have to be sent to the same remote host, up to remote_max_parallel deliveries
+are done simultaneously. If more than remote_max_parallel deliveries are
+required, the maximum number of processes are started, and as each one
+finishes, another is begun. The order of starting processes is the same as if
+sequential delivery were being done, and can be controlled by the 
+remote_sort_domains option. If parallel delivery takes place while running with
+debugging turned on, the debugging output from each delivery process is tagged
+with its process id.
+
+This option controls only the maximum number of parallel deliveries for one
+message in one Exim delivery process. Because Exim has no central queue
+manager, there is no way of controlling the total number of simultaneous
+deliveries if the configuration allows a delivery attempt as soon as a message
+is received.
+
+If you want to control the total number of deliveries on the system, you need
+to set the queue_only option. This ensures that all incoming messages are added
+to the queue without starting a delivery process. Then set up an Exim daemon to
+start queue runner processes at appropriate intervals (probably fairly often,
+for example, every minute), and limit the total number of queue runners by
+setting the queue_run_max parameter. Because each queue runner delivers only
+one message at a time, the maximum number of deliveries that can then take
+place at once is queue_run_max multiplied by remote_max_parallel.
+
+If it is purely remote deliveries you want to control, use queue_smtp_domains
+instead of queue_only. This has the added benefit of doing the SMTP routing
+before queueing, so that several messages for the same host will eventually get
+delivered down the same connection.
+
++-------------------+---------+------------------+--------------+
+|remote_sort_domains|Use: main|Type: domain list*|Default: unset|
++-------------------+---------+------------------+--------------+
+
+When there are a number of remote deliveries for a message, they are sorted by
+domain into the order given by this list. For example,
+
+remote_sort_domains = *.cam.ac.uk:*.uk
+
+would attempt to deliver to all addresses in the cam.ac.uk domain first, then
+to those in the uk domain, then to any others.
+
++-----------------+---------+----------+-----------+
+|retry_data_expire|Use: main|Type: time|Default: 7d|
++-----------------+---------+----------+-----------+
+
+This option sets a "use before" time on retry information in Exim's hints
+database. Any older retry data is ignored. This means that, for example, once a
+host has not been tried for 7 days, Exim behaves as if it has no knowledge of
+past failures.
+
++------------------+---------+----------+------------+
+|retry_interval_max|Use: main|Type: time|Default: 24h|
++------------------+---------+----------+------------+
+
+Chapter 32 describes Exim's mechanisms for controlling the intervals between
+delivery attempts for messages that cannot be delivered straight away. This
+option sets an overall limit to the length of time between retries. It cannot
+be set greater than 24 hours; any attempt to do so forces the default value.
+
++------------------+---------+-------------+-------------+
+|return_path_remove|Use: main|Type: boolean|Default: true|
++------------------+---------+-------------+-------------+
+
+RFC 2821, section 4.4, states that an SMTP server must insert a Return-path:
+header line into a message when it makes a "final delivery". The Return-path:
+header preserves the sender address as received in the MAIL command. This
+description implies that this header should not be present in an incoming
+message. If return_path_remove is true, any existing Return-path: headers are
+removed from messages at the time they are received. Exim's transports have
+options for adding Return-path: headers at the time of delivery. They are
+normally used only for final local deliveries.
+
++-----------------+---------+-------------+-------------+
+|return_size_limit|Use: main|Type: integer|Default: 100K|
++-----------------+---------+-------------+-------------+
+
+This option is an obsolete synonym for bounce_return_size_limit.
+
++-------------+---------+----------------+----------+
+|rfc1413_hosts|Use: main|Type: host list*|Default: *|
++-------------+---------+----------------+----------+
+
+RFC 1413 identification calls are made to any client host which matches an item
+in the list.
+
++---------------------+---------+----------+-----------+
+|rfc1413_query_timeout|Use: main|Type: time|Default: 5s|
++---------------------+---------+----------+-----------+
+
+This sets the timeout on RFC 1413 identification calls. If it is set to zero,
+no RFC 1413 calls are ever made.
+
++------------------------+---------+----------------+--------------+
+|sender_unqualified_hosts|Use: main|Type: host list*|Default: unset|
++------------------------+---------+----------------+--------------+
+
+This option lists those hosts from which Exim is prepared to accept unqualified
+sender addresses. The addresses are made fully qualified by the addition of 
+qualify_domain. This option also affects message header lines. Exim does not
+reject unqualified addresses in headers that contain sender addresses, but it
+qualifies them only if the message came from a host that matches 
+sender_unqualified_hosts, or if the message was submitted locally (not using
+TCP/IP), and the -bnq option was not set.
+
++---------------------+---------+-------------+-------------+
+|smtp_accept_keepalive|Use: main|Type: boolean|Default: true|
++---------------------+---------+-------------+-------------+
+
+This option controls the setting of the SO_KEEPALIVE option on incoming TCP/IP
+socket connections. When set, it causes the kernel to probe idle connections
+periodically, by sending packets with "old" sequence numbers. The other end of
+the connection should send an acknowledgment if the connection is still okay or
+a reset if the connection has been aborted. The reason for doing this is that
+it has the beneficial effect of freeing up certain types of connection that can
+get stuck when the remote host is disconnected without tidying up the TCP/IP
+call properly. The keepalive mechanism takes several hours to detect
+unreachable hosts.
+
++---------------+---------+-------------+-----------+
+|smtp_accept_max|Use: main|Type: integer|Default: 20|
++---------------+---------+-------------+-----------+
+
+This option specifies the maximum number of simultaneous incoming SMTP calls
+that Exim will accept. It applies only to the listening daemon; there is no
+control (in Exim) when incoming SMTP is being handled by inetd. If the value is
+set to zero, no limit is applied. However, it is required to be non-zero if
+either smtp_accept_max_per_host or smtp_accept_queue is set. See also 
+smtp_accept_reserve and smtp_load_reserve.
+
+A new SMTP connection is immediately rejected if the smtp_accept_max limit has
+been reached. If not, Exim first checks smtp_accept_max_per_host. If that limit
+has not been reached for the client host, smtp_accept_reserve and 
+smtp_load_reserve are then checked before accepting the connection.
+
++-----------------------+---------+-------------+-----------+
+|smtp_accept_max_nonmail|Use: main|Type: integer|Default: 10|
++-----------------------+---------+-------------+-----------+
+
+Exim counts the number of "non-mail" commands in an SMTP session, and drops the
+connection if there are too many. This option defines "too many". The check
+catches some denial-of-service attacks, repeated failing AUTHs, or a mad client
+looping sending EHLO, for example. The check is applied only if the client host
+matches smtp_accept_max_nonmail_hosts.
+
+When a new message is expected, one occurrence of RSET is not counted. This
+allows a client to send one RSET between messages (this is not necessary, but
+some clients do it). Exim also allows one uncounted occurrence of HELO or EHLO,
+and one occurrence of STARTTLS between messages. After starting up a TLS
+session, another EHLO is expected, and so it too is not counted. The first
+occurrence of AUTH in a connection, or immediately following STARTTLS is not
+counted. Otherwise, all commands other than MAIL, RCPT, DATA, and QUIT are
+counted.
+
++-----------------------------+---------+----------------+----------+
+|smtp_accept_max_nonmail_hosts|Use: main|Type: host list*|Default: *|
++-----------------------------+---------+----------------+----------+
+
+You can control which hosts are subject to the smtp_accept_max_nonmail check by
+setting this option. The default value makes it apply to all hosts. By changing
+the value, you can exclude any badly-behaved hosts that you have to live with.
+
++------------------------------+---------+-------------+-------------+
+|smtp_accept_max_per_connection|Use: main|Type: integer|Default: 1000|
++------------------------------+---------+-------------+-------------+
+
+The value of this option limits the number of MAIL commands that Exim is
+prepared to accept over a single SMTP connection, whether or not each command
+results in the transfer of a message. After the limit is reached, a 421
+response is given to subsequent MAIL commands. This limit is a safety
+precaution against a client that goes mad (incidents of this type have been
+seen).
+
++------------------------+---------+-------------+--------------+
+|smtp_accept_max_per_host|Use: main|Type: string*|Default: unset|
++------------------------+---------+-------------+--------------+
+
+This option restricts the number of simultaneous IP connections from a single
+host (strictly, from a single IP address) to the Exim daemon. The option is
+expanded, to enable different limits to be applied to different hosts by
+reference to $sender_host_address. Once the limit is reached, additional
+connection attempts from the same host are rejected with error code 421. This
+is entirely independent of smtp_accept_reserve. The option's default value of
+zero imposes no limit. If this option is set greater than zero, it is required
+that smtp_accept_max be non-zero.
+
+Warning: When setting this option you should not use any expansion
+constructions that take an appreciable amount of time. The expansion and test
+happen in the main daemon loop, in order to reject additional connections
+without forking additional processes (otherwise a denial-of-service attack
+could cause a vast number or processes to be created). While the daemon is
+doing this processing, it cannot accept any other incoming connections.
+
++-----------------+---------+-------------+----------+
+|smtp_accept_queue|Use: main|Type: integer|Default: 0|
++-----------------+---------+-------------+----------+
+
+If the number of simultaneous incoming SMTP connections being handled via the
+listening daemon exceeds this value, messages received by SMTP are just placed
+on the queue; no delivery processes are started automatically. The count is
+fixed at the start of an SMTP connection. It cannot be updated in the
+subprocess that receives messages, and so the queueing or not queueing applies
+to all messages received in the same connection.
+
+A value of zero implies no limit, and clearly any non-zero value is useful only
+if it is less than the smtp_accept_max value (unless that is zero). See also 
+queue_only, queue_only_load, queue_smtp_domains, and the various -odx command
+line options.
+
++--------------------------------+---------+-------------+-----------+
+|smtp_accept_queue_per_connection|Use: main|Type: integer|Default: 10|
++--------------------------------+---------+-------------+-----------+
+
+This option limits the number of delivery processes that Exim starts
+automatically when receiving messages via SMTP, whether via the daemon or by
+the use of -bs or -bS. If the value of the option is greater than zero, and the
+number of messages received in a single SMTP session exceeds this number,
+subsequent messages are placed on the queue, but no delivery processes are
+started. This helps to limit the number of Exim processes when a server
+restarts after downtime and there is a lot of mail waiting for it on other
+systems. On large systems, the default should probably be increased, and on
+dial-in client systems it should probably be set to zero (that is, disabled).
+
++-------------------+---------+-------------+----------+
+|smtp_accept_reserve|Use: main|Type: integer|Default: 0|
++-------------------+---------+-------------+----------+
+
+When smtp_accept_max is set greater than zero, this option specifies a number
+of SMTP connections that are reserved for connections from the hosts that are
+specified in smtp_reserve_hosts. The value set in smtp_accept_max includes this
+reserve pool. The specified hosts are not restricted to this number of
+connections; the option specifies a minimum number of connection slots for
+them, not a maximum. It is a guarantee that this group of hosts can always get
+at least smtp_accept_reserve connections. However, the limit specified by 
+smtp_accept_max_per_host is still applied to each individual host.
+
+For example, if smtp_accept_max is set to 50 and smtp_accept_reserve is set to
+5, once there are 45 active connections (from any hosts), new connections are
+accepted only from hosts listed in smtp_reserve_hosts, provided the other
+criteria for acceptance are met.
+
++--------------------+---------+-------------+--------------+
+|smtp_active_hostname|Use: main|Type: string*|Default: unset|
++--------------------+---------+-------------+--------------+
+
+This option is provided for multi-homed servers that want to masquerade as
+several different hosts. At the start of an incoming SMTP connection, its value
+is expanded and used instead of the value of $primary_hostname in SMTP
+responses. For example, it is used as domain name in the response to an
+incoming HELO or EHLO command.
+
+The active hostname is placed in the $smtp_active_hostname variable, which is
+saved with any messages that are received. It is therefore available for use in
+routers and transports when the message is later delivered.
+
+If this option is unset, or if its expansion is forced to fail, or if the
+expansion results in an empty string, the value of $primary_hostname is used.
+Other expansion failures cause a message to be written to the main and panic
+logs, and the SMTP command receives a temporary error. Typically, the value of 
+smtp_active_hostname depends on the incoming interface address. For example:
+
+smtp_active_hostname = ${if eq{$received_ip_address}{10.0.0.1}\
+  {cox.mydomain}{box.mydomain}}
+
+Although $smtp_active_hostname is primarily concerned with incoming messages,
+it is also used as the default for HELO commands in callout verification if
+there is no remote transport from which to obtain a helo_data value.
+
++-----------+---------+-------------+------------------+
+|smtp_banner|Use: main|Type: string*|Default: see below|
++-----------+---------+-------------+------------------+
+
+This string, which is expanded every time it is used, is output as the initial
+positive response to an SMTP connection. The default setting is:
+
+smtp_banner = $smtp_active_hostname ESMTP Exim \
+  $version_number $tod_full
+
+Failure to expand the string causes a panic error. If you want to create a
+multiline response to the initial SMTP connection, use "\n" in the string at
+appropriate points, but not at the end. Note that the 220 code is not included
+in this string. Exim adds it automatically (several times in the case of a
+multiline response).
+
++----------------------+---------+-------------+-------------+
+|smtp_check_spool_space|Use: main|Type: boolean|Default: true|
++----------------------+---------+-------------+-------------+
+
+When this option is set, if an incoming SMTP session encounters the SIZE option
+on a MAIL command, it checks that there is enough space in the spool
+directory's partition to accept a message of that size, while still leaving
+free the amount specified by check_spool_space (even if that value is zero). If
+there isn't enough space, a temporary error code is returned.
+
++--------------------+---------+-------------+-----------+
+|smtp_connect_backlog|Use: main|Type: integer|Default: 20|
++--------------------+---------+-------------+-----------+
+
+This option specifies a maximum number of waiting SMTP connections. Exim passes
+this value to the TCP/IP system when it sets up its listener. Once this number
+of connections are waiting for the daemon's attention, subsequent connection
+attempts are refused at the TCP/IP level. At least, that is what the manuals
+say; in some circumstances such connection attempts have been observed to time
+out instead. For large systems it is probably a good idea to increase the value
+(to 50, say). It also gives some protection against denial-of-service attacks
+by SYN flooding.
+
++-----------------+---------+-------------+-------------+
+|smtp_enforce_sync|Use: main|Type: boolean|Default: true|
++-----------------+---------+-------------+-------------+
+
+The SMTP protocol specification requires the client to wait for a response from
+the server at certain points in the dialogue. Without PIPELINING these
+synchronization points are after every command; with PIPELINING they are fewer,
+but they still exist.
+
+Some spamming sites send out a complete set of SMTP commands without waiting
+for any response. Exim protects against this by rejecting a message if the
+client has sent further input when it should not have. The error response "554
+SMTP synchronization error" is sent, and the connection is dropped. Testing for
+this error cannot be perfect because of transmission delays (unexpected input
+may be on its way but not yet received when Exim checks). However, it does
+detect many instances.
+
+The check can be globally disabled by setting smtp_enforce_sync false. If you
+want to disable the check selectively (for example, only for certain hosts),
+you can do so by an appropriate use of a control modifier in an ACL (see
+section 42.22). See also pipelining_advertise_hosts.
+
++-----------------+---------+-------------+--------------+
+|smtp_etrn_command|Use: main|Type: string*|Default: unset|
++-----------------+---------+-------------+--------------+
+
+If this option is set, the given command is run whenever an SMTP ETRN command
+is received from a host that is permitted to issue such commands (see chapter
+42). The string is split up into separate arguments which are independently
+expanded. The expansion variable $domain is set to the argument of the ETRN
+command, and no syntax checking is done on it. For example:
+
+smtp_etrn_command = /etc/etrn_command $domain \
+                    $sender_host_address
+
+A new process is created to run the command, but Exim does not wait for it to
+complete. Consequently, its status cannot be checked. If the command cannot be
+run, a line is written to the panic log, but the ETRN caller still receives a
+250 success response. Exim is normally running under its own uid when receiving
+SMTP, so it is not possible for it to change the uid before running the
+command.
+
++-------------------+---------+-------------+-------------+
+|smtp_etrn_serialize|Use: main|Type: boolean|Default: true|
++-------------------+---------+-------------+-------------+
+
+When this option is set, it prevents the simultaneous execution of more than
+one identical command as a result of ETRN in an SMTP connection. See section
+47.8 for details.
+
++-----------------+---------+-----------------+--------------+
+|smtp_load_reserve|Use: main|Type: fixed-point|Default: unset|
++-----------------+---------+-----------------+--------------+
+
+If the system load average ever gets higher than this, incoming SMTP calls are
+accepted only from those hosts that match an entry in smtp_reserve_hosts. If 
+smtp_reserve_hosts is not set, no incoming SMTP calls are accepted when the
+load is over the limit. The option has no effect on ancient operating systems
+on which Exim cannot determine the load average. See also 
+deliver_queue_load_max and queue_only_load.
+
++-----------------------+---------+-------------+----------+
+|smtp_max_synprot_errors|Use: main|Type: integer|Default: 3|
++-----------------------+---------+-------------+----------+
+
+Exim rejects SMTP commands that contain syntax or protocol errors. In
+particular, a syntactically invalid email address, as in this command:
+
+RCPT TO:<abc xyz@a.b.c>
+
+causes immediate rejection of the command, before any other tests are done.
+(The ACL cannot be run if there is no valid address to set up for it.) An
+example of a protocol error is receiving RCPT before MAIL. If there are too
+many syntax or protocol errors in one SMTP session, the connection is dropped.
+The limit is set by this option.
+
+When the PIPELINING extension to SMTP is in use, some protocol errors are
+"expected", for instance, a RCPT command after a rejected MAIL command. Exim
+assumes that PIPELINING will be used if it advertises it (see 
+pipelining_advertise_hosts), and in this situation, "expected" errors do not
+count towards the limit.
+
++-------------------------+---------+-------------+----------+
+|smtp_max_unknown_commands|Use: main|Type: integer|Default: 3|
++-------------------------+---------+-------------+----------+
+
+If there are too many unrecognized commands in an incoming SMTP session, an
+Exim server drops the connection. This is a defence against some kinds of abuse
+that subvert web clients into making connections to SMTP ports; in these
+circumstances, a number of non-SMTP command lines are sent first.
+
++--------------------+---------+----------------+--------------+
+|smtp_ratelimit_hosts|Use: main|Type: host list*|Default: unset|
++--------------------+---------+----------------+--------------+
+
+Some sites find it helpful to be able to limit the rate at which certain hosts
+can send them messages, and the rate at which an individual message can specify
+recipients.
+
+Exim has two rate-limiting facilities. This section describes the older
+facility, which can limit rates within a single connection. The newer ratelimit
+ACL condition can limit rates across all connections. See section 42.38 for
+details of the newer facility.
+
+When a host matches smtp_ratelimit_hosts, the values of smtp_ratelimit_mail and
+smtp_ratelimit_rcpt are used to control the rate of acceptance of MAIL and RCPT
+commands in a single SMTP session, respectively. Each option, if set, must
+contain a set of four comma-separated values:
+
+  * A threshold, before which there is no rate limiting.
+
+  * An initial time delay. Unlike other times in Exim, numbers with decimal
+    fractional parts are allowed here.
+
+  * A factor by which to increase the delay each time.
+
+  * A maximum value for the delay. This should normally be less than 5 minutes,
+    because after that time, the client is liable to timeout the SMTP command.
+
+For example, these settings have been used successfully at the site which first
+suggested this feature, for controlling mail from their customers:
+
+smtp_ratelimit_mail = 2,0.5s,1.05,4m
+smtp_ratelimit_rcpt = 4,0.25s,1.015,4m
+
+The first setting specifies delays that are applied to MAIL commands after two
+have been received over a single connection. The initial delay is 0.5 seconds,
+increasing by a factor of 1.05 each time. The second setting applies delays to
+RCPT commands when more than four occur in a single message.
+
++-------------------+---------+------------+--------------+
+|smtp_ratelimit_mail|Use: main|Type: string|Default: unset|
++-------------------+---------+------------+--------------+
+
+See smtp_ratelimit_hosts above.
+
++-------------------+---------+------------+--------------+
+|smtp_ratelimit_rcpt|Use: main|Type: string|Default: unset|
++-------------------+---------+------------+--------------+
+
+See smtp_ratelimit_hosts above.
+
++--------------------+---------+----------+-----------+
+|smtp_receive_timeout|Use: main|Type: time|Default: 5m|
++--------------------+---------+----------+-----------+
+
+This sets a timeout value for SMTP reception. It applies to all forms of SMTP
+input, including batch SMTP. If a line of input (either an SMTP command or a
+data line) is not received within this time, the SMTP connection is dropped and
+the message is abandoned. A line is written to the log containing one of the
+following messages:
+
+SMTP command timeout on connection from...
+SMTP data timeout on connection from...
+
+The former means that Exim was expecting to read an SMTP command; the latter
+means that it was in the DATA phase, reading the contents of a message.
+
+The value set by this option can be overridden by the -os command-line option.
+A setting of zero time disables the timeout, but this should never be used for
+SMTP over TCP/IP. (It can be useful in some cases of local input using -bs or 
+-bS.) For non-SMTP input, the reception timeout is controlled by 
+receive_timeout and -or.
+
++------------------+---------+----------------+--------------+
+|smtp_reserve_hosts|Use: main|Type: host list*|Default: unset|
++------------------+---------+----------------+--------------+
+
+This option defines hosts for which SMTP connections are reserved; see 
+smtp_accept_reserve and smtp_load_reserve above.
+
++-------------------------+---------+-------------+--------------+
+|smtp_return_error_details|Use: main|Type: boolean|Default: false|
++-------------------------+---------+-------------+--------------+
+
+In the default state, Exim uses bland messages such as "Administrative
+prohibition" when it rejects SMTP commands for policy reasons. Many sysadmins
+like this because it gives away little information to spammers. However, some
+other sysadmins who are applying strict checking policies want to give out much
+fuller information about failures. Setting smtp_return_error_details true
+causes Exim to be more forthcoming. For example, instead of "Administrative
+prohibition", it might give:
+
+550-Rejected after DATA: '>' missing at end of address:
+550 failing address in "From" header is: <user@dom.ain
+
++-------------+---------+------------+------------------+
+|spamd_address|Use: main|Type: string|Default: see below|
++-------------+---------+------------+------------------+
+
+This option is available when Exim is compiled with the content-scanning
+extension. It specifies how Exim connects to SpamAssassin's spamd daemon. The
+default value is
+
+127.0.0.1 783
+
+See section 43.2 for more details.
+
++---------------------+---------+-------------+--------------+
+|split_spool_directory|Use: main|Type: boolean|Default: false|
++---------------------+---------+-------------+--------------+
+
+If this option is set, it causes Exim to split its input directory into 62
+subdirectories, each with a single alphanumeric character as its name. The
+sixth character of the message id is used to allocate messages to
+subdirectories; this is the least significant base-62 digit of the time of
+arrival of the message.
+
+Splitting up the spool in this way may provide better performance on systems
+where there are long mail queues, by reducing the number of files in any one
+directory. The msglog directory is also split up in a similar way to the input
+directory; however, if preserve_message_logs is set, all old msglog files are
+still placed in the single directory msglog.OLD.
+
+It is not necessary to take any special action for existing messages when
+changing split_spool_directory. Exim notices messages that are in the "wrong"
+place, and continues to process them. If the option is turned off after a
+period of being on, the subdirectories will eventually empty and be
+automatically deleted.
+
+When split_spool_directory is set, the behaviour of queue runner processes
+changes. Instead of creating a list of all messages in the queue, and then
+trying to deliver each one in turn, it constructs a list of those in one
+sub-directory and tries to deliver them, before moving on to the next
+sub-directory. The sub-directories are processed in a random order. This
+spreads out the scanning of the input directories, and uses less memory. It is
+particularly beneficial when there are lots of messages on the queue. However,
+if queue_run_in_order is set, none of this new processing happens. The entire
+queue has to be scanned and sorted before any deliveries can start.
+
++---------------+---------+-------------+----------------------------+
+|spool_directory|Use: main|Type: string*|Default: set at compile time|
++---------------+---------+-------------+----------------------------+
+
+This defines the directory in which Exim keeps its spool, that is, the messages
+it is waiting to deliver. The default value is taken from the compile-time
+configuration setting, if there is one. If not, this option must be set. The
+string is expanded, so it can contain, for example, a reference to
+$primary_hostname.
+
+If the spool directory name is fixed on your installation, it is recommended
+that you set it at build time rather than from this option, particularly if the
+log files are being written to the spool directory (see log_file_path).
+Otherwise log files cannot be used for errors that are detected early on, such
+as failures in the configuration file.
+
+By using this option to override the compiled-in path, it is possible to run
+tests of Exim without using the standard spool.
+
++-------------------+---------+----------+-----------+
+|sqlite_lock_timeout|Use: main|Type: time|Default: 5s|
++-------------------+---------+----------+-----------+
+
+This option controls the timeout that the sqlite lookup uses when trying to
+access an SQLite database. See section 9.25 for more details.
+
++---------------+---------+-------------+--------------+
+|strict_acl_vars|Use: main|Type: boolean|Default: false|
++---------------+---------+-------------+--------------+
+
+This option controls what happens if a syntactically valid but undefined ACL
+variable is referenced. If it is false (the default), an empty string is
+substituted; if it is true, an error is generated. See section 42.19 for
+details of ACL variables.
+
++---------------------------+---------+-------------+--------------+
+|strip_excess_angle_brackets|Use: main|Type: boolean|Default: false|
++---------------------------+---------+-------------+--------------+
+
+If this option is set, redundant pairs of angle brackets round "route-addr"
+items in addresses are stripped. For example, <<xxx@a.b.c.d>> is treated as 
+<xxx@a.b.c.d>. If this is in the envelope and the message is passed on to
+another MTA, the excess angle brackets are not passed on. If this option is not
+set, multiple pairs of angle brackets cause a syntax error.
+
++------------------+---------+-------------+--------------+
+|strip_trailing_dot|Use: main|Type: boolean|Default: false|
++------------------+---------+-------------+--------------+
+
+If this option is set, a trailing dot at the end of a domain in an address is
+ignored. If this is in the envelope and the message is passed on to another
+MTA, the dot is not passed on. If this option is not set, a dot at the end of a
+domain causes a syntax error. However, addresses in header lines are checked
+only when an ACL requests header syntax checking.
+
++------------------+---------+-------------+-------------+
+|syslog_duplication|Use: main|Type: boolean|Default: true|
++------------------+---------+-------------+-------------+
+
+When Exim is logging to syslog, it writes the log lines for its three separate
+logs at different syslog priorities so that they can in principle be separated
+on the logging hosts. Some installations do not require this separation, and in
+those cases, the duplication of certain log lines is a nuisance. If 
+syslog_duplication is set false, only one copy of any particular log line is
+written to syslog. For lines that normally go to both the main log and the
+reject log, the reject log version (possibly containing message header lines)
+is written, at LOG_NOTICE priority. Lines that normally go to both the main and
+the panic log are written at the LOG_ALERT priority.
+
++---------------+---------+------------+--------------+
+|syslog_facility|Use: main|Type: string|Default: unset|
++---------------+---------+------------+--------------+
+
+This option sets the syslog "facility" name, used when Exim is logging to
+syslog. The value must be one of the strings "mail", "user", "news", "uucp",
+"daemon", or "localx" where x is a digit between 0 and 7. If this option is
+unset, "mail" is used. See chapter 51 for details of Exim's logging.
+
++------------------+---------+------------+---------------+
+|syslog_processname|Use: main|Type: string|Default: "exim"|
++------------------+---------+------------+---------------+
+
+This option sets the syslog "ident" name, used when Exim is logging to syslog.
+The value must be no longer than 32 characters. See chapter 51 for details of
+Exim's logging.
+
++----------------+---------+-------------+-------------+
+|syslog_timestamp|Use: main|Type: boolean|Default: true|
++----------------+---------+-------------+-------------+
+
+If syslog_timestamp is set false, the timestamps on Exim's log lines are
+omitted when these lines are sent to syslog. See chapter 51 for details of
+Exim's logging.
+
++-------------+---------+-------------+--------------+
+|system_filter|Use: main|Type: string*|Default: unset|
++-------------+---------+-------------+--------------+
+
+This option specifies an Exim filter file that is applied to all messages at
+the start of each delivery attempt, before any routing is done. System filters
+must be Exim filters; they cannot be Sieve filters. If the system filter
+generates any deliveries to files or pipes, or any new mail messages, the
+appropriate system_filter_..._transport option(s) must be set, to define which
+transports are to be used. Details of this facility are given in chapter 45.
+
++---------------------------------+---------+-------------+--------------+
+|system_filter_directory_transport|Use: main|Type: string*|Default: unset|
++---------------------------------+---------+-------------+--------------+
+
+This sets the name of the transport driver that is to be used when the save
+command in a system message filter specifies a path ending in "/", implying
+delivery of each message into a separate file in some directory. During the
+delivery, the variable $address_file contains the path name.
+
++----------------------------+---------+-------------+--------------+
+|system_filter_file_transport|Use: main|Type: string*|Default: unset|
++----------------------------+---------+-------------+--------------+
+
+This sets the name of the transport driver that is to be used when the save
+command in a system message filter specifies a path not ending in "/". During
+the delivery, the variable $address_file contains the path name.
+
++-------------------+---------+------------+--------------+
+|system_filter_group|Use: main|Type: string|Default: unset|
++-------------------+---------+------------+--------------+
+
+This option is used only when system_filter_user is also set. It sets the gid
+under which the system filter is run, overriding any gid that is associated
+with the user. The value may be numerical or symbolic.
+
++----------------------------+---------+-------------+--------------+
+|system_filter_pipe_transport|Use: main|Type: string*|Default: unset|
++----------------------------+---------+-------------+--------------+
+
+This specifies the transport driver that is to be used when a pipe command is
+used in a system filter. During the delivery, the variable $address_pipe
+contains the pipe command.
+
++-----------------------------+---------+-------------+--------------+
+|system_filter_reply_transport|Use: main|Type: string*|Default: unset|
++-----------------------------+---------+-------------+--------------+
+
+This specifies the transport driver that is to be used when a mail command is
+used in a system filter.
+
++------------------+---------+------------+--------------+
+|system_filter_user|Use: main|Type: string|Default: unset|
++------------------+---------+------------+--------------+
+
+If this option is set to root, the system filter is run in the main Exim
+delivery process, as root. Otherwise, the system filter runs in a separate
+process, as the given user, defaulting to the Exim run-time user. Unless the
+string consists entirely of digits, it is looked up in the password data.
+Failure to find the named user causes a configuration error. The gid is either
+taken from the password data, or specified by system_filter_group. When the uid
+is specified numerically, system_filter_group is required to be set.
+
+If the system filter generates any pipe, file, or reply deliveries, the uid
+under which the filter is run is used when transporting them, unless a
+transport option overrides.
+
++-----------+---------+-------------+-------------+
+|tcp_nodelay|Use: main|Type: boolean|Default: true|
++-----------+---------+-------------+-------------+
+
+If this option is set false, it stops the Exim daemon setting the TCP_NODELAY
+option on its listening sockets. Setting TCP_NODELAY turns off the "Nagle
+algorithm", which is a way of improving network performance in interactive
+(character-by-character) situations. Turning it off should improve Exim's
+performance a bit, so that is what happens by default. However, it appears that
+some broken clients cannot cope, and time out. Hence this option. It affects
+only those sockets that are set up for listening by the daemon. Sockets created
+by the smtp transport for delivering mail always set TCP_NODELAY.
+
++--------------------+---------+----------+-----------+
+|timeout_frozen_after|Use: main|Type: time|Default: 0s|
++--------------------+---------+----------+-----------+
+
+If timeout_frozen_after is set to a time greater than zero, a frozen message of
+any kind that has been on the queue for longer than the given time is
+automatically cancelled at the next queue run. If the frozen message is a
+bounce message, it is just discarded; otherwise, a bounce is sent to the
+sender, in a similar manner to cancellation by the -Mg command line option. If
+you want to timeout frozen bounce messages earlier than other kinds of frozen
+message, see ignore_bounce_errors_after.
+
+Note: the default value of zero means no timeouts; with this setting, frozen
+messages remain on the queue forever (except for any frozen bounce messages
+that are released by ignore_bounce_errors_after).
+
++--------+---------+------------+--------------+
+|timezone|Use: main|Type: string|Default: unset|
++--------+---------+------------+--------------+
+
+The value of timezone is used to set the environment variable TZ while running
+Exim (if it is different on entry). This ensures that all timestamps created by
+Exim are in the required timezone. If you want all your timestamps to be in UTC
+(aka GMT) you should set
+
+timezone = UTC
+
+The default value is taken from TIMEZONE_DEFAULT in Local/Makefile, or, if that
+is not set, from the value of the TZ environment variable when Exim is built.
+If timezone is set to the empty string, either at build or run time, any
+existing TZ variable is removed from the environment when Exim runs. This is
+appropriate behaviour for obtaining wall-clock time on some, but unfortunately
+not all, operating systems.
+
++-------------------+---------+----------------+--------------+
+|tls_advertise_hosts|Use: main|Type: host list*|Default: unset|
++-------------------+---------+----------------+--------------+
+
+When Exim is built with support for TLS encrypted connections, the availability
+of the STARTTLS command to set up an encrypted session is advertised in
+response to EHLO only to those client hosts that match this option. See chapter
+41 for details of Exim's support for TLS.
+
++---------------+---------+-------------+--------------+
+|tls_certificate|Use: main|Type: string*|Default: unset|
++---------------+---------+-------------+--------------+
+
+The value of this option is expanded, and must then be the absolute path to a
+file which contains the server's certificates. The server's private key is also
+assumed to be in this file if tls_privatekey is unset. See chapter 41 for
+further details.
+
+Note: The certificates defined by this option are used only when Exim is
+receiving incoming messages as a server. If you want to supply certificates for
+use when sending messages as a client, you must set the tls_certificate option
+in the relevant smtp transport.
+
+If the option contains $tls_out_sni and Exim is built against OpenSSL, then if
+the OpenSSL build supports TLS extensions and the TLS client sends the Server
+Name Indication extension, then this option and others documented in 41.10 will
+be re-expanded.
+
++-------+---------+-------------+--------------+
+|tls_crl|Use: main|Type: string*|Default: unset|
++-------+---------+-------------+--------------+
+
+This option specifies a certificate revocation list. The expanded value must be
+the name of a file that contains a CRL in PEM format.
+
+See 41.10 for discussion of when this option might be re-expanded.
+
++---------------+---------+-------------+-------------+
+|tls_dh_max_bits|Use: main|Type: integer|Default: 2236|
++---------------+---------+-------------+-------------+
+
+The number of bits used for Diffie-Hellman key-exchange may be suggested by the
+chosen TLS library. That value might prove to be too high for interoperability.
+This option provides a maximum clamp on the value suggested, trading off
+security for interoperability.
+
+The value must be at least 1024.
+
+The value 2236 was chosen because, at time of adding the option, it was the
+hard-coded maximum value supported by the NSS cryptographic library, as used by
+Thunderbird, while GnuTLS was suggesting 2432 bits as normal.
+
+If you prefer more security and are willing to break some clients, raise this
+number.
+
+Note that the value passed to GnuTLS for *generating* a new prime may be a
+little less than this figure, because GnuTLS is inexact and may produce a
+larger prime than requested.
+
++-----------+---------+-------------+--------------+
+|tls_dhparam|Use: main|Type: string*|Default: unset|
++-----------+---------+-------------+--------------+
+
+The value of this option is expanded and indicates the source of DH parameters
+to be used by Exim.
+
+If it is a filename starting with a "/", then it names a file from which DH
+parameters should be loaded. If the file exists, it should hold a PEM-encoded
+PKCS#3 representation of the DH prime. If the file does not exist, for OpenSSL
+it is an error. For GnuTLS, Exim will attempt to create the file and fill it
+with a generated DH prime. For OpenSSL, if the DH bit-count from loading the
+file is greater than tls_dh_max_bits then it will be ignored, and treated as
+though the tls_dhparam were set to "none".
+
+If this option expands to the string "none", then no DH parameters will be
+loaded by Exim.
+
+If this option expands to the string "historic" and Exim is using GnuTLS, then
+Exim will attempt to load a file from inside the spool directory. If the file
+does not exist, Exim will attempt to create it. See section 41.3 for further
+details.
+
+If Exim is using OpenSSL and this option is empty or unset, then Exim will load
+a default DH prime; the default is the 2048 bit prime described in section 2.2
+of RFC 5114, "2048-bit MODP Group with 224-bit Prime Order Subgroup", which in
+IKE is assigned number 23.
+
+Otherwise, the option must expand to the name used by Exim for any of a number
+of DH primes specified in RFC 2409, RFC 3526 and RFC 5114. As names, Exim uses
+"ike" followed by the number used by IKE, of "default" which corresponds to
+"ike23".
+
+The available primes are: "ike1", "ike2", "ike5", "ike14", "ike15", "ike16",
+"ike17", "ike18", "ike22", "ike23" (aka "default") and "ike24".
+
+Some of these will be too small to be accepted by clients. Some may be too
+large to be accepted by clients.
+
+The TLS protocol does not negotiate an acceptable size for this; clients tend
+to hard-drop connections if what is offered by the server is unacceptable,
+whether too large or too small, and there's no provision for the client to tell
+the server what these constraints are. Thus, as a server operator, you need to
+make an educated guess as to what is most likely to work for your userbase.
+
+Some known size constraints suggest that a bit-size in the range 2048 to 2236
+is most likely to maximise interoperability. The upper bound comes from
+applications using the Mozilla Network Security Services (NSS) library, which
+used to set its "DH_MAX_P_BITS" upper-bound to 2236. This affects many mail
+user agents (MUAs). The lower bound comes from Debian installs of Exim4 prior
+to the 4.80 release, as Debian used to patch Exim to raise the minimum
+acceptable bound from 1024 to 2048.
+
++-------------+---------+-------------+--------------+
+|tls_ocsp_file|Use: main|Type: string*|Default: unset|
++-------------+---------+-------------+--------------+
+
+This option must if set expand to the absolute path to a file which contains a
+current status proof for the server's certificate, as obtained from the
+Certificate Authority.
+
++--------------------+---------+-----------------+--------------+
+|tls_on_connect_ports|Use: main|Type: string list|Default: unset|
++--------------------+---------+-----------------+--------------+
+
+This option specifies a list of incoming SSMTP (aka SMTPS) ports that should
+operate the obsolete SSMTP (SMTPS) protocol, where a TLS session is immediately
+set up without waiting for the client to issue a STARTTLS command. For further
+details, see section 13.4.
+
++--------------+---------+-------------+--------------+
+|tls_privatekey|Use: main|Type: string*|Default: unset|
++--------------+---------+-------------+--------------+
+
+The value of this option is expanded, and must then be the absolute path to a
+file which contains the server's private key. If this option is unset, or if
+the expansion is forced to fail, or the result is an empty string, the private
+key is assumed to be in the same file as the server's certificates. See chapter
+41 for further details.
+
+See 41.10 for discussion of when this option might be re-expanded.
+
++------------------+---------+-------------+--------------+
+|tls_remember_esmtp|Use: main|Type: boolean|Default: false|
++------------------+---------+-------------+--------------+
+
+If this option is set true, Exim violates the RFCs by remembering that it is in
+"esmtp" state after successfully negotiating a TLS session. This provides
+support for broken clients that fail to send a new EHLO after starting a TLS
+session.
+
++-------------------+---------+-------------+--------------+
+|tls_require_ciphers|Use: main|Type: string*|Default: unset|
++-------------------+---------+-------------+--------------+
+
+This option controls which ciphers can be used for incoming TLS connections.
+The smtp transport has an option of the same name for controlling outgoing
+connections. This option is expanded for each connection, so can be varied for
+different clients if required. The value of this option must be a list of
+permitted cipher suites. The OpenSSL and GnuTLS libraries handle cipher control
+in somewhat different ways. If GnuTLS is being used, the client controls the
+preference order of the available ciphers. Details are given in sections 41.4
+and 41.5.
+
++--------------------+---------+----------------+--------------+
+|tls_try_verify_hosts|Use: main|Type: host list*|Default: unset|
++--------------------+---------+----------------+--------------+
+
+See tls_verify_hosts below.
+
++-----------------------+---------+-------------+--------------+
+|tls_verify_certificates|Use: main|Type: string*|Default: unset|
++-----------------------+---------+-------------+--------------+
+
+The value of this option is expanded, and must then be the absolute path to a
+file containing permitted certificates for clients that match tls_verify_hosts
+or tls_try_verify_hosts. Alternatively, if you are using OpenSSL, you can set 
+tls_verify_certificates to the name of a directory containing certificate
+files. This does not work with GnuTLS; the option must be set to the name of a
+single file if you are using GnuTLS.
+
+These certificates should be for the certificate authorities trusted, rather
+than the public cert of individual clients. With both OpenSSL and GnuTLS, if
+the value is a file then the certificates are sent by Exim as a server to
+connecting clients, defining the list of accepted certificate authorities. Thus
+the values defined should be considered public data. To avoid this, use OpenSSL
+with a directory.
+
+See 41.10 for discussion of when this option might be re-expanded.
+
+A forced expansion failure or setting to an empty string is equivalent to being
+unset.
+
++----------------+---------+----------------+--------------+
+|tls_verify_hosts|Use: main|Type: host list*|Default: unset|
++----------------+---------+----------------+--------------+
+
+This option, along with tls_try_verify_hosts, controls the checking of
+certificates from clients. The expected certificates are defined by 
+tls_verify_certificates, which must be set. A configuration error occurs if
+either tls_verify_hosts or tls_try_verify_hosts is set and 
+tls_verify_certificates is not set.
+
+Any client that matches tls_verify_hosts is constrained by 
+tls_verify_certificates. When the client initiates a TLS session, it must
+present one of the listed certificates. If it does not, the connection is
+aborted. Warning: Including a host in tls_verify_hosts does not require the
+host to use TLS. It can still send SMTP commands through unencrypted
+connections. Forcing a client to use TLS has to be done separately using an ACL
+to reject inappropriate commands when the connection is not encrypted.
+
+A weaker form of checking is provided by tls_try_verify_hosts. If a client
+matches this option (but not tls_verify_hosts), Exim requests a certificate and
+checks it against tls_verify_certificates, but does not abort the connection if
+there is no certificate or if it does not match. This state can be detected in
+an ACL, which makes it possible to implement policies such as "accept for relay
+only if a verified certificate has been received, but accept for local delivery
+if encrypted, even without a verified certificate".
+
+Client hosts that match neither of these lists are not asked to present
+certificates.
+
++--------------+---------+------------------+--------------+
+|trusted_groups|Use: main|Type: string list*|Default: unset|
++--------------+---------+------------------+--------------+
+
+This option is expanded just once, at the start of Exim's processing. If this
+option is set, any process that is running in one of the listed groups, or
+which has one of them as a supplementary group, is trusted. The groups can be
+specified numerically or by name. See section 5.2 for details of what trusted
+callers are permitted to do. If neither trusted_groups nor trusted_users is
+set, only root and the Exim user are trusted.
+
++-------------+---------+------------------+--------------+
+|trusted_users|Use: main|Type: string list*|Default: unset|
++-------------+---------+------------------+--------------+
+
+This option is expanded just once, at the start of Exim's processing. If this
+option is set, any process that is running as one of the listed users is
+trusted. The users can be specified numerically or by name. See section 5.2 for
+details of what trusted callers are permitted to do. If neither trusted_groups
+nor trusted_users is set, only root and the Exim user are trusted.
+
++-------------+---------+-------------+--------------+
+|unknown_login|Use: main|Type: string*|Default: unset|
++-------------+---------+-------------+--------------+
+
+This is a specialized feature for use in unusual configurations. By default, if
+the uid of the caller of Exim cannot be looked up using getpwuid(), Exim gives
+up. The unknown_login option can be used to set a login name to be used in this
+circumstance. It is expanded, so values like user$caller_uid can be set. When 
+unknown_login is used, the value of unknown_username is used for the user's
+real name (gecos field), unless this has been set by the -F option.
+
++----------------+---------+------------+--------------+
+|unknown_username|Use: main|Type: string|Default: unset|
++----------------+---------+------------+--------------+
+
+See unknown_login.
+
++--------------------+---------+-------------------+--------------+
+|untrusted_set_sender|Use: main|Type: address list*|Default: unset|
++--------------------+---------+-------------------+--------------+
+
+When an untrusted user submits a message to Exim using the standard input, Exim
+normally creates an envelope sender address from the user's login and the
+default qualification domain. Data from the -f option (for setting envelope
+senders on non-SMTP messages) or the SMTP MAIL command (if -bs or -bS is used)
+is ignored.
+
+However, untrusted users are permitted to set an empty envelope sender address,
+to declare that a message should never generate any bounces. For example:
+
+exim -f '<>' user@domain.example
+
+The untrusted_set_sender option allows you to permit untrusted users to set
+other envelope sender addresses in a controlled way. When it is set, untrusted
+users are allowed to set envelope sender addresses that match any of the
+patterns in the list. Like all address lists, the string is expanded. The
+identity of the user is in $sender_ident, so you can, for example, restrict
+users to setting senders that start with their login ids followed by a hyphen
+by a setting like this:
+
+untrusted_set_sender = ^$sender_ident-
+
+If you want to allow untrusted users to set envelope sender addresses without
+restriction, you can use
+
+untrusted_set_sender = *
+
+The untrusted_set_sender option applies to all forms of local input, but only
+to the setting of the envelope sender. It does not permit untrusted users to
+use the other options which trusted user can use to override message
+parameters. Furthermore, it does not stop Exim from removing an existing 
+Sender: header in the message, or from adding a Sender: header if necessary.
+See local_sender_retain and local_from_check for ways of overriding these
+actions. The handling of the Sender: header is also described in section 46.16.
+
+The log line for a message's arrival shows the envelope sender following "<=".
+For local messages, the user's login always follows, after "U=". In -bp
+displays, and in the Exim monitor, if an untrusted user sets an envelope sender
+address, the user's login is shown in parentheses after the sender address.
+
++-----------------+---------+------------+------------------+
+|uucp_from_pattern|Use: main|Type: string|Default: see below|
++-----------------+---------+------------+------------------+
+
+Some applications that pass messages to an MTA via a command line interface use
+an initial line starting with "From " to pass the envelope sender. In
+particular, this is used by UUCP software. Exim recognizes such a line by means
+of a regular expression that is set in uucp_from_pattern. When the pattern
+matches, the sender address is constructed by expanding the contents of 
+uucp_from_sender, provided that the caller of Exim is a trusted user. The
+default pattern recognizes lines in the following two forms:
+
+From ph10 Fri Jan  5 12:35 GMT 1996
+From ph10 Fri, 7 Jan 97 14:00:00 GMT
+
+The pattern can be seen by running
+
+exim -bP uucp_from_pattern
+
+It checks only up to the hours and minutes, and allows for a 2-digit or 4-digit
+year in the second case. The first word after "From " is matched in the regular
+expression by a parenthesized subpattern. The default value for 
+uucp_from_sender is "$1", which therefore just uses this first word ("ph10" in
+the example above) as the message's sender. See also ignore_fromline_hosts.
+
++----------------+---------+-------------+-------------+
+|uucp_from_sender|Use: main|Type: string*|Default: "$1"|
++----------------+---------+-------------+-------------+
+
+See uucp_from_pattern above.
+
++-----------------+---------+------------+--------------+
+|warn_message_file|Use: main|Type: string|Default: unset|
++-----------------+---------+------------+--------------+
+
+This option defines a template file containing paragraphs of text to be used
+for constructing the warning message which is sent by Exim when a message has
+been on the queue for a specified amount of time, as specified by delay_warning
+. Details of the file's contents are given in chapter 48. See also 
+bounce_message_file.
+
++---------------+---------+-------------+-------------+
+|write_rejectlog|Use: main|Type: boolean|Default: true|
++---------------+---------+-------------+-------------+
+
+If this option is set false, Exim no longer writes anything to the reject log.
+See chapter 51 for details of what Exim writes to its logs.
+
+
+
+===============================================================================
+15. GENERIC OPTIONS FOR ROUTERS
+
+This chapter describes the generic options that apply to all routers. Those
+that are preconditions are marked with ** in the "use" field.
+
+For a general description of how a router operates, see sections 3.10 and 3.12.
+The latter specifies the order in which the preconditions are tested. The order
+of expansion of the options that provide data for a transport is: errors_to, 
+headers_add, headers_remove, transport.
+
++------------+------------+-------------+--------------+
+|address_data|Use: routers|Type: string*|Default: unset|
++------------+------------+-------------+--------------+
+
+The string is expanded just before the router is run, that is, after all the
+precondition tests have succeeded. If the expansion is forced to fail, the
+router declines, the value of address_data remains unchanged, and the more
+option controls what happens next. Other expansion failures cause delivery of
+the address to be deferred.
+
+When the expansion succeeds, the value is retained with the address, and can be
+accessed using the variable $address_data in the current router, subsequent
+routers, and the eventual transport.
+
+Warning: If the current or any subsequent router is a redirect router that runs
+a user's filter file, the contents of $address_data are accessible in the
+filter. This is not normally a problem, because such data is usually either not
+confidential or it "belongs" to the current user, but if you do put
+confidential data into $address_data you need to remember this point.
+
+Even if the router declines or passes, the value of $address_data remains with
+the address, though it can be changed by another address_data setting on a
+subsequent router. If a router generates child addresses, the value of
+$address_data propagates to them. This also applies to the special kind of
+"child" that is generated by a router with the unseen option.
+
+The idea of address_data is that you can use it to look up a lot of data for
+the address once, and then pick out parts of the data later. For example, you
+could use a single LDAP lookup to return a string of the form
+
+uid=1234 gid=5678 mailbox=/mail/xyz forward=/home/xyz/.forward
+
+In the transport you could pick out the mailbox by a setting such as
+
+file = ${extract{mailbox}{$address_data}}
+
+This makes the configuration file less messy, and also reduces the number of
+lookups (though Exim does cache lookups).
+
+The address_data facility is also useful as a means of passing information from
+one router to another, and from a router to a transport. In addition, if
+$address_data is set by a router when verifying a recipient address from an
+ACL, it remains available for use in the rest of the ACL statement. After
+verifying a sender, the value is transferred to $sender_address_data.
+
++------------+--------------+-------------+-------------+
+|address_test|Use: routers**|Type: boolean|Default: true|
++------------+--------------+-------------+-------------+
+
+If this option is set false, the router is skipped when routing is being tested
+by means of the -bt command line option. This can be a convenience when your
+first router sends messages to an external scanner, because it saves you having
+to set the "already scanned" indicator when testing real address routing.
+
++--------------------+------------+-------------+--------------+
+|cannot_route_message|Use: routers|Type: string*|Default: unset|
++--------------------+------------+-------------+--------------+
+
+This option specifies a text message that is used when an address cannot be
+routed because Exim has run out of routers. The default message is "Unrouteable
+address". This option is useful only on routers that have more set false, or on
+the very last router in a configuration, because the value that is used is
+taken from the last router that is considered. This includes a router that is
+skipped because its preconditions are not met, as well as a router that
+declines. For example, using the default configuration, you could put:
+
+cannot_route_message = Remote domain not found in DNS
+
+on the first router, which is a dnslookup router with more set false, and
+
+cannot_route_message = Unknown local user
+
+on the final router that checks for local users. If string expansion fails for
+this option, the default message is used. Unless the expansion failure was
+explicitly forced, a message about the failure is written to the main and panic
+logs, in addition to the normal message about the routing failure.
+
++------------------+------------+-------------+--------------+
+|caseful_local_part|Use: routers|Type: boolean|Default: false|
++------------------+------------+-------------+--------------+
+
+By default, routers handle the local parts of addresses in a case-insensitive
+manner, though the actual case is preserved for transmission with the message.
+If you want the case of letters to be significant in a router, you must set
+this option true. For individual router options that contain address or local
+part lists (for example, local_parts), case-sensitive matching can be turned on
+by "+caseful" as a list item. See section 10.20 for more details.
+
+The value of the $local_part variable is forced to lower case while a router is
+running unless caseful_local_part is set. When a router assigns an address to a
+transport, the value of $local_part when the transport runs is the same as it
+was in the router. Similarly, when a router generates child addresses by
+aliasing or forwarding, the values of $original_local_part and
+$parent_local_part are those that were used by the redirecting router.
+
+This option applies to the processing of an address by a router. When a
+recipient address is being processed in an ACL, there is a separate control
+modifier that can be used to specify case-sensitive processing within the ACL
+(see section 42.22).
+
++----------------+--------------+-------------+--------------+
+|check_local_user|Use: routers**|Type: boolean|Default: false|
++----------------+--------------+-------------+--------------+
+
+When this option is true, Exim checks that the local part of the recipient
+address (with affixes removed if relevant) is the name of an account on the
+local system. The check is done by calling the getpwnam() function rather than
+trying to read /etc/passwd directly. This means that other methods of holding
+password data (such as NIS) are supported. If the local part is a local user,
+$home is set from the password data, and can be tested in other preconditions
+that are evaluated after this one (the order of evaluation is given in section
+3.12). However, the value of $home can be overridden by router_home_directory.
+If the local part is not a local user, the router is skipped.
+
+If you want to check that the local part is either the name of a local user or
+matches something else, you cannot combine check_local_user with a setting of 
+local_parts, because that specifies the logical and of the two conditions.
+However, you can use a passwd lookup in a local_parts setting to achieve this.
+For example:
+
+local_parts = passwd;$local_part : lsearch;/etc/other/users
+
+Note, however, that the side effects of check_local_user (such as setting up a
+home directory) do not occur when a passwd lookup is used in a local_parts (or
+any other) precondition.
+
++---------+--------------+-------------+--------------+
+|condition|Use: routers**|Type: string*|Default: unset|
++---------+--------------+-------------+--------------+
+
+This option specifies a general precondition test that has to succeed for the
+router to be called. The condition option is the last precondition to be
+evaluated (see section 3.12). The string is expanded, and if the result is a
+forced failure, or an empty string, or one of the strings "0" or "no" or
+"false" (checked without regard to the case of the letters), the router is
+skipped, and the address is offered to the next one.
+
+If the result is any other value, the router is run (as this is the last
+precondition to be evaluated, all the other preconditions must be true).
+
+This option is unusual in that multiple condition options may be present. All 
+condition options must succeed.
+
+The condition option provides a means of applying custom conditions to the
+running of routers. Note that in the case of a simple conditional expansion,
+the default expansion values are exactly what is wanted. For example:
+
+condition = ${if >{$message_age}{600}}
+
+Because of the default behaviour of the string expansion, this is equivalent to
+
+condition = ${if >{$message_age}{600}{true}{}}
+
+A multiple condition example, which succeeds:
+
+condition = ${if >{$message_age}{600}}
+condition = ${if !eq{${lc:$local_part}}{postmaster}}
+condition = foobar
+
+If the expansion fails (other than forced failure) delivery is deferred. Some
+of the other precondition options are common special cases that could in fact
+be specified using condition.
+
++-----------+------------+-------------+--------------+
+|debug_print|Use: routers|Type: string*|Default: unset|
++-----------+------------+-------------+--------------+
+
+If this option is set and debugging is enabled (see the -d command line option)
+or in address-testing mode (see the -bt command line option), the string is
+expanded and included in the debugging output. If expansion of the string
+fails, the error message is written to the debugging output, and Exim carries
+on processing. This option is provided to help with checking out the values of
+variables and so on when debugging router configurations. For example, if a 
+condition option appears not to be working, debug_print can be used to output
+the variables it references. The output happens after checks for domains, 
+local_parts, and check_local_user but before any other preconditions are
+tested. A newline is added to the text if it does not end with one. The
+variable $router_name contains the name of the router.
+
++---------------+------------+-------------+--------------+
+|disable_logging|Use: routers|Type: boolean|Default: false|
++---------------+------------+-------------+--------------+
+
+If this option is set true, nothing is logged for any routing errors or for any
+deliveries caused by this router. You should not set this option unless you
+really, really know what you are doing. See also the generic transport option
+of the same name.
+
++-------+--------------+------------------+--------------+
+|domains|Use: routers**|Type: domain list*|Default: unset|
++-------+--------------+------------------+--------------+
+
+If this option is set, the router is skipped unless the current domain matches
+the list. If the match is achieved by means of a file lookup, the data that the
+lookup returned for the domain is placed in $domain_data for use in string
+expansions of the driver's private options. See section 3.12 for a list of the
+order in which preconditions are evaluated.
+
++------+------------+------------+--------------+
+|driver|Use: routers|Type: string|Default: unset|
++------+------------+------------+--------------+
+
+This option must always be set. It specifies which of the available routers is
+to be used.
+
++---------+------------+-------------+--------------+
+|errors_to|Use: routers|Type: string*|Default: unset|
++---------+------------+-------------+--------------+
+
+If a router successfully handles an address, it may assign the address to a
+transport for delivery or it may generate child addresses. In both cases, if
+there is a delivery problem during later processing, the resulting bounce
+message is sent to the address that results from expanding this string,
+provided that the address verifies successfully. The errors_to option is
+expanded before headers_add, headers_remove, and transport.
+
+The errors_to setting associated with an address can be overridden if it
+subsequently passes through other routers that have their own errors_to
+settings, or if the message is delivered by a transport with a return_path
+setting.
+
+If errors_to is unset, or the expansion is forced to fail, or the result of the
+expansion fails to verify, the errors address associated with the incoming
+address is used. At top level, this is the envelope sender. A non-forced
+expansion failure causes delivery to be deferred.
+
+If an address for which errors_to has been set ends up being delivered over
+SMTP, the envelope sender for that delivery is the errors_to value, so that any
+bounces that are generated by other MTAs on the delivery route are also sent
+there. You can set errors_to to the empty string by either of these settings:
+
+errors_to =
+errors_to = ""
+
+An expansion item that yields an empty string has the same effect. If you do
+this, a locally detected delivery error for addresses processed by this router
+no longer gives rise to a bounce message; the error is discarded. If the
+address is delivered to a remote host, the return path is set to "<>", unless
+overridden by the return_path option on the transport.
+
+If for some reason you want to discard local errors, but use a non-empty MAIL
+command for remote delivery, you can preserve the original return path in
+$address_data in the router, and reinstate it in the transport by setting 
+return_path.
+
+The most common use of errors_to is to direct mailing list bounces to the
+manager of the list, as described in section 49.2, or to implement VERP
+(Variable Envelope Return Paths) (see section 49.6).
+
++----+--------------+-------------+-------------+
+|expn|Use: routers**|Type: boolean|Default: true|
++----+--------------+-------------+-------------+
+
+If this option is turned off, the router is skipped when testing an address as
+a result of processing an SMTP EXPN command. You might, for example, want to
+turn it off on a router for users' .forward files, while leaving it on for the
+system alias file. See section 3.12 for a list of the order in which
+preconditions are evaluated.
+
+The use of the SMTP EXPN command is controlled by an ACL (see chapter 42). When
+Exim is running an EXPN command, it is similar to testing an address with -bt.
+Compare VRFY, whose counterpart is -bv.
+
++-----------+------------+-------------+--------------+
+|fail_verify|Use: routers|Type: boolean|Default: false|
++-----------+------------+-------------+--------------+
+
+Setting this option has the effect of setting both fail_verify_sender and 
+fail_verify_recipient to the same value.
+
++---------------------+------------+-------------+--------------+
+|fail_verify_recipient|Use: routers|Type: boolean|Default: false|
++---------------------+------------+-------------+--------------+
+
+If this option is true and an address is accepted by this router when verifying
+a recipient, verification fails.
+
++------------------+------------+-------------+--------------+
+|fail_verify_sender|Use: routers|Type: boolean|Default: false|
++------------------+------------+-------------+--------------+
+
+If this option is true and an address is accepted by this router when verifying
+a sender, verification fails.
+
++--------------+------------+-----------------+--------------+
+|fallback_hosts|Use: routers|Type: string list|Default: unset|
++--------------+------------+-----------------+--------------+
+
+String expansion is not applied to this option. The argument must be a
+colon-separated list of host names or IP addresses. The list separator can be
+changed (see section 6.19), and a port can be specified with each name or
+address. In fact, the format of each item is exactly the same as defined for
+the list of hosts in a manualroute router (see section 20.5).
+
+If a router queues an address for a remote transport, this host list is
+associated with the address, and used instead of the transport's fallback host
+list. If hosts_randomize is set on the transport, the order of the list is
+randomized for each use. See the fallback_hosts option of the smtp transport
+for further details.
+
++-----+------------+-------------+------------------+
+|group|Use: routers|Type: string*|Default: see below|
++-----+------------+-------------+------------------+
+
+When a router queues an address for a transport, and the transport does not
+specify a group, the group given here is used when running the delivery
+process. The group may be specified numerically or by name. If expansion fails,
+the error is logged and delivery is deferred. The default is unset, unless 
+check_local_user is set, when the default is taken from the password
+information. See also initgroups and user and the discussion in chapter 23.
+
++-----------+------------+-----------+--------------+
+|headers_add|Use: routers|Type: list*|Default: unset|
++-----------+------------+-----------+--------------+
+
+This option specifies a list of text headers, newline-separated, that is
+associated with any addresses that are accepted by the router. Each item is
+separately expanded, at routing time. However, this option has no effect when
+an address is just being verified. The way in which the text is used to add
+header lines at transport time is described in section 46.17. New header lines
+are not actually added until the message is in the process of being
+transported. This means that references to header lines in string expansions in
+the transport's configuration do not "see" the added header lines.
+
+The headers_add option is expanded after errors_to, but before headers_remove
+and transport. If an item is empty, or if an item expansion is forced to fail,
+the item has no effect. Other expansion failures are treated as configuration
+errors.
+
+Unlike most options, headers_add can be specified multiple times for a router;
+all listed headers are added.
+
+Warning 1: The headers_add option cannot be used for a redirect router that has
+the one_time option set.
+
+Warning 2: If the unseen option is set on the router, all header additions are
+deleted when the address is passed on to subsequent routers. For a redirect
+router, if a generated address is the same as the incoming address, this can
+lead to duplicate addresses with different header modifications. Exim does not
+do duplicate deliveries (except, in certain circumstances, to pipes -- see
+section 22.7), but it is undefined which of the duplicates is discarded, so
+this ambiguous situation should be avoided. The repeat_use option of the 
+redirect router may be of help.
+
++--------------+------------+-----------+--------------+
+|headers_remove|Use: routers|Type: list*|Default: unset|
++--------------+------------+-----------+--------------+
+
+This option specifies a list of text headers, colon-separated, that is
+associated with any addresses that are accepted by the router. Each item is
+separately expanded, at routing time. However, this option has no effect when
+an address is just being verified. The way in which the text is used to remove
+header lines at transport time is described in section 46.17. Header lines are
+not actually removed until the message is in the process of being transported.
+This means that references to header lines in string expansions in the
+transport's configuration still "see" the original header lines.
+
+The headers_remove option is expanded after errors_to and headers_add, but
+before transport. If an item expansion is forced to fail, the item has no
+effect. Other expansion failures are treated as configuration errors.
+
+Unlike most options, headers_remove can be specified multiple times for a
+router; all listed headers are removed.
+
+Warning 1: The headers_remove option cannot be used for a redirect router that
+has the one_time option set.
+
+Warning 2: If the unseen option is set on the router, all header removal
+requests are deleted when the address is passed on to subsequent routers, and
+this can lead to problems with duplicates -- see the similar warning for 
+headers_add above.
+
++-------------------+------------+----------------+--------------+
+|ignore_target_hosts|Use: routers|Type: host list*|Default: unset|
++-------------------+------------+----------------+--------------+
+
+Although this option is a host list, it should normally contain IP address
+entries rather than names. If any host that is looked up by the router has an
+IP address that matches an item in this list, Exim behaves as if that IP
+address did not exist. This option allows you to cope with rogue DNS entries
+like
+
+remote.domain.example.  A  127.0.0.1
+
+by setting
+
+ignore_target_hosts = 127.0.0.1
+
+on the relevant router. If all the hosts found by a dnslookup router are
+discarded in this way, the router declines. In a conventional configuration, an
+attempt to mail to such a domain would normally provoke the "unrouteable
+domain" error, and an attempt to verify an address in the domain would fail.
+Similarly, if ignore_target_hosts is set on an ipliteral router, the router
+declines if presented with one of the listed addresses.
+
+You can use this option to disable the use of IPv4 or IPv6 for mail delivery by
+means of the first or the second of the following settings, respectively:
+
+ignore_target_hosts = 0.0.0.0/0
+ignore_target_hosts = <; 0::0/0
+
+The pattern in the first line matches all IPv4 addresses, whereas the pattern
+in the second line matches all IPv6 addresses.
+
+This option may also be useful for ignoring link-local and site-local IPv6
+addresses. Because, like all host lists, the value of ignore_target_hosts is
+expanded before use as a list, it is possible to make it dependent on the
+domain that is being routed.
+
+During its expansion, $host_address is set to the IP address that is being
+checked.
+
++----------+------------+-------------+--------------+
+|initgroups|Use: routers|Type: boolean|Default: false|
++----------+------------+-------------+--------------+
+
+If the router queues an address for a transport, and this option is true, and
+the uid supplied by the router is not overridden by the transport, the
+initgroups() function is called when running the transport to ensure that any
+additional groups associated with the uid are set up. See also group and user
+and the discussion in chapter 23.
+
++-----------------+--------------+-----------------+--------------+
+|local_part_prefix|Use: routers**|Type: string list|Default: unset|
++-----------------+--------------+-----------------+--------------+
+
+If this option is set, the router is skipped unless the local part starts with
+one of the given strings, or local_part_prefix_optional is true. See section
+3.12 for a list of the order in which preconditions are evaluated.
+
+The list is scanned from left to right, and the first prefix that matches is
+used. A limited form of wildcard is available; if the prefix begins with an
+asterisk, it matches the longest possible sequence of arbitrary characters at
+the start of the local part. An asterisk should therefore always be followed by
+some character that does not occur in normal local parts. Wildcarding can be
+used to set up multiple user mailboxes, as described in section 49.8.
+
+During the testing of the local_parts option, and while the router is running,
+the prefix is removed from the local part, and is available in the expansion
+variable $local_part_prefix. When a message is being delivered, if the router
+accepts the address, this remains true during subsequent delivery by a
+transport. In particular, the local part that is transmitted in the RCPT
+command for LMTP, SMTP, and BSMTP deliveries has the prefix removed by default.
+This behaviour can be overridden by setting rcpt_include_affixes true on the
+relevant transport.
+
+When an address is being verified, local_part_prefix affects only the behaviour
+of the router. If the callout feature of verification is in use, this means
+that the full address, including the prefix, will be used during the callout.
+
+The prefix facility is commonly used to handle local parts of the form 
+owner-something. Another common use is to support local parts of the form 
+real-username to bypass a user's .forward file - helpful when trying to tell a
+user their forwarding is broken - by placing a router like this one immediately
+before the router that handles .forward files:
+
+real_localuser:
+  driver = accept
+  local_part_prefix = real-
+  check_local_user
+  transport = local_delivery
+
+For security, it would probably be a good idea to restrict the use of this
+router to locally-generated messages, using a condition such as this:
+
+  condition = ${if match {$sender_host_address}\
+                         {\N^(|127\.0\.0\.1)$\N}}
+
+If both local_part_prefix and local_part_suffix are set for a router, both
+conditions must be met if not optional. Care must be taken if wildcards are
+used in both a prefix and a suffix on the same router. Different separator
+characters must be used to avoid ambiguity.
+
++--------------------------+------------+-------------+--------------+
+|local_part_prefix_optional|Use: routers|Type: boolean|Default: false|
++--------------------------+------------+-------------+--------------+
+
+See local_part_prefix above.
+
++-----------------+--------------+-----------------+--------------+
+|local_part_suffix|Use: routers**|Type: string list|Default: unset|
++-----------------+--------------+-----------------+--------------+
+
+This option operates in the same way as local_part_prefix, except that the
+local part must end (rather than start) with the given string, the 
+local_part_suffix_optional option determines whether the suffix is mandatory,
+and the wildcard * character, if present, must be the last character of the
+suffix. This option facility is commonly used to handle local parts of the form
+something-request and multiple user mailboxes of the form username-foo.
+
++--------------------------+------------+-------------+--------------+
+|local_part_suffix_optional|Use: routers|Type: boolean|Default: false|
++--------------------------+------------+-------------+--------------+
+
+See local_part_suffix above.
+
++-----------+--------------+----------------------+--------------+
+|local_parts|Use: routers**|Type: local part list*|Default: unset|
++-----------+--------------+----------------------+--------------+
+
+The router is run only if the local part of the address matches the list. See
+section 3.12 for a list of the order in which preconditions are evaluated, and
+section 10.21 for a discussion of local part lists. Because the string is
+expanded, it is possible to make it depend on the domain, for example:
+
+local_parts = dbm;/usr/local/specials/$domain
+
+If the match is achieved by a lookup, the data that the lookup returned for the
+local part is placed in the variable $local_part_data for use in expansions of
+the router's private options. You might use this option, for example, if you
+have a large number of local virtual domains, and you want to send all
+postmaster mail to the same place without having to set up an alias in each
+virtual domain:
+
+postmaster:
+  driver = redirect
+  local_parts = postmaster
+  data = postmaster@real.domain.example
+
++------------+------------+-------------+------------------+
+|log_as_local|Use: routers|Type: boolean|Default: see below|
++------------+------------+-------------+------------------+
+
+Exim has two logging styles for delivery, the idea being to make local
+deliveries stand out more visibly from remote ones. In the "local" style, the
+recipient address is given just as the local part, without a domain. The use of
+this style is controlled by this option. It defaults to true for the accept
+router, and false for all the others. This option applies only when a router
+assigns an address to a transport. It has no effect on routers that redirect
+addresses.
+
++----+------------+--------------+-------------+
+|more|Use: routers|Type: boolean*|Default: true|
++----+------------+--------------+-------------+
+
+The result of string expansion for this option must be a valid boolean value,
+that is, one of the strings "yes", "no", "true", or "false". Any other result
+causes an error, and delivery is deferred. If the expansion is forced to fail,
+the default value for the option (true) is used. Other failures cause delivery
+to be deferred.
+
+If this option is set false, and the router declines to handle the address, no
+further routers are tried, routing fails, and the address is bounced. However,
+if the router explicitly passes an address to the following router by means of
+the setting
+
+self = pass
+
+or otherwise, the setting of more is ignored. Also, the setting of more does
+not affect the behaviour if one of the precondition tests fails. In that case,
+the address is always passed to the next router.
+
+Note that address_data is not considered to be a precondition. If its expansion
+is forced to fail, the router declines, and the value of more controls what
+happens next.
+
++---------------+------------+-------------+--------------+
+|pass_on_timeout|Use: routers|Type: boolean|Default: false|
++---------------+------------+-------------+--------------+
+
+If a router times out during a host lookup, it normally causes deferral of the
+address. If pass_on_timeout is set, the address is passed on to the next
+router, overriding no_more. This may be helpful for systems that are
+intermittently connected to the Internet, or those that want to pass to a smart
+host any messages that cannot immediately be delivered.
+
+There are occasional other temporary errors that can occur while doing DNS
+lookups. They are treated in the same way as a timeout, and this option applies
+to all of them.
+
++-----------+------------+------------+--------------+
+|pass_router|Use: routers|Type: string|Default: unset|
++-----------+------------+------------+--------------+
+
+Routers that recognize the generic self option (dnslookup, ipliteral, and 
+manualroute) are able to return "pass", forcing routing to continue, and
+overriding a false setting of more. When one of these routers returns "pass",
+the address is normally handed on to the next router in sequence. This can be
+changed by setting pass_router to the name of another router. However (unlike 
+redirect_router) the named router must be below the current router, to avoid
+loops. Note that this option applies only to the special case of "pass". It
+does not apply when a router returns "decline" because it cannot handle an
+address.
+
++---------------+------------+------------+--------------+
+|redirect_router|Use: routers|Type: string|Default: unset|
++---------------+------------+------------+--------------+
+
+Sometimes an administrator knows that it is pointless to reprocess addresses
+generated from alias or forward files with the same router again. For example,
+if an alias file translates real names into login ids there is no point
+searching the alias file a second time, especially if it is a large file.
+
+The redirect_router option can be set to the name of any router instance. It
+causes the routing of any generated addresses to start at the named router
+instead of at the first router. This option has no effect if the router in
+which it is set does not generate new addresses.
+
++-------------+--------------+------------------+--------------+
+|require_files|Use: routers**|Type: string list*|Default: unset|
++-------------+--------------+------------------+--------------+
+
+This option provides a general mechanism for predicating the running of a
+router on the existence or non-existence of certain files or directories.
+Before running a router, as one of its precondition tests, Exim works its way
+through the require_files list, expanding each item separately.
+
+Because the list is split before expansion, any colons in expansion items must
+be doubled, or the facility for using a different list separator must be used.
+If any expansion is forced to fail, the item is ignored. Other expansion
+failures cause routing of the address to be deferred.
+
+If any expanded string is empty, it is ignored. Otherwise, except as described
+below, each string must be a fully qualified file path, optionally preceded by
+"!". The paths are passed to the stat() function to test for the existence of
+the files or directories. The router is skipped if any paths not preceded by "!
+" do not exist, or if any paths preceded by "!" do exist.
+
+If stat() cannot determine whether a file exists or not, delivery of the
+message is deferred. This can happen when NFS-mounted filesystems are
+unavailable.
+
+This option is checked after the domains, local_parts, and senders options, so
+you cannot use it to check for the existence of a file in which to look up a
+domain, local part, or sender. (See section 3.12 for a full list of the order
+in which preconditions are evaluated.) However, as these options are all
+expanded, you can use the exists expansion condition to make such tests. The 
+require_files option is intended for checking files that the router may be
+going to use internally, or which are needed by a transport (for example
+.procmailrc).
+
+During delivery, the stat() function is run as root, but there is a facility
+for some checking of the accessibility of a file by another user. This is not a
+proper permissions check, but just a "rough" check that operates as follows:
+
+If an item in a require_files list does not contain any forward slash
+characters, it is taken to be the user (and optional group, separated by a
+comma) to be checked for subsequent files in the list. If no group is specified
+but the user is specified symbolically, the gid associated with the uid is
+used. For example:
+
+require_files = mail:/some/file
+require_files = $local_part:$home/.procmailrc
+
+If a user or group name in a require_files list does not exist, the 
+require_files condition fails.
+
+Exim performs the check by scanning along the components of the file path, and
+checking the access for the given uid and gid. It checks for "x" access on
+directories, and "r" access on the final file. Note that this means that file
+access control lists, if the operating system has them, are ignored.
+
+Warning 1: When the router is being run to verify addresses for an incoming
+SMTP message, Exim is not running as root, but under its own uid. This may
+affect the result of a require_files check. In particular, stat() may yield the
+error EACCES ("Permission denied"). This means that the Exim user is not
+permitted to read one of the directories on the file's path.
+
+Warning 2: Even when Exim is running as root while delivering a message, stat()
+can yield EACCES for a file in an NFS directory that is mounted without root
+access. In this case, if a check for access by a particular user is requested,
+Exim creates a subprocess that runs as that user, and tries the check again in
+that process.
+
+The default action for handling an unresolved EACCES is to consider it to be
+caused by a configuration error, and routing is deferred because the existence
+or non-existence of the file cannot be determined. However, in some
+circumstances it may be desirable to treat this condition as if the file did
+not exist. If the file name (or the exclamation mark that precedes the file
+name for non-existence) is preceded by a plus sign, the EACCES error is treated
+as if the file did not exist. For example:
+
+require_files = +/some/file
+
+If the router is not an essential part of verification (for example, it handles
+users' .forward files), another solution is to set the verify option false so
+that the router is skipped when verifying.
+
++--------------------+------------+-------------+------------------+
+|retry_use_local_part|Use: routers|Type: boolean|Default: see below|
++--------------------+------------+-------------+------------------+
+
+When a delivery suffers a temporary routing failure, a retry record is created
+in Exim's hints database. For addresses whose routing depends only on the
+domain, the key for the retry record should not involve the local part, but for
+other addresses, both the domain and the local part should be included.
+Usually, remote routing is of the former kind, and local routing is of the
+latter kind.
+
+This option controls whether the local part is used to form the key for retry
+hints for addresses that suffer temporary errors while being handled by this
+router. The default value is true for any router that has check_local_user set,
+and false otherwise. Note that this option does not apply to hints keys for
+transport delays; they are controlled by a generic transport option of the same
+name.
+
+The setting of retry_use_local_part applies only to the router on which it
+appears. If the router generates child addresses, they are routed
+independently; this setting does not become attached to them.
+
++---------------------+------------+-------------+--------------+
+|router_home_directory|Use: routers|Type: string*|Default: unset|
++---------------------+------------+-------------+--------------+
+
+This option sets a home directory for use while the router is running. (Compare
+transport_home_directory, which sets a home directory for later transporting.)
+In particular, if used on a redirect router, this option sets a value for $home
+while a filter is running. The value is expanded; forced expansion failure
+causes the option to be ignored - other failures cause the router to defer.
+
+Expansion of router_home_directory happens immediately after the 
+check_local_user test (if configured), before any further expansions take
+place. (See section 3.12 for a list of the order in which preconditions are
+evaluated.) While the router is running, router_home_directory overrides the
+value of $home that came from check_local_user.
+
+When a router accepts an address and assigns it to a local transport (including
+the cases when a redirect router generates a pipe, file, or autoreply
+delivery), the home directory setting for the transport is taken from the first
+of these values that is set:
+
+  * The home_directory option on the transport;
+
+  * The transport_home_directory option on the router;
+
+  * The password data if check_local_user is set on the router;
+
+  * The router_home_directory option on the router.
+
+In other words, router_home_directory overrides the password data for the
+router, but not for the transport.
+
++----+------------+------------+---------------+
+|self|Use: routers|Type: string|Default: freeze|
++----+------------+------------+---------------+
+
+This option applies to those routers that use a recipient address to find a
+list of remote hosts. Currently, these are the dnslookup, ipliteral, and 
+manualroute routers. Certain configurations of the queryprogram router can also
+specify a list of remote hosts. Usually such routers are configured to send the
+message to a remote host via an smtp transport. The self option specifies what
+happens when the first host on the list turns out to be the local host. The way
+in which Exim checks for the local host is described in section 13.8.
+
+Normally this situation indicates either an error in Exim's configuration (for
+example, the router should be configured not to process this domain), or an
+error in the DNS (for example, the MX should not point to this host). For this
+reason, the default action is to log the incident, defer the address, and
+freeze the message. The following alternatives are provided for use in special
+cases:
+
+defer
+
+    Delivery of the message is tried again later, but the message is not
+    frozen.
+
+reroute: <domain>
+
+    The domain is changed to the given domain, and the address is passed back
+    to be reprocessed by the routers. No rewriting of headers takes place. This
+    behaviour is essentially a redirection.
+
+reroute: rewrite: <domain>
+
+    The domain is changed to the given domain, and the address is passed back
+    to be reprocessed by the routers. Any headers that contain the original
+    domain are rewritten.
+
+pass
+
+    The router passes the address to the next router, or to the router named in
+    the pass_router option if it is set. This overrides no_more. During
+    subsequent routing and delivery, the variable $self_hostname contains the
+    name of the local host that the router encountered. This can be used to
+    distinguish between different cases for hosts with multiple names. The
+    combination
+
+    self = pass
+    no_more
+
+    ensures that only those addresses that routed to the local host are passed
+    on. Without no_more, addresses that were declined for other reasons would
+    also be passed to the next router.
+
+fail
+
+    Delivery fails and an error report is generated.
+
+send
+
+    The anomaly is ignored and the address is queued for the transport. This
+    setting should be used with extreme caution. For an smtp transport, it
+    makes sense only in cases where the program that is listening on the SMTP
+    port is not this version of Exim. That is, it must be some other MTA, or
+    Exim with a different configuration file that handles the domain in another
+    way.
+
++-------+--------------+-------------------+--------------+
+|senders|Use: routers**|Type: address list*|Default: unset|
++-------+--------------+-------------------+--------------+
+
+If this option is set, the router is skipped unless the message's sender
+address matches something on the list. See section 3.12 for a list of the order
+in which preconditions are evaluated.
+
+There are issues concerning verification when the running of routers is
+dependent on the sender. When Exim is verifying the address in an errors_to
+setting, it sets the sender to the null string. When using the -bt option to
+check a configuration file, it is necessary also to use the -f option to set an
+appropriate sender. For incoming mail, the sender is unset when verifying the
+sender, but is available when verifying any recipients. If the SMTP VRFY
+command is enabled, it must be used after MAIL if the sender address matters.
+
++--------------------+------------+-------------+--------------+
+|translate_ip_address|Use: routers|Type: string*|Default: unset|
++--------------------+------------+-------------+--------------+
+
+There exist some rare networking situations (for example, packet radio) where
+it is helpful to be able to translate IP addresses generated by normal routing
+mechanisms into other IP addresses, thus performing a kind of manual IP
+routing. This should be done only if the normal IP routing of the TCP/IP stack
+is inadequate or broken. Because this is an extremely uncommon requirement, the
+code to support this option is not included in the Exim binary unless
+SUPPORT_TRANSLATE_IP_ADDRESS=yes is set in Local/Makefile.
+
+The translate_ip_address string is expanded for every IP address generated by
+the router, with the generated address set in $host_address. If the expansion
+is forced to fail, no action is taken. For any other expansion error, delivery
+of the message is deferred. If the result of the expansion is an IP address,
+that replaces the original address; otherwise the result is assumed to be a
+host name - this is looked up using gethostbyname() (or getipnodebyname() when
+available) to produce one or more replacement IP addresses. For example, to
+subvert all IP addresses in some specific networks, this could be added to a
+router:
+
+translate_ip_address = \
+  ${lookup{${mask:$host_address/26}}lsearch{/some/file}\
+    {$value}fail}}
+
+The file would contain lines like
+
+10.2.3.128/26    some.host
+10.8.4.34/26     10.44.8.15
+
+You should not make use of this facility unless you really understand what you
+are doing.
+
++---------+------------+-------------+--------------+
+|transport|Use: routers|Type: string*|Default: unset|
++---------+------------+-------------+--------------+
+
+This option specifies the transport to be used when a router accepts an address
+and sets it up for delivery. A transport is never needed if a router is used
+only for verification. The value of the option is expanded at routing time,
+after the expansion of errors_to, headers_add, and headers_remove, and result
+must be the name of one of the configured transports. If it is not, delivery is
+deferred.
+
+The transport option is not used by the redirect router, but it does have some
+private options that set up transports for pipe and file deliveries (see
+chapter 22).
+
++---------------------------+------------+-------------+--------------+
+|transport_current_directory|Use: routers|Type: string*|Default: unset|
++---------------------------+------------+-------------+--------------+
+
+This option associates a current directory with any address that is routed to a
+local transport. This can happen either because a transport is explicitly
+configured for the router, or because it generates a delivery to a file or a
+pipe. During the delivery process (that is, at transport time), this option
+string is expanded and is set as the current directory, unless overridden by a
+setting on the transport. If the expansion fails for any reason, including
+forced failure, an error is logged, and delivery is deferred. See chapter 23
+for details of the local delivery environment.
+
++------------------------+------------+-------------+------------------+
+|transport_home_directory|Use: routers|Type: string*|Default: see below|
++------------------------+------------+-------------+------------------+
+
+This option associates a home directory with any address that is routed to a
+local transport. This can happen either because a transport is explicitly
+configured for the router, or because it generates a delivery to a file or a
+pipe. During the delivery process (that is, at transport time), the option
+string is expanded and is set as the home directory, unless overridden by a
+setting of home_directory on the transport. If the expansion fails for any
+reason, including forced failure, an error is logged, and delivery is deferred.
+
+If the transport does not specify a home directory, and 
+transport_home_directory is not set for the router, the home directory for the
+transport is taken from the password data if check_local_user is set for the
+router. Otherwise it is taken from router_home_directory if that option is set;
+if not, no home directory is set for the transport.
+
+See chapter 23 for further details of the local delivery environment.
+
++------+------------+--------------+--------------+
+|unseen|Use: routers|Type: boolean*|Default: false|
++------+------------+--------------+--------------+
+
+The result of string expansion for this option must be a valid boolean value,
+that is, one of the strings "yes", "no", "true", or "false". Any other result
+causes an error, and delivery is deferred. If the expansion is forced to fail,
+the default value for the option (false) is used. Other failures cause delivery
+to be deferred.
+
+When this option is set true, routing does not cease if the router accepts the
+address. Instead, a copy of the incoming address is passed to the next router,
+overriding a false setting of more. There is little point in setting more false
+if unseen is always true, but it may be useful in cases when the value of 
+unseen contains expansion items (and therefore, presumably, is sometimes true
+and sometimes false).
+
+Setting the unseen option has a similar effect to the unseen command qualifier
+in filter files. It can be used to cause copies of messages to be delivered to
+some other destination, while also carrying out a normal delivery. In effect,
+the current address is made into a "parent" that has two children - one that is
+delivered as specified by this router, and a clone that goes on to be routed
+further. For this reason, unseen may not be combined with the one_time option
+in a redirect router.
+
+Warning: Header lines added to the address (or specified for removal) by this
+router or by previous routers affect the "unseen" copy of the message only. The
+clone that continues to be processed by further routers starts with no added
+headers and none specified for removal. For a redirect router, if a generated
+address is the same as the incoming address, this can lead to duplicate
+addresses with different header modifications. Exim does not do duplicate
+deliveries (except, in certain circumstances, to pipes -- see section 22.7),
+but it is undefined which of the duplicates is discarded, so this ambiguous
+situation should be avoided. The repeat_use option of the redirect router may
+be of help.
+
+Unlike the handling of header modifications, any data that was set by the 
+address_data option in the current or previous routers is passed on to
+subsequent routers.
+
++----+------------+-------------+------------------+
+|user|Use: routers|Type: string*|Default: see below|
++----+------------+-------------+------------------+
+
+When a router queues an address for a transport, and the transport does not
+specify a user, the user given here is used when running the delivery process.
+The user may be specified numerically or by name. If expansion fails, the error
+is logged and delivery is deferred. This user is also used by the redirect
+router when running a filter file. The default is unset, except when 
+check_local_user is set. In this case, the default is taken from the password
+information. If the user is specified as a name, and group is not set, the
+group associated with the user is used. See also initgroups and group and the
+discussion in chapter 23.
+
++------+--------------+-------------+-------------+
+|verify|Use: routers**|Type: boolean|Default: true|
++------+--------------+-------------+-------------+
+
+Setting this option has the effect of setting verify_sender and 
+verify_recipient to the same value.
+
++-----------+--------------+-------------+--------------+
+|verify_only|Use: routers**|Type: boolean|Default: false|
++-----------+--------------+-------------+--------------+
+
+If this option is set, the router is used only when verifying an address,
+delivering in cutthrough mode or testing with the -bv option, not when actually
+doing a delivery, testing with the -bt option, or running the SMTP EXPN
+command. It can be further restricted to verifying only senders or recipients
+by means of verify_sender and verify_recipient.
+
+Warning: When the router is being run to verify addresses for an incoming SMTP
+message, Exim is not running as root, but under its own uid. If the router
+accesses any files, you need to make sure that they are accessible to the Exim
+user or group.
+
++----------------+--------------+-------------+-------------+
+|verify_recipient|Use: routers**|Type: boolean|Default: true|
++----------------+--------------+-------------+-------------+
+
+If this option is false, the router is skipped when verifying recipient
+addresses, delivering in cutthrough mode or testing recipient verification
+using -bv. See section 3.12 for a list of the order in which preconditions are
+evaluated.
+
++-------------+--------------+-------------+-------------+
+|verify_sender|Use: routers**|Type: boolean|Default: true|
++-------------+--------------+-------------+-------------+
+
+If this option is false, the router is skipped when verifying sender addresses
+or testing sender verification using -bvs. See section 3.12 for a list of the
+order in which preconditions are evaluated.
+
+
+
+===============================================================================
+16. THE ACCEPT ROUTER
+
+The accept router has no private options of its own. Unless it is being used
+purely for verification (see verify_only) a transport is required to be defined
+by the generic transport option. If the preconditions that are specified by
+generic options are met, the router accepts the address and queues it for the
+given transport. The most common use of this router is for setting up
+deliveries to local mailboxes. For example:
+
+localusers:
+  driver = accept
+  domains = mydomain.example
+  check_local_user
+  transport = local_delivery
+
+The domains condition in this example checks the domain of the address, and 
+check_local_user checks that the local part is the login of a local user. When
+both preconditions are met, the accept router runs, and queues the address for
+the local_delivery transport.
+
+
+
+===============================================================================
+17. THE DNSLOOKUP ROUTER
+
+The dnslookup router looks up the hosts that handle mail for the recipient's
+domain in the DNS. A transport must always be set for this router, unless 
+verify_only is set.
+
+If SRV support is configured (see check_srv below), Exim first searches for SRV
+records. If none are found, or if SRV support is not configured, MX records are
+looked up. If no MX records exist, address records are sought. However, 
+mx_domains can be set to disable the direct use of address records.
+
+MX records of equal priority are sorted by Exim into a random order. Exim then
+looks for address records for the host names obtained from MX or SRV records.
+When a host has more than one IP address, they are sorted into a random order,
+except that IPv6 addresses are always sorted before IPv4 addresses. If all the
+IP addresses found are discarded by a setting of the ignore_target_hosts
+generic option, the router declines.
+
+Unless they have the highest priority (lowest MX value), MX records that point
+to the local host, or to any host name that matches hosts_treat_as_local, are
+discarded, together with any other MX records of equal or lower priority.
+
+If the host pointed to by the highest priority MX record, or looked up as an
+address record, is the local host, or matches hosts_treat_as_local, what
+happens is controlled by the generic self option.
+
+
+17.1 Problems with DNS lookups
+------------------------------
+
+There have been problems with DNS servers when SRV records are looked up. Some
+mis-behaving servers return a DNS error or timeout when a non-existent SRV
+record is sought. Similar problems have in the past been reported for MX
+records. The global dns_again_means_nonexist option can help with this problem,
+but it is heavy-handed because it is a global option.
+
+For this reason, there are two options, srv_fail_domains and mx_fail_domains,
+that control what happens when a DNS lookup in a dnslookup router results in a
+DNS failure or a "try again" response. If an attempt to look up an SRV or MX
+record causes one of these results, and the domain matches the relevant list,
+Exim behaves as if the DNS had responded "no such record". In the case of an
+SRV lookup, this means that the router proceeds to look for MX records; in the
+case of an MX lookup, it proceeds to look for A or AAAA records, unless the
+domain matches mx_domains, in which case routing fails.
+
+
+17.2 Declining addresses by dnslookup
+-------------------------------------
+
+There are a few cases where a dnslookup router will decline to accept an
+address; if such a router is expected to handle "all remaining non-local
+domains", then it is important to set no_more.
+
+Reasons for a dnslookup router to decline currently include:
+
+  * The domain does not exist in DNS
+
+  * The domain exists but the MX record's host part is just "."; this is a
+    common convention (borrowed from SRV) used to indicate that there is no
+    such service for this domain and to not fall back to trying A/AAAA records.
+
+  * Ditto, but for SRV records, when check_srv is set on this router.
+
+  * MX record points to a non-existent host.
+
+  * MX record points to an IP address and the main section option 
+    allow_mx_to_ip is not set.
+
+  * MX records exist and point to valid hosts, but all hosts resolve only to
+    addresses blocked by the ignore_target_hosts generic option on this router.
+
+  * The domain is not syntactically valid (see also allow_utf8_domains and 
+    dns_check_names_pattern for handling one variant of this)
+
+  * check_secondary_mx is set on this router but the local host can not be
+    found in the MX records (see below)
+
+
+17.3 Private options for dnslookup
+----------------------------------
+
+The private options for the dnslookup router are as follows:
+
++------------------+--------------+-------------+--------------+
+|check_secondary_mx|Use: dnslookup|Type: boolean|Default: false|
++------------------+--------------+-------------+--------------+
+
+If this option is set, the router declines unless the local host is found in
+(and removed from) the list of hosts obtained by MX lookup. This can be used to
+process domains for which the local host is a secondary mail exchanger
+differently to other domains. The way in which Exim decides whether a host is
+the local host is described in section 13.8.
+
++---------+--------------+-------------+--------------+
+|check_srv|Use: dnslookup|Type: string*|Default: unset|
++---------+--------------+-------------+--------------+
+
+The dnslookup router supports the use of SRV records (see RFC 2782) in addition
+to MX and address records. The support is disabled by default. To enable SRV
+support, set the check_srv option to the name of the service required. For
+example,
+
+check_srv = smtp
+
+looks for SRV records that refer to the normal smtp service. The option is
+expanded, so the service name can vary from message to message or address to
+address. This might be helpful if SRV records are being used for a submission
+service. If the expansion is forced to fail, the check_srv option is ignored,
+and the router proceeds to look for MX records in the normal way.
+
+When the expansion succeeds, the router searches first for SRV records for the
+given service (it assumes TCP protocol). A single SRV record with a host name
+that consists of just a single dot indicates "no such service for this domain";
+if this is encountered, the router declines. If other kinds of SRV record are
+found, they are used to construct a host list for delivery according to the
+rules of RFC 2782. MX records are not sought in this case.
+
+When no SRV records are found, MX records (and address records) are sought in
+the traditional way. In other words, SRV records take precedence over MX
+records, just as MX records take precedence over address records. Note that
+this behaviour is not sanctioned by RFC 2782, though a previous draft RFC
+defined it. It is apparently believed that MX records are sufficient for email
+and that SRV records should not be used for this purpose. However, SRV records
+have an additional "weight" feature which some people might find useful when
+trying to split an SMTP load between hosts of different power.
+
+See section 17.1 above for a discussion of Exim's behaviour when there is a DNS
+lookup error.
+
++----------------------+--------------+------------------+--------------+
+|dnssec_request_domains|Use: dnslookup|Type: domain list*|Default: unset|
++----------------------+--------------+------------------+--------------+
+
+DNS lookups for domains matching dnssec_request_domains will be done with the
+dnssec request bit set. This applies to all of the SRV, MX A6, AAAA, A lookup
+sequence.
+
++----------------------+--------------+------------------+--------------+
+|dnssec_require_domains|Use: dnslookup|Type: domain list*|Default: unset|
++----------------------+--------------+------------------+--------------+
+
+DNS lookups for domains matching dnssec_request_domains will be done with the
+dnssec request bit set. Any returns not having the Authenticated Data bit (AD
+bit) set wil be ignored and logged as a host-lookup failure. This applies to
+all of the SRV, MX A6, AAAA, A lookup sequence.
+
++----------+--------------+------------------+--------------+
+|mx_domains|Use: dnslookup|Type: domain list*|Default: unset|
++----------+--------------+------------------+--------------+
+
+A domain that matches mx_domains is required to have either an MX or an SRV
+record in order to be recognized. (The name of this option could be improved.)
+For example, if all the mail hosts in fict.example are known to have MX
+records, except for those in discworld.fict.example, you could use this
+setting:
+
+mx_domains = ! *.discworld.fict.example : *.fict.example
+
+This specifies that messages addressed to a domain that matches the list but
+has no MX record should be bounced immediately instead of being routed using
+the address record.
+
++---------------+--------------+------------------+--------------+
+|mx_fail_domains|Use: dnslookup|Type: domain list*|Default: unset|
++---------------+--------------+------------------+--------------+
+
+If the DNS lookup for MX records for one of the domains in this list causes a
+DNS lookup error, Exim behaves as if no MX records were found. See section 17.1
+for more discussion.
+
++--------------+--------------+-------------+-------------+
+|qualify_single|Use: dnslookup|Type: boolean|Default: true|
++--------------+--------------+-------------+-------------+
+
+When this option is true, the resolver option RES_DEFNAMES is set for DNS
+lookups. Typically, but not standardly, this causes the resolver to qualify
+single-component names with the default domain. For example, on a machine
+called dictionary.ref.example, the domain thesaurus would be changed to 
+thesaurus.ref.example inside the resolver. For details of what your resolver
+actually does, consult your man pages for resolver and resolv.conf.
+
++---------------+--------------+-------------+-------------+
+|rewrite_headers|Use: dnslookup|Type: boolean|Default: true|
++---------------+--------------+-------------+-------------+
+
+If the domain name in the address that is being processed is not fully
+qualified, it may be expanded to its full form by a DNS lookup. For example, if
+an address is specified as dormouse@teaparty, the domain might be expanded to 
+teaparty.wonderland.fict.example. Domain expansion can also occur as a result
+of setting the widen_domains option. If rewrite_headers is true, all
+occurrences of the abbreviated domain name in any Bcc:, Cc:, From:, Reply-to:, 
+Sender:, and To: header lines of the message are rewritten with the full domain
+name.
+
+This option should be turned off only when it is known that no message is ever
+going to be sent outside an environment where the abbreviation makes sense.
+
+When an MX record is looked up in the DNS and matches a wildcard record, name
+servers normally return a record containing the name that has been looked up,
+making it impossible to detect whether a wildcard was present or not. However,
+some name servers have recently been seen to return the wildcard entry. If the
+name returned by a DNS lookup begins with an asterisk, it is not used for
+header rewriting.
+
++------------------------+--------------+-------------+--------------+
+|same_domain_copy_routing|Use: dnslookup|Type: boolean|Default: false|
++------------------------+--------------+-------------+--------------+
+
+Addresses with the same domain are normally routed by the dnslookup router to
+the same list of hosts. However, this cannot be presumed, because the router
+options and preconditions may refer to the local part of the address. By
+default, therefore, Exim routes each address in a message independently. DNS
+servers run caches, so repeated DNS lookups are not normally expensive, and in
+any case, personal messages rarely have more than a few recipients.
+
+If you are running mailing lists with large numbers of subscribers at the same
+domain, and you are using a dnslookup router which is independent of the local
+part, you can set same_domain_copy_routing to bypass repeated DNS lookups for
+identical domains in one message. In this case, when dnslookup routes an
+address to a remote transport, any other unrouted addresses in the message that
+have the same domain are automatically given the same routing without
+processing them independently, provided the following conditions are met:
+
+  * No router that processed the address specified headers_add or 
+    headers_remove.
+
+  * The router did not change the address in any way, for example, by
+    "widening" the domain.
+
++--------------+--------------+-------------+--------------+
+|search_parents|Use: dnslookup|Type: boolean|Default: false|
++--------------+--------------+-------------+--------------+
+
+When this option is true, the resolver option RES_DNSRCH is set for DNS
+lookups. This is different from the qualify_single option in that it applies to
+domains containing dots. Typically, but not standardly, it causes the resolver
+to search for the name in the current domain and in parent domains. For
+example, on a machine in the fict.example domain, if looking up 
+teaparty.wonderland failed, the resolver would try 
+teaparty.wonderland.fict.example. For details of what your resolver actually
+does, consult your man pages for resolver and resolv.conf.
+
+Setting this option true can cause problems in domains that have a wildcard MX
+record, because any domain that does not have its own MX record matches the
+local wildcard.
+
++----------------+--------------+------------------+--------------+
+|srv_fail_domains|Use: dnslookup|Type: domain list*|Default: unset|
++----------------+--------------+------------------+--------------+
+
+If the DNS lookup for SRV records for one of the domains in this list causes a
+DNS lookup error, Exim behaves as if no SRV records were found. See section
+17.1 for more discussion.
+
++-------------+--------------+-----------------+--------------+
+|widen_domains|Use: dnslookup|Type: string list|Default: unset|
++-------------+--------------+-----------------+--------------+
+
+If a DNS lookup fails and this option is set, each of its strings in turn is
+added onto the end of the domain, and the lookup is tried again. For example,
+if
+
+widen_domains = fict.example:ref.example
+
+is set and a lookup of klingon.dictionary fails, 
+klingon.dictionary.fict.example is looked up, and if this fails, 
+klingon.dictionary.ref.example is tried. Note that the qualify_single and 
+search_parents options can cause some widening to be undertaken inside the DNS
+resolver. widen_domains is not applied to sender addresses when verifying,
+unless rewrite_headers is false (not the default).
+
+
+17.4 Effect of qualify_single and search_parents
+------------------------------------------------
+
+When a domain from an envelope recipient is changed by the resolver as a result
+of the qualify_single or search_parents options, Exim rewrites the
+corresponding address in the message's header lines unless rewrite_headers is
+set false. Exim then re-routes the address, using the full domain.
+
+These two options affect only the DNS lookup that takes place inside the router
+for the domain of the address that is being routed. They do not affect lookups
+such as that implied by
+
+domains = @mx_any
+
+that may happen while processing a router precondition before the router is
+entered. No widening ever takes place for these lookups.
+
+
+
+===============================================================================
+18. THE IPLITERAL ROUTER
+
+This router has no private options. Unless it is being used purely for
+verification (see verify_only) a transport is required to be defined by the
+generic transport option. The router accepts the address if its domain part
+takes the form of an RFC 2822 domain literal. For example, the ipliteral router
+handles the address
+
+root@[192.168.1.1]
+
+by setting up delivery to the host with that IP address. IPv4 domain literals
+consist of an IPv4 address enclosed in square brackets. IPv6 domain literals
+are similar, but the address is preceded by "ipv6:". For example:
+
+postmaster@[ipv6:fe80::a00:20ff:fe86:a061.5678]
+
+Exim allows "ipv4:" before IPv4 addresses, for consistency, and on the grounds
+that sooner or later somebody will try it.
+
+If the IP address matches something in ignore_target_hosts, the router
+declines. If an IP literal turns out to refer to the local host, the generic 
+self option determines what happens.
+
+The RFCs require support for domain literals; however, their use is
+controversial in today's Internet. If you want to use this router, you must
+also set the main configuration option allow_domain_literals. Otherwise, Exim
+will not recognize the domain literal syntax in addresses.
+
+
+
+===============================================================================
+19. THE IPLOOKUP ROUTER
+
+The iplookup router was written to fulfil a specific requirement in Cambridge
+University (which in fact no longer exists). For this reason, it is not
+included in the binary of Exim by default. If you want to include it, you must
+set
+
+ROUTER_IPLOOKUP=yes
+
+in your Local/Makefile configuration file.
+
+The iplookup router routes an address by sending it over a TCP or UDP
+connection to one or more specific hosts. The host can then return the same or
+a different address - in effect rewriting the recipient address in the
+message's envelope. The new address is then passed on to subsequent routers. If
+this process fails, the address can be passed on to other routers, or delivery
+can be deferred. Since iplookup is just a rewriting router, a transport must
+not be specified for it.
+
++-----+-------------+------------+--------------+
+|hosts|Use: iplookup|Type: string|Default: unset|
++-----+-------------+------------+--------------+
+
+This option must be supplied. Its value is a colon-separated list of host
+names. The hosts are looked up using gethostbyname() (or getipnodebyname() when
+available) and are tried in order until one responds to the query. If none
+respond, what happens is controlled by optional.
+
++--------+-------------+-------------+--------------+
+|optional|Use: iplookup|Type: boolean|Default: false|
++--------+-------------+-------------+--------------+
+
+If optional is true, if no response is obtained from any host, the address is
+passed to the next router, overriding no_more. If optional is false, delivery
+to the address is deferred.
+
++----+-------------+-------------+----------+
+|port|Use: iplookup|Type: integer|Default: 0|
++----+-------------+-------------+----------+
+
+This option must be supplied. It specifies the port number for the TCP or UDP
+call.
+
++--------+-------------+------------+------------+
+|protocol|Use: iplookup|Type: string|Default: udp|
++--------+-------------+------------+------------+
+
+This option can be set to "udp" or "tcp" to specify which of the two protocols
+is to be used.
+
++-----+-------------+-------------+------------------+
+|query|Use: iplookup|Type: string*|Default: see below|
++-----+-------------+-------------+------------------+
+
+This defines the content of the query that is sent to the remote hosts. The
+default value is:
+
+$local_part@$domain $local_part@$domain
+
+The repetition serves as a way of checking that a response is to the correct
+query in the default case (see response_pattern below).
+
++-------+-------------+-------------+--------------+
+|reroute|Use: iplookup|Type: string*|Default: unset|
++-------+-------------+-------------+--------------+
+
+If this option is not set, the rerouted address is precisely the byte string
+returned by the remote host, up to the first white space, if any. If set, the
+string is expanded to form the rerouted address. It can include parts matched
+in the response by response_pattern by means of numeric variables such as $1,
+$2, etc. The variable $0 refers to the entire input string, whether or not a
+pattern is in use. In all cases, the rerouted address must end up in the form 
+local_part@domain.
+
++----------------+-------------+------------+--------------+
+|response_pattern|Use: iplookup|Type: string|Default: unset|
++----------------+-------------+------------+--------------+
+
+This option can be set to a regular expression that is applied to the string
+returned from the remote host. If the pattern does not match the response, the
+router declines. If response_pattern is not set, no checking of the response is
+done, unless the query was defaulted, in which case there is a check that the
+text returned after the first white space is the original address. This checks
+that the answer that has been received is in response to the correct question.
+For example, if the response is just a new domain, the following could be used:
+
+response_pattern = ^([^@]+)$
+reroute = $local_part@$1
+
++-------+-------------+----------+-----------+
+|timeout|Use: iplookup|Type: time|Default: 5s|
++-------+-------------+----------+-----------+
+
+This specifies the amount of time to wait for a response from the remote
+machine. The same timeout is used for the connect() function for a TCP call. It
+does not apply to UDP.
+
+
+
+===============================================================================
+20. THE MANUALROUTE ROUTER
+
+The manualroute router is so-called because it provides a way of manually
+routing an address according to its domain. It is mainly used when you want to
+route addresses to remote hosts according to your own rules, bypassing the
+normal DNS routing that looks up MX records. However, manualroute can also
+route to local transports, a facility that may be useful if you want to save
+messages for dial-in hosts in local files.
+
+The manualroute router compares a list of domain patterns with the domain it is
+trying to route. If there is no match, the router declines. Each pattern has
+associated with it a list of hosts and some other optional data, which may
+include a transport. The combination of a pattern and its data is called a
+"routing rule". For patterns that do not have an associated transport, the
+generic transport option must specify a transport, unless the router is being
+used purely for verification (see verify_only).
+
+In the case of verification, matching the domain pattern is sufficient for the
+router to accept the address. When actually routing an address for delivery, an
+address that matches a domain pattern is queued for the associated transport.
+If the transport is not a local one, a host list must be associated with the
+pattern; IP addresses are looked up for the hosts, and these are passed to the
+transport along with the mail address. For local transports, a host list is
+optional. If it is present, it is passed in $host as a single text string.
+
+The list of routing rules can be provided as an inline string in route_list, or
+the data can be obtained by looking up the domain in a file or database by
+setting route_data. Only one of these settings may appear in any one instance
+of manualroute. The format of routing rules is described below, following the
+list of private options.
+
+
+20.1 Private options for manualroute
+------------------------------------
+
+The private options for the manualroute router are as follows:
+
++----------------+----------------+------------+--------------+
+|host_all_ignored|Use: manualroute|Type: string|Default: defer|
++----------------+----------------+------------+--------------+
+
+See host_find_failed.
+
++----------------+----------------+------------+---------------+
+|host_find_failed|Use: manualroute|Type: string|Default: freeze|
++----------------+----------------+------------+---------------+
+
+This option controls what happens when manualroute tries to find an IP address
+for a host, and the host does not exist. The option can be set to one of the
+following values:
+
+decline
+defer
+fail
+freeze
+ignore
+pass
+
+The default ("freeze") assumes that this state is a serious configuration
+error. The difference between "pass" and "decline" is that the former forces
+the address to be passed to the next router (or the router defined by 
+pass_router), overriding no_more, whereas the latter passes the address to the
+next router only if more is true.
+
+The value "ignore" causes Exim to completely ignore a host whose IP address
+cannot be found. If all the hosts in the list are ignored, the behaviour is
+controlled by the host_all_ignored option. This takes the same values as 
+host_find_failed, except that it cannot be set to "ignore".
+
+The host_find_failed option applies only to a definite "does not exist" state;
+if a host lookup gets a temporary error, delivery is deferred unless the
+generic pass_on_timeout option is set.
+
++---------------+----------------+-------------+--------------+
+|hosts_randomize|Use: manualroute|Type: boolean|Default: false|
++---------------+----------------+-------------+--------------+
+
+If this option is set, the order of the items in a host list in a routing rule
+is randomized each time the list is used, unless an option in the routing rule
+overrides (see below). Randomizing the order of a host list can be used to do
+crude load sharing. However, if more than one mail address is routed by the
+same router to the same host list, the host lists are considered to be the same
+(even though they may be randomized into different orders) for the purpose of
+deciding whether to batch the deliveries into a single SMTP transaction.
+
+When hosts_randomize is true, a host list may be split into groups whose order
+is separately randomized. This makes it possible to set up MX-like behaviour.
+The boundaries between groups are indicated by an item that is just "+" in the
+host list. For example:
+
+route_list = * host1:host2:host3:+:host4:host5
+
+The order of the first three hosts and the order of the last two hosts is
+randomized for each use, but the first three always end up before the last two.
+If hosts_randomize is not set, a "+" item in the list is ignored. If a
+randomized host list is passed to an smtp transport that also has 
+hosts_randomize set, the list is not re-randomized.
+
++----------+----------------+-------------+--------------+
+|route_data|Use: manualroute|Type: string*|Default: unset|
++----------+----------------+-------------+--------------+
+
+If this option is set, it must expand to yield the data part of a routing rule.
+Typically, the expansion string includes a lookup based on the domain. For
+example:
+
+route_data = ${lookup{$domain}dbm{/etc/routes}}
+
+If the expansion is forced to fail, or the result is an empty string, the
+router declines. Other kinds of expansion failure cause delivery to be
+deferred.
+
++----------+----------------+-----------------+--------------+
+|route_list|Use: manualroute|Type: string list|Default: unset|
++----------+----------------+-----------------+--------------+
+
+This string is a list of routing rules, in the form defined below. Note that,
+unlike most string lists, the items are separated by semicolons. This is so
+that they may contain colon-separated host lists.
+
++------------------------+----------------+-------------+--------------+
+|same_domain_copy_routing|Use: manualroute|Type: boolean|Default: false|
++------------------------+----------------+-------------+--------------+
+
+Addresses with the same domain are normally routed by the manualroute router to
+the same list of hosts. However, this cannot be presumed, because the router
+options and preconditions may refer to the local part of the address. By
+default, therefore, Exim routes each address in a message independently. DNS
+servers run caches, so repeated DNS lookups are not normally expensive, and in
+any case, personal messages rarely have more than a few recipients.
+
+If you are running mailing lists with large numbers of subscribers at the same
+domain, and you are using a manualroute router which is independent of the
+local part, you can set same_domain_copy_routing to bypass repeated DNS lookups
+for identical domains in one message. In this case, when manualroute routes an
+address to a remote transport, any other unrouted addresses in the message that
+have the same domain are automatically given the same routing without
+processing them independently. However, this is only done if headers_add and 
+headers_remove are unset.
+
+
+20.2 Routing rules in route_list
+--------------------------------
+
+The value of route_list is a string consisting of a sequence of routing rules,
+separated by semicolons. If a semicolon is needed in a rule, it can be entered
+as two semicolons. Alternatively, the list separator can be changed as
+described (for colon-separated lists) in section 6.19. Empty rules are ignored.
+The format of each rule is
+
+<domain pattern>  <list of hosts>  <options>
+
+The following example contains two rules, each with a simple domain pattern and
+no options:
+
+route_list = \
+  dict.ref.example  mail-1.ref.example:mail-2.ref.example ; \
+  thes.ref.example  mail-3.ref.example:mail-4.ref.example
+
+The three parts of a rule are separated by white space. The pattern and the
+list of hosts can be enclosed in quotes if necessary, and if they are, the
+usual quoting rules apply. Each rule in a route_list must start with a single
+domain pattern, which is the only mandatory item in the rule. The pattern is in
+the same format as one item in a domain list (see section 10.8), except that it
+may not be the name of an interpolated file. That is, it may be wildcarded, or
+a regular expression, or a file or database lookup (with semicolons doubled,
+because of the use of semicolon as a separator in a route_list).
+
+The rules in route_list are searched in order until one of the patterns matches
+the domain that is being routed. The list of hosts and then options are then
+used as described below. If there is no match, the router declines. When 
+route_list is set, route_data must not be set.
+
+
+20.3 Routing rules in route_data
+--------------------------------
+
+The use of route_list is convenient when there are only a small number of
+routing rules. For larger numbers, it is easier to use a file or database to
+hold the routing information, and use the route_data option instead. The value
+of route_data is a list of hosts, followed by (optional) options. Most
+commonly, route_data is set as a string that contains an expansion lookup. For
+example, suppose we place two routing rules in a file like this:
+
+dict.ref.example:  mail-1.ref.example:mail-2.ref.example
+thes.ref.example:  mail-3.ref.example:mail-4.ref.example
+
+This data can be accessed by setting
+
+route_data = ${lookup{$domain}lsearch{/the/file/name}}
+
+Failure of the lookup results in an empty string, causing the router to
+decline. However, you do not have to use a lookup in route_data. The only
+requirement is that the result of expanding the string is a list of hosts,
+possibly followed by options, separated by white space. The list of hosts must
+be enclosed in quotes if it contains white space.
+
+
+20.4 Format of the list of hosts
+--------------------------------
+
+A list of hosts, whether obtained via route_data or route_list, is always
+separately expanded before use. If the expansion fails, the router declines.
+The result of the expansion must be a colon-separated list of names and/or IP
+addresses, optionally also including ports. The format of each item in the list
+is described in the next section. The list separator can be changed as
+described in section 6.19.
+
+If the list of hosts was obtained from a route_list item, the following
+variables are set during its expansion:
+
+  * If the domain was matched against a regular expression, the numeric
+    variables $1, $2, etc. may be set. For example:
+
+    route_list = ^domain(\d+)   host-$1.text.example
+
+  * $0 is always set to the entire domain.
+
+  * $1 is also set when partial matching is done in a file lookup.
+
+  * If the pattern that matched the domain was a lookup item, the data that was
+    looked up is available in the expansion variable $value. For example:
+
+    route_list = lsearch;;/some/file.routes  $value
+
+Note the doubling of the semicolon in the pattern that is necessary because
+semicolon is the default route list separator.
+
+
+20.5 Format of one host item
+----------------------------
+
+Each item in the list of hosts is either a host name or an IP address,
+optionally with an attached port number. When no port is given, an IP address
+is not enclosed in brackets. When a port is specified, it overrides the port
+specification on the transport. The port is separated from the name or address
+by a colon. This leads to some complications:
+
+  * Because colon is the default separator for the list of hosts, either the
+    colon that specifies a port must be doubled, or the list separator must be
+    changed. The following two examples have the same effect:
+
+    route_list = * "host1.tld::1225 : host2.tld::1226"
+    route_list = * "<+ host1.tld:1225 + host2.tld:1226"
+
+  * When IPv6 addresses are involved, it gets worse, because they contain
+    colons of their own. To make this case easier, it is permitted to enclose
+    an IP address (either v4 or v6) in square brackets if a port number
+    follows. For example:
+
+    route_list = * "</ [10.1.1.1]:1225 / [::1]:1226"
+
+
+20.6 How the list of hosts is used
+----------------------------------
+
+When an address is routed to an smtp transport by manualroute, each of the
+hosts is tried, in the order specified, when carrying out the SMTP delivery.
+However, the order can be changed by setting the hosts_randomize option, either
+on the router (see section 20.1 above), or on the transport.
+
+Hosts may be listed by name or by IP address. An unadorned name in the list of
+hosts is interpreted as a host name. A name that is followed by "/MX" is
+interpreted as an indirection to a sublist of hosts obtained by looking up MX
+records in the DNS. For example:
+
+route_list = *  x.y.z:p.q.r/MX:e.f.g
+
+If this feature is used with a port specifier, the port must come last. For
+example:
+
+route_list = *  dom1.tld/mx::1225
+
+If the hosts_randomize option is set, the order of the items in the list is
+randomized before any lookups are done. Exim then scans the list; for any name
+that is not followed by "/MX" it looks up an IP address. If this turns out to
+be an interface on the local host and the item is not the first in the list,
+Exim discards it and any subsequent items. If it is the first item, what
+happens is controlled by the self option of the router.
+
+A name on the list that is followed by "/MX" is replaced with the list of hosts
+obtained by looking up MX records for the name. This is always a DNS lookup;
+the bydns and byname options (see section 20.7 below) are not relevant here.
+The order of these hosts is determined by the preference values in the MX
+records, according to the usual rules. Because randomizing happens before the
+MX lookup, it does not affect the order that is defined by MX preferences.
+
+If the local host is present in the sublist obtained from MX records, but is
+not the most preferred host in that list, it and any equally or less preferred
+hosts are removed before the sublist is inserted into the main list.
+
+If the local host is the most preferred host in the MX list, what happens
+depends on where in the original list of hosts the "/MX" item appears. If it is
+not the first item (that is, there are previous hosts in the main list), Exim
+discards this name and any subsequent items in the main list.
+
+If the MX item is first in the list of hosts, and the local host is the most
+preferred host, what happens is controlled by the self option of the router.
+
+DNS failures when lookup up the MX records are treated in the same way as DNS
+failures when looking up IP addresses: pass_on_timeout and host_find_failed are
+used when relevant.
+
+The generic ignore_target_hosts option applies to all hosts in the list,
+whether obtained from an MX lookup or not.
+
+
+20.7 How the options are used
+-----------------------------
+
+The options are a sequence of words; in practice no more than three are ever
+present. One of the words can be the name of a transport; this overrides the 
+transport option on the router for this particular routing rule only. The other
+words (if present) control randomization of the list of hosts on a per-rule
+basis, and how the IP addresses of the hosts are to be found when routing to a
+remote transport. These options are as follows:
+
+  * randomize: randomize the order of the hosts in this list, overriding the
+    setting of hosts_randomize for this routing rule only.
+
+  * no_randomize: do not randomize the order of the hosts in this list,
+    overriding the setting of hosts_randomize for this routing rule only.
+
+  * byname: use getipnodebyname() (gethostbyname() on older systems) to find IP
+    addresses. This function may ultimately cause a DNS lookup, but it may also
+    look in /etc/hosts or other sources of information.
+
+  * bydns: look up address records for the hosts directly in the DNS; fail if
+    no address records are found. If there is a temporary DNS error (such as a
+    timeout), delivery is deferred.
+
+For example:
+
+route_list = domain1  host1:host2:host3  randomize bydns;\
+             domain2  host4:host5
+
+If neither byname nor bydns is given, Exim behaves as follows: First, a DNS
+lookup is done. If this yields anything other than HOST_NOT_FOUND, that result
+is used. Otherwise, Exim goes on to try a call to getipnodebyname() or
+gethostbyname(), and the result of the lookup is the result of that call.
+
+Warning: It has been discovered that on some systems, if a DNS lookup called
+via getipnodebyname() times out, HOST_NOT_FOUND is returned instead of
+TRY_AGAIN. That is why the default action is to try a DNS lookup first. Only if
+that gives a definite "no such host" is the local function called.
+
+If no IP address for a host can be found, what happens is controlled by the 
+host_find_failed option.
+
+When an address is routed to a local transport, IP addresses are not looked up.
+The host list is passed to the transport in the $host variable.
+
+
+20.8 Manualroute examples
+-------------------------
+
+In some of the examples that follow, the presence of the remote_smtp transport,
+as defined in the default configuration file, is assumed:
+
+  * The manualroute router can be used to forward all external mail to a smart
+    host. If you have set up, in the main part of the configuration, a named
+    domain list that contains your local domains, for example:
+
+    domainlist local_domains = my.domain.example
+
+    You can arrange for all other domains to be routed to a smart host by
+    making your first router something like this:
+
+    smart_route:
+      driver = manualroute
+      domains = !+local_domains
+      transport = remote_smtp
+      route_list = * smarthost.ref.example
+
+    This causes all non-local addresses to be sent to the single host 
+    smarthost.ref.example. If a colon-separated list of smart hosts is given,
+    they are tried in order (but you can use hosts_randomize to vary the order
+    each time). Another way of configuring the same thing is this:
+
+    smart_route:
+      driver = manualroute
+      transport = remote_smtp
+      route_list = !+local_domains  smarthost.ref.example
+
+    There is no difference in behaviour between these two routers as they
+    stand. However, they behave differently if no_more is added to them. In the
+    first example, the router is skipped if the domain does not match the 
+    domains precondition; the following router is always tried. If the router
+    runs, it always matches the domain and so can never decline. Therefore, 
+    no_more would have no effect. In the second case, the router is never
+    skipped; it always runs. However, if it doesn't match the domain, it
+    declines. In this case no_more would prevent subsequent routers from
+    running.
+
+  * A mail hub is a host which receives mail for a number of domains via MX
+    records in the DNS and delivers it via its own private routing mechanism.
+    Often the final destinations are behind a firewall, with the mail hub being
+    the one machine that can connect to machines both inside and outside the
+    firewall. The manualroute router is usually used on a mail hub to route
+    incoming messages to the correct hosts. For a small number of domains, the
+    routing can be inline, using the route_list option, but for a larger number
+    a file or database lookup is easier to manage.
+
+    If the domain names are in fact the names of the machines to which the mail
+    is to be sent by the mail hub, the configuration can be quite simple. For
+    example:
+
+    hub_route:
+      driver = manualroute
+      transport = remote_smtp
+      route_list = *.rhodes.tvs.example  $domain
+
+    This configuration routes domains that match "*.rhodes.tvs.example" to
+    hosts whose names are the same as the mail domains. A similar approach can
+    be taken if the host name can be obtained from the domain name by a string
+    manipulation that the expansion facilities can handle. Otherwise, a lookup
+    based on the domain can be used to find the host:
+
+    through_firewall:
+      driver = manualroute
+      transport = remote_smtp
+      route_data = ${lookup {$domain} cdb {/internal/host/routes}}
+
+    The result of the lookup must be the name or IP address of the host (or
+    hosts) to which the address is to be routed. If the lookup fails, the route
+    data is empty, causing the router to decline. The address then passes to
+    the next router.
+
+  * You can use manualroute to deliver messages to pipes or files in batched
+    SMTP format for onward transportation by some other means. This is one way
+    of storing mail for a dial-up host when it is not connected. The route list
+    entry can be as simple as a single domain name in a configuration like
+    this:
+
+    save_in_file:
+      driver = manualroute
+      transport = batchsmtp_appendfile
+      route_list = saved.domain.example
+
+    though often a pattern is used to pick up more than one domain. If there
+    are several domains or groups of domains with different transport
+    requirements, different transports can be listed in the routing
+    information:
+
+    save_in_file:
+      driver = manualroute
+      route_list = \
+        *.saved.domain1.example  $domain  batch_appendfile; \
+        *.saved.domain2.example  \
+          ${lookup{$domain}dbm{/domain2/hosts}{$value}fail} \
+          batch_pipe
+
+    The first of these just passes the domain in the $host variable, which
+    doesn't achieve much (since it is also in $domain), but the second does a
+    file lookup to find a value to pass, causing the router to decline to
+    handle the address if the lookup fails.
+
+  * Routing mail directly to UUCP software is a specific case of the use of 
+    manualroute in a gateway to another mail environment. This is an example of
+    one way it can be done:
+
+    # Transport
+    uucp:
+      driver = pipe
+      user = nobody
+      command = /usr/local/bin/uux -r - \
+        ${substr_-5:$host}!rmail ${local_part}
+      return_fail_output = true
+
+    # Router
+    uucphost:
+      transport = uucp
+      driver = manualroute
+      route_data = \
+        ${lookup{$domain}lsearch{/usr/local/exim/uucphosts}}
+
+    The file /usr/local/exim/uucphosts contains entries like
+
+    darksite.ethereal.example:           darksite.UUCP
+
+    It can be set up more simply without adding and removing ".UUCP" but this
+    way makes clear the distinction between the domain name 
+    darksite.ethereal.example and the UUCP host name darksite.
+
+
+
+===============================================================================
+21. THE QUERYPROGRAM ROUTER
+
+The queryprogram router routes an address by running an external command and
+acting on its output. This is an expensive way to route, and is intended mainly
+for use in lightly-loaded systems, or for performing experiments. However, if
+it is possible to use the precondition options (domains, local_parts, etc) to
+skip this router for most addresses, it could sensibly be used in special
+cases, even on a busy host. There are the following private options:
+
++-------+-----------------+-------------+--------------+
+|command|Use: queryprogram|Type: string*|Default: unset|
++-------+-----------------+-------------+--------------+
+
+This option must be set. It specifies the command that is to be run. The
+command is split up into a command name and arguments, and then each is
+expanded separately (exactly as for a pipe transport, described in chapter 29).
+
++-------------+-----------------+------------+--------------+
+|command_group|Use: queryprogram|Type: string|Default: unset|
++-------------+-----------------+------------+--------------+
+
+This option specifies a gid to be set when running the command while routing an
+address for deliver. It must be set if command_user specifies a numerical uid.
+If it begins with a digit, it is interpreted as the numerical value of the gid.
+Otherwise it is looked up using getgrnam().
+
++------------+-----------------+------------+--------------+
+|command_user|Use: queryprogram|Type: string|Default: unset|
++------------+-----------------+------------+--------------+
+
+This option must be set. It specifies the uid which is set when running the
+command while routing an address for delivery. If the value begins with a
+digit, it is interpreted as the numerical value of the uid. Otherwise, it is
+looked up using getpwnam() to obtain a value for the uid and, if command_group
+is not set, a value for the gid also.
+
+Warning: Changing uid and gid is possible only when Exim is running as root,
+which it does during a normal delivery in a conventional configuration.
+However, when an address is being verified during message reception, Exim is
+usually running as the Exim user, not as root. If the queryprogram router is
+called from a non-root process, Exim cannot change uid or gid before running
+the command. In this circumstance the command runs under the current uid and
+gid.
+
++-----------------+-----------------+------------+----------+
+|current_directory|Use: queryprogram|Type: string|Default: /|
++-----------------+-----------------+------------+----------+
+
+This option specifies an absolute path which is made the current directory
+before running the command.
+
++-------+-----------------+----------+-----------+
+|timeout|Use: queryprogram|Type: time|Default: 1h|
++-------+-----------------+----------+-----------+
+
+If the command does not complete within the timeout period, its process group
+is killed and the message is frozen. A value of zero time specifies no timeout.
+
+The standard output of the command is connected to a pipe, which is read when
+the command terminates. It should consist of a single line of output,
+containing up to five fields, separated by white space. The maximum length of
+the line is 1023 characters. Longer lines are silently truncated. The first
+field is one of the following words (case-insensitive):
+
+  * Accept: routing succeeded; the remaining fields specify what to do (see
+    below).
+
+  * Decline: the router declines; pass the address to the next router, unless 
+    no_more is set.
+
+  * Fail: routing failed; do not pass the address to any more routers. Any
+    subsequent text on the line is an error message. If the router is run as
+    part of address verification during an incoming SMTP message, the message
+    is included in the SMTP response.
+
+  * Defer: routing could not be completed at this time; try again later. Any
+    subsequent text on the line is an error message which is logged. It is not
+    included in any SMTP response.
+
+  * Freeze: the same as defer, except that the message is frozen.
+
+  * Pass: pass the address to the next router (or the router specified by 
+    pass_router), overriding no_more.
+
+  * Redirect: the message is redirected. The remainder of the line is a list of
+    new addresses, which are routed independently, starting with the first
+    router, or the router specified by redirect_router, if set.
+
+When the first word is accept, the remainder of the line consists of a number
+of keyed data values, as follows (split into two lines here, to fit on the
+page):
+
+ACCEPT TRANSPORT=<transport> HOSTS=<list of hosts>
+LOOKUP=byname|bydns DATA=<text>
+
+The data items can be given in any order, and all are optional. If no transport
+is included, the transport specified by the generic transport option is used.
+The list of hosts and the lookup type are needed only if the transport is an 
+smtp transport that does not itself supply a list of hosts.
+
+The format of the list of hosts is the same as for the manualroute router. As
+well as host names and IP addresses with optional port numbers, as described in
+section 20.5, it may contain names followed by "/MX" to specify sublists of
+hosts that are obtained by looking up MX records (see section 20.6).
+
+If the lookup type is not specified, Exim behaves as follows when trying to
+find an IP address for each host: First, a DNS lookup is done. If this yields
+anything other than HOST_NOT_FOUND, that result is used. Otherwise, Exim goes
+on to try a call to getipnodebyname() or gethostbyname(), and the result of the
+lookup is the result of that call.
+
+If the DATA field is set, its value is placed in the $address_data variable.
+For example, this return line
+
+accept hosts=x1.y.example:x2.y.example data="rule1"
+
+routes the address to the default transport, passing a list of two hosts. When
+the transport runs, the string "rule1" is in $address_data.
+
+
+
+===============================================================================
+22. THE REDIRECT ROUTER
+
+The redirect router handles several kinds of address redirection. Its most
+common uses are for resolving local part aliases from a central alias file
+(usually called /etc/aliases) and for handling users' personal .forward files,
+but it has many other potential uses. The incoming address can be redirected in
+several different ways:
+
+  * It can be replaced by one or more new addresses which are themselves routed
+    independently.
+
+  * It can be routed to be delivered to a given file or directory.
+
+  * It can be routed to be delivered to a specified pipe command.
+
+  * It can cause an automatic reply to be generated.
+
+  * It can be forced to fail, optionally with a custom error message.
+
+  * It can be temporarily deferred, optionally with a custom message.
+
+  * It can be discarded.
+
+The generic transport option must not be set for redirect routers. However,
+there are some private options which define transports for delivery to files
+and pipes, and for generating autoreplies. See the file_transport, 
+pipe_transport and reply_transport descriptions below.
+
+
+22.1 Redirection data
+---------------------
+
+The router operates by interpreting a text string which it obtains either by
+expanding the contents of the data option, or by reading the entire contents of
+a file whose name is given in the file option. These two options are mutually
+exclusive. The first is commonly used for handling system aliases, in a
+configuration like this:
+
+system_aliases:
+  driver = redirect
+  data = ${lookup{$local_part}lsearch{/etc/aliases}}
+
+If the lookup fails, the expanded string in this example is empty. When the
+expansion of data results in an empty string, the router declines. A forced
+expansion failure also causes the router to decline; other expansion failures
+cause delivery to be deferred.
+
+A configuration using file is commonly used for handling users' .forward files,
+like this:
+
+userforward:
+  driver = redirect
+  check_local_user
+  file = $home/.forward
+  no_verify
+
+If the file does not exist, or causes no action to be taken (for example, it is
+empty or consists only of comments), the router declines. Warning: This is not
+the case when the file contains syntactically valid items that happen to yield
+empty addresses, for example, items containing only RFC 2822 address comments.
+
+
+22.2 Forward files and address verification
+-------------------------------------------
+
+It is usual to set no_verify on redirect routers which handle users' .forward
+files, as in the example above. There are two reasons for this:
+
+  * When Exim is receiving an incoming SMTP message from a remote host, it is
+    running under the Exim uid, not as root. Exim is unable to change uid to
+    read the file as the user, and it may not be able to read it as the Exim
+    user. So in practice the router may not be able to operate.
+
+  * However, even when the router can operate, the existence of a .forward file
+    is unimportant when verifying an address. What should be checked is whether
+    the local part is a valid user name or not. Cutting out the redirection
+    processing saves some resources.
+
+
+22.3 Interpreting redirection data
+----------------------------------
+
+The contents of the data string, whether obtained from data or file, can be
+interpreted in two different ways:
+
+  * If the allow_filter option is set true, and the data begins with the text "
+    #Exim filter" or "#Sieve filter", it is interpreted as a list of filtering
+    instructions in the form of an Exim or Sieve filter file, respectively.
+    Details of the syntax and semantics of filter files are described in a
+    separate document entitled Exim's interfaces to mail filtering; this
+    document is intended for use by end users.
+
+  * Otherwise, the data must be a comma-separated list of redirection items, as
+    described in the next section.
+
+When a message is redirected to a file (a "mail folder"), the file name given
+in a non-filter redirection list must always be an absolute path. A filter may
+generate a relative path - how this is handled depends on the transport's
+configuration. See section 26.1 for a discussion of this issue for the 
+appendfile transport.
+
+
+22.4 Items in a non-filter redirection list
+-------------------------------------------
+
+When the redirection data is not an Exim or Sieve filter, for example, if it
+comes from a conventional alias or forward file, it consists of a list of
+addresses, file names, pipe commands, or certain special items (see section
+22.6 below). The special items can be individually enabled or disabled by means
+of options whose names begin with allow_ or forbid_, depending on their default
+values. The items in the list are separated by commas or newlines. If a comma
+is required in an item, the entire item must be enclosed in double quotes.
+
+Lines starting with a # character are comments, and are ignored, and # may also
+appear following a comma, in which case everything between the # and the next
+newline character is ignored.
+
+If an item is entirely enclosed in double quotes, these are removed. Otherwise
+double quotes are retained because some forms of mail address require their use
+(but never to enclose the entire address). In the following description, "item"
+refers to what remains after any surrounding double quotes have been removed.
+
+Warning: If you use an Exim expansion to construct a redirection address, and
+the expansion contains a reference to $local_part, you should make use of the 
+quote_local_part expansion operator, in case the local part contains special
+characters. For example, to redirect all mail for the domain obsolete.example,
+retaining the existing local part, you could use this setting:
+
+data = ${quote_local_part:$local_part}@newdomain.example
+
+
+22.5 Redirecting to a local mailbox
+-----------------------------------
+
+A redirection item may safely be the same as the address currently under
+consideration. This does not cause a routing loop, because a router is
+automatically skipped if any ancestor of the address that is being processed is
+the same as the current address and was processed by the current router. Such
+an address is therefore passed to the following routers, so it is handled as if
+there were no redirection. When making this loop-avoidance test, the complete
+local part, including any prefix or suffix, is used.
+
+Specifying the same local part without a domain is a common usage in personal
+filter files when the user wants to have messages delivered to the local
+mailbox and also forwarded elsewhere. For example, the user whose login is cleo
+might have a .forward file containing this:
+
+cleo, cleopatra@egypt.example
+
+For compatibility with other MTAs, such unqualified local parts may be preceded
+by "\", but this is not a requirement for loop prevention. However, it does
+make a difference if more than one domain is being handled synonymously.
+
+If an item begins with "\" and the rest of the item parses as a valid RFC 2822
+address that does not include a domain, the item is qualified using the domain
+of the incoming address. In the absence of a leading "\", unqualified addresses
+are qualified using the value in qualify_recipient, but you can force the
+incoming domain to be used by setting qualify_preserve_domain.
+
+Care must be taken if there are alias names for local users. Consider an MTA
+handling a single local domain where the system alias file contains:
+
+Sam.Reman: spqr
+
+Now suppose that Sam (whose login id is spqr) wants to save copies of messages
+in the local mailbox, and also forward copies elsewhere. He creates this
+forward file:
+
+Sam.Reman, spqr@reme.elsewhere.example
+
+With these settings, an incoming message addressed to Sam.Reman fails. The 
+redirect router for system aliases does not process Sam.Reman the second time
+round, because it has previously routed it, and the following routers
+presumably cannot handle the alias. The forward file should really contain
+
+spqr, spqr@reme.elsewhere.example
+
+but because this is such a common error, the check_ancestor option (see below)
+exists to provide a way to get round it. This is normally set on a redirect
+router that is handling users' .forward files.
+
+
+22.6 Special items in redirection lists
+---------------------------------------
+
+In addition to addresses, the following types of item may appear in redirection
+lists (that is, in non-filter redirection data):
+
+  * An item is treated as a pipe command if it begins with "|" and does not
+    parse as a valid RFC 2822 address that includes a domain. A transport for
+    running the command must be specified by the pipe_transport option.
+    Normally, either the router or the transport specifies a user and a group
+    under which to run the delivery. The default is to use the Exim user and
+    group.
+
+    Single or double quotes can be used for enclosing the individual arguments
+    of the pipe command; no interpretation of escapes is done for single
+    quotes. If the command contains a comma character, it is necessary to put
+    the whole item in double quotes, for example:
+
+    "|/some/command ready,steady,go"
+
+    since items in redirection lists are terminated by commas. Do not, however,
+    quote just the command. An item such as
+
+    |"/some/command ready,steady,go"
+
+    is interpreted as a pipe with a rather strange command name, and no
+    arguments.
+
+    Note that the above example assumes that the text comes from a lookup
+    source of some sort, so that the quotes are part of the data. If composing
+    a redirect router with a data option directly specifying this command, the
+    quotes will be used by the configuration parser to define the extent of one
+    string, but will not be passed down into the redirect router itself. There
+    are two main approaches to get around this: escape quotes to be part of the
+    data itself, or avoid using this mechanism and instead create a custom
+    transport with the command option set and reference that transport from an 
+    accept router.
+
+  * An item is interpreted as a path name if it begins with "/" and does not
+    parse as a valid RFC 2822 address that includes a domain. For example,
+
+    /home/world/minbari
+
+    is treated as a file name, but
+
+    /s=molari/o=babylon/@x400gate.way
+
+    is treated as an address. For a file name, a transport must be specified
+    using the file_transport option. However, if the generated path name ends
+    with a forward slash character, it is interpreted as a directory name
+    rather than a file name, and directory_transport is used instead.
+
+    Normally, either the router or the transport specifies a user and a group
+    under which to run the delivery. The default is to use the Exim user and
+    group.
+
+    However, if a redirection item is the path /dev/null, delivery to it is
+    bypassed at a high level, and the log entry shows "**bypassed**" instead of
+    a transport name. In this case the user and group are not used.
+
+  * If an item is of the form
+
+    :include:<path name>
+
+    a list of further items is taken from the given file and included at that
+    point. Note: Such a file can not be a filter file; it is just an
+    out-of-line addition to the list. The items in the included list are
+    separated by commas or newlines and are not subject to expansion. If this
+    is the first item in an alias list in an lsearch file, a colon must be used
+    to terminate the alias name. This example is incorrect:
+
+    list1    :include:/opt/lists/list1
+
+    It must be given as
+
+    list1:   :include:/opt/lists/list1
+
+  * Sometimes you want to throw away mail to a particular local part. Making
+    the data option expand to an empty string does not work, because that
+    causes the router to decline. Instead, the alias item :blackhole: can be
+    used. It does what its name implies. No delivery is done, and no error
+    message is generated. This has the same effect as specifing /dev/null as a
+    destination, but it can be independently disabled.
+
+    Warning: If :blackhole: appears anywhere in a redirection list, no delivery
+    is done for the original local part, even if other redirection items are
+    present. If you are generating a multi-item list (for example, by reading a
+    database) and need the ability to provide a no-op item, you must use /dev/
+    null.
+
+  * An attempt to deliver a particular address can be deferred or forced to
+    fail by redirection items of the form
+
+    :defer:
+    :fail:
+
+    respectively. When a redirection list contains such an item, it applies to
+    the entire redirection; any other items in the list are ignored. Any text
+    following :fail: or :defer: is placed in the error text associated with the
+    failure. For example, an alias file might contain:
+
+    X.Employee:  :fail: Gone away, no forwarding address
+
+    In the case of an address that is being verified from an ACL or as the
+    subject of a VRFY command, the text is included in the SMTP error response
+    by default. The text is not included in the response to an EXPN command. In
+    non-SMTP cases the text is included in the error message that Exim
+    generates.
+
+    By default, Exim sends a 451 SMTP code for a :defer:, and 550 for :fail:.
+    However, if the message starts with three digits followed by a space,
+    optionally followed by an extended code of the form n.n.n, also followed by
+    a space, and the very first digit is the same as the default error code,
+    the code from the message is used instead. If the very first digit is
+    incorrect, a panic error is logged, and the default code is used. You can
+    suppress the use of the supplied code in a redirect router by setting the 
+    forbid_smtp_code option true. In this case, any SMTP code is quietly
+    ignored.
+
+    In an ACL, an explicitly provided message overrides the default, but the
+    default message is available in the variable $acl_verify_message and can
+    therefore be included in a custom message if this is desired.
+
+    Normally the error text is the rest of the redirection list - a comma does
+    not terminate it - but a newline does act as a terminator. Newlines are not
+    normally present in alias expansions. In lsearch lookups they are removed
+    as part of the continuation process, but they may exist in other kinds of
+    lookup and in :include: files.
+
+    During routing for message delivery (as opposed to verification), a
+    redirection containing :fail: causes an immediate failure of the incoming
+    address, whereas :defer: causes the message to remain on the queue so that
+    a subsequent delivery attempt can happen at a later time. If an address is
+    deferred for too long, it will ultimately fail, because the normal retry
+    rules still apply.
+
+  * Sometimes it is useful to use a single-key search type with a default (see
+    chapter 9) to look up aliases. However, there may be a need for exceptions
+    to the default. These can be handled by aliasing them to :unknown:. This
+    differs from :fail: in that it causes the redirect router to decline,
+    whereas :fail: forces routing to fail. A lookup which results in an empty
+    redirection list has the same effect.
+
+
+22.7 Duplicate addresses
+------------------------
+
+Exim removes duplicate addresses from the list to which it is delivering, so as
+to deliver just one copy to each address. This does not apply to deliveries
+routed to pipes by different immediate parent addresses, but an indirect
+aliasing scheme of the type
+
+pipe:       |/some/command $local_part
+localpart1: pipe
+localpart2: pipe
+
+does not work with a message that is addressed to both local parts, because
+when the second is aliased to the intermediate local part "pipe" it gets
+discarded as being the same as a previously handled address. However, a scheme
+such as
+
+localpart1: |/some/command $local_part
+localpart2: |/some/command $local_part
+
+does result in two different pipe deliveries, because the immediate parents of
+the pipes are distinct.
+
+
+22.8 Repeated redirection expansion
+-----------------------------------
+
+When a message cannot be delivered to all of its recipients immediately,
+leading to two or more delivery attempts, redirection expansion is carried out
+afresh each time for those addresses whose children were not all previously
+delivered. If redirection is being used as a mailing list, this can lead to new
+members of the list receiving copies of old messages. The one_time option can
+be used to avoid this.
+
+
+22.9 Errors in redirection lists
+--------------------------------
+
+If skip_syntax_errors is set, a malformed address that causes a parsing error
+is skipped, and an entry is written to the main log. This may be useful for
+mailing lists that are automatically managed. Otherwise, if an error is
+detected while generating the list of new addresses, the original address is
+deferred. See also syntax_errors_to.
+
+
+22.10 Private options for the redirect router
+---------------------------------------------
+
+The private options for the redirect router are as follows:
+
++-----------+-------------+-------------+--------------+
+|allow_defer|Use: redirect|Type: boolean|Default: false|
++-----------+-------------+-------------+--------------+
+
+Setting this option allows the use of :defer: in non-filter redirection data,
+or the defer command in an Exim filter file.
+
++----------+-------------+-------------+--------------+
+|allow_fail|Use: redirect|Type: boolean|Default: false|
++----------+-------------+-------------+--------------+
+
+If this option is true, the :fail: item can be used in a redirection list, and
+the fail command may be used in an Exim filter file.
+
++------------+-------------+-------------+--------------+
+|allow_filter|Use: redirect|Type: boolean|Default: false|
++------------+-------------+-------------+--------------+
+
+Setting this option allows Exim to interpret redirection data that starts with
+"#Exim filter" or "#Sieve filter" as a set of filtering instructions. There are
+some features of Exim filter files that some administrators may wish to lock
+out; see the forbid_filter_xxx options below.
+
+It is also possible to lock out Exim filters or Sieve filters while allowing
+the other type; see forbid_exim_filter and forbid_sieve_filter.
+
+The filter is run using the uid and gid set by the generic user and group
+options. These take their defaults from the password data if check_local_user
+is set, so in the normal case of users' personal filter files, the filter is
+run as the relevant user. When allow_filter is set true, Exim insists that
+either check_local_user or user is set.
+
++------------+-------------+-------------+--------------+
+|allow_freeze|Use: redirect|Type: boolean|Default: false|
++------------+-------------+-------------+--------------+
+
+Setting this option allows the use of the freeze command in an Exim filter.
+This command is more normally encountered in system filters, and is disabled by
+default for redirection filters because it isn't something you usually want to
+let ordinary users do.
+
++--------------+-------------+-------------+--------------+
+|check_ancestor|Use: redirect|Type: boolean|Default: false|
++--------------+-------------+-------------+--------------+
+
+This option is concerned with handling generated addresses that are the same as
+some address in the list of redirection ancestors of the current address.
+Although it is turned off by default in the code, it is set in the default
+configuration file for handling users' .forward files. It is recommended for
+this use of the redirect router.
+
+When check_ancestor is set, if a generated address (including the domain) is
+the same as any ancestor of the current address, it is replaced by a copy of
+the current address. This helps in the case where local part A is aliased to B,
+and B has a .forward file pointing back to A. For example, within a single
+domain, the local part "Joe.Bloggs" is aliased to "jb" and  jb/.forward
+contains:
+
+\Joe.Bloggs, <other item(s)>
+
+Without the check_ancestor setting, either local part ("jb" or "joe.bloggs")
+gets processed once by each router and so ends up as it was originally. If "jb"
+is the real mailbox name, mail to "jb" gets delivered (having been turned into
+"joe.bloggs" by the .forward file and back to "jb" by the alias), but mail to
+"joe.bloggs" fails. Setting check_ancestor on the redirect router that handles
+the .forward file prevents it from turning "jb" back into "joe.bloggs" when
+that was the original address. See also the repeat_use option below.
+
++-----------+-------------+-------------+------------------+
+|check_group|Use: redirect|Type: boolean|Default: see below|
++-----------+-------------+-------------+------------------+
+
+When the file option is used, the group owner of the file is checked only when
+this option is set. The permitted groups are those listed in the owngroups
+option, together with the user's default group if check_local_user is set. If
+the file has the wrong group, routing is deferred. The default setting for this
+option is true if check_local_user is set and the modemask option permits the
+group write bit, or if the owngroups option is set. Otherwise it is false, and
+no group check occurs.
+
++-----------+-------------+-------------+------------------+
+|check_owner|Use: redirect|Type: boolean|Default: see below|
++-----------+-------------+-------------+------------------+
+
+When the file option is used, the owner of the file is checked only when this
+option is set. If check_local_user is set, the local user is permitted;
+otherwise the owner must be one of those listed in the owners option. The
+default value for this option is true if check_local_user or owners is set.
+Otherwise the default is false, and no owner check occurs.
+
++----+-------------+-------------+--------------+
+|data|Use: redirect|Type: string*|Default: unset|
++----+-------------+-------------+--------------+
+
+This option is mutually exclusive with file. One or other of them must be set,
+but not both. The contents of data are expanded, and then used as the list of
+forwarding items, or as a set of filtering instructions. If the expansion is
+forced to fail, or the result is an empty string or a string that has no effect
+(consists entirely of comments), the router declines.
+
+When filtering instructions are used, the string must begin with "#Exim
+filter", and all comments in the string, including this initial one, must be
+terminated with newline characters. For example:
+
+data = #Exim filter\n\
+       if $h_to: contains Exim then save $home/mail/exim endif
+
+If you are reading the data from a database where newlines cannot be included,
+you can use the ${sg} expansion item to turn the escape string of your choice
+into a newline.
+
++-------------------+-------------+-------------+--------------+
+|directory_transport|Use: redirect|Type: string*|Default: unset|
++-------------------+-------------+-------------+--------------+
+
+A redirect router sets up a direct delivery to a directory when a path name
+ending with a slash is specified as a new "address". The transport used is
+specified by this option, which, after expansion, must be the name of a
+configured transport. This should normally be an appendfile transport.
+
++----+-------------+-------------+--------------+
+|file|Use: redirect|Type: string*|Default: unset|
++----+-------------+-------------+--------------+
+
+This option specifies the name of a file that contains the redirection data. It
+is mutually exclusive with the data option. The string is expanded before use;
+if the expansion is forced to fail, the router declines. Other expansion
+failures cause delivery to be deferred. The result of a successful expansion
+must be an absolute path. The entire file is read and used as the redirection
+data. If the data is an empty string or a string that has no effect (consists
+entirely of comments), the router declines.
+
+If the attempt to open the file fails with a "does not exist" error, Exim runs
+a check on the containing directory, unless ignore_enotdir is true (see below).
+If the directory does not appear to exist, delivery is deferred. This can
+happen when users' .forward files are in NFS-mounted directories, and there is
+a mount problem. If the containing directory does exist, but the file does not,
+the router declines.
+
++--------------+-------------+-------------+--------------+
+|file_transport|Use: redirect|Type: string*|Default: unset|
++--------------+-------------+-------------+--------------+
+
+A redirect router sets up a direct delivery to a file when a path name not
+ending in a slash is specified as a new "address". The transport used is
+specified by this option, which, after expansion, must be the name of a
+configured transport. This should normally be an appendfile transport. When it
+is running, the file name is in $address_file.
+
++-------------------+-------------+-------------+-------------+
+|filter_prepend_home|Use: redirect|Type: boolean|Default: true|
++-------------------+-------------+-------------+-------------+
+
+When this option is true, if a save command in an Exim filter specifies a
+relative path, and $home is defined, it is automatically prepended to the
+relative path. If this option is set false, this action does not happen. The
+relative path is then passed to the transport unmodified.
+
++----------------+-------------+-------------+--------------+
+|forbid_blackhole|Use: redirect|Type: boolean|Default: false|
++----------------+-------------+-------------+--------------+
+
+If this option is true, the :blackhole: item may not appear in a redirection
+list.
+
++------------------+-------------+-------------+--------------+
+|forbid_exim_filter|Use: redirect|Type: boolean|Default: false|
++------------------+-------------+-------------+--------------+
+
+If this option is set true, only Sieve filters are permitted when allow_filter
+is true.
+
++-----------+-------------+-------------+--------------+
+|forbid_file|Use: redirect|Type: boolean|Default: false|
++-----------+-------------+-------------+--------------+
+
+If this option is true, this router may not generate a new address that
+specifies delivery to a local file or directory, either from a filter or from a
+conventional forward file. This option is forced to be true if one_time is set.
+It applies to Sieve filters as well as to Exim filters, but if true, it locks
+out the Sieve's "keep" facility.
+
++--------------------+-------------+-------------+--------------+
+|forbid_filter_dlfunc|Use: redirect|Type: boolean|Default: false|
++--------------------+-------------+-------------+--------------+
+
+If this option is true, string expansions in Exim filters are not allowed to
+make use of the dlfunc expansion facility to run dynamically loaded functions.
+
++------------------------+-------------+-------------+--------------+
+|forbid_filter_existstest|Use: redirect|Type: boolean|Default: false|
++------------------------+-------------+-------------+--------------+
+
+If this option is true, string expansions in Exim filters are not allowed to
+make use of the exists condition or the stat expansion item.
+
++----------------------+-------------+-------------+--------------+
+|forbid_filter_logwrite|Use: redirect|Type: boolean|Default: false|
++----------------------+-------------+-------------+--------------+
+
+If this option is true, use of the logging facility in Exim filters is not
+permitted. Logging is in any case available only if the filter is being run
+under some unprivileged uid (which is normally the case for ordinary users'
+.forward files).
+
++--------------------+-------------+-------------+--------------+
+|forbid_filter_lookup|Use: redirect|Type: boolean|Default: false|
++--------------------+-------------+-------------+--------------+
+
+If this option is true, string expansions in Exim filter files are not allowed
+to make use of lookup items.
+
++------------------+-------------+-------------+--------------+
+|forbid_filter_perl|Use: redirect|Type: boolean|Default: false|
++------------------+-------------+-------------+--------------+
+
+This option has an effect only if Exim is built with embedded Perl support. If
+it is true, string expansions in Exim filter files are not allowed to make use
+of the embedded Perl support.
+
++----------------------+-------------+-------------+--------------+
+|forbid_filter_readfile|Use: redirect|Type: boolean|Default: false|
++----------------------+-------------+-------------+--------------+
+
+If this option is true, string expansions in Exim filter files are not allowed
+to make use of readfile items.
+
++------------------------+-------------+-------------+--------------+
+|forbid_filter_readsocket|Use: redirect|Type: boolean|Default: false|
++------------------------+-------------+-------------+--------------+
+
+If this option is true, string expansions in Exim filter files are not allowed
+to make use of readsocket items.
+
++-------------------+-------------+-------------+--------------+
+|forbid_filter_reply|Use: redirect|Type: boolean|Default: false|
++-------------------+-------------+-------------+--------------+
+
+If this option is true, this router may not generate an automatic reply
+message. Automatic replies can be generated only from Exim or Sieve filter
+files, not from traditional forward files. This option is forced to be true if 
+one_time is set.
+
++-----------------+-------------+-------------+--------------+
+|forbid_filter_run|Use: redirect|Type: boolean|Default: false|
++-----------------+-------------+-------------+--------------+
+
+If this option is true, string expansions in Exim filter files are not allowed
+to make use of run items.
+
++--------------+-------------+-------------+--------------+
+|forbid_include|Use: redirect|Type: boolean|Default: false|
++--------------+-------------+-------------+--------------+
+
+If this option is true, items of the form
+
+:include:<path name>
+
+are not permitted in non-filter redirection lists.
+
++-----------+-------------+-------------+--------------+
+|forbid_pipe|Use: redirect|Type: boolean|Default: false|
++-----------+-------------+-------------+--------------+
+
+If this option is true, this router may not generate a new address which
+specifies delivery to a pipe, either from an Exim filter or from a conventional
+forward file. This option is forced to be true if one_time is set.
+
++-------------------+-------------+-------------+--------------+
+|forbid_sieve_filter|Use: redirect|Type: boolean|Default: false|
++-------------------+-------------+-------------+--------------+
+
+If this option is set true, only Exim filters are permitted when allow_filter
+is true.
+
++----------------+-------------+-------------+--------------+
+|forbid_smtp_code|Use: redirect|Type: boolean|Default: false|
++----------------+-------------+-------------+--------------+
+
+If this option is set true, any SMTP error codes that are present at the start
+of messages specified for ":defer:" or ":fail:" are quietly ignored, and the
+default codes (451 and 550, respectively) are always used.
+
++--------------------+-------------+-------------+--------------+
+|hide_child_in_errmsg|Use: redirect|Type: boolean|Default: false|
++--------------------+-------------+-------------+--------------+
+
+If this option is true, it prevents Exim from quoting a child address if it
+generates a bounce or delay message for it. Instead it says "an address
+generated from <the top level address>". Of course, this applies only to
+bounces generated locally. If a message is forwarded to another host, its
+bounce may well quote the generated address.
+
++-------------+-------------+-------------+--------------+
+|ignore_eacces|Use: redirect|Type: boolean|Default: false|
++-------------+-------------+-------------+--------------+
+
+If this option is set and an attempt to open a redirection file yields the
+EACCES error (permission denied), the redirect router behaves as if the file
+did not exist.
+
++--------------+-------------+-------------+--------------+
+|ignore_enotdir|Use: redirect|Type: boolean|Default: false|
++--------------+-------------+-------------+--------------+
+
+If this option is set and an attempt to open a redirection file yields the
+ENOTDIR error (something on the path is not a directory), the redirect router
+behaves as if the file did not exist.
+
+Setting ignore_enotdir has another effect as well: When a redirect router that
+has the file option set discovers that the file does not exist (the ENOENT
+error), it tries to stat() the parent directory, as a check against unmounted
+NFS directories. If the parent can not be statted, delivery is deferred.
+However, it seems wrong to do this check when ignore_enotdir is set, because
+that option tells Exim to ignore "something on the path is not a directory"
+(the ENOTDIR error). This is a confusing area, because it seems that some
+operating systems give ENOENT where others give ENOTDIR.
+
++-----------------+-------------+------------+--------------+
+|include_directory|Use: redirect|Type: string|Default: unset|
++-----------------+-------------+------------+--------------+
+
+If this option is set, the path names of any :include: items in a redirection
+list must start with this directory.
+
++--------+-------------+-------------------+------------+
+|modemask|Use: redirect|Type: octal integer|Default: 022|
++--------+-------------+-------------------+------------+
+
+This specifies mode bits which must not be set for a file specified by the file
+option. If any of the forbidden bits are set, delivery is deferred.
+
++--------+-------------+-------------+--------------+
+|one_time|Use: redirect|Type: boolean|Default: false|
++--------+-------------+-------------+--------------+
+
+Sometimes the fact that Exim re-evaluates aliases and reprocesses redirection
+files each time it tries to deliver a message causes a problem when one or more
+of the generated addresses fails be delivered at the first attempt. The problem
+is not one of duplicate delivery - Exim is clever enough to handle that - but
+of what happens when the redirection list changes during the time that the
+message is on Exim's queue. This is particularly true in the case of mailing
+lists, where new subscribers might receive copies of messages that were posted
+before they subscribed.
+
+If one_time is set and any addresses generated by the router fail to deliver at
+the first attempt, the failing addresses are added to the message as "top
+level" addresses, and the parent address that generated them is marked
+"delivered". Thus, redirection does not happen again at the next delivery
+attempt.
+
+Warning 1: Any header line addition or removal that is specified by this router
+would be lost if delivery did not succeed at the first attempt. For this
+reason, the headers_add and headers_remove generic options are not permitted
+when one_time is set.
+
+Warning 2: To ensure that the router generates only addresses (as opposed to
+pipe or file deliveries or auto-replies) forbid_file, forbid_pipe, and 
+forbid_filter_reply are forced to be true when one_time is set.
+
+Warning 3: The unseen generic router option may not be set with one_time.
+
+The original top-level address is remembered with each of the generated
+addresses, and is output in any log messages. However, any intermediate parent
+addresses are not recorded. This makes a difference to the log only if 
+all_parents log selector is set. It is expected that one_time will typically be
+used for mailing lists, where there is normally just one level of expansion.
+
++------+-------------+-----------------+--------------+
+|owners|Use: redirect|Type: string list|Default: unset|
++------+-------------+-----------------+--------------+
+
+This specifies a list of permitted owners for the file specified by file. This
+list is in addition to the local user when check_local_user is set. See 
+check_owner above.
+
++---------+-------------+-----------------+--------------+
+|owngroups|Use: redirect|Type: string list|Default: unset|
++---------+-------------+-----------------+--------------+
+
+This specifies a list of permitted groups for the file specified by file. The
+list is in addition to the local user's primary group when check_local_user is
+set. See check_group above.
+
++--------------+-------------+-------------+--------------+
+|pipe_transport|Use: redirect|Type: string*|Default: unset|
++--------------+-------------+-------------+--------------+
+
+A redirect router sets up a direct delivery to a pipe when a string starting
+with a vertical bar character is specified as a new "address". The transport
+used is specified by this option, which, after expansion, must be the name of a
+configured transport. This should normally be a pipe transport. When the
+transport is run, the pipe command is in $address_pipe.
+
++--------------+-------------+-------------+--------------+
+|qualify_domain|Use: redirect|Type: string*|Default: unset|
++--------------+-------------+-------------+--------------+
+
+If this option is set, and an unqualified address (one without a domain) is
+generated, and that address would normally be qualified by the global setting
+in qualify_recipient, it is instead qualified with the domain specified by
+expanding this string. If the expansion fails, the router declines. If you want
+to revert to the default, you can have the expansion generate
+$qualify_recipient.
+
+This option applies to all unqualified addresses generated by Exim filters, but
+for traditional .forward files, it applies only to addresses that are not
+preceded by a backslash. Sieve filters cannot generate unqualified addresses.
+
++-----------------------+-------------+-------------+--------------+
+|qualify_preserve_domain|Use: redirect|Type: boolean|Default: false|
++-----------------------+-------------+-------------+--------------+
+
+If this option is set, the router's local qualify_domain option must not be set
+(a configuration error occurs if it is). If an unqualified address (one without
+a domain) is generated, it is qualified with the domain of the parent address
+(the immediately preceding ancestor) instead of the global qualify_recipient
+value. In the case of a traditional .forward file, this applies whether or not
+the address is preceded by a backslash.
+
++----------+-------------+-------------+-------------+
+|repeat_use|Use: redirect|Type: boolean|Default: true|
++----------+-------------+-------------+-------------+
+
+If this option is set false, the router is skipped for a child address that has
+any ancestor that was routed by this router. This test happens before any of
+the other preconditions are tested. Exim's default anti-looping rules skip only
+when the ancestor is the same as the current address. See also check_ancestor
+above and the generic redirect_router option.
+
++---------------+-------------+-------------+--------------+
+|reply_transport|Use: redirect|Type: string*|Default: unset|
++---------------+-------------+-------------+--------------+
+
+A redirect router sets up an automatic reply when a mail or vacation command is
+used in a filter file. The transport used is specified by this option, which,
+after expansion, must be the name of a configured transport. This should
+normally be an autoreply transport. Other transports are unlikely to do
+anything sensible or useful.
+
++-------+-------------+-------------+-------------+
+|rewrite|Use: redirect|Type: boolean|Default: true|
++-------+-------------+-------------+-------------+
+
+If this option is set false, addresses generated by the router are not subject
+to address rewriting. Otherwise, they are treated like new addresses and are
+rewritten according to the global rewriting rules.
+
++----------------+-------------+-------------+--------------+
+|sieve_subaddress|Use: redirect|Type: string*|Default: unset|
++----------------+-------------+-------------+--------------+
+
+The value of this option is passed to a Sieve filter to specify the :subaddress
+part of an address.
+
++-----------------+-------------+-------------+--------------+
+|sieve_useraddress|Use: redirect|Type: string*|Default: unset|
++-----------------+-------------+-------------+--------------+
+
+The value of this option is passed to a Sieve filter to specify the :user part
+of an address. However, if it is unset, the entire original local part
+(including any prefix or suffix) is used for :user.
+
++------------------------+-------------+-------------+--------------+
+|sieve_vacation_directory|Use: redirect|Type: string*|Default: unset|
++------------------------+-------------+-------------+--------------+
+
+To enable the "vacation" extension for Sieve filters, you must set 
+sieve_vacation_directory to the directory where vacation databases are held (do
+not put anything else in that directory), and ensure that the reply_transport
+option refers to an autoreply transport. Each user needs their own directory;
+Exim will create it if necessary.
+
++------------------+-------------+-------------+--------------+
+|skip_syntax_errors|Use: redirect|Type: boolean|Default: false|
++------------------+-------------+-------------+--------------+
+
+If skip_syntax_errors is set, syntactically malformed addresses in non-filter
+redirection data are skipped, and each failing address is logged. If 
+syntax_errors_to is set, a message is sent to the address it defines, giving
+details of the failures. If syntax_errors_text is set, its contents are
+expanded and placed at the head of the error message generated by 
+syntax_errors_to. Usually it is appropriate to set syntax_errors_to to be the
+same address as the generic errors_to option. The skip_syntax_errors option is
+often used when handling mailing lists.
+
+If all the addresses in a redirection list are skipped because of syntax
+errors, the router declines to handle the original address, and it is passed to
+the following routers.
+
+If skip_syntax_errors is set when an Exim filter is interpreted, any syntax
+error in the filter causes filtering to be abandoned without any action being
+taken. The incident is logged, and the router declines to handle the address,
+so it is passed to the following routers.
+
+Syntax errors in a Sieve filter file cause the "keep" action to occur. This
+action is specified by RFC 3028. The values of skip_syntax_errors, 
+syntax_errors_to, and syntax_errors_text are not used.
+
+skip_syntax_errors can be used to specify that errors in users' forward lists
+or filter files should not prevent delivery. The syntax_errors_to option, used
+with an address that does not get redirected, can be used to notify users of
+these errors, by means of a router like this:
+
+userforward:
+  driver = redirect
+  allow_filter
+  check_local_user
+  file = $home/.forward
+  file_transport = address_file
+  pipe_transport = address_pipe
+  reply_transport = address_reply
+  no_verify
+  skip_syntax_errors
+  syntax_errors_to = real-$local_part@$domain
+  syntax_errors_text = \
+   This is an automatically generated message. An error has\n\
+   been found in your .forward file. Details of the error are\n\
+   reported below. While this error persists, you will receive\n\
+   a copy of this message for every message that is addressed\n\
+   to you. If your .forward file is a filter file, or if it is\n\
+   a non-filter file containing no valid forwarding addresses,\n\
+   a copy of each incoming message will be put in your normal\n\
+   mailbox. If a non-filter file contains at least one valid\n\
+   forwarding address, forwarding to the valid addresses will\n\
+   happen, and those will be the only deliveries that occur.
+
+You also need a router to ensure that local addresses that are prefixed by
+"real-" are recognized, but not forwarded or filtered. For example, you could
+put this immediately before the userforward router:
+
+real_localuser:
+  driver = accept
+  check_local_user
+  local_part_prefix = real-
+  transport = local_delivery
+
+For security, it would probably be a good idea to restrict the use of this
+router to locally-generated messages, using a condition such as this:
+
+  condition = ${if match {$sender_host_address}\
+                         {\N^(|127\.0\.0\.1)$\N}}
+
++------------------+-------------+-------------+--------------+
+|syntax_errors_text|Use: redirect|Type: string*|Default: unset|
++------------------+-------------+-------------+--------------+
+
+See skip_syntax_errors above.
+
++----------------+-------------+------------+--------------+
+|syntax_errors_to|Use: redirect|Type: string|Default: unset|
++----------------+-------------+------------+--------------+
+
+See skip_syntax_errors above.
+
+
+
+===============================================================================
+23. ENVIRONMENT FOR RUNNING LOCAL TRANSPORTS
+
+Local transports handle deliveries to files and pipes. (The autoreply transport
+can be thought of as similar to a pipe.) Exim always runs transports in
+subprocesses, under specified uids and gids. Typical deliveries to local
+mailboxes run under the uid and gid of the local user.
+
+Exim also sets a specific current directory while running the transport; for
+some transports a home directory setting is also relevant. The pipe transport
+is the only one that sets up environment variables; see section 29.4 for
+details.
+
+The values used for the uid, gid, and the directories may come from several
+different places. In many cases, the router that handles the address associates
+settings with that address as a result of its check_local_user, group, or user
+options. However, values may also be given in the transport's own
+configuration, and these override anything that comes from the router.
+
+
+23.1 Concurrent deliveries
+--------------------------
+
+If two different messages for the same local recipient arrive more or less
+simultaneously, the two delivery processes are likely to run concurrently. When
+the appendfile transport is used to write to a file, Exim applies locking rules
+to stop concurrent processes from writing to the same file at the same time.
+
+However, when you use a pipe transport, it is up to you to arrange any locking
+that is needed. Here is a silly example:
+
+my_transport:
+  driver = pipe
+  command = /bin/sh -c 'cat >>/some/file'
+
+This is supposed to write the message at the end of the file. However, if two
+messages arrive at the same time, the file will be scrambled. You can use the 
+exim_lock utility program (see section 52.15) to lock a file using the same
+algorithm that Exim itself uses.
+
+
+23.2 Uids and gids
+------------------
+
+All transports have the options group and user. If group is set, it overrides
+any group that the router set in the address, even if user is not set for the
+transport. This makes it possible, for example, to run local mail delivery
+under the uid of the recipient (set by the router), but in a special group (set
+by the transport). For example:
+
+# Routers ...
+# User/group are set by check_local_user in this router
+local_users:
+  driver = accept
+  check_local_user
+  transport = group_delivery
+
+# Transports ...
+# This transport overrides the group
+group_delivery:
+  driver = appendfile
+  file = /var/spool/mail/$local_part
+  group = mail
+
+If user is set for a transport, its value overrides what is set in the address
+by the router. If user is non-numeric and group is not set, the gid associated
+with the user is used. If user is numeric, group must be set.
+
+When the uid is taken from the transport's configuration, the initgroups()
+function is called for the groups associated with that uid if the initgroups
+option is set for the transport. When the uid is not specified by the
+transport, but is associated with the address by a router, the option for
+calling initgroups() is taken from the router configuration.
+
+The pipe transport contains the special option pipe_as_creator. If this is set
+and user is not set, the uid of the process that called Exim to receive the
+message is used, and if group is not set, the corresponding original gid is
+also used.
+
+This is the detailed preference order for obtaining a gid; the first of the
+following that is set is used:
+
+  * A group setting of the transport;
+
+  * A group setting of the router;
+
+  * A gid associated with a user setting of the router, either as a result of 
+    check_local_user or an explicit non-numeric user setting;
+
+  * The group associated with a non-numeric user setting of the transport;
+
+  * In a pipe transport, the creator's gid if deliver_as_creator is set and the
+    uid is the creator's uid;
+
+  * The Exim gid if the Exim uid is being used as a default.
+
+If, for example, the user is specified numerically on the router and there are
+no group settings, no gid is available. In this situation, an error occurs.
+This is different for the uid, for which there always is an ultimate default.
+The first of the following that is set is used:
+
+  * A user setting of the transport;
+
+  * In a pipe transport, the creator's uid if deliver_as_creator is set;
+
+  * A user setting of the router;
+
+  * A check_local_user setting of the router;
+
+  * The Exim uid.
+
+Of course, an error will still occur if the uid that is chosen is on the 
+never_users list.
+
+
+23.3 Current and home directories
+---------------------------------
+
+Routers may set current and home directories for local transports by means of
+the transport_current_directory and transport_home_directory options. However,
+if the transport's current_directory or home_directory options are set, they
+override the router's values. In detail, the home directory for a local
+transport is taken from the first of these values that is set:
+
+  * The home_directory option on the transport;
+
+  * The transport_home_directory option on the router;
+
+  * The password data if check_local_user is set on the router;
+
+  * The router_home_directory option on the router.
+
+The current directory is taken from the first of these values that is set:
+
+  * The current_directory option on the transport;
+
+  * The transport_current_directory option on the router.
+
+If neither the router nor the transport sets a current directory, Exim uses the
+value of the home directory, if it is set. Otherwise it sets the current
+directory to / before running a local transport.
+
+
+23.4 Expansion variables derived from the address
+-------------------------------------------------
+
+Normally a local delivery is handling a single address, and in that case the
+variables such as $domain and $local_part are set during local deliveries.
+However, in some circumstances more than one address may be handled at once
+(for example, while writing batch SMTP for onward transmission by some other
+means). In this case, the variables associated with the local part are never
+set, $domain is set only if all the addresses have the same domain, and
+$original_domain is never set.
+
+
+
+===============================================================================
+24. GENERIC OPTIONS FOR TRANSPORTS
+
+The following generic options apply to all transports:
+
++---------+---------------+-------------+--------------+
+|body_only|Use: transports|Type: boolean|Default: false|
++---------+---------------+-------------+--------------+
+
+If this option is set, the message's headers are not transported. It is
+mutually exclusive with headers_only. If it is used with the appendfile or pipe
+transports, the settings of message_prefix and message_suffix should be
+checked, because this option does not automatically suppress them.
+
++-----------------+---------------+-------------+--------------+
+|current_directory|Use: transports|Type: string*|Default: unset|
++-----------------+---------------+-------------+--------------+
+
+This specifies the current directory that is to be set while running the
+transport, overriding any value that may have been set by the router. If the
+expansion fails for any reason, including forced failure, an error is logged,
+and delivery is deferred.
+
++---------------+---------------+-------------+--------------+
+|disable_logging|Use: transports|Type: boolean|Default: false|
++---------------+---------------+-------------+--------------+
+
+If this option is set true, nothing is logged for any deliveries by the
+transport or for any transport errors. You should not set this option unless
+you really, really know what you are doing.
+
++-----------+---------------+-------------+--------------+
+|debug_print|Use: transports|Type: string*|Default: unset|
++-----------+---------------+-------------+--------------+
+
+If this option is set and debugging is enabled (see the -d command line
+option), the string is expanded and included in the debugging output when the
+transport is run. If expansion of the string fails, the error message is
+written to the debugging output, and Exim carries on processing. This facility
+is provided to help with checking out the values of variables and so on when
+debugging driver configurations. For example, if a headers_add option is not
+working properly, debug_print could be used to output the variables it
+references. A newline is added to the text if it does not end with one. The
+variables $transport_name and $router_name contain the name of the transport
+and the router that called it.
+
++-----------------+---------------+-------------+--------------+
+|delivery_date_add|Use: transports|Type: boolean|Default: false|
++-----------------+---------------+-------------+--------------+
+
+If this option is true, a Delivery-date: header is added to the message. This
+gives the actual time the delivery was made. As this is not a standard header,
+Exim has a configuration option (delivery_date_remove) which requests its
+removal from incoming messages, so that delivered messages can safely be resent
+to other recipients.
+
++------+---------------+------------+--------------+
+|driver|Use: transports|Type: string|Default: unset|
++------+---------------+------------+--------------+
+
+This specifies which of the available transport drivers is to be used. There is
+no default, and this option must be set for every transport.
+
++---------------+---------------+-------------+--------------+
+|envelope_to_add|Use: transports|Type: boolean|Default: false|
++---------------+---------------+-------------+--------------+
+
+If this option is true, an Envelope-to: header is added to the message. This
+gives the original address(es) in the incoming envelope that caused this
+delivery to happen. More than one address may be present if the transport is
+configured to handle several addresses at once, or if more than one original
+address was redirected to the same final address. As this is not a standard
+header, Exim has a configuration option (envelope_to_remove) which requests its
+removal from incoming messages, so that delivered messages can safely be resent
+to other recipients.
+
++-----+---------------+-------------+-------------------+
+|group|Use: transports|Type: string*|Default: Exim group|
++-----+---------------+-------------+-------------------+
+
+This option specifies a gid for running the transport process, overriding any
+value that the router supplies, and also overriding any value associated with 
+user (see below).
+
++-----------+---------------+-----------+--------------+
+|headers_add|Use: transports|Type: list*|Default: unset|
++-----------+---------------+-----------+--------------+
+
+This option specifies a list of text headers, newline-separated, which are
+(separately) expanded and added to the header portion of a message as it is
+transported, as described in section 46.17. Additional header lines can also be
+specified by routers. If the result of the expansion is an empty string, or if
+the expansion is forced to fail, no action is taken. Other expansion failures
+are treated as errors and cause the delivery to be deferred.
+
+Unlike most options, headers_add can be specified multiple times for a
+transport; all listed headers are added.
+
++------------+---------------+-------------+--------------+
+|headers_only|Use: transports|Type: boolean|Default: false|
++------------+---------------+-------------+--------------+
+
+If this option is set, the message's body is not transported. It is mutually
+exclusive with body_only. If it is used with the appendfile or pipe transports,
+the settings of message_prefix and message_suffix should be checked, since this
+option does not automatically suppress them.
+
++--------------+---------------+-----------+--------------+
+|headers_remove|Use: transports|Type: list*|Default: unset|
++--------------+---------------+-----------+--------------+
+
+This option specifies a list of header names, colon-separated; these headers
+are omitted from the message as it is transported, as described in section
+46.17. Header removal can also be specified by routers. Each list item is
+separately expanded. If the result of the expansion is an empty string, or if
+the expansion is forced to fail, no action is taken. Other expansion failures
+are treated as errors and cause the delivery to be deferred.
+
+Unlike most options, headers_remove can be specified multiple times for a
+router; all listed headers are removed.
+
++---------------+---------------+------------+--------------+
+|headers_rewrite|Use: transports|Type: string|Default: unset|
++---------------+---------------+------------+--------------+
+
+This option allows addresses in header lines to be rewritten at transport time,
+that is, as the message is being copied to its destination. The contents of the
+option are a colon-separated list of rewriting rules. Each rule is in exactly
+the same form as one of the general rewriting rules that are applied when a
+message is received. These are described in chapter 31. For example,
+
+headers_rewrite = a@b c@d f : \
+                  x@y w@z
+
+changes a@b into c@d in From: header lines, and x@y into w@z in all
+address-bearing header lines. The rules are applied to the header lines just
+before they are written out at transport time, so they affect only those copies
+of the message that pass through the transport. However, only the message's
+original header lines, and any that were added by a system filter, are
+rewritten. If a router or transport adds header lines, they are not affected by
+this option. These rewriting rules are not applied to the envelope. You can
+change the return path using return_path, but you cannot change envelope
+recipients at this time.
+
++--------------+---------------+-------------+--------------+
+|home_directory|Use: transports|Type: string*|Default: unset|
++--------------+---------------+-------------+--------------+
+
+This option specifies a home directory setting for a local transport,
+overriding any value that may be set by the router. The home directory is
+placed in $home while expanding the transport's private options. It is also
+used as the current directory if no current directory is set by the 
+current_directory option on the transport or the transport_current_directory
+option on the router. If the expansion fails for any reason, including forced
+failure, an error is logged, and delivery is deferred.
+
++----------+---------------+-------------+--------------+
+|initgroups|Use: transports|Type: boolean|Default: false|
++----------+---------------+-------------+--------------+
+
+If this option is true and the uid for the delivery process is provided by the
+transport, the initgroups() function is called when running the transport to
+ensure that any additional groups associated with the uid are set up.
+
++------------------+---------------+-------------+----------+
+|message_size_limit|Use: transports|Type: string*|Default: 0|
++------------------+---------------+-------------+----------+
+
+This option controls the size of messages passed through the transport. It is
+expanded before use; the result of the expansion must be a sequence of decimal
+digits, optionally followed by K or M. If the expansion fails for any reason,
+including forced failure, or if the result is not of the required form,
+delivery is deferred. If the value is greater than zero and the size of a
+message exceeds this limit, the address is failed. If there is any chance that
+the resulting bounce message could be routed to the same transport, you should
+ensure that return_size_limit is less than the transport's message_size_limit,
+as otherwise the bounce message will fail to get delivered.
+
++--------------------+---------------+-------------+--------------+
+|rcpt_include_affixes|Use: transports|Type: boolean|Default: false|
++--------------------+---------------+-------------+--------------+
+
+When this option is false (the default), and an address that has had any
+affixes (prefixes or suffixes) removed from the local part is delivered by any
+form of SMTP or LMTP, the affixes are not included. For example, if a router
+that contains
+
+local_part_prefix = *-
+
+routes the address abc-xyz@some.domain to an SMTP transport, the envelope is
+delivered with
+
+RCPT TO:<xyz@some.domain>
+
+This is also the case when an ACL-time callout is being used to verify a
+recipient address. However, if rcpt_include_affixes is set true, the whole
+local part is included in the RCPT command. This option applies to BSMTP
+deliveries by the appendfile and pipe transports as well as to the lmtp and 
+smtp transports.
+
++--------------------+---------------+-------------+------------------+
+|retry_use_local_part|Use: transports|Type: boolean|Default: see below|
++--------------------+---------------+-------------+------------------+
+
+When a delivery suffers a temporary failure, a retry record is created in
+Exim's hints database. For remote deliveries, the key for the retry record is
+based on the name and/or IP address of the failing remote host. For local
+deliveries, the key is normally the entire address, including both the local
+part and the domain. This is suitable for most common cases of local delivery
+temporary failure - for example, exceeding a mailbox quota should delay only
+deliveries to that mailbox, not to the whole domain.
+
+However, in some special cases you may want to treat a temporary local delivery
+as a failure associated with the domain, and not with a particular local part.
+(For example, if you are storing all mail for some domain in files.) You can do
+this by setting retry_use_local_part false.
+
+For all the local transports, its default value is true. For remote transports,
+the default value is false for tidiness, but changing the value has no effect
+on a remote transport in the current implementation.
+
++-----------+---------------+-------------+--------------+
+|return_path|Use: transports|Type: string*|Default: unset|
++-----------+---------------+-------------+--------------+
+
+If this option is set, the string is expanded at transport time and replaces
+the existing return path (envelope sender) value in the copy of the message
+that is being delivered. An empty return path is permitted. This feature is
+designed for remote deliveries, where the value of this option is used in the
+SMTP MAIL command. If you set return_path for a local transport, the only
+effect is to change the address that is placed in the Return-path: header line,
+if one is added to the message (see the next option).
+
+Note: A changed return path is not logged unless you add 
+return_path_on_delivery to the log selector.
+
+The expansion can refer to the existing value via $return_path. This is either
+the message's envelope sender, or an address set by the errors_to option on a
+router. If the expansion is forced to fail, no replacement occurs; if it fails
+for another reason, delivery is deferred. This option can be used to support
+VERP (Variable Envelope Return Paths) - see section 49.6.
+
+Note: If a delivery error is detected locally, including the case when a remote
+server rejects a message at SMTP time, the bounce message is not sent to the
+value of this option. It is sent to the previously set errors address. This
+defaults to the incoming sender address, but can be changed by setting 
+errors_to in a router.
+
++---------------+---------------+-------------+--------------+
+|return_path_add|Use: transports|Type: boolean|Default: false|
++---------------+---------------+-------------+--------------+
+
+If this option is true, a Return-path: header is added to the message. Although
+the return path is normally available in the prefix line of BSD mailboxes, this
+is commonly not displayed by MUAs, and so the user does not have easy access to
+it.
+
+RFC 2821 states that the Return-path: header is added to a message "when the
+delivery SMTP server makes the final delivery". This implies that this header
+should not be present in incoming messages. Exim has a configuration option, 
+return_path_remove, which requests removal of this header from incoming
+messages, so that delivered messages can safely be resent to other recipients.
+
++----------------+---------------+-------------+--------------+
+|shadow_condition|Use: transports|Type: string*|Default: unset|
++----------------+---------------+-------------+--------------+
+
+See shadow_transport below.
+
++----------------+---------------+------------+--------------+
+|shadow_transport|Use: transports|Type: string|Default: unset|
++----------------+---------------+------------+--------------+
+
+A local transport may set the shadow_transport option to the name of another
+local transport. Shadow remote transports are not supported.
+
+Whenever a delivery to the main transport succeeds, and either shadow_condition
+is unset, or its expansion does not result in the empty string or one of the
+strings "0" or "no" or "false", the message is also passed to the shadow
+transport, with the same delivery address or addresses. If expansion fails, no
+action is taken except that non-forced expansion failures cause a log line to
+be written.
+
+The result of the shadow transport is discarded and does not affect the
+subsequent processing of the message. Only a single level of shadowing is
+provided; the shadow_transport option is ignored on any transport when it is
+running as a shadow. Options concerned with output from pipes are also ignored.
+The log line for the successful delivery has an item added on the end, of the
+form
+
+ST=<shadow transport name>
+
+If the shadow transport did not succeed, the error message is put in
+parentheses afterwards. Shadow transports can be used for a number of different
+purposes, including keeping more detailed log information than Exim normally
+provides, and implementing automatic acknowledgment policies based on message
+headers that some sites insist on.
+
++----------------+---------------+-------------+--------------+
+|transport_filter|Use: transports|Type: string*|Default: unset|
++----------------+---------------+-------------+--------------+
+
+This option sets up a filtering (in the Unix shell sense) process for messages
+at transport time. It should not be confused with mail filtering as set up by
+individual users or via a system filter.
+
+When the message is about to be written out, the command specified by 
+transport_filter is started up in a separate, parallel process, and the entire
+message, including the header lines, is passed to it on its standard input
+(this in fact is done from a third process, to avoid deadlock). The command
+must be specified as an absolute path.
+
+The lines of the message that are written to the transport filter are
+terminated by newline ("\n"). The message is passed to the filter before any
+SMTP-specific processing, such as turning "\n" into "\r\n" and escaping lines
+beginning with a dot, and also before any processing implied by the settings of
+check_string and escape_string in the appendfile or pipe transports.
+
+The standard error for the filter process is set to the same destination as its
+standard output; this is read and written to the message's ultimate
+destination. The process that writes the message to the filter, the filter
+itself, and the original process that reads the result and delivers it are all
+run in parallel, like a shell pipeline.
+
+The filter can perform any transformations it likes, but of course should take
+care not to break RFC 2822 syntax. Exim does not check the result, except to
+test for a final newline when SMTP is in use. All messages transmitted over
+SMTP must end with a newline, so Exim supplies one if it is missing.
+
+A transport filter can be used to provide content-scanning on a per-user basis
+at delivery time if the only required effect of the scan is to modify the
+message. For example, a content scan could insert a new header line containing
+a spam score. This could be interpreted by a filter in the user's MUA. It is
+not possible to discard a message at this stage.
+
+A problem might arise if the filter increases the size of a message that is
+being sent down an SMTP connection. If the receiving SMTP server has indicated
+support for the SIZE parameter, Exim will have sent the size of the message at
+the start of the SMTP session. If what is actually sent is substantially more,
+the server might reject the message. This can be worked round by setting the 
+size_addition option on the smtp transport, either to allow for additions to
+the message, or to disable the use of SIZE altogether.
+
+The value of the transport_filter option is the command string for starting the
+filter, which is run directly from Exim, not under a shell. The string is
+parsed by Exim in the same way as a command string for the pipe transport: Exim
+breaks it up into arguments and then expands each argument separately (see
+section 29.3). Any kind of expansion failure causes delivery to be deferred.
+The special argument $pipe_addresses is replaced by a number of arguments, one
+for each address that applies to this delivery. (This isn't an ideal name for
+this feature here, but as it was already implemented for the pipe transport, it
+seemed sensible not to change it.)
+
+The expansion variables $host and $host_address are available when the
+transport is a remote one. They contain the name and IP address of the host to
+which the message is being sent. For example:
+
+transport_filter = /some/directory/transport-filter.pl \
+  $host $host_address $sender_address $pipe_addresses
+
+Two problems arise if you want to use more complicated expansion items to
+generate transport filter commands, both of which due to the fact that the
+command is split up before expansion.
+
+  * If an expansion item contains white space, you must quote it, so that it is
+    all part of the same command item. If the entire option setting is one such
+    expansion item, you have to take care what kind of quoting you use. For
+    example:
+
+    transport_filter = '/bin/cmd${if eq{$host}{a.b.c}{1}{2}}'
+
+    This runs the command /bin/cmd1 if the host name is a.b.c, and /bin/cmd2
+    otherwise. If double quotes had been used, they would have been stripped by
+    Exim when it read the option's value. When the value is used, if the single
+    quotes were missing, the line would be split into two items, "/bin/cmd${if"
+    and "eq{$host}{a.b.c}{1}{2}", and an error would occur when Exim tried to
+    expand the first one.
+
+  * Except for the special case of $pipe_addresses that is mentioned above, an
+    expansion cannot generate multiple arguments, or a command name followed by
+    arguments. Consider this example:
+
+    transport_filter = ${lookup{$host}lsearch{/a/file}\
+                        {$value}{/bin/cat}}
+
+    The result of the lookup is interpreted as the name of the command, even if
+    it contains white space. The simplest way round this is to use a shell:
+
+    transport_filter = /bin/sh -c ${lookup{$host}lsearch{/a/file}\
+                                   {$value}{/bin/cat}}
+
+The filter process is run under the same uid and gid as the normal delivery.
+For remote deliveries this is the Exim uid/gid by default. The command should
+normally yield a zero return code. Transport filters are not supposed to fail.
+A non-zero code is taken to mean that the transport filter encountered some
+serious problem. Delivery of the message is deferred; the message remains on
+the queue and is tried again later. It is not possible to cause a message to be
+bounced from a transport filter.
+
+If a transport filter is set on an autoreply transport, the original message is
+passed through the filter as it is being copied into the newly generated
+message, which happens if the return_message option is set.
+
++------------------------+---------------+----------+-----------+
+|transport_filter_timeout|Use: transports|Type: time|Default: 5m|
++------------------------+---------------+----------+-----------+
+
+When Exim is reading the output of a transport filter, it applies a timeout
+that can be set by this option. Exceeding the timeout is normally treated as a
+temporary delivery failure. However, if a transport filter is used with a pipe
+transport, a timeout in the transport filter is treated in the same way as a
+timeout in the pipe command itself. By default, a timeout is a hard error, but
+if the pipe transport's timeout_defer option is set true, it becomes a
+temporary error.
+
++----+---------------+-------------+------------------+
+|user|Use: transports|Type: string*|Default: Exim user|
++----+---------------+-------------+------------------+
+
+This option specifies the user under whose uid the delivery process is to be
+run, overriding any uid that may have been set by the router. If the user is
+given as a name, the uid is looked up from the password data, and the
+associated group is taken as the value of the gid to be used if the group
+option is not set.
+
+For deliveries that use local transports, a user and group are normally
+specified explicitly or implicitly (for example, as a result of 
+check_local_user) by the router or transport.
+
+For remote transports, you should leave this option unset unless you really are
+sure you know what you are doing. When a remote transport is running, it needs
+to be able to access Exim's hints databases, because each host may have its own
+retry data.
+
+
+
+===============================================================================
+25. ADDRESS BATCHING IN LOCAL TRANSPORTS
+
+The only remote transport (smtp) is normally configured to handle more than one
+address at a time, so that when several addresses are routed to the same remote
+host, just one copy of the message is sent. Local transports, however, normally
+handle one address at a time. That is, a separate instance of the transport is
+run for each address that is routed to the transport. A separate copy of the
+message is delivered each time.
+
+In special cases, it may be desirable to handle several addresses at once in a
+local transport, for example:
+
+  * In an appendfile transport, when storing messages in files for later
+    delivery by some other means, a single copy of the message with multiple
+    recipients saves space.
+
+  * In an lmtp transport, when delivering over "local SMTP" to some process, a
+    single copy saves time, and is the normal way LMTP is expected to work.
+
+  * In a pipe transport, when passing the message to a scanner program or to
+    some other delivery mechanism such as UUCP, multiple recipients may be
+    acceptable.
+
+These three local transports all have the same options for controlling multiple
+("batched") deliveries, namely batch_max and batch_id. To save repeating the
+information for each transport, these options are described here.
+
+The batch_max option specifies the maximum number of addresses that can be
+delivered together in a single run of the transport. Its default value is one
+(no batching). When more than one address is routed to a transport that has a 
+batch_max value greater than one, the addresses are delivered in a batch (that
+is, in a single run of the transport with multiple recipients), subject to
+certain conditions:
+
+  * If any of the transport's options contain a reference to $local_part, no
+    batching is possible.
+
+  * If any of the transport's options contain a reference to $domain, only
+    addresses with the same domain are batched.
+
+  * If batch_id is set, it is expanded for each address, and only those
+    addresses with the same expanded value are batched. This allows you to
+    specify customized batching conditions. Failure of the expansion for any
+    reason, including forced failure, disables batching, but it does not stop
+    the delivery from taking place.
+
+  * Batched addresses must also have the same errors address (where to send
+    delivery errors), the same header additions and removals, the same user and
+    group for the transport, and if a host list is present, the first host must
+    be the same.
+
+In the case of the appendfile and pipe transports, batching applies both when
+the file or pipe command is specified in the transport, and when it is
+specified by a redirect router, but all the batched addresses must of course be
+routed to the same file or pipe command. These two transports have an option
+called use_bsmtp, which causes them to deliver the message in "batched SMTP"
+format, with the envelope represented as SMTP commands. The check_string and 
+escape_string options are forced to the values
+
+check_string = "."
+escape_string = ".."
+
+when batched SMTP is in use. A full description of the batch SMTP mechanism is
+given in section 47.10. The lmtp transport does not have a use_bsmtp option,
+because it always delivers using the SMTP protocol.
+
+If the generic envelope_to_add option is set for a batching transport, the 
+Envelope-to: header that is added to the message contains all the addresses
+that are being processed together. If you are using a batching appendfile
+transport without use_bsmtp, the only way to preserve the recipient addresses
+is to set the envelope_to_add option.
+
+If you are using a pipe transport without BSMTP, and setting the transport's 
+command option, you can include $pipe_addresses as part of the command. This is
+not a true variable; it is a bit of magic that causes each of the recipient
+addresses to be inserted into the command as a separate argument. This provides
+a way of accessing all the addresses that are being delivered in the batch. 
+Note: This is not possible for pipe commands that are specified by a redirect
+router.
+
+
+
+===============================================================================
+26. THE APPENDFILE TRANSPORT
+
+The appendfile transport delivers a message by appending it to an existing
+file, or by creating an entirely new file in a specified directory. Single
+files to which messages are appended can be in the traditional Unix mailbox
+format, or optionally in the MBX format supported by the Pine MUA and
+University of Washington IMAP daemon, inter alia. When each message is being
+delivered as a separate file, "maildir" format can optionally be used to give
+added protection against failures that happen part-way through the delivery. A
+third form of separate-file delivery known as "mailstore" is also supported.
+For all file formats, Exim attempts to create as many levels of directory as
+necessary, provided that create_directory is set.
+
+The code for the optional formats is not included in the Exim binary by
+default. It is necessary to set SUPPORT_MBX, SUPPORT_MAILDIR and/or
+SUPPORT_MAILSTORE in Local/Makefile to have the appropriate code included.
+
+Exim recognizes system quota errors, and generates an appropriate message. Exim
+also supports its own quota control within the transport, for use when the
+system facility is unavailable or cannot be used for some reason.
+
+If there is an error while appending to a file (for example, quota exceeded or
+partition filled), Exim attempts to reset the file's length and last
+modification time back to what they were before. If there is an error while
+creating an entirely new file, the new file is removed.
+
+Before appending to a file, a number of security checks are made, and the file
+is locked. A detailed description is given below, after the list of private
+options.
+
+The appendfile transport is most commonly used for local deliveries to users'
+mailboxes. However, it can also be used as a pseudo-remote transport for
+putting messages into files for remote delivery by some means other than Exim.
+"Batch SMTP" format is often used in this case (see the use_bsmtp option).
+
+
+26.1 The file and directory options
+-----------------------------------
+
+The file option specifies a single file, to which the message is appended; the 
+directory option specifies a directory, in which a new file containing the
+message is created. Only one of these two options can be set, and for normal
+deliveries to mailboxes, one of them must be set.
+
+However, appendfile is also used for delivering messages to files or
+directories whose names (or parts of names) are obtained from alias,
+forwarding, or filtering operations (for example, a save command in a user's
+Exim filter). When such a transport is running, $local_part contains the local
+part that was aliased or forwarded, and $address_file contains the name (or
+partial name) of the file or directory generated by the redirection operation.
+There are two cases:
+
+  * If neither file nor directory is set, the redirection operation must
+    specify an absolute path (one that begins with "/"). This is the most
+    common case when users with local accounts use filtering to sort mail into
+    different folders. See for example, the address_file transport in the
+    default configuration. If the path ends with a slash, it is assumed to be
+    the name of a directory. A delivery to a directory can also be forced by
+    setting maildir_format or mailstore_format.
+
+  * If file or directory is set for a delivery from a redirection, it is used
+    to determine the file or directory name for the delivery. Normally, the
+    contents of $address_file are used in some way in the string expansion.
+
+As an example of the second case, consider an environment where users do not
+have home directories. They may be permitted to use Exim filter commands of the
+form:
+
+save folder23
+
+or Sieve filter commands of the form:
+
+require "fileinto";
+fileinto "folder23";
+
+In this situation, the expansion of file or directory in the transport must
+transform the relative path into an appropriate absolute file name. In the case
+of Sieve filters, the name inbox must be handled. It is the name that is used
+as a result of a "keep" action in the filter. This example shows one way of
+handling this requirement:
+
+file = ${if eq{$address_file}{inbox} \
+            {/var/mail/$local_part} \
+            {${if eq{${substr_0_1:$address_file}}{/} \
+                  {$address_file} \
+                  {$home/mail/$address_file} \
+            }} \
+       }
+
+With this setting of file, inbox refers to the standard mailbox location,
+absolute paths are used without change, and other folders are in the mail
+directory within the home directory.
+
+Note 1: While processing an Exim filter, a relative path such as folder23 is
+turned into an absolute path if a home directory is known to the router. In
+particular, this is the case if check_local_user is set. If you want to prevent
+this happening at routing time, you can set router_home_directory empty. This
+forces the router to pass the relative path to the transport.
+
+Note 2: An absolute path in $address_file is not treated specially; the file or
+directory option is still used if it is set.
+
+
+26.2 Private options for appendfile
+-----------------------------------
+
++----------+---------------+-------------+--------------+
+|allow_fifo|Use: appendfile|Type: boolean|Default: false|
++----------+---------------+-------------+--------------+
+
+Setting this option permits delivery to named pipes (FIFOs) as well as to
+regular files. If no process is reading the named pipe at delivery time, the
+delivery is deferred.
+
++-------------+---------------+-------------+--------------+
+|allow_symlink|Use: appendfile|Type: boolean|Default: false|
++-------------+---------------+-------------+--------------+
+
+By default, appendfile will not deliver if the path name for the file is that
+of a symbolic link. Setting this option relaxes that constraint, but there are
+security issues involved in the use of symbolic links. Be sure you know what
+you are doing if you set this. Details of exactly what this option affects are
+included in the discussion which follows this list of options.
+
++--------+---------------+-------------+--------------+
+|batch_id|Use: appendfile|Type: string*|Default: unset|
++--------+---------------+-------------+--------------+
+
+See the description of local delivery batching in chapter 25. However, batching
+is automatically disabled for appendfile deliveries that happen as a result of
+forwarding or aliasing or other redirection directly to a file.
+
++---------+---------------+-------------+----------+
+|batch_max|Use: appendfile|Type: integer|Default: 1|
++---------+---------------+-------------+----------+
+
+See the description of local delivery batching in chapter 25.
+
++-----------+---------------+-------------+--------------+
+|check_group|Use: appendfile|Type: boolean|Default: false|
++-----------+---------------+-------------+--------------+
+
+When this option is set, the group owner of the file defined by the file option
+is checked to see that it is the same as the group under which the delivery
+process is running. The default setting is false because the default file mode
+is 0600, which means that the group is irrelevant.
+
++-----------+---------------+-------------+-------------+
+|check_owner|Use: appendfile|Type: boolean|Default: true|
++-----------+---------------+-------------+-------------+
+
+When this option is set, the owner of the file defined by the file option is
+checked to ensure that it is the same as the user under which the delivery
+process is running.
+
++------------+---------------+------------+------------------+
+|check_string|Use: appendfile|Type: string|Default: see below|
++------------+---------------+------------+------------------+
+
+As appendfile writes the message, the start of each line is tested for matching
+check_string, and if it does, the initial matching characters are replaced by
+the contents of escape_string. The value of check_string is a literal string,
+not a regular expression, and the case of any letters it contains is
+significant.
+
+If use_bsmtp is set the values of check_string and escape_string are forced to
+"." and ".." respectively, and any settings in the configuration are ignored.
+Otherwise, they default to "From " and ">From " when the file option is set,
+and unset when any of the directory, maildir, or mailstore options are set.
+
+The default settings, along with message_prefix and message_suffix, are
+suitable for traditional "BSD" mailboxes, where a line beginning with "From "
+indicates the start of a new message. All four options need changing if another
+format is used. For example, to deliver to mailboxes in MMDF format:
+
+check_string = "\1\1\1\1\n"
+escape_string = "\1\1\1\1 \n"
+message_prefix = "\1\1\1\1\n"
+message_suffix = "\1\1\1\1\n"
+
++----------------+---------------+-------------+-------------+
+|create_directory|Use: appendfile|Type: boolean|Default: true|
++----------------+---------------+-------------+-------------+
+
+When this option is true, Exim attempts to create any missing superior
+directories for the file that it is about to write. A created directory's mode
+is given by the directory_mode option.
+
+The group ownership of a newly created directory is highly dependent on the
+operating system (and possibly the file system) that is being used. For
+example, in Solaris, if the parent directory has the setgid bit set, its group
+is propagated to the child; if not, the currently set group is used. However,
+in FreeBSD, the parent's group is always used.
+
++-----------+---------------+------------+-----------------+
+|create_file|Use: appendfile|Type: string|Default: anywhere|
++-----------+---------------+------------+-----------------+
+
+This option constrains the location of files and directories that are created
+by this transport. It applies to files defined by the file option and
+directories defined by the directory option. In the case of maildir delivery,
+it applies to the top level directory, not the maildir directories beneath.
+
+The option must be set to one of the words "anywhere", "inhome", or
+"belowhome". In the second and third cases, a home directory must have been set
+for the transport. This option is not useful when an explicit file name is
+given for normal mailbox deliveries. It is intended for the case when file
+names are generated from users' .forward files. These are usually handled by an
+appendfile transport called address_file. See also file_must_exist.
+
++---------+---------------+-------------+--------------+
+|directory|Use: appendfile|Type: string*|Default: unset|
++---------+---------------+-------------+--------------+
+
+This option is mutually exclusive with the file option, but one of file or 
+directory must be set, unless the delivery is the direct result of a
+redirection (see section 26.1).
+
+When directory is set, the string is expanded, and the message is delivered
+into a new file or files in or below the given directory, instead of being
+appended to a single mailbox file. A number of different formats are provided
+(see maildir_format and mailstore_format), and see section 26.4 for further
+details of this form of delivery.
+
++--------------+---------------+-------------+------------------+
+|directory_file|Use: appendfile|Type: string*|Default: see below|
++--------------+---------------+-------------+------------------+
+
+When directory is set, but neither maildir_format nor mailstore_format is set, 
+appendfile delivers each message into a file whose name is obtained by
+expanding this string. The default value is:
+
+q${base62:$tod_epoch}-$inode
+
+This generates a unique name from the current time, in base 62 form, and the
+inode of the file. The variable $inode is available only when expanding this
+option.
+
++--------------+---------------+-------------------+-------------+
+|directory_mode|Use: appendfile|Type: octal integer|Default: 0700|
++--------------+---------------+-------------------+-------------+
+
+If appendfile creates any directories as a result of the create_directory
+option, their mode is specified by this option.
+
++-------------+---------------+------------+------------------------+
+|escape_string|Use: appendfile|Type: string|Default: see description|
++-------------+---------------+------------+------------------------+
+
+See check_string above.
+
++----+---------------+-------------+--------------+
+|file|Use: appendfile|Type: string*|Default: unset|
++----+---------------+-------------+--------------+
+
+This option is mutually exclusive with the directory option, but one of file or
+directory must be set, unless the delivery is the direct result of a
+redirection (see section 26.1). The file option specifies a single file, to
+which the message is appended. One or more of use_fcntl_lock, use_flock_lock,
+or use_lockfile must be set with file.
+
+If you are using more than one host to deliver over NFS into the same
+mailboxes, you should always use lock files.
+
+The string value is expanded for each delivery, and must yield an absolute
+path. The most common settings of this option are variations on one of these
+examples:
+
+file = /var/spool/mail/$local_part
+file = /home/$local_part/inbox
+file = $home/inbox
+
+In the first example, all deliveries are done into the same directory. If Exim
+is configured to use lock files (see use_lockfile below) it must be able to
+create a file in the directory, so the "sticky" bit must be turned on for
+deliveries to be possible, or alternatively the group option can be used to run
+the delivery under a group id which has write access to the directory.
+
++-----------+---------------+------------+--------------+
+|file_format|Use: appendfile|Type: string|Default: unset|
++-----------+---------------+------------+--------------+
+
+This option requests the transport to check the format of an existing file
+before adding to it. The check consists of matching a specific string at the
+start of the file. The value of the option consists of an even number of
+colon-separated strings. The first of each pair is the test string, and the
+second is the name of a transport. If the transport associated with a matched
+string is not the current transport, control is passed over to the other
+transport. For example, suppose the standard local_delivery transport has this
+added to it:
+
+file_format = "From       : local_delivery :\
+               \1\1\1\1\n : local_mmdf_delivery"
+
+Mailboxes that begin with "From" are still handled by this transport, but if a
+mailbox begins with four binary ones followed by a newline, control is passed
+to a transport called local_mmdf_delivery, which presumably is configured to do
+the delivery in MMDF format. If a mailbox does not exist or is empty, it is
+assumed to match the current transport. If the start of a mailbox doesn't match
+any string, or if the transport named for a given string is not defined,
+delivery is deferred.
+
++---------------+---------------+-------------+--------------+
+|file_must_exist|Use: appendfile|Type: boolean|Default: false|
++---------------+---------------+-------------+--------------+
+
+If this option is true, the file specified by the file option must exist. A
+temporary error occurs if it does not, causing delivery to be deferred. If this
+option is false, the file is created if it does not exist.
+
++------------------+---------------+----------+-----------+
+|lock_fcntl_timeout|Use: appendfile|Type: time|Default: 0s|
++------------------+---------------+----------+-----------+
+
+By default, the appendfile transport uses non-blocking calls to fcntl() when
+locking an open mailbox file. If the call fails, the delivery process sleeps
+for lock_interval and tries again, up to lock_retries times. Non-blocking calls
+are used so that the file is not kept open during the wait for the lock; the
+reason for this is to make it as safe as possible for deliveries over NFS in
+the case when processes might be accessing an NFS mailbox without using a lock
+file. This should not be done, but misunderstandings and hence
+misconfigurations are not unknown.
+
+On a busy system, however, the performance of a non-blocking lock approach is
+not as good as using a blocking lock with a timeout. In this case, the waiting
+is done inside the system call, and Exim's delivery process acquires the lock
+and can proceed as soon as the previous lock holder releases it.
+
+If lock_fcntl_timeout is set to a non-zero time, blocking locks, with that
+timeout, are used. There may still be some retrying: the maximum number of
+retries is
+
+(lock_retries * lock_interval) / lock_fcntl_timeout
+
+rounded up to the next whole number. In other words, the total time during
+which appendfile is trying to get a lock is roughly the same, unless 
+lock_fcntl_timeout is set very large.
+
+You should consider setting this option if you are getting a lot of delayed
+local deliveries because of errors of the form
+
+failed to lock mailbox /some/file (fcntl)
+
++------------------+---------------+----------+-----------+
+|lock_flock_timeout|Use: appendfile|Type: time|Default: 0s|
++------------------+---------------+----------+-----------+
+
+This timeout applies to file locking when using flock() (see use_flock); the
+timeout operates in a similar manner to lock_fcntl_timeout.
+
++-------------+---------------+----------+-----------+
+|lock_interval|Use: appendfile|Type: time|Default: 3s|
++-------------+---------------+----------+-----------+
+
+This specifies the time to wait between attempts to lock the file. See below
+for details of locking.
+
++------------+---------------+-------------+-----------+
+|lock_retries|Use: appendfile|Type: integer|Default: 10|
++------------+---------------+-------------+-----------+
+
+This specifies the maximum number of attempts to lock the file. A value of zero
+is treated as 1. See below for details of locking.
+
++-------------+---------------+-------------------+-------------+
+|lockfile_mode|Use: appendfile|Type: octal integer|Default: 0600|
++-------------+---------------+-------------------+-------------+
+
+This specifies the mode of the created lock file, when a lock file is being
+used (see use_lockfile and use_mbx_lock).
+
++----------------+---------------+----------+------------+
+|lockfile_timeout|Use: appendfile|Type: time|Default: 30m|
++----------------+---------------+----------+------------+
+
+When a lock file is being used (see use_lockfile), if a lock file already
+exists and is older than this value, it is assumed to have been left behind by
+accident, and Exim attempts to remove it.
+
++-----------------+---------------+-------------+--------------+
+|mailbox_filecount|Use: appendfile|Type: string*|Default: unset|
++-----------------+---------------+-------------+--------------+
+
+If this option is set, it is expanded, and the result is taken as the current
+number of files in the mailbox. It must be a decimal number, optionally
+followed by K or M. This provides a way of obtaining this information from an
+external source that maintains the data.
+
++------------+---------------+-------------+--------------+
+|mailbox_size|Use: appendfile|Type: string*|Default: unset|
++------------+---------------+-------------+--------------+
+
+If this option is set, it is expanded, and the result is taken as the current
+size the mailbox. It must be a decimal number, optionally followed by K or M.
+This provides a way of obtaining this information from an external source that
+maintains the data. This is likely to be helpful for maildir deliveries where
+it is computationally expensive to compute the size of a mailbox.
+
++--------------+---------------+-------------+--------------+
+|maildir_format|Use: appendfile|Type: boolean|Default: false|
++--------------+---------------+-------------+--------------+
+
+If this option is set with the directory option, the delivery is into a new
+file, in the "maildir" format that is used by other mail software. When the
+transport is activated directly from a redirect router (for example, the 
+address_file transport in the default configuration), setting maildir_format
+causes the path received from the router to be treated as a directory, whether
+or not it ends with "/". This option is available only if SUPPORT_MAILDIR is
+present in Local/Makefile. See section 26.5 below for further details.
+
++-----------------------------+---------------+------------+------------------+
+|maildir_quota_directory_regex|Use: appendfile|Type: string|Default: See below|
++-----------------------------+---------------+------------+------------------+
+
+This option is relevant only when maildir_use_size_file is set. It defines a
+regular expression for specifying directories, relative to the quota directory
+(see quota_directory), that should be included in the quota calculation. The
+default value is:
+
+maildir_quota_directory_regex = ^(?:cur|new|\..*)$
+
+This includes the cur and new directories, and any maildir++ folders
+(directories whose names begin with a dot). If you want to exclude the Trash
+folder from the count (as some sites do), you need to change this setting to
+
+maildir_quota_directory_regex = ^(?:cur|new|\.(?!Trash).*)$
+
+This uses a negative lookahead in the regular expression to exclude the
+directory whose name is .Trash. When a directory is excluded from quota
+calculations, quota processing is bypassed for any messages that are delivered
+directly into that directory.
+
++---------------+---------------+-------------+-----------+
+|maildir_retries|Use: appendfile|Type: integer|Default: 10|
++---------------+---------------+-------------+-----------+
+
+This option specifies the number of times to retry when writing a file in
+"maildir" format. See section 26.5 below.
+
++-----------+---------------+-------------+--------------+
+|maildir_tag|Use: appendfile|Type: string*|Default: unset|
++-----------+---------------+-------------+--------------+
+
+This option applies only to deliveries in maildir format, and is described in
+section 26.5 below.
+
++---------------------+----------------+-------------+--------------+
+|maildir_use_size_file|Use: appendfile*|Type: boolean|Default: false|
++---------------------+----------------+-------------+--------------+
+
+The result of string expansion for this option must be a valid boolean value.
+If it is true, it enables support for maildirsize files. Exim creates a
+maildirsize file in a maildir if one does not exist, taking the quota from the 
+quota option of the transport. If quota is unset, the value is zero. See 
+maildir_quota_directory_regex above and section 26.5 below for further details.
+
++--------------------------+---------------+------------+--------------+
+|maildirfolder_create_regex|Use: appendfile|Type: string|Default: unset|
++--------------------------+---------------+------------+--------------+
+
+The value of this option is a regular expression. If it is unset, it has no
+effect. Otherwise, before a maildir delivery takes place, the pattern is
+matched against the name of the maildir directory, that is, the directory
+containing the new and tmp subdirectories that will be used for the delivery.
+If there is a match, Exim checks for the existence of a file called
+maildirfolder in the directory, and creates it if it does not exist. See
+section 26.5 for more details.
+
++----------------+---------------+-------------+--------------+
+|mailstore_format|Use: appendfile|Type: boolean|Default: false|
++----------------+---------------+-------------+--------------+
+
+If this option is set with the directory option, the delivery is into two new
+files in "mailstore" format. The option is available only if SUPPORT_MAILSTORE
+is present in Local/Makefile. See section 26.4 below for further details.
+
++----------------+---------------+-------------+--------------+
+|mailstore_prefix|Use: appendfile|Type: string*|Default: unset|
++----------------+---------------+-------------+--------------+
+
+This option applies only to deliveries in mailstore format, and is described in
+section 26.4 below.
+
++----------------+---------------+-------------+--------------+
+|mailstore_suffix|Use: appendfile|Type: string*|Default: unset|
++----------------+---------------+-------------+--------------+
+
+This option applies only to deliveries in mailstore format, and is described in
+section 26.4 below.
+
++----------+---------------+-------------+--------------+
+|mbx_format|Use: appendfile|Type: boolean|Default: false|
++----------+---------------+-------------+--------------+
+
+This option is available only if Exim has been compiled with SUPPORT_MBX set in
+Local/Makefile. If mbx_format is set with the file option, the message is
+appended to the mailbox file in MBX format instead of traditional Unix format.
+This format is supported by Pine4 and its associated IMAP and POP daemons, by
+means of the c-client library that they all use.
+
+Note: The message_prefix and message_suffix options are not automatically
+changed by the use of mbx_format. They should normally be set empty when using
+MBX format, so this option almost always appears in this combination:
+
+mbx_format = true
+message_prefix =
+message_suffix =
+
+If none of the locking options are mentioned in the configuration, use_mbx_lock
+is assumed and the other locking options default to false. It is possible to
+specify the other kinds of locking with mbx_format, but use_fcntl_lock and 
+use_mbx_lock are mutually exclusive. MBX locking interworks with c-client,
+providing for shared access to the mailbox. It should not be used if any
+program that does not use this form of locking is going to access the mailbox,
+nor should it be used if the mailbox file is NFS mounted, because it works only
+when the mailbox is accessed from a single host.
+
+If you set use_fcntl_lock with an MBX-format mailbox, you cannot use the
+standard version of c-client, because as long as it has a mailbox open (this
+means for the whole of a Pine or IMAP session), Exim will not be able to append
+messages to it.
+
++--------------+---------------+-------------+------------------+
+|message_prefix|Use: appendfile|Type: string*|Default: see below|
++--------------+---------------+-------------+------------------+
+
+The string specified here is expanded and output at the start of every message.
+The default is unset unless file is specified and use_bsmtp is not set, in
+which case it is:
+
+message_prefix = "From ${if def:return_path{$return_path}\
+  {MAILER-DAEMON}} $tod_bsdinbox\n"
+
+Note: If you set use_crlf true, you must change any occurrences of "\n" to "\r\
+n" in message_prefix.
+
++--------------+---------------+-------------+------------------+
+|message_suffix|Use: appendfile|Type: string*|Default: see below|
++--------------+---------------+-------------+------------------+
+
+The string specified here is expanded and output at the end of every message.
+The default is unset unless file is specified and use_bsmtp is not set, in
+which case it is a single newline character. The suffix can be suppressed by
+setting
+
+message_suffix =
+
+Note: If you set use_crlf true, you must change any occurrences of "\n" to "\r\
+n" in message_suffix.
+
++----+---------------+-------------------+-------------+
+|mode|Use: appendfile|Type: octal integer|Default: 0600|
++----+---------------+-------------------+-------------+
+
+If the output file is created, it is given this mode. If it already exists and
+has wider permissions, they are reduced to this mode. If it has narrower
+permissions, an error occurs unless mode_fail_narrower is false. However, if
+the delivery is the result of a save command in a filter file specifying a
+particular mode, the mode of the output file is always forced to take that
+value, and this option is ignored.
+
++------------------+---------------+-------------+-------------+
+|mode_fail_narrower|Use: appendfile|Type: boolean|Default: true|
++------------------+---------------+-------------+-------------+
+
+This option applies in the case when an existing mailbox file has a narrower
+mode than that specified by the mode option. If mode_fail_narrower is true, the
+delivery is deferred ("mailbox has the wrong mode"); otherwise Exim continues
+with the delivery attempt, using the existing mode of the file.
+
++-------------+---------------+-------------+--------------+
+|notify_comsat|Use: appendfile|Type: boolean|Default: false|
++-------------+---------------+-------------+--------------+
+
+If this option is true, the comsat daemon is notified after every successful
+delivery to a user mailbox. This is the daemon that notifies logged on users
+about incoming mail.
+
++-----+---------------+-------------+--------------+
+|quota|Use: appendfile|Type: string*|Default: unset|
++-----+---------------+-------------+--------------+
+
+This option imposes a limit on the size of the file to which Exim is appending,
+or to the total space used in the directory tree when the directory option is
+set. In the latter case, computation of the space used is expensive, because
+all the files in the directory (and any sub-directories) have to be
+individually inspected and their sizes summed. (See quota_size_regex and 
+maildir_use_size_file for ways to avoid this in environments where users have
+no shell access to their mailboxes).
+
+As there is no interlock against two simultaneous deliveries into a multi-file
+mailbox, it is possible for the quota to be overrun in this case. For
+single-file mailboxes, of course, an interlock is a necessity.
+
+A file's size is taken as its used value. Because of blocking effects, this may
+be a lot less than the actual amount of disk space allocated to the file. If
+the sizes of a number of files are being added up, the rounding effect can
+become quite noticeable, especially on systems that have large block sizes.
+Nevertheless, it seems best to stick to the used figure, because this is the
+obvious value which users understand most easily.
+
+The value of the option is expanded, and must then be a numerical value
+(decimal point allowed), optionally followed by one of the letters K, M, or G,
+for kilobytes, megabytes, or gigabytes. If Exim is running on a system with
+large file support (Linux and FreeBSD have this), mailboxes larger than 2G can
+be handled.
+
+Note: A value of zero is interpreted as "no quota".
+
+The expansion happens while Exim is running as root, before it changes uid for
+the delivery. This means that files that are inaccessible to the end user can
+be used to hold quota values that are looked up in the expansion. When delivery
+fails because this quota is exceeded, the handling of the error is as for
+system quota failures.
+
+By default, Exim's quota checking mimics system quotas, and restricts the
+mailbox to the specified maximum size, though the value is not accurate to the
+last byte, owing to separator lines and additional headers that may get added
+during message delivery. When a mailbox is nearly full, large messages may get
+refused even though small ones are accepted, because the size of the current
+message is added to the quota when the check is made. This behaviour can be
+changed by setting quota_is_inclusive false. When this is done, the check for
+exceeding the quota does not include the current message. Thus, deliveries
+continue until the quota has been exceeded; thereafter, no further messages are
+delivered. See also quota_warn_threshold.
+
++---------------+---------------+-------------+--------------+
+|quota_directory|Use: appendfile|Type: string*|Default: unset|
++---------------+---------------+-------------+--------------+
+
+This option defines the directory to check for quota purposes when delivering
+into individual files. The default is the delivery directory, or, if a file
+called maildirfolder exists in a maildir directory, the parent of the delivery
+directory.
+
++---------------+---------------+-------------+----------+
+|quota_filecount|Use: appendfile|Type: string*|Default: 0|
++---------------+---------------+-------------+----------+
+
+This option applies when the directory option is set. It limits the total
+number of files in the directory (compare the inode limit in system quotas). It
+can only be used if quota is also set. The value is expanded; an expansion
+failure causes delivery to be deferred. A value of zero is interpreted as "no
+quota".
+
++------------------+---------------+-------------+-------------+
+|quota_is_inclusive|Use: appendfile|Type: boolean|Default: true|
++------------------+---------------+-------------+-------------+
+
+See quota above.
+
++----------------+---------------+------------+--------------+
+|quota_size_regex|Use: appendfile|Type: string|Default: unset|
++----------------+---------------+------------+--------------+
+
+This option applies when one of the delivery modes that writes a separate file
+for each message is being used. When Exim wants to find the size of one of
+these files in order to test the quota, it first checks quota_size_regex. If
+this is set to a regular expression that matches the file name, and it captures
+one string, that string is interpreted as a representation of the file's size.
+The value of quota_size_regex is not expanded.
+
+This feature is useful only when users have no shell access to their mailboxes
+- otherwise they could defeat the quota simply by renaming the files. This
+facility can be used with maildir deliveries, by setting maildir_tag to add the
+file length to the file name. For example:
+
+maildir_tag = ,S=$message_size
+quota_size_regex = ,S=(\d+)
+
+An alternative to $message_size is $message_linecount, which contains the
+number of lines in the message.
+
+The regular expression should not assume that the length is at the end of the
+file name (even though maildir_tag puts it there) because maildir MUAs
+sometimes add other information onto the ends of message file names.
+
+Section 26.7 contains further information.
+
++------------------+---------------+-------------+------------------+
+|quota_warn_message|Use: appendfile|Type: string*|Default: see below|
++------------------+---------------+-------------+------------------+
+
+See below for the use of this option. If it is not set when 
+quota_warn_threshold is set, it defaults to
+
+quota_warn_message = "\
+  To: $local_part@$domain\n\
+  Subject: Your mailbox\n\n\
+  This message is automatically created \
+  by mail delivery software.\n\n\
+  The size of your mailbox has exceeded \
+  a warning threshold that is\n\
+  set by the system administrator.\n"
+
++--------------------+---------------+-------------+----------+
+|quota_warn_threshold|Use: appendfile|Type: string*|Default: 0|
++--------------------+---------------+-------------+----------+
+
+This option is expanded in the same way as quota (see above). If the resulting
+value is greater than zero, and delivery of the message causes the size of the
+file or total space in the directory tree to cross the given threshold, a
+warning message is sent. If quota is also set, the threshold may be specified
+as a percentage of it by following the value with a percent sign. For example:
+
+quota = 10M
+quota_warn_threshold = 75%
+
+If quota is not set, a setting of quota_warn_threshold that ends with a percent
+sign is ignored.
+
+The warning message itself is specified by the quota_warn_message option, and
+it must start with a To: header line containing the recipient(s) of the warning
+message. These do not necessarily have to include the recipient(s) of the
+original message. A Subject: line should also normally be supplied. You can
+include any other header lines that you want. If you do not include a From:
+line, the default is:
+
+From: Mail Delivery System <mailer-daemon@$qualify_domain_sender>
+
+If you supply a Reply-To: line, it overrides the global errors_reply_to option.
+
+The quota option does not have to be set in order to use this option; they are
+independent of one another except when the threshold is specified as a
+percentage.
+
++---------+---------------+-------------+--------------+
+|use_bsmtp|Use: appendfile|Type: boolean|Default: false|
++---------+---------------+-------------+--------------+
+
+If this option is set true, appendfile writes messages in "batch SMTP" format,
+with the envelope sender and recipient(s) included as SMTP commands. If you
+want to include a leading HELO command with such messages, you can do so by
+setting the message_prefix option. See section 47.10 for details of batch SMTP.
+
++--------+---------------+-------------+--------------+
+|use_crlf|Use: appendfile|Type: boolean|Default: false|
++--------+---------------+-------------+--------------+
+
+This option causes lines to be terminated with the two-character CRLF sequence
+(carriage return, linefeed) instead of just a linefeed character. In the case
+of batched SMTP, the byte sequence written to the file is then an exact image
+of what would be sent down a real SMTP connection.
+
+Note: The contents of the message_prefix and message_suffix options (which are
+used to supply the traditional "From " and blank line separators in
+Berkeley-style mailboxes) are written verbatim, so must contain their own
+carriage return characters if these are needed. In cases where these options
+have non-empty defaults, the values end with a single linefeed, so they must be
+changed to end with "\r\n" if use_crlf is set.
+
++--------------+---------------+-------------+------------------+
+|use_fcntl_lock|Use: appendfile|Type: boolean|Default: see below|
++--------------+---------------+-------------+------------------+
+
+This option controls the use of the fcntl() function to lock a file for
+exclusive use when a message is being appended. It is set by default unless 
+use_flock_lock is set. Otherwise, it should be turned off only if you know that
+all your MUAs use lock file locking. When both use_fcntl_lock and 
+use_flock_lock are unset, use_lockfile must be set.
+
++--------------+---------------+-------------+--------------+
+|use_flock_lock|Use: appendfile|Type: boolean|Default: false|
++--------------+---------------+-------------+--------------+
+
+This option is provided to support the use of flock() for file locking, for the
+few situations where it is needed. Most modern operating systems support fcntl
+() and lockf() locking, and these two functions interwork with each other. Exim
+uses fcntl() locking by default.
+
+This option is required only if you are using an operating system where flock()
+is used by programs that access mailboxes (typically MUAs), and where flock()
+does not correctly interwork with fcntl(). You can use both fcntl() and flock()
+locking simultaneously if you want.
+
+Not all operating systems provide flock(). Some versions of Solaris do not have
+it (and some, I think, provide a not quite right version built on top of lockf
+()). If the OS does not have flock(), Exim will be built without the ability to
+use it, and any attempt to do so will cause a configuration error.
+
+Warning: flock() locks do not work on NFS files (unless flock() is just being
+mapped onto fcntl() by the OS).
+
++------------+---------------+-------------+------------------+
+|use_lockfile|Use: appendfile|Type: boolean|Default: see below|
++------------+---------------+-------------+------------------+
+
+If this option is turned off, Exim does not attempt to create a lock file when
+appending to a mailbox file. In this situation, the only locking is by fcntl().
+You should only turn use_lockfile off if you are absolutely sure that every MUA
+that is ever going to look at your users' mailboxes uses fcntl() rather than a
+lock file, and even then only when you are not delivering over NFS from more
+than one host.
+
+In order to append to an NFS file safely from more than one host, it is
+necessary to take out a lock before opening the file, and the lock file
+achieves this. Otherwise, even with fcntl() locking, there is a risk of file
+corruption.
+
+The use_lockfile option is set by default unless use_mbx_lock is set. It is not
+possible to turn both use_lockfile and use_fcntl_lock off, except when 
+mbx_format is set.
+
++------------+---------------+-------------+------------------+
+|use_mbx_lock|Use: appendfile|Type: boolean|Default: see below|
++------------+---------------+-------------+------------------+
+
+This option is available only if Exim has been compiled with SUPPORT_MBX set in
+Local/Makefile. Setting the option specifies that special MBX locking rules be
+used. It is set by default if mbx_format is set and none of the locking options
+are mentioned in the configuration. The locking rules are the same as are used
+by the c-client library that underlies Pine and the IMAP4 and POP daemons that
+come with it (see the discussion below). The rules allow for shared access to
+the mailbox. However, this kind of locking does not work when the mailbox is
+NFS mounted.
+
+You can set use_mbx_lock with either (or both) of use_fcntl_lock and 
+use_flock_lock to control what kind of locking is used in implementing the MBX
+locking rules. The default is to use fcntl() if use_mbx_lock is set without 
+use_fcntl_lock or use_flock_lock.
+
+
+26.3 Operational details for appending
+--------------------------------------
+
+Before appending to a file, the following preparations are made:
+
+  * If the name of the file is /dev/null, no action is taken, and a success
+    return is given.
+
+  * If any directories on the file's path are missing, Exim creates them if the
+    create_directory option is set. A created directory's mode is given by the 
+    directory_mode option.
+
+  * If file_format is set, the format of an existing file is checked. If this
+    indicates that a different transport should be used, control is passed to
+    that transport.
+
+  * If use_lockfile is set, a lock file is built in a way that will work
+    reliably over NFS, as follows:
+
+     1. Create a "hitching post" file whose name is that of the lock file with
+        the current time, primary host name, and process id added, by opening
+        for writing as a new file. If this fails with an access error, delivery
+        is deferred.
+
+     2. Close the hitching post file, and hard link it to the lock file name.
+
+     3. If the call to link() succeeds, creation of the lock file has
+        succeeded. Unlink the hitching post name.
+
+     4. Otherwise, use stat() to get information about the hitching post file,
+        and then unlink hitching post name. If the number of links is exactly
+        two, creation of the lock file succeeded but something (for example, an
+        NFS server crash and restart) caused this fact not to be communicated
+        to the link() call.
+
+     5. If creation of the lock file failed, wait for lock_interval and try
+        again, up to lock_retries times. However, since any program that writes
+        to a mailbox should complete its task very quickly, it is reasonable to
+        time out old lock files that are normally the result of user agent and
+        system crashes. If an existing lock file is older than lockfile_timeout
+        Exim attempts to unlink it before trying again.
+
+  * A call is made to lstat() to discover whether the main file exists, and if
+    so, what its characteristics are. If lstat() fails for any reason other
+    than non-existence, delivery is deferred.
+
+  * If the file does exist and is a symbolic link, delivery is deferred, unless
+    the allow_symlink option is set, in which case the ownership of the link is
+    checked, and then stat() is called to find out about the real file, which
+    is then subjected to the checks below. The check on the top-level link
+    ownership prevents one user creating a link for another's mailbox in a
+    sticky directory, though allowing symbolic links in this case is definitely
+    not a good idea. If there is a chain of symbolic links, the intermediate
+    ones are not checked.
+
+  * If the file already exists but is not a regular file, or if the file's
+    owner and group (if the group is being checked - see check_group above) are
+    different from the user and group under which the delivery is running,
+    delivery is deferred.
+
+  * If the file's permissions are more generous than specified, they are
+    reduced. If they are insufficient, delivery is deferred, unless 
+    mode_fail_narrower is set false, in which case the delivery is tried using
+    the existing permissions.
+
+  * The file's inode number is saved, and the file is then opened for
+    appending. If this fails because the file has vanished, appendfile behaves
+    as if it hadn't existed (see below). For any other failures, delivery is
+    deferred.
+
+  * If the file is opened successfully, check that the inode number hasn't
+    changed, that it is still a regular file, and that the owner and
+    permissions have not changed. If anything is wrong, defer delivery and
+    freeze the message.
+
+  * If the file did not exist originally, defer delivery if the file_must_exist
+    option is set. Otherwise, check that the file is being created in a
+    permitted directory if the create_file option is set (deferring on
+    failure), and then open for writing as a new file, with the O_EXCL and
+    O_CREAT options, except when dealing with a symbolic link (the 
+    allow_symlink option must be set). In this case, which can happen if the
+    link points to a non-existent file, the file is opened for writing using
+    O_CREAT but not O_EXCL, because that prevents link following.
+
+  * If opening fails because the file exists, obey the tests given above for
+    existing files. However, to avoid looping in a situation where the file is
+    being continuously created and destroyed, the exists/not-exists loop is
+    broken after 10 repetitions, and the message is then frozen.
+
+  * If opening fails with any other error, defer delivery.
+
+  * Once the file is open, unless both use_fcntl_lock and use_flock_lock are
+    false, it is locked using fcntl() or flock() or both. If use_mbx_lock is
+    false, an exclusive lock is requested in each case. However, if 
+    use_mbx_lock is true, Exim takes out a shared lock on the open file, and an
+    exclusive lock on the file whose name is
+
+    /tmp/.<device-number>.<inode-number>
+
+    using the device and inode numbers of the open mailbox file, in accordance
+    with the MBX locking rules. This file is created with a mode that is
+    specified by the lockfile_mode option.
+
+    If Exim fails to lock the file, there are two possible courses of action,
+    depending on the value of the locking timeout. This is obtained from 
+    lock_fcntl_timeout or lock_flock_timeout, as appropriate.
+
+    If the timeout value is zero, the file is closed, Exim waits for 
+    lock_interval, and then goes back and re-opens the file as above and tries
+    to lock it again. This happens up to lock_retries times, after which the
+    delivery is deferred.
+
+    If the timeout has a value greater than zero, blocking calls to fcntl() or
+    flock() are used (with the given timeout), so there has already been some
+    waiting involved by the time locking fails. Nevertheless, Exim does not
+    give up immediately. It retries up to
+
+    (lock_retries * lock_interval) / <timeout>
+
+    times (rounded up).
+
+At the end of delivery, Exim closes the file (which releases the fcntl() and/or
+flock() locks) and then deletes the lock file if one was created.
+
+
+26.4 Operational details for delivery to a new file
+---------------------------------------------------
+
+When the directory option is set instead of file, each message is delivered
+into a newly-created file or set of files. When appendfile is activated
+directly from a redirect router, neither file nor directory is normally set,
+because the path for delivery is supplied by the router. (See for example, the 
+address_file transport in the default configuration.) In this case, delivery is
+to a new file if either the path name ends in "/", or the maildir_format or 
+mailstore_format option is set.
+
+No locking is required while writing the message to a new file, so the various
+locking options of the transport are ignored. The "From" line that by default
+separates messages in a single file is not normally needed, nor is the escaping
+of message lines that start with "From", and there is no need to ensure a
+newline at the end of each message. Consequently, the default values for 
+check_string, message_prefix, and message_suffix are all unset when any of 
+directory, maildir_format, or mailstore_format is set.
+
+If Exim is required to check a quota setting, it adds up the sizes of all the
+files in the delivery directory by default. However, you can specify a
+different directory by setting quota_directory. Also, for maildir deliveries
+(see below) the maildirfolder convention is honoured.
+
+There are three different ways in which delivery to individual files can be
+done, controlled by the settings of the maildir_format and mailstore_format
+options. Note that code to support maildir or mailstore formats is not included
+in the binary unless SUPPORT_MAILDIR or SUPPORT_MAILSTORE, respectively, is set
+in Local/Makefile.
+
+In all three cases an attempt is made to create the directory and any necessary
+sub-directories if they do not exist, provided that the create_directory option
+is set (the default). The location of a created directory can be constrained by
+setting create_file. A created directory's mode is given by the directory_mode
+option. If creation fails, or if the create_directory option is not set when
+creation is required, delivery is deferred.
+
+
+26.5 Maildir delivery
+---------------------
+
+If the maildir_format option is true, Exim delivers each message by writing it
+to a file whose name is tmp/<stime>.H<mtime>P<pid>.<host> in the directory that
+is defined by the directory option (the "delivery directory"). If the delivery
+is successful, the file is renamed into the new subdirectory.
+
+In the file name, <stime> is the current time of day in seconds, and <mtime> is
+the microsecond fraction of the time. After a maildir delivery, Exim checks
+that the time-of-day clock has moved on by at least one microsecond before
+terminating the delivery process. This guarantees uniqueness for the file name.
+However, as a precaution, Exim calls stat() for the file before opening it. If
+any response other than ENOENT (does not exist) is given, Exim waits 2 seconds
+and tries again, up to maildir_retries times.
+
+Before Exim carries out a maildir delivery, it ensures that subdirectories
+called new, cur, and tmp exist in the delivery directory. If they do not exist,
+Exim tries to create them and any superior directories in their path, subject
+to the create_directory and create_file options. If the 
+maildirfolder_create_regex option is set, and the regular expression it
+contains matches the delivery directory, Exim also ensures that a file called
+maildirfolder exists in the delivery directory. If a missing directory or
+maildirfolder file cannot be created, delivery is deferred.
+
+These features make it possible to use Exim to create all the necessary files
+and directories in a maildir mailbox, including subdirectories for maildir++
+folders. Consider this example:
+
+maildir_format = true
+directory = /var/mail/$local_part\
+           ${if eq{$local_part_suffix}{}{}\
+           {/.${substr_1:$local_part_suffix}}}
+maildirfolder_create_regex = /\.[^/]+$
+
+If $local_part_suffix is empty (there was no suffix for the local part),
+delivery is into a toplevel maildir with a name like /var/mail/pimbo (for the
+user called pimbo). The pattern in maildirfolder_create_regex does not match
+this name, so Exim will not look for or create the file /var/mail/pimbo/
+maildirfolder, though it will create /var/mail/pimbo/{cur,new,tmp} if
+necessary.
+
+However, if $local_part_suffix contains "-eximusers" (for example), delivery is
+into the maildir++ folder /var/mail/pimbo/.eximusers, which does match 
+maildirfolder_create_regex. In this case, Exim will create /var/mail/pimbo
+/.eximusers/maildirfolder as well as the three maildir directories /var/mail/
+pimbo/.eximusers/{cur,new,tmp}.
+
+Warning: Take care when setting maildirfolder_create_regex that it does not
+inadvertently match the toplevel maildir directory, because a maildirfolder
+file at top level would completely break quota calculations.
+
+If Exim is required to check a quota setting before a maildir delivery, and 
+quota_directory is not set, it looks for a file called maildirfolder in the
+maildir directory (alongside new, cur, tmp). If this exists, Exim assumes the
+directory is a maildir++ folder directory, which is one level down from the
+user's top level mailbox directory. This causes it to start at the parent
+directory instead of the current directory when calculating the amount of space
+used.
+
+One problem with delivering into a multi-file mailbox is that it is
+computationally expensive to compute the size of the mailbox for quota
+checking. Various approaches have been taken to reduce the amount of work
+needed. The next two sections describe two of them. A third alternative is to
+use some external process for maintaining the size data, and use the expansion
+of the mailbox_size option as a way of importing it into Exim.
+
+
+26.6 Using tags to record message sizes
+---------------------------------------
+
+If maildir_tag is set, the string is expanded for each delivery. When the
+maildir file is renamed into the new sub-directory, the tag is added to its
+name. However, if adding the tag takes the length of the name to the point
+where the test stat() call fails with ENAMETOOLONG, the tag is dropped and the
+maildir file is created with no tag.
+
+Tags can be used to encode the size of files in their names; see 
+quota_size_regex above for an example. The expansion of maildir_tag happens
+after the message has been written. The value of the $message_size variable is
+set to the number of bytes actually written. If the expansion is forced to
+fail, the tag is ignored, but a non-forced failure causes delivery to be
+deferred. The expanded tag may contain any printing characters except "/".
+Non-printing characters in the string are ignored; if the resulting string is
+empty, it is ignored. If it starts with an alphanumeric character, a leading
+colon is inserted; this default has not proven to be the path that popular
+maildir implementations have chosen (but changing it in Exim would break
+backwards compatibility).
+
+For one common implementation, you might set:
+
+maildir_tag = ,S=${message_size}
+
+but you should check the documentation of the other software to be sure.
+
+It is advisable to also set quota_size_regex when setting maildir_tag as this
+allows Exim to extract the size from your tag, instead of having to stat() each
+message file.
+
+
+26.7 Using a maildirsize file
+-----------------------------
+
+If maildir_use_size_file is true, Exim implements the maildir++ rules for
+storing quota and message size information in a file called maildirsize within
+the toplevel maildir directory. If this file does not exist, Exim creates it,
+setting the quota from the quota option of the transport. If the maildir
+directory itself does not exist, it is created before any attempt to write a
+maildirsize file.
+
+The maildirsize file is used to hold information about the sizes of messages in
+the maildir, thus speeding up quota calculations. The quota value in the file
+is just a cache; if the quota is changed in the transport, the new value
+overrides the cached value when the next message is delivered. The cache is
+maintained for the benefit of other programs that access the maildir and need
+to know the quota.
+
+If the quota option in the transport is unset or zero, the maildirsize file is
+maintained (with a zero quota setting), but no quota is imposed.
+
+A regular expression is available for controlling which directories in the
+maildir participate in quota calculations when a maildirsizefile is in use. See
+the description of the maildir_quota_directory_regex option above for details.
+
+
+26.8 Mailstore delivery
+-----------------------
+
+If the mailstore_format option is true, each message is written as two files in
+the given directory. A unique base name is constructed from the message id and
+the current delivery process, and the files that are written use this base name
+plus the suffixes .env and .msg. The .env file contains the message's envelope,
+and the .msg file contains the message itself. The base name is placed in the
+variable $mailstore_basename.
+
+During delivery, the envelope is first written to a file with the suffix .tmp.
+The .msg file is then written, and when it is complete, the .tmp file is
+renamed as the .env file. Programs that access messages in mailstore format
+should wait for the presence of both a .msg and a .env file before accessing
+either of them. An alternative approach is to wait for the absence of a .tmp
+file.
+
+The envelope file starts with any text defined by the mailstore_prefix option,
+expanded and terminated by a newline if there isn't one. Then follows the
+sender address on one line, then all the recipient addresses, one per line.
+There can be more than one recipient only if the batch_max option is set
+greater than one. Finally, mailstore_suffix is expanded and the result appended
+to the file, followed by a newline if it does not end with one.
+
+If expansion of mailstore_prefix or mailstore_suffix ends with a forced
+failure, it is ignored. Other expansion errors are treated as serious
+configuration errors, and delivery is deferred. The variable
+$mailstore_basename is available for use during these expansions.
+
+
+26.9 Non-special new file delivery
+----------------------------------
+
+If neither maildir_format nor mailstore_format is set, a single new file is
+created directly in the named directory. For example, when delivering messages
+into files in batched SMTP format for later delivery to some host (see section
+47.10), a setting such as
+
+directory = /var/bsmtp/$host
+
+might be used. A message is written to a file with a temporary name, which is
+then renamed when the delivery is complete. The final name is obtained by
+expanding the contents of the directory_file option.
+
+
+
+===============================================================================
+27. THE AUTOREPLY TRANSPORT
+
+The autoreply transport is not a true transport in that it does not cause the
+message to be transmitted. Instead, it generates a new mail message as an
+automatic reply to the incoming message. References: and Auto-Submitted: header
+lines are included. These are constructed according to the rules in RFCs 2822
+and 3834, respectively.
+
+If the router that passes the message to this transport does not have the 
+unseen option set, the original message (for the current recipient) is not
+delivered anywhere. However, when the unseen option is set on the router that
+passes the message to this transport, routing of the address continues, so
+another router can set up a normal message delivery.
+
+The autoreply transport is usually run as the result of mail filtering, a
+"vacation" message being the standard example. However, it can also be run
+directly from a router like any other transport. To reduce the possibility of
+message cascades, messages created by the autoreply transport always have empty
+envelope sender addresses, like bounce messages.
+
+The parameters of the message to be sent can be specified in the configuration
+by options described below. However, these are used only when the address
+passed to the transport does not contain its own reply information. When the
+transport is run as a consequence of a mail or vacation command in a filter
+file, the parameters of the message are supplied by the filter, and passed with
+the address. The transport's options that define the message are then ignored
+(so they are not usually set in this case). The message is specified entirely
+by the filter or by the transport; it is never built from a mixture of options.
+However, the file_optional, mode, and return_message options apply in all
+cases.
+
+Autoreply is implemented as a local transport. When used as a result of a
+command in a user's filter file, autoreply normally runs under the uid and gid
+of the user, and with appropriate current and home directories (see chapter 23
+).
+
+There is a subtle difference between routing a message to a pipe transport that
+generates some text to be returned to the sender, and routing it to an 
+autoreply transport. This difference is noticeable only if more than one
+address from the same message is so handled. In the case of a pipe, the
+separate outputs from the different addresses are gathered up and returned to
+the sender in a single message, whereas if autoreply is used, a separate
+message is generated for each address that is passed to it.
+
+Non-printing characters are not permitted in the header lines generated for the
+message that autoreply creates, with the exception of newlines that are
+immediately followed by white space. If any non-printing characters are found,
+the transport defers. Whether characters with the top bit set count as printing
+characters or not is controlled by the print_topbitchars global option.
+
+If any of the generic options for manipulating headers (for example, 
+headers_add) are set on an autoreply transport, they apply to the copy of the
+original message that is included in the generated message when return_message
+is set. They do not apply to the generated message itself.
+
+If the autoreply transport receives return code 2 from Exim when it submits the
+message, indicating that there were no recipients, it does not treat this as an
+error. This means that autoreplies sent to $sender_address when this is empty
+(because the incoming message is a bounce message) do not cause problems. They
+are just discarded.
+
+
+27.1 Private options for autoreply
+----------------------------------
+
++---+--------------+-------------+--------------+
+|bcc|Use: autoreply|Type: string*|Default: unset|
++---+--------------+-------------+--------------+
+
+This specifies the addresses that are to receive "blind carbon copies" of the
+message when the message is specified by the transport.
+
++--+--------------+-------------+--------------+
+|cc|Use: autoreply|Type: string*|Default: unset|
++--+--------------+-------------+--------------+
+
+This specifies recipients of the message and the contents of the Cc: header
+when the message is specified by the transport.
+
++----+--------------+-------------+--------------+
+|file|Use: autoreply|Type: string*|Default: unset|
++----+--------------+-------------+--------------+
+
+The contents of the file are sent as the body of the message when the message
+is specified by the transport. If both file and text are set, the text string
+comes first.
+
++-----------+--------------+-------------+--------------+
+|file_expand|Use: autoreply|Type: boolean|Default: false|
++-----------+--------------+-------------+--------------+
+
+If this is set, the contents of the file named by the file option are subjected
+to string expansion as they are added to the message.
+
++-------------+--------------+-------------+--------------+
+|file_optional|Use: autoreply|Type: boolean|Default: false|
++-------------+--------------+-------------+--------------+
+
+If this option is true, no error is generated if the file named by the file
+option or passed with the address does not exist or cannot be read.
+
++----+--------------+-------------+--------------+
+|from|Use: autoreply|Type: string*|Default: unset|
++----+--------------+-------------+--------------+
+
+This specifies the contents of the From: header when the message is specified
+by the transport.
+
++-------+--------------+-------------+--------------+
+|headers|Use: autoreply|Type: string*|Default: unset|
++-------+--------------+-------------+--------------+
+
+This specifies additional RFC 2822 headers that are to be added to the message
+when the message is specified by the transport. Several can be given by using "
+\n" to separate them. There is no check on the format.
+
++---+--------------+-------------+--------------+
+|log|Use: autoreply|Type: string*|Default: unset|
++---+--------------+-------------+--------------+
+
+This option names a file in which a record of every message sent is logged when
+the message is specified by the transport.
+
++----+--------------+-------------------+-------------+
+|mode|Use: autoreply|Type: octal integer|Default: 0600|
++----+--------------+-------------------+-------------+
+
+If either the log file or the "once" file has to be created, this mode is used.
+
++----------+--------------+-------------------+--------------+
+|never_mail|Use: autoreply|Type: address list*|Default: unset|
++----------+--------------+-------------------+--------------+
+
+If any run of the transport creates a message with a recipient that matches any
+item in the list, that recipient is quietly discarded. If all recipients are
+discarded, no message is created. This applies both when the recipients are
+generated by a filter and when they are specified in the transport.
+
++----+--------------+-------------+--------------+
+|once|Use: autoreply|Type: string*|Default: unset|
++----+--------------+-------------+--------------+
+
+This option names a file or DBM database in which a record of each To:
+recipient is kept when the message is specified by the transport. Note: This
+does not apply to Cc: or Bcc: recipients.
+
+If once is unset, or is set to an empty string, the message is always sent. By
+default, if once is set to a non-empty file name, the message is not sent if a
+potential recipient is already listed in the database. However, if the 
+once_repeat option specifies a time greater than zero, the message is sent if
+that much time has elapsed since a message was last sent to this recipient. A
+setting of zero time for once_repeat (the default) prevents a message from
+being sent a second time - in this case, zero means infinity.
+
+If once_file_size is zero, a DBM database is used to remember recipients, and
+it is allowed to grow as large as necessary. If once_file_size is set greater
+than zero, it changes the way Exim implements the once option. Instead of using
+a DBM file to record every recipient it sends to, it uses a regular file, whose
+size will never get larger than the given value.
+
+In the file, Exim keeps a linear list of recipient addresses and the times at
+which they were sent messages. If the file is full when a new address needs to
+be added, the oldest address is dropped. If once_repeat is not set, this means
+that a given recipient may receive multiple messages, but at unpredictable
+intervals that depend on the rate of turnover of addresses in the file. If 
+once_repeat is set, it specifies a maximum time between repeats.
+
++--------------+--------------+-------------+----------+
+|once_file_size|Use: autoreply|Type: integer|Default: 0|
++--------------+--------------+-------------+----------+
+
+See once above.
+
++-----------+--------------+-----------+-----------+
+|once_repeat|Use: autoreply|Type: time*|Default: 0s|
++-----------+--------------+-----------+-----------+
+
+See once above. After expansion, the value of this option must be a valid time
+value.
+
++--------+--------------+-------------+--------------+
+|reply_to|Use: autoreply|Type: string*|Default: unset|
++--------+--------------+-------------+--------------+
+
+This specifies the contents of the Reply-To: header when the message is
+specified by the transport.
+
++--------------+--------------+-------------+--------------+
+|return_message|Use: autoreply|Type: boolean|Default: false|
++--------------+--------------+-------------+--------------+
+
+If this is set, a copy of the original message is returned with the new
+message, subject to the maximum size set in the return_size_limit global
+configuration option.
+
++-------+--------------+-------------+--------------+
+|subject|Use: autoreply|Type: string*|Default: unset|
++-------+--------------+-------------+--------------+
+
+This specifies the contents of the Subject: header when the message is
+specified by the transport. It is tempting to quote the original subject in
+automatic responses. For example:
+
+subject = Re: $h_subject:
+
+There is a danger in doing this, however. It may allow a third party to
+subscribe your users to an opt-in mailing list, provided that the list accepts
+bounce messages as subscription confirmations. Well-managed lists require a
+non-bounce message to confirm a subscription, so the danger is relatively
+small.
+
++----+--------------+-------------+--------------+
+|text|Use: autoreply|Type: string*|Default: unset|
++----+--------------+-------------+--------------+
+
+This specifies a single string to be used as the body of the message when the
+message is specified by the transport. If both text and file are set, the text
+comes first.
+
++--+--------------+-------------+--------------+
+|to|Use: autoreply|Type: string*|Default: unset|
++--+--------------+-------------+--------------+
+
+This specifies recipients of the message and the contents of the To: header
+when the message is specified by the transport.
+
+
+
+===============================================================================
+28. THE LMTP TRANSPORT
+
+The lmtp transport runs the LMTP protocol (RFC 2033) over a pipe to a specified
+command or by interacting with a Unix domain socket. This transport is
+something of a cross between the pipe and smtp transports. Exim also has
+support for using LMTP over TCP/IP; this is implemented as an option for the 
+smtp transport. Because LMTP is expected to be of minority interest, the
+default build-time configure in src/EDITME has it commented out. You need to
+ensure that
+
+TRANSPORT_LMTP=yes
+
+is present in your Local/Makefile in order to have the lmtp transport included
+in the Exim binary. The private options of the lmtp transport are as follows:
+
++--------+---------+-------------+--------------+
+|batch_id|Use: lmtp|Type: string*|Default: unset|
++--------+---------+-------------+--------------+
+
+See the description of local delivery batching in chapter 25.
+
++---------+---------+-------------+----------+
+|batch_max|Use: lmtp|Type: integer|Default: 1|
++---------+---------+-------------+----------+
+
+This limits the number of addresses that can be handled in a single delivery.
+Most LMTP servers can handle several addresses at once, so it is normally a
+good idea to increase this value. See the description of local delivery
+batching in chapter 25.
+
++-------+---------+-------------+--------------+
+|command|Use: lmtp|Type: string*|Default: unset|
++-------+---------+-------------+--------------+
+
+This option must be set if socket is not set. The string is a command which is
+run in a separate process. It is split up into a command name and list of
+arguments, each of which is separately expanded (so expansion cannot change the
+number of arguments). The command is run directly, not via a shell. The message
+is passed to the new process using the standard input and output to operate the
+LMTP protocol.
+
++------------+---------+-------------+--------------+
+|ignore_quota|Use: lmtp|Type: boolean|Default: false|
++------------+---------+-------------+--------------+
+
+If this option is set true, the string "IGNOREQUOTA" is added to RCPT commands,
+provided that the LMTP server has advertised support for IGNOREQUOTA in its
+response to the LHLO command.
+
++------+---------+-------------+--------------+
+|socket|Use: lmtp|Type: string*|Default: unset|
++------+---------+-------------+--------------+
+
+This option must be set if command is not set. The result of expansion must be
+the name of a Unix domain socket. The transport connects to the socket and
+delivers the message to it using the LMTP protocol.
+
++-------+---------+----------+-----------+
+|timeout|Use: lmtp|Type: time|Default: 5m|
++-------+---------+----------+-----------+
+
+The transport is aborted if the created process or Unix domain socket does not
+respond to LMTP commands or message input within this timeout. Delivery is
+deferred, and will be tried again later. Here is an example of a typical LMTP
+transport:
+
+lmtp:
+  driver = lmtp
+  command = /some/local/lmtp/delivery/program
+  batch_max = 20
+  user = exim
+
+This delivers up to 20 addresses at a time, in a mixture of domains if
+necessary, running as the user exim.
+
+
+
+===============================================================================
+29. THE PIPE TRANSPORT
+
+The pipe transport is used to deliver messages via a pipe to a command running
+in another process. One example is the use of pipe as a pseudo-remote transport
+for passing messages to some other delivery mechanism (such as UUCP). Another
+is the use by individual users to automatically process their incoming
+messages. The pipe transport can be used in one of the following ways:
+
+  * A router routes one address to a transport in the normal way, and the
+    transport is configured as a pipe transport. In this case, $local_part
+    contains the local part of the address (as usual), and the command that is
+    run is specified by the command option on the transport.
+
+  * If the batch_max option is set greater than 1 (the default is 1), the
+    transport can handle more than one address in a single run. In this case,
+    when more than one address is routed to the transport, $local_part is not
+    set (because it is not unique). However, the pseudo-variable
+    $pipe_addresses (described in section 29.3 below) contains all the
+    addresses that are routed to the transport.
+
+  * A router redirects an address directly to a pipe command (for example, from
+    an alias or forward file). In this case, $address_pipe contains the text of
+    the pipe command, and the command option on the transport is ignored unless
+    force_command is set. If only one address is being transported (batch_max
+    is not greater than one, or only one address was redirected to this pipe
+    command), $local_part contains the local part that was redirected.
+
+The pipe transport is a non-interactive delivery method. Exim can also deliver
+messages over pipes using the LMTP interactive protocol. This is implemented by
+the lmtp transport.
+
+In the case when pipe is run as a consequence of an entry in a local user's
+.forward file, the command runs under the uid and gid of that user. In other
+cases, the uid and gid have to be specified explicitly, either on the transport
+or on the router that handles the address. Current and "home" directories are
+also controllable. See chapter 23 for details of the local delivery environment
+and chapter 25 for a discussion of local delivery batching.
+
+
+29.1 Concurrent delivery
+------------------------
+
+If two messages arrive at almost the same time, and both are routed to a pipe
+delivery, the two pipe transports may be run concurrently. You must ensure that
+any pipe commands you set up are robust against this happening. If the commands
+write to a file, the exim_lock utility might be of use.
+
+
+29.2 Returned status and data
+-----------------------------
+
+If the command exits with a non-zero return code, the delivery is deemed to
+have failed, unless either the ignore_status option is set (in which case the
+return code is treated as zero), or the return code is one of those listed in
+the temp_errors option, which are interpreted as meaning "try again later". In
+this case, delivery is deferred. Details of a permanent failure are logged, but
+are not included in the bounce message, which merely contains "local delivery
+failed".
+
+If the command exits on a signal and the freeze_signal option is set then the
+message will be frozen in the queue. If that option is not set, a bounce will
+be sent as normal.
+
+If the return code is greater than 128 and the command being run is a shell
+script, it normally means that the script was terminated by a signal whose
+value is the return code minus 128. The freeze_signal option does not apply in
+this case.
+
+If Exim is unable to run the command (that is, if execve() fails), the return
+code is set to 127. This is the value that a shell returns if it is asked to
+run a non-existent command. The wording for the log line suggests that a
+non-existent command may be the problem.
+
+The return_output option can affect the result of a pipe delivery. If it is set
+and the command produces any output on its standard output or standard error
+streams, the command is considered to have failed, even if it gave a zero
+return code or if ignore_status is set. The output from the command is included
+as part of the bounce message. The return_fail_output option is similar, except
+that output is returned only when the command exits with a failure return code,
+that is, a value other than zero or a code that matches temp_errors.
+
+
+29.3 How the command is run
+---------------------------
+
+The command line is (by default) broken down into a command name and arguments
+by the pipe transport itself. The allow_commands and restrict_to_path options
+can be used to restrict the commands that may be run.
+
+Unquoted arguments are delimited by white space. If an argument appears in
+double quotes, backslash is interpreted as an escape character in the usual
+way. If an argument appears in single quotes, no escaping is done.
+
+String expansion is applied to the command line except when it comes from a
+traditional .forward file (commands from a filter file are expanded). The
+expansion is applied to each argument in turn rather than to the whole line.
+For this reason, any string expansion item that contains white space must be
+quoted so as to be contained within a single argument. A setting such as
+
+command = /some/path ${if eq{$local_part}{postmaster}{xx}{yy}}
+
+will not work, because the expansion item gets split between several arguments.
+You have to write
+
+command = /some/path "${if eq{$local_part}{postmaster}{xx}{yy}}"
+
+to ensure that it is all in one argument. The expansion is done in this way,
+argument by argument, so that the number of arguments cannot be changed as a
+result of expansion, and quotes or backslashes in inserted variables do not
+interact with external quoting. However, this leads to problems if you want to
+generate multiple arguments (or the command name plus arguments) from a single
+expansion. In this situation, the simplest solution is to use a shell. For
+example:
+
+command = /bin/sh -c ${lookup{$local_part}lsearch{/some/file}}
+
+Special handling takes place when an argument consists of precisely the text
+"$pipe_addresses". This is not a general expansion variable; the only place
+this string is recognized is when it appears as an argument for a pipe or
+transport filter command. It causes each address that is being handled to be
+inserted in the argument list at that point as a separate argument. This avoids
+any problems with spaces or shell metacharacters, and is of use when a pipe
+transport is handling groups of addresses in a batch.
+
+If force_command is enabled on the transport, Special handling takes place for
+an argument that consists of precisely the text "$address_pipe". It is handled
+similarly to $pipe_addresses above. It is expanded and each argument is
+inserted in the argument list at that point as a separate argument. The
+"$address_pipe" item does not need to be the only item in the argument; in
+fact, if it were then force_command should behave as a no-op. Rather, it should
+be used to adjust the command run while preserving the argument vector
+separation.
+
+After splitting up into arguments and expansion, the resulting command is run
+in a subprocess directly from the transport, not under a shell. The message
+that is being delivered is supplied on the standard input, and the standard
+output and standard error are both connected to a single pipe that is read by
+Exim. The max_output option controls how much output the command may produce,
+and the return_output and return_fail_output options control what is done with
+it.
+
+Not running the command under a shell (by default) lessens the security risks
+in cases when a command from a user's filter file is built out of data that was
+taken from an incoming message. If a shell is required, it can of course be
+explicitly specified as the command to be run. However, there are circumstances
+where existing commands (for example, in .forward files) expect to be run under
+a shell and cannot easily be modified. To allow for these cases, there is an
+option called use_shell, which changes the way the pipe transport works.
+Instead of breaking up the command line as just described, it expands it as a
+single string and passes the result to /bin/sh. The restrict_to_path option and
+the $pipe_addresses facility cannot be used with use_shell, and the whole
+mechanism is inherently less secure.
+
+
+29.4 Environment variables
+--------------------------
+
+The environment variables listed below are set up when the command is invoked.
+This list is a compromise for maximum compatibility with other MTAs. Note that
+the environment option can be used to add additional variables to this
+environment.
+
+DOMAIN               the domain of the address
+HOME                 the home directory, if set
+HOST                 the host name when called from a router (see below)
+LOCAL_PART           see below
+LOCAL_PART_PREFIX    see below
+LOCAL_PART_SUFFIX    see below
+LOGNAME              see below
+MESSAGE_ID           Exim's local ID for the message
+PATH                 as specified by the path option below
+QUALIFY_DOMAIN       the sender qualification domain
+RECIPIENT            the complete recipient address
+SENDER               the sender of the message (empty if a bounce)
+SHELL                /bin/sh
+TZ                   the value of the timezone option, if set
+USER                 see below
+
+When a pipe transport is called directly from (for example) an accept router,
+LOCAL_PART is set to the local part of the address. When it is called as a
+result of a forward or alias expansion, LOCAL_PART is set to the local part of
+the address that was expanded. In both cases, any affixes are removed from the
+local part, and made available in LOCAL_PART_PREFIX and LOCAL_PART_SUFFIX,
+respectively. LOGNAME and USER are set to the same value as LOCAL_PART for
+compatibility with other MTAs.
+
+HOST is set only when a pipe transport is called from a router that associates
+hosts with an address, typically when using pipe as a pseudo-remote transport.
+HOST is set to the first host name specified by the router.
+
+If the transport's generic home_directory option is set, its value is used for
+the HOME environment variable. Otherwise, a home directory may be set by the
+router's transport_home_directory option, which defaults to the user's home
+directory if check_local_user is set.
+
+
+29.5 Private options for pipe
+-----------------------------
+
++--------------+---------+------------------+--------------+
+|allow_commands|Use: pipe|Type: string list*|Default: unset|
++--------------+---------+------------------+--------------+
+
+The string is expanded, and is then interpreted as a colon-separated list of
+permitted commands. If restrict_to_path is not set, the only commands permitted
+are those in the allow_commands list. They need not be absolute paths; the path
+option is still used for relative paths. If restrict_to_path is set with 
+allow_commands, the command must either be in the allow_commands list, or a
+name without any slashes that is found on the path. In other words, if neither 
+allow_commands nor restrict_to_path is set, there is no restriction on the
+command, but otherwise only commands that are permitted by one or the other are
+allowed. For example, if
+
+allow_commands = /usr/bin/vacation
+
+and restrict_to_path is not set, the only permitted command is /usr/bin/
+vacation. The allow_commands option may not be set if use_shell is set.
+
++--------+---------+-------------+--------------+
+|batch_id|Use: pipe|Type: string*|Default: unset|
++--------+---------+-------------+--------------+
+
+See the description of local delivery batching in chapter 25.
+
++---------+---------+-------------+----------+
+|batch_max|Use: pipe|Type: integer|Default: 1|
++---------+---------+-------------+----------+
+
+This limits the number of addresses that can be handled in a single delivery.
+See the description of local delivery batching in chapter 25.
+
++------------+---------+------------+--------------+
+|check_string|Use: pipe|Type: string|Default: unset|
++------------+---------+------------+--------------+
+
+As pipe writes the message, the start of each line is tested for matching 
+check_string, and if it does, the initial matching characters are replaced by
+the contents of escape_string, provided both are set. The value of check_string
+is a literal string, not a regular expression, and the case of any letters it
+contains is significant. When use_bsmtp is set, the contents of check_string
+and escape_string are forced to values that implement the SMTP escaping
+protocol. Any settings made in the configuration file are ignored.
+
++-------+---------+-------------+--------------+
+|command|Use: pipe|Type: string*|Default: unset|
++-------+---------+-------------+--------------+
+
+This option need not be set when pipe is being used to deliver to pipes
+obtained directly from address redirections. In other cases, the option must be
+set, to provide a command to be run. It need not yield an absolute path (see
+the path option below). The command is split up into separate arguments by
+Exim, and each argument is separately expanded, as described in section 29.3
+above.
+
++-----------+---------+-------------+--------------+
+|environment|Use: pipe|Type: string*|Default: unset|
++-----------+---------+-------------+--------------+
+
+This option is used to add additional variables to the environment in which the
+command runs (see section 29.4 for the default list). Its value is a string
+which is expanded, and then interpreted as a colon-separated list of
+environment settings of the form <name>=<value>.
+
++-------------+---------+------------+--------------+
+|escape_string|Use: pipe|Type: string|Default: unset|
++-------------+---------+------------+--------------+
+
+See check_string above.
+
++----------------+---------+-------------+--------------+
+|freeze_exec_fail|Use: pipe|Type: boolean|Default: false|
++----------------+---------+-------------+--------------+
+
+Failure to exec the command in a pipe transport is by default treated like any
+other failure while running the command. However, if freeze_exec_fail is set,
+failure to exec is treated specially, and causes the message to be frozen,
+whatever the setting of ignore_status.
+
++-------------+---------+-------------+--------------+
+|freeze_signal|Use: pipe|Type: boolean|Default: false|
++-------------+---------+-------------+--------------+
+
+Normally if the process run by a command in a pipe transport exits on a signal,
+a bounce message is sent. If freeze_signal is set, the message will be frozen
+in Exim's queue instead.
+
++-------------+---------+-------------+--------------+
+|force_command|Use: pipe|Type: boolean|Default: false|
++-------------+---------+-------------+--------------+
+
+Normally when a router redirects an address directly to a pipe command the 
+command option on the transport is ignored. If force_command is set, the 
+command option will used. This is especially useful for forcing a wrapper or
+additional argument to be added to the command. For example:
+
+command = /usr/bin/remote_exec myhost -- $address_pipe
+force_command
+
+Note that $address_pipe is handled specially in command when force_command is
+set, expanding out to the original argument vector as separate items, similarly
+to a Unix shell ""$@"" construct.
+
++-------------+---------+-------------+--------------+
+|ignore_status|Use: pipe|Type: boolean|Default: false|
++-------------+---------+-------------+--------------+
+
+If this option is true, the status returned by the subprocess that is set up to
+run the command is ignored, and Exim behaves as if zero had been returned.
+Otherwise, a non-zero status or termination by signal causes an error return
+from the transport unless the status value is one of those listed in 
+temp_errors; these cause the delivery to be deferred and tried again later.
+
+Note: This option does not apply to timeouts, which do not return a status. See
+the timeout_defer option for how timeouts are handled.
+
++----------------+---------+-------------+--------------+
+|log_defer_output|Use: pipe|Type: boolean|Default: false|
++----------------+---------+-------------+--------------+
+
+If this option is set, and the status returned by the command is one of the
+codes listed in temp_errors (that is, delivery was deferred), and any output
+was produced, the first line of it is written to the main log.
+
++---------------+---------+-------------+--------------+
+|log_fail_output|Use: pipe|Type: boolean|Default: false|
++---------------+---------+-------------+--------------+
+
+If this option is set, and the command returns any output, and also ends with a
+return code that is neither zero nor one of the return codes listed in 
+temp_errors (that is, the delivery failed), the first line of output is written
+to the main log. This option and log_output are mutually exclusive. Only one of
+them may be set.
+
++----------+---------+-------------+--------------+
+|log_output|Use: pipe|Type: boolean|Default: false|
++----------+---------+-------------+--------------+
+
+If this option is set and the command returns any output, the first line of
+output is written to the main log, whatever the return code. This option and 
+log_fail_output are mutually exclusive. Only one of them may be set.
+
++----------+---------+-------------+------------+
+|max_output|Use: pipe|Type: integer|Default: 20K|
++----------+---------+-------------+------------+
+
+This specifies the maximum amount of output that the command may produce on its
+standard output and standard error file combined. If the limit is exceeded, the
+process running the command is killed. This is intended as a safety measure to
+catch runaway processes. The limit is applied independently of the settings of
+the options that control what is done with such output (for example, 
+return_output). Because of buffering effects, the amount of output may exceed
+the limit by a small amount before Exim notices.
+
++--------------+---------+-------------+------------------+
+|message_prefix|Use: pipe|Type: string*|Default: see below|
++--------------+---------+-------------+------------------+
+
+The string specified here is expanded and output at the start of every message.
+The default is unset if use_bsmtp is set. Otherwise it is
+
+message_prefix = \
+  From ${if def:return_path{$return_path}{MAILER-DAEMON}}\
+  ${tod_bsdinbox}\n
+
+This is required by the commonly used /usr/bin/vacation program. However, it
+must not be present if delivery is to the Cyrus IMAP server, or to the tmail
+local delivery agent. The prefix can be suppressed by setting
+
+message_prefix =
+
+Note: If you set use_crlf true, you must change any occurrences of "\n" to "\r\
+n" in message_prefix.
+
++--------------+---------+-------------+------------------+
+|message_suffix|Use: pipe|Type: string*|Default: see below|
++--------------+---------+-------------+------------------+
+
+The string specified here is expanded and output at the end of every message.
+The default is unset if use_bsmtp is set. Otherwise it is a single newline. The
+suffix can be suppressed by setting
+
+message_suffix =
+
+Note: If you set use_crlf true, you must change any occurrences of "\n" to "\r\
+n" in message_suffix.
+
++----+---------+------------+------------------+
+|path|Use: pipe|Type: string|Default: see below|
++----+---------+------------+------------------+
+
+This option specifies the string that is set up in the PATH environment
+variable of the subprocess. The default is:
+
+/bin:/usr/bin
+
+If the command option does not yield an absolute path name, the command is
+sought in the PATH directories, in the usual way. Warning: This does not apply
+to a command specified as a transport filter.
+
++---------------+---------+-------------+--------------+
+|permit_coredump|Use: pipe|Type: boolean|Default: false|
++---------------+---------+-------------+--------------+
+
+Normally Exim inhibits core-dumps during delivery. If you have a need to get a
+core-dump of a pipe command, enable this command. This enables core-dumps
+during delivery and affects both the Exim binary and the pipe command run. It
+is recommended that this option remain off unless and until you have a need for
+it and that this only be enabled when needed, as the risk of excessive resource
+consumption can be quite high. Note also that Exim is typically installed as a
+setuid binary and most operating systems will inhibit coredumps of these by
+default, so further OS-specific action may be required.
+
++---------------+---------+-------------+--------------+
+|pipe_as_creator|Use: pipe|Type: boolean|Default: false|
++---------------+---------+-------------+--------------+
+
+If the generic user option is not set and this option is true, the delivery
+process is run under the uid that was in force when Exim was originally called
+to accept the message. If the group id is not otherwise set (via the generic 
+group option), the gid that was in force when Exim was originally called to
+accept the message is used.
+
++----------------+---------+-------------+--------------+
+|restrict_to_path|Use: pipe|Type: boolean|Default: false|
++----------------+---------+-------------+--------------+
+
+When this option is set, any command name not listed in allow_commands must
+contain no slashes. The command is searched for only in the directories listed
+in the path option. This option is intended for use in the case when a pipe
+command has been generated from a user's .forward file. This is usually handled
+by a pipe transport called address_pipe.
+
++------------------+---------+-------------+--------------+
+|return_fail_output|Use: pipe|Type: boolean|Default: false|
++------------------+---------+-------------+--------------+
+
+If this option is true, and the command produced any output and ended with a
+return code other than zero or one of the codes listed in temp_errors (that is,
+the delivery failed), the output is returned in the bounce message. However, if
+the message has a null sender (that is, it is itself a bounce message), output
+from the command is discarded. This option and return_output are mutually
+exclusive. Only one of them may be set.
+
++-------------+---------+-------------+--------------+
+|return_output|Use: pipe|Type: boolean|Default: false|
++-------------+---------+-------------+--------------+
+
+If this option is true, and the command produced any output, the delivery is
+deemed to have failed whatever the return code from the command, and the output
+is returned in the bounce message. Otherwise, the output is just discarded.
+However, if the message has a null sender (that is, it is a bounce message),
+output from the command is always discarded, whatever the setting of this
+option. This option and return_fail_output are mutually exclusive. Only one of
+them may be set.
+
++-----------+---------+-----------------+------------------+
+|temp_errors|Use: pipe|Type: string list|Default: see below|
++-----------+---------+-----------------+------------------+
+
+This option contains either a colon-separated list of numbers, or a single
+asterisk. If ignore_status is false and return_output is not set, and the
+command exits with a non-zero return code, the failure is treated as temporary
+and the delivery is deferred if the return code matches one of the numbers, or
+if the setting is a single asterisk. Otherwise, non-zero return codes are
+treated as permanent errors. The default setting contains the codes defined by
+EX_TEMPFAIL and EX_CANTCREAT in sysexits.h. If Exim is compiled on a system
+that does not define these macros, it assumes values of 75 and 73,
+respectively.
+
++-------+---------+----------+-----------+
+|timeout|Use: pipe|Type: time|Default: 1h|
++-------+---------+----------+-----------+
+
+If the command fails to complete within this time, it is killed. This normally
+causes the delivery to fail (but see timeout_defer). A zero time interval
+specifies no timeout. In order to ensure that any subprocesses created by the
+command are also killed, Exim makes the initial process a process group leader,
+and kills the whole process group on a timeout. However, this can be defeated
+if one of the processes starts a new process group.
+
++-------------+---------+-------------+--------------+
+|timeout_defer|Use: pipe|Type: boolean|Default: false|
++-------------+---------+-------------+--------------+
+
+A timeout in a pipe transport, either in the command that the transport runs,
+or in a transport filter that is associated with it, is by default treated as a
+hard error, and the delivery fails. However, if timeout_defer is set true, both
+kinds of timeout become temporary errors, causing the delivery to be deferred.
+
++-----+---------+-------------------+------------+
+|umask|Use: pipe|Type: octal integer|Default: 022|
++-----+---------+-------------------+------------+
+
+This specifies the umask setting for the subprocess that runs the command.
+
++---------+---------+-------------+--------------+
+|use_bsmtp|Use: pipe|Type: boolean|Default: false|
++---------+---------+-------------+--------------+
+
+If this option is set true, the pipe transport writes messages in "batch SMTP"
+format, with the envelope sender and recipient(s) included as SMTP commands. If
+you want to include a leading HELO command with such messages, you can do so by
+setting the message_prefix option. See section 47.10 for details of batch SMTP.
+
++------------------+---------+-------------+--------------+
+|use_classresources|Use: pipe|Type: boolean|Default: false|
++------------------+---------+-------------+--------------+
+
+This option is available only when Exim is running on FreeBSD, NetBSD, or BSD/
+OS. If it is set true, the setclassresources() function is used to set resource
+limits when a pipe transport is run to perform a delivery. The limits for the
+uid under which the pipe is to run are obtained from the login class database.
+
++--------+---------+-------------+--------------+
+|use_crlf|Use: pipe|Type: boolean|Default: false|
++--------+---------+-------------+--------------+
+
+This option causes lines to be terminated with the two-character CRLF sequence
+(carriage return, linefeed) instead of just a linefeed character. In the case
+of batched SMTP, the byte sequence written to the pipe is then an exact image
+of what would be sent down a real SMTP connection.
+
+The contents of the message_prefix and message_suffix options are written
+verbatim, so must contain their own carriage return characters if these are
+needed. When use_bsmtp is not set, the default values for both message_prefix
+and message_suffix end with a single linefeed, so their values must be changed
+to end with "\r\n" if use_crlf is set.
+
++---------+---------+-------------+--------------+
+|use_shell|Use: pipe|Type: boolean|Default: false|
++---------+---------+-------------+--------------+
+
+If this option is set, it causes the command to be passed to /bin/sh instead of
+being run directly from the transport, as described in section 29.3. This is
+less secure, but is needed in some situations where the command is expected to
+be run under a shell and cannot easily be modified. The allow_commands and 
+restrict_to_path options, and the "$pipe_addresses" facility are incompatible
+with use_shell. The command is expanded as a single string, and handed to /bin/
+sh as data for its -c option.
+
+
+29.6 Using an external local delivery agent
+-------------------------------------------
+
+The pipe transport can be used to pass all messages that require local delivery
+to a separate local delivery agent such as procmail. When doing this, care must
+be taken to ensure that the pipe is run under an appropriate uid and gid. In
+some configurations one wants this to be a uid that is trusted by the delivery
+agent to supply the correct sender of the message. It may be necessary to
+recompile or reconfigure the delivery agent so that it trusts an appropriate
+user. The following is an example transport and router configuration for 
+procmail:
+
+# transport
+procmail_pipe:
+  driver = pipe
+  command = /usr/local/bin/procmail -d $local_part
+  return_path_add
+  delivery_date_add
+  envelope_to_add
+  check_string = "From "
+  escape_string = ">From "
+  umask = 077
+  user = $local_part
+  group = mail
+
+# router
+procmail:
+  driver = accept
+  check_local_user
+  transport = procmail_pipe
+
+In this example, the pipe is run as the local user, but with the group set to 
+mail. An alternative is to run the pipe as a specific user such as mail or exim
+, but in this case you must arrange for procmail to trust that user to supply a
+correct sender address. If you do not specify either a group or a user option,
+the pipe command is run as the local user. The home directory is the user's
+home directory by default.
+
+Note: The command that the pipe transport runs does not begin with
+
+IFS=" "
+
+as shown in some procmail documentation, because Exim does not by default use a
+shell to run pipe commands.
+
+The next example shows a transport and a router for a system where local
+deliveries are handled by the Cyrus IMAP server.
+
+# transport
+local_delivery_cyrus:
+  driver = pipe
+  command = /usr/cyrus/bin/deliver \
+            -m ${substr_1:$local_part_suffix} -- $local_part
+  user = cyrus
+  group = mail
+  return_output
+  log_output
+  message_prefix =
+  message_suffix =
+
+# router
+local_user_cyrus:
+  driver = accept
+  check_local_user
+  local_part_suffix = .*
+  transport = local_delivery_cyrus
+
+Note the unsetting of message_prefix and message_suffix, and the use of 
+return_output to cause any text written by Cyrus to be returned to the sender.
+
+
+
+===============================================================================
+30. THE SMTP TRANSPORT
+
+The smtp transport delivers messages over TCP/IP connections using the SMTP or
+LMTP protocol. The list of hosts to try can either be taken from the address
+that is being processed (having been set up by the router), or specified
+explicitly for the transport. Timeout and retry processing (see chapter 32) is
+applied to each IP address independently.
+
+
+30.1 Multiple messages on a single connection
+---------------------------------------------
+
+The sending of multiple messages over a single TCP/IP connection can arise in
+two ways:
+
+  * If a message contains more than max_rcpt (see below) addresses that are
+    routed to the same host, more than one copy of the message has to be sent
+    to that host. In this situation, multiple copies may be sent in a single
+    run of the smtp transport over a single TCP/IP connection. (What Exim
+    actually does when it has too many addresses to send in one message also
+    depends on the value of the global remote_max_parallel option. Details are
+    given in section 47.1.)
+
+  * When a message has been successfully delivered over a TCP/IP connection,
+    Exim looks in its hints database to see if there are any other messages
+    awaiting a connection to the same host. If there are, a new delivery
+    process is started for one of them, and the current TCP/IP connection is
+    passed on to it. The new process may in turn send multiple copies and
+    possibly create yet another process.
+
+For each copy sent over the same TCP/IP connection, a sequence counter is
+incremented, and if it ever gets to the value of connection_max_messages, no
+further messages are sent over that connection.
+
+
+30.2 Use of the $host and $host_address variables
+-------------------------------------------------
+
+At the start of a run of the smtp transport, the values of $host and
+$host_address are the name and IP address of the first host on the host list
+passed by the router. However, when the transport is about to connect to a
+specific host, and while it is connected to that host, $host and $host_address
+are set to the values for that host. These are the values that are in force
+when the helo_data, hosts_try_auth, interface, serialize_hosts, and the various
+TLS options are expanded.
+
+
+30.3 Use of $tls_cipher and $tls_peerdn
+---------------------------------------
+
+At the start of a run of the smtp transport, the values of $tls_bits,
+$tls_cipher, $tls_peerdn and $tls_sni are the values that were set when the
+message was received. These are the values that are used for options that are
+expanded before any SMTP connections are made. Just before each connection is
+made, these four variables are emptied. If TLS is subsequently started, they
+are set to the appropriate values for the outgoing connection, and these are
+the values that are in force when any authenticators are run and when the 
+authenticated_sender option is expanded.
+
+These variables are deprecated in favour of $tls_in_cipher et. al. and will be
+removed in a future release.
+
+
+30.4 Private options for smtp
+-----------------------------
+
+The private options of the smtp transport are as follows:
+
++----------------------------+---------+-------------+-------------+
+|address_retry_include_sender|Use: smtp|Type: boolean|Default: true|
++----------------------------+---------+-------------+-------------+
+
+When an address is delayed because of a 4xx response to a RCPT command, it is
+the combination of sender and recipient that is delayed in subsequent queue
+runs until the retry time is reached. You can delay the recipient without
+reference to the sender (which is what earlier versions of Exim did), by
+setting address_retry_include_sender false. However, this can lead to problems
+with servers that regularly issue 4xx responses to RCPT commands.
+
++---------------+---------+-------------+--------------+
+|allow_localhost|Use: smtp|Type: boolean|Default: false|
++---------------+---------+-------------+--------------+
+
+When a host specified in hosts or fallback_hosts (see below) turns out to be
+the local host, or is listed in hosts_treat_as_local, delivery is deferred by
+default. However, if allow_localhost is set, Exim goes on to do the delivery
+anyway. This should be used only in special cases when the configuration
+ensures that no looping will result (for example, a differently configured Exim
+is listening on the port to which the message is sent).
+
++--------------------+---------+-------------+--------------+
+|authenticated_sender|Use: smtp|Type: string*|Default: unset|
++--------------------+---------+-------------+--------------+
+
+When Exim has authenticated as a client, or if authenticated_sender_force is
+true, this option sets a value for the AUTH= item on outgoing MAIL commands,
+overriding any existing authenticated sender value. If the string expansion is
+forced to fail, the option is ignored. Other expansion failures cause delivery
+to be deferred. If the result of expansion is an empty string, that is also
+ignored.
+
+The expansion happens after the outgoing connection has been made and TLS
+started, if required. This means that the $host, $host_address, $tls_out_cipher
+, and $tls_out_peerdn variables are set according to the particular connection.
+
+If the SMTP session is not authenticated, the expansion of authenticated_sender
+still happens (and can cause the delivery to be deferred if it fails), but no
+AUTH= item is added to MAIL commands unless authenticated_sender_force is true.
+
+This option allows you to use the smtp transport in LMTP mode to deliver mail
+to Cyrus IMAP and provide the proper local part as the "authenticated sender",
+via a setting such as:
+
+authenticated_sender = $local_part
+
+This removes the need for IMAP subfolders to be assigned special ACLs to allow
+direct delivery to those subfolders.
+
+Because of expected uses such as that just described for Cyrus (when no domain
+is involved), there is no checking on the syntax of the provided value.
+
++--------------------------+---------+-------------+--------------+
+|authenticated_sender_force|Use: smtp|Type: boolean|Default: false|
++--------------------------+---------+-------------+--------------+
+
+If this option is set true, the authenticated_sender option's value is used for
+the AUTH= item on outgoing MAIL commands, even if Exim has not authenticated as
+a client.
+
++---------------+---------+----------+-----------+
+|command_timeout|Use: smtp|Type: time|Default: 5m|
++---------------+---------+----------+-----------+
+
+This sets a timeout for receiving a response to an SMTP command that has been
+sent out. It is also used when waiting for the initial banner line from the
+remote host. Its value must not be zero.
+
++---------------+---------+----------+-----------+
+|connect_timeout|Use: smtp|Type: time|Default: 5m|
++---------------+---------+----------+-----------+
+
+This sets a timeout for the connect() function, which sets up a TCP/IP call to
+a remote host. A setting of zero allows the system timeout (typically several
+minutes) to act. To have any effect, the value of this option must be less than
+the system timeout. However, it has been observed that on some systems there is
+no system timeout, which is why the default value for this option is 5 minutes,
+a value recommended by RFC 1123.
+
++-----------------------+---------+-------------+------------+
+|connection_max_messages|Use: smtp|Type: integer|Default: 500|
++-----------------------+---------+-------------+------------+
+
+This controls the maximum number of separate message deliveries that are sent
+over a single TCP/IP connection. If the value is zero, there is no limit. For
+testing purposes, this value can be overridden by the -oB command line option.
+
++------------+---------+----------+-----------+
+|data_timeout|Use: smtp|Type: time|Default: 5m|
++------------+---------+----------+-----------+
+
+This sets a timeout for the transmission of each block in the data portion of
+the message. As a result, the overall timeout for a message depends on the size
+of the message. Its value must not be zero. See also final_timeout.
+
++------------------+---------+-------------+-------------+
+|delay_after_cutoff|Use: smtp|Type: boolean|Default: true|
++------------------+---------+-------------+-------------+
+
+This option controls what happens when all remote IP addresses for a given
+domain have been inaccessible for so long that they have passed their retry
+cutoff times.
+
+In the default state, if the next retry time has not been reached for any of
+them, the address is bounced without trying any deliveries. In other words,
+Exim delays retrying an IP address after the final cutoff time until a new
+retry time is reached, and can therefore bounce an address without ever trying
+a delivery, when machines have been down for a long time. Some people are
+unhappy at this prospect, so...
+
+If delay_after_cutoff is set false, Exim behaves differently. If all IP
+addresses are past their final cutoff time, Exim tries to deliver to those IP
+addresses that have not been tried since the message arrived. If there are
+none, of if they all fail, the address is bounced. In other words, it does not
+delay when a new message arrives, but immediately tries those expired IP
+addresses that haven't been tried since the message arrived. If there is a
+continuous stream of messages for the dead hosts, unsetting delay_after_cutoff
+means that there will be many more attempts to deliver to them.
+
++------------------+---------+-------------+-------------+
+|dns_qualify_single|Use: smtp|Type: boolean|Default: true|
++------------------+---------+-------------+-------------+
+
+If the hosts or fallback_hosts option is being used, and the gethostbyname
+option is false, the RES_DEFNAMES resolver option is set. See the 
+qualify_single option in chapter 17 for more details.
+
++------------------+---------+-------------+--------------+
+|dns_search_parents|Use: smtp|Type: boolean|Default: false|
++------------------+---------+-------------+--------------+
+
+If the hosts or fallback_hosts option is being used, and the gethostbyname
+option is false, the RES_DNSRCH resolver option is set. See the search_parents
+option in chapter 17 for more details.
+
++----------------------+---------+------------------+--------------+
+|dnssec_request_domains|Use: smtp|Type: domain list*|Default: unset|
++----------------------+---------+------------------+--------------+
+
+DNS lookups for domains matching dnssec_request_domains will be done with the
+dnssec request bit set. This applies to all of the SRV, MX A6, AAAA, A lookup
+sequence.
+
++----------------------+---------+------------------+--------------+
+|dnssec_require_domains|Use: smtp|Type: domain list*|Default: unset|
++----------------------+---------+------------------+--------------+
+
+DNS lookups for domains matching dnssec_request_domains will be done with the
+dnssec request bit set. Any returns not having the Authenticated Data bit (AD
+bit) set wil be ignored and logged as a host-lookup failure. This applies to
+all of the SRV, MX A6, AAAA, A lookup sequence.
+
++----+---------+-------------+--------------+
+|dscp|Use: smtp|Type: string*|Default: unset|
++----+---------+-------------+--------------+
+
+This option causes the DSCP value associated with a socket to be set to one of
+a number of fixed strings or to numeric value. The -bI:dscp option may be used
+to ask Exim which names it knows of. Common values include "throughput",
+"mincost", and on newer systems "ef", "af41", etc. Numeric values may be in the
+range 0 to 0x3F.
+
+The outbound packets from Exim will be marked with this value in the header
+(for IPv4, the TOS field; for IPv6, the TCLASS field); there is no guarantee
+that these values will have any effect, not be stripped by networking
+equipment, or do much of anything without cooperation with your Network
+Engineer and those of all network operators between the source and destination.
+
++--------------+---------+-----------------+--------------+
+|fallback_hosts|Use: smtp|Type: string list|Default: unset|
++--------------+---------+-----------------+--------------+
+
+String expansion is not applied to this option. The argument must be a
+colon-separated list of host names or IP addresses, optionally also including
+port numbers, though the separator can be changed, as described in section 6.19
+. Each individual item in the list is the same as an item in a route_list
+setting for the manualroute router, as described in section 20.5.
+
+Fallback hosts can also be specified on routers, which associate them with the
+addresses they process. As for the hosts option without hosts_override, 
+fallback_hosts specified on the transport is used only if the address does not
+have its own associated fallback host list. Unlike hosts, a setting of 
+fallback_hosts on an address is not overridden by hosts_override. However, 
+hosts_randomize does apply to fallback host lists.
+
+If Exim is unable to deliver to any of the hosts for a particular address, and
+the errors are not permanent rejections, the address is put on a separate
+transport queue with its host list replaced by the fallback hosts, unless the
+address was routed via MX records and the current host was in the original MX
+list. In that situation, the fallback host list is not used.
+
+Once normal deliveries are complete, the fallback queue is delivered by
+re-running the same transports with the new host lists. If several failing
+addresses have the same fallback hosts (and max_rcpt permits it), a single copy
+of the message is sent.
+
+The resolution of the host names on the fallback list is controlled by the 
+gethostbyname option, as for the hosts option. Fallback hosts apply both to
+cases when the host list comes with the address and when it is taken from hosts
+. This option provides a "use a smart host only if delivery fails" facility.
+
++-------------+---------+----------+------------+
+|final_timeout|Use: smtp|Type: time|Default: 10m|
++-------------+---------+----------+------------+
+
+This is the timeout that applies while waiting for the response to the final
+line containing just "." that terminates a message. Its value must not be zero.
+
++-------------+---------+-------------+--------------+
+|gethostbyname|Use: smtp|Type: boolean|Default: false|
++-------------+---------+-------------+--------------+
+
+If this option is true when the hosts and/or fallback_hosts options are being
+used, names are looked up using gethostbyname() (or getipnodebyname() when
+available) instead of using the DNS. Of course, that function may in fact use
+the DNS, but it may also consult other sources of information such as /etc/
+hosts.
+
++------------------+---------+-------------+--------------+
+|gnutls_compat_mode|Use: smtp|Type: boolean|Default: unset|
++------------------+---------+-------------+--------------+
+
+This option controls whether GnuTLS is used in compatibility mode in an Exim
+server. This reduces security slightly, but improves interworking with older
+implementations of TLS.
+
++---------+---------+-------------+------------------+
+|helo_data|Use: smtp|Type: string*|Default: see below|
++---------+---------+-------------+------------------+
+
+The value of this option is expanded after a connection to a another host has
+been set up. The result is used as the argument for the EHLO, HELO, or LHLO
+command that starts the outgoing SMTP or LMTP session. The default value of the
+option is:
+
+$primary_hostname
+
+During the expansion, the variables $host and $host_address are set to the
+identity of the remote host, and the variables $sending_ip_address and
+$sending_port are set to the local IP address and port number that are being
+used. These variables can be used to generate different values for different
+servers or different local IP addresses. For example, if you want the string
+that is used for helo_data to be obtained by a DNS lookup of the outgoing
+interface address, you could use this:
+
+helo_data = ${lookup dnsdb{ptr=$sending_ip_address}{$value}\
+  {$primary_hostname}}
+
+The use of helo_data applies both to sending messages and when doing callouts.
+
++-----+---------+------------------+--------------+
+|hosts|Use: smtp|Type: string list*|Default: unset|
++-----+---------+------------------+--------------+
+
+Hosts are associated with an address by a router such as dnslookup, which finds
+the hosts by looking up the address domain in the DNS, or by manualroute, which
+has lists of hosts in its configuration. However, email addresses can be passed
+to the smtp transport by any router, and not all of them can provide an
+associated list of hosts.
+
+The hosts option specifies a list of hosts to be used if the address being
+processed does not have any hosts associated with it. The hosts specified by 
+hosts are also used, whether or not the address has its own hosts, if 
+hosts_override is set.
+
+The string is first expanded, before being interpreted as a colon-separated
+list of host names or IP addresses, possibly including port numbers. The
+separator may be changed to something other than colon, as described in section
+6.19. Each individual item in the list is the same as an item in a route_list
+setting for the manualroute router, as described in section 20.5. However, note
+that the "/MX" facility of the manualroute router is not available here.
+
+If the expansion fails, delivery is deferred. Unless the failure was caused by
+the inability to complete a lookup, the error is logged to the panic log as
+well as the main log. Host names are looked up either by searching directly for
+address records in the DNS or by calling gethostbyname() (or getipnodebyname()
+when available), depending on the setting of the gethostbyname option. When
+Exim is compiled with IPv6 support, if a host that is looked up in the DNS has
+both IPv4 and IPv6 addresses, both types of address are used.
+
+During delivery, the hosts are tried in order, subject to their retry status,
+unless hosts_randomize is set.
+
++-----------------+---------+----------------+--------------+
+|hosts_avoid_esmtp|Use: smtp|Type: host list*|Default: unset|
++-----------------+---------+----------------+--------------+
+
+This option is for use with broken hosts that announce ESMTP facilities (for
+example, PIPELINING) and then fail to implement them properly. When a host
+matches hosts_avoid_esmtp, Exim sends HELO rather than EHLO at the start of the
+SMTP session. This means that it cannot use any of the ESMTP facilities such as
+AUTH, PIPELINING, SIZE, and STARTTLS.
+
++----------------------+---------+----------------+--------------+
+|hosts_avoid_pipelining|Use: smtp|Type: host list*|Default: unset|
++----------------------+---------+----------------+--------------+
+
+Exim will not use the SMTP PIPELINING extension when delivering to any host
+that matches this list, even if the server host advertises PIPELINING support.
+
++---------------+---------+----------------+--------------+
+|hosts_avoid_tls|Use: smtp|Type: host list*|Default: unset|
++---------------+---------+----------------+--------------+
+
+Exim will not try to start a TLS session when delivering to any host that
+matches this list. See chapter 41 for details of TLS.
+
++----------------------+---------+----------------+----------+
+|hosts_verify_avoid_tls|Use: smtp|Type: host list*|Default: *|
++----------------------+---------+----------------+----------+
+
+Exim will not try to start a TLS session for a verify callout, or when
+delivering in cutthrough mode, to any host that matches this list. Note that
+the default is to not use TLS.
+
++-------------+---------+-------------+----------+
+|hosts_max_try|Use: smtp|Type: integer|Default: 5|
++-------------+---------+-------------+----------+
+
+This option limits the number of IP addresses that are tried for any one
+delivery in cases where there are temporary delivery errors. Section 30.5
+describes in detail how the value of this option is used.
+
++-----------------------+---------+-------------+-----------+
+|hosts_max_try_hardlimit|Use: smtp|Type: integer|Default: 50|
++-----------------------+---------+-------------+-----------+
+
+This is an additional check on the maximum number of IP addresses that Exim
+tries for any one delivery. Section 30.5 describes its use and why it exists.
+
++----------------+---------+----------------+--------------+
+|hosts_nopass_tls|Use: smtp|Type: host list*|Default: unset|
++----------------+---------+----------------+--------------+
+
+For any host that matches this list, a connection on which a TLS session has
+been started will not be passed to a new delivery process for sending another
+message on the same connection. See section 41.11 for an explanation of when
+this might be needed.
+
++--------------+---------+-------------+--------------+
+|hosts_override|Use: smtp|Type: boolean|Default: false|
++--------------+---------+-------------+--------------+
+
+If this option is set and the hosts option is also set, any hosts that are
+attached to the address are ignored, and instead the hosts specified by the 
+hosts option are always used. This option does not apply to fallback_hosts.
+
++---------------+---------+-------------+--------------+
+|hosts_randomize|Use: smtp|Type: boolean|Default: false|
++---------------+---------+-------------+--------------+
+
+If this option is set, and either the list of hosts is taken from the hosts or
+the fallback_hosts option, or the hosts supplied by the router were not
+obtained from MX records (this includes fallback hosts from the router), and
+were not randomized by the router, the order of trying the hosts is randomized
+each time the transport runs. Randomizing the order of a host list can be used
+to do crude load sharing.
+
+When hosts_randomize is true, a host list may be split into groups whose order
+is separately randomized. This makes it possible to set up MX-like behaviour.
+The boundaries between groups are indicated by an item that is just "+" in the
+host list. For example:
+
+hosts = host1:host2:host3:+:host4:host5
+
+The order of the first three hosts and the order of the last two hosts is
+randomized for each use, but the first three always end up before the last two.
+If hosts_randomize is not set, a "+" item in the list is ignored.
+
++------------------+---------+----------------+--------------+
+|hosts_require_auth|Use: smtp|Type: host list*|Default: unset|
++------------------+---------+----------------+--------------+
+
+This option provides a list of servers for which authentication must succeed
+before Exim will try to transfer a message. If authentication fails for servers
+which are not in this list, Exim tries to send unauthenticated. If
+authentication fails for one of these servers, delivery is deferred. This
+temporary error is detectable in the retry rules, so it can be turned into a
+hard failure if required. See also hosts_try_auth, and chapter 33 for details
+of authentication.
+
++------------------+---------+----------------+----------+
+|hosts_request_ocsp|Use: smtp|Type: host list*|Default: *|
++------------------+---------+----------------+----------+
+
+Exim will request a Certificate Status on a TLS session for any host that
+matches this list. tls_verify_certificates should also be set for the
+transport.
+
++------------------+---------+----------------+--------------+
+|hosts_require_ocsp|Use: smtp|Type: host list*|Default: unset|
++------------------+---------+----------------+--------------+
+
+Exim will request, and check for a valid Certificate Status being given, on a
+TLS session for any host that matches this list. tls_verify_certificates should
+also be set for the transport.
+
++-----------------+---------+----------------+--------------+
+|hosts_require_tls|Use: smtp|Type: host list*|Default: unset|
++-----------------+---------+----------------+--------------+
+
+Exim will insist on using a TLS session when delivering to any host that
+matches this list. See chapter 41 for details of TLS. Note: This option affects
+outgoing mail only. To insist on TLS for incoming messages, use an appropriate
+ACL.
+
++--------------+---------+----------------+--------------+
+|hosts_try_auth|Use: smtp|Type: host list*|Default: unset|
++--------------+---------+----------------+--------------+
+
+This option provides a list of servers to which, provided they announce
+authentication support, Exim will attempt to authenticate as a client when it
+connects. If authentication fails, Exim will try to transfer the message
+unauthenticated. See also hosts_require_auth, and chapter 33 for details of
+authentication.
+
++--------------+---------+----------------+--------------+
+|hosts_try_prdr|Use: smtp|Type: host list*|Default: unset|
++--------------+---------+----------------+--------------+
+
+This option provides a list of servers to which, provided they announce PRDR
+support, Exim will attempt to negotiate PRDR for multi-recipient messages.
+
++---------+---------+------------------+--------------+
+|interface|Use: smtp|Type: string list*|Default: unset|
++---------+---------+------------------+--------------+
+
+This option specifies which interface to bind to when making an outgoing SMTP
+call. The value is an IP address, not an interface name such as "eth0". Do not
+confuse this with the interface address that was used when a message was
+received, which is in $received_ip_address, formerly known as
+$interface_address. The name was changed to minimize confusion with the
+outgoing interface address. There is no variable that contains an outgoing
+interface address because, unless it is set by this option, its value is
+unknown.
+
+During the expansion of the interface option the variables $host and
+$host_address refer to the host to which a connection is about to be made
+during the expansion of the string. Forced expansion failure, or an empty
+string result causes the option to be ignored. Otherwise, after expansion, the
+string must be a list of IP addresses, colon-separated by default, but the
+separator can be changed in the usual way. For example:
+
+interface = <; 192.168.123.123 ; 3ffe:ffff:836f::fe86:a061
+
+The first interface of the correct type (IPv4 or IPv6) is used for the outgoing
+connection. If none of them are the correct type, the option is ignored. If 
+interface is not set, or is ignored, the system's IP functions choose which
+interface to use if the host has more than one.
+
++---------+---------+-------------+-------------+
+|keepalive|Use: smtp|Type: boolean|Default: true|
++---------+---------+-------------+-------------+
+
+This option controls the setting of SO_KEEPALIVE on outgoing TCP/IP socket
+connections. When set, it causes the kernel to probe idle connections
+periodically, by sending packets with "old" sequence numbers. The other end of
+the connection should send a acknowledgment if the connection is still okay or
+a reset if the connection has been aborted. The reason for doing this is that
+it has the beneficial effect of freeing up certain types of connection that can
+get stuck when the remote host is disconnected without tidying up the TCP/IP
+call properly. The keepalive mechanism takes several hours to detect
+unreachable hosts.
+
++-----------------+---------+-------------+--------------+
+|lmtp_ignore_quota|Use: smtp|Type: boolean|Default: false|
++-----------------+---------+-------------+--------------+
+
+If this option is set true when the protocol option is set to "lmtp", the
+string "IGNOREQUOTA" is added to RCPT commands, provided that the LMTP server
+has advertised support for IGNOREQUOTA in its response to the LHLO command.
+
++--------+---------+-------------+------------+
+|max_rcpt|Use: smtp|Type: integer|Default: 100|
++--------+---------+-------------+------------+
+
+This option limits the number of RCPT commands that are sent in a single SMTP
+message transaction. Each set of addresses is treated independently, and so can
+cause parallel connections to the same host if remote_max_parallel permits
+this.
+
++------------+---------+-------------+-------------+
+|multi_domain|Use: smtp|Type: boolean|Default: true|
++------------+---------+-------------+-------------+
+
+When this option is set, the smtp transport can handle a number of addresses
+containing a mixture of different domains provided they all resolve to the same
+list of hosts. Turning the option off restricts the transport to handling only
+one domain at a time. This is useful if you want to use $domain in an expansion
+for the transport, because it is set only when there is a single domain
+involved in a remote delivery.
+
++----+---------+-------------+------------------+
+|port|Use: smtp|Type: string*|Default: see below|
++----+---------+-------------+------------------+
+
+This option specifies the TCP/IP port on the server to which Exim connects. 
+Note: Do not confuse this with the port that was used when a message was
+received, which is in $received_port, formerly known as $interface_port. The
+name was changed to minimize confusion with the outgoing port. There is no
+variable that contains an outgoing port.
+
+If the value of this option begins with a digit it is taken as a port number;
+otherwise it is looked up using getservbyname(). The default value is normally
+"smtp", but if protocol is set to "lmtp", the default is "lmtp". If the
+expansion fails, or if a port number cannot be found, delivery is deferred.
+
++--------+---------+------------+-------------+
+|protocol|Use: smtp|Type: string|Default: smtp|
++--------+---------+------------+-------------+
+
+If this option is set to "lmtp" instead of "smtp", the default value for the 
+port option changes to "lmtp", and the transport operates the LMTP protocol
+(RFC 2033) instead of SMTP. This protocol is sometimes used for local
+deliveries into closed message stores. Exim also has support for running LMTP
+over a pipe to a local process - see chapter 28.
+
+If this option is set to "smtps", the default vaule for the port option changes
+to "smtps", and the transport initiates TLS immediately after connecting, as an
+outbound SSL-on-connect, instead of using STARTTLS to upgrade. The Internet
+standards bodies strongly discourage use of this mode.
+
++------------------------+---------+-------------+-------------+
+|retry_include_ip_address|Use: smtp|Type: boolean|Default: true|
++------------------------+---------+-------------+-------------+
+
+Exim normally includes both the host name and the IP address in the key it
+constructs for indexing retry data after a temporary delivery failure. This
+means that when one of several IP addresses for a host is failing, it gets
+tried periodically (controlled by the retry rules), but use of the other IP
+addresses is not affected.
+
+However, in some dialup environments hosts are assigned a different IP address
+each time they connect. In this situation the use of the IP address as part of
+the retry key leads to undesirable behaviour. Setting this option false causes
+Exim to use only the host name. This should normally be done on a separate
+instance of the smtp transport, set up specially to handle the dialup hosts.
+
++---------------+---------+----------------+--------------+
+|serialize_hosts|Use: smtp|Type: host list*|Default: unset|
++---------------+---------+----------------+--------------+
+
+Because Exim operates in a distributed manner, if several messages for the same
+host arrive at around the same time, more than one simultaneous connection to
+the remote host can occur. This is not usually a problem except when there is a
+slow link between the hosts. In that situation it may be helpful to restrict
+Exim to one connection at a time. This can be done by setting serialize_hosts
+to match the relevant hosts.
+
+Exim implements serialization by means of a hints database in which a record is
+written whenever a process connects to one of the restricted hosts. The record
+is deleted when the connection is completed. Obviously there is scope for
+records to get left lying around if there is a system or program crash. To
+guard against this, Exim ignores any records that are more than six hours old.
+
+If you set up this kind of serialization, you should also arrange to delete the
+relevant hints database whenever your system reboots. The names of the files
+start with misc and they are kept in the spool/db directory. There may be one
+or two files, depending on the type of DBM in use. The same files are used for
+ETRN serialization.
+
++-------------+---------+-------------+-------------+
+|size_addition|Use: smtp|Type: integer|Default: 1024|
++-------------+---------+-------------+-------------+
+
+If a remote SMTP server indicates that it supports the SIZE option of the MAIL
+command, Exim uses this to pass over the message size at the start of an SMTP
+transaction. It adds the value of size_addition to the value it sends, to allow
+for headers and other text that may be added during delivery by configuration
+options or in a transport filter. It may be necessary to increase this if a lot
+of text is added to messages.
+
+Alternatively, if the value of size_addition is set negative, it disables the
+use of the SIZE option altogether.
+
++---------------+---------+-------------+--------------+
+|tls_certificate|Use: smtp|Type: string*|Default: unset|
++---------------+---------+-------------+--------------+
+
+The value of this option must be the absolute path to a file which contains the
+client's certificate, for possible use when sending a message over an encrypted
+connection. The values of $host and $host_address are set to the name and
+address of the server during the expansion. See chapter 41 for details of TLS.
+
+Note: This option must be set if you want Exim to be able to use a TLS
+certificate when sending messages as a client. The global option of the same
+name specifies the certificate for Exim as a server; it is not automatically
+assumed that the same certificate should be used when Exim is operating as a
+client.
+
++-------+---------+-------------+--------------+
+|tls_crl|Use: smtp|Type: string*|Default: unset|
++-------+---------+-------------+--------------+
+
+This option specifies a certificate revocation list. The expanded value must be
+the name of a file that contains a CRL in PEM format.
+
++---------------+---------+-------------+-------------+
+|tls_dh_min_bits|Use: smtp|Type: integer|Default: 1024|
++---------------+---------+-------------+-------------+
+
+When establishing a TLS session, if a ciphersuite which uses Diffie-Hellman key
+agreement is negotiated, the server will provide a large prime number for use.
+This option establishes the minimum acceptable size of that number. If the
+parameter offered by the server is too small, then the TLS handshake will fail.
+
+Only supported when using GnuTLS.
+
++--------------+---------+-------------+--------------+
+|tls_privatekey|Use: smtp|Type: string*|Default: unset|
++--------------+---------+-------------+--------------+
+
+The value of this option must be the absolute path to a file which contains the
+client's private key. This is used when sending a message over an encrypted
+connection using a client certificate. The values of $host and $host_address
+are set to the name and address of the server during the expansion. If this
+option is unset, or the expansion is forced to fail, or the result is an empty
+string, the private key is assumed to be in the same file as the certificate.
+See chapter 41 for details of TLS.
+
++-------------------+---------+-------------+--------------+
+|tls_require_ciphers|Use: smtp|Type: string*|Default: unset|
++-------------------+---------+-------------+--------------+
+
+The value of this option must be a list of permitted cipher suites, for use
+when setting up an outgoing encrypted connection. (There is a global option of
+the same name for controlling incoming connections.) The values of $host and
+$host_address are set to the name and address of the server during the
+expansion. See chapter 41 for details of TLS; note that this option is used in
+different ways by OpenSSL and GnuTLS (see sections 41.4 and 41.5). For GnuTLS,
+the order of the ciphers is a preference order.
+
++-------+---------+-------------+--------------+
+|tls_sni|Use: smtp|Type: string*|Default: unset|
++-------+---------+-------------+--------------+
+
+If this option is set then it sets the $tls_out_sni variable and causes any TLS
+session to pass this value as the Server Name Indication extension to the
+remote side, which can be used by the remote side to select an appropriate
+certificate and private key for the session.
+
+See 41.10 for more information.
+
+Note that for OpenSSL, this feature requires a build of OpenSSL that supports
+TLS extensions.
+
++---------------------+---------+-------------+-------------+
+|tls_tempfail_tryclear|Use: smtp|Type: boolean|Default: true|
++---------------------+---------+-------------+-------------+
+
+When the server host is not in hosts_require_tls, and there is a problem in
+setting up a TLS session, this option determines whether or not Exim should try
+to deliver the message unencrypted. If it is set false, delivery to the current
+host is deferred; if there are other hosts, they are tried. If this option is
+set true, Exim attempts to deliver unencrypted after a 4xx response to
+STARTTLS. Also, if STARTTLS is accepted, but the subsequent TLS negotiation
+fails, Exim closes the current connection (because it is in an unknown state),
+opens a new one to the same host, and then tries the delivery in clear.
+
++--------------------+---------+----------------------+--------+
+|tls_try_verify_hosts|Use: smtp|Type: host list* unset|Default:|
++--------------------+---------+----------------------+--------+
+
+This option gives a list of hosts for which, on encrypted connections,
+certificate verification will be tried but need not succeed. The 
+tls_verify_certificates option must also be set. Note that unless the host is
+in this list TLS connections will be denied to hosts using self-signed
+certificates when tls_verify_certificates is set. The
+$tls_out_certificate_verified variable is set when certificate verification
+succeeds.
+
++-----------------------+---------+-------------+--------------+
+|tls_verify_certificates|Use: smtp|Type: string*|Default: unset|
++-----------------------+---------+-------------+--------------+
+
+The value of this option must be the absolute path to a file containing
+permitted server certificates, for use when setting up an encrypted connection.
+Alternatively, if you are using OpenSSL, you can set tls_verify_certificates to
+the name of a directory containing certificate files. This does not work with
+GnuTLS; the option must be set to the name of a single file if you are using
+GnuTLS. The values of $host and $host_address are set to the name and address
+of the server during the expansion of this option. See chapter 41 for details
+of TLS.
+
+For back-compatability, if neither tls_verify_hosts nor tls_try_verify_hosts
+are set and certificate verification fails the TLS connection is closed.
+
++----------------+---------+----------------------+--------+
+|tls_verify_hosts|Use: smtp|Type: host list* unset|Default:|
++----------------+---------+----------------------+--------+
+
+This option gives a list of hosts for which. on encrypted connections,
+certificate verification must succeed. The tls_verify_certificates option must
+also be set. If both this option and tls_try_verify_hosts are unset operation
+is as if this option selected all hosts.
+
+
+30.5 How the limits for the number of hosts to try are used
+-----------------------------------------------------------
+
+There are two options that are concerned with the number of hosts that are
+tried when an SMTP delivery takes place. They are hosts_max_try and 
+hosts_max_try_hardlimit.
+
+The hosts_max_try option limits the number of hosts that are tried for a single
+delivery. However, despite the term "host" in its name, the option actually
+applies to each IP address independently. In other words, a multihomed host is
+treated as several independent hosts, just as it is for retrying.
+
+Many of the larger ISPs have multiple MX records which often point to
+multihomed hosts. As a result, a list of a dozen or more IP addresses may be
+created as a result of routing one of these domains.
+
+Trying every single IP address on such a long list does not seem sensible; if
+several at the top of the list fail, it is reasonable to assume there is some
+problem that is likely to affect all of them. Roughly speaking, the value of 
+hosts_max_try is the maximum number that are tried before deferring the
+delivery. However, the logic cannot be quite that simple.
+
+Firstly, IP addresses that are skipped because their retry times have not
+arrived do not count, and in addition, addresses that are past their retry
+limits are also not counted, even when they are tried. This means that when
+some IP addresses are past their retry limits, more than the value of 
+hosts_max_retry may be tried. The reason for this behaviour is to ensure that
+all IP addresses are considered before timing out an email address (but see
+below for an exception).
+
+Secondly, when the hosts_max_try limit is reached, Exim looks down the host
+list to see if there is a subsequent host with a different (higher valued) MX.
+If there is, that host is considered next, and the current IP address is used
+but not counted. This behaviour helps in the case of a domain with a retry rule
+that hardly ever delays any hosts, as is now explained:
+
+Consider the case of a long list of hosts with one MX value, and a few with a
+higher MX value. If hosts_max_try is small (the default is 5) only a few hosts
+at the top of the list are tried at first. With the default retry rule, which
+specifies increasing retry times, the higher MX hosts are eventually tried when
+those at the top of the list are skipped because they have not reached their
+retry times.
+
+However, it is common practice to put a fixed short retry time on domains for
+large ISPs, on the grounds that their servers are rarely down for very long.
+Unfortunately, these are exactly the domains that tend to resolve to long lists
+of hosts. The short retry time means that the lowest MX hosts are tried every
+time. The attempts may be in a different order because of random sorting, but
+without the special MX check, the higher MX hosts would never be tried until
+all the lower MX hosts had timed out (which might be several days), because
+there are always some lower MX hosts that have reached their retry times. With
+the special check, Exim considers at least one IP address from each MX value at
+every delivery attempt, even if the hosts_max_try limit has already been
+reached.
+
+The above logic means that hosts_max_try is not a hard limit, and in
+particular, Exim normally eventually tries all the IP addresses before timing
+out an email address. When hosts_max_try was implemented, this seemed a
+reasonable thing to do. Recently, however, some lunatic DNS configurations have
+been set up with hundreds of IP addresses for some domains. It can take a very
+long time indeed for an address to time out in these cases.
+
+The hosts_max_try_hardlimit option was added to help with this problem. Exim
+never tries more than this number of IP addresses; if it hits this limit and
+they are all timed out, the email address is bounced, even though not all
+possible IP addresses have been tried.
+
+
+
+===============================================================================
+31. ADDRESS REWRITING
+
+There are some circumstances in which Exim automatically rewrites domains in
+addresses. The two most common are when an address is given without a domain
+(referred to as an "unqualified address") or when an address contains an
+abbreviated domain that is expanded by DNS lookup.
+
+Unqualified envelope addresses are accepted only for locally submitted
+messages, or for messages that are received from hosts matching 
+sender_unqualified_hosts or recipient_unqualified_hosts, as appropriate.
+Unqualified addresses in header lines are qualified if they are in locally
+submitted messages, or messages from hosts that are permitted to send
+unqualified envelope addresses. Otherwise, unqualified addresses in header
+lines are neither qualified nor rewritten.
+
+One situation in which Exim does not automatically rewrite a domain is when it
+is the name of a CNAME record in the DNS. The older RFCs suggest that such a
+domain should be rewritten using the "canonical" name, and some MTAs do this.
+The new RFCs do not contain this suggestion.
+
+
+31.1 Explicitly configured address rewriting
+--------------------------------------------
+
+This chapter describes the rewriting rules that can be used in the main rewrite
+section of the configuration file, and also in the generic headers_rewrite
+option that can be set on any transport.
+
+Some people believe that configured address rewriting is a Mortal Sin. Others
+believe that life is not possible without it. Exim provides the facility; you
+do not have to use it.
+
+The main rewriting rules that appear in the "rewrite" section of the
+configuration file are applied to addresses in incoming messages, both envelope
+addresses and addresses in header lines. Each rule specifies the types of
+address to which it applies.
+
+Whether or not addresses in header lines are rewritten depends on the origin of
+the headers and the type of rewriting. Global rewriting, that is, rewriting
+rules from the rewrite section of the configuration file, is applied only to
+those headers that were received with the message. Header lines that are added
+by ACLs or by a system filter or by individual routers or transports (which are
+specific to individual recipient addresses) are not rewritten by the global
+rules.
+
+Rewriting at transport time, by means of the headers_rewrite option, applies
+all headers except those added by routers and transports. That is, as well as
+the headers that were received with the message, it also applies to headers
+that were added by an ACL or a system filter.
+
+In general, rewriting addresses from your own system or domain has some
+legitimacy. Rewriting other addresses should be done only with great care and
+in special circumstances. The author of Exim believes that rewriting should be
+used sparingly, and mainly for "regularizing" addresses in your own domains.
+Although it can sometimes be used as a routing tool, this is very strongly
+discouraged.
+
+There are two commonly encountered circumstances where rewriting is used, as
+illustrated by these examples:
+
+  * The company whose domain is hitch.fict.example has a number of hosts that
+    exchange mail with each other behind a firewall, but there is only a single
+    gateway to the outer world. The gateway rewrites *.hitch.fict.example as 
+    hitch.fict.example when sending mail off-site.
+
+  * A host rewrites the local parts of its own users so that, for example, 
+    fp42@hitch.fict.example becomes Ford.Prefect@hitch.fict.example.
+
+
+31.2 When does rewriting happen?
+--------------------------------
+
+Configured address rewriting can take place at several different stages of a
+message's processing.
+
+At the start of an ACL for MAIL, the sender address may have been rewritten by
+a special SMTP-time rewrite rule (see section 31.9), but no ordinary rewrite
+rules have yet been applied. If, however, the sender address is verified in the
+ACL, it is rewritten before verification, and remains rewritten thereafter. The
+subsequent value of $sender_address is the rewritten address. This also applies
+if sender verification happens in a RCPT ACL. Otherwise, when the sender
+address is not verified, it is rewritten as soon as a message's header lines
+have been received.
+
+Similarly, at the start of an ACL for RCPT, the current recipient's address may
+have been rewritten by a special SMTP-time rewrite rule, but no ordinary
+rewrite rules have yet been applied to it. However, the behaviour is different
+from the sender address when a recipient is verified. The address is rewritten
+for the verification, but the rewriting is not remembered at this stage. The
+value of $local_part and $domain after verification are always the same as they
+were before (that is, they contain the unrewritten - except for SMTP-time
+rewriting - address).
+
+As soon as a message's header lines have been received, all the envelope
+recipient addresses are permanently rewritten, and rewriting is also applied to
+the addresses in the header lines (if configured). This happens before adding
+any header lines that were specified in MAIL or RCPT ACLs, and before the DATA
+ACL and local_scan() functions are run.
+
+When an address is being routed, either for delivery or for verification,
+rewriting is applied immediately to child addresses that are generated by
+redirection, unless no_rewrite is set on the router.
+
+At transport time, additional rewriting of addresses in header lines can be
+specified by setting the generic headers_rewrite option on a transport. This
+option contains rules that are identical in form to those in the rewrite
+section of the configuration file. They are applied to the original message
+header lines and any that were added by ACLs or a system filter. They are not
+applied to header lines that are added by routers or the transport.
+
+The outgoing envelope sender can be rewritten by means of the return_path
+transport option. However, it is not possible to rewrite envelope recipients at
+transport time.
+
+
+31.3 Testing the rewriting rules that apply on input
+----------------------------------------------------
+
+Exim's input rewriting configuration appears in a part of the run time
+configuration file headed by "begin rewrite". It can be tested by the -brw
+command line option. This takes an address (which can be a full RFC 2822
+address) as its argument. The output is a list of how the address would be
+transformed by the rewriting rules for each of the different places it might
+appear in an incoming message, that is, for each different header and for the
+envelope sender and recipient fields. For example,
+
+exim -brw ph10@exim.workshop.example
+
+might produce the output
+
+sender: Philip.Hazel@exim.workshop.example
+from: Philip.Hazel@exim.workshop.example
+to: ph10@exim.workshop.example
+cc: ph10@exim.workshop.example
+bcc: ph10@exim.workshop.example
+reply-to: Philip.Hazel@exim.workshop.example
+env-from: Philip.Hazel@exim.workshop.example
+env-to: ph10@exim.workshop.example
+
+which shows that rewriting has been set up for that address when used in any of
+the source fields, but not when it appears as a recipient address. At the
+present time, there is no equivalent way of testing rewriting rules that are
+set for a particular transport.
+
+
+31.4 Rewriting rules
+--------------------
+
+The rewrite section of the configuration file consists of lines of rewriting
+rules in the form
+
+<source pattern>  <replacement>  <flags>
+
+Rewriting rules that are specified for the headers_rewrite generic transport
+option are given as a colon-separated list. Each item in the list takes the
+same form as a line in the main rewriting configuration (except that any colons
+must be doubled, of course).
+
+The formats of source patterns and replacement strings are described below.
+Each is terminated by white space, unless enclosed in double quotes, in which
+case normal quoting conventions apply inside the quotes. The flags are single
+characters which may appear in any order. Spaces and tabs between them are
+ignored.
+
+For each address that could potentially be rewritten, the rules are scanned in
+order, and replacements for the address from earlier rules can themselves be
+replaced by later rules (but see the "q" and "R" flags).
+
+The order in which addresses are rewritten is undefined, may change between
+releases, and must not be relied on, with one exception: when a message is
+received, the envelope sender is always rewritten first, before any header
+lines are rewritten. For example, the replacement string for a rewrite of an
+address in To: must not assume that the message's address in From: has (or has
+not) already been rewritten. However, a rewrite of From: may assume that the
+envelope sender has already been rewritten.
+
+The variables $local_part and $domain can be used in the replacement string to
+refer to the address that is being rewritten. Note that lookup-driven rewriting
+can be done by a rule of the form
+
+*@*   ${lookup ...
+
+where the lookup key uses $1 and $2 or $local_part and $domain to refer to the
+address that is being rewritten.
+
+
+31.5 Rewriting patterns
+-----------------------
+
+The source pattern in a rewriting rule is any item which may appear in an
+address list (see section 10.19). It is in fact processed as a single-item
+address list, which means that it is expanded before being tested against the
+address. As always, if you use a regular expression as a pattern, you must take
+care to escape dollar and backslash characters, or use the "\N" facility to
+suppress string expansion within the regular expression.
+
+Domains in patterns should be given in lower case. Local parts in patterns are
+case-sensitive. If you want to do case-insensitive matching of local parts, you
+can use a regular expression that starts with "^(?i)".
+
+After matching, the numerical variables $1, $2, etc. may be set, depending on
+the type of match which occurred. These can be used in the replacement string
+to insert portions of the incoming address. $0 always refers to the complete
+incoming address. When a regular expression is used, the numerical variables
+are set from its capturing subexpressions. For other types of pattern they are
+set as follows:
+
+  * If a local part or domain starts with an asterisk, the numerical variables
+    refer to the character strings matched by asterisks, with $1 associated
+    with the first asterisk, and $2 with the second, if present. For example,
+    if the pattern
+
+    *queen@*.fict.example
+
+    is matched against the address hearts-queen@wonderland.fict.example then
+
+    $0 = hearts-queen@wonderland.fict.example
+    $1 = hearts-
+    $2 = wonderland
+
+    Note that if the local part does not start with an asterisk, but the domain
+    does, it is $1 that contains the wild part of the domain.
+
+  * If the domain part of the pattern is a partial lookup, the wild and fixed
+    parts of the domain are placed in the next available numerical variables.
+    Suppose, for example, that the address foo@bar.baz.example is processed by
+    a rewriting rule of the form
+
+    *@partial-dbm;/some/dbm/file    <replacement string>
+
+    and the key in the file that matches the domain is "*.baz.example". Then
+
+    $1 = foo
+    $2 = bar
+    $3 = baz.example
+
+    If the address foo@baz.example is looked up, this matches the same wildcard
+    file entry, and in this case $2 is set to the empty string, but $3 is still
+    set to baz.example. If a non-wild key is matched in a partial lookup, $2 is
+    again set to the empty string and $3 is set to the whole domain. For
+    non-partial domain lookups, no numerical variables are set.
+
+
+31.6 Rewriting replacements
+---------------------------
+
+If the replacement string for a rule is a single asterisk, addresses that match
+the pattern and the flags are not rewritten, and no subsequent rewriting rules
+are scanned. For example,
+
+hatta@lookingglass.fict.example  *  f
+
+specifies that hatta@lookingglass.fict.example is never to be rewritten in 
+From: headers.
+
+If the replacement string is not a single asterisk, it is expanded, and must
+yield a fully qualified address. Within the expansion, the variables
+$local_part and $domain refer to the address that is being rewritten. Any
+letters they contain retain their original case - they are not lower cased. The
+numerical variables are set up according to the type of pattern that matched
+the address, as described above. If the expansion is forced to fail by the
+presence of "fail" in a conditional or lookup item, rewriting by the current
+rule is abandoned, but subsequent rules may take effect. Any other expansion
+failure causes the entire rewriting operation to be abandoned, and an entry
+written to the panic log.
+
+
+31.7 Rewriting flags
+--------------------
+
+There are three different kinds of flag that may appear on rewriting rules:
+
+  * Flags that specify which headers and envelope addresses to rewrite: E, F,
+    T, b, c, f, h, r, s, t.
+
+  * A flag that specifies rewriting at SMTP time: S.
+
+  * Flags that control the rewriting process: Q, q, R, w.
+
+For rules that are part of the headers_rewrite generic transport option, E, F,
+T, and S are not permitted.
+
+
+31.8 Flags specifying which headers and envelope addresses to rewrite
+---------------------------------------------------------------------
+
+If none of the following flag letters, nor the "S" flag (see section 31.9) are
+present, a main rewriting rule applies to all headers and to both the sender
+and recipient fields of the envelope, whereas a transport-time rewriting rule
+just applies to all headers. Otherwise, the rewriting rule is skipped unless
+the relevant addresses are being processed.
+
+E       rewrite all envelope fields
+F       rewrite the envelope From field
+T       rewrite the envelope To field
+b       rewrite the Bcc: header
+c       rewrite the Cc: header
+f       rewrite the From: header
+h       rewrite all headers
+r       rewrite the Reply-To: header
+s       rewrite the Sender: header
+t       rewrite the To: header
+
+"All headers" means all of the headers listed above that can be selected
+individually, plus their Resent- versions. It does not include other headers
+such as Subject: etc.
+
+You should be particularly careful about rewriting Sender: headers, and
+restrict this to special known cases in your own domains.
+
+
+31.9 The SMTP-time rewriting flag
+---------------------------------
+
+The rewrite flag "S" specifies a rewrite of incoming envelope addresses at SMTP
+time, as soon as an address is received in a MAIL or RCPT command, and before
+any other processing; even before syntax checking. The pattern is required to
+be a regular expression, and it is matched against the whole of the data for
+the command, including any surrounding angle brackets.
+
+This form of rewrite rule allows for the handling of addresses that are not
+compliant with RFCs 2821 and 2822 (for example, "bang paths" in batched SMTP
+input). Because the input is not required to be a syntactically valid address,
+the variables $local_part and $domain are not available during the expansion of
+the replacement string. The result of rewriting replaces the original address
+in the MAIL or RCPT command.
+
+
+31.10 Flags controlling the rewriting process
+---------------------------------------------
+
+There are four flags which control the way the rewriting process works. These
+take effect only when a rule is invoked, that is, when the address is of the
+correct type (matches the flags) and matches the pattern:
+
+  * If the "Q" flag is set on a rule, the rewritten address is permitted to be
+    an unqualified local part. It is qualified with qualify_recipient. In the
+    absence of "Q" the rewritten address must always include a domain.
+
+  * If the "q" flag is set on a rule, no further rewriting rules are
+    considered, even if no rewriting actually takes place because of a "fail"
+    in the expansion. The "q" flag is not effective if the address is of the
+    wrong type (does not match the flags) or does not match the pattern.
+
+  * The "R" flag causes a successful rewriting rule to be re-applied to the new
+    address, up to ten times. It can be combined with the "q" flag, to stop
+    rewriting once it fails to match (after at least one successful rewrite).
+
+  * When an address in a header is rewritten, the rewriting normally applies
+    only to the working part of the address, with any comments and RFC 2822
+    "phrase" left unchanged. For example, rewriting might change
+
+    From: Ford Prefect <fp42@restaurant.hitch.fict.example>
+
+    into
+
+    From: Ford Prefect <prefectf@hitch.fict.example>
+
+    Sometimes there is a need to replace the whole address item, and this can
+    be done by adding the flag letter "w" to a rule. If this is set on a rule
+    that causes an address in a header line to be rewritten, the entire address
+    is replaced, not just the working part. The replacement must be a complete
+    RFC 2822 address, including the angle brackets if necessary. If text
+    outside angle brackets contains a character whose value is greater than 126
+    or less than 32 (except for tab), the text is encoded according to RFC
+    2047. The character set is taken from headers_charset, which defaults to
+    ISO-8859-1.
+
+    When the "w" flag is set on a rule that causes an envelope address to be
+    rewritten, all but the working part of the replacement address is
+    discarded.
+
+
+31.11 Rewriting examples
+------------------------
+
+Here is an example of the two common rewriting paradigms:
+
+*@*.hitch.fict.example  $1@hitch.fict.example
+*@hitch.fict.example    ${lookup{$1}dbm{/etc/realnames}\
+                     {$value}fail}@hitch.fict.example bctfrF
+
+Note the use of "fail" in the lookup expansion in the second rule, forcing the
+string expansion to fail if the lookup does not succeed. In this context it has
+the effect of leaving the original address unchanged, but Exim goes on to
+consider subsequent rewriting rules, if any, because the "q" flag is not
+present in that rule. An alternative to "fail" would be to supply $1
+explicitly, which would cause the rewritten address to be the same as before,
+at the cost of a small bit of processing. Not supplying either of these is an
+error, since the rewritten address would then contain no local part.
+
+The first example above replaces the domain with a superior, more general
+domain. This may not be desirable for certain local parts. If the rule
+
+root@*.hitch.fict.example  *
+
+were inserted before the first rule, rewriting would be suppressed for the
+local part root at any domain ending in hitch.fict.example.
+
+Rewriting can be made conditional on a number of tests, by making use of ${if
+in the expansion item. For example, to apply a rewriting rule only to messages
+that originate outside the local host:
+
+*@*.hitch.fict.example  "${if !eq {$sender_host_address}{}\
+                         {$1@hitch.fict.example}fail}"
+
+The replacement string is quoted in this example because it contains white
+space.
+
+Exim does not handle addresses in the form of "bang paths". If it sees such an
+address it treats it as an unqualified local part which it qualifies with the
+local qualification domain (if the source of the message is local or if the
+remote host is permitted to send unqualified addresses). Rewriting can
+sometimes be used to handle simple bang paths with a fixed number of
+components. For example, the rule
+
+\N^([^!]+)!(.*)@your.domain.example$\N   $2@$1
+
+rewrites a two-component bang path host.name!user as the domain address 
+user@host.name. However, there is a security implication in using this as a
+global rewriting rule for envelope addresses. It can provide a backdoor method
+for using your system as a relay, because the incoming addresses appear to be
+local. If the bang path addresses are received via SMTP, it is safer to use the
+"S" flag to rewrite them as they are received, so that relay checking can be
+done on the rewritten addresses.
+
+
+
+===============================================================================
+32. RETRY CONFIGURATION
+
+The "retry" section of the runtime configuration file contains a list of retry
+rules that control how often Exim tries to deliver messages that cannot be
+delivered at the first attempt. If there are no retry rules (the section is
+empty or not present), there are no retries. In this situation, temporary
+errors are treated as permanent. The default configuration contains a single,
+general-purpose retry rule (see section 7.5). The -brt command line option can
+be used to test which retry rule will be used for a given address, domain and
+error.
+
+The most common cause of retries is temporary failure to deliver to a remote
+host because the host is down, or inaccessible because of a network problem.
+Exim's retry processing in this case is applied on a per-host (strictly, per IP
+address) basis, not on a per-message basis. Thus, if one message has recently
+been delayed, delivery of a new message to the same host is not immediately
+tried, but waits for the host's retry time to arrive. If the retry_defer log
+selector is set, the message "retry time not reached" is written to the main
+log whenever a delivery is skipped for this reason. Section 47.2 contains more
+details of the handling of errors during remote deliveries.
+
+Retry processing applies to routing as well as to delivering, except as covered
+in the next paragraph. The retry rules do not distinguish between these
+actions. It is not possible, for example, to specify different behaviour for
+failures to route the domain snark.fict.example and failures to deliver to the
+host snark.fict.example. I didn't think anyone would ever need this added
+complication, so did not implement it. However, although they share the same
+retry rule, the actual retry times for routing and transporting a given domain
+are maintained independently.
+
+When a delivery is not part of a queue run (typically an immediate delivery on
+receipt of a message), the routers are always run, and local deliveries are
+always attempted, even if retry times are set for them. This makes for better
+behaviour if one particular message is causing problems (for example, causing
+quota overflow, or provoking an error in a filter file). If such a delivery
+suffers a temporary failure, the retry data is updated as normal, and
+subsequent delivery attempts from queue runs occur only when the retry time for
+the local address is reached.
+
+
+32.1 Changing retry rules
+-------------------------
+
+If you change the retry rules in your configuration, you should consider
+whether or not to delete the retry data that is stored in Exim's spool area in
+files with names like db/retry. Deleting any of Exim's hints files is always
+safe; that is why they are called "hints".
+
+The hints retry data contains suggested retry times based on the previous
+rules. In the case of a long-running problem with a remote host, it might
+record the fact that the host has timed out. If your new rules increase the
+timeout time for such a host, you should definitely remove the old retry data
+and let Exim recreate it, based on the new rules. Otherwise Exim might bounce
+messages that it should now be retaining.
+
+
+32.2 Format of retry rules
+--------------------------
+
+Each retry rule occupies one line and consists of three or four parts,
+separated by white space: a pattern, an error name, an optional list of sender
+addresses, and a list of retry parameters. The pattern and sender lists must be
+enclosed in double quotes if they contain white space. The rules are searched
+in order until one is found where the pattern, error name, and sender list (if
+present) match the failing host or address, the error that occurred, and the
+message's sender, respectively.
+
+The pattern is any single item that may appear in an address list (see section
+10.19). It is in fact processed as a one-item address list, which means that it
+is expanded before being tested against the address that has been delayed. A
+negated address list item is permitted. Address list processing treats a plain
+domain name as if it were preceded by "*@", which makes it possible for many
+retry rules to start with just a domain. For example,
+
+lookingglass.fict.example        *  F,24h,30m;
+
+provides a rule for any address in the lookingglass.fict.example domain,
+whereas
+
+alice@lookingglass.fict.example  *  F,24h,30m;
+
+applies only to temporary failures involving the local part alice. In practice,
+almost all rules start with a domain name pattern without a local part.
+
+Warning: If you use a regular expression in a retry rule pattern, it must match
+a complete address, not just a domain, because that is how regular expressions
+work in address lists.
+
+^\Nxyz\d+\.abc\.example$\N        *  G,1h,10m,2     Wrong
+^\N[^@]+@xyz\d+\.abc\.example$\N  *  G,1h,10m,2     Right
+
+
+32.3 Choosing which retry rule to use for address errors
+--------------------------------------------------------
+
+When Exim is looking for a retry rule after a routing attempt has failed (for
+example, after a DNS timeout), each line in the retry configuration is tested
+against the complete address only if retry_use_local_part is set for the
+router. Otherwise, only the domain is used, except when matching against a
+regular expression, when the local part of the address is replaced with "*". A
+domain on its own can match a domain pattern, or a pattern that starts with
+"*@". By default, retry_use_local_part is true for routers where 
+check_local_user is true, and false for other routers.
+
+Similarly, when Exim is looking for a retry rule after a local delivery has
+failed (for example, after a mailbox full error), each line in the retry
+configuration is tested against the complete address only if 
+retry_use_local_part is set for the transport (it defaults true for all local
+transports).
+
+However, when Exim is looking for a retry rule after a remote delivery attempt
+suffers an address error (a 4xx SMTP response for a recipient address), the
+whole address is always used as the key when searching the retry rules. The
+rule that is found is used to create a retry time for the combination of the
+failing address and the message's sender. It is the combination of sender and
+recipient that is delayed in subsequent queue runs until its retry time is
+reached. You can delay the recipient without regard to the sender by setting 
+address_retry_include_sender false in the smtp transport but this can lead to
+problems with servers that regularly issue 4xx responses to RCPT commands.
+
+
+32.4 Choosing which retry rule to use for host and message errors
+-----------------------------------------------------------------
+
+For a temporary error that is not related to an individual address (for
+example, a connection timeout), each line in the retry configuration is checked
+twice. First, the name of the remote host is used as a domain name (preceded by
+"*@" when matching a regular expression). If this does not match the line, the
+domain from the email address is tried in a similar fashion. For example,
+suppose the MX records for a.b.c.example are
+
+a.b.c.example  MX  5  x.y.z.example
+               MX  6  p.q.r.example
+               MX  7  m.n.o.example
+
+and the retry rules are
+
+p.q.r.example    *      F,24h,30m;
+a.b.c.example    *      F,4d,45m;
+
+and a delivery to the host x.y.z.example suffers a connection failure. The
+first rule matches neither the host nor the domain, so Exim looks at the second
+rule. This does not match the host, but it does match the domain, so it is used
+to calculate the retry time for the host x.y.z.example. Meanwhile, Exim tries
+to deliver to p.q.r.example. If this also suffers a host error, the first retry
+rule is used, because it matches the host.
+
+In other words, temporary failures to deliver to host p.q.r.example use the
+first rule to determine retry times, but for all the other hosts for the domain
+a.b.c.example, the second rule is used. The second rule is also used if routing
+to a.b.c.example suffers a temporary failure.
+
+Note: The host name is used when matching the patterns, not its IP address.
+However, if a message is routed directly to an IP address without the use of a
+host name, for example, if a manualroute router contains a setting such as:
+
+route_list = *.a.example  192.168.34.23
+
+then the "host name" that is used when searching for a retry rule is the
+textual form of the IP address.
+
+
+32.5 Retry rules for specific errors
+------------------------------------
+
+The second field in a retry rule is the name of a particular error, or an
+asterisk, which matches any error. The errors that can be tested for are:
+
+auth_failed
+
+    Authentication failed when trying to send to a host in the 
+    hosts_require_auth list in an smtp transport.
+
+data_4xx
+
+    A 4xx error was received for an outgoing DATA command, either immediately
+    after the command, or after sending the message's data.
+
+mail_4xx
+
+    A 4xx error was received for an outgoing MAIL command.
+
+rcpt_4xx
+
+    A 4xx error was received for an outgoing RCPT command.
+
+For the three 4xx errors, either the first or both of the x's can be given as
+specific digits, for example: "mail_45x" or "rcpt_436". For example, to
+recognize 452 errors given to RCPT commands for addresses in a certain domain,
+and have retries every ten minutes with a one-hour timeout, you could set up a
+retry rule of this form:
+
+the.domain.name  rcpt_452   F,1h,10m
+
+These errors apply to both outgoing SMTP (the smtp transport) and outgoing LMTP
+(either the lmtp transport, or the smtp transport in LMTP mode).
+
+lost_connection
+
+    A server unexpectedly closed the SMTP connection. There may, of course,
+    legitimate reasons for this (host died, network died), but if it repeats a
+    lot for the same host, it indicates something odd.
+
+refused_MX
+
+    A connection to a host obtained from an MX record was refused.
+
+refused_A
+
+    A connection to a host not obtained from an MX record was refused.
+
+refused
+
+    A connection was refused.
+
+timeout_connect_MX
+
+    A connection attempt to a host obtained from an MX record timed out.
+
+timeout_connect_A
+
+    A connection attempt to a host not obtained from an MX record timed out.
+
+timeout_connect
+
+    A connection attempt timed out.
+
+timeout_MX
+
+    There was a timeout while connecting or during an SMTP session with a host
+    obtained from an MX record.
+
+timeout_A
+
+    There was a timeout while connecting or during an SMTP session with a host
+    not obtained from an MX record.
+
+timeout
+
+    There was a timeout while connecting or during an SMTP session.
+
+tls_required
+
+    The server was required to use TLS (it matched hosts_require_tls in the 
+    smtp transport), but either did not offer TLS, or it responded with 4xx to
+    STARTTLS, or there was a problem setting up the TLS connection.
+
+quota
+
+    A mailbox quota was exceeded in a local delivery by the appendfile
+    transport.
+
+quota_<time>
+
+    A mailbox quota was exceeded in a local delivery by the appendfile
+    transport, and the mailbox has not been accessed for <time>. For example, 
+    quota_4d applies to a quota error when the mailbox has not been accessed
+    for four days.
+
+The idea of quota_<time> is to make it possible to have shorter timeouts when
+the mailbox is full and is not being read by its owner. Ideally, it should be
+based on the last time that the user accessed the mailbox. However, it is not
+always possible to determine this. Exim uses the following heuristic rules:
+
+  * If the mailbox is a single file, the time of last access (the "atime") is
+    used. As no new messages are being delivered (because the mailbox is over
+    quota), Exim does not access the file, so this is the time of last user
+    access.
+
+  * For a maildir delivery, the time of last modification of the new
+    subdirectory is used. As the mailbox is over quota, no new files are
+    created in the new subdirectory, because no new messages are being
+    delivered. Any change to the new subdirectory is therefore assumed to be
+    the result of an MUA moving a new message to the cur directory when it is
+    first read. The time that is used is therefore the last time that the user
+    read a new message.
+
+  * For other kinds of multi-file mailbox, the time of last access cannot be
+    obtained, so a retry rule that uses this type of error field is never
+    matched.
+
+The quota errors apply both to system-enforced quotas and to Exim's own quota
+mechanism in the appendfile transport. The quota error also applies when a
+local delivery is deferred because a partition is full (the ENOSPC error).
+
+
+32.6 Retry rules for specified senders
+--------------------------------------
+
+You can specify retry rules that apply only when the failing message has a
+specific sender. In particular, this can be used to define retry rules that
+apply only to bounce messages. The third item in a retry rule can be of this
+form:
+
+senders=<address list>
+
+The retry timings themselves are then the fourth item. For example:
+
+*   rcpt_4xx   senders=:   F,1h,30m
+
+matches recipient 4xx errors for bounce messages sent to any address at any
+host. If the address list contains white space, it must be enclosed in quotes.
+For example:
+
+a.domain  rcpt_452  senders="xb.dom : yc.dom"  G,8h,10m,1.5
+
+Warning: This facility can be unhelpful if it is used for host errors (which do
+not depend on the recipient). The reason is that the sender is used only to
+match the retry rule. Once the rule has been found for a host error, its
+contents are used to set a retry time for the host, and this will apply to all
+messages, not just those with specific senders.
+
+When testing retry rules using -brt, you can supply a sender using the -f
+command line option, like this:
+
+exim -f "" -brt user@dom.ain
+
+If you do not set -f with -brt, a retry rule that contains a senders list is
+never matched.
+
+
+32.7 Retry parameters
+---------------------
+
+The third (or fourth, if a senders list is present) field in a retry rule is a
+sequence of retry parameter sets, separated by semicolons. Each set consists of
+
+<letter>,<cutoff time>,<arguments>
+
+The letter identifies the algorithm for computing a new retry time; the cutoff
+time is the time beyond which this algorithm no longer applies, and the
+arguments vary the algorithm's action. The cutoff time is measured from the
+time that the first failure for the domain (combined with the local part if
+relevant) was detected, not from the time the message was received.
+
+The available algorithms are:
+
+  * F: retry at fixed intervals. There is a single time parameter specifying
+    the interval.
+
+  * G: retry at geometrically increasing intervals. The first argument
+    specifies a starting value for the interval, and the second a multiplier,
+    which is used to increase the size of the interval at each retry.
+
+  * H: retry at randomized intervals. The arguments are as for G. For each
+    retry, the previous interval is multiplied by the factor in order to get a
+    maximum for the next interval. The minimum interval is the first argument
+    of the parameter, and an actual interval is chosen randomly between them.
+    Such a rule has been found to be helpful in cluster configurations when all
+    the members of the cluster restart at once, and may therefore synchronize
+    their queue processing times.
+
+When computing the next retry time, the algorithm definitions are scanned in
+order until one whose cutoff time has not yet passed is reached. This is then
+used to compute a new retry time that is later than the current time. In the
+case of fixed interval retries, this simply means adding the interval to the
+current time. For geometrically increasing intervals, retry intervals are
+computed from the rule's parameters until one that is greater than the previous
+interval is found. The main configuration variable retry_interval_max limits
+the maximum interval between retries. It cannot be set greater than "24h",
+which is its default value.
+
+A single remote domain may have a number of hosts associated with it, and each
+host may have more than one IP address. Retry algorithms are selected on the
+basis of the domain name, but are applied to each IP address independently. If,
+for example, a host has two IP addresses and one is unusable, Exim will
+generate retry times for it and will not try to use it until its next retry
+time comes. Thus the good IP address is likely to be tried first most of the
+time.
+
+Retry times are hints rather than promises. Exim does not make any attempt to
+run deliveries exactly at the computed times. Instead, a queue runner process
+starts delivery processes for delayed messages periodically, and these attempt
+new deliveries only for those addresses that have passed their next retry time.
+If a new message arrives for a deferred address, an immediate delivery attempt
+occurs only if the address has passed its retry time. In the absence of new
+messages, the minimum time between retries is the interval between queue runner
+processes. There is not much point in setting retry times of five minutes if
+your queue runners happen only once an hour, unless there are a significant
+number of incoming messages (which might be the case on a system that is
+sending everything to a smart host, for example).
+
+The data in the retry hints database can be inspected by using the exim_dumpdb
+or exim_fixdb utility programs (see chapter 52). The latter utility can also be
+used to change the data. The exinext utility script can be used to find out
+what the next retry times are for the hosts associated with a particular mail
+domain, and also for local deliveries that have been deferred.
+
+
+32.8 Retry rule examples
+------------------------
+
+Here are some example retry rules:
+
+alice@wonderland.fict.example quota_5d  F,7d,3h
+wonderland.fict.example       quota_5d
+wonderland.fict.example       *         F,1h,15m; G,2d,1h,2;
+lookingglass.fict.example     *         F,24h,30m;
+*                 refused_A   F,2h,20m;
+*                 *           F,2h,15m; G,16h,1h,1.5; F,5d,8h
+
+The first rule sets up special handling for mail to 
+alice@wonderland.fict.example when there is an over-quota error and the mailbox
+has not been read for at least 5 days. Retries continue every three hours for 7
+days. The second rule handles over-quota errors for all other local parts at 
+wonderland.fict.example; the absence of a local part has the same effect as
+supplying "*@". As no retry algorithms are supplied, messages that fail are
+bounced immediately if the mailbox has not been read for at least 5 days.
+
+The third rule handles all other errors at wonderland.fict.example; retries
+happen every 15 minutes for an hour, then with geometrically increasing
+intervals until two days have passed since a delivery first failed. After the
+first hour there is a delay of one hour, then two hours, then four hours, and
+so on (this is a rather extreme example).
+
+The fourth rule controls retries for the domain lookingglass.fict.example. They
+happen every 30 minutes for 24 hours only. The remaining two rules handle all
+other domains, with special action for connection refusal from hosts that were
+not obtained from an MX record.
+
+The final rule in a retry configuration should always have asterisks in the
+first two fields so as to provide a general catch-all for any addresses that do
+not have their own special handling. This example tries every 15 minutes for 2
+hours, then with intervals starting at one hour and increasing by a factor of
+1.5 up to 16 hours, then every 8 hours up to 5 days.
+
+
+32.9 Timeout of retry data
+--------------------------
+
+Exim timestamps the data that it writes to its retry hints database. When it
+consults the data during a delivery it ignores any that is older than the value
+set in retry_data_expire (default 7 days). If, for example, a host hasn't been
+tried for 7 days, Exim will try to deliver to it immediately a message arrives,
+and if that fails, it will calculate a retry time as if it were failing for the
+first time.
+
+This improves the behaviour for messages routed to rarely-used hosts such as MX
+backups. If such a host was down at one time, and happens to be down again when
+Exim tries a month later, using the old retry data would imply that it had been
+down all the time, which is not a justified assumption.
+
+If a host really is permanently dead, this behaviour causes a burst of retries
+every now and again, but only if messages routed to it are rare. If there is a
+message at least once every 7 days the retry data never expires.
+
+
+32.10 Long-term failures
+------------------------
+
+Special processing happens when an email address has been failing for so long
+that the cutoff time for the last algorithm is reached. For example, using the
+default retry rule:
+
+* * F,2h,15m; G,16h,1h,1.5; F,4d,6h
+
+the cutoff time is four days. Reaching the retry cutoff is independent of how
+long any specific message has been failing; it is the length of continuous
+failure for the recipient address that counts.
+
+When the cutoff time is reached for a local delivery, or for all the IP
+addresses associated with a remote delivery, a subsequent delivery failure
+causes Exim to give up on the address, and a bounce message is generated. In
+order to cater for new messages that use the failing address, a next retry time
+is still computed from the final algorithm, and is used as follows:
+
+For local deliveries, one delivery attempt is always made for any subsequent
+messages. If this delivery fails, the address fails immediately. The
+post-cutoff retry time is not used.
+
+If the delivery is remote, there are two possibilities, controlled by the 
+delay_after_cutoff option of the smtp transport. The option is true by default.
+Until the post-cutoff retry time for one of the IP addresses is reached, the
+failing email address is bounced immediately, without a delivery attempt taking
+place. After that time, one new delivery attempt is made to those IP addresses
+that are past their retry times, and if that still fails, the address is
+bounced and new retry times are computed.
+
+In other words, when all the hosts for a given email address have been failing
+for a long time, Exim bounces rather then defers until one of the hosts' retry
+times is reached. Then it tries once, and bounces if that attempt fails. This
+behaviour ensures that few resources are wasted in repeatedly trying to deliver
+to a broken destination, but if the host does recover, Exim will eventually
+notice.
+
+If delay_after_cutoff is set false, Exim behaves differently. If all IP
+addresses are past their final cutoff time, Exim tries to deliver to those IP
+addresses that have not been tried since the message arrived. If there are no
+suitable IP addresses, or if they all fail, the address is bounced. In other
+words, it does not delay when a new message arrives, but tries the expired
+addresses immediately, unless they have been tried since the message arrived.
+If there is a continuous stream of messages for the failing domains, setting 
+delay_after_cutoff false means that there will be many more attempts to deliver
+to permanently failing IP addresses than when delay_after_cutoff is true.
+
+
+32.11 Deliveries that work intermittently
+-----------------------------------------
+
+Some additional logic is needed to cope with cases where a host is
+intermittently available, or when a message has some attribute that prevents
+its delivery when others to the same address get through. In this situation,
+because some messages are successfully delivered, the "retry clock" for the
+host or address keeps getting reset by the successful deliveries, and so
+failing messages remain on the queue for ever because the cutoff time is never
+reached.
+
+Two exceptional actions are applied to prevent this happening. The first
+applies to errors that are related to a message rather than a remote host.
+Section 47.2 has a discussion of the different kinds of error; examples of
+message-related errors are 4xx responses to MAIL or DATA commands, and quota
+failures. For this type of error, if a message's arrival time is earlier than
+the "first failed" time for the error, the earlier time is used when scanning
+the retry rules to decide when to try next and when to time out the address.
+
+The exceptional second action applies in all cases. If a message has been on
+the queue for longer than the cutoff time of any applicable retry rule for a
+given address, a delivery is attempted for that address, even if it is not yet
+time, and if this delivery fails, the address is timed out. A new retry time is
+not computed in this case, so that other messages for the same address are
+considered immediately.
+
+
+
+===============================================================================
+33. SMTP AUTHENTICATION
+
+The "authenticators" section of Exim's run time configuration is concerned with
+SMTP authentication. This facility is an extension to the SMTP protocol,
+described in RFC 2554, which allows a client SMTP host to authenticate itself
+to a server. This is a common way for a server to recognize clients that are
+permitted to use it as a relay. SMTP authentication is not of relevance to the
+transfer of mail between servers that have no managerial connection with each
+other.
+
+Very briefly, the way SMTP authentication works is as follows:
+
+  * The server advertises a number of authentication mechanisms in response to
+    the client's EHLO command.
+
+  * The client issues an AUTH command, naming a specific mechanism. The command
+    may, optionally, contain some authentication data.
+
+  * The server may issue one or more challenges, to which the client must send
+    appropriate responses. In simple authentication mechanisms, the challenges
+    are just prompts for user names and passwords. The server does not have to
+    issue any challenges - in some mechanisms the relevant data may all be
+    transmitted with the AUTH command.
+
+  * The server either accepts or denies authentication.
+
+  * If authentication succeeds, the client may optionally make use of the AUTH
+    option on the MAIL command to pass an authenticated sender in subsequent
+    mail transactions. Authentication lasts for the remainder of the SMTP
+    connection.
+
+  * If authentication fails, the client may give up, or it may try a different
+    authentication mechanism, or it may try transferring mail over the
+    unauthenticated connection.
+
+If you are setting up a client, and want to know which authentication
+mechanisms the server supports, you can use Telnet to connect to port 25 (the
+SMTP port) on the server, and issue an EHLO command. The response to this
+includes the list of supported mechanisms. For example:
+
+$ telnet server.example 25
+Trying 192.168.34.25...
+Connected to server.example.
+Escape character is '^]'.
+220 server.example ESMTP Exim 4.20 ...
+ehlo client.example
+250-server.example Hello client.example [10.8.4.5]
+250-SIZE 52428800
+250-PIPELINING
+250-AUTH PLAIN
+250 HELP
+
+The second-last line of this example output shows that the server supports
+authentication using the PLAIN mechanism. In Exim, the different authentication
+mechanisms are configured by specifying authenticator drivers. Like the routers
+and transports, which authenticators are included in the binary is controlled
+by build-time definitions. The following are currently available, included by
+setting
+
+AUTH_CRAM_MD5=yes
+AUTH_CYRUS_SASL=yes
+AUTH_DOVECOT=yes
+AUTH_GSASL=yes
+AUTH_HEIMDAL_GSSAPI=yes
+AUTH_PLAINTEXT=yes
+AUTH_SPA=yes
+
+in Local/Makefile, respectively. The first of these supports the CRAM-MD5
+authentication mechanism (RFC 2195), and the second provides an interface to
+the Cyrus SASL authentication library. The third is an interface to Dovecot's
+authentication system, delegating the work via a socket interface. The fourth
+provides an interface to the GNU SASL authentication library, which provides
+mechanisms but typically not data sources. The fifth provides direct access to
+Heimdal GSSAPI, geared for Kerberos, but supporting setting a server keytab.
+The sixth can be configured to support the PLAIN authentication mechanism (RFC
+2595) or the LOGIN mechanism, which is not formally documented, but used by
+several MUAs. The seventh authenticator supports Microsoft's Secure Password
+Authentication mechanism.
+
+The authenticators are configured using the same syntax as other drivers (see
+section 6.22). If no authenticators are required, no authentication section
+need be present in the configuration file. Each authenticator can in principle
+have both server and client functions. When Exim is receiving SMTP mail, it is
+acting as a server; when it is sending out messages over SMTP, it is acting as
+a client. Authenticator configuration options are provided for use in both
+these circumstances.
+
+To make it clear which options apply to which situation, the prefixes server_
+and client_ are used on option names that are specific to either the server or
+the client function, respectively. Server and client functions are disabled if
+none of their options are set. If an authenticator is to be used for both
+server and client functions, a single definition, using both sets of options,
+is required. For example:
+
+cram:
+  driver = cram_md5
+  public_name = CRAM-MD5
+  server_secret = ${if eq{$auth1}{ph10}{secret1}fail}
+  client_name = ph10
+  client_secret = secret2
+
+The server_ option is used when Exim is acting as a server, and the client_
+options when it is acting as a client.
+
+Descriptions of the individual authenticators are given in subsequent chapters.
+The remainder of this chapter covers the generic options for the
+authenticators, followed by general discussion of the way authentication works
+in Exim.
+
+Beware: the meaning of $auth1, $auth2, ... varies on a per-driver and
+per-mechanism basis. Please read carefully to determine which variables hold
+account labels such as usercodes and which hold passwords or other
+authenticating data.
+
+Note that some mechanisms support two different identifiers for accounts: the 
+authentication id and the authorization id. The contractions authn and authz
+are commonly encountered. The American spelling is standard here. Conceptually,
+authentication data such as passwords are tied to the identifier used to
+authenticate; servers may have rules to permit one user to act as a second
+user, so that after login the session is treated as though that second user had
+logged in. That second user is the authorization id. A robust configuration
+might confirm that the authz field is empty or matches the authn field. Often
+this is just ignored. The authn can be considered as verified data, the authz
+as an unverified request which the server might choose to honour.
+
+A realm is a text string, typically a domain name, presented by a server to a
+client to help it select an account and credentials to use. In some mechanisms,
+the client and server provably agree on the realm, but clients typically can
+not treat the realm as secure data to be blindly trusted.
+
+
+33.1 Generic options for authenticators
+---------------------------------------
+
++----------------+-------------------+-------------+--------------+
+|client_condition|Use: authenticators|Type: string*|Default: unset|
++----------------+-------------------+-------------+--------------+
+
+When Exim is authenticating as a client, it skips any authenticator whose 
+client_condition expansion yields "0", "no", or "false". This can be used, for
+example, to skip plain text authenticators when the connection is not encrypted
+by a setting such as:
+
+client_condition = ${if !eq{$tls_out_cipher}{}}
+
++-------------+-------------------+-------------+--------------+
+|client_set_id|Use: authenticators|Type: string*|Default: unset|
++-------------+-------------------+-------------+--------------+
+
+When client authentication succeeds, this condition is expanded; the result is
+used in the log lines for outbound messasges. Typically it will be the user
+name used for authentication.
+
++------+-------------------+------------+--------------+
+|driver|Use: authenticators|Type: string|Default: unset|
++------+-------------------+------------+--------------+
+
+This option must always be set. It specifies which of the available
+authenticators is to be used.
+
++-----------+-------------------+------------+--------------+
+|public_name|Use: authenticators|Type: string|Default: unset|
++-----------+-------------------+------------+--------------+
+
+This option specifies the name of the authentication mechanism that the driver
+implements, and by which it is known to the outside world. These names should
+contain only upper case letters, digits, underscores, and hyphens (RFC 2222),
+but Exim in fact matches them caselessly. If public_name is not set, it
+defaults to the driver's instance name.
+
++--------------------------+-------------------+-------------+--------------+
+|server_advertise_condition|Use: authenticators|Type: string*|Default: unset|
++--------------------------+-------------------+-------------+--------------+
+
+When a server is about to advertise an authentication mechanism, the condition
+is expanded. If it yields the empty string, "0", "no", or "false", the
+mechanism is not advertised. If the expansion fails, the mechanism is not
+advertised. If the failure was not forced, and was not caused by a lookup
+defer, the incident is logged. See section 33.3 below for further discussion.
+
++----------------+-------------------+-------------+--------------+
+|server_condition|Use: authenticators|Type: string*|Default: unset|
++----------------+-------------------+-------------+--------------+
+
+This option must be set for a plaintext server authenticator, where it is used
+directly to control authentication. See section 34.2 for details.
+
+For the gsasl authenticator, this option is required for various mechanisms;
+see chapter 38 for details.
+
+For the other authenticators, server_condition can be used as an additional
+authentication or authorization mechanism that is applied after the other
+authenticator conditions succeed. If it is set, it is expanded when the
+authenticator would otherwise return a success code. If the expansion is forced
+to fail, authentication fails. Any other expansion failure causes a temporary
+error code to be returned. If the result of a successful expansion is an empty
+string, "0", "no", or "false", authentication fails. If the result of the
+expansion is "1", "yes", or "true", authentication succeeds. For any other
+result, a temporary error code is returned, with the expanded string as the
+error text.
+
++------------------+-------------------+-------------+--------------+
+|server_debug_print|Use: authenticators|Type: string*|Default: unset|
++------------------+-------------------+-------------+--------------+
+
+If this option is set and authentication debugging is enabled (see the -d
+command line option), the string is expanded and included in the debugging
+output when the authenticator is run as a server. This can help with checking
+out the values of variables. If expansion of the string fails, the error
+message is written to the debugging output, and Exim carries on processing.
+
++-------------+-------------------+-------------+--------------+
+|server_set_id|Use: authenticators|Type: string*|Default: unset|
++-------------+-------------------+-------------+--------------+
+
+When an Exim server successfully authenticates a client, this string is
+expanded using data from the authentication, and preserved for any incoming
+messages in the variable $authenticated_id. It is also included in the log
+lines for incoming messages. For example, a user/password authenticator
+configuration might preserve the user name that was used to authenticate, and
+refer to it subsequently during delivery of the message. If expansion fails,
+the option is ignored.
+
++--------------------------+-------------------+-------------+--------------+
+|server_mail_auth_condition|Use: authenticators|Type: string*|Default: unset|
++--------------------------+-------------------+-------------+--------------+
+
+This option allows a server to discard authenticated sender addresses supplied
+as part of MAIL commands in SMTP connections that are authenticated by the
+driver on which server_mail_auth_condition is set. The option is not used as
+part of the authentication process; instead its (unexpanded) value is
+remembered for later use. How it is used is described in the following section.
+
+
+33.2 The AUTH parameter on MAIL commands
+----------------------------------------
+
+When a client supplied an AUTH= item on a MAIL command, Exim applies the
+following checks before accepting it as the authenticated sender of the
+message:
+
+  * If the connection is not using extended SMTP (that is, HELO was used rather
+    than EHLO), the use of AUTH= is a syntax error.
+
+  * If the value of the AUTH= parameter is "<>", it is ignored.
+
+  * If acl_smtp_mailauth is defined, the ACL it specifies is run. While it is
+    running, the value of $authenticated_sender is set to the value obtained
+    from the AUTH= parameter. If the ACL does not yield "accept", the value of
+    $authenticated_sender is deleted. The acl_smtp_mailauth ACL may not return
+    "drop" or "discard". If it defers, a temporary error code (451) is given
+    for the MAIL command.
+
+  * If acl_smtp_mailauth is not defined, the value of the AUTH= parameter is
+    accepted and placed in $authenticated_sender only if the client has
+    authenticated.
+
+  * If the AUTH= value was accepted by either of the two previous rules, and
+    the client has authenticated, and the authenticator has a setting for the 
+    server_mail_auth_condition, the condition is checked at this point. The
+    valued that was saved from the authenticator is expanded. If the expansion
+    fails, or yields an empty string, "0", "no", or "false", the value of
+    $authenticated_sender is deleted. If the expansion yields any other value,
+    the value of $authenticated_sender is retained and passed on with the
+    message.
+
+When $authenticated_sender is set for a message, it is passed on to other hosts
+to which Exim authenticates as a client. Do not confuse this value with
+$authenticated_id, which is a string obtained from the authentication process,
+and which is not usually a complete email address.
+
+Whenever an AUTH= value is ignored, the incident is logged. The ACL for MAIL,
+if defined, is run after AUTH= is accepted or ignored. It can therefore make
+use of $authenticated_sender. The converse is not true: the value of
+$sender_address is not yet set up when the acl_smtp_mailauth ACL is run.
+
+
+33.3 Authentication on an Exim server
+-------------------------------------
+
+When Exim receives an EHLO command, it advertises the public names of those
+authenticators that are configured as servers, subject to the following
+conditions:
+
+  * The client host must match auth_advertise_hosts (default *).
+
+  * It the server_advertise_condition option is set, its expansion must not
+    yield the empty string, "0", "no", or "false".
+
+The order in which the authenticators are defined controls the order in which
+the mechanisms are advertised.
+
+Some mail clients (for example, some versions of Netscape) require the user to
+provide a name and password for authentication whenever AUTH is advertised,
+even though authentication may not in fact be needed (for example, Exim may be
+set up to allow unconditional relaying from the client by an IP address check).
+You can make such clients more friendly by not advertising AUTH to them. For
+example, if clients on the 10.9.8.0/24 network are permitted (by the ACL that
+runs for RCPT) to relay without authentication, you should set
+
+auth_advertise_hosts = ! 10.9.8.0/24
+
+so that no authentication mechanisms are advertised to them.
+
+The server_advertise_condition controls the advertisement of individual
+authentication mechanisms. For example, it can be used to restrict the
+advertisement of a particular mechanism to encrypted connections, by a setting
+such as:
+
+server_advertise_condition = ${if eq{$tls_in_cipher}{}{no}{yes}}
+
+If the session is encrypted, $tls_in_cipher is not empty, and so the expansion
+yields "yes", which allows the advertisement to happen.
+
+When an Exim server receives an AUTH command from a client, it rejects it
+immediately if AUTH was not advertised in response to an earlier EHLO command.
+This is the case if
+
+  * The client host does not match auth_advertise_hosts; or
+
+  * No authenticators are configured with server options; or
+
+  * Expansion of server_advertise_condition blocked the advertising of all the
+    server authenticators.
+
+Otherwise, Exim runs the ACL specified by acl_smtp_auth in order to decide
+whether to accept the command. If acl_smtp_auth is not set, AUTH is accepted
+from any client host.
+
+If AUTH is not rejected by the ACL, Exim searches its configuration for a
+server authentication mechanism that was advertised in response to EHLO and
+that matches the one named in the AUTH command. If it finds one, it runs the
+appropriate authentication protocol, and authentication either succeeds or
+fails. If there is no matching advertised mechanism, the AUTH command is
+rejected with a 504 error.
+
+When a message is received from an authenticated host, the value of
+$received_protocol is set to "esmtpa" or "esmtpsa" instead of "esmtp" or
+"esmtps", and $sender_host_authenticated contains the name (not the public
+name) of the authenticator driver that successfully authenticated the client
+from which the message was received. This variable is empty if there was no
+successful authentication.
+
+
+33.4 Testing server authentication
+----------------------------------
+
+Exim's -bh option can be useful for testing server authentication
+configurations. The data for the AUTH command has to be sent using base64
+encoding. A quick way to produce such data for testing is the following Perl
+script:
+
+use MIME::Base64;
+printf ("%s", encode_base64(eval "\"$ARGV[0]\""));
+
+This interprets its argument as a Perl string, and then encodes it. The
+interpretation as a Perl string allows binary zeros, which are required for
+some kinds of authentication, to be included in the data. For example, a
+command line to run this script on such data might be
+
+encode '\0user\0password'
+
+Note the use of single quotes to prevent the shell interpreting the
+backslashes, so that they can be interpreted by Perl to specify characters
+whose code value is zero.
+
+Warning 1: If either of the user or password strings starts with an octal
+digit, you must use three zeros instead of one after the leading backslash. If
+you do not, the octal digit that starts your string will be incorrectly
+interpreted as part of the code for the first character.
+
+Warning 2: If there are characters in the strings that Perl interprets
+specially, you must use a Perl escape to prevent them being misinterpreted. For
+example, a command such as
+
+encode '\0user@domain.com\0pas$$word'
+
+gives an incorrect answer because of the unescaped "@" and "$" characters.
+
+If you have the mimencode command installed, another way to do produce
+base64-encoded strings is to run the command
+
+echo -e -n `\0user\0password' | mimencode
+
+The -e option of echo enables the interpretation of backslash escapes in the
+argument, and the -n option specifies no newline at the end of its output.
+However, not all versions of echo recognize these options, so you should check
+your version before relying on this suggestion.
+
+
+33.5 Authentication by an Exim client
+-------------------------------------
+
+The smtp transport has two options called hosts_require_auth and hosts_try_auth
+. When the smtp transport connects to a server that announces support for
+authentication, and the host matches an entry in either of these options, Exim
+(as a client) tries to authenticate as follows:
+
+  * For each authenticator that is configured as a client, in the order in
+    which they are defined in the configuration, it searches the authentication
+    mechanisms announced by the server for one whose name matches the public
+    name of the authenticator.
+
+  * When it finds one that matches, it runs the authenticator's client code.
+    The variables $host and $host_address are available for any string
+    expansions that the client might do. They are set to the server's name and
+    IP address. If any expansion is forced to fail, the authentication attempt
+    is abandoned, and Exim moves on to the next authenticator. Otherwise an
+    expansion failure causes delivery to be deferred.
+
+  * If the result of the authentication attempt is a temporary error or a
+    timeout, Exim abandons trying to send the message to the host for the
+    moment. It will try again later. If there are any backup hosts available,
+    they are tried in the usual way.
+
+  * If the response to authentication is a permanent error (5xx code), Exim
+    carries on searching the list of authenticators and tries another one if
+    possible. If all authentication attempts give permanent errors, or if there
+    are no attempts because no mechanisms match (or option expansions force
+    failure), what happens depends on whether the host matches 
+    hosts_require_auth or hosts_try_auth. In the first case, a temporary error
+    is generated, and delivery is deferred. The error can be detected in the
+    retry rules, and thereby turned into a permanent error if you wish. In the
+    second case, Exim tries to deliver the message unauthenticated.
+
+When Exim has authenticated itself to a remote server, it adds the AUTH
+parameter to the MAIL commands it sends, if it has an authenticated sender for
+the message. If the message came from a remote host, the authenticated sender
+is the one that was receiving on an incoming MAIL command, provided that the
+incoming connection was authenticated and the server_mail_auth condition
+allowed the authenticated sender to be retained. If a local process calls Exim
+to send a message, the sender address that is built from the login name and 
+qualify_domain is treated as authenticated. However, if the 
+authenticated_sender option is set on the smtp transport, it overrides the
+authenticated sender that was received with the message.
+
+
+
+===============================================================================
+34. THE PLAINTEXT AUTHENTICATOR
+
+The plaintext authenticator can be configured to support the PLAIN and LOGIN
+authentication mechanisms, both of which transfer authentication data as plain
+(unencrypted) text (though base64 encoded). The use of plain text is a security
+risk; you are strongly advised to insist on the use of SMTP encryption (see
+chapter 41) if you use the PLAIN or LOGIN mechanisms. If you do use unencrypted
+plain text, you should not use the same passwords for SMTP connections as you
+do for login accounts.
+
+
+34.1 Plaintext options
+----------------------
+
+When configured as a server, plaintext uses the following options:
+
++----------------+-------------------+-------------+--------------+
+|server_condition|Use: authenticators|Type: string*|Default: unset|
++----------------+-------------------+-------------+--------------+
+
+This is actually a global authentication option, but it must be set in order to
+configure the plaintext driver as a server. Its use is described below.
+
++--------------+--------------+-------------+--------------+
+|server_prompts|Use: plaintext|Type: string*|Default: unset|
++--------------+--------------+-------------+--------------+
+
+The contents of this option, after expansion, must be a colon-separated list of
+prompt strings. If expansion fails, a temporary authentication rejection is
+given.
+
+
+34.2 Using plaintext in a server
+--------------------------------
+
+When running as a server, plaintext performs the authentication test by
+expanding a string. The data sent by the client with the AUTH command, or in
+response to subsequent prompts, is base64 encoded, and so may contain any byte
+values when decoded. If any data is supplied with the command, it is treated as
+a list of strings, separated by NULs (binary zeros), the first three of which
+are placed in the expansion variables $auth1, $auth2, and $auth3 (neither LOGIN
+nor PLAIN uses more than three strings).
+
+For compatibility with previous releases of Exim, the values are also placed in
+the expansion variables $1, $2, and $3. However, the use of these variables for
+this purpose is now deprecated, as it can lead to confusion in string
+expansions that also use them for other things.
+
+If there are more strings in server_prompts than the number of strings supplied
+with the AUTH command, the remaining prompts are used to obtain more data. Each
+response from the client may be a list of NUL-separated strings.
+
+Once a sufficient number of data strings have been received, server_condition
+is expanded. If the expansion is forced to fail, authentication fails. Any
+other expansion failure causes a temporary error code to be returned. If the
+result of a successful expansion is an empty string, "0", "no", or "false",
+authentication fails. If the result of the expansion is "1", "yes", or "true",
+authentication succeeds and the generic server_set_id option is expanded and
+saved in $authenticated_id. For any other result, a temporary error code is
+returned, with the expanded string as the error text
+
+Warning: If you use a lookup in the expansion to find the user's password, be
+sure to make the authentication fail if the user is unknown. There are good and
+bad examples at the end of the next section.
+
+
+34.3 The PLAIN authentication mechanism
+---------------------------------------
+
+The PLAIN authentication mechanism (RFC 2595) specifies that three strings be
+sent as one item of data (that is, one combined string containing two NUL
+separators). The data is sent either as part of the AUTH command, or
+subsequently in response to an empty prompt from the server.
+
+The second and third strings are a user name and a corresponding password.
+Using a single fixed user name and password as an example, this could be
+configured as follows:
+
+fixed_plain:
+  driver = plaintext
+  public_name = PLAIN
+  server_prompts = :
+  server_condition = \
+    ${if and {{eq{$auth2}{username}}{eq{$auth3}{mysecret}}}}
+  server_set_id = $auth2
+
+Note that the default result strings from if ("true" or an empty string) are
+exactly what we want here, so they need not be specified. Obviously, if the
+password contains expansion-significant characters such as dollar, backslash,
+or closing brace, they have to be escaped.
+
+The server_prompts setting specifies a single, empty prompt (empty items at the
+end of a string list are ignored). If all the data comes as part of the AUTH
+command, as is commonly the case, the prompt is not used. This authenticator is
+advertised in the response to EHLO as
+
+250-AUTH PLAIN
+
+and a client host can authenticate itself by sending the command
+
+AUTH PLAIN AHVzZXJuYW1lAG15c2VjcmV0
+
+As this contains three strings (more than the number of prompts), no further
+data is required from the client. Alternatively, the client may just send
+
+AUTH PLAIN
+
+to initiate authentication, in which case the server replies with an empty
+prompt. The client must respond with the combined data string.
+
+The data string is base64 encoded, as required by the RFC. This example, when
+decoded, is <NUL>"username"<NUL>"mysecret", where <NUL> represents a zero byte.
+This is split up into three strings, the first of which is empty. The 
+server_condition option in the authenticator checks that the second two are
+"username" and "mysecret" respectively.
+
+Having just one fixed user name and password, as in this example, is not very
+realistic, though for a small organization with only a handful of
+authenticating clients it could make sense.
+
+A more sophisticated instance of this authenticator could use the user name in
+$auth2 to look up a password in a file or database, and maybe do an encrypted
+comparison (see crypteq in chapter 11). Here is a example of this approach,
+where the passwords are looked up in a DBM file. Warning: This is an incorrect
+example:
+
+server_condition = \
+  ${if eq{$auth3}{${lookup{$auth2}dbm{/etc/authpwd}}}}
+
+The expansion uses the user name ($auth2) as the key to look up a password,
+which it then compares to the supplied password ($auth3). Why is this example
+incorrect? It works fine for existing users, but consider what happens if a
+non-existent user name is given. The lookup fails, but as no success/failure
+strings are given for the lookup, it yields an empty string. Thus, to defeat
+the authentication, all a client has to do is to supply a non-existent user
+name and an empty password. The correct way of writing this test is:
+
+server_condition = ${lookup{$auth2}dbm{/etc/authpwd}\
+  {${if eq{$value}{$auth3}}} {false}}
+
+In this case, if the lookup succeeds, the result is checked; if the lookup
+fails, "false" is returned and authentication fails. If crypteq is being used
+instead of eq, the first example is in fact safe, because crypteq always fails
+if its second argument is empty. However, the second way of writing the test
+makes the logic clearer.
+
+
+34.4 The LOGIN authentication mechanism
+---------------------------------------
+
+The LOGIN authentication mechanism is not documented in any RFC, but is in use
+in a number of programs. No data is sent with the AUTH command. Instead, a user
+name and password are supplied separately, in response to prompts. The
+plaintext authenticator can be configured to support this as in this example:
+
+fixed_login:
+  driver = plaintext
+  public_name = LOGIN
+  server_prompts = User Name : Password
+  server_condition = \
+    ${if and {{eq{$auth1}{username}}{eq{$auth2}{mysecret}}}}
+  server_set_id = $auth1
+
+Because of the way plaintext operates, this authenticator accepts data supplied
+with the AUTH command (in contravention of the specification of LOGIN), but if
+the client does not supply it (as is the case for LOGIN clients), the prompt
+strings are used to obtain two data items.
+
+Some clients are very particular about the precise text of the prompts. For
+example, Outlook Express is reported to recognize only "Username:" and
+"Password:". Here is an example of a LOGIN authenticator that uses those
+strings. It uses the ldapauth expansion condition to check the user name and
+password by binding to an LDAP server:
+
+login:
+  driver = plaintext
+  public_name = LOGIN
+  server_prompts = Username:: : Password::
+  server_condition = ${if and{{ \
+    !eq{}{$auth1} }{ \
+    ldapauth{\
+      user="uid=${quote_ldap_dn:$auth1},ou=people,o=example.org" \
+      pass=${quote:$auth2} \
+      ldap://ldap.example.org/} }} }
+  server_set_id = uid=$auth1,ou=people,o=example.org
+
+We have to check that the username is not empty before using it, because LDAP
+does not permit empty DN components. We must also use the quote_ldap_dn
+operator to correctly quote the DN for authentication. However, the basic quote
+operator, rather than any of the LDAP quoting operators, is the correct one to
+use for the password, because quoting is needed only to make the password
+conform to the Exim syntax. At the LDAP level, the password is an uninterpreted
+string.
+
+
+34.5 Support for different kinds of authentication
+--------------------------------------------------
+
+A number of string expansion features are provided for the purpose of
+interfacing to different ways of user authentication. These include checking
+traditionally encrypted passwords from /etc/passwd (or equivalent), PAM,
+Radius, ldapauth, pwcheck, and saslauthd. For details see section 11.7.
+
+
+34.6 Using plaintext in a client
+--------------------------------
+
+The plaintext authenticator has two client options:
+
++----------------------------+--------------+-------------+--------------+
+|client_ignore_invalid_base64|Use: plaintext|Type: boolean|Default: false|
++----------------------------+--------------+-------------+--------------+
+
+If the client receives a server prompt that is not a valid base64 string,
+authentication is abandoned by default. However, if this option is set true,
+the error in the challenge is ignored and the client sends the response as
+usual.
+
++-----------+--------------+-------------+--------------+
+|client_send|Use: plaintext|Type: string*|Default: unset|
++-----------+--------------+-------------+--------------+
+
+The string is a colon-separated list of authentication data strings. Each
+string is independently expanded before being sent to the server. The first
+string is sent with the AUTH command; any more strings are sent in response to
+prompts from the server. Before each string is expanded, the value of the most
+recent prompt is placed in the next $auth<n> variable, starting with $auth1 for
+the first prompt. Up to three prompts are stored in this way. Thus, the prompt
+that is received in response to sending the first string (with the AUTH
+command) can be used in the expansion of the second string, and so on. If an
+invalid base64 string is received when client_ignore_invalid_base64 is set, an
+empty string is put in the $auth<n> variable.
+
+Note: You cannot use expansion to create multiple strings, because splitting
+takes priority and happens first.
+
+Because the PLAIN authentication mechanism requires NUL (binary zero) bytes in
+the data, further processing is applied to each string before it is sent. If
+there are any single circumflex characters in the string, they are converted to
+NULs. Should an actual circumflex be required as data, it must be doubled in
+the string.
+
+This is an example of a client configuration that implements the PLAIN
+authentication mechanism with a fixed user name and password:
+
+fixed_plain:
+  driver = plaintext
+  public_name = PLAIN
+  client_send = ^username^mysecret
+
+The lack of colons means that the entire text is sent with the AUTH command,
+with the circumflex characters converted to NULs. A similar example that uses
+the LOGIN mechanism is:
+
+fixed_login:
+  driver = plaintext
+  public_name = LOGIN
+  client_send = : username : mysecret
+
+The initial colon means that the first string is empty, so no data is sent with
+the AUTH command itself. The remaining strings are sent in response to prompts.
+
+
+
+===============================================================================
+35. THE CRAM_MD5 AUTHENTICATOR
+
+The CRAM-MD5 authentication mechanism is described in RFC 2195. The server
+sends a challenge string to the client, and the response consists of a user
+name and the CRAM-MD5 digest of the challenge string combined with a secret
+string (password) which is known to both server and client. Thus, the secret is
+not sent over the network as plain text, which makes this authenticator more
+secure than plaintext. However, the downside is that the secret has to be
+available in plain text at either end.
+
+
+35.1 Using cram_md5 as a server
+-------------------------------
+
+This authenticator has one server option, which must be set to configure the
+authenticator as a server:
+
++-------------+-------------+-------------+--------------+
+|server_secret|Use: cram_md5|Type: string*|Default: unset|
++-------------+-------------+-------------+--------------+
+
+When the server receives the client's response, the user name is placed in the
+expansion variable $auth1, and server_secret is expanded to obtain the password
+for that user. The server then computes the CRAM-MD5 digest that the client
+should have sent, and checks that it received the correct string. If the
+expansion of server_secret is forced to fail, authentication fails. If the
+expansion fails for some other reason, a temporary error code is returned to
+the client.
+
+For compatibility with previous releases of Exim, the user name is also placed
+in $1. However, the use of this variables for this purpose is now deprecated,
+as it can lead to confusion in string expansions that also use numeric
+variables for other things.
+
+For example, the following authenticator checks that the user name given by the
+client is "ph10", and if so, uses "secret" as the password. For any other user
+name, authentication fails.
+
+fixed_cram:
+  driver = cram_md5
+  public_name = CRAM-MD5
+  server_secret = ${if eq{$auth1}{ph10}{secret}fail}
+  server_set_id = $auth1
+
+If authentication succeeds, the setting of server_set_id preserves the user
+name in $authenticated_id. A more typical configuration might look up the
+secret string in a file, using the user name as the key. For example:
+
+lookup_cram:
+  driver = cram_md5
+  public_name = CRAM-MD5
+  server_secret = ${lookup{$auth1}lsearch{/etc/authpwd}\
+                  {$value}fail}
+  server_set_id = $auth1
+
+Note that this expansion explicitly forces failure if the lookup fails because
+$auth1 contains an unknown user name.
+
+As another example, if you wish to re-use a Cyrus SASL sasldb2 file without
+using the relevant libraries, you need to know the realm to specify in the
+lookup and then ask for the "userPassword" attribute for that user in that
+realm, with:
+
+cyrusless_crammd5:
+  driver = cram_md5
+  public_name = CRAM-MD5
+  server_secret = ${lookup{$auth1:mail.example.org:userPassword}\
+                  dbmjz{/etc/sasldb2}}
+  server_set_id = $auth1
+
+
+35.2 Using cram_md5 as a client
+-------------------------------
+
+When used as a client, the cram_md5 authenticator has two options:
+
++-----------+-------------+-------------+------------------------------+
+|client_name|Use: cram_md5|Type: string*|Default: the primary host name|
++-----------+-------------+-------------+------------------------------+
+
+This string is expanded, and the result used as the user name data when
+computing the response to the server's challenge.
+
++-------------+-------------+-------------+--------------+
+|client_secret|Use: cram_md5|Type: string*|Default: unset|
++-------------+-------------+-------------+--------------+
+
+This option must be set for the authenticator to work as a client. Its value is
+expanded and the result used as the secret string when computing the response.
+
+Different user names and secrets can be used for different servers by referring
+to $host or $host_address in the options. Forced failure of either expansion
+string is treated as an indication that this authenticator is not prepared to
+handle this case. Exim moves on to the next configured client authenticator.
+Any other expansion failure causes Exim to give up trying to send the message
+to the current server.
+
+A simple example configuration of a cram_md5 authenticator, using fixed
+strings, is:
+
+fixed_cram:
+  driver = cram_md5
+  public_name = CRAM-MD5
+  client_name = ph10
+  client_secret = secret
+
+
+
+===============================================================================
+36. THE CYRUS_SASL AUTHENTICATOR
+
+The code for this authenticator was provided by Matthew Byng-Maddick of A L
+Digital Ltd (http://www.aldigital.co.uk).
+
+The cyrus_sasl authenticator provides server support for the Cyrus SASL library
+implementation of the RFC 2222 ("Simple Authentication and Security Layer").
+This library supports a number of authentication mechanisms, including PLAIN
+and LOGIN, but also several others that Exim does not support directly. In
+particular, there is support for Kerberos authentication.
+
+The cyrus_sasl authenticator provides a gatewaying mechanism directly to the
+Cyrus interface, so if your Cyrus library can do, for example, CRAM-MD5, then
+so can the cyrus_sasl authenticator. By default it uses the public name of the
+driver to determine which mechanism to support.
+
+Where access to some kind of secret file is required, for example in GSSAPI or
+CRAM-MD5, it is worth noting that the authenticator runs as the Exim user, and
+that the Cyrus SASL library has no way of escalating privileges by default. You
+may also find you need to set environment variables, depending on the driver
+you are using.
+
+The application name provided by Exim is "exim", so various SASL options may be
+set in exim.conf in your SASL directory. If you are using GSSAPI for Kerberos,
+note that because of limitations in the GSSAPI interface, changing the server
+keytab might need to be communicated down to the Kerberos layer independently.
+The mechanism for doing so is dependent upon the Kerberos implementation.
+
+For example, for older releases of Heimdal, the environment variable
+KRB5_KTNAME may be set to point to an alternative keytab file. Exim will pass
+this variable through from its own inherited environment when started as root
+or the Exim user. The keytab file needs to be readable by the Exim user. With
+newer releases of Heimdal, a setuid Exim may cause Heimdal to discard the
+environment variable. In practice, for those releases, the Cyrus authenticator
+is not a suitable interface for GSSAPI (Kerberos) support. Instead, consider
+the heimdal_gssapi authenticator, described in chapter 39
+
+
+36.1 Using cyrus_sasl as a server
+---------------------------------
+
+The cyrus_sasl authenticator has four private options. It puts the username (on
+a successful authentication) into $auth1. For compatibility with previous
+releases of Exim, the username is also placed in $1. However, the use of this
+variable for this purpose is now deprecated, as it can lead to confusion in
+string expansions that also use numeric variables for other things.
+
++---------------+---------------+-------------+------------------+
+|server_hostname|Use: cyrus_sasl|Type: string*|Default: see below|
++---------------+---------------+-------------+------------------+
+
+This option selects the hostname that is used when communicating with the
+library. The default value is "$primary_hostname". It is up to the underlying
+SASL plug-in what it does with this data.
+
++-----------+---------------+------------+------------------+
+|server_mech|Use: cyrus_sasl|Type: string|Default: see below|
++-----------+---------------+------------+------------------+
+
+This option selects the authentication mechanism this driver should use. The
+default is the value of the generic public_name option. This option allows you
+to use a different underlying mechanism from the advertised name. For example:
+
+sasl:
+  driver = cyrus_sasl
+  public_name = X-ANYTHING
+  server_mech = CRAM-MD5
+  server_set_id = $auth1
+
++------------+---------------+-------------+--------------+
+|server_realm|Use: cyrus_sasl|Type: string*|Default: unset|
++------------+---------------+-------------+--------------+
+
+This specifies the SASL realm that the server claims to be in.
+
++--------------+---------------+------------+---------------+
+|server_service|Use: cyrus_sasl|Type: string|Default: "smtp"|
++--------------+---------------+------------+---------------+
+
+This is the SASL service that the server claims to implement.
+
+For straightforward cases, you do not need to set any of the authenticator's
+private options. All you need to do is to specify an appropriate mechanism as
+the public name. Thus, if you have a SASL library that supports CRAM-MD5 and
+PLAIN, you could have two authenticators as follows:
+
+sasl_cram_md5:
+  driver = cyrus_sasl
+  public_name = CRAM-MD5
+  server_set_id = $auth1
+
+sasl_plain:
+  driver = cyrus_sasl
+  public_name = PLAIN
+  server_set_id = $auth2
+
+Cyrus SASL does implement the LOGIN authentication method, even though it is
+not a standard method. It is disabled by default in the source distribution,
+but it is present in many binary distributions.
+
+
+
+===============================================================================
+37. THE DOVECOT AUTHENTICATOR
+
+This authenticator is an interface to the authentication facility of the
+Dovecot POP/IMAP server, which can support a number of authentication methods.
+
+Note that Dovecot must be configured to use auth-client not auth-userdb.
+
+If you are using Dovecot to authenticate POP/IMAP clients, it might be helpful
+to use the same mechanisms for SMTP authentication. This is a server
+authenticator only. There is only one option:
+
++-------------+------------+------------+--------------+
+|server_socket|Use: dovecot|Type: string|Default: unset|
++-------------+------------+------------+--------------+
+
+This option must specify the socket that is the interface to Dovecot
+authentication. The public_name option must specify an authentication mechanism
+that Dovecot is configured to support. You can have several authenticators for
+different mechanisms. For example:
+
+dovecot_plain:
+  driver = dovecot
+  public_name = PLAIN
+  server_socket = /var/run/dovecot/auth-client
+  server_set_id = $auth1
+
+dovecot_ntlm:
+  driver = dovecot
+  public_name = NTLM
+  server_socket = /var/run/dovecot/auth-client
+  server_set_id = $auth1
+
+If the SMTP connection is encrypted, or if $sender_host_address is equal to
+$received_ip_address (that is, the connection is local), the "secured" option
+is passed in the Dovecot authentication command. If, for a TLS connection, a
+client certificate has been verified, the "valid-client-cert" option is passed.
+When authentication succeeds, the identity of the user who authenticated is
+placed in $auth1.
+
+
+
+===============================================================================
+38. THE GSASL AUTHENTICATOR
+
+The gsasl authenticator provides server integration for the GNU SASL library
+and the mechanisms it provides. This is new as of the 4.80 release and there
+are a few areas where the library does not let Exim smoothly scale to handle
+future authentication mechanisms, so no guarantee can be made that any
+particular new authentication mechanism will be supported without code changes
+in Exim.
+
++---------------------+----------+-------------+--------------+
+|server_channelbinding|Use: gsasl|Type: boolean|Default: false|
++---------------------+----------+-------------+--------------+
+
+Some authentication mechanisms are able to use external context at both ends of
+the session to bind the authentication to that context, and fail the
+authentication process if that context differs. Specifically, some TLS
+ciphersuites can provide identifying information about the cryptographic
+context.
+
+This means that certificate identity and verification becomes a non-issue, as a
+man-in-the-middle attack will cause the correct client and server to see
+different identifiers and authentication will fail.
+
+This is currently only supported when using the GnuTLS library. This is only
+usable by mechanisms which support "channel binding"; at time of writing,
+that's the SCRAM family.
+
+This defaults off to ensure smooth upgrade across Exim releases, in case this
+option causes some clients to start failing. Some future release of Exim may
+switch the default to be true.
+
++---------------+----------+-------------+------------------+
+|server_hostname|Use: gsasl|Type: string*|Default: see below|
++---------------+----------+-------------+------------------+
+
+This option selects the hostname that is used when communicating with the
+library. The default value is "$primary_hostname". Some mechanisms will use
+this data.
+
++-----------+----------+------------+------------------+
+|server_mech|Use: gsasl|Type: string|Default: see below|
++-----------+----------+------------+------------------+
+
+This option selects the authentication mechanism this driver should use. The
+default is the value of the generic public_name option. This option allows you
+to use a different underlying mechanism from the advertised name. For example:
+
+sasl:
+  driver = gsasl
+  public_name = X-ANYTHING
+  server_mech = CRAM-MD5
+  server_set_id = $auth1
+
++---------------+----------+-------------+--------------+
+|server_password|Use: gsasl|Type: string*|Default: unset|
++---------------+----------+-------------+--------------+
+
+Various mechanisms need access to the cleartext password on the server, so that
+proof-of-possession can be demonstrated on the wire, without sending the
+password itself.
+
+The data available for lookup varies per mechanism. In all cases, $auth1 is set
+to the authentication id. The $auth2 variable will always be the authorization
+id (authz) if available, else the empty string. The $auth3 variable will always
+be the realm if available, else the empty string.
+
+A forced failure will cause authentication to defer.
+
+If using this option, it may make sense to set the server_condition option to
+be simply "true".
+
++------------+----------+-------------+--------------+
+|server_realm|Use: gsasl|Type: string*|Default: unset|
++------------+----------+-------------+--------------+
+
+This specifies the SASL realm that the server claims to be in. Some mechanisms
+will use this data.
+
++-----------------+----------+-------------+--------------+
+|server_scram_iter|Use: gsasl|Type: string*|Default: unset|
++-----------------+----------+-------------+--------------+
+
+This option provides data for the SCRAM family of mechanisms. $auth1 is not
+available at evaluation time. (This may change, as we receive feedback on use)
+
++-----------------+----------+-------------+--------------+
+|server_scram_salt|Use: gsasl|Type: string*|Default: unset|
++-----------------+----------+-------------+--------------+
+
+This option provides data for the SCRAM family of mechanisms. $auth1 is not
+available at evaluation time. (This may change, as we receive feedback on use)
+
++--------------+----------+------------+---------------+
+|server_service|Use: gsasl|Type: string|Default: "smtp"|
++--------------+----------+------------+---------------+
+
+This is the SASL service that the server claims to implement. Some mechanisms
+will use this data.
+
+
+38.1 gsasl auth variables
+-------------------------
+
+These may be set when evaluating specific options, as detailed above. They will
+also be set when evaluating server_condition.
+
+Unless otherwise stated below, the gsasl integration will use the following
+meanings for these variables:
+
+  * $auth1: the authentication id
+
+  * $auth2: the authorization id
+
+  * $auth3: the realm
+
+On a per-mechanism basis:
+
+  * EXTERNAL: only $auth1 is set, to the possibly empty authorization id; the 
+    server_condition option must be present.
+
+  * ANONYMOUS: only $auth1 is set, to the possibly empty anonymous token; the 
+    server_condition option must be present.
+
+  * GSSAPI: $auth1 will be set to the GSSAPI Display Name; $auth2 will be set
+    to the authorization id, the server_condition option must be present.
+
+An anonymous token is something passed along as an unauthenticated identifier;
+this is analogous to FTP anonymous authentication passing an email address, or
+software-identifier@, as the "password".
+
+An example showing the password having the realm specified in the callback and
+demonstrating a Cyrus SASL to GSASL migration approach is:
+
+gsasl_cyrusless_crammd5:
+  driver = gsasl
+  public_name = CRAM-MD5
+  server_realm = imap.example.org
+  server_password = ${lookup{$auth1:$auth3:userPassword}\
+                    dbmjz{/etc/sasldb2}{$value}fail}
+  server_set_id = ${quote:$auth1}
+  server_condition = yes
+
+
+
+===============================================================================
+39. THE HEIMDAL_GSSAPI AUTHENTICATOR
+
+The heimdal_gssapi authenticator provides server integration for the Heimdal
+GSSAPI/Kerberos library, permitting Exim to set a keytab pathname reliably.
+
++---------------+-------------------+-------------+------------------+
+|server_hostname|Use: heimdal_gssapi|Type: string*|Default: see below|
++---------------+-------------------+-------------+------------------+
+
+This option selects the hostname that is used, with server_service, for
+constructing the GSS server name, as a GSS_C_NT_HOSTBASED_SERVICE identifier.
+The default value is "$primary_hostname".
+
++-------------+-------------------+-------------+--------------+
+|server_keytab|Use: heimdal_gssapi|Type: string*|Default: unset|
++-------------+-------------------+-------------+--------------+
+
+If set, then Heimdal will not use the system default keytab (typically /etc/
+krb5.keytab) but instead the pathname given in this option. The value should be
+a pathname, with no "file:" prefix.
+
++--------------+-------------------+-------------+-------------+
+|server_service|Use: heimdal_gssapi|Type: string*|Default: smtp|
++--------------+-------------------+-------------+-------------+
+
+This option specifies the service identifier used, in conjunction with 
+server_hostname, for building the identifer for finding credentials from the
+keytab.
+
+
+39.1 heimdal_gssapi auth variables
+----------------------------------
+
+Beware that these variables will typically include a realm, thus will appear to
+be roughly like an email address already. The authzid in $auth2 is not
+verified, so a malicious client can set it to anything.
+
+The $auth1 field should be safely trustable as a value from the Key
+Distribution Center. Note that these are not quite email addresses. Each
+identifier is for a role, and so the left-hand-side may include a role suffix.
+For instance, "joe/admin@EXAMPLE.ORG".
+
+  * $auth1: the authentication id, set to the GSS Display Name.
+
+  * $auth2: the authorization id, sent within SASL encapsulation after
+    authentication. If that was empty, this will also be set to the GSS Display
+    Name.
+
+
+
+===============================================================================
+40. THE SPA AUTHENTICATOR
+
+The spa authenticator provides client support for Microsoft's Secure Password
+Authentication mechanism, which is also sometimes known as NTLM (NT LanMan).
+The code for client side of this authenticator was contributed by Marc
+Prud'hommeaux, and much of it is taken from the Samba project (http://
+www.samba.org). The code for the server side was subsequently contributed by
+Tom Kistner. The mechanism works as follows:
+
+  * After the AUTH command has been accepted, the client sends an SPA
+    authentication request based on the user name and optional domain.
+
+  * The server sends back a challenge.
+
+  * The client builds a challenge response which makes use of the user's
+    password and sends it to the server, which then accepts or rejects it.
+
+Encryption is used to protect the password in transit.
+
+
+40.1 Using spa as a server
+--------------------------
+
+The spa authenticator has just one server option:
+
++---------------+--------+-------------+--------------+
+|server_password|Use: spa|Type: string*|Default: unset|
++---------------+--------+-------------+--------------+
+
+This option is expanded, and the result must be the cleartext password for the
+authenticating user, whose name is at this point in $auth1. For compatibility
+with previous releases of Exim, the user name is also placed in $1. However,
+the use of this variable for this purpose is now deprecated, as it can lead to
+confusion in string expansions that also use numeric variables for other
+things. For example:
+
+spa:
+  driver = spa
+  public_name = NTLM
+  server_password = \
+    ${lookup{$auth1}lsearch{/etc/exim/spa_clearpass}{$value}fail}
+
+If the expansion is forced to fail, authentication fails. Any other expansion
+failure causes a temporary error code to be returned.
+
+
+40.2 Using spa as a client
+--------------------------
+
+The spa authenticator has the following client options:
+
++-------------+--------+-------------+--------------+
+|client_domain|Use: spa|Type: string*|Default: unset|
++-------------+--------+-------------+--------------+
+
+This option specifies an optional domain for the authentication.
+
++---------------+--------+-------------+--------------+
+|client_password|Use: spa|Type: string*|Default: unset|
++---------------+--------+-------------+--------------+
+
+This option specifies the user's password, and must be set.
+
++---------------+--------+-------------+--------------+
+|client_username|Use: spa|Type: string*|Default: unset|
++---------------+--------+-------------+--------------+
+
+This option specifies the user name, and must be set. Here is an example of a
+configuration of this authenticator for use with the mail servers at msn.com:
+
+msn:
+  driver = spa
+  public_name = MSN
+  client_username = msn/msn_username
+  client_password = msn_plaintext_password
+  client_domain = DOMAIN_OR_UNSET
+
+
+
+===============================================================================
+41. ENCRYPTED SMTP CONNECTIONS USING TLS/SSL
+
+Support for TLS (Transport Layer Security), formerly known as SSL (Secure
+Sockets Layer), is implemented by making use of the OpenSSL library or the
+GnuTLS library (Exim requires GnuTLS release 1.0 or later). There is no
+cryptographic code in the Exim distribution itself for implementing TLS. In
+order to use this feature you must install OpenSSL or GnuTLS, and then build a
+version of Exim that includes TLS support (see section 4.7). You also need to
+understand the basic concepts of encryption at a managerial level, and in
+particular, the way that public keys, private keys, and certificates are used.
+
+RFC 3207 defines how SMTP connections can make use of encryption. Once a
+connection is established, the client issues a STARTTLS command. If the server
+accepts this, the client and the server negotiate an encryption mechanism. If
+the negotiation succeeds, the data that subsequently passes between them is
+encrypted.
+
+Exim's ACLs can detect whether the current SMTP session is encrypted or not,
+and if so, what cipher suite is in use, whether the client supplied a
+certificate, and whether or not that certificate was verified. This makes it
+possible for an Exim server to deny or accept certain commands based on the
+encryption state.
+
+Warning: Certain types of firewall and certain anti-virus products can disrupt
+TLS connections. You need to turn off SMTP scanning for these products in order
+to get TLS to work.
+
+
+41.1 Support for the legacy "ssmtp" (aka "smtps") protocol
+----------------------------------------------------------
+
+Early implementations of encrypted SMTP used a different TCP port from normal
+SMTP, and expected an encryption negotiation to start immediately, instead of
+waiting for a STARTTLS command from the client using the standard SMTP port.
+The protocol was called "ssmtp" or "smtps", and port 465 was allocated for this
+purpose.
+
+This approach was abandoned when encrypted SMTP was standardized, but there are
+still some legacy clients that use it. Exim supports these clients by means of
+the tls_on_connect_ports global option. Its value must be a list of port
+numbers; the most common use is expected to be:
+
+tls_on_connect_ports = 465
+
+The port numbers specified by this option apply to all SMTP connections, both
+via the daemon and via inetd. You still need to specify all the ports that the
+daemon uses (by setting daemon_smtp_ports or local_interfaces or the -oX
+command line option) because tls_on_connect_ports does not add an extra port -
+rather, it specifies different behaviour on a port that is defined elsewhere.
+
+There is also a -tls-on-connect command line option. This overrides 
+tls_on_connect_ports; it forces the legacy behaviour for all ports.
+
+
+41.2 OpenSSL vs GnuTLS
+----------------------
+
+The first TLS support in Exim was implemented using OpenSSL. Support for GnuTLS
+followed later, when the first versions of GnuTLS were released. To build Exim
+to use GnuTLS, you need to set
+
+USE_GNUTLS=yes
+
+in Local/Makefile, in addition to
+
+SUPPORT_TLS=yes
+
+You must also set TLS_LIBS and TLS_INCLUDE appropriately, so that the include
+files and libraries for GnuTLS can be found.
+
+There are some differences in usage when using GnuTLS instead of OpenSSL:
+
+  * The tls_verify_certificates option must contain the name of a file, not the
+    name of a directory (for OpenSSL it can be either).
+
+  * The default value for tls_dhparam differs for historical reasons.
+
+  * Distinguished Name (DN) strings reported by the OpenSSL library use a slash
+    for separating fields; GnuTLS uses commas, in accordance with RFC 2253.
+    This affects the value of the $tls_in_peerdn and $tls_out_peerdn variables.
+
+  * OpenSSL identifies cipher suites using hyphens as separators, for example:
+    DES-CBC3-SHA. GnuTLS historically used underscores, for example:
+    RSA_ARCFOUR_SHA. What is more, OpenSSL complains if underscores are present
+    in a cipher list. To make life simpler, Exim changes underscores to hyphens
+    for OpenSSL and passes the string unchanged to GnuTLS (expecting the
+    library to handle its own older variants) when processing lists of cipher
+    suites in the tls_require_ciphers options (the global option and the smtp
+    transport option).
+
+  * The tls_require_ciphers options operate differently, as described in the
+    sections 41.4 and 41.5.
+
+  * The tls_dh_min_bits SMTP transport option is only honoured by GnuTLS. When
+    using OpenSSL, this option is ignored. (If an API is found to let OpenSSL
+    be configured in this way, let the Exim Maintainers know and we'll likely
+    use it).
+
+  * Some other recently added features may only be available in one or the
+    other. This should be documented with the feature. If the documentation
+    does not explicitly state that the feature is infeasible in the other TLS
+    implementation, then patches are welcome.
+
+
+41.3 GnuTLS parameter computation
+---------------------------------
+
+This section only applies if tls_dhparam is set to "historic" or to an explicit
+path; if the latter, then the text about generation still applies, but not the
+chosen filename. By default, as of Exim 4.80 a hard-coded D-H prime is used.
+See the documentation of tls_dhparam for more information.
+
+GnuTLS uses D-H parameters that may take a substantial amount of time to
+compute. It is unreasonable to re-compute them for every TLS session.
+Therefore, Exim keeps this data in a file in its spool directory, called
+gnutls-params-NNNN for some value of NNNN, corresponding to the number of bits
+requested. The file is owned by the Exim user and is readable only by its
+owner. Every Exim process that start up GnuTLS reads the D-H parameters from
+this file. If the file does not exist, the first Exim process that needs it
+computes the data and writes it to a temporary file which is renamed once it is
+complete. It does not matter if several Exim processes do this simultaneously
+(apart from wasting a few resources). Once a file is in place, new Exim
+processes immediately start using it.
+
+For maximum security, the parameters that are stored in this file should be
+recalculated periodically, the frequency depending on your paranoia level. If
+you are avoiding using the fixed D-H primes published in RFCs, then you are
+concerned about some advanced attacks and will wish to do this; if you do not
+regenerate then you might as well stick to the standard primes.
+
+Arranging this is easy in principle; just delete the file when you want new
+values to be computed. However, there may be a problem. The calculation of new
+parameters needs random numbers, and these are obtained from /dev/random. If
+the system is not very active, /dev/random may delay returning data until
+enough randomness (entropy) is available. This may cause Exim to hang for a
+substantial amount of time, causing timeouts on incoming connections.
+
+The solution is to generate the parameters externally to Exim. They are stored
+in gnutls-params-N in PEM format, which means that they can be generated
+externally using the certtool command that is part of GnuTLS.
+
+To replace the parameters with new ones, instead of deleting the file and
+letting Exim re-create it, you can generate new parameters using certtool and,
+when this has been done, replace Exim's cache file by renaming. The relevant
+commands are something like this:
+
+# ls
+[ look for file; assume gnutls-params-2236 is the most recent ]
+# rm -f new-params
+# touch new-params
+# chown exim:exim new-params
+# chmod 0600 new-params
+# certtool --generate-dh-params --bits 2236 >>new-params
+# openssl dhparam -noout -text -in new-params | head
+[ check the first line, make sure it's not more than 2236;
+  if it is, then go back to the start ("rm") and repeat
+  until the size generated is at most the size requested ]
+# chmod 0400 new-params
+# mv new-params gnutls-params-2236
+
+If Exim never has to generate the parameters itself, the possibility of
+stalling is removed.
+
+The filename changed in Exim 4.80, to gain the -bits suffix. The value which
+Exim will choose depends upon the version of GnuTLS in use. For older GnuTLS,
+the value remains hard-coded in Exim as 1024. As of GnuTLS 2.12.x, there is a
+way for Exim to ask for the "normal" number of bits for D-H public-key usage,
+and Exim does so. This attempt to remove Exim from TLS policy decisions failed,
+as GnuTLS 2.12 returns a value higher than the current hard-coded limit of the
+NSS library. Thus Exim gains the tls_dh_max_bits global option, which applies
+to all D-H usage, client or server. If the value returned by GnuTLS is greater
+than tls_dh_max_bits then the value will be clamped down to tls_dh_max_bits.
+The default value has been set at the current NSS limit, which is still much
+higher than Exim historically used.
+
+The filename and bits used will change as the GnuTLS maintainers change the
+value for their parameter "GNUTLS_SEC_PARAM_NORMAL", as clamped by 
+tls_dh_max_bits. At the time of writing (mid 2012), GnuTLS 2.12 recommends 2432
+bits, while NSS is limited to 2236 bits.
+
+In fact, the requested value will be *lower* than tls_dh_max_bits, to increase
+the chance of the generated prime actually being within acceptable bounds, as
+GnuTLS has been observed to overshoot. Note the check step in the procedure
+above. There is no sane procedure available to Exim to double-check the size of
+the generated prime, so it might still be too large.
+
+
+41.4 Requiring specific ciphers in OpenSSL
+------------------------------------------
+
+There is a function in the OpenSSL library that can be passed a list of cipher
+suites before the cipher negotiation takes place. This specifies which ciphers
+are acceptable. The list is colon separated and may contain names like
+DES-CBC3-SHA. Exim passes the expanded value of tls_require_ciphers directly to
+this function call. Many systems will install the OpenSSL manual-pages, so you
+may have ciphers(1) available to you. The following quotation from the OpenSSL
+documentation specifies what forms of item are allowed in the cipher string:
+
+  * It can consist of a single cipher suite such as RC4-SHA.
+
+  * It can represent a list of cipher suites containing a certain algorithm, or
+    cipher suites of a certain type. For example SHA1 represents all ciphers
+    suites using the digest algorithm SHA1 and SSLv3 represents all SSL v3
+    algorithms.
+
+  * Lists of cipher suites can be combined in a single cipher string using the
+    + character. This is used as a logical and operation. For example SHA1+DES
+    represents all cipher suites containing the SHA1 and the DES algorithms.
+
+Each cipher string can be optionally preceded by one of the characters "!", "-"
+or "+".
+
+  * If "!" is used, the ciphers are permanently deleted from the list. The
+    ciphers deleted can never reappear in the list even if they are explicitly
+    stated.
+
+  * If "-" is used, the ciphers are deleted from the list, but some or all of
+    the ciphers can be added again by later options.
+
+  * If "+" is used, the ciphers are moved to the end of the list. This option
+    does not add any new ciphers; it just moves matching existing ones.
+
+If none of these characters is present, the string is interpreted as a list of
+ciphers to be appended to the current preference list. If the list includes any
+ciphers already present they will be ignored: that is, they will not be moved
+to the end of the list.
+
+The OpenSSL ciphers(1) command may be used to test the results of a given
+string:
+
+# note single-quotes to get ! past any shell history expansion
+$ openssl ciphers 'HIGH:!MD5:!SHA1'
+
+This example will let the library defaults be permitted on the MX port, where
+there's probably no identity verification anyway, but ups the ante on the
+submission ports where the administrator might have some influence on the
+choice of clients used:
+
+# OpenSSL variant; see man ciphers(1)
+tls_require_ciphers = ${if =={$received_port}{25}\
+                           {DEFAULT}\
+                           {HIGH:!MD5:!SHA1}}
+
+
+41.5 Requiring specific ciphers or other parameters in GnuTLS
+-------------------------------------------------------------
+
+The GnuTLS library allows the caller to provide a "priority string", documented
+as part of the gnutls_priority_init function. This is very similar to the
+ciphersuite specification in OpenSSL.
+
+The tls_require_ciphers option is treated as the GnuTLS priority string.
+
+The tls_require_ciphers option is available both as an global option,
+controlling how Exim behaves as a server, and also as an option of the smtp
+transport, controlling how Exim behaves as a client. In both cases the value is
+string expanded. The resulting string is not an Exim list and the string is
+given to the GnuTLS library, so that Exim does not need to be aware of future
+feature enhancements of GnuTLS.
+
+Documentation of the strings accepted may be found in the GnuTLS manual, under
+"Priority strings". This is online as http://www.gnutls.org/manual/html_node/
+Priority-Strings.html, but beware that this relates to GnuTLS 3, which may be
+newer than the version installed on your system. If you are using GnuTLS 3, 
+then the example code on that site can be used to test a given string.
+
+Prior to Exim 4.80, an older API of GnuTLS was used, and Exim supported three
+additional options, "gnutls_require_kx", "gnutls_require_mac" and "
+gnutls_require_protocols". tls_require_ciphers was an Exim list.
+
+This example will let the library defaults be permitted on the MX port, where
+there's probably no identity verification anyway, and lowers security further
+by increasing compatibility; but this ups the ante on the submission ports
+where the administrator might have some influence on the choice of clients
+used:
+
+# GnuTLS variant
+tls_require_ciphers = ${if =={$received_port}{25}\
+                           {NORMAL:%COMPAT}\
+                           {SECURE128}}
+
+
+41.6 Configuring an Exim server to use TLS
+------------------------------------------
+
+When Exim has been built with TLS support, it advertises the availability of
+the STARTTLS command to client hosts that match tls_advertise_hosts, but not to
+any others. The default value of this option is unset, which means that
+STARTTLS is not advertised at all. This default is chosen because you need to
+set some other options in order to make TLS available, and also it is sensible
+for systems that want to use TLS only as a client.
+
+If a client issues a STARTTLS command and there is some configuration problem
+in the server, the command is rejected with a 454 error. If the client persists
+in trying to issue SMTP commands, all except QUIT are rejected with the error
+
+554 Security failure
+
+If a STARTTLS command is issued within an existing TLS session, it is rejected
+with a 554 error code.
+
+To enable TLS operations on a server, you must set tls_advertise_hosts to match
+some hosts. You can, of course, set it to * to match all hosts. However, this
+is not all you need to do. TLS sessions to a server won't work without some
+further configuration at the server end.
+
+It is rumoured that all existing clients that support TLS/SSL use RSA
+encryption. To make this work you need to set, in the server,
+
+tls_certificate = /some/file/name
+tls_privatekey = /some/file/name
+
+These options are, in fact, expanded strings, so you can make them depend on
+the identity of the client that is connected if you wish. The first file
+contains the server's X509 certificate, and the second contains the private key
+that goes with it. These files need to be readable by the Exim user, and must
+always be given as full path names. They can be the same file if both the
+certificate and the key are contained within it. If tls_privatekey is not set,
+or if its expansion is forced to fail or results in an empty string, this is
+assumed to be the case. The certificate file may also contain intermediate
+certificates that need to be sent to the client to enable it to authenticate
+the server's certificate.
+
+If you do not understand about certificates and keys, please try to find a
+source of this background information, which is not Exim-specific. (There are a
+few comments below in section 41.12.)
+
+Note: These options do not apply when Exim is operating as a client - they
+apply only in the case of a server. If you need to use a certificate in an Exim
+client, you must set the options of the same names in an smtp transport.
+
+With just these options, an Exim server will be able to use TLS. It does not
+require the client to have a certificate (but see below for how to insist on
+this). There is one other option that may be needed in other situations. If
+
+tls_dhparam = /some/file/name
+
+is set, the SSL library is initialized for the use of Diffie-Hellman ciphers
+with the parameters contained in the file. Set this to "none" to disable use of
+DH entirely, by making no prime available:
+
+tls_dhparam = none
+
+This may also be set to a string identifying a standard prime to be used for
+DH; if it is set to "default" or, for OpenSSL, is unset, then the prime used is
+"ike23". There are a few standard primes available, see the documentation for 
+tls_dhparam for the complete list.
+
+See the command
+
+openssl dhparam
+
+for a way of generating file data.
+
+The strings supplied for these three options are expanded every time a client
+host connects. It is therefore possible to use different certificates and keys
+for different hosts, if you so wish, by making use of the client's IP address
+in $sender_host_address to control the expansion. If a string expansion is
+forced to fail, Exim behaves as if the option is not set.
+
+The variable $tls_in_cipher is set to the cipher suite that was negotiated for
+an incoming TLS connection. It is included in the Received: header of an
+incoming message (by default - you can, of course, change this), and it is also
+included in the log line that records a message's arrival, keyed by "X=",
+unless the tls_cipher log selector is turned off. The encrypted condition can
+be used to test for specific cipher suites in ACLs.
+
+Once TLS has been established, the ACLs that run for subsequent SMTP commands
+can check the name of the cipher suite and vary their actions accordingly. The
+cipher suite names vary, depending on which TLS library is being used. For
+example, OpenSSL uses the name DES-CBC3-SHA for the cipher suite which in other
+contexts is known as TLS_RSA_WITH_3DES_EDE_CBC_SHA. Check the OpenSSL or GnuTLS
+documentation for more details.
+
+For outgoing SMTP deliveries, $tls_out_cipher is used and logged (again
+depending on the tls_cipher log selector).
+
+
+41.7 Requesting and verifying client certificates
+-------------------------------------------------
+
+If you want an Exim server to request a certificate when negotiating a TLS
+session with a client, you must set either tls_verify_hosts or 
+tls_try_verify_hosts. You can, of course, set either of them to * to apply to
+all TLS connections. For any host that matches one of these options, Exim
+requests a certificate as part of the setup of the TLS session. The contents of
+the certificate are verified by comparing it with a list of expected
+certificates. These must be available in a file or, for OpenSSL only (not
+GnuTLS), a directory, identified by tls_verify_certificates.
+
+A file can contain multiple certificates, concatenated end to end. If a
+directory is used (OpenSSL only), each certificate must be in a separate file,
+with a name (or a symbolic link) of the form <hash>.0, where <hash> is a hash
+value constructed from the certificate. You can compute the relevant hash by
+running the command
+
+openssl x509 -hash -noout -in /cert/file
+
+where /cert/file contains a single certificate.
+
+The difference between tls_verify_hosts and tls_try_verify_hosts is what
+happens if the client does not supply a certificate, or if the certificate does
+not match any of the certificates in the collection named by 
+tls_verify_certificates. If the client matches tls_verify_hosts, the attempt to
+set up a TLS session is aborted, and the incoming connection is dropped. If the
+client matches tls_try_verify_hosts, the (encrypted) SMTP session continues.
+ACLs that run for subsequent SMTP commands can detect the fact that no
+certificate was verified, and vary their actions accordingly. For example, you
+can insist on a certificate before accepting a message for relaying, but not
+when the message is destined for local delivery.
+
+When a client supplies a certificate (whether it verifies or not), the value of
+the Distinguished Name of the certificate is made available in the variable
+$tls_in_peerdn during subsequent processing of the message.
+
+Because it is often a long text string, it is not included in the log line or 
+Received: header by default. You can arrange for it to be logged, keyed by "DN=
+", by setting the tls_peerdn log selector, and you can use received_header_text
+to change the Received: header. When no certificate is supplied, $tls_in_peerdn
+is empty.
+
+
+41.8 Revoked certificates
+-------------------------
+
+Certificate issuing authorities issue Certificate Revocation Lists (CRLs) when
+certificates are revoked. If you have such a list, you can pass it to an Exim
+server using the global option called tls_crl and to an Exim client using an
+identically named option for the smtp transport. In each case, the value of the
+option is expanded and must then be the name of a file that contains a CRL in
+PEM format. The downside is that clients have to periodically re-download a
+potentially huge file from every certificate authority the know of.
+
+The way with most moving parts at query time is Online Certificate Status
+Protocol (OCSP), where the client verifies the certificate against an OCSP
+server run by the CA. This lets the CA track all usage of the certs. It
+requires running software with access to the private key of the CA, to sign the
+responses to the OCSP queries. OCSP is based on HTTP and can be proxied
+accordingly.
+
+The only widespread OCSP server implementation (known to this writer) comes as
+part of OpenSSL and aborts on an invalid request, such as connecting to the
+port and then disconnecting. This requires re-entering the passphrase each time
+some random client does this.
+
+The third way is OCSP Stapling; in this, the server using a certificate issued
+by the CA periodically requests an OCSP proof of validity from the OCSP server,
+then serves it up inline as part of the TLS negotiation. This approach adds no
+extra round trips, does not let the CA track users, scales well with number of
+certs issued by the CA and is resilient to temporary OCSP server failures, as
+long as the server starts retrying to fetch an OCSP proof some time before its
+current proof expires. The downside is that it requires server support.
+
+Unless Exim is built with the support disabled, or with GnuTLS earlier than
+version 3.1.3, support for OCSP stapling is included.
+
+There is a global option called tls_ocsp_file. The file specified therein is
+expected to be in DER format, and contain an OCSP proof. Exim will serve it as
+part of the TLS handshake. This option will be re-expanded for SNI, if the 
+tls_certificate option contains "tls_in_sni", as per other TLS options.
+
+Exim does not at this time implement any support for fetching a new OCSP proof.
+The burden is on the administrator to handle this, outside of Exim. The file
+specified should be replaced atomically, so that the contents are always valid.
+Exim will expand the tls_ocsp_file option on each connection, so a new file
+will be handled transparently on the next connection.
+
+When built with OpenSSL Exim will check for a valid next update timestamp in
+the OCSP proof; if not present, or if the proof has expired, it will be
+ignored.
+
+For the client to be able to verify the stapled OCSP the server must also
+supply, in its stapled information, any intermediate certificates for the chain
+leading to the OCSP proof from the signer of the server certificate. There may
+be zero or one such. These intermediate certificates should be added to the
+server OCSP stapling file named by tls_ocsp_file.
+
+Note that the proof only covers the terminal server certificate, not any of the
+chain from CA to it.
+
+There is no current way to staple a proof for a client certificate.
+
+  A helper script "ocsp_fetch.pl" for fetching a proof from a CA
+  OCSP server is supplied.  The server URL may be included in the
+  server certificate, if the CA is helpful.
+
+  One failure mode seen was the OCSP Signer cert expiring before the end
+  of validity of the OCSP proof. The checking done by Exim/OpenSSL
+  noted this as invalid overall, but the re-fetch script did not.
+
+
+41.9 Configuring an Exim client to use TLS
+------------------------------------------
+
+The tls_cipher and tls_peerdn log selectors apply to outgoing SMTP deliveries
+as well as to incoming, the latter one causing logging of the server
+certificate's DN. The remaining client configuration for TLS is all within the 
+smtp transport.
+
+It is not necessary to set any options to have TLS work in the smtp transport.
+If Exim is built with TLS support, and TLS is advertised by a server, the smtp
+transport always tries to start a TLS session. However, this can be prevented
+by setting hosts_avoid_tls (an option of the transport) to a list of server
+hosts for which TLS should not be used.
+
+If you do not want Exim to attempt to send messages unencrypted when an attempt
+to set up an encrypted connection fails in any way, you can set 
+hosts_require_tls to a list of hosts for which encryption is mandatory. For
+those hosts, delivery is always deferred if an encrypted connection cannot be
+set up. If there are any other hosts for the address, they are tried in the
+usual way.
+
+When the server host is not in hosts_require_tls, Exim may try to deliver the
+message unencrypted. It always does this if the response to STARTTLS is a 5xx
+code. For a temporary error code, or for a failure to negotiate a TLS session
+after a success response code, what happens is controlled by the 
+tls_tempfail_tryclear option of the smtp transport. If it is false, delivery to
+this host is deferred, and other hosts (if available) are tried. If it is true,
+Exim attempts to deliver unencrypted after a 4xx response to STARTTLS, and if
+STARTTLS is accepted, but the subsequent TLS negotiation fails, Exim closes the
+current connection (because it is in an unknown state), opens a new one to the
+same host, and then tries the delivery unencrypted.
+
+The tls_certificate and tls_privatekey options of the smtp transport provide
+the client with a certificate, which is passed to the server if it requests it.
+If the server is Exim, it will request a certificate only if tls_verify_hosts
+or tls_try_verify_hosts matches the client.
+
+If the tls_verify_certificates option is set on the smtp transport, it must
+name a file or, for OpenSSL only (not GnuTLS), a directory, that contains a
+collection of expected server certificates. The client verifies the server's
+certificate against this collection, taking into account any revoked
+certificates that are in the list defined by tls_crl. Failure to verify fails
+the TLS connection unless either of the tls_verify_hosts or 
+tls_try_verify_hosts options are set.
+
+The tls_verify_hosts and tls_try_verify_hosts options restrict certificate
+verification to the listed servers. Verification either must or need not
+succeed respectively.
+
+The smtp transport has two OCSP-related options: hosts_require_ocsp; a
+host-list for which a Certificate Status is requested and required for the
+connection to proceed. The default value is empty. hosts_request_ocsp; a
+host-list for which (additionally) a Certificate Status is requested (but not
+necessarily verified). The default value is "*" meaning that requests are made
+unless configured otherwise.
+
+The host(s) should also be in hosts_require_tls, and tls_verify_certificates
+configured for the transport, for OCSP to be relevant.
+
+If tls_require_ciphers is set on the smtp transport, it must contain a list of
+permitted cipher suites. If either of these checks fails, delivery to the
+current host is abandoned, and the smtp transport tries to deliver to
+alternative hosts, if any.
+
+Note: These options must be set in the smtp transport for Exim to use TLS when
+it is operating as a client. Exim does not assume that a server certificate
+(set by the global options of the same name) should also be used when operating
+as a client.
+
+All the TLS options in the smtp transport are expanded before use, with $host
+and $host_address containing the name and address of the server to which the
+client is connected. Forced failure of an expansion causes Exim to behave as if
+the relevant option were unset.
+
+Before an SMTP connection is established, the $tls_out_bits, $tls_out_cipher,
+$tls_out_peerdn and $tls_out_sni variables are emptied. (Until the first
+connection, they contain the values that were set when the message was
+received.) If STARTTLS is subsequently successfully obeyed, these variables are
+set to the relevant values for the outgoing connection.
+
+
+41.10 Use of TLS Server Name Indication
+---------------------------------------
+
+With TLS1.0 or above, there is an extension mechanism by which extra
+information can be included at various points in the protocol. One of these
+extensions, documented in RFC 6066 (and before that RFC 4366) is "Server Name
+Indication", commonly "SNI". This extension is sent by the client in the
+initial handshake, so that the server can examine the servername within and
+possibly choose to use different certificates and keys (and more) for this
+session.
+
+This is analagous to HTTP's "Host:" header, and is the main mechanism by which
+HTTPS-enabled web-sites can be virtual-hosted, many sites to one IP address.
+
+With SMTP to MX, there are the same problems here as in choosing the identity
+against which to validate a certificate: you can't rely on insecure DNS to
+provide the identity which you then cryptographically verify. So this will be
+of limited use in that environment.
+
+With SMTP to Submission, there is a well-defined hostname which clients are
+connecting to and can validate certificates against. Thus clients can choose to
+include this information in the TLS negotiation. If this becomes wide-spread,
+then hosters can choose to present different certificates to different clients.
+Or even negotiate different cipher suites.
+
+The tls_sni option on an SMTP transport is an expanded string; the result, if
+not empty, will be sent on a TLS session as part of the handshake. There's
+nothing more to it. Choosing a sensible value not derived insecurely is the
+only point of caution. The $tls_out_sni variable will be set to this string for
+the lifetime of the client connection (including during authentication).
+
+Except during SMTP client sessions, if $tls_in_sni is set then it is a string
+received from a client. It can be logged with the log_selector item "+tls_sni".
+
+If the string "tls_in_sni" appears in the main section's tls_certificate option
+(prior to expansion) then the following options will be re-expanded during TLS
+session handshake, to permit alternative values to be chosen:
+
+  * tls_certificate
+
+  * tls_crl
+
+  * tls_privatekey
+
+  * tls_verify_certificates
+
+  * tls_verify_certificates
+
+Great care should be taken to deal with matters of case, various injection
+attacks in the string ("../" or SQL), and ensuring that a valid filename can
+always be referenced; it is important to remember that $tls_sni is arbitrary
+unverified data provided prior to authentication.
+
+The Exim developers are proceeding cautiously and so far no other TLS options
+are re-expanded.
+
+When Exim is built againt OpenSSL, OpenSSL must have been built with support
+for TLS Extensions. This holds true for OpenSSL 1.0.0+ and 0.9.8+ with
+enable-tlsext in EXTRACONFIGURE. If you invoke openssl s_client -h and see
+"-servername" in the output, then OpenSSL has support.
+
+When Exim is built against GnuTLS, SNI support is available as of GnuTLS
+0.5.10. (Its presence predates the current API which Exim uses, so if Exim
+built, then you have SNI support).
+
+
+41.11 Multiple messages on the same encrypted TCP/IP connection
+---------------------------------------------------------------
+
+Exim sends multiple messages down the same TCP/IP connection by starting up an
+entirely new delivery process for each message, passing the socket from one
+process to the next. This implementation does not fit well with the use of TLS,
+because there is quite a lot of state information associated with a TLS
+connection, not just a socket identification. Passing all the state information
+to a new process is not feasible. Consequently, Exim shuts down an existing TLS
+session before passing the socket to a new process. The new process may then
+try to start a new TLS session, and if successful, may try to re-authenticate
+if AUTH is in use, before sending the next message.
+
+The RFC is not clear as to whether or not an SMTP session continues in clear
+after TLS has been shut down, or whether TLS may be restarted again later, as
+just described. However, if the server is Exim, this shutdown and
+reinitialization works. It is not known which (if any) other servers operate
+successfully if the client closes a TLS session and continues with unencrypted
+SMTP, but there are certainly some that do not work. For such servers, Exim
+should not pass the socket to another process, because the failure of the
+subsequent attempt to use it would cause Exim to record a temporary host error,
+and delay other deliveries to that host.
+
+To test for this case, Exim sends an EHLO command to the server after closing
+down the TLS session. If this fails in any way, the connection is closed
+instead of being passed to a new delivery process, but no retry information is
+recorded.
+
+There is also a manual override; you can set hosts_nopass_tls on the smtp
+transport to match those hosts for which Exim should not pass connections to
+new processes if TLS has been used.
+
+
+41.12 Certificates and all that
+-------------------------------
+
+In order to understand fully how TLS works, you need to know about
+certificates, certificate signing, and certificate authorities. This is not the
+place to give a tutorial, especially as I do not know very much about it
+myself. Some helpful introduction can be found in the FAQ for the SSL addition
+to Apache, currently at
+
+http://www.modssl.org/docs/2.7/ssl_faq.html#ToC24
+
+Other parts of the modssl documentation are also helpful, and have links to
+further files. Eric Rescorla's book, SSL and TLS, published by Addison-Wesley
+(ISBN 0-201-61598-3), contains both introductory and more in-depth
+descriptions. Some sample programs taken from the book are available from
+
+http://www.rtfm.com/openssl-examples/
+
+
+41.13 Certificate chains
+------------------------
+
+The file named by tls_certificate may contain more than one certificate. This
+is useful in the case where the certificate that is being sent is validated by
+an intermediate certificate which the other end does not have. Multiple
+certificates must be in the correct order in the file. First the host's
+certificate itself, then the first intermediate certificate to validate the
+issuer of the host certificate, then the next intermediate certificate to
+validate the issuer of the first intermediate certificate, and so on, until
+finally (optionally) the root certificate. The root certificate must already be
+trusted by the recipient for validation to succeed, of course, but if it's not
+preinstalled, sending the root certificate along with the rest makes it
+available for the user to install if the receiving end is a client MUA that can
+interact with a user.
+
+Note that certificates using MD5 are unlikely to work on today's Internet; even
+if your libraries allow loading them for use in Exim when acting as a server,
+increasingly clients will not accept such certificates. The error diagnostics
+in such a case can be frustratingly vague.
+
+
+41.14 Self-signed certificates
+------------------------------
+
+You can create a self-signed certificate using the req command provided with
+OpenSSL, like this:
+
+openssl req -x509 -newkey rsa:1024 -keyout file1 -out file2 \
+            -days 9999 -nodes
+
+file1 and file2 can be the same file; the key and the certificate are delimited
+and so can be identified independently. The -days option specifies a period for
+which the certificate is valid. The -nodes option is important: if you do not
+set it, the key is encrypted with a passphrase that you are prompted for, and
+any use that is made of the key causes more prompting for the passphrase. This
+is not helpful if you are going to use this certificate and key in an MTA,
+where prompting is not possible.
+
+NB: we are now past the point where 9999 days takes us past the 32-bit Unix
+epoch. If your system uses unsigned time_t (most do) and is 32-bit, then the
+above command might produce a date in the past. Think carefully about the
+lifetime of the systems you're deploying, and either reduce the duration of the
+certificate or reconsider your platform deployment. (At time of writing,
+reducing the duration is the most likely choice, but the inexorable progression
+of time takes us steadily towards an era where this will not be a sensible
+resolution).
+
+A self-signed certificate made in this way is sufficient for testing, and may
+be adequate for all your requirements if you are mainly interested in
+encrypting transfers, and not in secure identification.
+
+However, many clients require that the certificate presented by the server be a
+user (also called "leaf" or "site") certificate, and not a self-signed
+certificate. In this situation, the self-signed certificate described above
+must be installed on the client host as a trusted root certification authority
+(CA), and the certificate used by Exim must be a user certificate signed with
+that self-signed certificate.
+
+For information on creating self-signed CA certificates and using them to sign
+user certificates, see the General implementation overview chapter of the
+Open-source PKI book, available online at http://ospkibook.sourceforge.net/.
+
+
+
+===============================================================================
+42. ACCESS CONTROL LISTS
+
+Access Control Lists (ACLs) are defined in a separate section of the run time
+configuration file, headed by "begin acl". Each ACL definition starts with a
+name, terminated by a colon. Here is a complete ACL section that contains just
+one very small ACL:
+
+begin acl
+small_acl:
+  accept   hosts = one.host.only
+
+You can have as many lists as you like in the ACL section, and the order in
+which they appear does not matter. The lists are self-terminating.
+
+The majority of ACLs are used to control Exim's behaviour when it receives
+certain SMTP commands. This applies both to incoming TCP/IP connections, and
+when a local process submits a message using SMTP by specifying the -bs option.
+The most common use is for controlling which recipients are accepted in
+incoming messages. In addition, you can define an ACL that is used to check
+local non-SMTP messages. The default configuration file contains an example of
+a realistic ACL for checking RCPT commands. This is discussed in chapter 7.
+
+
+42.1 Testing ACLs
+-----------------
+
+The -bh command line option provides a way of testing your ACL configuration
+locally by running a fake SMTP session with which you interact. The host 
+relay-test.mail-abuse.org provides a service for checking your relaying
+configuration (see section 42.53 for more details).
+
+
+42.2 Specifying when ACLs are used
+----------------------------------
+
+In order to cause an ACL to be used, you have to name it in one of the relevant
+options in the main part of the configuration. These options are: 
+
+    acl_not_smtp       ACL for non-SMTP messages
+    acl_not_smtp_mime  ACL for non-SMTP MIME parts
+    acl_not_smtp_start ACL at start of non-SMTP message
+    acl_smtp_auth      ACL for AUTH
+    acl_smtp_connect   ACL for start of SMTP connection
+    acl_smtp_data      ACL after DATA is complete
+    acl_smtp_data_prdr ACL for each recipient, after DATA is complete
+    acl_smtp_etrn      ACL for ETRN
+    acl_smtp_expn      ACL for EXPN
+    acl_smtp_helo      ACL for HELO or EHLO
+    acl_smtp_mail      ACL for MAIL
+    acl_smtp_mailauth  ACL for the AUTH parameter of MAIL
+    acl_smtp_mime      ACL for content-scanning MIME parts
+    acl_smtp_notquit   ACL for non-QUIT terminations
+    acl_smtp_predata   ACL at start of DATA command
+    acl_smtp_quit      ACL for QUIT
+    acl_smtp_rcpt      ACL for RCPT
+    acl_smtp_starttls  ACL for STARTTLS
+    acl_smtp_vrfy      ACL for VRFY
+
+For example, if you set
+
+acl_smtp_rcpt = small_acl
+
+the little ACL defined above is used whenever Exim receives a RCPT command in
+an SMTP dialogue. The majority of policy tests on incoming messages can be done
+when RCPT commands arrive. A rejection of RCPT should cause the sending MTA to
+give up on the recipient address contained in the RCPT command, whereas
+rejection at other times may cause the client MTA to keep on trying to deliver
+the message. It is therefore recommended that you do as much testing as
+possible at RCPT time.
+
+
+42.3 The non-SMTP ACLs
+----------------------
+
+The non-SMTP ACLs apply to all non-interactive incoming messages, that is, they
+apply to batched SMTP as well as to non-SMTP messages. (Batched SMTP is not
+really SMTP.) Many of the ACL conditions (for example, host tests, and tests on
+the state of the SMTP connection such as encryption and authentication) are not
+relevant and are forbidden in these ACLs. However, the sender and recipients
+are known, so the senders and sender_domains conditions and the $sender_address
+and $recipients variables can be used. Variables such as $authenticated_sender
+are also available. You can specify added header lines in any of these ACLs.
+
+The acl_not_smtp_start ACL is run right at the start of receiving a non-SMTP
+message, before any of the message has been read. (This is the analogue of the 
+acl_smtp_predata ACL for SMTP input.) In the case of batched SMTP input, it
+runs after the DATA command has been reached. The result of this ACL is
+ignored; it cannot be used to reject a message. If you really need to, you
+could set a value in an ACL variable here and reject based on that in the 
+acl_not_smtp ACL. However, this ACL can be used to set controls, and in
+particular, it can be used to set
+
+control = suppress_local_fixups
+
+This cannot be used in the other non-SMTP ACLs because by the time they are
+run, it is too late.
+
+The acl_not_smtp_mime ACL is available only when Exim is compiled with the
+content-scanning extension. For details, see chapter 43.
+
+The acl_not_smtp ACL is run just before the local_scan() function. Any kind of
+rejection is treated as permanent, because there is no way of sending a
+temporary error for these kinds of message.
+
+
+42.4 The SMTP connect ACL
+-------------------------
+
+The ACL test specified by acl_smtp_connect happens at the start of an SMTP
+session, after the test specified by host_reject_connection (which is now an
+anomaly) and any TCP Wrappers testing (if configured). If the connection is
+accepted by an accept verb that has a message modifier, the contents of the
+message override the banner message that is otherwise specified by the 
+smtp_banner option.
+
+
+42.5 The EHLO/HELO ACL
+----------------------
+
+The ACL test specified by acl_smtp_helo happens when the client issues an EHLO
+or HELO command, after the tests specified by helo_accept_junk_hosts, 
+helo_allow_chars, helo_verify_hosts, and helo_try_verify_hosts. Note that a
+client may issue more than one EHLO or HELO command in an SMTP session, and
+indeed is required to issue a new EHLO or HELO after successfully setting up
+encryption following a STARTTLS command.
+
+If the command is accepted by an accept verb that has a message modifier, the
+message may not contain more than one line (it will be truncated at the first
+newline and a panic logged if it does). Such a message cannot affect the EHLO
+options that are listed on the second and subsequent lines of an EHLO response.
+
+
+42.6 The DATA ACLs
+------------------
+
+Two ACLs are associated with the DATA command, because it is two-stage command,
+with two responses being sent to the client. When the DATA command is received,
+the ACL defined by acl_smtp_predata is obeyed. This gives you control after all
+the RCPT commands, but before the message itself is received. It offers the
+opportunity to give a negative response to the DATA command before the data is
+transmitted. Header lines added by MAIL or RCPT ACLs are not visible at this
+time, but any that are defined here are visible when the acl_smtp_data ACL is
+run.
+
+You cannot test the contents of the message, for example, to verify addresses
+in the headers, at RCPT time or when the DATA command is received. Such tests
+have to appear in the ACL that is run after the message itself has been
+received, before the final response to the DATA command is sent. This is the
+ACL specified by acl_smtp_data, which is the second ACL that is associated with
+the DATA command.
+
+For both of these ACLs, it is not possible to reject individual recipients. An
+error response rejects the entire message. Unfortunately, it is known that some
+MTAs do not treat hard (5xx) responses to the DATA command (either before or
+after the data) correctly - they keep the message on their queues and try again
+later, but that is their problem, though it does waste some of your resources.
+
+The acl_smtp_data ACL is run after the acl_smtp_data_prdr, the acl_smtp_dkim
+and the acl_smtp_mime ACLs.
+
+
+42.7 The SMTP DKIM ACL
+----------------------
+
+The acl_smtp_dkim ACL is available only when Exim is compiled with DKIM support
+enabled (which is the default).
+
+The ACL test specified by acl_smtp_dkim happens after a message has been
+received, and is executed for each DKIM signature found in a message. If not
+otherwise specified, the default action is to accept.
+
+This ACL is evaluated before acl_smtp_mime and acl_smtp_data.
+
+For details on the operation of DKIM, see chapter 56.
+
+
+42.8 The SMTP MIME ACL
+----------------------
+
+The acl_smtp_mime option is available only when Exim is compiled with the
+content-scanning extension. For details, see chapter 43.
+
+This ACL is evaluated after acl_smtp_dkim but before acl_smtp_data.
+
+
+42.9 The SMTP PRDR ACL
+----------------------
+
+The acl_smtp_data_prdr ACL is available only when Exim is compiled with PRDR
+support enabled (which is the default). It becomes active only when the PRDR
+feature is negotiated between client and server for a message, and more than
+one recipient has been accepted.
+
+The ACL test specfied by acl_smtp_data_prdr happens after a message has been
+recieved, and is executed for each recipient of the message. The test may
+accept or deny for inividual recipients. The acl_smtp_data will still be called
+after this ACL and can reject the message overall, even if this ACL has
+accepted it for some or all recipients.
+
+PRDR may be used to support per-user content filtering. Without it one must
+defer any recipient after the first that has a different content-filter
+configuration. With PRDR, the RCPT-time check for this can be disabled when the
+MAIL-time $smtp_command included "PRDR". Any required difference in behaviour
+of the main DATA-time ACL should however depend on the PRDR-time ACL having
+run, as Exim will avoid doing so in some situations (eg. single-recipient
+mails).
+
+See also the prdr_enable global option and the hosts_try_prdr smtp transport
+option.
+
+This ACL is evaluated after acl_smtp_dkim but before acl_smtp_data. If the ACL
+is not defined, processing completes as if the feature was not requested by the
+client.
+
+
+42.10 The QUIT ACL
+------------------
+
+The ACL for the SMTP QUIT command is anomalous, in that the outcome of the ACL
+does not affect the response code to QUIT, which is always 221. Thus, the ACL
+does not in fact control any access. For this reason, the only verbs that are
+permitted are accept and warn.
+
+This ACL can be used for tasks such as custom logging at the end of an SMTP
+session. For example, you can use ACL variables in other ACLs to count
+messages, recipients, etc., and log the totals at QUIT time using one or more 
+logwrite modifiers on a warn verb.
+
+Warning: Only the $acl_cx variables can be used for this, because the $acl_mx
+variables are reset at the end of each incoming message.
+
+You do not need to have a final accept, but if you do, you can use a message
+modifier to specify custom text that is sent as part of the 221 response to
+QUIT.
+
+This ACL is run only for a "normal" QUIT. For certain kinds of disastrous
+failure (for example, failure to open a log file, or when Exim is bombing out
+because it has detected an unrecoverable error), all SMTP commands from the
+client are given temporary error responses until QUIT is received or the
+connection is closed. In these special cases, the QUIT ACL does not run.
+
+
+42.11 The not-QUIT ACL
+----------------------
+
+The not-QUIT ACL, specified by acl_smtp_notquit, is run in most cases when an
+SMTP session ends without sending QUIT. However, when Exim itself is in bad
+trouble, such as being unable to write to its log files, this ACL is not run,
+because it might try to do things (such as write to log files) that make the
+situation even worse.
+
+Like the QUIT ACL, this ACL is provided to make it possible to do customized
+logging or to gather statistics, and its outcome is ignored. The delay modifier
+is forbidden in this ACL, and the only permitted verbs are accept and warn.
+
+When the not-QUIT ACL is running, the variable $smtp_notquit_reason is set to a
+string that indicates the reason for the termination of the SMTP connection.
+The possible values are:
+
+    "acl-drop"              Another ACL issued a drop command
+    "bad-commands"          Too many unknown or non-mail commands
+    "command-timeout"       Timeout while reading SMTP commands
+    "connection-lost"       The SMTP connection has been lost
+    "data-timeout"          Timeout while reading message data
+    "local-scan-error"      The local_scan() function crashed
+    "local-scan-timeout"    The local_scan() function timed out
+    "signal-exit"           SIGTERM or SIGINT
+    "synchronization-error" SMTP synchronization error
+    "tls-failed"            TLS failed to start
+
+In most cases when an SMTP connection is closed without having received QUIT,
+Exim sends an SMTP response message before actually closing the connection.
+With the exception of the "acl-drop" case, the default message can be
+overridden by the message modifier in the not-QUIT ACL. In the case of a drop
+verb in another ACL, it is the message from the other ACL that is used.
+
+
+42.12 Finding an ACL to use
+---------------------------
+
+The value of an acl_smtp_xxx option is expanded before use, so you can use
+different ACLs in different circumstances. For example,
+
+acl_smtp_rcpt = ${if ={25}{$interface_port} \
+                     {acl_check_rcpt} {acl_check_rcpt_submit} }
+
+In the default configuration file there are some example settings for providing
+an RFC 4409 message submission service on port 587 and a non-standard "smtps"
+service on port 465. You can use a string expansion like this to choose an ACL
+for MUAs on these ports which is more appropriate for this purpose than the
+default ACL on port 25.
+
+The expanded string does not have to be the name of an ACL in the configuration
+file; there are other possibilities. Having expanded the string, Exim searches
+for an ACL as follows:
+
+  * If the string begins with a slash, Exim uses it as a file name, and reads
+    its contents as an ACL. The lines are processed in the same way as lines in
+    the Exim configuration file. In particular, continuation lines are
+    supported, blank lines are ignored, as are lines whose first non-whitespace
+    character is "#". If the file does not exist or cannot be read, an error
+    occurs (typically causing a temporary failure of whatever caused the ACL to
+    be run). For example:
+
+    acl_smtp_data = /etc/acls/\
+      ${lookup{$sender_host_address}lsearch\
+      {/etc/acllist}{$value}{default}}
+
+    This looks up an ACL file to use on the basis of the host's IP address,
+    falling back to a default if the lookup fails. If an ACL is successfully
+    read from a file, it is retained in memory for the duration of the Exim
+    process, so that it can be re-used without having to re-read the file.
+
+  * If the string does not start with a slash, and does not contain any spaces,
+    Exim searches the ACL section of the configuration for an ACL whose name
+    matches the string.
+
+  * If no named ACL is found, or if the string contains spaces, Exim parses the
+    string as an inline ACL. This can save typing in cases where you just want
+    to have something like
+
+    acl_smtp_vrfy = accept
+
+    in order to allow free use of the VRFY command. Such a string may contain
+    newlines; it is processed in the same way as an ACL that is read from a
+    file.
+
+
+42.13 ACL return codes
+----------------------
+
+Except for the QUIT ACL, which does not affect the SMTP return code (see
+section 42.10 above), the result of running an ACL is either "accept" or
+"deny", or, if some test cannot be completed (for example, if a database is
+down), "defer". These results cause 2xx, 5xx, and 4xx return codes,
+respectively, to be used in the SMTP dialogue. A fourth return, "error", occurs
+when there is an error such as invalid syntax in the ACL. This also causes a 4
+xx return code.
+
+For the non-SMTP ACL, "defer" and "error" are treated in the same way as
+"deny", because there is no mechanism for passing temporary errors to the
+submitters of non-SMTP messages.
+
+ACLs that are relevant to message reception may also return "discard". This has
+the effect of "accept", but causes either the entire message or an individual
+recipient address to be discarded. In other words, it is a blackholing
+facility. Use it with care.
+
+If the ACL for MAIL returns "discard", all recipients are discarded, and no ACL
+is run for subsequent RCPT commands. The effect of "discard" in a RCPT ACL is
+to discard just the one recipient address. If there are no recipients left when
+the message's data is received, the DATA ACL is not run. A "discard" return
+from the DATA or the non-SMTP ACL discards all the remaining recipients. The
+"discard" return is not permitted for the acl_smtp_predata ACL.
+
+The local_scan() function is always run, even if there are no remaining
+recipients; it may create new recipients.
+
+
+42.14 Unset ACL options
+-----------------------
+
+The default actions when any of the acl_xxx options are unset are not all the
+same. Note: These defaults apply only when the relevant ACL is not defined at
+all. For any defined ACL, the default action when control reaches the end of
+the ACL statements is "deny".
+
+For acl_smtp_quit and acl_not_smtp_start there is no default because these two
+are ACLs that are used only for their side effects. They cannot be used to
+accept or reject anything.
+
+For acl_not_smtp, acl_smtp_auth, acl_smtp_connect, acl_smtp_data, acl_smtp_helo
+, acl_smtp_mail, acl_smtp_mailauth, acl_smtp_mime, acl_smtp_predata, and 
+acl_smtp_starttls, the action when the ACL is not defined is "accept".
+
+For the others (acl_smtp_etrn, acl_smtp_expn, acl_smtp_rcpt, and acl_smtp_vrfy
+), the action when the ACL is not defined is "deny". This means that 
+acl_smtp_rcpt must be defined in order to receive any messages over an SMTP
+connection. For an example, see the ACL in the default configuration file.
+
+
+42.15 Data for message ACLs
+---------------------------
+
+When a MAIL or RCPT ACL, or either of the DATA ACLs, is running, the variables
+that contain information about the host and the message's sender (for example,
+$sender_host_address and $sender_address) are set, and can be used in ACL
+statements. In the case of RCPT (but not MAIL or DATA), $domain and $local_part
+are set from the argument address. The entire SMTP command is available in
+$smtp_command.
+
+When an ACL for the AUTH parameter of MAIL is running, the variables that
+contain information about the host are set, but $sender_address is not yet set.
+Section 33.2 contains a discussion of this parameter and how it is used.
+
+The $message_size variable is set to the value of the SIZE parameter on the
+MAIL command at MAIL, RCPT and pre-data time, or to -1 if that parameter is not
+given. The value is updated to the true message size by the time the final DATA
+ACL is run (after the message data has been received).
+
+The $rcpt_count variable increases by one for each RCPT command received. The
+$recipients_count variable increases by one each time a RCPT command is
+accepted, so while an ACL for RCPT is being processed, it contains the number
+of previously accepted recipients. At DATA time (for both the DATA ACLs),
+$rcpt_count contains the total number of RCPT commands, and $recipients_count
+contains the total number of accepted recipients.
+
+
+42.16 Data for non-message ACLs
+-------------------------------
+
+When an ACL is being run for AUTH, EHLO, ETRN, EXPN, HELO, STARTTLS, or VRFY,
+the remainder of the SMTP command line is placed in $smtp_command_argument, and
+the entire SMTP command is available in $smtp_command. These variables can be
+tested using a condition condition. For example, here is an ACL for use with
+AUTH, which insists that either the session is encrypted, or the CRAM-MD5
+authentication method is used. In other words, it does not permit
+authentication methods that use cleartext passwords on unencrypted connections.
+
+acl_check_auth:
+  accept encrypted = *
+  accept condition = ${if eq{${uc:$smtp_command_argument}}\
+                     {CRAM-MD5}}
+  deny   message   = TLS encryption or CRAM-MD5 required
+
+(Another way of applying this restriction is to arrange for the authenticators
+that use cleartext passwords not to be advertised when the connection is not
+encrypted. You can use the generic server_advertise_condition authenticator
+option to do this.)
+
+
+42.17 Format of an ACL
+----------------------
+
+An individual ACL consists of a number of statements. Each statement starts
+with a verb, optionally followed by a number of conditions and "modifiers".
+Modifiers can change the way the verb operates, define error and log messages,
+set variables, insert delays, and vary the processing of accepted messages.
+
+If all the conditions are met, the verb is obeyed. The same condition may be
+used (with different arguments) more than once in the same statement. This
+provides a means of specifying an "and" conjunction between conditions. For
+example:
+
+deny  dnslists = list1.example
+dnslists = list2.example
+
+If there are no conditions, the verb is always obeyed. Exim stops evaluating
+the conditions and modifiers when it reaches a condition that fails. What
+happens then depends on the verb (and in one case, on a special modifier). Not
+all the conditions make sense at every testing point. For example, you cannot
+test a sender address in the ACL that is run for a VRFY command.
+
+
+42.18 ACL verbs
+---------------
+
+The ACL verbs are as follows:
+
+  * accept: If all the conditions are met, the ACL returns "accept". If any of
+    the conditions are not met, what happens depends on whether endpass appears
+    among the conditions (for syntax see below). If the failing condition is
+    before endpass, control is passed to the next ACL statement; if it is after
+    endpass, the ACL returns "deny". Consider this statement, used to check a
+    RCPT command:
+
+    accept domains = +local_domains
+    endpass
+    verify = recipient
+
+    If the recipient domain does not match the domains condition, control
+    passes to the next statement. If it does match, the recipient is verified,
+    and the command is accepted if verification succeeds. However, if
+    verification fails, the ACL yields "deny", because the failing condition is
+    after endpass.
+
+    The endpass feature has turned out to be confusing to many people, so its
+    use is not recommended nowadays. It is always possible to rewrite an ACL so
+    that endpass is not needed, and it is no longer used in the default
+    configuration.
+
+    If a message modifier appears on an accept statement, its action depends on
+    whether or not endpass is present. In the absence of endpass (when an 
+    accept verb either accepts or passes control to the next statement), 
+    message can be used to vary the message that is sent when an SMTP command
+    is accepted. For example, in a RCPT ACL you could have:
+
+    accept  <some conditions>
+            message = OK, I will allow you through today
+
+    You can specify an SMTP response code, optionally followed by an "extended
+    response code" at the start of the message, but the first digit must be the
+    same as would be sent by default, which is 2 for an accept verb.
+
+    If endpass is present in an accept statement, message specifies an error
+    message that is used when access is denied. This behaviour is retained for
+    backward compatibility, but current "best practice" is to avoid the use of 
+    endpass.
+
+  * defer: If all the conditions are true, the ACL returns "defer" which, in an
+    SMTP session, causes a 4xx response to be given. For a non-SMTP ACL, defer
+    is the same as deny, because there is no way of sending a temporary error.
+    For a RCPT command, defer is much the same as using a redirect router and
+    ":defer:" while verifying, but the defer verb can be used in any ACL, and
+    even for a recipient it might be a simpler approach.
+
+  * deny: If all the conditions are met, the ACL returns "deny". If any of the
+    conditions are not met, control is passed to the next ACL statement. For
+    example,
+
+    deny dnslists = blackholes.mail-abuse.org
+
+    rejects commands from hosts that are on a DNS black list.
+
+  * discard: This verb behaves like accept, except that it returns "discard"
+    from the ACL instead of "accept". It is permitted only on ACLs that are
+    concerned with receiving messages. When all the conditions are true, the
+    sending entity receives a "success" response. However, discard causes
+    recipients to be discarded. If it is used in an ACL for RCPT, just the one
+    recipient is discarded; if used for MAIL, DATA or in the non-SMTP ACL, all
+    the message's recipients are discarded. Recipients that are discarded
+    before DATA do not appear in the log line when the received_recipients log
+    selector is set.
+
+    If the log_message modifier is set when discard operates, its contents are
+    added to the line that is automatically written to the log. The message
+    modifier operates exactly as it does for accept.
+
+  * drop: This verb behaves like deny, except that an SMTP connection is
+    forcibly closed after the 5xx error message has been sent. For example:
+
+    drop   message   = I don't take more than 20 RCPTs
+           condition = ${if > {$rcpt_count}{20}}
+
+    There is no difference between deny and drop for the connect-time ACL. The
+    connection is always dropped after sending a 550 response.
+
+  * require: If all the conditions are met, control is passed to the next ACL
+    statement. If any of the conditions are not met, the ACL returns "deny".
+    For example, when checking a RCPT command,
+
+    require message = Sender did not verify
+            verify  = sender
+
+    passes control to subsequent statements only if the message's sender can be
+    verified. Otherwise, it rejects the command. Note the positioning of the 
+    message modifier, before the verify condition. The reason for this is
+    discussed in section 42.20.
+
+  * warn: If all the conditions are true, a line specified by the log_message
+    modifier is written to Exim's main log. Control always passes to the next
+    ACL statement. If any condition is false, the log line is not written. If
+    an identical log line is requested several times in the same message, only
+    one copy is actually written to the log. If you want to force duplicates to
+    be written, use the logwrite modifier instead.
+
+    If log_message is not present, a warn verb just checks its conditions and
+    obeys any "immediate" modifiers (such as control, set, logwrite, add_header
+    , and remove_header) that appear before the first failing condition. There
+    is more about adding header lines in section 42.24.
+
+    If any condition on a warn statement cannot be completed (that is, there is
+    some sort of defer), the log line specified by log_message is not written.
+    This does not include the case of a forced failure from a lookup, which is
+    considered to be a successful completion. After a defer, no further
+    conditions or modifiers in the warn statement are processed. The incident
+    is logged, and the ACL continues to be processed, from the next statement
+    onwards.
+
+    When one of the warn conditions is an address verification that fails, the
+    text of the verification failure message is in $acl_verify_message. If you
+    want this logged, you must set it up explicitly. For example:
+
+    warn   !verify = sender
+           log_message = sender verify failed: $acl_verify_message
+
+At the end of each ACL there is an implicit unconditional deny.
+
+As you can see from the examples above, the conditions and modifiers are
+written one to a line, with the first one on the same line as the verb, and
+subsequent ones on following lines. If you have a very long condition, you can
+continue it onto several physical lines by the usual backslash continuation
+mechanism. It is conventional to align the conditions vertically.
+
+
+42.19 ACL variables
+-------------------
+
+There are some special variables that can be set during ACL processing. They
+can be used to pass information between different ACLs, different invocations
+of the same ACL in the same SMTP connection, and between ACLs and the routers,
+transports, and filters that are used to deliver a message. The names of these
+variables must begin with $acl_c or $acl_m, followed either by a digit or an
+underscore, but the remainder of the name can be any sequence of alphanumeric
+characters and underscores that you choose. There is no limit on the number of
+ACL variables. The two sets act as follows:
+
+  * The values of those variables whose names begin with $acl_c persist
+    throughout an SMTP connection. They are never reset. Thus, a value that is
+    set while receiving one message is still available when receiving the next
+    message on the same SMTP connection.
+
+  * The values of those variables whose names begin with $acl_m persist only
+    while a message is being received. They are reset afterwards. They are also
+    reset by MAIL, RSET, EHLO, HELO, and after starting up a TLS session.
+
+When a message is accepted, the current values of all the ACL variables are
+preserved with the message and are subsequently made available at delivery
+time. The ACL variables are set by a modifier called set. For example:
+
+accept hosts = whatever
+       set acl_m4 = some value
+accept authenticated = *
+       set acl_c_auth = yes
+
+Note: A leading dollar sign is not used when naming a variable that is to be
+set. If you want to set a variable without taking any action, you can use a 
+warn verb without any other modifiers or conditions.
+
+What happens if a syntactically valid but undefined ACL variable is referenced
+depends on the setting of the strict_acl_vars option. If it is false (the
+default), an empty string is substituted; if it is true, an error is generated.
+
+Versions of Exim before 4.64 have a limited set of numbered variables, but
+their names are compatible, so there is no problem with upgrading.
+
+
+42.20 Condition and modifier processing
+---------------------------------------
+
+An exclamation mark preceding a condition negates its result. For example:
+
+deny   domains = *.dom.example
+      !verify  = recipient
+
+causes the ACL to return "deny" if the recipient domain ends in dom.example and
+the recipient address cannot be verified. Sometimes negation can be used on the
+right-hand side of a condition. For example, these two statements are
+equivalent:
+
+deny  hosts = !192.168.3.4
+deny !hosts =  192.168.3.4
+
+However, for many conditions (verify being a good example), only left-hand side
+negation of the whole condition is possible.
+
+The arguments of conditions and modifiers are expanded. A forced failure of an
+expansion causes a condition to be ignored, that is, it behaves as if the
+condition is true. Consider these two statements:
+
+accept  senders = ${lookup{$host_name}lsearch\
+                  {/some/file}{$value}fail}
+accept  senders = ${lookup{$host_name}lsearch\
+                  {/some/file}{$value}{}}
+
+Each attempts to look up a list of acceptable senders. If the lookup succeeds,
+the returned list is searched, but if the lookup fails the behaviour is
+different in the two cases. The fail in the first statement causes the
+condition to be ignored, leaving no further conditions. The accept verb
+therefore succeeds. The second statement, however, generates an empty list when
+the lookup fails. No sender can match an empty list, so the condition fails,
+and therefore the accept also fails.
+
+ACL modifiers appear mixed in with conditions in ACL statements. Some of them
+specify actions that are taken as the conditions for a statement are checked;
+others specify text for messages that are used when access is denied or a
+warning is generated. The control modifier affects the way an incoming message
+is handled.
+
+The positioning of the modifiers in an ACL statement is important, because the
+processing of a verb ceases as soon as its outcome is known. Only those
+modifiers that have already been encountered will take effect. For example,
+consider this use of the message modifier:
+
+require message = Can't verify sender
+        verify  = sender
+        message = Can't verify recipient
+        verify  = recipient
+        message = This message cannot be used
+
+If sender verification fails, Exim knows that the result of the statement is
+"deny", so it goes no further. The first message modifier has been seen, so its
+text is used as the error message. If sender verification succeeds, but
+recipient verification fails, the second message is used. If recipient
+verification succeeds, the third message becomes "current", but is never used
+because there are no more conditions to cause failure.
+
+For the deny verb, on the other hand, it is always the last message modifier
+that is used, because all the conditions must be true for rejection to happen.
+Specifying more than one message modifier does not make sense, and the message
+can even be specified after all the conditions. For example:
+
+deny   hosts = ...
+      !senders = *@my.domain.example
+       message = Invalid sender from client host
+
+The "deny" result does not happen until the end of the statement is reached, by
+which time Exim has set up the message.
+
+
+42.21 ACL modifiers
+-------------------
+
+The ACL modifiers are as follows:
+
+add_header = <text>
+
+    This modifier specifies one or more header lines that are to be added to an
+    incoming message, assuming, of course, that the message is ultimately
+    accepted. For details, see section 42.24.
+
+continue = <text>
+
+    This modifier does nothing of itself, and processing of the ACL always
+    continues with the next condition or modifier. The value of continue is in
+    the side effects of expanding its argument. Typically this could be used to
+    update a database. It is really just a syntactic tidiness, to avoid having
+    to write rather ugly lines like this:
+
+    condition = ${if eq{0}{<some expansion>}{true}{true}}
+
+    Instead, all you need is
+
+    continue = <some expansion>
+
+control = <text>
+
+    This modifier affects the subsequent processing of the SMTP connection or
+    of an incoming message that is accepted. The effect of the first type of
+    control lasts for the duration of the connection, whereas the effect of the
+    second type lasts only until the current message has been received. The
+    message-specific controls always apply to the whole message, not to
+    individual recipients, even if the control modifier appears in a RCPT ACL.
+
+    As there are now quite a few controls that can be applied, they are
+    described separately in section 42.22. The control modifier can be used in
+    several different ways. For example:
+
+      * It can be at the end of an accept statement:
+
+            accept  ...some conditions
+                    control = queue_only
+
+        In this case, the control is applied when this statement yields
+        "accept", in other words, when the conditions are all true.
+
+      * It can be in the middle of an accept statement:
+
+            accept  ...some conditions...
+                    control = queue_only
+                    ...some more conditions...
+
+        If the first set of conditions are true, the control is applied, even
+        if the statement does not accept because one of the second set of
+        conditions is false. In this case, some subsequent statement must yield
+        "accept" for the control to be relevant.
+
+      * It can be used with warn to apply the control, leaving the decision
+        about accepting or denying to a subsequent verb. For example:
+
+            warn    ...some conditions...
+                    control = freeze
+            accept  ...
+
+        This example of warn does not contain message, log_message, or logwrite
+        , so it does not add anything to the message and does not write a log
+        entry.
+
+      * If you want to apply a control unconditionally, you can use it with a 
+        require verb. For example:
+
+            require  control = no_multiline_responses
+
+delay = <time>
+
+    This modifier may appear in any ACL except notquit. It causes Exim to wait
+    for the time interval before proceeding. However, when testing Exim using
+    the -bh option, the delay is not actually imposed (an appropriate message
+    is output instead). The time is given in the usual Exim notation, and the
+    delay happens as soon as the modifier is processed. In an SMTP session,
+    pending output is flushed before the delay is imposed.
+
+    Like control, delay can be used with accept or deny, for example:
+
+    deny    ...some conditions...
+            delay = 30s
+
+    The delay happens if all the conditions are true, before the statement
+    returns "deny". Compare this with:
+
+    deny    delay = 30s
+            ...some conditions...
+
+    which waits for 30s before processing the conditions. The delay modifier
+    can also be used with warn and together with control:
+
+    warn    ...some conditions...
+            delay = 2m
+            control = freeze
+    accept  ...
+
+    If delay is encountered when the SMTP PIPELINING extension is in use,
+    responses to several commands are no longer buffered and sent in one packet
+    (as they would normally be) because all output is flushed before imposing
+    the delay. This optimization is disabled so that a number of small delays
+    do not appear to the client as one large aggregated delay that might
+    provoke an unwanted timeout. You can, however, disable output flushing for 
+    delay by using a control modifier to set no_delay_flush.
+
+endpass
+
+    This modifier, which has no argument, is recognized only in accept and 
+    discard statements. It marks the boundary between the conditions whose
+    failure causes control to pass to the next statement, and the conditions
+    whose failure causes the ACL to return "deny". This concept has proved to
+    be confusing to some people, so the use of endpass is no longer recommended
+    as "best practice". See the description of accept above for more details.
+
+log_message = <text>
+
+    This modifier sets up a message that is used as part of the log message if
+    the ACL denies access or a warn statement's conditions are true. For
+    example:
+
+    require log_message = wrong cipher suite $tls_in_cipher
+            encrypted   = DES-CBC3-SHA
+
+    log_message is also used when recipients are discarded by discard. For
+    example:
+
+    discard <some conditions>
+            log_message = Discarded $local_part@$domain because...
+
+    When access is denied, log_message adds to any underlying error message
+    that may exist because of a condition failure. For example, while verifying
+    a recipient address, a :fail: redirection might have already set up a
+    message.
+
+    The message may be defined before the conditions to which it applies,
+    because the string expansion does not happen until Exim decides that access
+    is to be denied. This means that any variables that are set by the
+    condition are available for inclusion in the message. For example, the
+    $dnslist_<xxx> variables are set after a DNS black list lookup succeeds. If
+    the expansion of log_message fails, or if the result is an empty string,
+    the modifier is ignored.
+
+    If you want to use a warn statement to log the result of an address
+    verification, you can use $acl_verify_message to include the verification
+    error message.
+
+    If log_message is used with a warn statement, "Warning:" is added to the
+    start of the logged message. If the same warning log message is requested
+    more than once while receiving a single email message, only one copy is
+    actually logged. If you want to log multiple copies, use logwrite instead
+    of log_message. In the absence of log_message and logwrite, nothing is
+    logged for a successful warn statement.
+
+    If log_message is not present and there is no underlying error message (for
+    example, from the failure of address verification), but message is present,
+    the message text is used for logging rejections. However, if any text for
+    logging contains newlines, only the first line is logged. In the absence of
+    both log_message and message, a default built-in message is used for
+    logging rejections.
+
+log_reject_target = <log name list>
+
+    This modifier makes it possible to specify which logs are used for messages
+    about ACL rejections. Its argument is a colon-separated list of words that
+    can be "main", "reject", or "panic". The default is "main:reject". The list
+    may be empty, in which case a rejection is not logged at all. For example,
+    this ACL fragment writes no logging information when access is denied:
+
+    deny <some conditions>
+         log_reject_target =
+
+    This modifier can be used in SMTP and non-SMTP ACLs. It applies to both
+    permanent and temporary rejections. Its effect lasts for the rest of the
+    current ACL.
+
+logwrite = <text>
+
+    This modifier writes a message to a log file as soon as it is encountered
+    when processing an ACL. (Compare log_message, which, except in the case of 
+    warn and discard, is used only if the ACL statement denies access.) The 
+    logwrite modifier can be used to log special incidents in ACLs. For
+    example:
+
+    accept <some special conditions>
+           control  = freeze
+           logwrite = froze message because ...
+
+    By default, the message is written to the main log. However, it may begin
+    with a colon, followed by a comma-separated list of log names, and then
+    another colon, to specify exactly which logs are to be written. For
+    example:
+
+    logwrite = :main,reject: text for main and reject logs
+    logwrite = :panic: text for panic log only
+
+message = <text>
+
+    This modifier sets up a text string that is expanded and used as a response
+    message when an ACL statement terminates the ACL with an "accept", "deny",
+    or "defer" response. (In the case of the accept and discard verbs, there is
+    some complication if endpass is involved; see the description of accept for
+    details.)
+
+    The expansion of the message happens at the time Exim decides that the ACL
+    is to end, not at the time it processes message. If the expansion fails, or
+    generates an empty string, the modifier is ignored. Here is an example
+    where message must be specified first, because the ACL ends with a
+    rejection if the hosts condition fails:
+
+    require  message = Host not recognized
+             hosts = 10.0.0.0/8
+
+    (Once a condition has failed, no further conditions or modifiers are
+    processed.)
+
+    For ACLs that are triggered by SMTP commands, the message is returned as
+    part of the SMTP response. The use of message with accept (or discard) is
+    meaningful only for SMTP, as no message is returned when a non-SMTP message
+    is accepted. In the case of the connect ACL, accepting with a message
+    modifier overrides the value of smtp_banner. For the EHLO/HELO ACL, a
+    customized accept message may not contain more than one line (otherwise it
+    will be truncated at the first newline and a panic logged), and it cannot
+    affect the EHLO options.
+
+    When SMTP is involved, the message may begin with an overriding response
+    code, consisting of three digits optionally followed by an "extended
+    response code" of the form n.n.n, each code being followed by a space. For
+    example:
+
+    deny  message = 599 1.2.3 Host not welcome
+          hosts = 192.168.34.0/24
+
+    The first digit of the supplied response code must be the same as would be
+    sent by default. A panic occurs if it is not. Exim uses a 550 code when it
+    denies access, but for the predata ACL, note that the default success code
+    is 354, not 2xx.
+
+    Notwithstanding the previous paragraph, for the QUIT ACL, unlike the
+    others, the message modifier cannot override the 221 response code.
+
+    The text in a message modifier is literal; any quotes are taken as
+    literals, but because the string is expanded, backslash escapes are
+    processed anyway. If the message contains newlines, this gives rise to a
+    multi-line SMTP response.
+
+    If message is used on a statement that verifies an address, the message
+    specified overrides any message that is generated by the verification
+    process. However, the original message is available in the variable
+    $acl_verify_message, so you can incorporate it into your message if you
+    wish. In particular, if you want the text from :fail: items in redirect
+    routers to be passed back as part of the SMTP response, you should either
+    not use a message modifier, or make use of $acl_verify_message.
+
+    For compatibility with previous releases of Exim, a message modifier that
+    is used with a warn verb behaves in a similar way to the add_header
+    modifier, but this usage is now deprecated. However, message acts only when
+    all the conditions are true, wherever it appears in an ACL command, whereas
+    add_header acts as soon as it is encountered. If message is used with warn
+    in an ACL that is not concerned with receiving a message, it has no effect.
+
+remove_header = <text>
+
+    This modifier specifies one or more header names in a colon-separated list
+    that are to be removed from an incoming message, assuming, of course, that
+    the message is ultimately accepted. For details, see section 42.25.
+
+set <acl_name> = <value>
+
+    This modifier puts a value into one of the ACL variables (see section 42.19
+    ).
+
+udpsend = <parameters>
+
+    This modifier sends a UDP packet, for purposes such as statistics
+    collection or behaviour monitoring. The parameters are expanded, and the
+    result of the expansion must be a colon-separated list consisting of a
+    destination server, port number, and the packet contents. The server can be
+    specified as a host name or IPv4 or IPv6 address. The separator can be
+    changed with the usual angle bracket syntax. For example, you might want to
+    collect information on which hosts connect when:
+
+    udpsend = <; 2001:dB8::dead:beef ; 1234 ;\
+                 $tod_zulu $sender_host_address
+
+
+42.22 Use of the control modifier
+---------------------------------
+
+The control modifier supports the following settings:
+
+control = allow_auth_unadvertised
+
+    This modifier allows a client host to use the SMTP AUTH command even when
+    it has not been advertised in response to EHLO. Furthermore, because there
+    are apparently some really broken clients that do this, Exim will accept
+    AUTH after HELO (rather than EHLO) when this control is set. It should be
+    used only if you really need it, and you should limit its use to those
+    broken clients that do not work without it. For example:
+
+    warn hosts   = 192.168.34.25
+         control = allow_auth_unadvertised
+
+    Normally, when an Exim server receives an AUTH command, it checks the name
+    of the authentication mechanism that is given in the command to ensure that
+    it matches an advertised mechanism. When this control is set, the check
+    that a mechanism has been advertised is bypassed. Any configured mechanism
+    can be used by the client. This control is permitted only in the connection
+    and HELO ACLs.
+
+control = caseful_local_part, control = caselower_local_part
+
+    These two controls are permitted only in the ACL specified by acl_smtp_rcpt
+    (that is, during RCPT processing). By default, the contents of $local_part
+    are lower cased before ACL processing. If "caseful_local_part" is
+    specified, any uppercase letters in the original local part are restored in
+    $local_part for the rest of the ACL, or until a control that sets
+    "caselower_local_part" is encountered.
+
+    These controls affect only the current recipient. Moreover, they apply only
+    to local part handling that takes place directly in the ACL (for example,
+    as a key in lookups). If a test to verify the recipient is obeyed, the
+    case-related handling of the local part during the verification is
+    controlled by the router configuration (see the caseful_local_part generic
+    router option).
+
+    This facility could be used, for example, to add a spam score to local
+    parts containing upper case letters. For example, using $acl_m4 to
+    accumulate the spam score:
+
+    warn  control = caseful_local_part
+          set acl_m4 = ${eval:\
+                         $acl_m4 + \
+                         ${if match{$local_part}{[A-Z]}{1}{0}}\
+                        }
+          control = caselower_local_part
+
+    Notice that we put back the lower cased version afterwards, assuming that
+    is what is wanted for subsequent tests.
+
+control = cutthrough_delivery
+
+    This option requests delivery be attempted while the item is being
+    received. It is usable in the RCPT ACL and valid only for single-recipient
+    mails forwarded from one SMTP connection to another. If a recipient-verify
+    callout connection is requested in the same ACL it is held open and used
+    for the data, otherwise one is made after the ACL completes.
+
+    Note that routers are used in verify mode, and cannot depend on content of
+    received headers. Note also that headers cannot be modified by any of the
+    post-data ACLs (DATA, MIME and DKIM). Headers may be modified by routers
+    (subject to the above) and transports.
+
+    Cutthrough delivery is not supported via transport-filters or when DKIM
+    signing of outgoing messages is done, because it sends data to the ultimate
+    destination before the entire message has been received from the source.
+
+    Should the ultimate destination system positively accept or reject the
+    mail, a corresponding indication is given to the source system and nothing
+    is queued. If there is a temporary error the item is queued for later
+    delivery in the usual fashion. If the item is successfully delivered in
+    cutthrough mode the log line is tagged with ">>" rather than "=>" and
+    appears before the acceptance "<=" line.
+
+    Delivery in this mode avoids the generation of a bounce mail to a (possibly
+    faked) sender when the destination system is doing content-scan based
+    rejection.
+
+control = debug/<options>
+
+    This control turns on debug logging, almost as though Exim had been invoked
+    with "-d", with the output going to a new logfile, by default called 
+    debuglog. The filename can be adjusted with the tag option, which may
+    access any variables already defined. The logging may be adjusted with the 
+    opts option, which takes the same values as the "-d" command-line option.
+    Some examples (which depend on variables that don't exist in all contexts):
+
+          control = debug
+          control = debug/tag=.$sender_host_address
+          control = debug/opts=+expand+acl
+          control = debug/tag=.$message_exim_id/opts=+expand
+
+control = dkim_disable_verify
+
+    This control turns off DKIM verification processing entirely. For details
+    on the operation and configuration of DKIM, see chapter 56.
+
+control = dscp/<value>
+
+    This option causes the DSCP value associated with the socket for the
+    inbound connection to be adjusted to a given value, given as one of a
+    number of fixed strings or to numeric value. The -bI:dscp option may be
+    used to ask Exim which names it knows of. Common values include
+    "throughput", "mincost", and on newer systems "ef", "af41", etc. Numeric
+    values may be in the range 0 to 0x3F.
+
+    The outbound packets from Exim will be marked with this value in the header
+    (for IPv4, the TOS field; for IPv6, the TCLASS field); there is no
+    guarantee that these values will have any effect, not be stripped by
+    networking equipment, or do much of anything without cooperation with your
+    Network Engineer and those of all network operators between the source and
+    destination.
+
+control = enforce_sync, control = no_enforce_sync
+
+    These controls make it possible to be selective about when SMTP
+    synchronization is enforced. The global option smtp_enforce_sync specifies
+    the initial state of the switch (it is true by default). See the
+    description of this option in chapter 14 for details of SMTP
+    synchronization checking.
+
+    The effect of these two controls lasts for the remainder of the SMTP
+    connection. They can appear in any ACL except the one for the non-SMTP
+    messages. The most straightforward place to put them is in the ACL defined
+    by acl_smtp_connect, which is run at the start of an incoming SMTP
+    connection, before the first synchronization check. The expected use is to
+    turn off the synchronization checks for badly-behaved hosts that you
+    nevertheless need to work with.
+
+control = fakedefer/<message>
+
+    This control works in exactly the same way as fakereject (described below)
+    except that it causes an SMTP 450 response after the message data instead
+    of a 550 response. You must take care when using fakedefer because it
+    causes the messages to be duplicated when the sender retries. Therefore,
+    you should not use fakedefer if the message is to be delivered normally.
+
+control = fakereject/<message>
+
+    This control is permitted only for the MAIL, RCPT, and DATA ACLs, in other
+    words, only when an SMTP message is being received. If Exim accepts the
+    message, instead the final 250 response, a 550 rejection message is sent.
+    However, Exim proceeds to deliver the message as normal. The control
+    applies only to the current message, not to any subsequent ones that may be
+    received in the same SMTP connection.
+
+    The text for the 550 response is taken from the control modifier. If no
+    message is supplied, the following is used:
+
+    550-Your message has been rejected but is being
+    550-kept for evaluation.
+    550-If it was a legitimate message, it may still be
+    550 delivered to the target recipient(s).
+
+    This facility should be used with extreme caution.
+
+control = freeze
+
+    This control is permitted only for the MAIL, RCPT, DATA, and non-SMTP ACLs,
+    in other words, only when a message is being received. If the message is
+    accepted, it is placed on Exim's queue and frozen. The control applies only
+    to the current message, not to any subsequent ones that may be received in
+    the same SMTP connection.
+
+    This modifier can optionally be followed by "/no_tell". If the global
+    option freeze_tell is set, it is ignored for the current message (that is,
+    nobody is told about the freezing), provided all the control=freeze
+    modifiers that are obeyed for the current message have the "/no_tell"
+    option.
+
+control = no_delay_flush
+
+    Exim normally flushes SMTP output before implementing a delay in an ACL, to
+    avoid unexpected timeouts in clients when the SMTP PIPELINING extension is
+    in use. This control, as long as it is encountered before the delay
+    modifier, disables such output flushing.
+
+control = no_callout_flush
+
+    Exim normally flushes SMTP output before performing a callout in an ACL, to
+    avoid unexpected timeouts in clients when the SMTP PIPELINING extension is
+    in use. This control, as long as it is encountered before the verify
+    condition that causes the callout, disables such output flushing.
+
+control = no_mbox_unspool
+
+    This control is available when Exim is compiled with the content scanning
+    extension. Content scanning may require a copy of the current message, or
+    parts of it, to be written in "mbox format" to a spool file, for passing to
+    a virus or spam scanner. Normally, such copies are deleted when they are no
+    longer needed. If this control is set, the copies are not deleted. The
+    control applies only to the current message, not to any subsequent ones
+    that may be received in the same SMTP connection. It is provided for
+    debugging purposes and is unlikely to be useful in production.
+
+control = no_multiline_responses
+
+    This control is permitted for any ACL except the one for non-SMTP messages.
+    It seems that there are broken clients in use that cannot handle multiline
+    SMTP responses, despite the fact that RFC 821 defined them over 20 years
+    ago.
+
+    If this control is set, multiline SMTP responses from ACL rejections are
+    suppressed. One way of doing this would have been to put out these
+    responses as one long line. However, RFC 2821 specifies a maximum of 512
+    bytes per response ("use multiline responses for more" it says - ha!), and
+    some of the responses might get close to that. So this facility, which is
+    after all only a sop to broken clients, is implemented by doing two very
+    easy things:
+
+      * Extra information that is normally output as part of a rejection caused
+        by sender verification failure is omitted. Only the final line
+        (typically "sender verification failed") is sent.
+
+      * If a message modifier supplies a multiline response, only the first
+        line is output.
+
+    The setting of the switch can, of course, be made conditional on the
+    calling host. Its effect lasts until the end of the SMTP connection.
+
+control = no_pipelining
+
+    This control turns off the advertising of the PIPELINING extension to SMTP
+    in the current session. To be useful, it must be obeyed before Exim sends
+    its response to an EHLO command. Therefore, it should normally appear in an
+    ACL controlled by acl_smtp_connect or acl_smtp_helo. See also 
+    pipelining_advertise_hosts.
+
+control = queue_only
+
+    This control is permitted only for the MAIL, RCPT, DATA, and non-SMTP ACLs,
+    in other words, only when a message is being received. If the message is
+    accepted, it is placed on Exim's queue and left there for delivery by a
+    subsequent queue runner. No immediate delivery process is started. In other
+    words, it has the effect as the queue_only global option. However, the
+    control applies only to the current message, not to any subsequent ones
+    that may be received in the same SMTP connection.
+
+control = submission/<options>
+
+    This control is permitted only for the MAIL, RCPT, and start of data ACLs
+    (the latter is the one defined by acl_smtp_predata). Setting it tells Exim
+    that the current message is a submission from a local MUA. In this case,
+    Exim operates in "submission mode", and applies certain fixups to the
+    message if necessary. For example, it adds a Date: header line if one is
+    not present. This control is not permitted in the acl_smtp_data ACL,
+    because that is too late (the message has already been created).
+
+    Chapter 46 describes the processing that Exim applies to messages. Section
+    46.1 covers the processing that happens in submission mode; the available
+    options for this control are described there. The control applies only to
+    the current message, not to any subsequent ones that may be received in the
+    same SMTP connection.
+
+control = suppress_local_fixups
+
+    This control applies to locally submitted (non TCP/IP) messages, and is the
+    complement of "control = submission". It disables the fixups that are
+    normally applied to locally-submitted messages. Specifically:
+
+      * Any Sender: header line is left alone (in this respect, it is a dynamic
+        version of local_sender_retain).
+
+      * No Message-ID:, From:, or Date: header lines are added.
+
+      * There is no check that From: corresponds to the actual sender.
+
+    This control may be useful when a remotely-originated message is accepted,
+    passed to some scanning program, and then re-submitted for delivery. It can
+    be used only in the acl_smtp_mail, acl_smtp_rcpt, acl_smtp_predata, and 
+    acl_not_smtp_start ACLs, because it has to be set before the message's data
+    is read.
+
+    Note: This control applies only to the current message, not to any others
+    that are being submitted at the same time using -bs or -bS.
+
+
+42.23 Summary of message fixup control
+--------------------------------------
+
+All four possibilities for message fixups can be specified:
+
+  * Locally submitted, fixups applied: the default.
+
+  * Locally submitted, no fixups applied: use "control =
+    suppress_local_fixups".
+
+  * Remotely submitted, no fixups applied: the default.
+
+  * Remotely submitted, fixups applied: use "control = submission".
+
+
+42.24 Adding header lines in ACLs
+---------------------------------
+
+The add_header modifier can be used to add one or more extra header lines to an
+incoming message, as in this example:
+
+warn dnslists = sbl.spamhaus.org : \
+                dialup.mail-abuse.org
+     add_header = X-blacklisted-at: $dnslist_domain
+
+The add_header modifier is permitted in the MAIL, RCPT, PREDATA, DATA, MIME,
+DKIM, and non-SMTP ACLs (in other words, those that are concerned with
+receiving a message). The message must ultimately be accepted for add_header to
+have any significant effect. You can use add_header with any ACL verb,
+including deny (though this is potentially useful only in a RCPT ACL).
+
+Headers will not be added to the message if the modifier is used in DATA, MIME
+or DKIM ACLs for messages delivered by cutthrough routing.
+
+Leading and trailing newlines are removed from the data for the add_header
+modifier; if it then contains one or more newlines that are not followed by a
+space or a tab, it is assumed to contain multiple header lines. Each one is
+checked for valid syntax; "X-ACL-Warn:" is added to the front of any line that
+is not a valid header line.
+
+Added header lines are accumulated during the MAIL, RCPT, and predata ACLs.
+They are added to the message before processing the DATA and MIME ACLs.
+However, if an identical header line is requested more than once, only one copy
+is actually added to the message. Further header lines may be accumulated
+during the DATA and MIME ACLs, after which they are added to the message, again
+with duplicates suppressed. Thus, it is possible to add two identical header
+lines to an SMTP message, but only if one is added before DATA and one after.
+In the case of non-SMTP messages, new headers are accumulated during the
+non-SMTP ACLs, and are added to the message after all the ACLs have run. If a
+message is rejected after DATA or by the non-SMTP ACL, all added header lines
+are included in the entry that is written to the reject log.
+
+Header lines are not visible in string expansions of message headers until they
+are added to the message. It follows that header lines defined in the MAIL,
+RCPT, and predata ACLs are not visible until the DATA ACL and MIME ACLs are
+run. Similarly, header lines that are added by the DATA or MIME ACLs are not
+visible in those ACLs. Because of this restriction, you cannot use header lines
+as a way of passing data between (for example) the MAIL and RCPT ACLs. If you
+want to do this, you can use ACL variables, as described in section 42.19.
+
+The list of headers yet to be added is given by the $headers_added variable.
+
+The add_header modifier acts immediately as it is encountered during the
+processing of an ACL. Notice the difference between these two cases:
+
+accept add_header = ADDED: some text
+       <some condition>
+
+accept <some condition>
+       add_header = ADDED: some text
+
+In the first case, the header line is always added, whether or not the
+condition is true. In the second case, the header line is added only if the
+condition is true. Multiple occurrences of add_header may occur in the same ACL
+statement. All those that are encountered before a condition fails are
+honoured.
+
+For compatibility with previous versions of Exim, a message modifier for a warn
+verb acts in the same way as add_header, except that it takes effect only if
+all the conditions are true, even if it appears before some of them.
+Furthermore, only the last occurrence of message is honoured. This usage of 
+message is now deprecated. If both add_header and message are present on a warn
+verb, both are processed according to their specifications.
+
+By default, new header lines are added to a message at the end of the existing
+header lines. However, you can specify that any particular header line should
+be added right at the start (before all the Received: lines), immediately after
+the first block of Received: lines, or immediately before any line that is not
+a Received: or Resent-something: header.
+
+This is done by specifying ":at_start:", ":after_received:", or
+":at_start_rfc:" (or, for completeness, ":at_end:") before the text of the
+header line, respectively. (Header text cannot start with a colon, as there has
+to be a header name first.) For example:
+
+warn add_header = \
+       :after_received:X-My-Header: something or other...
+
+If more than one header line is supplied in a single add_header modifier, each
+one is treated independently and can therefore be placed differently. If you
+add more than one line at the start, or after the Received: block, they end up
+in reverse order.
+
+Warning: This facility currently applies only to header lines that are added in
+an ACL. It does NOT work for header lines that are added in a system filter or
+in a router or transport.
+
+
+42.25 Removing header lines in ACLs
+-----------------------------------
+
+The remove_header modifier can be used to remove one or more header lines from
+an incoming message, as in this example:
+
+warn   message        = Remove internal headers
+       remove_header  = x-route-mail1 : x-route-mail2
+
+The remove_header modifier is permitted in the MAIL, RCPT, PREDATA, DATA, MIME,
+DKIM, and non-SMTP ACLs (in other words, those that are concerned with
+receiving a message). The message must ultimately be accepted for remove_header
+to have any significant effect. You can use remove_header with any ACL verb,
+including deny, though this is really not useful for any verb that doesn't
+result in a delivered message.
+
+Headers will not be removed to the message if the modifier is used in DATA,
+MIME or DKIM ACLs for messages delivered by cutthrough routing.
+
+More than one header can be removed at the same time by using a colon separated
+list of header names. The header matching is case insensitive. Wildcards are
+not permitted, nor is list expansion performed, so you cannot use hostlists to
+create a list of headers, however both connection and message variable
+expansion are performed ($acl_c_* and $acl_m_*), illustrated in this example:
+
+warn   hosts           = +internal_hosts
+       set acl_c_ihdrs = x-route-mail1 : x-route-mail2
+warn   message         = Remove internal headers
+       remove_header   = $acl_c_ihdrs
+
+Removed header lines are accumulated during the MAIL, RCPT, and predata ACLs.
+They are removed from the message before processing the DATA and MIME ACLs.
+There is no harm in attempting to remove the same header twice nor is removing
+a non-existent header. Further header lines to be removed may be accumulated
+during the DATA and MIME ACLs, after which they are removed from the message,
+if present. In the case of non-SMTP messages, headers to be removed are
+accumulated during the non-SMTP ACLs, and are removed from the message after
+all the ACLs have run. If a message is rejected after DATA or by the non-SMTP
+ACL, there really is no effect because there is no logging of what headers
+would have been removed.
+
+Header lines are not visible in string expansions until the DATA phase when it
+is received. Any header lines removed in the MAIL, RCPT, and predata ACLs are
+not visible in the DATA ACL and MIME ACLs. Similarly, header lines that are
+removed by the DATA or MIME ACLs are still visible in those ACLs. Because of
+this restriction, you cannot use header lines as a way of controlling data
+passed between (for example) the MAIL and RCPT ACLs. If you want to do this,
+you should instead use ACL variables, as described in section 42.19.
+
+The remove_header modifier acts immediately as it is encountered during the
+processing of an ACL. Notice the difference between these two cases:
+
+accept remove_header = X-Internal
+       <some condition>
+
+accept <some condition>
+       remove_header = X-Internal
+
+In the first case, the header line is always removed, whether or not the
+condition is true. In the second case, the header line is removed only if the
+condition is true. Multiple occurrences of remove_header may occur in the same
+ACL statement. All those that are encountered before a condition fails are
+honoured.
+
+Warning: This facility currently applies only to header lines that are present
+during ACL processing. It does NOT remove header lines that are added in a
+system filter or in a router or transport.
+
+
+42.26 ACL conditions
+--------------------
+
+Some of the conditions listed in this section are available only when Exim is
+compiled with the content-scanning extension. They are included here briefly
+for completeness. More detailed descriptions can be found in the discussion on
+content scanning in chapter 43.
+
+Not all conditions are relevant in all circumstances. For example, testing
+senders and recipients does not make sense in an ACL that is being run as the
+result of the arrival of an ETRN command, and checks on message headers can be
+done only in the ACLs specified by acl_smtp_data and acl_not_smtp. You can use
+the same condition (with different parameters) more than once in the same ACL
+statement. This provides a way of specifying an "and" conjunction. The
+conditions are as follows:
+
+acl = <name of acl or ACL string or file name >
+
+    The possible values of the argument are the same as for the acl_smtp_xxx
+    options. The named or inline ACL is run. If it returns "accept" the
+    condition is true; if it returns "deny" the condition is false. If it
+    returns "defer", the current ACL returns "defer" unless the condition is on
+    a warn verb. In that case, a "defer" return makes the condition false. This
+    means that further processing of the warn verb ceases, but processing of
+    the ACL continues.
+
+    If the argument is a named ACL, up to nine space-separated optional values
+    can be appended; they appear within the called ACL in $acl_arg1 to
+    $acl_arg9, and $acl_narg is set to the count of values. Previous values of
+    these variables are restored after the call returns. The name and values
+    are expanded separately.
+
+    If the nested acl returns "drop" and the outer condition denies access, the
+    connection is dropped. If it returns "discard", the verb must be accept or 
+    discard, and the action is taken immediately - no further conditions are
+    tested.
+
+    ACLs may be nested up to 20 deep; the limit exists purely to catch runaway
+    loops. This condition allows you to use different ACLs in different
+    circumstances. For example, different ACLs can be used to handle RCPT
+    commands for different local users or different local domains.
+
+authenticated = <string list>
+
+    If the SMTP connection is not authenticated, the condition is false.
+    Otherwise, the name of the authenticator is tested against the list. To
+    test for authentication by any authenticator, you can set
+
+    authenticated = *
+
+condition = <string>
+
+    This feature allows you to make up custom conditions. If the result of
+    expanding the string is an empty string, the number zero, or one of the
+    strings "no" or "false", the condition is false. If the result is any
+    non-zero number, or one of the strings "yes" or "true", the condition is
+    true. For any other value, some error is assumed to have occurred, and the
+    ACL returns "defer". However, if the expansion is forced to fail, the
+    condition is ignored. The effect is to treat it as true, whether it is
+    positive or negative.
+
+decode = <location>
+
+    This condition is available only when Exim is compiled with the
+    content-scanning extension, and it is allowed only in the ACL defined by 
+    acl_smtp_mime. It causes the current MIME part to be decoded into a file.
+    If all goes well, the condition is true. It is false only if there are
+    problems such as a syntax error or a memory shortage. For more details, see
+    chapter 43.
+
+demime = <extension list>
+
+    This condition is available only when Exim is compiled with the
+    content-scanning extension. Its use is described in section 43.6.
+
+dnslists = <list of domain names and other data>
+
+    This condition checks for entries in DNS black lists. These are also known
+    as "RBL lists", after the original Realtime Blackhole List, but note that
+    the use of the lists at mail-abuse.org now carries a charge. There are too
+    many different variants of this condition to describe briefly here. See
+    sections 42.27-42.37 for details.
+
+domains = <domain list>
+
+    This condition is relevant only after a RCPT command. It checks that the
+    domain of the recipient address is in the domain list. If percent-hack
+    processing is enabled, it is done before this test is done. If the check
+    succeeds with a lookup, the result of the lookup is placed in $domain_data
+    until the next domains test.
+
+    Note carefully (because many people seem to fall foul of this): you cannot
+    use domains in a DATA ACL.
+
+encrypted = <string list>
+
+    If the SMTP connection is not encrypted, the condition is false. Otherwise,
+    the name of the cipher suite in use is tested against the list. To test for
+    encryption without testing for any specific cipher suite(s), set
+
+    encrypted = *
+
+hosts = <host list>
+
+    This condition tests that the calling host matches the host list. If you
+    have name lookups or wildcarded host names and IP addresses in the same
+    host list, you should normally put the IP addresses first. For example, you
+    could have:
+
+    accept hosts = 10.9.8.7 : dbm;/etc/friendly/hosts
+
+    The lookup in this example uses the host name for its key. This is implied
+    by the lookup type "dbm". (For a host address lookup you would use
+    "net-dbm" and it wouldn't matter which way round you had these two items.)
+
+    The reason for the problem with host names lies in the left-to-right way
+    that Exim processes lists. It can test IP addresses without doing any DNS
+    lookups, but when it reaches an item that requires a host name, it fails if
+    it cannot find a host name to compare with the pattern. If the above list
+    is given in the opposite order, the accept statement fails for a host whose
+    name cannot be found, even if its IP address is 10.9.8.7.
+
+    If you really do want to do the name check first, and still recognize the
+    IP address even if the name lookup fails, you can rewrite the ACL like
+    this:
+
+    accept hosts = dbm;/etc/friendly/hosts
+    accept hosts = 10.9.8.7
+
+    The default action on failing to find the host name is to assume that the
+    host is not in the list, so the first accept statement fails. The second
+    statement can then check the IP address.
+
+    If a hosts condition is satisfied by means of a lookup, the result of the
+    lookup is made available in the $host_data variable. This allows you, for
+    example, to set up a statement like this:
+
+    deny  hosts = net-lsearch;/some/file
+    message = $host_data
+
+    which gives a custom error message for each denied host.
+
+local_parts = <local part list>
+
+    This condition is relevant only after a RCPT command. It checks that the
+    local part of the recipient address is in the list. If percent-hack
+    processing is enabled, it is done before this test. If the check succeeds
+    with a lookup, the result of the lookup is placed in $local_part_data,
+    which remains set until the next local_parts test.
+
+malware = <option>
+
+    This condition is available only when Exim is compiled with the
+    content-scanning extension. It causes the incoming message to be scanned
+    for viruses. For details, see chapter 43.
+
+mime_regex = <list of regular expressions>
+
+    This condition is available only when Exim is compiled with the
+    content-scanning extension, and it is allowed only in the ACL defined by 
+    acl_smtp_mime. It causes the current MIME part to be scanned for a match
+    with any of the regular expressions. For details, see chapter 43.
+
+ratelimit = <parameters>
+
+    This condition can be used to limit the rate at which a user or host
+    submits messages. Details are given in section 42.38.
+
+recipients = <address list>
+
+    This condition is relevant only after a RCPT command. It checks the entire
+    recipient address against a list of recipients.
+
+regex = <list of regular expressions>
+
+    This condition is available only when Exim is compiled with the
+    content-scanning extension, and is available only in the DATA, MIME, and
+    non-SMTP ACLs. It causes the incoming message to be scanned for a match
+    with any of the regular expressions. For details, see chapter 43.
+
+sender_domains = <domain list>
+
+    This condition tests the domain of the sender of the message against the
+    given domain list. Note: The domain of the sender address is in
+    $sender_address_domain. It is not put in $domain during the testing of this
+    condition. This is an exception to the general rule for testing domain
+    lists. It is done this way so that, if this condition is used in an ACL for
+    a RCPT command, the recipient's domain (which is in $domain) can be used to
+    influence the sender checking.
+
+    Warning: It is a bad idea to use this condition on its own as a control on
+    relaying, because sender addresses are easily, and commonly, forged.
+
+senders = <address list>
+
+    This condition tests the sender of the message against the given list. To
+    test for a bounce message, which has an empty sender, set
+
+    senders = :
+
+    Warning: It is a bad idea to use this condition on its own as a control on
+    relaying, because sender addresses are easily, and commonly, forged.
+
+spam = <username>
+
+    This condition is available only when Exim is compiled with the
+    content-scanning extension. It causes the incoming message to be scanned by
+    SpamAssassin. For details, see chapter 43.
+
+verify = certificate
+
+    This condition is true in an SMTP session if the session is encrypted, and
+    a certificate was received from the client, and the certificate was
+    verified. The server requests a certificate only if the client matches 
+    tls_verify_hosts or tls_try_verify_hosts (see chapter 41).
+
+verify = csa
+
+    This condition checks whether the sending host (the client) is authorized
+    to send email. Details of how this works are given in section 42.50.
+
+verify = header_names_ascii
+
+    This condition is relevant only in an ACL that is run after a message has
+    been received, that is, in an ACL specified by acl_smtp_data or 
+    acl_not_smtp. It checks all header names (not the content) to make sure
+    there are no non-ASCII characters, also excluding control characters. The
+    allowable characters are decimal ASCII values 33 through 126.
+
+    Exim itself will handle headers with non-ASCII characters, but it can cause
+    problems for downstream applications, so this option will allow their
+    detection and rejection in the DATA ACL's.
+
+verify = header_sender/<options>
+
+    This condition is relevant only in an ACL that is run after a message has
+    been received, that is, in an ACL specified by acl_smtp_data or 
+    acl_not_smtp. It checks that there is a verifiable address in at least one
+    of the Sender:, Reply-To:, or From: header lines. Such an address is
+    loosely thought of as a "sender" address (hence the name of the test).
+    However, an address that appears in one of these headers need not be an
+    address that accepts bounce messages; only sender addresses in envelopes
+    are required to accept bounces. Therefore, if you use the callout option on
+    this check, you might want to arrange for a non-empty address in the MAIL
+    command.
+
+    Details of address verification and the options are given later, starting
+    at section 42.44 (callouts are described in section 42.45). You can combine
+    this condition with the senders condition to restrict it to bounce messages
+    only:
+
+    deny    senders = :
+            message = A valid sender header is required for bounces
+           !verify  = header_sender
+
+verify = header_syntax
+
+    This condition is relevant only in an ACL that is run after a message has
+    been received, that is, in an ACL specified by acl_smtp_data or 
+    acl_not_smtp. It checks the syntax of all header lines that can contain
+    lists of addresses (Sender:, From:, Reply-To:, To:, Cc:, and Bcc:).
+    Unqualified addresses (local parts without domains) are permitted only in
+    locally generated messages and from hosts that match 
+    sender_unqualified_hosts or recipient_unqualified_hosts, as appropriate.
+
+    Note that this condition is a syntax check only. However, a common spamming
+    ploy used to be to send syntactically invalid headers such as
+
+    To: @
+
+    and this condition can be used to reject such messages, though they are not
+    as common as they used to be.
+
+verify = helo
+
+    This condition is true if a HELO or EHLO command has been received from the
+    client host, and its contents have been verified. If there has been no
+    previous attempt to verify the HELO/EHLO contents, it is carried out when
+    this condition is encountered. See the description of the helo_verify_hosts
+    and helo_try_verify_hosts options for details of how to request
+    verification independently of this condition.
+
+    For SMTP input that does not come over TCP/IP (the -bs command line
+    option), this condition is always true.
+
+verify = not_blind
+
+    This condition checks that there are no blind (bcc) recipients in the
+    message. Every envelope recipient must appear either in a To: header line
+    or in a Cc: header line for this condition to be true. Local parts are
+    checked case-sensitively; domains are checked case-insensitively. If 
+    Resent-To: or Resent-Cc: header lines exist, they are also checked. This
+    condition can be used only in a DATA or non-SMTP ACL.
+
+    There are, of course, many legitimate messages that make use of blind (bcc)
+    recipients. This check should not be used on its own for blocking messages.
+
+verify = recipient/<options>
+
+    This condition is relevant only after a RCPT command. It verifies the
+    current recipient. Details of address verification are given later,
+    starting at section 42.44. After a recipient has been verified, the value
+    of $address_data is the last value that was set while routing the address.
+    This applies even if the verification fails. When an address that is being
+    verified is redirected to a single address, verification continues with the
+    new address, and in that case, the subsequent value of $address_data is the
+    value for the child address.
+
+verify = reverse_host_lookup
+
+    This condition ensures that a verified host name has been looked up from
+    the IP address of the client host. (This may have happened already if the
+    host name was needed for checking a host list, or if the host matched 
+    host_lookup.) Verification ensures that the host name obtained from a
+    reverse DNS lookup, or one of its aliases, does, when it is itself looked
+    up in the DNS, yield the original IP address.
+
+    If this condition is used for a locally generated message (that is, when
+    there is no client host involved), it always succeeds.
+
+verify = sender/<options>
+
+    This condition is relevant only after a MAIL or RCPT command, or after a
+    message has been received (the acl_smtp_data or acl_not_smtp ACLs). If the
+    message's sender is empty (that is, this is a bounce message), the
+    condition is true. Otherwise, the sender address is verified.
+
+    If there is data in the $address_data variable at the end of routing, its
+    value is placed in $sender_address_data at the end of verification. This
+    value can be used in subsequent conditions and modifiers in the same ACL
+    statement. It does not persist after the end of the current statement. If
+    you want to preserve the value for longer, you can save it in an ACL
+    variable.
+
+    Details of verification are given later, starting at section 42.44. Exim
+    caches the result of sender verification, to avoid doing it more than once
+    per message.
+
+verify = sender=<address>/<options>
+
+    This is a variation of the previous option, in which a modified address is
+    verified as a sender.
+
+
+42.27 Using DNS lists
+---------------------
+
+In its simplest form, the dnslists condition tests whether the calling host is
+on at least one of a number of DNS lists by looking up the inverted IP address
+in one or more DNS domains. (Note that DNS list domains are not mail domains,
+so the "+" syntax for named lists doesn't work - it is used for special options
+instead.) For example, if the calling host's IP address is 192.168.62.43, and
+the ACL statement is
+
+deny dnslists = blackholes.mail-abuse.org : \
+                dialups.mail-abuse.org
+
+the following records are looked up:
+
+43.62.168.192.blackholes.mail-abuse.org
+43.62.168.192.dialups.mail-abuse.org
+
+As soon as Exim finds an existing DNS record, processing of the list stops.
+Thus, multiple entries on the list provide an "or" conjunction. If you want to
+test that a host is on more than one list (an "and" conjunction), you can use
+two separate conditions:
+
+deny dnslists = blackholes.mail-abuse.org
+     dnslists = dialups.mail-abuse.org
+
+If a DNS lookup times out or otherwise fails to give a decisive answer, Exim
+behaves as if the host does not match the list item, that is, as if the DNS
+record does not exist. If there are further items in the DNS list, they are
+processed.
+
+This is usually the required action when dnslists is used with deny (which is
+the most common usage), because it prevents a DNS failure from blocking mail.
+However, you can change this behaviour by putting one of the following special
+items in the list:
+
++include_unknown    behave as if the item is on the list
++exclude_unknown    behave as if the item is not on the list (default)
++defer_unknown      give a temporary error
+
+Each of these applies to any subsequent items on the list. For example:
+
+deny dnslists = +defer_unknown : foo.bar.example
+
+Testing the list of domains stops as soon as a match is found. If you want to
+warn for one list and block for another, you can use two different statements:
+
+deny  dnslists = blackholes.mail-abuse.org
+warn  message  = X-Warn: sending host is on dialups list
+      dnslists = dialups.mail-abuse.org
+
+DNS list lookups are cached by Exim for the duration of the SMTP session, so a
+lookup based on the IP address is done at most once for any incoming
+connection. Exim does not share information between multiple incoming
+connections (but your local name server cache should be active).
+
+
+42.28 Specifying the IP address for a DNS list lookup
+-----------------------------------------------------
+
+By default, the IP address that is used in a DNS list lookup is the IP address
+of the calling host. However, you can specify another IP address by listing it
+after the domain name, introduced by a slash. For example:
+
+deny dnslists = black.list.tld/192.168.1.2
+
+This feature is not very helpful with explicit IP addresses; it is intended for
+use with IP addresses that are looked up, for example, the IP addresses of the
+MX hosts or nameservers of an email sender address. For an example, see section
+42.30 below.
+
+
+42.29 DNS lists keyed on domain names
+-------------------------------------
+
+There are some lists that are keyed on domain names rather than inverted IP
+addresses (see for example the domain based zones link at http://
+www.rfc-ignorant.org/). No reversing of components is used with these lists.
+You can change the name that is looked up in a DNS list by listing it after the
+domain name, introduced by a slash. For example,
+
+deny  message  = Sender's domain is listed at $dnslist_domain
+      dnslists = dsn.rfc-ignorant.org/$sender_address_domain
+
+This particular example is useful only in ACLs that are obeyed after the RCPT
+or DATA commands, when a sender address is available. If (for example) the
+message's sender is user@tld.example the name that is looked up by this example
+is
+
+tld.example.dsn.rfc-ignorant.org
+
+A single dnslists condition can contain entries for both names and IP
+addresses. For example:
+
+deny dnslists = sbl.spamhaus.org : \
+                dsn.rfc-ignorant.org/$sender_address_domain
+
+The first item checks the sending host's IP address; the second checks a domain
+name. The whole condition is true if either of the DNS lookups succeeds.
+
+
+42.30 Multiple explicit keys for a DNS list
+-------------------------------------------
+
+The syntax described above for looking up explicitly-defined values (either
+names or IP addresses) in a DNS blacklist is a simplification. After the domain
+name for the DNS list, what follows the slash can in fact be a list of items.
+As with all lists in Exim, the default separator is a colon. However, because
+this is a sublist within the list of DNS blacklist domains, it is necessary
+either to double the separators like this:
+
+dnslists = black.list.tld/name.1::name.2
+
+or to change the separator character, like this:
+
+dnslists = black.list.tld/<;name.1;name.2
+
+If an item in the list is an IP address, it is inverted before the DNS
+blacklist domain is appended. If it is not an IP address, no inversion occurs.
+Consider this condition:
+
+dnslists = black.list.tld/<;192.168.1.2;a.domain
+
+The DNS lookups that occur are:
+
+2.1.168.192.black.list.tld
+a.domain.black.list.tld
+
+Once a DNS record has been found (that matches a specific IP return address, if
+specified - see section 42.33), no further lookups are done. If there is a
+temporary DNS error, the rest of the sublist of domains or IP addresses is
+tried. A temporary error for the whole dnslists item occurs only if no other
+DNS lookup in this sublist succeeds. In other words, a successful lookup for
+any of the items in the sublist overrides a temporary error for a previous
+item.
+
+The ability to supply a list of items after the slash is in some sense just a
+syntactic convenience. These two examples have the same effect:
+
+dnslists = black.list.tld/a.domain : black.list.tld/b.domain
+dnslists = black.list.tld/a.domain::b.domain
+
+However, when the data for the list is obtained from a lookup, the second form
+is usually much more convenient. Consider this example:
+
+deny message  = The mail servers for the domain \
+                $sender_address_domain \
+                are listed at $dnslist_domain ($dnslist_value); \
+                see $dnslist_text.
+     dnslists = sbl.spamhaus.org/<|${lookup dnsdb {>|a=<|\
+                                   ${lookup dnsdb {>|mxh=\
+                                   $sender_address_domain} }} }
+
+Note the use of ">|" in the dnsdb lookup to specify the separator for multiple
+DNS records. The inner dnsdb lookup produces a list of MX hosts and the outer
+dnsdb lookup finds the IP addresses for these hosts. The result of expanding
+the condition might be something like this:
+
+dnslists = sbl.spahmaus.org/<|192.168.2.3|192.168.5.6|...
+
+Thus, this example checks whether or not the IP addresses of the sender
+domain's mail servers are on the Spamhaus black list.
+
+The key that was used for a successful DNS list lookup is put into the variable
+$dnslist_matched (see section 42.32).
+
+
+42.31 Data returned by DNS lists
+--------------------------------
+
+DNS lists are constructed using address records in the DNS. The original RBL
+just used the address 127.0.0.1 on the right hand side of each record, but the
+RBL+ list and some other lists use a number of values with different meanings.
+The values used on the RBL+ list are:
+
+127.1.0.1  RBL
+127.1.0.2  DUL
+127.1.0.3  DUL and RBL
+127.1.0.4  RSS
+127.1.0.5  RSS and RBL
+127.1.0.6  RSS and DUL
+127.1.0.7  RSS and DUL and RBL
+
+Section 42.33 below describes how you can distinguish between different values.
+Some DNS lists may return more than one address record; see section 42.35 for
+details of how they are checked.
+
+
+42.32 Variables set from DNS lists
+----------------------------------
+
+When an entry is found in a DNS list, the variable $dnslist_domain contains the
+name of the overall domain that matched (for example, "spamhaus.example"),
+$dnslist_matched contains the key within that domain (for example,
+"192.168.5.3"), and $dnslist_value contains the data from the DNS record. When
+the key is an IP address, it is not reversed in $dnslist_matched (though it is,
+of course, in the actual lookup). In simple cases, for example:
+
+deny dnslists = spamhaus.example
+
+the key is also available in another variable (in this case,
+$sender_host_address). In more complicated cases, however, this is not true.
+For example, using a data lookup (as described in section 42.30) might generate
+a dnslists lookup like this:
+
+deny dnslists = spamhaus.example/<|192.168.1.2|192.168.6.7|...
+
+If this condition succeeds, the value in $dnslist_matched might be
+"192.168.6.7" (for example).
+
+If more than one address record is returned by the DNS lookup, all the IP
+addresses are included in $dnslist_value, separated by commas and spaces. The
+variable $dnslist_text contains the contents of any associated TXT record. For
+lists such as RBL+ the TXT record for a merged entry is often not very
+meaningful. See section 42.36 for a way of obtaining more information.
+
+You can use the DNS list variables in message or log_message modifiers -
+although these appear before the condition in the ACL, they are not expanded
+until after it has failed. For example:
+
+deny    hosts = !+local_networks
+        message = $sender_host_address is listed \
+                  at $dnslist_domain
+        dnslists = rbl-plus.mail-abuse.example
+
+
+42.33 Additional matching conditions for DNS lists
+--------------------------------------------------
+
+You can add an equals sign and an IP address after a dnslists domain name in
+order to restrict its action to DNS records with a matching right hand side.
+For example,
+
+deny dnslists = rblplus.mail-abuse.org=127.0.0.2
+
+rejects only those hosts that yield 127.0.0.2. Without this additional data,
+any address record is considered to be a match. For the moment, we assume that
+the DNS lookup returns just one record. Section 42.35 describes how multiple
+records are handled.
+
+More than one IP address may be given for checking, using a comma as a
+separator. These are alternatives - if any one of them matches, the dnslists
+condition is true. For example:
+
+deny  dnslists = a.b.c=127.0.0.2,127.0.0.3
+
+If you want to specify a constraining address list and also specify names or IP
+addresses to be looked up, the constraining address list must be specified
+first. For example:
+
+deny dnslists = dsn.rfc-ignorant.org\
+                =127.0.0.2/$sender_address_domain
+
+If the character "&" is used instead of "=", the comparison for each listed IP
+address is done by a bitwise "and" instead of by an equality test. In other
+words, the listed addresses are used as bit masks. The comparison is true if
+all the bits in the mask are present in the address that is being tested. For
+example:
+
+dnslists = a.b.c&0.0.0.3
+
+matches if the address is x.x.x.3, x.x.x.7, x.x.x.11, etc. If you want to test
+whether one bit or another bit is present (as opposed to both being present),
+you must use multiple values. For example:
+
+dnslists = a.b.c&0.0.0.1,0.0.0.2
+
+matches if the final component of the address is an odd number or two times an
+odd number.
+
+
+42.34 Negated DNS matching conditions
+-------------------------------------
+
+You can supply a negative list of IP addresses as part of a dnslists condition.
+Whereas
+
+deny  dnslists = a.b.c=127.0.0.2,127.0.0.3
+
+means "deny if the host is in the black list at the domain a.b.c and the IP
+address yielded by the list is either 127.0.0.2 or 127.0.0.3",
+
+deny  dnslists = a.b.c!=127.0.0.2,127.0.0.3
+
+means "deny if the host is in the black list at the domain a.b.c and the IP
+address yielded by the list is not 127.0.0.2 and not 127.0.0.3". In other
+words, the result of the test is inverted if an exclamation mark appears before
+the "=" (or the "&") sign.
+
+Note: This kind of negation is not the same as negation in a domain, host, or
+address list (which is why the syntax is different).
+
+If you are using just one list, the negation syntax does not gain you much. The
+previous example is precisely equivalent to
+
+deny  dnslists = a.b.c
+     !dnslists = a.b.c=127.0.0.2,127.0.0.3
+
+However, if you are using multiple lists, the negation syntax is clearer.
+Consider this example:
+
+deny  dnslists = sbl.spamhaus.org : \
+                 list.dsbl.org : \
+                 dnsbl.njabl.org!=127.0.0.3 : \
+                 relays.ordb.org
+
+Using only positive lists, this would have to be:
+
+deny  dnslists = sbl.spamhaus.org : \
+                 list.dsbl.org
+deny  dnslists = dnsbl.njabl.org
+     !dnslists = dnsbl.njabl.org=127.0.0.3
+deny  dnslists = relays.ordb.org
+
+which is less clear, and harder to maintain.
+
+
+42.35 Handling multiple DNS records from a DNS list
+---------------------------------------------------
+
+A DNS lookup for a dnslists condition may return more than one DNS record,
+thereby providing more than one IP address. When an item in a dnslists list is
+followed by "=" or "&" and a list of IP addresses, in order to restrict the
+match to specific results from the DNS lookup, there are two ways in which the
+checking can be handled. For example, consider the condition:
+
+dnslists = a.b.c=127.0.0.1
+
+What happens if the DNS lookup for the incoming IP address yields both
+127.0.0.1 and 127.0.0.2 by means of two separate DNS records? Is the condition
+true because at least one given value was found, or is it false because at
+least one of the found values was not listed? And how does this affect negated
+conditions? Both possibilities are provided for with the help of additional
+separators "==" and "=&".
+
+  * If "=" or "&" is used, the condition is true if any one of the looked up IP
+    addresses matches one of the listed addresses. For the example above, the
+    condition is true because 127.0.0.1 matches.
+
+  * If "==" or "=&" is used, the condition is true only if every one of the
+    looked up IP addresses matches one of the listed addresses. If the
+    condition is changed to:
+
+    dnslists = a.b.c==127.0.0.1
+
+    and the DNS lookup yields both 127.0.0.1 and 127.0.0.2, the condition is
+    false because 127.0.0.2 is not listed. You would need to have:
+
+    dnslists = a.b.c==127.0.0.1,127.0.0.2
+
+    for the condition to be true.
+
+When "!" is used to negate IP address matching, it inverts the result, giving
+the precise opposite of the behaviour above. Thus:
+
+  * If "!=" or "!&" is used, the condition is true if none of the looked up IP
+    addresses matches one of the listed addresses. Consider:
+
+    dnslists = a.b.c!&0.0.0.1
+
+    If the DNS lookup yields both 127.0.0.1 and 127.0.0.2, the condition is
+    false because 127.0.0.1 matches.
+
+  * If "!==" or "!=&" is used, the condition is true if there is at least one
+    looked up IP address that does not match. Consider:
+
+    dnslists = a.b.c!=&0.0.0.1
+
+    If the DNS lookup yields both 127.0.0.1 and 127.0.0.2, the condition is
+    true, because 127.0.0.2 does not match. You would need to have:
+
+    dnslists = a.b.c!=&0.0.0.1,0.0.0.2
+
+    for the condition to be false.
+
+When the DNS lookup yields only a single IP address, there is no difference
+between "=" and "==" and between "&" and "=&".
+
+
+42.36 Detailed information from merged DNS lists
+------------------------------------------------
+
+When the facility for restricting the matching IP values in a DNS list is used,
+the text from the TXT record that is set in $dnslist_text may not reflect the
+true reason for rejection. This happens when lists are merged and the IP
+address in the A record is used to distinguish them; unfortunately there is
+only one TXT record. One way round this is not to use merged lists, but that
+can be inefficient because it requires multiple DNS lookups where one would do
+in the vast majority of cases when the host of interest is not on any of the
+lists.
+
+A less inefficient way of solving this problem is available. If two domain
+names, comma-separated, are given, the second is used first to do an initial
+check, making use of any IP value restrictions that are set. If there is a
+match, the first domain is used, without any IP value restrictions, to get the
+TXT record. As a byproduct of this, there is also a check that the IP being
+tested is indeed on the first list. The first domain is the one that is put in
+$dnslist_domain. For example:
+
+reject message  = \
+         rejected because $sender_host_address is blacklisted \
+         at $dnslist_domain\n$dnslist_text
+       dnslists = \
+         sbl.spamhaus.org,sbl-xbl.spamhaus.org=127.0.0.2 : \
+         dul.dnsbl.sorbs.net,dnsbl.sorbs.net=127.0.0.10
+
+For the first blacklist item, this starts by doing a lookup in 
+sbl-xbl.spamhaus.org and testing for a 127.0.0.2 return. If there is a match,
+it then looks in sbl.spamhaus.org, without checking the return value, and as
+long as something is found, it looks for the corresponding TXT record. If there
+is no match in sbl-xbl.spamhaus.org, nothing more is done. The second blacklist
+item is processed similarly.
+
+If you are interested in more than one merged list, the same list must be given
+several times, but because the results of the DNS lookups are cached, the DNS
+calls themselves are not repeated. For example:
+
+reject dnslists = \
+         http.dnsbl.sorbs.net,dnsbl.sorbs.net=127.0.0.2 : \
+         socks.dnsbl.sorbs.net,dnsbl.sorbs.net=127.0.0.3 : \
+         misc.dnsbl.sorbs.net,dnsbl.sorbs.net=127.0.0.4 : \
+         dul.dnsbl.sorbs.net,dnsbl.sorbs.net=127.0.0.10
+
+In this case there is one lookup in dnsbl.sorbs.net, and if none of the IP
+values matches (or if no record is found), this is the only lookup that is
+done. Only if there is a match is one of the more specific lists consulted.
+
+
+42.37 DNS lists and IPv6
+------------------------
+
+If Exim is asked to do a dnslist lookup for an IPv6 address, it inverts it
+nibble by nibble. For example, if the calling host's IP address is
+3ffe:ffff:836f:0a00:000a:0800:200a:c031, Exim might look up
+
+1.3.0.c.a.0.0.2.0.0.8.0.a.0.0.0.0.0.a.0.f.6.3.8.
+  f.f.f.f.e.f.f.3.blackholes.mail-abuse.org
+
+(split over two lines here to fit on the page). Unfortunately, some of the DNS
+lists contain wildcard records, intended for IPv4, that interact badly with
+IPv6. For example, the DNS entry
+
+*.3.some.list.example.    A    127.0.0.1
+
+is probably intended to put the entire 3.0.0.0/8 IPv4 network on the list.
+Unfortunately, it also matches the entire 3::/4 IPv6 network.
+
+You can exclude IPv6 addresses from DNS lookups by making use of a suitable 
+condition condition, as in this example:
+
+deny   condition = ${if isip4{$sender_host_address}}
+       dnslists  = some.list.example
+
+If an explicit key is being used for a DNS lookup and it may be an IPv6 address
+you should specify alternate list separators for both the outer (DNS list name)
+list and inner (lookup keys) list:
+
+       dnslists = <; dnsbl.example.com/<|$acl_m_addrslist
+
+
+42.38 Rate limiting incoming messages
+-------------------------------------
+
+The ratelimit ACL condition can be used to measure and control the rate at
+which clients can send email. This is more powerful than the smtp_ratelimit_*
+options, because those options control the rate of commands in a single SMTP
+session only, whereas the ratelimit condition works across all connections
+(concurrent and sequential) from the same client host. The syntax of the 
+ratelimit condition is:
+
+ratelimit = <m> / <p> / <options> / <key>
+
+If the average client sending rate is less than m messages per time period p
+then the condition is false; otherwise it is true.
+
+As a side-effect, the ratelimit condition sets the expansion variable
+$sender_rate to the client's computed rate, $sender_rate_limit to the
+configured value of m, and $sender_rate_period to the configured value of p.
+
+The parameter p is the smoothing time constant, in the form of an Exim time
+interval, for example, "8h" for eight hours. A larger time constant means that
+it takes Exim longer to forget a client's past behaviour. The parameter m is
+the maximum number of messages that a client is permitted to send in each time
+interval. It also specifies the number of messages permitted in a fast burst.
+By increasing both m and p but keeping m/p constant, you can allow a client to
+send more messages in a burst without changing its long-term sending rate
+limit. Conversely, if m and p are both small, messages must be sent at an even
+rate.
+
+There is a script in util/ratelimit.pl which extracts sending rates from log
+files, to assist with choosing appropriate settings for m and p when deploying
+the ratelimit ACL condition. The script prints usage instructions when it is
+run with no arguments.
+
+The key is used to look up the data for calculating the client's average
+sending rate. This data is stored in Exim's spool directory, alongside the
+retry and other hints databases. The default key is $sender_host_address, which
+means Exim computes the sending rate of each client host IP address. By
+changing the key you can change how Exim identifies clients for the purpose of
+ratelimiting. For example, to limit the sending rate of each authenticated
+user, independent of the computer they are sending from, set the key to
+$authenticated_id. You must ensure that the lookup key is meaningful; for
+example, $authenticated_id is only meaningful if the client has authenticated
+(which you can check with the authenticated ACL condition).
+
+The lookup key does not have to identify clients: If you want to limit the rate
+at which a recipient receives messages, you can use the key
+"$local_part@$domain" with the per_rcpt option (see below) in a RCPT ACL.
+
+Each ratelimit condition can have up to four options. A per_* option specifies
+what Exim measures the rate of, for example messages or recipients or bytes.
+You can adjust the measurement using the unique= and/or count= options. You can
+also control when Exim updates the recorded rate using a strict, leaky, or 
+readonly option. The options are separated by a slash, like the other
+parameters. They may appear in any order.
+
+Internally, Exim appends the smoothing constant p onto the lookup key with any
+options that alter the meaning of the stored data. The limit m is not stored,
+so you can alter the configured maximum rate and Exim will still remember
+clients' past behaviour. If you change the per_* mode or add or remove the 
+unique= option, the lookup key changes so Exim will forget past behaviour. The
+lookup key is not affected by changes to the update mode and the count= option.
+
+
+42.39 Ratelimit options for what is being measured
+--------------------------------------------------
+
+The per_conn option limits the client's connection rate. It is not normally
+used in the acl_not_smtp, acl_not_smtp_mime, or acl_not_smtp_start ACLs.
+
+The per_mail option limits the client's rate of sending messages. This is the
+default if none of the per_* options is specified. It can be used in 
+acl_smtp_mail, acl_smtp_rcpt, acl_smtp_predata, acl_smtp_mime, acl_smtp_data,
+or acl_not_smtp.
+
+The per_byte option limits the sender's email bandwidth. It can be used in the
+same ACLs as the per_mail option, though it is best to use this option in the 
+acl_smtp_mime, acl_smtp_data or acl_not_smtp ACLs; if it is used in an earlier
+ACL, Exim relies on the SIZE parameter given by the client in its MAIL command,
+which may be inaccurate or completely missing. You can follow the limit m in
+the configuration with K, M, or G to specify limits in kilobytes, megabytes, or
+gigabytes, respectively.
+
+The per_rcpt option causes Exim to limit the rate at which recipients are
+accepted. It can be used in the acl_smtp_rcpt, acl_smtp_predata, acl_smtp_mime,
+acl_smtp_data, or acl_smtp_rcpt ACLs. In acl_smtp_rcpt the rate is updated one
+recipient at a time; in the other ACLs the rate is updated with the total
+recipient count in one go. Note that in either case the rate limiting engine
+will see a message with many recipients as a large high-speed burst.
+
+The per_addr option is like the per_rcpt option, except it counts the number of
+different recipients that the client has sent messages to in the last time
+period. That is, if the client repeatedly sends messages to the same recipient,
+its measured rate is not increased. This option can only be used in 
+acl_smtp_rcpt.
+
+The per_cmd option causes Exim to recompute the rate every time the condition
+is processed. This can be used to limit the rate of any SMTP command. If it is
+used in multiple ACLs it can limit the aggregate rate of multiple different
+commands.
+
+The count= option can be used to alter how much Exim adds to the client's
+measured rate. For example, the per_byte option is equivalent to "per_mail/
+count=$message_size". If there is no count= option, Exim increases the measured
+rate by one (except for the per_rcpt option in ACLs other than acl_smtp_rcpt).
+The count does not have to be an integer.
+
+The unique= option is described in section 42.42 below.
+
+
+42.40 Ratelimit update modes
+----------------------------
+
+You can specify one of three options with the ratelimit condition to control
+when its database is updated. This section describes the readonly mode, and the
+next section describes the strict and leaky modes.
+
+If the ratelimit condition is used in readonly mode, Exim looks up a
+previously-computed rate to check against the limit.
+
+For example, you can test the client's sending rate and deny it access (when it
+is too fast) in the connect ACL. If the client passes this check then it can go
+on to send a message, in which case its recorded rate will be updated in the
+MAIL ACL. Subsequent connections from the same client will check this new rate.
+
+acl_check_connect:
+ deny ratelimit = 100 / 5m / readonly
+    log_message = RATE CHECK: $sender_rate/$sender_rate_period \
+                  (max $sender_rate_limit)
+# ...
+acl_check_mail:
+ warn ratelimit = 100 / 5m / strict
+    log_message = RATE UPDATE: $sender_rate/$sender_rate_period \
+                  (max $sender_rate_limit)
+
+If Exim encounters multiple ratelimit conditions with the same key when
+processing a message then it may increase the client's measured rate more than
+it should. For example, this will happen if you check the per_rcpt option in
+both acl_smtp_rcpt and acl_smtp_data. However it's OK to check the same 
+ratelimit condition multiple times in the same ACL. You can avoid any multiple
+update problems by using the readonly option on later ratelimit checks.
+
+The per_* options described above do not make sense in some ACLs. If you use a 
+per_* option in an ACL where it is not normally permitted then the update mode
+defaults to readonly and you cannot specify the strict or leaky modes. In other
+ACLs the default update mode is leaky (see the next section) so you must
+specify the readonly option explicitly.
+
+
+42.41 Ratelimit options for handling fast clients
+-------------------------------------------------
+
+If a client's average rate is greater than the maximum, the rate limiting
+engine can react in two possible ways, depending on the presence of the strict
+or leaky update modes. This is independent of the other counter-measures (such
+as rejecting the message) that may be specified by the rest of the ACL.
+
+The leaky (default) option means that the client's recorded rate is not updated
+if it is above the limit. The effect of this is that Exim measures the client's
+average rate of successfully sent email, which cannot be greater than the
+maximum allowed. If the client is over the limit it may suffer some
+counter-measures (as specified in the ACL), but it will still be able to send
+email at the configured maximum rate, whatever the rate of its attempts. This
+is generally the better choice if you have clients that retry automatically.
+For example, it does not prevent a sender with an over-aggressive retry rate
+from getting any email through.
+
+The strict option means that the client's recorded rate is always updated. The
+effect of this is that Exim measures the client's average rate of attempts to
+send email, which can be much higher than the maximum it is actually allowed.
+If the client is over the limit it may be subjected to counter-measures by the
+ACL. It must slow down and allow sufficient time to pass that its computed rate
+falls below the maximum before it can send email again. The time (the number of
+smoothing periods) it must wait and not attempt to send mail can be calculated
+with this formula:
+
+        ln(peakrate/maxrate)
+
+
+42.42 Limiting the rate of different events
+-------------------------------------------
+
+The ratelimit unique= option controls a mechanism for counting the rate of
+different events. For example, the per_addr option uses this mechanism to count
+the number of different recipients that the client has sent messages to in the
+last time period; it is equivalent to "per_rcpt/unique=$local_part@$domain".
+You could use this feature to measure the rate that a client uses different
+sender addresses with the options "per_mail/unique=$sender_address".
+
+For each ratelimit key Exim stores the set of unique= values that it has seen
+for that key. The whole set is thrown away when it is older than the rate
+smoothing period p, so each different event is counted at most once per period.
+In the leaky update mode, an event that causes the client to go over the limit
+is not added to the set, in the same way that the client's recorded rate is not
+updated in the same situation.
+
+When you combine the unique= and readonly options, the specific unique= value
+is ignored, and Exim just retrieves the client's stored rate.
+
+The unique= mechanism needs more space in the ratelimit database than the other
+ratelimit options in order to store the event set. The number of unique values
+is potentially as large as the rate limit, so the extra space required
+increases with larger limits.
+
+The uniqueification is not perfect: there is a small probability that Exim will
+think a new event has happened before. If the sender's rate is less than the
+limit, Exim should be more than 99.9% correct. However in strict mode the
+measured rate can go above the limit, in which case Exim may under-count events
+by a significant margin. Fortunately, if the rate is high enough (2.7 times the
+limit) that the false positive rate goes above 9%, then Exim will throw away
+the over-full event set before the measured rate falls below the limit.
+Therefore the only harm should be that exceptionally high sending rates are
+logged incorrectly; any countermeasures you configure will be as effective as
+intended.
+
+
+42.43 Using rate limiting
+-------------------------
+
+Exim's other ACL facilities are used to define what counter-measures are taken
+when the rate limit is exceeded. This might be anything from logging a warning
+(for example, while measuring existing sending rates in order to define
+policy), through time delays to slow down fast senders, up to rejecting the
+message. For example:
+
+# Log all senders' rates
+warn ratelimit = 0 / 1h / strict
+     log_message = Sender rate $sender_rate / $sender_rate_period
+
+# Slow down fast senders; note the need to truncate $sender_rate
+# at the decimal point.
+warn ratelimit = 100 / 1h / per_rcpt / strict
+     delay     = ${eval: ${sg{$sender_rate}{[.].*}{}} - \
+                   $sender_rate_limit }s
+
+# Keep authenticated users under control
+deny authenticated = *
+     ratelimit = 100 / 1d / strict / $authenticated_id
+
+# System-wide rate limit
+defer message = Sorry, too busy. Try again later.
+     ratelimit = 10 / 1s / $primary_hostname
+
+# Restrict incoming rate from each host, with a default
+# set using a macro and special cases looked up in a table.
+defer message = Sender rate exceeds $sender_rate_limit \
+               messages per $sender_rate_period
+     ratelimit = ${lookup {$sender_host_address} \
+                   cdb {DB/ratelimits.cdb} \
+                   {$value} {RATELIMIT} }
+
+Warning: If you have a busy server with a lot of ratelimit tests, especially
+with the per_rcpt option, you may suffer from a performance bottleneck caused
+by locking on the ratelimit hints database. Apart from making your ACLs less
+complicated, you can reduce the problem by using a RAM disk for Exim's hints
+directory (usually /var/spool/exim/db/). However this means that Exim will lose
+its hints data after a reboot (including retry hints, the callout cache, and
+ratelimit data).
+
+
+42.44 Address verification
+--------------------------
+
+Several of the verify conditions described in section 42.26 cause addresses to
+be verified. Section 42.48 discusses the reporting of sender verification
+failures. The verification conditions can be followed by options that modify
+the verification process. The options are separated from the keyword and from
+each other by slashes, and some of them contain parameters. For example:
+
+verify = sender/callout
+verify = recipient/defer_ok/callout=10s,defer_ok
+
+The first stage of address verification, which always happens, is to run the
+address through the routers, in "verify mode". Routers can detect the
+difference between verification and routing for delivery, and their actions can
+be varied by a number of generic options such as verify and verify_only (see
+chapter 15). If routing fails, verification fails. The available options are as
+follows:
+
+  * If the callout option is specified, successful routing to one or more
+    remote hosts is followed by a "callout" to those hosts as an additional
+    check. Callouts and their sub-options are discussed in the next section.
+
+  * If there is a defer error while doing verification routing, the ACL
+    normally returns "defer". However, if you include defer_ok in the options,
+    the condition is forced to be true instead. Note that this is a main
+    verification option as well as a suboption for callouts.
+
+  * The no_details option is covered in section 42.48, which discusses the
+    reporting of sender address verification failures.
+
+  * The success_on_redirect option causes verification always to succeed
+    immediately after a successful redirection. By default, if a redirection
+    generates just one address, that address is also verified. See further
+    discussion in section 42.49.
+
+After an address verification failure, $acl_verify_message contains the error
+message that is associated with the failure. It can be preserved by coding like
+this:
+
+warn  !verify = sender
+       set acl_m0 = $acl_verify_message
+
+If you are writing your own custom rejection message or log message when
+denying access, you can use this variable to include information about the
+verification failure.
+
+In addition, $sender_verify_failure or $recipient_verify_failure (as
+appropriate) contains one of the following words:
+
+  * qualify: The address was unqualified (no domain), and the message was
+    neither local nor came from an exempted host.
+
+  * route: Routing failed.
+
+  * mail: Routing succeeded, and a callout was attempted; rejection occurred at
+    or before the MAIL command (that is, on initial connection, HELO, or MAIL).
+
+  * recipient: The RCPT command in a callout was rejected.
+
+  * postmaster: The postmaster check in a callout was rejected.
+
+The main use of these variables is expected to be to distinguish between
+rejections of MAIL and rejections of RCPT in callouts.
+
+
+42.45 Callout verification
+--------------------------
+
+For non-local addresses, routing verifies the domain, but is unable to do any
+checking of the local part. There are situations where some means of verifying
+the local part is desirable. One way this can be done is to make an SMTP 
+callback to a delivery host for the sender address or a callforward to a
+subsequent host for a recipient address, to see if the host accepts the
+address. We use the term callout to cover both cases. Note that for a sender
+address, the callback is not to the client host that is trying to deliver the
+message, but to one of the hosts that accepts incoming mail for the sender's
+domain.
+
+Exim does not do callouts by default. If you want them to happen, you must
+request them by setting appropriate options on the verify condition, as
+described below. This facility should be used with care, because it can add a
+lot of resource usage to the cost of verifying an address. However, Exim does
+cache the results of callouts, which helps to reduce the cost. Details of
+caching are in section 42.47.
+
+Recipient callouts are usually used only between hosts that are controlled by
+the same administration. For example, a corporate gateway host could use
+callouts to check for valid recipients on an internal mailserver. A successful
+callout does not guarantee that a real delivery to the address would succeed;
+on the other hand, a failing callout does guarantee that a delivery would fail.
+
+If the callout option is present on a condition that verifies an address, a
+second stage of verification occurs if the address is successfully routed to
+one or more remote hosts. The usual case is routing by a dnslookup or a 
+manualroute router, where the router specifies the hosts. However, if a router
+that does not set up hosts routes to an smtp transport with a hosts setting,
+the transport's hosts are used. If an smtp transport has hosts_override set,
+its hosts are always used, whether or not the router supplies a host list.
+Callouts are only supported on smtp transports.
+
+The port that is used is taken from the transport, if it is specified and is a
+remote transport. (For routers that do verification only, no transport need be
+specified.) Otherwise, the default SMTP port is used. If a remote transport
+specifies an outgoing interface, this is used; otherwise the interface is not
+specified. Likewise, the text that is used for the HELO command is taken from
+the transport's helo_data option; if there is no transport, the value of
+$smtp_active_hostname is used.
+
+For a sender callout check, Exim makes SMTP connections to the remote hosts, to
+test whether a bounce message could be delivered to the sender address. The
+following SMTP commands are sent:
+
+HELO <local host name>
+MAIL FROM:<>
+RCPT TO:<the address to be tested>
+QUIT
+
+LHLO is used instead of HELO if the transport's protocol option is set to
+"lmtp".
+
+The callout may use EHLO, AUTH and/or STARTTLS given appropriate option
+settings.
+
+A recipient callout check is similar. By default, it also uses an empty address
+for the sender. This default is chosen because most hosts do not make use of
+the sender address when verifying a recipient. Using the same address means
+that a single cache entry can be used for each recipient. Some sites, however,
+do make use of the sender address when verifying. These are catered for by the 
+use_sender and use_postmaster options, described in the next section.
+
+If the response to the RCPT command is a 2xx code, the verification succeeds.
+If it is 5xx, the verification fails. For any other condition, Exim tries the
+next host, if any. If there is a problem with all the remote hosts, the ACL
+yields "defer", unless the defer_ok parameter of the callout option is given,
+in which case the condition is forced to succeed.
+
+A callout may take a little time. For this reason, Exim normally flushes SMTP
+output before performing a callout in an ACL, to avoid unexpected timeouts in
+clients when the SMTP PIPELINING extension is in use. The flushing can be
+disabled by using a control modifier to set no_callout_flush.
+
+
+42.46 Additional parameters for callouts
+----------------------------------------
+
+The callout option can be followed by an equals sign and a number of optional
+parameters, separated by commas. For example:
+
+verify = recipient/callout=10s,defer_ok
+
+The old syntax, which had callout_defer_ok and check_postmaster as separate
+verify options, is retained for backwards compatibility, but is now deprecated.
+The additional parameters for callout are as follows:
+
+<a time interval>
+
+    This specifies the timeout that applies for the callout attempt to each
+    host. For example:
+
+    verify = sender/callout=5s
+
+    The default is 30 seconds. The timeout is used for each response from the
+    remote host. It is also used for the initial connection, unless overridden
+    by the connect parameter.
+
+connect = <time interval>
+
+    This parameter makes it possible to set a different (usually smaller)
+    timeout for making the SMTP connection. For example:
+
+    verify = sender/callout=5s,connect=1s
+
+    If not specified, this timeout defaults to the general timeout value.
+
+defer_ok
+
+    When this parameter is present, failure to contact any host, or any other
+    kind of temporary error, is treated as success by the ACL. However, the
+    cache is not updated in this circumstance.
+
+fullpostmaster
+
+    This operates like the postmaster option (see below), but if the check for 
+    postmaster@domain fails, it tries just postmaster, without a domain, in
+    accordance with the specification in RFC 2821. The RFC states that the
+    unqualified address postmaster should be accepted.
+
+mailfrom = <email address>
+
+    When verifying addresses in header lines using the header_sender
+    verification option, Exim behaves by default as if the addresses are
+    envelope sender addresses from a message. Callout verification therefore
+    tests to see whether a bounce message could be delivered, by using an empty
+    address in the MAIL command. However, it is arguable that these addresses
+    might never be used as envelope senders, and could therefore justifiably
+    reject bounce messages (empty senders). The mailfrom callout parameter
+    allows you to specify what address to use in the MAIL command. For example:
+
+    require  verify = header_sender/callout=mailfrom=abcd@x.y.z
+
+    This parameter is available only for the header_sender verification option.
+
+maxwait = <time interval>
+
+    This parameter sets an overall timeout for performing a callout
+    verification. For example:
+
+    verify = sender/callout=5s,maxwait=30s
+
+    This timeout defaults to four times the callout timeout for individual SMTP
+    commands. The overall timeout applies when there is more than one host that
+    can be tried. The timeout is checked before trying the next host. This
+    prevents very long delays if there are a large number of hosts and all are
+    timing out (for example, when network connections are timing out).
+
+no_cache
+
+    When this parameter is given, the callout cache is neither read nor
+    updated.
+
+postmaster
+
+    When this parameter is set, a successful callout check is followed by a
+    similar check for the local part postmaster at the same domain. If this
+    address is rejected, the callout fails (but see fullpostmaster above). The
+    result of the postmaster check is recorded in a cache record; if it is a
+    failure, this is used to fail subsequent callouts for the domain without a
+    connection being made, until the cache record expires.
+
+postmaster_mailfrom = <email address>
+
+    The postmaster check uses an empty sender in the MAIL command by default.
+    You can use this parameter to do a postmaster check using a different
+    address. For example:
+
+    require  verify = sender/callout=postmaster_mailfrom=abc@x.y.z
+
+    If both postmaster and postmaster_mailfrom are present, the rightmost one
+    overrides. The postmaster parameter is equivalent to this example:
+
+    require  verify = sender/callout=postmaster_mailfrom=
+
+    Warning: The caching arrangements for postmaster checking do not take
+    account of the sender address. It is assumed that either the empty address
+    or a fixed non-empty address will be used. All that Exim remembers is that
+    the postmaster check for the domain succeeded or failed.
+
+random
+
+    When this parameter is set, before doing the normal callout check, Exim
+    does a check for a "random" local part at the same domain. The local part
+    is not really random - it is defined by the expansion of the option 
+    callout_random_local_part, which defaults to
+
+    $primary_hostname-$tod_epoch-testing
+
+    The idea here is to try to determine whether the remote host accepts all
+    local parts without checking. If it does, there is no point in doing
+    callouts for specific local parts. If the "random" check succeeds, the
+    result is saved in a cache record, and used to force the current and
+    subsequent callout checks to succeed without a connection being made, until
+    the cache record expires.
+
+use_postmaster
+
+    This parameter applies to recipient callouts only. For example:
+
+    deny  !verify = recipient/callout=use_postmaster
+
+    It causes a non-empty postmaster address to be used in the MAIL command
+    when performing the callout for the recipient, and also for a "random"
+    check if that is configured. The local part of the address is "postmaster"
+    and the domain is the contents of $qualify_domain.
+
+use_sender
+
+    This option applies to recipient callouts only. For example:
+
+    require  verify = recipient/callout=use_sender
+
+    It causes the message's actual sender address to be used in the MAIL
+    command when performing the callout, instead of an empty address. There is
+    no need to use this option unless you know that the called hosts make use
+    of the sender when checking recipients. If used indiscriminately, it
+    reduces the usefulness of callout caching.
+
+If you use any of the parameters that set a non-empty sender for the MAIL
+command (mailfrom, postmaster_mailfrom, use_postmaster, or use_sender), you
+should think about possible loops. Recipient checking is usually done between
+two hosts that are under the same management, and the host that receives the
+callouts is not normally configured to do callouts itself. Therefore, it is
+normally safe to use use_postmaster or use_sender in these circumstances.
+
+However, if you use a non-empty sender address for a callout to an arbitrary
+host, there is the likelihood that the remote host will itself initiate a
+callout check back to your host. As it is checking what appears to be a message
+sender, it is likely to use an empty address in MAIL, thus avoiding a callout
+loop. However, to be on the safe side it would be best to set up your own ACLs
+so that they do not do sender verification checks when the recipient is the
+address you use for header sender or postmaster callout checking.
+
+Another issue to think about when using non-empty senders for callouts is
+caching. When you set mailfrom or use_sender, the cache record is keyed by the
+sender/recipient combination; thus, for any given recipient, many more actual
+callouts are performed than when an empty sender or postmaster is used.
+
+
+42.47 Callout caching
+---------------------
+
+Exim caches the results of callouts in order to reduce the amount of resources
+used, unless you specify the no_cache parameter with the callout option. A
+hints database called "callout" is used for the cache. Two different record
+types are used: one records the result of a callout check for a specific
+address, and the other records information that applies to the entire domain
+(for example, that it accepts the local part postmaster).
+
+When an original callout fails, a detailed SMTP error message is given about
+the failure. However, for subsequent failures use the cache data, this message
+is not available.
+
+The expiry times for negative and positive address cache records are
+independent, and can be set by the global options callout_negative_expire
+(default 2h) and callout_positive_expire (default 24h), respectively.
+
+If a host gives a negative response to an SMTP connection, or rejects any
+commands up to and including
+
+MAIL FROM:<>
+
+(but not including the MAIL command with a non-empty address), any callout
+attempt is bound to fail. Exim remembers such failures in a domain cache
+record, which it uses to fail callouts for the domain without making new
+connections, until the domain record times out. There are two separate expiry
+times for domain cache records: callout_domain_negative_expire (default 3h) and
+callout_domain_positive_expire (default 7d).
+
+Domain records expire when the negative expiry time is reached if callouts
+cannot be made for the domain, or if the postmaster check failed. Otherwise,
+they expire when the positive expiry time is reached. This ensures that, for
+example, a host that stops accepting "random" local parts will eventually be
+noticed.
+
+The callout caching mechanism is based on the domain of the address that is
+being tested. If the domain routes to several hosts, it is assumed that their
+behaviour will be the same.
+
+
+42.48 Sender address verification reporting
+-------------------------------------------
+
+See section 42.44 for a general discussion of verification. When sender
+verification fails in an ACL, the details of the failure are given as
+additional output lines before the 550 response to the relevant SMTP command
+(RCPT or DATA). For example, if sender callout is in use, you might see:
+
+MAIL FROM:<xyz@abc.example>
+250 OK
+RCPT TO:<pqr@def.example>
+550-Verification failed for <xyz@abc.example>
+550-Called:   192.168.34.43
+550-Sent:     RCPT TO:<xyz@abc.example>
+550-Response: 550 Unknown local part xyz in <xyz@abc.example>
+550 Sender verification failed
+
+If more than one RCPT command fails in the same way, the details are given only
+for the first of them. However, some administrators do not want to send out
+this much information. You can suppress the details by adding "/no_details" to
+the ACL statement that requests sender verification. For example:
+
+verify = sender/no_details
+
+
+42.49 Redirection while verifying
+---------------------------------
+
+A dilemma arises when a local address is redirected by aliasing or forwarding
+during verification: should the generated addresses themselves be verified, or
+should the successful expansion of the original address be enough to verify it?
+By default, Exim takes the following pragmatic approach:
+
+  * When an incoming address is redirected to just one child address,
+    verification continues with the child address, and if that fails to verify,
+    the original verification also fails.
+
+  * When an incoming address is redirected to more than one child address,
+    verification does not continue. A success result is returned.
+
+This seems the most reasonable behaviour for the common use of aliasing as a
+way of redirecting different local parts to the same mailbox. It means, for
+example, that a pair of alias entries of the form
+
+A.Wol:   aw123
+aw123:   :fail: Gone away, no forwarding address
+
+work as expected, with both local parts causing verification failure. When a
+redirection generates more than one address, the behaviour is more like a
+mailing list, where the existence of the alias itself is sufficient for
+verification to succeed.
+
+It is possible, however, to change the default behaviour so that all successful
+redirections count as successful verifications, however many new addresses are
+generated. This is specified by the success_on_redirect verification option.
+For example:
+
+require verify = recipient/success_on_redirect/callout=10s
+
+In this example, verification succeeds if a router generates a new address, and
+the callout does not occur, because no address was routed to a remote host.
+
+When verification is being tested via the -bv option, the treatment of
+redirections is as just described, unless the -v or any debugging option is
+also specified. In that case, full verification is done for every generated
+address and a report is output for each of them.
+
+
+42.50 Client SMTP authorization (CSA)
+-------------------------------------
+
+Client SMTP Authorization is a system that allows a site to advertise which
+machines are and are not permitted to send email. This is done by placing
+special SRV records in the DNS; these are looked up using the client's HELO
+domain. At the time of writing, CSA is still an Internet Draft. Client SMTP
+Authorization checks in Exim are performed by the ACL condition:
+
+verify = csa
+
+This fails if the client is not authorized. If there is a DNS problem, or if no
+valid CSA SRV record is found, or if the client is authorized, the condition
+succeeds. These three cases can be distinguished using the expansion variable
+$csa_status, which can take one of the values "fail", "defer", "unknown", or
+"ok". The condition does not itself defer because that would be likely to cause
+problems for legitimate email.
+
+The error messages produced by the CSA code include slightly more detail. If
+$csa_status is "defer", this may be because of problems looking up the CSA SRV
+record, or problems looking up the CSA target address record. There are four
+reasons for $csa_status being "fail":
+
+  * The client's host name is explicitly not authorized.
+
+  * The client's IP address does not match any of the CSA target IP addresses.
+
+  * The client's host name is authorized but it has no valid target IP
+    addresses (for example, the target's addresses are IPv6 and the client is
+    using IPv4).
+
+  * The client's host name has no CSA SRV record but a parent domain has
+    asserted that all subdomains must be explicitly authorized.
+
+The csa verification condition can take an argument which is the domain to use
+for the DNS query. The default is:
+
+verify = csa/$sender_helo_name
+
+This implementation includes an extension to CSA. If the query domain is an
+address literal such as [192.0.2.95], or if it is a bare IP address, Exim
+searches for CSA SRV records in the reverse DNS as if the HELO domain was (for
+example) 95.2.0.192.in-addr.arpa. Therefore it is meaningful to say:
+
+verify = csa/$sender_host_address
+
+In fact, this is the check that Exim performs if the client does not say HELO.
+This extension can be turned off by setting the main configuration option 
+dns_csa_use_reverse to be false.
+
+If a CSA SRV record is not found for the domain itself, a search is performed
+through its parent domains for a record which might be making assertions about
+subdomains. The maximum depth of this search is limited using the main
+configuration option dns_csa_search_limit, which is 5 by default. Exim does not
+look for CSA SRV records in a top level domain, so the default settings handle
+HELO domains as long as seven (hostname.five.four.three.two.one.com). This
+encompasses the vast majority of legitimate HELO domains.
+
+The dnsdb lookup also has support for CSA. Although dnsdb also supports direct
+SRV lookups, this is not sufficient because of the extra parent domain search
+behaviour of CSA, and (as with PTR lookups) dnsdb also turns IP addresses into
+lookups in the reverse DNS space. The result of a successful lookup such as:
+
+${lookup dnsdb {csa=$sender_helo_name}}
+
+has two space-separated fields: an authorization code and a target host name.
+The authorization code can be "Y" for yes, "N" for no, "X" for explicit
+authorization required but absent, or "?" for unknown.
+
+
+42.51 Bounce address tag validation
+-----------------------------------
+
+Bounce address tag validation (BATV) is a scheme whereby the envelope senders
+of outgoing messages have a cryptographic, timestamped "tag" added to them.
+Genuine incoming bounce messages should therefore always be addressed to
+recipients that have a valid tag. This scheme is a way of detecting unwanted
+bounce messages caused by sender address forgeries (often called "collateral
+spam"), because the recipients of such messages do not include valid tags.
+
+There are two expansion items to help with the implementation of the BATV
+"prvs" (private signature) scheme in an Exim configuration. This scheme signs
+the original envelope sender address by using a simple key to add a hash of the
+address and some time-based randomizing information. The prvs expansion item
+creates a signed address, and the prvscheck expansion item checks one. The
+syntax of these expansion items is described in section 11.5.
+
+As an example, suppose the secret per-address keys are stored in an MySQL
+database. A query to look up the key for an address could be defined as a macro
+like this:
+
+PRVSCHECK_SQL = ${lookup mysql{SELECT secret FROM batv_prvs \
+                WHERE sender='${quote_mysql:$prvscheck_address}'\
+                }{$value}}
+
+Suppose also that the senders who make use of BATV are defined by an address
+list called batv_senders. Then, in the ACL for RCPT commands, you could use
+this:
+
+# Bounces: drop unsigned addresses for BATV senders
+deny message = This address does not send an unsigned reverse path
+     senders = :
+     recipients = +batv_senders
+
+# Bounces: In case of prvs-signed address, check signature.
+deny message = Invalid reverse path signature.
+     senders = :
+     condition  = ${prvscheck {$local_part@$domain}\
+                  {PRVSCHECK_SQL}{1}}
+     !condition = $prvscheck_result
+
+The first statement rejects recipients for bounce messages that are addressed
+to plain BATV sender addresses, because it is known that BATV senders do not
+send out messages with plain sender addresses. The second statement rejects
+recipients that are prvs-signed, but with invalid signatures (either because
+the key is wrong, or the signature has timed out).
+
+A non-prvs-signed address is not rejected by the second statement, because the 
+prvscheck expansion yields an empty string if its first argument is not a
+prvs-signed address, thus causing the condition condition to be false. If the
+first argument is a syntactically valid prvs-signed address, the yield is the
+third string (in this case "1"), whether or not the cryptographic and timeout
+checks succeed. The $prvscheck_result variable contains the result of the
+checks (empty for failure, "1" for success).
+
+There is one more issue you must consider when implementing prvs-signing: you
+have to ensure that the routers accept prvs-signed addresses and deliver them
+correctly. The easiest way to handle this is to use a redirect router to remove
+the signature with a configuration along these lines:
+
+batv_redirect:
+  driver = redirect
+  data = ${prvscheck {$local_part@$domain}{PRVSCHECK_SQL}}
+
+This works because, if the third argument of prvscheck is empty, the result of
+the expansion of a prvs-signed address is the decoded value of the original
+address. This router should probably be the first of your routers that handles
+local addresses.
+
+To create BATV-signed addresses in the first place, a transport of this form
+can be used:
+
+external_smtp_batv:
+  driver = smtp
+  return_path = ${prvs {$return_path} \
+                       {${lookup mysql{SELECT \
+                       secret FROM batv_prvs WHERE \
+                       sender='${quote_mysql:$sender_address}'} \
+                       {$value}fail}}}
+
+If no key can be found for the existing return path, no signing takes place.
+
+
+42.52 Using an ACL to control relaying
+--------------------------------------
+
+An MTA is said to relay a message if it receives it from some host and delivers
+it directly to another host as a result of a remote address contained within
+it. Redirecting a local address via an alias or forward file and then passing
+the message on to another host is not relaying, but a redirection as a result
+of the "percent hack" is.
+
+Two kinds of relaying exist, which are termed "incoming" and "outgoing". A host
+which is acting as a gateway or an MX backup is concerned with incoming
+relaying from arbitrary hosts to a specific set of domains. On the other hand,
+a host which is acting as a smart host for a number of clients is concerned
+with outgoing relaying from those clients to the Internet at large. Often the
+same host is fulfilling both functions, but in principle these two kinds of
+relaying are entirely independent. What is not wanted is the transmission of
+mail from arbitrary remote hosts through your system to arbitrary domains.
+
+You can implement relay control by means of suitable statements in the ACL that
+runs for each RCPT command. For convenience, it is often easiest to use Exim's
+named list facility to define the domains and hosts involved. For example,
+suppose you want to do the following:
+
+  * Deliver a number of domains to mailboxes on the local host (or process them
+    locally in some other way). Let's say these are my.dom1.example and 
+    my.dom2.example.
+
+  * Relay mail for a number of other domains for which you are the secondary
+    MX. These might be friend1.example and friend2.example.
+
+  * Relay mail from the hosts on your local LAN, to whatever domains are
+    involved. Suppose your LAN is 192.168.45.0/24.
+
+In the main part of the configuration, you put the following definitions:
+
+domainlist local_domains    = my.dom1.example : my.dom2.example
+domainlist relay_to_domains = friend1.example : friend2.example
+hostlist   relay_from_hosts = 192.168.45.0/24
+
+Now you can use these definitions in the ACL that is run for every RCPT
+command:
+
+acl_check_rcpt:
+  accept domains = +local_domains : +relay_to_domains
+  accept hosts   = +relay_from_hosts
+
+The first statement accepts any RCPT command that contains an address in the
+local or relay domains. For any other domain, control passes to the second
+statement, which accepts the command only if it comes from one of the relay
+hosts. In practice, you will probably want to make your ACL more sophisticated
+than this, for example, by including sender and recipient verification. The
+default configuration includes a more comprehensive example, which is described
+in chapter 7.
+
+
+42.53 Checking a relay configuration
+------------------------------------
+
+You can check the relay characteristics of your configuration in the same way
+that you can test any ACL behaviour for an incoming SMTP connection, by using
+the -bh option to run a fake SMTP session with which you interact.
+
+For specifically testing for unwanted relaying, the host 
+relay-test.mail-abuse.org provides a useful service. If you telnet to this host
+from the host on which Exim is running, using the normal telnet port, you will
+see a normal telnet connection message and then quite a long delay. Be patient.
+The remote host is making an SMTP connection back to your host, and trying a
+number of common probes to test for open relay vulnerability. The results of
+the tests will eventually appear on your terminal.
+
+
+
+===============================================================================
+43. CONTENT SCANNING AT ACL TIME
+
+The extension of Exim to include content scanning at ACL time, formerly known
+as "exiscan", was originally implemented as a patch by Tom Kistner. The code
+was integrated into the main source for Exim release 4.50, and Tom continues to
+maintain it. Most of the wording of this chapter is taken from Tom's
+specification.
+
+It is also possible to scan the content of messages at other times. The
+local_scan() function (see chapter 44) allows for content scanning after all
+the ACLs have run. A transport filter can be used to scan messages at delivery
+time (see the transport_filter option, described in chapter 24).
+
+If you want to include the ACL-time content-scanning features when you compile
+Exim, you need to arrange for WITH_CONTENT_SCAN to be defined in your Local/
+Makefile. When you do that, the Exim binary is built with:
+
+  * Two additional ACLs (acl_smtp_mime and acl_not_smtp_mime) that are run for
+    all MIME parts for SMTP and non-SMTP messages, respectively.
+
+  * Additional ACL conditions and modifiers: decode, malware, mime_regex, regex
+    , and spam. These can be used in the ACL that is run at the end of message
+    reception (the acl_smtp_data ACL).
+
+  * An additional control feature ("no_mbox_unspool") that saves spooled copies
+    of messages, or parts of messages, for debugging purposes.
+
+  * Additional expansion variables that are set in the new ACL and by the new
+    conditions.
+
+  * Two new main configuration options: av_scanner and spamd_address.
+
+There is another content-scanning configuration option for Local/Makefile,
+called WITH_OLD_DEMIME. If this is set, the old, deprecated demime ACL
+condition is compiled, in addition to all the other content-scanning features.
+
+Content-scanning is continually evolving, and new features are still being
+added. While such features are still unstable and liable to incompatible
+changes, they are made available in Exim by setting options whose names begin
+EXPERIMENTAL_ in Local/Makefile. Such features are not documented in this
+manual. You can find out about them by reading the file called doc/
+experimental.txt.
+
+All the content-scanning facilities work on a MBOX copy of the message that is
+temporarily created in a file called:
+
+<spool_directory>/scan/<message_id>/<message_id>.eml
+
+The .eml extension is a friendly hint to virus scanners that they can expect an
+MBOX-like structure inside that file. The file is created when the first
+content scanning facility is called. Subsequent calls to content scanning
+conditions open the same file again. The directory is recursively removed when
+the acl_smtp_data ACL has finished running, unless
+
+control = no_mbox_unspool
+
+has been encountered. When the MIME ACL decodes files, they are put into the
+same directory by default.
+
+
+43.1 Scanning for viruses
+-------------------------
+
+The malware ACL condition lets you connect virus scanner software to Exim. It
+supports a "generic" interface to scanners called via the shell, and
+specialized interfaces for "daemon" type virus scanners, which are resident in
+memory and thus are much faster.
+
+You can set the av_scanner option in first part of the Exim configuration file
+to specify which scanner to use, together with any additional options that are
+needed. The basic syntax is as follows:
+
+av_scanner = <scanner-type>:<option1>:<option2>:[...]
+
+If you do not set av_scanner, it defaults to
+
+av_scanner = sophie:/var/run/sophie
+
+If the value of av_scanner starts with a dollar character, it is expanded
+before use. The usual list-parsing of the content (see 6.19) applies. The
+following scanner types are supported in this release:
+
+aveserver
+
+    This is the scanner daemon of Kaspersky Version 5. You can get a trial
+    version at http://www.kaspersky.com. This scanner type takes one option,
+    which is the path to the daemon's UNIX socket. The default is shown in this
+    example:
+
+    av_scanner = aveserver:/var/run/aveserver
+
+clamd
+
+    This daemon-type scanner is GPL and free. You can get it at http://
+    www.clamav.net/. Some older versions of clamd do not seem to unpack MIME
+    containers, so it used to be recommended to unpack MIME attachments in the
+    MIME ACL. This no longer believed to be necessary. One option is required:
+    either the path and name of a UNIX socket file, or a hostname or IP number,
+    and a port, separated by space, as in the second of these examples:
+
+    av_scanner = clamd:/opt/clamd/socket
+    av_scanner = clamd:192.0.2.3 1234
+    av_scanner = clamd:192.0.2.3 1234:local
+    av_scanner = clamd:192.0.2.3 1234 : 192.0.2.4 1234
+
+    If the value of av_scanner points to a UNIX socket file or contains the
+    local keyword, then the ClamAV interface will pass a filename containing
+    the data to be scanned, which will should normally result in less I/O
+    happening and be more efficient. Normally in the TCP case, the data is
+    streamed to ClamAV as Exim does not assume that there is a common
+    filesystem with the remote host. There is an option WITH_OLD_CLAMAV_STREAM
+    in src/EDITME available, should you be running a version of ClamAV prior to
+    0.95.
+
+    The final example shows that multiple TCP targets can be specified. Exim
+    will randomly use one for each incoming email (i.e. it load balances them).
+    Note that only TCP targets may be used if specifying a list of scanners; a
+    UNIX socket cannot be mixed in with TCP targets. If one of the servers
+    becomes unavailable, Exim will try the remaining one(s) until it finds one
+    that works. When a clamd server becomes unreachable, Exim will log a
+    message. Exim does not keep track of scanner state between multiple
+    messages, and the scanner selection is random, so the message will get
+    logged in the mainlog for each email that the down scanner gets chosen
+    first (message wrapped to be readable):
+
+    2013-10-09 14:30:39 1VTumd-0000Y8-BQ malware acl condition:
+       clamd: connection to localhost, port 3310 failed
+       (Connection refused)
+
+    If the option is unset, the default is /tmp/clamd. Thanks to David Saez for
+    contributing the code for this scanner.
+
+cmdline
+
+    This is the keyword for the generic command line scanner interface. It can
+    be used to attach virus scanners that are invoked from the shell. This
+    scanner type takes 3 mandatory options:
+
+     1. The full path and name of the scanner binary, with all command line
+        options, and a placeholder ("%s") for the directory to scan.
+
+     2. A regular expression to match against the STDOUT and STDERR output of
+        the virus scanner. If the expression matches, a virus was found. You
+        must make absolutely sure that this expression matches on "virus
+        found". This is called the "trigger" expression.
+
+     3. Another regular expression, containing exactly one pair of parentheses,
+        to match the name of the virus found in the scanners output. This is
+        called the "name" expression.
+
+    For example, Sophos Sweep reports a virus on a line like this:
+
+    Virus 'W32/Magistr-B' found in file ./those.bat
+
+    For the trigger expression, we can match the phrase "found in file". For
+    the name expression, we want to extract the W32/Magistr-B string, so we can
+    match for the single quotes left and right of it. Altogether, this makes
+    the configuration setting:
+
+    av_scanner = cmdline:\
+                 /path/to/sweep -ss -all -rec -archive %s:\
+                 found in file:'(.+)'
+
+drweb
+
+    The DrWeb daemon scanner (http://www.sald.com/) interface takes one
+    argument, either a full path to a UNIX socket, or an IP address and port
+    separated by white space, as in these examples:
+
+    av_scanner = drweb:/var/run/drwebd.sock
+    av_scanner = drweb:192.168.2.20 31337
+
+    If you omit the argument, the default path /usr/local/drweb/run/drwebd.sock
+    is used. Thanks to Alex Miller for contributing the code for this scanner.
+
+fsecure
+
+    The F-Secure daemon scanner (http://www.f-secure.com) takes one argument
+    which is the path to a UNIX socket. For example:
+
+    av_scanner = fsecure:/path/to/.fsav
+
+    If no argument is given, the default is /var/run/.fsav. Thanks to Johan
+    Thelmen for contributing the code for this scanner.
+
+kavdaemon
+
+    This is the scanner daemon of Kaspersky Version 4. This version of the
+    Kaspersky scanner is outdated. Please upgrade (see aveserver above). This
+    scanner type takes one option, which is the path to the daemon's UNIX
+    socket. For example:
+
+    av_scanner = kavdaemon:/opt/AVP/AvpCtl
+
+    The default path is /var/run/AvpCtl.
+
+mksd
+
+    This is a daemon type scanner that is aimed mainly at Polish users, though
+    some parts of documentation are now available in English. You can get it at
+    http://linux.mks.com.pl/. The only option for this scanner type is the
+    maximum number of processes used simultaneously to scan the attachments,
+    provided that the demime facility is employed and also provided that mksd
+    has been run with at least the same number of child processes. For example:
+
+    av_scanner = mksd:2
+
+    You can safely omit this option (the default value is 1).
+
+sock
+
+    This is a general-purpose way of talking to simple scanner daemons running
+    on the local machine. There are four options: an address (which may be an
+    IP addres and port, or the path of a Unix socket), a commandline to send
+    (may include a single %s which will be replaced with the path to the mail
+    file to be scanned), an RE to trigger on from the returned data, an RE to
+    extract malware_name from the returned data. For example:
+
+    av_scanner = sock:127.0.0.1 6001:%s:(SPAM|VIRUS):(.*)\$
+
+    Default for the socket specifier is /tmp/malware.sock. Default for the
+    commandline is %s\n. Both regular-expressions are required.
+
+sophie
+
+    Sophie is a daemon that uses Sophos' libsavi library to scan for viruses.
+    You can get Sophie at http://www.clanfield.info/sophie/. The only option
+    for this scanner type is the path to the UNIX socket that Sophie uses for
+    client communication. For example:
+
+    av_scanner = sophie:/tmp/sophie
+
+    The default path is /var/run/sophie, so if you are using this, you can omit
+    the option.
+
+When av_scanner is correctly set, you can use the malware condition in the DATA
+ACL. Note: You cannot use the malware condition in the MIME ACL.
+
+The av_scanner option is expanded each time malware is called. This makes it
+possible to use different scanners. See further below for an example. The 
+malware condition caches its results, so when you use it multiple times for the
+same message, the actual scanning process is only carried out once. However,
+using expandable items in av_scanner disables this caching, in which case each
+use of the malware condition causes a new scan of the message.
+
+The malware condition takes a right-hand argument that is expanded before use.
+It can then be one of
+
+  * "true", "*", or "1", in which case the message is scanned for viruses. The
+    condition succeeds if a virus was found, and fail otherwise. This is the
+    recommended usage.
+
+  * "false" or "0" or an empty string, in which case no scanning is done and
+    the condition fails immediately.
+
+  * A regular expression, in which case the message is scanned for viruses. The
+    condition succeeds if a virus is found and its name matches the regular
+    expression. This allows you to take special actions on certain types of
+    virus.
+
+You can append "/defer_ok" to the malware condition to accept messages even if
+there is a problem with the virus scanner. Otherwise, such a problem causes the
+ACL to defer.
+
+When a virus is found, the condition sets up an expansion variable called
+$malware_name that contains the name of the virus. You can use it in a message
+modifier that specifies the error returned to the sender, and/or in logging
+data.
+
+If your virus scanner cannot unpack MIME and TNEF containers itself, you should
+use the demime condition (see section 43.6) before the malware condition.
+
+Beware the interaction of Exim's message_size_limit with any size limits
+imposed by your anti-virus scanner.
+
+Here is a very simple scanning example:
+
+deny message = This message contains malware ($malware_name)
+     demime = *
+     malware = *
+
+The next example accepts messages when there is a problem with the scanner:
+
+deny message = This message contains malware ($malware_name)
+     demime = *
+     malware = */defer_ok
+
+The next example shows how to use an ACL variable to scan with both sophie and
+aveserver. It assumes you have set:
+
+av_scanner = $acl_m0
+
+in the main Exim configuration.
+
+deny message = This message contains malware ($malware_name)
+     set acl_m0 = sophie
+     malware = *
+
+deny message = This message contains malware ($malware_name)
+     set acl_m0 = aveserver
+     malware = *
+
+
+43.2 Scanning with SpamAssassin
+-------------------------------
+
+The spam ACL condition calls SpamAssassin's spamd daemon to get a spam score
+and a report for the message. You can get SpamAssassin at http://
+www.spamassassin.org, or, if you have a working Perl installation, you can use
+CPAN by running:
+
+perl -MCPAN -e 'install Mail::SpamAssassin'
+
+SpamAssassin has its own set of configuration files. Please review its
+documentation to see how you can tweak it. The default installation should work
+nicely, however.
+
+After having installed and configured SpamAssassin, start the spamd daemon. By
+default, it listens on 127.0.0.1, TCP port 783. If you use another host or port
+for spamd, you must set the spamd_address option in the global part of the Exim
+configuration as follows (example):
+
+spamd_address = 192.168.99.45 387
+
+You do not need to set this option if you use the default. As of version 2.60, 
+spamd also supports communication over UNIX sockets. If you want to use these,
+supply spamd_address with an absolute file name instead of a address/port pair:
+
+spamd_address = /var/run/spamd_socket
+
+You can have multiple spamd servers to improve scalability. These can reside on
+other hardware reachable over the network. To specify multiple spamd servers,
+put multiple address/port pairs in the spamd_address option, separated with
+colons:
+
+spamd_address = 192.168.2.10 783 : \
+                192.168.2.11 783 : \
+                192.168.2.12 783
+
+Up to 32 spamd servers are supported. The servers are queried in a random
+fashion. When a server fails to respond to the connection attempt, all other
+servers are tried until one succeeds. If no server responds, the spam condition
+defers.
+
+Warning: It is not possible to use the UNIX socket connection method with
+multiple spamd servers.
+
+The spamd_address variable is expanded before use if it starts with a dollar
+sign. In this case, the expansion may return a string that is used as the list
+so that multiple spamd servers can be the result of an expansion.
+
+
+43.3 Calling SpamAssassin from an Exim ACL
+------------------------------------------
+
+Here is a simple example of the use of the spam condition in a DATA ACL:
+
+deny message = This message was classified as SPAM
+     spam = joe
+
+The right-hand side of the spam condition specifies a name. This is relevant if
+you have set up multiple SpamAssassin profiles. If you do not want to scan
+using a specific profile, but rather use the SpamAssassin system-wide default
+profile, you can scan for an unknown name, or simply use "nobody". However, you
+must put something on the right-hand side.
+
+The name allows you to use per-domain or per-user antispam profiles in
+principle, but this is not straightforward in practice, because a message may
+have multiple recipients, not necessarily all in the same domain. Because the 
+spam condition has to be called from a DATA ACL in order to be able to read the
+contents of the message, the variables $local_part and $domain are not set.
+
+The right-hand side of the spam condition is expanded before being used, so you
+can put lookups or conditions there. When the right-hand side evaluates to "0"
+or "false", no scanning is done and the condition fails immediately.
+
+Scanning with SpamAssassin uses a lot of resources. If you scan every message,
+large ones may cause significant performance degradation. As most spam messages
+are quite small, it is recommended that you do not scan the big ones. For
+example:
+
+deny message = This message was classified as SPAM
+     condition = ${if < {$message_size}{10K}}
+     spam = nobody
+
+The spam condition returns true if the threshold specified in the user's
+SpamAssassin profile has been matched or exceeded. If you want to use the spam
+condition for its side effects (see the variables below), you can make it
+always return "true" by appending ":true" to the username.
+
+When the spam condition is run, it sets up a number of expansion variables.
+These variables are saved with the received message, thus they are available
+for use at delivery time.
+
+$spam_score
+
+    The spam score of the message, for example "3.4" or "30.5". This is useful
+    for inclusion in log or reject messages.
+
+$spam_score_int
+
+    The spam score of the message, multiplied by ten, as an integer value. For
+    example "34" or "305". It may appear to disagree with $spam_score because
+    $spam_score is rounded and $spam_score_int is truncated. The integer value
+    is useful for numeric comparisons in conditions.
+
+$spam_bar
+
+    A string consisting of a number of "+" or "-" characters, representing the
+    integer part of the spam score value. A spam score of 4.4 would have a
+    $spam_bar value of "++++". This is useful for inclusion in warning headers,
+    since MUAs can match on such strings.
+
+$spam_report
+
+    A multiline text table, containing the full SpamAssassin report for the
+    message. Useful for inclusion in headers or reject messages.
+
+The spam condition caches its results unless expansion in spamd_address was
+used. If you call it again with the same user name, it does not scan again, but
+rather returns the same values as before.
+
+The spam condition returns DEFER if there is any error while running the
+message through SpamAssassin or if the expansion of spamd_address failed. If
+you want to treat DEFER as FAIL (to pass on to the next ACL statement block),
+append "/defer_ok" to the right-hand side of the spam condition, like this:
+
+deny message = This message was classified as SPAM
+     spam    = joe/defer_ok
+
+This causes messages to be accepted even if there is a problem with spamd.
+
+Here is a longer, commented example of the use of the spam condition:
+
+# put headers in all messages (no matter if spam or not)
+warn  spam = nobody:true
+      add_header = X-Spam-Score: $spam_score ($spam_bar)
+      add_header = X-Spam-Report: $spam_report
+
+# add second subject line with *SPAM* marker when message
+# is over threshold
+warn  spam = nobody
+      add_header = Subject: *SPAM* $h_Subject:
+
+# reject spam at high scores (> 12)
+deny  message = This message scored $spam_score spam points.
+      spam = nobody:true
+      condition = ${if >{$spam_score_int}{120}{1}{0}}
+
+
+43.4 Scanning MIME parts
+------------------------
+
+The acl_smtp_mime global option specifies an ACL that is called once for each
+MIME part of an SMTP message, including multipart types, in the sequence of
+their position in the message. Similarly, the acl_not_smtp_mime option
+specifies an ACL that is used for the MIME parts of non-SMTP messages. These
+options may both refer to the same ACL if you want the same processing in both
+cases.
+
+These ACLs are called (possibly many times) just before the acl_smtp_data ACL
+in the case of an SMTP message, or just before the acl_not_smtp ACL in the case
+of a non-SMTP message. However, a MIME ACL is called only if the message
+contains a Content-Type: header line. When a call to a MIME ACL does not yield
+"accept", ACL processing is aborted and the appropriate result code is sent to
+the client. In the case of an SMTP message, the acl_smtp_data ACL is not called
+when this happens.
+
+You cannot use the malware or spam conditions in a MIME ACL; these can only be
+used in the DATA or non-SMTP ACLs. However, you can use the regex condition to
+match against the raw MIME part. You can also use the mime_regex condition to
+match against the decoded MIME part (see section 43.5).
+
+At the start of a MIME ACL, a number of variables are set from the header
+information for the relevant MIME part. These are described below. The contents
+of the MIME part are not by default decoded into a disk file except for MIME
+parts whose content-type is "message/rfc822". If you want to decode a MIME part
+into a disk file, you can use the decode condition. The general syntax is:
+
+decode = [/<path>/]<filename>
+
+The right hand side is expanded before use. After expansion, the value can be:
+
+ 1. "0" or "false", in which case no decoding is done.
+
+ 2. The string "default". In that case, the file is put in the temporary
+    "default" directory <spool_directory>/scan/<message_id>/ with a sequential
+    file name consisting of the message id and a sequence number. The full path
+    and name is available in $mime_decoded_filename after decoding.
+
+ 3. A full path name starting with a slash. If the full name is an existing
+    directory, it is used as a replacement for the default directory. The
+    filename is then sequentially assigned. If the path does not exist, it is
+    used as the full path and file name.
+
+ 4. If the string does not start with a slash, it is used as the filename, and
+    the default path is then used.
+
+The decode condition normally succeeds. It is only false for syntax errors or
+unusual circumstances such as memory shortages. You can easily decode a file
+with its original, proposed filename using
+
+decode = $mime_filename
+
+However, you should keep in mind that $mime_filename might contain anything. If
+you place files outside of the default path, they are not automatically
+unlinked.
+
+For RFC822 attachments (these are messages attached to messages, with a
+content-type of "message/rfc822"), the ACL is called again in the same manner
+as for the primary message, only that the $mime_is_rfc822 expansion variable is
+set (see below). Attached messages are always decoded to disk before being
+checked, and the files are unlinked once the check is done.
+
+The MIME ACL supports the regex and mime_regex conditions. These can be used to
+match regular expressions against raw and decoded MIME parts, respectively.
+They are described in section 43.5.
+
+The following list describes all expansion variables that are available in the
+MIME ACL:
+
+$mime_boundary
+
+    If the current part is a multipart (see $mime_is_multipart) below, it
+    should have a boundary string, which is stored in this variable. If the
+    current part has no boundary parameter in the Content-Type: header, this
+    variable contains the empty string.
+
+$mime_charset
+
+    This variable contains the character set identifier, if one was found in
+    the Content-Type: header. Examples for charset identifiers are:
+
+    us-ascii
+    gb2312 (Chinese)
+    iso-8859-1
+
+    Please note that this value is not normalized, so you should do matches
+    case-insensitively.
+
+$mime_content_description
+
+    This variable contains the normalized content of the Content-Description:
+    header. It can contain a human-readable description of the parts content.
+    Some implementations repeat the filename for attachments here, but they are
+    usually only used for display purposes.
+
+$mime_content_disposition
+
+    This variable contains the normalized content of the Content-Disposition:
+    header. You can expect strings like "attachment" or "inline" here.
+
+$mime_content_id
+
+    This variable contains the normalized content of the Content-ID: header.
+    This is a unique ID that can be used to reference a part from another part.
+
+$mime_content_size
+
+    This variable is set only after the decode modifier (see above) has been
+    successfully run. It contains the size of the decoded part in kilobytes.
+    The size is always rounded up to full kilobytes, so only a completely empty
+    part has a $mime_content_size of zero.
+
+$mime_content_transfer_encoding
+
+    This variable contains the normalized content of the 
+    Content-transfer-encoding: header. This is a symbolic name for an encoding
+    type. Typical values are "base64" and "quoted-printable".
+
+$mime_content_type
+
+    If the MIME part has a Content-Type: header, this variable contains its
+    value, lowercased, and without any options (like "name" or "charset"). Here
+    are some examples of popular MIME types, as they may appear in this
+    variable:
+
+    text/plain
+    text/html
+    application/octet-stream
+    image/jpeg
+    audio/midi
+
+    If the MIME part has no Content-Type: header, this variable contains the
+    empty string.
+
+$mime_decoded_filename
+
+    This variable is set only after the decode modifier (see above) has been
+    successfully run. It contains the full path and file name of the file
+    containing the decoded data.
+
+$mime_filename
+
+    This is perhaps the most important of the MIME variables. It contains a
+    proposed filename for an attachment, if one was found in either the 
+    Content-Type: or Content-Disposition: headers. The filename will be RFC2047
+    decoded, but no additional sanity checks are done. If no filename was
+    found, this variable contains the empty string.
+
+$mime_is_coverletter
+
+    This variable attempts to differentiate the "cover letter" of an e-mail
+    from attached data. It can be used to clamp down on flashy or unnecessarily
+    encoded content in the cover letter, while not restricting attachments at
+    all.
+
+    The variable contains 1 (true) for a MIME part believed to be part of the
+    cover letter, and 0 (false) for an attachment. At present, the algorithm is
+    as follows:
+
+     1. The outermost MIME part of a message is always a cover letter.
+
+     2. If a multipart/alternative or multipart/related MIME part is a cover
+        letter, so are all MIME subparts within that multipart.
+
+     3. If any other multipart is a cover letter, the first subpart is a cover
+        letter, and the rest are attachments.
+
+     4. All parts contained within an attachment multipart are attachments.
+
+    As an example, the following will ban "HTML mail" (including that sent with
+    alternative plain text), while allowing HTML files to be attached. HTML
+    coverletter mail attached to non-HMTL coverletter mail will also be
+    allowed:
+
+    deny message = HTML mail is not accepted here
+    !condition = $mime_is_rfc822
+    condition = $mime_is_coverletter
+    condition = ${if eq{$mime_content_type}{text/html}{1}{0}}
+
+$mime_is_multipart
+
+    This variable has the value 1 (true) when the current part has the main
+    type "multipart", for example "multipart/alternative" or "multipart/mixed".
+    Since multipart entities only serve as containers for other parts, you may
+    not want to carry out specific actions on them.
+
+$mime_is_rfc822
+
+    This variable has the value 1 (true) if the current part is not a part of
+    the checked message itself, but part of an attached message. Attached
+    message decoding is fully recursive.
+
+$mime_part_count
+
+    This variable is a counter that is raised for each processed MIME part. It
+    starts at zero for the very first part (which is usually a multipart). The
+    counter is per-message, so it is reset when processing RFC822 attachments
+    (see $mime_is_rfc822). The counter stays set after acl_smtp_mime is
+    complete, so you can use it in the DATA ACL to determine the number of MIME
+    parts of a message. For non-MIME messages, this variable contains the value
+    -1.
+
+
+43.5 Scanning with regular expressions
+--------------------------------------
+
+You can specify your own custom regular expression matches on the full body of
+the message, or on individual MIME parts.
+
+The regex condition takes one or more regular expressions as arguments and
+matches them against the full message (when called in the DATA ACL) or a raw
+MIME part (when called in the MIME ACL). The regex condition matches linewise,
+with a maximum line length of 32K characters. That means you cannot have
+multiline matches with the regex condition.
+
+The mime_regex condition can be called only in the MIME ACL. It matches up to
+32K of decoded content (the whole content at once, not linewise). If the part
+has not been decoded with the decode modifier earlier in the ACL, it is decoded
+automatically when mime_regex is executed (using default path and filename
+values). If the decoded data is larger than 32K, only the first 32K characters
+are checked.
+
+The regular expressions are passed as a colon-separated list. To include a
+literal colon, you must double it. Since the whole right-hand side string is
+expanded before being used, you must also escape dollar signs and backslashes
+with more backslashes, or use the "\N" facility to disable expansion. Here is a
+simple example that contains two regular expressions:
+
+deny message = contains blacklisted regex ($regex_match_string)
+     regex = [Mm]ortgage : URGENT BUSINESS PROPOSAL
+
+The conditions returns true if any one of the regular expressions matches. The
+$regex_match_string expansion variable is then set up and contains the matching
+regular expression.
+
+Warning: With large messages, these conditions can be fairly CPU-intensive.
+
+
+43.6 The demime condition
+-------------------------
+
+The demime ACL condition provides MIME unpacking, sanity checking and file
+extension blocking. It is usable only in the DATA and non-SMTP ACLs. The demime
+condition uses a simpler interface to MIME decoding than the MIME ACL
+functionality, but provides no additional facilities. Please note that this
+condition is deprecated and kept only for backward compatibility. You must set
+the WITH_OLD_DEMIME option in Local/Makefile at build time to be able to use
+the demime condition.
+
+The demime condition unpacks MIME containers in the message. It detects errors
+in MIME containers and can match file extensions found in the message against a
+list. Using this facility produces files containing the unpacked MIME parts of
+the message in the temporary scan directory. If you do antivirus scanning, it
+is recommended that you use the demime condition before the antivirus (malware)
+condition.
+
+On the right-hand side of the demime condition you can pass a colon-separated
+list of file extensions that it should match against. For example:
+
+deny message = Found blacklisted file attachment
+     demime  = vbs:com:bat:pif:prf:lnk
+
+If one of the file extensions is found, the condition is true, otherwise it is
+false. If there is a temporary error while demimeing (for example, "disk
+full"), the condition defers, and the message is temporarily rejected (unless
+the condition is on a warn verb).
+
+The right-hand side is expanded before being treated as a list, so you can have
+conditions and lookups there. If it expands to an empty string, "false", or
+zero ("0"), no demimeing is done and the condition is false.
+
+The demime condition set the following variables:
+
+$demime_errorlevel
+
+    When an error is detected in a MIME container, this variable contains the
+    severity of the error, as an integer number. The higher the value, the more
+    severe the error (the current maximum value is 3). If this variable is
+    unset or zero, no error occurred.
+
+$demime_reason
+
+    When $demime_errorlevel is greater than zero, this variable contains a
+    human-readable text string describing the MIME error that occurred.
+
+$found_extension
+
+    When the demime condition is true, this variable contains the file
+    extension it found.
+
+Both $demime_errorlevel and $demime_reason are set by the first call of the 
+demime condition, and are not changed on subsequent calls.
+
+If you do not want to check for file extensions, but rather use the demime
+condition for unpacking or error checking purposes, pass "*" as the right-hand
+side value. Here is a more elaborate example of how to use this facility:
+
+# Reject messages with serious MIME container errors
+deny  message = Found MIME error ($demime_reason).
+      demime = *
+      condition = ${if >{$demime_errorlevel}{2}{1}{0}}
+
+# Reject known virus spreading file extensions.
+# Accepting these is pretty much braindead.
+deny  message = contains $found_extension file (blacklisted).
+      demime  = com:vbs:bat:pif:scr
+
+# Freeze .exe and .doc files. Postmaster can
+# examine them and eventually thaw them.
+deny  log_message = Another $found_extension file.
+      demime = exe:doc
+      control = freeze
+
+
+
+===============================================================================
+44. ADDING A LOCAL SCAN FUNCTION TO EXIM
+
+In these days of email worms, viruses, and ever-increasing spam, some sites
+want to apply a lot of checking to messages before accepting them.
+
+The content scanning extension (chapter 43) has facilities for passing messages
+to external virus and spam scanning software. You can also do a certain amount
+in Exim itself through string expansions and the condition condition in the ACL
+that runs after the SMTP DATA command or the ACL for non-SMTP messages (see
+chapter 42), but this has its limitations.
+
+To allow for further customization to a site's own requirements, there is the
+possibility of linking Exim with a private message scanning function, written
+in C. If you want to run code that is written in something other than C, you
+can of course use a little C stub to call it.
+
+The local scan function is run once for every incoming message, at the point
+when Exim is just about to accept the message. It can therefore be used to
+control non-SMTP messages from local processes as well as messages arriving via
+SMTP.
+
+Exim applies a timeout to calls of the local scan function, and there is an
+option called local_scan_timeout for setting it. The default is 5 minutes. Zero
+means "no timeout". Exim also sets up signal handlers for SIGSEGV, SIGILL,
+SIGFPE, and SIGBUS before calling the local scan function, so that the most
+common types of crash are caught. If the timeout is exceeded or one of those
+signals is caught, the incoming message is rejected with a temporary error if
+it is an SMTP message. For a non-SMTP message, the message is dropped and Exim
+ends with a non-zero code. The incident is logged on the main and reject logs.
+
+
+44.1 Building Exim to use a local scan function
+-----------------------------------------------
+
+To make use of the local scan function feature, you must tell Exim where your
+function is before building Exim, by setting LOCAL_SCAN_SOURCE in your Local/
+Makefile. A recommended place to put it is in the Local directory, so you might
+set
+
+LOCAL_SCAN_SOURCE=Local/local_scan.c
+
+for example. The function must be called local_scan(). It is called by Exim
+after it has received a message, when the success return code is about to be
+sent. This is after all the ACLs have been run. The return code from your
+function controls whether the message is actually accepted or not. There is a
+commented template function (that just accepts the message) in the file _src/
+local_scan.c_.
+
+If you want to make use of Exim's run time configuration file to set options
+for your local_scan() function, you must also set
+
+LOCAL_SCAN_HAS_OPTIONS=yes
+
+in Local/Makefile (see section 44.3 below).
+
+
+44.2 API for local_scan()
+-------------------------
+
+You must include this line near the start of your code:
+
+#include "local_scan.h"
+
+This header file defines a number of variables and other values, and the
+prototype for the function itself. Exim is coded to use unsigned char values
+almost exclusively, and one of the things this header defines is a shorthand
+for "unsigned char" called "uschar". It also contains the following macro
+definitions, to simplify casting character strings and pointers to character
+strings:
+
+#define CS   (char *)
+#define CCS  (const char *)
+#define CSS  (char **)
+#define US   (unsigned char *)
+#define CUS  (const unsigned char *)
+#define USS  (unsigned char **)
+
+The function prototype for local_scan() is:
+
+extern int local_scan(int fd, uschar **return_text);
+
+The arguments are as follows:
+
+  * fd is a file descriptor for the file that contains the body of the message
+    (the -D file). The file is open for reading and writing, but updating it is
+    not recommended. Warning: You must not close this file descriptor.
+
+    The descriptor is positioned at character 19 of the file, which is the
+    first character of the body itself, because the first 19 characters are the
+    message id followed by "-D" and a newline. If you rewind the file, you
+    should use the macro SPOOL_DATA_START_OFFSET to reset to the start of the
+    data, just in case this changes in some future version.
+
+  * return_text is an address which you can use to return a pointer to a text
+    string at the end of the function. The value it points to on entry is NULL.
+
+The function must return an int value which is one of the following macros:
+
+"LOCAL_SCAN_ACCEPT"
+
+    The message is accepted. If you pass back a string of text, it is saved
+    with the message, and made available in the variable $local_scan_data. No
+    newlines are permitted (if there are any, they are turned into spaces) and
+    the maximum length of text is 1000 characters.
+
+"LOCAL_SCAN_ACCEPT_FREEZE"
+
+    This behaves as LOCAL_SCAN_ACCEPT, except that the accepted message is
+    queued without immediate delivery, and is frozen.
+
+"LOCAL_SCAN_ACCEPT_QUEUE"
+
+    This behaves as LOCAL_SCAN_ACCEPT, except that the accepted message is
+    queued without immediate delivery.
+
+"LOCAL_SCAN_REJECT"
+
+    The message is rejected; the returned text is used as an error message
+    which is passed back to the sender and which is also logged. Newlines are
+    permitted - they cause a multiline response for SMTP rejections, but are
+    converted to "\n" in log lines. If no message is given, "Administrative
+    prohibition" is used.
+
+"LOCAL_SCAN_TEMPREJECT"
+
+    The message is temporarily rejected; the returned text is used as an error
+    message as for LOCAL_SCAN_REJECT. If no message is given, "Temporary local
+    problem" is used.
+
+"LOCAL_SCAN_REJECT_NOLOGHDR"
+
+    This behaves as LOCAL_SCAN_REJECT, except that the header of the rejected
+    message is not written to the reject log. It has the effect of unsetting
+    the rejected_header log selector for just this rejection. If 
+    rejected_header is already unset (see the discussion of the log_selection
+    option in section 51.15), this code is the same as LOCAL_SCAN_REJECT.
+
+"LOCAL_SCAN_TEMPREJECT_NOLOGHDR"
+
+    This code is a variation of LOCAL_SCAN_TEMPREJECT in the same way that
+    LOCAL_SCAN_REJECT_NOLOGHDR is a variation of LOCAL_SCAN_REJECT.
+
+If the message is not being received by interactive SMTP, rejections are
+reported by writing to stderr or by sending an email, as configured by the -oe
+command line options.
+
+
+44.3 Configuration options for local_scan()
+-------------------------------------------
+
+It is possible to have option settings in the main configuration file that set
+values in static variables in the local_scan() module. If you want to do this,
+you must have the line
+
+LOCAL_SCAN_HAS_OPTIONS=yes
+
+in your Local/Makefile when you build Exim. (This line is in OS/
+Makefile-Default, commented out). Then, in the local_scan() source file, you
+must define static variables to hold the option values, and a table to define
+them.
+
+The table must be a vector called local_scan_options, of type "optionlist".
+Each entry is a triplet, consisting of a name, an option type, and a pointer to
+the variable that holds the value. The entries must appear in alphabetical
+order. Following local_scan_options you must also define a variable called 
+local_scan_options_count that contains the number of entries in the table. Here
+is a short example, showing two kinds of option:
+
+static int my_integer_option = 42;
+static uschar *my_string_option = US"a default string";
+
+optionlist local_scan_options[] = {
+  { "my_integer", opt_int,       &my_integer_option },
+  { "my_string",  opt_stringptr, &my_string_option }
+};
+
+int local_scan_options_count =
+  sizeof(local_scan_options)/sizeof(optionlist);
+
+The values of the variables can now be changed from Exim's runtime
+configuration file by including a local scan section as in this example:
+
+begin local_scan
+my_integer = 99
+my_string = some string of text...
+
+The available types of option data are as follows:
+
+opt_bool
+
+    This specifies a boolean (true/false) option. The address should point to a
+    variable of type "BOOL", which will be set to TRUE or FALSE, which are
+    macros that are defined as "1" and "0", respectively. If you want to detect
+    whether such a variable has been set at all, you can initialize it to
+    TRUE_UNSET. (BOOL variables are integers underneath, so can hold more than
+    two values.)
+
+opt_fixed
+
+    This specifies a fixed point number, such as is used for load averages. The
+    address should point to a variable of type "int". The value is stored
+    multiplied by 1000, so, for example, 1.4142 is truncated and stored as
+    1414.
+
+opt_int
+
+    This specifies an integer; the address should point to a variable of type
+    "int". The value may be specified in any of the integer formats accepted by
+    Exim.
+
+opt_mkint
+
+    This is the same as opt_int, except that when such a value is output in a 
+    -bP listing, if it is an exact number of kilobytes or megabytes, it is
+    printed with the suffix K or M.
+
+opt_octint
+
+    This also specifies an integer, but the value is always interpreted as an
+    octal integer, whether or not it starts with the digit zero, and it is
+    always output in octal.
+
+opt_stringptr
+
+    This specifies a string value; the address must be a pointer to a variable
+    that points to a string (for example, of type "uschar *").
+
+opt_time
+
+    This specifies a time interval value. The address must point to a variable
+    of type "int". The value that is placed there is a number of seconds.
+
+If the -bP command line option is followed by "local_scan", Exim prints out the
+values of all the local_scan() options.
+
+
+44.4 Available Exim variables
+-----------------------------
+
+The header local_scan.h gives you access to a number of C variables. These are
+the only ones that are guaranteed to be maintained from release to release.
+Note, however, that you can obtain the value of any Exim expansion variable,
+including $recipients, by calling expand_string(). The exported C variables are
+as follows:
+
+int body_linecount
+
+    This variable contains the number of lines in the message's body.
+
+int body_zerocount
+
+    This variable contains the number of binary zero bytes in the message's
+    body.
+
+unsigned int debug_selector
+
+    This variable is set to zero when no debugging is taking place. Otherwise,
+    it is a bitmap of debugging selectors. Two bits are identified for use in
+    local_scan(); they are defined as macros:
+
+      * The "D_v" bit is set when -v was present on the command line. This is a
+        testing option that is not privileged - any caller may set it. All the
+        other selector bits can be set only by admin users.
+
+      * The "D_local_scan" bit is provided for use by local_scan(); it is set
+        by the "+local_scan" debug selector. It is not included in the default
+        set of debugging bits.
+
+    Thus, to write to the debugging output only when "+local_scan" has been
+    selected, you should use code like this:
+
+    if ((debug_selector & D_local_scan) != 0)
+      debug_printf("xxx", ...);
+
+uschar *expand_string_message
+
+    After a failing call to expand_string() (returned value NULL), the variable
+    expand_string_message contains the error message, zero-terminated.
+
+header_line *header_list
+
+    A pointer to a chain of header lines. The header_line structure is
+    discussed below.
+
+header_line *header_last
+
+    A pointer to the last of the header lines.
+
+uschar *headers_charset
+
+    The value of the headers_charset configuration option.
+
+BOOL host_checking
+
+    This variable is TRUE during a host checking session that is initiated by
+    the -bh command line option.
+
+uschar *interface_address
+
+    The IP address of the interface that received the message, as a string.
+    This is NULL for locally submitted messages.
+
+int interface_port
+
+    The port on which this message was received. When testing with the -bh
+    command line option, the value of this variable is -1 unless a port has
+    been specified via the -oMi option.
+
+uschar *message_id
+
+    This variable contains Exim's message id for the incoming message (the
+    value of $message_exim_id) as a zero-terminated string.
+
+uschar *received_protocol
+
+    The name of the protocol by which the message was received.
+
+int recipients_count
+
+    The number of accepted recipients.
+
+recipient_item *recipients_list
+
+    The list of accepted recipients, held in a vector of length 
+    recipients_count. The recipient_item structure is discussed below. You can
+    add additional recipients by calling receive_add_recipient() (see below).
+    You can delete recipients by removing them from the vector and adjusting
+    the value in recipients_count. In particular, by setting recipients_count
+    to zero you remove all recipients. If you then return the value
+    "LOCAL_SCAN_ACCEPT", the message is accepted, but immediately blackholed.
+    To replace the recipients, you can set recipients_count to zero and then
+    call receive_add_recipient() as often as needed.
+
+uschar *sender_address
+
+    The envelope sender address. For bounce messages this is the empty string.
+
+uschar *sender_host_address
+
+    The IP address of the sending host, as a string. This is NULL for
+    locally-submitted messages.
+
+uschar *sender_host_authenticated
+
+    The name of the authentication mechanism that was used, or NULL if the
+    message was not received over an authenticated SMTP connection.
+
+uschar *sender_host_name
+
+    The name of the sending host, if known.
+
+int sender_host_port
+
+    The port on the sending host.
+
+BOOL smtp_input
+
+    This variable is TRUE for all SMTP input, including BSMTP.
+
+BOOL smtp_batched_input
+
+    This variable is TRUE for BSMTP input.
+
+int store_pool
+
+    The contents of this variable control which pool of memory is used for new
+    requests. See section 44.8 for details.
+
+
+44.5 Structure of header lines
+------------------------------
+
+The header_line structure contains the members listed below. You can add
+additional header lines by calling the header_add() function (see below). You
+can cause header lines to be ignored (deleted) by setting their type to *.
+
+struct header_line *next
+
+    A pointer to the next header line, or NULL for the last line.
+
+int type
+
+    A code identifying certain headers that Exim recognizes. The codes are
+    printing characters, and are documented in chapter 55 of this manual.
+    Notice in particular that any header line whose type is * is not
+    transmitted with the message. This flagging is used for header lines that
+    have been rewritten, or are to be removed (for example, Envelope-sender:
+    header lines.) Effectively, * means "deleted".
+
+int slen
+
+    The number of characters in the header line, including the terminating and
+    any internal newlines.
+
+uschar *text
+
+    A pointer to the text of the header. It always ends with a newline,
+    followed by a zero byte. Internal newlines are preserved.
+
+
+44.6 Structure of recipient items
+---------------------------------
+
+The recipient_item structure contains these members:
+
+uschar *address
+
+    This is a pointer to the recipient address as it was received.
+
+int pno
+
+    This is used in later Exim processing when top level addresses are created
+    by the one_time option. It is not relevant at the time local_scan() is run
+    and must always contain -1 at this stage.
+
+uschar *errors_to
+
+    If this value is not NULL, bounce messages caused by failing to deliver to
+    the recipient are sent to the address it contains. In other words, it
+    overrides the envelope sender for this one recipient. (Compare the 
+    errors_to generic router option.) If a local_scan() function sets an 
+    errors_to field to an unqualified address, Exim qualifies it using the
+    domain from qualify_recipient. When local_scan() is called, the errors_to
+    field is NULL for all recipients.
+
+
+44.7 Available Exim functions
+-----------------------------
+
+The header local_scan.h gives you access to a number of Exim functions. These
+are the only ones that are guaranteed to be maintained from release to release:
+
+pid_t child_open
+    (uschar **argv, uschar **envp, int newumask, int *infdptr, int *outfdptr,
+      BOOL make_leader)
+
+    This function creates a child process that runs the command specified by 
+    argv. The environment for the process is specified by envp, which can be
+    NULL if no environment variables are to be passed. A new umask is supplied
+    for the process in newumask.
+
+    Pipes to the standard input and output of the new process are set up and
+    returned to the caller via the infdptr and outfdptr arguments. The standard
+    error is cloned to the standard output. If there are any file descriptors
+    "in the way" in the new process, they are closed. If the final argument is
+    TRUE, the new process is made into a process group leader.
+
+    The function returns the pid of the new process, or -1 if things go wrong.
+
+int child_close(pid_t pid, int timeout)
+
+    This function waits for a child process to terminate, or for a timeout (in
+    seconds) to expire. A timeout value of zero means wait as long as it takes.
+    The return value is as follows:
+
+      * >= 0
+
+        The process terminated by a normal exit and the value is the process
+        ending status.
+
+      * < 0 and > -256
+
+        The process was terminated by a signal and the value is the negation of
+        the signal number.
+
+      * -256
+
+        The process timed out.
+
+      * -257
+
+        The was some other error in wait(); errno is still set.
+
+pid_t child_open_exim(int *fd)
+
+    This function provide you with a means of submitting a new message to Exim.
+    (Of course, you can also call /usr/sbin/sendmail yourself if you want, but
+    this packages it all up for you.) The function creates a pipe, forks a
+    subprocess that is running
+
+    exim -t -oem -oi -f <>
+
+    and returns to you (via the "int *" argument) a file descriptor for the
+    pipe that is connected to the standard input. The yield of the function is
+    the PID of the subprocess. You can then write a message to the file
+    descriptor, with recipients in To:, Cc:, and/or Bcc: header lines.
+
+    When you have finished, call child_close() to wait for the process to
+    finish and to collect its ending status. A timeout value of zero is usually
+    fine in this circumstance. Unless you have made a mistake with the
+    recipient addresses, you should get a return code of zero.
+
+pid_t child_open_exim2(int *fd, uschar *sender, uschar *sender_authentication)
+
+    This function is a more sophisticated version of child_open(). The command
+    that it runs is:
+
+    exim -t -oem -oi -f sender -oMas sender_authentication
+
+    The third argument may be NULL, in which case the -oMas option is omitted.
+
+void debug_printf(char *, ...)
+
+    This is Exim's debugging function, with arguments as for (printf(). The
+    output is written to the standard error stream. If no debugging is
+    selected, calls to debug_printf() have no effect. Normally, you should make
+    calls conditional on the "local_scan" debug selector by coding like this:
+
+    if ((debug_selector & D_local_scan) != 0)
+      debug_printf("xxx", ...);
+
+uschar *expand_string(uschar *string)
+
+    This is an interface to Exim's string expansion code. The return value is
+    the expanded string, or NULL if there was an expansion failure. The C
+    variable expand_string_message contains an error message after an expansion
+    failure. If expansion does not change the string, the return value is the
+    pointer to the input string. Otherwise, the return value points to a new
+    block of memory that was obtained by a call to store_get(). See section
+    44.8 below for a discussion of memory handling.
+
+void header_add(int type, char *format, ...)
+
+    This function allows you to an add additional header line at the end of the
+    existing ones. The first argument is the type, and should normally be a
+    space character. The second argument is a format string and any number of
+    substitution arguments as for sprintf(). You may include internal newlines
+    if you want, and you must ensure that the string ends with a newline.
+
+void header_add_at_position
+    (BOOL after, uschar *name, BOOL topnot, int type, char *format,   ...)
+
+    This function adds a new header line at a specified point in the header
+    chain. The header itself is specified as for header_add().
+
+    If name is NULL, the new header is added at the end of the chain if after
+    is true, or at the start if after is false. If name is not NULL, the header
+    lines are searched for the first non-deleted header that matches the name.
+    If one is found, the new header is added before it if after is false. If 
+    after is true, the new header is added after the found header and any
+    adjacent subsequent ones with the same name (even if marked "deleted"). If
+    no matching non-deleted header is found, the topnot option controls where
+    the header is added. If it is true, addition is at the top; otherwise at
+    the bottom. Thus, to add a header after all the Received: headers, or at
+    the top if there are no Received: headers, you could use
+
+    header_add_at_position(TRUE, US"Received", TRUE,
+      ' ', "X-xxx: ...");
+
+    Normally, there is always at least one non-deleted Received: header, but
+    there may not be if received_header_text expands to an empty string.
+
+void header_remove(int occurrence, uschar *name)
+
+    This function removes header lines. If occurrence is zero or negative, all
+    occurrences of the header are removed. If occurrence is greater than zero,
+    that particular instance of the header is removed. If no header(s) can be
+    found that match the specification, the function does nothing.
+
+BOOL header_testname(header_line *hdr, uschar *name, int length, BOOL notdel)
+
+    This function tests whether the given header has the given name. It is not
+    just a string comparison, because white space is permitted between the name
+    and the colon. If the notdel argument is true, a false return is forced for
+    all "deleted" headers; otherwise they are not treated specially. For
+    example:
+
+    if (header_testname(h, US"X-Spam", 6, TRUE)) ...
+
+uschar *lss_b64encode(uschar *cleartext, int length)
+
+    This function base64-encodes a string, which is passed by address and
+    length. The text may contain bytes of any value, including zero. The result
+    is passed back in dynamic memory that is obtained by calling store_get().
+    It is zero-terminated.
+
+int lss_b64decode(uschar *codetext, uschar **cleartext)
+
+    This function decodes a base64-encoded string. Its arguments are a
+    zero-terminated base64-encoded string and the address of a variable that is
+    set to point to the result, which is in dynamic memory. The length of the
+    decoded string is the yield of the function. If the input is invalid base64
+    data, the yield is -1. A zero byte is added to the end of the output string
+    to make it easy to interpret as a C string (assuming it contains no zeros
+    of its own). The added zero byte is not included in the returned count.
+
+int lss_match_domain(uschar *domain, uschar *list)
+
+    This function checks for a match in a domain list. Domains are always
+    matched caselessly. The return value is one of the following:
+
+    OK      match succeeded
+    FAIL    match failed
+    DEFER   match deferred
+
+    DEFER is usually caused by some kind of lookup defer, such as the inability
+    to contact a database.
+
+int lss_match_local_part(uschar *localpart, uschar *list, BOOL caseless)
+
+    This function checks for a match in a local part list. The third argument
+    controls case-sensitivity. The return values are as for lss_match_domain().
+
+int lss_match_address(uschar *address, uschar *list, BOOL caseless)
+
+    This function checks for a match in an address list. The third argument
+    controls the case-sensitivity of the local part match. The domain is always
+    matched caselessly. The return values are as for lss_match_domain().
+
+int lss_match_host(uschar *host_name, uschar *host_address, uschar *list)
+
+    This function checks for a match in a host list. The most common usage is
+    expected to be
+
+    lss_match_host(sender_host_name, sender_host_address, ...)
+
+    An empty address field matches an empty item in the host list. If the host
+    name is NULL, the name corresponding to $sender_host_address is
+    automatically looked up if a host name is required to match an item in the
+    list. The return values are as for lss_match_domain(), but in addition, 
+    lss_match_host() returns ERROR in the case when it had to look up a host
+    name, but the lookup failed.
+
+void log_write(unsigned int selector, int which, char *format, ...)
+
+    This function writes to Exim's log files. The first argument should be zero
+    (it is concerned with log_selector). The second argument can be "LOG_MAIN"
+    or "LOG_REJECT" or "LOG_PANIC" or the inclusive "or" of any combination of
+    them. It specifies to which log or logs the message is written. The
+    remaining arguments are a format and relevant insertion arguments. The
+    string should not contain any newlines, not even at the end.
+
+void receive_add_recipient(uschar *address, int pno)
+
+    This function adds an additional recipient to the message. The first
+    argument is the recipient address. If it is unqualified (has no domain), it
+    is qualified with the qualify_recipient domain. The second argument must
+    always be -1.
+
+    This function does not allow you to specify a private errors_to address (as
+    described with the structure of recipient_item above), because it pre-dates
+    the addition of that field to the structure. However, it is easy to add
+    such a value afterwards. For example:
+
+     receive_add_recipient(US"monitor@mydom.example", -1);
+     recipients_list[recipients_count-1].errors_to =
+       US"postmaster@mydom.example";
+
+BOOL receive_remove_recipient(uschar *recipient)
+
+    This is a convenience function to remove a named recipient from the list of
+    recipients. It returns true if a recipient was removed, and false if no
+    matching recipient could be found. The argument must be a complete email
+    address.
+
+uschar rfc2047_decode
+    (uschar *string, BOOL lencheck, uschar *target, int zeroval, int *lenptr,
+      uschar **error)
+
+    This function decodes strings that are encoded according to RFC 2047.
+    Typically these are the contents of header lines. First, each "encoded
+    word" is decoded from the Q or B encoding into a byte-string. Then, if
+    provided with the name of a charset encoding, and if the iconv() function
+    is available, an attempt is made to translate the result to the named
+    character set. If this fails, the binary string is returned with an error
+    message.
+
+    The first argument is the string to be decoded. If lencheck is TRUE, the
+    maximum MIME word length is enforced. The third argument is the target
+    encoding, or NULL if no translation is wanted.
+
+    If a binary zero is encountered in the decoded string, it is replaced by
+    the contents of the zeroval argument. For use with Exim headers, the value
+    must not be 0 because header lines are handled as zero-terminated strings.
+
+    The function returns the result of processing the string, zero-terminated;
+    if lenptr is not NULL, the length of the result is set in the variable to
+    which it points. When zeroval is 0, lenptr should not be NULL.
+
+    If an error is encountered, the function returns NULL and uses the error
+    argument to return an error message. The variable pointed to by error is
+    set to NULL if there is no error; it may be set non-NULL even when the
+    function returns a non-NULL value if decoding was successful, but there was
+    a problem with translation.
+
+int smtp_fflush(void)
+
+    This function is used in conjunction with smtp_printf(), as described
+    below.
+
+void smtp_printf(char *, ...)
+
+    The arguments of this function are like printf(); it writes to the SMTP
+    output stream. You should use this function only when there is an SMTP
+    output stream, that is, when the incoming message is being received via
+    interactive SMTP. This is the case when smtp_input is TRUE and 
+    smtp_batched_input is FALSE. If you want to test for an incoming message
+    from another host (as opposed to a local process that used the -bs command
+    line option), you can test the value of sender_host_address, which is
+    non-NULL when a remote host is involved.
+
+    If an SMTP TLS connection is established, smtp_printf() uses the TLS output
+    function, so it can be used for all forms of SMTP connection.
+
+    Strings that are written by smtp_printf() from within local_scan() must
+    start with an appropriate response code: 550 if you are going to return
+    LOCAL_SCAN_REJECT, 451 if you are going to return LOCAL_SCAN_TEMPREJECT,
+    and 250 otherwise. Because you are writing the initial lines of a
+    multi-line response, the code must be followed by a hyphen to indicate that
+    the line is not the final response line. You must also ensure that the
+    lines you write terminate with CRLF. For example:
+
+    smtp_printf("550-this is some extra info\r\n");
+    return LOCAL_SCAN_REJECT;
+
+    Note that you can also create multi-line responses by including newlines in
+    the data returned via the return_text argument. The added value of using 
+    smtp_printf() is that, for instance, you could introduce delays between
+    multiple output lines.
+
+    The smtp_printf() function does not return any error indication, because it
+    does not automatically flush pending output, and therefore does not test
+    the state of the stream. (In the main code of Exim, flushing and error
+    detection is done when Exim is ready for the next SMTP input command.) If
+    you want to flush the output and check for an error (for example, the
+    dropping of a TCP/IP connection), you can call smtp_fflush(), which has no
+    arguments. It flushes the output stream, and returns a non-zero value if
+    there is an error.
+
+void *store_get(int)
+
+    This function accesses Exim's internal store (memory) manager. It gets a
+    new chunk of memory whose size is given by the argument. Exim bombs out if
+    it ever runs out of memory. See the next section for a discussion of memory
+    handling.
+
+void *store_get_perm(int)
+
+    This function is like store_get(), but it always gets memory from the
+    permanent pool. See the next section for a discussion of memory handling.
+
+uschar *string_copy(uschar *string)
+
+    See below.
+
+uschar *string_copyn(uschar *string, int length)
+
+    See below.
+
+uschar *string_sprintf(char *format, ...)
+
+    These three functions create strings using Exim's dynamic memory
+    facilities. The first makes a copy of an entire string. The second copies
+    up to a maximum number of characters, indicated by the second argument. The
+    third uses a format and insertion arguments to create a new string. In each
+    case, the result is a pointer to a new string in the current memory pool.
+    See the next section for more discussion.
+
+
+44.8 More about Exim's memory handling
+--------------------------------------
+
+No function is provided for freeing memory, because that is never needed. The
+dynamic memory that Exim uses when receiving a message is automatically
+recycled if another message is received by the same process (this applies only
+to incoming SMTP connections - other input methods can supply only one message
+at a time). After receiving the last message, a reception process terminates.
+
+Because it is recycled, the normal dynamic memory cannot be used for holding
+data that must be preserved over a number of incoming messages on the same SMTP
+connection. However, Exim in fact uses two pools of dynamic memory; the second
+one is not recycled, and can be used for this purpose.
+
+If you want to allocate memory that remains available for subsequent messages
+in the same SMTP connection, you should set
+
+store_pool = POOL_PERM
+
+before calling the function that does the allocation. There is no need to
+restore the value if you do not need to; however, if you do want to revert to
+the normal pool, you can either restore the previous value of store_pool or set
+it explicitly to POOL_MAIN.
+
+The pool setting applies to all functions that get dynamic memory, including 
+expand_string(), store_get(), and the string_xxx() functions. There is also a
+convenience function called store_get_perm() that gets a block of memory from
+the permanent pool while preserving the value of store_pool.
+
+
+
+===============================================================================
+45. SYSTEM-WIDE MESSAGE FILTERING
+
+The previous chapters (on ACLs and the local scan function) describe checks
+that can be applied to messages before they are accepted by a host. There is
+also a mechanism for checking messages once they have been received, but before
+they are delivered. This is called the system filter.
+
+The system filter operates in a similar manner to users' filter files, but it
+is run just once per message (however many recipients the message has). It
+should not normally be used as a substitute for routing, because deliver
+commands in a system router provide new envelope recipient addresses. The
+system filter must be an Exim filter. It cannot be a Sieve filter.
+
+The system filter is run at the start of a delivery attempt, before any routing
+is done. If a message fails to be completely delivered at the first attempt,
+the system filter is run again at the start of every retry. If you want your
+filter to do something only once per message, you can make use of the 
+first_delivery condition in an if command in the filter to prevent it happening
+on retries.
+
+Warning: Because the system filter runs just once, variables that are specific
+to individual recipient addresses, such as $local_part and $domain, are not
+set, and the "personal" condition is not meaningful. If you want to run a
+centrally-specified filter for each recipient address independently, you can do
+so by setting up a suitable redirect router, as described in section 45.8
+below.
+
+
+45.1 Specifying a system filter
+-------------------------------
+
+The name of the file that contains the system filter must be specified by
+setting system_filter. If you want the filter to run under a uid and gid other
+than root, you must also set system_filter_user and system_filter_group as
+appropriate. For example:
+
+system_filter = /etc/mail/exim.filter
+system_filter_user = exim
+
+If a system filter generates any deliveries directly to files or pipes (via the
+save or pipe commands), transports to handle these deliveries must be specified
+by setting system_filter_file_transport and system_filter_pipe_transport,
+respectively. Similarly, system_filter_reply_transport must be set to handle
+any messages generated by the reply command.
+
+
+45.2 Testing a system filter
+----------------------------
+
+You can run simple tests of a system filter in the same way as for a user
+filter, but you should use -bF rather than -bf, so that features that are
+permitted only in system filters are recognized.
+
+If you want to test the combined effect of a system filter and a user filter,
+you can use both -bF and -bf on the same command line.
+
+
+45.3 Contents of a system filter
+--------------------------------
+
+The language used to specify system filters is the same as for users' filter
+files. It is described in the separate end-user document Exim's interface to
+mail filtering. However, there are some additional features that are available
+only in system filters; these are described in subsequent sections. If they are
+encountered in a user's filter file or when testing with -bf, they cause
+errors.
+
+There are two special conditions which, though available in users' filter
+files, are designed for use in system filters. The condition first_delivery is
+true only for the first attempt at delivering a message, and manually_thawed is
+true only if the message has been frozen, and subsequently thawed by an admin
+user. An explicit forced delivery counts as a manual thaw, but thawing as a
+result of the auto_thaw setting does not.
+
+Warning: If a system filter uses the first_delivery condition to specify an
+"unseen" (non-significant) delivery, and that delivery does not succeed, it
+will not be tried again. If you want Exim to retry an unseen delivery until it
+succeeds, you should arrange to set it up every time the filter runs.
+
+When a system filter finishes running, the values of the variables $n0 - $n9
+are copied into $sn0 - $sn9 and are thereby made available to users' filter
+files. Thus a system filter can, for example, set up "scores" to which users'
+filter files can refer.
+
+
+45.4 Additional variable for system filters
+-------------------------------------------
+
+The expansion variable $recipients, containing a list of all the recipients of
+the message (separated by commas and white space), is available in system
+filters. It is not available in users' filters for privacy reasons.
+
+
+45.5 Defer, freeze, and fail commands for system filters
+--------------------------------------------------------
+
+There are three extra commands (defer, freeze and fail) which are always
+available in system filters, but are not normally enabled in users' filters.
+(See the allow_defer, allow_freeze and allow_fail options for the redirect
+router.) These commands can optionally be followed by the word text and a
+string containing an error message, for example:
+
+fail text "this message looks like spam to me"
+
+The keyword text is optional if the next character is a double quote.
+
+The defer command defers delivery of the original recipients of the message.
+The fail command causes all the original recipients to be failed, and a bounce
+message to be created. The freeze command suspends all delivery attempts for
+the original recipients. In all cases, any new deliveries that are specified by
+the filter are attempted as normal after the filter has run.
+
+The freeze command is ignored if the message has been manually unfrozen and not
+manually frozen since. This means that automatic freezing by a system filter
+can be used as a way of checking out suspicious messages. If a message is found
+to be all right, manually unfreezing it allows it to be delivered.
+
+The text given with a fail command is used as part of the bounce message as
+well as being written to the log. If the message is quite long, this can fill
+up a lot of log space when such failures are common. To reduce the size of the
+log message, Exim interprets the text in a special way if it starts with the
+two characters "<<" and contains ">>" later. The text between these two strings
+is written to the log, and the rest of the text is used in the bounce message.
+For example:
+
+fail "<<filter test 1>>Your message is rejected \
+     because it contains attachments that we are \
+     not prepared to receive."
+
+Take great care with the fail command when basing the decision to fail on the
+contents of the message, because the bounce message will of course include the
+contents of the original message and will therefore trigger the fail command
+again (causing a mail loop) unless steps are taken to prevent this. Testing the
+error_message condition is one way to prevent this. You could use, for example
+
+if $message_body contains "this is spam" and not error_message
+then fail text "spam is not wanted here" endif
+
+though of course that might let through unwanted bounce messages. The
+alternative is clever checking of the body and/or headers to detect bounces
+generated by the filter.
+
+The interpretation of a system filter file ceases after a defer, freeze, or 
+fail command is obeyed. However, any deliveries that were set up earlier in the
+filter file are honoured, so you can use a sequence such as
+
+mail ...
+freeze
+
+to send a specified message when the system filter is freezing (or deferring or
+failing) a message. The normal deliveries for the message do not, of course,
+take place.
+
+
+45.6 Adding and removing headers in a system filter
+---------------------------------------------------
+
+Two filter commands that are available only in system filters are:
+
+headers add <string>
+headers remove <string>
+
+The argument for the headers add is a string that is expanded and then added to
+the end of the message's headers. It is the responsibility of the filter
+maintainer to make sure it conforms to RFC 2822 syntax. Leading white space is
+ignored, and if the string is otherwise empty, or if the expansion is forced to
+fail, the command has no effect.
+
+You can use "\n" within the string, followed by white space, to specify
+continued header lines. More than one header may be added in one command by
+including "\n" within the string without any following white space. For
+example:
+
+headers add "X-header-1: ....\n  \
+             continuation of X-header-1 ...\n\
+             X-header-2: ...."
+
+Note that the header line continuation white space after the first newline must
+be placed before the backslash that continues the input string, because white
+space after input continuations is ignored.
+
+The argument for headers remove is a colon-separated list of header names. This
+command applies only to those headers that are stored with the message; those
+that are added at delivery time (such as Envelope-To: and Return-Path:) cannot
+be removed by this means. If there is more than one header with the same name,
+they are all removed.
+
+The headers command in a system filter makes an immediate change to the set of
+header lines that was received with the message (with possible additions from
+ACL processing). Subsequent commands in the system filter operate on the
+modified set, which also forms the basis for subsequent message delivery.
+Unless further modified during routing or transporting, this set of headers is
+used for all recipients of the message.
+
+During routing and transporting, the variables that refer to the contents of
+header lines refer only to those lines that are in this set. Thus, header lines
+that are added by a system filter are visible to users' filter files and to all
+routers and transports. This contrasts with the manipulation of header lines by
+routers and transports, which is not immediate, but which instead is saved up
+until the message is actually being written (see section 46.17).
+
+If the message is not delivered at the first attempt, header lines that were
+added by the system filter are stored with the message, and so are still
+present at the next delivery attempt. Header lines that were removed are still
+present, but marked "deleted" so that they are not transported with the
+message. For this reason, it is usual to make the headers command conditional
+on first_delivery so that the set of header lines is not modified more than
+once.
+
+Because header modification in a system filter acts immediately, you have to
+use an indirect approach if you want to modify the contents of a header line.
+For example:
+
+headers add "Old-Subject: $h_subject:"
+headers remove "Subject"
+headers add "Subject: new subject (was: $h_old-subject:)"
+headers remove "Old-Subject"
+
+
+45.7 Setting an errors address in a system filter
+-------------------------------------------------
+
+In a system filter, if a deliver command is followed by
+
+errors_to <some address>
+
+in order to change the envelope sender (and hence the error reporting) for that
+delivery, any address may be specified. (In a user filter, only the current
+user's address can be set.) For example, if some mail is being monitored, you
+might use
+
+unseen deliver monitor@spying.example errors_to root@local.example
+
+to take a copy which would not be sent back to the normal error reporting
+address if its delivery failed.
+
+
+45.8 Per-address filtering
+--------------------------
+
+In contrast to the system filter, which is run just once per message for each
+delivery attempt, it is also possible to set up a system-wide filtering
+operation that runs once for each recipient address. In this case, variables
+such as $local_part and $domain can be used, and indeed, the choice of filter
+file could be made dependent on them. This is an example of a router which
+implements such a filter:
+
+central_filter:
+  check_local_user
+  driver = redirect
+  domains = +local_domains
+  file = /central/filters/$local_part
+  no_verify
+  allow_filter
+  allow_freeze
+
+The filter is run in a separate process under its own uid. Therefore, either 
+check_local_user must be set (as above), in which case the filter is run as the
+local user, or the user option must be used to specify which user to use. If
+both are set, user overrides.
+
+Care should be taken to ensure that none of the commands in the filter file
+specify a significant delivery if the message is to go on to be delivered to
+its intended recipient. The router will not then claim to have dealt with the
+address, so it will be passed on to subsequent routers to be delivered in the
+normal way.
+
+
+
+===============================================================================
+46. MESSAGE PROCESSING
+
+Exim performs various transformations on the sender and recipient addresses of
+all messages that it handles, and also on the messages' header lines. Some of
+these are optional and configurable, while others always take place. All of
+this processing, except rewriting as a result of routing, and the addition or
+removal of header lines while delivering, happens when a message is received,
+before it is placed on Exim's queue.
+
+Some of the automatic processing takes place by default only for
+"locally-originated" messages. This adjective is used to describe messages that
+are not received over TCP/IP, but instead are passed to an Exim process on its
+standard input. This includes the interactive "local SMTP" case that is set up
+by the -bs command line option.
+
+Note: Messages received over TCP/IP on the loopback interface (127.0.0.1 or
+::1) are not considered to be locally-originated. Exim does not treat the
+loopback interface specially in any way.
+
+If you want the loopback interface to be treated specially, you must ensure
+that there are appropriate entries in your ACLs.
+
+
+46.1 Submission mode for non-local messages
+-------------------------------------------
+
+Processing that happens automatically for locally-originated messages (unless 
+suppress_local_fixups is set) can also be requested for messages that are
+received over TCP/IP. The term "submission mode" is used to describe this
+state. Submission mode is set by the modifier
+
+control = submission
+
+in a MAIL, RCPT, or pre-data ACL for an incoming message (see sections 42.21
+and 42.22). This makes Exim treat the message as a local submission, and is
+normally used when the source of the message is known to be an MUA running on a
+client host (as opposed to an MTA). For example, to set submission mode for
+messages originating on the IPv4 loopback interface, you could include the
+following in the MAIL ACL:
+
+warn  hosts = 127.0.0.1
+      control = submission
+
+There are some options that can be used when setting submission mode. A slash
+is used to separate options. For example:
+
+control = submission/sender_retain
+
+Specifying sender_retain has the effect of setting local_sender_retain true and
+local_from_check false for the current incoming message. The first of these
+allows an existing Sender: header in the message to remain, and the second
+suppresses the check to ensure that From: matches the authenticated sender.
+With this setting, Exim still fixes up messages by adding Date: and Message-ID:
+header lines if they are missing, but makes no attempt to check sender
+authenticity in header lines.
+
+When sender_retain is not set, a submission mode setting may specify a domain
+to be used when generating a From: or Sender: header line. For example:
+
+control = submission/domain=some.domain
+
+The domain may be empty. How this value is used is described in sections 46.11
+and 46.16. There is also a name option that allows you to specify the user's
+full name for inclusion in a created Sender: or From: header line. For example:
+
+accept authenticated = *
+       control = submission/domain=wonderland.example/\
+                            name=${lookup {$authenticated_id} \
+                                   lsearch {/etc/exim/namelist}}
+
+Because the name may contain any characters, including slashes, the name option
+must be given last. The remainder of the string is used as the name. For the
+example above, if /etc/exim/namelist contains:
+
+bigegg:  Humpty Dumpty
+
+then when the sender has authenticated as bigegg, the generated Sender: line
+would be:
+
+Sender: Humpty Dumpty <bigegg@wonderland.example>
+
+By default, submission mode forces the return path to the same address as is
+used to create the Sender: header. However, if sender_retain is specified, the
+return path is also left unchanged.
+
+Note: The changes caused by submission mode take effect after the predata ACL.
+This means that any sender checks performed before the fix-ups use the
+untrusted sender address specified by the user, not the trusted sender address
+specified by submission mode. Although this might be slightly unexpected, it
+does mean that you can configure ACL checks to spot that a user is trying to
+spoof another's address.
+
+
+46.2 Line endings
+-----------------
+
+RFC 2821 specifies that CRLF (two characters: carriage-return, followed by
+linefeed) is the line ending for messages transmitted over the Internet using
+SMTP over TCP/IP. However, within individual operating systems, different
+conventions are used. For example, Unix-like systems use just LF, but others
+use CRLF or just CR.
+
+Exim was designed for Unix-like systems, and internally, it stores messages
+using the system's convention of a single LF as a line terminator. When
+receiving a message, all line endings are translated to this standard format.
+Originally, it was thought that programs that passed messages directly to an
+MTA within an operating system would use that system's convention. Experience
+has shown that this is not the case; for example, there are Unix applications
+that use CRLF in this circumstance. For this reason, and for compatibility with
+other MTAs, the way Exim handles line endings for all messages is now as
+follows:
+
+  * LF not preceded by CR is treated as a line ending.
+
+  * CR is treated as a line ending; if it is immediately followed by LF, the LF
+    is ignored.
+
+  * The sequence "CR, dot, CR" does not terminate an incoming SMTP message, nor
+    a local message in the state where a line containing only a dot is a
+    terminator.
+
+  * If a bare CR is encountered within a header line, an extra space is added
+    after the line terminator so as not to end the header line. The reasoning
+    behind this is that bare CRs in header lines are most likely either to be
+    mistakes, or people trying to play silly games.
+
+  * If the first header line received in a message ends with CRLF, a subsequent
+    bare LF in a header line is treated in the same way as a bare CR in a
+    header line.
+
+
+46.3 Unqualified addresses
+--------------------------
+
+By default, Exim expects every envelope address it receives from an external
+host to be fully qualified. Unqualified addresses cause negative responses to
+SMTP commands. However, because SMTP is used as a means of transporting
+messages from MUAs running on personal workstations, there is sometimes a
+requirement to accept unqualified addresses from specific hosts or IP networks.
+
+Exim has two options that separately control which hosts may send unqualified
+sender or recipient addresses in SMTP commands, namely sender_unqualified_hosts
+and recipient_unqualified_hosts. In both cases, if an unqualified address is
+accepted, it is qualified by adding the value of qualify_domain or 
+qualify_recipient, as appropriate.
+
+Unqualified addresses in header lines are automatically qualified for messages
+that are locally originated, unless the -bnq option is given on the command
+line. For messages received over SMTP, unqualified addresses in header lines
+are qualified only if unqualified addresses are permitted in SMTP commands. In
+other words, such qualification is also controlled by sender_unqualified_hosts
+and recipient_unqualified_hosts,
+
+
+46.4 The UUCP From line
+-----------------------
+
+Messages that have come from UUCP (and some other applications) often begin
+with a line containing the envelope sender and a timestamp, following the word
+"From". Examples of two common formats are:
+
+From a.oakley@berlin.mus Fri Jan  5 12:35 GMT 1996
+From f.butler@berlin.mus Fri, 7 Jan 97 14:00:00 GMT
+
+This line precedes the RFC 2822 header lines. For compatibility with Sendmail,
+Exim recognizes such lines at the start of messages that are submitted to it
+via the command line (that is, on the standard input). It does not recognize
+such lines in incoming SMTP messages, unless the sending host matches 
+ignore_fromline_hosts or the -bs option was used for a local message and 
+ignore_fromline_local is set. The recognition is controlled by a regular
+expression that is defined by the uucp_from_pattern option, whose default value
+matches the two common cases shown above and puts the address that follows
+"From" into $1.
+
+When the caller of Exim for a non-SMTP message that contains a "From" line is a
+trusted user, the message's sender address is constructed by expanding the
+contents of uucp_sender_address, whose default value is "$1". This is then
+parsed as an RFC 2822 address. If there is no domain, the local part is
+qualified with qualify_domain unless it is the empty string. However, if the
+command line -f option is used, it overrides the "From" line.
+
+If the caller of Exim is not trusted, the "From" line is recognized, but the
+sender address is not changed. This is also the case for incoming SMTP messages
+that are permitted to contain "From" lines.
+
+Only one "From" line is recognized. If there is more than one, the second is
+treated as a data line that starts the body of the message, as it is not valid
+as a header line. This also happens if a "From" line is present in an incoming
+SMTP message from a source that is not permitted to send them.
+
+
+46.5 Resent- header lines
+-------------------------
+
+RFC 2822 makes provision for sets of header lines starting with the string
+"Resent-" to be added to a message when it is resent by the original recipient
+to somebody else. These headers are Resent-Date:, Resent-From:, Resent-Sender:,
+Resent-To:, Resent-Cc:, Resent-Bcc: and Resent-Message-ID:. The RFC says:
+
+    Resent fields are strictly informational. They MUST NOT be used in the
+    normal processing of replies or other such automatic actions on messages.
+
+This leaves things a bit vague as far as other processing actions such as
+address rewriting are concerned. Exim treats Resent- header lines as follows:
+
+  * A Resent-From: line that just contains the login id of the submitting user
+    is automatically rewritten in the same way as From: (see below).
+
+  * If there's a rewriting rule for a particular header line, it is also
+    applied to Resent- header lines of the same type. For example, a rule that
+    rewrites From: also rewrites Resent-From:.
+
+  * For local messages, if Sender: is removed on input, Resent-Sender: is also
+    removed.
+
+  * For a locally-submitted message, if there are any Resent- header lines but
+    no Resent-Date:, Resent-From:, or Resent-Message-Id:, they are added as
+    necessary. It is the contents of Resent-Message-Id: (rather than 
+    Message-Id:) which are included in log lines in this case.
+
+  * The logic for adding Sender: is duplicated for Resent-Sender: when any 
+    Resent- header lines are present.
+
+
+46.6 The Auto-Submitted: header line
+------------------------------------
+
+Whenever Exim generates an autoreply, a bounce, or a delay warning message, it
+includes the header line:
+
+Auto-Submitted: auto-replied
+
+
+46.7 The Bcc: header line
+-------------------------
+
+If Exim is called with the -t option, to take recipient addresses from a
+message's header, it removes any Bcc: header line that may exist (after
+extracting its addresses). If -t is not present on the command line, any
+existing Bcc: is not removed.
+
+
+46.8 The Date: header line
+--------------------------
+
+If a locally-generated or submission-mode message has no Date: header line,
+Exim adds one, using the current date and time, unless the 
+suppress_local_fixups control has been specified.
+
+
+46.9 The Delivery-date: header line
+-----------------------------------
+
+Delivery-date: header lines are not part of the standard RFC 2822 header set.
+Exim can be configured to add them to the final delivery of messages. (See the
+generic delivery_date_add transport option.) They should not be present in
+messages in transit. If the delivery_date_remove configuration option is set
+(the default), Exim removes Delivery-date: header lines from incoming messages.
+
+
+46.10 The Envelope-to: header line
+----------------------------------
+
+Envelope-to: header lines are not part of the standard RFC 2822 header set.
+Exim can be configured to add them to the final delivery of messages. (See the
+generic envelope_to_add transport option.) They should not be present in
+messages in transit. If the envelope_to_remove configuration option is set (the
+default), Exim removes Envelope-to: header lines from incoming messages.
+
+
+46.11 The From: header line
+---------------------------
+
+If a submission-mode message does not contain a From: header line, Exim adds
+one if either of the following conditions is true:
+
+  * The envelope sender address is not empty (that is, this is not a bounce
+    message). The added header line copies the envelope sender address.
+
+  * The SMTP session is authenticated and $authenticated_id is not empty.
+
+     1. If no domain is specified by the submission control, the local part is
+        $authenticated_id and the domain is $qualify_domain.
+
+     2. If a non-empty domain is specified by the submission control, the local
+        part is $authenticated_id, and the domain is the specified domain.
+
+     3. If an empty domain is specified by the submission control,
+        $authenticated_id is assumed to be the complete address.
+
+A non-empty envelope sender takes precedence.
+
+If a locally-generated incoming message does not contain a From: header line,
+and the suppress_local_fixups control is not set, Exim adds one containing the
+sender's address. The calling user's login name and full name are used to
+construct the address, as described in section 46.18. They are obtained from
+the password data by calling getpwuid() (but see the unknown_login
+configuration option). The address is qualified with qualify_domain.
+
+For compatibility with Sendmail, if an incoming, non-SMTP message has a From:
+header line containing just the unqualified login name of the calling user,
+this is replaced by an address containing the user's login name and full name
+as described in section 46.18.
+
+
+46.12 The Message-ID: header line
+---------------------------------
+
+If a locally-generated or submission-mode incoming message does not contain a 
+Message-ID: or Resent-Message-ID: header line, and the suppress_local_fixups
+control is not set, Exim adds a suitable header line to the message. If there
+are any Resent-: headers in the message, it creates Resent-Message-ID:. The id
+is constructed from Exim's internal message id, preceded by the letter E to
+ensure it starts with a letter, and followed by @ and the primary host name.
+Additional information can be included in this header line by setting the 
+message_id_header_text and/or message_id_header_domain options.
+
+
+46.13 The Received: header line
+-------------------------------
+
+A Received: header line is added at the start of every message. The contents
+are defined by the received_header_text configuration option, and Exim
+automatically adds a semicolon and a timestamp to the configured string.
+
+The Received: header is generated as soon as the message's header lines have
+been received. At this stage, the timestamp in the Received: header line is the
+time that the message started to be received. This is the value that is seen by
+the DATA ACL and by the local_scan() function.
+
+Once a message is accepted, the timestamp in the Received: header line is
+changed to the time of acceptance, which is (apart from a small delay while the
+-H spool file is written) the earliest time at which delivery could start.
+
+
+46.14 The References: header line
+---------------------------------
+
+Messages created by the autoreply transport include a References: header line.
+This is constructed according to the rules that are described in section 3.64
+of RFC 2822 (which states that replies should contain such a header line), and
+section 3.14 of RFC 3834 (which states that automatic responses are not
+different in this respect). However, because some mail processing software does
+not cope well with very long header lines, no more than 12 message IDs are
+copied from the References: header line in the incoming message. If there are
+more than 12, the first one and then the final 11 are copied, before adding the
+message ID of the incoming message.
+
+
+46.15 The Return-path: header line
+----------------------------------
+
+Return-path: header lines are defined as something an MTA may insert when it
+does the final delivery of messages. (See the generic return_path_add transport
+option.) Therefore, they should not be present in messages in transit. If the 
+return_path_remove configuration option is set (the default), Exim removes 
+Return-path: header lines from incoming messages.
+
+
+46.16 The Sender: header line
+-----------------------------
+
+For a locally-originated message from an untrusted user, Exim may remove an
+existing Sender: header line, and it may add a new one. You can modify these
+actions by setting the local_sender_retain option true, the local_from_check
+option false, or by using the suppress_local_fixups control setting.
+
+When a local message is received from an untrusted user and local_from_check is
+true (the default), and the suppress_local_fixups control has not been set, a
+check is made to see if the address given in the From: header line is the
+correct (local) sender of the message. The address that is expected has the
+login name as the local part and the value of qualify_domain as the domain.
+Prefixes and suffixes for the local part can be permitted by setting 
+local_from_prefix and local_from_suffix appropriately. If From: does not
+contain the correct sender, a Sender: line is added to the message.
+
+If you set local_from_check false, this checking does not occur. However, the
+removal of an existing Sender: line still happens, unless you also set 
+local_sender_retain to be true. It is not possible to set both of these options
+true at the same time.
+
+By default, no processing of Sender: header lines is done for messages received
+over TCP/IP or for messages submitted by trusted users. However, when a message
+is received over TCP/IP in submission mode, and sender_retain is not specified
+on the submission control, the following processing takes place:
+
+First, any existing Sender: lines are removed. Then, if the SMTP session is
+authenticated, and $authenticated_id is not empty, a sender address is created
+as follows:
+
+  * If no domain is specified by the submission control, the local part is
+    $authenticated_id and the domain is $qualify_domain.
+
+  * If a non-empty domain is specified by the submission control, the local
+    part is $authenticated_id, and the domain is the specified domain.
+
+  * If an empty domain is specified by the submission control,
+    $authenticated_id is assumed to be the complete address.
+
+This address is compared with the address in the From: header line. If they are
+different, a Sender: header line containing the created address is added.
+Prefixes and suffixes for the local part in From: can be permitted by setting 
+local_from_prefix and local_from_suffix appropriately.
+
+Note: Whenever a Sender: header line is created, the return path for the
+message (the envelope sender address) is changed to be the same address, except
+in the case of submission mode when sender_retain is specified.
+
+
+46.17 Adding and removing header lines in routers and transports
+----------------------------------------------------------------
+
+When a message is delivered, the addition and removal of header lines can be
+specified in a system filter, or on any of the routers and transports that
+process the message. Section 45.6 contains details about modifying headers in a
+system filter. Header lines can also be added in an ACL as a message is
+received (see section 42.24).
+
+In contrast to what happens in a system filter, header modifications that are
+specified on routers and transports apply only to the particular recipient
+addresses that are being processed by those routers and transports. These
+changes do not actually take place until a copy of the message is being
+transported. Therefore, they do not affect the basic set of header lines, and
+they do not affect the values of the variables that refer to header lines.
+
+Note: In particular, this means that any expansions in the configuration of the
+transport cannot refer to the modified header lines, because such expansions
+all occur before the message is actually transported.
+
+For both routers and transports, the argument of a headers_add option must be
+in the form of one or more RFC 2822 header lines, separated by newlines (coded
+as "\n"). For example:
+
+headers_add = X-added-header: added by $primary_hostname\n\
+              X-added-second: another added header line
+
+Exim does not check the syntax of these added header lines.
+
+Multiple headers_add options for a single router or transport can be specified;
+the values will append to a single list of header lines. Each header-line is
+separately expanded.
+
+The argument of a headers_remove option must consist of a colon-separated list
+of header names. This is confusing, because header names themselves are often
+terminated by colons. In this case, the colons are the list separators, not
+part of the names. For example:
+
+headers_remove = return-receipt-to:acknowledge-to
+
+Multiple headers_remove options for a single router or transport can be
+specified; the arguments will append to a single header-names list. Each item
+is separately expanded.
+
+When headers_add or headers_remove is specified on a router, items are expanded
+at routing time, and then associated with all addresses that are accepted by
+that router, and also with any new addresses that it generates. If an address
+passes through several routers as a result of aliasing or forwarding, the
+changes are cumulative.
+
+However, this does not apply to multiple routers that result from the use of
+the unseen option. Any header modifications that were specified by the "unseen"
+router or its predecessors apply only to the "unseen" delivery.
+
+Addresses that end up with different headers_add or headers_remove settings
+cannot be delivered together in a batch, so a transport is always dealing with
+a set of addresses that have the same header-processing requirements.
+
+The transport starts by writing the original set of header lines that arrived
+with the message, possibly modified by the system filter. As it writes out
+these lines, it consults the list of header names that were attached to the
+recipient address(es) by headers_remove options in routers, and it also
+consults the transport's own headers_remove option. Header lines whose names
+are on either of these lists are not written out. If there are multiple
+instances of any listed header, they are all skipped.
+
+After the remaining original header lines have been written, new header lines
+that were specified by routers' headers_add options are written, in the order
+in which they were attached to the address. These are followed by any header
+lines specified by the transport's headers_add option.
+
+This way of handling header line modifications in routers and transports has
+the following consequences:
+
+  * The original set of header lines, possibly modified by the system filter,
+    remains "visible", in the sense that the $header_xxx variables refer to it,
+    at all times.
+
+  * Header lines that are added by a router's headers_add option are not
+    accessible by means of the $header_xxx expansion syntax in subsequent
+    routers or the transport.
+
+  * Conversely, header lines that are specified for removal by headers_remove
+    in a router remain visible to subsequent routers and the transport.
+
+  * Headers added to an address by headers_add in a router cannot be removed by
+    a later router or by a transport.
+
+  * An added header can refer to the contents of an original header that is to
+    be removed, even it has the same name as the added header. For example:
+
+    headers_remove = subject
+    headers_add = Subject: new subject (was: $h_subject:)
+
+Warning: The headers_add and headers_remove options cannot be used for a 
+redirect router that has the one_time option set.
+
+
+46.18 Constructed addresses
+---------------------------
+
+When Exim constructs a sender address for a locally-generated message, it uses
+the form
+
+<user name>  <login@qualify_domain>
+
+For example:
+
+Zaphod Beeblebrox <zaphod@end.univ.example>
+
+The user name is obtained from the -F command line option if set, or otherwise
+by looking up the calling user by getpwuid() and extracting the "gecos" field
+from the password entry. If the "gecos" field contains an ampersand character,
+this is replaced by the login name with the first letter upper cased, as is
+conventional in a number of operating systems. See the gecos_name option for a
+way to tailor the handling of the "gecos" field. The unknown_username option
+can be used to specify user names in cases when there is no password file
+entry.
+
+In all cases, the user name is made to conform to RFC 2822 by quoting all or
+parts of it if necessary. In addition, if it contains any non-printing
+characters, it is encoded as described in RFC 2047, which defines a way of
+including non-ASCII characters in header lines. The value of the 
+headers_charset option specifies the name of the encoding that is used (the
+characters are assumed to be in this encoding). The setting of 
+print_topbitchars controls whether characters with the top bit set (that is,
+with codes greater than 127) count as printing characters or not.
+
+
+46.19 Case of local parts
+-------------------------
+
+RFC 2822 states that the case of letters in the local parts of addresses cannot
+be assumed to be non-significant. Exim preserves the case of local parts of
+addresses, but by default it uses a lower-cased form when it is routing,
+because on most Unix systems, usernames are in lower case and case-insensitive
+routing is required. However, any particular router can be made to use the
+original case for local parts by setting the caseful_local_part generic router
+option.
+
+If you must have mixed-case user names on your system, the best way to proceed,
+assuming you want case-independent handling of incoming email, is to set up
+your first router to convert incoming local parts in your domains to the
+correct case by means of a file lookup. For example:
+
+correct_case:
+  driver = redirect
+  domains = +local_domains
+  data = ${lookup{$local_part}cdb\
+              {/etc/usercased.cdb}{$value}fail}\
+              @$domain
+
+For this router, the local part is forced to lower case by the default action (
+caseful_local_part is not set). The lower-cased local part is used to look up a
+new local part in the correct case. If you then set caseful_local_part on any
+subsequent routers which process your domains, they will operate on local parts
+with the correct case in a case-sensitive manner.
+
+
+46.20 Dots in local parts
+-------------------------
+
+RFC 2822 forbids empty components in local parts. That is, an unquoted local
+part may not begin or end with a dot, nor have two consecutive dots in the
+middle. However, it seems that many MTAs do not enforce this, so Exim permits
+empty components for compatibility.
+
+
+46.21 Rewriting addresses
+-------------------------
+
+Rewriting of sender and recipient addresses, and addresses in headers, can
+happen automatically, or as the result of configuration options, as described
+in chapter 31. The headers that may be affected by this are Bcc:, Cc:, From:, 
+Reply-To:, Sender:, and To:.
+
+Automatic rewriting includes qualification, as mentioned above. The other case
+in which it can happen is when an incomplete non-local domain is given. The
+routing process may cause this to be expanded into the full domain name. For
+example, a header such as
+
+To: hare@teaparty
+
+might get rewritten as
+
+To: hare@teaparty.wonderland.fict.example
+
+Rewriting as a result of routing is the one kind of message processing that
+does not happen at input time, as it cannot be done until the address has been
+routed.
+
+Strictly, one should not do any deliveries of a message until all its addresses
+have been routed, in case any of the headers get changed as a result of
+routing. However, doing this in practice would hold up many deliveries for
+unreasonable amounts of time, just because one address could not immediately be
+routed. Exim therefore does not delay other deliveries when routing of one or
+more addresses is deferred.
+
+
+
+===============================================================================
+47. SMTP PROCESSING
+
+Exim supports a number of different ways of using the SMTP protocol, and its
+LMTP variant, which is an interactive protocol for transferring messages into a
+closed mail store application. This chapter contains details of how SMTP is
+processed. For incoming mail, the following are available:
+
+  * SMTP over TCP/IP (Exim daemon or inetd);
+
+  * SMTP over the standard input and output (the -bs option);
+
+  * Batched SMTP on the standard input (the -bS option).
+
+For mail delivery, the following are available:
+
+  * SMTP over TCP/IP (the smtp transport);
+
+  * LMTP over TCP/IP (the smtp transport with the protocol option set to
+    "lmtp");
+
+  * LMTP over a pipe to a process running in the local host (the lmtp
+    transport);
+
+  * Batched SMTP to a file or pipe (the appendfile and pipe transports with the
+    use_bsmtp option set).
+
+Batched SMTP is the name for a process in which batches of messages are stored
+in or read from files (or pipes), in a format in which SMTP commands are used
+to contain the envelope information.
+
+
+47.1 Outgoing SMTP and LMTP over TCP/IP
+---------------------------------------
+
+Outgoing SMTP and LMTP over TCP/IP is implemented by the smtp transport. The 
+protocol option selects which protocol is to be used, but the actual processing
+is the same in both cases.
+
+If, in response to its EHLO command, Exim is told that the SIZE parameter is
+supported, it adds SIZE=<n> to each subsequent MAIL command. The value of <n>
+is the message size plus the value of the size_addition option (default 1024)
+to allow for additions to the message such as per-transport header lines, or
+changes made in a transport filter. If size_addition is set negative, the use
+of SIZE is suppressed.
+
+If the remote server advertises support for PIPELINING, Exim uses the
+pipelining extension to SMTP (RFC 2197) to reduce the number of TCP/IP packets
+required for the transaction.
+
+If the remote server advertises support for the STARTTLS command, and Exim was
+built to support TLS encryption, it tries to start a TLS session unless the
+server matches hosts_avoid_tls. See chapter 41 for more details. Either a match
+in that or hosts_verify_avoid_tls apply when the transport is called for
+verification.
+
+If the remote server advertises support for the AUTH command, Exim scans the
+authenticators configuration for any suitable client settings, as described in
+chapter 33.
+
+Responses from the remote host are supposed to be terminated by CR followed by
+LF. However, there are known to be hosts that do not send CR characters, so in
+order to be able to interwork with such hosts, Exim treats LF on its own as a
+line terminator.
+
+If a message contains a number of different addresses, all those with the same
+characteristics (for example, the same envelope sender) that resolve to the
+same set of hosts, in the same order, are sent in a single SMTP transaction,
+even if they are for different domains, unless there are more than the setting
+of the max_rcpts option in the smtp transport allows, in which case they are
+split into groups containing no more than max_rcpts addresses each. If 
+remote_max_parallel is greater than one, such groups may be sent in parallel
+sessions. The order of hosts with identical MX values is not significant when
+checking whether addresses can be batched in this way.
+
+When the smtp transport suffers a temporary failure that is not
+message-related, Exim updates its transport-specific database, which contains
+records indexed by host name that remember which messages are waiting for each
+particular host. It also updates the retry database with new retry times.
+
+Exim's retry hints are based on host name plus IP address, so if one address of
+a multi-homed host is broken, it will soon be skipped most of the time. See the
+next section for more detail about error handling.
+
+When a message is successfully delivered over a TCP/IP SMTP connection, Exim
+looks in the hints database for the transport to see if there are any queued
+messages waiting for the host to which it is connected. If it finds one, it
+creates a new Exim process using the -MC option (which can only be used by a
+process running as root or the Exim user) and passes the TCP/IP socket to it so
+that it can deliver another message using the same socket. The new process does
+only those deliveries that are routed to the connected host, and may in turn
+pass the socket on to a third process, and so on.
+
+The connection_max_messages option of the smtp transport can be used to limit
+the number of messages sent down a single TCP/IP connection.
+
+The second and subsequent messages delivered down an existing connection are
+identified in the main log by the addition of an asterisk after the closing
+square bracket of the IP address.
+
+
+47.2 Errors in outgoing SMTP
+----------------------------
+
+Three different kinds of error are recognized for outgoing SMTP: host errors,
+message errors, and recipient errors.
+
+Host errors
+
+    A host error is not associated with a particular message or with a
+    particular recipient of a message. The host errors are:
+
+      * Connection refused or timed out,
+
+      * Any error response code on connection,
+
+      * Any error response code to EHLO or HELO,
+
+      * Loss of connection at any time, except after ".",
+
+      * I/O errors at any time,
+
+      * Timeouts during the session, other than in response to MAIL, RCPT or
+        the "." at the end of the data.
+
+    For a host error, a permanent error response on connection, or in response
+    to EHLO, causes all addresses routed to the host to be failed. Any other
+    host error causes all addresses to be deferred, and retry data to be
+    created for the host. It is not tried again, for any message, until its
+    retry time arrives. If the current set of addresses are not all delivered
+    in this run (to some alternative host), the message is added to the list of
+    those waiting for this host, so if it is still undelivered when a
+    subsequent successful delivery is made to the host, it will be sent down
+    the same SMTP connection.
+
+Message errors
+
+    A message error is associated with a particular message when sent to a
+    particular host, but not with a particular recipient of the message. The
+    message errors are:
+
+      * Any error response code to MAIL, DATA, or the "." that terminates the
+        data,
+
+      * Timeout after MAIL,
+
+      * Timeout or loss of connection after the "." that terminates the data. A
+        timeout after the DATA command itself is treated as a host error, as is
+        loss of connection at any other time.
+
+    For a message error, a permanent error response (5xx) causes all addresses
+    to be failed, and a delivery error report to be returned to the sender. A
+    temporary error response (4xx), or one of the timeouts, causes all
+    addresses to be deferred. Retry data is not created for the host, but
+    instead, a retry record for the combination of host plus message id is
+    created. The message is not added to the list of those waiting for this
+    host. This ensures that the failing message will not be sent to this host
+    again until the retry time arrives. However, other messages that are routed
+    to the host are not affected, so if it is some property of the message that
+    is causing the error, it will not stop the delivery of other mail.
+
+    If the remote host specified support for the SIZE parameter in its response
+    to EHLO, Exim adds SIZE=nnn to the MAIL command, so an over-large message
+    will cause a message error because the error arrives as a response to MAIL.
+
+Recipient errors
+
+    A recipient error is associated with a particular recipient of a message.
+    The recipient errors are:
+
+      * Any error response to RCPT,
+
+      * Timeout after RCPT.
+
+    For a recipient error, a permanent error response (5xx) causes the
+    recipient address to be failed, and a bounce message to be returned to the
+    sender. A temporary error response (4xx) or a timeout causes the failing
+    address to be deferred, and routing retry data to be created for it. This
+    is used to delay processing of the address in subsequent queue runs, until
+    its routing retry time arrives. This applies to all messages, but because
+    it operates only in queue runs, one attempt will be made to deliver a new
+    message to the failing address before the delay starts to operate. This
+    ensures that, if the failure is really related to the message rather than
+    the recipient ("message too big for this recipient" is a possible example),
+    other messages have a chance of getting delivered. If a delivery to the
+    address does succeed, the retry information gets cleared, so all stuck
+    messages get tried again, and the retry clock is reset.
+
+    The message is not added to the list of those waiting for this host. Use of
+    the host for other messages is unaffected, and except in the case of a
+    timeout, other recipients are processed independently, and may be
+    successfully delivered in the current SMTP session. After a timeout it is
+    of course impossible to proceed with the session, so all addresses get
+    deferred. However, those other than the one that failed do not suffer any
+    subsequent retry delays. Therefore, if one recipient is causing trouble,
+    the others have a chance of getting through when a subsequent delivery
+    attempt occurs before the failing recipient's retry time.
+
+In all cases, if there are other hosts (or IP addresses) available for the
+current set of addresses (for example, from multiple MX records), they are
+tried in this run for any undelivered addresses, subject of course to their own
+retry data. In other words, recipient error retry data does not take effect
+until the next delivery attempt.
+
+Some hosts have been observed to give temporary error responses to every MAIL
+command at certain times ("insufficient space" has been seen). It would be nice
+if such circumstances could be recognized, and defer data for the host itself
+created, but this is not possible within the current Exim design. What actually
+happens is that retry data for every (host, message) combination is created.
+
+The reason that timeouts after MAIL and RCPT are treated specially is that
+these can sometimes arise as a result of the remote host's verification
+procedures. Exim makes this assumption, and treats them as if a temporary error
+response had been received. A timeout after "." is treated specially because it
+is known that some broken implementations fail to recognize the end of the
+message if the last character of the last line is a binary zero. Thus, it is
+helpful to treat this case as a message error.
+
+Timeouts at other times are treated as host errors, assuming a problem with the
+host, or the connection to it. If a timeout after MAIL, RCPT, or "." is really
+a connection problem, the assumption is that at the next try the timeout is
+likely to occur at some other point in the dialogue, causing it then to be
+treated as a host error.
+
+There is experimental evidence that some MTAs drop the connection after the
+terminating "." if they do not like the contents of the message for some
+reason, in contravention of the RFC, which indicates that a 5xx response should
+be given. That is why Exim treats this case as a message rather than a host
+error, in order not to delay other messages to the same host.
+
+
+47.3 Incoming SMTP messages over TCP/IP
+---------------------------------------
+
+Incoming SMTP messages can be accepted in one of two ways: by running a
+listening daemon, or by using inetd. In the latter case, the entry in /etc/
+inetd.conf should be like this:
+
+smtp stream tcp nowait exim /opt/exim/bin/exim in.exim -bs
+
+Exim distinguishes between this case and the case of a locally running user
+agent using the -bs option by checking whether or not the standard input is a
+socket. When it is, either the port must be privileged (less than 1024), or the
+caller must be root or the Exim user. If any other user passes a socket with an
+unprivileged port number, Exim prints a message on the standard error stream
+and exits with an error code.
+
+By default, Exim does not make a log entry when a remote host connects or
+disconnects (either via the daemon or inetd), unless the disconnection is
+unexpected. It can be made to write such log entries by setting the 
+smtp_connection log selector.
+
+Commands from the remote host are supposed to be terminated by CR followed by
+LF. However, there are known to be hosts that do not send CR characters. In
+order to be able to interwork with such hosts, Exim treats LF on its own as a
+line terminator. Furthermore, because common code is used for receiving
+messages from all sources, a CR on its own is also interpreted as a line
+terminator. However, the sequence "CR, dot, CR" does not terminate incoming
+SMTP data.
+
+One area that sometimes gives rise to problems concerns the EHLO or HELO
+commands. Some clients send syntactically invalid versions of these commands,
+which Exim rejects by default. (This is nothing to do with verifying the data
+that is sent, so helo_verify_hosts is not relevant.) You can tell Exim not to
+apply a syntax check by setting helo_accept_junk_hosts to match the broken
+hosts that send invalid commands.
+
+The amount of disk space available is checked whenever SIZE is received on a
+MAIL command, independently of whether message_size_limit or check_spool_space
+is configured, unless smtp_check_spool_space is set false. A temporary error is
+given if there is not enough space. If check_spool_space is set, the check is
+for that amount of space plus the value given with SIZE, that is, it checks
+that the addition of the incoming message will not reduce the space below the
+threshold.
+
+When a message is successfully received, Exim includes the local message id in
+its response to the final "." that terminates the data. If the remote host logs
+this text it can help with tracing what has happened to a message.
+
+The Exim daemon can limit the number of simultaneous incoming connections it is
+prepared to handle (see the smtp_accept_max option). It can also limit the
+number of simultaneous incoming connections from a single remote host (see the 
+smtp_accept_max_per_host option). Additional connection attempts are rejected
+using the SMTP temporary error code 421.
+
+The Exim daemon does not rely on the SIGCHLD signal to detect when a subprocess
+has finished, as this can get lost at busy times. Instead, it looks for
+completed subprocesses every time it wakes up. Provided there are other things
+happening (new incoming calls, starts of queue runs), completed processes will
+be noticed and tidied away. On very quiet systems you may sometimes see a
+"defunct" Exim process hanging about. This is not a problem; it will be noticed
+when the daemon next wakes up.
+
+When running as a daemon, Exim can reserve some SMTP slots for specific hosts,
+and can also be set up to reject SMTP calls from non-reserved hosts at times of
+high system load - for details see the smtp_accept_reserve, smtp_load_reserve,
+and smtp_reserve_hosts options. The load check applies in both the daemon and 
+inetd cases.
+
+Exim normally starts a delivery process for each message received, though this
+can be varied by means of the -odq command line option and the queue_only, 
+queue_only_file, and queue_only_load options. The number of simultaneously
+running delivery processes started in this way from SMTP input can be limited
+by the smtp_accept_queue and smtp_accept_queue_per_connection options. When
+either limit is reached, subsequently received messages are just put on the
+input queue without starting a delivery process.
+
+The controls that involve counts of incoming SMTP calls (smtp_accept_max, 
+smtp_accept_queue, smtp_accept_reserve) are not available when Exim is started
+up from the inetd daemon, because in that case each connection is handled by an
+entirely independent Exim process. Control by load average is, however,
+available with inetd.
+
+Exim can be configured to verify addresses in incoming SMTP commands as they
+are received. See chapter 42 for details. It can also be configured to rewrite
+addresses at this time - before any syntax checking is done. See section 31.9.
+
+Exim can also be configured to limit the rate at which a client host submits
+MAIL and RCPT commands in a single SMTP session. See the smtp_ratelimit_hosts
+option.
+
+
+47.4 Unrecognized SMTP commands
+-------------------------------
+
+If Exim receives more than smtp_max_unknown_commands unrecognized SMTP commands
+during a single SMTP connection, it drops the connection after sending the
+error response to the last command. The default value for 
+smtp_max_unknown_commands is 3. This is a defence against some kinds of abuse
+that subvert web servers into making connections to SMTP ports; in these
+circumstances, a number of non-SMTP lines are sent first.
+
+
+47.5 Syntax and protocol errors in SMTP commands
+------------------------------------------------
+
+A syntax error is detected if an SMTP command is recognized, but there is
+something syntactically wrong with its data, for example, a malformed email
+address in a RCPT command. Protocol errors include invalid command sequencing
+such as RCPT before MAIL. If Exim receives more than smtp_max_synprot_errors
+such commands during a single SMTP connection, it drops the connection after
+sending the error response to the last command. The default value for 
+smtp_max_synprot_errors is 3. This is a defence against broken clients that
+loop sending bad commands (yes, it has been seen).
+
+
+47.6 Use of non-mail SMTP commands
+----------------------------------
+
+The "non-mail" SMTP commands are those other than MAIL, RCPT, and DATA. Exim
+counts such commands, and drops the connection if there are too many of them in
+a single SMTP session. This action catches some denial-of-service attempts and
+things like repeated failing AUTHs, or a mad client looping sending EHLO. The
+global option smtp_accept_max_nonmail defines what "too many" means. Its
+default value is 10.
+
+When a new message is expected, one occurrence of RSET is not counted. This
+allows a client to send one RSET between messages (this is not necessary, but
+some clients do it). Exim also allows one uncounted occurrence of HELO or EHLO,
+and one occurrence of STARTTLS between messages. After starting up a TLS
+session, another EHLO is expected, and so it too is not counted.
+
+The first occurrence of AUTH in a connection, or immediately following STARTTLS
+is also not counted. Otherwise, all commands other than MAIL, RCPT, DATA, and
+QUIT are counted.
+
+You can control which hosts are subject to the limit set by 
+smtp_accept_max_nonmail by setting smtp_accept_max_nonmail_hosts. The default
+value is "*", which makes the limit apply to all hosts. This option means that
+you can exclude any specific badly-behaved hosts that you have to live with.
+
+
+47.7 The VRFY and EXPN commands
+-------------------------------
+
+When Exim receives a VRFY or EXPN command on a TCP/IP connection, it runs the
+ACL specified by acl_smtp_vrfy or acl_smtp_expn (as appropriate) in order to
+decide whether the command should be accepted or not. If no ACL is defined, the
+command is rejected.
+
+When VRFY is accepted, it runs exactly the same code as when Exim is called
+with the -bv option.
+
+When EXPN is accepted, a single-level expansion of the address is done. EXPN is
+treated as an "address test" (similar to the -bt option) rather than a
+verification (the -bv option). If an unqualified local part is given as the
+argument to EXPN, it is qualified with qualify_domain. Rejections of VRFY and
+EXPN commands are logged on the main and reject logs, and VRFY verification
+failures are logged on the main log for consistency with RCPT failures.
+
+
+47.8 The ETRN command
+---------------------
+
+RFC 1985 describes an SMTP command called ETRN that is designed to overcome the
+security problems of the TURN command (which has fallen into disuse). When Exim
+receives an ETRN command on a TCP/IP connection, it runs the ACL specified by 
+acl_smtp_etrn in order to decide whether the command should be accepted or not.
+If no ACL is defined, the command is rejected.
+
+The ETRN command is concerned with "releasing" messages that are awaiting
+delivery to certain hosts. As Exim does not organize its message queue by host,
+the only form of ETRN that is supported by default is the one where the text
+starts with the "#" prefix, in which case the remainder of the text is specific
+to the SMTP server. A valid ETRN command causes a run of Exim with the -R
+option to happen, with the remainder of the ETRN text as its argument. For
+example,
+
+ETRN #brigadoon
+
+runs the command
+
+exim -R brigadoon
+
+which causes a delivery attempt on all messages with undelivered addresses
+containing the text "brigadoon". When smtp_etrn_serialize is set (the default),
+Exim prevents the simultaneous execution of more than one queue run for the
+same argument string as a result of an ETRN command. This stops a misbehaving
+client from starting more than one queue runner at once.
+
+Exim implements the serialization by means of a hints database in which a
+record is written whenever a process is started by ETRN, and deleted when the
+process completes. However, Exim does not keep the SMTP session waiting for the
+ETRN process to complete. Once ETRN is accepted, the client is sent a "success"
+return code. Obviously there is scope for hints records to get left lying
+around if there is a system or program crash. To guard against this, Exim
+ignores any records that are more than six hours old.
+
+For more control over what ETRN does, the smtp_etrn_command option can used.
+This specifies a command that is run whenever ETRN is received, whatever the
+form of its argument. For example:
+
+smtp_etrn_command = /etc/etrn_command $domain \
+                    $sender_host_address
+
+The string is split up into arguments which are independently expanded. The
+expansion variable $domain is set to the argument of the ETRN command, and no
+syntax checking is done on the contents of this argument. Exim does not wait
+for the command to complete, so its status code is not checked. Exim runs under
+its own uid and gid when receiving incoming SMTP, so it is not possible for it
+to change them before running the command.
+
+
+47.9 Incoming local SMTP
+------------------------
+
+Some user agents use SMTP to pass messages to their local MTA using the
+standard input and output, as opposed to passing the envelope on the command
+line and writing the message to the standard input. This is supported by the 
+-bs option. This form of SMTP is handled in the same way as incoming messages
+over TCP/IP (including the use of ACLs), except that the envelope sender given
+in a MAIL command is ignored unless the caller is trusted. In an ACL you can
+detect this form of SMTP input by testing for an empty host identification. It
+is common to have this as the first line in the ACL that runs for RCPT
+commands:
+
+accept hosts = :
+
+This accepts SMTP messages from local processes without doing any other tests.
+
+
+47.10 Outgoing batched SMTP
+---------------------------
+
+Both the appendfile and pipe transports can be used for handling batched SMTP.
+Each has an option called use_bsmtp which causes messages to be output in BSMTP
+format. No SMTP responses are possible for this form of delivery. All it is
+doing is using SMTP commands as a way of transmitting the envelope along with
+the message.
+
+The message is written to the file or pipe preceded by the SMTP commands MAIL
+and RCPT, and followed by a line containing a single dot. Lines in the message
+that start with a dot have an extra dot added. The SMTP command HELO is not
+normally used. If it is required, the message_prefix option can be used to
+specify it.
+
+Because appendfile and pipe are both local transports, they accept only one
+recipient address at a time by default. However, you can arrange for them to
+handle several addresses at once by setting the batch_max option. When this is
+done for BSMTP, messages may contain multiple RCPT commands. See chapter 25 for
+more details.
+
+When one or more addresses are routed to a BSMTP transport by a router that
+sets up a host list, the name of the first host on the list is available to the
+transport in the variable $host. Here is an example of such a transport and
+router:
+
+begin routers
+route_append:
+  driver = manualroute
+  transport = smtp_appendfile
+  route_list = domain.example  batch.host.example
+
+begin transports
+smtp_appendfile:
+  driver = appendfile
+  directory = /var/bsmtp/$host
+  batch_max = 1000
+  use_bsmtp
+  user = exim
+
+This causes messages addressed to domain.example to be written in BSMTP format
+to /var/bsmtp/batch.host.example, with only a single copy of each message
+(unless there are more than 1000 recipients).
+
+
+47.11 Incoming batched SMTP
+---------------------------
+
+The -bS command line option causes Exim to accept one or more messages by
+reading SMTP on the standard input, but to generate no responses. If the caller
+is trusted, the senders in the MAIL commands are believed; otherwise the sender
+is always the caller of Exim. Unqualified senders and receivers are not
+rejected (there seems little point) but instead just get qualified. HELO and
+EHLO act as RSET; VRFY, EXPN, ETRN and HELP, act as NOOP; QUIT quits.
+
+Minimal policy checking is done for BSMTP input. Only the non-SMTP ACL is run
+in the same way as for non-SMTP local input.
+
+If an error is detected while reading a message, including a missing "." at the
+end, Exim gives up immediately. It writes details of the error to the standard
+output in a stylized way that the calling program should be able to make some
+use of automatically, for example:
+
+554 Unexpected end of file
+Transaction started in line 10
+Error detected in line 14
+
+It writes a more verbose version, for human consumption, to the standard error
+file, for example:
+
+An error was detected while processing a file of BSMTP input.
+The error message was:
+
+501 '>' missing at end of address
+
+The SMTP transaction started in line 10.
+The error was detected in line 12.
+The SMTP command at fault was:
+
+rcpt to:<malformed@in.com.plete
+
+1 previous message was successfully processed.
+The rest of the batch was abandoned.
+
+The return code from Exim is zero only if there were no errors. It is 1 if some
+messages were accepted before an error was detected, and 2 if no messages were
+accepted.
+
+
+
+===============================================================================
+48. CUSTOMIZING BOUNCE AND WARNING MESSAGES
+
+When a message fails to be delivered, or remains on the queue for more than a
+configured amount of time, Exim sends a message to the original sender, or to
+an alternative configured address. The text of these messages is built into the
+code of Exim, but it is possible to change it, either by adding a single
+string, or by replacing each of the paragraphs by text supplied in a file.
+
+The From: and To: header lines are automatically generated; you can cause a 
+Reply-To: line to be added by setting the errors_reply_to option. Exim also
+adds the line
+
+Auto-Submitted: auto-generated
+
+to all warning and bounce messages,
+
+
+48.1 Customizing bounce messages
+--------------------------------
+
+If bounce_message_text is set, its contents are included in the default message
+immediately after "This message was created automatically by mail delivery
+software." The string is not expanded. It is not used if bounce_message_file is
+set.
+
+When bounce_message_file is set, it must point to a template file for
+constructing error messages. The file consists of a series of text items,
+separated by lines consisting of exactly four asterisks. If the file cannot be
+opened, default text is used and a message is written to the main and panic
+logs. If any text item in the file is empty, default text is used for that
+item.
+
+Each item of text that is read from the file is expanded, and there are two
+expansion variables which can be of use here: $bounce_recipient is set to the
+recipient of an error message while it is being created, and
+$bounce_return_size_limit contains the value of the return_size_limit option,
+rounded to a whole number.
+
+The items must appear in the file in the following order:
+
+  * The first item is included in the headers, and should include at least a 
+    Subject: header. Exim does not check the syntax of these headers.
+
+  * The second item forms the start of the error message. After it, Exim lists
+    the failing addresses with their error messages.
+
+  * The third item is used to introduce any text from pipe transports that is
+    to be returned to the sender. It is omitted if there is no such text.
+
+  * The fourth item is used to introduce the copy of the message that is
+    returned as part of the error report.
+
+  * The fifth item is added after the fourth one if the returned message is
+    truncated because it is bigger than return_size_limit.
+
+  * The sixth item is added after the copy of the original message.
+
+The default state (bounce_message_file unset) is equivalent to the following
+file, in which the sixth item is empty. The Subject: and some other lines have
+been split in order to fit them on the page:
+
+Subject: Mail delivery failed
+  ${if eq{$sender_address}{$bounce_recipient}
+  {: returning message to sender}}
+****
+This message was created automatically by mail delivery software.
+
+A message ${if eq{$sender_address}{$bounce_recipient}
+  {that you sent }{sent by
+
+<$sender_address>
+
+}}could not be delivered to all of its recipients.
+This is a permanent error. The following address(es) failed:
+****
+The following text was generated during the delivery attempt(s):
+****
+------ This is a copy of the message, including all the headers.
+  ------
+****
+------ The body of the message is $message_size characters long;
+  only the first
+------ $bounce_return_size_limit or so are included here.
+****
+
+
+48.2 Customizing warning messages
+---------------------------------
+
+The option warn_message_file can be pointed at a template file for use when
+warnings about message delays are created. In this case there are only three
+text sections:
+
+  * The first item is included in the headers, and should include at least a 
+    Subject: header. Exim does not check the syntax of these headers.
+
+  * The second item forms the start of the warning message. After it, Exim
+    lists the delayed addresses.
+
+  * The third item then ends the message.
+
+The default state is equivalent to the following file, except that some lines
+have been split here, in order to fit them on the page:
+
+Subject: Warning: message $message_exim_id delayed
+  $warn_message_delay
+****
+This message was created automatically by mail delivery software.
+
+A message ${if eq{$sender_address}{$warn_message_recipients}
+{that you sent }{sent by
+
+<$sender_address>
+
+}}has not been delivered to all of its recipients after
+more than $warn_message_delay on the queue on $primary_hostname.
+
+The message identifier is:     $message_exim_id
+The subject of the message is: $h_subject
+The date of the message is:    $h_date
+
+The following address(es) have not yet been delivered:
+****
+No action is required on your part. Delivery attempts will
+continue for some time, and this warning may be repeated at
+intervals if the message remains undelivered. Eventually the
+mail delivery software will give up, and when that happens,
+the message will be returned to you.
+
+However, in the default state the subject and date lines are omitted if no
+appropriate headers exist. During the expansion of this file,
+$warn_message_delay is set to the delay time in one of the forms "<n> minutes"
+or "<n> hours", and $warn_message_recipients contains a list of recipients for
+the warning message. There may be more than one if there are multiple addresses
+with different errors_to settings on the routers that handled them.
+
+
+
+===============================================================================
+49. SOME COMMON CONFIGURATION SETTINGS
+
+This chapter discusses some configuration settings that seem to be fairly
+common. More examples and discussion can be found in the Exim book.
+
+
+49.1 Sending mail to a smart host
+---------------------------------
+
+If you want to send all mail for non-local domains to a "smart host", you
+should replace the default dnslookup router with a router which does the
+routing explicitly:
+
+send_to_smart_host:
+  driver = manualroute
+  route_list = !+local_domains smart.host.name
+  transport = remote_smtp
+
+You can use the smart host's IP address instead of the name if you wish. If you
+are using Exim only to submit messages to a smart host, and not for receiving
+incoming messages, you can arrange for it to do the submission synchronously by
+setting the mua_wrapper option (see chapter 50).
+
+
+49.2 Using Exim to handle mailing lists
+---------------------------------------
+
+Exim can be used to run simple mailing lists, but for large and/or complicated
+requirements, the use of additional specialized mailing list software such as
+Majordomo or Mailman is recommended.
+
+The redirect router can be used to handle mailing lists where each list is
+maintained in a separate file, which can therefore be managed by an independent
+manager. The domains router option can be used to run these lists in a separate
+domain from normal mail. For example:
+
+lists:
+  driver = redirect
+  domains = lists.example
+  file = /usr/lists/$local_part
+  forbid_pipe
+  forbid_file
+  errors_to = $local_part-request@lists.example
+  no_more
+
+This router is skipped for domains other than lists.example. For addresses in
+that domain, it looks for a file that matches the local part. If there is no
+such file, the router declines, but because no_more is set, no subsequent
+routers are tried, and so the whole delivery fails.
+
+The forbid_pipe and forbid_file options prevent a local part from being
+expanded into a file name or a pipe delivery, which is usually inappropriate in
+a mailing list.
+
+The errors_to option specifies that any delivery errors caused by addresses
+taken from a mailing list are to be sent to the given address rather than the
+original sender of the message. However, before acting on this, Exim verifies
+the error address, and ignores it if verification fails.
+
+For example, using the configuration above, mail sent to dicts@lists.example is
+passed on to those addresses contained in /usr/lists/dicts, with error reports
+directed to dicts-request@lists.example, provided that this address can be
+verified. There could be a file called /usr/lists/dicts-request containing the
+address(es) of this particular list's manager(s), but other approaches, such as
+setting up an earlier router (possibly using the local_part_prefix or 
+local_part_suffix options) to handle addresses of the form owner-xxx or xxx-
+request, are also possible.
+
+
+49.3 Syntax errors in mailing lists
+-----------------------------------
+
+If an entry in redirection data contains a syntax error, Exim normally defers
+delivery of the original address. That means that a syntax error in a mailing
+list holds up all deliveries to the list. This may not be appropriate when a
+list is being maintained automatically from data supplied by users, and the
+addresses are not rigorously checked.
+
+If the skip_syntax_errors option is set, the redirect router just skips entries
+that fail to parse, noting the incident in the log. If in addition 
+syntax_errors_to is set to a verifiable address, a message is sent to it
+whenever a broken address is skipped. It is usually appropriate to set 
+syntax_errors_to to the same address as errors_to.
+
+
+49.4 Re-expansion of mailing lists
+----------------------------------
+
+Exim remembers every individual address to which a message has been delivered,
+in order to avoid duplication, but it normally stores only the original
+recipient addresses with a message. If all the deliveries to a mailing list
+cannot be done at the first attempt, the mailing list is re-expanded when the
+delivery is next tried. This means that alterations to the list are taken into
+account at each delivery attempt, so addresses that have been added to the list
+since the message arrived will therefore receive a copy of the message, even
+though it pre-dates their subscription.
+
+If this behaviour is felt to be undesirable, the one_time option can be set on
+the redirect router. If this is done, any addresses generated by the router
+that fail to deliver at the first attempt are added to the message as "top
+level" addresses, and the parent address that generated them is marked
+"delivered". Thus, expansion of the mailing list does not happen again at the
+subsequent delivery attempts. The disadvantage of this is that if any of the
+failing addresses are incorrect, correcting them in the file has no effect on
+pre-existing messages.
+
+The original top-level address is remembered with each of the generated
+addresses, and is output in any log messages. However, any intermediate parent
+addresses are not recorded. This makes a difference to the log only if the 
+all_parents selector is set, but for mailing lists there is normally only one
+level of expansion anyway.
+
+
+49.5 Closed mailing lists
+-------------------------
+
+The examples so far have assumed open mailing lists, to which anybody may send
+mail. It is also possible to set up closed lists, where mail is accepted from
+specified senders only. This is done by making use of the generic senders
+option to restrict the router that handles the list.
+
+The following example uses the same file as a list of recipients and as a list
+of permitted senders. It requires three routers:
+
+lists_request:
+  driver = redirect
+  domains = lists.example
+  local_part_suffix = -request
+  file = /usr/lists/$local_part$local_part_suffix
+  no_more
+
+lists_post:
+  driver = redirect
+  domains = lists.example
+  senders = ${if exists {/usr/lists/$local_part}\
+             {lsearch;/usr/lists/$local_part}{*}}
+  file = /usr/lists/$local_part
+  forbid_pipe
+  forbid_file
+  errors_to = $local_part-request@lists.example
+  no_more
+
+lists_closed:
+  driver = redirect
+  domains = lists.example
+  allow_fail
+  data = :fail: $local_part@lists.example is a closed mailing list
+
+All three routers have the same domains setting, so for any other domains, they
+are all skipped. The first router runs only if the local part ends in -request.
+It handles messages to the list manager(s) by means of an open mailing list.
+
+The second router runs only if the senders precondition is satisfied. It checks
+for the existence of a list that corresponds to the local part, and then checks
+that the sender is on the list by means of a linear search. It is necessary to
+check for the existence of the file before trying to search it, because
+otherwise Exim thinks there is a configuration error. If the file does not
+exist, the expansion of senders is *, which matches all senders. This means
+that the router runs, but because there is no list, declines, and no_more
+ensures that no further routers are run. The address fails with an "unrouteable
+address" error.
+
+The third router runs only if the second router is skipped, which happens when
+a mailing list exists, but the sender is not on it. This router forcibly fails
+the address, giving a suitable error message.
+
+
+49.6 Variable Envelope Return Paths (VERP)
+------------------------------------------
+
+Variable Envelope Return Paths - see http://cr.yp.to/proto/verp.txt - are a way
+of helping mailing list administrators discover which subscription address is
+the cause of a particular delivery failure. The idea is to encode the original
+recipient address in the outgoing envelope sender address, so that if the
+message is forwarded by another host and then subsequently bounces, the
+original recipient can be extracted from the recipient address of the bounce.
+
+Envelope sender addresses can be modified by Exim using two different
+facilities: the errors_to option on a router (as shown in previous mailing list
+examples), or the return_path option on a transport. The second of these is
+effective only if the message is successfully delivered to another host; it is
+not used for errors detected on the local host (see the description of 
+return_path in chapter 24). Here is an example of the use of return_path to
+implement VERP on an smtp transport:
+
+verp_smtp:
+  driver = smtp
+  max_rcpt = 1
+  return_path = \
+    ${if match {$return_path}{^(.+?)-request@your.dom.example\$}\
+      {$1-request+$local_part=$domain@your.dom.example}fail}
+
+This has the effect of rewriting the return path (envelope sender) on outgoing
+SMTP messages, if the local part of the original return path ends in
+"-request", and the domain is your.dom.example. The rewriting inserts the local
+part and domain of the recipient into the return path. Suppose, for example,
+that a message whose return path has been set to 
+somelist-request@your.dom.example is sent to subscriber@other.dom.example. In
+the transport, the return path is rewritten as
+
+somelist-request+subscriber=other.dom.example@your.dom.example
+
+For this to work, you must tell Exim to send multiple copies of messages that
+have more than one recipient, so that each copy has just one recipient. This is
+achieved by setting max_rcpt to 1. Without this, a single copy of a message
+might be sent to several different recipients in the same domain, in which case
+$local_part is not available in the transport, because it is not unique.
+
+Unless your host is doing nothing but mailing list deliveries, you should
+probably use a separate transport for the VERP deliveries, so as not to use
+extra resources in making one-per-recipient copies for other deliveries. This
+can easily be done by expanding the transport option in the router:
+
+dnslookup:
+  driver = dnslookup
+  domains = ! +local_domains
+  transport = \
+    ${if match {$return_path}{^(.+?)-request@your.dom.example\$}\
+      {verp_smtp}{remote_smtp}}
+  no_more
+
+If you want to change the return path using errors_to in a router instead of
+using return_path in the transport, you need to set errors_to on all routers
+that handle mailing list addresses. This will ensure that all delivery errors,
+including those detected on the local host, are sent to the VERP address.
+
+On a host that does no local deliveries and has no manual routing, only the 
+dnslookup router needs to be changed. A special transport is not needed for
+SMTP deliveries. Every mailing list recipient has its own return path value,
+and so Exim must hand them to the transport one at a time. Here is an example
+of a dnslookup router that implements VERP:
+
+verp_dnslookup:
+  driver = dnslookup
+  domains = ! +local_domains
+  transport = remote_smtp
+  errors_to = \
+    ${if match {$return_path}{^(.+?)-request@your.dom.example\$}}
+     {$1-request+$local_part=$domain@your.dom.example}fail}
+  no_more
+
+Before you start sending out messages with VERPed return paths, you must also
+configure Exim to accept the bounce messages that come back to those paths.
+Typically this is done by setting a local_part_suffix option for a router, and
+using this to route the messages to wherever you want to handle them.
+
+The overhead incurred in using VERP depends very much on the size of the
+message, the number of recipient addresses that resolve to the same remote
+host, and the speed of the connection over which the message is being sent. If
+a lot of addresses resolve to the same host and the connection is slow, sending
+a separate copy of the message for each address may take substantially longer
+than sending a single copy with many recipients (for which VERP cannot be
+used).
+
+
+49.7 Virtual domains
+--------------------
+
+The phrase virtual domain is unfortunately used with two rather different
+meanings:
+
+  * A domain for which there are no real mailboxes; all valid local parts are
+    aliases for other email addresses. Common examples are organizational
+    top-level domains and "vanity" domains.
+
+  * One of a number of independent domains that are all handled by the same
+    host, with mailboxes on that host, but where the mailbox owners do not
+    necessarily have login accounts on that host.
+
+The first usage is probably more common, and does seem more "virtual" than the
+second. This kind of domain can be handled in Exim with a straightforward
+aliasing router. One approach is to create a separate alias file for each
+virtual domain. Exim can test for the existence of the alias file to determine
+whether the domain exists. The dsearch lookup type is useful here, leading to a
+router of this form:
+
+virtual:
+  driver = redirect
+  domains = dsearch;/etc/mail/virtual
+  data = ${lookup{$local_part}lsearch{/etc/mail/virtual/$domain}}
+  no_more
+
+The domains option specifies that the router is to be skipped, unless there is
+a file in the /etc/mail/virtual directory whose name is the same as the domain
+that is being processed. When the router runs, it looks up the local part in
+the file to find a new address (or list of addresses). The no_more setting
+ensures that if the lookup fails (leading to data being an empty string), Exim
+gives up on the address without trying any subsequent routers.
+
+This one router can handle all the virtual domains because the alias file names
+follow a fixed pattern. Permissions can be arranged so that appropriate people
+can edit the different alias files. A successful aliasing operation results in
+a new envelope recipient address, which is then routed from scratch.
+
+The other kind of "virtual" domain can also be handled in a straightforward
+way. One approach is to create a file for each domain containing a list of
+valid local parts, and use it in a router like this:
+
+my_domains:
+  driver = accept
+  domains = dsearch;/etc/mail/domains
+  local_parts = lsearch;/etc/mail/domains/$domain
+  transport = my_mailboxes
+
+The address is accepted if there is a file for the domain, and the local part
+can be found in the file. The domains option is used to check for the file's
+existence because domains is tested before the local_parts option (see section
+3.12). You cannot use require_files, because that option is tested after 
+local_parts. The transport is as follows:
+
+my_mailboxes:
+  driver = appendfile
+  file = /var/mail/$domain/$local_part
+  user = mail
+
+This uses a directory of mailboxes for each domain. The user setting is
+required, to specify which uid is to be used for writing to the mailboxes.
+
+The configuration shown here is just one example of how you might support this
+requirement. There are many other ways this kind of configuration can be set
+up, for example, by using a database instead of separate files to hold all the
+information about the domains.
+
+
+49.8 Multiple user mailboxes
+----------------------------
+
+Heavy email users often want to operate with multiple mailboxes, into which
+incoming mail is automatically sorted. A popular way of handling this is to
+allow users to use multiple sender addresses, so that replies can easily be
+identified. Users are permitted to add prefixes or suffixes to their local
+parts for this purpose. The wildcard facility of the generic router options 
+local_part_prefix and local_part_suffix can be used for this. For example,
+consider this router:
+
+userforward:
+  driver = redirect
+  check_local_user
+  file = $home/.forward
+  local_part_suffix = -*
+  local_part_suffix_optional
+  allow_filter
+
+It runs a user's .forward file for all local parts of the form username-*.
+Within the filter file the user can distinguish different cases by testing the
+variable $local_part_suffix. For example:
+
+if $local_part_suffix contains -special then
+save /home/$local_part/Mail/special
+endif
+
+If the filter file does not exist, or does not deal with such addresses, they
+fall through to subsequent routers, and, assuming no subsequent use of the 
+local_part_suffix option is made, they presumably fail. Thus, users have
+control over which suffixes are valid.
+
+Alternatively, a suffix can be used to trigger the use of a different .forward
+file - which is the way a similar facility is implemented in another MTA:
+
+userforward:
+  driver = redirect
+  check_local_user
+  file = $home/.forward$local_part_suffix
+  local_part_suffix = -*
+  local_part_suffix_optional
+  allow_filter
+
+If there is no suffix, .forward is used; if the suffix is -special, for
+example, .forward-special is used. Once again, if the appropriate file does not
+exist, or does not deal with the address, it is passed on to subsequent
+routers, which could, if required, look for an unqualified .forward file to use
+as a default.
+
+
+49.9 Simplified vacation processing
+-----------------------------------
+
+The traditional way of running the vacation program is for a user to set up a
+pipe command in a .forward file (see section 22.6 for syntax details). This is
+prone to error by inexperienced users. There are two features of Exim that can
+be used to make this process simpler for users:
+
+  * A local part prefix such as "vacation-" can be specified on a router which
+    can cause the message to be delivered directly to the vacation program, or
+    alternatively can use Exim's autoreply transport. The contents of a user's
+    .forward file are then much simpler. For example:
+
+    spqr, vacation-spqr
+
+  * The require_files generic router option can be used to trigger a vacation
+    delivery by checking for the existence of a certain file in the user's home
+    directory. The unseen generic option should also be used, to ensure that
+    the original delivery also proceeds. In this case, all the user has to do
+    is to create a file called, say, .vacation, containing a vacation message.
+
+Another advantage of both these methods is that they both work even when the
+use of arbitrary pipes by users is locked out.
+
+
+49.10 Taking copies of mail
+---------------------------
+
+Some installations have policies that require archive copies of all messages to
+be made. A single copy of each message can easily be taken by an appropriate
+command in a system filter, which could, for example, use a different file for
+each day's messages.
+
+There is also a shadow transport mechanism that can be used to take copies of
+messages that are successfully delivered by local transports, one copy per
+delivery. This could be used, inter alia, to implement automatic notification
+of delivery by sites that insist on doing such things.
+
+
+49.11 Intermittently connected hosts
+------------------------------------
+
+It has become quite common (because it is cheaper) for hosts to connect to the
+Internet periodically rather than remain connected all the time. The normal
+arrangement is that mail for such hosts accumulates on a system that is
+permanently connected.
+
+Exim was designed for use on permanently connected hosts, and so it is not
+particularly well-suited to use in an intermittently connected environment.
+Nevertheless there are some features that can be used.
+
+
+49.12 Exim on the upstream server host
+--------------------------------------
+
+It is tempting to arrange for incoming mail for the intermittently connected
+host to remain on Exim's queue until the client connects. However, this
+approach does not scale very well. Two different kinds of waiting message are
+being mixed up in the same queue - those that cannot be delivered because of
+some temporary problem, and those that are waiting for their destination host
+to connect. This makes it hard to manage the queue, as well as wasting
+resources, because each queue runner scans the entire queue.
+
+A better approach is to separate off those messages that are waiting for an
+intermittently connected host. This can be done by delivering these messages
+into local files in batch SMTP, "mailstore", or other envelope-preserving
+format, from where they are transmitted by other software when their
+destination connects. This makes it easy to collect all the mail for one host
+in a single directory, and to apply local timeout rules on a per-message basis
+if required.
+
+On a very small scale, leaving the mail on Exim's queue can be made to work. If
+you are doing this, you should configure Exim with a long retry period for the
+intermittent host. For example:
+
+cheshire.wonderland.fict.example    *   F,5d,24h
+
+This stops a lot of failed delivery attempts from occurring, but Exim remembers
+which messages it has queued up for that host. Once the intermittent host comes
+online, forcing delivery of one message (either by using the -M or -R options,
+or by using the ETRN SMTP command (see section 47.8) causes all the queued up
+messages to be delivered, often down a single SMTP connection. While the host
+remains connected, any new messages get delivered immediately.
+
+If the connecting hosts do not have fixed IP addresses, that is, if a host is
+issued with a different IP address each time it connects, Exim's retry
+mechanisms on the holding host get confused, because the IP address is normally
+used as part of the key string for holding retry information. This can be
+avoided by unsetting retry_include_ip_address on the smtp transport. Since this
+has disadvantages for permanently connected hosts, it is best to arrange a
+separate transport for the intermittently connected ones.
+
+
+49.13 Exim on the intermittently connected client host
+------------------------------------------------------
+
+The value of smtp_accept_queue_per_connection should probably be increased, or
+even set to zero (that is, disabled) on the intermittently connected host, so
+that all incoming messages down a single connection get delivered immediately.
+
+Mail waiting to be sent from an intermittently connected host will probably not
+have been routed, because without a connection DNS lookups are not possible.
+This means that if a normal queue run is done at connection time, each message
+is likely to be sent in a separate SMTP session. This can be avoided by
+starting the queue run with a command line option beginning with -qq instead of
+-q. In this case, the queue is scanned twice. In the first pass, routing is
+done but no deliveries take place. The second pass is a normal queue run; since
+all the messages have been previously routed, those destined for the same host
+are likely to get sent as multiple deliveries in a single SMTP connection.
+
+
+
+===============================================================================
+50. USING EXIM AS A NON-QUEUEING CLIENT
+
+On a personal computer, it is a common requirement for all email to be sent to
+a "smart host". There are plenty of MUAs that can be configured to operate that
+way, for all the popular operating systems. However, there are some MUAs for
+Unix-like systems that cannot be so configured: they submit messages using the
+command line interface of /usr/sbin/sendmail. Furthermore, utility programs
+such as cron submit messages this way.
+
+If the personal computer runs continuously, there is no problem, because it can
+run a conventional MTA that handles delivery to the smart host, and deal with
+any delays via its queueing mechanism. However, if the computer does not run
+continuously or runs different operating systems at different times, queueing
+email is not desirable.
+
+There is therefore a requirement for something that can provide the /usr/sbin/
+sendmail interface but deliver messages to a smart host without any queueing or
+retrying facilities. Furthermore, the delivery to the smart host should be
+synchronous, so that if it fails, the sending MUA is immediately informed. In
+other words, we want something that extends an MUA that submits to a local MTA
+via the command line so that it behaves like one that submits to a remote smart
+host using TCP/SMTP.
+
+There are a number of applications (for example, there is one called ssmtp)
+that do this job. However, people have found them to be lacking in various
+ways. For instance, you might want to allow aliasing and forwarding to be done
+before sending a message to the smart host.
+
+Exim already had the necessary infrastructure for doing this job. Just a few
+tweaks were needed to make it behave as required, though it is somewhat of an
+overkill to use a fully-featured MTA for this purpose.
+
+There is a Boolean global option called mua_wrapper, defaulting false. Setting 
+mua_wrapper true causes Exim to run in a special mode where it assumes that it
+is being used to "wrap" a command-line MUA in the manner just described. As
+well as setting mua_wrapper, you also need to provide a compatible router and
+transport configuration. Typically there will be just one router and one
+transport, sending everything to a smart host.
+
+When run in MUA wrapping mode, the behaviour of Exim changes in the following
+ways:
+
+  * A daemon cannot be run, nor will Exim accept incoming messages from inetd.
+    In other words, the only way to submit messages is via the command line.
+
+  * Each message is synchronously delivered as soon as it is received (-odi is
+    assumed). All queueing options (queue_only, queue_smtp_domains, control in
+    an ACL, etc.) are quietly ignored. The Exim reception process does not
+    finish until the delivery attempt is complete. If the delivery is
+    successful, a zero return code is given.
+
+  * Address redirection is permitted, but the final routing for all addresses
+    must be to the same remote transport, and to the same list of hosts.
+    Furthermore, the return address (envelope sender) must be the same for all
+    recipients, as must any added or deleted header lines. In other words, it
+    must be possible to deliver the message in a single SMTP transaction,
+    however many recipients there are.
+
+  * If these conditions are not met, or if routing any address results in a
+    failure or defer status, or if Exim is unable to deliver all the recipients
+    successfully to one of the smart hosts, delivery of the entire message
+    fails.
+
+  * Because no queueing is allowed, all failures are treated as permanent;
+    there is no distinction between 4xx and 5xx SMTP response codes from the
+    smart host. Furthermore, because only a single yes/no response can be given
+    to the caller, it is not possible to deliver to some recipients and not
+    others. If there is an error (temporary or permanent) for any recipient,
+    all are failed.
+
+  * If more than one smart host is listed, Exim will try another host after a
+    connection failure or a timeout, in the normal way. However, if this kind
+    of failure happens for all the hosts, the delivery fails.
+
+  * When delivery fails, an error message is written to the standard error
+    stream (as well as to Exim's log), and Exim exits to the caller with a
+    return code value 1. The message is expunged from Exim's spool files. No
+    bounce messages are ever generated.
+
+  * No retry data is maintained, and any retry rules are ignored.
+
+  * A number of Exim options are overridden: deliver_drop_privilege is forced
+    true, max_rcpt in the smtp transport is forced to "unlimited", 
+    remote_max_parallel is forced to one, and fallback hosts are ignored.
+
+The overall effect is that Exim makes a single synchronous attempt to deliver
+the message, failing if there is any kind of problem. Because no local
+deliveries are done and no daemon can be run, Exim does not need root
+privilege. It should be possible to run it setuid to exim instead of setuid to 
+root. See section 54.3 for a general discussion about the advantages and
+disadvantages of running without root privilege.
+
+
+
+===============================================================================
+51. LOG FILES
+
+Exim writes three different logs, referred to as the main log, the reject log,
+and the panic log:
+
+  * The main log records the arrival of each message and each delivery in a
+    single line in each case. The format is as compact as possible, in an
+    attempt to keep down the size of log files. Two-character flag sequences
+    make it easy to pick out these lines. A number of other events are recorded
+    in the main log. Some of them are optional, in which case the log_selector
+    option controls whether they are included or not. A Perl script called 
+    eximstats, which does simple analysis of main log files, is provided in the
+    Exim distribution (see section 52.7).
+
+  * The reject log records information from messages that are rejected as a
+    result of a configuration option (that is, for policy reasons). The first
+    line of each rejection is a copy of the line that is also written to the
+    main log. Then, if the message's header has been read at the time the log
+    is written, its contents are written to this log. Only the original header
+    lines are available; header lines added by ACLs are not logged. You can use
+    the reject log to check that your policy controls are working correctly; on
+    a busy host this may be easier than scanning the main log for rejection
+    messages. You can suppress the writing of the reject log by setting 
+    write_rejectlog false.
+
+  * When certain serious errors occur, Exim writes entries to its panic log. If
+    the error is sufficiently disastrous, Exim bombs out afterwards. Panic log
+    entries are usually written to the main log as well, but can get lost amid
+    the mass of other entries. The panic log should be empty under normal
+    circumstances. It is therefore a good idea to check it (or to have a cron
+    script check it) regularly, in order to become aware of any problems. When
+    Exim cannot open its panic log, it tries as a last resort to write to the
+    system log (syslog). This is opened with LOG_PID+LOG_CONS and the facility
+    code of LOG_MAIL. The message itself is written at priority LOG_CRIT.
+
+Every log line starts with a timestamp, in the format shown in the following
+example. Note that many of the examples shown in this chapter are line-wrapped.
+In the log file, this would be all on one line:
+
+2001-09-16 16:09:47 SMTP connection from [127.0.0.1] closed
+  by QUIT
+
+By default, the timestamps are in the local timezone. There are two ways of
+changing this:
+
+  * You can set the timezone option to a different time zone; in particular, if
+    you set
+
+    timezone = UTC
+
+    the timestamps will be in UTC (aka GMT).
+
+  * If you set log_timezone true, the time zone is added to the timestamp, for
+    example:
+
+    2003-04-25 11:17:07 +0100 Start queue run: pid=12762
+
+Exim does not include its process id in log lines by default, but you can
+request that it does so by specifying the "pid" log selector (see section 51.15
+). When this is set, the process id is output, in square brackets, immediately
+after the time and date.
+
+
+51.1 Where the logs are written
+-------------------------------
+
+The logs may be written to local files, or to syslog, or both. However, it
+should be noted that many syslog implementations use UDP as a transport, and
+are therefore unreliable in the sense that messages are not guaranteed to
+arrive at the loghost, nor is the ordering of messages necessarily maintained.
+It has also been reported that on large log files (tens of megabytes) you may
+need to tweak syslog to prevent it syncing the file with each write - on Linux
+this has been seen to make syslog take 90% plus of CPU time.
+
+The destination for Exim's logs is configured by setting LOG_FILE_PATH in Local
+/Makefile or by setting log_file_path in the run time configuration. This
+latter string is expanded, so it can contain, for example, references to the
+host name:
+
+log_file_path = /var/log/$primary_hostname/exim_%slog
+
+It is generally advisable, however, to set the string in Local/Makefile rather
+than at run time, because then the setting is available right from the start of
+Exim's execution. Otherwise, if there's something it wants to log before it has
+read the configuration file (for example, an error in the configuration file)
+it will not use the path you want, and may not be able to log at all.
+
+The value of LOG_FILE_PATH or log_file_path is a colon-separated list,
+currently limited to at most two items. This is one option where the facility
+for changing a list separator may not be used. The list must always be
+colon-separated. If an item in the list is "syslog" then syslog is used;
+otherwise the item must either be an absolute path, containing "%s" at the
+point where "main", "reject", or "panic" is to be inserted, or be empty,
+implying the use of a default path.
+
+When Exim encounters an empty item in the list, it searches the list defined by
+LOG_FILE_PATH, and uses the first item it finds that is neither empty nor
+"syslog". This means that an empty item in log_file_path can be used to mean
+"use the path specified at build time". It no such item exists, log files are
+written in the log subdirectory of the spool directory. This is equivalent to
+the setting:
+
+log_file_path = $spool_directory/log/%slog
+
+If you do not specify anything at build time or run time, that is where the
+logs are written.
+
+A log file path may also contain "%D" or "%M" if datestamped log file names are
+in use - see section 51.3 below.
+
+Here are some examples of possible settings:
+
+LOG_FILE_PATH=syslog                     syslog only
+LOG_FILE_PATH=:syslog                    syslog and default path
+LOG_FILE_PATH=syslog : /usr/log/exim_%s  syslog and specified path
+LOG_FILE_PATH=/usr/log/exim_%s           specified path only
+
+If there are more than two paths in the list, the first is used and a panic
+error is logged.
+
+
+51.2 Logging to local files that are periodically "cycled"
+----------------------------------------------------------
+
+Some operating systems provide centralized and standardized methods for cycling
+log files. For those that do not, a utility script called exicyclog is provided
+(see section 52.6). This renames and compresses the main and reject logs each
+time it is called. The maximum number of old logs to keep can be set. It is
+suggested this script is run as a daily cron job.
+
+An Exim delivery process opens the main log when it first needs to write to it,
+and it keeps the file open in case subsequent entries are required - for
+example, if a number of different deliveries are being done for the same
+message. However, remote SMTP deliveries can take a long time, and this means
+that the file may be kept open long after it is renamed if exicyclog or
+something similar is being used to rename log files on a regular basis. To
+ensure that a switch of log files is noticed as soon as possible, Exim calls
+stat() on the main log's name before reusing an open file, and if the file does
+not exist, or its inode has changed, the old file is closed and Exim tries to
+open the main log from scratch. Thus, an old log file may remain open for quite
+some time, but no Exim processes should write to it once it has been renamed.
+
+
+51.3 Datestamped log files
+--------------------------
+
+Instead of cycling the main and reject log files by renaming them periodically,
+some sites like to use files whose names contain a datestamp, for example,
+mainlog-20031225. The datestamp is in the form yyyymmdd or yyyymm. Exim has
+support for this way of working. It is enabled by setting the log_file_path
+option to a path that includes "%D" or "%M" at the point where the datestamp is
+required. For example:
+
+log_file_path = /var/spool/exim/log/%slog-%D
+log_file_path = /var/log/exim-%s-%D.log
+log_file_path = /var/spool/exim/log/%D-%slog
+log_file_path = /var/log/exim/%s.%M
+
+As before, "%s" is replaced by "main" or "reject"; the following are examples
+of names generated by the above examples:
+
+/var/spool/exim/log/mainlog-20021225
+/var/log/exim-reject-20021225.log
+/var/spool/exim/log/20021225-mainlog
+/var/log/exim/main.200212
+
+When this form of log file is specified, Exim automatically switches to new
+files at midnight. It does not make any attempt to compress old logs; you will
+need to write your own script if you require this. You should not run exicyclog
+with this form of logging.
+
+The location of the panic log is also determined by log_file_path, but it is
+not datestamped, because rotation of the panic log does not make sense. When
+generating the name of the panic log, "%D" or "%M" are removed from the string.
+In addition, if it immediately follows a slash, a following non-alphanumeric
+character is removed; otherwise a preceding non-alphanumeric character is
+removed. Thus, the four examples above would give these panic log names:
+
+/var/spool/exim/log/paniclog
+/var/log/exim-panic.log
+/var/spool/exim/log/paniclog
+/var/log/exim/panic
+
+
+51.4 Logging to syslog
+----------------------
+
+The use of syslog does not change what Exim logs or the format of its messages,
+except in one respect. If syslog_timestamp is set false, the timestamps on
+Exim's log lines are omitted when these lines are sent to syslog. Apart from
+that, the same strings are written to syslog as to log files. The syslog
+"facility" is set to LOG_MAIL, and the program name to "exim" by default, but
+you can change these by setting the syslog_facility and syslog_processname
+options, respectively. If Exim was compiled with SYSLOG_LOG_PID set in Local/
+Makefile (this is the default in src/EDITME), then, on systems that permit it
+(all except ULTRIX), the LOG_PID flag is set so that the syslog() call adds the
+pid as well as the time and host name to each line. The three log streams are
+mapped onto syslog priorities as follows:
+
+  * mainlog is mapped to LOG_INFO
+
+  * rejectlog is mapped to LOG_NOTICE
+
+  * paniclog is mapped to LOG_ALERT
+
+Many log lines are written to both mainlog and rejectlog, and some are written
+to both mainlog and paniclog, so there will be duplicates if these are routed
+by syslog to the same place. You can suppress this duplication by setting 
+syslog_duplication false.
+
+Exim's log lines can sometimes be very long, and some of its rejectlog entries
+contain multiple lines when headers are included. To cope with both these
+cases, entries written to syslog are split into separate syslog() calls at each
+internal newline, and also after a maximum of 870 data characters. (This allows
+for a total syslog line length of 1024, when additions such as timestamps are
+added.) If you are running a syslog replacement that can handle lines longer
+than the 1024 characters allowed by RFC 3164, you should set
+
+SYSLOG_LONG_LINES=yes
+
+in Local/Makefile before building Exim. That stops Exim from splitting long
+lines, but it still splits at internal newlines in reject log entries.
+
+To make it easy to re-assemble split lines later, each component of a split
+entry starts with a string of the form [<n>/<m>] or [<n>\<m>] where <n> is the
+component number and <m> is the total number of components in the entry. The /
+delimiter is used when the line was split because it was too long; if it was
+split because of an internal newline, the \ delimiter is used. For example,
+supposing the length limit to be 50 instead of 870, the following would be the
+result of a typical rejection message to mainlog (LOG_INFO), each line in
+addition being preceded by the time, host name, and pid as added by syslog:
+
+[1/5] 2002-09-16 16:09:43 16RdAL-0006pc-00 rejected from
+[2/5]  [127.0.0.1] (ph10): syntax error in 'From' header
+[3/5]  when scanning for sender: missing or malformed lo
+[4/5] cal part in "<>" (envelope sender is <ph10@cam.exa
+[5/5] mple>)
+
+The same error might cause the following lines to be written to "rejectlog"
+(LOG_NOTICE):
+
+[1/18] 2002-09-16 16:09:43 16RdAL-0006pc-00 rejected fro
+[2/18] m [127.0.0.1] (ph10): syntax error in 'From' head
+[3/18] er when scanning for sender: missing or malformed
+[4/18]  local part in "<>" (envelope sender is <ph10@cam
+[5\18] .example>)
+[6\18] Recipients: ph10@some.domain.cam.example
+[7\18] P Received: from [127.0.0.1] (ident=ph10)
+[8\18]        by xxxxx.cam.example with smtp (Exim 4.00)
+[9\18]        id 16RdAL-0006pc-00
+[10/18]        for ph10@cam.example; Mon, 16 Sep 2002 16:
+[11\18] 09:43 +0100
+[12\18] F From: <>
+[13\18]   Subject: this is a test header
+[18\18]   X-something: this is another header
+[15/18] I Message-Id: <E16RdAL-0006pc-00@xxxxx.cam.examp
+[16\18] le>
+[17\18] B Bcc:
+[18/18]   Date: Mon, 16 Sep 2002 16:09:43 +0100
+
+Log lines that are neither too long nor contain newlines are written to syslog
+without modification.
+
+If only syslog is being used, the Exim monitor is unable to provide a log tail
+display, unless syslog is routing mainlog to a file on the local host and the
+environment variable EXIMON_LOG_FILE_PATH is set to tell the monitor where it
+is.
+
+
+51.5 Log line flags
+-------------------
+
+One line is written to the main log for each message received, and for each
+successful, unsuccessful, and delayed delivery. These lines can readily be
+picked out by the distinctive two-character flags that immediately follow the
+timestamp. The flags are:
+
+<=     message arrival
+=>     normal message delivery
+->     additional address in same delivery
+>>     cutthrough message delivery
+*>     delivery suppressed by -N
+**     delivery failed; address bounced
+==     delivery deferred; temporary problem
+
+
+51.6 Logging message reception
+------------------------------
+
+The format of the single-line entry in the main log that is written for every
+message received is shown in the basic example below, which is split over
+several lines in order to fit it on the page:
+
+2002-10-31 08:57:53 16ZCW1-0005MB-00 <= kryten@dwarf.fict.example
+  H=mailer.fict.example [192.168.123.123] U=exim
+  P=smtp S=5678 id=<incoming message id>
+
+The address immediately following "<=" is the envelope sender address. A bounce
+message is shown with the sender address "<>", and if it is locally generated,
+this is followed by an item of the form
+
+R=<message id>
+
+which is a reference to the message that caused the bounce to be sent.
+
+For messages from other hosts, the H and U fields identify the remote host and
+record the RFC 1413 identity of the user that sent the message, if one was
+received. The number given in square brackets is the IP address of the sending
+host. If there is a single, unparenthesized host name in the H field, as above,
+it has been verified to correspond to the IP address (see the host_lookup
+option). If the name is in parentheses, it was the name quoted by the remote
+host in the SMTP HELO or EHLO command, and has not been verified. If
+verification yields a different name to that given for HELO or EHLO, the
+verified name appears first, followed by the HELO or EHLO name in parentheses.
+
+Misconfigured hosts (and mail forgers) sometimes put an IP address, with or
+without brackets, in the HELO or EHLO command, leading to entries in the log
+containing text like these examples:
+
+H=(10.21.32.43) [192.168.8.34]
+H=([10.21.32.43]) [192.168.8.34]
+
+This can be confusing. Only the final address in square brackets can be relied
+on.
+
+For locally generated messages (that is, messages not received over TCP/IP),
+the H field is omitted, and the U field contains the login name of the caller
+of Exim.
+
+For all messages, the P field specifies the protocol used to receive the
+message. This is the value that is stored in $received_protocol. In the case of
+incoming SMTP messages, the value indicates whether or not any SMTP extensions
+(ESMTP), encryption, or authentication were used. If the SMTP session was
+encrypted, there is an additional X field that records the cipher suite that
+was used.
+
+The protocol is set to "esmtpsa" or "esmtpa" for messages received from hosts
+that have authenticated themselves using the SMTP AUTH command. The first value
+is used when the SMTP connection was encrypted ("secure"). In this case there
+is an additional item A= followed by the name of the authenticator that was
+used. If an authenticated identification was set up by the authenticator's 
+server_set_id option, this is logged too, separated by a colon from the
+authenticator name.
+
+The id field records the existing message id, if present. The size of the
+received message is given by the S field. When the message is delivered,
+headers may be removed or added, so that the size of delivered copies of the
+message may not correspond with this value (and indeed may be different to each
+other).
+
+The log_selector option can be used to request the logging of additional data
+when a message is received. See section 51.15 below.
+
+
+51.7 Logging deliveries
+-----------------------
+
+The format of the single-line entry in the main log that is written for every
+delivery is shown in one of the examples below, for local and remote
+deliveries, respectively. Each example has been split into two lines in order
+to fit it on the page:
+
+2002-10-31 08:59:13 16ZCW1-0005MB-00 => marv
+  <marv@hitch.fict.example> R=localuser T=local_delivery
+2002-10-31 09:00:10 16ZCW1-0005MB-00 =>
+  monk@holistic.fict.example R=dnslookup T=remote_smtp
+  H=holistic.fict.example [192.168.234.234]
+
+For ordinary local deliveries, the original address is given in angle brackets
+after the final delivery address, which might be a pipe or a file. If
+intermediate address(es) exist between the original and the final address, the
+last of these is given in parentheses after the final address. The R and T
+fields record the router and transport that were used to process the address.
+
+If SMTP AUTH was used for the delivery there is an additional item A= followed
+by the name of the authenticator that was used. If an authenticated
+identification was set up by the authenticator's client_set_id option, this is
+logged too, separated by a colon from the authenticator name.
+
+If a shadow transport was run after a successful local delivery, the log line
+for the successful delivery has an item added on the end, of the form
+
+ST=<shadow transport name>
+
+If the shadow transport did not succeed, the error message is put in
+parentheses afterwards.
+
+When more than one address is included in a single delivery (for example, two
+SMTP RCPT commands in one transaction) the second and subsequent addresses are
+flagged with "->" instead of "=>". When two or more messages are delivered down
+a single SMTP connection, an asterisk follows the IP address in the log lines
+for the second and subsequent messages.
+
+When delivery is done in cutthrough mode it is flagged with ">>" and the log
+line precedes the reception line, since cutthrough waits for a possible
+rejection from the destination in case it can reject the sourced item.
+
+The generation of a reply message by a filter file gets logged as a "delivery"
+to the addressee, preceded by ">".
+
+The log_selector option can be used to request the logging of additional data
+when a message is delivered. See section 51.15 below.
+
+
+51.8 Discarded deliveries
+-------------------------
+
+When a message is discarded as a result of the command "seen finish" being
+obeyed in a filter file which generates no deliveries, a log entry of the form
+
+2002-12-10 00:50:49 16auJc-0001UB-00 => discarded
+  <low.club@bridge.example> R=userforward
+
+is written, to record why no deliveries are logged. When a message is discarded
+because it is aliased to ":blackhole:" the log line is like this:
+
+1999-03-02 09:44:33 10HmaX-0005vi-00 => :blackhole:
+  <hole@nowhere.example> R=blackhole_router
+
+
+51.9 Deferred deliveries
+------------------------
+
+When a delivery is deferred, a line of the following form is logged:
+
+2002-12-19 16:20:23 16aiQz-0002Q5-00 == marvin@endrest.example
+  R=dnslookup T=smtp defer (146): Connection refused
+
+In the case of remote deliveries, the error is the one that was given for the
+last IP address that was tried. Details of individual SMTP failures are also
+written to the log, so the above line would be preceded by something like
+
+2002-12-19 16:20:23 16aiQz-0002Q5-00 Failed to connect to
+  mail1.endrest.example [192.168.239.239]: Connection refused
+
+When a deferred address is skipped because its retry time has not been reached,
+a message is written to the log, but this can be suppressed by setting an
+appropriate value in log_selector.
+
+
+51.10 Delivery failures
+-----------------------
+
+If a delivery fails because an address cannot be routed, a line of the
+following form is logged:
+
+1995-12-19 16:20:23 0tRiQz-0002Q5-00 ** jim@trek99.example
+  <jim@trek99.example>: unknown mail domain
+
+If a delivery fails at transport time, the router and transport are shown, and
+the response from the remote host is included, as in this example:
+
+2002-07-11 07:14:17 17SXDU-000189-00 ** ace400@pb.example
+  R=dnslookup T=remote_smtp: SMTP error from remote mailer
+  after pipelined RCPT TO:<ace400@pb.example>: host
+  pbmail3.py.example [192.168.63.111]: 553 5.3.0
+  <ace400@pb.example>...Addressee unknown
+
+The word "pipelined" indicates that the SMTP PIPELINING extension was being
+used. See hosts_avoid_esmtp in the smtp transport for a way of disabling
+PIPELINING. The log lines for all forms of delivery failure are flagged with
+"**".
+
+
+51.11 Fake deliveries
+---------------------
+
+If a delivery does not actually take place because the -N option has been used
+to suppress it, a normal delivery line is written to the log, except that "=>"
+is replaced by "*>".
+
+
+51.12 Completion
+----------------
+
+A line of the form
+
+2002-10-31 09:00:11 16ZCW1-0005MB-00 Completed
+
+is written to the main log when a message is about to be removed from the spool
+at the end of its processing.
+
+
+51.13 Summary of Fields in Log Lines
+------------------------------------
+
+A summary of the field identifiers that are used in log lines is shown in the
+following table:
+
+A           authenticator name (and optional id and sender)
+C           SMTP confirmation on delivery
+            command list for "no mail in SMTP session"
+CV          certificate verification status
+D           duration of "no mail in SMTP session"
+DN          distinguished name from peer certificate
+DT          on => lines: time taken for a delivery
+F           sender address (on delivery lines)
+H           host name and IP address
+I           local interface used
+id          message id for incoming message
+P           on <= lines: protocol used
+            on => and ** lines: return path
+QT          on => lines: time spent on queue so far
+            on "Completed" lines: time spent on queue
+R           on <= lines: reference for local bounce
+            on =>  ** and == lines: router name
+S           size of message
+SNI         server name indication from TLS client hello
+ST          shadow transport name
+T           on <= lines: message subject (topic)
+            on => ** and == lines: transport name
+U           local user or RFC 1413 identity
+X           TLS cipher suite
+
+
+51.14 Other log entries
+-----------------------
+
+Various other types of log entry are written from time to time. Most should be
+self-explanatory. Among the more common are:
+
+  * retry time not reached  An address previously suffered a temporary error
+    during routing or local delivery, and the time to retry has not yet
+    arrived. This message is not written to an individual message log file
+    unless it happens during the first delivery attempt.
+
+  * retry time not reached for any host  An address previously suffered
+    temporary errors during remote delivery, and the retry time has not yet
+    arrived for any of the hosts to which it is routed.
+
+  * spool file locked  An attempt to deliver a message cannot proceed because
+    some other Exim process is already working on the message. This can be
+    quite common if queue running processes are started at frequent intervals.
+    The exiwhat utility script can be used to find out what Exim processes are
+    doing.
+
+  * error ignored  There are several circumstances that give rise to this
+    message:
+
+     1. Exim failed to deliver a bounce message whose age was greater than 
+        ignore_bounce_errors_after. The bounce was discarded.
+
+     2. A filter file set up a delivery using the "noerror" option, and the
+        delivery failed. The delivery was discarded.
+
+     3. A delivery set up by a router configured with
+
+            errors_to = <>
+
+        failed. The delivery was discarded.
+
+
+51.15 Reducing or increasing what is logged
+-------------------------------------------
+
+By setting the log_selector global option, you can disable some of Exim's
+default logging, or you can request additional logging. The value of 
+log_selector is made up of names preceded by plus or minus characters. For
+example:
+
+log_selector = +arguments -retry_defer
+
+The list of optional log items is in the following table, with the default
+selection marked by asterisks:
+
+ 8bitmime                     received 8BITMIME status
+*acl_warn_skipped             skipped warn statement in ACL
+ address_rewrite              address rewriting
+ all_parents                  all parents in => lines
+ arguments                    command line arguments
+*connection_reject            connection rejections
+*delay_delivery               immediate delivery delayed
+ deliver_time                 time taken to perform delivery
+ delivery_size                add S=nnn to => lines
+*dnslist_defer                defers of DNS list (aka RBL) lookups
+*etrn                         ETRN commands
+*host_lookup_failed           as it says
+ ident_timeout                timeout for ident connection
+ incoming_interface           incoming interface on <= lines
+ incoming_port                incoming port on <= lines
+*lost_incoming_connection     as it says (includes timeouts)
+ outgoing_port                add remote port to => lines
+*queue_run                    start and end queue runs
+ queue_time                   time on queue for one recipient
+ queue_time_overall           time on queue for whole message
+ pid                          Exim process id
+ received_recipients          recipients on <= lines
+ received_sender              sender on <= lines
+*rejected_header              header contents on reject log
+*retry_defer                  "retry time not reached"
+ return_path_on_delivery      put return path on => and ** lines
+ sender_on_delivery           add sender to => lines
+*sender_verify_fail           sender verification failures
+*size_reject                  rejection because too big
+*skip_delivery                delivery skipped in a queue run
+*smtp_confirmation            SMTP confirmation on => lines
+ smtp_connection              SMTP connections
+ smtp_incomplete_transaction  incomplete SMTP transactions
+ smtp_mailauth                AUTH argument to MAIL commands
+ smtp_no_mail                 session with no MAIL commands
+ smtp_protocol_error          SMTP protocol errors
+ smtp_syntax_error            SMTP syntax errors
+ subject                      contents of Subject: on <= lines
+ tls_certificate_verified     certificate verification status
+*tls_cipher                   TLS cipher suite on <= and => lines
+ tls_peerdn                   TLS peer DN on <= and => lines
+ tls_sni                      TLS SNI on <= lines
+ unknown_in_list              DNS lookup failed in list match
+
+ all                          all of the above
+
+More details on each of these items follows:
+
+  * 8bitmime: This causes Exim to log any 8BITMIME status of received messages,
+    which may help in tracking down interoperability issues with ancient MTAs
+    that are not 8bit clean. This is added to the "<=" line, tagged with "M8S="
+    and a value of "0", "7" or "8", corresponding to "not given", "7BIT" and
+    "8BITMIME" respectively.
+
+  * acl_warn_skipped: When an ACL warn statement is skipped because one of its
+    conditions cannot be evaluated, a log line to this effect is written if
+    this log selector is set.
+
+  * address_rewrite: This applies both to global rewrites and per-transport
+    rewrites, but not to rewrites in filters run as an unprivileged user
+    (because such users cannot access the log).
+
+  * all_parents: Normally only the original and final addresses are logged on
+    delivery lines; with this selector, intermediate parents are given in
+    parentheses between them.
+
+  * arguments: This causes Exim to write the arguments with which it was called
+    to the main log, preceded by the current working directory. This is a
+    debugging feature, added to make it easier to find out how certain MUAs
+    call /usr/sbin/sendmail. The logging does not happen if Exim has given up
+    root privilege because it was called with the -C or -D options. Arguments
+    that are empty or that contain white space are quoted. Non-printing
+    characters are shown as escape sequences. This facility cannot log
+    unrecognized arguments, because the arguments are checked before the
+    configuration file is read. The only way to log such cases is to interpose
+    a script such as util/logargs.sh between the caller and Exim.
+
+  * connection_reject: A log entry is written whenever an incoming SMTP
+    connection is rejected, for whatever reason.
+
+  * delay_delivery: A log entry is written whenever a delivery process is not
+    started for an incoming message because the load is too high or too many
+    messages were received on one connection. Logging does not occur if no
+    delivery process is started because queue_only is set or -odq was used.
+
+  * deliver_time: For each delivery, the amount of real time it has taken to
+    perform the actual delivery is logged as DT=<time>, for example, "DT=1s".
+
+  * delivery_size: For each delivery, the size of message delivered is added to
+    the "=>" line, tagged with S=.
+
+  * dnslist_defer: A log entry is written if an attempt to look up a host in a
+    DNS black list suffers a temporary error.
+
+  * etrn: Every valid ETRN command that is received is logged, before the ACL
+    is run to determine whether or not it is actually accepted. An invalid ETRN
+    command, or one received within a message transaction is not logged by this
+    selector (see smtp_syntax_error and smtp_protocol_error).
+
+  * host_lookup_failed: When a lookup of a host's IP addresses fails to find
+    any addresses, or when a lookup of an IP address fails to find a host name,
+    a log line is written. This logging does not apply to direct DNS lookups
+    when routing email addresses, but it does apply to "byname" lookups.
+
+  * ident_timeout: A log line is written whenever an attempt to connect to a
+    client's ident port times out.
+
+  * incoming_interface: The interface on which a message was received is added
+    to the "<=" line as an IP address in square brackets, tagged by I= and
+    followed by a colon and the port number. The local interface and port are
+    also added to other SMTP log lines, for example "SMTP connection from", and
+    to rejection lines.
+
+  * incoming_port: The remote port number from which a message was received is
+    added to log entries and Received: header lines, following the IP address
+    in square brackets, and separated from it by a colon. This is implemented
+    by changing the value that is put in the $sender_fullhost and
+    $sender_rcvhost variables. Recording the remote port number has become more
+    important with the widening use of NAT (see RFC 2505).
+
+  * lost_incoming_connection: A log line is written when an incoming SMTP
+    connection is unexpectedly dropped.
+
+  * outgoing_port: The remote port number is added to delivery log lines (those
+    containing => tags) following the IP address. This option is not included
+    in the default setting, because for most ordinary configurations, the
+    remote port number is always 25 (the SMTP port).
+
+  * pid: The current process id is added to every log line, in square brackets,
+    immediately after the time and date.
+
+  * queue_run: The start and end of every queue run are logged.
+
+  * queue_time: The amount of time the message has been in the queue on the
+    local host is logged as QT=<time> on delivery ("=>") lines, for example,
+    "QT=3m45s". The clock starts when Exim starts to receive the message, so it
+    includes reception time as well as the delivery time for the current
+    address. This means that it may be longer than the difference between the
+    arrival and delivery log line times, because the arrival log line is not
+    written until the message has been successfully received.
+
+  * queue_time_overall: The amount of time the message has been in the queue on
+    the local host is logged as QT=<time> on "Completed" lines, for example,
+    "QT=3m45s". The clock starts when Exim starts to receive the message, so it
+    includes reception time as well as the total delivery time.
+
+  * received_recipients: The recipients of a message are listed in the main log
+    as soon as the message is received. The list appears at the end of the log
+    line that is written when a message is received, preceded by the word
+    "for". The addresses are listed after they have been qualified, but before
+    any rewriting has taken place. Recipients that were discarded by an ACL for
+    MAIL or RCPT do not appear in the list.
+
+  * received_sender: The unrewritten original sender of a message is added to
+    the end of the log line that records the message's arrival, after the word
+    "from" (before the recipients if received_recipients is also set).
+
+  * rejected_header: If a message's header has been received at the time a
+    rejection is written to the reject log, the complete header is added to the
+    log. Header logging can be turned off individually for messages that are
+    rejected by the local_scan() function (see section 44.2).
+
+  * retry_defer: A log line is written if a delivery is deferred because a
+    retry time has not yet been reached. However, this "retry time not reached"
+    message is always omitted from individual message logs after the first
+    delivery attempt.
+
+  * return_path_on_delivery: The return path that is being transmitted with the
+    message is included in delivery and bounce lines, using the tag P=. This is
+    omitted if no delivery actually happens, for example, if routing fails, or
+    if delivery is to /dev/null or to ":blackhole:".
+
+  * sender_on_delivery: The message's sender address is added to every delivery
+    and bounce line, tagged by F= (for "from"). This is the original sender
+    that was received with the message; it is not necessarily the same as the
+    outgoing return path.
+
+  * sender_verify_fail: If this selector is unset, the separate log line that
+    gives details of a sender verification failure is not written. Log lines
+    for the rejection of SMTP commands contain just "sender verify failed", so
+    some detail is lost.
+
+  * size_reject: A log line is written whenever a message is rejected because
+    it is too big.
+
+  * skip_delivery: A log line is written whenever a message is skipped during a
+    queue run because it is frozen or because another process is already
+    delivering it. The message that is written is "spool file is locked".
+
+  * smtp_confirmation: The response to the final "." in the SMTP or LMTP
+    dialogue for outgoing messages is added to delivery log lines in the form
+    "C="<text>. A number of MTAs (including Exim) return an identifying string
+    in this response.
+
+  * smtp_connection: A log line is written whenever an SMTP connection is
+    established or closed, unless the connection is from a host that matches 
+    hosts_connection_nolog. (In contrast, lost_incoming_connection applies only
+    when the closure is unexpected.) This applies to connections from local
+    processes that use -bs as well as to TCP/IP connections. If a connection is
+    dropped in the middle of a message, a log line is always written, whether
+    or not this selector is set, but otherwise nothing is written at the start
+    and end of connections unless this selector is enabled.
+
+    For TCP/IP connections to an Exim daemon, the current number of connections
+    is included in the log message for each new connection, but note that the
+    count is reset if the daemon is restarted. Also, because connections are
+    closed (and the closure is logged) in subprocesses, the count may not
+    include connections that have been closed but whose termination the daemon
+    has not yet noticed. Thus, while it is possible to match up the opening and
+    closing of connections in the log, the value of the logged counts may not
+    be entirely accurate.
+
+  * smtp_incomplete_transaction: When a mail transaction is aborted by RSET,
+    QUIT, loss of connection, or otherwise, the incident is logged, and the
+    message sender plus any accepted recipients are included in the log line.
+    This can provide evidence of dictionary attacks.
+
+  * smtp_no_mail: A line is written to the main log whenever an accepted SMTP
+    connection terminates without having issued a MAIL command. This includes
+    both the case when the connection is dropped, and the case when QUIT is
+    used. It does not include cases where the connection is rejected right at
+    the start (by an ACL, or because there are too many connections, or
+    whatever). These cases already have their own log lines.
+
+    The log line that is written contains the identity of the client in the
+    usual way, followed by D= and a time, which records the duration of the
+    connection. If the connection was authenticated, this fact is logged
+    exactly as it is for an incoming message, with an A= item. If the
+    connection was encrypted, CV=, DN=, and X= items may appear as they do for
+    an incoming message, controlled by the same logging options.
+
+    Finally, if any SMTP commands were issued during the connection, a C= item
+    is added to the line, listing the commands that were used. For example,
+
+    C=EHLO,QUIT
+
+    shows that the client issued QUIT straight after EHLO. If there were fewer
+    than 20 commands, they are all listed. If there were more than 20 commands,
+    the last 20 are listed, preceded by "...". However, with the default
+    setting of 10 for smtp_accep_max_nonmail, the connection will in any case
+    have been aborted before 20 non-mail commands are processed.
+
+  * smtp_mailauth: A third subfield with the authenticated sender,
+    colon-separated, is appended to the A= item for a message arrival or
+    delivery log line, if an AUTH argument to the SMTP MAIL command (see 33.2)
+    was accepted or used.
+
+  * smtp_protocol_error: A log line is written for every SMTP protocol error
+    encountered. Exim does not have perfect detection of all protocol errors
+    because of transmission delays and the use of pipelining. If PIPELINING has
+    been advertised to a client, an Exim server assumes that the client will
+    use it, and therefore it does not count "expected" errors (for example,
+    RCPT received after rejecting MAIL) as protocol errors.
+
+  * smtp_syntax_error: A log line is written for every SMTP syntax error
+    encountered. An unrecognized command is treated as a syntax error. For an
+    external connection, the host identity is given; for an internal connection
+    using -bs the sender identification (normally the calling user) is given.
+
+  * subject: The subject of the message is added to the arrival log line,
+    preceded by "T=" (T for "topic", since S is already used for "size"). Any
+    MIME "words" in the subject are decoded. The print_topbitchars option
+    specifies whether characters with values greater than 127 should be logged
+    unchanged, or whether they should be rendered as escape sequences.
+
+  * tls_certificate_verified: An extra item is added to <= and => log lines
+    when TLS is in use. The item is "CV=yes" if the peer's certificate was
+    verified, and "CV=no" if not.
+
+  * tls_cipher: When a message is sent or received over an encrypted
+    connection, the cipher suite used is added to the log line, preceded by X=.
+
+  * tls_peerdn: When a message is sent or received over an encrypted
+    connection, and a certificate is supplied by the remote host, the peer DN
+    is added to the log line, preceded by DN=.
+
+  * tls_sni: When a message is received over an encrypted connection, and the
+    remote host provided the Server Name Indication extension, the SNI is added
+    to the log line, preceded by SNI=.
+
+  * unknown_in_list: This setting causes a log entry to be written when the
+    result of a list match is failure because a DNS lookup failed.
+
+
+51.16 Message log
+-----------------
+
+In addition to the general log files, Exim writes a log file for each message
+that it handles. The names of these per-message logs are the message ids, and
+they are kept in the msglog sub-directory of the spool directory. Each message
+log contains copies of the log lines that apply to the message. This makes it
+easier to inspect the status of an individual message without having to search
+the main log. A message log is deleted when processing of the message is
+complete, unless preserve_message_logs is set, but this should be used only
+with great care because they can fill up your disk very quickly.
+
+On a heavily loaded system, it may be desirable to disable the use of
+per-message logs, in order to reduce disk I/O. This can be done by setting the 
+message_logs option false.
+
+
+
+===============================================================================
+52. EXIM UTILITIES
+
+A number of utility scripts and programs are supplied with Exim and are
+described in this chapter. There is also the Exim Monitor, which is covered in
+the next chapter. The utilities described here are:
+
+    52.1  exiwhat          list what Exim processes are doing
+    52.2  exiqgrep         grep the queue
+    52.3  exiqsumm         summarize the queue
+    52.4  exigrep          search the main log
+    52.5  exipick          select messages on various criteria
+    52.6  exicyclog        cycle (rotate) log files
+    52.7  eximstats        extract statistics from the log
+    52.8  exim_checkaccess check address acceptance from given IP
+    52.9  exim_dbmbuild    build a DBM file
+    52.10 exinext          extract retry information
+    52.11 exim_dumpdb      dump a hints database
+    52.11 exim_tidydb      clean up a hints database
+    52.11 exim_fixdb       patch a hints database
+    52.15 exim_lock        lock a mailbox file
+
+Another utility that might be of use to sites with many MTAs is Tom Kistner's 
+exilog. It provides log visualizations across multiple Exim servers. See http:/
+/duncanthrax.net/exilog/ for details.
+
+
+52.1 Finding out what Exim processes are doing (exiwhat)
+--------------------------------------------------------
+
+On operating systems that can restart a system call after receiving a signal
+(most modern OS), an Exim process responds to the SIGUSR1 signal by writing a
+line describing what it is doing to the file exim-process.info in the Exim
+spool directory. The exiwhat script sends the signal to all Exim processes it
+can find, having first emptied the file. It then waits for one second to allow
+the Exim processes to react before displaying the results. In order to run 
+exiwhat successfully you have to have sufficient privilege to send the signal
+to the Exim processes, so it is normally run as root.
+
+Warning: This is not an efficient process. It is intended for occasional use by
+system administrators. It is not sensible, for example, to set up a script that
+sends SIGUSR1 signals to Exim processes at short intervals.
+
+Unfortunately, the ps command that exiwhat uses to find Exim processes varies
+in different operating systems. Not only are different options used, but the
+format of the output is different. For this reason, there are some system
+configuration options that configure exactly how exiwhat works. If it doesn't
+seem to be working for you, check the following compile-time options:
+
+EXIWHAT_PS_CMD     the command for running ps
+EXIWHAT_PS_ARG     the argument for ps
+EXIWHAT_EGREP_ARG  the argument for egrep to select from ps output
+EXIWHAT_KILL_ARG   the argument for the kill command
+
+An example of typical output from exiwhat is
+
+164 daemon: -q1h, listening on port 25
+10483 running queue: waiting for 0tAycK-0002ij-00 (10492)
+10492 delivering 0tAycK-0002ij-00 to mail.ref.example
+  [10.19.42.42] (editor@ref.example)
+10592 handling incoming call from [192.168.243.242]
+10628 accepting a local non-SMTP message
+
+The first number in the output line is the process number. The third line has
+been split here, in order to fit it on the page.
+
+
+52.2 Selective queue listing (exiqgrep)
+---------------------------------------
+
+This utility is a Perl script contributed by Matt Hubbard. It runs
+
+exim -bpu
+
+or (in case -a switch is specified)
+
+exim -bp
+
+The -C option is used to specify an alternate exim.conf which might contain
+alternate exim configuration the queue management might be using.
+
+to obtain a queue listing, and then greps the output to select messages that
+match given criteria. The following selection options are available:
+
+-f <regex>
+
+    Match the sender address using a case-insensitive search. The field that is
+    tested is enclosed in angle brackets, so you can test for bounce messages
+    with
+
+    exiqgrep -f '^<>$'
+
+-r <regex>
+
+    Match a recipient address using a case-insensitve search. The field that is
+    tested is not enclosed in angle brackets.
+
+-s <regex>
+
+    Match against the size field.
+
+-y <seconds>
+
+    Match messages that are younger than the given time.
+
+-o <seconds>
+
+    Match messages that are older than the given time.
+
+-z
+
+    Match only frozen messages.
+
+-x
+
+    Match only non-frozen messages.
+
+The following options control the format of the output:
+
+-c
+
+    Display only the count of matching messages.
+
+-l
+
+    Long format - display the full message information as output by Exim. This
+    is the default.
+
+-i
+
+    Display message ids only.
+
+-b
+
+    Brief format - one line per message.
+
+-R
+
+    Display messages in reverse order.
+
+-a
+
+    Include delivered recipients in queue listing.
+
+There is one more option, -h, which outputs a list of options.
+
+
+52.3 Summarizing the queue (exiqsumm)
+-------------------------------------
+
+The exiqsumm utility is a Perl script which reads the output of "exim -bp" and
+produces a summary of the messages on the queue. Thus, you use it by running a
+command such as
+
+exim -bp | exiqsumm
+
+The output consists of one line for each domain that has messages waiting for
+it, as in the following example:
+
+3   2322   74m   66m  msn.com.example
+
+Each line lists the number of pending deliveries for a domain, their total
+volume, and the length of time that the oldest and the newest messages have
+been waiting. Note that the number of pending deliveries is greater than the
+number of messages when messages have more than one recipient.
+
+A summary line is output at the end. By default the output is sorted on the
+domain name, but exiqsumm has the options -a and -c, which cause the output to
+be sorted by oldest message and by count of messages, respectively. There are
+also three options that split the messages for each domain into two or more
+subcounts: -b separates bounce messages, -f separates frozen messages, and -s
+separates messages according to their sender.
+
+The output of exim -bp contains the original addresses in the message, so this
+also applies to the output from exiqsumm. No domains from addresses generated
+by aliasing or forwarding are included (unless the one_time option of the 
+redirect router has been used to convert them into "top level" addresses).
+
+
+52.4 Extracting specific information from the log (exigrep)
+-----------------------------------------------------------
+
+The exigrep utility is a Perl script that searches one or more main log files
+for entries that match a given pattern. When it finds a match, it extracts all
+the log entries for the relevant message, not just those that match the
+pattern. Thus, exigrep can extract complete log entries for a given message, or
+all mail for a given user, or for a given host, for example. The input files
+can be in Exim log format or syslog format. If a matching log line is not
+associated with a specific message, it is included in exigrep's output without
+any additional lines. The usage is:
+
+exigrep [-t<n>] [-I] [-l] [-v] <pattern> [<log file>] ...
+
+If no log file names are given on the command line, the standard input is read.
+
+The -t argument specifies a number of seconds. It adds an additional condition
+for message selection. Messages that are complete are shown only if they spent
+more than <n> seconds on the queue.
+
+By default, exigrep does case-insensitive matching. The -I option makes it
+case-sensitive. This may give a performance improvement when searching large
+log files. Without -I, the Perl pattern matches use Perl's "/i" option; with -I
+they do not. In both cases it is possible to change the case sensitivity within
+the pattern by using "(?i)" or "(?-i)".
+
+The -l option means "literal", that is, treat all characters in the pattern as
+standing for themselves. Otherwise the pattern must be a Perl regular
+expression.
+
+The -v option inverts the matching condition. That is, a line is selected if it
+does not match the pattern.
+
+If the location of a zcat command is known from the definition of ZCAT_COMMAND
+in Local/Makefile, exigrep automatically passes any file whose name ends in
+COMPRESS_SUFFIX through zcat as it searches it.
+
+
+52.5 Selecting messages by various criteria (exipick)
+-----------------------------------------------------
+
+John Jetmore's exipick utility is included in the Exim distribution. It lists
+messages from the queue according to a variety of criteria. For details of 
+exipick's facilities, visit the web page at http://www.exim.org/eximwiki/
+ToolExipickManPage or run exipick with the --help option.
+
+
+52.6 Cycling log files (exicyclog)
+----------------------------------
+
+The exicyclog script can be used to cycle (rotate) mainlog and rejectlog files.
+This is not necessary if only syslog is being used, or if you are using log
+files with datestamps in their names (see section 51.3). Some operating systems
+have their own standard mechanisms for log cycling, and these can be used
+instead of exicyclog if preferred. There are two command line options for 
+exicyclog:
+
+  * -k <count> specifies the number of log files to keep, overriding the
+    default that is set when Exim is built. The default default is 10.
+
+  * -l <path> specifies the log file path, in the same format as Exim's 
+    log_file_path option (for example, "/var/log/exim_%slog"), again overriding
+    the script's default, which is to find the setting from Exim's
+    configuration.
+
+Each time exicyclog is run the file names get "shuffled down" by one. If the
+main log file name is mainlog (the default) then when exicyclog is run mainlog
+becomes mainlog.01, the previous mainlog.01 becomes mainlog.02 and so on, up to
+the limit that is set in the script or by the -k option. Log files whose
+numbers exceed the limit are discarded. Reject logs are handled similarly.
+
+If the limit is greater than 99, the script uses 3-digit numbers such as
+mainlog.001, mainlog.002, etc. If you change from a number less than 99 to one
+that is greater, or vice versa, you will have to fix the names of any existing
+log files.
+
+If no mainlog file exists, the script does nothing. Files that "drop off" the
+end are deleted. All files with numbers greater than 01 are compressed, using a
+compression command which is configured by the COMPRESS_COMMAND setting in
+Local/Makefile. It is usual to run exicyclog daily from a root crontab entry of
+the form
+
+1 0 * * * su exim -c /usr/exim/bin/exicyclog
+
+assuming you have used the name "exim" for the Exim user. You can run exicyclog
+as root if you wish, but there is no need.
+
+
+52.7 Mail statistics (eximstats)
+--------------------------------
+
+A Perl script called eximstats is provided for extracting statistical
+information from log files. The output is either plain text, or HTML. Exim log
+files are also supported by the Lire system produced by the LogReport
+Foundation http://www.logreport.org.
+
+The eximstats script has been hacked about quite a bit over time. The latest
+version is the result of some extensive revision by Steve Campbell. A lot of
+information is given by default, but there are options for suppressing various
+parts of it. Following any options, the arguments to the script are a list of
+files, which should be main log files. For example:
+
+eximstats -nr /var/spool/exim/log/mainlog.01
+
+By default, eximstats extracts information about the number and volume of
+messages received from or delivered to various hosts. The information is sorted
+both by message count and by volume, and the top fifty hosts in each category
+are listed on the standard output. Similar information, based on email
+addresses or domains instead of hosts can be requested by means of various
+options. For messages delivered and received locally, similar statistics are
+also produced per user.
+
+The output also includes total counts and statistics about delivery errors, and
+histograms showing the number of messages received and deliveries made in each
+hour of the day. A delivery with more than one address in its envelope (for
+example, an SMTP transaction with more than one RCPT command) is counted as a
+single delivery by eximstats.
+
+Though normally more deliveries than receipts are reported (as messages may
+have multiple recipients), it is possible for eximstats to report more messages
+received than delivered, even though the queue is empty at the start and end of
+the period in question. If an incoming message contains no valid recipients, no
+deliveries are recorded for it. A bounce message is handled as an entirely
+separate message.
+
+eximstats always outputs a grand total summary giving the volume and number of
+messages received and deliveries made, and the number of hosts involved in each
+case. It also outputs the number of messages that were delayed (that is, not
+completely delivered at the first attempt), and the number that had at least
+one address that failed.
+
+The remainder of the output is in sections that can be independently disabled
+or modified by various options. It consists of a summary of deliveries by
+transport, histograms of messages received and delivered per time interval
+(default per hour), information about the time messages spent on the queue, a
+list of relayed messages, lists of the top fifty sending hosts, local senders,
+destination hosts, and destination local users by count and by volume, and a
+list of delivery errors that occurred.
+
+The relay information lists messages that were actually relayed, that is, they
+came from a remote host and were directly delivered to some other remote host,
+without being processed (for example, for aliasing or forwarding) locally.
+
+There are quite a few options for eximstats to control exactly what it outputs.
+These are documented in the Perl script itself, and can be extracted by running
+the command perldoc on the script. For example:
+
+perldoc /usr/exim/bin/eximstats
+
+
+52.8 Checking access policy (exim_checkaccess)
+----------------------------------------------
+
+The -bh command line argument allows you to run a fake SMTP session with
+debugging output, in order to check what Exim is doing when it is applying
+policy controls to incoming SMTP mail. However, not everybody is sufficiently
+familiar with the SMTP protocol to be able to make full use of -bh, and
+sometimes you just want to answer the question "Does this address have access?"
+without bothering with any further details.
+
+The exim_checkaccess utility is a "packaged" version of -bh. It takes two
+arguments, an IP address and an email address:
+
+exim_checkaccess 10.9.8.7 A.User@a.domain.example
+
+The utility runs a call to Exim with the -bh option, to test whether the given
+email address would be accepted in a RCPT command in a TCP/IP connection from
+the host with the given IP address. The output of the utility is either the
+word "accepted", or the SMTP error response, for example:
+
+Rejected:
+550 Relay not permitted
+
+When running this test, the utility uses "<>" as the envelope sender address
+for the MAIL command, but you can change this by providing additional options.
+These are passed directly to the Exim command. For example, to specify that the
+test is to be run with the sender address himself@there.example you can use:
+
+exim_checkaccess 10.9.8.7 A.User@a.domain.example \
+                 -f himself@there.example
+
+Note that these additional Exim command line items must be given after the two
+mandatory arguments.
+
+Because the exim_checkaccess uses -bh, it does not perform callouts while
+running its checks. You can run checks that include callouts by using -bhc, but
+this is not yet available in a "packaged" form.
+
+
+52.9 Making DBM files (exim_dbmbuild)
+-------------------------------------
+
+The exim_dbmbuild program reads an input file containing keys and data in the
+format used by the lsearch lookup (see section 9.3). It writes a DBM file using
+the lower-cased alias names as keys and the remainder of the information as
+data. The lower-casing can be prevented by calling the program with the -nolc
+option.
+
+A terminating zero is included as part of the key string. This is expected by
+the dbm lookup type. However, if the option -nozero is given, exim_dbmbuild
+creates files without terminating zeroes in either the key strings or the data
+strings. The dbmnz lookup type can be used with such files.
+
+The program requires two arguments: the name of the input file (which can be a
+single hyphen to indicate the standard input), and the name of the output file.
+It creates the output under a temporary name, and then renames it if all went
+well.
+
+If the native DB interface is in use (USE_DB is set in a compile-time
+configuration file - this is common in free versions of Unix) the two file
+names must be different, because in this mode the Berkeley DB functions create
+a single output file using exactly the name given. For example,
+
+exim_dbmbuild /etc/aliases /etc/aliases.db
+
+reads the system alias file and creates a DBM version of it in /etc/aliases.db.
+
+In systems that use the ndbm routines (mostly proprietary versions of Unix),
+two files are used, with the suffixes .dir and .pag. In this environment, the
+suffixes are added to the second argument of exim_dbmbuild, so it can be the
+same as the first. This is also the case when the Berkeley functions are used
+in compatibility mode (though this is not recommended), because in that case it
+adds a .db suffix to the file name.
+
+If a duplicate key is encountered, the program outputs a warning, and when it
+finishes, its return code is 1 rather than zero, unless the -noduperr option is
+used. By default, only the first of a set of duplicates is used - this makes it
+compatible with lsearch lookups. There is an option -lastdup which causes it to
+use the data for the last duplicate instead. There is also an option -nowarn,
+which stops it listing duplicate keys to stderr. For other errors, where it
+doesn't actually make a new file, the return code is 2.
+
+
+52.10 Finding individual retry times (exinext)
+----------------------------------------------
+
+A utility called exinext (mostly a Perl script) provides the ability to fish
+specific information out of the retry database. Given a mail domain (or a
+complete address), it looks up the hosts for that domain, and outputs any retry
+information for the hosts or for the domain. At present, the retry information
+is obtained by running exim_dumpdb (see below) and post-processing the output.
+For example:
+
+$ exinext piglet@milne.fict.example
+kanga.milne.example:192.168.8.1 error 146: Connection refused
+  first failed: 21-Feb-1996 14:57:34
+  last tried:   21-Feb-1996 14:57:34
+  next try at:  21-Feb-1996 15:02:34
+roo.milne.example:192.168.8.3 error 146: Connection refused
+  first failed: 20-Jan-1996 13:12:08
+  last tried:   21-Feb-1996 11:42:03
+  next try at:  21-Feb-1996 19:42:03
+  past final cutoff time
+
+You can also give exinext a local part, without a domain, and it will give any
+retry information for that local part in your default domain. A message id can
+be used to obtain retry information pertaining to a specific message. This
+exists only when an attempt to deliver a message to a remote host suffers a
+message-specific error (see section 47.2). exinext is not particularly
+efficient, but then it is not expected to be run very often.
+
+The exinext utility calls Exim to find out information such as the location of
+the spool directory. The utility has -C and -D options, which are passed on to
+the exim commands. The first specifies an alternate Exim configuration file,
+and the second sets macros for use within the configuration file. These
+features are mainly to help in testing, but might also be useful in
+environments where more than one configuration file is in use.
+
+
+52.11 Hints database maintenance
+--------------------------------
+
+Three utility programs are provided for maintaining the DBM files that Exim
+uses to contain its delivery hint information. Each program requires two
+arguments. The first specifies the name of Exim's spool directory, and the
+second is the name of the database it is to operate on. These are as follows:
+
+  * retry: the database of retry information
+
+  * wait-<transport name>: databases of information about messages waiting for
+    remote hosts
+
+  * callout: the callout cache
+
+  * ratelimit: the data for implementing the ratelimit ACL condition
+
+  * misc: other hints data
+
+The misc database is used for
+
+  * Serializing ETRN runs (when smtp_etrn_serialize is set)
+
+  * Serializing delivery to a specific host (when serialize_hosts is set in an 
+    smtp transport)
+
+
+52.12 exim_dumpdb
+-----------------
+
+The entire contents of a database are written to the standard output by the 
+exim_dumpdb program, which has no options or arguments other than the spool and
+database names. For example, to dump the retry database:
+
+exim_dumpdb /var/spool/exim retry
+
+Two lines of output are produced for each entry:
+
+T:mail.ref.example:192.168.242.242 146 77 Connection refused
+31-Oct-1995 12:00:12 02-Nov-1995 12:21:39 02-Nov-1995 20:21:39 *
+
+The first item on the first line is the key of the record. It starts with one
+of the letters R, or T, depending on whether it refers to a routing or
+transport retry. For a local delivery, the next part is the local address; for
+a remote delivery it is the name of the remote host, followed by its failing IP
+address (unless retry_include_ip_address is set false on the smtp transport).
+If the remote port is not the standard one (port 25), it is added to the IP
+address. Then there follows an error code, an additional error code, and a
+textual description of the error.
+
+The three times on the second line are the time of first failure, the time of
+the last delivery attempt, and the computed time for the next attempt. The line
+ends with an asterisk if the cutoff time for the last retry rule has been
+exceeded.
+
+Each output line from exim_dumpdb for the wait-xxx databases consists of a host
+name followed by a list of ids for messages that are or were waiting to be
+delivered to that host. If there are a very large number for any one host,
+continuation records, with a sequence number added to the host name, may be
+seen. The data in these records is often out of date, because a message may be
+routed to several alternative hosts, and Exim makes no effort to keep
+cross-references.
+
+
+52.13 exim_tidydb
+-----------------
+
+The exim_tidydb utility program is used to tidy up the contents of a hints
+database. If run with no options, it removes all records that are more than 30
+days old. The age is calculated from the date and time that the record was last
+updated. Note that, in the case of the retry database, it is not the time since
+the first delivery failure. Information about a host that has been down for
+more than 30 days will remain in the database, provided that the record is
+updated sufficiently often.
+
+The cutoff date can be altered by means of the -t option, which must be
+followed by a time. For example, to remove all records older than a week from
+the retry database:
+
+exim_tidydb -t 7d /var/spool/exim retry
+
+Both the wait-xxx and retry databases contain items that involve message ids.
+In the former these appear as data in records keyed by host - they were
+messages that were waiting for that host - and in the latter they are the keys
+for retry information for messages that have suffered certain types of error.
+When exim_tidydb is run, a check is made to ensure that message ids in database
+records are those of messages that are still on the queue. Message ids for
+messages that no longer exist are removed from wait-xxx records, and if this
+leaves any records empty, they are deleted. For the retry database, records
+whose keys are non-existent message ids are removed. The exim_tidydb utility
+outputs comments on the standard output whenever it removes information from
+the database.
+
+Certain records are automatically removed by Exim when they are no longer
+needed, but others are not. For example, if all the MX hosts for a domain are
+down, a retry record is created for each one. If the primary MX host comes back
+first, its record is removed when Exim successfully delivers to it, but the
+records for the others remain because Exim has not tried to use those hosts.
+
+It is important, therefore, to run exim_tidydb periodically on all the hints
+databases. You should do this at a quiet time of day, because it requires a
+database to be locked (and therefore inaccessible to Exim) while it does its
+work. Removing records from a DBM file does not normally make the file smaller,
+but all the common DBM libraries are able to re-use the space that is released.
+After an initial phase of increasing in size, the databases normally reach a
+point at which they no longer get any bigger, as long as they are regularly
+tidied.
+
+Warning: If you never run exim_tidydb, the space used by the hints databases is
+likely to keep on increasing.
+
+
+52.14 exim_fixdb
+----------------
+
+The exim_fixdb program is a utility for interactively modifying databases. Its
+main use is for testing Exim, but it might also be occasionally useful for
+getting round problems in a live system. It has no options, and its interface
+is somewhat crude. On entry, it prompts for input with a right angle-bracket. A
+key of a database record can then be entered, and the data for that record is
+displayed.
+
+If "d" is typed at the next prompt, the entire record is deleted. For all
+except the retry database, that is the only operation that can be carried out.
+For the retry database, each field is output preceded by a number, and data for
+individual fields can be changed by typing the field number followed by new
+data, for example:
+
+> 4 951102:1000
+
+resets the time of the next delivery attempt. Time values are given as a
+sequence of digit pairs for year, month, day, hour, and minute. Colons can be
+used as optional separators.
+
+
+52.15 Mailbox maintenance (exim_lock)
+-------------------------------------
+
+The exim_lock utility locks a mailbox file using the same algorithm as Exim.
+For a discussion of locking issues, see section 26.3. Exim_lock can be used to
+prevent any modification of a mailbox by Exim or a user agent while
+investigating a problem. The utility requires the name of the file as its first
+argument. If the locking is successful, the second argument is run as a command
+(using C's system() function); if there is no second argument, the value of the
+SHELL environment variable is used; if this is unset or empty, /bin/sh is run.
+When the command finishes, the mailbox is unlocked and the utility ends. The
+following options are available:
+
+-fcntl
+
+    Use fcntl() locking on the open mailbox.
+
+-flock
+
+    Use flock() locking on the open mailbox, provided the operating system
+    supports it.
+
+-interval
+
+    This must be followed by a number, which is a number of seconds; it sets
+    the interval to sleep between retries (default 3).
+
+-lockfile
+
+    Create a lock file before opening the mailbox.
+
+-mbx
+
+    Lock the mailbox using MBX rules.
+
+-q
+
+    Suppress verification output.
+
+-retries
+
+    This must be followed by a number; it sets the number of times to try to
+    get the lock (default 10).
+
+-restore_time
+
+    This option causes exim_lock to restore the modified and read times to the
+    locked file before exiting. This allows you to access a locked mailbox (for
+    example, to take a backup copy) without disturbing the times that the user
+    subsequently sees.
+
+-timeout
+
+    This must be followed by a number, which is a number of seconds; it sets a
+    timeout to be used with a blocking fcntl() lock. If it is not set (the
+    default), a non-blocking call is used.
+
+-v
+
+    Generate verbose output.
+
+If none of -fcntl, -flock, -lockfile or -mbx are given, the default is to
+create a lock file and also to use fcntl() locking on the mailbox, which is the
+same as Exim's default. The use of -flock or -fcntl requires that the file be
+writeable; the use of -lockfile requires that the directory containing the file
+be writeable. Locking by lock file does not last for ever; Exim assumes that a
+lock file is expired if it is more than 30 minutes old.
+
+The -mbx option can be used with either or both of -fcntl or -flock. It assumes
+-fcntl by default. MBX locking causes a shared lock to be taken out on the open
+mailbox, and an exclusive lock on the file /tmp/.n.m where n and m are the
+device number and inode number of the mailbox file. When the locking is
+released, if an exclusive lock can be obtained for the mailbox, the file in /
+tmp is deleted.
+
+The default output contains verification of the locking that takes place. The 
+-v option causes some additional information to be given. The -q option
+suppresses all output except error messages.
+
+A command such as
+
+exim_lock /var/spool/mail/spqr
+
+runs an interactive shell while the file is locked, whereas
+
+exim_lock -q /var/spool/mail/spqr <<End
+<some commands>
+End
+
+runs a specific non-interactive sequence of commands while the file is locked,
+suppressing all verification output. A single command can be run by a command
+such as
+
+exim_lock -q /var/spool/mail/spqr \
+  "cp /var/spool/mail/spqr /some/where"
+
+Note that if a command is supplied, it must be entirely contained within the
+second argument - hence the quotes.
+
+
+
+===============================================================================
+53. THE EXIM MONITOR
+
+The Exim monitor is an application which displays in an X window information
+about the state of Exim's queue and what Exim is doing. An admin user can
+perform certain operations on messages from this GUI interface; however all
+such facilities are also available from the command line, and indeed, the
+monitor itself makes use of the command line to perform any actions requested.
+
+
+53.1 Running the monitor
+------------------------
+
+The monitor is started by running the script called eximon. This is a shell
+script that sets up a number of environment variables, and then runs the binary
+called eximon.bin. The default appearance of the monitor window can be changed
+by editing the Local/eximon.conf file created by editing exim_monitor/EDITME.
+Comments in that file describe what the various parameters are for.
+
+The parameters that get built into the eximon script can be overridden for a
+particular invocation by setting up environment variables of the same names,
+preceded by "EXIMON_". For example, a shell command such as
+
+EXIMON_LOG_DEPTH=400 eximon
+
+(in a Bourne-compatible shell) runs eximon with an overriding setting of the
+LOG_DEPTH parameter. If EXIMON_LOG_FILE_PATH is set in the environment, it
+overrides the Exim log file configuration. This makes it possible to have 
+eximon tailing log data that is written to syslog, provided that MAIL.INFO
+syslog messages are routed to a file on the local host.
+
+X resources can be used to change the appearance of the window in the normal
+way. For example, a resource setting of the form
+
+Eximon*background: gray94
+
+changes the colour of the background to light grey rather than white. The
+stripcharts are drawn with both the data lines and the reference lines in
+black. This means that the reference lines are not visible when on top of the
+data. However, their colour can be changed by setting a resource called
+"highlight" (an odd name, but that's what the Athena stripchart widget uses).
+For example, if your X server is running Unix, you could set up lighter
+reference lines in the stripcharts by obeying
+
+xrdb -merge <<End
+Eximon*highlight: gray
+End
+
+In order to see the contents of messages on the queue, and to operate on them, 
+eximon must either be run as root or by an admin user.
+
+The command-line parameters of eximon are passed to eximon.bin and may contain
+X11 resource parameters interpreted by the X11 library. In addition, if the
+first parameter starts with the string "gdb" then it is removed and the binary
+is invoked under gdb (the parameter is used as the gdb command-name, so
+versioned variants of gdb can be invoked).
+
+The monitor's window is divided into three parts. The first contains one or
+more stripcharts and two action buttons, the second contains a "tail" of the
+main log file, and the third is a display of the queue of messages awaiting
+delivery, with two more action buttons. The following sections describe these
+different parts of the display.
+
+
+53.2 The stripcharts
+--------------------
+
+The first stripchart is always a count of messages on the queue. Its name can
+be configured by setting QUEUE_STRIPCHART_NAME in the Local/eximon.conf file.
+The remaining stripcharts are defined in the configuration script by regular
+expression matches on log file entries, making it possible to display, for
+example, counts of messages delivered to certain hosts or using certain
+transports. The supplied defaults display counts of received and delivered
+messages, and of local and SMTP deliveries. The default period between
+stripchart updates is one minute; this can be adjusted by a parameter in the
+Local/eximon.conf file.
+
+The stripchart displays rescale themselves automatically as the value they are
+displaying changes. There are always 10 horizontal lines in each chart; the
+title string indicates the value of each division when it is greater than one.
+For example, "x2" means that each division represents a value of 2.
+
+It is also possible to have a stripchart which shows the percentage fullness of
+a particular disk partition, which is useful when local deliveries are confined
+to a single partition.
+
+This relies on the availability of the statvfs() function or equivalent in the
+operating system. Most, but not all versions of Unix that support Exim have
+this. For this particular stripchart, the top of the chart always represents
+100%, and the scale is given as "x10%". This chart is configured by setting
+SIZE_STRIPCHART and (optionally) SIZE_STRIPCHART_NAME in the Local/eximon.conf
+file.
+
+
+53.3 Main action buttons
+------------------------
+
+Below the stripcharts there is an action button for quitting the monitor. Next
+to this is another button marked "Size". They are placed here so that shrinking
+the window to its default minimum size leaves just the queue count stripchart
+and these two buttons visible. Pressing the "Size" button causes the window to
+expand to its maximum size, unless it is already at the maximum, in which case
+it is reduced to its minimum.
+
+When expanding to the maximum, if the window cannot be fully seen where it
+currently is, it is moved back to where it was the last time it was at full
+size. When it is expanding from its minimum size, the old position is
+remembered, and next time it is reduced to the minimum it is moved back there.
+
+The idea is that you can keep a reduced window just showing one or two
+stripcharts at a convenient place on your screen, easily expand it to show the
+full window when required, and just as easily put it back to what it was. The
+idea is copied from what the twm window manager does for its f.fullzoom action.
+The minimum size of the window can be changed by setting the MIN_HEIGHT and
+MIN_WIDTH values in Local/eximon.conf.
+
+Normally, the monitor starts up with the window at its full size, but it can be
+built so that it starts up with the window at its smallest size, by setting
+START_SMALL=yes in Local/eximon.conf.
+
+
+53.4 The log display
+--------------------
+
+The second section of the window is an area in which a display of the tail of
+the main log is maintained. To save space on the screen, the timestamp on each
+log line is shortened by removing the date and, if log_timezone is set, the
+timezone. The log tail is not available when the only destination for logging
+data is syslog, unless the syslog lines are routed to a local file whose name
+is passed to eximon via the EXIMON_LOG_FILE_PATH environment variable.
+
+The log sub-window has a scroll bar at its lefthand side which can be used to
+move back to look at earlier text, and the up and down arrow keys also have a
+scrolling effect. The amount of log that is kept depends on the setting of
+LOG_BUFFER in Local/eximon.conf, which specifies the amount of memory to use.
+When this is full, the earlier 50% of data is discarded - this is much more
+efficient than throwing it away line by line. The sub-window also has a
+horizontal scroll bar for accessing the ends of long log lines. This is the
+only means of horizontal scrolling; the right and left arrow keys are not
+available. Text can be cut from this part of the window using the mouse in the
+normal way. The size of this subwindow is controlled by parameters in the
+configuration file Local/eximon.conf.
+
+Searches of the text in the log window can be carried out by means of the ^R
+and ^S keystrokes, which default to a reverse and a forward search,
+respectively. The search covers only the text that is displayed in the window.
+It cannot go further back up the log.
+
+The point from which the search starts is indicated by a caret marker. This is
+normally at the end of the text in the window, but can be positioned explicitly
+by pointing and clicking with the left mouse button, and is moved automatically
+by a successful search. If new text arrives in the window when it is scrolled
+back, the caret remains where it is, but if the window is not scrolled back,
+the caret is moved to the end of the new text.
+
+Pressing ^R or ^S pops up a window into which the search text can be typed.
+There are buttons for selecting forward or reverse searching, for carrying out
+the search, and for cancelling. If the "Search" button is pressed, the search
+happens and the window remains so that further searches can be done. If the
+"Return" key is pressed, a single search is done and the window is closed. If ^
+C is typed the search is cancelled.
+
+The searching facility is implemented using the facilities of the Athena text
+widget. By default this pops up a window containing both "search" and "replace"
+options. In order to suppress the unwanted "replace" portion for eximon, a
+modified version of the TextPop widget is distributed with Exim. However, the
+linkers in BSDI and HP-UX seem unable to handle an externally provided version
+of TextPop when the remaining parts of the text widget come from the standard
+libraries. The compile-time option EXIMON_TEXTPOP can be unset to cut out the
+modified TextPop, making it possible to build Eximon on these systems, at the
+expense of having unwanted items in the search popup window.
+
+
+53.5 The queue display
+----------------------
+
+The bottom section of the monitor window contains a list of all messages that
+are on the queue, which includes those currently being received or delivered,
+as well as those awaiting delivery. The size of this subwindow is controlled by
+parameters in the configuration file Local/eximon.conf, and the frequency at
+which it is updated is controlled by another parameter in the same file - the
+default is 5 minutes, since queue scans can be quite expensive. However, there
+is an "Update" action button just above the display which can be used to force
+an update of the queue display at any time.
+
+When a host is down for some time, a lot of pending mail can build up for it,
+and this can make it hard to deal with other messages on the queue. To help
+with this situation there is a button next to "Update" called "Hide". If
+pressed, a dialogue box called "Hide addresses ending with" is put up. If you
+type anything in here and press "Return", the text is added to a chain of such
+texts, and if every undelivered address in a message matches at least one of
+the texts, the message is not displayed.
+
+If there is an address that does not match any of the texts, all the addresses
+are displayed as normal. The matching happens on the ends of addresses so, for
+example, cam.ac.uk specifies all addresses in Cambridge, while 
+xxx@foo.com.example specifies just one specific address. When any hiding has
+been set up, a button called "Unhide" is displayed. If pressed, it cancels all
+hiding. Also, to ensure that hidden messages do not get forgotten, a hide
+request is automatically cancelled after one hour.
+
+While the dialogue box is displayed, you can't press any buttons or do anything
+else to the monitor window. For this reason, if you want to cut text from the
+queue display to use in the dialogue box, you have to do the cutting before
+pressing the "Hide" button.
+
+The queue display contains, for each unhidden queued message, the length of
+time it has been on the queue, the size of the message, the message id, the
+message sender, and the first undelivered recipient, all on one line. If it is
+a bounce message, the sender is shown as "<>". If there is more than one
+recipient to which the message has not yet been delivered, subsequent ones are
+listed on additional lines, up to a maximum configured number, following which
+an ellipsis is displayed. Recipients that have already received the message are
+not shown.
+
+If a message is frozen, an asterisk is displayed at the left-hand side.
+
+The queue display has a vertical scroll bar, and can also be scrolled by means
+of the arrow keys. Text can be cut from it using the mouse in the normal way.
+The text searching facilities, as described above for the log window, are also
+available, but the caret is always moved to the end of the text when the queue
+display is updated.
+
+
+53.6 The queue menu
+-------------------
+
+If the shift key is held down and the left button is clicked when the mouse
+pointer is over the text for any message, an action menu pops up, and the first
+line of the queue display for the message is highlighted. This does not affect
+any selected text.
+
+If you want to use some other event for popping up the menu, you can set the
+MENU_EVENT parameter in Local/eximon.conf to change the default, or set
+EXIMON_MENU_EVENT in the environment before starting the monitor. The value set
+in this parameter is a standard X event description. For example, to run eximon
+using ctrl rather than shift you could use
+
+EXIMON_MENU_EVENT='Ctrl<Btn1Down>' eximon
+
+The title of the menu is the message id, and it contains entries which act as
+follows:
+
+  * message log: The contents of the message log for the message are displayed
+    in a new text window.
+
+  * headers: Information from the spool file that contains the envelope
+    information and headers is displayed in a new text window. See chapter 55
+    for a description of the format of spool files.
+
+  * body: The contents of the spool file containing the body of the message are
+    displayed in a new text window. There is a default limit of 20,000 bytes to
+    the amount of data displayed. This can be changed by setting the BODY_MAX
+    option at compile time, or the EXIMON_BODY_MAX option at run time.
+
+  * deliver message: A call to Exim is made using the -M option to request
+    delivery of the message. This causes an automatic thaw if the message is
+    frozen. The -v option is also set, and the output from Exim is displayed in
+    a new text window. The delivery is run in a separate process, to avoid
+    holding up the monitor while the delivery proceeds.
+
+  * freeze message: A call to Exim is made using the -Mf option to request that
+    the message be frozen.
+
+  * thaw message: A call to Exim is made using the -Mt option to request that
+    the message be thawed.
+
+  * give up on msg: A call to Exim is made using the -Mg option to request that
+    Exim gives up trying to deliver the message. A bounce message is generated
+    for any remaining undelivered addresses.
+
+  * remove message: A call to Exim is made using the -Mrm option to request
+    that the message be deleted from the system without generating a bounce
+    message.
+
+  * add recipient: A dialog box is displayed into which a recipient address can
+    be typed. If the address is not qualified and the QUALIFY_DOMAIN parameter
+    is set in Local/eximon.conf, the address is qualified with that domain.
+    Otherwise it must be entered as a fully qualified address. Pressing RETURN
+    causes a call to Exim to be made using the -Mar option to request that an
+    additional recipient be added to the message, unless the entry box is
+    empty, in which case no action is taken.
+
+  * mark delivered: A dialog box is displayed into which a recipient address
+    can be typed. If the address is not qualified and the QUALIFY_DOMAIN
+    parameter is set in Local/eximon.conf, the address is qualified with that
+    domain. Otherwise it must be entered as a fully qualified address. Pressing
+    RETURN causes a call to Exim to be made using the -Mmd option to mark the
+    given recipient address as already delivered, unless the entry box is
+    empty, in which case no action is taken.
+
+  * mark all delivered: A call to Exim is made using the -Mmad option to mark
+    all recipient addresses as already delivered.
+
+  * edit sender: A dialog box is displayed initialized with the current
+    sender's address. Pressing RETURN causes a call to Exim to be made using
+    the -Mes option to replace the sender address, unless the entry box is
+    empty, in which case no action is taken. If you want to set an empty sender
+    (as in bounce messages), you must specify it as "<>". Otherwise, if the
+    address is not qualified and the QUALIFY_DOMAIN parameter is set in Local/
+    eximon.conf, the address is qualified with that domain.
+
+When a delivery is forced, a window showing the -v output is displayed. In
+other cases when a call to Exim is made, if there is any output from Exim (in
+particular, if the command fails) a window containing the command and the
+output is displayed. Otherwise, the results of the action are normally apparent
+from the log and queue displays. However, if you set ACTION_OUTPUT=yes in Local
+/eximon.conf, a window showing the Exim command is always opened, even if no
+output is generated.
+
+The queue display is automatically updated for actions such as freezing and
+thawing, unless ACTION_QUEUE_UPDATE=no has been set in Local/eximon.conf. In
+this case the "Update" button has to be used to force an update of the display
+after one of these actions.
+
+In any text window that is displayed as result of a menu action, the normal
+cut-and-paste facility is available, and searching can be carried out using ^R
+and ^S, as described above for the log tail window.
+
+
+
+===============================================================================
+54. SECURITY CONSIDERATIONS
+
+This chapter discusses a number of issues concerned with security, some of
+which are also covered in other parts of this manual.
+
+For reasons that this author does not understand, some people have promoted
+Exim as a "particularly secure" mailer. Perhaps it is because of the existence
+of this chapter in the documentation. However, the intent of the chapter is
+simply to describe the way Exim works in relation to certain security concerns,
+not to make any specific claims about the effectiveness of its security as
+compared with other MTAs.
+
+What follows is a description of the way Exim is supposed to be. Best efforts
+have been made to try to ensure that the code agrees with the theory, but an
+absence of bugs can never be guaranteed. Any that are reported will get fixed
+as soon as possible.
+
+
+54.1 Building a more "hardened" Exim
+------------------------------------
+
+There are a number of build-time options that can be set in Local/Makefile to
+create Exim binaries that are "harder" to attack, in particular by a rogue Exim
+administrator who does not have the root password, or by someone who has
+penetrated the Exim (but not the root) account. These options are as follows:
+
+  * ALT_CONFIG_PREFIX can be set to a string that is required to match the
+    start of any file names used with the -C option. When it is set, these file
+    names are also not allowed to contain the sequence "/../". (However, if the
+    value of the -C option is identical to the value of CONFIGURE_FILE in Local
+    /Makefile, Exim ignores -C and proceeds as usual.) There is no default
+    setting for ALT_CONFIG_PREFIX.
+
+    If the permitted configuration files are confined to a directory to which
+    only root has access, this guards against someone who has broken into the
+    Exim account from running a privileged Exim with an arbitrary configuration
+    file, and using it to break into other accounts.
+
+  * If a non-trusted configuration file (i.e. not the default configuration
+    file or one which is trusted by virtue of being listed in the
+    TRUSTED_CONFIG_LIST file) is specified with -C, or if macros are given with
+    -D (but see the next item), then root privilege is retained only if the
+    caller of Exim is root. This locks out the possibility of testing a
+    configuration using -C right through message reception and delivery, even
+    if the caller is root. The reception works, but by that time, Exim is
+    running as the Exim user, so when it re-execs to regain privilege for the
+    delivery, the use of -C causes privilege to be lost. However, root can test
+    reception and delivery using two separate commands.
+
+  * The WHITELIST_D_MACROS build option declares some macros to be safe to
+    override with -D if the real uid is one of root, the Exim run-time user or
+    the CONFIGURE_OWNER, if defined. The potential impact of this option is
+    limited by requiring the run-time value supplied to -D to match a regex
+    that errs on the restrictive side. Requiring build-time selection of safe
+    macros is onerous but this option is intended solely as a transition
+    mechanism to permit previously-working configurations to continue to work
+    after release 4.73.
+
+  * If DISABLE_D_OPTION is defined, the use of the -D command line option is
+    disabled.
+
+  * FIXED_NEVER_USERS can be set to a colon-separated list of users that are
+    never to be used for any deliveries. This is like the never_users runtime
+    option, but it cannot be overridden; the runtime option adds additional
+    users to the list. The default setting is "root"; this prevents a non-root
+    user who is permitted to modify the runtime file from using Exim as a way
+    to get root.
+
+
+54.2 Root privilege
+-------------------
+
+The Exim binary is normally setuid to root, which means that it gains root
+privilege (runs as root) when it starts execution. In some special cases (for
+example, when the daemon is not in use and there are no local deliveries), it
+may be possible to run Exim setuid to some user other than root. This is
+discussed in the next section. However, in most installations, root privilege
+is required for two things:
+
+  * To set up a socket connected to the standard SMTP port (25) when
+    initialising the listening daemon. If Exim is run from inetd, this
+    privileged action is not required.
+
+  * To be able to change uid and gid in order to read users' .forward files and
+    perform local deliveries as the receiving user or as specified in the
+    configuration.
+
+It is not necessary to be root to do any of the other things Exim does, such as
+receiving messages and delivering them externally over SMTP, and it is
+obviously more secure if Exim does not run as root except when necessary. For
+this reason, a user and group for Exim to use must be defined in Local/Makefile
+. These are known as "the Exim user" and "the Exim group". Their values can be
+changed by the run time configuration, though this is not recommended. Often a
+user called exim is used, but some sites use mail or another user name
+altogether.
+
+Exim uses setuid() whenever it gives up root privilege. This is a permanent
+abdication; the process cannot regain root afterwards. Prior to release 4.00,
+seteuid() was used in some circumstances, but this is no longer the case.
+
+After a new Exim process has interpreted its command line options, it changes
+uid and gid in the following cases:
+
+  * If the -C option is used to specify an alternate configuration file, or if
+    the -D option is used to define macro values for the configuration, and the
+    calling process is not running as root, the uid and gid are changed to
+    those of the calling process. However, if DISABLE_D_OPTION is defined in
+    Local/Makefile, the -D option may not be used at all. If WHITELIST_D_MACROS
+    is defined in Local/Makefile, then some macro values can be supplied if the
+    calling process is running as root, the Exim run-time user or
+    CONFIGURE_OWNER, if defined.
+
+  * If the expansion test option (-be) or one of the filter testing options (
+    -bf or -bF) are used, the uid and gid are changed to those of the calling
+    process.
+
+  * If the process is not a daemon process or a queue runner process or a
+    delivery process or a process for testing address routing (started with -bt
+    ), the uid and gid are changed to the Exim user and group. This means that
+    Exim always runs under its own uid and gid when receiving messages. This
+    also applies when testing address verification (the -bv option) and testing
+    incoming message policy controls (the -bh option).
+
+  * For a daemon, queue runner, delivery, or address testing process, the uid
+    remains as root at this stage, but the gid is changed to the Exim group.
+
+The processes that initially retain root privilege behave as follows:
+
+  * A daemon process changes the gid to the Exim group and the uid to the Exim
+    user after setting up one or more listening sockets. The initgroups()
+    function is called, so that if the Exim user is in any additional groups,
+    they will be used during message reception.
+
+  * A queue runner process retains root privilege throughout its execution. Its
+    job is to fork a controlled sequence of delivery processes.
+
+  * A delivery process retains root privilege throughout most of its execution,
+    but any actual deliveries (that is, the transports themselves) are run in
+    subprocesses which always change to a non-root uid and gid. For local
+    deliveries this is typically the uid and gid of the owner of the mailbox;
+    for remote deliveries, the Exim uid and gid are used. Once all the delivery
+    subprocesses have been run, a delivery process changes to the Exim uid and
+    gid while doing post-delivery tidying up such as updating the retry
+    database and generating bounce and warning messages.
+
+    While the recipient addresses in a message are being routed, the delivery
+    process runs as root. However, if a user's filter file has to be processed,
+    this is done in a subprocess that runs under the individual user's uid and
+    gid. A system filter is run as root unless system_filter_user is set.
+
+  * A process that is testing addresses (the -bt option) runs as root so that
+    the routing is done in the same environment as a message delivery.
+
+
+54.3 Running Exim without privilege
+-----------------------------------
+
+Some installations like to run Exim in an unprivileged state for more of its
+operation, for added security. Support for this mode of operation is provided
+by the global option deliver_drop_privilege. When this is set, the uid and gid
+are changed to the Exim user and group at the start of a delivery process (and
+also queue runner and address testing processes). This means that address
+routing is no longer run as root, and the deliveries themselves cannot change
+to any other uid.
+
+Leaving the binary setuid to root, but setting deliver_drop_privilege means
+that the daemon can still be started in the usual way, and it can respond
+correctly to SIGHUP because the re-invocation regains root privilege.
+
+An alternative approach is to make Exim setuid to the Exim user and also setgid
+to the Exim group. If you do this, the daemon must be started from a root
+process. (Calling Exim from a root process makes it behave in the way it does
+when it is setuid root.) However, the daemon cannot restart itself after a
+SIGHUP signal because it cannot regain privilege.
+
+It is still useful to set deliver_drop_privilege in this case, because it stops
+Exim from trying to re-invoke itself to do a delivery after a message has been
+received. Such a re-invocation is a waste of resources because it has no
+effect.
+
+If restarting the daemon is not an issue (for example, if mua_wrapper is set,
+or inetd is being used instead of a daemon), having the binary setuid to the
+Exim user seems a clean approach, but there is one complication:
+
+In this style of operation, Exim is running with the real uid and gid set to
+those of the calling process, and the effective uid/gid set to Exim's values.
+Ideally, any association with the calling process' uid/gid should be dropped,
+that is, the real uid/gid should be reset to the effective values so as to
+discard any privileges that the caller may have. While some operating systems
+have a function that permits this action for a non-root effective uid, quite a
+number of them do not. Because of this lack of standardization, Exim does not
+address this problem at this time.
+
+For this reason, the recommended approach for "mostly unprivileged" running is
+to keep the Exim binary setuid to root, and to set deliver_drop_privilege. This
+also has the advantage of allowing a daemon to be used in the most
+straightforward way.
+
+If you configure Exim not to run delivery processes as root, there are a number
+of restrictions on what you can do:
+
+  * You can deliver only as the Exim user/group. You should explicitly use the 
+    user and group options to override routers or local transports that
+    normally deliver as the recipient. This makes sure that configurations that
+    work in this mode function the same way in normal mode. Any implicit or
+    explicit specification of another user causes an error.
+
+  * Use of .forward files is severely restricted, such that it is usually not
+    worthwhile to include them in the configuration.
+
+  * Users who wish to use .forward would have to make their home directory and
+    the file itself accessible to the Exim user. Pipe and append-to-file
+    entries, and their equivalents in Exim filters, cannot be used. While they
+    could be enabled in the Exim user's name, that would be insecure and not
+    very useful.
+
+  * Unless the local user mailboxes are all owned by the Exim user (possible in
+    some POP3 or IMAP-only environments):
+
+     1. They must be owned by the Exim group and be writeable by that group.
+        This implies you must set mode in the appendfile configuration, as well
+        as the mode of the mailbox files themselves.
+
+     2. You must set no_check_owner, since most or all of the files will not be
+        owned by the Exim user.
+
+     3. You must set file_must_exist, because Exim cannot set the owner
+        correctly on a newly created mailbox when unprivileged. This also
+        implies that new mailboxes need to be created manually.
+
+These restrictions severely restrict what can be done in local deliveries.
+However, there are no restrictions on remote deliveries. If you are running a
+gateway host that does no local deliveries, setting deliver_drop_privilege
+gives more security at essentially no cost.
+
+If you are using the mua_wrapper facility (see chapter 50), 
+deliver_drop_privilege is forced to be true.
+
+
+54.4 Delivering to local files
+------------------------------
+
+Full details of the checks applied by appendfile before it writes to a file are
+given in chapter 26.
+
+
+54.5 Running local commands
+---------------------------
+
+There are a number of ways in which an administrator can configure Exim to run
+commands based upon received, untrustworthy, data. Further, in some
+configurations a user who can control a .forward file can also arrange to run
+commands. Configuration to check includes, but is not limited to:
+
+  * Use of use_shell in the pipe transport: various forms of shell command
+    injection may be possible with this option present. It is dangerous and
+    should be used only with considerable caution. Consider constraints which
+    whitelist allowed characters in a variable which is to be used in a pipe
+    transport that has use_shell enabled.
+
+  * A number of options such as forbid_filter_run, forbid_filter_perl, 
+    forbid_filter_dlfunc and so forth which restrict facilities available to
+    .forward files in a redirect router. If Exim is running on a central mail
+    hub to which ordinary users do not have shell access, but home directories
+    are NFS mounted (for instance) then administrators should review the list
+    of these forbid options available, and should bear in mind that the options
+    that may need forbidding can change as new features are added between
+    releases.
+
+  * The ${run...} expansion item does not use a shell by default, but
+    administrators can configure use of /bin/sh as part of the command. Such
+    invocations should be viewed with prejudicial suspicion.
+
+  * Administrators who use embedded Perl are advised to explore how Perl's
+    taint checking might apply to their usage.
+
+  * Use of ${expand...} is somewhat analagous to shell's eval builtin and
+    administrators are well advised to view its use with suspicion, in case
+    (for instance) it allows a local-part to contain embedded Exim directives.
+
+  * Use of ${match_local_part...} and friends becomes more dangerous if Exim
+    was built with EXPAND_LISTMATCH_RHS defined: the second string in each can
+    reference arbitrary lists and files, rather than just being a list of
+    opaque strings. The EXPAND_LISTMATCH_RHS option was added and set false by
+    default because of real-world security vulnerabilities caused by its use
+    with untrustworthy data injected in, for SQL injection attacks. Consider
+    the use of the inlisti expansion condition instead.
+
+
+54.6 Trust in configuration data
+--------------------------------
+
+If configuration data for Exim can come from untrustworthy sources, there are
+some issues to be aware of:
+
+  * Use of ${expand...} may provide a path for shell injection attacks.
+
+  * Letting untrusted data provide a regular expression is unwise.
+
+  * Using ${match...} to apply a fixed regular expression against untrusted
+    data may result in pathological behaviour within PCRE. Be aware of what
+    "backtracking" means and consider options for being more strict with a
+    regular expression. Avenues to explore include limiting what can match
+    (avoiding "." when "[a-z0-9]" or other character class will do), use of
+    atomic grouping and possessive quantifiers or just not using regular
+    expressions against untrusted data.
+
+  * It can be important to correctly use ${quote:...}, ${quote_local_part:...}
+    and ${quote_<lookup-type>:...} expansion items to ensure that data is
+    correctly constructed.
+
+  * Some lookups might return multiple results, even though normal usage is
+    only expected to yield one result.
+
+
+54.7 IPv4 source routing
+------------------------
+
+Many operating systems suppress IP source-routed packets in the kernel, but
+some cannot be made to do this, so Exim does its own check. It logs incoming
+IPv4 source-routed TCP calls, and then drops them. Things are all different in
+IPv6. No special checking is currently done.
+
+
+54.8 The VRFY, EXPN, and ETRN commands in SMTP
+----------------------------------------------
+
+Support for these SMTP commands is disabled by default. If required, they can
+be enabled by defining suitable ACLs.
+
+
+54.9 Privileged users
+---------------------
+
+Exim recognizes two sets of users with special privileges. Trusted users are
+able to submit new messages to Exim locally, but supply their own sender
+addresses and information about a sending host. For other users submitting
+local messages, Exim sets up the sender address from the uid, and doesn't
+permit a remote host to be specified.
+
+However, an untrusted user is permitted to use the -f command line option in
+the special form -f <> to indicate that a delivery failure for the message
+should not cause an error report. This affects the message's envelope, but it
+does not affect the Sender: header. Untrusted users may also be permitted to
+use specific forms of address with the -f option by setting the 
+untrusted_set_sender option.
+
+Trusted users are used to run processes that receive mail messages from some
+other mail domain and pass them on to Exim for delivery either locally, or over
+the Internet. Exim trusts a caller that is running as root, as the Exim user,
+as any user listed in the trusted_users configuration option, or under any
+group listed in the trusted_groups option.
+
+Admin users are permitted to do things to the messages on Exim's queue. They
+can freeze or thaw messages, cause them to be returned to their senders, remove
+them entirely, or modify them in various ways. In addition, admin users can run
+the Exim monitor and see all the information it is capable of providing, which
+includes the contents of files on the spool.
+
+By default, the use of the -M and -q options to cause Exim to attempt delivery
+of messages on its queue is restricted to admin users. This restriction can be
+relaxed by setting the no_prod_requires_admin option. Similarly, the use of -bp
+(and its variants) to list the contents of the queue is also restricted to
+admin users. This restriction can be relaxed by setting 
+no_queue_list_requires_admin.
+
+Exim recognizes an admin user if the calling process is running as root or as
+the Exim user or if any of the groups associated with the calling process is
+the Exim group. It is not necessary actually to be running under the Exim
+group. However, if admin users who are not root or the Exim user are to access
+the contents of files on the spool via the Exim monitor (which runs
+unprivileged), Exim must be built to allow group read access to its spool
+files.
+
+
+54.10 Spool files
+-----------------
+
+Exim's spool directory and everything it contains is owned by the Exim user and
+set to the Exim group. The mode for spool files is defined in the Local/
+Makefile configuration file, and defaults to 0640. This means that any user who
+is a member of the Exim group can access these files.
+
+
+54.11 Use of argv[0]
+--------------------
+
+Exim examines the last component of argv[0], and if it matches one of a set of
+specific strings, Exim assumes certain options. For example, calling Exim with
+the last component of argv[0] set to "rsmtp" is exactly equivalent to calling
+it with the option -bS. There are no security implications in this.
+
+
+54.12 Use of %f formatting
+--------------------------
+
+The only use made of "%f" by Exim is in formatting load average values. These
+are actually stored in integer variables as 1000 times the load average.
+Consequently, their range is limited and so therefore is the length of the
+converted output.
+
+
+54.13 Embedded Exim path
+------------------------
+
+Exim uses its own path name, which is embedded in the code, only when it needs
+to re-exec in order to regain root privilege. Therefore, it is not root when it
+does so. If some bug allowed the path to get overwritten, it would lead to an
+arbitrary program's being run as exim, not as root.
+
+
+54.14 Dynamic module directory
+------------------------------
+
+Any dynamically loadable modules must be installed into the directory defined
+in "LOOKUP_MODULE_DIR" in Local/Makefile for Exim to permit loading it.
+
+
+54.15 Use of sprintf()
+----------------------
+
+A large number of occurrences of "sprintf" in the code are actually calls to 
+string_sprintf(), a function that returns the result in malloc'd store. The
+intermediate formatting is done into a large fixed buffer by a function that
+runs through the format string itself, and checks the length of each conversion
+before performing it, thus preventing buffer overruns.
+
+The remaining uses of sprintf() happen in controlled circumstances where the
+output buffer is known to be sufficiently long to contain the converted string.
+
+
+54.16 Use of debug_printf() and log_write()
+-------------------------------------------
+
+Arbitrary strings are passed to both these functions, but they do their
+formatting by calling the function string_vformat(), which runs through the
+format string itself, and checks the length of each conversion.
+
+
+54.17 Use of strcat() and strcpy()
+----------------------------------
+
+These are used only in cases where the output buffer is known to be large
+enough to hold the result.
+
+
+
+===============================================================================
+55. FORMAT OF SPOOL FILES
+
+A message on Exim's queue consists of two files, whose names are the message id
+followed by -D and -H, respectively. The data portion of the message is kept in
+the -D file on its own. The message's envelope, status, and headers are all
+kept in the -H file, whose format is described in this chapter. Each of these
+two files contains the final component of its own name as its first line. This
+is insurance against disk crashes where the directory is lost but the files
+themselves are recoverable.
+
+Some people are tempted into editing -D files in order to modify messages. You
+need to be extremely careful if you do this; it is not recommended and you are
+on your own if you do it. Here are some of the pitfalls:
+
+  * You must ensure that Exim does not try to deliver the message while you are
+    fiddling with it. The safest way is to take out a write lock on the -D
+    file, which is what Exim itself does, using fcntl(). If you update the file
+    in place, the lock will be retained. If you write a new file and rename it,
+    the lock will be lost at the instant of rename.
+
+  * If you change the number of lines in the file, the value of $body_linecount
+    , which is stored in the -H file, will be incorrect. At present, this value
+    is not used by Exim, but there is no guarantee that this will always be the
+    case.
+
+  * If the message is in MIME format, you must take care not to break it.
+
+  * If the message is cryptographically signed, any change will invalidate the
+    signature.
+
+All in all, modifying -D files is fraught with danger.
+
+Files whose names end with -J may also be seen in the input directory (or its
+subdirectories when split_spool_directory is set). These are journal files,
+used to record addresses to which the message has been delivered during the
+course of a delivery attempt. If there are still undelivered recipients at the
+end, the -H file is updated, and the -J file is deleted. If, however, there is
+some kind of crash (for example, a power outage) before this happens, the -J
+file remains in existence. When Exim next processes the message, it notices the
+-J file and uses it to update the -H file before starting the next delivery
+attempt.
+
+
+55.1 Format of the -H file
+--------------------------
+
+The second line of the -H file contains the login name for the uid of the
+process that called Exim to read the message, followed by the numerical uid and
+gid. For a locally generated message, this is normally the user who sent the
+message. For a message received over TCP/IP via the daemon, it is normally the
+Exim user.
+
+The third line of the file contains the address of the message's sender as
+transmitted in the envelope, contained in angle brackets. The sender address is
+empty for bounce messages. For incoming SMTP mail, the sender address is given
+in the MAIL command. For locally generated mail, the sender address is created
+by Exim from the login name of the current user and the configured 
+qualify_domain. However, this can be overridden by the -f option or a leading
+"From " line if the caller is trusted, or if the supplied address is "<>" or an
+address that matches untrusted_set_senders.
+
+The fourth line contains two numbers. The first is the time that the message
+was received, in the conventional Unix form - the number of seconds since the
+start of the epoch. The second number is a count of the number of messages
+warning of delayed delivery that have been sent to the sender.
+
+There follow a number of lines starting with a hyphen. These can appear in any
+order, and are omitted when not relevant:
+
+-acl <number> <length>
+
+    This item is obsolete, and is not generated from Exim release 4.61 onwards;
+    -aclc and -aclm are used instead. However, -acl is still recognized, to
+    provide backward compatibility. In the old format, a line of this form is
+    present for every ACL variable that is not empty. The number identifies the
+    variable; the acl_cx variables are numbered 0-9 and the acl_mx variables
+    are numbered 10-19. The length is the length of the data string for the
+    variable. The string itself starts at the beginning of the next line, and
+    is followed by a newline character. It may contain internal newlines.
+
+-aclc <rest-of-name> <length>
+
+    A line of this form is present for every ACL connection variable that is
+    defined. Note that there is a space between -aclc and the rest of the name.
+    The length is the length of the data string for the variable. The string
+    itself starts at the beginning of the next line, and is followed by a
+    newline character. It may contain internal newlines.
+
+-aclm <rest-of-name> <length>
+
+    A line of this form is present for every ACL message variable that is
+    defined. Note that there is a space between -aclm and the rest of the name.
+    The length is the length of the data string for the variable. The string
+    itself starts at the beginning of the next line, and is followed by a
+    newline character. It may contain internal newlines.
+
+-active_hostname <hostname>
+
+    This is present if, when the message was received over SMTP, the value of
+    $smtp_active_hostname was different to the value of $primary_hostname.
+
+-allow_unqualified_recipient
+
+    This is present if unqualified recipient addresses are permitted in header
+    lines (to stop such addresses from being qualified if rewriting occurs at
+    transport time). Local messages that were input using -bnq and remote
+    messages from hosts that match recipient_unqualified_hosts set this flag.
+
+-allow_unqualified_sender
+
+    This is present if unqualified sender addresses are permitted in header
+    lines (to stop such addresses from being qualified if rewriting occurs at
+    transport time). Local messages that were input using -bnq and remote
+    messages from hosts that match sender_unqualified_hosts set this flag.
+
+-auth_id <text>
+
+    The id information for a message received on an authenticated SMTP
+    connection - the value of the $authenticated_id variable.
+
+-auth_sender <address>
+
+    The address of an authenticated sender - the value of the
+    $authenticated_sender variable.
+
+-body_linecount <number>
+
+    This records the number of lines in the body of the message, and is always
+    present.
+
+-body_zerocount <number>
+
+    This records the number of binary zero bytes in the body of the message,
+    and is present if the number is greater than zero.
+
+-deliver_firsttime
+
+    This is written when a new message is first added to the spool. When the
+    spool file is updated after a deferral, it is omitted.
+
+-frozen <time>
+
+    The message is frozen, and the freezing happened at <time>.
+
+-helo_name <text>
+
+    This records the host name as specified by a remote host in a HELO or EHLO
+    command.
+
+-host_address <address>.<port>
+
+    This records the IP address of the host from which the message was received
+    and the remote port number that was used. It is omitted for locally
+    generated messages.
+
+-host_auth <text>
+
+    If the message was received on an authenticated SMTP connection, this
+    records the name of the authenticator - the value of the
+    $sender_host_authenticated variable.
+
+-host_lookup_failed
+
+    This is present if an attempt to look up the sending host's name from its
+    IP address failed. It corresponds to the $host_lookup_failed variable.
+
+-host_name <text>
+
+    This records the name of the remote host from which the message was
+    received, if the host name was looked up from the IP address when the
+    message was being received. It is not present if no reverse lookup was
+    done.
+
+-ident <text>
+
+    For locally submitted messages, this records the login of the originating
+    user, unless it was a trusted user and the -oMt option was used to specify
+    an ident value. For messages received over TCP/IP, this records the ident
+    string supplied by the remote host, if any.
+
+-interface_address <address>.<port>
+
+    This records the IP address of the local interface and the port number
+    through which a message was received from a remote host. It is omitted for
+    locally generated messages.
+
+-local
+
+    The message is from a local sender.
+
+-localerror
+
+    The message is a locally-generated bounce message.
+
+-local_scan <string>
+
+    This records the data string that was returned by the local_scan() function
+    when the message was received - the value of the $local_scan_data variable.
+    It is omitted if no data was returned.
+
+-manual_thaw
+
+    The message was frozen but has been thawed manually, that is, by an
+    explicit Exim command rather than via the auto-thaw process.
+
+-N
+
+    A testing delivery process was started using the -N option to suppress any
+    actual deliveries, but delivery was deferred. At any further delivery
+    attempts, -N is assumed.
+
+-received_protocol
+
+    This records the value of the $received_protocol variable, which contains
+    the name of the protocol by which the message was received.
+
+-sender_set_untrusted
+
+    The envelope sender of this message was set by an untrusted local caller
+    (used to ensure that the caller is displayed in queue listings).
+
+-spam_score_int <number>
+
+    If a message was scanned by SpamAssassin, this is present. It records the
+    value of $spam_score_int.
+
+-tls_certificate_verified
+
+    A TLS certificate was received from the client that sent this message, and
+    the certificate was verified by the server.
+
+-tls_cipher <cipher name>
+
+    When the message was received over an encrypted connection, this records
+    the name of the cipher suite that was used.
+
+-tls_peerdn <peer DN>
+
+    When the message was received over an encrypted connection, and a
+    certificate was received from the client, this records the Distinguished
+    Name from that certificate.
+
+Following the options there is a list of those addresses to which the message
+is not to be delivered. This set of addresses is initialized from the command
+line when the -t option is used and extract_addresses_remove_arguments is set;
+otherwise it starts out empty. Whenever a successful delivery is made, the
+address is added to this set. The addresses are kept internally as a balanced
+binary tree, and it is a representation of that tree which is written to the
+spool file. If an address is expanded via an alias or forward file, the
+original address is added to the tree when deliveries to all its child
+addresses are complete.
+
+If the tree is empty, there is a single line in the spool file containing just
+the text "XX". Otherwise, each line consists of two letters, which are either Y
+or N, followed by an address. The address is the value for the node of the
+tree, and the letters indicate whether the node has a left branch and/or a
+right branch attached to it, respectively. If branches exist, they immediately
+follow. Here is an example of a three-node tree:
+
+YY darcy@austen.fict.example
+NN alice@wonderland.fict.example
+NN editor@thesaurus.ref.example
+
+After the non-recipients tree, there is a list of the message's recipients.
+This is a simple list, preceded by a count. It includes all the original
+recipients of the message, including those to whom the message has already been
+delivered. In the simplest case, the list contains one address per line. For
+example:
+
+4
+editor@thesaurus.ref.example
+darcy@austen.fict.example
+rdo@foundation
+alice@wonderland.fict.example
+
+However, when a child address has been added to the top-level addresses as a
+result of the use of the one_time option on a redirect router, each line is of
+the following form:
+
+<top-level address> <errors_to address> <length>,<parent number>#<flag bits>
+
+The 01 flag bit indicates the presence of the three other fields that follow
+the top-level address. Other bits may be used in future to support additional
+fields. The <parent number> is the offset in the recipients list of the
+original parent of the "one time" address. The first two fields are the
+envelope sender that is associated with this address and its length. If the
+length is zero, there is no special envelope sender (there are then two space
+characters in the line). A non-empty field can arise from a redirect router
+that has an errors_to setting.
+
+A blank line separates the envelope and status information from the headers
+which follow. A header may occupy several lines of the file, and to save effort
+when reading it in, each header is preceded by a number and an identifying
+character. The number is the number of characters in the header, including any
+embedded newlines and the terminating newline. The character is one of the
+following:
+
+<blank> header in which Exim has no special interest
+"B"     Bcc: header
+"C"     Cc: header
+"F"     From: header
+"I"     Message-id: header
+"P"     Received: header - P for "postmark"
+"R"     Reply-To: header
+"S"     Sender: header
+"T"     To: header
+"*"     replaced or deleted header
+
+Deleted or replaced (rewritten) headers remain in the spool file for debugging
+purposes. They are not transmitted when the message is delivered. Here is a
+typical set of headers:
+
+111P Received: by hobbit.fict.example with local (Exim 4.00)
+id 14y9EI-00026G-00; Fri, 11 May 2001 10:28:59 +0100
+049  Message-Id: <E14y9EI-00026G-00@hobbit.fict.example>
+038* X-rewrote-sender: bb@hobbit.fict.example
+042* From: Bilbo Baggins <bb@hobbit.fict.example>
+049F From: Bilbo Baggins <B.Baggins@hobbit.fict.example>
+099* To: alice@wonderland.fict.example, rdo@foundation,
+darcy@austen.fict.example, editor@thesaurus.ref.example
+104T To: alice@wonderland.fict.example, rdo@foundation.example,
+darcy@austen.fict.example, editor@thesaurus.ref.example
+038  Date: Fri, 11 May 2001 10:28:59 +0100
+
+The asterisked headers indicate that the envelope sender, From: header, and To:
+header have been rewritten, the last one because routing expanded the
+unqualified domain foundation.
+
+
+
+===============================================================================
+56. SUPPORT FOR DKIM (DOMAINKEYS IDENTIFIED MAIL)
+
+DKIM is a mechanism by which messages sent by some entity can be provably
+linked to a domain which that entity controls. It permits reputation to be
+tracked on a per-domain basis, rather than merely upon source IP address. DKIM
+is documented in RFC 4871.
+
+Since version 4.70, DKIM support is compiled into Exim by default. It can be
+disabled by setting DISABLE_DKIM=yes in Local/Makefile.
+
+Exim's DKIM implementation allows to
+
+ 1. Sign outgoing messages: This function is implemented in the SMTP transport.
+    It can co-exist with all other Exim features (including transport filters)
+    except cutthrough delivery.
+
+ 2. Verify signatures in incoming messages: This is implemented by an
+    additional ACL (acl_smtp_dkim), which can be called several times per
+    message, with different signature contexts.
+
+In typical Exim style, the verification implementation does not include any
+default "policy". Instead it enables you to build your own policy using Exim's
+standard controls.
+
+Please note that verification of DKIM signatures in incoming mail is turned on
+by default for logging purposes. For each signature in incoming email, exim
+will log a line displaying the most important signature details, and the
+signature status. Here is an example (with line-breaks added for clarity):
+
+2009-09-09 10:22:28 1MlIRf-0003LU-U3 DKIM:
+    d=facebookmail.com s=q1-2009b
+    c=relaxed/relaxed a=rsa-sha1
+    i=@facebookmail.com t=1252484542 [verification succeeded]
+
+You might want to turn off DKIM verification processing entirely for internal
+or relay mail sources. To do that, set the dkim_disable_verify ACL control
+modifier. This should typically be done in the RCPT ACL, at points where you
+accept mail from relay sources (internal hosts or authenticated senders).
+
+
+56.1 Signing outgoing messages
+------------------------------
+
+Signing is implemented by setting private options on the SMTP transport. These
+options take (expandable) strings as arguments.
+
++-----------+---------+-------------+--------------+
+|dkim_domain|Use: smtp|Type: string*|Default: unset|
++-----------+---------+-------------+--------------+
+
+MANDATORY: The domain you want to sign with. The result of this expanded option
+is put into the $dkim_domain expansion variable.
+
++-------------+---------+-------------+--------------+
+|dkim_selector|Use: smtp|Type: string*|Default: unset|
++-------------+---------+-------------+--------------+
+
+MANDATORY: This sets the key selector string. You can use the $dkim_domain
+expansion variable to look up a matching selector. The result is put in the
+expansion variable $dkim_selector which should be used in the dkim_private_key
+option along with $dkim_domain.
+
++----------------+---------+-------------+--------------+
+|dkim_private_key|Use: smtp|Type: string*|Default: unset|
++----------------+---------+-------------+--------------+
+
+MANDATORY: This sets the private key to use. You can use the $dkim_domain and 
+$dkim_selector expansion variables to determine the private key to use. The
+result can either
+
+  * be a valid RSA private key in ASCII armor, including line breaks.
+
+  * start with a slash, in which case it is treated as a file that contains the
+    private key.
+
+  * be "0", "false" or the empty string, in which case the message will not be
+    signed. This case will not result in an error, even if dkim_strict is set.
+
++----------+---------+-------------+--------------+
+|dkim_canon|Use: smtp|Type: string*|Default: unset|
++----------+---------+-------------+--------------+
+
+OPTIONAL: This option sets the canonicalization method used when signing a
+message. The DKIM RFC currently supports two methods: "simple" and "relaxed".
+The option defaults to "relaxed" when unset. Note: the current implementation
+only supports using the same canonicalization method for both headers and body.
+
++-----------+---------+-------------+--------------+
+|dkim_strict|Use: smtp|Type: string*|Default: unset|
++-----------+---------+-------------+--------------+
+
+OPTIONAL: This option defines how Exim behaves when signing a message that
+should be signed fails for some reason. When the expansion evaluates to either
+"1" or "true", Exim will defer. Otherwise Exim will send the message unsigned.
+You can use the $dkim_domain and $dkim_selector expansion variables here.
+
++-----------------+---------+-------------+--------------+
+|dkim_sign_headers|Use: smtp|Type: string*|Default: unset|
++-----------------+---------+-------------+--------------+
+
+OPTIONAL: When set, this option must expand to (or be specified as) a
+colon-separated list of header names. Headers with these names will be included
+in the message signature. When unspecified, the header names recommended in
+RFC4871 will be used.
+
+
+56.2 Verifying DKIM signatures in incoming mail
+-----------------------------------------------
+
+Verification of DKIM signatures in incoming email is implemented via the 
+acl_smtp_dkim ACL. By default, this ACL is called once for each syntactically
+(!) correct signature in the incoming message. A missing ACL definition
+defaults to accept. If any ACL call does not acccept, the message is not
+accepted. If a cutthrough delivery was in progress for the message it is
+summarily dropped (having wasted the transmission effort).
+
+To evaluate the signature in the ACL a large number of expansion variables
+containing the signature status and its details are set up during the runtime
+of the ACL.
+
+Calling the ACL only for existing signatures is not sufficient to build more
+advanced policies. For that reason, the global option dkim_verify_signers, and
+a global expansion variable $dkim_signers exist.
+
+The global option dkim_verify_signers can be set to a colon-separated list of
+DKIM domains or identities for which the ACL acl_smtp_dkim is called. It is
+expanded when the message has been received. At this point, the expansion
+variable $dkim_signers already contains a colon-separated list of signer
+domains and identities for the message. When dkim_verify_signers is not
+specified in the main configuration, it defaults as:
+
+dkim_verify_signers = $dkim_signers
+
+This leads to the default behaviour of calling acl_smtp_dkim for each DKIM
+signature in the message. Current DKIM verifiers may want to explicitly call
+the ACL for known domains or identities. This would be achieved as follows:
+
+dkim_verify_signers = paypal.com:ebay.com:$dkim_signers
+
+This would result in acl_smtp_dkim always being called for "paypal.com" and
+"ebay.com", plus all domains and identities that have signatures in the
+message. You can also be more creative in constructing your policy. For
+example:
+
+dkim_verify_signers = $sender_address_domain:$dkim_signers
+
+If a domain or identity is listed several times in the (expanded) value of 
+dkim_verify_signers, the ACL is only called once for that domain or identity.
+
+Inside the acl_smtp_dkim, the following expansion variables are available (from
+most to least important):
+
+$dkim_cur_signer
+
+    The signer that is being evaluated in this ACL run. This can be a domain or
+    an identity. This is one of the list items from the expanded main option 
+    dkim_verify_signers (see above).
+
+$dkim_verify_status
+
+    A string describing the general status of the signature. One of
+
+      * none: There is no signature in the message for the current domain or
+        identity (as reflected by $dkim_cur_signer).
+
+      * invalid: The signature could not be verified due to a processing error.
+        More detail is available in $dkim_verify_reason.
+
+      * fail: Verification of the signature failed. More detail is available in
+        $dkim_verify_reason.
+
+      * pass: The signature passed verification. It is valid.
+
+$dkim_verify_reason
+
+    A string giving a litte bit more detail when $dkim_verify_status is either
+    "fail" or "invalid". One of
+
+      * pubkey_unavailable (when $dkim_verify_status="invalid"): The public key
+        for the domain could not be retrieved. This may be a temporary problem.
+
+      * pubkey_syntax (when $dkim_verify_status="invalid"): The public key
+        record for the domain is syntactically invalid.
+
+      * bodyhash_mismatch (when $dkim_verify_status="fail"): The calculated
+        body hash does not match the one specified in the signature header.
+        This means that the message body was modified in transit.
+
+      * signature_incorrect (when $dkim_verify_status="fail"): The signature
+        could not be verified. This may mean that headers were modified,
+        re-written or otherwise changed in a way which is incompatible with
+        DKIM verification. It may of course also mean that the signature is
+        forged.
+
+$dkim_domain
+
+    The signing domain. IMPORTANT: This variable is only populated if there is
+    an actual signature in the message for the current domain or identity (as
+    reflected by $dkim_cur_signer).
+
+$dkim_identity
+
+    The signing identity, if present. IMPORTANT: This variable is only
+    populated if there is an actual signature in the message for the current
+    domain or identity (as reflected by $dkim_cur_signer).
+
+$dkim_selector
+
+    The key record selector string.
+
+$dkim_algo
+
+    The algorithm used. One of 'rsa-sha1' or 'rsa-sha256'.
+
+$dkim_canon_body
+
+    The body canonicalization method. One of 'relaxed' or 'simple'.
+
+dkim_canon_headers
+
+    The header canonicalization method. One of 'relaxed' or 'simple'.
+
+$dkim_copiedheaders
+
+    A transcript of headers and their values which are included in the
+    signature (copied from the 'z=' tag of the signature).
+
+$dkim_bodylength
+
+    The number of signed body bytes. If zero ("0"), the body is unsigned. If no
+    limit was set by the signer, "9999999999999" is returned. This makes sure
+    that this variable always expands to an integer value.
+
+$dkim_created
+
+    UNIX timestamp reflecting the date and time when the signature was created.
+    When this was not specified by the signer, "0" is returned.
+
+$dkim_expires
+
+    UNIX timestamp reflecting the date and time when the signer wants the
+    signature to be treated as "expired". When this was not specified by the
+    signer, "9999999999999" is returned. This makes it possible to do useful
+    integer size comparisons against this value.
+
+$dkim_headernames
+
+    A colon-separated list of names of headers included in the signature.
+
+$dkim_key_testing
+
+    "1" if the key record has the "testing" flag set, "0" if not.
+
+$dkim_key_nosubdomains
+
+    "1" if the key record forbids subdomaining, "0" otherwise.
+
+$dkim_key_srvtype
+
+    Service type (tag s=) from the key record. Defaults to "*" if not specified
+    in the key record.
+
+$dkim_key_granularity
+
+    Key granularity (tag g=) from the key record. Defaults to "*" if not
+    specified in the key record.
+
+$dkim_key_notes
+
+    Notes from the key record (tag n=).
+
+In addition, two ACL conditions are provided:
+
+dkim_signers
+
+    ACL condition that checks a colon-separated list of domains or identities
+    for a match against the domain or identity that the ACL is currently
+    verifying (reflected by $dkim_cur_signer). This is typically used to
+    restrict an ACL verb to a group of domains or identities. For example:
+
+    # Warn when Mail purportedly from GMail has no signature at all
+    warn log_message = GMail sender without DKIM signature
+         sender_domains = gmail.com
+         dkim_signers = gmail.com
+         dkim_status = none
+
+dkim_status
+
+    ACL condition that checks a colon-separated list of possible DKIM
+    verification results against the actual result of verification. This is
+    typically used to restrict an ACL verb to a list of verification outcomes,
+    for example:
+
+    deny message = Mail from Paypal with invalid/missing signature
+         sender_domains = paypal.com:paypal.de
+         dkim_signers = paypal.com:paypal.de
+         dkim_status = none:invalid:fail
+
+    The possible status keywords are: 'none','invalid','fail' and 'pass'.
+    Please see the documentation of the $dkim_verify_status expansion variable
+    above for more information of what they mean.
+
+
+
+===============================================================================
+57. ADDING NEW DRIVERS OR LOOKUP TYPES
+
+The following actions have to be taken in order to add a new router, transport,
+authenticator, or lookup type to Exim:
+
+ 1. Choose a name for the driver or lookup type that does not conflict with any
+    existing name; I will use "newdriver" in what follows.
+
+ 2. Add to src/EDITME the line:
+
+    <type>_NEWDRIVER=yes
+
+    where <type> is ROUTER, TRANSPORT, AUTH, or LOOKUP. If the code is not to
+    be included in the binary by default, comment this line out. You should
+    also add any relevant comments about the driver or lookup type.
+
+ 3. Add to src/config.h.defaults the line:
+
+    #define <type>_NEWDRIVER
+
+ 4. Edit src/drtables.c, adding conditional code to pull in the private header
+    and create a table entry as is done for all the other drivers and lookup
+    types.
+
+ 5. Edit scripts/lookups-Makefile if this is a new lookup; there is a for-loop
+    near the bottom, ranging the "name_mod" variable over a list of all
+    lookups. Add your "NEWDRIVER" to that list. As long as the dynamic module
+    would be named newdriver.so, you can use the simple form that most lookups
+    have.
+
+ 6. Edit Makefile in the appropriate sub-directory (src/routers, src/transports
+    , src/auths, or src/lookups); add a line for the new driver or lookup type
+    and add it to the definition of OBJ.
+
+ 7. Create newdriver.h and newdriver.c in the appropriate sub-directory of src.
+
+ 8. Edit scripts/MakeLinks and add commands to link the .h and .c files as for
+    other drivers and lookups.
+
+Then all you need to do is write the code! A good way to start is to make a
+proforma by copying an existing module of the same type, globally changing all
+occurrences of the name, and cutting out most of the code. Note that any
+options you create must be listed in alphabetical order, because the tables are
+searched using a binary chop procedure.
+
+There is a README file in each of the sub-directories of src describing the
+interface that is expected.
+
diff --git a/exim_monitor/EDITME b/exim_monitor/EDITME
new file mode 100644 (file)
index 0000000..a70b7cb
--- /dev/null
@@ -0,0 +1,179 @@
+##################################################
+#                The Exim Monitor                #
+##################################################
+
+# This is the template for the Exim monitor's main build-time configuration
+# file. It contains settings that are independent of any operating system. It
+# should be edited and then saved to a file called Local/eximon.conf before
+# running the make command to build the monitor, if any settings are required.
+# Local/eximon.conf can be empty if no changes are needed. The examples given
+# here (commented out) are the default settings.
+
+# Any settings made in the configuration file can be overridden at run time
+# by setting up an environment variable with the same name as any of these
+# options, but preceded by EXIMON_, for example, EXIMON_WINDOW_TITLE.
+
+
+##################################################################
+#      Set these variables as appropriate for your system        #
+##################################################################
+
+# The qualifying name for your domain. The only use made of this is for
+# testing that certain addresses are the same when displaying the
+# log tail, and for shortening sender addresses in the queue display.
+
+# QUALIFY_DOMAIN=
+
+# The default minimum width and height for the whole window are 103 and
+# 162 pixels respectively. This is enough to hold the left-most stripchart
+# and the quit button. The values can be changed here.
+
+# MIN_HEIGHT=162
+# MIN_WIDTH=103
+
+# If you uncomment the following setting, the window will start up at
+# its minimum size, instead of the default maximum. There may be a quick
+# flash during the start-up process. Defining it this way allows it to be
+# overridden by an environment variable.
+
+# START_SMALL=${EXIMON_START_SMALL-yes}
+
+# The title for eximon's main display window. It is possible to have
+# host name of the machine you are running on substituted into the
+# title string. If you include the string ${fullhostname} then the
+# complete name is used. If you include ${hostname} then the full
+# host name will have the string contained in the DOMAIN variable
+# stripped from its right-hand end before being substituted. Any other
+# shell or environment variables may also be included.
+
+# If you use any substitutions, remember to ensure that the $ and {}
+# characters are escaped from the shell, e.g. by using single quotes.
+
+# WINDOW_TITLE="${hostname} eximon"
+
+# The domain that you want to be stripped from the machine's full hostname
+# when forming the short host name for the eximon window title, as
+# described above.
+
+# DOMAIN=
+
+# Parameters for the rolling display of the tail of the exim log file.
+# The width and depth are measured in pixels; LOG_BUFFER specifies the
+# amount of store to set aside for holding the log tail, which is displayed
+# in a scrolling window. When this store is full, the earlier 50% of it
+# is discarded - this is much more efficient that throwing it away line
+# by line. The number given can be followed by the letter K to indicate
+# that the value is in kilobytes. A minimum value of 1K is enforced.
+
+# LOG_DEPTH=300
+# LOG_WIDTH=950
+# LOG_BUFFER=20K
+
+# The font which is used in the log tail display. This is defined in
+# the normal X manner. It must be a "character cell" font, because this
+# is required by the text widget.
+
+# LOG_FONT=-misc-fixed-medium-r-normal-*-14-140-*-*-*-*-iso8859-1
+
+# Parameters for the display of message that are on the exim queue.
+# The width and depth are measured in pixels.
+
+# QUEUE_DEPTH=200
+# QUEUE_WIDTH=950
+
+# The font which is used in the queue display.
+
+# QUEUE_FONT=$LOG_FONT
+
+# When a message has more than one undelivered address, they are listed
+# one below the other. A limit can be placed on the number of addresses
+# displayed for any one message. If there are more, then "..." is used
+# to indicate this.
+
+# QUEUE_MAX_ADDRESSES=10
+
+# The display of the contents of the queue is updated every QUEUE_INTERVAL
+# seconds by default (there is a button to request update).
+
+# QUEUE_INTERVAL=300
+
+# The size of the popup text window that is used for looking at the
+# contents of messages, etc.
+
+# TEXT_DEPTH=200
+
+# The keystroke/mouse-operation that is used to pop up the menu in the
+# queue window is configurable. The default is Shift with the lefthand
+# mouse button. The name of an alternative can be specified in the standard
+# X way of naming these things. With the default configuration for the monitor,
+# individuals can override this by setting the EXIMON_MENU_EVENT environment
+# variable.
+
+# MENU_EVENT='Shift<Btn1Down>'
+
+# When the menu is used to perform an operation on a message, the result of the
+# operation is normally visible in the log window, so Eximon doesn't display
+# the output of the generated Exim command. However, you can request that
+# this output be shown in a separate window by setting ACTION_OUTPUT to "yes".
+# This does not apply to the output generated from attempting to deliver a
+# message, which is always shown.
+
+# ACTION_OUTPUT=no
+
+# When some action is taken on a message, such as freezing it, or changing
+# its recipients, the queue display is normally automatically updated. On
+# systems that have very large queues, this can take some time and be dis-
+# tracting. If this option is set to "no", the queue display is no longer
+# automatically updated after an action is applied to a message.
+
+# ACTION_QUEUE_UPDATE=yes
+
+# When the menu item to display a message's body is invoked, the amount
+# of data is limited to BODY_MAX bytes. This limit is a safety precaution
+# to save the screen scrolling for ever on an enormous message.
+
+# BODY_MAX=20000
+
+# The stripcharts are updated every STRIPCHART_INTERVAL seconds.
+
+# STRIPCHART_INTERVAL=60
+
+# A stripchart showing the count of messages in the queue is always
+# displayed on the left of eximon's window. Its name is "queue" by
+# default, but can be changed by this variable.
+
+# QUEUE_STRIPCHART_NAME=queue
+
+# The following variable may be set to the name of a disc partition. If
+# it is, a stripchart showing the percentage fullness of the partition
+# will be displayed as the second stripchart. This can be used to keep
+# a display of a mail spool partition on the screen.
+
+# SIZE_STRIPCHART=/var/mail
+
+# The name of the size stripchart will be the last component of SIZE_STRIPCHART
+# unless the following variable is set to override it.
+
+# SIZE_STRIPCHART_NAME=space
+
+# The following variable contains a specification of which stripcharts
+# you want eximon to display based on log entries. The string consists of
+# pairs of strings, delimited by slash characters. The first string in each
+# pair is a regular expression that matches some distinguishing feature in a
+# exim log entry.
+
+# Entries that match the expression will be counted and displayed in a
+# stripchart whose title is given by the second string. The string may
+# be continued over several input lines, provided that it is split
+# after a slash, and an additional slash (optionally preceded by white
+# space) is included at the start of the continuation line.
+
+# Stripcharts configured by the following parameter are displayed to the
+# right of the queue and size stripcharts, in the order defined here.
+
+# LOG_STRIPCHARTS='/ <= /in/
+#                  / => /out/
+#                  / => .+ R=local/local/
+#                  / => .+ T=[^ ]*smtp/smtp/'
+
+# End of exim_monitor/EDITME
diff --git a/exim_monitor/em_StripChart.c b/exim_monitor/em_StripChart.c
new file mode 100644 (file)
index 0000000..2409c40
--- /dev/null
@@ -0,0 +1,504 @@
+/***********************************************************
+Copyright 1987, 1988 by Digital Equipment Corporation, Maynard, Massachusetts,
+and the Massachusetts Institute of Technology, Cambridge, Massachusetts.
+
+                        All Rights Reserved
+
+Permission to use, copy, modify, and distribute this software and its
+documentation for any purpose and without fee is hereby granted,
+provided that the above copyright notice appear in all copies and that
+both that copyright notice and this permission notice appear in
+supporting documentation, and that the names of Digital or MIT not be
+used in advertising or publicity pertaining to distribution of the
+software without specific, written prior permission.
+
+DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
+ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
+DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
+ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
+WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
+ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
+SOFTWARE.
+
+******************************************************************/
+
+/* This is the Athena StripChart widget, slightly hacked by
+Philip Hazel <ph10@cus.cam.ac.uk> in order to give access to
+its repaint_window function so that a repaint can be forced.
+
+The repaint_window function has also been nobbled so that it only
+ever changes scale to 10. There is probably a better way to handle
+this - such as inventing some new resources, but I'm not up to
+that just at the moment.
+
+On SunOS4 there are name clashes when trying to link this with the
+Athena library. So to avoid them, rename a few things by inserting
+"my" at the front of "strip". */
+
+
+#include <stdio.h>
+#include <X11/IntrinsicP.h>
+#include <X11/StringDefs.h>
+#include <X11/Xaw/XawInit.h>
+#include <X11/Xaw/StripCharP.h>
+#include <X11/Xfuncs.h>
+
+#define MS_PER_SEC 1000
+
+/* Private Data */
+
+#define offset(field) XtOffsetOf(StripChartRec, field)
+
+static XtResource resources[] = {
+    {XtNwidth, XtCWidth, XtRDimension, sizeof(Dimension),
+       offset(core.width), XtRImmediate, (XtPointer) 120},
+    {XtNheight, XtCHeight, XtRDimension, sizeof(Dimension),
+       offset(core.height), XtRImmediate, (XtPointer) 120},
+    {XtNupdate, XtCInterval, XtRInt, sizeof(int),
+        offset(strip_chart.update), XtRImmediate, (XtPointer) 10},
+    {XtNminScale, XtCScale, XtRInt, sizeof(int),
+        offset(strip_chart.min_scale), XtRImmediate, (XtPointer) 1},
+    {XtNforeground, XtCForeground, XtRPixel, sizeof(Pixel),
+        offset(strip_chart.fgpixel), XtRString, XtDefaultForeground},
+    {XtNhighlight, XtCForeground, XtRPixel, sizeof(Pixel),
+        offset(strip_chart.hipixel), XtRString, XtDefaultForeground},
+    {XtNgetValue, XtCCallback, XtRCallback, sizeof(XtPointer),
+        offset(strip_chart.get_value), XtRImmediate, (XtPointer) NULL},
+    {XtNjumpScroll, XtCJumpScroll, XtRInt, sizeof(int),
+        offset(strip_chart.jump_val), XtRImmediate, (XtPointer) DEFAULT_JUMP},
+};
+
+#undef offset
+
+/* Added argument types to these to shut picky compilers up. PH */
+
+static void CreateGC(StripChartWidget, unsigned int);
+static void DestroyGC(StripChartWidget, unsigned int);
+static void Initialize(), Destroy(), Redisplay();
+static void MoveChart(StripChartWidget, Boolean);
+static void SetPoints(StripChartWidget);
+static Boolean SetValues();
+
+int repaint_window(StripChartWidget, int, int);     /* PH hack */
+/* static int repaint_window(); */
+
+StripChartClassRec stripChartClassRec = {
+    { /* core fields */
+    /* superclass              */      (WidgetClass) &simpleClassRec,
+    /* class_name              */      "StripChart",
+    /* size                    */      sizeof(StripChartRec),
+    /* class_initialize                */      XawInitializeWidgetSet,
+    /* class_part_initialize   */      NULL,
+    /* class_inited            */      FALSE,
+    /* initialize              */      Initialize,
+    /* initialize_hook         */      NULL,
+    /* realize                 */      XtInheritRealize,
+    /* actions                 */      NULL,
+    /* num_actions             */      0,
+    /* resources               */      resources,
+    /* num_resources           */      XtNumber(resources),
+    /* xrm_class               */      NULLQUARK,
+    /* compress_motion         */      TRUE,
+    /* compress_exposure       */      XtExposeCompressMultiple |
+                                       XtExposeGraphicsExposeMerged,
+    /* compress_enterleave     */      TRUE,
+    /* visible_interest                */      FALSE,
+    /* destroy                 */      Destroy,
+    /* resize                  */      (void (*)(Widget))SetPoints,
+    /* expose                  */      Redisplay,
+    /* set_values              */      SetValues,
+    /* set_values_hook         */      NULL,
+    /* set_values_almost       */      NULL,
+    /* get_values_hook         */      NULL,
+    /* accept_focus            */      NULL,
+    /* version                 */      XtVersion,
+    /* callback_private                */      NULL,
+    /* tm_table                        */      NULL,
+    /* query_geometry          */      XtInheritQueryGeometry,
+    /* display_accelerator     */      XtInheritDisplayAccelerator,
+    /* extension               */      NULL
+    },
+    { /* Simple class fields */
+    /* change_sensitive                */      XtInheritChangeSensitive
+    }
+};
+
+WidgetClass mystripChartWidgetClass = (WidgetClass) &stripChartClassRec;
+
+/****************************************************************
+ *
+ * Private Procedures
+ *
+ ****************************************************************/
+
+static void draw_it();
+
+/*     Function Name: CreateGC
+ *     Description: Creates the GC's
+ *     Arguments: w - the strip chart widget.
+ *                 which - which GC's to create.
+ *     Returns: none
+ */
+
+static void
+CreateGC(w, which)
+StripChartWidget w;
+unsigned int which;
+{
+  XGCValues    myXGCV;
+
+  if (which & FOREGROUND) {
+    myXGCV.foreground = w->strip_chart.fgpixel;
+    w->strip_chart.fgGC = XtGetGC((Widget) w, GCForeground, &myXGCV);
+  }
+
+  if (which & HIGHLIGHT) {
+    myXGCV.foreground = w->strip_chart.hipixel;
+    w->strip_chart.hiGC = XtGetGC((Widget) w, GCForeground, &myXGCV);
+  }
+}
+
+/*     Function Name: DestroyGC
+ *     Description: Destroys the GC's
+ *     Arguments: w - the strip chart widget.
+ *                 which - which GC's to destroy.
+ *     Returns: none
+ */
+
+static void
+DestroyGC(w, which)
+StripChartWidget w;
+unsigned int which;
+{
+  if (which & FOREGROUND)
+    XtReleaseGC((Widget) w, w->strip_chart.fgGC);
+
+  if (which & HIGHLIGHT)
+    XtReleaseGC((Widget) w, w->strip_chart.hiGC);
+}
+
+/* ARGSUSED */
+static void Initialize (greq, gnew)
+    Widget greq, gnew;
+{
+    StripChartWidget w = (StripChartWidget)gnew;
+
+    if (w->strip_chart.update > 0)
+        w->strip_chart.interval_id = XtAppAddTimeOut(
+                                       XtWidgetToApplicationContext(gnew),
+                                       w->strip_chart.update * MS_PER_SEC,
+                                       draw_it, (XtPointer) gnew);
+    CreateGC(w, (unsigned int) ALL_GCS);
+
+    w->strip_chart.scale = w->strip_chart.min_scale;
+    w->strip_chart.interval = 0;
+    w->strip_chart.max_value = 0.0;
+    w->strip_chart.points = NULL;
+    SetPoints(w);
+}
+
+static void Destroy (gw)
+     Widget gw;
+{
+     StripChartWidget w = (StripChartWidget)gw;
+
+     if (w->strip_chart.update > 0)
+         XtRemoveTimeOut (w->strip_chart.interval_id);
+     if (w->strip_chart.points)
+        XtFree((char *) w->strip_chart.points);
+     DestroyGC(w, (unsigned int) ALL_GCS);
+}
+
+/*
+ * NOTE: This function really needs to recieve graphics exposure
+ *       events, but since this is not easily supported until R4 I am
+ *       going to hold off until then.
+ */
+
+/* ARGSUSED */
+static void Redisplay(w, event, region)
+     Widget w;
+     XEvent *event;
+     Region region;
+{
+    if (event->type == GraphicsExpose)
+       (void) repaint_window ((StripChartWidget)w, event->xgraphicsexpose.x,
+                              event->xgraphicsexpose.width);
+    else
+       (void) repaint_window ((StripChartWidget)w, event->xexpose.x,
+                              event->xexpose.width);
+}
+
+/* ARGSUSED */
+static void
+draw_it(client_data, id)
+XtPointer client_data;
+XtIntervalId *id;              /* unused */
+{
+   StripChartWidget w = (StripChartWidget)client_data;
+   double value;
+
+   if (w->strip_chart.update > 0)
+       w->strip_chart.interval_id =
+       XtAppAddTimeOut(XtWidgetToApplicationContext( (Widget) w),
+                      w->strip_chart.update * MS_PER_SEC,draw_it,client_data);
+
+   if (w->strip_chart.interval >= (int)w->core.width)
+       MoveChart( (StripChartWidget) w, TRUE);
+
+   /* Get the value, stash the point and draw corresponding line. */
+
+   if (w->strip_chart.get_value == NULL)
+       return;
+
+   XtCallCallbacks( (Widget)w, XtNgetValue, (XtPointer)&value );
+
+   /*
+    * Keep w->strip_chart.max_value up to date, and if this data
+    * point is off the graph, change the scale to make it fit.
+    */
+
+   if (value > w->strip_chart.max_value) {
+       w->strip_chart.max_value = value;
+       if (w->strip_chart.max_value > w->strip_chart.scale) {
+          XClearWindow( XtDisplay (w), XtWindow (w));
+          w->strip_chart.interval = repaint_window(w, 0, (int) w->core.width);
+       }
+   }
+
+   w->strip_chart.valuedata[w->strip_chart.interval] = value;
+   if (XtIsRealized((Widget)w)) {
+       int y = (int) (w->core.height
+                     - (int)(w->core.height * value) / w->strip_chart.scale);
+
+       XFillRectangle(XtDisplay(w), XtWindow(w), w->strip_chart.fgGC,
+                     w->strip_chart.interval, y,
+                     (unsigned int) 1, w->core.height - y);
+       /*
+       * Fill in the graph lines we just painted over.
+       */
+
+       if (w->strip_chart.points != NULL) {
+          w->strip_chart.points[0].x = w->strip_chart.interval;
+          XDrawPoints(XtDisplay(w), XtWindow(w), w->strip_chart.hiGC,
+                      w->strip_chart.points, w->strip_chart.scale - 1,
+                      CoordModePrevious);
+       }
+
+       XFlush(XtDisplay(w));               /* Flush output buffers */
+   }
+   w->strip_chart.interval++;              /* Next point */
+} /* draw_it */
+
+/* Blts data according to current size, then redraws the stripChart window.
+ * Next represents the number of valid points in data.  Returns the (possibly)
+ * adjusted value of next.  If next is 0, this routine draws an empty window
+ * (scale - 1 lines for graph).  If next is less than the current window width,
+ * the returned value is identical to the initial value of next and data is
+ * unchanged.  Otherwise keeps half a window's worth of data.  If data is
+ * changed, then w->strip_chart.max_value is updated to reflect the
+ * largest data point.
+ */
+
+/* static int */
+int              /* PH hack */
+repaint_window(w, left, width)
+StripChartWidget w;
+int left, width;
+{
+    register int i, j;
+    register int next = w->strip_chart.interval;
+    int scale = w->strip_chart.scale;
+    int scalewidth = 0;
+
+    /* Compute the minimum scale required to graph the data, but don't go
+       lower than min_scale. */
+    if (w->strip_chart.interval != 0 || scale <= (int)w->strip_chart.max_value)
+      scale = ((int) (w->strip_chart.max_value)) + 1;
+    if (scale < w->strip_chart.min_scale)
+      scale = w->strip_chart.min_scale;
+
+/*    if (scale != w->strip_chart.scale) { */
+
+    if (scale != w->strip_chart.scale && scale == 10) {
+      w->strip_chart.scale = scale;
+      left = 0;
+      width = next;
+      scalewidth = w->core.width;
+
+      SetPoints(w);
+
+      if (XtIsRealized ((Widget) w))
+       XClearWindow (XtDisplay (w), XtWindow (w));
+
+    }
+
+    if (XtIsRealized((Widget)w)) {
+       Display *dpy = XtDisplay(w);
+       Window win = XtWindow(w);
+
+       width += left - 1;
+       if (!scalewidth) scalewidth = width;
+
+       if (next < ++width) width = next;
+
+       /* Draw data point lines. */
+       for (i = left; i < width; i++) {
+           int y = (int) (w->core.height -
+                          (int)(w->core.height * w->strip_chart.valuedata[i]) /
+                          w->strip_chart.scale);
+
+           XFillRectangle(dpy, win, w->strip_chart.fgGC,
+                          i, y, (unsigned int) 1,
+                          (unsigned int) (w->core.height - y));
+       }
+
+       /* Draw graph reference lines */
+       for (i = 1; i < w->strip_chart.scale; i++) {
+           j = i * ((int)w->core.height / w->strip_chart.scale);
+           XDrawLine(dpy, win, w->strip_chart.hiGC, left, j, scalewidth, j);
+       }
+    }
+    return(next);
+}
+
+/*     Function Name: MoveChart
+ *     Description: moves the chart over when it would run off the end.
+ *     Arguments: w - the load widget.
+ *                 blit - blit the bits? (TRUE/FALSE).
+ *     Returns: none.
+ */
+
+static void
+MoveChart(StripChartWidget w, Boolean blit)
+{
+    double old_max;
+    int left, i, j;
+    register int next = w->strip_chart.interval;
+
+    if (!XtIsRealized((Widget) w)) return;
+
+    if (w->strip_chart.jump_val == DEFAULT_JUMP)
+        j = w->core.width >> 1; /* Half the window width. */
+    else {
+        j = w->core.width - w->strip_chart.jump_val;
+       if (j < 0) j = 0;
+    }
+
+    bcopy((char *)(w->strip_chart.valuedata + next - j),
+         (char *)(w->strip_chart.valuedata), j * sizeof(double));
+    next = w->strip_chart.interval = j;
+
+    /*
+     * Since we just lost some data, recompute the
+     * w->strip_chart.max_value.
+     */
+
+    old_max = w->strip_chart.max_value;
+    w->strip_chart.max_value = 0.0;
+    for (i = 0; i < next; i++) {
+      if (w->strip_chart.valuedata[i] > w->strip_chart.max_value)
+       w->strip_chart.max_value = w->strip_chart.valuedata[i];
+    }
+
+    if (!blit) return;         /* we are done... */
+
+    if ( ((int) old_max) != ( (int) w->strip_chart.max_value) ) {
+      XClearWindow(XtDisplay(w), XtWindow(w));
+      repaint_window(w, 0, (int) w->core.width);
+      return;
+    }
+
+    XCopyArea(XtDisplay((Widget)w), XtWindow((Widget)w), XtWindow((Widget)w),
+             w->strip_chart.hiGC, (int) w->core.width - j, 0,
+             (unsigned int) j, (unsigned int) w->core.height,
+             0, 0);
+
+    XClearArea(XtDisplay((Widget)w), XtWindow((Widget)w),
+              (int) j, 0,
+              (unsigned int) w->core.width - j, (unsigned int)w->core.height,
+              FALSE);
+
+    /* Draw graph reference lines */
+    left = j;
+    for (i = 1; i < w->strip_chart.scale; i++) {
+      j = i * ((int)w->core.height / w->strip_chart.scale);
+      XDrawLine(XtDisplay((Widget) w), XtWindow( (Widget) w),
+               w->strip_chart.hiGC, left, j, (int)w->core.width, j);
+    }
+    return;
+}
+
+/* ARGSUSED */
+static Boolean SetValues (current, request, new)
+    Widget current, request, new;
+{
+    StripChartWidget old = (StripChartWidget)current;
+    StripChartWidget w = (StripChartWidget)new;
+    Boolean ret_val = FALSE;
+    unsigned int new_gc = NO_GCS;
+
+    if (w->strip_chart.update != old->strip_chart.update) {
+       if (old->strip_chart.update > 0)
+           XtRemoveTimeOut (old->strip_chart.interval_id);
+       if (w->strip_chart.update > 0)
+           w->strip_chart.interval_id =
+               XtAppAddTimeOut(XtWidgetToApplicationContext(new),
+                               w->strip_chart.update * MS_PER_SEC,
+                               draw_it, (XtPointer)w);
+    }
+
+    if ( w->strip_chart.min_scale > (int) ((w->strip_chart.max_value) + 1) )
+      ret_val = TRUE;
+
+    if ( w->strip_chart.fgpixel != old->strip_chart.fgpixel ) {
+      new_gc |= FOREGROUND;
+      ret_val = True;
+    }
+
+    if ( w->strip_chart.hipixel != old->strip_chart.hipixel ) {
+      new_gc |= HIGHLIGHT;
+      ret_val = True;
+    }
+
+    DestroyGC(old, new_gc);
+    CreateGC(w, new_gc);
+
+    return( ret_val );
+}
+
+/*     Function Name: SetPoints
+ *     Description: Sets up the polypoint that will be used to draw in
+ *                   the graph lines.
+ *     Arguments: w - the StripChart widget.
+ *     Returns: none.
+ */
+
+#define HEIGHT ( (unsigned int) w->core.height)
+
+static void
+SetPoints(w)
+StripChartWidget w;
+{
+    XPoint * points;
+    Cardinal size;
+    int i;
+
+    if (w->strip_chart.scale <= 1) { /* no scale lines. */
+       XtFree ((char *) w->strip_chart.points);
+       w->strip_chart.points = NULL;
+       return;
+    }
+
+    size = sizeof(XPoint) * (w->strip_chart.scale - 1);
+
+    points = (XPoint *) XtRealloc( (XtPointer) w->strip_chart.points, size);
+    w->strip_chart.points = points;
+
+    /* Draw graph reference lines into clip mask */
+
+    for (i = 1; i < w->strip_chart.scale; i++) {
+       points[i - 1].x = 0;
+       points[i - 1].y = HEIGHT / w->strip_chart.scale;
+    }
+}
diff --git a/exim_monitor/em_TextPop.c b/exim_monitor/em_TextPop.c
new file mode 100644 (file)
index 0000000..faa51ed
--- /dev/null
@@ -0,0 +1,767 @@
+/***********************************************************
+Copyright 1989 by the Massachusetts Institute of Technology,
+Cambridge, Massachusetts.
+
+                        All Rights Reserved
+
+Permission to use, copy, modify, and distribute this software and its
+documentation for any purpose and without fee is hereby granted,
+provided that the above copyright notice appear in all copies and that
+both that copyright notice and this permission notice appear in
+supporting documentation, and that the names of Digital or MIT not be
+used in advertising or publicity pertaining to distribution of the
+software without specific, written prior permission.
+
+DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
+ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
+DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
+ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
+WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
+ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
+SOFTWARE.
+
+******************************************************************/
+
+
+/****************************************************************************
+* Modified by Philip Hazel for use with Exim. The "replace" and "insert     *
+* file" features of the search facility have been removed.  Also took out   *
+* the declaration of sys_errlist, as it isn't used and causes trouble on    *
+* some systems that declare it differently. September 1996.                 *
+* Added the arguments into the static functions declared at the head, to    *
+* stop some compiler warnings. August 1999.                                 *
+* Took out the separate declarations of errno and sys_nerr at the start,    *
+* because they too aren't actually used, and the declaration causes trouble *
+* on some systems. December 2002.                                           *
+****************************************************************************/
+
+
+/************************************************************
+ *
+ * This file is broken up into three sections one dealing with
+ * each of the three popups created here:
+ *
+ * FileInsert, Search, and Replace.
+ *
+ * There is also a section at the end for utility functions
+ * used by all more than one of these dialogs.
+ *
+ * The following functions are the only non-static ones defined
+ * in this module.  They are located at the begining of the
+ * section that contains this dialog box that uses them.
+ *
+ * void _XawTextInsertFileAction(w, event, params, num_params);
+ * void _XawTextDoSearchAction(w, event, params, num_params);
+ * void _XawTextDoReplaceAction(w, event, params, num_params);
+ * void _XawTextInsertFile(w, event, params, num_params);
+ *
+ *************************************************************/
+
+#include <X11/IntrinsicP.h>
+#include <X11/StringDefs.h>
+#include <X11/Shell.h>
+
+#include <X11/Xaw/TextP.h>
+#include <X11/Xaw/AsciiText.h>
+#include <X11/Xaw/Cardinals.h>
+#include <X11/Xaw/Command.h>
+#include <X11/Xaw/Form.h>
+#include <X11/Xaw/Toggle.h>
+#include <X11/Xmu/CharSet.h>
+#include <stdio.h>
+#include <X11/Xos.h>           /* for O_RDONLY */
+#include <errno.h>
+
+/* extern int errno, sys_nerr; */
+/* extern char* sys_errlist[]; */
+
+#define DISMISS_NAME  ("cancel")
+#define DISMISS_NAME_LEN 6
+#define FORM_NAME     ("form")
+#define LABEL_NAME    ("label")
+#define TEXT_NAME     ("text")
+
+#define R_OFFSET      1
+
+/* Argument types added by PH August 1999 */
+
+static void CenterWidgetOnPoint(Widget, XEvent *);
+static void PopdownSearch(Widget, XtPointer, XtPointer);
+static void InitializeSearchWidget(struct SearchAndReplace *,
+  XawTextScanDirection, Boolean);
+static void  SetResource(Widget, char *, XtArgVal);
+static void  SetSearchLabels(struct SearchAndReplace *, String, String,
+  Boolean);
+static Widget CreateDialog(Widget, String, String,
+  void (*)(Widget, char *, Widget));
+static Widget  GetShell(Widget);
+static void SetWMProtocolTranslations(Widget w);
+static Boolean DoSearch(struct SearchAndReplace *);
+static String GetString(Widget);
+
+static void AddSearchChildren(Widget, char *, Widget);
+
+static char radio_trans_string[] =
+    "<Btn1Down>,<Btn1Up>:   set() notify()";
+
+static char search_text_trans[] =
+  "~Shift<Key>Return:      DoSearchAction(Popdown) \n\
+   Ctrl<Key>c:             PopdownSearchAction() \n\
+   ";
+
+
+
+/************************************************************
+ *
+ * This section of the file contains all the functions that
+ * the search dialog box uses.
+ *
+ ************************************************************/
+
+/*     Function Name: _XawTextDoSearchAction
+ *     Description: Action routine that can be bound to dialog box's
+ *                   Text Widget that will search for a string in the main
+ *                   Text Widget.
+ *     Arguments:   (Standard Action Routine args)
+ *     Returns:     none.
+ *
+ * Note:
+ *
+ * If the search was sucessful and the argument popdown is passed to
+ * this action routine then the widget will automatically popdown the
+ * search widget.
+ */
+
+/* ARGSUSED */
+void
+_XawTextDoSearchAction(w, event, params, num_params)
+Widget w;
+XEvent *event;
+String * params;
+Cardinal * num_params;
+{
+  TextWidget tw = (TextWidget) XtParent(XtParent(XtParent(w)));
+  Boolean popdown = FALSE;
+
+  if ( (*num_params == 1) &&
+       ((params[0][0] == 'p') || (params[0][0] == 'P')) )
+      popdown = TRUE;
+
+  if (DoSearch(tw->text.search) && popdown)
+    PopdownSearch(w, (XtPointer) tw->text.search, NULL);
+}
+
+/*     Function Name: _XawTextPopdownSearchAction
+ *     Description: Action routine that can be bound to dialog box's
+ *                   Text Widget that will popdown the search widget.
+ *     Arguments:   (Standard Action Routine args)
+ *     Returns:     none.
+ */
+
+/* ARGSUSED */
+void
+_XawTextPopdownSearchAction(w, event, params, num_params)
+Widget w;
+XEvent *event;
+String * params;
+Cardinal * num_params;
+{
+  TextWidget tw = (TextWidget) XtParent(XtParent(XtParent(w)));
+
+  PopdownSearch(w, (XtPointer) tw->text.search, NULL);
+}
+
+/*     Function Name: PopdownSeach
+ *     Description: Pops down the search widget and resets it.
+ *     Arguments: w - *** NOT USED ***.
+ *                 closure - a pointer to the search structure.
+ *                 call_data - *** NOT USED ***.
+ *     Returns: none
+ */
+
+/* ARGSUSED */
+static void
+PopdownSearch(w, closure, call_data)
+Widget w;
+XtPointer closure;
+XtPointer call_data;
+{
+  struct SearchAndReplace * search = (struct SearchAndReplace *) closure;
+
+  SetSearchLabels(search, "Search", "", FALSE);
+  XtPopdown( search->search_popup );
+}
+
+/*     Function Name: SearchButton
+ *     Description: Performs a search when the button is clicked.
+ *     Arguments: w - *** NOT USED **.
+ *                 closure - a pointer to the search info.
+ *                 call_data - *** NOT USED ***.
+ *     Returns:
+ */
+
+/* ARGSUSED */
+static void
+SearchButton(w, closure, call_data)
+Widget w;
+XtPointer closure;
+XtPointer call_data;
+{
+  (void) DoSearch( (struct SearchAndReplace *) closure );
+}
+
+/*     Function Name: _XawTextSearch
+ *     Description: Action routine that can be bound to the text widget
+ *                   it will popup the search dialog box.
+ *     Arguments:   w - the text widget.
+ *                   event - X Event (used to get x and y location).
+ *                   params, num_params - the parameter list.
+ *     Returns:     none.
+ *
+ * NOTE:
+ *
+ * The parameter list contains one or two entries that may be the following.
+ *
+ * First Entry:   The first entry is the direction to search by default.
+ *                This arguement must be specified and may have a value of
+ *                "left" or "right".
+ *
+ * Second Entry:  This entry is optional and contains the value of the default
+ *                string to search for.
+ */
+
+#define SEARCH_HEADER ("Text Widget - Search():")
+
+void
+_XawTextSearch(w, event, params, num_params)
+Widget w;
+XEvent *event;
+String * params;
+Cardinal * num_params;
+{
+  TextWidget ctx = (TextWidget)w;
+  XawTextScanDirection dir;
+  char * ptr, buf[BUFSIZ];
+  XawTextEditType edit_mode;
+  Arg args[1];
+
+#ifdef notdef
+  if (ctx->text.source->Search == NULL) {
+      XBell(XtDisplay(w), 0);
+      return;
+  }
+#endif
+
+  if ( (*num_params < 1) || (*num_params > 2) ) {
+    sprintf(buf, "%s %s\n%s", SEARCH_HEADER, "This action must have only",
+           "one or two parameters");
+    XtAppWarning(XtWidgetToApplicationContext(w), buf);
+    return;
+  }
+  else if (*num_params == 1)
+    ptr = "";
+  else
+    ptr = params[1];
+
+  switch(params[0][0]) {
+  case 'b':                    /* Left. */
+  case 'B':
+    dir = XawsdLeft;
+    break;
+  case 'f':                    /* Right. */
+  case 'F':
+    dir = XawsdRight;
+    break;
+  default:
+    sprintf(buf, "%s %s\n%s", SEARCH_HEADER, "The first parameter must be",
+           "Either 'backward' or 'forward'");
+    XtAppWarning(XtWidgetToApplicationContext(w), buf);
+    return;
+  }
+
+  if (ctx->text.search== NULL) {
+    ctx->text.search = XtNew(struct SearchAndReplace);
+    ctx->text.search->search_popup = CreateDialog(w, ptr, "search",
+                                                 AddSearchChildren);
+    XtRealizeWidget(ctx->text.search->search_popup);
+    SetWMProtocolTranslations(ctx->text.search->search_popup);
+  }
+  else if (*num_params > 1) {
+    XtVaSetValues(ctx->text.search->search_text, XtNstring, ptr, NULL);
+  }
+
+  XtSetArg(args[0], XtNeditType,&edit_mode);
+  XtGetValues(ctx->text.source, args, ONE);
+
+  InitializeSearchWidget(ctx->text.search, dir, (edit_mode == XawtextEdit));
+
+  CenterWidgetOnPoint(ctx->text.search->search_popup, event);
+  XtPopup(ctx->text.search->search_popup, XtGrabNone);
+}
+
+/*     Function Name: InitializeSearchWidget
+ *     Description: This function initializes the search widget and
+ *                   is called each time the search widget is poped up.
+ *     Arguments: search - the search widget structure.
+ *                 dir - direction to search.
+ *                 replace_active - state of the sensitivity for the
+ *                                  replace button.
+ *     Returns: none.
+ */
+
+static void
+InitializeSearchWidget(struct SearchAndReplace *search,
+  XawTextScanDirection dir, Boolean replace_active)
+{
+replace_active = replace_active; /* PH - shuts compilers up */
+
+  switch (dir) {
+  case XawsdLeft:
+    SetResource(search->left_toggle, XtNstate, (XtArgVal) TRUE);
+    break;
+  case XawsdRight:
+    SetResource(search->right_toggle, XtNstate, (XtArgVal) TRUE);
+    break;
+  default:
+    break;
+  }
+}
+
+/*     Function Name: AddSearchChildren
+ *     Description: Adds all children to the Search Dialog Widget.
+ *     Arguments: form - the form widget for the search widget.
+ *                 ptr - a pointer to the initial string for the Text Widget.
+ *                 tw - the main text widget.
+ *     Returns: none.
+ */
+
+static void
+AddSearchChildren(form, ptr, tw)
+Widget form, tw;
+char * ptr;
+{
+  Arg args[10];
+  Cardinal num_args;
+  Widget cancel, search_button, s_label, s_text;
+  XtTranslations trans;
+  struct SearchAndReplace * search = ((TextWidget) tw)->text.search;
+
+  num_args = 0;
+  XtSetArg(args[num_args], XtNleft, XtChainLeft); num_args++;
+  XtSetArg(args[num_args], XtNright, XtChainLeft); num_args++;
+  XtSetArg(args[num_args], XtNresizable, TRUE ); num_args++;
+  XtSetArg(args[num_args], XtNborderWidth, 0 ); num_args++;
+  search->label1 = XtCreateManagedWidget("label1", labelWidgetClass,
+                                        form, args, num_args);
+
+ /*
+ * We need to add R_OFFSET to the radio_data, because the value zero (0)
+ * has special meaning.
+ */
+
+  num_args = 0;
+  XtSetArg(args[num_args], XtNlabel, "Backward"); num_args++;
+  XtSetArg(args[num_args], XtNfromVert, search->label1); num_args++;
+  XtSetArg(args[num_args], XtNleft, XtChainLeft); num_args++;
+  XtSetArg(args[num_args], XtNright, XtChainLeft); num_args++;
+  XtSetArg(args[num_args], XtNradioData, (caddr_t) XawsdLeft + R_OFFSET);
+  num_args++;
+  search->left_toggle = XtCreateManagedWidget("backwards", toggleWidgetClass,
+                                             form, args, num_args);
+
+  num_args = 0;
+  XtSetArg(args[num_args], XtNlabel, "Forward"); num_args++;
+  XtSetArg(args[num_args], XtNfromVert, search->label1); num_args++;
+  XtSetArg(args[num_args], XtNfromHoriz, search->left_toggle); num_args++;
+  XtSetArg(args[num_args], XtNleft, XtChainLeft); num_args++;
+  XtSetArg(args[num_args], XtNright, XtChainLeft); num_args++;
+  XtSetArg(args[num_args], XtNradioGroup, search->left_toggle); num_args++;
+  XtSetArg(args[num_args], XtNradioData, (caddr_t) XawsdRight + R_OFFSET);
+  num_args++;
+  search->right_toggle = XtCreateManagedWidget("forwards", toggleWidgetClass,
+                                              form, args, num_args);
+
+  {
+    XtTranslations radio_translations;
+
+    radio_translations = XtParseTranslationTable(radio_trans_string);
+    XtOverrideTranslations(search->left_toggle, radio_translations);
+    XtOverrideTranslations(search->right_toggle, radio_translations);
+  }
+
+  num_args = 0;
+  XtSetArg(args[num_args], XtNfromVert, search->left_toggle); num_args++;
+  XtSetArg(args[num_args], XtNlabel, "Search for:  ");num_args++;
+  XtSetArg(args[num_args], XtNleft, XtChainLeft); num_args++;
+  XtSetArg(args[num_args], XtNright, XtChainLeft); num_args++;
+  XtSetArg(args[num_args], XtNborderWidth, 0 ); num_args++;
+  s_label = XtCreateManagedWidget("searchLabel", labelWidgetClass,
+                                 form, args, num_args);
+
+  num_args = 0;
+  XtSetArg(args[num_args], XtNfromVert, search->left_toggle); num_args++;
+  XtSetArg(args[num_args], XtNfromHoriz, s_label); num_args++;
+  XtSetArg(args[num_args], XtNleft, XtChainLeft); num_args++;
+  XtSetArg(args[num_args], XtNright, XtChainRight); num_args++;
+  XtSetArg(args[num_args], XtNeditType, XawtextEdit); num_args++;
+  XtSetArg(args[num_args], XtNresizable, TRUE); num_args++;
+  XtSetArg(args[num_args], XtNresize, XawtextResizeWidth); num_args++;
+  XtSetArg(args[num_args], XtNstring, ptr); num_args++;
+  s_text = XtCreateManagedWidget("searchText", asciiTextWidgetClass, form,
+                                args, num_args);
+  search->search_text = s_text;
+
+  num_args = 0;
+  XtSetArg(args[num_args], XtNlabel, "Search"); num_args++;
+  XtSetArg(args[num_args], XtNfromVert, s_text); num_args++;
+  XtSetArg(args[num_args], XtNleft, XtChainLeft); num_args++;
+  XtSetArg(args[num_args], XtNright, XtChainLeft); num_args++;
+  search_button = XtCreateManagedWidget("search", commandWidgetClass, form,
+                                       args, num_args);
+
+  num_args = 0;
+  XtSetArg(args[num_args], XtNlabel, "Cancel"); num_args++;
+  XtSetArg(args[num_args], XtNfromVert, s_text); num_args++;
+  XtSetArg(args[num_args], XtNfromHoriz, search_button); num_args++;
+  XtSetArg(args[num_args], XtNleft, XtChainLeft); num_args++;
+  XtSetArg(args[num_args], XtNright, XtChainLeft); num_args++;
+  cancel = XtCreateManagedWidget(DISMISS_NAME, commandWidgetClass, form,
+                                args, num_args);
+
+  XtAddCallback(search_button, XtNcallback, SearchButton, (XtPointer) search);
+  XtAddCallback(cancel, XtNcallback, PopdownSearch, (XtPointer) search);
+
+/*
+ * Initialize the text entry fields.
+ */
+
+  SetSearchLabels(search, "Search", "", FALSE);
+  XtSetKeyboardFocus(form, search->search_text);
+
+/*
+ * Bind Extra translations.
+ */
+
+  trans = XtParseTranslationTable(search_text_trans);
+  XtOverrideTranslations(search->search_text, trans);
+}
+
+/*     Function Name: DoSearch
+ *     Description: Performs a search.
+ *     Arguments: search - the serach structure.
+ *     Returns: TRUE if sucessful.
+ */
+
+/* ARGSUSED */
+static Boolean
+DoSearch(search)
+struct SearchAndReplace * search;
+{
+  char msg[BUFSIZ];
+  Widget tw = XtParent(search->search_popup);
+  XawTextPosition pos;
+  XawTextScanDirection dir;
+  XawTextBlock text;
+
+  text.ptr = GetString(search->search_text);
+  text.length = strlen(text.ptr);
+  text.firstPos = 0;
+  text.format = FMT8BIT;
+
+  dir = (XawTextScanDirection)(int) ((caddr_t)XawToggleGetCurrent(search->left_toggle) -
+                               R_OFFSET);
+
+  pos = XawTextSearch( tw, dir, &text);
+
+  if (pos == XawTextSearchError)
+    sprintf( msg, "Could not find string '%s'.", text.ptr);
+  else {
+    if (dir == XawsdRight)
+      XawTextSetInsertionPoint( tw, pos + text.length);
+    else
+      XawTextSetInsertionPoint( tw, pos);
+
+    XawTextSetSelection( tw, pos, pos + text.length);
+    search->selection_changed = FALSE; /* selection is good. */
+    return(TRUE);
+  }
+
+  XawTextUnsetSelection(tw);
+  SetSearchLabels(search, msg, "", TRUE);
+  return(FALSE);
+}
+
+
+/*     Function Name: SetSearchLabels
+ *     Description: Sets both the search labels, and also rings the bell
+ *  HACKED: Only one label needed now
+ *     Arguments: search - the search structure.
+ *                 msg1, msg2 - message to put in each search label.
+ *                 bell - if TRUE then ring bell.
+ *     Returns: none.
+ */
+
+static void
+SetSearchLabels(struct SearchAndReplace *search, String msg1, String msg2,
+  Boolean bell)
+{
+msg2 = msg2; /* PH - shuts compilers up */
+  (void) SetResource( search->label1, XtNlabel, (XtArgVal) msg1);
+  /* (void) SetResource( search->label2, XtNlabel, (XtArgVal) msg2); */
+  if (bell)
+    XBell(XtDisplay(search->search_popup), 0);
+}
+
+/************************************************************
+ *
+ * This section of the file contains utility routines used by
+ * other functions in this file.
+ *
+ ************************************************************/
+
+
+/*     Function Name: SetResource
+ *     Description: Sets a resource in a widget
+ *     Arguments: w - the widget.
+ *                 res_name - name of the resource.
+ *                 value - the value of the resource.
+ *     Returns: none.
+ */
+
+static void
+SetResource(w, res_name, value)
+Widget w;
+char * res_name;
+XtArgVal value;
+{
+  Arg args[1];
+
+  XtSetArg(args[0], res_name, value);
+  XtSetValues( w, args, ONE );
+}
+
+/*     Function Name: GetString
+ *     Description:   Gets the value for the string in the popup.
+ *     Arguments:     text - the text widget whose string we will get.
+ *     Returns:       the string.
+ */
+
+static String
+GetString(text)
+Widget text;
+{
+  String string;
+  Arg args[1];
+
+  XtSetArg( args[0], XtNstring, &string );
+  XtGetValues( text, args, ONE );
+  return(string);
+}
+
+/*     Function Name: CenterWidgetOnPoint.
+ *     Description: Centers a shell widget on a point relative to
+ *                   the root window.
+ *     Arguments: w - the shell widget.
+ *                 event - event containing the location of the point
+ *     Returns: none.
+ *
+ * NOTE: The widget is not allowed to go off the screen.
+ */
+
+static void
+CenterWidgetOnPoint(w, event)
+Widget w;
+XEvent *event;
+{
+  Arg args[3];
+  Cardinal num_args;
+  Dimension width, height, b_width;
+  Position x=0, y=0, max_x, max_y;
+
+  if (event != NULL) {
+    switch (event->type) {
+    case ButtonPress:
+    case ButtonRelease:
+      x = event->xbutton.x_root;
+      y = event->xbutton.y_root;
+      break;
+    case KeyPress:
+    case KeyRelease:
+      x = event->xkey.x_root;
+      y = event->xkey.y_root;
+      break;
+    default:
+      return;
+    }
+  }
+
+  num_args = 0;
+  XtSetArg(args[num_args], XtNwidth, &width); num_args++;
+  XtSetArg(args[num_args], XtNheight, &height); num_args++;
+  XtSetArg(args[num_args], XtNborderWidth, &b_width); num_args++;
+  XtGetValues(w, args, num_args);
+
+  width += 2 * b_width;
+  height += 2 * b_width;
+
+  x -= ( (Position) width/2 );
+  if (x < 0) x = 0;
+  if ( x > (max_x = (Position) (XtScreen(w)->width - width)) ) x = max_x;
+
+  y -= ( (Position) height/2 );
+  if (y < 0) y = 0;
+  if ( y > (max_y = (Position) (XtScreen(w)->height - height)) ) y = max_y;
+
+  num_args = 0;
+  XtSetArg(args[num_args], XtNx, x); num_args++;
+  XtSetArg(args[num_args], XtNy, y); num_args++;
+  XtSetValues(w, args, num_args);
+}
+
+/*     Function Name: CreateDialog
+ *     Description: Actually creates a dialog.
+ *     Arguments: parent - the parent of the dialog - the main text widget.
+ *                 ptr - initial_string for the dialog.
+ *                 name - name of the dialog.
+ *                 func - function to create the children of the dialog.
+ *     Returns: the popup shell of the dialog.
+ *
+ * NOTE:
+ *
+ * The function argument is passed the following arguements.
+ *
+ * form - the from widget that is the dialog.
+ * ptr - the initial string for the dialog's text widget.
+ * parent - the parent of the dialog - the main text widget.
+ */
+
+static Widget
+CreateDialog(parent, ptr, name, func)
+Widget parent;
+String ptr, name;
+void (*func)();
+{
+  Widget popup, form;
+  Arg args[5];
+  Cardinal num_args;
+
+  num_args = 0;
+  XtSetArg(args[num_args], XtNiconName, name); num_args++;
+  XtSetArg(args[num_args], XtNgeometry, NULL); num_args++;
+  XtSetArg(args[num_args], XtNallowShellResize, TRUE); num_args++;
+  XtSetArg(args[num_args], XtNtransientFor, GetShell(parent)); num_args++;
+  popup = XtCreatePopupShell(name, transientShellWidgetClass,
+                            parent, args, num_args);
+
+  form = XtCreateManagedWidget(FORM_NAME, formWidgetClass, popup,
+                              NULL, ZERO);
+
+  (*func) (form, ptr, parent);
+  return(popup);
+}
+
+ /*    Function Name: GetShell
+  *    Description: Walks up the widget hierarchy to find the
+  *            nearest shell widget.
+  *    Arguments: w - the widget whose parent shell should be returned.
+  *    Returns: The shell widget among the ancestors of w that is the
+  *            fewest levels up in the widget hierarchy.
+  */
+
+static Widget
+GetShell(w)
+Widget w;
+{
+    while ((w != NULL) && !XtIsShell(w))
+       w = XtParent(w);
+
+    return (w);
+}
+
+/* Add proper prototype to keep IRIX 6 compiler happy. PH */
+
+static Boolean InParams(String, String *, Cardinal);
+
+static Boolean InParams(str, p, n)
+    String str;
+    String *p;
+    Cardinal n;
+{
+    int i;
+    for (i=0; i < n; p++, i++)
+       if (! XmuCompareISOLatin1(*p, str)) return True;
+    return False;
+}
+
+static char *WM_DELETE_WINDOW = "WM_DELETE_WINDOW";
+
+static void WMProtocols(w, event, params, num_params)
+    Widget w;          /* popup shell */
+    XEvent *event;
+    String *params;
+    Cardinal *num_params;
+{
+    Atom wm_delete_window;
+    Atom wm_protocols;
+
+    wm_delete_window = XInternAtom(XtDisplay(w), WM_DELETE_WINDOW, True);
+    wm_protocols = XInternAtom(XtDisplay(w), "WM_PROTOCOLS", True);
+
+    /* Respond to a recognized WM protocol request iff
+     * event type is ClientMessage and no parameters are passed, or
+     * event type is ClientMessage and event data is matched to parameters, or
+     * event type isn't ClientMessage and parameters make a request.
+     */
+#define DO_DELETE_WINDOW InParams(WM_DELETE_WINDOW, params, *num_params)
+
+    if ((event->type == ClientMessage &&
+        event->xclient.message_type == wm_protocols &&
+        event->xclient.data.l[0] == wm_delete_window &&
+        (*num_params == 0 || DO_DELETE_WINDOW))
+       ||
+       (event->type != ClientMessage && DO_DELETE_WINDOW)) {
+
+#undef DO_DELETE_WINDOW
+
+       Widget cancel;
+       char descendant[DISMISS_NAME_LEN + 2];
+       sprintf(descendant, "*%s", DISMISS_NAME);
+       cancel = XtNameToWidget(w, descendant);
+       if (cancel) XtCallCallbacks(cancel, XtNcallback, (XtPointer)NULL);
+    }
+}
+
+static void SetWMProtocolTranslations(w)
+    Widget     w;      /* realized popup shell */
+{
+    int i;
+    XtAppContext app_context;
+    Atom wm_delete_window;
+    static XtTranslations compiled_table;      /* initially 0 */
+    static XtAppContext *app_context_list;     /* initially 0 */
+    static Cardinal list_size;                 /* initially 0 */
+
+    app_context = XtWidgetToApplicationContext(w);
+
+    /* parse translation table once */
+    if (! compiled_table) compiled_table = XtParseTranslationTable
+       ("<Message>WM_PROTOCOLS: XawWMProtocols()\n");
+
+    /* add actions once per application context */
+    for (i=0; i < list_size && app_context_list[i] != app_context; i++) ;
+    if (i == list_size) {
+       XtActionsRec actions[1];
+       actions[0].string = "XawWMProtocols";
+       actions[0].proc = WMProtocols;
+       list_size++;
+       app_context_list = (XtAppContext *) XtRealloc
+           ((char *)app_context_list, list_size * sizeof(XtAppContext));
+       XtAppAddActions(app_context, actions, 1);
+       app_context_list[i] = app_context;
+    }
+
+    /* establish communication between the window manager and each shell */
+    XtAugmentTranslations(w, compiled_table);
+    wm_delete_window = XInternAtom(XtDisplay(w), WM_DELETE_WINDOW, False);
+    (void) XSetWMProtocols(XtDisplay(w), XtWindow(w), &wm_delete_window, 1);
+}
diff --git a/exim_monitor/em_globals.c b/exim_monitor/em_globals.c
new file mode 100644 (file)
index 0000000..671bd7f
--- /dev/null
@@ -0,0 +1,239 @@
+/*************************************************
+*                Exim Monitor                    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2012 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+
+#include "em_hdr.h"
+
+/* This source module contains all the global variables used in
+the exim monitor, including those that are used by the standard
+Exim modules that are included in Eximon. For comments on their
+usage, see em_hdr.h and globals.h. */
+
+
+/* The first set are unique to Eximon */
+
+Display *X_display;
+XtAppContext X_appcon;
+
+XtActionsRec actionTable[] = {
+  { "dialogAction",  (XtActionProc)dialogAction}};
+
+int actionTableSize = sizeof(actionTable)/sizeof(XtActionsRec);
+
+XtTranslations queue_trans;
+XtTranslations text_trans;
+
+Widget  dialog_ref_widget;
+Widget  toplevel_widget;
+Widget  log_widget = NULL;
+Widget  queue_widget;
+Widget  unhide_widget = NULL;
+
+
+FILE   *LOG;
+
+int     action_output = FALSE;
+int     action_queue_update = TRUE;
+uschar  actioned_message[24];
+uschar *action_required;
+uschar *alternate_config = NULL;
+
+#ifdef EXPERIMENTAL_BRIGHTMAIL
+int     bmi_run                = 0;
+uschar *bmi_verdicts           = NULL;
+#endif
+
+int     body_max = 20000;
+
+uschar *exim_path              = US BIN_DIRECTORY "/exim"
+                        "\0<---------------Space to patch exim_path->";
+
+int     eximon_initialized = FALSE;
+
+int     log_buffer_size = 10240;
+BOOL    log_datestamping = FALSE;
+int     log_depth = 150;
+uschar *log_display_buffer;
+uschar *log_file = NULL;
+uschar  log_file_open[256];
+uschar *log_font = NULL;
+ino_t   log_inode;
+long int log_position;
+int     log_width = 600;
+
+uschar *menu_event = US"Shift<Btn1Down>";
+int     menu_is_up = FALSE;
+int     min_height = 162;
+int     min_width  = 103;
+
+pipe_item *pipe_chain = NULL;
+
+uschar *qualify_domain = NULL;
+int     queue_depth = 200;
+uschar *queue_font = NULL;
+int     queue_max_addresses = 10;
+skip_item *queue_skip = NULL;
+uschar *queue_stripchart_name = NULL;
+int     queue_update = 60;
+int     queue_width = 600;
+
+pcre   *yyyymmdd_regex;
+
+uschar *size_stripchart = NULL;
+uschar *size_stripchart_name = NULL;
+int     spool_is_split = FALSE;
+int     start_small = FALSE;
+int     stripchart_height = 90;
+int     stripchart_number = 1;
+pcre  **stripchart_regex;
+uschar **stripchart_title;
+int    *stripchart_total;
+int     stripchart_update = 60;
+int     stripchart_width = 80;
+int     stripchart_varstart = 1;
+
+int     text_depth = 200;
+int     tick_queue_accumulator = 999999;
+
+uschar *window_title = US"exim monitor";
+
+
+/***********************************************************/
+/***********************************************************/
+
+
+/* These ones are used by Exim modules included in Eximon. Not all are
+actually relevant to the operation of Eximon. If SPOOL_DIRECTORY is not
+defined (Exim was compiled with it unset), just define it empty. The script
+that fires up the monitor fishes the value out by using -bP anyway. */
+
+#ifndef SPOOL_DIRECTORY
+#define SPOOL_DIRECTORY ""
+#endif
+
+tree_node *acl_var_c           = NULL;
+tree_node *acl_var_m           = NULL;
+uschar *active_hostname        = NULL;
+BOOL    allow_unqualified_recipient = FALSE;
+BOOL    allow_unqualified_sender = FALSE;
+uschar *authenticated_id       = NULL;
+uschar *authenticated_sender   = NULL;
+
+uschar *big_buffer             = NULL;
+int     big_buffer_size        = BIG_BUFFER_SIZE;
+int     body_linecount         = 0;
+int     body_zerocount         = 0;
+
+BOOL    deliver_firsttime      = FALSE;
+BOOL    deliver_freeze         = FALSE;
+time_t  deliver_frozen_at      = 0;
+BOOL    deliver_manual_thaw    = FALSE;
+
+#ifndef DISABLE_DKIM
+uschar *dkim_cur_signer          = NULL;
+uschar *dkim_signers             = NULL;
+uschar *dkim_signing_domain      = NULL;
+uschar *dkim_signing_selector    = NULL;
+uschar *dkim_verify_signers      = US"$dkim_signers";
+BOOL    dkim_collect_input       = FALSE;
+BOOL    dkim_disable_verify      = FALSE;
+#endif
+
+BOOL    dont_deliver           = FALSE;
+
+#ifdef EXPERIMENTAL_DSN
+int     dsn_ret                = 0;
+uschar *dsn_envid              = NULL;
+#endif
+
+#ifdef WITH_CONTENT_SCAN
+int     fake_response          = OK;
+#endif
+
+header_line *header_last       = NULL;
+header_line *header_list       = NULL;
+
+BOOL    host_lookup_deferred   = FALSE;
+BOOL    host_lookup_failed     = FALSE;
+uschar *interface_address      = NULL;
+int     interface_port         = 0;
+
+BOOL    local_error_message    = FALSE;
+uschar *local_scan_data        = NULL;
+BOOL    log_timezone           = FALSE;
+
+#ifdef WITH_CONTENT_SCAN
+uschar *spam_bar               = NULL;
+uschar *spam_report            = NULL;
+uschar *spam_score             = NULL;
+uschar *spam_score_int         = NULL;
+#endif
+
+int     max_received_linelength= 0;
+int     message_age            = 0;
+uschar *message_id;
+uschar *message_id_external;
+uschar  message_id_option[MESSAGE_ID_LENGTH + 3];
+
+int     message_linecount      = 0;
+int     message_size           = 0;
+uschar  message_subdir[2]      = { 0, 0 };
+
+gid_t   originator_gid;
+uschar *originator_login;
+uid_t   originator_uid;
+
+uschar *primary_hostname       = NULL;
+
+int     received_count         = 0;
+uschar *received_protocol      = NULL;
+int     received_time          = 0;
+int     recipients_count       = 0;
+recipient_item *recipients_list = NULL;
+int     recipients_list_max    = 0;
+int     running_in_test_harness=FALSE;
+
+uschar *sender_address         = NULL;
+uschar *sender_fullhost        = NULL;
+uschar *sender_helo_name       = NULL;
+uschar *sender_host_address    = NULL;
+uschar *sender_host_authenticated = NULL;
+uschar *sender_host_name       = NULL;
+int     sender_host_port       = 0;
+uschar *sender_ident           = NULL;
+BOOL    sender_local           = FALSE;
+BOOL    sender_set_untrusted   = FALSE;
+uschar *smtp_active_hostname   = NULL;
+
+BOOL    split_spool_directory  = FALSE;
+uschar *spool_directory        = US SPOOL_DIRECTORY;
+int     string_datestamp_offset=-1;
+int     string_datestamp_length= 0;
+int     string_datestamp_type  = -1;
+
+BOOL    timestamps_utc         = FALSE;
+tls_support tls_in = {
+ -1,   /* tls_active */
+ 0,    /* bits */
+ FALSE,        /* tls_certificate_verified */
+ NULL, /* tls_cipher */
+ FALSE,        /* tls_on_connect */
+ NULL, /* tls_on_connect_ports */
+ NULL, /* tls_peerdn */
+ NULL  /* tls_sni */
+};
+
+tree_node *tree_duplicates     = NULL;
+tree_node *tree_nonrecipients  = NULL;
+tree_node *tree_unusable       = NULL;
+
+uschar *version_date           = US"?";
+uschar *version_string         = US"?";
+
+int     warning_count          = 0;
+
+/* End of em_globals.c */
diff --git a/exim_monitor/em_hdr.h b/exim_monitor/em_hdr.h
new file mode 100644 (file)
index 0000000..ed95716
--- /dev/null
@@ -0,0 +1,327 @@
+/*************************************************
+*                 Exim Monitor                   *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+
+/* This is the general header file for all the modules that comprise
+the exim monitor program. */
+
+/* If this macro is defined, Eximon will anonymize all email addresses. This
+feature is just so that screen shots can be obtained for documentation
+purposes! */
+
+/* #define ANONYMIZE */
+
+/* System compilation parameters */
+
+#define queue_index_size  10      /* Size of index into queue */
+
+/* Assume most systems have statfs() unless os.h undefines this macro */
+
+#define HAVE_STATFS
+
+/* Bring in the system-dependent stuff */
+
+#include "os.h"
+
+
+/* ANSI C includes */
+
+#include <ctype.h>
+#include <setjmp.h>
+#include <signal.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+
+/* Not-fully-ANSI systems (e.g. SunOS4 are missing some things) */
+
+#ifndef SEEK_SET
+#define SEEK_SET 0
+#define SEEK_CUR 1
+#define SEEK_END 2
+#endif
+
+/* Unix includes */
+
+#include <sys/types.h>
+#include <errno.h>
+#include <dirent.h>
+#include <fcntl.h>
+#include <pwd.h>
+#include <grp.h>
+#include <sys/param.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+/* The new standard is statvfs; some OS have statfs. Also arrange
+to be able to cut it out altogether for way-out OS that don't have
+anything. */
+
+#ifdef HAVE_STATFS
+#ifdef HAVE_SYS_STATVFS_H
+#include <sys/statvfs.h>
+
+#else
+  #define statvfs statfs
+  #ifdef HAVE_SYS_VFS_H
+    #include <sys/vfs.h>
+    #ifdef HAVE_SYS_STATFS_H
+    #include <sys/statfs.h>
+    #endif
+  #endif
+  #ifdef HAVE_SYS_MOUNT_H
+  #include <sys/mount.h>
+  #endif
+#endif
+#endif
+
+#include <sys/wait.h>
+
+/* Regular expression include */
+
+#include <pcre.h>
+
+/* Includes from the main source of Exim. We need to have MAXPACKET defined for
+the benefit of structs.h. One of these days I should tidy up this interface so
+that this kind of kludge isn't needed. */
+
+#define MAXPACKET 1024
+
+#include "config.h"
+#include "mytypes.h"
+#include "macros.h"
+
+#include "local_scan.h"
+#include "structs.h"
+#include "globals.h"
+#include "dbstuff.h"
+#include "functions.h"
+#include "osfunctions.h"
+#include "store.h"
+
+/* The sys/resource.h header on SunOS 4 causes trouble with the gcc
+compiler. Just stuff the bit we want in here; pragmatic easy way out. */
+
+#ifdef NO_SYS_RESOURCE_H
+#define RLIMIT_NOFILE   6               /* maximum descriptor index + 1 */
+struct rlimit {
+        int     rlim_cur;               /* current (soft) limit */
+        int     rlim_max;               /* maximum value for rlim_cur */
+};
+#else
+#include <sys/time.h>
+#include <sys/resource.h>
+#endif
+
+/* X11 includes */
+
+#include <X11/Xlib.h>
+#include <X11/Intrinsic.h>
+#include <X11/StringDefs.h>
+#include <X11/cursorfont.h>
+#include <X11/keysym.h>
+#include <X11/Shell.h>
+#include <X11/Xaw/AsciiText.h>
+#include <X11/Xaw/Command.h>
+#include <X11/Xaw/Form.h>
+#include <X11/Xaw/Dialog.h>
+#include <X11/Xaw/Label.h>
+#include <X11/Xaw/SimpleMenu.h>
+#include <X11/Xaw/SmeBSB.h>
+#include <X11/Xaw/SmeLine.h>
+#include <X11/Xaw/TextSrc.h>
+#include <X11/Xaw/TextSink.h>
+
+/* These are required because exim monitor has its own munged
+version of the stripchart widget. */
+
+#include <X11/IntrinsicP.h>
+#include <X11/StringDefs.h>
+#include <X11/Xaw/XawInit.h>
+#include <X11/Xaw/StripCharP.h>
+
+extern WidgetClass mystripChartWidgetClass;
+
+
+
+/*************************************************
+*               Enumerations                     *
+*************************************************/
+
+/* Operations on the in-store message queue */
+
+enum { queue_noop, queue_add };
+
+/* Operations on the destinations queue */
+
+enum { dest_noop, dest_add, dest_remove };
+
+
+/*************************************************
+*          Structure for destinations            *
+*************************************************/
+
+typedef struct dest_item {
+  struct dest_item *next;
+  struct dest_item *parent;
+  uschar address[1];
+} dest_item;
+
+
+
+/*************************************************
+*           Structure for queue items            *
+*************************************************/
+
+typedef struct queue_item {
+  struct queue_item *next;
+  struct queue_item *prev;
+  struct dest_item  *destinations;
+  int  input_time;
+  int  update_time;
+  int  size;
+  uschar *sender;
+  uschar name[17];
+  uschar seen;
+  uschar frozen;
+  uschar dir_char;
+} queue_item;
+
+
+/*************************************************
+*          Structure for queue skip items        *
+*************************************************/
+
+typedef struct skip_item {
+  struct skip_item *next;
+  time_t reveal;
+  uschar text[1];
+} skip_item;
+
+
+/*************************************************
+*           Structure for delivery displays      *
+*************************************************/
+
+typedef struct pipe_item {
+  struct pipe_item *next;
+  int fd;
+  Widget widget;
+} pipe_item;
+
+
+
+/*************************************************
+*                Global variables                *
+*************************************************/
+
+extern Display *X_display;         /* Current display */
+extern XtAppContext X_appcon;      /* Application context */
+extern XtActionsRec actionTable[]; /* Actions table */
+
+extern XtTranslations queue_trans; /* translation table for queue text widget */
+extern XtTranslations text_trans;  /* translation table for other text widgets */
+
+extern Widget  dialog_ref_widget;   /* for positioning dialog box */
+extern Widget  toplevel_widget;
+extern Widget  log_widget;          /* widget for tail display */
+extern Widget  queue_widget;        /* widget for queue display */
+extern Widget  unhide_widget;       /* widget for unhide button */
+
+extern FILE   *LOG;
+
+extern int     action_output;       /* TRUE when wanting action command output */
+extern int     action_queue_update; /* controls auto updates */
+extern int     actionTableSize;     /* # entries in actionTable */
+extern uschar  actioned_message[];  /* For menu handling */
+extern uschar *action_required;
+extern uschar *alternate_config;    /* Alternate Exim configuration file */
+
+extern int     body_max;            /* Max size of body to display */
+
+extern int     eximon_initialized;  /* TRUE when initialized */
+
+extern int     log_buffer_size;     /* size of log buffer */
+extern BOOL    log_datestamping;    /* TRUE if logs are datestamped */
+extern int     log_depth;           /* depth of log tail window */
+extern uschar *log_display_buffer;  /* to hold display text */
+extern uschar *log_file;            /* supplied name of exim log file */
+extern uschar  log_file_open[256];  /* actual open file */
+extern uschar *log_font;            /* font for log display */
+extern ino_t   log_inode;           /* the inode of the log file */
+extern long int log_position;      /* position in log file */
+extern int     log_width;           /* width of log tail window */
+
+extern uschar *menu_event;          /* name of menu event */
+extern int     menu_is_up;          /* TRUE when menu displayed */
+extern int     min_height;          /* min window height */
+extern int     min_width;           /* min window width */
+
+extern pipe_item *pipe_chain;      /* for delivery displays */
+
+extern uschar *qualify_domain;
+extern int     queue_depth;         /* depth of queue window */
+extern uschar *queue_font;          /* font for queue display */
+extern int     queue_max_addresses; /* limit on per-message list */
+extern skip_item *queue_skip;      /* for hiding bits of queue */
+extern uschar *queue_stripchart_name; /* sic */
+extern int     queue_update;        /* update interval */
+extern int     queue_width;         /* width of queue window */
+
+extern pcre   *yyyymmdd_regex;    /* for matching yyyy-mm-dd */
+
+extern uschar *size_stripchart;     /* path for size monitoring */
+extern uschar *size_stripchart_name; /* name for size stripchart */
+extern uschar *spool_directory;     /* Name of exim spool directory */
+extern int     spool_is_split;      /* True if detected split spool */
+extern int     start_small;         /* True to start with small window */
+extern int     stripchart_height;   /* height of stripcharts */
+extern int     stripchart_number;   /* number of stripcharts */
+extern pcre  **stripchart_regex;  /* vector of regexps */
+extern uschar **stripchart_title;    /* vector of titles */
+extern int    *stripchart_total;    /* vector of accumulating values */
+extern int     stripchart_update;   /* update interval */
+extern int     stripchart_width;    /* width of stripcharts */
+extern int     stripchart_varstart; /* starting number for variable charts */
+
+extern int     text_depth;          /* depth of text windows */
+extern int     tick_queue_accumulator; /* For timing next auto update */
+
+extern uschar *window_title;        /* title of the exim monitor window */
+
+
+/*************************************************
+*                Global functions                *
+*************************************************/
+
+extern XtActionProc dialogAction(Widget, XEvent *, String *, Cardinal *);
+
+extern uschar *copystring(uschar *);
+extern void    create_dialog(uschar *, uschar *);
+extern void    create_stripchart(Widget, uschar *);
+extern void    debug(char *, ...);
+extern dest_item *find_dest(queue_item *, uschar *, int, BOOL);
+extern queue_item *find_queue(uschar *, int, int);
+extern void    init(int, uschar **);
+extern void    menu_create(Widget, XEvent *, String *, Cardinal *);
+extern void    NonMessageDialogue(uschar *);
+extern void    queue_display(void);
+extern void    read_log(void);
+extern int     read_spool(uschar *);
+extern int     read_spool_init(uschar *);
+extern void    read_spool_tidy(void);
+extern int     repaint_window(StripChartWidget, int, int);
+extern void    scan_spool_input(int);
+extern void    stripchart_init(void);
+extern void    text_empty(Widget);
+extern void    text_show(Widget, uschar *);
+extern void    text_showf(Widget, char *, ...);
+extern void    xs_SetValues(Widget, Cardinal, ...);
+
+/* End of em_hdr.h */
diff --git a/exim_monitor/em_init.c b/exim_monitor/em_init.c
new file mode 100644 (file)
index 0000000..c9ef66a
--- /dev/null
@@ -0,0 +1,237 @@
+/*************************************************
+*                  Exim monitor                  *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* This module contains code to initialize things from the
+environment and the arguments. */
+
+
+#include "em_hdr.h"
+
+
+
+/*************************************************
+*            Decode stripchart config            *
+*************************************************/
+
+/* First determine how many are requested, then compile the
+regular expressions and save the title strings. Note that
+stripchart_number is initialized to 1 or 2 to count the always-
+present queue stripchart, and the optional size-monitoring
+stripchart. */
+
+static void decode_stripchart_config(uschar *s)
+{
+int i;
+
+/* Loop: first time just counts, second time does the
+work. */
+
+for (i = 0; i <= 1; i++)
+
+  {
+  int first = 1;
+  int count = 0;
+  uschar *p = s;
+
+  if (*p == '/') p++;   /* allow optional / at start */
+
+  /* This loops for all the substrings, using the first flag
+  to determine whether each is the first or second of the pairs. */
+
+  while (*p)
+    {
+    uschar *pp;
+    /* Handle continuations */
+    if (*p == '\n')
+      {
+      while (*(++p) == ' ' || *p == '\t');
+      if (*p == '/') p++;
+      }
+
+    /* Find the end of the string and count if first string */
+
+    pp = p;
+    while (*p && *p != '/') p++;
+    if (first) count++;
+
+    /* Take action on the second time round. */
+
+    if (i != 0)
+      {
+      uschar buffer[256];
+      int indx = count + stripchart_varstart - 1;
+      Ustrncpy(buffer, pp, p-pp);
+      buffer[p-pp] = 0;
+      if (first)
+        {
+        int offset;
+        const uschar *error;
+        stripchart_regex[indx] = pcre_compile(CS buffer, PCRE_COPT,
+          (const char **)&error, &offset, NULL);
+        if (stripchart_regex[indx] == NULL)
+          {
+          printf("regular expression error: %s at offset %d "
+            "while compiling %s\n", error, offset, buffer);
+          exit(99);
+          }
+        }
+      else stripchart_title[indx] = string_copy(buffer);
+      }
+
+    /* Advance past the delimiter and flip the first/second flag */
+
+    p++;
+    first = !first;
+    }
+
+  /* On the first pass, we now know the number of stripcharts. Get
+  store for holding the pointers to the regular expressions and
+  title strings. */
+
+  if (i == 0)
+    {
+    stripchart_number += count;
+    stripchart_regex = (pcre **)store_malloc(stripchart_number * sizeof(pcre *));
+    stripchart_title = (uschar **)store_malloc(stripchart_number * sizeof(uschar *));
+    }
+  }
+}
+
+
+/*************************************************
+*                    Initialize                  *
+*************************************************/
+
+void init(int argc, uschar **argv)
+{
+int x;
+int erroroffset;
+uschar *s;
+const uschar *error;
+
+argc = argc;     /* These are currently unused. */
+argv = argv;
+
+/* Deal with simple values in the environment. */
+
+s = US getenv("ACTION_OUTPUT");
+if (s != NULL)
+  {
+  if (Ustrcmp(s, "no") == 0) action_output = FALSE;
+  if (Ustrcmp(s, "yes") == 0) action_output = TRUE;
+  }
+
+s = US getenv("ACTION_QUEUE_UPDATE");
+if (s != NULL)
+  {
+  if (Ustrcmp(s, "no") == 0) action_queue_update = FALSE;
+  if (Ustrcmp(s, "yes") == 0) action_queue_update = TRUE;
+  }
+
+s = US getenv("BODY_MAX");
+if (s != NULL && (x = Uatoi(s)) != 0) body_max = x;
+
+s = US getenv("EXIM_PATH");
+if (s != NULL) exim_path = string_copy(s);
+
+s = US getenv("EXIMON_EXIM_CONFIG");
+if (s != NULL) alternate_config = string_copy(s);
+
+s = US getenv("LOG_BUFFER");
+if (s != NULL)
+  {
+  uschar c[1];
+  if (sscanf(CS s, "%d%c", &x, c) > 0)
+    {
+    if (c[0] == 'K' || c[0] == 'k') x *= 1024;
+    if (x < 1024) x = 1024;
+    log_buffer_size = x;
+    }
+  }
+
+s = US getenv("LOG_DEPTH");
+if (s != NULL && (x = Uatoi(s)) != 0) log_depth = x;
+
+s = US getenv("LOG_FILE_NAME");
+if (s != NULL) log_file = string_copy(s);
+
+s = US getenv("LOG_FONT");
+if (s != NULL) log_font = string_copy(s);
+
+s = US getenv("LOG_WIDTH");
+if (s != NULL && (x = Uatoi(s)) != 0) log_width = x;
+
+s = US getenv("MENU_EVENT");
+if (s != NULL) menu_event = string_copy(s);
+
+s = US getenv("MIN_HEIGHT");
+if (s != NULL && (x = Uatoi(s)) > 0) min_height = x;
+
+s = US getenv("MIN_WIDTH");
+if (s != NULL && (x = Uatoi(s)) > 0) min_width = x;
+
+s = US getenv("QUALIFY_DOMAIN");
+if (s != NULL) qualify_domain = string_copy(s);
+  else qualify_domain = US"";  /* Don't want NULL */
+
+s = US getenv("QUEUE_DEPTH");
+if (s != NULL && (x = Uatoi(s)) != 0) queue_depth = x;
+
+s = US getenv("QUEUE_FONT");
+if (s != NULL) queue_font = string_copy(s);
+
+s = US getenv("QUEUE_INTERVAL");
+if (s != NULL && (x = Uatoi(s)) != 0) queue_update = x;
+
+s = US getenv("QUEUE_MAX_ADDRESSES");
+if (s != NULL && (x = Uatoi(s)) != 0) queue_max_addresses = x;
+
+s = US getenv("QUEUE_WIDTH");
+if (s != NULL && (x = Uatoi(s)) != 0) queue_width = x;
+
+s = US getenv("SPOOL_DIRECTORY");
+if (s != NULL) spool_directory = string_copy(s);
+
+s = US getenv("START_SMALL");
+if (s != NULL && Ustrcmp(s, "yes") == 0) start_small = 1;
+
+s = US getenv("TEXT_DEPTH");
+if (s != NULL && (x = Uatoi(s)) != 0) text_depth = x;
+
+s = US getenv("WINDOW_TITLE");
+if (s != NULL) window_title = string_copy(s);
+
+/* Deal with stripchart configuration. First see if we are monitoring
+the size of a partition, then deal with log stripcharts in a separate
+function */
+
+s = US getenv("SIZE_STRIPCHART");
+if (s != NULL && *s != 0)
+  {
+  stripchart_number++;
+  stripchart_varstart++;
+  size_stripchart = string_copy(s);
+  s = US getenv("SIZE_STRIPCHART_NAME");
+  if (s != NULL && *s != 0) size_stripchart_name = string_copy(s);
+  }
+
+s = US getenv("LOG_STRIPCHARTS");
+if (s != NULL) decode_stripchart_config(s);
+
+s = US getenv("STRIPCHART_INTERVAL");
+if (s != NULL && (x = Uatoi(s)) != 0) stripchart_update = x;
+
+s = US getenv("QUEUE_STRIPCHART_NAME");
+queue_stripchart_name = (s != NULL)? string_copy(s) : US"queue";
+
+/* Compile the regex for matching yyyy-mm-dd at the start of a string. */
+
+yyyymmdd_regex = pcre_compile("^\\d{4}-\\d\\d-\\d\\d\\s", PCRE_COPT,
+  (const char **)&error, &erroroffset, NULL);
+}
+
+/* End of em_init.c */
diff --git a/exim_monitor/em_log.c b/exim_monitor/em_log.c
new file mode 100644 (file)
index 0000000..6efd9c0
--- /dev/null
@@ -0,0 +1,406 @@
+/*************************************************
+*                 Exim Monitor                   *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2014 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* This module contains code for scanning the main log,
+extracting information from it, and displaying a "tail". */
+
+#include "em_hdr.h"
+
+#define log_buffer_len 4096      /* For each log entry */
+
+/* If anonymizing, don't alter these strings (this is all an ad hoc hack). */
+
+#ifdef ANONYMIZE
+static char *oklist[] = {
+  "Completed",
+  "defer",
+  "from",
+  "Connection timed out",
+  "Start queue run: pid=",
+  "End queue run: pid=",
+  "host lookup did not complete",
+  "unexpected disconnection while reading SMTP command from",
+  "verify failed for SMTP recipient",
+  "H=",
+  "U=",
+  "id=",
+  "<",
+  ">",
+  "(",
+  ")",
+  "[",
+  "]",
+  "@",
+  "=",
+  "*",
+  ".",
+  "-",
+  "\"",
+  " ",
+  "\n"};
+static int oklist_size = sizeof(oklist) / sizeof(uschar *);
+#endif
+
+
+
+/*************************************************
+*             Write to the log display           *
+*************************************************/
+
+static int visible = 0;
+static int scrolled = FALSE;
+static int size = 0;
+static int top = 0;
+
+static void show_log(char *s, ...) PRINTF_FUNCTION(1,2);
+
+static void show_log(char *s, ...)
+{
+int length, newtop;
+va_list ap;
+XawTextBlock b;
+uschar buffer[log_buffer_len + 24];
+
+/* Do nothing if not tailing a log */
+
+if (log_widget == NULL) return;
+
+/* Initialize the text block structure */
+
+b.firstPos = 0;
+b.ptr = CS buffer;
+b.format = FMT8BIT;
+
+/* We want to know whether the window has been scrolled back or not,
+so that we can cease automatically scrolling with new text. This turns
+out to be tricky with the text widget. We can detect whether the
+scroll bar has been operated by checking on the "top" value, but it's
+harder to detect that it has been returned to the bottom. The following
+heuristic does its best. */
+
+newtop = XawTextTopPosition(log_widget);
+if (newtop != top)
+  {
+  if (!scrolled)
+    {
+    visible = size - top;      /* save size of window */
+    scrolled = newtop < top;
+    }
+  else if (newtop > size - visible) scrolled = FALSE;
+  top = newtop;
+  }
+
+/* Format the text that is to be written. */
+
+va_start(ap, s);
+vsprintf(CS buffer, s, ap);
+va_end(ap);
+length = Ustrlen(buffer);
+
+/* If we are anonymizing for screen shots, flatten various things. */
+
+#ifdef ANONYMIZE
+  {
+  uschar *p = buffer + 9;
+  if (p[6] == '-' && p[13] == '-') p += 17;
+
+  while (p < buffer + length)
+    {
+    int i;
+
+    /* Check for strings to be left alone */
+
+    for (i = 0; i < oklist_size; i++)
+      {
+      int len = Ustrlen(oklist[i]);
+      if (Ustrncmp(p, oklist[i], len) == 0)
+        {
+        p += len;
+        break;
+        }
+      }
+    if (i < oklist_size) continue;
+
+    /* Leave driver names, size, protocol, alone */
+
+    if ((*p == 'D' || *p == 'P' || *p == 'T' || *p == 'S' || *p == 'R') &&
+        p[1] == '=')
+      {
+      p += 2;
+      while (*p != ' ' && *p != 0) p++;
+      continue;
+      }
+
+    /* Leave C= text alone */
+
+    if (Ustrncmp(p, "C=\"", 3) == 0)
+      {
+      p += 3;
+      while (*p != 0 && *p != '"') p++;
+      continue;
+      }
+
+    /* Flatten remaining chars */
+
+    if (isdigit(*p)) *p++ = 'x';
+    else if (isalpha(*p)) *p++ = 'x';
+    else *p++ = '$';
+    }
+  }
+#endif
+
+/* If this would overflow the buffer, throw away 50% of the
+current stuff in the buffer. Code defensively against odd
+extreme cases that shouldn't actually arise. */
+
+if (size + length > log_buffer_size)
+  {
+  if (size == 0) length = log_buffer_size/2; else
+    {
+    int cutcount = log_buffer_size/2;
+    if (cutcount > size) cutcount = size; else
+      {
+      while (cutcount < size && log_display_buffer[cutcount] != '\n')
+        cutcount++;
+      cutcount++;
+      }
+    b.length = 0;
+    XawTextReplace(log_widget, 0, cutcount, &b);
+    size -= cutcount;
+    top -= cutcount;
+    if (top < 0) top = 0;
+    if (top < cutcount) XawTextInvalidate(log_widget, 0, 999999);
+    xs_SetValues(log_widget, 1, "displayPosition", top);
+    }
+  }
+
+/* Insert the new text at the end of the buffer. */
+
+b.length = length;
+XawTextReplace(log_widget, 999999, 999999, &b);
+size += length;
+
+/* When not scrolled back, we want to keep the bottom line
+always visible. Put the insert point at the start of it because
+this stops left/right scrolling with some X libraries. */
+
+if (!scrolled)
+  {
+  XawTextSetInsertionPoint(log_widget, size - length);
+  top = XawTextTopPosition(log_widget);
+  }
+}
+
+
+
+
+/*************************************************
+*            Function to read the log            *
+*************************************************/
+
+/* We read any new log entries, and use their data to
+updated total counts for the configured stripcharts.
+The count for the queue chart is handled separately.
+We also munge the log entries and display a one-line
+version in the log window. */
+
+void read_log(void)
+{
+struct stat statdata;
+uschar buffer[log_buffer_len];
+
+/* If log is not yet open, skip all of this. */
+
+if (LOG != NULL)
+  {
+  fseek(LOG, log_position, SEEK_SET);
+
+  while (Ufgets(buffer, log_buffer_len, LOG) != NULL)
+    {
+    uschar *id;
+    uschar *p = buffer;
+    void *reset_point;
+    int length = Ustrlen(buffer);
+    int i;
+
+    /* Skip totally blank lines (paranoia: there shouldn't be any) */
+
+    while (*p == ' ' || *p == '\t') p++;
+    if (*p == '\n') continue;
+
+    /* We should now have a complete log entry in the buffer; check
+    it for various regular expression matches and take appropriate
+    action. Get the current store point so we can reset to it. */
+
+    reset_point = store_get(0);
+
+    /* First, update any stripchart data values, noting that the zeroth
+    stripchart is the queue length, which is handled elsewhere, and the
+    1st may the a size monitor. */
+
+    for (i = stripchart_varstart; i < stripchart_number; i++)
+      {
+      if (pcre_exec(stripchart_regex[i], NULL, CS buffer, length, 0, PCRE_EOPT,
+            NULL, 0) >= 0)
+        stripchart_total[i]++;
+      }
+
+    /* Munge the log entry and display shortened form on one line.
+    We omit the date and show only the time. Remove any time zone offset.
+    Take note of the presence of [pid]. */
+
+    if (pcre_exec(yyyymmdd_regex,NULL,CS buffer,length,0,PCRE_EOPT,NULL,0) >= 0)
+      {
+      int pidlength = 0;
+      if ((buffer[20] == '+' || buffer[20] == '-') &&
+          isdigit(buffer[21]) && buffer[25] == ' ')
+        memmove(buffer + 20, buffer + 26, Ustrlen(buffer + 26) + 1);
+      if (buffer[20] == '[')
+        {
+        while (Ustrchr("[]0123456789", buffer[20+pidlength++]) != NULL);
+        }
+      id = string_copyn(buffer + 20 + pidlength, MESSAGE_ID_LENGTH);
+      show_log("%s", buffer+11);
+      }
+    else
+      {
+      id = US"";
+      show_log("%s", buffer);
+      }
+
+    /* Deal with frozen and unfrozen messages */
+
+    if (strstric(buffer, US"frozen", FALSE) != NULL)
+      {
+      queue_item *qq = find_queue(id, queue_noop, 0);
+      if (qq != NULL)
+        {
+        if (strstric(buffer, US"unfrozen", FALSE) != NULL)
+          qq->frozen = FALSE;
+        else qq->frozen = TRUE;
+        }
+      }
+
+    /* Notice defer messages, and add the destination if it
+    isn't already on the list for this message, with a pointer
+    to the parent if we can. */
+
+    if ((p = Ustrstr(buffer, "==")) != NULL)
+      {
+      queue_item *qq = find_queue(id, queue_noop, 0);
+      if (qq != NULL)
+        {
+        dest_item *d;
+        uschar *q, *r;
+        p += 2;
+        while (isspace(*p)) p++;
+        q = p;
+        while (*p != 0 && !isspace(*p))
+          {
+          if (*p++ != '\"') continue;
+          while (*p != 0)
+            {
+            if (*p == '\\') p += 2;
+              else if (*p++ == '\"') break;
+            }
+          }
+        *p++ = 0;
+        if ((r = strstric(q, qualify_domain, FALSE)) != NULL &&
+          *(--r) == '@') *r = 0;
+
+        /* If we already have this destination, as tested case-insensitively,
+        do not add it to the destinations list. */
+
+        d = find_dest(qq, q, dest_add, TRUE);
+
+        if (d->parent == NULL)
+          {
+          while (isspace(*p)) p++;
+          if (*p == '<')
+            {
+            dest_item *dd;
+            q = ++p;
+            while (*p != 0 && *p != '>') p++;
+            *p = 0;
+            if ((p = strstric(q, qualify_domain, FALSE)) != NULL &&
+              *(--p) == '@') *p = 0;
+            dd = find_dest(qq, q, dest_noop, FALSE);
+            if (dd != NULL && dd != d) d->parent = dd;
+            }
+          }
+        }
+      }
+
+    store_reset(reset_point);
+    }
+  }
+
+
+/* We have to detect when the log file is changed, and switch to the new file.
+In practice, for non-datestamped files, this means that some deliveries might
+go unrecorded, since they'll be written to the old file, but this usually
+happens in the middle of the night, and I don't think the hassle of keeping
+track of two log files is worth it.
+
+First we check the datestamped name of the log file if necessary; if it is
+different to the file we currently have open, go for the new file. As happens
+in Exim itself, we leave in the following inode check, even when datestamping
+because it does no harm and will cope should a file actually be renamed for
+some reason.
+
+The test for a changed log file is to look up the inode of the file by name and
+compare it with the saved inode of the file we currently are processing. This
+accords with the usual interpretation of POSIX and other Unix specs that imply
+"one file, one inode". However, it appears that on some Digital systems, if an
+open file is unlinked, a new file may be created with the same inode while the
+old file remains in existence. This can happen if the old log file is renamed,
+processed in some way, and then deleted. To work round this, also test for a
+link count of zero on the currently open file. */
+
+if (log_datestamping)
+  {
+  uschar log_file_wanted[256];
+  /* Do *not* use "%s" here, we need the %D datestamp in the log_file to
+   *   be expanded! */
+  string_format(log_file_wanted, sizeof(log_file_wanted), CS log_file);
+  if (Ustrcmp(log_file_wanted, log_file_open) != 0)
+    {
+    if (LOG != NULL)
+      {
+      fclose(LOG);
+      LOG = NULL;
+      }
+    Ustrcpy(log_file_open, log_file_wanted);
+    }
+  }
+
+if (LOG == NULL ||
+    (fstat(fileno(LOG), &statdata) == 0 && statdata.st_nlink == 0) ||
+    (Ustat(log_file, &statdata) == 0 && log_inode != statdata.st_ino))
+  {
+  FILE *TEST;
+
+  /* Experiment shows that sometimes you can't immediately open
+  the new log file - presumably immediately after the old one
+  is renamed and before the new one exists. Therefore do a
+  trial open first to be sure. */
+
+  if ((TEST = fopen(CS log_file_open, "r")) != NULL)
+    {
+    if (LOG != NULL) fclose(LOG);
+    LOG = TEST;
+    fstat(fileno(LOG), &statdata);
+    log_inode = statdata.st_ino;
+    }
+  }
+
+/* Save the position we have got to in the log. */
+
+if (LOG != NULL) log_position = ftell(LOG);
+}
+
+/* End of em_log.c */
diff --git a/exim_monitor/em_main.c b/exim_monitor/em_main.c
new file mode 100644 (file)
index 0000000..d210a07
--- /dev/null
@@ -0,0 +1,939 @@
+/*************************************************
+*                  Exim Monitor                  *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2012 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+
+#include "em_hdr.h"
+
+/* This module contains the main program of the Exim monitor, which
+sets up the world and then lets the XtAppMainLoop function
+run things off X events. */
+
+
+/*************************************************
+*               Static variables                 *
+*************************************************/
+
+/* Fallback resources */
+
+static String fallback_resources[] = {"eximon.geometry: +150+0", NULL};
+
+/* X11 fixed argument lists */
+
+static Arg quit_args[] = {
+  {XtNfromVert, (XtArgVal) NULL},         /* must be first */
+  {XtNlabel,    (XtArgVal) " Quit "},
+  {"left",      XawChainLeft},
+  {"right",     XawChainLeft},
+  {"top",       XawChainTop},
+  {"bottom",    XawChainTop}
+};
+
+static Arg resize_args[] = {
+  {XtNfromVert, (XtArgVal) NULL},         /* must be first */
+  {XtNfromHoriz,(XtArgVal) NULL},         /* must be second */
+  {XtNlabel,    (XtArgVal) " Size "},
+  {"left",      XawChainLeft},
+  {"right",     XawChainLeft},
+  {"top",       XawChainTop},
+  {"bottom",    XawChainTop}
+};
+
+static Arg update_args[] = {
+  {XtNfromVert, (XtArgVal) NULL},         /* must be first */
+  {XtNlabel,    (XtArgVal) " Update "},
+  {"left",      XawChainLeft},
+  {"right",     XawChainLeft},
+  {"top",       XawChainTop},
+  {"bottom",    XawChainTop}
+};
+
+static Arg hide_args[] = {
+  {XtNfromVert, (XtArgVal) NULL},         /* must be first */
+  {XtNfromHoriz,(XtArgVal) NULL},         /* must be second */
+  {XtNlabel,    (XtArgVal) " Hide "},
+  {"left",      XawChainLeft},
+  {"right",     XawChainLeft},
+  {"top",       XawChainTop},
+  {"bottom",    XawChainTop}
+};
+
+static Arg unhide_args[] = {
+  {XtNfromVert, (XtArgVal) NULL},         /* must be first */
+  {XtNfromHoriz,(XtArgVal) NULL},         /* must be second */
+  {XtNlabel,    (XtArgVal) " Unhide "},
+  {"left",      XawChainLeft},
+  {"right",     XawChainLeft},
+  {"top",       XawChainTop},
+  {"bottom",    XawChainTop}
+};
+
+static Arg log_args[] = {
+  {XtNfromVert, (XtArgVal) NULL},         /* must be first */
+  {"editType",  XawtextEdit},
+  {"useStringInPlace", (XtArgVal)TRUE},
+  {"string",    (XtArgVal)""},            /* dummy to get it going */
+  {"scrollVertical", XawtextScrollAlways},
+  {"scrollHorizontal", XawtextScrollAlways},
+  {"right",     XawChainRight},
+  {"top",       XawChainTop},
+  {"bottom",    XawChainTop}
+};
+
+static Arg queue_args[] = {
+  {XtNfromVert, (XtArgVal) NULL},         /* must be first */
+  {"editType",  XawtextEdit},
+  {"string",    (XtArgVal)""},            /* dummy to get it going */
+  {"scrollVertical", XawtextScrollAlways},
+  {"right",     XawChainRight},
+  {"top",       XawChainTop},
+  {"bottom",    XawChainBottom}
+};
+
+static Arg sizepos_args[] = {
+  {"width",     (XtArgVal)NULL},
+  {"height",    (XtArgVal)NULL},
+  {"x",         (XtArgVal)NULL},
+  {"y",         (XtArgVal)NULL}
+};
+
+XtActionsRec menu_action_table[] = {
+  { "menu-create",  menu_create } };
+
+/* Types of non-message dialog action */
+
+enum { da_hide };
+
+/* Miscellaneous local variables */
+
+static int dialog_action;
+static int tick_stripchart_accumulator = 999999;
+static int tick_interval = 2;
+static int maxposset = 0;
+static int minposset = 0;
+static int x_adjustment = -1;
+static int y_adjustment = -1;
+static Dimension screenwidth, screenheight;
+static Dimension original_x, original_y;
+static Dimension maxposx, maxposy;
+static Dimension minposx, minposy;
+static Dimension maxwidth, maxheight;
+static Widget outer_form_widget;
+static Widget hide_widget;
+static Widget above_queue_widget;
+
+
+
+
+#ifdef STRERROR_FROM_ERRLIST
+/*************************************************
+*     Provide strerror() for non-ANSI libraries  *
+*************************************************/
+
+/* Some old-fashioned systems still around (e.g. SunOS4) don't have strerror()
+in their libraries, but can provide the same facility by this simple
+alternative function. */
+
+uschar *
+strerror(int n)
+{
+if (n < 0 || n >= sys_nerr) return "unknown error number";
+return sys_errlist[n];
+}
+#endif /* STRERROR_FROM_ERRLIST */
+
+
+
+/*************************************************
+*         Handle attempts to write the log       *
+*************************************************/
+
+/* The message gets written to stderr when log_write() is called from a
+utility. The message always gets '\n' added on the end of it. These calls come
+from modules such as store.c when things go drastically wrong (e.g. malloc()
+failing). In normal use they won't get obeyed.
+
+Arguments:
+  selector  not relevant when running a utility
+  flags     not relevant when running a utility
+  format    a printf() format
+  ...       arguments for format
+
+Returns:    nothing
+*/
+
+void
+log_write(unsigned int selector, int flags, const char *format, ...)
+{
+va_list ap;
+va_start(ap, format);
+vfprintf(stderr, format, ap);
+fprintf(stderr, "\n");
+va_end(ap);
+selector = selector;     /* Keep picky compilers happy */
+flags = flags;
+}
+
+
+
+
+/*************************************************
+*        Extract port from address string        *
+*************************************************/
+
+/* In the spool file, a host plus port is given as an IP address followed by a
+dot and a port number. This function decodes this. It is needed by the
+spool-reading function, and copied here to avoid having to include the whole
+host.c module. One day the interaction between exim and eximon with regard to
+included code MUST be tidied up!
+
+Argument:
+  address    points to the string; if there is a port, the '.' in the string
+             is overwritten with zero to terminate the address
+
+Returns:     0 if there is no port, else the port number.
+*/
+
+int
+host_address_extract_port(uschar *address)
+{
+int skip = -3;                     /* Skip 3 dots in IPv4 addresses */
+address--;
+while (*(++address) != 0)
+  {
+  int ch = *address;
+  if (ch == ':') skip = 0;         /* Skip 0 dots in IPv6 addresses */
+    else if (ch == '.' && skip++ >= 0) break;
+  }
+if (*address == 0) return 0;
+*address++ = 0;
+return Uatoi(address);
+}
+
+
+
+
+/*************************************************
+*                SIGCHLD handler                 *
+*************************************************/
+
+/* Operations on messages are done in subprocesses; this handler
+just catches them when they finish. It causes a queue display update
+unless configured not to. */
+
+static void sigchld_handler(int sig)
+{
+while (waitpid(-1, NULL, WNOHANG) > 0);
+signal(sig, sigchld_handler);
+if (action_queue_update) tick_queue_accumulator = 999999;
+}
+
+
+
+/*************************************************
+*             Callback routines                  *
+*************************************************/
+
+
+void updateAction(Widget w, XtPointer client_data, XtPointer call_data)
+{
+w = w;       /* Keep picky compilers happy */
+client_data = client_data;
+call_data = call_data;
+scan_spool_input(TRUE);
+queue_display();
+tick_queue_accumulator = 0;
+}
+
+void hideAction(Widget w, XtPointer client_data, XtPointer call_data)
+{
+w = w;       /* Keep picky compilers happy */
+client_data = client_data;
+call_data = call_data;
+actioned_message[0] = 0;
+dialog_ref_widget = w;
+dialog_action = da_hide;
+create_dialog(US"Hide addresses ending with", US"");
+}
+
+void unhideAction(Widget w, XtPointer client_data, XtPointer call_data)
+{
+skip_item *sk = queue_skip;
+
+w = w;       /* Keep picky compilers happy */
+client_data = client_data;
+call_data = call_data;
+
+while (sk != NULL)
+  {
+  skip_item *next = sk->next;
+  store_free(sk);
+  sk = next;
+  }
+queue_skip = NULL;
+
+XtDestroyWidget(unhide_widget);
+unhide_widget = NULL;
+
+scan_spool_input(TRUE);
+queue_display();
+tick_queue_accumulator = 0;
+}
+
+void quitAction(Widget w, XtPointer client_data, XtPointer call_data)
+{
+w = w;       /* Keep picky compilers happy */
+client_data = client_data;
+call_data = call_data;
+exit(0);
+}
+
+
+/* Action when the "Size" button is pressed. This is a kludged up mess
+that I made work after much messing around. Reading the position of the
+toplevel widget gets the absolute position of the data portion of the window,
+excluding the window manager's furniture. However, positioning the toplevel
+widget's window seems to position the top corner of the furniture under the twm
+window manager, but not under fwvm and others. The two cases are distinguished
+by the values of x_adjustment and y_adjustment.
+
+For twm (adjustment >= 0), one has to fudge the miminizing function to ensure
+that we go back to exactly the same position as before.
+
+For fwvm (adjustment < 0), one has to fudge the "top left hand corner"
+positioning to ensure that the window manager's furniture gets displayed on the
+screen. I haven't found a way of discovering the thickness of the furniture, so
+some screwed-in values are used.
+
+This is all ad hoc, developed by floundering around as I haven't found any
+documentation that tells me what I really should do. */
+
+void resizeAction(Widget button, XtPointer client_data, XtPointer call_data)
+{
+Dimension x, y;
+Dimension width, height;
+XWindowAttributes a;
+Window w = XtWindow(toplevel_widget);
+
+button = button;    /* Keep picky compilers happy */
+client_data = client_data;
+call_data = call_data;
+
+/* Get the position and size of the top level widget. */
+
+sizepos_args[0].value = (XtArgVal)(&width);
+sizepos_args[1].value = (XtArgVal)(&height);
+sizepos_args[2].value = (XtArgVal)(&x);
+sizepos_args[3].value = (XtArgVal)(&y);
+XtGetValues(toplevel_widget, sizepos_args, 4);
+
+/* Get the position of the widget's window relative to its parent; this
+gives the thickness of the window manager's furniture. At least it does
+in twm. For fwvm it gives zero. The size/movement function uses this data.
+I tried doing this before entering the main loop, but it didn't always
+work properly with twm. Running it every time seems to be OK. */
+
+XGetWindowAttributes(X_display, XtWindow(toplevel_widget), &a);
+if (a.x != 0) x_adjustment = a.x;
+if (a.y != 0) y_adjustment = a.y;
+
+/* If at maximum size, reduce to minimum and move back to where it was
+when maximized, if that value is set, allowing for the furniture in cases
+where the positioning includes the furniture. */
+
+if (width == maxwidth && height == maxheight)
+  {
+  maxposx = x;
+  maxposy = y;
+  maxposset = 1;
+
+  if (minposset)
+    xs_SetValues(toplevel_widget, 4,
+      "width",     min_width,
+      "height",    min_height,
+      "x",         minposx - ((x_adjustment >= 0)? x_adjustment : 0),
+      "y",         minposy - ((y_adjustment >= 0)? y_adjustment : 0));
+  else
+    xs_SetValues(toplevel_widget, 2,
+      "width",     min_width,
+      "height",    min_height);
+  }
+
+/* Else always expand to maximum. If currently at minimum size, remember where
+it was for coming back. If we don't have a value for the thickness of the
+furniture, the implication is that the coordinates position the application
+window, so we can't use (0,0) because that loses the furniture. Use screwed in
+values that seem to work with fvwm. */
+
+else
+  {
+  int xx = x;
+  int yy = y;
+
+  if (width == min_width && height == min_height)
+    {
+    minposx = x;
+    minposy = y;
+    minposset = 1;
+    }
+
+  if ((int)(x + maxwidth) > (int)screenwidth ||
+      (int)(y + maxheight + 10) > (int)screenheight)
+    {
+    if (maxposset)
+      {
+      xx = maxposx - ((x_adjustment >= 0)? x_adjustment : 0);
+      yy = maxposy - ((y_adjustment >= 0)? y_adjustment : 0);
+      }
+    else
+      {
+      if ((int)(x + maxwidth) > (int)screenwidth)
+        xx = (x_adjustment >= 0)? 0 : 4;
+      if ((int)(y + maxheight + 10) > (int)screenheight)
+        yy = (y_adjustment >= 0)? 0 : 21;
+      }
+
+    xs_SetValues(toplevel_widget, 4,
+      "width",     maxwidth,
+      "height",    maxheight,
+      "x",         xx,
+      "y",         yy);
+    }
+
+  else xs_SetValues(toplevel_widget, 2,
+        "width",     maxwidth,
+        "height",    maxheight);
+  }
+
+/* Ensure the window is at the top */
+
+XRaiseWindow(X_display, w);
+}
+
+
+
+
+/*************************************************
+*          Handle input from non-msg dialogue    *
+*************************************************/
+
+/* The various cases here are: hide domain, (no more yet) */
+
+void NonMessageDialogue(uschar *s)
+{
+skip_item *sk;
+
+switch(dialog_action)
+  {
+  case da_hide:
+
+  /* Create the unhide button if not present */
+
+  if (unhide_widget == NULL)
+    {
+    unhide_args[0].value = (XtArgVal) above_queue_widget;
+    unhide_args[1].value = (XtArgVal) hide_widget;
+    unhide_widget = XtCreateManagedWidget("unhide", commandWidgetClass,
+      outer_form_widget, unhide_args, XtNumber(unhide_args));
+    XtAddCallback(unhide_widget, "callback",  unhideAction, NULL);
+    }
+
+  /* Add item to skip queue */
+
+  sk = (skip_item *)store_malloc(sizeof(skip_item) + Ustrlen(s));
+  sk->next = queue_skip;
+  queue_skip = sk;
+  Ustrcpy(sk->text, s);
+  sk->reveal = time(NULL) + 60 * 60;
+  scan_spool_input(TRUE);
+  queue_display();
+  tick_queue_accumulator = 0;
+  break;
+  }
+}
+
+
+
+/*************************************************
+*              Ticker function                   *
+*************************************************/
+
+/* This function is called initially to set up the starting data
+values; it then sets a timeout so that it continues to be called
+every 2 seconds. */
+
+static void ticker(XtPointer pt, XtIntervalId *i)
+{
+pipe_item **pp = &pipe_chain;
+pipe_item *p = pipe_chain;
+tick_queue_accumulator += tick_interval;
+tick_stripchart_accumulator += tick_interval;
+read_log();
+
+pt = pt;    /* Keep picky compilers happy */
+i = i;
+
+/* If we have passed the queue update time, we must do a full
+scan of the queue, checking for new arrivals, etc. This will
+as a by-product set the count of items for use by the stripchart
+display. On some systems, SIGCHLD signals can get lost at busy times,
+so just in case, clean up any completed children here. */
+
+if (tick_queue_accumulator >= queue_update)
+  {
+  scan_spool_input(TRUE);
+  queue_display();
+  tick_queue_accumulator = 0;
+  if (tick_stripchart_accumulator >= stripchart_update)
+    tick_stripchart_accumulator = 0;
+  while (waitpid(-1, NULL, WNOHANG) > 0);
+  }
+
+/* Otherwise, if we have exceeded the stripchart interval,
+do a reduced queue scan that simply provides the count for
+the stripchart. */
+
+else if (tick_stripchart_accumulator >= stripchart_update)
+  {
+  scan_spool_input(FALSE);
+  tick_stripchart_accumulator = 0;
+  }
+
+/* Scan any pipes that are set up for listening to delivery processes,
+and display their output if their windows are still open. */
+
+while (p != NULL)
+  {
+  int count;
+  uschar buffer[256];
+
+  while ((count = read(p->fd, buffer, 254)) > 0)
+    {
+    buffer[count] = 0;
+    if (p->widget != NULL) text_show(p->widget, buffer);
+    }
+
+  if (count == 0)
+    {
+    close(p->fd);
+    *pp = p->next;
+    store_free(p);
+    /* If configured, cause display update */
+    if (action_queue_update) tick_queue_accumulator = 999999;
+    }
+
+  else pp = &(p->next);
+
+  p = *pp;
+  }
+
+/* Reset the timer for next time */
+
+XtAppAddTimeOut(X_appcon, tick_interval * 1000, ticker, 0);
+}
+
+
+
+/*************************************************
+*             Find Num Lock modifiers            *
+*************************************************/
+
+/* Return a string with the modifiers generated by XK_Num_Lock, or return
+NULL if XK_Num_Lock doesn't generate any modifiers. This is needed because Num
+Lock isn't always the same modifier on all servers.
+
+Arguments:
+  display   the Display
+  buf       a buffer in which to put the answers (long enough to hold 5)
+
+Returns:    points to the buffer, or NULL
+*/
+
+static uschar *
+numlock_modifiers(Display *display, uschar *buf)
+{
+XModifierKeymap *m;
+int i, j;
+uschar *ret = NULL;
+
+m = XGetModifierMapping(display);
+if (m == NULL)
+  {
+  printf("Not enough memory\n");
+  exit (EXIT_FAILURE);
+  }
+
+/* Look at Mod1 through Mod5, and fill in the buffer as necessary. */
+
+buf[0] = 0;
+for (i = 3; i < 8; i++)
+  {
+  for (j = 0; j < m->max_keypermod; j++)
+    {
+    if (XKeycodeToKeysym(display, m->modifiermap [i*m->max_keypermod + j], 0)
+        == XK_Num_Lock)
+      {
+      sprintf(CS(buf+Ustrlen(buf)), " Mod%d", i-2);
+      ret = buf;
+      }
+    }
+  }
+
+XFreeModifiermap(m);
+return ret;
+}
+
+
+
+/*************************************************
+*               Initialize                       *
+*************************************************/
+
+int main(int argc, char **argv)
+{
+int i;
+struct stat statdata;
+uschar modbuf[] = " Mod1 Mod2 Mod3 Mod4 Mod5";
+uschar *numlock;
+Widget stripchart_form_widget,
+       update_widget,
+       quit_widget,
+       resize_widget;
+
+/* The exim global message_id needs to get set */
+
+message_id_external = message_id_option + 1;
+message_id = message_id_external + 1;
+message_subdir[1] = 0;
+
+/* Some store needs getting for big_buffer, which is used for
+constructing file names and things. This call will initialize
+the store_get() function. */
+
+big_buffer_size = 1024;
+big_buffer = store_get(big_buffer_size);
+
+/* Set up the version string and date and output them */
+
+version_init();
+printf("\nExim Monitor version %s (compiled %s) initializing\n",
+  version_string, version_date);
+
+/* Initialize various things from the environment and arguments. */
+
+init(argc, USS argv);
+
+/* Set up the SIGCHLD handler */
+
+signal(SIGCHLD, sigchld_handler);
+
+/* Get the buffer for storing the string for the log display. */
+
+log_display_buffer = (uschar *)store_malloc(log_buffer_size);
+log_display_buffer[0] = 0;
+
+/* Initialize the data structures for the stripcharts */
+
+stripchart_init();
+
+/* If log_file contains the empty string, then Exim is running using syslog
+only, and we can't tail the log. If not, open the log file and position to the
+end of it. Before doing so, we have to detect whether the log files are
+datestamped, and if so, sort out the name. The string in log_file already has
+%s replaced by "main"; if datestamping is occurring, %D or %M will be present.
+In fact, we don't need to test explicitly - just process the string with
+string_format.
+
+Once opened, save the file's inode so that we can detect when the file is
+switched to another one for non-datestamped files. However, allow the monitor
+to start up without a log file (can happen if no messages have been sent
+today.) */
+
+if (log_file[0] != 0)
+  {
+  /* Do *not* use "%s" here, we need the %D datestamp in the log_file to
+  be expanded! */
+  (void)string_format(log_file_open, sizeof(log_file_open), CS log_file);
+  log_datestamping = string_datestamp_offset >= 0;
+
+  LOG = fopen(CS log_file_open, "r");
+
+  if (LOG == NULL)
+    {
+    printf("*** eximon warning: can't open log file %s - will try "
+      "periodically\n", log_file_open);
+    }
+  else
+    {
+    fseek(LOG, 0, SEEK_END);
+    log_position = ftell(LOG);
+    fstat(fileno(LOG), &statdata);
+    log_inode = statdata.st_ino;
+    }
+  }
+else
+  {
+  printf("*** eximon warning: no log file available to tail\n");
+  }
+
+/* Now initialize the X world and create the top-level widget */
+
+toplevel_widget = XtAppInitialize(&X_appcon, "Eximon", NULL, 0, &argc, argv,
+  fallback_resources, NULL, 0);
+X_display = XtDisplay(toplevel_widget);
+xs_SetValues(toplevel_widget, 4,
+  "title",     window_title,
+  "iconName",  window_title,
+  "minWidth",  min_width,
+  "minHeight", min_height);
+
+
+/* Create the action for setting up the menu in the queue display
+window, and register the action for positioning the menu. */
+
+XtAppAddActions(X_appcon, menu_action_table, 1);
+XawSimpleMenuAddGlobalActions(X_appcon);
+
+/* Set up translation tables for the text widgets we use. We don't
+want all the generality of editing, etc. that the defaults provide.
+This cannot be done before initializing X - the parser complains
+about unknown events, modifiers, etc. in an unhelpful way... The
+queue text widget has a different table which includes the button
+for popping up the menu. Note that the order of things in these
+tables is significant. Shift<thing> must come before <thing> as
+otherwise it isn't noticed. */
+
+/*
+   <FocusIn>:      display-caret(on)\n\
+   <FocusOut>:     display-caret(off)\n\
+*/
+
+/* The translation manager sets up passive grabs for the menu popups as a
+result of MenuPopup(), but the grabs match only the exact modifiers listed,
+hence combinations with and without caps-lock and num-lock must be given,
+rather than just one "Shift<Btn1Down>" (or whatever menu_event is set to),
+despite the fact that that notation (without a leading !) should ignore the
+state of other modifiers. Thanks to Kevin Ryde for this information, and for
+the function above that discovers which modifier is Num Lock, because it turns
+out that it varies from server to server. */
+
+sprintf(CS big_buffer,
+  "!%s:            menu-create() XawPositionSimpleMenu(menu) MenuPopup(menu)\n\
+   !Lock %s:       menu-create() XawPositionSimpleMenu(menu) MenuPopup(menu)\n\
+  ", menu_event, menu_event);
+
+numlock = numlock_modifiers(X_display, modbuf); /* Get Num Lock modifier(s) */
+
+if (numlock != NULL) sprintf(CS big_buffer + Ustrlen(big_buffer),
+  "!%s %s:         menu-create() XawPositionSimpleMenu(menu) MenuPopup(menu)\n\
+   !Lock %s %s:    menu-create() XawPositionSimpleMenu(menu) MenuPopup(menu)\n\
+  ", numlock, menu_event, numlock, menu_event);
+
+sprintf(CS big_buffer + Ustrlen(big_buffer),
+  "<Btn1Down>:     select-start()\n\
+   <Btn1Motion>:   extend-adjust()\n\
+   <Btn1Up>:       extend-end(PRIMARY,CUT_BUFFER0)\n\
+   <Btn3Down>:     extend-start()\n\
+   <Btn3Motion>:   extend-adjust()\n\
+   <Btn3Up>:       extend-end(PRIMARY,CUT_BUFFER0)\n\
+   <Key>Up:        scroll-one-line-down()\n\
+   <Key>Down:      scroll-one-line-up()\n\
+   Ctrl<Key>R:     search(backward)\n\
+   Ctrl<Key>S:     search(forward)\n\
+  ");
+
+queue_trans = XtParseTranslationTable(CS big_buffer);
+
+text_trans = XtParseTranslationTable(
+  "<Btn1Down>:     select-start()\n\
+   <Btn1Motion>:   extend-adjust()\n\
+   <Btn1Up>:       extend-end(PRIMARY,CUT_BUFFER0)\n\
+   <Btn3Down>:     extend-start()\n\
+   <Btn3Motion>:   extend-adjust()\n\
+   <Btn3Up>:       extend-end(PRIMARY,CUT_BUFFER0)\n\
+   <Key>Up:        scroll-one-line-down()\n\
+   <Key>Down:      scroll-one-line-up()\n\
+   Ctrl<Key>R:     search(backward)\n\
+   Ctrl<Key>S:     search(forward)\n\
+  ");
+
+
+/* Create a toplevel form widget to hold all the other things */
+
+outer_form_widget = XtCreateManagedWidget("form", formWidgetClass,
+  toplevel_widget, NULL, 0);
+
+/* Now create an inner form to hold the stripcharts */
+
+stripchart_form_widget = XtCreateManagedWidget("form", formWidgetClass,
+  outer_form_widget, NULL, 0);
+xs_SetValues(stripchart_form_widget, 5,
+  "defaultDistance", 8,
+  "left",            XawChainLeft,
+  "right",           XawChainLeft,
+  "top",             XawChainTop,
+  "bottom",          XawChainTop);
+
+/* Create the queue count stripchart and its label. */
+
+create_stripchart(stripchart_form_widget, queue_stripchart_name);
+
+/* If configured, create the size monitoring stripchart, but
+only if the OS supports statfs(). */
+
+if (size_stripchart != NULL)
+  {
+#ifdef HAVE_STATFS
+  if (size_stripchart_name == NULL)
+    {
+    size_stripchart_name = size_stripchart + Ustrlen(size_stripchart) - 1;
+    while (size_stripchart_name > size_stripchart &&
+      *size_stripchart_name == '/') size_stripchart_name--;
+    while (size_stripchart_name > size_stripchart &&
+      *size_stripchart_name != '/') size_stripchart_name--;
+    }
+  create_stripchart(stripchart_form_widget, size_stripchart_name);
+#else
+  printf("Can't create size stripchart: statfs() function not available\n");
+#endif
+  }
+
+/* Now create the configured input/output stripcharts; note
+the total number includes the queue stripchart. */
+
+for (i = stripchart_varstart; i < stripchart_number; i++)
+  create_stripchart(stripchart_form_widget, stripchart_title[i]);
+
+/* Next in vertical order come the Resize & Quit buttons */
+
+quit_args[0].value = (XtArgVal) stripchart_form_widget;
+quit_widget = XtCreateManagedWidget("quit", commandWidgetClass,
+  outer_form_widget, quit_args, XtNumber(quit_args));
+XtAddCallback(quit_widget, "callback",  quitAction, NULL);
+
+resize_args[0].value = (XtArgVal) stripchart_form_widget;
+resize_args[1].value = (XtArgVal) quit_widget;
+resize_widget = XtCreateManagedWidget("resize", commandWidgetClass,
+  outer_form_widget, resize_args, XtNumber(resize_args));
+XtAddCallback(resize_widget, "callback",  resizeAction, NULL);
+
+/* In the absence of log tailing, the quit widget is the one above the
+queue listing. */
+
+above_queue_widget = quit_widget;
+
+/* Create an Ascii text widget for the log tail display if we are tailing a
+log. Skip it if not. */
+
+if (log_file[0] != 0)
+  {
+  log_args[0].value = (XtArgVal) quit_widget;
+  log_widget = XtCreateManagedWidget("log", asciiTextWidgetClass,
+    outer_form_widget, log_args, XtNumber(log_args));
+  XawTextDisplayCaret(log_widget, TRUE);
+  xs_SetValues(log_widget, 6,
+    "editType",  XawtextEdit,
+    "translations", text_trans,
+    "string",    log_display_buffer,
+    "length",    log_buffer_size,
+    "height",    log_depth,
+    "width",     log_width);
+
+  if (log_font != NULL)
+    {
+    XFontStruct *f = XLoadQueryFont(X_display, CS log_font);
+    if (f != NULL) xs_SetValues(log_widget, 1, "font", f);
+    }
+
+  above_queue_widget = log_widget;
+  }
+
+/* The update button */
+
+update_args[0].value = (XtArgVal) above_queue_widget;
+update_widget = XtCreateManagedWidget("update", commandWidgetClass,
+  outer_form_widget, update_args, XtNumber(update_args));
+XtAddCallback(update_widget, "callback",  updateAction, NULL);
+
+/* The hide button */
+
+hide_args[0].value = (XtArgVal) above_queue_widget;
+hide_args[1].value = (XtArgVal) update_widget;
+hide_widget = XtCreateManagedWidget("hide", commandWidgetClass,
+  outer_form_widget, hide_args, XtNumber(hide_args));
+XtAddCallback(hide_widget, "callback",  hideAction, NULL);
+
+/* Create an Ascii text widget for the queue display. */
+
+queue_args[0].value = (XtArgVal) update_widget;
+queue_widget = XtCreateManagedWidget("queue", asciiTextWidgetClass,
+  outer_form_widget, queue_args, XtNumber(queue_args));
+XawTextDisplayCaret(queue_widget, TRUE);
+
+xs_SetValues(queue_widget, 4,
+  "editType",  XawtextEdit,
+  "height",    queue_depth,
+  "width",     queue_width,
+  "translations", queue_trans);
+
+if (queue_font != NULL)
+  {
+  XFontStruct *f = XLoadQueryFont(X_display, CS queue_font);
+  if (f != NULL) xs_SetValues(queue_widget, 1, "font", f);
+  }
+
+/* Call the ticker function to get the initial data set up. It
+arranges to have itself recalled every 2 seconds. */
+
+ticker(NULL, NULL);
+
+/* Everything is now set up; this flag is used by the regerror
+function and also by the queue reader. */
+
+eximon_initialized = TRUE;
+printf("\nExim Monitor running\n");
+
+/* Realize the toplevel and thereby get things displayed */
+
+XtRealizeWidget(toplevel_widget);
+
+/* Find out the size of the initial window, and set that as its
+maximum. While we are at it, get the initial position. */
+
+sizepos_args[0].value = (XtArgVal)(&maxwidth);
+sizepos_args[1].value = (XtArgVal)(&maxheight);
+sizepos_args[2].value = (XtArgVal)(&original_x);
+sizepos_args[3].value = (XtArgVal)(&original_y);
+XtGetValues(toplevel_widget, sizepos_args, 4);
+
+xs_SetValues(toplevel_widget, 2,
+  "maxWidth",  maxwidth,
+  "maxHeight", maxheight);
+
+/* Set up the size of the screen */
+
+screenwidth = XDisplayWidth(X_display, 0);
+screenheight= XDisplayHeight(X_display,0);
+
+/* Register the action table */
+
+XtAppAddActions(X_appcon, actionTable, actionTableSize);
+
+/* Reduce the window to the small size if this is wanted */
+
+if (start_small) resizeAction(NULL, NULL, NULL);
+
+/* Enter the application loop which handles things from here
+onwards. The return statement is never obeyed, but is needed to
+keep pedantic ANSI compilers happy. */
+
+XtAppMainLoop(X_appcon);
+
+return 0;
+}
+
+/* End of em_main.c */
+
diff --git a/exim_monitor/em_menu.c b/exim_monitor/em_menu.c
new file mode 100644 (file)
index 0000000..9d3ca1c
--- /dev/null
@@ -0,0 +1,984 @@
+/*************************************************
+*                  Exim Monitor                  *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+
+#include "em_hdr.h"
+
+/* This module contains code for handling the popup menus. */
+
+static Widget menushell;
+static Widget queue_text_sink;
+static Widget dialog_shell, dialog_widget;
+
+static Widget text_create(uschar *, int);
+
+static int highlighted_start, highlighted_end, highlighted_x, highlighted_y;
+
+
+
+static Arg queue_get_arg[] = {
+  { "textSink",   (XtArgVal)NULL },
+  { "textSource", (XtArgVal)NULL },
+  { "string",     (XtArgVal)NULL } };
+
+static Arg dialog_arg[] = {
+  { "label",      (XtArgVal)"dialog" },
+  { "value",      (XtArgVal)"value" } };
+
+static Arg get_pos_args[] = {
+  {"x",           (XtArgVal)NULL },
+  {"y",           (XtArgVal)NULL } };
+
+static Arg menushell_arg[] = {
+  { "label",      (XtArgVal)NULL } };
+
+static Arg button_arg[] = {
+  { XtNfromVert, (XtArgVal) NULL },         /* must be first */
+  { XtNlabel,    (XtArgVal) " Dismiss " },
+  { "left",      XawChainLeft },
+  { "right",     XawChainLeft },
+  { "top",       XawChainBottom },
+  { "bottom",    XawChainBottom } };
+
+static Arg text_arg[] = {
+  { XtNfromVert, (XtArgVal) NULL },         /* must be first */
+  { "editType",  XawtextEdit },
+  { "string",    (XtArgVal)"" },            /* dummy to get it going */
+  { "scrollVertical", XawtextScrollAlways },
+  { "wrap",      XawtextWrapWord },
+  { "top",       XawChainTop },
+  { "bottom",    XawChainBottom } };
+
+static Arg item_1_arg[] = {
+  { XtNfromVert,  (XtArgVal)NULL },         /* must be first */
+  { "label",      (XtArgVal)" Message log" } };
+
+static Arg item_2_arg[] = {
+  { XtNfromVert,  (XtArgVal) NULL },        /* must be first */
+  { "label",      (XtArgVal)" Headers" } };
+
+static Arg item_3_arg[] = {
+  { XtNfromVert,  (XtArgVal) NULL },        /* must be first */
+  { "label",      (XtArgVal)" Body" } };
+
+static Arg item_4_arg[] = {
+  { XtNfromVert,  (XtArgVal) NULL },        /* must be first */
+  { "label",      (XtArgVal)" Deliver message" } };
+
+static Arg item_5_arg[] = {
+  { XtNfromVert,  (XtArgVal) NULL },        /* must be first */
+  { "label",      (XtArgVal)" Freeze message" } };
+
+static Arg item_6_arg[] = {
+  { XtNfromVert,  (XtArgVal) NULL },        /* must be first */
+  { "label",      (XtArgVal)" Thaw message" } };
+
+static Arg item_7_arg[] = {
+  { XtNfromVert,  (XtArgVal) NULL },        /* must be first */
+  { "label",      (XtArgVal)" Give up on msg" } };
+
+static Arg item_8_arg[] = {
+  { XtNfromVert,  (XtArgVal) NULL },        /* must be first */
+  { "label",      (XtArgVal)" Remove message" } };
+
+static Arg item_9_arg[] = {
+  { XtNfromVert,  (XtArgVal) NULL },        /* must be first */
+  { "label",      (XtArgVal)"----------------" } };
+
+static Arg item_10_arg[] = {
+  { XtNfromVert,  (XtArgVal) NULL },        /* must be first */
+  { "label",      (XtArgVal)" Add recipient" } };
+
+static Arg item_11_arg[] = {
+  { XtNfromVert,  (XtArgVal) NULL },        /* must be first */
+  { "label",      (XtArgVal)" Mark delivered" } };
+
+static Arg item_12_arg[] = {
+  { XtNfromVert,  (XtArgVal) NULL },        /* must be first */
+  { "label",      (XtArgVal)" Mark all delivered" } };
+
+static Arg item_13_arg[] = {
+  { XtNfromVert,  (XtArgVal) NULL },        /* must be first */
+  { "label",      (XtArgVal)" Edit sender" } };
+
+static Arg item_99_arg[] = {
+  { XtNfromVert,  (XtArgVal) NULL },        /* must be first */
+  { "label",      (XtArgVal)" " } };
+
+
+
+/*************************************************
+*        Destroy the menu when popped down       *
+*************************************************/
+
+static void popdownAction(Widget w, XtPointer client_data, XtPointer call_data)
+{
+client_data = client_data;    /* Keep picky compilers happy */
+call_data = call_data;
+if (highlighted_x >= 0)
+  XawTextSinkDisplayText(queue_text_sink,
+    highlighted_x, highlighted_y,
+    highlighted_start, highlighted_end, 0);
+XtDestroyWidget(w);
+menu_is_up = FALSE;
+}
+
+
+
+/*************************************************
+*          Display the message log               *
+*************************************************/
+
+static void msglogAction(Widget w, XtPointer client_data, XtPointer call_data)
+{
+int i;
+uschar buffer[256];
+Widget text = text_create((uschar *)client_data, text_depth);
+FILE *f = NULL;
+
+w = w;      /* Keep picky compilers happy */
+call_data = call_data;
+
+/* End up with the split version, so message looks right when non-exist */
+
+for (i = 0; i < (spool_is_split? 2:1); i++)
+  {
+  message_subdir[0] = (i != 0)? ((uschar *)client_data)[5] : 0;
+  sprintf(CS buffer, "%s/msglog/%s/%s", spool_directory, message_subdir,
+    (uschar *)client_data);
+  f = fopen(CS buffer, "r");
+  if (f != NULL) break;
+  }
+
+if (f == NULL)
+  text_showf(text, "%s: %s\n", buffer, strerror(errno));
+else
+  {
+  while (Ufgets(buffer, 256, f) != NULL) text_show(text, buffer);
+  fclose(f);
+  }
+}
+
+
+
+/*************************************************
+*          Display the message body               *
+*************************************************/
+
+static void bodyAction(Widget w, XtPointer client_data, XtPointer call_data)
+{
+int i;
+uschar buffer[256];
+Widget text = text_create((uschar *)client_data, text_depth);
+FILE *f = NULL;
+
+w = w;      /* Keep picky compilers happy */
+call_data = call_data;
+
+for (i = 0; i < (spool_is_split? 2:1); i++)
+  {
+  message_subdir[0] = (i != 0)? ((uschar *)client_data)[5] : 0;
+  sprintf(CS buffer, "%s/input/%s/%s-D", spool_directory, message_subdir,
+    (uschar *)client_data);
+  f = fopen(CS buffer, "r");
+  if (f != NULL) break;
+  }
+
+if (f == NULL)
+  text_showf(text, "Failed to open file: %s\n", strerror(errno));
+else
+  {
+  int count = 0;
+  while (Ufgets(buffer, 256, f) != NULL)
+    {
+    text_show(text, buffer);
+    count += Ustrlen(buffer);
+    if (count > body_max)
+      {
+      text_show(text, US"\n*** Message length exceeds BODY_MAX ***\n");
+      break;
+      }
+    }
+  fclose(f);
+  }
+}
+
+
+
+/*************************************************
+*        Do something to a message               *
+*************************************************/
+
+/* The output is not shown in a window for non-delivery actions that succeed,
+unless action_output is set. We can't, however, tell until we have run
+the command whether we want the output or not, so the pipe has to be set up in
+all cases. */
+
+static void ActOnMessage(uschar *id, uschar *action, uschar *address_arg)
+{
+int pid;
+int pipe_fd[2];
+int delivery = Ustrcmp(action + Ustrlen(action) - 2, "-M") == 0;
+uschar *quote = US"";
+uschar *at = US"";
+uschar *qualify = US"";
+uschar buffer[256];
+queue_item *qq;
+Widget text = NULL;
+
+/* If the address arg is not empty and does not contain @ and there is a
+qualify domain, qualify it. (But don't qualify '<>'.)*/
+
+if (address_arg[0] != 0)
+  {
+  quote = US"\'";
+  if (Ustrchr(address_arg, '@') == NULL &&
+      Ustrcmp(address_arg, "<>") != 0 &&
+      qualify_domain != NULL &&
+      qualify_domain[0] != 0)
+    {
+    at = US"@";
+    qualify = qualify_domain;
+    }
+  }
+sprintf(CS buffer, "%s %s %s %s %s %s%s%s%s%s", exim_path,
+  (alternate_config == NULL)? US"" : US"-C",
+  (alternate_config == NULL)? US"" : alternate_config,
+  action, id, quote, address_arg, at, qualify, quote);
+
+/* If we know we are going to need the window, create it now. */
+
+if (action_output || delivery)
+  {
+  text = text_create(id, text_depth);
+  text_showf(text, "%s\n", buffer);
+  }
+
+/* Create the pipe for output. Remember, on most systems pipe[0] is
+for reading and pipe[1] is for writing! Solaris, with its two-way
+pipes is a trap! */
+
+if (pipe(pipe_fd) != 0)
+  {
+  if (text == NULL)
+    {
+    text = text_create(id, text_depth);
+    text_showf(text, "%s\n", buffer);
+    }
+  text_show(text, US"*** Failed to create pipe ***\n");
+  return;
+  }
+
+fcntl(pipe_fd[0], F_SETFL, O_NONBLOCK);
+fcntl(pipe_fd[1], F_SETFL, O_NONBLOCK);
+
+/* Delivering a message can take some time, and we want to show the
+output as it goes along. This requires subprocesses and is coded below. For
+other commands, we can assume an immediate response, and so need not waste
+resources with subprocesses. If action_output is FALSE, don't show the
+output at all. */
+
+if (!delivery)
+  {
+  int count, rc;
+  int save_stdout = dup(1);
+  int save_stderr = dup(2);
+
+  close(1);
+  close(2);
+
+  dup2(pipe_fd[1], 1);
+  dup2(pipe_fd[1], 2);
+  close(pipe_fd[1]);
+
+  rc = system(CS buffer);
+
+  close(1);
+  close(2);
+
+  if (action_output || rc != 0)
+    {
+    if (text == NULL)
+      {
+      text = text_create(id, text_depth);
+      text_showf(text, "%s\n", buffer);
+      }
+    while ((count = read(pipe_fd[0], buffer, 254)) > 0)
+      {
+      buffer[count] = 0;
+      text_show(text, buffer);
+      }
+    }
+
+  close(pipe_fd[0]);
+
+  dup2(save_stdout, 1);
+  dup2(save_stderr, 2);
+  close(save_stdout);
+  close(save_stderr);
+
+  /* If action was to change the sender, and it succeeded, we have to
+  update the in-store data. */
+
+  if (rc == 0 && Ustrcmp(action + Ustrlen(action) - 4, "-Mes") == 0)
+    {
+    queue_item *q = find_queue(id, queue_noop, 0);
+    if (q != NULL)
+      {
+      if (q->sender != NULL) store_free(q->sender);
+      q->sender = store_malloc(Ustrlen(address_arg) + 1);
+      Ustrcpy(q->sender, address_arg);
+      }
+    }
+
+  /* If configured, cause a display update and return */
+
+  if (action_queue_update) tick_queue_accumulator = 999999;
+  return;
+  }
+
+/* Message is to be delivered. Ensure that it is marked unfrozen,
+because nothing will get written to the log to show that this has
+happened. (Other freezing/unfreezings get logged and picked up from
+there.) */
+
+qq = find_queue(id, queue_noop, 0);
+if (qq != NULL) qq->frozen = FALSE;
+
+/* New, asynchronous code runs in a subprocess for commands that
+will take some time. The main process does not wait. There is a
+SIGCHLD handler in the main program that cleans up any terminating
+sub processes. */
+
+if ((pid = fork()) == 0)
+  {
+  close(1);
+  close(2);
+
+  dup2(pipe_fd[1], 1);
+  dup2(pipe_fd[1], 2);
+  close(pipe_fd[1]);
+
+  system(CS buffer);
+
+  close(1);
+  close(2);
+  close(pipe_fd[0]);
+  _exit(0);
+  }
+
+/* Main process - set up an item for the main ticker to watch. */
+
+if (pid < 0) text_showf(text, "Failed to fork: %s\n", strerror(pid)); else
+  {
+  pipe_item *p = (pipe_item *)store_malloc(sizeof(pipe_item));
+
+  if (p == NULL)
+    {
+    text_show(text, US"Run out of store\n");
+    return;
+    }
+
+  p->widget = text;
+  p->fd = pipe_fd[0];
+
+  p->next = pipe_chain;
+  pipe_chain = p;
+
+  close(pipe_fd[1]);
+  }
+}
+
+
+
+
+/*************************************************
+*        Cause a message to be delivered         *
+*************************************************/
+
+static void deliverAction(Widget w, XtPointer client_data, XtPointer call_data)
+{
+w = w;      /* Keep picky compilers happy */
+call_data = call_data;
+ActOnMessage((uschar *)client_data, US"-v -M", US"");
+}
+
+
+
+/*************************************************
+*        Cause a message to be Frozen            *
+*************************************************/
+
+static void freezeAction(Widget w, XtPointer client_data, XtPointer call_data)
+{
+w = w;      /* Keep picky compilers happy */
+call_data = call_data;
+ActOnMessage((uschar *)client_data, US"-Mf", US"");
+}
+
+
+
+/*************************************************
+*        Cause a message to be thawed            *
+*************************************************/
+
+static void thawAction(Widget w, XtPointer client_data, XtPointer call_data)
+{
+w = w;      /* Keep picky compilers happy */
+call_data = call_data;
+ActOnMessage((uschar *)client_data, US"-Mt", US"");
+}
+
+
+
+/*************************************************
+*          Take action using dialog data         *
+*************************************************/
+
+/* This function is called after a dialog box has been filled
+in. It is global because it is set up in the action table at
+start-up time. If the string is empty, do nothing. */
+
+XtActionProc dialogAction(Widget w, XEvent *event, String *ss, Cardinal *c)
+{
+uschar *s = US XawDialogGetValueString(dialog_widget);
+
+w = w;      /* Keep picky compilers happy */
+event = event;
+ss = ss;
+c = c;
+
+XtPopdown((Widget)dialog_shell);
+XtDestroyWidget((Widget)dialog_shell);
+while (isspace(*s)) s++;
+if (s[0] != 0)
+  {
+  if (actioned_message[0] != 0)
+    ActOnMessage(actioned_message, action_required, s);
+  else
+    NonMessageDialogue(s);    /* When called from somewhere else */
+  }
+return NULL;
+}
+
+
+
+/*************************************************
+*              Create a dialog box               *
+*************************************************/
+
+/* The focus is grabbed exclusively, so nothing else can
+be done to the application until the box is filled in. This
+function is also used by the Hide button handler. */
+
+void create_dialog(uschar *label, uschar *value)
+{
+Arg warg[4];
+Dimension x, y, xx, yy;
+XtTranslations pop_trans;
+Widget text;
+
+/* Get the position of a reference widget so the dialog box can be put
+near to it. */
+
+get_pos_args[0].value = (XtArgVal)(&x);
+get_pos_args[1].value = (XtArgVal)(&y);
+XtGetValues(dialog_ref_widget, get_pos_args, 2);
+
+/* When this is not a message_specific thing, the position of the reference
+widget is relative to the window. Get the position of the top level widget and
+add to the position. */
+
+if (dialog_ref_widget != menushell)
+  {
+  get_pos_args[0].value = (XtArgVal)(&xx);
+  get_pos_args[1].value = (XtArgVal)(&yy);
+  XtGetValues(toplevel_widget, get_pos_args, 2);
+  x += xx;
+  y += yy;
+  }
+
+/* Create a transient shell for the dialog box. */
+
+XtSetArg(warg[0], XtNtransientFor, queue_widget);
+XtSetArg(warg[1], XtNx, x + 50);
+XtSetArg(warg[2], XtNy, y + 50);
+XtSetArg(warg[3], XtNallowShellResize, True);
+dialog_shell = XtCreatePopupShell("forDialog", transientShellWidgetClass,
+   toplevel_widget, warg, 4);
+
+/* Create the dialog box. */
+
+dialog_arg[0].value = (XtArgVal)label;
+dialog_arg[1].value = (XtArgVal)value;
+dialog_widget = XtCreateManagedWidget("dialog", dialogWidgetClass, dialog_shell,
+  dialog_arg, XtNumber(dialog_arg));
+
+/* Get the text widget from within the dialog box, give it the keyboard focus,
+make it wider than the default, and override its translations to make Return
+call the dialog action function. */
+
+text = XtNameToWidget(dialog_widget, "value");
+XawTextSetInsertionPoint(text, Ustrlen(value));
+XtSetKeyboardFocus(dialog_widget, text);
+xs_SetValues(text, 1, "width", 200);
+pop_trans = XtParseTranslationTable(
+  "<Key>Return:         dialogAction()\n");
+XtOverrideTranslations(text, pop_trans);
+
+/* Pop the thing up. */
+
+XtPopup(dialog_shell, XtGrabExclusive);
+XFlush(X_display);
+}
+
+
+
+
+
+/*************************************************
+*        Cause a recipient to be added           *
+*************************************************/
+
+/* This just sets up the dialog box; the action happens when it has been filled
+in. */
+
+static void addrecipAction(Widget w, XtPointer client_data, XtPointer call_data)
+{
+w = w;      /* Keep picky compilers happy */
+call_data = call_data;
+Ustrcpy(actioned_message, (uschar *)client_data);
+action_required = US"-Mar";
+dialog_ref_widget = menushell;
+create_dialog(US"Recipient address to add?", US"");
+}
+
+
+
+/*************************************************
+*    Cause an address to be marked delivered     *
+*************************************************/
+
+static void markdelAction(Widget w, XtPointer client_data, XtPointer call_data)
+{
+w = w;      /* Keep picky compilers happy */
+call_data = call_data;
+Ustrcpy(actioned_message, (uschar *)client_data);
+action_required = US"-Mmd";
+dialog_ref_widget = menushell;
+create_dialog(US"Recipient address to mark delivered?", US"");
+}
+
+
+/*************************************************
+*   Cause all addresses to be marked delivered   *
+*************************************************/
+
+static void markalldelAction(Widget w, XtPointer client_data, XtPointer call_data)
+{
+w = w;      /* Keep picky compilers happy */
+call_data = call_data;
+ActOnMessage((uschar *)client_data, US"-Mmad", US"");
+}
+
+
+/*************************************************
+*        Edit the message's sender               *
+*************************************************/
+
+static void editsenderAction(Widget w, XtPointer client_data,
+  XtPointer call_data)
+{
+queue_item *q;
+uschar *sender;
+w = w;      /* Keep picky compilers happy */
+call_data = call_data;
+Ustrcpy(actioned_message, (uschar *)client_data);
+q = find_queue(actioned_message, queue_noop, 0);
+sender = (q == NULL)? US"" : (q->sender[0] == 0)? US"<>" : q->sender;
+action_required = US"-Mes";
+dialog_ref_widget = menushell;
+create_dialog(US"New sender address?", sender);
+}
+
+
+/*************************************************
+*    Cause a message to be returned to sender    *
+*************************************************/
+
+static void giveupAction(Widget w, XtPointer client_data, XtPointer call_data)
+{
+w = w;      /* Keep picky compilers happy */
+call_data = call_data;
+ActOnMessage((uschar *)client_data, US"-v -Mg", US"");
+}
+
+
+
+/*************************************************
+*      Cause a message to be cancelled           *
+*************************************************/
+
+static void removeAction(Widget w, XtPointer client_data, XtPointer call_data)
+{
+w = w;      /* Keep picky compilers happy */
+call_data = call_data;
+ActOnMessage((uschar *)client_data, US"-Mrm", US"");
+}
+
+
+
+/*************************************************
+*             Display a message's headers        *
+*************************************************/
+
+static void headersAction(Widget w, XtPointer client_data, XtPointer call_data)
+{
+uschar buffer[256];
+header_line *h, *next;
+Widget text = text_create((uschar *)client_data, text_depth);
+void *reset_point;
+
+w = w;      /* Keep picky compilers happy */
+call_data = call_data;
+
+/* Remember the point in the dynamic store so we can recover to it afterwards.
+Then use Exim's function to read the header. */
+
+reset_point = store_get(0);
+
+sprintf(CS buffer, "%s-H", (uschar *)client_data);
+if (spool_read_header(buffer, TRUE, FALSE) != spool_read_OK)
+  {
+  if (errno == ERRNO_SPOOLFORMAT)
+    {
+    struct stat statbuf;
+    sprintf(CS big_buffer, "%s/input/%s", spool_directory, buffer);
+    if (Ustat(big_buffer, &statbuf) == 0)
+      text_showf(text, "Format error in spool file %s: size=%d\n", buffer,
+        statbuf.st_size);
+    else text_showf(text, "Format error in spool file %s\n", buffer);
+    }
+  else text_showf(text, "Read error for spool file %s\n", buffer);
+  store_reset(reset_point);
+  return;
+  }
+
+if (sender_address != NULL)
+  {
+  text_showf(text, "%s sender: <%s>\n", sender_local? "Local" : "Remote",
+    sender_address);
+  }
+
+if (recipients_list != NULL)
+  {
+  int i;
+  text_show(text, US"Recipients:\n");
+  for (i = 0; i < recipients_count; i++)
+    {
+    text_showf(text, "  %s %s\n",
+      (tree_search(tree_nonrecipients, recipients_list[i].address) == NULL)?
+        " ":"*", recipients_list[i].address);
+    }
+  text_show(text, US"\n");
+  }
+
+for (h = header_list; h != NULL; h = next)
+  {
+  next = h->next;
+  text_showf(text, "%c ", h->type);   /* Don't push h->text through a %s */
+  text_show(text, h->text);           /* expansion as it may be v large */
+  }
+
+store_reset(reset_point);
+}
+
+
+
+
+/*************************************************
+*              Dismiss a text window             *
+*************************************************/
+
+static void dismissAction(Widget w, XtPointer client_data, XtPointer call_data)
+{
+pipe_item *p = pipe_chain;
+
+w = w;      /* Keep picky compilers happy */
+call_data = call_data;
+
+XtPopdown((Widget)client_data);
+XtDestroyWidget((Widget)client_data);
+
+/* If this is a text widget for a sub-process, clear it out of
+the chain so that subsequent data doesn't try to use it. We have
+to search the parents of the saved widget to see if one of them
+is what we have just destroyed. */
+
+while (p != NULL)
+  {
+  Widget pp = p->widget;
+  while (pp != NULL)
+    {
+    if (pp == (Widget)client_data) { p->widget = NULL; return; }
+    pp = XtParent(pp);
+    }
+  p = p->next;
+  }
+}
+
+
+
+/*************************************************
+*             Set up popup text window           *
+*************************************************/
+
+static Widget text_create(uschar *name, int height)
+{
+Widget textshell, form, text, button;
+
+/* Create a popup shell widget to display as an additional
+toplevel window. */
+
+textshell = XtCreatePopupShell("textshell", topLevelShellWidgetClass,
+  toplevel_widget, NULL, 0);
+xs_SetValues(textshell, 4,
+  "title",     name,
+  "iconName",  name,
+  "minWidth",  100,
+  "minHeight", 100);
+
+/* Create a form widget, containing the text widget and the
+dismiss button widget. */
+
+form = XtCreateManagedWidget("textform", formWidgetClass,
+  textshell, NULL, 0);
+xs_SetValues(form, 1, "defaultDistance", 8);
+
+text = XtCreateManagedWidget("texttext", asciiTextWidgetClass,
+  form, text_arg, XtNumber(text_arg));
+xs_SetValues(text, 4,
+  "editType",        XawtextAppend,
+  "width",           700,
+  "height",          height,
+  "translations",    text_trans);
+XawTextDisplayCaret(text, TRUE);
+
+/* Use the same font as for the queue display */
+
+if (queue_font != NULL)
+  {
+  XFontStruct *f = XLoadQueryFont(X_display, CS queue_font);
+  if (f != NULL) xs_SetValues(text, 1, "font", f);
+  }
+
+button_arg[0].value = (XtArgVal)text;
+button = XtCreateManagedWidget("dismiss", commandWidgetClass,
+  form, button_arg, XtNumber(button_arg));
+XtAddCallback(button, "callback",  dismissAction, (XtPointer)textshell);
+
+/* Get the toplevel popup displayed, and yield the text widget so
+that text can be put into it. */
+
+XtPopup(textshell, XtGrabNone);
+return text;
+}
+
+
+
+
+/*************************************************
+*            Set up menu in queue window         *
+*************************************************/
+
+/* We have added an action table that causes this function to
+be called, and set up button 2 in the text widgets to call it. */
+
+void menu_create(Widget w, XEvent *event, String *actargs, Cardinal *count)
+{
+int line;
+int i;
+uschar *s;
+XawTextPosition p;
+Widget src, menu_line, item_1, item_2, item_3, item_4,
+  item_5, item_6, item_7, item_8, item_9, item_10, item_11,
+  item_12, item_13;
+XtTranslations menu_trans = XtParseTranslationTable(
+  "<EnterWindow>:   highlight()\n\
+   <LeaveWindow>:   unhighlight()\n\
+   <BtnMotion>:     highlight()\n\
+   <BtnUp>:         MenuPopdown()notify()unhighlight()\n\
+  ");
+
+actargs = actargs;   /* Keep picky compilers happy */
+count = count;
+
+/* Get the sink and source and the current text pointer */
+
+queue_get_arg[0].value = (XtArgVal)(&queue_text_sink);
+queue_get_arg[1].value = (XtArgVal)(&src);
+queue_get_arg[2].value = (XtArgVal)(&s);
+XtGetValues(w, queue_get_arg, 3);
+
+/* Find the line number of the pointer in the window, and the
+character offset of the top lefthand of the window. */
+
+line = (event->xbutton).y / XawTextSinkMaxHeight(queue_text_sink, 1);
+p = XawTextTopPosition(w);
+
+/* Find the start of the line on which the button was clicked. */
+
+i = line;
+while (i-- > 0)
+  {
+  while (s[p] != 0 && s[p++] != '\n');
+  }
+
+/* Now pointing either at 0 or 1st uschar after \n, or very 1st uschar.
+If 0, the click was beyond the end of the data; just set up a dummy
+menu. (Not easy to ignore as several actions are specified for the
+mouse click and it expects this one to set up a menu.) If on a
+continuation line, move back to the main line. */
+
+if (s[p] == 0)
+  {
+  menushell_arg[0].value = (XtArgVal)"No message selected";
+  menushell = XtCreatePopupShell("menu", simpleMenuWidgetClass,
+    queue_widget, menushell_arg, XtNumber(menushell_arg));
+  XtAddCallback(menushell, "popdownCallback", popdownAction, NULL);
+  xs_SetValues(menushell, 2,
+    "cursor",       XCreateFontCursor(X_display, XC_arrow),
+    "translations", menu_trans);
+
+  /* To keep the widgets in XFree86 happy, we have to create at least one menu
+  item, it seems. (Openwindows doesn't mind a menu with no items.) Otherwise
+  there's a complaint about a zero width menu, and a crash. */
+
+  menu_line = XtCreateManagedWidget("line", smeLineObjectClass, menushell,
+    NULL, 0);
+
+  item_99_arg[0].value = (XtArgVal)menu_line;
+  (void)XtCreateManagedWidget("item99", smeBSBObjectClass, menushell,
+    item_99_arg, XtNumber(item_99_arg));
+
+  highlighted_x = -1;
+  return;
+  }
+
+while (p > 0 && s[p+11] == ' ')
+  {
+  line--;
+  p--;
+  while (p > 0 && s[p-1] != '\n') p--;
+  }
+
+/* Now pointing at first character of a main line. */
+
+Ustrncpy(message_id, s+p+11, MESSAGE_ID_LENGTH);
+message_id[MESSAGE_ID_LENGTH] = 0;
+
+/* Highlight the line being menued, and save its parameters so that it
+can be de-highlighted at popdown. */
+
+highlighted_start = highlighted_end = p;
+while (s[highlighted_end] != '\n') highlighted_end++;
+highlighted_x = 17;
+highlighted_y = line * XawTextSinkMaxHeight(queue_text_sink, 1) + 2;
+
+XawTextSinkDisplayText(queue_text_sink,
+  highlighted_x, highlighted_y,
+  highlighted_start, highlighted_end, 1);
+
+/* Create the popup shell and the other widgets that comprise the menu.
+Set the translations and pointer shape, and add the callback pointers. */
+
+menushell_arg[0].value = (XtArgVal)message_id;
+menushell = XtCreatePopupShell("menu", simpleMenuWidgetClass,
+  queue_widget, menushell_arg, XtNumber(menushell_arg));
+XtAddCallback(menushell, "popdownCallback", popdownAction, NULL);
+
+xs_SetValues(menushell, 2,
+  "cursor",       XCreateFontCursor(X_display, XC_arrow),
+  "translations", menu_trans);
+
+menu_line = XtCreateManagedWidget("line", smeLineObjectClass, menushell,
+  NULL, 0);
+
+item_1_arg[0].value = (XtArgVal)menu_line;
+item_1 = XtCreateManagedWidget("item1", smeBSBObjectClass, menushell,
+  item_1_arg, XtNumber(item_1_arg));
+XtAddCallback(item_1, "callback",  msglogAction, (XtPointer)message_id);
+
+item_2_arg[0].value = (XtArgVal)item_1;
+item_2 = XtCreateManagedWidget("item2", smeBSBObjectClass, menushell,
+  item_2_arg, XtNumber(item_2_arg));
+XtAddCallback(item_2, "callback",  headersAction, (XtPointer)message_id);
+
+item_3_arg[0].value = (XtArgVal)item_2;
+item_3 = XtCreateManagedWidget("item3", smeBSBObjectClass, menushell,
+  item_3_arg, XtNumber(item_3_arg));
+XtAddCallback(item_3, "callback",  bodyAction, (XtPointer)message_id);
+
+item_4_arg[0].value = (XtArgVal)item_3;
+item_4 = XtCreateManagedWidget("item4", smeBSBObjectClass, menushell,
+  item_4_arg, XtNumber(item_4_arg));
+XtAddCallback(item_4, "callback",  deliverAction, (XtPointer)message_id);
+
+item_5_arg[0].value = (XtArgVal)item_4;
+item_5 = XtCreateManagedWidget("item5", smeBSBObjectClass, menushell,
+  item_5_arg, XtNumber(item_5_arg));
+XtAddCallback(item_5, "callback",  freezeAction, (XtPointer)message_id);
+
+item_6_arg[0].value = (XtArgVal)item_5;
+item_6 = XtCreateManagedWidget("item6", smeBSBObjectClass, menushell,
+  item_6_arg, XtNumber(item_6_arg));
+XtAddCallback(item_6, "callback",  thawAction, (XtPointer)message_id);
+
+item_7_arg[0].value = (XtArgVal)item_6;
+item_7 = XtCreateManagedWidget("item7", smeBSBObjectClass, menushell,
+  item_7_arg, XtNumber(item_7_arg));
+XtAddCallback(item_7, "callback",  giveupAction, (XtPointer)message_id);
+
+item_8_arg[0].value = (XtArgVal)item_7;
+item_8 = XtCreateManagedWidget("item8", smeBSBObjectClass, menushell,
+  item_8_arg, XtNumber(item_8_arg));
+XtAddCallback(item_8, "callback",  removeAction, (XtPointer)message_id);
+
+item_9_arg[0].value = (XtArgVal)item_8;
+item_9 = XtCreateManagedWidget("item9", smeBSBObjectClass, menushell,
+  item_9_arg, XtNumber(item_9_arg));
+
+item_10_arg[0].value = (XtArgVal)item_9;
+item_10 = XtCreateManagedWidget("item10", smeBSBObjectClass, menushell,
+  item_10_arg, XtNumber(item_10_arg));
+XtAddCallback(item_10, "callback",  addrecipAction, (XtPointer)message_id);
+
+item_11_arg[0].value = (XtArgVal)item_10;
+item_11 = XtCreateManagedWidget("item11", smeBSBObjectClass, menushell,
+  item_11_arg, XtNumber(item_11_arg));
+XtAddCallback(item_11, "callback",  markdelAction, (XtPointer)message_id);
+
+item_12_arg[0].value = (XtArgVal)item_11;
+item_12 = XtCreateManagedWidget("item12", smeBSBObjectClass, menushell,
+  item_12_arg, XtNumber(item_12_arg));
+XtAddCallback(item_12, "callback",  markalldelAction, (XtPointer)message_id);
+
+item_13_arg[0].value = (XtArgVal)item_12;
+item_13 = XtCreateManagedWidget("item13", smeBSBObjectClass, menushell,
+  item_13_arg, XtNumber(item_13_arg));
+XtAddCallback(item_13, "callback",  editsenderAction, (XtPointer)message_id);
+
+/* Arrange that the menu pops up with the first item selected. */
+
+xs_SetValues(menushell, 1, "popupOnEntry", item_1);
+
+/* Flag that the menu is up to suppress queue updates. */
+
+menu_is_up = TRUE;
+}
+
+/* End of em_menu.c */
diff --git a/exim_monitor/em_queue.c b/exim_monitor/em_queue.c
new file mode 100644 (file)
index 0000000..c01a80f
--- /dev/null
@@ -0,0 +1,834 @@
+/*************************************************
+*                 Exim Monitor                   *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+
+#include "em_hdr.h"
+
+
+/* This module contains functions to do with scanning exim's
+queue and displaying the data therefrom. */
+
+
+/* If we are anonymizing for screen shots, define a function to anonymize
+addresses. Otherwise, define a macro that does nothing. */
+
+#ifdef ANONYMIZE
+static uschar *anon(uschar *s)
+{
+static uschar anon_result[256];
+uschar *ss = anon_result;
+for (; *s != 0; s++) *ss++ = (*s == '@' || *s == '.')? *s : 'x';
+*ss = 0;
+return anon_result;
+}
+#else
+#define anon(x) x
+#endif
+
+
+/*************************************************
+*                 Static variables               *
+*************************************************/
+
+static int queue_total = 0;   /* number of items in queue */
+
+/* Table for turning base-62 numbers into binary */
+
+static uschar tab62[] =
+          {0,1,2,3,4,5,6,7,8,9,0,0,0,0,0,0,     /* 0-9 */
+           0,10,11,12,13,14,15,16,17,18,19,20,  /* A-K */
+          21,22,23,24,25,26,27,28,29,30,31,32,  /* L-W */
+          33,34,35, 0, 0, 0, 0, 0,              /* X-Z */
+           0,36,37,38,39,40,41,42,43,44,45,46,  /* a-k */
+          47,48,49,50,51,52,53,54,55,56,57,58,  /* l-w */
+          59,60,61};                            /* x-z */
+
+/* Index for quickly finding things in the ordered queue. */
+
+static queue_item *queue_index[queue_index_size];
+
+
+
+/*************************************************
+*         Find/Create/Delete a destination       *
+*************************************************/
+
+/* If the action is dest_noop, then just return item or NULL;
+if it is dest_add, then add if not present, and return item;
+if it is dest_remove, remove if present and return NULL. The
+address is lowercased to start with, unless it begins with
+"*", which it does for error messages. */
+
+dest_item *find_dest(queue_item *q, uschar *name, int action, BOOL caseless)
+{
+dest_item *dd;
+dest_item **d = &(q->destinations);
+
+while (*d != NULL)
+  {
+  if ((caseless? strcmpic(name,(*d)->address) : Ustrcmp(name,(*d)->address))
+        == 0)
+    {
+    dest_item *ddd;
+
+    if (action != dest_remove) return *d;
+    dd = *d;
+    *d = dd->next;
+    store_free(dd);
+
+    /* Unset any parent pointers that were to this address */
+
+    for (ddd = q->destinations; ddd != NULL; ddd = ddd->next)
+      {
+      if (ddd->parent == dd) ddd->parent = NULL;
+      }
+
+    return NULL;
+    }
+  d = &((*d)->next);
+  }
+
+if (action != dest_add) return NULL;
+
+dd = (dest_item *)store_malloc(sizeof(dest_item) + Ustrlen(name));
+Ustrcpy(dd->address, name);
+dd->next = NULL;
+dd->parent = NULL;
+*d = dd;
+return dd;
+}
+
+
+
+/*************************************************
+*            Clean up a dead queue item          *
+*************************************************/
+
+static void clean_up(queue_item *p)
+{
+dest_item *dd = p->destinations;
+while (dd != NULL)
+  {
+  dest_item *next = dd->next;
+  store_free(dd);
+  dd = next;
+  }
+if (p->sender != NULL) store_free(p->sender);
+store_free(p);
+}
+
+
+/*************************************************
+*         Set up an ACL variable                 *
+*************************************************/
+
+/* The spool_read_header() function calls acl_var_create() when it reads in an
+ACL variable. We know that in this case, the variable will be new, not re-used,
+so this is a cut-down version, to save including the whole acl.c module (which
+would need conditional compilation to cut most of it out). */
+
+tree_node *
+acl_var_create(uschar *name)
+{
+tree_node *node, **root;
+root = (name[0] == 'c')? &acl_var_c : &acl_var_m;
+node = store_get(sizeof(tree_node) + Ustrlen(name));
+Ustrcpy(node->name, name);
+node->data.ptr = NULL;
+(void)tree_insertnode(root, node);
+return node;
+}
+
+
+
+/*************************************************
+*             Set up new queue item              *
+*************************************************/
+
+static queue_item *set_up(uschar *name, int dir_char)
+{
+int i, rc, save_errno;
+struct stat statdata;
+void *reset_point;
+uschar *p;
+queue_item *q = (queue_item *)store_malloc(sizeof(queue_item));
+uschar buffer[256];
+
+/* Initialize the block */
+
+q->next = q->prev = NULL;
+q->destinations = NULL;
+Ustrcpy(q->name, name);
+q->seen = TRUE;
+q->frozen = FALSE;
+q->dir_char = dir_char;
+q->sender = NULL;
+q->size = 0;
+
+/* Read the header file from the spool; if there is a failure it might mean
+inaccessibility as a result of protections. A successful read will have caused
+sender_address to get set and the recipients fields to be initialized. If
+there's a format error in the headers, we can still display info from the
+envelope.
+
+Before reading the header remember the position in the dynamic store so that
+we can recover the store into which the header is read. All data read by
+spool_read_header that is to be preserved is copied into malloc store. */
+
+reset_point = store_get(0);
+message_size = 0;
+message_subdir[0] = dir_char;
+sprintf(CS buffer, "%s-H", name);
+rc =  spool_read_header(buffer, FALSE, TRUE);
+save_errno = errno;
+
+/* If we failed to read the envelope, compute the input time by
+interpreting the id as a base-62 number. */
+
+if (rc != spool_read_OK && rc != spool_read_hdrerror)
+  {
+  int t = 0;
+  for (i = 0; i < 6; i++) t = t * 62 + tab62[name[i] - '0'];
+  q->update_time = q->input_time = t;
+  }
+
+/* Envelope read; get input time and remove qualify_domain from sender address,
+if it's there. */
+
+else
+  {
+  q->update_time = q->input_time = received_time;
+  if ((p = strstric(sender_address+1, qualify_domain, FALSE)) != NULL &&
+    *(--p) == '@') *p = 0;
+  }
+
+/* If we didn't read the whole header successfully, generate an error
+message. If the envelope was read, this appears as a first recipient;
+otherwise it sets set up in the sender field. */
+
+if (rc != spool_read_OK)
+  {
+  uschar *msg;
+
+  if (save_errno == ERRNO_SPOOLFORMAT)
+    {
+    struct stat statbuf;
+    sprintf(CS big_buffer, "%s/input/%s", spool_directory, buffer);
+    if (Ustat(big_buffer, &statbuf) == 0)
+      msg = string_sprintf("*** Format error in spool file: size = %d ***",
+        statbuf.st_size);
+    else msg = string_sprintf("*** Format error in spool file ***");
+    }
+  else msg = string_sprintf("*** Cannot read spool file ***");
+
+  if (rc == spool_read_hdrerror)
+    {
+    (void)find_dest(q, msg, dest_add, FALSE);
+    }
+  else
+    {
+    deliver_freeze = FALSE;
+    sender_address = msg;
+    recipients_count = 0;
+    }
+  }
+
+/* Now set up the remaining data. */
+
+q->frozen = deliver_freeze;
+
+if (sender_set_untrusted)
+  {
+  if (sender_address[0] == 0)
+    {
+    q->sender = store_malloc(Ustrlen(originator_login) + 6);
+    sprintf(CS q->sender, "<> (%s)", originator_login);
+    }
+  else
+    {
+    q->sender = store_malloc(Ustrlen(sender_address) +
+      Ustrlen(originator_login) + 4);
+    sprintf(CS q->sender, "%s (%s)", sender_address, originator_login);
+    }
+  }
+else
+  {
+  q->sender = store_malloc(Ustrlen(sender_address) + 1);
+  Ustrcpy(q->sender, sender_address);
+  }
+
+sender_address = NULL;
+
+sprintf(CS buffer, "%s/input/%s/%s-D", spool_directory, message_subdir, name);
+if (Ustat(buffer, &statdata) == 0)
+  q->size = message_size + statdata.st_size - SPOOL_DATA_START_OFFSET + 1;
+
+/* Scan and process the recipients list, skipping any that have already
+been delivered, and removing visible names. */
+
+if (recipients_list != NULL)
+  {
+  for (i = 0; i < recipients_count; i++)
+    {
+    uschar *r = recipients_list[i].address;
+    if (tree_search(tree_nonrecipients, r) == NULL)
+      {
+      if ((p = strstric(r+1, qualify_domain, FALSE)) != NULL &&
+        *(--p) == '@') *p = 0;
+      (void)find_dest(q, r, dest_add, FALSE);
+      }
+    }
+  }
+
+/* Recover the dynamic store used by spool_read_header(). */
+
+store_reset(reset_point);
+return q;
+}
+
+
+
+/*************************************************
+*             Find/Create a queue item           *
+*************************************************/
+
+/* The queue is kept as a doubly-linked list, sorted by name. However,
+to speed up searches, an index into the list is used. This is maintained
+by the scan_spool_input function when it goes down the list throwing
+out entries that are no longer needed. When the action is "add" and
+we don't need to add, mark the found item as seen. */
+
+
+#ifdef never
+static void debug_queue(void)
+{
+int i;
+int count = 0;
+queue_item *p;
+printf("\nqueue_total=%d\n", queue_total);
+
+for (i = 0; i < queue_index_size; i++)
+  printf("index %d = %d %s\n", i, (int)(queue_index[i]),
+    (queue_index[i])->name);
+
+printf("Queue is:\n");
+p = queue_index[0];
+while (p != NULL)
+  {
+  count++;
+  for (i = 0; i < queue_index_size; i++)
+    {
+    if (queue_index[i] == p) printf("count=%d index=%d\n", count, (int)p);
+    }
+  printf("%d %d %d %s\n", (int)p, (int)p->next, (int)p->prev, p->name);
+  p = p->next;
+  }
+}
+#endif
+
+
+
+queue_item *find_queue(uschar *name, int action, int dir_char)
+{
+int first = 0;
+int last = queue_index_size - 1;
+int middle = (first + last)/2;
+queue_item *p, *q, *qq;
+
+/* Handle the empty queue as a special case. */
+
+if (queue_total == 0)
+  {
+  if (action != queue_add) return NULL;
+  if ((qq = set_up(name, dir_char)) != NULL)
+    {
+    int i;
+    for (i = 0; i < queue_index_size; i++) queue_index[i] = qq;
+    queue_total++;
+    return qq;
+    }
+  return NULL;
+  }
+
+/* Also handle insertion at the start or end of the queue
+as special cases. */
+
+if (Ustrcmp(name, (queue_index[0])->name) < 0)
+  {
+  if (action != queue_add) return NULL;
+  if ((qq = set_up(name, dir_char)) != NULL)
+    {
+    qq->next = queue_index[0];
+    (queue_index[0])->prev = qq;
+    queue_index[0] = qq;
+    queue_total++;
+    return qq;
+    }
+  return NULL;
+  }
+
+if (Ustrcmp(name, (queue_index[queue_index_size-1])->name) > 0)
+  {
+  if (action != queue_add) return NULL;
+  if ((qq = set_up(name, dir_char)) != NULL)
+    {
+    qq->prev = queue_index[queue_index_size-1];
+    (queue_index[queue_index_size-1])->next = qq;
+    queue_index[queue_index_size-1] = qq;
+    queue_total++;
+    return qq;
+    }
+  return NULL;
+  }
+
+/* Use binary chopping on the index to get a range of the queue to search
+when the name is somewhere in the middle, if present. */
+
+while (middle > first)
+  {
+  if (Ustrcmp(name, (queue_index[middle])->name) >= 0) first = middle;
+    else last = middle;
+  middle = (first + last)/2;
+  }
+
+/* Now search down the part of the queue in which the item must
+lie if it exists. Both end points are inclusive - though in fact
+the bottom one can only be = if it is the original bottom. */
+
+p = queue_index[first];
+q = queue_index[last];
+
+for (;;)
+  {
+  int c = Ustrcmp(name, p->name);
+
+  /* Already on queue; mark seen if required. */
+
+  if (c == 0)
+    {
+    if (action == queue_add) p->seen = TRUE;
+    return p;
+    }
+
+  /* Not on the queue; add an entry if required. Note that set-up might
+  fail (the file might vanish under our feet). Note also that we know
+  there is always a previous item to p because the end points are
+  inclusive. */
+
+  else if (c < 0)
+    {
+    if (action == queue_add)
+      {
+      if ((qq = set_up(name, dir_char)) != NULL)
+        {
+        qq->next = p;
+        qq->prev = p->prev;
+        p->prev->next = qq;
+        p->prev = qq;
+        queue_total++;
+        return qq;
+        }
+      }
+    return NULL;
+    }
+
+  /* Control should not reach here if p == q, because the name
+  is supposed to be <= the name of the bottom item. */
+
+  if (p == q) return NULL;
+
+  /* Else might be further down the queue; continue */
+
+  p = p->next;
+  }
+
+/* Control should never reach here. */
+}
+
+
+
+/*************************************************
+*        Scan the exim spool directory           *
+*************************************************/
+
+/* If we discover that there are subdirectories, set a flag so that the menu
+code knows to look for them. We count the entries to set the value for the
+queue stripchart, and set up data for the queue display window if the "full"
+option is given. */
+
+void scan_spool_input(int full)
+{
+int i;
+int subptr;
+int subdir_max = 1;
+int count = 0;
+int indexptr = 1;
+queue_item *p;
+struct dirent *ent;
+DIR *dd;
+uschar input_dir[256];
+uschar subdirs[64];
+
+subdirs[0] = 0;
+stripchart_total[0] = 0;
+
+sprintf(CS input_dir, "%s/input", spool_directory);
+subptr = Ustrlen(input_dir);
+input_dir[subptr+2] = 0;               /* terminator for lengthened name */
+
+/* Loop for each spool file on the queue - searching any subdirectories that
+may exist. When initializing eximon, every file will have to be read. To show
+there is progress, output a dot for each one to the standard output. */
+
+for (i = 0; i < subdir_max; i++)
+  {
+  int subdirchar = subdirs[i];      /* 0 for main directory */
+  if (subdirchar != 0)
+    {
+    input_dir[subptr] = '/';
+    input_dir[subptr+1] = subdirchar;
+    }
+
+  dd = opendir(CS input_dir);
+  if (dd == NULL) continue;
+
+  while ((ent = readdir(dd)) != NULL)
+    {
+    uschar *name = US ent->d_name;
+    int len = Ustrlen(name);
+
+    /* If we find a single alphameric sub-directory on the first
+    pass, add it to the list for subsequent scans, and remember that
+    we are dealing with a split directory. */
+
+    if (i == 0 && len == 1 && isalnum(*name))
+      {
+      subdirs[subdir_max++] = *name;
+      spool_is_split = TRUE;
+      continue;
+      }
+
+    /* Otherwise, if it is a header spool file, add it to the list */
+
+    if (len == SPOOL_NAME_LENGTH &&
+        name[SPOOL_NAME_LENGTH - 2] == '-' &&
+        name[SPOOL_NAME_LENGTH - 1] == 'H')
+      {
+      uschar basename[SPOOL_NAME_LENGTH + 1];
+      stripchart_total[0]++;
+      if (!eximon_initialized) { printf("."); fflush(stdout); }
+      Ustrcpy(basename, name);
+      basename[SPOOL_NAME_LENGTH - 2] = 0;
+      if (full) find_queue(basename, queue_add, subdirchar);
+      }
+    }
+  closedir(dd);
+  }
+
+/* If simply counting the number, we are done; same if there are no
+items in the in-store queue. */
+
+if (!full || queue_total == 0) return;
+
+/* Now scan the queue and remove any items that were not in the directory. At
+the same time, set up the index pointers into the queue. Because we are
+removing items, the total that we are comparing against isn't actually correct,
+but in a long queue it won't make much difference, and in a short queue it
+doesn't matter anyway!*/
+
+p = queue_index[0];
+while (p != NULL)
+  {
+  if (!p->seen)
+    {
+    queue_item *next = p->next;
+    if (p->prev == NULL) queue_index[0] = next;
+      else p->prev->next = next;
+    if (next == NULL)
+      {
+      int i;
+      queue_item *q = queue_index[queue_index_size-1];
+      for (i = queue_index_size - 1; i >= 0; i--)
+        if (queue_index[i] == q) queue_index[i] = p->prev;
+      }
+    else next->prev = p->prev;
+    clean_up(p);
+    queue_total--;
+    p = next;
+    }
+  else
+    {
+    if (++count > (queue_total * indexptr)/(queue_index_size-1))
+      {
+      queue_index[indexptr++] = p;
+      }
+    p->seen = FALSE;  /* for next time */
+    p = p->next;
+    }
+  }
+
+/* If a lot of messages have been removed at the bottom, we may not
+have got the index all filled in yet. Make sure all the pointers
+are legal. */
+
+while (indexptr < queue_index_size - 1)
+  {
+  queue_index[indexptr++] = queue_index[queue_index_size-1];
+  }
+}
+
+
+
+
+/*************************************************
+*    Update the recipients list for a message    *
+*************************************************/
+
+/* We read the spool file only if its update time differs from last time,
+or if there is a journal file in existence. */
+
+/* First, a local subroutine to scan the non-recipients tree and
+remove any of them from the address list */
+
+static void
+scan_tree(queue_item *p, tree_node *tn)
+{
+if (tn != NULL)
+  {
+  if (tn->left != NULL) scan_tree(p, tn->left);
+  if (tn->right != NULL) scan_tree(p, tn->right);
+  (void)find_dest(p, tn->name, dest_remove, FALSE);
+  }
+}
+
+/* The main function */
+
+static void update_recipients(queue_item *p)
+{
+int i;
+FILE *jread;
+void *reset_point;
+struct stat statdata;
+uschar buffer[1024];
+
+message_subdir[0] = p->dir_char;
+
+sprintf(CS buffer, "%s/input/%s/%s-J", spool_directory, message_subdir, p->name);
+jread = fopen(CS buffer, "r");
+if (jread == NULL)
+  {
+  sprintf(CS buffer, "%s/input/%s/%s-H", spool_directory, message_subdir, p->name);
+  if (Ustat(buffer, &statdata) < 0 || p->update_time == statdata.st_mtime)
+    return;
+  }
+
+/* Get the contents of the header file; if any problem, just give up.
+Arrange to recover the dynamic store afterwards. */
+
+reset_point = store_get(0);
+sprintf(CS buffer, "%s-H", p->name);
+if (spool_read_header(buffer, FALSE, TRUE) != spool_read_OK)
+  {
+  store_reset(reset_point);
+  if (jread != NULL) fclose(jread);
+  return;
+  }
+
+/* If there's a journal file, add its contents to the non-recipients tree */
+
+if (jread != NULL)
+  {
+  while (Ufgets(big_buffer, big_buffer_size, jread) != NULL)
+    {
+    int n = Ustrlen(big_buffer);
+    big_buffer[n-1] = 0;
+    tree_add_nonrecipient(big_buffer);
+    }
+  fclose(jread);
+  }
+
+/* Scan and process the recipients list, removing any that have already
+been delivered, and removing visible names. In the nonrecipients tree,
+domains are lower cased. */
+
+if (recipients_list != NULL)
+  {
+  for (i = 0; i < recipients_count; i++)
+    {
+    uschar *pp;
+    uschar *r = recipients_list[i].address;
+    tree_node *node = tree_search(tree_nonrecipients, r);
+
+    if (node == NULL)
+      {
+      uschar temp[256];
+      uschar *rr = temp;
+      Ustrcpy(temp, r);
+      while (*rr != 0 && *rr != '@') rr++;
+      while (*rr != 0) { *rr = tolower(*rr); rr++; }
+      node = tree_search(tree_nonrecipients, temp);
+      }
+
+    if ((pp = strstric(r+1, qualify_domain, FALSE)) != NULL &&
+      *(--pp) == '@') *pp = 0;
+    if (node == NULL)
+      (void)find_dest(p, r, dest_add, FALSE);
+    else
+      (void)find_dest(p, r, dest_remove, FALSE);
+    }
+  }
+
+/* We also need to scan the tree of non-recipients, which might
+contain child addresses that are not in the recipients list, but
+which may have got onto the address list as a result of eximon
+noticing an == line in the log. Then remember the update time,
+recover the dynamic store, and we are done. */
+
+scan_tree(p, tree_nonrecipients);
+p->update_time = statdata.st_mtime;
+store_reset(reset_point);
+}
+
+
+
+/*************************************************
+*              Display queue data                *
+*************************************************/
+
+/* The present implementation simple re-writes the entire information each
+time. Take some care to keep the scrolled position as it previously was, but,
+if it was at the bottom, keep it at the bottom. Take note of any hide list, and
+time out the entries as appropriate. */
+
+void
+queue_display(void)
+{
+int now = (int)time(NULL);
+queue_item *p = queue_index[0];
+
+if (menu_is_up) return;            /* Avoid nasty interactions */
+
+text_empty(queue_widget);
+
+while (p != NULL)
+  {
+  int count = 1;
+  dest_item *dd, *ddd;
+  uschar u = 'm';
+  int t = (now - p->input_time)/60;  /* minutes on queue */
+
+  if (t > 90)
+    {
+    u = 'h';
+    t = (t + 30)/60;
+    if (t > 72)
+      {
+      u = 'd';
+      t = (t + 12)/24;
+      if (t > 99)                    /* someone had > 99 days */
+        {
+        u = 'w';
+        t = (t + 3)/7;
+        if (t > 99)                  /* so, just in case */
+          {
+          u = 'y';
+          t = (t + 26)/52;
+          }
+        }
+      }
+    }
+
+  update_recipients(p);                   /* update destinations */
+
+  /* Can't set this earlier, as header data may change things. */
+
+  dd = p->destinations;
+
+  /* Check to see if this message is on the hide list; if any hide
+  item has timed out, remove it from the list. Hide if all destinations
+  are on the hide list. */
+
+  for (ddd = dd; ddd != NULL; ddd = ddd->next)
+    {
+    skip_item *sk;
+    skip_item **skp;
+    int len_address;
+
+    if (ddd->address[0] == '*') break;
+    len_address = Ustrlen(ddd->address);
+
+    for (skp = &queue_skip; ; skp = &(sk->next))
+      {
+      int len_skip;
+
+      sk = *skp;
+      while (sk != NULL && now >= sk->reveal)
+        {
+        *skp = sk->next;
+        store_free(sk);
+        sk = *skp;
+        if (queue_skip == NULL)
+          {
+          XtDestroyWidget(unhide_widget);
+          unhide_widget = NULL;
+          }
+        }
+      if (sk == NULL) break;
+
+      /* If this address matches the skip item, break (sk != NULL) */
+
+      len_skip = Ustrlen(sk->text);
+      if (len_skip <= len_address &&
+          Ustrcmp(ddd->address + len_address - len_skip, sk->text) == 0)
+        break;
+      }
+
+    if (sk == NULL) break;
+    }
+
+  /* Don't use more than one call of anon() in one statement - it uses
+  a fixed static buffer. */
+
+  if (ddd != NULL || dd == NULL)
+    {
+    text_showf(queue_widget, "%c%2d%c %s %s %-8s ",
+      (p->frozen)? '*' : ' ',
+      t, u,
+      string_format_size(p->size, big_buffer),
+      p->name,
+      (p->sender == NULL)? US"       " :
+        (p->sender[0] == 0)? US"<>     " : anon(p->sender));
+
+    text_showf(queue_widget, "%s%s%s",
+      (dd == NULL || dd->address[0] == '*')? "" : "<",
+      (dd == NULL)? US"" : anon(dd->address),
+      (dd == NULL || dd->address[0] == '*')? "" : ">");
+
+    if (dd != NULL && dd->parent != NULL && dd->parent->address[0] != '*')
+      text_showf(queue_widget, " parent <%s>", anon(dd->parent->address));
+
+    text_show(queue_widget, US"\n");
+
+    if (dd != NULL) dd = dd->next;
+    while (dd != NULL && count++ < queue_max_addresses)
+      {
+      text_showf(queue_widget, "                                     <%s>",
+        anon(dd->address));
+      if (dd->parent != NULL && dd->parent->address[0] != '*')
+        text_showf(queue_widget, " parent <%s>", anon(dd->parent->address));
+      text_show(queue_widget, US"\n");
+      dd = dd->next;
+      }
+    if (dd != NULL)
+      text_showf(queue_widget, "                                     ...\n");
+    }
+
+  p = p->next;
+  }
+}
+
+/* End of em_queue.c */
diff --git a/exim_monitor/em_strip.c b/exim_monitor/em_strip.c
new file mode 100644 (file)
index 0000000..00675d4
--- /dev/null
@@ -0,0 +1,264 @@
+/*************************************************
+*                   Exim Monitor                 *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+
+#include "em_hdr.h"
+
+/* This module contains functions for handling stripcharts */
+
+
+/*************************************************
+*               Static variables                 *
+*************************************************/
+
+static int     queue_first_time = 1;         /* flag for resetting time */
+static int     size_first_time = 1;          /* and another */
+
+static int     stripchart_count = 0;         /* count stripcharts created */
+static int    *stripchart_delay;             /* vector of delay counts */
+static Widget *stripchart_label;             /* vector of label widgets */
+static int    *stripchart_last_total;        /* vector of prevous values */
+static int    *stripchart_max;               /* vector of maxima */
+static int    *stripchart_middelay;          /* vector of */
+static int    *stripchart_midmax;            /* vector of */
+static uschar  **stripchart_name;              /* vector of name strings */
+static Widget  stripchart_prev_chart = NULL; /* previously created chart */
+static Widget  stripchart_prev_label = NULL; /* previously created label */
+
+
+
+/*************************************************
+*               Initialize                       *
+*************************************************/
+
+void stripchart_init(void)
+{
+stripchart_delay =      (int *)store_malloc(stripchart_number * sizeof(int));
+stripchart_label =   (Widget *)store_malloc(stripchart_number * sizeof(Widget));
+stripchart_last_total = (int *)store_malloc(stripchart_number * sizeof(int));
+stripchart_max =        (int *)store_malloc(stripchart_number * sizeof(int));
+stripchart_middelay =   (int *)store_malloc(stripchart_number * sizeof(int));
+stripchart_midmax =     (int *)store_malloc(stripchart_number * sizeof(int));
+stripchart_name =     (uschar **)store_malloc(stripchart_number * sizeof(uschar *));
+stripchart_total =      (int *)store_malloc(stripchart_number * sizeof(int));
+}
+
+
+
+/*************************************************
+*           Stripchart callback function         *
+*************************************************/
+
+/* The client data is the index of the stripchart. We have to play
+a little game in order to ensure that the double value is correctly
+passed back via the value pointer without the compiler doing an
+unwanted cast. */
+
+static void stripchartAction(Widget w, XtPointer client_data, XtPointer value)
+{
+double *ptr = (double *)value;
+static int thresholds[] =
+  {10, 20, 50, 100, 200, 500, 1000, 2000, 5000, 10000, 0};
+int num = (int)client_data;
+int oldmax = 0;
+int newmax = 0;
+int newvalue = 0;
+int i = 0;
+
+/* For the queue stripchart, the value is the current vector value.
+We reset the initial delay of 1 second to the normal value. */
+
+if (num == 0)
+  {
+  newvalue = stripchart_total[0];
+  if (queue_first_time)
+    {
+    xs_SetValues(w, 1, "update", stripchart_update);
+    queue_first_time = 0;
+    }
+  }
+
+/* For the size monitoring stripchart, the value is the percentage
+fullness of the partition. A similar fudge to the above is implemented
+for the first time. Not all OS have statvfs(); for those that don't this
+code is omitted. In fact it should never be obeyed, as we don't allow
+size_stripchart to get set in that case. For some OS the old function
+and struct name statfs is used; that is handled by a macro. */
+
+else if (size_stripchart != NULL && num == 1)
+  {
+#ifdef HAVE_STATFS
+  struct statvfs statbuf;
+  if (statvfs(CS size_stripchart, &statbuf) == 0)
+    {
+    int used = statbuf.f_blocks - statbuf.f_bfree;
+    int max = used + statbuf.f_bavail;
+    double fraction = ((double)used) / ((double)max);
+    newvalue = (int)((fraction + 0.005) * 100.0);
+    }
+#endif
+  if (size_first_time)
+    {
+    xs_SetValues(w, 1, "update", stripchart_update);
+    size_first_time = 0;
+    }
+  }
+
+/* For the configured stripcharts, the value to be set is
+the difference from last time; save the current total for
+next time. */
+
+else
+  {
+  newvalue = stripchart_total[num] - stripchart_last_total[num];
+  stripchart_last_total[num] = stripchart_total[num];
+  }
+
+/* Adjust the scale of the stripchart according to the value;
+we delay enlarging the scale for a while after the values
+reduce. Keep the maximum value while delaying, and reset
+down to that. For the size stripchart, the threshold is always
+forced to be at least 100. */
+
+while (thresholds[i] > 0)
+  {
+  int thresh = (size_stripchart != NULL && num == 1)? 100 : thresholds[i++];
+  if (newvalue < (double)thresh)
+    {
+    /* If the current maximum is less than required, or if it is
+    greater and we have delayed long enough, adjust the scale. */
+
+    if (stripchart_max[num] < thresh ||
+       (stripchart_max[num] > thresh && stripchart_delay[num]++ > 20))
+      {
+      uschar buffer[128];
+      newmax = (thresh > stripchart_midmax[num])?
+        thresh : stripchart_midmax[num];
+      if (newmax == 10) sprintf(CS buffer, "%s", stripchart_name[num]);
+        else sprintf(CS buffer, "%s x%d", stripchart_name[num], newmax/10);
+      if (size_stripchart != NULL && num == 1) Ustrcat(buffer, "%");
+      xs_SetValues(stripchart_label[num], 1, "label", buffer);
+      oldmax = stripchart_max[num];
+      stripchart_max[num] = newmax;
+      stripchart_midmax[num] = 0;
+      stripchart_delay[num] -= stripchart_middelay[num];
+      }
+
+    /* Otherwise, if the current maximum is greater than required,
+    keep the highest value encountered during the delay, and its
+    position so we can adjust the delay when re-scaling. */
+
+    else if (stripchart_max[num] > thresh)
+      {
+      if (thresh > stripchart_midmax[num])
+        {
+        stripchart_midmax[num] = thresh;
+        stripchart_middelay[num] = stripchart_delay[num];
+        }
+      }
+
+    /* If the maximum is exactly what we need, reset the delay. */
+
+    if (stripchart_max[num] == thresh) stripchart_delay[num] = 0;
+    break;
+    }
+  }
+
+/* The vanilla Athena stripchart widget does not support change of
+scale - it just draws scale lines closer and closer together, which
+doesn't work when the number gets very large. However, we can cause
+it to change scale quite simply by recomputing all the values and
+then calling its repaint routine. I had to nobble the repaint routine
+too, to stop it changing scale to anything other than 10. There's
+probably a better way to do this, like adding some new resource, but
+I'm not a widget programmer and want to get on with the rest of
+eximon... */
+
+if (oldmax > 0)
+  {
+  int i;
+  StripChartWidget ww = (StripChartWidget)w;
+  ww->strip_chart.max_value = 0;
+  for (i = 0; i < (int)ww->strip_chart.interval; i++)
+    {
+    ww->strip_chart.valuedata[i] =
+      (ww->strip_chart.valuedata[i] * oldmax)/newmax;
+    if (ww->strip_chart.valuedata[i] > ww->strip_chart.max_value)
+      ww->strip_chart.max_value = ww->strip_chart.valuedata[i];
+    }
+  XClearWindow( XtDisplay(w), XtWindow(w));
+  ww->strip_chart.interval = repaint_window(ww, 0, (int)w->core.width);
+  }
+
+/* Pass back the new value at the new scale */
+
+*ptr = ((double)newvalue * 10.0)/(double)(stripchart_max[num]);
+}
+
+
+
+/*************************************************
+*            Create one stripchart               *
+*************************************************/
+
+/* This function creates two widgets, one being the title and the other being
+the stripchart. The client_data values for each stripchart are index into the
+stripchart_values vector; each new stripchart just gets the next number. There
+is a fudge for the very first stripchart, which is the queue length display,
+and for the second if it is a partition size display; its update time is
+initially set to 1 second so that it gives an immediate display of the queue.
+The first time its callback function is obeyed, the update time gets reset. */
+
+void create_stripchart(Widget parent, uschar *title)
+{
+Widget chart;
+
+Widget label = XtCreateManagedWidget("label",
+  labelWidgetClass, parent, NULL, 0);
+
+xs_SetValues(label, 10,
+  "label",          title,
+  "width",          stripchart_width + 2,
+  "borderWidth",    0,
+  "internalHeight", 0,
+  "internalWidth",  0,
+  "left",           XawChainLeft,
+  "right",          XawChainLeft,
+  "top",            XawChainTop,
+  "bottom",         XawChainTop,
+  XtNfromHoriz,     stripchart_prev_label);
+
+chart = XtCreateManagedWidget("stripchart",
+  mystripChartWidgetClass, parent, NULL, 0);
+
+xs_SetValues(chart, 11,
+  "jumpScroll", 1,
+  "update",     (stripchart_count < stripchart_varstart)? 1:stripchart_update,
+  "minScale",   10,
+  "width",      stripchart_width,
+  "height",     stripchart_height,
+  "left",       XawChainLeft,
+  "right",      XawChainLeft,
+  "top",        XawChainTop,
+  "bottom",     XawChainTop,
+  XtNfromHoriz, stripchart_prev_chart,
+  XtNfromVert,  label);
+
+XtAddCallback(chart, "getValue", stripchartAction,
+  (XtPointer)stripchart_count);
+
+stripchart_last_total[stripchart_count] = 0;
+stripchart_max[stripchart_count] = 10;
+stripchart_midmax[stripchart_count] = 0;
+stripchart_name[stripchart_count] = title;
+stripchart_prev_label = stripchart_label[stripchart_count] = label;
+stripchart_prev_chart = chart;
+stripchart_total[stripchart_count] = 0;
+stripchart_count++;
+}
+
+/* End of em_strip.c */
diff --git a/exim_monitor/em_text.c b/exim_monitor/em_text.c
new file mode 100644 (file)
index 0000000..3a36829
--- /dev/null
@@ -0,0 +1,73 @@
+/*************************************************
+*               Exim Monitor                     *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2012 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+
+#include "em_hdr.h"
+
+
+/* This module contains functions for displaying text in a
+text widget. It is not used for the log widget, because that
+is dynamically updated and has special scrolling requirements. */
+
+
+/* Count of characters displayed */
+
+static int text_count = 0;
+
+
+/*************************************************
+*               Empty the widget                 *
+*************************************************/
+
+void text_empty(Widget w)
+{
+XawTextBlock b;
+b.firstPos = 0;
+b.ptr = CS &b;
+b.format = FMT8BIT;
+b.length = 0;
+XawTextReplace(w, 0, text_count, &b);
+text_count = 0;
+XawTextSetInsertionPoint(w, text_count);
+}
+
+
+
+/*************************************************
+*                 Display text                   *
+*************************************************/
+
+void text_show(Widget w, uschar *s)
+{
+XawTextBlock b;
+b.firstPos = 0;
+b.ptr = CS s;
+b.format = FMT8BIT;
+b.length = Ustrlen(s);
+XawTextReplace(w, text_count, text_count, &b);
+text_count += b.length;
+XawTextSetInsertionPoint(w, text_count);
+}
+
+
+/*************************************************
+*           Display text from format             *
+*************************************************/
+
+void text_showf(Widget w, char *s, ...) PRINTF_FUNCTION(2,3);
+
+void text_showf(Widget w, char *s, ...)
+{
+va_list ap;
+uschar buffer[1024];
+va_start(ap, s);
+vsprintf(CS buffer, s, ap);
+va_end(ap);
+text_show(w, buffer);
+}
+
+/* End of em_text.c */
diff --git a/exim_monitor/em_version.c b/exim_monitor/em_version.c
new file mode 100644 (file)
index 0000000..a10aac4
--- /dev/null
@@ -0,0 +1,37 @@
+/*************************************************
+*                  Exim Monitor                  *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+#include "mytypes.h"
+#include "macros.h"
+#include <string.h>
+#include <stdlib.h>
+
+extern uschar *version_string;
+extern uschar *version_date;
+
+void
+version_init(void)
+{
+int i = 0;
+uschar today[20];
+
+version_string = US"2.06";
+
+Ustrcpy(today, __DATE__);
+if (today[4] == ' ') i = 1;
+today[3] = today[6] = '-';
+
+version_date = (uschar *)malloc(32);
+version_date[0] = 0;
+Ustrncat(version_date, today+4+i, 3-i);
+Ustrncat(version_date, today, 4);
+Ustrncat(version_date, today+7, 4);
+Ustrcat(version_date, " ");
+Ustrcat(version_date, __TIME__);
+}
+
+/* End of em_version.c */
diff --git a/exim_monitor/em_xs.c b/exim_monitor/em_xs.c
new file mode 100644 (file)
index 0000000..87371b5
--- /dev/null
@@ -0,0 +1,44 @@
+/*************************************************
+*               Exim Monitor                     *
+*************************************************/
+
+/* Copyright (c) University of Cambridge, 1995 - 2007 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* This file contains a number of subroutines that are in effect
+just alternative packaging for calls to various X functions that
+happen to be convenient for this program. */
+
+#include "em_hdr.h"
+
+
+
+/*************************************************
+*                  xs_SetValues                  *
+*************************************************/
+
+/* Unpick a variable-length argument list and set up an
+appropriate call to XtSetValues. To make it reasonably
+efficient, we keep a working Arg structure of length 15;
+the largest call in eximon sets 11 values. The code uses
+malloc/free if more, just in case there is ever a longer
+one that gets overlooked. */
+
+static Arg xs_temparg[15];
+
+void xs_SetValues(Widget w, Cardinal num_args, ...)
+{
+int i;
+va_list ap;
+Arg *aa = (num_args > 15)? (Arg *)malloc(num_args*sizeof(Arg)) : xs_temparg;
+va_start(ap, num_args);
+for (i = 0; i < num_args; i++)
+  {
+  aa[i].name = va_arg(ap, String);
+  aa[i].value = va_arg(ap, XtArgVal);
+  }
+XtSetValues(w, aa, num_args);
+if (num_args > 15) free(aa);
+}
+
+/* End of em_xs.c */
diff --git a/scripts/Configure b/scripts/Configure
new file mode 100755 (executable)
index 0000000..6c340ee
--- /dev/null
@@ -0,0 +1,10 @@
+#! /bin/sh -e
+
+# A script to be called to run all the other configuring scripts manually.
+
+scripts/Configure-Makefile
+scripts/Configure-os.h
+scripts/Configure-os.c
+scripts/Configure-config.h
+
+# End of Configure
diff --git a/scripts/Configure-Makefile b/scripts/Configure-Makefile
new file mode 100755 (executable)
index 0000000..eeb26ee
--- /dev/null
@@ -0,0 +1,292 @@
+#! /bin/sh
+
+# Shell script to build Makefile in a build directory. It must be called
+# from inside the directory. It does its own checking of when to rebuild; it
+# just got too horrendous to get it right in "make", because of the optionally
+# existing configuration files.
+
+
+# First off, get the OS type, and check that there is a make file for it.
+
+ostype=`../scripts/os-type -generic` || exit 1
+
+if [ ! -r ../OS/Makefile-$ostype ] ; then
+  echo ""
+  echo "*** Sorry - operating system $ostype is not supported"
+  echo "*** See OS/Makefile-* for supported systems" 1>&2
+  echo ""
+  exit 1
+fi
+
+# We also need the architecture type, in order to test for any architecture-
+# specific configuration files.
+
+archtype=`../scripts/arch-type` || exit 1
+
+# Now test for either the non-existence of Makefile, or for any of its
+# components being newer. Note that the "newer" script gives the right
+# answer (for our purposes) when the first file is non-existent.
+
+editme=../Local/Makefile
+rebuild=yes
+
+if [ -f Makefile ] ; then
+  rebuild=no
+  if ../scripts/newer $editme Makefile || \
+     ../scripts/newer $editme-$ostype Makefile || \
+     ../scripts/newer $editme-$archtype Makefile || \
+     ../scripts/newer $editme-$ostype-$archtype Makefile || \
+     ../scripts/newer ../scripts/Configure-Makefile Makefile || \
+     ../scripts/newer ../OS/Makefile-Base Makefile || \
+     ../scripts/newer ../OS/Makefile-Default Makefile
+  then
+    rebuild=yes
+  fi
+fi
+
+# If the "build" variable is set it means that a build name was explicitly
+# given. Arrange to pick up a build-specific configuration file.
+
+if [ "X$build" != "X" ] ; then
+  mfb=Local/Makefile-$build
+  if ../scripts/newer $editme-$build Makefile ; then
+    rebuild=yes
+  fi
+else
+  mfb=
+fi
+
+
+# If Makefile is up-to-date, no need to rebuild it.
+
+if [ $rebuild = no ] ; then
+  echo "\`Makefile' is up to date."
+  echo " "
+  exit
+fi
+
+# Makefile needs to be rebuilt in the current directory by joining
+# the generic default makefile, the OS base makefile, and then local
+# generic, OS-specific, architecture-specific, and OS+architecture-specific
+# makefiles, if they exist. These files all contain macro definitions, with
+# later definitions overriding earlier ones. Make a temporary file first, in
+# case things go wrong. A second temporary is needed for sorting out the
+# default Perl stuff. Use short macro names to save typing.
+
+mf=Makefile
+mft=$mf-t
+mftt=$mf-tt
+
+look_mf=lookups/Makefile
+look_mf_pre=${look_mf}.predynamic
+look_mf_post=${look_mf}.postdynamic
+
+# Ensure the temporary does not exist and start the new one by setting
+# the OSTYPE and ARCHTYPE variables.
+
+rm -f $mft $mftt $look_mf-t
+(echo "OSTYPE=$ostype"; echo "ARCHTYPE=$archtype"; echo "") > $mft || exit 1
+
+# Now concatenate the files to the temporary file. Copy the files using sed to
+# remove comments, blank lines, and trailing white space.
+
+# BEWARE: a tab character is needed in the sed command below. It has had
+# a nasty tendency to get lost in the past, causing a problem if a tab has
+# actually been present in one of the files. Use a variable to hold a space
+# and a tab to keep the tab in one place.
+
+st='    '
+
+for f in OS/Makefile-Default \
+         OS/Makefile-$ostype \
+         Local/Makefile \
+         Local/Makefile-$ostype \
+         Local/Makefile-$archtype \
+         Local/Makefile-$ostype-$archtype \
+         $mfb
+do   if test -r ../$f
+     then   echo "# From $f"
+            sed "/^#/d;/^[$st]*\$/d;s/[$st]*\$//" ../$f || exit 1
+            echo "# End of $f"
+            echo ""
+     fi
+done >> $mft || exit 1
+
+# handle pkg-config
+# beware portability of extended regexps with sed.
+
+egrep "^[$st]*(AUTH|LOOKUP)_[A-Z0-9_]*[$st]*=[$st]*" $mft | \
+  sed "s/[$st]*=/='/" | \
+  sed "s/\$/'/" > $mftt
+egrep "^[$st]*((USE_(OPENSSL|GNUTLS)_PC)|SUPPORT_TLS|USE_GNUTLS|PCRE_CONFIG|AVOID_GNUTLS_PKCS11)[$st]*=[$st]*" $mft | \
+  sed "s/[$st]*=/='/" | \
+  sed "s/\$/'/" >> $mftt
+if test -s $mftt
+then
+  (
+  echo "# pkg-config fixups"
+  . ./$mftt
+  for var in `cut -d = -f 1 < $mftt`; do
+    case $var in
+
+      USE_*_PC)
+        eval "pc_value=\"\$$var\""
+        need_this=''
+        if [ ".$SUPPORT_TLS" = "." ]; then
+          # no TLS, not referencing
+          true
+        elif [ ".$var" = ".USE_GNUTLS_PC" ] && [ ".$USE_GNUTLS" != "." ]; then
+          need_this=t
+        elif [ ".$var" = ".USE_OPENSSL_PC" ] && [ ".$USE_GNUTLS" = "." ]; then
+          need_this=t
+        fi
+        if [ ".$need_this" != "." ]; then
+          tls_include=`pkg-config --cflags $pc_value`
+          if [ $? -ne 0 ]; then
+            echo >&2 "*** Missing pkg-config for package $pc_value (for Exim $var build option)"
+            exit 1
+          fi
+          tls_libs=`pkg-config --libs $pc_value`
+          echo "TLS_INCLUDE=$tls_include"
+          echo "TLS_LIBS=$tls_libs"
+        fi
+        ;;
+
+      *_PC)
+        eval "pc_value=\"\$$var\""
+        base=`echo $var | sed 's/_PC$//'`
+        eval "basevalue=\"\$$base\""
+        if [ ".$basevalue" = "." ]; then
+          # not pulling in this module, _PC defined as default? Ignore
+          true
+        elif [ $basevalue = 2 ]; then
+          # module; handled in scripts/lookups-Makefile
+          true
+        else
+          # main binary
+          cflags=`pkg-config --cflags $pc_value`
+          if [ $? -ne 0 ]; then
+            echo >&2 "*** Missing pkg-config for package $pc_value (for Exim $var build option)"
+            exit 1
+          fi
+          libs=`pkg-config --libs $pc_value`
+          if [ "$var" != "${var#LOOKUP_}" ]; then
+            echo "LOOKUP_INCLUDE += $cflags"
+            echo "LOOKUP_LIBS += $libs"
+          elif [ "$var" != "${var#AUTH_}" ]; then
+            echo "CFLAGS += $cflags"
+            echo "AUTH_LIBS += $libs"
+          else
+            echo >&2 "Don't know how to handle pkg-config for $var"
+          fi
+        fi
+        ;;
+
+      PCRE_CONFIG)
+        case $PCRE_CONFIG in
+          yes|YES|y|Y)
+            cflags=`pcre-config --cflags`
+            if [ $? -ne 0 ]; then
+              echo >&2 "*** Missing pcre-config for regular expression support"
+              exit 1
+            fi
+            libs=`pcre-config --libs`
+            if [ ".$cflags" != "." ]; then
+              echo "INCLUDE += $cflags"
+            fi
+            echo "PCRE_LIBS=$libs"
+            ;;
+        esac
+        ;;
+
+      AVOID_GNUTLS_PKCS11)
+        echo "$var=yes"
+        ;;
+
+    esac
+  done
+  echo "# End of pkg-config fixups"
+  echo
+  ) >> $mft
+  subexit=$?
+  if [ $subexit -ne 0 ]; then
+    exit $subexit
+  fi
+fi
+rm -f $mftt
+
+# make the lookups Makefile with the definitions
+# the auxiliary script generates $look_mf_post from $look_mf_pre
+
+cp ../src/lookups/Makefile $look_mf_pre
+../scripts/lookups-Makefile
+
+# See if there is a definition of EXIM_PERL in what we have built so far.
+# If so, run Perl to find the default values for PERL_CC, PERL_CCOPTS,
+# and PERL_LIBS. These need to be put at the top of the Makefile, so we rename
+# what we have so far and then copy it afterwards. Use the value of PERL_COMMAND
+# if it has been defined.
+
+EXIM_PERL=`grep EXIM_PERL $mft`
+
+PERL_COMMAND=`grep PERL_COMMAND $mft | sed -e "\\$!d;s/^[$st]*PERL_COMMAND[$st]*=[$st]*//"`
+if [ "${PERL_COMMAND}" = "" ] ; then
+  PERL_COMMAND='perl'
+fi
+
+if [ "${EXIM_PERL}" != "" ] ; then
+  testperl=`$PERL_COMMAND --version`
+  if [ "$testperl" = "" ] ; then
+    echo "*** EXIM_PERL is set, but '$PERL_COMMAND --version' failed"
+    exit 1
+  fi
+
+  EXTUTILS_EMBED_NOT_INSTALLED=`$PERL_COMMAND -MExtUtils::Embed -e ";" 2>&1`
+  if [ "${EXTUTILS_EMBED_NOT_INSTALLED}" != "" ] ; then
+    echo "Please install ExtUtils::Embed for $PERL_COMMAND"
+    exit 1;
+  fi
+
+  mv $mft $mftt
+  echo "PERL_CC=`$PERL_COMMAND -MConfig -e 'print $Config{cc}'`" >>$mft
+  echo "PERL_CCOPTS=`$PERL_COMMAND -MExtUtils::Embed -e ccopts`" >>$mft
+  echo "PERL_LIBS=`$PERL_COMMAND -MExtUtils::Embed -e ldopts`" >>$mft
+  echo "" >>$mft
+  cat $mftt >> $mft
+  rm -f $mftt
+fi
+
+# Record the build variable in the Makefile.
+
+echo "build=$build" >>$mft
+echo "" >>$mft
+
+# Finally, join on the generic base make file, which contains the actual
+# rules and stuff.
+
+echo "# From ../OS/Makefile-Base" >> $mft
+cat ../OS/Makefile-Base >> $mft || exit 1
+
+# If the new makefile is the same as the existing one, say so, and just
+# update the timestamp. Otherwise remove the old and install the new.
+
+if      [ -s $mf ] && cmp -s $mft $mf && [ -s $look_mf ] && cmp -s $look_mf_post $look_mf
+then    echo ">>> rebuilt $mf unchanged"
+        echo " "
+        touch $mf || exit
+        rm -f $mft $look_mf_pre $look_mf_post
+elif    rm -f $mf $look_mf $look_mf_pre
+        mv $mft $mf
+       mv $look_mf_post $look_mf
+then    echo ">>> New $mf & $look_mf installed"
+        echo '>>> Use "make makefile" if you need to force rebuilding of the makefile'
+        echo " "
+else    echo " "
+        echo "*** Failed to install $mf - see $mft"
+       echo "    (or $look_mft)"
+        echo " "
+        exit 1;
+fi
+
+# vim: set ft=sh :
+# End of Configure-Makefile
diff --git a/scripts/Configure-config.h b/scripts/Configure-config.h
new file mode 100755 (executable)
index 0000000..75d366f
--- /dev/null
@@ -0,0 +1,60 @@
+#! /bin/sh
+
+# Build the config.h file, using the buildconfig program, first ensuring that
+# it exists.
+
+# 22-May-1996: remove the use of the "-a" flag for /bin/sh because it is not
+# implemented in the FreeBSD shell. Sigh.
+
+# 12-Mar-1997: add s/#.*$// to the sed script to allow for comments on the
+# ends of settings - someone got caught.
+
+# 18-Apr-1997: put the tab character into a variable to stop it getting
+# lost by accident (which has happened a couple of times).
+
+# 19-Jan-1998: indented settings in the makefile weren't being handled
+# correctly; added [$st]* before \\([A-Z] in the pattern, to ignore leading
+# space. Oddly, the pattern previously read ^\([A-Z which didn't seem to
+# cause a problem (but did when the new bit was put in).
+
+# 04-May-2005: if $1 is set, copy it into $MAKE, and then use $MAKE, if set,
+# instead of "make" so that if gmake is used, it is used consistently.
+
+if [ "$1" != "" ] ; then MAKE=$1 ; fi
+if [ "$MAKE" = "" ] ; then MAKE=make ; fi
+
+$MAKE buildconfig || exit 1
+
+# BEWARE: tab characters needed in the following sed command. They have had
+# a nasty tendency to get lost in the past, causing a problem if a tab has
+# actually been present in makefile. Use a variable to hold a space and a
+# tab to keep the tab in one place. This makes the sed option horrendous to
+# read, but the whole script is safer.
+
+st='    '
+
+(sed -n \
+  "/\\\$/d;s/#.*\$//;s/^[$st]*\\([A-Z][^:!+$st]*\\)[$st]*=[$st]*\\([^$st]*\\)[$st]*\$/\\1=\\2 export \\1/p" \
+  < Makefile ; echo "./buildconfig") | /bin/sh
+
+# If buildconfig ends with an error code, it will have output an error
+# message. Ensure that a broken config.h gets deleted.
+
+if [ $? != 0 ] ; then
+  rm -f config.h
+  exit 1
+fi
+
+# Double-check that config.h is complete.
+
+if [ "`tail -1 config.h`" != "/* End of config.h */" ] ; then
+  echo "*** config.h appears to be incomplete"
+  echo "*** unexpected failure in buildconfig program"
+  exit 1
+fi
+
+echo ">>> config.h built"
+echo ""
+
+# vim: set ft=sh :
+# End of Configure-config.h
diff --git a/scripts/Configure-eximon b/scripts/Configure-eximon
new file mode 100755 (executable)
index 0000000..0aca3b3
--- /dev/null
@@ -0,0 +1,51 @@
+#! /bin/sh
+
+# Shell script to build the configurable part of the Exim monitor's start-up
+# script. This is built from various configuration files. The final part is
+# added in the Makefile, using various macros that are available at that stage.
+
+scripts=../scripts
+
+# First off, get the OS type, and check that there is a make file for it.
+
+os=`$scripts/os-type -generic` || exit 1
+
+if     test ! -r ../OS/Makefile-$os
+then    echo ""
+       echo "*** Sorry - operating system $os is not supported"
+        echo "*** See OS/Makefile-* for supported systems" 1>&2
+        echo ""
+       exit 1;
+fi
+
+# We also need the architecture type, in order to test for any architecture-
+# specific configuration files.
+
+arch=`$scripts/arch-type` || exit 1
+
+# Build a file called eximon in the current directory by joining
+# the generic default configure file, the OS base configure file, and then
+# local generic, OS-specific, architecture-specific, and OS+architecture-
+# specific configurationfiles, if they exist. These files all contain variable
+# definitions, with later definitions overriding earlier ones.
+
+echo "#!/bin/sh" > eximon
+chmod a+x eximon
+
+# Concatenate the configuration files that exist
+
+for f in OS/eximon.conf-Default \
+         OS/eximon.conf-$os \
+         Local/eximon.conf \
+         Local/eximon.conf-$os \
+         Local/eximon.conf-$arch \
+         Local/eximon.conf-$os-$arch
+do   if test -r ../$f
+     then   echo "# From $f"
+           sed '/^#/d;/^[   ]*$/d' ../$f || exit 1
+           echo "# End of $f"
+           echo ""
+     fi
+done >> eximon || exit 1
+
+# End of Configure-eximon
diff --git a/scripts/Configure-os.c b/scripts/Configure-os.c
new file mode 100755 (executable)
index 0000000..f00a562
--- /dev/null
@@ -0,0 +1,30 @@
+#! /bin/sh
+
+# Shell script to build os.c. There doesn't have to be an OS-specific os.c
+# file, but if there is, it gets copied at the start of os.c. The basic src
+# copy of os.c contains generic functions, controlled in some cases by
+# macro switches so that where they are common to a number of OS, they can
+# just be switched in.
+
+scripts=../scripts
+
+# First off, get the OS type, and check that there is a make file for it.
+
+os=`$scripts/os-type -generic` || exit 1
+
+if     test ! -r ../OS/Makefile-$os
+then    echo ""
+       echo "*** Sorry - operating system $os is not supported"
+        echo "*** See OS/Makefile-* for supported systems" 1>&2
+        echo ""
+       exit 1;
+fi
+
+# Now build the file
+
+rm -f os.c
+echo '#include "exim.h"' > os.c || exit 1
+test -r ../OS/os.c-$os && cat ../OS/os.c-$os >> os.c
+echo '#include "../src/os.c"' >> os.c || exit 1
+
+# End of Configure-os.c
diff --git a/scripts/Configure-os.h b/scripts/Configure-os.h
new file mode 100755 (executable)
index 0000000..e9f6afd
--- /dev/null
@@ -0,0 +1,51 @@
+#! /bin/sh
+
+# Shell script to create a link to the appropriate OS-specific header file.
+
+scripts=../scripts
+
+# Get the OS type, and check that there is a make file for it.
+
+os=`$scripts/os-type -generic` || exit 1
+
+if     test ! -r ../OS/Makefile-$os
+then    echo ""
+       echo "*** Sorry - operating system $os is not supported"
+        echo "*** See OS/Makefile-* for supported systems" 1>&2
+        echo ""
+       exit 1;
+fi
+
+# Ensure there is an OS-specific header file, and link it to os.h. There should
+# always be one if there is a make file for the OS, so its absence is somewhat
+# disastrous.
+
+if     test ! -r ../OS/os.h-$os
+then    echo ""
+       echo "*** Build error: OS/os.h-$os file is missing"
+        echo ""
+       exit 1;
+fi
+rm -f os.h
+
+# In order to accomodate for the fudge below, copy the file instead of
+# symlinking it. Otherwise we pollute the clean copy with the fudge.
+cp -p ../OS/os.h-$os os.h || exit 1
+
+# Special-purpose fudge for older versions of Linux (pre 2.1.15) that
+# use the structure name "options" instead of "ip_options".
+
+if [ "$os" != "Linux" -a "$os" != "Linux-libc5" ] ; then exit 0; fi
+
+grep ip_options /usr/include/linux/ip.h >/dev/null
+if [ $? = 0 ] ; then exit 0; fi
+
+cat >>os.h <<End
+
+/* Fudge added because this Linux doesn't appear to have a definition
+for ip_options in /usr/include/linux/ip.h. */
+
+#define ip_options options
+End
+
+# End of Configure-os.h
diff --git a/scripts/MakeLinks b/scripts/MakeLinks
new file mode 100755 (executable)
index 0000000..01cd21f
--- /dev/null
@@ -0,0 +1,276 @@
+#!/bin/sh
+
+# Script to build links for all the exim source files from the system-
+# specific build directory. It should be run from within that directory.
+
+test ! -d ../src && \
+  echo "*** $0 should be run in a system-specific subdirectory." && \
+  exit 1
+test -r version.c && \
+  echo "*** It appears that $0 has already been run." && \
+  exit 1
+if [ -r pcre/Makefile ] ; then
+  echo "*** It appears that $0 was previously interrupted."
+  echo "*** You need to remove the build directory, and then run 'make' again."
+  exit 1
+fi
+
+
+echo ""
+echo ">>> Creating links to source files..."
+
+
+# The sources for modular parts of Exim have to be linked independently
+# in their own sub-directories, since their .o files are built using
+# their own Makefile in their sub-directory.
+
+# Firstly the lookups
+mkdir lookups
+cd lookups
+ln -s ../../src/lookups/README           README
+# Makefile is generated
+ln -s ../../src/lookups/cdb.c            cdb.c
+ln -s ../../src/lookups/dbmdb.c          dbmdb.c
+ln -s ../../src/lookups/dnsdb.c          dnsdb.c
+ln -s ../../src/lookups/dsearch.c        dsearch.c
+ln -s ../../src/lookups/ibase.c          ibase.c
+ln -s ../../src/lookups/ldap.h           ldap.h
+ln -s ../../src/lookups/ldap.c           ldap.c
+ln -s ../../src/lookups/lsearch.c        lsearch.c
+ln -s ../../src/lookups/mysql.c          mysql.c
+ln -s ../../src/lookups/redis.c          redis.c
+ln -s ../../src/lookups/nis.c            nis.c
+ln -s ../../src/lookups/nisplus.c        nisplus.c
+ln -s ../../src/lookups/oracle.c         oracle.c
+ln -s ../../src/lookups/passwd.c         passwd.c
+ln -s ../../src/lookups/pgsql.c          pgsql.c
+ln -s ../../src/lookups/spf.c            spf.c
+ln -s ../../src/lookups/sqlite.c         sqlite.c
+ln -s ../../src/lookups/testdb.c         testdb.c
+ln -s ../../src/lookups/whoson.c         whoson.c
+
+ln -s ../../src/lookups/lf_functions.h   lf_functions.h
+ln -s ../../src/lookups/lf_check_file.c  lf_check_file.c
+ln -s ../../src/lookups/lf_quote.c       lf_quote.c
+ln -s ../../src/lookups/lf_sqlperform.c  lf_sqlperform.c
+
+cd ..
+
+# Likewise for the code for the routers
+mkdir routers
+cd routers
+ln -s ../../src/routers/README           README
+ln -s ../../src/routers/Makefile         Makefile
+ln -s ../../src/routers/accept.h         accept.h
+ln -s ../../src/routers/accept.c         accept.c
+ln -s ../../src/routers/dnslookup.h      dnslookup.h
+ln -s ../../src/routers/dnslookup.c      dnslookup.c
+ln -s ../../src/routers/ipliteral.h      ipliteral.h
+ln -s ../../src/routers/ipliteral.c      ipliteral.c
+ln -s ../../src/routers/iplookup.h       iplookup.h
+ln -s ../../src/routers/iplookup.c       iplookup.c
+ln -s ../../src/routers/manualroute.h    manualroute.h
+ln -s ../../src/routers/manualroute.c    manualroute.c
+ln -s ../../src/routers/queryprogram.h   queryprogram.h
+ln -s ../../src/routers/queryprogram.c   queryprogram.c
+ln -s ../../src/routers/redirect.h       redirect.h
+ln -s ../../src/routers/redirect.c       redirect.c
+
+ln -s ../../src/routers/rf_functions.h          rf_functions.h
+ln -s ../../src/routers/rf_change_domain.c      rf_change_domain.c
+ln -s ../../src/routers/rf_expand_data.c        rf_expand_data.c
+ln -s ../../src/routers/rf_get_errors_address.c rf_get_errors_address.c
+ln -s ../../src/routers/rf_get_munge_headers.c  rf_get_munge_headers.c
+ln -s ../../src/routers/rf_get_transport.c      rf_get_transport.c
+ln -s ../../src/routers/rf_get_ugid.c           rf_get_ugid.c
+ln -s ../../src/routers/rf_queue_add.c          rf_queue_add.c
+ln -s ../../src/routers/rf_lookup_hostlist.c    rf_lookup_hostlist.c
+ln -s ../../src/routers/rf_self_action.c        rf_self_action.c
+ln -s ../../src/routers/rf_set_ugid.c           rf_set_ugid.c
+
+cd ..
+
+# Likewise for the code for the transports
+mkdir transports
+cd transports
+ln -s ../../src/transports/README           README
+ln -s ../../src/transports/Makefile         Makefile
+ln -s ../../src/transports/appendfile.h     appendfile.h
+ln -s ../../src/transports/appendfile.c     appendfile.c
+ln -s ../../src/transports/autoreply.h      autoreply.h
+ln -s ../../src/transports/autoreply.c      autoreply.c
+ln -s ../../src/transports/lmtp.h           lmtp.h
+ln -s ../../src/transports/lmtp.c           lmtp.c
+ln -s ../../src/transports/pipe.h           pipe.h
+ln -s ../../src/transports/pipe.c           pipe.c
+ln -s ../../src/transports/smtp.h           smtp.h
+ln -s ../../src/transports/smtp.c           smtp.c
+
+ln -s ../../src/transports/tf_maildir.c     tf_maildir.c
+ln -s ../../src/transports/tf_maildir.h     tf_maildir.h
+
+cd ..
+
+# Likewise for the code for the authorization functions
+mkdir auths
+cd auths
+ln -s ../../src/auths/README             README
+ln -s ../../src/auths/Makefile           Makefile
+ln -s ../../src/auths/b64encode.c        b64encode.c
+ln -s ../../src/auths/b64decode.c        b64decode.c
+ln -s ../../src/auths/call_pam.c         call_pam.c
+ln -s ../../src/auths/call_pwcheck.c     call_pwcheck.c
+ln -s ../../src/auths/call_radius.c      call_radius.c
+ln -s ../../src/auths/check_serv_cond.c  check_serv_cond.c
+ln -s ../../src/auths/cyrus_sasl.c       cyrus_sasl.c
+ln -s ../../src/auths/cyrus_sasl.h       cyrus_sasl.h
+ln -s ../../src/auths/gsasl_exim.c       gsasl_exim.c
+ln -s ../../src/auths/gsasl_exim.h       gsasl_exim.h
+ln -s ../../src/auths/get_data.c         get_data.c
+ln -s ../../src/auths/get_no64_data.c    get_no64_data.c
+ln -s ../../src/auths/heimdal_gssapi.c   heimdal_gssapi.c
+ln -s ../../src/auths/heimdal_gssapi.h   heimdal_gssapi.h
+ln -s ../../src/auths/md5.c              md5.c
+ln -s ../../src/auths/xtextencode.c      xtextencode.c
+ln -s ../../src/auths/xtextdecode.c      xtextdecode.c
+ln -s ../../src/auths/cram_md5.c         cram_md5.c
+ln -s ../../src/auths/cram_md5.h         cram_md5.h
+ln -s ../../src/auths/plaintext.c        plaintext.c
+ln -s ../../src/auths/plaintext.h        plaintext.h
+ln -s ../../src/auths/pwcheck.c          pwcheck.c
+ln -s ../../src/auths/pwcheck.h          pwcheck.h
+ln -s ../../src/auths/auth-spa.c         auth-spa.c
+ln -s ../../src/auths/auth-spa.h         auth-spa.h
+ln -s ../../src/auths/dovecot.c          dovecot.c
+ln -s ../../src/auths/dovecot.h          dovecot.h
+ln -s ../../src/auths/sha1.c             sha1.c
+ln -s ../../src/auths/spa.c              spa.c
+ln -s ../../src/auths/spa.h              spa.h
+cd ..
+
+# Likewise for the code for the PDKIM library
+mkdir pdkim
+cd pdkim
+ln -s ../../src/pdkim/README             README
+ln -s ../../src/pdkim/Makefile           Makefile
+ln -s ../../src/pdkim/base64.c           base64.c
+ln -s ../../src/pdkim/base64.h           base64.h
+ln -s ../../src/pdkim/bignum.c           bignum.c
+ln -s ../../src/pdkim/bignum.h           bignum.h
+ln -s ../../src/pdkim/bn_mul.h           bn_mul.h
+ln -s ../../src/pdkim/pdkim.c            pdkim.c
+ln -s ../../src/pdkim/pdkim.h            pdkim.h
+ln -s ../../src/pdkim/rsa.c              rsa.c
+ln -s ../../src/pdkim/rsa.h              rsa.h
+ln -s ../../src/pdkim/sha1.c             sha1.c
+ln -s ../../src/pdkim/sha1.h             sha1.h
+ln -s ../../src/pdkim/sha2.c             sha2.c
+ln -s ../../src/pdkim/sha2.h             sha2.h
+cd ..
+
+# The basic source files for Exim and utilities. NB local_scan.h gets linked,
+# but local_scan.c does not, because its location is taken from the build-time
+# configuration. Likewise for the os.c file, which gets build dynamically.
+
+ln -s ../src/dbfunctions.h     dbfunctions.h
+ln -s ../src/dbstuff.h         dbstuff.h
+ln -s ../src/exim.h            exim.h
+ln -s ../src/functions.h       functions.h
+ln -s ../src/globals.h         globals.h
+ln -s ../src/local_scan.h      local_scan.h
+ln -s ../src/macros.h          macros.h
+ln -s ../src/mytypes.h         mytypes.h
+ln -s ../src/osfunctions.h     osfunctions.h
+ln -s ../src/store.h           store.h
+ln -s ../src/structs.h         structs.h
+ln -s ../src/lookupapi.h       lookupapi.h
+
+ln -s ../src/acl.c             acl.c
+ln -s ../src/buildconfig.c     buildconfig.c
+ln -s ../src/child.c           child.c
+ln -s ../src/crypt16.c         crypt16.c
+ln -s ../src/daemon.c          daemon.c
+ln -s ../src/dbfn.c            dbfn.c
+ln -s ../src/debug.c           debug.c
+ln -s ../src/deliver.c         deliver.c
+ln -s ../src/directory.c       directory.c
+ln -s ../src/dns.c             dns.c
+ln -s ../src/drtables.c        drtables.c
+ln -s ../src/dummies.c         dummies.c
+ln -s ../src/enq.c             enq.c
+ln -s ../src/exim.c            exim.c
+ln -s ../src/exim_dbmbuild.c   exim_dbmbuild.c
+ln -s ../src/exim_dbutil.c     exim_dbutil.c
+ln -s ../src/exim_lock.c       exim_lock.c
+ln -s ../src/expand.c          expand.c
+ln -s ../src/filter.c          filter.c
+ln -s ../src/filtertest.c      filtertest.c
+ln -s ../src/globals.c         globals.c
+ln -s ../src/header.c          header.c
+ln -s ../src/host.c            host.c
+ln -s ../src/ip.c              ip.c
+ln -s ../src/log.c             log.c
+ln -s ../src/lss.c             lss.c
+ln -s ../src/match.c           match.c
+ln -s ../src/moan.c            moan.c
+ln -s ../src/parse.c           parse.c
+ln -s ../src/perl.c            perl.c
+ln -s ../src/queue.c           queue.c
+ln -s ../src/rda.c             rda.c
+ln -s ../src/readconf.c        readconf.c
+ln -s ../src/receive.c         receive.c
+ln -s ../src/retry.c           retry.c
+ln -s ../src/rewrite.c         rewrite.c
+ln -s ../src/rfc2047.c         rfc2047.c
+ln -s ../src/route.c           route.c
+ln -s ../src/search.c          search.c
+ln -s ../src/sieve.c           sieve.c
+ln -s ../src/smtp_in.c         smtp_in.c
+ln -s ../src/smtp_out.c        smtp_out.c
+ln -s ../src/spool_in.c        spool_in.c
+ln -s ../src/spool_out.c       spool_out.c
+ln -s ../src/std-crypto.c      std-crypto.c
+ln -s ../src/store.c           store.c
+ln -s ../src/string.c          string.c
+ln -s ../src/tls.c             tls.c
+ln -s ../src/tlscert-gnu.c     tlscert-gnu.c
+ln -s ../src/tlscert-openssl.c tlscert-openssl.c
+ln -s ../src/tls-gnu.c         tls-gnu.c
+ln -s ../src/tls-openssl.c     tls-openssl.c
+ln -s ../src/tod.c             tod.c
+ln -s ../src/transport.c       transport.c
+ln -s ../src/tree.c            tree.c
+ln -s ../src/verify.c          verify.c
+ln -s ../src/version.c         version.c
+ln -s ../src/dkim.c            dkim.c
+ln -s ../src/dkim.h            dkim.h
+ln -s ../src/dmarc.c           dmarc.c
+ln -s ../src/dmarc.h           dmarc.h
+ln -s ../src/valgrind.h        valgrind.h
+ln -s ../src/memcheck.h        memcheck.h
+
+# WITH_CONTENT_SCAN
+ln -s ../src/spam.c            spam.c
+ln -s ../src/spam.h            spam.h
+ln -s ../src/spool_mbox.c      spool_mbox.c
+ln -s ../src/regex.c           regex.c
+ln -s ../src/mime.c            mime.c
+ln -s ../src/mime.h            mime.h
+ln -s ../src/malware.c         malware.c
+
+# WITH_OLD_DEMIME
+ln -s ../src/demime.c          demime.c
+ln -s ../src/demime.h          demime.h
+
+# EXPERIMENTAL_*
+ln -s ../src/bmi_spam.c        bmi_spam.c
+ln -s ../src/bmi_spam.h        bmi_spam.h
+ln -s ../src/spf.c             spf.c
+ln -s ../src/spf.h             spf.h
+ln -s ../src/srs.c             srs.c
+ln -s ../src/srs.h             srs.h
+ln -s ../src/dcc.c             dcc.c
+ln -s ../src/dcc.h             dcc.h
+
+
+# End of MakeLinks
diff --git a/scripts/arch-type b/scripts/arch-type
new file mode 100755 (executable)
index 0000000..f5fe8eb
--- /dev/null
@@ -0,0 +1,74 @@
+#! /bin/sh
+
+# Shell script to determine the architecture type.
+
+# If EXIM_ARCHTYPE is set, use it. This allows a manual override.
+
+case "$EXIM_ARCHTYPE" in ?*) arch="$EXIM_ARCHTYPE";; esac
+
+# Otherwise, try to get a value from the uname command. When uname -p gives
+# "unknown" or something containing spaces, try -m.
+
+case "$arch" in '') arch=`uname -p 2> /dev/null`;; esac
+case "$arch" in ''|unknown|*\ *) arch=`uname -m 2> /dev/null`;; esac
+
+# Otherwise, see if ARCHTYPE is set. Some versions of NetBSD set it to
+# "NetBSD", which isn't very helpful. However, we expect uname to have
+# worked under NetBSD, so this shouldn't matter.
+
+case "$arch" in '') arch="$ARCHTYPE";; esac
+
+# Otherwise, as a cheap test, try shell's HOSTTYPE, but as tcsh sometimes sets
+# it to the OS name, ignore it if running with tcsh.
+
+case "$SHELL" in ?*tcsh) HOSTTYPE="";; esac
+
+case "$arch++$HOSTTYPE" in
+++?*) arch="$HOSTTYPE"
+      # Fix up disagreements :-)
+      case "$arch" in
+      sun4*)    arch=sparc;;
+
+# Comment by Vadim Vygonets:
+# Maybe sun4/sun4c/sun4m and sun4u (or whatever else they call the
+# Ultras, sparc64?) should be different platforms.  Maybe not.
+# NetBSD and OpenBSD (the latter is not supported) think about them
+# as different platforms.  Solaris doesn't seem to.  I have no idea
+# about Linux.
+
+      sgi)      arch=mips;;
+      MIPSEL)   arch=mips;;
+      esac
+      ;;
+esac
+
+# Give up if failed.
+
+case "$arch" in
+'') echo "" 1>&2
+    echo "*** Failed to determine the machine architecture type." 1>&2
+    echo "" 1>&2
+    echo UnKnown
+    exit 1;;
+esac
+
+# Get rid of any gash characters in the string
+
+arch=`echo $arch | sed 's,[^-+_.a-zA-Z0-9],,g'`
+
+# Some further fixups needed
+
+case "$arch" in
+i[3456]86*)         arch=i386;;
+RISC)               arch=mips;;     # MIPS Ultrix
+IP22)               arch=mips;;
+9000[78][0-9][0-9]) arch=hp9000s700;;
+9000[34][0-9][0-9]) arch=hp9000s400;;
+3050R)              arch=3050;;
+esac
+
+# OK, the script seems to have worked. Pass the value back.
+
+echo "$arch"
+
+# End of arch-type
diff --git a/scripts/exim_install b/scripts/exim_install
new file mode 100755 (executable)
index 0000000..b9330ae
--- /dev/null
@@ -0,0 +1,470 @@
+#! /bin/sh
+
+# Script to install Exim binaries in BIN_DIRECTORY, which is defined in
+# the local Makefile. It expects to be run in a build directory. It needs
+# to be run as root in order to make exim setuid to root. If exim runs setuid
+# to (e.g.) exim, this script should be run as that user or root.
+
+# This script also installs a default configuration file in CONFIGURE_FILE
+# if there is no configuration file there, but only if CONFIGURE_FILE specifies
+# single file. If it specifies a list, no action is taken.
+
+# If a default configuration file is installed, the existence of the system
+# aliases file is tested. A default, containing only comments, is installed if
+# necessary.
+
+# If INFO_DIRECTORY is defined in any of the local Makefiles, and the Exim doc
+# directory contains the Texinfo documentation, this script also installs a
+# the info files in INFO_DIRECTORY.
+
+# If DESTDIR is defined, all file paths are prefixed with ${DESTDIR}, with the
+# sole exception of the reference to the system aliases file in the default
+# configuration, because it is assumed that Exim is not actually going to be
+# run from this position. For backward compatibility, if DESTDIR is not
+# defined, ROOT is used instead.
+
+# The script can be made to output what it would do, without actually doing
+# anything, by giving it the option "-n" (cf make). Arguments are the names
+# of things to install. No arguments installs everything.
+
+do_chown=yes
+do_symlink=yes
+
+while [ $# -gt 0 ] ; do
+  case "$1" in
+    -n)
+      real="true || "
+      ver="verification "
+      com=": "
+      echo $com ""
+      echo $com "*** Verification mode only: no commands will actually be obeyed"
+      echo $com "*** You can cut and paste the bits you want to a shell, etc"
+      echo $com ""
+      echo cd `pwd`
+      ;;
+
+    -no_chown)
+      do_chown=no
+      ;;
+
+    -no_symlink)
+      do_symlink=no
+      ;;
+
+    *)
+      break
+      ;;
+  esac
+  shift
+done
+
+# Get the values of BIN_DIRECTORY, CONFIGURE_FILE, INFO_DIRECTORY, NO_SYMLINK,
+# SYSTEM_ALIASES_FILE, and EXE from the global Makefile (in the build
+# directory). EXE is empty except in the Cygwin environment. In each case, keep
+# the latest definition, thus respecting the Makefiles precedence. The sed
+# sequences here are messy, but have to be very "basic" in order to work on
+# Solaris, where the regular expressions in sed are primitive indeed. Modify at
+# your peril.
+
+BIN_DIRECTORY=`sed -n   -e '/^ *BIN_DIRECTORY *=/{s/^[^=]*= *//; s/ \{1,\}#.*//;s/ *$//;h;}' -e '${g;p;}' Makefile`
+CONFIGURE_FILE=`sed -n -e '/^ *CONFIGURE_FILE *=/{s/^[^=]*= *//; s/ \{1,\}#.*//;s/ *$//;h;}' -e '${g;p;}' Makefile`
+INFO_DIRECTORY=`sed -n -e '/^ *INFO_DIRECTORY *=/{s/^[^=]*= *//; s/ \{1,\}#.*//;s/ *$//;h;}' -e '${g;p;}' Makefile`
+NO_SYMLINK=`sed -n         -e '/^ *NO_SYMLINK *=/{s/^[^=]*= *//; s/ \{1,\}#.*//;s/ *$//;h;}' -e '${g;p;}' Makefile`
+
+CHOWN=`sed -n           -e '/^ *CHOWN_COMMAND *=/{s/^[^=]*= *//; s/ \{1,\}#.*//;s/ *$//;h;}' -e '${g;p;}' Makefile`
+MV=`sed -n                 -e '/^ *MV_COMMAND *=/{s/^[^=]*= *//; s/ \{1,\}#.*//;s/ *$//;h;}' -e '${g;p;}' Makefile`
+
+SYSTEM_ALIASES_FILE=`sed -n -e '/^ *SYSTEM_ALIASES_FILE *=/{s/^[^=]*= *//; s/ \{1,\}#.*//;s/ *$//;h;}' -e '${g;p;}' Makefile`
+EXE=`sed -n                                 -e '/^ *EXE *=/{s/^[^=]*= *//; s/ \{1,\}#.*//;s/ *$//;h;}' -e '${g;p;}' Makefile`
+
+# Set a default for SYSTEM_ALIASES_FILE
+
+if [ "${SYSTEM_ALIASES_FILE}" = "" ] ; then
+  SYSTEM_ALIASES_FILE=/etc/aliases
+fi
+
+# Allow INST_xx to over-ride xx
+case "$INST_BIN_DIRECTORY"       in ?*) BIN_DIRECTORY="$INST_BIN_DIRECTORY";; esac
+case "$INST_CONFIGURE_FILE"      in ?*) CONFIGURE_FILE="$INST_CONFIGURE_FILE";; esac
+case "$INST_INFO_DIRECTORY"      in ?*) INFO_DIRECTORY="$INST_INFO_DIRECTORY";; esac
+case "$INST_SYSTEM_ALIASES_FILE" in ?*) SYSTEM_ALIASES_FILE="$INST_SYSTEM_ALIASES_FILE";; esac
+
+case "$INST_CHOWN"               in ?*) CHOWN="$INST_CHOWN";; esac
+case "$INST_MV"                  in ?*) MV="$INST_MV";; esac
+
+case "$INST_UID"     in '') INST_UID=root;;    *) INST_UID="$INST_UID";; esac
+case "$INST_CP"      in '') CP=cp;;            *) CP="$INST_CP";; esac
+case "$INST_LN"      in '') LN=ln;;            *) LN="$INST_LN";; esac
+case "$INST_CHMOD"   in '') CHMOD=chmod;;      *) CHMOD="$INST_CHMOD";; esac
+case "$INST_DIRNAME" in '') DIRNAME=dirname;;  *) DIRNAME="$INST_DIRNAME";; esac
+case "$INST_MKDIR"   in '') MKDIR=mkdir;;      *) MKDIR="$INST_MKDIR";; esac
+
+# Allow the user to over-ride xx
+case "$inst_dest"    in ?*) BIN_DIRECTORY="$inst_dest";; esac
+case "$inst_conf"    in ?*) CONFIGURE_FILE="$inst_conf";; esac
+case "$inst_info"    in ?*) INFO_DIRECTORY="$inst_info";; esac
+case "$inst_aliases" in ?*) SYSTEM_ALIASES_FILE="$inst_aliases";; esac
+
+# Insert ${DESTDIR} at the start of all paths so that the whole thing can be
+# installed under a different file root. For backwards compatibility, use
+# ${ROOT} if ${DESTDIR} is not set. However, we need to save the value of
+# the real system aliases file, and use that in the default configuration.
+
+ACTUAL_SYSTEM_ALIASES_FILE=${SYSTEM_ALIASES_FILE}
+DESTDIR=${DESTDIR:-${ROOT}}
+
+BIN_DIRECTORY=${DESTDIR}${BIN_DIRECTORY}
+CONFIGURE_FILE=${DESTDIR}${CONFIGURE_FILE}
+SYSTEM_ALIASES_FILE=${DESTDIR}${SYSTEM_ALIASES_FILE}
+
+if [ "${INFO_DIRECTORY}" != "" ] ; then
+  INFO_DIRECTORY=${DESTDIR}${INFO_DIRECTORY}
+fi
+
+# Overrides of other things
+case "$inst_uid"     in ?*) INST_UID="$inst_uid";; esac
+case "$inst_cp"      in ?*) CP="$inst_cp";; esac
+case "$inst_mv"      in ?*) MV="$inst_mv";; esac
+case "$inst_ln"      in ?*) LN="$inst_ln";; esac
+case "$inst_chown"   in ?*) CHOWN="$inst_chown";; esac
+case "$inst_chmod"   in ?*) CHMOD="$inst_chmod";; esac
+case "$inst_dirname" in ?*) DIRNAME="$inst_dirname";; esac
+case "$inst_mkdir"   in ?*) MKDIR="$inst_mkdir";; esac
+
+# chown is a special case; in at least one OS it is in /usr/etc instead
+# of in /usr/bin, and therefore not likely to be on the path. Another OS
+# has it in /usr/sbin. This fudge tries to cope with these variations.
+
+# Otherwise, and for other commands, we assume that the normal PATH will
+# give access to where they are on your operating system (normally /usr/bin
+# or /bin).
+
+if [ "${CHOWN}" = "chown" -a -x /usr/sbin/chown ] ; then
+  CHOWN=/usr/sbin/chown
+fi
+
+if [ "${CHOWN}" = "chown" -a ! -f /usr/bin/chown -a -f /usr/etc/chown ] ; then
+  CHOWN=/usr/etc/chown
+fi
+
+# The values of CHOWN and MV taken from the Makefile are sometimes set to
+# "look_for_it", which causes a search of the usual suspects. This code is
+# similar to that in exicyclog, but has to be fudged for upper/lower case
+# distinctions.
+
+for cmd in CHOWN MV ; do
+  eval "oldcmd=\$$cmd"
+  if [ "$oldcmd" != "look_for_it" ] ; then continue ; fi
+  if [ "$cmd" = "CHOWN" ] ; then cmdlc="chown" ; fi
+  if [ "$cmd" = "MV" ] ; then cmdlc="mv" ; fi
+  newcmd=$cmdlc
+  for dir in /bin /usr/bin /usr/sbin /usr/etc ; do
+    if [ -f $dir/$cmdlc ] ; then
+      newcmd=$dir/$cmdlc
+      break
+    fi
+  done
+  eval $cmd=$newcmd
+done
+
+# See if the exim monitor has been built
+
+if [ -f eximon -a -f eximon.bin ]; then
+  exim_monitor="eximon eximon.bin"
+fi
+
+# If bin directory doesn't exist, try to create it
+
+if [ ! -d "${BIN_DIRECTORY}" ]; then
+  echo mkdir -p ${BIN_DIRECTORY}
+  ${real} mkdir -p ${BIN_DIRECTORY}
+  if [ $? -ne 0 ]; then
+    echo $com ""
+    echo $com "*** Exim installation ${ver}failed ***"
+    exit 1
+  else
+    ${real} echo $com ${BIN_DIRECTORY} created
+  fi
+fi
+
+# If no arguments, install everything
+
+if [ $# -gt 0 ]; then
+  set $@
+else
+  set exim${EXE} ${exim_monitor} exim_dumpdb${EXE} exim_fixdb${EXE} \
+      exim_tidydb${EXE} exinext exiwhat exim_dbmbuild${EXE} exicyclog \
+      exigrep eximstats exipick exiqgrep exiqsumm exim_lock${EXE} \
+      exim_checkaccess
+fi
+
+echo $com ""
+echo $com Installation directory is ${BIN_DIRECTORY}
+echo $com ""
+
+while [ $# -gt 0 ]; do
+  name=$1
+  shift
+
+  if [ ! -s ${name} ]; then
+    echo $com ""
+    echo $com "*** `pwd`/${name} does not exist or is empty"
+    echo $com "*** Have you built Exim successfully?"
+    echo $com "*** Exim installation ${ver}failed ***"
+    exit 1
+  fi
+
+  # The exim binary is handled specially
+
+  if [ $name = exim${EXE} ]; then
+    version=exim-`./exim -bV -C /dev/null | \
+      awk '/Exim version/ { OFS=""; print $3,"-",substr($4,2,length($4)-1) }'`${EXE}
+
+    if [ "${version}" = "exim-${EXE}" ]; then
+      echo $com ""
+      echo $com "*** Could not run ./exim to find version number ***"
+      echo $com "*** Exim installation ${ver}failed ***"
+      exit 1
+    fi
+
+    # Do something only if newer than existing file, or no existing file
+
+    if ../scripts/newer ${name} ${BIN_DIRECTORY}/${version}; then
+      echo ${CP} ${name} ${BIN_DIRECTORY}/${version}
+      ${real} ${CP} ${name} ${BIN_DIRECTORY}/${version}
+      if [ $? -ne 0 ]; then
+        echo $com ""
+        echo $com "*** Exim installation ${ver}failed ***"
+        exit 1
+      fi
+
+      # After copy, set ownership and permissions, unless disabled
+
+      if [ "$do_chown" != "no" ]; then
+        echo ${CHOWN} ${INST_UID} ${BIN_DIRECTORY}/${version}
+        ${real} ${CHOWN} ${INST_UID} ${BIN_DIRECTORY}/${version}
+        if [ $? -ne 0 ]; then
+          echo $com ""
+          echo $com "*** You must be ${INST_UID} to install exim ***"
+          exit 1
+        fi
+        echo ${CHMOD} a+x ${BIN_DIRECTORY}/${version}
+        ${real} ${CHMOD} a+x ${BIN_DIRECTORY}/${version}
+        if [ $? -ne 0 ]; then
+          echo $com ""
+          echo $com "*** Exim installation ${ver}failed ***"
+          exit 1
+        fi
+        echo ${CHMOD} u+s ${BIN_DIRECTORY}/${version}
+        ${real} ${CHMOD} u+s ${BIN_DIRECTORY}/${version}
+        if [ $? -ne 0 ]; then
+          echo $com ""
+          echo $com "*** Exim installation ${ver}failed ***"
+          exit 1
+        fi
+      else
+        echo $com "$CHOWN $INST_UID omitted: -no_chown was specified"
+        echo $com "$CHMOD u+s omitted: -no_chown was specified"
+      fi
+
+      # Now sort out the "exim" alias, unless NO_SYMLINK is set.
+
+      if [ "X$NO_SYMLINK" = "X" ] && [ "$do_symlink" != "no" ] ; then
+
+        #  First check whether "exim" exists in the directory.
+        if [ -f ${BIN_DIRECTORY}/exim ]; then
+
+          # If it's not a symbolic link, make a copy with the old version number
+          if [ `ls -l ${BIN_DIRECTORY}/exim | cut -c1-1` != 'l' ]; then
+            oldversion=exim-`${BIN_DIRECTORY}/exim -bV -C /dev/null | \
+              awk '/Exim version/ { OFS=""; print $3,"-",substr($4,2,length($4)-1) }'`${EXE}
+            if [ "${version}" = "${oldversion}" ] ; then
+              echo $com ""
+              echo $com "*** Existing file called exim has the same version and compile number ***"
+              echo $com "*** Exim installation ${ver}failed ***"
+              exit 1
+            fi
+            echo ${CP} ${BIN_DIRECTORY}/exim ${BIN_DIRECTORY}/${oldversion}
+            ${real} ${CP} ${BIN_DIRECTORY}/exim ${BIN_DIRECTORY}/${oldversion}
+            if [ $? -ne 0 ]; then
+              echo $com ""
+              echo $com "*** Exim installation ${ver}failed ***"
+              exit 1
+            fi
+          fi
+
+          # Now we can move the name "exim" to be a symbolic link to the new
+          # version, atomically.
+
+          echo \(cd ${BIN_DIRECTORY}\; ${LN} -s ${version} temporary_exim\)
+          (${real} cd ${BIN_DIRECTORY}; ${real} ${LN} -s ${version} temporary_exim)
+          if [ $? -ne 0 ]; then
+            echo $com ""
+            echo $com "*** Exim installation ${ver}failed ***"
+            exit 1
+          fi
+
+          echo ${MV} -f ${BIN_DIRECTORY}/temporary_exim ${BIN_DIRECTORY}/exim
+          ${real} ${MV} -f ${BIN_DIRECTORY}/temporary_exim ${BIN_DIRECTORY}/exim
+          if [ $? -ne 0 ]; then
+            echo $com ""
+            echo $com "*** Exim installation ${ver}failed ***"
+            exit 1
+          fi
+
+        # If "exim" does not already exist just create a symbolic link.
+
+        else
+          echo \(cd ${BIN_DIRECTORY}\; ${LN} -s ${version} exim\)
+          (${real} cd ${BIN_DIRECTORY}; ${real} ${LN} -s ${version} exim)
+          if [ $? -ne 0 ]; then
+            echo $com ""
+            echo $com "*** Exim installation ${ver}failed ***"
+            exit 1
+          fi
+        fi
+
+      else
+        echo $com "creation of symlink omitted"
+        if [ "X$NO_SYMLINK" != "X" ] ; then
+          echo $com "(NO_SYMLINK is specified in Local/Makefile)"
+        else
+          echo $com "(-no_symlink was specified)"
+        fi
+      fi
+
+    # New binary is not newer than the installed file
+
+    else
+      echo $com ${name} is not newer than ${BIN_DIRECTORY}/${version}
+    fi
+
+  # Handle everything other than the exim binary itself
+
+  else
+    if ../scripts/newer ${name} ${BIN_DIRECTORY}/${name}; then
+      if [ -f ${BIN_DIRECTORY}/${name} ]; then
+        echo ${CP} ${BIN_DIRECTORY}/${name} ${BIN_DIRECTORY}/${name}.O
+        ${real} ${CP} ${BIN_DIRECTORY}/${name} ${BIN_DIRECTORY}/${name}.O
+        if [ $? -ne 0 ]; then
+          echo $com ""
+          echo $com "*** Exim installation ${ver}failed ***"
+          exit 1
+        fi
+      fi
+      echo ${CP} ${name} ${BIN_DIRECTORY}
+      ${real} ${CP} ${name} ${BIN_DIRECTORY}
+      if [ $? -ne 0 ]; then
+        echo $com ""
+        echo $com "*** Exim installation ${ver}failed ***"
+        exit 1
+      fi
+    else
+      echo $com ${name} is not newer than ${BIN_DIRECTORY}/${name}
+    fi
+  fi
+
+done
+
+
+
+# If there is no configuration file, install the default, modifying it to refer
+# to the configured system aliases file. If there is no setting for
+# SYSTEM_ALIASES_FILE, use the traditional /etc/aliases. If the file does not
+# exist, install a default (dummy) for that too.
+
+# However, if CONFIGURE_FILE specifies a list of files, skip this code.
+
+echo $com ""
+
+if [ `expr -- "${CONFIGURE_FILE}" : ".*:"` -ne 0 ] ; then
+  echo $com Runtime configuration is specified as the following list:
+  echo $com ' ' ${CONFIGURE_FILE}
+  echo $com Therefore, skipping automatic installation.
+
+elif [ ! -f ${CONFIGURE_FILE} ]; then
+  echo $com Installing default configuration in ${CONFIGURE_FILE}
+  echo $com because there is no existing configuration file.
+  if [ "${SYSTEM_ALIASES_FILE}" = "" ] ; then
+    SYSTEM_ALIASES_FILE=/etc/aliases
+    echo $com This configuration has system aliases in ${SYSTEM_ALIASES_FILE}.
+  fi
+
+  echo ${MKDIR} -p `${DIRNAME} ${CONFIGURE_FILE}`
+  ${real} ${MKDIR} -p `${DIRNAME} ${CONFIGURE_FILE}`
+
+  echo sed -e '\\'
+  echo "  \"/SYSTEM_ALIASES_FILE/ s'SYSTEM_ALIASES_FILE'${ACTUAL_SYSTEM_ALIASES_FILE}'\"" '\\'
+  echo "  ../src/configure.default > \${CONFIGURE_FILE}"
+
+  # I can't find a way of writing this using the ${real} feature because
+  # it seems that the output redirection always happens, even when -n was
+  # specified. So control it the hard way.
+
+  if [ "$real" = "" ] ; then
+    sed -e \
+      "/SYSTEM_ALIASES_FILE/ s'SYSTEM_ALIASES_FILE'${ACTUAL_SYSTEM_ALIASES_FILE}'" \
+      ../src/configure.default > ${CONFIGURE_FILE}
+  else
+    true
+  fi
+
+  if [ $? -ne 0 ]; then
+    echo $com ""
+    echo $com "*** Exim installation ${ver}failed ***"
+    exit 1
+  fi
+  if [ ! -f ${SYSTEM_ALIASES_FILE} ]; then
+    echo $com '****'
+    echo $com Installing a dummy ${SYSTEM_ALIASES_FILE} file because you do not have
+    echo $com one, and the default configuration requires it. You should
+    echo $com edit ${SYSTEM_ALIASES_FILE} and at least create an alias for postmaster.
+    echo $com '***'
+    echo ${CP} ../src/aliases.default ${SYSTEM_ALIASES_FILE}
+    ${real} ${CP} ../src/aliases.default ${SYSTEM_ALIASES_FILE}
+  fi
+
+else
+  echo $com Configuration file ${CONFIGURE_FILE} already exists
+fi
+
+# Install info files if the directory is defined and the Texinfo
+# source documentation is present.
+
+if [ "${INFO_DIRECTORY}" != "" -a -f ../doc/spec.texinfo ] ; then
+  echo $com ""
+  if [ ! -d "${INFO_DIRECTORY}" ] ; then
+    echo mkdir -p ${INFO_DIRECTORY}
+    ${real} mkdir -p ${INFO_DIRECTORY}
+    if [ $? -ne 0 ]; then
+      echo $com ""
+      echo $com "*** Exim installation ${ver}failed ***"
+      exit 1
+    else
+      echo $com ${INFO_DIRECTORY} created
+    fi
+  fi
+
+  echo $com Info installation directory is ${INFO_DIRECTORY}
+  echo $com ""
+
+  ${real} makeinfo --no-split --output exim.info ../doc/spec.texinfo
+  echo ${CP} exim.info ${INFO_DIRECTORY}
+  ${real} ${CP} exim.info ${INFO_DIRECTORY}
+  ${real} install-info --section="Exim" \
+      --entry "* User guide: (exim).           Exim manual" \
+      ${INFO_DIRECTORY}/exim.info ${INFO_DIRECTORY}/dir
+  ${real} makeinfo --no-split --output exim_filter.info ../doc/filter.texinfo
+  echo ${CP} exim_filter.info ${INFO_DIRECTORY}
+  ${real} ${CP} exim_filter.info ${INFO_DIRECTORY}
+  ${real} install-info --section="Exim" \
+      --entry "* Filtering: (exim_filter).     Filtering mail with Exim" \
+      ${INFO_DIRECTORY}/exim_filter.info ${INFO_DIRECTORY}/dir
+fi
+
+# Everything OK
+
+echo $com ""
+echo $com Exim installation ${ver}complete
+
+# End of exim_install
diff --git a/scripts/lookups-Makefile b/scripts/lookups-Makefile
new file mode 100755 (executable)
index 0000000..61493c6
--- /dev/null
@@ -0,0 +1,197 @@
+#! /bin/sh
+
+# We turn the configure-built build-$foo/lookups/Makefile.predynamic into Makefile
+
+# We always re-exec ourselves at least once, because it's the cleanest and
+# most portable way to turn on various features we expect of POSIX sh.
+if [ -z "$EXIM_LOOKUP_MAKEFILE_ADJUSTED" ]
+then
+  SHELL=/bin/sh
+  EXIM_LOOKUP_MAKEFILE_ADJUSTED=yes
+  export EXIM_LOOKUP_MAKEFILE_ADJUSTED
+
+  # Solaris sh and tr are problematic until we get xpg4 variants
+  if [ -x /usr/xpg4/bin/sh ]
+  then
+    PATH="/usr/xpg4/bin:$PATH"
+    export PATH
+    SHELL=/usr/xpg4/bin/sh
+    export SHELL
+  fi
+
+  # IRIX uses /bin/ksh for sh but in a compatibility mode unless $_XPG == 1,
+  # where said compatibility mode disables $(...)
+  _XPG=1
+  export _XPG
+
+  # We need the _right_ tr, so must do that first; but if a shell which
+  # we're more confident is sane is available, let's try that.  Mostly,
+  # the problem is that "local" is not actually in "the" standard, it's
+  # just in every not-insane shell.  Though arguably, there are no shells
+  # with POSIX-ish syntax which qualify as "not insane".
+  for b in /bin/dash /bin/bash /usr/local/bin/bash
+  do
+    if [ -x "$b" ]
+    then
+      SHELL="$b"
+      break
+    fi
+  done
+  # if we get a report of a system with zsh but not bash, we can add that
+  # to the list, but be sure to enable sh_word_split in that case.
+
+  exec "$SHELL" "$0" "$@"
+fi
+
+input=lookups/Makefile.predynamic
+target=lookups/Makefile.postdynamic
+defs_source=Makefile-t
+tag_marker='MAGIC-TAG-MODS-OBJ-RULES-GO-HERE'
+
+tab='  '
+
+# We rely on tr(1) for translating case below.  Some people export
+# values of LC_CTYPE and LC_COLLATE which apparently break our assumptions.
+# We're a script expecting certain output based on known inputs and not dealing
+# with UTF8, so we should be safe doingthis:
+LC_ALL=C
+export LC_ALL
+
+if [ -f "$defs_source" ]
+then
+  :
+  # we are happy
+else
+  echo >&2 "$0: ERROR: MISSING FILE '${defs_source}'"
+  echo >&2 "$0: SHOULD HAVE BEEN CALLED FROM scripts/Configure-Makefile"
+  exit 1
+fi
+
+# nb: do not permit leading whitespace for this, as CFLAGS_DYNAMIC is exported
+# to the lookups subdir via a line with leading whitespace which otherwise
+# matches
+if grep -q "^CFLAGS_DYNAMIC[ $tab?:]*=" "$defs_source"
+then
+  # we have a definition, we're good to go
+  echo >&2 ">>> Creating lookups/Makefile for building dynamic modules"
+  enable_dynamic=yes
+else
+  echo >&2 ">>> Creating lookups/Makefile without dynamic module support"
+  enable_dynamic=''
+  # We always do something now, since there should always be a lookup,
+  # and now we need to run in order to put the OBJ=$(OBJ)+ rules in.  So we
+  # continue on.
+fi
+
+# For the want_ checks, we need to let the user override values from the make
+# command-line, not just check the Makefile.
+
+want_dynamic() {
+  local dyn_name="$1"
+  local re="LOOKUP_${dyn_name}[ $tab]*=[ $tab]*2"
+  env | grep -q "^$re"
+  if [ $? -eq 0 ]; then return 0; fi
+  grep -q "^[ $tab]*$re" "$defs_source"
+}
+
+want_at_all() {
+  local want_name="$1"
+  local re="LOOKUP_${want_name}[ $tab]*=[ $tab]*."
+  env | grep -q "^$re"
+  if [ $? -eq 0 ]; then return 0; fi
+  grep -q "^[ $tab]*$re" "$defs_source"
+}
+
+# Adapted want_at_all above to work for EXPERIMENTAL features
+want_experimental() {
+  local want_name="$1"
+  local re="EXPERIMENTAL_${want_name}[ $tab]*=[ $tab]*."
+  env | grep -q "^$re"
+  if [ $? -eq 0 ]; then return 0; fi
+  grep -q "^[ $tab]*$re" "$defs_source"
+}
+
+# The values of these variables will be emitted into the Makefile.
+
+MODS=""
+OBJ=""
+
+emit_module_rule() {
+  local lookup_name="$1"
+  local mod_name pkgconf
+  if [ "${lookup_name%:*}" = "$lookup_name" ]
+  then
+    # Square brackets are redundant but benign for POSIX compliant tr,
+    # however Solaris /usr/bin/tr requires them. Sometimes Solaris
+    # gets installed without a complete set of xpg4 tools, sigh.
+    mod_name=$(echo $lookup_name | tr [A-Z] [a-z])
+  else
+    mod_name="${lookup_name#*:}"
+    lookup_name="${lookup_name%:*}"
+  fi
+
+  if want_dynamic "$lookup_name"
+  then
+    if [ -z "$enable_dynamic" ]; then
+      echo >&2 "Missing CFLAGS_DYNAMIC prevents building dynamic $lookup_name"
+      exit 1
+    fi
+    MODS="${MODS} ${mod_name}.so"
+    pkgconf=$(grep "^LOOKUP_${lookup_name}_PC" "$defs_source")
+    if [ $? -eq 0 ]; then
+      pkgconf=$(echo $pkgconf | sed 's/^.*= *//')
+      echo "LOOKUP_${mod_name}_INCLUDE = $(pkg-config --cflags $pkgconf)"
+      echo "LOOKUP_${mod_name}_LIBS = $(pkg-config --libs $pkgconf)"
+    else
+      grep "^LOOKUP_${lookup_name}_" "$defs_source"
+      echo "LOOKUP_${mod_name}_INCLUDE = \$(LOOKUP_${lookup_name}_INCLUDE)"
+      echo "LOOKUP_${mod_name}_LIBS = \$(LOOKUP_${lookup_name}_LIBS)"
+    fi
+  elif want_at_all "$lookup_name"
+  then
+    OBJ="${OBJ} ${mod_name}.o"
+  fi
+}
+
+rm -f "$target"
+exec 5>&1
+exec > "$target"
+
+sed -n "1,/$tag_marker/p" < "$input"
+
+for name_mod in \
+    CDB DBM:dbmdb DNSDB DSEARCH IBASE LSEARCH MYSQL NIS NISPLUS ORACLE \
+    PASSWD PGSQL SQLITE TESTDB WHOSON
+do
+  emit_module_rule $name_mod
+done
+
+if want_at_all LDAP
+then
+  OBJ="${OBJ} ldap.o"
+fi
+
+# Because the variable is EXPERIMENTAL_SPF and not LOOKUP_SPF we
+# always include spf.o and compile a dummy if EXPERIMENTAL_SPF is not
+# defined.
+
+OBJ="${OBJ} spf.o"
+
+# Because the variable is EXPERIMENTAL_REDIS and not LOOKUP_REDIS we
+# use a different function to check for EXPERIMENTAL_* features
+# requested. Don't use the SPF method with dummy functions above.
+if want_experimental REDIS
+then
+  OBJ="${OBJ} redis.o"
+fi
+
+echo "MODS = $MODS"
+echo "OBJ = $OBJ"
+
+sed -n "/$tag_marker/,\$p" < "$input"
+
+exec >&5
+
+# Configure-Makefile will move $target into place
+
+# vim: set ft=sh sw=2 :
diff --git a/scripts/newer b/scripts/newer
new file mode 100755 (executable)
index 0000000..24c09e8
--- /dev/null
@@ -0,0 +1,21 @@
+#! /bin/sh
+
+# Script to determine whether the first file is newer than the second.
+# If the first does not exist, the answer is "no";
+# if the second does not exist, the answer is "yes";
+# otherwise their ages are compared using "find".
+
+if [ $# -ne 2 ]; then
+  echo "*** Two file names needed for 'newer' ***"
+  exit 2;
+fi
+
+if [ ! -f $1 ]; then exit 1; fi
+if [ ! -f $2 ]; then exit 0; fi
+
+case `find $1 -newer $2 -print` in
+'')    exit 1;;
+*)     exit 0;;
+esac
+
+# End
diff --git a/scripts/os-type b/scripts/os-type
new file mode 100755 (executable)
index 0000000..a188c4a
--- /dev/null
@@ -0,0 +1,164 @@
+#! /bin/sh
+
+# Shell script to determine the operating system type. Some of the heuristics
+# herein have accumulated over the years and may not strictly be needed now,
+# but they are left in under the principle of "If it ain't broke, don't fix
+# it."
+
+# For some OS there are two variants: a full name, which is used for the
+# build directory, and a generic name, which is used to identify the OS-
+# specific scripts, and which can be the same for different versions of
+# the OS. Solaris 2 is one such OS. The option -generic specifies the
+# latter type of output.
+
+# If EXIM_OSTYPE is set, use it. This allows a manual override.
+
+case "$EXIM_OSTYPE" in ?*) os="$EXIM_OSTYPE";; esac
+
+# Otherwise, try to get a value from the uname command. Use an explicit
+# option just in case there are any systems where -s is not the default.
+
+case "$os" in '') os=`uname -s`;; esac
+
+# Identify Glibc systems under different names.
+
+case "$os" in GNU) os=GNU;; esac
+case "$os" in GNU/*|Linux) os=Linux;; esac
+
+# It is believed that all systems respond to uname -s, but just in case
+# there is one that doesn't, use the shell's $OSTYPE variable. It is known
+# to be unhelpful for some systems (under IRIX is it "irix" and under BSDI
+# 3.0 it may be "386BSD") but those systems respond to uname -s, so this
+# doesn't matter.
+
+case "$os" in '') os="$OSTYPE";; esac
+
+# Failed to find OS type.
+
+case "$os" in
+'') echo "" 1>&2
+    echo "*** Failed to determine the operating system type." 1>&2
+    echo "" 1>&2
+    echo UnKnown
+    exit 1;;
+esac
+
+# Clean out gash characters
+
+os=`echo $os | sed 's,[^-+_.a-zA-Z0-9],,g'`
+
+# A value has been obtained for the os. Some massaging may be needed in
+# some cases to get a uniform set of values. In earlier versions of this
+# script, $OSTYPE was looked at before uname -s, and various shells set it
+# to things that are subtly different. It is possible that some of this may
+# no longer be needed.
+
+case "$os" in
+aix*)       os=AIX;;
+AIX*)       os=AIX;;
+bsdi*)      os=BSDI;;
+BSDOS)      os=BSDI;;
+BSD_OS)     os=BSDI;;
+CYGWIN*)    os=CYGWIN;;
+dgux)       os=DGUX;;
+freebsd*)   os=FreeBSD;;
+gnu)        os=GNU;;
+Irix5)      os=IRIX;;
+Irix6)      os=IRIX6;;
+IRIX64)     os=IRIX6;;
+irix6.5)    os=IRIX65;;
+IRIX)       version=`uname -r`
+            case "$version" in
+            5*)  os=IRIX;;
+            6.5) version=`uname -R | awk '{print $NF}'`
+                 version=`echo $version | sed 's,[^-+_a-zA-Z0-9],,g'`
+                 os=IRIX$version;;
+            6*)  os=IRIX632;;
+            esac;;
+HI-OSF1-MJ) os=HI-OSF;;
+HI-UXMPP)   os=HI-OSF;;
+hpux*)      os=HP-UX;;
+linux)      os=Linux;;
+linux-*)    os=Linux;;
+Linux-*)    os=Linux;;
+netbsd*)    os=NetBSD;;
+NetBSD*)    os=NetBSD;;
+openbsd*)   os=OpenBSD;;
+osf1)       os=OSF1;;
+qnx*)       os=QNX;;
+solaris*)   os=SunOS5;;
+sunos4*)    os=SunOS4;;
+UnixWare)   os=Unixware7;;
+Ultrix)     os=ULTRIX;;
+ultrix*)    os=ULTRIX;;
+esac
+
+# In the case of SunOS we need to distinguish between SunOS4 and Solaris (aka
+# SunOS5); in the case of BSDI we need to distinguish between versions 3 and 4;
+# in the case of HP-UX we need to distinguish between version 9 and later.
+
+case "$os" in
+SunOS)  case `uname -r` in
+        5*)     os="${os}5";;
+        4*)     os="${os}4";;
+        esac;;
+
+BSDI)   case `uname -r` in
+        3*)     os="${os}3";;
+        4.2*)   os="${os}4.2";;
+        4*)     os="${os}4";;
+        esac;;
+
+HP-UX)  case `uname -r` in
+        A.09*)  os="${os}-9";;
+        esac;;
+esac
+
+# Need to distinguish Solaris from the version on the HAL (64bit sparc,
+# CC=hcc -DV7). Also need to distinguish different versions of the OS
+# for building different binaries.
+
+case "$os" in
+SunOS5) case `uname -m` in
+        sun4H)  os="${os}-hal";;
+            *)  os="${os}-`uname -r`";;
+        esac
+        ;;
+
+# In the case of Linux we used to distinguish which libc was used so that
+# the old libc5 was supported as well as the current glibc. This support
+# was giving some people problems, so it was removed in June 2005, under
+# the assumption that nobody would be using libc5 any more (it is over seven
+# years old).
+
+# In the case of NetBSD we need to distinguish between a.out, ELF
+# and COFF binary formats.  However, a.out and COFF are the same
+# for our purposes, so both of them are defined as "a.out".
+# Todd Vierling of Wasabi Systems reported that NetBSD/sh3 (the
+# only NetBSD port that uses COFF binary format) will switch to
+# ELF soon.
+
+NetBSD) if echo __ELF__ | ${CC-cc} -E - | grep -q __ELF__ ; then
+        # Non-ELF system
+        os="NetBSD-a.out"
+        fi
+        ;;
+
+esac
+
+# If a generic OS name is requested, some further massaging is needed
+# for some systems.
+
+if [ "$1" = '-generic' ]; then
+  case "$os" in
+  SunOS5*) os=SunOS5;;
+  BSDI*)   os=BSDI;;
+  IRIX65*) os=IRIX65;;
+  esac
+fi
+
+# OK, the script seems to have worked. Pass the value back.
+
+echo "$os"
+
+# End of os-type
diff --git a/scripts/reversion b/scripts/reversion
new file mode 100755 (executable)
index 0000000..c461886
--- /dev/null
@@ -0,0 +1,80 @@
+#!/bin/sh
+
+# Update Exim's version header file.
+
+# Compatibility gross-ness for non-POSIX systems
+if [ -z "$EXIM_REVERSION_ADJUSTED" ]
+then
+  SHELL=/bin/sh
+  EXIM_REVERSION_ADJUSTED=yes
+  export SHELL EXIM_REVERSION_ADJUSTED
+  # Solaris:
+  if [ -x /usr/xpg4/bin/sh ]
+  then
+    PATH="/usr/xpg4/bin:$PATH"
+    SHELL=/usr/xpg4/bin/sh
+    export PATH SHELL
+  fi
+  # Irix:
+  _XPG=1 ; export _XPG
+  #
+  exec "$SHELL" "$0" "$@"
+fi
+
+# Read version information that was generated by a previous run of
+# this script, or during the release process.
+
+if   [ -f ./version.sh ]
+then .    ./version.sh
+elif [ -f ../src/version.sh ]
+then .    ../src/version.sh
+fi
+
+# If this tree is a git working directory, use that to get version information.
+
+if [ -d ../../.git ] || [ "$1" = "release" ]
+then
+       # Modify the output of git describe into separate parts for
+       # the name "exim" and the release and variant versions.
+       # Put a dot in the version number and remove a spurious g.
+       set $(git describe --dirty=-XX --match 'exim-4*' |
+               sed 's|-| |;s|_|.|;s|[-_]| _|;s|-g|-|')
+       # Only update if we need to
+       if [ "$2 $3" != "$EXIM_RELEASE_VERSION $EXIM_VARIANT_VERSION" ]
+       then
+               EXIM_RELEASE_VERSION="$2"
+               EXIM_VARIANT_VERSION="$3"
+               rm -f version.h
+       fi
+fi
+
+# If you are maintaining a patched version of Exim, you can either
+# create your own version.sh as part of your release process, or you
+# can modify EXIM_VARIANT_VERSION at this point in this script.
+
+case "$EXIM_RELEASE_VERSION" in
+'')    echo "*** Your copy of Exim lacks any version information."
+       exit 1
+esac
+
+EXIM_COMPILE_NUMBER=$(expr "${EXIM_COMPILE_NUMBER:-0}" + 1)
+
+echo "$EXIM_COMPILE_NUMBER" >cnumber.h
+
+( echo '# automatically generated file - see ../scripts/reversion'
+  echo EXIM_RELEASE_VERSION='"'"$EXIM_RELEASE_VERSION"'"'
+  echo EXIM_VARIANT_VERSION='"'"$EXIM_VARIANT_VERSION"'"'
+  echo EXIM_COMPILE_NUMBER='"'"$EXIM_COMPILE_NUMBER"'"'
+) >version.sh
+
+if [ ! -f version.h ]
+then
+( echo '/* automatically generated file - see ../scripts/reversion */'
+  echo '#define EXIM_RELEASE_VERSION "'"$EXIM_RELEASE_VERSION"'"'
+  echo '#define EXIM_VARIANT_VERSION "'"$EXIM_VARIANT_VERSION"'"'
+  echo '#define EXIM_VERSION_STR EXIM_RELEASE_VERSION EXIM_VARIANT_VERSION'
+) >version.h
+fi
+
+echo ">>> version $EXIM_RELEASE_VERSION$EXIM_VARIANT_VERSION #$EXIM_COMPILE_NUMBER"
+echo
diff --git a/scripts/source_checks b/scripts/source_checks
new file mode 100644 (file)
index 0000000..eac4b8d
--- /dev/null
@@ -0,0 +1,50 @@
+#!/bin/sh
+
+cd src;
+
+# Tables with struct items
+while read file table
+do
+  : $file $table
+  < $file \
+  perl -e '$/= undef; while (<>) { print $1 if /(?<='$table'\[\])\s*=\s*{\n(([^}].*\n)+)/m }' \
+  | awk '/{ (US)?"/ {print $2}' \
+  | awk -F\" '{print $2}' \
+  | LC_ALL=C sort -c \
+  || exit 1
+done <<-END
+       readconf.c      optionlist_config
+       globals.c       optionlist_auths
+       globals.c       debug_options
+       globals.c       header_names
+       globals.c       log_options
+       expand.c        item_table
+       transport.c     optionlist_transports
+       route.c         optionlist_routers
+       transports/appendfile.c appendfile_transport_options
+       transports/autoreply.c  autoreply_transport_options
+       transports/lmtp.c       lmtp_transport_options
+       transports/pipe.c       pipe_transport_options
+       transports/smtp.c       smtp_transport_options
+       expand.c        var_table
+END
+
+# Tables with just string items
+while read file table
+do
+  : $file $table
+  < $file \
+  perl -e '$/= undef; while (<>) { print $1 if /(?<='$table'\[\])\s*=\s*{\s?(([^}]*)+)}/m }' \
+  | awk -F\" '/"/ {print $2}' \
+  | LC_ALL=C sort -c \
+  || exit 1
+
+done <<-END
+       expand.c        item_table
+       expand.c        op_table_underscore
+       expand.c        op_table_main
+       expand.c        cond_table
+       acl.c           verbs
+       acl.c           conditions
+END
+
diff --git a/src/EDITME b/src/EDITME
new file mode 100644 (file)
index 0000000..d576fd7
--- /dev/null
@@ -0,0 +1,1343 @@
+##################################################
+#          The Exim mail transport agent         #
+##################################################
+
+# This is the template for Exim's main build-time configuration file. It
+# contains settings that are independent of any operating system. These are
+# things that are mostly sysadmin choices. The items below are divided into
+# those you must specify, those you probably want to specify, those you might
+# often want to specify, and those that you almost never need to mention.
+
+# Edit this file and save the result to a file called Local/Makefile within the
+# Exim distribution directory before running the "make" command.
+
+# Things that depend on the operating system have default settings in
+# OS/Makefile-Default, but these are overridden for some OS by files called
+# called OS/Makefile-<osname>. You can further override these by creating files
+# called Local/Makefile-<osname>, where "<osname>" stands for the name of your
+# operating system - look at the names in the OS directory to see which names
+# are recognized.
+
+# However, if you are building Exim for a single OS only, you don't need to
+# worry about setting up Local/Makefile-<osname>. Any build-time configuration
+# settings you require can in fact be placed in the one file called
+# Local/Makefile. It is only if you are building for several OS from the same
+# source files that you need to worry about splitting off your own OS-dependent
+# settings into separate files. (There's more explanation about how this all
+# works in the toplevel README file, under "Modifying the building process", as
+# well as in the Exim specification.)
+
+# One OS-specific thing that may need to be changed is the command for running
+# the C compiler; the overall default is gcc, but some OS Makefiles specify cc.
+# You can override anything that is set by putting CC=whatever in your
+# Local/Makefile.
+
+# NOTE: You should never need to edit any of the distributed Makefiles; all
+# overriding can be done in your Local/Makefile(s). This will make it easier
+# for you when the next release comes along.
+
+# The location of the X11 libraries is something else that is quite variable
+# even between different versions of the same operating system (and indeed
+# there are different versions of X11 as well, of course). The four settings
+# concerned here are X11, XINCLUDE, XLFLAGS (linking flags) and X11_LD_LIB
+# (dynamic run-time library). You need not worry about X11 unless you want to
+# compile the Exim monitor utility. Exim itself does not use X11.
+
+# Another area of variability between systems is the type and location of the
+# DBM library package. Exim has support for ndbm, gdbm, tdb, and Berkeley DB.
+# By default the code assumes ndbm; this often works with gdbm or DB, provided
+# they are correctly installed, via their compatibility interfaces. However,
+# Exim can also be configured to use the native calls for Berkeley DB (obsolete
+# versions 1.85, 2.x, 3.x, or the current 4.x version) and also for gdbm.
+
+# For some operating systems, a default DBM library (other than ndbm) is
+# selected by a setting in the OS-specific Makefile. Most modern OS now have
+# a DBM library installed as standard, and in many cases this will be selected
+# for you by the OS-specific configuration. If Exim compiles without any
+# problems, you probably do not have to worry about the DBM library. If you
+# do want or need to change it, you should first read the discussion in the
+# file doc/dbm.discuss.txt, which also contains instructions for testing Exim's
+# interface to the DBM library.
+
+# In Local/Makefiles blank lines and lines starting with # are ignored. It is
+# also permitted to use the # character to add a comment to a setting, for
+# example
+#
+# EXIM_GID=42   # the "mail" group
+#
+# However, with some versions of "make" this works only if there is no white
+# space between the end of the setting and the #, so perhaps it is best
+# avoided. A consequence of this facility is that it is not possible to have
+# the # character present in any setting, but I can't think of any cases where
+# this would be wanted.
+###############################################################################
+
+
+
+###############################################################################
+#                    THESE ARE THINGS YOU MUST SPECIFY                        #
+###############################################################################
+
+# Exim will not build unless you specify BIN_DIRECTORY, CONFIGURE_FILE, and
+# EXIM_USER. You also need EXIM_GROUP if EXIM_USER specifies a uid by number.
+
+# If you don't specify SPOOL_DIRECTORY, Exim won't fail to build. However, it
+# really is a very good idea to specify it here rather than at run time. This
+# is particularly true if you let the logs go to their default location in the
+# spool directory, because it means that the location of the logs is known
+# before Exim has read the run time configuration file.
+
+#------------------------------------------------------------------------------
+# BIN_DIRECTORY defines where the exim binary will be installed by "make
+# install". The path is also used internally by Exim when it needs to re-invoke
+# itself, either to send an error message, or to recover root privilege. Exim's
+# utility binaries and scripts are also installed in this directory. There is
+# no "standard" place for the binary directory. Some people like to keep all
+# the Exim files under one directory such as /usr/exim; others just let the
+# Exim binaries go into an existing directory such as /usr/sbin or
+# /usr/local/sbin. The installation script will try to create this directory,
+# and any superior directories, if they do not exist.
+
+BIN_DIRECTORY=/usr/exim/bin
+
+
+#------------------------------------------------------------------------------
+# CONFIGURE_FILE defines where Exim's run time configuration file is to be
+# found. It is the complete pathname for the file, not just a directory. The
+# location of all other run time files and directories can be changed in the
+# run time configuration file. There is a lot of variety in the choice of
+# location in different OS, and in the preferences of different sysadmins. Some
+# common locations are in /etc or /etc/mail or /usr/local/etc or
+# /usr/local/etc/mail. Another possibility is to keep all the Exim files under
+# a single directory such as /usr/exim. Whatever you choose, the installation
+# script will try to make the directory and any superior directories if they
+# don't exist. It will also install a default runtime configuration if this
+# file does not exist.
+
+CONFIGURE_FILE=/usr/exim/configure
+
+# It is possible to specify a colon-separated list of files for CONFIGURE_FILE.
+# In this case, Exim will use the first of them that exists when it is run.
+# However, if a list is specified, the installation script no longer tries to
+# make superior directories or to install a default runtime configuration.
+
+
+#------------------------------------------------------------------------------
+# The Exim binary must normally be setuid root, so that it starts executing as
+# root, but (depending on the options with which it is called) it does not
+# always need to retain the root privilege. These settings define the user and
+# group that is used for Exim processes when they no longer need to be root. In
+# particular, this applies when receiving messages and when doing remote
+# deliveries. (Local deliveries run as various non-root users, typically as the
+# owner of a local mailbox.) Specifying these values as root is not supported.
+
+EXIM_USER=
+
+# If you specify EXIM_USER as a name, this is looked up at build time, and the
+# uid number is built into the binary. However, you can specify that this
+# lookup is deferred until runtime. In this case, it is the name that is built
+# into the binary. You can do this by a setting of the form:
+
+# EXIM_USER=ref:exim
+
+# In other words, put "ref:" in front of the user name. If you set EXIM_USER
+# like this, any value specified for EXIM_GROUP is also passed "by reference".
+# Although this costs a bit of resource at runtime, it is convenient to use
+# this feature when building binaries that are to be run on multiple systems
+# where the name may refer to different uids. It also allows you to build Exim
+# on a system where there is no Exim user defined.
+
+# If the setting of EXIM_USER is numeric (e.g. EXIM_USER=42), there must
+# also be a setting of EXIM_GROUP. If, on the other hand, you use a name
+# for EXIM_USER (e.g. EXIM_USER=exim), you don't need to set EXIM_GROUP unless
+# you want to use a group other than the default group for the given user.
+
+# EXIM_GROUP=
+
+# Many sites define a user called "exim", with an appropriate default group,
+# and use
+#
+# EXIM_USER=exim
+#
+# while leaving EXIM_GROUP unspecified (commented out).
+
+
+#------------------------------------------------------------------------------
+# SPOOL_DIRECTORY defines the directory where all the data for messages in
+# transit is kept. It is strongly recommended that you define it here, though
+# it is possible to leave this till the run time configuration.
+
+# Exim creates the spool directory if it does not exist. The owner and group
+# will be those defined by EXIM_USER and EXIM_GROUP, and this also applies to
+# all the files and directories that are created in the spool directory.
+
+# Almost all installations choose this:
+
+SPOOL_DIRECTORY=/var/spool/exim
+
+
+
+###############################################################################
+#           THESE ARE THINGS YOU PROBABLY WANT TO SPECIFY                     #
+###############################################################################
+
+# If you need extra header file search paths on all compiles, put the -I
+# options in INCLUDE.  If you want the extra searches only for certain
+# parts of the build, see more specific xxx_INCLUDE variables below.
+
+# INCLUDE=-I/example/include
+
+# You need to specify some routers and transports if you want the Exim that you
+# are building to be capable of delivering mail. You almost certainly need at
+# least one type of lookup. You should consider whether you want to build
+# the Exim monitor or not.
+
+
+#------------------------------------------------------------------------------
+# These settings determine which individual router drivers are included in the
+# Exim binary. There are no defaults in the code; those routers that are wanted
+# must be defined here by setting the appropriate variables to the value "yes".
+# Including a router in the binary does not cause it to be used automatically.
+# It has also to be configured in the run time configuration file. By
+# commenting out those you know you don't want to use, you can make the binary
+# a bit smaller. If you are unsure, leave all of these included for now.
+
+ROUTER_ACCEPT=yes
+ROUTER_DNSLOOKUP=yes
+ROUTER_IPLITERAL=yes
+ROUTER_MANUALROUTE=yes
+ROUTER_QUERYPROGRAM=yes
+ROUTER_REDIRECT=yes
+
+# This one is very special-purpose, so is not included by default.
+
+# ROUTER_IPLOOKUP=yes
+
+
+#------------------------------------------------------------------------------
+# These settings determine which individual transport drivers are included in
+# the Exim binary. There are no defaults; those transports that are wanted must
+# be defined here by setting the appropriate variables to the value "yes".
+# Including a transport in the binary does not cause it to be used
+# automatically. It has also to be configured in the run time configuration
+# file. By commenting out those you know you don't want to use, you can make
+# the binary a bit smaller. If you are unsure, leave all of these included for
+# now.
+
+TRANSPORT_APPENDFILE=yes
+TRANSPORT_AUTOREPLY=yes
+TRANSPORT_PIPE=yes
+TRANSPORT_SMTP=yes
+
+# This one is special-purpose, and commonly not required, so it is not
+# included by default.
+
+# TRANSPORT_LMTP=yes
+
+
+#------------------------------------------------------------------------------
+# The appendfile transport can write messages to local mailboxes in a number
+# of formats. The code for three specialist formats, maildir, mailstore, and
+# MBX, is included only when requested. If you do not know what this is about,
+# leave these settings commented out.
+
+# SUPPORT_MAILDIR=yes
+# SUPPORT_MAILSTORE=yes
+# SUPPORT_MBX=yes
+
+
+#------------------------------------------------------------------------------
+# See below for dynamic lookup modules.
+#
+# If not using package management but using this anyway, then think about how
+# you perform upgrades and revert them. You should consider the benefit of
+# embedding the Exim version number into LOOKUP_MODULE_DIR, so that you can
+# maintain two concurrent sets of modules.
+# 
+# *BEWARE*: ability to modify the files in LOOKUP_MODULE_DIR is equivalent to
+# the ability to modify the Exim binary, which is often setuid root!  The Exim
+# developers only intend this functionality be used by OS software packagers
+# and we suggest that such packagings' integrity checks should be paranoid
+# about the permissions of the directory and the files within.
+
+# LOOKUP_MODULE_DIR=/usr/lib/exim/lookups/
+
+# To build a module dynamically, you'll need to define CFLAGS_DYNAMIC for
+# your platform.  Eg:
+# CFLAGS_DYNAMIC=-shared -rdynamic
+# CFLAGS_DYNAMIC=-shared -rdynamic -fPIC
+
+#------------------------------------------------------------------------------
+# These settings determine which file and database lookup methods are included
+# in the binary. See the manual chapter entitled "File and database lookups"
+# for discussion. DBM and lsearch (linear search) are included by default. If
+# you are unsure about the others, leave them commented out for now.
+# LOOKUP_DNSDB does *not* refer to general mail routing using the DNS. It is
+# for the specialist case of using the DNS as a general database facility (not
+# common).
+# If set to "2" instead of "yes" then the corresponding lookup will be
+# built as a module and must be installed into LOOKUP_MODULE_DIR. You need to
+# add -export-dynamic -rdynamic to EXTRALIBS. You may also need to add -ldl to
+# EXTRALIBS so that dlopen() is available to Exim. You need to define
+# LOOKUP_MODULE_DIR above so the exim binary actually loads dynamic lookup
+# modules.
+# Also, instead of adding all the libraries/includes to LOOKUP_INCLUDE and
+# LOOKUP_LIBS, add them to the respective LOOKUP_*_INCLUDE and LOOKUP_*_LIBS
+# (where * is the name as given here in this list). That ensures that only
+# the dynamic library and not the exim binary will be linked against the
+# library.
+# NOTE: LDAP cannot be built as a module!
+#
+# If your system has pkg-config then the _INCLUDE/_LIBS setting can be
+# handled for you automatically by also defining the _PC variable to reference
+# the name of the pkg-config package, if such is available.
+
+LOOKUP_DBM=yes
+LOOKUP_LSEARCH=yes
+LOOKUP_DNSDB=yes
+
+# LOOKUP_CDB=yes
+# LOOKUP_DSEARCH=yes
+# LOOKUP_IBASE=yes
+# LOOKUP_LDAP=yes
+# LOOKUP_MYSQL=yes
+# LOOKUP_NIS=yes
+# LOOKUP_NISPLUS=yes
+# LOOKUP_ORACLE=yes
+# LOOKUP_PASSWD=yes
+# LOOKUP_PGSQL=yes
+# LOOKUP_SQLITE=yes
+# LOOKUP_SQLITE_PC=sqlite3
+# LOOKUP_WHOSON=yes
+
+# These two settings are obsolete; all three lookups are compiled when
+# LOOKUP_LSEARCH is enabled. However, we retain these for backward
+# compatibility. Setting one forces LOOKUP_LSEARCH if it is not set.
+
+# LOOKUP_WILDLSEARCH=yes
+# LOOKUP_NWILDLSEARCH=yes
+
+
+#------------------------------------------------------------------------------
+# If you have set LOOKUP_LDAP=yes, you should set LDAP_LIB_TYPE to indicate
+# which LDAP library you have. Unfortunately, though most of their functions
+# are the same, there are minor differences. Currently Exim knows about four
+# LDAP libraries: the one from the University of Michigan (also known as
+# OpenLDAP 1), OpenLDAP 2, the Netscape SDK library, and the library that comes
+# with Solaris 7 onwards. Uncomment whichever of these you are using.
+
+# LDAP_LIB_TYPE=OPENLDAP1
+# LDAP_LIB_TYPE=OPENLDAP2
+# LDAP_LIB_TYPE=NETSCAPE
+# LDAP_LIB_TYPE=SOLARIS
+
+# If you don't set any of these, Exim assumes the original University of
+# Michigan (OpenLDAP 1) library.
+
+
+#------------------------------------------------------------------------------
+# The PCRE library is required for exim.  There is no longer an embedded
+# version of the PCRE library included with the source code, instead you
+# must use a system library or build your own copy of PCRE.
+# In either case you must specify the library link info here.  If the
+# PCRE header files are not in the standard search path you must also
+# modify the INCLUDE path (above)
+#
+# Use PCRE_CONFIG to query the pcre-config command (first found in $PATH)
+# to find the include files and libraries, else use PCRE_LIBS and set INCLUDE
+# too if needed.
+
+PCRE_CONFIG=yes
+# PCRE_LIBS=-lpcre
+
+
+#------------------------------------------------------------------------------
+# Additional libraries and include directories may be required for some
+# lookup styles (e.g. LDAP, MYSQL or PGSQL). LOOKUP_LIBS is included only on
+# the command for linking Exim itself, not on any auxiliary programs. You
+# don't need to set LOOKUP_INCLUDE if the relevant directories are already
+# specified in INCLUDE. The settings below are just examples; -lpq is for
+# PostgreSQL, -lgds is for Interbase, -lsqlite3 is for SQLite.
+#
+# You do not need to use this for any lookup information added via pkg-config.
+
+# LOOKUP_INCLUDE=-I /usr/local/ldap/include -I /usr/local/mysql/include -I /usr/local/pgsql/include
+# LOOKUP_LIBS=-L/usr/local/lib -lldap -llber -lmysqlclient -lpq -lgds -lsqlite3
+
+
+#------------------------------------------------------------------------------
+# Compiling the Exim monitor: If you want to compile the Exim monitor, a
+# program that requires an X11 display, then EXIM_MONITOR should be set to the
+# value "eximon.bin". Comment out this setting to disable compilation of the
+# monitor. The locations of various X11 directories for libraries and include
+# files are defaulted in the OS/Makefile-Default file, but can be overridden in
+# local OS-specific make files.
+
+EXIM_MONITOR=eximon.bin
+
+
+#------------------------------------------------------------------------------
+# Compiling Exim with content scanning support: If you want to compile Exim
+# with support for message body content scanning, set WITH_CONTENT_SCAN to
+# the value "yes". This will give you malware and spam scanning in the DATA ACL,
+# and the MIME ACL. Please read the documentation to learn more about these
+# features.
+
+# WITH_CONTENT_SCAN=yes
+
+# If you want to use the deprecated "demime" condition in the DATA ACL,
+# uncomment the line below. Doing so will also explicitly turn on the
+# WITH_CONTENT_SCAN option. If possible, use the MIME ACL instead of
+# the "demime" condition.
+
+# WITH_OLD_DEMIME=yes
+
+# If you're using ClamAV and are backporting fixes to an old version, instead
+# of staying current (which is the more usual approach) then you may need to
+# use an older API which uses a STREAM command, now deprecated, instead of
+# zINSTREAM.  If you need to set this, please let the Exim developers know, as
+# if nobody reports a need for it, we'll remove this option and clean up the
+# code.  zINSTREAM was introduced with ClamAV 0.95.
+#
+# WITH_OLD_CLAMAV_STREAM=yes
+
+#------------------------------------------------------------------------------
+# By default Exim includes code to support DKIM (DomainKeys Identified
+# Mail, RFC4871) signing and verification.  Verification of signatures is
+# turned on by default.  See the spec for information on conditionally
+# disabling it.  To disable the inclusion of the entire feature, set
+# DISABLE_DKIM to "yes"
+
+# DISABLE_DKIM=yes
+
+#------------------------------------------------------------------------------
+# Uncomment the following line to remove Per-Recipient-Data-Response support.
+
+# DISABLE_PRDR=yes
+
+#------------------------------------------------------------------------------
+# Uncomment the following line to remove OCSP stapling support in TLS,
+# from Exim.  Note it can only be supported when built with
+# GnuTLS 3.1.3 or later, or OpenSSL
+
+# DISABLE_OCSP=yes
+
+#------------------------------------------------------------------------------
+# By default, Exim has support for checking the AD bit in a DNS response, to
+# determine if DNSSEC validation was successful.  If your system libraries
+# do not support that bit, then set DISABLE_DNSSEC to "yes"
+
+# DISABLE_DNSSEC=yes
+
+
+#------------------------------------------------------------------------------
+# Compiling Exim with experimental features. These are documented in
+# experimental-spec.txt. "Experimental" means that the way these features are
+# implemented may still change. Backward compatibility is not guaranteed.
+
+# Uncomment the following line to add support for talking to dccifd.  This
+# defaults the socket path to /usr/local/dcc/var/dccifd.
+
+# EXPERIMENTAL_DCC=yes
+
+# Uncomment the following lines to add SPF support. You need to have libspf2
+# installed on your system (www.libspf2.org). Depending on where it is installed
+# you may have to edit the CFLAGS and LDFLAGS lines.
+
+# EXPERIMENTAL_SPF=yes
+# CFLAGS  += -I/usr/local/include
+# LDFLAGS += -lspf2
+
+# Uncomment the following lines to add SRS (Sender rewriting scheme) support.
+# You need to have libsrs_alt installed on your system (srs.mirtol.com).
+# Depending on where it is installed you may have to edit the CFLAGS and
+# LDFLAGS lines.
+
+# EXPERIMENTAL_SRS=yes
+# CFLAGS  += -I/usr/local/include
+# LDFLAGS += -lsrs_alt
+
+# Uncomment the following lines to add Brightmail AntiSpam support. You need
+# to have the Brightmail client SDK installed. Please check the experimental
+# documentation for implementation details. You need to edit the CFLAGS and
+# LDFLAGS lines.
+
+# EXPERIMENTAL_BRIGHTMAIL=yes
+# CFLAGS  += -I/opt/brightmail/bsdk-6.0/include
+# LDFLAGS += -lxml2_single -lbmiclient_single -L/opt/brightmail/bsdk-6.0/lib
+
+# Uncomment the following line to add DMARC checking capability, implemented
+# using libopendmarc libraries.
+# EXPERIMENTAL_DMARC=yes
+# CFLAGS += -I/usr/local/include
+# LDFLAGS += -lopendmarc
+
+
+# Uncomment the following line to support Transport post-delivery actions,
+# eg. for logging to a database.
+# EXPERIMENTAL_TPDA=yes
+
+# Uncomment the following line to add Redis lookup support
+# You need to have hiredis installed on your system (https://github.com/redis/hiredis).
+# Depending on where it is installed you may have to edit the CFLAGS and LDFLAGS lines.
+# EXPERIMENTAL_REDIS=yes
+# CFLAGS += -I/usr/local/include
+# LDFLAGS += -lhiredis
+
+# Uncomment the following line to enable Experimental Proxy Protocol
+# EXPERIMENTAL_PROXY=yes
+
+# Uncomment the following line to enable support for checking certiticate
+# ownership
+# EXPERIMENTAL_CERTNAMES=yes
+
+# Uncomment the following line to add DSN support
+# EXPERIMENTAL_DSN=yes
+
+###############################################################################
+#                 THESE ARE THINGS YOU MIGHT WANT TO SPECIFY                  #
+###############################################################################
+
+# The items in this section are those that are commonly changed according to
+# the sysadmin's preferences, but whose defaults are often acceptable. The
+# first five are concerned with security issues, where differing levels of
+# paranoia are appropriate in different environments. Sysadmins also vary in
+# their views on appropriate levels of defence in these areas. If you do not
+# understand these issues, go with the defaults, which are used by many sites.
+
+
+#------------------------------------------------------------------------------
+# Although Exim is normally a setuid program, owned by root, it refuses to run
+# local deliveries as root by default. There is a runtime option called
+# "never_users" which lists the users that must never be used for local
+# deliveries. There is also the setting below, which provides a list that
+# cannot be overridden at runtime. This guards against problems caused by
+# unauthorized changes to the runtime configuration. You are advised not to
+# remove "root" from this option, but you can add other users if you want. The
+# list is colon-separated. It must NOT contain any spaces.
+
+# FIXED_NEVER_USERS=root:bin:daemon
+FIXED_NEVER_USERS=root
+
+
+#------------------------------------------------------------------------------
+# By default, Exim insists that its configuration file be owned by root. You
+# can specify one additional permitted owner here.
+
+# CONFIGURE_OWNER=
+
+# If the configuration file is group-writeable, Exim insists by default that it
+# is owned by root. You can specify one additional permitted group owner here.
+
+# CONFIGURE_GROUP=
+
+# If you specify CONFIGURE_OWNER or CONFIGURE_GROUP as a name, this is looked
+# up at build time, and the uid or gid number is built into the binary.
+# However, you can specify that the lookup is deferred until runtime. In this
+# case, it is the name that is built into the binary. You can do this by a
+# setting of the form:
+
+# CONFIGURE_OWNER=ref:mail
+# CONFIGURE_GROUP=ref:sysadmin
+
+# In other words, put "ref:" in front of the user or group name. Although this
+# costs a bit of resource at runtime, it is convenient to use this feature when
+# building binaries that are to be run on multiple systems where the names may
+# refer to different uids or gids. It also allows you to build Exim on a system
+# where the relevant user or group is not defined.
+
+
+#------------------------------------------------------------------------------
+# The -C option allows Exim to be run with an alternate runtime configuration
+# file. When this is used by root, root privilege is retained by the binary
+# (for any other caller including the Exim user, it is dropped). You can
+# restrict the location of alternate configurations by defining a prefix below.
+# Any file used with -C must then start with this prefix (except that /dev/null
+# is also permitted if the caller is root, because that is used in the install
+# script). If the prefix specifies a directory that is owned by root, a
+# compromise of the Exim account does not permit arbitrary alternate
+# configurations to be used. The prefix can be more restrictive than just a
+# directory (the second example).
+
+# ALT_CONFIG_PREFIX=/some/directory/
+# ALT_CONFIG_PREFIX=/some/directory/exim.conf-
+
+
+#------------------------------------------------------------------------------
+# When a user other than root uses the -C option to override the configuration
+# file (including the Exim user when re-executing Exim to regain root
+# privileges for local message delivery), this will normally cause Exim to
+# drop root privileges. The TRUSTED_CONFIG_LIST option, specifies a file which
+# contains a list of trusted configuration filenames, one per line. If the -C
+# option is used by the Exim user or by the user specified in the
+# CONFIGURE_OWNER setting, to specify a configuration file which is listed in
+# the TRUSTED_CONFIG_LIST file, then root privileges are not dropped by Exim.
+
+# TRUSTED_CONFIG_LIST=/usr/exim/trusted_configs
+
+
+#------------------------------------------------------------------------------
+# Uncommenting this option disables the use of the -D command line option,
+# which changes the values of macros in the runtime configuration file.
+# This is another protection against somebody breaking into the Exim account.
+
+# DISABLE_D_OPTION=yes
+
+
+#------------------------------------------------------------------------------
+# By contrast, you might be maintaining a system which relies upon the ability
+# to override values with -D and assumes that these will be passed through to
+# the delivery processes.  As of Exim 4.73, this is no longer the case by
+# default.  Going forward, we strongly recommend that you use a shim Exim
+# configuration file owned by root stored under TRUSTED_CONFIG_LIST.
+# That shim can set macros before .include'ing your main configuration file.
+#
+# As a strictly transient measure to ease migration to 4.73, the
+# WHITELIST_D_MACROS value definies a colon-separated list of macro-names
+# which are permitted to be overridden from the command-line which will be
+# honoured by the Exim user.  So these are macros that can persist to delivery
+# time.
+# Examples might be -DTLS or -DSPOOL=/some/dir.  The values on the
+# command-line are filtered to only permit: [A-Za-z0-9_/.-]*
+#
+# This option is highly likely to be removed in a future release.  It exists
+# only to make 4.73 as easy as possible to migrate to.  If you use it, we
+# encourage you to schedule time to rework your configuration to not depend
+# upon it.  Most people should not need to use this.
+#
+# By default, no macros are whitelisted for -D usage.
+
+# WHITELIST_D_MACROS=TLS:SPOOL
+
+#------------------------------------------------------------------------------
+# Exim has support for the AUTH (authentication) extension of the SMTP
+# protocol, as defined by RFC 2554. If you don't know what SMTP authentication
+# is, you probably won't want to include this code, so you should leave these
+# settings commented out. If you do want to make use of SMTP authentication,
+# you must uncomment at least one of the following, so that appropriate code is
+# included in the Exim binary. You will then need to set up the run time
+# configuration to make use of the mechanism(s) selected.
+
+# AUTH_CRAM_MD5=yes
+# AUTH_CYRUS_SASL=yes
+# AUTH_DOVECOT=yes
+# AUTH_GSASL=yes
+# AUTH_GSASL_PC=libgsasl
+# AUTH_HEIMDAL_GSSAPI=yes
+# AUTH_HEIMDAL_GSSAPI_PC=heimdal-gssapi
+# AUTH_PLAINTEXT=yes
+# AUTH_SPA=yes
+
+
+#------------------------------------------------------------------------------
+# If you specified AUTH_CYRUS_SASL above, you should ensure that you have the
+# Cyrus SASL library installed before trying to build Exim, and you probably
+# want to uncomment the first line below.
+# Similarly for GNU SASL, unless pkg-config is used via AUTH_GSASL_PC.
+# Ditto for AUTH_HEIMDAL_GSSAPI(_PC).
+
+# AUTH_LIBS=-lsasl2
+# AUTH_LIBS=-lgsasl
+# AUTH_LIBS=-lgssapi -lheimntlm -lkrb5 -lhx509 -lcom_err -lhcrypto -lasn1 -lwind -lroken -lcrypt
+
+
+#------------------------------------------------------------------------------
+# When Exim is decoding MIME "words" in header lines, most commonly for use
+# in the $header_xxx expansion, it converts any foreign character sets to the
+# one that is set in the headers_charset option. The default setting is
+# defined by this setting:
+
+HEADERS_CHARSET="ISO-8859-1"
+
+# If you are going to make use of $header_xxx expansions in your configuration
+# file, or if your users are going to use them in filter files, and the normal
+# character set on your host is something other than ISO-8859-1, you might
+# like to specify a different default here. This value can be overridden in
+# the runtime configuration, and it can also be overridden in individual filter
+# files.
+#
+# IMPORTANT NOTE: The iconv() function is needed for character code
+# conversions. Please see the next item...
+
+
+#------------------------------------------------------------------------------
+# Character code conversions are possible only if the iconv() function is
+# installed on your operating system. There are two places in Exim where this
+# is relevant: (a) The $header_xxx expansion (see the previous item), and (b)
+# the Sieve filter support. For those OS where iconv() is known to be installed
+# as standard, the file in OS/Makefile-xxxx contains
+#
+# HAVE_ICONV=yes
+#
+# If you are not using one of those systems, but have installed iconv(), you
+# need to uncomment that line above. In some cases, you may find that iconv()
+# and its header file are not in the default places. You might need to use
+# something like this:
+#
+# HAVE_ICONV=yes
+# CFLAGS=-O -I/usr/local/include
+# EXTRALIBS_EXIM=-L/usr/local/lib -liconv
+#
+# but of course there may need to be other things in CFLAGS and EXTRALIBS_EXIM
+# as well.
+
+
+#------------------------------------------------------------------------------
+# The passwords for user accounts are normally encrypted with the crypt()
+# function. Comparisons with encrypted passwords can be done using Exim's
+# "crypteq" expansion operator. (This is commonly used as part of the
+# configuration of an authenticator for use with SMTP AUTH.) At least one
+# operating system has an extended function called crypt16(), which uses up to
+# 16 characters of a password (the normal crypt() uses only the first 8). Exim
+# supports the use of crypt16() as well as crypt() but note the warning below.
+
+# You can always indicate a crypt16-encrypted password by preceding it with
+# "{crypt16}". If you want the default handling (without any preceding
+# indicator) to use crypt16(), uncomment the following line:
+
+# DEFAULT_CRYPT=crypt16
+
+# If you do that, you can still access the basic crypt() function by preceding
+# an encrypted password with "{crypt}". For more details, see the description
+# of the "crypteq" condition in the manual chapter on string expansions.
+
+# Some operating systems do not include a crypt16() function, so Exim has one
+# of its own, which it uses unless HAVE_CRYPT16 is defined. Normally, that will
+# be set in an OS-specific Makefile for the OS that have such a function, so
+# you should not need to bother with it.
+
+# *** WARNING *** WARNING *** WARNING *** WARNING *** WARNING ***
+# It turns out that the above is not entirely accurate. As well as crypt16()
+# there is a function called bigcrypt() that some operating systems have. This
+# may or may not use the same algorithm, and both of them may be different to
+# Exim's built-in crypt16() that is used unless HAVE_CRYPT16 is defined.
+#
+# However, since there is now a move away from the traditional crypt()
+# functions towards using SHA1 and other algorithms, tidying up this area of
+# Exim is seen as very low priority. In practice, if you need to, you can
+# define DEFAULT_CRYPT to the name of any function that has the same interface
+# as the traditional crypt() function.
+# *** WARNING *** WARNING *** WARNING *** WARNING *** WARNING ***
+
+
+#------------------------------------------------------------------------------
+# Exim can be built to support the SMTP STARTTLS command, which implements
+# Transport Layer Security using SSL (Secure Sockets Layer). To do this, you
+# must install the OpenSSL library package or the GnuTLS library. Exim contains
+# no cryptographic code of its own. Uncomment the following lines if you want
+# to build Exim with TLS support. If you don't know what this is all about,
+# leave these settings commented out.
+
+# This setting is required for any TLS support (either OpenSSL or GnuTLS)
+# SUPPORT_TLS=yes
+
+# Uncomment one of these settings if you are using OpenSSL; pkg-config vs not
+# USE_OPENSSL_PC=openssl
+# TLS_LIBS=-lssl -lcrypto
+
+# Uncomment the first and either the second or the third of these if you
+# are using GnuTLS.  If you have pkg-config, then the second, else the third.
+# USE_GNUTLS=yes
+# USE_GNUTLS_PC=gnutls
+# TLS_LIBS=-lgnutls -ltasn1 -lgcrypt
+
+# The security fix we provide with the gnutls_allow_auto_pkcs11 option
+# (4.82 PP/09) introduces a compatibility regression.  The symbol is
+# not available if GnuTLS is build without p11-kit (--without-p11-kit
+# configure option).  In this case use AVOID_GNUTLS_PKCS11=yes when
+# building Exim.
+# AVOID_GNUTLS_PKCS11=yes
+
+# If you are running Exim as a server, note that just building it with TLS
+# support is not all you need to do. You also need to set up a suitable
+# certificate, and tell Exim about it by means of the tls_certificate
+# and tls_privatekey run time options. You also need to set tls_advertise_hosts
+# to specify the hosts to which Exim advertises TLS support. On the other hand,
+# if you are running Exim only as a client, building it with TLS support
+# is all you need to do.
+
+# If you are using pkg-config then you should not need to worry where the
+# libraries and headers are installed, as the pkg-config .pc specification
+# should include all -L/-I information necessary.  If not using pkg-config
+# then you might need to specify the locations too.
+
+# Additional libraries and include files are required for both OpenSSL and
+# GnuTLS. The TLS_LIBS settings above assume that the libraries are installed
+# with all your other libraries. If they are in a special directory, you may
+# need something like
+
+# TLS_LIBS=-L/usr/local/openssl/lib -lssl -lcrypto
+# or
+# TLS_LIBS=-L/opt/gnu/lib -lgnutls -ltasn1 -lgcrypt
+
+# TLS_LIBS is included only on the command for linking Exim itself, not on any
+# auxiliary programs. If the include files are not in a standard place, you can
+# set TLS_INCLUDE to specify where they are, for example:
+
+# TLS_INCLUDE=-I/usr/local/openssl/include/
+# or
+# TLS_INCLUDE=-I/opt/gnu/include
+
+# You don't need to set TLS_INCLUDE if the relevant directories are already
+# specified in INCLUDE.
+
+
+#------------------------------------------------------------------------------
+# The default distribution of Exim contains only the plain text form of the
+# documentation. Other forms are available separately. If you want to install
+# the documentation in "info" format, first fetch the Texinfo documentation
+# sources from the ftp directory and unpack them, which should create files
+# with the extension "texinfo" in the doc directory. You may find that the
+# version number of the texinfo files is different to your Exim version number,
+# because the main documentation isn't updated as often as the code. For
+# example, if you have Exim version 4.43, the source tarball upacks into a
+# directory called exim-4.43, but the texinfo tarball unpacks into exim-4.40.
+# In this case, move the contents of exim-4.40/doc into exim-4.43/doc after you
+# have unpacked them. Then set INFO_DIRECTORY to the location of your info
+# directory. This varies from system to system, but is often /usr/share/info.
+# Once you have done this, "make install" will build the info files and
+# install them in the directory you have defined.
+
+# INFO_DIRECTORY=/usr/share/info
+
+
+#------------------------------------------------------------------------------
+# Exim log directory and files: Exim creates several log files inside a
+# single log directory. You can define the directory and the form of the
+# log file name here. If you do not set anything, Exim creates a directory
+# called "log" inside its spool directory (see SPOOL_DIRECTORY above) and uses
+# the filenames "mainlog", "paniclog", and "rejectlog". If you want to change
+# this, you can set LOG_FILE_PATH to a path name containing one occurrence of
+# %s. This will be replaced by one of the strings "main", "panic", or "reject"
+# to form the final file names. Some installations may want something like this:
+
+# LOG_FILE_PATH=/var/log/exim_%slog
+
+# which results in files with names /var/log/exim_mainlog, etc. The directory
+# in which the log files are placed must exist; Exim does not try to create
+# it for itself. It is also your responsibility to ensure that Exim is capable
+# of writing files using this path name. The Exim user (see EXIM_USER above)
+# must be able to create and update files in the directory you have specified.
+
+# You can also configure Exim to use syslog, instead of or as well as log
+# files, by settings such as these
+
+# LOG_FILE_PATH=syslog
+# LOG_FILE_PATH=syslog:/var/log/exim_%slog
+
+# The first of these uses only syslog; the second uses syslog and also writes
+# to log files. Do not include white space in such a setting as it messes up
+# the building process.
+
+
+#------------------------------------------------------------------------------
+# When logging to syslog, the following option caters for syslog replacements
+# that are able to accept log entries longer than the 1024 characters allowed
+# by RFC 3164. It is up to you to make sure your syslog daemon can handle this.
+# Non-printable characters are usually unacceptable regardless, so log entries
+# are still split on newline characters.
+
+# SYSLOG_LONG_LINES=yes
+
+# If you are not interested in the process identifier (pid) of the Exim that is
+# making the call to syslog, then comment out the following line.
+
+SYSLOG_LOG_PID=yes
+
+
+#------------------------------------------------------------------------------
+# Cycling log files: this variable specifies the maximum number of old
+# log files that are kept by the exicyclog log-cycling script. You don't have
+# to use exicyclog. If your operating system has other ways of cycling log
+# files, you can use them instead. The exicyclog script isn't run by default;
+# you have to set up a cron job for it if you want it.
+
+EXICYCLOG_MAX=10
+
+
+#------------------------------------------------------------------------------
+# The compress command is used by the exicyclog script to compress old log
+# files. Both the name of the command and the suffix that it adds to files
+# need to be defined here. See also the EXICYCLOG_MAX configuration.
+
+COMPRESS_COMMAND=/usr/bin/gzip
+COMPRESS_SUFFIX=gz
+
+
+#------------------------------------------------------------------------------
+# If the exigrep utility is fed compressed log files, it tries to uncompress
+# them using this command.
+
+ZCAT_COMMAND=/usr/bin/zcat
+
+
+#------------------------------------------------------------------------------
+# Compiling in support for embedded Perl: If you want to be able to
+# use Perl code in Exim's string manipulation language and you have Perl
+# (version 5.004 or later) installed, set EXIM_PERL to perl.o. Using embedded
+# Perl costs quite a lot of resources. Only do this if you really need it.
+
+# EXIM_PERL=perl.o
+
+
+#------------------------------------------------------------------------------
+# Support for dynamically-loaded string expansion functions via ${dlfunc. If
+# you are using gcc the dynamically-loaded object must be compiled with the
+# -shared option, and you will need to add -export-dynamic to EXTRALIBS so
+# that the local_scan API is made available by the linker. You may also need
+# to add -ldl to EXTRALIBS so that dlopen() is available to Exim.
+
+# EXPAND_DLFUNC=yes
+
+
+#------------------------------------------------------------------------------
+# Exim has support for PAM (Pluggable Authentication Modules), a facility
+# which is available in the latest releases of Solaris and in some GNU/Linux
+# distributions (see http://ftp.kernel.org/pub/linux/libs/pam/). The Exim
+# support, which is intended for use in conjunction with the SMTP AUTH
+# facilities, is included only when requested by the following setting:
+
+# SUPPORT_PAM=yes
+
+# You probably need to add -lpam to EXTRALIBS, and in some releases of
+# GNU/Linux -ldl is also needed.
+
+
+#------------------------------------------------------------------------------
+# Support for authentication via Radius is also available. The Exim support,
+# which is intended for use in conjunction with the SMTP AUTH facilities,
+# is included only when requested by setting the following parameter to the
+# location of your Radius configuration file:
+
+# RADIUS_CONFIG_FILE=/etc/radiusclient/radiusclient.conf
+# RADIUS_CONFIG_FILE=/etc/radius.conf
+
+# If you have set RADIUS_CONFIG_FILE, you should also set one of these to
+# indicate which RADIUS library is used:
+
+# RADIUS_LIB_TYPE=RADIUSCLIENT
+# RADIUS_LIB_TYPE=RADIUSCLIENTNEW
+# RADIUS_LIB_TYPE=RADLIB
+
+# RADIUSCLIENT is the radiusclient library; you probably need to add
+#   -lradiusclient to EXTRALIBS.
+#
+# The API for the radiusclient library was changed at release 0.4.0.
+# Unfortunately, the header file does not define a version number that clients
+# can use to support both the old and new APIs. If you are using version 0.4.0
+# or later of the radiusclient library, you should use RADIUSCLIENTNEW.
+#
+# RADLIB is the Radius library that comes with FreeBSD (the header file is
+#   called radlib.h); you probably need to add -lradius to EXTRALIBS.
+#
+# If you do not set RADIUS_LIB_TYPE, Exim assumes the radiusclient library,
+# using the original API.
+
+
+#------------------------------------------------------------------------------
+# Support for authentication via the Cyrus SASL pwcheck daemon is available.
+# Note, however, that pwcheck is now deprecated in favour of saslauthd (see
+# next item). The Exim support for pwcheck, which is intented for use in
+# conjunction with the SMTP AUTH facilities, is included only when requested by
+# setting the following parameter to the location of the pwcheck daemon's
+# socket.
+#
+# There is no need to install all of SASL on your system. You just need to run
+# ./configure --with-pwcheck, cd to the pwcheck directory within the sources,
+# make and make install. You must create the socket directory (default
+# /var/pwcheck) and chown it to exim's user and group. Once you have installed
+# pwcheck, you should arrange for it to be started by root at boot time.
+
+# CYRUS_PWCHECK_SOCKET=/var/pwcheck/pwcheck
+
+
+#------------------------------------------------------------------------------
+# Support for authentication via the Cyrus SASL saslauthd daemon is available.
+# The Exim support, which is intented for use in conjunction with the SMTP AUTH
+# facilities, is included only when requested by setting the following
+# parameter to the location of the saslauthd daemon's socket.
+#
+# There is no need to install all of SASL on your system. You just need to run
+# ./configure --with-saslauthd (and any other options you need, for example, to
+# select or deselect authentication mechanisms), cd to the saslauthd directory
+# within the sources, make and make install. You must create the socket
+# directory (default /var/state/saslauthd) and chown it to exim's user and
+# group. Once you have installed saslauthd, you should arrange for it to be
+# started by root at boot time.
+
+# CYRUS_SASLAUTHD_SOCKET=/var/state/saslauthd/mux
+
+
+#------------------------------------------------------------------------------
+# TCP wrappers: If you want to use tcpwrappers from within Exim, uncomment
+# this setting. See the manual section entitled "Use of tcpwrappers" in the
+# chapter on building and installing Exim.
+#
+# USE_TCP_WRAPPERS=yes
+#
+# You may well also have to specify a local "include" file and an additional
+# library for TCP wrappers, so you probably need something like this:
+#
+# USE_TCP_WRAPPERS=yes
+# CFLAGS=-O -I/usr/local/include
+# EXTRALIBS_EXIM=-L/usr/local/lib -lwrap
+#
+# but of course there may need to be other things in CFLAGS and EXTRALIBS_EXIM
+# as well.
+#
+# To use a name other than exim in the tcpwrappers config file,
+# e.g. if you're running multiple daemons with different access lists,
+# or multiple MTAs with the same access list, define
+# TCP_WRAPPERS_DAEMON_NAME accordingly
+#
+# TCP_WRAPPERS_DAEMON_NAME="exim"
+
+
+#------------------------------------------------------------------------------
+# The default action of the exim_install script (which is run by "make
+# install") is to install the Exim binary with a unique name such as
+# exim-4.43-1, and then set up a symbolic link called "exim" to reference it,
+# moving the symbolic link from any previous version. If you define NO_SYMLINK
+# (the value doesn't matter), the symbolic link is not created or moved. You
+# will then have to "turn Exim on" by setting up the link manually.
+
+# NO_SYMLINK=yes
+
+
+#------------------------------------------------------------------------------
+# Another default action of the install script is to install a default runtime
+# configuration file if one does not exist. This configuration has a router for
+# expanding system aliases. The default assumes that these aliases are kept
+# in the traditional file called /etc/aliases. If such a file does not exist,
+# the installation script creates one that contains just comments (no actual
+# aliases). The following setting can be changed to specify a different
+# location for the system alias file.
+
+SYSTEM_ALIASES_FILE=/etc/aliases
+
+
+#------------------------------------------------------------------------------
+# There are some testing options (-be, -bt, -bv) that read data from the
+# standard input when no arguments are supplied. By default, the input lines
+# are read using the standard fgets() function. This does not support line
+# editing during interactive input (though the terminal's "erase" character
+# works as normal). If your operating system has the readline() function, and
+# in addition supports dynamic loading of library functions, you can cause
+# Exim to use readline() for the -be testing option (only) by uncommenting the
+# following setting. Dynamic loading is used so that the library is loaded only
+# when the -be testing option is given; by the time the loading occurs,
+# Exim has given up its root privilege and is running as the calling user. This
+# is the reason why readline() is NOT supported for -bt and -bv, because Exim
+# runs as root or as exim, respectively, for those options. When USE_READLINE
+# is "yes", as well as supporting line editing, a history of input lines in the
+# current run is maintained.
+
+# USE_READLINE=yes
+
+# You may need to add -ldl to EXTRALIBS when you set USE_READLINE=yes.
+# Note that this option adds to the size of the Exim binary, because the
+# dynamic loading library is not otherwise included.
+
+
+#------------------------------------------------------------------------------
+# Uncomment this setting to include IPv6 support.
+
+# HAVE_IPV6=yes
+
+###############################################################################
+#              THINGS YOU ALMOST NEVER NEED TO MENTION                        #
+###############################################################################
+
+# The settings in this section are available for use in special circumstances.
+# In the vast majority of installations you need not change anything below.
+
+
+#------------------------------------------------------------------------------
+# The following commands live in different places in some OS. Either the
+# ultimate default settings, or the OS-specific files should already point to
+# the right place, but they can be overridden here if necessary. These settings
+# are used when building various scripts to ensure that the correct paths are
+# used when the scripts are run. They are not used in the Makefile itself. Perl
+# is not necessary for running Exim unless you set EXIM_PERL (see above) to get
+# it embedded, but there are some utilities that are Perl scripts. If you
+# haven't got Perl, Exim will still build and run; you just won't be able to
+# use those utilities.
+
+# CHOWN_COMMAND=/usr/bin/chown
+# CHGRP_COMMAND=/usr/bin/chgrp
+# CHMOD_COMMAND=/usr/bin/chmod
+# MV_COMMAND=/bin/mv
+# RM_COMMAND=/bin/rm
+# TOUCH_COMMAND=/usr/bin/touch
+# PERL_COMMAND=/usr/bin/perl
+
+
+#------------------------------------------------------------------------------
+# The following macro can be used to change the command for building a library
+# of functions. By default the "ar" command is used, with options "cq".
+# Only in rare circumstances should you need to change this.
+
+# AR=ar cq
+
+
+#------------------------------------------------------------------------------
+# In some operating systems, the value of the TMPDIR environment variable
+# controls where temporary files are created. Exim does not make use of
+# temporary files, except when delivering to MBX mailboxes. However, if Exim
+# calls any external libraries (e.g. DBM libraries), they may use temporary
+# files, and thus be influenced by the value of TMPDIR. For this reason, when
+# Exim starts, it checks the environment for TMPDIR, and if it finds it is set,
+# it replaces the value with what is defined here. Commenting this setting
+# suppresses the check altogether.
+
+TMPDIR="/tmp"
+
+
+#------------------------------------------------------------------------------
+# The following macros can be used to change the default modes that are used
+# by the appendfile transport. In most installations the defaults are just
+# fine, and in any case, you can change particular instances of the transport
+# at run time if you want.
+
+# APPENDFILE_MODE=0600
+# APPENDFILE_DIRECTORY_MODE=0700
+# APPENDFILE_LOCKFILE_MODE=0600
+
+
+#------------------------------------------------------------------------------
+# In some installations there may be multiple machines sharing file systems,
+# where a different configuration file is required for Exim on the different
+# machines. If CONFIGURE_FILE_USE_NODE is defined, then Exim will first look
+# for a configuration file whose name is that defined by CONFIGURE_FILE,
+# with the node name obtained by uname() tacked on the end, separated by a
+# period (for example, /usr/exim/configure.host.in.some.domain). If this file
+# does not exist, then the bare configuration file name is tried.
+
+# CONFIGURE_FILE_USE_NODE=yes
+
+
+#------------------------------------------------------------------------------
+# In some esoteric configurations two different versions of Exim are run,
+# with different setuid values, and different configuration files are required
+# to handle the different cases. If CONFIGURE_FILE_USE_EUID is defined, then
+# Exim will first look for a configuration file whose name is that defined
+# by CONFIGURE_FILE, with the effective uid tacked on the end, separated by
+# a period (for eximple, /usr/exim/configure.0). If this file does not exist,
+# then the bare configuration file name is tried. In the case when both
+# CONFIGURE_FILE_USE_EUID and CONFIGURE_FILE_USE_NODE are set, four files
+# are tried: <name>.<euid>.<node>, <name>.<node>, <name>.<euid>, and <name>.
+
+# CONFIGURE_FILE_USE_EUID=yes
+
+
+#------------------------------------------------------------------------------
+# The size of the delivery buffers: These specify the sizes (in bytes) of
+# the buffers that are used when copying a message from the spool to a
+# destination. There is rarely any need to change these values.
+
+# DELIVER_IN_BUFFER_SIZE=8192
+# DELIVER_OUT_BUFFER_SIZE=8192
+
+
+#------------------------------------------------------------------------------
+# The mode of the database directory: Exim creates a directory called "db"
+# in its spool directory, to hold its databases of hints. This variable
+# determines the mode of the created directory. The default value in the
+# source is 0750.
+
+# EXIMDB_DIRECTORY_MODE=0750
+
+
+#------------------------------------------------------------------------------
+# Database file mode: The mode of files created in the "db" directory defaults
+# to 0640 in the source, and can be changed here.
+
+# EXIMDB_MODE=0640
+
+
+#------------------------------------------------------------------------------
+# Database lock file mode: The mode of zero-length files created in the "db"
+# directory to use for locking purposes defaults to 0640 in the source, and
+# can be changed here.
+
+# EXIMDB_LOCKFILE_MODE=0640
+
+
+#------------------------------------------------------------------------------
+# This parameter sets the maximum length of the header portion of a message
+# that Exim is prepared to process. The default setting is one megabyte. The
+# limit exists in order to catch rogue mailers that might connect to your SMTP
+# port, start off a header line, and then just pump junk at it for ever. The
+# message_size_limit option would also catch this, but it may not be set.
+# The value set here is the default; it can be changed at runtime.
+
+# HEADER_MAXSIZE="(1024*1024)"
+
+
+#------------------------------------------------------------------------------
+# The mode of the input directory: The input directory is where messages are
+# kept while awaiting delivery. Exim creates it if necessary, using a mode
+# which can be defined here (default 0750).
+
+# INPUT_DIRECTORY_MODE=0750
+
+
+#------------------------------------------------------------------------------
+# The mode of Exim's log directory, when it is created by Exim inside the spool
+# directory, defaults to 0750 but can be changed here.
+
+# LOG_DIRECTORY_MODE=0750
+
+
+#------------------------------------------------------------------------------
+# The log files themselves are created as required, with a mode that defaults
+# to 0640, but which can be changed here.
+
+# LOG_MODE=0640
+
+
+#------------------------------------------------------------------------------
+# The TESTDB lookup is for performing tests on the handling of lookup results,
+# and is not useful for general running. It should be included only when
+# debugging the code of Exim.
+
+# LOOKUP_TESTDB=yes
+
+
+#------------------------------------------------------------------------------
+# /bin/sh is used by default as the shell in which to run commands that are
+# defined in the makefiles. This can be changed if necessary, by uncommenting
+# this line and specifying another shell, but note that a Bourne-compatible
+# shell is expected.
+
+# MAKE_SHELL=/bin/sh
+
+
+#------------------------------------------------------------------------------
+# The maximum number of named lists of each type (address, domain, host, and
+# local part) can be increased by changing this value. It should be set to
+# a multiple of 16.
+
+# MAX_NAMED_LIST=16
+
+
+#------------------------------------------------------------------------------
+# Network interfaces: Unless you set the local_interfaces option in the runtime
+# configuration file to restrict Exim to certain interfaces only, it will run
+# code to find all the interfaces there are on your host. Unfortunately,
+# the call to the OS that does this requires a buffer large enough to hold
+# data for all the interfaces - it was designed in the days when a host rarely
+# had more than three or four interfaces. Nowadays hosts can have very many
+# virtual interfaces running on the same hardware. If you have more than 250
+# virtual interfaces, you will need to uncomment this setting and increase the
+# value.
+
+# MAXINTERFACES=250
+
+
+#------------------------------------------------------------------------------
+# Per-message logs: While a message is in the process of being delivered,
+# comments on its progress are written to a message log, for the benefit of
+# human administrators. These logs are held in a directory called "msglog"
+# in the spool directory. Its mode defaults to 0750, but can be changed here.
+# The message log directory is also used for storing files that are used by
+# transports for returning data to a message's sender (see the "return_output"
+# option for transports).
+
+# MSGLOG_DIRECTORY_MODE=0750
+
+
+#------------------------------------------------------------------------------
+# There are three options which are used when compiling the Perl interface and
+# when linking with Perl. The default values for these are placed automatically
+# at the head of the Makefile by the script which builds it. However, if you
+# want to override them, you can do so here.
+
+# PERL_CC=
+# PERL_CCOPTS=
+# PERL_LIBS=
+
+
+#------------------------------------------------------------------------------
+# If you wish to disable valgrind in the binary, define NVALGRIND=1.
+# This should not be needed.
+
+# NVALGRIND=1
+
+#------------------------------------------------------------------------------
+# Identifying the daemon: When an Exim daemon starts up, it writes its pid
+# (process id) to a file so that it can easily be identified. The path of the
+# file can be specified here. Some installations may want something like this:
+
+# PID_FILE_PATH=/var/lock/exim.pid
+
+# If PID_FILE_PATH is not defined, Exim writes a file in its spool directory
+# using the name "exim-daemon.pid".
+
+# If you start up a daemon without the -bd option (for example, with just
+# the -q15m option), a pid file is not written. Also, if you override the
+# configuration file with the -oX option, no pid file is written. In other
+# words, the pid file is written only for a "standard" daemon.
+
+
+#------------------------------------------------------------------------------
+# If Exim creates the spool directory, it is given this mode, defaulting in the
+# source to 0750.
+
+# SPOOL_DIRECTORY_MODE=0750
+
+
+#------------------------------------------------------------------------------
+# The mode of files on the input spool which hold the contents of messages can
+# be changed here. The default is 0640 so that information from the spool is
+# available to anyone who is a member of the Exim group.
+
+# SPOOL_MODE=0640
+
+
+#------------------------------------------------------------------------------
+# Moving frozen messages: If the following is uncommented, Exim is compiled
+# with support for automatically moving frozen messages out of the main spool
+# directory, a facility that is found useful by some large installations. A
+# run time option is required to cause the moving actually to occur. Such
+# messages become "invisible" to the normal management tools.
+
+# SUPPORT_MOVE_FROZEN_MESSAGES=yes
+
+
+#------------------------------------------------------------------------------
+# Expanding match_* second paramters: BE CAREFUL IF ENABLING THIS!
+# It has proven too easy in practice for administrators to configure security
+# problems into their Exim install, by treating match_domain{}{} and friends
+# as a form of string comparison, where the second string comes from untrusted
+# data. Because these options take lists, which can include lookup;LOOKUPDATA
+# style elements, a foe can then cause Exim to, eg, execute an arbitrary MySQL
+# query, dropping tables.
+# From Exim 4.77 onwards, the second parameter is not expanded; it can still
+# be a list literal, or a macro, or a named list reference.  There is also
+# the new expansion condition "inlisti" which does expand the second parameter,
+# but treats it as a list of strings; also, there's "eqi" which is probably
+# what is normally wanted.
+#
+# If you really need to have the old behaviour, know what you are doing and
+# will not complain if your system is compromised as a result of doing so, then
+# uncomment this option to get the old behaviour back.
+
+# EXPAND_LISTMATCH_RHS=yes
+
+#------------------------------------------------------------------------------
+# Disabling the use of fsync(): DO NOT UNCOMMENT THE FOLLOWING LINE unless you
+# really, really, really know what you are doing. And even then, think again.
+# You should never uncomment this when compiling a binary for distribution.
+# Use it only when compiling Exim for your own use.
+#
+# Uncommenting this line enables the use of a runtime option called
+# disable_fsync, which can be used to stop Exim using fsync() to ensure that
+# files are written to disc before proceeding. When this is disabled, crashes
+# and hardware problems such as power outages can cause data to be lost. This
+# feature should only be used in very exceptional circumstances. YOU HAVE BEEN
+# WARNED.
+
+# ENABLE_DISABLE_FSYNC=yes
+
+# End of EDITME for Exim 4.
diff --git a/src/acl.c b/src/acl.c
new file mode 100644 (file)
index 0000000..6e635fb
--- /dev/null
+++ b/src/acl.c
@@ -0,0 +1,4488 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2014 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Code for handling Access Control Lists (ACLs) */
+
+#include "exim.h"
+
+
+/* Default callout timeout */
+
+#define CALLOUT_TIMEOUT_DEFAULT 30
+
+/* ACL verb codes - keep in step with the table of verbs that follows */
+
+enum { ACL_ACCEPT, ACL_DEFER, ACL_DENY, ACL_DISCARD, ACL_DROP, ACL_REQUIRE,
+       ACL_WARN };
+
+/* ACL verbs */
+
+static uschar *verbs[] = {
+    US"accept",
+    US"defer",
+    US"deny",
+    US"discard",
+    US"drop",
+    US"require",
+    US"warn" };
+
+/* For each verb, the conditions for which "message" or "log_message" are used
+are held as a bitmap. This is to avoid expanding the strings unnecessarily. For
+"accept", the FAIL case is used only after "endpass", but that is selected in
+the code. */
+
+static int msgcond[] = {
+  (1<<OK) | (1<<FAIL) | (1<<FAIL_DROP),  /* accept */
+  (1<<OK),                               /* defer */
+  (1<<OK),                               /* deny */
+  (1<<OK) | (1<<FAIL) | (1<<FAIL_DROP),  /* discard */
+  (1<<OK),                               /* drop */
+  (1<<FAIL) | (1<<FAIL_DROP),            /* require */
+  (1<<OK)                                /* warn */
+  };
+
+/* ACL condition and modifier codes - keep in step with the table that
+follows, and the cond_expand_at_top and uschar cond_modifiers tables lower
+down. */
+
+enum { ACLC_ACL,
+       ACLC_ADD_HEADER,
+       ACLC_AUTHENTICATED,
+#ifdef EXPERIMENTAL_BRIGHTMAIL
+       ACLC_BMI_OPTIN,
+#endif
+       ACLC_CONDITION,
+       ACLC_CONTINUE,
+       ACLC_CONTROL,
+#ifdef EXPERIMENTAL_DCC
+       ACLC_DCC,
+#endif
+#ifdef WITH_CONTENT_SCAN
+       ACLC_DECODE,
+#endif
+       ACLC_DELAY,
+#ifdef WITH_OLD_DEMIME
+       ACLC_DEMIME,
+#endif
+#ifndef DISABLE_DKIM
+       ACLC_DKIM_SIGNER,
+       ACLC_DKIM_STATUS,
+#endif
+#ifdef EXPERIMENTAL_DMARC
+       ACLC_DMARC_STATUS,
+#endif
+       ACLC_DNSLISTS,
+       ACLC_DOMAINS,
+       ACLC_ENCRYPTED,
+       ACLC_ENDPASS,
+       ACLC_HOSTS,
+       ACLC_LOCAL_PARTS,
+       ACLC_LOG_MESSAGE,
+       ACLC_LOG_REJECT_TARGET,
+       ACLC_LOGWRITE,
+#ifdef WITH_CONTENT_SCAN
+       ACLC_MALWARE,
+#endif
+       ACLC_MESSAGE,
+#ifdef WITH_CONTENT_SCAN
+       ACLC_MIME_REGEX,
+#endif
+       ACLC_RATELIMIT,
+       ACLC_RECIPIENTS,
+#ifdef WITH_CONTENT_SCAN
+       ACLC_REGEX,
+#endif
+       ACLC_REMOVE_HEADER,
+       ACLC_SENDER_DOMAINS,
+       ACLC_SENDERS,
+       ACLC_SET,
+#ifdef WITH_CONTENT_SCAN
+       ACLC_SPAM,
+#endif
+#ifdef EXPERIMENTAL_SPF
+       ACLC_SPF,
+       ACLC_SPF_GUESS,
+#endif
+       ACLC_UDPSEND,
+       ACLC_VERIFY };
+
+/* ACL conditions/modifiers: "delay", "control", "continue", "endpass",
+"message", "log_message", "log_reject_target", "logwrite", and "set" are
+modifiers that look like conditions but always return TRUE. They are used for
+their side effects. */
+
+static uschar *conditions[] = {
+  US"acl",
+  US"add_header",
+  US"authenticated",
+#ifdef EXPERIMENTAL_BRIGHTMAIL
+  US"bmi_optin",
+#endif
+  US"condition",
+  US"continue",
+  US"control",
+#ifdef EXPERIMENTAL_DCC
+  US"dcc",
+#endif
+#ifdef WITH_CONTENT_SCAN
+  US"decode",
+#endif
+  US"delay",
+#ifdef WITH_OLD_DEMIME
+  US"demime",
+#endif
+#ifndef DISABLE_DKIM
+  US"dkim_signers",
+  US"dkim_status",
+#endif
+#ifdef EXPERIMENTAL_DMARC
+  US"dmarc_status",
+#endif
+  US"dnslists",
+  US"domains",
+  US"encrypted",
+  US"endpass",
+  US"hosts",
+  US"local_parts",
+  US"log_message",
+  US"log_reject_target",
+  US"logwrite",
+#ifdef WITH_CONTENT_SCAN
+  US"malware",
+#endif
+  US"message",
+#ifdef WITH_CONTENT_SCAN
+  US"mime_regex",
+#endif
+  US"ratelimit",
+  US"recipients",
+#ifdef WITH_CONTENT_SCAN
+  US"regex",
+#endif
+  US"remove_header",
+  US"sender_domains", US"senders", US"set",
+#ifdef WITH_CONTENT_SCAN
+  US"spam",
+#endif
+#ifdef EXPERIMENTAL_SPF
+  US"spf",
+  US"spf_guess",
+#endif
+  US"udpsend",
+  US"verify" };
+
+
+/* Return values from decode_control(); keep in step with the table of names
+that follows! */
+
+enum {
+  CONTROL_AUTH_UNADVERTISED,
+  #ifdef EXPERIMENTAL_BRIGHTMAIL
+  CONTROL_BMI_RUN,
+  #endif
+  CONTROL_DEBUG,
+  #ifndef DISABLE_DKIM
+  CONTROL_DKIM_VERIFY,
+  #endif
+  #ifdef EXPERIMENTAL_DMARC
+  CONTROL_DMARC_VERIFY,
+  CONTROL_DMARC_FORENSIC,
+  #endif
+  CONTROL_DSCP,
+  CONTROL_ERROR,
+  CONTROL_CASEFUL_LOCAL_PART,
+  CONTROL_CASELOWER_LOCAL_PART,
+  CONTROL_CUTTHROUGH_DELIVERY,
+  CONTROL_ENFORCE_SYNC,
+  CONTROL_NO_ENFORCE_SYNC,
+  CONTROL_FREEZE,
+  CONTROL_QUEUE_ONLY,
+  CONTROL_SUBMISSION,
+  CONTROL_SUPPRESS_LOCAL_FIXUPS,
+  #ifdef WITH_CONTENT_SCAN
+  CONTROL_NO_MBOX_UNSPOOL,
+  #endif
+  CONTROL_FAKEDEFER,
+  CONTROL_FAKEREJECT,
+  CONTROL_NO_MULTILINE,
+  CONTROL_NO_PIPELINING,
+  CONTROL_NO_DELAY_FLUSH,
+  CONTROL_NO_CALLOUT_FLUSH
+};
+
+/* ACL control names; keep in step with the table above! This list is used for
+turning ids into names. The actual list of recognized names is in the variable
+control_def controls_list[] below. The fact that there are two lists is a mess
+and should be tidied up. */
+
+static uschar *controls[] = {
+  US"allow_auth_unadvertised",
+  #ifdef EXPERIMENTAL_BRIGHTMAIL
+  US"bmi_run",
+  #endif
+  US"debug",
+  #ifndef DISABLE_DKIM
+  US"dkim_disable_verify",
+  #endif
+  #ifdef EXPERIMENTAL_DMARC
+  US"dmarc_disable_verify",
+  US"dmarc_enable_forensic",
+  #endif
+  US"dscp",
+  US"error",
+  US"caseful_local_part",
+  US"caselower_local_part",
+  US"cutthrough_delivery",
+  US"enforce_sync",
+  US"no_enforce_sync",
+  US"freeze",
+  US"queue_only",
+  US"submission",
+  US"suppress_local_fixups",
+  #ifdef WITH_CONTENT_SCAN
+  US"no_mbox_unspool",
+  #endif
+  US"fakedefer",
+  US"fakereject",
+  US"no_multiline_responses",
+  US"no_pipelining",
+  US"no_delay_flush",
+  US"no_callout_flush"
+};
+
+/* Flags to indicate for which conditions/modifiers a string expansion is done
+at the outer level. In the other cases, expansion already occurs in the
+checking functions. */
+
+static uschar cond_expand_at_top[] = {
+  FALSE,   /* acl */
+  TRUE,    /* add_header */
+  FALSE,   /* authenticated */
+#ifdef EXPERIMENTAL_BRIGHTMAIL
+  TRUE,    /* bmi_optin */
+#endif
+  TRUE,    /* condition */
+  TRUE,    /* continue */
+  TRUE,    /* control */
+#ifdef EXPERIMENTAL_DCC
+  TRUE,    /* dcc */
+#endif
+#ifdef WITH_CONTENT_SCAN
+  TRUE,    /* decode */
+#endif
+  TRUE,    /* delay */
+#ifdef WITH_OLD_DEMIME
+  TRUE,    /* demime */
+#endif
+#ifndef DISABLE_DKIM
+  TRUE,    /* dkim_signers */
+  TRUE,    /* dkim_status */
+#endif
+#ifdef EXPERIMENTAL_DMARC
+  TRUE,    /* dmarc_status */
+#endif
+  TRUE,    /* dnslists */
+  FALSE,   /* domains */
+  FALSE,   /* encrypted */
+  TRUE,    /* endpass */
+  FALSE,   /* hosts */
+  FALSE,   /* local_parts */
+  TRUE,    /* log_message */
+  TRUE,    /* log_reject_target */
+  TRUE,    /* logwrite */
+#ifdef WITH_CONTENT_SCAN
+  TRUE,    /* malware */
+#endif
+  TRUE,    /* message */
+#ifdef WITH_CONTENT_SCAN
+  TRUE,    /* mime_regex */
+#endif
+  TRUE,    /* ratelimit */
+  FALSE,   /* recipients */
+#ifdef WITH_CONTENT_SCAN
+  TRUE,    /* regex */
+#endif
+  TRUE,    /* remove_header */
+  FALSE,   /* sender_domains */
+  FALSE,   /* senders */
+  TRUE,    /* set */
+#ifdef WITH_CONTENT_SCAN
+  TRUE,    /* spam */
+#endif
+#ifdef EXPERIMENTAL_SPF
+  TRUE,    /* spf */
+  TRUE,    /* spf_guess */
+#endif
+  TRUE,    /* udpsend */
+  TRUE     /* verify */
+};
+
+/* Flags to identify the modifiers */
+
+static uschar cond_modifiers[] = {
+  FALSE,   /* acl */
+  TRUE,    /* add_header */
+  FALSE,   /* authenticated */
+#ifdef EXPERIMENTAL_BRIGHTMAIL
+  TRUE,    /* bmi_optin */
+#endif
+  FALSE,   /* condition */
+  TRUE,    /* continue */
+  TRUE,    /* control */
+#ifdef EXPERIMENTAL_DCC
+  FALSE,   /* dcc */
+#endif
+#ifdef WITH_CONTENT_SCAN
+  FALSE,   /* decode */
+#endif
+  TRUE,    /* delay */
+#ifdef WITH_OLD_DEMIME
+  FALSE,   /* demime */
+#endif
+#ifndef DISABLE_DKIM
+  FALSE,   /* dkim_signers */
+  FALSE,   /* dkim_status */
+#endif
+#ifdef EXPERIMENTAL_DMARC
+  FALSE,   /* dmarc_status */
+#endif
+  FALSE,   /* dnslists */
+  FALSE,   /* domains */
+  FALSE,   /* encrypted */
+  TRUE,    /* endpass */
+  FALSE,   /* hosts */
+  FALSE,   /* local_parts */
+  TRUE,    /* log_message */
+  TRUE,    /* log_reject_target */
+  TRUE,    /* logwrite */
+#ifdef WITH_CONTENT_SCAN
+  FALSE,   /* malware */
+#endif
+  TRUE,    /* message */
+#ifdef WITH_CONTENT_SCAN
+  FALSE,   /* mime_regex */
+#endif
+  FALSE,   /* ratelimit */
+  FALSE,   /* recipients */
+#ifdef WITH_CONTENT_SCAN
+  FALSE,   /* regex */
+#endif
+  TRUE,    /* remove_header */
+  FALSE,   /* sender_domains */
+  FALSE,   /* senders */
+  TRUE,    /* set */
+#ifdef WITH_CONTENT_SCAN
+  FALSE,   /* spam */
+#endif
+#ifdef EXPERIMENTAL_SPF
+  FALSE,   /* spf */
+  FALSE,   /* spf_guess */
+#endif
+  TRUE,    /* udpsend */
+  FALSE    /* verify */
+};
+
+/* Bit map vector of which conditions and modifiers are not allowed at certain
+times. For each condition and modifier, there's a bitmap of dis-allowed times.
+For some, it is easier to specify the negation of a small number of allowed
+times. */
+
+static unsigned int cond_forbids[] = {
+  0,                                               /* acl */
+
+  (unsigned int)
+  ~((1<<ACL_WHERE_MAIL)|(1<<ACL_WHERE_RCPT)|       /* add_header */
+    (1<<ACL_WHERE_PREDATA)|(1<<ACL_WHERE_DATA)|
+  #ifndef DISABLE_PRDR
+    (1<<ACL_WHERE_PRDR)|
+  #endif
+    (1<<ACL_WHERE_MIME)|(1<<ACL_WHERE_NOTSMTP)|
+    (1<<ACL_WHERE_DKIM)|
+    (1<<ACL_WHERE_NOTSMTP_START)),
+
+  (1<<ACL_WHERE_NOTSMTP)|                          /* authenticated */
+    (1<<ACL_WHERE_NOTSMTP_START)|
+    (1<<ACL_WHERE_CONNECT)|(1<<ACL_WHERE_HELO),
+
+  #ifdef EXPERIMENTAL_BRIGHTMAIL
+  (1<<ACL_WHERE_AUTH)|                             /* bmi_optin */
+    (1<<ACL_WHERE_CONNECT)|(1<<ACL_WHERE_HELO)|
+    (1<<ACL_WHERE_DATA)|(1<<ACL_WHERE_MIME)|
+  #ifndef DISABLE_PRDR
+    (1<<ACL_WHERE_PRDR)|
+  #endif
+    (1<<ACL_WHERE_ETRN)|(1<<ACL_WHERE_EXPN)|
+    (1<<ACL_WHERE_MAILAUTH)|
+    (1<<ACL_WHERE_MAIL)|(1<<ACL_WHERE_STARTTLS)|
+    (1<<ACL_WHERE_VRFY)|(1<<ACL_WHERE_PREDATA)|
+    (1<<ACL_WHERE_NOTSMTP_START),
+  #endif
+
+  0,                                               /* condition */
+
+  0,                                               /* continue */
+
+  /* Certain types of control are always allowed, so we let it through
+  always and check in the control processing itself. */
+
+  0,                                               /* control */
+
+  #ifdef EXPERIMENTAL_DCC
+  (unsigned int)
+  ~((1<<ACL_WHERE_DATA)|                           /* dcc */
+  #ifndef DISABLE_PRDR
+    (1<<ACL_WHERE_PRDR)|
+  #endif
+    (1<<ACL_WHERE_NOTSMTP)),
+  #endif
+
+  #ifdef WITH_CONTENT_SCAN
+  (unsigned int)
+  ~(1<<ACL_WHERE_MIME),                            /* decode */
+  #endif
+
+  (1<<ACL_WHERE_NOTQUIT),                          /* delay */
+
+  #ifdef WITH_OLD_DEMIME
+  (unsigned int)
+  ~((1<<ACL_WHERE_DATA)|                           /* demime */
+  #ifndef DISABLE_PRDR
+    (1<<ACL_WHERE_PRDR)|
+  #endif
+    (1<<ACL_WHERE_NOTSMTP)),
+  #endif
+
+  #ifndef DISABLE_DKIM
+  (unsigned int)
+  ~(1<<ACL_WHERE_DKIM),                            /* dkim_signers */
+
+  (unsigned int)
+  ~(1<<ACL_WHERE_DKIM),                            /* dkim_status */
+  #endif
+
+  #ifdef EXPERIMENTAL_DMARC
+  (unsigned int)
+  ~(1<<ACL_WHERE_DATA),                            /* dmarc_status */
+  #endif
+
+  (1<<ACL_WHERE_NOTSMTP)|                          /* dnslists */
+    (1<<ACL_WHERE_NOTSMTP_START),
+
+  (unsigned int)
+  ~((1<<ACL_WHERE_RCPT)                            /* domains */
+  #ifndef DISABLE_PRDR
+    |(1<<ACL_WHERE_PRDR)
+  #endif
+    ),
+
+  (1<<ACL_WHERE_NOTSMTP)|                          /* encrypted */
+    (1<<ACL_WHERE_CONNECT)|
+    (1<<ACL_WHERE_NOTSMTP_START)|
+    (1<<ACL_WHERE_HELO),
+
+  0,                                               /* endpass */
+
+  (1<<ACL_WHERE_NOTSMTP)|                          /* hosts */
+    (1<<ACL_WHERE_NOTSMTP_START),
+
+  (unsigned int)
+  ~((1<<ACL_WHERE_RCPT)                             /* local_parts */
+  #ifndef DISABLE_PRDR
+    |(1<<ACL_WHERE_PRDR)
+  #endif
+    ),
+
+  0,                                               /* log_message */
+
+  0,                                               /* log_reject_target */
+
+  0,                                               /* logwrite */
+
+  #ifdef WITH_CONTENT_SCAN
+  (unsigned int)
+  ~((1<<ACL_WHERE_DATA)|                           /* malware */
+  #ifndef DISABLE_PRDR
+    (1<<ACL_WHERE_PRDR)|
+  #endif
+    (1<<ACL_WHERE_NOTSMTP)),
+  #endif
+
+  0,                                               /* message */
+
+  #ifdef WITH_CONTENT_SCAN
+  (unsigned int)
+  ~(1<<ACL_WHERE_MIME),                            /* mime_regex */
+  #endif
+
+  0,                                               /* ratelimit */
+
+  (unsigned int)
+  ~(1<<ACL_WHERE_RCPT),                            /* recipients */
+
+  #ifdef WITH_CONTENT_SCAN
+  (unsigned int)
+  ~((1<<ACL_WHERE_DATA)|                           /* regex */
+  #ifndef DISABLE_PRDR
+    (1<<ACL_WHERE_PRDR)|
+  #endif
+    (1<<ACL_WHERE_NOTSMTP)|
+    (1<<ACL_WHERE_MIME)),
+  #endif
+
+  (unsigned int)
+  ~((1<<ACL_WHERE_MAIL)|(1<<ACL_WHERE_RCPT)|       /* remove_header */
+    (1<<ACL_WHERE_PREDATA)|(1<<ACL_WHERE_DATA)|
+  #ifndef DISABLE_PRDR
+    (1<<ACL_WHERE_PRDR)|
+  #endif
+    (1<<ACL_WHERE_MIME)|(1<<ACL_WHERE_NOTSMTP)|
+    (1<<ACL_WHERE_NOTSMTP_START)),
+
+  (1<<ACL_WHERE_AUTH)|(1<<ACL_WHERE_CONNECT)|      /* sender_domains */
+    (1<<ACL_WHERE_HELO)|
+    (1<<ACL_WHERE_MAILAUTH)|(1<<ACL_WHERE_QUIT)|
+    (1<<ACL_WHERE_ETRN)|(1<<ACL_WHERE_EXPN)|
+    (1<<ACL_WHERE_STARTTLS)|(1<<ACL_WHERE_VRFY),
+
+  (1<<ACL_WHERE_AUTH)|(1<<ACL_WHERE_CONNECT)|      /* senders */
+    (1<<ACL_WHERE_HELO)|
+    (1<<ACL_WHERE_MAILAUTH)|(1<<ACL_WHERE_QUIT)|
+    (1<<ACL_WHERE_ETRN)|(1<<ACL_WHERE_EXPN)|
+    (1<<ACL_WHERE_STARTTLS)|(1<<ACL_WHERE_VRFY),
+
+  0,                                               /* set */
+
+  #ifdef WITH_CONTENT_SCAN
+  (unsigned int)
+  ~((1<<ACL_WHERE_DATA)|                           /* spam */
+  #ifndef DISABLE_PRDR
+    (1<<ACL_WHERE_PRDR)|
+  #endif
+    (1<<ACL_WHERE_NOTSMTP)),
+  #endif
+
+  #ifdef EXPERIMENTAL_SPF
+  (1<<ACL_WHERE_AUTH)|(1<<ACL_WHERE_CONNECT)|      /* spf */
+    (1<<ACL_WHERE_HELO)|
+    (1<<ACL_WHERE_MAILAUTH)|
+    (1<<ACL_WHERE_ETRN)|(1<<ACL_WHERE_EXPN)|
+    (1<<ACL_WHERE_STARTTLS)|(1<<ACL_WHERE_VRFY)|
+    (1<<ACL_WHERE_NOTSMTP)|
+    (1<<ACL_WHERE_NOTSMTP_START),
+
+  (1<<ACL_WHERE_AUTH)|(1<<ACL_WHERE_CONNECT)|      /* spf_guess */
+    (1<<ACL_WHERE_HELO)|
+    (1<<ACL_WHERE_MAILAUTH)|
+    (1<<ACL_WHERE_ETRN)|(1<<ACL_WHERE_EXPN)|
+    (1<<ACL_WHERE_STARTTLS)|(1<<ACL_WHERE_VRFY)|
+    (1<<ACL_WHERE_NOTSMTP)|
+    (1<<ACL_WHERE_NOTSMTP_START),
+  #endif
+
+  0,                                               /* udpsend */
+
+  /* Certain types of verify are always allowed, so we let it through
+  always and check in the verify function itself */
+
+  0                                                /* verify */
+};
+
+
+/* Bit map vector of which controls are not allowed at certain times. For
+each control, there's a bitmap of dis-allowed times. For some, it is easier to
+specify the negation of a small number of allowed times. */
+
+static unsigned int control_forbids[] = {
+  (unsigned int)
+  ~((1<<ACL_WHERE_CONNECT)|(1<<ACL_WHERE_HELO)),   /* allow_auth_unadvertised */
+
+  #ifdef EXPERIMENTAL_BRIGHTMAIL
+  0,                                               /* bmi_run */
+  #endif
+
+  0,                                               /* debug */
+
+  #ifndef DISABLE_DKIM
+  (1<<ACL_WHERE_DATA)|(1<<ACL_WHERE_NOTSMTP)|      /* dkim_disable_verify */
+  #ifndef DISABLE_PRDR
+    (1<<ACL_WHERE_PRDR)|
+  #endif
+    (1<<ACL_WHERE_NOTSMTP_START),
+  #endif
+
+  #ifdef EXPERIMENTAL_DMARC
+  (1<<ACL_WHERE_DATA)|(1<<ACL_WHERE_NOTSMTP)|      /* dmarc_disable_verify */
+    (1<<ACL_WHERE_NOTSMTP_START),
+  (1<<ACL_WHERE_DATA)|(1<<ACL_WHERE_NOTSMTP)|      /* dmarc_enable_forensic */
+    (1<<ACL_WHERE_NOTSMTP_START),
+  #endif
+
+  (1<<ACL_WHERE_NOTSMTP)|
+    (1<<ACL_WHERE_NOTSMTP_START)|
+    (1<<ACL_WHERE_NOTQUIT),                        /* dscp */
+
+  0,                                               /* error */
+
+  (unsigned int)
+  ~(1<<ACL_WHERE_RCPT),                            /* caseful_local_part */
+
+  (unsigned int)
+  ~(1<<ACL_WHERE_RCPT),                            /* caselower_local_part */
+
+  (unsigned int)
+  0,                                              /* cutthrough_delivery */
+
+  (1<<ACL_WHERE_NOTSMTP)|                          /* enforce_sync */
+    (1<<ACL_WHERE_NOTSMTP_START),
+
+  (1<<ACL_WHERE_NOTSMTP)|                          /* no_enforce_sync */
+    (1<<ACL_WHERE_NOTSMTP_START),
+
+  (unsigned int)
+  ~((1<<ACL_WHERE_MAIL)|(1<<ACL_WHERE_RCPT)|       /* freeze */
+    (1<<ACL_WHERE_PREDATA)|(1<<ACL_WHERE_DATA)|
+    // (1<<ACL_WHERE_PRDR)|    /* Not allow one user to freeze for all */
+    (1<<ACL_WHERE_NOTSMTP)|(1<<ACL_WHERE_MIME)),
+
+  (unsigned int)
+  ~((1<<ACL_WHERE_MAIL)|(1<<ACL_WHERE_RCPT)|       /* queue_only */
+    (1<<ACL_WHERE_PREDATA)|(1<<ACL_WHERE_DATA)|
+    // (1<<ACL_WHERE_PRDR)|    /* Not allow one user to freeze for all */
+    (1<<ACL_WHERE_NOTSMTP)|(1<<ACL_WHERE_MIME)),
+
+  (unsigned int)
+  ~((1<<ACL_WHERE_MAIL)|(1<<ACL_WHERE_RCPT)|       /* submission */
+    (1<<ACL_WHERE_PREDATA)),
+
+  (unsigned int)
+  ~((1<<ACL_WHERE_MAIL)|(1<<ACL_WHERE_RCPT)|       /* suppress_local_fixups */
+    (1<<ACL_WHERE_PREDATA)|
+    (1<<ACL_WHERE_NOTSMTP_START)),
+
+  #ifdef WITH_CONTENT_SCAN
+  (unsigned int)
+  ~((1<<ACL_WHERE_MAIL)|(1<<ACL_WHERE_RCPT)|       /* no_mbox_unspool */
+    (1<<ACL_WHERE_PREDATA)|(1<<ACL_WHERE_DATA)|
+    // (1<<ACL_WHERE_PRDR)|    /* Not allow one user to freeze for all */
+    (1<<ACL_WHERE_MIME)),
+  #endif
+
+  (unsigned int)
+  ~((1<<ACL_WHERE_MAIL)|(1<<ACL_WHERE_RCPT)|       /* fakedefer */
+    (1<<ACL_WHERE_PREDATA)|(1<<ACL_WHERE_DATA)|
+  #ifndef DISABLE_PRDR
+    (1<<ACL_WHERE_PRDR)|
+  #endif
+    (1<<ACL_WHERE_MIME)),
+
+  (unsigned int)
+  ~((1<<ACL_WHERE_MAIL)|(1<<ACL_WHERE_RCPT)|       /* fakereject */
+    (1<<ACL_WHERE_PREDATA)|(1<<ACL_WHERE_DATA)|
+  #ifndef DISABLE_PRDR
+    (1<<ACL_WHERE_PRDR)|
+  #endif
+    (1<<ACL_WHERE_MIME)),
+
+  (1<<ACL_WHERE_NOTSMTP)|                          /* no_multiline */
+    (1<<ACL_WHERE_NOTSMTP_START),
+
+  (1<<ACL_WHERE_NOTSMTP)|                          /* no_pipelining */
+    (1<<ACL_WHERE_NOTSMTP_START),
+
+  (1<<ACL_WHERE_NOTSMTP)|                          /* no_delay_flush */
+    (1<<ACL_WHERE_NOTSMTP_START),
+
+  (1<<ACL_WHERE_NOTSMTP)|                          /* no_callout_flush */
+    (1<<ACL_WHERE_NOTSMTP_START)
+};
+
+/* Structure listing various control arguments, with their characteristics. */
+
+typedef struct control_def {
+  uschar *name;
+  int    value;                  /* CONTROL_xxx value */
+  BOOL   has_option;             /* Has /option(s) following */
+} control_def;
+
+static control_def controls_list[] = {
+  { US"allow_auth_unadvertised", CONTROL_AUTH_UNADVERTISED, FALSE },
+#ifdef EXPERIMENTAL_BRIGHTMAIL
+  { US"bmi_run",                 CONTROL_BMI_RUN, FALSE },
+#endif
+  { US"debug",                   CONTROL_DEBUG, TRUE },
+#ifndef DISABLE_DKIM
+  { US"dkim_disable_verify",     CONTROL_DKIM_VERIFY, FALSE },
+#endif
+#ifdef EXPERIMENTAL_DMARC
+  { US"dmarc_disable_verify",    CONTROL_DMARC_VERIFY, FALSE },
+  { US"dmarc_enable_forensic",   CONTROL_DMARC_FORENSIC, FALSE },
+#endif
+  { US"dscp",                    CONTROL_DSCP, TRUE },
+  { US"caseful_local_part",      CONTROL_CASEFUL_LOCAL_PART, FALSE },
+  { US"caselower_local_part",    CONTROL_CASELOWER_LOCAL_PART, FALSE },
+  { US"enforce_sync",            CONTROL_ENFORCE_SYNC, FALSE },
+  { US"freeze",                  CONTROL_FREEZE, TRUE },
+  { US"no_callout_flush",        CONTROL_NO_CALLOUT_FLUSH, FALSE },
+  { US"no_delay_flush",          CONTROL_NO_DELAY_FLUSH, FALSE },
+  { US"no_enforce_sync",         CONTROL_NO_ENFORCE_SYNC, FALSE },
+  { US"no_multiline_responses",  CONTROL_NO_MULTILINE, FALSE },
+  { US"no_pipelining",           CONTROL_NO_PIPELINING, FALSE },
+  { US"queue_only",              CONTROL_QUEUE_ONLY, FALSE },
+#ifdef WITH_CONTENT_SCAN
+  { US"no_mbox_unspool",         CONTROL_NO_MBOX_UNSPOOL, FALSE },
+#endif
+  { US"fakedefer",               CONTROL_FAKEDEFER, TRUE },
+  { US"fakereject",              CONTROL_FAKEREJECT, TRUE },
+  { US"submission",              CONTROL_SUBMISSION, TRUE },
+  { US"suppress_local_fixups",   CONTROL_SUPPRESS_LOCAL_FIXUPS, FALSE },
+  { US"cutthrough_delivery",     CONTROL_CUTTHROUGH_DELIVERY, FALSE }
+  };
+
+/* Support data structures for Client SMTP Authorization. acl_verify_csa()
+caches its result in a tree to avoid repeated DNS queries. The result is an
+integer code which is used as an index into the following tables of
+explanatory strings and verification return codes. */
+
+static tree_node *csa_cache = NULL;
+
+enum { CSA_UNKNOWN, CSA_OK, CSA_DEFER_SRV, CSA_DEFER_ADDR,
+ CSA_FAIL_EXPLICIT, CSA_FAIL_DOMAIN, CSA_FAIL_NOADDR, CSA_FAIL_MISMATCH };
+
+/* The acl_verify_csa() return code is translated into an acl_verify() return
+code using the following table. It is OK unless the client is definitely not
+authorized. This is because CSA is supposed to be optional for sending sites,
+so recipients should not be too strict about checking it - especially because
+DNS problems are quite likely to occur. It's possible to use $csa_status in
+further ACL conditions to distinguish ok, unknown, and defer if required, but
+the aim is to make the usual configuration simple. */
+
+static int csa_return_code[] = {
+  OK, OK, OK, OK,
+  FAIL, FAIL, FAIL, FAIL
+};
+
+static uschar *csa_status_string[] = {
+  US"unknown", US"ok", US"defer", US"defer",
+  US"fail", US"fail", US"fail", US"fail"
+};
+
+static uschar *csa_reason_string[] = {
+  US"unknown",
+  US"ok",
+  US"deferred (SRV lookup failed)",
+  US"deferred (target address lookup failed)",
+  US"failed (explicit authorization required)",
+  US"failed (host name not authorized)",
+  US"failed (no authorized addresses)",
+  US"failed (client address mismatch)"
+};
+
+/* Options for the ratelimit condition. Note that there are two variants of
+the per_rcpt option, depending on the ACL that is used to measure the rate.
+However any ACL must be able to look up per_rcpt rates in /noupdate mode,
+so the two variants must have the same internal representation as well as
+the same configuration string. */
+
+enum {
+  RATE_PER_WHAT, RATE_PER_CLASH, RATE_PER_ADDR, RATE_PER_BYTE, RATE_PER_CMD,
+  RATE_PER_CONN, RATE_PER_MAIL, RATE_PER_RCPT, RATE_PER_ALLRCPTS
+};
+
+#define RATE_SET(var,new) \
+  (((var) == RATE_PER_WHAT) ? ((var) = RATE_##new) : ((var) = RATE_PER_CLASH))
+
+static uschar *ratelimit_option_string[] = {
+  US"?", US"!", US"per_addr", US"per_byte", US"per_cmd",
+  US"per_conn", US"per_mail", US"per_rcpt", US"per_rcpt"
+};
+
+/* Enable recursion between acl_check_internal() and acl_check_condition() */
+
+static int acl_check_wargs(int, address_item *, uschar *, int, uschar **,
+    uschar **);
+
+
+/*************************************************
+*         Pick out name from list                *
+*************************************************/
+
+/* Use a binary chop method
+
+Arguments:
+  name        name to find
+  list        list of names
+  end         size of list
+
+Returns:      offset in list, or -1 if not found
+*/
+
+static int
+acl_checkname(uschar *name, uschar **list, int end)
+{
+int start = 0;
+
+while (start < end)
+  {
+  int mid = (start + end)/2;
+  int c = Ustrcmp(name, list[mid]);
+  if (c == 0) return mid;
+  if (c < 0) end = mid; else start = mid + 1;
+  }
+
+return -1;
+}
+
+
+/*************************************************
+*            Read and parse one ACL              *
+*************************************************/
+
+/* This function is called both from readconf in order to parse the ACLs in the
+configuration file, and also when an ACL is encountered dynamically (e.g. as
+the result of an expansion). It is given a function to call in order to
+retrieve the lines of the ACL. This function handles skipping comments and
+blank lines (where relevant).
+
+Arguments:
+  func        function to get next line of ACL
+  error       where to put an error message
+
+Returns:      pointer to ACL, or NULL
+              NULL can be legal (empty ACL); in this case error will be NULL
+*/
+
+acl_block *
+acl_read(uschar *(*func)(void), uschar **error)
+{
+acl_block *yield = NULL;
+acl_block **lastp = &yield;
+acl_block *this = NULL;
+acl_condition_block *cond;
+acl_condition_block **condp = NULL;
+uschar *s;
+
+*error = NULL;
+
+while ((s = (*func)()) != NULL)
+  {
+  int v, c;
+  BOOL negated = FALSE;
+  uschar *saveline = s;
+  uschar name[64];
+
+  /* Conditions (but not verbs) are allowed to be negated by an initial
+  exclamation mark. */
+
+  while (isspace(*s)) s++;
+  if (*s == '!')
+    {
+    negated = TRUE;
+    s++;
+    }
+
+  /* Read the name of a verb or a condition, or the start of a new ACL, which
+  can be started by a name, or by a macro definition. */
+
+  s = readconf_readname(name, sizeof(name), s);
+  if (*s == ':' || (isupper(name[0]) && *s == '=')) return yield;
+
+  /* If a verb is unrecognized, it may be another condition or modifier that
+  continues the previous verb. */
+
+  v = acl_checkname(name, verbs, sizeof(verbs)/sizeof(char *));
+  if (v < 0)
+    {
+    if (this == NULL)
+      {
+      *error = string_sprintf("unknown ACL verb \"%s\" in \"%s\"", name,
+        saveline);
+      return NULL;
+      }
+    }
+
+  /* New verb */
+
+  else
+    {
+    if (negated)
+      {
+      *error = string_sprintf("malformed ACL line \"%s\"", saveline);
+      return NULL;
+      }
+    this = store_get(sizeof(acl_block));
+    *lastp = this;
+    lastp = &(this->next);
+    this->next = NULL;
+    this->verb = v;
+    this->condition = NULL;
+    condp = &(this->condition);
+    if (*s == 0) continue;               /* No condition on this line */
+    if (*s == '!')
+      {
+      negated = TRUE;
+      s++;
+      }
+    s = readconf_readname(name, sizeof(name), s);  /* Condition name */
+    }
+
+  /* Handle a condition or modifier. */
+
+  c = acl_checkname(name, conditions, sizeof(conditions)/sizeof(char *));
+  if (c < 0)
+    {
+    *error = string_sprintf("unknown ACL condition/modifier in \"%s\"",
+      saveline);
+    return NULL;
+    }
+
+  /* The modifiers may not be negated */
+
+  if (negated && cond_modifiers[c])
+    {
+    *error = string_sprintf("ACL error: negation is not allowed with "
+      "\"%s\"", conditions[c]);
+    return NULL;
+    }
+
+  /* ENDPASS may occur only with ACCEPT or DISCARD. */
+
+  if (c == ACLC_ENDPASS &&
+      this->verb != ACL_ACCEPT &&
+      this->verb != ACL_DISCARD)
+    {
+    *error = string_sprintf("ACL error: \"%s\" is not allowed with \"%s\"",
+      conditions[c], verbs[this->verb]);
+    return NULL;
+    }
+
+  cond = store_get(sizeof(acl_condition_block));
+  cond->next = NULL;
+  cond->type = c;
+  cond->u.negated = negated;
+
+  *condp = cond;
+  condp = &(cond->next);
+
+  /* The "set" modifier is different in that its argument is "name=value"
+  rather than just a value, and we can check the validity of the name, which
+  gives us a variable name to insert into the data block. The original ACL
+  variable names were acl_c0 ... acl_c9 and acl_m0 ... acl_m9. This was
+  extended to 20 of each type, but after that people successfully argued for
+  arbitrary names. In the new scheme, the names must start with acl_c or acl_m.
+  After that, we allow alphanumerics and underscores, but the first character
+  after c or m must be a digit or an underscore. This retains backwards
+  compatibility. */
+
+  if (c == ACLC_SET)
+    {
+    uschar *endptr;
+
+    if (Ustrncmp(s, "acl_c", 5) != 0 &&
+        Ustrncmp(s, "acl_m", 5) != 0)
+      {
+      *error = string_sprintf("invalid variable name after \"set\" in ACL "
+        "modifier \"set %s\" (must start \"acl_c\" or \"acl_m\")", s);
+      return NULL;
+      }
+
+    endptr = s + 5;
+    if (!isdigit(*endptr) && *endptr != '_')
+      {
+      *error = string_sprintf("invalid variable name after \"set\" in ACL "
+        "modifier \"set %s\" (digit or underscore must follow acl_c or acl_m)",
+        s);
+      return NULL;
+      }
+
+    while (*endptr != 0 && *endptr != '=' && !isspace(*endptr))
+      {
+      if (!isalnum(*endptr) && *endptr != '_')
+        {
+        *error = string_sprintf("invalid character \"%c\" in variable name "
+          "in ACL modifier \"set %s\"", *endptr, s);
+        return NULL;
+        }
+      endptr++;
+      }
+
+    cond->u.varname = string_copyn(s + 4, endptr - s - 4);
+    s = endptr;
+    while (isspace(*s)) s++;
+    }
+
+  /* For "set", we are now positioned for the data. For the others, only
+  "endpass" has no data */
+
+  if (c != ACLC_ENDPASS)
+    {
+    if (*s++ != '=')
+      {
+      *error = string_sprintf("\"=\" missing after ACL \"%s\" %s", name,
+        cond_modifiers[c]? US"modifier" : US"condition");
+      return NULL;
+      }
+    while (isspace(*s)) s++;
+    cond->arg = string_copy(s);
+    }
+  }
+
+return yield;
+}
+
+
+
+/*************************************************
+*         Set up added header line(s)            *
+*************************************************/
+
+/* This function is called by the add_header modifier, and also from acl_warn()
+to implement the now-deprecated way of adding header lines using "message" on a
+"warn" verb. The argument is treated as a sequence of header lines which are
+added to a chain, provided there isn't an identical one already there.
+
+Argument:   string of header lines
+Returns:    nothing
+*/
+
+static void
+setup_header(uschar *hstring)
+{
+uschar *p, *q;
+int hlen = Ustrlen(hstring);
+
+/* Ignore any leading newlines */
+while (*hstring == '\n') hstring++, hlen--;
+
+/* An empty string does nothing; ensure exactly one final newline. */
+if (hlen <= 0) return;
+if (hstring[--hlen] != '\n') hstring = string_sprintf("%s\n", hstring);
+else while(hstring[--hlen] == '\n') hstring[hlen+1] = '\0';
+
+/* Loop for multiple header lines, taking care about continuations */
+
+for (p = q = hstring; *p != 0; )
+  {
+  uschar *s;
+  int newtype = htype_add_bot;
+  header_line **hptr = &acl_added_headers;
+
+  /* Find next header line within the string */
+
+  for (;;)
+    {
+    q = Ustrchr(q, '\n');
+    if (*(++q) != ' ' && *q != '\t') break;
+    }
+
+  /* If the line starts with a colon, interpret the instruction for where to
+  add it. This temporarily sets up a new type. */
+
+  if (*p == ':')
+    {
+    if (strncmpic(p, US":after_received:", 16) == 0)
+      {
+      newtype = htype_add_rec;
+      p += 16;
+      }
+    else if (strncmpic(p, US":at_start_rfc:", 14) == 0)
+      {
+      newtype = htype_add_rfc;
+      p += 14;
+      }
+    else if (strncmpic(p, US":at_start:", 10) == 0)
+      {
+      newtype = htype_add_top;
+      p += 10;
+      }
+    else if (strncmpic(p, US":at_end:", 8) == 0)
+      {
+      newtype = htype_add_bot;
+      p += 8;
+      }
+    while (*p == ' ' || *p == '\t') p++;
+    }
+
+  /* See if this line starts with a header name, and if not, add X-ACL-Warn:
+  to the front of it. */
+
+  for (s = p; s < q - 1; s++)
+    {
+    if (*s == ':' || !isgraph(*s)) break;
+    }
+
+  s = string_sprintf("%s%.*s", (*s == ':')? "" : "X-ACL-Warn: ", (int) (q - p), p);
+  hlen = Ustrlen(s);
+
+  /* See if this line has already been added */
+
+  while (*hptr != NULL)
+    {
+    if (Ustrncmp((*hptr)->text, s, hlen) == 0) break;
+    hptr = &((*hptr)->next);
+    }
+
+  /* Add if not previously present */
+
+  if (*hptr == NULL)
+    {
+    header_line *h = store_get(sizeof(header_line));
+    h->text = s;
+    h->next = NULL;
+    h->type = newtype;
+    h->slen = hlen;
+    *hptr = h;
+    hptr = &(h->next);
+    }
+
+  /* Advance for next header line within the string */
+
+  p = q;
+  }
+}
+
+
+
+/*************************************************
+*        List the added header lines            *
+*************************************************/
+uschar *
+fn_hdrs_added(void)
+{
+uschar * ret = NULL;
+header_line * h = acl_added_headers;
+uschar * s;
+uschar * cp;
+int size = 0;
+int ptr = 0;
+
+if (!h) return NULL;
+
+do
+  {
+  s = h->text;
+  while ((cp = Ustrchr(s, '\n')) != NULL)
+    {
+    if (cp[1] == '\0') break;
+
+    /* contains embedded newline; needs doubling */
+    ret = string_cat(ret, &size, &ptr, s, cp-s+1);
+    ret = string_cat(ret, &size, &ptr, US"\n", 1);
+    s = cp+1;
+    }
+  /* last bit of header */
+
+  ret = string_cat(ret, &size, &ptr, s, cp-s+1);       /* newline-sep list */
+  }
+while((h = h->next));
+
+ret[ptr-1] = '\0';     /* overwrite last newline */
+return ret;
+}
+
+
+/*************************************************
+*        Set up removed header line(s)           *
+*************************************************/
+
+/* This function is called by the remove_header modifier.  The argument is
+treated as a sequence of header names which are added to a colon separated
+list, provided there isn't an identical one already there.
+
+Argument:   string of header names
+Returns:    nothing
+*/
+
+static void
+setup_remove_header(uschar *hnames)
+{
+if (*hnames != 0)
+  {
+  if (acl_removed_headers == NULL)
+    acl_removed_headers = hnames;
+  else
+    acl_removed_headers = string_sprintf("%s : %s", acl_removed_headers, hnames);
+  }
+}
+
+
+
+/*************************************************
+*               Handle warnings                  *
+*************************************************/
+
+/* This function is called when a WARN verb's conditions are true. It adds to
+the message's headers, and/or writes information to the log. In each case, this
+only happens once (per message for headers, per connection for log).
+
+** NOTE: The header adding action using the "message" setting is historic, and
+its use is now deprecated. The new add_header modifier should be used instead.
+
+Arguments:
+  where          ACL_WHERE_xxxx indicating which ACL this is
+  user_message   message for adding to headers
+  log_message    message for logging, if different
+
+Returns:         nothing
+*/
+
+static void
+acl_warn(int where, uschar *user_message, uschar *log_message)
+{
+if (log_message != NULL && log_message != user_message)
+  {
+  uschar *text;
+  string_item *logged;
+
+  text = string_sprintf("%s Warning: %s",  host_and_ident(TRUE),
+    string_printing(log_message));
+
+  /* If a sender verification has failed, and the log message is "sender verify
+  failed", add the failure message. */
+
+  if (sender_verified_failed != NULL &&
+      sender_verified_failed->message != NULL &&
+      strcmpic(log_message, US"sender verify failed") == 0)
+    text = string_sprintf("%s: %s", text, sender_verified_failed->message);
+
+  /* Search previously logged warnings. They are kept in malloc
+  store so they can be freed at the start of a new message. */
+
+  for (logged = acl_warn_logged; logged != NULL; logged = logged->next)
+    if (Ustrcmp(logged->text, text) == 0) break;
+
+  if (logged == NULL)
+    {
+    int length = Ustrlen(text) + 1;
+    log_write(0, LOG_MAIN, "%s", text);
+    logged = store_malloc(sizeof(string_item) + length);
+    logged->text = (uschar *)logged + sizeof(string_item);
+    memcpy(logged->text, text, length);
+    logged->next = acl_warn_logged;
+    acl_warn_logged = logged;
+    }
+  }
+
+/* If there's no user message, we are done. */
+
+if (user_message == NULL) return;
+
+/* If this isn't a message ACL, we can't do anything with a user message.
+Log an error. */
+
+if (where > ACL_WHERE_NOTSMTP)
+  {
+  log_write(0, LOG_MAIN|LOG_PANIC, "ACL \"warn\" with \"message\" setting "
+    "found in a non-message (%s) ACL: cannot specify header lines here: "
+    "message ignored", acl_wherenames[where]);
+  return;
+  }
+
+/* The code for setting up header lines is now abstracted into a separate
+function so that it can be used for the add_header modifier as well. */
+
+setup_header(user_message);
+}
+
+
+
+/*************************************************
+*         Verify and check reverse DNS           *
+*************************************************/
+
+/* Called from acl_verify() below. We look up the host name(s) of the client IP
+address if this has not yet been done. The host_name_lookup() function checks
+that one of these names resolves to an address list that contains the client IP
+address, so we don't actually have to do the check here.
+
+Arguments:
+  user_msgptr  pointer for user message
+  log_msgptr   pointer for log message
+
+Returns:       OK        verification condition succeeded
+               FAIL      verification failed
+               DEFER     there was a problem verifying
+*/
+
+static int
+acl_verify_reverse(uschar **user_msgptr, uschar **log_msgptr)
+{
+int rc;
+
+user_msgptr = user_msgptr;  /* stop compiler warning */
+
+/* Previous success */
+
+if (sender_host_name != NULL) return OK;
+
+/* Previous failure */
+
+if (host_lookup_failed)
+  {
+  *log_msgptr = string_sprintf("host lookup failed%s", host_lookup_msg);
+  return FAIL;
+  }
+
+/* Need to do a lookup */
+
+HDEBUG(D_acl)
+  debug_printf("looking up host name to force name/address consistency check\n");
+
+if ((rc = host_name_lookup()) != OK)
+  {
+  *log_msgptr = (rc == DEFER)?
+    US"host lookup deferred for reverse lookup check"
+    :
+    string_sprintf("host lookup failed for reverse lookup check%s",
+      host_lookup_msg);
+  return rc;    /* DEFER or FAIL */
+  }
+
+host_build_sender_fullhost();
+return OK;
+}
+
+
+
+/*************************************************
+*   Check client IP address matches CSA target   *
+*************************************************/
+
+/* Called from acl_verify_csa() below. This routine scans a section of a DNS
+response for address records belonging to the CSA target hostname. The section
+is specified by the reset argument, either RESET_ADDITIONAL or RESET_ANSWERS.
+If one of the addresses matches the client's IP address, then the client is
+authorized by CSA. If there are target IP addresses but none of them match
+then the client is using an unauthorized IP address. If there are no target IP
+addresses then the client cannot be using an authorized IP address. (This is
+an odd configuration - why didn't the SRV record have a weight of 1 instead?)
+
+Arguments:
+  dnsa       the DNS answer block
+  dnss       a DNS scan block for us to use
+  reset      option specifing what portion to scan, as described above
+  target     the target hostname to use for matching RR names
+
+Returns:     CSA_OK             successfully authorized
+             CSA_FAIL_MISMATCH  addresses found but none matched
+             CSA_FAIL_NOADDR    no target addresses found
+*/
+
+static int
+acl_verify_csa_address(dns_answer *dnsa, dns_scan *dnss, int reset,
+                       uschar *target)
+{
+dns_record *rr;
+dns_address *da;
+
+BOOL target_found = FALSE;
+
+for (rr = dns_next_rr(dnsa, dnss, reset);
+     rr != NULL;
+     rr = dns_next_rr(dnsa, dnss, RESET_NEXT))
+  {
+  /* Check this is an address RR for the target hostname. */
+
+  if (rr->type != T_A
+    #if HAVE_IPV6
+      && rr->type != T_AAAA
+      #ifdef SUPPORT_A6
+        && rr->type != T_A6
+      #endif
+    #endif
+  ) continue;
+
+  if (strcmpic(target, rr->name) != 0) continue;
+
+  target_found = TRUE;
+
+  /* Turn the target address RR into a list of textual IP addresses and scan
+  the list. There may be more than one if it is an A6 RR. */
+
+  for (da = dns_address_from_rr(dnsa, rr); da != NULL; da = da->next)
+    {
+    /* If the client IP address matches the target IP address, it's good! */
+
+    DEBUG(D_acl) debug_printf("CSA target address is %s\n", da->address);
+
+    if (strcmpic(sender_host_address, da->address) == 0) return CSA_OK;
+    }
+  }
+
+/* If we found some target addresses but none of them matched, the client is
+using an unauthorized IP address, otherwise the target has no authorized IP
+addresses. */
+
+if (target_found) return CSA_FAIL_MISMATCH;
+else return CSA_FAIL_NOADDR;
+}
+
+
+
+/*************************************************
+*       Verify Client SMTP Authorization         *
+*************************************************/
+
+/* Called from acl_verify() below. This routine calls dns_lookup_special()
+to find the CSA SRV record corresponding to the domain argument, or
+$sender_helo_name if no argument is provided. It then checks that the
+client is authorized, and that its IP address corresponds to the SRV
+target's address by calling acl_verify_csa_address() above. The address
+should have been returned in the DNS response's ADDITIONAL section, but if
+not we perform another DNS lookup to get it.
+
+Arguments:
+  domain    pointer to optional parameter following verify = csa
+
+Returns:    CSA_UNKNOWN    no valid CSA record found
+            CSA_OK         successfully authorized
+            CSA_FAIL_*     client is definitely not authorized
+            CSA_DEFER_*    there was a DNS problem
+*/
+
+static int
+acl_verify_csa(uschar *domain)
+{
+tree_node *t;
+uschar *found, *p;
+int priority, weight, port;
+dns_answer dnsa;
+dns_scan dnss;
+dns_record *rr;
+int rc, type;
+uschar target[256];
+
+/* Work out the domain we are using for the CSA lookup. The default is the
+client's HELO domain. If the client has not said HELO, use its IP address
+instead. If it's a local client (exim -bs), CSA isn't applicable. */
+
+while (isspace(*domain) && *domain != '\0') ++domain;
+if (*domain == '\0') domain = sender_helo_name;
+if (domain == NULL) domain = sender_host_address;
+if (sender_host_address == NULL) return CSA_UNKNOWN;
+
+/* If we have an address literal, strip off the framing ready for turning it
+into a domain. The framing consists of matched square brackets possibly
+containing a keyword and a colon before the actual IP address. */
+
+if (domain[0] == '[')
+  {
+  uschar *start = Ustrchr(domain, ':');
+  if (start == NULL) start = domain;
+  domain = string_copyn(start + 1, Ustrlen(start) - 2);
+  }
+
+/* Turn domains that look like bare IP addresses into domains in the reverse
+DNS. This code also deals with address literals and $sender_host_address. It's
+not quite kosher to treat bare domains such as EHLO 192.0.2.57 the same as
+address literals, but it's probably the most friendly thing to do. This is an
+extension to CSA, so we allow it to be turned off for proper conformance. */
+
+if (string_is_ip_address(domain, NULL) != 0)
+  {
+  if (!dns_csa_use_reverse) return CSA_UNKNOWN;
+  dns_build_reverse(domain, target);
+  domain = target;
+  }
+
+/* Find out if we've already done the CSA check for this domain. If we have,
+return the same result again. Otherwise build a new cached result structure
+for this domain. The name is filled in now, and the value is filled in when
+we return from this function. */
+
+t = tree_search(csa_cache, domain);
+if (t != NULL) return t->data.val;
+
+t = store_get_perm(sizeof(tree_node) + Ustrlen(domain));
+Ustrcpy(t->name, domain);
+(void)tree_insertnode(&csa_cache, t);
+
+/* Now we are ready to do the actual DNS lookup(s). */
+
+found = domain;
+switch (dns_special_lookup(&dnsa, domain, T_CSA, &found))
+  {
+  /* If something bad happened (most commonly DNS_AGAIN), defer. */
+
+  default:
+  return t->data.val = CSA_DEFER_SRV;
+
+  /* If we found nothing, the client's authorization is unknown. */
+
+  case DNS_NOMATCH:
+  case DNS_NODATA:
+  return t->data.val = CSA_UNKNOWN;
+
+  /* We got something! Go on to look at the reply in more detail. */
+
+  case DNS_SUCCEED:
+  break;
+  }
+
+/* Scan the reply for well-formed CSA SRV records. */
+
+for (rr = dns_next_rr(&dnsa, &dnss, RESET_ANSWERS);
+     rr != NULL;
+     rr = dns_next_rr(&dnsa, &dnss, RESET_NEXT))
+  {
+  if (rr->type != T_SRV) continue;
+
+  /* Extract the numerical SRV fields (p is incremented) */
+
+  p = rr->data;
+  GETSHORT(priority, p);
+  GETSHORT(weight, p);
+  GETSHORT(port, p);
+
+  DEBUG(D_acl)
+    debug_printf("CSA priority=%d weight=%d port=%d\n", priority, weight, port);
+
+  /* Check the CSA version number */
+
+  if (priority != 1) continue;
+
+  /* If the domain does not have a CSA SRV record of its own (i.e. the domain
+  found by dns_special_lookup() is a parent of the one we asked for), we check
+  the subdomain assertions in the port field. At the moment there's only one
+  assertion: legitimate SMTP clients are all explicitly authorized with CSA
+  SRV records of their own. */
+
+  if (found != domain)
+    {
+    if (port & 1)
+      return t->data.val = CSA_FAIL_EXPLICIT;
+    else
+      return t->data.val = CSA_UNKNOWN;
+    }
+
+  /* This CSA SRV record refers directly to our domain, so we check the value
+  in the weight field to work out the domain's authorization. 0 and 1 are
+  unauthorized; 3 means the client is authorized but we can't check the IP
+  address in order to authenticate it, so we treat it as unknown; values
+  greater than 3 are undefined. */
+
+  if (weight < 2) return t->data.val = CSA_FAIL_DOMAIN;
+
+  if (weight > 2) continue;
+
+  /* Weight == 2, which means the domain is authorized. We must check that the
+  client's IP address is listed as one of the SRV target addresses. Save the
+  target hostname then break to scan the additional data for its addresses. */
+
+  (void)dn_expand(dnsa.answer, dnsa.answer + dnsa.answerlen, p,
+    (DN_EXPAND_ARG4_TYPE)target, sizeof(target));
+
+  DEBUG(D_acl) debug_printf("CSA target is %s\n", target);
+
+  break;
+  }
+
+/* If we didn't break the loop then no appropriate records were found. */
+
+if (rr == NULL) return t->data.val = CSA_UNKNOWN;
+
+/* Do not check addresses if the target is ".", in accordance with RFC 2782.
+A target of "." indicates there are no valid addresses, so the client cannot
+be authorized. (This is an odd configuration because weight=2 target=. is
+equivalent to weight=1, but we check for it in order to keep load off the
+root name servers.) Note that dn_expand() turns "." into "". */
+
+if (Ustrcmp(target, "") == 0) return t->data.val = CSA_FAIL_NOADDR;
+
+/* Scan the additional section of the CSA SRV reply for addresses belonging
+to the target. If the name server didn't return any additional data (e.g.
+because it does not fully support SRV records), we need to do another lookup
+to obtain the target addresses; otherwise we have a definitive result. */
+
+rc = acl_verify_csa_address(&dnsa, &dnss, RESET_ADDITIONAL, target);
+if (rc != CSA_FAIL_NOADDR) return t->data.val = rc;
+
+/* The DNS lookup type corresponds to the IP version used by the client. */
+
+#if HAVE_IPV6
+if (Ustrchr(sender_host_address, ':') != NULL)
+  type = T_AAAA;
+else
+#endif /* HAVE_IPV6 */
+  type = T_A;
+
+
+#if HAVE_IPV6 && defined(SUPPORT_A6)
+DNS_LOOKUP_AGAIN:
+#endif
+
+lookup_dnssec_authenticated = NULL;
+switch (dns_lookup(&dnsa, target, type, NULL))
+  {
+  /* If something bad happened (most commonly DNS_AGAIN), defer. */
+
+  default:
+  return t->data.val = CSA_DEFER_ADDR;
+
+  /* If the query succeeded, scan the addresses and return the result. */
+
+  case DNS_SUCCEED:
+  rc = acl_verify_csa_address(&dnsa, &dnss, RESET_ANSWERS, target);
+  if (rc != CSA_FAIL_NOADDR) return t->data.val = rc;
+  /* else fall through */
+
+  /* If the target has no IP addresses, the client cannot have an authorized
+  IP address. However, if the target site uses A6 records (not AAAA records)
+  we have to do yet another lookup in order to check them. */
+
+  case DNS_NOMATCH:
+  case DNS_NODATA:
+
+  #if HAVE_IPV6 && defined(SUPPORT_A6)
+  if (type == T_AAAA) { type = T_A6; goto DNS_LOOKUP_AGAIN; }
+  #endif
+
+  return t->data.val = CSA_FAIL_NOADDR;
+  }
+}
+
+
+
+/*************************************************
+*     Handle verification (address & other)      *
+*************************************************/
+
+enum { VERIFY_REV_HOST_LKUP, VERIFY_CERT, VERIFY_HELO, VERIFY_CSA, VERIFY_HDR_SYNTAX,
+       VERIFY_NOT_BLIND, VERIFY_HDR_SNDR, VERIFY_SNDR, VERIFY_RCPT,
+       VERIFY_HDR_NAMES_ASCII
+  };
+typedef struct {
+  uschar * name;
+  int     value;
+  unsigned where_allowed;      /* bitmap */
+  BOOL    no_options;          /* Never has /option(s) following */
+  unsigned alt_opt_sep;                /* >0 Non-/ option separator (custom parser) */
+  } verify_type_t;
+static verify_type_t verify_type_list[] = {
+    { US"reverse_host_lookup", VERIFY_REV_HOST_LKUP,   ~0,     TRUE, 0 },
+    { US"certificate",         VERIFY_CERT,            ~0,     TRUE, 0 },
+    { US"helo",                        VERIFY_HELO,            ~0,     TRUE, 0 },
+    { US"csa",                 VERIFY_CSA,             ~0,     FALSE, 0 },
+    { US"header_syntax",       VERIFY_HDR_SYNTAX,      (1<<ACL_WHERE_DATA)|(1<<ACL_WHERE_NOTSMTP), TRUE, 0 },
+    { US"not_blind",           VERIFY_NOT_BLIND,       (1<<ACL_WHERE_DATA)|(1<<ACL_WHERE_NOTSMTP), TRUE, 0 },
+    { US"header_sender",       VERIFY_HDR_SNDR,        (1<<ACL_WHERE_DATA)|(1<<ACL_WHERE_NOTSMTP), FALSE, 0 },
+    { US"sender",              VERIFY_SNDR,            (1<<ACL_WHERE_MAIL)|(1<<ACL_WHERE_RCPT)
+                       |(1<<ACL_WHERE_PREDATA)|(1<<ACL_WHERE_DATA)|(1<<ACL_WHERE_NOTSMTP),
+                                                                               FALSE, 6 },
+    { US"recipient",           VERIFY_RCPT,            (1<<ACL_WHERE_RCPT),    FALSE, 0 },
+    { US"header_names_ascii",  VERIFY_HDR_NAMES_ASCII, (1<<ACL_WHERE_DATA)|(1<<ACL_WHERE_NOTSMTP), TRUE, 0 }
+  };
+
+
+enum { CALLOUT_DEFER_OK, CALLOUT_NOCACHE, CALLOUT_RANDOM, CALLOUT_USE_SENDER,
+  CALLOUT_USE_POSTMASTER, CALLOUT_POSTMASTER, CALLOUT_FULLPOSTMASTER,
+  CALLOUT_MAILFROM, CALLOUT_POSTMASTER_MAILFROM, CALLOUT_MAXWAIT, CALLOUT_CONNECT,
+  CALLOUT_TIME
+  };
+typedef struct {
+  uschar * name;
+  int      value;
+  int     flag;
+  BOOL     has_option; /* Has =option(s) following */
+  BOOL     timeval;    /* Has a time value */
+  } callout_opt_t;
+static callout_opt_t callout_opt_list[] = {
+    { US"defer_ok",      CALLOUT_DEFER_OK,      0,                             FALSE, FALSE },
+    { US"no_cache",      CALLOUT_NOCACHE,       vopt_callout_no_cache,         FALSE, FALSE },
+    { US"random",        CALLOUT_RANDOM,        vopt_callout_random,           FALSE, FALSE },
+    { US"use_sender",     CALLOUT_USE_SENDER,   vopt_callout_recipsender,      FALSE, FALSE },
+    { US"use_postmaster", CALLOUT_USE_POSTMASTER,vopt_callout_recippmaster,    FALSE, FALSE },
+    { US"postmaster_mailfrom",CALLOUT_POSTMASTER_MAILFROM,0,                   TRUE,  FALSE },
+    { US"postmaster",    CALLOUT_POSTMASTER,    0,                             FALSE, FALSE },
+    { US"fullpostmaster", CALLOUT_FULLPOSTMASTER,vopt_callout_fullpm,          FALSE, FALSE },
+    { US"mailfrom",      CALLOUT_MAILFROM,      0,                             TRUE,  FALSE },
+    { US"maxwait",       CALLOUT_MAXWAIT,       0,                             TRUE,  TRUE },
+    { US"connect",       CALLOUT_CONNECT,       0,                             TRUE,  TRUE },
+    { NULL,              CALLOUT_TIME,          0,                             FALSE, TRUE }
+  };
+
+
+
+/* This function implements the "verify" condition. It is called when
+encountered in any ACL, because some tests are almost always permitted. Some
+just don't make sense, and always fail (for example, an attempt to test a host
+lookup for a non-TCP/IP message). Others are restricted to certain ACLs.
+
+Arguments:
+  where        where called from
+  addr         the recipient address that the ACL is handling, or NULL
+  arg          the argument of "verify"
+  user_msgptr  pointer for user message
+  log_msgptr   pointer for log message
+  basic_errno  where to put verify errno
+
+Returns:       OK        verification condition succeeded
+               FAIL      verification failed
+               DEFER     there was a problem verifying
+               ERROR     syntax error
+*/
+
+static int
+acl_verify(int where, address_item *addr, uschar *arg,
+  uschar **user_msgptr, uschar **log_msgptr, int *basic_errno)
+{
+int sep = '/';
+int callout = -1;
+int callout_overall = -1;
+int callout_connect = -1;
+int verify_options = 0;
+int rc;
+BOOL verify_header_sender = FALSE;
+BOOL defer_ok = FALSE;
+BOOL callout_defer_ok = FALSE;
+BOOL no_details = FALSE;
+BOOL success_on_redirect = FALSE;
+address_item *sender_vaddr = NULL;
+uschar *verify_sender_address = NULL;
+uschar *pm_mailfrom = NULL;
+uschar *se_mailfrom = NULL;
+
+/* Some of the verify items have slash-separated options; some do not. Diagnose
+an error if options are given for items that don't expect them.
+*/
+
+uschar *slash = Ustrchr(arg, '/');
+uschar *list = arg;
+uschar *ss = string_nextinlist(&list, &sep, big_buffer, big_buffer_size);
+verify_type_t * vp;
+
+if (ss == NULL) goto BAD_VERIFY;
+
+/* Handle name/address consistency verification in a separate function. */
+
+for (vp= verify_type_list;
+     (char *)vp < (char *)verify_type_list + sizeof(verify_type_list);
+     vp++
+    )
+  if (vp->alt_opt_sep ? strncmpic(ss, vp->name, vp->alt_opt_sep) == 0
+                      : strcmpic (ss, vp->name) == 0)
+   break;
+if ((char *)vp >= (char *)verify_type_list + sizeof(verify_type_list))
+  goto BAD_VERIFY;
+
+if (vp->no_options && slash != NULL)
+  {
+  *log_msgptr = string_sprintf("unexpected '/' found in \"%s\" "
+    "(this verify item has no options)", arg);
+  return ERROR;
+  }
+if (!(vp->where_allowed & (1<<where)))
+  {
+  *log_msgptr = string_sprintf("cannot verify %s in ACL for %s", vp->name, acl_wherenames[where]);
+  return ERROR;
+  }
+switch(vp->value)
+  {
+  case VERIFY_REV_HOST_LKUP:
+    if (sender_host_address == NULL) return OK;
+    return acl_verify_reverse(user_msgptr, log_msgptr);
+
+  case VERIFY_CERT:
+    /* TLS certificate verification is done at STARTTLS time; here we just
+    test whether it was successful or not. (This is for optional verification; for
+    mandatory verification, the connection doesn't last this long.) */
+
+      if (tls_in.certificate_verified) return OK;
+      *user_msgptr = US"no verified certificate";
+      return FAIL;
+
+  case VERIFY_HELO:
+    /* We can test the result of optional HELO verification that might have
+    occurred earlier. If not, we can attempt the verification now. */
+
+      if (!helo_verified && !helo_verify_failed) smtp_verify_helo();
+      return helo_verified? OK : FAIL;
+
+  case VERIFY_CSA:
+    /* Do Client SMTP Authorization checks in a separate function, and turn the
+    result code into user-friendly strings. */
+
+      rc = acl_verify_csa(list);
+      *log_msgptr = *user_msgptr = string_sprintf("client SMTP authorization %s",
+                                              csa_reason_string[rc]);
+      csa_status = csa_status_string[rc];
+      DEBUG(D_acl) debug_printf("CSA result %s\n", csa_status);
+      return csa_return_code[rc];
+
+  case VERIFY_HDR_SYNTAX:
+    /* Check that all relevant header lines have the correct syntax. If there is
+    a syntax error, we return details of the error to the sender if configured to
+    send out full details. (But a "message" setting on the ACL can override, as
+    always). */
+
+    rc = verify_check_headers(log_msgptr);
+    if (rc != OK && smtp_return_error_details && *log_msgptr != NULL)
+      *user_msgptr = string_sprintf("Rejected after DATA: %s", *log_msgptr);
+    return rc;
+
+  case VERIFY_HDR_NAMES_ASCII:
+    /* Check that all header names are true 7 bit strings
+    See RFC 5322, 2.2. and RFC 6532, 3. */
+
+    rc = verify_check_header_names_ascii(log_msgptr);
+    if (rc != OK && smtp_return_error_details && *log_msgptr != NULL)
+      *user_msgptr = string_sprintf("Rejected after DATA: %s", *log_msgptr);
+    return rc;
+
+  case VERIFY_NOT_BLIND:
+    /* Check that no recipient of this message is "blind", that is, every envelope
+    recipient must be mentioned in either To: or Cc:. */
+
+    rc = verify_check_notblind();
+    if (rc != OK)
+      {
+      *log_msgptr = string_sprintf("bcc recipient detected");
+      if (smtp_return_error_details)
+        *user_msgptr = string_sprintf("Rejected after DATA: %s", *log_msgptr);
+      }
+    return rc;
+
+  /* The remaining verification tests check recipient and sender addresses,
+  either from the envelope or from the header. There are a number of
+  slash-separated options that are common to all of them. */
+
+  case VERIFY_HDR_SNDR:
+    verify_header_sender = TRUE;
+    break;
+
+  case VERIFY_SNDR:
+    /* In the case of a sender, this can optionally be followed by an address to use
+    in place of the actual sender (rare special-case requirement). */
+    {
+    uschar *s = ss + 6;
+    if (*s == 0)
+      verify_sender_address = sender_address;
+    else
+      {
+      while (isspace(*s)) s++;
+      if (*s++ != '=') goto BAD_VERIFY;
+      while (isspace(*s)) s++;
+      verify_sender_address = string_copy(s);
+      }
+    }
+    break;
+
+  case VERIFY_RCPT:
+    break;
+  }
+
+
+
+/* Remaining items are optional; they apply to sender and recipient
+verification, including "header sender" verification. */
+
+while ((ss = string_nextinlist(&list, &sep, big_buffer, big_buffer_size))
+      != NULL)
+  {
+  if (strcmpic(ss, US"defer_ok") == 0) defer_ok = TRUE;
+  else if (strcmpic(ss, US"no_details") == 0) no_details = TRUE;
+  else if (strcmpic(ss, US"success_on_redirect") == 0) success_on_redirect = TRUE;
+
+  /* These two old options are left for backwards compatibility */
+
+  else if (strcmpic(ss, US"callout_defer_ok") == 0)
+    {
+    callout_defer_ok = TRUE;
+    if (callout == -1) callout = CALLOUT_TIMEOUT_DEFAULT;
+    }
+
+  else if (strcmpic(ss, US"check_postmaster") == 0)
+     {
+     pm_mailfrom = US"";
+     if (callout == -1) callout = CALLOUT_TIMEOUT_DEFAULT;
+     }
+
+  /* The callout option has a number of sub-options, comma separated */
+
+  else if (strncmpic(ss, US"callout", 7) == 0)
+    {
+    callout = CALLOUT_TIMEOUT_DEFAULT;
+    ss += 7;
+    if (*ss != 0)
+      {
+      while (isspace(*ss)) ss++;
+      if (*ss++ == '=')
+        {
+        int optsep = ',';
+        uschar *opt;
+        uschar buffer[256];
+        while (isspace(*ss)) ss++;
+
+        while ((opt = string_nextinlist(&ss, &optsep, buffer, sizeof(buffer)))
+              != NULL)
+          {
+         callout_opt_t * op;
+         double period = 1.0F;
+
+         for (op= callout_opt_list; op->name; op++)
+           if (strncmpic(opt, op->name, Ustrlen(op->name)) == 0)
+             break;
+
+         verify_options |= op->flag;
+         if (op->has_option)
+           {
+           opt += Ustrlen(op->name);
+            while (isspace(*opt)) opt++;
+            if (*opt++ != '=')
+              {
+              *log_msgptr = string_sprintf("'=' expected after "
+                "\"%s\" in ACL verify condition \"%s\"", op->name, arg);
+              return ERROR;
+              }
+            while (isspace(*opt)) opt++;
+           }
+         if (op->timeval)
+           {
+            period = readconf_readtime(opt, 0, FALSE);
+            if (period < 0)
+              {
+              *log_msgptr = string_sprintf("bad time value in ACL condition "
+                "\"verify %s\"", arg);
+              return ERROR;
+              }
+           }
+
+         switch(op->value)
+           {
+            case CALLOUT_DEFER_OK:             callout_defer_ok = TRUE; break;
+            case CALLOUT_POSTMASTER:           pm_mailfrom = US"";     break;
+            case CALLOUT_FULLPOSTMASTER:       pm_mailfrom = US"";     break;
+            case CALLOUT_MAILFROM:
+              if (!verify_header_sender)
+                {
+                *log_msgptr = string_sprintf("\"mailfrom\" is allowed as a "
+                  "callout option only for verify=header_sender (detected in ACL "
+                  "condition \"%s\")", arg);
+                return ERROR;
+                }
+              se_mailfrom = string_copy(opt);
+             break;
+            case CALLOUT_POSTMASTER_MAILFROM:  pm_mailfrom = string_copy(opt); break;
+            case CALLOUT_MAXWAIT:              callout_overall = period;       break;
+            case CALLOUT_CONNECT:              callout_connect = period;       break;
+            case CALLOUT_TIME:                 callout = period;               break;
+           }
+          }
+        }
+      else
+        {
+        *log_msgptr = string_sprintf("'=' expected after \"callout\" in "
+          "ACL condition \"%s\"", arg);
+        return ERROR;
+        }
+      }
+    }
+
+  /* Option not recognized */
+
+  else
+    {
+    *log_msgptr = string_sprintf("unknown option \"%s\" in ACL "
+      "condition \"verify %s\"", ss, arg);
+    return ERROR;
+    }
+  }
+
+if ((verify_options & (vopt_callout_recipsender|vopt_callout_recippmaster)) ==
+      (vopt_callout_recipsender|vopt_callout_recippmaster))
+  {
+  *log_msgptr = US"only one of use_sender and use_postmaster can be set "
+    "for a recipient callout";
+  return ERROR;
+  }
+
+/* Handle sender-in-header verification. Default the user message to the log
+message if giving out verification details. */
+
+if (verify_header_sender)
+  {
+  int verrno;
+  rc = verify_check_header_address(user_msgptr, log_msgptr, callout,
+    callout_overall, callout_connect, se_mailfrom, pm_mailfrom, verify_options,
+    &verrno);
+  if (rc != OK)
+    {
+    *basic_errno = verrno;
+    if (smtp_return_error_details)
+      {
+      if (*user_msgptr == NULL && *log_msgptr != NULL)
+        *user_msgptr = string_sprintf("Rejected after DATA: %s", *log_msgptr);
+      if (rc == DEFER) acl_temp_details = TRUE;
+      }
+    }
+  }
+
+/* Handle a sender address. The default is to verify *the* sender address, but
+optionally a different address can be given, for special requirements. If the
+address is empty, we are dealing with a bounce message that has no sender, so
+we cannot do any checking. If the real sender address gets rewritten during
+verification (e.g. DNS widening), set the flag to stop it being rewritten again
+during message reception.
+
+A list of verified "sender" addresses is kept to try to avoid doing to much
+work repetitively when there are multiple recipients in a message and they all
+require sender verification. However, when callouts are involved, it gets too
+complicated because different recipients may require different callout options.
+Therefore, we always do a full sender verify when any kind of callout is
+specified. Caching elsewhere, for instance in the DNS resolver and in the
+callout handling, should ensure that this is not terribly inefficient. */
+
+else if (verify_sender_address != NULL)
+  {
+  if ((verify_options & (vopt_callout_recipsender|vopt_callout_recippmaster))
+       != 0)
+    {
+    *log_msgptr = US"use_sender or use_postmaster cannot be used for a "
+      "sender verify callout";
+    return ERROR;
+    }
+
+  sender_vaddr = verify_checked_sender(verify_sender_address);
+  if (sender_vaddr != NULL &&               /* Previously checked */
+      callout <= 0)                         /* No callout needed this time */
+    {
+    /* If the "routed" flag is set, it means that routing worked before, so
+    this check can give OK (the saved return code value, if set, belongs to a
+    callout that was done previously). If the "routed" flag is not set, routing
+    must have failed, so we use the saved return code. */
+
+    if (testflag(sender_vaddr, af_verify_routed)) rc = OK; else
+      {
+      rc = sender_vaddr->special_action;
+      *basic_errno = sender_vaddr->basic_errno;
+      }
+    HDEBUG(D_acl) debug_printf("using cached sender verify result\n");
+    }
+
+  /* Do a new verification, and cache the result. The cache is used to avoid
+  verifying the sender multiple times for multiple RCPTs when callouts are not
+  specified (see comments above).
+
+  The cache is also used on failure to give details in response to the first
+  RCPT that gets bounced for this reason. However, this can be suppressed by
+  the no_details option, which sets the flag that says "this detail has already
+  been sent". The cache normally contains just one address, but there may be
+  more in esoteric circumstances. */
+
+  else
+    {
+    BOOL routed = TRUE;
+    uschar *save_address_data = deliver_address_data;
+
+    sender_vaddr = deliver_make_addr(verify_sender_address, TRUE);
+    if (no_details) setflag(sender_vaddr, af_sverify_told);
+    if (verify_sender_address[0] != 0)
+      {
+      /* If this is the real sender address, save the unrewritten version
+      for use later in receive. Otherwise, set a flag so that rewriting the
+      sender in verify_address() does not update sender_address. */
+
+      if (verify_sender_address == sender_address)
+        sender_address_unrewritten = sender_address;
+      else
+        verify_options |= vopt_fake_sender;
+
+      if (success_on_redirect)
+        verify_options |= vopt_success_on_redirect;
+
+      /* The recipient, qualify, and expn options are never set in
+      verify_options. */
+
+      rc = verify_address(sender_vaddr, NULL, verify_options, callout,
+        callout_overall, callout_connect, se_mailfrom, pm_mailfrom, &routed);
+
+      HDEBUG(D_acl) debug_printf("----------- end verify ------------\n");
+
+      if (rc == OK)
+        {
+        if (Ustrcmp(sender_vaddr->address, verify_sender_address) != 0)
+          {
+          DEBUG(D_acl) debug_printf("sender %s verified ok as %s\n",
+            verify_sender_address, sender_vaddr->address);
+          }
+        else
+          {
+          DEBUG(D_acl) debug_printf("sender %s verified ok\n",
+            verify_sender_address);
+          }
+        }
+      else *basic_errno = sender_vaddr->basic_errno;
+      }
+    else rc = OK;  /* Null sender */
+
+    /* Cache the result code */
+
+    if (routed) setflag(sender_vaddr, af_verify_routed);
+    if (callout > 0) setflag(sender_vaddr, af_verify_callout);
+    sender_vaddr->special_action = rc;
+    sender_vaddr->next = sender_verified_list;
+    sender_verified_list = sender_vaddr;
+
+    /* Restore the recipient address data, which might have been clobbered by
+    the sender verification. */
+
+    deliver_address_data = save_address_data;
+    }
+
+  /* Put the sender address_data value into $sender_address_data */
+
+  sender_address_data = sender_vaddr->p.address_data;
+  }
+
+/* A recipient address just gets a straightforward verify; again we must handle
+the DEFER overrides. */
+
+else
+  {
+  address_item addr2;
+
+  if (success_on_redirect)
+    verify_options |= vopt_success_on_redirect;
+
+  /* We must use a copy of the address for verification, because it might
+  get rewritten. */
+
+  addr2 = *addr;
+  rc = verify_address(&addr2, NULL, verify_options|vopt_is_recipient, callout,
+    callout_overall, callout_connect, se_mailfrom, pm_mailfrom, NULL);
+  HDEBUG(D_acl) debug_printf("----------- end verify ------------\n");
+
+  *basic_errno = addr2.basic_errno;
+  *log_msgptr = addr2.message;
+  *user_msgptr = (addr2.user_message != NULL)?
+    addr2.user_message : addr2.message;
+
+  /* Allow details for temporary error if the address is so flagged. */
+  if (testflag((&addr2), af_pass_message)) acl_temp_details = TRUE;
+
+  /* Make $address_data visible */
+  deliver_address_data = addr2.p.address_data;
+  }
+
+/* We have a result from the relevant test. Handle defer overrides first. */
+
+if (rc == DEFER && (defer_ok ||
+   (callout_defer_ok && *basic_errno == ERRNO_CALLOUTDEFER)))
+  {
+  HDEBUG(D_acl) debug_printf("verify defer overridden by %s\n",
+    defer_ok? "defer_ok" : "callout_defer_ok");
+  rc = OK;
+  }
+
+/* If we've failed a sender, set up a recipient message, and point
+sender_verified_failed to the address item that actually failed. */
+
+if (rc != OK && verify_sender_address != NULL)
+  {
+  if (rc != DEFER)
+    {
+    *log_msgptr = *user_msgptr = US"Sender verify failed";
+    }
+  else if (*basic_errno != ERRNO_CALLOUTDEFER)
+    {
+    *log_msgptr = *user_msgptr = US"Could not complete sender verify";
+    }
+  else
+    {
+    *log_msgptr = US"Could not complete sender verify callout";
+    *user_msgptr = smtp_return_error_details? sender_vaddr->user_message :
+      *log_msgptr;
+    }
+
+  sender_verified_failed = sender_vaddr;
+  }
+
+/* Verifying an address messes up the values of $domain and $local_part,
+so reset them before returning if this is a RCPT ACL. */
+
+if (addr != NULL)
+  {
+  deliver_domain = addr->domain;
+  deliver_localpart = addr->local_part;
+  }
+return rc;
+
+/* Syntax errors in the verify argument come here. */
+
+BAD_VERIFY:
+*log_msgptr = string_sprintf("expected \"sender[=address]\", \"recipient\", "
+  "\"helo\", \"header_syntax\", \"header_sender\", \"header_names_ascii\" "
+  "or \"reverse_host_lookup\" at start of ACL condition "
+  "\"verify %s\"", arg);
+return ERROR;
+}
+
+
+
+
+/*************************************************
+*        Check argument for control= modifier    *
+*************************************************/
+
+/* Called from acl_check_condition() below
+
+Arguments:
+  arg         the argument string for control=
+  pptr        set to point to the terminating character
+  where       which ACL we are in
+  log_msgptr  for error messages
+
+Returns:      CONTROL_xxx value
+*/
+
+static int
+decode_control(uschar *arg, uschar **pptr, int where, uschar **log_msgptr)
+{
+int len;
+control_def *d;
+
+for (d = controls_list;
+     d < controls_list + sizeof(controls_list)/sizeof(control_def);
+     d++)
+  {
+  len = Ustrlen(d->name);
+  if (Ustrncmp(d->name, arg, len) == 0) break;
+  }
+
+if (d >= controls_list + sizeof(controls_list)/sizeof(control_def) ||
+   (arg[len] != 0 && (!d->has_option || arg[len] != '/')))
+  {
+  *log_msgptr = string_sprintf("syntax error in \"control=%s\"", arg);
+  return CONTROL_ERROR;
+  }
+
+*pptr = arg + len;
+return d->value;
+}
+
+
+
+
+/*************************************************
+*        Return a ratelimit error                *
+*************************************************/
+
+/* Called from acl_ratelimit() below
+
+Arguments:
+  log_msgptr  for error messages
+  format      format string
+  ...         supplementary arguments
+  ss          ratelimit option name
+  where       ACL_WHERE_xxxx indicating which ACL this is
+
+Returns:      ERROR
+*/
+
+static int
+ratelimit_error(uschar **log_msgptr, const char *format, ...)
+{
+va_list ap;
+uschar buffer[STRING_SPRINTF_BUFFER_SIZE];
+va_start(ap, format);
+if (!string_vformat(buffer, sizeof(buffer), format, ap))
+  log_write(0, LOG_MAIN|LOG_PANIC_DIE,
+    "string_sprintf expansion was longer than " SIZE_T_FMT, sizeof(buffer));
+va_end(ap);
+*log_msgptr = string_sprintf(
+  "error in arguments to \"ratelimit\" condition: %s", buffer);
+return ERROR;
+}
+
+
+
+
+/*************************************************
+*            Handle rate limiting                *
+*************************************************/
+
+/* Called by acl_check_condition() below to calculate the result
+of the ACL ratelimit condition.
+
+Note that the return value might be slightly unexpected: if the
+sender's rate is above the limit then the result is OK. This is
+similar to the dnslists condition, and is so that you can write
+ACL clauses like: defer ratelimit = 15 / 1h
+
+Arguments:
+  arg         the option string for ratelimit=
+  where       ACL_WHERE_xxxx indicating which ACL this is
+  log_msgptr  for error messages
+
+Returns:       OK        - Sender's rate is above limit
+               FAIL      - Sender's rate is below limit
+               DEFER     - Problem opening ratelimit database
+               ERROR     - Syntax error in options.
+*/
+
+static int
+acl_ratelimit(uschar *arg, int where, uschar **log_msgptr)
+{
+double limit, period, count;
+uschar *ss;
+uschar *key = NULL;
+uschar *unique = NULL;
+int sep = '/';
+BOOL leaky = FALSE, strict = FALSE, readonly = FALSE;
+BOOL noupdate = FALSE, badacl = FALSE;
+int mode = RATE_PER_WHAT;
+int old_pool, rc;
+tree_node **anchor, *t;
+open_db dbblock, *dbm;
+int dbdb_size;
+dbdata_ratelimit *dbd;
+dbdata_ratelimit_unique *dbdb;
+struct timeval tv;
+
+/* Parse the first two options and record their values in expansion
+variables. These variables allow the configuration to have informative
+error messages based on rate limits obtained from a table lookup. */
+
+/* First is the maximum number of messages per period / maximum burst
+size, which must be greater than or equal to zero. Zero is useful for
+rate measurement as opposed to rate limiting. */
+
+sender_rate_limit = string_nextinlist(&arg, &sep, NULL, 0);
+if (sender_rate_limit == NULL)
+  limit = -1.0;
+else
+  {
+  limit = Ustrtod(sender_rate_limit, &ss);
+  if (tolower(*ss) == 'k') { limit *= 1024.0; ss++; }
+  else if (tolower(*ss) == 'm') { limit *= 1024.0*1024.0; ss++; }
+  else if (tolower(*ss) == 'g') { limit *= 1024.0*1024.0*1024.0; ss++; }
+  }
+if (limit < 0.0 || *ss != '\0')
+  return ratelimit_error(log_msgptr,
+    "\"%s\" is not a positive number", sender_rate_limit);
+
+/* Second is the rate measurement period / exponential smoothing time
+constant. This must be strictly greater than zero, because zero leads to
+run-time division errors. */
+
+sender_rate_period = string_nextinlist(&arg, &sep, NULL, 0);
+if (sender_rate_period == NULL) period = -1.0;
+else period = readconf_readtime(sender_rate_period, 0, FALSE);
+if (period <= 0.0)
+  return ratelimit_error(log_msgptr,
+    "\"%s\" is not a time value", sender_rate_period);
+
+/* By default we are counting one of something, but the per_rcpt,
+per_byte, and count options can change this. */
+
+count = 1.0;
+
+/* Parse the other options. */
+
+while ((ss = string_nextinlist(&arg, &sep, big_buffer, big_buffer_size))
+       != NULL)
+  {
+  if (strcmpic(ss, US"leaky") == 0) leaky = TRUE;
+  else if (strcmpic(ss, US"strict") == 0) strict = TRUE;
+  else if (strcmpic(ss, US"noupdate") == 0) noupdate = TRUE;
+  else if (strcmpic(ss, US"readonly") == 0) readonly = TRUE;
+  else if (strcmpic(ss, US"per_cmd") == 0) RATE_SET(mode, PER_CMD);
+  else if (strcmpic(ss, US"per_conn") == 0)
+    {
+    RATE_SET(mode, PER_CONN);
+    if (where == ACL_WHERE_NOTSMTP || where == ACL_WHERE_NOTSMTP_START)
+      badacl = TRUE;
+    }
+  else if (strcmpic(ss, US"per_mail") == 0)
+    {
+    RATE_SET(mode, PER_MAIL);
+    if (where > ACL_WHERE_NOTSMTP) badacl = TRUE;
+    }
+  else if (strcmpic(ss, US"per_rcpt") == 0)
+    {
+    /* If we are running in the RCPT ACL, then we'll count the recipients
+    one by one, but if we are running when we have accumulated the whole
+    list then we'll add them all in one batch. */
+    if (where == ACL_WHERE_RCPT)
+      RATE_SET(mode, PER_RCPT);
+    else if (where >= ACL_WHERE_PREDATA && where <= ACL_WHERE_NOTSMTP)
+      RATE_SET(mode, PER_ALLRCPTS), count = (double)recipients_count;
+    else if (where == ACL_WHERE_MAIL || where > ACL_WHERE_NOTSMTP)
+      RATE_SET(mode, PER_RCPT), badacl = TRUE;
+    }
+  else if (strcmpic(ss, US"per_byte") == 0)
+    {
+    /* If we have not yet received the message data and there was no SIZE
+    declaration on the MAIL comand, then it's safe to just use a value of
+    zero and let the recorded rate decay as if nothing happened. */
+    RATE_SET(mode, PER_MAIL);
+    if (where > ACL_WHERE_NOTSMTP) badacl = TRUE;
+      else count = message_size < 0 ? 0.0 : (double)message_size;
+    }
+  else if (strcmpic(ss, US"per_addr") == 0)
+    {
+    RATE_SET(mode, PER_RCPT);
+    if (where != ACL_WHERE_RCPT) badacl = TRUE, unique = US"*";
+      else unique = string_sprintf("%s@%s", deliver_localpart, deliver_domain);
+    }
+  else if (strncmpic(ss, US"count=", 6) == 0)
+    {
+    uschar *e;
+    count = Ustrtod(ss+6, &e);
+    if (count < 0.0 || *e != '\0')
+      return ratelimit_error(log_msgptr,
+       "\"%s\" is not a positive number", ss);
+    }
+  else if (strncmpic(ss, US"unique=", 7) == 0)
+    unique = string_copy(ss + 7);
+  else if (key == NULL)
+    key = string_copy(ss);
+  else
+    key = string_sprintf("%s/%s", key, ss);
+  }
+
+/* Sanity check. When the badacl flag is set the update mode must either
+be readonly (which is the default if it is omitted) or, for backwards
+compatibility, a combination of noupdate and strict or leaky. */
+
+if (mode == RATE_PER_CLASH)
+  return ratelimit_error(log_msgptr, "conflicting per_* options");
+if (leaky + strict + readonly > 1)
+  return ratelimit_error(log_msgptr, "conflicting update modes");
+if (badacl && (leaky || strict) && !noupdate)
+  return ratelimit_error(log_msgptr,
+    "\"%s\" must not have /leaky or /strict option in %s ACL",
+    ratelimit_option_string[mode], acl_wherenames[where]);
+
+/* Set the default values of any unset options. In readonly mode we
+perform the rate computation without any increment so that its value
+decays to eventually allow over-limit senders through. */
+
+if (noupdate) readonly = TRUE, leaky = strict = FALSE;
+if (badacl) readonly = TRUE;
+if (readonly) count = 0.0;
+if (!strict && !readonly) leaky = TRUE;
+if (mode == RATE_PER_WHAT) mode = RATE_PER_MAIL;
+
+/* Create the lookup key. If there is no explicit key, use sender_host_address.
+If there is no sender_host_address (e.g. -bs or acl_not_smtp) then we simply
+omit it. The smoothing constant (sender_rate_period) and the per_xxx options
+are added to the key because they alter the meaning of the stored data. */
+
+if (key == NULL)
+  key = (sender_host_address == NULL)? US"" : sender_host_address;
+
+key = string_sprintf("%s/%s/%s%s",
+  sender_rate_period,
+  ratelimit_option_string[mode],
+  unique == NULL ? "" : "unique/",
+  key);
+
+HDEBUG(D_acl)
+  debug_printf("ratelimit condition count=%.0f %.1f/%s\n", count, limit, key);
+
+/* See if we have already computed the rate by looking in the relevant tree.
+For per-connection rate limiting, store tree nodes and dbdata in the permanent
+pool so that they survive across resets. In readonly mode we only remember the
+result for the rest of this command in case a later command changes it. After
+this bit of logic the code is independent of the per_* mode. */
+
+old_pool = store_pool;
+
+if (readonly)
+  anchor = &ratelimiters_cmd;
+else switch(mode) {
+case RATE_PER_CONN:
+  anchor = &ratelimiters_conn;
+  store_pool = POOL_PERM;
+  break;
+case RATE_PER_BYTE:
+case RATE_PER_MAIL:
+case RATE_PER_ALLRCPTS:
+  anchor = &ratelimiters_mail;
+  break;
+case RATE_PER_ADDR:
+case RATE_PER_CMD:
+case RATE_PER_RCPT:
+  anchor = &ratelimiters_cmd;
+  break;
+default:
+  anchor = NULL; /* silence an "unused" complaint */
+  log_write(0, LOG_MAIN|LOG_PANIC_DIE,
+    "internal ACL error: unknown ratelimit mode %d", mode);
+  break;
+}
+
+t = tree_search(*anchor, key);
+if (t != NULL)
+  {
+  dbd = t->data.ptr;
+  /* The following few lines duplicate some of the code below. */
+  rc = (dbd->rate < limit)? FAIL : OK;
+  store_pool = old_pool;
+  sender_rate = string_sprintf("%.1f", dbd->rate);
+  HDEBUG(D_acl)
+    debug_printf("ratelimit found pre-computed rate %s\n", sender_rate);
+  return rc;
+  }
+
+/* We aren't using a pre-computed rate, so get a previously recorded rate
+from the database, which will be updated and written back if required. */
+
+dbm = dbfn_open(US"ratelimit", O_RDWR, &dbblock, TRUE);
+if (dbm == NULL)
+  {
+  store_pool = old_pool;
+  sender_rate = NULL;
+  HDEBUG(D_acl) debug_printf("ratelimit database not available\n");
+  *log_msgptr = US"ratelimit database not available";
+  return DEFER;
+  }
+dbdb = dbfn_read_with_length(dbm, key, &dbdb_size);
+dbd = NULL;
+
+gettimeofday(&tv, NULL);
+
+if (dbdb != NULL)
+  {
+  /* Locate the basic ratelimit block inside the DB data. */
+  HDEBUG(D_acl) debug_printf("ratelimit found key in database\n");
+  dbd = &dbdb->dbd;
+
+  /* Forget the old Bloom filter if it is too old, so that we count each
+  repeating event once per period. We don't simply clear and re-use the old
+  filter because we want its size to change if the limit changes. Note that
+  we keep the dbd pointer for copying the rate into the new data block. */
+
+  if(unique != NULL && tv.tv_sec > dbdb->bloom_epoch + period)
+    {
+    HDEBUG(D_acl) debug_printf("ratelimit discarding old Bloom filter\n");
+    dbdb = NULL;
+    }
+
+  /* Sanity check. */
+
+  if(unique != NULL && dbdb_size < sizeof(*dbdb))
+    {
+    HDEBUG(D_acl) debug_printf("ratelimit discarding undersize Bloom filter\n");
+    dbdb = NULL;
+    }
+  }
+
+/* Allocate a new data block if the database lookup failed
+or the Bloom filter passed its age limit. */
+
+if (dbdb == NULL)
+  {
+  if (unique == NULL)
+    {
+    /* No Bloom filter. This basic ratelimit block is initialized below. */
+    HDEBUG(D_acl) debug_printf("ratelimit creating new rate data block\n");
+    dbdb_size = sizeof(*dbd);
+    dbdb = store_get(dbdb_size);
+    }
+  else
+    {
+    int extra;
+    HDEBUG(D_acl) debug_printf("ratelimit creating new Bloom filter\n");
+
+    /* See the long comment below for an explanation of the magic number 2.
+    The filter has a minimum size in case the rate limit is very small;
+    this is determined by the definition of dbdata_ratelimit_unique. */
+
+    extra = (int)limit * 2 - sizeof(dbdb->bloom);
+    if (extra < 0) extra = 0;
+    dbdb_size = sizeof(*dbdb) + extra;
+    dbdb = store_get(dbdb_size);
+    dbdb->bloom_epoch = tv.tv_sec;
+    dbdb->bloom_size = sizeof(dbdb->bloom) + extra;
+    memset(dbdb->bloom, 0, dbdb->bloom_size);
+
+    /* Preserve any basic ratelimit data (which is our longer-term memory)
+    by copying it from the discarded block. */
+
+    if (dbd != NULL)
+      {
+      dbdb->dbd = *dbd;
+      dbd = &dbdb->dbd;
+      }
+    }
+  }
+
+/* If we are counting unique events, find out if this event is new or not.
+If the client repeats the event during the current period then it should be
+counted. We skip this code in readonly mode for efficiency, because any
+changes to the filter will be discarded and because count is already set to
+zero. */
+
+if (unique != NULL && !readonly)
+  {
+  /* We identify unique events using a Bloom filter. (You can find my
+  notes on Bloom filters at http://fanf.livejournal.com/81696.html)
+  With the per_addr option, an "event" is a recipient address, though the
+  user can use the unique option to define their own events. We only count
+  an event if we have not seen it before.
+
+  We size the filter according to the rate limit, which (in leaky mode)
+  is the limit on the population of the filter. We allow 16 bits of space
+  per entry (see the construction code above) and we set (up to) 8 of them
+  when inserting an element (see the loop below). The probability of a false
+  positive (an event we have not seen before but which we fail to count) is
+
+    size    = limit * 16
+    numhash = 8
+    allzero = exp(-numhash * pop / size)
+            = exp(-0.5 * pop / limit)
+    fpr     = pow(1 - allzero, numhash)
+
+  For senders at the limit the fpr is      0.06%    or  1 in 1700
+  and for senders at half the limit it is  0.0006%  or  1 in 170000
+
+  In strict mode the Bloom filter can fill up beyond the normal limit, in
+  which case the false positive rate will rise. This means that the
+  measured rate for very fast senders can bogusly drop off after a while.
+
+  At twice the limit, the fpr is  2.5%  or  1 in 40
+  At four times the limit, it is  31%   or  1 in 3.2
+
+  It takes ln(pop/limit) periods for an over-limit burst of pop events to
+  decay below the limit, and if this is more than one then the Bloom filter
+  will be discarded before the decay gets that far. The false positive rate
+  at this threshold is 9.3% or 1 in 10.7. */
+
+  BOOL seen;
+  unsigned n, hash, hinc;
+  uschar md5sum[16];
+  md5 md5info;
+
+  /* Instead of using eight independent hash values, we combine two values
+  using the formula h1 + n * h2. This does not harm the Bloom filter's
+  performance, and means the amount of hash we need is independent of the
+  number of bits we set in the filter. */
+
+  md5_start(&md5info);
+  md5_end(&md5info, unique, Ustrlen(unique), md5sum);
+  hash = md5sum[0] | md5sum[1] << 8 | md5sum[2] << 16 | md5sum[3] << 24;
+  hinc = md5sum[4] | md5sum[5] << 8 | md5sum[6] << 16 | md5sum[7] << 24;
+
+  /* Scan the bits corresponding to this event. A zero bit means we have
+  not seen it before. Ensure all bits are set to record this event. */
+
+  HDEBUG(D_acl) debug_printf("ratelimit checking uniqueness of %s\n", unique);
+
+  seen = TRUE;
+  for (n = 0; n < 8; n++, hash += hinc)
+    {
+    int bit = 1 << (hash % 8);
+    int byte = (hash / 8) % dbdb->bloom_size;
+    if ((dbdb->bloom[byte] & bit) == 0)
+      {
+      dbdb->bloom[byte] |= bit;
+      seen = FALSE;
+      }
+    }
+
+  /* If this event has occurred before, do not count it. */
+
+  if (seen)
+    {
+    HDEBUG(D_acl) debug_printf("ratelimit event found in Bloom filter\n");
+    count = 0.0;
+    }
+  else
+    HDEBUG(D_acl) debug_printf("ratelimit event added to Bloom filter\n");
+  }
+
+/* If there was no previous ratelimit data block for this key, initialize
+the new one, otherwise update the block from the database. The initial rate
+is what would be computed by the code below for an infinite interval. */
+
+if (dbd == NULL)
+  {
+  HDEBUG(D_acl) debug_printf("ratelimit initializing new key's rate data\n");
+  dbd = &dbdb->dbd;
+  dbd->time_stamp = tv.tv_sec;
+  dbd->time_usec = tv.tv_usec;
+  dbd->rate = count;
+  }
+else
+  {
+  /* The smoothed rate is computed using an exponentially weighted moving
+  average adjusted for variable sampling intervals. The standard EWMA for
+  a fixed sampling interval is:  f'(t) = (1 - a) * f(t) + a * f'(t - 1)
+  where f() is the measured value and f'() is the smoothed value.
+
+  Old data decays out of the smoothed value exponentially, such that data n
+  samples old is multiplied by a^n. The exponential decay time constant p
+  is defined such that data p samples old is multiplied by 1/e, which means
+  that a = exp(-1/p). We can maintain the same time constant for a variable
+  sampling interval i by using a = exp(-i/p).
+
+  The rate we are measuring is messages per period, suitable for directly
+  comparing with the limit. The average rate between now and the previous
+  message is period / interval, which we feed into the EWMA as the sample.
+
+  It turns out that the number of messages required for the smoothed rate
+  to reach the limit when they are sent in a burst is equal to the limit.
+  This can be seen by analysing the value of the smoothed rate after N
+  messages sent at even intervals. Let k = (1 - a) * p/i
+
+    rate_1 = (1 - a) * p/i + a * rate_0
+           = k + a * rate_0
+    rate_2 = k + a * rate_1
+           = k + a * k + a^2 * rate_0
+    rate_3 = k + a * k + a^2 * k + a^3 * rate_0
+    rate_N = rate_0 * a^N + k * SUM(x=0..N-1)(a^x)
+           = rate_0 * a^N + k * (1 - a^N) / (1 - a)
+           = rate_0 * a^N + p/i * (1 - a^N)
+
+  When N is large, a^N -> 0 so rate_N -> p/i as desired.
+
+    rate_N = p/i + (rate_0 - p/i) * a^N
+    a^N = (rate_N - p/i) / (rate_0 - p/i)
+    N * -i/p = log((rate_N - p/i) / (rate_0 - p/i))
+    N = p/i * log((rate_0 - p/i) / (rate_N - p/i))
+
+  Numerical analysis of the above equation, setting the computed rate to
+  increase from rate_0 = 0 to rate_N = limit, shows that for large sending
+  rates, p/i, the number of messages N = limit. So limit serves as both the
+  maximum rate measured in messages per period, and the maximum number of
+  messages that can be sent in a fast burst. */
+
+  double this_time = (double)tv.tv_sec
+                   + (double)tv.tv_usec / 1000000.0;
+  double prev_time = (double)dbd->time_stamp
+                   + (double)dbd->time_usec / 1000000.0;
+
+  /* We must avoid division by zero, and deal gracefully with the clock going
+  backwards. If we blunder ahead when time is in reverse then the computed
+  rate will be bogus. To be safe we clamp interval to a very small number. */
+
+  double interval = this_time - prev_time <= 0.0 ? 1e-9
+                  : this_time - prev_time;
+
+  double i_over_p = interval / period;
+  double a = exp(-i_over_p);
+
+  /* Combine the instantaneous rate (period / interval) with the previous rate
+  using the smoothing factor a. In order to measure sized events, multiply the
+  instantaneous rate by the count of bytes or recipients etc. */
+
+  dbd->time_stamp = tv.tv_sec;
+  dbd->time_usec = tv.tv_usec;
+  dbd->rate = (1 - a) * count / i_over_p + a * dbd->rate;
+
+  /* When events are very widely spaced the computed rate tends towards zero.
+  Although this is accurate it turns out not to be useful for our purposes,
+  especially when the first event after a long silence is the start of a spam
+  run. A more useful model is that the rate for an isolated event should be the
+  size of the event per the period size, ignoring the lack of events outside
+  the current period and regardless of where the event falls in the period. So,
+  if the interval was so long that the calculated rate is unhelpfully small, we
+  re-intialize the rate. In the absence of higher-rate bursts, the condition
+  below is true if the interval is greater than the period. */
+
+  if (dbd->rate < count) dbd->rate = count;
+  }
+
+/* Clients sending at the limit are considered to be over the limit.
+This matters for edge cases such as a limit of zero, when the client
+should be completely blocked. */
+
+rc = (dbd->rate < limit)? FAIL : OK;
+
+/* Update the state if the rate is low or if we are being strict. If we
+are in leaky mode and the sender's rate is too high, we do not update
+the recorded rate in order to avoid an over-aggressive sender's retry
+rate preventing them from getting any email through. If readonly is set,
+neither leaky nor strict are set, so we do not do any updates. */
+
+if ((rc == FAIL && leaky) || strict)
+  {
+  dbfn_write(dbm, key, dbdb, dbdb_size);
+  HDEBUG(D_acl) debug_printf("ratelimit db updated\n");
+  }
+else
+  {
+  HDEBUG(D_acl) debug_printf("ratelimit db not updated: %s\n",
+    readonly? "readonly mode" : "over the limit, but leaky");
+  }
+
+dbfn_close(dbm);
+
+/* Store the result in the tree for future reference. */
+
+t = store_get(sizeof(tree_node) + Ustrlen(key));
+t->data.ptr = dbd;
+Ustrcpy(t->name, key);
+(void)tree_insertnode(anchor, t);
+
+/* We create the formatted version of the sender's rate very late in
+order to ensure that it is done using the correct storage pool. */
+
+store_pool = old_pool;
+sender_rate = string_sprintf("%.1f", dbd->rate);
+
+HDEBUG(D_acl)
+  debug_printf("ratelimit computed rate %s\n", sender_rate);
+
+return rc;
+}
+
+
+
+/*************************************************
+*            The udpsend ACL modifier            *
+*************************************************/
+
+/* Called by acl_check_condition() below.
+
+Arguments:
+  arg          the option string for udpsend=
+  log_msgptr   for error messages
+
+Returns:       OK        - Completed.
+               DEFER     - Problem with DNS lookup.
+               ERROR     - Syntax error in options.
+*/
+
+static int
+acl_udpsend(uschar *arg, uschar **log_msgptr)
+{
+int sep = 0;
+uschar *hostname;
+uschar *portstr;
+uschar *portend;
+host_item *h;
+int portnum;
+int len;
+int r, s;
+uschar * errstr;
+
+hostname = string_nextinlist(&arg, &sep, NULL, 0);
+portstr = string_nextinlist(&arg, &sep, NULL, 0);
+
+if (hostname == NULL)
+  {
+  *log_msgptr = US"missing destination host in \"udpsend\" modifier";
+  return ERROR;
+  }
+if (portstr == NULL)
+  {
+  *log_msgptr = US"missing destination port in \"udpsend\" modifier";
+  return ERROR;
+  }
+if (arg == NULL)
+  {
+  *log_msgptr = US"missing datagram payload in \"udpsend\" modifier";
+  return ERROR;
+  }
+portnum = Ustrtol(portstr, &portend, 10);
+if (*portend != '\0')
+  {
+  *log_msgptr = US"bad destination port in \"udpsend\" modifier";
+  return ERROR;
+  }
+
+/* Make a single-item host list. */
+h = store_get(sizeof(host_item));
+memset(h, 0, sizeof(host_item));
+h->name = hostname;
+h->port = portnum;
+h->mx = MX_NONE;
+
+if (string_is_ip_address(hostname, NULL))
+  h->address = hostname, r = HOST_FOUND;
+else
+  r = host_find_byname(h, NULL, 0, NULL, FALSE);
+if (r == HOST_FIND_FAILED || r == HOST_FIND_AGAIN)
+  {
+  *log_msgptr = US"DNS lookup failed in \"udpsend\" modifier";
+  return DEFER;
+  }
+
+HDEBUG(D_acl)
+  debug_printf("udpsend [%s]:%d %s\n", h->address, portnum, arg);
+
+r = s = ip_connectedsocket(SOCK_DGRAM, h->address, portnum, portnum,
+               1, NULL, &errstr);
+if (r < 0) goto defer;
+len = Ustrlen(arg);
+r = send(s, arg, len, 0);
+if (r < 0)
+  {
+  errstr = US strerror(errno);
+  close(s);
+  goto defer;
+  }
+close(s);
+if (r < len)
+  {
+  *log_msgptr =
+    string_sprintf("\"udpsend\" truncated from %d to %d octets", len, r);
+  return DEFER;
+  }
+
+HDEBUG(D_acl)
+  debug_printf("udpsend %d bytes\n", r);
+
+return OK;
+
+defer:
+*log_msgptr = string_sprintf("\"udpsend\" failed: %s", errstr);
+return DEFER;
+}
+
+
+
+/*************************************************
+*   Handle conditions/modifiers on an ACL item   *
+*************************************************/
+
+/* Called from acl_check() below.
+
+Arguments:
+  verb         ACL verb
+  cb           ACL condition block - if NULL, result is OK
+  where        where called from
+  addr         the address being checked for RCPT, or NULL
+  level        the nesting level
+  epp          pointer to pass back TRUE if "endpass" encountered
+                 (applies only to "accept" and "discard")
+  user_msgptr  user message pointer
+  log_msgptr   log message pointer
+  basic_errno  pointer to where to put verify error
+
+Returns:       OK        - all conditions are met
+               DISCARD   - an "acl" condition returned DISCARD - only allowed
+                             for "accept" or "discard" verbs
+               FAIL      - at least one condition fails
+               FAIL_DROP - an "acl" condition returned FAIL_DROP
+               DEFER     - can't tell at the moment (typically, lookup defer,
+                             but can be temporary callout problem)
+               ERROR     - ERROR from nested ACL or expansion failure or other
+                             error
+*/
+
+static int
+acl_check_condition(int verb, acl_condition_block *cb, int where,
+  address_item *addr, int level, BOOL *epp, uschar **user_msgptr,
+  uschar **log_msgptr, int *basic_errno)
+{
+uschar *user_message = NULL;
+uschar *log_message = NULL;
+uschar *debug_tag = NULL;
+uschar *debug_opts = NULL;
+uschar *p = NULL;
+int rc = OK;
+#ifdef WITH_CONTENT_SCAN
+int sep = '/';
+#endif
+
+for (; cb != NULL; cb = cb->next)
+  {
+  uschar *arg;
+  int control_type;
+
+  /* The message and log_message items set up messages to be used in
+  case of rejection. They are expanded later. */
+
+  if (cb->type == ACLC_MESSAGE)
+    {
+    HDEBUG(D_acl) debug_printf("  message: %s\n", cb->arg);
+    user_message = cb->arg;
+    continue;
+    }
+
+  if (cb->type == ACLC_LOG_MESSAGE)
+    {
+    HDEBUG(D_acl) debug_printf("l_message: %s\n", cb->arg);
+    log_message = cb->arg;
+    continue;
+    }
+
+  /* The endpass "condition" just sets a flag to show it occurred. This is
+  checked at compile time to be on an "accept" or "discard" item. */
+
+  if (cb->type == ACLC_ENDPASS)
+    {
+    *epp = TRUE;
+    continue;
+    }
+
+  /* For other conditions and modifiers, the argument is expanded now for some
+  of them, but not for all, because expansion happens down in some lower level
+  checking functions in some cases. */
+
+  if (cond_expand_at_top[cb->type])
+    {
+    arg = expand_string(cb->arg);
+    if (arg == NULL)
+      {
+      if (expand_string_forcedfail) continue;
+      *log_msgptr = string_sprintf("failed to expand ACL string \"%s\": %s",
+        cb->arg, expand_string_message);
+      return search_find_defer? DEFER : ERROR;
+      }
+    }
+  else arg = cb->arg;
+
+  /* Show condition, and expanded condition if it's different */
+
+  HDEBUG(D_acl)
+    {
+    int lhswidth = 0;
+    debug_printf("check %s%s %n",
+      (!cond_modifiers[cb->type] && cb->u.negated)? "!":"",
+      conditions[cb->type], &lhswidth);
+
+    if (cb->type == ACLC_SET)
+      {
+      debug_printf("acl_%s ", cb->u.varname);
+      lhswidth += 5 + Ustrlen(cb->u.varname);
+      }
+
+    debug_printf("= %s\n", cb->arg);
+
+    if (arg != cb->arg)
+      debug_printf("%.*s= %s\n", lhswidth,
+      US"                             ", CS arg);
+    }
+
+  /* Check that this condition makes sense at this time */
+
+  if ((cond_forbids[cb->type] & (1 << where)) != 0)
+    {
+    *log_msgptr = string_sprintf("cannot %s %s condition in %s ACL",
+      cond_modifiers[cb->type]? "use" : "test",
+      conditions[cb->type], acl_wherenames[where]);
+    return ERROR;
+    }
+
+  /* Run the appropriate test for each condition, or take the appropriate
+  action for the remaining modifiers. */
+
+  switch(cb->type)
+    {
+    case ACLC_ADD_HEADER:
+    setup_header(arg);
+    break;
+
+    /* A nested ACL that returns "discard" makes sense only for an "accept" or
+    "discard" verb. */
+
+    case ACLC_ACL:
+      rc = acl_check_wargs(where, addr, arg, level+1, user_msgptr, log_msgptr);
+      if (rc == DISCARD && verb != ACL_ACCEPT && verb != ACL_DISCARD)
+        {
+        *log_msgptr = string_sprintf("nested ACL returned \"discard\" for "
+          "\"%s\" command (only allowed with \"accept\" or \"discard\")",
+          verbs[verb]);
+        return ERROR;
+        }
+    break;
+
+    case ACLC_AUTHENTICATED:
+    rc = (sender_host_authenticated == NULL)? FAIL :
+      match_isinlist(sender_host_authenticated, &arg, 0, NULL, NULL, MCL_STRING,
+        TRUE, NULL);
+    break;
+
+    #ifdef EXPERIMENTAL_BRIGHTMAIL
+    case ACLC_BMI_OPTIN:
+      {
+      int old_pool = store_pool;
+      store_pool = POOL_PERM;
+      bmi_current_optin = string_copy(arg);
+      store_pool = old_pool;
+      }
+    break;
+    #endif
+
+    case ACLC_CONDITION:
+    /* The true/false parsing here should be kept in sync with that used in
+    expand.c when dealing with ECOND_BOOL so that we don't have too many
+    different definitions of what can be a boolean. */
+    if (*arg == '-'
+       ? Ustrspn(arg+1, "0123456789") == Ustrlen(arg+1)    /* Negative number */
+       : Ustrspn(arg,   "0123456789") == Ustrlen(arg))     /* Digits, or empty */
+      rc = (Uatoi(arg) == 0)? FAIL : OK;
+    else
+      rc = (strcmpic(arg, US"no") == 0 ||
+            strcmpic(arg, US"false") == 0)? FAIL :
+           (strcmpic(arg, US"yes") == 0 ||
+            strcmpic(arg, US"true") == 0)? OK : DEFER;
+    if (rc == DEFER)
+      *log_msgptr = string_sprintf("invalid \"condition\" value \"%s\"", arg);
+    break;
+
+    case ACLC_CONTINUE:    /* Always succeeds */
+    break;
+
+    case ACLC_CONTROL:
+    control_type = decode_control(arg, &p, where, log_msgptr);
+
+    /* Check if this control makes sense at this time */
+
+    if ((control_forbids[control_type] & (1 << where)) != 0)
+      {
+      *log_msgptr = string_sprintf("cannot use \"control=%s\" in %s ACL",
+        controls[control_type], acl_wherenames[where]);
+      return ERROR;
+      }
+
+    switch(control_type)
+      {
+      case CONTROL_AUTH_UNADVERTISED:
+      allow_auth_unadvertised = TRUE;
+      break;
+
+      #ifdef EXPERIMENTAL_BRIGHTMAIL
+      case CONTROL_BMI_RUN:
+      bmi_run = 1;
+      break;
+      #endif
+
+      #ifndef DISABLE_DKIM
+      case CONTROL_DKIM_VERIFY:
+      dkim_disable_verify = TRUE;
+      #ifdef EXPERIMENTAL_DMARC
+      /* Since DKIM was blocked, skip DMARC too */
+      dmarc_disable_verify = TRUE;
+      dmarc_enable_forensic = FALSE;
+      #endif
+      break;
+      #endif
+
+      #ifdef EXPERIMENTAL_DMARC
+      case CONTROL_DMARC_VERIFY:
+      dmarc_disable_verify = TRUE;
+      break;
+
+      case CONTROL_DMARC_FORENSIC:
+      dmarc_enable_forensic = TRUE;
+      break;
+      #endif
+
+      case CONTROL_DSCP:
+      if (*p == '/')
+        {
+        int fd, af, level, optname, value;
+        /* If we are acting on stdin, the setsockopt may fail if stdin is not
+        a socket; we can accept that, we'll just debug-log failures anyway. */
+        fd = fileno(smtp_in);
+        af = ip_get_address_family(fd);
+        if (af < 0)
+          {
+          HDEBUG(D_acl)
+            debug_printf("smtp input is probably not a socket [%s], not setting DSCP\n",
+                strerror(errno));
+          break;
+          }
+        if (dscp_lookup(p+1, af, &level, &optname, &value))
+          {
+          if (setsockopt(fd, level, optname, &value, sizeof(value)) < 0)
+            {
+            HDEBUG(D_acl) debug_printf("failed to set input DSCP[%s]: %s\n",
+                p+1, strerror(errno));
+            }
+          else
+            {
+            HDEBUG(D_acl) debug_printf("set input DSCP to \"%s\"\n", p+1);
+            }
+          }
+        else
+          {
+          *log_msgptr = string_sprintf("unrecognised DSCP value in \"control=%s\"", arg);
+          return ERROR;
+          }
+        }
+      else
+        {
+        *log_msgptr = string_sprintf("syntax error in \"control=%s\"", arg);
+        return ERROR;
+        }
+      break;
+
+      case CONTROL_ERROR:
+      return ERROR;
+
+      case CONTROL_CASEFUL_LOCAL_PART:
+      deliver_localpart = addr->cc_local_part;
+      break;
+
+      case CONTROL_CASELOWER_LOCAL_PART:
+      deliver_localpart = addr->lc_local_part;
+      break;
+
+      case CONTROL_ENFORCE_SYNC:
+      smtp_enforce_sync = TRUE;
+      break;
+
+      case CONTROL_NO_ENFORCE_SYNC:
+      smtp_enforce_sync = FALSE;
+      break;
+
+      #ifdef WITH_CONTENT_SCAN
+      case CONTROL_NO_MBOX_UNSPOOL:
+      no_mbox_unspool = TRUE;
+      break;
+      #endif
+
+      case CONTROL_NO_MULTILINE:
+      no_multiline_responses = TRUE;
+      break;
+
+      case CONTROL_NO_PIPELINING:
+      pipelining_enable = FALSE;
+      break;
+
+      case CONTROL_NO_DELAY_FLUSH:
+      disable_delay_flush = TRUE;
+      break;
+
+      case CONTROL_NO_CALLOUT_FLUSH:
+      disable_callout_flush = TRUE;
+      break;
+
+      case CONTROL_FAKEREJECT:
+      cancel_cutthrough_connection("fakereject");
+      case CONTROL_FAKEDEFER:
+      fake_response = (control_type == CONTROL_FAKEDEFER) ? DEFER : FAIL;
+      if (*p == '/')
+        {
+        uschar *pp = p + 1;
+        while (*pp != 0) pp++;
+        fake_response_text = expand_string(string_copyn(p+1, pp-p-1));
+        p = pp;
+        }
+       else
+        {
+        /* Explicitly reset to default string */
+        fake_response_text = US"Your message has been rejected but is being kept for evaluation.\nIf it was a legitimate message, it may still be delivered to the target recipient(s).";
+        }
+      break;
+
+      case CONTROL_FREEZE:
+      deliver_freeze = TRUE;
+      deliver_frozen_at = time(NULL);
+      freeze_tell = freeze_tell_config;       /* Reset to configured value */
+      if (Ustrncmp(p, "/no_tell", 8) == 0)
+        {
+        p += 8;
+        freeze_tell = NULL;
+        }
+      if (*p != 0)
+        {
+        *log_msgptr = string_sprintf("syntax error in \"control=%s\"", arg);
+        return ERROR;
+        }
+      cancel_cutthrough_connection("item frozen");
+      break;
+
+      case CONTROL_QUEUE_ONLY:
+      queue_only_policy = TRUE;
+      cancel_cutthrough_connection("queueing forced");
+      break;
+
+      case CONTROL_SUBMISSION:
+      originator_name = US"";
+      submission_mode = TRUE;
+      while (*p == '/')
+        {
+        if (Ustrncmp(p, "/sender_retain", 14) == 0)
+          {
+          p += 14;
+          active_local_sender_retain = TRUE;
+          active_local_from_check = FALSE;
+          }
+        else if (Ustrncmp(p, "/domain=", 8) == 0)
+          {
+          uschar *pp = p + 8;
+          while (*pp != 0 && *pp != '/') pp++;
+          submission_domain = string_copyn(p+8, pp-p-8);
+          p = pp;
+          }
+        /* The name= option must be last, because it swallows the rest of
+        the string. */
+        else if (Ustrncmp(p, "/name=", 6) == 0)
+          {
+          uschar *pp = p + 6;
+          while (*pp != 0) pp++;
+          submission_name = string_copy(parse_fix_phrase(p+6, pp-p-6,
+            big_buffer, big_buffer_size));
+          p = pp;
+          }
+        else break;
+        }
+      if (*p != 0)
+        {
+        *log_msgptr = string_sprintf("syntax error in \"control=%s\"", arg);
+        return ERROR;
+        }
+      break;
+
+      case CONTROL_DEBUG:
+      while (*p == '/')
+        {
+        if (Ustrncmp(p, "/tag=", 5) == 0)
+          {
+          uschar *pp = p + 5;
+          while (*pp != '\0' && *pp != '/') pp++;
+          debug_tag = string_copyn(p+5, pp-p-5);
+          p = pp;
+          }
+        else if (Ustrncmp(p, "/opts=", 6) == 0)
+          {
+          uschar *pp = p + 6;
+          while (*pp != '\0' && *pp != '/') pp++;
+          debug_opts = string_copyn(p+6, pp-p-6);
+          p = pp;
+          }
+        }
+        debug_logging_activate(debug_tag, debug_opts);
+      break;
+
+      case CONTROL_SUPPRESS_LOCAL_FIXUPS:
+      suppress_local_fixups = TRUE;
+      break;
+
+      case CONTROL_CUTTHROUGH_DELIVERY:
+      if (deliver_freeze)
+        *log_msgptr = US"frozen";
+      else if (queue_only_policy)
+        *log_msgptr = US"queue-only";
+      else if (fake_response == FAIL)
+        *log_msgptr = US"fakereject";
+      else
+       {
+       cutthrough_delivery = TRUE;
+       break;
+       }
+      *log_msgptr = string_sprintf("\"control=%s\" on %s item",
+                                   arg, *log_msgptr);
+      return ERROR;
+      }
+    break;
+
+    #ifdef EXPERIMENTAL_DCC
+    case ACLC_DCC:
+      {
+      /* Seperate the regular expression and any optional parameters. */
+      uschar *ss = string_nextinlist(&arg, &sep, big_buffer, big_buffer_size);
+      /* Run the dcc backend. */
+      rc = dcc_process(&ss);
+      /* Modify return code based upon the existance of options. */
+      while ((ss = string_nextinlist(&arg, &sep, big_buffer, big_buffer_size))
+            != NULL) {
+        if (strcmpic(ss, US"defer_ok") == 0 && rc == DEFER)
+          {
+          /* FAIL so that the message is passed to the next ACL */
+          rc = FAIL;
+          }
+        }
+      }
+    break;
+    #endif
+
+    #ifdef WITH_CONTENT_SCAN
+    case ACLC_DECODE:
+    rc = mime_decode(&arg);
+    break;
+    #endif
+
+    case ACLC_DELAY:
+      {
+      int delay = readconf_readtime(arg, 0, FALSE);
+      if (delay < 0)
+        {
+        *log_msgptr = string_sprintf("syntax error in argument for \"delay\" "
+          "modifier: \"%s\" is not a time value", arg);
+        return ERROR;
+        }
+      else
+        {
+        HDEBUG(D_acl) debug_printf("delay modifier requests %d-second delay\n",
+          delay);
+        if (host_checking)
+          {
+          HDEBUG(D_acl)
+            debug_printf("delay skipped in -bh checking mode\n");
+          }
+
+        /* It appears to be impossible to detect that a TCP/IP connection has
+        gone away without reading from it. This means that we cannot shorten
+        the delay below if the client goes away, because we cannot discover
+        that the client has closed its end of the connection. (The connection
+        is actually in a half-closed state, waiting for the server to close its
+        end.) It would be nice to be able to detect this state, so that the
+        Exim process is not held up unnecessarily. However, it seems that we
+        can't. The poll() function does not do the right thing, and in any case
+        it is not always available.
+
+        NOTE 1: If ever this state of affairs changes, remember that we may be
+        dealing with stdin/stdout here, in addition to TCP/IP connections.
+        Also, delays may be specified for non-SMTP input, where smtp_out and
+        smtp_in will be NULL. Whatever is done must work in all cases.
+
+        NOTE 2: The added feature of flushing the output before a delay must
+        apply only to SMTP input. Hence the test for smtp_out being non-NULL.
+        */
+
+        else
+          {
+          if (smtp_out != NULL && !disable_delay_flush) mac_smtp_fflush();
+          while (delay > 0) delay = sleep(delay);
+          }
+        }
+      }
+    break;
+
+    #ifdef WITH_OLD_DEMIME
+    case ACLC_DEMIME:
+      rc = demime(&arg);
+    break;
+    #endif
+
+    #ifndef DISABLE_DKIM
+    case ACLC_DKIM_SIGNER:
+    if (dkim_cur_signer != NULL)
+      rc = match_isinlist(dkim_cur_signer,
+                          &arg,0,NULL,NULL,MCL_STRING,TRUE,NULL);
+    else
+       rc = FAIL;
+    break;
+
+    case ACLC_DKIM_STATUS:
+    rc = match_isinlist(dkim_exim_expand_query(DKIM_VERIFY_STATUS),
+                        &arg,0,NULL,NULL,MCL_STRING,TRUE,NULL);
+    break;
+    #endif
+
+    #ifdef EXPERIMENTAL_DMARC
+    case ACLC_DMARC_STATUS:
+    if (!dmarc_has_been_checked)
+      dmarc_process();
+    dmarc_has_been_checked = TRUE;
+    /* used long way of dmarc_exim_expand_query() in case we need more
+     * view into the process in the future. */
+    rc = match_isinlist(dmarc_exim_expand_query(DMARC_VERIFY_STATUS),
+                        &arg,0,NULL,NULL,MCL_STRING,TRUE,NULL);
+    break;
+    #endif
+
+    case ACLC_DNSLISTS:
+    rc = verify_check_dnsbl(&arg);
+    break;
+
+    case ACLC_DOMAINS:
+    rc = match_isinlist(addr->domain, &arg, 0, &domainlist_anchor,
+      addr->domain_cache, MCL_DOMAIN, TRUE, &deliver_domain_data);
+    break;
+
+    /* The value in tls_cipher is the full cipher name, for example,
+    TLSv1:DES-CBC3-SHA:168, whereas the values to test for are just the
+    cipher names such as DES-CBC3-SHA. But program defensively. We don't know
+    what may in practice come out of the SSL library - which at the time of
+    writing is poorly documented. */
+
+    case ACLC_ENCRYPTED:
+    if (tls_in.cipher == NULL) rc = FAIL; else
+      {
+      uschar *endcipher = NULL;
+      uschar *cipher = Ustrchr(tls_in.cipher, ':');
+      if (cipher == NULL) cipher = tls_in.cipher; else
+        {
+        endcipher = Ustrchr(++cipher, ':');
+        if (endcipher != NULL) *endcipher = 0;
+        }
+      rc = match_isinlist(cipher, &arg, 0, NULL, NULL, MCL_STRING, TRUE, NULL);
+      if (endcipher != NULL) *endcipher = ':';
+      }
+    break;
+
+    /* Use verify_check_this_host() instead of verify_check_host() so that
+    we can pass over &host_data to catch any looked up data. Once it has been
+    set, it retains its value so that it's still there if another ACL verb
+    comes through here and uses the cache. However, we must put it into
+    permanent store in case it is also expected to be used in a subsequent
+    message in the same SMTP connection. */
+
+    case ACLC_HOSTS:
+    rc = verify_check_this_host(&arg, sender_host_cache, NULL,
+      (sender_host_address == NULL)? US"" : sender_host_address, &host_data);
+    if (host_data != NULL) host_data = string_copy_malloc(host_data);
+    break;
+
+    case ACLC_LOCAL_PARTS:
+    rc = match_isinlist(addr->cc_local_part, &arg, 0,
+      &localpartlist_anchor, addr->localpart_cache, MCL_LOCALPART, TRUE,
+      &deliver_localpart_data);
+    break;
+
+    case ACLC_LOG_REJECT_TARGET:
+      {
+      int logbits = 0;
+      int sep = 0;
+      uschar *s = arg;
+      uschar *ss;
+      while ((ss = string_nextinlist(&s, &sep, big_buffer, big_buffer_size))
+              != NULL)
+        {
+        if (Ustrcmp(ss, "main") == 0) logbits |= LOG_MAIN;
+        else if (Ustrcmp(ss, "panic") == 0) logbits |= LOG_PANIC;
+        else if (Ustrcmp(ss, "reject") == 0) logbits |= LOG_REJECT;
+        else
+          {
+          logbits |= LOG_MAIN|LOG_REJECT;
+          log_write(0, LOG_MAIN|LOG_PANIC, "unknown log name \"%s\" in "
+            "\"log_reject_target\" in %s ACL", ss, acl_wherenames[where]);
+          }
+        }
+      log_reject_target = logbits;
+      }
+    break;
+
+    case ACLC_LOGWRITE:
+      {
+      int logbits = 0;
+      uschar *s = arg;
+      if (*s == ':')
+        {
+        s++;
+        while (*s != ':')
+          {
+          if (Ustrncmp(s, "main", 4) == 0)
+            { logbits |= LOG_MAIN; s += 4; }
+          else if (Ustrncmp(s, "panic", 5) == 0)
+            { logbits |= LOG_PANIC; s += 5; }
+          else if (Ustrncmp(s, "reject", 6) == 0)
+            { logbits |= LOG_REJECT; s += 6; }
+          else
+            {
+            logbits = LOG_MAIN|LOG_PANIC;
+            s = string_sprintf(":unknown log name in \"%s\" in "
+              "\"logwrite\" in %s ACL", arg, acl_wherenames[where]);
+            }
+          if (*s == ',') s++;
+          }
+        s++;
+        }
+      while (isspace(*s)) s++;
+
+
+      if (logbits == 0) logbits = LOG_MAIN;
+      log_write(0, logbits, "%s", string_printing(s));
+      }
+    break;
+
+    #ifdef WITH_CONTENT_SCAN
+    case ACLC_MALWARE:
+      {
+      /* Separate the regular expression and any optional parameters. */
+      uschar *ss = string_nextinlist(&arg, &sep, big_buffer, big_buffer_size);
+      /* Run the malware backend. */
+      rc = malware(&ss);
+      /* Modify return code based upon the existance of options. */
+      while ((ss = string_nextinlist(&arg, &sep, big_buffer, big_buffer_size))
+            != NULL) {
+        if (strcmpic(ss, US"defer_ok") == 0 && rc == DEFER)
+          {
+          /* FAIL so that the message is passed to the next ACL */
+          rc = FAIL;
+          }
+        }
+      }
+    break;
+
+    case ACLC_MIME_REGEX:
+    rc = mime_regex(&arg);
+    break;
+    #endif
+
+    case ACLC_RATELIMIT:
+    rc = acl_ratelimit(arg, where, log_msgptr);
+    break;
+
+    case ACLC_RECIPIENTS:
+    rc = match_address_list(addr->address, TRUE, TRUE, &arg, NULL, -1, 0,
+      &recipient_data);
+    break;
+
+    #ifdef WITH_CONTENT_SCAN
+    case ACLC_REGEX:
+    rc = regex(&arg);
+    break;
+    #endif
+
+    case ACLC_REMOVE_HEADER:
+    setup_remove_header(arg);
+    break;
+
+    case ACLC_SENDER_DOMAINS:
+      {
+      uschar *sdomain;
+      sdomain = Ustrrchr(sender_address, '@');
+      sdomain = (sdomain == NULL)? US"" : sdomain + 1;
+      rc = match_isinlist(sdomain, &arg, 0, &domainlist_anchor,
+        sender_domain_cache, MCL_DOMAIN, TRUE, NULL);
+      }
+    break;
+
+    case ACLC_SENDERS:
+    rc = match_address_list(sender_address, TRUE, TRUE, &arg,
+      sender_address_cache, -1, 0, &sender_data);
+    break;
+
+    /* Connection variables must persist forever */
+
+    case ACLC_SET:
+      {
+      int old_pool = store_pool;
+      if (cb->u.varname[0] == 'c') store_pool = POOL_PERM;
+      acl_var_create(cb->u.varname)->data.ptr = string_copy(arg);
+      store_pool = old_pool;
+      }
+    break;
+
+    #ifdef WITH_CONTENT_SCAN
+    case ACLC_SPAM:
+      {
+      /* Seperate the regular expression and any optional parameters. */
+      uschar *ss = string_nextinlist(&arg, &sep, big_buffer, big_buffer_size);
+      /* Run the spam backend. */
+      rc = spam(&ss);
+      /* Modify return code based upon the existance of options. */
+      while ((ss = string_nextinlist(&arg, &sep, big_buffer, big_buffer_size))
+            != NULL) {
+        if (strcmpic(ss, US"defer_ok") == 0 && rc == DEFER)
+          {
+          /* FAIL so that the message is passed to the next ACL */
+          rc = FAIL;
+          }
+        }
+      }
+    break;
+    #endif
+
+    #ifdef EXPERIMENTAL_SPF
+    case ACLC_SPF:
+      rc = spf_process(&arg, sender_address, SPF_PROCESS_NORMAL);
+    break;
+    case ACLC_SPF_GUESS:
+      rc = spf_process(&arg, sender_address, SPF_PROCESS_GUESS);
+    break;
+    #endif
+
+    case ACLC_UDPSEND:
+    rc = acl_udpsend(arg, log_msgptr);
+    break;
+
+    /* If the verb is WARN, discard any user message from verification, because
+    such messages are SMTP responses, not header additions. The latter come
+    only from explicit "message" modifiers. However, put the user message into
+    $acl_verify_message so it can be used in subsequent conditions or modifiers
+    (until something changes it). */
+
+    case ACLC_VERIFY:
+    rc = acl_verify(where, addr, arg, user_msgptr, log_msgptr, basic_errno);
+    acl_verify_message = *user_msgptr;
+    if (verb == ACL_WARN) *user_msgptr = NULL;
+    break;
+
+    default:
+    log_write(0, LOG_MAIN|LOG_PANIC_DIE, "internal ACL error: unknown "
+      "condition %d", cb->type);
+    break;
+    }
+
+  /* If a condition was negated, invert OK/FAIL. */
+
+  if (!cond_modifiers[cb->type] && cb->u.negated)
+    {
+    if (rc == OK) rc = FAIL;
+      else if (rc == FAIL || rc == FAIL_DROP) rc = OK;
+    }
+
+  if (rc != OK) break;   /* Conditions loop */
+  }
+
+
+/* If the result is the one for which "message" and/or "log_message" are used,
+handle the values of these modifiers. If there isn't a log message set, we make
+it the same as the user message.
+
+"message" is a user message that will be included in an SMTP response. Unless
+it is empty, it overrides any previously set user message.
+
+"log_message" is a non-user message, and it adds to any existing non-user
+message that is already set.
+
+Most verbs have but a single return for which the messages are relevant, but
+for "discard", it's useful to have the log message both when it succeeds and
+when it fails. For "accept", the message is used in the OK case if there is no
+"endpass", but (for backwards compatibility) in the FAIL case if "endpass" is
+present. */
+
+if (*epp && rc == OK) user_message = NULL;
+
+if (((1<<rc) & msgcond[verb]) != 0)
+  {
+  uschar *expmessage;
+  uschar *old_user_msgptr = *user_msgptr;
+  uschar *old_log_msgptr = (*log_msgptr != NULL)? *log_msgptr : old_user_msgptr;
+
+  /* If the verb is "warn", messages generated by conditions (verification or
+  nested ACLs) are always discarded. This also happens for acceptance verbs
+  when they actually do accept. Only messages specified at this level are used.
+  However, the value of an existing message is available in $acl_verify_message
+  during expansions. */
+
+  if (verb == ACL_WARN ||
+      (rc == OK && (verb == ACL_ACCEPT || verb == ACL_DISCARD)))
+    *log_msgptr = *user_msgptr = NULL;
+
+  if (user_message != NULL)
+    {
+    acl_verify_message = old_user_msgptr;
+    expmessage = expand_string(user_message);
+    if (expmessage == NULL)
+      {
+      if (!expand_string_forcedfail)
+        log_write(0, LOG_MAIN|LOG_PANIC, "failed to expand ACL message \"%s\": %s",
+          user_message, expand_string_message);
+      }
+    else if (expmessage[0] != 0) *user_msgptr = expmessage;
+    }
+
+  if (log_message != NULL)
+    {
+    acl_verify_message = old_log_msgptr;
+    expmessage = expand_string(log_message);
+    if (expmessage == NULL)
+      {
+      if (!expand_string_forcedfail)
+        log_write(0, LOG_MAIN|LOG_PANIC, "failed to expand ACL message \"%s\": %s",
+          log_message, expand_string_message);
+      }
+    else if (expmessage[0] != 0)
+      {
+      *log_msgptr = (*log_msgptr == NULL)? expmessage :
+        string_sprintf("%s: %s", expmessage, *log_msgptr);
+      }
+    }
+
+  /* If no log message, default it to the user message */
+
+  if (*log_msgptr == NULL) *log_msgptr = *user_msgptr;
+  }
+
+acl_verify_message = NULL;
+return rc;
+}
+
+
+
+
+
+/*************************************************
+*        Get line from a literal ACL             *
+*************************************************/
+
+/* This function is passed to acl_read() in order to extract individual lines
+of a literal ACL, which we access via static pointers. We can destroy the
+contents because this is called only once (the compiled ACL is remembered).
+
+This code is intended to treat the data in the same way as lines in the main
+Exim configuration file. That is:
+
+  . Leading spaces are ignored.
+
+  . A \ at the end of a line is a continuation - trailing spaces after the \
+    are permitted (this is because I don't believe in making invisible things
+    significant). Leading spaces on the continued part of a line are ignored.
+
+  . Physical lines starting (significantly) with # are totally ignored, and
+    may appear within a sequence of backslash-continued lines.
+
+  . Blank lines are ignored, but will end a sequence of continuations.
+
+Arguments: none
+Returns:   a pointer to the next line
+*/
+
+
+static uschar *acl_text;          /* Current pointer in the text */
+static uschar *acl_text_end;      /* Points one past the terminating '0' */
+
+
+static uschar *
+acl_getline(void)
+{
+uschar *yield;
+
+/* This loop handles leading blank lines and comments. */
+
+for(;;)
+  {
+  while (isspace(*acl_text)) acl_text++;   /* Leading spaces/empty lines */
+  if (*acl_text == 0) return NULL;         /* No more data */
+  yield = acl_text;                        /* Potential data line */
+
+  while (*acl_text != 0 && *acl_text != '\n') acl_text++;
+
+  /* If we hit the end before a newline, we have the whole logical line. If
+  it's a comment, there's no more data to be given. Otherwise, yield it. */
+
+  if (*acl_text == 0) return (*yield == '#')? NULL : yield;
+
+  /* After reaching a newline, end this loop if the physical line does not
+  start with '#'. If it does, it's a comment, and the loop continues. */
+
+  if (*yield != '#') break;
+  }
+
+/* This loop handles continuations. We know we have some real data, ending in
+newline. See if there is a continuation marker at the end (ignoring trailing
+white space). We know that *yield is not white space, so no need to test for
+cont > yield in the backwards scanning loop. */
+
+for(;;)
+  {
+  uschar *cont;
+  for (cont = acl_text - 1; isspace(*cont); cont--);
+
+  /* If no continuation follows, we are done. Mark the end of the line and
+  return it. */
+
+  if (*cont != '\\')
+    {
+    *acl_text++ = 0;
+    return yield;
+    }
+
+  /* We have encountered a continuation. Skip over whitespace at the start of
+  the next line, and indeed the whole of the next line or lines if they are
+  comment lines. */
+
+  for (;;)
+    {
+    while (*(++acl_text) == ' ' || *acl_text == '\t');
+    if (*acl_text != '#') break;
+    while (*(++acl_text) != 0 && *acl_text != '\n');
+    }
+
+  /* We have the start of a continuation line. Move all the rest of the data
+  to join onto the previous line, and then find its end. If the end is not a
+  newline, we are done. Otherwise loop to look for another continuation. */
+
+  memmove(cont, acl_text, acl_text_end - acl_text);
+  acl_text_end -= acl_text - cont;
+  acl_text = cont;
+  while (*acl_text != 0 && *acl_text != '\n') acl_text++;
+  if (*acl_text == 0) return yield;
+  }
+
+/* Control does not reach here */
+}
+
+
+
+
+
+/*************************************************
+*        Check access using an ACL               *
+*************************************************/
+
+/* This function is called from address_check. It may recurse via
+acl_check_condition() - hence the use of a level to stop looping. The ACL is
+passed as a string which is expanded. A forced failure implies no access check
+is required. If the result is a single word, it is taken as the name of an ACL
+which is sought in the global ACL tree. Otherwise, it is taken as literal ACL
+text, complete with newlines, and parsed as such. In both cases, the ACL check
+is then run. This function uses an auxiliary function for acl_read() to call
+for reading individual lines of a literal ACL. This is acl_getline(), which
+appears immediately above.
+
+Arguments:
+  where        where called from
+  addr         address item when called from RCPT; otherwise NULL
+  s            the input string; NULL is the same as an empty ACL => DENY
+  level        the nesting level
+  user_msgptr  where to put a user error (for SMTP response)
+  log_msgptr   where to put a logging message (not for SMTP response)
+
+Returns:       OK         access is granted
+               DISCARD    access is apparently granted...
+               FAIL       access is denied
+               FAIL_DROP  access is denied; drop the connection
+               DEFER      can't tell at the moment
+               ERROR      disaster
+*/
+
+static int
+acl_check_internal(int where, address_item *addr, uschar *s, int level,
+  uschar **user_msgptr, uschar **log_msgptr)
+{
+int fd = -1;
+acl_block *acl = NULL;
+uschar *acl_name = US"inline ACL";
+uschar *ss;
+
+/* Catch configuration loops */
+
+if (level > 20)
+  {
+  *log_msgptr = US"ACL nested too deep: possible loop";
+  return ERROR;
+  }
+
+if (s == NULL)
+  {
+  HDEBUG(D_acl) debug_printf("ACL is NULL: implicit DENY\n");
+  return FAIL;
+  }
+
+/* At top level, we expand the incoming string. At lower levels, it has already
+been expanded as part of condition processing. */
+
+if (level == 0)
+  {
+  ss = expand_string(s);
+  if (ss == NULL)
+    {
+    if (expand_string_forcedfail) return OK;
+    *log_msgptr = string_sprintf("failed to expand ACL string \"%s\": %s", s,
+      expand_string_message);
+    return ERROR;
+    }
+  }
+else ss = s;
+
+while (isspace(*ss))ss++;
+
+/* If we can't find a named ACL, the default is to parse it as an inline one.
+(Unless it begins with a slash; non-existent files give rise to an error.) */
+
+acl_text = ss;
+
+/* Handle the case of a string that does not contain any spaces. Look for a
+named ACL among those read from the configuration, or a previously read file.
+It is possible that the pointer to the ACL is NULL if the configuration
+contains a name with no data. If not found, and the text begins with '/',
+read an ACL from a file, and save it so it can be re-used. */
+
+if (Ustrchr(ss, ' ') == NULL)
+  {
+  tree_node *t = tree_search(acl_anchor, ss);
+  if (t != NULL)
+    {
+    acl = (acl_block *)(t->data.ptr);
+    if (acl == NULL)
+      {
+      HDEBUG(D_acl) debug_printf("ACL \"%s\" is empty: implicit DENY\n", ss);
+      return FAIL;
+      }
+    acl_name = string_sprintf("ACL \"%s\"", ss);
+    HDEBUG(D_acl) debug_printf("using ACL \"%s\"\n", ss);
+    }
+
+  else if (*ss == '/')
+    {
+    struct stat statbuf;
+    fd = Uopen(ss, O_RDONLY, 0);
+    if (fd < 0)
+      {
+      *log_msgptr = string_sprintf("failed to open ACL file \"%s\": %s", ss,
+        strerror(errno));
+      return ERROR;
+      }
+
+    if (fstat(fd, &statbuf) != 0)
+      {
+      *log_msgptr = string_sprintf("failed to fstat ACL file \"%s\": %s", ss,
+        strerror(errno));
+      return ERROR;
+      }
+
+    acl_text = store_get(statbuf.st_size + 1);
+    acl_text_end = acl_text + statbuf.st_size + 1;
+
+    if (read(fd, acl_text, statbuf.st_size) != statbuf.st_size)
+      {
+      *log_msgptr = string_sprintf("failed to read ACL file \"%s\": %s",
+        ss, strerror(errno));
+      return ERROR;
+      }
+    acl_text[statbuf.st_size] = 0;
+    (void)close(fd);
+
+    acl_name = string_sprintf("ACL \"%s\"", ss);
+    HDEBUG(D_acl) debug_printf("read ACL from file %s\n", ss);
+    }
+  }
+
+/* Parse an ACL that is still in text form. If it came from a file, remember it
+in the ACL tree, having read it into the POOL_PERM store pool so that it
+persists between multiple messages. */
+
+if (acl == NULL)
+  {
+  int old_pool = store_pool;
+  if (fd >= 0) store_pool = POOL_PERM;
+  acl = acl_read(acl_getline, log_msgptr);
+  store_pool = old_pool;
+  if (acl == NULL && *log_msgptr != NULL) return ERROR;
+  if (fd >= 0)
+    {
+    tree_node *t = store_get_perm(sizeof(tree_node) + Ustrlen(ss));
+    Ustrcpy(t->name, ss);
+    t->data.ptr = acl;
+    (void)tree_insertnode(&acl_anchor, t);
+    }
+  }
+
+/* Now we have an ACL to use. It's possible it may be NULL. */
+
+while (acl != NULL)
+  {
+  int cond;
+  int basic_errno = 0;
+  BOOL endpass_seen = FALSE;
+
+  *log_msgptr = *user_msgptr = NULL;
+  acl_temp_details = FALSE;
+
+  if ((where == ACL_WHERE_QUIT || where == ACL_WHERE_NOTQUIT) &&
+      acl->verb != ACL_ACCEPT &&
+      acl->verb != ACL_WARN)
+    {
+    *log_msgptr = string_sprintf("\"%s\" is not allowed in a QUIT or not-QUIT ACL",
+      verbs[acl->verb]);
+    return ERROR;
+    }
+
+  HDEBUG(D_acl) debug_printf("processing \"%s\"\n", verbs[acl->verb]);
+
+  /* Clear out any search error message from a previous check before testing
+  this condition. */
+
+  search_error_message = NULL;
+  cond = acl_check_condition(acl->verb, acl->condition, where, addr, level,
+    &endpass_seen, user_msgptr, log_msgptr, &basic_errno);
+
+  /* Handle special returns: DEFER causes a return except on a WARN verb;
+  ERROR always causes a return. */
+
+  switch (cond)
+    {
+    case DEFER:
+    HDEBUG(D_acl) debug_printf("%s: condition test deferred in %s\n", verbs[acl->verb], acl_name);
+    if (basic_errno != ERRNO_CALLOUTDEFER)
+      {
+      if (search_error_message != NULL && *search_error_message != 0)
+        *log_msgptr = search_error_message;
+      if (smtp_return_error_details) acl_temp_details = TRUE;
+      }
+    else
+      {
+      acl_temp_details = TRUE;
+      }
+    if (acl->verb != ACL_WARN) return DEFER;
+    break;
+
+    default:      /* Paranoia */
+    case ERROR:
+    HDEBUG(D_acl) debug_printf("%s: condition test error in %s\n", verbs[acl->verb], acl_name);
+    return ERROR;
+
+    case OK:
+    HDEBUG(D_acl) debug_printf("%s: condition test succeeded in %s\n",
+      verbs[acl->verb], acl_name);
+    break;
+
+    case FAIL:
+    HDEBUG(D_acl) debug_printf("%s: condition test failed in %s\n", verbs[acl->verb], acl_name);
+    break;
+
+    /* DISCARD and DROP can happen only from a nested ACL condition, and
+    DISCARD can happen only for an "accept" or "discard" verb. */
+
+    case DISCARD:
+    HDEBUG(D_acl) debug_printf("%s: condition test yielded \"discard\" in %s\n",
+      verbs[acl->verb], acl_name);
+    break;
+
+    case FAIL_DROP:
+    HDEBUG(D_acl) debug_printf("%s: condition test yielded \"drop\" in %s\n",
+      verbs[acl->verb], acl_name);
+    break;
+    }
+
+  /* At this point, cond for most verbs is either OK or FAIL or (as a result of
+  a nested ACL condition) FAIL_DROP. However, for WARN, cond may be DEFER, and
+  for ACCEPT and DISCARD, it may be DISCARD after a nested ACL call. */
+
+  switch(acl->verb)
+    {
+    case ACL_ACCEPT:
+    if (cond == OK || cond == DISCARD) return cond;
+    if (endpass_seen)
+      {
+      HDEBUG(D_acl) debug_printf("accept: endpass encountered - denying access\n");
+      return cond;
+      }
+    break;
+
+    case ACL_DEFER:
+    if (cond == OK)
+      {
+      acl_temp_details = TRUE;
+      return DEFER;
+      }
+    break;
+
+    case ACL_DENY:
+    if (cond == OK) return FAIL;
+    break;
+
+    case ACL_DISCARD:
+    if (cond == OK || cond == DISCARD) return DISCARD;
+    if (endpass_seen)
+      {
+      HDEBUG(D_acl) debug_printf("discard: endpass encountered - denying access\n");
+      return cond;
+      }
+    break;
+
+    case ACL_DROP:
+    if (cond == OK) return FAIL_DROP;
+    break;
+
+    case ACL_REQUIRE:
+    if (cond != OK) return cond;
+    break;
+
+    case ACL_WARN:
+    if (cond == OK)
+      acl_warn(where, *user_msgptr, *log_msgptr);
+    else if (cond == DEFER && (log_extra_selector & LX_acl_warn_skipped) != 0)
+      log_write(0, LOG_MAIN, "%s Warning: ACL \"warn\" statement skipped: "
+        "condition test deferred%s%s", host_and_ident(TRUE),
+        (*log_msgptr == NULL)? US"" : US": ",
+        (*log_msgptr == NULL)? US"" : *log_msgptr);
+    *log_msgptr = *user_msgptr = NULL;  /* In case implicit DENY follows */
+    break;
+
+    default:
+    log_write(0, LOG_MAIN|LOG_PANIC_DIE, "internal ACL error: unknown verb %d",
+      acl->verb);
+    break;
+    }
+
+  /* Pass to the next ACL item */
+
+  acl = acl->next;
+  }
+
+/* We have reached the end of the ACL. This is an implicit DENY. */
+
+HDEBUG(D_acl) debug_printf("end of %s: implicit DENY\n", acl_name);
+return FAIL;
+}
+
+
+
+
+/* Same args as acl_check_internal() above, but the string s is
+the name of an ACL followed optionally by up to 9 space-separated arguments.
+The name and args are separately expanded.  Args go into $acl_arg globals. */
+static int
+acl_check_wargs(int where, address_item *addr, uschar *s, int level,
+  uschar **user_msgptr, uschar **log_msgptr)
+{
+uschar * tmp;
+uschar * tmp_arg[9];   /* must match acl_arg[] */
+uschar * sav_arg[9];   /* must match acl_arg[] */
+int sav_narg;
+uschar * name;
+int i;
+int ret;
+
+if (!(tmp = string_dequote(&s)) || !(name = expand_string(tmp)))
+  goto bad;
+
+for (i = 0; i < 9; i++)
+  {
+  while (*s && isspace(*s)) s++;
+  if (!*s) break;
+  if (!(tmp = string_dequote(&s)) || !(tmp_arg[i] = expand_string(tmp)))
+    {
+    tmp = name;
+    goto bad;
+    }
+  }
+
+sav_narg = acl_narg;
+acl_narg = i;
+for (i = 0; i < acl_narg; i++)
+  {
+  sav_arg[i] = acl_arg[i];
+  acl_arg[i] = tmp_arg[i];
+  }
+while (i < 9)
+  {
+  sav_arg[i] = acl_arg[i];
+  acl_arg[i++] = NULL;
+  }
+
+ret = acl_check_internal(where, addr, name, level, user_msgptr, log_msgptr);
+
+acl_narg = sav_narg;
+for (i = 0; i < 9; i++) acl_arg[i] = sav_arg[i];
+return ret;
+
+bad:
+if (expand_string_forcedfail) return ERROR;
+*log_msgptr = string_sprintf("failed to expand ACL string \"%s\": %s",
+  tmp, expand_string_message);
+return search_find_defer?DEFER:ERROR;
+}
+
+
+
+/*************************************************
+*        Check access using an ACL               *
+*************************************************/
+
+/* Alternate interface for ACL, used by expansions */
+int
+acl_eval(int where, uschar *s, uschar **user_msgptr, uschar **log_msgptr)
+{
+address_item adb;
+address_item *addr = NULL;
+
+*user_msgptr = *log_msgptr = NULL;
+sender_verified_failed = NULL;
+ratelimiters_cmd = NULL;
+log_reject_target = LOG_MAIN|LOG_REJECT;
+
+if (where == ACL_WHERE_RCPT)
+  {
+  adb = address_defaults;
+  addr = &adb;
+  addr->address = expand_string(US"$local_part@$domain");
+  addr->domain = deliver_domain;
+  addr->local_part = deliver_localpart;
+  addr->cc_local_part = deliver_localpart;
+  addr->lc_local_part = deliver_localpart;
+  }
+
+return acl_check_internal(where, addr, s, 0, user_msgptr, log_msgptr);
+}
+
+
+
+/* This is the external interface for ACL checks. It sets up an address and the
+expansions for $domain and $local_part when called after RCPT, then calls
+acl_check_internal() to do the actual work.
+
+Arguments:
+  where        ACL_WHERE_xxxx indicating where called from
+  recipient    RCPT address for RCPT check, else NULL
+  s            the input string; NULL is the same as an empty ACL => DENY
+  user_msgptr  where to put a user error (for SMTP response)
+  log_msgptr   where to put a logging message (not for SMTP response)
+
+Returns:       OK         access is granted by an ACCEPT verb
+               DISCARD    access is granted by a DISCARD verb
+               FAIL       access is denied
+               FAIL_DROP  access is denied; drop the connection
+               DEFER      can't tell at the moment
+               ERROR      disaster
+*/
+int acl_where = ACL_WHERE_UNKNOWN;
+
+int
+acl_check(int where, uschar *recipient, uschar *s, uschar **user_msgptr,
+  uschar **log_msgptr)
+{
+int rc;
+address_item adb;
+address_item *addr = NULL;
+
+*user_msgptr = *log_msgptr = NULL;
+sender_verified_failed = NULL;
+ratelimiters_cmd = NULL;
+log_reject_target = LOG_MAIN|LOG_REJECT;
+
+#ifndef DISABLE_PRDR
+if (where == ACL_WHERE_RCPT || where == ACL_WHERE_PRDR )
+#else
+if (where == ACL_WHERE_RCPT )
+#endif
+  {
+  adb = address_defaults;
+  addr = &adb;
+  addr->address = recipient;
+  if (deliver_split_address(addr) == DEFER)
+    {
+    *log_msgptr = US"defer in percent_hack_domains check";
+    return DEFER;
+    }
+  deliver_domain = addr->domain;
+  deliver_localpart = addr->local_part;
+  }
+
+acl_where = where;
+rc = acl_check_internal(where, addr, s, 0, user_msgptr, log_msgptr);
+acl_where = ACL_WHERE_UNKNOWN;
+
+/* Cutthrough - if requested,
+and WHERE_RCPT and not yet opened conn as result of recipient-verify,
+and rcpt acl returned accept,
+and first recipient (cancel on any subsequents)
+open one now and run it up to RCPT acceptance.
+A failed verify should cancel cutthrough request.
+
+Initial implementation:  dual-write to spool.
+Assume the rxd datastream is now being copied byte-for-byte to an open cutthrough connection.
+
+Cease cutthrough copy on rxd final dot; do not send one.
+
+On a data acl, if not accept and a cutthrough conn is open, hard-close it (no SMTP niceness).
+
+On data acl accept, terminate the dataphase on an open cutthrough conn.  If accepted or
+perm-rejected, reflect that to the original sender - and dump the spooled copy.
+If temp-reject, close the conn (and keep the spooled copy).
+If conn-failure, no action (and keep the spooled copy).
+*/
+switch (where)
+{
+case ACL_WHERE_RCPT:
+#ifndef DISABLE_PRDR
+case ACL_WHERE_PRDR:
+#endif
+  if( rcpt_count > 1 )
+    cancel_cutthrough_connection("more than one recipient");
+  else if (rc == OK  &&  cutthrough_delivery  &&  cutthrough_fd < 0)
+    open_cutthrough_connection(addr);
+  break;
+
+case ACL_WHERE_PREDATA:
+  if( rc == OK )
+    cutthrough_predata();
+  else
+    cancel_cutthrough_connection("predata acl not ok");
+  break;
+
+case ACL_WHERE_QUIT:
+case ACL_WHERE_NOTQUIT:
+  cancel_cutthrough_connection("quit or notquit");
+  break;
+
+default:
+  break;
+}
+
+deliver_domain = deliver_localpart = deliver_address_data =
+  sender_address_data = NULL;
+
+/* A DISCARD response is permitted only for message ACLs, excluding the PREDATA
+ACL, which is really in the middle of an SMTP command. */
+
+if (rc == DISCARD)
+  {
+  if (where > ACL_WHERE_NOTSMTP || where == ACL_WHERE_PREDATA)
+    {
+    log_write(0, LOG_MAIN|LOG_PANIC, "\"discard\" verb not allowed in %s "
+      "ACL", acl_wherenames[where]);
+    return ERROR;
+    }
+  return DISCARD;
+  }
+
+/* A DROP response is not permitted from MAILAUTH */
+
+if (rc == FAIL_DROP && where == ACL_WHERE_MAILAUTH)
+  {
+  log_write(0, LOG_MAIN|LOG_PANIC, "\"drop\" verb not allowed in %s "
+    "ACL", acl_wherenames[where]);
+  return ERROR;
+  }
+
+/* Before giving a response, take a look at the length of any user message, and
+split it up into multiple lines if possible. */
+
+*user_msgptr = string_split_message(*user_msgptr);
+if (fake_response != OK)
+  fake_response_text = string_split_message(fake_response_text);
+
+return rc;
+}
+
+
+/*************************************************
+*             Create ACL variable                *
+*************************************************/
+
+/* Create an ACL variable or reuse an existing one. ACL variables are in a
+binary tree (see tree.c) with acl_var_c and acl_var_m as root nodes.
+
+Argument:
+  name    pointer to the variable's name, starting with c or m
+
+Returns   the pointer to variable's tree node
+*/
+
+tree_node *
+acl_var_create(uschar *name)
+{
+tree_node *node, **root;
+root = (name[0] == 'c')? &acl_var_c : &acl_var_m;
+node = tree_search(*root, name);
+if (node == NULL)
+  {
+  node = store_get(sizeof(tree_node) + Ustrlen(name));
+  Ustrcpy(node->name, name);
+  (void)tree_insertnode(root, node);
+  }
+node->data.ptr = NULL;
+return node;
+}
+
+
+
+/*************************************************
+*       Write an ACL variable in spool format    *
+*************************************************/
+
+/* This function is used as a callback for tree_walk when writing variables to
+the spool file. To retain spool file compatibility, what is written is -aclc or
+-aclm followed by the rest of the name and the data length, space separated,
+then the value itself, starting on a new line, and terminated by an additional
+newline. When we had only numbered ACL variables, the first line might look
+like this: "-aclc 5 20". Now it might be "-aclc foo 20" for the variable called
+acl_cfoo.
+
+Arguments:
+  name    of the variable
+  value   of the variable
+  ctx     FILE pointer (as a void pointer)
+
+Returns:  nothing
+*/
+
+void
+acl_var_write(uschar *name, uschar *value, void *ctx)
+{
+FILE *f = (FILE *)ctx;
+fprintf(f, "-acl%c %s %d\n%s\n", name[0], name+1, Ustrlen(value), value);
+}
+
+/* vi: aw ai sw=2
+*/
+/* End of acl.c */
diff --git a/src/aliases.default b/src/aliases.default
new file mode 100644 (file)
index 0000000..ca48b3a
--- /dev/null
@@ -0,0 +1,40 @@
+# Default aliases file, installed by Exim. This file contains no real aliases.
+# You should edit it to taste.
+
+
+# The following alias is required by the mail RFCs 2821 and 2822.
+# Set it to the address of a HUMAN who deals with this system's mail problems.
+
+# postmaster: someone@your.domain
+
+# It is also common to set the following alias so that if anybody replies to a
+# bounce message from this host, the reply goes to the postmaster.
+
+# mailer-daemon: postmaster
+
+
+# You should also set up an alias for messages to root, because it is not
+# usually a good idea to deliver mail as root.
+
+# root: postmaster
+
+# It is a good idea to redirect any messages sent to system accounts so that
+# they don't just get ignored. Here are some common examples:
+
+# bin: root
+# daemon: root
+# ftp: root
+# nobody: root
+# operator: root
+# uucp: root
+
+# You should check your /etc/passwd for any others.
+
+
+# Other commonly enountered aliases are:
+#
+# abuse:       the person dealing with network and mail abuse
+# hostmaster:  the person dealing with DNS problems
+# webmaster:   the person dealing with your web site
+
+####
diff --git a/src/auths/Makefile b/src/auths/Makefile
new file mode 100644 (file)
index 0000000..c6ef218
--- /dev/null
@@ -0,0 +1,47 @@
+# Make file for building a library containing all the available authorization
+# methods, and calling it auths.a. In addition, there are functions that are
+# of general use in several methods; these are in separate modules so they are
+# linked in only when needed. This Makefile is called from the main make file,
+# after cd'ing to the auths subdirectory. When the relevant AUTH_ macros are
+# defined, the equivalent modules herein is not included in the final binary.
+
+OBJ = auth-spa.o b64decode.o b64encode.o call_pam.o call_pwcheck.o \
+      call_radius.o check_serv_cond.o cram_md5.o cyrus_sasl.o dovecot.o \
+      get_data.o get_no64_data.o gsasl_exim.o heimdal_gssapi.o \
+      md5.o plaintext.o pwcheck.o sha1.o \
+      spa.o xtextdecode.o xtextencode.o
+
+auths.a:         $(OBJ)
+                @$(RM_COMMAND) -f auths.a
+                @echo "$(AR) auths.a"
+                $(FE)$(AR) auths.a $(OBJ)
+                $(RANLIB) $@
+
+.SUFFIXES:       .o .c
+.c.o:;           @echo "$(CC) $*.c"
+                $(FE)$(CC) -c $(CFLAGS) $(INCLUDE) $*.c
+
+auth-spa.o:         $(HDRS) auth-spa.c
+b64encode.o:        $(HDRS) b64encode.c
+b64decode.o:        $(HDRS) b64decode.c
+call_pam.o:         $(HDRS) call_pam.c
+call_pwcheck.o:     $(HDRS) call_pwcheck.c pwcheck.h
+call_radius.o:      $(HDRS) call_radius.c
+check_serv_cond.o:  $(HDRS) check_serv_cond.c
+get_data.o:         $(HDRS) get_data.c
+get_no64_data.o:    $(HDRS) get_no64_data.c
+md5.o:              $(HDRS) md5.c
+pwcheck.o:          $(HDRS) pwcheck.c pwcheck.h
+sha1.o:             $(HDRS) sha1.c
+xtextdecode.o:      $(HDRS) xtextdecode.c
+xtextencode.o:      $(HDRS) xtextencode.c
+
+cram_md5.o:         $(HDRS) cram_md5.c cram_md5.h
+cyrus_sasl.o:       $(HDRS) cyrus_sasl.c cyrus_sasl.h
+dovecot.o:          $(HDRS) dovecot.c dovecot.h
+gsasl_exim.o:       $(HDRS) gsasl_exim.c gsasl_exim.h
+heimdal_gssapi.o:   $(HDRS) heimdal_gssapi.c heimdal_gssapi.h
+plaintext.o:        $(HDRS) plaintext.c plaintext.h
+spa.o:              $(HDRS) spa.c spa.h
+
+# End
diff --git a/src/auths/README b/src/auths/README
new file mode 100644 (file)
index 0000000..21b0cda
--- /dev/null
@@ -0,0 +1,98 @@
+AUTHS
+
+The modules in this directory are in support of various authentication
+functions. Some of them, such as the base64 encoding/decoding and MD5
+computation, are just functions that might be used by several authentication
+mechanisms. Others are the SMTP AUTH mechanisms themselves, included in the
+final binary if the relevant AUTH_XXX value is set in Local/Makefile. The
+general functions are in separate modules so that they get included in the
+final binary only if they are actually called from somewhere.
+
+GENERAL FUNCTIONS
+
+The API for each of these functions is documented with the function's code.
+
+  auth_b64encode       encode in base 64
+  auth_b64decode       decode from base 64
+  auth_call_pam        do PAM authentication (if build with SUPPORT_PAM)
+  auth_get_data        issue SMTP AUTH challenge and read response
+  auth_xtextencode     encode as xtext
+  auth_xtextdecode     decode from xtext
+
+INTERFACE TO SMTP AUTHENTICATION MECHANISMS
+
+These are general SSL mechanisms, adapted for use with SMTP. Each
+authentication mechanism has three functions, for initialization, server
+authentication, and client authentication.
+
+INITIALIZATION
+
+The initialization function is called when the configuration is read, and can
+check for incomplete or illegal settings. It has one argument, a pointer to the
+instance block for this configured mechanism. It must set the flags called
+"server" and "client" in the generic auth_instance block to indicate whether
+the server and/or client functions are available for this authenticator.
+Typically this depends on whether server or client configuration options have
+been set, but it is also possible to have an authenticator that has only one of
+the server or client functions.
+
+SERVER AUTHENTICATION
+
+The second function performs authentication as a server. It receives a pointer
+to the instance block, and its second argument is the remainder of the data
+from the AUTH command. The numeric variable maximum setting (expand_nmax) is
+set to zero, with $0 initialized as unset. The authenticator may set up numeric
+variables according to its (old) specification and $auth<n> variables the
+preferred ones nowadays; it should leave them set at the end so that they can
+be used for the expansion of the generic server_set_id option, which happens
+centrally.
+
+This function has access to the SMTP input and output so that it can write
+intermediate responses and read more data if necessary. There is a packaged
+function in auth_get_data() which outputs a challenge and reads a response.
+
+The yield of a server authentication check must be one of:
+
+  OK          success
+  DEFER       couldn't complete the check
+  FAIL        authentication failed
+  CANCELLED   authentication forced to fail by "*" response to challenge,
+                or by certain forced string expansion failures
+  BAD64       bad base64 data received
+  UNEXPECTED  unexpected data received
+
+In the case of DEFER, auth_defer_msg should point to an error message.
+
+CLIENT AUTHENTICATION
+
+The third function performs authentication as a client. It receives a pointer
+to the instance block, and four further arguments:
+
+  The smtp_inblock item for the connection to the remote host.
+
+  The normal command-reading timeout value.
+
+  A pointer to a buffer, to be used for receiving responses. It is done this
+    way so that the buffer is available for logging etc. in the calling
+    function in cases of error.
+
+  The size of the buffer.
+
+The yield of a client authentication check must be one of:
+
+  OK          success
+  FAIL_SEND   error after writing a command; errno is set
+  FAIL        failed after reading a response;
+              either errno is set (for timeouts, I/O failures) or
+              the buffer contains the SMTP response line
+  CANCELLED   the client cancelled authentication (often "fail" in expansion)
+              the buffer may contain a message; if not, *buffer = 0
+  ERROR       local problem (typically expansion error); message in buffer
+
+To communicate with the remote host the client should call
+smtp_write_command(). If this yields FALSE, the authenticator should return
+FAIL. After a successful write, the response is received by a call to
+smtp_read_response(), which should use the buffer handed to the client function
+as an argument.
+
+****
diff --git a/src/auths/auth-spa.c b/src/auths/auth-spa.c
new file mode 100644 (file)
index 0000000..7ad5a1d
--- /dev/null
@@ -0,0 +1,1542 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/*
+ * This file provides the necessary methods for authenticating with
+ * Microsoft's Secure Password Authentication.
+
+ * All the original code used here was torn by Marc Prud'hommeaux out of the
+ * Samba project (by Andrew Tridgell, Jeremy Allison, and others).
+
+ * Tom Kistner provided additional code, adding spa_build_auth_challenge() to
+ * support server authentication mode.
+
+ * Mark Lyda provided a patch to solve this problem:
+
+ - Exim is indicating in its Authentication Request message (Type 1) that it
+   can transmit text in either Unicode or OEM format.
+
+ - Microsoft's SMTP server (smtp.email.msn.com) is responding in its
+   Challenge message (Type 2) that it will be expecting the OEM format.
+
+ - Exim does not pay attention to the text format requested by Microsoft's
+   SMTP server and, instead, defaults to using the Unicode format.
+
+ * References:
+ * http://www.innovation.ch/java/ntlm.html
+ * http://www.kuro5hin.org/story/2002/4/28/1436/66154
+
+ * It seems that some systems have existing but different definitions of some
+ * of the following types. I received a complaint about "int16" causing
+ * compilation problems. So I (PH) have renamed them all, to be on the safe
+ * side, by adding 'x' on the end.
+
+ * typedef signed short int16;
+ * typedef unsigned short uint16;
+ * typedef unsigned uint32;
+ * typedef unsigned char  uint8;
+
+ * The API is extremely simple:
+ * 1. Form a SPA authentication request based on the username
+ *    and (optional) domain
+ * 2. Send the request to the server and get an SPA challenge
+ * 3. Build the challenge response and send it back.
+ *
+ * Example usage is as
+ * follows:
+ *
+int main (int argc, char ** argv)
+{
+       SPAAuthRequest   request;
+       SPAAuthChallenge challenge;
+       SPAAuthResponse  response;
+       char msgbuf[2048];
+       char buffer[512];
+       char *username, *password, *domain, *challenge_str;
+
+       if (argc < 3)
+       {
+               printf ("Usage: %s <username> <password> [SPA Challenge]\n",
+                       argv [0]);
+               exit (1);
+       }
+
+       username = argv [1];
+       password = argv [2];
+       domain = 0;
+
+       spa_build_auth_request (&request, username, domain);
+
+       spa_bits_to_base64 (msgbuf, (unsigned char*)&request,
+               spa_request_length(&request));
+
+       printf ("SPA Login request for username=%s:\n   %s\n",
+               argv [1], msgbuf);
+
+       if (argc < 4)
+       {
+               printf ("Run: %s <username> <password> [NTLM Challenge] " \
+                       "to complete authenitcation\n", argv [0]);
+               exit (0);
+       }
+
+       challenge_str = argv [3];
+
+       if (spa_base64_to_bits ((char *)&challenge, sizeof(challenge),
+                (const char *)(challenge_str))<0)
+       {
+                printf("bad base64 data in challenge: %s\n", challenge_str);
+                exit (1);
+       }
+
+       spa_build_auth_response (&challenge, &response, username, password);
+       spa_bits_to_base64 (msgbuf, (unsigned char*)&response,
+               spa_request_length(&response));
+
+       printf ("SPA Response to challenge:\n   %s\n for " \
+               "username=%s, password=%s:\n   %s\n",
+               argv[3], argv [1], argv [2], msgbuf);
+       return 0;
+}
+ *
+ *
+ * All the client code used here was torn by Marc Prud'hommeaux out of the
+ * Samba project (by Andrew Tridgell, Jeremy Allison, and others).
+ * Previous comments are below:
+ */
+
+/*
+   Unix SMB/Netbios implementation.
+   Version 1.9.
+
+   a partial implementation of DES designed for use in the
+   SMB authentication protocol
+
+   Copyright (C) Andrew Tridgell 1998
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+*/
+
+
+/* NOTES:
+
+   This code makes no attempt to be fast! In fact, it is a very
+   slow implementation
+
+   This code is NOT a complete DES implementation. It implements only
+   the minimum necessary for SMB authentication, as used by all SMB
+   products (including every copy of Microsoft Windows95 ever sold)
+
+   In particular, it can only do a unchained forward DES pass. This
+   means it is not possible to use this code for encryption/decryption
+   of data, instead it is only useful as a "hash" algorithm.
+
+   There is no entry point into this code that allows normal DES operation.
+
+   I believe this means that this code does not come under ITAR
+   regulations but this is NOT a legal opinion. If you are concerned
+   about the applicability of ITAR regulations to this code then you
+   should confirm it for yourself (and maybe let me know if you come
+   up with a different answer to the one above)
+*/
+
+#define DEBUG(a,b) ;
+
+extern int DEBUGLEVEL;
+
+#include <sys/types.h>     /* For size_t */
+#include "auth-spa.h"
+#include <assert.h>
+#include <ctype.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+#ifndef _AIX
+typedef unsigned char uchar;
+#endif
+
+
+typedef int BOOL;
+#define False 0
+#define True  1
+
+#ifndef _BYTEORDER_H
+#define _BYTEORDER_H
+
+#define RW_PCVAL(read,inbuf,outbuf,len) \
+       { if (read) { PCVAL (inbuf,0,outbuf,len); } \
+       else      { PSCVAL(inbuf,0,outbuf,len); } }
+
+#define RW_PIVAL(read,big_endian,inbuf,outbuf,len) \
+       { if (read) { if (big_endian) { RPIVAL(inbuf,0,outbuf,len); } else { PIVAL(inbuf,0,outbuf,len); } } \
+       else      { if (big_endian) { RPSIVAL(inbuf,0,outbuf,len); } else { PSIVAL(inbuf,0,outbuf,len); } } }
+
+#define RW_PSVAL(read,big_endian,inbuf,outbuf,len) \
+       { if (read) { if (big_endian) { RPSVAL(inbuf,0,outbuf,len); } else { PSVAL(inbuf,0,outbuf,len); } } \
+       else      { if (big_endian) { RPSSVAL(inbuf,0,outbuf,len); } else { PSSVAL(inbuf,0,outbuf,len); } } }
+
+#define RW_CVAL(read, inbuf, outbuf, offset) \
+       { if (read) { (outbuf) = CVAL (inbuf,offset); } \
+       else      { SCVAL(inbuf,offset,outbuf); } }
+
+#define RW_IVAL(read, big_endian, inbuf, outbuf, offset) \
+       { if (read) { (outbuf) = ((big_endian) ? RIVAL(inbuf,offset) : IVAL (inbuf,offset)); } \
+       else      { if (big_endian) { RSIVAL(inbuf,offset,outbuf); } else { SIVAL(inbuf,offset,outbuf); } } }
+
+#define RW_SVAL(read, big_endian, inbuf, outbuf, offset) \
+       { if (read) { (outbuf) = ((big_endian) ? RSVAL(inbuf,offset) : SVAL (inbuf,offset)); } \
+       else      { if (big_endian) { RSSVAL(inbuf,offset,outbuf); } else { SSVAL(inbuf,offset,outbuf); } } }
+
+#undef CAREFUL_ALIGNMENT
+
+/* we know that the 386 can handle misalignment and has the "right"
+   byteorder */
+#ifdef __i386__
+#define CAREFUL_ALIGNMENT 0
+#endif
+
+#ifndef CAREFUL_ALIGNMENT
+#define CAREFUL_ALIGNMENT 1
+#endif
+
+#define CVAL(buf,pos) (((unsigned char *)(buf))[pos])
+#define PVAL(buf,pos) ((unsigned)CVAL(buf,pos))
+#define SCVAL(buf,pos,val) (CVAL(buf,pos) = (val))
+
+
+#if CAREFUL_ALIGNMENT
+
+#define SVAL(buf,pos) (PVAL(buf,pos)|PVAL(buf,(pos)+1)<<8)
+#define IVAL(buf,pos) (SVAL(buf,pos)|SVAL(buf,(pos)+2)<<16)
+#define SSVALX(buf,pos,val) (CVAL(buf,pos)=(val)&0xFF,CVAL(buf,pos+1)=(val)>>8)
+#define SIVALX(buf,pos,val) (SSVALX(buf,pos,val&0xFFFF),SSVALX(buf,pos+2,val>>16))
+#define SVALS(buf,pos) ((int16x)SVAL(buf,pos))
+#define IVALS(buf,pos) ((int32x)IVAL(buf,pos))
+#define SSVAL(buf,pos,val) SSVALX((buf),(pos),((uint16x)(val)))
+#define SIVAL(buf,pos,val) SIVALX((buf),(pos),((uint32x)(val)))
+#define SSVALS(buf,pos,val) SSVALX((buf),(pos),((int16x)(val)))
+#define SIVALS(buf,pos,val) SIVALX((buf),(pos),((int32x)(val)))
+
+#else /* CAREFUL_ALIGNMENT */
+
+/* this handles things for architectures like the 386 that can handle
+   alignment errors */
+/*
+   WARNING: This section is dependent on the length of int16x and int32x
+   being correct
+*/
+
+/* get single value from an SMB buffer */
+#define SVAL(buf,pos) (*(uint16x *)((char *)(buf) + (pos)))
+#define IVAL(buf,pos) (*(uint32x *)((char *)(buf) + (pos)))
+#define SVALS(buf,pos) (*(int16x *)((char *)(buf) + (pos)))
+#define IVALS(buf,pos) (*(int32x *)((char *)(buf) + (pos)))
+
+/* store single value in an SMB buffer */
+#define SSVAL(buf,pos,val) SVAL(buf,pos)=((uint16x)(val))
+#define SIVAL(buf,pos,val) IVAL(buf,pos)=((uint32x)(val))
+#define SSVALS(buf,pos,val) SVALS(buf,pos)=((int16x)(val))
+#define SIVALS(buf,pos,val) IVALS(buf,pos)=((int32x)(val))
+
+#endif /* CAREFUL_ALIGNMENT */
+
+/* macros for reading / writing arrays */
+
+#define SMBMACRO(macro,buf,pos,val,len,size) \
+{ int l; for (l = 0; l < (len); l++) (val)[l] = macro((buf), (pos) + (size)*l); }
+
+#define SSMBMACRO(macro,buf,pos,val,len,size) \
+{ int l; for (l = 0; l < (len); l++) macro((buf), (pos) + (size)*l, (val)[l]); }
+
+/* reads multiple data from an SMB buffer */
+#define PCVAL(buf,pos,val,len) SMBMACRO(CVAL,buf,pos,val,len,1)
+#define PSVAL(buf,pos,val,len) SMBMACRO(SVAL,buf,pos,val,len,2)
+#define PIVAL(buf,pos,val,len) SMBMACRO(IVAL,buf,pos,val,len,4)
+#define PCVALS(buf,pos,val,len) SMBMACRO(CVALS,buf,pos,val,len,1)
+#define PSVALS(buf,pos,val,len) SMBMACRO(SVALS,buf,pos,val,len,2)
+#define PIVALS(buf,pos,val,len) SMBMACRO(IVALS,buf,pos,val,len,4)
+
+/* stores multiple data in an SMB buffer */
+#define PSCVAL(buf,pos,val,len) SSMBMACRO(SCVAL,buf,pos,val,len,1)
+#define PSSVAL(buf,pos,val,len) SSMBMACRO(SSVAL,buf,pos,val,len,2)
+#define PSIVAL(buf,pos,val,len) SSMBMACRO(SIVAL,buf,pos,val,len,4)
+#define PSCVALS(buf,pos,val,len) SSMBMACRO(SCVALS,buf,pos,val,len,1)
+#define PSSVALS(buf,pos,val,len) SSMBMACRO(SSVALS,buf,pos,val,len,2)
+#define PSIVALS(buf,pos,val,len) SSMBMACRO(SIVALS,buf,pos,val,len,4)
+
+
+/* now the reverse routines - these are used in nmb packets (mostly) */
+#define SREV(x) ((((x)&0xFF)<<8) | (((x)>>8)&0xFF))
+#define IREV(x) ((SREV(x)<<16) | (SREV((x)>>16)))
+
+#define RSVAL(buf,pos) SREV(SVAL(buf,pos))
+#define RSVALS(buf,pos) SREV(SVALS(buf,pos))
+#define RIVAL(buf,pos) IREV(IVAL(buf,pos))
+#define RIVALS(buf,pos) IREV(IVALS(buf,pos))
+#define RSSVAL(buf,pos,val) SSVAL(buf,pos,SREV(val))
+#define RSSVALS(buf,pos,val) SSVALS(buf,pos,SREV(val))
+#define RSIVAL(buf,pos,val) SIVAL(buf,pos,IREV(val))
+#define RSIVALS(buf,pos,val) SIVALS(buf,pos,IREV(val))
+
+/* reads multiple data from an SMB buffer (big-endian) */
+#define RPSVAL(buf,pos,val,len) SMBMACRO(RSVAL,buf,pos,val,len,2)
+#define RPIVAL(buf,pos,val,len) SMBMACRO(RIVAL,buf,pos,val,len,4)
+#define RPSVALS(buf,pos,val,len) SMBMACRO(RSVALS,buf,pos,val,len,2)
+#define RPIVALS(buf,pos,val,len) SMBMACRO(RIVALS,buf,pos,val,len,4)
+
+/* stores multiple data in an SMB buffer (big-endian) */
+#define RPSSVAL(buf,pos,val,len) SSMBMACRO(RSSVAL,buf,pos,val,len,2)
+#define RPSIVAL(buf,pos,val,len) SSMBMACRO(RSIVAL,buf,pos,val,len,4)
+#define RPSSVALS(buf,pos,val,len) SSMBMACRO(RSSVALS,buf,pos,val,len,2)
+#define RPSIVALS(buf,pos,val,len) SSMBMACRO(RSIVALS,buf,pos,val,len,4)
+
+#define DBG_RW_PCVAL(charmode,string,depth,base,read,inbuf,outbuf,len) \
+       { RW_PCVAL(read,inbuf,outbuf,len) \
+       DEBUG(5,("%s%04x %s: ", \
+             tab_depth(depth), base,string)); \
+    if (charmode) print_asc(5, (unsigned char*)(outbuf), (len)); else \
+       { int idx; for (idx = 0; idx < len; idx++) { DEBUG(5,("%02x ", (outbuf)[idx])); } } \
+       DEBUG(5,("\n")); }
+
+#define DBG_RW_PSVAL(charmode,string,depth,base,read,big_endian,inbuf,outbuf,len) \
+       { RW_PSVAL(read,big_endian,inbuf,outbuf,len) \
+       DEBUG(5,("%s%04x %s: ", \
+             tab_depth(depth), base,string)); \
+    if (charmode) print_asc(5, (unsigned char*)(outbuf), 2*(len)); else \
+       { int idx; for (idx = 0; idx < len; idx++) { DEBUG(5,("%04x ", (outbuf)[idx])); } } \
+       DEBUG(5,("\n")); }
+
+#define DBG_RW_PIVAL(charmode,string,depth,base,read,big_endian,inbuf,outbuf,len) \
+       { RW_PIVAL(read,big_endian,inbuf,outbuf,len) \
+       DEBUG(5,("%s%04x %s: ", \
+             tab_depth(depth), base,string)); \
+    if (charmode) print_asc(5, (unsigned char*)(outbuf), 4*(len)); else \
+       { int idx; for (idx = 0; idx < len; idx++) { DEBUG(5,("%08x ", (outbuf)[idx])); } } \
+       DEBUG(5,("\n")); }
+
+#define DBG_RW_CVAL(string,depth,base,read,inbuf,outbuf) \
+       { RW_CVAL(read,inbuf,outbuf,0) \
+       DEBUG(5,("%s%04x %s: %02x\n", \
+             tab_depth(depth), base, string, outbuf)); }
+
+#define DBG_RW_SVAL(string,depth,base,read,big_endian,inbuf,outbuf) \
+       { RW_SVAL(read,big_endian,inbuf,outbuf,0) \
+       DEBUG(5,("%s%04x %s: %04x\n", \
+             tab_depth(depth), base, string, outbuf)); }
+
+#define DBG_RW_IVAL(string,depth,base,read,big_endian,inbuf,outbuf) \
+       { RW_IVAL(read,big_endian,inbuf,outbuf,0) \
+       DEBUG(5,("%s%04x %s: %08x\n", \
+             tab_depth(depth), base, string, outbuf)); }
+
+#endif /* _BYTEORDER_H */
+
+void E_P16 (unsigned char *p14, unsigned char *p16);
+void E_P24 (unsigned char *p21, unsigned char *c8, unsigned char *p24);
+void D_P16 (unsigned char *p14, unsigned char *in, unsigned char *out);
+void SMBOWFencrypt (uchar passwd[16], uchar * c8, uchar p24[24]);
+
+void mdfour (unsigned char *out, unsigned char *in, int n);
+
+
+/*
+ * base64.c -- base-64 conversion routines.
+ *
+ * For license terms, see the file COPYING in this directory.
+ *
+ * This base 64 encoding is defined in RFC2045 section 6.8,
+ * "Base64 Content-Transfer-Encoding", but lines must not be broken in the
+ * scheme used here.
+ */
+
+static const char base64digits[] =
+  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
+
+#define BAD    -1
+static const char base64val[] = {
+  BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD,
+    BAD,
+  BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD,
+    BAD,
+  BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, 62, BAD, BAD, BAD,
+    63,
+  52, 53, 54, 55, 56, 57, 58, 59, 60, 61, BAD, BAD, BAD, BAD, BAD, BAD,
+  BAD, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
+  15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, BAD, BAD, BAD, BAD, BAD,
+  BAD, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
+  41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, BAD, BAD, BAD, BAD, BAD
+};
+#define DECODE64(c)  (isascii(c) ? base64val[c] : BAD)
+
+void
+spa_bits_to_base64 (unsigned char *out, const unsigned char *in, int inlen)
+/* raw bytes in quasi-big-endian order to base 64 string (NUL-terminated) */
+{
+  for (; inlen >= 3; inlen -= 3)
+    {
+      *out++ = base64digits[in[0] >> 2];
+      *out++ = base64digits[((in[0] << 4) & 0x30) | (in[1] >> 4)];
+      *out++ = base64digits[((in[1] << 2) & 0x3c) | (in[2] >> 6)];
+      *out++ = base64digits[in[2] & 0x3f];
+      in += 3;
+    }
+  if (inlen > 0)
+    {
+      unsigned char fragment;
+
+      *out++ = base64digits[in[0] >> 2];
+      fragment = (in[0] << 4) & 0x30;
+      if (inlen > 1)
+       fragment |= in[1] >> 4;
+      *out++ = base64digits[fragment];
+      *out++ = (inlen < 2) ? '=' : base64digits[(in[1] << 2) & 0x3c];
+      *out++ = '=';
+    }
+  *out = '\0';
+}
+
+
+/* The outlength parameter was added by PH, December 2004 */
+
+int
+spa_base64_to_bits (char *out, int outlength, const char *in)
+/* base 64 to raw bytes in quasi-big-endian order, returning count of bytes */
+{
+  int len = 0;
+  register unsigned char digit1, digit2, digit3, digit4;
+
+  if (in[0] == '+' && in[1] == ' ')
+    in += 2;
+  if (*in == '\r')
+    return (0);
+
+  do
+    {
+      if (len >= outlength)                   /* Added by PH */
+        return (-1);                          /* Added by PH */
+      digit1 = in[0];
+      if (DECODE64 (digit1) == BAD)
+       return (-1);
+      digit2 = in[1];
+      if (DECODE64 (digit2) == BAD)
+       return (-1);
+      digit3 = in[2];
+      if (digit3 != '=' && DECODE64 (digit3) == BAD)
+       return (-1);
+      digit4 = in[3];
+      if (digit4 != '=' && DECODE64 (digit4) == BAD)
+       return (-1);
+      in += 4;
+      *out++ = (DECODE64 (digit1) << 2) | (DECODE64 (digit2) >> 4);
+      ++len;
+      if (digit3 != '=')
+       {
+         if (len >= outlength)                   /* Added by PH */
+           return (-1);                          /* Added by PH */
+         *out++ =
+           ((DECODE64 (digit2) << 4) & 0xf0) | (DECODE64 (digit3) >> 2);
+         ++len;
+         if (digit4 != '=')
+           {
+             if (len >= outlength)                   /* Added by PH */
+               return (-1);                          /* Added by PH */
+             *out++ = ((DECODE64 (digit3) << 6) & 0xc0) | DECODE64 (digit4);
+             ++len;
+           }
+       }
+    }
+  while (*in && *in != '\r' && digit4 != '=');
+
+  return (len);
+}
+
+
+#define uchar unsigned char
+
+static uchar perm1[56] = { 57, 49, 41, 33, 25, 17, 9,
+  1, 58, 50, 42, 34, 26, 18,
+  10, 2, 59, 51, 43, 35, 27,
+  19, 11, 3, 60, 52, 44, 36,
+  63, 55, 47, 39, 31, 23, 15,
+  7, 62, 54, 46, 38, 30, 22,
+  14, 6, 61, 53, 45, 37, 29,
+  21, 13, 5, 28, 20, 12, 4
+};
+
+static uchar perm2[48] = { 14, 17, 11, 24, 1, 5,
+  3, 28, 15, 6, 21, 10,
+  23, 19, 12, 4, 26, 8,
+  16, 7, 27, 20, 13, 2,
+  41, 52, 31, 37, 47, 55,
+  30, 40, 51, 45, 33, 48,
+  44, 49, 39, 56, 34, 53,
+  46, 42, 50, 36, 29, 32
+};
+
+static uchar perm3[64] = { 58, 50, 42, 34, 26, 18, 10, 2,
+  60, 52, 44, 36, 28, 20, 12, 4,
+  62, 54, 46, 38, 30, 22, 14, 6,
+  64, 56, 48, 40, 32, 24, 16, 8,
+  57, 49, 41, 33, 25, 17, 9, 1,
+  59, 51, 43, 35, 27, 19, 11, 3,
+  61, 53, 45, 37, 29, 21, 13, 5,
+  63, 55, 47, 39, 31, 23, 15, 7
+};
+
+static uchar perm4[48] = { 32, 1, 2, 3, 4, 5,
+  4, 5, 6, 7, 8, 9,
+  8, 9, 10, 11, 12, 13,
+  12, 13, 14, 15, 16, 17,
+  16, 17, 18, 19, 20, 21,
+  20, 21, 22, 23, 24, 25,
+  24, 25, 26, 27, 28, 29,
+  28, 29, 30, 31, 32, 1
+};
+
+static uchar perm5[32] = { 16, 7, 20, 21,
+  29, 12, 28, 17,
+  1, 15, 23, 26,
+  5, 18, 31, 10,
+  2, 8, 24, 14,
+  32, 27, 3, 9,
+  19, 13, 30, 6,
+  22, 11, 4, 25
+};
+
+
+static uchar perm6[64] = { 40, 8, 48, 16, 56, 24, 64, 32,
+  39, 7, 47, 15, 55, 23, 63, 31,
+  38, 6, 46, 14, 54, 22, 62, 30,
+  37, 5, 45, 13, 53, 21, 61, 29,
+  36, 4, 44, 12, 52, 20, 60, 28,
+  35, 3, 43, 11, 51, 19, 59, 27,
+  34, 2, 42, 10, 50, 18, 58, 26,
+  33, 1, 41, 9, 49, 17, 57, 25
+};
+
+
+static uchar sc[16] = { 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1 };
+
+static uchar sbox[8][4][16] = {
+  {{14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7},
+   {0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8},
+   {4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0},
+   {15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13}},
+
+  {{15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10},
+   {3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5},
+   {0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15},
+   {13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9}},
+
+  {{10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8},
+   {13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1},
+   {13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7},
+   {1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12}},
+
+  {{7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15},
+   {13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9},
+   {10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4},
+   {3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14}},
+
+  {{2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9},
+   {14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6},
+   {4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14},
+   {11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3}},
+
+  {{12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11},
+   {10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8},
+   {9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6},
+   {4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13}},
+
+  {{4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1},
+   {13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6},
+   {1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2},
+   {6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12}},
+
+  {{13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7},
+   {1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2},
+   {7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8},
+   {2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11}}
+};
+
+static void
+permute (char *out, char *in, uchar * p, int n)
+{
+  int i;
+  for (i = 0; i < n; i++)
+    out[i] = in[p[i] - 1];
+}
+
+static void
+lshift (char *d, int count, int n)
+{
+  char out[64];
+  int i;
+  for (i = 0; i < n; i++)
+    out[i] = d[(i + count) % n];
+  for (i = 0; i < n; i++)
+    d[i] = out[i];
+}
+
+static void
+concat (char *out, char *in1, char *in2, int l1, int l2)
+{
+  while (l1--)
+    *out++ = *in1++;
+  while (l2--)
+    *out++ = *in2++;
+}
+
+static void
+xor (char *out, char *in1, char *in2, int n)
+{
+  int i;
+  for (i = 0; i < n; i++)
+    out[i] = in1[i] ^ in2[i];
+}
+
+static void
+dohash (char *out, char *in, char *key, int forw)
+{
+  int i, j, k;
+  char pk1[56];
+  char c[28];
+  char d[28];
+  char cd[56];
+  char ki[16][48];
+  char pd1[64];
+  char l[32], r[32];
+  char rl[64];
+
+  permute (pk1, key, perm1, 56);
+
+  for (i = 0; i < 28; i++)
+    c[i] = pk1[i];
+  for (i = 0; i < 28; i++)
+    d[i] = pk1[i + 28];
+
+  for (i = 0; i < 16; i++)
+    {
+      lshift (c, sc[i], 28);
+      lshift (d, sc[i], 28);
+
+      concat (cd, c, d, 28, 28);
+      permute (ki[i], cd, perm2, 48);
+    }
+
+  permute (pd1, in, perm3, 64);
+
+  for (j = 0; j < 32; j++)
+    {
+      l[j] = pd1[j];
+      r[j] = pd1[j + 32];
+    }
+
+  for (i = 0; i < 16; i++)
+    {
+      char er[48];
+      char erk[48];
+      char b[8][6];
+      char cb[32];
+      char pcb[32];
+      char r2[32];
+
+      permute (er, r, perm4, 48);
+
+      xor (erk, er, ki[forw ? i : 15 - i], 48);
+
+      for (j = 0; j < 8; j++)
+       for (k = 0; k < 6; k++)
+         b[j][k] = erk[j * 6 + k];
+
+      for (j = 0; j < 8; j++)
+       {
+         int m, n;
+         m = (b[j][0] << 1) | b[j][5];
+
+         n = (b[j][1] << 3) | (b[j][2] << 2) | (b[j][3] << 1) | b[j][4];
+
+         for (k = 0; k < 4; k++)
+           b[j][k] = (sbox[j][m][n] & (1 << (3 - k))) ? 1 : 0;
+       }
+
+      for (j = 0; j < 8; j++)
+       for (k = 0; k < 4; k++)
+         cb[j * 4 + k] = b[j][k];
+      permute (pcb, cb, perm5, 32);
+
+      xor (r2, l, pcb, 32);
+
+      for (j = 0; j < 32; j++)
+       l[j] = r[j];
+
+      for (j = 0; j < 32; j++)
+       r[j] = r2[j];
+    }
+
+  concat (rl, r, l, 32, 32);
+
+  permute (out, rl, perm6, 64);
+}
+
+static void
+str_to_key (unsigned char *str, unsigned char *key)
+{
+  int i;
+
+  key[0] = str[0] >> 1;
+  key[1] = ((str[0] & 0x01) << 6) | (str[1] >> 2);
+  key[2] = ((str[1] & 0x03) << 5) | (str[2] >> 3);
+  key[3] = ((str[2] & 0x07) << 4) | (str[3] >> 4);
+  key[4] = ((str[3] & 0x0F) << 3) | (str[4] >> 5);
+  key[5] = ((str[4] & 0x1F) << 2) | (str[5] >> 6);
+  key[6] = ((str[5] & 0x3F) << 1) | (str[6] >> 7);
+  key[7] = str[6] & 0x7F;
+  for (i = 0; i < 8; i++)
+    {
+      key[i] = (key[i] << 1);
+    }
+}
+
+
+static void
+smbhash (unsigned char *out, unsigned char *in, unsigned char *key, int forw)
+{
+  int i;
+  char outb[64];
+  char inb[64];
+  char keyb[64];
+  unsigned char key2[8];
+
+  str_to_key (key, key2);
+
+  for (i = 0; i < 64; i++)
+    {
+      inb[i] = (in[i / 8] & (1 << (7 - (i % 8)))) ? 1 : 0;
+      keyb[i] = (key2[i / 8] & (1 << (7 - (i % 8)))) ? 1 : 0;
+      outb[i] = 0;
+    }
+
+  dohash (outb, inb, keyb, forw);
+
+  for (i = 0; i < 8; i++)
+    {
+      out[i] = 0;
+    }
+
+  for (i = 0; i < 64; i++)
+    {
+      if (outb[i])
+       out[i / 8] |= (1 << (7 - (i % 8)));
+    }
+}
+
+void
+E_P16 (unsigned char *p14, unsigned char *p16)
+{
+  unsigned char sp8[8] = { 0x4b, 0x47, 0x53, 0x21, 0x40, 0x23, 0x24, 0x25 };
+  smbhash (p16, sp8, p14, 1);
+  smbhash (p16 + 8, sp8, p14 + 7, 1);
+}
+
+void
+E_P24 (unsigned char *p21, unsigned char *c8, unsigned char *p24)
+{
+  smbhash (p24, c8, p21, 1);
+  smbhash (p24 + 8, c8, p21 + 7, 1);
+  smbhash (p24 + 16, c8, p21 + 14, 1);
+}
+
+void
+D_P16 (unsigned char *p14, unsigned char *in, unsigned char *out)
+{
+  smbhash (out, in, p14, 0);
+  smbhash (out + 8, in + 8, p14 + 7, 0);
+}
+
+/****************************************************************************
+ Like strncpy but always null terminates. Make sure there is room!
+ The variable n should always be one less than the available size.
+****************************************************************************/
+
+char *
+StrnCpy (char *dest, const char *src, size_t n)
+{
+  char *d = dest;
+  if (!dest)
+    return (NULL);
+  if (!src)
+    {
+      *dest = 0;
+      return (dest);
+    }
+  while (n-- && (*d++ = *src++));
+  *d = 0;
+  return (dest);
+}
+
+size_t
+skip_multibyte_char (char c)
+{
+  /* bogus if to get rid of unused compiler warning */
+  if (c)
+    return 0;
+  else
+    return 0;
+}
+
+
+/*******************************************************************
+safe string copy into a known length string. maxlength does not
+include the terminating zero.
+********************************************************************/
+
+char *
+safe_strcpy (char *dest, const char *src, size_t maxlength)
+{
+  size_t len;
+
+  if (!dest)
+    {
+      DEBUG (0, ("ERROR: NULL dest in safe_strcpy\n"));
+      return NULL;
+    }
+
+  if (!src)
+    {
+      *dest = 0;
+      return dest;
+    }
+
+  len = strlen (src);
+
+  if (len > maxlength)
+    {
+      DEBUG (0, ("ERROR: string overflow by %d in safe_strcpy [%.50s]\n",
+                (int) (len - maxlength), src));
+      len = maxlength;
+    }
+
+  memcpy (dest, src, len);
+  dest[len] = 0;
+  return dest;
+}
+
+
+void
+strupper (char *s)
+{
+  while (*s)
+    {
+      {
+       size_t skip = skip_multibyte_char (*s);
+       if (skip != 0)
+         s += skip;
+       else
+         {
+           if (islower ((unsigned char)(*s)))
+             *s = toupper (*s);
+           s++;
+         }
+      }
+    }
+}
+
+
+/*
+ This implements the X/Open SMB password encryption
+ It takes a password, a 8 byte "crypt key" and puts 24 bytes of
+ encrypted password into p24
+ */
+
+void
+spa_smb_encrypt (uchar * passwd, uchar * c8, uchar * p24)
+{
+  uchar p14[15], p21[21];
+
+  memset (p21, '\0', 21);
+  memset (p14, '\0', 14);
+  StrnCpy ((char *) p14, (char *) passwd, 14);
+
+  strupper ((char *) p14);
+  E_P16 (p14, p21);
+
+  SMBOWFencrypt (p21, c8, p24);
+
+#ifdef DEBUG_PASSWORD
+  DEBUG (100, ("spa_smb_encrypt: lm#, challenge, response\n"));
+  dump_data (100, (char *) p21, 16);
+  dump_data (100, (char *) c8, 8);
+  dump_data (100, (char *) p24, 24);
+#endif
+}
+
+/* Routines for Windows NT MD4 Hash functions. */
+static int
+_my_wcslen (int16x * str)
+{
+  int len = 0;
+  while (*str++ != 0)
+    len++;
+  return len;
+}
+
+/*
+ * Convert a string into an NT UNICODE string.
+ * Note that regardless of processor type
+ * this must be in intel (little-endian)
+ * format.
+ */
+
+static int
+_my_mbstowcs (int16x * dst, uchar * src, int len)
+{
+  int i;
+  int16x val;
+
+  for (i = 0; i < len; i++)
+    {
+      val = *src;
+      SSVAL (dst, 0, val);
+      dst++;
+      src++;
+      if (val == 0)
+       break;
+    }
+  return i;
+}
+
+/*
+ * Creates the MD4 Hash of the users password in NT UNICODE.
+ */
+
+void
+E_md4hash (uchar * passwd, uchar * p16)
+{
+  int len;
+  int16x wpwd[129];
+
+  /* Password cannot be longer than 128 characters */
+  len = strlen ((char *) passwd);
+  if (len > 128)
+    len = 128;
+  /* Password must be converted to NT unicode */
+  _my_mbstowcs (wpwd, passwd, len);
+  wpwd[len] = 0;               /* Ensure string is null terminated */
+  /* Calculate length in bytes */
+  len = _my_wcslen (wpwd) * sizeof (int16x);
+
+  mdfour (p16, (unsigned char *) wpwd, len);
+}
+
+/* Does both the NT and LM owfs of a user's password */
+void
+nt_lm_owf_gen (char *pwd, uchar nt_p16[16], uchar p16[16])
+{
+  char passwd[130];
+
+  memset (passwd, '\0', 130);
+  safe_strcpy (passwd, pwd, sizeof (passwd) - 1);
+
+  /* Calculate the MD4 hash (NT compatible) of the password */
+  memset (nt_p16, '\0', 16);
+  E_md4hash ((uchar *) passwd, nt_p16);
+
+#ifdef DEBUG_PASSWORD
+  DEBUG (100, ("nt_lm_owf_gen: pwd, nt#\n"));
+  dump_data (120, passwd, strlen (passwd));
+  dump_data (100, (char *) nt_p16, 16);
+#endif
+
+  /* Mangle the passwords into Lanman format */
+  passwd[14] = '\0';
+  strupper (passwd);
+
+  /* Calculate the SMB (lanman) hash functions of the password */
+
+  memset (p16, '\0', 16);
+  E_P16 ((uchar *) passwd, (uchar *) p16);
+
+#ifdef DEBUG_PASSWORD
+  DEBUG (100, ("nt_lm_owf_gen: pwd, lm#\n"));
+  dump_data (120, passwd, strlen (passwd));
+  dump_data (100, (char *) p16, 16);
+#endif
+  /* clear out local copy of user's password (just being paranoid). */
+  memset (passwd, '\0', sizeof (passwd));
+}
+
+/* Does the des encryption from the NT or LM MD4 hash. */
+void
+SMBOWFencrypt (uchar passwd[16], uchar * c8, uchar p24[24])
+{
+  uchar p21[21];
+
+  memset (p21, '\0', 21);
+
+  memcpy (p21, passwd, 16);
+  E_P24 (p21, c8, p24);
+}
+
+/* Does the des encryption from the FIRST 8 BYTES of the NT or LM MD4 hash. */
+void
+NTLMSSPOWFencrypt (uchar passwd[8], uchar * ntlmchalresp, uchar p24[24])
+{
+  uchar p21[21];
+
+  memset (p21, '\0', 21);
+  memcpy (p21, passwd, 8);
+  memset (p21 + 8, 0xbd, 8);
+
+  E_P24 (p21, ntlmchalresp, p24);
+#ifdef DEBUG_PASSWORD
+  DEBUG (100, ("NTLMSSPOWFencrypt: p21, c8, p24\n"));
+  dump_data (100, (char *) p21, 21);
+  dump_data (100, (char *) ntlmchalresp, 8);
+  dump_data (100, (char *) p24, 24);
+#endif
+}
+
+
+/* Does the NT MD4 hash then des encryption. */
+
+void
+spa_smb_nt_encrypt (uchar * passwd, uchar * c8, uchar * p24)
+{
+  uchar p21[21];
+
+  memset (p21, '\0', 21);
+
+  E_md4hash (passwd, p21);
+  SMBOWFencrypt (p21, c8, p24);
+
+#ifdef DEBUG_PASSWORD
+  DEBUG (100, ("spa_smb_nt_encrypt: nt#, challenge, response\n"));
+  dump_data (100, (char *) p21, 16);
+  dump_data (100, (char *) c8, 8);
+  dump_data (100, (char *) p24, 24);
+#endif
+}
+
+static uint32x A, B, C, D;
+
+static uint32x
+F (uint32x X, uint32x Y, uint32x Z)
+{
+  return (X & Y) | ((~X) & Z);
+}
+
+static uint32x
+G (uint32x X, uint32x Y, uint32x Z)
+{
+  return (X & Y) | (X & Z) | (Y & Z);
+}
+
+static uint32x
+H (uint32x X, uint32x Y, uint32x Z)
+{
+  return X ^ Y ^ Z;
+}
+
+static uint32x
+lshift_a (uint32x x, int s)
+{
+  x &= 0xFFFFFFFF;
+  return ((x << s) & 0xFFFFFFFF) | (x >> (32 - s));
+}
+
+#define ROUND1(a,b,c,d,k,s) a = lshift_a(a + F(b,c,d) + X[k], s)
+#define ROUND2(a,b,c,d,k,s) a = lshift_a(a + G(b,c,d) + X[k] + (uint32x)0x5A827999,s)
+#define ROUND3(a,b,c,d,k,s) a = lshift_a(a + H(b,c,d) + X[k] + (uint32x)0x6ED9EBA1,s)
+
+/* this applies md4 to 64 byte chunks */
+static void
+spa_mdfour64 (uint32x * M)
+{
+  int j;
+  uint32x AA, BB, CC, DD;
+  uint32x X[16];
+
+  for (j = 0; j < 16; j++)
+    X[j] = M[j];
+
+  AA = A;
+  BB = B;
+  CC = C;
+  DD = D;
+
+  ROUND1 (A, B, C, D, 0, 3);
+  ROUND1 (D, A, B, C, 1, 7);
+  ROUND1 (C, D, A, B, 2, 11);
+  ROUND1 (B, C, D, A, 3, 19);
+  ROUND1 (A, B, C, D, 4, 3);
+  ROUND1 (D, A, B, C, 5, 7);
+  ROUND1 (C, D, A, B, 6, 11);
+  ROUND1 (B, C, D, A, 7, 19);
+  ROUND1 (A, B, C, D, 8, 3);
+  ROUND1 (D, A, B, C, 9, 7);
+  ROUND1 (C, D, A, B, 10, 11);
+  ROUND1 (B, C, D, A, 11, 19);
+  ROUND1 (A, B, C, D, 12, 3);
+  ROUND1 (D, A, B, C, 13, 7);
+  ROUND1 (C, D, A, B, 14, 11);
+  ROUND1 (B, C, D, A, 15, 19);
+
+  ROUND2 (A, B, C, D, 0, 3);
+  ROUND2 (D, A, B, C, 4, 5);
+  ROUND2 (C, D, A, B, 8, 9);
+  ROUND2 (B, C, D, A, 12, 13);
+  ROUND2 (A, B, C, D, 1, 3);
+  ROUND2 (D, A, B, C, 5, 5);
+  ROUND2 (C, D, A, B, 9, 9);
+  ROUND2 (B, C, D, A, 13, 13);
+  ROUND2 (A, B, C, D, 2, 3);
+  ROUND2 (D, A, B, C, 6, 5);
+  ROUND2 (C, D, A, B, 10, 9);
+  ROUND2 (B, C, D, A, 14, 13);
+  ROUND2 (A, B, C, D, 3, 3);
+  ROUND2 (D, A, B, C, 7, 5);
+  ROUND2 (C, D, A, B, 11, 9);
+  ROUND2 (B, C, D, A, 15, 13);
+
+  ROUND3 (A, B, C, D, 0, 3);
+  ROUND3 (D, A, B, C, 8, 9);
+  ROUND3 (C, D, A, B, 4, 11);
+  ROUND3 (B, C, D, A, 12, 15);
+  ROUND3 (A, B, C, D, 2, 3);
+  ROUND3 (D, A, B, C, 10, 9);
+  ROUND3 (C, D, A, B, 6, 11);
+  ROUND3 (B, C, D, A, 14, 15);
+  ROUND3 (A, B, C, D, 1, 3);
+  ROUND3 (D, A, B, C, 9, 9);
+  ROUND3 (C, D, A, B, 5, 11);
+  ROUND3 (B, C, D, A, 13, 15);
+  ROUND3 (A, B, C, D, 3, 3);
+  ROUND3 (D, A, B, C, 11, 9);
+  ROUND3 (C, D, A, B, 7, 11);
+  ROUND3 (B, C, D, A, 15, 15);
+
+  A += AA;
+  B += BB;
+  C += CC;
+  D += DD;
+
+  A &= 0xFFFFFFFF;
+  B &= 0xFFFFFFFF;
+  C &= 0xFFFFFFFF;
+  D &= 0xFFFFFFFF;
+
+  for (j = 0; j < 16; j++)
+    X[j] = 0;
+}
+
+static void
+copy64 (uint32x * M, unsigned char *in)
+{
+  int i;
+
+  for (i = 0; i < 16; i++)
+    M[i] = (in[i * 4 + 3] << 24) | (in[i * 4 + 2] << 16) |
+      (in[i * 4 + 1] << 8) | (in[i * 4 + 0] << 0);
+}
+
+static void
+copy4 (unsigned char *out, uint32x x)
+{
+  out[0] = x & 0xFF;
+  out[1] = (x >> 8) & 0xFF;
+  out[2] = (x >> 16) & 0xFF;
+  out[3] = (x >> 24) & 0xFF;
+}
+
+/* produce a md4 message digest from data of length n bytes */
+void
+mdfour (unsigned char *out, unsigned char *in, int n)
+{
+  unsigned char buf[128];
+  uint32x M[16];
+  uint32x b = n * 8;
+  int i;
+
+  A = 0x67452301;
+  B = 0xefcdab89;
+  C = 0x98badcfe;
+  D = 0x10325476;
+
+  while (n > 64)
+    {
+      copy64 (M, in);
+      spa_mdfour64 (M);
+      in += 64;
+      n -= 64;
+    }
+
+  for (i = 0; i < 128; i++)
+    buf[i] = 0;
+  memcpy (buf, in, n);
+  buf[n] = 0x80;
+
+  if (n <= 55)
+    {
+      copy4 (buf + 56, b);
+      copy64 (M, buf);
+      spa_mdfour64 (M);
+    }
+  else
+    {
+      copy4 (buf + 120, b);
+      copy64 (M, buf);
+      spa_mdfour64 (M);
+      copy64 (M, buf + 64);
+      spa_mdfour64 (M);
+    }
+
+  for (i = 0; i < 128; i++)
+    buf[i] = 0;
+  copy64 (M, buf);
+
+  copy4 (out, A);
+  copy4 (out + 4, B);
+  copy4 (out + 8, C);
+  copy4 (out + 12, D);
+
+  A = B = C = D = 0;
+}
+
+char versionString[] = "libntlm version 0.21";
+
+/* Utility routines that handle NTLM auth structures. */
+
+/* The [IS]VAL macros are to take care of byte order for non-Intel
+ * Machines -- I think this file is OK, but it hasn't been tested.
+ * The other files (the ones stolen from Samba) should be OK.
+ */
+
+
+/* I am not crazy about these macros -- they seem to have gotten
+ * a bit complex.  A new scheme for handling string/buffer fields
+ * in the structures probably needs to be designed
+ */
+
+#define spa_bytes_add(ptr, header, buf, count) \
+{ \
+if (buf != NULL  &&  count) \
+  { \
+  SSVAL(&ptr->header.len,0,count); \
+  SSVAL(&ptr->header.maxlen,0,count); \
+  SIVAL(&ptr->header.offset,0,((ptr->buffer - ((uint8x*)ptr)) + ptr->bufIndex)); \
+  memcpy(ptr->buffer+ptr->bufIndex, buf, count); \
+  ptr->bufIndex += count; \
+  } \
+else \
+  { \
+  ptr->header.len = \
+  ptr->header.maxlen = 0; \
+  SIVAL(&ptr->header.offset,0,((ptr->buffer - ((uint8x*)ptr)) + ptr->bufIndex)); \
+  } \
+}
+
+#define spa_string_add(ptr, header, string) \
+{ \
+char *p = string; \
+int len = 0; \
+if (p) len = strlen(p); \
+spa_bytes_add(ptr, header, ((unsigned char*)p), len); \
+}
+
+#define spa_unicode_add_string(ptr, header, string) \
+{ \
+char *p = string; \
+unsigned char *b = NULL; \
+int len = 0; \
+if (p) \
+  { \
+  len = strlen(p); \
+  b = strToUnicode(p); \
+  } \
+spa_bytes_add(ptr, header, b, len*2); \
+}
+
+
+#define GetUnicodeString(structPtr, header) \
+unicodeToString(((char*)structPtr) + IVAL(&structPtr->header.offset,0) , SVAL(&structPtr->header.len,0)/2)
+#define GetString(structPtr, header) \
+toString((((char *)structPtr) + IVAL(&structPtr->header.offset,0)), SVAL(&structPtr->header.len,0))
+#define DumpBuffer(fp, structPtr, header) \
+dumpRaw(fp,((unsigned char*)structPtr)+IVAL(&structPtr->header.offset,0),SVAL(&structPtr->header.len,0))
+
+
+static void
+dumpRaw (FILE * fp, unsigned char *buf, size_t len)
+{
+  int i;
+
+  for (i = 0; i < len; ++i)
+    fprintf (fp, "%02x ", buf[i]);
+
+  fprintf (fp, "\n");
+}
+
+char *
+unicodeToString (char *p, size_t len)
+{
+  int i;
+  static char buf[1024];
+
+  assert (len + 1 < sizeof buf);
+
+  for (i = 0; i < len; ++i)
+    {
+      buf[i] = *p & 0x7f;
+      p += 2;
+    }
+
+  buf[i] = '\0';
+  return buf;
+}
+
+static unsigned char *
+strToUnicode (char *p)
+{
+  static unsigned char buf[1024];
+  size_t l = strlen (p);
+  int i = 0;
+
+  assert (l * 2 < sizeof buf);
+
+  while (l--)
+    {
+      buf[i++] = *p++;
+      buf[i++] = 0;
+    }
+
+  return buf;
+}
+
+static unsigned char *
+toString (char *p, size_t len)
+{
+  static unsigned char buf[1024];
+
+  assert (len + 1 < sizeof buf);
+
+  memcpy (buf, p, len);
+  buf[len] = 0;
+  return buf;
+}
+
+void
+dumpSmbNtlmAuthRequest (FILE * fp, SPAAuthRequest * request)
+{
+  fprintf (fp, "NTLM Request:\n");
+  fprintf (fp, "      Ident = %s\n", request->ident);
+  fprintf (fp, "      mType = %d\n", IVAL (&request->msgType, 0));
+  fprintf (fp, "      Flags = %08x\n", IVAL (&request->flags, 0));
+  fprintf (fp, "       User = %s\n", GetString (request, user));
+  fprintf (fp, "     Domain = %s\n", GetString (request, domain));
+}
+
+void
+dumpSmbNtlmAuthChallenge (FILE * fp, SPAAuthChallenge * challenge)
+{
+  fprintf (fp, "NTLM Challenge:\n");
+  fprintf (fp, "      Ident = %s\n", challenge->ident);
+  fprintf (fp, "      mType = %d\n", IVAL (&challenge->msgType, 0));
+  fprintf (fp, "     Domain = %s\n", GetUnicodeString (challenge, uDomain));
+  fprintf (fp, "      Flags = %08x\n", IVAL (&challenge->flags, 0));
+  fprintf (fp, "  Challenge = ");
+  dumpRaw (fp, challenge->challengeData, 8);
+}
+
+void
+dumpSmbNtlmAuthResponse (FILE * fp, SPAAuthResponse * response)
+{
+  fprintf (fp, "NTLM Response:\n");
+  fprintf (fp, "      Ident = %s\n", response->ident);
+  fprintf (fp, "      mType = %d\n", IVAL (&response->msgType, 0));
+  fprintf (fp, "     LmResp = ");
+  DumpBuffer (fp, response, lmResponse);
+  fprintf (fp, "     NTResp = ");
+  DumpBuffer (fp, response, ntResponse);
+  fprintf (fp, "     Domain = %s\n", GetUnicodeString (response, uDomain));
+  fprintf (fp, "       User = %s\n", GetUnicodeString (response, uUser));
+  fprintf (fp, "        Wks = %s\n", GetUnicodeString (response, uWks));
+  fprintf (fp, "       sKey = ");
+  DumpBuffer (fp, response, sessionKey);
+  fprintf (fp, "      Flags = %08x\n", IVAL (&response->flags, 0));
+}
+
+void
+spa_build_auth_request (SPAAuthRequest * request, char *user, char *domain)
+{
+  char *u = strdup (user);
+  char *p = strchr (u, '@');
+
+  if (p)
+    {
+      if (!domain)
+       domain = p + 1;
+      *p = '\0';
+    }
+
+  request->bufIndex = 0;
+  memcpy (request->ident, "NTLMSSP\0\0\0", 8);
+  SIVAL (&request->msgType, 0, 1);
+  SIVAL (&request->flags, 0, 0x0000b207);      /* have to figure out what these mean */
+  spa_string_add (request, user, u);
+  spa_string_add (request, domain, domain);
+  free (u);
+}
+
+
+
+void
+spa_build_auth_challenge (SPAAuthRequest * request, SPAAuthChallenge * challenge)
+{
+  char chalstr[8];
+  int i;
+  int p = (int)getpid();
+  int random_seed = (int)time(NULL) ^ ((p << 16) | p);
+
+  request = request;  /* Added by PH to stop compilers whinging */
+
+  /* Ensure challenge data is cleared, in case it isn't all used. This
+  patch added by PH on suggestion of Russell King */
+
+  memset(challenge, 0, sizeof(SPAAuthChallenge));
+
+  challenge->bufIndex = 0;
+  memcpy (challenge->ident, "NTLMSSP\0", 8);
+  SIVAL (&challenge->msgType, 0, 2);
+  SIVAL (&challenge->flags, 0, 0x00008201);
+  SIVAL (&challenge->uDomain.len, 0, 0x0000);
+  SIVAL (&challenge->uDomain.maxlen, 0, 0x0000);
+  SIVAL (&challenge->uDomain.offset, 0, 0x00002800);
+
+  /* generate eight pseudo random bytes (method ripped from host.c) */
+
+  for(i=0;i<8;i++) {
+    chalstr[i] = (unsigned char)(random_seed >> 16) % 256;
+    random_seed = (1103515245 - (chalstr[i])) * random_seed + 12345;
+  };
+
+  memcpy(challenge->challengeData,chalstr,8);
+}
+
+
+
+
+/* This is the original source of this function, preserved here for reference.
+The new version below was re-organized by PH following a patch and some further
+suggestions from Mark Lyda to fix the problem that is described at the head of
+this module. At the same time, I removed the untidiness in the code below that
+involves the "d" and "domain" variables. */
+
+#ifdef NEVER
+void
+spa_build_auth_response (SPAAuthChallenge * challenge,
+                        SPAAuthResponse * response, char *user,
+                        char *password)
+{
+  uint8x lmRespData[24];
+  uint8x ntRespData[24];
+  char *d = strdup (GetUnicodeString (challenge, uDomain));
+  char *domain = d;
+  char *u = strdup (user);
+  char *p = strchr (u, '@');
+
+  if (p)
+    {
+      domain = p + 1;
+      *p = '\0';
+    }
+
+  spa_smb_encrypt ((uchar *)password, challenge->challengeData, lmRespData);
+  spa_smb_nt_encrypt ((uchar *)password, challenge->challengeData, ntRespData);
+
+  response->bufIndex = 0;
+  memcpy (response->ident, "NTLMSSP\0\0\0", 8);
+  SIVAL (&response->msgType, 0, 3);
+
+  spa_bytes_add (response, lmResponse, lmRespData, 24);
+  spa_bytes_add (response, ntResponse, ntRespData, 24);
+  spa_unicode_add_string (response, uDomain, domain);
+  spa_unicode_add_string (response, uUser, u);
+  spa_unicode_add_string (response, uWks, u);
+  spa_string_add (response, sessionKey, NULL);
+
+  response->flags = challenge->flags;
+
+  free (d);
+  free (u);
+}
+#endif
+
+
+/* This is the re-organized version (see comments above) */
+
+void
+spa_build_auth_response (SPAAuthChallenge * challenge,
+                        SPAAuthResponse * response, char *user,
+                        char *password)
+{
+  uint8x lmRespData[24];
+  uint8x ntRespData[24];
+  uint32x cf = IVAL(&challenge->flags, 0);
+  char *u = strdup (user);
+  char *p = strchr (u, '@');
+  char *d = NULL;
+  char *domain;
+
+  if (p)
+    {
+    domain = p + 1;
+    *p = '\0';
+    }
+
+  else domain = d = strdup((cf & 0x1)?
+    (const char *)GetUnicodeString(challenge, uDomain) :
+    (const char *)GetString(challenge, uDomain));
+
+  spa_smb_encrypt ((uchar *)password, challenge->challengeData, lmRespData);
+  spa_smb_nt_encrypt ((uchar *)password, challenge->challengeData, ntRespData);
+
+  response->bufIndex = 0;
+  memcpy (response->ident, "NTLMSSP\0\0\0", 8);
+  SIVAL (&response->msgType, 0, 3);
+
+  spa_bytes_add (response, lmResponse, lmRespData, (cf & 0x200) ? 24 : 0);
+  spa_bytes_add (response, ntResponse, ntRespData, (cf & 0x8000) ? 24 : 0);
+
+  if (cf & 0x1) {      /* Unicode Text */
+       spa_unicode_add_string (response, uDomain, domain);
+       spa_unicode_add_string (response, uUser, u);
+       spa_unicode_add_string (response, uWks, u);
+  } else {             /* OEM Text */
+       spa_string_add (response, uDomain, domain);
+       spa_string_add (response, uUser, u);
+       spa_string_add (response, uWks, u);
+  }
+
+  spa_string_add (response, sessionKey, NULL);
+  response->flags = challenge->flags;
+
+  if (d != NULL) free (d);
+  free (u);
+}
diff --git a/src/auths/auth-spa.h b/src/auths/auth-spa.h
new file mode 100644 (file)
index 0000000..cfe1b08
--- /dev/null
@@ -0,0 +1,92 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/*
+ * This file provides the necessary methods for authenticating with
+ * Microsoft's Secure Password Authentication.
+
+ * All the code used here was torn by Marc Prud'hommeaux out of the
+ * Samba project (by Andrew Tridgell, Jeremy Allison, and others).
+ */
+
+/* December 2004: The spa_base64_to_bits() function has no length checking in
+it. I have added a check. PH */
+
+/* It seems that some systems have existing but different definitions of some
+of the following types. I received a complaint about "int16" causing
+compilation problems. So I (PH) have renamed them all, to be on the safe side.
+
+typedef signed short int16;
+typedef unsigned short uint16;
+typedef unsigned uint32;
+typedef unsigned char  uint8;
+*/
+
+typedef signed short int16x;
+typedef unsigned short uint16x;
+typedef unsigned uint32x;
+typedef unsigned char  uint8x;
+
+typedef struct
+{
+       uint16x         len;
+       uint16x         maxlen;
+       uint32x         offset;
+} SPAStrHeader;
+
+typedef struct
+{
+       char         ident[8];
+       uint32x         msgType;
+       SPAStrHeader    uDomain;
+       uint32x         flags;
+       uint8x         challengeData[8];
+       uint8x         reserved[8];
+       SPAStrHeader    emptyString;
+       uint8x         buffer[1024];
+       uint32x         bufIndex;
+} SPAAuthChallenge;
+
+
+typedef struct
+{
+       char         ident[8];
+       uint32x         msgType;
+       uint32x         flags;
+       SPAStrHeader    user;
+       SPAStrHeader    domain;
+       uint8x         buffer[1024];
+       uint32x         bufIndex;
+} SPAAuthRequest;
+
+typedef struct
+{
+       char         ident[8];
+       uint32x         msgType;
+       SPAStrHeader    lmResponse;
+       SPAStrHeader    ntResponse;
+       SPAStrHeader    uDomain;
+       SPAStrHeader    uUser;
+       SPAStrHeader    uWks;
+       SPAStrHeader    sessionKey;
+       uint32x         flags;
+       uint8x         buffer[1024];
+       uint32x         bufIndex;
+} SPAAuthResponse;
+
+#define spa_request_length(ptr) (((ptr)->buffer - (uint8x*)(ptr)) + (ptr)->bufIndex)
+
+void spa_bits_to_base64 (unsigned char *, const unsigned char *, int);
+int spa_base64_to_bits(char *, int, const char *);
+void spa_build_auth_response (SPAAuthChallenge *challenge,
+       SPAAuthResponse *response, char *user, char *password);
+void spa_build_auth_request (SPAAuthRequest *request, char *user,
+       char *domain);
+extern void spa_smb_encrypt (unsigned char * passwd, unsigned char * c8,
+                             unsigned char * p24);
+extern void spa_smb_nt_encrypt (unsigned char * passwd, unsigned char * c8,
+                                unsigned char * p24);
+extern char *unicodeToString(char *p, size_t len);
+extern void spa_build_auth_challenge(SPAAuthRequest *, SPAAuthChallenge *);
+
diff --git a/src/auths/b64decode.c b/src/auths/b64decode.c
new file mode 100644 (file)
index 0000000..0a3c2ec
--- /dev/null
@@ -0,0 +1,84 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+#include "../exim.h"
+
+
+/*************************************************
+*          Decode byte-string in base 64         *
+*************************************************/
+
+/* This function decodes a string in base 64 format as defined in RFC 2045
+(MIME) and required by the SMTP AUTH extension (RFC 2554). The decoding
+algorithm is written out in a straightforward way. Turning it into some kind of
+compact loop is messy and would probably run more slowly.
+
+Arguments:
+  code        points to the coded string, zero-terminated
+  ptr         where to put the pointer to the result, which is in
+              dynamic store, and zero-terminated
+
+Returns:      the number of bytes in the result,
+              or -1 if the input was malformed
+
+A zero is added on to the end to make it easy in cases where the result is to
+be interpreted as text. This is not included in the count. */
+
+static uschar dec64table[] = {
+  255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, /*  0-15 */
+  255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, /* 16-31 */
+  255,255,255,255,255,255,255,255,255,255,255, 62,255,255,255, 63, /* 32-47 */
+   52, 53, 54, 55, 56, 57, 58, 59, 60, 61,255,255,255,255,255,255, /* 48-63 */
+  255,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, /* 64-79 */
+   15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,255,255,255,255,255, /* 80-95 */
+  255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, /* 96-111 */
+   41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,255,255,255,255,255  /* 112-127*/
+};
+
+int
+auth_b64decode(uschar *code, uschar **ptr)
+{
+register int x, y;
+uschar *result = store_get(3*(Ustrlen(code)/4) + 1);
+
+*ptr = result;
+
+/* Each cycle of the loop handles a quantum of 4 input bytes. For the last
+quantum this may decode to 1, 2, or 3 output bytes. */
+
+while ((x = (*code++)) != 0)
+  {
+  if (x > 127 || (x = dec64table[x]) == 255) return -1;
+  if ((y = (*code++)) == 0 || (y = dec64table[y]) == 255)
+    return -1;
+  *result++ = (x << 2) | (y >> 4);
+
+  if ((x = (*code++)) == '=')
+    {
+    if (*code++ != '=' || *code != 0) return -1;
+    }
+  else
+    {
+    if (x > 127 || (x = dec64table[x]) == 255) return -1;
+    *result++ = (y << 4) | (x >> 2);
+    if ((y = (*code++)) == '=')
+      {
+      if (*code != 0) return -1;
+      }
+    else
+      {
+      if (y > 127 || (y = dec64table[y]) == 255) return -1;
+      *result++ = (x << 6) | y;
+      }
+    }
+  }
+
+*result = 0;
+return result - *ptr;
+}
+
+/* End of b64decode.c */
diff --git a/src/auths/b64encode.c b/src/auths/b64encode.c
new file mode 100644 (file)
index 0000000..509590c
--- /dev/null
@@ -0,0 +1,74 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+#include "../exim.h"
+
+
+/*************************************************
+*          Encode byte-string in base 64         *
+*************************************************/
+
+/* This function encodes a string of bytes, containing any values whatsoever,
+in base 64 as defined in RFC 2045 (MIME) and required by the SMTP AUTH
+extension (RFC 2554). The encoding algorithm is written out in a
+straightforward way. Turning it into some kind of compact loop is messy and
+would probably run more slowly.
+
+Arguments:
+  clear       points to the clear text bytes
+  len         the number of bytes to encode
+
+Returns:      a pointer to the zero-terminated base 64 string, which
+              is in working store
+*/
+
+static uschar *enc64table =
+  US"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
+
+uschar *
+auth_b64encode(uschar *clear, int len)
+{
+uschar *code = store_get(4*((len+2)/3) + 1);
+uschar *p = code;
+
+while (len-- >0)
+  {
+  register int x, y;
+
+  x = *clear++;
+  *p++ = enc64table[(x >> 2) & 63];
+
+  if (len-- <= 0)
+    {
+    *p++ = enc64table[(x << 4) & 63];
+    *p++ = '=';
+    *p++ = '=';
+    break;
+    }
+
+  y = *clear++;
+  *p++ = enc64table[((x << 4) | ((y >> 4) & 15)) & 63];
+
+  if (len-- <= 0)
+    {
+    *p++ = enc64table[(y << 2) & 63];
+    *p++ = '=';
+    break;
+    }
+
+  x = *clear++;
+  *p++ = enc64table[((y << 2) | ((x >> 6) & 3)) & 63];
+
+  *p++ = enc64table[x & 63];
+  }
+
+*p = 0;
+
+return code;
+}
+
+/* End of b64encode.c */
diff --git a/src/auths/call_pam.c b/src/auths/call_pam.c
new file mode 100644 (file)
index 0000000..710de7d
--- /dev/null
@@ -0,0 +1,205 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+#include "../exim.h"
+
+/* This module contains functions that call the PAM authentication mechanism
+defined by Sun for Solaris and also available for Linux and other OS.
+
+We can't just compile this code and allow the library mechanism to omit the
+functions if they are not wanted, because we need to have the PAM headers
+available for compiling. Therefore, compile these functions only if SUPPORT_PAM
+is defined. However, some compilers don't like compiling empty modules, so keep
+them happy with a dummy when skipping the rest. Make it reference itself to
+stop picky compilers complaining that it is unused, and put in a dummy argument
+to stop even pickier compilers complaining about infinite loops.
+Then use a mutually-recursive pair as gcc is just getting stupid. */
+
+#ifndef SUPPORT_PAM
+static void dummy(int x);
+static void dummy2(int x) { dummy(x-1); }
+static void dummy(int x) { dummy2(x-1); }
+#else  /* SUPPORT_PAM */
+
+#ifdef PAM_H_IN_PAM
+#include <pam/pam_appl.h>
+#else
+#include <security/pam_appl.h>
+#endif
+
+/* According to the specification, it should be possible to have an application
+data pointer passed to the conversation function. However, I was unable to get
+this to work on Solaris 2.6, so static variables are used instead. */
+
+static int pam_conv_had_error;
+static uschar *pam_args;
+static BOOL pam_arg_ended;
+
+
+
+/*************************************************
+*           PAM conversation function            *
+*************************************************/
+
+/* This function is passed to the PAM authentication function, and it calls it
+back when it wants data from the client. The string list is in pam_args. When
+we reach the end, we pass back an empty string once. If this function is called
+again, it will give an error response. This is protection against something
+crazy happening.
+
+Arguments:
+  num_msg        number of messages associated with the call
+  msg            points to an array of length num_msg of pam_message structures
+  resp           set to point to the response block, which has to be got by
+                   this function
+  appdata_ptr    the application data pointer - not used because in Solaris
+                   2.6 it always arrived in pam_converse() as NULL
+
+Returns:         a PAM return code
+*/
+
+static int
+pam_converse (int num_msg, PAM_CONVERSE_ARG2_TYPE **msg,
+  struct pam_response **resp, void *appdata_ptr)
+{
+int i;
+int sep = 0;
+struct pam_response *reply;
+
+if (pam_arg_ended) return PAM_CONV_ERR;
+
+reply = malloc(sizeof(struct pam_response) * num_msg);
+
+if (reply == NULL) return PAM_CONV_ERR;
+
+for (i = 0; i < num_msg; i++)
+  {
+  uschar *arg;
+  switch (msg[i]->msg_style)
+    {
+    case PAM_PROMPT_ECHO_ON:
+    case PAM_PROMPT_ECHO_OFF:
+    arg = string_nextinlist(&pam_args, &sep, big_buffer, big_buffer_size);
+    if (arg == NULL)
+      {
+      arg = US"";
+      pam_arg_ended = TRUE;
+      }
+    reply[i].resp = CS string_copy_malloc(arg); /* PAM frees resp */
+    reply[i].resp_retcode = PAM_SUCCESS;
+    break;
+
+    case PAM_TEXT_INFO:    /* Just acknowledge messages */
+    case PAM_ERROR_MSG:
+    reply[i].resp_retcode = PAM_SUCCESS;
+    reply[i].resp = NULL;
+    break;
+
+    default:  /* Must be an error of some sort... */
+    free (reply);
+    pam_conv_had_error = TRUE;
+    return PAM_CONV_ERR;
+    }
+  }
+
+*resp = reply;
+return PAM_SUCCESS;
+}
+
+
+
+/*************************************************
+*              Perform PAM authentication        *
+*************************************************/
+
+/* This function calls the PAM authentication mechanism, passing over one or
+more data strings.
+
+Arguments:
+  s        a colon-separated list of strings
+  errptr   where to point an error message
+
+Returns:   OK if authentication succeeded
+           FAIL if authentication failed
+           ERROR some other error condition
+*/
+
+int
+auth_call_pam(uschar *s, uschar **errptr)
+{
+pam_handle_t *pamh = NULL;
+struct pam_conv pamc;
+int pam_error;
+int sep = 0;
+uschar *user;
+
+/* Set up the input data structure: the address of the conversation function,
+and a pointer to application data, which we don't use because I couldn't get it
+to work under Solaris 2.6 - it always arrived in pam_converse() as NULL. */
+
+pamc.conv = pam_converse;
+pamc.appdata_ptr = NULL;
+
+/* Initialize the static data - the current input data, the error flag, and the
+flag for data end. */
+
+pam_args = s;
+pam_conv_had_error = FALSE;
+pam_arg_ended = FALSE;
+
+/* The first string in the list is the user. If this is an empty string, we
+fail. PAM doesn't support authentication with an empty user (it prompts for it,
+causing a potential mis-interpretation). */
+
+user = string_nextinlist(&pam_args, &sep, big_buffer, big_buffer_size);
+if (user == NULL || user[0] == 0) return FAIL;
+
+/* Start off PAM interaction */
+
+DEBUG(D_auth)
+  debug_printf("Running PAM authentication for user \"%s\"\n", user);
+
+pam_error = pam_start ("exim", CS user, &pamc, &pamh);
+
+/* Do the authentication - the pam_authenticate() will call pam_converse() to
+get the data it wants. After successful authentication we call pam_acct_mgmt()
+to apply any other restrictions (e.g. only some times of day). */
+
+if (pam_error == PAM_SUCCESS)
+  {
+  pam_error = pam_authenticate (pamh, PAM_SILENT);
+  if (pam_error == PAM_SUCCESS && !pam_conv_had_error)
+    pam_error = pam_acct_mgmt (pamh, PAM_SILENT);
+  }
+
+/* Finish the PAM interaction - this causes it to clean up store etc. Unclear
+what should be passed as the second argument. */
+
+pam_end(pamh, PAM_SUCCESS);
+
+/* Sort out the return code. If not success, set the error message. */
+
+if (pam_error == PAM_SUCCESS)
+  {
+  DEBUG(D_auth) debug_printf("PAM success\n");
+  return OK;
+  }
+
+*errptr = (uschar *)pam_strerror(pamh, pam_error);
+DEBUG(D_auth) debug_printf("PAM error: %s\n", *errptr);
+
+if (pam_error == PAM_USER_UNKNOWN ||
+    pam_error == PAM_AUTH_ERR ||
+    pam_error == PAM_ACCT_EXPIRED)
+  return FAIL;
+
+return ERROR;
+}
+
+#endif  /* SUPPORT_PAM */
+
+/* End of call_pam.c */
diff --git a/src/auths/call_pwcheck.c b/src/auths/call_pwcheck.c
new file mode 100644 (file)
index 0000000..a4567c1
--- /dev/null
@@ -0,0 +1,120 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* This module contains interface functions to the two Cyrus authentication
+daemons. The original one was "pwcheck", which gives its name to the source
+file. This is now deprecated in favour of "saslauthd". */
+
+
+#include "../exim.h"
+#include "pwcheck.h"
+
+
+/*************************************************
+*      External entry point for pwcheck          *
+*************************************************/
+
+/* This function calls the now-deprecated "pwcheck" Cyrus-SASL authentication
+daemon, passing over a colon-separated user name and password. As this is
+called from the string expander, the string will always be in dynamic store and
+can be overwritten.
+
+Arguments:
+  s        a colon-separated username:password string
+  errptr   where to point an error message
+
+Returns:   OK if authentication succeeded
+           FAIL if authentication failed
+           ERROR some other error condition
+*/
+
+int
+auth_call_pwcheck(uschar *s, uschar **errptr)
+{
+uschar *reply = NULL;
+uschar *pw = Ustrrchr(s, ':');
+
+if (pw == NULL)
+  {
+  *errptr = US"pwcheck: malformed input - missing colon";
+  return ERROR;
+  }
+
+*pw++ = 0;   /* Separate user and password */
+
+DEBUG(D_auth)
+  debug_printf("Running pwcheck authentication for user \"%s\"\n", s);
+
+switch (pwcheck_verify_password(CS s, CS pw, (const char **)(&reply)))
+  {
+  case PWCHECK_OK:
+  DEBUG(D_auth) debug_printf("pwcheck: success (%s)\n", reply);
+  return OK;
+
+  case PWCHECK_NO:
+  DEBUG(D_auth) debug_printf("pwcheck: access denied (%s)\n", reply);
+  return FAIL;
+
+  default:
+  DEBUG(D_auth) debug_printf("pwcheck: query failed (%s)\n", reply);
+  *errptr = reply;
+  return ERROR;
+  }
+}
+
+
+/*************************************************
+*       External entry point for pwauthd         *
+*************************************************/
+
+/* This function calls the "saslauthd" Cyrus-SASL authentication daemon,
+saslauthd, As this is called from the string expander, all the strings will
+always be in dynamic store and can be overwritten.
+
+Arguments:
+  username        username
+  password        password
+  service         optional service
+  realm           optional realm
+  errptr          where to point an error message
+
+Returns:   OK if authentication succeeded
+           FAIL if authentication failed
+           ERROR some other error condition
+*/
+
+int
+auth_call_saslauthd(uschar *username, uschar *password, uschar *service,
+  uschar *realm, uschar **errptr)
+{
+uschar *reply = NULL;
+
+if (service == NULL) service = US"";
+if (realm == NULL) realm = US"";
+
+DEBUG(D_auth)
+  debug_printf("Running saslauthd authentication for user \"%s\" \n", username);
+
+switch (saslauthd_verify_password(username, password, service,
+        realm, (const uschar **)(&reply)))
+  {
+  case PWCHECK_OK:
+  DEBUG(D_auth) debug_printf("saslauthd: success (%s)\n", reply);
+  return OK;
+
+  case PWCHECK_NO:
+  DEBUG(D_auth) debug_printf("saslauthd: access denied (%s)\n", reply);
+  return FAIL;
+
+  default:
+  DEBUG(D_auth) debug_printf("saslauthd: query failed (%s)\n", reply);
+  *errptr = reply;
+  return ERROR;
+  }
+}
+
+/* End of call_pwcheck.c */
diff --git a/src/auths/call_radius.c b/src/auths/call_radius.c
new file mode 100644 (file)
index 0000000..2064ed2
--- /dev/null
@@ -0,0 +1,221 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* This file was originally supplied by Ian Kirk. The libradius support came
+from Alex Kiernan. */
+
+#include "../exim.h"
+
+/* This module contains functions that call the Radius authentication
+mechanism.
+
+We can't just compile this code and allow the library mechanism to omit the
+functions if they are not wanted, because we need to have the Radius headers
+available for compiling. Therefore, compile these functions only if
+RADIUS_CONFIG_FILE is defined. However, some compilers don't like compiling
+empty modules, so keep them happy with a dummy when skipping the rest. Make it
+reference itself to stop picky compilers complaining that it is unused, and put
+in a dummy argument to stop even pickier compilers complaining about infinite
+loops. Then use a mutually-recursive pair as gcc is just getting stupid. */
+
+#ifndef RADIUS_CONFIG_FILE
+static void dummy(int x);
+static void dummy2(int x) { dummy(x-1); }
+static void dummy(int x) { dummy2(x-1); }
+#else  /* RADIUS_CONFIG_FILE */
+
+
+/* Two different Radius libraries are supported. The default is radiusclient,
+using its original API. At release 0.4.0 the API changed. */
+
+#ifdef RADIUS_LIB_RADLIB
+  #include <radlib.h>
+#else
+  #if !defined(RADIUS_LIB_RADIUSCLIENT) && !defined(RADIUS_LIB_RADIUSCLIENTNEW)
+  #define RADIUS_LIB_RADIUSCLIENT
+  #endif
+  #include <radiusclient.h>
+#endif
+
+
+
+/*************************************************
+*              Perform RADIUS authentication     *
+*************************************************/
+
+/* This function calls the Radius authentication mechanism, passing over one or
+more data strings.
+
+Arguments:
+  s        a colon-separated list of strings
+  errptr   where to point an error message
+
+Returns:   OK if authentication succeeded
+           FAIL if authentication failed
+           ERROR some other error condition
+*/
+
+int
+auth_call_radius(uschar *s, uschar **errptr)
+{
+uschar *user;
+uschar *radius_args = s;
+int result;
+int sep = 0;
+
+#ifdef RADIUS_LIB_RADLIB
+  struct rad_handle *h;
+#else
+  #ifdef RADIUS_LIB_RADIUSCLIENTNEW
+    rc_handle *h;
+  #endif
+  VALUE_PAIR *send = NULL;
+  VALUE_PAIR *received;
+  unsigned int service = PW_AUTHENTICATE_ONLY;
+  char msg[4096];
+#endif
+
+
+user = string_nextinlist(&radius_args, &sep, big_buffer, big_buffer_size);
+if (user == NULL) user = US"";
+
+DEBUG(D_auth) debug_printf("Running RADIUS authentication for user \"%s\" "
+               "and \"%s\"\n", user, radius_args);
+
+*errptr = NULL;
+
+
+/* Authenticate using the radiusclient library */
+
+#ifndef RADIUS_LIB_RADLIB
+
+rc_openlog("exim");
+
+#ifdef RADIUS_LIB_RADIUSCLIENT
+if (rc_read_config(RADIUS_CONFIG_FILE) != 0)
+  *errptr = string_sprintf("RADIUS: can't open %s", RADIUS_CONFIG_FILE);
+
+else if (rc_read_dictionary(rc_conf_str("dictionary")) != 0)
+  *errptr = string_sprintf("RADIUS: can't read dictionary");
+
+else if (rc_avpair_add(&send, PW_USER_NAME, user, 0) == NULL)
+  *errptr = string_sprintf("RADIUS: add user name failed\n");
+
+else if (rc_avpair_add(&send, PW_USER_PASSWORD, CS radius_args, 0) == NULL)
+  *errptr = string_sprintf("RADIUS: add password failed\n");
+
+else if (rc_avpair_add(&send, PW_SERVICE_TYPE, &service, 0) == NULL)
+  *errptr = string_sprintf("RADIUS: add service type failed\n");
+
+#else  /* RADIUS_LIB_RADIUSCLIENT unset => RADIUS_LIB_RADIUSCLIENT2 */
+
+if ((h = rc_read_config(RADIUS_CONFIG_FILE)) == NULL)
+  *errptr = string_sprintf("RADIUS: can't open %s", RADIUS_CONFIG_FILE);
+
+else if (rc_read_dictionary(h, rc_conf_str(h, "dictionary")) != 0)
+  *errptr = string_sprintf("RADIUS: can't read dictionary");
+
+else if (rc_avpair_add(h, &send, PW_USER_NAME, user, Ustrlen(user), 0) == NULL)
+  *errptr = string_sprintf("RADIUS: add user name failed\n");
+
+else if (rc_avpair_add(h, &send, PW_USER_PASSWORD, CS radius_args,
+    Ustrlen(radius_args), 0) == NULL)
+  *errptr = string_sprintf("RADIUS: add password failed\n");
+
+else if (rc_avpair_add(h, &send, PW_SERVICE_TYPE, &service, 0, 0) == NULL)
+  *errptr = string_sprintf("RADIUS: add service type failed\n");
+
+#endif  /* RADIUS_LIB_RADIUSCLIENT */
+
+if (*errptr != NULL)
+  {
+  DEBUG(D_auth) debug_printf("%s\n", *errptr);
+  return ERROR;
+  }
+
+#ifdef RADIUS_LIB_RADIUSCLIENT
+result = rc_auth(0, send, &received, msg);
+#else
+result = rc_auth(h, 0, send, &received, msg);
+#endif
+
+DEBUG(D_auth) debug_printf("RADIUS code returned %d\n", result);
+
+switch (result)
+  {
+  case OK_RC:
+  return OK;
+
+  case ERROR_RC:
+  return FAIL;
+
+  case TIMEOUT_RC:
+  *errptr = US"RADIUS: timed out";
+  return ERROR;
+
+  default:
+  case BADRESP_RC:
+  *errptr = string_sprintf("RADIUS: unexpected response (%d)", result);
+  return ERROR;
+  }
+
+#else  /* RADIUS_LIB_RADLIB is set */
+
+/* Authenticate using the libradius library */
+
+h = rad_auth_open();
+if (h == NULL)
+  {
+  *errptr = string_sprintf("RADIUS: can't initialise libradius");
+  return ERROR;
+  }
+if (rad_config(h, RADIUS_CONFIG_FILE) != 0 ||
+    rad_create_request(h, RAD_ACCESS_REQUEST) != 0 ||
+    rad_put_string(h, RAD_USER_NAME, CS user) != 0 ||
+    rad_put_string(h, RAD_USER_PASSWORD, CS radius_args) != 0 ||
+    rad_put_int(h, RAD_SERVICE_TYPE, RAD_AUTHENTICATE_ONLY) != 0 ||
+    rad_put_string(h, RAD_NAS_IDENTIFIER, CS primary_hostname) != 0)
+  {
+  *errptr = string_sprintf("RADIUS: %s", rad_strerror(h));
+  result = ERROR;
+  }
+else
+  {
+  result = rad_send_request(h);
+
+  switch(result)
+    {
+    case RAD_ACCESS_ACCEPT:
+    result = OK;
+    break;
+
+    case RAD_ACCESS_REJECT:
+    result = FAIL;
+    break;
+
+    case -1:
+    *errptr = string_sprintf("RADIUS: %s", rad_strerror(h));
+    result = ERROR;
+    break;
+
+    default:
+    *errptr = string_sprintf("RADIUS: unexpected response (%d)", result);
+    result= ERROR;
+    break;
+    }
+  }
+
+if (*errptr != NULL) DEBUG(D_auth) debug_printf("%s\n", *errptr);
+rad_close(h);
+return result;
+
+#endif  /* RADIUS_LIB_RADLIB */
+}
+
+#endif  /* RADIUS_CONFIG_FILE */
+
+/* End of call_radius.c */
diff --git a/src/auths/check_serv_cond.c b/src/auths/check_serv_cond.c
new file mode 100644 (file)
index 0000000..96c4b56
--- /dev/null
@@ -0,0 +1,130 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2012 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+#include "../exim.h"
+
+/* This module contains the function server_condition(), which is used
+by all authenticators. */
+
+
+/*************************************************
+*              Check server_condition            *
+*************************************************/
+
+/* This function is called from the server code of all authenticators. For
+plaintext and gsasl, it is always called: the argument cannot be empty, because
+for those, setting server_condition is what enables it as a server
+authenticator. For all the other authenticators, this function is called after
+they have authenticated, to enable additional authorization to be done.
+
+Argument:     the authenticator's instance block
+
+Returns:
+  OK          NULL argument, or success
+  DEFER       couldn't complete the check
+  FAIL        authentication failed
+*/
+
+int
+auth_check_serv_cond(auth_instance *ablock)
+{
+  return auth_check_some_cond(ablock,
+      US"server_condition", ablock->server_condition, OK);
+}
+
+
+/*************************************************
+*         Check some server condition            *
+*************************************************/
+
+/* This underlies server_condition, but is also used for some more generic
+ checks.
+
+Arguments:
+  ablock     the authenticator's instance block
+  label      debugging label naming the string checked
+  condition  the condition string to be expanded and checked
+  unset      value to return on NULL condition
+
+Returns:
+  OK          success (or unset=OK)
+  DEFER       couldn't complete the check
+  FAIL        authentication failed
+*/
+
+int
+auth_check_some_cond(auth_instance *ablock,
+    uschar *label, uschar *condition, int unset)
+{
+uschar *cond;
+
+HDEBUG(D_auth)
+  {
+  int i;
+  debug_printf("%s authenticator %s:\n", ablock->name, label);
+  for (i = 0; i < AUTH_VARS; i++)
+    {
+    if (auth_vars[i] != NULL)
+      debug_printf("  $auth%d = %s\n", i + 1, auth_vars[i]);
+    }
+  for (i = 1; i <= expand_nmax; i++)
+    debug_printf("  $%d = %.*s\n", i, expand_nlength[i], expand_nstring[i]);
+  debug_print_string(ablock->server_debug_string);    /* customized debug */
+  }
+
+/* For the plaintext authenticator, server_condition is never NULL. For the
+rest, an unset condition lets everything through. */
+
+/* For server_condition, an unset condition lets everything through.
+For plaintext/gsasl authenticators, it will have been pre-checked to prevent
+this.  We return the unset scenario value given to us, which for
+server_condition will be OK and otherwise will typically be FAIL. */
+
+if (condition == NULL) return unset;
+cond = expand_string(condition);
+
+HDEBUG(D_auth)
+  {
+  if (cond == NULL)
+    debug_printf("expansion failed: %s\n", expand_string_message);
+  else
+    debug_printf("expanded string: %s\n", cond);
+  }
+
+/* A forced expansion failure causes authentication to fail. Other expansion
+failures yield DEFER, which will cause a temporary error code to be returned to
+the AUTH command. The problem is at the server end, so the client should try
+again later. */
+
+if (cond == NULL)
+  {
+  if (expand_string_forcedfail) return FAIL;
+  auth_defer_msg = expand_string_message;
+  return DEFER;
+  }
+
+/* Return FAIL for empty string, "0", "no", and "false"; return OK for
+"1", "yes", and "true"; return DEFER for anything else, with the string
+available as an error text for the user. */
+
+if (*cond == 0 ||
+    Ustrcmp(cond, "0") == 0 ||
+    strcmpic(cond, US"no") == 0 ||
+    strcmpic(cond, US"false") == 0)
+  return FAIL;
+
+if (Ustrcmp(cond, "1") == 0 ||
+    strcmpic(cond, US"yes") == 0 ||
+    strcmpic(cond, US"true") == 0)
+  return OK;
+
+auth_defer_msg = cond;
+auth_defer_user_msg = string_sprintf(": %s", cond);
+return DEFER;
+}
+
+/* End of check_serv_cond.c */
diff --git a/src/auths/cram_md5.c b/src/auths/cram_md5.c
new file mode 100644 (file)
index 0000000..f744a89
--- /dev/null
@@ -0,0 +1,351 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2012 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+
+/* The stand-alone version just tests the algorithm. We have to drag
+in the MD5 computation functions, without their own stand-alone main
+program. */
+
+#ifdef STAND_ALONE
+#define CRAM_STAND_ALONE
+#include "md5.c"
+
+
+/* This is the normal, non-stand-alone case */
+
+#else
+#include "../exim.h"
+#include "cram_md5.h"
+
+/* Options specific to the cram_md5 authentication mechanism. */
+
+optionlist auth_cram_md5_options[] = {
+  { "client_name",        opt_stringptr,
+      (void *)(offsetof(auth_cram_md5_options_block, client_name)) },
+  { "client_secret",      opt_stringptr,
+      (void *)(offsetof(auth_cram_md5_options_block, client_secret)) },
+  { "server_secret",      opt_stringptr,
+      (void *)(offsetof(auth_cram_md5_options_block, server_secret)) }
+};
+
+/* Size of the options list. An extern variable has to be used so that its
+address can appear in the tables drtables.c. */
+
+int auth_cram_md5_options_count =
+  sizeof(auth_cram_md5_options)/sizeof(optionlist);
+
+/* Default private options block for the contidion authentication method. */
+
+auth_cram_md5_options_block auth_cram_md5_option_defaults = {
+  NULL,             /* server_secret */
+  NULL,             /* client_secret */
+  NULL              /* client_name */
+};
+
+
+/*************************************************
+*          Initialization entry point            *
+*************************************************/
+
+/* Called for each instance, after its options have been read, to
+enable consistency checks to be done, or anything else that needs
+to be set up. */
+
+void
+auth_cram_md5_init(auth_instance *ablock)
+{
+auth_cram_md5_options_block *ob =
+  (auth_cram_md5_options_block *)(ablock->options_block);
+if (ob->server_secret != NULL) ablock->server = TRUE;
+if (ob->client_secret != NULL)
+  {
+  ablock->client = TRUE;
+  if (ob->client_name == NULL) ob->client_name = primary_hostname;
+  }
+}
+
+#endif  /* STAND_ALONE */
+
+
+
+/*************************************************
+*      Peform the CRAM-MD5 algorithm             *
+*************************************************/
+
+/* The CRAM-MD5 algorithm is described in RFC 2195. It computes
+
+  MD5((secret XOR opad), MD5((secret XOR ipad), challenge))
+
+where secret is padded out to 64 characters (after being reduced to an MD5
+digest if longer than 64) and ipad and opad are 64-byte strings of 0x36 and
+0x5c respectively, and comma means concatenation.
+
+Arguments:
+  secret         the shared secret
+  challenge      the challenge text
+  digest         16-byte slot to put the answer in
+
+Returns:         nothing
+*/
+
+static void
+compute_cram_md5(uschar *secret, uschar *challenge, uschar *digestptr)
+{
+md5 base;
+int i;
+int len = Ustrlen(secret);
+uschar isecret[64];
+uschar osecret[64];
+uschar md5secret[16];
+
+/* If the secret is longer than 64 characters, we compute its MD5 digest
+and use that. */
+
+if (len > 64)
+  {
+  md5_start(&base);
+  md5_end(&base, (uschar *)secret, len, md5secret);
+  secret = (uschar *)md5secret;
+  len = 16;
+  }
+
+/* The key length is now known to be <= 64. Set up the padded and xor'ed
+versions. */
+
+memcpy(isecret, secret, len);
+memset(isecret+len, 0, 64-len);
+memcpy(osecret, isecret, 64);
+
+for (i = 0; i < 64; i++)
+  {
+  isecret[i] ^= 0x36;
+  osecret[i] ^= 0x5c;
+  }
+
+/* Compute the inner MD5 digest */
+
+md5_start(&base);
+md5_mid(&base, isecret);
+md5_end(&base, (uschar *)challenge, Ustrlen(challenge), md5secret);
+
+/* Compute the outer MD5 digest */
+
+md5_start(&base);
+md5_mid(&base, osecret);
+md5_end(&base, md5secret, 16, digestptr);
+}
+
+
+#ifndef STAND_ALONE
+
+/*************************************************
+*             Server entry point                 *
+*************************************************/
+
+/* For interface, see auths/README */
+
+int
+auth_cram_md5_server(auth_instance *ablock, uschar *data)
+{
+auth_cram_md5_options_block *ob =
+  (auth_cram_md5_options_block *)(ablock->options_block);
+uschar *challenge = string_sprintf("<%d.%ld@%s>", getpid(),
+    (long int) time(NULL), primary_hostname);
+uschar *clear, *secret;
+uschar digest[16];
+int i, rc, len;
+
+/* If we are running in the test harness, always send the same challenge,
+an example string taken from the RFC. */
+
+if (running_in_test_harness)
+  challenge = US"<1896.697170952@postoffice.reston.mci.net>";
+
+/* No data should have been sent with the AUTH command */
+
+if (*data != 0) return UNEXPECTED;
+
+/* Send the challenge, read the return */
+
+if ((rc = auth_get_data(&data, challenge, Ustrlen(challenge))) != OK) return rc;
+if ((len = auth_b64decode(data, &clear)) < 0) return BAD64;
+
+/* The return consists of a user name, space-separated from the CRAM-MD5
+digest, expressed in hex. Extract the user name and put it in $auth1 and $1.
+The former is now the preferred variable; the latter is the original one. Then
+check that the remaining length is 32. */
+
+auth_vars[0] = expand_nstring[1] = clear;
+while (*clear != 0 && !isspace(*clear)) clear++;
+if (!isspace(*clear)) return FAIL;
+*clear++ = 0;
+
+expand_nlength[1] = clear - expand_nstring[1] - 1;
+if (len - expand_nlength[1] - 1 != 32) return FAIL;
+expand_nmax = 1;
+
+/* Expand the server_secret string so that it can compute a value dependent on
+the user name if necessary. */
+
+debug_print_string(ablock->server_debug_string);    /* customized debugging */
+secret = expand_string(ob->server_secret);
+
+/* A forced fail implies failure of authentication - i.e. we have no secret for
+the given name. */
+
+if (secret == NULL)
+  {
+  if (expand_string_forcedfail) return FAIL;
+  auth_defer_msg = expand_string_message;
+  return DEFER;
+  }
+
+/* Compute the CRAM-MD5 digest that we should have received from the client. */
+
+compute_cram_md5(secret, challenge, digest);
+
+HDEBUG(D_auth)
+  {
+  uschar buff[64];
+  debug_printf("CRAM-MD5: user name = %s\n", auth_vars[0]);
+  debug_printf("          challenge = %s\n", challenge);
+  debug_printf("          received  = %s\n", clear);
+  Ustrcpy(buff,"          digest    = ");
+  for (i = 0; i < 16; i++) sprintf(CS buff+22+2*i, "%02x", digest[i]);
+  debug_printf("%.54s\n", buff);
+  }
+
+/* We now have to compare the digest, which is 16 bytes in binary, with the
+data received, which is expressed in lower case hex. We checked above that
+there were 32 characters of data left. */
+
+for (i = 0; i < 16; i++)
+  {
+  int a = *clear++;
+  int b = *clear++;
+  if (((((a >= 'a')? a - 'a' + 10 : a - '0') << 4) +
+        ((b >= 'a')? b - 'a' + 10 : b - '0')) != digest[i]) return FAIL;
+  }
+
+/* Expand server_condition as an authorization check */
+return auth_check_serv_cond(ablock);
+}
+
+
+
+/*************************************************
+*              Client entry point                *
+*************************************************/
+
+/* For interface, see auths/README */
+
+int
+auth_cram_md5_client(
+  auth_instance *ablock,                 /* authenticator block */
+  smtp_inblock *inblock,                 /* input connection */
+  smtp_outblock *outblock,               /* output connection */
+  int timeout,                           /* command timeout */
+  uschar *buffer,                        /* for reading response */
+  int buffsize)                          /* size of buffer */
+{
+auth_cram_md5_options_block *ob =
+  (auth_cram_md5_options_block *)(ablock->options_block);
+uschar *secret = expand_string(ob->client_secret);
+uschar *name = expand_string(ob->client_name);
+uschar *challenge, *p;
+int i;
+uschar digest[16];
+
+/* If expansion of either the secret or the user name failed, return CANCELLED
+or ERROR, as approriate. */
+
+if (secret == NULL || name == NULL)
+  {
+  if (expand_string_forcedfail)
+    {
+    *buffer = 0;           /* No message */
+    return CANCELLED;
+    }
+  string_format(buffer, buffsize, "expansion of \"%s\" failed in "
+    "%s authenticator: %s",
+    (secret == NULL)? ob->client_secret : ob->client_name,
+    ablock->name, expand_string_message);
+  return ERROR;
+  }
+
+/* Initiate the authentication exchange and read the challenge, which arrives
+in base 64. */
+
+if (smtp_write_command(outblock, FALSE, "AUTH %s\r\n", ablock->public_name) < 0)
+  return FAIL_SEND;
+if (smtp_read_response(inblock, (uschar *)buffer, buffsize, '3', timeout) < 0)
+  return FAIL;
+
+if (auth_b64decode(buffer + 4, &challenge) < 0)
+  {
+  string_format(buffer, buffsize, "bad base 64 string in challenge: %s",
+    big_buffer + 4);
+  return ERROR;
+  }
+
+/* Run the CRAM-MD5 algorithm on the secret and the challenge */
+
+compute_cram_md5(secret, challenge, digest);
+
+/* Create the response from the user name plus the CRAM-MD5 digest */
+
+string_format(big_buffer, big_buffer_size - 36, "%s", name);
+p = big_buffer;
+while (*p != 0) p++;
+*p++ = ' ';
+
+for (i = 0; i < 16; i++)
+  {
+  sprintf(CS p, "%02x", digest[i]);
+  p += 2;
+  }
+
+/* Send the response, in base 64, and check the result. The response is
+in big_buffer, but auth_b64encode() returns its result in working store,
+so calling smtp_write_command(), which uses big_buffer, is OK. */
+
+buffer[0] = 0;
+if (smtp_write_command(outblock, FALSE, "%s\r\n", auth_b64encode(big_buffer,
+  p - big_buffer)) < 0) return FAIL_SEND;
+
+return smtp_read_response(inblock, (uschar *)buffer, buffsize, '2', timeout)?
+  OK : FAIL;
+}
+#endif  /* STAND_ALONE */
+
+
+/*************************************************
+**************************************************
+*             Stand-alone test program           *
+**************************************************
+*************************************************/
+
+#ifdef STAND_ALONE
+
+int main(int argc, char **argv)
+{
+int i;
+uschar *secret = US argv[1];
+uschar *challenge = US argv[2];
+uschar digest[16];
+
+compute_cram_md5(secret, challenge, digest);
+
+for (i = 0; i < 16; i++) printf("%02x", digest[i]);
+printf("\n");
+
+return 0;
+}
+
+#endif
+
+/* End of cram_md5.c */
diff --git a/src/auths/cram_md5.h b/src/auths/cram_md5.h
new file mode 100644 (file)
index 0000000..e1363bf
--- /dev/null
@@ -0,0 +1,32 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Private structure for the private options. */
+
+typedef struct {
+  uschar *server_secret;
+  uschar *client_secret;
+  uschar *client_name;
+} auth_cram_md5_options_block;
+
+/* Data for reading the private options. */
+
+extern optionlist auth_cram_md5_options[];
+extern int auth_cram_md5_options_count;
+
+/* Block containing default values. */
+
+extern auth_cram_md5_options_block auth_cram_md5_option_defaults;
+
+/* The entry points for the mechanism */
+
+extern void auth_cram_md5_init(auth_instance *);
+extern int auth_cram_md5_server(auth_instance *, uschar *);
+extern int auth_cram_md5_client(auth_instance *, smtp_inblock *,
+                                smtp_outblock *, int, uschar *, int);
+
+/* End of cram_md5.h */
diff --git a/src/auths/cyrus_sasl.c b/src/auths/cyrus_sasl.c
new file mode 100644 (file)
index 0000000..c7fb593
--- /dev/null
@@ -0,0 +1,530 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2012 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* This code was originally contributed by Matthew Byng-Maddick */
+
+/* Copyright (c) A L Digital 2004 */
+
+/* A generic (mechanism independent) Cyrus SASL authenticator. */
+
+
+#include "../exim.h"
+
+
+/* We can't just compile this code and allow the library mechanism to omit the
+functions if they are not wanted, because we need to have the Cyrus SASL header
+available for compiling. Therefore, compile these functions only if
+AUTH_CYRUS_SASL is defined. However, some compilers don't like compiling empty
+modules, so keep them happy with a dummy when skipping the rest. Make it
+reference itself to stop picky compilers complaining that it is unused, and put
+in a dummy argument to stop even pickier compilers complaining about infinite
+loops. */
+
+#ifndef AUTH_CYRUS_SASL
+static void dummy(int x);
+static void dummy2(int x) { dummy(x-1); }
+static void dummy(int x) { dummy2(x-1); }
+#else
+
+
+#include <sasl/sasl.h>
+#include "cyrus_sasl.h"
+
+/* Options specific to the cyrus_sasl authentication mechanism. */
+
+optionlist auth_cyrus_sasl_options[] = {
+  { "server_hostname",      opt_stringptr,
+      (void *)(offsetof(auth_cyrus_sasl_options_block, server_hostname)) },
+  { "server_mech",          opt_stringptr,
+      (void *)(offsetof(auth_cyrus_sasl_options_block, server_mech)) },
+  { "server_realm",         opt_stringptr,
+      (void *)(offsetof(auth_cyrus_sasl_options_block, server_realm)) },
+  { "server_service",       opt_stringptr,
+      (void *)(offsetof(auth_cyrus_sasl_options_block, server_service)) }
+};
+
+/* Size of the options list. An extern variable has to be used so that its
+address can appear in the tables drtables.c. */
+
+int auth_cyrus_sasl_options_count =
+  sizeof(auth_cyrus_sasl_options)/sizeof(optionlist);
+
+/* Default private options block for the cyrus_sasl authentication method. */
+
+auth_cyrus_sasl_options_block auth_cyrus_sasl_option_defaults = {
+  US"smtp",         /* server_service */
+  US"$primary_hostname", /* server_hostname */
+  NULL,             /* server_realm */
+  NULL              /* server_mech */
+};
+
+
+/*************************************************
+*          Initialization entry point            *
+*************************************************/
+
+/* Called for each instance, after its options have been read, to
+enable consistency checks to be done, or anything else that needs
+to be set up. */
+
+
+/* Auxiliary function, passed in data to sasl_server_init(). */
+
+static int
+mysasl_config(void *context,
+              const char *plugin_name,
+              const char *option,
+              const char **result,
+              unsigned int *len)
+{
+if (context && !strcmp(option, "mech_list"))
+  {
+  *result = context;
+  if (len != NULL) *len = strlen(*result);
+  return SASL_OK;
+  }
+return SASL_FAIL;
+}
+
+/* Here's the real function */
+
+void
+auth_cyrus_sasl_init(auth_instance *ablock)
+{
+auth_cyrus_sasl_options_block *ob =
+  (auth_cyrus_sasl_options_block *)(ablock->options_block);
+uschar *list, *listptr, *buffer;
+int rc, i;
+unsigned int len;
+uschar *rs_point, *expanded_hostname;
+char *realm_expanded;
+
+sasl_conn_t *conn;
+sasl_callback_t cbs[]={
+  {SASL_CB_GETOPT, NULL, NULL },
+  {SASL_CB_LIST_END, NULL, NULL}};
+
+/* default the mechanism to our "public name" */
+if(ob->server_mech == NULL)
+  ob->server_mech=string_copy(ablock->public_name);
+
+expanded_hostname = expand_string(ob->server_hostname);
+if (expanded_hostname == NULL)
+  log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s authenticator:  "
+      "couldn't expand server_hostname [%s]: %s",
+      ablock->name, ob->server_hostname, expand_string_message);
+
+realm_expanded=NULL;
+if (ob->server_realm != NULL) {
+  realm_expanded = CS expand_string(ob->server_realm);
+  if (realm_expanded == NULL)
+    log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s authenticator:  "
+        "couldn't expand server_realm [%s]: %s",
+        ablock->name, ob->server_realm, expand_string_message);
+}
+
+/* we're going to initialise the library to check that there is an
+ * authenticator of type whatever mechanism we're using
+ */
+
+cbs[0].proc = (int(*)(void)) &mysasl_config;
+cbs[0].context = ob->server_mech;
+
+rc=sasl_server_init(cbs, "exim");
+
+if( rc != SASL_OK )
+  log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s authenticator:  "
+      "couldn't initialise Cyrus SASL library.", ablock->name);
+
+rc=sasl_server_new(CS ob->server_service, CS expanded_hostname,
+                   realm_expanded, NULL, NULL, NULL, 0, &conn);
+if( rc != SASL_OK )
+  log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s authenticator:  "
+      "couldn't initialise Cyrus SASL server connection.", ablock->name);
+
+rc=sasl_listmech(conn, NULL, "", ":", "", (const char **)(&list), &len, &i);
+if( rc != SASL_OK )
+  log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s authenticator:  "
+      "couldn't get Cyrus SASL mechanism list.", ablock->name);
+
+i=':';
+listptr=list;
+
+HDEBUG(D_auth) {
+  debug_printf("Initialised Cyrus SASL service=\"%s\" fqdn=\"%s\" realm=\"%s\"\n",
+      ob->server_service, expanded_hostname, realm_expanded);
+  debug_printf("Cyrus SASL knows mechanisms: %s\n", list);
+}
+
+/* the store_get / store_reset mechanism is hierarchical
+ * the hierarchy is stored for us behind our back. This point
+ * creates a hierarchy point for this function.
+ */
+rs_point=store_get(0);
+
+/* loop until either we get to the end of the list, or we match the
+ * public name of this authenticator
+ */
+while( ( buffer = string_nextinlist(&listptr, &i, NULL, 0) ) &&
+       strcmpic(buffer,ob->server_mech) );
+
+if(!buffer)
+  log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s authenticator:  "
+      "Cyrus SASL doesn't know about mechanism %s.", ablock->name, ob->server_mech);
+
+store_reset(rs_point);
+
+HDEBUG(D_auth) debug_printf("Cyrus SASL driver %s: %s initialised\n", ablock->name, ablock->public_name);
+
+/* make sure that if we get here then we're allowed to advertise. */
+ablock->server = TRUE;
+
+sasl_dispose(&conn);
+sasl_done();
+}
+
+/*************************************************
+*             Server entry point                 *
+*************************************************/
+
+/* For interface, see auths/README */
+
+/* note, we don't care too much about memory allocation in this, because this is entirely
+ * within a shortlived child
+ */
+
+int
+auth_cyrus_sasl_server(auth_instance *ablock, uschar *data)
+{
+auth_cyrus_sasl_options_block *ob =
+  (auth_cyrus_sasl_options_block *)(ablock->options_block);
+uschar *output, *out2, *input, *clear, *hname;
+uschar *debug = NULL;   /* Stops compiler complaining */
+sasl_callback_t cbs[]={{SASL_CB_LIST_END, NULL, NULL}};
+sasl_conn_t *conn;
+char *realm_expanded;
+int rc, i, firsttime=1, clen, *negotiated_ssf_ptr=NULL, negotiated_ssf;
+unsigned int inlen, outlen;
+
+input=data;
+inlen=Ustrlen(data);
+
+HDEBUG(D_auth) debug=string_copy(data);
+
+hname=expand_string(ob->server_hostname);
+realm_expanded=NULL;
+if (hname && ob->server_realm)
+  realm_expanded= CS expand_string(ob->server_realm);
+if((hname == NULL) ||
+   ((realm_expanded == NULL) && (ob->server_realm != NULL)))
+  {
+  auth_defer_msg = expand_string_message;
+  return DEFER;
+  }
+
+if(inlen)
+  {
+  clen=auth_b64decode(input, &clear);
+  if(clen < 0)
+    {
+    return BAD64;
+    }
+  input=clear;
+  inlen=clen;
+  }
+
+rc=sasl_server_init(cbs, "exim");
+if (rc != SASL_OK)
+  {
+  auth_defer_msg = US"couldn't initialise Cyrus SASL library";
+  return DEFER;
+  }
+
+rc=sasl_server_new(CS ob->server_service, CS hname, realm_expanded, NULL,
+  NULL, NULL, 0, &conn);
+
+HDEBUG(D_auth)
+  debug_printf("Initialised Cyrus SASL server connection; service=\"%s\" fqdn=\"%s\" realm=\"%s\"\n",
+      ob->server_service, hname, realm_expanded);
+
+if( rc != SASL_OK )
+  {
+  auth_defer_msg = US"couldn't initialise Cyrus SASL connection";
+  sasl_done();
+  return DEFER;
+  }
+
+if (tls_in.cipher)
+  {
+  rc = sasl_setprop(conn, SASL_SSF_EXTERNAL, (sasl_ssf_t *) &tls_in.bits);
+  if (rc != SASL_OK)
+    {
+    HDEBUG(D_auth) debug_printf("Cyrus SASL EXTERNAL SSF set %d failed: %s\n",
+        tls_in.bits, sasl_errstring(rc, NULL, NULL));
+    auth_defer_msg = US"couldn't set Cyrus SASL EXTERNAL SSF";
+    sasl_done();
+    return DEFER;
+    }
+  else
+    HDEBUG(D_auth) debug_printf("Cyrus SASL set EXTERNAL SSF to %d\n", tls_in.bits);
+  }
+else
+  HDEBUG(D_auth) debug_printf("Cyrus SASL: no TLS, no EXTERNAL SSF set\n");
+
+/* So sasl_setprop() documents non-shorted IPv6 addresses which is incredibly
+annoying; looking at cyrus-imapd-2.3.x source, the IP address is constructed
+with their iptostring() function, which just wraps
+getnameinfo(..., NI_NUMERICHOST|NI_NUMERICSERV), which is equivalent to the
+inet_ntop which we wrap in our host_ntoa() function.
+
+So the docs are too strict and we shouldn't worry about :: contractions. */
+
+/* Set properties for remote and local host-ip;port */
+for (i=0; i < 2; ++i)
+  {
+  struct sockaddr_storage ss;
+  int (*query)(int, struct sockaddr *, socklen_t *);
+  int propnum, port;
+  const uschar *label;
+  uschar *address, *address_port;
+  const char *s_err;
+  socklen_t sslen;
+
+  if (i)
+    {
+    query = &getpeername;
+    propnum = SASL_IPREMOTEPORT;
+    label = CUS"peer";
+    }
+  else
+    {
+    query = &getsockname;
+    propnum = SASL_IPLOCALPORT;
+    label = CUS"local";
+    }
+
+  sslen = sizeof(ss);
+  rc = query(fileno(smtp_in), (struct sockaddr *) &ss, &sslen);
+  if (rc < 0)
+    {
+    HDEBUG(D_auth)
+      debug_printf("Failed to get %s address information: %s\n",
+          label, strerror(errno));
+    break;
+    }
+
+  address = host_ntoa(-1, &ss, NULL, &port);
+  address_port = string_sprintf("%s;%d", address, port);
+
+  rc = sasl_setprop(conn, propnum, address_port);
+  if (rc != SASL_OK)
+    {
+    s_err = sasl_errdetail(conn);
+    HDEBUG(D_auth)
+      debug_printf("Failed to set %s SASL property: [%d] %s\n",
+          label, rc, s_err ? s_err : "<unknown reason>");
+    break;
+    }
+  HDEBUG(D_auth) debug_printf("Cyrus SASL set %s hostport to: %s\n",
+      label, address_port);
+  }
+
+rc=SASL_CONTINUE;
+
+while(rc==SASL_CONTINUE)
+  {
+  if(firsttime)
+    {
+    firsttime=0;
+    HDEBUG(D_auth) debug_printf("Calling sasl_server_start(%s,\"%s\")\n", ob->server_mech, debug);
+    rc=sasl_server_start(conn, CS ob->server_mech, inlen?CS input:NULL, inlen,
+           (const char **)(&output), &outlen);
+    }
+  else
+    {
+    /* make sure that we have a null-terminated string */
+    out2=store_get(outlen+1);
+    memcpy(out2,output,outlen);
+    out2[outlen]='\0';
+    if((rc=auth_get_data(&input, out2, outlen))!=OK)
+      {
+      /* we couldn't get the data, so free up the library before
+       * returning whatever error we get */
+      sasl_dispose(&conn);
+      sasl_done();
+      return rc;
+      }
+    inlen=Ustrlen(input);
+
+    HDEBUG(D_auth) debug=string_copy(input);
+    if(inlen)
+      {
+      clen=auth_b64decode(input, &clear);
+      if(clen < 0)
+       {
+        sasl_dispose(&conn);
+        sasl_done();
+       return BAD64;
+       }
+      input=clear;
+      inlen=clen;
+      }
+
+    HDEBUG(D_auth) debug_printf("Calling sasl_server_step(\"%s\")\n", debug);
+    rc=sasl_server_step(conn, CS input, inlen, (const char **)(&output), &outlen);
+    }
+  if(rc==SASL_BADPROT)
+    {
+    sasl_dispose(&conn);
+    sasl_done();
+    return UNEXPECTED;
+    }
+  else if( rc==SASL_FAIL     || rc==SASL_BUFOVER
+       || rc==SASL_BADMAC   || rc==SASL_BADAUTH
+       || rc==SASL_NOAUTHZ  || rc==SASL_ENCRYPT
+       || rc==SASL_EXPIRED  || rc==SASL_DISABLED
+       || rc==SASL_NOUSER   )
+    {
+    /* these are considered permanent failure codes */
+    HDEBUG(D_auth)
+      debug_printf("Cyrus SASL permanent failure %d (%s)\n", rc, sasl_errstring(rc, NULL, NULL));
+    log_write(0, LOG_REJECT, "%s authenticator (%s):\n  "
+       "Cyrus SASL permanent failure: %s", ablock->name, ob->server_mech,
+       sasl_errstring(rc, NULL, NULL));
+    sasl_dispose(&conn);
+    sasl_done();
+    return FAIL;
+    }
+  else if(rc==SASL_NOMECH)
+    {
+    /* this is a temporary failure, because the mechanism is not
+     * available for this user. If it wasn't available at all, we
+     * shouldn't have got here in the first place...
+     */
+    HDEBUG(D_auth)
+      debug_printf("Cyrus SASL temporary failure %d (%s)\n", rc, sasl_errstring(rc, NULL, NULL));
+    auth_defer_msg =
+        string_sprintf("Cyrus SASL: mechanism %s not available", ob->server_mech);
+    sasl_dispose(&conn);
+    sasl_done();
+    return DEFER;
+    }
+  else if(!(rc==SASL_OK || rc==SASL_CONTINUE))
+    {
+    /* Anything else is a temporary failure, and we'll let SASL print out
+     * the error string for us
+     */
+    HDEBUG(D_auth)
+      debug_printf("Cyrus SASL temporary failure %d (%s)\n", rc, sasl_errstring(rc, NULL, NULL));
+    auth_defer_msg =
+        string_sprintf("Cyrus SASL: %s", sasl_errstring(rc, NULL, NULL));
+    sasl_dispose(&conn);
+    sasl_done();
+    return DEFER;
+    }
+  else if(rc==SASL_OK)
+    {
+    /* Get the username and copy it into $auth1 and $1. The former is now the
+    preferred variable; the latter is the original variable. */
+    rc = sasl_getprop(conn, SASL_USERNAME, (const void **)(&out2));
+    if (rc != SASL_OK)
+      {
+      HDEBUG(D_auth)
+        debug_printf("Cyrus SASL library will not tell us the username: %s\n",
+            sasl_errstring(rc, NULL, NULL));
+      log_write(0, LOG_REJECT, "%s authenticator (%s):\n  "
+         "Cyrus SASL username fetch problem: %s", ablock->name, ob->server_mech,
+         sasl_errstring(rc, NULL, NULL));
+      sasl_dispose(&conn);
+      sasl_done();
+      return FAIL;
+      }
+
+    auth_vars[0] = expand_nstring[1] = string_copy(out2);
+    expand_nlength[1] = Ustrlen(expand_nstring[1]);
+    expand_nmax = 1;
+
+    HDEBUG(D_auth)
+      debug_printf("Cyrus SASL %s authentication succeeded for %s\n",
+          ob->server_mech, auth_vars[0]);
+
+    rc = sasl_getprop(conn, SASL_SSF, (const void **)(&negotiated_ssf_ptr));
+    if (rc != SASL_OK)
+      {
+      HDEBUG(D_auth)
+        debug_printf("Cyrus SASL library will not tell us the SSF: %s\n",
+            sasl_errstring(rc, NULL, NULL));
+      log_write(0, LOG_REJECT, "%s authenticator (%s):\n  "
+          "Cyrus SASL SSF value not available: %s", ablock->name, ob->server_mech,
+          sasl_errstring(rc, NULL, NULL));
+      sasl_dispose(&conn);
+      sasl_done();
+      return FAIL;
+      }
+    negotiated_ssf = *negotiated_ssf_ptr;
+    HDEBUG(D_auth)
+      debug_printf("Cyrus SASL %s negotiated SSF: %d\n", ob->server_mech, negotiated_ssf);
+    if (negotiated_ssf > 0)
+      {
+      HDEBUG(D_auth)
+        debug_printf("Exim does not implement SASL wrapping (needed for SSF %d).\n", negotiated_ssf);
+      log_write(0, LOG_REJECT, "%s authenticator (%s):\n  "
+          "Cyrus SASL SSF %d not supported by Exim", ablock->name, ob->server_mech, negotiated_ssf);
+      sasl_dispose(&conn);
+      sasl_done();
+      return FAIL;
+      }
+
+    /* close down the connection, freeing up library's memory */
+    sasl_dispose(&conn);
+    sasl_done();
+
+    /* Expand server_condition as an authorization check */
+    return auth_check_serv_cond(ablock);
+    }
+  }
+/* NOTREACHED */
+return 0;  /* Stop compiler complaints */
+}
+
+/*************************************************
+*                Diagnostic API                  *
+*************************************************/
+
+void
+auth_cyrus_sasl_version_report(FILE *f)
+{
+  const char *implementation, *version;
+  sasl_version_info(&implementation, &version, NULL, NULL, NULL, NULL);
+  fprintf(f, "Library version: Cyrus SASL: Compile: %d.%d.%d\n"
+             "                             Runtime: %s [%s]\n",
+          SASL_VERSION_MAJOR, SASL_VERSION_MINOR, SASL_VERSION_STEP,
+          version, implementation);
+}
+
+/*************************************************
+*              Client entry point                *
+*************************************************/
+
+/* For interface, see auths/README */
+
+int
+auth_cyrus_sasl_client(
+  auth_instance *ablock,                 /* authenticator block */
+  smtp_inblock *inblock,                 /* input connection */
+  smtp_outblock *outblock,               /* output connection */
+  int timeout,                           /* command timeout */
+  uschar *buffer,                          /* for reading response */
+  int buffsize)                          /* size of buffer */
+{
+/* We don't support clients (yet) in this implementation of cyrus_sasl */
+return FAIL;
+}
+
+#endif  /* AUTH_CYRUS_SASL */
+
+/* End of cyrus_sasl.c */
diff --git a/src/auths/cyrus_sasl.h b/src/auths/cyrus_sasl.h
new file mode 100644 (file)
index 0000000..8481054
--- /dev/null
@@ -0,0 +1,36 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2012 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Copyright (c) A L Digital Ltd 2004 */
+
+/* Private structure for the private options. */
+
+typedef struct {
+  uschar *server_service;
+  uschar *server_hostname;
+  uschar *server_realm;
+  uschar *server_mech;
+} auth_cyrus_sasl_options_block;
+
+/* Data for reading the private options. */
+
+extern optionlist auth_cyrus_sasl_options[];
+extern int auth_cyrus_sasl_options_count;
+
+/* Block containing default values. */
+
+extern auth_cyrus_sasl_options_block auth_cyrus_sasl_option_defaults;
+
+/* The entry points for the mechanism */
+
+extern void auth_cyrus_sasl_init(auth_instance *);
+extern int auth_cyrus_sasl_server(auth_instance *, uschar *);
+extern int auth_cyrus_sasl_client(auth_instance *, smtp_inblock *,
+                                smtp_outblock *, int, uschar *, int);
+extern void auth_cyrus_sasl_version_report(FILE *f);
+
+/* End of cyrus_sasl.h */
diff --git a/src/auths/dovecot.c b/src/auths/dovecot.c
new file mode 100644 (file)
index 0000000..1874f32
--- /dev/null
@@ -0,0 +1,459 @@
+/*
+ * Copyright (c) 2004 Andrey Panin <pazke@donpac.ru>
+ * Copyright (c) 2006-2014 The Exim Maintainers
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published
+ * by the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ */
+
+/* A number of modifications have been made to the original code. Originally I
+commented them specially, but now they are getting quite extensive, so I have
+ceased doing that. The biggest change is to use unbuffered I/O on the socket
+because using C buffered I/O gives problems on some operating systems. PH */
+
+/* Protocol specifications:
+ * Dovecot 1, protocol version 1.1
+ *   http://wiki.dovecot.org/Authentication%20Protocol
+ *
+ * Dovecot 2, protocol version 1.1
+ *   http://wiki2.dovecot.org/Design/AuthProtocol
+ */
+
+#include "../exim.h"
+#include "dovecot.h"
+
+#define VERSION_MAJOR  1
+#define VERSION_MINOR  0
+
+/* http://wiki.dovecot.org/Authentication%20Protocol
+"The maximum line length isn't defined,
+ but it's currently expected to fit into 8192 bytes"
+*/
+#define DOVECOT_AUTH_MAXLINELEN 8192
+
+/* This was hard-coded as 8.
+AUTH req C->S sends {"AUTH", id, mechanism, service } + params, 5 defined for
+Dovecot 1; Dovecot 2 (same protocol version) defines 9.
+
+Master->Server sends {"USER", id, userid} + params, 6 defined.
+Server->Client only gives {"OK", id} + params, unspecified, only 1 guaranteed.
+
+We only define here to accept S->C; max seen is 3+<unspecified>, plus the two
+for the command and id, where unspecified might include _at least_ user=...
+
+So: allow for more fields than we ever expect to see, while aware that count
+can go up without changing protocol version.
+The cost is the length of an array of pointers on the stack.
+*/
+#define DOVECOT_AUTH_MAXFIELDCOUNT 16
+
+/* Options specific to the authentication mechanism. */
+optionlist auth_dovecot_options[] = {
+       {
+       "server_socket",
+       opt_stringptr,
+       (void *)(offsetof(auth_dovecot_options_block, server_socket))
+       },
+};
+
+/* Size of the options list. An extern variable has to be used so that its
+address can appear in the tables drtables.c. */
+
+int auth_dovecot_options_count =
+       sizeof(auth_dovecot_options) / sizeof(optionlist);
+
+/* Default private options block for the authentication method. */
+
+auth_dovecot_options_block auth_dovecot_option_defaults = {
+       NULL,                           /* server_socket */
+};
+
+
+/* Static variables for reading from the socket */
+
+static uschar sbuffer[256];
+static int socket_buffer_left;
+
+
+
+/*************************************************
+ *          Initialization entry point           *
+ *************************************************/
+
+/* Called for each instance, after its options have been read, to
+enable consistency checks to be done, or anything else that needs
+to be set up. */
+
+void auth_dovecot_init(auth_instance *ablock)
+{
+       auth_dovecot_options_block *ob =
+               (auth_dovecot_options_block *)(ablock->options_block);
+
+       if (ablock->public_name == NULL)
+               ablock->public_name = ablock->name;
+       if (ob->server_socket != NULL)
+               ablock->server = TRUE;
+       ablock->client = FALSE;
+}
+
+/*************************************************
+ *    "strcut" to split apart server lines       *
+ *************************************************/
+
+/* Dovecot auth protocol uses TAB \t as delimiter; a line consists
+of a command-name, TAB, and then any parameters, each separated by a TAB.
+A parameter can be param=value or a bool, just param.
+
+This function modifies the original str in-place, inserting NUL characters.
+It initialises ptrs entries, setting all to NULL and only setting
+non-NULL N entries, where N is the return value, the number of fields seen
+(one more than the number of tabs).
+
+Note that the return value will always be at least 1, is the count of
+actual fields (so last valid offset into ptrs is one less).
+*/
+
+static int
+strcut(uschar *str, uschar **ptrs, int nptrs)
+{
+       uschar *last_sub_start = str;
+       int n;
+
+       for (n = 0; n < nptrs; n++)
+               ptrs[n] = NULL;
+       n = 1;
+
+       while (*str) {
+               if (*str == '\t') {
+                       if (n <= nptrs) {
+                               *ptrs++ = last_sub_start;
+                               last_sub_start = str + 1;
+                               *str = '\0';
+                       }
+                       n++;
+               }
+               str++;
+       }
+
+       /* It's acceptable for the string to end with a tab character.  We see
+       this in AUTH PLAIN without an initial response from the client, which
+       causing us to send "334 " and get the data from the client. */
+       if (n <= nptrs) {
+               *ptrs = last_sub_start;
+       } else {
+               HDEBUG(D_auth) debug_printf("dovecot: warning: too many results from tab-splitting; saw %d fields, room for %d\n", n, nptrs);
+               n = nptrs;
+       }
+
+       return n <= nptrs ? n : nptrs;
+}
+
+static void debug_strcut(uschar **ptrs, int nlen, int alen) ARG_UNUSED;
+static void
+debug_strcut(uschar **ptrs, int nlen, int alen)
+{
+        int i;
+        debug_printf("%d read but unreturned bytes; strcut() gave %d results: ",
+                        socket_buffer_left, nlen);
+        for (i = 0; i < nlen; i++) {
+                debug_printf(" {%s}", ptrs[i]);
+        }
+        if (nlen < alen)
+                debug_printf(" last is %s\n", ptrs[i] ? ptrs[i] : US"<null>");
+        else
+                debug_printf(" (max for capacity)\n");
+}
+
+#define CHECK_COMMAND(str, arg_min, arg_max) do { \
+       if (strcmpic(US(str), args[0]) != 0) \
+               goto out; \
+       if (nargs - 1 < (arg_min)) \
+               goto out; \
+       if ( (arg_max != -1) && (nargs - 1 > (arg_max)) ) \
+               goto out; \
+} while (0)
+
+#define OUT(msg) do { \
+       auth_defer_msg = (US msg); \
+       goto out; \
+} while(0)
+
+
+
+/*************************************************
+*      "fgets" to read directly from socket      *
+*************************************************/
+
+/* Added by PH after a suggestion by Steve Usher because the previous use of
+C-style buffered I/O gave trouble. */
+
+static uschar *
+dc_gets(uschar *s, int n, int fd)
+{
+int p = 0;
+int count = 0;
+
+for (;;)
+  {
+  if (socket_buffer_left == 0)
+    {
+    socket_buffer_left = read(fd, sbuffer, sizeof(sbuffer));
+    if (socket_buffer_left == 0) { if (count == 0) return NULL; else break; }
+    p = 0;
+    }
+
+  while (p < socket_buffer_left)
+    {
+    if (count >= n - 1) break;
+    s[count++] = sbuffer[p];
+    if (sbuffer[p++] == '\n') break;
+    }
+
+  memmove(sbuffer, sbuffer + p, socket_buffer_left - p);
+  socket_buffer_left -= p;
+
+  if (s[count-1] == '\n' || count >= n - 1) break;
+  }
+
+s[count] = '\0';
+return s;
+}
+
+
+
+
+/*************************************************
+*              Server entry point                *
+*************************************************/
+
+int auth_dovecot_server(auth_instance *ablock, uschar *data)
+{
+       auth_dovecot_options_block *ob =
+               (auth_dovecot_options_block *)(ablock->options_block);
+       struct sockaddr_un sa;
+       uschar buffer[DOVECOT_AUTH_MAXLINELEN];
+       uschar *args[DOVECOT_AUTH_MAXFIELDCOUNT];
+       uschar *auth_command;
+       uschar *auth_extra_data = US"";
+       uschar *p;
+       int nargs, tmp;
+       int crequid = 1, cont = 1, fd, ret = DEFER;
+       BOOL found = FALSE;
+
+       HDEBUG(D_auth) debug_printf("dovecot authentication\n");
+
+       memset(&sa, 0, sizeof(sa));
+       sa.sun_family = AF_UNIX;
+
+       /* This was the original code here: it is nonsense because strncpy()
+       does not return an integer. I have converted this to use the function
+       that formats and checks length. PH */
+
+       /*
+       if (strncpy(sa.sun_path, ob->server_socket, sizeof(sa.sun_path)) < 0) {
+       */
+
+       if (!string_format(US sa.sun_path, sizeof(sa.sun_path), "%s",
+                          ob->server_socket)) {
+               auth_defer_msg = US"authentication socket path too long";
+               return DEFER;
+       }
+
+       auth_defer_msg = US"authentication socket connection error";
+
+       fd = socket(PF_UNIX, SOCK_STREAM, 0);
+       if (fd < 0)
+               return DEFER;
+
+       if (connect(fd, (struct sockaddr *) &sa, sizeof(sa)) < 0)
+               goto out;
+
+       auth_defer_msg = US"authentication socket protocol error";
+
+       socket_buffer_left = 0;  /* Global, used to read more than a line but return by line */
+       while (cont) {
+               if (dc_gets(buffer, sizeof(buffer), fd) == NULL)
+                       OUT("authentication socket read error or premature eof");
+               p = buffer + Ustrlen(buffer) - 1;
+               if (*p != '\n') {
+                       OUT("authentication socket protocol line too long");
+               }
+               *p = '\0';
+               HDEBUG(D_auth) debug_printf("received: %s\n", buffer);
+               nargs = strcut(buffer, args, sizeof(args) / sizeof(args[0]));
+               /* HDEBUG(D_auth) debug_strcut(args, nargs, sizeof(args) / sizeof(args[0])); */
+
+               /* Code below rewritten by Kirill Miazine (km@krot.org). Only check commands that
+                  Exim will need. Original code also failed if Dovecot server sent unknown
+                  command. E.g. COOKIE in version 1.1 of the protocol would cause troubles. */
+               /* pdp: note that CUID is a per-connection identifier sent by the server,
+                  which increments at server discretion.
+                  By contrast, the "id" field of the protocol is a connection-specific request
+                  identifier, which needs to be unique per request from the client and is not
+                  connected to the CUID value, so we ignore CUID from server.  It's purely for
+                  diagnostics. */
+               if (Ustrcmp(args[0], US"VERSION") == 0) {
+                       CHECK_COMMAND("VERSION", 2, 2);
+                       if (Uatoi(args[1]) != VERSION_MAJOR)
+                               OUT("authentication socket protocol version mismatch");
+               } else if (Ustrcmp(args[0], US"MECH") == 0) {
+                       CHECK_COMMAND("MECH", 1, INT_MAX);
+                       if (strcmpic(US args[1], ablock->public_name) == 0)
+                               found = TRUE;
+               } else if (Ustrcmp(args[0], US"DONE") == 0) {
+                       CHECK_COMMAND("DONE", 0, 0);
+                       cont = 0;
+               }
+       }
+
+       if (!found) {
+               auth_defer_msg = string_sprintf("Dovecot did not advertise mechanism \"%s\" to us", ablock->public_name);
+               goto out;
+       }
+
+       /* Added by PH: data must not contain tab (as it is
+       b64 it shouldn't, but check for safety). */
+
+       if (Ustrchr(data, '\t') != NULL) {
+               ret = FAIL;
+               goto out;
+       }
+
+       /* Added by PH: extra fields when TLS is in use or if the TCP/IP
+       connection is local. */
+
+       if (tls_in.cipher != NULL)
+               auth_extra_data = string_sprintf("secured\t%s%s",
+                   tls_in.certificate_verified? "valid-client-cert" : "",
+                   tls_in.certificate_verified? "\t" : "");
+       else if (interface_address != NULL &&
+                Ustrcmp(sender_host_address, interface_address) == 0)
+               auth_extra_data = US"secured\t";
+
+
+/****************************************************************************
+   The code below was the original code here. It didn't work. A reading of the
+   file auth-protocol.txt.gz that came with Dovecot 1.0_beta8 indicated that
+   this was not right. Maybe something changed. I changed it to move the
+   service indication into the AUTH command, and it seems to be better. PH
+
+       fprintf(f, "VERSION\t%d\t%d\r\nSERVICE\tSMTP\r\nCPID\t%d\r\n"
+               "AUTH\t%d\t%s\trip=%s\tlip=%s\tresp=%s\r\n",
+               VERSION_MAJOR, VERSION_MINOR, getpid(), cuid,
+               ablock->public_name, sender_host_address, interface_address,
+               data ? (char *) data : "");
+
+   Subsequently, the command was modified to add "secured" and "valid-client-
+   cert" when relevant.
+****************************************************************************/
+
+       auth_command = string_sprintf("VERSION\t%d\t%d\nCPID\t%d\n"
+               "AUTH\t%d\t%s\tservice=smtp\t%srip=%s\tlip=%s\tnologin\tresp=%s\n",
+               VERSION_MAJOR, VERSION_MINOR, getpid(), crequid,
+               ablock->public_name, auth_extra_data, sender_host_address,
+               interface_address, data ? (char *) data : "");
+
+       if (write(fd, auth_command, Ustrlen(auth_command)) < 0)
+              HDEBUG(D_auth) debug_printf("error sending auth_command: %s\n",
+                strerror(errno));
+
+       HDEBUG(D_auth) debug_printf("sent: %s", auth_command);
+
+       while (1) {
+               uschar *temp;
+               uschar *auth_id_pre = NULL;
+               int i;
+
+               if (dc_gets(buffer, sizeof(buffer), fd) == NULL) {
+                       auth_defer_msg = US"authentication socket read error or premature eof";
+                       goto out;
+               }
+
+               buffer[Ustrlen(buffer) - 1] = 0;
+               HDEBUG(D_auth) debug_printf("received: %s\n", buffer);
+               nargs = strcut(buffer, args, sizeof(args) / sizeof(args[0]));
+
+               if (Uatoi(args[1]) != crequid)
+                       OUT("authentication socket connection id mismatch");
+
+               switch (toupper(*args[0])) {
+               case 'C':
+                       CHECK_COMMAND("CONT", 1, 2);
+
+                       tmp = auth_get_no64_data(&data, US args[2]);
+                       if (tmp != OK) {
+                               ret = tmp;
+                               goto out;
+                       }
+
+                       /* Added by PH: data must not contain tab (as it is
+                       b64 it shouldn't, but check for safety). */
+
+                       if (Ustrchr(data, '\t') != NULL) {
+                               ret = FAIL;
+                               goto out;
+                       }
+
+                       temp = string_sprintf("CONT\t%d\t%s\n", crequid, data);
+                       if (write(fd, temp, Ustrlen(temp)) < 0)
+                               OUT("authentication socket write error");
+                       break;
+
+               case 'F':
+                       CHECK_COMMAND("FAIL", 1, -1);
+
+                       for (i=2; (i<nargs) && (auth_id_pre == NULL); i++)
+                       {
+                               if ( Ustrncmp(args[i], US"user=", 5) == 0 )
+                               {
+                                       auth_id_pre = args[i]+5;
+                                       expand_nstring[1] = auth_vars[0] =
+                                               string_copy(auth_id_pre); /* PH */
+                                       expand_nlength[1] = Ustrlen(auth_id_pre);
+                                       expand_nmax = 1;
+                               }
+                       }
+
+                       ret = FAIL;
+                       goto out;
+
+               case 'O':
+                       CHECK_COMMAND("OK", 2, -1);
+
+                       /*
+                        * Search for the "user=$USER" string in the args array
+                        * and return the proper value.
+                        */
+                       for (i=2; (i<nargs) && (auth_id_pre == NULL); i++)
+                       {
+                               if ( Ustrncmp(args[i], US"user=", 5) == 0 )
+                               {
+                                       auth_id_pre = args[i]+5;
+                                       expand_nstring[1] = auth_vars[0] =
+                                               string_copy(auth_id_pre); /* PH */
+                                       expand_nlength[1] = Ustrlen(auth_id_pre);
+                                       expand_nmax = 1;
+                               }
+                       }
+
+                       if (auth_id_pre == NULL)
+                               OUT("authentication socket protocol error, username missing");
+
+                       ret = OK;
+                       /* fallthrough */
+
+               default:
+                       goto out;
+               }
+       }
+
+out:
+       /* close the socket used by dovecot */
+       if (fd >= 0)
+              close(fd);
+
+       /* Expand server_condition as an authorization check */
+       return (ret == OK)? auth_check_serv_cond(ablock) : ret;
+}
diff --git a/src/auths/dovecot.h b/src/auths/dovecot.h
new file mode 100644 (file)
index 0000000..ea3f04d
--- /dev/null
@@ -0,0 +1,28 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Private structure for the private options. */
+
+typedef struct {
+  uschar *server_socket;
+} auth_dovecot_options_block;
+
+/* Data for reading the private options. */
+
+extern optionlist auth_dovecot_options[];
+extern int auth_dovecot_options_count;
+
+/* Block containing default values. */
+
+extern auth_dovecot_options_block auth_dovecot_option_defaults;
+
+/* The entry points for the mechanism */
+
+extern void auth_dovecot_init(auth_instance *);
+extern int auth_dovecot_server(auth_instance *, uschar *);
+
+/* End of dovecot.h */
diff --git a/src/auths/get_data.c b/src/auths/get_data.c
new file mode 100644 (file)
index 0000000..a121bde
--- /dev/null
@@ -0,0 +1,47 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+#include "../exim.h"
+
+
+/*************************************************
+*      Issue a challenge and get a response      *
+*************************************************/
+
+/* This function is used by authentication drivers to output a challenge
+to the SMTP client and read the response line.
+
+Arguments:
+   aptr       set to point to the response (which is in big_buffer)
+   challenge  the challenge text (unencoded, may be binary)
+   challen    the length of the challenge text
+
+Returns:      OK on success
+              BAD64 if response too large for buffer
+              CANCELLED if response is "*"
+*/
+
+int
+auth_get_data(uschar **aptr, uschar *challenge, int challen)
+{
+int c;
+int p = 0;
+smtp_printf("334 %s\r\n", auth_b64encode(challenge, challen));
+while ((c = receive_getc()) != '\n' && c != EOF)
+  {
+  if (p >= big_buffer_size - 1) return BAD64;
+  big_buffer[p++] = c;
+  }
+if (p > 0 && big_buffer[p-1] == '\r') p--;
+big_buffer[p] = 0;
+DEBUG(D_receive) debug_printf("SMTP<< %s\n", big_buffer);
+if (Ustrcmp(big_buffer, "*") == 0) return CANCELLED;
+*aptr = big_buffer;
+return OK;
+}
+
+/* End of get_data.c */
diff --git a/src/auths/get_no64_data.c b/src/auths/get_no64_data.c
new file mode 100644 (file)
index 0000000..d3ffe08
--- /dev/null
@@ -0,0 +1,47 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2012 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+#include "../exim.h"
+
+
+/*************************************************
+*  Issue a non-b64 challenge and get a response  *
+*************************************************/
+
+/* This function is used by authentication drivers to output a challenge
+to the SMTP client and read the response line. This version does not use base
+64 encoding for the text on the 334 line. It is used by the SPA, dovecot
+and gsasl authenticators.
+
+Arguments:
+   aptr       set to point to the response (which is in big_buffer)
+   challenge  the challenge text (unencoded)
+
+Returns:      OK on success
+              BAD64 if response too large for buffer
+              CANCELLED if response is "*"
+*/
+
+int
+auth_get_no64_data(uschar **aptr, uschar *challenge)
+{
+int c;
+int p = 0;
+smtp_printf("334 %s\r\n", challenge);
+while ((c = receive_getc()) != '\n' && c != EOF)
+  {
+  if (p >= big_buffer_size - 1) return BAD64;
+  big_buffer[p++] = c;
+  }
+if (p > 0 && big_buffer[p-1] == '\r') p--;
+big_buffer[p] = 0;
+if (Ustrcmp(big_buffer, "*") == 0) return CANCELLED;
+*aptr = big_buffer;
+return OK;
+}
+
+/* End of get_no64_data.c */
diff --git a/src/auths/gsasl_exim.c b/src/auths/gsasl_exim.c
new file mode 100644 (file)
index 0000000..87be9b5
--- /dev/null
@@ -0,0 +1,619 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2012 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Copyright (c) Twitter Inc 2012
+   Author: Phil Pennock <pdp@exim.org> */
+/* Copyright (c) Phil Pennock 2012 */
+
+/* Interface to GNU SASL library for generic authentication. */
+
+/* Trade-offs:
+
+GNU SASL does not provide authentication data itself, so we have to expose
+that decision to configuration.  For some mechanisms, we need to act much
+like plaintext.  For others, we only need to be able to provide some
+evaluated data on demand.  There's no abstracted way (ie, without hardcoding
+knowledge of authenticators here) to know which need what properties; we
+can't query a session or the library for "we will need these for mechanism X".
+
+So: we always require server_condition, even if sometimes it will just be
+set as "yes".  We do provide a number of other hooks, which might not make
+sense in all contexts.  For some, we can do checks at init time.
+*/
+
+#include "../exim.h"
+
+#ifndef AUTH_GSASL
+/* dummy function to satisfy compilers when we link in an "empty" file. */
+static void dummy(int x);
+static void dummy2(int x) { dummy(x-1); }
+static void dummy(int x) { dummy2(x-1); }
+#else
+
+#include <gsasl.h>
+#include "gsasl_exim.h"
+
+/* Authenticator-specific options. */
+/* I did have server_*_condition options for various mechanisms, but since
+we only ever handle one mechanism at a time, I didn't see the point in keeping
+that.  In case someone sees a point, I've left the condition_check() API
+alone. */
+optionlist auth_gsasl_options[] = {
+  { "server_channelbinding", opt_bool,
+      (void *)(offsetof(auth_gsasl_options_block, server_channelbinding)) },
+  { "server_hostname",      opt_stringptr,
+      (void *)(offsetof(auth_gsasl_options_block, server_hostname)) },
+  { "server_mech",          opt_stringptr,
+      (void *)(offsetof(auth_gsasl_options_block, server_mech)) },
+  { "server_password",      opt_stringptr,
+      (void *)(offsetof(auth_gsasl_options_block, server_password)) },
+  { "server_realm",         opt_stringptr,
+      (void *)(offsetof(auth_gsasl_options_block, server_realm)) },
+  { "server_scram_iter",    opt_stringptr,
+      (void *)(offsetof(auth_gsasl_options_block, server_scram_iter)) },
+  { "server_scram_salt",    opt_stringptr,
+      (void *)(offsetof(auth_gsasl_options_block, server_scram_salt)) },
+  { "server_service",       opt_stringptr,
+      (void *)(offsetof(auth_gsasl_options_block, server_service)) }
+};
+/* GSASL_SCRAM_SALTED_PASSWORD documented only for client, so not implementing
+hooks to avoid cleartext passwords in the Exim server. */
+
+int auth_gsasl_options_count =
+  sizeof(auth_gsasl_options)/sizeof(optionlist);
+
+/* Defaults for the authenticator-specific options. */
+auth_gsasl_options_block auth_gsasl_option_defaults = {
+  US"smtp",                 /* server_service */
+  US"$primary_hostname",    /* server_hostname */
+  NULL,                     /* server_realm */
+  NULL,                     /* server_mech */
+  NULL,                     /* server_password */
+  NULL,                     /* server_scram_iter */
+  NULL,                     /* server_scram_salt */
+  FALSE                     /* server_channelbinding */
+};
+
+/* "Globals" for managing the gsasl interface. */
+
+static Gsasl *gsasl_ctx = NULL;
+static int
+  main_callback(Gsasl *ctx, Gsasl_session *sctx, Gsasl_property prop);
+static int
+  server_callback(Gsasl *ctx, Gsasl_session *sctx, Gsasl_property prop, auth_instance *ablock);
+static int
+  client_callback(Gsasl *ctx, Gsasl_session *sctx, Gsasl_property prop, auth_instance *ablock);
+
+static BOOL sasl_error_should_defer = FALSE;
+static Gsasl_property callback_loop = 0;
+static BOOL checked_server_condition = FALSE;
+
+enum { CURRENTLY_SERVER = 1, CURRENTLY_CLIENT = 2 };
+
+struct callback_exim_state {
+  auth_instance *ablock;
+  int currently;
+};
+
+
+/*************************************************
+*          Initialization entry point            *
+*************************************************/
+
+/* Called for each instance, after its options have been read, to
+enable consistency checks to be done, or anything else that needs
+to be set up. */
+
+void
+auth_gsasl_init(auth_instance *ablock)
+{
+  char *p;
+  int rc, supported;
+  auth_gsasl_options_block *ob =
+    (auth_gsasl_options_block *)(ablock->options_block);
+
+  /* As per existing Cyrus glue, use the authenticator's public name as
+  the default for the mechanism name; we don't handle multiple mechanisms
+  in one authenticator, but the same driver can be used multiple times. */
+
+  if (ob->server_mech == NULL)
+    ob->server_mech = string_copy(ablock->public_name);
+
+  /* Can get multiple session contexts from one library context, so just
+  initialise the once. */
+  if (gsasl_ctx == NULL) {
+    rc = gsasl_init(&gsasl_ctx);
+    if (rc != GSASL_OK) {
+      log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s authenticator:  "
+                "couldn't initialise GNU SASL library: %s (%s)",
+                ablock->name, gsasl_strerror_name(rc), gsasl_strerror(rc));
+    }
+    gsasl_callback_set(gsasl_ctx, main_callback);
+  }
+
+  /* We don't need this except to log it for debugging. */
+  rc = gsasl_server_mechlist(gsasl_ctx, &p);
+  if (rc != GSASL_OK)
+    log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s authenticator:  "
+              "failed to retrieve list of mechanisms: %s (%s)",
+              ablock->name,  gsasl_strerror_name(rc), gsasl_strerror(rc));
+  HDEBUG(D_auth) debug_printf("GNU SASL supports: %s\n", p);
+
+  supported = gsasl_client_support_p(gsasl_ctx, (const char *)ob->server_mech);
+  if (!supported)
+    log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s authenticator:  "
+              "GNU SASL does not support mechanism \"%s\"",
+              ablock->name, ob->server_mech);
+
+  if ((ablock->server_condition == NULL) &&
+      (streqic(ob->server_mech, US"EXTERNAL") ||
+       streqic(ob->server_mech, US"ANONYMOUS") ||
+       streqic(ob->server_mech, US"PLAIN") ||
+       streqic(ob->server_mech, US"LOGIN")))
+    log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s authenticator:  "
+              "Need server_condition for %s mechanism",
+              ablock->name, ob->server_mech);
+
+  /* This does *not* scale to new SASL mechanisms.  Need a better way to ask
+  which properties will be needed. */
+  if ((ob->server_realm == NULL) &&
+      streqic(ob->server_mech, US"DIGEST-MD5"))
+    log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s authenticator:  "
+              "Need server_realm for %s mechanism",
+              ablock->name, ob->server_mech);
+
+  /* At present, for mechanisms we don't panic on absence of server_condition;
+  need to figure out the most generically correct approach to deciding when
+  it's critical and when it isn't.  Eg, for simple validation (PLAIN mechanism,
+  etc) it clearly is critical.
+
+  So don't activate without server_condition, this might be relaxed in the future.
+  */
+  if (ablock->server_condition != NULL) ablock->server = TRUE;
+  ablock->client = FALSE;
+}
+
+
+/* GNU SASL uses one top-level callback, registered at library level.
+We dispatch to client and server functions instead. */
+
+static int
+main_callback(Gsasl *ctx, Gsasl_session *sctx, Gsasl_property prop)
+{
+  int rc = 0;
+  struct callback_exim_state *cb_state =
+    (struct callback_exim_state *)gsasl_session_hook_get(sctx);
+
+  HDEBUG(D_auth)
+    debug_printf("GNU SASL Callback entered, prop=%d (loop prop=%d)\n",
+        prop, callback_loop);
+
+  if (cb_state == NULL) {
+    HDEBUG(D_auth) debug_printf("  not from our server/client processing.\n");
+    return GSASL_NO_CALLBACK;
+  }
+
+  if (callback_loop > 0) {
+    /* Most likely is that we were asked for property foo, and to
+    expand the string we asked for property bar to put into an auth
+    variable, but property bar is not supplied for this mechanism. */
+    HDEBUG(D_auth)
+      debug_printf("Loop, asked for property %d while handling property %d\n",
+          prop, callback_loop);
+    return GSASL_NO_CALLBACK;
+  }
+  callback_loop = prop;
+
+  if (cb_state->currently == CURRENTLY_CLIENT)
+    rc = client_callback(ctx, sctx, prop, cb_state->ablock);
+  else if (cb_state->currently == CURRENTLY_SERVER)
+    rc = server_callback(ctx, sctx, prop, cb_state->ablock);
+  else {
+    log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s authenticator:  "
+        "unhandled callback state, bug in Exim", cb_state->ablock->name);
+    /* NOTREACHED */
+  }
+
+  callback_loop = 0;
+  return rc;
+}
+
+
+/*************************************************
+*             Server entry point                 *
+*************************************************/
+
+/* For interface, see auths/README */
+
+int
+auth_gsasl_server(auth_instance *ablock, uschar *initial_data)
+{
+  char *tmps;
+  char *to_send, *received;
+  Gsasl_session *sctx = NULL;
+  auth_gsasl_options_block *ob =
+    (auth_gsasl_options_block *)(ablock->options_block);
+  struct callback_exim_state cb_state;
+  int rc, auth_result, exim_error, exim_error_override;
+
+  HDEBUG(D_auth)
+    debug_printf("GNU SASL: initialising session for %s, mechanism %s.\n",
+        ablock->name, ob->server_mech);
+
+  rc = gsasl_server_start(gsasl_ctx, (const char *)ob->server_mech, &sctx);
+  if (rc != GSASL_OK) {
+    auth_defer_msg = string_sprintf("GNU SASL: session start failure: %s (%s)",
+        gsasl_strerror_name(rc), gsasl_strerror(rc));
+    HDEBUG(D_auth) debug_printf("%s\n", auth_defer_msg);
+    return DEFER;
+  }
+  /* Hereafter: gsasl_finish(sctx) please */
+
+  gsasl_session_hook_set(sctx, (void *)ablock);
+  cb_state.ablock = ablock;
+  cb_state.currently = CURRENTLY_SERVER;
+  gsasl_session_hook_set(sctx, (void *)&cb_state);
+
+  tmps = CS expand_string(ob->server_service);
+  gsasl_property_set(sctx, GSASL_SERVICE, tmps);
+  tmps = CS expand_string(ob->server_hostname);
+  gsasl_property_set(sctx, GSASL_HOSTNAME, tmps);
+  if (ob->server_realm) {
+    tmps = CS expand_string(ob->server_realm);
+    if (tmps && *tmps) {
+      gsasl_property_set(sctx, GSASL_REALM, tmps);
+    }
+  }
+  /* We don't support protection layers. */
+  gsasl_property_set(sctx, GSASL_QOPS, "qop-auth");
+#ifdef SUPPORT_TLS
+  if (tls_channelbinding_b64) {
+    /* Some auth mechanisms can ensure that both sides are talking withing the
+    same security context; for TLS, this means that even if a bad certificate
+    has been accepted, they remain MitM-proof because both sides must be within
+    the same negotiated session; if someone is terminating one sesson and
+    proxying data on within a second, authentication will fail.
+
+    We might not have this available, depending upon TLS implementation,
+    ciphersuite, phase of moon ...
+
+    If we do, it results in extra SASL mechanisms being available; here,
+    Exim's one-mechanism-per-authenticator potentially causes problems.
+    It depends upon how GNU SASL will implement the PLUS variants of GS2
+    and whether it automatically mandates a switch to the bound PLUS
+    if the data is available.  Since default-on, despite being more secure,
+    would then result in mechanism name changes on a library update, we
+    have little choice but to default it off and let the admin choose to
+    enable it.  *sigh*
+    */
+    if (ob->server_channelbinding) {
+      HDEBUG(D_auth) debug_printf("Auth %s: Enabling channel-binding\n",
+          ablock->name);
+      gsasl_property_set(sctx, GSASL_CB_TLS_UNIQUE,
+          (const char *) tls_channelbinding_b64);
+    } else {
+      HDEBUG(D_auth)
+        debug_printf("Auth %s: Not enabling channel-binding (data available)\n",
+            ablock->name);
+    }
+  } else {
+    HDEBUG(D_auth)
+      debug_printf("Auth %s: no channel-binding data available\n",
+          ablock->name);
+  }
+#endif
+
+  checked_server_condition = FALSE;
+
+  received = CS initial_data;
+  to_send = NULL;
+  exim_error = exim_error_override = OK;
+
+  do {
+    rc = gsasl_step64(sctx, received, &to_send);
+
+    switch (rc) {
+      case GSASL_OK:
+        if (!to_send)
+          goto STOP_INTERACTION;
+        break;
+
+      case GSASL_NEEDS_MORE:
+        break;
+
+      case GSASL_AUTHENTICATION_ERROR:
+      case GSASL_INTEGRITY_ERROR:
+      case GSASL_NO_AUTHID:
+      case GSASL_NO_ANONYMOUS_TOKEN:
+      case GSASL_NO_AUTHZID:
+      case GSASL_NO_PASSWORD:
+      case GSASL_NO_PASSCODE:
+      case GSASL_NO_PIN:
+      case GSASL_BASE64_ERROR:
+        HDEBUG(D_auth) debug_printf("GNU SASL permanent error: %s (%s)\n",
+            gsasl_strerror_name(rc), gsasl_strerror(rc));
+        log_write(0, LOG_REJECT, "%s authenticator (%s):\n  "
+            "GNU SASL permanent failure: %s (%s)",
+            ablock->name, ob->server_mech,
+            gsasl_strerror_name(rc), gsasl_strerror(rc));
+        if (rc == GSASL_BASE64_ERROR)
+          exim_error_override = BAD64;
+        goto STOP_INTERACTION;
+
+      default:
+        auth_defer_msg = string_sprintf("GNU SASL temporary error: %s (%s)",
+            gsasl_strerror_name(rc), gsasl_strerror(rc));
+        HDEBUG(D_auth) debug_printf("%s\n", auth_defer_msg);
+        exim_error_override = DEFER;
+        goto STOP_INTERACTION;
+    }
+
+    if ((rc == GSASL_NEEDS_MORE) ||
+        (to_send && *to_send))
+      exim_error =
+        auth_get_no64_data((uschar **)&received, (uschar *)to_send);
+
+    if (to_send) {
+      free(to_send);
+      to_send = NULL;
+    }
+
+    if (exim_error)
+      break; /* handles * cancelled check */
+
+  } while (rc == GSASL_NEEDS_MORE);
+
+STOP_INTERACTION:
+  auth_result = rc;
+
+  gsasl_finish(sctx);
+
+  /* Can return: OK DEFER FAIL CANCELLED BAD64 UNEXPECTED */
+
+  if (exim_error != OK)
+    return exim_error;
+
+  if (auth_result != GSASL_OK) {
+    HDEBUG(D_auth) debug_printf("authentication returned %s (%s)\n",
+        gsasl_strerror_name(auth_result), gsasl_strerror(auth_result));
+    if (exim_error_override != OK)
+      return exim_error_override; /* might be DEFER */
+    if (sasl_error_should_defer) /* overriding auth failure SASL error */
+      return DEFER;
+    return FAIL;
+  }
+
+  /* Auth succeeded, check server_condition unless already done in callback */
+  return checked_server_condition ? OK : auth_check_serv_cond(ablock);
+}
+
+/* returns the GSASL status of expanding the Exim string given */
+static int
+condition_check(auth_instance *ablock, uschar *label, uschar *condition_string)
+{
+  int exim_rc;
+
+  exim_rc = auth_check_some_cond(ablock, label, condition_string, FAIL);
+
+  if (exim_rc == OK) {
+    return GSASL_OK;
+  } else if (exim_rc == DEFER) {
+    sasl_error_should_defer = TRUE;
+    return GSASL_AUTHENTICATION_ERROR;
+  } else if (exim_rc == FAIL) {
+    return GSASL_AUTHENTICATION_ERROR;
+  }
+
+  log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s authenticator:  "
+            "Unhandled return from checking %s: %d",
+            ablock->name, label, exim_rc);
+  /* NOTREACHED */
+  return GSASL_AUTHENTICATION_ERROR;
+}
+
+static int
+server_callback(Gsasl *ctx, Gsasl_session *sctx, Gsasl_property prop, auth_instance *ablock)
+{
+  char *tmps;
+  uschar *propval;
+  int cbrc = GSASL_NO_CALLBACK;
+  int i;
+  auth_gsasl_options_block *ob =
+    (auth_gsasl_options_block *)(ablock->options_block);
+
+  HDEBUG(D_auth)
+    debug_printf("GNU SASL callback %d for %s/%s as server\n",
+        prop, ablock->name, ablock->public_name);
+
+  for (i = 0; i < AUTH_VARS; i++) auth_vars[i] = NULL;
+  expand_nmax = 0;
+
+  switch (prop) {
+    case GSASL_VALIDATE_SIMPLE:
+      /* GSASL_AUTHID, GSASL_AUTHZID, and GSASL_PASSWORD */
+      propval = (uschar *) gsasl_property_fast(sctx, GSASL_AUTHID);
+      auth_vars[0] = expand_nstring[1] = propval ? propval : US"";
+      propval = (uschar *) gsasl_property_fast(sctx, GSASL_AUTHZID);
+      auth_vars[1] = expand_nstring[2] = propval ? propval : US"";
+      propval = (uschar *) gsasl_property_fast(sctx, GSASL_PASSWORD);
+      auth_vars[2] = expand_nstring[3] = propval ? propval : US"";
+      expand_nmax = 3;
+      for (i = 1; i <= 3; ++i)
+        expand_nlength[i] = Ustrlen(expand_nstring[i]);
+
+      cbrc = condition_check(ablock, US"server_condition", ablock->server_condition);
+      checked_server_condition = TRUE;
+      break;
+
+    case GSASL_VALIDATE_EXTERNAL:
+      if (ablock->server_condition == NULL) {
+        HDEBUG(D_auth) debug_printf("No server_condition supplied, to validate EXTERNAL.\n");
+        cbrc = GSASL_AUTHENTICATION_ERROR;
+        break;
+      }
+      propval = (uschar *) gsasl_property_fast(sctx, GSASL_AUTHZID);
+      /* We always set $auth1, even if only to empty string. */
+      auth_vars[0] = expand_nstring[1] = propval ? propval : US"";
+      expand_nlength[1] = Ustrlen(expand_nstring[1]);
+      expand_nmax = 1;
+
+      cbrc = condition_check(ablock,
+          US"server_condition (EXTERNAL)", ablock->server_condition);
+      checked_server_condition = TRUE;
+      break;
+
+    case GSASL_VALIDATE_ANONYMOUS:
+      if (ablock->server_condition == NULL) {
+        HDEBUG(D_auth) debug_printf("No server_condition supplied, to validate ANONYMOUS.\n");
+        cbrc = GSASL_AUTHENTICATION_ERROR;
+        break;
+      }
+      propval = (uschar *) gsasl_property_fast(sctx, GSASL_ANONYMOUS_TOKEN);
+      /* We always set $auth1, even if only to empty string. */
+      auth_vars[0] = expand_nstring[1] = propval ? propval : US"";
+      expand_nlength[1] = Ustrlen(expand_nstring[1]);
+      expand_nmax = 1;
+
+      cbrc = condition_check(ablock,
+          US"server_condition (ANONYMOUS)", ablock->server_condition);
+      checked_server_condition = TRUE;
+      break;
+
+    case GSASL_VALIDATE_GSSAPI:
+      /* GSASL_AUTHZID and GSASL_GSSAPI_DISPLAY_NAME
+      The display-name is authenticated as part of GSS, the authzid is claimed
+      by the SASL integration after authentication; protected against tampering
+      (if the SASL mechanism supports that, which Kerberos does) but is
+      unverified, same as normal for other mechanisms.
+
+      First coding, we had these values swapped, but for consistency and prior
+      to the first release of Exim with this authenticator, they've been
+      switched to match the ordering of GSASL_VALIDATE_SIMPLE. */
+      propval = (uschar *) gsasl_property_fast(sctx, GSASL_GSSAPI_DISPLAY_NAME);
+      auth_vars[0] = expand_nstring[1] = propval ? propval : US"";
+      propval = (uschar *) gsasl_property_fast(sctx, GSASL_AUTHZID);
+      auth_vars[1] = expand_nstring[2] = propval ? propval : US"";
+      expand_nmax = 2;
+      for (i = 1; i <= 2; ++i)
+        expand_nlength[i] = Ustrlen(expand_nstring[i]);
+
+      /* In this one case, it perhaps makes sense to default back open?
+      But for consistency, let's just mandate server_condition here too. */
+      cbrc = condition_check(ablock,
+          US"server_condition (GSSAPI family)", ablock->server_condition);
+      checked_server_condition = TRUE;
+      break;
+
+    case GSASL_PASSWORD:
+      /* DIGEST-MD5: GSASL_AUTHID, GSASL_AUTHZID and GSASL_REALM
+         CRAM-MD5: GSASL_AUTHID
+         PLAIN: GSASL_AUTHID and GSASL_AUTHZID
+         LOGIN: GSASL_AUTHID
+       */
+      if (ob->server_scram_iter) {
+        tmps = CS expand_string(ob->server_scram_iter);
+        gsasl_property_set(sctx, GSASL_SCRAM_ITER, tmps);
+      }
+      if (ob->server_scram_salt) {
+        tmps = CS expand_string(ob->server_scram_salt);
+        gsasl_property_set(sctx, GSASL_SCRAM_SALT, tmps);
+      }
+      /* Asking for GSASL_AUTHZID calls back into us if we use
+      gsasl_property_get(), thus the use of gsasl_property_fast().
+      Do we really want to hardcode limits per mechanism?  What happens when
+      a new mechanism is added to the library.  It *shouldn't* result in us
+      needing to add more glue, since avoiding that is a large part of the
+      point of SASL. */
+      propval = (uschar *) gsasl_property_fast(sctx, GSASL_AUTHID);
+      auth_vars[0] = expand_nstring[1] = propval ? propval : US"";
+      propval = (uschar *) gsasl_property_fast(sctx, GSASL_AUTHZID);
+      auth_vars[1] = expand_nstring[2] = propval ? propval : US"";
+      propval = (uschar *) gsasl_property_fast(sctx, GSASL_REALM);
+      auth_vars[2] = expand_nstring[3] = propval ? propval : US"";
+      expand_nmax = 3;
+      for (i = 1; i <= 3; ++i)
+        expand_nlength[i] = Ustrlen(expand_nstring[i]);
+
+      tmps = CS expand_string(ob->server_password);
+      if (tmps == NULL) {
+        sasl_error_should_defer = expand_string_forcedfail ? FALSE : TRUE;
+        HDEBUG(D_auth) debug_printf("server_password expansion failed, so "
+            "can't tell GNU SASL library the password for %s\n", auth_vars[0]);
+        return GSASL_AUTHENTICATION_ERROR;
+      }
+      gsasl_property_set(sctx, GSASL_PASSWORD, tmps);
+      /* This is inadequate; don't think Exim's store stacks are geared
+      for memory wiping, so expanding strings will leave stuff laying around.
+      But no need to compound the problem, so get rid of the one we can. */
+      memset(tmps, '\0', strlen(tmps));
+      cbrc = GSASL_OK;
+      break;
+
+    default:
+      HDEBUG(D_auth) debug_printf("Unrecognised callback: %d\n", prop);
+      cbrc = GSASL_NO_CALLBACK;
+  }
+
+  HDEBUG(D_auth) debug_printf("Returning %s (%s)\n",
+      gsasl_strerror_name(cbrc), gsasl_strerror(cbrc));
+
+  return cbrc;
+}
+
+
+/*************************************************
+*              Client entry point                *
+*************************************************/
+
+/* For interface, see auths/README */
+
+int
+auth_gsasl_client(
+  auth_instance *ablock,                 /* authenticator block */
+  smtp_inblock *inblock,                 /* connection inblock */
+  smtp_outblock *outblock,               /* connection outblock */
+  int timeout,                           /* command timeout */
+  uschar *buffer,                        /* buffer for reading response */
+  int buffsize)                          /* size of buffer */
+{
+  HDEBUG(D_auth)
+    debug_printf("Client side NOT IMPLEMENTED: you should not see this!\n");
+  /* NOT IMPLEMENTED */
+  return FAIL;
+}
+
+static int
+client_callback(Gsasl *ctx, Gsasl_session *sctx, Gsasl_property prop, auth_instance *ablock)
+{
+  int cbrc = GSASL_NO_CALLBACK;
+  HDEBUG(D_auth)
+    debug_printf("GNU SASL callback %d for %s/%s as client\n",
+        prop, ablock->name, ablock->public_name);
+
+  HDEBUG(D_auth)
+    debug_printf("Client side NOT IMPLEMENTED: you should not see this!\n");
+
+  return cbrc;
+}
+
+/*************************************************
+*                Diagnostic API                  *
+*************************************************/
+
+void
+auth_gsasl_version_report(FILE *f)
+{
+  const char *runtime;
+  runtime = gsasl_check_version(NULL);
+  fprintf(f, "Library version: GNU SASL: Compile: %s\n"
+             "                           Runtime: %s\n",
+          GSASL_VERSION, runtime);
+}
+
+#endif  /* AUTH_GSASL */
+
+/* End of gsasl_exim.c */
diff --git a/src/auths/gsasl_exim.h b/src/auths/gsasl_exim.h
new file mode 100644 (file)
index 0000000..785b853
--- /dev/null
@@ -0,0 +1,42 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2012 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Copyright (c) Twitter Inc 2012 */
+
+/* Interface to GNU SASL library for generic authentication. */
+
+/* Authenticator-specific options. */
+
+typedef struct {
+  uschar *server_service;
+  uschar *server_hostname;
+  uschar *server_realm;
+  uschar *server_mech;
+  uschar *server_password;
+  uschar *server_scram_iter;
+  uschar *server_scram_salt;
+  BOOL    server_channelbinding;
+} auth_gsasl_options_block;
+
+/* Data for reading the authenticator-specific options. */
+
+extern optionlist auth_gsasl_options[];
+extern int auth_gsasl_options_count;
+
+/* Defaults for the authenticator-specific options. */
+
+extern auth_gsasl_options_block auth_gsasl_option_defaults;
+
+/* The entry points for the mechanism */
+
+extern void auth_gsasl_init(auth_instance *);
+extern int auth_gsasl_server(auth_instance *, uschar *);
+extern int auth_gsasl_client(auth_instance *, smtp_inblock *,
+                                smtp_outblock *, int, uschar *, int);
+extern void auth_gsasl_version_report(FILE *f);
+
+/* End of gsasl_exim.h */
diff --git a/src/auths/heimdal_gssapi.c b/src/auths/heimdal_gssapi.c
new file mode 100644 (file)
index 0000000..21ed75b
--- /dev/null
@@ -0,0 +1,595 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2012 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Copyright (c) Twitter Inc 2012
+   Author: Phil Pennock <pdp@exim.org> */
+/* Copyright (c) Phil Pennock 2012 */
+
+/* Interface to Heimdal library for GSSAPI authentication. */
+
+/* Naming and rationale
+
+Sensibly, this integration would be deferred to a SASL library, but none
+of them appear to offer keytab file selection interfaces in their APIs.  It
+might be that this driver only requires minor modification to work with MIT
+Kerberos.
+
+Heimdal provides a number of interfaces for various forms of authentication.
+As GS2 does not appear to provide keytab control interfaces either, we may
+end up supporting that too.  It's possible that we could trivially expand to
+support NTLM support via Heimdal, etc.  Rather than try to be too generic
+immediately, this driver is directly only supporting GSSAPI.
+
+Without rename, we could add an option for GS2 support in the future.
+*/
+
+/* Sources
+
+* mailcheck-imap (Perl, client-side, written by me years ago)
+* gsasl driver (GPL, server-side)
+* heimdal sources and man-pages, plus http://www.h5l.org/manual/
+* FreeBSD man-pages (very informative!)
+* http://www.ggf.org/documents/GFD.24.pdf confirming GSS_KRB5_REGISTER_ACCEPTOR_IDENTITY_X
+  semantics, that found by browsing Heimdal source to find how to set the keytab; however,
+  after multiple attempts I failed to get that to work and instead switched to
+  gsskrb5_register_acceptor_identity().
+*/
+
+#include "../exim.h"
+
+#ifndef AUTH_HEIMDAL_GSSAPI
+/* dummy function to satisfy compilers when we link in an "empty" file. */
+static void dummy(int x);
+static void dummy2(int x) { dummy(x-1); }
+static void dummy(int x) { dummy2(x-1); }
+#else
+
+#include <gssapi/gssapi.h>
+#include <gssapi/gssapi_krb5.h>
+
+/* for the _init debugging */
+#include <krb5.h>
+
+#include "heimdal_gssapi.h"
+
+/* Authenticator-specific options. */
+optionlist auth_heimdal_gssapi_options[] = {
+  { "server_hostname",      opt_stringptr,
+      (void *)(offsetof(auth_heimdal_gssapi_options_block, server_hostname)) },
+  { "server_keytab",        opt_stringptr,
+      (void *)(offsetof(auth_heimdal_gssapi_options_block, server_keytab)) },
+  { "server_service",       opt_stringptr,
+      (void *)(offsetof(auth_heimdal_gssapi_options_block, server_service)) }
+};
+
+int auth_heimdal_gssapi_options_count =
+  sizeof(auth_heimdal_gssapi_options)/sizeof(optionlist);
+
+/* Defaults for the authenticator-specific options. */
+auth_heimdal_gssapi_options_block auth_heimdal_gssapi_option_defaults = {
+  US"$primary_hostname",    /* server_hostname */
+  NULL,                     /* server_keytab */
+  US"smtp",                 /* server_service */
+};
+
+/* "Globals" for managing the heimdal_gssapi interface. */
+
+/* Utility functions */
+static void
+  exim_heimdal_error_debug(const char *, krb5_context, krb5_error_code);
+static int
+  exim_gssapi_error_defer(uschar *, OM_uint32, OM_uint32, const char *, ...)
+    PRINTF_FUNCTION(4, 5);
+
+#define EmptyBuf(buf) do { buf.value = NULL; buf.length = 0; } while (0)
+
+
+/*************************************************
+*          Initialization entry point            *
+*************************************************/
+
+/* Called for each instance, after its options have been read, to
+enable consistency checks to be done, or anything else that needs
+to be set up. */
+
+/* Heimdal provides a GSSAPI extension method for setting the keytab;
+in the init, we mostly just use raw krb5 methods so that we can report
+the keytab contents, for -D+auth debugging. */
+
+void
+auth_heimdal_gssapi_init(auth_instance *ablock)
+{
+  krb5_context context;
+  krb5_keytab keytab;
+  krb5_kt_cursor cursor;
+  krb5_keytab_entry entry;
+  krb5_error_code krc;
+  char *principal, *enctype_s;
+  const char *k_keytab_typed_name = NULL;
+  auth_heimdal_gssapi_options_block *ob =
+    (auth_heimdal_gssapi_options_block *)(ablock->options_block);
+
+  ablock->server = FALSE;
+  ablock->client = FALSE;
+
+  if (!ob->server_service || !*ob->server_service) {
+    HDEBUG(D_auth) debug_printf("heimdal: missing server_service\n");
+    return;
+  }
+
+  krc = krb5_init_context(&context);
+  if (krc != 0) {
+    int kerr = errno;
+    HDEBUG(D_auth) debug_printf("heimdal: failed to initialise krb5 context: %s\n",
+        strerror(kerr));
+    return;
+  }
+
+  if (ob->server_keytab) {
+    k_keytab_typed_name = CCS string_sprintf("file:%s", expand_string(ob->server_keytab));
+    HDEBUG(D_auth) debug_printf("heimdal: using keytab %s\n", k_keytab_typed_name);
+    krc = krb5_kt_resolve(context, k_keytab_typed_name, &keytab);
+    if (krc) {
+      HDEBUG(D_auth) exim_heimdal_error_debug("krb5_kt_resolve", context, krc);
+      return;
+    }
+  } else {
+    HDEBUG(D_auth) debug_printf("heimdal: using system default keytab\n");
+    krc = krb5_kt_default(context, &keytab);
+    if (krc) {
+      HDEBUG(D_auth) exim_heimdal_error_debug("krb5_kt_default", context, krc);
+      return;
+    }
+  }
+
+  HDEBUG(D_auth) {
+    /* http://www.h5l.org/manual/HEAD/krb5/krb5_keytab_intro.html */
+    krc = krb5_kt_start_seq_get(context, keytab, &cursor);
+    if (krc)
+      exim_heimdal_error_debug("krb5_kt_start_seq_get", context, krc);
+    else {
+      while ((krc = krb5_kt_next_entry(context, keytab, &entry, &cursor)) == 0) {
+        principal = enctype_s = NULL;
+        krb5_unparse_name(context, entry.principal, &principal);
+        krb5_enctype_to_string(context, entry.keyblock.keytype, &enctype_s);
+        debug_printf("heimdal: keytab principal: %s  vno=%d  type=%s\n",
+            principal ? principal : "??",
+            entry.vno,
+            enctype_s ? enctype_s : "??");
+        free(principal);
+        free(enctype_s);
+        krb5_kt_free_entry(context, &entry);
+      }
+      krc = krb5_kt_end_seq_get(context, keytab, &cursor);
+      if (krc)
+        exim_heimdal_error_debug("krb5_kt_end_seq_get", context, krc);
+    }
+  }
+
+  krc = krb5_kt_close(context, keytab);
+  if (krc)
+    HDEBUG(D_auth) exim_heimdal_error_debug("krb5_kt_close", context, krc);
+
+  krb5_free_context(context);
+
+  /* RFC 4121 section 5.2, SHOULD support 64K input buffers */
+  if (big_buffer_size < (64 * 1024)) {
+    uschar *newbuf;
+    big_buffer_size = 64 * 1024;
+    newbuf = store_malloc(big_buffer_size);
+    store_free(big_buffer);
+    big_buffer = newbuf;
+  }
+
+  ablock->server = TRUE;
+}
+
+
+static void
+exim_heimdal_error_debug(const char *label,
+    krb5_context context, krb5_error_code err)
+{
+  const char *kerrsc;
+  kerrsc = krb5_get_error_message(context, err);
+  debug_printf("heimdal %s: %s\n", label, kerrsc ? kerrsc : "unknown error");
+  krb5_free_error_message(context, kerrsc);
+}
+
+/*************************************************
+*             Server entry point                 *
+*************************************************/
+
+/* For interface, see auths/README */
+
+/* GSSAPI notes:
+OM_uint32: portable type for unsigned int32
+gss_buffer_desc / *gss_buffer_t: hold/point-to size_t .length & void *value
+  -- all strings/etc passed in should go through one of these
+  -- when allocated by gssapi, release with gss_release_buffer()
+*/
+
+int
+auth_heimdal_gssapi_server(auth_instance *ablock, uschar *initial_data)
+{
+  gss_name_t gclient = GSS_C_NO_NAME;
+  gss_name_t gserver = GSS_C_NO_NAME;
+  gss_cred_id_t gcred = GSS_C_NO_CREDENTIAL;
+  gss_ctx_id_t gcontext = GSS_C_NO_CONTEXT;
+  uschar *ex_server_str;
+  gss_buffer_desc gbufdesc = GSS_C_EMPTY_BUFFER;
+  gss_buffer_desc gbufdesc_in = GSS_C_EMPTY_BUFFER;
+  gss_buffer_desc gbufdesc_out = GSS_C_EMPTY_BUFFER;
+  gss_OID mech_type;
+  OM_uint32 maj_stat, min_stat;
+  int step, error_out, i;
+  uschar *tmp1, *tmp2, *from_client;
+  auth_heimdal_gssapi_options_block *ob =
+    (auth_heimdal_gssapi_options_block *)(ablock->options_block);
+  BOOL handled_empty_ir;
+  uschar *store_reset_point;
+  uschar *keytab;
+  uschar sasl_config[4];
+  uschar requested_qop;
+
+  store_reset_point = store_get(0);
+
+  HDEBUG(D_auth)
+    debug_printf("heimdal: initialising auth context for %s\n", ablock->name);
+
+  /* Construct our gss_name_t gserver describing ourselves */
+  tmp1 = expand_string(ob->server_service);
+  tmp2 = expand_string(ob->server_hostname);
+  ex_server_str = string_sprintf("%s@%s", tmp1, tmp2);
+  gbufdesc.value = (void *) ex_server_str;
+  gbufdesc.length = Ustrlen(ex_server_str);
+  maj_stat = gss_import_name(&min_stat,
+      &gbufdesc, GSS_C_NT_HOSTBASED_SERVICE, &gserver);
+  if (GSS_ERROR(maj_stat))
+    return exim_gssapi_error_defer(store_reset_point, maj_stat, min_stat,
+        "gss_import_name(%s)", CS gbufdesc.value);
+
+  /* Use a specific keytab, if specified */
+  if (ob->server_keytab) {
+    keytab = expand_string(ob->server_keytab);
+    maj_stat = gsskrb5_register_acceptor_identity(CCS keytab);
+    if (GSS_ERROR(maj_stat))
+      return exim_gssapi_error_defer(store_reset_point, maj_stat, min_stat,
+          "registering keytab \"%s\"", keytab);
+    HDEBUG(D_auth)
+      debug_printf("heimdal: using keytab \"%s\"\n", keytab);
+  }
+
+  /* Acquire our credentials */
+  maj_stat = gss_acquire_cred(&min_stat,
+      gserver,             /* desired name */
+      0,                   /* time */
+      GSS_C_NULL_OID_SET,  /* desired mechs */
+      GSS_C_ACCEPT,        /* cred usage */
+      &gcred,              /* handle */
+      NULL                 /* actual mechs */,
+      NULL                 /* time rec */);
+  if (GSS_ERROR(maj_stat))
+    return exim_gssapi_error_defer(store_reset_point, maj_stat, min_stat,
+        "gss_acquire_cred(%s)", ex_server_str);
+
+  maj_stat = gss_release_name(&min_stat, &gserver);
+
+  HDEBUG(D_auth) debug_printf("heimdal: have server credentials.\n");
+
+  /* Loop talking to client */
+  step = 0;
+  from_client = initial_data;
+  handled_empty_ir = FALSE;
+  error_out = OK;
+
+  /* buffer sizes: auth_get_data() uses big_buffer, which we grow per
+  GSSAPI RFC in _init, if needed, to meet the SHOULD size of 64KB.
+  (big_buffer starts life at the MUST size of 16KB). */
+
+  /* step values
+  0: getting initial data from client to feed into GSSAPI
+  1: iterating for as long as GSS_S_CONTINUE_NEEDED
+  2: GSS_S_COMPLETE, SASL wrapping for authz and qop to send to client
+  3: unpick final auth message from client
+  4: break/finish (non-step)
+  */
+  while (step < 4) {
+    switch (step) {
+      case 0:
+        if (!from_client || *from_client == '\0') {
+          if (handled_empty_ir) {
+            HDEBUG(D_auth) debug_printf("gssapi: repeated empty input, grr.\n");
+            error_out = BAD64;
+            goto ERROR_OUT;
+          } else {
+            HDEBUG(D_auth) debug_printf("gssapi: missing initial response, nudging.\n");
+            error_out = auth_get_data(&from_client, US"", 0);
+            if (error_out != OK)
+              goto ERROR_OUT;
+            handled_empty_ir = TRUE;
+            continue;
+          }
+        }
+        /* We should now have the opening data from the client, base64-encoded. */
+        step += 1;
+        HDEBUG(D_auth) debug_printf("heimdal: have initial client data\n");
+        break;
+
+      case 1:
+        gbufdesc_in.length = auth_b64decode(from_client, USS &gbufdesc_in.value);
+        if (gclient) {
+          maj_stat = gss_release_name(&min_stat, &gclient);
+          gclient = GSS_C_NO_NAME;
+        }
+        maj_stat = gss_accept_sec_context(&min_stat,
+            &gcontext,          /* context handle */
+            gcred,              /* acceptor cred handle */
+            &gbufdesc_in,       /* input from client */
+            GSS_C_NO_CHANNEL_BINDINGS,  /* XXX fixme: use the channel bindings from GnuTLS */
+            &gclient,           /* client identifier */
+            &mech_type,         /* mechanism in use */
+            &gbufdesc_out,      /* output to send to client */
+            NULL,               /* return flags */
+            NULL,               /* time rec */
+            NULL                /* delegated cred_handle */
+            );
+        if (GSS_ERROR(maj_stat)) {
+          exim_gssapi_error_defer(NULL, maj_stat, min_stat,
+              "gss_accept_sec_context()");
+          error_out = FAIL;
+          goto ERROR_OUT;
+        }
+        if (&gbufdesc_out.length != 0) {
+          error_out = auth_get_data(&from_client,
+              gbufdesc_out.value, gbufdesc_out.length);
+          if (error_out != OK)
+            goto ERROR_OUT;
+
+          gss_release_buffer(&min_stat, &gbufdesc_out);
+          EmptyBuf(gbufdesc_out);
+        }
+        if (maj_stat == GSS_S_COMPLETE) {
+          step += 1;
+          HDEBUG(D_auth) debug_printf("heimdal: GSS complete\n");
+        } else {
+          HDEBUG(D_auth) debug_printf("heimdal: need more data\n");
+        }
+        break;
+
+      case 2:
+        memset(sasl_config, 0xFF, 4);
+        /* draft-ietf-sasl-gssapi-06.txt defines bitmasks for first octet
+        0x01 No security layer
+        0x02 Integrity protection
+        0x04 Confidentiality protection
+
+        The remaining three octets are the maximum buffer size for wrapped
+        content. */
+        sasl_config[0] = 0x01;  /* Exim does not wrap/unwrap SASL layers after auth */
+        gbufdesc.value = (void *) sasl_config;
+        gbufdesc.length = 4;
+        maj_stat = gss_wrap(&min_stat,
+            gcontext,
+            0,                    /* conf_req_flag: integrity only */
+            GSS_C_QOP_DEFAULT,    /* qop requested */
+            &gbufdesc,            /* message to protect */
+            NULL,                 /* conf_state: no confidentiality applied */
+            &gbufdesc_out         /* output buffer */
+            );
+        if (GSS_ERROR(maj_stat)) {
+          exim_gssapi_error_defer(NULL, maj_stat, min_stat,
+              "gss_wrap(SASL state after auth)");
+          error_out = FAIL;
+          goto ERROR_OUT;
+        }
+
+        HDEBUG(D_auth) debug_printf("heimdal SASL: requesting QOP with no security layers\n");
+
+        error_out = auth_get_data(&from_client,
+            gbufdesc_out.value, gbufdesc_out.length);
+        if (error_out != OK)
+          goto ERROR_OUT;
+
+        gss_release_buffer(&min_stat, &gbufdesc_out);
+        EmptyBuf(gbufdesc_out);
+        step += 1;
+        break;
+
+      case 3:
+        gbufdesc_in.length = auth_b64decode(from_client, USS &gbufdesc_in.value);
+        maj_stat = gss_unwrap(&min_stat,
+            gcontext,
+            &gbufdesc_in,       /* data from client */
+            &gbufdesc_out,      /* results */
+            NULL,               /* conf state */
+            NULL                /* qop state */
+            );
+        if (GSS_ERROR(maj_stat)) {
+          exim_gssapi_error_defer(NULL, maj_stat, min_stat,
+              "gss_unwrap(final SASL message from client)");
+          error_out = FAIL;
+          goto ERROR_OUT;
+        }
+        if (gbufdesc_out.length < 4) {
+          HDEBUG(D_auth)
+            debug_printf("gssapi: final message too short; "
+                "need flags, buf sizes and optional authzid\n");
+          error_out = FAIL;
+          goto ERROR_OUT;
+        }
+
+        requested_qop = (CS gbufdesc_out.value)[0];
+        if ((requested_qop & 0x01) == 0) {
+          HDEBUG(D_auth)
+            debug_printf("gssapi: client requested security layers (%x)\n",
+                (unsigned int) requested_qop);
+          error_out = FAIL;
+          goto ERROR_OUT;
+        }
+
+        for (i = 0; i < AUTH_VARS; i++) auth_vars[i] = NULL;
+        expand_nmax = 0;
+
+        /* Identifiers:
+        The SASL provided identifier is an unverified authzid.
+        GSSAPI provides us with a verified identifier, but it might be empty
+        for some clients.
+        */
+
+        /* $auth2 is authzid requested at SASL layer */
+        if (gbufdesc_out.length > 4) {
+          expand_nlength[2] = gbufdesc_out.length - 4;
+          auth_vars[1] = expand_nstring[2] =
+            string_copyn((US gbufdesc_out.value) + 4, expand_nlength[2]);
+          expand_nmax = 2;
+        }
+
+        gss_release_buffer(&min_stat, &gbufdesc_out);
+        EmptyBuf(gbufdesc_out);
+
+        /* $auth1 is GSSAPI display name */
+        maj_stat = gss_display_name(&min_stat,
+            gclient,
+            &gbufdesc_out,
+            &mech_type);
+        if (GSS_ERROR(maj_stat)) {
+          auth_vars[1] = expand_nstring[2] = NULL;
+          expand_nmax = 0;
+          exim_gssapi_error_defer(NULL, maj_stat, min_stat,
+              "gss_display_name(client identifier)");
+          error_out = FAIL;
+          goto ERROR_OUT;
+        }
+
+        expand_nlength[1] = gbufdesc_out.length;
+        auth_vars[0] = expand_nstring[1] =
+          string_copyn(gbufdesc_out.value, gbufdesc_out.length);
+
+        if (expand_nmax == 0) { /* should be: authzid was empty */
+          expand_nmax = 2;
+          expand_nlength[2] = expand_nlength[1];
+          auth_vars[1] = expand_nstring[2] = string_copyn(expand_nstring[1], expand_nlength[1]);
+          HDEBUG(D_auth)
+            debug_printf("heimdal SASL: empty authzid, set to dup of GSSAPI display name\n");
+        }
+
+        HDEBUG(D_auth)
+          debug_printf("heimdal SASL: happy with client request\n"
+             "  auth1 (verified GSSAPI display-name): \"%s\"\n"
+             "  auth2 (unverified SASL requested authzid): \"%s\"\n",
+             auth_vars[0], auth_vars[1]);
+
+        step += 1;
+        break;
+
+    } /* switch */
+  } /* while step */
+
+
+ERROR_OUT:
+  maj_stat = gss_release_cred(&min_stat, &gcred);
+  if (gclient) {
+    gss_release_name(&min_stat, &gclient);
+    gclient = GSS_C_NO_NAME;
+  }
+  if (gbufdesc_out.length) {
+    gss_release_buffer(&min_stat, &gbufdesc_out);
+    EmptyBuf(gbufdesc_out);
+  }
+  if (gcontext != GSS_C_NO_CONTEXT) {
+    gss_delete_sec_context(&min_stat, &gcontext, GSS_C_NO_BUFFER);
+  }
+
+  store_reset(store_reset_point);
+
+  if (error_out != OK)
+    return error_out;
+
+  /* Auth succeeded, check server_condition */
+  return auth_check_serv_cond(ablock);
+}
+
+
+static int
+exim_gssapi_error_defer(uschar *store_reset_point,
+    OM_uint32 major, OM_uint32 minor,
+    const char *format, ...)
+{
+  va_list ap;
+  uschar buffer[STRING_SPRINTF_BUFFER_SIZE];
+  OM_uint32 maj_stat, min_stat;
+  OM_uint32 msgcontext = 0;
+  gss_buffer_desc status_string;
+
+  va_start(ap, format);
+  if (!string_vformat(buffer, sizeof(buffer), format, ap))
+    log_write(0, LOG_MAIN|LOG_PANIC_DIE,
+        "exim_gssapi_error_defer expansion larger than %lu",
+        sizeof(buffer));
+  va_end(ap);
+
+  auth_defer_msg = NULL;
+
+  do {
+    maj_stat = gss_display_status(&min_stat,
+        major, GSS_C_GSS_CODE, GSS_C_NO_OID,
+        &msgcontext, &status_string);
+
+    if (auth_defer_msg == NULL) {
+      auth_defer_msg = string_copy(US status_string.value);
+    }
+
+    HDEBUG(D_auth) debug_printf("heimdal %s: %.*s\n",
+        buffer, (int)status_string.length, CS status_string.value);
+    gss_release_buffer(&min_stat, &status_string);
+
+  } while (msgcontext != 0);
+
+  if (store_reset_point)
+    store_reset(store_reset_point);
+  return DEFER;
+}
+
+
+/*************************************************
+*              Client entry point                *
+*************************************************/
+
+/* For interface, see auths/README */
+
+int
+auth_heimdal_gssapi_client(
+  auth_instance *ablock,                 /* authenticator block */
+  smtp_inblock *inblock,                 /* connection inblock */
+  smtp_outblock *outblock,               /* connection outblock */
+  int timeout,                           /* command timeout */
+  uschar *buffer,                        /* buffer for reading response */
+  int buffsize)                          /* size of buffer */
+{
+  HDEBUG(D_auth)
+    debug_printf("Client side NOT IMPLEMENTED: you should not see this!\n");
+  /* NOT IMPLEMENTED */
+  return FAIL;
+}
+
+/*************************************************
+*                Diagnostic API                  *
+*************************************************/
+
+void
+auth_heimdal_gssapi_version_report(FILE *f)
+{
+  /* No build-time constants available unless we link against libraries at
+  build-time and export the result as a string into a header ourselves. */
+  fprintf(f, "Library version: Heimdal: Runtime: %s\n"
+             " Build Info: %s\n",
+          heimdal_version, heimdal_long_version);
+}
+
+#endif  /* AUTH_HEIMDAL_GSSAPI */
+
+/* End of heimdal_gssapi.c */
diff --git a/src/auths/heimdal_gssapi.h b/src/auths/heimdal_gssapi.h
new file mode 100644 (file)
index 0000000..a606a5c
--- /dev/null
@@ -0,0 +1,39 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2012 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Copyright (c) Twitter Inc 2012
+   Author: Phil Pennock <pdp@exim.org> */
+/* Copyright (c) Phil Pennock 2012 */
+
+/* Interface to Heimdal library for GSSAPI authentication. */
+
+/* Authenticator-specific options. */
+
+typedef struct {
+  uschar *server_hostname;
+  uschar *server_keytab;
+  uschar *server_service;
+} auth_heimdal_gssapi_options_block;
+
+/* Data for reading the authenticator-specific options. */
+
+extern optionlist auth_heimdal_gssapi_options[];
+extern int auth_heimdal_gssapi_options_count;
+
+/* Defaults for the authenticator-specific options. */
+
+extern auth_heimdal_gssapi_options_block auth_heimdal_gssapi_option_defaults;
+
+/* The entry points for the mechanism */
+
+extern void auth_heimdal_gssapi_init(auth_instance *);
+extern int auth_heimdal_gssapi_server(auth_instance *, uschar *);
+extern int auth_heimdal_gssapi_client(auth_instance *, smtp_inblock *,
+                                smtp_outblock *, int, uschar *, int);
+extern void auth_heimdal_gssapi_version_report(FILE *f);
+
+/* End of heimdal_gssapi.h */
diff --git a/src/auths/md5.c b/src/auths/md5.c
new file mode 100644 (file)
index 0000000..25655e9
--- /dev/null
@@ -0,0 +1,357 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+#ifndef STAND_ALONE
+#include "../exim.h"
+
+/* For stand-alone testing, we need to have the structure defined, and
+to be able to do I/O */
+
+#else
+#include <stdio.h>
+#include "../mytypes.h"
+typedef struct md5 {
+  unsigned int length;
+  unsigned int abcd[4];
+  }
+md5;
+#endif
+
+
+
+/*************************************************
+*        Start off a new MD5 computation.        *
+*************************************************/
+
+/*
+Argument:  pointer to md5 storage structure
+Returns:   nothing
+*/
+
+void
+md5_start(md5 *base)
+{
+base->abcd[0] = 0x67452301;
+base->abcd[1] = 0xefcdab89;
+base->abcd[2] = 0x98badcfe;
+base->abcd[3] = 0x10325476;
+base->length = 0;
+}
+
+
+
+/*************************************************
+*       Process another 64-byte block            *
+*************************************************/
+
+/* This function implements central part of the algorithm which is described
+in RFC 1321.
+
+Arguments:
+  base       pointer to md5 storage structure
+  text       pointer to next 64 bytes of subject text
+
+Returns:     nothing
+*/
+
+void
+md5_mid(md5 *base, const uschar *text)
+{
+register unsigned int a = base->abcd[0];
+register unsigned int b = base->abcd[1];
+register unsigned int c = base->abcd[2];
+register unsigned int d = base->abcd[3];
+int i;
+unsigned int X[16];
+base->length += 64;
+
+/* Load the 64 bytes into a set of working integers, treating them as 32-bit
+numbers in little-endian order. */
+
+for (i = 0; i < 16; i++)
+  {
+  X[i] = (unsigned int)(text[0]) |
+         ((unsigned int)(text[1]) << 8) |
+         ((unsigned int)(text[2]) << 16) |
+         ((unsigned int)(text[3]) << 24);
+  text += 4;
+  }
+
+/* For each round of processing there is a function to be applied. We define it
+as a macro each time round. */
+
+/***********************************************
+*                    Round 1                   *
+*            F(X,Y,Z) = XY v not(X) Z          *
+* a = b + ((a + F(b,c,d) + X[k] + T[i]) <<< s) *
+***********************************************/
+
+#define OP(a, b, c, d, k, s, ti) \
+     a += ((b & c) | (~b & d)) + X[k] + (unsigned int)ti; \
+     a = b + ((a << s) | (a >> (32 - s)))
+
+OP(a, b, c, d,  0,  7, 0xd76aa478);
+OP(d, a, b, c,  1, 12, 0xe8c7b756);
+OP(c, d, a, b,  2, 17, 0x242070db);
+OP(b, c, d, a,  3, 22, 0xc1bdceee);
+OP(a, b, c, d,  4,  7, 0xf57c0faf);
+OP(d, a, b, c,  5, 12, 0x4787c62a);
+OP(c, d, a, b,  6, 17, 0xa8304613);
+OP(b, c, d, a,  7, 22, 0xfd469501);
+OP(a, b, c, d,  8,  7, 0x698098d8);
+OP(d, a, b, c,  9, 12, 0x8b44f7af);
+OP(c, d, a, b, 10, 17, 0xffff5bb1);
+OP(b, c, d, a, 11, 22, 0x895cd7be);
+OP(a, b, c, d, 12,  7, 0x6b901122);
+OP(d, a, b, c, 13, 12, 0xfd987193);
+OP(c, d, a, b, 14, 17, 0xa679438e);
+OP(b, c, d, a, 15, 22, 0x49b40821);
+
+#undef OP
+
+/***********************************************
+*                    Round 2                   *
+*            F(X,Y,Z) = XZ v Y not(Z)          *
+* a = b + ((a + F(b,c,d) + X[k] + T[i]) <<< s) *
+***********************************************/
+
+#define OP(a, b, c, d, k, s, ti) \
+     a += ((b & d) | (c & ~d)) + X[k] + (unsigned int)ti; \
+     a = b + ((a << s) | (a >> (32 - s)))
+
+OP(a, b, c, d,  1,  5, 0xf61e2562);
+OP(d, a, b, c,  6,  9, 0xc040b340);
+OP(c, d, a, b, 11, 14, 0x265e5a51);
+OP(b, c, d, a,  0, 20, 0xe9b6c7aa);
+OP(a, b, c, d,  5,  5, 0xd62f105d);
+OP(d, a, b, c, 10,  9, 0x02441453);
+OP(c, d, a, b, 15, 14, 0xd8a1e681);
+OP(b, c, d, a,  4, 20, 0xe7d3fbc8);
+OP(a, b, c, d,  9,  5, 0x21e1cde6);
+OP(d, a, b, c, 14,  9, 0xc33707d6);
+OP(c, d, a, b,  3, 14, 0xf4d50d87);
+OP(b, c, d, a,  8, 20, 0x455a14ed);
+OP(a, b, c, d, 13,  5, 0xa9e3e905);
+OP(d, a, b, c,  2,  9, 0xfcefa3f8);
+OP(c, d, a, b,  7, 14, 0x676f02d9);
+OP(b, c, d, a, 12, 20, 0x8d2a4c8a);
+
+#undef OP
+
+/***********************************************
+*                    Round 3                   *
+*            F(X,Y,Z) = X xor Y xor Z          *
+* a = b + ((a + F(b,c,d) + X[k] + T[i]) <<< s) *
+***********************************************/
+
+#define OP(a, b, c, d, k, s, ti) \
+     a += (b ^ c ^ d) + X[k] + (unsigned int)ti; \
+     a = b + ((a << s) | (a >> (32 - s)))
+
+OP(a, b, c, d,  5,  4, 0xfffa3942);
+OP(d, a, b, c,  8, 11, 0x8771f681);
+OP(c, d, a, b, 11, 16, 0x6d9d6122);
+OP(b, c, d, a, 14, 23, 0xfde5380c);
+OP(a, b, c, d,  1,  4, 0xa4beea44);
+OP(d, a, b, c,  4, 11, 0x4bdecfa9);
+OP(c, d, a, b,  7, 16, 0xf6bb4b60);
+OP(b, c, d, a, 10, 23, 0xbebfbc70);
+OP(a, b, c, d, 13,  4, 0x289b7ec6);
+OP(d, a, b, c,  0, 11, 0xeaa127fa);
+OP(c, d, a, b,  3, 16, 0xd4ef3085);
+OP(b, c, d, a,  6, 23, 0x04881d05);
+OP(a, b, c, d,  9,  4, 0xd9d4d039);
+OP(d, a, b, c, 12, 11, 0xe6db99e5);
+OP(c, d, a, b, 15, 16, 0x1fa27cf8);
+OP(b, c, d, a,  2, 23, 0xc4ac5665);
+
+#undef OP
+
+/***********************************************
+*                    Round 4                   *
+*          F(X,Y,Z) = Y xor (X v not(Z))       *
+* a = b + ((a + F(b,c,d) + X[k] + T[i]) <<< s) *
+***********************************************/
+
+#define OP(a, b, c, d, k, s, ti) \
+     a += (c ^ (b | ~d)) + X[k] + (unsigned int)ti; \
+     a = b + ((a << s) | (a >> (32 - s)))
+
+OP(a, b, c, d,  0,  6, 0xf4292244);
+OP(d, a, b, c,  7, 10, 0x432aff97);
+OP(c, d, a, b, 14, 15, 0xab9423a7);
+OP(b, c, d, a,  5, 21, 0xfc93a039);
+OP(a, b, c, d, 12,  6, 0x655b59c3);
+OP(d, a, b, c,  3, 10, 0x8f0ccc92);
+OP(c, d, a, b, 10, 15, 0xffeff47d);
+OP(b, c, d, a,  1, 21, 0x85845dd1);
+OP(a, b, c, d,  8,  6, 0x6fa87e4f);
+OP(d, a, b, c, 15, 10, 0xfe2ce6e0);
+OP(c, d, a, b,  6, 15, 0xa3014314);
+OP(b, c, d, a, 13, 21, 0x4e0811a1);
+OP(a, b, c, d,  4,  6, 0xf7537e82);
+OP(d, a, b, c, 11, 10, 0xbd3af235);
+OP(c, d, a, b,  2, 15, 0x2ad7d2bb);
+OP(b, c, d, a,  9, 21, 0xeb86d391);
+
+#undef OP
+
+/* Add the new values back into the accumulators. */
+
+base->abcd[0] += a;
+base->abcd[1] += b;
+base->abcd[2] += c;
+base->abcd[3] += d;
+}
+
+
+
+
+/*************************************************
+*     Process the final text string              *
+*************************************************/
+
+/* The string may be of any length. It is padded out according to the rules
+for computing MD5 digests. The final result is then converted to text form
+and returned.
+
+Arguments:
+  base      pointer to the md5 storage structure
+  text      pointer to the final text vector
+  length    length of the final text vector
+  digest    points to 16 bytes in which to place the result
+
+Returns:    nothing
+*/
+
+void
+md5_end(md5 *base, const uschar *text, int length, uschar *digest)
+{
+int i;
+uschar work[64];
+
+/* Process in chunks of 64 until we have less than 64 bytes left. */
+
+while (length >= 64)
+  {
+  md5_mid(base, text);
+  text += 64;
+  length -= 64;
+  }
+
+/* If the remaining string contains more than 55 bytes, we must pad it
+out to 64, process it, and then set up the final chunk as 56 bytes of
+padding. If it has less than 56 bytes, we pad it out to 56 bytes as the
+final chunk. */
+
+memcpy(work, text, length);
+work[length] = 0x80;
+
+if (length > 55)
+  {
+  memset(work+length+1, 0, 63-length);
+  md5_mid(base, work);
+  base->length -= 64;
+  memset(work, 0, 56);
+  }
+else
+  {
+  memset(work+length+1, 0, 55-length);
+  }
+
+/* The final 8 bytes of the final chunk are a 64-bit representation of the
+length of the input string *bits*, before padding, low order word first, and
+low order bytes first in each word. This implementation is designed for short
+strings, and so operates with a single int counter only. */
+
+length += base->length;   /* Total length in bytes */
+length <<= 3;             /* Total length in bits */
+
+work[56] = length         & 0xff;
+work[57] = (length >>  8) & 0xff;
+work[58] = (length >> 16) & 0xff;
+work[59] = (length >> 24) & 0xff;
+
+memset(work+60, 0, 4);
+
+/* Process the final 64-byte chunk */
+
+md5_mid(base, work);
+
+/* Pass back the result, low-order byte first in each word. */
+
+for (i = 0; i < 4; i++)
+  {
+  register int x = base->abcd[i];
+  *digest++ =  x        & 0xff;
+  *digest++ = (x >>  8) & 0xff;
+  *digest++ = (x >> 16) & 0xff;
+  *digest++ = (x >> 24) & 0xff;
+  }
+}
+
+
+
+/*************************************************
+**************************************************
+*             Stand-alone test program           *
+**************************************************
+*************************************************/
+
+#if defined STAND_ALONE & !defined CRAM_STAND_ALONE
+
+/* Test values */
+
+static uschar *tests[] = {
+  "", "d41d8cd98f00b204e9800998ecf8427e",
+
+  "a", "0cc175b9c0f1b6a831c399e269772661",
+
+  "abc", "900150983cd24fb0d6963f7d28e17f72",
+
+  "message digest", "f96b697d7cb7938d525a2f31aaf161d0",
+
+  "abcdefghijklmnopqrstuvwxyz", "c3fcd3d76192e4007dfb496cca67e13b",
+
+  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
+     "d174ab98d277d9f5a5611c2c9f419d9f",
+
+  "1234567890123456789012345678901234567890123456789012345678901234567890"
+  "1234567890",
+    "57edf4a22be3c955ac49da2e2107b67a",
+
+  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
+  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
+  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
+    "a0842fcc02167127b0bb9a7c38e71ba8"
+};
+
+int main(void)
+{
+md5 base;
+int i = 0x01020304;
+uschar *ctest = (uschar *)(&i);
+uschar buffer[256];
+uschar digest[16];
+printf("Checking md5: %s-endian\n", (ctest[0] == 0x04)? "little" : "big");
+
+for (i = 0; i < sizeof(tests)/sizeof(uschar *); i += 2)
+  {
+  int j;
+  uschar s[33];
+  printf("%s\nShould be: %s\n", tests[i], tests[i+1]);
+  md5_start(&base);
+  md5_end(&base, tests[i], strlen(tests[i]), digest);
+  for (j = 0; j < 16; j++) sprintf(s+2*j, "%02x", digest[j]);
+  printf("Computed:  %s\n", s);
+  if (strcmp(s, tests[i+1]) != 0) printf("*** No match ***\n");
+  printf("\n");
+  }
+}
+#endif
+
+/* End of md5.c */
diff --git a/src/auths/plaintext.c b/src/auths/plaintext.c
new file mode 100644 (file)
index 0000000..ff449e5
--- /dev/null
@@ -0,0 +1,299 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+#include "../exim.h"
+#include "plaintext.h"
+
+
+/* Options specific to the plaintext authentication mechanism. */
+
+optionlist auth_plaintext_options[] = {
+  { "client_ignore_invalid_base64", opt_bool,
+      (void *)(offsetof(auth_plaintext_options_block, client_ignore_invalid_base64)) },
+  { "client_send",        opt_stringptr,
+      (void *)(offsetof(auth_plaintext_options_block, client_send)) },
+  { "server_prompts",     opt_stringptr,
+      (void *)(offsetof(auth_plaintext_options_block, server_prompts)) }
+};
+
+/* Size of the options list. An extern variable has to be used so that its
+address can appear in the tables drtables.c. */
+
+int auth_plaintext_options_count =
+  sizeof(auth_plaintext_options)/sizeof(optionlist);
+
+/* Default private options block for the plaintext authentication method. */
+
+auth_plaintext_options_block auth_plaintext_option_defaults = {
+  NULL,              /* server_prompts */
+  NULL,              /* client_send */
+  FALSE              /* client_ignore_invalid_base64 */
+};
+
+
+/*************************************************
+*          Initialization entry point            *
+*************************************************/
+
+/* Called for each instance, after its options have been read, to
+enable consistency checks to be done, or anything else that needs
+to be set up. */
+
+void
+auth_plaintext_init(auth_instance *ablock)
+{
+auth_plaintext_options_block *ob =
+  (auth_plaintext_options_block *)(ablock->options_block);
+if (ablock->public_name == NULL) ablock->public_name = ablock->name;
+if (ablock->server_condition != NULL) ablock->server = TRUE;
+if (ob->client_send != NULL) ablock->client = TRUE;
+}
+
+
+
+/*************************************************
+*             Server entry point                 *
+*************************************************/
+
+/* For interface, see auths/README */
+
+int
+auth_plaintext_server(auth_instance *ablock, uschar *data)
+{
+auth_plaintext_options_block *ob =
+  (auth_plaintext_options_block *)(ablock->options_block);
+uschar *prompts = ob->server_prompts;
+uschar *clear, *end, *s;
+int number = 1;
+int len, rc;
+int sep = 0;
+
+/* Expand a non-empty list of prompt strings */
+
+if (prompts != NULL)
+  {
+  prompts = expand_string(prompts);
+  if (prompts == NULL)
+    {
+    auth_defer_msg = expand_string_message;
+    return DEFER;
+    }
+  }
+
+/* If data was supplied on the AUTH command, decode it, and split it up into
+multiple items at binary zeros. The strings are put into $auth1, $auth2, etc,
+up to a maximum. To retain backwards compatibility, they are also put int $1,
+$2, etc. If the data consists of the string "=" it indicates a single, empty
+string. */
+
+if (*data != 0)
+  {
+  if (Ustrcmp(data, "=") == 0)
+    {
+    auth_vars[0] = expand_nstring[++expand_nmax] = US"";
+    expand_nlength[expand_nmax] = 0;
+    }
+  else
+    {
+    if ((len = auth_b64decode(data, &clear)) < 0) return BAD64;
+    end = clear + len;
+    while (clear < end && expand_nmax < EXPAND_MAXN)
+      {
+      if (expand_nmax < AUTH_VARS) auth_vars[expand_nmax] = clear;
+      expand_nstring[++expand_nmax] = clear;
+      while (*clear != 0) clear++;
+      expand_nlength[expand_nmax] = clear++ - expand_nstring[expand_nmax];
+      }
+    }
+  }
+
+/* Now go through the list of prompt strings. Skip over any whose data has
+already been provided as part of the AUTH command. For the rest, send them
+out as prompts, and get a data item back. If the data item is "*", abandon the
+authentication attempt. Otherwise, split it into items as above. */
+
+while ((s = string_nextinlist(&prompts, &sep, big_buffer, big_buffer_size))
+        != NULL && expand_nmax < EXPAND_MAXN)
+  {
+  if (number++ <= expand_nmax) continue;
+  if ((rc = auth_get_data(&data, s, Ustrlen(s))) != OK) return rc;
+  if ((len = auth_b64decode(data, &clear)) < 0) return BAD64;
+  end = clear + len;
+
+  /* This loop must run at least once, in case the length is zero */
+  do
+    {
+    if (expand_nmax < AUTH_VARS) auth_vars[expand_nmax] = clear;
+    expand_nstring[++expand_nmax] = clear;
+    while (*clear != 0) clear++;
+    expand_nlength[expand_nmax] = clear++ - expand_nstring[expand_nmax];
+    }
+  while (clear < end && expand_nmax < EXPAND_MAXN);
+  }
+
+/* We now have a number of items of data in $auth1, $auth2, etc (and also, for
+compatibility, in $1, $2, etc). Authentication and authorization are handled
+together for this authenticator by expanding the server_condition option. Note
+that ablock->server_condition is always non-NULL because that's what configures
+this authenticator as a server. */
+
+return auth_check_serv_cond(ablock);
+}
+
+
+
+/*************************************************
+*              Client entry point                *
+*************************************************/
+
+/* For interface, see auths/README */
+
+int
+auth_plaintext_client(
+  auth_instance *ablock,                 /* authenticator block */
+  smtp_inblock *inblock,                 /* connection inblock */
+  smtp_outblock *outblock,               /* connection outblock */
+  int timeout,                           /* command timeout */
+  uschar *buffer,                        /* buffer for reading response */
+  int buffsize)                          /* size of buffer */
+{
+auth_plaintext_options_block *ob =
+  (auth_plaintext_options_block *)(ablock->options_block);
+uschar *text = ob->client_send;
+uschar *s;
+BOOL first = TRUE;
+int sep = 0;
+int auth_var_idx = 0;
+
+/* The text is broken up into a number of different data items, which are
+sent one by one. The first one is sent with the AUTH command; the remainder are
+sent in response to subsequent prompts. Each is expanded before being sent. */
+
+while ((s = string_nextinlist(&text, &sep, big_buffer, big_buffer_size)) != NULL)
+  {
+  int i, len, clear_len;
+  uschar *ss = expand_string(s);
+  uschar *clear;
+
+  /* Forced expansion failure is not an error; authentication is abandoned. On
+  all but the first string, we have to abandon the authentication attempt by
+  sending a line containing "*". Save the failed expansion string, because it
+  is in big_buffer, and that gets used by the sending function. */
+
+  if (ss == NULL)
+    {
+    uschar *ssave = string_copy(s);
+    if (!first)
+      {
+      if (smtp_write_command(outblock, FALSE, "*\r\n") >= 0)
+        (void) smtp_read_response(inblock, US buffer, buffsize, '2', timeout);
+      }
+    if (expand_string_forcedfail)
+      {
+      *buffer = 0;       /* No message */
+      return CANCELLED;
+      }
+    string_format(buffer, buffsize, "expansion of \"%s\" failed in %s "
+      "authenticator: %s", ssave, ablock->name, expand_string_message);
+    return ERROR;
+    }
+
+  len = Ustrlen(ss);
+
+  /* The character ^ is used as an escape for a binary zero character, which is
+  needed for the PLAIN mechanism. It must be doubled if really needed. */
+
+  for (i = 0; i < len; i++)
+    {
+    if (ss[i] == '^')
+      {
+      if (ss[i+1] != '^') ss[i] = 0; else
+        {
+        i++;
+        len--;
+        memmove(ss + i, ss + i + 1, len - i);
+        }
+      }
+    }
+
+  /* The first string is attached to the AUTH command; others are sent
+  unembelished. */
+
+  if (first)
+    {
+    first = FALSE;
+    if (smtp_write_command(outblock, FALSE, "AUTH %s%s%s\r\n",
+         ablock->public_name, (len == 0)? "" : " ",
+         auth_b64encode(ss, len)) < 0)
+      return FAIL_SEND;
+    }
+  else
+    {
+    if (smtp_write_command(outblock, FALSE, "%s\r\n",
+          auth_b64encode(ss, len)) < 0)
+      return FAIL_SEND;
+    }
+
+  /* If we receive a success response from the server, authentication
+  has succeeded. There may be more data to send, but is there any point
+  in provoking an error here? */
+
+  if (smtp_read_response(inblock, US buffer, buffsize, '2', timeout)) return OK;
+
+  /* Not a success response. If errno != 0 there is some kind of transmission
+  error. Otherwise, check the response code in the buffer. If it starts with
+  '3', more data is expected. */
+
+  if (errno != 0 || buffer[0] != '3') return FAIL;
+
+  /* If there is no more data to send, we have to cancel the authentication
+  exchange and return ERROR. */
+
+  if (text == NULL)
+    {
+    if (smtp_write_command(outblock, FALSE, "*\r\n") >= 0)
+      (void)smtp_read_response(inblock, US buffer, buffsize, '2', timeout);
+    string_format(buffer, buffsize, "Too few items in client_send in %s "
+      "authenticator", ablock->name);
+    return ERROR;
+    }
+
+  /* Now that we know we'll continue, we put the received data into $auth<n>,
+  if possible. First, decode it: buffer+4 skips over the SMTP status code. */
+
+  clear_len = auth_b64decode(buffer+4, &clear);
+
+  /* If decoding failed, the default is to terminate the authentication, and
+  return FAIL, with the SMTP response still in the buffer. However, if client_
+  ignore_invalid_base64 is set, we ignore the error, and put an empty string
+  into $auth<n>. */
+
+  if (clear_len < 0)
+    {
+    uschar *save_bad = string_copy(buffer);
+    if (!ob->client_ignore_invalid_base64)
+      {
+      if (smtp_write_command(outblock, FALSE, "*\r\n") >= 0)
+        (void)smtp_read_response(inblock, US buffer, buffsize, '2', timeout);
+      string_format(buffer, buffsize, "Invalid base64 string in server "
+        "response \"%s\"", save_bad);
+      return CANCELLED;
+      }
+    clear = US"";
+    clear_len = 0;
+    }
+
+  if (auth_var_idx < AUTH_VARS)
+    auth_vars[auth_var_idx++] = string_copy(clear);
+  }
+
+/* Control should never actually get here. */
+
+return FAIL;
+}
+
+/* End of plaintext.c */
diff --git a/src/auths/plaintext.h b/src/auths/plaintext.h
new file mode 100644 (file)
index 0000000..6cf3522
--- /dev/null
@@ -0,0 +1,32 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Private structure for the private options. */
+
+typedef struct {
+  uschar *server_prompts;
+  uschar *client_send;
+  BOOL    client_ignore_invalid_base64;
+} auth_plaintext_options_block;
+
+/* Data for reading the private options. */
+
+extern optionlist auth_plaintext_options[];
+extern int auth_plaintext_options_count;
+
+/* Block containing default values. */
+
+extern auth_plaintext_options_block auth_plaintext_option_defaults;
+
+/* The entry points for the mechanism */
+
+extern void auth_plaintext_init(auth_instance *);
+extern int auth_plaintext_server(auth_instance *, uschar *);
+extern int auth_plaintext_client(auth_instance *, smtp_inblock *,
+                                 smtp_outblock *, int, uschar *, int);
+
+/* End of plaintext.h */
diff --git a/src/auths/pwcheck.c b/src/auths/pwcheck.c
new file mode 100644 (file)
index 0000000..645265d
--- /dev/null
@@ -0,0 +1,453 @@
+/* SASL server API implementation
+ * Rob Siemborski
+ * Tim Martin
+ * $Id: checkpw.c,v 1.49 2002/03/07 19:14:04 ken3 Exp $
+ */
+/*
+ * Copyright (c) 2001 Carnegie Mellon University.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ *
+ * 3. The name "Carnegie Mellon University" must not be used to
+ *    endorse or promote products derived from this software without
+ *    prior written permission. For permission or any other legal
+ *    details, please contact
+ *      Office of Technology Transfer
+ *      Carnegie Mellon University
+ *      5000 Forbes Avenue
+ *      Pittsburgh, PA  15213-3890
+ *      (412) 268-4387, fax: (412) 268-7395
+ *      tech-transfer@andrew.cmu.edu
+ *
+ * 4. Redistributions of any form whatsoever must retain the following
+ *    acknowledgment:
+ *    "This product includes software developed by Computing Services
+ *     at Carnegie Mellon University (http://www.cmu.edu/computing/)."
+ *
+ * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
+ * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+ * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
+ * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
+ * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
+ * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+/*
+ * Taken from Cyrus-SASL library and adapted by Alexander S. Sabourenkov
+ * Oct 2001 - Apr 2002: Slightly modified by Philip Hazel.
+ * Aug 2003: new code for saslauthd from Alexander S. Sabourenkov incorporated
+ *           by Philip Hazel (minor mods to avoid compiler warnings)
+ * Oct 2006: (PH) removed redundant tests on "reply" being NULL - some were
+ *           missing, and confused someone who was using this code for some
+ *           other purpose. Here in Exim, "reply" is never NULL.
+ *
+ * screwdriver@lxnt.info
+ *
+ */
+
+/* Originally this module supported only the pwcheck daemon, which is where its
+name comes from. Nowadays it supports saslauthd as well; pwcheck is in fact
+deprecated. The definitions of CYRUS_PWCHECK_SOCKET and CYRUS_SASLAUTHD_SOCKET
+determine whether the facilities are actually supported or not. */
+
+
+#include "../exim.h"
+#include "pwcheck.h"
+
+
+#if defined(CYRUS_PWCHECK_SOCKET) || defined(CYRUS_SASLAUTHD_SOCKET)
+
+#include <sys/uio.h>
+
+static int retry_read(int, void *, unsigned );
+static int retry_writev(int, struct iovec *, int );
+static int read_string(int, uschar **);
+static int write_string(int, const uschar *, int);
+
+#endif
+
+
+/* A dummy function that always fails if pwcheck support is not
+wanted. */
+
+#ifndef CYRUS_PWCHECK_SOCKET
+int pwcheck_verify_password(const char *userid,
+                            const char *passwd,
+                            const char **reply)
+{
+userid = userid;  /* Keep picky compilers happy */
+passwd = passwd;
+*reply = "pwcheck support is not included in this Exim binary";
+return PWCHECK_FAIL;
+}
+
+
+/* This is the real function */
+
+#else
+
+ /* taken from cyrus-sasl file checkpw.c */
+ /* pwcheck daemon-authenticated login */
+ int pwcheck_verify_password(const char *userid,
+                                  const char *passwd,
+                                  const char **reply)
+ {
+     int s, start, r, n;
+     struct sockaddr_un srvaddr;
+     struct iovec iov[2];
+     static char response[1024];
+
+     *reply = NULL;
+
+     s = socket(AF_UNIX, SOCK_STREAM, 0);
+     if (s == -1) { return PWCHECK_FAIL; }
+
+     memset((char *)&srvaddr, 0, sizeof(srvaddr));
+     srvaddr.sun_family = AF_UNIX;
+     strncpy(srvaddr.sun_path, CYRUS_PWCHECK_SOCKET, sizeof(srvaddr.sun_path));
+     r = connect(s, (struct sockaddr *)&srvaddr, sizeof(srvaddr));
+     if (r == -1) {
+        DEBUG(D_auth)
+            debug_printf("Cannot connect to pwcheck daemon (at '%s')\n",CYRUS_PWCHECK_SOCKET);
+       *reply = "cannot connect to pwcheck daemon";
+       return PWCHECK_FAIL;
+     }
+
+     iov[0].iov_base = (char *)userid;
+     iov[0].iov_len = strlen(userid)+1;
+     iov[1].iov_base = (char *)passwd;
+     iov[1].iov_len = strlen(passwd)+1;
+
+     retry_writev(s, iov, 2);
+
+     start = 0;
+     while (start < sizeof(response) - 1) {
+       n = read(s, response+start, sizeof(response) - 1 - start);
+       if (n < 1) break;
+       start += n;
+     }
+
+     (void)close(s);
+
+     if (start > 1 && !strncmp(response, "OK", 2)) {
+       return PWCHECK_OK;
+     }
+
+     response[start] = '\0';
+     *reply = response;
+     return PWCHECK_NO;
+ }
+
+#endif
+
+
+
+ /* A dummy function that always fails if saslauthd support is not
+wanted. */
+
+#ifndef CYRUS_SASLAUTHD_SOCKET
+int saslauthd_verify_password(const uschar *userid,
+                const uschar *passwd,
+                const uschar *service,
+                const uschar *realm,
+                const uschar **reply)
+{
+userid = userid;  /* Keep picky compilers happy */
+passwd = passwd;
+service = service;
+realm = realm;
+*reply = US"saslauthd support is not included in this Exim binary";
+return PWCHECK_FAIL;
+}
+
+
+/* This is the real function */
+
+#else
+ /* written from scratch  */
+ /* saslauthd daemon-authenticated login */
+
+int saslauthd_verify_password(const uschar *userid,
+                const uschar *password,
+                const uschar *service,
+                const uschar *realm,
+                const uschar **reply)
+{
+    uschar *daemon_reply = NULL;
+    int s, r;
+    struct sockaddr_un srvaddr;
+
+    DEBUG(D_auth)
+       debug_printf("saslauthd userid='%s' servicename='%s'"
+                    " realm='%s'\n", userid, service, realm );
+
+    *reply = NULL;
+
+    s = socket(AF_UNIX, SOCK_STREAM, 0);
+    if (s == -1) {
+       *reply = CUstrerror(errno);
+       return PWCHECK_FAIL;
+    }
+
+    memset((char *)&srvaddr, 0, sizeof(srvaddr));
+    srvaddr.sun_family = AF_UNIX;
+    strncpy(srvaddr.sun_path, CYRUS_SASLAUTHD_SOCKET,
+            sizeof(srvaddr.sun_path));
+    r = connect(s, (struct sockaddr *)&srvaddr, sizeof(srvaddr));
+    if (r == -1) {
+       DEBUG(D_auth)
+            debug_printf("Cannot connect to saslauthd daemon (at '%s'): %s\n",
+                         CYRUS_SASLAUTHD_SOCKET, strerror(errno));
+       *reply = string_sprintf("cannot connect to saslauthd daemon at "
+                               "%s: %s", CYRUS_SASLAUTHD_SOCKET,
+                               strerror(errno));
+       return PWCHECK_FAIL;
+    }
+
+    if ( write_string(s, userid, Ustrlen(userid)) < 0) {
+        DEBUG(D_auth)
+            debug_printf("Failed to send userid to saslauthd daemon \n");
+        (void)close(s);
+        return PWCHECK_FAIL;
+    }
+
+    if ( write_string(s, password, Ustrlen(password)) < 0) {
+        DEBUG(D_auth)
+            debug_printf("Failed to send password to saslauthd daemon \n");
+        (void)close(s);
+        return PWCHECK_FAIL;
+    }
+
+    memset((void *)password, 0, Ustrlen(password));
+
+    if ( write_string(s, service, Ustrlen(service)) < 0) {
+        DEBUG(D_auth)
+            debug_printf("Failed to send service name to saslauthd daemon \n");
+        (void)close(s);
+        return PWCHECK_FAIL;
+    }
+
+    if ( write_string(s, realm, Ustrlen(realm)) < 0) {
+        DEBUG(D_auth)
+            debug_printf("Failed to send realm to saslauthd daemon \n");
+        (void)close(s);
+        return PWCHECK_FAIL;
+    }
+
+    if ( read_string(s, &daemon_reply ) < 2) {
+        DEBUG(D_auth)
+            debug_printf("Corrupted answer '%s' received. \n", daemon_reply);
+        (void)close(s);
+        return PWCHECK_FAIL;
+    }
+
+    (void)close(s);
+
+    DEBUG(D_auth)
+        debug_printf("Answer '%s' received. \n", daemon_reply);
+
+    *reply = daemon_reply;
+
+    if ( (daemon_reply[0] == 'O') && (daemon_reply[1] == 'K') )
+        return PWCHECK_OK;
+
+    if ( (daemon_reply[0] == 'N') && (daemon_reply[1] == 'O') )
+        return PWCHECK_NO;
+
+    return PWCHECK_FAIL;
+}
+
+#endif
+
+
+/* helper functions */
+#if defined(CYRUS_PWCHECK_SOCKET) || defined(CYRUS_SASLAUTHD_SOCKET)
+
+#define MAX_REQ_LEN 1024
+
+/* written from scratch */
+
+/* FUNCTION: read_string */
+
+/* SYNOPSIS
+ * read a sasld-style counted string into
+ * store-allocated buffer, set pointer to the buffer,
+ * return number of bytes read or -1 on failure.
+ * END SYNOPSIS */
+
+static int read_string(int fd, uschar **retval) {
+    unsigned short count;
+    int rc;
+
+    rc = (retry_read(fd, &count, sizeof(count)) < (int) sizeof(count));
+    if (!rc) {
+        count = ntohs(count);
+        if (count > MAX_REQ_LEN) {
+            return -1;
+        } else {
+            *retval = store_get(count + 1);
+            rc = (retry_read(fd, *retval, count) < (int) count);
+            (*retval)[count] = '\0';
+            return count;
+        }
+    }
+    return -1;
+}
+
+
+/* FUNCTION: write_string */
+
+/* SYNOPSIS
+ * write a sasld-style counted string into given fd
+ * written bytes on success, -1 on failure.
+ * END SYNOPSIS */
+
+static int write_string(int fd, const uschar *string, int len) {
+    unsigned short count;
+    int rc;
+    struct iovec iov[2];
+
+    count = htons(len);
+
+    iov[0].iov_base = (void *) &count;
+    iov[0].iov_len = sizeof(count);
+    iov[1].iov_base = (void *) string;
+    iov[1].iov_len = len;
+
+    rc = retry_writev(fd, iov, 2);
+
+    return rc;
+}
+
+
+/* taken from cyrus-sasl file saslauthd/saslauthd-unix.c  */
+
+/* FUNCTION: retry_read */
+
+/* SYNOPSIS
+ * Keep calling the read() system call with 'fd', 'buf', and 'nbyte'
+ * until all the data is read in or an error occurs.
+ * END SYNOPSIS */
+static int retry_read(int fd, void *inbuf, unsigned nbyte)
+{
+    int n;
+    int nread = 0;
+    char *buf = (char *)inbuf;
+
+    if (nbyte == 0) return 0;
+
+    for (;;) {
+       n = read(fd, buf, nbyte);
+       if (n == 0) {
+           /* end of file */
+           return -1;
+       }
+       if (n == -1) {
+           if (errno == EINTR) continue;
+           return -1;
+       }
+
+       nread += n;
+
+       if (n >= (int) nbyte) return nread;
+
+       buf += n;
+       nbyte -= n;
+    }
+}
+
+/* END FUNCTION: retry_read */
+
+/* FUNCTION: retry_writev */
+
+/* SYNOPSIS
+ * Keep calling the writev() system call with 'fd', 'iov', and 'iovcnt'
+ * until all the data is written out or an error occurs.
+ * END SYNOPSIS */
+
+static int     /* R: bytes written, or -1 on error */
+retry_writev (
+  /* PARAMETERS */
+  int fd,                              /* I: fd to write on */
+  struct iovec *iov,                   /* U: iovec array base
+                                        *    modified as data written */
+  int iovcnt                           /* I: number of iovec entries */
+  /* END PARAMETERS */
+  )
+{
+    /* VARIABLES */
+    int n;                             /* return value from writev() */
+    int i;                             /* loop counter */
+    int written;                       /* bytes written so far */
+    static int iov_max;                        /* max number of iovec entries */
+    /* END VARIABLES */
+
+    /* initialization */
+#ifdef MAXIOV
+    iov_max = MAXIOV;
+#else /* ! MAXIOV */
+# ifdef IOV_MAX
+    iov_max = IOV_MAX;
+# else /* ! IOV_MAX */
+    iov_max = 8192;
+# endif /* ! IOV_MAX */
+#endif /* ! MAXIOV */
+    written = 0;
+
+    for (;;) {
+
+       while (iovcnt && iov[0].iov_len == 0) {
+           iov++;
+           iovcnt--;
+       }
+
+       if (!iovcnt) {
+           return written;
+       }
+
+       n = writev(fd, iov, iovcnt > iov_max ? iov_max : iovcnt);
+       if (n == -1) {
+           if (errno == EINVAL && iov_max > 10) {
+               iov_max /= 2;
+               continue;
+           }
+           if (errno == EINTR) {
+               continue;
+           }
+           return -1;
+       } else {
+           written += n;
+       }
+
+       for (i = 0; i < iovcnt; i++) {
+           if (iov[i].iov_len > (unsigned) n) {
+               iov[i].iov_base = (char *)iov[i].iov_base + n;
+               iov[i].iov_len -= n;
+               break;
+           }
+           n -= iov[i].iov_len;
+           iov[i].iov_len = 0;
+       }
+
+       if (i == iovcnt) {
+           return written;
+       }
+    }
+    /* NOTREACHED */
+}
+
+/* END FUNCTION: retry_writev */
+#endif
+
+/* End of auths/pwcheck.c */
diff --git a/src/auths/pwcheck.h b/src/auths/pwcheck.h
new file mode 100644 (file)
index 0000000..1287ea2
--- /dev/null
@@ -0,0 +1,27 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* This file provides support for authentication via the Cyrus SASL pwcheck
+daemon (whence its name) and the newer saslauthd daemon. */
+
+/* Error codes used internally within the authentication functions */
+
+/* PWCHECK_OK   - auth successful
+   PWCHECK_NO   - access denied
+   PWCHECK_FAIL - [temporary] failure */
+
+#define PWCHECK_OK   0
+#define PWCHECK_NO   1
+#define PWCHECK_FAIL 2
+
+/* Cyrus functions for doing the business. */
+
+extern int pwcheck_verify_password(const char *, const char *, const char **);
+extern int saslauthd_verify_password(const uschar *, const uschar *,
+           const uschar *, const uschar *, const uschar **);
+
+/* End of pwcheck.h */
diff --git a/src/auths/sha1.c b/src/auths/sha1.c
new file mode 100644 (file)
index 0000000..67a1191
--- /dev/null
@@ -0,0 +1,552 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+#ifndef STAND_ALONE
+#include "../exim.h"
+
+/* For stand-alone testing, we need to have the structure defined, and
+to be able to do I/O */
+
+#else
+#include <stdio.h>
+#include <stdlib.h>
+typedef unsigned char uschar;
+typedef struct sha1 {
+  unsigned int H[5];
+  unsigned int length;
+  }
+sha1;
+#endif
+
+
+
+/*************************************************
+*        Start off a new SHA-1 computation.      *
+*************************************************/
+
+/*
+Argument:  pointer to sha1 storage structure
+Returns:   nothing
+*/
+
+void
+sha1_start(sha1 *base)
+{
+base->H[0] = 0x67452301;
+base->H[1] = 0xefcdab89;
+base->H[2] = 0x98badcfe;
+base->H[3] = 0x10325476;
+base->H[4] = 0xc3d2e1f0;
+base->length = 0;
+}
+
+
+
+/*************************************************
+*       Process another 64-byte block            *
+*************************************************/
+
+/* This function implements central part of the algorithm
+
+Arguments:
+  base       pointer to sha1 storage structure
+  text       pointer to next 64 bytes of subject text
+
+Returns:     nothing
+*/
+
+void
+sha1_mid(sha1 *base, const uschar *text)
+{
+register int i;
+unsigned int A, B, C, D, E;
+unsigned int W[80];
+
+base->length += 64;
+
+for (i = 0; i < 16; i++)
+  {
+  W[i] = (text[0] << 24) | (text[1] << 16) | (text[2] << 8) | text[3];
+  text += 4;
+  }
+
+for (i = 16; i < 80; i++)
+  {
+  register unsigned int x = W[i-3] ^ W[i-8] ^ W[i-14] ^ W[i-16];
+  W[i] = (x << 1) | (x >> 31);
+  }
+
+A = base->H[0];
+B = base->H[1];
+C = base->H[2];
+D = base->H[3];
+E = base->H[4];
+
+for (i = 0; i < 20; i++)
+  {
+  unsigned int T;
+  T = ((A << 5) | (A >> 27)) + ((B & C) | ((~B) & D)) + E + W[i] + 0x5a827999;
+  E = D;
+  D = C;
+  C = (B << 30) | (B >> 2);
+  B = A;
+  A = T;
+  }
+
+for (i = 20; i < 40; i++)
+  {
+  unsigned int T;
+  T = ((A << 5) | (A >> 27)) + (B ^ C ^ D) + E + W[i] + 0x6ed9eba1;
+  E = D;
+  D = C;
+  C = (B << 30) | (B >> 2);
+  B = A;
+  A = T;
+  }
+
+for (i = 40; i < 60; i++)
+  {
+  unsigned int T;
+  T = ((A << 5) | (A >> 27)) + ((B & C) | (B & D) | (C & D)) + E + W[i] +
+    0x8f1bbcdc;
+  E = D;
+  D = C;
+  C = (B << 30) | (B >> 2);
+  B = A;
+  A = T;
+  }
+
+for (i = 60; i < 80; i++)
+  {
+  unsigned int T;
+  T = ((A << 5) | (A >> 27)) + (B ^ C ^ D) + E + W[i] + 0xca62c1d6;
+  E = D;
+  D = C;
+  C = (B << 30) | (B >> 2);
+  B = A;
+  A = T;
+  }
+
+base->H[0] += A;
+base->H[1] += B;
+base->H[2] += C;
+base->H[3] += D;
+base->H[4] += E;
+}
+
+
+
+
+/*************************************************
+*     Process the final text string              *
+*************************************************/
+
+/* The string may be of any length. It is padded out according to the rules
+for computing SHA-1 digests. The final result is then converted to text form
+and returned.
+
+Arguments:
+  base      pointer to the sha1 storage structure
+  text      pointer to the final text vector
+  length    length of the final text vector
+  digest    points to 16 bytes in which to place the result
+
+Returns:    nothing
+*/
+
+void
+sha1_end(sha1 *base, const uschar *text, int length, uschar *digest)
+{
+int i;
+uschar work[64];
+
+/* Process in chunks of 64 until we have less than 64 bytes left. */
+
+while (length >= 64)
+  {
+  sha1_mid(base, text);
+  text += 64;
+  length -= 64;
+  }
+
+/* If the remaining string contains more than 55 bytes, we must pad it
+out to 64, process it, and then set up the final chunk as 56 bytes of
+padding. If it has less than 56 bytes, we pad it out to 56 bytes as the
+final chunk. */
+
+memcpy(work, text, length);
+work[length] = 0x80;
+
+if (length > 55)
+  {
+  memset(work+length+1, 0, 63-length);
+  sha1_mid(base, work);
+  base->length -= 64;
+  memset(work, 0, 56);
+  }
+else
+  {
+  memset(work+length+1, 0, 55-length);
+  }
+
+/* The final 8 bytes of the final chunk are a 64-bit representation of the
+length of the input string *bits*, before padding, high order word first, and
+high order bytes first in each word. This implementation is designed for short
+strings, and so operates with a single int counter only. */
+
+length += base->length;   /* Total length in bytes */
+length <<= 3;             /* Total length in bits */
+
+work[63] = length         & 0xff;
+work[62] = (length >>  8) & 0xff;
+work[61] = (length >> 16) & 0xff;
+work[60] = (length >> 24) & 0xff;
+
+memset(work+56, 0, 4);
+
+/* Process the final 64-byte chunk */
+
+sha1_mid(base, work);
+
+/* Pass back the result, high-order byte first in each word. */
+
+for (i = 0; i < 5; i++)
+  {
+  register int x = base->H[i];
+  *digest++ = (x >> 24) & 0xff;
+  *digest++ = (x >> 16) & 0xff;
+  *digest++ = (x >>  8) & 0xff;
+  *digest++ =  x        & 0xff;
+  }
+}
+
+
+
+/*************************************************
+**************************************************
+*             Stand-alone test program           *
+**************************************************
+*************************************************/
+
+#ifdef STAND_ALONE
+
+/* Test values. The first 128 may contain binary zeros and have increasing
+length. */
+
+static uschar *tests[] = {
+  "",
+  "\x24",
+  "\x70\xf0",
+  "\x0e\x1e\xf0",
+  "\x08\x38\x78\x8f",
+  "\x10\x3e\x08\xfc\x0f",
+  "\xe7\xc7\x1e\x07\xef\x03",
+  "\xe0\xfb\x71\xf8\xf9\xc1\xfc",
+  "\xff\x7c\x60\x3c\x1f\x80\xe2\x07",
+  "\xf0\x3f\xc8\x60\x81\xfe\x01\xf8\x7f",
+  "\x9f\xc7\xf8\x1f\xc1\xe3\xc7\xc7\x3f\x00",
+  "\x00\x7f\xbf\xdf\xc0\xfe\x02\x7e\x00\xf8\x7f",
+  "\x01\x01\xc0\x1e\x03\xf8\x30\x08\x0f\xf3\xf9\xff",
+  "\xc4\x03\xfc\x0f\xf8\x01\xc0\x0f\xf0\x06\x10\x41\xff",
+  "\xff\x07\x80\x47\xfc\x1f\xc0\x60\x30\x1f\xe0\x3c\x03\xff",
+  "\x80\x3f\x84\x3e\xff\xc0\x3f\x0f\x00\x7f\xc0\x1f\xe7\xfc\x00",
+  "\xff\xe0\x7f\x01\x81\x81\xff\x81\xff\x00\x3e\x00\x20\x7f\x80\x0f",
+  "\x00\x3e\x00\x70\x1f\xe0\x0f\xfa\xff\xc8\x3f\xf3\xfe\x00\xff\x80\xff",
+  "\x7f\xef\xc0\x1e\x7c\xff\xe0\x1f\xfe\x00\x1f\xf0\x08\xff\xc0\x7f\xf0\x00",
+  "\xe0\x0f\x80\x07\x0c\x01\xff\xe0\x03\xf0\x2f\xf8\x3f\xef\x00\x78\x01\xfe\x00",
+  "\xe7\x00\x10\x00\xf8\x18\x0f\xf0\xff\x00\xff\x80\x3f\xc3\xfe\xf0\x0f\xfc\x01\xff",
+  "\x00\x1f\xf8\x0f\xfc\x00\xfc\x00\xff\x87\xc0\x0f\x80\x7b\xff\x00\x0f\x02\x01\xff\xc0",
+  "\x00\x0f\xf0\x03\xc7\xf8\x3e\x03\xff\x80\x03\xff\x80\x07\xff\x0f\xff\x1f\x83\xff\x80\x1f",
+  "\xff\xc0\x1f\x80\x3f\x9f\xf8\x78\x3e\x7f\xf8\x00\x3e\x20\x04\x3f\x80\x7f\xfc\x00\x1f\xfe\x00",
+  "\x3f\x07\x80\xe0\x07\xe0\x00\xfc\x7f\xc0\xc0\x0f\x8f\xf0\x80\x0e\x0e\x03\xff\xbf\xfc\x01\xff\xe0",
+  "\xff\xfc\x11\xfc\xe0\x0e\x1f\xff\x87\x80\x1f\xe0\xff\xfd\xff\xc0\x03\xff\xc0\x0f\x00\x07\xf0\x01\xff",
+  "\xf0\x07\xc1\xfe\x00\xf8\x01\xe7\x80\xff\x80\x3f\x1f\x7f\x8c\x00\x1c\x00\x0f\xf8\x07\xfc\x00\xff\xfc\x00",
+  "\x00\x0f\xf8\x3f\xc0\x60\x00\x7f\xf8\xff\x00\x03\xf0\x3c\x07\xc0\x7f\xe0\x3f\xf8\x01\x00\x7e\x03\xff\xc0\x00",
+  "\x00\x0f\xf8\x03\x00\x1f\xff\x00\x0f\xfe\x00\x3f\x00\x03\xff\xe0\x07\xc0\xff\x00\x3c\x7f\xf0\x01\xff\xf8\x3f\xff",
+  "\x00\x01\xe0\xe0\x1f\xfe\x00\x03\xfc\x00\x0f\xff\xe0\x0f\xff\x00\x0e\x00\x7f\xfc\x0f\xfe\x00\x78\x00\x3f\xff\x00\xff",
+  "\x80\x41\xff\xc3\xfe\x00\x1e\x00\x0f\xff\xe0\xff\x80\x0f\xe0\x00\x7f\xf7\xff\x01\xfe\x01\xff\xdf\xff\x00\x01\xff\xe0\x00",
+  "\xf8\x07\x00\xff\xc0\x7f\xbe\x00\x0f\xff\x00\x03\xe3\xf0\xff\xf0\x00\x1f\x81\xff\x80\x0f\xff\x80\x20\x03\xf0\x03\x80\xff\xfc",
+  "\x00\x38\x20\x00\x7f\xf0\x01\xff\xfe\xcf\xfe\x07\xff\xc0\x00\x7f\xf8\x1f\x00\x00\xc0\x00\xc0\x0f\xff\x3e\x0f\xc0\x0f\xff\x80\x00",
+  "\x1f\xf8\x07\xff\xf8\x03\xe0\x01\xff\xfc\x3f\xf8\x00\x38\x1f\x00\x3f\xdc\x01\xc0\x04\xff\xff\x00\x0f\xfc\x08\x02\x00\x01\xf0\x3f\xff",
+  "\x80\x07\x86\x00\x03\xff\xe0\x00\x3f\xf8\x00\x0f\x80\x0f\xf8\x0f\xff\xe0\x00\x1f\x80\x00\x7f\xf8\xc0\x0f\xff\xf0\x7c\x04\x07\xff\x00\x00",
+  "\x01\xff\x00\x18\x3e\x0f\x00\x07\xff\xc0\x00\xf0\x1f\xfe\x07\x80\x60\x0f\xf8\x00\x3f\xfe\x38\x1f\xc0\x00\x3f\x81\xff\xfc\x1f\xe0\x00\x3f\xff",
+  "\xf0\x3f\xff\xc0\x00\x7f\xf0\x00\x3f\xff\x0f\xe0\x07\x0f\xfc\x7e\x03\xff\xf0\xfc\x0f\x9f\xc0\x3f\xff\xcf\xff\x00\x00\xff\xc0\x00\xe7\x01\xff\xf8",
+  "\x00\x01\xff\x80\x20\x00\x7f\xe0\x00\x7e\x07\xff\xf8\xc7\xf8\xff\xf0\x0f\xfe\x00\x00\xe0\x0f\xe0\x00\x1f\xff\x87\xff\x00\x01\xf0\x00\x7f\xc1\xff\xff",
+  "\x00\x00\x7f\xff\xc0\x01\xfe\x7e\x01\xff\xfe\xff\xf0\x7f\xff\xcf\xf8\x07\xfe\x00\x0f\xff\xc0\x07\xff\xfc\x00\x3e\x00\x07\xfc\x00\x7f\xc0\x07\x80\x0f\xff",
+  "\xff\xff\x03\xff\x07\xf8\xff\xff\x80\x00\x7f\xfe\xff\xfe\x00\x03\xff\xf8\x1f\xff\x3f\xf8\x1f\xff\x00\x1f\xff\x0f\xc0\x7f\xf0\x01\xff\xe0\x00\x1f\xff\x00\x00",
+  "\xff\xff\x00\x00\xff\xfc\x00\x03\x0f\xff\xf0\x01\xf8\x00\x0f\xe1\xff\xff\x03\xe0\x3f\x1f\xff\x80\x00\x7c\x00\x01\xff\xc0\x01\x7f\xfe\x00\x0e\x07\xff\xe0\xff\xff",
+  "\xc0\x00\x3f\xfe\x03\xfc\x0c\x00\x04\x01\xff\xe1\xe0\x03\xff\xe0\x30\x01\xff\x00\x00\x3c\x1e\x01\x80\x01\xff\x00\x40\x3f\xfe\x00\x3f\xff\x80\x7c\x01\xff\x80\x00\x7f",
+  "\x3f\xa0\x00\x0f\xff\x81\xff\xc0\x0f\xf0\x7f\xf8\x00\x0f\xc0\x00\x7f\xe0\x01\xe0\x00\x04\xff\x00\x1f\xfe\x00\x01\xff\x80\x07\xff\xfe\x00\x3f\xff\xc0\x03\xff\x80\x00\x3f",
+  "\xf0\x1f\xff\x01\xff\x80\xff\xc0\x80\x07\xf0\x00\x03\xff\x80\x00\x18\x01\xff\xfc\x00\xff\xfc\x03\xff\xff\x00\x7f\xc0\x03\xff\xc7\xff\xc0\x03\xf0\xff\x80\x00\x3f\xfe\x00\x00",
+  "\x07\xf1\xbf\xff\xe0\x00\x78\x00\x07\xe0\x00\x80\x03\xf0\x3f\xf7\x00\x00\x38\x00\xfe\x00\xf8\x0f\xfe\x00\x00\x80\x3f\xff\xc1\xff\xfc\x00\xff\xff\x8f\xf0\x00\x1f\xff\xf0\x0f\xff",
+  "\x00\x1c\x00\x07\xff\xfc\x00\x5e\x3f\xff\x00\x00\x3c\xff\xff\xc0\x3f\xff\x81\xe0\x70\x00\x1f\xfc\x00\x03\xff\x00\x00\x7f\xff\xc0\x1f\x8c\x0f\xff\xf0\xff\x80\x07\xe0\x10\x01\xff\xff",
+  "\xc0\x00\x07\xff\x80\x7f\xff\x80\x01\x80\x3f\xff\xcf\xc0\xfe\x00\xff\xc0\x1f\xfc\x01\xff\xf8\x00\xff\xfe\x0f\xff\xf0\x06\x00\x00\xc0\x3f\xff\x80\x78\xff\xfc\x00\x0f\xff\xf0\x00\x0f\xff",
+  "\xff\xe0\x07\xff\xf8\x00\x7f\xf0\x1f\xff\x80\x01\xff\xf8\x1f\xf8\x01\x03\xff\xe0\x00\x03\xe0\x78\x0f\xff\x00\x0f\xfc\x1f\xf8\x00\x0f\xff\xe0\x1f\x00\x07\xff\xfc\x00\x1f\x03\xff\xf7\xff\xff",
+  "\xc0\xf8\x00\x03\xfe\x00\x3f\xff\xf0\x00\x03\xfc\x0f\xff\x80\x00\xe3\xff\xf8\x3f\xfe\x00\x00\x73\xe0\xff\xfc\x07\xff\xc3\xff\xfe\x03\x00\x00\x70\x00\x03\xff\xf8\x0f\xff\xe0\x00\x1f\xff\xf8\x00",
+  "\xff\xf0\x0f\xc7\xff\xfc\x00\x3f\xfe\x00\x00\x3f\xff\x80\x3f\x80\x00\x3f\xff\xc0\x00\x70\x01\xff\xc1\x80\x03\xff\xff\x80\x00\x61\xff\xfe\x03\xfd\x80\x3f\xff\xe0\x01\xc1\xff\xff\x80\x00\x0f\xfe\x00",
+  "\xff\xfc\x00\x03\xff\xf0\x0f\xf8\x00\x07\xdf\x8f\xff\xf8\x00\x01\xff\xfe\x00\x80\x00\xff\x80\x1f\xf0\x00\x01\x1c\x00\x00\x3f\xf8\x00\x3f\xff\xef\xff\xfe\x01\xc3\x80\x80\x01\xff\xff\xc0\x00\x07\xff\xff",
+  "\xff\xff\xc0\x01\xff\xc1\xff\xff\x87\xff\xff\x00\x3f\x00\x00\x1f\xfc\x00\x01\xff\x80\x1f\xc0\x1f\xff\x00\x00\xff\x80\x1f\xff\xf8\x7f\xf8\x3f\xff\xc1\xff\xff\xe0\x01\xc0\x3f\xf7\xff\xfe\xfc\x00\x00\x3f\xff",
+  "\x00\xff\x81\xff\xe0\x03\xf8\x0e\x00\x00\xff\xf8\x1f\xff\xfe\x00\x00\xff\x80\x00\x07\xff\xf8\x01\xff\xe0\x00\x0f\xf0\x01\xfe\x00\x3f\xf0\x7f\xe0\x00\x7f\xff\xe0\x1f\xff\xfc\x01\xff\xe0\x01\x80\x00\x07\xff\xff",
+  "\x00\x0f\xff\xf0\x00\x00\xe0\x0f\xf8\x00\x00\xff\xff\x80\x03\xff\xe1\xff\xff\x3f\xf8\x0f\xff\xc7\xe0\x00\x1f\xff\x00\x3f\xfe\x0f\xff\xf0\x03\x00\xc0\x00\x1f\xff\xfc\x3f\xff\xe0\x3f\xff\xf8\x1f\xf0\x00\x1f\xff\xc0",
+  "\x01\x80\x00\x1f\x01\xff\xff\x83\x00\x01\xfc\x00\x7f\xe0\x0e\x7f\xfe\x00\x00\x38\x00\xff\x00\x00\x3f\xff\x83\x83\xff\xc0\x00\x7f\xff\x80\x1f\xff\xf0\x1f\xff\xfc\x00\x03\x7f\xff\x81\xc0\x00\x07\xff\x83\xff\xff\x00\x00",
+  "\xff\x80\x0d\xff\xe0\x03\xff\xf0\x00\xff\xfc\x00\xf0\x01\xf8\x07\xff\xf8\x0f\x80\x0f\xff\xff\x00\xff\xff\x87\xff\xe1\xff\xfc\x67\x8c\x7f\xfe\x00\x03\xff\x3f\xfc\x07\x01\xff\xff\xe0\x00\x01\xff\xff\xc0\x0c\x40\x0f\xff\xff",
+  "\x00\x00\x1f\xff\xfe\x00\x1f\x00\x00\x1f\xff\xff\x07\xff\xff\xc0\x07\xff\xe0\x00\x02\x00\x00\xff\x00\x78\x00\x00\xe0\x00\x08\x00\x1f\xff\xff\x00\x03\xf8\x1f\x00\x00\x0f\xff\xc0\x00\x01\xff\xff\xe1\xf8\x00\x00\x3f\x80\x0f\xff",
+  "\x00\x0f\xf8\x00\xfc\x00\x03\xff\xff\x00\x00\x3f\xf0\x01\xff\xff\xe0\x7f\xf8\x00\xf8\x0f\xff\xff\x80\x00\x0f\xff\xfc\x0f\xff\xe0\x00\x00\xff\xc3\xff\xf0\x07\xff\xff\x00\x38\xf8\x00\x20\x1f\xfe\x3f\xfe\x00\xfe\x00\x7f\xff\xc0\x00",
+  "\x00\x3f\x00\xe0\x00\x0f\xff\xfc\x7f\xff\xfc\x00\x00\x7e\x00\x00\xff\xfe\x1f\xf0\x00\x1f\xf0\x00\x1f\xff\x87\xf0\x00\x3f\xc0\x0f\xff\x87\xff\x00\x3f\x81\xff\xff\xf7\xff\xe0\xff\xe0\x3f\x9f\xff\x00\x07\x00\x7f\xfc\x03\xff\xf0\x00\x00",
+  "\xe0\x3f\xff\xf0\xff\x80\x3e\x00\x03\xff\xe0\x00\x0f\xfc\x00\x07\xff\xf8\x00\x00\x7f\x80\x00\x0f\xf8\x01\xff\x7f\xff\xf0\x00\x3f\xff\xfe\x7f\xff\xe0\x00\xff\xc3\xff\xff\x00\x00\xf0\x00\x00\x7f\xff\x00\x3f\xff\xf0\x00\x01\xc0\x03\xff\xff",
+  "\x00\x03\xc0\x01\xff\xdf\xfd\xff\x9f\xfe\x1f\xff\xff\x00\x3f\xff\xfe\x00\x00\x7f\xcf\xff\xf0\x1f\xff\xfe\x07\xf0\x00\xff\xff\xe0\x00\x01\x00\x07\xff\x80\x1f\xe0\x00\x00\xff\xfe\x03\xff\xff\x80\x03\xf0\x0f\xff\xfe\x00\x00\x1f\xff\xf8\x00\x00",
+  "\x00\x1f\xff\xfb\xff\xfe\x00\x07\xff\xf0\x00\x00\xff\xff\x00\x00\x0f\xf3\xff\xfe\x00\x78\x00\x00\x3e\x00\x00\x3f\xff\xf8\x00\x1f\xff\xff\x80\x00\x03\xff\xff\x00\x07\xff\xee\x00\x1f\xfc\x00\x78\x00\x00\x1f\xff\x07\xff\xfe\x03\xff\xff\xe0\x00\x00",
+  "\x00\x7f\xff\xfe\x00\x00\x3f\xfc\x03\xff\xfc\x1f\xff\xf0\x7f\xd8\x03\xf0\x00\xfd\xfc\x38\x00\x08\x00\x10\x00\xe0\x06\x00\x7f\xfe\x00\x00\x0f\xff\x80\x00\x3f\x03\xff\xfe\xff\xff\xf9\xff\xf8\x00\x07\xff\xfc\x01\xff\xc0\x00\x03\xff\xff\xe0\x03\xff\xff",
+  "\xff\xf0\x0f\xff\xff\x00\x06\x00\xff\xff\xf0\x07\xff\xe0\x04\x00\x03\x00\x00\x03\xf0\xff\xff\x00\x03\xff\xfb\xff\xc3\xff\xf0\x07\xff\xff\xc7\x00\x7f\x80\x00\x03\xff\xf8\x00\x1f\xe1\xff\xf8\x63\xfc\x00\x3f\xc0\x9f\xff\xf8\x00\x00\x7f\xff\x1f\xff\xfc\x00",
+  "\x00\x3f\xff\xfc\x00\x0f\xc7\x80\x00\x02\x00\x1e\x00\x00\x60\x7f\x03\xfe\x00\x00\x1f\xff\x80\x1f\xf8\x00\x00\xff\xff\x80\x00\x03\xff\xc0\x00\x7f\xff\xc0\x7f\xe0\x03\xfc\x00\xff\xf7\xff\xff\x00\x00\x1f\xf0\x00\x03\xff\xff\xe1\xff\xff\x80\x0f\xf8\x00\x00\x1f",
+  "\x00\x01\xfe\x00\x03\x83\xf3\xff\xff\x80\x07\xff\xfc\x3f\xff\xfc\x03\xff\x80\x00\x06\x00\x00\x78\x00\x07\xff\xff\x80\x07\xfc\x01\xf8\x00\x07\xff\xff\xc0\x00\x38\x00\x07\xff\xfe\x3f\xff\xf8\x3f\xff\xcf\x3f\xfc\x00\x7f\xff\x00\x1f\xff\x80\x00\x30\x03\xff\xff\x00",
+  "\xf8\x00\x38\x00\x00\x3e\x3f\x00\x00\x3f\xff\xf0\x02\x00\x00\x0f\xff\xff\x80\x80\x03\xff\xc0\x00\x04\x00\x0f\xc0\x3f\xff\xfe\x00\x00\x3f\xff\xfe\x00\x3f\xff\xf8\x00\x30\x00\x7b\xff\x00\x00\x03\xff\xfc\x3f\xe1\xff\x80\x00\x70\x1f\xff\xc0\x07\xfc\x00\x1f\xff\xf0\x00",
+  "\x00\x03\xf8\x18\x00\x00\x70\x3f\xff\xf8\x00\x00\xff\xcf\xff\xff\xc0\x03\xff\xfe\x00\x10\x00\x00\xfe\x03\xff\xf8\x00\x00\x7e\x00\x00\x7f\x8f\xff\xc0\x00\x00\x7f\xff\xe0\x00\x3c\x07\xc0\x00\x00\x7f\xff\x01\xff\xf8\x01\xff\x80\x00\x0f\xff\xf9\xe0\x00\x3f\xff\xe0\x00\x00",
+  "\xff\xfe\x00\x3f\xc0\x1f\xff\xf0\x7f\xf8\x00\x01\xff\xf8\x1f\xff\xfe\x00\x00\xff\xff\xf8\x00\x7f\xff\x80\x3f\xff\xff\x00\x7f\xff\xf8\x00\x0c\x00\x00\x0f\xfe\x7e\x00\x3f\xe0\x18\x7f\xfe\x00\x00\x38\x00\x00\x3f\xff\xfe\x00\x00\x03\xfc\xff\xe1\xfe\x1f\xff\xfe\x00\x00\x07\xff",
+  "\x00\x00\x07\xff\xfe\x00\x00\x07\xfe\x00\x00\x3f\xe0\x00\x7f\xff\xc0\x00\x00\x7f\xff\xfc\x00\xfe\x00\x03\xff\xe0\x00\x1f\x0f\xfc\x00\x1f\xff\x80\x00\x07\xff\xff\xf0\x00\xff\xff\xf0\x00\x00\x1f\xff\xf8\x01\xff\xe0\x1f\xff\xff\x00\x1f\x80\x07\xf0\x00\x01\xff\xf8\x00\x01\xff\xff",
+  "\x00\x00\x3f\xff\xff\x03\xfe\x00\x00\x07\xc0\x00\x00\x7f\xfc\x0f\xf0\x00\x00\x1f\xff\xfe\x00\x00\x07\xc0\x00\x00\xff\xfe\x00\x00\x3f\xff\xfc\x01\xff\x7f\xfc\x00\x1f\xf8\x00\x1f\xff\x07\xff\xff\xe0\x00\x7f\xff\xfc\x01\xff\xff\xf0\x00\x01\xff\xf8\x00\x1e\x00\x00\x7f\xfc\x00\x3f\xff",
+  "\xfe\x3f\xff\x83\xff\xfe\x00\x07\xff\xff\xf0\x00\x3e\x00\x00\xff\xff\xfc\x00\x40\x3f\xfe\x00\x00\x03\xf0\x00\x00\x70\x3f\xf8\x0f\xff\xff\xe0\x1f\x80\x00\x03\xc3\xff\xff\xf0\x00\x01\xff\xf0\x0f\x80\x00\x0f\xe0\xff\xff\xfe\xf0\x00\x01\xff\xc0\x00\x00\x7f\xf0\x00\x00\x7f\xfe\xe0\x00\x00",
+  "\x00\x00\x03\xff\xf0\x01\xfc\x00\x00\xff\xff\x00\x00\x7f\xff\xff\x80\x07\xff\x8f\xff\x80\x00\x0f\xff\xf0\x00\x00\x3c\x00\x03\xc0\xff\xff\xfe\x01\xff\xff\x80\x0c\x7f\xff\xf8\x00\x00\x1f\xf0\x00\x00\x7f\x80\x00\x00\x80\x00\x00\xff\xff\xf0\x1f\xff\xe0\x00\xff\xff\xfe\x1f\xff\x1f\xc0\x00\x00",
+  "\xff\xff\xfe\x07\xff\xc0\x00\x06\x3f\x9f\xf0\x07\xff\xf0\x3f\xfe\x1f\xff\xff\x81\xff\xff\xc0\x00\x02\x00\xfe\x00\x04\x00\x07\x00\x00\x01\xff\xff\xfe\x00\x00\x07\xff\xfe\x00\x1f\xfe\x00\x00\xff\xff\xe0\x07\xf8\x00\xff\xff\xfc\x00\x3f\xf3\xff\xff\xc0\x00\x7f\xff\xe0\x00\x0f\xff\xfc\x07\xff\xff",
+  "\xff\xf0\x00\x00\x7e\x00\x1e\x03\xff\xff\x00\x00\x73\xff\xf0\x00\x00\x0f\xff\xdf\xff\xff\xdf\xfc\x00\x07\xfe\x07\xff\xfe\x00\x00\x1f\xdf\xef\xff\xf0\x3f\xff\xfc\x00\x00\x07\xff\xff\xf0\x00\x00\x7f\xe0\x07\xff\x80\x00\x00\x7f\xe0\x03\xff\xff\xf9\xff\xe0\x00\x00\x3f\xe3\xff\xff\xfc\x00\x00\x03\xff",
+  "\x00\x03\xff\x00\x00\x3f\xff\x80\x01\xf0\x00\x0f\xfe\x00\x00\x06\x00\x03\xff\xff\xfc\x03\xff\xff\xf7\x80\x00\x00\x7f\xc0\x0f\xff\xe3\xfe\x0f\x00\x00\x7f\xff\x00\x7f\xf8\x00\x00\xff\xff\xee\x00\x7e\x01\xc0\x00\x1f\xe0\x00\x07\xff\xff\xf8\x00\x00\xe1\xff\xfc\x3f\xe7\xff\xff\xf8\x3f\xff\xfc\x00\x1f\xff",
+  "\x00\x00\x0f\xff\xf8\x00\x00\xff\xff\xfc\x00\x1f\xe0\x07\xff\xff\x00\x01\xff\xdf\xff\x80\x00\x3f\xff\xfc\x00\x00\x0f\xfc\x07\xff\x00\x00\xff\x80\x00\x03\xff\xff\xf0\x00\x07\xff\xff\xf0\x00\xff\xfe\x1f\xff\xff\xe0\x3f\xff\xfe\x00\x00\x60\x00\x00\xff\xff\x7f\xff\xf0\x00\x03\xff\xff\xc0\x07\x00\x01\xff\xff",
+  "\x00\x00\x20\x7f\xfe\x0f\x83\xff\xff\x80\x03\xff\x00\x00\x00\xff\xff\xe0\x00\x1f\xff\xff\xe0\x00\x3f\xfe\x7f\xff\xf0\x00\x1f\xff\xff\xe0\x00\x00\xff\xff\x87\xff\xc0\x00\x17\xfd\xff\x9f\xff\xfb\xff\xff\xe0\x00\x03\xe0\x00\x07\xff\x9f\xff\xff\x80\x00\x7f\xff\xff\x00\x01\xff\xff\xc0\xff\xff\xc0\x10\x00\x00\x1f",
+  "\x00\x00\x07\xff\xc0\x00\xff\xe0\x00\x07\xff\x80\x03\x80\x00\x0f\xf8\x00\x00\x7f\xff\xfe\x00\x00\x18\x00\xff\xf0\x20\x01\xff\xfe\x00\x00\x60\x0f\xf0\xe0\x03\xff\xfe\x00\x3e\x1f\xff\xfc\x00\x03\xff\x80\x00\x00\xff\xf8\x00\x01\x00\x00\x0f\xf3\xff\xfc\x00\x03\xff\xff\xe1\xff\xff\xc1\xf0\x00\x00\xff\xff\xff\x00\x00",
+  "\xff\xff\xf0\x00\x00\x07\xff\xfc\x00\x7f\x87\xff\xff\x00\x00\x00\x7f\xff\xc0\x7f\xff\x80\x00\x03\xf0\xff\x3f\xff\x80\x30\x07\xff\xff\x1f\x8e\x00\x7f\xff\xff\xc0\x01\xff\xfc\x07\xf8\x00\x00\x7f\xff\xfc\x00\x3f\xf0\x00\xf8\x00\x00\x07\xff\x00\x00\x0e\x00\x0f\xff\x80\x00\x7f\xc0\x01\xff\x8f\xf8\x00\x07\x01\xff\xff\xff",
+  "\xff\x80\x3f\xff\x3f\xfe\x00\x00\xff\xff\xff\x9f\xff\xf8\x3f\xff\xf8\x00\x00\x0f\xf8\x00\x00\x03\xfe\x00\x7f\xff\xff\x00\x0f\xff\x01\xff\xf0\x0f\xff\xe0\x20\x7f\xff\xfc\xff\x01\xf8\x00\x07\xff\xe0\x00\x7f\xf8\x00\x0f\xff\x80\x00\x00\x7f\xe0\x00\x3f\xf8\x01\xfe\x00\x07\xff\xf0\x00\x00\x7f\xff\xff\xc0\x00\x01\xff\xff\xff",
+  "\x00\x7f\xff\xe0\x00\x01\xff\xff\xf8\x00\x00\x3f\xff\xfc\x00\x7f\xfe\x00\x00\x03\xff\xff\xf0\x03\xff\xe0\x00\x7f\x80\x00\x0f\xff\x3f\xf8\x00\x00\x7f\xff\xff\x00\x07\x80\x1f\x83\xf8\x00\x00\x0f\xfe\x3f\xff\xc0\x3f\xff\xfe\x1f\xe0\x00\x07\xc0\x03\xff\xf0\x0f\xc0\x00\x03\xff\xff\x80\x00\x00\x7f\x80\x00\x00\xff\xff\x80\x00\x00",
+  "\xfe\x00\x00\x20\x00\x04\x00\x0f\xff\xff\xc0\x01\xff\xf8\x3f\xc0\x00\x00\xff\xff\xc0\x00\xff\xff\xff\x80\x00\x3f\xf8\x00\x7f\xff\xfe\x7f\xf8\x00\x7f\xff\x80\x07\xff\xc0\x00\x0f\xff\xf8\x00\x7f\xff\xc0\x00\xff\xff\xc0\x3f\xff\xff\xe0\x0f\xff\xff\xe0\xe0\x1f\xff\x80\x00\x00\x7f\xff\xc0\x71\xff\xff\xfc\x00\x01\xff\xff\xf8\x00\x00",
+  "\xff\xff\xe0\x00\x0f\xff\xf0\x00\x00\x3f\xff\xff\xc0\x00\xff\xff\x00\x00\x0f\xff\xff\xe0\x00\x01\xff\x00\x00\x1f\xff\xe0\x3f\xfc\x00\x03\xe0\x1f\xf0\x1f\xf8\x00\x00\x3f\xff\xff\xc0\x0f\xfe\x00\x00\x20\x00\x00\xff\xfc\x00\x0f\xff\xfe\x3f\xff\xff\x00\xff\xf0\x00\x00\x80\x00\x1f\x03\xe0\x01\xff\xfa\x00\x3f\xe0\x00\x00\x70\x00\x00\x0f",
+  "\xfd\xff\xc0\x00\x20\x01\xfe\x00\x3f\xf8\x00\x03\xff\x00\x00\x03\xf8\xff\xcf\xc3\xff\xff\xfc\x00\x03\xff\xff\xfc\x00\x00\x78\x3f\xff\xf0\x01\xff\xe0\x0f\xff\xff\x00\x00\x07\xff\xff\xfc\xff\xff\xf8\x00\x01\xff\x80\x00\x07\xff\xff\xfc\x00\x00\x1c\x00\x01\xff\xff\x07\xf8\x00\x00\x1f\xff\xff\xf0\x00\x01\xfe\x00\x7f\xff\xf0\x1f\xfc\x00\x00",
+  "\x00\x00\x00\x3f\xff\x00\x00\x07\xff\xff\xfc\x00\x00\x01\xe0\x0f\xff\x83\xfc\x01\xff\xff\xf0\x0f\xff\x00\x00\x00\x3f\xff\xff\xc0\x1f\xff\xff\xe0\x00\x20\x00\x00\x3f\xff\xff\x00\x00\xff\xfc\x03\xff\xff\xf0\x00\x1f\xff\xfc\x00\x00\x03\xff\xff\xc0\x7f\x80\x00\xff\xff\xff\x00\x0f\x0f\xff\xff\xe0\x00\x00\xff\xf8\x00\x00\xff\xfe\x00\x00\x0f\xff",
+  "\xff\xfe\x03\x80\x00\x03\xff\xff\xc0\x3f\xff\xff\x00\x03\xff\xff\xf8\x00\x00\x01\xff\xff\xcf\xfc\x00\x00\xe0\xef\xf8\x00\x0f\xff\xe3\xf8\x00\x3f\xff\xff\x80\x3f\xbf\xfe\x00\x00\xff\xfc\x00\x00\x01\xff\x00\x00\xcf\xc0\x01\xfc\x00\x00\x7f\xff\xff\xc0\x00\x10\x7f\xff\xfc\xfe\x00\x00\x07\xc0\xff\xff\xff\x8f\xff\x00\x00\x1f\x9e\x00\x00\x01\xff\xff",
+  "\x00\x7f\xff\xe0\x00\x01\xff\xff\xc3\xff\x80\x01\x03\xfc\x00\x00\x00\xfc\x01\xff\xff\xf8\x7f\xe7\xf0\x00\x00\x7f\xc0\x3f\xff\xff\xc0\x00\x00\x1f\xff\x80\x00\x01\xff\xe0\x00\x00\x70\x00\x00\x1c\x7f\xff\xf8\x1f\xfc\x00\x00\x07\xef\xe0\xff\xff\xc1\xff\xfc\x00\x00\x01\xff\xff\xff\xa0\x07\xff\x00\x1e\x00\x1f\xfc\x00\x00\x38\x00\x18\xc0\x00\x00\x7f\xff",
+  "\x00\x0f\xff\xf8\x00\x00\x07\xff\x00\xfc\x00\x00\x03\xff\xfc\x00\x07\xff\xff\xe0\x00\x00\xff\xfc\x0f\xff\x00\x00\x0f\xff\xfe\x0f\x80\x07\xff\x03\xff\xff\xf9\xff\xfe\x00\x00\x03\xf8\x00\x00\x07\xe0\x00\x00\xc0\x00\x1f\xff\xf0\x7f\xff\xff\xc0\x07\xff\xff\xfc\x00\x00\x3f\xff\xff\xe0\x00\x00\x1f\xff\xf8\x1f\xfe\x00\x00\x3f\xff\xff\xe0\x3f\x06\x00\x00\x00",
+  "\xff\xf0\x00\x08\x00\x0f\xef\xff\xff\xfc\x00\x00\x7f\xff\xf0\x00\x7f\xff\xf8\x00\xff\xff\x81\xff\xff\xe0\xff\xff\xff\x00\x00\x00\x80\x00\x03\xff\x80\x3f\xff\xfc\x00\x00\x1f\xff\xc0\x0f\xff\xfe\x00\x00\x00\x73\xf0\x1f\xfe\x00\xff\xc0\x3f\xff\x00\x3f\xff\x83\xff\xfe\x01\xff\xff\xf7\xff\xff\x80\x00\x00\x3f\x00\x00\x1f\xe3\xff\xff\xf0\x00\x0f\xff\xf0\x00\x00",
+  "\x00\x00\x7f\xfc\x00\x7f\xe0\x00\x0f\xff\xe0\x01\xf8\x00\x3f\xff\x00\x00\x78\x00\x7f\xe0\x00\x00\x1f\x00\x07\xff\xff\xf8\xf9\xf0\x01\xff\xf8\x07\xc0\x0f\xff\xf8\x00\x07\xf8\x7f\xfe\x00\x00\x0f\xff\xe3\xf0\x00\x07\xff\xff\xfc\x03\x1c\x00\x00\x7f\xe0\x00\xff\xff\xfc\x00\x00\x0f\xf3\xff\xe0\x00\x00\x0f\xff\xf9\x00\x00\x10\x00\x3f\xff\xfc\xf8\x7f\xff\x00\x00\x00",
+  "\x00\x03\xff\xff\xc0\x7f\xff\xff\xc0\x00\x03\xff\xff\xff\x00\x00\x0f\xff\xf0\x1f\xff\xf0\x00\x07\xff\xff\xef\xff\x81\xf7\xff\xfe\x00\x07\xff\xf0\x00\x00\x1f\xff\xc0\x0f\x80\x00\x0f\xff\xfc\x00\x00\xff\xff\xff\xc0\x03\xff\xe3\xff\xff\xfe\x00\x1f\xff\xff\x00\x00\xff\xff\xff\x0f\xff\xf1\xf8\x00\x00\x01\xff\xff\xff\x80\x1f\xff\xfe\x00\x08\x00\x00\x7f\xff\xff\x80\x00",
+  "\x1f\xe0\x00\x7c\x1f\xc0\x07\xff\xc0\x07\xff\xff\xfe\x00\x3c\x00\x00\x00\xff\xff\x80\x00\x07\xff\xff\x00\x1f\xf8\xff\xc0\x00\xff\x81\xff\x01\xff\xfe\x00\x78\x7f\xff\xf0\x00\x01\x80\x00\x00\x1f\xff\x00\x00\x1f\xff\xf0\x00\x1f\xff\xff\xe0\x00\x3c\x00\x00\x1f\xff\xff\x80\x03\xff\xe0\x01\xff\xff\xf9\xff\xf8\x00\x00\x7c\x00\x00\xfe\x00\x00\xff\xff\xff\x00\x00\x0f\xff\xff",
+  "\xfc\x00\x01\xff\x00\x00\x0c\x00\xff\xff\xe3\xff\xff\xf0\x80\x0e\x0e\x00\x00\x0f\xfe\x00\x03\xff\xc0\x00\x00\x7f\xff\xff\xe0\xc0\x00\x00\x07\xe0\xff\xff\x03\x9f\xff\xff\xc1\xc0\x00\x03\xff\xff\xc3\xff\xff\xfc\xff\xff\xc0\x00\x01\xfc\x00\x0f\xfc\x00\x00\x00\x7f\xff\xff\x03\xff\xff\xfc\x0f\xff\xfe\x00\x00\x03\x80\x3f\xff\xff\x00\x00\xff\xff\xf8\x00\x03\xff\xff\x80\x00\x00",
+  "\xff\xff\x80\xff\xff\xf8\x00\x00\xfc\x00\x03\xff\xf8\x00\x0f\xff\xff\x00\x03\x00\x00\x00\x7f\xff\xf0\x00\x3f\xff\xf0\x00\x01\xfc\x01\x00\x03\xff\x80\x1f\xff\xe3\xff\xff\xf8\x00\x1f\xff\xff\xf8\x01\xff\xdf\xff\xfb\xff\xc0\x00\x00\x3f\xff\xf8\x00\x00\x80\xc7\xff\xff\xf8\x0f\xff\x00\x60\x1f\xff\xe0\x00\x01\xff\xff\xfe\x0f\xff\xff\xfc\x00\x00\x00\xf0\x06\x03\xff\xff\xfe\x00\x00",
+  "\xff\x00\x0f\xff\xfc\x00\x0f\xff\xff\xfc\x07\xff\xfc\x00\x00\xf0\x00\x00\x80\x00\x7f\xfe\x00\x00\x0f\xff\xff\xfc\x3f\x00\xff\xff\xff\x00\x1f\xff\xff\xf0\x00\x1f\xff\xff\xe0\x00\x1f\xff\xff\xc3\xff\x00\x00\x01\xff\xff\xf0\x00\x00\x0f\xff\xe0\x07\xfc\x00\x00\x00\xfe\x00\x07\xff\xff\xf8\x00\x00\x3f\x00\x00\x0f\x80\x00\x3f\xff\xc0\x00\x11\xff\xef\x00\x07\x00\x7f\xff\xfc\x00\x00\x00",
+  "\xfe\x00\x00\x7f\xf7\xff\xff\x00\x00\x0f\xff\xff\xe0\x01\xff\xe0\x00\x00\x03\xff\xe0\x00\xff\xfe\x00\x01\xff\xf7\xff\xf8\x00\x0f\xff\x00\x00\x00\x38\x00\x07\xff\xf8\x07\xff\xfc\x00\x1f\xff\xff\x0f\xc1\xff\xff\xc0\x00\xff\xff\x0f\xff\xf0\x01\xff\xf8\x00\x01\xff\xff\x80\x00\x00\x0f\xf8\x00\x3f\xff\xfe\x3f\xff\xff\xf0\x00\x00\x38\x0f\xc3\xff\xff\xff\x1f\xff\xc0\x3f\xff\xff\xe0\x00\x00",
+  "\xff\xff\xf0\x00\x7f\xc0\x07\xff\xff\x81\xff\xc0\x00\x01\xff\xff\xff\xc0\x00\x00\x1f\xff\xfe\x03\xc0\x00\x0f\xff\xff\xfc\x00\x03\xff\xff\xff\x83\xff\xc0\x00\x07\xf0\x00\x00\x1f\x80\x00\x00\x3f\xff\xff\xe7\xff\x00\x07\xff\xff\xfc\x00\x00\x1f\xff\xf8\x00\x03\xf0\x00\xff\xfc\x00\x1f\xff\xff\xe0\x03\xff\xf0\x00\x01\xff\xff\xff\x80\x00\x00\x0f\xff\xff\xf8\x10\x00\x1e\x03\xff\xff\xff\x80\x00",
+  "\x00\x01\xff\xfe\x00\x00\x00\x7f\xff\xff\xf0\x00\x00\x1f\xff\x80\x07\xff\xff\xff\x80\x00\x01\xff\xfc\x03\xff\xff\x9f\xff\xfe\x00\x08\x00\x00\x06\x00\x00\x00\x81\xff\xff\xc0\x00\x07\xf8\x00\xff\xff\xff\x00\x00\x00\xff\xff\xf8\x00\x3f\xff\xff\xf8\x3f\xf8\x00\x00\x00\x7f\xff\xc0\x7f\xff\xff\xc0\xff\xff\xfe\x00\x00\x01\xff\xf8\x00\x00\x03\xff\xc1\xff\xf0\x00\x00\x03\x07\xf8\x01\xff\xfe\x00\x00",
+  "\xf8\x00\x00\x01\xff\xff\x80\x00\x0f\xff\xff\x8f\xff\x00\x1f\xff\x0f\xff\xc0\x00\x00\xff\xff\xfc\x00\x0f\xff\xf0\x00\x70\x00\x00\x3f\xff\xc0\x00\x00\x7c\x00\x00\x7e\x00\x0f\xfc\x00\x00\x00\xff\xff\x80\x00\x00\x0f\xff\xff\xfc\x38\x00\x00\x03\xf0\x00\x31\xf0\x1f\xff\xff\xc0\x07\xff\xff\xe0\x1f\xff\xff\xf3\xfe\x00\x00\x00\xff\xff\xc0\x00\x1f\xe7\xff\xe1\xff\xff\xdf\x00\x00\x00\x1f\xff\x00\x00\x00",
+  "\x3f\xff\xff\xf8\x00\x00\x00\x60\x00\x0f\xff\xff\xe0\x07\xff\xff\xff\x00\x00\x3f\xff\xff\xf0\x1f\xff\xff\x80\x00\x70\x00\x00\x01\x00\x00\x00\x3f\xff\xfe\x00\x00\x00\x1f\xf8\xfc\xc0\x0f\xff\xf8\x00\x3f\xff\xc0\xff\xff\x80\x00\x03\xff\xff\xf8\x00\x3f\xff\xfc\x00\x00\x0f\x81\xff\xc0\x03\xff\xc0\x3f\xff\xff\x80\x03\xff\xfe\x00\xff\xff\xfe\x00\x00\x1c\x00\x00\x00\x3f\xff\xff\xf8\x00\x7f\xff\xc0\x00\x00",
+  "\x00\x00\x00\x0f\xff\xfe\x0f\xff\xff\x87\xff\xff\xff\x00\x80\x00\x0f\xff\xc0\x00\x03\xf0\x1f\xf7\xe0\x00\x00\x70\x00\x01\xff\xff\xff\x80\x01\xfe\x07\xf0\x00\x01\xff\xfc\x00\x00\x04\x00\x01\xff\xfe\x07\xff\xff\xfe\x00\x07\xc0\x00\x00\xff\xff\xff\x87\xf0\x03\xff\xfc\x00\x00\x1f\xf8\x00\x01\xff\xff\xc0\x00\x00\x3f\xff\xc0\x00\x00\x7f\x8f\xff\xf8\x00\x00\x00\x7f\xff\xe0\x06\x0e\x00\x00\x0f\xff\xff\x80\x00",
+  "\x03\xff\xff\xfd\xe0\x00\x00\x1f\xff\xf8\x01\xff\xff\xfb\xff\xff\xe0\x01\xf0\xf0\x00\x00\xff\xff\xff\x80\x00\x00\x7f\xff\xff\xe0\x1f\xff\xff\xf0\x01\x80\xff\xff\xff\xe0\x00\x00\x7f\xff\xf0\xff\xff\xc0\x00\x00\x07\xfe\x00\x00\x00\x1e\x00\x1f\xff\xff\xe0\x1f\xff\xe0\x01\xff\xc0\x00\x3f\xff\xe0\x00\x00\x0f\xf0\x00\xff\xff\x7f\xc0\x1f\xf8\x3f\xff\xff\xc0\x00\x00\x7f\xff\xe0\xff\xfc\x00\x00\xff\x0f\xff\xff\xff",
+  "\x07\xff\xfc\x03\xff\xff\xff\xdf\xff\xff\x87\xff\x18\x00\x03\x80\x01\xff\x00\x00\x00\x1f\xff\x00\x00\x3f\xff\xff\xc0\x1f\xe0\x3f\xff\xff\xfc\x00\x00\x01\xff\xf8\x00\x00\x3f\xff\xff\xf8\x00\x00\x07\xe0\x00\x07\xff\xf9\xff\xe0\x00\x3f\xe0\x00\x7f\xef\xf0\x00\x07\x81\xff\xfc\x00\x00\x00\xff\xe0\x00\x30\x00\x00\x00\xff\xff\xf0\x00\x00\x03\xff\xfc\x7f\x07\xf8\x03\xff\xff\xff\x00\x3f\xfc\x00\x00\x01\xff\xc0\x00\x00",
+  "\x00\x7f\xfc\x00\x00\x03\xff\xf8\x00\x00\x61\xfe\x7f\xff\xfe\x00\x00\x1f\xff\xfc\x3f\xff\x80\x01\xff\xff\xff\xe0\x00\xff\xff\xff\x80\x1f\xf8\x00\x7f\xff\xff\xf8\x00\x00\x07\xff\xff\xe0\x00\x00\x07\xff\xff\xff\x80\x00\xff\x80\x0f\xff\xff\xfc\x00\x00\x7f\xff\xfe\x00\x00\x00\x30\x00\x00\x7f\x80\x00\x07\xff\xff\xf0\x00\x00\x03\xff\xc0\x0f\xff\xff\x80\x3f\xff\x80\x03\xff\xff\xfe\x03\xff\xff\xff\x7f\xfc\x1f\xf0\x00\x00",
+  "\x1f\xf0\x00\x00\x7f\xff\xfe\x02\x00\x00\x03\xff\xff\xff\xd8\x07\xff\xff\xe0\x01\xff\xff\x80\x00\x00\x07\xc0\x00\x0f\xff\xc0\x7f\xf0\x00\x07\xff\xff\x80\x00\x07\xf0\x00\x00\x7f\xfc\x03\xff\xff\xff\xc0\x00\x01\xff\xff\xf9\xff\xfe\x00\x00\x1f\xff\xc0\x00\x00\x03\xfe\x3f\xff\xff\x00\x07\xfe\x00\x00\x03\xc0\x00\x3f\xf8\x00\x10\x03\xfc\x00\x0f\xff\xc0\x00\x7f\xff\xe0\x00\xff\xf0\x00\x00\x7f\xff\xe0\x00\x00\x0f\xff\xff\xff",
+  "\xff\xff\xff\xf8\x00\x00\x60\x00\x00\x00\xff\xff\xfc\x03\xff\xfc\x00\x00\x3c\x00\x3f\xe0\x7f\xf8\x00\x07\xff\xf8\x0f\xf8\x00\x00\x7f\xff\xff\xfc\x00\x7f\xc2\x00\x03\xff\xff\xfe\x00\x01\xff\xff\xff\xf0\x03\xff\xff\xf0\x18\x07\xc0\x00\x0f\xff\xc0\x00\x00\x7f\xff\xff\x87\xe0\x00\x00\x07\x00\x1f\x80\x04\x07\xff\xe0\x00\x00\x1f\xff\x81\xff\x80\x00\x03\xff\xfc\x00\x00\x07\xff\xff\xff\xc0\x00\x00\x1f\x80\x01\xff\xff\x00\x00\x00",
+  "\x00\x0f\xfc\x1f\xf8\x00\x0f\xff\xff\xf8\x07\xff\xf1\xfc\x00\x1f\xff\xff\xf0\x00\x0f\xff\xdf\xff\xff\xff\x00\x00\x03\xff\xff\xff\x00\x01\xff\xff\xff\xc0\x10\x0f\xf0\x00\x00\x00\xfc\x00\x1f\x00\x07\x00\x01\xf0\x00\x00\x1f\xe0\x00\x00\x30\x7c\x3f\xff\xe0\x00\xff\xfc\x07\xff\xfc\x00\x3f\xff\xff\xf8\xff\xff\xc1\xfc\x1f\xff\xff\xf8\x00\x01\xff\xfc\x00\x00\x0f\xff\xff\xff\x00\x00\xff\xf8\x0c\x00\x00\x07\xff\xff\x00\x00\x00\x7f\xff",
+  "\xff\xff\x80\xff\xff\x00\x00\x00\x7f\xff\xff\x00\x1f\xfc\x00\x06\x00\x0f\xf8\x00\x00\x01\x80\x00\x00\x7f\xff\xff\xe0\x3f\xff\xff\xfc\x00\x60\x00\x00\x00\xfe\x00\x00\x07\xff\xff\xf0\x7f\xff\xff\xf8\x00\x00\x80\x00\x00\x0f\xff\xff\xff\xbf\xff\xff\xc0\x07\xff\xfe\x00\x00\x1c\x00\x1f\xfc\x07\x00\x01\xff\xff\x00\x00\x00\x80\x00\x1f\xff\x03\x80\x00\x00\x3f\xff\xff\xf8\x00\x07\xff\xff\xff\x80\x00\x1f\xff\xff\xe0\x1f\xff\xff\xc0\x00\x00",
+  "\xff\xff\xff\xf8\x00\x00\x00\x7e\x00\x00\x00\x1f\xff\x80\x07\xff\xff\xff\x80\x00\x00\x0f\xff\xff\xc3\xff\xf0\x00\x00\x04\x7f\xc0\x7f\xf0\x00\x3f\xff\x80\x00\x7f\xe0\x00\x03\xff\xc0\x00\x07\xff\x00\x00\x0f\xff\x80\x00\x00\x07\x80\x00\x00\x0f\xff\xff\xff\x01\xff\xff\xff\xc0\x03\xc0\x00\x00\x03\xff\xff\xe0\x00\x0f\xff\xff\xc0\x00\x03\xff\xfe\x00\x03\xff\xf8\x00\x00\x0f\xff\xff\xc0\x01\xff\xe0\x00\x00\xff\xff\xfc\x00\x00\x1f\xff\xff\xff",
+  "\xff\xf0\x00\x3f\xfc\x00\x00\xff\xff\xff\xe0\x1f\xc3\xfe\x00\x07\xff\xf8\x00\x0f\xf0\x01\xff\xff\xf0\x00\x00\xff\xc0\x0f\xff\xff\x80\x00\x00\xff\xff\xf3\xff\x80\x00\x00\x80\x08\x38\x00\x00\x0f\xff\xf0\x00\x1f\xff\xff\xfc\x00\x0f\x80\x00\x70\x00\x00\x31\xff\xff\xfe\x3f\xff\xf8\x00\x00\x00\x3c\x3f\xf0\x0f\xff\xff\x00\x03\xff\xfb\xff\xff\xff\x00\x0f\xff\xff\xfe\x00\x00\x00\xf0\x00\x00\x00\xff\xff\xfc\x00\x7f\xff\xf0\x00\x01\xff\xff\xfe\x00",
+  "\xff\xff\xf0\x0f\xf8\x3f\xff\xff\xe0\x00\x03\xff\xfe\x00\x00\x3f\xff\x80\xff\xff\xff\x00\x00\x00\xff\xff\xf8\xff\xff\xf0\x00\x0f\xf1\xff\xff\x00\x00\x00\x0f\xff\xfc\x00\x00\x00\x1f\xf0\x00\x00\x1f\xf0\x03\xff\xff\xff\xe1\xff\xe0\x00\x00\x1f\xff\xc1\xfe\x00\x00\x07\xff\xfc\x00\x00\x00\x1f\xfe\x3f\xc0\x00\x00\x01\xff\xfc\x00\x00\x1f\xff\xff\x0f\xe0\x00\x01\xfc\x00\xfe\x00\x00\x00\x7f\xe0\x00\x1f\xff\xff\xe0\x7f\x00\x0f\xf0\x00\xff\xfe\x00\x00",
+  "\x00\x7f\xff\xc0\x07\xff\xff\xff\x80\x07\xff\xff\xfe\x00\x00\x00\x7e\x00\x00\x00\x0f\xff\x80\x1f\xff\xfe\x07\xff\xff\xf0\x03\xc7\xff\xff\xfe\x00\x00\x00\x7f\xfe\x00\x00\x1f\xff\xfe\x00\x00\x00\xff\xfc\x00\x1f\xff\xc0\x00\x00\x3f\xff\x00\x1e\x00\x00\x03\xff\xff\xff\x80\x00\x00\x7f\xff\xf8\x03\xff\xfc\x00\x01\xff\xff\xfe\x00\x0f\xff\x02\x00\x07\xff\xff\xfe\x00\x00\x00\xfe\x01\xff\xff\xf7\xff\xff\x19\xff\xff\x00\x00\x07\xff\xc1\xff\x00\x00\x07\xff",
+  "\x00\x00\x00\x40\x00\x00\x07\xff\xff\xf8\x1f\xff\xff\xc0\x00\x3e\xff\xff\xf0\x00\x00\x00\x7f\xff\xfe\x00\x00\x00\xff\x80\x00\x00\x3f\xf1\xff\xff\xff\xe0\x00\x00\x01\xff\xff\xff\x00\x00\x1f\xff\xf8\x00\x07\xff\xff\xf8\x00\x1f\xff\xc1\xff\xff\xff\xe0\x01\xff\xff\xff\xe0\x00\x00\x07\xff\xff\xff\xcf\xff\xe0\x00\x3f\xe0\xff\xff\xc0\x00\x07\xff\xff\xe0\x01\xff\xfc\x3f\x00\x01\xff\xff\xfe\x00\x01\xff\x0f\xff\xff\xfc\x00\x00\x01\xff\xff\x80\x00\x00\x0f\xff",
+  "\xff\xff\xf0\xff\xff\xff\xc0\x03\x80\x00\x01\x00\x00\x03\xff\xff\xff\xf1\xff\xff\xff\xe0\x07\xfc\x00\x00\x03\xff\xf0\x00\x00\x00\x7e\x00\x00\x00\x07\x00\x3f\xff\xfc\x00\x0f\xc7\xff\xff\x00\x00\x07\xff\xff\xc0\x00\x00\x03\xff\xfc\x00\x00\xff\xe0\x00\x00\x00\x7f\xf0\x00\x00\xff\xff\xff\xfd\xff\x00\xff\xe0\xff\xff\xe0\x07\xff\xff\xf8\x7f\xff\xfe\x18\x00\x00\x01\xf0\x00\x1f\xff\xfe\x01\xc0\x01\xff\xff\xff\xf8\x00\x01\xfe\xff\xff\xff\x80\x00\x00\x07\xff\xff",
+  "\xff\xff\xf0\x00\x1f\xff\xff\x00\x00\x3f\xff\xf0\x00\xff\xff\x00\x07\xff\xff\xf8\x03\xff\xff\xe7\xff\xff\xff\x81\x00\x00\x01\xff\x00\x00\x3f\xff\xff\xf8\x00\x00\x00\xf0\x07\xff\xc0\x0f\xf0\x00\x3f\xff\xc0\x00\x7f\xf8\x00\xff\xfc\x00\x00\x1f\xff\x80\x1f\xfc\x00\x00\x01\xff\xff\x00\x00\x7f\xff\xff\xfe\x00\x00\x0f\xff\x80\x00\x00\x0f\xff\xff\xfc\x00\x00\x00\xff\xff\xfc\x00\x0f\xff\x80\x00\x3f\xff\xfe\x00\x00\x3f\xe0\x0f\xff\xff\x00\x00\x01\xff\xf0\x00\x00\x00",
+  "\xff\xc0\x00\x00\x03\xff\xff\xfc\x00\xff\xff\xfc\x00\x00\x00\x7f\xff\xff\x00\x00\x00\x1f\xe0\x00\x0f\xff\xc0\xf0\x00\x00\x7f\xff\xff\xe0\x00\x20\x1f\xff\xff\xff\x00\x00\x00\x1f\x80\x00\x00\x07\xff\xf1\xff\xff\xff\xc0\x00\x00\x1f\xff\xff\xf0\x00\x3f\xff\xf8\x00\x3f\xff\xff\xfe\x01\xff\xff\xfe\x7f\x9e\x00\x1f\xff\xfc\x00\x7f\xe0\x7f\xff\xff\xe0\x00\x7f\xff\xfe\x00\x00\x01\xff\xff\xff\xf8\x01\xff\xc0\x03\x00\x0f\xff\xf8\x00\x00\x0f\xf0\x0f\xff\x00\x00\x00\x0f\xff",
+  "\x00\x03\xff\xff\xff\xcf\xff\xf8\x7f\x8f\xff\xff\xfc\x01\xff\xff\xfc\x00\x00\x1f\xff\xff\xff\x80\x00\x00\x01\xff\xff\xe1\xff\xf0\x00\x00\x00\xff\xff\xff\xf8\x03\x80\x00\x3f\x80\x00\x0f\xff\xff\xff\xc0\x00\x00\x02\x7f\xff\xf8\x03\xff\xc0\x00\x00\x3f\xff\x80\x00\x00\x01\xff\xff\xe0\x00\x00\x03\x80\x00\x00\xff\xe0\x7f\xff\xff\xfc\x00\x00\x01\xff\xff\xfc\x00\x00\x00\xff\xff\x80\x00\x07\xfe\x00\x00\x07\xff\xf0\x00\x00\x1f\x80\x00\x00\x3e\x1f\xff\xff\xff\x9f\xff\xff\xff",
+  "\xff\xff\xfe\x00\x03\xff\xff\xff\x80\x01\xff\xff\xff\xa0\x3f\xff\xf8\x00\x7f\x03\xff\xff\xc0\x00\x0f\xff\xc3\xff\xf8\x00\x03\xff\xff\xff\xc0\x7f\xf0\x1f\xe0\x0f\xff\xff\xc0\x00\x1f\xfe\x0f\xff\xff\xe0\x00\x07\xff\xff\xfc\x00\x00\xfc\x00\x3f\xff\xff\xf0\x00\x00\x00\x3f\xfc\x00\x00\x00\x0f\xff\xc4\x00\x00\xff\xc0\x00\x03\xff\xff\xff\x80\x00\x03\xfc\x0f\xff\xff\xf0\x00\x00\x03\xff\xff\xc0\x07\xff\xff\xf8\x0c\x3f\xff\xf0\x00\x1f\xff\x80\x00\x00\x01\xff\xfe\x00\x00\x3f\xff",
+  "\x00\x00\x00\x1f\xff\xc3\xff\xff\x80\x00\x00\x3f\xff\xff\xfe\x00\x00\x00\x0f\xe0\x00\x7f\xff\xe0\x0f\xfe\x00\xff\xff\xff\xe0\x03\xff\xf8\x00\x00\x00\x3f\xe0\x00\x00\x01\xff\xff\xe7\xff\xff\xff\xe0\x00\x00\x07\xff\xff\xc0\x00\x1f\xe0\x00\x00\x01\xff\xff\x00\x00\x03\xff\xff\xff\xe0\x00\x00\x1f\xe0\x03\xff\xff\x00\x00\x00\x07\xff\xf0\x3f\x80\x00\x00\x0f\xff\xff\xfe\x00\x00\xff\xff\xff\x00\x00\x3f\xff\xff\x80\x0f\xff\xfe\x01\xff\xff\xff\xf8\x3f\xff\xff\xc0\x00\x00\xff\xff\xff",
+  "\xff\xff\xff\x80\x00\xff\xff\xff\xf0\x00\x00\x1f\x00\x3f\xff\xfe\x0f\xf0\x1c\x00\xff\xff\xe0\x00\x0f\xc0\x1f\xff\xc0\x00\x00\x01\xff\x80\xf7\xff\xf8\x00\x00\x3f\xff\xc0\x00\x00\x01\xff\xf0\x00\x03\xe3\xfc\x00\x07\xf8\x00\x00\x3f\xff\xff\xfe\x00\x00\x1f\x00\x00\x00\x18\x00\x3f\xff\xff\xf8\x0f\xe0\x00\x00\x00\x60\x00\x07\xff\xff\xfe\x00\x60\xff\xff\xff\xf8\x00\x00\x3f\xff\x80\x00\x00\x3f\xff\xff\xf0\x00\x00\x0f\xf0\x00\xff\xff\xf1\xff\x00\x3f\xff\xff\xff\x01\xff\xe0\x00\x00\x00",
+  "\xff\xff\xff\xfc\x00\x00\x00\xff\xff\xf0\x01\xff\xff\xf8\x00\x00\xff\xff\xff\xe7\xff\xf8\x01\xf8\x7f\xff\xff\x80\x00\x3f\xff\xfc\x00\x00\x01\xff\xff\xe0\x00\x3f\xc0\x00\x00\x7f\xff\xff\x00\x00\x0f\xff\xff\xff\xc0\x00\x0f\xff\xf1\xff\xff\xff\xf8\x00\x0f\xff\xff\xfc\x00\x00\x07\xff\xff\xff\x80\xff\xff\xf8\x07\xf0\x00\x00\x00\x1f\xff\xff\xff\xc0\x00\xff\xff\xff\xc0\x00\x0e\xff\xff\xff\xc0\x00\x00\x03\xff\xf8\x00\x00\x03\xff\x80\x00\x00\x00\xe0\x00\x00\x0f\xf8\x00\x00\x3f\xff\xff\xff",
+  "\x3f\x00\x03\xc0\x1f\x00\x00\x00\x0f\xf0\x00\x07\xff\xff\xef\x00\xfe\x00\x7f\xfe\x00\x00\x3f\xf0\x00\x3f\xc0\x00\x00\x07\xfc\x00\x00\x03\xff\xff\xff\x0f\xff\xff\xff\xc0\x07\xf8\x00\x00\xf8\x00\x00\x3f\x9f\xff\xff\xfc\x00\x00\x3f\xff\xc0\x00\x03\xff\xff\xc0\x00\x00\xff\xfc\x00\x1f\xff\xe0\x00\x00\x07\xff\xe0\x07\x00\x00\x00\x7f\xff\xfe\x00\x00\x00\xff\xff\xfe\x00\x00\x0f\xfc\x00\x07\xff\xf0\x00\x00\x00\x7f\x80\x03\xcf\xff\x80\x00\x01\xff\xff\xe0\x3c\x00\x00\x3f\xff\xff\xff\x80\x00\x00",
+  "\x30\x00\x00\x00\xff\xf8\x00\x00\xff\xfc\x00\x3f\xff\xff\x80\x00\x00\x0f\xcf\xff\xcf\xff\xff\xc0\xff\xff\xff\x80\x00\x00\x01\xff\xff\xff\xf0\x00\x00\x00\x1f\xff\x00\x03\xff\xfc\x00\x00\x00\x07\xff\x80\x00\x7f\xff\xff\xfe\x0f\xff\xc0\x00\x00\x00\x7f\xf0\xbf\xff\xff\xe0\x00\x00\xff\xf8\x00\x00\x01\xff\xff\xf8\x00\x00\x3e\x00\x00\x07\xff\xe0\x00\x00\x00\x80\x00\x03\x80\x01\xff\xff\xff\xf8\x60\x00\x00\x00\xff\xf0\x00\x1f\xff\xff\xc7\xff\xf0\x40\xff\xff\xfe\x00\x00\x07\xff\xdf\xff\x80\x00\x00",
+  "\xff\xff\xff\x83\xff\xf8\x1f\xff\x1f\xff\xff\x80\x0f\xff\xff\xe0\x00\x00\x00\x3f\xff\xff\xc0\xf8\x00\x00\x78\x00\x1c\x00\x00\x00\x1f\xe0\x00\x00\x00\xff\xff\xe0\x3f\xff\xff\xfe\x00\x00\x03\xff\xff\xfe\x00\x00\x00\x1f\xff\xfc\x00\x7f\xff\x00\x00\x00\x1f\xff\xff\x00\x02\x00\x00\x3f\xff\xfc\x00\x00\x00\xff\xf0\x1f\xfe\xff\xff\xc0\x01\xff\xff\xff\xf0\x00\x00\x00\xff\xff\xfe\x00\x00\x00\x0f\xff\xc0\x00\x00\x7f\x7f\xc0\x00\x00\x01\xff\xff\xfe\x0f\xff\xff\xff\xc0\x00\x0f\x80\x00\x00\x3f\xff\xff\xff",
+  "\xff\xff\xff\xfe\x00\x00\x00\xff\xf8\x0f\xff\xff\xf0\x00\x00\x07\xff\xff\xfb\xff\xc0\x00\x07\xfc\x07\xe0\x00\x00\x01\xff\xff\xe0\x00\x7f\xff\xff\xf8\x00\x00\x3f\xff\xf8\x00\x00\x00\x0f\xff\xff\x00\x00\x0f\xc0\x00\x00\x3f\xff\xf0\x00\x00\x01\xff\xff\xfe\x1f\xff\xf8\x00\x00\x20\x00\x00\x00\x3f\xff\xbf\xff\x9f\xff\xff\xfc\x3f\xff\xff\xf0\x00\x00\x07\x80\x00\x00\xff\xff\xe7\xff\xff\xff\xf0\x00\x00\x3f\xff\xff\xff\x00\x00\x07\xff\xff\xe0\xff\xff\xff\xe0\x01\xff\xff\xff\xf8\x00\x00\x00\x7f\xff\xff\xff",
+  "\x00\x00\x00\x1f\xff\xc0\x00\x0f\xff\xc0\x00\x00\x1f\xff\xc0\x00\x1f\xc0\x00\x00\x03\xff\x80\x00\x00\x07\xff\xff\xff\xc0\x00\x00\x00\x80\x00\x3f\xfc\x00\xc0\x00\x0f\xff\xff\x00\x00\x06\x00\x3f\xfc\x1e\x00\x1f\xff\xff\xf0\x00\x3e\x0f\xff\xff\xf0\x00\x00\x3f\xff\xf0\x00\x00\x00\x1f\xff\xc0\x00\x00\xff\xff\xff\xf8\x00\xe0\x00\x00\xff\xff\xff\xfe\x00\x00\x0f\xff\xff\xfe\x3f\xff\xff\xfc\x07\xfe\x00\x00\x00\xc0\x00\x7f\xff\xff\xfe\x00\xff\xff\xff\xfc\x07\xff\xff\xe0\x00\x3f\xe3\xff\xff\xc0\x00\x00\x3f\xff",
+  "\x00\x00\x00\x0f\xff\xc0\x00\x0f\xf8\x00\x00\x00\xff\xfc\x00\x00\x0f\xff\xff\xfe\x00\x00\x00\x03\xff\x00\xff\xfc\x07\xff\xf0\x1f\xff\xfe\x0f\xff\xff\xfd\xff\xff\xff\xf0\x1f\xff\xf0\x00\x07\xf8\xff\xf8\x00\x00\x00\xff\xff\xc0\x3f\xff\xff\xff\x80\x00\x00\x0f\xff\xff\xff\x00\x00\x03\xff\xff\xf0\x00\x07\xff\xff\x00\x00\x3f\xff\xf0\x01\xff\xff\xc0\x01\xff\xff\xff\x00\x3f\xff\xf8\x1f\xff\xff\xfe\x1f\xff\xff\xff\xc0\x00\x00\x7f\xe0\x00\x07\xff\xff\xfe\x00\x00\x00\x03\xe0\x07\xff\xc0\x03\xfc\x00\x07\xff\xff\xff",
+  "\xff\xff\xff\x00\x00\x03\xc0\x00\x00\x01\xff\xff\xf8\x00\x00\x00\x0f\xff\xff\xff\x00\x00\x0f\x00\x00\xff\xff\xf8\x80\x00\xf8\x00\x0f\xc0\x00\x00\x00\xe0\x00\x00\x00\xff\xff\xff\xf8\x0f\xff\xff\xfe\x00\x00\x18\x00\x00\x7f\xff\xff\xff\x00\x00\x03\xff\xff\xff\x00\x7f\xff\xff\xfc\x00\x03\xc0\x00\x00\x0f\xff\xff\xff\xf0\x00\x07\xff\xff\x80\x01\xff\xff\xff\xe0\x00\x0f\xff\xfe\x07\xff\xff\xf8\x00\xff\xff\xff\xc0\x00\x00\x03\xe0\x00\x07\xff\xf0\x0f\xff\xf0\x00\x00\xff\xff\xf8\x7f\xc0\x03\xc0\x3f\xff\xe0\x00\x00\x00",
+
+  /* These are zero-terminated strings */
+
+  "abc",
+  "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
+};
+
+static uschar *hashes[] = {
+  "DA39A3EE5E6B4B0D3255BFEF95601890AFD80709",
+  "3CDF2936DA2FC556BFA533AB1EB59CE710AC80E5",
+  "19C1E2048FA7393CFBF2D310AD8209EC11D996E5",
+  "CA775D8C80FAA6F87FA62BECA6CA6089D63B56E5",
+  "71AC973D0E4B50AE9E5043FF4D615381120A25A0",
+  "A6B5B9F854CFB76701C3BDDBF374B3094EA49CBA",
+  "D87A0EE74E4B9AD72E6847C87BDEEB3D07844380",
+  "1976B8DD509FE66BF09C9A8D33534D4EF4F63BFD",
+  "5A78F439B6DB845BB8A558E4CEB106CD7B7FF783",
+  "F871BCE62436C1E280357416695EE2EF9B83695C",
+  "62B243D1B780E1D31CF1BA2DE3F01C72AEEA0E47",
+  "1698994A273404848E56E7FDA4457B5900DE1342",
+  "056F4CDC02791DA7ED1EB2303314F7667518DEEF",
+  "9FE2DA967BD8441EEA1C32DF68DDAA9DC1FC8E4B",
+  "73A31777B4ACE9384EFA8BBEAD45C51A71ABA6DD",
+  "3F9D7C4E2384EDDABFF5DD8A31E23DE3D03F42AC",
+  "4814908F72B93FFD011135BEE347DE9A08DA838F",
+  "0978374B67A412A3102C5AA0B10E1A6596FC68EB",
+  "44AD6CB618BD935460D46D3F921D87B99AB91C1E",
+  "02DC989AF265B09CF8485640842128DCF95E9F39",
+  "67507B8D497B35D6E99FC01976D73F54AECA75CF",
+  "1EAE0373C1317CB60C36A42A867B716039D441F5",
+  "9C3834589E5BFFAC9F50950E0199B3EC2620BEC8",
+  "209F7ABC7F3B878EE46CDF3A1FBB9C21C3474F32",
+  "05FC054B00D97753A9B3E2DA8FBBA3EE808CEF22",
+  "0C4980EA3A46C757DFBFC5BAA38AC6C8E72DDCE7",
+  "96A460D2972D276928B69864445BEA353BDCFFD2",
+  "F3EF04D8FA8C6FA9850F394A4554C080956FA64B",
+  "F2A31D875D1D7B30874D416C4D2EA6BAF0FFBAFE",
+  "F4942D3B9E9588DCFDC6312A84DF75D05F111C20",
+  "310207DF35B014E4676D30806FA34424813734DD",
+  "4DA1955B2FA7C7E74E3F47D7360CE530BBF57CA3",
+  "74C4BC5B26FB4A08602D40CCEC6C6161B6C11478",
+  "0B103CE297338DFC7395F7715EE47539B556DDB6",
+  "EFC72D99E3D2311CE14190C0B726BDC68F4B0821",
+  "660EDAC0A8F4CE33DA0D8DBAE597650E97687250",
+  "FE0A55A988B3B93946A63EB36B23785A5E6EFC3E",
+  "0CBDF2A5781C59F907513147A0DE3CC774B54BF3",
+  "663E40FEE5A44BFCB1C99EA5935A6B5BC9F583B0",
+  "00162134256952DD9AE6B51EFB159B35C3C138C7",
+  "CEB88E4736E354416E2010FC1061B3B53B81664B",
+  "A6A2C4B6BCC41DDC67278F3DF4D8D0B9DD7784EF",
+  "C23D083CD8820B57800A869F5F261D45E02DC55D",
+  "E8AC31927B78DDEC41A31CA7A44EB7177165E7AB",
+  "E864EC5DBAB0F9FF6984AB6AD43A8C9B81CC9F9C",
+  "CFED6269069417A84D6DE2347220F4B858BCD530",
+  "D9217BFB46C96348722C3783D29D4B1A3FEDA38C",
+  "DEC24E5554F79697218D317315FA986229CE3350",
+  "83A099DF7071437BA5495A5B0BFBFEFE1C0EF7F3",
+  "AA3198E30891A83E33CE3BFA0587D86A197D4F80",
+  "9B6ACBEB4989CBEE7015C7D515A75672FFDE3442",
+  "B021EB08A436B02658EAA7BA3C88D49F1219C035",
+  "CAE36DAB8AEA29F62E0855D9CB3CD8E7D39094B1",
+  "02DE8BA699F3C1B0CB5AD89A01F2346E630459D7",
+  "88021458847DD39B4495368F7254941859FAD44B",
+  "91A165295C666FE85C2ADBC5A10329DAF0CB81A0",
+  "4B31312EAF8B506811151A9DBD162961F7548C4B",
+  "3FE70971B20558F7E9BAC303ED2BC14BDE659A62",
+  "93FB769D5BF49D6C563685954E2AECC024DC02D6",
+  "BC8827C3E614D515E83DEA503989DEA4FDA6EA13",
+  "E83868DBE4A389AB48E61CFC4ED894F32AE112AC",
+  "55C95459CDE4B33791B4B2BCAAF840930AF3F3BD",
+  "36BB0E2BA438A3E03214D9ED2B28A4D5C578FCAA",
+  "3ACBF874199763EBA20F3789DFC59572ACA4CF33",
+  "86BE037C4D509C9202020767D860DAB039CADACE",
+  "51B57D7080A87394EEC3EB2E0B242E553F2827C9",
+  "1EFBFA78866315CE6A71E457F3A750A38FACAB41",
+  "57D6CB41AEEC20236F365B3A490C61D0CFA39611",
+  "C532CB64B4BA826372BCCF2B4B5793D5B88BB715",
+  "15833B5631032663E783686A209C6A2B47A1080E",
+  "D04F2043C96E10CD83B574B1E1C217052CD4A6B2",
+  "E8882627C64DB743F7DB8B4413DD033FC63BEB20",
+  "CD2D32286B8867BC124A0AF2236FC74BE3622199",
+  "019B70D745375091ED5C7B218445EC986D0F5A82",
+  "E5FF5FEC1DADBAED02BF2DAD4026BE6A96B3F2AF",
+  "6F4E23B3F2E2C068D13921FE4E5E053FFED4E146",
+  "25E179602A575C915067566FBA6DA930E97F8678",
+  "67DED0E68E235C8A523E051E86108EEB757EFBFD",
+  "AF78536EA83C822796745556D62A3EE82C7BE098",
+  "64D7AC52E47834BE72455F6C64325F9C358B610D",
+  "9D4866BAA3639C13E541F250FFA3D8BC157A491F",
+  "2E258811961D3EB876F30E7019241A01F9517BEC",
+  "8E0EBC487146F83BC9077A1630E0FB3AB3C89E63",
+  "CE8953741FFF3425D2311FBBF4AB481B669DEF70",
+  "789D1D2DAB52086BD90C0E137E2515ED9C6B59B5",
+  "B76CE7472700DD68D6328B7AA8437FB051D15745",
+  "F218669B596C5FFB0B1C14BD03C467FC873230A0",
+  "1FF3BDBE0D504CB0CDFAB17E6C37ABA6B3CFFDED",
+  "2F3CBACBB14405A4652ED52793C1814FD8C4FCE0",
+  "982C8AB6CE164F481915AF59AAED9FFF2A391752",
+  "5CD92012D488A07ECE0E47901D0E083B6BD93E3F",
+  "69603FEC02920851D4B3B8782E07B92BB2963009",
+  "3E90F76437B1EA44CF98A08D83EA24CECF6E6191",
+  "34C09F107C42D990EB4881D4BF2DDDCAB01563AE",
+  "474BE0E5892EB2382109BFC5E3C8249A9283B03D",
+  "A04B4F75051786682483252438F6A75BF4705EC6",
+  "BE88A6716083EB50ED9416719D6A247661299383",
+  "C67E38717FEE1A5F65EC6C7C7C42AFC00CD37F04",
+  "959AC4082388E19E9BE5DE571C047EF10C174A8D",
+  "BAA7AA7B7753FA0ABDC4A541842B5D238D949F0A",
+  "351394DCEBC08155D100FCD488578E6AE71D0E9C",
+  "AB8BE94C5AF60D9477EF1252D604E58E27B2A9EE",
+  "3429EC74A695FDD3228F152564952308AFE0680A",
+  "907FA46C029BC67EAA8E4F46E3C2A232F85BD122",
+  "2644C87D1FBBBC0FC8D65F64BCA2492DA15BAAE4",
+  "110A3EEB408756E2E81ABAF4C5DCD4D4C6AFCF6D",
+  "CD4FDC35FAC7E1ADB5DE40F47F256EF74D584959",
+  "8E6E273208AC256F9ECCF296F3F5A37BC8A0F9F7",
+  "FE0606100BDBC268DB39B503E0FDFE3766185828",
+  "6C63C3E58047BCDB35A17F74EEBA4E9B14420809",
+  "BCC2BD305F0BCDA8CF2D478EF9FE080486CB265F",
+  "CE5223FD3DD920A3B666481D5625B16457DCB5E8",
+  "948886776E42E4F5FAE1B2D0C906AC3759E3F8B0",
+  "4C12A51FCFE242F832E3D7329304B11B75161EFB",
+  "C54BDD2050504D92F551D378AD5FC72C9ED03932",
+  "8F53E8FA79EA09FD1B682AF5ED1515ECA965604C",
+  "2D7E17F6294524CE78B33EAB72CDD08E5FF6E313",
+  "64582B4B57F782C9302BFE7D07F74AA176627A3A",
+  "6D88795B71D3E386BBD1EB830FB9F161BA98869F",
+  "86AD34A6463F12CEE6DE9596ABA72F0DF1397FD1",
+  "7EB46685A57C0D466152DC339C8122548C757ED1",
+  "E7A98FB0692684054407CC221ABC60C199D6F52A",
+  "34DF1306662206FD0A5FC2969A4BEEC4EB0197F7",
+  "56CF7EBF08D10F0CB9FE7EE3B63A5C3A02BCB450",
+  "3BAE5CB8226642088DA760A6F78B0CF8EDDEA9F1",
+  "6475DF681E061FA506672C27CBABFA9AA6DDFF62",
+  "79D81991FA4E4957C8062753439DBFD47BBB277D",
+  "BAE224477B20302E881F5249F52EC6C34DA8ECEF",
+  "EDE4DEB4293CFE4138C2C056B7C46FF821CC0ACC",
+
+  "A9993E364706816ABA3E25717850C26C9CD0D89D",
+  "84983E441C3BD26EBAAE4AA1F95129E5E54670F1"
+};
+
+static uschar *atest = "34AA973CD4C4DAA4F61EEB2BDBAD27316534016F";
+
+int main(void)
+{
+sha1 base;
+int j;
+int i = 0x01020304;
+uschar *ctest = (uschar *)(&i);
+uschar buffer[256];
+uschar digest[20];
+uschar s[41];
+printf("Checking sha1: %s-endian\n\n", (ctest[0] == 0x04)? "little" : "big");
+
+for (i = 0; i < sizeof(tests)/sizeof(uschar *); i ++)
+  {
+  printf("%d.\nShould be: %s\n", i, hashes[i]);
+  sha1_start(&base);
+  sha1_end(&base, tests[i], (i <= 128)? i : strlen(tests[i]), digest);
+  for (j = 0; j < 20; j++) sprintf(s+2*j, "%02X", digest[j]);
+  printf("Computed:  %s\n", s);
+  if (strcmp(s, hashes[i]) != 0) printf("*** No match ***\n");
+  printf("\n");
+  }
+
+/* 1 000 000 repetitions of "a" */
+
+ctest = malloc(1000000);
+memset(ctest, 'a', 1000000);
+
+printf("1 000 000 repetitions of 'a'\n");
+printf("Should be: %s\n", atest);
+sha1_start(&base);
+sha1_end(&base, ctest, 1000000, digest);
+for (j = 0; j < 20; j++) sprintf(s+2*j, "%02X", digest[j]);
+printf("Computed:  %s\n", s);
+if (strcmp(s, atest) != 0) printf("*** No match ***\n");
+
+}
+#endif
+
+/* End of sha1.c */
diff --git a/src/auths/spa.c b/src/auths/spa.c
new file mode 100644 (file)
index 0000000..0bf7b04
--- /dev/null
@@ -0,0 +1,369 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* This file, which provides support for Microsoft's Secure Password
+Authentication, was contributed by Marc Prud'hommeaux. Tom Kistner added SPA
+server support. I (PH) have only modified it in very trivial ways.
+
+References:
+  http://www.innovation.ch/java/ntlm.html
+  http://www.kuro5hin.org/story/2002/4/28/1436/66154
+  http://download.microsoft.com/download/9/5/e/95ef66af-9026-4bb0-a41d-a4f81802d92c/%5bMS-SMTP%5d.pdf
+
+ * It seems that some systems have existing but different definitions of some
+ * of the following types. I received a complaint about "int16" causing
+ * compilation problems. So I (PH) have renamed them all, to be on the safe
+ * side, by adding 'x' on the end. See auths/auth-spa.h.
+
+ * typedef signed short int16;
+ * typedef unsigned short uint16;
+ * typedef unsigned uint32;
+ * typedef unsigned char  uint8;
+
+07-August-2003:  PH: Patched up the code to avoid assert bombouts for stupid
+                     input data. Find appropriate comment by grepping for "PH".
+16-October-2006: PH: Added a call to auth_check_serv_cond() at the end
+05-June-2010:    PP: handle SASL initial response
+*/
+
+
+#include "../exim.h"
+#include "spa.h"
+
+/* #define DEBUG_SPA */
+
+#ifdef DEBUG_SPA
+#define DSPA(x,y,z)   debug_printf(x,y,z)
+#else
+#define DSPA(x,y,z)
+#endif
+
+/* Options specific to the spa authentication mechanism. */
+
+optionlist auth_spa_options[] = {
+  { "client_domain",             opt_stringptr,
+      (void *)(offsetof(auth_spa_options_block, spa_domain)) },
+  { "client_password",           opt_stringptr,
+      (void *)(offsetof(auth_spa_options_block, spa_password)) },
+  { "client_username",           opt_stringptr,
+      (void *)(offsetof(auth_spa_options_block, spa_username)) },
+  { "server_password",           opt_stringptr,
+      (void *)(offsetof(auth_spa_options_block, spa_serverpassword)) }
+};
+
+/* Size of the options list. An extern variable has to be used so that its
+address can appear in the tables drtables.c. */
+
+int auth_spa_options_count =
+  sizeof(auth_spa_options)/sizeof(optionlist);
+
+/* Default private options block for the contidion authentication method. */
+
+auth_spa_options_block auth_spa_option_defaults = {
+  NULL,              /* spa_password */
+  NULL,              /* spa_username */
+  NULL,              /* spa_domain */
+  NULL               /* spa_serverpassword (for server side use) */
+};
+
+
+/*************************************************
+*          Initialization entry point            *
+*************************************************/
+
+/* Called for each instance, after its options have been read, to
+enable consistency checks to be done, or anything else that needs
+to be set up. */
+
+void
+auth_spa_init(auth_instance *ablock)
+{
+auth_spa_options_block *ob =
+  (auth_spa_options_block *)(ablock->options_block);
+
+/* The public name defaults to the authenticator name */
+
+if (ablock->public_name == NULL) ablock->public_name = ablock->name;
+
+/* Both username and password must be set for a client */
+
+if ((ob->spa_username == NULL) != (ob->spa_password == NULL))
+  log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s authenticator:\n  "
+      "one of client_username and client_password cannot be set without "
+      "the other", ablock->name);
+ablock->client = ob->spa_username != NULL;
+
+/* For a server we have just one option */
+
+ablock->server = ob->spa_serverpassword != NULL;
+}
+
+
+
+/*************************************************
+*             Server entry point                 *
+*************************************************/
+
+/* For interface, see auths/README */
+
+#define CVAL(buf,pos) (((unsigned char *)(buf))[pos])
+#define PVAL(buf,pos) ((unsigned)CVAL(buf,pos))
+#define SVAL(buf,pos) (PVAL(buf,pos)|PVAL(buf,(pos)+1)<<8)
+#define IVAL(buf,pos) (SVAL(buf,pos)|SVAL(buf,(pos)+2)<<16)
+
+int
+auth_spa_server(auth_instance *ablock, uschar *data)
+{
+auth_spa_options_block *ob = (auth_spa_options_block *)(ablock->options_block);
+uint8x lmRespData[24];
+uint8x ntRespData[24];
+SPAAuthRequest request;
+SPAAuthChallenge challenge;
+SPAAuthResponse  response;
+SPAAuthResponse  *responseptr = &response;
+uschar msgbuf[2048];
+uschar *clearpass;
+
+/* send a 334, MS Exchange style, and grab the client's request,
+unless we already have it via an initial response. */
+
+if ((*data == '\0') &&
+    (auth_get_no64_data(&data, US"NTLM supported") != OK))
+  {
+  /* something borked */
+  return FAIL;
+  }
+
+if (spa_base64_to_bits((char *)(&request), sizeof(request), (const char *)(data)) < 0)
+  {
+  DEBUG(D_auth) debug_printf("auth_spa_server(): bad base64 data in "
+  "request: %s\n", data);
+  return FAIL;
+  }
+
+/* create a challenge and send it back */
+
+spa_build_auth_challenge(&request,&challenge);
+spa_bits_to_base64 (msgbuf, (unsigned char*)&challenge,
+    spa_request_length(&challenge));
+
+if (auth_get_no64_data(&data, msgbuf) != OK)
+  {
+  /* something borked */
+  return FAIL;
+  }
+
+/* dump client response */
+if (spa_base64_to_bits((char *)(&response), sizeof(response), (const char *)(data)) < 0)
+  {
+  DEBUG(D_auth) debug_printf("auth_spa_server(): bad base64 data in "
+  "response: %s\n", data);
+  return FAIL;
+  }
+
+/***************************************************************
+PH 07-Aug-2003: The original code here was this:
+
+Ustrcpy(msgbuf, unicodeToString(((char*)responseptr) +
+  IVAL(&responseptr->uUser.offset,0),
+  SVAL(&responseptr->uUser.len,0)/2) );
+
+However, if the response data is too long, unicodeToString bombs out on
+an assertion failure. It uses a 1024 fixed buffer. Bombing out is not a good
+idea. It's too messy to try to rework that function to return an error because
+it is called from a number of other places in the auth-spa.c module. Instead,
+since it is a very small function, I reproduce its code here, with a size check
+that causes failure if the size of msgbuf is exceeded. ****/
+
+  {
+  int i;
+  char *p = ((char*)responseptr) + IVAL(&responseptr->uUser.offset,0);
+  int len = SVAL(&responseptr->uUser.len,0)/2;
+
+  if (len + 1 >= sizeof(msgbuf)) return FAIL;
+  for (i = 0; i < len; ++i)
+    {
+    msgbuf[i] = *p & 0x7f;
+    p += 2;
+    }
+  msgbuf[i] = 0;
+  }
+
+/***************************************************************/
+
+/* Put the username in $auth1 and $1. The former is now the preferred variable;
+the latter is the original variable. These have to be out of stack memory, and
+need to be available once known even if not authenticated, for error messages
+(server_set_id, which only makes it to authenticated_id if we return OK) */
+
+auth_vars[0] = expand_nstring[1] = string_copy(msgbuf);
+expand_nlength[1] = Ustrlen(msgbuf);
+expand_nmax = 1;
+
+debug_print_string(ablock->server_debug_string);    /* customized debug */
+
+/* look up password */
+
+clearpass = expand_string(ob->spa_serverpassword);
+if (clearpass == NULL)
+  {
+  if (expand_string_forcedfail)
+    {
+    DEBUG(D_auth) debug_printf("auth_spa_server(): forced failure while "
+      "expanding spa_serverpassword\n");
+    return FAIL;
+    }
+  else
+    {
+    DEBUG(D_auth) debug_printf("auth_spa_server(): error while expanding "
+      "spa_serverpassword: %s\n", expand_string_message);
+    return DEFER;
+    }
+  }
+
+/* create local hash copy */
+
+spa_smb_encrypt (clearpass, challenge.challengeData, lmRespData);
+spa_smb_nt_encrypt (clearpass, challenge.challengeData, ntRespData);
+
+/* compare NT hash (LM may not be available) */
+
+if (memcmp(ntRespData,
+      ((unsigned char*)responseptr)+IVAL(&responseptr->ntResponse.offset,0),
+      24) == 0)
+  /* success. we have a winner. */
+  {
+  return auth_check_serv_cond(ablock);
+  }
+
+  /* Expand server_condition as an authorization check (PH) */
+
+return FAIL;
+}
+
+
+/*************************************************
+*              Client entry point                *
+*************************************************/
+
+/* For interface, see auths/README */
+
+int
+auth_spa_client(
+  auth_instance *ablock,                 /* authenticator block */
+  smtp_inblock *inblock,                 /* connection inblock */
+  smtp_outblock *outblock,               /* connection outblock */
+  int timeout,                           /* command timeout */
+  uschar *buffer,                        /* buffer for reading response */
+  int buffsize)                          /* size of buffer */
+{
+       auth_spa_options_block *ob =
+               (auth_spa_options_block *)(ablock->options_block);
+       SPAAuthRequest   request;
+       SPAAuthChallenge challenge;
+       SPAAuthResponse  response;
+       char msgbuf[2048];
+       char *domain = NULL;
+       char *username, *password;
+
+       /* Code added by PH to expand the options */
+
+       *buffer = 0;    /* Default no message when cancelled */
+
+       username = CS expand_string(ob->spa_username);
+       if (username == NULL)
+         {
+         if (expand_string_forcedfail) return CANCELLED;
+         string_format(buffer, buffsize, "expansion of \"%s\" failed in %s "
+           "authenticator: %s", ob->spa_username, ablock->name,
+           expand_string_message);
+         return ERROR;
+         }
+
+       password = CS expand_string(ob->spa_password);
+       if (password == NULL)
+         {
+         if (expand_string_forcedfail) return CANCELLED;
+         string_format(buffer, buffsize, "expansion of \"%s\" failed in %s "
+           "authenticator: %s", ob->spa_password, ablock->name,
+           expand_string_message);
+         return ERROR;
+         }
+
+       if (ob->spa_domain != NULL)
+         {
+         domain = CS expand_string(ob->spa_domain);
+         if (domain == NULL)
+           {
+           if (expand_string_forcedfail) return CANCELLED;
+           string_format(buffer, buffsize, "expansion of \"%s\" failed in %s "
+             "authenticator: %s", ob->spa_domain, ablock->name,
+             expand_string_message);
+           return ERROR;
+           }
+         }
+
+       /* Original code */
+
+    if (smtp_write_command(outblock, FALSE, "AUTH %s\r\n",
+         ablock->public_name) < 0)
+               return FAIL_SEND;
+
+       /* wait for the 3XX OK message */
+       if (!smtp_read_response(inblock, (uschar *)buffer, buffsize, '3', timeout))
+               return FAIL;
+
+       DSPA("\n\n%s authenticator: using domain %s\n\n",
+               ablock->name, domain);
+
+       spa_build_auth_request (&request, CS username, domain);
+       spa_bits_to_base64 (US msgbuf, (unsigned char*)&request,
+               spa_request_length(&request));
+
+       DSPA("\n\n%s authenticator: sending request (%s)\n\n", ablock->name,
+               msgbuf);
+
+       /* send the encrypted password */
+       if (smtp_write_command(outblock, FALSE, "%s\r\n", msgbuf) < 0)
+               return FAIL_SEND;
+
+       /* wait for the auth challenge */
+       if (!smtp_read_response(inblock, (uschar *)buffer, buffsize, '3', timeout))
+               return FAIL;
+
+       /* convert the challenge into the challenge struct */
+       DSPA("\n\n%s authenticator: challenge (%s)\n\n",
+               ablock->name, buffer + 4);
+       spa_base64_to_bits ((char *)(&challenge), sizeof(challenge), (const char *)(buffer + 4));
+
+       spa_build_auth_response (&challenge, &response,
+               CS username, CS password);
+       spa_bits_to_base64 (US msgbuf, (unsigned char*)&response,
+               spa_request_length(&response));
+       DSPA("\n\n%s authenticator: challenge response (%s)\n\n", ablock->name,
+               msgbuf);
+
+       /* send the challenge response */
+       if (smtp_write_command(outblock, FALSE, "%s\r\n", msgbuf) < 0)
+               return FAIL_SEND;
+
+       /* If we receive a success response from the server, authentication
+       has succeeded. There may be more data to send, but is there any point
+       in provoking an error here? */
+       if (smtp_read_response(inblock, US buffer, buffsize, '2', timeout))
+               return OK;
+
+       /* Not a success response. If errno != 0 there is some kind of transmission
+       error. Otherwise, check the response code in the buffer. If it starts with
+       '3', more data is expected. */
+       if (errno != 0 || buffer[0] != '3')
+               return FAIL;
+
+       return FAIL;
+}
+
+/* End of spa.c */
diff --git a/src/auths/spa.h b/src/auths/spa.h
new file mode 100644 (file)
index 0000000..c140fe5
--- /dev/null
@@ -0,0 +1,39 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* This file, which provides support for Microsoft's Secure Password
+Authentication, was contributed by Marc Prud'hommeaux. */
+
+
+#include "auth-spa.h"
+
+/* Private structure for the private options. */
+
+typedef struct {
+  uschar *spa_username;
+  uschar *spa_password;
+  uschar *spa_domain;
+  uschar *spa_serverpassword;
+} auth_spa_options_block;
+
+/* Data for reading the private options. */
+
+extern optionlist auth_spa_options[];
+extern int auth_spa_options_count;
+
+/* Block containing default values. */
+
+extern auth_spa_options_block auth_spa_option_defaults;
+
+/* The entry points for the mechanism */
+
+extern void auth_spa_init(auth_instance *);
+extern int auth_spa_server(auth_instance *, uschar *);
+extern int auth_spa_client(auth_instance *, smtp_inblock *,
+                                 smtp_outblock *, int, uschar *, int);
+
+/* End of spa.h */
diff --git a/src/auths/xtextdecode.c b/src/auths/xtextdecode.c
new file mode 100644 (file)
index 0000000..5312acf
--- /dev/null
@@ -0,0 +1,57 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+#include "../exim.h"
+
+
+/*************************************************
+*          Decode byte-string in xtext           *
+*************************************************/
+
+/* This function decodes a string in xtextformat as defined in RFC 1891 and
+required by the SMTP AUTH extension (RFC 2554). We put the result in a piece of
+store of equal length - it cannot be longer than this. Although in general the
+result of decoding an xtext may be binary, in the context in which it is used
+by Exim (for decoding the value of AUTH on a MAIL command), the result is
+expected to be an addr-spec. We therefore add on a terminating zero, for
+convenience.
+
+Arguments:
+  code        points to the coded string, zero-terminated
+  ptr         where to put the pointer to the result, which is in
+              dynamic store
+
+Returns:      the number of bytes in the result, excluding the final zero;
+              -1 if the input is malformed
+*/
+
+int
+auth_xtextdecode(uschar *code, uschar **ptr)
+{
+register int x;
+uschar *result = store_get(Ustrlen(code) + 1);
+*ptr = result;
+
+while ((x = (*code++)) != 0)
+  {
+  if (x < 33 || x > 127 || x == '=') return -1;
+  if (x == '+')
+    {
+    register int y;
+    if (!isxdigit((x = (*code++)))) return -1;
+    y = ((isdigit(x))? x - '0' : (tolower(x) - 'a' + 10)) << 4;
+    if (!isxdigit((x = (*code++)))) return -1;
+    *result++ = y | ((isdigit(x))? x - '0' : (tolower(x) - 'a' + 10));
+    }
+  else *result++ = x;
+  }
+
+*result = 0;
+return result - *ptr;
+}
+
+/* End of xtextdecode.c */
diff --git a/src/auths/xtextencode.c b/src/auths/xtextencode.c
new file mode 100644 (file)
index 0000000..7cdfe32
--- /dev/null
@@ -0,0 +1,61 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+#include "../exim.h"
+
+
+/*************************************************
+*          Encode byte-string in xtext           *
+*************************************************/
+
+/* This function encodes a string of bytes, containing any values whatsoever,
+as "xtext", as defined in RFC 1891 and required by the SMTP AUTH extension (RFC
+2554).
+
+Arguments:
+  clear       points to the clear text bytes
+  len         the number of bytes to encode
+
+Returns:      a pointer to the zero-terminated xtext string, which
+              is in working store
+*/
+
+uschar *
+auth_xtextencode(uschar *clear, int len)
+{
+uschar *code;
+uschar *p = (uschar *)clear;
+uschar *pp;
+int c = len;
+int count = 1;
+register int x;
+
+/* We have to do a prepass to find out how many specials there are,
+in order to get the right amount of store. */
+
+while (c -- > 0)
+  count += ((x = *p++) < 33 || x > 127 || x == '+' || x == '=')? 3 : 1;
+
+pp = code = store_get(count);
+
+p = (uschar *)clear;
+c = len;
+while (c-- > 0)
+  {
+  if ((x = *p++) < 33 || x > 127 || x == '+' || x == '=')
+    {
+    sprintf(CS pp, "+%.02x", x);   /* There's always room */
+    pp += 3;
+    }
+  else *pp++ = x;
+  }
+
+*pp = 0;
+return code;
+}
+
+/* End of xtextencode.c */
diff --git a/src/bmi_spam.c b/src/bmi_spam.c
new file mode 100644 (file)
index 0000000..3785149
--- /dev/null
@@ -0,0 +1,470 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Code for calling Brightmail AntiSpam.
+   Copyright (c) Tom Kistner <tom@duncanthrax.net> 2004
+   License: GPL */
+
+#include "exim.h"
+#ifdef EXPERIMENTAL_BRIGHTMAIL
+
+#include "bmi_spam.h"
+
+uschar *bmi_current_optin = NULL;
+
+uschar *bmi_process_message(header_line *header_list, int data_fd) {
+  BmiSystem *system = NULL;
+  BmiMessage *message = NULL;
+  BmiError err;
+  BmiErrorLocation err_loc;
+  BmiErrorType err_type;
+  const BmiVerdict *verdict = NULL;
+  FILE *data_file;
+  uschar data_buffer[4096];
+  uschar localhost[] = "127.0.0.1";
+  uschar *host_address;
+  uschar *verdicts = NULL;
+  int i,j;
+
+  err = bmiInitSystem(BMI_VERSION, (char *)bmi_config_file, &system);
+  if (bmiErrorIsFatal(err) == BMI_TRUE) {
+    err_loc = bmiErrorGetLocation(err);
+    err_type = bmiErrorGetType(err);
+    log_write(0, LOG_PANIC,
+               "bmi error [loc %d type %d]: could not initialize Brightmail system.", (int)err_loc, (int)err_type);
+    return NULL;
+  }
+
+  err = bmiInitMessage(system, &message);
+  if (bmiErrorIsFatal(err) == BMI_TRUE) {
+    err_loc = bmiErrorGetLocation(err);
+    err_type = bmiErrorGetType(err);
+    log_write(0, LOG_PANIC,
+               "bmi error [loc %d type %d]: could not initialize Brightmail message.", (int)err_loc, (int)err_type);
+    bmiFreeSystem(system);
+    return NULL;
+  }
+
+  /* Send IP address of sending host */
+  if (sender_host_address == NULL)
+    host_address = localhost;
+  else
+    host_address = sender_host_address;
+  err = bmiProcessConnection((char *)host_address, message);
+  if (bmiErrorIsFatal(err) == BMI_TRUE) {
+    err_loc = bmiErrorGetLocation(err);
+    err_type = bmiErrorGetType(err);
+    log_write(0, LOG_PANIC,
+               "bmi error [loc %d type %d]: bmiProcessConnection() failed (IP %s).", (int)err_loc, (int)err_type, (char *)host_address);
+    bmiFreeMessage(message);
+    bmiFreeSystem(system);
+    return NULL;
+  };
+
+  /* Send envelope sender address */
+  err = bmiProcessFROM((char *)sender_address, message);
+  if (bmiErrorIsFatal(err) == BMI_TRUE) {
+    err_loc = bmiErrorGetLocation(err);
+    err_type = bmiErrorGetType(err);
+    log_write(0, LOG_PANIC,
+               "bmi error [loc %d type %d]: bmiProcessFROM() failed (address %s).", (int)err_loc, (int)err_type, (char *)sender_address);
+    bmiFreeMessage(message);
+    bmiFreeSystem(system);
+    return NULL;
+  };
+
+  /* Send envelope recipients */
+  for(i=0;i<recipients_count;i++) {
+    recipient_item *r = recipients_list + i;
+    BmiOptin *optin = NULL;
+
+    /* create optin object if optin string is given */
+    if ((r->bmi_optin != NULL) && (Ustrlen(r->bmi_optin) > 1)) {
+      debug_printf("passing bmiOptin string: %s\n", r->bmi_optin);
+      bmiOptinInit(&optin);
+      err = bmiOptinMset(optin, r->bmi_optin, ':');
+      if (bmiErrorIsFatal(err) == BMI_TRUE) {
+        log_write(0, LOG_PANIC|LOG_MAIN,
+                   "bmi warning: [loc %d type %d]: bmiOptinMSet() failed (address '%s', string '%s').", (int)err_loc, (int)err_type, (char *)r->address, (char *)r->bmi_optin);
+        if (optin != NULL)
+          bmiOptinFree(optin);
+        optin = NULL;
+      };
+    };
+
+    err = bmiAccumulateTO((char *)r->address, optin, message);
+
+    if (optin != NULL)
+      bmiOptinFree(optin);
+
+    if (bmiErrorIsFatal(err) == BMI_TRUE) {
+      err_loc = bmiErrorGetLocation(err);
+      err_type = bmiErrorGetType(err);
+      log_write(0, LOG_PANIC,
+                 "bmi error [loc %d type %d]: bmiAccumulateTO() failed (address %s).", (int)err_loc, (int)err_type, (char *)r->address);
+      bmiFreeMessage(message);
+      bmiFreeSystem(system);
+      return NULL;
+    };
+  };
+  err = bmiEndTO(message);
+  if (bmiErrorIsFatal(err) == BMI_TRUE) {
+    err_loc = bmiErrorGetLocation(err);
+    err_type = bmiErrorGetType(err);
+    log_write(0, LOG_PANIC,
+               "bmi error [loc %d type %d]: bmiEndTO() failed.", (int)err_loc, (int)err_type);
+    bmiFreeMessage(message);
+    bmiFreeSystem(system);
+    return NULL;
+  };
+
+  /* Send message headers */
+  while (header_list != NULL) {
+    /* skip deleted headers */
+    if (header_list->type == '*') {
+      header_list = header_list->next;
+      continue;
+    };
+    err = bmiAccumulateHeaders((const char *)header_list->text, header_list->slen, message);
+    if (bmiErrorIsFatal(err) == BMI_TRUE) {
+      err_loc = bmiErrorGetLocation(err);
+      err_type = bmiErrorGetType(err);
+      log_write(0, LOG_PANIC,
+                 "bmi error [loc %d type %d]: bmiAccumulateHeaders() failed.", (int)err_loc, (int)err_type);
+      bmiFreeMessage(message);
+      bmiFreeSystem(system);
+      return NULL;
+    };
+    header_list = header_list->next;
+  };
+  err = bmiEndHeaders(message);
+  if (bmiErrorIsFatal(err) == BMI_TRUE) {
+    err_loc = bmiErrorGetLocation(err);
+    err_type = bmiErrorGetType(err);
+    log_write(0, LOG_PANIC,
+               "bmi error [loc %d type %d]: bmiEndHeaders() failed.", (int)err_loc, (int)err_type);
+    bmiFreeMessage(message);
+    bmiFreeSystem(system);
+    return NULL;
+  };
+
+  /* Send body */
+  data_file = fdopen(data_fd,"r");
+  do {
+    j = fread(data_buffer, 1, sizeof(data_buffer), data_file);
+    if (j > 0) {
+      err = bmiAccumulateBody((const char *)data_buffer, j, message);
+      if (bmiErrorIsFatal(err) == BMI_TRUE) {
+        err_loc = bmiErrorGetLocation(err);
+        err_type = bmiErrorGetType(err);
+        log_write(0, LOG_PANIC,
+                   "bmi error [loc %d type %d]: bmiAccumulateBody() failed.", (int)err_loc, (int)err_type);
+        bmiFreeMessage(message);
+        bmiFreeSystem(system);
+        return NULL;
+      };
+    };
+  } while (j > 0);
+  err = bmiEndBody(message);
+  if (bmiErrorIsFatal(err) == BMI_TRUE) {
+    err_loc = bmiErrorGetLocation(err);
+    err_type = bmiErrorGetType(err);
+    log_write(0, LOG_PANIC,
+               "bmi error [loc %d type %d]: bmiEndBody() failed.", (int)err_loc, (int)err_type);
+    bmiFreeMessage(message);
+    bmiFreeSystem(system);
+    return NULL;
+  };
+
+
+  /* End message */
+  err = bmiEndMessage(message);
+  if (bmiErrorIsFatal(err) == BMI_TRUE) {
+    err_loc = bmiErrorGetLocation(err);
+    err_type = bmiErrorGetType(err);
+    log_write(0, LOG_PANIC,
+               "bmi error [loc %d type %d]: bmiEndMessage() failed.", (int)err_loc, (int)err_type);
+    bmiFreeMessage(message);
+    bmiFreeSystem(system);
+    return NULL;
+  };
+
+  /* get store for the verdict string */
+  verdicts = store_get(1);
+  *verdicts = '\0';
+
+  for ( err = bmiAccessFirstVerdict(message, &verdict);
+        verdict != NULL;
+        err = bmiAccessNextVerdict(message, verdict, &verdict) ) {
+    char *verdict_str;
+
+    err = bmiCreateStrFromVerdict(verdict,&verdict_str);
+    if (!store_extend(verdicts, Ustrlen(verdicts)+1, Ustrlen(verdicts)+1+strlen(verdict_str)+1)) {
+      /* can't allocate more store */
+      return NULL;
+    };
+    if (*verdicts != '\0')
+      Ustrcat(verdicts, US ":");
+    Ustrcat(verdicts, US verdict_str);
+    bmiFreeStr(verdict_str);
+  };
+
+  DEBUG(D_receive) debug_printf("bmi verdicts: %s\n", verdicts);
+
+  if (Ustrlen(verdicts) == 0)
+    return NULL;
+  else
+    return verdicts;
+}
+
+
+int bmi_get_delivery_status(uschar *base64_verdict) {
+  BmiError err;
+  BmiErrorLocation err_loc;
+  BmiErrorType err_type;
+  BmiVerdict *verdict = NULL;
+  int rc = 1;   /* deliver by default */
+
+  /* always deliver when there is no verdict */
+  if (base64_verdict == NULL)
+    return 1;
+
+  /* create verdict from base64 string */
+  err = bmiCreateVerdictFromStr(CS base64_verdict, &verdict);
+  if (bmiErrorIsFatal(err) == BMI_TRUE) {
+    err_loc = bmiErrorGetLocation(err);
+    err_type = bmiErrorGetType(err);
+    log_write(0, LOG_PANIC,
+               "bmi error [loc %d type %d]: bmiCreateVerdictFromStr() failed. [%s]", (int)err_loc, (int)err_type, base64_verdict);
+    return 1;
+  };
+
+  err = bmiVerdictError(verdict);
+  if (bmiErrorIsFatal(err) == BMI_TRUE) {
+    /* deliver normally due to error */
+    rc = 1;
+  }
+  else if (bmiVerdictDestinationIsDefault(verdict) == BMI_TRUE) {
+    /* deliver normally */
+    rc = 1;
+  }
+  else if (bmiVerdictAccessDestination(verdict) == NULL) {
+    /* do not deliver */
+    rc = 0;
+  }
+  else {
+    /* deliver to alternate location */
+    rc = 1;
+  };
+
+  bmiFreeVerdict(verdict);
+  return rc;
+}
+
+
+uschar *bmi_get_alt_location(uschar *base64_verdict) {
+  BmiError err;
+  BmiErrorLocation err_loc;
+  BmiErrorType err_type;
+  BmiVerdict *verdict = NULL;
+  uschar *rc = NULL;
+
+  /* always deliver when there is no verdict */
+  if (base64_verdict == NULL)
+    return NULL;
+
+  /* create verdict from base64 string */
+  err = bmiCreateVerdictFromStr(CS base64_verdict, &verdict);
+  if (bmiErrorIsFatal(err) == BMI_TRUE) {
+    err_loc = bmiErrorGetLocation(err);
+    err_type = bmiErrorGetType(err);
+    log_write(0, LOG_PANIC,
+               "bmi error [loc %d type %d]: bmiCreateVerdictFromStr() failed. [%s]", (int)err_loc, (int)err_type, base64_verdict);
+    return NULL;
+  };
+
+  err = bmiVerdictError(verdict);
+  if (bmiErrorIsFatal(err) == BMI_TRUE) {
+    /* deliver normally due to error */
+    rc = NULL;
+  }
+  else if (bmiVerdictDestinationIsDefault(verdict) == BMI_TRUE) {
+    /* deliver normally */
+    rc = NULL;
+  }
+  else if (bmiVerdictAccessDestination(verdict) == NULL) {
+    /* do not deliver */
+    rc = NULL;
+  }
+  else {
+    /* deliver to alternate location */
+    rc = store_get(strlen(bmiVerdictAccessDestination(verdict))+1);
+    Ustrcpy(rc, bmiVerdictAccessDestination(verdict));
+    rc[strlen(bmiVerdictAccessDestination(verdict))] = '\0';
+  };
+
+  bmiFreeVerdict(verdict);
+  return rc;
+}
+
+uschar *bmi_get_base64_verdict(uschar *bmi_local_part, uschar *bmi_domain) {
+  BmiError err;
+  BmiErrorLocation err_loc;
+  BmiErrorType err_type;
+  BmiVerdict *verdict = NULL;
+  const BmiRecipient *recipient = NULL;
+  const char *verdict_str = NULL;
+  uschar *verdict_ptr;
+  uschar *verdict_buffer = NULL;
+  int sep = 0;
+
+  /* return nothing if there are no verdicts available */
+  if (bmi_verdicts == NULL)
+    return NULL;
+
+  /* allocate room for the b64 verdict string */
+  verdict_buffer = store_get(Ustrlen(bmi_verdicts)+1);
+
+  /* loop through verdicts */
+  verdict_ptr = bmi_verdicts;
+  while ((verdict_str = (const char *)string_nextinlist(&verdict_ptr, &sep,
+                                          verdict_buffer,
+                                          Ustrlen(bmi_verdicts)+1)) != NULL) {
+
+    /* create verdict from base64 string */
+    err = bmiCreateVerdictFromStr(verdict_str, &verdict);
+    if (bmiErrorIsFatal(err) == BMI_TRUE) {
+      err_loc = bmiErrorGetLocation(err);
+      err_type = bmiErrorGetType(err);
+      log_write(0, LOG_PANIC,
+                 "bmi error [loc %d type %d]: bmiCreateVerdictFromStr() failed. [%s]", (int)err_loc, (int)err_type, verdict_str);
+      return NULL;
+    };
+
+    /* loop through rcpts for this verdict */
+    for ( recipient = bmiVerdictAccessFirstRecipient(verdict);
+          recipient != NULL;
+          recipient = bmiVerdictAccessNextRecipient(verdict, recipient)) {
+      uschar *rcpt_local_part;
+      uschar *rcpt_domain;
+
+      /* compare address against our subject */
+      rcpt_local_part = (unsigned char *)bmiRecipientAccessAddress(recipient);
+      rcpt_domain = Ustrchr(rcpt_local_part,'@');
+      if (rcpt_domain == NULL) {
+        rcpt_domain = US"";
+      }
+      else {
+        *rcpt_domain = '\0';
+        rcpt_domain++;
+      };
+
+      if ( (strcmpic(rcpt_local_part, bmi_local_part) == 0) &&
+           (strcmpic(rcpt_domain, bmi_domain) == 0) ) {
+        /* found verdict */
+        bmiFreeVerdict(verdict);
+        return (uschar *)verdict_str;
+      };
+    };
+
+    bmiFreeVerdict(verdict);
+  };
+
+  return NULL;
+}
+
+
+uschar *bmi_get_base64_tracker_verdict(uschar *base64_verdict) {
+  BmiError err;
+  BmiErrorLocation err_loc;
+  BmiErrorType err_type;
+  BmiVerdict *verdict = NULL;
+  uschar *rc = NULL;
+
+  /* always deliver when there is no verdict */
+  if (base64_verdict == NULL)
+    return NULL;
+
+  /* create verdict from base64 string */
+  err = bmiCreateVerdictFromStr(CS base64_verdict, &verdict);
+  if (bmiErrorIsFatal(err) == BMI_TRUE) {
+    err_loc = bmiErrorGetLocation(err);
+    err_type = bmiErrorGetType(err);
+    log_write(0, LOG_PANIC,
+               "bmi error [loc %d type %d]: bmiCreateVerdictFromStr() failed. [%s]", (int)err_loc, (int)err_type, base64_verdict);
+    return NULL;
+  };
+
+  /* create old tracker string from verdict */
+  err = bmiCreateOldStrFromVerdict(verdict, &rc);
+  if (bmiErrorIsFatal(err) == BMI_TRUE) {
+    err_loc = bmiErrorGetLocation(err);
+    err_type = bmiErrorGetType(err);
+    log_write(0, LOG_PANIC,
+               "bmi error [loc %d type %d]: bmiCreateOldStrFromVerdict() failed. [%s]", (int)err_loc, (int)err_type, base64_verdict);
+    return NULL;
+  };
+
+  bmiFreeVerdict(verdict);
+  return rc;
+}
+
+
+int bmi_check_rule(uschar *base64_verdict, uschar *option_list) {
+  BmiError err;
+  BmiErrorLocation err_loc;
+  BmiErrorType err_type;
+  BmiVerdict *verdict = NULL;
+  int rc = 0;
+  uschar *rule_num;
+  uschar *rule_ptr;
+  uschar rule_buffer[32];
+  int sep = 0;
+
+
+  /* no verdict -> no rule fired */
+  if (base64_verdict == NULL)
+    return 0;
+
+  /* create verdict from base64 string */
+  err = bmiCreateVerdictFromStr(CS base64_verdict, &verdict);
+  if (bmiErrorIsFatal(err) == BMI_TRUE) {
+    err_loc = bmiErrorGetLocation(err);
+    err_type = bmiErrorGetType(err);
+    log_write(0, LOG_PANIC,
+               "bmi error [loc %d type %d]: bmiCreateVerdictFromStr() failed. [%s]", (int)err_loc, (int)err_type, base64_verdict);
+    return 0;
+  };
+
+  err = bmiVerdictError(verdict);
+  if (bmiErrorIsFatal(err) == BMI_TRUE) {
+    /* error -> no rule fired */
+    bmiFreeVerdict(verdict);
+    return 0;
+  }
+
+  /* loop through numbers */
+  rule_ptr = option_list;
+  while ((rule_num = string_nextinlist(&rule_ptr, &sep,
+                                       rule_buffer, 32)) != NULL) {
+    int rule_int = -1;
+
+    /* try to translate to int */
+    (void)sscanf(rule_num, "%d", &rule_int);
+    if (rule_int > 0) {
+      debug_printf("checking rule #%d\n", rule_int);
+      /* check if rule fired on the message */
+      if (bmiVerdictRuleFired(verdict, rule_int) == BMI_TRUE) {
+        debug_printf("rule #%d fired\n", rule_int);
+        rc = 1;
+        break;
+      };
+    };
+  };
+
+  bmiFreeVerdict(verdict);
+  return rc;
+};
+
+#endif
diff --git a/src/bmi_spam.h b/src/bmi_spam.h
new file mode 100644 (file)
index 0000000..a9af778
--- /dev/null
@@ -0,0 +1,22 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Code for calling Brightmail AntiSpam.
+   Copyright (c) Tom Kistner <tom@duncanthrax.net> 2004
+   License: GPL */
+
+#ifdef EXPERIMENTAL_BRIGHTMAIL
+
+#include <bmi_api.h>
+
+extern uschar *bmi_process_message(header_line *, int);
+extern uschar *bmi_get_base64_verdict(uschar *, uschar *);
+extern uschar *bmi_get_base64_tracker_verdict(uschar *);
+extern int bmi_get_delivery_status(uschar *);
+extern uschar *bmi_get_alt_location(uschar *);
+extern int bmi_check_rule(uschar *,uschar *);
+
+extern uschar *bmi_current_optin;
+
+#endif
diff --git a/src/buildconfig.c b/src/buildconfig.c
new file mode 100644 (file)
index 0000000..f3390cb
--- /dev/null
@@ -0,0 +1,930 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2012 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+
+/*************************************************
+*       Build configuration header for Exim      *
+*************************************************/
+
+/* This auxiliary program builds the file config.h by the following
+process:
+
+First, it determines the size of off_t and time_t variables, and generates
+macro code to define OFF_T_FMT and TIME_T_FMT as suitable formats, if they are
+not already defined in the system-specific header file.
+
+Then it reads Makefile, looking for certain OS-specific definitions which it
+uses to define some specific macros. Finally, it reads the defaults file
+config.h.defaults.
+
+The defaults file contains normal C #define statements for various macros; if
+the name of a macro is found in the environment, the environment value replaces
+the default. If the default #define does not contain any value, then that macro
+is not copied to the created file unless there is some value in the
+environment.
+
+This program is compiled and run as part of the Make process and is not
+normally called independently. */
+
+
+#include <ctype.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+#include <pwd.h>
+#include <grp.h>
+
+typedef struct {
+  const char *name;
+  int *flag;
+} have_item;
+
+typedef struct {
+  const char *name;
+  char *data;
+} save_item;
+
+static const char *db_opts[] = { "", "USE_DB", "USE_GDBM", "USE_TDB" };
+
+static int have_ipv6 = 0;
+static int have_iconv = 0;
+
+static char errno_quota[256];
+static char ostype[256];
+static char cc[256];
+
+/* If any entry is an initial substring of another, the longer one must
+appear first. */
+
+static have_item have_list[] = {
+  { "HAVE_IPV6",      &have_ipv6 },
+  { "HAVE_ICONV",     &have_iconv },
+  { NULL, NULL}
+};
+
+static save_item save_list[] = {
+  { "ERRNO_QUOTA",    errno_quota },
+  { "OSTYPE",         ostype },
+  { "CC",             cc },
+  { NULL, NULL}
+};
+
+
+/* Subroutine to check a string for precisely one instance of "%s". If not,
+bomb out. */
+
+void
+check_percent_ess(char *value, char *name)
+{
+int OK = 0;
+char *p = strstr(value, "%s");
+if (p != NULL) OK = strstr(p+2, "%s") == NULL;
+if (!OK)
+  {
+  printf("\n*** \"%s\" (%s) must contain precisely one occurrence of\n"
+    "*** \"%%s\". Please review your build-time configuration.\n\n/", value,
+    name);
+  exit(1);
+  }
+}
+
+
+/* Main program */
+
+int
+main(int argc, char **argv)
+{
+off_t test_off_t = 0;
+time_t test_time_t = 0;
+#if ! (__STDC_VERSION__ >= 199901L)
+size_t test_size_t = 0;
+ssize_t test_ssize_t = 0;
+unsigned long test_ulong_t = 0L;
+#endif
+long test_long_t = 0;
+FILE *base;
+FILE *new;
+int last_initial = 'A';
+int linecount = 0;
+int have_auth = 0;
+int in_local_makefile = 0;
+int use_which_db = 0;
+int use_which_db_in_local_makefile = 0;
+int support_crypteq = 0;
+char buffer[1024];
+
+if (argc != 1)
+  {
+  printf("*** Buildconfig: called with incorrect arguments\n");
+  exit(1);
+  }
+
+new = fopen("config.h", "wb");
+if (new == NULL)
+  {
+  printf("*** Buildconfig: failed to open config.h for output\n");
+  exit(1);
+  }
+
+printf("Building configuration file config.h\n");
+
+fprintf(new, "/*************************************************\n");
+fprintf(new, "*           Configuration header for Exim        *\n");
+fprintf(new, "*************************************************/\n\n");
+
+fprintf(new, "/* This file was automatically generated from Makefile and "
+  "config.h.defaults,\n");
+fprintf(new, "using values specified in the configuration file Local/Makefile.\n");
+fprintf(new, "Do not edit it. Instead, edit Local/Makefile and "
+  "rerun make. */\n\n");
+
+/* First, deal with the printing format for off_t variables. We assume that if
+the size of off_t is greater than 4, "%lld" will be available as a format for
+printing long long variables, and there will be support for the long long type.
+This assumption is known to be OK for the common operating systems. */
+
+fprintf(new, "#ifndef OFF_T_FMT\n");
+if (sizeof(test_off_t) > sizeof(test_long_t))
+  {
+  fprintf(new, "#define OFF_T_FMT  \"%%lld\"\n");
+  fprintf(new, "#define LONGLONG_T long long int\n");
+  }
+else
+  {
+  fprintf(new, "#define OFF_T_FMT  \"%%ld\"\n");
+  fprintf(new, "#define LONGLONG_T long int\n");
+  }
+fprintf(new, "#endif\n\n");
+
+/* Now do the same thing for time_t variables. If the length is greater than
+4, we want to assume long long support (even if off_t was less than 4). If the
+length is 4 or less, we can leave LONGLONG_T to whatever was defined above for
+off_t. */
+
+fprintf(new, "#ifndef TIME_T_FMT\n");
+if (sizeof(test_time_t) > sizeof(test_long_t))
+  {
+  fprintf(new, "#define TIME_T_FMT  \"%%lld\"\n");
+  fprintf(new, "#undef  LONGLONG_T\n");
+  fprintf(new, "#define LONGLONG_T long long int\n");
+  }
+else
+  {
+  fprintf(new, "#define TIME_T_FMT  \"%%ld\"\n");
+  }
+fprintf(new, "#endif\n\n");
+
+/* And for sizeof() results, size_t, which should with C99 be just %zu, deal
+with C99 not being ubiquitous yet.  Unfortunately.  Assume ssize_t is same
+size as size_t on C99; if someone comes up with a version where it's not, fix
+it then. */
+
+#if __STDC_VERSION__ >= 199901L
+fprintf(new, "#define SIZE_T_FMT  \"%%zu\"\n");
+fprintf(new, "#define SSIZE_T_FMT  \"%%zd\"\n");
+#else
+if (sizeof(test_size_t) > sizeof (test_ulong_t))
+  fprintf(new, "#define SIZE_T_FMT  \"%%llu\"\n");
+else
+  fprintf(new, "#define SIZE_T_FMT  \"%%lu\"\n");
+if (sizeof(test_ssize_t) > sizeof(test_long_t))
+  fprintf(new, "#define SSIZE_T_FMT  \"%%lld\"\n");
+else
+  fprintf(new, "#define SSIZE_T_FMT  \"%%ld\"\n");
+#endif
+
+/* Now search the makefile for certain settings */
+
+base = fopen("Makefile", "rb");
+if (base == NULL)
+  {
+  printf("*** Buildconfig: failed to open Makefile\n");
+  (void)fclose(new);
+  exit(1);
+  }
+
+errno_quota[0] = 0;    /* no over-riding value set */
+ostype[0] = 0;         /* just in case */
+cc[0] = 0;
+
+while (fgets(buffer, sizeof(buffer), base) != NULL)
+  {
+  int i;
+  have_item *h;
+  save_item *s;
+  char *p = buffer + (int)strlen(buffer);
+  linecount++;
+  while (p > buffer && isspace((unsigned char)p[-1])) p--;
+  *p = 0;
+  p = buffer;
+  while (isspace((unsigned char)*p)) p++;
+
+  /* Notice when we hit the user's makefile */
+
+  if (strcmp(p, "# From Local/Makefile") == 0)
+    {
+    in_local_makefile = 1;
+    continue;
+    }
+
+  /* Remember the last DB option setting. If we hit two in the user's
+  Makefile, complain. */
+
+  for (i = 1; i < sizeof(db_opts)/sizeof(char *); i++)
+    {
+    int len = (int)strlen(db_opts[i]);
+    if (strncmp(p, db_opts[i], len) == 0 && (p[len] == ' ' || p[len] == '='))
+      {
+      if (in_local_makefile)
+        {
+        if (use_which_db_in_local_makefile)
+          {
+          printf("*** Only one of USE_DB, USE_GDBM, or USE_TDB should be "
+            "defined in Local/Makefile\n");
+          exit(1);
+          }
+        use_which_db_in_local_makefile = 1;
+        }
+      use_which_db = i;
+      break;
+      }
+    }
+  if (i < sizeof(db_opts)/sizeof(char *)) continue;
+
+  /* Items where we just save a boolean */
+
+  for (h = have_list; h->name != NULL; h++)
+    {
+    int len = (int)strlen(h->name);
+    if (strncmp(p, h->name, len) == 0)
+      {
+      p += len;
+      while (isspace((unsigned char)*p)) p++;
+      if (*p++ != '=')
+        {
+        printf("*** Buildconfig: syntax error in Makefile line %d\n", linecount);
+        exit(1);
+        }
+      while (isspace((unsigned char)*p)) p++;
+      if (strcmp(p, "YES") == 0 || strcmp(p, "yes") == 0) *(h->flag) = 1;
+        else *(h->flag) = 0;   /* Must reset in case multiple instances */
+      break;
+      }
+    }
+
+  if (h->name != NULL) continue;
+
+  /* Items where we save the complete string */
+
+  for (s = save_list; s->name != NULL; s++)
+    {
+    int len = (int)strlen(s->name);
+    if (strncmp(p, s->name, len) == 0)
+      {
+      p += len;
+      while (isspace((unsigned char)*p)) p++;
+      if (*p++ != '=')
+        {
+        printf("*** Buildconfig: syntax error in Makefile line %d\n", linecount);
+        exit(1);
+        }
+      while (isspace((unsigned char)*p)) p++;
+      strcpy(s->data, p);
+      }
+    }
+  }
+
+fprintf(new, "#define HAVE_IPV6             %s\n",
+  have_ipv6? "TRUE" : "FALSE");
+
+fprintf(new, "#define HAVE_ICONV            %s\n",
+  have_iconv? "TRUE" : "FALSE");
+
+if (errno_quota[0] != 0)
+  fprintf(new, "\n#define ERRNO_QUOTA           %s\n", errno_quota);
+
+if (strcmp(cc, "gcc") == 0 &&
+    (strstr(ostype, "IRIX") != NULL || strstr(ostype, "AIX") != NULL))
+  {
+  fprintf(new, "\n/* This switch includes the code to fix the inet_ntoa() */");
+  fprintf(new, "\n/* bug when using gcc on an IRIX or AIX system. */");
+  fprintf(new, "\n#define USE_INET_NTOA_FIX");
+  }
+
+fprintf(new, "\n");
+(void)fclose(base);
+
+
+/* Now handle the macros listed in the defaults */
+
+base = fopen("../src/config.h.defaults", "rb");
+if (base == NULL)
+  {
+  printf("*** Buildconfig: failed to open ../src/config.h.defaults\n");
+  (void)fclose(new);
+  exit(1);
+  }
+
+while (fgets(buffer, sizeof(buffer), base) != NULL)
+  {
+  int i;
+  char name[256];
+  char *value;
+  char *p = buffer;
+  char *q = name;
+
+  while (*p == ' ' || *p == '\t') p++;
+
+  if (strncmp(p, "#ifdef ", 7) == 0
+   || strncmp(p, "#ifndef ", 8) == 0
+   || strncmp(p, "#if ", 4) == 0
+   || strncmp(p, "#endif", 6) == 0
+     )
+    {
+    fputs(buffer, new);
+    continue;
+    }
+
+  if (strncmp(p, "#define ", 8) != 0) continue;
+
+  p += 8;
+  while (*p == ' ' || *p == '\t') p++;
+
+  if (*p < last_initial) fprintf(new, "\n");
+  last_initial = *p;
+
+  while (*p && (isalnum((unsigned char)*p) || *p == '_')) *q++ = *p++;
+  *q = 0;
+
+  /* USE_DB, USE_GDBM, and USE_TDB are special cases. We want to have only
+  one of them set. The scan of the Makefile has saved which was the last one
+  encountered. */
+
+  for (i = 1; i < sizeof(db_opts)/sizeof(char *); i++)
+    {
+    if (strcmp(name, db_opts[i]) == 0)
+      {
+      if (use_which_db == i)
+        fprintf(new, "#define %s %.*syes\n", db_opts[i],
+          21 - (int)strlen(db_opts[i]), "                         ");
+      else
+        fprintf(new, "/* %s not set */\n", name);
+      break;
+      }
+    }
+  if (i < sizeof(db_opts)/sizeof(char *)) continue;
+
+  /* EXIM_USER is a special case. We look in the environment for EXIM_USER or
+  EXIM_UID (the latter for backward compatibility with Exim 3). If the value is
+  not numeric, we look up the user, and default the GID if found. Otherwise,
+  EXIM_GROUP or EXIM_GID must be in the environment. */
+
+  if (strcmp(name, "EXIM_UID") == 0)
+    {
+    uid_t uid = 0;
+    gid_t gid = 0;
+    int gid_set = 0;
+    int uid_not_set = 0;
+    char *username = NULL;
+    char *groupname = NULL;
+    char *s;
+    char *user = getenv("EXIM_USER");
+    char *group = getenv("EXIM_GROUP");
+
+    if (user == NULL) user = getenv("EXIM_UID");
+    if (group == NULL) group = getenv("EXIM_GID");
+
+    if (user == NULL)
+      {
+      printf("\n*** EXIM_USER has not been defined in any of the Makefiles in "
+        "the\n    \"Local\" directory. Please review your build-time "
+        "configuration.\n\n");
+      return 1;
+      }
+
+    while (isspace((unsigned char)(*user))) user++;
+    if (*user == 0)
+      {
+      printf("\n*** EXIM_USER is defined as an empty string in one of the "
+        "files\n    in the \"Local\" directory. Please review your build-time"
+        "\n    configuration.\n\n");
+      return 1;
+      }
+
+    for (s = user; *s != 0; s++)
+      {
+      if (iscntrl((unsigned char)(*s)))
+        {
+        printf("\n*** EXIM_USER contains the control character 0x%02X in one "
+          "of the files\n    in the \"Local\" directory. Please review your "
+          "build-time\n    configuration.\n\n", *s);
+        return 1;
+        }
+      }
+
+    /* Numeric uid given */
+
+    if (user[strspn(user, "0123456789")] == 0)
+      {
+      uid = (uid_t)atoi(user);
+      }
+
+    /* User name given. Normally, we look up the uid right away. However,
+    people building binary distributions sometimes want to retain the name till
+    runtime. This is supported if the name begins "ref:". */
+
+    else if (strncmp(user, "ref:", 4) == 0)
+      {
+      user += 4;
+      while (isspace(*user)) user++;
+      username = user;
+      gid_set = 1;
+      uid_not_set = 1;
+      }
+
+    else
+      {
+      struct passwd *pw = getpwnam(user);
+      if (pw == NULL)
+        {
+        printf("\n*** User \"%s\" (specified in one of the Makefiles) does not "
+          "exist.\n    Please review your build-time configuration.\n\n",
+          user);
+        return 1;
+        }
+
+      uid = pw->pw_uid;
+      gid = pw->pw_gid;
+      gid_set = 1;
+      }
+
+    /* Use explicit group if set. */
+
+    if (group != NULL)
+      {
+      while (isspace((unsigned char)(*group))) group++;
+      if (*group == 0)
+        {
+        printf("\n*** EXIM_GROUP is defined as an empty string in one of "
+          "the files in the\n    \"Local\" directory. ");
+        if (gid_set)
+          {
+          printf("If you want the Exim group to be taken from the\n    "
+            "password data for the Exim user, just remove the EXIM_GROUP "
+            "setting.\n    Otherwise, p");
+          }
+        else printf("EXIM_USER is defined numerically, so there is no"
+          "\n    default for EXIM_GROUP and you must set it explicitly.\n    P");
+        printf("lease review your build-time configuration.\n\n");
+        return 1;
+        }
+
+      for (s = group; *s != 0; s++)
+        {
+        if (iscntrl((unsigned char)(*s)))
+          {
+          printf("\n*** EXIM_GROUP contains the control character 0x%02X in one "
+            "of the files\n    in the \"Local\" directory. Please review your "
+            "build-time\n    configuration.\n\n", *s);
+          return 1;
+          }
+        }
+
+      /* Group name given. This may be by reference or to be looked up now,
+      as for user. */
+
+      if (strncmp(group, "ref:", 4) == 0)
+        {
+        group += 4;
+        while (isspace(*group)) group++;
+        groupname = group;
+        }
+
+      else if (username != NULL)
+        {
+        groupname = group;
+        }
+
+      else if (group[strspn(group, "0123456789")] == 0)
+        {
+        gid = (gid_t)atoi(group);
+        }
+
+      else
+        {
+        struct group *gr = getgrnam(group);
+        if (gr == NULL)
+          {
+          printf("\n*** Group \"%s\" (specified in one of the Makefiles) does "
+            "not exist.\n   Please review your build-time configuration.\n\n",
+            group);
+          return 1;
+          }
+        gid = gr->gr_gid;
+        }
+      }
+
+    /* Else trouble unless found in passwd file with user */
+
+    else if (!gid_set)
+      {
+      printf("\n*** No group set for Exim. Please review your build-time "
+        "configuration.\n\n");
+      return 1;
+      }
+
+    /* security sanity checks
+    if ref: is being used, we can never be sure, but we can take reasonable
+    steps to filter out the most obvious ones.  */
+
+    if ((!uid_not_set && uid == 0) ||
+        ((username != NULL) && (
+          (strcmp(username, "root") == 0) ||
+          (strcmp(username, "toor") == 0) )))
+      {
+      printf("\n*** Exim's internal user must not be root.\n\n");
+      return 1;
+      }
+
+    /* Output user and group names or uid/gid. When names are set, uid/gid
+    are set to zero but will be replaced at runtime. */
+
+    if (username != NULL)
+      fprintf(new, "#define EXIM_USERNAME         \"%s\"\n", username);
+    if (groupname != NULL)
+      fprintf(new, "#define EXIM_GROUPNAME        \"%s\"\n", groupname);
+
+    fprintf(new, "#define EXIM_UID              %d\n", (int)uid);
+    fprintf(new, "#define EXIM_GID              %d\n", (int)gid);
+    continue;
+    }
+
+  /* CONFIGURE_OWNER and CONFIGURE_GROUP are special cases. We look in the
+  environment for first. If the value is not numeric, we look up the user or
+  group. A lot of this code is similar to that for EXIM_USER, but it's easier
+  to keep it separate. */
+
+  if (strcmp(name, "CONFIGURE_OWNER") == 0 ||
+      strcmp(name, "CONFIGURE_GROUP") == 0)
+    {
+    int isgroup = name[10] == 'G';
+    uid_t uid = 0;
+    gid_t gid = 0;
+    const char *s;
+    const char *username = NULL;
+    const char *user = getenv(name);
+
+    if (user == NULL) user = "";
+    while (isspace((unsigned char)(*user))) user++;
+    if (*user == 0)
+      {
+      fprintf(new, "/* %s not set */\n", name);
+      continue;
+      }
+
+    for (s = user; *s != 0; s++)
+      {
+      if (iscntrl((unsigned char)(*s)))
+        {
+        printf("\n*** %s contains the control character 0x%02X in "
+          "one of the files\n    in the \"Local\" directory. Please review "
+          "your build-time\n    configuration.\n\n", name, *s);
+        return 1;
+        }
+      }
+
+    /* Numeric uid given */
+
+    if (user[strspn(user, "0123456789")] == 0)
+      {
+      if (isgroup)
+        gid = (gid_t)atoi(user);
+      else
+        uid = (uid_t)atoi(user);
+      }
+
+    /* Name given. Normally, we look up the uid or gid right away. However,
+    people building binary distributions sometimes want to retain the name till
+    runtime. This is supported if the name begins "ref:". */
+
+    else if (strncmp(user, "ref:", 4) == 0)
+      {
+      user += 4;
+      while (isspace(*user)) user++;
+      username = user;
+      }
+else if (isgroup)
+      {
+      struct group *gr = getgrnam(user);
+      if (gr == NULL)
+        {
+        printf("\n*** Group \"%s\" (specified in one of the Makefiles) does not "
+          "exist.\n    Please review your build-time configuration.\n\n",
+          user);
+        return 1;
+        }
+      gid = gr->gr_gid;
+      }
+
+    else
+      {
+      struct passwd *pw = getpwnam(user);
+      if (pw == NULL)
+        {
+        printf("\n*** User \"%s\" (specified in one of the Makefiles) does not "
+          "exist.\n    Please review your build-time configuration.\n\n",
+          user);
+        return 1;
+        }
+      uid = pw->pw_uid;
+      }
+
+    /* Output user and group names or uid/gid. When names are set, uid/gid
+    are set to zero but will be replaced at runtime. */
+
+    if (username != NULL)
+      {
+      if (isgroup)
+        fprintf(new, "#define CONFIGURE_GROUPNAME         \"%s\"\n", username);
+      else
+        fprintf(new, "#define CONFIGURE_OWNERNAME         \"%s\"\n", username);
+      }
+
+    if (isgroup)
+      fprintf(new, "#define CONFIGURE_GROUP              %d\n", (int)gid);
+    else
+      fprintf(new, "#define CONFIGURE_OWNER              %d\n", (int)uid);
+    continue;
+    }
+
+  /* FIXED_NEVER_USERS is another special case. Look up the uid values and
+  create suitable initialization data for a vector. */
+
+  if (strcmp(name, "FIXED_NEVER_USERS") == 0)
+    {
+    char *list = getenv("FIXED_NEVER_USERS");
+    if (list == NULL)
+      {
+      fprintf(new, "#define FIXED_NEVER_USERS     0\n");
+      }
+    else
+      {
+      int count = 1;
+      int i, j;
+      uid_t *vector;
+      char *p = list;
+      while (*p != 0) if (*p++ == ':') count++;
+
+      vector = malloc((count+1) * sizeof(uid_t));
+      vector[0] = (uid_t)count;
+
+      for (i = 1, j = 0; i <= count; list++, i++)
+        {
+        char name[64];
+
+        p = list;
+        while (*list != 0 && *list != ':') list++;
+        strncpy(name, p, list-p);
+        name[list-p] = 0;
+
+        if (name[0] == 0)
+          {
+          continue;
+          }
+        else if (name[strspn(name, "0123456789")] == 0)
+          {
+          vector[j++] = (uid_t)atoi(name);
+          }
+        else
+          {
+          struct passwd *pw = getpwnam(name);
+          if (pw == NULL)
+            {
+            printf("\n*** User \"%s\" (specified for FIXED_NEVER_USERS in one of the Makefiles) does not "
+              "exist.\n    Please review your build-time configuration.\n\n",
+              name);
+            return 1;
+            }
+          vector[j++] = pw->pw_uid;
+          }
+        }
+      fprintf(new, "#define FIXED_NEVER_USERS     %d", j);
+      for (i = 0; i < j; i++) fprintf(new, ", %d", (unsigned int)vector[i]);
+      fprintf(new, "\n");
+      }
+    continue;
+    }
+
+  /* WITH_CONTENT_SCAN is another special case: it must be set if either it or
+  WITH_OLD_DEMIME is set. */
+
+  if (strcmp(name, "WITH_CONTENT_SCAN") == 0)
+    {
+    char *wcs = getenv("WITH_CONTENT_SCAN");
+    char *wod = getenv("WITH_OLD_DEMIME");
+    char *dcc = getenv("EXPERIMENTAL_DCC");
+    if (wcs != NULL || wod != NULL || dcc != NULL)
+      fprintf(new, "#define WITH_CONTENT_SCAN     yes\n");
+    else fprintf(new, "/* WITH_CONTENT_SCAN not set */\n");
+    continue;
+    }
+
+  /* Otherwise, check whether a value exists in the environment. Remember if
+  it is an AUTH setting or SUPPORT_CRYPTEQ. */
+
+  if ((value = getenv(name)) != NULL)
+    {
+    int len;
+    len = 21 - (int)strlen(name);
+
+    if (strncmp(name, "AUTH_", 5) == 0) have_auth = 1;
+    if (strncmp(name, "SUPPORT_CRYPTEQ", 15) == 0) support_crypteq = 1;
+
+    /* The text value of LDAP_LIB_TYPE refers to a macro that gets set. */
+
+    if (strcmp(name, "LDAP_LIB_TYPE") == 0)
+      {
+      if (strcmp(value, "NETSCAPE") == 0 ||
+          strcmp(value, "UMICHIGAN") == 0 ||
+          strcmp(value, "OPENLDAP1") == 0 ||
+          strcmp(value, "OPENLDAP2") == 0 ||
+          strcmp(value, "SOLARIS") == 0 ||
+          strcmp(value, "SOLARIS7") == 0)              /* Compatibility */
+        {
+        fprintf(new, "#define LDAP_LIB_%s\n", value);
+        }
+      else
+        {
+        printf("\n*** LDAP_LIB_TYPE=%s is not a recognized LDAP library type."
+          "\n*** Please review your build-time configuration.\n\n", value);
+        return 1;
+        }
+      }
+
+    else if (strcmp(name, "RADIUS_LIB_TYPE") == 0)
+      {
+      if (strcmp(value, "RADIUSCLIENT") == 0 ||
+          strcmp(value, "RADIUSCLIENTNEW") == 0 ||
+          strcmp(value, "RADLIB") == 0)
+        {
+        fprintf(new, "#define RADIUS_LIB_%s\n", value);
+        }
+      else
+        {
+        printf("\n*** RADIUS_LIB_TYPE=%s is not a recognized RADIUS library type."
+          "\n*** Please review your build-time configuration.\n\n", value);
+        return 1;
+        }
+      }
+
+    /* Other macros get set to the environment value. */
+
+    else
+      {
+      fprintf(new, "#define %s ", name);
+      while(len-- > 0) fputc(' ', new);
+
+      /* LOG_FILE_PATH is now messy because it can be a path containing %s or
+      it can be "syslog" or ":syslog" or "syslog:path" or even "path:syslog". */
+
+      if (strcmp(name, "LOG_FILE_PATH") == 0)
+        {
+        char *ss = value;
+        for(;;)
+          {
+          char *pp;
+          char *sss = strchr(ss, ':');
+          if (sss != NULL)
+            {
+            strncpy(buffer, ss, sss-ss);
+            buffer[sss-ss] = 0;  /* For empty case */
+            }
+          else strcpy(buffer, ss);
+          pp = buffer + (int)strlen(buffer);
+          while (pp > buffer && isspace((unsigned char)pp[-1])) pp--;
+          *pp = 0;
+          if (buffer[0] != 0 && strcmp(buffer, "syslog") != 0)
+            check_percent_ess(buffer, name);
+          if (sss == NULL) break;
+          ss = sss + 1;
+          while (isspace((unsigned char)*ss)) ss++;
+          }
+        fprintf(new, "\"%s\"\n", value);
+        }
+
+      /* Timezone values HEADERS_CHARSET, TCP_WRAPPERS_DAEMON_NAME and
+      WHITELIST_D_MACROS get quoted */
+
+      else if (strcmp(name, "TIMEZONE_DEFAULT") == 0||
+               strcmp(name, "TCP_WRAPPERS_DAEMON_NAME") == 0||
+               strcmp(name, "HEADERS_CHARSET") == 0||
+               strcmp(name, "WHITELIST_D_MACROS") == 0)
+        fprintf(new, "\"%s\"\n", value);
+
+      /* GnuTLS constants; first is for debugging, others are tuning */
+
+      /* less than 0 is not-active; 0-9 are normal, API suggests higher
+      taken without problems */
+      else if (strcmp(name, "EXIM_GNUTLS_LIBRARY_LOG_LEVEL") == 0)
+        {
+        long nv;
+        char *end;
+        nv = strtol(value, &end, 10);
+        if (end != value && *end == '\0' && nv >= -1 && nv <= 100)
+          {
+          fprintf(new, "%s\n", value);
+          }
+        else
+          {
+          printf("Value of %s should be -1..9\n", name);
+          return 1;
+          }
+        }
+
+      /* how many bits Exim, as a client, demands must be in D-H */
+      /* 1024 is a historical figure; some sites actually use lower, so we
+      permit the value to be lowered "dangerously" low, but not "insanely"
+      low.  Though actually, 1024 is becoming "dangerous". */
+      else if ((strcmp(name, "EXIM_CLIENT_DH_MIN_MIN_BITS") == 0) ||
+               (strcmp(name, "EXIM_CLIENT_DH_DEFAULT_MIN_BITS") == 0) ||
+               (strcmp(name, "EXIM_SERVER_DH_BITS_PRE2_12") == 0))
+        {
+        long nv;
+        char *end;
+        nv = strtol(value, &end, 10);
+        if (end != value && *end == '\0' && nv >= 512 && nv < 500000)
+          {
+          fprintf(new, "%s\n", value);
+          }
+        else
+          {
+          printf("Unreasonable value (%s) of \"%s\".\n", value, name);
+          return 1;
+          }
+        }
+
+      /* For others, quote any paths and don't quote anything else */
+
+      else
+        {
+        if (value[0] == '/') fprintf(new, "\"%s\"\n", value);
+          else fprintf(new, "%s\n", value);
+        }
+      }
+    }
+
+  /* Value not defined in the environment; use the default */
+
+  else
+    {
+    char *t = p;
+    while (*p == ' ' || *p == '\t') p++;
+    if (*p != '\n') fputs(buffer, new); else
+      {
+      *t = 0;
+      if (strcmp(name, "BIN_DIRECTORY")   == 0 ||
+          strcmp(name, "CONFIGURE_FILE")  == 0)
+        {
+        printf("\n*** %s has not been defined in any of the Makefiles in the\n"
+          "    \"Local\" directory. "
+          "Please review your build-time configuration.\n\n", name);
+        return 1;
+        }
+
+      if (strcmp(name, "TIMEZONE_DEFAULT") == 0)
+        {
+        char *tz = getenv("TZ");
+        fprintf(new, "#define TIMEZONE_DEFAULT      ");
+        if (tz == NULL) fprintf(new, "NULL\n"); else
+          fprintf(new, "\"%s\"\n", tz);
+        }
+
+      else fprintf(new, "/* %s not set */\n", name);
+      }
+    }
+  }
+
+(void)fclose(base);
+
+/* If any AUTH macros were defined, ensure that SUPPORT_CRYPTEQ is also
+defined. */
+
+if (have_auth)
+  {
+  if (!support_crypteq) fprintf(new, "/* Force SUPPORT_CRYPTEQ for AUTH */\n"
+    "#define SUPPORT_CRYPTEQ\n");
+  }
+
+/* End off */
+
+fprintf(new, "\n/* End of config.h */\n");
+(void)fclose(new);
+return 0;
+}
+
+/* End of buildconfig.c */
diff --git a/src/child.c b/src/child.c
new file mode 100644 (file)
index 0000000..20083b4
--- /dev/null
@@ -0,0 +1,514 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+
+#include "exim.h"
+
+static void (*oldsignal)(int);
+
+
+/*************************************************
+*          Ensure an fd has a given value        *
+*************************************************/
+
+/* This function is called when we want to ensure that a certain fd has a
+specific value (one of 0, 1, 2). If it hasn't got it already, close the value
+we want, duplicate the fd, then close the old one.
+
+Arguments:
+  oldfd        original fd
+  newfd        the fd we want
+
+Returns:       nothing
+*/
+
+static void
+force_fd(int oldfd, int newfd)
+{
+if (oldfd == newfd) return;
+(void)close(newfd);
+(void)dup2(oldfd, newfd);
+(void)close(oldfd);
+}
+
+
+#ifndef STAND_ALONE
+/*************************************************
+*   Build argv list and optionally re-exec Exim  *
+*************************************************/
+
+/* This function is called when Exim wants to re-exec (overlay) itself in the
+current process. This is different to child_open_exim(), which runs another
+Exim process in parallel (but it then calls this function). The function's
+basic job is to build the argv list according to the values of current options
+settings. There is a basic list that all calls require, and an additional list
+that some do not require. Further additions can be given as additional
+arguments. An option specifies whether the exec() is actually to happen, and if
+so, what is to be done if it fails.
+
+Arguments:
+  exec_type      CEE_RETURN_ARGV => don't exec; return the argv list
+                 CEE_EXEC_EXIT   => just exit() on exec failure
+                 CEE_EXEC_PANIC  => panic-die on exec failure
+  kill_v         if TRUE, don't pass on the D_v flag
+  pcount         if not NULL, points to extra size of argv required, and if
+                   CEE_RETURN_ARGV is specified, it is updated to give the
+                   number of slots used
+  minimal        TRUE if only minimal argv is required
+  acount         number of additional arguments
+  ...            further values to add to argv
+
+Returns:         if CEE_RETURN_ARGV is given, returns a pointer to argv;
+                 otherwise, does not return
+*/
+
+uschar **
+child_exec_exim(int exec_type, BOOL kill_v, int *pcount, BOOL minimal,
+  int acount, ...)
+{
+int first_special = -1;
+int n = 0;
+int extra = (pcount != NULL)? *pcount : 0;
+uschar **argv =
+  store_get((extra + acount + MAX_CLMACROS + 16) * sizeof(char *));
+
+/* In all case, the list starts out with the path, any macros, and a changed
+config file. */
+
+argv[n++] = exim_path;
+if (clmacro_count > 0)
+  {
+  memcpy(argv + n, clmacros, clmacro_count * sizeof(uschar *));
+  n += clmacro_count;
+  }
+if (config_changed)
+  {
+  argv[n++] = US"-C";
+  argv[n++] = config_main_filename;
+  }
+
+/* These values are added only for non-minimal cases. If debug_selector is
+precisely D_v, we have to assume this was started by a non-admin user, and
+we suppress the flag when requested. (This happens when passing on an SMTP
+connection, and after ETRN.) If there's more debugging going on, an admin user
+was involved, so we do pass it on. */
+
+if (!minimal)
+  {
+  if (debug_selector == D_v)
+    {
+    if (!kill_v) argv[n++] = US"-v";
+    }
+  else
+    {
+    if (debug_selector != 0)
+      argv[n++] = string_sprintf("-d=0x%x", debug_selector);
+    }
+  if (dont_deliver) argv[n++] = US"-N";
+  if (queue_smtp) argv[n++] = US"-odqs";
+  if (synchronous_delivery) argv[n++] = US"-odi";
+  if (connection_max_messages >= 0)
+    argv[n++] = string_sprintf("-oB%d", connection_max_messages);
+  }
+
+/* Now add in any others that are in the call. Remember which they were,
+for more helpful diagnosis on failure. */
+
+if (acount > 0)
+  {
+  va_list ap;
+  va_start(ap, acount);
+  first_special = n;
+  while (acount-- > 0)
+    argv[n++] = va_arg(ap, uschar *);
+  va_end(ap);
+  }
+
+/* Terminate the list, and return it, if that is what is wanted. */
+
+argv[n] = NULL;
+if (exec_type == CEE_RETURN_ARGV)
+  {
+  if (pcount != NULL) *pcount = n;
+  return argv;
+  }
+
+/* Otherwise, do the exec() here, and handle the consequences of an unexpected
+failure. We know that there will always be at least one extra option in the
+call when exec() is done here, so it can be used to add to the panic data. */
+
+DEBUG(D_exec) debug_print_argv(argv);
+exim_nullstd();                            /* Make sure std{in,out,err} exist */
+execv(CS argv[0], (char *const *)argv);
+
+log_write(0,
+  LOG_MAIN | ((exec_type == CEE_EXEC_EXIT)? LOG_PANIC : LOG_PANIC_DIE),
+  "re-exec of exim (%s) with %s failed: %s", exim_path, argv[first_special],
+  strerror(errno));
+
+/* Get here if exec_type == CEE_EXEC_EXIT.
+Note: this must be _exit(), not exit(). */
+
+_exit(EX_EXECFAILED);
+
+return NULL;   /* To keep compilers happy */
+}
+
+
+
+
+/*************************************************
+*          Create a child Exim process           *
+*************************************************/
+
+/* This function is called when Exim wants to run a parallel instance of itself
+in order to inject a message via the standard input. The function creates a
+child process and runs Exim in it. It sets up a pipe to the standard input of
+the new process, and returns that to the caller via fdptr. The function returns
+the pid of the new process, or -1 if things go wrong. If debug_fd is
+non-negative, it is passed as stderr.
+
+This interface is now a just wrapper for the more complicated function
+child_open_exim2(), which has additional arguments. The wrapper must continue
+to exist, even if all calls from within Exim are changed, because it is
+documented for use from local_scan().
+
+Argument: fdptr   pointer to int for the stdin fd
+Returns:          pid of the created process or -1 if anything has gone wrong
+*/
+
+pid_t
+child_open_exim(int *fdptr)
+{
+return child_open_exim2(fdptr, US"<>", bounce_sender_authentication);
+}
+
+
+/* This is a more complicated function for creating a child Exim process, with
+more arguments.
+
+Arguments:
+  fdptr                   pointer to int for the stdin fd
+  sender                  for a sender address (data for -f)
+  sender_authentication   authenticated sender address or NULL
+
+Returns:          pid of the created process or -1 if anything has gone wrong
+*/
+
+pid_t
+child_open_exim2(int *fdptr, uschar *sender, uschar *sender_authentication)
+{
+int pfd[2];
+int save_errno;
+pid_t pid;
+
+/* Create the pipe and fork the process. Ensure that SIGCHLD is set to
+SIG_DFL before forking, so that the child process can be waited for. We
+sometimes get here with it set otherwise. Save the old state for resetting
+on the wait. */
+
+if (pipe(pfd) != 0) return (pid_t)(-1);
+oldsignal = signal(SIGCHLD, SIG_DFL);
+pid = fork();
+
+/* Child process: make the reading end of the pipe into the standard input and
+close the writing end. If debugging, pass debug_fd as stderr. Then re-exec
+Exim with appropriat options. In the test harness, use -odi unless queue_only
+is set, so that the bounce is fully delivered before returning. Failure is
+signalled with EX_EXECFAILED (specified by CEE_EXEC_EXIT), but this shouldn't
+occur. */
+
+if (pid == 0)
+  {
+  force_fd(pfd[pipe_read], 0);
+  (void)close(pfd[pipe_write]);
+  if (debug_fd > 0) force_fd(debug_fd, 2);
+  if (running_in_test_harness && !queue_only)
+    {
+    if (sender_authentication != NULL)
+      child_exec_exim(CEE_EXEC_EXIT, FALSE, NULL, FALSE, 9,
+        US "-odi", US"-t", US"-oem", US"-oi", US"-f", sender, US"-oMas",
+        sender_authentication, message_id_option);
+    else
+      child_exec_exim(CEE_EXEC_EXIT, FALSE, NULL, FALSE, 7,
+        US "-odi", US"-t", US"-oem", US"-oi", US"-f", sender,
+        message_id_option);
+    /* Control does not return here. */
+    }
+  else   /* Not test harness */
+    {
+    if (sender_authentication != NULL)
+      child_exec_exim(CEE_EXEC_EXIT, FALSE, NULL, FALSE, 8,
+        US"-t", US"-oem", US"-oi", US"-f", sender, US"-oMas",
+        sender_authentication, message_id_option);
+    else
+      child_exec_exim(CEE_EXEC_EXIT, FALSE, NULL, FALSE, 6,
+        US"-t", US"-oem", US"-oi", US"-f", sender, message_id_option);
+    /* Control does not return here. */
+    }
+  }
+
+/* Parent process. Save fork() errno and close the reading end of the stdin
+pipe. */
+
+save_errno = errno;
+(void)close(pfd[pipe_read]);
+
+/* Fork succeeded */
+
+if (pid > 0)
+  {
+  *fdptr = pfd[pipe_write];   /* return writing end of stdin pipe */
+  return pid;                 /* and pid of new process */
+  }
+
+/* Fork failed */
+
+(void)close(pfd[pipe_write]);
+errno = save_errno;
+return (pid_t)(-1);
+}
+#endif   /* STAND_ALONE */
+
+
+
+/*************************************************
+*         Create a non-Exim child process        *
+*************************************************/
+
+/* This function creates a child process and runs the given command in it. It
+sets up pipes to the standard input and output of the new process, and returns
+them to the caller. The standard error is cloned to the output. If there are
+any file descriptors "in the way" in the new process, they are closed. A new
+umask is supplied for the process, and an optional new uid and gid are also
+available. These are used by the queryprogram router to set an unprivileged id.
+SIGUSR1 is always disabled in the new process, as it is not going to be running
+Exim (the function child_open_exim() is provided for that). This function
+returns the pid of the new process, or -1 if things go wrong.
+
+Arguments:
+  argv        the argv for exec in the new process
+  envp        the envp for exec in the new process
+  newumask    umask to set in the new process
+  newuid      point to uid for the new process or NULL for no change
+  newgid      point to gid for the new process or NULL for no change
+  infdptr     pointer to int into which the fd of the stdin of the new process
+                is placed
+  outfdptr    pointer to int into which the fd of the stdout/stderr of the new
+                process is placed
+  wd          if not NULL, a path to be handed to chdir() in the new process
+  make_leader if TRUE, make the new process a process group leader
+
+Returns:      the pid of the created process or -1 if anything has gone wrong
+*/
+
+pid_t
+child_open_uid(uschar **argv, uschar **envp, int newumask, uid_t *newuid,
+  gid_t *newgid, int *infdptr, int *outfdptr, uschar *wd, BOOL make_leader)
+{
+int save_errno;
+int inpfd[2], outpfd[2];
+pid_t pid;
+
+/* Create the pipes. */
+
+if (pipe(inpfd) != 0) return (pid_t)(-1);
+if (pipe(outpfd) != 0)
+  {
+  (void)close(inpfd[pipe_read]);
+  (void)close(inpfd[pipe_write]);
+  return (pid_t)(-1);
+  }
+
+/* Fork the process. Ensure that SIGCHLD is set to SIG_DFL before forking, so
+that the child process can be waited for. We sometimes get here with it set
+otherwise. Save the old state for resetting on the wait. */
+
+oldsignal = signal(SIGCHLD, SIG_DFL);
+pid = fork();
+
+/* Handle the child process. First, set the required environment. We must do
+this before messing with the pipes, in order to be able to write debugging
+output when things go wrong. */
+
+if (pid == 0)
+  {
+  signal(SIGUSR1, SIG_IGN);
+  signal(SIGPIPE, SIG_DFL);
+
+  if (newgid != NULL && setgid(*newgid) < 0)
+    {
+    DEBUG(D_any) debug_printf("failed to set gid=%ld in subprocess: %s\n",
+      (long int)(*newgid), strerror(errno));
+    goto CHILD_FAILED;
+    }
+
+  if (newuid != NULL && setuid(*newuid) < 0)
+    {
+    DEBUG(D_any) debug_printf("failed to set uid=%ld in subprocess: %s\n",
+      (long int)(*newuid), strerror(errno));
+    goto CHILD_FAILED;
+    }
+
+  (void)umask(newumask);
+
+  if (wd != NULL && Uchdir(wd) < 0)
+    {
+    DEBUG(D_any) debug_printf("failed to chdir to %s: %s\n", wd,
+      strerror(errno));
+    goto CHILD_FAILED;
+    }
+
+  /* Becomes a process group leader if requested, and then organize the pipes.
+  Any unexpected failure is signalled with EX_EXECFAILED; these are all "should
+  never occur" failures, except for exec failing because the command doesn't
+  exist. */
+
+  if (make_leader && setpgid(0,0) < 0)
+    {
+    DEBUG(D_any) debug_printf("failed to set group leader in subprocess: %s\n",
+      strerror(errno));
+    goto CHILD_FAILED;
+    }
+
+  (void)close(inpfd[pipe_write]);
+  force_fd(inpfd[pipe_read], 0);
+
+  (void)close(outpfd[pipe_read]);
+  force_fd(outpfd[pipe_write], 1);
+
+  (void)close(2);
+  (void)dup2(1, 2);
+
+  /* Now do the exec */
+
+  if (envp == NULL) execv(CS argv[0], (char *const *)argv);
+    else execve(CS argv[0], (char *const *)argv, (char *const *)envp);
+
+  /* Failed to execv. Signal this failure using EX_EXECFAILED. We are
+  losing the actual errno we got back, because there is no way to return
+  this information. */
+
+  CHILD_FAILED:
+  _exit(EX_EXECFAILED);      /* Note: must be _exit(), NOT exit() */
+  }
+
+/* Parent process. Save any fork failure code, and close the reading end of the
+stdin pipe, and the writing end of the stdout pipe. */
+
+save_errno = errno;
+(void)close(inpfd[pipe_read]);
+(void)close(outpfd[pipe_write]);
+
+/* Fork succeeded; return the input/output pipes and the pid */
+
+if (pid > 0)
+  {
+  *infdptr = inpfd[pipe_write];
+  *outfdptr = outpfd[pipe_read];
+  return pid;
+  }
+
+/* Fork failed; reset fork errno before returning */
+
+(void)close(inpfd[pipe_write]);
+(void)close(outpfd[pipe_read]);
+errno = save_errno;
+return (pid_t)(-1);
+}
+
+
+
+
+/*************************************************
+*    Create child process without uid change     *
+*************************************************/
+
+/* This function is a wrapper for child_open_uid() that doesn't have the uid,
+gid and working directory changing arguments. The function is provided so as to
+have a clean interface for use from local_scan(), but also saves writing NULL
+arguments several calls that would otherwise use child_open_uid().
+
+Arguments:
+  argv        the argv for exec in the new process
+  envp        the envp for exec in the new process
+  newumask    umask to set in the new process
+  infdptr     pointer to int into which the fd of the stdin of the new process
+                is placed
+  outfdptr    pointer to int into which the fd of the stdout/stderr of the new
+                process is placed
+  make_leader if TRUE, make the new process a process group leader
+
+Returns:      the pid of the created process or -1 if anything has gone wrong
+*/
+
+pid_t
+child_open(uschar **argv, uschar **envp, int newumask, int *infdptr,
+  int *outfdptr, BOOL make_leader)
+{
+return child_open_uid(argv, envp, newumask, NULL, NULL, infdptr, outfdptr,
+  NULL, make_leader);
+}
+
+
+
+
+/*************************************************
+*           Close down child process             *
+*************************************************/
+
+/* Wait for the given process to finish, with optional timeout.
+
+Arguments
+  pid:      the pid to wait for
+  timeout:  maximum time to wait; 0 means for as long as it takes
+
+Returns:    >= 0          process terminated by exiting; value is process
+                            ending status; if an execve() failed, the value
+                            is typically 127 (defined as EX_EXECFAILED)
+            < 0 & > -256  process was terminated by a signal; value is the
+                            negation of the signal number
+            -256          timed out
+            -257          other error in wait(); errno still set
+*/
+
+int
+child_close(pid_t pid, int timeout)
+{
+int yield;
+
+if (timeout > 0)
+  {
+  sigalrm_seen = FALSE;
+  alarm(timeout);
+  }
+
+for(;;)
+  {
+  int status;
+  pid_t rc = waitpid(pid, &status, 0);
+  if (rc == pid)
+    {
+    int lowbyte = status & 255;
+    if (lowbyte == 0) yield = (status >> 8) & 255;
+      else yield = -lowbyte;
+    break;
+    }
+  if (rc < 0)
+    {
+    yield = (errno == EINTR && sigalrm_seen)? -256 : -257;
+    break;
+    }
+  }
+
+if (timeout > 0) alarm(0);
+
+signal(SIGCHLD, oldsignal);   /* restore */
+return yield;
+}
+
+/* End of child.c */
diff --git a/src/cnumber.h b/src/cnumber.h
new file mode 100644 (file)
index 0000000..d00491f
--- /dev/null
@@ -0,0 +1 @@
+1
diff --git a/src/config.h.defaults b/src/config.h.defaults
new file mode 100644 (file)
index 0000000..ba4615c
--- /dev/null
@@ -0,0 +1,200 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2014 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* The default settings for Exim configuration variables. A #define without
+any data just defines the existence of the variable; it won't get included
+in config.h unless some value is defined in Local/Makefile. If there is data,
+it's a default value. */
+
+#define ALT_CONFIG_PREFIX
+#define TRUSTED_CONFIG_LIST
+
+#define APPENDFILE_MODE            0600
+#define APPENDFILE_DIRECTORY_MODE  0700
+#define APPENDFILE_LOCKFILE_MODE   0600
+
+#define AUTH_CRAM_MD5
+#define AUTH_CYRUS_SASL
+#define AUTH_DOVECOT
+#define AUTH_GSASL
+#define AUTH_HEIMDAL_GSSAPI
+#define AUTH_PLAINTEXT
+#define AUTH_SPA
+
+#define AUTH_VARS                     3
+
+#define BIN_DIRECTORY
+
+#define CONFIGURE_FILE
+#define CONFIGURE_FILE_USE_EUID
+#define CONFIGURE_FILE_USE_NODE
+#define CONFIGURE_GROUP
+#define CONFIGURE_OWNER
+#define CYRUS_PWCHECK_SOCKET
+#define CYRUS_SASLAUTHD_SOCKET
+
+#define DEFAULT_CRYPT              crypt
+#define DELIVER_IN_BUFFER_SIZE     8192
+#define DELIVER_OUT_BUFFER_SIZE    8192
+#define DISABLE_DKIM
+#define DISABLE_PRDR
+#define DISABLE_OCSP
+#define DISABLE_DNSSEC
+#define DISABLE_D_OPTION
+
+#define ENABLE_DISABLE_FSYNC
+
+#define EXIMDB_DIRECTORY_MODE      0750
+#define EXIMDB_LOCK_TIMEOUT          60
+#define EXIMDB_LOCKFILE_MODE       0640
+#define EXIMDB_MODE                0640
+#define EXIM_CLIENT_DH_MIN_MIN_BITS         512
+#define EXIM_CLIENT_DH_DEFAULT_MIN_BITS    1024
+#define EXIM_GNUTLS_LIBRARY_LOG_LEVEL
+#define EXIM_SERVER_DH_BITS_PRE2_12
+#define EXIM_PERL
+/* Both uid and gid are triggered by this */
+#define EXIM_UID
+#define EXPAND_DLFUNC
+#define EXPAND_LISTMATCH_RHS
+
+#define FIXED_NEVER_USERS         "root"
+
+#define HAVE_CRYPT16
+#define HAVE_SA_LEN
+#define HEADERS_CHARSET           "ISO-8859-1"
+#define HEADER_ADD_BUFFER_SIZE    (8192 * 4)
+#define HEADER_MAXSIZE            (1024*1024)
+
+#define INPUT_DIRECTORY_MODE       0750
+#define IPV6_USE_INET_PTON
+
+#define LDAP_LIB_TYPE
+#define LOCAL_SCAN_HAS_OPTIONS
+
+#define LOG_DIRECTORY_MODE         0750
+#define LOG_FILE_PATH
+#define LOG_MODE                   0640
+
+#define LOOKUP_CDB
+#define LOOKUP_DBM
+#define LOOKUP_DNSDB
+#define LOOKUP_DSEARCH
+#define LOOKUP_IBASE
+#define LOOKUP_LDAP
+#define LOOKUP_LSEARCH
+#define LOOKUP_MYSQL
+#define LOOKUP_NIS
+#define LOOKUP_NISPLUS
+#define LOOKUP_ORACLE
+#define LOOKUP_PASSWD
+#define LOOKUP_PGSQL
+#define LOOKUP_SQLITE
+#define LOOKUP_TESTDB
+#define LOOKUP_WHOSON
+#define LOOKUP_WILDLSEARCH
+#define LOOKUP_NWILDLSEARCH
+
+#define LOOKUP_MODULE_DIR
+
+#define MAX_FILTER_SIZE           (1024*1024)
+#define MAX_LOCALHOST_NUMBER        256
+#define MAX_INCLUDE_SIZE          (1024*1024)
+#define MAX_INTERFACES              250
+#define MAX_NAMED_LIST               16
+#define MSGLOG_DIRECTORY_MODE      0750
+
+#define NVALGRIND
+
+#define PID_FILE_PATH
+
+#define RADIUS_CONFIG_FILE
+#define RADIUS_LIB_TYPE
+
+#define ROUTER_ACCEPT
+#define ROUTER_DNSLOOKUP
+#define ROUTER_IPLITERAL
+#define ROUTER_IPLOOKUP
+#define ROUTER_MANUALROUTE
+#define ROUTER_QUERYPROGRAM
+#define ROUTER_REDIRECT
+
+#define SPOOL_DIRECTORY
+#define SPOOL_DIRECTORY_MODE       0750
+#define SPOOL_MODE                 0640
+#define STRING_SPRINTF_BUFFER_SIZE (8192 * 4)
+
+#define SUPPORT_A6
+#define SUPPORT_CRYPTEQ
+#define SUPPORT_MAILDIR
+#define SUPPORT_MAILSTORE
+#define SUPPORT_MBX
+#define SUPPORT_MOVE_FROZEN_MESSAGES
+#define SUPPORT_PAM
+#define SUPPORT_TLS
+#define SUPPORT_TRANSLATE_IP_ADDRESS
+
+#define SYSLOG_LOG_PID
+#define SYSLOG_LONG_LINES
+
+#define TCP_WRAPPERS_DAEMON_NAME "exim"
+#define TIMEZONE_DEFAULT
+#define TMPDIR
+
+#define TRANSPORT_APPENDFILE
+#define TRANSPORT_AUTOREPLY
+#define TRANSPORT_LMTP
+#define TRANSPORT_PIPE
+#define TRANSPORT_SMTP
+
+#define USE_DB
+#define USE_GDBM
+#define USE_GNUTLS
+#define AVOID_GNUTLS_PKCS11
+#define USE_READLINE
+#define USE_TCP_WRAPPERS
+#define USE_TDB
+
+#define WHITELIST_D_MACROS
+
+#define WITH_CONTENT_SCAN
+#define WITH_OLD_DEMIME
+#define WITH_OLD_CLAMAV_STREAM
+
+/* EXPERIMENTAL features */
+#define EXPERIMENTAL_BRIGHTMAIL
+#define EXPERIMENTAL_CERTNAMES
+#define EXPERIMENTAL_DCC
+#define EXPERIMENTAL_DMARC
+#define EXPERIMENTAL_DSN
+#define EXPERIMENTAL_PROXY
+#define EXPERIMENTAL_REDIS
+#define EXPERIMENTAL_SPF
+#define EXPERIMENTAL_SRS
+#define EXPERIMENTAL_TPDA
+
+/* For developers */
+#define WANT_DEEPER_PRINTF_CHECKS
+
+/* Things that are not routinely changed but are nevertheless configurable
+just in case. */
+
+#define DNS_MAXNAME                1024
+#define EXPAND_MAXN                  20
+#define ROOT_UID                      0
+#define ROOT_GID                      0
+
+/* Sizes for integer arithmetic.
+Go for 64bit; can be overridden in OS/Makefile-FOO
+If you make it a different number of bits, provide a definition
+for EXIM_64B_MAX and _MIN in OS/oh.h-FOO */
+#define int_eximarith_t int64_t
+#define PR_EXIM_ARITH "%" PRId64               /* C99 standard, printf %lld */
+#define SC_EXIM_ARITH "%" SCNi64               /* scanf incl. 0x prefix */
+#define SC_EXIM_DEC   "%" SCNd64               /* scanf decimal */
+
+/* End of config.h.defaults */
diff --git a/src/configure.default b/src/configure.default
new file mode 100644 (file)
index 0000000..1274349
--- /dev/null
@@ -0,0 +1,822 @@
+######################################################################
+#                  Runtime configuration file for Exim               #
+######################################################################
+
+
+# This is a default configuration file which will operate correctly in
+# uncomplicated installations. Please see the manual for a complete list
+# of all the runtime configuration options that can be included in a
+# configuration file. There are many more than are mentioned here. The
+# manual is in the file doc/spec.txt in the Exim distribution as a plain
+# ASCII file. Other formats (PostScript, Texinfo, HTML, PDF) are available
+# from the Exim ftp sites. The manual is also online at the Exim web sites.
+
+
+# This file is divided into several parts, all but the first of which are
+# headed by a line starting with the word "begin". Only those parts that
+# are required need to be present. Blank lines, and lines starting with #
+# are ignored.
+
+
+########### IMPORTANT ########## IMPORTANT ########### IMPORTANT ###########
+#                                                                          #
+# Whenever you change Exim's configuration file, you *must* remember to    #
+# HUP the Exim daemon, because it will not pick up the new configuration   #
+# until you do. However, any other Exim processes that are started, for    #
+# example, a process started by an MUA in order to send a message, will    #
+# see the new configuration as soon as it is in place.                     #
+#                                                                          #
+# You do not need to HUP the daemon for changes in auxiliary files that    #
+# are referenced from this file. They are read every time they are used.   #
+#                                                                          #
+# It is usually a good idea to test a new configuration for syntactic      #
+# correctness before installing it (for example, by running the command    #
+# "exim -C /config/file.new -bV").                                         #
+#                                                                          #
+########### IMPORTANT ########## IMPORTANT ########### IMPORTANT ###########
+
+
+
+######################################################################
+#                    MAIN CONFIGURATION SETTINGS                     #
+######################################################################
+
+# Specify your host's canonical name here. This should normally be the fully
+# qualified "official" name of your host. If this option is not set, the
+# uname() function is called to obtain the name. In many cases this does
+# the right thing and you need not set anything explicitly.
+
+# primary_hostname =
+
+
+# The next three settings create two lists of domains and one list of hosts.
+# These lists are referred to later in this configuration using the syntax
+# +local_domains, +relay_to_domains, and +relay_from_hosts, respectively. They
+# are all colon-separated lists:
+
+domainlist local_domains = @
+domainlist relay_to_domains =
+hostlist   relay_from_hosts = localhost
+# (We rely upon hostname resolution working for localhost, because the default
+# uncommented configuration needs to work in IPv4-only environments.)
+
+# Most straightforward access control requirements can be obtained by
+# appropriate settings of the above options. In more complicated situations,
+# you may need to modify the Access Control Lists (ACLs) which appear later in
+# this file.
+
+# The first setting specifies your local domains, for example:
+#
+#   domainlist local_domains = my.first.domain : my.second.domain
+#
+# You can use "@" to mean "the name of the local host", as in the default
+# setting above. This is the name that is specified by primary_hostname,
+# as specified above (or defaulted). If you do not want to do any local
+# deliveries, remove the "@" from the setting above. If you want to accept mail
+# addressed to your host's literal IP address, for example, mail addressed to
+# "user@[192.168.23.44]", you can add "@[]" as an item in the local domains
+# list. You also need to uncomment "allow_domain_literals" below. This is not
+# recommended for today's Internet.
+
+# The second setting specifies domains for which your host is an incoming relay.
+# If you are not doing any relaying, you should leave the list empty. However,
+# if your host is an MX backup or gateway of some kind for some domains, you
+# must set relay_to_domains to match those domains. For example:
+#
+# domainlist relay_to_domains = *.myco.com : my.friend.org
+#
+# This will allow any host to relay through your host to those domains.
+# See the section of the manual entitled "Control of relaying" for more
+# information.
+
+# The third setting specifies hosts that can use your host as an outgoing relay
+# to any other host on the Internet. Such a setting commonly refers to a
+# complete local network as well as the localhost. For example:
+#
+# hostlist relay_from_hosts = <; 127.0.0.1 ; ::1 ; 192.168.0.0/16
+#
+# The "/16" is a bit mask (CIDR notation), not a number of hosts. Note that you
+# have to include 127.0.0.1 if you want to allow processes on your host to send
+# SMTP mail by using the loopback address. A number of MUAs use this method of
+# sending mail.  Often, connections are made to "localhost", which might be ::1
+# on IPv6-enabled hosts.  Do not forget CIDR for your IPv6 networks.
+
+# All three of these lists may contain many different kinds of item, including
+# wildcarded names, regular expressions, and file lookups. See the reference
+# manual for details. The lists above are used in the access control lists for
+# checking incoming messages. The names of these ACLs are defined here:
+
+acl_smtp_rcpt = acl_check_rcpt
+acl_smtp_data = acl_check_data
+
+# You should not change those settings until you understand how ACLs work.
+
+
+# If you are running a version of Exim that was compiled with the content-
+# scanning extension, you can cause incoming messages to be automatically
+# scanned for viruses. You have to modify the configuration in two places to
+# set this up. The first of them is here, where you define the interface to
+# your scanner. This example is typical for ClamAV; see the manual for details
+# of what to set for other virus scanners. The second modification is in the
+# acl_check_data access control list (see below).
+
+# av_scanner = clamd:/tmp/clamd
+
+
+# For spam scanning, there is a similar option that defines the interface to
+# SpamAssassin. You do not need to set this if you are using the default, which
+# is shown in this commented example. As for virus scanning, you must also
+# modify the acl_check_data access control list to enable spam scanning.
+
+# spamd_address = 127.0.0.1 783
+
+
+# If Exim is compiled with support for TLS, you may want to enable the
+# following options so that Exim allows clients to make encrypted
+# connections. In the authenticators section below, there are template
+# configurations for plaintext username/password authentication. This kind
+# of authentication is only safe when used within a TLS connection, so the
+# authenticators will only work if the following TLS settings are turned on
+# as well.
+
+# Allow any client to use TLS.
+
+# tls_advertise_hosts = *
+
+# Specify the location of the Exim server's TLS certificate and private key.
+# The private key must not be encrypted (password protected). You can put
+# the certificate and private key in the same file, in which case you only
+# need the first setting, or in separate files, in which case you need both
+# options.
+
+# tls_certificate = /etc/ssl/exim.crt
+# tls_privatekey = /etc/ssl/exim.pem
+
+# In order to support roaming users who wish to send email from anywhere,
+# you may want to make Exim listen on other ports as well as port 25, in
+# case these users need to send email from a network that blocks port 25.
+# The standard port for this purpose is port 587, the "message submission"
+# port. See RFC 4409 for details. Microsoft MUAs cannot be configured to
+# talk the message submission protocol correctly, so if you need to support
+# them you should also allow TLS-on-connect on the traditional but
+# non-standard port 465.
+
+# daemon_smtp_ports = 25 : 465 : 587
+# tls_on_connect_ports = 465
+
+
+# Specify the domain you want to be added to all unqualified addresses
+# here. An unqualified address is one that does not contain an "@" character
+# followed by a domain. For example, "caesar@rome.example" is a fully qualified
+# address, but the string "caesar" (i.e. just a login name) is an unqualified
+# email address. Unqualified addresses are accepted only from local callers by
+# default. See the recipient_unqualified_hosts option if you want to permit
+# unqualified addresses from remote sources. If this option is not set, the
+# primary_hostname value is used for qualification.
+
+# qualify_domain =
+
+
+# If you want unqualified recipient addresses to be qualified with a different
+# domain to unqualified sender addresses, specify the recipient domain here.
+# If this option is not set, the qualify_domain value is used.
+
+# qualify_recipient =
+
+
+# The following line must be uncommented if you want Exim to recognize
+# addresses of the form "user@[10.11.12.13]" that is, with a "domain literal"
+# (an IP address) instead of a named domain. The RFCs still require this form,
+# but it makes little sense to permit mail to be sent to specific hosts by
+# their IP address in the modern Internet. This ancient format has been used
+# by those seeking to abuse hosts by using them for unwanted relaying. If you
+# really do want to support domain literals, uncomment the following line, and
+# see also the "domain_literal" router below.
+
+# allow_domain_literals
+
+
+# No deliveries will ever be run under the uids of users specified by
+# never_users (a colon-separated list). An attempt to do so causes a panic
+# error to be logged, and the delivery to be deferred. This is a paranoic
+# safety catch. There is an even stronger safety catch in the form of the
+# FIXED_NEVER_USERS setting in the configuration for building Exim. The list of
+# users that it specifies is built into the binary, and cannot be changed. The
+# option below just adds additional users to the list. The default for
+# FIXED_NEVER_USERS is "root", but just to be absolutely sure, the default here
+# is also "root".
+
+# Note that the default setting means you cannot deliver mail addressed to root
+# as if it were a normal user. This isn't usually a problem, as most sites have
+# an alias for root that redirects such mail to a human administrator.
+
+never_users = root
+
+
+# The setting below causes Exim to do a reverse DNS lookup on all incoming
+# IP calls, in order to get the true host name. If you feel this is too
+# expensive, you can specify the networks for which a lookup is done, or
+# remove the setting entirely.
+
+host_lookup = *
+
+
+# The settings below, which are actually the same as the defaults in the
+# code, cause Exim to make RFC 1413 (ident) callbacks for all incoming SMTP
+# calls. You can limit the hosts to which these calls are made, and/or change
+# the timeout that is used. If you set the timeout to zero, all RFC 1413 calls
+# are disabled. RFC 1413 calls are cheap and can provide useful information
+# for tracing problem messages, but some hosts and firewalls have problems
+# with them. This can result in a timeout instead of an immediate refused
+# connection, leading to delays on starting up SMTP sessions. (The default was
+# reduced from 30s to 5s for release 4.61.)
+
+rfc1413_hosts = *
+rfc1413_query_timeout = 5s
+
+
+# By default, Exim expects all envelope addresses to be fully qualified, that
+# is, they must contain both a local part and a domain. If you want to accept
+# unqualified addresses (just a local part) from certain hosts, you can specify
+# these hosts by setting one or both of
+#
+# sender_unqualified_hosts =
+# recipient_unqualified_hosts =
+#
+# to control sender and recipient addresses, respectively. When this is done,
+# unqualified addresses are qualified using the settings of qualify_domain
+# and/or qualify_recipient (see above).
+
+
+# If you want Exim to support the "percent hack" for certain domains,
+# uncomment the following line and provide a list of domains. The "percent
+# hack" is the feature by which mail addressed to x%y@z (where z is one of
+# the domains listed) is locally rerouted to x@y and sent on. If z is not one
+# of the "percent hack" domains, x%y is treated as an ordinary local part. This
+# hack is rarely needed nowadays; you should not enable it unless you are sure
+# that you really need it.
+#
+# percent_hack_domains =
+#
+# As well as setting this option you will also need to remove the test
+# for local parts containing % in the ACL definition below.
+
+
+# When Exim can neither deliver a message nor return it to sender, it "freezes"
+# the delivery error message (aka "bounce message"). There are also other
+# circumstances in which messages get frozen. They will stay on the queue for
+# ever unless one of the following options is set.
+
+# This option unfreezes frozen bounce messages after two days, tries
+# once more to deliver them, and ignores any delivery failures.
+
+ignore_bounce_errors_after = 2d
+
+# This option cancels (removes) frozen messages that are older than a week.
+
+timeout_frozen_after = 7d
+
+
+# By default, messages that are waiting on Exim's queue are all held in a
+# single directory called "input" which it itself within Exim's spool
+# directory. (The default spool directory is specified when Exim is built, and
+# is often /var/spool/exim/.) Exim works best when its queue is kept short, but
+# there are circumstances where this is not always possible. If you uncomment
+# the setting below, messages on the queue are held in 62 subdirectories of
+# "input" instead of all in the same directory. The subdirectories are called
+# 0, 1, ... A, B, ... a, b, ... z. This has two benefits: (1) If your file
+# system degrades with many files in one directory, this is less likely to
+# happen; (2) Exim can process the queue one subdirectory at a time instead of
+# all at once, which can give better performance with large queues.
+
+# split_spool_directory = true
+
+
+# If you're in a part of the world where ASCII is not sufficient for most
+# text, then you're probably familiar with RFC2047 message header extensions.
+# By default, Exim adheres to the specification, including a limit of 76
+# characters to a line, with encoded words fitting within a line.
+# If you wish to use decoded headers in message filters in such a way
+# that successful decoding of malformed messages matters, you may wish to
+# configure Exim to be more lenient.
+#
+# check_rfc2047_length = false
+#
+# In particular, the Exim maintainers have had multiple reports of problems
+# from Russian administrators of issues until they disable this check,
+# because of some popular, yet buggy, mail composition software.
+
+
+# If you wish to be strictly RFC compliant, or if you know you'll be
+# exchanging email with systems that are not 8-bit clean, then you may
+# wish to disable advertising 8BITMIME.  Uncomment this option to do so.
+
+# accept_8bitmime = false
+
+
+######################################################################
+#                       ACL CONFIGURATION                            #
+#         Specifies access control lists for incoming SMTP mail      #
+######################################################################
+
+begin acl
+
+# This access control list is used for every RCPT command in an incoming
+# SMTP message. The tests are run in order until the address is either
+# accepted or denied.
+
+acl_check_rcpt:
+
+  # Accept if the source is local SMTP (i.e. not over TCP/IP). We do this by
+  # testing for an empty sending host field.
+
+  accept  hosts = :
+          control = dkim_disable_verify
+
+  #############################################################################
+  # The following section of the ACL is concerned with local parts that contain
+  # @ or % or ! or / or | or dots in unusual places.
+  #
+  # The characters other than dots are rarely found in genuine local parts, but
+  # are often tried by people looking to circumvent relaying restrictions.
+  # Therefore, although they are valid in local parts, these rules lock them
+  # out, as a precaution.
+  #
+  # Empty components (two dots in a row) are not valid in RFC 2822, but Exim
+  # allows them because they have been encountered. (Consider local parts
+  # constructed as "firstinitial.secondinitial.familyname" when applied to
+  # someone like me, who has no second initial.) However, a local part starting
+  # with a dot or containing /../ can cause trouble if it is used as part of a
+  # file name (e.g. for a mailing list). This is also true for local parts that
+  # contain slashes. A pipe symbol can also be troublesome if the local part is
+  # incorporated unthinkingly into a shell command line.
+  #
+  # Two different rules are used. The first one is stricter, and is applied to
+  # messages that are addressed to one of the local domains handled by this
+  # host. The line "domains = +local_domains" restricts it to domains that are
+  # defined by the "domainlist local_domains" setting above. The rule  blocks
+  # local parts that begin with a dot or contain @ % ! / or |. If you have
+  # local accounts that include these characters, you will have to modify this
+  # rule.
+
+  deny    message       = Restricted characters in address
+          domains       = +local_domains
+          local_parts   = ^[.] : ^.*[@%!/|]
+
+  # The second rule applies to all other domains, and is less strict. The line
+  # "domains = !+local_domains" restricts it to domains that are NOT defined by
+  # the "domainlist local_domains" setting above. The exclamation mark is a
+  # negating operator. This rule allows your own users to send outgoing
+  # messages to sites that use slashes and vertical bars in their local parts.
+  # It blocks local parts that begin with a dot, slash, or vertical bar, but
+  # allows these characters within the local part. However, the sequence /../
+  # is barred. The use of @ % and ! is blocked, as before. The motivation here
+  # is to prevent your users (or your users' viruses) from mounting certain
+  # kinds of attack on remote sites.
+
+  deny    message       = Restricted characters in address
+          domains       = !+local_domains
+          local_parts   = ^[./|] : ^.*[@%!] : ^.*/\\.\\./
+  #############################################################################
+
+  # Accept mail to postmaster in any local domain, regardless of the source,
+  # and without verifying the sender.
+
+  accept  local_parts   = postmaster
+          domains       = +local_domains
+
+  # Deny unless the sender address can be verified.
+
+  require verify        = sender
+
+  # Accept if the message comes from one of the hosts for which we are an
+  # outgoing relay. It is assumed that such hosts are most likely to be MUAs,
+  # so we set control=submission to make Exim treat the message as a
+  # submission. It will fix up various errors in the message, for example, the
+  # lack of a Date: header line. If you are actually relaying out out from
+  # MTAs, you may want to disable this. If you are handling both relaying from
+  # MTAs and submissions from MUAs you should probably split them into two
+  # lists, and handle them differently.
+
+  # Recipient verification is omitted here, because in many cases the clients
+  # are dumb MUAs that don't cope well with SMTP error responses. If you are
+  # actually relaying out from MTAs, you should probably add recipient
+  # verification here.
+
+  # Note that, by putting this test before any DNS black list checks, you will
+  # always accept from these hosts, even if they end up on a black list. The
+  # assumption is that they are your friends, and if they get onto a black
+  # list, it is a mistake.
+
+  accept  hosts         = +relay_from_hosts
+          control       = submission
+          control       = dkim_disable_verify
+
+  # Accept if the message arrived over an authenticated connection, from
+  # any host. Again, these messages are usually from MUAs, so recipient
+  # verification is omitted, and submission mode is set. And again, we do this
+  # check before any black list tests.
+
+  accept  authenticated = *
+          control       = submission
+          control       = dkim_disable_verify
+
+  # Insist that any other recipient address that we accept is either in one of
+  # our local domains, or is in a domain for which we explicitly allow
+  # relaying. Any other domain is rejected as being unacceptable for relaying.
+
+  require message = relay not permitted
+          domains = +local_domains : +relay_to_domains
+
+  # We also require all accepted addresses to be verifiable. This check will
+  # do local part verification for local domains, but only check the domain
+  # for remote domains. The only way to check local parts for the remote
+  # relay domains is to use a callout (add /callout), but please read the
+  # documentation about callouts before doing this.
+
+  require verify = recipient
+
+  #############################################################################
+  # There are no default checks on DNS black lists because the domains that
+  # contain these lists are changing all the time. However, here are two
+  # examples of how you can get Exim to perform a DNS black list lookup at this
+  # point. The first one denies, whereas the second just warns.
+  #
+  # deny    message       = rejected because $sender_host_address is in a black list at $dnslist_domain\n$dnslist_text
+  #         dnslists      = black.list.example
+  #
+  # warn    dnslists      = black.list.example
+  #         add_header    = X-Warning: $sender_host_address is in a black list at $dnslist_domain
+  #         log_message   = found in $dnslist_domain
+  #############################################################################
+
+  #############################################################################
+  # This check is commented out because it is recognized that not every
+  # sysadmin will want to do it. If you enable it, the check performs
+  # Client SMTP Authorization (csa) checks on the sending host. These checks
+  # do DNS lookups for SRV records. The CSA proposal is currently (May 2005)
+  # an Internet draft. You can, of course, add additional conditions to this
+  # ACL statement to restrict the CSA checks to certain hosts only.
+  #
+  # require verify = csa
+  #############################################################################
+
+  # At this point, the address has passed all the checks that have been
+  # configured, so we accept it unconditionally.
+
+  accept
+
+
+# This ACL is used after the contents of a message have been received. This
+# is the ACL in which you can test a message's headers or body, and in
+# particular, this is where you can invoke external virus or spam scanners.
+# Some suggested ways of configuring these tests are shown below, commented
+# out. Without any tests, this ACL accepts all messages. If you want to use
+# such tests, you must ensure that Exim is compiled with the content-scanning
+# extension (WITH_CONTENT_SCAN=yes in Local/Makefile).
+
+acl_check_data:
+
+  # Deny if the message contains a virus. Before enabling this check, you
+  # must install a virus scanner and set the av_scanner option above.
+  #
+  # deny    malware    = *
+  #         message    = This message contains a virus ($malware_name).
+
+  # Add headers to a message if it is judged to be spam. Before enabling this,
+  # you must install SpamAssassin. You may also need to set the spamd_address
+  # option above.
+  #
+  # warn    spam       = nobody
+  #         add_header = X-Spam_score: $spam_score\n\
+  #                      X-Spam_score_int: $spam_score_int\n\
+  #                      X-Spam_bar: $spam_bar\n\
+  #                      X-Spam_report: $spam_report
+
+  # Accept the message.
+
+  accept
+
+
+
+######################################################################
+#                      ROUTERS CONFIGURATION                         #
+#               Specifies how addresses are handled                  #
+######################################################################
+#     THE ORDER IN WHICH THE ROUTERS ARE DEFINED IS IMPORTANT!       #
+# An address is passed to each router in turn until it is accepted.  #
+######################################################################
+
+begin routers
+
+# This router routes to remote hosts over SMTP by explicit IP address,
+# when an email address is given in "domain literal" form, for example,
+# <user@[192.168.35.64]>. The RFCs require this facility. However, it is
+# little-known these days, and has been exploited by evil people seeking
+# to abuse SMTP relays. Consequently it is commented out in the default
+# configuration. If you uncomment this router, you also need to uncomment
+# allow_domain_literals above, so that Exim can recognize the syntax of
+# domain literal addresses.
+
+# domain_literal:
+#   driver = ipliteral
+#   domains = ! +local_domains
+#   transport = remote_smtp
+
+
+# This router routes addresses that are not in local domains by doing a DNS
+# lookup on the domain name. The exclamation mark that appears in "domains = !
+# +local_domains" is a negating operator, that is, it can be read as "not". The
+# recipient's domain must not be one of those defined by "domainlist
+# local_domains" above for this router to be used.
+#
+# If the router is used, any domain that resolves to 0.0.0.0 or to a loopback
+# interface address (127.0.0.0/8) is treated as if it had no DNS entry. Note
+# that 0.0.0.0 is the same as 0.0.0.0/32, which is commonly treated as the
+# local host inside the network stack. It is not 0.0.0.0/0, the default route.
+# If the DNS lookup fails, no further routers are tried because of the no_more
+# setting, and consequently the address is unrouteable.
+
+dnslookup:
+  driver = dnslookup
+  domains = ! +local_domains
+  transport = remote_smtp
+  ignore_target_hosts = 0.0.0.0 : 127.0.0.0/8
+# if ipv6-enabled then instead use:
+# ignore_target_hosts = <; 0.0.0.0 ; 127.0.0.0/8 ; ::1
+  no_more
+
+
+# This alternative router can be used when you want to send all mail to a
+# server which handles DNS lookups for you; an ISP will typically run such
+# a server for their customers.  If you uncomment "smarthost" then you
+# should comment out "dnslookup" above.  Setting a real hostname in route_data
+# wouldn't hurt either.
+
+# smarthost:
+#   driver = manualroute
+#   domains = ! +local_domains
+#   transport = remote_smtp
+#   route_data = MAIL.HOSTNAME.FOR.CENTRAL.SERVER.EXAMPLE
+#   ignore_target_hosts = <; 0.0.0.0 ; 127.0.0.0/8 ; ::1
+#   no_more
+
+
+# The remaining routers handle addresses in the local domain(s), that is those
+# domains that are defined by "domainlist local_domains" above.
+
+
+# This router handles aliasing using a linearly searched alias file with the
+# name SYSTEM_ALIASES_FILE. When this configuration is installed automatically,
+# the name gets inserted into this file from whatever is set in Exim's
+# build-time configuration. The default path is the traditional /etc/aliases.
+# If you install this configuration by hand, you need to specify the correct
+# path in the "data" setting below.
+#
+##### NB  You must ensure that the alias file exists. It used to be the case
+##### NB  that every Unix had that file, because it was the Sendmail default.
+##### NB  These days, there are systems that don't have it. Your aliases
+##### NB  file should at least contain an alias for "postmaster".
+#
+# If any of your aliases expand to pipes or files, you will need to set
+# up a user and a group for these deliveries to run under. You can do
+# this by uncommenting the "user" option below (changing the user name
+# as appropriate) and adding a "group" option if necessary. Alternatively, you
+# can specify "user" on the transports that are used. Note that the transports
+# listed below are the same as are used for .forward files; you might want
+# to set up different ones for pipe and file deliveries from aliases.
+
+system_aliases:
+  driver = redirect
+  allow_fail
+  allow_defer
+  data = ${lookup{$local_part}lsearch{SYSTEM_ALIASES_FILE}}
+# user = exim
+  file_transport = address_file
+  pipe_transport = address_pipe
+
+
+# This router handles forwarding using traditional .forward files in users'
+# home directories. If you want it also to allow mail filtering when a forward
+# file starts with the string "# Exim filter" or "# Sieve filter", uncomment
+# the "allow_filter" option.
+
+# The no_verify setting means that this router is skipped when Exim is
+# verifying addresses. Similarly, no_expn means that this router is skipped if
+# Exim is processing an EXPN command.
+
+# If you want this router to treat local parts with suffixes introduced by "-"
+# or "+" characters as if the suffixes did not exist, uncomment the two local_
+# part_suffix options. Then, for example, xxxx-foo@your.domain will be treated
+# in the same way as xxxx@your.domain by this router. Because this router is
+# not used for verification, if you choose to uncomment those options, then you
+# will *need* to make the same change to the localuser router.  (There are
+# other approaches, if this is undesirable, but they add complexity).
+
+# The check_ancestor option means that if the forward file generates an
+# address that is an ancestor of the current one, the current one gets
+# passed on instead. This covers the case where A is aliased to B and B
+# has a .forward file pointing to A.
+
+# The three transports specified at the end are those that are used when
+# forwarding generates a direct delivery to a file, or to a pipe, or sets
+# up an auto-reply, respectively.
+
+userforward:
+  driver = redirect
+  check_local_user
+# local_part_suffix = +* : -*
+# local_part_suffix_optional
+  file = $home/.forward
+# allow_filter
+  no_verify
+  no_expn
+  check_ancestor
+  file_transport = address_file
+  pipe_transport = address_pipe
+  reply_transport = address_reply
+
+
+# This router matches local user mailboxes. If the router fails, the error
+# message is "Unknown user".
+
+# If you want this router to treat local parts with suffixes introduced by "-"
+# or "+" characters as if the suffixes did not exist, uncomment the two local_
+# part_suffix options. Then, for example, xxxx-foo@your.domain will be treated
+# in the same way as xxxx@your.domain by this router.
+
+localuser:
+  driver = accept
+  check_local_user
+# local_part_suffix = +* : -*
+# local_part_suffix_optional
+  transport = local_delivery
+  cannot_route_message = Unknown user
+
+
+
+######################################################################
+#                      TRANSPORTS CONFIGURATION                      #
+######################################################################
+#                       ORDER DOES NOT MATTER                        #
+#     Only one appropriate transport is called for each delivery.    #
+######################################################################
+
+# A transport is used only when referenced from a router that successfully
+# handles an address.
+
+begin transports
+
+
+# This transport is used for delivering messages over SMTP connections.
+
+remote_smtp:
+  driver = smtp
+
+
+# This transport is used for local delivery to user mailboxes in traditional
+# BSD mailbox format. By default it will be run under the uid and gid of the
+# local user, and requires the sticky bit to be set on the /var/mail directory.
+# Some systems use the alternative approach of running mail deliveries under a
+# particular group instead of using the sticky bit. The commented options below
+# show how this can be done.
+
+local_delivery:
+  driver = appendfile
+  file = /var/mail/$local_part
+  delivery_date_add
+  envelope_to_add
+  return_path_add
+# group = mail
+# mode = 0660
+
+
+# This transport is used for handling pipe deliveries generated by alias or
+# .forward files. If the pipe generates any standard output, it is returned
+# to the sender of the message as a delivery error. Set return_fail_output
+# instead of return_output if you want this to happen only when the pipe fails
+# to complete normally. You can set different transports for aliases and
+# forwards if you want to - see the references to address_pipe in the routers
+# section above.
+
+address_pipe:
+  driver = pipe
+  return_output
+
+
+# This transport is used for handling deliveries directly to files that are
+# generated by aliasing or forwarding.
+
+address_file:
+  driver = appendfile
+  delivery_date_add
+  envelope_to_add
+  return_path_add
+
+
+# This transport is used for handling autoreplies generated by the filtering
+# option of the userforward router.
+
+address_reply:
+  driver = autoreply
+
+
+
+######################################################################
+#                      RETRY CONFIGURATION                           #
+######################################################################
+
+begin retry
+
+# This single retry rule applies to all domains and all errors. It specifies
+# retries every 15 minutes for 2 hours, then increasing retry intervals,
+# starting at 1 hour and increasing each time by a factor of 1.5, up to 16
+# hours, then retries every 6 hours until 4 days have passed since the first
+# failed delivery.
+
+# WARNING: If you do not have any retry rules at all (this section of the
+# configuration is non-existent or empty), Exim will not do any retries of
+# messages that fail to get delivered at the first attempt. The effect will
+# be to treat temporary errors as permanent. Therefore, DO NOT remove this
+# retry rule unless you really don't want any retries.
+
+# Address or Domain    Error       Retries
+# -----------------    -----       -------
+
+*                      *           F,2h,15m; G,16h,1h,1.5; F,4d,6h
+
+
+
+######################################################################
+#                      REWRITE CONFIGURATION                         #
+######################################################################
+
+# There are no rewriting specifications in this default configuration file.
+
+begin rewrite
+
+
+
+######################################################################
+#                   AUTHENTICATION CONFIGURATION                     #
+######################################################################
+
+# The following authenticators support plaintext username/password
+# authentication using the standard PLAIN mechanism and the traditional
+# but non-standard LOGIN mechanism, with Exim acting as the server.
+# PLAIN and LOGIN are enough to support most MUA software.
+#
+# These authenticators are not complete: you need to change the
+# server_condition settings to specify how passwords are verified.
+# They are set up to offer authentication to the client only if the
+# connection is encrypted with TLS, so you also need to add support
+# for TLS. See the global configuration options section at the start
+# of this file for more about TLS.
+#
+# The default RCPT ACL checks for successful authentication, and will accept
+# messages from authenticated users from anywhere on the Internet.
+
+begin authenticators
+
+# PLAIN authentication has no server prompts. The client sends its
+# credentials in one lump, containing an authorization ID (which we do not
+# use), an authentication ID, and a password. The latter two appear as
+# $auth2 and $auth3 in the configuration and should be checked against a
+# valid username and password. In a real configuration you would typically
+# use $auth2 as a lookup key, and compare $auth3 against the result of the
+# lookup, perhaps using the crypteq{}{} condition.
+
+#PLAIN:
+#  driver                     = plaintext
+#  server_set_id              = $auth2
+#  server_prompts             = :
+#  server_condition           = Authentication is not yet configured
+#  server_advertise_condition = ${if def:tls_in_cipher }
+
+# LOGIN authentication has traditional prompts and responses. There is no
+# authorization ID in this mechanism, so unlike PLAIN the username and
+# password are $auth1 and $auth2. Apart from that you can use the same
+# server_condition setting for both authenticators.
+
+#LOGIN:
+#  driver                     = plaintext
+#  server_set_id              = $auth1
+#  server_prompts             = <| Username: | Password:
+#  server_condition           = Authentication is not yet configured
+#  server_advertise_condition = ${if def:tls_in_cipher }
+
+
+######################################################################
+#                   CONFIGURATION FOR local_scan()                   #
+######################################################################
+
+# If you have built Exim to include a local_scan() function that contains
+# tables for private options, you can define those options here. Remember to
+# uncomment the "begin" line. It is commented by default because it provokes
+# an error with Exim binaries that are not built with LOCAL_SCAN_HAS_OPTIONS
+# set in the Local/Makefile.
+
+# begin local_scan
+
+
+# End of Exim configuration file
diff --git a/src/convert4r3.src b/src/convert4r3.src
new file mode 100755 (executable)
index 0000000..0ccaf6c
--- /dev/null
@@ -0,0 +1,1368 @@
+#! PERL_COMMAND -w
+
+# This is a Perl script that reads an Exim run-time configuration file and
+# checks for settings that were valid prior to release 3.00 but which were
+# obsoleted by that release. It writes a new file with suggested changes to
+# the standard output, and commentary about what it has done to stderr.
+
+# It is assumed that the input is a valid Exim configuration file.
+
+
+##################################################
+#             Analyse one line                   #
+##################################################
+
+# This is called for the main and the driver sections, not for retry
+# or rewrite sections (which are unmodified).
+
+sub checkline{
+my($line) = $_[0];
+
+return "comment" if $line =~ /^\s*(#|$)/;
+return "end"     if $line =~ /^\s*end\s*$/;
+
+# Macros are recognized only in the first section of the file.
+
+return "macro" if $prefix eq "" && $line =~ /^\s*[A-Z]/;
+
+# Pick out the name at the start and the rest of the line (into global
+# variables) and return whether the start of a driver or not.
+
+($i1,$name,$i2,$rest) = $line =~ /^(\s*)([a-z0-9_]+)(\s*)(.*?)\s*$/;
+return ($rest =~ /^:/)? "driver" : "option";
+}
+
+
+
+
+##################################################
+#       Add transport setting to a director      #
+##################################################
+
+# This function adds a transport setting to an aliasfile or forwardfile
+# director if a global setting exists and a local one does not. If neither
+# exist, it adds file/pipe/reply, but not the directory ones.
+
+sub add_transport{
+my($option) = @_;
+
+my($key) = "$prefix$driver.${option}_transport";
+if (!exists $o{$key})
+  {
+  if (exists $o{"address_${option}_transport"})
+    {
+    print STDOUT "# >> Option added by convert4r3\n";
+    printf STDOUT "${i1}${option}_transport = %s\n",
+      $o{"address_${option}_transport"};
+    printf STDERR
+      "\n%03d ${option}_transport added to $driver director.\n",
+      ++$count;
+    }
+  else
+    {
+    if ($option eq "pipe" || $option eq "file" || $option eq "reply")
+      {
+      print STDOUT "# >> Option added by convert4r3\n";
+      printf STDOUT "${i1}${option}_transport = address_${option}\n";
+      printf STDERR
+        "\n%03d ${option}_transport added to $driver director.\n",
+        ++$count;
+      }
+    }
+  }
+}
+
+
+
+
+##################################################
+#       Negate a list of things                  #
+##################################################
+
+sub negate {
+my($list) = $_[0];
+
+return $list if ! defined $list;
+
+($list) = $list =~ /^"?(.*?)"?\s*$/s;
+
+# Under Perl 5.005 we can split very nicely at colons, ignoring double
+# colons, like this:
+#
+# @split = split /\s*(?<!:):(?!:)\s*(?:\\\s*)?/s, $list;
+#
+# However, we'd better make this work under Perl 5.004, since there is
+# a lot of that about.
+
+$list =~ s/::/>%%%%</g;
+@split = split /\s*:\s*(?:\\\s*)?/s, $list;
+foreach $item (@split)
+  {
+  $item =~ s/>%%%%</::/g;
+  }
+
+$" = " : \\\n    ! ";
+return "! @split";
+}
+
+
+
+
+
+##################################################
+#          Skip blank lines                      #
+##################################################
+
+# This function is called after we have generated no output for an option;
+# it skips subsequent blank lines if the previous line was blank.
+
+sub skipblanks {
+my($i) = $_[0];
+if ($last_was_blank)
+  {
+  $i++ while $c[$i+1] =~ /^\s*$/;
+  }
+return $i;
+}
+
+
+
+
+
+##################################################
+#       Get base name of data key                #
+##################################################
+
+sub base {
+return "$_[0]" if $_[0] !~ /^(?:d|r|t)\.[^.]+\.(.*)/;
+return $1;
+}
+
+
+
+##################################################
+#     Amalgamate accept/reject/reject_except     #
+##################################################
+
+# This function amalgamates the three previous kinds of
+# option into a single list, using negation for the middle one if
+# the final argument is "+", or for the outer two if the final
+# argument is "-".
+
+sub amalgamate {
+my($accept,$reject,$reject_except,$name);
+my($last_was_negated) = 0;
+my($join) = "";
+
+$accept = $o{$_[0]};
+$reject = $o{$_[1]};
+$reject_except = $o{$_[2]};
+$name = $_[3];
+
+if ($_[4] eq "+")
+  {
+  ($accept) = $accept =~ /^"?(.*?)"?\s*$/s if defined $accept;
+  $reject = &negate($reject) if defined $reject;
+  ($reject_except) = $reject_except =~ /^"?(.*?)"?\s*$/s if defined $reject_except;
+  }
+else
+  {
+  $accept = &negate($accept) if defined $accept;
+  ($reject) = $reject =~ /^"?(.*?)"?\s*$/s if defined $reject;
+  $reject_except = &negate($reject_except) if defined $reject_except;
+  }
+
+print STDOUT "# >> Option rewritten by convert4r3\n";
+print STDOUT "${i1}$name = \"";
+
+if (defined $reject_except)
+  {
+  print STDOUT "$reject_except";
+  $join = " : \\\n    ";
+  $last_was_negated = ($_[4] ne "+");
+  }
+if (defined $reject)
+  {
+  print STDOUT "$join$reject";
+  $join = " : \\\n    ";
+  $last_was_negated = ($_[4] eq "+");
+  }
+if (defined $accept)
+  {
+  print STDOUT "$join$accept";
+  $last_was_negated = ($_[4] ne "+");
+  $join = " : \\\n    ";
+  }
+
+print STDOUT "$join*" if $last_was_negated;
+
+print STDOUT "\"\n";
+
+my($driver_name);
+my($driver_type) = "";
+
+if ($_[0] =~ /^(d|r|t)\.([^.]+)\./ ||
+    $_[1] =~ /^(d|r|t)\.([^.]+)\./ ||
+    $_[2] =~ /^(d|r|t)\.([^.]+)\./)
+  {
+  $driver_type = ($1 eq 'd')? "director" : ($1 eq 'r')? "router" : "transport";
+  $driver_name = $2;
+  }
+
+my($x) = ($driver_type ne "")? " in \"$driver_name\" $driver_type" : "";
+
+my($l0) = &base($_[0]);
+my($l1) = &base($_[1]);
+my($l2) = &base($_[2]);
+
+
+if ($l2 eq "")
+  {
+  if ($l0 eq "")
+    {
+    printf STDERR "\n%03d $l1 converted to $name$x.\n", ++$count;
+    }
+  else
+    {
+    printf STDERR "\n%03d $l0 and $l1\n    amalgamated into $name$x.\n",
+      ++$count;
+    }
+  }
+else
+  {
+  if ($l1 eq "")
+    {
+    printf STDERR "\n%03d $l0 and $l2\n    amalgamated into $name$x.\n",
+      ++$count;
+    }
+  else
+    {
+    printf STDERR "\n%03d $l0, $l1 and $l2\n    amalgamated into " .
+      "$name$x.\n", ++$count;
+    }
+  }
+}
+
+
+
+
+##################################################
+#         Join two lists, if they exist          #
+##################################################
+
+sub pair{
+my($l1) = $o{"$_[0]"};
+my($l2) = $o{"$_[1]"};
+
+return $l2 if (!defined $l1);
+return $l1 if (!defined $l2);
+
+($l1) = $l1 =~ /^"?(.*?)"?\s*$/s;
+($l2) = $l2 =~ /^"?(.*?)"?\s*$/s;
+
+return "$l1 : $l2";
+}
+
+
+
+
+##################################################
+#  Amalgamate accept/reject/reject_except pairs  #
+##################################################
+
+# This is like amalgamate, but it combines pairs of arguments, and
+# doesn't output commentary (easier to write a generic one for the few
+# cases).
+
+sub amalgamatepairs {
+my($accept) = &pair($_[0], $_[1]);
+my($reject) = &pair($_[2], $_[3]);
+my($reject_except) = &pair($_[4], $_[5]);
+my($last_was_negated) = 0;
+my($join) = "";
+
+if ($_[7] eq "+")
+  {
+  ($accept) = $accept =~ /^"?(.*?)"?\s*$/s if defined $accept;
+  $reject = &negate($reject) if defined $reject;
+  ($reject_except) = $reject_except =~ /^"?(.*?)"?\s*$/s if defined $reject_except;
+  }
+else
+  {
+  $accept = &negate($accept) if defined $accept;
+  ($reject) = $reject =~ /^"?(.*?)"?$/s if defined $reject;
+  $reject_except = &negate($reject_except) if defined $reject_except;
+  }
+
+print STDOUT "# >> Option rewritten by convert4r3\n";
+print STDOUT "${i1}$_[6] = \"";
+
+if (defined $reject_except)
+  {
+  print STDOUT "$reject_except";
+  $join = " : \\\n    ";
+  $last_was_negated = ($_[7] ne "+");
+  }
+if (defined $reject)
+  {
+  print STDOUT "$join$reject";
+  $join = " : \\\n    ";
+  $last_was_negated = ($_[7] eq "+");
+  }
+if (defined $accept)
+  {
+  print STDOUT "$join$accept";
+  $last_was_negated = ($_[7] ne "+");
+  $join = " : \\\n    ";
+  }
+
+print STDOUT "$join*" if $last_was_negated;
+print STDOUT "\"\n";
+}
+
+
+
+##################################################
+#      Amalgamate boolean and exception list(s)  #
+##################################################
+
+sub amalgboolandlist {
+my($name,$bool,$e1,$e2) = @_;
+
+print STDOUT "# >> Option rewritten by convert4r3\n";
+if ($bool eq "false")
+  {
+  printf STDOUT "$i1$name =\n";
+  }
+else
+  {
+  printf STDOUT "$i1$name = ";
+  my($n1) = &negate($o{$e1});
+  my($n2) = &negate($o{$e2});
+  if (!defined $n1 && !defined $n2)
+    {
+    print STDOUT "*\n";
+    }
+  elsif (!defined $n1)
+    {
+    print STDOUT "\"$n2 : \\\n    *\"\n";
+    }
+  elsif (!defined $n2)
+    {
+    print STDOUT "\"$n1 : \\\n    *\"\n";
+    }
+  else
+    {
+    print STDOUT "\"$n1 : \\\n    $n2 : \\\n    *\"\n";
+    }
+  }
+}
+
+
+
+##################################################
+#             Convert mask format                #
+##################################################
+
+# This function converts an address and mask in old-fashioned dotted-quad
+# format into an address plus a new format mask.
+
+@byte_list = (0, 128, 192, 224, 240, 248, 252, 254, 255);
+
+sub mask {
+my($address,$mask) = @_;
+my($length) = 0;
+my($i, $j);
+
+my(@bytes) = split /\./, $mask;
+
+for ($i = 0; $i < 4; $i++)
+  {
+  for ($j = 0; $j <= 8; $j++)
+    {
+    if ($bytes[$i] == $byte_list[$j])
+      {
+      $length += $j;
+      if ($j != 8)
+        {
+        for ($i++; $i < 4; $i++)
+          {
+          $j = 9 if ($bytes[$i] != 0);
+          }
+        }
+      last;
+      }
+    }
+
+  if ($j > 8)
+    {
+    print STDERR "*** IP mask $mask cannot be converted to /n format. ***\n";
+    return "$address/$mask";
+    }
+  }
+
+if (!defined $masks{$mask})
+  {
+  printf STDERR "\n%03d IP address mask $mask converted to /$length\n",
+    ++$count, $mask, $length;
+  $masks{$mask} = 1;
+  }
+
+return sprintf "$address/%d", $length;
+}
+
+
+
+
+
+##################################################
+#                  Main program                  #
+##################################################
+
+print STDERR "Exim pre-release 3.00 configuration file converter.\n";
+
+$count = 0;
+$seen_helo_accept_junk = 0;
+$seen_hold_domains = 0;
+$seen_receiver_unqualified = 0;
+$seen_receiver_verify_except = 0;
+$seen_receiver_verify_senders = 0;
+$seen_rfc1413_except = 0;
+$seen_sender_accept = 0;
+$seen_sender_accept_recipients = 0;
+$seen_sender_host_accept = 0;
+$seen_sender_host_accept_recipients = 0;
+$seen_sender_host_accept_relay = 0;
+$seen_sender_unqualified = 0;
+$seen_sender_verify_except_hosts = 0;
+$seen_smtp_etrn = 0;
+$seen_smtp_expn = 0;
+$seen_smtp_reserve = 0;
+$semicomma = 0;
+
+# Read the entire file into an array
+
+chomp(@c = <STDIN>);
+
+# First, go through the input and covert any net masks in the old dotted-quad
+# style into the new /n style.
+
+for ($i = 0; $i < scalar(@c); $i++)
+  {
+  $c[$i] =~
+    s"((?:\d{1,3}\.){3}\d{1,3})/((?:\d{1,3}\.){3}\d{1,3})"&mask($1,$2)"eg;
+  }
+
+# We now make two more passes over the input. In the first pass, we place all
+# the option values into an associative array. Main options are keyed by their
+# names; options for drivers are keyed by a driver type letter, the driver
+# name, and the option name, dot-separated. In the second pass we modify
+# the options if necessary, and write the output file.
+
+for ($pass = 1; $pass < 3; $pass++)
+  {
+  $prefix = "";
+  $driver = "";
+  $last_was_blank = 0;
+
+  for ($i = 0; $i < scalar(@c); $i++)
+    {
+    # Everything after the router section is just copied in pass 2 and
+    # ignored in pass 1.
+
+    if ($prefix eq "end")
+      {
+      print STDOUT "$c[$i]\n" if $pass == 2;
+      next;
+      }
+
+    # Analyze the line
+
+    $type = &checkline($c[$i]);
+
+    # Skip comments in pass 1; copy in pass 2
+
+    if ($type eq "comment")
+      {
+      $last_was_blank = ($c[$i] =~ /^\s*$/)? 1 : 0;
+      print STDOUT "$c[$i]\n" if $pass == 2;
+      next;
+      }
+
+    # Skip/copy macro definitions, but must handle continuations
+
+    if ($type eq "macro")
+      {
+      print STDOUT "$c[$i]\n" if $pass == 2;
+      while ($c[$i] =~ /\\\s*$/)
+        {
+        $i++;
+        print STDOUT "$c[$i]\n" if $pass == 2;
+        }
+      $last_was_blank = 0;
+      next;
+      }
+
+    # Handle end of section
+
+    if ($type eq "end")
+      {
+      $prefix = "end"if $prefix eq "r.";
+      $prefix = "r." if $prefix eq "d.";
+      $prefix = "d." if $prefix eq "t.";
+      $prefix = "t." if $prefix eq "";
+      print STDOUT "$c[$i]\n" if $pass == 2;
+      $last_was_blank = 0;
+      next;
+      }
+
+    # Handle start of a new driver
+
+    if ($type eq "driver")
+      {
+      $driver = $name;
+      print STDOUT "$c[$i]\n" if $pass == 2;
+      $last_was_blank = 0;
+      $seen_domains = 0;
+      $seen_local_parts = 0;
+      $seen_senders = 0;
+      $seen_mx_domains = 0;
+      $seen_serialize = 0;
+      next;
+      }
+
+    # Handle definition of an option
+
+    if ($type eq "option")
+      {
+      # Handle continued strings
+
+      if ($rest =~ /^=\s*".*\\$/)
+        {
+        for (;;)
+          {
+          $rest .= "\n$c[++$i]";
+          last unless $c[$i] =~ /(\\\s*$|^\s*#)/;
+          }
+        }
+
+      # Remove any terminating commas and semicolons in pass 2
+
+      if ($pass == 2 && $rest =~ /[;,]\s*$/)
+        {
+        $rest =~ s/\s*[;,]\s*$//;
+        if (!$semicomma)
+          {
+          printf STDERR
+            "\n%03d Terminating semicolons and commas removed from driver " .
+            "options.\n", ++$count;
+          $semicomma = 1;
+          }
+        }
+
+      # Convert all booleans to "x = true/false" format, but save the
+      # original so that it can be reproduced unchanged for options that
+      # are not of interest.
+
+      $origname = $name;
+      $origrest = $rest;
+
+      if ($name =~ /^not?_(.*)/)
+        {
+        $name = $1;
+        $rest = "= false";
+        }
+      elsif ($rest !~ /^=/)
+        {
+        $rest = "= true";
+        }
+
+      # Set up the associative array key, and get rid of the = on the data
+
+      $key = ($prefix eq "")? "$name" : "$prefix$driver.$name";
+      ($rest) = $rest =~ /^=\s*(.*)/s;
+
+      # Create the associative array of values in pass 1
+
+      if ($pass == 1)
+        {
+        $o{$key} = $rest;
+        }
+
+      # In pass 2, test for interesting options and do the necessary; copy
+      # all the rest.
+
+      else
+        {
+        ##########  Global configuration ##########
+
+        # These global options are abolished
+
+        if ($name eq "address_directory_transport" ||
+            $name eq "address_directory2_transport" ||
+            $name eq "address_file_transport" ||
+            $name eq "address_pipe_transport" ||
+            $name eq "address_reply_transport")
+          {
+          ($n2) = $name =~ /^address_(.*)/;
+          printf STDERR "\n%03d $name option deleted.\n", ++$count;
+          printf STDERR "    $n2 will be added to appropriate directors.\n";
+          $i = &skipblanks($i);
+          next;
+          }
+
+        # This debugging option is abolished
+
+        elsif ($name eq "sender_verify_log_details")
+          {
+          printf STDERR "\n%03d $name option deleted.\n", ++$count;
+          printf STDERR "    (Little used facility abolished.)\n";
+          }
+
+        # This option has been renamed
+
+        elsif ($name eq "check_dns_names")
+          {
+          $origname =~ s/check_dns/dns_check/;
+          print STDOUT "# >> Option rewritten by convert4r3\n";
+          print STDOUT "$i1$origname$i2$origrest\n";
+          printf STDERR "\n%03d check_dns_names renamed as dns_check_names.\n",
+            ++$count;
+          }
+
+        # helo_accept_junk_nets is abolished
+
+        elsif ($name eq "helo_accept_junk_nets" ||
+               $name eq "helo_accept_junk_hosts")
+          {
+          if (!$seen_helo_accept_junk)
+            {
+            &amalgamate("helo_accept_junk_nets", "",
+              "helo_accept_junk_hosts", "helo_accept_junk_hosts", "+");
+            $seen_helo_accept_junk = 1;
+            }
+          else
+            {
+            $i = &skipblanks($i);
+            next;
+            }
+          }
+
+        # helo_verify_except_{hosts,nets} are abolished, and helo_verify
+        # is now a host list instead of a boolean.
+
+        elsif ($name eq "helo_verify")
+          {
+          &amalgboolandlist("helo_verify", $rest, "helo_verify_except_hosts",
+            "helo_verify_except_nets");
+          printf STDERR "\n%03d helo_verify converted to host list.\n",
+            ++$count;
+          }
+        elsif ($name eq "helo_verify_except_hosts" ||
+               $name eq "helo_verify_except_nets")
+          {
+          $i = &skipblanks($i);
+          next;
+          }
+
+        # helo_verify_nets was an old synonym for host_lookup_nets; only
+        # one of them will be encountered. Change to a new name.
+
+        elsif ($name eq "helo_verify_nets" ||
+               $name eq "host_lookup_nets")
+          {
+          print STDOUT "# >> Option rewritten by convert4r3\n";
+          print STDOUT "${i1}host_lookup$i2$origrest\n";
+          printf STDERR "\n%03d $name renamed as host_lookup.\n", ++$count;
+          }
+
+        # hold_domains_except is abolished; add as negated items to
+        # hold_domains.
+
+        elsif ($name eq "hold_domains_except" ||
+               $name eq "hold_domains")
+          {
+          if ($seen_hold_domains)         # If already done with these
+            {                             # omit, and following blanks.
+            $i = &skipblanks($i);
+            next;
+            }
+          $seen_hold_domains = 1;
+
+          if (exists $o{"hold_domains_except"})
+            {
+            &amalgamate("hold_domains", "hold_domains_except", "",
+              "hold_domains", "+");
+            }
+          else
+            {
+            print STDOUT "$i1$origname$i2$origrest\n";
+            }
+          }
+
+        # ignore_fromline_nets is renamed as ignore_fromline_hosts
+
+        elsif ($name eq "ignore_fromline_nets")
+          {
+          $origname =~ s/_nets/_hosts/;
+          print STDOUT "# >> Option rewritten by convert4r3\n";
+          print STDOUT "$i1$origname$i2$origrest\n";
+          printf STDERR
+            "\n%03d ignore_fromline_nets renamed as ignore_fromline_hosts.\n",
+            ++$count;
+          }
+
+        # Output a warning for message filters with no transports set
+
+        elsif ($name eq "message_filter")
+          {
+          print STDOUT "$i1$origname$i2$origrest\n";
+
+          if (!exists $o{"message_filter_directory_transport"} &&
+              !exists $o{"message_filter_directory2_transport"} &&
+              !exists $o{"message_filter_file_transport"} &&
+              !exists $o{"message_filter_pipe_transport"} &&
+              !exists $o{"message_filter_reply_transport"})
+            {
+            printf STDERR
+              "\n%03d message_filter is set, but no message_filter transports "
+              . "are defined.\n"
+              . "    If your filter generates file or pipe deliveries, or "
+              . "auto-replies,\n"
+              . "    you will need to define "
+              . "message_filter_{file,pipe,reply}_transport\n"
+              . "    options, as required.\n", ++$count;
+            }
+          }
+
+        # queue_remote_except is abolished, and queue_remote is replaced by
+        # queue_remote_domains, which is a host list.
+
+        elsif ($name eq "queue_remote")
+          {
+          &amalgboolandlist("queue_remote_domains", $rest,
+            "queue_remote_except", "");
+          printf STDERR
+            "\n%03d queue_remote converted to domain list queue_remote_domains.\n",
+            ++$count;
+          }
+        elsif ($name eq "queue_remote_except")
+          {
+          $i = &skipblanks($i);
+          next;
+          }
+
+        # queue_smtp_except is abolished, and queue_smtp is replaced by
+        # queue_smtp_domains, which is a host list.
+
+        elsif ($name eq "queue_smtp")
+          {
+          &amalgboolandlist("queue_smtp_domains", $rest,
+            "queue_smtp_except", "");
+          printf STDERR
+            "\n%03d queue_smtp converted to domain list queue_smtp_domains.\n",
+            ++$count;
+          }
+        elsif ($name eq "queue_smtp_except")
+          {
+          $i = &skipblanks($i);
+          next;
+          }
+
+        # rbl_except_nets is replaced by rbl_hosts
+
+        elsif ($name eq "rbl_except_nets")
+          {
+          &amalgamate("", "rbl_except_nets", "", "rbl_hosts", "+");
+          }
+
+        # receiver_unqualified_nets is abolished
+
+        elsif ($name eq "receiver_unqualified_nets" ||
+               $name eq "receiver_unqualified_hosts")
+          {
+          if (!$seen_receiver_unqualified)
+            {
+            &amalgamate("receiver_unqualified_nets", "",
+              "receiver_unqualified_hosts", "receiver_unqualified_hosts", "+");
+            $seen_receiver_unqualified = 1;
+            }
+          else
+            {
+            $i = &skipblanks($i);
+            next;
+            }
+          }
+
+        # receiver_verify_except_{hosts,nets} are replaced by
+        # receiver_verify_hosts.
+
+        elsif ($name eq "receiver_verify_except_hosts" ||
+               $name eq "receiver_verify_except_nets")
+          {
+          if (!$seen_receiver_verify_except)
+            {
+            &amalgboolandlist("receiver_verify_hosts", "true",
+              "receiver_verify_except_hosts", "receiver_verify_except_nets");
+            printf STDERR
+              "\n%03d receiver_verify_except_{hosts,nets} converted to " .
+              "receiver_verify_hosts.\n",
+              ++$count;
+            $seen_receiver_verify_except = 1;
+            }
+          else
+            {
+            $i = &skipblanks($i);
+            next;
+            }
+          }
+
+        # receiver_verify_senders_except is abolished
+
+        elsif ($name eq "receiver_verify_senders" ||
+               $name eq "receiver_verify_senders_except")
+          {
+          if (defined $o{"receiver_verify_senders_except"})
+            {
+            if (!$seen_receiver_verify_senders)
+              {
+              &amalgamate("receiver_verify_senders",
+                "receiver_verify_senders_except", "",
+                "receiver_verify_senders", "+");
+              $seen_receiver_verify_senders = 1;
+              }
+            else
+              {
+              $i = &skipblanks($i);
+              next;
+              }
+            }
+          else
+            {
+            print STDOUT "$i1$origname$i2$origrest\n";
+            }
+          }
+
+        # rfc1413_except_{hosts,nets} are replaced by rfc1413_hosts.
+
+        elsif ($name eq "rfc1413_except_hosts" ||
+               $name eq "rfc1413_except_nets")
+          {
+          if (!$seen_rfc1413_except)
+            {
+            &amalgboolandlist("rfc1413_hosts", "true",
+              "rfc1413_except_hosts", "rfc1413_except_nets");
+            printf STDERR
+              "\n%03d rfc1413_except_{hosts,nets} converted to rfc1413_hosts.\n",
+              ++$count;
+            $seen_rfc1413_except = 1;
+            }
+          else
+            {
+            $i = &skipblanks($i);
+            next;
+            }
+          }
+
+        # sender_accept and sender_reject_except are abolished
+
+        elsif ($name eq "sender_accept" ||
+               $name eq "sender_reject")
+          {
+          if (!$seen_sender_accept)
+            {
+            &amalgamate("sender_accept", "sender_reject",
+              "sender_reject_except", "sender_reject", "-");
+            $seen_sender_accept = 1;
+            }
+          else
+            {
+            $i = &skipblanks($i);
+            next;
+            }
+          }
+
+        # sender_accept_recipients is also abolished; sender_reject_except
+        # also used to apply to this, so we include it here as well.
+
+        elsif ($name eq "sender_accept_recipients" ||
+               $name eq "sender_reject_recipients")
+          {
+          if (!$seen_sender_accept_recipients)
+            {
+            &amalgamate("sender_accept_recipients", "sender_reject_recipients",
+              "sender_reject_except", "sender_reject_recipients", "-");
+            $seen_sender_accept_recipients = 1;
+            }
+          else
+            {
+            $i = &skipblanks($i);
+            next;
+            }
+          }
+
+        # sender_reject_except must be removed
+
+        elsif ($name eq "sender_reject_except")
+          {
+          $i = &skipblanks($i);
+          next;
+          }
+
+        # sender_{host,net}_{accept,reject}[_except] all collapse into
+        # host_reject.
+
+        elsif ($name eq "sender_host_accept" ||
+               $name eq "sender_net_accept" ||
+               $name eq "sender_host_reject" ||
+               $name eq "sender_net_reject")
+          {
+          if (!$seen_sender_host_accept)
+            {
+            &amalgamatepairs("sender_host_accept", "sender_net_accept",
+              "sender_host_reject", "sender_net_reject",
+              "sender_host_reject_except", "sender_net_reject_except",
+              "host_reject", "-");
+            printf STDERR "\n%03d sender_{host,net}_{accept,reject} and " .
+              "sender_{host_net}_reject_except\n" .
+              "    amalgamated into host_reject.\n", ++$count;
+            $seen_sender_host_accept = 1;
+            }
+          else
+            {
+            $i = &skipblanks($i);
+            next;
+            }
+          }
+
+        # sender_{host,net}_{accept,reject}_recipients all collapse into
+        # host_reject_recipients.
+
+        elsif ($name eq "sender_host_accept_recipients" ||
+               $name eq "sender_net_accept_recipients" ||
+               $name eq "sender_host_reject_recipients" ||
+               $name eq "sender_net_reject_recipients")
+          {
+          if (!$seen_sender_host_accept_recipients)
+            {
+            &amalgamatepairs("sender_host_accept_recipients",
+              "sender_net_accept_recipients",
+              "sender_host_reject_recipients",
+              "sender_net_reject_recipients",
+              "sender_host_reject_except", "sender_net_reject_except",
+              "host_reject_recipients", "-");
+            printf STDERR "\n%03d sender_{host,net}_{accept,reject}_recipients"
+              . "\n    and sender_{host_net}_reject_except"
+              . "\n    amalgamated into host_reject_recipients.\n", ++$count;
+            $seen_sender_host_accept_recipients = 1;
+            }
+          else
+            {
+            $i = &skipblanks($i);
+            next;
+            }
+          }
+
+        # sender_{host,net}_reject_except must be removed
+
+        elsif ($name eq "sender_host_reject_except" ||
+               $name eq "sender_net_reject_except")
+          {
+          $i = &skipblanks($i);
+          next;
+          }
+
+        # sender_{host,net}_{accept,reject}_relay all collapse into
+        # host_accept_relay.
+
+        elsif ($name eq "sender_host_accept_relay" ||
+               $name eq "sender_net_accept_relay" ||
+               $name eq "sender_host_reject_relay" ||
+               $name eq "sender_net_reject_relay")
+          {
+          if (!$seen_sender_host_accept_relay)
+            {
+            &amalgamatepairs("sender_host_accept_relay",
+              "sender_net_accept_relay",
+              "sender_host_reject_relay",
+              "sender_net_reject_relay",
+              "sender_host_reject_relay_except",
+              "sender_net_reject_relay_except",
+              "host_accept_relay", "+");
+            printf STDERR "\n%03d sender_{host,net}_{accept,reject}_relay"
+              . "\n    and sender_{host_net}_reject_relay_except"
+              . "\n    amalgamated into host_accept_relay.\n", ++$count;
+            $seen_sender_host_accept_relay = 1;
+            }
+          else
+            {
+            $i = &skipblanks($i);
+            next;
+            }
+          }
+
+        # sender_{host,net}_reject_relay_except must be removed
+
+        elsif ($name eq "sender_host_reject_relay_except" ||
+               $name eq "sender_net_reject_relay_except")
+          {
+          $i = &skipblanks($i);
+          next;
+          }
+
+
+        # sender_unqualified_nets is abolished
+
+        elsif ($name eq "sender_unqualified_nets" ||
+               $name eq "sender_unqualified_hosts")
+          {
+          if (!$seen_sender_unqualified)
+            {
+            &amalgamate("sender_unqualified_nets", "",
+              "sender_unqualified_hosts", "sender_unqualified_hosts", "+");
+            $seen_sender_unqualified = 1;
+            }
+          else
+            {
+            $i = &skipblanks($i);
+            next;
+            }
+          }
+
+        # sender_verify_except_{hosts,nets} are replaced by sender_verify_hosts.
+
+        elsif ($name eq "sender_verify_except_hosts" ||
+               $name eq "sender_verify_except_nets")
+          {
+          if (!$seen_sender_verify_except_hosts)
+            {
+            &amalgboolandlist("sender_verify_hosts", "true",
+              "sender_verify_except_hosts", "sender_verify_except_nets");
+            printf STDERR
+              "\n%03d sender_verify_except_{hosts,nets} converted to " .
+              "sender_verify_hosts.\n",
+              ++$count;
+            $seen_sender_verify_except_hosts = 1;
+            }
+          else
+            {
+            $i = &skipblanks($i);
+            next;
+            }
+          }
+
+        # smtp_etrn_nets is abolished
+
+        elsif ($name eq "smtp_etrn_nets" ||
+               $name eq "smtp_etrn_hosts")
+          {
+          if (!$seen_smtp_etrn)
+            {
+            &amalgamate("smtp_etrn_nets", "",
+              "smtp_etrn_hosts", "smtp_etrn_hosts", "+");
+            $seen_smtp_etrn = 1;
+            }
+          else
+            {
+            $i = &skipblanks($i);
+            next;
+            }
+          }
+
+        # smtp_expn_nets is abolished
+
+        elsif ($name eq "smtp_expn_nets" ||
+               $name eq "smtp_expn_hosts")
+          {
+          if (!$seen_smtp_expn)
+            {
+            &amalgamate("smtp_expn_nets", "",
+              "smtp_expn_hosts", "smtp_expn_hosts", "+");
+            $seen_smtp_expn = 1;
+            }
+          else
+            {
+            $i = &skipblanks($i);
+            next;
+            }
+          }
+
+        # This option has been renamed
+
+        elsif ($name eq "smtp_log_connections")
+          {
+          $origname =~ s/smtp_log/log_smtp/;
+          print STDOUT "# >> Option rewritten by convert4r3\n";
+          print STDOUT "$i1$origname$i2$origrest\n";
+          printf STDERR "\n%03d smtp_log_connections renamed as " .
+            "log_smtp_connections.\n",
+            ++$count;
+          }
+
+        # smtp_reserve_nets is abolished
+
+        elsif ($name eq "smtp_reserve_nets" ||
+               $name eq "smtp_reserve_hosts")
+          {
+          if (!$seen_smtp_reserve)
+            {
+            &amalgamate("smtp_reserve_nets", "",
+              "smtp_reserve_hosts", "smtp_reserve_hosts", "+");
+            $seen_smtp_reserve = 1;
+            }
+          else
+            {
+            $i = &skipblanks($i);
+            next;
+            }
+          }
+
+        ###########  Driver configurations  ##########
+
+        # For aliasfile and forwardfile directors, add file, pipe, and
+        # reply transports - copying from the globals if they are set.
+
+        elsif ($name eq "driver")
+          {
+          $driver_type = $rest;
+          print STDOUT "$i1$origname$i2$origrest\n";
+          if ($rest eq "aliasfile" || $rest eq "forwardfile")
+            {
+            &add_transport("directory");
+            &add_transport("directory2");
+            &add_transport("file");
+            &add_transport("pipe");
+            &add_transport("reply") if $rest eq "forwardfile";
+            }
+          }
+
+        # except_domains is abolished; add as negated items to domains.
+
+        elsif ($name eq "except_domains" ||
+               $name eq "domains")
+          {
+          if ($seen_domains)              # If already done with these
+            {                             # omit, and following blanks.
+            $i = &skipblanks($i);
+            next;
+            }
+          $seen_domains = 1;
+
+          if (exists $o{"$prefix$driver.except_domains"})
+            {
+            &amalgamate("$prefix$driver.domains",
+                         "$prefix$driver.except_domains", "",
+              "domains", "+");
+            }
+          else
+            {
+            print STDOUT "$i1$origname$i2$origrest\n";
+            }
+          }
+
+        # except_local_parts is abolished; add as negated items to
+        # local_parts.
+
+        elsif ($name eq "except_local_parts" ||
+               $name eq "local_parts")
+          {
+          if ($seen_local_parts)              # If already done with these
+            {                             # omit, and following blanks.
+            $i = &skipblanks($i);
+            next;
+            }
+          $seen_local_parts = 1;
+
+          if (exists $o{"$prefix$driver.except_local_parts"})
+            {
+            &amalgamate("$prefix$driver.local_parts",
+                         "$prefix$driver.except_local_parts", "",
+              "local_parts", "+");
+            }
+          else
+            {
+            print STDOUT "$i1$origname$i2$origrest\n";
+            }
+          }
+
+        # except_senders is abolished; add as negated items to senders
+
+        elsif ($name eq "except_senders" ||
+               $name eq "senders")
+          {
+          if ($seen_senders)              # If already done with these
+            {                             # omit, and following blanks.
+            $i = &skipblanks($i);
+            next;
+            }
+          $seen_senders = 1;
+
+          if (exists $o{"$prefix$driver.except_senders"})
+            {
+            &amalgamate("$prefix$driver.senders",
+                         "$prefix$driver.except_senders", "",
+              "senders", "+");
+            }
+          else
+            {
+            print STDOUT "$i1$origname$i2$origrest\n";
+            }
+          }
+
+        # This option has been renamed
+
+        elsif ($name eq "directory" && $driver_type eq "aliasfile")
+          {
+          $origname =~ s/directory/home_directory/;
+          print STDOUT "# >> Option rewritten by convert4r3\n";
+          print STDOUT "$i1$origname$i2$origrest\n";
+          printf STDERR "\n%03d directory renamed as " .
+            "home_directory in \"$driver\" director.\n",
+            ++$count;
+          }
+
+        # This option has been renamed
+
+        elsif ($name eq "directory" && $driver_type eq "forwardfile")
+          {
+          $origname =~ s/directory/file_directory/;
+          print STDOUT "# >> Option rewritten by convert4r3\n";
+          print STDOUT "$i1$origname$i2$origrest\n";
+          printf STDERR "\n%03d directory renamed as " .
+            "file_directory in \"$driver\" director.\n",
+            ++$count;
+          }
+
+        # This option has been renamed
+
+        elsif ($name eq "forbid_filter_log" && $driver_type eq "forwardfile")
+          {
+          $origname =~ s/log/logwrite/;
+          print STDOUT "# >> Option rewritten by convert4r3\n";
+          print STDOUT "$i1$origname$i2$origrest\n";
+          printf STDERR "\n%03d forbid_filter_log renamed as " .
+            "forbid_filter_logwrite in \"$driver\" director.\n",
+            ++$count;
+          }
+
+        # This option has been renamed
+
+        elsif ($name eq "directory" && $driver_type eq "localuser")
+          {
+          $origname =~ s/directory/match_directory/;
+          print STDOUT "# >> Option rewritten by convert4r3\n";
+          print STDOUT "$i1$origname$i2$origrest\n";
+          printf STDERR "\n%03d directory renamed as " .
+            "match_directory in \"$driver\" director.\n",
+            ++$count;
+          }
+
+        # mx_domains_except (and old synonym non_mx_domains) are abolished
+        # (both lookuphost router and smtp transport)
+
+        elsif ($name eq "mx_domains" ||
+               $name eq "mx_domains_except" ||
+               $name eq "non_mx_domains")
+          {
+          if ($seen_mx_domains)              # If already done with these
+            {                             # omit, and following blanks.
+            $i = &skipblanks($i);
+            next;
+            }
+          $seen_mx_domains = 1;
+
+          if (exists $o{"$prefix$driver.mx_domains_except"} ||
+              exists $o{"$prefix$driver.non_mx_domains"})
+            {
+            $o{"$prefix$driver.mx_domains_except"} =
+              &pair("$prefix$driver.mx_domains_except",
+                    "$prefix$driver.non_mx_domains");
+
+            &amalgamate("$prefix$driver.mx_domains",
+                        "$prefix$driver.mx_domains_except", "",
+              "mx_domains", "+");
+            }
+          else
+            {
+            print STDOUT "$i1$origname$i2$origrest\n";
+            }
+          }
+
+        # This option has been renamed
+
+        elsif ($name eq "directory" && $driver_type eq "pipe")
+          {
+          $origname =~ s/directory/home_directory/;
+          print STDOUT "# >> Option rewritten by convert4r3\n";
+          print STDOUT "$i1$origname$i2$origrest\n";
+          printf STDERR "\n%03d directory renamed as " .
+            "home_directory in \"$driver\" director.\n",
+            ++$count;
+          }
+
+        # serialize_nets is abolished
+
+        elsif ($name eq "serialize_nets" ||
+               $name eq "serialize_hosts")
+          {
+          if (!$seen_serialize)
+            {
+            &amalgamate("$prefix$driver.serialize_nets", "",
+              "$prefix$driver.serialize_hosts", "serialize_hosts", "+");
+            $seen_serialize = 1;
+            }
+          else
+            {
+            $i = &skipblanks($i);
+            next;
+            }
+          }
+
+
+        # Option not of interest; reproduce verbatim
+
+        else
+          {
+          print STDOUT "$i1$origname$i2$origrest\n";
+          }
+
+
+        $last_was_blank = 0;
+        }
+      }
+    }
+
+  }
+
+# Debugging: show the associative array
+# foreach $key (sort keys %o) { print STDERR "$key = $o{$key}\n"; }
+
+print STDERR "\nEnd of configuration file conversion.\n";
+print STDERR "\n*******************************************************\n";
+print STDERR   "***** Please review the generated file carefully. *****\n";
+print STDERR   "*******************************************************\n\n";
+
+print STDERR "In particular:\n\n";
+
+print STDERR "(1) If you use regular expressions in any options that have\n";
+print STDERR "    been rewritten by this script, they might have been put\n";
+print STDERR "    inside quotes, when then were not previously quoted. This\n";
+print STDERR "    means that any backslashes in them must now be escaped.\n\n";
+
+print STDERR "(2) If your configuration refers to any external files that\n";
+print STDERR "    contain lists of network addresses, check that the masks\n";
+print STDERR "    are specified as single numbers, e.g. /24 and NOT as dotted\n";
+print STDERR "    quads (e.g. 255.255.255.0) because Exim release 3.00 does\n";
+print STDERR "    not recognize the dotted quad form.\n\n";
+
+print STDERR "(3) If your configuration uses macros for lists of domains or\n";
+print STDERR "    hosts or addresses, check to see if any of the references\n";
+print STDERR "    have been negated. If so, you will have to rework things,\n";
+print STDERR "    because the negation will apply only to the first item in\n";
+print STDERR "    the macro-generated list.\n\n";
+
+print STDERR "(4) If you do not generate deliveries to pipes, files, or\n";
+print STDERR "    auto-replies in your aliasfile and forwardfile directors,\n";
+print STDERR "    you can remove the added transport settings.\n\n";
+
+# End of convert4r3
diff --git a/src/convert4r4.src b/src/convert4r4.src
new file mode 100755 (executable)
index 0000000..4455fb7
--- /dev/null
@@ -0,0 +1,2513 @@
+#! PERL_COMMAND -w
+
+# This is a Perl script that reads an Exim run-time configuration file for
+# Exim 3. It makes what changes it can for Exim 4, and also output commentary
+# on what it has done, and on things it cannot do.
+
+# It is assumed that the input is a valid Exim 3 configuration file.
+
+
+# These are lists of main options which are abolished in Exim 4.
+# The first contains options that are used to construct new options.
+
+@skipped_options = (
+"auth_hosts",
+"auth_over_tls_hosts",
+"errors_address",
+"headers_check_syntax",
+"headers_checks_fail",
+"headers_sender_verify",
+"headers_sender_verify_errmsg",
+"host_accept_relay",
+"host_auth_accept_relay",
+"host_reject_recipients",
+"local_domains",
+"local_domains_include_host",
+"local_domains_include_host_literals",
+"log_all_parents",
+"log_arguments",
+"log_incoming_port",
+"log_interface",
+"log_level",
+"log_received_sender",
+"log_received_recipients",
+"log_rewrites",
+"log_sender_on_delivery",
+"log_smtp_confirmation",
+"log_smtp_connections",
+"log_smtp_syntax_errors",
+"log_subject",
+"log_queue_run_level",
+"rbl_domains",
+"rbl_hosts",
+"rbl_reject_recipients",
+"receiver_verify",
+"receiver_verify_addresses",
+"receiver_verify_hosts",
+"receiver_verify_senders",
+"recipients_reject_except",
+"recipients_reject_except_senders",
+"relay_domains",
+"relay_domains_include_local_mx",
+"sender_address_relay",
+"sender_address_relay_hosts",
+"sender_reject_recipients",
+"sender_verify",
+"sender_verify_hosts_callback",
+"sender_verify_callback_domains",
+"sender_verify_callback_timeout",
+"sender_verify_hosts",
+"smtp_etrn_hosts",
+"smtp_expn_hosts",
+"smtp_verify",
+"tls_host_accept_relay",
+"tls_hosts",
+"tls_log_cipher",
+"tls_log_peerdn",
+"tls_verify_ciphers"
+);
+
+# The second contains options that are completely abolished and have
+# no equivalent.
+
+@abolished_options = (
+"always_bcc",
+"debug_level",
+"helo_strict_syntax",
+"kill_ip_options",
+"log_ip_options",
+"log_refused_recipients",
+"message_size_limit_count_recipients",
+"rbl_log_headers",
+"rbl_log_rcpt_count",
+"receiver_try_verify",
+"refuse_ip_options",
+"relay_match_host_or_sender",
+"sender_try_verify",
+"sender_verify_batch",
+"sender_verify_fixup",
+"sender_verify_reject",
+"sender_verify_max_retry_rate",
+);
+
+# This is a list of options that are not otherwise handled, but which
+# contain domain or host lists that have to be processed so that any
+# regular expressions are marked "not for expansion".
+
+@list_options = (
+"dns_again_means_nonexist",
+"hold_domains",
+"hosts_treat_as_local",
+"percent_hack_domains",
+"queue_smtp_domains",
+"helo_accept_junk_hosts",
+"host_lookup",
+"ignore_fromline_hosts",
+"rfc1413_hosts",
+"sender_unqualified_hosts",
+"smtp_reserve_hosts",
+"tls_advertise_hosts",
+"tls_verify_hosts",
+);
+
+
+
+##################################################
+#          Output problem rubric once            #
+##################################################
+
+sub rubric {
+return if $rubric_output;
+$rubric_output = 1;
+print STDERR "\n" .
+"** The following comments describe problems that have been encountered\n" .
+"   while converting an Exim 3 runtime file for Exim 4. More detail can\n" .
+"   be found in the file doc/Exim4.upgrade.\n";
+}
+
+
+##################################################
+#             Analyse one line                   #
+##################################################
+
+sub checkline{
+my($line) = $_[0];
+
+return "comment" if $line =~ /^\s*(#|$)/;
+return "end"     if $line =~ /^\s*end\s*$/i;
+
+# Macros are recognized only in the first section of the file.
+
+return "macro" if $prefix eq "" && $line =~ /^\s*[A-Z]/;
+
+# In retry and rewrite sections, the type is always "other"
+
+return "other" if $prefix eq "=retry" || $prefix eq "=rewrite";
+
+# Pick out the name at the start and the rest of the line (into global
+# variables) and return whether the start of a driver or not.
+
+($hide,$name,$rest) = $line =~ /^\s*(hide\s+|)([a-z0-9_]+)\s*(.*?)\s*$/;
+
+# If $rest begins with a colon, this is a driver name
+
+return "driver" if $rest =~ /^:/;
+
+# If $rest begins with an = the value of the option is given explicitly;
+# remove the = from the start. Turn "yes"/"no" into "true"/"false".
+
+if ($rest =~ /^=/)
+  {
+  $rest =~ s/^=\s*//;
+  $rest = "true" if $rest eq "yes";
+  $rest = "false" if $rest eq "no";
+  }
+
+# Otherwise we have a boolean option. Set up a "true"/"false" value.
+
+else
+  {
+  if ($name =~ /^not?_/)     # Recognize "no_" or "not_"
+    {
+    $rest = "false";
+    $name =~ s/^not?_//;
+    }
+  else
+    {
+    $rest = "true";
+    }
+  }
+
+return "option";
+}
+
+
+
+##################################################
+#       Negate a list of things                  #
+##################################################
+
+# Can be tricky, because there may be comment lines in the list.
+# Also, lists may have different delimiters.
+
+sub negate {
+my($list) = $_[0];
+my($delim) = ":";
+my($leadin) = "";
+
+return $list if ! defined $list;
+
+($list) = $list =~ /^"?(.*?)"?\s*$/s;      # Remove surrounding quotes
+$list =~ s/\\\s*\n\s*//g;                  # Remove continuation markers
+
+if ($list =~ /^(\s*<(\S)\s*)(.*)/s)
+  {
+  $leadin = $1;
+  $delim = $2;
+  $list = $3;
+  }
+
+$list =~ s/^\s+//;
+$list =~ s/\Q$delim$delim/>%%%%</g;
+@split = split /\s*\Q$delim\E\s*/s, $list;
+
+foreach $item (@split)
+  {
+  $item =~ s/>%%%%</$delim$delim/g;
+
+  if ($item =~ /^\s*#/)
+    {
+    $item =~ s/((?:^\s*#[^\n]*\n)+\s*)/$1! /mg;
+    $item =~ s/!\s*!//sg;
+    }
+  else
+    {
+    if ($item =~ /^\s*!(.*)/)
+      { $item = $1; }
+    else
+      { $item = "! " . $item; }
+    }
+  }
+
+$" = " $delim \\\n    ";
+$leadin .= " " if $leadin !~ /(^|\s)$/;
+return "$leadin@split";
+}
+
+
+
+##################################################
+#   Prevent regex expansion in a list of things  #
+##################################################
+
+# Can be tricky, because there may be comment lines in the list.
+# Also, lists may have different delimiters.
+
+sub no_expand_regex {
+my($list) = $_[0];
+my($delim) = ":";
+my($leadin) = "";
+
+return $list if ! defined $list;
+
+$delim = $_[1] if (defined $_[1]);
+
+my($is_route_list) = $delim eq ";";
+
+($list) = $list =~ /^"?(.*?)"?\s*$/s;      # Remove surrounding quotes
+$list =~ s/\\\s*\n\s*//g;                  # Remove continuation markers
+
+if ($list =~ /^(\s*<(\S)\s*)(.*)/s)
+  {
+  $leadin = $1;
+  $delim = $2;
+  $list = $3;
+  }
+
+$list =~ s/^\s+//;
+$list =~ s/\Q$delim$delim/>%%%%</g;
+@split = split /\s*\Q$delim\E\s*/s, $list;
+
+my($changed) = 0;
+foreach $item (@split)
+  {
+  $item =~ s/>%%%%</$delim$delim/g;
+  if ($item =~ /^\^/)
+    {
+    # Fudge for route_list items
+
+    if ($is_route_list)
+      {
+      $item = "\\N$item";      # Only one item ...
+      }
+    else
+      {
+      $item = "\\N$item\\N";
+      }
+    $changed = 1;
+    }
+  }
+print STDOUT
+  "#!!# Regular expressions enclosed in \\N...\\N to avoid expansion\n"
+    if $changed;
+
+$" = " $delim \\\n    ";
+$leadin .= " " if $leadin !~ /(^|\s)$/;
+return "$leadin@split";
+}
+
+
+
+##################################################
+#      Sort out lookups in an address list       #
+##################################################
+
+# Can be tricky, because there may be comment lines in the list.
+# Also, lists may have different delimiters.
+
+sub sort_address_list {
+my($list) = $_[0];
+my($name) = $_[1];
+my($delim) = ":";
+my($leadin) = "";
+my($quoted) = 0;
+
+return $list if ! defined $list;
+
+if ($list =~ /^"(.*?)"\s*$/s)            # Remove surrounding quotes
+  {
+  $list = $1;
+  $quoted = 1;
+  }
+
+$list =~ s/\\\s*\n\s*//g;                  # Remove continuation markers
+
+if ($list =~ /^(\s*<(\S)\s*)(.*)/s)
+  {
+  $leadin = $1;
+  $delim = $2;
+  $list = $3;
+  }
+
+$list =~ s/^\s+//;
+$list =~ s/\Q$delim$delim/>%%%%</g;
+@split = split /\s*\Q$delim\E\s*/s, $list;
+
+foreach $item (@split)
+  {
+  $item =~ s/>%%%%</$delim$delim/g;
+  if ($item =~ /^\s*(?:partial-)?(\w+;.*)$/)
+    {
+    my($lookup) = $1;
+    if ($lookup =~ /^lsearch|^dbm|^cdb|^nis[^p]/)
+      {
+      &rubric();
+      print STDERR "\n" .
+"** The Exim 3 \"$name\" option specifies an address\n" .
+"   list that includes the item\n\n" .
+"     $item\n\n" .
+"   In Exim 4 address lists, single-key lookups without a local part just\n" .
+"   look up the complete address. They don't also try the domain, as\n" .
+"   happened in Exim 3. The item has been rewritten as two items to make\n" .
+"   it behave in the same way as Exim 3, but you should check to see if\n" .
+"   this is actually what you want.\n";
+
+      $item = "*\@$item $delim $lookup";
+      }
+    }
+  }
+
+$" = " $delim \\\n    ";
+$leadin .= " " if $leadin !~ /(^|\s)$/;
+
+return $quoted? "\"$leadin@split\"" : "$leadin@split";
+}
+
+
+
+##################################################
+#       Quote a string against expansion         #
+##################################################
+
+# Used for setting up new "domains" options
+
+sub expquote {
+my($s) = $_[0];
+$s =~ s/\$/\\\$/sg;
+$s =~ s/\\(?!\s*\n)/\\\\/sg;
+return $s;
+}
+
+
+
+##################################################
+#          Dequote an option string              #
+##################################################
+
+# If the original list is not quoted, do nothing.
+# If it is quoted, just get rid of the quotes.
+
+sub unquote {
+my($s) = $_[0];
+$s =~ s/^"(.*)"$/$1/s;
+return $s;
+}
+
+
+##################################################
+#      Quote/dequote an option string            #
+##################################################
+
+# If the original list is not quoted, quote it against expansion.
+# If it is quoted, just get rid of the quotes. Also, indent any
+# continuations.
+
+sub acl_quote {
+my($s) = $_[0];
+$s = ($s =~ /^"(.*)"$/s)? $1 : &expquote($s);
+$s =~ s/\n/\n  /g;
+$s =~ s/\n\s{11,}/\n           /g;
+return $s;
+}
+
+
+##################################################
+#       Handle abolished driver options          #
+##################################################
+
+sub abolished {
+my($hash) = shift @_;
+my($name) = shift @_;
+for $abolished (@_)
+  {
+  if (defined $$hash{$abolished})
+    {
+    &rubric();
+    print STDERR "\n" .
+"** $name used the \"$abolished\" option, which no\n".
+"   longer exists. The option has been removed.\n";
+    print STDOUT "#!!# $abolished option removed\n";
+    delete $$hash{$abolished};
+    }
+  }
+}
+
+
+
+##################################################
+#        Handle renamed driver options           #
+##################################################
+
+sub renamed {
+my($hash,$old,$new) = @_;
+if (defined $$hash{$old})
+  {
+  print STDOUT "#!!# $old renamed $new\n";
+  $$hash{$new} = $$hash{$old};
+  delete $$hash{$old};
+  }
+}
+
+
+
+##################################################
+#      Comment on user names in require_files    #
+##################################################
+
+sub check_require {
+my($string, $name) = @_;
+
+$string =~ s/::/[[[]]]/g;
+my(@list) = split /:/, $string;
+my($item);
+
+for $item (@list)
+  {
+  if ($item =~ /^\s*[\w,]+\s*$/)
+    {
+    &rubric();
+    $item =~ s/^\s*//;
+    $item =~ s/\s*$//;
+    print STDERR "\n" .
+"** A setting of require_files in the $name contains\n" .
+"   what appears to be a user name ('$item'). The ability to check files\n" .
+"   as a specific user is done differently in Exim 4. In fact, because the\n" .
+"   routers run as root, you may not need this at all.\n"
+    }
+  }
+}
+
+
+##################################################
+#        Handle current and home directory       #
+##################################################
+
+sub handle_current_and_home_directory {
+my($hash,$driver,$name) = @_;
+
+for ("current_directory", "home_directory")
+  {
+  if (defined $$hash{$_} && $$hash{$_} eq "check_local_user")
+    {
+    my($article) = (substr($driver, 0, 1) eq "a")? "an" : "a";
+    &rubric();
+    print STDERR "\n" .
+"** The Exim 3 configuration contains $article '$driver' director called\n" .
+"   '$name', which set '$_' to the special value\n" .
+"   'check_local_user'. This facility has been abolished in Exim 4 because\n" .
+"   it is no longer necessary. The setting has therefore been omitted. See\n" .
+"   note X.\n";
+    delete $$hash{$_};
+    }
+  else
+    {
+    &renamed($hash, $_, "transport_$_");
+    }
+  }
+}
+
+
+
+##################################################
+#    Handle batch/bsmtp for appendfile/pipe      #
+##################################################
+
+sub handle_batch_and_bsmtp{
+my($hash) = @_;
+
+if (defined $$hash{"bsmtp"})
+  {
+  if ($$hash{"bsmtp"} ne "none")
+    {
+    $$hash{"use_bsmtp"} = "true";
+    $$hash{"message_prefix"} = "\"HELO \$primary_host_name\\n\""
+      if defined $$hash{"bsmtp_helo"} && $$hash{"bsmtp_helo"} eq "true";
+    }
+
+  if ($$hash{"bsmtp"} eq "one")
+    {
+    delete $$hash{"batch"};
+    }
+  else
+    {
+    $$hash{"batch"} = $$hash{"bsmtp"};
+    }
+
+  delete $$hash{"bsmtp"};
+  delete $$hash{"bsmtp_helo"};
+  }
+
+if (defined $$hash{"batch"} && $$hash{"batch"} ne "none")
+  {
+  $$hash{"batch_max"} = "100" if !defined $$hash{"batch_max"};
+  $$hash{"batch_id"} = "\$domain" if $$hash{"batch"} eq "domain";
+  }
+else
+  {
+  $$hash{"batch_max"} = "1" if defined $$hash{"batch_max"};
+  }
+delete $$hash{"batch"};
+}
+
+
+
+##################################################
+#              Output one option                 #
+##################################################
+
+sub outopt {
+my($hash, $key, $no_expand) = @_;
+my($data) = $$hash{$key};
+
+print STDOUT "hide " if defined $$hash{"$key-hide"};
+
+# Output booleans in the form that doesn't use "="
+
+if ($data eq "true")
+  {
+  print STDOUT "$key\n";
+  }
+elsif ($data eq "false")
+  {
+  print STDOUT "no_$key\n";
+  }
+else
+  {
+  if ($no_expand)
+    {
+    printf STDOUT ("$key = %s\n", &no_expand_regex($data));
+    }
+  else
+    {
+    print STDOUT "$key = $data\n";
+    }
+  }
+}
+
+
+
+##################################################
+#       Output the options for one driver        #
+##################################################
+
+# Put the "driver" option first
+
+sub outdriver {
+my($hash) = $_[0];
+print STDOUT "  driver = $$hash{'driver'}\n";
+foreach $key (sort keys %$hash)
+  {
+  next if $key eq "driver" || $key =~ /-hide$/;
+  print STDOUT "  ";
+  &outopt($hash, $key, 0);
+  }
+}
+
+
+
+##################################################
+#      Output a rewrite or a retry line          #
+##################################################
+
+# These lines start with patterns which are now always expanded. If the
+# pattern is a regex, arrange for it not to expand.
+
+sub print_no_expand {
+my($s) = $_[0];
+if ($s =~ /^\^/)
+  {
+  if (!$escape_output)
+    {
+    &rubric();
+    print STDERR "\n" .
+"** You have a retry or rewrite pattern that is a regular expression. Because\n" .
+"   these patterns are now always expanded, you need to be sure that the\n" .
+"   special characters in the regex are not interpreted by the expander.\n" .
+"   \\N has been inserted at the start of the regex to prevent the rest of\n" .
+"   it from being expanded.\n";
+    $escape_output = 1;
+    }
+  print STDOUT "\\N";
+  }
+print STDOUT "$s\n";
+}
+
+
+
+##################################################
+#          Test a boolean main option            #
+##################################################
+
+# This just saves a lot of typing
+
+sub bool {
+return defined $main{$_[0]} && $main{$_[0]} eq "true";
+}
+
+
+
+##################################################
+#                  Main program                  #
+##################################################
+
+print STDERR "Runtime configuration file converter for Exim release 4.\n";
+
+$transport_start = $director_start = $router_start = $retry_start
+  = $rewrite_start = $auth_start = 999999;
+
+$macro_output = "";
+$rubric_output = 0;
+$errmsg_output = 0;
+$key_output = 0;
+$unk_output = 0;
+$escape_output = 0;
+$add_no_more = 0;
+$add_caseful_local_part = 0;
+$done_dns_check_names = 0;
+
+$queue_only_load_was_present = 0;
+$deliver_queue_load_max_was_present = 0;
+
+# Read the entire file into an array
+
+chomp(@c = <STDIN>);
+$clen = scalar @c;
+
+# Remove the standard comment that appears at the end of the default
+
+if ($clen > 0 && $c[$clen-1] =~ /^#\s*End of Exim configuration file\s*/i)
+  {
+  pop @c;
+  $clen--;
+  }
+
+# The first pass over the input fishes out all the options settings in the
+# main, transport, director, and router sections, and places their values in
+# associative arrays. It also notes the starting position of all the sections.
+
+$prefix = "";
+%main = ();
+$hash = \%main;
+
+for ($i = 0; $i < $clen; $i++)
+  {
+  # Change references to +allow_unknown and +warn_unknown into +include_unknown
+
+  if ($c[$i] =~ /\+(?:allow|warn)_unknown/)
+    {
+    if (!$unk_output)
+      {
+      &rubric();
+      print STDERR "\n" .
+"** You have used '+allow_unknown' or '+warn_unknown' in a configuration\n" .
+"   option. This has been converted to '+include_unknown', but the action\n" .
+"   is different in Exim 4, so you should review all the relevant options.\n";
+      $unk_output = 1;
+      }
+    $c[$i] =~ s/\+(?:allow|warn)_unknown/+include_unknown/g;
+    }
+
+  # Any reference to $errmsg_recipient is changed to $bounce_recipient
+
+  if ($c[$i] =~ /\$errmsg_recipient/)
+    {
+    if (!$errmsg_output)
+      {
+      &rubric();
+      print STDERR "\n" .
+"** References to \$errmsg_recipient have been changed to \$bounce_recipient\n";
+      $errmsg_output = 1;
+      }
+    $c[$i] =~ s/\$errmsg_recipient/\$bounce_recipient/g;
+    }
+
+
+  # Analyse the type of line
+
+  $type = &checkline($c[$i]);
+  next if $type eq "comment";
+
+  # Output a warning if $key is used
+
+  if ($c[$i] =~ /\$key/ && !$key_output)
+    {
+    &rubric();
+    print STDERR "\n" .
+"** You have used '\$key' in a configuration option. This variable does not\n" .
+"   exist in Exim 4. Instead, the value you need for your lookup will be\n" .
+"   in one of the other variables such as '\$domain' or '\$host'. You will\n" .
+"   need to edit the new configuration to sort this out.\n";
+    $key_output = 1;
+    }
+
+  # Save macro definitions so we can output them first; must handle
+  # continuations.
+
+  if ($type eq "macro")
+    {
+    $macro_output .= "$c[$i++]\n" while $c[$i] =~ /\\\s*$|^\s*#/;
+    $macro_output .= "$c[$i]\n";
+    }
+
+  # Handle end of section
+
+  elsif ($type eq "end")
+    {
+    if ($prefix eq "=rewrite")
+      {
+      $prefix = "a.";
+      $auth_start = $i + 1;
+      last;
+      }
+    elsif ($prefix eq "=retry")
+      {
+      $prefix = "=rewrite";
+      $rewrite_start = $i + 1;
+      }
+    elsif ($prefix eq "r.")
+      {
+      $prefix = "=retry";
+      $retry_start = $i + 1;
+      }
+    elsif ($prefix eq "d.")
+      {
+      $prefix = "r.";
+      $router_start = $i + 1;
+      }
+    elsif ($prefix eq "t.")
+      {
+      $prefix = "d.";
+      $director_start = $i + 1;
+      }
+    elsif ($prefix eq "")
+      {
+      $prefix = "t.";
+      $transport_start = $i + 1;
+      }
+    }
+
+  # Handle start of a new director, router or transport driver
+
+  elsif ($type eq "driver" && $prefix !~ /^=/)
+    {
+    $hash = {};
+    if (defined $driverlist{"$prefix$name"})
+      {
+      die "*** There are two drivers with the name \"$name\"\n";
+      }
+    $driverlist{"$prefix$name"} = $hash;
+    $first_director = $name if !defined $first_director && $prefix eq "d.";
+    }
+
+  # Handle definition of an option; we must pull in any continuation
+  # strings, and save the value in the current hash. Note if the option
+  # is hidden.
+
+  elsif ($type eq "option")
+    {
+    my($nextline) = "";
+
+    while ($i < $clen - 1 && ($rest =~ /\\\s*$/s || $nextline =~ /^\s*#/))
+      {
+      $nextline = $c[++$i];
+      $rest .= "\n$nextline";
+      }
+
+    $$hash{$name} = $rest;
+    $$hash{"$name-hide"} = 1 if $hide ne "";
+    }
+  }
+
+
+# Generate the new configuration. Start with a warning rubric.
+
+print STDOUT "#!!# This file is output from the convert4r4 script, which tries\n";
+print STDOUT "#!!# to convert Exim 3 configurations into Exim 4 configurations.\n";
+print STDOUT "#!!# However, it is not perfect, especially with non-simple\n";
+print STDOUT "#!!# configurations. You must check it before running it.\n";
+print STDOUT "\n\n";
+
+# Output the macro definitions
+
+if ($macro_output ne "")
+  {
+  print STDOUT "#!!# All macro definitions have been gathered here to ensure\n";
+  print STDOUT "#!!# they precede any references to them.\n\n";
+  print STDOUT "$macro_output\n";
+  }
+
+# Output some default pointers to ACLs for RCPT and DATA time. If no Exim 3
+# options that apply are set, non-restricting ACLs are generated.
+
+print STDOUT "#!!# These options specify the Access Control Lists (ACLs) that\n";
+print STDOUT "#!!# are used for incoming SMTP messages - after the RCPT and DATA\n";
+print STDOUT "#!!# commands, respectively.\n\n";
+
+print STDOUT "acl_smtp_rcpt = check_recipient\n";
+print STDOUT "acl_smtp_data = check_message\n\n";
+
+if (defined $main{"auth_over_tls_hosts"})
+  {
+  print STDOUT "#!!# This option specifies the Access Control List (ACL) that\n";
+  print STDOUT "#!!# is used after an AUTH command.\n\n";
+  print STDOUT "acl_smtp_auth = check_auth\n\n";
+  }
+
+if (&bool("smtp_verify") ||
+    defined $main{"smtp_etrn_hosts"} ||
+    defined $main{"smtp_expn_hosts"})
+  {
+  print STDOUT "#!!# These options specify the Access Control Lists (ACLs) that\n";
+  print STDOUT "#!!# are used to control the ETRN, EXPN, and VRFY commands.\n";
+  print STDOUT "#!!# Where no ACL is defined, the command is locked out.\n\n";
+
+  print STDOUT "acl_smtp_etrn = check_etrn\n" if defined $main{"smtp_etrn_hosts"};
+  print STDOUT "acl_smtp_expn = check_expn\n" if defined $main{"smtp_expn_hosts"};
+  print STDOUT "acl_smtp_vrfy = check_vrfy\n" if &bool("smtp_verify");
+  print STDOUT "\n";
+  }
+
+# If local_domains was set, get its value; otherwise set to "@". Add into it
+# appropriate magic for local_domains_include_host[_literals].
+
+$local_domains = (defined $main{"local_domains"})? $main{"local_domains"} : "@";
+
+$ldsep = ":";
+if ($local_domains =~ /^\s*<(.)\s*(.*)/s)
+  {
+  $ldsep = $1;
+  $local_domains = $2;
+  }
+
+$local_domains = "\@[] $ldsep " . $local_domains
+  if defined $main{"local_domains_include_host_literals"} &&
+     $main{"local_domains_include_host_literals"} eq "true";
+
+$local_domains = "\@ $ldsep " . $local_domains
+  if defined $main{"local_domains_include_host"} &&
+     $main{"local_domains_include_host"} eq "true";
+
+$local_domains = "<$ldsep " . $local_domains if $ldsep ne ":";
+
+# Output a domain list setting for these domains, provided something is defined
+
+if ($local_domains !~ /^\s*$/)
+  {
+  print STDOUT "#!!# This setting defines a named domain list called\n";
+  print STDOUT "#!!# local_domains, created from the old options that\n";
+  print STDOUT "#!!# referred to local domains. It will be referenced\n";
+  print STDOUT "#!!# later on by the syntax \"+local_domains\".\n";
+  print STDOUT "#!!# Other domain and host lists may follow.\n\n";
+
+  printf STDOUT ("domainlist local_domains = %s\n\n",
+    &no_expand_regex($local_domains));
+  }
+
+$relay_domains = (defined $main{"relay_domains"})? $main{"relay_domains"} : "";
+
+$ldsep = ":";
+if ($relay_domains =~ /^\s*<(.)\s*(.*)/s)
+  {
+  $ldsep = $1;
+  }
+
+if (defined $main{"relay_domains_include_local_mx"})
+  {
+  $relay_domains .= ($relay_domains =~ /^\s*$/)? "\@mx_any" :
+    " $ldsep \@mx_any";
+  }
+
+printf STDOUT ("domainlist relay_domains = %s\n",
+  &no_expand_regex($relay_domains))
+    if $relay_domains !~ /^\s*$/;
+
+
+# If ignore_errmsg_errors is set, we are going to force 0s as the value
+# for ignore_errmsg_errors_after, so arrange to skip any other value.
+
+push @skipped_options, "ignore_errmsg_errors_after"
+   if &bool("ignore_errmsg_errors");
+
+
+# If rbl_domains is set, split it up and generate six lists:
+#   rbl_warn_domains, rbl_warn_domains_skiprelay
+#   rbl_reject_domains, rbl_reject_domains_skiprelay
+#   rbl_accept_domains, rbl_accept_domains_skiprelay
+
+if (defined $main{"rbl_domains"})
+  {
+  my($s) = &unquote($main{"rbl_domains"});
+  $s =~ s/\s*\\\s*\n\s*/ /g;
+  my(@list) = split /\s*:\s*/, $s;
+
+  foreach $d (@list)
+    {
+    my(@sublist) = split /\//, $d;
+    my($name) = shift @sublist;
+    my($warn) = 0;
+    if (defined $main{"rbl_reject_recipients"})
+      {
+      $warn = $main{"rbl_reject_recipients"} ne "true";
+      }
+
+    foreach $o (@sublist)
+      {
+      $warn = 1 if $o eq "warn";
+      $warn = 0 if $o eq "reject";
+      $warn = 2 if $o eq "accept";
+      $skiprelay = 1 if $o eq "skiprelay";
+      }
+
+    if ($skiprelay)
+      {
+      if ($warn == 0)
+        {
+        $rbl_reject_skiprelay .= ((defined $rbl_reject_skiprelay)? ":":"").$name;
+        }
+      elsif ($warn == 1)
+        {
+        $rbl_warn_skiprelay .= ((defined $rbl_warn_skiprelay)? ":":"").$name;
+        }
+      elsif ($warn == 2)
+        {
+        $rbl_accept_skiprelay .= ((defined $rbl_accept_skiprelay)? ":":"").$name;
+        }
+      }
+    else
+      {
+      if ($warn == 0)
+        {
+        $rbl_reject_domains .= ((defined $rbl_reject_domains)? ":":"").$name;
+        }
+      elsif ($warn == 1)
+        {
+        $rbl_warn_domains .= ((defined $rbl_warn_domains)? ":":"").$name;
+        }
+      elsif ($warn == 2)
+        {
+        $rbl_accept_domains .= ((defined $rbl_accept_domains)? ":":"").$name;
+        }
+      }
+    }
+  }
+
+
+# Output host list settings
+
+printf STDOUT ("hostlist auth_hosts = %s\n",
+  &no_expand_regex($main{"auth_hosts"}))
+    if defined $main{"auth_hosts"};
+printf STDOUT ("hostlist rbl_hosts = %s\n",
+  &no_expand_regex($main{"rbl_hosts"}))
+    if defined $main{"rbl_hosts"};
+printf STDOUT ("hostlist relay_hosts = %s\n",
+  &no_expand_regex($main{"host_accept_relay"}))
+    if defined $main{"host_accept_relay"};
+printf STDOUT ("hostlist auth_relay_hosts = %s\n",
+  &no_expand_regex($main{"host_auth_accept_relay"}))
+    if defined $main{"host_auth_accept_relay"};
+
+printf STDOUT ("hostlist auth_over_tls_hosts = %s\n",
+  &no_expand_regex($main{"auth_over_tls_hosts"}))
+    if defined $main{"auth_over_tls_hosts"};
+printf STDOUT ("hostlist tls_hosts = %s\n",
+  &no_expand_regex($main{"tls_hosts"}))
+    if defined $main{"tls_hosts"};
+printf STDOUT ("hostlist tls_relay_hosts = %s\n",
+  &no_expand_regex($main{"tls_host_accept_relay"}))
+    if defined $main{"tls_host_accept_relay"};
+
+print STDOUT "\n";
+
+
+# Convert various logging options
+
+$log_selector = "";
+$sep = " \\\n             ";
+
+if (defined $main{"log_level"})
+  {
+  my($level) = $main{"log_level"};
+  $log_selector .= "$sep -retry_defer$sep -skip_delivery" if $level < 5;
+  $log_selector .= "$sep -lost_incoming_connection$sep -smtp_syntax_error" .
+                   "$sep -delay_delivery" if $level < 4;
+  $log_selector .= "$sep -size_reject" if $level < 2;
+  }
+
+$log_selector .= "$sep -queue_run"
+  if defined $main{"log_queue_run_level"} &&
+     defined $main{"log_level"} &&
+     $main{"log_queue_run_level"} > $main{"log_level"};
+
+$log_selector .= "$sep +address_rewrite"       if &bool("log_rewrites");
+$log_selector .= "$sep +all_parents"           if &bool("log_all_parents");
+$log_selector .= "$sep +arguments"             if &bool("log_arguments");
+$log_selector .= "$sep +incoming_port"         if &bool("log_incoming_port");
+$log_selector .= "$sep +incoming_interface"    if &bool("log_interface");
+$log_selector .= "$sep +received_sender"       if &bool("log_received_sender");
+$log_selector .= "$sep +received_recipients"   if &bool("log_received_recipients");
+$log_selector .= "$sep +sender_on_delivery"    if &bool("log_sender_on_delivery");
+$log_selector .= "$sep +smtp_confirmation"     if &bool("log_smtp_confirmation");
+$log_selector .= "$sep +smtp_connection"       if &bool("log_smtp_connections");
+$log_selector .= "$sep +smtp_syntax_error"     if &bool("log_smtp_syntax_errors");
+$log_selector .= "$sep +subject"               if &bool("log_subject");
+$log_selector .= "$sep +tls_cipher"            if &bool("tls_log_cipher");
+$log_selector .= "$sep +tls_peerdn"            if &bool("tls_log_peerdn");
+
+
+if ($log_selector ne "")
+  {
+  print STDOUT "#!!# All previous logging options are combined into a single\n"
+             . "#!!# option in Exim 4. This setting is an approximation to\n"
+             . "#!!# the previous state - some logging has changed.\n\n";
+  print STDOUT "log_selector = $log_selector\n\n";
+  }
+
+# If deliver_load_max is set, replace it with queue_only_load (taking the
+# lower value if both set) and also set deliver_queue_load_max if it is
+# not already set. When scanning for output, deliver_load_max is skipped.
+
+if (defined $main{"deliver_load_max"})
+  {
+  &rubric();
+  print STDERR "\n" .
+"** deliver_load_max is abolished in Exim 4.\n";
+
+  if (defined $main{"queue_only_load"})
+    {
+    $queue_only_load_was_present = 1;
+    if ($main{"queue_only_load"} < $main{"deliver_load_max"})
+      {
+      print STDERR
+"   As queue_only_load was set lower, deliver_load_max is just removed.\n";
+      }
+    else
+      {
+      print STDERR
+"   As queue_only_load was set higher, it's value has been replaced by\n" .
+"   the value of deliver_load_max.\n";
+      $main{"queue_only_load"} = $main{"deliver_load_max"};
+      }
+    }
+  else
+    {
+    print STDERR
+"   queue_only_load has been set to the load value.\n";
+    $main{"queue_only_load"} = $main{"deliver_load_max"};
+    }
+
+  if (!defined $main{"deliver_queue_load_max"})
+    {
+    print STDERR
+"   deliver_queue_load_max has been set to the value of queue_only_load.\n";
+    $main{"deliver_queue_load_max"} = $main{"queue_only_load"};
+    }
+  else
+    {
+    $deliver_queue_load_max_was_present = 1;
+    }
+  }
+
+
+# Now we scan through the various parts of the file again, making changes
+# as necessary.
+
+# -------- The main configuration --------
+
+$prefix = "";
+MainLine: for ($i = 0; $i < $clen; $i++)
+  {
+  my($nextline) = "";
+  $type = &checkline($c[$i]);
+  last if $type eq "end";
+
+  if ($type eq "macro")
+    {
+    $i++ while $c[$i] =~ /\\\s*$|^\s*#/;
+    next;
+    }
+
+  if ($type eq "comment") { print STDOUT "$c[$i]\n"; next; }
+
+  # Collect any continuation lines for an option setting
+
+  while ($rest =~ /\\\s*$/s || $nextline =~ /^\s*#/)
+    {
+    $nextline = $c[++$i];
+    $rest .= "\n$nextline";
+    }
+
+  $rest =~ s/^=\s*//;
+
+  # Deal with main options that are skipped (they are used in other
+  # options in other places).
+
+  for $skipped (@skipped_options)
+    {
+    next MainLine if $name eq $skipped;
+    }
+
+  # Deal with main options that are totally abolished
+
+  for $abolished (@abolished_options)
+    {
+    if ($name eq $abolished)
+      {
+      &rubric();
+      print STDERR "\n" .
+"** The $name option no longer exists, and has no equivalent\n" .
+"   in Exim 4.\n";
+      next MainLine;
+      }
+    }
+
+  # There is a special case for rbl_warn_header
+
+  if ($name eq "rbl_warn_header")
+    {
+    &rubric();
+    print STDERR "\n" .
+"** The $name option no longer exists. In Exim 4 you can achieve the\n" .
+"   effect by adding a suitable \"message\" statement in the ACL.\n";
+    }
+
+  # There is a special case for sender_reject and host_reject
+
+  elsif ($name eq "sender_reject" || $name eq "host_reject")
+    {
+    &rubric();
+    print STDERR "\n" .
+"** The $name option no longer exists. Its data has been used in\n" .
+"   an Access Control List as if it were in ${name}_recipients.\n";
+    }
+
+  # And a special message for prohibition_message
+
+  elsif ($name eq "prohibition_message")
+    {
+    &rubric();
+    print STDERR "\n" .
+"** The prohibition_message option no longer exists. The facility is\n" .
+"   provided in a different way in Exim 4, via the \"message\" keyword\n" .
+"   in Access Control Lists. It isn't possible to do an automatic conversion,\n" .
+"   so the value of prohibition_message has been ignored. You will have to\n" .
+"   modify the ACLs if you want to reinstate the feature.\n";
+    }
+
+  # auth_always_advertise gets converted to auth_advertise_hosts
+
+  elsif ($name eq "auth_always_advertise")
+    {
+    print STDOUT "#!!# auth_always_advertise converted to auth_advertise_hosts\n";
+    if (&bool("auth_always_advertise"))
+      {
+      print STDOUT "auth_advertise_hosts = *\n";
+      }
+    else
+      {
+      $sep = "";
+      print STDOUT "auth_advertise_hosts =";
+      if (defined $main{"auth_hosts"})
+        {
+        print STDOUT "$sep +auth_hosts";
+        $sep = " :";
+        }
+      if (defined $main{"host_accept_relay"})
+        {
+        print STDOUT "$sep !+relay_hosts";
+        $sep = " :";
+        }
+      if (defined $main{"host_auth_accept_relay"})
+        {
+        print STDOUT "$sep +auth_relay_hosts";
+        }
+      print STDOUT "\n";
+      }
+    }
+
+  # Deal with main options that have to be rewritten
+
+  elsif ($name eq "accept_timeout")
+    {
+    print STDOUT "#!!# accept_timeout renamed receive_timeout\n";
+    print STDOUT "receive_timeout = $rest\n";
+    }
+
+  elsif ($name eq "collapse_source_routes")
+    {
+    print STDOUT "#!!# collapse_source_routes removed\n";
+    print STDOUT "#!!# It has been a no-op since 3.10.\n";
+    }
+
+  elsif ($name eq "daemon_smtp_service")
+    {
+    print STDOUT "#!!# daemon_smtp_service renamed daemon_smtp_port\n";
+    print STDOUT "daemon_smtp_port = $rest\n";
+    }
+
+  elsif ($name eq "dns_check_names" || $name eq "dns_check_names_pattern")
+    {
+    if (!$done_dns_check_names)
+      {
+      if (&bool("dns_check_names"))
+        {
+        if (defined $main{"dns_check_names_pattern"})
+          {
+          &outopt(\%main, "dns_check_names_pattern", 0);
+          }
+        }
+
+      else
+        {
+        print STDOUT "#!!# dns_check_names has been abolished\n";
+        print STDOUT "#!!# setting dns_check_pattern empty to turn off check\n";
+        print STDOUT "dns_check_names_pattern =\n";
+        }
+
+      $done_dns_check_names = 1;
+      }
+    }
+
+  elsif ($name eq "deliver_load_max")
+    {
+    print STDOUT "deliver_queue_load_max = $main{'deliver_queue_load_max'}\n"
+      if !$deliver_queue_load_max_was_present;
+    print STDOUT "queue_only_load = $main{'queue_only_load'}\n"
+      if !$queue_only_load_was_present;
+    }
+
+  elsif ($name eq "errmsg_file")
+    {
+    print STDOUT "#!!# errmsg_file renamed bounce_message_file\n";
+    print STDOUT "bounce_message_file = $rest\n";
+    }
+
+  elsif ($name eq "errmsg_text")
+    {
+    print STDOUT "#!!# errmsg_text renamed bounce_message_text\n";
+    print STDOUT "bounce_message_text = $rest\n";
+    }
+
+  elsif ($name eq "forbid_domain_literals")
+    {
+    print STDOUT "#!!# forbid_domain_literals replaced by allow_domain_literals\n";
+    print STDOUT "allow_domain_literals = ",
+          &bool("forbid_domain_literals")? "false" : "true", "\n";
+    }
+
+  elsif ($name eq "freeze_tell_mailmaster")
+    {
+    print STDOUT "#!!# freeze_tell_mailmaster replaced by freeze_tell\n";
+    if (&bool("freeze_tell_mailmaster"))
+      {
+      print STDOUT "freeze_tell = ",
+        ((defined $main{"errors_address"})?
+          $main{"errors_address"} : "postmaster"), "\n";
+      }
+    else
+      {
+      print STDOUT "#!!# freeze_tell is unset by default\n";
+      }
+    }
+
+  elsif ($name eq "helo_verify")
+    {
+    print STDOUT "#!!# helo_verify renamed helo_verify_hosts\n";
+    printf STDOUT ("helo_verify_hosts = %s\n", &no_expand_regex($rest));
+    }
+
+  elsif ($name eq "ignore_errmsg_errors")
+    {
+    print STDOUT "ignore_bounce_errors_after = 0s\n";
+    }
+
+  elsif ($name eq "ignore_errmsg_errors_after")
+    {
+    print STDOUT "#!!# ignore_errmsg_errors_after renamed ignore_bounce_errors_after\n";
+    print STDOUT "ignore_bounce_errors_after = $rest\n";
+    }
+
+  elsif ($name eq "ipv4_address_lookup" || $name eq "dns_ipv4_lookup")
+    {
+    print STDOUT "#!!# $name changed to dns_ipv4_lookup\n"
+      if $name eq "ipv4_address_lookup";
+    print STDOUT "#!!# dns_ipv4_lookup is now a domain list\n";
+    if (&bool($name))
+      {
+      print STDOUT "dns_ipv4_lookup = *\n";
+      }
+    else
+      {
+      print STDOUT "#!!# default for dns_ipv4_lookup is unset\n";
+      }
+    }
+
+  elsif ($name eq "locally_caseless")
+    {
+    print STDOUT "#!!# locally_caseless removed\n";
+    print STDOUT "#!!# caseful_local_part will be added to ex-directors\n";
+    $add_caseful_local_part = 1;
+    }
+
+  elsif ($name eq "message_filter_directory2_transport")
+    {
+    print STDOUT "#!!# message_filter_directory2_transport removed\n";
+    }
+
+  elsif ($name =~ /^message_filter(.*)/)
+    {
+    print STDOUT "#!!# $name renamed system_filter$1\n";
+    print STDOUT "system_filter$1 = $rest\n";
+    }
+
+  elsif ($name eq "queue_remote_domains")
+    {
+    print STDOUT "#!!# queue_remote_domains renamed queue_domains\n";
+    printf STDOUT ("queue_domains = %s\n", &no_expand_regex($rest));
+    }
+
+  elsif ($name eq "receiver_unqualified_hosts")
+    {
+    print STDOUT "#!!# receiver_unqualified_hosts renamed recipient_unqualified_hosts\n";
+    printf STDOUT ("recipient_unqualified_hosts = %s\n",
+      &no_expand_regex($rest));
+    }
+
+  elsif ($name eq "remote_sort")
+    {
+    print STDOUT "#!!# remote_sort renamed remote_sort_domains\n";
+    printf STDOUT ("remote_sort_domains = %s\n", &no_expand_regex($rest));
+    }
+
+  elsif ($name eq "security")
+    {
+    if ($rest eq "unprivileged")
+      {
+      print STDOUT "#!!# security=unprivileged changed to deliver_drop_privilege\n";
+      print STDOUT "deliver_drop_privilege\n";
+      }
+    else
+      {
+      &rubric();
+      print STDERR "\n" .
+"** The 'security' option no longer exists.\n";
+      }
+    }
+
+  elsif ($name eq "timestamps_utc")
+    {
+    print STDOUT "#!!# timestamps_utc changed to use timezone\n";
+    print STDOUT "timezone = utc\n";
+    }
+
+  elsif ($name eq "untrusted_set_sender")
+    {
+    print STDOUT "#!!# untrusted_set_sender is now a list of what can be set\n";
+    print STDOUT "#!!# The default is an empty list.\n";
+    if (&bool("untrusted_set_sender"))
+      {
+      print STDOUT "untrusted_set_sender = *\n";
+      }
+    }
+
+  elsif ($name eq "warnmsg_file")
+    {
+    print STDOUT "#!!# warnmsg_file renamed warn_message_file\n";
+    print STDOUT "warn_message_file = $rest\n";
+    }
+
+  # Remaining options just get copied unless they are one of those that's
+  # a list where any regular expressions have to be escaped.
+
+  else
+    {
+    my($no_expand) = 0;
+    foreach $o (@list_options)
+      {
+      if ($name eq $o)
+        {
+        $no_expand = 1;
+        last;
+        }
+      }
+    &outopt(\%main, $name, $no_expand);
+    }
+  }
+
+
+# -------- The ACL configuration --------
+
+print STDOUT "\n";
+print STDOUT "#!!#######################################################!!#\n";
+print STDOUT "#!!# This new section of the configuration contains ACLs #!!#\n";
+print STDOUT "#!!# (Access Control Lists) derived from the Exim 3      #!!#\n";
+print STDOUT "#!!# policy control options.                             #!!#\n";
+print STDOUT "#!!#######################################################!!#\n";
+
+print STDOUT "\n";
+print STDOUT "#!!# These ACLs are crudely constructed from Exim 3 options.\n";
+print STDOUT "#!!# They are almost certainly not optimal. You should study\n";
+print STDOUT "#!!# them and rewrite as necessary.\n";
+
+print STDOUT "\nbegin acl\n\n";
+
+
+# Output an ACL for use after the RCPT command. This combines all the previous
+# policy checking options.
+
+print STDOUT "#!!# ACL that is used after the RCPT command\n";
+print STDOUT "check_recipient:\n";
+
+print STDOUT "  # Exim 3 had no checking on -bs messages, so for compatibility\n";
+print STDOUT "  # we accept if the source is local SMTP (i.e. not over TCP/IP).\n";
+print STDOUT "  # We do this by testing for an empty sending host field.\n";
+print STDOUT "  accept  hosts = :\n";
+
+if (defined $main{"tls_verify_ciphers"})
+  {
+  print STDOUT "  deny    ";
+  print STDOUT "hosts = $main{'tls_verify_hosts'}\n         "
+    if defined $main{"tls_verify_hosts"};
+  print STDOUT " encrypted = *\n         ";
+  print STDOUT "!encrypted = $main{'tls_verify_ciphers'}\n";
+  }
+
+print STDOUT "  deny    hosts = +auth_hosts\n" .
+             "          message = authentication required\n" .
+             "         !authenticated = *\n"
+  if defined $main{"auth_hosts"};
+
+print STDOUT "  deny    hosts = +tls_hosts\n" .
+             "          message = encryption required\n" .
+             "         !encrypted = *\n"
+  if defined $main{"tls_hosts"};
+
+printf STDOUT ("  accept  recipients = %s\n",
+  &acl_quote(&sort_address_list($main{"recipients_reject_except"},
+    "recipients_reject_except")))
+      if defined $main{"recipients_reject_except"};
+
+printf STDOUT ("  accept  senders = %s\n",
+  &acl_quote(&sort_address_list($main{"recipients_reject_except_senders"},
+    "recipients_reject_except_senders")))
+      if defined $main{"recipients_reject_except_senders"};
+
+printf STDOUT ("  deny    hosts = %s\n", &acl_quote($main{"host_reject"}))
+  if defined $main{"host_reject"};
+
+printf STDOUT ("  deny    hosts = %s\n",
+  &acl_quote($main{"host_reject_recipients"}))
+    if defined $main{"host_reject_recipients"};
+
+if (defined $main{"rbl_domains"})
+  {
+  my($msg) = "message = host is listed in \$dnslist_domain\n          ";
+  my($hlist) = (defined $main{"rbl_hosts"})?
+    "hosts = +rbl_hosts\n          " : "";
+
+  print STDOUT "  accept  ${hlist}dnslists = $rbl_accept_domains\n"
+    if defined $rbl_accept_domains;
+  print STDOUT "  deny    ${hlist}${msg}dnslists = $rbl_reject_domains\n"
+    if defined $rbl_reject_domains;
+  print STDOUT "  warn    ${hlist}" .
+    "message = X-Warning: \$sender_host_address is listed at \$dnslist_domain\n" .
+    "          dnslists = $rbl_warn_domains\n"
+      if defined $rbl_warn_domains;
+
+  if (defined $main{"host_accept_relay"})
+    {
+    $hlist .= "hosts = !+relay_hosts\n          ";
+    print STDOUT "  accept  ${hlist}dnslists = $rbl_accept_skiprelay\n"
+      if defined $rbl_accept_skiprelay;
+    print STDOUT "  deny    ${hlist}${msg}dnslists = $rbl_reject_skiprelay\n"
+      if defined $rbl_reject_skiprelay;
+    print STDOUT "  warn    ${hlist}" .
+      "message = X-Warning: \$sender_host_address is listed at \$dnslist_domain\n" .
+      "          dnslists = $rbl_warn_skiprelay\n"
+        if defined $rbl_warn_skiprelay;
+    }
+  }
+
+printf STDOUT ("  deny    senders = %s\n",
+  &acl_quote(&sort_address_list($main{"sender_reject"}, "sender_reject")))
+    if defined $main{"sender_reject"};
+
+printf STDOUT ("  deny    senders = %s\n",
+  &acl_quote(&sort_address_list($main{"sender_reject_recipients"},
+    "sender_reject_recipients")))
+      if defined $main{"sender_reject_recipients"};
+
+if (&bool("sender_verify"))
+  {
+  if (defined $main{"sender_verify_hosts_callback"} &&
+      defined $main{"sender_verify_callback_domains"})
+    {
+    printf STDOUT ("  deny    hosts = %s\n",
+      &acl_quote($main{"sender_verify_hosts_callback"}));
+    printf STDOUT ("          sender_domains = %s\n",
+      &acl_quote($main{"sender_verify_callback_domains"}));
+    print  STDOUT  "         !verify = sender/callout";
+    print  STDOUT  "=$main{\"sender_verify_callback_timeout\"}"
+      if defined $main{"sender_verify_callback_timeout"};
+    print  STDOUT  "\n";
+    }
+
+  if (defined $main{"sender_verify_hosts"})
+    {
+    printf STDOUT ("  deny    hosts = %s\n",
+      &acl_quote($main{"sender_verify_hosts"}));
+    print  STDOUT  "         !verify = sender\n";
+    }
+  else
+    {
+    print STDOUT "  require verify = sender\n";
+    }
+  }
+
+if (&bool("receiver_verify"))
+  {
+  print  STDOUT  "  deny    message = unrouteable address\n";
+  printf STDOUT ("          recipients = %s\n",
+    &acl_quote(&sort_address_list($main{"receiver_verify_addresses"},
+      "receiver_verify_addresses")))
+        if defined $main{"receiver_verify_addresses"};
+  printf STDOUT ("          hosts = %s\n",
+    &acl_quote($main{"receiver_verify_hosts"}))
+      if defined $main{"receiver_verify_hosts"};
+  printf STDOUT ("          senders = %s\n",
+    &acl_quote(&sort_address_list($main{"receiver_verify_senders"},
+      "receiver_verify_senders")))
+        if defined $main{"receiver_verify_senders"};
+  print  STDOUT  "         !verify = recipient\n";
+  }
+
+print STDOUT "  accept  domains = +local_domains\n"
+  if $local_domains !~ /^\s*$/;
+
+print STDOUT "  accept  domains = +relay_domains\n"
+  if $relay_domains !~ /^\s*$/;
+
+if (defined $main{"host_accept_relay"})
+  {
+  if (defined $main{"sender_address_relay"})
+    {
+    if (defined $main{"sender_address_relay_hosts"})
+      {
+      printf STDOUT ("  accept  hosts = %s\n",
+        &acl_quote($main{"sender_address_relay_hosts"}));
+      print  STDOUT  "          endpass\n";
+      print  STDOUT  "          message = invalid sender\n";
+      printf STDOUT ("          senders = %s\n",
+        &acl_quote(&sort_address_list($main{"sender_address_relay"},
+          "sender_address_relay")));
+      print  STDOUT  "  accept  hosts = +relay_hosts\n";
+      }
+    else
+      {
+      print  STDOUT  "  accept  hosts = +relay_hosts\n";
+      print  STDOUT  "          endpass\n";
+      print  STDOUT  "          message = invalid sender\n";
+      printf STDOUT ("          senders = %s\n",
+        &acl_quote(&sort_address_list($main{"sender_address_relay"},
+          "sender_address_relay")));
+      }
+    }
+  else
+    {
+    print STDOUT "  accept  hosts = +relay_hosts\n";
+    }
+  }
+
+print STDOUT "  accept  hosts = +auth_relay_hosts\n" .
+             "          endpass\n" .
+             "          message = authentication required\n" .
+             "          authenticated = *\n"
+  if defined $main{"host_auth_accept_relay"};
+
+print STDOUT "  accept  hosts = +tls_relay_hosts\n" .
+             "          endpass\n" .
+             "          message = encryption required\n" .
+             "          encrypted = *\n"
+  if defined $main{"tls_host_accept_relay"};
+
+print STDOUT "  deny    message = relay not permitted\n\n";
+
+
+# Output an ACL for use after the DATA command. This is concerned with
+# header checking.
+
+print STDOUT "#!!# ACL that is used after the DATA command\n";
+print STDOUT "check_message:\n";
+
+# Default for headers_checks_fail is true
+
+if (!defined $main{"headers_checks_fail"} ||
+    $main{"headers_checks_fail"} eq "true")
+  {
+  print STDOUT "  require verify = header_syntax\n"
+    if &bool("headers_check_syntax");
+  print STDOUT "  require verify = header_sender\n"
+    if &bool("headers_sender_verify");
+  print STDOUT "  accept  senders = !:\n  require verify = header_sender\n"
+    if &bool("headers_sender_verify_errmsg");
+  }
+else
+  {
+  print STDOUT "  warn    !verify = header_syntax\n"
+    if &bool("headers_check_syntax");
+  print STDOUT "  warn    !verify = header_sender\n"
+    if &bool("headers_sender_verify");
+  print STDOUT "  accept  senders = !:\n  warn    !verify = header_sender\n"
+    if &bool("headers_sender_verify_errmsg");
+  }
+
+print STDOUT "  accept\n\n";
+
+
+# Output an ACL for AUTH if required
+
+if (defined $main{"auth_over_tls_hosts"})
+  {
+  print STDOUT "#!!# ACL that is used after the AUTH command\n" .
+               "check_auth:\n" .
+               "  accept  hosts = +auth_over_tls_hosts\n" .
+               "          endpass\n" .
+               "          message = STARTTLS required before AUTH\n" .
+               "          encrypted = *\n" .
+               "  accept\n";
+  }
+
+
+# Output ACLs for ETRN, EXPN, and VRFY if required
+
+if (defined $main{"smtp_etrn_hosts"})
+  {
+  print STDOUT "#!!# ACL that is used after the ETRN command\n" .
+               "check_etrn:\n";
+  print STDOUT "  deny    hosts = +auth_hosts\n" .
+               "          message = authentication required\n" .
+               "         !authenticated = *\n"
+    if defined $main{"auth_hosts"};
+  print STDOUT "  accept  hosts = $main{\"smtp_etrn_hosts\"}\n\n";
+  }
+
+if (defined $main{"smtp_expn_hosts"})
+  {
+  print STDOUT "#!!# ACL that is used after the EXPN command\n" .
+               "check_expn:\n";
+  print STDOUT "  deny    hosts = +auth_hosts\n" .
+               "          message = authentication required\n" .
+               "         !authenticated = *\n"
+    if defined $main{"auth_hosts"};
+  print STDOUT "  accept  hosts = $main{\"smtp_expn_hosts\"}\n\n";
+  }
+
+if (&bool("smtp_verify"))
+  {
+  print STDOUT "#!!# ACL that is used after the VRFY command\n" .
+               "check_vrfy:\n";
+  print STDOUT "  deny    hosts = +auth_hosts\n" .
+               "          message = authentication required\n" .
+               "         !authenticated = *\n"
+    if defined $main{"auth_hosts"};
+  print STDOUT "  accept\n\n";
+  }
+
+# -------- The authenticators --------
+
+$started = 0;
+for ($i = $auth_start; $i < $clen; $i++)
+  {
+  if (!$started)
+    {
+    if ($c[$i] !~ /^\s*(#|$)/)
+      {
+      print STDOUT "\nbegin authenticators\n\n";
+      $started = 1;
+      }
+    }
+  print STDOUT "$c[$i]\n";
+  }
+
+
+# -------- Rewrite section --------
+
+$started = 0;
+for ($i = $rewrite_start; $i < $clen && $i < $auth_start - 1; $i++)
+  {
+  if (!$started)
+    {
+    if ($c[$i] !~ /^\s*(#|$)/)
+      {
+      print STDOUT "\nbegin rewrite\n\n";
+      $started = 1;
+      }
+    }
+  &print_no_expand($c[$i]);
+  }
+
+
+# -------- The routers configuration --------
+
+# The new routers configuration is created out of the old directors and routers
+# configuration. We put the old routers first, adding a "domains" option to
+# any that don't have one, to make them select the domains that do not match
+# the original local_domains. The routers get modified as necessary, and the
+# final one has "no_more" set, unless it has conditions. In that case we have
+# to add an extra router to be sure of failing all non-local addresses that
+# fall through. We do this also if there are no routers at all. The old
+# directors follow, modified as required.
+
+$prefix = "r.";
+undef @comments;
+
+print STDOUT "\n";
+print STDOUT "#!!#######################################################!!#\n";
+print STDOUT "#!!# Here follow routers created from the old routers,   #!!#\n";
+print STDOUT "#!!# for handling non-local domains.                     #!!#\n";
+print STDOUT "#!!#######################################################!!#\n";
+
+print STDOUT "\nbegin routers\n\n";
+
+for ($i = $router_start; $i < $clen; $i++)
+  {
+  $type = &checkline($c[$i]);
+  last if $type eq "end";
+
+  if ($type eq "comment") { push(@comments, "$c[$i]\n"); next; }
+
+  # When we hit the start of a driver, modify its options as necessary,
+  # and then output it from the stored option settings, having first output
+  # and previous comments.
+
+  if ($type eq "driver")
+    {
+    print STDOUT shift @comments while scalar(@comments) > 0;
+
+    $hash = $driverlist{"$prefix$name"};
+    $driver = $$hash{"driver"};
+    print STDOUT "$name:\n";
+
+    $add_no_more =
+      ! defined $$hash{"domains"} &&
+      ! defined $$hash{"local_parts"} &&
+      ! defined $$hash{"senders"} &&
+      ! defined $$hash{"condition"} &&
+      ! defined $$hash{"require_files"} &&
+      (!defined $$hash{"verify_only"} || $$hash{"verify_only"} eq "false") &&
+      (!defined $$hash{"verify"} || $$hash{"verify"} eq "true");
+
+    # Create a "domains" setting if there isn't one, unless local domains
+    # was explicitly empty.
+
+    $$hash{"domains"} = "! +local_domains"
+      if !defined $$hash{"domains"} && $local_domains !~ /^\s*$/;
+
+    # If the router had a local_parts setting, add caseful_local_part
+
+    $$hash{"caseful_local_part"} = "true" if defined $$hash{"local_parts"};
+
+    # If the router has "self=local" set, change it to "self=pass", and
+    # set pass_router to the router that was the first director. Change the
+    # obsolete self settings of "fail_hard" and "fail_soft" to "fail" and
+    # "pass".
+
+    if (defined $$hash{"self"})
+      {
+      if ($$hash{"self"} eq "local")
+        {
+        $$hash{"self"} = "pass";
+        $$hash{"pass_router"} = $first_director;
+        }
+      elsif ($$hash{"self"} eq "fail_hard")
+        {
+        $$hash{"self"} = "fail";
+        }
+      elsif ($$hash{"self"} eq "fail_soft")
+        {
+        $$hash{"self"} = "pass";
+        }
+      }
+
+    # If the router had a require_files setting, check it for user names
+    # and colons that are part of expansion items
+
+    if (defined $$hash{"require_files"})
+      {
+      &check_require($$hash{"require_files"}, "'$name' router");
+      if (($$hash{"require_files"} =~ s/(\$\{\w+):/$1::/g) > 0 ||
+          ($$hash{"require_files"} =~ s/ldap:/ldap::/g) > 0)
+        {
+        &rubric();
+        print STDERR "\n" .
+"*** A setting of require_files in the $name router contains\n" .
+"    a colon in what appears to be an expansion item. In Exim 3, the\n" .
+"    whole string was expanded before splitting the list, but in Exim 4\n" .
+"    each item is expanded separately, so colons that are not list\n" .
+"    item separators have to be doubled. One or more such colons in this\n" .
+"    list have been doubled as a precaution. Please check the result.\n";
+        }
+      }
+
+    # If the router had a "senders" setting, munge the address list
+
+    $$hash{"senders"} = &sort_address_list($$hash{"senders"}, "senders")
+      if defined $$hash{"senders"};
+
+    # ---- Changes to domainlist router ----
+
+    if ($driver eq "domainlist")
+      {
+      &abolished($hash, "A domainlist router",
+        "modemask", "owners", "owngroups",
+        "qualify_single", "search_parents");
+
+      # The name has changed
+
+      $$hash{"driver"} = "manualroute";
+
+      # Turn "route_file", "route_query" and "route_queries" into lookups for
+      # route_data.
+
+      if (defined $$hash{"route_file"})
+        {
+        $$hash{"route_data"} = "\${lookup\{\$domain\}$$hash{'search_type'}" .
+                         "\{$$hash{'route_file'}\}\}";
+        }
+      elsif (defined $$hash{"route_query"})
+        {
+        $$hash{"route_data"} = "\${lookup $$hash{'search_type'}" .
+                               "\{" . &unquote($$hash{'route_query'}) . "\}\}";
+        }
+      elsif (defined $$hash{"route_queries"})
+        {
+        $endkets = 0;
+        $$hash{"route_data"} = "";
+        $route_queries = $$hash{'route_queries'};
+        $route_queries =~ s/^"(.*)"$/$1/s;
+        $route_queries =~ s/::/++colons++/g;
+        @qq = split(/:/, $route_queries);
+
+        foreach $q (@qq)
+          {
+          $q =~ s/\+\+colons\+\+/:/g;
+          $q =~ s/^\s+//;
+          $q =~ s/\s+$//;
+          if ($endkets > 0)
+            {
+            $$hash{"route_data"} .= "\\\n    {";
+            $endkets++;
+            }
+          $$hash{"route_data"} .= "\${lookup $$hash{'search_type'} \{$q\}\{\$value\}";
+          $endkets++;
+          }
+
+        $$hash{"route_data"} .= "}" x $endkets;
+        }
+
+      delete $$hash{"route_file"};
+      delete $$hash{"route_query"};
+      delete $$hash{"route_queries"};
+      delete $$hash{"search_type"};
+
+      # But we can't allow both route_data and route_list
+
+      if (defined $$hash{"route_data"} && defined $$hash{"route_list"})
+        {
+        &rubric();
+        print STDERR "\n" .
+"** An Exim 3 'domainlist' router called '$name' contained a 'route_list'\n" .
+"   option as well as a setting of 'route_file', 'route_query', or\n" .
+"   'route_queries'. The latter has been turned into a 'route_data' setting,\n".
+"   but in Exim 4 you can't have both 'route_data' and 'route_list'. You'll\n" .
+"   have to rewrite this router; in the meantime, 'route_list' has been\n" .
+"   omitted.\n";
+        print STDOUT "#!!# route_list option removed\n";
+        delete $$hash{"route_list"};
+        }
+
+      # Change bydns_a into bydns in a route_list; also bydns_mx, but that
+      # works differently.
+
+      if (defined $$hash{"route_list"})
+        {
+        $$hash{"route_list"} =~ s/bydns_a/bydns/g;
+        if ($$hash{"route_list"} =~ /bydns_mx/)
+          {
+          $$hash{"route_list"} =~ s/bydns_mx/bydns/g;
+          &rubric();
+          print STDERR "\n" .
+"*** An Exim 3 'domainlist' router called '$name' contained a 'route_list'\n" .
+"    option which used 'bydns_mx'. This feature no longer exists in Exim 4.\n" .
+"    It has been changed to 'bydns', but it won't have the same effect,\n" .
+"    because it will look for A rather than MX records. Use the 'dnslookup'\n" .
+"    router to do MX lookups - if you want to override the hosts found from\n" .
+"    MX records, you should route to a special 'smtp' transport which has\n" .
+"    both 'hosts' and 'hosts_override' set.\n";
+          }
+        }
+
+      # Arrange to not expand regex
+
+      $$hash{"route_list"} = &no_expand_regex($$hash{"route_list"}, ";")
+        if (defined $$hash{"route_list"})
+      }
+
+
+    # ---- Changes to iplookup router ----
+
+    elsif ($driver eq "iplookup")
+      {
+      &renamed($hash, "service", "port");
+      }
+
+
+    # ---- Changes to lookuphost router ----
+
+    elsif ($driver eq "lookuphost")
+      {
+      $$hash{"driver"} = "dnslookup";
+
+      if (defined $$hash{"gethostbyname"})
+        {
+        &rubric();
+        print STDERR "\n" .
+"** An Exim 3 'lookuphost' router called '$name' used the 'gethostbyname'\n" .
+"   option, which no longer exists. You will have to rewrite it.\n";
+        print STDOUT "#!!# gethostbyname option removed\n";
+        delete $$hash{"gethostbyname"};
+        }
+
+      $$hash{"mx_domains"} = &no_expand_regex($$hash{"mx_domains"})
+        if defined $$hash{"mx_domains"};
+      }
+
+
+    # ---- Changes to the queryprogram router ----
+
+    elsif ($driver eq "queryprogram")
+      {
+      &rubric();
+      print STDERR "\n" .
+"** The configuration contains a 'queryprogram' router. Please note that\n" .
+"   the specification for the text that is returned by the program run\n" .
+"   by this router has changed in Exim 4. You will need to modify your\n" .
+"   program.\n";
+
+      if (!defined $$hash{'command_user'})
+        {
+        &rubric();
+        print STDERR "\n" .
+"** The 'queryprogram' router called '$name' does not have a setting for\n" .
+"   the 'command_user' option. This is mandatory in Exim 4. A setting of\n" .
+"   'nobody' has been created.\n";
+        $$hash{"command_user"} = "nobody";
+        }
+      }
+
+
+    # -------------------------------------
+
+    # Output the router's option settings
+
+    &outdriver($hash);
+    next;
+    }
+
+  # Skip past any continuation lines for an option setting
+  while ($c[$i] =~ /\\\s*$/s && $i < $clen - 1)
+    {
+    $i++;
+    $i++ while ($c[$i] =~ /^\s*#/);
+    }
+  }
+
+# Add "no_more" to the final driver from the old routers, provided it had no
+# conditions. Otherwise, or if there were no routers, make up one to fail all
+# non-local domains.
+
+if ($add_no_more)
+  {
+  print STDOUT "  no_more\n";
+  print STDOUT shift @comments while scalar(@comments) > 0;
+  }
+else
+  {
+  print STDOUT shift @comments while scalar(@comments) > 0;
+  print STDOUT "\n#!!# This new router is put here to fail all domains that\n";
+  print STDOUT "#!!# were not in local_domains in the Exim 3 configuration.\n\n";
+  print STDOUT "fail_remote_domains:\n";
+  print STDOUT "  driver = redirect\n";
+  print STDOUT "  domains = ! +local_domains\n";
+  print STDOUT "  allow_fail\n";
+  print STDOUT "  data = :fail: unrouteable mail domain \"\$domain\"\n\n";
+  }
+
+# Now copy the directors, making appropriate changes
+
+print STDOUT "\n";
+print STDOUT "#!!#######################################################!!#\n";
+print STDOUT "#!!# Here follow routers created from the old directors, #!!#\n";
+print STDOUT "#!!# for handling local domains.                         #!!#\n";
+print STDOUT "#!!#######################################################!!#\n";
+
+$prefix = "d.";
+for ($i = $director_start; $i < $clen; $i++)
+  {
+  $type = &checkline($c[$i]);
+  last if $type eq "end";
+
+  if ($type eq "comment") { print STDOUT "$c[$i]\n"; next; }
+
+  undef $second_router;
+
+  if ($type eq "driver")
+    {
+    $hash = $driverlist{"$prefix$name"};
+    $driver = $$hash{"driver"};
+    print STDOUT "$name:\n";
+
+    $$hash{"caseful_local_part"} = "true" if $add_caseful_local_part;
+
+    if (defined $$hash{"local_parts"} &&
+        (defined $$hash{"prefix"} || defined $hash{"suffix"}))
+      {
+      &rubric();
+      print STDERR "\n" .
+"** The Exim 3 configuration contains a director called '$name' which has\n" .
+"   'local_parts' set, together with either or both of 'prefix' and 'suffix'\n".
+"   This combination has a different effect in Exim 4, where the affix\n" .
+"   is removed *before* 'local_parts' is tested. You will probably need\n" .
+"   to make changes to this driver.\n";
+      }
+
+    &renamed($hash, "prefix", "local_part_prefix");
+    &renamed($hash, "prefix_optional", "local_part_prefix_optional");
+    &renamed($hash, "suffix", "local_part_suffix");
+    &renamed($hash, "suffix_optional", "local_part_suffix_optional");
+    &renamed($hash, "new_director", "redirect_router");
+
+    &handle_current_and_home_directory($hash, $driver, $name);
+
+    # If the director had a require_files setting, check it for user names
+    # and colons that are part of expansion items
+
+    if (defined $$hash{"require_files"})
+      {
+      &check_require($$hash{"require_files"}, "'$name' director");
+      if (($$hash{"require_files"} =~ s/(\$\{\w+):/$1::/g) > 0 ||
+          ($$hash{"require_files"} =~ s/ldap:/ldap::/g) > 0)
+        {
+        &rubric();
+        print STDERR "\n" .
+"*** A setting of require_files in the $name director contains\n" .
+"    a colon in what appears to be an expansion item. In Exim 3, the\n" .
+"    whole string was expanded before splitting the list, but in Exim 4\n" .
+"    each item is expanded separately, so colons that are not list\n" .
+"    item separators have to be doubled. One or more such colons in this\n" .
+"    list have been doubled as a precaution. Please check the result.\n";
+        }
+      }
+
+    # If the director had a "senders" setting, munge the address list
+
+    $$hash{"senders"} = &sort_address_list($$hash{"senders"}, "senders")
+      if defined $$hash{"senders"};
+
+    # ---- Changes to aliasfile director ----
+
+    if ($driver eq "aliasfile")
+      {
+      &abolished($hash, "An aliasfile director",
+        "directory2_transport", "freeze_missing_include",
+        "modemask", "owners", "owngroups");
+
+      $$hash{"driver"} = "redirect";
+
+      $key = "\$local_part";
+      $key = "\$local_part\@\$domain"
+        if defined $$hash{"include_domain"} &&
+          $$hash{"include_domain"} eq "true";
+      delete $$hash{"include_domain"};
+
+      if (defined $$hash{"forbid_special"} && $$hash{"forbid_special"} eq "true")
+        {
+        $$hash{"forbid_blackhole"} = "true";
+        }
+      else
+        {
+        $$hash{"allow_defer"} = "true";
+        $$hash{"allow_fail"} = "true";
+        }
+      delete $$hash{"forbid_special"};
+
+      # Deal with "file", "query", or "queries"
+
+      if (defined $$hash{"file"})
+        {
+        $$hash{"data"} =
+          "\$\{lookup\{$key\}$$hash{'search_type'}\{$$hash{'file'}\}\}";
+        if (defined $$hash{"optional"} && $$hash{"optional"} eq "true")
+          {
+          $$hash{"data"} =
+            "\$\{if exists\{$$hash{'file'}\}\{$$hash{'data'}\}\}";
+          }
+        delete $$hash{"optional"};
+        }
+      elsif (defined $$hash{"query"})
+        {
+        &abolished($hash, "An aliasfile director", "optional");
+        $$hash{"data"} = "\${lookup $$hash{'search_type'} " .
+                         "\{" . &unquote($$hash{'query'}) . "\}\}";
+        }
+      else   # Must be queries
+        {
+        &abolished($hash, "An aliasfile director", "optional");
+        $endkets = 0;
+        $$hash{"data"} = "";
+        $queries = $$hash{'queries'};
+        $queries =~ s/^"(.*)"$/$1/s;
+        $queries =~ s/::/++colons++/g;
+        @qq = split(/:/, $queries);
+
+        foreach $q (@qq)
+          {
+          $q =~ s/\+\+colons\+\+/:/g;
+          $q =~ s/^\s+//;
+          $q =~ s/\s+$//;
+          if ($endkets > 0)
+            {
+            $$hash{"data"} .= "\\\n    {";
+            $endkets++;
+            }
+          $$hash{"data"} .= "\${lookup $$hash{'search_type'} \{$q\}\{\$value\}";
+          $endkets++;
+          }
+
+        $$hash{"data"} .= "}" x $endkets;
+        }
+
+      $$hash{"data"} = "\${expand:$$hash{'data'}\}"
+        if (defined $$hash{"expand"} && $$hash{"expand"} eq "true");
+
+      delete $$hash{"expand"};
+      delete $$hash{"file"};
+      delete $$hash{"query"};
+      delete $$hash{"queries"};
+      delete $$hash{"search_type"};
+
+      # Turn aliasfile + transport into accept + condition
+
+      if (defined $$hash{'transport'})
+        {
+        &rubric();
+        if (!defined $$hash{'condition'})
+          {
+          print STDERR "\n" .
+"** The Exim 3 configuration contains an aliasfile director called '$name',\n".
+"   which has 'transport' set. This has been turned into an 'accept' router\n".
+"   with a 'condition' setting, but should be carefully checked.\n";
+          $$hash{'driver'} = "accept";
+          $$hash{'condition'} =
+            "\$\{if eq \{\}\{$$hash{'data'}\}\{no\}\{yes\}\}";
+          delete $$hash{'data'};
+          delete $$hash{'allow_defer'};
+          delete $$hash{'allow_fail'};
+          }
+        else
+          {
+          print STDERR "\n" .
+"** The Exim 3 configuration contains an aliasfile director called '$name',\n".
+"   which has 'transport' set. This cannot be turned into an 'accept' router\n".
+"   with a 'condition' setting, because there is already a 'condition'\n" .
+"   setting. It has been left as 'redirect' with a transport, which is\n" .
+"   invalid - you must sort this one out.\n";
+          }
+        }
+      }
+
+
+    # ---- Changes to forwardfile director ----
+
+    elsif ($driver eq "forwardfile")
+      {
+      &abolished($hash, "A forwardfile director",
+        "check_group", "directory2_transport",
+        "freeze_missing_include", "match_directory",
+        "seteuid");
+
+      &renamed($hash, "filter", "allow_filter");
+
+      $$hash{"driver"} = "redirect";
+      $$hash{"check_local_user"} = "true"
+        if !defined $$hash{"check_local_user"};
+
+      if (defined $$hash{"forbid_pipe"} && $$hash{"forbid_pipe"} eq "true")
+        {
+        print STDOUT "#!!# forbid_filter_run added because forbid_pipe is set\n";
+        $$hash{"forbid_filter_run"} = "true";
+        }
+
+      if (defined $$hash{'allow_system_actions'} &&
+          $$hash{'allow_system_actions'} eq 'true')
+        {
+        $$hash{'allow_freeze'} = "true";
+        }
+      delete $$hash{'allow_system_actions'};
+
+      # If file_directory is defined, use it to qualify relative paths; if not,
+      # and check_local_user is defined, use $home. Remove file_directory from
+      # the output.
+
+      $dir = "";
+      if (defined $$hash{"file_directory"})
+        {
+        $dir = $$hash{"file_directory"} . "/";
+        delete $$hash{"file_directory"};
+        }
+      elsif ($$hash{"check_local_user"} eq "true")
+        {
+        $dir = "\$home/";
+        }
+
+      # If it begins with an upper case letter, guess that this is really
+      # a macro.
+
+      if (defined $$hash{"file"} && $$hash{"file"} !~ /^[\/A-Z]/)
+        {
+        $$hash{"file"} = $dir . $$hash{"file"};
+        }
+      }
+
+
+    # ---- Changes to localuser director ----
+
+    elsif ($driver eq "localuser")
+      {
+      &abolished($hash, "A localuser director", "match_directory");
+      $$hash{"driver"} = "accept";
+      $$hash{"check_local_user"} = "true";
+      }
+
+
+    # ---- Changes to smartuser director ----
+
+    elsif ($driver eq "smartuser")
+      {
+      &abolished($hash, "A smartuser director", "panic_expansion_fail");
+
+      $transport = $$hash{"transport"};
+      $new_address = $$hash{"new_address"};
+
+      if (defined $transport && defined $new_address)
+        {
+        &rubric();
+        print STDERR "\n" .
+"** The Exim 3 configuration contains a smartuser director called '$name',\n".
+"   which has both 'transport' and 'new_address' set. This has been turned\n".
+"   into two routers for Exim 4. However, if the new address contains a\n" .
+"   reference to \$local_part, this won't work correctly. In any case, you\n".
+"   may be able to make it tidier by rewriting.\n";
+        $$hash{"driver"} = "redirect";
+        $$hash{"data"} = $new_address;
+        $$hash{"redirect_router"} = "${name}_part2";
+
+        $second_router = "\n".
+          "#!!# This router is invented to go with the previous one because\n".
+          "#!!# in Exim 4 you can't have a change of address and a transport\n".
+          "#!!# setting in the same router as you could in Exim 3.\n\n" .
+          "${name}_part2:\n".
+          "  driver = accept\n".
+          "  condition = \$\{if eq\{\$local_part@\$domain\}" .
+          "\{$new_address\}\{yes\}\{no\}\}\n".
+          "  transport = $$hash{'transport'}\n";
+
+        delete $$hash{"new_address"};
+        delete $$hash{"transport"};
+        }
+      elsif (defined $new_address)
+        {
+        $$hash{"driver"} = "redirect";
+        $$hash{"data"} = $new_address;
+        $$hash{"allow_defer"} = "true";
+        $$hash{"allow_fail"} = "true";
+        delete $$hash{"new_address"};
+        }
+      else     # Includes the case of neither set (verify_only)
+        {
+        $$hash{"driver"} = "accept";
+        if (defined $$hash{"rewrite"})
+          {
+          &rubric();
+          print STDERR "\n" .
+"** The Exim 3 configuration contains a setting of the 'rewrite' option on\n".
+"   a smartuser director called '$name', but this director does not have\n".
+"   a setting of 'new_address', so 'rewrite' has no effect. The director\n".
+"   has been turned into an 'accept' router, and 'rewrite' has been discarded.";
+          delete $$hash{"rewrite"};
+          }
+        }
+      }
+
+
+    # -------------------------------------
+
+    # For ex-directors that don't have check_local_user set, add
+    # retry_use_local_part to imitate what Exim 3 would have done.
+
+    $$hash{"retry_use_local_part"} = "true"
+      if (!defined $$hash{"check_local_user"} ||
+          $$hash{"check_local_user"} eq "false") ;
+
+    # Output the router's option settings
+
+    &outdriver($hash);
+
+    # Output an auxiliary router if one is needed
+
+    print STDOUT $second_router if defined $second_router;
+
+    next;
+    }
+
+  # Skip past any continuation lines for an option setting
+  while ($c[$i] =~ /\\\s*$/s)
+    {
+    $i++;
+    $i++ while ($c[$i] =~ /^\s*#/);
+    }
+  }
+
+
+
+# -------- The transports configuration --------
+
+$started = 0;
+$prefix = "t.";
+for ($i = $transport_start; $i < $clen; $i++)
+  {
+  $type = &checkline($c[$i]);
+  last if $type eq "end";
+
+  if ($type eq "comment") { print STDOUT "$c[$i]\n"; next; }
+
+  if (!$started)
+    {
+    print STDOUT "begin transports\n\n";
+    $started = 1;
+    }
+
+  if ($type eq "driver")
+    {
+    $hash = $driverlist{"$prefix$name"};
+    $driver = $$hash{"driver"};
+    print STDOUT "$name:\n";
+
+    # ---- Changes to the appendfile transport ----
+
+    if ($driver eq "appendfile")
+      {
+      &renamed($hash, "prefix", "message_prefix");
+      &renamed($hash, "suffix", "message_suffix");
+      &abolished($hash, "An appendfile transport",
+        "require_lockfile");
+      &handle_batch_and_bsmtp($hash);
+      if (defined $$hash{"from_hack"} && $$hash{"from_hack"} eq "false")
+        {
+        print STDOUT "#!!# no_from_hack replaced by check_string\n";
+        $$hash{"check_string"} = "";
+        }
+      delete $$hash{"from_hack"};
+      }
+
+    # ---- Changes to the lmtp transport ----
+
+    elsif ($driver eq "lmtp")
+      {
+      if (defined $$hash{"batch"} && $$hash{"batch"} ne "none")
+        {
+        $$hash{"batch_max"} = "100" if !defined $$hash{"batch_max"};
+        $$hash{"batch_id"} = "\$domain" if $$hash{"batch"} eq "domain";
+        }
+      else
+        {
+        $$hash{"batch_max"} = "1" if defined $$hash{"batch_max"};
+        }
+      delete $$hash{"batch"};
+      }
+
+    # ---- Changes to the pipe transport ----
+
+    elsif ($driver eq "pipe")
+      {
+      &renamed($hash, "prefix", "message_prefix");
+      &renamed($hash, "suffix", "message_suffix");
+      &handle_batch_and_bsmtp($hash);
+      if (defined $$hash{"from_hack"} && $$hash{"from_hack"} eq "false")
+        {
+        print STDOUT "#!!# no_from_hack replaced by check_string\n";
+        $$hash{"check_string"} = "";
+        }
+      delete $$hash{"from_hack"};
+      }
+
+    # ---- Changes to the smtp transport ----
+
+    elsif ($driver eq "smtp")
+      {
+      &abolished($hash, "An smtp transport", "mx_domains");
+      &renamed($hash, "service", "port");
+      &renamed($hash, "tls_verify_ciphers", "tls_require_ciphers");
+      &renamed($hash, "authenticate_hosts", "hosts_try_auth");
+
+      if (defined $$hash{"batch_max"})
+        {
+        print STDOUT "#!!# batch_max renamed connection_max_messages\n";
+        $$hash{"connection_max_messages"} = $$hash{"batch_max"};
+        delete $$hash{"batch_max"};
+        }
+
+      foreach $o ("hosts_try_auth", "hosts_avoid_tls", "hosts_require_tls",
+                  "mx_domains", "serialize_hosts")
+        {
+        $$hash{$o} = &no_expand_regex($$hash{$o}) if defined $$hash{$o};
+        }
+      }
+
+    &outdriver($driverlist{"$prefix$name"});
+    next;
+    }
+
+  # Skip past any continuation lines for an option setting
+  while ($c[$i] =~ /\\\s*$/s)
+    {
+    $i++;
+    $i++ while ($c[$i] =~ /^\s*#/);
+    }
+  }
+
+
+# -------- The retry configuration --------
+
+$started = 0;
+for ($i = $retry_start; $i < $clen && $i < $rewrite_start - 1; $i++)
+  {
+  if (!$started)
+    {
+    if ($c[$i] !~ /^\s*(#|$)/)
+      {
+      print STDOUT "\nbegin retry\n\n";
+      $started = 1;
+      }
+    }
+  &print_no_expand($c[$i]);
+  }
+
+print STDOUT "\n# End of Exim 4 configuration\n";
+
+print STDERR "\n*******************************************************\n";
+print STDERR   "***** Please review the generated file carefully. *****\n";
+print STDERR   "*******************************************************\n\n";
+
+# End of convert4r4
+
diff --git a/src/crypt16.c b/src/crypt16.c
new file mode 100644 (file)
index 0000000..e8a4fe8
--- /dev/null
@@ -0,0 +1,75 @@
+/*
+ * Copyright (c) 2000-2002
+ *   Chris Adams <cmadams@iruntheinter.net>
+ *   written for HiWAAY Internet Services
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
+ * USA
+ */
+
+/*
+ * Adapted for Exim by Tamas TEVESZ <ice@extreme.hu>
+ * Further adapted by Philip Hazel to cut out this function for operating
+ *   systems that have a built-in version.
+ */
+
+/* The OS has a built-in crypt16(). Some compilers don't like compiling empty
+modules, so keep them happy with a dummy when skipping the rest. */
+
+#include "config.h"
+
+#ifdef HAVE_CRYPT16
+static void dummy(int x) { dummy(x-1); }
+#else
+
+/* The OS doesn't have a built-in crypt16(). Compile this one. */
+
+#include <unistd.h>
+#include <string.h>
+#include "os.h"
+
+#ifdef CRYPT_H
+#include <crypt.h>
+#endif
+
+char *crypt16(char *key, char *salt)
+{
+       static char res[25];
+       static char s2[3];
+       char *p;
+
+       /* Clear the string of any previous data */
+       memset (res, 0, sizeof (res));
+
+       /* crypt the first part */
+       p = crypt (key, salt);
+       strncpy (res, p, 13);
+
+       if (strlen (key) > 8)
+       {
+               /* crypt the rest
+                * the first two characters of the first block (not counting
+                * the salt) make up the new salt */
+               strncpy (s2, &(res[2]), 2);
+               p = crypt (&(key[8]), s2);
+               strncpy (&(res[13]), &(p[2]), 11);
+               memset (s2, 0, sizeof (s2));
+       }
+
+       return (res);
+}
+#endif
+
+/* End of crypt16.c */
diff --git a/src/daemon.c b/src/daemon.c
new file mode 100644 (file)
index 0000000..5c64205
--- /dev/null
@@ -0,0 +1,2095 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2014 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Functions concerned with running Exim as a daemon */
+
+
+#include "exim.h"
+
+
+/* Structure for holding data for each SMTP connection */
+
+typedef struct smtp_slot {
+  pid_t pid;                       /* pid of the spawned reception process */
+  uschar *host_address;            /* address of the client host */
+} smtp_slot;
+
+/* An empty slot for initializing (Standard C does not allow constructor
+expressions in assigments except as initializers in declarations). */
+
+static smtp_slot empty_smtp_slot = { 0, NULL };
+
+
+
+/*************************************************
+*               Local static variables           *
+*************************************************/
+
+static SIGNAL_BOOL sigchld_seen;
+static SIGNAL_BOOL sighup_seen;
+
+static int   accept_retry_count = 0;
+static int   accept_retry_errno;
+static BOOL  accept_retry_select_failed;
+
+static int   queue_run_count = 0;
+static pid_t *queue_pid_slots = NULL;
+static smtp_slot *smtp_slots = NULL;
+
+static BOOL  write_pid = TRUE;
+
+
+
+/*************************************************
+*             SIGHUP Handler                     *
+*************************************************/
+
+/* All this handler does is to set a flag and re-enable the signal.
+
+Argument: the signal number
+Returns:  nothing
+*/
+
+static void
+sighup_handler(int sig)
+{
+sig = sig;    /* Keep picky compilers happy */
+sighup_seen = TRUE;
+signal(SIGHUP, sighup_handler);
+}
+
+
+
+/*************************************************
+*     SIGCHLD handler for main daemon process    *
+*************************************************/
+
+/* Don't re-enable the handler here, since we aren't doing the
+waiting here. If the signal is re-enabled, there will just be an
+infinite sequence of calls to this handler. The SIGCHLD signal is
+used just as a means of waking up the daemon so that it notices
+terminated subprocesses as soon as possible.
+
+Argument: the signal number
+Returns:  nothing
+*/
+
+static void
+main_sigchld_handler(int sig)
+{
+sig = sig;    /* Keep picky compilers happy */
+os_non_restarting_signal(SIGCHLD, SIG_DFL);
+sigchld_seen = TRUE;
+}
+
+
+
+
+/*************************************************
+*          Unexpected errors in SMTP calls       *
+*************************************************/
+
+/* This function just saves a bit of repetitious coding.
+
+Arguments:
+  log_msg        Text of message to be logged
+  smtp_msg       Text of SMTP error message
+  was_errno      The failing errno
+
+Returns:         nothing
+*/
+
+static void
+never_error(uschar *log_msg, uschar *smtp_msg, int was_errno)
+{
+uschar *emsg = (was_errno <= 0)? US"" :
+  string_sprintf(": %s", strerror(was_errno));
+log_write(0, LOG_MAIN|LOG_PANIC, "%s%s", log_msg, emsg);
+if (smtp_out != NULL) smtp_printf("421 %s\r\n", smtp_msg);
+}
+
+
+
+
+/*************************************************
+*            Handle a connected SMTP call        *
+*************************************************/
+
+/* This function is called when an SMTP connection has been accepted.
+If there are too many, give an error message and close down. Otherwise
+spin off a sub-process to handle the call. The list of listening sockets
+is required so that they can be closed in the sub-process. Take care not to
+leak store in this process - reset the stacking pool at the end.
+
+Arguments:
+  listen_sockets        sockets which are listening for incoming calls
+  listen_socket_count   count of listening sockets
+  accept_socket         socket of the current accepted call
+  accepted              socket information about the current call
+
+Returns:            nothing
+*/
+
+static void
+handle_smtp_call(int *listen_sockets, int listen_socket_count,
+  int accept_socket, struct sockaddr *accepted)
+{
+pid_t pid;
+union sockaddr_46 interface_sockaddr;
+EXIM_SOCKLEN_T ifsize = sizeof(interface_sockaddr);
+int dup_accept_socket = -1;
+int max_for_this_host = 0;
+int wfsize = 0;
+int wfptr = 0;
+int use_log_write_selector = log_write_selector;
+uschar *whofrom = NULL;
+
+void *reset_point = store_get(0);
+
+/* Make the address available in ASCII representation, and also fish out
+the remote port. */
+
+sender_host_address = host_ntoa(-1, accepted, NULL, &sender_host_port);
+DEBUG(D_any) debug_printf("Connection request from %s port %d\n",
+  sender_host_address, sender_host_port);
+
+/* Set up the output stream, check the socket has duplicated, and set up the
+input stream. These operations fail only the exceptional circumstances. Note
+that never_error() won't use smtp_out if it is NULL. */
+
+smtp_out = fdopen(accept_socket, "wb");
+if (smtp_out == NULL)
+  {
+  never_error(US"daemon: fdopen() for smtp_out failed", US"", errno);
+  goto ERROR_RETURN;
+  }
+
+dup_accept_socket = dup(accept_socket);
+if (dup_accept_socket < 0)
+  {
+  never_error(US"daemon: couldn't dup socket descriptor",
+    US"Connection setup failed", errno);
+  goto ERROR_RETURN;
+  }
+
+smtp_in = fdopen(dup_accept_socket, "rb");
+if (smtp_in == NULL)
+  {
+  never_error(US"daemon: fdopen() for smtp_in failed",
+    US"Connection setup failed", errno);
+  goto ERROR_RETURN;
+  }
+
+/* Get the data for the local interface address. Panic for most errors, but
+"connection reset by peer" just means the connection went away. */
+
+if (getsockname(accept_socket, (struct sockaddr *)(&interface_sockaddr),
+     &ifsize) < 0)
+  {
+  log_write(0, LOG_MAIN | ((errno == ECONNRESET)? 0 : LOG_PANIC),
+    "getsockname() failed: %s", strerror(errno));
+  smtp_printf("421 Local problem: getsockname() failed; please try again later\r\n");
+  goto ERROR_RETURN;
+  }
+
+interface_address = host_ntoa(-1, &interface_sockaddr, NULL, &interface_port);
+DEBUG(D_interface) debug_printf("interface address=%s port=%d\n",
+  interface_address, interface_port);
+
+/* Build a string identifying the remote host and, if requested, the port and
+the local interface data. This is for logging; at the end of this function the
+memory is reclaimed. */
+
+whofrom = string_append(whofrom, &wfsize, &wfptr, 3, "[", sender_host_address, "]");
+
+if ((log_extra_selector & LX_incoming_port) != 0)
+  whofrom = string_append(whofrom, &wfsize, &wfptr, 2, ":", string_sprintf("%d",
+    sender_host_port));
+
+if ((log_extra_selector & LX_incoming_interface) != 0)
+  whofrom = string_append(whofrom, &wfsize, &wfptr, 4, " I=[",
+    interface_address, "]:", string_sprintf("%d", interface_port));
+
+whofrom[wfptr] = 0;    /* Terminate the newly-built string */
+
+/* Check maximum number of connections. We do not check for reserved
+connections or unacceptable hosts here. That is done in the subprocess because
+it might take some time. */
+
+if (smtp_accept_max > 0 && smtp_accept_count >= smtp_accept_max)
+  {
+  DEBUG(D_any) debug_printf("rejecting SMTP connection: count=%d max=%d\n",
+    smtp_accept_count, smtp_accept_max);
+  smtp_printf("421 Too many concurrent SMTP connections; "
+    "please try again later.\r\n");
+  log_write(L_connection_reject,
+            LOG_MAIN, "Connection from %s refused: too many connections",
+    whofrom);
+  goto ERROR_RETURN;
+  }
+
+/* If a load limit above which only reserved hosts are acceptable is defined,
+get the load average here, and if there are in fact no reserved hosts, do
+the test right away (saves a fork). If there are hosts, do the check in the
+subprocess because it might take time. */
+
+if (smtp_load_reserve >= 0)
+  {
+  load_average = OS_GETLOADAVG();
+  if (smtp_reserve_hosts == NULL && load_average > smtp_load_reserve)
+    {
+    DEBUG(D_any) debug_printf("rejecting SMTP connection: load average = %.2f\n",
+      (double)load_average/1000.0);
+    smtp_printf("421 Too much load; please try again later.\r\n");
+    log_write(L_connection_reject,
+              LOG_MAIN, "Connection from %s refused: load average = %.2f",
+      whofrom, (double)load_average/1000.0);
+    goto ERROR_RETURN;
+    }
+  }
+
+/* Check that one specific host (strictly, IP address) is not hogging
+resources. This is done here to prevent a denial of service attack by someone
+forcing you to fork lots of times before denying service. The value of
+smtp_accept_max_per_host is a string which is expanded. This makes it possible
+to provide host-specific limits according to $sender_host address, but because
+this is in the daemon mainline, only fast expansions (such as inline address
+checks) should be used. The documentation is full of warnings. */
+
+if (smtp_accept_max_per_host != NULL)
+  {
+  uschar *expanded = expand_string(smtp_accept_max_per_host);
+  if (expanded == NULL)
+    {
+    if (!expand_string_forcedfail)
+      log_write(0, LOG_MAIN|LOG_PANIC, "expansion of smtp_accept_max_per_host "
+        "failed for %s: %s", whofrom, expand_string_message);
+    }
+  /* For speed, interpret a decimal number inline here */
+  else
+    {
+    uschar *s = expanded;
+    while (isdigit(*s))
+      max_for_this_host = max_for_this_host * 10 + *s++ - '0';
+    if (*s != 0)
+      log_write(0, LOG_MAIN|LOG_PANIC, "expansion of smtp_accept_max_per_host "
+        "for %s contains non-digit: %s", whofrom, expanded);
+    }
+  }
+
+/* If we have fewer connections than max_for_this_host, we can skip the tedious
+per host_address checks. Note that at this stage smtp_accept_count contains the
+count of *other* connections, not including this one. */
+
+if ((max_for_this_host > 0) &&
+    (smtp_accept_count >= max_for_this_host))
+  {
+  int i;
+  int host_accept_count = 0;
+  int other_host_count = 0;    /* keep a count of non matches to optimise */
+
+  for (i = 0; i < smtp_accept_max; ++i)
+    {
+    if (smtp_slots[i].host_address != NULL)
+      {
+      if (Ustrcmp(sender_host_address, smtp_slots[i].host_address) == 0)
+       host_accept_count++;
+      else
+       other_host_count++;
+
+      /* Testing all these strings is expensive - see if we can drop out
+      early, either by hitting the target, or finding there are not enough
+      connections left to make the target. */
+
+      if ((host_accept_count >= max_for_this_host) ||
+         ((smtp_accept_count - other_host_count) < max_for_this_host))
+       break;
+      }
+    }
+
+  if (host_accept_count >= max_for_this_host)
+    {
+    DEBUG(D_any) debug_printf("rejecting SMTP connection: too many from this "
+      "IP address: count=%d max=%d\n",
+      host_accept_count, max_for_this_host);
+    smtp_printf("421 Too many concurrent SMTP connections "
+      "from this IP address; please try again later.\r\n");
+    log_write(L_connection_reject,
+              LOG_MAIN, "Connection from %s refused: too many connections "
+      "from that IP address", whofrom);
+    goto ERROR_RETURN;
+    }
+  }
+
+/* OK, the connection count checks have been passed. Before we can fork the
+accepting process, we must first log the connection if requested. This logging
+used to happen in the subprocess, but doing that means that the value of
+smtp_accept_count can be out of step by the time it is logged. So we have to do
+the logging here and accept the performance cost. Note that smtp_accept_count
+hasn't yet been incremented to take account of this connection.
+
+In order to minimize the cost (because this is going to happen for every
+connection), do a preliminary selector test here. This saves ploughing through
+the generalized logging code each time when the selector is false. If the
+selector is set, check whether the host is on the list for logging. If not,
+arrange to unset the selector in the subprocess. */
+
+if ((log_write_selector & L_smtp_connection) != 0)
+  {
+  uschar *list = hosts_connection_nolog;
+  if (list != NULL && verify_check_host(&list) == OK)
+    use_log_write_selector &= ~L_smtp_connection;
+  else
+    log_write(L_smtp_connection, LOG_MAIN, "SMTP connection from %s "
+      "(TCP/IP connection count = %d)", whofrom, smtp_accept_count + 1);
+  }
+
+/* Now we can fork the accepting process; do a lookup tidy, just in case any
+expansion above did a lookup. */
+
+search_tidyup();
+pid = fork();
+
+/* Handle the child process */
+
+if (pid == 0)
+  {
+  int i;
+  int queue_only_reason = 0;
+  int old_pool = store_pool;
+  int save_debug_selector = debug_selector;
+  BOOL local_queue_only;
+  BOOL session_local_queue_only;
+  #ifdef SA_NOCLDWAIT
+  struct sigaction act;
+  #endif
+
+  smtp_accept_count++;    /* So that it includes this process */
+
+  /* May have been modified for the subprocess */
+
+  log_write_selector = use_log_write_selector;
+
+  /* Get the local interface address into permanent store */
+
+  store_pool = POOL_PERM;
+  interface_address = string_copy(interface_address);
+  store_pool = old_pool;
+
+  /* Check for a tls-on-connect port */
+
+  if (host_is_tls_on_connect_port(interface_port)) tls_in.on_connect = TRUE;
+
+  /* Expand smtp_active_hostname if required. We do not do this any earlier,
+  because it may depend on the local interface address (indeed, that is most
+  likely what it depends on.) */
+
+  smtp_active_hostname = primary_hostname;
+  if (raw_active_hostname != NULL)
+    {
+    uschar *nah = expand_string(raw_active_hostname);
+    if (nah == NULL)
+      {
+      if (!expand_string_forcedfail)
+        {
+        log_write(0, LOG_MAIN|LOG_PANIC, "failed to expand \"%s\" "
+          "(smtp_active_hostname): %s", raw_active_hostname,
+          expand_string_message);
+        smtp_printf("421 Local configuration error; "
+          "please try again later.\r\n");
+        mac_smtp_fflush();
+        search_tidyup();
+        _exit(EXIT_FAILURE);
+        }
+      }
+    else if (nah[0] != 0) smtp_active_hostname = nah;
+    }
+
+  /* Initialize the queueing flags */
+
+  queue_check_only();
+  session_local_queue_only = queue_only;
+
+  /* Close the listening sockets, and set the SIGCHLD handler to SIG_IGN.
+  We also attempt to set things up so that children are automatically reaped,
+  but just in case this isn't available, there's a paranoid waitpid() in the
+  loop too (except for systems where we are sure it isn't needed). See the more
+  extensive comment before the reception loop in exim.c for a fuller
+  explanation of this logic. */
+
+  for (i = 0; i < listen_socket_count; i++) (void)close(listen_sockets[i]);
+
+  /* Set FD_CLOEXEC on the SMTP socket. We don't want any rogue child processes
+  to be able to communicate with them, under any circumstances. */
+  (void)fcntl(accept_socket, F_SETFD,
+              fcntl(accept_socket, F_GETFD) | FD_CLOEXEC);
+  (void)fcntl(dup_accept_socket, F_SETFD,
+              fcntl(dup_accept_socket, F_GETFD) | FD_CLOEXEC);
+
+  #ifdef SA_NOCLDWAIT
+  act.sa_handler = SIG_IGN;
+  sigemptyset(&(act.sa_mask));
+  act.sa_flags = SA_NOCLDWAIT;
+  sigaction(SIGCHLD, &act, NULL);
+  #else
+  signal(SIGCHLD, SIG_IGN);
+  #endif
+
+  /* Attempt to get an id from the sending machine via the RFC 1413
+  protocol. We do this in the sub-process in order not to hold up the
+  main process if there is any delay. Then set up the fullhost information
+  in case there is no HELO/EHLO.
+
+  If debugging is enabled only for the daemon, we must turn if off while
+  finding the id, but turn it on again afterwards so that information about the
+  incoming connection is output. */
+
+  if (debug_daemon) debug_selector = 0;
+  verify_get_ident(IDENT_PORT);
+  host_build_sender_fullhost();
+  debug_selector = save_debug_selector;
+
+  DEBUG(D_any)
+    debug_printf("Process %d is handling incoming connection from %s\n",
+      (int)getpid(), sender_fullhost);
+
+  /* Now disable debugging permanently if it's required only for the daemon
+  process. */
+
+  if (debug_daemon) debug_selector = 0;
+
+  /* If there are too many child processes for immediate delivery,
+  set the session_local_queue_only flag, which is initialized from the
+  configured value and may therefore already be TRUE. Leave logging
+  till later so it will have a message id attached. Note that there is no
+  possibility of re-calculating this per-message, because the value of
+  smtp_accept_count does not change in this subprocess. */
+
+  if (smtp_accept_queue > 0 && smtp_accept_count > smtp_accept_queue)
+    {
+    session_local_queue_only = TRUE;
+    queue_only_reason = 1;
+    }
+
+  /* Handle the start of the SMTP session, then loop, accepting incoming
+  messages from the SMTP connection. The end will come at the QUIT command,
+  when smtp_setup_msg() returns 0. A break in the connection causes the
+  process to die (see accept.c).
+
+  NOTE: We do *not* call smtp_log_no_mail() if smtp_start_session() fails,
+  because a log line has already been written for all its failure exists
+  (usually "connection refused: <reason>") and writing another one is
+  unnecessary clutter. */
+
+  if (!smtp_start_session())
+    {
+    mac_smtp_fflush();
+    search_tidyup();
+    _exit(EXIT_SUCCESS);
+    }
+
+  for (;;)
+    {
+    int rc;
+    message_id[0] = 0;            /* Clear out any previous message_id */
+    reset_point = store_get(0);   /* Save current store high water point */
+
+    DEBUG(D_any)
+      debug_printf("Process %d is ready for new message\n", (int)getpid());
+
+    /* Smtp_setup_msg() returns 0 on QUIT or if the call is from an
+    unacceptable host or if an ACL "drop" command was triggered, -1 on
+    connection lost, and +1 on validly reaching DATA. Receive_msg() almost
+    always returns TRUE when smtp_input is true; just retry if no message was
+    accepted (can happen for invalid message parameters). However, it can yield
+    FALSE if the connection was forcibly dropped by the DATA ACL. */
+
+    if ((rc = smtp_setup_msg()) > 0)
+      {
+      BOOL ok = receive_msg(FALSE);
+      search_tidyup();                    /* Close cached databases */
+      if (!ok)                            /* Connection was dropped */
+        {
+        mac_smtp_fflush();
+        smtp_log_no_mail();               /* Log no mail if configured */
+        _exit(EXIT_SUCCESS);
+        }
+      if (message_id[0] == 0) continue;   /* No message was accepted */
+      }
+    else
+      {
+      mac_smtp_fflush();
+      search_tidyup();
+      smtp_log_no_mail();                 /* Log no mail if configured */
+      _exit((rc == 0)? EXIT_SUCCESS : EXIT_FAILURE);
+      }
+
+    /* Show the recipients when debugging */
+
+    DEBUG(D_receive)
+      {
+      int i;
+      if (sender_address != NULL)
+        debug_printf("Sender: %s\n", sender_address);
+      if (recipients_list != NULL)
+        {
+        debug_printf("Recipients:\n");
+        for (i = 0; i < recipients_count; i++)
+          debug_printf("  %s\n", recipients_list[i].address);
+        }
+      }
+
+    /* A message has been accepted. Clean up any previous delivery processes
+    that have completed and are defunct, on systems where they don't go away
+    by themselves (see comments when setting SIG_IGN above). On such systems
+    (if any) these delivery processes hang around after termination until
+    the next message is received. */
+
+    #ifndef SIG_IGN_WORKS
+    while (waitpid(-1, NULL, WNOHANG) > 0);
+    #endif
+
+    /* Reclaim up the store used in accepting this message */
+
+    store_reset(reset_point);
+
+    /* If queue_only is set or if there are too many incoming connections in
+    existence, session_local_queue_only will be TRUE. If it is not, check
+    whether we have received too many messages in this session for immediate
+    delivery. */
+
+    if (!session_local_queue_only &&
+        smtp_accept_queue_per_connection > 0 &&
+        receive_messagecount > smtp_accept_queue_per_connection)
+      {
+      session_local_queue_only = TRUE;
+      queue_only_reason = 2;
+      }
+
+    /* Initialize local_queue_only from session_local_queue_only. If it is not
+    true, and queue_only_load is set, check that the load average is below it.
+    If local_queue_only is set by this means, we also set if for the session if
+    queue_only_load_latch is true (the default). This means that, once set,
+    local_queue_only remains set for any subsequent messages on the same SMTP
+    connection. This is a deliberate choice; even though the load average may
+    fall, it doesn't seem right to deliver later messages on the same call when
+    not delivering earlier ones. However, the are special circumstances such as
+    very long-lived connections from scanning appliances where this is not the
+    best strategy. In such cases, queue_only_load_latch should be set false. */
+
+    local_queue_only = session_local_queue_only;
+    if (!local_queue_only && queue_only_load >= 0)
+      {
+      local_queue_only = (load_average = OS_GETLOADAVG()) > queue_only_load;
+      if (local_queue_only)
+        {
+        queue_only_reason = 3;
+        if (queue_only_load_latch) session_local_queue_only = TRUE;
+        }
+      }
+
+    /* Log the queueing here, when it will get a message id attached, but
+    not if queue_only is set (case 0). */
+
+    if (local_queue_only) switch(queue_only_reason)
+      {
+      case 1:
+      log_write(L_delay_delivery,
+                LOG_MAIN, "no immediate delivery: too many connections "
+                "(%d, max %d)", smtp_accept_count, smtp_accept_queue);
+      break;
+
+      case 2:
+      log_write(L_delay_delivery,
+                LOG_MAIN, "no immediate delivery: more than %d messages "
+                "received in one connection", smtp_accept_queue_per_connection);
+      break;
+
+      case 3:
+      log_write(L_delay_delivery,
+                LOG_MAIN, "no immediate delivery: load average %.2f",
+                (double)load_average/1000.0);
+      break;
+      }
+
+    /* If a delivery attempt is required, spin off a new process to handle it.
+    If we are not root, we have to re-exec exim unless deliveries are being
+    done unprivileged. */
+
+    else if (!queue_only_policy && !deliver_freeze)
+      {
+      pid_t dpid;
+
+      /* Before forking, ensure that the C output buffer is flushed. Otherwise
+      anything that it in it will get duplicated, leading to duplicate copies
+      of the pending output. */
+
+      mac_smtp_fflush();
+
+      if ((dpid = fork()) == 0)
+        {
+        (void)fclose(smtp_in);
+        (void)fclose(smtp_out);
+
+        /* Don't ever molest the parent's SSL connection, but do clean up
+        the data structures if necessary. */
+
+        #ifdef SUPPORT_TLS
+        tls_close(TRUE, FALSE);
+        #endif
+
+        /* Reset SIGHUP and SIGCHLD in the child in both cases. */
+
+        signal(SIGHUP,  SIG_DFL);
+        signal(SIGCHLD, SIG_DFL);
+
+        if (geteuid() != root_uid && !deliver_drop_privilege)
+          {
+          signal(SIGALRM, SIG_DFL);
+          (void)child_exec_exim(CEE_EXEC_PANIC, FALSE, NULL, FALSE, 2, US"-Mc",
+            message_id);
+          /* Control does not return here. */
+          }
+
+        /* No need to re-exec; SIGALRM remains set to the default handler */
+
+        (void)deliver_message(message_id, FALSE, FALSE);
+        search_tidyup();
+        _exit(EXIT_SUCCESS);
+        }
+
+      if (dpid > 0)
+        {
+        DEBUG(D_any) debug_printf("forked delivery process %d\n", (int)dpid);
+        }
+      else
+        {
+        log_write(0, LOG_MAIN|LOG_PANIC, "daemon: delivery process fork "
+          "failed: %s", strerror(errno));
+        }
+      }
+    }
+  }
+
+
+/* Carrying on in the parent daemon process... Can't do much if the fork
+failed. Otherwise, keep count of the number of accepting processes and
+remember the pid for ticking off when the child completes. */
+
+if (pid < 0)
+  {
+  never_error(US"daemon: accept process fork failed", US"Fork failed", errno);
+  }
+else
+  {
+  int i;
+  for (i = 0; i < smtp_accept_max; ++i)
+    {
+    if (smtp_slots[i].pid <= 0)
+      {
+      smtp_slots[i].pid = pid;
+      if (smtp_accept_max_per_host != NULL)
+        smtp_slots[i].host_address = string_copy_malloc(sender_host_address);
+      smtp_accept_count++;
+      break;
+      }
+    }
+  DEBUG(D_any) debug_printf("%d SMTP accept process%s running\n",
+    smtp_accept_count, (smtp_accept_count == 1)? "" : "es");
+  }
+
+/* Get here via goto in error cases */
+
+ERROR_RETURN:
+
+/* Close the streams associated with the socket which will also close the
+socket fds in this process. We can't do anything if fclose() fails, but
+logging brings it to someone's attention. However, "connection reset by peer"
+isn't really a problem, so skip that one. On Solaris, a dropped connection can
+manifest itself as a broken pipe, so drop that one too. If the streams don't
+exist, something went wrong while setting things up. Make sure the socket
+descriptors are closed, in order to drop the connection. */
+
+if (smtp_out != NULL)
+  {
+  if (fclose(smtp_out) != 0 && errno != ECONNRESET && errno != EPIPE)
+    log_write(0, LOG_MAIN|LOG_PANIC, "daemon: fclose(smtp_out) failed: %s",
+      strerror(errno));
+  smtp_out = NULL;
+  }
+else (void)close(accept_socket);
+
+if (smtp_in != NULL)
+  {
+  if (fclose(smtp_in) != 0 && errno != ECONNRESET && errno != EPIPE)
+    log_write(0, LOG_MAIN|LOG_PANIC, "daemon: fclose(smtp_in) failed: %s",
+      strerror(errno));
+  smtp_in = NULL;
+  }
+else (void)close(dup_accept_socket);
+
+/* Release any store used in this process, including the store used for holding
+the incoming host address and an expanded active_hostname. */
+
+store_reset(reset_point);
+sender_host_address = NULL;
+}
+
+
+
+
+/*************************************************
+*       Check wildcard listen special cases      *
+*************************************************/
+
+/* This function is used when binding and listening on lists of addresses and
+ports. It tests for special cases of wildcard listening, when IPv4 and IPv6
+sockets may interact in different ways in different operating systems. It is
+passed an error number, the list of listening addresses, and the current
+address. Two checks are available: for a previous wildcard IPv6 address, or for
+a following wildcard IPv4 address, in both cases on the same port.
+
+In practice, pairs of wildcard addresses should be adjacent in the address list
+because they are sorted that way below.
+
+Arguments:
+  eno            the error number
+  addresses      the list of addresses
+  ipa            the current IP address
+  back           if TRUE, check for previous wildcard IPv6 address
+                 if FALSE, check for a following wildcard IPv4 address
+
+Returns:         TRUE or FALSE
+*/
+
+static BOOL
+check_special_case(int eno, ip_address_item *addresses, ip_address_item *ipa,
+  BOOL back)
+{
+ip_address_item *ipa2;
+
+/* For the "back" case, if the failure was "address in use" for a wildcard IPv4
+address, seek a previous IPv6 wildcard address on the same port. As it is
+previous, it must have been successfully bound and be listening. Flag it as a
+"6 including 4" listener. */
+
+if (back)
+  {
+  if (eno != EADDRINUSE || ipa->address[0] != 0) return FALSE;
+  for (ipa2 = addresses; ipa2 != ipa; ipa2 = ipa2->next)
+    {
+    if (ipa2->address[1] == 0 && ipa2->port == ipa->port)
+      {
+      ipa2->v6_include_v4 = TRUE;
+      return TRUE;
+      }
+    }
+  }
+
+/* For the "forward" case, if the current address is a wildcard IPv6 address,
+we seek a following wildcard IPv4 address on the same port. */
+
+else
+  {
+  if (ipa->address[0] != ':' || ipa->address[1] != 0) return FALSE;
+  for (ipa2 = ipa->next; ipa2 != NULL; ipa2 = ipa2->next)
+    if (ipa2->address[0] == 0 && ipa->port == ipa2->port) return TRUE;
+  }
+
+return FALSE;
+}
+
+
+
+
+/*************************************************
+*         Handle terminating subprocesses        *
+*************************************************/
+
+/* Handle the termination of child processes. Theoretically, this need be done
+only when sigchld_seen is TRUE, but rumour has it that some systems lose
+SIGCHLD signals at busy times, so to be on the safe side, this function is
+called each time round. It shouldn't be too expensive.
+
+Arguments:  none
+Returns:    nothing
+*/
+
+static void
+handle_ending_processes(void)
+{
+int status;
+pid_t pid;
+
+while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
+  {
+  int i;
+  DEBUG(D_any)
+    {
+    debug_printf("child %d ended: status=0x%x\n", (int)pid, status);
+#ifdef WCOREDUMP
+    if (WIFEXITED(status))
+      debug_printf("  normal exit, %d\n", WEXITSTATUS(status));
+    else if (WIFSIGNALED(status))
+      debug_printf("  signal exit, signal %d%s\n", WTERMSIG(status),
+          WCOREDUMP(status) ? " (core dumped)" : "");
+#endif
+    }
+
+  /* If it's a listening daemon for which we are keeping track of individual
+  subprocesses, deal with an accepting process that has terminated. */
+
+  if (smtp_slots != NULL)
+    {
+    for (i = 0; i < smtp_accept_max; i++)
+      {
+      if (smtp_slots[i].pid == pid)
+        {
+        if (smtp_slots[i].host_address != NULL)
+          store_free(smtp_slots[i].host_address);
+        smtp_slots[i] = empty_smtp_slot;
+        if (--smtp_accept_count < 0) smtp_accept_count = 0;
+        DEBUG(D_any) debug_printf("%d SMTP accept process%s now running\n",
+          smtp_accept_count, (smtp_accept_count == 1)? "" : "es");
+        break;
+        }
+      }
+    if (i < smtp_accept_max) continue;  /* Found an accepting process */
+    }
+
+  /* If it wasn't an accepting process, see if it was a queue-runner
+  process that we are tracking. */
+
+  if (queue_pid_slots != NULL)
+    {
+    for (i = 0; i < queue_run_max; i++)
+      {
+      if (queue_pid_slots[i] == pid)
+        {
+        queue_pid_slots[i] = 0;
+        if (--queue_run_count < 0) queue_run_count = 0;
+        DEBUG(D_any) debug_printf("%d queue-runner process%s now running\n",
+          queue_run_count, (queue_run_count == 1)? "" : "es");
+        break;
+        }
+      }
+    }
+  }
+}
+
+
+
+/*************************************************
+*              Exim Daemon Mainline              *
+*************************************************/
+
+/* The daemon can do two jobs, either of which is optional:
+
+(1) Listens for incoming SMTP calls and spawns off a sub-process to handle
+each one. This is requested by the -bd option, with -oX specifying the SMTP
+port on which to listen (for testing).
+
+(2) Spawns a queue-running process every so often. This is controlled by the
+-q option with a an interval time. (If no time is given, a single queue run
+is done from the main function, and control doesn't get here.)
+
+Root privilege is required in order to attach to port 25. Some systems require
+it when calling socket() rather than bind(). To cope with all cases, we run as
+root for both socket() and bind(). Some systems also require root in order to
+write to the pid file directory. This function must therefore be called as root
+if it is to work properly in all circumstances. Once the socket is bound and
+the pid file written, root privilege is given up if there is an exim uid.
+
+There are no arguments to this function, and it never returns. */
+
+void
+daemon_go(void)
+{
+struct passwd *pw;
+int *listen_sockets = NULL;
+int listen_socket_count = 0;
+ip_address_item *addresses = NULL;
+time_t last_connection_time = (time_t)0;
+
+/* If any debugging options are set, turn on the D_pid bit so that all
+debugging lines get the pid added. */
+
+DEBUG(D_any|D_v) debug_selector |= D_pid;
+
+if (inetd_wait_mode)
+  {
+  int on = 1;
+
+  listen_socket_count = 1;
+  listen_sockets = store_get(sizeof(int *));
+  (void) close(3);
+  if (dup2(0, 3) == -1)
+    {
+    log_write(0, LOG_MAIN|LOG_PANIC_DIE,
+        "failed to dup inetd socket safely away: %s", strerror(errno));
+    }
+  listen_sockets[0] = 3;
+  (void) close(0);
+  (void) close(1);
+  (void) close(2);
+  exim_nullstd();
+
+  if (debug_file == stderr)
+    {
+    /* need a call to log_write before call to open debug_file, so that
+    log.c:file_path has been initialised.  This is unfortunate. */
+    log_write(0, LOG_MAIN, "debugging Exim in inetd wait mode starting");
+
+    fclose(debug_file);
+    debug_file = NULL;
+    exim_nullstd(); /* re-open fd2 after we just closed it again */
+    debug_logging_activate(US"-wait", NULL);
+    }
+
+  DEBUG(D_any) debug_printf("running in inetd wait mode\n");
+
+  /* As per below, when creating sockets ourselves, we handle tcp_nodelay for
+  our own buffering; we assume though that inetd set the socket REUSEADDR. */
+
+  if (tcp_nodelay) setsockopt(3, IPPROTO_TCP, TCP_NODELAY,
+    (uschar *)(&on), sizeof(on));
+  }
+
+
+if (inetd_wait_mode || daemon_listen)
+  {
+  /* If any option requiring a load average to be available during the
+  reception of a message is set, call os_getloadavg() while we are root
+  for those OS for which this is necessary the first time it is called (in
+  order to perform an "open" on the kernel memory file). */
+
+  #ifdef LOAD_AVG_NEEDS_ROOT
+  if (queue_only_load >= 0 || smtp_load_reserve >= 0 ||
+       (deliver_queue_load_max >= 0 && deliver_drop_privilege))
+    (void)os_getloadavg();
+  #endif
+  }
+
+
+/* Do the preparation for setting up a listener on one or more interfaces, and
+possible on various ports. This is controlled by the combination of
+local_interfaces (which can set IP addresses and ports) and daemon_smtp_port
+(which is a list of default ports to use for those items in local_interfaces
+that do not specify a port). The -oX command line option can be used to
+override one or both of these options.
+
+If local_interfaces is not set, the default is to listen on all interfaces.
+When it is set, it can include "all IPvx interfaces" as an item. This is useful
+when different ports are in use.
+
+It turns out that listening on all interfaces is messy in an IPv6 world,
+because several different implementation approaches have been taken. This code
+is now supposed to work with all of them. The point of difference is whether an
+IPv6 socket that is listening on all interfaces will receive incoming IPv4
+calls or not. We also have to cope with the case when IPv6 libraries exist, but
+there is no IPv6 support in the kernel.
+
+. On Solaris, an IPv6 socket will accept IPv4 calls, and give them as mapped
+  addresses. However, if an IPv4 socket is also listening on all interfaces,
+  calls are directed to the appropriate socket.
+
+. On (some versions of) Linux, an IPv6 socket will accept IPv4 calls, and
+  give them as mapped addresses, but an attempt also to listen on an IPv4
+  socket on all interfaces causes an error.
+
+. On OpenBSD, an IPv6 socket will not accept IPv4 calls. You have to set up
+  two sockets if you want to accept both kinds of call.
+
+. FreeBSD is like OpenBSD, but it has the IPV6_V6ONLY socket option, which
+  can be turned off, to make it behave like the versions of Linux described
+  above.
+
+. I heard a report that the USAGI IPv6 stack for Linux has implemented
+  IPV6_V6ONLY.
+
+So, what we do when IPv6 is supported is as follows:
+
+ (1) After it is set up, the list of interfaces is scanned for wildcard
+     addresses. If an IPv6 and an IPv4 wildcard are both found for the same
+     port, the list is re-arranged so that they are together, with the IPv6
+     wildcard first.
+
+ (2) If the creation of a wildcard IPv6 socket fails, we just log the error and
+     carry on if an IPv4 wildcard socket for the same port follows later in the
+     list. This allows Exim to carry on in the case when the kernel has no IPv6
+     support.
+
+ (3) Having created an IPv6 wildcard socket, we try to set IPV6_V6ONLY if that
+     option is defined. However, if setting fails, carry on regardless (but log
+     the incident).
+
+ (4) If binding or listening on an IPv6 wildcard socket fails, it is a serious
+     error.
+
+ (5) If binding or listening on an IPv4 wildcard socket fails with the error
+     EADDRINUSE, and a previous interface was an IPv6 wildcard for the same
+     port (which must have succeeded or we wouldn't have got this far), we
+     assume we are in the situation where just a single socket is permitted,
+     and ignore the error.
+
+Phew!
+
+The preparation code decodes options and sets up the relevant data. We do this
+first, so that we can return non-zero if there are any syntax errors, and also
+write to stderr. */
+
+if (daemon_listen && !inetd_wait_mode)
+  {
+  int *default_smtp_port;
+  int sep;
+  int pct = 0;
+  uschar *s;
+  uschar *list;
+  uschar *local_iface_source = US"local_interfaces";
+  ip_address_item *ipa;
+  ip_address_item **pipa;
+
+  /* If -oX was used, disable the writing of a pid file unless -oP was
+  explicitly used to force it. Then scan the string given to -oX. Any items
+  that contain neither a dot nor a colon are used to override daemon_smtp_port.
+  Any other items are used to override local_interfaces. */
+
+  if (override_local_interfaces != NULL)
+    {
+    uschar *new_smtp_port = NULL;
+    uschar *new_local_interfaces = NULL;
+    int portsize = 0;
+    int portptr = 0;
+    int ifacesize = 0;
+    int ifaceptr = 0;
+
+    if (override_pid_file_path == NULL) write_pid = FALSE;
+
+    list = override_local_interfaces;
+    sep = 0;
+    while ((s = string_nextinlist(&list,&sep,big_buffer,big_buffer_size))
+           != NULL)
+      {
+      uschar joinstr[4];
+      uschar **ptr;
+      int *sizeptr;
+      int *ptrptr;
+
+      if (Ustrpbrk(s, ".:") == NULL)
+        {
+        ptr = &new_smtp_port;
+        sizeptr = &portsize;
+        ptrptr = &portptr;
+        }
+      else
+        {
+        ptr = &new_local_interfaces;
+        sizeptr = &ifacesize;
+        ptrptr = &ifaceptr;
+        }
+
+      if (*ptr == NULL)
+        {
+        joinstr[0] = sep;
+        joinstr[1] = ' ';
+        *ptr = string_cat(*ptr, sizeptr, ptrptr, US"<", 1);
+        }
+
+      *ptr = string_cat(*ptr, sizeptr, ptrptr, joinstr, 2);
+      *ptr = string_cat(*ptr, sizeptr, ptrptr, s, Ustrlen(s));
+      }
+
+    if (new_smtp_port != NULL)
+      {
+      new_smtp_port[portptr] = 0;
+      daemon_smtp_port = new_smtp_port;
+      DEBUG(D_any) debug_printf("daemon_smtp_port overridden by -oX:\n  %s\n",
+        daemon_smtp_port);
+      }
+
+    if (new_local_interfaces != NULL)
+      {
+      new_local_interfaces[ifaceptr] = 0;
+      local_interfaces = new_local_interfaces;
+      local_iface_source = US"-oX data";
+      DEBUG(D_any) debug_printf("local_interfaces overridden by -oX:\n  %s\n",
+        local_interfaces);
+      }
+    }
+
+  /* Create a list of default SMTP ports, to be used if local_interfaces
+  contains entries without explict ports. First count the number of ports, then
+  build a translated list in a vector. */
+
+  list = daemon_smtp_port;
+  sep = 0;
+  while ((s = string_nextinlist(&list,&sep,big_buffer,big_buffer_size)))
+    pct++;
+  default_smtp_port = store_get((pct+1) * sizeof(int));
+  list = daemon_smtp_port;
+  sep = 0;
+  for (pct = 0;
+       (s = string_nextinlist(&list,&sep,big_buffer,big_buffer_size));
+       pct++)
+    {
+    if (isdigit(*s))
+      {
+      uschar *end;
+      default_smtp_port[pct] = Ustrtol(s, &end, 0);
+      if (end != s + Ustrlen(s))
+        log_write(0, LOG_PANIC_DIE|LOG_CONFIG, "invalid SMTP port: %s", s);
+      }
+    else
+      {
+      struct servent *smtp_service = getservbyname(CS s, "tcp");
+      if (!smtp_service)
+        log_write(0, LOG_PANIC_DIE|LOG_CONFIG, "TCP port \"%s\" not found", s);
+      default_smtp_port[pct] = ntohs(smtp_service->s_port);
+      }
+    }
+  default_smtp_port[pct] = 0;
+
+  /* Check the list of TLS-on-connect ports and do name lookups if needed */
+
+  list = tls_in.on_connect_ports;
+  sep = 0;
+  while ((s = string_nextinlist(&list, &sep, big_buffer, big_buffer_size)))
+    if (!isdigit(*s))
+      {
+      list = tls_in.on_connect_ports;
+      tls_in.on_connect_ports = NULL;
+      sep = 0;
+      while ((s = string_nextinlist(&list, &sep, big_buffer, big_buffer_size)))
+       {
+        if (!isdigit(*s))
+         {
+         struct servent *smtp_service = getservbyname(CS s, "tcp");
+         if (!smtp_service)
+           log_write(0, LOG_PANIC_DIE|LOG_CONFIG, "TCP port \"%s\" not found", s);
+         s= string_sprintf("%d", (int)ntohs(smtp_service->s_port));
+         }
+       tls_in.on_connect_ports = string_append_listele(tls_in.on_connect_ports,
+           ':', s);
+       }
+      break;
+      }
+
+  /* Create the list of local interfaces, possibly with ports included. This
+  list may contain references to 0.0.0.0 and ::0 as wildcards. These special
+  values are converted below. */
+
+  addresses = host_build_ifacelist(local_interfaces, local_iface_source);
+
+  /* In the list of IP addresses, convert 0.0.0.0 into an empty string, and ::0
+  into the string ":". We use these to recognize wildcards in IPv4 and IPv6. In
+  fact, many IP stacks recognize 0.0.0.0 and ::0 and handle them as wildcards
+  anyway, but we need to know which are the wildcard addresses, and the shorter
+  strings are neater.
+
+  In the same scan, fill in missing port numbers from the default list. When
+  there is more than one item in the list, extra items are created. */
+
+  for (ipa = addresses; ipa != NULL; ipa = ipa->next)
+    {
+    int i;
+
+    if (Ustrcmp(ipa->address, "0.0.0.0") == 0) ipa->address[0] = 0;
+    else if (Ustrcmp(ipa->address, "::0") == 0)
+      {
+      ipa->address[0] = ':';
+      ipa->address[1] = 0;
+      }
+
+    if (ipa->port > 0) continue;
+
+    if (daemon_smtp_port[0] <= 0)
+      log_write(0, LOG_MAIN|LOG_PANIC_DIE, "no port specified for interface "
+        "%s and daemon_smtp_port is unset; cannot start daemon",
+        (ipa->address[0] == 0)? US"\"all IPv4\"" :
+        (ipa->address[1] == 0)? US"\"all IPv6\"" : ipa->address);
+    ipa->port = default_smtp_port[0];
+    for (i = 1; default_smtp_port[i] > 0; i++)
+      {
+      ip_address_item *new = store_get(sizeof(ip_address_item));
+      memcpy(new->address, ipa->address, Ustrlen(ipa->address) + 1);
+      new->port = default_smtp_port[i];
+      new->next = ipa->next;
+      ipa->next = new;
+      ipa = new;
+      }
+    }
+
+  /* Scan the list of addresses for wildcards. If we find an IPv4 and an IPv6
+  wildcard for the same port, ensure that (a) they are together and (b) the
+  IPv6 address comes first. This makes handling the messy features easier, and
+  also simplifies the construction of the "daemon started" log line. */
+
+  pipa = &addresses;
+  for (ipa = addresses; ipa != NULL; pipa = &(ipa->next), ipa = ipa->next)
+    {
+    ip_address_item *ipa2;
+
+    /* Handle an IPv4 wildcard */
+
+    if (ipa->address[0] == 0)
+      {
+      for (ipa2 = ipa; ipa2->next != NULL; ipa2 = ipa2->next)
+        {
+        ip_address_item *ipa3 = ipa2->next;
+        if (ipa3->address[0] == ':' &&
+            ipa3->address[1] == 0 &&
+            ipa3->port == ipa->port)
+          {
+          ipa2->next = ipa3->next;
+          ipa3->next = ipa;
+          *pipa = ipa3;
+          break;
+          }
+        }
+      }
+
+    /* Handle an IPv6 wildcard. */
+
+    else if (ipa->address[0] == ':' && ipa->address[1] == 0)
+      {
+      for (ipa2 = ipa; ipa2->next != NULL; ipa2 = ipa2->next)
+        {
+        ip_address_item *ipa3 = ipa2->next;
+        if (ipa3->address[0] == 0 && ipa3->port == ipa->port)
+          {
+          ipa2->next = ipa3->next;
+          ipa3->next = ipa->next;
+          ipa->next = ipa3;
+          ipa = ipa3;
+          break;
+          }
+        }
+      }
+    }
+
+  /* Get a vector to remember all the sockets in */
+
+  for (ipa = addresses; ipa != NULL; ipa = ipa->next)
+    listen_socket_count++;
+  listen_sockets = store_get(sizeof(int *) * listen_socket_count);
+
+  } /* daemon_listen but not inetd_wait_mode */
+
+if (daemon_listen)
+  {
+
+  /* Do a sanity check on the max connects value just to save us from getting
+  a huge amount of store. */
+
+  if (smtp_accept_max > 4095) smtp_accept_max = 4096;
+
+  /* There's no point setting smtp_accept_queue unless it is less than the max
+  connects limit. The configuration reader ensures that the max is set if the
+  queue-only option is set. */
+
+  if (smtp_accept_queue > smtp_accept_max) smtp_accept_queue = 0;
+
+  /* Get somewhere to keep the list of SMTP accepting pids if we are keeping
+  track of them for total number and queue/host limits. */
+
+  if (smtp_accept_max > 0)
+    {
+    int i;
+    smtp_slots = store_get(smtp_accept_max * sizeof(smtp_slot));
+    for (i = 0; i < smtp_accept_max; i++) smtp_slots[i] = empty_smtp_slot;
+    }
+  }
+
+/* The variable background_daemon is always false when debugging, but
+can also be forced false in order to keep a non-debugging daemon in the
+foreground. If background_daemon is true, close all open file descriptors that
+we know about, but then re-open stdin, stdout, and stderr to /dev/null.  Also
+do this for inetd_wait mode.
+
+This is protection against any called functions (in libraries, or in
+Perl, or whatever) that think they can write to stderr (or stdout). Before this
+was added, it was quite likely that an SMTP connection would use one of these
+file descriptors, in which case writing random stuff to it caused chaos.
+
+Then disconnect from the controlling terminal, Most modern Unixes seem to have
+setsid() for getting rid of the controlling terminal. For any OS that doesn't,
+setsid() can be #defined as a no-op, or as something else. */
+
+if (background_daemon || inetd_wait_mode)
+  {
+  log_close_all();    /* Just in case anything was logged earlier */
+  search_tidyup();    /* Just in case any were used in reading the config. */
+  (void)close(0);           /* Get rid of stdin/stdout/stderr */
+  (void)close(1);
+  (void)close(2);
+  exim_nullstd();     /* Connect stdin/stdout/stderr to /dev/null */
+  log_stderr = NULL;  /* So no attempt to copy paniclog output */
+  }
+
+if (background_daemon)
+  {
+  /* If the parent process of this one has pid == 1, we are re-initializing the
+  daemon as the result of a SIGHUP. In this case, there is no need to do
+  anything, because the controlling terminal has long gone. Otherwise, fork, in
+  case current process is a process group leader (see 'man setsid' for an
+  explanation) before calling setsid(). */
+
+  if (getppid() != 1)
+    {
+    pid_t pid = fork();
+    if (pid < 0) log_write(0, LOG_MAIN|LOG_PANIC_DIE,
+      "fork() failed when starting daemon: %s", strerror(errno));
+    if (pid > 0) exit(EXIT_SUCCESS);      /* in parent process, just exit */
+    (void)setsid();                       /* release controlling terminal */
+    }
+  }
+
+/* We are now in the disconnected, daemon process (unless debugging). Set up
+the listening sockets if required. */
+
+if (daemon_listen && !inetd_wait_mode)
+  {
+  int sk;
+  int on = 1;
+  ip_address_item *ipa;
+
+  /* For each IP address, create a socket, bind it to the appropriate port, and
+  start listening. See comments above about IPv6 sockets that may or may not
+  accept IPv4 calls when listening on all interfaces. We also have to cope with
+  the case of a system with IPv6 libraries, but no IPv6 support in the kernel.
+  listening, provided a wildcard IPv4 socket for the same port follows. */
+
+  for (ipa = addresses, sk = 0; sk < listen_socket_count; ipa = ipa->next, sk++)
+    {
+    BOOL wildcard;
+    ip_address_item *ipa2;
+    int af;
+
+    if (Ustrchr(ipa->address, ':') != NULL)
+      {
+      af = AF_INET6;
+      wildcard = ipa->address[1] == 0;
+      }
+    else
+      {
+      af = AF_INET;
+      wildcard = ipa->address[0] == 0;
+      }
+
+    listen_sockets[sk] = ip_socket(SOCK_STREAM, af);
+    if (listen_sockets[sk] < 0)
+      {
+      if (check_special_case(0, addresses, ipa, FALSE))
+        {
+        log_write(0, LOG_MAIN, "Failed to create IPv6 socket for wildcard "
+          "listening (%s): will use IPv4", strerror(errno));
+        goto SKIP_SOCKET;
+        }
+      log_write(0, LOG_PANIC_DIE, "IPv%c socket creation failed: %s",
+        (af == AF_INET6)? '6' : '4', strerror(errno));
+      }
+
+    /* If this is an IPv6 wildcard socket, set IPV6_V6ONLY if that option is
+    available. Just log failure (can get protocol not available, just like
+    socket creation can). */
+
+    #ifdef IPV6_V6ONLY
+    if (af == AF_INET6 && wildcard &&
+        setsockopt(listen_sockets[sk], IPPROTO_IPV6, IPV6_V6ONLY, (char *)(&on),
+          sizeof(on)) < 0)
+      log_write(0, LOG_MAIN, "Setting IPV6_V6ONLY on daemon's IPv6 wildcard "
+        "socket failed (%s): carrying on without it", strerror(errno));
+    #endif  /* IPV6_V6ONLY */
+
+    /* Set SO_REUSEADDR so that the daemon can be restarted while a connection
+    is being handled.  Without this, a connection will prevent reuse of the
+    smtp port for listening. */
+
+    if (setsockopt(listen_sockets[sk], SOL_SOCKET, SO_REUSEADDR,
+                   (uschar *)(&on), sizeof(on)) < 0)
+      log_write(0, LOG_MAIN|LOG_PANIC_DIE, "setting SO_REUSEADDR on socket "
+        "failed when starting daemon: %s", strerror(errno));
+
+    /* Set TCP_NODELAY; Exim does its own buffering. There is a switch to
+    disable this because it breaks some broken clients. */
+
+    if (tcp_nodelay) setsockopt(listen_sockets[sk], IPPROTO_TCP, TCP_NODELAY,
+      (uschar *)(&on), sizeof(on));
+
+    /* Now bind the socket to the required port; if Exim is being restarted
+    it may not always be possible to bind immediately, even with SO_REUSEADDR
+    set, so try 10 times, waiting between each try. After 10 failures, we give
+    up. In an IPv6 environment, if bind () fails with the error EADDRINUSE and
+    we are doing wildcard IPv4 listening and there was a previous IPv6 wildcard
+    address for the same port, ignore the error on the grounds that we must be
+    in a system where the IPv6 socket accepts both kinds of call. This is
+    necessary for (some release of) USAGI Linux; other IP stacks fail at the
+    listen() stage instead. */
+
+    for(;;)
+      {
+      uschar *msg, *addr;
+      if (ip_bind(listen_sockets[sk], af, ipa->address, ipa->port) >= 0) break;
+      if (check_special_case(errno, addresses, ipa, TRUE))
+        {
+        DEBUG(D_any) debug_printf("wildcard IPv4 bind() failed after IPv6 "
+          "listen() success; EADDRINUSE ignored\n");
+        (void)close(listen_sockets[sk]);
+        goto SKIP_SOCKET;
+        }
+      msg = US strerror(errno);
+      addr = wildcard? ((af == AF_INET6)? US"(any IPv6)" : US"(any IPv4)") :
+        ipa->address;
+      if (daemon_startup_retries <= 0)
+        log_write(0, LOG_MAIN|LOG_PANIC_DIE,
+          "socket bind() to port %d for address %s failed: %s: "
+          "daemon abandoned", ipa->port, addr, msg);
+      log_write(0, LOG_MAIN, "socket bind() to port %d for address %s "
+        "failed: %s: waiting %s before trying again (%d more %s)",
+        ipa->port, addr, msg, readconf_printtime(daemon_startup_sleep),
+        daemon_startup_retries, (daemon_startup_retries > 1)? "tries" : "try");
+      daemon_startup_retries--;
+      sleep(daemon_startup_sleep);
+      }
+
+    DEBUG(D_any)
+      {
+      if (wildcard)
+        debug_printf("listening on all interfaces (IPv%c) port %d\n",
+          (af == AF_INET6)? '6' : '4', ipa->port);
+      else
+        debug_printf("listening on %s port %d\n", ipa->address, ipa->port);
+      }
+
+    /* Start listening on the bound socket, establishing the maximum backlog of
+    connections that is allowed. On success, continue to the next address. */
+
+    if (listen(listen_sockets[sk], smtp_connect_backlog) >= 0) continue;
+
+    /* Listening has failed. In an IPv6 environment, as for bind(), if listen()
+    fails with the error EADDRINUSE and we are doing IPv4 wildcard listening
+    and there was a previous successful IPv6 wildcard listen on the same port,
+    we want to ignore the error on the grounds that we must be in a system
+    where the IPv6 socket accepts both kinds of call. */
+
+    if (!check_special_case(errno, addresses, ipa, TRUE))
+      log_write(0, LOG_PANIC_DIE, "listen() failed on interface %s: %s",
+        wildcard? ((af == AF_INET6)? US"(any IPv6)" : US"(any IPv4)") :
+        ipa->address,
+        strerror(errno));
+
+    DEBUG(D_any) debug_printf("wildcard IPv4 listen() failed after IPv6 "
+      "listen() success; EADDRINUSE ignored\n");
+    (void)close(listen_sockets[sk]);
+
+    /* Come here if there has been a problem with the socket which we
+    are going to ignore. We remove the address from the chain, and back up the
+    counts. */
+
+    SKIP_SOCKET:
+    sk--;                          /* Back up the count */
+    listen_socket_count--;         /* Reduce the total */
+    if (ipa == addresses) addresses = ipa->next; else
+      {
+      for (ipa2 = addresses; ipa2->next != ipa; ipa2 = ipa2->next);
+      ipa2->next = ipa->next;
+      ipa = ipa2;
+      }
+    }          /* End of bind/listen loop for each address */
+  }            /* End of setup for listening */
+
+
+/* If we are not listening, we want to write a pid file only if -oP was
+explicitly given. */
+
+else if (override_pid_file_path == NULL) write_pid = FALSE;
+
+/* Write the pid to a known file for assistance in identification, if required.
+We do this before giving up root privilege, because on some systems it is
+necessary to be root in order to write into the pid file directory. There's
+nothing to stop multiple daemons running, as long as no more than one listens
+on a given TCP/IP port on the same interface(s). However, in these
+circumstances it gets far too complicated to mess with pid file names
+automatically. Consequently, Exim 4 writes a pid file only
+
+  (a) When running in the test harness, or
+  (b) When -bd is used and -oX is not used, or
+  (c) When -oP is used to supply a path.
+
+The variable daemon_write_pid is used to control this. */
+
+if (running_in_test_harness || write_pid)
+  {
+  FILE *f;
+
+  if (override_pid_file_path != NULL)
+    pid_file_path = override_pid_file_path;
+
+  if (pid_file_path[0] == 0)
+    pid_file_path = string_sprintf("%s/exim-daemon.pid", spool_directory);
+
+  f = modefopen(pid_file_path, "wb", 0644);
+  if (f != NULL)
+    {
+    (void)fprintf(f, "%d\n", (int)getpid());
+    (void)fclose(f);
+    DEBUG(D_any) debug_printf("pid written to %s\n", pid_file_path);
+    }
+  else
+    {
+    DEBUG(D_any)
+      debug_printf("%s\n", string_open_failed(errno, "pid file %s",
+        pid_file_path));
+    }
+  }
+
+/* Set up the handler for SIGHUP, which causes a restart of the daemon. */
+
+sighup_seen = FALSE;
+signal(SIGHUP, sighup_handler);
+
+/* Give up root privilege at this point (assuming that exim_uid and exim_gid
+are not root). The third argument controls the running of initgroups().
+Normally we do this, in order to set up the groups for the Exim user. However,
+if we are not root at this time - some odd installations run that way - we
+cannot do this. */
+
+exim_setugid(exim_uid, exim_gid, geteuid()==root_uid, US"running as a daemon");
+
+/* Update the originator_xxx fields so that received messages as listed as
+coming from Exim, not whoever started the daemon. */
+
+originator_uid = exim_uid;
+originator_gid = exim_gid;
+originator_login = ((pw = getpwuid(exim_uid)) != NULL)?
+  string_copy_malloc(US pw->pw_name) : US"exim";
+
+/* Get somewhere to keep the list of queue-runner pids if we are keeping track
+of them (and also if we are doing queue runs). */
+
+if (queue_interval > 0 && queue_run_max > 0)
+  {
+  int i;
+  queue_pid_slots = store_get(queue_run_max * sizeof(pid_t));
+  for (i = 0; i < queue_run_max; i++) queue_pid_slots[i] = 0;
+  }
+
+/* Set up the handler for termination of child processes. */
+
+sigchld_seen = FALSE;
+os_non_restarting_signal(SIGCHLD, main_sigchld_handler);
+
+/* If we are to run the queue periodically, pretend the alarm has just gone
+off. This will cause the first queue-runner to get kicked off straight away. */
+
+sigalrm_seen = (queue_interval > 0);
+
+/* Log the start up of a daemon - at least one of listening or queue running
+must be set up. */
+
+if (inetd_wait_mode)
+  {
+  uschar *p = big_buffer;
+
+  if (inetd_wait_timeout >= 0)
+    sprintf(CS p, "terminating after %d seconds", inetd_wait_timeout);
+  else
+    sprintf(CS p, "with no wait timeout");
+
+  log_write(0, LOG_MAIN,
+    "exim %s daemon started: pid=%d, launched with listening socket, %s",
+    version_string, getpid(), big_buffer);
+  set_process_info("daemon: pre-listening socket");
+
+  /* set up the timeout logic */
+  sigalrm_seen = 1;
+  }
+
+else if (daemon_listen)
+  {
+  int i, j;
+  int smtp_ports = 0;
+  int smtps_ports = 0;
+  ip_address_item *ipa;
+  uschar *p = big_buffer;
+  uschar *qinfo = (queue_interval > 0)?
+    string_sprintf("-q%s", readconf_printtime(queue_interval))
+    :
+    US"no queue runs";
+
+  /* Build a list of listening addresses in big_buffer, but limit it to 10
+  items. The style is for backwards compatibility.
+
+  It is now possible to have some ports listening for SMTPS (the old,
+  deprecated protocol that starts TLS without using STARTTLS), and others
+  listening for standard SMTP. Keep their listings separate. */
+
+  for (j = 0; j < 2; j++)
+    {
+    for (i = 0, ipa = addresses; i < 10 && ipa != NULL; i++, ipa = ipa->next)
+       {
+       /* First time round, look for SMTP ports; second time round, look for
+       SMTPS ports. For the first one of each, insert leading text. */
+
+       if (host_is_tls_on_connect_port(ipa->port) == (j > 0))
+         {
+         if (j == 0)
+           {
+           if (smtp_ports++ == 0)
+             {
+             memcpy(p, "SMTP on", 8);
+             p += 7;
+             }
+           }
+         else
+           {
+           if (smtps_ports++ == 0)
+             {
+             (void)sprintf(CS p, "%sSMTPS on",
+               (smtp_ports == 0)? "":" and for ");
+             while (*p != 0) p++;
+             }
+           }
+
+         /* Now the information about the port (and sometimes interface) */
+
+         if (ipa->address[0] == ':' && ipa->address[1] == 0)
+           {
+           if (ipa->next != NULL && ipa->next->address[0] == 0 &&
+               ipa->next->port == ipa->port)
+             {
+             (void)sprintf(CS p, " port %d (IPv6 and IPv4)", ipa->port);
+             ipa = ipa->next;
+             }
+           else if (ipa->v6_include_v4)
+             (void)sprintf(CS p, " port %d (IPv6 with IPv4)", ipa->port);
+           else
+             (void)sprintf(CS p, " port %d (IPv6)", ipa->port);
+           }
+         else if (ipa->address[0] == 0)
+           (void)sprintf(CS p, " port %d (IPv4)", ipa->port);
+         else
+           (void)sprintf(CS p, " [%s]:%d", ipa->address, ipa->port);
+         while (*p != 0) p++;
+         }
+       }
+
+    if (ipa != NULL)
+      {
+      memcpy(p, " ...", 5);
+      p += 4;
+      }
+    }
+
+  log_write(0, LOG_MAIN,
+    "exim %s daemon started: pid=%d, %s, listening for %s",
+    version_string, getpid(), qinfo, big_buffer);
+  set_process_info("daemon: %s, listening for %s", qinfo, big_buffer);
+  }
+
+else
+  {
+  log_write(0, LOG_MAIN,
+    "exim %s daemon started: pid=%d, -q%s, not listening for SMTP",
+    version_string, getpid(), readconf_printtime(queue_interval));
+  set_process_info("daemon: -q%s, not listening",
+    readconf_printtime(queue_interval));
+  }
+
+
+/* Close the log so it can be renamed and moved. In the few cases below where
+this long-running process writes to the log (always exceptional conditions), it
+closes the log afterwards, for the same reason. */
+
+log_close_all();
+
+DEBUG(D_any) debug_print_ids(US"daemon running with");
+
+/* Any messages accepted via this route are going to be SMTP. */
+
+smtp_input = TRUE;
+
+/* Enter the never-ending loop... */
+
+for (;;)
+  {
+  #if HAVE_IPV6
+  struct sockaddr_in6 accepted;
+  #else
+  struct sockaddr_in accepted;
+  #endif
+
+  EXIM_SOCKLEN_T len;
+  pid_t pid;
+
+  /* This code is placed first in the loop, so that it gets obeyed at the
+  start, before the first wait, for the queue-runner case, so that the first
+  one can be started immediately.
+
+  The other option is that we have an inetd wait timeout specified to -bw. */
+
+  if (sigalrm_seen)
+    {
+    if (inetd_wait_timeout > 0)
+      {
+      time_t resignal_interval = inetd_wait_timeout;
+
+      if (last_connection_time == (time_t)0)
+        {
+        DEBUG(D_any)
+          debug_printf("inetd wait timeout expired, but still not seen first message, ignoring\n");
+        }
+      else
+        {
+        time_t now = time(NULL);
+        if (now == (time_t)-1)
+          {
+          DEBUG(D_any) debug_printf("failed to get time: %s\n", strerror(errno));
+          }
+        else
+          {
+          if ((now - last_connection_time) >= inetd_wait_timeout)
+            {
+            DEBUG(D_any)
+              debug_printf("inetd wait timeout %d expired, ending daemon\n",
+                  inetd_wait_timeout);
+            log_write(0, LOG_MAIN, "exim %s daemon terminating, inetd wait timeout reached.\n",
+                version_string);
+            exit(EXIT_SUCCESS);
+            }
+          else
+            {
+            resignal_interval -= (now - last_connection_time);
+            }
+          }
+        }
+
+      sigalrm_seen = FALSE;
+      alarm(resignal_interval);
+      }
+
+    else
+      {
+      DEBUG(D_any) debug_printf("SIGALRM received\n");
+
+      /* Do a full queue run in a child process, if required, unless we already
+      have enough queue runners on the go. If we are not running as root, a
+      re-exec is required. */
+
+      if (queue_interval > 0 &&
+         (queue_run_max <= 0 || queue_run_count < queue_run_max))
+        {
+        if ((pid = fork()) == 0)
+          {
+          int sk;
+
+          DEBUG(D_any) debug_printf("Starting queue-runner: pid %d\n",
+            (int)getpid());
+
+          /* Disable debugging if it's required only for the daemon process. We
+          leave the above message, because it ties up with the "child ended"
+          debugging messages. */
+
+          if (debug_daemon) debug_selector = 0;
+
+          /* Close any open listening sockets in the child */
+
+          for (sk = 0; sk < listen_socket_count; sk++)
+            (void)close(listen_sockets[sk]);
+
+          /* Reset SIGHUP and SIGCHLD in the child in both cases. */
+
+          signal(SIGHUP,  SIG_DFL);
+          signal(SIGCHLD, SIG_DFL);
+
+          /* Re-exec if privilege has been given up, unless deliver_drop_
+          privilege is set. Reset SIGALRM before exec(). */
+
+          if (geteuid() != root_uid && !deliver_drop_privilege)
+            {
+            uschar opt[8];
+            uschar *p = opt;
+            uschar *extra[5];
+            int extracount = 1;
+
+            signal(SIGALRM, SIG_DFL);
+            *p++ = '-';
+            *p++ = 'q';
+            if (queue_2stage) *p++ = 'q';
+            if (queue_run_first_delivery) *p++ = 'i';
+            if (queue_run_force) *p++ = 'f';
+            if (deliver_force_thaw) *p++ = 'f';
+            if (queue_run_local) *p++ = 'l';
+            *p = 0;
+            extra[0] = opt;
+
+            /* If -R or -S were on the original command line, ensure they get
+            passed on. */
+
+            if (deliver_selectstring != NULL)
+              {
+              extra[extracount++] = deliver_selectstring_regex? US"-Rr" : US"-R";
+              extra[extracount++] = deliver_selectstring;
+              }
+
+            if (deliver_selectstring_sender != NULL)
+              {
+              extra[extracount++] = deliver_selectstring_sender_regex?
+                US"-Sr" : US"-S";
+              extra[extracount++] = deliver_selectstring_sender;
+              }
+
+            /* Overlay this process with a new execution. */
+
+            (void)child_exec_exim(CEE_EXEC_PANIC, FALSE, NULL, TRUE, extracount,
+              extra[0], extra[1], extra[2], extra[3], extra[4]);
+
+            /* Control never returns here. */
+            }
+
+          /* No need to re-exec; SIGALRM remains set to the default handler */
+
+          queue_run(NULL, NULL, FALSE);
+          _exit(EXIT_SUCCESS);
+          }
+
+        if (pid < 0)
+          {
+          log_write(0, LOG_MAIN|LOG_PANIC, "daemon: fork of queue-runner "
+            "process failed: %s", strerror(errno));
+          log_close_all();
+          }
+        else
+          {
+          int i;
+          for (i = 0; i < queue_run_max; ++i)
+            {
+            if (queue_pid_slots[i] <= 0)
+              {
+              queue_pid_slots[i] = pid;
+              queue_run_count++;
+              break;
+              }
+            }
+          DEBUG(D_any) debug_printf("%d queue-runner process%s running\n",
+            queue_run_count, (queue_run_count == 1)? "" : "es");
+          }
+        }
+
+      /* Reset the alarm clock */
+
+      sigalrm_seen = FALSE;
+      alarm(queue_interval);
+      }
+
+    } /* sigalrm_seen */
+
+
+  /* Sleep till a connection happens if listening, and handle the connection if
+  that is why we woke up. The FreeBSD operating system requires the use of
+  select() before accept() because the latter function is not interrupted by
+  a signal, and we want to wake up for SIGCHLD and SIGALRM signals. Some other
+  OS do notice signals in accept() but it does no harm to have the select()
+  in for all of them - and it won't then be a lurking problem for ports to
+  new OS. In fact, the later addition of listening on specific interfaces only
+  requires this way of working anyway. */
+
+  if (daemon_listen)
+    {
+    int sk, lcount, select_errno;
+    int max_socket = 0;
+    BOOL select_failed = FALSE;
+    fd_set select_listen;
+
+    FD_ZERO(&select_listen);
+    for (sk = 0; sk < listen_socket_count; sk++)
+      {
+      FD_SET(listen_sockets[sk], &select_listen);
+      if (listen_sockets[sk] > max_socket) max_socket = listen_sockets[sk];
+      }
+
+    DEBUG(D_any) debug_printf("Listening...\n");
+
+    /* In rare cases we may have had a SIGCHLD signal in the time between
+    setting the handler (below) and getting back here. If so, pretend that the
+    select() was interrupted so that we reap the child. This might still leave
+    a small window when a SIGCHLD could get lost. However, since we use SIGCHLD
+    only to do the reaping more quickly, it shouldn't result in anything other
+    than a delay until something else causes a wake-up. */
+
+    if (sigchld_seen)
+      {
+      lcount = -1;
+      errno = EINTR;
+      }
+    else
+      {
+      lcount = select(max_socket + 1, (SELECT_ARG2_TYPE *)&select_listen,
+        NULL, NULL, NULL);
+      }
+
+    if (lcount < 0)
+      {
+      select_failed = TRUE;
+      lcount = 1;
+      }
+
+    /* Clean up any subprocesses that may have terminated. We need to do this
+    here so that smtp_accept_max_per_host works when a connection to that host
+    has completed, and we are about to accept a new one. When this code was
+    later in the sequence, a new connection could be rejected, even though an
+    old one had just finished. Preserve the errno from any select() failure for
+    the use of the common select/accept error processing below. */
+
+    select_errno = errno;
+    handle_ending_processes();
+    errno = select_errno;
+
+    /* Loop for all the sockets that are currently ready to go. If select
+    actually failed, we have set the count to 1 and select_failed=TRUE, so as
+    to use the common error code for select/accept below. */
+
+    while (lcount-- > 0)
+      {
+      int accept_socket = -1;
+      if (!select_failed)
+        {
+        for (sk = 0; sk < listen_socket_count; sk++)
+          {
+          if (FD_ISSET(listen_sockets[sk], &select_listen))
+            {
+            len = sizeof(accepted);
+            accept_socket = accept(listen_sockets[sk],
+              (struct sockaddr *)&accepted, &len);
+            FD_CLR(listen_sockets[sk], &select_listen);
+            break;
+            }
+          }
+        }
+
+      /* If select or accept has failed and this was not caused by an
+      interruption, log the incident and try again. With asymmetric TCP/IP
+      routing errors such as "No route to network" have been seen here. Also
+      "connection reset by peer" has been seen. These cannot be classed as
+      disastrous errors, but they could fill up a lot of log. The code in smail
+      crashes the daemon after 10 successive failures of accept, on the grounds
+      that some OS fail continuously. Exim originally followed suit, but this
+      appears to have caused problems. Now it just keeps going, but instead of
+      logging each error, it batches them up when they are continuous. */
+
+      if (accept_socket < 0 && errno != EINTR)
+        {
+        if (accept_retry_count == 0)
+          {
+          accept_retry_errno = errno;
+          accept_retry_select_failed = select_failed;
+          }
+        else
+          {
+          if (errno != accept_retry_errno ||
+              select_failed != accept_retry_select_failed ||
+              accept_retry_count >= 50)
+            {
+            log_write(0, LOG_MAIN | ((accept_retry_count >= 50)? LOG_PANIC : 0),
+              "%d %s() failure%s: %s",
+              accept_retry_count,
+              accept_retry_select_failed? "select" : "accept",
+              (accept_retry_count == 1)? "" : "s",
+              strerror(accept_retry_errno));
+            log_close_all();
+            accept_retry_count = 0;
+            accept_retry_errno = errno;
+            accept_retry_select_failed = select_failed;
+            }
+          }
+        accept_retry_count++;
+        }
+
+      else
+        {
+        if (accept_retry_count > 0)
+          {
+          log_write(0, LOG_MAIN, "%d %s() failure%s: %s",
+            accept_retry_count,
+            accept_retry_select_failed? "select" : "accept",
+            (accept_retry_count == 1)? "" : "s",
+            strerror(accept_retry_errno));
+          log_close_all();
+          accept_retry_count = 0;
+          }
+        }
+
+      /* If select/accept succeeded, deal with the connection. */
+
+      if (accept_socket >= 0)
+        {
+        if (inetd_wait_timeout)
+          last_connection_time = time(NULL);
+        handle_smtp_call(listen_sockets, listen_socket_count, accept_socket,
+          (struct sockaddr *)&accepted);
+        }
+      }
+    }
+
+  /* If not listening, then just sleep for the queue interval. If we woke
+  up early the last time for some other signal, it won't matter because
+  the alarm signal will wake at the right time. This code originally used
+  sleep() but it turns out that on the FreeBSD system, sleep() is not inter-
+  rupted by signals, so it wasn't waking up for SIGALRM or SIGCHLD. Luckily
+  select() can be used as an interruptible sleep() on all versions of Unix. */
+
+  else
+    {
+    struct timeval tv;
+    tv.tv_sec = queue_interval;
+    tv.tv_usec = 0;
+    select(0, NULL, NULL, NULL, &tv);
+    handle_ending_processes();
+    }
+
+  /* Re-enable the SIGCHLD handler if it has been run. It can't do it
+  for itself, because it isn't doing the waiting itself. */
+
+  if (sigchld_seen)
+    {
+    sigchld_seen = FALSE;
+    os_non_restarting_signal(SIGCHLD, main_sigchld_handler);
+    }
+
+  /* Handle being woken by SIGHUP. We know at this point that the result
+  of accept() has been dealt with, so we can re-exec exim safely, first
+  closing the listening sockets so that they can be reused. Cancel any pending
+  alarm in case it is just about to go off, and set SIGHUP to be ignored so
+  that another HUP in quick succession doesn't clobber the new daemon before it
+  gets going. All log files get closed by the close-on-exec flag; however, if
+  the exec fails, we need to close the logs. */
+
+  if (sighup_seen)
+    {
+    int sk;
+    log_write(0, LOG_MAIN, "pid %d: SIGHUP received: re-exec daemon",
+      getpid());
+    for (sk = 0; sk < listen_socket_count; sk++)
+      (void)close(listen_sockets[sk]);
+    alarm(0);
+    signal(SIGHUP, SIG_IGN);
+    sighup_argv[0] = exim_path;
+    exim_nullstd();
+    execv(CS exim_path, (char *const *)sighup_argv);
+    log_write(0, LOG_MAIN|LOG_PANIC_DIE, "pid %d: exec of %s failed: %s",
+      getpid(), exim_path, strerror(errno));
+    log_close_all();
+    }
+
+  }   /* End of main loop */
+
+/* Control never reaches here */
+}
+
+/* vi: aw ai sw=2
+*/
+/* End of exim_daemon.c */
diff --git a/src/dbfn.c b/src/dbfn.c
new file mode 100644 (file)
index 0000000..4a1c20f
--- /dev/null
@@ -0,0 +1,691 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+
+#include "exim.h"
+
+
+/* Functions for accessing Exim's hints database, which consists of a number of
+different DBM files. This module does not contain code for reading DBM files
+for (e.g.) alias expansion. That is all contained within the general search
+functions. As Exim now has support for several DBM interfaces, all the relevant
+functions are called as macros.
+
+All the data in Exim's database is in the nature of *hints*. Therefore it
+doesn't matter if it gets destroyed by accident. These functions are not
+supposed to implement a "safe" database.
+
+Keys are passed in as C strings, and the terminating zero *is* used when
+building the dbm files. This just makes life easier when scanning the files
+sequentially.
+
+Synchronization is required on the database files, and this is achieved by
+means of locking on independent lock files. (Earlier attempts to lock on the
+DBM files themselves were never completely successful.) Since callers may in
+general want to do more than one read or write while holding the lock, there
+are separate open and close functions. However, the calling modules should
+arrange to hold the locks for the bare minimum of time. */
+
+
+
+/*************************************************
+*         Berkeley DB error callback             *
+*************************************************/
+
+/* For Berkeley DB >= 2, we can define a function to be called in case of DB
+errors. This should help with debugging strange DB problems, e.g. getting "File
+exists" when you try to open a db file. The API for this function was changed
+at DB release 4.3. */
+
+#if defined(USE_DB) && defined(DB_VERSION_STRING)
+void
+#if DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 3)
+dbfn_bdb_error_callback(const DB_ENV *dbenv, const char *pfx, const char *msg)
+{
+dbenv = dbenv;
+#else
+dbfn_bdb_error_callback(const char *pfx, char *msg)
+{
+#endif
+pfx = pfx;
+log_write(0, LOG_MAIN, "Berkeley DB error: %s", msg);
+}
+#endif
+
+
+
+
+/*************************************************
+*          Open and lock a database file         *
+*************************************************/
+
+/* Used for accessing Exim's hints databases.
+
+Arguments:
+  name     The single-component name of one of Exim's database files.
+  flags    Either O_RDONLY or O_RDWR, indicating the type of open required;
+             O_RDWR implies "create if necessary"
+  dbblock  Points to an open_db block to be filled in.
+  lof      If TRUE, write to the log for actual open failures (locking failures
+           are always logged).
+
+Returns:   NULL if the open failed, or the locking failed. After locking
+           failures, errno is zero.
+
+           On success, dbblock is returned. This contains the dbm pointer and
+           the fd of the locked lock file.
+
+There are some calls that use O_RDWR|O_CREAT for the flags. Having discovered
+this in December 2005, I'm not sure if this is correct or not, but for the
+moment I haven't changed them.
+*/
+
+open_db *
+dbfn_open(uschar *name, int flags, open_db *dbblock, BOOL lof)
+{
+int rc, save_errno;
+BOOL read_only = flags == O_RDONLY;
+BOOL created = FALSE;
+flock_t lock_data;
+uschar buffer[256];
+
+/* The first thing to do is to open a separate file on which to lock. This
+ensures that Exim has exclusive use of the database before it even tries to
+open it. Early versions tried to lock on the open database itself, but that
+gave rise to mysterious problems from time to time - it was suspected that some
+DB libraries "do things" on their open() calls which break the interlocking.
+The lock file is never written to, but we open it for writing so we can get a
+write lock if required. If it does not exist, we create it. This is done
+separately so we know when we have done it, because when running as root we
+need to change the ownership - see the bottom of this function. We also try to
+make the directory as well, just in case. We won't be doing this many times
+unnecessarily, because usually the lock file will be there. If the directory
+exists, there is no error. */
+
+sprintf(CS buffer, "%s/db/%s.lockfile", spool_directory, name);
+
+if ((dbblock->lockfd = Uopen(buffer, O_RDWR, EXIMDB_LOCKFILE_MODE)) < 0)
+  {
+  created = TRUE;
+  (void)directory_make(spool_directory, US"db", EXIMDB_DIRECTORY_MODE, TRUE);
+  dbblock->lockfd = Uopen(buffer, O_RDWR|O_CREAT, EXIMDB_LOCKFILE_MODE);
+  }
+
+if (dbblock->lockfd < 0)
+  {
+  log_write(0, LOG_MAIN, "%s",
+    string_open_failed(errno, "database lock file %s", buffer));
+  errno = 0;      /* Indicates locking failure */
+  return NULL;
+  }
+
+/* Now we must get a lock on the opened lock file; do this with a blocking
+lock that times out. */
+
+lock_data.l_type = read_only? F_RDLCK : F_WRLCK;
+lock_data.l_whence = lock_data.l_start = lock_data.l_len = 0;
+
+DEBUG(D_hints_lookup|D_retry|D_route|D_deliver)
+  debug_printf("locking %s\n", buffer);
+
+sigalrm_seen = FALSE;
+alarm(EXIMDB_LOCK_TIMEOUT);
+rc = fcntl(dbblock->lockfd, F_SETLKW, &lock_data);
+alarm(0);
+
+if (sigalrm_seen) errno = ETIMEDOUT;
+if (rc < 0)
+  {
+  log_write(0, LOG_MAIN|LOG_PANIC, "Failed to get %s lock for %s: %s",
+    read_only? "read" : "write", buffer,
+    (errno == ETIMEDOUT)? "timed out" : strerror(errno));
+  (void)close(dbblock->lockfd);
+  errno = 0;       /* Indicates locking failure */
+  return NULL;
+  }
+
+DEBUG(D_hints_lookup) debug_printf("locked %s\n", buffer);
+
+/* At this point we have an opened and locked separate lock file, that is,
+exclusive access to the database, so we can go ahead and open it. If we are
+expected to create it, don't do so at first, again so that we can detect
+whether we need to change its ownership (see comments about the lock file
+above.) There have been regular reports of crashes while opening hints
+databases - often this is caused by non-matching db.h and the library. To make
+it easy to pin this down, there are now debug statements on either side of the
+open call. */
+
+sprintf(CS buffer, "%s/db/%s", spool_directory, name);
+DEBUG(D_hints_lookup) debug_printf("EXIM_DBOPEN(%s)\n", buffer);
+EXIM_DBOPEN(buffer, flags, EXIMDB_MODE, &(dbblock->dbptr));
+DEBUG(D_hints_lookup) debug_printf("returned from EXIM_DBOPEN\n");
+
+if (dbblock->dbptr == NULL && errno == ENOENT && flags == O_RDWR)
+  {
+  DEBUG(D_hints_lookup)
+    debug_printf("%s appears not to exist: trying to create\n", buffer);
+  created = TRUE;
+  EXIM_DBOPEN(buffer, flags|O_CREAT, EXIMDB_MODE, &(dbblock->dbptr));
+  DEBUG(D_hints_lookup) debug_printf("returned from EXIM_DBOPEN\n");
+  }
+
+save_errno = errno;
+
+/* If we are running as root and this is the first access to the database, its
+files will be owned by root. We want them to be owned by exim. We detect this
+situation by noting above when we had to create the lock file or the database
+itself. Because the different dbm libraries use different extensions for their
+files, I don't know of any easier way of arranging this than scanning the
+directory for files with the appropriate base name. At least this deals with
+the lock file at the same time. Also, the directory will typically have only
+half a dozen files, so the scan will be quick.
+
+This code is placed here, before the test for successful opening, because there
+was a case when a file was created, but the DBM library still returned NULL
+because of some problem. It also sorts out the lock file if that was created
+but creation of the database file failed. */
+
+if (created && geteuid() == root_uid)
+  {
+  DIR *dd;
+  struct dirent *ent;
+  uschar *lastname = Ustrrchr(buffer, '/') + 1;
+  int namelen = Ustrlen(name);
+
+  *lastname = 0;
+  dd = opendir(CS buffer);
+
+  while ((ent = readdir(dd)) != NULL)
+    {
+    if (Ustrncmp(ent->d_name, name, namelen) == 0)
+      {
+      struct stat statbuf;
+      Ustrcpy(lastname, ent->d_name);
+      if (Ustat(buffer, &statbuf) >= 0 && statbuf.st_uid != exim_uid)
+        {
+        DEBUG(D_hints_lookup) debug_printf("ensuring %s is owned by exim\n", buffer);
+        if (Uchown(buffer, exim_uid, exim_gid))
+          DEBUG(D_hints_lookup) debug_printf("failed setting %s to owned by exim\n", buffer);
+        }
+      }
+    }
+
+  closedir(dd);
+  }
+
+/* If the open has failed, return NULL, leaving errno set. If lof is TRUE,
+log the event - also for debugging - but not if the file just doesn't exist. */
+
+if (dbblock->dbptr == NULL)
+  {
+  if (save_errno != ENOENT)
+    {
+    if (lof)
+      log_write(0, LOG_MAIN, "%s", string_open_failed(save_errno, "DB file %s",
+        buffer));
+    else
+      DEBUG(D_hints_lookup)
+        debug_printf("%s", CS string_open_failed(save_errno, "DB file %s\n",
+          buffer));
+    }
+  (void)close(dbblock->lockfd);
+  errno = save_errno;
+  return NULL;
+  }
+
+DEBUG(D_hints_lookup)
+  debug_printf("opened hints database %s: flags=%s\n", buffer,
+    (flags == O_RDONLY)? "O_RDONLY" : (flags == O_RDWR)? "O_RDWR" :
+    (flags == (O_RDWR|O_CREAT))? "O_RDWR|O_CREAT" : "??");
+
+/* Pass back the block containing the opened database handle and the open fd
+for the lock. */
+
+return dbblock;
+}
+
+
+
+
+/*************************************************
+*         Unlock and close a database file       *
+*************************************************/
+
+/* Closing a file automatically unlocks it, so after closing the database, just
+close the lock file.
+
+Argument: a pointer to an open database block
+Returns:  nothing
+*/
+
+void
+dbfn_close(open_db *dbblock)
+{
+EXIM_DBCLOSE(dbblock->dbptr);
+(void)close(dbblock->lockfd);
+}
+
+
+
+
+/*************************************************
+*             Read from database file            *
+*************************************************/
+
+/* Passing back the pointer unchanged is useless, because there is
+no guarantee of alignment. Since all the records used by Exim need
+to be properly aligned to pick out the timestamps, etc., we might as
+well do the copying centrally here.
+
+Most calls don't need the length, so there is a macro called dbfn_read which
+has two arguments; it calls this function adding NULL as the third.
+
+Arguments:
+  dbblock   a pointer to an open database block
+  key       the key of the record to be read
+  length    a pointer to an int into which to return the length, if not NULL
+
+Returns: a pointer to the retrieved record, or
+         NULL if the record is not found
+*/
+
+void *
+dbfn_read_with_length(open_db *dbblock, uschar *key, int *length)
+{
+void *yield;
+EXIM_DATUM key_datum, result_datum;
+
+DEBUG(D_hints_lookup) debug_printf("dbfn_read: key=%s\n", key);
+
+EXIM_DATUM_INIT(key_datum);         /* Some DBM libraries require the datum */
+EXIM_DATUM_INIT(result_datum);      /* to be cleared before use. */
+EXIM_DATUM_DATA(key_datum) = CS key;
+EXIM_DATUM_SIZE(key_datum) = Ustrlen(key) + 1;
+
+if (!EXIM_DBGET(dbblock->dbptr, key_datum, result_datum)) return NULL;
+
+yield = store_get(EXIM_DATUM_SIZE(result_datum));
+memcpy(yield, EXIM_DATUM_DATA(result_datum), EXIM_DATUM_SIZE(result_datum));
+if (length != NULL) *length = EXIM_DATUM_SIZE(result_datum);
+
+EXIM_DATUM_FREE(result_datum);    /* Some DBM libs require freeing */
+return yield;
+}
+
+
+
+/*************************************************
+*             Write to database file             *
+*************************************************/
+
+/*
+Arguments:
+  dbblock   a pointer to an open database block
+  key       the key of the record to be written
+  ptr       a pointer to the record to be written
+  length    the length of the record to be written
+
+Returns:    the yield of the underlying dbm or db "write" function. If this
+            is dbm, the value is zero for OK.
+*/
+
+int
+dbfn_write(open_db *dbblock, uschar *key, void *ptr, int length)
+{
+EXIM_DATUM key_datum, value_datum;
+dbdata_generic *gptr = (dbdata_generic *)ptr;
+gptr->time_stamp = time(NULL);
+
+DEBUG(D_hints_lookup) debug_printf("dbfn_write: key=%s\n", key);
+
+EXIM_DATUM_INIT(key_datum);         /* Some DBM libraries require the datum */
+EXIM_DATUM_INIT(value_datum);       /* to be cleared before use. */
+EXIM_DATUM_DATA(key_datum) = CS key;
+EXIM_DATUM_SIZE(key_datum) = Ustrlen(key) + 1;
+EXIM_DATUM_DATA(value_datum) = CS ptr;
+EXIM_DATUM_SIZE(value_datum) = length;
+return EXIM_DBPUT(dbblock->dbptr, key_datum, value_datum);
+}
+
+
+
+/*************************************************
+*           Delete record from database file     *
+*************************************************/
+
+/*
+Arguments:
+  dbblock    a pointer to an open database block
+  key        the key of the record to be deleted
+
+Returns: the yield of the underlying dbm or db "delete" function.
+*/
+
+int
+dbfn_delete(open_db *dbblock, uschar *key)
+{
+EXIM_DATUM key_datum;
+EXIM_DATUM_INIT(key_datum);         /* Some DBM libraries require clearing */
+EXIM_DATUM_DATA(key_datum) = CS key;
+EXIM_DATUM_SIZE(key_datum) = Ustrlen(key) + 1;
+return EXIM_DBDEL(dbblock->dbptr, key_datum);
+}
+
+
+
+/*************************************************
+*         Scan the keys of a database file       *
+*************************************************/
+
+/*
+Arguments:
+  dbblock  a pointer to an open database block
+  start    TRUE if starting a new scan
+           FALSE if continuing with the current scan
+  cursor   a pointer to a pointer to a cursor anchor, for those dbm libraries
+           that use the notion of a cursor
+
+Returns:   the next record from the file, or
+           NULL if there are no more
+*/
+
+uschar *
+dbfn_scan(open_db *dbblock, BOOL start, EXIM_CURSOR **cursor)
+{
+EXIM_DATUM key_datum, value_datum;
+uschar *yield;
+value_datum = value_datum;    /* dummy; not all db libraries use this */
+
+/* Some dbm require an initialization */
+
+if (start) EXIM_DBCREATE_CURSOR(dbblock->dbptr, cursor);
+
+EXIM_DATUM_INIT(key_datum);         /* Some DBM libraries require the datum */
+EXIM_DATUM_INIT(value_datum);       /* to be cleared before use. */
+
+yield = (EXIM_DBSCAN(dbblock->dbptr, key_datum, value_datum, start, *cursor))?
+  US EXIM_DATUM_DATA(key_datum) : NULL;
+
+/* Some dbm require a termination */
+
+if (!yield) EXIM_DBDELETE_CURSOR(*cursor);
+return yield;
+}
+
+
+
+/*************************************************
+**************************************************
+*             Stand-alone test program           *
+**************************************************
+*************************************************/
+
+#ifdef STAND_ALONE
+
+int
+main(int argc, char **cargv)
+{
+open_db dbblock[8];
+int max_db = sizeof(dbblock)/sizeof(open_db);
+int current = -1;
+int showtime = 0;
+int i;
+dbdata_wait *dbwait = NULL;
+uschar **argv = USS cargv;
+uschar buffer[256];
+uschar structbuffer[1024];
+
+if (argc != 2)
+  {
+  printf("Usage: test_dbfn directory\n");
+  printf("The subdirectory called \"db\" in the given directory is used for\n");
+  printf("the files used in this test program.\n");
+  return 1;
+  }
+
+/* Initialize */
+
+spool_directory = argv[1];
+debug_selector = D_all - D_memory;
+debug_file = stderr;
+big_buffer = malloc(big_buffer_size);
+
+for (i = 0; i < max_db; i++) dbblock[i].dbptr = NULL;
+
+printf("\nExim's db functions tester: interface type is %s\n", EXIM_DBTYPE);
+printf("DBM library: ");
+
+#ifdef DB_VERSION_STRING
+printf("Berkeley DB: %s\n", DB_VERSION_STRING);
+#elif defined(BTREEVERSION) && defined(HASHVERSION)
+  #ifdef USE_DB
+  printf("probably Berkeley DB version 1.8x (native mode)\n");
+  #else
+  printf("probably Berkeley DB version 1.8x (compatibility mode)\n");
+  #endif
+#elif defined(_DBM_RDONLY) || defined(dbm_dirfno)
+printf("probably ndbm\n");
+#elif defined(USE_TDB)
+printf("using tdb\n");
+#else
+  #ifdef USE_GDBM
+  printf("probably GDBM (native mode)\n");
+  #else
+  printf("probably GDBM (compatibility mode)\n");
+  #endif
+#endif
+
+/* Test the functions */
+
+printf("\nTest the functions\n> ");
+
+while (Ufgets(buffer, 256, stdin) != NULL)
+  {
+  int len = Ustrlen(buffer);
+  int count = 1;
+  clock_t start = 1;
+  clock_t stop = 0;
+  uschar *cmd = buffer;
+  while (len > 0 && isspace((uschar)buffer[len-1])) len--;
+  buffer[len] = 0;
+
+  if (isdigit((uschar)*cmd))
+    {
+    count = Uatoi(cmd);
+    while (isdigit((uschar)*cmd)) cmd++;
+    while (isspace((uschar)*cmd)) cmd++;
+    }
+
+  if (Ustrncmp(cmd, "open", 4) == 0)
+    {
+    int i;
+    open_db *odb;
+    uschar *s = cmd + 4;
+    while (isspace((uschar)*s)) s++;
+
+    for (i = 0; i < max_db; i++)
+      if (dbblock[i].dbptr == NULL) break;
+
+    if (i >= max_db)
+      {
+      printf("Too many open databases\n> ");
+      continue;
+      }
+
+    start = clock();
+    odb = dbfn_open(s, O_RDWR, dbblock + i, TRUE);
+    stop = clock();
+
+    if (odb != NULL)
+      {
+      current = i;
+      printf("opened %d\n", current);
+      }
+    /* Other error cases will have written messages */
+    else if (errno == ENOENT)
+      {
+      printf("open failed: %s%s\n", strerror(errno),
+        #ifdef USE_DB
+        " (or other Berkeley DB error)"
+        #else
+        ""
+        #endif
+        );
+      }
+    }
+
+  else if (Ustrncmp(cmd, "write", 5) == 0)
+    {
+    int rc = 0;
+    uschar *key = cmd + 5;
+    uschar *data;
+
+    if (current < 0)
+      {
+      printf("No current database\n");
+      continue;
+      }
+
+    while (isspace((uschar)*key)) key++;
+    data = key;
+    while (*data != 0 && !isspace((uschar)*data)) data++;
+    *data++ = 0;
+    while (isspace((uschar)*data)) data++;
+
+    dbwait = (dbdata_wait *)(&structbuffer);
+    Ustrcpy(dbwait->text, data);
+
+    start = clock();
+    while (count-- > 0)
+      rc = dbfn_write(dbblock + current, key, dbwait,
+        Ustrlen(data) + sizeof(dbdata_wait));
+    stop = clock();
+    if (rc != 0) printf("Failed: %s\n", strerror(errno));
+    }
+
+  else if (Ustrncmp(cmd, "read", 4) == 0)
+    {
+    uschar *key = cmd + 4;
+    if (current < 0)
+      {
+      printf("No current database\n");
+      continue;
+      }
+    while (isspace((uschar)*key)) key++;
+    start = clock();
+    while (count-- > 0)
+      dbwait = (dbdata_wait *)dbfn_read_with_length(dbblock+ current, key, NULL);
+    stop = clock();
+    printf("%s\n", (dbwait == NULL)? "<not found>" : CS dbwait->text);
+    }
+
+  else if (Ustrncmp(cmd, "delete", 6) == 0)
+    {
+    uschar *key = cmd + 6;
+    if (current < 0)
+      {
+      printf("No current database\n");
+      continue;
+      }
+    while (isspace((uschar)*key)) key++;
+    dbfn_delete(dbblock + current, key);
+    }
+
+  else if (Ustrncmp(cmd, "scan", 4) == 0)
+    {
+    EXIM_CURSOR *cursor;
+    BOOL startflag = TRUE;
+    uschar *key;
+    uschar keybuffer[256];
+    if (current < 0)
+      {
+      printf("No current database\n");
+      continue;
+      }
+    start = clock();
+    while ((key = dbfn_scan(dbblock + current, startflag, &cursor)) != NULL)
+      {
+      startflag = FALSE;
+      Ustrcpy(keybuffer, key);
+      dbwait = (dbdata_wait *)dbfn_read_with_length(dbblock + current,
+        keybuffer, NULL);
+      printf("%s: %s\n", keybuffer, dbwait->text);
+      }
+    stop = clock();
+    printf("End of scan\n");
+    }
+
+  else if (Ustrncmp(cmd, "close", 5) == 0)
+    {
+    uschar *s = cmd + 5;
+    while (isspace((uschar)*s)) s++;
+    i = Uatoi(s);
+    if (i >= max_db || dbblock[i].dbptr == NULL) printf("Not open\n"); else
+      {
+      start = clock();
+      dbfn_close(dbblock + i);
+      stop = clock();
+      dbblock[i].dbptr = NULL;
+      if (i == current) current = -1;
+      }
+    }
+
+  else if (Ustrncmp(cmd, "file", 4) == 0)
+    {
+    uschar *s = cmd + 4;
+    while (isspace((uschar)*s)) s++;
+    i = Uatoi(s);
+    if (i >= max_db || dbblock[i].dbptr == NULL) printf("Not open\n");
+      else current = i;
+    }
+
+  else if (Ustrncmp(cmd, "time", 4) == 0)
+    {
+    showtime = ~showtime;
+    printf("Timing %s\n", showtime? "on" : "off");
+    }
+
+  else if (Ustrcmp(cmd, "q") == 0 || Ustrncmp(cmd, "quit", 4) == 0) break;
+
+  else if (Ustrncmp(cmd, "help", 4) == 0)
+    {
+    printf("close  [<number>]              close file [<number>]\n");
+    printf("delete <key>                   remove record from current file\n");
+    printf("file   <number>                make file <number> current\n");
+    printf("open   <name>                  open db file\n");
+    printf("q[uit]                         exit program\n");
+    printf("read   <key>                   read record from current file\n");
+    printf("scan                           scan current file\n");
+    printf("time                           time display on/off\n");
+    printf("write  <key> <rest-of-line>    write record to current file\n");
+    }
+
+  else printf("Eh?\n");
+
+  if (showtime && stop >= start)
+    printf("start=%d stop=%d difference=%d\n", (int)start, (int)stop,
+     (int)(stop - start));
+
+  printf("> ");
+  }
+
+for (i = 0; i < max_db; i++)
+  {
+  if (dbblock[i].dbptr != NULL)
+    {
+    printf("\nClosing %d", i);
+    dbfn_close(dbblock + i);
+    }
+  }
+
+printf("\n");
+return 0;
+}
+
+#endif
+
+/* End of dbfn.c */
diff --git a/src/dbfunctions.h b/src/dbfunctions.h
new file mode 100644 (file)
index 0000000..1963fa9
--- /dev/null
@@ -0,0 +1,33 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+
+/* Functions for reading/writing exim database files */
+
+void     dbfn_close(open_db *);
+int      dbfn_delete(open_db *, uschar *);
+open_db *dbfn_open(uschar *, int, open_db *, BOOL);
+void    *dbfn_read_with_length(open_db *, uschar *, int *);
+uschar  *dbfn_scan(open_db *, BOOL, EXIM_CURSOR **);
+int      dbfn_write(open_db *, uschar *, void *, int);
+
+/* Macro for the common call to read without wanting to know the length. */
+
+#define  dbfn_read(a, b) dbfn_read_with_length(a, b, NULL)
+
+/* Berkeley DB uses a callback function to pass back error details. Its API
+changed at release 4.3. */
+
+#if defined(USE_DB) && defined(DB_VERSION_STRING)
+#if DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 3)
+void     dbfn_bdb_error_callback(const DB_ENV *, const char *, const char *);
+#else
+void     dbfn_bdb_error_callback(const char *, char *);
+#endif
+#endif
+
+/* End of dbfunctions.h */
diff --git a/src/dbstuff.h b/src/dbstuff.h
new file mode 100644 (file)
index 0000000..ce81f1e
--- /dev/null
@@ -0,0 +1,665 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* This header file contains macro definitions so that a variety of DBM
+libraries can be used by Exim. Nigel Metheringham provided the original set for
+Berkeley DB 1.x in native mode and ndbm. Subsequently, versions for Berkeley DB
+2.x and 3.x were added. Later still, support for tdb was added, courtesy of
+James Antill. Most recently, support for native mode gdbm was added, with code
+from Pierre A. Humblet, so Exim could be made to work with Cygwin.
+
+For convenience, the definitions of the structures used in the various hints
+databases are also kept in this file, which is used by the maintenance
+utilities as well as the main Exim binary. */
+
+
+# ifdef USE_TDB
+
+/* ************************* tdb interface ************************ */
+
+#include <tdb.h>
+
+/* Basic DB type */
+#define EXIM_DB TDB_CONTEXT
+
+/* Cursor type: tdb uses the previous "key" in _nextkey() (really it wants
+tdb_traverse to be called) */
+#define EXIM_CURSOR TDB_DATA
+
+/* The datum type used for queries */
+#define EXIM_DATUM TDB_DATA
+
+/* Some text for messages */
+#define EXIM_DBTYPE "tdb"
+
+/* Access functions */
+
+/* EXIM_DBOPEN - sets *dbpp to point to an EXIM_DB, NULL if failed */
+#define EXIM_DBOPEN(name, flags, mode, dbpp) \
+       *(dbpp) = tdb_open(CS name, 0, TDB_DEFAULT, flags, mode)
+
+/* EXIM_DBGET - returns TRUE if successful, FALSE otherwise */
+#define EXIM_DBGET(db, key, data)      \
+       (data = tdb_fetch(db, key), data.dptr != NULL)
+
+/* EXIM_DBPUT - returns nothing useful, assumes replace mode */
+#define EXIM_DBPUT(db, key, data)      \
+       tdb_store(db, key, data, TDB_REPLACE)
+
+/* EXIM_DBPUTB - non-overwriting for use by dbmbuild */
+#define EXIM_DBPUTB(db, key, data)      \
+       tdb_store(db, key, data, TDB_INSERT)
+
+/* Returns from EXIM_DBPUTB */
+
+#define EXIM_DBPUTB_OK  0
+#define EXIM_DBPUTB_DUP (-1)
+
+/* EXIM_DBDEL */
+#define EXIM_DBDEL(db, key) tdb_delete(db, key)
+
+/* EXIM_DBCREATE_CURSOR - initialize for scanning operation */
+#define EXIM_DBCREATE_CURSOR(db, cursor) { \
+   *(cursor) = malloc(sizeof(TDB_DATA)); (*(cursor))->dptr = NULL; }
+
+/* EXIM_DBSCAN - This is complicated because we have to free the last datum
+free() must not die when passed NULL */
+#define EXIM_DBSCAN(db, key, data, first, cursor)      \
+       (key = (first ? tdb_firstkey(db) : tdb_nextkey(db, *(cursor))), \
+        free((cursor)->dptr), *(cursor) = key, \
+        key.dptr != NULL)
+
+/* EXIM_DBDELETE_CURSOR - terminate scanning operation. */
+#define EXIM_DBDELETE_CURSOR(cursor) free(cursor)
+
+/* EXIM_DBCLOSE */
+#define EXIM_DBCLOSE(db)        tdb_close(db)
+
+/* Datum access types - these are intended to be assignable */
+
+#define EXIM_DATUM_SIZE(datum)  (datum).dsize
+#define EXIM_DATUM_DATA(datum)  (datum).dptr
+
+/* Free the stuff inside the datum. */
+
+#define EXIM_DATUM_FREE(datum)  (free((datum).dptr), (datum).dptr = NULL)
+
+/* No initialization is needed. */
+
+#define EXIM_DATUM_INIT(datum)
+
+
+
+/********************* Berkeley db native definitions **********************/
+
+#elif defined USE_DB
+
+#include <db.h>
+
+
+/* We can distinguish between versions 1.x and 2.x/3.x by looking for a
+definition of DB_VERSION_STRING, which is present in versions 2.x onwards. */
+
+#ifdef DB_VERSION_STRING
+
+/* The API changed (again!) between the 2.x and 3.x versions */
+
+#if DB_VERSION_MAJOR >= 3
+
+/***************** Berkeley db 3.x/4.x native definitions ******************/
+
+/* Basic DB type */
+#define EXIM_DB       DB
+
+/* Cursor type, for scanning */
+#define EXIM_CURSOR   DBC
+
+/* The datum type used for queries */
+#define EXIM_DATUM    DBT
+
+/* Some text for messages */
+#define EXIM_DBTYPE   "db (v3/4)"
+
+/* Access functions */
+
+/* EXIM_DBOPEN - sets *dbpp to point to an EXIM_DB, NULL if failed. The
+API changed for DB 4.1. */
+
+#if DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 1)
+#define EXIM_DBOPEN(name, flags, mode, dbpp) \
+       if (db_create(dbpp, NULL, 0) != 0 || \
+         ((*dbpp)->set_errcall(*dbpp, dbfn_bdb_error_callback), \
+         ((*dbpp)->open)(*dbpp, NULL, CS name, NULL, \
+         ((flags) == O_RDONLY)? DB_UNKNOWN : DB_HASH, \
+         ((flags) == O_RDONLY)? DB_RDONLY : DB_CREATE, \
+         mode)) != 0) *(dbpp) = NULL
+#else
+#define EXIM_DBOPEN(name, flags, mode, dbpp) \
+       if (db_create(dbpp, NULL, 0) != 0 || \
+         ((*dbpp)->set_errcall(*dbpp, dbfn_bdb_error_callback), \
+         ((*dbpp)->open)(*dbpp, CS name, NULL, \
+         ((flags) == O_RDONLY)? DB_UNKNOWN : DB_HASH, \
+         ((flags) == O_RDONLY)? DB_RDONLY : DB_CREATE, \
+         mode)) != 0) *(dbpp) = NULL
+#endif
+
+/* EXIM_DBGET - returns TRUE if successful, FALSE otherwise */
+#define EXIM_DBGET(db, key, data)      \
+       ((db)->get(db, NULL, &key, &data, 0) == 0)
+
+/* EXIM_DBPUT - returns nothing useful, assumes replace mode */
+#define EXIM_DBPUT(db, key, data)      \
+       (db)->put(db, NULL, &key, &data, 0)
+
+/* EXIM_DBPUTB - non-overwriting for use by dbmbuild */
+#define EXIM_DBPUTB(db, key, data)      \
+       (db)->put(db, NULL, &key, &data, DB_NOOVERWRITE)
+
+/* Return values from EXIM_DBPUTB */
+
+#define EXIM_DBPUTB_OK  0
+#define EXIM_DBPUTB_DUP DB_KEYEXIST
+
+/* EXIM_DBDEL */
+#define EXIM_DBDEL(db, key)     (db)->del(db, NULL, &key, 0)
+
+/* EXIM_DBCREATE_CURSOR - initialize for scanning operation */
+
+#define EXIM_DBCREATE_CURSOR(db, cursor) \
+       (db)->cursor(db, NULL, cursor, 0)
+
+/* EXIM_DBSCAN - returns TRUE if data is returned, FALSE at end */
+#define EXIM_DBSCAN(db, key, data, first, cursor)      \
+       ((cursor)->c_get(cursor, &key, &data,         \
+         (first? DB_FIRST : DB_NEXT)) == 0)
+
+/* EXIM_DBDELETE_CURSOR - terminate scanning operation */
+#define EXIM_DBDELETE_CURSOR(cursor) \
+       (cursor)->c_close(cursor)
+
+/* EXIM_DBCLOSE */
+#define EXIM_DBCLOSE(db)        (db)->close(db, 0)
+
+/* Datum access types - these are intended to be assignable. */
+
+#define EXIM_DATUM_SIZE(datum)  (datum).size
+#define EXIM_DATUM_DATA(datum)  (datum).data
+
+/* The whole datum structure contains other fields that must be cleared
+before use, but we don't have to free anything after reading data. */
+
+#define EXIM_DATUM_INIT(datum)   memset(&datum, 0, sizeof(datum))
+#define EXIM_DATUM_FREE(datum)
+
+
+#else /* DB_VERSION_MAJOR >= 3 */
+
+/******************* Berkeley db 2.x native definitions ********************/
+
+/* Basic DB type */
+#define EXIM_DB       DB
+
+/* Cursor type, for scanning */
+#define EXIM_CURSOR   DBC
+
+/* The datum type used for queries */
+#define EXIM_DATUM    DBT
+
+/* Some text for messages */
+#define EXIM_DBTYPE   "db (v2)"
+
+/* Access functions */
+
+/* EXIM_DBOPEN - sets *dbpp to point to an EXIM_DB, NULL if failed */
+#define EXIM_DBOPEN(name, flags, mode, dbpp)         \
+       if ((errno = db_open(CS name, DB_HASH,           \
+         ((flags) == O_RDONLY)? DB_RDONLY : DB_CREATE, \
+         mode, NULL, NULL, dbpp)) != 0) *(dbpp) = NULL
+
+/* EXIM_DBGET - returns TRUE if successful, FALSE otherwise */
+#define EXIM_DBGET(db, key, data)      \
+       ((db)->get(db, NULL, &key, &data, 0) == 0)
+
+/* EXIM_DBPUT - returns nothing useful, assumes replace mode */
+#define EXIM_DBPUT(db, key, data)      \
+       (db)->put(db, NULL, &key, &data, 0)
+
+/* EXIM_DBPUTB - non-overwriting for use by dbmbuild */
+#define EXIM_DBPUTB(db, key, data)      \
+       (db)->put(db, NULL, &key, &data, DB_NOOVERWRITE)
+
+/* Return values from EXIM_DBPUTB */
+
+#define EXIM_DBPUTB_OK  0
+#define EXIM_DBPUTB_DUP DB_KEYEXIST
+
+/* EXIM_DBDEL */
+#define EXIM_DBDEL(db, key)     (db)->del(db, NULL, &key, 0)
+
+/* EXIM_DBCREATE_CURSOR - initialize for scanning operation */
+
+/* The API of this function was changed between releases 2.4.14 and 2.7.3. I do
+not know exactly where the change happened, but the Change Log for 2.5.9 lists
+the new option that is available, so I guess that it happened at 2.5.x. */
+
+#if DB_VERSION_MINOR >= 5
+#define EXIM_DBCREATE_CURSOR(db, cursor) \
+       (db)->cursor(db, NULL, cursor, 0)
+#else
+#define EXIM_DBCREATE_CURSOR(db, cursor) \
+       (db)->cursor(db, NULL, cursor)
+#endif
+
+/* EXIM_DBSCAN - returns TRUE if data is returned, FALSE at end */
+#define EXIM_DBSCAN(db, key, data, first, cursor)      \
+       ((cursor)->c_get(cursor, &key, &data,         \
+         (first? DB_FIRST : DB_NEXT)) == 0)
+
+/* EXIM_DBDELETE_CURSOR - terminate scanning operation */
+#define EXIM_DBDELETE_CURSOR(cursor) \
+       (cursor)->c_close(cursor)
+
+/* EXIM_DBCLOSE */
+#define EXIM_DBCLOSE(db)        (db)->close(db, 0)
+
+/* Datum access types - these are intended to be assignable. */
+
+#define EXIM_DATUM_SIZE(datum)  (datum).size
+#define EXIM_DATUM_DATA(datum)  (datum).data
+
+/* The whole datum structure contains other fields that must be cleared
+before use, but we don't have to free anything after reading data. */
+
+#define EXIM_DATUM_INIT(datum)   memset(&datum, 0, sizeof(datum))
+#define EXIM_DATUM_FREE(datum)
+
+#endif /* DB_VERSION_MAJOR >= 3 */
+
+
+/* If DB_VERSION_TYPE is not defined, we have version 1.x */
+
+#else  /* DB_VERSION_TYPE */
+
+/******************* Berkeley db 1.x native definitions ********************/
+
+/* Basic DB type */
+#define EXIM_DB       DB
+
+/* Cursor type, not used with DB 1.x: just set up a dummy */
+#define EXIM_CURSOR   int
+
+/* The datum type used for queries */
+#define EXIM_DATUM    DBT
+
+/* Some text for messages */
+#define EXIM_DBTYPE   "db (v1)"
+
+/* When scanning, for the non-first case we historically just passed 0
+as the flags field and it worked.  On FreeBSD 8 it no longer works and
+instead leads to memory exhaustion.  The man-page on FreeBSD says to use
+R_NEXT, but this 1.x is a historical fallback and I've no idea how portable
+the use of that flag is; so the solution is to define R_NEXT here if it's not
+already defined, with a default value of 0 because that's what we've always
+before been able to pass successfully. */
+#ifndef R_NEXT
+#define R_NEXT 0
+#endif
+
+/* Access functions */
+
+/* EXIM_DBOPEN - sets *dbpp to point to an EXIM_DB, NULL if failed */
+#define EXIM_DBOPEN(name, flags, mode, dbpp) \
+       *(dbpp) = dbopen(CS name, flags, mode, DB_HASH, NULL)
+
+/* EXIM_DBGET - returns TRUE if successful, FALSE otherwise */
+#define EXIM_DBGET(db, key, data)      \
+       ((db)->get(db, &key, &data, 0) == 0)
+
+/* EXIM_DBPUT - returns nothing useful, assumes replace mode */
+#define EXIM_DBPUT(db, key, data)      \
+       (db)->put(db, &key, &data, 0)
+
+/* EXIM_DBPUTB - non-overwriting for use by dbmbuild */
+#define EXIM_DBPUTB(db, key, data)      \
+       (db)->put(db, &key, &data, R_NOOVERWRITE)
+
+/* Returns from EXIM_DBPUTB */
+
+#define EXIM_DBPUTB_OK  0
+#define EXIM_DBPUTB_DUP 1
+
+/* EXIM_DBDEL */
+#define EXIM_DBDEL(db, key)     (db)->del(db, &key, 0)
+
+/* EXIM_DBCREATE_CURSOR - initialize for scanning operation (null) */
+#define EXIM_DBCREATE_CURSOR(db, cursor) {}
+
+/* EXIM_DBSCAN - returns TRUE if data is returned, FALSE at end */
+#define EXIM_DBSCAN(db, key, data, first, cursor)      \
+       ((db)->seq(db, &key, &data, (first? R_FIRST : R_NEXT)) == 0)
+
+/* EXIM_DBDELETE_CURSOR - terminate scanning operation (null). Make it
+refer to cursor, to keep picky compilers happy. */
+#define EXIM_DBDELETE_CURSOR(cursor) { cursor = cursor; }
+
+/* EXIM_DBCLOSE */
+#define EXIM_DBCLOSE(db)        (db)->close(db)
+
+/* Datum access types - these are intended to be assignable */
+
+#define EXIM_DATUM_SIZE(datum)  (datum).size
+#define EXIM_DATUM_DATA(datum)  (datum).data
+
+/* There's no clearing required before use, and we don't have to free anything
+after reading data. */
+
+#define EXIM_DATUM_INIT(datum)
+#define EXIM_DATUM_FREE(datum)
+
+#endif /* DB_VERSION_STRING */
+
+
+
+/********************* gdbm interface definitions **********************/
+
+#elif defined USE_GDBM
+
+#include <gdbm.h>
+
+/* Basic DB type */
+typedef struct {
+       GDBM_FILE gdbm;  /* Database */
+       datum lkey;      /* Last key, for scans */
+} EXIM_DB;
+
+/* Cursor type, not used with gdbm: just set up a dummy */
+#define EXIM_CURSOR int
+
+/* The datum type used for queries */
+#define EXIM_DATUM datum
+
+/* Some text for messages */
+
+#define EXIM_DBTYPE "gdbm"
+
+/* Access functions */
+
+/* EXIM_DBOPEN - returns a EXIM_DB *, NULL if failed */
+#define EXIM_DBOPEN(name, flags, mode, dbpp) \
+     { (*(dbpp)) = (EXIM_DB *) malloc(sizeof(EXIM_DB));\
+       if (*(dbpp) != NULL) { \
+         (*(dbpp))->lkey.dptr = NULL;\
+         (*(dbpp))->gdbm = gdbm_open(CS name, 0, (((flags) & O_CREAT))?GDBM_WRCREAT:(((flags) & (O_RDWR|O_WRONLY))?GDBM_WRITER:GDBM_READER), mode, 0);\
+          if ((*(dbpp))->gdbm == NULL) {\
+              free(*(dbpp));\
+              *(dbpp) = NULL;\
+          }\
+       }\
+     }
+
+/* EXIM_DBGET - returns TRUE if successful, FALSE otherwise */
+#define EXIM_DBGET(db, key, data)      \
+       (data = gdbm_fetch(db->gdbm, key), data.dptr != NULL)
+
+/* EXIM_DBPUT - returns nothing useful, assumes replace mode */
+#define EXIM_DBPUT(db, key, data)      \
+       gdbm_store(db->gdbm, key, data, GDBM_REPLACE)
+
+/* EXIM_DBPUTB - non-overwriting for use by dbmbuild */
+#define EXIM_DBPUTB(db, key, data)      \
+       gdbm_store(db->gdbm, key, data, GDBM_INSERT)
+
+/* Returns from EXIM_DBPUTB */
+
+#define EXIM_DBPUTB_OK  0
+#define EXIM_DBPUTB_DUP 1
+
+/* EXIM_DBDEL */
+#define EXIM_DBDEL(db, key) gdbm_delete(db->gdbm, key)
+
+/* EXIM_DBCREATE_CURSOR - initialize for scanning operation (null) */
+#define EXIM_DBCREATE_CURSOR(db, cursor) {}
+
+/* EXIM_DBSCAN */
+#define EXIM_DBSCAN(db, key, data, first, cursor)      \
+  ( key = ((first)? gdbm_firstkey(db->gdbm) : gdbm_nextkey(db->gdbm, db->lkey)), \
+    (((db)->lkey.dptr != NULL)? (free((db)->lkey.dptr),1) : 1),\
+    db->lkey = key, key.dptr != NULL)
+
+/* EXIM_DBDELETE_CURSOR - terminate scanning operation (null). Make it
+refer to cursor, to keep picky compilers happy. */
+#define EXIM_DBDELETE_CURSOR(cursor) { cursor = cursor; }
+
+/* EXIM_DBCLOSE */
+#define EXIM_DBCLOSE(db) \
+{ gdbm_close((db)->gdbm);\
+  if ((db)->lkey.dptr != NULL) free((db)->lkey.dptr);\
+  free(db); }
+
+/* Datum access types - these are intended to be assignable */
+
+#define EXIM_DATUM_SIZE(datum) (datum).dsize
+#define EXIM_DATUM_DATA(datum) (datum).dptr
+
+/* There's no clearing required before use, but we have to free the dptr
+after reading data. */
+
+#define EXIM_DATUM_INIT(datum)
+#define EXIM_DATUM_FREE(datum) free(datum.dptr)
+
+#else  /* USE_GDBM */
+
+
+/* If none of USE_DB, USG_GDBM, or USE_TDB are set, the default is the NDBM
+interface */
+
+
+/********************* ndbm interface definitions **********************/
+
+#include <ndbm.h>
+
+/* Basic DB type */
+#define EXIM_DB DBM
+
+/* Cursor type, not used with ndbm: just set up a dummy */
+#define EXIM_CURSOR int
+
+/* The datum type used for queries */
+#define EXIM_DATUM datum
+
+/* Some text for messages */
+
+#define EXIM_DBTYPE "ndbm"
+
+/* Access functions */
+
+/* EXIM_DBOPEN - returns a EXIM_DB *, NULL if failed */
+#define EXIM_DBOPEN(name, flags, mode, dbpp) \
+       *(dbpp) = dbm_open(CS name, flags, mode)
+
+/* EXIM_DBGET - returns TRUE if successful, FALSE otherwise */
+#define EXIM_DBGET(db, key, data)      \
+       (data = dbm_fetch(db, key), data.dptr != NULL)
+
+/* EXIM_DBPUT - returns nothing useful, assumes replace mode */
+#define EXIM_DBPUT(db, key, data)      \
+       dbm_store(db, key, data, DBM_REPLACE)
+
+/* EXIM_DBPUTB - non-overwriting for use by dbmbuild */
+#define EXIM_DBPUTB(db, key, data)      \
+       dbm_store(db, key, data, DBM_INSERT)
+
+/* Returns from EXIM_DBPUTB */
+
+#define EXIM_DBPUTB_OK  0
+#define EXIM_DBPUTB_DUP 1
+
+/* EXIM_DBDEL */
+#define EXIM_DBDEL(db, key) dbm_delete(db, key)
+
+/* EXIM_DBCREATE_CURSOR - initialize for scanning operation (null) */
+#define EXIM_DBCREATE_CURSOR(db, cursor) {}
+
+/* EXIM_DBSCAN */
+#define EXIM_DBSCAN(db, key, data, first, cursor)      \
+       (key = (first? dbm_firstkey(db) : dbm_nextkey(db)), key.dptr != NULL)
+
+/* EXIM_DBDELETE_CURSOR - terminate scanning operation (null). Make it
+refer to cursor, to keep picky compilers happy. */
+#define EXIM_DBDELETE_CURSOR(cursor) { cursor = cursor; }
+
+/* EXIM_DBCLOSE */
+#define EXIM_DBCLOSE(db) dbm_close(db)
+
+/* Datum access types - these are intended to be assignable */
+
+#define EXIM_DATUM_SIZE(datum) (datum).dsize
+#define EXIM_DATUM_DATA(datum) (datum).dptr
+
+/* There's no clearing required before use, and we don't have to free anything
+after reading data. */
+
+#define EXIM_DATUM_INIT(datum)
+#define EXIM_DATUM_FREE(datum)
+
+#endif /* USE_GDBM */
+
+/********************* End of dbm library definitions **********************/
+
+
+/* Structure for carrying around an open DBM file, and an open locking file
+that relates to it. */
+
+typedef struct {
+  EXIM_DB *dbptr;
+  int lockfd;
+} open_db;
+
+
+/* Structures for records stored in exim database dbm files. They all
+start with the same fields, described in the generic type. */
+
+
+typedef struct {
+  time_t time_stamp;      /* Timestamp of writing */
+} dbdata_generic;
+
+
+/* This structure keeps track of retry information for a host or a local
+address. */
+
+typedef struct {
+  time_t time_stamp;
+  /*************/
+  time_t first_failed;    /* Time of first failure */
+  time_t last_try;        /* Time of last try */
+  time_t next_try;        /* Time of next try */
+  BOOL   expired;         /* Retry time has expired */
+  int    basic_errno;     /* Errno of last failure */
+  int    more_errno;      /* Additional information */
+  uschar text[1];         /* Text message for last failure */
+} dbdata_retry;
+
+/* These structures keep track of addresses that have had callout verification
+performed on them. There are two groups of records:
+
+1. keyed by localpart@domain -
+     Full address was tested and record holds result
+
+2. keyed by domain -
+     Domain response upto MAIL FROM:<>, postmaster, random local part;
+
+If a record exists, the result field is either ccache_accept or ccache_reject,
+or, for a domain record only, ccache_reject_mfnull when MAIL FROM:<> was
+rejected. The other fields, however, (which are only relevant to domain
+records) may also contain ccache_unknown if that particular test has not been
+done.
+
+Originally, there was only one structure, used for both types. However, it got
+expanded for domain records, so it got split. To make it possible for Exim to
+handle the old type of record, we retain the old definition. The different
+kinds of record can be distinguised by their different lengths. */
+
+typedef struct {
+  time_t time_stamp;
+  /*************/
+  int   result;
+  int   postmaster_result; /* Postmaster is accepted */
+  int   random_result;     /* Random local part was accepted */
+} dbdata_callout_cache_obs;
+
+typedef struct {
+  time_t time_stamp;       /* Timestamp of last address check */
+  /*************/
+  int   result;            /* accept or reject */
+} dbdata_callout_cache_address;
+
+/* For this new layout, we put the additional fields (the timestamps)
+last so that if somebody reverts to an older Exim, the new records will
+still make sense because they match the old layout. */
+
+typedef struct {
+  time_t time_stamp;       /* Time stamp of last connection */
+  /*************/
+  int   result;            /* Domain reject or accept */
+  int   postmaster_result; /* Postmaster result */
+  int   random_result;     /* Random result */
+  time_t postmaster_stamp; /* Timestamp of postmaster check */
+  time_t random_stamp;     /* Timestamp of random check */
+} dbdata_callout_cache;
+
+/* This structure keeps track of messages that are waiting for a particular
+host for a particular transport. */
+
+typedef struct {
+  time_t time_stamp;
+  /*************/
+  int    count;           /* Count of message ids */
+  int    sequence;        /* Sequence for continued records */
+  uschar text[1];         /* One long character string */
+} dbdata_wait;
+
+
+/* The contents of the "misc" database are a mixture of different kinds of
+record, as defined below. The keys used for a specific type all start with a
+given string such as "etrn-" or "host-serialize-". */
+
+
+/* This structure records a connection to a particular host, for the
+purpose of serializing access to certain hosts. For possible future extension,
+a field is defined for holding the count of connections, but it is not
+at present in use. The same structure is used for recording a running ETRN
+process. */
+
+typedef struct {
+  time_t time_stamp;
+  /*************/
+  int    count;           /* Reserved for possible connection count */
+} dbdata_serialize;
+
+
+/* This structure records the information required for the ratelimit
+ACL condition. */
+
+typedef struct {
+  time_t time_stamp;
+  /*************/
+  int    time_usec;       /* Fractional part of time, from gettimeofday() */
+  double rate;            /* Smoothed sending rate at that time */
+} dbdata_ratelimit;
+
+/* Same as above, plus a Bloom filter for uniquifying events. */
+
+typedef struct {
+  dbdata_ratelimit dbd;
+  time_t   bloom_epoch;   /* When the Bloom filter was last reset */
+  unsigned bloom_size;    /* Number of bytes in the Bloom filter */
+  uschar   bloom[40];     /* Bloom filter which may be larger than this */
+} dbdata_ratelimit_unique;
+
+
+/* End of dbstuff.h */
diff --git a/src/dcc.c b/src/dcc.c
new file mode 100644 (file)
index 0000000..44c0c00
--- /dev/null
+++ b/src/dcc.c
@@ -0,0 +1,517 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) Wolfgang Breyha 2005-2013
+ * Vienna University Computer Center
+ * wbreyha@gmx.net
+ * See the file NOTICE for conditions of use and distribution.
+ */
+
+/* This patch is based on code from Tom Kistners exiscan (ACL integration) and
+ * the DCC local_scan patch from Christopher Bodenstein */
+
+/* Code for calling dccifd. Called from acl.c. */
+
+#include "exim.h"
+#ifdef EXPERIMENTAL_DCC
+#include "dcc.h"
+#include "unistd.h"
+
+uschar dcc_header_str[256];
+int dcc_ok = 0;
+int dcc_rc = 0;
+
+/* This function takes a file descriptor and a buffer as input and
+ * returns either 0 for success or errno in case of error. */
+
+int flushbuffer (int socket, uschar *buffer)
+ {
+  int retval, rsp;
+  rsp = write(socket, buffer, Ustrlen(buffer));
+  DEBUG(D_acl)
+    debug_printf("DCC: Result of the write() = %d\n", rsp);
+  if(rsp < 0)
+  {
+    DEBUG(D_acl)
+      debug_printf("DCC: Error writing buffer to socket: %s\n", strerror(errno));
+    retval = errno;
+  }
+  else {
+    DEBUG(D_acl)
+      debug_printf("DCC: Wrote buffer to socket:\n%s\n", buffer);
+    retval = 0;
+  }
+  return retval;
+}
+
+int dcc_process(uschar **listptr) {
+  int sep = 0;
+  uschar *list = *listptr;
+  FILE *data_file;
+  uschar *dcc_default_ip_option = US"127.0.0.1";
+  uschar *dcc_helo_option = US"localhost";
+  uschar *dcc_reject_message = US"Rejected by DCC";
+  uschar *xtra_hdrs = NULL;
+  uschar *override_client_ip  = NULL;
+
+  /* from local_scan */
+  int i, j, k, c, retval, sockfd, resp, line;
+  unsigned int portnr;
+  struct sockaddr_un  serv_addr;
+  struct sockaddr_in  serv_addr_in;
+  struct hostent *ipaddress;
+  uschar sockpath[128];
+  uschar sockip[40], client_ip[40];
+  uschar opts[128];
+  uschar rcpt[128], from[128];
+  uschar sendbuf[4096];
+  uschar recvbuf[4096];
+  uschar dcc_return_text[1024];
+  uschar mbox_path[1024];
+  uschar message_subdir[2];
+  struct header_line *dcchdr;
+  uschar *dcc_acl_options;
+  uschar dcc_acl_options_buffer[10];
+  uschar dcc_xtra_hdrs[1024];
+
+  /* grep 1st option */
+  if ((dcc_acl_options = string_nextinlist(&list, &sep,
+                                           dcc_acl_options_buffer,
+                                           sizeof(dcc_acl_options_buffer))) != NULL)
+  {
+    /* parse 1st option */
+    if ( (strcmpic(dcc_acl_options,US"false") == 0) ||
+         (Ustrcmp(dcc_acl_options,"0") == 0) ) {
+      /* explicitly no matching */
+      return FAIL;
+    };
+
+    /* special cases (match anything except empty) */
+    if ( (strcmpic(dcc_acl_options,US"true") == 0) ||
+         (Ustrcmp(dcc_acl_options,"*") == 0) ||
+         (Ustrcmp(dcc_acl_options,"1") == 0) ) {
+      dcc_acl_options = dcc_acl_options;
+    };
+  }
+  else {
+    /* empty means "don't match anything" */
+    return FAIL;
+  };
+
+  sep = 0;
+
+  /* if we scanned this message last time, just return */
+  if ( dcc_ok )
+      return dcc_rc;
+
+  /* open the spooled body */
+  message_subdir[1] = '\0';
+  for (i = 0; i < 2; i++) {
+    message_subdir[0] = (split_spool_directory == (i == 0))? message_id[5] : 0;
+    sprintf(CS mbox_path, "%s/input/%s/%s-D", spool_directory, message_subdir, message_id);
+    data_file = Ufopen(mbox_path,"rb");
+    if (data_file != NULL)
+      break;
+  };
+
+  if (data_file == NULL) {
+    /* error while spooling */
+    log_write(0, LOG_MAIN|LOG_PANIC,
+           "dcc acl condition: error while opening spool file");
+    return DEFER;
+  };
+
+  /* Initialize the variables */
+
+  bzero(sockip,sizeof(sockip));
+  if (dccifd_address) {
+    if (dccifd_address[0] == '/')
+      Ustrncpy(sockpath, dccifd_address, sizeof(sockpath));
+    else
+      if( sscanf(CS dccifd_address, "%s %u", sockip, &portnr) != 2) {
+        log_write(0, LOG_MAIN,
+          "dcc acl condition: warning - invalid dccifd address: '%s'", dccifd_address);
+        (void)fclose(data_file);
+        return DEFER;
+      }
+  }
+
+  /* opts is what we send as dccifd options - see man dccifd */
+  /* We don't support any other option than 'header' so just copy that */
+  bzero(opts,sizeof(opts));
+  Ustrncpy(opts, dccifd_options, sizeof(opts)-1);
+  /* if $acl_m_dcc_override_client_ip is set use it */
+  if (((override_client_ip = expand_string(US"$acl_m_dcc_override_client_ip")) != NULL) && 
+       (override_client_ip[0] != '\0')) {
+    Ustrncpy(client_ip, override_client_ip, sizeof(client_ip)-1);
+    DEBUG(D_acl)
+      debug_printf("DCC: Client IP (overridden): %s\n", client_ip);
+  } 
+  else if(sender_host_address) {
+  /* else if $sender_host_address is available use that? */
+    Ustrncpy(client_ip, sender_host_address, sizeof(client_ip)-1);
+    DEBUG(D_acl)
+      debug_printf("DCC: Client IP (sender_host_address): %s\n", client_ip);
+  } 
+  else {
+    /* sender_host_address is NULL which means it comes from localhost */
+    Ustrncpy(client_ip, dcc_default_ip_option, sizeof(client_ip)-1);
+    DEBUG(D_acl)
+      debug_printf("DCC: Client IP (default): %s\n", client_ip);
+  }
+  /* strncat(opts, my_request, strlen(my_request)); */
+  Ustrcat(opts, "\n");
+  Ustrncat(opts, client_ip, sizeof(opts)-Ustrlen(opts)-1);
+  Ustrncat(opts, "\nHELO ", sizeof(opts)-Ustrlen(opts)-1);
+  Ustrncat(opts, dcc_helo_option, sizeof(opts)-Ustrlen(opts)-2);
+  Ustrcat(opts, "\n");
+
+  /* initialize the other variables */
+  dcchdr = header_list;
+  /* we set the default return value to DEFER */
+  retval = DEFER;
+
+  bzero(sendbuf,sizeof(sendbuf));
+  bzero(dcc_header_str,sizeof(dcc_header_str));
+  bzero(rcpt,sizeof(rcpt));
+  bzero(from,sizeof(from));
+
+  /* send a null return path as "<>". */
+  if (Ustrlen(sender_address) > 0)
+    Ustrncpy(from, sender_address, sizeof(from));
+  else
+    Ustrncpy(from, "<>", sizeof(from));
+  Ustrncat(from, "\n", sizeof(from)-Ustrlen(from)-1);
+
+  /**************************************
+   * Now creating the socket connection *
+   **************************************/
+
+  /* If sockip contains an ip, we use a tcp socket, otherwise a UNIX socket */
+  if(Ustrcmp(sockip, "")){
+    ipaddress = gethostbyname((char *)sockip);
+    bzero((char *) &serv_addr_in, sizeof(serv_addr_in));
+    serv_addr_in.sin_family = AF_INET;
+    bcopy((char *)ipaddress->h_addr, (char *)&serv_addr_in.sin_addr.s_addr, ipaddress->h_length);
+    serv_addr_in.sin_port = htons(portnr);
+    if ((sockfd = socket(AF_INET, SOCK_STREAM,0)) < 0){
+      DEBUG(D_acl)
+        debug_printf("DCC: Creating TCP socket connection failed: %s\n", strerror(errno));
+      log_write(0,LOG_PANIC,"DCC: Creating TCP socket connection failed: %s\n", strerror(errno));
+      /* if we cannot create the socket, defer the mail */
+      (void)fclose(data_file);
+      return retval;
+    }
+    /* Now connecting the socket (INET) */
+    if (connect(sockfd, (struct sockaddr *)&serv_addr_in, sizeof(serv_addr_in)) < 0){
+      DEBUG(D_acl)
+        debug_printf("DCC: Connecting to TCP socket failed: %s\n", strerror(errno));
+      log_write(0,LOG_PANIC,"DCC: Connecting to TCP socket failed: %s\n", strerror(errno));
+      /* if we cannot contact the socket, defer the mail */
+      (void)fclose(data_file);
+      return retval;
+    }
+  } else {
+    /* connecting to the dccifd UNIX socket */
+    bzero((char *)&serv_addr,sizeof(serv_addr));
+    serv_addr.sun_family = AF_UNIX;
+    Ustrcpy(serv_addr.sun_path, sockpath);
+    if ((sockfd = socket(AF_UNIX, SOCK_STREAM,0)) < 0){
+      DEBUG(D_acl)
+        debug_printf("DCC: Creating UNIX socket connection failed: %s\n", strerror(errno));
+      log_write(0,LOG_PANIC,"DCC: Creating UNIX socket connection failed: %s\n", strerror(errno));
+      /* if we cannot create the socket, defer the mail */
+      (void)fclose(data_file);
+      return retval;
+    }
+    /* Now connecting the socket (UNIX) */
+    if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0){
+      DEBUG(D_acl)
+                            debug_printf("DCC: Connecting to UNIX socket failed: %s\n", strerror(errno));
+      log_write(0,LOG_PANIC,"DCC: Connecting to UNIX socket failed: %s\n", strerror(errno));
+      /* if we cannot contact the socket, defer the mail */
+      (void)fclose(data_file);
+      return retval;
+    }
+  }
+  /* the socket is open, now send the options to dccifd*/
+  DEBUG(D_acl)
+    debug_printf("\nDCC: ---------------------------\nDCC: Socket opened; now sending input\nDCC: -----------------\n");
+  /* First, fill in the input buffer */
+  Ustrncpy(sendbuf, opts, sizeof(sendbuf));
+  Ustrncat(sendbuf, from, sizeof(sendbuf)-Ustrlen(sendbuf)-1);
+
+  DEBUG(D_acl)
+  {
+    debug_printf("DCC: opts = %s\nDCC: sender = %s\nDCC: rcpt count = %d\n", opts, from, recipients_count);
+    debug_printf("DCC: Sending options:\nDCC: ****************************\n");
+  }
+
+  /* let's send each of the recipients to dccifd */
+  for (i = 0; i < recipients_count; i++){
+    DEBUG(D_acl)
+      debug_printf("DCC: recipient = %s\n",recipients_list[i].address);
+    if(Ustrlen(sendbuf) + Ustrlen(recipients_list[i].address) > sizeof(sendbuf))
+    {
+      DEBUG(D_acl)
+        debug_printf("DCC: Writing buffer: %s\n", sendbuf);
+      flushbuffer(sockfd, sendbuf);
+      bzero(sendbuf, sizeof(sendbuf));
+    }
+    Ustrncat(sendbuf, recipients_list[i].address, sizeof(sendbuf)-Ustrlen(sendbuf)-1);
+    Ustrncat(sendbuf, "\r\n", sizeof(sendbuf)-Ustrlen(sendbuf)-1);
+  }
+  /* send a blank line between options and message */
+  Ustrncat(sendbuf, "\n", sizeof(sendbuf)-Ustrlen(sendbuf)-1);
+  /* Now we send the input buffer */
+  DEBUG(D_acl)
+    debug_printf("DCC: %s\nDCC: ****************************\n", sendbuf);
+  flushbuffer(sockfd, sendbuf);
+
+  /* now send the message */
+  /* Clear the input buffer */
+  bzero(sendbuf, sizeof(sendbuf));
+  /* First send the headers */
+  /* Now send the headers */
+  DEBUG(D_acl)
+    debug_printf("DCC: Sending headers:\nDCC: ****************************\n");
+  Ustrncpy(sendbuf, dcchdr->text, sizeof(sendbuf)-2);
+  while((dcchdr=dcchdr->next)) {
+    if(dcchdr->slen > sizeof(sendbuf)-2) {
+      /* The size of the header is bigger than the size of
+       * the input buffer, so split it up in smaller parts. */
+       flushbuffer(sockfd, sendbuf);
+       bzero(sendbuf, sizeof(sendbuf));
+       j = 0;
+       while(j < dcchdr->slen)
+       {
+        for(i = 0; i < sizeof(sendbuf)-2; i++) {
+          sendbuf[i] = dcchdr->text[j];
+          j++;
+        }
+        flushbuffer(sockfd, sendbuf);
+        bzero(sendbuf, sizeof(sendbuf));
+       }
+    } else if(Ustrlen(sendbuf) + dcchdr->slen > sizeof(sendbuf)-2) {
+      flushbuffer(sockfd, sendbuf);
+      bzero(sendbuf, sizeof(sendbuf));
+      Ustrncpy(sendbuf, dcchdr->text, sizeof(sendbuf)-2);
+    } else {
+      Ustrncat(sendbuf, dcchdr->text, sizeof(sendbuf)-Ustrlen(sendbuf)-2);
+    }
+  }
+
+  /* a blank line seperates header from body */
+  Ustrncat(sendbuf, "\n", sizeof(sendbuf)-Ustrlen(sendbuf)-1);
+  flushbuffer(sockfd, sendbuf);
+  DEBUG(D_acl)
+    debug_printf("\nDCC: ****************************\n%s", sendbuf);
+
+  /* Clear the input buffer */
+  bzero(sendbuf, sizeof(sendbuf));
+
+  /* now send the body */
+  DEBUG(D_acl)
+    debug_printf("DCC: Writing body:\nDCC: ****************************\n");
+  (void)fseek(data_file, SPOOL_DATA_START_OFFSET, SEEK_SET);
+  while((fread(sendbuf, 1, sizeof(sendbuf)-1, data_file)) > 0) {
+    flushbuffer(sockfd, sendbuf);
+    bzero(sendbuf, sizeof(sendbuf));
+  }
+  DEBUG(D_acl)
+    debug_printf("\nDCC: ****************************\n");
+
+  /* shutdown() the socket */
+  if(shutdown(sockfd, 1) < 0){
+    DEBUG(D_acl)
+      debug_printf("DCC: Couldn't shutdown socket: %s\n", strerror(errno));
+    log_write(0,LOG_MAIN,"DCC: Couldn't shutdown socket: %s\n", strerror(errno));
+    /* If there is a problem with the shutdown()
+     * defer the mail. */
+    (void)fclose(data_file);
+    return retval;
+  }
+  DEBUG(D_acl)
+    debug_printf("\nDCC: -------------------------\nDCC: Input sent.\nDCC: -------------------------\n");
+
+    /********************************
+   * receiving output from dccifd *
+   ********************************/
+  DEBUG(D_acl)
+    debug_printf("\nDCC: -------------------------------------\nDCC: Now receiving output from server\nDCC: -----------------------------------\n");
+
+  /******************************************************************
+   * We should get 3 lines:                                         *
+   * 1/ First line is overall result: either 'A' for Accept,        *
+   *    'R' for Reject, 'S' for accept Some recipients or           *
+   *    'T' for a Temporary error.                                  *
+   * 2/ Second line contains the list of Accepted/Rejected          *
+   *    recipients in the form AARRA (A = accepted, R = rejected).  *
+   * 3/ Third line contains the X-DCC header.                       *
+   ******************************************************************/
+
+  line = 1;    /* we start at the first line of the output */
+  j = 0;       /* will be used as index for the recipients list */
+  k = 0;       /* initializing the index of the X-DCC header: dcc_header_str[k] */
+
+  /* Let's read from the socket until there's nothing left to read */
+  bzero(recvbuf, sizeof(recvbuf));
+  while((resp = read(sockfd, recvbuf, sizeof(recvbuf)-1)) > 0) {
+    /* How much did we get from the socket */
+    c = Ustrlen(recvbuf) + 1;
+    DEBUG(D_acl)
+      debug_printf("DCC: Length of the output buffer is: %d\nDCC: Output buffer is:\nDCC: ------------\nDCC: %s\nDCC: -----------\n", c, recvbuf);
+
+    /* Now let's read each character and see what we've got */
+    for(i = 0; i < c; i++) {
+      /* First check if we reached the end of the line and
+       * then increment the line counter */
+      if(recvbuf[i] == '\n') {
+        line++;
+      }
+      else {
+        /* The first character of the first line is the
+         * overall response. If there's another character
+         * on that line it is not correct. */
+        if(line == 1) {
+          if(i == 0) {
+            /* Now get the value and set the
+             * return value accordingly */
+            if(recvbuf[i] == 'A') {
+              DEBUG(D_acl)
+                debug_printf("DCC: Overall result = A\treturning OK\n");
+              Ustrcpy(dcc_return_text, "Mail accepted by DCC");
+              dcc_result = US"A";
+              retval = OK;
+            }
+            else if(recvbuf[i] == 'R') {
+              DEBUG(D_acl)
+                debug_printf("DCC: Overall result = R\treturning FAIL\n");
+              dcc_result = US"R";
+              retval = FAIL;
+              if(sender_host_name) {
+                log_write(0, LOG_MAIN, "H=%s [%s] F=<%s>: rejected by DCC", sender_host_name, sender_host_address, sender_address);
+              }
+              else {
+                log_write(0, LOG_MAIN, "H=[%s] F=<%s>: rejected by DCC", sender_host_address, sender_address);
+              }
+              Ustrncpy(dcc_return_text, dcc_reject_message, Ustrlen(dcc_reject_message) + 1);
+            }
+            else if(recvbuf[i] == 'S') {
+              DEBUG(D_acl)
+                debug_printf("DCC: Overall result  = S\treturning OK\n");
+              Ustrcpy(dcc_return_text, "Not all recipients accepted by DCC");
+              /* Since we're in an ACL we want a global result
+               * so we accept for all */
+              dcc_result = US"A";
+              retval = OK;
+            }
+            else if(recvbuf[i] == 'G') {
+              DEBUG(D_acl)
+                debug_printf("DCC: Overall result  = G\treturning FAIL\n");
+              Ustrcpy(dcc_return_text, "Greylisted by DCC");
+              dcc_result = US"G";
+              retval = FAIL;
+            }
+            else if(recvbuf[i] == 'T') {
+              DEBUG(D_acl)
+                debug_printf("DCC: Overall result = T\treturning DEFER\n");
+              retval = DEFER;
+              log_write(0,LOG_MAIN,"Temporary error with DCC: %s\n", recvbuf);
+              Ustrcpy(dcc_return_text, "Temporary error with DCC");
+              dcc_result = US"T";
+            }
+            else {
+              DEBUG(D_acl)
+                debug_printf("DCC: Overall result = something else\treturning DEFER\n");
+              retval = DEFER;
+              log_write(0,LOG_MAIN,"Unknown DCC response: %s\n", recvbuf);
+              Ustrcpy(dcc_return_text, "Unknown DCC response");
+              dcc_result = US"T";
+            }
+          }
+          else {
+          /* We're on the first line but not on the first character,
+           * there must be something wrong. */
+            DEBUG(D_acl)
+              debug_printf("DCC: Line = %d but i = %d != 0  character is %c - This is wrong!\n", line, i, recvbuf[i]);
+              log_write(0,LOG_MAIN,"Wrong header from DCC, output is %s\n", recvbuf);
+          }
+        }
+        else if(line == 2) {
+          /* On the second line we get a list of
+           * answers for each recipient. We don't care about
+           * it because we're in an acl and take the
+           * global result. */
+        }
+        else if(line > 2) {
+          /* The third and following lines are the X-DCC header,
+           * so we store it in dcc_header_str. */
+          /* check if we don't get more than we can handle */
+          if(k < sizeof(dcc_header_str)) { 
+            dcc_header_str[k] = recvbuf[i];
+            k++;
+          }
+          else {
+            DEBUG(D_acl)
+              debug_printf("DCC: We got more output than we can store in the X-DCC header. Truncating at 120 characters.\n");
+          }
+        }
+        else {
+          /* Wrong line number. There must be a problem with the output. */
+          DEBUG(D_acl)
+            debug_printf("DCC: Wrong line number in output. Line number is %d\n", line);
+        }
+      }
+    }
+    /* we reinitialize the output buffer before we read again */
+    bzero(recvbuf,sizeof(recvbuf));
+  }
+  /* We have read everything from the socket */
+
+  /* We need to terminate the X-DCC header with a '\n' character. This needs to be k-1
+   * since dcc_header_str[k] contains '\0'. */
+  dcc_header_str[k-1] = '\n';
+
+  /* Now let's sum up what we've got. */
+  DEBUG(D_acl)
+    debug_printf("\nDCC: --------------------------\nDCC: Overall result = %d\nDCC: X-DCC header: %sReturn message: %s\nDCC: dcc_result: %s\n", retval, dcc_header_str, dcc_return_text, dcc_result);
+
+  /* We only add the X-DCC header if it starts with X-DCC */
+  if(!(Ustrncmp(dcc_header_str, "X-DCC", 5))){
+    dcc_header = dcc_header_str;
+    if(dcc_direct_add_header) {
+      header_add(' ' , "%s", dcc_header_str);
+  /* since the MIME ACL already writes the .eml file to disk without DCC Header we've to erase it */
+      unspool_mbox();
+    }
+  }
+  else {
+    DEBUG(D_acl)
+      debug_printf("DCC: Wrong format of the X-DCC header: %s\n", dcc_header_str);
+  }
+
+  /* check if we should add additional headers passed in acl_m_dcc_add_header */
+  if(dcc_direct_add_header) {
+    if (((xtra_hdrs = expand_string(US"$acl_m_dcc_add_header")) != NULL) && (xtra_hdrs[0] != '\0')) {
+      Ustrncpy(dcc_xtra_hdrs, xtra_hdrs, sizeof(dcc_xtra_hdrs) - 2);
+      if (dcc_xtra_hdrs[Ustrlen(dcc_xtra_hdrs)-1] != '\n')
+        Ustrcat(dcc_xtra_hdrs, "\n");
+      header_add(' ', "%s", dcc_xtra_hdrs);
+      DEBUG(D_acl)
+        debug_printf("DCC: adding additional headers in $acl_m_dcc_add_header: %s", dcc_xtra_hdrs);
+    }
+  }
+
+  dcc_ok = 1;
+  /* Now return to exim main process */
+  DEBUG(D_acl)
+    debug_printf("DCC: Before returning to exim main process:\nDCC: return_text = %s - retval = %d\nDCC: dcc_result = %s\n", dcc_return_text, retval, dcc_result);
+
+  (void)fclose(data_file);
+  dcc_rc = retval;
+  return dcc_rc;
+}
+
+#endif
diff --git a/src/dcc.h b/src/dcc.h
new file mode 100644 (file)
index 0000000..9f394f0
--- /dev/null
+++ b/src/dcc.h
@@ -0,0 +1,16 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/*
+ * Copyright (c) Wolfgang Breyha 2005
+ * See the file NOTICE for conditions of use and distribution.
+ *
+ * original dccifd_localscan
+ * Copyright (c) Christopher Bodenstein 2003-2005
+ * <cb@physicman.net>
+*/
+
+#ifdef EXPERIMENTAL_DCC
+/* currently empty */
+#endif
diff --git a/src/debug.c b/src/debug.c
new file mode 100644 (file)
index 0000000..ebd932f
--- /dev/null
@@ -0,0 +1,240 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+
+#include "exim.h"
+
+static uschar  debug_buffer[2048];
+static uschar *debug_ptr = debug_buffer;
+static int     debug_prefix_length = 0;
+
+
+
+/*************************************************
+*               Print tree                       *
+*************************************************/
+
+/* Recursive tree-printing subroutine. It uses a static vector of uschar to
+hold the line-drawing characters that need to be printed on every line as it
+moves down the page. This function is used only in debugging circumstances. The
+output is done via debug_printf(). */
+
+#define tree_printlinesize 132   /* line size for printing */
+static uschar tree_printline[tree_printlinesize];
+
+/* Internal recursive subroutine.
+
+Arguments:
+  p          tree node
+  pos        amount of indenting & vertical bars to pring
+  barswitch  if TRUE print | at the pos value
+
+Returns:     nothing
+*/
+
+static void
+tree_printsub(tree_node *p, int pos, int barswitch)
+{
+int i;
+if (p->right != NULL) tree_printsub(p->right, pos+2, 1);
+for (i = 0; i <= pos-1; i++) debug_printf("%c", tree_printline[i]);
+debug_printf("-->%s [%d]\n", p->name, p->balance);
+tree_printline[pos] = barswitch? '|' : ' ';
+if (p->left != NULL)
+  {
+  tree_printline[pos+2] = '|';
+  tree_printsub(p->left, pos+2, 0);
+  }
+}
+
+/* The external function, with just a tree node argument. */
+
+void
+debug_print_tree(tree_node *p)
+{
+int i;
+for (i = 0; i < tree_printlinesize; i++) tree_printline[i] = ' ';
+if (p == NULL) debug_printf("Empty Tree\n"); else tree_printsub(p, 0, 0);
+debug_printf("---- End of tree ----\n");
+}
+
+
+
+/*************************************************
+*          Print an argv vector                  *
+*************************************************/
+
+/* Called when about to obey execv().
+
+Argument:    the argv vector
+Returns:     nothing
+*/
+
+void
+debug_print_argv(uschar **argv)
+{
+debug_printf("exec");
+while (*argv != NULL) debug_printf(" %.256s", *argv++);
+debug_printf("\n");
+}
+
+
+
+/*************************************************
+*      Expand and print debugging string         *
+*************************************************/
+
+/* The string is expanded and written as debugging output. If
+expansion fails, a message is written instead.
+
+Argument:    the string
+Returns:     nothing
+*/
+
+void
+debug_print_string(uschar *debug_string)
+{
+if (debug_string == NULL) return;
+HDEBUG(D_any|D_v)
+  {
+  uschar *s = expand_string(debug_string);
+  if (s == NULL)
+    debug_printf("failed to expand debug_output \"%s\": %s\n", debug_string,
+      expand_string_message);
+  else if (s[0] != 0)
+    debug_printf("%s%s", s, (s[Ustrlen(s)-1] == '\n')? "" : "\n");
+  }
+}
+
+
+
+/*************************************************
+*      Print current uids and gids               *
+*************************************************/
+
+/*
+Argument:   an introductory string
+Returns:    nothing
+*/
+
+void
+debug_print_ids(uschar *s)
+{
+debug_printf("%s uid=%ld gid=%ld euid=%ld egid=%ld\n", s,
+  (long int)getuid(), (long int)getgid(), (long int)geteuid(),
+  (long int)getegid());
+}
+
+
+
+
+/*************************************************
+*           Print debugging message              *
+*************************************************/
+
+/* There are two entries, one for use when being called directly from a
+function with a variable argument list.
+
+If debug_pid is nonzero, print the pid at the start of each line. This is for
+tidier output when running parallel remote deliveries with debugging turned on.
+Must do the whole thing with a single printf and flush, as otherwise output may
+get interleaved. Since some calls to debug_printf() don't end with newline,
+we save up the text until we do get the newline. */
+
+void
+debug_printf(const char *format, ...)
+{
+va_list ap;
+va_start(ap, format);
+debug_vprintf(format, ap);
+va_end(ap);
+}
+
+void
+debug_vprintf(const char *format, va_list ap)
+{
+if (debug_file == NULL) return;
+
+/* Various things can be inserted at the start of a line. Don't use the
+tod_stamp() function for the timestamp, because that will overwrite the
+timestamp buffer, which may contain something useful. (This was a bug fix: the
++memory debugging with +timestamp did cause a problem.) */
+
+if (debug_ptr == debug_buffer)
+  {
+  DEBUG(D_timestamp)
+    {
+    time_t now = time(NULL);
+    struct tm *t = timestamps_utc? gmtime(&now) : localtime(&now);
+    (void) sprintf(CS debug_ptr, "%02d:%02d:%02d ", t->tm_hour, t->tm_min,
+      t->tm_sec);
+    while(*debug_ptr != 0) debug_ptr++;
+    }
+
+  DEBUG(D_pid)
+    {
+    sprintf(CS debug_ptr, "%5d ", (int)getpid());
+    while(*debug_ptr != 0) debug_ptr++;
+    }
+
+  /* Set up prefix if outputting for host checking and not debugging */
+
+  if (host_checking && debug_selector == 0)
+    {
+    Ustrcpy(debug_ptr, ">>> ");
+    debug_ptr += 4;
+    }
+
+  debug_prefix_length = debug_ptr - debug_buffer;
+  }
+
+/* Use the checked formatting routine to ensure that the buffer
+does not overflow. Ensure there's space for a newline at the end. */
+
+if (!string_vformat(debug_ptr,
+     sizeof(debug_buffer) - (debug_ptr - debug_buffer) - 1, format, ap))
+  {
+  uschar *s = US"**** debug string too long - truncated ****\n";
+  uschar *p = debug_buffer + Ustrlen(debug_buffer);
+  int maxlen = sizeof(debug_buffer) - Ustrlen(s) - 3;
+  if (p > debug_buffer + maxlen) p = debug_buffer + maxlen;
+  if (p > debug_buffer && p[-1] != '\n') *p++ = '\n';
+  Ustrcpy(p, s);
+  }
+
+while(*debug_ptr != 0) debug_ptr++;
+
+/* Output the line if it is complete. If we added any prefix data and there
+are internal newlines, make sure the prefix is on the continuation lines,
+as long as there is room in the buffer. We want to do just a single fprintf()
+so as to avoid interleaving. */
+
+if (debug_ptr[-1] == '\n')
+  {
+  if (debug_prefix_length > 0)
+    {
+    uschar *p = debug_buffer;
+    int left = sizeof(debug_buffer) - (debug_ptr - debug_buffer) - 1;
+    while ((p = Ustrchr(p, '\n') + 1) != debug_ptr &&
+           left >= debug_prefix_length)
+      {
+      int len = debug_ptr - p;
+      memmove(p + debug_prefix_length, p, len + 1);
+      memmove(p, debug_buffer, debug_prefix_length);
+      debug_ptr += debug_prefix_length;
+      left -= debug_prefix_length;
+      }
+    }
+
+  fprintf(debug_file, "%s", CS debug_buffer);
+  fflush(debug_file);
+  debug_ptr = debug_buffer;
+  debug_prefix_length = 0;
+  }
+}
+
+/* End of debug.c */
diff --git a/src/deliver.c b/src/deliver.c
new file mode 100644 (file)
index 0000000..87b54d8
--- /dev/null
@@ -0,0 +1,7683 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2014 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* The main code for delivering a message. */
+
+
+#include "exim.h"
+
+
+/* Data block for keeping track of subprocesses for parallel remote
+delivery. */
+
+typedef struct pardata {
+  address_item *addrlist;      /* chain of addresses */
+  address_item *addr;          /* next address data expected for */
+  pid_t pid;                   /* subprocess pid */
+  int fd;                      /* pipe fd for getting result from subprocess */
+  int transport_count;         /* returned transport count value */
+  BOOL done;                   /* no more data needed */
+  uschar *msg;                 /* error message */
+  uschar *return_path;         /* return_path for these addresses */
+} pardata;
+
+/* Values for the process_recipients variable */
+
+enum { RECIP_ACCEPT, RECIP_IGNORE, RECIP_DEFER,
+       RECIP_FAIL, RECIP_FAIL_FILTER, RECIP_FAIL_TIMEOUT,
+       RECIP_FAIL_LOOP};
+
+/* Mutually recursive functions for marking addresses done. */
+
+static void child_done(address_item *, uschar *);
+static void address_done(address_item *, uschar *);
+
+/* Table for turning base-62 numbers into binary */
+
+static uschar tab62[] =
+          {0,1,2,3,4,5,6,7,8,9,0,0,0,0,0,0,     /* 0-9 */
+           0,10,11,12,13,14,15,16,17,18,19,20,  /* A-K */
+          21,22,23,24,25,26,27,28,29,30,31,32,  /* L-W */
+          33,34,35, 0, 0, 0, 0, 0,              /* X-Z */
+           0,36,37,38,39,40,41,42,43,44,45,46,  /* a-k */
+          47,48,49,50,51,52,53,54,55,56,57,58,  /* l-w */
+          59,60,61};                            /* x-z */
+
+
+/*************************************************
+*            Local static variables              *
+*************************************************/
+
+/* addr_duplicate is global because it needs to be seen from the Envelope-To
+writing code. */
+
+static address_item *addr_defer = NULL;
+static address_item *addr_failed = NULL;
+static address_item *addr_fallback = NULL;
+static address_item *addr_local = NULL;
+static address_item *addr_new = NULL;
+static address_item *addr_remote = NULL;
+static address_item *addr_route = NULL;
+static address_item *addr_succeed = NULL;
+#ifdef EXPERIMENTAL_DSN
+static address_item *addr_dsntmp = NULL;
+static address_item *addr_senddsn = NULL;
+#endif
+
+static FILE *message_log = NULL;
+static BOOL update_spool;
+static BOOL remove_journal;
+static int  parcount = 0;
+static pardata *parlist = NULL;
+static int  return_count;
+static uschar *frozen_info = US"";
+static uschar *used_return_path = NULL;
+
+static uschar spoolname[PATH_MAX];
+
+
+
+/*************************************************
+*             Make a new address item            *
+*************************************************/
+
+/* This function gets the store and initializes with default values. The
+transport_return value defaults to DEFER, so that any unexpected failure to
+deliver does not wipe out the message. The default unique string is set to a
+copy of the address, so that its domain can be lowercased.
+
+Argument:
+  address     the RFC822 address string
+  copy        force a copy of the address
+
+Returns:      a pointer to an initialized address_item
+*/
+
+address_item *
+deliver_make_addr(uschar *address, BOOL copy)
+{
+address_item *addr = store_get(sizeof(address_item));
+*addr = address_defaults;
+if (copy) address = string_copy(address);
+addr->address = address;
+addr->unique = string_copy(address);
+return addr;
+}
+
+
+
+
+/*************************************************
+*     Set expansion values for an address        *
+*************************************************/
+
+/* Certain expansion variables are valid only when handling an address or
+address list. This function sets them up or clears the values, according to its
+argument.
+
+Arguments:
+  addr          the address in question, or NULL to clear values
+Returns:        nothing
+*/
+
+void
+deliver_set_expansions(address_item *addr)
+{
+if (addr == NULL)
+  {
+  uschar ***p = address_expansions;
+  while (*p != NULL) **p++ = NULL;
+  return;
+  }
+
+/* Exactly what gets set depends on whether there is one or more addresses, and
+what they contain. These first ones are always set, taking their values from
+the first address. */
+
+if (addr->host_list == NULL)
+  {
+  deliver_host = deliver_host_address = US"";
+  }
+else
+  {
+  deliver_host = addr->host_list->name;
+  deliver_host_address = addr->host_list->address;
+  }
+
+deliver_recipients = addr;
+deliver_address_data = addr->p.address_data;
+deliver_domain_data = addr->p.domain_data;
+deliver_localpart_data = addr->p.localpart_data;
+
+/* These may be unset for multiple addresses */
+
+deliver_domain = addr->domain;
+self_hostname = addr->self_hostname;
+
+#ifdef EXPERIMENTAL_BRIGHTMAIL
+bmi_deliver = 1;    /* deliver by default */
+bmi_alt_location = NULL;
+bmi_base64_verdict = NULL;
+bmi_base64_tracker_verdict = NULL;
+#endif
+
+/* If there's only one address we can set everything. */
+
+if (addr->next == NULL)
+  {
+  address_item *addr_orig;
+
+  deliver_localpart = addr->local_part;
+  deliver_localpart_prefix = addr->prefix;
+  deliver_localpart_suffix = addr->suffix;
+
+  for (addr_orig = addr; addr_orig->parent != NULL;
+    addr_orig = addr_orig->parent);
+  deliver_domain_orig = addr_orig->domain;
+
+  /* Re-instate any prefix and suffix in the original local part. In all
+  normal cases, the address will have a router associated with it, and we can
+  choose the caseful or caseless version accordingly. However, when a system
+  filter sets up a pipe, file, or autoreply delivery, no router is involved.
+  In this case, though, there won't be any prefix or suffix to worry about. */
+
+  deliver_localpart_orig = (addr_orig->router == NULL)? addr_orig->local_part :
+    addr_orig->router->caseful_local_part?
+      addr_orig->cc_local_part : addr_orig->lc_local_part;
+
+  /* If there's a parent, make its domain and local part available, and if
+  delivering to a pipe or file, or sending an autoreply, get the local
+  part from the parent. For pipes and files, put the pipe or file string
+  into address_pipe and address_file. */
+
+  if (addr->parent != NULL)
+    {
+    deliver_domain_parent = addr->parent->domain;
+    deliver_localpart_parent = (addr->parent->router == NULL)?
+      addr->parent->local_part :
+        addr->parent->router->caseful_local_part?
+          addr->parent->cc_local_part : addr->parent->lc_local_part;
+
+    /* File deliveries have their own flag because they need to be picked out
+    as special more often. */
+
+    if (testflag(addr, af_pfr))
+      {
+      if (testflag(addr, af_file)) address_file = addr->local_part;
+        else if (deliver_localpart[0] == '|') address_pipe = addr->local_part;
+      deliver_localpart = addr->parent->local_part;
+      deliver_localpart_prefix = addr->parent->prefix;
+      deliver_localpart_suffix = addr->parent->suffix;
+      }
+    }
+
+#ifdef EXPERIMENTAL_BRIGHTMAIL
+    /* Set expansion variables related to Brightmail AntiSpam */
+    bmi_base64_verdict = bmi_get_base64_verdict(deliver_localpart_orig, deliver_domain_orig);
+    bmi_base64_tracker_verdict = bmi_get_base64_tracker_verdict(bmi_base64_verdict);
+    /* get message delivery status (0 - don't deliver | 1 - deliver) */
+    bmi_deliver = bmi_get_delivery_status(bmi_base64_verdict);
+    /* if message is to be delivered, get eventual alternate location */
+    if (bmi_deliver == 1) {
+      bmi_alt_location = bmi_get_alt_location(bmi_base64_verdict);
+    };
+#endif
+
+  }
+
+/* For multiple addresses, don't set local part, and leave the domain and
+self_hostname set only if it is the same for all of them. It is possible to
+have multiple pipe and file addresses, but only when all addresses have routed
+to the same pipe or file. */
+
+else
+  {
+  address_item *addr2;
+  if (testflag(addr, af_pfr))
+    {
+    if (testflag(addr, af_file)) address_file = addr->local_part;
+      else if (addr->local_part[0] == '|') address_pipe = addr->local_part;
+    }
+  for (addr2 = addr->next; addr2 != NULL; addr2 = addr2->next)
+    {
+    if (deliver_domain != NULL &&
+        Ustrcmp(deliver_domain, addr2->domain) != 0)
+      deliver_domain = NULL;
+    if (self_hostname != NULL && (addr2->self_hostname == NULL ||
+        Ustrcmp(self_hostname, addr2->self_hostname) != 0))
+      self_hostname = NULL;
+    if (deliver_domain == NULL && self_hostname == NULL) break;
+    }
+  }
+}
+
+
+
+
+/*************************************************
+*                Open a msglog file              *
+*************************************************/
+
+/* This function is used both for normal message logs, and for files in the
+msglog directory that are used to catch output from pipes. Try to create the
+directory if it does not exist. From release 4.21, normal message logs should
+be created when the message is received.
+
+Argument:
+  filename  the file name
+  mode      the mode required
+  error     used for saying what failed
+
+Returns:    a file descriptor, or -1 (with errno set)
+*/
+
+static int
+open_msglog_file(uschar *filename, int mode, uschar **error)
+{
+int fd = Uopen(filename, O_WRONLY|O_APPEND|O_CREAT, mode);
+
+if (fd < 0 && errno == ENOENT)
+  {
+  uschar temp[16];
+  sprintf(CS temp, "msglog/%s", message_subdir);
+  if (message_subdir[0] == 0) temp[6] = 0;
+  (void)directory_make(spool_directory, temp, MSGLOG_DIRECTORY_MODE, TRUE);
+  fd = Uopen(filename, O_WRONLY|O_APPEND|O_CREAT, mode);
+  }
+
+/* Set the close-on-exec flag and change the owner to the exim uid/gid (this
+function is called as root). Double check the mode, because the group setting
+doesn't always get set automatically. */
+
+if (fd >= 0)
+  {
+  (void)fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
+  if (fchown(fd, exim_uid, exim_gid) < 0)
+    {
+    *error = US"chown";
+    return -1;
+    }
+  if (fchmod(fd, mode) < 0)
+    {
+    *error = US"chmod";
+    return -1;
+    }
+  }
+else *error = US"create";
+
+return fd;
+}
+
+
+
+
+/*************************************************
+*           Write to msglog if required          *
+*************************************************/
+
+/* Write to the message log, if configured. This function may also be called
+from transports.
+
+Arguments:
+  format       a string format
+
+Returns:       nothing
+*/
+
+void
+deliver_msglog(const char *format, ...)
+{
+va_list ap;
+if (!message_logs) return;
+va_start(ap, format);
+vfprintf(message_log, format, ap);
+fflush(message_log);
+va_end(ap);
+}
+
+
+
+
+/*************************************************
+*            Replicate status for batch          *
+*************************************************/
+
+/* When a transport handles a batch of addresses, it may treat them
+individually, or it may just put the status in the first one, and return FALSE,
+requesting that the status be copied to all the others externally. This is the
+replication function. As well as the status, it copies the transport pointer,
+which may have changed if appendfile passed the addresses on to a different
+transport.
+
+Argument:    pointer to the first address in a chain
+Returns:     nothing
+*/
+
+static void
+replicate_status(address_item *addr)
+{
+address_item *addr2;
+for (addr2 = addr->next; addr2 != NULL; addr2 = addr2->next)
+  {
+  addr2->transport = addr->transport;
+  addr2->transport_return = addr->transport_return;
+  addr2->basic_errno = addr->basic_errno;
+  addr2->more_errno = addr->more_errno;
+  addr2->special_action = addr->special_action;
+  addr2->message = addr->message;
+  addr2->user_message = addr->user_message;
+  }
+}
+
+
+
+/*************************************************
+*              Compare lists of hosts            *
+*************************************************/
+
+/* This function is given two pointers to chains of host items, and it yields
+TRUE if the lists refer to the same hosts in the same order, except that
+
+(1) Multiple hosts with the same non-negative MX values are permitted to appear
+    in different orders. Round-robinning nameservers can cause this to happen.
+
+(2) Multiple hosts with the same negative MX values less than MX_NONE are also
+    permitted to appear in different orders. This is caused by randomizing
+    hosts lists.
+
+This enables Exim to use a single SMTP transaction for sending to two entirely
+different domains that happen to end up pointing at the same hosts.
+
+Arguments:
+  one       points to the first host list
+  two       points to the second host list
+
+Returns:    TRUE if the lists refer to the same host set
+*/
+
+static BOOL
+same_hosts(host_item *one, host_item *two)
+{
+while (one != NULL && two != NULL)
+  {
+  if (Ustrcmp(one->name, two->name) != 0)
+    {
+    int mx = one->mx;
+    host_item *end_one = one;
+    host_item *end_two = two;
+
+    /* Batch up only if there was no MX and the list was not randomized */
+
+    if (mx == MX_NONE) return FALSE;
+
+    /* Find the ends of the shortest sequence of identical MX values */
+
+    while (end_one->next != NULL && end_one->next->mx == mx &&
+           end_two->next != NULL && end_two->next->mx == mx)
+      {
+      end_one = end_one->next;
+      end_two = end_two->next;
+      }
+
+    /* If there aren't any duplicates, there's no match. */
+
+    if (end_one == one) return FALSE;
+
+    /* For each host in the 'one' sequence, check that it appears in the 'two'
+    sequence, returning FALSE if not. */
+
+    for (;;)
+      {
+      host_item *hi;
+      for (hi = two; hi != end_two->next; hi = hi->next)
+        if (Ustrcmp(one->name, hi->name) == 0) break;
+      if (hi == end_two->next) return FALSE;
+      if (one == end_one) break;
+      one = one->next;
+      }
+
+    /* All the hosts in the 'one' sequence were found in the 'two' sequence.
+    Ensure both are pointing at the last host, and carry on as for equality. */
+
+    two = end_two;
+    }
+
+  /* Hosts matched */
+
+  one = one->next;
+  two = two->next;
+  }
+
+/* True if both are NULL */
+
+return (one == two);
+}
+
+
+
+/*************************************************
+*              Compare header lines              *
+*************************************************/
+
+/* This function is given two pointers to chains of header items, and it yields
+TRUE if they are the same header texts in the same order.
+
+Arguments:
+  one       points to the first header list
+  two       points to the second header list
+
+Returns:    TRUE if the lists refer to the same header set
+*/
+
+static BOOL
+same_headers(header_line *one, header_line *two)
+{
+for (;;)
+  {
+  if (one == two) return TRUE;   /* Includes the case where both NULL */
+  if (one == NULL || two == NULL) return FALSE;
+  if (Ustrcmp(one->text, two->text) != 0) return FALSE;
+  one = one->next;
+  two = two->next;
+  }
+}
+
+
+
+/*************************************************
+*            Compare string settings             *
+*************************************************/
+
+/* This function is given two pointers to strings, and it returns
+TRUE if they are the same pointer, or if the two strings are the same.
+
+Arguments:
+  one       points to the first string
+  two       points to the second string
+
+Returns:    TRUE or FALSE
+*/
+
+static BOOL
+same_strings(uschar *one, uschar *two)
+{
+if (one == two) return TRUE;   /* Includes the case where both NULL */
+if (one == NULL || two == NULL) return FALSE;
+return (Ustrcmp(one, two) == 0);
+}
+
+
+
+/*************************************************
+*        Compare uid/gid for addresses           *
+*************************************************/
+
+/* This function is given a transport and two addresses. It yields TRUE if the
+uid/gid/initgroups settings for the two addresses are going to be the same when
+they are delivered.
+
+Arguments:
+  tp            the transort
+  addr1         the first address
+  addr2         the second address
+
+Returns:        TRUE or FALSE
+*/
+
+static BOOL
+same_ugid(transport_instance *tp, address_item *addr1, address_item *addr2)
+{
+if (!tp->uid_set && tp->expand_uid == NULL && !tp->deliver_as_creator)
+  {
+  if (testflag(addr1, af_uid_set) != testflag(addr2, af_gid_set) ||
+       (testflag(addr1, af_uid_set) &&
+         (addr1->uid != addr2->uid ||
+          testflag(addr1, af_initgroups) != testflag(addr2, af_initgroups))))
+    return FALSE;
+  }
+
+if (!tp->gid_set && tp->expand_gid == NULL)
+  {
+  if (testflag(addr1, af_gid_set) != testflag(addr2, af_gid_set) ||
+     (testflag(addr1, af_gid_set) && addr1->gid != addr2->gid))
+    return FALSE;
+  }
+
+return TRUE;
+}
+
+
+
+
+/*************************************************
+*      Record that an address is complete        *
+*************************************************/
+
+/* This function records that an address is complete. This is straightforward
+for most addresses, where the unique address is just the full address with the
+domain lower cased. For homonyms (addresses that are the same as one of their
+ancestors) their are complications. Their unique addresses have \x\ prepended
+(where x = 0, 1, 2...), so that de-duplication works correctly for siblings and
+cousins.
+
+Exim used to record the unique addresses of homonyms as "complete". This,
+however, fails when the pattern of redirection varies over time (e.g. if taking
+unseen copies at only some times of day) because the prepended numbers may vary
+from one delivery run to the next. This problem is solved by never recording
+prepended unique addresses as complete. Instead, when a homonymic address has
+actually been delivered via a transport, we record its basic unique address
+followed by the name of the transport. This is checked in subsequent delivery
+runs whenever an address is routed to a transport.
+
+If the completed address is a top-level one (has no parent, which means it
+cannot be homonymic) we also add the original address to the non-recipients
+tree, so that it gets recorded in the spool file and therefore appears as
+"done" in any spool listings. The original address may differ from the unique
+address in the case of the domain.
+
+Finally, this function scans the list of duplicates, marks as done any that
+match this address, and calls child_done() for their ancestors.
+
+Arguments:
+  addr        address item that has been completed
+  now         current time as a string
+
+Returns:      nothing
+*/
+
+static void
+address_done(address_item *addr, uschar *now)
+{
+address_item *dup;
+
+update_spool = TRUE;        /* Ensure spool gets updated */
+
+/* Top-level address */
+
+if (addr->parent == NULL)
+  {
+  tree_add_nonrecipient(addr->unique);
+  tree_add_nonrecipient(addr->address);
+  }
+
+/* Homonymous child address */
+
+else if (testflag(addr, af_homonym))
+  {
+  if (addr->transport != NULL)
+    {
+    tree_add_nonrecipient(
+      string_sprintf("%s/%s", addr->unique + 3, addr->transport->name));
+    }
+  }
+
+/* Non-homonymous child address */
+
+else tree_add_nonrecipient(addr->unique);
+
+/* Check the list of duplicate addresses and ensure they are now marked
+done as well. */
+
+for (dup = addr_duplicate; dup != NULL; dup = dup->next)
+  {
+  if (Ustrcmp(addr->unique, dup->unique) == 0)
+    {
+    tree_add_nonrecipient(dup->unique);
+    child_done(dup, now);
+    }
+  }
+}
+
+
+
+
+/*************************************************
+*      Decrease counts in parents and mark done  *
+*************************************************/
+
+/* This function is called when an address is complete. If there is a parent
+address, its count of children is decremented. If there are still other
+children outstanding, the function exits. Otherwise, if the count has become
+zero, address_done() is called to mark the parent and its duplicates complete.
+Then loop for any earlier ancestors.
+
+Arguments:
+  addr      points to the completed address item
+  now       the current time as a string, for writing to the message log
+
+Returns:    nothing
+*/
+
+static void
+child_done(address_item *addr, uschar *now)
+{
+address_item *aa;
+while (addr->parent != NULL)
+  {
+  addr = addr->parent;
+  if ((addr->child_count -= 1) > 0) return;   /* Incomplete parent */
+  address_done(addr, now);
+
+  /* Log the completion of all descendents only when there is no ancestor with
+  the same original address. */
+
+  for (aa = addr->parent; aa != NULL; aa = aa->parent)
+    if (Ustrcmp(aa->address, addr->address) == 0) break;
+  if (aa != NULL) continue;
+
+  deliver_msglog("%s %s: children all complete\n", now, addr->address);
+  DEBUG(D_deliver) debug_printf("%s: children all complete\n", addr->address);
+  }
+}
+
+
+
+
+static uschar *
+d_hostlog(uschar * s, int * sizep, int * ptrp, address_item * addr)
+{
+  s = string_append(s, sizep, ptrp, 5, US" H=", addr->host_used->name,
+    US" [", addr->host_used->address, US"]");
+  if ((log_extra_selector & LX_outgoing_port) != 0)
+    s = string_append(s, sizep, ptrp, 2, US":", string_sprintf("%d",
+      addr->host_used->port));
+  return s;
+}
+
+#ifdef SUPPORT_TLS
+static uschar *
+d_tlslog(uschar * s, int * sizep, int * ptrp, address_item * addr)
+{
+  if ((log_extra_selector & LX_tls_cipher) != 0 && addr->cipher != NULL)
+    s = string_append(s, sizep, ptrp, 2, US" X=", addr->cipher);
+  if ((log_extra_selector & LX_tls_certificate_verified) != 0 &&
+       addr->cipher != NULL)
+    s = string_append(s, sizep, ptrp, 2, US" CV=",
+      testflag(addr, af_cert_verified)? "yes":"no");
+  if ((log_extra_selector & LX_tls_peerdn) != 0 && addr->peerdn != NULL)
+    s = string_append(s, sizep, ptrp, 3, US" DN=\"",
+      string_printing(addr->peerdn), US"\"");
+  return s;
+}
+#endif
+
+/* If msg is NULL this is a delivery log and logchar is used. Otherwise
+this is a nonstandard call; no two-character delivery flag is written
+but sender-host and sender are prefixed and "msg" is inserted in the log line.
+
+Arguments:
+  flags                passed to log_write()
+*/
+void
+delivery_log(int flags, address_item * addr, int logchar, uschar * msg)
+{
+uschar *log_address;
+int size = 256;         /* Used for a temporary, */
+int ptr = 0;            /* expanding buffer, for */
+uschar *s;              /* building log lines;   */
+void *reset_point;      /* released afterwards.  */
+
+
+/* Log the delivery on the main log. We use an extensible string to build up
+the log line, and reset the store afterwards. Remote deliveries should always
+have a pointer to the host item that succeeded; local deliveries can have a
+pointer to a single host item in their host list, for use by the transport. */
+
+#ifdef EXPERIMENTAL_TPDA
+  tpda_delivery_ip = NULL;     /* presume no successful remote delivery */
+  tpda_delivery_port = 0;
+  tpda_delivery_fqdn = NULL;
+  tpda_delivery_local_part = NULL;
+  tpda_delivery_domain = NULL;
+  tpda_delivery_confirmation = NULL;
+  lookup_dnssec_authenticated = NULL;
+#endif
+
+s = reset_point = store_get(size);
+
+log_address = string_log_address(addr, (log_write_selector & L_all_parents) != 0, TRUE);
+if (msg)
+  s = string_append(s, &size, &ptr, 3, host_and_ident(TRUE), US" ", log_address);
+else
+  {
+  s[ptr++] = logchar;
+  s = string_append(s, &size, &ptr, 2, US"> ", log_address);
+  }
+
+if ((log_extra_selector & LX_sender_on_delivery) != 0  ||  msg)
+  s = string_append(s, &size, &ptr, 3, US" F=<", sender_address, US">");
+
+#ifdef EXPERIMENTAL_SRS
+if(addr->p.srs_sender)
+  s = string_append(s, &size, &ptr, 3, US" SRS=<", addr->p.srs_sender, US">");
+#endif
+
+/* You might think that the return path must always be set for a successful
+delivery; indeed, I did for some time, until this statement crashed. The case
+when it is not set is for a delivery to /dev/null which is optimised by not
+being run at all. */
+
+if (used_return_path != NULL &&
+      (log_extra_selector & LX_return_path_on_delivery) != 0)
+  s = string_append(s, &size, &ptr, 3, US" P=<", used_return_path, US">");
+
+if (msg)
+  s = string_append(s, &size, &ptr, 2, US" ", msg);
+
+/* For a delivery from a system filter, there may not be a router */
+if (addr->router != NULL)
+  s = string_append(s, &size, &ptr, 2, US" R=", addr->router->name);
+
+s = string_append(s, &size, &ptr, 2, US" T=", addr->transport->name);
+
+if ((log_extra_selector & LX_delivery_size) != 0)
+  s = string_append(s, &size, &ptr, 2, US" S=",
+    string_sprintf("%d", transport_count));
+
+/* Local delivery */
+
+if (addr->transport->info->local)
+  {
+  if (addr->host_list != NULL)
+    {
+    s = string_append(s, &size, &ptr, 2, US" H=", addr->host_list->name);
+    #ifdef EXPERIMENTAL_TPDA
+      tpda_delivery_fqdn = addr->host_list->name;
+    #endif
+    }
+  if (addr->shadow_message != NULL)
+    s = string_cat(s, &size, &ptr, addr->shadow_message,
+      Ustrlen(addr->shadow_message));
+  }
+
+/* Remote delivery */
+
+else
+  {
+  if (addr->host_used)
+    {
+    s = d_hostlog(s, &size, &ptr, addr);
+    if (continue_sequence > 1)
+      s = string_cat(s, &size, &ptr, US"*", 1);
+
+    #ifdef EXPERIMENTAL_TPDA
+    tpda_delivery_ip =           addr->host_used->address;
+    tpda_delivery_port =         addr->host_used->port;
+    tpda_delivery_fqdn =         addr->host_used->name;
+    tpda_delivery_local_part =   addr->local_part;
+    tpda_delivery_domain =       addr->domain;
+    tpda_delivery_confirmation = addr->message;
+
+    /* DNS lookup status */
+    lookup_dnssec_authenticated = addr->host_used->dnssec==DS_YES ? US"yes"
+                             : addr->host_used->dnssec==DS_NO ? US"no"
+                             : NULL;
+    #endif
+    }
+
+  #ifdef SUPPORT_TLS
+  s = d_tlslog(s, &size, &ptr, addr);
+  #endif
+
+  if (addr->authenticator)
+    {
+    s = string_append(s, &size, &ptr, 2, US" A=", addr->authenticator);
+    if (addr->auth_id)
+      {
+      s = string_append(s, &size, &ptr, 2, US":", addr->auth_id);
+      if (log_extra_selector & LX_smtp_mailauth  &&  addr->auth_sndr)
+        s = string_append(s, &size, &ptr, 2, US":", addr->auth_sndr);
+      }
+    }
+
+  #ifndef DISABLE_PRDR
+  if (addr->flags & af_prdr_used)
+    s = string_append(s, &size, &ptr, 1, US" PRDR");
+  #endif
+  }
+
+/* confirmation message (SMTP (host_used) and LMTP (driver_name)) */
+
+if (log_extra_selector & LX_smtp_confirmation &&
+    addr->message &&
+    (addr->host_used || Ustrcmp(addr->transport->driver_name, "lmtp") == 0))
+  {
+  int i;
+  uschar *p = big_buffer;
+  uschar *ss = addr->message;
+  *p++ = '\"';
+  for (i = 0; i < 256 && ss[i] != 0; i++)      /* limit logged amount */
+    {
+    if (ss[i] == '\"' || ss[i] == '\\') *p++ = '\\'; /* quote \ and " */
+    *p++ = ss[i];
+    }
+  *p++ = '\"';
+  *p = 0;
+  s = string_append(s, &size, &ptr, 2, US" C=", big_buffer);
+  }
+
+/* Time on queue and actual time taken to deliver */
+
+if ((log_extra_selector & LX_queue_time) != 0)
+  s = string_append(s, &size, &ptr, 2, US" QT=",
+    readconf_printtime( (int) ((long)time(NULL) - (long)received_time)) );
+
+if ((log_extra_selector & LX_deliver_time) != 0)
+  s = string_append(s, &size, &ptr, 2, US" DT=",
+    readconf_printtime(addr->more_errno));
+
+/* string_cat() always leaves room for the terminator. Release the
+store we used to build the line after writing it. */
+
+s[ptr] = 0;
+log_write(0, flags, "%s", s);
+
+#ifdef EXPERIMENTAL_TPDA
+if (addr->transport->tpda_delivery_action)
+  {
+  DEBUG(D_deliver)
+    debug_printf("  TPDA(Delivery): tpda_deliver_action=|%s| tpda_delivery_IP=%s\n",
+      addr->transport->tpda_delivery_action, tpda_delivery_ip);
+
+  router_name =    addr->router->name;
+  transport_name = addr->transport->name;
+  if (!expand_string(addr->transport->tpda_delivery_action) && *expand_string_message)
+    log_write(0, LOG_MAIN|LOG_PANIC, "failed to expand tpda_deliver_action in %s: %s\n",
+      transport_name, expand_string_message);
+  router_name = NULL;
+  transport_name = NULL;
+  }
+#endif
+store_reset(reset_point);
+return;
+}
+
+
+
+/*************************************************
+*    Actions at the end of handling an address   *
+*************************************************/
+
+/* This is a function for processing a single address when all that can be done
+with it has been done.
+
+Arguments:
+  addr         points to the address block
+  result       the result of the delivery attempt
+  logflags     flags for log_write() (LOG_MAIN and/or LOG_PANIC)
+  driver_type  indicates which type of driver (transport, or router) was last
+                 to process the address
+  logchar      '=' or '-' for use when logging deliveries with => or ->
+
+Returns:       nothing
+*/
+
+static void
+post_process_one(address_item *addr, int result, int logflags, int driver_type,
+  int logchar)
+{
+uschar *now = tod_stamp(tod_log);
+uschar *driver_kind = NULL;
+uschar *driver_name = NULL;
+uschar *log_address;
+
+int size = 256;         /* Used for a temporary, */
+int ptr = 0;            /* expanding buffer, for */
+uschar *s;              /* building log lines;   */
+void *reset_point;      /* released afterwards.  */
+
+
+DEBUG(D_deliver) debug_printf("post-process %s (%d)\n", addr->address, result);
+
+/* Set up driver kind and name for logging. Disable logging if the router or
+transport has disabled it. */
+
+if (driver_type == DTYPE_TRANSPORT)
+  {
+  if (addr->transport != NULL)
+    {
+    driver_name = addr->transport->name;
+    driver_kind = US" transport";
+    disable_logging = addr->transport->disable_logging;
+    }
+  else driver_kind = US"transporting";
+  }
+else if (driver_type == DTYPE_ROUTER)
+  {
+  if (addr->router != NULL)
+    {
+    driver_name = addr->router->name;
+    driver_kind = US" router";
+    disable_logging = addr->router->disable_logging;
+    }
+  else driver_kind = US"routing";
+  }
+
+/* If there's an error message set, ensure that it contains only printing
+characters - it should, but occasionally things slip in and this at least
+stops the log format from getting wrecked. We also scan the message for an LDAP
+expansion item that has a password setting, and flatten the password. This is a
+fudge, but I don't know a cleaner way of doing this. (If the item is badly
+malformed, it won't ever have gone near LDAP.) */
+
+if (addr->message != NULL)
+  {
+  addr->message = string_printing(addr->message);
+  if (((Ustrstr(addr->message, "failed to expand") != NULL) || (Ustrstr(addr->message, "expansion of ") != NULL)) &&
+      (Ustrstr(addr->message, "mysql") != NULL ||
+       Ustrstr(addr->message, "pgsql") != NULL ||
+#ifdef EXPERIMENTAL_REDIS
+       Ustrstr(addr->message, "redis") != NULL ||
+#endif
+       Ustrstr(addr->message, "sqlite") != NULL ||
+       Ustrstr(addr->message, "ldap:") != NULL ||
+       Ustrstr(addr->message, "ldapdn:") != NULL ||
+       Ustrstr(addr->message, "ldapm:") != NULL))
+    {
+      addr->message = string_sprintf("Temporary internal error");
+    }
+  }
+
+/* If we used a transport that has one of the "return_output" options set, and
+if it did in fact generate some output, then for return_output we treat the
+message as failed if it was not already set that way, so that the output gets
+returned to the sender, provided there is a sender to send it to. For
+return_fail_output, do this only if the delivery failed. Otherwise we just
+unlink the file, and remove the name so that if the delivery failed, we don't
+try to send back an empty or unwanted file. The log_output options operate only
+on a non-empty file.
+
+In any case, we close the message file, because we cannot afford to leave a
+file-descriptor for one address while processing (maybe very many) others. */
+
+if (addr->return_file >= 0 && addr->return_filename != NULL)
+  {
+  BOOL return_output = FALSE;
+  struct stat statbuf;
+  (void)EXIMfsync(addr->return_file);
+
+  /* If there is no output, do nothing. */
+
+  if (fstat(addr->return_file, &statbuf) == 0 && statbuf.st_size > 0)
+    {
+    transport_instance *tb = addr->transport;
+
+    /* Handle logging options */
+
+    if (tb->log_output || (result == FAIL && tb->log_fail_output) ||
+                          (result == DEFER && tb->log_defer_output))
+      {
+      uschar *s;
+      FILE *f = Ufopen(addr->return_filename, "rb");
+      if (f == NULL)
+        log_write(0, LOG_MAIN|LOG_PANIC, "failed to open %s to log output "
+          "from %s transport: %s", addr->return_filename, tb->name,
+          strerror(errno));
+      else
+        {
+        s = US Ufgets(big_buffer, big_buffer_size, f);
+        if (s != NULL)
+          {
+          uschar *p = big_buffer + Ustrlen(big_buffer);
+          while (p > big_buffer && isspace(p[-1])) p--;
+          *p = 0;
+          s = string_printing(big_buffer);
+          log_write(0, LOG_MAIN, "<%s>: %s transport output: %s",
+            addr->address, tb->name, s);
+          }
+        (void)fclose(f);
+        }
+      }
+
+    /* Handle returning options, but only if there is an address to return
+    the text to. */
+
+    if (sender_address[0] != 0 || addr->p.errors_address != NULL)
+      {
+      if (tb->return_output)
+        {
+        addr->transport_return = result = FAIL;
+        if (addr->basic_errno == 0 && addr->message == NULL)
+          addr->message = US"return message generated";
+        return_output = TRUE;
+        }
+      else
+        if (tb->return_fail_output && result == FAIL) return_output = TRUE;
+      }
+    }
+
+  /* Get rid of the file unless it might be returned, but close it in
+  all cases. */
+
+  if (!return_output)
+    {
+    Uunlink(addr->return_filename);
+    addr->return_filename = NULL;
+    addr->return_file = -1;
+    }
+
+  (void)close(addr->return_file);
+  }
+
+/* The success case happens only after delivery by a transport. */
+
+if (result == OK)
+  {
+  addr->next = addr_succeed;
+  addr_succeed = addr;
+
+  /* Call address_done() to ensure that we don't deliver to this address again,
+  and write appropriate things to the message log. If it is a child address, we
+  call child_done() to scan the ancestors and mark them complete if this is the
+  last child to complete. */
+
+  address_done(addr, now);
+  DEBUG(D_deliver) debug_printf("%s delivered\n", addr->address);
+
+  if (addr->parent == NULL)
+    deliver_msglog("%s %s: %s%s succeeded\n", now, addr->address,
+      driver_name, driver_kind);
+  else
+    {
+    deliver_msglog("%s %s <%s>: %s%s succeeded\n", now, addr->address,
+      addr->parent->address, driver_name, driver_kind);
+    child_done(addr, now);
+    }
+
+  /* Certificates for logging (via TPDA) */
+  #ifdef SUPPORT_TLS
+  tls_out.ourcert = addr->ourcert;
+  addr->ourcert = NULL;
+  tls_out.peercert = addr->peercert;
+  addr->peercert = NULL;
+
+  tls_out.cipher = addr->cipher;
+  tls_out.peerdn = addr->peerdn;
+  tls_out.ocsp = addr->ocsp;
+  #endif
+
+  delivery_log(LOG_MAIN, addr, logchar, NULL);
+
+  #ifdef SUPPORT_TLS
+  if (tls_out.ourcert)
+    {
+    tls_free_cert(tls_out.ourcert);
+    tls_out.ourcert = NULL;
+    }
+  if (tls_out.peercert)
+    {
+    tls_free_cert(tls_out.peercert);
+    tls_out.peercert = NULL;
+    }
+  tls_out.cipher = NULL;
+  tls_out.peerdn = NULL;
+  tls_out.ocsp = OCSP_NOT_REQ;
+  #endif
+  }
+
+
+/* Soft failure, or local delivery process failed; freezing may be
+requested. */
+
+else if (result == DEFER || result == PANIC)
+  {
+  if (result == PANIC) logflags |= LOG_PANIC;
+
+  /* This puts them on the chain in reverse order. Do not change this, because
+  the code for handling retries assumes that the one with the retry
+  information is last. */
+
+  addr->next = addr_defer;
+  addr_defer = addr;
+
+  /* The only currently implemented special action is to freeze the
+  message. Logging of this is done later, just before the -H file is
+  updated. */
+
+  if (addr->special_action == SPECIAL_FREEZE)
+    {
+    deliver_freeze = TRUE;
+    deliver_frozen_at = time(NULL);
+    update_spool = TRUE;
+    }
+
+  /* If doing a 2-stage queue run, we skip writing to either the message
+  log or the main log for SMTP defers. */
+
+  if (!queue_2stage || addr->basic_errno != 0)
+    {
+    uschar ss[32];
+
+    /* For errors of the type "retry time not reached" (also remotes skipped
+    on queue run), logging is controlled by L_retry_defer. Note that this kind
+    of error number is negative, and all the retry ones are less than any
+    others. */
+
+    unsigned int use_log_selector = (addr->basic_errno <= ERRNO_RETRY_BASE)?
+      L_retry_defer : 0;
+
+    /* Build up the line that is used for both the message log and the main
+    log. */
+
+    s = reset_point = store_get(size);
+
+    /* Create the address string for logging. Must not do this earlier, because
+    an OK result may be changed to FAIL when a pipe returns text. */
+
+    log_address = string_log_address(addr,
+      (log_write_selector & L_all_parents) != 0, result == OK);
+
+    s = string_cat(s, &size, &ptr, log_address, Ustrlen(log_address));
+
+    /* Either driver_name contains something and driver_kind contains
+    " router" or " transport" (note the leading space), or driver_name is
+    a null string and driver_kind contains "routing" without the leading
+    space, if all routing has been deferred. When a domain has been held,
+    so nothing has been done at all, both variables contain null strings. */
+
+    if (driver_name == NULL)
+      {
+      if (driver_kind != NULL)
+        s = string_append(s, &size, &ptr, 2, US" ", driver_kind);
+      }
+     else
+      {
+      if (driver_kind[1] == 't' && addr->router != NULL)
+        s = string_append(s, &size, &ptr, 2, US" R=", addr->router->name);
+      Ustrcpy(ss, " ?=");
+      ss[1] = toupper(driver_kind[1]);
+      s = string_append(s, &size, &ptr, 2, ss, driver_name);
+      }
+
+    sprintf(CS ss, " defer (%d)", addr->basic_errno);
+    s = string_cat(s, &size, &ptr, ss, Ustrlen(ss));
+
+    if (addr->basic_errno > 0)
+      s = string_append(s, &size, &ptr, 2, US": ",
+        US strerror(addr->basic_errno));
+
+    if (addr->message != NULL)
+      s = string_append(s, &size, &ptr, 2, US": ", addr->message);
+
+    s[ptr] = 0;
+
+    /* Log the deferment in the message log, but don't clutter it
+    up with retry-time defers after the first delivery attempt. */
+
+    if (deliver_firsttime || addr->basic_errno > ERRNO_RETRY_BASE)
+      deliver_msglog("%s %s\n", now, s);
+
+    /* Write the main log and reset the store */
+
+    log_write(use_log_selector, logflags, "== %s", s);
+    store_reset(reset_point);
+    }
+  }
+
+
+/* Hard failure. If there is an address to which an error message can be sent,
+put this address on the failed list. If not, put it on the deferred list and
+freeze the mail message for human attention. The latter action can also be
+explicitly requested by a router or transport. */
+
+else
+  {
+  /* If this is a delivery error, or a message for which no replies are
+  wanted, and the message's age is greater than ignore_bounce_errors_after,
+  force the af_ignore_error flag. This will cause the address to be discarded
+  later (with a log entry). */
+
+  if (sender_address[0] == 0 && message_age >= ignore_bounce_errors_after)
+    setflag(addr, af_ignore_error);
+
+  /* Freeze the message if requested, or if this is a bounce message (or other
+  message with null sender) and this address does not have its own errors
+  address. However, don't freeze if errors are being ignored. The actual code
+  to ignore occurs later, instead of sending a message. Logging of freezing
+  occurs later, just before writing the -H file. */
+
+  if (!testflag(addr, af_ignore_error) &&
+      (addr->special_action == SPECIAL_FREEZE ||
+        (sender_address[0] == 0 && addr->p.errors_address == NULL)
+      ))
+    {
+    frozen_info = (addr->special_action == SPECIAL_FREEZE)? US"" :
+      (sender_local && !local_error_message)?
+        US" (message created with -f <>)" : US" (delivery error message)";
+    deliver_freeze = TRUE;
+    deliver_frozen_at = time(NULL);
+    update_spool = TRUE;
+
+    /* The address is put on the defer rather than the failed queue, because
+    the message is being retained. */
+
+    addr->next = addr_defer;
+    addr_defer = addr;
+    }
+
+  /* Don't put the address on the nonrecipients tree yet; wait until an
+  error message has been successfully sent. */
+
+  else
+    {
+    addr->next = addr_failed;
+    addr_failed = addr;
+    }
+
+  /* Build up the log line for the message and main logs */
+
+  s = reset_point = store_get(size);
+
+  /* Create the address string for logging. Must not do this earlier, because
+  an OK result may be changed to FAIL when a pipe returns text. */
+
+  log_address = string_log_address(addr,
+    (log_write_selector & L_all_parents) != 0, result == OK);
+
+  s = string_cat(s, &size, &ptr, log_address, Ustrlen(log_address));
+
+  if ((log_extra_selector & LX_sender_on_delivery) != 0)
+    s = string_append(s, &size, &ptr, 3, US" F=<", sender_address, US">");
+
+  /* Return path may not be set if no delivery actually happened */
+
+  if (used_return_path != NULL &&
+      (log_extra_selector & LX_return_path_on_delivery) != 0)
+    s = string_append(s, &size, &ptr, 3, US" P=<", used_return_path, US">");
+
+  if (addr->router != NULL)
+    s = string_append(s, &size, &ptr, 2, US" R=", addr->router->name);
+  if (addr->transport != NULL)
+    s = string_append(s, &size, &ptr, 2, US" T=", addr->transport->name);
+
+  if (addr->host_used != NULL)
+    s = d_hostlog(s, &size, &ptr, addr);
+
+  #ifdef SUPPORT_TLS
+  s = d_tlslog(s, &size, &ptr, addr);
+  #endif
+
+  if (addr->basic_errno > 0)
+    s = string_append(s, &size, &ptr, 2, US": ",
+      US strerror(addr->basic_errno));
+
+  if (addr->message != NULL)
+    s = string_append(s, &size, &ptr, 2, US": ", addr->message);
+
+  s[ptr] = 0;
+
+  /* Do the logging. For the message log, "routing failed" for those cases,
+  just to make it clearer. */
+
+  if (driver_name == NULL)
+    deliver_msglog("%s %s failed for %s\n", now, driver_kind, s);
+  else
+    deliver_msglog("%s %s\n", now, s);
+
+  log_write(0, LOG_MAIN, "** %s", s);
+  store_reset(reset_point);
+  }
+
+/* Ensure logging is turned on again in all cases */
+
+disable_logging = FALSE;
+}
+
+
+
+
+/*************************************************
+*            Address-independent error           *
+*************************************************/
+
+/* This function is called when there's an error that is not dependent on a
+particular address, such as an expansion string failure. It puts the error into
+all the addresses in a batch, logs the incident on the main and panic logs, and
+clears the expansions. It is mostly called from local_deliver(), but can be
+called for a remote delivery via findugid().
+
+Arguments:
+  logit        TRUE if (MAIN+PANIC) logging required
+  addr         the first of the chain of addresses
+  code         the error code
+  format       format string for error message, or NULL if already set in addr
+  ...          arguments for the format
+
+Returns:       nothing
+*/
+
+static void
+common_error(BOOL logit, address_item *addr, int code, uschar *format, ...)
+{
+address_item *addr2;
+addr->basic_errno = code;
+
+if (format != NULL)
+  {
+  va_list ap;
+  uschar buffer[512];
+  va_start(ap, format);
+  if (!string_vformat(buffer, sizeof(buffer), CS format, ap))
+    log_write(0, LOG_MAIN|LOG_PANIC_DIE,
+      "common_error expansion was longer than " SIZE_T_FMT, sizeof(buffer));
+  va_end(ap);
+  addr->message = string_copy(buffer);
+  }
+
+for (addr2 = addr->next; addr2 != NULL; addr2 = addr2->next)
+  {
+  addr2->basic_errno = code;
+  addr2->message = addr->message;
+  }
+
+if (logit) log_write(0, LOG_MAIN|LOG_PANIC, "%s", addr->message);
+deliver_set_expansions(NULL);
+}
+
+
+
+
+/*************************************************
+*         Check a "never users" list             *
+*************************************************/
+
+/* This function is called to check whether a uid is on one of the two "never
+users" lists.
+
+Arguments:
+  uid         the uid to be checked
+  nusers      the list to be scanned; the first item in the list is the count
+
+Returns:      TRUE if the uid is on the list
+*/
+
+static BOOL
+check_never_users(uid_t uid, uid_t *nusers)
+{
+int i;
+if (nusers == NULL) return FALSE;
+for (i = 1; i <= (int)(nusers[0]); i++) if (nusers[i] == uid) return TRUE;
+return FALSE;
+}
+
+
+
+/*************************************************
+*          Find uid and gid for a transport      *
+*************************************************/
+
+/* This function is called for both local and remote deliveries, to find the
+uid/gid under which to run the delivery. The values are taken preferentially
+from the transport (either explicit or deliver_as_creator), then from the
+address (i.e. the router), and if nothing is set, the exim uid/gid are used. If
+the resulting uid is on the "never_users" or the "fixed_never_users" list, a
+panic error is logged, and the function fails (which normally leads to delivery
+deferral).
+
+Arguments:
+  addr         the address (possibly a chain)
+  tp           the transport
+  uidp         pointer to uid field
+  gidp         pointer to gid field
+  igfp         pointer to the use_initgroups field
+
+Returns:       FALSE if failed - error has been set in address(es)
+*/
+
+static BOOL
+findugid(address_item *addr, transport_instance *tp, uid_t *uidp, gid_t *gidp,
+  BOOL *igfp)
+{
+uschar *nuname = NULL;
+BOOL gid_set = FALSE;
+
+/* Default initgroups flag comes from the transport */
+
+*igfp = tp->initgroups;
+
+/* First see if there's a gid on the transport, either fixed or expandable.
+The expanding function always logs failure itself. */
+
+if (tp->gid_set)
+  {
+  *gidp = tp->gid;
+  gid_set = TRUE;
+  }
+else if (tp->expand_gid != NULL)
+  {
+  if (route_find_expanded_group(tp->expand_gid, tp->name, US"transport", gidp,
+    &(addr->message))) gid_set = TRUE;
+  else
+    {
+    common_error(FALSE, addr, ERRNO_GIDFAIL, NULL);
+    return FALSE;
+    }
+  }
+
+/* If the transport did not set a group, see if the router did. */
+
+if (!gid_set && testflag(addr, af_gid_set))
+  {
+  *gidp = addr->gid;
+  gid_set = TRUE;
+  }
+
+/* Pick up a uid from the transport if one is set. */
+
+if (tp->uid_set) *uidp = tp->uid;
+
+/* Otherwise, try for an expandable uid field. If it ends up as a numeric id,
+it does not provide a passwd value from which a gid can be taken. */
+
+else if (tp->expand_uid != NULL)
+  {
+  struct passwd *pw;
+  if (!route_find_expanded_user(tp->expand_uid, tp->name, US"transport", &pw,
+       uidp, &(addr->message)))
+    {
+    common_error(FALSE, addr, ERRNO_UIDFAIL, NULL);
+    return FALSE;
+    }
+  if (!gid_set && pw != NULL)
+    {
+    *gidp = pw->pw_gid;
+    gid_set = TRUE;
+    }
+  }
+
+/* If the transport doesn't set the uid, test the deliver_as_creator flag. */
+
+else if (tp->deliver_as_creator)
+  {
+  *uidp = originator_uid;
+  if (!gid_set)
+    {
+    *gidp = originator_gid;
+    gid_set = TRUE;
+    }
+  }
+
+/* Otherwise see if the address specifies the uid and if so, take it and its
+initgroups flag. */
+
+else if (testflag(addr, af_uid_set))
+  {
+  *uidp = addr->uid;
+  *igfp = testflag(addr, af_initgroups);
+  }
+
+/* Nothing has specified the uid - default to the Exim user, and group if the
+gid is not set. */
+
+else
+  {
+  *uidp = exim_uid;
+  if (!gid_set)
+    {
+    *gidp = exim_gid;
+    gid_set = TRUE;
+    }
+  }
+
+/* If no gid is set, it is a disaster. We default to the Exim gid only if
+defaulting to the Exim uid. In other words, if the configuration has specified
+a uid, it must also provide a gid. */
+
+if (!gid_set)
+  {
+  common_error(TRUE, addr, ERRNO_GIDFAIL, US"User set without group for "
+    "%s transport", tp->name);
+  return FALSE;
+  }
+
+/* Check that the uid is not on the lists of banned uids that may not be used
+for delivery processes. */
+
+if (check_never_users(*uidp, never_users))
+  nuname = US"never_users";
+else if (check_never_users(*uidp, fixed_never_users))
+  nuname = US"fixed_never_users";
+
+if (nuname != NULL)
+  {
+  common_error(TRUE, addr, ERRNO_UIDFAIL, US"User %ld set for %s transport "
+    "is on the %s list", (long int)(*uidp), tp->name, nuname);
+  return FALSE;
+  }
+
+/* All is well */
+
+return TRUE;
+}
+
+
+
+
+/*************************************************
+*   Check the size of a message for a transport  *
+*************************************************/
+
+/* Checks that the message isn't too big for the selected transport.
+This is called only when it is known that the limit is set.
+
+Arguments:
+  tp          the transport
+  addr        the (first) address being delivered
+
+Returns:      OK
+              DEFER   expansion failed or did not yield an integer
+              FAIL    message too big
+*/
+
+int
+check_message_size(transport_instance *tp, address_item *addr)
+{
+int rc = OK;
+int size_limit;
+
+deliver_set_expansions(addr);
+size_limit = expand_string_integer(tp->message_size_limit, TRUE);
+deliver_set_expansions(NULL);
+
+if (expand_string_message != NULL)
+  {
+  rc = DEFER;
+  if (size_limit == -1)
+    addr->message = string_sprintf("failed to expand message_size_limit "
+      "in %s transport: %s", tp->name, expand_string_message);
+  else
+    addr->message = string_sprintf("invalid message_size_limit "
+      "in %s transport: %s", tp->name, expand_string_message);
+  }
+else if (size_limit > 0 && message_size > size_limit)
+  {
+  rc = FAIL;
+  addr->message =
+    string_sprintf("message is too big (transport limit = %d)",
+      size_limit);
+  }
+
+return rc;
+}
+
+
+
+/*************************************************
+*  Transport-time check for a previous delivery  *
+*************************************************/
+
+/* Check that this base address hasn't previously been delivered to its routed
+transport. If it has been delivered, mark it done. The check is necessary at
+delivery time in order to handle homonymic addresses correctly in cases where
+the pattern of redirection changes between delivery attempts (so the unique
+fields change). Non-homonymic previous delivery is detected earlier, at routing
+time (which saves unnecessary routing).
+
+Arguments:
+  addr      the address item
+  testing   TRUE if testing wanted only, without side effects
+
+Returns:    TRUE if previously delivered by the transport
+*/
+
+static BOOL
+previously_transported(address_item *addr, BOOL testing)
+{
+(void)string_format(big_buffer, big_buffer_size, "%s/%s",
+  addr->unique + (testflag(addr, af_homonym)? 3:0), addr->transport->name);
+
+if (tree_search(tree_nonrecipients, big_buffer) != 0)
+  {
+  DEBUG(D_deliver|D_route|D_transport)
+    debug_printf("%s was previously delivered (%s transport): discarded\n",
+    addr->address, addr->transport->name);
+  if (!testing) child_done(addr, tod_stamp(tod_log));
+  return TRUE;
+  }
+
+return FALSE;
+}
+
+
+
+/******************************************************
+*      Check for a given header in a header string    *
+******************************************************/
+
+/* This function is used when generating quota warnings. The configuration may
+specify any header lines it likes in quota_warn_message. If certain of them are
+missing, defaults are inserted, so we need to be able to test for the presence
+of a given header.
+
+Arguments:
+  hdr         the required header name
+  hstring     the header string
+
+Returns:      TRUE  the header is in the string
+              FALSE the header is not in the string
+*/
+
+static BOOL
+contains_header(uschar *hdr, uschar *hstring)
+{
+int len = Ustrlen(hdr);
+uschar *p = hstring;
+while (*p != 0)
+  {
+  if (strncmpic(p, hdr, len) == 0)
+    {
+    p += len;
+    while (*p == ' ' || *p == '\t') p++;
+    if (*p == ':') return TRUE;
+    }
+  while (*p != 0 && *p != '\n') p++;
+  if (*p == '\n') p++;
+  }
+return FALSE;
+}
+
+
+
+
+/*************************************************
+*           Perform a local delivery             *
+*************************************************/
+
+/* Each local delivery is performed in a separate process which sets its
+uid and gid as specified. This is a safer way than simply changing and
+restoring using seteuid(); there is a body of opinion that seteuid() cannot be
+used safely. From release 4, Exim no longer makes any use of it. Besides, not
+all systems have seteuid().
+
+If the uid/gid are specified in the transport_instance, they are used; the
+transport initialization must ensure that either both or neither are set.
+Otherwise, the values associated with the address are used. If neither are set,
+it is a configuration error.
+
+The transport or the address may specify a home directory (transport over-
+rides), and if they do, this is set as $home. If neither have set a working
+directory, this value is used for that as well. Otherwise $home is left unset
+and the cwd is set to "/" - a directory that should be accessible to all users.
+
+Using a separate process makes it more complicated to get error information
+back. We use a pipe to pass the return code and also an error code and error
+text string back to the parent process.
+
+Arguments:
+  addr       points to an address block for this delivery; for "normal" local
+             deliveries this is the only address to be delivered, but for
+             pseudo-remote deliveries (e.g. by batch SMTP to a file or pipe)
+             a number of addresses can be handled simultaneously, and in this
+             case addr will point to a chain of addresses with the same
+             characteristics.
+
+  shadowing  TRUE if running a shadow transport; this causes output from pipes
+             to be ignored.
+
+Returns:     nothing
+*/
+
+static void
+deliver_local(address_item *addr, BOOL shadowing)
+{
+BOOL use_initgroups;
+uid_t uid;
+gid_t gid;
+int status, len, rc;
+int pfd[2];
+pid_t pid;
+uschar *working_directory;
+address_item *addr2;
+transport_instance *tp = addr->transport;
+
+/* Set up the return path from the errors or sender address. If the transport
+has its own return path setting, expand it and replace the existing value. */
+
+if(addr->p.errors_address != NULL)
+  return_path = addr->p.errors_address;
+#ifdef EXPERIMENTAL_SRS
+else if(addr->p.srs_sender != NULL)
+  return_path = addr->p.srs_sender;
+#endif
+else
+  return_path = sender_address;
+
+if (tp->return_path != NULL)
+  {
+  uschar *new_return_path = expand_string(tp->return_path);
+  if (new_return_path == NULL)
+    {
+    if (!expand_string_forcedfail)
+      {
+      common_error(TRUE, addr, ERRNO_EXPANDFAIL,
+        US"Failed to expand return path \"%s\" in %s transport: %s",
+        tp->return_path, tp->name, expand_string_message);
+      return;
+      }
+    }
+  else return_path = new_return_path;
+  }
+
+/* For local deliveries, one at a time, the value used for logging can just be
+set directly, once and for all. */
+
+used_return_path = return_path;
+
+/* Sort out the uid, gid, and initgroups flag. If an error occurs, the message
+gets put into the address(es), and the expansions are unset, so we can just
+return. */
+
+if (!findugid(addr, tp, &uid, &gid, &use_initgroups)) return;
+
+/* See if either the transport or the address specifies a home directory. A
+home directory set in the address may already be expanded; a flag is set to
+indicate that. In other cases we must expand it. */
+
+if ((deliver_home = tp->home_dir) != NULL ||       /* Set in transport, or */
+     ((deliver_home = addr->home_dir) != NULL &&   /* Set in address and */
+       !testflag(addr, af_home_expanded)))         /*   not expanded */
+  {
+  uschar *rawhome = deliver_home;
+  deliver_home = NULL;                      /* in case it contains $home */
+  deliver_home = expand_string(rawhome);
+  if (deliver_home == NULL)
+    {
+    common_error(TRUE, addr, ERRNO_EXPANDFAIL, US"home directory \"%s\" failed "
+      "to expand for %s transport: %s", rawhome, tp->name,
+      expand_string_message);
+    return;
+    }
+  if (*deliver_home != '/')
+    {
+    common_error(TRUE, addr, ERRNO_NOTABSOLUTE, US"home directory path \"%s\" "
+      "is not absolute for %s transport", deliver_home, tp->name);
+    return;
+    }
+  }
+
+/* See if either the transport or the address specifies a current directory,
+and if so, expand it. If nothing is set, use the home directory, unless it is
+also unset in which case use "/", which is assumed to be a directory to which
+all users have access. It is necessary to be in a visible directory for some
+operating systems when running pipes, as some commands (e.g. "rm" under Solaris
+2.5) require this. */
+
+working_directory = (tp->current_dir != NULL)?
+  tp->current_dir : addr->current_dir;
+
+if (working_directory != NULL)
+  {
+  uschar *raw = working_directory;
+  working_directory = expand_string(raw);
+  if (working_directory == NULL)
+    {
+    common_error(TRUE, addr, ERRNO_EXPANDFAIL, US"current directory \"%s\" "
+      "failed to expand for %s transport: %s", raw, tp->name,
+      expand_string_message);
+    return;
+    }
+  if (*working_directory != '/')
+    {
+    common_error(TRUE, addr, ERRNO_NOTABSOLUTE, US"current directory path "
+      "\"%s\" is not absolute for %s transport", working_directory, tp->name);
+    return;
+    }
+  }
+else working_directory = (deliver_home == NULL)? US"/" : deliver_home;
+
+/* If one of the return_output flags is set on the transport, create and open a
+file in the message log directory for the transport to write its output onto.
+This is mainly used by pipe transports. The file needs to be unique to the
+address. This feature is not available for shadow transports. */
+
+if (!shadowing && (tp->return_output || tp->return_fail_output ||
+    tp->log_output || tp->log_fail_output))
+  {
+  uschar *error;
+  addr->return_filename =
+    string_sprintf("%s/msglog/%s/%s-%d-%d", spool_directory, message_subdir,
+      message_id, getpid(), return_count++);
+  addr->return_file = open_msglog_file(addr->return_filename, 0400, &error);
+  if (addr->return_file < 0)
+    {
+    common_error(TRUE, addr, errno, US"Unable to %s file for %s transport "
+      "to return message: %s", error, tp->name, strerror(errno));
+    return;
+    }
+  }
+
+/* Create the pipe for inter-process communication. */
+
+if (pipe(pfd) != 0)
+  {
+  common_error(TRUE, addr, ERRNO_PIPEFAIL, US"Creation of pipe failed: %s",
+    strerror(errno));
+  return;
+  }
+
+/* Now fork the process to do the real work in the subprocess, but first
+ensure that all cached resources are freed so that the subprocess starts with
+a clean slate and doesn't interfere with the parent process. */
+
+search_tidyup();
+
+if ((pid = fork()) == 0)
+  {
+  BOOL replicate = TRUE;
+
+  /* Prevent core dumps, as we don't want them in users' home directories.
+  HP-UX doesn't have RLIMIT_CORE; I don't know how to do this in that
+  system. Some experimental/developing systems (e.g. GNU/Hurd) may define
+  RLIMIT_CORE but not support it in setrlimit(). For such systems, do not
+  complain if the error is "not supported".
+
+  There are two scenarios where changing the max limit has an effect.  In one,
+  the user is using a .forward and invoking a command of their choice via pipe;
+  for these, we do need the max limit to be 0 unless the admin chooses to
+  permit an increased limit.  In the other, the command is invoked directly by
+  the transport and is under administrator control, thus being able to raise
+  the limit aids in debugging.  So there's no general always-right answer.
+
+  Thus we inhibit core-dumps completely but let individual transports, while
+  still root, re-raise the limits back up to aid debugging.  We make the
+  default be no core-dumps -- few enough people can use core dumps in
+  diagnosis that it's reasonable to make them something that has to be explicitly requested.
+  */
+
+  #ifdef RLIMIT_CORE
+  struct rlimit rl;
+  rl.rlim_cur = 0;
+  rl.rlim_max = 0;
+  if (setrlimit(RLIMIT_CORE, &rl) < 0)
+    {
+    #ifdef SETRLIMIT_NOT_SUPPORTED
+    if (errno != ENOSYS && errno != ENOTSUP)
+    #endif
+      log_write(0, LOG_MAIN|LOG_PANIC, "setrlimit(RLIMIT_CORE) failed: %s",
+        strerror(errno));
+    }
+  #endif
+
+  /* Reset the random number generator, so different processes don't all
+  have the same sequence. */
+
+  random_seed = 0;
+
+  /* If the transport has a setup entry, call this first, while still
+  privileged. (Appendfile uses this to expand quota, for example, while
+  able to read private files.) */
+
+  if (addr->transport->setup != NULL)
+    {
+    switch((addr->transport->setup)(addr->transport, addr, NULL, uid, gid,
+           &(addr->message)))
+      {
+      case DEFER:
+      addr->transport_return = DEFER;
+      goto PASS_BACK;
+
+      case FAIL:
+      addr->transport_return = PANIC;
+      goto PASS_BACK;
+      }
+    }
+
+  /* Ignore SIGINT and SIGTERM during delivery. Also ignore SIGUSR1, as
+  when the process becomes unprivileged, it won't be able to write to the
+  process log. SIGHUP is ignored throughout exim, except when it is being
+  run as a daemon. */
+
+  signal(SIGINT, SIG_IGN);
+  signal(SIGTERM, SIG_IGN);
+  signal(SIGUSR1, SIG_IGN);
+
+  /* Close the unwanted half of the pipe, and set close-on-exec for the other
+  half - for transports that exec things (e.g. pipe). Then set the required
+  gid/uid. */
+
+  (void)close(pfd[pipe_read]);
+  (void)fcntl(pfd[pipe_write], F_SETFD, fcntl(pfd[pipe_write], F_GETFD) |
+    FD_CLOEXEC);
+  exim_setugid(uid, gid, use_initgroups,
+    string_sprintf("local delivery to %s <%s> transport=%s", addr->local_part,
+      addr->address, addr->transport->name));
+
+  DEBUG(D_deliver)
+    {
+    address_item *batched;
+    debug_printf("  home=%s current=%s\n", deliver_home, working_directory);
+    for (batched = addr->next; batched != NULL; batched = batched->next)
+      debug_printf("additional batched address: %s\n", batched->address);
+    }
+
+  /* Set an appropriate working directory. */
+
+  if (Uchdir(working_directory) < 0)
+    {
+    addr->transport_return = DEFER;
+    addr->basic_errno = errno;
+    addr->message = string_sprintf("failed to chdir to %s", working_directory);
+    }
+
+  /* If successful, call the transport */
+
+  else
+    {
+    BOOL ok = TRUE;
+    set_process_info("delivering %s to %s using %s", message_id,
+     addr->local_part, addr->transport->name);
+
+    /* Setting this global in the subprocess means we need never clear it */
+    transport_name = addr->transport->name;
+
+    /* If a transport filter has been specified, set up its argument list.
+    Any errors will get put into the address, and FALSE yielded. */
+
+    if (addr->transport->filter_command != NULL)
+      {
+      ok = transport_set_up_command(&transport_filter_argv,
+        addr->transport->filter_command,
+        TRUE, PANIC, addr, US"transport filter", NULL);
+      transport_filter_timeout = addr->transport->filter_timeout;
+      }
+    else transport_filter_argv = NULL;
+
+    if (ok)
+      {
+      debug_print_string(addr->transport->debug_string);
+      replicate = !(addr->transport->info->code)(addr->transport, addr);
+      }
+    }
+
+  /* Pass the results back down the pipe. If necessary, first replicate the
+  status in the top address to the others in the batch. The label is the
+  subject of a goto when a call to the transport's setup function fails. We
+  pass the pointer to the transport back in case it got changed as a result of
+  file_format in appendfile. */
+
+  PASS_BACK:
+
+  if (replicate) replicate_status(addr);
+  for (addr2 = addr; addr2 != NULL; addr2 = addr2->next)
+    {
+    int i;
+    int local_part_length = Ustrlen(addr2->local_part);
+    uschar *s;
+    int ret;
+
+    if(  (ret = write(pfd[pipe_write], (void *)&(addr2->transport_return), sizeof(int))) != sizeof(int)
+      || (ret = write(pfd[pipe_write], (void *)&transport_count, sizeof(transport_count))) != sizeof(transport_count)
+      || (ret = write(pfd[pipe_write], (void *)&(addr2->flags), sizeof(addr2->flags))) != sizeof(addr2->flags)
+      || (ret = write(pfd[pipe_write], (void *)&(addr2->basic_errno), sizeof(int))) != sizeof(int)
+      || (ret = write(pfd[pipe_write], (void *)&(addr2->more_errno), sizeof(int))) != sizeof(int)
+      || (ret = write(pfd[pipe_write], (void *)&(addr2->special_action), sizeof(int))) != sizeof(int)
+      || (ret = write(pfd[pipe_write], (void *)&(addr2->transport),
+        sizeof(transport_instance *))) != sizeof(transport_instance *)
+
+    /* For a file delivery, pass back the local part, in case the original
+    was only part of the final delivery path. This gives more complete
+    logging. */
+
+      || (testflag(addr2, af_file)
+          && (  (ret = write(pfd[pipe_write], (void *)&local_part_length, sizeof(int))) != sizeof(int)
+             || (ret = write(pfd[pipe_write], addr2->local_part, local_part_length)) != local_part_length
+            )
+        )
+      )
+      log_write(0, LOG_MAIN|LOG_PANIC, "Failed writing transport results to pipe: %s\n",
+       ret == -1 ? strerror(errno) : "short write");
+
+    /* Now any messages */
+
+    for (i = 0, s = addr2->message; i < 2; i++, s = addr2->user_message)
+      {
+      int message_length = (s == NULL)? 0 : Ustrlen(s) + 1;
+      if(  (ret = write(pfd[pipe_write], (void *)&message_length, sizeof(int))) != sizeof(int)
+        || (message_length > 0  && (ret = write(pfd[pipe_write], s, message_length)) != message_length)
+       )
+        log_write(0, LOG_MAIN|LOG_PANIC, "Failed writing transport results to pipe: %s\n",
+         ret == -1 ? strerror(errno) : "short write");
+      }
+    }
+
+  /* OK, this process is now done. Free any cached resources that it opened,
+  and close the pipe we were writing down before exiting. */
+
+  (void)close(pfd[pipe_write]);
+  search_tidyup();
+  exit(EXIT_SUCCESS);
+  }
+
+/* Back in the main process: panic if the fork did not succeed. This seems
+better than returning an error - if forking is failing it is probably best
+not to try other deliveries for this message. */
+
+if (pid < 0)
+  log_write(0, LOG_MAIN|LOG_PANIC_DIE, "Fork failed for local delivery to %s",
+    addr->address);
+
+/* Read the pipe to get the delivery status codes and error messages. Our copy
+of the writing end must be closed first, as otherwise read() won't return zero
+on an empty pipe. We check that a status exists for each address before
+overwriting the address structure. If data is missing, the default DEFER status
+will remain. Afterwards, close the reading end. */
+
+(void)close(pfd[pipe_write]);
+
+for (addr2 = addr; addr2 != NULL; addr2 = addr2->next)
+  {
+  len = read(pfd[pipe_read], (void *)&status, sizeof(int));
+  if (len > 0)
+    {
+    int i;
+    uschar **sptr;
+
+    addr2->transport_return = status;
+    len = read(pfd[pipe_read], (void *)&transport_count,
+      sizeof(transport_count));
+    len = read(pfd[pipe_read], (void *)&(addr2->flags), sizeof(addr2->flags));
+    len = read(pfd[pipe_read], (void *)&(addr2->basic_errno), sizeof(int));
+    len = read(pfd[pipe_read], (void *)&(addr2->more_errno), sizeof(int));
+    len = read(pfd[pipe_read], (void *)&(addr2->special_action), sizeof(int));
+    len = read(pfd[pipe_read], (void *)&(addr2->transport),
+      sizeof(transport_instance *));
+
+    if (testflag(addr2, af_file))
+      {
+      int local_part_length;
+      len = read(pfd[pipe_read], (void *)&local_part_length, sizeof(int));
+      len = read(pfd[pipe_read], (void *)big_buffer, local_part_length);
+      big_buffer[local_part_length] = 0;
+      addr2->local_part = string_copy(big_buffer);
+      }
+
+    for (i = 0, sptr = &(addr2->message); i < 2;
+         i++, sptr = &(addr2->user_message))
+      {
+      int message_length;
+      len = read(pfd[pipe_read], (void *)&message_length, sizeof(int));
+      if (message_length > 0)
+        {
+        len = read(pfd[pipe_read], (void *)big_buffer, message_length);
+        if (len > 0) *sptr = string_copy(big_buffer);
+        }
+      }
+    }
+
+  else
+    {
+    log_write(0, LOG_MAIN|LOG_PANIC, "failed to read delivery status for %s "
+      "from delivery subprocess", addr2->unique);
+    break;
+    }
+  }
+
+(void)close(pfd[pipe_read]);
+
+/* Unless shadowing, write all successful addresses immediately to the journal
+file, to ensure they are recorded asap. For homonymic addresses, use the base
+address plus the transport name. Failure to write the journal is panic-worthy,
+but don't stop, as it may prove possible subsequently to update the spool file
+in order to record the delivery. */
+
+if (!shadowing)
+  {
+  for (addr2 = addr; addr2 != NULL; addr2 = addr2->next)
+    {
+    if (addr2->transport_return != OK) continue;
+
+    if (testflag(addr2, af_homonym))
+      sprintf(CS big_buffer, "%.500s/%s\n", addr2->unique + 3, tp->name);
+    else
+      sprintf(CS big_buffer, "%.500s\n", addr2->unique);
+
+    /* In the test harness, wait just a bit to let the subprocess finish off
+    any debug output etc first. */
+
+    if (running_in_test_harness) millisleep(300);
+
+    DEBUG(D_deliver) debug_printf("journalling %s", big_buffer);
+    len = Ustrlen(big_buffer);
+    if (write(journal_fd, big_buffer, len) != len)
+      log_write(0, LOG_MAIN|LOG_PANIC, "failed to update journal for %s: %s",
+        big_buffer, strerror(errno));
+    }
+
+  /* Ensure the journal file is pushed out to disk. */
+
+  if (EXIMfsync(journal_fd) < 0)
+    log_write(0, LOG_MAIN|LOG_PANIC, "failed to fsync journal: %s",
+      strerror(errno));
+  }
+
+/* Wait for the process to finish. If it terminates with a non-zero code,
+freeze the message (except for SIGTERM, SIGKILL and SIGQUIT), but leave the
+status values of all the addresses as they are. Take care to handle the case
+when the subprocess doesn't seem to exist. This has been seen on one system
+when Exim was called from an MUA that set SIGCHLD to SIG_IGN. When that
+happens, wait() doesn't recognize the termination of child processes. Exim now
+resets SIGCHLD to SIG_DFL, but this code should still be robust. */
+
+while ((rc = wait(&status)) != pid)
+  {
+  if (rc < 0 && errno == ECHILD)      /* Process has vanished */
+    {
+    log_write(0, LOG_MAIN, "%s transport process vanished unexpectedly",
+      addr->transport->driver_name);
+    status = 0;
+    break;
+    }
+  }
+
+if ((status & 0xffff) != 0)
+  {
+  int msb = (status >> 8) & 255;
+  int lsb = status & 255;
+  int code = (msb == 0)? (lsb & 0x7f) : msb;
+  if (msb != 0 || (code != SIGTERM && code != SIGKILL && code != SIGQUIT))
+    addr->special_action = SPECIAL_FREEZE;
+  log_write(0, LOG_MAIN|LOG_PANIC, "%s transport process returned non-zero "
+    "status 0x%04x: %s %d",
+    addr->transport->driver_name,
+    status,
+    (msb == 0)? "terminated by signal" : "exit code",
+    code);
+  }
+
+/* If SPECIAL_WARN is set in the top address, send a warning message. */
+
+if (addr->special_action == SPECIAL_WARN &&
+    addr->transport->warn_message != NULL)
+  {
+  int fd;
+  uschar *warn_message;
+
+  DEBUG(D_deliver) debug_printf("Warning message requested by transport\n");
+
+  warn_message = expand_string(addr->transport->warn_message);
+  if (warn_message == NULL)
+    log_write(0, LOG_MAIN|LOG_PANIC, "Failed to expand \"%s\" (warning "
+      "message for %s transport): %s", addr->transport->warn_message,
+      addr->transport->name, expand_string_message);
+  else
+    {
+    pid_t pid = child_open_exim(&fd);
+    if (pid > 0)
+      {
+      FILE *f = fdopen(fd, "wb");
+      if (errors_reply_to != NULL &&
+          !contains_header(US"Reply-To", warn_message))
+        fprintf(f, "Reply-To: %s\n", errors_reply_to);
+      fprintf(f, "Auto-Submitted: auto-replied\n");
+      if (!contains_header(US"From", warn_message)) moan_write_from(f);
+      fprintf(f, "%s", CS warn_message);
+
+      /* Close and wait for child process to complete, without a timeout. */
+
+      (void)fclose(f);
+      (void)child_close(pid, 0);
+      }
+    }
+
+  addr->special_action = SPECIAL_NONE;
+  }
+}
+
+
+
+/*************************************************
+*              Do local deliveries               *
+*************************************************/
+
+/* This function processes the list of addresses in addr_local. True local
+deliveries are always done one address at a time. However, local deliveries can
+be batched up in some cases. Typically this is when writing batched SMTP output
+files for use by some external transport mechanism, or when running local
+deliveries over LMTP.
+
+Arguments:   None
+Returns:     Nothing
+*/
+
+static void
+do_local_deliveries(void)
+{
+open_db dbblock;
+open_db *dbm_file = NULL;
+time_t now = time(NULL);
+
+/* Loop until we have exhausted the supply of local deliveries */
+
+while (addr_local != NULL)
+  {
+  time_t delivery_start;
+  int deliver_time;
+  address_item *addr2, *addr3, *nextaddr;
+  int logflags = LOG_MAIN;
+  int logchar = dont_deliver? '*' : '=';
+  transport_instance *tp;
+
+  /* Pick the first undelivered address off the chain */
+
+  address_item *addr = addr_local;
+  addr_local = addr->next;
+  addr->next = NULL;
+
+  DEBUG(D_deliver|D_transport)
+    debug_printf("--------> %s <--------\n", addr->address);
+
+  /* An internal disaster if there is no transport. Should not occur! */
+
+  if ((tp = addr->transport) == NULL)
+    {
+    logflags |= LOG_PANIC;
+    disable_logging = FALSE;  /* Jic */
+    addr->message =
+      (addr->router != NULL)?
+        string_sprintf("No transport set by %s router", addr->router->name)
+        :
+        string_sprintf("No transport set by system filter");
+    post_process_one(addr, DEFER, logflags, DTYPE_TRANSPORT, 0);
+    continue;
+    }
+
+  /* Check that this base address hasn't previously been delivered to this
+  transport. The check is necessary at this point to handle homonymic addresses
+  correctly in cases where the pattern of redirection changes between delivery
+  attempts. Non-homonymic previous delivery is detected earlier, at routing
+  time. */
+
+  if (previously_transported(addr, FALSE)) continue;
+
+  /* There are weird cases where logging is disabled */
+
+  disable_logging = tp->disable_logging;
+
+  /* Check for batched addresses and possible amalgamation. Skip all the work
+  if either batch_max <= 1 or there aren't any other addresses for local
+  delivery. */
+
+  if (tp->batch_max > 1 && addr_local != NULL)
+    {
+    int batch_count = 1;
+    BOOL uses_dom = readconf_depends((driver_instance *)tp, US"domain");
+    BOOL uses_lp = (testflag(addr, af_pfr) &&
+      (testflag(addr, af_file) || addr->local_part[0] == '|')) ||
+      readconf_depends((driver_instance *)tp, US"local_part");
+    uschar *batch_id = NULL;
+    address_item **anchor = &addr_local;
+    address_item *last = addr;
+    address_item *next;
+
+    /* Expand the batch_id string for comparison with other addresses.
+    Expansion failure suppresses batching. */
+
+    if (tp->batch_id != NULL)
+      {
+      deliver_set_expansions(addr);
+      batch_id = expand_string(tp->batch_id);
+      deliver_set_expansions(NULL);
+      if (batch_id == NULL)
+        {
+        log_write(0, LOG_MAIN|LOG_PANIC, "Failed to expand batch_id option "
+          "in %s transport (%s): %s", tp->name, addr->address,
+          expand_string_message);
+        batch_count = tp->batch_max;
+        }
+      }
+
+    /* Until we reach the batch_max limit, pick off addresses which have the
+    same characteristics. These are:
+
+      same transport
+      not previously delivered (see comment about 50 lines above)
+      same local part if the transport's configuration contains $local_part
+        or if this is a file or pipe delivery from a redirection
+      same domain if the transport's configuration contains $domain
+      same errors address
+      same additional headers
+      same headers to be removed
+      same uid/gid for running the transport
+      same first host if a host list is set
+    */
+
+    while ((next = *anchor) != NULL && batch_count < tp->batch_max)
+      {
+      BOOL ok =
+        tp == next->transport &&
+        !previously_transported(next, TRUE) &&
+        (addr->flags & (af_pfr|af_file)) == (next->flags & (af_pfr|af_file)) &&
+        (!uses_lp  || Ustrcmp(next->local_part, addr->local_part) == 0) &&
+        (!uses_dom || Ustrcmp(next->domain, addr->domain) == 0) &&
+        same_strings(next->p.errors_address, addr->p.errors_address) &&
+        same_headers(next->p.extra_headers, addr->p.extra_headers) &&
+        same_strings(next->p.remove_headers, addr->p.remove_headers) &&
+        same_ugid(tp, addr, next) &&
+        ((addr->host_list == NULL && next->host_list == NULL) ||
+         (addr->host_list != NULL && next->host_list != NULL &&
+          Ustrcmp(addr->host_list->name, next->host_list->name) == 0));
+
+      /* If the transport has a batch_id setting, batch_id will be non-NULL
+      from the expansion outside the loop. Expand for this address and compare.
+      Expansion failure makes this address ineligible for batching. */
+
+      if (ok && batch_id != NULL)
+        {
+        uschar *bid;
+        address_item *save_nextnext = next->next;
+        next->next = NULL;            /* Expansion for a single address */
+        deliver_set_expansions(next);
+        next->next = save_nextnext;
+        bid = expand_string(tp->batch_id);
+        deliver_set_expansions(NULL);
+        if (bid == NULL)
+          {
+          log_write(0, LOG_MAIN|LOG_PANIC, "Failed to expand batch_id option "
+            "in %s transport (%s): %s", tp->name, next->address,
+            expand_string_message);
+          ok = FALSE;
+          }
+        else ok = (Ustrcmp(batch_id, bid) == 0);
+        }
+
+      /* Take address into batch if OK. */
+
+      if (ok)
+        {
+        *anchor = next->next;           /* Include the address */
+        next->next = NULL;
+        last->next = next;
+        last = next;
+        batch_count++;
+        }
+      else anchor = &(next->next);      /* Skip the address */
+      }
+    }
+
+  /* We now have one or more addresses that can be delivered in a batch. Check
+  whether the transport is prepared to accept a message of this size. If not,
+  fail them all forthwith. If the expansion fails, or does not yield an
+  integer, defer delivery. */
+
+  if (tp->message_size_limit != NULL)
+    {
+    int rc = check_message_size(tp, addr);
+    if (rc != OK)
+      {
+      replicate_status(addr);
+      while (addr != NULL)
+        {
+        addr2 = addr->next;
+        post_process_one(addr, rc, logflags, DTYPE_TRANSPORT, 0);
+        addr = addr2;
+        }
+      continue;    /* With next batch of addresses */
+      }
+    }
+
+  /* If we are not running the queue, or if forcing, all deliveries will be
+  attempted. Otherwise, we must respect the retry times for each address. Even
+  when not doing this, we need to set up the retry key string, and determine
+  whether a retry record exists, because after a successful delivery, a delete
+  retry item must be set up. Keep the retry database open only for the duration
+  of these checks, rather than for all local deliveries, because some local
+  deliveries (e.g. to pipes) can take a substantial time. */
+
+  dbm_file = dbfn_open(US"retry", O_RDONLY, &dbblock, FALSE);
+  if (dbm_file == NULL)
+    {
+    DEBUG(D_deliver|D_retry|D_hints_lookup)
+      debug_printf("no retry data available\n");
+    }
+
+  addr2 = addr;
+  addr3 = NULL;
+  while (addr2 != NULL)
+    {
+    BOOL ok = TRUE;   /* to deliver this address */
+    uschar *retry_key;
+
+    /* Set up the retry key to include the domain or not, and change its
+    leading character from "R" to "T". Must make a copy before doing this,
+    because the old key may be pointed to from a "delete" retry item after
+    a routing delay. */
+
+    retry_key = string_copy(
+      (tp->retry_use_local_part)? addr2->address_retry_key :
+        addr2->domain_retry_key);
+    *retry_key = 'T';
+
+    /* Inspect the retry data. If there is no hints file, delivery happens. */
+
+    if (dbm_file != NULL)
+      {
+      dbdata_retry *retry_record = dbfn_read(dbm_file, retry_key);
+
+      /* If there is no retry record, delivery happens. If there is,
+      remember it exists so it can be deleted after a successful delivery. */
+
+      if (retry_record != NULL)
+        {
+        setflag(addr2, af_lt_retry_exists);
+
+        /* A retry record exists for this address. If queue running and not
+        forcing, inspect its contents. If the record is too old, or if its
+        retry time has come, or if it has passed its cutoff time, delivery
+        will go ahead. */
+
+        DEBUG(D_retry)
+          {
+          debug_printf("retry record exists: age=%s ",
+            readconf_printtime(now - retry_record->time_stamp));
+          debug_printf("(max %s)\n", readconf_printtime(retry_data_expire));
+          debug_printf("  time to retry = %s expired = %d\n",
+            readconf_printtime(retry_record->next_try - now),
+            retry_record->expired);
+          }
+
+        if (queue_running && !deliver_force)
+          {
+          ok = (now - retry_record->time_stamp > retry_data_expire) ||
+               (now >= retry_record->next_try) ||
+               retry_record->expired;
+
+          /* If we haven't reached the retry time, there is one more check
+          to do, which is for the ultimate address timeout. */
+
+          if (!ok)
+            ok = retry_ultimate_address_timeout(retry_key, addr2->domain,
+                retry_record, now);
+          }
+        }
+      else DEBUG(D_retry) debug_printf("no retry record exists\n");
+      }
+
+    /* This address is to be delivered. Leave it on the chain. */
+
+    if (ok)
+      {
+      addr3 = addr2;
+      addr2 = addr2->next;
+      }
+
+    /* This address is to be deferred. Take it out of the chain, and
+    post-process it as complete. Must take it out of the chain first,
+    because post processing puts it on another chain. */
+
+    else
+      {
+      address_item *this = addr2;
+      this->message = US"Retry time not yet reached";
+      this->basic_errno = ERRNO_LRETRY;
+      if (addr3 == NULL) addr2 = addr = addr2->next;
+        else addr2 = addr3->next = addr2->next;
+      post_process_one(this, DEFER, logflags, DTYPE_TRANSPORT, 0);
+      }
+    }
+
+  if (dbm_file != NULL) dbfn_close(dbm_file);
+
+  /* If there are no addresses left on the chain, they all deferred. Loop
+  for the next set of addresses. */
+
+  if (addr == NULL) continue;
+
+  /* So, finally, we do have some addresses that can be passed to the
+  transport. Before doing so, set up variables that are relevant to a
+  single delivery. */
+
+  deliver_set_expansions(addr);
+  delivery_start = time(NULL);
+  deliver_local(addr, FALSE);
+  deliver_time = (int)(time(NULL) - delivery_start);
+
+  /* If a shadow transport (which must perforce be another local transport), is
+  defined, and its condition is met, we must pass the message to the shadow
+  too, but only those addresses that succeeded. We do this by making a new
+  chain of addresses - also to keep the original chain uncontaminated. We must
+  use a chain rather than doing it one by one, because the shadow transport may
+  batch.
+
+  NOTE: if the condition fails because of a lookup defer, there is nothing we
+  can do! */
+
+  if (tp->shadow != NULL &&
+      (tp->shadow_condition == NULL ||
+      expand_check_condition(tp->shadow_condition, tp->name, US"transport")))
+    {
+    transport_instance *stp;
+    address_item *shadow_addr = NULL;
+    address_item **last = &shadow_addr;
+
+    for (stp = transports; stp != NULL; stp = stp->next)
+      if (Ustrcmp(stp->name, tp->shadow) == 0) break;
+
+    if (stp == NULL)
+      log_write(0, LOG_MAIN|LOG_PANIC, "shadow transport \"%s\" not found ",
+        tp->shadow);
+
+    /* Pick off the addresses that have succeeded, and make clones. Put into
+    the shadow_message field a pointer to the shadow_message field of the real
+    address. */
+
+    else for (addr2 = addr; addr2 != NULL; addr2 = addr2->next)
+      {
+      if (addr2->transport_return != OK) continue;
+      addr3 = store_get(sizeof(address_item));
+      *addr3 = *addr2;
+      addr3->next = NULL;
+      addr3->shadow_message = (uschar *)(&(addr2->shadow_message));
+      addr3->transport = stp;
+      addr3->transport_return = DEFER;
+      addr3->return_filename = NULL;
+      addr3->return_file = -1;
+      *last = addr3;
+      last = &(addr3->next);
+      }
+
+    /* If we found any addresses to shadow, run the delivery, and stick any
+    message back into the shadow_message field in the original. */
+
+    if (shadow_addr != NULL)
+      {
+      int save_count = transport_count;
+
+      DEBUG(D_deliver|D_transport)
+        debug_printf(">>>>>>>>>>>>>>>> Shadow delivery >>>>>>>>>>>>>>>>\n");
+      deliver_local(shadow_addr, TRUE);
+
+      for(; shadow_addr != NULL; shadow_addr = shadow_addr->next)
+        {
+        int sresult = shadow_addr->transport_return;
+        *((uschar **)(shadow_addr->shadow_message)) = (sresult == OK)?
+          string_sprintf(" ST=%s", stp->name) :
+          string_sprintf(" ST=%s (%s%s%s)", stp->name,
+            (shadow_addr->basic_errno <= 0)?
+              US"" : US strerror(shadow_addr->basic_errno),
+            (shadow_addr->basic_errno <= 0 || shadow_addr->message == NULL)?
+              US"" : US": ",
+            (shadow_addr->message != NULL)? shadow_addr->message :
+              (shadow_addr->basic_errno <= 0)? US"unknown error" : US"");
+
+        DEBUG(D_deliver|D_transport)
+          debug_printf("%s shadow transport returned %s for %s\n",
+            stp->name,
+            (sresult == OK)?    "OK" :
+            (sresult == DEFER)? "DEFER" :
+            (sresult == FAIL)?  "FAIL" :
+            (sresult == PANIC)? "PANIC" : "?",
+            shadow_addr->address);
+        }
+
+      DEBUG(D_deliver|D_transport)
+        debug_printf(">>>>>>>>>>>>>>>> End shadow delivery >>>>>>>>>>>>>>>>\n");
+
+      transport_count = save_count;   /* Restore original transport count */
+      }
+    }
+
+  /* Cancel the expansions that were set up for the delivery. */
+
+  deliver_set_expansions(NULL);
+
+  /* Now we can process the results of the real transport. We must take each
+  address off the chain first, because post_process_one() puts it on another
+  chain. */
+
+  for (addr2 = addr; addr2 != NULL; addr2 = nextaddr)
+    {
+    int result = addr2->transport_return;
+    nextaddr = addr2->next;
+
+    DEBUG(D_deliver|D_transport)
+      debug_printf("%s transport returned %s for %s\n",
+        tp->name,
+        (result == OK)?    "OK" :
+        (result == DEFER)? "DEFER" :
+        (result == FAIL)?  "FAIL" :
+        (result == PANIC)? "PANIC" : "?",
+        addr2->address);
+
+    /* If there is a retry_record, or if delivery is deferred, build a retry
+    item for setting a new retry time or deleting the old retry record from
+    the database. These items are handled all together after all addresses
+    have been handled (so the database is open just for a short time for
+    updating). */
+
+    if (result == DEFER || testflag(addr2, af_lt_retry_exists))
+      {
+      int flags = (result == DEFER)? 0 : rf_delete;
+      uschar *retry_key = string_copy((tp->retry_use_local_part)?
+        addr2->address_retry_key : addr2->domain_retry_key);
+      *retry_key = 'T';
+      retry_add_item(addr2, retry_key, flags);
+      }
+
+    /* Done with this address */
+
+    if (result == OK) addr2->more_errno = deliver_time;
+    post_process_one(addr2, result, logflags, DTYPE_TRANSPORT, logchar);
+
+    /* If a pipe delivery generated text to be sent back, the result may be
+    changed to FAIL, and we must copy this for subsequent addresses in the
+    batch. */
+
+    if (addr2->transport_return != result)
+      {
+      for (addr3 = nextaddr; addr3 != NULL; addr3 = addr3->next)
+        {
+        addr3->transport_return = addr2->transport_return;
+        addr3->basic_errno = addr2->basic_errno;
+        addr3->message = addr2->message;
+        }
+      result = addr2->transport_return;
+      }
+
+    /* Whether or not the result was changed to FAIL, we need to copy the
+    return_file value from the first address into all the addresses of the
+    batch, so they are all listed in the error message. */
+
+    addr2->return_file = addr->return_file;
+
+    /* Change log character for recording successful deliveries. */
+
+    if (result == OK) logchar = '-';
+    }
+  }        /* Loop back for next batch of addresses */
+}
+
+
+
+
+/*************************************************
+*           Sort remote deliveries               *
+*************************************************/
+
+/* This function is called if remote_sort_domains is set. It arranges that the
+chain of addresses for remote deliveries is ordered according to the strings
+specified. Try to make this shuffling reasonably efficient by handling
+sequences of addresses rather than just single ones.
+
+Arguments:  None
+Returns:    Nothing
+*/
+
+static void
+sort_remote_deliveries(void)
+{
+int sep = 0;
+address_item **aptr = &addr_remote;
+uschar *listptr = remote_sort_domains;
+uschar *pattern;
+uschar patbuf[256];
+
+while (*aptr != NULL &&
+       (pattern = string_nextinlist(&listptr, &sep, patbuf, sizeof(patbuf)))
+       != NULL)
+  {
+  address_item *moved = NULL;
+  address_item **bptr = &moved;
+
+  while (*aptr != NULL)
+    {
+    address_item **next;
+    deliver_domain = (*aptr)->domain;   /* set $domain */
+    if (match_isinlist(deliver_domain, &pattern, UCHAR_MAX+1,
+          &domainlist_anchor, NULL, MCL_DOMAIN, TRUE, NULL) == OK)
+      {
+      aptr = &((*aptr)->next);
+      continue;
+      }
+
+    next = &((*aptr)->next);
+    while (*next != NULL &&
+           (deliver_domain = (*next)->domain,  /* Set $domain */
+            match_isinlist(deliver_domain, &pattern, UCHAR_MAX+1,
+              &domainlist_anchor, NULL, MCL_DOMAIN, TRUE, NULL)) != OK)
+      next = &((*next)->next);
+
+    /* If the batch of non-matchers is at the end, add on any that were
+    extracted further up the chain, and end this iteration. Otherwise,
+    extract them from the chain and hang on the moved chain. */
+
+    if (*next == NULL)
+      {
+      *next = moved;
+      break;
+      }
+
+    *bptr = *aptr;
+    *aptr = *next;
+    *next = NULL;
+    bptr = next;
+    aptr = &((*aptr)->next);
+    }
+
+  /* If the loop ended because the final address matched, *aptr will
+  be NULL. Add on to the end any extracted non-matching addresses. If
+  *aptr is not NULL, the loop ended via "break" when *next is null, that
+  is, there was a string of non-matching addresses at the end. In this
+  case the extracted addresses have already been added on the end. */
+
+  if (*aptr == NULL) *aptr = moved;
+  }
+
+DEBUG(D_deliver)
+  {
+  address_item *addr;
+  debug_printf("remote addresses after sorting:\n");
+  for (addr = addr_remote; addr != NULL; addr = addr->next)
+    debug_printf("  %s\n", addr->address);
+  }
+}
+
+
+
+/*************************************************
+*  Read from pipe for remote delivery subprocess *
+*************************************************/
+
+/* This function is called when the subprocess is complete, but can also be
+called before it is complete, in order to empty a pipe that is full (to prevent
+deadlock). It must therefore keep track of its progress in the parlist data
+block.
+
+We read the pipe to get the delivery status codes and a possible error message
+for each address, optionally preceded by unusability data for the hosts and
+also by optional retry data.
+
+Read in large chunks into the big buffer and then scan through, interpreting
+the data therein. In most cases, only a single read will be necessary. No
+individual item will ever be anywhere near 2500 bytes in length, so by ensuring
+that we read the next chunk when there is less than 2500 bytes left in the
+non-final chunk, we can assume each item is complete in the buffer before
+handling it. Each item is written using a single write(), which is atomic for
+small items (less than PIPE_BUF, which seems to be at least 512 in any Unix and
+often bigger) so even if we are reading while the subprocess is still going, we
+should never have only a partial item in the buffer.
+
+Argument:
+  poffset     the offset of the parlist item
+  eop         TRUE if the process has completed
+
+Returns:      TRUE if the terminating 'Z' item has been read,
+              or there has been a disaster (i.e. no more data needed);
+              FALSE otherwise
+*/
+
+static BOOL
+par_read_pipe(int poffset, BOOL eop)
+{
+host_item *h;
+pardata *p = parlist + poffset;
+address_item *addrlist = p->addrlist;
+address_item *addr = p->addr;
+pid_t pid = p->pid;
+int fd = p->fd;
+uschar *endptr = big_buffer;
+uschar *ptr = endptr;
+uschar *msg = p->msg;
+BOOL done = p->done;
+BOOL unfinished = TRUE;
+
+/* Loop through all items, reading from the pipe when necessary. The pipe
+is set up to be non-blocking, but there are two different Unix mechanisms in
+use. Exim uses O_NONBLOCK if it is defined. This returns 0 for end of file,
+and EAGAIN for no more data. If O_NONBLOCK is not defined, Exim uses O_NDELAY,
+which returns 0 for both end of file and no more data. We distinguish the
+two cases by taking 0 as end of file only when we know the process has
+completed.
+
+Each separate item is written to the pipe in a single write(), and as they are
+all short items, the writes will all be atomic and we should never find
+ourselves in the position of having read an incomplete item. "Short" in this
+case can mean up to about 1K in the case when there is a long error message
+associated with an address. */
+
+DEBUG(D_deliver) debug_printf("reading pipe for subprocess %d (%s)\n",
+  (int)p->pid, eop? "ended" : "not ended");
+
+while (!done)
+  {
+  retry_item *r, **rp;
+  int remaining = endptr - ptr;
+
+  /* Read (first time) or top up the chars in the buffer if necessary.
+  There will be only one read if we get all the available data (i.e. don't
+  fill the buffer completely). */
+
+  if (remaining < 2500 && unfinished)
+    {
+    int len;
+    int available = big_buffer_size - remaining;
+
+    if (remaining > 0) memmove(big_buffer, ptr, remaining);
+
+    ptr = big_buffer;
+    endptr = big_buffer + remaining;
+    len = read(fd, endptr, available);
+
+    DEBUG(D_deliver) debug_printf("read() yielded %d\n", len);
+
+    /* If the result is EAGAIN and the process is not complete, just
+    stop reading any more and process what we have already. */
+
+    if (len < 0)
+      {
+      if (!eop && errno == EAGAIN) len = 0; else
+        {
+        msg = string_sprintf("failed to read pipe from transport process "
+          "%d for transport %s: %s", pid, addr->transport->driver_name,
+          strerror(errno));
+        break;
+        }
+      }
+
+    /* If the length is zero (eof or no-more-data), just process what we
+    already have. Note that if the process is still running and we have
+    read all the data in the pipe (but less that "available") then we
+    won't read any more, as "unfinished" will get set FALSE. */
+
+    endptr += len;
+    unfinished = len == available;
+    }
+
+  /* If we are at the end of the available data, exit the loop. */
+
+  if (ptr >= endptr) break;
+
+  /* Handle each possible type of item, assuming the complete item is
+  available in store. */
+
+  switch (*ptr++)
+    {
+    /* Host items exist only if any hosts were marked unusable. Match
+    up by checking the IP address. */
+
+    case 'H':
+    for (h = addrlist->host_list; h != NULL; h = h->next)
+      {
+      if (h->address == NULL || Ustrcmp(h->address, ptr+2) != 0) continue;
+      h->status = ptr[0];
+      h->why = ptr[1];
+      }
+    ptr += 2;
+    while (*ptr++);
+    break;
+
+    /* Retry items are sent in a preceding R item for each address. This is
+    kept separate to keep each message short enough to guarantee it won't
+    be split in the pipe. Hopefully, in the majority of cases, there won't in
+    fact be any retry items at all.
+
+    The complete set of retry items might include an item to delete a
+    routing retry if there was a previous routing delay. However, routing
+    retries are also used when a remote transport identifies an address error.
+    In that case, there may also be an "add" item for the same key. Arrange
+    that a "delete" item is dropped in favour of an "add" item. */
+
+    case 'R':
+    if (addr == NULL) goto ADDR_MISMATCH;
+
+    DEBUG(D_deliver|D_retry)
+      debug_printf("reading retry information for %s from subprocess\n",
+        ptr+1);
+
+    /* Cut out any "delete" items on the list. */
+
+    for (rp = &(addr->retries); (r = *rp) != NULL; rp = &(r->next))
+      {
+      if (Ustrcmp(r->key, ptr+1) == 0)           /* Found item with same key */
+        {
+        if ((r->flags & rf_delete) == 0) break;  /* It was not "delete" */
+        *rp = r->next;                           /* Excise a delete item */
+        DEBUG(D_deliver|D_retry)
+          debug_printf("  existing delete item dropped\n");
+        }
+      }
+
+    /* We want to add a delete item only if there is no non-delete item;
+    however we still have to step ptr through the data. */
+
+    if (r == NULL || (*ptr & rf_delete) == 0)
+      {
+      r = store_get(sizeof(retry_item));
+      r->next = addr->retries;
+      addr->retries = r;
+      r->flags = *ptr++;
+      r->key = string_copy(ptr);
+      while (*ptr++);
+      memcpy(&(r->basic_errno), ptr, sizeof(r->basic_errno));
+      ptr += sizeof(r->basic_errno);
+      memcpy(&(r->more_errno), ptr, sizeof(r->more_errno));
+      ptr += sizeof(r->more_errno);
+      r->message = (*ptr)? string_copy(ptr) : NULL;
+      DEBUG(D_deliver|D_retry)
+        debug_printf("  added %s item\n",
+          ((r->flags & rf_delete) == 0)? "retry" : "delete");
+      }
+
+    else
+      {
+      DEBUG(D_deliver|D_retry)
+        debug_printf("  delete item not added: non-delete item exists\n");
+      ptr++;
+      while(*ptr++);
+      ptr += sizeof(r->basic_errno) + sizeof(r->more_errno);
+      }
+
+    while(*ptr++);
+    break;
+
+    /* Put the amount of data written into the parlist block */
+
+    case 'S':
+    memcpy(&(p->transport_count), ptr, sizeof(transport_count));
+    ptr += sizeof(transport_count);
+    break;
+
+    /* Address items are in the order of items on the address chain. We
+    remember the current address value in case this function is called
+    several times to empty the pipe in stages. Information about delivery
+    over TLS is sent in a preceding X item for each address. We don't put
+    it in with the other info, in order to keep each message short enough to
+    guarantee it won't be split in the pipe. */
+
+    #ifdef SUPPORT_TLS
+    case 'X':
+    if (addr == NULL) goto ADDR_MISMATCH;          /* Below, in 'A' handler */
+    switch (*ptr++)
+      {
+      case '1':
+      addr->cipher = NULL;
+      addr->peerdn = NULL;
+
+      if (*ptr)
+       addr->cipher = string_copy(ptr);
+      while (*ptr++);
+      if (*ptr)
+       addr->peerdn = string_copy(ptr);
+      break;
+
+      case '2':
+      addr->peercert = NULL;
+      if (*ptr)
+       (void) tls_import_cert(ptr, &addr->peercert);
+      break;
+
+      case '3':
+      addr->ourcert = NULL;
+      if (*ptr)
+       (void) tls_import_cert(ptr, &addr->ourcert);
+      break;
+
+      #ifndef DISABLE_OCSP
+      case '4':
+      addr->ocsp = OCSP_NOT_REQ;
+      if (*ptr)
+       addr->ocsp = *ptr - '0';
+      break;
+      #endif
+      }
+    while (*ptr++);
+    break;
+    #endif     /*SUPPORT_TLS*/
+
+    case 'C':  /* client authenticator information */
+    switch (*ptr++)
+      {
+      case '1':
+       addr->authenticator = (*ptr)? string_copy(ptr) : NULL;
+       break;
+      case '2':
+       addr->auth_id = (*ptr)? string_copy(ptr) : NULL;
+       break;
+      case '3':
+       addr->auth_sndr = (*ptr)? string_copy(ptr) : NULL;
+       break;
+      }
+    while (*ptr++);
+    break;
+
+#ifndef DISABLE_PRDR
+    case 'P':
+    addr->flags |= af_prdr_used;
+    break;
+#endif
+
+    #ifdef EXPERIMENTAL_DSN
+    case 'D':
+    if (addr == NULL) break;
+    memcpy(&(addr->dsn_aware), ptr, sizeof(addr->dsn_aware));
+    ptr += sizeof(addr->dsn_aware);
+    DEBUG(D_deliver) debug_printf("DSN read: addr->dsn_aware = %d\n", addr->dsn_aware);
+    break;
+    #endif
+
+    case 'A':
+    if (addr == NULL)
+      {
+      ADDR_MISMATCH:
+      msg = string_sprintf("address count mismatch for data read from pipe "
+        "for transport process %d for transport %s", pid,
+          addrlist->transport->driver_name);
+      done = TRUE;
+      break;
+      }
+
+    addr->transport_return = *ptr++;
+    addr->special_action = *ptr++;
+    memcpy(&(addr->basic_errno), ptr, sizeof(addr->basic_errno));
+    ptr += sizeof(addr->basic_errno);
+    memcpy(&(addr->more_errno), ptr, sizeof(addr->more_errno));
+    ptr += sizeof(addr->more_errno);
+    memcpy(&(addr->flags), ptr, sizeof(addr->flags));
+    ptr += sizeof(addr->flags);
+    addr->message = (*ptr)? string_copy(ptr) : NULL;
+    while(*ptr++);
+    addr->user_message = (*ptr)? string_copy(ptr) : NULL;
+    while(*ptr++);
+
+    /* Always two strings for host information, followed by the port number and DNSSEC mark */
+
+    if (*ptr != 0)
+      {
+      h = store_get(sizeof(host_item));
+      h->name = string_copy(ptr);
+      while (*ptr++);
+      h->address = string_copy(ptr);
+      while(*ptr++);
+      memcpy(&(h->port), ptr, sizeof(h->port));
+      ptr += sizeof(h->port);
+      h->dnssec = *ptr == '2' ? DS_YES
+               : *ptr == '1' ? DS_NO
+               : DS_UNK;
+      ptr++;
+      addr->host_used = h;
+      }
+    else ptr++;
+
+    /* Finished with this address */
+
+    addr = addr->next;
+    break;
+
+    /* Z marks the logical end of the data. It is followed by '0' if
+    continue_transport was NULL at the end of transporting, otherwise '1'.
+    We need to know when it becomes NULL during a delivery down a passed SMTP
+    channel so that we don't try to pass anything more down it. Of course, for
+    most normal messages it will remain NULL all the time. */
+
+    case 'Z':
+    if (*ptr == '0')
+      {
+      continue_transport = NULL;
+      continue_hostname = NULL;
+      }
+    done = TRUE;
+    DEBUG(D_deliver) debug_printf("Z%c item read\n", *ptr);
+    break;
+
+    /* Anything else is a disaster. */
+
+    default:
+    msg = string_sprintf("malformed data (%d) read from pipe for transport "
+      "process %d for transport %s", ptr[-1], pid,
+        addr->transport->driver_name);
+    done = TRUE;
+    break;
+    }
+  }
+
+/* The done flag is inspected externally, to determine whether or not to
+call the function again when the process finishes. */
+
+p->done = done;
+
+/* If the process hadn't finished, and we haven't seen the end of the data
+or suffered a disaster, update the rest of the state, and return FALSE to
+indicate "not finished". */
+
+if (!eop && !done)
+  {
+  p->addr = addr;
+  p->msg = msg;
+  return FALSE;
+  }
+
+/* Close our end of the pipe, to prevent deadlock if the far end is still
+pushing stuff into it. */
+
+(void)close(fd);
+p->fd = -1;
+
+/* If we have finished without error, but haven't had data for every address,
+something is wrong. */
+
+if (msg == NULL && addr != NULL)
+  msg = string_sprintf("insufficient address data read from pipe "
+    "for transport process %d for transport %s", pid,
+      addr->transport->driver_name);
+
+/* If an error message is set, something has gone wrong in getting back
+the delivery data. Put the message into each address and freeze it. */
+
+if (msg != NULL)
+  {
+  for (addr = addrlist; addr != NULL; addr = addr->next)
+    {
+    addr->transport_return = DEFER;
+    addr->special_action = SPECIAL_FREEZE;
+    addr->message = msg;
+    }
+  }
+
+/* Return TRUE to indicate we have got all we need from this process, even
+if it hasn't actually finished yet. */
+
+return TRUE;
+}
+
+
+
+/*************************************************
+*   Post-process a set of remote addresses       *
+*************************************************/
+
+/* Do what has to be done immediately after a remote delivery for each set of
+addresses, then re-write the spool if necessary. Note that post_process_one
+puts the address on an appropriate queue; hence we must fish off the next
+one first. This function is also called if there is a problem with setting
+up a subprocess to do a remote delivery in parallel. In this case, the final
+argument contains a message, and the action must be forced to DEFER.
+
+Argument:
+   addr      pointer to chain of address items
+   logflags  flags for logging
+   msg       NULL for normal cases; -> error message for unexpected problems
+   fallback  TRUE if processing fallback hosts
+
+Returns:     nothing
+*/
+
+static void
+remote_post_process(address_item *addr, int logflags, uschar *msg,
+  BOOL fallback)
+{
+host_item *h;
+
+/* If any host addresses were found to be unusable, add them to the unusable
+tree so that subsequent deliveries don't try them. */
+
+for (h = addr->host_list; h != NULL; h = h->next)
+  {
+  if (h->address == NULL) continue;
+  if (h->status >= hstatus_unusable) tree_add_unusable(h);
+  }
+
+/* Now handle each address on the chain. The transport has placed '=' or '-'
+into the special_action field for each successful delivery. */
+
+while (addr != NULL)
+  {
+  address_item *next = addr->next;
+
+  /* If msg == NULL (normal processing) and the result is DEFER and we are
+  processing the main hosts and there are fallback hosts available, put the
+  address on the list for fallback delivery. */
+
+  if (addr->transport_return == DEFER &&
+      addr->fallback_hosts != NULL &&
+      !fallback &&
+      msg == NULL)
+    {
+    addr->host_list = addr->fallback_hosts;
+    addr->next = addr_fallback;
+    addr_fallback = addr;
+    DEBUG(D_deliver) debug_printf("%s queued for fallback host(s)\n", addr->address);
+    }
+
+  /* If msg is set (=> unexpected problem), set it in the address before
+  doing the ordinary post processing. */
+
+  else
+    {
+    if (msg != NULL)
+      {
+      addr->message = msg;
+      addr->transport_return = DEFER;
+      }
+    (void)post_process_one(addr, addr->transport_return, logflags,
+      DTYPE_TRANSPORT, addr->special_action);
+    }
+
+  /* Next address */
+
+  addr = next;
+  }
+
+/* If we have just delivered down a passed SMTP channel, and that was
+the last address, the channel will have been closed down. Now that
+we have logged that delivery, set continue_sequence to 1 so that
+any subsequent deliveries don't get "*" incorrectly logged. */
+
+if (continue_transport == NULL) continue_sequence = 1;
+}
+
+
+
+/*************************************************
+*     Wait for one remote delivery subprocess    *
+*************************************************/
+
+/* This function is called while doing remote deliveries when either the
+maximum number of processes exist and we need one to complete so that another
+can be created, or when waiting for the last ones to complete. It must wait for
+the completion of one subprocess, empty the control block slot, and return a
+pointer to the address chain.
+
+Arguments:    none
+Returns:      pointer to the chain of addresses handled by the process;
+              NULL if no subprocess found - this is an unexpected error
+*/
+
+static address_item *
+par_wait(void)
+{
+int poffset, status;
+address_item *addr, *addrlist;
+pid_t pid;
+
+set_process_info("delivering %s: waiting for a remote delivery subprocess "
+  "to finish", message_id);
+
+/* Loop until either a subprocess completes, or there are no subprocesses in
+existence - in which case give an error return. We cannot proceed just by
+waiting for a completion, because a subprocess may have filled up its pipe, and
+be waiting for it to be emptied. Therefore, if no processes have finished, we
+wait for one of the pipes to acquire some data by calling select(), with a
+timeout just in case.
+
+The simple approach is just to iterate after reading data from a ready pipe.
+This leads to non-ideal behaviour when the subprocess has written its final Z
+item, closed the pipe, and is in the process of exiting (the common case). A
+call to waitpid() yields nothing completed, but select() shows the pipe ready -
+reading it yields EOF, so you end up with busy-waiting until the subprocess has
+actually finished.
+
+To avoid this, if all the data that is needed has been read from a subprocess
+after select(), an explicit wait() for it is done. We know that all it is doing
+is writing to the pipe and then exiting, so the wait should not be long.
+
+The non-blocking waitpid() is to some extent just insurance; if we could
+reliably detect end-of-file on the pipe, we could always know when to do a
+blocking wait() for a completed process. However, because some systems use
+NDELAY, which doesn't distinguish between EOF and pipe empty, it is easier to
+use code that functions without the need to recognize EOF.
+
+There's a double loop here just in case we end up with a process that is not in
+the list of remote delivery processes. Something has obviously gone wrong if
+this is the case. (For example, a process that is incorrectly left over from
+routing or local deliveries might be found.) The damage can be minimized by
+looping back and looking for another process. If there aren't any, the error
+return will happen. */
+
+for (;;)   /* Normally we do not repeat this loop */
+  {
+  while ((pid = waitpid(-1, &status, WNOHANG)) <= 0)
+    {
+    struct timeval tv;
+    fd_set select_pipes;
+    int maxpipe, readycount;
+
+    /* A return value of -1 can mean several things. If errno != ECHILD, it
+    either means invalid options (which we discount), or that this process was
+    interrupted by a signal. Just loop to try the waitpid() again.
+
+    If errno == ECHILD, waitpid() is telling us that there are no subprocesses
+    in existence. This should never happen, and is an unexpected error.
+    However, there is a nasty complication when running under Linux. If "strace
+    -f" is being used under Linux to trace this process and its children,
+    subprocesses are "stolen" from their parents and become the children of the
+    tracing process. A general wait such as the one we've just obeyed returns
+    as if there are no children while subprocesses are running. Once a
+    subprocess completes, it is restored to the parent, and waitpid(-1) finds
+    it. Thanks to Joachim Wieland for finding all this out and suggesting a
+    palliative.
+
+    This does not happen using "truss" on Solaris, nor (I think) with other
+    tracing facilities on other OS. It seems to be specific to Linux.
+
+    What we do to get round this is to use kill() to see if any of our
+    subprocesses are still in existence. If kill() gives an OK return, we know
+    it must be for one of our processes - it can't be for a re-use of the pid,
+    because if our process had finished, waitpid() would have found it. If any
+    of our subprocesses are in existence, we proceed to use select() as if
+    waitpid() had returned zero. I think this is safe. */
+
+    if (pid < 0)
+      {
+      if (errno != ECHILD) continue;   /* Repeats the waitpid() */
+
+      DEBUG(D_deliver)
+        debug_printf("waitpid() returned -1/ECHILD: checking explicitly "
+          "for process existence\n");
+
+      for (poffset = 0; poffset < remote_max_parallel; poffset++)
+        {
+        if ((pid = parlist[poffset].pid) != 0 && kill(pid, 0) == 0)
+          {
+          DEBUG(D_deliver) debug_printf("process %d still exists: assume "
+            "stolen by strace\n", (int)pid);
+          break;   /* With poffset set */
+          }
+        }
+
+      if (poffset >= remote_max_parallel)
+        {
+        DEBUG(D_deliver) debug_printf("*** no delivery children found\n");
+        return NULL;   /* This is the error return */
+        }
+      }
+
+    /* A pid value greater than 0 breaks the "while" loop. A negative value has
+    been handled above. A return value of zero means that there is at least one
+    subprocess, but there are no completed subprocesses. See if any pipes are
+    ready with any data for reading. */
+
+    DEBUG(D_deliver) debug_printf("selecting on subprocess pipes\n");
+
+    maxpipe = 0;
+    FD_ZERO(&select_pipes);
+    for (poffset = 0; poffset < remote_max_parallel; poffset++)
+      {
+      if (parlist[poffset].pid != 0)
+        {
+        int fd = parlist[poffset].fd;
+        FD_SET(fd, &select_pipes);
+        if (fd > maxpipe) maxpipe = fd;
+        }
+      }
+
+    /* Stick in a 60-second timeout, just in case. */
+
+    tv.tv_sec = 60;
+    tv.tv_usec = 0;
+
+    readycount = select(maxpipe + 1, (SELECT_ARG2_TYPE *)&select_pipes,
+         NULL, NULL, &tv);
+
+    /* Scan through the pipes and read any that are ready; use the count
+    returned by select() to stop when there are no more. Select() can return
+    with no processes (e.g. if interrupted). This shouldn't matter.
+
+    If par_read_pipe() returns TRUE, it means that either the terminating Z was
+    read, or there was a disaster. In either case, we are finished with this
+    process. Do an explicit wait() for the process and break the main loop if
+    it succeeds.
+
+    It turns out that we have to deal with the case of an interrupted system
+    call, which can happen on some operating systems if the signal handling is
+    set up to do that by default. */
+
+    for (poffset = 0;
+         readycount > 0 && poffset < remote_max_parallel;
+         poffset++)
+      {
+      if ((pid = parlist[poffset].pid) != 0 &&
+           FD_ISSET(parlist[poffset].fd, &select_pipes))
+        {
+        readycount--;
+        if (par_read_pipe(poffset, FALSE))    /* Finished with this pipe */
+          {
+          for (;;)                            /* Loop for signals */
+            {
+            pid_t endedpid = waitpid(pid, &status, 0);
+            if (endedpid == pid) goto PROCESS_DONE;
+            if (endedpid != (pid_t)(-1) || errno != EINTR)
+              log_write(0, LOG_MAIN|LOG_PANIC_DIE, "Unexpected error return "
+                "%d (errno = %d) from waitpid() for process %d",
+                (int)endedpid, errno, (int)pid);
+            }
+          }
+        }
+      }
+
+    /* Now go back and look for a completed subprocess again. */
+    }
+
+  /* A completed process was detected by the non-blocking waitpid(). Find the
+  data block that corresponds to this subprocess. */
+
+  for (poffset = 0; poffset < remote_max_parallel; poffset++)
+    if (pid == parlist[poffset].pid) break;
+
+  /* Found the data block; this is a known remote delivery process. We don't
+  need to repeat the outer loop. This should be what normally happens. */
+
+  if (poffset < remote_max_parallel) break;
+
+  /* This situation is an error, but it's probably better to carry on looking
+  for another process than to give up (as we used to do). */
+
+  log_write(0, LOG_MAIN|LOG_PANIC, "Process %d finished: not found in remote "
+    "transport process list", pid);
+  }  /* End of the "for" loop */
+
+/* Come here when all the data was completely read after a select(), and
+the process in pid has been wait()ed for. */
+
+PROCESS_DONE:
+
+DEBUG(D_deliver)
+  {
+  if (status == 0)
+    debug_printf("remote delivery process %d ended\n", (int)pid);
+  else
+    debug_printf("remote delivery process %d ended: status=%04x\n", (int)pid,
+      status);
+  }
+
+set_process_info("delivering %s", message_id);
+
+/* Get the chain of processed addresses */
+
+addrlist = parlist[poffset].addrlist;
+
+/* If the process did not finish cleanly, record an error and freeze (except
+for SIGTERM, SIGKILL and SIGQUIT), and also ensure the journal is not removed,
+in case the delivery did actually happen. */
+
+if ((status & 0xffff) != 0)
+  {
+  uschar *msg;
+  int msb = (status >> 8) & 255;
+  int lsb = status & 255;
+  int code = (msb == 0)? (lsb & 0x7f) : msb;
+
+  msg = string_sprintf("%s transport process returned non-zero status 0x%04x: "
+    "%s %d",
+    addrlist->transport->driver_name,
+    status,
+    (msb == 0)? "terminated by signal" : "exit code",
+    code);
+
+  if (msb != 0 || (code != SIGTERM && code != SIGKILL && code != SIGQUIT))
+    addrlist->special_action = SPECIAL_FREEZE;
+
+  for (addr = addrlist; addr != NULL; addr = addr->next)
+    {
+    addr->transport_return = DEFER;
+    addr->message = msg;
+    }
+
+  remove_journal = FALSE;
+  }
+
+/* Else complete reading the pipe to get the result of the delivery, if all
+the data has not yet been obtained. */
+
+else if (!parlist[poffset].done) (void)par_read_pipe(poffset, TRUE);
+
+/* Put the data count and return path into globals, mark the data slot unused,
+decrement the count of subprocesses, and return the address chain. */
+
+transport_count = parlist[poffset].transport_count;
+used_return_path = parlist[poffset].return_path;
+parlist[poffset].pid = 0;
+parcount--;
+return addrlist;
+}
+
+
+
+/*************************************************
+*      Wait for subprocesses and post-process    *
+*************************************************/
+
+/* This function waits for subprocesses until the number that are still running
+is below a given threshold. For each complete subprocess, the addresses are
+post-processed. If we can't find a running process, there is some shambles.
+Better not bomb out, as that might lead to multiple copies of the message. Just
+log and proceed as if all done.
+
+Arguments:
+  max         maximum number of subprocesses to leave running
+  fallback    TRUE if processing fallback hosts
+
+Returns:      nothing
+*/
+
+static void
+par_reduce(int max, BOOL fallback)
+{
+while (parcount > max)
+  {
+  address_item *doneaddr = par_wait();
+  if (doneaddr == NULL)
+    {
+    log_write(0, LOG_MAIN|LOG_PANIC,
+      "remote delivery process count got out of step");
+    parcount = 0;
+    }
+  else remote_post_process(doneaddr, LOG_MAIN, NULL, fallback);
+  }
+}
+
+
+
+
+static void
+rmt_dlv_checked_write(int fd, void * buf, int size)
+{
+int ret = write(fd, buf, size);
+if(ret != size)
+  log_write(0, LOG_MAIN|LOG_PANIC_DIE, "Failed writing transport result to pipe: %s\n",
+    ret == -1 ? strerror(errno) : "short write");
+}
+
+/*************************************************
+*           Do remote deliveries                 *
+*************************************************/
+
+/* This function is called to process the addresses in addr_remote. We must
+pick off the queue all addresses that have the same transport, remote
+destination, and errors address, and hand them to the transport in one go,
+subject to some configured limitations. If this is a run to continue delivering
+to an existing delivery channel, skip all but those addresses that can go to
+that channel. The skipped addresses just get deferred.
+
+If mua_wrapper is set, all addresses must be able to be sent in a single
+transaction. If not, this function yields FALSE.
+
+In Exim 4, remote deliveries are always done in separate processes, even
+if remote_max_parallel = 1 or if there's only one delivery to do. The reason
+is so that the base process can retain privilege. This makes the
+implementation of fallback transports feasible (though not initially done.)
+
+We create up to the configured number of subprocesses, each of which passes
+back the delivery state via a pipe. (However, when sending down an existing
+connection, remote_max_parallel is forced to 1.)
+
+Arguments:
+  fallback  TRUE if processing fallback hosts
+
+Returns:    TRUE normally
+            FALSE if mua_wrapper is set and the addresses cannot all be sent
+              in one transaction
+*/
+
+static BOOL
+do_remote_deliveries(BOOL fallback)
+{
+int parmax;
+int delivery_count;
+int poffset;
+
+parcount = 0;    /* Number of executing subprocesses */
+
+/* When sending down an existing channel, only do one delivery at a time.
+We use a local variable (parmax) to hold the maximum number of processes;
+this gets reduced from remote_max_parallel if we can't create enough pipes. */
+
+if (continue_transport != NULL) remote_max_parallel = 1;
+parmax = remote_max_parallel;
+
+/* If the data for keeping a list of processes hasn't yet been
+set up, do so. */
+
+if (parlist == NULL)
+  {
+  parlist = store_get(remote_max_parallel * sizeof(pardata));
+  for (poffset = 0; poffset < remote_max_parallel; poffset++)
+    parlist[poffset].pid = 0;
+  }
+
+/* Now loop for each remote delivery */
+
+for (delivery_count = 0; addr_remote != NULL; delivery_count++)
+  {
+  pid_t pid;
+  uid_t uid;
+  gid_t gid;
+  int pfd[2];
+  int address_count = 1;
+  int address_count_max;
+  BOOL multi_domain;
+  BOOL use_initgroups;
+  BOOL pipe_done = FALSE;
+  transport_instance *tp;
+  address_item **anchor = &addr_remote;
+  address_item *addr = addr_remote;
+  address_item *last = addr;
+  address_item *next;
+
+  /* Pull the first address right off the list. */
+
+  addr_remote = addr->next;
+  addr->next = NULL;
+
+  DEBUG(D_deliver|D_transport)
+    debug_printf("--------> %s <--------\n", addr->address);
+
+  /* If no transport has been set, there has been a big screw-up somewhere. */
+
+  if ((tp = addr->transport) == NULL)
+    {
+    disable_logging = FALSE;  /* Jic */
+    remote_post_process(addr, LOG_MAIN|LOG_PANIC,
+      US"No transport set by router", fallback);
+    continue;
+    }
+
+  /* Check that this base address hasn't previously been delivered to this
+  transport. The check is necessary at this point to handle homonymic addresses
+  correctly in cases where the pattern of redirection changes between delivery
+  attempts. Non-homonymic previous delivery is detected earlier, at routing
+  time. */
+
+  if (previously_transported(addr, FALSE)) continue;
+
+  /* Force failure if the message is too big. */
+
+  if (tp->message_size_limit != NULL)
+    {
+    int rc = check_message_size(tp, addr);
+    if (rc != OK)
+      {
+      addr->transport_return = rc;
+      remote_post_process(addr, LOG_MAIN, NULL, fallback);
+      continue;
+      }
+    }
+
+  /* Get the flag which specifies whether the transport can handle different
+  domains that nevertheless resolve to the same set of hosts. */
+
+  multi_domain = tp->multi_domain;
+
+  /* Get the maximum it can handle in one envelope, with zero meaning
+  unlimited, which is forced for the MUA wrapper case. */
+
+  address_count_max = tp->max_addresses;
+  if (address_count_max == 0 || mua_wrapper) address_count_max = 999999;
+
+
+  /************************************************************************/
+  /*****    This is slightly experimental code, but should be safe.   *****/
+
+  /* The address_count_max value is the maximum number of addresses that the
+  transport can send in one envelope. However, the transport must be capable of
+  dealing with any number of addresses. If the number it gets exceeds its
+  envelope limitation, it must send multiple copies of the message. This can be
+  done over a single connection for SMTP, so uses less resources than making
+  multiple connections. On the other hand, if remote_max_parallel is greater
+  than one, it is perhaps a good idea to use parallel processing to move the
+  message faster, even if that results in multiple simultaneous connections to
+  the same host.
+
+  How can we come to some compromise between these two ideals? What we do is to
+  limit the number of addresses passed to a single instance of a transport to
+  the greater of (a) its address limit (rcpt_max for SMTP) and (b) the total
+  number of addresses routed to remote transports divided by
+  remote_max_parallel. For example, if the message has 100 remote recipients,
+  remote max parallel is 2, and rcpt_max is 10, we'd never send more than 50 at
+  once. But if rcpt_max is 100, we could send up to 100.
+
+  Of course, not all the remotely addresses in a message are going to go to the
+  same set of hosts (except in smarthost configurations), so this is just a
+  heuristic way of dividing up the work.
+
+  Furthermore (1), because this may not be wanted in some cases, and also to
+  cope with really pathological cases, there is also a limit to the number of
+  messages that are sent over one connection. This is the same limit that is
+  used when sending several different messages over the same connection.
+  Continue_sequence is set when in this situation, to the number sent so
+  far, including this message.
+
+  Furthermore (2), when somebody explicitly sets the maximum value to 1, it
+  is probably because they are using VERP, in which case they want to pass only
+  one address at a time to the transport, in order to be able to use
+  $local_part and $domain in constructing a new return path. We could test for
+  the use of these variables, but as it is so likely they will be used when the
+  maximum is 1, we don't bother. Just leave the value alone. */
+
+  if (address_count_max != 1 &&
+      address_count_max < remote_delivery_count/remote_max_parallel)
+    {
+    int new_max = remote_delivery_count/remote_max_parallel;
+    int message_max = tp->connection_max_messages;
+    if (connection_max_messages >= 0) message_max = connection_max_messages;
+    message_max -= continue_sequence - 1;
+    if (message_max > 0 && new_max > address_count_max * message_max)
+      new_max = address_count_max * message_max;
+    address_count_max = new_max;
+    }
+
+  /************************************************************************/
+
+
+  /* Pick off all addresses which have the same transport, errors address,
+  destination, and extra headers. In some cases they point to the same host
+  list, but we also need to check for identical host lists generated from
+  entirely different domains. The host list pointers can be NULL in the case
+  where the hosts are defined in the transport. There is also a configured
+  maximum limit of addresses that can be handled at once (see comments above
+  for how it is computed). */
+
+  while ((next = *anchor) != NULL && address_count < address_count_max)
+    {
+    if ((multi_domain || Ustrcmp(next->domain, addr->domain) == 0)
+        &&
+        tp == next->transport
+        &&
+        same_hosts(next->host_list, addr->host_list)
+        &&
+        same_strings(next->p.errors_address, addr->p.errors_address)
+        &&
+        same_headers(next->p.extra_headers, addr->p.extra_headers)
+        &&
+        same_ugid(tp, next, addr)
+        &&
+        (next->p.remove_headers == addr->p.remove_headers ||
+          (next->p.remove_headers != NULL &&
+           addr->p.remove_headers != NULL &&
+           Ustrcmp(next->p.remove_headers, addr->p.remove_headers) == 0)))
+      {
+      *anchor = next->next;
+      next->next = NULL;
+      next->first = addr;  /* remember top one (for retry processing) */
+      last->next = next;
+      last = next;
+      address_count++;
+      }
+    else anchor = &(next->next);
+    }
+
+  /* If we are acting as an MUA wrapper, all addresses must go in a single
+  transaction. If not, put them back on the chain and yield FALSE. */
+
+  if (mua_wrapper && addr_remote != NULL)
+    {
+    last->next = addr_remote;
+    addr_remote = addr;
+    return FALSE;
+    }
+
+  /* Set up the expansion variables for this set of addresses */
+
+  deliver_set_expansions(addr);
+
+  /* Ensure any transport-set auth info is fresh */
+  addr->authenticator = addr->auth_id = addr->auth_sndr = NULL;
+
+  /* Compute the return path, expanding a new one if required. The old one
+  must be set first, as it might be referred to in the expansion. */
+
+  if(addr->p.errors_address != NULL)
+    return_path = addr->p.errors_address;
+#ifdef EXPERIMENTAL_SRS
+  else if(addr->p.srs_sender != NULL)
+    return_path = addr->p.srs_sender;
+#endif
+  else
+    return_path = sender_address;
+
+  if (tp->return_path != NULL)
+    {
+    uschar *new_return_path = expand_string(tp->return_path);
+    if (new_return_path == NULL)
+      {
+      if (!expand_string_forcedfail)
+        {
+        remote_post_process(addr, LOG_MAIN|LOG_PANIC,
+          string_sprintf("Failed to expand return path \"%s\": %s",
+          tp->return_path, expand_string_message), fallback);
+        continue;
+        }
+      }
+    else return_path = new_return_path;
+    }
+
+  /* Find the uid, gid, and use_initgroups setting for this transport. Failure
+  logs and sets up error messages, so we just post-process and continue with
+  the next address. */
+
+  if (!findugid(addr, tp, &uid, &gid, &use_initgroups))
+    {
+    remote_post_process(addr, LOG_MAIN|LOG_PANIC, NULL, fallback);
+    continue;
+    }
+
+  /* If this transport has a setup function, call it now so that it gets
+  run in this process and not in any subprocess. That way, the results of
+  any setup that are retained by the transport can be reusable. One of the
+  things the setup does is to set the fallback host lists in the addresses.
+  That is why it is called at this point, before the continue delivery
+  processing, because that might use the fallback hosts. */
+
+  if (tp->setup != NULL)
+    (void)((tp->setup)(addr->transport, addr, NULL, uid, gid, NULL));
+
+  /* If this is a run to continue delivery down an already-established
+  channel, check that this set of addresses matches the transport and
+  the channel. If it does not, defer the addresses. If a host list exists,
+  we must check that the continue host is on the list. Otherwise, the
+  host is set in the transport. */
+
+  continue_more = FALSE;           /* In case got set for the last lot */
+  if (continue_transport != NULL)
+    {
+    BOOL ok = Ustrcmp(continue_transport, tp->name) == 0;
+    if (ok && addr->host_list != NULL)
+      {
+      host_item *h;
+      ok = FALSE;
+      for (h = addr->host_list; h != NULL; h = h->next)
+        {
+        if (Ustrcmp(h->name, continue_hostname) == 0)
+          { ok = TRUE; break; }
+        }
+      }
+
+    /* Addresses not suitable; defer or queue for fallback hosts (which
+    might be the continue host) and skip to next address. */
+
+    if (!ok)
+      {
+      DEBUG(D_deliver) debug_printf("not suitable for continue_transport\n");
+      next = addr;
+
+      if (addr->fallback_hosts != NULL && !fallback)
+        {
+        for (;;)
+          {
+          next->host_list = next->fallback_hosts;
+          DEBUG(D_deliver) debug_printf("%s queued for fallback host(s)\n", next->address);
+          if (next->next == NULL) break;
+          next = next->next;
+          }
+        next->next = addr_fallback;
+        addr_fallback = addr;
+        }
+
+      else
+        {
+        while (next->next != NULL) next = next->next;
+        next->next = addr_defer;
+        addr_defer = addr;
+        }
+
+      continue;
+      }
+
+    /* Set a flag indicating whether there are further addresses that list
+    the continued host. This tells the transport to leave the channel open,
+    but not to pass it to another delivery process. */
+
+    for (next = addr_remote; next != NULL; next = next->next)
+      {
+      host_item *h;
+      for (h = next->host_list; h != NULL; h = h->next)
+        {
+        if (Ustrcmp(h->name, continue_hostname) == 0)
+          { continue_more = TRUE; break; }
+        }
+      }
+    }
+
+  /* The transports set up the process info themselves as they may connect
+  to more than one remote machine. They also have to set up the filter
+  arguments, if required, so that the host name and address are available
+  for expansion. */
+
+  transport_filter_argv = NULL;
+
+  /* Create the pipe for inter-process communication. If pipe creation
+  fails, it is probably because the value of remote_max_parallel is so
+  large that too many file descriptors for pipes have been created. Arrange
+  to wait for a process to finish, and then try again. If we still can't
+  create a pipe when all processes have finished, break the retry loop. */
+
+  while (!pipe_done)
+    {
+    if (pipe(pfd) == 0) pipe_done = TRUE;
+      else if (parcount > 0) parmax = parcount;
+        else break;
+
+    /* We need to make the reading end of the pipe non-blocking. There are
+    two different options for this. Exim is cunningly (I hope!) coded so
+    that it can use either of them, though it prefers O_NONBLOCK, which
+    distinguishes between EOF and no-more-data. */
+
+    #ifdef O_NONBLOCK
+    (void)fcntl(pfd[pipe_read], F_SETFL, O_NONBLOCK);
+    #else
+    (void)fcntl(pfd[pipe_read], F_SETFL, O_NDELAY);
+    #endif
+
+    /* If the maximum number of subprocesses already exist, wait for a process
+    to finish. If we ran out of file descriptors, parmax will have been reduced
+    from its initial value of remote_max_parallel. */
+
+    par_reduce(parmax - 1, fallback);
+    }
+
+  /* If we failed to create a pipe and there were no processes to wait
+  for, we have to give up on this one. Do this outside the above loop
+  so that we can continue the main loop. */
+
+  if (!pipe_done)
+    {
+    remote_post_process(addr, LOG_MAIN|LOG_PANIC,
+      string_sprintf("unable to create pipe: %s", strerror(errno)), fallback);
+    continue;
+    }
+
+  /* Find a free slot in the pardata list. Must do this after the possible
+  waiting for processes to finish, because a terminating process will free
+  up a slot. */
+
+  for (poffset = 0; poffset < remote_max_parallel; poffset++)
+    if (parlist[poffset].pid == 0) break;
+
+  /* If there isn't one, there has been a horrible disaster. */
+
+  if (poffset >= remote_max_parallel)
+    {
+    (void)close(pfd[pipe_write]);
+    (void)close(pfd[pipe_read]);
+    remote_post_process(addr, LOG_MAIN|LOG_PANIC,
+      US"Unexpectedly no free subprocess slot", fallback);
+    continue;
+    }
+
+  /* Now fork a subprocess to do the remote delivery, but before doing so,
+  ensure that any cached resourses are released so as not to interfere with
+  what happens in the subprocess. */
+
+  search_tidyup();
+
+  if ((pid = fork()) == 0)
+    {
+    int fd = pfd[pipe_write];
+    host_item *h;
+
+    /* Setting this global in the subprocess means we need never clear it */
+    transport_name = tp->name;
+
+    /* There are weird circumstances in which logging is disabled */
+    disable_logging = tp->disable_logging;
+
+    /* Show pids on debug output if parallelism possible */
+
+    if (parmax > 1 && (parcount > 0 || addr_remote != NULL))
+      {
+      DEBUG(D_any|D_v) debug_selector |= D_pid;
+      DEBUG(D_deliver) debug_printf("Remote delivery process started\n");
+      }
+
+    /* Reset the random number generator, so different processes don't all
+    have the same sequence. In the test harness we want different, but
+    predictable settings for each delivery process, so do something explicit
+    here rather they rely on the fixed reset in the random number function. */
+
+    random_seed = running_in_test_harness? 42 + 2*delivery_count : 0;
+
+    /* Set close-on-exec on the pipe so that it doesn't get passed on to
+    a new process that may be forked to do another delivery down the same
+    SMTP connection. */
+
+    (void)fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
+
+    /* Close open file descriptors for the pipes of other processes
+    that are running in parallel. */
+
+    for (poffset = 0; poffset < remote_max_parallel; poffset++)
+      if (parlist[poffset].pid != 0) (void)close(parlist[poffset].fd);
+
+    /* This process has inherited a copy of the file descriptor
+    for the data file, but its file pointer is shared with all the
+    other processes running in parallel. Therefore, we have to re-open
+    the file in order to get a new file descriptor with its own
+    file pointer. We don't need to lock it, as the lock is held by
+    the parent process. There doesn't seem to be any way of doing
+    a dup-with-new-file-pointer. */
+
+    (void)close(deliver_datafile);
+    sprintf(CS spoolname, "%s/input/%s/%s-D", spool_directory, message_subdir,
+      message_id);
+    deliver_datafile = Uopen(spoolname, O_RDWR | O_APPEND, 0);
+
+    if (deliver_datafile < 0)
+      log_write(0, LOG_MAIN|LOG_PANIC_DIE, "Failed to reopen %s for remote "
+        "parallel delivery: %s", spoolname, strerror(errno));
+
+    /* Set the close-on-exec flag */
+
+    (void)fcntl(deliver_datafile, F_SETFD, fcntl(deliver_datafile, F_GETFD) |
+      FD_CLOEXEC);
+
+    /* Set the uid/gid of this process; bombs out on failure. */
+
+    exim_setugid(uid, gid, use_initgroups,
+      string_sprintf("remote delivery to %s with transport=%s",
+        addr->address, tp->name));
+
+    /* Close the unwanted half of this process' pipe, set the process state,
+    and run the transport. Afterwards, transport_count will contain the number
+    of bytes written. */
+
+    (void)close(pfd[pipe_read]);
+    set_process_info("delivering %s using %s", message_id, tp->name);
+    debug_print_string(tp->debug_string);
+    if (!(tp->info->code)(addr->transport, addr)) replicate_status(addr);
+
+    set_process_info("delivering %s (just run %s for %s%s in subprocess)",
+      message_id, tp->name, addr->address, (addr->next == NULL)? "" : ", ...");
+
+    /* Ensure any cached resources that we used are now released */
+
+    search_tidyup();
+
+    /* Pass the result back down the pipe. This is a lot more information
+    than is needed for a local delivery. We have to send back the error
+    status for each address, the usability status for each host that is
+    flagged as unusable, and all the retry items. When TLS is in use, we
+    send also the cipher and peerdn information. Each type of information
+    is flagged by an identifying byte, and is then in a fixed format (with
+    strings terminated by zeros), and there is a final terminator at the
+    end. The host information and retry information is all attached to
+    the first address, so that gets sent at the start. */
+
+    /* Host unusability information: for most success cases this will
+    be null. */
+
+    for (h = addr->host_list; h != NULL; h = h->next)
+      {
+      if (h->address == NULL || h->status < hstatus_unusable) continue;
+      sprintf(CS big_buffer, "H%c%c%s", h->status, h->why, h->address);
+      rmt_dlv_checked_write(fd, big_buffer, Ustrlen(big_buffer+3) + 4);
+      }
+
+    /* The number of bytes written. This is the same for each address. Even
+    if we sent several copies of the message down the same connection, the
+    size of each one is the same, and it's that value we have got because
+    transport_count gets reset before calling transport_write_message(). */
+
+    big_buffer[0] = 'S';
+    memcpy(big_buffer+1, &transport_count, sizeof(transport_count));
+    rmt_dlv_checked_write(fd, big_buffer, sizeof(transport_count) + 1);
+
+    /* Information about what happened to each address. Four item types are
+    used: an optional 'X' item first, for TLS information, then an optional "C"
+    item for any client-auth info followed by 'R' items for any retry settings,
+    and finally an 'A' item for the remaining data. */
+
+    for(; addr != NULL; addr = addr->next)
+      {
+      uschar *ptr;
+      retry_item *r;
+
+      /* The certificate verification status goes into the flags */
+      if (tls_out.certificate_verified) setflag(addr, af_cert_verified);
+
+      /* Use an X item only if there's something to send */
+      #ifdef SUPPORT_TLS
+      if (addr->cipher)
+        {
+        ptr = big_buffer;
+        sprintf(CS ptr, "X1%.128s", addr->cipher);
+        while(*ptr++);
+        if (!addr->peerdn)
+         *ptr++ = 0;
+       else
+          {
+          sprintf(CS ptr, "%.512s", addr->peerdn);
+          while(*ptr++);
+          }
+
+        rmt_dlv_checked_write(fd, big_buffer, ptr - big_buffer);
+        }
+      if (addr->peercert)
+       {
+        ptr = big_buffer;
+       *ptr++ = 'X'; *ptr++ = '2';
+       if (!tls_export_cert(ptr, big_buffer_size-2, addr->peercert))
+         while(*ptr++);
+       else
+         *ptr++ = 0;
+        rmt_dlv_checked_write(fd, big_buffer, ptr - big_buffer);
+       }
+      if (addr->ourcert)
+       {
+        ptr = big_buffer;
+       *ptr++ = 'X'; *ptr++ = '3';
+       if (!tls_export_cert(ptr, big_buffer_size-2, addr->ourcert))
+         while(*ptr++);
+       else
+         *ptr++ = 0;
+        rmt_dlv_checked_write(fd, big_buffer, ptr - big_buffer);
+       }
+      #ifndef DISABLE_OCSP
+      if (addr->ocsp > OCSP_NOT_REQ)
+       {
+       ptr = big_buffer;
+       sprintf(CS ptr, "X4%c", addr->ocsp + '0');
+       while(*ptr++);
+        rmt_dlv_checked_write(fd, big_buffer, ptr - big_buffer);
+       }
+      # endif
+      #endif   /*SUPPORT_TLS*/
+
+      if (client_authenticator)
+        {
+        ptr = big_buffer;
+       sprintf(CS big_buffer, "C1%.64s", client_authenticator);
+        while(*ptr++);
+        rmt_dlv_checked_write(fd, big_buffer, ptr - big_buffer);
+       }
+      if (client_authenticated_id)
+        {
+        ptr = big_buffer;
+       sprintf(CS big_buffer, "C2%.64s", client_authenticated_id);
+        while(*ptr++);
+        rmt_dlv_checked_write(fd, big_buffer, ptr - big_buffer);
+       }
+      if (client_authenticated_sender)
+        {
+        ptr = big_buffer;
+       sprintf(CS big_buffer, "C3%.64s", client_authenticated_sender);
+        while(*ptr++);
+        rmt_dlv_checked_write(fd, big_buffer, ptr - big_buffer);
+       }
+
+      #ifndef DISABLE_PRDR
+      if (addr->flags & af_prdr_used)
+       rmt_dlv_checked_write(fd, "P", 1);
+      #endif
+
+      #ifdef EXPERIMENTAL_DSN
+      big_buffer[0] = 'D';
+      memcpy(big_buffer+1, &addr->dsn_aware, sizeof(addr->dsn_aware));
+      rmt_dlv_checked_write(fd, big_buffer, sizeof(addr->dsn_aware) + 1);
+      DEBUG(D_deliver) debug_printf("DSN write: addr->dsn_aware = %d\n", addr->dsn_aware);
+      #endif
+
+      /* Retry information: for most success cases this will be null. */
+
+      for (r = addr->retries; r != NULL; r = r->next)
+        {
+        uschar *ptr;
+        sprintf(CS big_buffer, "R%c%.500s", r->flags, r->key);
+        ptr = big_buffer + Ustrlen(big_buffer+2) + 3;
+        memcpy(ptr, &(r->basic_errno), sizeof(r->basic_errno));
+        ptr += sizeof(r->basic_errno);
+        memcpy(ptr, &(r->more_errno), sizeof(r->more_errno));
+        ptr += sizeof(r->more_errno);
+        if (r->message == NULL) *ptr++ = 0; else
+          {
+          sprintf(CS ptr, "%.512s", r->message);
+          while(*ptr++);
+          }
+        rmt_dlv_checked_write(fd, big_buffer, ptr - big_buffer);
+        }
+
+      /* The rest of the information goes in an 'A' item. */
+
+      ptr = big_buffer + 3;
+      sprintf(CS big_buffer, "A%c%c", addr->transport_return,
+        addr->special_action);
+      memcpy(ptr, &(addr->basic_errno), sizeof(addr->basic_errno));
+      ptr += sizeof(addr->basic_errno);
+      memcpy(ptr, &(addr->more_errno), sizeof(addr->more_errno));
+      ptr += sizeof(addr->more_errno);
+      memcpy(ptr, &(addr->flags), sizeof(addr->flags));
+      ptr += sizeof(addr->flags);
+
+      if (addr->message == NULL) *ptr++ = 0; else
+        {
+        sprintf(CS ptr, "%.1024s", addr->message);
+        while(*ptr++);
+        }
+
+      if (addr->user_message == NULL) *ptr++ = 0; else
+        {
+        sprintf(CS ptr, "%.1024s", addr->user_message);
+        while(*ptr++);
+        }
+
+      if (addr->host_used == NULL) *ptr++ = 0; else
+        {
+        sprintf(CS ptr, "%.256s", addr->host_used->name);
+        while(*ptr++);
+        sprintf(CS ptr, "%.64s", addr->host_used->address);
+        while(*ptr++);
+        memcpy(ptr, &(addr->host_used->port), sizeof(addr->host_used->port));
+        ptr += sizeof(addr->host_used->port);
+
+        /* DNS lookup status */
+       *ptr++ = addr->host_used->dnssec==DS_YES ? '2'
+              : addr->host_used->dnssec==DS_NO ? '1' : '0';
+
+        }
+      rmt_dlv_checked_write(fd, big_buffer, ptr - big_buffer);
+      }
+
+    /* Add termination flag, close the pipe, and that's it. The character
+    after 'Z' indicates whether continue_transport is now NULL or not.
+    A change from non-NULL to NULL indicates a problem with a continuing
+    connection. */
+
+    big_buffer[0] = 'Z';
+    big_buffer[1] = (continue_transport == NULL)? '0' : '1';
+    rmt_dlv_checked_write(fd, big_buffer, 2);
+    (void)close(fd);
+    exit(EXIT_SUCCESS);
+    }
+
+  /* Back in the mainline: close the unwanted half of the pipe. */
+
+  (void)close(pfd[pipe_write]);
+
+  /* Fork failed; defer with error message */
+
+  if (pid < 0)
+    {
+    (void)close(pfd[pipe_read]);
+    remote_post_process(addr, LOG_MAIN|LOG_PANIC,
+      string_sprintf("fork failed for remote delivery to %s: %s",
+        addr->domain, strerror(errno)), fallback);
+    continue;
+    }
+
+  /* Fork succeeded; increment the count, and remember relevant data for
+  when the process finishes. */
+
+  parcount++;
+  parlist[poffset].addrlist = parlist[poffset].addr = addr;
+  parlist[poffset].pid = pid;
+  parlist[poffset].fd = pfd[pipe_read];
+  parlist[poffset].done = FALSE;
+  parlist[poffset].msg = NULL;
+  parlist[poffset].return_path = return_path;
+
+  /* If the process we've just started is sending a message down an existing
+  channel, wait for it now. This ensures that only one such process runs at
+  once, whatever the value of remote_max parallel. Otherwise, we might try to
+  send two or more messages simultaneously down the same channel. This could
+  happen if there are different domains that include the same host in otherwise
+  different host lists.
+
+  Also, if the transport closes down the channel, this information gets back
+  (continue_transport gets set to NULL) before we consider any other addresses
+  in this message. */
+
+  if (continue_transport != NULL) par_reduce(0, fallback);
+
+  /* Otherwise, if we are running in the test harness, wait a bit, to let the
+  newly created process get going before we create another process. This should
+  ensure repeatability in the tests. We only need to wait a tad. */
+
+  else if (running_in_test_harness) millisleep(500);
+  }
+
+/* Reached the end of the list of addresses. Wait for all the subprocesses that
+are still running and post-process their addresses. */
+
+par_reduce(0, fallback);
+return TRUE;
+}
+
+
+
+
+/*************************************************
+*   Split an address into local part and domain  *
+*************************************************/
+
+/* This function initializes an address for routing by splitting it up into a
+local part and a domain. The local part is set up twice - once in its original
+casing, and once in lower case, and it is dequoted. We also do the "percent
+hack" for configured domains. This may lead to a DEFER result if a lookup
+defers. When a percent-hacking takes place, we insert a copy of the original
+address as a new parent of this address, as if we have had a redirection.
+
+Argument:
+  addr      points to an addr_item block containing the address
+
+Returns:    OK
+            DEFER   - could not determine if domain is %-hackable
+*/
+
+int
+deliver_split_address(address_item *addr)
+{
+uschar *address = addr->address;
+uschar *domain = Ustrrchr(address, '@');
+uschar *t;
+int len = domain - address;
+
+addr->domain = string_copylc(domain+1);    /* Domains are always caseless */
+
+/* The implication in the RFCs (though I can't say I've seen it spelled out
+explicitly) is that quoting should be removed from local parts at the point
+where they are locally interpreted. [The new draft "821" is more explicit on
+this, Jan 1999.] We know the syntax is valid, so this can be done by simply
+removing quoting backslashes and any unquoted doublequotes. */
+
+t = addr->cc_local_part = store_get(len+1);
+while(len-- > 0)
+  {
+  register int c = *address++;
+  if (c == '\"') continue;
+  if (c == '\\')
+    {
+    *t++ = *address++;
+    len--;
+    }
+  else *t++ = c;
+  }
+*t = 0;
+
+/* We do the percent hack only for those domains that are listed in
+percent_hack_domains. A loop is required, to copy with multiple %-hacks. */
+
+if (percent_hack_domains != NULL)
+  {
+  int rc;
+  uschar *new_address = NULL;
+  uschar *local_part = addr->cc_local_part;
+
+  deliver_domain = addr->domain;  /* set $domain */
+
+  while ((rc = match_isinlist(deliver_domain, &percent_hack_domains, 0,
+           &domainlist_anchor, addr->domain_cache, MCL_DOMAIN, TRUE, NULL))
+             == OK &&
+         (t = Ustrrchr(local_part, '%')) != NULL)
+    {
+    new_address = string_copy(local_part);
+    new_address[t - local_part] = '@';
+    deliver_domain = string_copylc(t+1);
+    local_part = string_copyn(local_part, t - local_part);
+    }
+
+  if (rc == DEFER) return DEFER;   /* lookup deferred */
+
+  /* If hackery happened, set up new parent and alter the current address. */
+
+  if (new_address != NULL)
+    {
+    address_item *new_parent = store_get(sizeof(address_item));
+    *new_parent = *addr;
+    addr->parent = new_parent;
+    addr->address = new_address;
+    addr->unique = string_copy(new_address);
+    addr->domain = deliver_domain;
+    addr->cc_local_part = local_part;
+    DEBUG(D_deliver) debug_printf("%%-hack changed address to: %s\n",
+      addr->address);
+    }
+  }
+
+/* Create the lowercased version of the final local part, and make that the
+default one to be used. */
+
+addr->local_part = addr->lc_local_part = string_copylc(addr->cc_local_part);
+return OK;
+}
+
+
+
+
+/*************************************************
+*      Get next error message text               *
+*************************************************/
+
+/* If f is not NULL, read the next "paragraph", from a customized error message
+text file, terminated by a line containing ****, and expand it.
+
+Arguments:
+  f          NULL or a file to read from
+  which      string indicating which string (for errors)
+
+Returns:     NULL or an expanded string
+*/
+
+static uschar *
+next_emf(FILE *f, uschar *which)
+{
+int size = 256;
+int ptr = 0;
+uschar *para, *yield;
+uschar buffer[256];
+
+if (f == NULL) return NULL;
+
+if (Ufgets(buffer, sizeof(buffer), f) == NULL ||
+    Ustrcmp(buffer, "****\n") == 0) return NULL;
+
+para = store_get(size);
+for (;;)
+  {
+  para = string_cat(para, &size, &ptr, buffer, Ustrlen(buffer));
+  if (Ufgets(buffer, sizeof(buffer), f) == NULL ||
+      Ustrcmp(buffer, "****\n") == 0) break;
+  }
+para[ptr] = 0;
+
+yield = expand_string(para);
+if (yield != NULL) return yield;
+
+log_write(0, LOG_MAIN|LOG_PANIC, "Failed to expand string from "
+  "bounce_message_file or warn_message_file (%s): %s", which,
+  expand_string_message);
+return NULL;
+}
+
+
+
+
+/*************************************************
+*      Close down a passed transport channel     *
+*************************************************/
+
+/* This function is called when a passed transport channel cannot be used.
+It attempts to close it down tidily. The yield is always DELIVER_NOT_ATTEMPTED
+so that the function call can be the argument of a "return" statement.
+
+Arguments:  None
+Returns:    DELIVER_NOT_ATTEMPTED
+*/
+
+static int
+continue_closedown(void)
+{
+if (continue_transport != NULL)
+  {
+  transport_instance *t;
+  for (t = transports; t != NULL; t = t->next)
+    {
+    if (Ustrcmp(t->name, continue_transport) == 0)
+      {
+      if (t->info->closedown != NULL) (t->info->closedown)(t);
+      break;
+      }
+    }
+  }
+return DELIVER_NOT_ATTEMPTED;
+}
+
+
+
+
+/*************************************************
+*           Print address information            *
+*************************************************/
+
+/* This function is called to output an address, or information about an
+address, for bounce or defer messages. If the hide_child flag is set, all we
+output is the original ancestor address.
+
+Arguments:
+  addr         points to the address
+  f            the FILE to print to
+  si           an initial string
+  sc           a continuation string for before "generated"
+  se           an end string
+
+Returns:       TRUE if the address is not hidden
+*/
+
+static BOOL
+print_address_information(address_item *addr, FILE *f, uschar *si, uschar *sc,
+  uschar *se)
+{
+BOOL yield = TRUE;
+uschar *printed = US"";
+address_item *ancestor = addr;
+while (ancestor->parent != NULL) ancestor = ancestor->parent;
+
+fprintf(f, "%s", CS si);
+
+if (addr->parent != NULL && testflag(addr, af_hide_child))
+  {
+  printed = US"an undisclosed address";
+  yield = FALSE;
+  }
+else if (!testflag(addr, af_pfr) || addr->parent == NULL)
+  printed = addr->address;
+
+else
+  {
+  uschar *s = addr->address;
+  uschar *ss;
+
+  if (addr->address[0] == '>') { ss = US"mail"; s++; }
+  else if (addr->address[0] == '|') ss = US"pipe";
+  else ss = US"save";
+
+  fprintf(f, "%s to %s%sgenerated by ", ss, s, sc);
+  printed = addr->parent->address;
+  }
+
+fprintf(f, "%s", CS string_printing(printed));
+
+if (ancestor != addr)
+  {
+  uschar *original = (ancestor->onetime_parent == NULL)?
+    ancestor->address : ancestor->onetime_parent;
+  if (strcmpic(original, printed) != 0)
+    fprintf(f, "%s(%sgenerated from %s)", sc,
+      (ancestor != addr->parent)? "ultimately " : "",
+      string_printing(original));
+  }
+
+fprintf(f, "%s", CS se);
+return yield;
+}
+
+
+
+
+
+/*************************************************
+*         Print error for an address             *
+*************************************************/
+
+/* This function is called to print the error information out of an address for
+a bounce or a warning message. It tries to format the message reasonably by
+introducing newlines. All lines are indented by 4; the initial printing
+position must be set before calling.
+
+This function used always to print the error. Nowadays we want to restrict it
+to cases such as LMTP/SMTP errors from a remote host, and errors from :fail:
+and filter "fail". We no longer pass other information willy-nilly in bounce
+and warning messages. Text in user_message is always output; text in message
+only if the af_pass_message flag is set.
+
+Arguments:
+  addr         the address
+  f            the FILE to print on
+  t            some leading text
+
+Returns:       nothing
+*/
+
+static void
+print_address_error(address_item *addr, FILE *f, uschar *t)
+{
+int count = Ustrlen(t);
+uschar *s = testflag(addr, af_pass_message)? addr->message : NULL;
+
+if (s == NULL)
+  {
+  if (addr->user_message != NULL) s = addr->user_message; else return;
+  }
+
+fprintf(f, "\n    %s", t);
+
+while (*s != 0)
+  {
+  if (*s == '\\' && s[1] == 'n')
+    {
+    fprintf(f, "\n    ");
+    s += 2;
+    count = 0;
+    }
+  else
+    {
+    fputc(*s, f);
+    count++;
+    if (*s++ == ':' && isspace(*s) && count > 45)
+      {
+      fprintf(f, "\n   ");  /* sic (because space follows) */
+      count = 0;
+      }
+    }
+  }
+}
+
+
+
+
+
+
+/*************************************************
+*     Check list of addresses for duplication    *
+*************************************************/
+
+/* This function was introduced when the test for duplicate addresses that are
+not pipes, files, or autoreplies was moved from the middle of routing to when
+routing was complete. That was to fix obscure cases when the routing history
+affects the subsequent routing of identical addresses. This function is called
+after routing, to check that the final routed addresses are not duplicates.
+
+If we detect a duplicate, we remember what it is a duplicate of. Note that
+pipe, file, and autoreply de-duplication is handled during routing, so we must
+leave such "addresses" alone here, as otherwise they will incorrectly be
+discarded.
+
+Argument:     address of list anchor
+Returns:      nothing
+*/
+
+static void
+do_duplicate_check(address_item **anchor)
+{
+address_item *addr;
+while ((addr = *anchor) != NULL)
+  {
+  tree_node *tnode;
+  if (testflag(addr, af_pfr))
+    {
+    anchor = &(addr->next);
+    }
+  else if ((tnode = tree_search(tree_duplicates, addr->unique)) != NULL)
+    {
+    DEBUG(D_deliver|D_route)
+      debug_printf("%s is a duplicate address: discarded\n", addr->unique);
+    *anchor = addr->next;
+    addr->dupof = tnode->data.ptr;
+    addr->next = addr_duplicate;
+    addr_duplicate = addr;
+    }
+  else
+    {
+    tree_add_duplicate(addr->unique, addr);
+    anchor = &(addr->next);
+    }
+  }
+}
+
+
+
+
+/*************************************************
+*              Deliver one message               *
+*************************************************/
+
+/* This is the function which is called when a message is to be delivered. It
+is passed the id of the message. It is possible that the message no longer
+exists, if some other process has delivered it, and it is also possible that
+the message is being worked on by another process, in which case the data file
+will be locked.
+
+If no delivery is attempted for any of the above reasons, the function returns
+DELIVER_NOT_ATTEMPTED.
+
+If the give_up flag is set true, do not attempt any deliveries, but instead
+fail all outstanding addresses and return the message to the sender (or
+whoever).
+
+A delivery operation has a process all to itself; we never deliver more than
+one message in the same process. Therefore we needn't worry too much about
+store leakage.
+
+Arguments:
+  id          the id of the message to be delivered
+  forced      TRUE if delivery was forced by an administrator; this overrides
+              retry delays and causes a delivery to be tried regardless
+  give_up     TRUE if an administrator has requested that delivery attempts
+              be abandoned
+
+Returns:      When the global variable mua_wrapper is FALSE:
+                DELIVER_ATTEMPTED_NORMAL   if a delivery attempt was made
+                DELIVER_NOT_ATTEMPTED      otherwise (see comment above)
+              When the global variable mua_wrapper is TRUE:
+                DELIVER_MUA_SUCCEEDED      if delivery succeeded
+                DELIVER_MUA_FAILED         if delivery failed
+                DELIVER_NOT_ATTEMPTED      if not attempted (should not occur)
+*/
+
+int
+deliver_message(uschar *id, BOOL forced, BOOL give_up)
+{
+int i, rc;
+int final_yield = DELIVER_ATTEMPTED_NORMAL;
+time_t now = time(NULL);
+address_item *addr_last = NULL;
+uschar *filter_message = NULL;
+FILE *jread;
+int process_recipients = RECIP_ACCEPT;
+open_db dbblock;
+open_db *dbm_file;
+extern int acl_where;
+
+uschar *info = (queue_run_pid == (pid_t)0)?
+  string_sprintf("delivering %s", id) :
+  string_sprintf("delivering %s (queue run pid %d)", id, queue_run_pid);
+
+/* If the D_process_info bit is on, set_process_info() will output debugging
+information. If not, we want to show this initial information if D_deliver or
+D_queue_run is set or in verbose mode. */
+
+set_process_info("%s", info);
+
+if ((debug_selector & D_process_info) == 0 &&
+    (debug_selector & (D_deliver|D_queue_run|D_v)) != 0)
+  debug_printf("%s\n", info);
+
+/* Ensure that we catch any subprocesses that are created. Although Exim
+sets SIG_DFL as its initial default, some routes through the code end up
+here with it set to SIG_IGN - cases where a non-synchronous delivery process
+has been forked, but no re-exec has been done. We use sigaction rather than
+plain signal() on those OS where SA_NOCLDWAIT exists, because we want to be
+sure it is turned off. (There was a problem on AIX with this.) */
+
+#ifdef SA_NOCLDWAIT
+  {
+  struct sigaction act;
+  act.sa_handler = SIG_DFL;
+  sigemptyset(&(act.sa_mask));
+  act.sa_flags = 0;
+  sigaction(SIGCHLD, &act, NULL);
+  }
+#else
+signal(SIGCHLD, SIG_DFL);
+#endif
+
+/* Make the forcing flag available for routers and transports, set up the
+global message id field, and initialize the count for returned files and the
+message size. This use of strcpy() is OK because the length id is checked when
+it is obtained from a command line (the -M or -q options), and otherwise it is
+known to be a valid message id. */
+
+Ustrcpy(message_id, id);
+deliver_force = forced;
+return_count = 0;
+message_size = 0;
+
+/* Initialize some flags */
+
+update_spool = FALSE;
+remove_journal = TRUE;
+
+/* Set a known context for any ACLs we call via expansions */
+acl_where = ACL_WHERE_DELIVERY;
+
+/* Reset the random number generator, so that if several delivery processes are
+started from a queue runner that has already used random numbers (for sorting),
+they don't all get the same sequence. */
+
+random_seed = 0;
+
+/* Open and lock the message's data file. Exim locks on this one because the
+header file may get replaced as it is re-written during the delivery process.
+Any failures cause messages to be written to the log, except for missing files
+while queue running - another process probably completed delivery. As part of
+opening the data file, message_subdir gets set. */
+
+if (!spool_open_datafile(id))
+  return continue_closedown();  /* yields DELIVER_NOT_ATTEMPTED */
+
+/* The value of message_size at this point has been set to the data length,
+plus one for the blank line that notionally precedes the data. */
+
+/* Now read the contents of the header file, which will set up the headers in
+store, and also the list of recipients and the tree of non-recipients and
+assorted flags. It updates message_size. If there is a reading or format error,
+give up; if the message has been around for sufficiently long, remove it. */
+
+sprintf(CS spoolname, "%s-H", id);
+if ((rc = spool_read_header(spoolname, TRUE, TRUE)) != spool_read_OK)
+  {
+  if (errno == ERRNO_SPOOLFORMAT)
+    {
+    struct stat statbuf;
+    sprintf(CS big_buffer, "%s/input/%s/%s", spool_directory, message_subdir,
+      spoolname);
+    if (Ustat(big_buffer, &statbuf) == 0)
+      log_write(0, LOG_MAIN, "Format error in spool file %s: "
+        "size=" OFF_T_FMT, spoolname, statbuf.st_size);
+    else log_write(0, LOG_MAIN, "Format error in spool file %s", spoolname);
+    }
+  else
+    log_write(0, LOG_MAIN, "Error reading spool file %s: %s", spoolname,
+      strerror(errno));
+
+  /* If we managed to read the envelope data, received_time contains the
+  time the message was received. Otherwise, we can calculate it from the
+  message id. */
+
+  if (rc != spool_read_hdrerror)
+    {
+    received_time = 0;
+    for (i = 0; i < 6; i++)
+      received_time = received_time * BASE_62 + tab62[id[i] - '0'];
+    }
+
+  /* If we've had this malformed message too long, sling it. */
+
+  if (now - received_time > keep_malformed)
+    {
+    sprintf(CS spoolname, "%s/msglog/%s/%s", spool_directory, message_subdir, id);
+    Uunlink(spoolname);
+    sprintf(CS spoolname, "%s/input/%s/%s-D", spool_directory, message_subdir, id);
+    Uunlink(spoolname);
+    sprintf(CS spoolname, "%s/input/%s/%s-H", spool_directory, message_subdir, id);
+    Uunlink(spoolname);
+    sprintf(CS spoolname, "%s/input/%s/%s-J", spool_directory, message_subdir, id);
+    Uunlink(spoolname);
+    log_write(0, LOG_MAIN, "Message removed because older than %s",
+      readconf_printtime(keep_malformed));
+    }
+
+  (void)close(deliver_datafile);
+  deliver_datafile = -1;
+  return continue_closedown();   /* yields DELIVER_NOT_ATTEMPTED */
+  }
+
+/* The spool header file has been read. Look to see if there is an existing
+journal file for this message. If there is, it means that a previous delivery
+attempt crashed (program or host) before it could update the spool header file.
+Read the list of delivered addresses from the journal and add them to the
+nonrecipients tree. Then update the spool file. We can leave the journal in
+existence, as it will get further successful deliveries added to it in this
+run, and it will be deleted if this function gets to its end successfully.
+Otherwise it might be needed again. */
+
+sprintf(CS spoolname, "%s/input/%s/%s-J", spool_directory, message_subdir, id);
+jread = Ufopen(spoolname, "rb");
+if (jread != NULL)
+  {
+  while (Ufgets(big_buffer, big_buffer_size, jread) != NULL)
+    {
+    int n = Ustrlen(big_buffer);
+    big_buffer[n-1] = 0;
+    tree_add_nonrecipient(big_buffer);
+    DEBUG(D_deliver) debug_printf("Previously delivered address %s taken from "
+      "journal file\n", big_buffer);
+    }
+  (void)fclose(jread);
+  /* Panic-dies on error */
+  (void)spool_write_header(message_id, SW_DELIVERING, NULL);
+  }
+else if (errno != ENOENT)
+  {
+  log_write(0, LOG_MAIN|LOG_PANIC, "attempt to open journal for reading gave: "
+    "%s", strerror(errno));
+  return continue_closedown();   /* yields DELIVER_NOT_ATTEMPTED */
+  }
+
+/* A null recipients list indicates some kind of disaster. */
+
+if (recipients_list == NULL)
+  {
+  (void)close(deliver_datafile);
+  deliver_datafile = -1;
+  log_write(0, LOG_MAIN, "Spool error: no recipients for %s", spoolname);
+  return continue_closedown();   /* yields DELIVER_NOT_ATTEMPTED */
+  }
+
+
+/* Handle a message that is frozen. There are a number of different things that
+can happen, but in the default situation, unless forced, no delivery is
+attempted. */
+
+if (deliver_freeze)
+  {
+  #ifdef SUPPORT_MOVE_FROZEN_MESSAGES
+  /* Moving to another directory removes the message from Exim's view. Other
+  tools must be used to deal with it. Logging of this action happens in
+  spool_move_message() and its subfunctions. */
+
+  if (move_frozen_messages &&
+      spool_move_message(id, message_subdir, US"", US"F"))
+    return continue_closedown();   /* yields DELIVER_NOT_ATTEMPTED */
+  #endif
+
+  /* For all frozen messages (bounces or not), timeout_frozen_after sets the
+  maximum time to keep messages that are frozen. Thaw if we reach it, with a
+  flag causing all recipients to be failed. The time is the age of the
+  message, not the time since freezing. */
+
+  if (timeout_frozen_after > 0 && message_age >= timeout_frozen_after)
+    {
+    log_write(0, LOG_MAIN, "cancelled by timeout_frozen_after");
+    process_recipients = RECIP_FAIL_TIMEOUT;
+    }
+
+  /* For bounce messages (and others with no sender), thaw if the error message
+  ignore timer is exceeded. The message will be discarded if this delivery
+  fails. */
+
+  else if (sender_address[0] == 0 && message_age >= ignore_bounce_errors_after)
+    {
+    log_write(0, LOG_MAIN, "Unfrozen by errmsg timer");
+    }
+
+  /* If this is a bounce message, or there's no auto thaw, or we haven't
+  reached the auto thaw time yet, and this delivery is not forced by an admin
+  user, do not attempt delivery of this message. Note that forced is set for
+  continuing messages down the same channel, in order to skip load checking and
+  ignore hold domains, but we don't want unfreezing in that case. */
+
+  else
+    {
+    if ((sender_address[0] == 0 ||
+         auto_thaw <= 0 ||
+         now <= deliver_frozen_at + auto_thaw
+        )
+        &&
+        (!forced || !deliver_force_thaw || !admin_user ||
+          continue_hostname != NULL
+        ))
+      {
+      (void)close(deliver_datafile);
+      deliver_datafile = -1;
+      log_write(L_skip_delivery, LOG_MAIN, "Message is frozen");
+      return continue_closedown();   /* yields DELIVER_NOT_ATTEMPTED */
+      }
+
+    /* If delivery was forced (by an admin user), assume a manual thaw.
+    Otherwise it's an auto thaw. */
+
+    if (forced)
+      {
+      deliver_manual_thaw = TRUE;
+      log_write(0, LOG_MAIN, "Unfrozen by forced delivery");
+      }
+    else log_write(0, LOG_MAIN, "Unfrozen by auto-thaw");
+    }
+
+  /* We get here if any of the rules for unfreezing have triggered. */
+
+  deliver_freeze = FALSE;
+  update_spool = TRUE;
+  }
+
+
+/* Open the message log file if we are using them. This records details of
+deliveries, deferments, and failures for the benefit of the mail administrator.
+The log is not used by exim itself to track the progress of a message; that is
+done by rewriting the header spool file. */
+
+if (message_logs)
+  {
+  uschar *error;
+  int fd;
+
+  sprintf(CS spoolname, "%s/msglog/%s/%s", spool_directory, message_subdir, id);
+  fd = open_msglog_file(spoolname, SPOOL_MODE, &error);
+
+  if (fd < 0)
+    {
+    log_write(0, LOG_MAIN|LOG_PANIC, "Couldn't %s message log %s: %s", error,
+      spoolname, strerror(errno));
+    return continue_closedown();   /* yields DELIVER_NOT_ATTEMPTED */
+    }
+
+  /* Make a C stream out of it. */
+
+  message_log = fdopen(fd, "a");
+  if (message_log == NULL)
+    {
+    log_write(0, LOG_MAIN|LOG_PANIC, "Couldn't fdopen message log %s: %s",
+      spoolname, strerror(errno));
+    return continue_closedown();   /* yields DELIVER_NOT_ATTEMPTED */
+    }
+  }
+
+
+/* If asked to give up on a message, log who did it, and set the action for all
+the addresses. */
+
+if (give_up)
+  {
+  struct passwd *pw = getpwuid(real_uid);
+  log_write(0, LOG_MAIN, "cancelled by %s", (pw != NULL)?
+        US pw->pw_name : string_sprintf("uid %ld", (long int)real_uid));
+  process_recipients = RECIP_FAIL;
+  }
+
+/* Otherwise, if there are too many Received: headers, fail all recipients. */
+
+else if (received_count > received_headers_max)
+  process_recipients = RECIP_FAIL_LOOP;
+
+/* Otherwise, if a system-wide, address-independent message filter is
+specified, run it now, except in the case when we are failing all recipients as
+a result of timeout_frozen_after. If the system filter yields "delivered", then
+ignore the true recipients of the message. Failure of the filter file is
+logged, and the delivery attempt fails. */
+
+else if (system_filter != NULL && process_recipients != RECIP_FAIL_TIMEOUT)
+  {
+  int rc;
+  int filtertype;
+  ugid_block ugid;
+  redirect_block redirect;
+
+  if (system_filter_uid_set)
+    {
+    ugid.uid = system_filter_uid;
+    ugid.gid = system_filter_gid;
+    ugid.uid_set = ugid.gid_set = TRUE;
+    }
+  else
+    {
+    ugid.uid_set = ugid.gid_set = FALSE;
+    }
+
+  return_path = sender_address;
+  enable_dollar_recipients = TRUE;   /* Permit $recipients in system filter */
+  system_filtering = TRUE;
+
+  /* Any error in the filter file causes a delivery to be abandoned. */
+
+  redirect.string = system_filter;
+  redirect.isfile = TRUE;
+  redirect.check_owner = redirect.check_group = FALSE;
+  redirect.owners = NULL;
+  redirect.owngroups = NULL;
+  redirect.pw = NULL;
+  redirect.modemask = 0;
+
+  DEBUG(D_deliver|D_filter) debug_printf("running system filter\n");
+
+  rc = rda_interpret(
+    &redirect,              /* Where the data is */
+    RDO_DEFER |             /* Turn on all the enabling options */
+      RDO_FAIL |            /* Leave off all the disabling options */
+      RDO_FILTER |
+      RDO_FREEZE |
+      RDO_REALLOG |
+      RDO_REWRITE,
+    NULL,                   /* No :include: restriction (not used in filter) */
+    NULL,                   /* No sieve vacation directory (not sieve!) */
+    NULL,                   /* No sieve enotify mailto owner (not sieve!) */
+    NULL,                   /* No sieve user address (not sieve!) */
+    NULL,                   /* No sieve subaddress (not sieve!) */
+    &ugid,                  /* uid/gid data */
+    &addr_new,              /* Where to hang generated addresses */
+    &filter_message,        /* Where to put error message */
+    NULL,                   /* Don't skip syntax errors */
+    &filtertype,            /* Will always be set to FILTER_EXIM for this call */
+    US"system filter");     /* For error messages */
+
+  DEBUG(D_deliver|D_filter) debug_printf("system filter returned %d\n", rc);
+
+  if (rc == FF_ERROR || rc == FF_NONEXIST)
+    {
+    (void)close(deliver_datafile);
+    deliver_datafile = -1;
+    log_write(0, LOG_MAIN|LOG_PANIC, "Error in system filter: %s",
+      string_printing(filter_message));
+    return continue_closedown();   /* yields DELIVER_NOT_ATTEMPTED */
+    }
+
+  /* Reset things. If the filter message is an empty string, which can happen
+  for a filter "fail" or "freeze" command with no text, reset it to NULL. */
+
+  system_filtering = FALSE;
+  enable_dollar_recipients = FALSE;
+  if (filter_message != NULL && filter_message[0] == 0) filter_message = NULL;
+
+  /* Save the values of the system filter variables so that user filters
+  can use them. */
+
+  memcpy(filter_sn, filter_n, sizeof(filter_sn));
+
+  /* The filter can request that delivery of the original addresses be
+  deferred. */
+
+  if (rc == FF_DEFER)
+    {
+    process_recipients = RECIP_DEFER;
+    deliver_msglog("Delivery deferred by system filter\n");
+    log_write(0, LOG_MAIN, "Delivery deferred by system filter");
+    }
+
+  /* The filter can request that a message be frozen, but this does not
+  take place if the message has been manually thawed. In that case, we must
+  unset "delivered", which is forced by the "freeze" command to make -bF
+  work properly. */
+
+  else if (rc == FF_FREEZE && !deliver_manual_thaw)
+    {
+    deliver_freeze = TRUE;
+    deliver_frozen_at = time(NULL);
+    process_recipients = RECIP_DEFER;
+    frozen_info = string_sprintf(" by the system filter%s%s",
+      (filter_message == NULL)? US"" : US": ",
+      (filter_message == NULL)? US"" : filter_message);
+    }
+
+  /* The filter can request that a message be failed. The error message may be
+  quite long - it is sent back to the sender in the bounce - but we don't want
+  to fill up the log with repetitions of it. If it starts with << then the text
+  between << and >> is written to the log, with the rest left for the bounce
+  message. */
+
+  else if (rc == FF_FAIL)
+    {
+    uschar *colon = US"";
+    uschar *logmsg = US"";
+    int loglen = 0;
+
+    process_recipients = RECIP_FAIL_FILTER;
+
+    if (filter_message != NULL)
+      {
+      uschar *logend;
+      colon = US": ";
+      if (filter_message[0] == '<' && filter_message[1] == '<' &&
+          (logend = Ustrstr(filter_message, ">>")) != NULL)
+        {
+        logmsg = filter_message + 2;
+        loglen = logend - logmsg;
+        filter_message = logend + 2;
+        if (filter_message[0] == 0) filter_message = NULL;
+        }
+      else
+        {
+        logmsg = filter_message;
+        loglen = Ustrlen(filter_message);
+        }
+      }
+
+    log_write(0, LOG_MAIN, "cancelled by system filter%s%.*s", colon, loglen,
+      logmsg);
+    }
+
+  /* Delivery can be restricted only to those recipients (if any) that the
+  filter specified. */
+
+  else if (rc == FF_DELIVERED)
+    {
+    process_recipients = RECIP_IGNORE;
+    if (addr_new == NULL)
+      log_write(0, LOG_MAIN, "=> discarded (system filter)");
+    else
+      log_write(0, LOG_MAIN, "original recipients ignored (system filter)");
+    }
+
+  /* If any new addresses were created by the filter, fake up a "parent"
+  for them. This is necessary for pipes, etc., which are expected to have
+  parents, and it also gives some sensible logging for others. Allow
+  pipes, files, and autoreplies, and run them as the filter uid if set,
+  otherwise as the current uid. */
+
+  if (addr_new != NULL)
+    {
+    int uid = (system_filter_uid_set)? system_filter_uid : geteuid();
+    int gid = (system_filter_gid_set)? system_filter_gid : getegid();
+
+    /* The text "system-filter" is tested in transport_set_up_command() and in
+    set_up_shell_command() in the pipe transport, to enable them to permit
+    $recipients, so don't change it here without also changing it there. */
+
+    address_item *p = addr_new;
+    address_item *parent = deliver_make_addr(US"system-filter", FALSE);
+
+    parent->domain = string_copylc(qualify_domain_recipient);
+    parent->local_part = US"system-filter";
+
+    /* As part of this loop, we arrange for addr_last to end up pointing
+    at the final address. This is used if we go on to add addresses for the
+    original recipients. */
+
+    while (p != NULL)
+      {
+      if (parent->child_count == SHRT_MAX)
+        log_write(0, LOG_MAIN|LOG_PANIC_DIE, "system filter generated more "
+          "than %d delivery addresses", SHRT_MAX);
+      parent->child_count++;
+      p->parent = parent;
+
+      if (testflag(p, af_pfr))
+        {
+        uschar *tpname;
+        uschar *type;
+        p->uid = uid;
+        p->gid = gid;
+        setflag(p, af_uid_set |
+                   af_gid_set |
+                   af_allow_file |
+                   af_allow_pipe |
+                   af_allow_reply);
+
+        /* Find the name of the system filter's appropriate pfr transport */
+
+        if (p->address[0] == '|')
+          {
+          type = US"pipe";
+          tpname = system_filter_pipe_transport;
+          address_pipe = p->address;
+          }
+        else if (p->address[0] == '>')
+          {
+          type = US"reply";
+          tpname = system_filter_reply_transport;
+          }
+        else
+          {
+          if (p->address[Ustrlen(p->address)-1] == '/')
+            {
+            type = US"directory";
+            tpname = system_filter_directory_transport;
+            }
+          else
+            {
+            type = US"file";
+            tpname = system_filter_file_transport;
+            }
+          address_file = p->address;
+          }
+
+        /* Now find the actual transport, first expanding the name. We have
+        set address_file or address_pipe above. */
+
+        if (tpname != NULL)
+          {
+          uschar *tmp = expand_string(tpname);
+          address_file = address_pipe = NULL;
+          if (tmp == NULL)
+            p->message = string_sprintf("failed to expand \"%s\" as a "
+              "system filter transport name", tpname);
+          tpname = tmp;
+          }
+        else
+          {
+          p->message = string_sprintf("system_filter_%s_transport is unset",
+            type);
+          }
+
+        if (tpname != NULL)
+          {
+          transport_instance *tp;
+          for (tp = transports; tp != NULL; tp = tp->next)
+            {
+            if (Ustrcmp(tp->name, tpname) == 0)
+              {
+              p->transport = tp;
+              break;
+              }
+            }
+          if (tp == NULL)
+            p->message = string_sprintf("failed to find \"%s\" transport "
+              "for system filter delivery", tpname);
+          }
+
+        /* If we couldn't set up a transport, defer the delivery, putting the
+        error on the panic log as well as the main log. */
+
+        if (p->transport == NULL)
+          {
+          address_item *badp = p;
+          p = p->next;
+          if (addr_last == NULL) addr_new = p; else addr_last->next = p;
+          badp->local_part = badp->address;   /* Needed for log line */
+          post_process_one(badp, DEFER, LOG_MAIN|LOG_PANIC, DTYPE_ROUTER, 0);
+          continue;
+          }
+        }    /* End of pfr handling */
+
+      /* Either a non-pfr delivery, or we found a transport */
+
+      DEBUG(D_deliver|D_filter)
+        debug_printf("system filter added %s\n", p->address);
+
+      addr_last = p;
+      p = p->next;
+      }    /* Loop through all addr_new addresses */
+    }
+  }
+
+
+/* Scan the recipients list, and for every one that is not in the non-
+recipients tree, add an addr item to the chain of new addresses. If the pno
+value is non-negative, we must set the onetime parent from it. This which
+points to the relevant entry in the recipients list.
+
+This processing can be altered by the setting of the process_recipients
+variable, which is changed if recipients are to be ignored, failed, or
+deferred. This can happen as a result of system filter activity, or if the -Mg
+option is used to fail all of them.
+
+Duplicate addresses are handled later by a different tree structure; we can't
+just extend the non-recipients tree, because that will be re-written to the
+spool if the message is deferred, and in any case there are casing
+complications for local addresses. */
+
+if (process_recipients != RECIP_IGNORE)
+  {
+  for (i = 0; i < recipients_count; i++)
+    {
+    if (tree_search(tree_nonrecipients, recipients_list[i].address) == NULL)
+      {
+      recipient_item *r = recipients_list + i;
+      address_item *new = deliver_make_addr(r->address, FALSE);
+      new->p.errors_address = r->errors_to;
+
+      if (r->pno >= 0)
+        new->onetime_parent = recipients_list[r->pno].address;
+
+      #ifdef EXPERIMENTAL_DSN
+      /* If DSN support is enabled, set the dsn flags and the original receipt 
+         to be passed on to other DSN enabled MTAs */
+      new->dsn_flags = r->dsn_flags & rf_dsnflags;
+      new->dsn_orcpt = r->orcpt;
+      DEBUG(D_deliver) debug_printf("DSN: set orcpt: %s  flags: %d\n", new->dsn_orcpt, new->dsn_flags);
+      #endif
+
+      switch (process_recipients)
+        {
+        /* RECIP_DEFER is set when a system filter freezes a message. */
+
+        case RECIP_DEFER:
+        new->next = addr_defer;
+        addr_defer = new;
+        break;
+
+
+        /* RECIP_FAIL_FILTER is set when a system filter has obeyed a "fail"
+        command. */
+
+        case RECIP_FAIL_FILTER:
+        new->message =
+          (filter_message == NULL)? US"delivery cancelled" : filter_message;
+        setflag(new, af_pass_message);
+        goto RECIP_QUEUE_FAILED;   /* below */
+
+
+        /* RECIP_FAIL_TIMEOUT is set when a message is frozen, but is older
+        than the value in timeout_frozen_after. Treat non-bounce messages
+        similarly to -Mg; for bounce messages we just want to discard, so
+        don't put the address on the failed list. The timeout has already
+        been logged. */
+
+        case RECIP_FAIL_TIMEOUT:
+        new->message  = US"delivery cancelled; message timed out";
+        goto RECIP_QUEUE_FAILED;   /* below */
+
+
+        /* RECIP_FAIL is set when -Mg has been used. */
+
+        case RECIP_FAIL:
+        new->message  = US"delivery cancelled by administrator";
+        /* Fall through */
+
+        /* Common code for the failure cases above. If this is not a bounce
+        message, put the address on the failed list so that it is used to
+        create a bounce. Otherwise do nothing - this just discards the address.
+        The incident has already been logged. */
+
+        RECIP_QUEUE_FAILED:
+        if (sender_address[0] != 0)
+          {
+          new->next = addr_failed;
+          addr_failed = new;
+          }
+        break;
+
+
+        /* RECIP_FAIL_LOOP is set when there are too many Received: headers
+        in the message. Process each address as a routing failure; if this
+        is a bounce message, it will get frozen. */
+
+        case RECIP_FAIL_LOOP:
+        new->message = US"Too many \"Received\" headers - suspected mail loop";
+        post_process_one(new, FAIL, LOG_MAIN, DTYPE_ROUTER, 0);
+        break;
+
+
+        /* Value should be RECIP_ACCEPT; take this as the safe default. */
+
+        default:
+        if (addr_new == NULL) addr_new = new; else addr_last->next = new;
+        addr_last = new;
+        break;
+        }
+      }
+    }
+  }
+
+DEBUG(D_deliver)
+  {
+  address_item *p = addr_new;
+  debug_printf("Delivery address list:\n");
+  while (p != NULL)
+    {
+    debug_printf("  %s %s\n", p->address, (p->onetime_parent == NULL)? US"" :
+      p->onetime_parent);
+    p = p->next;
+    }
+  }
+
+/* Set up the buffers used for copying over the file when delivering. */
+
+deliver_in_buffer = store_malloc(DELIVER_IN_BUFFER_SIZE);
+deliver_out_buffer = store_malloc(DELIVER_OUT_BUFFER_SIZE);
+
+
+
+/* Until there are no more new addresses, handle each one as follows:
+
+ . If this is a generated address (indicated by the presence of a parent
+   pointer) then check to see whether it is a pipe, file, or autoreply, and
+   if so, handle it directly here. The router that produced the address will
+   have set the allow flags into the address, and also set the uid/gid required.
+   Having the routers generate new addresses and then checking them here at
+   the outer level is tidier than making each router do the checking, and
+   means that routers don't need access to the failed address queue.
+
+ . Break up the address into local part and domain, and make lowercased
+   versions of these strings. We also make unquoted versions of the local part.
+
+ . Handle the percent hack for those domains for which it is valid.
+
+ . For child addresses, determine if any of the parents have the same address.
+   If so, generate a different string for previous delivery checking. Without
+   this code, if the address spqr generates spqr via a forward or alias file,
+   delivery of the generated spqr stops further attempts at the top level spqr,
+   which is not what is wanted - it may have generated other addresses.
+
+ . Check on the retry database to see if routing was previously deferred, but
+   only if in a queue run. Addresses that are to be routed are put on the
+   addr_route chain. Addresses that are to be deferred are put on the
+   addr_defer chain. We do all the checking first, so as not to keep the
+   retry database open any longer than necessary.
+
+ . Now we run the addresses through the routers. A router may put the address
+   on either the addr_local or the addr_remote chain for local or remote
+   delivery, respectively, or put it on the addr_failed chain if it is
+   undeliveable, or it may generate child addresses and put them on the
+   addr_new chain, or it may defer an address. All the chain anchors are
+   passed as arguments so that the routers can be called for verification
+   purposes as well.
+
+ . If new addresses have been generated by the routers, da capo.
+*/
+
+header_rewritten = FALSE;          /* No headers rewritten yet */
+while (addr_new != NULL)           /* Loop until all addresses dealt with */
+  {
+  address_item *addr, *parent;
+  dbm_file = dbfn_open(US"retry", O_RDONLY, &dbblock, FALSE);
+
+  /* Failure to open the retry database is treated the same as if it does
+  not exist. In both cases, dbm_file is NULL. */
+
+  if (dbm_file == NULL)
+    {
+    DEBUG(D_deliver|D_retry|D_route|D_hints_lookup)
+      debug_printf("no retry data available\n");
+    }
+
+  /* Scan the current batch of new addresses, to handle pipes, files and
+  autoreplies, and determine which others are ready for routing. */
+
+  while (addr_new != NULL)
+    {
+    int rc;
+    uschar *p;
+    tree_node *tnode;
+    dbdata_retry *domain_retry_record;
+    dbdata_retry *address_retry_record;
+
+    addr = addr_new;
+    addr_new = addr->next;
+
+    DEBUG(D_deliver|D_retry|D_route)
+      {
+      debug_printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
+      debug_printf("Considering: %s\n", addr->address);
+      }
+
+    /* Handle generated address that is a pipe or a file or an autoreply. */
+
+    if (testflag(addr, af_pfr))
+      {
+      /* If an autoreply in a filter could not generate a syntactically valid
+      address, give up forthwith. Set af_ignore_error so that we don't try to
+      generate a bounce. */
+
+      if (testflag(addr, af_bad_reply))
+        {
+        addr->basic_errno = ERRNO_BADADDRESS2;
+        addr->local_part = addr->address;
+        addr->message =
+          US"filter autoreply generated syntactically invalid recipient";
+        setflag(addr, af_ignore_error);
+        (void)post_process_one(addr, FAIL, LOG_MAIN, DTYPE_ROUTER, 0);
+        continue;   /* with the next new address */
+        }
+
+      /* If two different users specify delivery to the same pipe or file or
+      autoreply, there should be two different deliveries, so build a unique
+      string that incorporates the original address, and use this for
+      duplicate testing and recording delivery, and also for retrying. */
+
+      addr->unique =
+        string_sprintf("%s:%s", addr->address, addr->parent->unique +
+          (testflag(addr->parent, af_homonym)? 3:0));
+
+      addr->address_retry_key = addr->domain_retry_key =
+        string_sprintf("T:%s", addr->unique);
+
+      /* If a filter file specifies two deliveries to the same pipe or file,
+      we want to de-duplicate, but this is probably not wanted for two mail
+      commands to the same address, where probably both should be delivered.
+      So, we have to invent a different unique string in that case. Just
+      keep piling '>' characters on the front. */
+
+      if (addr->address[0] == '>')
+        {
+        while (tree_search(tree_duplicates, addr->unique) != NULL)
+          addr->unique = string_sprintf(">%s", addr->unique);
+        }
+
+      else if ((tnode = tree_search(tree_duplicates, addr->unique)) != NULL)
+        {
+        DEBUG(D_deliver|D_route)
+          debug_printf("%s is a duplicate address: discarded\n", addr->address);
+        addr->dupof = tnode->data.ptr;
+        addr->next = addr_duplicate;
+        addr_duplicate = addr;
+        continue;
+        }
+
+      DEBUG(D_deliver|D_route) debug_printf("unique = %s\n", addr->unique);
+
+      /* Check for previous delivery */
+
+      if (tree_search(tree_nonrecipients, addr->unique) != NULL)
+        {
+        DEBUG(D_deliver|D_route)
+          debug_printf("%s was previously delivered: discarded\n", addr->address);
+        child_done(addr, tod_stamp(tod_log));
+        continue;
+        }
+
+      /* Save for checking future duplicates */
+
+      tree_add_duplicate(addr->unique, addr);
+
+      /* Set local part and domain */
+
+      addr->local_part = addr->address;
+      addr->domain = addr->parent->domain;
+
+      /* Ensure that the delivery is permitted. */
+
+      if (testflag(addr, af_file))
+        {
+        if (!testflag(addr, af_allow_file))
+          {
+          addr->basic_errno = ERRNO_FORBIDFILE;
+          addr->message = US"delivery to file forbidden";
+          (void)post_process_one(addr, FAIL, LOG_MAIN, DTYPE_ROUTER, 0);
+          continue;   /* with the next new address */
+          }
+        }
+      else if (addr->address[0] == '|')
+        {
+        if (!testflag(addr, af_allow_pipe))
+          {
+          addr->basic_errno = ERRNO_FORBIDPIPE;
+          addr->message = US"delivery to pipe forbidden";
+          (void)post_process_one(addr, FAIL, LOG_MAIN, DTYPE_ROUTER, 0);
+          continue;   /* with the next new address */
+          }
+        }
+      else if (!testflag(addr, af_allow_reply))
+        {
+        addr->basic_errno = ERRNO_FORBIDREPLY;
+        addr->message = US"autoreply forbidden";
+        (void)post_process_one(addr, FAIL, LOG_MAIN, DTYPE_ROUTER, 0);
+        continue;     /* with the next new address */
+        }
+
+      /* If the errno field is already set to BADTRANSPORT, it indicates
+      failure to expand a transport string, or find the associated transport,
+      or an unset transport when one is required. Leave this test till now so
+      that the forbid errors are given in preference. */
+
+      if (addr->basic_errno == ERRNO_BADTRANSPORT)
+        {
+        (void)post_process_one(addr, DEFER, LOG_MAIN, DTYPE_ROUTER, 0);
+        continue;
+        }
+
+      /* Treat /dev/null as a special case and abandon the delivery. This
+      avoids having to specify a uid on the transport just for this case.
+      Arrange for the transport name to be logged as "**bypassed**". */
+
+      if (Ustrcmp(addr->address, "/dev/null") == 0)
+        {
+        uschar *save = addr->transport->name;
+        addr->transport->name = US"**bypassed**";
+        (void)post_process_one(addr, OK, LOG_MAIN, DTYPE_TRANSPORT, '=');
+        addr->transport->name = save;
+        continue;   /* with the next new address */
+        }
+
+      /* Pipe, file, or autoreply delivery is to go ahead as a normal local
+      delivery. */
+
+      DEBUG(D_deliver|D_route)
+        debug_printf("queued for %s transport\n", addr->transport->name);
+      addr->next = addr_local;
+      addr_local = addr;
+      continue;       /* with the next new address */
+      }
+
+    /* Handle normal addresses. First, split up into local part and domain,
+    handling the %-hack if necessary. There is the possibility of a defer from
+    a lookup in percent_hack_domains. */
+
+    if ((rc = deliver_split_address(addr)) == DEFER)
+      {
+      addr->message = US"cannot check percent_hack_domains";
+      addr->basic_errno = ERRNO_LISTDEFER;
+      (void)post_process_one(addr, DEFER, LOG_MAIN, DTYPE_NONE, 0);
+      continue;
+      }
+
+    /* Check to see if the domain is held. If so, proceed only if the
+    delivery was forced by hand. */
+
+    deliver_domain = addr->domain;  /* set $domain */
+    if (!forced && hold_domains != NULL &&
+         (rc = match_isinlist(addr->domain, &hold_domains, 0,
+           &domainlist_anchor, addr->domain_cache, MCL_DOMAIN, TRUE,
+           NULL)) != FAIL)
+      {
+      if (rc == DEFER)
+        {
+        addr->message = US"hold_domains lookup deferred";
+        addr->basic_errno = ERRNO_LISTDEFER;
+        }
+      else
+        {
+        addr->message = US"domain is held";
+        addr->basic_errno = ERRNO_HELD;
+        }
+      (void)post_process_one(addr, DEFER, LOG_MAIN, DTYPE_NONE, 0);
+      continue;
+      }
+
+    /* Now we can check for duplicates and previously delivered addresses. In
+    order to do this, we have to generate a "unique" value for each address,
+    because there may be identical actual addresses in a line of descendents.
+    The "unique" field is initialized to the same value as the "address" field,
+    but gets changed here to cope with identically-named descendents. */
+
+    for (parent = addr->parent; parent != NULL; parent = parent->parent)
+      if (strcmpic(addr->address, parent->address) == 0) break;
+
+    /* If there's an ancestor with the same name, set the homonym flag. This
+    influences how deliveries are recorded. Then add a prefix on the front of
+    the unique address. We use \n\ where n starts at 0 and increases each time.
+    It is unlikely to pass 9, but if it does, it may look odd but will still
+    work. This means that siblings or cousins with the same names are treated
+    as duplicates, which is what we want. */
+
+    if (parent != NULL)
+      {
+      setflag(addr, af_homonym);
+      if (parent->unique[0] != '\\')
+        addr->unique = string_sprintf("\\0\\%s", addr->address);
+      else
+        addr->unique = string_sprintf("\\%c\\%s", parent->unique[1] + 1,
+          addr->address);
+      }
+
+    /* Ensure that the domain in the unique field is lower cased, because
+    domains are always handled caselessly. */
+
+    p = Ustrrchr(addr->unique, '@');
+    while (*p != 0) { *p = tolower(*p); p++; }
+
+    DEBUG(D_deliver|D_route) debug_printf("unique = %s\n", addr->unique);
+
+    if (tree_search(tree_nonrecipients, addr->unique) != NULL)
+      {
+      DEBUG(D_deliver|D_route)
+        debug_printf("%s was previously delivered: discarded\n", addr->unique);
+      child_done(addr, tod_stamp(tod_log));
+      continue;
+      }
+
+    /* Get the routing retry status, saving the two retry keys (with and
+    without the local part) for subsequent use. If there is no retry record for
+    the standard address routing retry key, we look for the same key with the
+    sender attached, because this form is used by the smtp transport after a
+    4xx response to RCPT when address_retry_include_sender is true. */
+
+    addr->domain_retry_key = string_sprintf("R:%s", addr->domain);
+    addr->address_retry_key = string_sprintf("R:%s@%s", addr->local_part,
+      addr->domain);
+
+    if (dbm_file == NULL)
+      domain_retry_record = address_retry_record = NULL;
+    else
+      {
+      domain_retry_record = dbfn_read(dbm_file, addr->domain_retry_key);
+      if (domain_retry_record != NULL &&
+          now - domain_retry_record->time_stamp > retry_data_expire)
+        domain_retry_record = NULL;    /* Ignore if too old */
+
+      address_retry_record = dbfn_read(dbm_file, addr->address_retry_key);
+      if (address_retry_record != NULL &&
+          now - address_retry_record->time_stamp > retry_data_expire)
+        address_retry_record = NULL;   /* Ignore if too old */
+
+      if (address_retry_record == NULL)
+        {
+        uschar *altkey = string_sprintf("%s:<%s>", addr->address_retry_key,
+          sender_address);
+        address_retry_record = dbfn_read(dbm_file, altkey);
+        if (address_retry_record != NULL &&
+            now - address_retry_record->time_stamp > retry_data_expire)
+          address_retry_record = NULL;   /* Ignore if too old */
+        }
+      }
+
+    DEBUG(D_deliver|D_retry)
+      {
+      if (domain_retry_record == NULL)
+        debug_printf("no domain retry record\n");
+      if (address_retry_record == NULL)
+        debug_printf("no address retry record\n");
+      }
+
+    /* If we are sending a message down an existing SMTP connection, we must
+    assume that the message which created the connection managed to route
+    an address to that connection. We do not want to run the risk of taking
+    a long time over routing here, because if we do, the server at the other
+    end of the connection may time it out. This is especially true for messages
+    with lots of addresses. For this kind of delivery, queue_running is not
+    set, so we would normally route all addresses. We take a pragmatic approach
+    and defer routing any addresses that have any kind of domain retry record.
+    That is, we don't even look at their retry times. It doesn't matter if this
+    doesn't work occasionally. This is all just an optimization, after all.
+
+    The reason for not doing the same for address retries is that they normally
+    arise from 4xx responses, not DNS timeouts. */
+
+    if (continue_hostname != NULL && domain_retry_record != NULL)
+      {
+      addr->message = US"reusing SMTP connection skips previous routing defer";
+      addr->basic_errno = ERRNO_RRETRY;
+      (void)post_process_one(addr, DEFER, LOG_MAIN, DTYPE_ROUTER, 0);
+      }
+
+    /* If we are in a queue run, defer routing unless there is no retry data or
+    we've passed the next retry time, or this message is forced. In other
+    words, ignore retry data when not in a queue run.
+
+    However, if the domain retry time has expired, always allow the routing
+    attempt. If it fails again, the address will be failed. This ensures that
+    each address is routed at least once, even after long-term routing
+    failures.
+
+    If there is an address retry, check that too; just wait for the next
+    retry time. This helps with the case when the temporary error on the
+    address was really message-specific rather than address specific, since
+    it allows other messages through.
+
+    We also wait for the next retry time if this is a message sent down an
+    existing SMTP connection (even though that will be forced). Otherwise there
+    will be far too many attempts for an address that gets a 4xx error. In
+    fact, after such an error, we should not get here because, the host should
+    not be remembered as one this message needs. However, there was a bug that
+    used to cause this to  happen, so it is best to be on the safe side.
+
+    Even if we haven't reached the retry time in the hints, there is one more
+    check to do, which is for the ultimate address timeout. We only do this
+    check if there is an address retry record and there is not a domain retry
+    record; this implies that previous attempts to handle the address had the
+    retry_use_local_parts option turned on. We use this as an approximation
+    for the destination being like a local delivery, for example delivery over
+    LMTP to an IMAP message store. In this situation users are liable to bump
+    into their quota and thereby have intermittently successful deliveries,
+    which keep the retry record fresh, which can lead to us perpetually
+    deferring messages. */
+
+    else if (((queue_running && !deliver_force) || continue_hostname != NULL)
+            &&
+            ((domain_retry_record != NULL &&
+              now < domain_retry_record->next_try &&
+              !domain_retry_record->expired)
+            ||
+            (address_retry_record != NULL &&
+              now < address_retry_record->next_try))
+            &&
+           (domain_retry_record != NULL ||
+            address_retry_record == NULL ||
+            !retry_ultimate_address_timeout(addr->address_retry_key,
+              addr->domain, address_retry_record, now)))
+      {
+      addr->message = US"retry time not reached";
+      addr->basic_errno = ERRNO_RRETRY;
+      (void)post_process_one(addr, DEFER, LOG_MAIN, DTYPE_ROUTER, 0);
+      }
+
+    /* The domain is OK for routing. Remember if retry data exists so it
+    can be cleaned up after a successful delivery. */
+
+    else
+      {
+      if (domain_retry_record != NULL || address_retry_record != NULL)
+        setflag(addr, af_dr_retry_exists);
+      addr->next = addr_route;
+      addr_route = addr;
+      DEBUG(D_deliver|D_route)
+        debug_printf("%s: queued for routing\n", addr->address);
+      }
+    }
+
+  /* The database is closed while routing is actually happening. Requests to
+  update it are put on a chain and all processed together at the end. */
+
+  if (dbm_file != NULL) dbfn_close(dbm_file);
+
+  /* If queue_domains is set, we don't even want to try routing addresses in
+  those domains. During queue runs, queue_domains is forced to be unset.
+  Optimize by skipping this pass through the addresses if nothing is set. */
+
+  if (!deliver_force && queue_domains != NULL)
+    {
+    address_item *okaddr = NULL;
+    while (addr_route != NULL)
+      {
+      address_item *addr = addr_route;
+      addr_route = addr->next;
+
+      deliver_domain = addr->domain;  /* set $domain */
+      if ((rc = match_isinlist(addr->domain, &queue_domains, 0,
+            &domainlist_anchor, addr->domain_cache, MCL_DOMAIN, TRUE, NULL))
+              != OK)
+        {
+        if (rc == DEFER)
+          {
+          addr->basic_errno = ERRNO_LISTDEFER;
+          addr->message = US"queue_domains lookup deferred";
+          (void)post_process_one(addr, DEFER, LOG_MAIN, DTYPE_ROUTER, 0);
+          }
+        else
+          {
+          addr->next = okaddr;
+          okaddr = addr;
+          }
+        }
+      else
+        {
+        addr->basic_errno = ERRNO_QUEUE_DOMAIN;
+        addr->message = US"domain is in queue_domains";
+        (void)post_process_one(addr, DEFER, LOG_MAIN, DTYPE_ROUTER, 0);
+        }
+      }
+
+    addr_route = okaddr;
+    }
+
+  /* Now route those addresses that are not deferred. */
+
+  while (addr_route != NULL)
+    {
+    int rc;
+    address_item *addr = addr_route;
+    uschar *old_domain = addr->domain;
+    uschar *old_unique = addr->unique;
+    addr_route = addr->next;
+    addr->next = NULL;
+
+    /* Just in case some router parameter refers to it. */
+
+    return_path = (addr->p.errors_address != NULL)?
+      addr->p.errors_address : sender_address;
+
+    /* If a router defers an address, add a retry item. Whether or not to
+    use the local part in the key is a property of the router. */
+
+    if ((rc = route_address(addr, &addr_local, &addr_remote, &addr_new,
+         &addr_succeed, v_none)) == DEFER)
+      retry_add_item(addr, (addr->router->retry_use_local_part)?
+        string_sprintf("R:%s@%s", addr->local_part, addr->domain) :
+        string_sprintf("R:%s", addr->domain), 0);
+
+    /* Otherwise, if there is an existing retry record in the database, add
+    retry items to delete both forms. We must also allow for the possibility
+    of a routing retry that includes the sender address. Since the domain might
+    have been rewritten (expanded to fully qualified) as a result of routing,
+    ensure that the rewritten form is also deleted. */
+
+    else if (testflag(addr, af_dr_retry_exists))
+      {
+      uschar *altkey = string_sprintf("%s:<%s>", addr->address_retry_key,
+        sender_address);
+      retry_add_item(addr, altkey, rf_delete);
+      retry_add_item(addr, addr->address_retry_key, rf_delete);
+      retry_add_item(addr, addr->domain_retry_key, rf_delete);
+      if (Ustrcmp(addr->domain, old_domain) != 0)
+        retry_add_item(addr, string_sprintf("R:%s", old_domain), rf_delete);
+      }
+
+    /* DISCARD is given for :blackhole: and "seen finish". The event has been
+    logged, but we need to ensure the address (and maybe parents) is marked
+    done. */
+
+    if (rc == DISCARD)
+      {
+      address_done(addr, tod_stamp(tod_log));
+      continue;  /* route next address */
+      }
+
+    /* The address is finished with (failed or deferred). */
+
+    if (rc != OK)
+      {
+      (void)post_process_one(addr, rc, LOG_MAIN, DTYPE_ROUTER, 0);
+      continue;  /* route next address */
+      }
+
+    /* The address has been routed. If the router changed the domain, it will
+    also have changed the unique address. We have to test whether this address
+    has already been delivered, because it's the unique address that finally
+    gets recorded. */
+
+    if (addr->unique != old_unique &&
+        tree_search(tree_nonrecipients, addr->unique) != 0)
+      {
+      DEBUG(D_deliver|D_route) debug_printf("%s was previously delivered: "
+        "discarded\n", addr->address);
+      if (addr_remote == addr) addr_remote = addr->next;
+      else if (addr_local == addr) addr_local = addr->next;
+      }
+
+    /* If the router has same_domain_copy_routing set, we are permitted to copy
+    the routing for any other addresses with the same domain. This is an
+    optimisation to save repeated DNS lookups for "standard" remote domain
+    routing. The option is settable only on routers that generate host lists.
+    We play it very safe, and do the optimization only if the address is routed
+    to a remote transport, there are no header changes, and the domain was not
+    modified by the router. */
+
+    if (addr_remote == addr &&
+        addr->router->same_domain_copy_routing &&
+        addr->p.extra_headers == NULL &&
+        addr->p.remove_headers == NULL &&
+        old_domain == addr->domain)
+      {
+      address_item **chain = &addr_route;
+      while (*chain != NULL)
+        {
+        address_item *addr2 = *chain;
+        if (Ustrcmp(addr2->domain, addr->domain) != 0)
+          {
+          chain = &(addr2->next);
+          continue;
+          }
+
+        /* Found a suitable address; take it off the routing list and add it to
+        the remote delivery list. */
+
+        *chain = addr2->next;
+        addr2->next = addr_remote;
+        addr_remote = addr2;
+
+        /* Copy the routing data */
+
+        addr2->domain = addr->domain;
+        addr2->router = addr->router;
+        addr2->transport = addr->transport;
+        addr2->host_list = addr->host_list;
+        addr2->fallback_hosts = addr->fallback_hosts;
+        addr2->p.errors_address = addr->p.errors_address;
+        copyflag(addr2, addr, af_hide_child | af_local_host_removed);
+
+        DEBUG(D_deliver|D_route)
+          {
+          debug_printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n"
+                       "routing %s\n"
+                       "Routing for %s copied from %s\n",
+            addr2->address, addr2->address, addr->address);
+          }
+        }
+      }
+    }  /* Continue with routing the next address. */
+  }    /* Loop to process any child addresses that the routers created, and
+          any rerouted addresses that got put back on the new chain. */
+
+
+/* Debugging: show the results of the routing */
+
+DEBUG(D_deliver|D_retry|D_route)
+  {
+  address_item *p = addr_local;
+  debug_printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
+  debug_printf("After routing:\n  Local deliveries:\n");
+  while (p != NULL)
+    {
+    debug_printf("    %s\n", p->address);
+    p = p->next;
+    }
+
+  p = addr_remote;
+  debug_printf("  Remote deliveries:\n");
+  while (p != NULL)
+    {
+    debug_printf("    %s\n", p->address);
+    p = p->next;
+    }
+
+  p = addr_failed;
+  debug_printf("  Failed addresses:\n");
+  while (p != NULL)
+    {
+    debug_printf("    %s\n", p->address);
+    p = p->next;
+    }
+
+  p = addr_defer;
+  debug_printf("  Deferred addresses:\n");
+  while (p != NULL)
+    {
+    debug_printf("    %s\n", p->address);
+    p = p->next;
+    }
+  }
+
+/* Free any resources that were cached during routing. */
+
+search_tidyup();
+route_tidyup();
+
+/* These two variables are set only during routing, after check_local_user.
+Ensure they are not set in transports. */
+
+local_user_gid = (gid_t)(-1);
+local_user_uid = (uid_t)(-1);
+
+/* Check for any duplicate addresses. This check is delayed until after
+routing, because the flexibility of the routing configuration means that
+identical addresses with different parentage may end up being redirected to
+different addresses. Checking for duplicates too early (as we previously used
+to) makes this kind of thing not work. */
+
+do_duplicate_check(&addr_local);
+do_duplicate_check(&addr_remote);
+
+/* When acting as an MUA wrapper, we proceed only if all addresses route to a
+remote transport. The check that they all end up in one transaction happens in
+the do_remote_deliveries() function. */
+
+if (mua_wrapper && (addr_local != NULL || addr_failed != NULL ||
+                    addr_defer != NULL))
+  {
+  address_item *addr;
+  uschar *which, *colon, *msg;
+
+  if (addr_local != NULL)
+    {
+    addr = addr_local;
+    which = US"local";
+    }
+  else if (addr_defer != NULL)
+    {
+    addr = addr_defer;
+    which = US"deferred";
+    }
+  else
+    {
+    addr = addr_failed;
+    which = US"failed";
+    }
+
+  while (addr->parent != NULL) addr = addr->parent;
+
+  if (addr->message != NULL)
+    {
+    colon = US": ";
+    msg = addr->message;
+    }
+  else colon = msg = US"";
+
+  /* We don't need to log here for a forced failure as it will already
+  have been logged. Defer will also have been logged, but as a defer, so we do
+  need to do the failure logging. */
+
+  if (addr != addr_failed)
+    log_write(0, LOG_MAIN, "** %s routing yielded a %s delivery",
+      addr->address, which);
+
+  /* Always write an error to the caller */
+
+  fprintf(stderr, "routing %s yielded a %s delivery%s%s\n", addr->address,
+    which, colon, msg);
+
+  final_yield = DELIVER_MUA_FAILED;
+  addr_failed = addr_defer = NULL;   /* So that we remove the message */
+  goto DELIVERY_TIDYUP;
+  }
+
+
+/* If this is a run to continue deliveries to an external channel that is
+already set up, defer any local deliveries. */
+
+if (continue_transport != NULL)
+  {
+  if (addr_defer == NULL) addr_defer = addr_local; else
+    {
+    address_item *addr = addr_defer;
+    while (addr->next != NULL) addr = addr->next;
+    addr->next = addr_local;
+    }
+  addr_local = NULL;
+  }
+
+
+/* Because address rewriting can happen in the routers, we should not really do
+ANY deliveries until all addresses have been routed, so that all recipients of
+the message get the same headers. However, this is in practice not always
+possible, since sometimes remote addresses give DNS timeouts for days on end.
+The pragmatic approach is to deliver what we can now, saving any rewritten
+headers so that at least the next lot of recipients benefit from the rewriting
+that has already been done.
+
+If any headers have been rewritten during routing, update the spool file to
+remember them for all subsequent deliveries. This can be delayed till later if
+there is only address to be delivered - if it succeeds the spool write need not
+happen. */
+
+if (header_rewritten &&
+    ((addr_local != NULL &&
+       (addr_local->next != NULL || addr_remote != NULL)) ||
+     (addr_remote != NULL && addr_remote->next != NULL)))
+  {
+  /* Panic-dies on error */
+  (void)spool_write_header(message_id, SW_DELIVERING, NULL);
+  header_rewritten = FALSE;
+  }
+
+
+/* If there are any deliveries to be done, open the journal file. This is used
+to record successful deliveries as soon as possible after each delivery is
+known to be complete. A file opened with O_APPEND is used so that several
+processes can run simultaneously.
+
+The journal is just insurance against crashes. When the spool file is
+ultimately updated at the end of processing, the journal is deleted. If a
+journal is found to exist at the start of delivery, the addresses listed
+therein are added to the non-recipients. */
+
+if (addr_local != NULL || addr_remote != NULL)
+  {
+  sprintf(CS spoolname, "%s/input/%s/%s-J", spool_directory, message_subdir, id);
+  journal_fd = Uopen(spoolname, O_WRONLY|O_APPEND|O_CREAT, SPOOL_MODE);
+
+  if (journal_fd < 0)
+    {
+    log_write(0, LOG_MAIN|LOG_PANIC, "Couldn't open journal file %s: %s",
+      spoolname, strerror(errno));
+    return DELIVER_NOT_ATTEMPTED;
+    }
+
+  /* Set the close-on-exec flag, make the file owned by Exim, and ensure
+  that the mode is correct - the group setting doesn't always seem to get
+  set automatically. */
+
+  if(  fcntl(journal_fd, F_SETFD, fcntl(journal_fd, F_GETFD) | FD_CLOEXEC)
+    || fchown(journal_fd, exim_uid, exim_gid)
+    || fchmod(journal_fd, SPOOL_MODE)
+    )
+    {
+    int ret = Uunlink(spoolname);
+    log_write(0, LOG_MAIN|LOG_PANIC, "Couldn't set perms on journal file %s: %s",
+      spoolname, strerror(errno));
+    if(ret  &&  errno != ENOENT)
+      log_write(0, LOG_MAIN|LOG_PANIC_DIE, "failed to unlink %s: %s",
+        spoolname, strerror(errno));
+    return DELIVER_NOT_ATTEMPTED;
+    }
+  }
+
+
+
+/* Now we can get down to the business of actually doing deliveries. Local
+deliveries are done first, then remote ones. If ever the problems of how to
+handle fallback transports are figured out, this section can be put into a loop
+for handling fallbacks, though the uid switching will have to be revised. */
+
+/* Precompile a regex that is used to recognize a parameter in response
+to an LHLO command, if is isn't already compiled. This may be used on both
+local and remote LMTP deliveries. */
+
+if (regex_IGNOREQUOTA == NULL) regex_IGNOREQUOTA =
+  regex_must_compile(US"\\n250[\\s\\-]IGNOREQUOTA(\\s|\\n|$)", FALSE, TRUE);
+
+/* Handle local deliveries */
+
+if (addr_local != NULL)
+  {
+  DEBUG(D_deliver|D_transport)
+    debug_printf(">>>>>>>>>>>>>>>> Local deliveries >>>>>>>>>>>>>>>>\n");
+  do_local_deliveries();
+  disable_logging = FALSE;
+  }
+
+/* If queue_run_local is set, we do not want to attempt any remote deliveries,
+so just queue them all. */
+
+if (queue_run_local)
+  {
+  while (addr_remote != NULL)
+    {
+    address_item *addr = addr_remote;
+    addr_remote = addr->next;
+    addr->next = NULL;
+    addr->basic_errno = ERRNO_LOCAL_ONLY;
+    addr->message = US"remote deliveries suppressed";
+    (void)post_process_one(addr, DEFER, LOG_MAIN, DTYPE_TRANSPORT, 0);
+    }
+  }
+
+/* Handle remote deliveries */
+
+if (addr_remote != NULL)
+  {
+  DEBUG(D_deliver|D_transport)
+    debug_printf(">>>>>>>>>>>>>>>> Remote deliveries >>>>>>>>>>>>>>>>\n");
+
+  /* Precompile some regex that are used to recognize parameters in response
+  to an EHLO command, if they aren't already compiled. */
+
+  if (regex_PIPELINING == NULL) regex_PIPELINING =
+    regex_must_compile(US"\\n250[\\s\\-]PIPELINING(\\s|\\n|$)", FALSE, TRUE);
+
+  if (regex_SIZE == NULL) regex_SIZE =
+    regex_must_compile(US"\\n250[\\s\\-]SIZE(\\s|\\n|$)", FALSE, TRUE);
+
+  if (regex_AUTH == NULL) regex_AUTH =
+    regex_must_compile(US"\\n250[\\s\\-]AUTH\\s+([\\-\\w\\s]+)(?:\\n|$)",
+      FALSE, TRUE);
+
+  #ifdef SUPPORT_TLS
+  if (regex_STARTTLS == NULL) regex_STARTTLS =
+    regex_must_compile(US"\\n250[\\s\\-]STARTTLS(\\s|\\n|$)", FALSE, TRUE);
+  #endif
+
+  #ifndef DISABLE_PRDR
+  if (regex_PRDR == NULL) regex_PRDR =
+    regex_must_compile(US"\\n250[\\s\\-]PRDR(\\s|\\n|$)", FALSE, TRUE);
+  #endif
+
+  #ifdef EXPERIMENTAL_DSN
+  /* Set the regex to check for DSN support on remote MTA */
+  if (regex_DSN == NULL) regex_DSN  =
+    regex_must_compile(US"\\n250[\\s\\-]DSN(\\s|\\n|$)", FALSE, TRUE);
+  #endif
+
+  /* Now sort the addresses if required, and do the deliveries. The yield of
+  do_remote_deliveries is FALSE when mua_wrapper is set and all addresses
+  cannot be delivered in one transaction. */
+
+  if (remote_sort_domains != NULL) sort_remote_deliveries();
+  if (!do_remote_deliveries(FALSE))
+    {
+    log_write(0, LOG_MAIN, "** mua_wrapper is set but recipients cannot all "
+      "be delivered in one transaction");
+    fprintf(stderr, "delivery to smarthost failed (configuration problem)\n");
+
+    final_yield = DELIVER_MUA_FAILED;
+    addr_failed = addr_defer = NULL;   /* So that we remove the message */
+    goto DELIVERY_TIDYUP;
+    }
+
+  /* See if any of the addresses that failed got put on the queue for delivery
+  to their fallback hosts. We do it this way because often the same fallback
+  host is used for many domains, so all can be sent in a single transaction
+  (if appropriately configured). */
+
+  if (addr_fallback != NULL && !mua_wrapper)
+    {
+    DEBUG(D_deliver) debug_printf("Delivering to fallback hosts\n");
+    addr_remote = addr_fallback;
+    addr_fallback = NULL;
+    if (remote_sort_domains != NULL) sort_remote_deliveries();
+    do_remote_deliveries(TRUE);
+    }
+  disable_logging = FALSE;
+  }
+
+
+/* All deliveries are now complete. Ignore SIGTERM during this tidying up
+phase, to minimize cases of half-done things. */
+
+DEBUG(D_deliver)
+  debug_printf(">>>>>>>>>>>>>>>> deliveries are done >>>>>>>>>>>>>>>>\n");
+
+/* Root privilege is no longer needed */
+
+exim_setugid(exim_uid, exim_gid, FALSE, US"post-delivery tidying");
+
+set_process_info("tidying up after delivering %s", message_id);
+signal(SIGTERM, SIG_IGN);
+
+/* When we are acting as an MUA wrapper, the smtp transport will either have
+succeeded for all addresses, or failed them all in normal cases. However, there
+are some setup situations (e.g. when a named port does not exist) that cause an
+immediate exit with deferral of all addresses. Convert those into failures. We
+do not ever want to retry, nor do we want to send a bounce message. */
+
+if (mua_wrapper)
+  {
+  if (addr_defer != NULL)
+    {
+    address_item *addr, *nextaddr;
+    for (addr = addr_defer; addr != NULL; addr = nextaddr)
+      {
+      log_write(0, LOG_MAIN, "** %s mua_wrapper forced failure for deferred "
+        "delivery", addr->address);
+      nextaddr = addr->next;
+      addr->next = addr_failed;
+      addr_failed = addr;
+      }
+    addr_defer = NULL;
+    }
+
+  /* Now all should either have succeeded or failed. */
+
+  if (addr_failed == NULL) final_yield = DELIVER_MUA_SUCCEEDED; else
+    {
+    uschar *s = (addr_failed->user_message != NULL)?
+      addr_failed->user_message : addr_failed->message;
+
+    fprintf(stderr, "Delivery failed: ");
+    if (addr_failed->basic_errno > 0)
+      {
+      fprintf(stderr, "%s", strerror(addr_failed->basic_errno));
+      if (s != NULL) fprintf(stderr, ": ");
+      }
+    if (s == NULL)
+      {
+      if (addr_failed->basic_errno <= 0) fprintf(stderr, "unknown error");
+      }
+    else fprintf(stderr, "%s", CS s);
+    fprintf(stderr, "\n");
+
+    final_yield = DELIVER_MUA_FAILED;
+    addr_failed = NULL;
+    }
+  }
+
+/* In a normal configuration, we now update the retry database. This is done in
+one fell swoop at the end in order not to keep opening and closing (and
+locking) the database. The code for handling retries is hived off into a
+separate module for convenience. We pass it the addresses of the various
+chains, because deferred addresses can get moved onto the failed chain if the
+retry cutoff time has expired for all alternative destinations. Bypass the
+updating of the database if the -N flag is set, which is a debugging thing that
+prevents actual delivery. */
+
+else if (!dont_deliver) retry_update(&addr_defer, &addr_failed, &addr_succeed);
+
+#ifdef EXPERIMENTAL_DSN
+/* Send DSN for successful messages */
+addr_dsntmp = addr_succeed;
+addr_senddsn = NULL;
+
+while(addr_dsntmp != NULL)
+  {
+  DEBUG(D_deliver)
+    debug_printf("DSN: processing router : %s\n", addr_dsntmp->router->name);
+
+  DEBUG(D_deliver)
+    debug_printf("DSN: processing successful delivery address: %s\n", addr_dsntmp->address);
+
+  /* af_ignore_error not honored here. it's not an error */
+
+  DEBUG(D_deliver) debug_printf("DSN: Sender_address: %s\n", sender_address);
+  DEBUG(D_deliver) debug_printf("DSN: orcpt: %s  flags: %d\n", addr_dsntmp->dsn_orcpt, addr_dsntmp->dsn_flags);
+  DEBUG(D_deliver) debug_printf("DSN: envid: %s  ret: %d\n", dsn_envid, dsn_ret);
+  DEBUG(D_deliver) debug_printf("DSN: Final recipient: %s\n", addr_dsntmp->address);
+  DEBUG(D_deliver) debug_printf("DSN: Remote SMTP server supports DSN: %d\n", addr_dsntmp->dsn_aware);
+
+  /* send report if next hop not DSN aware or a router flagged "last DSN hop"
+     and a report was requested */
+  if (((addr_dsntmp->dsn_aware != dsn_support_yes) ||
+       ((addr_dsntmp->dsn_flags & rf_dsnlasthop) != 0))
+      &&
+      (((addr_dsntmp->dsn_flags & rf_dsnflags) != 0) &&
+        ((addr_dsntmp->dsn_flags & rf_notify_success) != 0)))
+    {
+    /* copy and relink address_item and send report with all of them at once later */
+    address_item *addr_next;
+    addr_next = addr_senddsn;
+    addr_senddsn = store_get(sizeof(address_item));
+    memcpy(addr_senddsn, addr_dsntmp, sizeof(address_item));
+    addr_senddsn->next = addr_next;
+    }
+  else
+    {
+      DEBUG(D_deliver) debug_printf("DSN: *** NOT SENDING DSN SUCCESS Message ***\n"); 
+    }
+
+  addr_dsntmp = addr_dsntmp->next;
+  }
+
+if (addr_senddsn != NULL)
+  {
+  pid_t pid;
+  int fd;
+
+  /* create exim process to send message */      
+  pid = child_open_exim(&fd);
+
+  DEBUG(D_deliver) debug_printf("DSN: child_open_exim returns: %d\n", pid);
+     
+  if (pid < 0)  /* Creation of child failed */
+    {
+    log_write(0, LOG_MAIN|LOG_PANIC_DIE, "Process %d (parent %d) failed to "
+      "create child process to send failure message: %s", getpid(),
+      getppid(), strerror(errno));
+
+      DEBUG(D_deliver) debug_printf("DSN: child_open_exim failed\n");
+
+    }    
+  else  /* Creation of child succeeded */
+    {
+    FILE *f = fdopen(fd, "wb");
+    /* header only as required by RFC. only failure DSN needs to honor RET=FULL */
+    int topt = topt_add_return_path | topt_no_body;
+    uschar boundaryStr[64];
+     
+    DEBUG(D_deliver) debug_printf("sending error message to: %s\n", sender_address);
+  
+    /* build unique id for MIME boundary */
+    snprintf(boundaryStr, sizeof(boundaryStr)-1, TIME_T_FMT "-eximdsn-%d",
+      time(NULL), rand());
+    DEBUG(D_deliver) debug_printf("DSN: MIME boundary: %s\n", boundaryStr);
+  
+    if (errors_reply_to)
+      fprintf(f, "Reply-To: %s\n", errors_reply_to);
+    fprintf(f, "Auto-Submitted: auto-generated\n"
+       "From: Mail Delivery System <Mailer-Daemon@%s>\n"
+       "To: %s\n"
+       "Subject: Delivery Status Notification\n"
+       "Content-Type: multipart/report; report-type=delivery-status; boundary=%s\n"
+       "MIME-Version: 1.0\n\n"
+
+       "--%s\n"
+       "Content-type: text/plain; charset=us-ascii\n\n"
+   
+       "This message was created automatically by mail delivery software.\n"
+       " ----- The following addresses had successful delivery notifications -----\n",
+      qualify_domain_sender, sender_address, boundaryStr, boundaryStr);
+
+    addr_dsntmp = addr_senddsn;
+    while(addr_dsntmp)
+      {
+      fprintf(f, "<%s> (relayed %s)\n\n",
+       addr_dsntmp->address,
+       (addr_dsntmp->dsn_flags & rf_dsnlasthop) == 1
+         ? "via non DSN router"
+         : addr_dsntmp->dsn_aware == dsn_support_no
+         ? "to non-DSN-aware mailer"
+         : "via non \"Remote SMTP\" router"
+       );
+      addr_dsntmp = addr_dsntmp->next;
+      }
+    fprintf(f, "--%s\n"
+       "Content-type: message/delivery-status\n\n"
+       "Reporting-MTA: dns; %s\n",
+      boundaryStr, smtp_active_hostname);
+
+    if (dsn_envid != NULL) {
+      /* must be decoded from xtext: see RFC 3461:6.3a */
+      uschar *xdec_envid;
+      if (auth_xtextdecode(dsn_envid, &xdec_envid) > 0)
+        fprintf(f, "Original-Envelope-ID: %s\n", dsn_envid);
+      else
+        fprintf(f, "X-Original-Envelope-ID: error decoding xtext formated ENVID\n");
+      }
+    fputc('\n', f);
+
+    for (addr_dsntmp = addr_senddsn;
+        addr_dsntmp;
+        addr_dsntmp = addr_dsntmp->next)
+      {
+      if (addr_dsntmp->dsn_orcpt)
+        fprintf(f,"Original-Recipient: %s\n", addr_dsntmp->dsn_orcpt);
+
+      fprintf(f, "Action: delivered\n"
+         "Final-Recipient: rfc822;%s\n"
+         "Status: 2.0.0\n",
+       addr_dsntmp->address);
+
+      if (addr_dsntmp->host_used && addr_dsntmp->host_used->name)
+        fprintf(f, "Remote-MTA: dns; %s\nDiagnostic-Code: smtp; 250 Ok\n",
+         addr_dsntmp->host_used->name);
+      else
+       fprintf(f,"Diagnostic-Code: X-Exim; relayed via non %s router\n",
+         (addr_dsntmp->dsn_flags & rf_dsnlasthop) == 1 ? "DSN" : "SMTP");
+      fputc('\n', f);
+      }
+
+    fprintf(f, "--%s\nContent-type: text/rfc822-headers\n\n", boundaryStr);
+           
+    fflush(f);
+    transport_filter_argv = NULL;   /* Just in case */
+    return_path = sender_address;   /* In case not previously set */
+           
+    /* Write the original email out */
+    transport_write_message(NULL, fileno(f), topt, 0, NULL, NULL, NULL, NULL, NULL, 0);
+    fflush(f);
+
+    fprintf(f,"\n");       
+    fprintf(f,"--%s--\n", boundaryStr);
+
+    fflush(f);
+    fclose(f);
+    rc = child_close(pid, 0);     /* Waits for child to close, no timeout */
+    }
+  }
+#endif /*EXPERIMENTAL_DSN*/
+
+/* If any addresses failed, we must send a message to somebody, unless
+af_ignore_error is set, in which case no action is taken. It is possible for
+several messages to get sent if there are addresses with different
+requirements. */
+
+while (addr_failed != NULL)
+  {
+  pid_t pid;
+  int fd;
+  uschar *logtod = tod_stamp(tod_log);
+  address_item *addr;
+  address_item *handled_addr = NULL;
+  address_item **paddr;
+  address_item *msgchain = NULL;
+  address_item **pmsgchain = &msgchain;
+
+  /* There are weird cases when logging is disabled in the transport. However,
+  there may not be a transport (address failed by a router). */
+
+  disable_logging = FALSE;
+  if (addr_failed->transport != NULL)
+    disable_logging = addr_failed->transport->disable_logging;
+
+  DEBUG(D_deliver)
+    debug_printf("processing failed address %s\n", addr_failed->address);
+
+  /* There are only two ways an address in a bounce message can get here:
+
+  (1) When delivery was initially deferred, but has now timed out (in the call
+      to retry_update() above). We can detect this by testing for
+      af_retry_timedout. If the address does not have its own errors address,
+      we arrange to ignore the error.
+
+  (2) If delivery failures for bounce messages are being ignored. We can detect
+      this by testing for af_ignore_error. This will also be set if a bounce
+      message has been autothawed and the ignore_bounce_errors_after time has
+      passed. It might also be set if a router was explicitly configured to
+      ignore errors (errors_to = "").
+
+  If neither of these cases obtains, something has gone wrong. Log the
+  incident, but then ignore the error. */
+
+  if (sender_address[0] == 0 && addr_failed->p.errors_address == NULL)
+    {
+    if (!testflag(addr_failed, af_retry_timedout) &&
+        !testflag(addr_failed, af_ignore_error))
+      {
+      log_write(0, LOG_MAIN|LOG_PANIC, "internal error: bounce message "
+        "failure is neither frozen nor ignored (it's been ignored)");
+      }
+    setflag(addr_failed, af_ignore_error);
+    }
+
+  /* If the first address on the list has af_ignore_error set, just remove
+  it from the list, throw away any saved message file, log it, and
+  mark the recipient done. */
+
+  if (testflag(addr_failed, af_ignore_error)
+#ifdef EXPERIMENTAL_DSN
+      || (((addr_failed->dsn_flags & rf_dsnflags) != 0)
+         && ((addr_failed->dsn_flags & rf_notify_failure) != rf_notify_failure))
+#endif
+     )
+  {
+    addr = addr_failed;
+    addr_failed = addr->next;
+    if (addr->return_filename != NULL) Uunlink(addr->return_filename);
+
+    log_write(0, LOG_MAIN, "%s%s%s%s: error ignored",
+      addr->address,
+      (addr->parent == NULL)? US"" : US" <",
+      (addr->parent == NULL)? US"" : addr->parent->address,
+      (addr->parent == NULL)? US"" : US">");
+
+    address_done(addr, logtod);
+    child_done(addr, logtod);
+    /* Panic-dies on error */
+    (void)spool_write_header(message_id, SW_DELIVERING, NULL);
+    }
+
+  /* Otherwise, handle the sending of a message. Find the error address for
+  the first address, then send a message that includes all failed addresses
+  that have the same error address. Note the bounce_recipient is a global so
+  that it can be accesssed by $bounce_recipient while creating a customized
+  error message. */
+
+  else
+    {
+    bounce_recipient = (addr_failed->p.errors_address == NULL)?
+      sender_address : addr_failed->p.errors_address;
+
+    /* Make a subprocess to send a message */
+
+    pid = child_open_exim(&fd);
+
+    /* Creation of child failed */
+
+    if (pid < 0)
+      log_write(0, LOG_MAIN|LOG_PANIC_DIE, "Process %d (parent %d) failed to "
+        "create child process to send failure message: %s", getpid(),
+        getppid(), strerror(errno));
+
+    /* Creation of child succeeded */
+
+    else
+      {
+      int ch, rc;
+      int filecount = 0;
+      int rcount = 0;
+      uschar *bcc, *emf_text;
+      FILE *f = fdopen(fd, "wb");
+      FILE *emf = NULL;
+      BOOL to_sender = strcmpic(sender_address, bounce_recipient) == 0;
+      int max = (bounce_return_size_limit/DELIVER_IN_BUFFER_SIZE + 1) *
+        DELIVER_IN_BUFFER_SIZE;
+#ifdef EXPERIMENTAL_DSN
+      uschar boundaryStr[64];
+      uschar *dsnlimitmsg;
+      uschar *dsnnotifyhdr;
+      int topt;
+#endif
+
+      DEBUG(D_deliver)
+        debug_printf("sending error message to: %s\n", bounce_recipient);
+
+      /* Scan the addresses for all that have the same errors address, removing
+      them from the addr_failed chain, and putting them on msgchain. */
+
+      paddr = &addr_failed;
+      for (addr = addr_failed; addr != NULL; addr = *paddr)
+        {
+        if (Ustrcmp(bounce_recipient, (addr->p.errors_address == NULL)?
+              sender_address : addr->p.errors_address) != 0)
+          {
+          paddr = &(addr->next);      /* Not the same; skip */
+          }
+        else                          /* The same - dechain */
+          {
+          *paddr = addr->next;
+          *pmsgchain = addr;
+          addr->next = NULL;
+          pmsgchain = &(addr->next);
+          }
+        }
+
+      /* Include X-Failed-Recipients: for automatic interpretation, but do
+      not let any one header line get too long. We do this by starting a
+      new header every 50 recipients. Omit any addresses for which the
+      "hide_child" flag is set. */
+
+      for (addr = msgchain; addr != NULL; addr = addr->next)
+        {
+        if (testflag(addr, af_hide_child)) continue;
+        if (rcount >= 50)
+          {
+          fprintf(f, "\n");
+          rcount = 0;
+          }
+        fprintf(f, "%s%s",
+          (rcount++ == 0)? "X-Failed-Recipients: " : ",\n  ",
+          (testflag(addr, af_pfr) && addr->parent != NULL)?
+            string_printing(addr->parent->address) :
+            string_printing(addr->address));
+        }
+      if (rcount > 0) fprintf(f, "\n");
+
+      /* Output the standard headers */
+
+      if (errors_reply_to != NULL)
+        fprintf(f, "Reply-To: %s\n", errors_reply_to);
+      fprintf(f, "Auto-Submitted: auto-replied\n");
+      moan_write_from(f);
+      fprintf(f, "To: %s\n", bounce_recipient);
+
+#ifdef EXPERIMENTAL_DSN
+      /* generate boundary string and output MIME-Headers */
+      snprintf(boundaryStr, sizeof(boundaryStr)-1, TIME_T_FMT "-eximdsn-%d",
+       time(NULL), rand());
+
+      fprintf(f, "Content-Type: multipart/report;"
+           " report-type=delivery-status; boundary=%s\n"
+         "MIME-Version: 1.0\n",
+       boundaryStr);
+#endif
+
+      /* Open a template file if one is provided. Log failure to open, but
+      carry on - default texts will be used. */
+
+      if (bounce_message_file)
+        if (!(emf = Ufopen(bounce_message_file, "rb")))
+          log_write(0, LOG_MAIN|LOG_PANIC, "Failed to open %s for error "
+            "message texts: %s", bounce_message_file, strerror(errno));
+
+      /* Quietly copy to configured additional addresses if required. */
+
+      if ((bcc = moan_check_errorcopy(bounce_recipient)))
+       fprintf(f, "Bcc: %s\n", bcc);
+
+      /* The texts for the message can be read from a template file; if there
+      isn't one, or if it is too short, built-in texts are used. The first
+      emf text is a Subject: and any other headers. */
+
+      if ((emf_text = next_emf(emf, US"header")))
+       fprintf(f, "%s\n", emf_text);
+      else
+        fprintf(f, "Subject: Mail delivery failed%s\n\n",
+          to_sender? ": returning message to sender" : "");
+
+#ifdef EXPERIMENTAL_DSN
+      /* output human readable part as text/plain section */
+      fprintf(f, "--%s\n"
+         "Content-type: text/plain; charset=us-ascii\n\n",
+       boundaryStr);
+#endif
+
+      if ((emf_text = next_emf(emf, US"intro")))
+       fprintf(f, "%s", CS emf_text);
+      else
+        {
+        fprintf(f,
+/* This message has been reworded several times. It seems to be confusing to
+somebody, however it is worded. I have retreated to the original, simple
+wording. */
+"This message was created automatically by mail delivery software.\n");
+
+        if (bounce_message_text)
+         fprintf(f, "%s", CS bounce_message_text);
+        if (to_sender)
+          fprintf(f,
+"\nA message that you sent could not be delivered to one or more of its\n"
+"recipients. This is a permanent error. The following address(es) failed:\n");
+        else
+          fprintf(f,
+"\nA message sent by\n\n  <%s>\n\n"
+"could not be delivered to one or more of its recipients. The following\n"
+"address(es) failed:\n", sender_address);
+        }
+      fputc('\n', f);
+
+      /* Process the addresses, leaving them on the msgchain if they have a
+      file name for a return message. (There has already been a check in
+      post_process_one() for the existence of data in the message file.) A TRUE
+      return from print_address_information() means that the address is not
+      hidden. */
+
+      paddr = &msgchain;
+      for (addr = msgchain; addr != NULL; addr = *paddr)
+        {
+        if (print_address_information(addr, f, US"  ", US"\n    ", US""))
+          print_address_error(addr, f, US"");
+
+        /* End the final line for the address */
+
+        fputc('\n', f);
+
+        /* Leave on msgchain if there's a return file. */
+
+        if (addr->return_file >= 0)
+          {
+          paddr = &(addr->next);
+          filecount++;
+          }
+
+        /* Else save so that we can tick off the recipient when the
+        message is sent. */
+
+        else
+          {
+          *paddr = addr->next;
+          addr->next = handled_addr;
+          handled_addr = addr;
+          }
+        }
+
+      fputc('\n', f);
+
+      /* Get the next text, whether we need it or not, so as to be
+      positioned for the one after. */
+
+      emf_text = next_emf(emf, US"generated text");
+
+      /* If there were any file messages passed by the local transports,
+      include them in the message. Then put the address on the handled chain.
+      In the case of a batch of addresses that were all sent to the same
+      transport, the return_file field in all of them will contain the same
+      fd, and the return_filename field in the *last* one will be set (to the
+      name of the file). */
+
+      if (msgchain)
+        {
+        address_item *nextaddr;
+
+        if (emf_text)
+         fprintf(f, "%s", CS emf_text);
+       else
+          fprintf(f,
+            "The following text was generated during the delivery "
+            "attempt%s:\n", (filecount > 1)? "s" : "");
+
+        for (addr = msgchain; addr != NULL; addr = nextaddr)
+          {
+          FILE *fm;
+          address_item *topaddr = addr;
+
+          /* List all the addresses that relate to this file */
+
+         fputc('\n', f);
+          while(addr)                   /* Insurance */
+            {
+            print_address_information(addr, f, US"------ ",  US"\n       ",
+              US" ------\n");
+            if (addr->return_filename) break;
+            addr = addr->next;
+            }
+         fputc('\n', f);
+
+          /* Now copy the file */
+
+          fm = Ufopen(addr->return_filename, "rb");
+
+          if (fm == NULL)
+            fprintf(f, "    +++ Exim error... failed to open text file: %s\n",
+              strerror(errno));
+          else
+            {
+            while ((ch = fgetc(fm)) != EOF) fputc(ch, f);
+            (void)fclose(fm);
+            }
+          Uunlink(addr->return_filename);
+
+          /* Can now add to handled chain, first fishing off the next
+          address on the msgchain. */
+
+          nextaddr = addr->next;
+          addr->next = handled_addr;
+          handled_addr = topaddr;
+          }
+       fputc('\n', f);
+        }
+
+#ifdef EXPERIMENTAL_DSN
+      /* output machine readable part */
+      fprintf(f, "--%s\n"
+         "Content-type: message/delivery-status\n\n"
+         "Reporting-MTA: dns; %s\n",
+       boundaryStr, smtp_active_hostname);
+
+      if (dsn_envid)
+       {
+        /* must be decoded from xtext: see RFC 3461:6.3a */
+        uschar *xdec_envid;
+        if (auth_xtextdecode(dsn_envid, &xdec_envid) > 0)
+          fprintf(f, "Original-Envelope-ID: %s\n", dsn_envid);
+        else
+          fprintf(f, "X-Original-Envelope-ID: error decoding xtext formated ENVID\n");
+        }
+      fputc('\n', f);
+      for (addr = handled_addr; addr; addr = addr->next)
+        {
+        fprintf(f, "Action: failed\n"
+           "Final-Recipient: rfc822;%s\n"
+           "Status: 5.0.0\n",
+           addr->address);
+        if (addr->host_used && addr->host_used->name)
+          fprintf(f, "Remote-MTA: dns; %s\nDiagnostic-Code: smtp; %d\n",
+           addr->host_used->name, addr->basic_errno);
+        }
+#endif
+
+      /* Now copy the message, trying to give an intelligible comment if
+      it is too long for it all to be copied. The limit isn't strictly
+      applied because of the buffering. There is, however, an option
+      to suppress copying altogether. */
+
+      emf_text = next_emf(emf, US"copy");
+
+#ifndef EXPERIMENTAL_DSN
+      if (bounce_return_message)
+        {
+        int topt = topt_add_return_path;
+        if (!bounce_return_body) topt |= topt_no_body;
+
+        if (emf_text)
+         fprintf(f, "%s", CS emf_text);
+       else
+          {
+          if (bounce_return_body) fprintf(f,
+"------ This is a copy of the message, including all the headers. ------\n");
+          else fprintf(f,
+"------ This is a copy of the message's headers. ------\n");
+          }
+
+        /* While reading the "truncated" message, set return_size_limit to
+        the actual max testing value, rounded. We need to read the message
+        whether we are going to use it or not. */
+
+          {
+          int temp = bounce_return_size_limit;
+          bounce_return_size_limit = (max/1000)*1000;
+          emf_text = next_emf(emf, US"truncated");
+          bounce_return_size_limit = temp;
+          }
+
+        if (bounce_return_body && bounce_return_size_limit > 0)
+          {
+          struct stat statbuf;
+          if (fstat(deliver_datafile, &statbuf) == 0 && statbuf.st_size > max)
+            if (emf_text)
+             fprintf(f, "%s", CS emf_text);
+           else
+              fprintf(f,
+"------ The body of the message is " OFF_T_FMT " characters long; only the first\n"
+"------ %d or so are included here.\n", statbuf.st_size, max);
+          }
+
+       fputc('\n', f);
+        fflush(f);
+
+        transport_filter_argv = NULL;   /* Just in case */
+        return_path = sender_address;   /* In case not previously set */
+        transport_write_message(NULL, fileno(f), topt,
+          bounce_return_size_limit, NULL, NULL, NULL, NULL, NULL, 0);
+        }
+
+      /* Write final text and close the template file if one is open */
+
+      if (emf)
+        {
+        if ((emf_text = next_emf(emf, US"final")))
+         fprintf(f, "%s", CS emf_text);
+        (void)fclose(emf);
+        }
+#else
+      /* add message body
+         we ignore the intro text from template and add 
+         the text for bounce_return_size_limit at the end.
+  
+         bounce_return_message is ignored
+         in case RET= is defined we honor these values
+         otherwise bounce_return_body is honored.
+         
+         bounce_return_size_limit is always honored.
+      */
+  
+      fprintf(f, "\n--%s\n", boundaryStr);
+
+      dsnlimitmsg = US"X-Exim-DSN-Information: Due to administrative limits only headers are returned";
+      dsnnotifyhdr = NULL;
+      topt = topt_add_return_path;
+
+      /* RET=HDRS? top priority */
+      if (dsn_ret == dsn_ret_hdrs)
+        topt |= topt_no_body;
+      else
+        /* no full body return at all? */
+        if (!bounce_return_body)
+          {
+          topt |= topt_no_body;
+          /* add header if we overrule RET=FULL */
+          if (dsn_ret == dsn_ret_full)
+            dsnnotifyhdr = dsnlimitmsg;
+          }
+        /* size limited ... return headers only if limit reached */
+        else if (bounce_return_size_limit > 0)
+          {
+          struct stat statbuf;
+          if (fstat(deliver_datafile, &statbuf) == 0 && statbuf.st_size > max)
+            {
+              topt |= topt_no_body;
+              dsnnotifyhdr = dsnlimitmsg;
+            }
+          }
+  
+      if (topt & topt_no_body)
+        fprintf(f,"Content-type: text/rfc822-headers\n\n");
+      else
+        fprintf(f,"Content-type: message/rfc822\n\n");
+
+      fflush(f);
+      transport_filter_argv = NULL;   /* Just in case */
+      return_path = sender_address;   /* In case not previously set */
+      transport_write_message(NULL, fileno(f), topt,
+        0, dsnnotifyhdr, NULL, NULL, NULL, NULL, 0);
+      fflush(f);
+      /* we never add the final text. close the file */
+      if (emf)
+        (void)fclose(emf);
+      fprintf(f, "\n--%s--\n", boundaryStr);
+#endif /*EXPERIMENTAL_DSN*/
+
+      /* Close the file, which should send an EOF to the child process
+      that is receiving the message. Wait for it to finish. */
+
+      (void)fclose(f);
+      rc = child_close(pid, 0);     /* Waits for child to close, no timeout */
+
+      /* In the test harness, let the child do it's thing first. */
+
+      if (running_in_test_harness) millisleep(500);
+
+      /* If the process failed, there was some disaster in setting up the
+      error message. Unless the message is very old, ensure that addr_defer
+      is non-null, which will have the effect of leaving the message on the
+      spool. The failed addresses will get tried again next time. However, we
+      don't really want this to happen too often, so freeze the message unless
+      there are some genuine deferred addresses to try. To do this we have
+      to call spool_write_header() here, because with no genuine deferred
+      addresses the normal code below doesn't get run. */
+
+      if (rc != 0)
+        {
+        uschar *s = US"";
+        if (now - received_time < retry_maximum_timeout && addr_defer == NULL)
+          {
+          addr_defer = (address_item *)(+1);
+          deliver_freeze = TRUE;
+          deliver_frozen_at = time(NULL);
+          /* Panic-dies on error */
+          (void)spool_write_header(message_id, SW_DELIVERING, NULL);
+          s = US" (frozen)";
+          }
+        deliver_msglog("Process failed (%d) when writing error message "
+          "to %s%s", rc, bounce_recipient, s);
+        log_write(0, LOG_MAIN, "Process failed (%d) when writing error message "
+          "to %s%s", rc, bounce_recipient, s);
+        }
+
+      /* The message succeeded. Ensure that the recipients that failed are
+      now marked finished with on the spool and their parents updated. */
+
+      else
+        {
+        for (addr = handled_addr; addr != NULL; addr = addr->next)
+          {
+          address_done(addr, logtod);
+          child_done(addr, logtod);
+          }
+        /* Panic-dies on error */
+        (void)spool_write_header(message_id, SW_DELIVERING, NULL);
+        }
+      }
+    }
+  }
+
+disable_logging = FALSE;  /* In case left set */
+
+/* Come here from the mua_wrapper case if routing goes wrong */
+
+DELIVERY_TIDYUP:
+
+/* If there are now no deferred addresses, we are done. Preserve the
+message log if so configured, and we are using them. Otherwise, sling it.
+Then delete the message itself. */
+
+if (addr_defer == NULL)
+  {
+  if (message_logs)
+    {
+    sprintf(CS spoolname, "%s/msglog/%s/%s", spool_directory, message_subdir,
+      id);
+    if (preserve_message_logs)
+      {
+      int rc;
+      sprintf(CS big_buffer, "%s/msglog.OLD/%s", spool_directory, id);
+      if ((rc = Urename(spoolname, big_buffer)) < 0)
+        {
+        (void)directory_make(spool_directory, US"msglog.OLD",
+          MSGLOG_DIRECTORY_MODE, TRUE);
+        rc = Urename(spoolname, big_buffer);
+        }
+      if (rc < 0)
+        log_write(0, LOG_MAIN|LOG_PANIC_DIE, "failed to move %s to the "
+          "msglog.OLD directory", spoolname);
+      }
+    else
+      {
+      if (Uunlink(spoolname) < 0)
+        log_write(0, LOG_MAIN|LOG_PANIC_DIE, "failed to unlink %s: %s",
+                 spoolname, strerror(errno));
+      }
+    }
+
+  /* Remove the two message files. */
+
+  sprintf(CS spoolname, "%s/input/%s/%s-D", spool_directory, message_subdir, id);
+  if (Uunlink(spoolname) < 0)
+    log_write(0, LOG_MAIN|LOG_PANIC_DIE, "failed to unlink %s: %s",
+      spoolname, strerror(errno));
+  sprintf(CS spoolname, "%s/input/%s/%s-H", spool_directory, message_subdir, id);
+  if (Uunlink(spoolname) < 0)
+    log_write(0, LOG_MAIN|LOG_PANIC_DIE, "failed to unlink %s: %s",
+      spoolname, strerror(errno));
+
+  /* Log the end of this message, with queue time if requested. */
+
+  if ((log_extra_selector & LX_queue_time_overall) != 0)
+    log_write(0, LOG_MAIN, "Completed QT=%s",
+      readconf_printtime( (int) ((long)time(NULL) - (long)received_time)) );
+  else
+    log_write(0, LOG_MAIN, "Completed");
+
+  /* Unset deliver_freeze so that we won't try to move the spool files further down */
+  deliver_freeze = FALSE;
+  }
+
+/* If there are deferred addresses, we are keeping this message because it is
+not yet completed. Lose any temporary files that were catching output from
+pipes for any of the deferred addresses, handle one-time aliases, and see if
+the message has been on the queue for so long that it is time to send a warning
+message to the sender, unless it is a mailer-daemon. If all deferred addresses
+have the same domain, we can set deliver_domain for the expansion of
+delay_warning_ condition - if any of them are pipes, files, or autoreplies, use
+the parent's domain.
+
+If all the deferred addresses have an error number that indicates "retry time
+not reached", skip sending the warning message, because it won't contain the
+reason for the delay. It will get sent at the next real delivery attempt.
+However, if at least one address has tried, we'd better include all of them in
+the message.
+
+If we can't make a process to send the message, don't worry.
+
+For mailing list expansions we want to send the warning message to the
+mailing list manager. We can't do a perfect job here, as some addresses may
+have different errors addresses, but if we take the errors address from
+each deferred address it will probably be right in most cases.
+
+If addr_defer == +1, it means there was a problem sending an error message
+for failed addresses, and there were no "real" deferred addresses. The value
+was set just to keep the message on the spool, so there is nothing to do here.
+*/
+
+else if (addr_defer != (address_item *)(+1))
+  {
+  address_item *addr;
+  uschar *recipients = US"";
+  BOOL delivery_attempted = FALSE;
+
+  deliver_domain = testflag(addr_defer, af_pfr)?
+    addr_defer->parent->domain : addr_defer->domain;
+
+  for (addr = addr_defer; addr != NULL; addr = addr->next)
+    {
+    address_item *otaddr;
+
+    if (addr->basic_errno > ERRNO_RETRY_BASE) delivery_attempted = TRUE;
+
+    if (deliver_domain != NULL)
+      {
+      uschar *d = (testflag(addr, af_pfr))? addr->parent->domain : addr->domain;
+
+      /* The domain may be unset for an address that has never been routed
+      because the system filter froze the message. */
+
+      if (d == NULL || Ustrcmp(d, deliver_domain) != 0) deliver_domain = NULL;
+      }
+
+    if (addr->return_filename != NULL) Uunlink(addr->return_filename);
+
+    /* Handle the case of one-time aliases. If any address in the ancestry
+    of this one is flagged, ensure it is in the recipients list, suitably
+    flagged, and that its parent is marked delivered. */
+
+    for (otaddr = addr; otaddr != NULL; otaddr = otaddr->parent)
+      if (otaddr->onetime_parent != NULL) break;
+
+    if (otaddr != NULL)
+      {
+      int i;
+      int t = recipients_count;
+
+      for (i = 0; i < recipients_count; i++)
+        {
+        uschar *r = recipients_list[i].address;
+        if (Ustrcmp(otaddr->onetime_parent, r) == 0) t = i;
+        if (Ustrcmp(otaddr->address, r) == 0) break;
+        }
+
+      /* Didn't find the address already in the list, and did find the
+      ultimate parent's address in the list. After adding the recipient,
+      update the errors address in the recipients list. */
+
+      if (i >= recipients_count && t < recipients_count)
+        {
+        DEBUG(D_deliver) debug_printf("one_time: adding %s in place of %s\n",
+          otaddr->address, otaddr->parent->address);
+        receive_add_recipient(otaddr->address, t);
+        recipients_list[recipients_count-1].errors_to = otaddr->p.errors_address;
+        tree_add_nonrecipient(otaddr->parent->address);
+        update_spool = TRUE;
+        }
+      }
+
+    /* Except for error messages, ensure that either the errors address for
+    this deferred address or, if there is none, the sender address, is on the
+    list of recipients for a warning message. */
+
+    if (sender_address[0] != 0)
+      {
+      if (addr->p.errors_address == NULL)
+        {
+        if (Ustrstr(recipients, sender_address) == NULL)
+          recipients = string_sprintf("%s%s%s", recipients,
+            (recipients[0] == 0)? "" : ",", sender_address);
+        }
+      else
+        {
+        if (Ustrstr(recipients, addr->p.errors_address) == NULL)
+          recipients = string_sprintf("%s%s%s", recipients,
+            (recipients[0] == 0)? "" : ",", addr->p.errors_address);
+        }
+      }
+    }
+
+  /* Send a warning message if the conditions are right. If the condition check
+  fails because of a lookup defer, there is nothing we can do. The warning
+  is not sent. Another attempt will be made at the next delivery attempt (if
+  it also defers). */
+
+  if (!queue_2stage && delivery_attempted &&
+#ifdef EXPERIMENTAL_DSN
+      (((addr_defer->dsn_flags & rf_dsnflags) == 0) ||
+       (addr_defer->dsn_flags & rf_notify_delay) == rf_notify_delay) &&
+#endif
+      delay_warning[1] > 0 && sender_address[0] != 0 &&
+       (delay_warning_condition == NULL ||
+          expand_check_condition(delay_warning_condition,
+            US"delay_warning", US"option")))
+    {
+    int count;
+    int show_time;
+    int queue_time = time(NULL) - received_time;
+
+    /* When running in the test harness, there's an option that allows us to
+    fudge this time so as to get repeatability of the tests. Take the first
+    time off the list. In queue runs, the list pointer gets updated in the
+    calling process. */
+
+    if (running_in_test_harness && fudged_queue_times[0] != 0)
+      {
+      int qt = readconf_readtime(fudged_queue_times, '/', FALSE);
+      if (qt >= 0)
+        {
+        DEBUG(D_deliver) debug_printf("fudged queue_times = %s\n",
+          fudged_queue_times);
+        queue_time = qt;
+        }
+      }
+
+    /* See how many warnings we should have sent by now */
+
+    for (count = 0; count < delay_warning[1]; count++)
+      if (queue_time < delay_warning[count+2]) break;
+
+    show_time = delay_warning[count+1];
+
+    if (count >= delay_warning[1])
+      {
+      int extra;
+      int last_gap = show_time;
+      if (count > 1) last_gap -= delay_warning[count];
+      extra = (queue_time - delay_warning[count+1])/last_gap;
+      show_time += last_gap * extra;
+      count += extra;
+      }
+
+    DEBUG(D_deliver)
+      {
+      debug_printf("time on queue = %s\n", readconf_printtime(queue_time));
+      debug_printf("warning counts: required %d done %d\n", count,
+        warning_count);
+      }
+
+    /* We have computed the number of warnings there should have been by now.
+    If there haven't been enough, send one, and up the count to what it should
+    have been. */
+
+    if (warning_count < count)
+      {
+      header_line *h;
+      int fd;
+      pid_t pid = child_open_exim(&fd);
+
+      if (pid > 0)
+        {
+        uschar *wmf_text;
+        FILE *wmf = NULL;
+        FILE *f = fdopen(fd, "wb");
+#ifdef EXPERIMENTAL_DSN
+       uschar boundaryStr[64];
+#endif
+
+        if (warn_message_file)
+          {
+          wmf = Ufopen(warn_message_file, "rb");
+          if (wmf == NULL)
+            log_write(0, LOG_MAIN|LOG_PANIC, "Failed to open %s for warning "
+              "message texts: %s", warn_message_file, strerror(errno));
+          }
+
+        warnmsg_recipients = recipients;
+        warnmsg_delay = (queue_time < 120*60)?
+          string_sprintf("%d minutes", show_time/60):
+          string_sprintf("%d hours", show_time/3600);
+
+        if (errors_reply_to)
+          fprintf(f, "Reply-To: %s\n", errors_reply_to);
+        fprintf(f, "Auto-Submitted: auto-replied\n");
+        moan_write_from(f);
+        fprintf(f, "To: %s\n", recipients);
+
+#ifdef EXPERIMENTAL_DSN
+        /* generated boundary string and output MIME-Headers */
+        snprintf(boundaryStr, sizeof(boundaryStr)-1,
+         TIME_T_FMT "-eximdsn-%d", time(NULL), rand());
+
+        fprintf(f, "Content-Type: multipart/report;"
+           " report-type=delivery-status; boundary=%s\n"
+           "MIME-Version: 1.0\n",
+         boundaryStr);
+#endif
+
+        if ((wmf_text = next_emf(wmf, US"header")))
+          fprintf(f, "%s\n", wmf_text);
+        else
+          fprintf(f, "Subject: Warning: message %s delayed %s\n\n",
+            message_id, warnmsg_delay);
+
+#ifdef EXPERIMENTAL_DSN
+        /* output human readable part as text/plain section */
+        fprintf(f, "--%s\n"
+           "Content-type: text/plain; charset=us-ascii\n\n",
+         boundaryStr);
+#endif
+
+        if ((wmf_text = next_emf(wmf, US"intro")))
+         fprintf(f, "%s", CS wmf_text);
+       else
+          {
+          fprintf(f,
+"This message was created automatically by mail delivery software.\n");
+
+          if (Ustrcmp(recipients, sender_address) == 0)
+            fprintf(f,
+"A message that you sent has not yet been delivered to one or more of its\n"
+"recipients after more than ");
+
+          else
+           fprintf(f,
+"A message sent by\n\n  <%s>\n\n"
+"has not yet been delivered to one or more of its recipients after more than \n",
+             sender_address);
+
+          fprintf(f, "%s on the queue on %s.\n\n"
+             "The message identifier is:     %s\n",
+           warnmsg_delay, primary_hostname, message_id);
+
+          for (h = header_list; h != NULL; h = h->next)
+            if (strncmpic(h->text, US"Subject:", 8) == 0)
+              fprintf(f, "The subject of the message is: %s", h->text + 9);
+            else if (strncmpic(h->text, US"Date:", 5) == 0)
+              fprintf(f, "The date of the message is:    %s", h->text + 6);
+          fputc('\n', f);
+
+          fprintf(f, "The address%s to which the message has not yet been "
+            "delivered %s:\n",
+            !addr_defer->next ? "" : "es",
+            !addr_defer->next ? "is": "are");
+          }
+
+        /* List the addresses, with error information if allowed */
+
+#ifdef EXPERIMENTAL_DSN
+        /* store addr_defer for machine readable part */
+        address_item *addr_dsndefer = addr_defer;
+#endif
+        fputc('\n', f);
+        while (addr_defer)
+          {
+          address_item *addr = addr_defer;
+          addr_defer = addr->next;
+          if (print_address_information(addr, f, US"  ", US"\n    ", US""))
+            print_address_error(addr, f, US"Delay reason: ");
+          fputc('\n', f);
+          }
+        fputc('\n', f);
+
+        /* Final text */
+
+        if (wmf)
+          {
+          if ((wmf_text = next_emf(wmf, US"final")))
+           fprintf(f, "%s", CS wmf_text);
+          (void)fclose(wmf);
+          }
+        else
+          {
+          fprintf(f,
+"No action is required on your part. Delivery attempts will continue for\n"
+"some time, and this warning may be repeated at intervals if the message\n"
+"remains undelivered. Eventually the mail delivery software will give up,\n"
+"and when that happens, the message will be returned to you.\n");
+          }
+
+#ifdef EXPERIMENTAL_DSN
+        /* output machine readable part */
+        fprintf(f, "\n--%s\n"
+           "Content-type: message/delivery-status\n\n"
+           "Reporting-MTA: dns; %s\n",
+         boundaryStr,
+         smtp_active_hostname);
+
+        if (dsn_envid)
+         {
+          /* must be decoded from xtext: see RFC 3461:6.3a */
+          uschar *xdec_envid;
+          if (auth_xtextdecode(dsn_envid, &xdec_envid) > 0)
+            fprintf(f,"Original-Envelope-ID: %s\n", dsn_envid);
+          else
+            fprintf(f,"X-Original-Envelope-ID: error decoding xtext formated ENVID\n");
+          }
+        fputc('\n', f);
+
+        while (addr_dsndefer)
+          {
+          if (addr_dsndefer->dsn_orcpt)
+            fprintf(f,"Original-Recipient: %s\n", addr_dsndefer->dsn_orcpt);
+
+          fprintf(f,"Action: delayed\n");
+          fprintf(f,"Final-Recipient: rfc822;%s\n", addr_dsndefer->address);
+          fprintf(f,"Status: 4.0.0\n");
+          if (addr_dsndefer->host_used && addr_dsndefer->host_used->name)
+            fprintf(f,"Remote-MTA: dns; %s\nDiagnostic-Code: smtp; %d\n", 
+                   addr_dsndefer->host_used->name, addr_dsndefer->basic_errno);
+          addr_dsndefer = addr_dsndefer->next;
+          }
+
+        fprintf(f, "\n--%s\n"
+           "Content-type: text/rfc822-headers\n\n",
+         boundaryStr);
+
+        fflush(f);
+        /* header only as required by RFC. only failure DSN needs to honor RET=FULL */
+        int topt = topt_add_return_path | topt_no_body;
+        transport_filter_argv = NULL;   /* Just in case */
+        return_path = sender_address;   /* In case not previously set */
+        /* Write the original email out */
+        transport_write_message(NULL, fileno(f), topt, 0, NULL, NULL, NULL, NULL, NULL, 0);
+        fflush(f);
+
+        fprintf(f,"\n--%s--\n", boundaryStr);
+
+        fflush(f);
+#endif /*EXPERIMENTAL_DSN*/
+
+        /* Close and wait for child process to complete, without a timeout.
+        If there's an error, don't update the count. */
+
+        (void)fclose(f);
+        if (child_close(pid, 0) == 0)
+          {
+          warning_count = count;
+          update_spool = TRUE;    /* Ensure spool rewritten */
+          }
+        }
+      }
+    }
+
+  /* Clear deliver_domain */
+
+  deliver_domain = NULL;
+
+  /* If this was a first delivery attempt, unset the first time flag, and
+  ensure that the spool gets updated. */
+
+  if (deliver_firsttime)
+    {
+    deliver_firsttime = FALSE;
+    update_spool = TRUE;
+    }
+
+  /* If delivery was frozen and freeze_tell is set, generate an appropriate
+  message, unless the message is a local error message (to avoid loops). Then
+  log the freezing. If the text in "frozen_info" came from a system filter,
+  it has been escaped into printing characters so as not to mess up log lines.
+  For the "tell" message, we turn \n back into newline. Also, insert a newline
+  near the start instead of the ": " string. */
+
+  if (deliver_freeze)
+    {
+    if (freeze_tell != NULL && freeze_tell[0] != 0 && !local_error_message)
+      {
+      uschar *s = string_copy(frozen_info);
+      uschar *ss = Ustrstr(s, " by the system filter: ");
+
+      if (ss != NULL)
+        {
+        ss[21] = '.';
+        ss[22] = '\n';
+        }
+
+      ss = s;
+      while (*ss != 0)
+        {
+        if (*ss == '\\' && ss[1] == 'n')
+          {
+          *ss++ = ' ';
+          *ss++ = '\n';
+          }
+        else ss++;
+        }
+      moan_tell_someone(freeze_tell, addr_defer, US"Message frozen",
+        "Message %s has been frozen%s.\nThe sender is <%s>.\n", message_id,
+        s, sender_address);
+      }
+
+    /* Log freezing just before we update the -H file, to minimize the chance
+    of a race problem. */
+
+    deliver_msglog("*** Frozen%s\n", frozen_info);
+    log_write(0, LOG_MAIN, "Frozen%s", frozen_info);
+    }
+
+  /* If there have been any updates to the non-recipients list, or other things
+  that get written to the spool, we must now update the spool header file so
+  that it has the right information for the next delivery attempt. If there
+  was more than one address being delivered, the header_change update is done
+  earlier, in case one succeeds and then something crashes. */
+
+  DEBUG(D_deliver)
+    debug_printf("delivery deferred: update_spool=%d header_rewritten=%d\n",
+      update_spool, header_rewritten);
+
+  if (update_spool || header_rewritten)
+    /* Panic-dies on error */
+    (void)spool_write_header(message_id, SW_DELIVERING, NULL);
+  }
+
+/* Finished with the message log. If the message is complete, it will have
+been unlinked or renamed above. */
+
+if (message_logs) (void)fclose(message_log);
+
+/* Now we can close and remove the journal file. Its only purpose is to record
+successfully completed deliveries asap so that this information doesn't get
+lost if Exim (or the machine) crashes. Forgetting about a failed delivery is
+not serious, as trying it again is not harmful. The journal might not be open
+if all addresses were deferred at routing or directing. Nevertheless, we must
+remove it if it exists (may have been lying around from a crash during the
+previous delivery attempt). We don't remove the journal if a delivery
+subprocess failed to pass back delivery information; this is controlled by
+the remove_journal flag. When the journal is left, we also don't move the
+message off the main spool if frozen and the option is set. It should get moved
+at the next attempt, after the journal has been inspected. */
+
+if (journal_fd >= 0) (void)close(journal_fd);
+
+if (remove_journal)
+  {
+  sprintf(CS spoolname, "%s/input/%s/%s-J", spool_directory, message_subdir, id);
+  if (Uunlink(spoolname) < 0 && errno != ENOENT)
+    log_write(0, LOG_MAIN|LOG_PANIC_DIE, "failed to unlink %s: %s", spoolname,
+      strerror(errno));
+
+  /* Move the message off the spool if reqested */
+
+  #ifdef SUPPORT_MOVE_FROZEN_MESSAGES
+  if (deliver_freeze && move_frozen_messages)
+    (void)spool_move_message(id, message_subdir, US"", US"F");
+  #endif
+  }
+
+/* Closing the data file frees the lock; if the file has been unlinked it
+will go away. Otherwise the message becomes available for another process
+to try delivery. */
+
+(void)close(deliver_datafile);
+deliver_datafile = -1;
+DEBUG(D_deliver) debug_printf("end delivery of %s\n", id);
+
+/* It is unlikely that there will be any cached resources, since they are
+released after routing, and in the delivery subprocesses. However, it's
+possible for an expansion for something afterwards (for example,
+expand_check_condition) to do a lookup. We must therefore be sure everything is
+released. */
+
+search_tidyup();
+acl_where = ACL_WHERE_UNKNOWN;
+return final_yield;
+}
+
+/* vi: aw ai sw=2
+*/
+/* End of deliver.c */
diff --git a/src/demime.c b/src/demime.c
new file mode 100644 (file)
index 0000000..887678d
--- /dev/null
@@ -0,0 +1,1243 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) Tom Kistner <tom@duncanthrax.net> 2003-???? */
+/* License: GPL */
+
+/* Code for unpacking MIME containers. Called from acl.c. */
+
+#include "exim.h"
+#ifdef WITH_OLD_DEMIME
+
+#include "demime.h"
+
+uschar demime_reason_buffer[1024];
+struct file_extension *file_extensions = NULL;
+
+int demime(uschar **listptr) {
+  int sep = 0;
+  uschar *list = *listptr;
+  uschar *option;
+  uschar option_buffer[64];
+  unsigned long mbox_size;
+  FILE *mbox_file;
+  uschar defer_error_buffer[1024];
+  int demime_rc = 0;
+
+  /* reset found_extension variable */
+  found_extension = NULL;
+
+  /* try to find 1st option */
+  if ((option = string_nextinlist(&list, &sep,
+                                  option_buffer,
+                                  sizeof(option_buffer))) != NULL) {
+
+    /* parse 1st option */
+    if ( (Ustrcmp(option,"false") == 0) || (Ustrcmp(option,"0") == 0) ) {
+      /* explicitly no demimeing */
+      return FAIL;
+    };
+  }
+  else {
+    /* no options -> no demimeing */
+    return FAIL;
+  };
+
+  /* make sure the eml mbox file is spooled up */
+  mbox_file = spool_mbox(&mbox_size, NULL);
+
+  if (mbox_file == NULL) {
+    /* error while spooling */
+    log_write(0, LOG_MAIN|LOG_PANIC,
+           "demime acl condition: error while creating mbox spool file");
+    return DEFER;
+  };
+
+  /* call demimer if not already done earlier */
+  if (!demime_ok)
+    demime_rc = mime_demux(mbox_file, defer_error_buffer);
+
+  (void)fclose(mbox_file);
+
+  if (demime_rc == DEFER) {
+    /* temporary failure (DEFER => DEFER) */
+    log_write(0, LOG_MAIN,
+        "demime acl condition: %s", defer_error_buffer);
+    return DEFER;
+  };
+
+  /* set demime_ok to avoid unpacking again */
+  demime_ok = 1;
+
+  /* check for file extensions, if there */
+  while (option != NULL) {
+    struct file_extension *this_extension = file_extensions;
+
+    /* Look for the wildcard. If it is found, we always return true.
+    The user must then use a custom condition to evaluate demime_errorlevel */
+    if (Ustrcmp(option,"*") == 0) {
+      found_extension = NULL;
+      return OK;
+    };
+
+    /* loop thru extension list */
+    while (this_extension != NULL) {
+      if (strcmpic(option, this_extension->file_extension_string) == 0) {
+        /* found one */
+        found_extension = this_extension->file_extension_string;
+        return OK;
+      };
+      this_extension = this_extension->next;
+    };
+
+    /* grab next extension from option list */
+    option = string_nextinlist(&list, &sep,
+                               option_buffer,
+                               sizeof(option_buffer));
+  };
+
+  /* nothing found */
+  return FAIL;
+}
+
+
+/*************************************************
+* small hex_str -> integer conversion function   *
+*************************************************/
+
+/* needed for quoted-printable
+*/
+
+unsigned int mime_hstr_i(uschar *cptr) {
+  unsigned int i, j = 0;
+
+  while (cptr && *cptr && isxdigit(*cptr)) {
+    i = *cptr++ - '0';
+    if (9 < i) i -= 7;
+    j <<= 4;
+    j |= (i & 0x0f);
+  }
+
+  return(j);
+}
+
+
+/*************************************************
+* decode quoted-printable chars                  *
+*************************************************/
+
+/* gets called when we hit a =
+   returns: new pointer position
+   result code in c:
+          -2 - decode error
+          -1 - soft line break, no char
+           0-255 - char to write
+*/
+
+uschar *mime_decode_qp(uschar *qp_p,int *c) {
+  uschar hex[] = {0,0,0};
+  int nan = 0;
+  uschar *initial_pos = qp_p;
+
+  /* advance one char */
+  qp_p++;
+
+  REPEAT_FIRST:
+  if ( (*qp_p == '\t') || (*qp_p == ' ') || (*qp_p == '\r') )  {
+    /* tab or whitespace may follow
+       just ignore it, but remember
+       that this is not a valid hex
+       encoding any more */
+    nan = 1;
+    qp_p++;
+    goto REPEAT_FIRST;
+  }
+  else if ( (('0' <= *qp_p) && (*qp_p <= '9')) || (('A' <= *qp_p) && (*qp_p <= 'F'))  || (('a' <= *qp_p) && (*qp_p <= 'f')) ) {
+    /* this is a valid hex char, if nan is unset */
+    if (nan) {
+      /* this is illegal */
+      *c = -2;
+      return initial_pos;
+    }
+    else {
+      hex[0] = *qp_p;
+      qp_p++;
+    };
+  }
+  else if (*qp_p == '\n') {
+    /* hit soft line break already, continue */
+    *c = -1;
+    return qp_p;
+  }
+  else {
+    /* illegal char here */
+    *c = -2;
+    return initial_pos;
+  };
+
+  if ( (('0' <= *qp_p) && (*qp_p <= '9')) || (('A' <= *qp_p) && (*qp_p <= 'F')) || (('a' <= *qp_p) && (*qp_p <= 'f')) ) {
+    if (hex[0] > 0) {
+      hex[1] = *qp_p;
+      /* do hex conversion */
+      *c = mime_hstr_i(hex);
+      qp_p++;
+      return qp_p;
+    }
+    else {
+      /* huh ? */
+      *c = -2;
+      return initial_pos;
+    };
+  }
+  else {
+    /* illegal char */
+    *c = -2;
+    return initial_pos;
+  };
+
+}
+
+
+/*************************************************
+* open new dump file                             *
+*************************************************/
+
+/* open new dump file
+   returns: -2 soft error
+            or file #, FILE * in f
+*/
+
+int mime_get_dump_file(uschar *extension, FILE **f, uschar *info) {
+  uschar file_name[1024];
+  int result;
+  unsigned int file_nr;
+  uschar default_extension[] = ".com";
+  uschar *p;
+
+  if (extension == NULL)
+    extension = default_extension;
+
+  /* scan the proposed extension.
+     if it is longer than 4 chars, or
+     contains exotic chars, use the default extension */
+
+/*  if (Ustrlen(extension) > 4) {
+    extension = default_extension;
+  };
+*/
+
+  p = extension+1;
+
+  while (*p != 0) {
+    *p = (uschar)tolower((uschar)*p);
+    if ( (*p < 97) || (*p > 122) ) {
+      extension = default_extension;
+      break;
+    };
+    p++;
+  };
+
+  /* find a new file to write to */
+  file_nr = 0;
+  do {
+    struct stat mystat;
+
+    (void)string_format(file_name,1024,"%s/scan/%s/%s-%05u%s",spool_directory,message_id,message_id,file_nr,extension);
+    file_nr++;
+    if (file_nr >= MIME_SANITY_MAX_DUMP_FILES) {
+      /* max parts reached */
+      mime_trigger_error(MIME_ERRORLEVEL_TOO_MANY_PARTS);
+      break;
+    };
+    result = stat(CS file_name,&mystat);
+  }
+  while(result != -1);
+
+  *f = modefopen(file_name,"wb+",SPOOL_MODE);
+  if (*f == NULL) {
+    /* cannot open new dump file, disk full ? -> soft error */
+    (void)string_format(info, 1024,"unable to open dump file");
+    return -2;
+  };
+
+  return file_nr;
+}
+
+
+/*************************************************
+* Find a string in a mime header                 *
+*************************************************/
+
+/* Find a string in a mime header, and optionally fill in
+   the value associated with it into *value
+
+   returns: 0 - nothing found
+            1 - found param
+            2 - found param + value
+*/
+
+int mime_header_find(uschar *header, uschar *param, uschar **value) {
+  uschar *needle;
+
+  needle = strstric(header,param,FALSE);
+  if (needle != NULL) {
+    if (value != NULL) {
+      needle += Ustrlen(param);
+      if (*needle == '=') {
+        uschar *value_start;
+        uschar *value_end;
+
+        value_start = needle + 1;
+        value_end = strstric(value_start,US";",FALSE);
+        if (value_end != NULL) {
+          /* allocate mem for value */
+          *value = (uschar *)malloc((value_end - value_start)+1);
+          if (*value == NULL)
+            return 0;
+
+          Ustrncpy(*value,value_start,(value_end - value_start));
+          (*value)[(value_end - value_start)] = '\0';
+          return 2;
+        };
+      };
+    };
+    return 1;
+  };
+  return 0;
+}
+
+
+/*************************************************
+* Read a line of MIME input                      *
+*************************************************/
+/* returns status code, one of
+   MIME_READ_LINE_EOF 0
+   MIME_READ_LINE_OK 1
+   MIME_READ_LINE_OVERFLOW 2
+
+   In header mode, the line will be "cooked".
+*/
+
+int mime_read_line(FILE *f, int mime_demux_mode, uschar *buffer, long *num_copied) {
+  int c = EOF;
+  int done = 0;
+  int header_value_mode = 0;
+  int header_open_brackets = 0;
+
+  *num_copied = 0;
+
+  while(!done) {
+
+    c = fgetc(f);
+    if (c == EOF) break;
+
+    /* --------- header mode -------------- */
+    if (mime_demux_mode == MIME_DEMUX_MODE_MIME_HEADERS) {
+
+      /* always skip CRs */
+      if (c == '\r') continue;
+
+      if (c == '\n') {
+        if ((*num_copied) > 0) {
+          /* look if next char is '\t' or ' ' */
+          c = fgetc(f);
+          if (c == EOF) break;
+          if ( (c == '\t') || (c == ' ') ) continue;
+          (void)ungetc(c,f);
+        };
+        /* end of the header, terminate with ';' */
+        c = ';';
+        done = 1;
+      };
+
+      /* skip control characters */
+      if (c < 32) continue;
+
+      /* skip whitespace + tabs */
+      if ( (c == ' ') || (c == '\t') )
+        continue;
+
+      if (header_value_mode) {
+        /* --------- value mode ----------- */
+        /* skip quotes */
+        if (c == '"') continue;
+
+        /* leave value mode on ';' */
+        if (c == ';') {
+          header_value_mode = 0;
+        };
+        /* -------------------------------- */
+      }
+      else {
+        /* -------- non-value mode -------- */
+        if (c == '\\') {
+          /* quote next char. can be used
+          to escape brackets. */
+          c = fgetc(f);
+          if (c == EOF) break;
+        }
+        else if (c == '(') {
+          header_open_brackets++;
+          continue;
+        }
+        else if ((c == ')') && header_open_brackets) {
+          header_open_brackets--;
+          continue;
+        }
+        else if ( (c == '=') && !header_open_brackets ) {
+          /* enter value mode */
+          header_value_mode = 1;
+        };
+
+        /* skip chars while we are in a comment */
+        if (header_open_brackets > 0)
+          continue;
+        /* -------------------------------- */
+      };
+    }
+    /* ------------------------------------ */
+    else {
+    /* ----------- non-header mode -------- */
+      /* break on '\n' */
+      if (c == '\n')
+        done = 1;
+    /* ------------------------------------ */
+    };
+
+    /* copy the char to the buffer */
+    buffer[*num_copied] = (uschar)c;
+    /* raise counter */
+    (*num_copied)++;
+
+    /* break if buffer is full */
+    if (*num_copied > MIME_SANITY_MAX_LINE_LENGTH-1) {
+      done = 1;
+    };
+  }
+
+  /* 0-terminate */
+  buffer[*num_copied] = '\0';
+
+  if (*num_copied > MIME_SANITY_MAX_LINE_LENGTH-1)
+    return MIME_READ_LINE_OVERFLOW;
+  else
+    if (c == EOF)
+      return MIME_READ_LINE_EOF;
+    else
+      return MIME_READ_LINE_OK;
+}
+
+
+/*************************************************
+* Check for a MIME boundary                      *
+*************************************************/
+
+/* returns: 0 - no boundary found
+            1 - start boundary found
+            2 - end boundary found
+*/
+
+int mime_check_boundary(uschar *line, struct boundary *boundaries) {
+  struct boundary *thisboundary = boundaries;
+  uschar workbuf[MIME_SANITY_MAX_LINE_LENGTH+1];
+  unsigned int i,j=0;
+
+  /* check for '--' first */
+  if (Ustrncmp(line,"--",2) == 0) {
+
+    /* strip tab and space */
+    for (i = 2; i < Ustrlen(line); i++) {
+      if ((line[i] != ' ') && (line[i] != '\t')) {
+        workbuf[j] = line[i];
+        j++;
+      };
+    };
+    workbuf[j+1]='\0';
+
+    while(thisboundary != NULL) {
+      if (Ustrncmp(workbuf,thisboundary->boundary_string,Ustrlen(thisboundary->boundary_string)) == 0) {
+        if (Ustrncmp(&workbuf[Ustrlen(thisboundary->boundary_string)],"--",2) == 0) {
+          /* final boundary found */
+          return 2;
+        };
+        return 1;
+      };
+      thisboundary = thisboundary->next;
+    };
+  };
+
+  return 0;
+}
+
+
+/*************************************************
+* Check for start of a UUENCODE block            *
+*************************************************/
+
+/* returns 0 for no hit,
+           >0 for hit
+*/
+
+int mime_check_uu_start(uschar *line, uschar *uu_file_extension, int *has_tnef) {
+
+  if ( (strncmpic(line,US"begin ",6) == 0)) {
+    uschar *uu_filename = &line[6];
+
+    /* skip perms, if present */
+    Ustrtoul(&line[6],&uu_filename,10);
+
+    /* advance one char */
+    uu_filename++;
+
+    /* This should be the filename.
+    Check if winmail.dat is present,
+    which indicates TNEF. */
+    if (strncmpic(uu_filename,US"winmail.dat",11) == 0) {
+      *has_tnef = 1;
+    };
+
+    /* reverse to dot if present,
+    copy up to 4 chars for the extension */
+    if (Ustrrchr(uu_filename,'.') != NULL)
+      uu_filename = Ustrrchr(uu_filename,'.');
+
+    return sscanf(CS uu_filename, "%4[.0-9A-Za-z]",CS uu_file_extension);
+  }
+  else {
+    /* nothing found */
+    return 0;
+  };
+}
+
+
+/*************************************************
+* Decode a uu line                               *
+*************************************************/
+
+/* returns number of decoded bytes
+         -2 for soft errors
+*/
+
+int warned_about_uudec_line_sanity_1 = 0;
+int warned_about_uudec_line_sanity_2 = 0;
+long uu_decode_line(uschar *line, uschar **data, long line_len, uschar *info) {
+  uschar *p;
+  long num_decoded = 0;
+  uschar tmp_c;
+  uschar *work;
+  int uu_decoded_line_len, uu_encoded_line_len;
+
+  /* allocate memory for data and work buffer */
+  *data = (uschar *)malloc(line_len);
+  if (*data == NULL) {
+    (void)string_format(info, 1024,"unable to allocate %lu bytes",line_len);
+    return -2;
+  };
+
+  work = (uschar *)malloc(line_len);
+  if (work == NULL) {
+    (void)string_format(info, 1024,"unable to allocate %lu bytes",line_len);
+    return -2;
+  };
+
+  memcpy(work,line,line_len);
+
+  /* First char is line length
+  This is microsofts way of getting it. Scary. */
+  if (work[0] < 32) {
+    /* ignore this line */
+    return 0;
+  }
+  else {
+    uu_decoded_line_len = uudec[work[0]];
+  };
+
+  p = &work[1];
+
+  while (*p > 32) {
+    *p = uudec[*p];
+    p++;
+  };
+
+  uu_encoded_line_len = (p - &work[1]);
+  p = &work[1];
+
+  /* check that resulting line length is a multiple of 4 */
+  if ( ( uu_encoded_line_len % 4 ) != 0) {
+    if (!warned_about_uudec_line_sanity_1) {
+      mime_trigger_error(MIME_ERRORLEVEL_UU_MISALIGNED);
+      warned_about_uudec_line_sanity_1 = 1;
+    };
+    return -1;
+  };
+
+  /* check that the line length matches */
+  if ( ( (((uu_encoded_line_len/4)*3)-2) > uu_decoded_line_len ) || (((uu_encoded_line_len/4)*3) < uu_decoded_line_len) ) {
+    if (!warned_about_uudec_line_sanity_2) {
+      mime_trigger_error(MIME_ERRORLEVEL_UU_LINE_LENGTH);
+      warned_about_uudec_line_sanity_2 = 1;
+    };
+    return -1;
+  };
+
+  while ( ((p - &work[1]) < uu_encoded_line_len) && (num_decoded < uu_decoded_line_len)) {
+
+    /* byte 0 ---------------------- */
+    if ((p - &work[1] + 1) >= uu_encoded_line_len) {
+      return 0;
+    }
+
+    (*data)[num_decoded] = *p;
+    (*data)[num_decoded] <<= 2;
+
+    tmp_c = *(p+1);
+    tmp_c >>= 4;
+    (*data)[num_decoded] |= tmp_c;
+
+    num_decoded++;
+    p++;
+
+    /* byte 1 ---------------------- */
+    if ((p - &work[1] + 1) >= uu_encoded_line_len) {
+      return 0;
+    }
+
+    (*data)[num_decoded] = *p;
+    (*data)[num_decoded] <<= 4;
+
+    tmp_c = *(p+1);
+    tmp_c >>= 2;
+    (*data)[num_decoded] |= tmp_c;
+
+    num_decoded++;
+    p++;
+
+    /* byte 2 ---------------------- */
+    if ((p - &work[1] + 1) >= uu_encoded_line_len) {
+      return 0;
+    }
+
+    (*data)[num_decoded] = *p;
+    (*data)[num_decoded] <<= 6;
+
+    (*data)[num_decoded] |= *(p+1);
+
+    num_decoded++;
+    p+=2;
+
+  };
+
+  return uu_decoded_line_len;
+}
+
+
+/*************************************************
+* Decode a b64 or qp line                        *
+*************************************************/
+
+/* returns number of decoded bytes
+         -1 for hard errors
+         -2 for soft errors
+*/
+
+int warned_about_b64_line_length = 0;
+int warned_about_b64_line_sanity = 0;
+int warned_about_b64_illegal_char = 0;
+int warned_about_qp_line_sanity = 0;
+long mime_decode_line(int mime_demux_mode,uschar *line, uschar **data, long max_data_len, uschar *info) {
+  uschar *p;
+  long num_decoded = 0;
+  int offset = 0;
+  uschar tmp_c;
+
+  /* allocate memory for data */
+  *data = (uschar *)malloc(max_data_len);
+  if (*data == NULL) {
+    (void)string_format(info, 1024,"unable to allocate %lu bytes",max_data_len);
+    return -2;
+  };
+
+  if (mime_demux_mode == MIME_DEMUX_MODE_BASE64) {
+    /* ---------------------------------------------- */
+
+    /* NULL out trailing '\r' and '\n' chars */
+    while (Ustrrchr(line,'\r') != NULL) {
+      *(Ustrrchr(line,'\r')) = '\0';
+    };
+    while (Ustrrchr(line,'\n') != NULL) {
+      *(Ustrrchr(line,'\n')) = '\0';
+    };
+
+    /* check maximum base 64 line length */
+    if (Ustrlen(line) > MIME_SANITY_MAX_B64_LINE_LENGTH ) {
+      if (!warned_about_b64_line_length) {
+        mime_trigger_error(MIME_ERRORLEVEL_B64_LINE_LENGTH);
+        warned_about_b64_line_length = 1;
+      };
+    };
+
+    p = line;
+    offset = 0;
+    while (*(p+offset) != '\0') {
+      /* hit illegal char ? */
+      if (b64[*(p+offset)] == 128) {
+        if (!warned_about_b64_illegal_char) {
+          mime_trigger_error(MIME_ERRORLEVEL_B64_ILLEGAL_CHAR);
+          warned_about_b64_illegal_char = 1;
+        };
+        offset++;
+      }
+      else {
+        *p = b64[*(p+offset)];
+        p++;
+      };
+    };
+    *p = 255;
+
+    /* check that resulting line length is a multiple of 4 */
+    if ( ( (p - &line[0]) % 4 ) != 0) {
+      if (!warned_about_b64_line_sanity) {
+        mime_trigger_error(MIME_ERRORLEVEL_B64_MISALIGNED);
+        warned_about_b64_line_sanity = 1;
+      };
+    };
+
+    /* line is translated, start bit shifting */
+    p = line;
+    num_decoded = 0;
+
+    while(*p != 255) {
+
+      /* byte 0 ---------------------- */
+      if (*(p+1) == 255) {
+        break;
+      }
+
+      (*data)[num_decoded] = *p;
+      (*data)[num_decoded] <<= 2;
+
+      tmp_c = *(p+1);
+      tmp_c >>= 4;
+      (*data)[num_decoded] |= tmp_c;
+
+      num_decoded++;
+      p++;
+
+      /* byte 1 ---------------------- */
+      if (*(p+1) == 255) {
+        break;
+      }
+
+      (*data)[num_decoded] = *p;
+      (*data)[num_decoded] <<= 4;
+
+      tmp_c = *(p+1);
+      tmp_c >>= 2;
+      (*data)[num_decoded] |= tmp_c;
+
+      num_decoded++;
+      p++;
+
+      /* byte 2 ---------------------- */
+      if (*(p+1) == 255) {
+        break;
+      }
+
+      (*data)[num_decoded] = *p;
+      (*data)[num_decoded] <<= 6;
+
+      (*data)[num_decoded] |= *(p+1);
+
+      num_decoded++;
+      p+=2;
+
+    };
+    return num_decoded;
+    /* ---------------------------------------------- */
+  }
+  else if (mime_demux_mode == MIME_DEMUX_MODE_QP) {
+    /* ---------------------------------------------- */
+    p = line;
+
+    while (*p != 0) {
+      if (*p == '=') {
+        int decode_qp_result;
+
+        p = mime_decode_qp(p,&decode_qp_result);
+
+        if (decode_qp_result == -2) {
+          /* Error from decoder. p is unchanged. */
+          if (!warned_about_qp_line_sanity) {
+            mime_trigger_error(MIME_ERRORLEVEL_QP_ILLEGAL_CHAR);
+            warned_about_qp_line_sanity = 1;
+          };
+          (*data)[num_decoded] = '=';
+          num_decoded++;
+          p++;
+        }
+        else if (decode_qp_result == -1) {
+          /* End of the line with soft line break.
+          Bail out. */
+          goto QP_RETURN;
+        }
+        else if (decode_qp_result >= 0) {
+          (*data)[num_decoded] = decode_qp_result;
+          num_decoded++;
+        };
+      }
+      else {
+        (*data)[num_decoded] = *p;
+        num_decoded++;
+        p++;
+      };
+    };
+    QP_RETURN:
+    return num_decoded;
+    /* ---------------------------------------------- */
+  };
+
+  return 0;
+}
+
+
+
+/*************************************************
+* Log demime errors and set mime error level     *
+*************************************************/
+
+/* This sets the global demime_reason expansion
+variable and the demime_errorlevel gauge. */
+
+void mime_trigger_error(int level, uschar *format, ...) {
+  char *f;
+  va_list ap;
+
+  if( (f = malloc(16384+23)) != NULL ) {
+    /* first log the incident */
+    sprintf(f,"demime acl condition: ");
+    f+=22;
+    va_start(ap, format);
+    (void)string_vformat(US f, 16383,(char *)format, ap);
+    va_end(ap);
+    f-=22;
+    log_write(0, LOG_MAIN, "%s", f);
+    /* then copy to demime_reason_buffer if new
+    level is greater than old level */
+    if (level > demime_errorlevel) {
+      demime_errorlevel = level;
+      Ustrcpy(demime_reason_buffer, US f);
+      demime_reason = demime_reason_buffer;
+    };
+    free(f);
+  };
+}
+
+/*************************************************
+* Demultiplex MIME stream.                       *
+*************************************************/
+
+/* We can handle BASE64, QUOTED-PRINTABLE, and UUENCODE.
+ UUENCODE does not need to have a proper
+ transfer-encoding header, we detect it with "begin"
+
+ This function will report human parsable errors in
+ *info.
+
+ returns DEFER -> soft error (see *info)
+         OK    -> EOF hit, all ok
+*/
+
+int mime_demux(FILE *f, uschar *info) {
+  int mime_demux_mode = MIME_DEMUX_MODE_MIME_HEADERS;
+  int uu_mode = MIME_UU_MODE_OFF;
+  FILE *mime_dump_file = NULL;
+  FILE *uu_dump_file = NULL;
+  uschar *line;
+  int mime_read_line_status = MIME_READ_LINE_OK;
+  long line_len;
+  struct boundary *boundaries = NULL;
+  struct mime_part mime_part_p;
+  int has_tnef = 0;
+  int has_rfc822 = 0;
+
+  /* allocate room for our linebuffer */
+  line = (uschar *)malloc(MIME_SANITY_MAX_LINE_LENGTH);
+  if (line == NULL) {
+    (void)string_format(info, 1024,"unable to allocate %u bytes",MIME_SANITY_MAX_LINE_LENGTH);
+    return DEFER;
+  };
+
+  /* clear MIME header structure */
+  memset(&mime_part_p,0,sizeof(mime_part));
+
+  /* ----------------------- start demux loop --------------------- */
+  while (mime_read_line_status == MIME_READ_LINE_OK) {
+
+    /* read a line of input. Depending on the mode we are in,
+    the returned format will differ. */
+    mime_read_line_status = mime_read_line(f,mime_demux_mode,line,&line_len);
+
+    if (mime_read_line_status == MIME_READ_LINE_OVERFLOW) {
+      mime_trigger_error(MIME_ERRORLEVEL_LONG_LINE);
+      /* despite the error, continue  .. */
+      mime_read_line_status = MIME_READ_LINE_OK;
+      continue;
+    }
+    else if (mime_read_line_status == MIME_READ_LINE_EOF) {
+      break;
+    };
+
+    if (mime_demux_mode == MIME_DEMUX_MODE_MIME_HEADERS) {
+      /* -------------- header mode --------------------- */
+
+      /* Check for an empty line, which is the end of the headers.
+       In HEADER mode, the line is returned "cooked", with the
+       final '\n' replaced by a ';' */
+      if (line_len == 1) {
+        int tmp;
+
+        /* We have reached the end of the headers. Start decoding
+        with the collected settings. */
+        if (mime_part_p.seen_content_transfer_encoding > 1) {
+          mime_demux_mode = mime_part_p.seen_content_transfer_encoding;
+        }
+        else {
+          /* default to plain mode if no specific encoding type found */
+          mime_demux_mode = MIME_DEMUX_MODE_PLAIN;
+        };
+
+        /* open new dump file */
+        tmp = mime_get_dump_file(mime_part_p.extension, &mime_dump_file, info);
+        if (tmp < 0) {
+          return DEFER;
+        };
+
+        /* clear out mime_part */
+        memset(&mime_part_p,0,sizeof(mime_part));
+      }
+      else {
+        /* Another header to check for file extensions,
+        encoding type and boundaries */
+        if (strncmpic(US"content-type:",line,Ustrlen("content-type:")) == 0) {
+          /* ---------------------------- Content-Type header ------------------------------- */
+          uschar *value = line;
+
+          /* check for message/partial MIME type and reject it */
+          if (mime_header_find(line,US"message/partial",NULL) > 0)
+            mime_trigger_error(MIME_ERRORLEVEL_MESSAGE_PARTIAL);
+
+          /* check for TNEF content type, remember to unpack TNEF later. */
+          if (mime_header_find(line,US"application/ms-tnef",NULL) > 0)
+            has_tnef = 1;
+
+          /* check for message/rfcxxx attachments */
+          if (mime_header_find(line,US"message/rfc822",NULL) > 0)
+            has_rfc822 = 1;
+
+          /* find the file extension, but do not fill it in
+          it is already set, since content-disposition has
+          precedence. */
+          if (mime_part_p.extension == NULL) {
+            if (mime_header_find(line,US"name",&value) == 2) {
+              if (Ustrlen(value) > MIME_SANITY_MAX_FILENAME)
+                mime_trigger_error(MIME_ERRORLEVEL_FILENAME_LENGTH);
+              mime_part_p.extension = value;
+              mime_part_p.extension = Ustrrchr(value,'.');
+              if (mime_part_p.extension == NULL) {
+                /* file without extension, setting
+                NULL will use the default extension later */
+                mime_part_p.extension = NULL;
+              }
+              else {
+                struct file_extension *this_extension =
+                  (struct file_extension *)malloc(sizeof(file_extension));
+
+                this_extension->file_extension_string =
+                  (uschar *)malloc(Ustrlen(mime_part_p.extension)+1);
+                Ustrcpy(this_extension->file_extension_string,
+                        mime_part_p.extension+1);
+                this_extension->next = file_extensions;
+                file_extensions = this_extension;
+              };
+            };
+          };
+
+          /* find a boundary and add it to the list, if present */
+          value = line;
+          if (mime_header_find(line,US"boundary",&value) == 2) {
+            struct boundary *thisboundary;
+
+            if (Ustrlen(value) > MIME_SANITY_MAX_BOUNDARY_LENGTH) {
+              mime_trigger_error(MIME_ERRORLEVEL_BOUNDARY_LENGTH);
+            }
+            else {
+              thisboundary = (struct boundary*)malloc(sizeof(boundary));
+              thisboundary->next = boundaries;
+              thisboundary->boundary_string = value;
+              boundaries = thisboundary;
+            };
+          };
+
+          if (mime_part_p.seen_content_type == 0) {
+            mime_part_p.seen_content_type = 1;
+          }
+          else {
+            mime_trigger_error(MIME_ERRORLEVEL_DOUBLE_HEADERS);
+          };
+          /* ---------------------------------------------------------------------------- */
+        }
+        else if (strncmpic(US"content-transfer-encoding:",line,Ustrlen("content-transfer-encoding:")) == 0) {
+          /* ---------------------------- Content-Transfer-Encoding header -------------- */
+
+         if (mime_part_p.seen_content_transfer_encoding == 0) {
+            if (mime_header_find(line,US"base64",NULL) > 0) {
+              mime_part_p.seen_content_transfer_encoding = MIME_DEMUX_MODE_BASE64;
+            }
+            else if (mime_header_find(line,US"quoted-printable",NULL) > 0) {
+              mime_part_p.seen_content_transfer_encoding = MIME_DEMUX_MODE_QP;
+            }
+            else {
+              mime_part_p.seen_content_transfer_encoding = MIME_DEMUX_MODE_PLAIN;
+            };
+          }
+          else {
+            mime_trigger_error(MIME_ERRORLEVEL_DOUBLE_HEADERS);
+          };
+          /* ---------------------------------------------------------------------------- */
+        }
+        else if (strncmpic(US"content-disposition:",line,Ustrlen("content-disposition:")) == 0) {
+          /* ---------------------------- Content-Disposition header -------------------- */
+          uschar *value = line;
+
+          if (mime_part_p.seen_content_disposition == 0) {
+            mime_part_p.seen_content_disposition = 1;
+
+            if (mime_header_find(line,US"filename",&value) == 2) {
+              if (Ustrlen(value) > MIME_SANITY_MAX_FILENAME)
+                mime_trigger_error(MIME_ERRORLEVEL_FILENAME_LENGTH);
+              mime_part_p.extension = value;
+              mime_part_p.extension = Ustrrchr(value,'.');
+              if (mime_part_p.extension == NULL) {
+                /* file without extension, setting
+                NULL will use the default extension later */
+                mime_part_p.extension = NULL;
+              }
+              else {
+                struct file_extension *this_extension =
+                  (struct file_extension *)malloc(sizeof(file_extension));
+
+                this_extension->file_extension_string =
+                  (uschar *)malloc(Ustrlen(mime_part_p.extension)+1);
+                Ustrcpy(this_extension->file_extension_string,
+                        mime_part_p.extension+1);
+                this_extension->next = file_extensions;
+                file_extensions = this_extension;
+              };
+            };
+          }
+          else {
+            mime_trigger_error(MIME_ERRORLEVEL_DOUBLE_HEADERS);
+          };
+          /* ---------------------------------------------------------------------------- */
+        };
+      };    /* End of header checks */
+      /* ------------------------------------------------ */
+    }
+    else {
+      /* -------------- non-header mode ----------------- */
+      int tmp;
+
+      if (uu_mode == MIME_UU_MODE_OFF) {
+        uschar uu_file_extension[5];
+        /* We are not currently decoding UUENCODE
+        Check for possible UUENCODE start tag. */
+        if (mime_check_uu_start(line,uu_file_extension,&has_tnef)) {
+          /* possible UUENCODING start detected.
+          Set unconfirmed mode first. */
+          uu_mode = MIME_UU_MODE_UNCONFIRMED;
+          /* open new uu dump file */
+          tmp = mime_get_dump_file(uu_file_extension, &uu_dump_file, info);
+          if (tmp < 0) {
+            free(line);
+            return DEFER;
+          };
+        };
+      }
+      else {
+        uschar *data;
+        long data_len = 0;
+
+        if (uu_mode == MIME_UU_MODE_UNCONFIRMED) {
+         /* We are in unconfirmed UUENCODE mode. */
+
+         data_len = uu_decode_line(line,&data,line_len,info);
+
+         if (data_len == -2) {
+           /* temp error, turn off uudecode mode */
+           if (uu_dump_file != NULL) {
+            (void)fclose(uu_dump_file); uu_dump_file = NULL;
+           };
+           uu_mode = MIME_UU_MODE_OFF;
+           return DEFER;
+         }
+         else if (data_len == -1) {
+           if (uu_dump_file != NULL) {
+            (void)fclose(uu_dump_file); uu_dump_file = NULL;
+           };
+           uu_mode = MIME_UU_MODE_OFF;
+           data_len = 0;
+         }
+         else if (data_len > 0) {
+           /* we have at least decoded a valid byte
+           turn on confirmed mode */
+           uu_mode = MIME_UU_MODE_CONFIRMED;
+         };
+        }
+        else if (uu_mode == MIME_UU_MODE_CONFIRMED) {
+          /* If we are in confirmed UU mode,
+          check for single "end" tag on line */
+          if ((strncmpic(line,US"end",3) == 0) && (line[3] < 32)) {
+            if (uu_dump_file != NULL) {
+              (void)fclose(uu_dump_file); uu_dump_file = NULL;
+            };
+            uu_mode = MIME_UU_MODE_OFF;
+          }
+          else {
+            data_len = uu_decode_line(line,&data,line_len,info);
+            if (data_len == -2) {
+               /* temp error, turn off uudecode mode */
+               if (uu_dump_file != NULL) {
+                 (void)fclose(uu_dump_file); uu_dump_file = NULL;
+               };
+               uu_mode = MIME_UU_MODE_OFF;
+               return DEFER;
+             }
+             else if (data_len == -1) {
+               /* skip this line */
+               data_len = 0;
+             };
+          };
+        };
+
+        /* write data to dump file, if available */
+        if (data_len > 0) {
+          if (fwrite(data,1,data_len,uu_dump_file) < data_len) {
+            /* short write */
+            (void)string_format(info, 1024,"short write on uudecode dump file");
+            free(line);
+            return DEFER;
+          };
+        };
+      };
+
+      if (mime_demux_mode != MIME_DEMUX_MODE_SCANNING) {
+        /* Non-scanning and Non-header mode. That means
+        we are currently decoding data to the dump
+        file. */
+
+        /* Check for a known boundary. */
+        tmp = mime_check_boundary(line,boundaries);
+        if (tmp == 1) {
+          /* We have hit a known start boundary.
+          That will put us back in header mode. */
+          mime_demux_mode = MIME_DEMUX_MODE_MIME_HEADERS;
+          if (mime_dump_file != NULL) {
+            /* if the attachment was a RFC822 message, recurse into it */
+            if (has_rfc822) {
+              has_rfc822 = 0;
+              rewind(mime_dump_file);
+              mime_demux(mime_dump_file,info);
+            };
+
+            (void)fclose(mime_dump_file); mime_dump_file = NULL;
+          };
+        }
+        else if (tmp == 2) {
+          /* We have hit a known end boundary.
+          That puts us into scanning mode, which will end when we hit another known start boundary */
+          mime_demux_mode = MIME_DEMUX_MODE_SCANNING;
+          if (mime_dump_file != NULL) {
+            /* if the attachment was a RFC822 message, recurse into it */
+            if (has_rfc822) {
+              has_rfc822 = 0;
+              rewind(mime_dump_file);
+              mime_demux(mime_dump_file,info);
+            };
+
+            (void)fclose(mime_dump_file); mime_dump_file = NULL;
+          };
+        }
+        else {
+          uschar *data;
+          long data_len = 0;
+
+          /* decode the line with the appropriate method */
+          if (mime_demux_mode == MIME_DEMUX_MODE_PLAIN) {
+            /* in plain mode, just dump the line */
+            data = line;
+            data_len = line_len;
+          }
+          else if ( (mime_demux_mode == MIME_DEMUX_MODE_QP) || (mime_demux_mode == MIME_DEMUX_MODE_BASE64) ) {
+            data_len = mime_decode_line(mime_demux_mode,line,&data,line_len,info);
+            if (data_len < 0) {
+              /* Error reported from the line decoder. */
+              data_len = 0;
+            };
+          };
+
+          /* write data to dump file */
+          if (data_len > 0) {
+            if (fwrite(data,1,data_len,mime_dump_file) < data_len) {
+              /* short write */
+              (void)string_format(info, 1024,"short write on dump file");
+              free(line);
+              return DEFER;
+            };
+          };
+
+        };
+      }
+      else {
+        /* Scanning mode. We end up here after a end boundary.
+        This will usually be at the end of a message or at
+        the end of a MIME container.
+        We need to look for another start boundary to get
+        back into header mode. */
+        if (mime_check_boundary(line,boundaries) == 1) {
+          mime_demux_mode = MIME_DEMUX_MODE_MIME_HEADERS;
+        };
+
+      };
+      /* ------------------------------------------------ */
+    };
+  };
+  /* ----------------------- end demux loop ----------------------- */
+
+  /* close files, they could still be open */
+  if (mime_dump_file != NULL)
+    (void)fclose(mime_dump_file);
+  if (uu_dump_file != NULL)
+    (void)fclose(uu_dump_file);
+
+  /* release line buffer */
+  free(line);
+
+  /* FIXME: release boundary buffers.
+  Not too much of a problem since
+  this instance of exim is not resident. */
+
+  if (has_tnef) {
+    uschar file_name[1024];
+    /* at least one file could be TNEF encoded.
+    attempt to send all decoded files thru the TNEF decoder */
+
+    (void)string_format(file_name,1024,"%s/scan/%s",spool_directory,message_id);
+    /* Removed FTTB. We need to decide on TNEF inclusion */
+    /* mime_unpack_tnef(file_name); */
+  };
+
+  return 0;
+}
+
+#endif
diff --git a/src/demime.h b/src/demime.h
new file mode 100644 (file)
index 0000000..0fec5be
--- /dev/null
@@ -0,0 +1,134 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) Tom Kistner <tom@duncanthrax.net> 2003-???? */
+/* License: GPL */
+
+/* demime defines */
+
+#ifdef WITH_OLD_DEMIME
+
+#define MIME_DEMUX_MODE_SCANNING     0
+#define MIME_DEMUX_MODE_MIME_HEADERS 1
+#define MIME_DEMUX_MODE_BASE64       2
+#define MIME_DEMUX_MODE_QP           3
+#define MIME_DEMUX_MODE_PLAIN        4
+
+#define MIME_UU_MODE_OFF             0
+#define MIME_UU_MODE_UNCONFIRMED     1
+#define MIME_UU_MODE_CONFIRMED       2
+
+#define MIME_MAX_EXTENSION 128
+
+#define MIME_READ_LINE_EOF 0
+#define MIME_READ_LINE_OK 1
+#define MIME_READ_LINE_OVERFLOW 2
+
+#define MIME_SANITY_MAX_LINE_LENGTH 131071
+#define MIME_SANITY_MAX_FILENAME 512
+#define MIME_SANITY_MAX_HEADER_OPTION_VALUE 1024
+#define MIME_SANITY_MAX_B64_LINE_LENGTH 76
+#define MIME_SANITY_MAX_BOUNDARY_LENGTH 1024
+#define MIME_SANITY_MAX_DUMP_FILES 1024
+
+
+
+/* MIME errorlevel settings */
+
+#define MIME_ERRORLEVEL_LONG_LINE        3,US"line length in message or single header size exceeds %u bytes",MIME_SANITY_MAX_LINE_LENGTH
+#define MIME_ERRORLEVEL_TOO_MANY_PARTS   3,US"too many MIME parts (max %u)",MIME_SANITY_MAX_DUMP_FILES
+#define MIME_ERRORLEVEL_MESSAGE_PARTIAL  3,US"'message/partial' MIME type"
+#define MIME_ERRORLEVEL_FILENAME_LENGTH  3,US"proposed filename exceeds %u characters",MIME_SANITY_MAX_FILENAME
+#define MIME_ERRORLEVEL_BOUNDARY_LENGTH  3,US"boundary length exceeds %u characters",MIME_SANITY_MAX_BOUNDARY_LENGTH
+#define MIME_ERRORLEVEL_DOUBLE_HEADERS   2,US"double headers (content-type, content-disposition or content-transfer-encoding)"
+#define MIME_ERRORLEVEL_UU_MISALIGNED    1,US"uuencoded line length is not a multiple of 4 characters"
+#define MIME_ERRORLEVEL_UU_LINE_LENGTH   1,US"uuencoded line length does not match advertised number of bytes"
+#define MIME_ERRORLEVEL_B64_LINE_LENGTH  1,US"base64 line length exceeds %u characters",MIME_SANITY_MAX_B64_LINE_LENGTH
+#define MIME_ERRORLEVEL_B64_ILLEGAL_CHAR 2,US"base64 line contains illegal character"
+#define MIME_ERRORLEVEL_B64_MISALIGNED   1,US"base64 line length is not a multiple of 4 characters"
+#define MIME_ERRORLEVEL_QP_ILLEGAL_CHAR  1,US"quoted-printable encoding contains illegal character"
+
+
+/* demime structures */
+
+typedef struct mime_part {
+  /* true if there was a content-type header */
+  int seen_content_type;
+  /* true if there was a content-transfer-encoding header
+     contains the encoding type */
+  int seen_content_transfer_encoding;
+  /* true if there was a content-disposition header */
+  int seen_content_disposition;
+  /* pointer to a buffer with the proposed file extension */
+  uschar *extension;
+} mime_part;
+
+typedef struct boundary {
+  struct boundary *next;
+  uschar *boundary_string;
+} boundary;
+
+typedef struct file_extension {
+  struct file_extension *next;
+  uschar *file_extension_string;
+} file_extension;
+
+/* demime.c prototypes */
+
+unsigned int mime_hstr_i(uschar *);
+uschar      *mime_decode_qp(uschar *, int *);
+int          mime_get_dump_file(uschar *, FILE **, uschar *);
+int          mime_header_find(uschar *, uschar *, uschar **);
+int          mime_read_line(FILE *, int, uschar *, long *);
+int          mime_check_boundary(uschar *, struct boundary *);
+int          mime_check_uu_start(uschar *, uschar *, int *);
+long         uu_decode_line(uschar *, uschar **, long, uschar *);
+long         mime_decode_line(int ,uschar *, uschar **, long, uschar *);
+void         mime_trigger_error(int, uschar *, ...);
+int          mime_demux(FILE *, uschar *);
+
+
+
+/* BASE64 decoder matrix */
+static unsigned char b64[256]={
+/*   0 */ 128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,
+/*  16 */ 128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,
+/*  32 */ 128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,   62,  128,  128,  128,   63,
+/*  48 */  52,   53,   54,   55,   56,   57,   58,   59,   60,   61,  128,  128,  128,  255,  128,  128,
+/*  64 */ 128,    0,    1,    2,    3,    4,    5,    6,    7,    8,    9,   10,   11,   12,   13,   14,
+/*  80 */  15,   16,   17,   18,   19,   20,   21,   22,   23,   24,   25,  128,  128,  128,  128,  128,
+/*  96 */ 128,   26,   27,   28,   29,   30,   31,   32,   33,   34,   35,   36,   37,   38,   39,   40,
+  41,   42,   43,   44,   45,   46,   47,   48,   49,   50,   51,  128,  128,  128,  128,  128,
+ 128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,
+ 128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,
+ 128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,
+ 128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,
+ 128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,
+ 128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,
+ 128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,
+ 128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128
+};
+
+
+/* Microsoft-Style uudecode matrix */
+static unsigned char uudec[256]={
+/*   0 */   0,   33,   34,   35,   36,   37,   38,   39,   40,   41,   42,   43,   44,   45,   46,   47,
+/*  16 */  48,   49,   50,   51,   52,   53,   54,   55,   56,   57,   58,   59,   60,   61,   62,   63,
+/*  32 */   0,    1,    2,    3,    4,    5,    6,    7,    8,    9,   10,   11,   12,   13,   14,   15,
+/*  48 */  16,   17,   18,   19,   20,   21,   22,   23,   24,   25,   26,   27,   28,   29,   30,   31,
+/*  64 */  32,   33,   34,   35,   36,   37,   38,   39,   40,   41,   42,   43,   44,   45,   46,   47,
+/*  80 */  48,   49,   50,   51,   52,   53,   54,   55,   56,   57,   58,   59,   60,   61,   62,   63,
+/*  96 */   0,    1,    2,    3,    4,    5,    6,    7,    8,    9,   10,   11,   12,   13,   14,   15,
+/* 112 */  16,   17,   18,   19,   20,   21,   22,   23,   24,   25,   26,   27,   28,   29,   30,   31,
+/* 128 */  32,   33,   34,   35,   36,   37,   38,   39,   40,   41,   42,   43,   44,   45,   46,   47,
+/* 144 */  48,   49,   50,   51,   52,   53,   54,   55,   56,   57,   58,   59,   60,   61,   62,   63,
+/* 160 */   0,    1,    2,    3,    4,    5,    6,    7,    8,    9,   10,   11,   12,   13,   14,   15,
+/* 176 */  16,   17,   18,   19,   20,   21,   22,   23,   24,   25,   26,   27,   28,   29,   30,   31,
+/* 192 */  32,   33,   34,   35,   36,   37,   38,   39,   40,   41,   42,   43,   44,   45,   46,   47,
+/* 208 */  48,   49,   50,   51,   52,   53,   54,   55,   56,   57,   58,   59,   60,   61,   62,   63,
+/* 224 */   0,    1,    2,    3,    4,    5,    6,    7,    8,    9,   10,   11,   12,   13,   14,   15,
+/* 240 */  16,   17,   18,   19,   20,   21,   22,   23,   24,   25,   26,   27,   28,   29,   30,   31
+};
+
+#endif
diff --git a/src/directory.c b/src/directory.c
new file mode 100644 (file)
index 0000000..5c55a45
--- /dev/null
@@ -0,0 +1,96 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+#include "exim.h"
+
+
+/*************************************************
+*           Attempt to create a directory        *
+*************************************************/
+
+/* All the directories that Exim ever creates for itself are within the spool
+directory as defined by spool_directory. We are prepared to create as many as
+necessary from that directory downwards, inclusive. However, directory creation
+can also be required in appendfile and sieve filters. The making function
+therefore has a parent argument, below which the new directories are to go. It
+can be NULL if the name is absolute.
+
+If a non-root uid has been specified for exim, and we are currently running as
+root, ensure the directory is owned by the non-root id if the parent is the
+spool directory.
+
+Arguments:
+  parent    parent directory name; if NULL the name must be absolute
+  name      directory name within the parent that we want
+  mode      mode for the new directory
+  panic     if TRUE, panic on failure
+
+Returns:    panic on failure if panic is set; otherwise return FALSE;
+            TRUE on success.
+*/
+
+BOOL
+directory_make(const uschar *parent, const uschar *name,
+               int mode, BOOL panic)
+{
+BOOL use_chown = parent == spool_directory && geteuid() == root_uid;
+uschar *p;
+const uschar *slash;
+int c = 1;
+struct stat statbuf;
+uschar buffer[256];
+
+if (parent == NULL)
+  {
+  p = buffer + 1;
+  slash = parent = CUS"";
+  }
+else
+  {
+  p = buffer + Ustrlen(parent);
+  slash = US"/";
+  }
+
+if (!string_format(buffer, sizeof(buffer), "%s%s%s", parent, slash, name))
+  log_write(0, LOG_MAIN|LOG_PANIC_DIE, "name too long in directory_make");
+
+while (c != 0 && *p != 0)
+  {
+  while (*p != 0 && *p != '/') p++;
+  c = *p;
+  *p = 0;
+  if (Ustat(buffer, &statbuf) != 0)
+    {
+    if (mkdir(CS buffer, mode) < 0 && errno != EEXIST)
+      {
+      if (!panic) return FALSE;
+      log_write(0, LOG_MAIN|LOG_PANIC_DIE,
+        "Failed to create directory \"%s\": %s\n", buffer, strerror(errno));
+      }
+
+    /* Set the ownership if necessary. */
+
+    if (use_chown && Uchown(buffer, exim_uid, exim_gid))
+      {
+      if (!panic) return FALSE;
+      log_write(0, LOG_MAIN|LOG_PANIC_DIE,
+        "Failed to set owner on directory \"%s\": %s\n", buffer, strerror(errno));
+      }
+
+    /* It appears that any mode bits greater than 0777 are ignored by
+    mkdir(), at least on some operating systems. Therefore, if the mode
+    contains any such bits, do an explicit mode setting. */
+
+    if ((mode & 0777000) != 0) (void)Uchmod(buffer, mode);
+    }
+  *p++ = c;
+  }
+
+return TRUE;
+}
+
+/* End of directory.c */
diff --git a/src/dkim.c b/src/dkim.c
new file mode 100644 (file)
index 0000000..171fccc
--- /dev/null
@@ -0,0 +1,580 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge, 1995 - 2007 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Code for DKIM support. Other DKIM relevant code is in
+   receive.c, transport.c and transports/smtp.c */
+
+#include "exim.h"
+
+#ifndef DISABLE_DKIM
+
+#include "pdkim/pdkim.h"
+
+pdkim_ctx       *dkim_verify_ctx = NULL;
+pdkim_signature *dkim_signatures = NULL;
+pdkim_signature *dkim_cur_sig    = NULL;
+
+int dkim_exim_query_dns_txt(char *name, char *answer) {
+  dns_answer dnsa;
+  dns_scan   dnss;
+  dns_record *rr;
+
+  lookup_dnssec_authenticated = NULL;
+  if (dns_lookup(&dnsa, (uschar *)name, T_TXT, NULL) != DNS_SUCCEED) return PDKIM_FAIL;
+
+  /* Search for TXT record */
+  for (rr = dns_next_rr(&dnsa, &dnss, RESET_ANSWERS);
+       rr != NULL;
+       rr = dns_next_rr(&dnsa, &dnss, RESET_NEXT))
+    if (rr->type == T_TXT) break;
+
+  /* Copy record content to the answer buffer */
+  if (rr != NULL) {
+    int rr_offset = 0;
+    int answer_offset = 0;
+    while (rr_offset < rr->size) {
+      uschar len = (rr->data)[rr_offset++];
+      snprintf(answer+(answer_offset),
+               PDKIM_DNS_TXT_MAX_RECLEN-(answer_offset),
+               "%.*s", (int)len, (char *)((rr->data)+rr_offset));
+      rr_offset+=len;
+      answer_offset+=len;
+      if (answer_offset >= PDKIM_DNS_TXT_MAX_RECLEN) {
+        return PDKIM_FAIL;
+      }
+    }
+  }
+  else return PDKIM_FAIL;
+
+  return PDKIM_OK;
+}
+
+
+void dkim_exim_verify_init(void) {
+
+  /* Free previous context if there is one */
+  if (dkim_verify_ctx) pdkim_free_ctx(dkim_verify_ctx);
+
+  /* Create new context */
+  dkim_verify_ctx = pdkim_init_verify(PDKIM_INPUT_SMTP,
+                                      &dkim_exim_query_dns_txt
+                                     );
+
+  if (dkim_verify_ctx != NULL) {
+    dkim_collect_input = TRUE;
+    pdkim_set_debug_stream(dkim_verify_ctx,debug_file);
+  }
+  else dkim_collect_input = FALSE;
+
+}
+
+
+void dkim_exim_verify_feed(uschar *data, int len) {
+  if (dkim_collect_input &&
+      pdkim_feed(dkim_verify_ctx,
+                 (char *)data,
+                 len) != PDKIM_OK) dkim_collect_input = FALSE;
+}
+
+
+void dkim_exim_verify_finish(void) {
+  pdkim_signature *sig = NULL;
+  int dkim_signers_size = 0;
+  int dkim_signers_ptr = 0;
+  dkim_signers = NULL;
+
+  /* Delete eventual previous signature chain */
+  dkim_signatures = NULL;
+
+  /* If we have arrived here with dkim_collect_input == FALSE, it
+     means there was a processing error somewhere along the way.
+     Log the incident and disable futher verification. */
+  if (!dkim_collect_input) {
+    log_write(0, LOG_MAIN, "DKIM: Error while running this message through validation, disabling signature verification.");
+    dkim_disable_verify = TRUE;
+    return;
+  }
+  dkim_collect_input = FALSE;
+
+  /* Finish DKIM operation and fetch link to signatures chain */
+  if (pdkim_feed_finish(dkim_verify_ctx,&dkim_signatures) != PDKIM_OK) return;
+
+  sig = dkim_signatures;
+  while (sig != NULL) {
+    int size = 0;
+    int ptr = 0;
+    /* Log a line for each signature */
+    uschar *logmsg = string_append(NULL, &size, &ptr, 5,
+
+      string_sprintf( "d=%s s=%s c=%s/%s a=%s ",
+                      sig->domain,
+                      sig->selector,
+                      (sig->canon_headers == PDKIM_CANON_SIMPLE)?"simple":"relaxed",
+                      (sig->canon_body    == PDKIM_CANON_SIMPLE)?"simple":"relaxed",
+                      (sig->algo          == PDKIM_ALGO_RSA_SHA256)?"rsa-sha256":"rsa-sha1"
+                    ),
+      ((sig->identity != NULL)?
+        string_sprintf("i=%s ", sig->identity)
+        :
+        US""
+      ),
+      ((sig->created > 0)?
+        string_sprintf("t=%lu ", sig->created)
+        :
+        US""
+      ),
+      ((sig->expires > 0)?
+        string_sprintf("x=%lu ", sig->expires)
+        :
+        US""
+      ),
+      ((sig->bodylength > -1)?
+        string_sprintf("l=%lu ", sig->bodylength)
+        :
+        US""
+      )
+    );
+
+    switch(sig->verify_status) {
+      case PDKIM_VERIFY_NONE:
+        logmsg = string_append(logmsg, &size, &ptr, 1, "[not verified]");
+      break;
+      case PDKIM_VERIFY_INVALID:
+        logmsg = string_append(logmsg, &size, &ptr, 1, "[invalid - ");
+        switch (sig->verify_ext_status) {
+          case PDKIM_VERIFY_INVALID_PUBKEY_UNAVAILABLE:
+            logmsg = string_append(logmsg, &size, &ptr, 1, "public key record (currently?) unavailable]");
+          break;
+          case PDKIM_VERIFY_INVALID_BUFFER_SIZE:
+            logmsg = string_append(logmsg, &size, &ptr, 1, "overlong public key record]");
+          break;
+          case PDKIM_VERIFY_INVALID_PUBKEY_PARSING:
+            logmsg = string_append(logmsg, &size, &ptr, 1, "syntax error in public key record]");
+          break;
+          default:
+            logmsg = string_append(logmsg, &size, &ptr, 1, "unspecified problem]");
+        }
+      break;
+      case PDKIM_VERIFY_FAIL:
+        logmsg = string_append(logmsg, &size, &ptr, 1, "[verification failed - ");
+        switch (sig->verify_ext_status) {
+          case PDKIM_VERIFY_FAIL_BODY:
+            logmsg = string_append(logmsg, &size, &ptr, 1, "body hash mismatch (body probably modified in transit)]");
+          break;
+          case PDKIM_VERIFY_FAIL_MESSAGE:
+            logmsg = string_append(logmsg, &size, &ptr, 1, "signature did not verify (headers probably modified in transit)]");
+          break;
+          default:
+            logmsg = string_append(logmsg, &size, &ptr, 1, "unspecified reason]");
+        }
+      break;
+      case PDKIM_VERIFY_PASS:
+        logmsg = string_append(logmsg, &size, &ptr, 1, "[verification succeeded]");
+      break;
+    }
+
+    logmsg[ptr] = '\0';
+    log_write(0, LOG_MAIN, "DKIM: %s", logmsg);
+
+    /* Build a colon-separated list of signing domains (and identities, if present) in dkim_signers */
+    dkim_signers = string_append(dkim_signers,
+                                 &dkim_signers_size,
+                                 &dkim_signers_ptr,
+                                 2,
+                                 sig->domain,
+                                 ":"
+                                );
+
+    if (sig->identity != NULL) {
+      dkim_signers = string_append(dkim_signers,
+                                   &dkim_signers_size,
+                                   &dkim_signers_ptr,
+                                   2,
+                                   sig->identity,
+                                   ":"
+                                  );
+    }
+
+    /* Process next signature */
+    sig = sig->next;
+  }
+
+  /* NULL-terminate and chop the last colon from the domain list */
+  if (dkim_signers != NULL) {
+    dkim_signers[dkim_signers_ptr] = '\0';
+    if (Ustrlen(dkim_signers) > 0)
+      dkim_signers[Ustrlen(dkim_signers)-1] = '\0';
+  }
+}
+
+
+void dkim_exim_acl_setup(uschar *id) {
+  pdkim_signature *sig = dkim_signatures;
+  dkim_cur_sig = NULL;
+  dkim_cur_signer = id;
+  if (dkim_disable_verify ||
+      !id || !dkim_verify_ctx) return;
+  /* Find signature to run ACL on */
+  while (sig != NULL) {
+    uschar *cmp_val = NULL;
+    if (Ustrchr(id,'@') != NULL) cmp_val = (uschar *)sig->identity;
+                            else cmp_val = (uschar *)sig->domain;
+    if (cmp_val && (strcmpic(cmp_val,id) == 0)) {
+      dkim_cur_sig = sig;
+      /* The "dkim_domain" and "dkim_selector" expansion variables have
+         related globals, since they are used in the signing code too.
+         Instead of inventing separate names for verification, we set
+         them here. This is easy since a domain and selector is guaranteed
+         to be in a signature. The other dkim_* expansion items are
+         dynamically fetched from dkim_cur_sig at expansion time (see
+         function below). */
+      dkim_signing_domain   = (uschar *)sig->domain;
+      dkim_signing_selector = (uschar *)sig->selector;
+      return;
+    }
+    sig = sig->next;
+  }
+}
+
+
+uschar *dkim_exim_expand_query(int what) {
+
+  if (!dkim_verify_ctx ||
+      dkim_disable_verify ||
+      !dkim_cur_sig) return dkim_exim_expand_defaults(what);
+
+  switch(what) {
+    case DKIM_ALGO:
+      switch(dkim_cur_sig->algo) {
+        case PDKIM_ALGO_RSA_SHA1:
+          return US"rsa-sha1";
+        case PDKIM_ALGO_RSA_SHA256:
+        default:
+          return US"rsa-sha256";
+      }
+    case DKIM_BODYLENGTH:
+      return (dkim_cur_sig->bodylength >= 0)?
+              (uschar *)string_sprintf(OFF_T_FMT,(LONGLONG_T)dkim_cur_sig->bodylength)
+              :dkim_exim_expand_defaults(what);
+    case DKIM_CANON_BODY:
+      switch(dkim_cur_sig->canon_body) {
+        case PDKIM_CANON_RELAXED:
+          return US"relaxed";
+        case PDKIM_CANON_SIMPLE:
+        default:
+          return US"simple";
+      }
+    case DKIM_CANON_HEADERS:
+      switch(dkim_cur_sig->canon_headers) {
+        case PDKIM_CANON_RELAXED:
+          return US"relaxed";
+        case PDKIM_CANON_SIMPLE:
+        default:
+          return US"simple";
+      }
+    case DKIM_COPIEDHEADERS:
+      return dkim_cur_sig->copiedheaders?
+              (uschar *)(dkim_cur_sig->copiedheaders)
+              :dkim_exim_expand_defaults(what);
+    case DKIM_CREATED:
+      return (dkim_cur_sig->created > 0)?
+              (uschar *)string_sprintf("%llu",dkim_cur_sig->created)
+              :dkim_exim_expand_defaults(what);
+    case DKIM_EXPIRES:
+      return (dkim_cur_sig->expires > 0)?
+              (uschar *)string_sprintf("%llu",dkim_cur_sig->expires)
+              :dkim_exim_expand_defaults(what);
+    case DKIM_HEADERNAMES:
+      return dkim_cur_sig->headernames?
+              (uschar *)(dkim_cur_sig->headernames)
+              :dkim_exim_expand_defaults(what);
+    case DKIM_IDENTITY:
+      return dkim_cur_sig->identity?
+              (uschar *)(dkim_cur_sig->identity)
+              :dkim_exim_expand_defaults(what);
+    case DKIM_KEY_GRANULARITY:
+      return dkim_cur_sig->pubkey?
+              (dkim_cur_sig->pubkey->granularity?
+                (uschar *)(dkim_cur_sig->pubkey->granularity)
+                :dkim_exim_expand_defaults(what)
+              )
+              :dkim_exim_expand_defaults(what);
+    case DKIM_KEY_SRVTYPE:
+      return dkim_cur_sig->pubkey?
+              (dkim_cur_sig->pubkey->srvtype?
+                (uschar *)(dkim_cur_sig->pubkey->srvtype)
+                :dkim_exim_expand_defaults(what)
+              )
+              :dkim_exim_expand_defaults(what);
+    case DKIM_KEY_NOTES:
+      return dkim_cur_sig->pubkey?
+              (dkim_cur_sig->pubkey->notes?
+                (uschar *)(dkim_cur_sig->pubkey->notes)
+                :dkim_exim_expand_defaults(what)
+              )
+              :dkim_exim_expand_defaults(what);
+    case DKIM_KEY_TESTING:
+      return dkim_cur_sig->pubkey?
+              (dkim_cur_sig->pubkey->testing?
+                US"1"
+                :dkim_exim_expand_defaults(what)
+              )
+              :dkim_exim_expand_defaults(what);
+    case DKIM_NOSUBDOMAINS:
+      return dkim_cur_sig->pubkey?
+              (dkim_cur_sig->pubkey->no_subdomaining?
+                US"1"
+                :dkim_exim_expand_defaults(what)
+              )
+              :dkim_exim_expand_defaults(what);
+    case DKIM_VERIFY_STATUS:
+      switch(dkim_cur_sig->verify_status) {
+        case PDKIM_VERIFY_INVALID:
+          return US"invalid";
+        case PDKIM_VERIFY_FAIL:
+          return US"fail";
+        case PDKIM_VERIFY_PASS:
+          return US"pass";
+        case PDKIM_VERIFY_NONE:
+        default:
+          return US"none";
+      }
+    case DKIM_VERIFY_REASON:
+      switch (dkim_cur_sig->verify_ext_status) {
+        case PDKIM_VERIFY_INVALID_PUBKEY_UNAVAILABLE:
+          return US"pubkey_unavailable";
+        case PDKIM_VERIFY_INVALID_PUBKEY_PARSING:
+          return US"pubkey_syntax";
+        case PDKIM_VERIFY_FAIL_BODY:
+          return US"bodyhash_mismatch";
+        case PDKIM_VERIFY_FAIL_MESSAGE:
+          return US"signature_incorrect";
+      }
+    default:
+      return US"";
+  }
+}
+
+
+uschar *dkim_exim_expand_defaults(int what) {
+  switch(what) {
+    case DKIM_ALGO:               return US"";
+    case DKIM_BODYLENGTH:         return US"9999999999999";
+    case DKIM_CANON_BODY:         return US"";
+    case DKIM_CANON_HEADERS:      return US"";
+    case DKIM_COPIEDHEADERS:      return US"";
+    case DKIM_CREATED:            return US"0";
+    case DKIM_EXPIRES:            return US"9999999999999";
+    case DKIM_HEADERNAMES:        return US"";
+    case DKIM_IDENTITY:           return US"";
+    case DKIM_KEY_GRANULARITY:    return US"*";
+    case DKIM_KEY_SRVTYPE:        return US"*";
+    case DKIM_KEY_NOTES:          return US"";
+    case DKIM_KEY_TESTING:        return US"0";
+    case DKIM_NOSUBDOMAINS:       return US"0";
+    case DKIM_VERIFY_STATUS:      return US"none";
+    case DKIM_VERIFY_REASON:      return US"";
+    default:                      return US"";
+  }
+}
+
+
+uschar *dkim_exim_sign(int dkim_fd,
+                       uschar *dkim_private_key,
+                       uschar *dkim_domain,
+                       uschar *dkim_selector,
+                       uschar *dkim_canon,
+                       uschar *dkim_sign_headers) {
+  int sep = 0;
+  uschar *seen_items = NULL;
+  int seen_items_size = 0;
+  int seen_items_offset = 0;
+  uschar itembuf[256];
+  uschar *dkim_canon_expanded;
+  uschar *dkim_sign_headers_expanded;
+  uschar *dkim_private_key_expanded;
+  pdkim_ctx *ctx = NULL;
+  uschar *rc = NULL;
+  uschar *sigbuf = NULL;
+  int sigsize = 0;
+  int sigptr = 0;
+  pdkim_signature *signature;
+  int pdkim_canon;
+  int pdkim_rc;
+  int sread;
+  char buf[4096];
+  int save_errno = 0;
+  int old_pool = store_pool;
+
+  store_pool = POOL_MAIN;
+
+  dkim_domain = expand_string(dkim_domain);
+  if (dkim_domain == NULL) {
+    /* expansion error, do not send message. */
+    log_write(0, LOG_MAIN|LOG_PANIC, "failed to expand "
+          "dkim_domain: %s", expand_string_message);
+    rc = NULL;
+    goto CLEANUP;
+  }
+
+  /* Set $dkim_domain expansion variable to each unique domain in list. */
+  while ((dkim_signing_domain = string_nextinlist(&dkim_domain, &sep,
+                                                  itembuf,
+                                                  sizeof(itembuf))) != NULL) {
+    if (!dkim_signing_domain || (dkim_signing_domain[0] == '\0')) continue;
+    /* Only sign once for each domain, no matter how often it
+       appears in the expanded list. */
+    if (seen_items != NULL) {
+      uschar *seen_items_list = seen_items;
+      if (match_isinlist(dkim_signing_domain,
+                         &seen_items_list,0,NULL,NULL,MCL_STRING,TRUE,NULL) == OK)
+        continue;
+      seen_items = string_append(seen_items,&seen_items_size,&seen_items_offset,1,":");
+    }
+    seen_items = string_append(seen_items,&seen_items_size,&seen_items_offset,1,dkim_signing_domain);
+    seen_items[seen_items_offset] = '\0';
+
+    /* Set up $dkim_selector expansion variable. */
+    dkim_signing_selector = expand_string(dkim_selector);
+    if (dkim_signing_selector == NULL) {
+      log_write(0, LOG_MAIN|LOG_PANIC, "failed to expand "
+                "dkim_selector: %s", expand_string_message);
+      rc = NULL;
+      goto CLEANUP;
+    }
+
+    /* Get canonicalization to use */
+    dkim_canon_expanded = expand_string(dkim_canon?dkim_canon:US"relaxed");
+    if (dkim_canon_expanded == NULL) {
+      /* expansion error, do not send message. */
+      log_write(0, LOG_MAIN|LOG_PANIC, "failed to expand "
+                "dkim_canon: %s", expand_string_message);
+      rc = NULL;
+      goto CLEANUP;
+    }
+    if (Ustrcmp(dkim_canon_expanded, "relaxed") == 0)
+      pdkim_canon = PDKIM_CANON_RELAXED;
+    else if (Ustrcmp(dkim_canon_expanded, "simple") == 0)
+      pdkim_canon = PDKIM_CANON_SIMPLE;
+    else {
+      log_write(0, LOG_MAIN, "DKIM: unknown canonicalization method '%s', defaulting to 'relaxed'.\n",dkim_canon_expanded);
+      pdkim_canon = PDKIM_CANON_RELAXED;
+    }
+
+    if (dkim_sign_headers) {
+      dkim_sign_headers_expanded = expand_string(dkim_sign_headers);
+      if (dkim_sign_headers_expanded == NULL) {
+        log_write(0, LOG_MAIN|LOG_PANIC, "failed to expand "
+                  "dkim_sign_headers: %s", expand_string_message);
+        rc = NULL;
+        goto CLEANUP;
+      }
+    }
+    else {
+      /* pass NULL, which means default header list */
+      dkim_sign_headers_expanded = NULL;
+    }
+
+    /* Get private key to use. */
+    dkim_private_key_expanded = expand_string(dkim_private_key);
+    if (dkim_private_key_expanded == NULL) {
+      log_write(0, LOG_MAIN|LOG_PANIC, "failed to expand "
+                "dkim_private_key: %s", expand_string_message);
+      rc = NULL;
+      goto CLEANUP;
+    }
+    if ( (Ustrlen(dkim_private_key_expanded) == 0) ||
+         (Ustrcmp(dkim_private_key_expanded,"0") == 0) ||
+         (Ustrcmp(dkim_private_key_expanded,"false") == 0) ) {
+      /* don't sign, but no error */
+      continue;
+    }
+
+    if (dkim_private_key_expanded[0] == '/') {
+      int privkey_fd = 0;
+      /* Looks like a filename, load the private key. */
+      memset(big_buffer,0,big_buffer_size);
+      privkey_fd = open(CS dkim_private_key_expanded,O_RDONLY);
+      if (privkey_fd < 0) {
+        log_write(0, LOG_MAIN|LOG_PANIC, "unable to open "
+                  "private key file for reading: %s", dkim_private_key_expanded);
+        rc = NULL;
+        goto CLEANUP;
+      }
+      if (read(privkey_fd,big_buffer,(big_buffer_size-2)) < 0) {
+        log_write(0, LOG_MAIN|LOG_PANIC, "unable to read private key file: %s",
+         dkim_private_key_expanded);
+        rc = NULL;
+        goto CLEANUP;
+      }
+      (void)close(privkey_fd);
+      dkim_private_key_expanded = big_buffer;
+    }
+
+    ctx = pdkim_init_sign(PDKIM_INPUT_SMTP,
+                          (char *)dkim_signing_domain,
+                          (char *)dkim_signing_selector,
+                          (char *)dkim_private_key_expanded
+                         );
+
+    pdkim_set_debug_stream(ctx,debug_file);
+
+    pdkim_set_optional(ctx,
+                       (char *)dkim_sign_headers_expanded,
+                       NULL,
+                       pdkim_canon,
+                       pdkim_canon,
+                       -1,
+                       PDKIM_ALGO_RSA_SHA256,
+                       0,
+                       0);
+
+    lseek(dkim_fd, 0, SEEK_SET);
+    while((sread = read(dkim_fd,&buf,4096)) > 0) {
+      if (pdkim_feed(ctx,buf,sread) != PDKIM_OK) {
+        rc = NULL;
+        goto CLEANUP;
+      }
+    }
+    /* Handle failed read above. */
+    if (sread == -1) {
+      debug_printf("DKIM: Error reading -K file.\n");
+      save_errno = errno;
+      rc = NULL;
+      goto CLEANUP;
+    }
+
+    pdkim_rc = pdkim_feed_finish(ctx,&signature);
+    if (pdkim_rc != PDKIM_OK) {
+      log_write(0, LOG_MAIN|LOG_PANIC, "DKIM: signing failed (RC %d)", pdkim_rc);
+      rc = NULL;
+      goto CLEANUP;
+    }
+
+    sigbuf = string_append(sigbuf, &sigsize, &sigptr, 2,
+                           US signature->signature_header,
+                           US"\r\n");
+
+    pdkim_free_ctx(ctx);
+    ctx = NULL;
+  }
+
+  if (sigbuf != NULL) {
+    sigbuf[sigptr] = '\0';
+    rc = sigbuf;
+  } else
+    rc = US"";
+
+  CLEANUP:
+  if (ctx != NULL)
+    pdkim_free_ctx(ctx);
+  store_pool = old_pool;
+  errno = save_errno;
+  return rc;
+}
+
+#endif
diff --git a/src/dkim.h b/src/dkim.h
new file mode 100644 (file)
index 0000000..15a7e60
--- /dev/null
@@ -0,0 +1,31 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge, 1995 - 2007 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+uschar *dkim_exim_sign(int,uschar *,uschar *,uschar *,uschar *,uschar *);
+void    dkim_exim_verify_init(void);
+void    dkim_exim_verify_feed(uschar *, int);
+void    dkim_exim_verify_finish(void);
+void    dkim_exim_acl_setup(uschar *);
+uschar *dkim_exim_expand_query(int);
+uschar *dkim_exim_expand_defaults(int);
+
+#define DKIM_ALGO               1
+#define DKIM_BODYLENGTH         2
+#define DKIM_CANON_BODY         3
+#define DKIM_CANON_HEADERS      4
+#define DKIM_COPIEDHEADERS      5
+#define DKIM_CREATED            6
+#define DKIM_EXPIRES            7
+#define DKIM_HEADERNAMES        8
+#define DKIM_IDENTITY           9
+#define DKIM_KEY_GRANULARITY   10
+#define DKIM_KEY_SRVTYPE       11
+#define DKIM_KEY_NOTES         12
+#define DKIM_KEY_TESTING       13
+#define DKIM_NOSUBDOMAINS      14
+#define DKIM_VERIFY_STATUS     15
+#define DKIM_VERIFY_REASON     16
diff --git a/src/dmarc.c b/src/dmarc.c
new file mode 100644 (file)
index 0000000..769e4d4
--- /dev/null
@@ -0,0 +1,641 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+/* Experimental DMARC support.
+   Copyright (c) Todd Lyons <tlyons@exim.org> 2012 - 2014
+   License: GPL */
+
+/* Portions Copyright (c) 2012, 2013, The Trusted Domain Project;
+   All rights reserved, licensed for use per LICENSE.opendmarc. */
+
+/* Code for calling dmarc checks via libopendmarc. Called from acl.c. */
+
+#include "exim.h"
+#ifdef EXPERIMENTAL_DMARC
+# if !defined EXPERIMENTAL_SPF
+#  error SPF must also be enabled for DMARC
+# elif defined DISABLE_DKIM
+#  error DKIM must also be enabled for DMARC
+# else
+
+#  include "functions.h"
+#  include "dmarc.h"
+#  include "pdkim/pdkim.h"
+
+OPENDMARC_LIB_T     dmarc_ctx;
+DMARC_POLICY_T     *dmarc_pctx = NULL;
+OPENDMARC_STATUS_T  libdm_status, action, dmarc_policy;
+OPENDMARC_STATUS_T  da, sa, action;
+BOOL dmarc_abort  = FALSE;
+uschar *dmarc_pass_fail = US"skipped";
+extern pdkim_signature  *dkim_signatures;
+header_line *from_header   = NULL;
+extern SPF_response_t   *spf_response;
+int dmarc_spf_ares_result  = 0;
+uschar *spf_sender_domain  = NULL;
+uschar *spf_human_readable = NULL;
+u_char *header_from_sender = NULL;
+int history_file_status    = DMARC_HIST_OK;
+uschar *dkim_history_buffer= NULL;
+
+typedef struct dmarc_exim_p {
+  uschar *name;
+  int    value;
+} dmarc_exim_p;
+
+static dmarc_exim_p dmarc_policy_description[] = {
+  { US"",           DMARC_RECORD_P_UNSPECIFIED },
+  { US"none",       DMARC_RECORD_P_NONE },
+  { US"quarantine", DMARC_RECORD_P_QUARANTINE },
+  { US"reject",     DMARC_RECORD_P_REJECT },
+  { NULL,           0 }
+};
+/* Accept an error_block struct, initialize if empty, parse to the
+ * end, and append the two strings passed to it.  Used for adding
+ * variable amounts of value:pair data to the forensic emails. */
+
+static error_block *
+add_to_eblock(error_block *eblock, uschar *t1, uschar *t2)
+{
+error_block *eb = malloc(sizeof(error_block));
+if (eblock == NULL)
+  eblock = eb;
+else
+  {
+  /* Find the end of the eblock struct and point it at eb */
+  error_block *tmp = eblock;
+  while(tmp->next != NULL)
+    tmp = tmp->next;
+  tmp->next = eb;
+  }
+eb->text1 = t1;
+eb->text2 = t2;
+eb->next  = NULL;
+return eblock;
+}
+
+/* dmarc_init sets up a context that can be re-used for several
+   messages on the same SMTP connection (that come from the
+   same host with the same HELO string) */
+
+int dmarc_init()
+{
+int *netmask   = NULL;   /* Ignored */
+int is_ipv6    = 0;
+char *tld_file = (dmarc_tld_file == NULL) ?
+                "/etc/exim/opendmarc.tlds" :
+                (char *)dmarc_tld_file;
+
+/* Set some sane defaults.  Also clears previous results when
+ * multiple messages in one connection. */
+dmarc_pctx         = NULL;
+dmarc_status       = US"none";
+dmarc_abort        = FALSE;
+dmarc_pass_fail    = US"skipped";
+dmarc_used_domain  = US"";
+dmarc_ar_header    = NULL;
+dmarc_has_been_checked = FALSE;
+header_from_sender = NULL;
+spf_sender_domain  = NULL;
+spf_human_readable = NULL;
+
+/* ACLs have "control=dmarc_disable_verify" */
+if (dmarc_disable_verify == TRUE)
+  return OK;
+
+(void) memset(&dmarc_ctx, '\0', sizeof dmarc_ctx);
+dmarc_ctx.nscount = 0;
+libdm_status = opendmarc_policy_library_init(&dmarc_ctx);
+if (libdm_status != DMARC_PARSE_OKAY)
+  {
+  log_write(0, LOG_MAIN|LOG_PANIC, "DMARC failure to init library: %s",
+                      opendmarc_policy_status_to_str(libdm_status));
+  dmarc_abort = TRUE;
+  }
+if (dmarc_tld_file == NULL)
+  dmarc_abort = TRUE;
+else if (opendmarc_tld_read_file(tld_file, NULL, NULL, NULL))
+  {
+  log_write(0, LOG_MAIN|LOG_PANIC, "DMARC failure to load tld list %s: %d",
+                      tld_file, errno);
+  dmarc_abort = TRUE;
+  }
+if (sender_host_address == NULL)
+  dmarc_abort = TRUE;
+/* This catches locally originated email and startup errors above. */
+if (!dmarc_abort)
+  {
+  is_ipv6 = string_is_ip_address(sender_host_address, netmask) == 6;
+  dmarc_pctx = opendmarc_policy_connect_init(sender_host_address, is_ipv6);
+  if (dmarc_pctx == NULL)
+    {
+    log_write(0, LOG_MAIN|LOG_PANIC,
+      "DMARC failure creating policy context: ip=%s", sender_host_address);
+    dmarc_abort = TRUE;
+    }
+  }
+
+return OK;
+}
+
+
+/* dmarc_store_data stores the header data so that subsequent
+ * dmarc_process can access the data */
+
+int dmarc_store_data(header_line *hdr) {
+  /* No debug output because would change every test debug output */
+  if (dmarc_disable_verify != TRUE)
+    from_header = hdr;
+  return OK;
+}
+
+
+/* dmarc_process adds the envelope sender address to the existing
+   context (if any), retrieves the result, sets up expansion
+   strings and evaluates the condition outcome. */
+
+int
+dmarc_process()
+{
+int sr, origin;             /* used in SPF section */
+int dmarc_spf_result  = 0;  /* stores spf into dmarc conn ctx */
+int tmp_ans, c;
+pdkim_signature *sig  = NULL;
+BOOL has_dmarc_record = TRUE;
+u_char **ruf; /* forensic report addressees, if called for */
+
+/* ACLs have "control=dmarc_disable_verify" */
+if (dmarc_disable_verify)
+  {
+  dmarc_ar_header = dmarc_auth_results_header(from_header, NULL);
+  return OK;
+  }
+
+/* Store the header From: sender domain for this part of DMARC.
+ * If there is no from_header struct, then it's likely this message
+ * is locally generated and relying on fixups to add it.  Just skip
+ * the entire DMARC system if we can't find a From: header....or if
+ * there was a previous error.
+ */
+if (!from_header || dmarc_abort)
+  dmarc_abort = TRUE;
+else
+  {
+    uschar * errormsg;
+    int dummy, domain;
+    uschar * p;
+    uschar saveend;
+
+    parse_allow_group = TRUE;
+    p = parse_find_address_end(from_header->text, FALSE);
+    saveend = *p; *p = '\0';
+    if ((header_from_sender = parse_extract_address(from_header->text, &errormsg,
+                                &dummy, &dummy, &domain, FALSE)))
+      header_from_sender += domain;
+    *p = saveend;
+
+    /* The opendmarc library extracts the domain from the email address, but
+     * only try to store it if it's not empty.  Otherwise, skip out of DMARC. */
+    if (!header_from_sender || (strcmp( CCS header_from_sender, "") == 0))
+      dmarc_abort = TRUE;
+    libdm_status = dmarc_abort ?
+      DMARC_PARSE_OKAY :
+      opendmarc_policy_store_from_domain(dmarc_pctx, header_from_sender);
+    if (libdm_status != DMARC_PARSE_OKAY)
+    {
+    log_write(0, LOG_MAIN|LOG_PANIC,
+             "failure to store header From: in DMARC: %s, header was '%s'",
+             opendmarc_policy_status_to_str(libdm_status), from_header->text);
+    dmarc_abort = TRUE;
+    }
+  }
+
+/* Skip DMARC if connection is SMTP Auth. Temporarily, admin should
+ * instead do this in the ACLs.  */
+if (!dmarc_abort && !sender_host_authenticated)
+  {
+  /* Use the envelope sender domain for this part of DMARC */
+  spf_sender_domain = expand_string(US"$sender_address_domain");
+  if (!spf_response)
+    {
+    /* No spf data means null envelope sender so generate a domain name
+     * from the sender_helo_name  */
+    if (!spf_sender_domain)
+      {
+      spf_sender_domain = sender_helo_name;
+      log_write(0, LOG_MAIN, "DMARC using synthesized SPF sender domain = %s\n",
+                            spf_sender_domain);
+      DEBUG(D_receive)
+       debug_printf("DMARC using synthesized SPF sender domain = %s\n",
+         spf_sender_domain);
+      }
+    dmarc_spf_result = DMARC_POLICY_SPF_OUTCOME_NONE;
+    dmarc_spf_ares_result = ARES_RESULT_UNKNOWN;
+    origin = DMARC_POLICY_SPF_ORIGIN_HELO;
+    spf_human_readable = US"";
+    }
+  else
+    {
+    sr = spf_response->result;
+    dmarc_spf_result = sr == SPF_RESULT_NEUTRAL  ? DMARC_POLICY_SPF_OUTCOME_NONE :
+                      sr == SPF_RESULT_PASS     ? DMARC_POLICY_SPF_OUTCOME_PASS :
+                      sr == SPF_RESULT_FAIL     ? DMARC_POLICY_SPF_OUTCOME_FAIL :
+                      sr == SPF_RESULT_SOFTFAIL ? DMARC_POLICY_SPF_OUTCOME_TMPFAIL :
+                      DMARC_POLICY_SPF_OUTCOME_NONE;
+    dmarc_spf_ares_result = sr == SPF_RESULT_NEUTRAL   ? ARES_RESULT_NEUTRAL :
+                           sr == SPF_RESULT_PASS      ? ARES_RESULT_PASS :
+                           sr == SPF_RESULT_FAIL      ? ARES_RESULT_FAIL :
+                           sr == SPF_RESULT_SOFTFAIL  ? ARES_RESULT_SOFTFAIL :
+                           sr == SPF_RESULT_NONE      ? ARES_RESULT_NONE :
+                           sr == SPF_RESULT_TEMPERROR ? ARES_RESULT_TEMPERROR :
+                           sr == SPF_RESULT_PERMERROR ? ARES_RESULT_PERMERROR :
+                           ARES_RESULT_UNKNOWN;
+    origin = DMARC_POLICY_SPF_ORIGIN_MAILFROM;
+    spf_human_readable = (uschar *)spf_response->header_comment;
+    DEBUG(D_receive)
+      debug_printf("DMARC using SPF sender domain = %s\n", spf_sender_domain);
+    }
+  if (strcmp( CCS spf_sender_domain, "") == 0)
+    dmarc_abort = TRUE;
+  if (!dmarc_abort)
+    {
+    libdm_status = opendmarc_policy_store_spf(dmarc_pctx, spf_sender_domain,
+                               dmarc_spf_result, origin, spf_human_readable);
+    if (libdm_status != DMARC_PARSE_OKAY)
+      log_write(0, LOG_MAIN|LOG_PANIC, "failure to store spf for DMARC: %s",
+                          opendmarc_policy_status_to_str(libdm_status));
+    }
+
+  /* Now we cycle through the dkim signature results and put into
+   * the opendmarc context, further building the DMARC reply.  */
+  sig = dkim_signatures;
+  dkim_history_buffer = US"";
+  while (sig)
+    {
+    int dkim_result, dkim_ares_result, vs, ves;
+    vs  = sig->verify_status;
+    ves = sig->verify_ext_status;
+    dkim_result = vs == PDKIM_VERIFY_PASS ? DMARC_POLICY_DKIM_OUTCOME_PASS :
+                 vs == PDKIM_VERIFY_FAIL ? DMARC_POLICY_DKIM_OUTCOME_FAIL :
+                 vs == PDKIM_VERIFY_INVALID ? DMARC_POLICY_DKIM_OUTCOME_TMPFAIL :
+                 DMARC_POLICY_DKIM_OUTCOME_NONE;
+    libdm_status = opendmarc_policy_store_dkim(dmarc_pctx, (uschar *)sig->domain,
+                                              dkim_result, US"");
+    DEBUG(D_receive)
+      debug_printf("DMARC adding DKIM sender domain = %s\n", sig->domain);
+    if (libdm_status != DMARC_PARSE_OKAY)
+      log_write(0, LOG_MAIN|LOG_PANIC,
+               "failure to store dkim (%s) for DMARC: %s",
+               sig->domain, opendmarc_policy_status_to_str(libdm_status));
+
+    dkim_ares_result =
+      vs == PDKIM_VERIFY_PASS    ? ARES_RESULT_PASS :
+      vs == PDKIM_VERIFY_FAIL    ? ARES_RESULT_FAIL :
+      vs == PDKIM_VERIFY_NONE    ? ARES_RESULT_NONE :
+      vs == PDKIM_VERIFY_INVALID ?
+       ves == PDKIM_VERIFY_INVALID_PUBKEY_UNAVAILABLE ? ARES_RESULT_PERMERROR :
+       ves == PDKIM_VERIFY_INVALID_BUFFER_SIZE        ? ARES_RESULT_PERMERROR :
+       ves == PDKIM_VERIFY_INVALID_PUBKEY_PARSING     ? ARES_RESULT_PERMERROR :
+       ARES_RESULT_UNKNOWN :
+      ARES_RESULT_UNKNOWN;
+    dkim_history_buffer = string_sprintf("%sdkim %s %d\n", dkim_history_buffer,
+                                        sig->domain, dkim_ares_result);
+    sig = sig->next;
+    }
+  libdm_status = opendmarc_policy_query_dmarc(dmarc_pctx, US"");
+  switch (libdm_status)
+    {
+    case DMARC_DNS_ERROR_NXDOMAIN:
+    case DMARC_DNS_ERROR_NO_RECORD:
+      DEBUG(D_receive)
+       debug_printf("DMARC no record found for %s\n", header_from_sender);
+      has_dmarc_record = FALSE;
+      break;
+    case DMARC_PARSE_OKAY:
+      DEBUG(D_receive)
+       debug_printf("DMARC record found for %s\n", header_from_sender);
+      break;
+    case DMARC_PARSE_ERROR_BAD_VALUE:
+      DEBUG(D_receive)
+       debug_printf("DMARC record parse error for %s\n", header_from_sender);
+      has_dmarc_record = FALSE;
+      break;
+    default:
+      /* everything else, skip dmarc */
+      DEBUG(D_receive)
+       debug_printf("DMARC skipping (%d), unsure what to do with %s",
+                     libdm_status, from_header->text);
+      has_dmarc_record = FALSE;
+      break;
+    }
+
+/* Store the policy string in an expandable variable. */
+
+  libdm_status = opendmarc_policy_fetch_p(dmarc_pctx, &tmp_ans);
+  for (c = 0; dmarc_policy_description[c].name; c++)
+    if (tmp_ans == dmarc_policy_description[c].value)
+      {
+      dmarc_domain_policy = string_sprintf("%s",dmarc_policy_description[c].name);
+      break;
+      }
+
+  /* Can't use exim's string manipulation functions so allocate memory
+   * for libopendmarc using its max hostname length definition. */
+
+  uschar *dmarc_domain = (uschar *)calloc(DMARC_MAXHOSTNAMELEN, sizeof(uschar));
+  libdm_status = opendmarc_policy_fetch_utilized_domain(dmarc_pctx,
+    dmarc_domain, DMARC_MAXHOSTNAMELEN-1);
+  dmarc_used_domain = string_copy(dmarc_domain);
+  free(dmarc_domain);
+
+  if (libdm_status != DMARC_PARSE_OKAY)
+    log_write(0, LOG_MAIN|LOG_PANIC,
+      "failure to read domainname used for DMARC lookup: %s",
+      opendmarc_policy_status_to_str(libdm_status));
+
+  libdm_status = opendmarc_get_policy_to_enforce(dmarc_pctx);
+  dmarc_policy = libdm_status;
+  switch(libdm_status)
+    {
+    case DMARC_POLICY_ABSENT:     /* No DMARC record found */
+      dmarc_status = US"norecord";
+      dmarc_pass_fail = US"none";
+      dmarc_status_text = US"No DMARC record";
+      action = DMARC_RESULT_ACCEPT;
+      break;
+    case DMARC_FROM_DOMAIN_ABSENT:    /* No From: domain */
+      dmarc_status = US"nofrom";
+      dmarc_pass_fail = US"temperror";
+      dmarc_status_text = US"No From: domain found";
+      action = DMARC_RESULT_ACCEPT;
+      break;
+    case DMARC_POLICY_NONE:       /* Accept and report */
+      dmarc_status = US"none";
+      dmarc_pass_fail = US"none";
+      dmarc_status_text = US"None, Accept";
+      action = DMARC_RESULT_ACCEPT;
+      break;
+    case DMARC_POLICY_PASS:       /* Explicit accept */
+      dmarc_status = US"accept";
+      dmarc_pass_fail = US"pass";
+      dmarc_status_text = US"Accept";
+      action = DMARC_RESULT_ACCEPT;
+      break;
+    case DMARC_POLICY_REJECT:       /* Explicit reject */
+      dmarc_status = US"reject";
+      dmarc_pass_fail = US"fail";
+      dmarc_status_text = US"Reject";
+      action = DMARC_RESULT_REJECT;
+      break;
+    case DMARC_POLICY_QUARANTINE:       /* Explicit quarantine */
+      dmarc_status = US"quarantine";
+      dmarc_pass_fail = US"fail";
+      dmarc_status_text = US"Quarantine";
+      action = DMARC_RESULT_QUARANTINE;
+      break;
+    default:
+      dmarc_status = US"temperror";
+      dmarc_pass_fail = US"temperror";
+      dmarc_status_text = US"Internal Policy Error";
+      action = DMARC_RESULT_TEMPFAIL;
+      break;
+    }
+
+  libdm_status = opendmarc_policy_fetch_alignment(dmarc_pctx, &da, &sa);
+  if (libdm_status != DMARC_PARSE_OKAY)
+    log_write(0, LOG_MAIN|LOG_PANIC, "failure to read DMARC alignment: %s",
+                            opendmarc_policy_status_to_str(libdm_status));
+
+  if (has_dmarc_record == TRUE)
+    {
+    log_write(0, LOG_MAIN, "DMARC results: spf_domain=%s dmarc_domain=%s "
+                          "spf_align=%s dkim_align=%s enforcement='%s'",
+                          spf_sender_domain, dmarc_used_domain,
+                          (sa==DMARC_POLICY_SPF_ALIGNMENT_PASS) ?"yes":"no",
+                          (da==DMARC_POLICY_DKIM_ALIGNMENT_PASS)?"yes":"no",
+                          dmarc_status_text);
+    history_file_status = dmarc_write_history_file();
+    /* Now get the forensic reporting addresses, if any */
+    ruf = opendmarc_policy_fetch_ruf(dmarc_pctx, NULL, 0, 1);
+    dmarc_send_forensic_report(ruf);
+    }
+  }
+
+/* set some global variables here */
+dmarc_ar_header = dmarc_auth_results_header(from_header, NULL);
+
+/* shut down libopendmarc */
+if ( dmarc_pctx != NULL )
+  (void) opendmarc_policy_connect_shutdown(dmarc_pctx);
+if ( dmarc_disable_verify == FALSE )
+  (void) opendmarc_policy_library_shutdown(&dmarc_ctx);
+
+return OK;
+}
+
+int
+dmarc_write_history_file()
+{
+int history_file_fd;
+ssize_t written_len;
+int tmp_ans;
+u_char **rua; /* aggregate report addressees */
+uschar *history_buffer = NULL;
+
+if (!dmarc_history_file)
+  return DMARC_HIST_DISABLED;
+history_file_fd = log_create(dmarc_history_file);
+
+if (history_file_fd < 0)
+{
+  log_write(0, LOG_MAIN|LOG_PANIC, "failure to create DMARC history file: %s",
+                          dmarc_history_file);
+  return DMARC_HIST_FILE_ERR;
+}
+
+/* Generate the contents of the history file */
+history_buffer = string_sprintf(
+  "job %s\nreporter %s\nreceived %ld\nipaddr %s\nfrom %s\nmfrom %s\n",
+  message_id, primary_hostname, time(NULL), sender_host_address,
+  header_from_sender, expand_string(US"$sender_address_domain"));
+
+if (spf_response)
+  history_buffer = string_sprintf("%sspf %d\n", history_buffer, dmarc_spf_ares_result);
+  /* history_buffer = string_sprintf("%sspf -1\n", history_buffer); */
+
+history_buffer = string_sprintf(
+  "%s%spdomain %s\npolicy %d\n",
+  history_buffer, dkim_history_buffer, dmarc_used_domain, dmarc_policy);
+
+if ((rua = opendmarc_policy_fetch_rua(dmarc_pctx, NULL, 0, 1)))
+  for (tmp_ans = 0; rua[tmp_ans]; tmp_ans++)
+    history_buffer = string_sprintf("%srua %s\n", history_buffer, rua[tmp_ans]);
+else
+  history_buffer = string_sprintf("%srua -\n", history_buffer);
+
+opendmarc_policy_fetch_pct(dmarc_pctx, &tmp_ans);
+history_buffer = string_sprintf("%spct %d\n", history_buffer, tmp_ans);
+
+opendmarc_policy_fetch_adkim(dmarc_pctx, &tmp_ans);
+history_buffer = string_sprintf("%sadkim %d\n", history_buffer, tmp_ans);
+
+opendmarc_policy_fetch_aspf(dmarc_pctx, &tmp_ans);
+history_buffer = string_sprintf("%saspf %d\n", history_buffer, tmp_ans);
+
+opendmarc_policy_fetch_p(dmarc_pctx, &tmp_ans);
+history_buffer = string_sprintf("%sp %d\n", history_buffer, tmp_ans);
+
+opendmarc_policy_fetch_sp(dmarc_pctx, &tmp_ans);
+history_buffer = string_sprintf("%ssp %d\n", history_buffer, tmp_ans);
+
+history_buffer = string_sprintf(
+  "%salign_dkim %d\nalign_spf %d\naction %d\n",
+  history_buffer, da, sa, action);
+
+/* Write the contents to the history file */
+DEBUG(D_receive)
+  debug_printf("DMARC logging history data for opendmarc reporting%s\n",
+            (host_checking || running_in_test_harness) ? " (not really)" : "");
+if (host_checking || running_in_test_harness)
+  {
+  DEBUG(D_receive)
+    debug_printf("DMARC history data for debugging:\n%s", history_buffer);
+  }
+else
+  {
+  written_len = write_to_fd_buf(history_file_fd,
+                               history_buffer,
+                               Ustrlen(history_buffer));
+  if (written_len == 0)
+    {
+    log_write(0, LOG_MAIN|LOG_PANIC, "failure to write to DMARC history file: %s",
+                          dmarc_history_file);
+    return DMARC_HIST_WRITE_ERR;
+    }
+  (void)close(history_file_fd);
+  }
+return DMARC_HIST_OK;
+}
+
+void
+dmarc_send_forensic_report(u_char **ruf)
+{
+int   c;
+uschar *recipient, *save_sender;
+BOOL  send_status = FALSE;
+error_block *eblock = NULL;
+FILE *message_file = NULL;
+
+/* Earlier ACL does not have *required* control=dmarc_enable_forensic */
+if (!dmarc_enable_forensic)
+  return;
+
+if ((dmarc_policy == DMARC_POLICY_REJECT     && action == DMARC_RESULT_REJECT) ||
+    (dmarc_policy == DMARC_POLICY_QUARANTINE && action == DMARC_RESULT_QUARANTINE) )
+  if (ruf)
+    {
+    eblock = add_to_eblock(eblock, US"Sender Domain", dmarc_used_domain);
+    eblock = add_to_eblock(eblock, US"Sender IP Address", sender_host_address);
+    eblock = add_to_eblock(eblock, US"Received Date", tod_stamp(tod_full));
+    eblock = add_to_eblock(eblock, US"SPF Alignment",
+                          (sa==DMARC_POLICY_SPF_ALIGNMENT_PASS) ?US"yes":US"no");
+    eblock = add_to_eblock(eblock, US"DKIM Alignment",
+                          (da==DMARC_POLICY_DKIM_ALIGNMENT_PASS)?US"yes":US"no");
+    eblock = add_to_eblock(eblock, US"DMARC Results", dmarc_status_text);
+    /* Set a sane default envelope sender */
+    dsn_from = dmarc_forensic_sender ? dmarc_forensic_sender :
+              dsn_from ? dsn_from :
+              string_sprintf("do-not-reply@%s",primary_hostname);
+    for (c = 0; ruf[c]; c++)
+      {
+      recipient = string_copylc(ruf[c]);
+      if (Ustrncmp(recipient, "mailto:",7))
+       continue;
+      /* Move to first character past the colon */
+      recipient += 7;
+      DEBUG(D_receive)
+       debug_printf("DMARC forensic report to %s%s\n", recipient,
+            (host_checking || running_in_test_harness) ? " (not really)" : "");
+      if (host_checking || running_in_test_harness)
+       continue;
+
+      save_sender = sender_address;
+      sender_address = recipient;
+      send_status = moan_to_sender(ERRMESS_DMARC_FORENSIC, eblock,
+                                  header_list, message_file, FALSE);
+      sender_address = save_sender;
+      if (!send_status)
+       log_write(0, LOG_MAIN|LOG_PANIC,
+         "failure to send DMARC forensic report to %s", recipient);
+      }
+    }
+}
+
+uschar *
+dmarc_exim_expand_query(int what)
+{
+if (dmarc_disable_verify || !dmarc_pctx)
+  return dmarc_exim_expand_defaults(what);
+
+switch(what)
+  {
+  case DMARC_VERIFY_STATUS:
+    return(dmarc_status);
+  default:
+    return US"";
+  }
+}
+
+uschar *
+dmarc_exim_expand_defaults(int what)
+{
+switch(what)
+  {
+  case DMARC_VERIFY_STATUS:
+    return dmarc_disable_verify ?  US"off" : US"none";
+  default:
+    return US"";
+  }
+}
+
+uschar *
+dmarc_auth_results_header(header_line *from_header, uschar *hostname)
+{
+uschar *hdr_tmp    = US"";
+
+/* Allow a server hostname to be passed to this function, but is
+ * currently unused */
+if (!hostname)
+  hostname = primary_hostname;
+hdr_tmp = string_sprintf("%s %s;", DMARC_AR_HEADER, hostname);
+
+#if 0
+/* I don't think this belongs here, but left it here commented out
+ * because it was a lot of work to get working right. */
+if (spf_response != NULL) {
+  uschar *dmarc_ar_spf = US"";
+  int sr               = 0;
+  sr = spf_response->result;
+  dmarc_ar_spf = (sr == SPF_RESULT_NEUTRAL)  ? US"neutral" :
+                (sr == SPF_RESULT_PASS)     ? US"pass" :
+                (sr == SPF_RESULT_FAIL)     ? US"fail" :
+                (sr == SPF_RESULT_SOFTFAIL) ? US"softfail" :
+                US"none";
+  hdr_tmp = string_sprintf("%s spf=%s (%s) smtp.mail=%s;",
+                          hdr_tmp, dmarc_ar_spf_result,
+                          spf_response->header_comment,
+                          expand_string(US"$sender_address") );
+}
+#endif
+
+hdr_tmp = string_sprintf("%s dmarc=%s", hdr_tmp, dmarc_pass_fail);
+if (header_from_sender)
+  hdr_tmp = string_sprintf("%s header.from=%s",
+                          hdr_tmp, header_from_sender);
+return hdr_tmp;
+}
+
+# endif /* EXPERIMENTAL_SPF */
+#endif /* EXPERIMENTAL_DMARC */
+/* vi: aw ai sw=2
+ */
diff --git a/src/dmarc.h b/src/dmarc.h
new file mode 100644 (file)
index 0000000..63d451c
--- /dev/null
@@ -0,0 +1,62 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Experimental DMARC support.
+   Copyright (c) Todd Lyons <tlyons@exim.org> 2012 - 2014
+   License: GPL */
+
+/* Portions Copyright (c) 2012, 2013, The Trusted Domain Project;
+   All rights reserved, licensed for use per LICENSE.opendmarc. */
+
+#ifdef EXPERIMENTAL_DMARC
+
+# include "opendmarc/dmarc.h"
+# ifdef EXPERIMENTAL_SPF
+#  include "spf2/spf.h"
+# endif /* EXPERIMENTAL_SPF */
+
+/* prototypes */
+int dmarc_init();
+int dmarc_store_data(header_line *);
+int dmarc_process();
+uschar *dmarc_exim_expand_query(int);
+uschar *dmarc_exim_expand_defaults(int);
+uschar *dmarc_auth_results_header(header_line *,uschar *);
+int dmarc_write_history_file();
+void dmarc_send_forensic_report(u_char **);
+
+#define DMARC_AR_HEADER        US"Authentication-Results:"
+#define DMARC_VERIFY_STATUS    1
+
+#define DMARC_HIST_OK          1
+#define DMARC_HIST_DISABLED    2
+#define DMARC_HIST_EMPTY       3
+#define DMARC_HIST_FILE_ERR    4
+#define DMARC_HIST_WRITE_ERR   5
+
+/* From opendmarc.c */
+#define DMARC_RESULT_REJECT     0
+#define DMARC_RESULT_DISCARD    1
+#define DMARC_RESULT_ACCEPT     2
+#define DMARC_RESULT_TEMPFAIL   3
+#define DMARC_RESULT_QUARANTINE 4
+
+/* From opendmarc-ar.h */
+/* ARES_RESULT_T -- type for specifying an authentication result */
+#define ARES_RESULT_UNDEFINED   (-1)
+#define ARES_RESULT_PASS    0
+#define ARES_RESULT_UNUSED  1
+#define ARES_RESULT_SOFTFAIL    2
+#define ARES_RESULT_NEUTRAL 3
+#define ARES_RESULT_TEMPERROR   4
+#define ARES_RESULT_PERMERROR   5
+#define ARES_RESULT_NONE    6
+#define ARES_RESULT_FAIL    7
+#define ARES_RESULT_POLICY  8
+#define ARES_RESULT_NXDOMAIN    9
+#define ARES_RESULT_SIGNED  10
+#define ARES_RESULT_UNKNOWN 11
+#define ARES_RESULT_DISCARD 12
+
+#endif /* EXPERIMENTAL_DMARC */
diff --git a/src/dns.c b/src/dns.c
new file mode 100644 (file)
index 0000000..6efb88d
--- /dev/null
+++ b/src/dns.c
@@ -0,0 +1,1260 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2014 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Functions for interfacing with the DNS. */
+
+#include "exim.h"
+
+
+/* Function declaration needed for mutual recursion when A6 records
+are supported. */
+
+#if HAVE_IPV6
+#ifdef SUPPORT_A6
+static void dns_complete_a6(dns_address ***, dns_answer *, dns_record *,
+  int, uschar *);
+#endif
+#endif
+
+
+/*************************************************
+*               Fake DNS resolver                *
+*************************************************/
+
+/* This function is called instead of res_search() when Exim is running in its
+test harness. It recognizes some special domain names, and uses them to force
+failure and retry responses (optionally with a delay). Otherwise, it calls an
+external utility that mocks-up a nameserver, if it can find the utility.
+If not, it passes its arguments on to res_search(). The fake nameserver may
+also return a code specifying that the name should be passed on.
+
+Background: the original test suite required a real nameserver to carry the
+test zones, whereas the new test suit has the fake server for portability. This
+code supports both.
+
+Arguments:
+  domain      the domain name
+  type        the DNS record type
+  answerptr   where to put the answer
+  size        size of the answer area
+
+Returns:      length of returned data, or -1 on error (h_errno set)
+*/
+
+static int
+fakens_search(uschar *domain, int type, uschar *answerptr, int size)
+{
+int len = Ustrlen(domain);
+int asize = size;                  /* Locally modified */
+uschar *endname;
+uschar name[256];
+uschar utilname[256];
+uschar *aptr = answerptr;          /* Locally modified */
+struct stat statbuf;
+
+/* Remove terminating dot. */
+
+if (domain[len - 1] == '.') len--;
+Ustrncpy(name, domain, len);
+name[len] = 0;
+endname = name + len;
+
+/* This code, for forcing TRY_AGAIN and NO_RECOVERY, is here so that it works
+for the old test suite that uses a real nameserver. When the old test suite is
+eventually abandoned, this code could be moved into the fakens utility. */
+
+if (len >= 14 && Ustrcmp(endname - 14, "test.again.dns") == 0)
+  {
+  int delay = Uatoi(name);  /* digits at the start of the name */
+  DEBUG(D_dns) debug_printf("Return from DNS lookup of %s (%s) faked for testing\n",
+    name, dns_text_type(type));
+  if (delay > 0)
+    {
+    DEBUG(D_dns) debug_printf("delaying %d seconds\n", delay);
+    sleep(delay);
+    }
+  h_errno = TRY_AGAIN;
+  return -1;
+  }
+
+if (len >= 13 && Ustrcmp(endname - 13, "test.fail.dns") == 0)
+  {
+  DEBUG(D_dns) debug_printf("Return from DNS lookup of %s (%s) faked for testing\n",
+    name, dns_text_type(type));
+  h_errno = NO_RECOVERY;
+  return -1;
+  }
+
+/* Look for the fakens utility, and if it exists, call it. */
+
+(void)string_format(utilname, sizeof(utilname), "%s/../bin/fakens",
+  spool_directory);
+
+if (stat(CS utilname, &statbuf) >= 0)
+  {
+  pid_t pid;
+  int infd, outfd, rc;
+  uschar *argv[5];
+
+  DEBUG(D_dns) debug_printf("DNS lookup of %s (%s) using fakens\n",
+    name, dns_text_type(type));
+
+  argv[0] = utilname;
+  argv[1] = spool_directory;
+  argv[2] = name;
+  argv[3] = dns_text_type(type);
+  argv[4] = NULL;
+
+  pid = child_open(argv, NULL, 0000, &infd, &outfd, FALSE);
+  if (pid < 0)
+    log_write(0, LOG_MAIN|LOG_PANIC_DIE, "failed to run fakens: %s",
+      strerror(errno));
+
+  len = 0;
+  rc = -1;
+  while (asize > 0 && (rc = read(outfd, aptr, asize)) > 0)
+    {
+    len += rc;
+    aptr += rc;       /* Don't modify the actual arguments, because they */
+    asize -= rc;      /* may need to be passed on to res_search(). */
+    }
+
+  if (rc < 0)
+    log_write(0, LOG_MAIN|LOG_PANIC_DIE, "read from fakens failed: %s",
+      strerror(errno));
+
+  switch(child_close(pid, 0))
+    {
+    case 0: return len;
+    case 1: h_errno = HOST_NOT_FOUND; return -1;
+    case 2: h_errno = TRY_AGAIN; return -1;
+    default:
+    case 3: h_errno = NO_RECOVERY; return -1;
+    case 4: h_errno = NO_DATA; return -1;
+    case 5: /* Pass on to res_search() */
+    DEBUG(D_dns) debug_printf("fakens returned PASS_ON\n");
+    }
+  }
+
+/* fakens utility not found, or it returned "pass on" */
+
+DEBUG(D_dns) debug_printf("passing %s on to res_search()\n", domain);
+
+return res_search(CS domain, C_IN, type, answerptr, size);
+}
+
+
+
+/*************************************************
+*        Initialize and configure resolver       *
+*************************************************/
+
+/* Initialize the resolver and the storage for holding DNS answers if this is
+the first time we have been here, and set the resolver options.
+
+Arguments:
+  qualify_single    TRUE to set the RES_DEFNAMES option
+  search_parents    TRUE to set the RES_DNSRCH option
+  use_dnssec        TRUE to set the RES_USE_DNSSEC option
+
+Returns:            nothing
+*/
+
+void
+dns_init(BOOL qualify_single, BOOL search_parents, BOOL use_dnssec)
+{
+res_state resp = os_get_dns_resolver_res();
+
+if ((resp->options & RES_INIT) == 0)
+  {
+  DEBUG(D_resolver) resp->options |= RES_DEBUG;     /* For Cygwin */
+  os_put_dns_resolver_res(resp);
+  res_init();
+  DEBUG(D_resolver) resp->options |= RES_DEBUG;
+  os_put_dns_resolver_res(resp);
+  }
+
+resp->options &= ~(RES_DNSRCH | RES_DEFNAMES);
+resp->options |= (qualify_single? RES_DEFNAMES : 0) |
+                (search_parents? RES_DNSRCH : 0);
+if (dns_retrans > 0) resp->retrans = dns_retrans;
+if (dns_retry > 0) resp->retry = dns_retry;
+
+#ifdef RES_USE_EDNS0
+if (dns_use_edns0 >= 0)
+  {
+  if (dns_use_edns0)
+    resp->options |= RES_USE_EDNS0;
+  else
+    resp->options &= ~RES_USE_EDNS0;
+  DEBUG(D_resolver)
+    debug_printf("Coerced resolver EDNS0 support %s.\n",
+        dns_use_edns0 ? "on" : "off");
+  }
+#else
+if (dns_use_edns0 >= 0)
+  DEBUG(D_resolver)
+    debug_printf("Unable to %sset EDNS0 without resolver support.\n",
+        dns_use_edns0 ? "" : "un");
+#endif
+
+#ifndef DISABLE_DNSSEC
+# ifdef RES_USE_DNSSEC
+#  ifndef RES_USE_EDNS0
+#   error Have RES_USE_DNSSEC but not RES_USE_EDNS0?  Something hinky ...
+#  endif
+if (use_dnssec)
+  resp->options |= RES_USE_DNSSEC;
+if (dns_dnssec_ok >= 0)
+  {
+  if (dns_use_edns0 == 0 && dns_dnssec_ok != 0)
+    {
+    DEBUG(D_resolver)
+      debug_printf("CONFLICT: dns_use_edns0 forced false, dns_dnssec_ok forced true, ignoring latter!\n");
+    }
+  else
+    {
+    if (dns_dnssec_ok)
+      resp->options |= RES_USE_DNSSEC;
+    else
+      resp->options &= ~RES_USE_DNSSEC;
+    DEBUG(D_resolver) debug_printf("Coerced resolver DNSSEC support %s.\n",
+        dns_dnssec_ok ? "on" : "off");
+    }
+  }
+# else
+if (dns_dnssec_ok >= 0)
+  DEBUG(D_resolver)
+    debug_printf("Unable to %sset DNSSEC without resolver support.\n",
+        dns_dnssec_ok ? "" : "un");
+if (use_dnssec)
+  DEBUG(D_resolver)
+    debug_printf("Unable to set DNSSEC without resolver support.\n");
+# endif
+#endif /* DISABLE_DNSSEC */
+
+os_put_dns_resolver_res(resp);
+}
+
+
+
+/*************************************************
+*       Build key name for PTR records           *
+*************************************************/
+
+/* This function inverts an IP address and adds the relevant domain, to produce
+a name that can be used to look up PTR records.
+
+Arguments:
+  string     the IP address as a string
+  buffer     a suitable buffer, long enough to hold the result
+
+Returns:     nothing
+*/
+
+void
+dns_build_reverse(uschar *string, uschar *buffer)
+{
+uschar *p = string + Ustrlen(string);
+uschar *pp = buffer;
+
+/* Handle IPv4 address */
+
+#if HAVE_IPV6
+if (Ustrchr(string, ':') == NULL)
+#endif
+  {
+  int i;
+  for (i = 0; i < 4; i++)
+    {
+    uschar *ppp = p;
+    while (ppp > string && ppp[-1] != '.') ppp--;
+    Ustrncpy(pp, ppp, p - ppp);
+    pp += p - ppp;
+    *pp++ = '.';
+    p = ppp - 1;
+    }
+  Ustrcpy(pp, "in-addr.arpa");
+  }
+
+/* Handle IPv6 address; convert to binary so as to fill out any
+abbreviation in the textual form. */
+
+#if HAVE_IPV6
+else
+  {
+  int i;
+  int v6[4];
+  (void)host_aton(string, v6);
+
+  /* The original specification for IPv6 reverse lookup was to invert each
+  nibble, and look in the ip6.int domain. The domain was subsequently
+  changed to ip6.arpa. */
+
+  for (i = 3; i >= 0; i--)
+    {
+    int j;
+    for (j = 0; j < 32; j += 4)
+      {
+      sprintf(CS pp, "%x.", (v6[i] >> j) & 15);
+      pp += 2;
+      }
+    }
+  Ustrcpy(pp, "ip6.arpa.");
+
+  /* Another way of doing IPv6 reverse lookups was proposed in conjunction
+  with A6 records. However, it fell out of favour when they did. The
+  alternative was to construct a binary key, and look in ip6.arpa. I tried
+  to make this code do that, but I could not make it work on Solaris 8. The
+  resolver seems to lose the initial backslash somehow. However, now that
+  this style of reverse lookup has been dropped, it doesn't matter. These
+  lines are left here purely for historical interest. */
+
+  /**************************************************
+  Ustrcpy(pp, "\\[x");
+  pp += 3;
+
+  for (i = 0; i < 4; i++)
+    {
+    sprintf(pp, "%08X", v6[i]);
+    pp += 8;
+    }
+  Ustrcpy(pp, "].ip6.arpa.");
+  **************************************************/
+
+  }
+#endif
+}
+
+
+
+
+/*************************************************
+*       Get next DNS record from answer block    *
+*************************************************/
+
+/* Call this with reset == RESET_ANSWERS to scan the answer block, reset ==
+RESET_AUTHORITY to scan the authority records, reset == RESET_ADDITIONAL to
+scan the additional records, and reset == RESET_NEXT to get the next record.
+The result is in static storage which must be copied if it is to be preserved.
+
+Arguments:
+  dnsa      pointer to dns answer block
+  dnss      pointer to dns scan block
+  reset     option specifing what portion to scan, as described above
+
+Returns:    next dns record, or NULL when no more
+*/
+
+dns_record *
+dns_next_rr(dns_answer *dnsa, dns_scan *dnss, int reset)
+{
+HEADER *h = (HEADER *)dnsa->answer;
+int namelen;
+
+/* Reset the saved data when requested to, and skip to the first required RR */
+
+if (reset != RESET_NEXT)
+  {
+  dnss->rrcount = ntohs(h->qdcount);
+  dnss->aptr = dnsa->answer + sizeof(HEADER);
+
+  /* Skip over questions; failure to expand the name just gives up */
+
+  while (dnss->rrcount-- > 0)
+    {
+    namelen = dn_expand(dnsa->answer, dnsa->answer + dnsa->answerlen,
+      dnss->aptr, (DN_EXPAND_ARG4_TYPE) &(dnss->srr.name), DNS_MAXNAME);
+    if (namelen < 0) { dnss->rrcount = 0; return NULL; }
+    dnss->aptr += namelen + 4;    /* skip name & type & class */
+    }
+
+  /* Get the number of answer records. */
+
+  dnss->rrcount = ntohs(h->ancount);
+
+  /* Skip over answers if we want to look at the authority section. Also skip
+  the NS records (i.e. authority section) if wanting to look at the additional
+  records. */
+
+  if (reset == RESET_ADDITIONAL) dnss->rrcount += ntohs(h->nscount);
+
+  if (reset == RESET_AUTHORITY || reset == RESET_ADDITIONAL)
+    {
+    while (dnss->rrcount-- > 0)
+      {
+      namelen = dn_expand(dnsa->answer, dnsa->answer + dnsa->answerlen,
+        dnss->aptr, (DN_EXPAND_ARG4_TYPE) &(dnss->srr.name), DNS_MAXNAME);
+      if (namelen < 0) { dnss->rrcount = 0; return NULL; }
+      dnss->aptr += namelen + 8;            /* skip name, type, class & TTL */
+      GETSHORT(dnss->srr.size, dnss->aptr); /* size of data portion */
+      dnss->aptr += dnss->srr.size;         /* skip over it */
+      }
+    dnss->rrcount = (reset == RESET_AUTHORITY)
+      ? ntohs(h->nscount) : ntohs(h->arcount);
+    }
+  }
+
+/* The variable dnss->aptr is now pointing at the next RR, and dnss->rrcount
+contains the number of RR records left. */
+
+if (dnss->rrcount-- <= 0) return NULL;
+
+/* If expanding the RR domain name fails, behave as if no more records
+(something safe). */
+
+namelen = dn_expand(dnsa->answer, dnsa->answer + dnsa->answerlen, dnss->aptr,
+  (DN_EXPAND_ARG4_TYPE) &(dnss->srr.name), DNS_MAXNAME);
+if (namelen < 0) { dnss->rrcount = 0; return NULL; }
+
+/* Move the pointer past the name and fill in the rest of the data structure
+from the following bytes. */
+
+dnss->aptr += namelen;
+GETSHORT(dnss->srr.type, dnss->aptr); /* Record type */
+dnss->aptr += 6;                      /* Don't want class or TTL */
+GETSHORT(dnss->srr.size, dnss->aptr); /* Size of data portion */
+dnss->srr.data = dnss->aptr;          /* The record's data follows */
+dnss->aptr += dnss->srr.size;         /* Advance to next RR */
+
+/* Return a pointer to the dns_record structure within the dns_answer. This is
+for convenience so that the scans can use nice-looking for loops. */
+
+return &(dnss->srr);
+}
+
+
+
+
+/*************************************************
+*    Return whether AD bit set in DNS result     *
+*************************************************/
+
+/* We do not perform DNSSEC work ourselves; if the administrator has installed
+a verifying resolver which sets AD as appropriate, though, we'll use that.
+(AD = Authentic Data)
+
+Argument:   pointer to dns answer block
+Returns:    bool indicating presence of AD bit
+*/
+
+BOOL
+dns_is_secure(dns_answer *dnsa)
+{
+#ifdef DISABLE_DNSSEC
+DEBUG(D_dns)
+  debug_printf("DNSSEC support disabled at build-time; dns_is_secure() false\n");
+return FALSE;
+#else
+HEADER *h = (HEADER *)dnsa->answer;
+return h->ad ? TRUE : FALSE;
+#endif
+}
+
+
+
+
+/*************************************************
+*            Turn DNS type into text             *
+*************************************************/
+
+/* Turn the coded record type into a string for printing. All those that Exim
+uses should be included here.
+
+Argument:   record type
+Returns:    pointer to string
+*/
+
+uschar *
+dns_text_type(int t)
+{
+switch(t)
+  {
+  case T_A:     return US"A";
+  case T_MX:    return US"MX";
+  case T_AAAA:  return US"AAAA";
+  case T_A6:    return US"A6";
+  case T_TXT:   return US"TXT";
+  case T_SPF:   return US"SPF";
+  case T_PTR:   return US"PTR";
+  case T_SOA:   return US"SOA";
+  case T_SRV:   return US"SRV";
+  case T_NS:    return US"NS";
+  case T_CNAME: return US"CNAME";
+  case T_TLSA:  return US"TLSA";
+  default:      return US"?";
+  }
+}
+
+
+
+/*************************************************
+*        Cache a failed DNS lookup result        *
+*************************************************/
+
+/* We cache failed lookup results so as not to experience timeouts many
+times for the same domain. We need to retain the resolver options because they
+may change. For successful lookups, we rely on resolver and/or name server
+caching.
+
+Arguments:
+  name       the domain name
+  type       the lookup type
+  rc         the return code
+
+Returns:     the return code
+*/
+
+static int
+dns_return(uschar *name, int type, int rc)
+{
+res_state resp = os_get_dns_resolver_res();
+tree_node *node = store_get_perm(sizeof(tree_node) + 290);
+sprintf(CS node->name, "%.255s-%s-%lx", name, dns_text_type(type),
+  resp->options);
+node->data.val = rc;
+(void)tree_insertnode(&tree_dns_fails, node);
+return rc;
+}
+
+
+
+/*************************************************
+*              Do basic DNS lookup               *
+*************************************************/
+
+/* Call the resolver to look up the given domain name, using the given type,
+and check the result. The error code TRY_AGAIN is documented as meaning "non-
+Authoritive Host not found, or SERVERFAIL". Sometimes there are badly set
+up nameservers that produce this error continually, so there is the option of
+providing a list of domains for which this is treated as a non-existent
+host.
+
+Arguments:
+  dnsa      pointer to dns_answer structure
+  name      name to look up
+  type      type of DNS record required (T_A, T_MX, etc)
+
+Returns:    DNS_SUCCEED   successful lookup
+            DNS_NOMATCH   name not found (NXDOMAIN)
+                          or name contains illegal characters (if checking)
+                          or name is an IP address (for IP address lookup)
+            DNS_NODATA    domain exists, but no data for this type (NODATA)
+            DNS_AGAIN     soft failure, try again later
+            DNS_FAIL      DNS failure
+*/
+
+int
+dns_basic_lookup(dns_answer *dnsa, uschar *name, int type)
+{
+#ifndef STAND_ALONE
+int rc = -1;
+uschar *save;
+#endif
+res_state resp = os_get_dns_resolver_res();
+
+tree_node *previous;
+uschar node_name[290];
+
+/* DNS lookup failures of any kind are cached in a tree. This is mainly so that
+a timeout on one domain doesn't happen time and time again for messages that
+have many addresses in the same domain. We rely on the resolver and name server
+caching for successful lookups. */
+
+sprintf(CS node_name, "%.255s-%s-%lx", name, dns_text_type(type),
+  resp->options);
+previous = tree_search(tree_dns_fails, node_name);
+if (previous != NULL)
+  {
+  DEBUG(D_dns) debug_printf("DNS lookup of %.255s-%s: using cached value %s\n",
+    name, dns_text_type(type),
+      (previous->data.val == DNS_NOMATCH)? "DNS_NOMATCH" :
+      (previous->data.val == DNS_NODATA)? "DNS_NODATA" :
+      (previous->data.val == DNS_AGAIN)? "DNS_AGAIN" :
+      (previous->data.val == DNS_FAIL)? "DNS_FAIL" : "??");
+  return previous->data.val;
+  }
+
+/* If configured, check the hygene of the name passed to lookup. Otherwise,
+although DNS lookups may give REFUSED at the lower level, some resolvers
+turn this into TRY_AGAIN, which is silly. Give a NOMATCH return, since such
+domains cannot be in the DNS. The check is now done by a regular expression;
+give it space for substring storage to save it having to get its own if the
+regex has substrings that are used - the default uses a conditional.
+
+This test is omitted for PTR records. These occur only in calls from the dnsdb
+lookup, which constructs the names itself, so they should be OK. Besides,
+bitstring labels don't conform to normal name syntax. (But the aren't used any
+more.)
+
+For SRV records, we omit the initial _smtp._tcp. components at the start. */
+
+#ifndef STAND_ALONE   /* Omit this for stand-alone tests */
+
+if (check_dns_names_pattern[0] != 0 && type != T_PTR && type != T_TXT)
+  {
+  uschar *checkname = name;
+  int ovector[3*(EXPAND_MAXN+1)];
+
+  if (regex_check_dns_names == NULL)
+    regex_check_dns_names =
+      regex_must_compile(check_dns_names_pattern, FALSE, TRUE);
+
+  /* For an SRV lookup, skip over the first two components (the service and
+  protocol names, which both start with an underscore). */
+
+  if (type == T_SRV)
+    {
+    while (*checkname++ != '.');
+    while (*checkname++ != '.');
+    }
+
+  if (pcre_exec(regex_check_dns_names, NULL, CS checkname, Ustrlen(checkname),
+      0, PCRE_EOPT, ovector, sizeof(ovector)/sizeof(int)) < 0)
+    {
+    DEBUG(D_dns)
+      debug_printf("DNS name syntax check failed: %s (%s)\n", name,
+        dns_text_type(type));
+    host_find_failed_syntax = TRUE;
+    return DNS_NOMATCH;
+    }
+  }
+
+#endif /* STAND_ALONE */
+
+/* Call the resolver; for an overlong response, res_search() will return the
+number of bytes the message would need, so we need to check for this case. The
+effect is to truncate overlong data.
+
+On some systems, res_search() will recognize "A-for-A" queries and return
+the IP address instead of returning -1 with h_error=HOST_NOT_FOUND. Some
+nameservers are also believed to do this. It is, of course, contrary to the
+specification of the DNS, so we lock it out. */
+
+if ((
+    #ifdef SUPPORT_A6
+    type == T_A6 ||
+    #endif
+    type == T_A || type == T_AAAA) &&
+    string_is_ip_address(name, NULL) != 0)
+  return DNS_NOMATCH;
+
+/* If we are running in the test harness, instead of calling the normal resolver
+(res_search), we call fakens_search(), which recognizes certain special
+domains, and interfaces to a fake nameserver for certain special zones. */
+
+if (running_in_test_harness)
+  dnsa->answerlen = fakens_search(name, type, dnsa->answer, MAXPACKET);
+else
+  dnsa->answerlen = res_search(CS name, C_IN, type, dnsa->answer, MAXPACKET);
+
+if (dnsa->answerlen > MAXPACKET)
+  {
+  DEBUG(D_dns) debug_printf("DNS lookup of %s (%s) resulted in overlong packet (size %d), truncating to %d.\n",
+    name, dns_text_type(type), dnsa->answerlen, MAXPACKET);
+  dnsa->answerlen = MAXPACKET;
+  }
+
+if (dnsa->answerlen < 0) switch (h_errno)
+  {
+  case HOST_NOT_FOUND:
+  DEBUG(D_dns) debug_printf("DNS lookup of %s (%s) gave HOST_NOT_FOUND\n"
+    "returning DNS_NOMATCH\n", name, dns_text_type(type));
+  return dns_return(name, type, DNS_NOMATCH);
+
+  case TRY_AGAIN:
+  DEBUG(D_dns) debug_printf("DNS lookup of %s (%s) gave TRY_AGAIN\n",
+    name, dns_text_type(type));
+
+  /* Cut this out for various test programs */
+  #ifndef STAND_ALONE
+  save = deliver_domain;
+  deliver_domain = name;  /* set $domain */
+  rc = match_isinlist(name, &dns_again_means_nonexist, 0, NULL, NULL,
+    MCL_DOMAIN, TRUE, NULL);
+  deliver_domain = save;
+  if (rc != OK)
+    {
+    DEBUG(D_dns) debug_printf("returning DNS_AGAIN\n");
+    return dns_return(name, type, DNS_AGAIN);
+    }
+  DEBUG(D_dns) debug_printf("%s is in dns_again_means_nonexist: returning "
+    "DNS_NOMATCH\n", name);
+  return dns_return(name, type, DNS_NOMATCH);
+
+  #else   /* For stand-alone tests */
+  return dns_return(name, type, DNS_AGAIN);
+  #endif
+
+  case NO_RECOVERY:
+  DEBUG(D_dns) debug_printf("DNS lookup of %s (%s) gave NO_RECOVERY\n"
+    "returning DNS_FAIL\n", name, dns_text_type(type));
+  return dns_return(name, type, DNS_FAIL);
+
+  case NO_DATA:
+  DEBUG(D_dns) debug_printf("DNS lookup of %s (%s) gave NO_DATA\n"
+    "returning DNS_NODATA\n", name, dns_text_type(type));
+  return dns_return(name, type, DNS_NODATA);
+
+  default:
+  DEBUG(D_dns) debug_printf("DNS lookup of %s (%s) gave unknown DNS error %d\n"
+    "returning DNS_FAIL\n", name, dns_text_type(type), h_errno);
+  return dns_return(name, type, DNS_FAIL);
+  }
+
+DEBUG(D_dns) debug_printf("DNS lookup of %s (%s) succeeded\n",
+  name, dns_text_type(type));
+
+return DNS_SUCCEED;
+}
+
+
+
+
+/************************************************
+*        Do a DNS lookup and handle CNAMES      *
+************************************************/
+
+/* Look up the given domain name, using the given type. Follow CNAMEs if
+necessary, but only so many times. There aren't supposed to be CNAME chains in
+the DNS, but you are supposed to cope with them if you find them.
+
+The assumption is made that if the resolver gives back records of the
+requested type *and* a CNAME, we don't need to make another call to look up
+the CNAME. I can't see how it could return only some of the right records. If
+it's done a CNAME lookup in the past, it will have all of them; if not, it
+won't return any.
+
+If fully_qualified_name is not NULL, set it to point to the full name
+returned by the resolver, if this is different to what it is given, unless
+the returned name starts with "*" as some nameservers seem to be returning
+wildcards in this form.
+
+Arguments:
+  dnsa                  pointer to dns_answer structure
+  name                  domain name to look up
+  type                  DNS record type (T_A, T_MX, etc)
+  fully_qualified_name  if not NULL, return the returned name here if its
+                          contents are different (i.e. it must be preset)
+
+Returns:                DNS_SUCCEED   successful lookup
+                        DNS_NOMATCH   name not found
+                        DNS_NODATA    no data found
+                        DNS_AGAIN     soft failure, try again later
+                        DNS_FAIL      DNS failure
+*/
+
+int
+dns_lookup(dns_answer *dnsa, uschar *name, int type, uschar **fully_qualified_name)
+{
+int i;
+uschar *orig_name = name;
+
+/* Loop to follow CNAME chains so far, but no further... */
+
+for (i = 0; i < 10; i++)
+  {
+  uschar data[256];
+  dns_record *rr, cname_rr, type_rr;
+  dns_scan dnss;
+  int datalen, rc;
+
+  /* DNS lookup failures get passed straight back. */
+
+  if ((rc = dns_basic_lookup(dnsa, name, type)) != DNS_SUCCEED) return rc;
+
+  /* We should have either records of the required type, or a CNAME record,
+  or both. We need to know whether both exist for getting the fully qualified
+  name, but avoid scanning more than necessary. Note that we must copy the
+  contents of any rr blocks returned by dns_next_rr() as they use the same
+  area in the dnsa block. */
+
+  cname_rr.data = type_rr.data = NULL;
+  for (rr = dns_next_rr(dnsa, &dnss, RESET_ANSWERS);
+       rr != NULL;
+       rr = dns_next_rr(dnsa, &dnss, RESET_NEXT))
+    {
+    if (rr->type == type)
+      {
+      if (type_rr.data == NULL) type_rr = *rr;
+      if (cname_rr.data != NULL) break;
+      }
+    else if (rr->type == T_CNAME) cname_rr = *rr;
+    }
+
+  /* For the first time round this loop, if a CNAME was found, take the fully
+  qualified name from it; otherwise from the first data record, if present. */
+
+  if (i == 0 && fully_qualified_name != NULL)
+    {
+    if (cname_rr.data != NULL)
+      {
+      if (Ustrcmp(cname_rr.name, *fully_qualified_name) != 0 &&
+          cname_rr.name[0] != '*')
+        *fully_qualified_name = string_copy_dnsdomain(cname_rr.name);
+      }
+    else if (type_rr.data != NULL)
+      {
+      if (Ustrcmp(type_rr.name, *fully_qualified_name) != 0 &&
+          type_rr.name[0] != '*')
+        *fully_qualified_name = string_copy_dnsdomain(type_rr.name);
+      }
+    }
+
+  /* If any data records of the correct type were found, we are done. */
+
+  if (type_rr.data != NULL) return DNS_SUCCEED;
+
+  /* If there are no data records, we need to re-scan the DNS using the
+  domain given in the CNAME record, which should exist (otherwise we should
+  have had a failure from dns_lookup). However code against the possibility of
+  its not existing. */
+
+  if (cname_rr.data == NULL) return DNS_FAIL;
+  datalen = dn_expand(dnsa->answer, dnsa->answer + dnsa->answerlen,
+    cname_rr.data, (DN_EXPAND_ARG4_TYPE)data, 256);
+  if (datalen < 0) return DNS_FAIL;
+  name = data;
+
+  DEBUG(D_dns) debug_printf("CNAME found: change to %s\n", name);
+  }       /* Loop back to do another lookup */
+
+/*Control reaches here after 10 times round the CNAME loop. Something isn't
+right... */
+
+log_write(0, LOG_MAIN, "CNAME loop for %s encountered", orig_name);
+return DNS_FAIL;
+}
+
+
+
+
+
+
+/************************************************
+*    Do a DNS lookup and handle virtual types   *
+************************************************/
+
+/* This function handles some invented "lookup types" that synthesize feature
+not available in the basic types. The special types all have negative values.
+Positive type values are passed straight on to dns_lookup().
+
+Arguments:
+  dnsa                  pointer to dns_answer structure
+  name                  domain name to look up
+  type                  DNS record type (T_A, T_MX, etc or a "special")
+  fully_qualified_name  if not NULL, return the returned name here if its
+                          contents are different (i.e. it must be preset)
+
+Returns:                DNS_SUCCEED   successful lookup
+                        DNS_NOMATCH   name not found
+                        DNS_NODATA    no data found
+                        DNS_AGAIN     soft failure, try again later
+                        DNS_FAIL      DNS failure
+*/
+
+int
+dns_special_lookup(dns_answer *dnsa, uschar *name, int type,
+  uschar **fully_qualified_name)
+{
+if (type >= 0) return dns_lookup(dnsa, name, type, fully_qualified_name);
+
+/* The "mx hosts only" type doesn't require any special action here */
+
+if (type == T_MXH) return dns_lookup(dnsa, name, T_MX, fully_qualified_name);
+
+/* Find nameservers for the domain or the nearest enclosing zone, excluding the
+root servers. */
+
+if (type == T_ZNS)
+  {
+  uschar *d = name;
+  while (d != 0)
+    {
+    int rc = dns_lookup(dnsa, d, T_NS, fully_qualified_name);
+    if (rc != DNS_NOMATCH && rc != DNS_NODATA) return rc;
+    while (*d != 0 && *d != '.') d++;
+    if (*d++ == 0) break;
+    }
+  return DNS_NOMATCH;
+  }
+
+/* Try to look up the Client SMTP Authorization SRV record for the name. If
+there isn't one, search from the top downwards for a CSA record in a parent
+domain, which might be making assertions about subdomains. If we find a record
+we set fully_qualified_name to whichever lookup succeeded, so that the caller
+can tell whether to look at the explicit authorization field or the subdomain
+assertion field. */
+
+if (type == T_CSA)
+  {
+  uschar *srvname, *namesuff, *tld, *p;
+  int priority, weight, port;
+  int limit, rc, i;
+  BOOL ipv6;
+  dns_record *rr;
+  dns_scan dnss;
+
+  DEBUG(D_dns) debug_printf("CSA lookup of %s\n", name);
+
+  srvname = string_sprintf("_client._smtp.%s", name);
+  rc = dns_lookup(dnsa, srvname, T_SRV, NULL);
+  if (rc == DNS_SUCCEED || rc == DNS_AGAIN)
+    {
+    if (rc == DNS_SUCCEED) *fully_qualified_name = name;
+    return rc;
+    }
+
+  /* Search for CSA subdomain assertion SRV records from the top downwards,
+  starting with the 2nd level domain. This order maximizes cache-friendliness.
+  We skip the top level domains to avoid loading their nameservers and because
+  we know they'll never have CSA SRV records. */
+
+  namesuff = Ustrrchr(name, '.');
+  if (namesuff == NULL) return DNS_NOMATCH;
+  tld = namesuff + 1;
+  ipv6 = FALSE;
+  limit = dns_csa_search_limit;
+
+  /* Use more appropriate search parameters if we are in the reverse DNS. */
+
+  if (strcmpic(namesuff, US".arpa") == 0)
+    {
+    if (namesuff - 8 > name && strcmpic(namesuff - 8, US".in-addr.arpa") == 0)
+      {
+      namesuff -= 8;
+      tld = namesuff + 1;
+      limit = 3;
+      }
+    else if (namesuff - 4 > name && strcmpic(namesuff - 4, US".ip6.arpa") == 0)
+      {
+      namesuff -= 4;
+      tld = namesuff + 1;
+      ipv6 = TRUE;
+      limit = 3;
+      }
+    }
+
+  DEBUG(D_dns) debug_printf("CSA TLD %s\n", tld);
+
+  /* Do not perform the search if the top level or 2nd level domains do not
+  exist. This is quite common, and when it occurs all the search queries would
+  go to the root or TLD name servers, which is not friendly. So we check the
+  AUTHORITY section; if it contains the root's SOA record or the TLD's SOA then
+  the TLD or the 2LD (respectively) doesn't exist and we can skip the search.
+  If the TLD and the 2LD exist but the explicit CSA record lookup failed, then
+  the AUTHORITY SOA will be the 2LD's or a subdomain thereof. */
+
+  if (rc == DNS_NOMATCH)
+    {
+    /* This is really gross. The successful return value from res_search() is
+    the packet length, which is stored in dnsa->answerlen. If we get a
+    negative DNS reply then res_search() returns -1, which causes the bounds
+    checks for name decompression to fail when it is treated as a packet
+    length, which in turn causes the authority search to fail. The correct
+    packet length has been lost inside libresolv, so we have to guess a
+    replacement value. (The only way to fix this properly would be to
+    re-implement res_search() and res_query() so that they don't muddle their
+    success and packet length return values.) For added safety we only reset
+    the packet length if the packet header looks plausible. */
+
+    HEADER *h = (HEADER *)dnsa->answer;
+    if (h->qr == 1 && h->opcode == QUERY && h->tc == 0
+        && (h->rcode == NOERROR || h->rcode == NXDOMAIN)
+        && ntohs(h->qdcount) == 1 && ntohs(h->ancount) == 0
+        && ntohs(h->nscount) >= 1)
+      dnsa->answerlen = MAXPACKET;
+
+    for (rr = dns_next_rr(dnsa, &dnss, RESET_AUTHORITY);
+         rr != NULL;
+         rr = dns_next_rr(dnsa, &dnss, RESET_NEXT))
+      if (rr->type != T_SOA) continue;
+      else if (strcmpic(rr->name, US"") == 0 ||
+               strcmpic(rr->name, tld) == 0) return DNS_NOMATCH;
+      else break;
+    }
+
+  for (i = 0; i < limit; i++)
+    {
+    if (ipv6)
+      {
+      /* Scan through the IPv6 reverse DNS in chunks of 16 bits worth of IP
+      address, i.e. 4 hex chars and 4 dots, i.e. 8 chars. */
+      namesuff -= 8;
+      if (namesuff <= name) return DNS_NOMATCH;
+      }
+    else
+      /* Find the start of the preceding domain name label. */
+      do
+        if (--namesuff <= name) return DNS_NOMATCH;
+      while (*namesuff != '.');
+
+    DEBUG(D_dns) debug_printf("CSA parent search at %s\n", namesuff + 1);
+
+    srvname = string_sprintf("_client._smtp.%s", namesuff + 1);
+    rc = dns_lookup(dnsa, srvname, T_SRV, NULL);
+    if (rc == DNS_AGAIN) return rc;
+    if (rc != DNS_SUCCEED) continue;
+
+    /* Check that the SRV record we have found is worth returning. We don't
+    just return the first one we find, because some lower level SRV record
+    might make stricter assertions than its parent domain. */
+
+    for (rr = dns_next_rr(dnsa, &dnss, RESET_ANSWERS);
+         rr != NULL;
+         rr = dns_next_rr(dnsa, &dnss, RESET_NEXT))
+      {
+      if (rr->type != T_SRV) continue;
+
+      /* Extract the numerical SRV fields (p is incremented) */
+      p = rr->data;
+      GETSHORT(priority, p);
+      GETSHORT(weight, p);
+      GETSHORT(port, p);
+
+      /* Check the CSA version number */
+      if (priority != 1) continue;
+
+      /* If it's making an interesting assertion, return this response. */
+      if (port & 1)
+        {
+        *fully_qualified_name = namesuff + 1;
+        return DNS_SUCCEED;
+        }
+      }
+    }
+  return DNS_NOMATCH;
+  }
+
+/* Control should never reach here */
+
+return DNS_FAIL;
+}
+
+
+
+/* Support for A6 records has been commented out since they were demoted to
+experimental status at IETF 51. */
+
+#if HAVE_IPV6 && defined(SUPPORT_A6)
+
+/*************************************************
+*        Search DNS block for prefix RRs         *
+*************************************************/
+
+/* Called from dns_complete_a6() to search an additional section or a main
+answer section for required prefix records to complete an IPv6 address obtained
+from an A6 record. For each prefix record, a recursive call to dns_complete_a6
+is made, with a new copy of the address so far.
+
+Arguments:
+  dnsa       the DNS answer block
+  which      RESET_ADDITIONAL or RESET_ANSWERS
+  name       name of prefix record
+  yptrptr    pointer to the pointer that points to where to hang the next
+               dns_address structure
+  bits       number of bits we have already got
+  bitvec     the bits we have already got
+
+Returns:     TRUE if any records were found
+*/
+
+static BOOL
+dns_find_prefix(dns_answer *dnsa, int which, uschar *name, dns_address
+  ***yptrptr, int bits, uschar *bitvec)
+{
+BOOL yield = FALSE;
+dns_record *rr;
+dns_scan dnss;
+
+for (rr = dns_next_rr(dnsa, &dnss, which);
+     rr != NULL;
+     rr = dns_next_rr(dnsa, &dnss, RESET_NEXT))
+  {
+  uschar cbitvec[16];
+  if (rr->type != T_A6 || strcmpic(rr->name, name) != 0) continue;
+  yield = TRUE;
+  memcpy(cbitvec, bitvec, sizeof(cbitvec));
+  dns_complete_a6(yptrptr, dnsa, rr, bits, cbitvec);
+  }
+
+return yield;
+}
+
+
+
+/*************************************************
+*            Follow chains of A6 records         *
+*************************************************/
+
+/* A6 records may be incomplete, with pointers to other records containing more
+bits of the address. There can be a tree structure, leading to a number of
+addresses originating from a single initial A6 record.
+
+Arguments:
+  yptrptr    pointer to the pointer that points to where to hang the next
+               dns_address structure
+  dnsa       the current DNS answer block
+  rr         the RR we have at present
+  bits       number of bits we have already got
+  bitvec     the bits we have already got
+
+Returns:     nothing
+*/
+
+static void
+dns_complete_a6(dns_address ***yptrptr, dns_answer *dnsa, dns_record *rr,
+  int bits, uschar *bitvec)
+{
+static uschar bitmask[] = { 0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80 };
+uschar *p = (uschar *)(rr->data);
+int prefix_len, suffix_len;
+int i, j, k;
+uschar *chainptr;
+uschar chain[264];
+dns_answer cdnsa;
+
+/* The prefix length is the first byte. It defines the prefix which is missing
+from the data in this record as a number of bits. Zero means this is the end of
+a chain. The suffix is the data in this record; only sufficient bytes to hold
+it are supplied. There may be zero bytes. We have to ignore trailing bits that
+we have already obtained from earlier RRs in the chain. */
+
+prefix_len = *p++;                      /* bits */
+suffix_len = (128 - prefix_len + 7)/8;  /* bytes */
+
+/* If the prefix in this record is greater than the prefix in the previous
+record in the chain, we have to ignore the record (RFC 2874). */
+
+if (prefix_len > 128 - bits) return;
+
+/* In this little loop, the number of bits up to and including the current byte
+is held in k. If we have none of the bits in this byte, we can just or it into
+the current data. If we have all of the bits in this byte, we skip it.
+Otherwise, some masking has to be done. */
+
+for (i = suffix_len - 1, j = 15, k = 8; i >= 0; i--)
+  {
+  int required = k - bits;
+  if (required >= 8) bitvec[j] |= p[i];
+    else if (required > 0) bitvec[j] |= p[i] & bitmask[required];
+  j--;     /* I tried putting these in the "for" statement, but gcc muttered */
+  k += 8;  /* about computed values not being used. */
+  }
+
+/* If the prefix_length is zero, we are at the end of a chain. Build a
+dns_address item with the current data, hang it onto the end of the chain,
+adjust the hanging pointer, and we are done. */
+
+if (prefix_len == 0)
+  {
+  dns_address *new = store_get(sizeof(dns_address) + 50);
+  inet_ntop(AF_INET6, bitvec, CS new->address, 50);
+  new->next = NULL;
+  **yptrptr = new;
+  *yptrptr = &(new->next);
+  return;
+  }
+
+/* Prefix length is not zero. Reset the number of bits that we have collected
+so far, and extract the chain name. */
+
+bits = 128 - prefix_len;
+p += suffix_len;
+
+chainptr = chain;
+while ((i = *p++) != 0)
+  {
+  if (chainptr != chain) *chainptr++ = '.';
+  memcpy(chainptr, p, i);
+  chainptr += i;
+  p += i;
+  }
+*chainptr = 0;
+chainptr = chain;
+
+/* Now scan the current DNS response record to see if the additional section
+contains the records we want. This processing can be cut out for testing
+purposes. */
+
+if (dns_find_prefix(dnsa, RESET_ADDITIONAL, chainptr, yptrptr, bits, bitvec))
+  return;
+
+/* No chain records were found in the current DNS response block. Do a new DNS
+lookup to try to find these records. This opens up the possibility of DNS
+failures. We ignore them at this point; if all branches of the tree fail, there
+will be no addresses at the end. */
+
+if (dns_lookup(&cdnsa, chainptr, T_A6, NULL) == DNS_SUCCEED)
+  (void)dns_find_prefix(&cdnsa, RESET_ANSWERS, chainptr, yptrptr, bits, bitvec);
+}
+#endif  /* HAVE_IPV6 && defined(SUPPORT_A6) */
+
+
+
+
+/*************************************************
+*          Get address(es) from DNS record       *
+*************************************************/
+
+/* The record type is either T_A for an IPv4 address or T_AAAA (or T_A6 when
+supported) for an IPv6 address. In the A6 case, there may be several addresses,
+generated by following chains. A recursive function does all the hard work. A6
+records now look like passing into history, so the code is only included when
+explicitly asked for.
+
+Argument:
+  dnsa       the DNS answer block
+  rr         the RR
+
+Returns:     pointer a chain of dns_address items
+*/
+
+dns_address *
+dns_address_from_rr(dns_answer *dnsa, dns_record *rr)
+{
+dns_address *yield = NULL;
+
+#if HAVE_IPV6 && defined(SUPPORT_A6)
+dns_address **yieldptr = &yield;
+uschar bitvec[16];
+#else
+dnsa = dnsa;    /* Stop picky compilers warning */
+#endif
+
+if (rr->type == T_A)
+  {
+  uschar *p = (uschar *)(rr->data);
+  yield = store_get(sizeof(dns_address) + 20);
+  (void)sprintf(CS yield->address, "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
+  yield->next = NULL;
+  }
+
+#if HAVE_IPV6
+
+#ifdef SUPPORT_A6
+else if (rr->type == T_A6)
+  {
+  memset(bitvec, 0, sizeof(bitvec));
+  dns_complete_a6(&yieldptr, dnsa, rr, 0, bitvec);
+  }
+#endif  /* SUPPORT_A6 */
+
+else
+  {
+  yield = store_get(sizeof(dns_address) + 50);
+  inet_ntop(AF_INET6, (uschar *)(rr->data), CS yield->address, 50);
+  yield->next = NULL;
+  }
+#endif  /* HAVE_IPV6 */
+
+return yield;
+}
+
+/* vi: aw ai sw=2
+*/
+/* End of dns.c */
diff --git a/src/drtables.c b/src/drtables.c
new file mode 100644 (file)
index 0000000..c2d8668
--- /dev/null
@@ -0,0 +1,674 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2014 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+
+#include "exim.h"
+
+#include <string.h>
+
+/* This module contains tables that define the lookup methods and drivers
+that are actually included in the binary. Its contents are controlled by
+various macros in config.h that ultimately come from Local/Makefile. They are
+all described in src/EDITME. */
+
+
+lookup_info **lookup_list;
+int lookup_list_count = 0;
+
+static int lookup_list_init_done = 0;
+
+/* Table of information about all possible authentication mechamisms. All
+entries are always present if any mechanism is declared, but the functions are
+set to NULL for those that are not compiled into the binary. */
+
+#ifdef AUTH_CRAM_MD5
+#include "auths/cram_md5.h"
+#endif
+
+#ifdef AUTH_CYRUS_SASL
+#include "auths/cyrus_sasl.h"
+#endif
+
+#ifdef AUTH_DOVECOT
+#include "auths/dovecot.h"
+#endif
+
+#ifdef AUTH_GSASL
+#include "auths/gsasl_exim.h"
+#endif
+
+#ifdef AUTH_HEIMDAL_GSSAPI
+#include "auths/heimdal_gssapi.h"
+#endif
+
+#ifdef AUTH_PLAINTEXT
+#include "auths/plaintext.h"
+#endif
+
+#ifdef AUTH_SPA
+#include "auths/spa.h"
+#endif
+
+auth_info auths_available[] = {
+
+/* Checking by an expansion condition on plain text */
+
+#ifdef AUTH_CRAM_MD5
+  {
+  US"cram_md5",                              /* lookup name */
+  auth_cram_md5_options,
+  &auth_cram_md5_options_count,
+  &auth_cram_md5_option_defaults,
+  sizeof(auth_cram_md5_options_block),
+  auth_cram_md5_init,                        /* init function */
+  auth_cram_md5_server,                      /* server function */
+  auth_cram_md5_client,                      /* client function */
+  NULL                                       /* diagnostic function */
+  },
+#endif
+
+#ifdef AUTH_CYRUS_SASL
+  {
+  US"cyrus_sasl",           /* lookup name */
+  auth_cyrus_sasl_options,
+  &auth_cyrus_sasl_options_count,
+  &auth_cyrus_sasl_option_defaults,
+  sizeof(auth_cyrus_sasl_options_block),
+  auth_cyrus_sasl_init,                      /* init function */
+  auth_cyrus_sasl_server,                    /* server function */
+  NULL,                                      /* client function */
+  auth_cyrus_sasl_version_report             /* diagnostic function */
+  },
+#endif
+
+#ifdef AUTH_DOVECOT
+  {
+  US"dovecot",                                /* lookup name */
+  auth_dovecot_options,
+  &auth_dovecot_options_count,
+  &auth_dovecot_option_defaults,
+  sizeof(auth_dovecot_options_block),
+  auth_dovecot_init,                          /* init function */
+  auth_dovecot_server,                        /* server function */
+  NULL,                                       /* client function */
+  NULL                                        /* diagnostic function */
+  },
+#endif
+
+#ifdef AUTH_GSASL
+  {
+  US"gsasl",                                  /* lookup name */
+  auth_gsasl_options,
+  &auth_gsasl_options_count,
+  &auth_gsasl_option_defaults,
+  sizeof(auth_gsasl_options_block),
+  auth_gsasl_init,                            /* init function */
+  auth_gsasl_server,                          /* server function */
+  NULL,                                       /* client function */
+  auth_gsasl_version_report                   /* diagnostic function */
+  },
+#endif
+
+#ifdef AUTH_HEIMDAL_GSSAPI
+  {
+  US"heimdal_gssapi",                         /* lookup name */
+  auth_heimdal_gssapi_options,
+  &auth_heimdal_gssapi_options_count,
+  &auth_heimdal_gssapi_option_defaults,
+  sizeof(auth_heimdal_gssapi_options_block),
+  auth_heimdal_gssapi_init,                   /* init function */
+  auth_heimdal_gssapi_server,                 /* server function */
+  NULL,                                       /* client function */
+  auth_heimdal_gssapi_version_report          /* diagnostic function */
+  },
+#endif
+
+#ifdef AUTH_PLAINTEXT
+  {
+  US"plaintext",                             /* lookup name */
+  auth_plaintext_options,
+  &auth_plaintext_options_count,
+  &auth_plaintext_option_defaults,
+  sizeof(auth_plaintext_options_block),
+  auth_plaintext_init,                       /* init function */
+  auth_plaintext_server,                     /* server function */
+  auth_plaintext_client,                     /* client function */
+  NULL                                       /* diagnostic function */
+  },
+#endif
+
+#ifdef AUTH_SPA
+  {
+  US"spa",                                   /* lookup name */
+  auth_spa_options,
+  &auth_spa_options_count,
+  &auth_spa_option_defaults,
+  sizeof(auth_spa_options_block),
+  auth_spa_init,                             /* init function */
+  auth_spa_server,                           /* server function */
+  auth_spa_client,                           /* client function */
+  NULL                                       /* diagnostic function */
+  },
+#endif
+
+{ US"", NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL  }
+};
+
+
+/* Tables of information about which routers and transports are included in the
+exim binary. */
+
+/* Pull in the necessary header files */
+
+#include "routers/rf_functions.h"
+
+#ifdef ROUTER_ACCEPT
+#include "routers/accept.h"
+#endif
+
+#ifdef ROUTER_DNSLOOKUP
+#include "routers/dnslookup.h"
+#endif
+
+#ifdef ROUTER_MANUALROUTE
+#include "routers/manualroute.h"
+#endif
+
+#ifdef ROUTER_IPLITERAL
+#include "routers/ipliteral.h"
+#endif
+
+#ifdef ROUTER_IPLOOKUP
+#include "routers/iplookup.h"
+#endif
+
+#ifdef ROUTER_QUERYPROGRAM
+#include "routers/queryprogram.h"
+#endif
+
+#ifdef ROUTER_REDIRECT
+#include "routers/redirect.h"
+#endif
+
+#ifdef TRANSPORT_APPENDFILE
+#include "transports/appendfile.h"
+#endif
+
+#ifdef TRANSPORT_AUTOREPLY
+#include "transports/autoreply.h"
+#endif
+
+#ifdef TRANSPORT_LMTP
+#include "transports/lmtp.h"
+#endif
+
+#ifdef TRANSPORT_PIPE
+#include "transports/pipe.h"
+#endif
+
+#ifdef TRANSPORT_SMTP
+#include "transports/smtp.h"
+#endif
+
+
+/* Now set up the structures, terminated by an entry with a null name. */
+
+router_info routers_available[] = {
+#ifdef ROUTER_ACCEPT
+  {
+  US"accept",
+  accept_router_options,
+  &accept_router_options_count,
+  &accept_router_option_defaults,
+  sizeof(accept_router_options_block),
+  accept_router_init,
+  accept_router_entry,
+  NULL,     /* no tidyup entry */
+  ri_yestransport
+  },
+#endif
+#ifdef ROUTER_DNSLOOKUP
+  {
+  US"dnslookup",
+  dnslookup_router_options,
+  &dnslookup_router_options_count,
+  &dnslookup_router_option_defaults,
+  sizeof(dnslookup_router_options_block),
+  dnslookup_router_init,
+  dnslookup_router_entry,
+  NULL,     /* no tidyup entry */
+  ri_yestransport
+  },
+#endif
+#ifdef ROUTER_IPLITERAL
+  {
+  US"ipliteral",
+  ipliteral_router_options,
+  &ipliteral_router_options_count,
+  &ipliteral_router_option_defaults,
+  sizeof(ipliteral_router_options_block),
+  ipliteral_router_init,
+  ipliteral_router_entry,
+  NULL,     /* no tidyup entry */
+  ri_yestransport
+  },
+#endif
+#ifdef ROUTER_IPLOOKUP
+  {
+  US"iplookup",
+  iplookup_router_options,
+  &iplookup_router_options_count,
+  &iplookup_router_option_defaults,
+  sizeof(iplookup_router_options_block),
+  iplookup_router_init,
+  iplookup_router_entry,
+  NULL,     /* no tidyup entry */
+  ri_notransport
+  },
+#endif
+#ifdef ROUTER_MANUALROUTE
+  {
+  US"manualroute",
+  manualroute_router_options,
+  &manualroute_router_options_count,
+  &manualroute_router_option_defaults,
+  sizeof(manualroute_router_options_block),
+  manualroute_router_init,
+  manualroute_router_entry,
+  NULL,     /* no tidyup entry */
+  0
+  },
+#endif
+#ifdef ROUTER_QUERYPROGRAM
+  {
+  US"queryprogram",
+  queryprogram_router_options,
+  &queryprogram_router_options_count,
+  &queryprogram_router_option_defaults,
+  sizeof(queryprogram_router_options_block),
+  queryprogram_router_init,
+  queryprogram_router_entry,
+  NULL,     /* no tidyup entry */
+  0
+  },
+#endif
+#ifdef ROUTER_REDIRECT
+  {
+  US"redirect",
+  redirect_router_options,
+  &redirect_router_options_count,
+  &redirect_router_option_defaults,
+  sizeof(redirect_router_options_block),
+  redirect_router_init,
+  redirect_router_entry,
+  NULL,     /* no tidyup entry */
+  ri_notransport
+  },
+#endif
+{ US"", NULL, NULL, NULL, 0, NULL, NULL, NULL, 0 }
+};
+
+
+
+transport_info transports_available[] = {
+#ifdef TRANSPORT_APPENDFILE
+  {
+  US"appendfile",                              /* driver name */
+  appendfile_transport_options,                /* local options table */
+  &appendfile_transport_options_count,         /* number of entries */
+  &appendfile_transport_option_defaults,       /* private options defaults */
+  sizeof(appendfile_transport_options_block),  /* size of private block */
+  appendfile_transport_init,                   /* init entry point */
+  appendfile_transport_entry,                  /* main entry point */
+  NULL,                                        /* no tidyup entry */
+  NULL,                                        /* no closedown entry */
+  TRUE,                                        /* local flag */
+  },
+#endif
+#ifdef TRANSPORT_AUTOREPLY
+  {
+  US"autoreply",                               /* driver name */
+  autoreply_transport_options,                 /* local options table */
+  &autoreply_transport_options_count,          /* number of entries */
+  &autoreply_transport_option_defaults,        /* private options defaults */
+  sizeof(autoreply_transport_options_block),   /* size of private block */
+  autoreply_transport_init,                    /* init entry point */
+  autoreply_transport_entry,                   /* main entry point */
+  NULL,                                        /* no tidyup entry */
+  NULL,                                        /* no closedown entry */
+  TRUE                                         /* local flag */
+  },
+#endif
+#ifdef TRANSPORT_LMTP
+  {
+  US"lmtp",                                    /* driver name */
+  lmtp_transport_options,                      /* local options table */
+  &lmtp_transport_options_count,               /* number of entries */
+  &lmtp_transport_option_defaults,             /* private options defaults */
+  sizeof(lmtp_transport_options_block),        /* size of private block */
+  lmtp_transport_init,                         /* init entry point */
+  lmtp_transport_entry,                        /* main entry point */
+  NULL,                                        /* no tidyup entry */
+  NULL,                                        /* no closedown entry */
+  TRUE                                         /* local flag */
+  },
+#endif
+#ifdef TRANSPORT_PIPE
+  {
+  US"pipe",                                    /* driver name */
+  pipe_transport_options,                      /* local options table */
+  &pipe_transport_options_count,               /* number of entries */
+  &pipe_transport_option_defaults,             /* private options defaults */
+  sizeof(pipe_transport_options_block),        /* size of private block */
+  pipe_transport_init,                         /* init entry point */
+  pipe_transport_entry,                        /* main entry point */
+  NULL,                                        /* no tidyup entry */
+  NULL,                                        /* no closedown entry */
+  TRUE                                         /* local flag */
+  },
+#endif
+#ifdef TRANSPORT_SMTP
+  {
+  US"smtp",                                    /* driver name */
+  smtp_transport_options,                      /* local options table */
+  &smtp_transport_options_count,               /* number of entries */
+  &smtp_transport_option_defaults,             /* private options defaults */
+  sizeof(smtp_transport_options_block),        /* size of private block */
+  smtp_transport_init,                         /* init entry point */
+  smtp_transport_entry,                        /* main entry point */
+  NULL,                                        /* no tidyup entry */
+  smtp_transport_closedown,                    /* close down passed channel */
+  FALSE                                        /* local flag */
+  },
+#endif
+{ US"", NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, FALSE }
+};
+
+struct lookupmodulestr
+{
+  void *dl;
+  struct lookup_module_info *info;
+  struct lookupmodulestr *next;
+};
+
+static struct lookupmodulestr *lookupmodules = NULL;
+
+static void addlookupmodule(void *dl, struct lookup_module_info *info)
+{
+  struct lookupmodulestr *p = store_malloc(sizeof(struct lookupmodulestr));
+  p->dl = dl;
+  p->info = info;
+  p->next = lookupmodules;
+  lookupmodules = p;
+  lookup_list_count += info->lookupcount;
+}
+
+/* only valid after lookup_list and lookup_list_count are assigned */
+static void add_lookup_to_list(lookup_info *info)
+{
+  /* need to add the lookup to lookup_list, sorted */
+  int pos = 0;
+
+  /* strategy is to go through the list until we find
+   * either an empty spot or a name that is higher.
+   * this can't fail because we have enough space. */
+  while (lookup_list[pos]
+      && (Ustrcmp(lookup_list[pos]->name, info->name) <= 0)) {
+    pos++;
+  }
+  if (lookup_list[pos]) {
+    /* need to insert it, so move all the other items up
+     * (last slot is still empty, of course) */
+    memmove(&lookup_list[pos+1],
+            &lookup_list[pos],
+            sizeof(lookup_info **) * (lookup_list_count-pos-1));
+  }
+  lookup_list[pos] = info;
+}
+
+
+/* These need to be at file level for old versions of gcc (2.95.2 reported),
+ * which give parse errors on an extern in function scope.  Each entry needs
+ * to also be invoked in init_lookup_list() below  */
+
+#if defined(LOOKUP_WHOSON) && LOOKUP_WHOSON!=2
+extern lookup_module_info whoson_lookup_module_info;
+#endif
+#if defined(LOOKUP_TESTDB) && LOOKUP_TESTDB!=2
+extern lookup_module_info testdb_lookup_module_info;
+#endif
+#if defined(LOOKUP_SQLITE) && LOOKUP_SQLITE!=2
+extern lookup_module_info sqlite_lookup_module_info;
+#endif
+#ifdef EXPERIMENTAL_SPF
+extern lookup_module_info spf_lookup_module_info;
+#endif
+#ifdef EXPERIMENTAL_REDIS
+extern lookup_module_info redis_lookup_module_info;
+#endif
+#if defined(LOOKUP_PGSQL) && LOOKUP_PGSQL!=2
+extern lookup_module_info pgsql_lookup_module_info;
+#endif
+#if defined(LOOKUP_PASSWD) && LOOKUP_PASSWD!=2
+extern lookup_module_info passwd_lookup_module_info;
+#endif
+#if defined(LOOKUP_ORACLE) && LOOKUP_ORACLE!=2
+extern lookup_module_info oracle_lookup_module_info;
+#endif
+#if defined(LOOKUP_NISPLUS) && LOOKUP_NISPLUS!=2
+extern lookup_module_info nisplus_lookup_module_info;
+#endif
+#if defined(LOOKUP_NIS) && LOOKUP_NIS!=2
+extern lookup_module_info nis_lookup_module_info;
+#endif
+#if defined(LOOKUP_MYSQL) && LOOKUP_MYSQL!=2
+extern lookup_module_info mysql_lookup_module_info;
+#endif
+#if defined(LOOKUP_LSEARCH) && LOOKUP_LSEARCH!=2
+extern lookup_module_info lsearch_lookup_module_info;
+#endif
+#ifdef LOOKUP_LDAP
+extern lookup_module_info ldap_lookup_module_info;
+#endif
+#if defined(LOOKUP_IBASE) && LOOKUP_IBASE!=2
+extern lookup_module_info ibase_lookup_module_info;
+#endif
+#if defined(LOOKUP_DSEARCH) && LOOKUP_DSEARCH!=2
+extern lookup_module_info dsearch_lookup_module_info;
+#endif
+#if defined(LOOKUP_DNSDB) && LOOKUP_DNSDB!=2
+extern lookup_module_info dnsdb_lookup_module_info;
+#endif
+#if defined(LOOKUP_DBM) && LOOKUP_DBM!=2
+extern lookup_module_info dbmdb_lookup_module_info;
+#endif
+#if defined(LOOKUP_CDB) && LOOKUP_CDB!=2
+extern lookup_module_info cdb_lookup_module_info;
+#endif
+
+void init_lookup_list(void)
+{
+#ifdef LOOKUP_MODULE_DIR
+  DIR *dd;
+  struct dirent *ent;
+  int countmodules = 0;
+  int moduleerrors = 0;
+#endif
+  struct lookupmodulestr *p;
+  const pcre *regex_islookupmod = regex_must_compile(
+      US"\\." DYNLIB_FN_EXT "$", FALSE, TRUE);
+
+  if (lookup_list_init_done)
+    return;
+  lookup_list_init_done = 1;
+
+#if defined(LOOKUP_CDB) && LOOKUP_CDB!=2
+  addlookupmodule(NULL, &cdb_lookup_module_info);
+#endif
+
+#if defined(LOOKUP_DBM) && LOOKUP_DBM!=2
+  addlookupmodule(NULL, &dbmdb_lookup_module_info);
+#endif
+
+#if defined(LOOKUP_DNSDB) && LOOKUP_DNSDB!=2
+  addlookupmodule(NULL, &dnsdb_lookup_module_info);
+#endif
+
+#if defined(LOOKUP_DSEARCH) && LOOKUP_DSEARCH!=2
+  addlookupmodule(NULL, &dsearch_lookup_module_info);
+#endif
+
+#if defined(LOOKUP_IBASE) && LOOKUP_IBASE!=2
+  addlookupmodule(NULL, &ibase_lookup_module_info);
+#endif
+
+#ifdef LOOKUP_LDAP
+  addlookupmodule(NULL, &ldap_lookup_module_info);
+#endif
+
+#if defined(LOOKUP_LSEARCH) && LOOKUP_LSEARCH!=2
+  addlookupmodule(NULL, &lsearch_lookup_module_info);
+#endif
+
+#if defined(LOOKUP_MYSQL) && LOOKUP_MYSQL!=2
+  addlookupmodule(NULL, &mysql_lookup_module_info);
+#endif
+
+#if defined(LOOKUP_NIS) && LOOKUP_NIS!=2
+  addlookupmodule(NULL, &nis_lookup_module_info);
+#endif
+
+#if defined(LOOKUP_NISPLUS) && LOOKUP_NISPLUS!=2
+  addlookupmodule(NULL, &nisplus_lookup_module_info);
+#endif
+
+#if defined(LOOKUP_ORACLE) && LOOKUP_ORACLE!=2
+  addlookupmodule(NULL, &oracle_lookup_module_info);
+#endif
+
+#if defined(LOOKUP_PASSWD) && LOOKUP_PASSWD!=2
+  addlookupmodule(NULL, &passwd_lookup_module_info);
+#endif
+
+#if defined(LOOKUP_PGSQL) && LOOKUP_PGSQL!=2
+  addlookupmodule(NULL, &pgsql_lookup_module_info);
+#endif
+
+#ifdef EXPERIMENTAL_REDIS
+  addlookupmodule(NULL, &redis_lookup_module_info);
+#endif
+
+#ifdef EXPERIMENTAL_SPF
+  addlookupmodule(NULL, &spf_lookup_module_info);
+#endif
+
+#if defined(LOOKUP_SQLITE) && LOOKUP_SQLITE!=2
+  addlookupmodule(NULL, &sqlite_lookup_module_info);
+#endif
+
+#if defined(LOOKUP_TESTDB) && LOOKUP_TESTDB!=2
+  addlookupmodule(NULL, &testdb_lookup_module_info);
+#endif
+
+#if defined(LOOKUP_WHOSON) && LOOKUP_WHOSON!=2
+  addlookupmodule(NULL, &whoson_lookup_module_info);
+#endif
+
+#ifdef LOOKUP_MODULE_DIR
+  dd = opendir(LOOKUP_MODULE_DIR);
+  if (dd == NULL) {
+    DEBUG(D_lookup) debug_printf("Couldn't open %s: not loading lookup modules\n", LOOKUP_MODULE_DIR);
+    log_write(0, LOG_MAIN, "Couldn't open %s: not loading lookup modules\n", LOOKUP_MODULE_DIR);
+  }
+  else {
+    DEBUG(D_lookup) debug_printf("Loading lookup modules from %s\n", LOOKUP_MODULE_DIR);
+    while ((ent = readdir(dd)) != NULL) {
+      char *name = ent->d_name;
+      int len = (int)strlen(name);
+      if (pcre_exec(regex_islookupmod, NULL, name, len, 0, PCRE_EOPT, NULL, 0) >= 0) {
+        int pathnamelen = len + (int)strlen(LOOKUP_MODULE_DIR) + 2;
+        void *dl;
+        struct lookup_module_info *info;
+        const char *errormsg;
+
+        /* SRH: am I being paranoid here or what? */
+        if (pathnamelen > big_buffer_size) {
+          fprintf(stderr, "Loading lookup modules: %s/%s: name too long\n", LOOKUP_MODULE_DIR, name);
+          log_write(0, LOG_MAIN|LOG_PANIC, "%s/%s: name too long\n", LOOKUP_MODULE_DIR, name);
+          continue;
+        }
+
+        /* SRH: snprintf here? */
+        sprintf(CS big_buffer, "%s/%s", LOOKUP_MODULE_DIR, name);
+
+        dl = dlopen(CS big_buffer, RTLD_NOW);// TJ was LAZY
+        if (dl == NULL) {
+          fprintf(stderr, "Error loading %s: %s\n", name, dlerror());
+          moduleerrors++;
+          log_write(0, LOG_MAIN|LOG_PANIC, "Error loading lookup module %s: %s\n", name, dlerror());
+          continue;
+        }
+
+        /* FreeBSD nsdispatch() can trigger dlerror() errors about
+         * _nss_cache_cycle_prevention_function; we need to clear the dlerror()
+         * state before calling dlsym(), so that any error afterwards only
+         * comes from dlsym().
+         */
+        errormsg = dlerror();
+
+        info = (struct lookup_module_info*) dlsym(dl, "_lookup_module_info");
+        if ((errormsg = dlerror()) != NULL) {
+          fprintf(stderr, "%s does not appear to be a lookup module (%s)\n", name, errormsg);
+          dlclose(dl);
+          moduleerrors++;
+          log_write(0, LOG_MAIN|LOG_PANIC, "%s does not appear to be a lookup module (%s)\n", name, errormsg);
+          continue;
+        }
+        if (info->magic != LOOKUP_MODULE_INFO_MAGIC) {
+          fprintf(stderr, "Lookup module %s is not compatible with this version of Exim\n", name);
+          dlclose(dl);
+          moduleerrors++;
+          log_write(0, LOG_MAIN|LOG_PANIC, "Lookup module %s is not compatible with this version of Exim\n", name);
+          continue;
+        }
+
+        addlookupmodule(dl, info);
+        DEBUG(D_lookup) debug_printf("Loaded \"%s\" (%d lookup types)\n", name, info->lookupcount);
+        countmodules++;
+      }
+    }
+    closedir(dd);
+  }
+
+  DEBUG(D_lookup) debug_printf("Loaded %d lookup modules\n", countmodules);
+#endif
+
+  store_free((void*)regex_islookupmod);
+
+  DEBUG(D_lookup) debug_printf("Total %d lookups\n", lookup_list_count);
+
+  lookup_list = store_malloc(sizeof(lookup_info *) * lookup_list_count);
+  memset(lookup_list, 0, sizeof(lookup_info *) * lookup_list_count);
+
+  /* now add all lookups to the real list */
+  p = lookupmodules;
+  while (p) {
+    int j;
+    struct lookupmodulestr *pnext;
+
+    for (j = 0; j < p->info->lookupcount; j++)
+      add_lookup_to_list(p->info->lookups[j]);
+
+    pnext = p->next;
+    store_free(p);
+    p = pnext;
+  }
+  /* just to be sure */
+  lookupmodules = NULL;
+}
+
+/* End of drtables.c */
diff --git a/src/dummies.c b/src/dummies.c
new file mode 100644 (file)
index 0000000..d176bef
--- /dev/null
@@ -0,0 +1,148 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* This file is not part of the main Exim code. There are little bits of test
+code for some of Exim's modules, and when they are used, the module they are
+testing may call other main Exim functions that are not available and/or
+should not be used in a test. The classic case is log_write(). This module
+contains dummy versions of such functions - well not really dummies, more like
+alternates. */
+
+#include <stdarg.h>
+#include <stdio.h>
+#include <errno.h>
+#include <string.h>
+
+/* We don't have the full Exim headers dragged in, but this function
+is used for debugging output. */
+
+extern int  string_vformat(char *, int, char *, va_list);
+
+
+/*************************************************
+*         Handle calls to write the log          *
+*************************************************/
+
+/* The message gets written to stderr when log_write() is called from a
+utility. The message always gets '\n' added on the end of it.
+
+Arguments:
+  selector  not relevant when running a utility
+  flags     not relevant when running a utility
+  format    a printf() format
+  ...       arguments for format
+
+Returns:    nothing
+*/
+
+void
+log_write(unsigned int selector, int flags, char *format, ...)
+{
+va_list ap;
+va_start(ap, format);
+vfprintf(stderr, format, ap);
+fprintf(stderr, "\n");
+va_end(ap);
+selector = selector;     /* Keep picky compilers happy */
+flags = flags;
+}
+
+
+/*************************************************
+*      Handle calls to print debug output        *
+*************************************************/
+
+/* The message just gets written to stderr
+
+Arguments:
+  format    a printf() format
+  ...       arguments for format
+
+Returns:    nothing
+*/
+
+void
+debug_printf(char *format, ...)
+{
+va_list ap;
+char buffer[1024];
+
+va_start(ap, format);
+
+if (!string_vformat(buffer, sizeof(buffer), format, ap))
+  {
+  char *s = "**** debug string overflowed buffer ****\n";
+  char *p = buffer + (int)strlen(buffer);
+  int maxlen = sizeof(buffer) - (int)strlen(s) - 3;
+  if (p > buffer + maxlen) p = buffer + maxlen;
+  if (p > buffer && p[-1] != '\n') *p++ = '\n';
+  strcpy(p, s);
+  }
+
+fprintf(stderr, "%s", buffer);
+fflush(stderr);
+va_end(ap);
+}
+
+
+
+/*************************************************
+*              SIGALRM handler                   *
+*************************************************/
+
+extern int sigalrm_seen;
+
+void
+sigalrm_handler(int sig)
+{
+sig = sig;            /* Keep picky compilers happy */
+sigalrm_seen = 1;
+}
+
+
+
+/*************************************************
+*              Complete Dummies                  *
+*************************************************/
+
+int
+header_checkname(void *h, char *name, int len)
+{
+h = h;            /* Keep picky compilers happy */
+name = name;
+len = len;
+return 0;
+}
+
+void
+directory_make(char *parent, char *name, int mode, int panic)
+{
+parent = parent;  /* Keep picky compilers happy */
+name = name;
+mode = mode;
+panic = panic;
+}
+
+void
+host_build_sender_fullhost(void) { }
+
+/* This one isn't needed for test_host */
+
+#ifndef TEST_HOST
+char *
+host_ntoa(int type, const void *arg, char *buffer, int *portptr)
+{
+type = type;      /* Keep picky compilers happy */
+arg = arg;
+buffer = buffer;
+portptr = portptr;
+return NULL;
+}
+#endif
+
+
+/* End of dummies.c */
diff --git a/src/enq.c b/src/enq.c
new file mode 100644 (file)
index 0000000..6cd243c
--- /dev/null
+++ b/src/enq.c
@@ -0,0 +1,100 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Functions concerned with serialization. */
+
+
+#include "exim.h"
+
+
+
+
+/*************************************************
+*       Test for host or ETRN serialization      *
+*************************************************/
+
+/* This function is called when a host is listed for serialization of
+connections. It is also called when ETRN is listed for serialization. We open
+the misc database and look for a record, which implies an existing connection
+or ETRN run. If not found, create one and return TRUE.
+
+Arguments:
+  key            string on which to serialize
+
+Returns:         TRUE if OK to proceed; FALSE otherwise
+*/
+
+
+BOOL
+enq_start(uschar *key)
+{
+dbdata_serialize *serial_record;
+dbdata_serialize new_record;
+open_db dbblock;
+open_db *dbm_file;
+
+DEBUG(D_transport) debug_printf("check serialized: %s\n", key);
+
+/* Open and lock the waiting information database. The absence of O_CREAT is
+deliberate; the dbfn_open() function - which is an Exim function - always tries
+to create if it can't open a read/write file. It expects only O_RDWR or
+O_RDONLY as its argument. */
+
+dbm_file = dbfn_open(US"misc", O_RDWR, &dbblock, TRUE);
+if (dbm_file == NULL) return FALSE;
+
+/* See if there is a record for this host or queue run; if there is, we cannot
+proceed with the connection unless the record is very old. */
+
+serial_record = dbfn_read(dbm_file, key);
+if (serial_record != NULL && time(NULL) - serial_record->time_stamp < 6*60*60)
+  {
+  dbfn_close(dbm_file);
+  DEBUG(D_transport) debug_printf("outstanding serialization record for %s\n",
+    key);
+  return FALSE;
+  }
+
+/* We can proceed - insert a new record or update the old one. At present
+the count field is not used; just set it to 1. */
+
+new_record.count = 1;
+dbfn_write(dbm_file, key, &new_record, (int)sizeof(dbdata_serialize));
+dbfn_close(dbm_file);
+return TRUE;
+}
+
+
+
+/*************************************************
+*              Release serialization             *
+*************************************************/
+
+/* This function is called when a serialized host's connection or serialized
+ETRN queue run ends. We open the relevant database and delete its record.
+
+Arguments:
+  key          the serialization key
+
+Returns:       nothing
+*/
+
+void
+enq_end(uschar *key)
+{
+open_db dbblock;
+open_db *dbm_file;
+
+DEBUG(D_transport) debug_printf("end serialized: %s\n", key);
+
+dbm_file = dbfn_open(US"misc", O_RDWR, &dbblock, TRUE);
+if (dbm_file == NULL) return;
+dbfn_delete(dbm_file, key);
+dbfn_close(dbm_file);
+}
+
+/* End of enq.c */
diff --git a/src/exicyclog.src b/src/exicyclog.src
new file mode 100644 (file)
index 0000000..01d1f2f
--- /dev/null
@@ -0,0 +1,339 @@
+#! /bin/sh
+
+# Copyright (c) University of Cambridge, 1995 - 2007
+# See the file NOTICE for conditions of use and distribution.
+
+# This script takes the following command line arguments:
+# -l dir       Log file directory
+# -k days      Number of days to keep the log files
+
+# Except when they appear in comments, the following placeholders in this
+# source are replaced when it is turned into a runnable script:
+#
+# CONFIGURE_FILE_USE_NODE
+# CONFIGURE_FILE_USE_EUID
+# CONFIGURE_FILE
+# BIN_DIRECTORY
+# EXICYCLOG_MAX
+# COMPRESS_COMMAND
+# COMPRESS_SUFFIX
+# CHOWN_COMMAND
+# CHGRP_COMMAND
+# CHMOD_COMMAND
+# TOUCH_COMMAND
+# MV_COMMAND
+# RM_COMMAND
+
+# PROCESSED_FLAG
+
+# This is a shell script for cycling exim main and reject log files. Each time
+# it is run, the files get "shuffled down" by one, the current one (e.g.
+# mainlog) becoming mainlog.01, the previous mainlog.01 becoming mainlog.02,
+# and so on, up to the limit configured here. When the number to keep is
+# greater than 99 (not common, but some people do it), three digits are used
+# (e.g. mainlog.001). The same shuffling happens to the reject logs. All
+# renamed files with numbers greater than 1 are compressed.
+
+# This script should be called regularly (e.g. daily) by a root crontab
+# entry of the form
+
+# 1 0 * * *   /opt/exim/bin/exicyclog
+
+# The following lines are generated from Exim's configuration file when
+# this source is built into a script, but you can subsequently edit them
+# without rebuilding things, as long are you are careful not to overwrite
+# the script in the next Exim rebuild/install. "Keep" is the number of old log
+# files that are required to be kept. Its value can be overridden by the -k
+# command line option. "Compress" and "suffix" define your chosen compression
+# method. The others are provided because the location of certain commands
+# varies from OS to OS. Sigh.
+
+keep=EXICYCLOG_MAX
+compress=COMPRESS_COMMAND
+suffix=COMPRESS_SUFFIX
+
+chgrp=CHGRP_COMMAND
+chmod=CHMOD_COMMAND
+chown=CHOWN_COMMAND
+mv=MV_COMMAND
+rm=RM_COMMAND
+touch=TOUCH_COMMAND
+
+# End of editable lines
+#########################################################################
+
+# Sort out command line options.
+
+while [ $# -gt 0 ] ; do
+  case "$1" in
+  -l) log_file_path=$2
+      shift
+      ;;
+  -k) keep=$2
+      shift
+      ;;
+   *) echo "** exicyclog: unknown option $1"
+      exit 1
+      ;;
+   esac
+   shift
+done
+
+# Some operating systems have different versions in which the commands live
+# in different places. We have a fudge that will search the usual suspects if
+# requested.
+
+for cmd in chgrp chmod chown mv rm touch; do
+  eval "oldcmd=\$$cmd"
+  if [ "$oldcmd" != "look_for_it" ] ; then continue ; fi
+  newcmd=$cmd
+  for dir in /bin /usr/bin /usr/sbin /usr/etc ; do
+    if [ -f $dir/$cmd ] ; then
+      newcmd=$dir/$cmd
+      break
+    fi
+  done
+  eval $cmd=$newcmd
+done
+
+# See if this installation is using the esoteric "USE_EUID" feature of Exim,
+# in which it uses the effective user id as a suffix for the configuration file
+# name. In order for this to work, exicyclog must be run under the appropriate
+# euid.
+
+if [ "CONFIGURE_FILE_USE_EUID" = "yes" ]; then
+  euid=.`id -u`
+fi
+
+# See if this installation is using the esoteric "USE_NODE" feature of Exim,
+# in which it uses the host's name as a suffix for the configuration file name.
+
+if [ "CONFIGURE_FILE_USE_NODE" = "yes" ]; then
+  hostsuffix=.`uname -n`
+fi
+
+# Now find the configuration file name. This has got complicated because the
+# CONFIGURE_FILE value may now be a list of files. The one that is used is the
+# first one that exists. Mimic the code in readconf.c by testing first for the
+# suffixed file in each case.
+
+set `awk -F: '{ for (i = 1; i <= NF; i++) print $i }' <<End
+CONFIGURE_FILE
+End
+`
+while [ "$config" = "" -a $# -gt 0 ] ; do
+  if [ -f "$1$euid$hostsuffix" ] ; then
+    config="$1$euid$hostsuffix"
+  elif [ -f "$1$euid" ] ; then
+    config="$1$euid"
+  elif [ -f "$1$hostsuffix" ] ; then
+    config="$1$hostsuffix"
+  elif [ -f "$1" ] ; then
+    config="$1"
+  fi
+  shift
+done
+
+# Determine if the log file path is set, and where the spool directory is.
+# Search for an exim_path setting in the configure file; otherwise use the bin
+# directory. Call that version of Exim to find the spool directory and log file
+# path, unless log_file_path was set above by a command line option. BEWARE: a
+# tab character is needed in the command below. It has had a nasty tendency to
+# get lost in the past. Use a variable to hold a space and a tab to keep the
+# tab in one place.
+
+st='    '
+exim_path=`grep "^[$st]*exim_path" $config | sed "s/.*=[$st]*//"`
+if test "$exim_path" = ""; then exim_path=BIN_DIRECTORY/exim; fi
+
+spool_directory=`$exim_path -C $config -bP spool_directory | sed 's/.*=[  ]*//'`
+
+if [ "$log_file_path" = "" ] ; then
+  log_file_path=`$exim_path -C $config -bP log_file_path | sed 's/.*=[  ]*//'`
+fi
+
+# If log_file_path contains only "syslog" then no Exim log files are in use.
+# We can't cycle anything. Complain and give up.
+
+if [ "$log_file_path" = "syslog" ] ; then
+  echo "*** Exim is logging to syslog - no log files to cycle ***"
+  exit 1
+fi
+
+# Otherwise, remove ":syslog" or "syslog:" (some spaces allowed) and inspect
+# what remains. The simplistic regex originally used failed when a filename
+# contained "syslog", so we have to use three less general ones, because sed
+# doesn't have much power in its regexs.
+
+log_file_path=`echo "$log_file_path" | \
+  sed 's/^ *:\{0,1\} *syslog *:\{0,1\} *//;s/: *syslog *:/:/;s/: *syslog *$//'`
+
+# If log_file_path is empty, try and get the compiled in default by using
+# /dev/null as the configuration file.
+
+if [ "$log_file_path" = "" ]; then
+  log_file_path=`$exim_path -C /dev/null -bP log_file_path | sed 's/.*=[  ]*//'`
+  log_file_path=`echo "$log_file_path" | \
+    sed 's/^ *:\{0,1\} *syslog *:\{0,1\} *//;s/: *syslog *:/:/;s/: *syslog *$//'`
+fi
+
+# If log_file_path is still empty, the logs we are interested in are probably
+# called "mainlog" and "rejectlog" in the directory called "log" in the spool
+# directory. Otherwise we fish out the directory from the given path, and also
+# the names of the logs.
+
+if [ "$log_file_path" = "" ]; then
+  logdir=$spool_directory/log
+  mainlog=mainlog
+  rejectlog=rejectlog
+  paniclog=paniclog
+else
+  logdir=`echo $log_file_path | sed 's?/[^/]*$??'`
+  logbase=`echo $log_file_path | sed 's?^.*/??'`
+  mainlog=`echo $logbase | sed 's/%s/main/'`
+  rejectlog=`echo $logbase | sed 's/%s/reject/'`
+  paniclog=`echo $logbase | sed 's/%s/panic/'`
+fi
+
+# Get into the log directory to do the business.
+
+cd $logdir || exit 1
+
+# If there is no main log file, do nothing.
+
+if [ ! -f $mainlog ]; then exit; fi
+
+# Find out the owner and group of the main log file so that we can re-instate
+# this on moved and compressed files, since some operating systems may change
+# things. This is a tedious bit of code, but it should work both in operating
+# systems where the -l option of ls gives the user and group, and those in which
+# you need -lg. The condition is that, if the fifth field of the output from
+# ls consists entirely of digits, then the third and fourth fields are the user
+# and group.
+
+a=`ls -lg $mainlog`
+b=`ls -l  $mainlog`
+
+# These statements work fine in the Bourne or Korn shells, but not in Bash.
+# So for the benefit of systems whose /bin/sh is really Bash, they have been
+# changed to a messier form.
+
+# user=`echo "$a\n$b\n" | awk 'BEGIN { OFS=""} { if ($5 ~ /^[0-9]+$/) print $3; }'`
+# group=`echo "$a\n$b\n" | awk 'BEGIN { OFS=""} { if ($5 ~ /^[0-9]+$/) print $4; }'`
+
+user=`echo "$a
+$b
+" | awk 'BEGIN { OFS=""} { if ($5 ~ /^[0-9]+$/) { print $3; exit; } }'`
+
+group=`echo "$a
+$b
+" | awk 'BEGIN { OFS=""} { if ($5 ~ /^[0-9]+$/) { print $4; exit; } }'`
+
+# Now do the job. First remove the files that have "fallen off the bottom".
+# Look for both the compressed and uncompressed forms.
+
+if [ $keep -lt 10 ]; then keept=0$keep; else keept=$keep; fi;
+
+if [ -f $mainlog.$keept ]; then $rm $mainlog.$keept; fi;
+if [ -f $mainlog.$keept.$suffix ]; then $rm $mainlog.$keept.$suffix; fi;
+
+if [ -f $rejectlog.$keept ]; then $rm $rejectlog.$keept; fi;
+if [ -f $rejectlog.$keept.$suffix ]; then $rm $rejectlog.$keept.$suffix; fi;
+
+if [ -f $paniclog.$keept ]; then $rm $paniclog.$keept; fi;
+if [ -f $paniclog.$keept.$suffix ]; then $rm $paniclog.$keept.$suffix; fi;
+
+# Now rename all the previous old files by increasing their numbers by 1.
+# When the number is less than 10, insert a leading zero.
+
+count=$keep
+if [ $count -lt 10 ]; then countt=0$count; else countt=$count; fi
+
+while [ $count -gt 1 ]; do
+  old=`expr -- $count - 1`
+  if [ $keep -gt 99 ]; then
+    if   [ $old -lt 10 ]; then oldt=00$old
+    elif [ $old -lt 100 ]; then oldt=0$old
+    else oldt=$old
+    fi
+  else
+    if [ $old -lt 10 ]; then oldt=0$old; else oldt=$old; fi;
+  fi
+  if [ -f $mainlog.$oldt ]; then
+    $mv $mainlog.$oldt $mainlog.$countt
+  elif [ -f $mainlog.$oldt.$suffix ]; then
+    $mv $mainlog.$oldt.$suffix $mainlog.$countt.$suffix
+  fi
+  if [ -f $rejectlog.$oldt ]; then
+    $mv $rejectlog.$oldt $rejectlog.$countt
+  elif [ -f $rejectlog.$oldt.$suffix ]; then
+    $mv $rejectlog.$oldt.$suffix $rejectlog.$countt.$suffix
+  fi
+  if [ -f $paniclog.$oldt ]; then
+    $mv $paniclog.$oldt $paniclog.$countt
+  elif [ -f $paniclog.$oldt.$suffix ]; then
+    $mv $paniclog.$oldt.$suffix $paniclog.$countt.$suffix
+  fi
+  count=$old
+  countt=$oldt
+done
+
+# Now rename the current files as 01 or 001 if keeping more than 99
+
+if [ $keep -gt 99 ]; then first=001; else first=01; fi
+
+if [ -f $mainlog ]; then
+  $mv $mainlog $mainlog.$first
+  $chown $user:$group $mainlog.$first
+  $touch $mainlog
+  $chown $user:$group $mainlog
+  $chmod 640 $mainlog
+fi
+
+if [ -f $rejectlog ]; then
+  $mv $rejectlog $rejectlog.$first
+  $chown $user:$group $rejectlog.$first
+  $touch $rejectlog
+  $chown $user:$group $rejectlog
+  $chmod 640 $rejectlog
+fi
+
+if [ -f $paniclog ]; then
+  $mv $paniclog $paniclog.$first
+  $chown $user:$group $paniclog.$first
+  $touch $paniclog
+  $chown $user:$group $paniclog
+  $chmod 640 $paniclog
+fi
+
+# Now scan the (0)02 and later files, compressing where necessary, and
+# ensuring that their owners and groups are correct.
+
+count=2;
+
+while [ $count -le $keep ]; do
+  if [ $keep -gt 99 ]; then
+    if   [ $count -lt 10 ]; then countt=00$count
+    elif [ $count -lt 100 ]; then countt=0$count
+    else countt=$count
+    fi
+  else
+    if [ $count -lt 10 ]; then countt=0$count; else countt=$count; fi
+  fi
+  if [ -f $mainlog.$countt ]; then $compress $mainlog.$countt; fi
+  if [ -f $mainlog.$countt.$suffix ]; then
+    $chown $user:$group $mainlog.$countt.$suffix
+  fi
+  if [ -f $rejectlog.$countt ]; then $compress $rejectlog.$countt; fi
+  if [ -f $rejectlog.$countt.$suffix ]; then
+    $chown $user:$group $rejectlog.$countt.$suffix
+  fi
+  if [ -f $paniclog.$countt ]; then $compress $paniclog.$countt; fi
+  if [ -f $paniclog.$countt.$suffix ]; then
+    $chown $user:$group $paniclog.$countt.$suffix
+  fi
+
+  count=`expr -- $count + 1`
+done
+
+# End of exicyclog
diff --git a/src/exigrep.src b/src/exigrep.src
new file mode 100644 (file)
index 0000000..2d3b40c
--- /dev/null
@@ -0,0 +1,230 @@
+#! PERL_COMMAND -w
+
+use strict;
+
+# Copyright (c) 2007-2014 University of Cambridge.
+# See the file NOTICE for conditions of use and distribution.
+
+# Except when they appear in comments, the following placeholders in this
+# source are replaced when it is turned into a runnable script:
+#
+# PERL_COMMAND
+# ZCAT_COMMAND
+# COMPRESS_SUFFIX
+
+# PROCESSED_FLAG
+
+# This is a perl script which extracts from an Exim log all entries
+# for all messages that have an entry that matches a given pattern.
+# If *any* entry for a particular message matches the pattern, *all*
+# entries for that message are displayed.
+
+# We buffer up information on a per-message basis. It is done this way rather
+# than reading the input twice so that the input can be a pipe.
+
+# There must be one argument, which is the pattern. Subsequent arguments
+# are the files to scan; if none, the standard input is read. If any file
+# appears to be compressed, it is passed through zcat. We can't just do this
+# for all files, because zcat chokes on non-compressed files.
+
+# Performance optimized in 02/02/2007 by Jori Hamalainen
+# Typical run time acceleration: 4 times
+
+
+use Getopt::Std qw(getopts);
+use POSIX qw(mktime);
+
+
+# This subroutine converts a time/date string from an Exim log line into
+# the number of seconds since the epoch. It handles optional timezone
+# information.
+
+sub seconds {
+my($year,$month,$day,$hour,$min,$sec,$tzs,$tzh,$tzm) =
+  $_[0] =~ /^(\d{4})-(\d\d)-(\d\d)\s(\d\d):(\d\d):(\d\d)(?>\s([+-])(\d\d)(\d\d))?/o;
+
+my $seconds = mktime $sec, $min, $hour, $day, $month - 1, $year - 1900;
+
+if (defined $tzs)
+  {
+  $seconds -= $tzh * 3600 + $tzm * 60 if $tzs eq "+";
+  $seconds += $tzh * 3600 + $tzm * 60 if $tzs eq "-";
+  }
+
+return $seconds;
+}
+
+
+# This subroutine processes a single line (in $_) from a log file. Program
+# defensively against short lines finding their way into the log.
+
+my (%saved, %id_list, $pattern, $queue_time, $insensitive, $invert);
+
+sub do_line {
+
+# Convert syslog lines to mainlog format, as in eximstats.
+
+if (!/^\d{4}-/o) { $_ =~ s/^.*? exim\b.*?: //o; }
+
+return unless
+  my($date,$id) = /^(\d{4}-\d\d-\d\d \d\d:\d\d:\d\d (?:[+-]\d{4} )?)(?:\[\d+\] )?(\w{6}\-\w{6}\-\w{2})?/o;
+
+# Handle the case when the log line belongs to a specific message. We save
+# lines for specific messages until the message is complete. Then either print
+# or discard.
+
+if (defined $id)
+  {
+  $saved{$id} = '' unless defined($saved{$id});
+
+  # Save up the data for this message in case it becomes interesting later.
+
+  $saved{$id} .= $_;
+
+  # Are we interested in this id ? Short circuit if we already were interested.
+
+  if ($invert)
+    {
+    $id_list{$id} = 1 if (!defined($id_list{$id}));
+    $id_list{$id} = 0 if (($insensitive && /$pattern/io) || /$pattern/o);
+    }
+  else
+    {
+    $id_list{$id} = 1 if defined $id_list{$id} ||
+      ($insensitive && /$pattern/io) || /$pattern/o;
+    }
+
+  # See if this is a completion for some message. If it is interesting,
+  # print it, but in any event, throw away what was saved.
+
+  if (index($_, 'Completed') != -1 ||
+      index($_, 'SMTP data timeout') != -1 ||
+        (index($_, 'rejected') != -1 &&
+          /^(\d{4}-\d\d-\d\d \d\d:\d\d:\d\d (?:[+-]\d{4} )?)(?:\[\d+\] )?\w{6}\-\w{6}\-\w{2} rejected/o))
+    {
+    if ($queue_time != -1 &&
+        $saved{$id} =~ /^(\d{4}-\d\d-\d\d \d\d:\d\d:\d\d ([+-]\d{4} )?)/o)
+      {
+      my $old_sec = &seconds($1);
+      my $sec = &seconds($date);
+      $id_list{$id} = 0 if $id_list{$id} && $sec - $old_sec <= $queue_time;
+      }
+
+    print "$saved{$id}\n" if ($id_list{$id});
+    delete $id_list{$id};
+    delete $saved{$id};
+    }
+  }
+
+# Handle the case where the log line does not belong to a specific message.
+# Print it if it is interesting.
+
+elsif ( ($invert && (($insensitive && !/$pattern/io) || !/$pattern/o)) ||
+       (!$invert && (($insensitive &&  /$pattern/io) ||  /$pattern/o)) )
+  { print "$_\n"; }
+}
+
+# Rotated log files are frequently compressed and there are a variety of
+# formats it could be compressed with. Rather than use just one that is
+# detected and hardcoded at Exim compile time, detect and use what the
+# logfile is compressed with on the fly.
+#
+# List of known compression extensions and their associated commands:
+my $compressors = {
+  gz   => { cmd => 'zcat',  args => '' },
+  bz2  => { cmd => 'bzcat', args => '' },
+  xz   => { cmd => 'xzcat', args => '' },
+  lzma => { cmd => 'lzma',  args => '-dc' }
+};
+my $csearch = 0;
+
+sub detect_compressor_bin
+  {
+  my $ext = shift();
+  my $c = $compressors->{$ext}->{cmd};
+  $compressors->{$ext}->{bin} = `which $c 2>/dev/null`;
+  chomp($compressors->{$ext}->{bin});
+  }
+
+sub detect_compressor_capable
+  {
+  my $filename = shift();
+  map { &detect_compressor_bin($_) } keys %$compressors
+    if (!$csearch);
+  $csearch = 1;
+  return undef
+    unless (grep {$filename =~ /\.(?:$_)$/} keys %$compressors);
+  # Loop through them, figure out which one it detected,
+  # and build the commandline.
+  my $cmdline = undef;
+  foreach my $ext (keys %$compressors)
+    {
+    if ($filename =~ /\.(?:$ext)$/)
+      {
+      # Just die if compressor not found; if this occurrs in the middle of
+      # two valid files with a lot of matches, error could easily be missed.
+      die("Didn't find $ext decompressor for $filename\n")
+        if ($compressors->{$ext}->{bin} eq '');
+      $cmdline = $compressors->{$ext}->{bin} ." ".
+                   $compressors->{$ext}->{args};
+      last;
+      }
+    }
+  return $cmdline;
+  }
+
+# The main program. Extract the pattern and make sure any relevant characters
+# are quoted if the -l flag is given. The -t flag gives a time-on-queue value
+# which is an additional condition.
+
+getopts('Ilvt:',\my %args);
+$queue_time  = $args{'t'}? $args{'t'} : -1;
+$insensitive = $args{'I'}? 0 : 1;
+$invert      = $args{'v'}? 1 : 0;
+
+die "usage: exigrep [-I] [-l] [-t <seconds>] [-v] <pattern> [<log file>]...\n"
+  if ($#ARGV < 0);
+
+$pattern = shift @ARGV;
+$pattern = quotemeta $pattern if $args{l};
+
+
+# If file arguments are given, open each one and process according as it is
+# is compressed or not.
+
+if (@ARGV)
+  {
+  foreach (@ARGV)
+    {
+    my $filename = $_;
+    if ($filename =~ /\.(?:COMPRESS_SUFFIX)$/o)
+      {
+      open(LOG, "ZCAT_COMMAND $filename |") ||
+        die "Unable to zcat $filename: $!\n";
+      }
+    elsif (my $cmdline = &detect_compressor_capable($filename))
+      {
+      open(LOG, "$cmdline $filename |") ||
+        die "Unable to decompress $filename: $!\n";
+      }
+    else
+      {
+      open(LOG, "<$filename") || die "Unable to open $filename: $!\n";
+      }
+    do_line() while (<LOG>);
+    close(LOG);
+    }
+  }
+
+# If no files are named, process STDIN only
+
+else { do_line() while (<STDIN>); }
+
+# At the end of processing all the input, print any uncompleted messages.
+
+for (keys %id_list)
+  {
+  print "+++ $_ has not completed +++\n$saved{$_}\n";
+  }
+
+# End of exigrep
diff --git a/src/exim.c b/src/exim.c
new file mode 100644 (file)
index 0000000..517b543
--- /dev/null
@@ -0,0 +1,5613 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2014 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+
+/* The main function: entry point, initialization, and high-level control.
+Also a few functions that don't naturally fit elsewhere. */
+
+
+#include "exim.h"
+
+extern void init_lookup_list(void);
+
+
+
+/*************************************************
+*      Function interface to store functions     *
+*************************************************/
+
+/* We need some real functions to pass to the PCRE regular expression library
+for store allocation via Exim's store manager. The normal calls are actually
+macros that pass over location information to make tracing easier. These
+functions just interface to the standard macro calls. A good compiler will
+optimize out the tail recursion and so not make them too expensive. There
+are two sets of functions; one for use when we want to retain the compiled
+regular expression for a long time; the other for short-term use. */
+
+static void *
+function_store_get(size_t size)
+{
+return store_get((int)size);
+}
+
+static void
+function_dummy_free(void *block) { block = block; }
+
+static void *
+function_store_malloc(size_t size)
+{
+return store_malloc((int)size);
+}
+
+static void
+function_store_free(void *block)
+{
+store_free(block);
+}
+
+
+
+
+/*************************************************
+*         Enums for cmdline interface            *
+*************************************************/
+
+enum commandline_info { CMDINFO_NONE=0,
+  CMDINFO_HELP, CMDINFO_SIEVE, CMDINFO_DSCP };
+
+
+
+
+/*************************************************
+*  Compile regular expression and panic on fail  *
+*************************************************/
+
+/* This function is called when failure to compile a regular expression leads
+to a panic exit. In other cases, pcre_compile() is called directly. In many
+cases where this function is used, the results of the compilation are to be
+placed in long-lived store, so we temporarily reset the store management
+functions that PCRE uses if the use_malloc flag is set.
+
+Argument:
+  pattern     the pattern to compile
+  caseless    TRUE if caseless matching is required
+  use_malloc  TRUE if compile into malloc store
+
+Returns:      pointer to the compiled pattern
+*/
+
+const pcre *
+regex_must_compile(uschar *pattern, BOOL caseless, BOOL use_malloc)
+{
+int offset;
+int options = PCRE_COPT;
+const pcre *yield;
+const uschar *error;
+if (use_malloc)
+  {
+  pcre_malloc = function_store_malloc;
+  pcre_free = function_store_free;
+  }
+if (caseless) options |= PCRE_CASELESS;
+yield = pcre_compile(CS pattern, options, (const char **)&error, &offset, NULL);
+pcre_malloc = function_store_get;
+pcre_free = function_dummy_free;
+if (yield == NULL)
+  log_write(0, LOG_MAIN|LOG_PANIC_DIE, "regular expression error: "
+    "%s at offset %d while compiling %s", error, offset, pattern);
+return yield;
+}
+
+
+
+
+/*************************************************
+*   Execute regular expression and set strings   *
+*************************************************/
+
+/* This function runs a regular expression match, and sets up the pointers to
+the matched substrings.
+
+Arguments:
+  re          the compiled expression
+  subject     the subject string
+  options     additional PCRE options
+  setup       if < 0 do full setup
+              if >= 0 setup from setup+1 onwards,
+                excluding the full matched string
+
+Returns:      TRUE or FALSE
+*/
+
+BOOL
+regex_match_and_setup(const pcre *re, uschar *subject, int options, int setup)
+{
+int ovector[3*(EXPAND_MAXN+1)];
+int n = pcre_exec(re, NULL, CS subject, Ustrlen(subject), 0,
+  PCRE_EOPT | options, ovector, sizeof(ovector)/sizeof(int));
+BOOL yield = n >= 0;
+if (n == 0) n = EXPAND_MAXN + 1;
+if (yield)
+  {
+  int nn;
+  expand_nmax = (setup < 0)? 0 : setup + 1;
+  for (nn = (setup < 0)? 0 : 2; nn < n*2; nn += 2)
+    {
+    expand_nstring[expand_nmax] = subject + ovector[nn];
+    expand_nlength[expand_nmax++] = ovector[nn+1] - ovector[nn];
+    }
+  expand_nmax--;
+  }
+return yield;
+}
+
+
+
+
+/*************************************************
+*            Set up processing details           *
+*************************************************/
+
+/* Save a text string for dumping when SIGUSR1 is received.
+Do checks for overruns.
+
+Arguments: format and arguments, as for printf()
+Returns:   nothing
+*/
+
+void
+set_process_info(const char *format, ...)
+{
+int len;
+va_list ap;
+sprintf(CS process_info, "%5d ", (int)getpid());
+len = Ustrlen(process_info);
+va_start(ap, format);
+if (!string_vformat(process_info + len, PROCESS_INFO_SIZE - len - 2, format, ap))
+  Ustrcpy(process_info + len, "**** string overflowed buffer ****");
+len = Ustrlen(process_info);
+process_info[len+0] = '\n';
+process_info[len+1] = '\0';
+process_info_len = len + 1;
+DEBUG(D_process_info) debug_printf("set_process_info: %s", process_info);
+va_end(ap);
+}
+
+
+
+
+/*************************************************
+*             Handler for SIGUSR1                *
+*************************************************/
+
+/* SIGUSR1 causes any exim process to write to the process log details of
+what it is currently doing. It will only be used if the OS is capable of
+setting up a handler that causes automatic restarting of any system call
+that is in progress at the time.
+
+This function takes care to be signal-safe.
+
+Argument: the signal number (SIGUSR1)
+Returns:  nothing
+*/
+
+static void
+usr1_handler(int sig)
+{
+int fd;
+
+os_restarting_signal(sig, usr1_handler);
+
+fd = Uopen(process_log_path, O_APPEND|O_WRONLY, LOG_MODE);
+if (fd < 0)
+  {
+  /* If we are already running as the Exim user, try to create it in the
+  current process (assuming spool_directory exists). Otherwise, if we are
+  root, do the creation in an exim:exim subprocess. */
+
+  int euid = geteuid();
+  if (euid == exim_uid)
+    fd = Uopen(process_log_path, O_CREAT|O_APPEND|O_WRONLY, LOG_MODE);
+  else if (euid == root_uid)
+    fd = log_create_as_exim(process_log_path);
+  }
+
+/* If we are neither exim nor root, or if we failed to create the log file,
+give up. There is not much useful we can do with errors, since we don't want
+to disrupt whatever is going on outside the signal handler. */
+
+if (fd < 0) return;
+
+{int dummy = write(fd, process_info, process_info_len); dummy = dummy; }
+(void)close(fd);
+}
+
+
+
+/*************************************************
+*             Timeout handler                    *
+*************************************************/
+
+/* This handler is enabled most of the time that Exim is running. The handler
+doesn't actually get used unless alarm() has been called to set a timer, to
+place a time limit on a system call of some kind. When the handler is run, it
+re-enables itself.
+
+There are some other SIGALRM handlers that are used in special cases when more
+than just a flag setting is required; for example, when reading a message's
+input. These are normally set up in the code module that uses them, and the
+SIGALRM handler is reset to this one afterwards.
+
+Argument: the signal value (SIGALRM)
+Returns:  nothing
+*/
+
+void
+sigalrm_handler(int sig)
+{
+sig = sig;      /* Keep picky compilers happy */
+sigalrm_seen = TRUE;
+os_non_restarting_signal(SIGALRM, sigalrm_handler);
+}
+
+
+
+/*************************************************
+*      Sleep for a fractional time interval      *
+*************************************************/
+
+/* This function is called by millisleep() and exim_wait_tick() to wait for a
+period of time that may include a fraction of a second. The coding is somewhat
+tedious. We do not expect setitimer() ever to fail, but if it does, the process
+will wait for ever, so we panic in this instance. (There was a case of this
+when a bug in a function that calls milliwait() caused it to pass invalid data.
+That's when I added the check. :-)
+
+Argument:  an itimerval structure containing the interval
+Returns:   nothing
+*/
+
+static void
+milliwait(struct itimerval *itval)
+{
+sigset_t sigmask;
+sigset_t old_sigmask;
+(void)sigemptyset(&sigmask);                           /* Empty mask */
+(void)sigaddset(&sigmask, SIGALRM);                    /* Add SIGALRM */
+(void)sigprocmask(SIG_BLOCK, &sigmask, &old_sigmask);  /* Block SIGALRM */
+if (setitimer(ITIMER_REAL, itval, NULL) < 0)           /* Start timer */
+  log_write(0, LOG_MAIN|LOG_PANIC_DIE,
+    "setitimer() failed: %s", strerror(errno));
+(void)sigfillset(&sigmask);                            /* All signals */
+(void)sigdelset(&sigmask, SIGALRM);                    /* Remove SIGALRM */
+(void)sigsuspend(&sigmask);                            /* Until SIGALRM */
+(void)sigprocmask(SIG_SETMASK, &old_sigmask, NULL);    /* Restore mask */
+}
+
+
+
+
+/*************************************************
+*         Millisecond sleep function             *
+*************************************************/
+
+/* The basic sleep() function has a granularity of 1 second, which is too rough
+in some cases - for example, when using an increasing delay to slow down
+spammers.
+
+Argument:    number of millseconds
+Returns:     nothing
+*/
+
+void
+millisleep(int msec)
+{
+struct itimerval itval;
+itval.it_interval.tv_sec = 0;
+itval.it_interval.tv_usec = 0;
+itval.it_value.tv_sec = msec/1000;
+itval.it_value.tv_usec = (msec % 1000) * 1000;
+milliwait(&itval);
+}
+
+
+
+/*************************************************
+*         Compare microsecond times              *
+*************************************************/
+
+/*
+Arguments:
+  tv1         the first time
+  tv2         the second time
+
+Returns:      -1, 0, or +1
+*/
+
+int
+exim_tvcmp(struct timeval *t1, struct timeval *t2)
+{
+if (t1->tv_sec > t2->tv_sec) return +1;
+if (t1->tv_sec < t2->tv_sec) return -1;
+if (t1->tv_usec > t2->tv_usec) return +1;
+if (t1->tv_usec < t2->tv_usec) return -1;
+return 0;
+}
+
+
+
+
+/*************************************************
+*          Clock tick wait function              *
+*************************************************/
+
+/* Exim uses a time + a pid to generate a unique identifier in two places: its
+message IDs, and in file names for maildir deliveries. Because some OS now
+re-use pids within the same second, sub-second times are now being used.
+However, for absolute certaintly, we must ensure the clock has ticked before
+allowing the relevant process to complete. At the time of implementation of
+this code (February 2003), the speed of processors is such that the clock will
+invariably have ticked already by the time a process has done its job. This
+function prepares for the time when things are faster - and it also copes with
+clocks that go backwards.
+
+Arguments:
+  then_tv      A timeval which was used to create uniqueness; its usec field
+                 has been rounded down to the value of the resolution.
+                 We want to be sure the current time is greater than this.
+  resolution   The resolution that was used to divide the microseconds
+                 (1 for maildir, larger for message ids)
+
+Returns:       nothing
+*/
+
+void
+exim_wait_tick(struct timeval *then_tv, int resolution)
+{
+struct timeval now_tv;
+long int now_true_usec;
+
+(void)gettimeofday(&now_tv, NULL);
+now_true_usec = now_tv.tv_usec;
+now_tv.tv_usec = (now_true_usec/resolution) * resolution;
+
+if (exim_tvcmp(&now_tv, then_tv) <= 0)
+  {
+  struct itimerval itval;
+  itval.it_interval.tv_sec = 0;
+  itval.it_interval.tv_usec = 0;
+  itval.it_value.tv_sec = then_tv->tv_sec - now_tv.tv_sec;
+  itval.it_value.tv_usec = then_tv->tv_usec + resolution - now_true_usec;
+
+  /* We know that, overall, "now" is less than or equal to "then". Therefore, a
+  negative value for the microseconds is possible only in the case when "now"
+  is more than a second less than "then". That means that itval.it_value.tv_sec
+  is greater than zero. The following correction is therefore safe. */
+
+  if (itval.it_value.tv_usec < 0)
+    {
+    itval.it_value.tv_usec += 1000000;
+    itval.it_value.tv_sec -= 1;
+    }
+
+  DEBUG(D_transport|D_receive)
+    {
+    if (!running_in_test_harness)
+      {
+      debug_printf("tick check: %lu.%06lu %lu.%06lu\n",
+        then_tv->tv_sec, (long) then_tv->tv_usec,
+               now_tv.tv_sec, (long) now_tv.tv_usec);
+      debug_printf("waiting %lu.%06lu\n", itval.it_value.tv_sec,
+        (long) itval.it_value.tv_usec);
+      }
+    }
+
+  milliwait(&itval);
+  }
+}
+
+
+
+
+/*************************************************
+*   Call fopen() with umask 777 and adjust mode  *
+*************************************************/
+
+/* Exim runs with umask(0) so that files created with open() have the mode that
+is specified in the open() call. However, there are some files, typically in
+the spool directory, that are created with fopen(). They end up world-writeable
+if no precautions are taken. Although the spool directory is not accessible to
+the world, this is an untidiness. So this is a wrapper function for fopen()
+that sorts out the mode of the created file.
+
+Arguments:
+   filename       the file name
+   options        the fopen() options
+   mode           the required mode
+
+Returns:          the fopened FILE or NULL
+*/
+
+FILE *
+modefopen(const uschar *filename, const char *options, mode_t mode)
+{
+mode_t saved_umask = umask(0777);
+FILE *f = Ufopen(filename, options);
+(void)umask(saved_umask);
+if (f != NULL) (void)fchmod(fileno(f), mode);
+return f;
+}
+
+
+
+
+/*************************************************
+*   Ensure stdin, stdout, and stderr exist       *
+*************************************************/
+
+/* Some operating systems grumble if an exec() happens without a standard
+input, output, and error (fds 0, 1, 2) being defined. The worry is that some
+file will be opened and will use these fd values, and then some other bit of
+code will assume, for example, that it can write error messages to stderr.
+This function ensures that fds 0, 1, and 2 are open if they do not already
+exist, by connecting them to /dev/null.
+
+This function is also used to ensure that std{in,out,err} exist at all times,
+so that if any library that Exim calls tries to use them, it doesn't crash.
+
+Arguments:  None
+Returns:    Nothing
+*/
+
+void
+exim_nullstd(void)
+{
+int i;
+int devnull = -1;
+struct stat statbuf;
+for (i = 0; i <= 2; i++)
+  {
+  if (fstat(i, &statbuf) < 0 && errno == EBADF)
+    {
+    if (devnull < 0) devnull = open("/dev/null", O_RDWR);
+    if (devnull < 0) log_write(0, LOG_MAIN|LOG_PANIC_DIE, "%s",
+      string_open_failed(errno, "/dev/null"));
+    if (devnull != i) (void)dup2(devnull, i);
+    }
+  }
+if (devnull > 2) (void)close(devnull);
+}
+
+
+
+
+/*************************************************
+*   Close unwanted file descriptors for delivery *
+*************************************************/
+
+/* This function is called from a new process that has been forked to deliver
+an incoming message, either directly, or using exec.
+
+We want any smtp input streams to be closed in this new process. However, it
+has been observed that using fclose() here causes trouble. When reading in -bS
+input, duplicate copies of messages have been seen. The files will be sharing a
+file pointer with the parent process, and it seems that fclose() (at least on
+some systems - I saw this on Solaris 2.5.1) messes with that file pointer, at
+least sometimes. Hence we go for closing the underlying file descriptors.
+
+If TLS is active, we want to shut down the TLS library, but without molesting
+the parent's SSL connection.
+
+For delivery of a non-SMTP message, we want to close stdin and stdout (and
+stderr unless debugging) because the calling process might have set them up as
+pipes and be waiting for them to close before it waits for the submission
+process to terminate. If they aren't closed, they hold up the calling process
+until the initial delivery process finishes, which is not what we want.
+
+Exception: We do want it for synchronous delivery!
+
+And notwithstanding all the above, if D_resolver is set, implying resolver
+debugging, leave stdout open, because that's where the resolver writes its
+debugging output.
+
+When we close stderr (which implies we've also closed stdout), we also get rid
+of any controlling terminal.
+
+Arguments:   None
+Returns:     Nothing
+*/
+
+static void
+close_unwanted(void)
+{
+if (smtp_input)
+  {
+  #ifdef SUPPORT_TLS
+  tls_close(TRUE, FALSE);      /* Shut down the TLS library */
+  #endif
+  (void)close(fileno(smtp_in));
+  (void)close(fileno(smtp_out));
+  smtp_in = NULL;
+  }
+else
+  {
+  (void)close(0);                                          /* stdin */
+  if ((debug_selector & D_resolver) == 0) (void)close(1);  /* stdout */
+  if (debug_selector == 0)                                 /* stderr */
+    {
+    if (!synchronous_delivery)
+      {
+      (void)close(2);
+      log_stderr = NULL;
+      }
+    (void)setsid();
+    }
+  }
+}
+
+
+
+
+/*************************************************
+*          Set uid and gid                       *
+*************************************************/
+
+/* This function sets a new uid and gid permanently, optionally calling
+initgroups() to set auxiliary groups. There are some special cases when running
+Exim in unprivileged modes. In these situations the effective uid will not be
+root; if we already have the right effective uid/gid, and don't need to
+initialize any groups, leave things as they are.
+
+Arguments:
+  uid        the uid
+  gid        the gid
+  igflag     TRUE if initgroups() wanted
+  msg        text to use in debugging output and failure log
+
+Returns:     nothing; bombs out on failure
+*/
+
+void
+exim_setugid(uid_t uid, gid_t gid, BOOL igflag, uschar *msg)
+{
+uid_t euid = geteuid();
+gid_t egid = getegid();
+
+if (euid == root_uid || euid != uid || egid != gid || igflag)
+  {
+  /* At least one OS returns +1 for initgroups failure, so just check for
+  non-zero. */
+
+  if (igflag)
+    {
+    struct passwd *pw = getpwuid(uid);
+    if (pw != NULL)
+      {
+      if (initgroups(pw->pw_name, gid) != 0)
+        log_write(0,LOG_MAIN|LOG_PANIC_DIE,"initgroups failed for uid=%ld: %s",
+          (long int)uid, strerror(errno));
+      }
+    else log_write(0, LOG_MAIN|LOG_PANIC_DIE, "cannot run initgroups(): "
+      "no passwd entry for uid=%ld", (long int)uid);
+    }
+
+  if (setgid(gid) < 0 || setuid(uid) < 0)
+    {
+    log_write(0, LOG_MAIN|LOG_PANIC_DIE, "unable to set gid=%ld or uid=%ld "
+      "(euid=%ld): %s", (long int)gid, (long int)uid, (long int)euid, msg);
+    }
+  }
+
+/* Debugging output included uid/gid and all groups */
+
+DEBUG(D_uid)
+  {
+  int group_count, save_errno;
+  gid_t group_list[NGROUPS_MAX];
+  debug_printf("changed uid/gid: %s\n  uid=%ld gid=%ld pid=%ld\n", msg,
+    (long int)geteuid(), (long int)getegid(), (long int)getpid());
+  group_count = getgroups(NGROUPS_MAX, group_list);
+  save_errno = errno;
+  debug_printf("  auxiliary group list:");
+  if (group_count > 0)
+    {
+    int i;
+    for (i = 0; i < group_count; i++) debug_printf(" %d", (int)group_list[i]);
+    }
+  else if (group_count < 0)
+    debug_printf(" <error: %s>", strerror(save_errno));
+  else debug_printf(" <none>");
+  debug_printf("\n");
+  }
+}
+
+
+
+
+/*************************************************
+*               Exit point                       *
+*************************************************/
+
+/* Exim exits via this function so that it always clears up any open
+databases.
+
+Arguments:
+  rc         return code
+
+Returns:     does not return
+*/
+
+void
+exim_exit(int rc)
+{
+search_tidyup();
+DEBUG(D_any)
+  debug_printf(">>>>>>>>>>>>>>>> Exim pid=%d terminating with rc=%d "
+    ">>>>>>>>>>>>>>>>\n", (int)getpid(), rc);
+exit(rc);
+}
+
+
+
+
+/*************************************************
+*         Extract port from host address         *
+*************************************************/
+
+/* Called to extract the port from the values given to -oMa and -oMi.
+It also checks the syntax of the address, and terminates it before the
+port data when a port is extracted.
+
+Argument:
+  address   the address, with possible port on the end
+
+Returns:    the port, or zero if there isn't one
+            bombs out on a syntax error
+*/
+
+static int
+check_port(uschar *address)
+{
+int port = host_address_extract_port(address);
+if (string_is_ip_address(address, NULL) == 0)
+  {
+  fprintf(stderr, "exim abandoned: \"%s\" is not an IP address\n", address);
+  exit(EXIT_FAILURE);
+  }
+return port;
+}
+
+
+
+/*************************************************
+*              Test/verify an address            *
+*************************************************/
+
+/* This function is called by the -bv and -bt code. It extracts a working
+address from a full RFC 822 address. This isn't really necessary per se, but it
+has the effect of collapsing source routes.
+
+Arguments:
+  s            the address string
+  flags        flag bits for verify_address()
+  exit_value   to be set for failures
+
+Returns:       nothing
+*/
+
+static void
+test_address(uschar *s, int flags, int *exit_value)
+{
+int start, end, domain;
+uschar *parse_error = NULL;
+uschar *address = parse_extract_address(s, &parse_error, &start, &end, &domain,
+  FALSE);
+if (address == NULL)
+  {
+  fprintf(stdout, "syntax error: %s\n", parse_error);
+  *exit_value = 2;
+  }
+else
+  {
+  int rc = verify_address(deliver_make_addr(address,TRUE), stdout, flags, -1,
+    -1, -1, NULL, NULL, NULL);
+  if (rc == FAIL) *exit_value = 2;
+    else if (rc == DEFER && *exit_value == 0) *exit_value = 1;
+  }
+}
+
+
+
+/*************************************************
+*          Show supported features               *
+*************************************************/
+
+/* This function is called for -bV/--version and for -d to output the optional
+features of the current Exim binary.
+
+Arguments:  a FILE for printing
+Returns:    nothing
+*/
+
+static void
+show_whats_supported(FILE *f)
+{
+  auth_info *authi;
+
+#ifdef DB_VERSION_STRING
+fprintf(f, "Berkeley DB: %s\n", DB_VERSION_STRING);
+#elif defined(BTREEVERSION) && defined(HASHVERSION)
+  #ifdef USE_DB
+  fprintf(f, "Probably Berkeley DB version 1.8x (native mode)\n");
+  #else
+  fprintf(f, "Probably Berkeley DB version 1.8x (compatibility mode)\n");
+  #endif
+#elif defined(_DBM_RDONLY) || defined(dbm_dirfno)
+fprintf(f, "Probably ndbm\n");
+#elif defined(USE_TDB)
+fprintf(f, "Using tdb\n");
+#else
+  #ifdef USE_GDBM
+  fprintf(f, "Probably GDBM (native mode)\n");
+  #else
+  fprintf(f, "Probably GDBM (compatibility mode)\n");
+  #endif
+#endif
+
+fprintf(f, "Support for:");
+#ifdef SUPPORT_CRYPTEQ
+  fprintf(f, " crypteq");
+#endif
+#if HAVE_ICONV
+  fprintf(f, " iconv()");
+#endif
+#if HAVE_IPV6
+  fprintf(f, " IPv6");
+#endif
+#ifdef HAVE_SETCLASSRESOURCES
+  fprintf(f, " use_setclassresources");
+#endif
+#ifdef SUPPORT_PAM
+  fprintf(f, " PAM");
+#endif
+#ifdef EXIM_PERL
+  fprintf(f, " Perl");
+#endif
+#ifdef EXPAND_DLFUNC
+  fprintf(f, " Expand_dlfunc");
+#endif
+#ifdef USE_TCP_WRAPPERS
+  fprintf(f, " TCPwrappers");
+#endif
+#ifdef SUPPORT_TLS
+  #ifdef USE_GNUTLS
+  fprintf(f, " GnuTLS");
+  #else
+  fprintf(f, " OpenSSL");
+  #endif
+#endif
+#ifdef SUPPORT_TRANSLATE_IP_ADDRESS
+  fprintf(f, " translate_ip_address");
+#endif
+#ifdef SUPPORT_MOVE_FROZEN_MESSAGES
+  fprintf(f, " move_frozen_messages");
+#endif
+#ifdef WITH_CONTENT_SCAN
+  fprintf(f, " Content_Scanning");
+#endif
+#ifndef DISABLE_DKIM
+  fprintf(f, " DKIM");
+#endif
+#ifdef WITH_OLD_DEMIME
+  fprintf(f, " Old_Demime");
+#endif
+#ifndef DISABLE_PRDR
+  fprintf(f, " PRDR");
+#endif
+#ifndef DISABLE_OCSP
+  fprintf(f, " OCSP");
+#endif
+#ifdef EXPERIMENTAL_SPF
+  fprintf(f, " Experimental_SPF");
+#endif
+#ifdef EXPERIMENTAL_SRS
+  fprintf(f, " Experimental_SRS");
+#endif
+#ifdef EXPERIMENTAL_BRIGHTMAIL
+  fprintf(f, " Experimental_Brightmail");
+#endif
+#ifdef EXPERIMENTAL_DCC
+  fprintf(f, " Experimental_DCC");
+#endif
+#ifdef EXPERIMENTAL_DMARC
+  fprintf(f, " Experimental_DMARC");
+#endif
+#ifdef EXPERIMENTAL_PROXY
+  fprintf(f, " Experimental_Proxy");
+#endif
+#ifdef EXPERIMENTAL_TPDA
+  fprintf(f, " Experimental_TPDA");
+#endif
+#ifdef EXPERIMENTAL_REDIS
+  fprintf(f, " Experimental_Redis");
+#endif
+#ifdef EXPERIMENTAL_CERTNAMES
+  fprintf(f, " Experimental_Certnames");
+#endif
+#ifdef EXPERIMENTAL_DSN
+  fprintf(f, " Experimental_DSN");
+#endif
+fprintf(f, "\n");
+
+fprintf(f, "Lookups (built-in):");
+#if defined(LOOKUP_LSEARCH) && LOOKUP_LSEARCH!=2
+  fprintf(f, " lsearch wildlsearch nwildlsearch iplsearch");
+#endif
+#if defined(LOOKUP_CDB) && LOOKUP_CDB!=2
+  fprintf(f, " cdb");
+#endif
+#if defined(LOOKUP_DBM) && LOOKUP_DBM!=2
+  fprintf(f, " dbm dbmjz dbmnz");
+#endif
+#if defined(LOOKUP_DNSDB) && LOOKUP_DNSDB!=2
+  fprintf(f, " dnsdb");
+#endif
+#if defined(LOOKUP_DSEARCH) && LOOKUP_DSEARCH!=2
+  fprintf(f, " dsearch");
+#endif
+#if defined(LOOKUP_IBASE) && LOOKUP_IBASE!=2
+  fprintf(f, " ibase");
+#endif
+#if defined(LOOKUP_LDAP) && LOOKUP_LDAP!=2
+  fprintf(f, " ldap ldapdn ldapm");
+#endif
+#if defined(LOOKUP_MYSQL) && LOOKUP_MYSQL!=2
+  fprintf(f, " mysql");
+#endif
+#if defined(LOOKUP_NIS) && LOOKUP_NIS!=2
+  fprintf(f, " nis nis0");
+#endif
+#if defined(LOOKUP_NISPLUS) && LOOKUP_NISPLUS!=2
+  fprintf(f, " nisplus");
+#endif
+#if defined(LOOKUP_ORACLE) && LOOKUP_ORACLE!=2
+  fprintf(f, " oracle");
+#endif
+#if defined(LOOKUP_PASSWD) && LOOKUP_PASSWD!=2
+  fprintf(f, " passwd");
+#endif
+#if defined(LOOKUP_PGSQL) && LOOKUP_PGSQL!=2
+  fprintf(f, " pgsql");
+#endif
+#if defined(LOOKUP_SQLITE) && LOOKUP_SQLITE!=2
+  fprintf(f, " sqlite");
+#endif
+#if defined(LOOKUP_TESTDB) && LOOKUP_TESTDB!=2
+  fprintf(f, " testdb");
+#endif
+#if defined(LOOKUP_WHOSON) && LOOKUP_WHOSON!=2
+  fprintf(f, " whoson");
+#endif
+fprintf(f, "\n");
+
+fprintf(f, "Authenticators:");
+#ifdef AUTH_CRAM_MD5
+  fprintf(f, " cram_md5");
+#endif
+#ifdef AUTH_CYRUS_SASL
+  fprintf(f, " cyrus_sasl");
+#endif
+#ifdef AUTH_DOVECOT
+  fprintf(f, " dovecot");
+#endif
+#ifdef AUTH_GSASL
+  fprintf(f, " gsasl");
+#endif
+#ifdef AUTH_HEIMDAL_GSSAPI
+  fprintf(f, " heimdal_gssapi");
+#endif
+#ifdef AUTH_PLAINTEXT
+  fprintf(f, " plaintext");
+#endif
+#ifdef AUTH_SPA
+  fprintf(f, " spa");
+#endif
+fprintf(f, "\n");
+
+fprintf(f, "Routers:");
+#ifdef ROUTER_ACCEPT
+  fprintf(f, " accept");
+#endif
+#ifdef ROUTER_DNSLOOKUP
+  fprintf(f, " dnslookup");
+#endif
+#ifdef ROUTER_IPLITERAL
+  fprintf(f, " ipliteral");
+#endif
+#ifdef ROUTER_IPLOOKUP
+  fprintf(f, " iplookup");
+#endif
+#ifdef ROUTER_MANUALROUTE
+  fprintf(f, " manualroute");
+#endif
+#ifdef ROUTER_QUERYPROGRAM
+  fprintf(f, " queryprogram");
+#endif
+#ifdef ROUTER_REDIRECT
+  fprintf(f, " redirect");
+#endif
+fprintf(f, "\n");
+
+fprintf(f, "Transports:");
+#ifdef TRANSPORT_APPENDFILE
+  fprintf(f, " appendfile");
+  #ifdef SUPPORT_MAILDIR
+    fprintf(f, "/maildir");
+  #endif
+  #ifdef SUPPORT_MAILSTORE
+    fprintf(f, "/mailstore");
+  #endif
+  #ifdef SUPPORT_MBX
+    fprintf(f, "/mbx");
+  #endif
+#endif
+#ifdef TRANSPORT_AUTOREPLY
+  fprintf(f, " autoreply");
+#endif
+#ifdef TRANSPORT_LMTP
+  fprintf(f, " lmtp");
+#endif
+#ifdef TRANSPORT_PIPE
+  fprintf(f, " pipe");
+#endif
+#ifdef TRANSPORT_SMTP
+  fprintf(f, " smtp");
+#endif
+fprintf(f, "\n");
+
+if (fixed_never_users[0] > 0)
+  {
+  int i;
+  fprintf(f, "Fixed never_users: ");
+  for (i = 1; i <= (int)fixed_never_users[0] - 1; i++)
+    fprintf(f, "%d:", (unsigned int)fixed_never_users[i]);
+  fprintf(f, "%d\n", (unsigned int)fixed_never_users[i]);
+  }
+
+fprintf(f, "Size of off_t: " SIZE_T_FMT "\n", sizeof(off_t));
+
+/* Everything else is details which are only worth reporting when debugging.
+Perhaps the tls_version_report should move into this too. */
+DEBUG(D_any) do {
+
+  int i;
+
+/* clang defines __GNUC__ (at least, for me) so test for it first */
+#if defined(__clang__)
+  fprintf(f, "Compiler: CLang [%s]\n", __clang_version__);
+#elif defined(__GNUC__)
+  fprintf(f, "Compiler: GCC [%s]\n",
+# ifdef __VERSION__
+      __VERSION__
+# else
+      "? unknown version ?"
+# endif
+      );
+#else
+  fprintf(f, "Compiler: <unknown>\n");
+#endif
+
+#ifdef SUPPORT_TLS
+  tls_version_report(f);
+#endif
+
+  for (authi = auths_available; *authi->driver_name != '\0'; ++authi) {
+    if (authi->version_report) {
+      (*authi->version_report)(f);
+    }
+  }
+
+  /* PCRE_PRERELEASE is either defined and empty or a bare sequence of
+  characters; unless it's an ancient version of PCRE in which case it
+  is not defined. */
+#ifndef PCRE_PRERELEASE
+#define PCRE_PRERELEASE
+#endif
+#define QUOTE(X) #X
+#define EXPAND_AND_QUOTE(X) QUOTE(X)
+  fprintf(f, "Library version: PCRE: Compile: %d.%d%s\n"
+             "                       Runtime: %s\n",
+          PCRE_MAJOR, PCRE_MINOR,
+          EXPAND_AND_QUOTE(PCRE_PRERELEASE) "",
+          pcre_version());
+#undef QUOTE
+#undef EXPAND_AND_QUOTE
+
+  init_lookup_list();
+  for (i = 0; i < lookup_list_count; i++)
+    {
+    if (lookup_list[i]->version_report)
+      lookup_list[i]->version_report(f);
+    }
+
+#ifdef WHITELIST_D_MACROS
+  fprintf(f, "WHITELIST_D_MACROS: \"%s\"\n", WHITELIST_D_MACROS);
+#else
+  fprintf(f, "WHITELIST_D_MACROS unset\n");
+#endif
+#ifdef TRUSTED_CONFIG_LIST
+  fprintf(f, "TRUSTED_CONFIG_LIST: \"%s\"\n", TRUSTED_CONFIG_LIST);
+#else
+  fprintf(f, "TRUSTED_CONFIG_LIST unset\n");
+#endif
+
+} while (0);
+}
+
+
+/*************************************************
+*     Show auxiliary information about Exim      *
+*************************************************/
+
+static void
+show_exim_information(enum commandline_info request, FILE *stream)
+{
+const uschar **pp;
+
+switch(request)
+  {
+  case CMDINFO_NONE:
+    fprintf(stream, "Oops, something went wrong.\n");
+    return;
+  case CMDINFO_HELP:
+    fprintf(stream,
+"The -bI: flag takes a string indicating which information to provide.\n"
+"If the string is not recognised, you'll get this help (on stderr).\n"
+"\n"
+"  exim -bI:help    this information\n"
+"  exim -bI:dscp    dscp value keywords known\n"
+"  exim -bI:sieve   list of supported sieve extensions, one per line.\n"
+);
+    return;
+  case CMDINFO_SIEVE:
+    for (pp = exim_sieve_extension_list; *pp; ++pp)
+      fprintf(stream, "%s\n", *pp);
+    return;
+  case CMDINFO_DSCP:
+    dscp_list_to_stream(stream);
+    return;
+  }
+}
+
+
+/*************************************************
+*               Quote a local part               *
+*************************************************/
+
+/* This function is used when a sender address or a From: or Sender: header
+line is being created from the caller's login, or from an authenticated_id. It
+applies appropriate quoting rules for a local part.
+
+Argument:    the local part
+Returns:     the local part, quoted if necessary
+*/
+
+uschar *
+local_part_quote(uschar *lpart)
+{
+BOOL needs_quote = FALSE;
+int size, ptr;
+uschar *yield;
+uschar *t;
+
+for (t = lpart; !needs_quote && *t != 0; t++)
+  {
+  needs_quote = !isalnum(*t) && strchr("!#$%&'*+-/=?^_`{|}~", *t) == NULL &&
+    (*t != '.' || t == lpart || t[1] == 0);
+  }
+
+if (!needs_quote) return lpart;
+
+size = ptr = 0;
+yield = string_cat(NULL, &size, &ptr, US"\"", 1);
+
+for (;;)
+  {
+  uschar *nq = US Ustrpbrk(lpart, "\\\"");
+  if (nq == NULL)
+    {
+    yield = string_cat(yield, &size, &ptr, lpart, Ustrlen(lpart));
+    break;
+    }
+  yield = string_cat(yield, &size, &ptr, lpart, nq - lpart);
+  yield = string_cat(yield, &size, &ptr, US"\\", 1);
+  yield = string_cat(yield, &size, &ptr, nq, 1);
+  lpart = nq + 1;
+  }
+
+yield = string_cat(yield, &size, &ptr, US"\"", 1);
+yield[ptr] = 0;
+return yield;
+}
+
+
+
+#ifdef USE_READLINE
+/*************************************************
+*         Load readline() functions              *
+*************************************************/
+
+/* This function is called from testing executions that read data from stdin,
+but only when running as the calling user. Currently, only -be does this. The
+function loads the readline() function library and passes back the functions.
+On some systems, it needs the curses library, so load that too, but try without
+it if loading fails. All this functionality has to be requested at build time.
+
+Arguments:
+  fn_readline_ptr   pointer to where to put the readline pointer
+  fn_addhist_ptr    pointer to where to put the addhistory function
+
+Returns:            the dlopen handle or NULL on failure
+*/
+
+static void *
+set_readline(char * (**fn_readline_ptr)(const char *),
+             void   (**fn_addhist_ptr)(const char *))
+{
+void *dlhandle;
+void *dlhandle_curses = dlopen("libcurses." DYNLIB_FN_EXT, RTLD_GLOBAL|RTLD_LAZY);
+
+dlhandle = dlopen("libreadline." DYNLIB_FN_EXT, RTLD_GLOBAL|RTLD_NOW);
+if (dlhandle_curses != NULL) dlclose(dlhandle_curses);
+
+if (dlhandle != NULL)
+  {
+  /* Checked manual pages; at least in GNU Readline 6.1, the prototypes are:
+   *   char * readline (const char *prompt);
+   *   void add_history (const char *string);
+   */
+  *fn_readline_ptr = (char *(*)(const char*))dlsym(dlhandle, "readline");
+  *fn_addhist_ptr = (void(*)(const char*))dlsym(dlhandle, "add_history");
+  }
+else
+  {
+  DEBUG(D_any) debug_printf("failed to load readline: %s\n", dlerror());
+  }
+
+return dlhandle;
+}
+#endif
+
+
+
+/*************************************************
+*    Get a line from stdin for testing things    *
+*************************************************/
+
+/* This function is called when running tests that can take a number of lines
+of input (for example, -be and -bt). It handles continuations and trailing
+spaces. And prompting and a blank line output on eof. If readline() is in use,
+the arguments are non-NULL and provide the relevant functions.
+
+Arguments:
+  fn_readline   readline function or NULL
+  fn_addhist    addhist function or NULL
+
+Returns:        pointer to dynamic memory, or NULL at end of file
+*/
+
+static uschar *
+get_stdinput(char *(*fn_readline)(const char *), void(*fn_addhist)(const char *))
+{
+int i;
+int size = 0;
+int ptr = 0;
+uschar *yield = NULL;
+
+if (fn_readline == NULL) { printf("> "); fflush(stdout); }
+
+for (i = 0;; i++)
+  {
+  uschar buffer[1024];
+  uschar *p, *ss;
+
+  #ifdef USE_READLINE
+  char *readline_line = NULL;
+  if (fn_readline != NULL)
+    {
+    if ((readline_line = fn_readline((i > 0)? "":"> ")) == NULL) break;
+    if (*readline_line != 0 && fn_addhist != NULL) fn_addhist(readline_line);
+    p = US readline_line;
+    }
+  else
+  #endif
+
+  /* readline() not in use */
+
+    {
+    if (Ufgets(buffer, sizeof(buffer), stdin) == NULL) break;
+    p = buffer;
+    }
+
+  /* Handle the line */
+
+  ss = p + (int)Ustrlen(p);
+  while (ss > p && isspace(ss[-1])) ss--;
+
+  if (i > 0)
+    {
+    while (p < ss && isspace(*p)) p++;   /* leading space after cont */
+    }
+
+  yield = string_cat(yield, &size, &ptr, p, ss - p);
+
+  #ifdef USE_READLINE
+  if (fn_readline != NULL) free(readline_line);
+  #endif
+
+  if (ss == p || yield[ptr-1] != '\\')
+    {
+    yield[ptr] = 0;
+    break;
+    }
+  yield[--ptr] = 0;
+  }
+
+if (yield == NULL) printf("\n");
+return yield;
+}
+
+
+
+/*************************************************
+*    Output usage information for the program    *
+*************************************************/
+
+/* This function is called when there are no recipients
+   or a specific --help argument was added.
+
+Arguments:
+  progname      information on what name we were called by
+
+Returns:        DOES NOT RETURN
+*/
+
+static void
+exim_usage(uschar *progname)
+{
+
+/* Handle specific program invocation varients */
+if (Ustrcmp(progname, US"-mailq") == 0)
+  {
+  fprintf(stderr,
+    "mailq - list the contents of the mail queue\n\n"
+    "For a list of options, see the Exim documentation.\n");
+  exit(EXIT_FAILURE);
+  }
+
+/* Generic usage - we output this whatever happens */
+fprintf(stderr,
+  "Exim is a Mail Transfer Agent. It is normally called by Mail User Agents,\n"
+  "not directly from a shell command line. Options and/or arguments control\n"
+  "what it does when called. For a list of options, see the Exim documentation.\n");
+
+exit(EXIT_FAILURE);
+}
+
+
+
+/*************************************************
+*    Validate that the macros given are okay     *
+*************************************************/
+
+/* Typically, Exim will drop privileges if macros are supplied.  In some
+cases, we want to not do so.
+
+Arguments:    none (macros is a global)
+Returns:      true if trusted, false otherwise
+*/
+
+static BOOL
+macros_trusted(void)
+{
+#ifdef WHITELIST_D_MACROS
+macro_item *m;
+uschar *whitelisted, *end, *p, **whites, **w;
+int white_count, i, n;
+size_t len;
+BOOL prev_char_item, found;
+#endif
+
+if (macros == NULL)
+  return TRUE;
+#ifndef WHITELIST_D_MACROS
+return FALSE;
+#else
+
+/* We only trust -D overrides for some invoking users:
+root, the exim run-time user, the optional config owner user.
+I don't know why config-owner would be needed, but since they can own the
+config files anyway, there's no security risk to letting them override -D. */
+if ( ! ((real_uid == root_uid)
+     || (real_uid == exim_uid)
+#ifdef CONFIGURE_OWNER
+     || (real_uid == config_uid)
+#endif
+   ))
+  {
+  debug_printf("macros_trusted rejecting macros for uid %d\n", (int) real_uid);
+  return FALSE;
+  }
+
+/* Get a list of macros which are whitelisted */
+whitelisted = string_copy_malloc(US WHITELIST_D_MACROS);
+prev_char_item = FALSE;
+white_count = 0;
+for (p = whitelisted; *p != '\0'; ++p)
+  {
+  if (*p == ':' || isspace(*p))
+    {
+    *p = '\0';
+    if (prev_char_item)
+      ++white_count;
+    prev_char_item = FALSE;
+    continue;
+    }
+  if (!prev_char_item)
+    prev_char_item = TRUE;
+  }
+end = p;
+if (prev_char_item)
+  ++white_count;
+if (!white_count)
+  return FALSE;
+whites = store_malloc(sizeof(uschar *) * (white_count+1));
+for (p = whitelisted, i = 0; (p != end) && (i < white_count); ++p)
+  {
+  if (*p != '\0')
+    {
+    whites[i++] = p;
+    if (i == white_count)
+      break;
+    while (*p != '\0' && p < end)
+      ++p;
+    }
+  }
+whites[i] = NULL;
+
+/* The list of macros should be very short.  Accept the N*M complexity. */
+for (m = macros; m != NULL; m = m->next)
+  {
+  found = FALSE;
+  for (w = whites; *w; ++w)
+    if (Ustrcmp(*w, m->name) == 0)
+      {
+      found = TRUE;
+      break;
+      }
+  if (!found)
+    return FALSE;
+  if (m->replacement == NULL)
+    continue;
+  len = Ustrlen(m->replacement);
+  if (len == 0)
+    continue;
+  n = pcre_exec(regex_whitelisted_macro, NULL, CS m->replacement, len,
+   0, PCRE_EOPT, NULL, 0);
+  if (n < 0)
+    {
+    if (n != PCRE_ERROR_NOMATCH)
+      debug_printf("macros_trusted checking %s returned %d\n", m->name, n);
+    return FALSE;
+    }
+  }
+DEBUG(D_any) debug_printf("macros_trusted overridden to true by whitelisting\n");
+return TRUE;
+#endif
+}
+
+
+/*************************************************
+*          Entry point and high-level code       *
+*************************************************/
+
+/* Entry point for the Exim mailer. Analyse the arguments and arrange to take
+the appropriate action. All the necessary functions are present in the one
+binary. I originally thought one should split it up, but it turns out that so
+much of the apparatus is needed in each chunk that one might as well just have
+it all available all the time, which then makes the coding easier as well.
+
+Arguments:
+  argc      count of entries in argv
+  argv      argument strings, with argv[0] being the program name
+
+Returns:    EXIT_SUCCESS if terminated successfully
+            EXIT_FAILURE otherwise, except when a message has been sent
+              to the sender, and -oee was given
+*/
+
+int
+main(int argc, char **cargv)
+{
+uschar **argv = USS cargv;
+int  arg_receive_timeout = -1;
+int  arg_smtp_receive_timeout = -1;
+int  arg_error_handling = error_handling;
+int  filter_sfd = -1;
+int  filter_ufd = -1;
+int  group_count;
+int  i, rv;
+int  list_queue_option = 0;
+int  msg_action = 0;
+int  msg_action_arg = -1;
+int  namelen = (argv[0] == NULL)? 0 : Ustrlen(argv[0]);
+int  queue_only_reason = 0;
+#ifdef EXIM_PERL
+int  perl_start_option = 0;
+#endif
+int  recipients_arg = argc;
+int  sender_address_domain = 0;
+int  test_retry_arg = -1;
+int  test_rewrite_arg = -1;
+BOOL arg_queue_only = FALSE;
+BOOL bi_option = FALSE;
+BOOL checking = FALSE;
+BOOL count_queue = FALSE;
+BOOL expansion_test = FALSE;
+BOOL extract_recipients = FALSE;
+BOOL flag_G = FALSE;
+BOOL flag_n = FALSE;
+BOOL forced_delivery = FALSE;
+BOOL f_end_dot = FALSE;
+BOOL deliver_give_up = FALSE;
+BOOL list_queue = FALSE;
+BOOL list_options = FALSE;
+BOOL local_queue_only;
+BOOL more = TRUE;
+BOOL one_msg_action = FALSE;
+BOOL queue_only_set = FALSE;
+BOOL receiving_message = TRUE;
+BOOL sender_ident_set = FALSE;
+BOOL session_local_queue_only;
+BOOL unprivileged;
+BOOL removed_privilege = FALSE;
+BOOL usage_wanted = FALSE;
+BOOL verify_address_mode = FALSE;
+BOOL verify_as_sender = FALSE;
+BOOL version_printed = FALSE;
+uschar *alias_arg = NULL;
+uschar *called_as = US"";
+uschar *cmdline_syslog_name = NULL;
+uschar *start_queue_run_id = NULL;
+uschar *stop_queue_run_id = NULL;
+uschar *expansion_test_message = NULL;
+uschar *ftest_domain = NULL;
+uschar *ftest_localpart = NULL;
+uschar *ftest_prefix = NULL;
+uschar *ftest_suffix = NULL;
+uschar *malware_test_file = NULL;
+uschar *real_sender_address;
+uschar *originator_home = US"/";
+size_t sz;
+void *reset_point;
+
+struct passwd *pw;
+struct stat statbuf;
+pid_t passed_qr_pid = (pid_t)0;
+int passed_qr_pipe = -1;
+gid_t group_list[NGROUPS_MAX];
+
+/* For the -bI: flag */
+enum commandline_info info_flag = CMDINFO_NONE;
+BOOL info_stdout = FALSE;
+
+/* Possible options for -R and -S */
+
+static uschar *rsopts[] = { US"f", US"ff", US"r", US"rf", US"rff" };
+
+/* Need to define this in case we need to change the environment in order
+to get rid of a bogus time zone. We have to make it char rather than uschar
+because some OS define it in /usr/include/unistd.h. */
+
+extern char **environ;
+
+/* If the Exim user and/or group and/or the configuration file owner/group were
+defined by ref:name at build time, we must now find the actual uid/gid values.
+This is a feature to make the lives of binary distributors easier. */
+
+#ifdef EXIM_USERNAME
+if (route_finduser(US EXIM_USERNAME, &pw, &exim_uid))
+  {
+  if (exim_uid == 0)
+    {
+    fprintf(stderr, "exim: refusing to run with uid 0 for \"%s\"\n",
+      EXIM_USERNAME);
+    exit(EXIT_FAILURE);
+    }
+  /* If ref:name uses a number as the name, route_finduser() returns
+  TRUE with exim_uid set and pw coerced to NULL. */
+  if (pw)
+    exim_gid = pw->pw_gid;
+#ifndef EXIM_GROUPNAME
+  else
+    {
+    fprintf(stderr,
+        "exim: ref:name should specify a usercode, not a group.\n"
+        "exim: can't let you get away with it unless you also specify a group.\n");
+    exit(EXIT_FAILURE);
+    }
+#endif
+  }
+else
+  {
+  fprintf(stderr, "exim: failed to find uid for user name \"%s\"\n",
+    EXIM_USERNAME);
+  exit(EXIT_FAILURE);
+  }
+#endif
+
+#ifdef EXIM_GROUPNAME
+if (!route_findgroup(US EXIM_GROUPNAME, &exim_gid))
+  {
+  fprintf(stderr, "exim: failed to find gid for group name \"%s\"\n",
+    EXIM_GROUPNAME);
+  exit(EXIT_FAILURE);
+  }
+#endif
+
+#ifdef CONFIGURE_OWNERNAME
+if (!route_finduser(US CONFIGURE_OWNERNAME, NULL, &config_uid))
+  {
+  fprintf(stderr, "exim: failed to find uid for user name \"%s\"\n",
+    CONFIGURE_OWNERNAME);
+  exit(EXIT_FAILURE);
+  }
+#endif
+
+/* We default the system_filter_user to be the Exim run-time user, as a
+sane non-root value. */
+system_filter_uid = exim_uid;
+
+#ifdef CONFIGURE_GROUPNAME
+if (!route_findgroup(US CONFIGURE_GROUPNAME, &config_gid))
+  {
+  fprintf(stderr, "exim: failed to find gid for group name \"%s\"\n",
+    CONFIGURE_GROUPNAME);
+  exit(EXIT_FAILURE);
+  }
+#endif
+
+/* In the Cygwin environment, some initialization needs doing. It is fudged
+in by means of this macro. */
+
+#ifdef OS_INIT
+OS_INIT
+#endif
+
+/* Check a field which is patched when we are running Exim within its
+testing harness; do a fast initial check, and then the whole thing. */
+
+running_in_test_harness =
+  *running_status == '<' && Ustrcmp(running_status, "<<<testing>>>") == 0;
+
+/* The C standard says that the equivalent of setlocale(LC_ALL, "C") is obeyed
+at the start of a program; however, it seems that some environments do not
+follow this. A "strange" locale can affect the formatting of timestamps, so we
+make quite sure. */
+
+setlocale(LC_ALL, "C");
+
+/* Set up the default handler for timing using alarm(). */
+
+os_non_restarting_signal(SIGALRM, sigalrm_handler);
+
+/* Ensure we have a buffer for constructing log entries. Use malloc directly,
+because store_malloc writes a log entry on failure. */
+
+log_buffer = (uschar *)malloc(LOG_BUFFER_SIZE);
+if (log_buffer == NULL)
+  {
+  fprintf(stderr, "exim: failed to get store for log buffer\n");
+  exit(EXIT_FAILURE);
+  }
+
+/* Set log_stderr to stderr, provided that stderr exists. This gets reset to
+NULL when the daemon is run and the file is closed. We have to use this
+indirection, because some systems don't allow writing to the variable "stderr".
+*/
+
+if (fstat(fileno(stderr), &statbuf) >= 0) log_stderr = stderr;
+
+/* Arrange for the PCRE regex library to use our store functions. Note that
+the normal calls are actually macros that add additional arguments for
+debugging purposes so we have to assign specially constructed functions here.
+The default is to use store in the stacking pool, but this is overridden in the
+regex_must_compile() function. */
+
+pcre_malloc = function_store_get;
+pcre_free = function_dummy_free;
+
+/* Ensure there is a big buffer for temporary use in several places. It is put
+in malloc store so that it can be freed for enlargement if necessary. */
+
+big_buffer = store_malloc(big_buffer_size);
+
+/* Set up the handler for the data request signal, and set the initial
+descriptive text. */
+
+set_process_info("initializing");
+os_restarting_signal(SIGUSR1, usr1_handler);
+
+/* SIGHUP is used to get the daemon to reconfigure. It gets set as appropriate
+in the daemon code. For the rest of Exim's uses, we ignore it. */
+
+signal(SIGHUP, SIG_IGN);
+
+/* We don't want to die on pipe errors as the code is written to handle
+the write error instead. */
+
+signal(SIGPIPE, SIG_IGN);
+
+/* Under some circumstance on some OS, Exim can get called with SIGCHLD
+set to SIG_IGN. This causes subprocesses that complete before the parent
+process waits for them not to hang around, so when Exim calls wait(), nothing
+is there. The wait() code has been made robust against this, but let's ensure
+that SIGCHLD is set to SIG_DFL, because it's tidier to wait and get a process
+ending status. We use sigaction rather than plain signal() on those OS where
+SA_NOCLDWAIT exists, because we want to be sure it is turned off. (There was a
+problem on AIX with this.) */
+
+#ifdef SA_NOCLDWAIT
+  {
+  struct sigaction act;
+  act.sa_handler = SIG_DFL;
+  sigemptyset(&(act.sa_mask));
+  act.sa_flags = 0;
+  sigaction(SIGCHLD, &act, NULL);
+  }
+#else
+signal(SIGCHLD, SIG_DFL);
+#endif
+
+/* Save the arguments for use if we re-exec exim as a daemon after receiving
+SIGHUP. */
+
+sighup_argv = argv;
+
+/* Set up the version number. Set up the leading 'E' for the external form of
+message ids, set the pointer to the internal form, and initialize it to
+indicate no message being processed. */
+
+version_init();
+message_id_option[0] = '-';
+message_id_external = message_id_option + 1;
+message_id_external[0] = 'E';
+message_id = message_id_external + 1;
+message_id[0] = 0;
+
+/* Set the umask to zero so that any files Exim creates using open() are
+created with the modes that it specifies. NOTE: Files created with fopen() have
+a problem, which was not recognized till rather late (February 2006). With this
+umask, such files will be world writeable. (They are all content scanning files
+in the spool directory, which isn't world-accessible, so this is not a
+disaster, but it's untidy.) I don't want to change this overall setting,
+however, because it will interact badly with the open() calls. Instead, there's
+now a function called modefopen() that fiddles with the umask while calling
+fopen(). */
+
+(void)umask(0);
+
+/* Precompile the regular expression for matching a message id. Keep this in
+step with the code that generates ids in the accept.c module. We need to do
+this here, because the -M options check their arguments for syntactic validity
+using mac_ismsgid, which uses this. */
+
+regex_ismsgid =
+  regex_must_compile(US"^(?:[^\\W_]{6}-){2}[^\\W_]{2}$", FALSE, TRUE);
+
+/* Precompile the regular expression that is used for matching an SMTP error
+code, possibly extended, at the start of an error message. Note that the
+terminating whitespace character is included. */
+
+regex_smtp_code =
+  regex_must_compile(US"^\\d\\d\\d\\s(?:\\d\\.\\d\\d?\\d?\\.\\d\\d?\\d?\\s)?",
+    FALSE, TRUE);
+
+#ifdef WHITELIST_D_MACROS
+/* Precompile the regular expression used to filter the content of macros
+given to -D for permissibility. */
+
+regex_whitelisted_macro =
+  regex_must_compile(US"^[A-Za-z0-9_/.-]*$", FALSE, TRUE);
+#endif
+
+
+/* If the program is called as "mailq" treat it as equivalent to "exim -bp";
+this seems to be a generally accepted convention, since one finds symbolic
+links called "mailq" in standard OS configurations. */
+
+if ((namelen == 5 && Ustrcmp(argv[0], "mailq") == 0) ||
+    (namelen  > 5 && Ustrncmp(argv[0] + namelen - 6, "/mailq", 6) == 0))
+  {
+  list_queue = TRUE;
+  receiving_message = FALSE;
+  called_as = US"-mailq";
+  }
+
+/* If the program is called as "rmail" treat it as equivalent to
+"exim -i -oee", thus allowing UUCP messages to be input using non-SMTP mode,
+i.e. preventing a single dot on a line from terminating the message, and
+returning with zero return code, even in cases of error (provided an error
+message has been sent). */
+
+if ((namelen == 5 && Ustrcmp(argv[0], "rmail") == 0) ||
+    (namelen  > 5 && Ustrncmp(argv[0] + namelen - 6, "/rmail", 6) == 0))
+  {
+  dot_ends = FALSE;
+  called_as = US"-rmail";
+  errors_sender_rc = EXIT_SUCCESS;
+  }
+
+/* If the program is called as "rsmtp" treat it as equivalent to "exim -bS";
+this is a smail convention. */
+
+if ((namelen == 5 && Ustrcmp(argv[0], "rsmtp") == 0) ||
+    (namelen  > 5 && Ustrncmp(argv[0] + namelen - 6, "/rsmtp", 6) == 0))
+  {
+  smtp_input = smtp_batched_input = TRUE;
+  called_as = US"-rsmtp";
+  }
+
+/* If the program is called as "runq" treat it as equivalent to "exim -q";
+this is a smail convention. */
+
+if ((namelen == 4 && Ustrcmp(argv[0], "runq") == 0) ||
+    (namelen  > 4 && Ustrncmp(argv[0] + namelen - 5, "/runq", 5) == 0))
+  {
+  queue_interval = 0;
+  receiving_message = FALSE;
+  called_as = US"-runq";
+  }
+
+/* If the program is called as "newaliases" treat it as equivalent to
+"exim -bi"; this is a sendmail convention. */
+
+if ((namelen == 10 && Ustrcmp(argv[0], "newaliases") == 0) ||
+    (namelen  > 10 && Ustrncmp(argv[0] + namelen - 11, "/newaliases", 11) == 0))
+  {
+  bi_option = TRUE;
+  receiving_message = FALSE;
+  called_as = US"-newaliases";
+  }
+
+/* Save the original effective uid for a couple of uses later. It should
+normally be root, but in some esoteric environments it may not be. */
+
+original_euid = geteuid();
+
+/* Get the real uid and gid. If the caller is root, force the effective uid/gid
+to be the same as the real ones. This makes a difference only if Exim is setuid
+(or setgid) to something other than root, which could be the case in some
+special configurations. */
+
+real_uid = getuid();
+real_gid = getgid();
+
+if (real_uid == root_uid)
+  {
+  rv = setgid(real_gid);
+  if (rv)
+    {
+    fprintf(stderr, "exim: setgid(%ld) failed: %s\n",
+        (long int)real_gid, strerror(errno));
+    exit(EXIT_FAILURE);
+    }
+  rv = setuid(real_uid);
+  if (rv)
+    {
+    fprintf(stderr, "exim: setuid(%ld) failed: %s\n",
+        (long int)real_uid, strerror(errno));
+    exit(EXIT_FAILURE);
+    }
+  }
+
+/* If neither the original real uid nor the original euid was root, Exim is
+running in an unprivileged state. */
+
+unprivileged = (real_uid != root_uid && original_euid != root_uid);
+
+/* Scan the program's arguments. Some can be dealt with right away; others are
+simply recorded for checking and handling afterwards. Do a high-level switch
+on the second character (the one after '-'), to save some effort. */
+
+for (i = 1; i < argc; i++)
+  {
+  BOOL badarg = FALSE;
+  uschar *arg = argv[i];
+  uschar *argrest;
+  int switchchar;
+
+  /* An argument not starting with '-' is the start of a recipients list;
+  break out of the options-scanning loop. */
+
+  if (arg[0] != '-')
+    {
+    recipients_arg = i;
+    break;
+    }
+
+  /* An option consistion of -- terminates the options */
+
+  if (Ustrcmp(arg, "--") == 0)
+    {
+    recipients_arg = i + 1;
+    break;
+    }
+
+  /* Handle flagged options */
+
+  switchchar = arg[1];
+  argrest = arg+2;
+
+  /* Make all -ex options synonymous with -oex arguments, since that
+  is assumed by various callers. Also make -qR options synonymous with -R
+  options, as that seems to be required as well. Allow for -qqR too, and
+  the same for -S options. */
+
+  if (Ustrncmp(arg+1, "oe", 2) == 0 ||
+      Ustrncmp(arg+1, "qR", 2) == 0 ||
+      Ustrncmp(arg+1, "qS", 2) == 0)
+    {
+    switchchar = arg[2];
+    argrest++;
+    }
+  else if (Ustrncmp(arg+1, "qqR", 3) == 0 || Ustrncmp(arg+1, "qqS", 3) == 0)
+    {
+    switchchar = arg[3];
+    argrest += 2;
+    queue_2stage = TRUE;
+    }
+
+  /* Make -r synonymous with -f, since it is a documented alias */
+
+  else if (arg[1] == 'r') switchchar = 'f';
+
+  /* Make -ov synonymous with -v */
+
+  else if (Ustrcmp(arg, "-ov") == 0)
+    {
+    switchchar = 'v';
+    argrest++;
+    }
+
+  /* deal with --option_aliases */
+  else if (switchchar == '-')
+    {
+    if (Ustrcmp(argrest, "help") == 0)
+      {
+      usage_wanted = TRUE;
+      break;
+      }
+    else if (Ustrcmp(argrest, "version") == 0)
+      {
+      switchchar = 'b';
+      argrest = US"V";
+      }
+    }
+
+  /* High-level switch on active initial letter */
+
+  switch(switchchar)
+    {
+
+    /* sendmail uses -Ac and -Am to control which .cf file is used;
+    we ignore them. */
+    case 'A':
+    if (*argrest == '\0') { badarg = TRUE; break; }
+    else
+      {
+      BOOL ignore = FALSE;
+      switch (*argrest)
+        {
+        case 'c':
+        case 'm':
+          if (*(argrest + 1) == '\0')
+            ignore = TRUE;
+          break;
+        }
+      if (!ignore) { badarg = TRUE; break; }
+      }
+    break;
+
+    /* -Btype is a sendmail option for 7bit/8bit setting. Exim is 8-bit clean
+    so has no need of it. */
+
+    case 'B':
+    if (*argrest == 0) i++;       /* Skip over the type */
+    break;
+
+
+    case 'b':
+    receiving_message = FALSE;    /* Reset TRUE for -bm, -bS, -bs below */
+
+    /* -bd:  Run in daemon mode, awaiting SMTP connections.
+       -bdf: Ditto, but in the foreground.
+    */
+
+    if (*argrest == 'd')
+      {
+      daemon_listen = TRUE;
+      if (*(++argrest) == 'f') background_daemon = FALSE;
+        else if (*argrest != 0) { badarg = TRUE; break; }
+      }
+
+    /* -be:  Run in expansion test mode
+       -bem: Ditto, but read a message from a file first
+    */
+
+    else if (*argrest == 'e')
+      {
+      expansion_test = checking = TRUE;
+      if (argrest[1] == 'm')
+        {
+        if (++i >= argc) { badarg = TRUE; break; }
+        expansion_test_message = argv[i];
+        argrest++;
+        }
+      if (argrest[1] != 0) { badarg = TRUE; break; }
+      }
+
+    /* -bF:  Run system filter test */
+
+    else if (*argrest == 'F')
+      {
+      filter_test |= FTEST_SYSTEM;
+      if (*(++argrest) != 0) { badarg = TRUE; break; }
+      if (++i < argc) filter_test_sfile = argv[i]; else
+        {
+        fprintf(stderr, "exim: file name expected after %s\n", argv[i-1]);
+        exit(EXIT_FAILURE);
+        }
+      }
+
+    /* -bf:  Run user filter test
+       -bfd: Set domain for filter testing
+       -bfl: Set local part for filter testing
+       -bfp: Set prefix for filter testing
+       -bfs: Set suffix for filter testing
+    */
+
+    else if (*argrest == 'f')
+      {
+      if (*(++argrest) == 0)
+        {
+        filter_test |= FTEST_USER;
+        if (++i < argc) filter_test_ufile = argv[i]; else
+          {
+          fprintf(stderr, "exim: file name expected after %s\n", argv[i-1]);
+          exit(EXIT_FAILURE);
+          }
+        }
+      else
+        {
+        if (++i >= argc)
+          {
+          fprintf(stderr, "exim: string expected after %s\n", arg);
+          exit(EXIT_FAILURE);
+          }
+        if (Ustrcmp(argrest, "d") == 0) ftest_domain = argv[i];
+        else if (Ustrcmp(argrest, "l") == 0) ftest_localpart = argv[i];
+        else if (Ustrcmp(argrest, "p") == 0) ftest_prefix = argv[i];
+        else if (Ustrcmp(argrest, "s") == 0) ftest_suffix = argv[i];
+        else { badarg = TRUE; break; }
+        }
+      }
+
+    /* -bh: Host checking - an IP address must follow. */
+
+    else if (Ustrcmp(argrest, "h") == 0 || Ustrcmp(argrest, "hc") == 0)
+      {
+      if (++i >= argc) { badarg = TRUE; break; }
+      sender_host_address = argv[i];
+      host_checking = checking = log_testing_mode = TRUE;
+      host_checking_callout = argrest[1] == 'c';
+      }
+
+    /* -bi: This option is used by sendmail to initialize *the* alias file,
+    though it has the -oA option to specify a different file. Exim has no
+    concept of *the* alias file, but since Sun's YP make script calls
+    sendmail this way, some support must be provided. */
+
+    else if (Ustrcmp(argrest, "i") == 0) bi_option = TRUE;
+
+    /* -bI: provide information, of the type to follow after a colon.
+    This is an Exim flag. */
+
+    else if (argrest[0] == 'I' && Ustrlen(argrest) >= 2 && argrest[1] == ':')
+      {
+      uschar *p = &argrest[2];
+      info_flag = CMDINFO_HELP;
+      if (Ustrlen(p))
+        {
+        if (strcmpic(p, CUS"sieve") == 0)
+          {
+          info_flag = CMDINFO_SIEVE;
+          info_stdout = TRUE;
+          }
+        else if (strcmpic(p, CUS"dscp") == 0)
+          {
+          info_flag = CMDINFO_DSCP;
+          info_stdout = TRUE;
+          }
+        else if (strcmpic(p, CUS"help") == 0)
+          {
+          info_stdout = TRUE;
+          }
+        }
+      }
+
+    /* -bm: Accept and deliver message - the default option. Reinstate
+    receiving_message, which got turned off for all -b options. */
+
+    else if (Ustrcmp(argrest, "m") == 0) receiving_message = TRUE;
+
+    /* -bmalware: test the filename given for malware */
+
+    else if (Ustrcmp(argrest, "malware") == 0)
+      {
+      if (++i >= argc) { badarg = TRUE; break; }
+      malware_test_file = argv[i];
+      }
+
+    /* -bnq: For locally originating messages, do not qualify unqualified
+    addresses. In the envelope, this causes errors; in header lines they
+    just get left. */
+
+    else if (Ustrcmp(argrest, "nq") == 0)
+      {
+      allow_unqualified_sender = FALSE;
+      allow_unqualified_recipient = FALSE;
+      }
+
+    /* -bpxx: List the contents of the mail queue, in various forms. If
+    the option is -bpc, just a queue count is needed. Otherwise, if the
+    first letter after p is r, then order is random. */
+
+    else if (*argrest == 'p')
+      {
+      if (*(++argrest) == 'c')
+        {
+        count_queue = TRUE;
+        if (*(++argrest) != 0) badarg = TRUE;
+        break;
+        }
+
+      if (*argrest == 'r')
+        {
+        list_queue_option = 8;
+        argrest++;
+        }
+      else list_queue_option = 0;
+
+      list_queue = TRUE;
+
+      /* -bp: List the contents of the mail queue, top-level only */
+
+      if (*argrest == 0) {}
+
+      /* -bpu: List the contents of the mail queue, top-level undelivered */
+
+      else if (Ustrcmp(argrest, "u") == 0) list_queue_option += 1;
+
+      /* -bpa: List the contents of the mail queue, including all delivered */
+
+      else if (Ustrcmp(argrest, "a") == 0) list_queue_option += 2;
+
+      /* Unknown after -bp[r] */
+
+      else
+        {
+        badarg = TRUE;
+        break;
+        }
+      }
+
+
+    /* -bP: List the configuration variables given as the address list.
+    Force -v, so configuration errors get displayed. */
+
+    else if (Ustrcmp(argrest, "P") == 0)
+      {
+      list_options = TRUE;
+      debug_selector |= D_v;
+      debug_file = stderr;
+      }
+
+    /* -brt: Test retry configuration lookup */
+
+    else if (Ustrcmp(argrest, "rt") == 0)
+      {
+      test_retry_arg = i + 1;
+      goto END_ARG;
+      }
+
+    /* -brw: Test rewrite configuration */
+
+    else if (Ustrcmp(argrest, "rw") == 0)
+      {
+      test_rewrite_arg = i + 1;
+      goto END_ARG;
+      }
+
+    /* -bS: Read SMTP commands on standard input, but produce no replies -
+    all errors are reported by sending messages. */
+
+    else if (Ustrcmp(argrest, "S") == 0)
+      smtp_input = smtp_batched_input = receiving_message = TRUE;
+
+    /* -bs: Read SMTP commands on standard input and produce SMTP replies
+    on standard output. */
+
+    else if (Ustrcmp(argrest, "s") == 0) smtp_input = receiving_message = TRUE;
+
+    /* -bt: address testing mode */
+
+    else if (Ustrcmp(argrest, "t") == 0)
+      address_test_mode = checking = log_testing_mode = TRUE;
+
+    /* -bv: verify addresses */
+
+    else if (Ustrcmp(argrest, "v") == 0)
+      verify_address_mode = checking = log_testing_mode = TRUE;
+
+    /* -bvs: verify sender addresses */
+
+    else if (Ustrcmp(argrest, "vs") == 0)
+      {
+      verify_address_mode = checking = log_testing_mode = TRUE;
+      verify_as_sender = TRUE;
+      }
+
+    /* -bV: Print version string and support details */
+
+    else if (Ustrcmp(argrest, "V") == 0)
+      {
+      printf("Exim version %s #%s built %s\n", version_string,
+        version_cnumber, version_date);
+      printf("%s\n", CS version_copyright);
+      version_printed = TRUE;
+      show_whats_supported(stdout);
+      }
+
+    /* -bw: inetd wait mode, accept a listening socket as stdin */
+
+    else if (*argrest == 'w')
+      {
+      inetd_wait_mode = TRUE;
+      background_daemon = FALSE;
+      daemon_listen = TRUE;
+      if (*(++argrest) != '\0')
+        {
+        inetd_wait_timeout = readconf_readtime(argrest, 0, FALSE);
+        if (inetd_wait_timeout <= 0)
+          {
+          fprintf(stderr, "exim: bad time value %s: abandoned\n", argv[i]);
+          exit(EXIT_FAILURE);
+          }
+        }
+      }
+
+    else badarg = TRUE;
+    break;
+
+
+    /* -C: change configuration file list; ignore if it isn't really
+    a change! Enforce a prefix check if required. */
+
+    case 'C':
+    if (*argrest == 0)
+      {
+      if(++i < argc) argrest = argv[i]; else
+        { badarg = TRUE; break; }
+      }
+    if (Ustrcmp(config_main_filelist, argrest) != 0)
+      {
+      #ifdef ALT_CONFIG_PREFIX
+      int sep = 0;
+      int len = Ustrlen(ALT_CONFIG_PREFIX);
+      uschar *list = argrest;
+      uschar *filename;
+      while((filename = string_nextinlist(&list, &sep, big_buffer,
+             big_buffer_size)) != NULL)
+        {
+        if ((Ustrlen(filename) < len ||
+             Ustrncmp(filename, ALT_CONFIG_PREFIX, len) != 0 ||
+             Ustrstr(filename, "/../") != NULL) &&
+             (Ustrcmp(filename, "/dev/null") != 0 || real_uid != root_uid))
+          {
+          fprintf(stderr, "-C Permission denied\n");
+          exit(EXIT_FAILURE);
+          }
+        }
+      #endif
+      if (real_uid != root_uid)
+        {
+        #ifdef TRUSTED_CONFIG_LIST
+
+        if (real_uid != exim_uid
+            #ifdef CONFIGURE_OWNER
+            && real_uid != config_uid
+            #endif
+            )
+          trusted_config = FALSE;
+        else
+          {
+          FILE *trust_list = Ufopen(TRUSTED_CONFIG_LIST, "rb");
+          if (trust_list)
+            {
+            struct stat statbuf;
+
+            if (fstat(fileno(trust_list), &statbuf) != 0 ||
+                (statbuf.st_uid != root_uid        /* owner not root */
+                 #ifdef CONFIGURE_OWNER
+                 && statbuf.st_uid != config_uid   /* owner not the special one */
+                 #endif
+                   ) ||                            /* or */
+                (statbuf.st_gid != root_gid        /* group not root */
+                 #ifdef CONFIGURE_GROUP
+                 && statbuf.st_gid != config_gid   /* group not the special one */
+                 #endif
+                 && (statbuf.st_mode & 020) != 0   /* group writeable */
+                   ) ||                            /* or */
+                (statbuf.st_mode & 2) != 0)        /* world writeable */
+              {
+              trusted_config = FALSE;
+              fclose(trust_list);
+              }
+           else
+              {
+              /* Well, the trust list at least is up to scratch... */
+              void *reset_point = store_get(0);
+              uschar *trusted_configs[32];
+              int nr_configs = 0;
+              int i = 0;
+
+              while (Ufgets(big_buffer, big_buffer_size, trust_list))
+                {
+                uschar *start = big_buffer, *nl;
+                while (*start && isspace(*start))
+                start++;
+                if (*start != '/')
+                  continue;
+                nl = Ustrchr(start, '\n');
+                if (nl)
+                  *nl = 0;
+                trusted_configs[nr_configs++] = string_copy(start);
+                if (nr_configs == 32)
+                  break;
+                }
+              fclose(trust_list);
+
+              if (nr_configs)
+                {
+                int sep = 0;
+                uschar *list = argrest;
+                uschar *filename;
+                while (trusted_config && (filename = string_nextinlist(&list,
+                        &sep, big_buffer, big_buffer_size)) != NULL)
+                  {
+                  for (i=0; i < nr_configs; i++)
+                    {
+                    if (Ustrcmp(filename, trusted_configs[i]) == 0)
+                      break;
+                    }
+                  if (i == nr_configs)
+                    {
+                    trusted_config = FALSE;
+                    break;
+                    }
+                  }
+                store_reset(reset_point);
+                }
+              else
+                {
+                /* No valid prefixes found in trust_list file. */
+                trusted_config = FALSE;
+                }
+              }
+           }
+          else
+            {
+            /* Could not open trust_list file. */
+            trusted_config = FALSE;
+            }
+          }
+      #else
+        /* Not root; don't trust config */
+        trusted_config = FALSE;
+      #endif
+        }
+
+      config_main_filelist = argrest;
+      config_changed = TRUE;
+      }
+    break;
+
+
+    /* -D: set up a macro definition */
+
+    case 'D':
+    #ifdef DISABLE_D_OPTION
+    fprintf(stderr, "exim: -D is not available in this Exim binary\n");
+    exit(EXIT_FAILURE);
+    #else
+      {
+      int ptr = 0;
+      macro_item *mlast = NULL;
+      macro_item *m;
+      uschar name[24];
+      uschar *s = argrest;
+
+      while (isspace(*s)) s++;
+
+      if (*s < 'A' || *s > 'Z')
+        {
+        fprintf(stderr, "exim: macro name set by -D must start with "
+          "an upper case letter\n");
+        exit(EXIT_FAILURE);
+        }
+
+      while (isalnum(*s) || *s == '_')
+        {
+        if (ptr < sizeof(name)-1) name[ptr++] = *s;
+        s++;
+        }
+      name[ptr] = 0;
+      if (ptr == 0) { badarg = TRUE; break; }
+      while (isspace(*s)) s++;
+      if (*s != 0)
+        {
+        if (*s++ != '=') { badarg = TRUE; break; }
+        while (isspace(*s)) s++;
+        }
+
+      for (m = macros; m != NULL; m = m->next)
+        {
+        if (Ustrcmp(m->name, name) == 0)
+          {
+          fprintf(stderr, "exim: duplicated -D in command line\n");
+          exit(EXIT_FAILURE);
+          }
+        mlast = m;
+        }
+
+      m = store_get(sizeof(macro_item) + Ustrlen(name));
+      m->next = NULL;
+      m->command_line = TRUE;
+      if (mlast == NULL) macros = m; else mlast->next = m;
+      Ustrcpy(m->name, name);
+      m->replacement = string_copy(s);
+
+      if (clmacro_count >= MAX_CLMACROS)
+        {
+        fprintf(stderr, "exim: too many -D options on command line\n");
+        exit(EXIT_FAILURE);
+        }
+      clmacros[clmacro_count++] = string_sprintf("-D%s=%s", m->name,
+        m->replacement);
+      }
+    #endif
+    break;
+
+    /* -d: Set debug level (see also -v below) or set the drop_cr option.
+    The latter is now a no-op, retained for compatibility only. If -dd is used,
+    debugging subprocesses of the daemon is disabled. */
+
+    case 'd':
+    if (Ustrcmp(argrest, "ropcr") == 0)
+      {
+      /* drop_cr = TRUE; */
+      }
+
+    /* Use an intermediate variable so that we don't set debugging while
+    decoding the debugging bits. */
+
+    else
+      {
+      unsigned int selector = D_default;
+      debug_selector = 0;
+      debug_file = NULL;
+      if (*argrest == 'd')
+        {
+        debug_daemon = TRUE;
+        argrest++;
+        }
+      if (*argrest != 0)
+        decode_bits(&selector, NULL, D_memory, 0, argrest, debug_options,
+          debug_options_count, US"debug", 0);
+      debug_selector = selector;
+      }
+    break;
+
+
+    /* -E: This is a local error message. This option is not intended for
+    external use at all, but is not restricted to trusted callers because it
+    does no harm (just suppresses certain error messages) and if Exim is run
+    not setuid root it won't always be trusted when it generates error
+    messages using this option. If there is a message id following -E, point
+    message_reference at it, for logging. */
+
+    case 'E':
+    local_error_message = TRUE;
+    if (mac_ismsgid(argrest)) message_reference = argrest;
+    break;
+
+
+    /* -ex: The vacation program calls sendmail with the undocumented "-eq"
+    option, so it looks as if historically the -oex options are also callable
+    without the leading -o. So we have to accept them. Before the switch,
+    anything starting -oe has been converted to -e. Exim does not support all
+    of the sendmail error options. */
+
+    case 'e':
+    if (Ustrcmp(argrest, "e") == 0)
+      {
+      arg_error_handling = ERRORS_SENDER;
+      errors_sender_rc = EXIT_SUCCESS;
+      }
+    else if (Ustrcmp(argrest, "m") == 0) arg_error_handling = ERRORS_SENDER;
+    else if (Ustrcmp(argrest, "p") == 0) arg_error_handling = ERRORS_STDERR;
+    else if (Ustrcmp(argrest, "q") == 0) arg_error_handling = ERRORS_STDERR;
+    else if (Ustrcmp(argrest, "w") == 0) arg_error_handling = ERRORS_SENDER;
+    else badarg = TRUE;
+    break;
+
+
+    /* -F: Set sender's full name, used instead of the gecos entry from
+    the password file. Since users can usually alter their gecos entries,
+    there's no security involved in using this instead. The data can follow
+    the -F or be in the next argument. */
+
+    case 'F':
+    if (*argrest == 0)
+      {
+      if(++i < argc) argrest = argv[i]; else
+        { badarg = TRUE; break; }
+      }
+    originator_name = argrest;
+    sender_name_forced = TRUE;
+    break;
+
+
+    /* -f: Set sender's address - this value is only actually used if Exim is
+    run by a trusted user, or if untrusted_set_sender is set and matches the
+    address, except that the null address can always be set by any user. The
+    test for this happens later, when the value given here is ignored when not
+    permitted. For an untrusted user, the actual sender is still put in Sender:
+    if it doesn't match the From: header (unless no_local_from_check is set).
+    The data can follow the -f or be in the next argument. The -r switch is an
+    obsolete form of -f but since there appear to be programs out there that
+    use anything that sendmail has ever supported, better accept it - the
+    synonymizing is done before the switch above.
+
+    At this stage, we must allow domain literal addresses, because we don't
+    know what the setting of allow_domain_literals is yet. Ditto for trailing
+    dots and strip_trailing_dot. */
+
+    case 'f':
+      {
+      int start, end;
+      uschar *errmess;
+      if (*argrest == 0)
+        {
+        if (i+1 < argc) argrest = argv[++i]; else
+          { badarg = TRUE; break; }
+        }
+      if (*argrest == 0)
+        {
+        sender_address = string_sprintf("");  /* Ensure writeable memory */
+        }
+      else
+        {
+        uschar *temp = argrest + Ustrlen(argrest) - 1;
+        while (temp >= argrest && isspace(*temp)) temp--;
+        if (temp >= argrest && *temp == '.') f_end_dot = TRUE;
+        allow_domain_literals = TRUE;
+        strip_trailing_dot = TRUE;
+        sender_address = parse_extract_address(argrest, &errmess, &start, &end,
+          &sender_address_domain, TRUE);
+        allow_domain_literals = FALSE;
+        strip_trailing_dot = FALSE;
+        if (sender_address == NULL)
+          {
+          fprintf(stderr, "exim: bad -f address \"%s\": %s\n", argrest, errmess);
+          return EXIT_FAILURE;
+          }
+        }
+      sender_address_forced = TRUE;
+      }
+    break;
+
+    /* -G: sendmail invocation to specify that it's a gateway submission and
+    sendmail may complain about problems instead of fixing them.
+    We make it equivalent to an ACL "control = suppress_local_fixups" and do
+    not at this time complain about problems. */
+
+    case 'G':
+    flag_G = TRUE;
+    break;
+
+    /* -h: Set the hop count for an incoming message. Exim does not currently
+    support this; it always computes it by counting the Received: headers.
+    To put it in will require a change to the spool header file format. */
+
+    case 'h':
+    if (*argrest == 0)
+      {
+      if(++i < argc) argrest = argv[i]; else
+        { badarg = TRUE; break; }
+      }
+    if (!isdigit(*argrest)) badarg = TRUE;
+    break;
+
+
+    /* -i: Set flag so dot doesn't end non-SMTP input (same as -oi, seems
+    not to be documented for sendmail but mailx (at least) uses it) */
+
+    case 'i':
+    if (*argrest == 0) dot_ends = FALSE; else badarg = TRUE;
+    break;
+
+
+    /* -L: set the identifier used for syslog; equivalent to setting
+    syslog_processname in the config file, but needs to be an admin option. */
+
+    case 'L':
+    if (*argrest == '\0')
+      {
+      if(++i < argc) argrest = argv[i]; else
+        { badarg = TRUE; break; }
+      }
+    sz = Ustrlen(argrest);
+    if (sz > 32)
+      {
+      fprintf(stderr, "exim: the -L syslog name is too long: \"%s\"\n", argrest);
+      return EXIT_FAILURE;
+      }
+    if (sz < 1)
+      {
+      fprintf(stderr, "exim: the -L syslog name is too short\n");
+      return EXIT_FAILURE;
+      }
+    cmdline_syslog_name = argrest;
+    break;
+
+    case 'M':
+    receiving_message = FALSE;
+
+    /* -MC:  continue delivery of another message via an existing open
+    file descriptor. This option is used for an internal call by the
+    smtp transport when there is a pending message waiting to go to an
+    address to which it has got a connection. Five subsequent arguments are
+    required: transport name, host name, IP address, sequence number, and
+    message_id. Transports may decline to create new processes if the sequence
+    number gets too big. The channel is stdin. This (-MC) must be the last
+    argument. There's a subsequent check that the real-uid is privileged.
+
+    If we are running in the test harness. delay for a bit, to let the process
+    that set this one up complete. This makes for repeatability of the logging,
+    etc. output. */
+
+    if (Ustrcmp(argrest, "C") == 0)
+      {
+      union sockaddr_46 interface_sock;
+      EXIM_SOCKLEN_T size = sizeof(interface_sock);
+
+      if (argc != i + 6)
+        {
+        fprintf(stderr, "exim: too many or too few arguments after -MC\n");
+        return EXIT_FAILURE;
+        }
+
+      if (msg_action_arg >= 0)
+        {
+        fprintf(stderr, "exim: incompatible arguments\n");
+        return EXIT_FAILURE;
+        }
+
+      continue_transport = argv[++i];
+      continue_hostname = argv[++i];
+      continue_host_address = argv[++i];
+      continue_sequence = Uatoi(argv[++i]);
+      msg_action = MSG_DELIVER;
+      msg_action_arg = ++i;
+      forced_delivery = TRUE;
+      queue_run_pid = passed_qr_pid;
+      queue_run_pipe = passed_qr_pipe;
+
+      if (!mac_ismsgid(argv[i]))
+        {
+        fprintf(stderr, "exim: malformed message id %s after -MC option\n",
+          argv[i]);
+        return EXIT_FAILURE;
+        }
+
+      /* Set up $sending_ip_address and $sending_port */
+
+      if (getsockname(fileno(stdin), (struct sockaddr *)(&interface_sock),
+          &size) == 0)
+        sending_ip_address = host_ntoa(-1, &interface_sock, NULL,
+          &sending_port);
+      else
+        {
+        fprintf(stderr, "exim: getsockname() failed after -MC option: %s\n",
+          strerror(errno));
+        return EXIT_FAILURE;
+        }
+
+      if (running_in_test_harness) millisleep(500);
+      break;
+      }
+
+    /* -MCA: set the smtp_authenticated flag; this is useful only when it
+    precedes -MC (see above). The flag indicates that the host to which
+    Exim is connected has accepted an AUTH sequence. */
+
+    else if (Ustrcmp(argrest, "CA") == 0)
+      {
+      smtp_authenticated = TRUE;
+      break;
+      }
+
+    #ifdef EXPERIMENTAL_DSN
+    /* -MCD: set the smtp_use_dsn flag; this indicates that the host
+       that exim is connected to supports the esmtp extension DSN */
+    else if (strcmp(argrest, "CD") == 0)
+      {
+      smtp_use_dsn = TRUE;
+      break;
+      }
+    #endif
+
+    /* -MCP: set the smtp_use_pipelining flag; this is useful only when
+    it preceded -MC (see above) */
+
+    else if (Ustrcmp(argrest, "CP") == 0)
+      {
+      smtp_use_pipelining = TRUE;
+      break;
+      }
+
+    /* -MCQ: pass on the pid of the queue-running process that started
+    this chain of deliveries and the fd of its synchronizing pipe; this
+    is useful only when it precedes -MC (see above) */
+
+    else if (Ustrcmp(argrest, "CQ") == 0)
+      {
+      if(++i < argc) passed_qr_pid = (pid_t)(Uatol(argv[i]));
+        else badarg = TRUE;
+      if(++i < argc) passed_qr_pipe = (int)(Uatol(argv[i]));
+        else badarg = TRUE;
+      break;
+      }
+
+    /* -MCS: set the smtp_use_size flag; this is useful only when it
+    precedes -MC (see above) */
+
+    else if (Ustrcmp(argrest, "CS") == 0)
+      {
+      smtp_use_size = TRUE;
+      break;
+      }
+
+    /* -MCT: set the tls_offered flag; this is useful only when it
+    precedes -MC (see above). The flag indicates that the host to which
+    Exim is connected has offered TLS support. */
+
+    #ifdef SUPPORT_TLS
+    else if (Ustrcmp(argrest, "CT") == 0)
+      {
+      tls_offered = TRUE;
+      break;
+      }
+    #endif
+
+    /* -M[x]: various operations on the following list of message ids:
+       -M    deliver the messages, ignoring next retry times and thawing
+       -Mc   deliver the messages, checking next retry times, no thawing
+       -Mf   freeze the messages
+       -Mg   give up on the messages
+       -Mt   thaw the messages
+       -Mrm  remove the messages
+    In the above cases, this must be the last option. There are also the
+    following options which are followed by a single message id, and which
+    act on that message. Some of them use the "recipient" addresses as well.
+       -Mar  add recipient(s)
+       -Mmad mark all recipients delivered
+       -Mmd  mark recipients(s) delivered
+       -Mes  edit sender
+       -Mset load a message for use with -be
+       -Mvb  show body
+       -Mvc  show copy (of whole message, in RFC 2822 format)
+       -Mvh  show header
+       -Mvl  show log
+    */
+
+    else if (*argrest == 0)
+      {
+      msg_action = MSG_DELIVER;
+      forced_delivery = deliver_force_thaw = TRUE;
+      }
+    else if (Ustrcmp(argrest, "ar") == 0)
+      {
+      msg_action = MSG_ADD_RECIPIENT;
+      one_msg_action = TRUE;
+      }
+    else if (Ustrcmp(argrest, "c") == 0)  msg_action = MSG_DELIVER;
+    else if (Ustrcmp(argrest, "es") == 0)
+      {
+      msg_action = MSG_EDIT_SENDER;
+      one_msg_action = TRUE;
+      }
+    else if (Ustrcmp(argrest, "f") == 0)  msg_action = MSG_FREEZE;
+    else if (Ustrcmp(argrest, "g") == 0)
+      {
+      msg_action = MSG_DELIVER;
+      deliver_give_up = TRUE;
+      }
+    else if (Ustrcmp(argrest, "mad") == 0)
+      {
+      msg_action = MSG_MARK_ALL_DELIVERED;
+      }
+    else if (Ustrcmp(argrest, "md") == 0)
+      {
+      msg_action = MSG_MARK_DELIVERED;
+      one_msg_action = TRUE;
+      }
+    else if (Ustrcmp(argrest, "rm") == 0) msg_action = MSG_REMOVE;
+    else if (Ustrcmp(argrest, "set") == 0)
+      {
+      msg_action = MSG_LOAD;
+      one_msg_action = TRUE;
+      }
+    else if (Ustrcmp(argrest, "t") == 0)  msg_action = MSG_THAW;
+    else if (Ustrcmp(argrest, "vb") == 0)
+      {
+      msg_action = MSG_SHOW_BODY;
+      one_msg_action = TRUE;
+      }
+    else if (Ustrcmp(argrest, "vc") == 0)
+      {
+      msg_action = MSG_SHOW_COPY;
+      one_msg_action = TRUE;
+      }
+    else if (Ustrcmp(argrest, "vh") == 0)
+      {
+      msg_action = MSG_SHOW_HEADER;
+      one_msg_action = TRUE;
+      }
+    else if (Ustrcmp(argrest, "vl") == 0)
+      {
+      msg_action = MSG_SHOW_LOG;
+      one_msg_action = TRUE;
+      }
+    else { badarg = TRUE; break; }
+
+    /* All the -Mxx options require at least one message id. */
+
+    msg_action_arg = i + 1;
+    if (msg_action_arg >= argc)
+      {
+      fprintf(stderr, "exim: no message ids given after %s option\n", arg);
+      return EXIT_FAILURE;
+      }
+
+    /* Some require only message ids to follow */
+
+    if (!one_msg_action)
+      {
+      int j;
+      for (j = msg_action_arg; j < argc; j++) if (!mac_ismsgid(argv[j]))
+        {
+        fprintf(stderr, "exim: malformed message id %s after %s option\n",
+          argv[j], arg);
+        return EXIT_FAILURE;
+        }
+      goto END_ARG;   /* Remaining args are ids */
+      }
+
+    /* Others require only one message id, possibly followed by addresses,
+    which will be handled as normal arguments. */
+
+    else
+      {
+      if (!mac_ismsgid(argv[msg_action_arg]))
+        {
+        fprintf(stderr, "exim: malformed message id %s after %s option\n",
+          argv[msg_action_arg], arg);
+        return EXIT_FAILURE;
+        }
+      i++;
+      }
+    break;
+
+
+    /* Some programs seem to call the -om option without the leading o;
+    for sendmail it askes for "me too". Exim always does this. */
+
+    case 'm':
+    if (*argrest != 0) badarg = TRUE;
+    break;
+
+
+    /* -N: don't do delivery - a debugging option that stops transports doing
+    their thing. It implies debugging at the D_v level. */
+
+    case 'N':
+    if (*argrest == 0)
+      {
+      dont_deliver = TRUE;
+      debug_selector |= D_v;
+      debug_file = stderr;
+      }
+    else badarg = TRUE;
+    break;
+
+
+    /* -n: This means "don't alias" in sendmail, apparently.
+    For normal invocations, it has no effect.
+    It may affect some other options. */
+
+    case 'n':
+    flag_n = TRUE;
+    break;
+
+    /* -O: Just ignore it. In sendmail, apparently -O option=value means set
+    option to the specified value. This form uses long names. We need to handle
+    -O option=value and -Ooption=value. */
+
+    case 'O':
+    if (*argrest == 0)
+      {
+      if (++i >= argc)
+        {
+        fprintf(stderr, "exim: string expected after -O\n");
+        exit(EXIT_FAILURE);
+        }
+      }
+    break;
+
+    case 'o':
+
+    /* -oA: Set an argument for the bi command (sendmail's "alternate alias
+    file" option). */
+
+    if (*argrest == 'A')
+      {
+      alias_arg = argrest + 1;
+      if (alias_arg[0] == 0)
+        {
+        if (i+1 < argc) alias_arg = argv[++i]; else
+          {
+          fprintf(stderr, "exim: string expected after -oA\n");
+          exit(EXIT_FAILURE);
+          }
+        }
+      }
+
+    /* -oB: Set a connection message max value for remote deliveries */
+
+    else if (*argrest == 'B')
+      {
+      uschar *p = argrest + 1;
+      if (p[0] == 0)
+        {
+        if (i+1 < argc && isdigit((argv[i+1][0]))) p = argv[++i]; else
+          {
+          connection_max_messages = 1;
+          p = NULL;
+          }
+        }
+
+      if (p != NULL)
+        {
+        if (!isdigit(*p))
+          {
+          fprintf(stderr, "exim: number expected after -oB\n");
+          exit(EXIT_FAILURE);
+          }
+        connection_max_messages = Uatoi(p);
+        }
+      }
+
+    /* -odb: background delivery */
+
+    else if (Ustrcmp(argrest, "db") == 0)
+      {
+      synchronous_delivery = FALSE;
+      arg_queue_only = FALSE;
+      queue_only_set = TRUE;
+      }
+
+    /* -odf: foreground delivery (smail-compatible option); same effect as
+       -odi: interactive (synchronous) delivery (sendmail-compatible option)
+    */
+
+    else if (Ustrcmp(argrest, "df") == 0 || Ustrcmp(argrest, "di") == 0)
+      {
+      synchronous_delivery = TRUE;
+      arg_queue_only = FALSE;
+      queue_only_set = TRUE;
+      }
+
+    /* -odq: queue only */
+
+    else if (Ustrcmp(argrest, "dq") == 0)
+      {
+      synchronous_delivery = FALSE;
+      arg_queue_only = TRUE;
+      queue_only_set = TRUE;
+      }
+
+    /* -odqs: queue SMTP only - do local deliveries and remote routing,
+    but no remote delivery */
+
+    else if (Ustrcmp(argrest, "dqs") == 0)
+      {
+      queue_smtp = TRUE;
+      arg_queue_only = FALSE;
+      queue_only_set = TRUE;
+      }
+
+    /* -oex: Sendmail error flags. As these are also accepted without the
+    leading -o prefix, for compatibility with vacation and other callers,
+    they are handled with -e above. */
+
+    /* -oi:     Set flag so dot doesn't end non-SMTP input (same as -i)
+       -oitrue: Another sendmail syntax for the same */
+
+    else if (Ustrcmp(argrest, "i") == 0 ||
+             Ustrcmp(argrest, "itrue") == 0)
+      dot_ends = FALSE;
+
+    /* -oM*: Set various characteristics for an incoming message; actually
+    acted on for trusted callers only. */
+
+    else if (*argrest == 'M')
+      {
+      if (i+1 >= argc)
+        {
+        fprintf(stderr, "exim: data expected after -o%s\n", argrest);
+        exit(EXIT_FAILURE);
+        }
+
+      /* -oMa: Set sender host address */
+
+      if (Ustrcmp(argrest, "Ma") == 0) sender_host_address = argv[++i];
+
+      /* -oMaa: Set authenticator name */
+
+      else if (Ustrcmp(argrest, "Maa") == 0)
+        sender_host_authenticated = argv[++i];
+
+      /* -oMas: setting authenticated sender */
+
+      else if (Ustrcmp(argrest, "Mas") == 0) authenticated_sender = argv[++i];
+
+      /* -oMai: setting authenticated id */
+
+      else if (Ustrcmp(argrest, "Mai") == 0) authenticated_id = argv[++i];
+
+      /* -oMi: Set incoming interface address */
+
+      else if (Ustrcmp(argrest, "Mi") == 0) interface_address = argv[++i];
+
+      /* -oMm: Message reference */
+
+      else if (Ustrcmp(argrest, "Mm") == 0)
+        {
+        if (!mac_ismsgid(argv[i+1]))
+          {
+            fprintf(stderr,"-oMm must be a valid message ID\n");
+            exit(EXIT_FAILURE);
+          }
+        if (!trusted_config)
+          {
+            fprintf(stderr,"-oMm must be called by a trusted user/config\n");
+            exit(EXIT_FAILURE);
+          }
+          message_reference = argv[++i];
+        }
+
+      /* -oMr: Received protocol */
+
+      else if (Ustrcmp(argrest, "Mr") == 0) received_protocol = argv[++i];
+
+      /* -oMs: Set sender host name */
+
+      else if (Ustrcmp(argrest, "Ms") == 0) sender_host_name = argv[++i];
+
+      /* -oMt: Set sender ident */
+
+      else if (Ustrcmp(argrest, "Mt") == 0)
+        {
+        sender_ident_set = TRUE;
+        sender_ident = argv[++i];
+        }
+
+      /* Else a bad argument */
+
+      else
+        {
+        badarg = TRUE;
+        break;
+        }
+      }
+
+    /* -om: Me-too flag for aliases. Exim always does this. Some programs
+    seem to call this as -m (undocumented), so that is also accepted (see
+    above). */
+
+    else if (Ustrcmp(argrest, "m") == 0) {}
+
+    /* -oo: An ancient flag for old-style addresses which still seems to
+    crop up in some calls (see in SCO). */
+
+    else if (Ustrcmp(argrest, "o") == 0) {}
+
+    /* -oP <name>: set pid file path for daemon */
+
+    else if (Ustrcmp(argrest, "P") == 0)
+      override_pid_file_path = argv[++i];
+
+    /* -or <n>: set timeout for non-SMTP acceptance
+       -os <n>: set timeout for SMTP acceptance */
+
+    else if (*argrest == 'r' || *argrest == 's')
+      {
+      int *tp = (*argrest == 'r')?
+        &arg_receive_timeout : &arg_smtp_receive_timeout;
+      if (argrest[1] == 0)
+        {
+        if (i+1 < argc) *tp= readconf_readtime(argv[++i], 0, FALSE);
+        }
+      else *tp = readconf_readtime(argrest + 1, 0, FALSE);
+      if (*tp < 0)
+        {
+        fprintf(stderr, "exim: bad time value %s: abandoned\n", argv[i]);
+        exit(EXIT_FAILURE);
+        }
+      }
+
+    /* -oX <list>: Override local_interfaces and/or default daemon ports */
+
+    else if (Ustrcmp(argrest, "X") == 0)
+      override_local_interfaces = argv[++i];
+
+    /* Unknown -o argument */
+
+    else badarg = TRUE;
+    break;
+
+
+    /* -ps: force Perl startup; -pd force delayed Perl startup */
+
+    case 'p':
+    #ifdef EXIM_PERL
+    if (*argrest == 's' && argrest[1] == 0)
+      {
+      perl_start_option = 1;
+      break;
+      }
+    if (*argrest == 'd' && argrest[1] == 0)
+      {
+      perl_start_option = -1;
+      break;
+      }
+    #endif
+
+    /* -panythingelse is taken as the Sendmail-compatible argument -prval:sval,
+    which sets the host protocol and host name */
+
+    if (*argrest == 0)
+      {
+      if (i+1 < argc) argrest = argv[++i]; else
+        { badarg = TRUE; break; }
+      }
+
+    if (*argrest != 0)
+      {
+      uschar *hn = Ustrchr(argrest, ':');
+      if (hn == NULL)
+        {
+        received_protocol = argrest;
+        }
+      else
+        {
+        received_protocol = string_copyn(argrest, hn - argrest);
+        sender_host_name = hn + 1;
+        }
+      }
+    break;
+
+
+    case 'q':
+    receiving_message = FALSE;
+    if (queue_interval >= 0)
+      {
+      fprintf(stderr, "exim: -q specified more than once\n");
+      exit(EXIT_FAILURE);
+      }
+
+    /* -qq...: Do queue runs in a 2-stage manner */
+
+    if (*argrest == 'q')
+      {
+      queue_2stage = TRUE;
+      argrest++;
+      }
+
+    /* -qi...: Do only first (initial) deliveries */
+
+    if (*argrest == 'i')
+      {
+      queue_run_first_delivery = TRUE;
+      argrest++;
+      }
+
+    /* -qf...: Run the queue, forcing deliveries
+       -qff..: Ditto, forcing thawing as well */
+
+    if (*argrest == 'f')
+      {
+      queue_run_force = TRUE;
+      if (*(++argrest) == 'f')
+        {
+        deliver_force_thaw = TRUE;
+        argrest++;
+        }
+      }
+
+    /* -q[f][f]l...: Run the queue only on local deliveries */
+
+    if (*argrest == 'l')
+      {
+      queue_run_local = TRUE;
+      argrest++;
+      }
+
+    /* -q[f][f][l]: Run the queue, optionally forced, optionally local only,
+    optionally starting from a given message id. */
+
+    if (*argrest == 0 &&
+        (i + 1 >= argc || argv[i+1][0] == '-' || mac_ismsgid(argv[i+1])))
+      {
+      queue_interval = 0;
+      if (i+1 < argc && mac_ismsgid(argv[i+1]))
+        start_queue_run_id = argv[++i];
+      if (i+1 < argc && mac_ismsgid(argv[i+1]))
+        stop_queue_run_id = argv[++i];
+      }
+
+    /* -q[f][f][l]<n>: Run the queue at regular intervals, optionally forced,
+    optionally local only. */
+
+    else
+      {
+      if (*argrest != 0)
+        queue_interval = readconf_readtime(argrest, 0, FALSE);
+      else
+        queue_interval = readconf_readtime(argv[++i], 0, FALSE);
+      if (queue_interval <= 0)
+        {
+        fprintf(stderr, "exim: bad time value %s: abandoned\n", argv[i]);
+        exit(EXIT_FAILURE);
+        }
+      }
+    break;
+
+
+    case 'R':   /* Synonymous with -qR... */
+    receiving_message = FALSE;
+
+    /* -Rf:   As -R (below) but force all deliveries,
+       -Rff:  Ditto, but also thaw all frozen messages,
+       -Rr:   String is regex
+       -Rrf:  Regex and force
+       -Rrff: Regex and force and thaw
+
+    in all cases provided there are no further characters in this
+    argument. */
+
+    if (*argrest != 0)
+      {
+      int i;
+      for (i = 0; i < sizeof(rsopts)/sizeof(uschar *); i++)
+        {
+        if (Ustrcmp(argrest, rsopts[i]) == 0)
+          {
+          if (i != 2) queue_run_force = TRUE;
+          if (i >= 2) deliver_selectstring_regex = TRUE;
+          if (i == 1 || i == 4) deliver_force_thaw = TRUE;
+          argrest += Ustrlen(rsopts[i]);
+          }
+        }
+      }
+
+    /* -R: Set string to match in addresses for forced queue run to
+    pick out particular messages. */
+
+    if (*argrest == 0)
+      {
+      if (i+1 < argc) deliver_selectstring = argv[++i]; else
+        {
+        fprintf(stderr, "exim: string expected after -R\n");
+        exit(EXIT_FAILURE);
+        }
+      }
+    else deliver_selectstring = argrest;
+    break;
+
+
+    /* -r: an obsolete synonym for -f (see above) */
+
+
+    /* -S: Like -R but works on sender. */
+
+    case 'S':   /* Synonymous with -qS... */
+    receiving_message = FALSE;
+
+    /* -Sf:   As -S (below) but force all deliveries,
+       -Sff:  Ditto, but also thaw all frozen messages,
+       -Sr:   String is regex
+       -Srf:  Regex and force
+       -Srff: Regex and force and thaw
+
+    in all cases provided there are no further characters in this
+    argument. */
+
+    if (*argrest != 0)
+      {
+      int i;
+      for (i = 0; i < sizeof(rsopts)/sizeof(uschar *); i++)
+        {
+        if (Ustrcmp(argrest, rsopts[i]) == 0)
+          {
+          if (i != 2) queue_run_force = TRUE;
+          if (i >= 2) deliver_selectstring_sender_regex = TRUE;
+          if (i == 1 || i == 4) deliver_force_thaw = TRUE;
+          argrest += Ustrlen(rsopts[i]);
+          }
+        }
+      }
+
+    /* -S: Set string to match in addresses for forced queue run to
+    pick out particular messages. */
+
+    if (*argrest == 0)
+      {
+      if (i+1 < argc) deliver_selectstring_sender = argv[++i]; else
+        {
+        fprintf(stderr, "exim: string expected after -S\n");
+        exit(EXIT_FAILURE);
+        }
+      }
+    else deliver_selectstring_sender = argrest;
+    break;
+
+    /* -Tqt is an option that is exclusively for use by the testing suite.
+    It is not recognized in other circumstances. It allows for the setting up
+    of explicit "queue times" so that various warning/retry things can be
+    tested. Otherwise variability of clock ticks etc. cause problems. */
+
+    case 'T':
+    if (running_in_test_harness && Ustrcmp(argrest, "qt") == 0)
+      fudged_queue_times = argv[++i];
+    else badarg = TRUE;
+    break;
+
+
+    /* -t: Set flag to extract recipients from body of message. */
+
+    case 't':
+    if (*argrest == 0) extract_recipients = TRUE;
+
+    /* -ti: Set flag to extract recipients from body of message, and also
+    specify that dot does not end the message. */
+
+    else if (Ustrcmp(argrest, "i") == 0)
+      {
+      extract_recipients = TRUE;
+      dot_ends = FALSE;
+      }
+
+    /* -tls-on-connect: don't wait for STARTTLS (for old clients) */
+
+    #ifdef SUPPORT_TLS
+    else if (Ustrcmp(argrest, "ls-on-connect") == 0) tls_in.on_connect = TRUE;
+    #endif
+
+    else badarg = TRUE;
+    break;
+
+
+    /* -U: This means "initial user submission" in sendmail, apparently. The
+    doc claims that in future sendmail may refuse syntactically invalid
+    messages instead of fixing them. For the moment, we just ignore it. */
+
+    case 'U':
+    break;
+
+
+    /* -v: verify things - this is a very low-level debugging */
+
+    case 'v':
+    if (*argrest == 0)
+      {
+      debug_selector |= D_v;
+      debug_file = stderr;
+      }
+    else badarg = TRUE;
+    break;
+
+
+    /* -x: AIX uses this to indicate some fancy 8-bit character stuff:
+
+      The -x flag tells the sendmail command that mail from a local
+      mail program has National Language Support (NLS) extended characters
+      in the body of the mail item. The sendmail command can send mail with
+      extended NLS characters across networks that normally corrupts these
+      8-bit characters.
+
+    As Exim is 8-bit clean, it just ignores this flag. */
+
+    case 'x':
+    if (*argrest != 0) badarg = TRUE;
+    break;
+
+    /* -X: in sendmail: takes one parameter, logfile, and sends debugging
+    logs to that file.  We swallow the parameter and otherwise ignore it. */
+
+    case 'X':
+    if (*argrest == '\0')
+      {
+      if (++i >= argc)
+        {
+        fprintf(stderr, "exim: string expected after -X\n");
+        exit(EXIT_FAILURE);
+        }
+      }
+    break;
+
+    /* All other initial characters are errors */
+
+    default:
+    badarg = TRUE;
+    break;
+    }         /* End of high-level switch statement */
+
+  /* Failed to recognize the option, or syntax error */
+
+  if (badarg)
+    {
+    fprintf(stderr, "exim abandoned: unknown, malformed, or incomplete "
+      "option %s\n", arg);
+    exit(EXIT_FAILURE);
+    }
+  }
+
+
+/* If -R or -S have been specified without -q, assume a single queue run. */
+
+if ((deliver_selectstring != NULL || deliver_selectstring_sender != NULL) &&
+  queue_interval < 0) queue_interval = 0;
+
+
+END_ARG:
+/* If usage_wanted is set we call the usage function - which never returns */
+if (usage_wanted) exim_usage(called_as);
+
+/* Arguments have been processed. Check for incompatibilities. */
+if ((
+    (smtp_input || extract_recipients || recipients_arg < argc) &&
+    (daemon_listen || queue_interval >= 0 || bi_option ||
+      test_retry_arg >= 0 || test_rewrite_arg >= 0 ||
+      filter_test != FTEST_NONE || (msg_action_arg > 0 && !one_msg_action))
+    ) ||
+    (
+    msg_action_arg > 0 &&
+    (daemon_listen || queue_interval >= 0 || list_options ||
+      (checking && msg_action != MSG_LOAD) ||
+      bi_option || test_retry_arg >= 0 || test_rewrite_arg >= 0)
+    ) ||
+    (
+    (daemon_listen || queue_interval >= 0) &&
+    (sender_address != NULL || list_options || list_queue || checking ||
+      bi_option)
+    ) ||
+    (
+    daemon_listen && queue_interval == 0
+    ) ||
+    (
+    inetd_wait_mode && queue_interval >= 0
+    ) ||
+    (
+    list_options &&
+    (checking || smtp_input || extract_recipients ||
+      filter_test != FTEST_NONE || bi_option)
+    ) ||
+    (
+    verify_address_mode &&
+    (address_test_mode || smtp_input || extract_recipients ||
+      filter_test != FTEST_NONE || bi_option)
+    ) ||
+    (
+    address_test_mode && (smtp_input || extract_recipients ||
+      filter_test != FTEST_NONE || bi_option)
+    ) ||
+    (
+    smtp_input && (sender_address != NULL || filter_test != FTEST_NONE ||
+      extract_recipients)
+    ) ||
+    (
+    deliver_selectstring != NULL && queue_interval < 0
+    ) ||
+    (
+    msg_action == MSG_LOAD &&
+      (!expansion_test || expansion_test_message != NULL)
+    )
+   )
+  {
+  fprintf(stderr, "exim: incompatible command-line options or arguments\n");
+  exit(EXIT_FAILURE);
+  }
+
+/* If debugging is set up, set the file and the file descriptor to pass on to
+child processes. It should, of course, be 2 for stderr. Also, force the daemon
+to run in the foreground. */
+
+if (debug_selector != 0)
+  {
+  debug_file = stderr;
+  debug_fd = fileno(debug_file);
+  background_daemon = FALSE;
+  if (running_in_test_harness) millisleep(100);   /* lets caller finish */
+  if (debug_selector != D_v)    /* -v only doesn't show this */
+    {
+    debug_printf("Exim version %s uid=%ld gid=%ld pid=%d D=%x\n",
+      version_string, (long int)real_uid, (long int)real_gid, (int)getpid(),
+      debug_selector);
+    if (!version_printed)
+      show_whats_supported(stderr);
+    }
+  }
+
+/* When started with root privilege, ensure that the limits on the number of
+open files and the number of processes (where that is accessible) are
+sufficiently large, or are unset, in case Exim has been called from an
+environment where the limits are screwed down. Not all OS have the ability to
+change some of these limits. */
+
+if (unprivileged)
+  {
+  DEBUG(D_any) debug_print_ids(US"Exim has no root privilege:");
+  }
+else
+  {
+  struct rlimit rlp;
+
+  #ifdef RLIMIT_NOFILE
+  if (getrlimit(RLIMIT_NOFILE, &rlp) < 0)
+    {
+    log_write(0, LOG_MAIN|LOG_PANIC, "getrlimit(RLIMIT_NOFILE) failed: %s",
+      strerror(errno));
+    rlp.rlim_cur = rlp.rlim_max = 0;
+    }
+
+  /* I originally chose 1000 as a nice big number that was unlikely to
+  be exceeded. It turns out that some older OS have a fixed upper limit of
+  256. */
+
+  if (rlp.rlim_cur < 1000)
+    {
+    rlp.rlim_cur = rlp.rlim_max = 1000;
+    if (setrlimit(RLIMIT_NOFILE, &rlp) < 0)
+      {
+      rlp.rlim_cur = rlp.rlim_max = 256;
+      if (setrlimit(RLIMIT_NOFILE, &rlp) < 0)
+        log_write(0, LOG_MAIN|LOG_PANIC, "setrlimit(RLIMIT_NOFILE) failed: %s",
+          strerror(errno));
+      }
+    }
+  #endif
+
+  #ifdef RLIMIT_NPROC
+  if (getrlimit(RLIMIT_NPROC, &rlp) < 0)
+    {
+    log_write(0, LOG_MAIN|LOG_PANIC, "getrlimit(RLIMIT_NPROC) failed: %s",
+      strerror(errno));
+    rlp.rlim_cur = rlp.rlim_max = 0;
+    }
+
+  #ifdef RLIM_INFINITY
+  if (rlp.rlim_cur != RLIM_INFINITY && rlp.rlim_cur < 1000)
+    {
+    rlp.rlim_cur = rlp.rlim_max = RLIM_INFINITY;
+  #else
+  if (rlp.rlim_cur < 1000)
+    {
+    rlp.rlim_cur = rlp.rlim_max = 1000;
+  #endif
+    if (setrlimit(RLIMIT_NPROC, &rlp) < 0)
+      log_write(0, LOG_MAIN|LOG_PANIC, "setrlimit(RLIMIT_NPROC) failed: %s",
+        strerror(errno));
+    }
+  #endif
+  }
+
+/* Exim is normally entered as root (but some special configurations are
+possible that don't do this). However, it always spins off sub-processes that
+set their uid and gid as required for local delivery. We don't want to pass on
+any extra groups that root may belong to, so we want to get rid of them all at
+this point.
+
+We need to obey setgroups() at this stage, before possibly giving up root
+privilege for a changed configuration file, but later on we might need to
+check on the additional groups for the admin user privilege - can't do that
+till after reading the config, which might specify the exim gid. Therefore,
+save the group list here first. */
+
+group_count = getgroups(NGROUPS_MAX, group_list);
+if (group_count < 0)
+  {
+  fprintf(stderr, "exim: getgroups() failed: %s\n", strerror(errno));
+  exit(EXIT_FAILURE);
+  }
+
+/* There is a fundamental difference in some BSD systems in the matter of
+groups. FreeBSD and BSDI are known to be different; NetBSD and OpenBSD are
+known not to be different. On the "different" systems there is a single group
+list, and the first entry in it is the current group. On all other versions of
+Unix there is a supplementary group list, which is in *addition* to the current
+group. Consequently, to get rid of all extraneous groups on a "standard" system
+you pass over 0 groups to setgroups(), while on a "different" system you pass
+over a single group - the current group, which is always the first group in the
+list. Calling setgroups() with zero groups on a "different" system results in
+an error return. The following code should cope with both types of system.
+
+However, if this process isn't running as root, setgroups() can't be used
+since you have to be root to run it, even if throwing away groups. Not being
+root here happens only in some unusual configurations. We just ignore the
+error. */
+
+if (setgroups(0, NULL) != 0)
+  {
+  if (setgroups(1, group_list) != 0 && !unprivileged)
+    {
+    fprintf(stderr, "exim: setgroups() failed: %s\n", strerror(errno));
+    exit(EXIT_FAILURE);
+    }
+  }
+
+/* If the configuration file name has been altered by an argument on the
+command line (either a new file name or a macro definition) and the caller is
+not root, or if this is a filter testing run, remove any setuid privilege the
+program has and run as the underlying user.
+
+The exim user is locked out of this, which severely restricts the use of -C
+for some purposes.
+
+Otherwise, set the real ids to the effective values (should be root unless run
+from inetd, which it can either be root or the exim uid, if one is configured).
+
+There is a private mechanism for bypassing some of this, in order to make it
+possible to test lots of configurations automatically, without having either to
+recompile each time, or to patch in an actual configuration file name and other
+values (such as the path name). If running in the test harness, pretend that
+configuration file changes and macro definitions haven't happened. */
+
+if ((                                            /* EITHER */
+    (!trusted_config ||                          /* Config changed, or */
+     !macros_trusted()) &&                       /*  impermissible macros and */
+    real_uid != root_uid &&                      /* Not root, and */
+    !running_in_test_harness                     /* Not fudged */
+    ) ||                                         /*   OR   */
+    expansion_test                               /* expansion testing */
+    ||                                           /*   OR   */
+    filter_test != FTEST_NONE)                   /* Filter testing */
+  {
+  setgroups(group_count, group_list);
+  exim_setugid(real_uid, real_gid, FALSE,
+    US"-C, -D, -be or -bf forces real uid");
+  removed_privilege = TRUE;
+
+  /* In the normal case when Exim is called like this, stderr is available
+  and should be used for any logging information because attempts to write
+  to the log will usually fail. To arrange this, we unset really_exim. However,
+  if no stderr is available there is no point - we might as well have a go
+  at the log (if it fails, syslog will be written).
+
+  Note that if the invoker is Exim, the logs remain available. Messing with
+  this causes unlogged successful deliveries.  */
+
+  if ((log_stderr != NULL) && (real_uid != exim_uid))
+    really_exim = FALSE;
+  }
+
+/* Privilege is to be retained for the moment. It may be dropped later,
+depending on the job that this Exim process has been asked to do. For now, set
+the real uid to the effective so that subsequent re-execs of Exim are done by a
+privileged user. */
+
+else exim_setugid(geteuid(), getegid(), FALSE, US"forcing real = effective");
+
+/* If testing a filter, open the file(s) now, before wasting time doing other
+setups and reading the message. */
+
+if ((filter_test & FTEST_SYSTEM) != 0)
+  {
+  filter_sfd = Uopen(filter_test_sfile, O_RDONLY, 0);
+  if (filter_sfd < 0)
+    {
+    fprintf(stderr, "exim: failed to open %s: %s\n", filter_test_sfile,
+      strerror(errno));
+    return EXIT_FAILURE;
+    }
+  }
+
+if ((filter_test & FTEST_USER) != 0)
+  {
+  filter_ufd = Uopen(filter_test_ufile, O_RDONLY, 0);
+  if (filter_ufd < 0)
+    {
+    fprintf(stderr, "exim: failed to open %s: %s\n", filter_test_ufile,
+      strerror(errno));
+    return EXIT_FAILURE;
+    }
+  }
+
+/* Initialise lookup_list
+If debugging, already called above via version reporting.
+In either case, we initialise the list of available lookups while running
+as root.  All dynamically modules are loaded from a directory which is
+hard-coded into the binary and is code which, if not a module, would be
+part of Exim already.  Ability to modify the content of the directory
+is equivalent to the ability to modify a setuid binary!
+
+This needs to happen before we read the main configuration. */
+init_lookup_list();
+
+/* Read the main runtime configuration data; this gives up if there
+is a failure. It leaves the configuration file open so that the subsequent
+configuration data for delivery can be read if needed. */
+
+readconf_main();
+
+/* If an action on specific messages is requested, or if a daemon or queue
+runner is being started, we need to know if Exim was called by an admin user.
+This is the case if the real user is root or exim, or if the real group is
+exim, or if one of the supplementary groups is exim or a group listed in
+admin_groups. We don't fail all message actions immediately if not admin_user,
+since some actions can be performed by non-admin users. Instead, set admin_user
+for later interrogation. */
+
+if (real_uid == root_uid || real_uid == exim_uid || real_gid == exim_gid)
+  admin_user = TRUE;
+else
+  {
+  int i, j;
+  for (i = 0; i < group_count; i++)
+    {
+    if (group_list[i] == exim_gid) admin_user = TRUE;
+    else if (admin_groups != NULL)
+      {
+      for (j = 1; j <= (int)(admin_groups[0]); j++)
+        if (admin_groups[j] == group_list[i])
+          { admin_user = TRUE; break; }
+      }
+    if (admin_user) break;
+    }
+  }
+
+/* Another group of privileged users are the trusted users. These are root,
+exim, and any caller matching trusted_users or trusted_groups. Trusted callers
+are permitted to specify sender_addresses with -f on the command line, and
+other message parameters as well. */
+
+if (real_uid == root_uid || real_uid == exim_uid)
+  trusted_caller = TRUE;
+else
+  {
+  int i, j;
+
+  if (trusted_users != NULL)
+    {
+    for (i = 1; i <= (int)(trusted_users[0]); i++)
+      if (trusted_users[i] == real_uid)
+        { trusted_caller = TRUE; break; }
+    }
+
+  if (!trusted_caller && trusted_groups != NULL)
+    {
+    for (i = 1; i <= (int)(trusted_groups[0]); i++)
+      {
+      if (trusted_groups[i] == real_gid)
+        trusted_caller = TRUE;
+      else for (j = 0; j < group_count; j++)
+        {
+        if (trusted_groups[i] == group_list[j])
+          { trusted_caller = TRUE; break; }
+        }
+      if (trusted_caller) break;
+      }
+    }
+  }
+
+/* Handle the decoding of logging options. */
+
+decode_bits(&log_write_selector, &log_extra_selector, 0, 0,
+  log_selector_string, log_options, log_options_count, US"log", 0);
+
+DEBUG(D_any)
+  {
+  debug_printf("configuration file is %s\n", config_main_filename);
+  debug_printf("log selectors = %08x %08x\n", log_write_selector,
+    log_extra_selector);
+  }
+
+/* If domain literals are not allowed, check the sender address that was
+supplied with -f. Ditto for a stripped trailing dot. */
+
+if (sender_address != NULL)
+  {
+  if (sender_address[sender_address_domain] == '[' && !allow_domain_literals)
+    {
+    fprintf(stderr, "exim: bad -f address \"%s\": domain literals not "
+      "allowed\n", sender_address);
+    return EXIT_FAILURE;
+    }
+  if (f_end_dot && !strip_trailing_dot)
+    {
+    fprintf(stderr, "exim: bad -f address \"%s.\": domain is malformed "
+      "(trailing dot not allowed)\n", sender_address);
+    return EXIT_FAILURE;
+    }
+  }
+
+/* See if an admin user overrode our logging. */
+
+if (cmdline_syslog_name != NULL)
+  {
+  if (admin_user)
+    {
+    syslog_processname = cmdline_syslog_name;
+    log_file_path = string_copy(CUS"syslog");
+    }
+  else
+    {
+    /* not a panic, non-privileged users should not be able to spam paniclog */
+    fprintf(stderr,
+        "exim: you lack sufficient privilege to specify syslog process name\n");
+    return EXIT_FAILURE;
+    }
+  }
+
+/* Paranoia check of maximum lengths of certain strings. There is a check
+on the length of the log file path in log.c, which will come into effect
+if there are any calls to write the log earlier than this. However, if we
+get this far but the string is very long, it is better to stop now than to
+carry on and (e.g.) receive a message and then have to collapse. The call to
+log_write() from here will cause the ultimate panic collapse if the complete
+file name exceeds the buffer length. */
+
+if (Ustrlen(log_file_path) > 200)
+  log_write(0, LOG_MAIN|LOG_PANIC_DIE,
+    "log_file_path is longer than 200 chars: aborting");
+
+if (Ustrlen(pid_file_path) > 200)
+  log_write(0, LOG_MAIN|LOG_PANIC_DIE,
+    "pid_file_path is longer than 200 chars: aborting");
+
+if (Ustrlen(spool_directory) > 200)
+  log_write(0, LOG_MAIN|LOG_PANIC_DIE,
+    "spool_directory is longer than 200 chars: aborting");
+
+/* Length check on the process name given to syslog for its TAG field,
+which is only permitted to be 32 characters or less. See RFC 3164. */
+
+if (Ustrlen(syslog_processname) > 32)
+  log_write(0, LOG_MAIN|LOG_PANIC_DIE,
+    "syslog_processname is longer than 32 chars: aborting");
+
+/* In some operating systems, the environment variable TMPDIR controls where
+temporary files are created; Exim doesn't use these (apart from when delivering
+to MBX mailboxes), but called libraries such as DBM libraries may require them.
+If TMPDIR is found in the environment, reset it to the value defined in the
+TMPDIR macro, if this macro is defined. */
+
+#ifdef TMPDIR
+  {
+  uschar **p;
+  for (p = USS environ; *p != NULL; p++)
+    {
+    if (Ustrncmp(*p, "TMPDIR=", 7) == 0 &&
+        Ustrcmp(*p+7, TMPDIR) != 0)
+      {
+      uschar *newp = malloc(Ustrlen(TMPDIR) + 8);
+      sprintf(CS newp, "TMPDIR=%s", TMPDIR);
+      *p = newp;
+      DEBUG(D_any) debug_printf("reset TMPDIR=%s in environment\n", TMPDIR);
+      }
+    }
+  }
+#endif
+
+/* Timezone handling. If timezone_string is "utc", set a flag to cause all
+timestamps to be in UTC (gmtime() is used instead of localtime()). Otherwise,
+we may need to get rid of a bogus timezone setting. This can arise when Exim is
+called by a user who has set the TZ variable. This then affects the timestamps
+in log files and in Received: headers, and any created Date: header lines. The
+required timezone is settable in the configuration file, so nothing can be done
+about this earlier - but hopefully nothing will normally be logged earlier than
+this. We have to make a new environment if TZ is wrong, but don't bother if
+timestamps_utc is set, because then all times are in UTC anyway. */
+
+if (timezone_string != NULL && strcmpic(timezone_string, US"UTC") == 0)
+  {
+  timestamps_utc = TRUE;
+  }
+else
+  {
+  uschar *envtz = US getenv("TZ");
+  if ((envtz == NULL && timezone_string != NULL) ||
+      (envtz != NULL &&
+        (timezone_string == NULL ||
+         Ustrcmp(timezone_string, envtz) != 0)))
+    {
+    uschar **p = USS environ;
+    uschar **new;
+    uschar **newp;
+    int count = 0;
+    while (*p++ != NULL) count++;
+    if (envtz == NULL) count++;
+    newp = new = malloc(sizeof(uschar *) * (count + 1));
+    for (p = USS environ; *p != NULL; p++)
+      {
+      if (Ustrncmp(*p, "TZ=", 3) == 0) continue;
+      *newp++ = *p;
+      }
+    if (timezone_string != NULL)
+      {
+      *newp = malloc(Ustrlen(timezone_string) + 4);
+      sprintf(CS *newp++, "TZ=%s", timezone_string);
+      }
+    *newp = NULL;
+    environ = CSS new;
+    tzset();
+    DEBUG(D_any) debug_printf("Reset TZ to %s: time is %s\n", timezone_string,
+      tod_stamp(tod_log));
+    }
+  }
+
+/* Handle the case when we have removed the setuid privilege because of -C or
+-D. This means that the caller of Exim was not root.
+
+There is a problem if we were running as the Exim user. The sysadmin may
+expect this case to retain privilege because "the binary was called by the
+Exim user", but it hasn't, because either the -D option set macros, or the
+-C option set a non-trusted configuration file. There are two possibilities:
+
+  (1) If deliver_drop_privilege is set, Exim is not going to re-exec in order
+      to do message deliveries. Thus, the fact that it is running as a
+      non-privileged user is plausible, and might be wanted in some special
+      configurations. However, really_exim will have been set false when
+      privilege was dropped, to stop Exim trying to write to its normal log
+      files. Therefore, re-enable normal log processing, assuming the sysadmin
+      has set up the log directory correctly.
+
+  (2) If deliver_drop_privilege is not set, the configuration won't work as
+      apparently intended, and so we log a panic message. In order to retain
+      root for -C or -D, the caller must either be root or be invoking a
+      trusted configuration file (when deliver_drop_privilege is false). */
+
+if (removed_privilege && (!trusted_config || macros != NULL) &&
+    real_uid == exim_uid)
+  {
+  if (deliver_drop_privilege)
+    really_exim = TRUE; /* let logging work normally */
+  else
+    log_write(0, LOG_MAIN|LOG_PANIC,
+      "exim user lost privilege for using %s option",
+      trusted_config? "-D" : "-C");
+  }
+
+/* Start up Perl interpreter if Perl support is configured and there is a
+perl_startup option, and the configuration or the command line specifies
+initializing starting. Note that the global variables are actually called
+opt_perl_xxx to avoid clashing with perl's namespace (perl_*). */
+
+#ifdef EXIM_PERL
+if (perl_start_option != 0)
+  opt_perl_at_start = (perl_start_option > 0);
+if (opt_perl_at_start && opt_perl_startup != NULL)
+  {
+  uschar *errstr;
+  DEBUG(D_any) debug_printf("Starting Perl interpreter\n");
+  errstr = init_perl(opt_perl_startup);
+  if (errstr != NULL)
+    {
+    fprintf(stderr, "exim: error in perl_startup code: %s\n", errstr);
+    return EXIT_FAILURE;
+    }
+  opt_perl_started = TRUE;
+  }
+#endif /* EXIM_PERL */
+
+/* Log the arguments of the call if the configuration file said so. This is
+a debugging feature for finding out what arguments certain MUAs actually use.
+Don't attempt it if logging is disabled, or if listing variables or if
+verifying/testing addresses or expansions. */
+
+if (((debug_selector & D_any) != 0 || (log_extra_selector & LX_arguments) != 0)
+      && really_exim && !list_options && !checking)
+  {
+  int i;
+  uschar *p = big_buffer;
+  char * dummy;
+  Ustrcpy(p, "cwd= (failed)");
+  dummy = /* quieten compiler */ getcwd(CS p+4, big_buffer_size - 4);
+  while (*p) p++;
+  (void)string_format(p, big_buffer_size - (p - big_buffer), " %d args:", argc);
+  while (*p) p++;
+  for (i = 0; i < argc; i++)
+    {
+    int len = Ustrlen(argv[i]);
+    uschar *printing;
+    uschar *quote;
+    if (p + len + 8 >= big_buffer + big_buffer_size)
+      {
+      Ustrcpy(p, " ...");
+      log_write(0, LOG_MAIN, "%s", big_buffer);
+      Ustrcpy(big_buffer, "...");
+      p = big_buffer + 3;
+      }
+    printing = string_printing(argv[i]);
+    if (printing[0] == 0) quote = US"\""; else
+      {
+      uschar *pp = printing;
+      quote = US"";
+      while (*pp != 0) if (isspace(*pp++)) { quote = US"\""; break; }
+      }
+    sprintf(CS p, " %s%.*s%s", quote, (int)(big_buffer_size -
+      (p - big_buffer) - 4), printing, quote);
+    while (*p) p++;
+    }
+
+  if ((log_extra_selector & LX_arguments) != 0)
+    log_write(0, LOG_MAIN, "%s", big_buffer);
+  else
+    debug_printf("%s\n", big_buffer);
+  }
+
+/* Set the working directory to be the top-level spool directory. We don't rely
+on this in the code, which always uses fully qualified names, but it's useful
+for core dumps etc. Don't complain if it fails - the spool directory might not
+be generally accessible and calls with the -C option (and others) have lost
+privilege by now. Before the chdir, we try to ensure that the directory exists.
+*/
+
+if (Uchdir(spool_directory) != 0)
+  {
+  int dummy;
+  (void)directory_make(spool_directory, US"", SPOOL_DIRECTORY_MODE, FALSE);
+  dummy = /* quieten compiler */ Uchdir(spool_directory);
+  }
+
+/* Handle calls with the -bi option. This is a sendmail option to rebuild *the*
+alias file. Exim doesn't have such a concept, but this call is screwed into
+Sun's YP makefiles. Handle this by calling a configured script, as the real
+user who called Exim. The -oA option can be used to pass an argument to the
+script. */
+
+if (bi_option)
+  {
+  (void)fclose(config_file);
+  if (bi_command != NULL)
+    {
+    int i = 0;
+    uschar *argv[3];
+    argv[i++] = bi_command;
+    if (alias_arg != NULL) argv[i++] = alias_arg;
+    argv[i++] = NULL;
+
+    setgroups(group_count, group_list);
+    exim_setugid(real_uid, real_gid, FALSE, US"running bi_command");
+
+    DEBUG(D_exec) debug_printf("exec %.256s %.256s\n", argv[0],
+      (argv[1] == NULL)? US"" : argv[1]);
+
+    execv(CS argv[0], (char *const *)argv);
+    fprintf(stderr, "exim: exec failed: %s\n", strerror(errno));
+    exit(EXIT_FAILURE);
+    }
+  else
+    {
+    DEBUG(D_any) debug_printf("-bi used but bi_command not set; exiting\n");
+    exit(EXIT_SUCCESS);
+    }
+  }
+
+/* We moved the admin/trusted check to be immediately after reading the
+configuration file.  We leave these prints here to ensure that syslog setup,
+logfile setup, and so on has already happened. */
+
+if (trusted_caller) DEBUG(D_any) debug_printf("trusted user\n");
+if (admin_user) DEBUG(D_any) debug_printf("admin user\n");
+
+/* Only an admin user may start the daemon or force a queue run in the default
+configuration, but the queue run restriction can be relaxed. Only an admin
+user may request that a message be returned to its sender forthwith. Only an
+admin user may specify a debug level greater than D_v (because it might show
+passwords, etc. in lookup queries). Only an admin user may request a queue
+count. Only an admin user can use the test interface to scan for email
+(because Exim will be in the spool dir and able to look at mails). */
+
+if (!admin_user)
+  {
+  BOOL debugset = (debug_selector & ~D_v) != 0;
+  if (deliver_give_up || daemon_listen || malware_test_file ||
+     (count_queue && queue_list_requires_admin) ||
+     (list_queue && queue_list_requires_admin) ||
+     (queue_interval >= 0 && prod_requires_admin) ||
+     (debugset && !running_in_test_harness))
+    {
+    fprintf(stderr, "exim:%s permission denied\n", debugset? " debugging" : "");
+    exit(EXIT_FAILURE);
+    }
+  }
+
+/* If the real user is not root or the exim uid, the argument for passing
+in an open TCP/IP connection for another message is not permitted, nor is
+running with the -N option for any delivery action, unless this call to exim is
+one that supplied an input message, or we are using a patched exim for
+regression testing. */
+
+if (real_uid != root_uid && real_uid != exim_uid &&
+     (continue_hostname != NULL ||
+       (dont_deliver &&
+         (queue_interval >= 0 || daemon_listen || msg_action_arg > 0)
+       )) && !running_in_test_harness)
+  {
+  fprintf(stderr, "exim: Permission denied\n");
+  return EXIT_FAILURE;
+  }
+
+/* If the caller is not trusted, certain arguments are ignored when running for
+real, but are permitted when checking things (-be, -bv, -bt, -bh, -bf, -bF).
+Note that authority for performing certain actions on messages is tested in the
+queue_action() function. */
+
+if (!trusted_caller && !checking && filter_test == FTEST_NONE)
+  {
+  sender_host_name = sender_host_address = interface_address =
+    sender_ident = received_protocol = NULL;
+  sender_host_port = interface_port = 0;
+  sender_host_authenticated = authenticated_sender = authenticated_id = NULL;
+  }
+
+/* If a sender host address is set, extract the optional port number off the
+end of it and check its syntax. Do the same thing for the interface address.
+Exim exits if the syntax is bad. */
+
+else
+  {
+  if (sender_host_address != NULL)
+    sender_host_port = check_port(sender_host_address);
+  if (interface_address != NULL)
+    interface_port = check_port(interface_address);
+  }
+
+/* If the caller is trusted, then they can use -G to suppress_local_fixups. */
+if (flag_G)
+  {
+  if (trusted_caller)
+    {
+    suppress_local_fixups = suppress_local_fixups_default = TRUE;
+    DEBUG(D_acl) debug_printf("suppress_local_fixups forced on by -G\n");
+    }
+  else
+    {
+    fprintf(stderr, "exim: permission denied (-G requires a trusted user)\n");
+    return EXIT_FAILURE;
+    }
+  }
+
+/* If an SMTP message is being received check to see if the standard input is a
+TCP/IP socket. If it is, we assume that Exim was called from inetd if the
+caller is root or the Exim user, or if the port is a privileged one. Otherwise,
+barf. */
+
+if (smtp_input)
+  {
+  union sockaddr_46 inetd_sock;
+  EXIM_SOCKLEN_T size = sizeof(inetd_sock);
+  if (getpeername(0, (struct sockaddr *)(&inetd_sock), &size) == 0)
+    {
+    int family = ((struct sockaddr *)(&inetd_sock))->sa_family;
+    if (family == AF_INET || family == AF_INET6)
+      {
+      union sockaddr_46 interface_sock;
+      size = sizeof(interface_sock);
+
+      if (getsockname(0, (struct sockaddr *)(&interface_sock), &size) == 0)
+        interface_address = host_ntoa(-1, &interface_sock, NULL,
+          &interface_port);
+
+      if (host_is_tls_on_connect_port(interface_port)) tls_in.on_connect = TRUE;
+
+      if (real_uid == root_uid || real_uid == exim_uid || interface_port < 1024)
+        {
+        is_inetd = TRUE;
+        sender_host_address = host_ntoa(-1, (struct sockaddr *)(&inetd_sock),
+          NULL, &sender_host_port);
+        if (mua_wrapper) log_write(0, LOG_MAIN|LOG_PANIC_DIE, "Input from "
+          "inetd is not supported when mua_wrapper is set");
+        }
+      else
+        {
+        fprintf(stderr,
+          "exim: Permission denied (unprivileged user, unprivileged port)\n");
+        return EXIT_FAILURE;
+        }
+      }
+    }
+  }
+
+/* If the load average is going to be needed while receiving a message, get it
+now for those OS that require the first call to os_getloadavg() to be done as
+root. There will be further calls later for each message received. */
+
+#ifdef LOAD_AVG_NEEDS_ROOT
+if (receiving_message &&
+      (queue_only_load >= 0 ||
+        (is_inetd && smtp_load_reserve >= 0)
+      ))
+  {
+  load_average = OS_GETLOADAVG();
+  }
+#endif
+
+/* The queue_only configuration option can be overridden by -odx on the command
+line, except that if queue_only_override is false, queue_only cannot be unset
+from the command line. */
+
+if (queue_only_set && (queue_only_override || arg_queue_only))
+  queue_only = arg_queue_only;
+
+/* The receive_timeout and smtp_receive_timeout options can be overridden by
+-or and -os. */
+
+if (arg_receive_timeout >= 0) receive_timeout = arg_receive_timeout;
+if (arg_smtp_receive_timeout >= 0)
+  smtp_receive_timeout = arg_smtp_receive_timeout;
+
+/* If Exim was started with root privilege, unless we have already removed the
+root privilege above as a result of -C, -D, -be, -bf or -bF, remove it now
+except when starting the daemon or doing some kind of delivery or address
+testing (-bt). These are the only cases when root need to be retained. We run
+as exim for -bv and -bh. However, if deliver_drop_privilege is set, root is
+retained only for starting the daemon. We always do the initgroups() in this
+situation (controlled by the TRUE below), in order to be as close as possible
+to the state Exim usually runs in. */
+
+if (!unprivileged &&                      /* originally had root AND */
+    !removed_privilege &&                 /* still got root AND      */
+    !daemon_listen &&                     /* not starting the daemon */
+    queue_interval <= 0 &&                /* (either kind of daemon) */
+      (                                   /*    AND EITHER           */
+      deliver_drop_privilege ||           /* requested unprivileged  */
+        (                                 /*       OR                */
+        queue_interval < 0 &&             /* not running the queue   */
+        (msg_action_arg < 0 ||            /*       and               */
+          msg_action != MSG_DELIVER) &&   /* not delivering and      */
+        (!checking || !address_test_mode) /* not address checking    */
+        )
+      ))
+  {
+  exim_setugid(exim_uid, exim_gid, TRUE, US"privilege not needed");
+  }
+
+/* When we are retaining a privileged uid, we still change to the exim gid. */
+
+else
+  {
+  int rv;
+  rv = setgid(exim_gid);
+  /* Impact of failure is that some stuff might end up with an incorrect group.
+  We track this for failures from root, since any attempt to change privilege
+  by root should succeed and failures should be examined.  For non-root,
+  there's no security risk.  For me, it's { exim -bV } on a just-built binary,
+  no need to complain then. */
+  if (rv == -1)
+    {
+    if (!(unprivileged || removed_privilege))
+      {
+      fprintf(stderr,
+          "exim: changing group failed: %s\n", strerror(errno));
+      exit(EXIT_FAILURE);
+      }
+    else
+      DEBUG(D_any) debug_printf("changing group to %ld failed: %s\n",
+          (long int)exim_gid, strerror(errno));
+    }
+  }
+
+/* Handle a request to scan a file for malware */
+if (malware_test_file)
+  {
+#ifdef WITH_CONTENT_SCAN
+  int result;
+  set_process_info("scanning file for malware");
+  result = malware_in_file(malware_test_file);
+  if (result == FAIL)
+    {
+    printf("No malware found.\n");
+    exit(EXIT_SUCCESS);
+    }
+  if (result != OK)
+    {
+    printf("Malware lookup returned non-okay/fail: %d\n", result);
+    exit(EXIT_FAILURE);
+    }
+  if (malware_name)
+    printf("Malware found: %s\n", malware_name);
+  else
+    printf("Malware scan detected malware of unknown name.\n");
+#else
+  printf("Malware scanning not enabled at compile time.\n");
+#endif
+  exit(EXIT_FAILURE);
+  }
+
+/* Handle a request to list the delivery queue */
+
+if (list_queue)
+  {
+  set_process_info("listing the queue");
+  queue_list(list_queue_option, argv + recipients_arg, argc - recipients_arg);
+  exit(EXIT_SUCCESS);
+  }
+
+/* Handle a request to count the delivery queue */
+
+if (count_queue)
+  {
+  set_process_info("counting the queue");
+  queue_count();
+  exit(EXIT_SUCCESS);
+  }
+
+/* Handle actions on specific messages, except for the force delivery and
+message load actions, which are done below. Some actions take a whole list of
+message ids, which are known to continue up to the end of the arguments. Others
+take a single message id and then operate on the recipients list. */
+
+if (msg_action_arg > 0 && msg_action != MSG_DELIVER && msg_action != MSG_LOAD)
+  {
+  int yield = EXIT_SUCCESS;
+  set_process_info("acting on specified messages");
+
+  if (!one_msg_action)
+    {
+    for (i = msg_action_arg; i < argc; i++)
+      if (!queue_action(argv[i], msg_action, NULL, 0, 0))
+        yield = EXIT_FAILURE;
+    }
+
+  else if (!queue_action(argv[msg_action_arg], msg_action, argv, argc,
+    recipients_arg)) yield = EXIT_FAILURE;
+  exit(yield);
+  }
+
+/* We used to set up here to skip reading the ACL section, on
+ (msg_action_arg > 0 || (queue_interval == 0 && !daemon_listen)
+Now, since the intro of the ${acl } expansion, ACL definitions may be
+needed in transports so we lost the optimisation. */
+
+readconf_rest();
+
+/* The configuration data will have been read into POOL_PERM because we won't
+ever want to reset back past it. Change the current pool to POOL_MAIN. In fact,
+this is just a bit of pedantic tidiness. It wouldn't really matter if the
+configuration were read into POOL_MAIN, because we don't do any resets till
+later on. However, it seems right, and it does ensure that both pools get used.
+*/
+
+store_pool = POOL_MAIN;
+
+/* Handle the -brt option. This is for checking out retry configurations.
+The next three arguments are a domain name or a complete address, and
+optionally two error numbers. All it does is to call the function that
+scans the retry configuration data. */
+
+if (test_retry_arg >= 0)
+  {
+  retry_config *yield;
+  int basic_errno = 0;
+  int more_errno = 0;
+  uschar *s1, *s2;
+
+  if (test_retry_arg >= argc)
+    {
+    printf("-brt needs a domain or address argument\n");
+    exim_exit(EXIT_FAILURE);
+    }
+  s1 = argv[test_retry_arg++];
+  s2 = NULL;
+
+  /* If the first argument contains no @ and no . it might be a local user
+  or it might be a single-component name. Treat as a domain. */
+
+  if (Ustrchr(s1, '@') == NULL && Ustrchr(s1, '.') == NULL)
+    {
+    printf("Warning: \"%s\" contains no '@' and no '.' characters. It is "
+      "being \ntreated as a one-component domain, not as a local part.\n\n",
+      s1);
+    }
+
+  /* There may be an optional second domain arg. */
+
+  if (test_retry_arg < argc && Ustrchr(argv[test_retry_arg], '.') != NULL)
+    s2 = argv[test_retry_arg++];
+
+  /* The final arg is an error name */
+
+  if (test_retry_arg < argc)
+    {
+    uschar *ss = argv[test_retry_arg];
+    uschar *error =
+      readconf_retry_error(ss, ss + Ustrlen(ss), &basic_errno, &more_errno);
+    if (error != NULL)
+      {
+      printf("%s\n", CS error);
+      return EXIT_FAILURE;
+      }
+
+    /* For the {MAIL,RCPT,DATA}_4xx errors, a value of 255 means "any", and a
+    code > 100 as an error is for matching codes to the decade. Turn them into
+    a real error code, off the decade. */
+
+    if (basic_errno == ERRNO_MAIL4XX ||
+        basic_errno == ERRNO_RCPT4XX ||
+        basic_errno == ERRNO_DATA4XX)
+      {
+      int code = (more_errno >> 8) & 255;
+      if (code == 255)
+        more_errno = (more_errno & 0xffff00ff) | (21 << 8);
+      else if (code > 100)
+        more_errno = (more_errno & 0xffff00ff) | ((code - 96) << 8);
+      }
+    }
+
+  yield = retry_find_config(s1, s2, basic_errno, more_errno);
+  if (yield == NULL) printf("No retry information found\n"); else
+    {
+    retry_rule *r;
+    more_errno = yield->more_errno;
+    printf("Retry rule: %s  ", yield->pattern);
+
+    if (yield->basic_errno == ERRNO_EXIMQUOTA)
+      {
+      printf("quota%s%s  ",
+        (more_errno > 0)? "_" : "",
+        (more_errno > 0)? readconf_printtime(more_errno) : US"");
+      }
+    else if (yield->basic_errno == ECONNREFUSED)
+      {
+      printf("refused%s%s  ",
+        (more_errno > 0)? "_" : "",
+        (more_errno == 'M')? "MX" :
+        (more_errno == 'A')? "A" : "");
+      }
+    else if (yield->basic_errno == ETIMEDOUT)
+      {
+      printf("timeout");
+      if ((more_errno & RTEF_CTOUT) != 0) printf("_connect");
+      more_errno &= 255;
+      if (more_errno != 0) printf("_%s",
+        (more_errno == 'M')? "MX" : "A");
+      printf("  ");
+      }
+    else if (yield->basic_errno == ERRNO_AUTHFAIL)
+      printf("auth_failed  ");
+    else printf("*  ");
+
+    for (r = yield->rules; r != NULL; r = r->next)
+      {
+      printf("%c,%s", r->rule, readconf_printtime(r->timeout)); /* Do not */
+      printf(",%s", readconf_printtime(r->p1));                 /* amalgamate */
+      if (r->rule == 'G')
+        {
+        int x = r->p2;
+        int f = x % 1000;
+        int d = 100;
+        printf(",%d.", x/1000);
+        do
+          {
+          printf("%d", f/d);
+          f %= d;
+          d /= 10;
+          }
+        while (f != 0);
+        }
+      printf("; ");
+      }
+
+    printf("\n");
+    }
+  exim_exit(EXIT_SUCCESS);
+  }
+
+/* Handle a request to list one or more configuration options */
+/* If -n was set, we suppress some information */
+
+if (list_options)
+  {
+  set_process_info("listing variables");
+  if (recipients_arg >= argc) readconf_print(US"all", NULL, flag_n);
+    else for (i = recipients_arg; i < argc; i++)
+      {
+      if (i < argc - 1 &&
+          (Ustrcmp(argv[i], "router") == 0 ||
+           Ustrcmp(argv[i], "transport") == 0 ||
+           Ustrcmp(argv[i], "authenticator") == 0 ||
+           Ustrcmp(argv[i], "macro") == 0))
+        {
+        readconf_print(argv[i+1], argv[i], flag_n);
+        i++;
+        }
+      else readconf_print(argv[i], NULL, flag_n);
+      }
+  exim_exit(EXIT_SUCCESS);
+  }
+
+
+/* Handle a request to deliver one or more messages that are already on the
+queue. Values of msg_action other than MSG_DELIVER and MSG_LOAD are dealt with
+above. MSG_LOAD is handled with -be (which is the only time it applies) below.
+
+Delivery of specific messages is typically used for a small number when
+prodding by hand (when the option forced_delivery will be set) or when
+re-execing to regain root privilege. Each message delivery must happen in a
+separate process, so we fork a process for each one, and run them sequentially
+so that debugging output doesn't get intertwined, and to avoid spawning too
+many processes if a long list is given. However, don't fork for the last one;
+this saves a process in the common case when Exim is called to deliver just one
+message. */
+
+if (msg_action_arg > 0 && msg_action != MSG_LOAD)
+  {
+  if (prod_requires_admin && !admin_user)
+    {
+    fprintf(stderr, "exim: Permission denied\n");
+    exim_exit(EXIT_FAILURE);
+    }
+  set_process_info("delivering specified messages");
+  if (deliver_give_up) forced_delivery = deliver_force_thaw = TRUE;
+  for (i = msg_action_arg; i < argc; i++)
+    {
+    int status;
+    pid_t pid;
+    if (i == argc - 1)
+      (void)deliver_message(argv[i], forced_delivery, deliver_give_up);
+    else if ((pid = fork()) == 0)
+      {
+      (void)deliver_message(argv[i], forced_delivery, deliver_give_up);
+      _exit(EXIT_SUCCESS);
+      }
+    else if (pid < 0)
+      {
+      fprintf(stderr, "failed to fork delivery process for %s: %s\n", argv[i],
+        strerror(errno));
+      exim_exit(EXIT_FAILURE);
+      }
+    else wait(&status);
+    }
+  exim_exit(EXIT_SUCCESS);
+  }
+
+
+/* If only a single queue run is requested, without SMTP listening, we can just
+turn into a queue runner, with an optional starting message id. */
+
+if (queue_interval == 0 && !daemon_listen)
+  {
+  DEBUG(D_queue_run) debug_printf("Single queue run%s%s%s%s\n",
+    (start_queue_run_id == NULL)? US"" : US" starting at ",
+    (start_queue_run_id == NULL)? US"" : start_queue_run_id,
+    (stop_queue_run_id == NULL)?  US"" : US" stopping at ",
+    (stop_queue_run_id == NULL)?  US"" : stop_queue_run_id);
+  set_process_info("running the queue (single queue run)");
+  queue_run(start_queue_run_id, stop_queue_run_id, FALSE);
+  exim_exit(EXIT_SUCCESS);
+  }
+
+
+/* Find the login name of the real user running this process. This is always
+needed when receiving a message, because it is written into the spool file. It
+may also be used to construct a from: or a sender: header, and in this case we
+need the user's full name as well, so save a copy of it, checked for RFC822
+syntax and munged if necessary, if it hasn't previously been set by the -F
+argument. We may try to get the passwd entry more than once, in case NIS or
+other delays are in evidence. Save the home directory for use in filter testing
+(only). */
+
+for (i = 0;;)
+  {
+  if ((pw = getpwuid(real_uid)) != NULL)
+    {
+    originator_login = string_copy(US pw->pw_name);
+    originator_home = string_copy(US pw->pw_dir);
+
+    /* If user name has not been set by -F, set it from the passwd entry
+    unless -f has been used to set the sender address by a trusted user. */
+
+    if (originator_name == NULL)
+      {
+      if (sender_address == NULL ||
+           (!trusted_caller && filter_test == FTEST_NONE))
+        {
+        uschar *name = US pw->pw_gecos;
+        uschar *amp = Ustrchr(name, '&');
+        uschar buffer[256];
+
+        /* Most Unix specify that a '&' character in the gecos field is
+        replaced by a copy of the login name, and some even specify that
+        the first character should be upper cased, so that's what we do. */
+
+        if (amp != NULL)
+          {
+          int loffset;
+          string_format(buffer, sizeof(buffer), "%.*s%n%s%s",
+            amp - name, name, &loffset, originator_login, amp + 1);
+          buffer[loffset] = toupper(buffer[loffset]);
+          name = buffer;
+          }
+
+        /* If a pattern for matching the gecos field was supplied, apply
+        it and then expand the name string. */
+
+        if (gecos_pattern != NULL && gecos_name != NULL)
+          {
+          const pcre *re;
+          re = regex_must_compile(gecos_pattern, FALSE, TRUE); /* Use malloc */
+
+          if (regex_match_and_setup(re, name, 0, -1))
+            {
+            uschar *new_name = expand_string(gecos_name);
+            expand_nmax = -1;
+            if (new_name != NULL)
+              {
+              DEBUG(D_receive) debug_printf("user name \"%s\" extracted from "
+                "gecos field \"%s\"\n", new_name, name);
+              name = new_name;
+              }
+            else DEBUG(D_receive) debug_printf("failed to expand gecos_name string "
+              "\"%s\": %s\n", gecos_name, expand_string_message);
+            }
+          else DEBUG(D_receive) debug_printf("gecos_pattern \"%s\" did not match "
+            "gecos field \"%s\"\n", gecos_pattern, name);
+          store_free((void *)re);
+          }
+        originator_name = string_copy(name);
+        }
+
+      /* A trusted caller has used -f but not -F */
+
+      else originator_name = US"";
+      }
+
+    /* Break the retry loop */
+
+    break;
+    }
+
+  if (++i > finduser_retries) break;
+  sleep(1);
+  }
+
+/* If we cannot get a user login, log the incident and give up, unless the
+configuration specifies something to use. When running in the test harness,
+any setting of unknown_login overrides the actual name. */
+
+if (originator_login == NULL || running_in_test_harness)
+  {
+  if (unknown_login != NULL)
+    {
+    originator_login = expand_string(unknown_login);
+    if (originator_name == NULL && unknown_username != NULL)
+      originator_name = expand_string(unknown_username);
+    if (originator_name == NULL) originator_name = US"";
+    }
+  if (originator_login == NULL)
+    log_write(0, LOG_MAIN|LOG_PANIC_DIE, "Failed to get user name for uid %d",
+      (int)real_uid);
+  }
+
+/* Ensure that the user name is in a suitable form for use as a "phrase" in an
+RFC822 address.*/
+
+originator_name = string_copy(parse_fix_phrase(originator_name,
+  Ustrlen(originator_name), big_buffer, big_buffer_size));
+
+/* If a message is created by this call of Exim, the uid/gid of its originator
+are those of the caller. These values are overridden if an existing message is
+read in from the spool. */
+
+originator_uid = real_uid;
+originator_gid = real_gid;
+
+DEBUG(D_receive) debug_printf("originator: uid=%d gid=%d login=%s name=%s\n",
+  (int)originator_uid, (int)originator_gid, originator_login, originator_name);
+
+/* Run in daemon and/or queue-running mode. The function daemon_go() never
+returns. We leave this till here so that the originator_ fields are available
+for incoming messages via the daemon. The daemon cannot be run in mua_wrapper
+mode. */
+
+if (daemon_listen || inetd_wait_mode || queue_interval > 0)
+  {
+  if (mua_wrapper)
+    {
+    fprintf(stderr, "Daemon cannot be run when mua_wrapper is set\n");
+    log_write(0, LOG_MAIN|LOG_PANIC_DIE, "Daemon cannot be run when "
+      "mua_wrapper is set");
+    }
+  daemon_go();
+  }
+
+/* If the sender ident has not been set (by a trusted caller) set it to
+the caller. This will get overwritten below for an inetd call. If a trusted
+caller has set it empty, unset it. */
+
+if (sender_ident == NULL) sender_ident = originator_login;
+  else if (sender_ident[0] == 0) sender_ident = NULL;
+
+/* Handle the -brw option, which is for checking out rewriting rules. Cause log
+writes (on errors) to go to stderr instead. Can't do this earlier, as want the
+originator_* variables set. */
+
+if (test_rewrite_arg >= 0)
+  {
+  really_exim = FALSE;
+  if (test_rewrite_arg >= argc)
+    {
+    printf("-brw needs an address argument\n");
+    exim_exit(EXIT_FAILURE);
+    }
+  rewrite_test(argv[test_rewrite_arg]);
+  exim_exit(EXIT_SUCCESS);
+  }
+
+/* A locally-supplied message is considered to be coming from a local user
+unless a trusted caller supplies a sender address with -f, or is passing in the
+message via SMTP (inetd invocation or otherwise). */
+
+if ((sender_address == NULL && !smtp_input) ||
+    (!trusted_caller && filter_test == FTEST_NONE))
+  {
+  sender_local = TRUE;
+
+  /* A trusted caller can supply authenticated_sender and authenticated_id
+  via -oMas and -oMai and if so, they will already be set. Otherwise, force
+  defaults except when host checking. */
+
+  if (authenticated_sender == NULL && !host_checking)
+    authenticated_sender = string_sprintf("%s@%s", originator_login,
+      qualify_domain_sender);
+  if (authenticated_id == NULL && !host_checking)
+    authenticated_id = originator_login;
+  }
+
+/* Trusted callers are always permitted to specify the sender address.
+Untrusted callers may specify it if it matches untrusted_set_sender, or if what
+is specified is the empty address. However, if a trusted caller does not
+specify a sender address for SMTP input, we leave sender_address unset. This
+causes the MAIL commands to be honoured. */
+
+if ((!smtp_input && sender_address == NULL) ||
+    !receive_check_set_sender(sender_address))
+  {
+  /* Either the caller is not permitted to set a general sender, or this is
+  non-SMTP input and the trusted caller has not set a sender. If there is no
+  sender, or if a sender other than <> is set, override with the originator's
+  login (which will get qualified below), except when checking things. */
+
+  if (sender_address == NULL             /* No sender_address set */
+       ||                                /*         OR            */
+       (sender_address[0] != 0 &&        /* Non-empty sender address, AND */
+       !checking &&                      /* Not running tests, AND */
+       filter_test == FTEST_NONE))       /* Not testing a filter */
+    {
+    sender_address = originator_login;
+    sender_address_forced = FALSE;
+    sender_address_domain = 0;
+    }
+  }
+
+/* Remember whether an untrusted caller set the sender address */
+
+sender_set_untrusted = sender_address != originator_login && !trusted_caller;
+
+/* Ensure that the sender address is fully qualified unless it is the empty
+address, which indicates an error message, or doesn't exist (root caller, smtp
+interface, no -f argument). */
+
+if (sender_address != NULL && sender_address[0] != 0 &&
+    sender_address_domain == 0)
+  sender_address = string_sprintf("%s@%s", local_part_quote(sender_address),
+    qualify_domain_sender);
+
+DEBUG(D_receive) debug_printf("sender address = %s\n", sender_address);
+
+/* Handle a request to verify a list of addresses, or test them for delivery.
+This must follow the setting of the sender address, since routers can be
+predicated upon the sender. If no arguments are given, read addresses from
+stdin. Set debug_level to at least D_v to get full output for address testing.
+*/
+
+if (verify_address_mode || address_test_mode)
+  {
+  int exit_value = 0;
+  int flags = vopt_qualify;
+
+  if (verify_address_mode)
+    {
+    if (!verify_as_sender) flags |= vopt_is_recipient;
+    DEBUG(D_verify) debug_print_ids(US"Verifying:");
+    }
+
+  else
+    {
+    flags |= vopt_is_recipient;
+    debug_selector |= D_v;
+    debug_file = stderr;
+    debug_fd = fileno(debug_file);
+    DEBUG(D_verify) debug_print_ids(US"Address testing:");
+    }
+
+  if (recipients_arg < argc)
+    {
+    while (recipients_arg < argc)
+      {
+      uschar *s = argv[recipients_arg++];
+      while (*s != 0)
+        {
+        BOOL finished = FALSE;
+        uschar *ss = parse_find_address_end(s, FALSE);
+        if (*ss == ',') *ss = 0; else finished = TRUE;
+        test_address(s, flags, &exit_value);
+        s = ss;
+        if (!finished)
+          while (*(++s) != 0 && (*s == ',' || isspace(*s)));
+        }
+      }
+    }
+
+  else for (;;)
+    {
+    uschar *s = get_stdinput(NULL, NULL);
+    if (s == NULL) break;
+    test_address(s, flags, &exit_value);
+    }
+
+  route_tidyup();
+  exim_exit(exit_value);
+  }
+
+/* Handle expansion checking. Either expand items on the command line, or read
+from stdin if there aren't any. If -Mset was specified, load the message so
+that its variables can be used, but restrict this facility to admin users.
+Otherwise, if -bem was used, read a message from stdin. */
+
+if (expansion_test)
+  {
+  if (msg_action_arg > 0 && msg_action == MSG_LOAD)
+    {
+    uschar spoolname[256];  /* Not big_buffer; used in spool_read_header() */
+    if (!admin_user)
+      {
+      fprintf(stderr, "exim: permission denied\n");
+      exit(EXIT_FAILURE);
+      }
+    message_id = argv[msg_action_arg];
+    (void)string_format(spoolname, sizeof(spoolname), "%s-H", message_id);
+    if (!spool_open_datafile(message_id))
+      printf ("Failed to load message datafile %s\n", message_id);
+    if (spool_read_header(spoolname, TRUE, FALSE) != spool_read_OK)
+      printf ("Failed to load message %s\n", message_id);
+    }
+
+  /* Read a test message from a file. We fudge it up to be on stdin, saving
+  stdin itself for later reading of expansion strings. */
+
+  else if (expansion_test_message != NULL)
+    {
+    int save_stdin = dup(0);
+    int fd = Uopen(expansion_test_message, O_RDONLY, 0);
+    if (fd < 0)
+      {
+      fprintf(stderr, "exim: failed to open %s: %s\n", expansion_test_message,
+        strerror(errno));
+      return EXIT_FAILURE;
+      }
+    (void) dup2(fd, 0);
+    filter_test = FTEST_USER;      /* Fudge to make it look like filter test */
+    message_ended = END_NOTENDED;
+    read_message_body(receive_msg(extract_recipients));
+    message_linecount += body_linecount;
+    (void)dup2(save_stdin, 0);
+    (void)close(save_stdin);
+    clearerr(stdin);               /* Required by Darwin */
+    }
+
+  /* Allow $recipients for this testing */
+
+  enable_dollar_recipients = TRUE;
+
+  /* Expand command line items */
+
+  if (recipients_arg < argc)
+    {
+    while (recipients_arg < argc)
+      {
+      uschar *s = argv[recipients_arg++];
+      uschar *ss = expand_string(s);
+      if (ss == NULL) printf ("Failed: %s\n", expand_string_message);
+      else printf("%s\n", CS ss);
+      }
+    }
+
+  /* Read stdin */
+
+  else
+    {
+    char *(*fn_readline)(const char *) = NULL;
+    void (*fn_addhist)(const char *) = NULL;
+
+    #ifdef USE_READLINE
+    void *dlhandle = set_readline(&fn_readline, &fn_addhist);
+    #endif
+
+    for (;;)
+      {
+      uschar *ss;
+      uschar *source = get_stdinput(fn_readline, fn_addhist);
+      if (source == NULL) break;
+      ss = expand_string(source);
+      if (ss == NULL)
+        printf ("Failed: %s\n", expand_string_message);
+      else printf("%s\n", CS ss);
+      }
+
+    #ifdef USE_READLINE
+    if (dlhandle != NULL) dlclose(dlhandle);
+    #endif
+    }
+
+  /* The data file will be open after -Mset */
+
+  if (deliver_datafile >= 0)
+    {
+    (void)close(deliver_datafile);
+    deliver_datafile = -1;
+    }
+
+  exim_exit(EXIT_SUCCESS);
+  }
+
+
+/* The active host name is normally the primary host name, but it can be varied
+for hosts that want to play several parts at once. We need to ensure that it is
+set for host checking, and for receiving messages. */
+
+smtp_active_hostname = primary_hostname;
+if (raw_active_hostname != NULL)
+  {
+  uschar *nah = expand_string(raw_active_hostname);
+  if (nah == NULL)
+    {
+    if (!expand_string_forcedfail)
+      log_write(0, LOG_MAIN|LOG_PANIC_DIE, "failed to expand \"%s\" "
+        "(smtp_active_hostname): %s", raw_active_hostname,
+        expand_string_message);
+    }
+  else if (nah[0] != 0) smtp_active_hostname = nah;
+  }
+
+/* Handle host checking: this facility mocks up an incoming SMTP call from a
+given IP address so that the blocking and relay configuration can be tested.
+Unless a sender_ident was set by -oMt, we discard it (the default is the
+caller's login name). An RFC 1413 call is made only if we are running in the
+test harness and an incoming interface and both ports are specified, because
+there is no TCP/IP call to find the ident for. */
+
+if (host_checking)
+  {
+  int x[4];
+  int size;
+
+  if (!sender_ident_set)
+    {
+    sender_ident = NULL;
+    if (running_in_test_harness && sender_host_port != 0 &&
+        interface_address != NULL && interface_port != 0)
+      verify_get_ident(1413);
+    }
+
+  /* In case the given address is a non-canonical IPv6 address, canonicize
+  it. The code works for both IPv4 and IPv6, as it happens. */
+
+  size = host_aton(sender_host_address, x);
+  sender_host_address = store_get(48);  /* large enough for full IPv6 */
+  (void)host_nmtoa(size, x, -1, sender_host_address, ':');
+
+  /* Now set up for testing */
+
+  host_build_sender_fullhost();
+  smtp_input = TRUE;
+  smtp_in = stdin;
+  smtp_out = stdout;
+  sender_local = FALSE;
+  sender_host_notsocket = TRUE;
+  debug_file = stderr;
+  debug_fd = fileno(debug_file);
+  fprintf(stdout, "\n**** SMTP testing session as if from host %s\n"
+    "**** but without any ident (RFC 1413) callback.\n"
+    "**** This is not for real!\n\n",
+      sender_host_address);
+
+  if (verify_check_host(&hosts_connection_nolog) == OK)
+    log_write_selector &= ~L_smtp_connection;
+  log_write(L_smtp_connection, LOG_MAIN, "%s", smtp_get_connection_info());
+
+  /* NOTE: We do *not* call smtp_log_no_mail() if smtp_start_session() fails,
+  because a log line has already been written for all its failure exists
+  (usually "connection refused: <reason>") and writing another one is
+  unnecessary clutter. */
+
+  if (smtp_start_session())
+    {
+    reset_point = store_get(0);
+    for (;;)
+      {
+      store_reset(reset_point);
+      if (smtp_setup_msg() <= 0) break;
+      if (!receive_msg(FALSE)) break;
+      }
+    smtp_log_no_mail();
+    }
+  exim_exit(EXIT_SUCCESS);
+  }
+
+
+/* Arrange for message reception if recipients or SMTP were specified;
+otherwise complain unless a version print (-bV) happened or this is a filter
+verification test or info dump.
+In the former case, show the configuration file name. */
+
+if (recipients_arg >= argc && !extract_recipients && !smtp_input)
+  {
+  if (version_printed)
+    {
+    printf("Configuration file is %s\n", config_main_filename);
+    return EXIT_SUCCESS;
+    }
+
+  if (info_flag != CMDINFO_NONE)
+    {
+    show_exim_information(info_flag, info_stdout ? stdout : stderr);
+    return info_stdout ? EXIT_SUCCESS : EXIT_FAILURE;
+    }
+
+  if (filter_test == FTEST_NONE)
+    exim_usage(called_as);
+  }
+
+
+/* If mua_wrapper is set, Exim is being used to turn an MUA that submits on the
+standard input into an MUA that submits to a smarthost over TCP/IP. We know
+that we are not called from inetd, because that is rejected above. The
+following configuration settings are forced here:
+
+  (1) Synchronous delivery (-odi)
+  (2) Errors to stderr (-oep == -oeq)
+  (3) No parallel remote delivery
+  (4) Unprivileged delivery
+
+We don't force overall queueing options because there are several of them;
+instead, queueing is avoided below when mua_wrapper is set. However, we do need
+to override any SMTP queueing. */
+
+if (mua_wrapper)
+  {
+  synchronous_delivery = TRUE;
+  arg_error_handling = ERRORS_STDERR;
+  remote_max_parallel = 1;
+  deliver_drop_privilege = TRUE;
+  queue_smtp = FALSE;
+  queue_smtp_domains = NULL;
+  }
+
+
+/* Prepare to accept one or more new messages on the standard input. When a
+message has been read, its id is returned in message_id[]. If doing immediate
+delivery, we fork a delivery process for each received message, except for the
+last one, where we can save a process switch.
+
+It is only in non-smtp mode that error_handling is allowed to be changed from
+its default of ERRORS_SENDER by argument. (Idle thought: are any of the
+sendmail error modes other than -oem ever actually used? Later: yes.) */
+
+if (!smtp_input) error_handling = arg_error_handling;
+
+/* If this is an inetd call, ensure that stderr is closed to prevent panic
+logging being sent down the socket and make an identd call to get the
+sender_ident. */
+
+else if (is_inetd)
+  {
+  (void)fclose(stderr);
+  exim_nullstd();                       /* Re-open to /dev/null */
+  verify_get_ident(IDENT_PORT);
+  host_build_sender_fullhost();
+  set_process_info("handling incoming connection from %s via inetd",
+    sender_fullhost);
+  }
+
+/* If the sender host address has been set, build sender_fullhost if it hasn't
+already been done (which it will have been for inetd). This caters for the
+case when it is forced by -oMa. However, we must flag that it isn't a socket,
+so that the test for IP options is skipped for -bs input. */
+
+if (sender_host_address != NULL && sender_fullhost == NULL)
+  {
+  host_build_sender_fullhost();
+  set_process_info("handling incoming connection from %s via -oMa",
+    sender_fullhost);
+  sender_host_notsocket = TRUE;
+  }
+
+/* Otherwise, set the sender host as unknown except for inetd calls. This
+prevents host checking in the case of -bs not from inetd and also for -bS. */
+
+else if (!is_inetd) sender_host_unknown = TRUE;
+
+/* If stdout does not exist, then dup stdin to stdout. This can happen
+if exim is started from inetd. In this case fd 0 will be set to the socket,
+but fd 1 will not be set. This also happens for passed SMTP channels. */
+
+if (fstat(1, &statbuf) < 0) (void)dup2(0, 1);
+
+/* Set up the incoming protocol name and the state of the program. Root is
+allowed to force received protocol via the -oMr option above. If we have come
+via inetd, the process info has already been set up. We don't set
+received_protocol here for smtp input, as it varies according to
+batch/HELO/EHLO/AUTH/TLS. */
+
+if (smtp_input)
+  {
+  if (!is_inetd) set_process_info("accepting a local %sSMTP message from <%s>",
+    smtp_batched_input? "batched " : "",
+    (sender_address!= NULL)? sender_address : originator_login);
+  }
+else
+  {
+  if (received_protocol == NULL)
+    received_protocol = string_sprintf("local%s", called_as);
+  set_process_info("accepting a local non-SMTP message from <%s>",
+    sender_address);
+  }
+
+/* Initialize the session_local_queue-only flag (this will be ignored if
+mua_wrapper is set) */
+
+queue_check_only();
+session_local_queue_only = queue_only;
+
+/* For non-SMTP and for batched SMTP input, check that there is enough space on
+the spool if so configured. On failure, we must not attempt to send an error
+message! (For interactive SMTP, the check happens at MAIL FROM and an SMTP
+error code is given.) */
+
+if ((!smtp_input || smtp_batched_input) && !receive_check_fs(0))
+  {
+  fprintf(stderr, "exim: insufficient disk space\n");
+  return EXIT_FAILURE;
+  }
+
+/* If this is smtp input of any kind, real or batched, handle the start of the
+SMTP session.
+
+NOTE: We do *not* call smtp_log_no_mail() if smtp_start_session() fails,
+because a log line has already been written for all its failure exists
+(usually "connection refused: <reason>") and writing another one is
+unnecessary clutter. */
+
+if (smtp_input)
+  {
+  smtp_in = stdin;
+  smtp_out = stdout;
+  if (verify_check_host(&hosts_connection_nolog) == OK)
+    log_write_selector &= ~L_smtp_connection;
+  log_write(L_smtp_connection, LOG_MAIN, "%s", smtp_get_connection_info());
+  if (!smtp_start_session())
+    {
+    mac_smtp_fflush();
+    exim_exit(EXIT_SUCCESS);
+    }
+  }
+
+/* Otherwise, set up the input size limit here. */
+
+else
+  {
+  thismessage_size_limit = expand_string_integer(message_size_limit, TRUE);
+  if (expand_string_message != NULL)
+    {
+    if (thismessage_size_limit == -1)
+      log_write(0, LOG_MAIN|LOG_PANIC_DIE, "failed to expand "
+        "message_size_limit: %s", expand_string_message);
+    else
+      log_write(0, LOG_MAIN|LOG_PANIC_DIE, "invalid value for "
+        "message_size_limit: %s", expand_string_message);
+    }
+  }
+
+/* Loop for several messages when reading SMTP input. If we fork any child
+processes, we don't want to wait for them unless synchronous delivery is
+requested, so set SIGCHLD to SIG_IGN in that case. This is not necessarily the
+same as SIG_DFL, despite the fact that documentation often lists the default as
+"ignore". This is a confusing area. This is what I know:
+
+At least on some systems (e.g. Solaris), just setting SIG_IGN causes child
+processes that complete simply to go away without ever becoming defunct. You
+can't then wait for them - but we don't want to wait for them in the
+non-synchronous delivery case. However, this behaviour of SIG_IGN doesn't
+happen for all OS (e.g. *BSD is different).
+
+But that's not the end of the story. Some (many? all?) systems have the
+SA_NOCLDWAIT option for sigaction(). This requests the behaviour that Solaris
+has by default, so it seems that the difference is merely one of default
+(compare restarting vs non-restarting signals).
+
+To cover all cases, Exim sets SIG_IGN with SA_NOCLDWAIT here if it can. If not,
+it just sets SIG_IGN. To be on the safe side it also calls waitpid() at the end
+of the loop below. Paranoia rules.
+
+February 2003: That's *still* not the end of the story. There are now versions
+of Linux (where SIG_IGN does work) that are picky. If, having set SIG_IGN, a
+process then calls waitpid(), a grumble is written to the system log, because
+this is logically inconsistent. In other words, it doesn't like the paranoia.
+As a consequenc of this, the waitpid() below is now excluded if we are sure
+that SIG_IGN works. */
+
+if (!synchronous_delivery)
+  {
+  #ifdef SA_NOCLDWAIT
+  struct sigaction act;
+  act.sa_handler = SIG_IGN;
+  sigemptyset(&(act.sa_mask));
+  act.sa_flags = SA_NOCLDWAIT;
+  sigaction(SIGCHLD, &act, NULL);
+  #else
+  signal(SIGCHLD, SIG_IGN);
+  #endif
+  }
+
+/* Save the current store pool point, for resetting at the start of
+each message, and save the real sender address, if any. */
+
+reset_point = store_get(0);
+real_sender_address = sender_address;
+
+/* Loop to receive messages; receive_msg() returns TRUE if there are more
+messages to be read (SMTP input), or FALSE otherwise (not SMTP, or SMTP channel
+collapsed). */
+
+while (more)
+  {
+  store_reset(reset_point);
+  message_id[0] = 0;
+
+  /* Handle the SMTP case; call smtp_setup_mst() to deal with the initial SMTP
+  input and build the recipients list, before calling receive_msg() to read the
+  message proper. Whatever sender address is given in the SMTP transaction is
+  often ignored for local senders - we use the actual sender, which is normally
+  either the underlying user running this process or a -f argument provided by
+  a trusted caller. It is saved in real_sender_address. The test for whether to
+  accept the SMTP sender is encapsulated in receive_check_set_sender(). */
+
+  if (smtp_input)
+    {
+    int rc;
+    if ((rc = smtp_setup_msg()) > 0)
+      {
+      if (real_sender_address != NULL &&
+          !receive_check_set_sender(sender_address))
+        {
+        sender_address = raw_sender = real_sender_address;
+        sender_address_unrewritten = NULL;
+        }
+
+      /* For batched SMTP, we have to run the acl_not_smtp_start ACL, since it
+      isn't really SMTP, so no other ACL will run until the acl_not_smtp one at
+      the very end. The result of the ACL is ignored (as for other non-SMTP
+      messages). It is run for its potential side effects. */
+
+      if (smtp_batched_input && acl_not_smtp_start != NULL)
+        {
+        uschar *user_msg, *log_msg;
+        enable_dollar_recipients = TRUE;
+        (void)acl_check(ACL_WHERE_NOTSMTP_START, NULL, acl_not_smtp_start,
+          &user_msg, &log_msg);
+        enable_dollar_recipients = FALSE;
+        }
+
+      /* Now get the data for the message */
+
+      more = receive_msg(extract_recipients);
+      if (message_id[0] == 0)
+        {
+        if (more) continue;
+        smtp_log_no_mail();               /* Log no mail if configured */
+        exim_exit(EXIT_FAILURE);
+        }
+      }
+    else
+      {
+      smtp_log_no_mail();               /* Log no mail if configured */
+      exim_exit((rc == 0)? EXIT_SUCCESS : EXIT_FAILURE);
+      }
+    }
+
+  /* In the non-SMTP case, we have all the information from the command
+  line, but must process it in case it is in the more general RFC822
+  format, and in any case, to detect syntax errors. Also, it appears that
+  the use of comma-separated lists as single arguments is common, so we
+  had better support them. */
+
+  else
+    {
+    int i;
+    int rcount = 0;
+    int count = argc - recipients_arg;
+    uschar **list = argv + recipients_arg;
+
+    /* These options cannot be changed dynamically for non-SMTP messages */
+
+    active_local_sender_retain = local_sender_retain;
+    active_local_from_check = local_from_check;
+
+    /* Save before any rewriting */
+
+    raw_sender = string_copy(sender_address);
+
+    /* Loop for each argument */
+
+    for (i = 0; i < count; i++)
+      {
+      int start, end, domain;
+      uschar *errmess;
+      uschar *s = list[i];
+
+      /* Loop for each comma-separated address */
+
+      while (*s != 0)
+        {
+        BOOL finished = FALSE;
+        uschar *recipient;
+        uschar *ss = parse_find_address_end(s, FALSE);
+
+        if (*ss == ',') *ss = 0; else finished = TRUE;
+
+        /* Check max recipients - if -t was used, these aren't recipients */
+
+        if (recipients_max > 0 && ++rcount > recipients_max &&
+            !extract_recipients)
+          {
+          if (error_handling == ERRORS_STDERR)
+            {
+            fprintf(stderr, "exim: too many recipients\n");
+            exim_exit(EXIT_FAILURE);
+            }
+          else
+            {
+            return
+              moan_to_sender(ERRMESS_TOOMANYRECIP, NULL, NULL, stdin, TRUE)?
+                errors_sender_rc : EXIT_FAILURE;
+            }
+          }
+
+        recipient =
+          parse_extract_address(s, &errmess, &start, &end, &domain, FALSE);
+
+        if (domain == 0 && !allow_unqualified_recipient)
+          {
+          recipient = NULL;
+          errmess = US"unqualified recipient address not allowed";
+          }
+
+        if (recipient == NULL)
+          {
+          if (error_handling == ERRORS_STDERR)
+            {
+            fprintf(stderr, "exim: bad recipient address \"%s\": %s\n",
+              string_printing(list[i]), errmess);
+            exim_exit(EXIT_FAILURE);
+            }
+          else
+            {
+            error_block eblock;
+            eblock.next = NULL;
+            eblock.text1 = string_printing(list[i]);
+            eblock.text2 = errmess;
+            return
+              moan_to_sender(ERRMESS_BADARGADDRESS, &eblock, NULL, stdin, TRUE)?
+                errors_sender_rc : EXIT_FAILURE;
+            }
+          }
+
+        receive_add_recipient(recipient, -1);
+        s = ss;
+        if (!finished)
+          while (*(++s) != 0 && (*s == ',' || isspace(*s)));
+        }
+      }
+
+    /* Show the recipients when debugging */
+
+    DEBUG(D_receive)
+      {
+      int i;
+      if (sender_address != NULL) debug_printf("Sender: %s\n", sender_address);
+      if (recipients_list != NULL)
+        {
+        debug_printf("Recipients:\n");
+        for (i = 0; i < recipients_count; i++)
+          debug_printf("  %s\n", recipients_list[i].address);
+        }
+      }
+
+    /* Run the acl_not_smtp_start ACL if required. The result of the ACL is
+    ignored; rejecting here would just add complication, and it can just as
+    well be done later. Allow $recipients to be visible in the ACL. */
+
+    if (acl_not_smtp_start != NULL)
+      {
+      uschar *user_msg, *log_msg;
+      enable_dollar_recipients = TRUE;
+      (void)acl_check(ACL_WHERE_NOTSMTP_START, NULL, acl_not_smtp_start,
+        &user_msg, &log_msg);
+      enable_dollar_recipients = FALSE;
+      }
+
+    /* Read the data for the message. If filter_test is not FTEST_NONE, this
+    will just read the headers for the message, and not write anything onto the
+    spool. */
+
+    message_ended = END_NOTENDED;
+    more = receive_msg(extract_recipients);
+
+    /* more is always FALSE here (not SMTP message) when reading a message
+    for real; when reading the headers of a message for filter testing,
+    it is TRUE if the headers were terminated by '.' and FALSE otherwise. */
+
+    if (message_id[0] == 0) exim_exit(EXIT_FAILURE);
+    }  /* Non-SMTP message reception */
+
+  /* If this is a filter testing run, there are headers in store, but
+  no message on the spool. Run the filtering code in testing mode, setting
+  the domain to the qualify domain and the local part to the current user,
+  unless they have been set by options. The prefix and suffix are left unset
+  unless specified. The the return path is set to to the sender unless it has
+  already been set from a return-path header in the message. */
+
+  if (filter_test != FTEST_NONE)
+    {
+    deliver_domain = (ftest_domain != NULL)?
+      ftest_domain : qualify_domain_recipient;
+    deliver_domain_orig = deliver_domain;
+    deliver_localpart = (ftest_localpart != NULL)?
+      ftest_localpart : originator_login;
+    deliver_localpart_orig = deliver_localpart;
+    deliver_localpart_prefix = ftest_prefix;
+    deliver_localpart_suffix = ftest_suffix;
+    deliver_home = originator_home;
+
+    if (return_path == NULL)
+      {
+      printf("Return-path copied from sender\n");
+      return_path = string_copy(sender_address);
+      }
+    else
+      {
+      printf("Return-path = %s\n", (return_path[0] == 0)? US"<>" : return_path);
+      }
+    printf("Sender      = %s\n", (sender_address[0] == 0)? US"<>" : sender_address);
+
+    receive_add_recipient(
+      string_sprintf("%s%s%s@%s",
+        (ftest_prefix == NULL)? US"" : ftest_prefix,
+        deliver_localpart,
+        (ftest_suffix == NULL)? US"" : ftest_suffix,
+        deliver_domain), -1);
+
+    printf("Recipient   = %s\n", recipients_list[0].address);
+    if (ftest_prefix != NULL) printf("Prefix    = %s\n", ftest_prefix);
+    if (ftest_suffix != NULL) printf("Suffix    = %s\n", ftest_suffix);
+
+    if (chdir("/"))   /* Get away from wherever the user is running this from */
+      {
+      DEBUG(D_receive) debug_printf("chdir(\"/\") failed\n");
+      exim_exit(EXIT_FAILURE);
+      }
+
+    /* Now we run either a system filter test, or a user filter test, or both.
+    In the latter case, headers added by the system filter will persist and be
+    available to the user filter. We need to copy the filter variables
+    explicitly. */
+
+    if ((filter_test & FTEST_SYSTEM) != 0)
+      {
+      if (!filter_runtest(filter_sfd, filter_test_sfile, TRUE, more))
+        exim_exit(EXIT_FAILURE);
+      }
+
+    memcpy(filter_sn, filter_n, sizeof(filter_sn));
+
+    if ((filter_test & FTEST_USER) != 0)
+      {
+      if (!filter_runtest(filter_ufd, filter_test_ufile, FALSE, more))
+        exim_exit(EXIT_FAILURE);
+      }
+
+    exim_exit(EXIT_SUCCESS);
+    }
+
+  /* Else act on the result of message reception. We should not get here unless
+  message_id[0] is non-zero. If queue_only is set, session_local_queue_only
+  will be TRUE. If it is not, check on the number of messages received in this
+  connection. */
+
+  if (!session_local_queue_only &&
+      smtp_accept_queue_per_connection > 0 &&
+      receive_messagecount > smtp_accept_queue_per_connection)
+    {
+    session_local_queue_only = TRUE;
+    queue_only_reason = 2;
+    }
+
+  /* Initialize local_queue_only from session_local_queue_only. If it is false,
+  and queue_only_load is set, check that the load average is below it. If it is
+  not, set local_queue_only TRUE. If queue_only_load_latch is true (the
+  default), we put the whole session into queue_only mode. It then remains this
+  way for any subsequent messages on the same SMTP connection. This is a
+  deliberate choice; even though the load average may fall, it doesn't seem
+  right to deliver later messages on the same call when not delivering earlier
+  ones. However, there are odd cases where this is not wanted, so this can be
+  changed by setting queue_only_load_latch false. */
+
+  local_queue_only = session_local_queue_only;
+  if (!local_queue_only && queue_only_load >= 0)
+    {
+    local_queue_only = (load_average = OS_GETLOADAVG()) > queue_only_load;
+    if (local_queue_only)
+      {
+      queue_only_reason = 3;
+      if (queue_only_load_latch) session_local_queue_only = TRUE;
+      }
+    }
+
+  /* If running as an MUA wrapper, all queueing options and freezing options
+  are ignored. */
+
+  if (mua_wrapper)
+    local_queue_only = queue_only_policy = deliver_freeze = FALSE;
+
+  /* Log the queueing here, when it will get a message id attached, but
+  not if queue_only is set (case 0). Case 1 doesn't happen here (too many
+  connections). */
+
+  if (local_queue_only) switch(queue_only_reason)
+    {
+    case 2:
+    log_write(L_delay_delivery,
+              LOG_MAIN, "no immediate delivery: more than %d messages "
+      "received in one connection", smtp_accept_queue_per_connection);
+    break;
+
+    case 3:
+    log_write(L_delay_delivery,
+              LOG_MAIN, "no immediate delivery: load average %.2f",
+              (double)load_average/1000.0);
+    break;
+    }
+
+  /* Else do the delivery unless the ACL or local_scan() called for queue only
+  or froze the message. Always deliver in a separate process. A fork failure is
+  not a disaster, as the delivery will eventually happen on a subsequent queue
+  run. The search cache must be tidied before the fork, as the parent will
+  do it before exiting. The child will trigger a lookup failure and
+  thereby defer the delivery if it tries to use (for example) a cached ldap
+  connection that the parent has called unbind on. */
+
+  else if (!queue_only_policy && !deliver_freeze)
+    {
+    pid_t pid;
+    search_tidyup();
+
+    if ((pid = fork()) == 0)
+      {
+      int rc;
+      close_unwanted();      /* Close unwanted file descriptors and TLS */
+      exim_nullstd();        /* Ensure std{in,out,err} exist */
+
+      /* Re-exec Exim if we need to regain privilege (note: in mua_wrapper
+      mode, deliver_drop_privilege is forced TRUE). */
+
+      if (geteuid() != root_uid && !deliver_drop_privilege && !unprivileged)
+        {
+        (void)child_exec_exim(CEE_EXEC_EXIT, FALSE, NULL, FALSE, 2, US"-Mc",
+          message_id);
+        /* Control does not return here. */
+        }
+
+      /* No need to re-exec */
+
+      rc = deliver_message(message_id, FALSE, FALSE);
+      search_tidyup();
+      _exit((!mua_wrapper || rc == DELIVER_MUA_SUCCEEDED)?
+        EXIT_SUCCESS : EXIT_FAILURE);
+      }
+
+    if (pid < 0)
+      {
+      log_write(0, LOG_MAIN|LOG_PANIC, "failed to fork automatic delivery "
+        "process: %s", strerror(errno));
+      }
+
+    /* In the parent, wait if synchronous delivery is required. This will
+    always be the case in MUA wrapper mode. */
+
+    else if (synchronous_delivery)
+      {
+      int status;
+      while (wait(&status) != pid);
+      if ((status & 0x00ff) != 0)
+        log_write(0, LOG_MAIN|LOG_PANIC,
+          "process %d crashed with signal %d while delivering %s",
+          (int)pid, status & 0x00ff, message_id);
+      if (mua_wrapper && (status & 0xffff) != 0) exim_exit(EXIT_FAILURE);
+      }
+    }
+
+  /* The loop will repeat if more is TRUE. If we do not know know that the OS
+  automatically reaps children (see comments above the loop), clear away any
+  finished subprocesses here, in case there are lots of messages coming in
+  from the same source. */
+
+  #ifndef SIG_IGN_WORKS
+  while (waitpid(-1, NULL, WNOHANG) > 0);
+  #endif
+  }
+
+exim_exit(EXIT_SUCCESS);   /* Never returns */
+return 0;                  /* To stop compiler warning */
+}
+
+/* End of exim.c */
diff --git a/src/exim.h b/src/exim.h
new file mode 100644 (file)
index 0000000..fb48a43
--- /dev/null
@@ -0,0 +1,594 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2014 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+
+/* Source files for exim all #include this header, which drags in everything
+that is needed. They don't all need everything, of course, but it's far too
+messy to have each one importing its own list, and anyway, most of them need
+most of these includes. */
+
+/* Assume most systems have statfs() unless os.h undefines this macro */
+
+#define HAVE_STATFS
+
+/* Similarly, assume most systems have srandom() unless os.h undefines it.
+This call dates back at least as far as SUSv2. */
+
+#define HAVE_SRANDOM
+
+/* This is primarily for the Gnu C library; we define it before os.h so that
+os.h has a chance to hurriedly undef it, Just In Case.  We need C99 for some
+64-bit math support, and defining _ISOC99_SOURCE breaks <resolv.h> and friends.
+*/
+
+#define _GNU_SOURCE 1
+
+/* First of all include the os-specific header, which might set things that
+are needed by any of the other headers, including system headers. */
+
+#include "os.h"
+
+/* If it didn't define os_find_running_interfaces, use the common function. */
+
+#ifndef os_find_running_interfaces
+# define os_find_running_interfaces os_common_find_running_interfaces
+#endif
+
+/* If it didn't define the base for "base 62" numbers, we really do use 62.
+This is the case for all real Unix and Unix-like OS. It's only Cygwin and
+Darwin, with their case-insensitive file systems, that can't use base 62 for
+making unique names. */
+
+#ifndef BASE_62
+# define BASE_62 62
+#endif
+
+/* The maximum value of localhost_number depends on the base being used */
+
+#if BASE_62 == 62
+# define LOCALHOST_MAX  16
+#else
+# define LOCALHOST_MAX  10
+#endif
+
+/* If not overridden by os.h, dynamic libraries have filenames ending .so */
+#ifndef DYNLIB_FN_EXT
+# define DYNLIB_FN_EXT "so"
+#endif
+
+/* ANSI C standard includes */
+
+#include <ctype.h>
+#include <locale.h>
+#include <math.h>
+#include <signal.h>
+#include <stdarg.h>
+#include <stddef.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+
+/* Unix includes */
+
+#include <errno.h>
+#if defined(__svr4__) && defined(__sparc) && ! defined(__EXTENSIONS__)
+# define __EXTENSIONS__  /* so that SunOS 5 gets NGROUPS_MAX */
+# include <limits.h>
+# undef  __EXTENSIONS__
+#else
+# include <limits.h>
+#endif
+
+/* C99 integer types, figure out how to undo this if needed for older systems */
+
+#include <inttypes.h>
+
+/* Just in case some aged system doesn't define them... */
+
+#ifndef INT_MAX
+# define INT_MAX 2147483647
+#endif
+
+#ifndef INT_MIN
+# define INT_MIN (-INT_MAX - 1)
+#endif
+
+#ifndef SHRT_MAX
+# define SHRT_MAX 32767
+#endif
+
+#ifndef UCHAR_MAX
+# define UCHAR_MAX 255
+#endif
+
+
+/* To match int_eximarith_t.  Define in OS/os.h-<your-system> to override. */
+#ifndef EXIM_ARITH_MAX
+# define EXIM_ARITH_MAX ((int_eximarith_t)9223372036854775807LL)
+#endif
+#ifndef EXIM_ARITH_MIN
+# define EXIM_ARITH_MIN (-EXIM_ARITH_MAX - 1)
+#endif
+
+/* Some systems have PATH_MAX and some have MAX_PATH_LEN. */
+
+#ifndef PATH_MAX
+# ifdef MAX_PATH_LEN
+#  define PATH_MAX MAX_PATH_LEN
+# else
+#  define PATH_MAX 1024
+# endif
+#endif
+
+#include <sys/types.h>
+#include <sys/file.h>
+#include <dirent.h>
+#include <netdb.h>
+#ifndef NO_POLL_H
+# include <poll.h>
+#endif
+#include <pwd.h>
+#include <grp.h>
+#include <syslog.h>
+
+/* Not all systems have flock() available. Those that do must define LOCK_SH
+in sys/file.h. */
+
+#ifndef LOCK_SH
+# define NO_FLOCK
+#endif
+
+#ifndef NO_SYSEXITS        /* some OS don't have this */
+# include <sysexits.h>
+#endif
+
+/* A few OS don't have socklen_t; their os.h files define EXIM_SOCKLEN_T to
+be size_t or whatever. We used to use SOCKLEN_T, but then it was discovered
+that this is used by the AIX include files. */
+
+#ifndef EXIM_SOCKLEN_T
+# define EXIM_SOCKLEN_T socklen_t
+#endif
+
+/* Ensure that the sysexits we reference are defined */
+
+#ifndef EX_UNAVAILABLE
+# define EX_UNAVAILABLE 69        /* service unavailable; used for execv fail */
+#endif
+#ifndef EX_CANTCREAT
+# define EX_CANTCREAT   73        /* can't create file: treat as temporary */
+#endif
+#ifndef EX_TEMPFAIL
+# define EX_TEMPFAIL    75        /* temp failure; user is invited to retry */
+#endif
+#ifndef EX_CONFIG
+# define EX_CONFIG      78        /* configuration error */
+#endif
+
+/* This one is not in any sysexits file that I've come across */
+
+#define EX_EXECFAILED 127        /* execve() failed */
+
+
+#include <sys/time.h>
+#include <sys/param.h>
+
+#ifndef NO_SYS_RESOURCE_H  /* QNX doesn't have this */
+# include <sys/resource.h>
+#endif
+
+#include <sys/socket.h>
+
+/* If we are on an IPv6 system, the macro AF_INET6 will have been defined in
+the sys/socket.h header. It is helpful to have this defined on an IPv4 system
+so that it can appear in the code, even if it is never actually used when
+the code is run. It saves some #ifdef occurrences. */
+
+#ifndef AF_INET6
+# define AF_INET6 24
+#endif
+
+#include <sys/ioctl.h>
+
+/* The new standard is statvfs; some OS have statfs. For statvfs the block
+counts must be multiplied by the "fragment size" f_frsize to get the actual
+size. In other cases the value seems to be f_bsize (which is sometimes the only
+block size), so we use a macro to get that instead.
+
+Also arrange to be able to cut it out altogether for way-out OS that don't have
+anything. I've indented a bit here to try to make the mess a bit more
+intelligible. Note that simply defining one name to be another when
+HAVE_SYS_STATVFS_H is not set will not work if the system has a statvfs macro
+or a macro with entries f_frsize and f_bsize. */
+
+#ifdef HAVE_STATFS
+  #ifdef HAVE_SYS_STATVFS_H
+    #include <sys/statvfs.h>
+    #define STATVFS statvfs
+    #define F_FRSIZE f_frsize
+  #else
+    #define STATVFS statfs
+    #define F_FRSIZE f_bsize
+    #ifdef HAVE_SYS_VFS_H
+      #include <sys/vfs.h>
+      #ifdef HAVE_SYS_STATFS_H
+      #include <sys/statfs.h>
+      #endif
+    #endif
+    #ifdef HAVE_SYS_MOUNT_H
+    #include <sys/mount.h>
+    #endif
+  #endif
+
+  /* Macros for the fields for the available space for non-superusers; define
+  these only if the OS header has not. Not all OS have f_favail; those that
+  are known to have it define F_FAVAIL as f_favail. The default is to use
+  f_free. */
+
+  #ifndef F_BAVAIL
+  # define F_BAVAIL f_bavail
+  #endif
+
+  #ifndef F_FAVAIL
+  # define F_FAVAIL f_ffree
+  #endif
+
+  /* All the systems I've been able to look at seem to have F_FILES */
+
+  #ifndef F_FILES
+  # define F_FILES  f_files
+  #endif
+
+#endif
+
+
+#ifndef  SIOCGIFCONF   /* HACK for SunOS 5 */
+# include <sys/sockio.h>
+#endif
+
+#include <sys/stat.h>
+#include <sys/wait.h>
+#include <sys/utsname.h>
+#include <fcntl.h>
+
+/* There's a shambles in IRIX6 - it defines EX_OK in unistd.h which conflicts
+with the definition in sysexits.h. Exim does not actually use this macro, so we
+just undefine it. It would be nice to be able to re-instate the definition from
+sysexits.h if there is no definition in unistd.h, but I do not think there is a
+way to do this in C because macro definitions are not scanned for other macros
+at definition time. [The code here used to assume they were, until I was
+disabused of the notion. Luckily, since EX_OK is not used, it didn't matter.] */
+
+#ifdef EX_OK
+# undef EX_OK
+#endif
+
+#include <unistd.h>
+
+#include <utime.h>
+#ifndef NO_NET_IF_H
+# include <net/if.h>
+#endif
+#include <sys/un.h>
+#include <netinet/in.h>
+#include <netinet/tcp.h>
+#include <arpa/inet.h>
+#include <arpa/nameser.h>
+
+
+/* If arpa/nameser.h defines a maximum name server packet size, use it,
+provided it is greater than 2048. Otherwise go for a default. PACKETSZ was used
+for this, but it seems that NS_PACKETSZ is coming into use. */
+
+#if defined(NS_PACKETSZ) && NS_PACKETSZ >= 2048
+  #define MAXPACKET NS_PACKETSZ
+#elif defined(PACKETSZ) && PACKETSZ >= 2048
+  #define MAXPACKET PACKETSZ
+#else
+  #define MAXPACKET 2048
+#endif
+
+/* While IPv6 is still young the definitions of T_AAAA and T_A6 may not be
+included in arpa/nameser.h. Fudge them here. */
+
+#ifndef T_AAAA
+#define T_AAAA 28
+#endif
+
+#ifndef T_A6
+#define T_A6 38
+#endif
+
+/* Ancient systems (e.g. SunOS4) don't appear to have T_TXT defined in their
+header files. I don't suppose they have T_SRV either. */
+
+#ifndef T_TXT
+# define T_TXT 16
+#endif
+
+#ifndef T_SRV
+# define T_SRV 33
+#endif
+
+/* Many systems do not have T_SPF. */
+
+#ifndef T_SPF
+# define T_SPF 99
+#endif
+
+/* New TLSA record for DANE */
+#ifndef T_TLSA
+# define T_TLSA 52
+#endif
+#define MAX_TLSA_EXPANDED_SIZE 8192
+
+/* It seems that some versions of arpa/nameser.h don't define *any* of the
+T_xxx macros, which seem to be non-standard nowadays. Just to be on the safe
+side, put in definitions for all the ones that Exim uses. */
+
+#ifndef T_A
+# define T_A 1
+#endif
+
+#ifndef T_CNAME
+# define T_CNAME 5
+#endif
+
+#ifndef T_SOA
+# define T_SOA 6
+#endif
+
+#ifndef T_MX
+# define T_MX 15
+#endif
+
+#ifndef T_NS
+# define T_NS 2
+#endif
+
+#ifndef T_PTR
+# define T_PTR 12
+#endif
+
+
+/* We define a few private types for special DNS lookups:
+
+ . T_ZNS gets the nameservers of the enclosing zone of a domain
+
+ . T_MXH gets the MX hostnames only (without their priorities)
+
+ . T_CSA gets the domain's Client SMTP Authorization SRV record
+
+ . T_ADDRESSES looks up both AAAA (or A6) and A records
+
+If any of these names appear in the RRtype list at:
+  <http://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml>
+then we should rename Exim's private type away from the conflict.
+*/
+
+#define T_ZNS (-1)
+#define T_MXH (-2)
+#define T_CSA (-3)
+#define T_ADDRESSES (-4)
+
+/* The resolv.h header defines __P(x) on some Solaris 2.5.1 systems (without
+checking that it is already defined, in fact). This conflicts with other
+headers that behave likewise (see below), leading to compiler warnings. Arrange
+to undefine it if resolv.h defines it. */
+
+#if defined(__P)
+# define __P_WAS_DEFINED_BEFORE_RESOLV
+#endif
+
+#include <resolv.h>
+
+#if defined(__P) && ! defined (__P_WAS_DEFINED_BEFORE_RESOLV)
+# undef __P
+#endif
+
+/* If not defined by os.h, we do nothing special to push DNS resolver state
+back to be available by the classic resolver routines.  Also, provide
+prototype for our get routine, unless defined away. */
+
+#ifndef os_put_dns_resolver_res
+# define os_put_dns_resolver_res(R) do {/**/} while(0)
+#endif
+#ifndef os_get_dns_resolver_res
+res_state os_get_dns_resolver_res(void);
+#endif
+
+/* These three are to support the IP option logging code. Linux is
+different to everyone else and there are also other systems which don't
+have netinet/ip_var.h, so there's a general macro to control its inclusion. */
+
+#include <netinet/in_systm.h>
+#include <netinet/ip.h>
+
+#ifndef NO_IP_VAR_H
+# include <netinet/ip_var.h>
+#endif
+
+/* Linux (and some others) uses a different type for the 2nd argument of
+iconv(). It's os.h file defines ICONV_ARG2_TYPE. For the rest, define a default
+here. */
+
+#ifndef ICONV_ARG2_TYPE
+# define ICONV_ARG2_TYPE const char **
+#endif
+
+/* One OS uses a different type for the 5th argument of getsockopt */
+
+#ifndef GETSOCKOPT_ARG5_TYPE
+# define GETSOCKOPT_ARG5_TYPE socklen_t *
+#endif
+
+/* One operating system uses a different type for the 2nd argument of select().
+Its os.h file defines SELECT_ARG2_TYPE. For the rest, define a default here. */
+
+#ifndef SELECT_ARG2_TYPE
+# define SELECT_ARG2_TYPE fd_set
+#endif
+
+/* One operating system uses a different type for the 4th argument of
+dn_expand(). Its os.h file defines DN_EXPAND_ARG4_TYPE. For the rest, define a
+default here. */
+
+#ifndef DN_EXPAND_ARG4_TYPE
+# define DN_EXPAND_ARG4_TYPE char *
+#endif
+
+/* One operating system defines a different type for the yield of inet_addr().
+In Exim code, its value is always assigned to the s_addr members of address
+structures. Casting the yield to the type of s_addr should fix the problem,
+since the size of the data is correct. Just in case this ever has to be
+changed, use a macro for the type, and define it here so that it is possible to
+use different values for specific OS if ever necessary. */
+
+#ifndef S_ADDR_TYPE
+# define S_ADDR_TYPE u_long
+#endif
+
+/* (At least) one operating system (Solaris) defines a different type for the
+second argument of pam_converse() - the difference is the absence of "const".
+Its os.h file defines PAM_CONVERSE_ARG2_TYPE. For the rest, define a default
+here. */
+
+#ifndef PAM_CONVERSE_ARG2_TYPE
+# define PAM_CONVERSE_ARG2_TYPE const struct pam_message
+#endif
+
+/* One operating system (SunOS4) defines getc, ungetc, feof, and ferror as
+macros and not as functions. Exim needs them to be assignable functions. This
+flag gets set to cause this to be sorted out here. */
+
+#ifdef FUDGE_GETC_AND_FRIENDS
+# undef getc
+extern int getc(FILE *);
+# undef ungetc
+extern int ungetc(int, FILE *);
+# undef feof
+extern int feof(FILE *);
+# undef ferror
+extern int ferror(FILE *);
+#endif
+
+/* The header from the PCRE regex package */
+
+#include <pcre.h>
+
+/* Exim includes are in several files. Note that local_scan.h #includes
+config.h, mytypes.h, and store.h, so we don't need to mention them explicitly.
+*/
+
+#include "local_scan.h"
+#include "macros.h"
+#include "dbstuff.h"
+#include "structs.h"
+#include "globals.h"
+#include "functions.h"
+#include "dbfunctions.h"
+#include "osfunctions.h"
+
+#ifdef EXPERIMENTAL_BRIGHTMAIL
+# include "bmi_spam.h"
+#endif
+#ifdef EXPERIMENTAL_SPF
+# include "spf.h"
+#endif
+#ifdef EXPERIMENTAL_SRS
+# include "srs.h"
+#endif
+#ifndef DISABLE_DKIM
+# include "dkim.h"
+#endif
+#ifdef EXPERIMENTAL_DMARC
+# include "dmarc.h"
+# include <opendmarc/dmarc.h>
+#endif
+
+/* The following stuff must follow the inclusion of config.h because it
+requires various things that are set therein. */
+
+#if HAVE_ICONV             /* Not all OS have this */
+# include <iconv.h>
+#endif
+
+#if defined(USE_READLINE) || defined(EXPAND_DLFUNC) || defined (LOOKUP_MODULE_DIR)
+# include <dlfcn.h>
+#endif
+
+#ifdef ENABLE_DISABLE_FSYNC
+# define EXIMfsync(f) (disable_fsync? 0 : fsync(f))
+#else
+# define EXIMfsync(f) fsync(f)
+#endif
+
+/* Backward compatibility; LOOKUP_LSEARCH now includes all three */
+
+#if (!defined LOOKUP_LSEARCH) && (defined LOOKUP_WILDLSEARCH || defined LOOKUP_NWILDLSEARCH)
+# define LOOKUP_LSEARCH yes
+#endif
+
+/* Define a union to hold either an IPv4 or an IPv6 sockaddr structure; this
+simplifies some of the coding.  We include the sockaddr to reduce type-punning
+issues in C99. */
+
+union sockaddr_46 {
+  struct sockaddr_in v4;
+  #if HAVE_IPV6
+  struct sockaddr_in6 v6;
+  #endif
+  struct sockaddr v0;
+};
+
+/* If SUPPORT_TLS is not defined, ensure that USE_GNUTLS is also not defined
+so that if USE_GNUTLS *is* set, we can assume SUPPORT_TLS is also set. */
+
+#ifndef SUPPORT_TLS
+# undef USE_GNUTLS
+#endif
+
+/* If SPOOL_DIRECTORY, LOG_FILE_PATH or PID_FILE_PATH have not been defined,
+set them to the null string. */
+
+#ifndef SPOOL_DIRECTORY
+  #define SPOOL_DIRECTORY ""
+#endif
+#ifndef LOG_FILE_PATH
+  #define LOG_FILE_PATH ""
+#endif
+#ifndef PID_FILE_PATH
+  #define PID_FILE_PATH ""
+#endif
+
+/* The EDQUOT error code isn't universally available, though it is widespread.
+There is a particular shambles in SunOS5, where it did not exist originally,
+but got installed with a particular patch for Solaris 2.4. There is a
+configuration variable for specifying what the system's "over quota" error is,
+which will end up in config.h if supplied in OS/Makefile-xxx. If it is not set,
+default to EDQUOT if it exists, otherwise ENOSPC. */
+
+#ifndef ERRNO_QUOTA
+# ifdef  EDQUOT
+#  define ERRNO_QUOTA EDQUOT
+# else
+#  define ERRNO_QUOTA ENOSPC
+# endif
+#endif
+
+/* Ensure PATH_MAX is defined */
+
+#ifndef PATH_MAX
+  #ifdef MAXPATHLEN
+  # define PATH_MAX MAXPATHLEN
+  #else
+  # define PATH_MAX 1024
+  #endif
+#endif
+
+/* End of exim.h */
diff --git a/src/exim_checkaccess.src b/src/exim_checkaccess.src
new file mode 100755 (executable)
index 0000000..84d03c0
--- /dev/null
@@ -0,0 +1,172 @@
+#! /bin/sh
+
+# Copyright (c) University of Cambridge, 1995 - 2007
+# See the file NOTICE for conditions of use and distribution.
+
+# Except when they appear in comments, the following placeholders in this
+# source are replaced when it is turned into a runnable script:
+#
+# CONFIGURE_FILE_USE_NODE
+# CONFIGURE_FILE
+# BIN_DIRECTORY
+# PERL_COMMAND
+
+# PROCESSED_FLAG
+
+# A shell+perl wrapper script to run an automated -bh test to check out
+# ACLs for incoming addresses.
+
+# Save the shell arguments because we are going to need the shell variables
+# while sorting out the configuration file.
+
+args="$@"
+
+# See if this installation is using the esoteric "USE_NODE" feature of Exim,
+# in which it uses the host's name as a suffix for the configuration file name.
+
+if [ "CONFIGURE_FILE_USE_NODE" = "yes" ]; then
+  hostsuffix=.`uname -n`
+fi
+
+# Now find the configuration file name. This has got complicated because
+# CONFIGURE_FILE may now be a list of files. The one that is used is the first
+# one that exists. Mimic the code in readconf.c by testing first for the
+# suffixed file in each case.
+
+set `awk -F: '{ for (i = 1; i <= NF; i++) print $i }' <<End
+CONFIGURE_FILE
+End
+`
+while [ "$config" = "" -a $# -gt 0 ] ; do
+  if [ -f "$1$hostsuffix" ] ; then
+    config="$1$hostsuffix"
+  elif [ -f "$1" ] ; then
+    config="$1"
+  fi
+  shift
+done
+
+# Search for an exim_path setting in the configure file; otherwise use the bin
+# directory. BEWARE: a tab character is needed in the command below. It has had
+# a nasty tendency to get lost in the past. Use a variable to hold a space and
+# a tab to keep the tab in one place.
+
+exim_path=`perl -ne 'chop;if (/^\s*exim_path\s*=\s*(.*)/){print "$1\n";last;}' $config`
+if test "$exim_path" = ""; then exim_path=BIN_DIRECTORY/exim; fi
+
+
+#########################################################################
+
+
+# Now run the perl script, passing in the Exim path and the arguments given
+# to the overall script.
+
+PERL_COMMAND - $exim_path $args <<'End'
+
+use FileHandle;
+use IPC::Open2;
+
+if (scalar(@ARGV) < 3)
+  {
+  print "Usage: exim_checkaccess <IP address> <email address> [exim options]\n";
+  exit(1);
+  }
+
+$exim_path = $ARGV[0];          # Set up by the calling shell script
+$host      = $ARGV[1];          # Mandatory original first argument
+$recipient = $ARGV[2];          # Mandatory original second argument
+
+$c4 = qr/2 (?:[0-4]\d | 5[0-5]) | 1\d\d | \d{1,2}/x;  # IPv4 component
+$a4 = qr/^$c4\.$c4\.$c4\.$c4$/;                       # IPv4 address
+
+$c6 = qr/[0-9a-f]{1,4}/i;                             # IPv6 component
+
+# Split the various formats of IPv6 addresses into several cases. I don't
+# think I can graft regex that matches all of them without using alternatives.
+
+# 1. Starts with :: followed by up to 7 components
+
+$a6_0 = qr/^::(?:$c6:){0,6}$c6$/x;
+
+# 2. 8 non-empty components
+
+$a6_1 = qr/^(?:$c6:){7}$c6$/x;
+
+# 3. This is the cunning one. Up to 7 components, one (and only one) of which
+# can be empty. We use 0 to cause a failure when we've already matched
+# an empty component and may be hitting other. This has to fail, because we
+# know we've just failed to match a component. We also do a final check to
+# ensure that there has been an empty component.
+
+$a6_2 = qr/^(?: (?: $c6 | (?(1)0 | () ) ) : ){1,7}$c6 $ (?(1)|.)/x;
+
+if ($host !~ /$a4 | $a6_0 | $a6_1 | $a6_2/x)
+  {
+  print "** Invalid IP address \"$host\"\n";
+  print "Usage: exim_checkaccess <IP address> <email address> [exim options]\n";
+  exit(1);
+  }
+
+# Build any remaining original arguments into a string for passing over
+# as Exim options.
+
+$opt = "";
+for ($i = 3; $i < scalar(@ARGV); $i++) { $opt .= "$ARGV[$i] "; }
+
+# If the string contains "-f xxxx", extract that as the sender. Otherwise
+# the sender is <>.
+
+$sender    = "";
+if ($opt =~ /(?:^|\s)-f\s+(\S+|"[^"]*")/)
+  {
+  $sender = $1;
+  $opt = $` . $';
+  }
+
+# Run a -bh test in Exim, passing the test data
+
+$pid = open2(*IN, *OUT, "$exim_path -bh $host $opt 2>/dev/null");
+print OUT "HELO [$host]\r\n";
+print OUT "MAIL FROM:<$sender>\r\n";
+print OUT "RCPT TO:<$recipient>\r\n";
+print OUT "QUIT\r\n";
+close OUT;
+
+# Read the output, ignoring anything but the SMTP response to the RCPT
+# command.
+
+$count = 0;
+$reply = "";
+
+while (<IN>)
+  {
+  next if !/^\d\d\d/;
+  $reply .= $_;
+  next if /^\d\d\d\-/;
+
+  if (++$count != 4)
+    {
+    $reply = "";
+    next;
+    }
+
+  # We have the response we want. Interpret it.
+
+  if ($reply =~ /^2\d\d/)
+    {
+    print "Accepted\n";
+    }
+  else
+    {
+    print "Rejected:\n";
+    $reply =~ s/\n(.)/\n  $1/g;
+    print "  $reply";
+    }
+  last;
+  }
+
+# Reap the child process
+
+waitpid $pid, 0;
+
+End
diff --git a/src/exim_dbmbuild.c b/src/exim_dbmbuild.c
new file mode 100644 (file)
index 0000000..ce06f16
--- /dev/null
@@ -0,0 +1,502 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+
+/* A small freestanding program to build dbm databases from serial input. For
+alias files, this program fulfils the function of the newaliases program used
+by other mailers, but it can be used for other dbm data files too. It operates
+by writing a new file or files, and then renaming; otherwise old entries can
+never get flushed out.
+
+This program is clever enough to cope with ndbm, which creates two files called
+<name>.dir and <name>.pag, or with db, which creates a single file called
+<name>.db. If native db is in use (USE_DB defined) or tdb is in use (USE_TDB
+defined) there is no extension to the output filename. This is also handled. If
+there are any other variants, the program won't cope.
+
+The first argument to the program is the name of the serial file; the second
+is the base name for the DBM file(s). When native db is in use, these must be
+different.
+
+Input lines beginning with # are ignored, as are blank lines. Entries begin
+with a key terminated by a colon or end of line or whitespace and continue with
+indented lines. Keys may be quoted if they contain colons or whitespace or #
+characters. */
+
+
+#include "exim.h"
+
+
+#define max_insize   20000
+#define max_outsize 100000
+
+/* This is global because it's defined in the headers and compilers grumble
+if it is made static. */
+
+const uschar *hex_digits = CUS"0123456789abcdef";
+
+
+#ifdef STRERROR_FROM_ERRLIST
+/* Some old-fashioned systems still around (e.g. SunOS4) don't have strerror()
+in their libraries, but can provide the same facility by this simple
+alternative function. */
+
+char *
+strerror(int n)
+{
+if (n < 0 || n >= sys_nerr) return "unknown error number";
+return sys_errlist[n];
+}
+#endif /* STRERROR_FROM_ERRLIST */
+
+
+/* For Berkeley DB >= 2, we can define a function to be called in case of DB
+errors. This should help with debugging strange DB problems, e.g. getting "File
+exists" when you try to open a db file. The API changed at release 4.3. */
+
+#if defined(USE_DB) && defined(DB_VERSION_STRING)
+void
+#if DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 3)
+dbfn_bdb_error_callback(const DB_ENV *dbenv, const char *pfx, const char *msg)
+{
+dbenv = dbenv;
+#else
+dbfn_bdb_error_callback(const char *pfx, char *msg)
+{
+#endif
+pfx = pfx;
+printf("Berkeley DB error: %s\n", msg);
+}
+#endif
+
+
+
+/*************************************************
+*          Interpret escape sequence             *
+*************************************************/
+
+/* This function is copied from the main Exim code.
+
+Arguments:
+  pp       points a pointer to the initiating "\" in the string;
+           the pointer gets updated to point to the final character
+Returns:   the value of the character escape
+*/
+
+int
+string_interpret_escape(uschar **pp)
+{
+int ch;
+uschar *p = *pp;
+ch = *(++p);
+if (isdigit(ch) && ch != '8' && ch != '9')
+  {
+  ch -= '0';
+  if (isdigit(p[1]) && p[1] != '8' && p[1] != '9')
+    {
+    ch = ch * 8 + *(++p) - '0';
+    if (isdigit(p[1]) && p[1] != '8' && p[1] != '9')
+      ch = ch * 8 + *(++p) - '0';
+    }
+  }
+else switch(ch)
+  {
+  case 'n':  ch = '\n'; break;
+  case 'r':  ch = '\r'; break;
+  case 't':  ch = '\t'; break;
+  case 'x':
+  ch = 0;
+  if (isxdigit(p[1]))
+    {
+    ch = ch * 16 +
+      Ustrchr(hex_digits, tolower(*(++p))) - hex_digits;
+    if (isxdigit(p[1])) ch = ch * 16 +
+      Ustrchr(hex_digits, tolower(*(++p))) - hex_digits;
+    }
+  break;
+  }
+*pp = p;
+return ch;
+}
+
+
+/*************************************************
+*               Main Program                     *
+*************************************************/
+
+int main(int argc, char **argv)
+{
+int started;
+int count = 0;
+int dupcount = 0;
+int yield = 0;
+int arg = 1;
+int add_zero = 1;
+BOOL lowercase = TRUE;
+BOOL warn = TRUE;
+BOOL duperr = TRUE;
+BOOL lastdup = FALSE;
+#if !defined (USE_DB) && !defined(USE_TDB) && !defined(USE_GDBM)
+int is_db = 0;
+struct stat statbuf;
+#endif
+FILE *f;
+EXIM_DB *d;
+EXIM_DATUM key, content;
+uschar *bptr;
+uschar  keybuffer[256];
+uschar  temp_dbmname[512];
+uschar  real_dbmname[512];
+uschar *buffer = malloc(max_outsize);
+uschar *line = malloc(max_insize);
+
+while (argc > 1)
+  {
+  if      (Ustrcmp(argv[arg], "-nolc") == 0)     lowercase = FALSE;
+  else if (Ustrcmp(argv[arg], "-nowarn") == 0)   warn = FALSE;
+  else if (Ustrcmp(argv[arg], "-lastdup") == 0)  lastdup = TRUE;
+  else if (Ustrcmp(argv[arg], "-noduperr") == 0) duperr = FALSE;
+  else if (Ustrcmp(argv[arg], "-nozero") == 0)   add_zero = 0;
+  else break;
+  arg++;
+  argc--;
+  }
+
+if (argc != 3)
+  {
+  printf("usage: exim_dbmbuild [-nolc] <source file> <dbm base name>\n");
+  exit(1);
+  }
+
+if (Ustrcmp(argv[arg], "-") == 0) f = stdin; else
+  {
+  f = fopen(argv[arg], "rb");
+  if (f == NULL)
+    {
+    printf("exim_dbmbuild: unable to open %s: %s\n", argv[arg], strerror(errno));
+    exit(1);
+    }
+  }
+
+/* By default Berkeley db does not put extensions on... which
+can be painful! */
+
+#if defined(USE_DB) || defined(USE_TDB) || defined(USE_GDBM)
+if (Ustrcmp(argv[arg], argv[arg+1]) == 0)
+  {
+  printf("exim_dbmbuild: input and output filenames are the same\n");
+  exit(1);
+  }
+#endif
+
+/* Check length of filename; allow for adding .dbmbuild_temp and .db or
+.dir/.pag later. */
+
+if (strlen(argv[arg+1]) > sizeof(temp_dbmname) - 20)
+  {
+  printf("exim_dbmbuild: output filename is ridiculously long\n");
+  exit(1);
+  }
+
+Ustrcpy(temp_dbmname, argv[arg+1]);
+Ustrcat(temp_dbmname, ".dbmbuild_temp");
+
+/* It is apparently necessary to open with O_RDWR for this to work
+with gdbm-1.7.3, though no reading is actually going to be done. */
+
+EXIM_DBOPEN(temp_dbmname, O_RDWR|O_CREAT|O_EXCL, 0644, &d);
+
+if (d == NULL)
+  {
+  printf("exim_dbmbuild: unable to create %s: %s\n", temp_dbmname,
+    strerror(errno));
+  (void)fclose(f);
+  exit(1);
+  }
+
+/* Unless using native db calls, see if we have created <name>.db; if not,
+assume .dir & .pag */
+
+#if !defined(USE_DB) && !defined(USE_TDB) && !defined(USE_GDBM)
+sprintf(CS real_dbmname, "%s.db", temp_dbmname);
+is_db = Ustat(real_dbmname, &statbuf) == 0;
+#endif
+
+/* Now do the business */
+
+bptr = buffer;
+started = 0;
+
+while (Ufgets(line, max_insize, f) != NULL)
+  {
+  uschar *p;
+  int len = Ustrlen(line);
+
+  p = line + len;
+
+  if (len >= max_insize - 1 && p[-1] != '\n')
+    {
+    printf("Overlong line read: max permitted length is %d\n", max_insize - 1);
+    yield = 2;
+    goto TIDYUP;
+    }
+
+  if (line[0] == '#') continue;
+  while (p > line && isspace(p[-1])) p--;
+  *p = 0;
+  if (line[0] == 0) continue;
+
+  /* A continuation line is valid only if there was a previous first
+  line. */
+
+  if (isspace(line[0]))
+    {
+    uschar *s = line;
+    if (!started)
+      {
+      printf("Unexpected continuation line ignored\n%s\n\n", line);
+      continue;
+      }
+    while (isspace(*s)) s++;
+    *(--s) = ' ';
+
+    if (bptr - buffer + p - s >= max_outsize - 1)
+      {
+      printf("Continued set of lines is too long: max permitted length is %d\n",
+        max_outsize -1);
+      yield = 2;
+      goto TIDYUP;
+      }
+
+    Ustrcpy(bptr, s);
+    bptr += p - s;
+    }
+
+  /* A first line must have a name followed by a colon or whitespace or
+  end of line, but first finish with a previous line. The key is lower
+  cased by default - this is what the newaliases program for sendmail does.
+  However, there's an option not to do this. */
+
+  else
+    {
+    int i, rc;
+    uschar *s = line;
+    uschar *keystart;
+
+    if (started)
+      {
+      EXIM_DATUM_INIT(content);
+      EXIM_DATUM_DATA(content) = CS buffer;
+      EXIM_DATUM_SIZE(content) = bptr - buffer + add_zero;
+
+      switch(rc = EXIM_DBPUTB(d, key, content))
+        {
+        case EXIM_DBPUTB_OK:
+        count++;
+        break;
+
+        case EXIM_DBPUTB_DUP:
+        if (warn) fprintf(stderr, "** Duplicate key \"%s\"\n",
+          keybuffer);
+        dupcount++;
+        if(duperr) yield = 1;
+        if (lastdup) EXIM_DBPUT(d, key, content);
+        break;
+
+        default:
+        fprintf(stderr, "Error %d while writing key %s: errno=%d\n", rc,
+          keybuffer, errno);
+        yield = 2;
+        goto TIDYUP;
+        }
+
+      bptr = buffer;
+      }
+
+    EXIM_DATUM_INIT(key);
+    EXIM_DATUM_DATA(key) = CS keybuffer;
+
+    /* Deal with quoted keys. Escape sequences always make one character
+    out of several, so we can re-build in place. */
+
+    if (*s == '\"')
+      {
+      uschar *t = s++;
+      keystart = t;
+      while (*s != 0 && *s != '\"')
+        {
+        if (*s == '\\') *t++ = string_interpret_escape(&s);
+          else *t++ = *s;
+        s++;
+        }
+      if (*s != 0) s++;               /* Past terminating " */
+      EXIM_DATUM_SIZE(key) = t - keystart + add_zero;
+      }
+    else
+      {
+      keystart = s;
+      while (*s != 0 && *s != ':' && !isspace(*s)) s++;
+      EXIM_DATUM_SIZE(key) = s - keystart + add_zero;
+      }
+
+    if (EXIM_DATUM_SIZE(key) > 256)
+      {
+      printf("Keys longer than 255 characters cannot be handled\n");
+      started = 0;
+      yield = 2;
+      goto TIDYUP;
+      }
+
+    if (lowercase)
+      {
+      for (i = 0; i < EXIM_DATUM_SIZE(key) - add_zero; i++)
+        keybuffer[i] = tolower(keystart[i]);
+      }
+    else
+      {
+      for (i = 0; i < EXIM_DATUM_SIZE(key) - add_zero; i++)
+        keybuffer[i] = keystart[i];
+      }
+
+    keybuffer[i] = 0;
+    started = 1;
+
+    while (isspace(*s))s++;
+    if (*s == ':')
+      {
+      s++;
+      while (isspace(*s))s++;
+      }
+    if (*s != 0)
+      {
+      Ustrcpy(bptr, s);
+      bptr += p - s;
+      }
+    else buffer[0] = 0;
+    }
+  }
+
+if (started)
+  {
+  int rc;
+  EXIM_DATUM_INIT(content);
+  EXIM_DATUM_DATA(content) = CS buffer;
+  EXIM_DATUM_SIZE(content) = bptr - buffer + add_zero;
+
+  switch(rc = EXIM_DBPUTB(d, key, content))
+    {
+    case EXIM_DBPUTB_OK:
+    count++;
+    break;
+
+    case EXIM_DBPUTB_DUP:
+    if (warn) fprintf(stderr, "** Duplicate key \"%s\"\n", keybuffer);
+    dupcount++;
+    if (duperr) yield = 1;
+    if (lastdup) EXIM_DBPUT(d, key, content);
+    break;
+
+    default:
+    fprintf(stderr, "Error %d while writing key %s: errno=%d\n", rc,
+      keybuffer, errno);
+    yield = 2;
+    break;
+    }
+  }
+
+/* Close files, rename or abandon the temporary files, and exit */
+
+TIDYUP:
+
+EXIM_DBCLOSE(d);
+(void)fclose(f);
+
+/* If successful, output the number of entries and rename the temporary
+files. */
+
+if (yield == 0 || yield == 1)
+  {
+  printf("%d entr%s written\n", count, (count == 1)? "y" : "ies");
+  if (dupcount > 0)
+    {
+    printf("%d duplicate key%s \n", dupcount, (dupcount > 1)? "s" : "");
+    }
+
+  #if defined(USE_DB) || defined(USE_TDB) || defined(USE_GDBM)
+  Ustrcpy(real_dbmname, temp_dbmname);
+  Ustrcpy(buffer, argv[arg+1]);
+  if (Urename(real_dbmname, buffer) != 0)
+    {
+    printf("Unable to rename %s as %s\n", real_dbmname, buffer);
+    return 1;
+    }
+  #else
+
+  /* Rename a single .db file */
+
+  if (is_db)
+    {
+    sprintf(CS real_dbmname, "%s.db", temp_dbmname);
+    sprintf(CS buffer, "%s.db", argv[arg+1]);
+    if (Urename(real_dbmname, buffer) != 0)
+      {
+      printf("Unable to rename %s as %s\n", real_dbmname, buffer);
+      return 1;
+      }
+    }
+
+  /* Rename .dir and .pag files */
+
+  else
+    {
+    sprintf(CS real_dbmname, "%s.dir", temp_dbmname);
+    sprintf(CS buffer, "%s.dir", argv[arg+1]);
+    if (Urename(real_dbmname, buffer) != 0)
+      {
+      printf("Unable to rename %s as %s\n", real_dbmname, buffer);
+      return 1;
+      }
+
+    sprintf(CS real_dbmname, "%s.pag", temp_dbmname);
+    sprintf(CS buffer, "%s.pag", argv[arg+1]);
+    if (Urename(real_dbmname, buffer) != 0)
+      {
+      printf("Unable to rename %s as %s\n", real_dbmname, buffer);
+      return 1;
+      }
+    }
+
+  #endif /* USE_DB || USE_TDB || USE_GDBM */
+  }
+
+/* Otherwise unlink the temporary files. */
+
+else
+  {
+  printf("dbmbuild abandoned\n");
+  #if defined(USE_DB) || defined(USE_TDB) || defined(USE_GDBM)
+  Uunlink(temp_dbmname);
+  #else
+  if (is_db)
+    {
+    sprintf(CS real_dbmname, "%s.db", temp_dbmname);
+    Uunlink(real_dbmname);
+    }
+  else
+    {
+    sprintf(CS real_dbmname, "%s.dir", temp_dbmname);
+    Uunlink(real_dbmname);
+    sprintf(CS real_dbmname, "%s.pag", temp_dbmname);
+    Uunlink(real_dbmname);
+    }
+  #endif /* USE_DB || USE_TDB */
+  }
+
+return yield;
+}
+
+/* End of exim_dbmbuild.c */
diff --git a/src/exim_dbutil.c b/src/exim_dbutil.c
new file mode 100644 (file)
index 0000000..124303a
--- /dev/null
@@ -0,0 +1,1358 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+
+/* This single source file is used to compile three utility programs for
+maintaining Exim hints databases.
+
+  exim_dumpdb     dumps out the contents
+  exim_fixdb      patches the database (really for Exim maintenance/testing)
+  exim_tidydb     removed obsolete data
+
+In all cases, the first argument is the name of the spool directory. The second
+argument is the name of the database file. The available names are:
+
+  retry:      retry delivery information
+  misc:       miscellaneous hints data
+  wait-<t>:   message waiting information; <t> is a transport name
+  callout:    callout verification cache
+
+There are a number of common subroutines, followed by three main programs,
+whose inclusion is controlled by -D on the compilation command. */
+
+
+#include "exim.h"
+
+
+/* Identifiers for the different database types. */
+
+#define type_retry     1
+#define type_wait      2
+#define type_misc      3
+#define type_callout   4
+#define type_ratelimit 5
+
+
+/* This is used by our cut-down dbfn_open(). */
+
+uschar *spool_directory;
+
+
+
+/*************************************************
+*         Berkeley DB error callback             *
+*************************************************/
+
+/* For Berkeley DB >= 2, we can define a function to be called in case of DB
+errors. This should help with debugging strange DB problems, e.g. getting "File
+exists" when you try to open a db file. The API changed at release 4.3. */
+
+#if defined(USE_DB) && defined(DB_VERSION_STRING)
+void
+#if DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 3)
+dbfn_bdb_error_callback(const DB_ENV *dbenv, const char *pfx, const char *msg)
+{
+dbenv = dbenv;
+#else
+dbfn_bdb_error_callback(const char *pfx, char *msg)
+{
+#endif
+pfx = pfx;
+printf("Berkeley DB error: %s\n", msg);
+}
+#endif
+
+
+
+/*************************************************
+*              SIGALRM handler                   *
+*************************************************/
+
+SIGNAL_BOOL sigalrm_seen;
+
+void
+sigalrm_handler(int sig)
+{
+sig = sig;            /* Keep picky compilers happy */
+sigalrm_seen = 1;
+}
+
+
+
+/*************************************************
+*        Output usage message and exit           *
+*************************************************/
+
+static void
+usage(uschar *name, uschar *options)
+{
+printf("Usage: exim_%s%s  <spool-directory> <database-name>\n", name, options);
+printf("  <database-name> = retry | misc | wait-<transport-name> | callout | ratelimit\n");
+exit(1);
+}
+
+
+
+/*************************************************
+*           Sort out the command arguments       *
+*************************************************/
+
+/* This function checks that there are exactly 2 arguments, and checks the
+second of them to be sure it is a known database name. */
+
+static int
+check_args(int argc, uschar **argv, uschar *name, uschar *options)
+{
+if (argc == 3)
+  {
+  if (Ustrcmp(argv[2], "retry") == 0) return type_retry;
+  if (Ustrcmp(argv[2], "misc") == 0) return type_misc;
+  if (Ustrncmp(argv[2], "wait-", 5) == 0) return type_wait;
+  if (Ustrcmp(argv[2], "callout") == 0) return type_callout;
+  if (Ustrcmp(argv[2], "ratelimit") == 0) return type_ratelimit;
+  }
+usage(name, options);
+return -1;              /* Never obeyed */
+}
+
+
+
+/*************************************************
+*         Handle attempts to write the log       *
+*************************************************/
+
+/* The message gets written to stderr when log_write() is called from a
+utility. The message always gets '\n' added on the end of it. These calls come
+from modules such as store.c when things go drastically wrong (e.g. malloc()
+failing). In normal use they won't get obeyed.
+
+Arguments:
+  selector  not relevant when running a utility
+  flags     not relevant when running a utility
+  format    a printf() format
+  ...       arguments for format
+
+Returns:    nothing
+*/
+
+void
+log_write(unsigned int selector, int flags, const char *format, ...)
+{
+va_list ap;
+va_start(ap, format);
+vfprintf(stderr, format, ap);
+fprintf(stderr, "\n");
+va_end(ap);
+selector = selector;     /* Keep picky compilers happy */
+flags = flags;
+}
+
+
+
+/*************************************************
+*        Format a time value for printing        *
+*************************************************/
+
+static uschar time_buffer[sizeof("09-xxx-1999 hh:mm:ss  ")];
+
+uschar *
+print_time(time_t t)
+{
+struct tm *tmstr = localtime(&t);
+Ustrftime(time_buffer, sizeof(time_buffer), "%d-%b-%Y %H:%M:%S", tmstr);
+return time_buffer;
+}
+
+
+
+/*************************************************
+*        Format a cache value for printing       *
+*************************************************/
+
+uschar *
+print_cache(int value)
+{
+return (value == ccache_accept)? US"accept" :
+       (value == ccache_reject)? US"reject" :
+       US"unknown";
+}
+
+
+#ifdef EXIM_FIXDB
+/*************************************************
+*                Read time value                 *
+*************************************************/
+
+static time_t
+read_time(uschar *s)
+{
+uschar *t = s;
+int field = 0;
+int value;
+time_t now = time(NULL);
+struct tm *tm = localtime(&now);
+
+tm->tm_sec = 0;
+tm->tm_isdst = -1;
+
+for (t = s + Ustrlen(s) - 1; t >= s; t--)
+  {
+  if (*t == ':') continue;
+  if (!isdigit((uschar)*t)) return -1;
+
+  value = *t - '0';
+  if (--t >= s)
+    {
+    if (!isdigit((uschar)*t)) return -1;
+    value = value + (*t - '0')*10;
+    }
+
+  switch (field++)
+    {
+    case 0: tm->tm_min = value; break;
+    case 1: tm->tm_hour = value; break;
+    case 2: tm->tm_mday = value; break;
+    case 3: tm->tm_mon = value - 1; break;
+    case 4: tm->tm_year = (value < 90)? value + 100 : value; break;
+    default: return -1;
+    }
+  }
+
+return mktime(tm);
+}
+#endif  /* EXIM_FIXDB */
+
+
+
+/*************************************************
+*       Open and lock a database file            *
+*************************************************/
+
+/* This is a cut-down version from the function in dbfn.h that Exim itself
+uses. We assume the database exists, and therefore give up if we cannot open
+the lock file.
+
+Arguments:
+  name     The single-component name of one of Exim's database files.
+  flags    O_RDONLY or O_RDWR
+  dbblock  Points to an open_db block to be filled in.
+  lof      Unused.
+
+Returns:   NULL if the open failed, or the locking failed.
+           On success, dbblock is returned. This contains the dbm pointer and
+           the fd of the locked lock file.
+*/
+
+open_db *
+dbfn_open(uschar *name, int flags, open_db *dbblock, BOOL lof)
+{
+int rc;
+struct flock lock_data;
+BOOL read_only = flags == O_RDONLY;
+uschar buffer[256];
+
+/* The first thing to do is to open a separate file on which to lock. This
+ensures that Exim has exclusive use of the database before it even tries to
+open it. If there is a database, there should be a lock file in existence. */
+
+sprintf(CS buffer, "%s/db/%s.lockfile", spool_directory, name);
+
+dbblock->lockfd = Uopen(buffer, flags, 0);
+if (dbblock->lockfd < 0)
+  {
+  printf("** Failed to open database lock file %s: %s\n", buffer,
+    strerror(errno));
+  return NULL;
+  }
+
+/* Now we must get a lock on the opened lock file; do this with a blocking
+lock that times out. */
+
+lock_data.l_type = read_only? F_RDLCK : F_WRLCK;
+lock_data.l_whence = lock_data.l_start = lock_data.l_len = 0;
+
+sigalrm_seen = FALSE;
+os_non_restarting_signal(SIGALRM, sigalrm_handler);
+alarm(EXIMDB_LOCK_TIMEOUT);
+rc = fcntl(dbblock->lockfd, F_SETLKW, &lock_data);
+alarm(0);
+
+if (sigalrm_seen) errno = ETIMEDOUT;
+if (rc < 0)
+  {
+  printf("** Failed to get %s lock for %s: %s",
+    ((flags & O_RDONLY) != 0)? "read" : "write", buffer,
+    (errno == ETIMEDOUT)? "timed out" : strerror(errno));
+  (void)close(dbblock->lockfd);
+  return NULL;
+  }
+
+/* At this point we have an opened and locked separate lock file, that is,
+exclusive access to the database, so we can go ahead and open it. */
+
+sprintf(CS buffer, "%s/db/%s", spool_directory, name);
+EXIM_DBOPEN(buffer, flags, 0, &(dbblock->dbptr));
+
+if (dbblock->dbptr == NULL)
+  {
+  printf("** Failed to open DBM file %s for %s:\n   %s%s\n", buffer,
+    read_only? "reading" : "writing", strerror(errno),
+    #ifdef USE_DB
+    " (or Berkeley DB error while opening)"
+    #else
+    ""
+    #endif
+    );
+  (void)close(dbblock->lockfd);
+  return NULL;
+  }
+
+return dbblock;
+}
+
+
+
+
+/*************************************************
+*         Unlock and close a database file       *
+*************************************************/
+
+/* Closing a file automatically unlocks it, so after closing the database, just
+close the lock file.
+
+Argument: a pointer to an open database block
+Returns:  nothing
+*/
+
+void
+dbfn_close(open_db *dbblock)
+{
+EXIM_DBCLOSE(dbblock->dbptr);
+(void)close(dbblock->lockfd);
+}
+
+
+
+
+/*************************************************
+*             Read from database file            *
+*************************************************/
+
+/* Passing back the pointer unchanged is useless, because there is no guarantee
+of alignment. Since all the records used by Exim need to be properly aligned to
+pick out the timestamps, etc., do the copying centrally here.
+
+Arguments:
+  dbblock   a pointer to an open database block
+  key       the key of the record to be read
+  length    where to put the length (or NULL if length not wanted)
+
+Returns: a pointer to the retrieved record, or
+         NULL if the record is not found
+*/
+
+void *
+dbfn_read_with_length(open_db *dbblock, uschar *key, int *length)
+{
+void *yield;
+EXIM_DATUM key_datum, result_datum;
+
+EXIM_DATUM_INIT(key_datum);         /* Some DBM libraries require the datum */
+EXIM_DATUM_INIT(result_datum);      /* to be cleared before use. */
+EXIM_DATUM_DATA(key_datum) = CS key;
+EXIM_DATUM_SIZE(key_datum) = Ustrlen(key) + 1;
+
+if (!EXIM_DBGET(dbblock->dbptr, key_datum, result_datum)) return NULL;
+
+yield = store_get(EXIM_DATUM_SIZE(result_datum));
+memcpy(yield, EXIM_DATUM_DATA(result_datum), EXIM_DATUM_SIZE(result_datum));
+if (length != NULL) *length = EXIM_DATUM_SIZE(result_datum);
+
+EXIM_DATUM_FREE(result_datum);    /* Some DBM libs require freeing */
+return yield;
+}
+
+
+
+#if defined(EXIM_TIDYDB) || defined(EXIM_FIXDB)
+
+/*************************************************
+*             Write to database file             *
+*************************************************/
+
+/*
+Arguments:
+  dbblock   a pointer to an open database block
+  key       the key of the record to be written
+  ptr       a pointer to the record to be written
+  length    the length of the record to be written
+
+Returns:    the yield of the underlying dbm or db "write" function. If this
+            is dbm, the value is zero for OK.
+*/
+
+int
+dbfn_write(open_db *dbblock, uschar *key, void *ptr, int length)
+{
+EXIM_DATUM key_datum, value_datum;
+dbdata_generic *gptr = (dbdata_generic *)ptr;
+gptr->time_stamp = time(NULL);
+
+EXIM_DATUM_INIT(key_datum);         /* Some DBM libraries require the datum */
+EXIM_DATUM_INIT(value_datum);       /* to be cleared before use. */
+EXIM_DATUM_DATA(key_datum) = CS key;
+EXIM_DATUM_SIZE(key_datum) = Ustrlen(key) + 1;
+EXIM_DATUM_DATA(value_datum) = CS ptr;
+EXIM_DATUM_SIZE(value_datum) = length;
+return EXIM_DBPUT(dbblock->dbptr, key_datum, value_datum);
+}
+
+
+
+/*************************************************
+*           Delete record from database file     *
+*************************************************/
+
+/*
+Arguments:
+  dbblock    a pointer to an open database block
+  key        the key of the record to be deleted
+
+Returns: the yield of the underlying dbm or db "delete" function.
+*/
+
+int
+dbfn_delete(open_db *dbblock, uschar *key)
+{
+EXIM_DATUM key_datum;
+EXIM_DATUM_INIT(key_datum);         /* Some DBM libraries require clearing */
+EXIM_DATUM_DATA(key_datum) = CS key;
+EXIM_DATUM_SIZE(key_datum) = Ustrlen(key) + 1;
+return EXIM_DBDEL(dbblock->dbptr, key_datum);
+}
+
+#endif  /* EXIM_TIDYDB || EXIM_FIXDB */
+
+
+
+#if defined(EXIM_DUMPDB) || defined(EXIM_TIDYDB)
+/*************************************************
+*         Scan the keys of a database file       *
+*************************************************/
+
+/*
+Arguments:
+  dbblock  a pointer to an open database block
+  start    TRUE if starting a new scan
+           FALSE if continuing with the current scan
+  cursor   a pointer to a pointer to a cursor anchor, for those dbm libraries
+           that use the notion of a cursor
+
+Returns:   the next record from the file, or
+           NULL if there are no more
+*/
+
+uschar *
+dbfn_scan(open_db *dbblock, BOOL start, EXIM_CURSOR **cursor)
+{
+EXIM_DATUM key_datum, value_datum;
+uschar *yield;
+value_datum = value_datum;    /* dummy; not all db libraries use this */
+
+/* Some dbm require an initialization */
+
+if (start) EXIM_DBCREATE_CURSOR(dbblock->dbptr, cursor);
+
+EXIM_DATUM_INIT(key_datum);         /* Some DBM libraries require the datum */
+EXIM_DATUM_INIT(value_datum);       /* to be cleared before use. */
+
+yield = (EXIM_DBSCAN(dbblock->dbptr, key_datum, value_datum, start, *cursor))?
+  US EXIM_DATUM_DATA(key_datum) : NULL;
+
+/* Some dbm require a termination */
+
+if (!yield) EXIM_DBDELETE_CURSOR(*cursor);
+return yield;
+}
+#endif  /* EXIM_DUMPDB || EXIM_TIDYDB */
+
+
+
+#ifdef EXIM_DUMPDB
+/*************************************************
+*           The exim_dumpdb main program         *
+*************************************************/
+
+int
+main(int argc, char **cargv)
+{
+int dbdata_type = 0;
+int yield = 0;
+open_db dbblock;
+open_db *dbm;
+EXIM_CURSOR *cursor;
+uschar **argv = USS cargv;
+uschar *key;
+uschar keybuffer[1024];
+
+/* Check the arguments, and open the database */
+
+dbdata_type = check_args(argc, argv, US"dumpdb", US"");
+spool_directory = argv[1];
+dbm = dbfn_open(argv[2], O_RDONLY, &dbblock, FALSE);
+if (dbm == NULL) exit(1);
+
+/* Scan the file, formatting the information for each entry. Note
+that data is returned in a malloc'ed block, in order that it be
+correctly aligned. */
+
+key = dbfn_scan(dbm, TRUE, &cursor);
+while (key != NULL)
+  {
+  dbdata_retry *retry;
+  dbdata_wait *wait;
+  dbdata_callout_cache *callout;
+  dbdata_ratelimit *ratelimit;
+  dbdata_ratelimit_unique *rate_unique;
+  int count_bad = 0;
+  int i, length;
+  uschar *t;
+  uschar name[MESSAGE_ID_LENGTH + 1];
+  void *value;
+
+  /* Keep a copy of the key separate, as in some DBM's the pointer is into data
+  which might change. */
+
+  if (Ustrlen(key) > sizeof(keybuffer) - 1)
+    {
+    printf("**** Overlong key encountered: %s\n", key);
+    return 1;
+    }
+  Ustrcpy(keybuffer, key);
+  value = dbfn_read_with_length(dbm, keybuffer, &length);
+
+  if (value == NULL)
+    fprintf(stderr, "**** Entry \"%s\" was in the key scan, but the record "
+                    "was not found in the file - something is wrong!\n",
+      CS keybuffer);
+  else
+    {
+    /* Note: don't use print_time more than once in one statement, since
+    it uses a single buffer. */
+
+    switch(dbdata_type)
+      {
+      case type_retry:
+      retry = (dbdata_retry *)value;
+      printf("  %s %d %d %s\n%s  ", keybuffer, retry->basic_errno,
+        retry->more_errno, retry->text,
+        print_time(retry->first_failed));
+      printf("%s  ", print_time(retry->last_try));
+      printf("%s %s\n", print_time(retry->next_try),
+        (retry->expired)? "*" : "");
+      break;
+
+      case type_wait:
+      wait = (dbdata_wait *)value;
+      printf("%s ", keybuffer);
+      t = wait->text;
+      name[MESSAGE_ID_LENGTH] = 0;
+
+      if (wait->count > WAIT_NAME_MAX)
+        {
+        fprintf(stderr,
+          "**** Data for %s corrupted\n  count=%d=0x%x max=%d\n",
+          CS keybuffer, wait->count, wait->count, WAIT_NAME_MAX);
+        wait->count = WAIT_NAME_MAX;
+        yield = count_bad = 1;
+        }
+      for (i = 1; i <= wait->count; i++)
+        {
+        Ustrncpy(name, t, MESSAGE_ID_LENGTH);
+        if (count_bad && name[0] == 0) break;
+        if (Ustrlen(name) != MESSAGE_ID_LENGTH ||
+            Ustrspn(name, "0123456789"
+                          "abcdefghijklmnopqrstuvwxyz"
+                          "ABCDEFGHIJKLMNOPQRSTUVWXYZ-") != MESSAGE_ID_LENGTH)
+          {
+          int j;
+          fprintf(stderr,
+            "**** Data for %s corrupted: bad character in message id\n",
+            CS keybuffer);
+          for (j = 0; j < MESSAGE_ID_LENGTH; j++)
+            fprintf(stderr, "%02x ", name[j]);
+          fprintf(stderr, "\n");
+          yield = 1;
+          break;
+          }
+        printf("%s ", name);
+        t += MESSAGE_ID_LENGTH;
+        }
+      printf("\n");
+      break;
+
+      case type_misc:
+      printf("%s %s\n", print_time(((dbdata_generic *)value)->time_stamp),
+        keybuffer);
+      break;
+
+      case type_callout:
+      callout = (dbdata_callout_cache *)value;
+
+      /* New-style address record */
+
+      if (length == sizeof(dbdata_callout_cache_address))
+        {
+        printf("%s %s callout=%s\n",
+          print_time(((dbdata_generic *)value)->time_stamp),
+          keybuffer,
+          print_cache(callout->result));
+        }
+
+      /* New-style domain record */
+
+      else if (length == sizeof(dbdata_callout_cache))
+        {
+        printf("%s %s callout=%s postmaster=%s",
+          print_time(((dbdata_generic *)value)->time_stamp),
+          keybuffer,
+          print_cache(callout->result),
+          print_cache(callout->postmaster_result));
+        if (callout->postmaster_result != ccache_unknown)
+          printf(" (%s)", print_time(callout->postmaster_stamp));
+        printf(" random=%s", print_cache(callout->random_result));
+        if (callout->random_result != ccache_unknown)
+          printf(" (%s)", print_time(callout->random_stamp));
+        printf("\n");
+        }
+
+      /* Old-style domain record, without separate timestamps. This code can
+      eventually be thrown away, say in 5 years' time (it's now Feb 2003). */
+
+      else
+        {
+        printf("%s %s callout=%s postmaster=%s random=%s\n",
+          print_time(((dbdata_generic *)value)->time_stamp),
+          keybuffer,
+          print_cache(callout->result),
+          print_cache(callout->postmaster_result),
+          print_cache(callout->random_result));
+        }
+
+      break;
+
+      case type_ratelimit:
+      if (Ustrstr(key, "/unique/") != NULL && length >= sizeof(*rate_unique))
+        {
+        ratelimit = (dbdata_ratelimit *)value;
+        rate_unique = (dbdata_ratelimit_unique *)value;
+        printf("%s.%06d rate: %10.3f epoch: %s size: %u key: %s\n",
+          print_time(ratelimit->time_stamp),
+          ratelimit->time_usec, ratelimit->rate,
+          print_time(rate_unique->bloom_epoch), rate_unique->bloom_size,
+          keybuffer);
+        }
+      else
+        {
+        ratelimit = (dbdata_ratelimit *)value;
+        printf("%s.%06d rate: %10.3f key: %s\n",
+          print_time(ratelimit->time_stamp),
+          ratelimit->time_usec, ratelimit->rate,
+          keybuffer);
+        }
+      break;
+      }
+    store_reset(value);
+    }
+  key = dbfn_scan(dbm, FALSE, &cursor);
+  }
+
+dbfn_close(dbm);
+return yield;
+}
+
+#endif  /* EXIM_DUMPDB */
+
+
+
+
+#ifdef EXIM_FIXDB
+/*************************************************
+*           The exim_fixdb main program          *
+*************************************************/
+
+/* In order not to hold the database lock any longer than is necessary, each
+operation on the database uses a separate open/close call. This is expensive,
+but then using this utility is not expected to be very common. Its main use is
+to provide a way of patching up hints databases in order to run tests.
+
+Syntax of commands:
+
+(1) <record name>
+    This causes the data from the given record to be displayed, or "not found"
+    to be output. Note that in the retry database, destination names are
+    preceded by R: or T: for router or transport retry info.
+
+(2) <record name> d
+    This causes the given record to be deleted or "not found" to be output.
+
+(3) <record name> <field number> <value>
+    This sets the given value into the given field, identified by a number
+    which is output by the display command. Not all types of record can
+    be changed.
+
+(4) q
+    This exits from exim_fixdb.
+
+If the record name is omitted from (2) or (3), the previously used record name
+is re-used. */
+
+
+int main(int argc, char **cargv)
+{
+int dbdata_type;
+uschar **argv = USS cargv;
+uschar buffer[256];
+uschar name[256];
+void *reset_point = store_get(0);
+
+name[0] = 0;  /* No name set */
+
+/* Sort out the database type, verify what we are working on and then process
+user requests */
+
+dbdata_type = check_args(argc, argv, US"fixdb", US"");
+printf("Modifying Exim hints database %s/db/%s\n", argv[1], argv[2]);
+
+for(;;)
+  {
+  open_db dbblock;
+  open_db *dbm;
+  void *record;
+  dbdata_retry *retry;
+  dbdata_wait *wait;
+  dbdata_callout_cache *callout;
+  dbdata_ratelimit *ratelimit;
+  dbdata_ratelimit_unique *rate_unique;
+  int i, oldlength;
+  uschar *t;
+  uschar field[256], value[256];
+
+  store_reset(reset_point);
+
+  printf("> ");
+  if (Ufgets(buffer, 256, stdin) == NULL) break;
+
+  buffer[Ustrlen(buffer)-1] = 0;
+  field[0] = value[0] = 0;
+
+  /* If the buffer contains just one digit, or just consists of "d", use the
+  previous name for an update. */
+
+  if ((isdigit((uschar)buffer[0]) && (buffer[1] == ' ' || buffer[1] == '\0'))
+       || Ustrcmp(buffer, "d") == 0)
+    {
+    if (name[0] == 0)
+      {
+      printf("No previous record name is set\n");
+      continue;
+      }
+    (void)sscanf(CS buffer, "%s %s", field, value);
+    }
+  else
+    {
+    name[0] = 0;
+    (void)sscanf(CS buffer, "%s %s %s", name, field, value);
+    }
+
+  /* Handle an update request */
+
+  if (field[0] != 0)
+    {
+    int verify = 1;
+    spool_directory = argv[1];
+    dbm = dbfn_open(argv[2], O_RDWR, &dbblock, FALSE);
+    if (dbm == NULL) continue;
+
+    if (Ustrcmp(field, "d") == 0)
+      {
+      if (value[0] != 0) printf("unexpected value after \"d\"\n");
+        else printf("%s\n", (dbfn_delete(dbm, name) < 0)?
+          "not found" : "deleted");
+      dbfn_close(dbm);
+      continue;
+      }
+
+    else if (isdigit((uschar)field[0]))
+      {
+      int fieldno = Uatoi(field);
+      if (value[0] == 0)
+        {
+        printf("value missing\n");
+        dbfn_close(dbm);
+        continue;
+        }
+      else
+        {
+        record = dbfn_read_with_length(dbm, name, &oldlength);
+        if (record == NULL) printf("not found\n"); else
+          {
+          time_t tt;
+          int length = 0;     /* Stops compiler warning */
+
+          switch(dbdata_type)
+            {
+            case type_retry:
+            retry = (dbdata_retry *)record;
+            length = sizeof(dbdata_retry) + Ustrlen(retry->text);
+
+            switch(fieldno)
+              {
+              case 0:
+              retry->basic_errno = Uatoi(value);
+              break;
+
+              case 1:
+              retry->more_errno = Uatoi(value);
+              break;
+
+              case 2:
+              if ((tt = read_time(value)) > 0) retry->first_failed = tt;
+                else printf("bad time value\n");
+              break;
+
+              case 3:
+              if ((tt = read_time(value)) > 0) retry->last_try = tt;
+                else printf("bad time value\n");
+              break;
+
+              case 4:
+              if ((tt = read_time(value)) > 0) retry->next_try = tt;
+                else printf("bad time value\n");
+              break;
+
+              case 5:
+              if (Ustrcmp(value, "yes") == 0) retry->expired = TRUE;
+              else if (Ustrcmp(value, "no") == 0) retry->expired = FALSE;
+              else printf("\"yes\" or \"no\" expected=n");
+              break;
+
+              default:
+              printf("unknown field number\n");
+              verify = 0;
+              break;
+              }
+            break;
+
+            case type_wait:
+            printf("Can't change contents of wait database record\n");
+            break;
+
+            case type_misc:
+            printf("Can't change contents of misc database record\n");
+            break;
+
+            case type_callout:
+            callout = (dbdata_callout_cache *)record;
+            length = sizeof(dbdata_callout_cache);
+            switch(fieldno)
+              {
+              case 0:
+              callout->result = Uatoi(value);
+              break;
+
+              case 1:
+              callout->postmaster_result = Uatoi(value);
+              break;
+
+              case 2:
+              callout->random_result = Uatoi(value);
+              break;
+
+              default:
+              printf("unknown field number\n");
+              verify = 0;
+              break;
+              }
+            break;
+
+            case type_ratelimit:
+            ratelimit = (dbdata_ratelimit *)record;
+            switch(fieldno)
+              {
+              case 0:
+              if ((tt = read_time(value)) > 0) ratelimit->time_stamp = tt;
+                else printf("bad time value\n");
+              break;
+
+              case 1:
+              ratelimit->time_usec = Uatoi(value);
+              break;
+
+              case 2:
+              ratelimit->rate = Ustrtod(value, NULL);
+              break;
+
+              case 3:
+              if (Ustrstr(name, "/unique/") != NULL
+                && oldlength >= sizeof(dbdata_ratelimit_unique))
+                {
+                rate_unique = (dbdata_ratelimit_unique *)record;
+                if ((tt = read_time(value)) > 0) rate_unique->bloom_epoch = tt;
+                  else printf("bad time value\n");
+                break;
+                }
+              /* else fall through */
+
+              case 4:
+              case 5:
+              if (Ustrstr(name, "/unique/") != NULL
+                && oldlength >= sizeof(dbdata_ratelimit_unique))
+                {
+                /* see acl.c */
+                BOOL seen;
+                unsigned n, hash, hinc;
+                uschar md5sum[16];
+                md5 md5info;
+                md5_start(&md5info);
+                md5_end(&md5info, value, Ustrlen(value), md5sum);
+                hash = md5sum[0] <<  0 | md5sum[1] <<  8
+                     | md5sum[2] << 16 | md5sum[3] << 24;
+                hinc = md5sum[4] <<  0 | md5sum[5] <<  8
+                     | md5sum[6] << 16 | md5sum[7] << 24;
+                rate_unique = (dbdata_ratelimit_unique *)record;
+                seen = TRUE;
+                for (n = 0; n < 8; n++, hash += hinc)
+                  {
+                  int bit = 1 << (hash % 8);
+                  int byte = (hash / 8) % rate_unique->bloom_size;
+                  if ((rate_unique->bloom[byte] & bit) == 0)
+                    {
+                    seen = FALSE;
+                    if (fieldno == 5) rate_unique->bloom[byte] |= bit;
+                    }
+                  }
+                printf("%s %s\n",
+                  seen ? "seen" : fieldno == 5 ? "added" : "unseen", value);
+                break;
+                }
+              /* else fall through */
+
+              default:
+              printf("unknown field number\n");
+              verify = 0;
+              break;
+              }
+            break;
+            }
+
+          dbfn_write(dbm, name, record, oldlength);
+          }
+        }
+      }
+
+    else
+      {
+      printf("field number or d expected\n");
+      verify = 0;
+      }
+
+    dbfn_close(dbm);
+    if (!verify) continue;
+    }
+
+  /* The "name" q causes an exit */
+
+  else if (Ustrcmp(name, "q") == 0) return 0;
+
+  /* Handle a read request, or verify after an update. */
+
+  spool_directory = argv[1];
+  dbm = dbfn_open(argv[2], O_RDONLY, &dbblock, FALSE);
+  if (dbm == NULL) continue;
+
+  record = dbfn_read_with_length(dbm, name, &oldlength);
+  if (record == NULL)
+    {
+    printf("record %s not found\n", name);
+    name[0] = 0;
+    }
+  else
+    {
+    int count_bad = 0;
+    printf("%s\n", CS print_time(((dbdata_generic *)record)->time_stamp));
+    switch(dbdata_type)
+      {
+      case type_retry:
+      retry = (dbdata_retry *)record;
+      printf("0 error number: %d %s\n", retry->basic_errno, retry->text);
+      printf("1 extra data:   %d\n", retry->more_errno);
+      printf("2 first failed: %s\n", print_time(retry->first_failed));
+      printf("3 last try:     %s\n", print_time(retry->last_try));
+      printf("4 next try:     %s\n", print_time(retry->next_try));
+      printf("5 expired:      %s\n", (retry->expired)? "yes" : "no");
+      break;
+
+      case type_wait:
+      wait = (dbdata_wait *)record;
+      t = wait->text;
+      printf("Sequence: %d\n", wait->sequence);
+      if (wait->count > WAIT_NAME_MAX)
+        {
+        printf("**** Data corrupted: count=%d=0x%x max=%d ****\n", wait->count,
+          wait->count, WAIT_NAME_MAX);
+        wait->count = WAIT_NAME_MAX;
+        count_bad = 1;
+        }
+      for (i = 1; i <= wait->count; i++)
+        {
+        Ustrncpy(value, t, MESSAGE_ID_LENGTH);
+        value[MESSAGE_ID_LENGTH] = 0;
+        if (count_bad && value[0] == 0) break;
+        if (Ustrlen(value) != MESSAGE_ID_LENGTH ||
+            Ustrspn(value, "0123456789"
+                          "abcdefghijklmnopqrstuvwxyz"
+                          "ABCDEFGHIJKLMNOPQRSTUVWXYZ-") != MESSAGE_ID_LENGTH)
+          {
+          int j;
+          printf("\n**** Data corrupted: bad character in message id ****\n");
+          for (j = 0; j < MESSAGE_ID_LENGTH; j++)
+            printf("%02x ", value[j]);
+          printf("\n");
+          break;
+          }
+        printf("%s ", value);
+        t += MESSAGE_ID_LENGTH;
+        }
+      printf("\n");
+      break;
+
+      case type_misc:
+      break;
+
+      case type_callout:
+      callout = (dbdata_callout_cache *)record;
+      printf("0 callout:    %s (%d)\n", print_cache(callout->result),
+          callout->result);
+      if (oldlength > sizeof(dbdata_callout_cache_address))
+        {
+        printf("1 postmaster: %s (%d)\n", print_cache(callout->postmaster_result),
+            callout->postmaster_result);
+        printf("2 random:     %s (%d)\n", print_cache(callout->random_result),
+            callout->random_result);
+        }
+      break;
+
+      case type_ratelimit:
+      ratelimit = (dbdata_ratelimit *)record;
+      printf("0 time stamp:  %s\n", print_time(ratelimit->time_stamp));
+      printf("1 fract. time: .%06d\n", ratelimit->time_usec);
+      printf("2 sender rate: % .3f\n", ratelimit->rate);
+      if (Ustrstr(name, "/unique/") != NULL
+       && oldlength >= sizeof(dbdata_ratelimit_unique))
+       {
+       rate_unique = (dbdata_ratelimit_unique *)record;
+       printf("3 filter epoch: %s\n", print_time(rate_unique->bloom_epoch));
+       printf("4 test filter membership\n");
+       printf("5 add element to filter\n");
+       }
+      break;
+      }
+    }
+
+  /* The database is closed after each request */
+
+  dbfn_close(dbm);
+  }
+
+printf("\n");
+return 0;
+}
+
+#endif  /* EXIM_FIXDB */
+
+
+
+#ifdef EXIM_TIDYDB
+/*************************************************
+*           The exim_tidydb main program         *
+*************************************************/
+
+
+/* Utility program to tidy the contents of an exim database file. There is one
+option:
+
+   -t <time>  expiry time for old records - default 30 days
+
+For backwards compatibility, an -f option is recognized and ignored. (It used
+to request a "full" tidy. This version always does the whole job.) */
+
+
+typedef struct key_item {
+  struct key_item *next;
+  uschar key[1];
+} key_item;
+
+
+int main(int argc, char **cargv)
+{
+struct stat statbuf;
+int maxkeep = 30 * 24 * 60 * 60;
+int dbdata_type, i, oldest, path_len;
+key_item *keychain = NULL;
+void *reset_point;
+open_db dbblock;
+open_db *dbm;
+EXIM_CURSOR *cursor;
+uschar **argv = USS cargv;
+uschar buffer[256];
+uschar *key;
+
+/* Scan the options */
+
+for (i = 1; i < argc; i++)
+  {
+  if (argv[i][0] != '-') break;
+  if (Ustrcmp(argv[i], "-f") == 0) continue;
+  if (Ustrcmp(argv[i], "-t") == 0)
+    {
+    uschar *s;
+    s = argv[++i];
+    maxkeep = 0;
+    while (*s != 0)
+      {
+      int value, count;
+      if (!isdigit(*s)) usage(US"tidydb", US" [-t <time>]");
+      (void)sscanf(CS s, "%d%n", &value, &count);
+      s += count;
+      switch (*s)
+        {
+        case 'w': value *= 7;
+        case 'd': value *= 24;
+        case 'h': value *= 60;
+        case 'm': value *= 60;
+        case 's': s++;
+        break;
+        default: usage(US"tidydb", US" [-t <time>]");
+        }
+      maxkeep += value;
+      }
+    }
+  else usage(US"tidydb", US" [-t <time>]");
+  }
+
+/* Adjust argument values and process arguments */
+
+argc -= --i;
+argv += i;
+
+dbdata_type = check_args(argc, argv, US"tidydb", US" [-t <time>]");
+
+/* Compute the oldest keep time, verify what we are doing, and open the
+database */
+
+oldest = time(NULL) - maxkeep;
+printf("Tidying Exim hints database %s/db/%s\n", argv[1], argv[2]);
+
+spool_directory = argv[1];
+dbm = dbfn_open(argv[2], O_RDWR, &dbblock, FALSE);
+if (dbm == NULL) exit(1);
+
+/* Prepare for building file names */
+
+sprintf(CS buffer, "%s/input/", argv[1]);
+path_len = Ustrlen(buffer);
+
+
+/* It appears, by experiment, that it is a bad idea to make changes
+to the file while scanning it. Pity the man page doesn't warn you about that.
+Therefore, we scan and build a list of all the keys. Then we use that to
+read the records and possibly update them. */
+
+key = dbfn_scan(dbm, TRUE, &cursor);
+while (key != NULL)
+  {
+  key_item *k = store_get(sizeof(key_item) + Ustrlen(key));
+  k->next = keychain;
+  keychain = k;
+  Ustrcpy(k->key, key);
+  key = dbfn_scan(dbm, FALSE, &cursor);
+  }
+
+/* Now scan the collected keys and operate on the records, resetting
+the store each time round. */
+
+reset_point = store_get(0);
+
+while (keychain != NULL)
+  {
+  dbdata_generic *value;
+
+  store_reset(reset_point);
+  key = keychain->key;
+  keychain = keychain->next;
+  value = dbfn_read_with_length(dbm, key, NULL);
+
+  /* A continuation record may have been deleted or renamed already, so
+  non-existence is not serious. */
+
+  if (value == NULL) continue;
+
+  /* Delete if too old */
+
+  if (value->time_stamp < oldest)
+    {
+    printf("deleted %s (too old)\n", key);
+    dbfn_delete(dbm, key);
+    continue;
+    }
+
+  /* Do database-specific tidying for wait databases, and message-
+  specific tidying for the retry database. */
+
+  if (dbdata_type == type_wait)
+    {
+    dbdata_wait *wait = (dbdata_wait *)value;
+    BOOL update = FALSE;
+
+    /* Leave corrupt records alone */
+
+    if (wait->count > WAIT_NAME_MAX)
+      {
+      printf("**** Data for %s corrupted\n  count=%d=0x%x max=%d\n",
+        key, wait->count, wait->count, WAIT_NAME_MAX);
+      continue;
+      }
+
+    /* Loop for renamed continuation records. For each message id,
+    check to see if the message exists, and if not, remove its entry
+    from the record. Because of the possibility of split input directories,
+    we must look in both possible places for a -D file. */
+
+    for (;;)
+      {
+      int offset;
+      int length = wait->count * MESSAGE_ID_LENGTH;
+
+      for (offset = length - MESSAGE_ID_LENGTH;
+           offset >= 0; offset -= MESSAGE_ID_LENGTH)
+        {
+        Ustrncpy(buffer+path_len, wait->text + offset, MESSAGE_ID_LENGTH);
+        sprintf(CS(buffer+path_len + MESSAGE_ID_LENGTH), "-D");
+
+        if (Ustat(buffer, &statbuf) != 0)
+          {
+          buffer[path_len] = wait->text[offset+5];
+          buffer[path_len+1] = '/';
+          Ustrncpy(buffer+path_len+2, wait->text + offset, MESSAGE_ID_LENGTH);
+          sprintf(CS(buffer+path_len+2 + MESSAGE_ID_LENGTH), "-D");
+
+          if (Ustat(buffer, &statbuf) != 0)
+            {
+            int left = length - offset - MESSAGE_ID_LENGTH;
+            if (left > 0) Ustrncpy(wait->text + offset,
+              wait->text + offset + MESSAGE_ID_LENGTH, left);
+            wait->count--;
+            length -= MESSAGE_ID_LENGTH;
+            update = TRUE;
+            }
+          }
+        }
+
+      /* If record is empty and the main record, either delete it or rename
+      the next continuation, repeating if that is also empty. */
+
+      if (wait->count == 0 && Ustrchr(key, ':') == NULL)
+        {
+        while (wait->count == 0 && wait->sequence > 0)
+          {
+          uschar newkey[256];
+          dbdata_generic *newvalue;
+          sprintf(CS newkey, "%s:%d", key, wait->sequence - 1);
+          newvalue = dbfn_read_with_length(dbm, newkey, NULL);
+          if (newvalue != NULL)
+            {
+            value = newvalue;
+            wait = (dbdata_wait *)newvalue;
+            dbfn_delete(dbm, newkey);
+            printf("renamed %s\n", newkey);
+            update = TRUE;
+            }
+          else wait->sequence--;
+          }
+
+        /* If we have ended up with an empty main record, delete it
+        and break the loop. Otherwise the new record will be scanned. */
+
+        if (wait->count == 0 && wait->sequence == 0)
+          {
+          dbfn_delete(dbm, key);
+          printf("deleted %s (empty)\n", key);
+          update = FALSE;
+          break;
+          }
+        }
+
+      /* If not an empty main record, break the loop */
+
+      else break;
+      }
+
+    /* Re-write the record if required */
+
+    if (update)
+      {
+      printf("updated %s\n", key);
+      dbfn_write(dbm, key, wait, sizeof(dbdata_wait) +
+        wait->count * MESSAGE_ID_LENGTH);
+      }
+    }
+
+  /* If a retry record's key ends with a message-id, check that that message
+  still exists; if not, remove this record. */
+
+  else if (dbdata_type == type_retry)
+    {
+    uschar *id;
+    int len = Ustrlen(key);
+
+    if (len < MESSAGE_ID_LENGTH + 1) continue;
+    id = key + len - MESSAGE_ID_LENGTH - 1;
+    if (*id++ != ':') continue;
+
+    for (i = 0; i < MESSAGE_ID_LENGTH; i++)
+      {
+      if (i == 6 || i == 13)
+        { if (id[i] != '-') break; }
+      else
+        { if (!isalnum(id[i])) break; }
+      }
+    if (i < MESSAGE_ID_LENGTH) continue;
+
+    Ustrncpy(buffer + path_len, id, MESSAGE_ID_LENGTH);
+    sprintf(CS(buffer + path_len + MESSAGE_ID_LENGTH), "-D");
+
+    if (Ustat(buffer, &statbuf) != 0)
+      {
+      sprintf(CS(buffer + path_len), "%c/%s-D", id[5], id);
+      if (Ustat(buffer, &statbuf) != 0)
+        {
+        dbfn_delete(dbm, key);
+        printf("deleted %s (no message)\n", key);
+        }
+      }
+    }
+  }
+
+dbfn_close(dbm);
+printf("Tidying complete\n");
+return 0;
+}
+
+#endif  /* EXIM_TIDYDB */
+
+/* End of exim_dbutil.c */
diff --git a/src/exim_lock.c b/src/exim_lock.c
new file mode 100644 (file)
index 0000000..37d9744
--- /dev/null
@@ -0,0 +1,638 @@
+/* A program to lock a file exactly as Exim would, for investigation of
+interlocking problems.
+
+Options:  -fcntl    use fcntl() lock
+          -flock    use flock() lock
+          -lockfile use lock file
+          -mbx      use mbx locking rules, with either fcntl() or flock()
+
+Default is -fcntl -lockfile.
+
+Argument: the name of the lock file
+*/
+
+#include "os.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <signal.h>
+#include <errno.h>
+#include <time.h>
+#include <netdb.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <utime.h>
+#include <sys/utsname.h>
+#include <sys/stat.h>
+#include <sys/file.h>
+#include <pwd.h>
+
+/* Not all systems have flock() available. Those that do must define LOCK_SH
+in sys/file.h. */
+
+#ifndef LOCK_SH
+#define NO_FLOCK
+#endif
+
+
+typedef int BOOL;
+#define FALSE 0
+#define TRUE  1
+
+
+/* Flag for timeout signal handler */
+
+static int sigalrm_seen = FALSE;
+
+
+/* We need to pull in strerror() and os_non_restarting_signal() from the
+os.c source, if they are required for this OS. However, we don't need any of
+the other stuff in os.c, so force the other macros to omit it. */
+
+#ifndef OS_RESTARTING_SIGNAL
+  #define OS_RESTARTING_SIGNAL
+#endif
+
+#ifndef OS_STRSIGNAL
+  #define OS_STRSIGNAL
+#endif
+
+#ifndef OS_STREXIT
+  #define OS_STREXIT
+#endif
+
+#ifndef OS_LOAD_AVERAGE
+  #define OS_LOAD_AVERAGE
+#endif
+
+#ifndef FIND_RUNNING_INTERFACES
+  #define FIND_RUNNING_INTERFACES
+#endif
+
+#ifndef OS_GET_DNS_RESOLVER_RES
+  #define OS_GET_DNS_RESOLVER_RES
+#endif
+
+#include "../src/os.c"
+
+
+
+/*************************************************
+*             Timeout handler                    *
+*************************************************/
+
+static void
+sigalrm_handler(int sig)
+{
+sig = sig;      /* Keep picky compilers happy */
+sigalrm_seen = TRUE;
+}
+
+
+
+/*************************************************
+*           Give usage and die                   *
+*************************************************/
+
+static void
+usage(void)
+{
+printf("usage: exim_lock [-v] [-q] [-lockfile] [-fcntl] [-flock] [-mbx]\n"
+       "       [-retries <n>] [-interval <n>] [-timeout <n>] [-restore-times]\n"
+       "       <file name> [command]\n");
+exit(1);
+}
+
+
+
+/*************************************************
+*         Apply a lock to a file descriptor      *
+*************************************************/
+
+static int
+apply_lock(int fd, int fcntltype, BOOL dofcntl, int fcntltime, BOOL doflock,
+    int flocktime)
+{
+int yield = 0;
+int save_errno;
+struct flock lock_data;
+lock_data.l_type = fcntltype;
+lock_data.l_whence = lock_data.l_start = lock_data.l_len = 0;
+
+sigalrm_seen = FALSE;
+
+if (dofcntl)
+  {
+  if (fcntltime > 0)
+    {
+    os_non_restarting_signal(SIGALRM, sigalrm_handler);
+    alarm(fcntltime);
+    yield = fcntl(fd, F_SETLKW, &lock_data);
+    save_errno = errno;
+    alarm(0);
+    errno = save_errno;
+    }
+  else yield = fcntl(fd, F_SETLK, &lock_data);
+  if (yield < 0) printf("exim_lock: fcntl() failed: %s\n", strerror(errno));
+  }
+
+#ifndef NO_FLOCK
+if (doflock && (yield >= 0))
+  {
+  int flocktype = (fcntltype == F_WRLCK)? LOCK_EX : LOCK_SH;
+  if (flocktime > 0)
+    {
+    os_non_restarting_signal(SIGALRM, sigalrm_handler);
+    alarm(flocktime);
+    yield = flock(fd, flocktype);
+    save_errno = errno;
+    alarm(0);
+    errno = save_errno;
+    }
+  else yield = flock(fd, flocktype | LOCK_NB);
+  if (yield < 0) printf("exim_lock: flock() failed: %s\n", strerror(errno));
+  }
+#endif
+
+return yield;
+}
+
+
+
+/*************************************************
+*           The exim_lock program                *
+*************************************************/
+
+int main(int argc, char **argv)
+{
+int  lock_retries = 10;
+int  lock_interval = 3;
+int  lock_fcntl_timeout = 0;
+int  lock_flock_timeout = 0;
+int  i, j, len;
+int  fd = -1;
+int  hd = -1;
+int  md = -1;
+int  yield = 0;
+time_t now = time(NULL);
+BOOL use_lockfile = FALSE;
+BOOL use_fcntl = FALSE;
+BOOL use_flock = FALSE;
+BOOL use_mbx = FALSE;
+BOOL verbose = FALSE;
+BOOL quiet = FALSE;
+BOOL restore_times = FALSE;
+char *filename;
+char *lockname = NULL, *hitchname = NULL;
+char *primary_hostname;
+const char *command;
+struct utsname s;
+char buffer[256];
+char tempname[256];
+
+/* Decode options */
+
+for (i = 1; i < argc; i++)
+  {
+  char *arg = argv[i];
+  if (*arg != '-') break;
+  if (strcmp(arg, "-fcntl") == 0) use_fcntl = TRUE;
+  else if (strcmp(arg, "-flock") == 0) use_flock = TRUE;
+  else if (strcmp(arg, "-lockfile") == 0) use_lockfile = TRUE;
+  else if (strcmp(arg, "-mbx") == 0) use_mbx = TRUE;
+  else if (strcmp(arg, "-v") == 0) verbose = TRUE;
+  else if (strcmp(arg, "-q") == 0) quiet = TRUE;
+  else if (strcmp(arg, "-restore-times") == 0) restore_times = TRUE;
+  else if (++i < argc)
+    {
+    int value = atoi(argv[i]);
+    if (strcmp(arg, "-retries") == 0) lock_retries = value;
+    else if (strcmp(arg, "-interval") == 0) lock_interval = value;
+    else if (strcmp(arg, "-timeout") == 0)
+      lock_fcntl_timeout = lock_flock_timeout = value;
+    else usage();
+    }
+  else usage();
+  }
+
+if (quiet) verbose = 0;
+
+/* Can't use flock() if the OS doesn't provide it */
+
+#ifdef NO_FLOCK
+if (use_flock)
+  {
+  printf("exim_lock: can't use flock() because it was not available in the\n"
+         "           operating system when exim_lock was compiled\n");
+  exit(1);
+  }
+#endif
+
+/* Default is to use lockfiles and fcntl(). */
+
+if (!use_lockfile && !use_fcntl && !use_flock && !use_mbx)
+  use_lockfile = use_fcntl = TRUE;
+
+/* Default fcntl() for use with mbx */
+
+if (use_mbx && !use_fcntl && !use_flock) use_fcntl = TRUE;
+
+/* Unset unused timeouts */
+
+if (!use_fcntl) lock_fcntl_timeout = 0;
+if (!use_flock) lock_flock_timeout = 0;
+
+/* A file name is required */
+
+if (i >= argc) usage();
+
+filename = argv[i++];
+
+/* Expand file names starting with ~ */
+
+if (*filename == '~')
+  {
+  struct passwd *pw;
+
+  if (*(++filename) == '/')
+    pw = getpwuid(getuid());
+  else
+    {
+    char *s = buffer;
+    while (*filename != 0 && *filename != '/')
+      *s++ = *filename++;
+    *s = 0;
+    pw = getpwnam(buffer);
+    }
+
+  if (pw == NULL)
+    {
+    printf("exim_lock: unable to expand file name %s\n", argv[i-1]);
+    exit(1);
+    }
+
+  if ((int)strlen(pw->pw_dir) + (int)strlen(filename) + 1 > sizeof(buffer))
+    {
+    printf("exim_lock: expanded file name %s%s is too long", pw->pw_dir,
+      filename);
+    exit(1);
+    }
+
+  strcpy(buffer, pw->pw_dir);
+  strcat(buffer, filename);
+  filename = buffer;
+  }
+
+/* If using a lock file, prepare by creating the lock file name and
+the hitching post name. */
+
+if (use_lockfile)
+  {
+  if (uname(&s) < 0)
+    {
+    printf("exim_lock: failed to find host name using uname()\n");
+    exit(1);
+    }
+  primary_hostname = s.nodename;
+
+  len = (int)strlen(filename);
+  lockname = malloc(len + 8);
+  sprintf(lockname, "%s.lock", filename);
+  hitchname = malloc(len + 32 + (int)strlen(primary_hostname));
+
+  /* Presumably, this must match appendfile.c */
+  sprintf(hitchname, "%s.%s.%08x.%08x", lockname, primary_hostname,
+    (unsigned int)now, (unsigned int)getpid());
+
+  if (verbose)
+    printf("exim_lock: lockname =  %s\n           hitchname = %s\n", lockname,
+      hitchname);
+  }
+
+/* Locking retry loop */
+
+for (j = 0; j < lock_retries; j++)
+  {
+  int sleep_before_retry = TRUE;
+  struct stat statbuf, ostatbuf, lstatbuf, statbuf2;
+  int mbx_tmp_oflags;
+
+  /* Try to build a lock file if so configured */
+
+  if (use_lockfile)
+    {
+    int rc;
+    if (verbose) printf("exim_lock: creating lock file\n");
+    hd = open(hitchname, O_WRONLY | O_CREAT | O_EXCL, 0440);
+    if (hd < 0)
+      {
+      printf("exim_lock: failed to create hitching post %s: %s\n", hitchname,
+        strerror(errno));
+      exit(1);
+      }
+
+    /* Apply hitching post algorithm. */
+
+    if ((rc = link(hitchname, lockname)) != 0) fstat(hd, &statbuf);
+    (void)close(hd);
+    unlink(hitchname);
+
+    if (rc != 0 && statbuf.st_nlink != 2)
+      {
+      printf("exim_lock: failed to link hitching post to lock file\n");
+      hd = -1;
+      goto RETRY;
+      }
+
+    if (!quiet) printf("exim_lock: lock file successfully created\n");
+    }
+
+  /* We are done if no other locking required. */
+
+  if (!use_fcntl && !use_flock && !use_mbx) break;
+
+  /* Open the file for writing. */
+
+  fd = open(filename, O_RDWR + O_APPEND);
+  if (fd < 0)
+    {
+    printf("exim_lock: failed to open %s for writing: %s\n", filename,
+      strerror(errno));
+    yield = 1;
+    goto CLEAN_UP;
+    }
+
+  /* If there is a timeout, implying blocked locking, we don't want to
+  sleep before any retries after this. */
+
+  if (lock_fcntl_timeout > 0 || lock_flock_timeout > 0)
+    sleep_before_retry = FALSE;
+
+  /* Lock using fcntl. There are pros and cons to using a blocking call vs
+  a non-blocking call and retries. Exim is non-blocking by default, but setting
+  a timeout changes it to blocking. */
+
+  if (!use_mbx && (use_fcntl || use_flock))
+    {
+    if (apply_lock(fd, F_WRLCK, use_fcntl, lock_fcntl_timeout, use_flock,
+        lock_flock_timeout) >= 0)
+      {
+      if (!quiet)
+        {
+        if (use_fcntl) printf("exim_lock: fcntl() lock successfully applied\n");
+        if (use_flock) printf("exim_lock: flock() lock successfully applied\n");
+        }
+      break;
+      }
+    else goto RETRY;   /* Message already output */
+    }
+
+  /* Lock using MBX rules. This is complicated and is documented with the
+  source of the c-client library that goes with Pine and IMAP. What has to
+  be done to interwork correctly is to take out a shared lock on the mailbox,
+  and an exclusive lock on a /tmp file. */
+
+  else
+    {
+    if (apply_lock(fd, F_RDLCK, use_fcntl, lock_fcntl_timeout, use_flock,
+        lock_flock_timeout) >= 0)
+      {
+      if (!quiet)
+        {
+        if (use_fcntl)
+          printf("exim_lock: fcntl() read lock successfully applied\n");
+        if (use_flock)
+          printf("exim_lock: fcntl() read lock successfully applied\n");
+        }
+      }
+    else goto RETRY;   /* Message already output */
+
+    if (fstat(fd, &statbuf) < 0)
+      {
+      printf("exim_lock: fstat() of %s failed: %s\n", filename,
+        strerror(errno));
+      yield = 1;
+      goto CLEAN_UP;
+      }
+
+    /* Set up file in /tmp and check its state if already existing. */
+
+    sprintf(tempname, "/tmp/.%lx.%lx", (long)statbuf.st_dev,
+      (long)statbuf.st_ino);
+
+    if (lstat(tempname, &statbuf) >= 0)
+      {
+      if ((statbuf.st_mode & S_IFMT) == S_IFLNK)
+        {
+        printf("exim_lock: symbolic link on lock name %s\n", tempname);
+        yield = 1;
+        goto CLEAN_UP;
+        }
+      if (statbuf.st_nlink > 1)
+        {
+        printf("exim_lock: hard link to lock name %s\n", tempname);
+        yield = 1;
+        goto CLEAN_UP;
+        }
+      }
+
+    mbx_tmp_oflags = O_RDWR | O_CREAT;
+#ifdef O_NOFOLLOW
+    mbx_tmp_oflags |= O_NOFOLLOW;
+#endif
+    md = open(tempname, mbx_tmp_oflags, 0600);
+    if (md < 0)
+      {
+      printf("exim_lock: failed to create mbx lock file %s: %s\n",
+        tempname, strerror(errno));
+      goto CLEAN_UP;
+      }
+
+    /* security fixes from 2010-05 */
+    if (lstat(tempname, &lstatbuf) < 0)
+      {
+      printf("exim_lock: failed to lstat(%s) after opening it: %s\n",
+          tempname, strerror(errno));
+      goto CLEAN_UP;
+      }
+    if (fstat(md, &statbuf2) < 0)
+      {
+      printf("exim_lock: failed to fstat() open fd of \"%s\": %s\n",
+          tempname, strerror(errno));
+      goto CLEAN_UP;
+      }
+    if ((statbuf2.st_nlink > 1) ||
+        (lstatbuf.st_nlink > 1) ||
+        (!S_ISREG(lstatbuf.st_mode)) ||
+        (lstatbuf.st_dev != statbuf2.st_dev) ||
+        (lstatbuf.st_ino != statbuf2.st_ino))
+      {
+      printf("exim_lock: race condition exploited against us when "
+          "locking \"%s\"\n", tempname);
+      goto CLEAN_UP;
+      }
+
+    (void)chmod(tempname, 0600);
+
+    if (apply_lock(md, F_WRLCK, use_fcntl, lock_fcntl_timeout, use_flock,
+        lock_flock_timeout) >= 0)
+      {
+      if (!quiet)
+        {
+        if (use_fcntl)
+          printf("exim_lock: fcntl() lock successfully applied to mbx "
+            "lock file %s\n", tempname);
+        if (use_flock)
+          printf("exim_lock: flock() lock successfully applied to mbx "
+            "lock file %s\n", tempname);
+        }
+
+      /* This test checks for a race condition */
+
+      if (lstat(tempname, &statbuf) != 0 ||
+          fstat(md, &ostatbuf) != 0 ||
+          statbuf.st_dev != ostatbuf.st_dev ||
+          statbuf.st_ino != ostatbuf.st_ino)
+       {
+       if (!quiet) printf("exim_lock: mbx lock file %s changed between "
+           "creation and locking\n", tempname);
+       goto RETRY;
+       }
+      else break;
+      }
+    else goto RETRY;   /* Message already output */
+    }
+
+  /* Clean up before retrying */
+
+  RETRY:
+
+  if (md >= 0)
+    {
+    if (close(md) < 0)
+      printf("exim_lock: close %s failed: %s\n", tempname, strerror(errno));
+    else
+      if (!quiet) printf("exim_lock: %s closed\n", tempname);
+    md = -1;
+    }
+
+  if (fd >= 0)
+    {
+    if (close(fd) < 0)
+      printf("exim_lock: close failed: %s\n", strerror(errno));
+    else
+      if (!quiet) printf("exim_lock: file closed\n");
+    fd = -1;
+    }
+
+  if (hd >= 0)
+    {
+    if (unlink(lockname) < 0)
+      printf("exim_lock: unlink of %s failed: %s\n", lockname, strerror(errno));
+    else
+      if (!quiet) printf("exim_lock: lock file removed\n");
+    hd = -1;
+    }
+
+  /* If a blocking call timed out, break the retry loop if the total time
+  so far is not less than than retries * interval. */
+
+  if (sigalrm_seen &&
+      (j + 1) * ((lock_fcntl_timeout > lock_flock_timeout)?
+        lock_fcntl_timeout : lock_flock_timeout) >=
+          lock_retries * lock_interval)
+    j = lock_retries;
+
+  /* Wait a bit before retrying, except when it was a blocked fcntl() that
+  caused the problem. */
+
+  if (j < lock_retries && sleep_before_retry)
+    {
+    printf(" ... waiting\n");
+    sleep(lock_interval);
+    }
+  }
+
+if (j >= lock_retries)
+  {
+  printf("exim_lock: locking failed too many times\n");
+  yield = 1;
+  goto CLEAN_UP;
+  }
+
+if (!quiet) printf("exim_lock: locking %s succeeded: ", filename);
+
+/* If there are no further arguments, run the user's shell; otherwise
+the next argument is a command to run. */
+
+if (i >= argc)
+  {
+  command = getenv("SHELL");
+  if (command == NULL || *command == 0) command = "/bin/sh";
+  if (!quiet) printf("running %s ...\n", command);
+  }
+else
+  {
+  command = argv[i];
+  if (!quiet) printf("running the command ...\n");
+  }
+
+/* Run the command, saving and restoring the times if required. */
+
+if (restore_times)
+  {
+  struct stat strestore;
+  struct utimbuf ut;
+  stat(filename, &strestore);
+  i = system(command);
+  ut.actime = strestore.st_atime;
+  ut.modtime = strestore.st_mtime;
+  utime(filename, &ut);
+  }
+else i = system(command);
+
+if(i && !quiet) printf("warning: nonzero status %d\n", i);
+
+/* Remove the locks and exit. Unlink the /tmp file if we can get an exclusive
+lock on the mailbox. This should be a non-blocking lock call, as there is no
+point in waiting. */
+
+CLEAN_UP:
+
+if (md >= 0)
+  {
+  if (apply_lock(fd, F_WRLCK, use_fcntl, 0, use_flock, 0) >= 0)
+    {
+    if (!quiet) printf("exim_lock: %s unlinked - no sharers\n", tempname);
+    unlink(tempname);
+    }
+  else if (!quiet)
+    printf("exim_lock: %s not unlinked - unable to get exclusive mailbox lock\n",
+      tempname);
+  if (close(md) < 0)
+    printf("exim_lock: close %s failed: %s\n", tempname, strerror(errno));
+  else
+    if (!quiet) printf("exim_lock: %s closed\n", tempname);
+  }
+
+if (fd >= 0)
+  {
+  if (close(fd) < 0)
+    printf("exim_lock: close %s failed: %s\n", filename, strerror(errno));
+  else
+    if (!quiet) printf("exim_lock: %s closed\n", filename);
+  }
+
+if (hd >= 0)
+  {
+  if (unlink(lockname) < 0)
+    printf("exim_lock: unlink %s failed: %s\n", lockname, strerror(errno));
+  else
+    if (!quiet) printf("exim_lock: lock file removed\n");
+  }
+
+return yield;
+}
+
+/* End */
diff --git a/src/eximon.src b/src/eximon.src
new file mode 100644 (file)
index 0000000..fac2420
--- /dev/null
@@ -0,0 +1,201 @@
+# Base source of start-up shell script for the Exim Monitor. Used to set the
+# required environment variables before running the program. Using script
+# rather than a configuration file means that computation can be done.
+# The build process concatenates on the front of this various settings from
+# os-specific files and from the user's configuration file.
+
+# Copyright (c) 2004 - 2012 University of Cambridge.
+# See the file NOTICE for conditions of use and distribution.
+
+# Except when they appear in comments, the following placeholders in this
+# source are replaced when it is turned into a runnable script:
+#
+# CONFIGURE_FILE_USE_NODE
+# CONFIGURE_FILE
+# BIN_DIRECTORY
+# BASENAME_COMMAND
+# HOSTNAME_COMMAND
+# X11_LD_LIBRARY
+
+# PROCESSED_FLAG
+
+# See if caller wants to invoke gdb
+
+use_gdb=''
+
+case ${1:-foo} in
+  gdb*) use_gdb="$1"; shift ;;
+esac
+
+# Save arguments (can be the usual X parameters)
+
+cmd_args="$@"
+
+# See if this installation is using the esoteric "USE_NODE" feature of Exim,
+# in which it uses the host's name as a suffix for the configuration file name.
+
+if [ "CONFIGURE_FILE_USE_NODE" = "yes" ]; then
+  hostsuffix=.`uname -n`
+fi
+
+# Now find the configuration file name. This has got complicated because
+# CONFIGURE_FILE may now be a list of files. The one that is used is the first
+# one that exists. Mimic the code in readconf.c by testing first for the
+# suffixed file in each case.
+
+set `awk -F: '{ for (i = 1; i <= NF; i++) print $i }' <<End
+CONFIGURE_FILE
+End
+`
+while [ "$config" = "" -a $# -gt 0 ] ; do
+  if [ -f "$1$hostsuffix" ] ; then
+    config="$1$hostsuffix"
+  elif [ -f "$1" ] ; then
+    config="$1"
+  fi
+  shift
+done
+
+# Determine where the spool directory is and whether there is any setting of
+# log_file_path. Search for an exim_path setting in the configure file;
+# otherwise use the bin directory. Call that version of Exim to find the spool
+# directory and the setting of log_file_path.
+
+config=${EXIMON_EXIM_CONFIG-$config}
+
+# Add code here to redefine "config" if an alternative configuration file
+# should be used in some circumstances. If you do that, you should also arrange
+# for the value to be set in EXIMON_EXIM_CONFIG, and to export that variable
+# into the environment. BEWARE: a tab character is needed in the command below.
+# It has had a nasty tendency to get lost in the past. Use a variable to hold a
+# space and a tab to keep the tab in one place.
+
+st='    '
+EXIM_PATH=`grep "^[$st]*exim_path" $config | sed "s/.*=[$st]*//"`
+if test "$EXIM_PATH" = ""; then EXIM_PATH=BIN_DIRECTORY/exim; fi
+
+SPOOL_DIRECTORY=`$EXIM_PATH -C $config -bP spool_directory | sed 's/.*=[  ]*//'`
+LOG_FILE_PATH=`$EXIM_PATH -C $config -bP log_file_path | sed 's/.*=[  ]*//'`
+
+# If log_file_path is "syslog" then logging is only to syslog, and the monitor
+# is unable to display a log tail unless EXIMON_LOG_FILE_PATH is set to tell
+# it where the log data is. Otherwise, remove any occurrences of
+# "syslog:" or ":syslog" (spaces allowed in various places) and look at the
+# remainder of the entry. If it's null, the default is "mainlog" in the
+# "log" directory in the spool directory. Otherwise, set the name from the
+# given path.
+
+if [ "$EXIMON_LOG_FILE_PATH" != "" ] ; then
+  LOG_FILE_NAME="$EXIMON_LOG_FILE_PATH"
+elif [ "$LOG_FILE_PATH" = "syslog" ] ; then
+  LOG_FILE_NAME=""
+  echo \*\*\*
+  echo Exim is using the syslog interface for its log data. If you redirect all
+  echo MAIL.INFO syslog messages into a separate file, you can point eximon at
+  echo that file with the EXIMON_LOG_FILE_PATH environment variable.
+  echo \*\*\*
+else
+  LOG_FILE_NAME=`echo $LOG_FILE_PATH | \
+    sed -e 's/ *: *syslog *: */:/' \
+        -e 's/ *: *syslog *$//' \
+        -e 's/^ *syslog *: *//' \
+        -e 's/%s/main/'`
+  if [ "$LOG_FILE_NAME" = "" ] ; then
+    LOG_FILE_NAME=$SPOOL_DIRECTORY/log/mainlog
+  fi
+fi
+
+# The basename and hostname commands vary from system to system
+
+basename=BASENAME_COMMAND
+hostname=HOSTNAME_COMMAND
+
+# SunOS5 is a pain in that they may be in one of two places. So is Linux
+# in the case of basename. Set up a general mechanism for searching for
+# them in several places.
+
+if [ "${basename}" = "look_for_it" ] ; then
+  if [ -f /usr/bin/basename ] ; then
+    basename=/usr/bin/basename
+  else
+    if [ -f /bin/basename ] ; then
+      basename=/bin/basename
+    else
+      basename=/usr/ucb/basename
+    fi
+  fi
+fi
+
+if [ "${hostname}" = "look_for_it" ] ; then
+  if [ -f /usr/bin/hostname ] ; then
+    hostname=/usr/bin/hostname
+  else
+    if [ -f /bin/hostname ] ; then
+      hostname=/bin/hostname
+    else
+      hostname=/usr/ucb/hostname
+    fi
+  fi
+fi
+
+# Set hostname to the full hostname with the specified domain
+# stripped off its end. On Solaris 2, the default basename
+# command treats its suffix argument as a pattern. Consequently,
+# if fullhostname contains no dots but ends with what looks like
+# the domain, straightforward use of basename screws things up.
+# Use a general test for this case, just in case any other OS
+# do the same.
+
+fullhostname=`${hostname}`
+case `${basename} abc .c` in
+  a) hostname=`${basename} ${fullhostname} '\.'${DOMAIN}` ;;
+  *) hostname=`${basename} ${fullhostname} .${DOMAIN}` ;;
+esac
+
+
+# Arrange for the window title field to be substituted by the shell
+# so that it can contain either the full or the short host name. This
+# is a tedious little bit of magic, but I don't know how to do it
+# in a less tortuous way.
+
+WINDOW_TITLE=`fullhostname=${fullhostname} hostname=${hostname} /bin/sh <<xx
+echo ${WINDOW_TITLE}
+xx
+`
+
+# Add the X11 library to the library path, and then export the
+# environment variables used by eximon. The string X11-LD-LIBRARY
+# (with underscores, not hyphens) below is replaced by the configured
+# library name when the script is built. (Hyphens are used in the description
+# to stop it getting changed there too.)
+
+X11LIB=X11_LD_LIBRARY
+
+if [ "${LD_LIBRARY_PATH}" = "" ] ; then
+  LD_LIBRARY_PATH=${X11LIB}
+else
+  LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${X11LIB}
+fi
+
+export EXIM_PATH LD_LIBRARY_PATH \
+  LOG_BUFFER LOG_DEPTH LOG_FILE_NAME LOG_FONT LOG_WIDTH \
+  ACTION_OUTPUT ACTION_QUEUE_UPDATE\
+  MENU_EVENT MIN_HEIGHT MIN_WIDTH \
+  QUALIFY_DOMAIN QUEUE_DEPTH QUEUE_FONT QUEUE_INTERVAL QUEUE_MAX_ADDRESSES \
+  QUEUE_STRIPCHART_NAME QUEUE_TOTAL QUEUE_WIDTH SPOOL_DIRECTORY \
+  START_DEPTH LOG_STRIPCHARTS SIZE_STRIPCHART SIZE_STRIPCHART_NAME \
+  START_SMALL STRIPCHART_INTERVAL \
+  TEXT_DEPTH WINDOW_TITLE
+
+# Exec to the program we really want to run, thereby continuing in
+# just the one process, and let it run in parallel with whatever
+# called this script (unless gdb was requested in original $1).
+
+if [ "${use_gdb:-}" = "" ] ; then
+  exec "${EXIMON_BINARY}" $cmd_args &
+else
+  exec "$use_gdb" "${EXIMON_BINARY}" $cmd_args
+  # not backgrounded
+fi
+
+# End
diff --git a/src/eximstats.src b/src/eximstats.src
new file mode 100644 (file)
index 0000000..4370b4e
--- /dev/null
@@ -0,0 +1,4218 @@
+#!PERL_COMMAND -w
+
+# Copyright (c) 2001-2014 University of Cambridge.
+# See the file NOTICE for conditions of use and distribution.
+
+# Perl script to generate statistics from one or more Exim log files.
+
+# Usage: eximstats [<options>] <log file> <log file> ...
+
+# 1996-05-21: Ignore lines not starting with valid date/time, just in case
+#               these get into a log file.
+# 1996-11-19: Add the -h option to control the size of the histogram,
+#               and optionally turn it off.
+#             Use some Perl 5 things; it should be everywhere by now.
+#             Add the Perl -w option and rewrite so no warnings are given.
+#             Add the -t option to control the length of the "top" listing.
+#             Add the -ne, -nt options to turn off errors and transport
+#               information.
+#             Add information about length of time on queue, and -q<list> to
+#               control the intervals and turn it off.
+#             Add count and percentage of delayed messages to the Received
+#               line.
+#             Show total number of errors.
+#             Add count and percentage of messages with errors to Received
+#               line.
+#             Add information about relaying and -nr to suppress it.
+# 1997-02-03  Merged in some of the things Nigel Metheringham had done:
+#               Re-worded headings
+#               Added received histogram as well as delivered
+#               Added local senders' league table
+#               Added local recipients' league table
+# 1997-03-10  Fixed typo "destinationss"
+#             Allow for intermediate address between final and original
+#               when testing for relaying
+#             Give better message when no input
+# 1997-04-24  Fixed bug in layout of error listing that was depending on
+#               text length (output line got repeated).
+# 1997-05-06  Bug in option decoding when only one option.
+#             Overflow bug when handling very large volumes.
+# 1997-10-28  Updated to handle revised log format that might show
+#               HELO name as well as host name before IP number
+# 1998-01-26  Bugs in the function for calculating the number of seconds
+#               since 1970 from a log date
+# 1998-02-02  Delivery to :blackhole: doesn't have a T= entry in the log
+#               line; cope with this, thereby avoiding undefined problems
+#             Very short log line gave substring error
+# 1998-02-03  A routed delivery to a local transport may not have <> in the
+#               log line; terminate the address at white space, not <
+# 1998-09-07  If first line of input was a => line, $thissize was undefined;
+#               ensure it is zero.
+# 1998-12-21  Adding of $thissize from => line should have been adding $size.
+#             Oops. Should have looked more closely when fixing the previous
+#               bug!
+# 1999-11-12  Increased the field widths for printed integers; numbers are
+#               bigger than originally envisaged.
+# 2001-03-21  Converted seconds() routine to use Time::Local, fixing a bug
+#               whereby seconds($timestamp) - id_seconds($id) gave an
+#               incorrect result.
+#             Added POD documentation.
+#             Moved usage instructions into help() subroutine.
+#             Added 'use strict' and declared all global variables.
+#             Added '-html' flag and resultant code.
+#             Added '-cache' flag and resultant code.
+#             Added add_volume() routine and converted all volume variables
+#               to use it, fixing the overflow problems for individual hosts
+#               on large sites.
+#             Converted all volume output to GB/MB/KB as appropriate.
+#             Don't store local user stats if -nfl is specified.
+#             Modifications done by: Steve Campbell (<steve@computurn.com>)
+# 2001-04-02  Added the -t_remote_users flag. Steve Campbell.
+# 2001-10-15  Added the -domain flag. Steve Campbell.
+# 2001-10-16  Accept files on STDIN or on the command line. Steve Campbell.
+# 2001-10-21  Removed -domain flag and added -bydomain, -byhost, and -byemail.
+#             We now generate our main parsing subroutine as an eval statement
+#             which improves performance dramatically when not all the results
+#             are required. We also cache the last timestamp to time convertion.
+#
+#             NOTE: 'Top 50 destinations by (message count|volume)' lines are
+#             now 'Top N (host|email|domain) destinations by (message count|volume)'
+#             where N is the topcount. Steve Campbell.
+#
+# 2001-10-30  V1.16 Joachim Wieland.
+#            Fixed minor bugs in add_volume() when taking over this version
+#               for use in Exim 4: -w gave uninitialized value warnings in
+#               two situations: for the first addition to a counter, and if
+#               there were never any gigabytes, thereby leaving the $gigs
+#               value unset.
+#             Initialized $last_timestamp to stop a -w uninitialized warning.
+#             Minor layout tweak for grand totals (nitpicking).
+#             Put the IP addresses for relaying stats in [] and separated by
+#               a space from the domain name.
+#             Removed the IPv4-specific address test when picking out addresses
+#               for relaying. Anything inside [] is OK.
+#
+# 2002-07-02  Philip Hazel
+#             Fixed "uninitialized variable" message that occurred for relay
+#               messages that arrived from H=[1.2.3.4] hosts (no name shown).
+#               This bug didn't affect the output.
+#
+# 2002-04-15  V1.17 Joachim Wieland.
+#             Added -charts, -chartdir. -chartrel options which use
+#             GD::Graph modules to create graphical charts of the statistics.
+#
+# 2002-04-15  V1.18 Steve Campbell.
+#             Added a check for $domain to to stop a -w uninitialized warning.
+#             Added -byemaildomain option.
+#             Only print HTML header links to included tables!
+#
+# 2002-08-02  V1.19 Steve Campbell.
+#             Changed the debug mode to dump the parser onto STDERR rather
+#             than STDOUT. Documented the -d flag into the help().
+#             Rejoined the divergent 2002-04-15 and 2002-07-02 releases.
+#
+# 2002-08-21  V1.20 Steve Campbell.
+#             Added the '-merge' option to allow merging of previous reports.
+#             Fixed a missing semicolon when doing -bydomain.
+#             Make volume charts plot the data gigs and bytes rather than just bytes.
+#             Only process log lines with $flag =~ /<=|=>|->|==|\*\*|Co/
+#             Converted Emaildomain to Edomain - the column header was too wide!
+#             This changes the text output slightly. You can revert to the old
+#             column widths by changing $COLUMN_WIDTHS to 7;
+#
+# 2002-09-04  V1.21 Andreas J Mueller
+#             Local deliveries domain now defaults to 'localdomain'.
+#             Don't match F=<From> when looking for the user.
+#
+# 2002-09-05  V1.22 Steve Campbell
+#             Fixed a perl 5.005 incompatibility problem ('our' variables).
+#
+# 2002-09-11  V1.23 Steve Campbell
+#             Stopped -charts option from throwing errors on null data.
+#             Don't print out 'Errors encountered' unless there are any.
+
+# 2002-10-21  V1.23a Philip Hazel - patch from Tony Finch put in until
+#               Steve's eximstats catches up.
+#             Handle log files that include the timezone after the timestamp.
+#             Switch to assuming that log timestamps are in local time, with
+#               an option for UTC timestamps, as in Exim itself.
+#
+# 2003-02-05  V1.24 Steve Campbell
+#             Added in Sergey Sholokh's code to convert '<' and '>' characters
+#             in HTML output. Also added code to convert them back with -merge.
+#             Fixed timestamp offsets to convert to seconds rather than minutes.
+#             Updated -merge to work with output files using timezones.
+#             Added cacheing to speed up the calculation of timezone offsets.
+#
+# 2003-02-07  V1.25 Steve Campbell
+#             Optimised the usage of mktime() in the seconds subroutine.
+#             Removed the now redundant '-cache' option.
+#             html2txt() now explicitly matches HTML tags.
+#             Implemented a new sorting algorithm - the top_n_sort() routine.
+#             Added Danny Carroll's '-nvr' flag and code.
+#
+# 2003-03-13  V1.26 Steve Campbell
+#             Implemented HTML compliance changes recommended by Bernard Massot.
+#             Bug fix to allow top_n_sort() to handle null keys.
+#             Convert all domains and edomains to lowercase.
+#             Remove preceding dots from domains.
+#
+# 2003-03-13  V1.27 Steve Campbell
+#             Replaced border attributes with 'border=1', as recommended by
+#             Bernard Massot.
+#
+# 2003-06-03  V1.28 John Newman
+#             Added in the ability to skip over the parsing and evaulation of
+#             specific transports as passed to eximstats via the new "-nt/.../"
+#             command line argument.  This new switch allows the viewing of
+#             not more accurate statistics but more applicable statistics when
+#             special transports are in use (ie; SpamAssassin).  We need to be
+#             able to ignore transports such as this otherwise the resulting
+#             local deliveries are significantly skewed (doubled)...
+#
+# 2003-11-06  V1.29 Steve Campbell
+#             Added the '-pattern "Description" "/pattern/"' option.
+#
+# 2004-02-17  V1.30 Steve Campbell
+#             Added warnings if required GD::Graph modules are not available or
+#             insufficient -chart* options are specified.
+#
+# 2004-02-20  V1.31 Andrea Balzi
+#             Only show the Local Sender/Destination links if the tables exist.
+#
+# 2004-07-05  V1.32 Steve Campbell
+#             Fix '-merge -h0' divide by zero error.
+#
+# 2004-07-15  V1.33 Steve Campbell
+#             Documentation update - I've converted the subroutine
+#             documentation from POD to comments.
+#
+# 2004-12-10  V1.34 Steve Campbell
+#             Eximstats can now parse syslog lines as well as mainlog lines.
+#
+# 2004-12-20  V1.35 Wouter Verhelst
+#             Pie charts by volume were actually generated by count. Fixed.
+#
+# 2005-02-07  V1.36 Gregor Herrmann / Steve Campbell
+#             Added average sizes to HTML Top tables.
+#
+# 2005-04-26  V1.37 Frank Heydlauf
+#             Added -xls and the ability to specify output files.
+#
+# 2005-04-29  V1.38 Steve Campbell
+#             Use FileHandles for outputing results.
+#             Allow any combination of xls, txt, and html output.
+#             Fixed display of large numbers with -nvr option
+#             Fixed merging of reports with empty tables.
+#
+# 2005-05-27  V1.39 Steve Campbell
+#             Added the -include_original_destination flag
+#             Removed tabs and trailing whitespace.
+#
+# 2005-06-03  V1.40 Steve Campbell
+#             Whilst parsing the mainlog(s), store information about
+#             the messages in a hash of arrays rather than using
+#             individual hashes. This is a bit cleaner and results in
+#             dramatic memory savings, albeit at a slight CPU cost.
+#
+# 2005-06-15  V1.41 Steve Campbell
+#             Added the -show_rt<list> flag.
+#             Added the -show_dt<list> flag.
+#
+# 2005-06-24  V1.42 Steve Campbell
+#             Added Histograms for user specified patterns.
+#
+# 2005-06-30  V1.43 Steve Campbell
+#             Bug fix for V1.42 with -h0 specified. Spotted by Chris Lear.
+#
+# 2005-07-26  V1.44 Steve Campbell
+#             Use a glob alias rather than an array ref in the generated
+#             parser. This improves both readability and performance.
+#
+# 2005-09-30  V1.45 Marco Gaiarin / Steve Campbell
+#             Collect SpamAssassin and rejection statistics.
+#             Don't display local sender or destination tables unless
+#             there is data to show.
+#             Added average volumes into the top table text output.
+#
+# 2006-02-07  V1.46 Steve Campbell
+#             Collect data on the number of addresses (recipients)
+#             as well as the number of messages.
+#
+# 2006-05-05  V1.47 Steve Campbell
+#             Added 'Message too big' to the list of mail rejection
+#             reasons (thanks to Marco Gaiarin).
+#
+# 2006-06-05  V1.48 Steve Campbell
+#             Mainlog lines which have GMT offsets and are too short to
+#             have a flag are now skipped.
+#
+# 2006-11-10  V1.49 Alain Williams
+#             Added the -emptyok flag.
+#
+# 2006-11-16  V1.50 Steve Campbell
+#             Fixes for obtaining the IP address from reject messages.
+#
+# 2006-11-27  V1.51 Steve Campbell
+#             Another update for obtaining the IP address from reject messages.
+#
+# 2006-11-27  V1.52 Steve Campbell
+#             Tally any reject message containing SpamAssassin.
+#
+# 2007-01-31  V1.53 Philip Hazel
+#             Allow for [pid] after date in log lines
+#
+# 2007-02-14  V1.54 Daniel Tiefnig
+#             Improved the '($parent) =' pattern match.
+#
+# 2007-03-19  V1.55 Steve Campbell
+#             Differentiate between permanent and temporary rejects.
+#
+# 2007-03-29  V1.56 Jez Hancock
+#             Fixed some broken HTML links and added missing column headers.
+#
+# 2007-03-30  V1.57 Steve Campbell
+#             Fixed Grand Total Summary Domains, Edomains, and Email columns
+#             for Rejects, Temp Rejects, Ham, and Spam rows.
+#
+# 2007-04-11  V1.58 Steve Campbell
+#             Fix to get <> and blackhole to show in edomain tables.
+#
+# 2007-09-20  V1.59 Steve Campbell
+#             Added the -bylocaldomain option
+#
+# 2007-09-20  V1.60 Heiko Schlittermann
+#             Fix for misinterpreted log lines
+#
+# 2013-01-14  V1.61 Steve Campbell
+#             Watch out for senders sending "HELO [IpAddr]"
+#
+#
+# For documentation on the logfile format, see
+# http://www.exim.org/exim-html-4.50/doc/html/spec_48.html#IX2793
+
+=head1 NAME
+
+eximstats - generates statistics from Exim mainlog or syslog files.
+
+=head1 SYNOPSIS
+
+ eximstats [Output] [Options] mainlog1 mainlog2 ...
+ eximstats -merge [Options] report.1.txt report.2.txt ... > weekly_report.txt
+
+=head2 Output:
+
+=over 4
+
+=item B<-txt>
+
+Output the results in plain text to STDOUT.
+
+=item B<-txt>=I<filename>
+
+Output the results in plain text. Filename '-' for STDOUT is accepted.
+
+=item B<-html>
+
+Output the results in HTML to STDOUT.
+
+=item B<-html>=I<filename>
+
+Output the results in HTML. Filename '-' for STDOUT is accepted.
+
+=item B<-xls>
+
+Output the results in Excel compatible Format to STDOUT.
+Requires the Spreadsheet::WriteExcel CPAN module.
+
+=item B<-xls>=I<filename>
+
+Output the results in Excel compatible format. Filename '-' for STDOUT is accepted.
+
+
+=back
+
+=head2 Options:
+
+=over 4
+
+=item B<-h>I<number>
+
+histogram divisions per hour. The default is 1, and
+0 suppresses histograms. Valid values are:
+
+0, 1, 2, 3, 5, 10, 15, 20, 30 or 60.
+
+=item B<-ne>
+
+Don't display error information.
+
+=item B<-nr>
+
+Don't display relaying information.
+
+=item B<-nr>I</pattern/>
+
+Don't display relaying information that matches.
+
+=item B<-nt>
+
+Don't display transport information.
+
+=item B<-nt>I</pattern/>
+
+Don't display transport information that matches
+
+=item B<-q>I<list>
+
+List of times for queuing information single 0 item suppresses.
+
+=item B<-t>I<number>
+
+Display top <number> sources/destinations
+default is 50, 0 suppresses top listing.
+
+=item B<-tnl>
+
+Omit local sources/destinations in top listing.
+
+=item B<-t_remote_users>
+
+Include remote users in the top source/destination listings.
+
+=item B<-include_original_destination>
+
+Include the original destination email addresses rather than just
+using the final ones.
+Useful for finding out which of your mailing lists are receiving mail.
+
+=item B<-show_dt>I<list>
+
+Show the delivery times (B<DT>)for all the messages.
+
+Exim must have been configured to use the +deliver_time logging option
+for this option to work.
+
+I<list> is an optional list of times. Eg -show_dt1,2,4,8 will show
+the number of messages with delivery times under 1 second, 2 seconds, 4 seconds,
+8 seconds, and over 8 seconds.
+
+=item B<-show_rt>I<list>
+
+Show the receipt times for all the messages. The receipt time is
+defined as the Completed hh:mm:ss - queue_time_overall - the Receipt hh:mm:ss.
+These figures will be skewed by pipelined messages so might not be that useful.
+
+Exim must have been configured to use the +queue_time_overall logging option
+for this option to work.
+
+I<list> is an optional list of times. Eg -show_rt1,2,4,8 will show
+the number of messages with receipt times under 1 second, 2 seconds, 4 seconds,
+8 seconds, and over 8 seconds.
+
+=item B<-byhost>
+
+Show results by sending host. This may be combined with
+B<-bydomain> and/or B<-byemail> and/or B<-byedomain>. If none of these options
+are specified, then B<-byhost> is assumed as a default.
+
+=item B<-bydomain>
+
+Show results by sending domain.
+May be combined with B<-byhost> and/or B<-byemail> and/or B<-byedomain>.
+
+=item B<-byemail>
+
+Show results by sender's email address.
+May be combined with B<-byhost> and/or B<-bydomain> and/or B<-byedomain>.
+
+=item B<-byemaildomain> or B<-byedomain>
+
+Show results by sender's email domain.
+May be combined with B<-byhost> and/or B<-bydomain> and/or B<-byemail>.
+
+=item B<-pattern> I<Description> I</Pattern/>
+
+Look for the specified pattern and count the number of lines in which it appears.
+This option can be specified multiple times. Eg:
+
+ -pattern 'Refused connections' '/refused connection/'
+
+
+=item B<-merge>
+
+This option allows eximstats to merge old eximstat reports together. Eg:
+
+ eximstats mainlog.sun > report.sun.txt
+ eximstats mainlog.mon > report.mon.txt
+ eximstats mainlog.tue > report.tue.txt
+ eximstats mainlog.wed > report.web.txt
+ eximstats mainlog.thu > report.thu.txt
+ eximstats mainlog.fri > report.fri.txt
+ eximstats mainlog.sat > report.sat.txt
+ eximstats -merge       report.*.txt > weekly_report.txt
+ eximstats -merge -html report.*.txt > weekly_report.html
+
+=over 4
+
+=item *
+
+You can merge text or html reports and output the results as text or html.
+
+=item *
+
+You can use all the normal eximstat output options, but only data
+included in the original reports can be shown!
+
+=item *
+
+When merging reports, some loss of accuracy may occur in the top I<n> lists.
+This will be towards the ends of the lists.
+
+=item *
+
+The order of items in the top I<n> lists may vary when the data volumes
+round to the same value.
+
+=back
+
+=item B<-charts>
+
+Create graphical charts to be displayed in HTML output.
+Only valid in combination with I<-html>.
+
+This requires the following modules which can be obtained
+from http://www.cpan.org/modules/01modules.index.html
+
+=over 4
+
+=item GD
+
+=item GDTextUtil
+
+=item GDGraph
+
+=back
+
+To install these, download and unpack them, then use the normal perl installation procedure:
+
+ perl Makefile.PL
+ make
+ make test
+ make install
+
+=item B<-chartdir>I <dir>
+
+Create the charts in the directory <dir>
+
+=item B<-chartrel>I <dir>
+
+Specify the relative directory for the "img src=" tags from where to include
+the charts
+
+=item B<-emptyok>
+
+Specify that it's OK to not find any valid log lines. Without this
+we will output an error message if we don't find any.
+
+=item B<-d>
+
+Debug flag. This outputs the eval()'d parser onto STDOUT which makes it
+easier to trap errors in the eval section. Remember to add 1 to the line numbers to allow for the
+title!
+
+=back
+
+=head1 DESCRIPTION
+
+Eximstats parses exim mainlog and syslog files to output a statistical
+analysis of the messages processed. By default, a text
+analysis is generated, but you can request other output formats
+using flags. See the help (B<-help>) to learn
+about how to create charts from the tables.
+
+=head1 AUTHOR
+
+There is a web site at http://www.exim.org - this contains details of the
+mailing list exim-users@exim.org.
+
+=head1 TO DO
+
+This program does not perfectly handle messages whose received
+and delivered log lines are in different files, which can happen
+when you have multiple mail servers and a message cannot be
+immediately delivered. Fixing this could be tricky...
+
+Merging of xls files is not (yet) possible. Be free to implement :)
+
+=cut
+
+use integer;
+use strict;
+use IO::File;
+
+# use Time::Local;  # PH/FANF
+use POSIX;
+
+use vars qw($HAVE_GD_Graph_pie $HAVE_GD_Graph_linespoints $HAVE_Spreadsheet_WriteExcel);
+eval { require GD::Graph::pie; };
+$HAVE_GD_Graph_pie = $@ ? 0 : 1;
+eval { require GD::Graph::linespoints; };
+$HAVE_GD_Graph_linespoints = $@ ? 0 : 1;
+eval { require Spreadsheet::WriteExcel; };
+$HAVE_Spreadsheet_WriteExcel = $@ ? 0 : 1;
+
+
+##################################################
+#             Static data                        #
+##################################################
+# 'use vars' instead of 'our' as perl5.005 is still in use out there!
+use vars qw(@tab62 @days_per_month $gig);
+use vars qw($VERSION);
+use vars qw($COLUMN_WIDTHS);
+use vars qw($WEEK $DAY $HOUR $MINUTE);
+
+
+@tab62 =
+  (0,1,2,3,4,5,6,7,8,9,0,0,0,0,0,0,     # 0-9
+   0,10,11,12,13,14,15,16,17,18,19,20,  # A-K
+  21,22,23,24,25,26,27,28,29,30,31,32,  # L-W
+  33,34,35, 0, 0, 0, 0, 0,              # X-Z
+   0,36,37,38,39,40,41,42,43,44,45,46,  # a-k
+  47,48,49,50,51,52,53,54,55,56,57,58,  # l-w
+  59,60,61);                            # x-z
+
+@days_per_month = (0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334);
+$gig     = 1024 * 1024 * 1024;
+$VERSION = '1.61';
+
+# How much space do we allow for the Hosts/Domains/Emails/Edomains column headers?
+$COLUMN_WIDTHS = 8;
+
+$MINUTE = 60;
+$HOUR   = 60 * $MINUTE;
+$DAY    = 24 * $HOUR;
+$WEEK   =  7 * $DAY;
+
+# Declare global variables.
+use vars qw($total_received_data  $total_received_data_gigs  $total_received_count);
+use vars qw($total_delivered_data $total_delivered_data_gigs $total_delivered_messages $total_delivered_addresses);
+use vars qw(%timestamp2time);                   #Hash of timestamp => time.
+use vars qw($last_timestamp $last_time);        #The last time convertion done.
+use vars qw($last_date $date_seconds);          #The last date convertion done.
+use vars qw($last_offset $offset_seconds);      #The last time offset convertion done.
+use vars qw($localtime_offset);
+use vars qw($i);                                #General loop counter.
+use vars qw($debug);                            #Debug mode?
+use vars qw($ntopchart);                        #How many entries should make it into the chart?
+use vars qw($gddirectory);                      #Where to put files from GD::Graph
+
+# SpamAssassin variables
+use vars qw($spam_score $spam_score_gigs);
+use vars qw($ham_score  $ham_score_gigs);
+use vars qw(%ham_count_by_ip %spam_count_by_ip);
+use vars qw(%rejected_count_by_ip %rejected_count_by_reason);
+use vars qw(%temporarily_rejected_count_by_ip %temporarily_rejected_count_by_reason);
+
+#For use in Speadsheed::WriteExcel
+use vars qw($workbook $ws_global $ws_relayed $ws_errors);
+use vars qw($row $col $row_hist $col_hist);
+use vars qw($run_hist);
+use vars qw($f_default $f_header1 $f_header2 $f_header2_m $f_headertab $f_percent); #Format Header
+
+# Output FileHandles
+use vars qw($txt_fh $htm_fh $xls_fh);
+
+$ntopchart = 5;
+
+# The following are parameters whose values are
+# set by command line switches:
+use vars qw($show_errors $show_relay $show_transport $transport_pattern);
+use vars qw($topcount $local_league_table $include_remote_users $do_local_domain);
+use vars qw($hist_opt $hist_interval $hist_number $volume_rounding $emptyOK);
+use vars qw($relay_pattern @queue_times @user_patterns @user_descriptions);
+use vars qw(@rcpt_times @delivery_times);
+use vars qw($include_original_destination);
+use vars qw($txt_fh $htm_fh $xls_fh);
+
+use vars qw(%do_sender);                #Do sender by Host, Domain, Email, and/or Edomain tables.
+use vars qw($charts $chartrel $chartdir $charts_option_specified);
+use vars qw($merge_reports);            #Merge old reports ?
+
+# The following are modified in the parse() routine, and
+# referred to in the print_*() routines.
+use vars qw($delayed_count $relayed_unshown $begin $end);
+use vars qw(%messages @message);
+use vars qw(%received_count       %received_data       %received_data_gigs);
+use vars qw(%delivered_messages      %delivered_data      %delivered_data_gigs %delivered_addresses);
+use vars qw(%received_count_user  %received_data_user  %received_data_gigs_user);
+use vars qw(%delivered_messages_user %delivered_addresses_user %delivered_data_user %delivered_data_gigs_user);
+use vars qw(%delivered_messages_local_domain %delivered_addresses_local_domain %delivered_data_local_domain %delivered_data_gigs_local_domain);
+use vars qw(%transported_count    %transported_data    %transported_data_gigs);
+use vars qw(%relayed %errors_count $message_errors);
+use vars qw(@qt_all_bin @qt_remote_bin);
+use vars qw($qt_all_overflow $qt_remote_overflow);
+use vars qw(@dt_all_bin @dt_remote_bin %rcpt_times_bin);
+use vars qw($dt_all_overflow $dt_remote_overflow %rcpt_times_overflow);
+use vars qw(@received_interval_count @delivered_interval_count);
+use vars qw(@user_pattern_totals @user_pattern_interval_count);
+
+use vars qw(%report_totals);
+
+# Enumerations
+use vars qw($SIZE $FROM_HOST $FROM_ADDRESS $ARRIVAL_TIME $REMOTE_DELIVERED $PROTOCOL);
+use vars qw($DELAYED $HAD_ERROR);
+$SIZE             = 0;
+$FROM_HOST        = 1;
+$FROM_ADDRESS     = 2;
+$ARRIVAL_TIME     = 3;
+$REMOTE_DELIVERED = 4;
+$DELAYED          = 5;
+$HAD_ERROR        = 6;
+$PROTOCOL         = 7;
+
+
+
+##################################################
+#                   Subroutines                  #
+##################################################
+
+#######################################################################
+# get_filehandle($file,\%output_files);
+# Return a filehandle writing to $file.
+#
+# If %output_files is defined, check that $output_files{$file}
+# doesn't exist and die if it does, or set it if it doesn't.
+#######################################################################
+sub get_filehandle {
+  my($file,$output_files_href) = @_;
+
+  $file = '-' if ($file eq '');
+
+  if (defined $output_files_href) {
+    die "You can only output to '$file' once! Use -h for help.\n" if exists $output_files_href->{$file};
+    $output_files_href->{$file} = 1;
+  }
+
+  if ($file eq '-') {
+    return \*STDOUT;
+  }
+
+  if (-e $file) {
+    unlink $file or die "Failed to rm $file: $!";
+  }
+
+  my $fh = new IO::File $file, O_WRONLY|O_CREAT|O_EXCL;
+  die "new IO::File $file failed: $!" unless (defined $fh);
+  return $fh;
+}
+
+
+#######################################################################
+# volume_rounded();
+#
+# $rounded_volume = volume_rounded($bytes,$gigabytes);
+#
+# Given a data size in bytes, round it to KB, MB, or GB
+# as appropriate.
+#
+# Eg 12000 => 12KB, 15000000 => 14GB, etc.
+#
+# Note: I've experimented with Math::BigInt and it results in a 33%
+# performance degredation as opposed to storing numbers split into
+# bytes and gigabytes.
+#######################################################################
+sub volume_rounded {
+  my($x,$g) = @_;
+  $x = 0 unless $x;
+  $g = 0 unless $g;
+  my($rounded);
+
+  while ($x > $gig) {
+    $g++;
+    $x -= $gig;
+  }
+
+  if ($volume_rounding) {
+    # Values < 1 GB
+    if ($g <= 0) {
+      if ($x < 10000) {
+        $rounded = sprintf("%6d", $x);
+      }
+      elsif ($x < 10000000) {
+        $rounded = sprintf("%4dKB", ($x + 512)/1024);
+      }
+      else {
+        $rounded = sprintf("%4dMB", ($x + 512*1024)/(1024*1024));
+      }
+    }
+    # Values between 1GB and 10GB are printed in MB
+    elsif ($g < 10) {
+      $rounded = sprintf("%4dMB", ($g * 1024) + ($x + 512*1024)/(1024*1024));
+    }
+    else {
+      # Handle values over 10GB
+      $rounded = sprintf("%4dGB", $g + ($x + $gig/2)/$gig);
+    }
+  }
+  else {
+    # We don't want any rounding to be done.
+    # and we don't need broken formated output which on one hand avoids numbers from
+    # being interpreted as string by Spreadsheed Calculators, on the other hand
+    # breaks if more than 4 digits! -> flexible length instead of fixed length
+    # Format the return value at the output routine! -fh
+    #$rounded = sprintf("%d", ($g * $gig) + $x);
+    no integer;
+    $rounded = sprintf("%.0f", ($g * $gig) + $x);
+  }
+
+  return $rounded;
+}
+
+
+#######################################################################
+# un_round();
+#
+#  un_round($rounded_volume,\$bytes,\$gigabytes);
+#
+# Given a volume in KB, MB or GB, as generated by volume_rounded(),
+# do the reverse transformation and convert it back into Bytes and Gigabytes.
+# These are added to the $bytes and $gigabytes parameters.
+#
+# Given a data size in bytes, round it to KB, MB, or GB
+# as appropriate.
+#
+# EG: 500 => (500,0), 14GB => (0,14), etc.
+#######################################################################
+sub un_round {
+  my($rounded,$bytes_sref,$gigabytes_sref) = @_;
+
+  if ($rounded =~ /(\d+)GB/) {
+    $$gigabytes_sref += $1;
+  }
+  elsif ($rounded =~ /(\d+)MB/) {
+    $$gigabytes_sref +=   $1 / 1024;
+    $$bytes_sref     += (($1 % 1024 ) * 1024 * 1024);
+  }
+  elsif ($rounded =~ /(\d+)KB/) {
+    $$gigabytes_sref +=  $1 / (1024 * 1024);
+    $$bytes_sref     += ($1 % (1024 * 1024) * 1024);
+  }
+  elsif ($rounded =~ /(\d+)/) {
+    # We need to turn off integer in case we are merging an -nvr report.
+    no integer;
+    $$gigabytes_sref += int($1 / $gig);
+    $$bytes_sref     += $1 % $gig;
+  }
+
+  #Now reduce the bytes down to less than 1GB.
+  add_volume($bytes_sref,$gigabytes_sref,0) if ($$bytes_sref > $gig);
+}
+
+
+#######################################################################
+# add_volume();
+#
+#   add_volume(\$bytes,\$gigs,$size);
+#
+# Add $size to $bytes/$gigs where this is a number split into
+# bytes ($bytes) and gigabytes ($gigs). This is significantly
+# faster than using Math::BigInt.
+#######################################################################
+sub add_volume {
+  my($bytes_ref,$gigs_ref,$size) = @_;
+  $$bytes_ref = 0 if ! defined $$bytes_ref;
+  $$gigs_ref = 0 if ! defined $$gigs_ref;
+  $$bytes_ref += $size;
+  while ($$bytes_ref > $gig) {
+    $$gigs_ref++;
+    $$bytes_ref -= $gig;
+  }
+}
+
+
+#######################################################################
+# format_time();
+#
+#  $formatted_time = format_time($seconds);
+#
+# Given a time in seconds, break it down into
+# weeks, days, hours, minutes, and seconds.
+#
+# Eg 12005 => 3h20m5s
+#######################################################################
+sub format_time {
+my($t) = pop @_;
+my($s) = $t % 60;
+$t /= 60;
+my($m) = $t % 60;
+$t /= 60;
+my($h) = $t % 24;
+$t /= 24;
+my($d) = $t % 7;
+my($w) = $t/7;
+my($p) = "";
+$p .= "$w"."w" if $w > 0;
+$p .= "$d"."d" if $d > 0;
+$p .= "$h"."h" if $h > 0;
+$p .= "$m"."m" if $m > 0;
+$p .= "$s"."s" if $s > 0 || $p eq "";
+$p;
+}
+
+
+#######################################################################
+#  unformat_time();
+#
+#  $seconds = unformat_time($formatted_time);
+#
+# Given a time in weeks, days, hours, minutes, or seconds, convert it to seconds.
+#
+# Eg 3h20m5s => 12005
+#######################################################################
+sub unformat_time {
+  my($formated_time) = pop @_;
+  my $time = 0;
+
+  while ($formated_time =~ s/^(\d+)([wdhms]?)//) {
+    $time +=  $1 if ($2 eq '' || $2 eq 's');
+    $time +=  $1 * 60 if ($2 eq 'm');
+    $time +=  $1 * 60 * 60 if ($2 eq 'h');
+    $time +=  $1 * 60 * 60 * 24 if ($2 eq 'd');
+    $time +=  $1 * 60 * 60 * 24  * 7 if ($2 eq 'w');
+  }
+  $time;
+}
+
+
+#######################################################################
+# seconds();
+#
+#  $time = seconds($timestamp);
+#
+# Given a time-of-day timestamp, convert it into a time() value using
+# POSIX::mktime.  We expect the timestamp to be of the form
+# "$year-$mon-$day $hour:$min:$sec", with month going from 1 to 12,
+# and the year to be absolute (we do the necessary conversions). The
+# timestamp may be followed with an offset from UTC like "+$hh$mm"; if the
+# offset is not present, and we have not been told that the log is in UTC
+# (with the -utc option), then we adjust the time by the current local
+# time offset so that it can be compared with the time recorded in message
+# IDs, which is UTC.
+#
+# To improve performance, we only use mktime on the date ($year-$mon-$day),
+# and only calculate it if the date is different to the previous time we
+# came here. We then add on seconds for the '$hour:$min:$sec'.
+#
+# We also store the results of the last conversion done, and only
+# recalculate if the date is different.
+#
+# We used to have the '-cache' flag which would store the results of the
+# mktime() call. However, the current way of just using mktime() on the
+# date obsoletes this.
+#######################################################################
+sub seconds {
+  my($timestamp) = @_;
+
+  # Is the timestamp the same as the last one?
+  return $last_time if ($last_timestamp eq $timestamp);
+
+  return 0 unless ($timestamp =~ /^((\d{4})\-(\d\d)-(\d\d))\s(\d\d):(\d\d):(\d\d)( ([+-])(\d\d)(\d\d))?/o);
+
+  unless ($last_date eq $1) {
+    $last_date = $1;
+    my(@timestamp) = (0,0,0,$4,$3,$2);
+    $timestamp[5] -= 1900;
+    $timestamp[4]--;
+    $date_seconds = mktime(@timestamp);
+  }
+  my $time = $date_seconds + ($5 * 3600) + ($6 * 60) + $7;
+
+  # SC. Use cacheing. Also note we want seconds not minutes.
+  #my($this_offset) = ($10 * 60 + $11) * ($9 . "1") if defined $8;
+  if (defined $8 && ($8 ne $last_offset)) {
+    $last_offset = $8;
+    $offset_seconds = ($10 * 60 + $11) * 60;
+    $offset_seconds = -$offset_seconds if ($9 eq '-');
+  }
+
+
+  if (defined $7) {
+    #$time -= $this_offset;
+    $time -= $offset_seconds;
+  } elsif (defined $localtime_offset) {
+    $time -= $localtime_offset;
+  }
+
+  # Store the last timestamp received.
+  $last_timestamp = $timestamp;
+  $last_time      = $time;
+
+  $time;
+}
+
+
+#######################################################################
+#  id_seconds();
+#
+#  $time = id_seconds($message_id);
+#
+# Given a message ID, convert it into a time() value.
+#######################################################################
+sub id_seconds {
+my($sub_id) = substr((pop @_), 0, 6);
+my($s) = 0;
+my(@c) = split(//, $sub_id);
+while($#c >= 0) { $s = $s * 62 + $tab62[ord(shift @c) - ord('0')] }
+$s;
+}
+
+#######################################################################
+#  wdhms_seconds();
+#
+#  $seconds = wdhms_seconds($string);
+#
+# Convert a string in a week/day/hour/minute/second format (eg 4h10s)
+# into seconds.
+#######################################################################
+sub wdhms_seconds {
+  if ($_[0] =~ /^(?:(\d+)w)?(?:(\d+)d)?(?:(\d+)h)?(?:(\d+)m)?(?:(\d+)s)?/) {
+    return((($1||0) * $WEEK) + (($2||0) * $DAY) + (($3||0) * $HOUR) + (($4||0) * $MINUTE) + ($5||0));
+  }
+  return undef;
+}
+
+#######################################################################
+#  queue_time();
+#
+#  $queued = queue_time($completed_tod, $arrival_time, $id);
+#
+# Given the completed time of day and either the arrival time
+# (preferred), or the message ID, calculate how long the message has
+# been on the queue.
+#
+#######################################################################
+sub queue_time {
+  my($completed_tod, $arrival_time, $id) = @_;
+
+  # Note: id_seconds() benchmarks as 42% slower than seconds()
+  # and computing the time accounts for a significant portion of
+  # the run time.
+  if (defined $arrival_time) {
+    return(seconds($completed_tod) - seconds($arrival_time));
+  }
+  else {
+    return(seconds($completed_tod) - id_seconds($id));
+  }
+}
+
+
+#######################################################################
+#  calculate_localtime_offset();
+#
+#  $localtime_offset = calculate_localtime_offset();
+#
+# Calculate the the localtime offset from gmtime in seconds.
+#
+#  $localtime = time() + $localtime_offset.
+#
+# These are the same semantics as ISO 8601 and RFC 2822 timezone offsets.
+# (West is negative, East is positive.)
+#######################################################################
+
+# $localtime = gmtime() + $localtime_offset.  OLD COMMENT
+# This subroutine commented out as it's not currently in use.
+
+#sub calculate_localtime_offset {
+#  # Pick an arbitrary date, convert it to localtime & gmtime, and return the difference.
+#  my (@sample_date) = (0,0,0,5,5,100);
+#  my $localtime = timelocal(@sample_date);
+#  my $gmtime    = timegm(@sample_date);
+#  my $offset = $localtime - $gmtime;
+#  return $offset;
+#}
+
+sub calculate_localtime_offset {
+  # Assume that the offset at the moment is valid across the whole
+  # period covered by the logs that we're analysing. This may not
+  # be true around the time the clocks change in spring or autumn.
+  my $utc = time;
+  # mktime works on local time and gmtime works in UTC
+  my $local = mktime(gmtime($utc));
+  return $local - $utc;
+}
+
+
+
+#######################################################################
+# print_duration_table();
+#
+#  print_duration_table($title, $message_type, \@times, \@values, $overflow);
+#
+# Print a table showing how long a particular step took for
+# the messages. The parameters are:
+#   $title         Eg "Time spent on the queue"
+#   $message_type  Eg "Remote"
+#   \@times        The maximum time a message took for it to increment
+#                  the corresponding @values counter.
+#   \@values       An array of message counters.
+#   $overflow      The number of messages which exceeded the maximum
+#                  time.
+#######################################################################
+sub print_duration_table {
+no integer;
+my($title, $message_type, $times_aref, $values_aref, $overflow) = @_;
+my(@chartdatanames);
+my(@chartdatavals);
+
+my $printed_one = 0;
+my $cumulative_percent = 0;
+
+my $queue_total = $overflow;
+map {$queue_total += $_} @$values_aref;
+
+my $temp = "$title: $message_type";
+
+
+my $txt_format = "%5s %4s   %6d %5.1f%%  %5.1f%%\n";
+my $htm_format = "<tr><td align=\"right\">%s %s</td><td align=\"right\">%d</td><td align=\"right\">%5.1f%%</td><td align=\"right\">%5.1f%%</td>\n";
+
+# write header
+printf $txt_fh ("%s\n%s\n\n", $temp, "-" x length($temp)) if $txt_fh;
+if ($htm_fh) {
+  print $htm_fh "<hr><a name=\"$title $message_type\"></a><h2>$temp</h2>\n";
+  print $htm_fh "<table border=0 width=\"100%\"><tr><td><table border=1>\n";
+  print $htm_fh "<tr><th>Time</th><th>Messages</th><th>Percentage</th><th>Cumulative Percentage</th>\n";
+}
+if ($xls_fh) {
+  $ws_global->write($row++, $col, "$title: ".$message_type, $f_header2);
+  my @content=("Time", "Messages", "Percentage", "Cumulative Percentage");
+  &set_worksheet_line($ws_global, $row++, 1, \@content, $f_headertab);
+}
+
+
+for ($i = 0; $i <= $#$times_aref; ++$i) {
+  if ($$values_aref[$i] > 0)
+    {
+    my $percent = ($values_aref->[$i] * 100)/$queue_total;
+    $cumulative_percent += $percent;
+
+    my @content=($printed_one? "     " : "Under",
+        format_time($times_aref->[$i]),
+        $values_aref->[$i], $percent, $cumulative_percent);
+
+    if ($htm_fh) {
+      printf $htm_fh ($htm_format, @content);
+      if (!defined($values_aref->[$i])) {
+        print $htm_fh "Not defined";
+      }
+    }
+    if ($txt_fh) {
+      printf $txt_fh ($txt_format, @content);
+      if (!defined($times_aref->[$i])) {
+        print $txt_fh "Not defined";
+      }
+    }
+    if ($xls_fh)
+    {
+      no integer;
+      &set_worksheet_line($ws_global, $row, 0, [@content[0,1,2]], $f_default);
+      &set_worksheet_line($ws_global, $row++, 3, [$content[3]/100,$content[4]/100], $f_percent);
+
+      if (!defined($times_aref->[$i])) {
+        $col=0;
+        $ws_global->write($row++, $col, "Not defined"  );
+      }
+    }
+
+    push(@chartdatanames,
+      ($printed_one? "" : "Under") . format_time($times_aref->[$i]));
+    push(@chartdatavals, $$values_aref[$i]);
+    $printed_one = 1;
+  }
+}
+
+if ($overflow && $overflow > 0) {
+  my $percent = ($overflow * 100)/$queue_total;
+  $cumulative_percent += $percent;
+
+    my @content = ("Over ", format_time($times_aref->[-1]),
+        $overflow, $percent, $cumulative_percent);
+
+    printf $txt_fh ($txt_format, @content) if $txt_fh;
+    printf $htm_fh ($htm_format, @content) if $htm_fh;
+    if ($xls_fh)
+    {
+      &set_worksheet_line($ws_global, $row, 0, [@content[0,1,2]], $f_default);
+      &set_worksheet_line($ws_global, $row++, 3, [$content[3]/100,$content[4]/100], $f_percent);
+    }
+
+}
+
+push(@chartdatanames, "Over " . format_time($times_aref->[-1]));
+push(@chartdatavals, $overflow);
+
+#printf("Unknown   %6d\n", $queue_unknown) if $queue_unknown > 0;
+if ($htm_fh) {
+  print $htm_fh "</table></td><td>";
+
+  if ($HAVE_GD_Graph_pie && $charts && ($#chartdatavals > 0)) {
+    my @data = (
+       \@chartdatanames,
+       \@chartdatavals
+    );
+    my $graph = GD::Graph::pie->new(200, 200);
+    my $pngname = "$title-$message_type.png";
+    $pngname =~ s/[^\w\-\.]/_/;
+
+    my $graph_title = "$title ($message_type)";
+    $graph->set(title => $graph_title) if (length($graph_title) < 21);
+
+    my $gd = $graph->plot(\@data) or warn($graph->error);
+    if ($gd) {
+      open(IMG, ">$chartdir/$pngname") or die "Could not write $chartdir/$pngname: $!\n";
+      binmode IMG;
+      print IMG $gd->png;
+      close IMG;
+      print $htm_fh "<img src=\"$chartrel/$pngname\">";
+    }
+  }
+  print $htm_fh "</td></tr></table>\n";
+}
+
+if ($xls_fh)
+{
+  $row++;
+}
+print $txt_fh "\n" if $txt_fh;
+print $htm_fh "\n" if $htm_fh;
+
+}
+
+
+#######################################################################
+# print_histogram();
+#
+#  print_histogram('Deliveries|Messages received|$pattern', $unit, @interval_count);
+#
+# Print a histogram of the messages delivered/received per time slot
+# (hour by default).
+#######################################################################
+sub print_histogram {
+my($text, $unit, @interval_count) = @_;
+my(@chartdatanames);
+my(@chartdatavals);
+my($maxd) = 0;
+
+# save first row of print_histogram for xls output
+if (!$run_hist) {
+  $row_hist = $row;
+}
+else {
+  $row = $row_hist;
+}
+
+for ($i = 0; $i < $hist_number; $i++)
+  { $maxd = $interval_count[$i] if $interval_count[$i] > $maxd; }
+
+my $scale = int(($maxd + 25)/50);
+$scale = 1 if $scale == 0;
+
+if ($scale != 1) {
+  if ($unit !~ s/y$/ies/) {
+    $unit .= 's';
+  }
+}
+
+# make and output title
+my $title = sprintf("$text per %s",
+    ($hist_interval == 60)? "hour" :
+    ($hist_interval == 1)?  "minute" : "$hist_interval minutes");
+
+my $txt_htm_title = $title . " (each dot is $scale $unit)";
+
+printf $txt_fh ("%s\n%s\n\n", $txt_htm_title, "-" x length($txt_htm_title)) if $txt_fh;
+
+if ($htm_fh) {
+  print $htm_fh "<hr><a name=\"$text\"></a><h2>$txt_htm_title</h2>\n";
+  print $htm_fh "<table border=0 width=\"100%\">\n";
+  print $htm_fh "<tr><td><pre>\n";
+}
+
+if ($xls_fh) {
+  $title =~ s/Messages/Msg/ ;
+  $row += 2;
+  $ws_global->write($row++, $col_hist+1, $title, $f_headertab);
+}
+
+
+my $hour = 0;
+my $minutes = 0;
+for ($i = 0; $i < $hist_number; $i++) {
+  my $c = $interval_count[$i];
+
+  # If the interval is an hour (the maximum) print the starting and
+  # ending hours as a label. Otherwise print the starting hour and
+  # minutes, which take up the same space.
+
+  my $temp;
+  if ($hist_opt == 1) {
+    $temp = sprintf("%02d-%02d", $hour, $hour + 1);
+
+    print $txt_fh $temp if $txt_fh;
+    print $htm_fh $temp if $htm_fh;
+
+    if ($xls_fh) {
+      if ($run_hist==0) {
+        # only on first run
+        $ws_global->write($row, 0, [$temp], $f_default);
+      }
+    }
+
+    push(@chartdatanames, $temp);
+    $hour++;
+  }
+  else {
+    if ($minutes == 0)
+      { $temp = sprintf("%02d:%02d", $hour, $minutes) }
+    else
+      { $temp = sprintf("  :%02d", $minutes) }
+
+    print $txt_fh $temp if $txt_fh;
+    print $htm_fh $temp if $htm_fh;
+    if (($xls_fh) and ($run_hist==0)) {
+      # only on first run
+      $temp = sprintf("%02d:%02d", $hour, $minutes);
+      $ws_global->write($row, 0, [$temp], $f_default);
+    }
+
+    push(@chartdatanames, $temp);
+    $minutes += $hist_interval;
+    if ($minutes >= 60) {
+      $minutes = 0;
+      $hour++;
+    }
+  }
+  push(@chartdatavals, $c);
+
+  printf $txt_fh (" %6d %s\n", $c, "." x ($c/$scale)) if $txt_fh;
+  printf $htm_fh (" %6d %s\n", $c, "." x ($c/$scale)) if $htm_fh;
+  $ws_global->write($row++, $col_hist+1, [$c], $f_default) if $xls_fh;
+
+} #end for
+
+printf $txt_fh "\n" if $txt_fh;
+printf $htm_fh "\n" if $htm_fh;
+
+if ($htm_fh)
+{
+  print $htm_fh "</pre>\n";
+  print $htm_fh "</td><td>\n";
+  if ($HAVE_GD_Graph_linespoints && $charts && ($#chartdatavals > 0)) {
+    # calculate the graph
+    my @data = (
+       \@chartdatanames,
+       \@chartdatavals
+    );
+    my $graph = GD::Graph::linespoints->new(300, 300);
+    $graph->set(
+        x_label           => 'Time',
+        y_label           => 'Amount',
+        title             => $text,
+        x_labels_vertical => 1
+    );
+    my $pngname = "histogram_$text.png";
+    $pngname =~ s/[^\w\._]/_/g;
+
+    my $gd = $graph->plot(\@data) or warn($graph->error);
+    if ($gd) {
+      open(IMG, ">$chartdir/$pngname") or die "Could not write $chartdir/$pngname: $!\n";
+      binmode IMG;
+      print IMG $gd->png;
+      close IMG;
+      print $htm_fh "<img src=\"$chartrel/$pngname\">";
+    }
+  }
+  print $htm_fh "</td></tr></table>\n";
+}
+
+$col_hist++; # where to continue next times
+
+$row+=2;     # leave some space after history block
+$run_hist=1; # we have done this once or more
+}
+
+
+
+#######################################################################
+# print_league_table();
+#
+#  print_league_table($league_table_type,\%message_count,\%address_count,\%message_data,\%message_data_gigs, $spreadsheet, $row_sref);
+#
+# Given hashes of message count, address count, and message data,
+# which are keyed by the table type (eg by the sending host), print a
+# league table showing the top $topcount (defaults to 50).
+#######################################################################
+sub print_league_table {
+  my($text,$m_count,$a_count,$m_data,$m_data_gigs,$spreadsheet, $row_sref) = @_;
+  my($name) = ($topcount == 1)? "$text" : "$topcount ${text}s";
+  my($title) = "Top $name by message count";
+  my(@chartdatanames) = ();
+  my(@chartdatavals) = ();
+  my $chartotherval = 0;
+  $text = ucfirst($text);
+
+  # Align non-local addresses to the right (so all the .com's line up).
+  # Local addresses are aligned on the left as they are userids.
+  my $align = ($text !~ /local/i) ? 'right' : 'left';
+
+
+  ################################################
+  # Generate the printf formats and table headers.
+  ################################################
+  my(@headers) = ('Messages');
+  #push(@headers,'Addresses') if defined $a_count;
+  push(@headers,'Addresses') if defined $a_count && %$a_count;
+  push(@headers,'Bytes','Average') if defined $m_data;
+
+  my $txt_format = "%10s " x @headers . "  %s\n";
+  my $txt_col_headers = sprintf $txt_format, @headers, $text;
+  my $htm_format = "<tr>" . '<td align="right">%s</td>'x@headers . "<td align=\"$align\" nowrap>%s</td></tr>\n";
+  my $htm_col_headers = sprintf $htm_format, @headers, $text;
+  $htm_col_headers =~ s/(<\/?)td/$1th/g;      #Convert <td>'s to <th>'s for the header.
+
+
+  ################################################
+  # Write the table headers
+  ################################################
+  printf $txt_fh ("%s\n%s\n%s", $title, "-" x length($title),$txt_col_headers) if $txt_fh;
+
+  if ($htm_fh) {
+    print $htm_fh <<EoText;
+<hr><a name="$text count"></a><h2>$title</h2>
+<table border=0 width="100%">
+<tr><td>
+<table border=1>
+EoText
+  print $htm_fh $htm_col_headers
+  }
+
+  if ($xls_fh) {
+    $spreadsheet->write(${$row_sref}++, 0, $title, $f_header2);
+    $spreadsheet->write(${$row_sref}++, 0, [@headers, $text], $f_headertab);
+  }
+
+
+  # write content
+  foreach my $key (top_n_sort($topcount,$m_count,$m_data_gigs,$m_data)) {
+
+    # When displaying the average figures, we calculate the average of
+    # the rounded data, as the user would calculate it. This reduces
+    # the accuracy slightly, but we have to do it this way otherwise
+    # when using -merge to convert results from text to HTML and
+    # vice-versa discrepencies would occur.
+    my $messages  = $$m_count{$key};
+    my @content = ($messages);
+    push(@content, $$a_count{$key}) if defined $a_count;
+    if (defined $m_data) {
+      my $rounded_volume = volume_rounded($$m_data{$key},$$m_data_gigs{$key});
+      my($data,$gigs) = (0,0);
+      un_round($rounded_volume,\$data,\$gigs);
+      my $rounded_average = volume_rounded($data/$messages,$gigs/$messages);
+      push(@content, $rounded_volume, $rounded_average);
+    }
+
+    # write content
+    printf $txt_fh ($txt_format, @content, $key) if $txt_fh;
+
+    if ($htm_fh) {
+      my $htmlkey = $key;
+      $htmlkey =~ s/>/\&gt\;/g;
+      $htmlkey =~ s/</\&lt\;/g;
+      printf $htm_fh ($htm_format, @content, $htmlkey);
+    }
+    $spreadsheet->write(${$row_sref}++, 0, [@content, $key], $f_default) if $xls_fh;
+
+    if (scalar @chartdatanames < $ntopchart) {
+      push(@chartdatanames, $key);
+      push(@chartdatavals, $$m_count{$key});
+    }
+    else {
+      $chartotherval += $$m_count{$key};
+    }
+  }
+
+  push(@chartdatanames, "Other");
+  push(@chartdatavals, $chartotherval);
+
+  print $txt_fh "\n" if $txt_fh;
+  if ($htm_fh) {
+    print $htm_fh "</table>\n";
+    print $htm_fh "</td><td>\n";
+    if ($HAVE_GD_Graph_pie && $charts && ($#chartdatavals > 0))
+      {
+      # calculate the graph
+      my @data = (
+         \@chartdatanames,
+         \@chartdatavals
+      );
+      my $graph = GD::Graph::pie->new(300, 300);
+      $graph->set(
+          x_label           => 'Name',
+          y_label           => 'Amount',
+          title             => 'By count',
+      );
+      my $gd = $graph->plot(\@data) or warn($graph->error);
+      if ($gd) {
+        my $temp = $text;
+        $temp =~ s/ /_/g;
+        open(IMG, ">$chartdir/${temp}_count.png") or die "Could not write $chartdir/${temp}_count.png: $!\n";
+        binmode IMG;
+        print IMG $gd->png;
+        close IMG;
+        print $htm_fh "<img src=\"$chartrel/${temp}_count.png\">";
+      }
+    }
+    print $htm_fh "</td><td>\n";
+    print $htm_fh "</td></tr></table>\n\n";
+  }
+  ++${$row_sref} if $xls_fh;
+
+
+  if (defined $m_data) {
+    # write header
+
+    $title = "Top $name by volume";
+
+    printf $txt_fh ("%s\n%s\n%s", $title, "-" x length($title),$txt_col_headers) if $txt_fh;
+
+    if ($htm_fh) {
+      print $htm_fh <<EoText;
+<hr><a name="$text volume"></a><h2>$title</h2>
+<table border=0 width="100%">
+<tr><td>
+<table border=1>
+EoText
+    print $htm_fh $htm_col_headers;
+    }
+    if ($xls_fh) {
+      $spreadsheet->write(${$row_sref}++, 0, $title, $f_header2);
+      $spreadsheet->write(${$row_sref}++, 0, [@headers, $text], $f_headertab);
+    }
+
+    @chartdatanames = ();
+    @chartdatavals = ();
+    $chartotherval = 0;
+    my $use_gig = 0;
+    foreach my $key (top_n_sort($topcount,$m_data_gigs,$m_data,$m_count)) {
+      # The largest volume will be the first (top of the list).
+      # If it has at least 1 gig, then just use gigabytes to avoid
+      # risking an integer overflow when generating the pie charts.
+      if ($$m_data_gigs{$key}) {
+        $use_gig = 1;
+      }
+
+      my $messages  = $$m_count{$key};
+      my @content = ($messages);
+      push(@content, $$a_count{$key}) if defined $a_count;
+      my $rounded_volume = volume_rounded($$m_data{$key},$$m_data_gigs{$key});
+      my($data ,$gigs) = (0,0);
+      un_round($rounded_volume,\$data,\$gigs);
+      my $rounded_average = volume_rounded($data/$messages,$gigs/$messages);
+      push(@content, $rounded_volume, $rounded_average );
+
+      # write content
+      printf $txt_fh ($txt_format, @content, $key) if $txt_fh;
+      if ($htm_fh) {
+        my $htmlkey = $key;
+        $htmlkey =~ s/>/\&gt\;/g;
+        $htmlkey =~ s/</\&lt\;/g;
+        printf $htm_fh ($htm_format, @content, $htmlkey);
+      }
+      $spreadsheet->write(${$row_sref}++, 0, [@content, $key], $f_default) if $xls_fh;
+
+
+      if (scalar @chartdatanames < $ntopchart) {
+        if ($use_gig) {
+          if ($$m_data_gigs{$key}) {
+            push(@chartdatanames, $key);
+            push(@chartdatavals, $$m_data_gigs{$key});
+          }
+        }
+        else {
+          push(@chartdatanames, $key);
+          push(@chartdatavals, $$m_data{$key});
+        }
+      }
+      else {
+        $chartotherval += ($use_gig) ? $$m_data_gigs{$key} : $$m_data{$key};
+      }
+    }
+    push(@chartdatanames, "Other");
+    push(@chartdatavals, $chartotherval);
+
+    print $txt_fh "\n" if $txt_fh;
+    if ($htm_fh) {
+      print $htm_fh "</table>\n";
+      print $htm_fh "</td><td>\n";
+      if ($HAVE_GD_Graph_pie && $charts && ($#chartdatavals > 0)) {
+        # calculate the graph
+        my @data = (
+           \@chartdatanames,
+           \@chartdatavals
+        );
+        my $graph = GD::Graph::pie->new(300, 300);
+        $graph->set(
+            x_label           => 'Name',
+            y_label           => 'Volume' ,
+            title             => 'By Volume',
+        );
+        my $gd = $graph->plot(\@data) or warn($graph->error);
+        if ($gd) {
+          my $temp = $text;
+          $temp =~ s/ /_/g;
+          open(IMG, ">$chartdir/${temp}_volume.png") or die "Could not write $chartdir/${temp}_volume.png: $!\n";
+          binmode IMG;
+          print IMG $gd->png;
+          close IMG;
+          print $htm_fh "<img src=\"$chartrel/${temp}_volume.png\">";
+        }
+      }
+      print $htm_fh "</td><td>\n";
+      print $htm_fh "</td></tr></table>\n\n";
+    }
+
+    ++${$row_sref} if $xls_fh;
+  }
+}
+
+
+#######################################################################
+# top_n_sort();
+#
+#   @sorted_keys = top_n_sort($n,$href1,$href2,$href3);
+#
+# Given a hash which has numerical values, return the sorted $n keys which
+# point to the top values. The second and third hashes are used as
+# tiebreakers. They all must have the same keys.
+#
+# The idea behind this routine is that when you only want to see the
+# top n members of a set, rather than sorting the entire set and then
+# plucking off the top n, sort through the stack as you go, discarding
+# any member which is lower than your current n'th highest member.
+#
+# This proves to be an order of magnitude faster for large hashes.
+# On 200,000 lines of mainlog it benchmarked 9 times faster.
+# On 700,000 lines of mainlog it benchmarked 13.8 times faster.
+#
+# We assume the values are > 0.
+#######################################################################
+sub top_n_sort {
+  my($n,$href1,$href2,$href3) = @_;
+
+  # PH's original sort was:
+  #
+  # foreach $key (sort
+  #               {
+  #               $$m_count{$b}     <=> $$m_count{$a} ||
+  #               $$m_data_gigs{$b} <=> $$m_data_gigs{$a}  ||
+  #               $$m_data{$b}      <=> $$m_data{$a}  ||
+  #               $a cmp $b
+  #               }
+  #             keys %{$m_count})
+  #
+
+  #We use a key of '_' to represent non-existant values, as null keys are valid.
+  #'_' is not a valid domain, edomain, host, or email.
+  my(@top_n_keys) = ('_') x $n;
+  my($minimum_value1,$minimum_value2,$minimum_value3) = (0,0,0);
+  my $top_n_key = '';
+  my $n_minus_1 = $n - 1;
+  my $n_minus_2 = $n - 2;
+
+  # Create a dummy hash incase the user has not provided us with
+  # tiebreaker hashes.
+  my(%dummy_hash);
+  $href2 = \%dummy_hash unless defined $href2;
+  $href3 = \%dummy_hash unless defined $href3;
+
+  # Pick out the top $n keys.
+  my($key,$value1,$value2,$value3,$i,$comparison,$insert_position);
+  while (($key,$value1) = each %$href1) {
+
+    #print STDERR "key $key ($value1,",$href2->{$key},",",$href3->{$key},") <=> ($minimum_value1,$minimum_value2,$minimum_value3)\n";
+
+    # Check to see that the new value is bigger than the lowest of the
+    # top n keys that we're keeping. We test the main key first, because
+    # for the majority of cases we can skip creating dummy hash values
+    # should the user have not provided real tie-breaking hashes.
+    next unless $value1 >= $minimum_value1;
+
+    # Create a dummy hash entry for the key if required.
+    # Note that setting the dummy_hash value sets it for both href2 &
+    # href3. Also note that currently we are guarenteed to have a real
+    # value for href3 if a real value for href2 exists so don't need to
+    # test for it as well.
+    $dummy_hash{$key} = 0 unless exists $href2->{$key};
+
+    $comparison = $value1        <=> $minimum_value1 ||
+                  $href2->{$key} <=> $minimum_value2 ||
+                  $href3->{$key} <=> $minimum_value3 ||
+                  $top_n_key cmp $key;
+    next unless ($comparison == 1);
+
+    # As we will be using these values a few times, extract them into scalars.
+    $value2 = $href2->{$key};
+    $value3 = $href3->{$key};
+
+    # This key is bigger than the bottom n key, so the lowest position we
+    # will insert it into is $n minus 1 (the bottom of the list).
+    $insert_position = $n_minus_1;
+
+    # Now go through the list, stopping when we find a key that we're
+    # bigger than, or we come to the penultimate position - we've
+    # already tested bigger than the last.
+    #
+    # Note: we go top down as the list starts off empty.
+    # Note: stepping through the list in this way benchmarks nearly
+    # three times faster than doing a sort() on the reduced list.
+    # I assume this is because the list is already in order, and
+    # we get a performance boost from not having to do hash lookups
+    # on the new key.
+    for ($i = 0; $i < $n_minus_1; $i++) {
+      $top_n_key = $top_n_keys[$i];
+      if ( ($top_n_key eq '_') ||
+           ( ($value1 <=> $href1->{$top_n_key} ||
+              $value2 <=> $href2->{$top_n_key} ||
+              $value3 <=> $href3->{$top_n_key} ||
+              $top_n_key cmp $key) == 1
+           )
+         ) {
+        $insert_position = $i;
+        last;
+      }
+    }
+
+    # Remove the last element, then insert the new one.
+    $#top_n_keys = $n_minus_2;
+    splice(@top_n_keys,$insert_position,0,$key);
+
+    # Extract our new minimum values.
+    $top_n_key = $top_n_keys[$n_minus_1];
+    if ($top_n_key ne '_') {
+      $minimum_value1 = $href1->{$top_n_key};
+      $minimum_value2 = $href2->{$top_n_key};
+      $minimum_value3 = $href3->{$top_n_key};
+    }
+  }
+
+  # Return the top n list, grepping out non-existant values, just in case
+  # we didn't have that many values.
+  return(grep(!/^_$/,@top_n_keys));
+}
+
+
+
+#######################################################################
+# html_header();
+#
+#  $header = html_header($title);
+#
+# Print our HTML header and start the <body> block.
+#######################################################################
+sub html_header {
+  my($title) = @_;
+  my $text = << "EoText";
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
+<html>
+<head>
+<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-15">
+<title>$title</title>
+</head>
+<body bgcolor="white">
+<h1>$title</h1>
+EoText
+  return $text;
+}
+
+
+
+#######################################################################
+# help();
+#
+#  help();
+#
+# Display usage instructions and exit.
+#######################################################################
+sub help {
+  print << "EoText";
+
+eximstats Version $VERSION
+
+Usage:
+  eximstats [Output] [Options] mainlog1 mainlog2 ...
+  eximstats -merge -html [Options] report.1.html ... > weekly_rep.html
+
+Examples:
+  eximstats -html=eximstats.html mainlog1 mainlog2 ...
+  eximstats mainlog1 mainlog2 ... > report.txt
+
+Parses exim mainlog or syslog files and generates a statistical analysis
+of the messages processed.
+
+Valid output types are:
+-txt[=<file>]   plain text (default unless no other type is specified)
+-html[=<file>]  HTML
+-xls[=<file>]   Excel
+With no type and file given, defaults to -txt and STDOUT.
+
+Valid options are:
+-h<number>      histogram divisions per hour. The default is 1, and
+                0 suppresses histograms. Other valid values are:
+                2, 3, 5, 10, 15, 20, 30 or 60.
+-ne             don't display error information
+-nr             don't display relaying information
+-nr/pattern/    don't display relaying information that matches
+-nt             don't display transport information
+-nt/pattern/    don't display transport information that matches
+-nvr            don't do volume rounding. Display in bytes, not KB/MB/GB.
+-t<number>      display top <number> sources/destinations
+                default is 50, 0 suppresses top listing
+-tnl            omit local sources/destinations in top listing
+-t_remote_users show top user sources/destinations from non-local domains
+-q<list>        list of times for queuing information. -q0 suppresses.
+-show_rt<list>  Show the receipt times for all the messages.
+-show_dt<list>  Show the delivery times for all the messages.
+                <list> is an optional list of times in seconds.
+                Eg -show_rt1,2,4,8.
+
+-include_original_destination   show both the final and original
+                destinations in the results rather than just the final ones.
+
+-byhost         show results by sending host (default unless bydomain or
+                byemail is specified)
+-bydomain       show results by sending domain.
+-byemail        show results by sender's email address
+-byedomain      show results by sender's email domain
+-bylocaldomain  show results by local domain
+
+-pattern "Description" /pattern/
+                Count lines matching specified patterns and show them in
+                the results. It can be specified multiple times. Eg:
+                -pattern 'Refused connections' '/refused connection/'
+
+-merge          merge previously generated reports into a new report
+
+-charts         Create charts (this requires the GD::Graph modules).
+                Only valid with -html.
+-chartdir <dir> Create the charts' png files in the directory <dir>
+-chartrel <dir> Specify the relative directory for the "img src=" tags
+                from where to include the charts in the html file
+                -chartdir and -chartrel default to '.'
+
+-emptyok        It is OK if there is no valid input, don't print an error.
+
+-d              Debug mode - dump the eval'ed parser onto STDERR.
+
+EoText
+
+  exit 1;
+}
+
+
+
+#######################################################################
+# generate_parser();
+#
+#  $parser = generate_parser();
+#
+# This subroutine generates the parsing routine which will be
+# used to parse the mainlog. We take the base operation, and remove bits not in use.
+# This improves performance depending on what bits you take out or add.
+#
+# I've tested using study(), but this does not improve performance.
+#
+# We store our parsing routing in a variable, and process it looking for #IFDEF (Expression)
+# or #IFNDEF (Expression) statements and corresponding #ENDIF (Expression) statements. If
+# the expression evaluates to true, then it is included/excluded accordingly.
+#######################################################################
+sub generate_parser {
+  my $parser = '
+  my($ip,$host,$email,$edomain,$domain,$thissize,$size,$old,$new);
+  my($tod,$m_hour,$m_min,$id,$flag,$extra,$length);
+  my($seconds,$queued,$rcpt_time,$local_domain);
+  my $rej_id = 0;
+  while (<$fh>) {
+
+    # Convert syslog lines to mainlog format.
+    if (! /^\\d{4}/) {
+      next unless s/^.*? exim\\b.*?: //;
+    }
+
+    $length = length($_);
+    next if ($length < 38);
+    next unless /^(\\d{4}\\-\\d\\d-\\d\\d\\s(\\d\\d):(\\d\\d):\\d\\d( [-+]\\d\\d\\d\\d)?)( \\[\\d+\\])?/o;
+
+    ($tod,$m_hour,$m_min) = ($1,$2,$3);
+
+    # PH - watch for GMT offsets in the timestamp.
+    if (defined($4)) {
+      $extra = 6;
+      next if ($length < 44);
+    }
+    else {
+      $extra = 0;
+    }
+
+    # PH - watch for PID added after the timestamp.
+    if (defined($5)) {
+      $extra += length($5);
+      next if ($length < 38 + $extra);
+    }
+
+    $id   = substr($_, 20 + $extra, 16);
+    $flag = substr($_, 37 + $extra, 2);
+
+    if ($flag !~ /^([<>=*-]+|SA)$/ && /rejected|refused|dropped/) {
+      $flag = "Re";
+      $extra -= 3;
+    }
+
+    # Rejects can have no MSGID...
+    if ($flag eq "Re" && $id !~ /^[-0-9a-zA-Z]+$/) {
+      $id   = "reject:" . ++$rej_id;
+      $extra -= 17;
+    }
+';
+
+  # Watch for user specified patterns.
+  my $user_pattern_index = 0;
+  foreach (@user_patterns) {
+    $user_pattern_totals[$user_pattern_index] = 0;
+    $parser .= "    if ($_) {\n";
+    $parser .= "      \$user_pattern_totals[$user_pattern_index]++;\n";
+    $parser .= "      \$user_pattern_interval_count[$user_pattern_index][(\$m_hour*60 + \$m_min)/$hist_interval]++;\n" if ($hist_opt > 0);
+    $parser .= "    }\n";
+    $user_pattern_index++;
+  }
+
+  $parser .= '
+    next unless ($flag =~ /<=|=>|->|==|\\*\\*|Co|SA|Re/);
+
+    #Strip away the timestamp, ID and flag to speed up later pattern matches.
+    #The flags include Co (Completed), Re (Rejected), and SA (SpamAssassin).
+    $_ = substr($_, 40 + $extra);  # PH
+
+    # Alias @message to the array of information about the message.
+    # This minimises the number of calls to hash functions.
+    $messages{$id} = [] unless exists $messages{$id};
+    *message = $messages{$id};
+
+
+    # JN - Skip over certain transports as specified via the "-nt/.../" command
+    # line switch (where ... is a perl style regular expression).  This is
+    # required so that transports that skew stats such as SpamAssassin can be
+    # ignored.
+    #IFDEF ($transport_pattern)
+    if (/\\sT=(\\S+)/) {
+       next if ($1 =~ /$transport_pattern/o) ;
+    }
+    #ENDIF ($transport_pattern)
+
+
+
+    # Do some pattern matches to get the host and IP address.
+    # We expect lines to be of the form "H=[IpAddr]" or "H=Host [IpAddr]" or
+    # "H=Host (UnverifiedHost) [IpAddr]" or "H=(UnverifiedHost) [IpAddr]".
+    # We do 2 separate matches to keep the matches simple and fast.
+    # Host is local unless otherwise specified.
+    # Watch out for "H=([IpAddr])" in case they send "[IpAddr]" as their HELO!
+    $ip = (/\\bH=(?:|.*? )(\\[[^]]+\\])/) ? $1
+     # 2008-03-31 06:25:22 Connection from [213.246.33.217]:39456 refused: too many connections from that IP address // .hs
+     : (/Connection from (\[\S+\])/) ? $1
+     # 2008-03-31 06:52:40 SMTP call from mail.cacoshrf.com (ccsd02.ccsd.local) [69.24.118.229]:4511 dropped: too many nonmail commands (last was "RSET") // .hs
+     : (/SMTP call from .*?(\[\S+\])/) ? $1
+     : "local";
+    $host = (/\\bH=(\\S+)/) ? $1 : "local";
+
+    $domain = "localdomain";  #Domain is localdomain unless otherwise specified.
+
+    #IFDEF ($do_sender{Domain})
+    if ($host =~ /^\\[/ || $host =~ /^[\\d\\.]+$/) {
+      # Host is just an IP address.
+      $domain = $host;
+    }
+    elsif ($host =~ /^(\\(?)[^\\.]+\\.([^\\.]+\\..*)/) {
+      # Remove the host portion from the DNS name. We ensure that we end up
+      # with at least xxx.yyy. $host can be "(x.y.z)" or  "x.y.z".
+      $domain = lc("$1.$2");
+      $domain =~ s/^\\.//;         #Remove preceding dot.
+    }
+    #ENDIF ($do_sender{Domain})
+
+    #IFDEF ($do_sender{Email})
+      #IFDEF ($include_original_destination)
+      # Catch both "a@b.com <c@d.com>" and "e@f.com"
+      #$email = (/^(\S+) (<(\S*?)>)?/) ? $3 || $1 : "";
+      $email = (/^(\S+ (<[^@>]+@?[^>]*>)?)/) ? $1 : "";
+      chomp($email);
+      #ENDIF ($include_original_destination)
+
+      #IFNDEF ($include_original_destination)
+      $email = (/^(\S+)/) ? $1 : "";
+      #ENDIF ($include_original_destination)
+    #ENDIF ($do_sender{Email})
+
+    #IFDEF ($do_sender{Edomain})
+      if (/^(<>|blackhole)/) {
+        $edomain = $1;
+      }
+      #IFDEF ($include_original_destination)
+        elsif (/^(\S+ (<\S*?\\@(\S+?)>)?)/) {
+          $edomain = $1;
+          chomp($edomain);
+          $edomain =~ s/@(\S+?)>/"@" . lc($1) . ">"/e;
+        }
+      #ENDIF ($include_original_destination)
+      #IFNDEF ($include_original_destination)
+        elsif (/^\S*?\\@(\S+)/) {
+          $edomain = lc($1);
+        }
+      #ENDIF ($include_original_destination)
+      else {
+        $edomain = "";
+      }
+
+    #ENDIF ($do_sender{Edomain})
+
+    if ($tod lt $begin) {
+      $begin = $tod;
+    }
+    elsif ($tod gt $end) {
+      $end   = $tod;
+    }
+
+
+    if ($flag eq "<=") {
+      $thissize = (/\\sS=(\\d+)( |$)/) ? $1 : 0;
+      $message[$SIZE] = $thissize;
+      $message[$PROTOCOL] = (/ P=(\S+)/) ? $1 : undef;
+
+      #IFDEF ($show_relay)
+      if ($host ne "local") {
+        # Save incoming information in case it becomes interesting
+        # later, when delivery lines are read.
+        my($from) = /^(\\S+)/;
+        $message[$FROM_HOST]    = "$host$ip";
+        $message[$FROM_ADDRESS] = $from;
+      }
+      #ENDIF ($show_relay)
+
+      #IFDEF ($local_league_table || $include_remote_users)
+        if (/\sU=(\\S+)/) {
+          my $user = $1;
+
+          #IFDEF ($local_league_table && $include_remote_users)
+          {                         #Store both local and remote users.
+          #ENDIF ($local_league_table && $include_remote_users)
+
+          #IFDEF ($local_league_table && ! $include_remote_users)
+          if ($host eq "local") {   #Store local users only.
+          #ENDIF ($local_league_table && ! $include_remote_users)
+
+          #IFDEF ($include_remote_users && ! $local_league_table)
+          if ($host ne "local") {   #Store remote users only.
+          #ENDIF ($include_remote_users && ! $local_league_table)
+
+            ++$received_count_user{$user};
+            add_volume(\\$received_data_user{$user},\\$received_data_gigs_user{$user},$thissize);
+          }
+        }
+      #ENDIF ($local_league_table || $include_remote_users)
+
+      #IFDEF ($do_sender{Host})
+        ++$received_count{Host}{$host};
+        add_volume(\\$received_data{Host}{$host},\\$received_data_gigs{Host}{$host},$thissize);
+      #ENDIF ($do_sender{Host})
+
+      #IFDEF ($do_sender{Domain})
+        if ($domain) {
+          ++$received_count{Domain}{$domain};
+          add_volume(\\$received_data{Domain}{$domain},\\$received_data_gigs{Domain}{$domain},$thissize);
+        }
+      #ENDIF ($do_sender{Domain})
+
+      #IFDEF ($do_sender{Email})
+        ++$received_count{Email}{$email};
+        add_volume(\\$received_data{Email}{$email},\\$received_data_gigs{Email}{$email},$thissize);
+      #ENDIF ($do_sender{Email})
+
+      #IFDEF ($do_sender{Edomain})
+        ++$received_count{Edomain}{$edomain};
+        add_volume(\\$received_data{Edomain}{$edomain},\\$received_data_gigs{Edomain}{$edomain},$thissize);
+      #ENDIF ($do_sender{Edomain})
+
+      ++$total_received_count;
+      add_volume(\\$total_received_data,\\$total_received_data_gigs,$thissize);
+
+      #IFDEF ($#queue_times >= 0 || $#rcpt_times >= 0)
+        $message[$ARRIVAL_TIME] = $tod;
+      #ENDIF ($#queue_times >= 0 || $#rcpt_times >= 0)
+
+      #IFDEF ($hist_opt > 0)
+        $received_interval_count[($m_hour*60 + $m_min)/$hist_interval]++;
+      #ENDIF ($hist_opt > 0)
+    }
+
+    elsif ($flag eq "=>") {
+      $size = $message[$SIZE] || 0;
+      if ($host ne "local") {
+        $message[$REMOTE_DELIVERED] = 1;
+
+
+        #IFDEF ($show_relay)
+        # Determine relaying address if either only one address listed,
+        # or two the same. If they are different, it implies a forwarding
+        # or aliasing, which is not relaying. Note that for multi-aliased
+        # addresses, there may be a further address between the first
+        # and last.
+
+        if (defined $message[$FROM_HOST]) {
+          if (/^(\\S+)(?:\\s+\\([^)]\\))?\\s+<([^>]+)>/) {
+            ($old,$new) = ($1,$2);
+          }
+          else {
+            $old = $new = "";
+          }
+
+          if ("\\L$new" eq "\\L$old") {
+            ($old) = /^(\\S+)/ if $old eq "";
+            my $key = "H=\\L$message[$FROM_HOST]\\E A=\\L$message[$FROM_ADDRESS]\\E => " .
+              "H=\\L$host\\E$ip A=\\L$old\\E";
+            if (!defined $relay_pattern || $key !~ /$relay_pattern/o) {
+              $relayed{$key} = 0 if !defined $relayed{$key};
+              ++$relayed{$key};
+            }
+            else {
+              ++$relayed_unshown;
+            }
+          }
+        }
+        #ENDIF ($show_relay)
+
+      }
+
+      #IFDEF ($local_league_table || $include_remote_users)
+        #IFDEF ($local_league_table && $include_remote_users)
+        {                         #Store both local and remote users.
+        #ENDIF ($local_league_table && $include_remote_users)
+
+        #IFDEF ($local_league_table && ! $include_remote_users)
+        if ($host eq "local") {   #Store local users only.
+        #ENDIF ($local_league_table && ! $include_remote_users)
+
+        #IFDEF ($include_remote_users && ! $local_league_table)
+        if ($host ne "local") {   #Store remote users only.
+        #ENDIF ($include_remote_users && ! $local_league_table)
+
+          if (my($user) = split((/\\s</)? " <" : " ", $_)) {
+            #IFDEF ($include_original_destination)
+            {
+            #ENDIF ($include_original_destination)
+            #IFNDEF ($include_original_destination)
+            if ($user =~ /^[\\/|]/) {
+            #ENDIF ($include_original_destination)
+              #my($parent) = $_ =~ /(<[^@]+@?[^>]*>)/;
+              my($parent) = $_ =~ / (<.+?>) /;              #DT 1.54
+              if (defined $parent) {
+                $user = "$user $parent";
+                #IFDEF ($do_local_domain)
+                if ($parent =~ /\\@(.+)>/) {
+                  $local_domain = lc($1);
+                  ++$delivered_messages_local_domain{$local_domain};
+                  ++$delivered_addresses_local_domain{$local_domain};
+                  add_volume(\\$delivered_data_local_domain{$local_domain},\\$delivered_data_gigs_local_domain{$local_domain},$size);
+                }
+                #ENDIF ($do_local_domain)
+              }
+            }
+            ++$delivered_messages_user{$user};
+            ++$delivered_addresses_user{$user};
+            add_volume(\\$delivered_data_user{$user},\\$delivered_data_gigs_user{$user},$size);
+          }
+        }
+      #ENDIF ($local_league_table || $include_remote_users)
+
+      #IFDEF ($do_sender{Host})
+        $delivered_messages{Host}{$host}++;
+        $delivered_addresses{Host}{$host}++;
+        add_volume(\\$delivered_data{Host}{$host},\\$delivered_data_gigs{Host}{$host},$size);
+      #ENDIF ($do_sender{Host})
+      #IFDEF ($do_sender{Domain})
+        if ($domain) {
+          ++$delivered_messages{Domain}{$domain};
+          ++$delivered_addresses{Domain}{$domain};
+          add_volume(\\$delivered_data{Domain}{$domain},\\$delivered_data_gigs{Domain}{$domain},$size);
+        }
+      #ENDIF ($do_sender{Domain})
+      #IFDEF ($do_sender{Email})
+        ++$delivered_messages{Email}{$email};
+        ++$delivered_addresses{Email}{$email};
+        add_volume(\\$delivered_data{Email}{$email},\\$delivered_data_gigs{Email}{$email},$size);
+      #ENDIF ($do_sender{Email})
+      #IFDEF ($do_sender{Edomain})
+        ++$delivered_messages{Edomain}{$edomain};
+        ++$delivered_addresses{Edomain}{$edomain};
+        add_volume(\\$delivered_data{Edomain}{$edomain},\\$delivered_data_gigs{Edomain}{$edomain},$size);
+      #ENDIF ($do_sender{Edomain})
+
+      ++$total_delivered_messages;
+      ++$total_delivered_addresses;
+      add_volume(\\$total_delivered_data,\\$total_delivered_data_gigs,$size);
+
+      #IFDEF ($show_transport)
+        my $transport = (/\\sT=(\\S+)/) ? $1 : ":blackhole:";
+        ++$transported_count{$transport};
+        add_volume(\\$transported_data{$transport},\\$transported_data_gigs{$transport},$size);
+      #ENDIF ($show_transport)
+
+      #IFDEF ($hist_opt > 0)
+        $delivered_interval_count[($m_hour*60 + $m_min)/$hist_interval]++;
+      #ENDIF ($hist_opt > 0)
+
+      #IFDEF ($#delivery_times > 0)
+        if (/ DT=(\S+)/) {
+          $seconds = wdhms_seconds($1);
+          for ($i = 0; $i <= $#delivery_times; $i++) {
+            if ($seconds < $delivery_times[$i]) {
+              ++$dt_all_bin[$i];
+              ++$dt_remote_bin[$i] if $message[$REMOTE_DELIVERED];
+              last;
+            }
+          }
+          if ($i > $#delivery_times) {
+            ++$dt_all_overflow;
+            ++$dt_remote_overflow if $message[$REMOTE_DELIVERED];
+          }
+        }
+      #ENDIF ($#delivery_times > 0)
+
+    }
+
+    elsif ($flag eq "->") {
+
+      #IFDEF ($local_league_table || $include_remote_users)
+        #IFDEF ($local_league_table && $include_remote_users)
+        {                         #Store both local and remote users.
+        #ENDIF ($local_league_table && $include_remote_users)
+
+        #IFDEF ($local_league_table && ! $include_remote_users)
+        if ($host eq "local") {   #Store local users only.
+        #ENDIF ($local_league_table && ! $include_remote_users)
+
+        #IFDEF ($include_remote_users && ! $local_league_table)
+        if ($host ne "local") {   #Store remote users only.
+        #ENDIF ($include_remote_users && ! $local_league_table)
+
+          if (my($user) = split((/\\s</)? " <" : " ", $_)) {
+            #IFDEF ($include_original_destination)
+            {
+            #ENDIF ($include_original_destination)
+            #IFNDEF ($include_original_destination)
+            if ($user =~ /^[\\/|]/) {
+            #ENDIF ($include_original_destination)
+              #my($parent) = $_ =~ /(<[^@]+@?[^>]*>)/;
+              my($parent) = $_ =~ / (<.+?>) /;              #DT 1.54
+              $user = "$user $parent" if defined $parent;
+            }
+            ++$delivered_addresses_user{$user};
+          }
+        }
+      #ENDIF ($local_league_table || $include_remote_users)
+
+      #IFDEF ($do_sender{Host})
+        $delivered_addresses{Host}{$host}++;
+      #ENDIF ($do_sender{Host})
+      #IFDEF ($do_sender{Domain})
+        if ($domain) {
+          ++$delivered_addresses{Domain}{$domain};
+        }
+      #ENDIF ($do_sender{Domain})
+      #IFDEF ($do_sender{Email})
+        ++$delivered_addresses{Email}{$email};
+      #ENDIF ($do_sender{Email})
+      #IFDEF ($do_sender{Edomain})
+        ++$delivered_addresses{Edomain}{$edomain};
+      #ENDIF ($do_sender{Edomain})
+
+      ++$total_delivered_addresses;
+    }
+
+    elsif ($flag eq "==" && defined($message[$SIZE]) && !defined($message[$DELAYED])) {
+      ++$delayed_count;
+      $message[$DELAYED] = 1;
+    }
+
+    elsif ($flag eq "**") {
+      if (defined ($message[$SIZE])) {
+        unless (defined $message[$HAD_ERROR]) {
+          ++$message_errors;
+          $message[$HAD_ERROR] = 1;
+        }
+      }
+
+      #IFDEF ($show_errors)
+        ++$errors_count{$_};
+      #ENDIF ($show_errors)
+
+    }
+
+    elsif ($flag eq "Co") {
+      #Completed?
+      #IFDEF ($#queue_times >= 0)
+        $queued = queue_time($tod, $message[$ARRIVAL_TIME], $id);
+
+        for ($i = 0; $i <= $#queue_times; $i++) {
+          if ($queued < $queue_times[$i]) {
+            ++$qt_all_bin[$i];
+            ++$qt_remote_bin[$i] if $message[$REMOTE_DELIVERED];
+            last;
+          }
+        }
+        if ($i > $#queue_times) {
+          ++$qt_all_overflow;
+          ++$qt_remote_overflow if $message[$REMOTE_DELIVERED];
+        }
+      #ENDIF ($#queue_times >= 0)
+
+      #IFDEF ($#rcpt_times >= 0)
+        if (/ QT=(\S+)/) {
+          $seconds = wdhms_seconds($1);
+          #Calculate $queued if not previously calculated above.
+          #IFNDEF ($#queue_times >= 0)
+            $queued = queue_time($tod, $message[$ARRIVAL_TIME], $id);
+          #ENDIF ($#queue_times >= 0)
+          $rcpt_time = $seconds - $queued;
+          my($protocol);
+
+          if (defined $message[$PROTOCOL]) {
+            $protocol = $message[$PROTOCOL];
+
+            # Create the bin if its not already defined.
+            unless (exists $rcpt_times_bin{$protocol}) {
+              initialise_rcpt_times($protocol);
+            }
+          }
+
+
+          for ($i = 0; $i <= $#rcpt_times; ++$i) {
+            if ($rcpt_time < $rcpt_times[$i]) {
+              ++$rcpt_times_bin{all}[$i];
+              ++$rcpt_times_bin{$protocol}[$i] if defined $protocol;
+              last;
+            }
+          }
+
+          if ($i > $#rcpt_times) {
+            ++$rcpt_times_overflow{all};
+            ++$rcpt_times_overflow{$protocol} if defined $protocol;
+          }
+        }
+      #ENDIF ($#rcpt_times >= 0)
+
+      delete($messages{$id});
+    }
+    elsif ($flag eq "SA") {
+      $ip = (/From.*?(\\[[^]]+\\])/ || /\\((local)\\)/) ? $1 : "";
+      #SpamAssassin message
+      if (/Action: ((permanently|temporarily) rejected message|flagged as Spam but accepted): score=(\d+\.\d)/) {
+        #add_volume(\\$spam_score,\\$spam_score_gigs,$3);
+        ++$spam_count_by_ip{$ip};
+      } elsif (/Action: scanned but message isn\'t spam: score=(-?\d+\.\d)/) {
+        #add_volume(\\$ham_score,\\$ham_score_gigs,$1);
+        ++$ham_count_by_ip{$ip};
+      } elsif (/(Not running SA because SAEximRunCond expanded to false|check skipped due to message size)/) {
+        ++$ham_count_by_ip{$ip};
+      }
+    }
+
+    # Look for Reject messages or blackholed messages (deliveries
+    # without a transport)
+    if ($flag eq "Re" || ($flag eq "=>" && ! /\\sT=\\S+/)) {
+      # Correct the IP address for rejects:
+      # rejected EHLO from my.test.net [10.0.0.5]: syntactically invalid argument(s):
+      # rejected EHLO from [10.0.0.6]: syntactically invalid argument(s):
+      $ip = $1 if ($ip eq "local" && /^rejected [HE][HE]LO from .*?(\[.+?\]):/);
+      if (/SpamAssassin/) {
+        ++$rejected_count_by_reason{"Rejected by SpamAssassin"};
+        ++$rejected_count_by_ip{$ip};
+      }
+      elsif (
+        /(temporarily rejected [A-Z]*) .*?(: .*?)(:|\s*$)/
+        ) {
+        ++$temporarily_rejected_count_by_reason{"\u$1$2"};
+        ++$temporarily_rejected_count_by_ip{$ip};
+      }
+      elsif (
+        /(temporarily refused connection)/
+        ) {
+        ++$temporarily_rejected_count_by_reason{"\u$1"};
+        ++$temporarily_rejected_count_by_ip{$ip};
+      }
+      elsif (
+        /(listed at [^ ]+)/ ||
+        /(Forged IP detected in HELO)/ ||
+        /(Invalid domain or IP given in HELO\/EHLO)/ ||
+        /(unqualified recipient rejected)/ ||
+        /(closed connection (after|in response) .*?)\s*$/ ||
+        /(sender rejected)/ ||
+        # 2005-09-23 15:07:49 1EInHJ-0007Ex-Au H=(a.b.c) [10.0.0.1] F=<> rejected after DATA: This message contains a virus: (Eicar-Test-Signature) please scan your system.
+        # 2005-10-06 10:50:07 1ENRS3-0000Nr-Kt => blackhole (DATA ACL discarded recipients): This message contains a virus: (Worm.SomeFool.P) please scan your system.
+        / rejected after DATA: (.*)/ ||
+        / (rejected DATA: .*)/ ||
+        /.DATA ACL discarded recipients.: (.*)/ ||
+        /rejected after DATA: (unqualified address not permitted)/ ||
+        /(VRFY rejected)/ ||
+#        /(sender verify (defer|fail))/i ||
+        /(too many recipients)/ ||
+        /(refused relay.*?) to/ ||
+        /(rejected by non-SMTP ACL: .*)/ ||
+        /(rejected by local_scan.*)/ ||
+        # SMTP call from %s dropped: too many syntax or protocol errors (last command was "%s"
+        # SMTP call from %s dropped: too many nonmail commands
+        /(dropped: too many ((nonmail|unrecognized) commands|syntax or protocol errors))/ ||
+
+        # local_scan() function crashed with signal %d - message temporarily rejected
+        # local_scan() function timed out - message temporarily rejected
+        /(local_scan.. function .* - message temporarily rejected)/ ||
+        # SMTP protocol synchronization error (input sent without waiting for greeting): rejected connection from %s
+        /(SMTP protocol .*?(error|violation))/ ||
+        /(message too big)/
+        ) {
+        ++$rejected_count_by_reason{"\u$1"};
+        ++$rejected_count_by_ip{$ip};
+      }
+      elsif (/rejected [HE][HE]LO from [^:]*: syntactically invalid argument/) {
+        ++$rejected_count_by_reason{"Rejected HELO/EHLO: syntactically invalid argument"};
+        ++$rejected_count_by_ip{$ip};
+      }
+      elsif (/response to "RCPT TO.*? was: (.*)/) {
+        ++$rejected_count_by_reason{"Response to RCPT TO was: $1"};
+        ++$rejected_count_by_ip{$ip};
+      }
+      elsif (
+        /(lookup of host )\S+ (failed)/ ||
+
+        # rejected from <%s>%s%s%s%s: message too big:
+        /(rejected [A-Z]*) .*?(: .*?)(:|\s*$)/ ||
+        # refused connection from %s (host_reject_connection)
+        # refused connection from %s (tcp wrappers)
+        /(refused connection )from.*? (\(.*)/ ||
+
+        # error from remote mailer after RCPT TO:<a@b.c>: host a.b.c [10.0.0.1]: 450 <a@b.c>: Recipient address rejected: Greylisted for 60 seconds
+        # error from remote mailer after MAIL FROM:<> SIZE=3468: host a.b.c [10.0.0.1]: 421 a.b.c has refused your connection because your server did not have a PTR record.
+        /(error from remote mailer after .*?:).*(: .*?)(:|\s*$)/ ||
+
+        # a.b.c F=<a@b.c> rejected after DATA: "@" or "." expected after "Undisclosed-Recipient": failing address in "To" header is: <Undisclosed-Recipient:;>
+        /rejected after DATA: ("." or "." expected).*?(: failing address in .*? header)/ ||
+
+        # connection from %s refused load average = %.2f
+        /(Connection )from.*? (refused: load average)/ ||
+        # connection from %s refused (IP options)
+        # Connection from %s refused: too many connections
+        # connection from %s refused
+        /([Cc]onnection )from.*? (refused.*)/ ||
+        # [10.0.0.1]: connection refused
+        /: (Connection refused)()/
+        ) {
+        ++$rejected_count_by_reason{"\u$1$2"};
+        ++$rejected_count_by_ip{$ip};
+      }
+      elsif (
+        # 2008-03-31 06:25:22 H=mail.densitron.com [216.70.140.224]:45386 temporarily rejected connection in "connect" ACL: too fast reconnects // .hs
+        # 2008-03-31 06:25:22 H=mail.densitron.com [216.70.140.224]:45386 temporarily rejected connection in "connect" ACL // .hs
+        /(temporarily rejected connection in .*?ACL:?.*)/
+        ) {
+        ++$temporarily_rejected_count_by_ip{$ip};
+        ++$temporarily_rejected_count_by_reason{"\u$1"};
+      }
+      else {
+        ++$rejected_count_by_reason{Unknown};
+        ++$rejected_count_by_ip{$ip};
+        print STDERR "Unknown rejection: $_" if $debug;
+      }
+    }
+  }';
+
+  # We now do a 'C preprocessor style operation on our parser
+  # to remove bits not in use.
+  my(%defines_in_operation,$removing_lines,$processed_parser);
+  foreach (split (/\n/,$parser)) {
+    if ((/^\s*#\s*IFDEF\s*\((.*?)\)/i  && ! eval $1) ||
+        (/^\s*#\s*IFNDEF\s*\((.*?)\)/i &&   eval $1)    ) {
+      $defines_in_operation{$1} = 1;
+      $removing_lines = 1;
+    }
+
+    # Convert constants.
+    while (/(\$[A-Z][A-Z_]*)\b/) {
+      my $constant = eval $1;
+      s/(\$[A-Z][A-Z_]*)\b/$constant/;
+    }
+
+    $processed_parser .= $_."\n" unless $removing_lines;
+
+    if (/^\s*#\s*ENDIF\s*\((.*?)\)/i) {
+      delete $defines_in_operation{$1};
+      unless (keys %defines_in_operation) {
+        $removing_lines = 0;
+      }
+    }
+  }
+  print STDERR "# START OF PARSER:$processed_parser\n# END OF PARSER\n\n" if $debug;
+
+  return $processed_parser;
+}
+
+
+
+#######################################################################
+# parse();
+#
+#  parse($parser,\*FILEHANDLE);
+#
+# This subroutine accepts a parser and a filehandle from main and parses each
+# line. We store the results into global variables.
+#######################################################################
+sub parse {
+  my($parser,$fh) = @_;
+
+  if ($merge_reports) {
+    parse_old_eximstat_reports($fh);
+  }
+  else {
+    eval $parser;
+    die ($@) if $@;
+  }
+
+}
+
+
+
+#######################################################################
+# print_header();
+#
+#  print_header();
+#
+# Print our headers and contents.
+#######################################################################
+sub print_header {
+
+
+  my $title = "Exim statistics from $begin to $end";
+
+  print $txt_fh "\n$title\n" if $txt_fh;
+  if ($htm_fh) {
+    print $htm_fh html_header($title);
+    print $htm_fh "<ul>\n";
+    print $htm_fh "<li><a href=\"#Grandtotal\">Grand total summary</a>\n";
+    print $htm_fh "<li><a href=\"#Patterns\">User Specified Patterns</a>\n" if @user_patterns;
+    print $htm_fh "<li><a href=\"#Transport\">Deliveries by Transport</a>\n" if $show_transport;
+    if ($hist_opt) {
+      print $htm_fh "<li><a href=\"#Messages received\">Messages received per hour</a>\n";
+      print $htm_fh "<li><a href=\"#Deliveries\">Deliveries per hour</a>\n";
+    }
+
+    if ($#queue_times >= 0) {
+      print $htm_fh "<li><a href=\"#Time spent on the queue all messages\">Time spent on the queue: all messages</a>\n";
+      print $htm_fh "<li><a href=\"#Time spent on the queue messages with at least one remote delivery\">Time spent on the queue: messages with at least one remote delivery</a>\n";
+    }
+
+    if ($#delivery_times >= 0) {
+      print $htm_fh "<li><a href=\"#Delivery times all messages\">Delivery times: all messages</a>\n";
+      print $htm_fh "<li><a href=\"#Delivery times messages with at least one remote delivery\">Delivery times: messages with at least one remote delivery</a>\n";
+    }
+
+    if ($#rcpt_times >= 0) {
+      print $htm_fh "<li><a href=\"#Receipt times all messages\">Receipt times</a>\n";
+    }
+
+    print $htm_fh "<li><a href=\"#Relayed messages\">Relayed messages</a>\n" if $show_relay;
+    if ($topcount) {
+      print $htm_fh "<li><a href=\"#Mail rejection reason count\">Top $topcount mail rejection reasons by message count</a>\n" if %rejected_count_by_reason;
+      foreach ('Host','Domain','Email','Edomain') {
+        next unless $do_sender{$_};
+        print $htm_fh "<li><a href=\"#Sending \l$_ count\">Top $topcount sending \l${_}s by message count</a>\n";
+        print $htm_fh "<li><a href=\"#Sending \l$_ volume\">Top $topcount sending \l${_}s by volume</a>\n";
+      }
+      if (($local_league_table || $include_remote_users) && %received_count_user) {
+        print $htm_fh "<li><a href=\"#Local sender count\">Top $topcount local senders by message count</a>\n";
+        print $htm_fh "<li><a href=\"#Local sender volume\">Top $topcount local senders by volume</a>\n";
+      }
+      foreach ('Host','Domain','Email','Edomain') {
+        next unless $do_sender{$_};
+        print $htm_fh "<li><a href=\"#$_ destination count\">Top $topcount \l$_ destinations by message count</a>\n";
+        print $htm_fh "<li><a href=\"#$_ destination volume\">Top $topcount \l$_ destinations by volume</a>\n";
+      }
+      if (($local_league_table || $include_remote_users) && %delivered_messages_user) {
+        print $htm_fh "<li><a href=\"#Local destination count\">Top $topcount local destinations by message count</a>\n";
+        print $htm_fh "<li><a href=\"#Local destination volume\">Top $topcount local destinations by volume</a>\n";
+      }
+      if (($local_league_table || $include_remote_users) && %delivered_messages_local_domain) {
+        print $htm_fh "<li><a href=\"#Local domain destination count\">Top $topcount local domain destinations by message count</a>\n";
+        print $htm_fh "<li><a href=\"#Local domain destination volume\">Top $topcount local domain destinations by volume</a>\n";
+      }
+
+      print $htm_fh "<li><a href=\"#Rejected ip count\">Top $topcount rejected ips by message count</a>\n" if %rejected_count_by_ip;
+      print $htm_fh "<li><a href=\"#Temporarily rejected ip count\">Top $topcount temporarily rejected ips by message count</a>\n" if %temporarily_rejected_count_by_ip;
+      print $htm_fh "<li><a href=\"#Non-rejected spamming ip count\">Top $topcount non-rejected spamming ips by message count</a>\n" if %spam_count_by_ip;
+
+    }
+    print $htm_fh "<li><a href=\"#errors\">List of errors</a>\n" if %errors_count;
+    print $htm_fh "</ul>\n<hr>\n";
+  }
+  if ($xls_fh)
+  {
+    $ws_global->write($row++, $col+0, "Exim Statistics",  $f_header1);
+    &set_worksheet_line($ws_global, $row, $col, ["from:",  $begin,  "to:", $end], $f_default);
+    $row+=2;
+  }
+}
+
+
+#######################################################################
+# print_grandtotals();
+#
+#  print_grandtotals();
+#
+# Print the grand totals.
+#######################################################################
+sub print_grandtotals {
+
+  # Get the sender by headings and results. This is complicated as we can have
+  # different numbers of columns.
+  my($sender_txt_header,$sender_txt_format,$sender_html_format);
+  my(@received_totals,@delivered_totals);
+  my($row_tablehead, $row_max);
+  my(@col_headers) = ('TOTAL', 'Volume', 'Messages', 'Addresses');
+
+  foreach ('Host','Domain','Email','Edomain') {
+    next unless $do_sender{$_};
+    if ($merge_reports) {
+      push(@received_totals, get_report_total($report_totals{Received},"${_}s"));
+      push(@delivered_totals,get_report_total($report_totals{Delivered},"${_}s"));
+    }
+    else {
+      push(@received_totals,scalar(keys %{$received_data{$_}}));
+      push(@delivered_totals,scalar(keys %{$delivered_data{$_}}));
+    }
+    $sender_txt_header  .= " " x ($COLUMN_WIDTHS - length($_)) . $_ . 's';
+    $sender_html_format .= "<td align=\"right\">%s</td>";
+    $sender_txt_format  .= " " x ($COLUMN_WIDTHS - 5) . "%6s";
+    push(@col_headers,"${_}s");
+  }
+
+  my $txt_format1 = "  %-16s %9s     %6d    %6s $sender_txt_format";
+  my $txt_format2 = "  %6d %4.1f%% %6d %4.1f%%",
+  my $htm_format1 = "<tr><td>%s</td><td align=\"right\">%s</td><td align=\"right\">%s</td><td align=\"right\">%s</td>$sender_html_format";
+  my $htm_format2 = "<td align=\"right\">%d</td><td align=\"right\">%4.1f%%</td><td align=\"right\">%d</td><td align=\"right\">%4.1f%%</td>";
+
+  if ($txt_fh) {
+    my $sender_spaces = " " x length($sender_txt_header);
+    print $txt_fh "\n";
+    print $txt_fh "Grand total summary\n";
+    print $txt_fh "-------------------\n";
+    print $txt_fh "                                              $sender_spaces           At least one address\n";
+    print $txt_fh "  TOTAL               Volume   Messages Addresses $sender_txt_header      Delayed       Failed\n";
+  }
+  if ($htm_fh) {
+    print $htm_fh "<a name=\"Grandtotal\"></a>\n";
+    print $htm_fh "<h2>Grand total summary</h2>\n";
+    print $htm_fh "<table border=1>\n";
+    print $htm_fh "<tr><th>" . join('</th><th>',@col_headers) . "</th><th colspan=2>At least one addr<br>Delayed</th><th colspan=2>At least one addr<br>Failed</th>\n";
+  }
+  if ($xls_fh) {
+    $ws_global->write($row++, 0, "Grand total summary", $f_header2);
+    $ws_global->write($row, 0, \@col_headers, $f_header2);
+    $ws_global->merge_range($row, scalar(@col_headers), $row, scalar(@col_headers)+1, "At least one addr Delayed", $f_header2_m);
+    $ws_global->merge_range($row, scalar(@col_headers)+2, $row, scalar(@col_headers)+3, "At least one addr Failed", $f_header2_m);
+    #$ws_global->write(++$row, scalar(@col_headers), ['Total','Percent','Total','Percent'], $f_header2);
+  }
+
+
+  my($volume,$failed_count);
+  if ($merge_reports) {
+    $volume = volume_rounded($report_totals{Received}{Volume}, $report_totals{Received}{'Volume-gigs'});
+    $total_received_count = get_report_total($report_totals{Received},'Messages');
+    $failed_count  = get_report_total($report_totals{Received},'Failed');
+    $delayed_count = get_report_total($report_totals{Received},'Delayed');
+  }
+  else {
+    $volume = volume_rounded($total_received_data, $total_received_data_gigs);
+    $failed_count = $message_errors;
+  }
+
+  {
+    no integer;
+
+    my @content=(
+        $volume,$total_received_count,'',
+        @received_totals,
+        $delayed_count,
+        ($total_received_count) ? ($delayed_count*100/$total_received_count) : 0,
+        $failed_count,
+        ($total_received_count) ? ($failed_count*100/$total_received_count) : 0
+    );
+
+    printf $txt_fh ("$txt_format1$txt_format2\n", 'Received', @content) if $txt_fh;
+    printf $htm_fh ("$htm_format1$htm_format2\n", 'Received', @content) if $htm_fh;
+    if ($xls_fh) {
+      $ws_global->write(++$row, 0, 'Received', $f_default);
+      for (my $i=0; $i < scalar(@content); $i++) {
+        if ($i == 4 || $i == 6) {
+          $ws_global->write($row, $i+1, $content[$i]/100, $f_percent);
+        }
+        else {
+          $ws_global->write($row, $i+1, $content[$i], $f_default);
+        }
+      }
+    }
+  }
+
+  if ($merge_reports) {
+    $volume = volume_rounded($report_totals{Delivered}{Volume}, $report_totals{Delivered}{'Volume-gigs'});
+    $total_delivered_messages = get_report_total($report_totals{Delivered},'Messages');
+    $total_delivered_addresses = get_report_total($report_totals{Delivered},'Addresses');
+  }
+  else {
+    $volume = volume_rounded($total_delivered_data, $total_delivered_data_gigs);
+  }
+
+  my @content=($volume, $total_delivered_messages, $total_delivered_addresses, @delivered_totals);
+  printf $txt_fh ("$txt_format1\n", 'Delivered', @content) if $txt_fh;
+  printf $htm_fh ("$htm_format1\n", 'Delivered', @content) if $htm_fh;
+
+  if ($xls_fh) {
+    $ws_global->write(++$row, 0, 'Delivered', $f_default);
+    for (my $i=0; $i < scalar(@content); $i++) {
+      $ws_global->write($row, $i+1, $content[$i], $f_default);
+    }
+  }
+
+  if ($merge_reports) {
+    foreach ('Rejects', 'Temp Rejects', 'Ham', 'Spam') {
+      my $messages = get_report_total($report_totals{$_},'Messages');
+      my $addresses = get_report_total($report_totals{$_},'Addresses');
+      if ($messages) {
+        @content = ($_, '', $messages, '');
+        push(@content,get_report_total($report_totals{$_},'Hosts')) if $do_sender{Host};
+        #These rows do not have entries for the following columns (if specified)
+        foreach ('Domain','Email','Edomain') {
+          push(@content,'') if $do_sender{$_};
+        }
+
+        printf $txt_fh ("$txt_format1\n", @content) if $txt_fh;
+        printf $htm_fh ("$htm_format1\n", @content) if $htm_fh;
+        $ws_global->write(++$row, 0, \@content) if $xls_fh;
+      }
+    }
+  }
+  else {
+    foreach my $total_aref (['Rejects',\%rejected_count_by_ip],
+                            ['Temp Rejects',\%temporarily_rejected_count_by_ip],
+                            ['Ham',\%ham_count_by_ip],
+                            ['Spam',\%spam_count_by_ip]) {
+      #Count the number of messages of this type.
+      my $messages = 0;
+      map {$messages += $_} values %{$total_aref->[1]};
+
+      if ($messages > 0) {
+        @content = ($total_aref->[0], '', $messages, '');
+
+        #Count the number of distict IPs for the Hosts column.
+        push(@content,scalar(keys %{$total_aref->[1]})) if $do_sender{Host};
+
+        #These rows do not have entries for the following columns (if specified)
+        foreach ('Domain','Email','Edomain') {
+          push(@content,'') if $do_sender{$_};
+        }
+
+        printf $txt_fh ("$txt_format1\n", @content) if $txt_fh;
+        printf $htm_fh ("$htm_format1\n", @content) if $htm_fh;
+        $ws_global->write(++$row, 0, \@content) if $xls_fh;
+      }
+    }
+  }
+
+  printf $txt_fh "\n"         if $txt_fh;
+  printf $htm_fh "</table>\n" if $htm_fh;
+  ++$row;
+}
+
+
+#######################################################################
+# print_user_patterns()
+#
+#  print_user_patterns();
+#
+# Print the counts of user specified patterns.
+#######################################################################
+sub print_user_patterns {
+  my $txt_format1 = "  %-18s  %6d";
+  my $htm_format1 = "<tr><td>%s</td><td align=\"right\">%d</td>";
+
+  if ($txt_fh) {
+    print $txt_fh "User Specified Patterns\n";
+    print $txt_fh "-----------------------";
+    print $txt_fh "\n                       Total\n";
+  }
+  if ($htm_fh) {
+    print $htm_fh "<hr><a name=\"Patterns\"></a><h2>User Specified Patterns</h2>\n";
+    print $htm_fh "<table border=0 width=\"100%\">\n";
+    print $htm_fh "<tr><td>\n";
+    print $htm_fh "<table border=1>\n";
+    print $htm_fh "<tr><th>&nbsp;</th><th>Total</th>\n";
+  }
+  if ($xls_fh) {
+      $ws_global->write($row++, $col, "User Specified Patterns", $f_header2);
+      &set_worksheet_line($ws_global, $row++, 1, ["Total"], $f_headertab);
+  }
+
+
+  my($key);
+  if ($merge_reports) {
+    # We are getting our data from previous reports.
+    foreach $key (@user_descriptions) {
+      my $count = get_report_total($report_totals{patterns}{$key},'Total');
+      printf $txt_fh ("$txt_format1\n",$key,$count) if $txt_fh;
+      printf $htm_fh ("$htm_format1\n",$key,$count) if $htm_fh;
+      if ($xls_fh)
+      {
+        &set_worksheet_line($ws_global, $row++, 0, [$key,$count], $f_default);
+      }
+    }
+  }
+  else {
+    # We are getting our data from mainlog files.
+    my $user_pattern_index = 0;
+    foreach $key (@user_descriptions) {
+      printf $txt_fh ("$txt_format1\n",$key,$user_pattern_totals[$user_pattern_index]) if $txt_fh;
+      printf $htm_fh ("$htm_format1\n",$key,$user_pattern_totals[$user_pattern_index]) if $htm_fh;
+      $ws_global->write($row++, 0, [$key,$user_pattern_totals[$user_pattern_index]]) if $xls_fh;
+      $user_pattern_index++;
+    }
+  }
+  print $txt_fh "\n" if $txt_fh;
+  print $htm_fh "</table>\n\n" if $htm_fh;
+  if ($xls_fh)
+  {
+    ++$row;
+  }
+
+  if ($hist_opt > 0) {
+    my $user_pattern_index = 0;
+    foreach $key (@user_descriptions) {
+      print_histogram($key, 'occurence', @{$user_pattern_interval_count[$user_pattern_index]});
+      $user_pattern_index++;
+    }
+  }
+}
+
+#######################################################################
+# print_rejects()
+#
+#  print_rejects();
+#
+# Print statistics about rejected mail.
+#######################################################################
+sub print_rejects {
+  my($format1,$reason);
+
+  my $txt_format1 = "  %-40s  %6d";
+  my $htm_format1 = "<tr><td>%s</td><td align=\"right\">%d</td>";
+
+  if ($txt_fh) {
+    print $txt_fh "Rejected mail by reason\n";
+    print $txt_fh "-----------------------";
+    print $txt_fh "\n                                             Total\n";
+  }
+  if ($htm_fh) {
+    print $htm_fh "<hr><a name=\"patterns\"></a><h2>Rejected mail by reason</h2>\n";
+    print $htm_fh "<table border=0 width=\"100%\"><tr><td><table border=1>\n";
+    print $htm_fh "<tr><th>&nbsp;</th><th>Total</th>\n";
+  }
+  if ($xls_fh) {
+    $ws_global->write($row++, $col, "Rejected mail by reason", $f_header2);
+    &set_worksheet_line($ws_global, $row++, 1, ["Total"], $f_headertab);
+  }
+
+
+  my $href = ($merge_reports) ? $report_totals{rejected_mail_by_reason} : \%rejected_count_by_reason;
+  my(@chartdatanames, @chartdatavals_count);
+
+  foreach $reason (top_n_sort($topcount, $href, undef, undef)) {
+    printf $txt_fh ("$txt_format1\n",$reason,$href->{$reason}) if $txt_fh;
+    printf $htm_fh ("$htm_format1\n",$reason,$href->{$reason}) if $htm_fh;
+    set_worksheet_line($ws_global, $row++, 0, [$reason,$href->{$reason}], $f_default) if $xls_fh;
+    push(@chartdatanames, $reason);
+    push(@chartdatavals_count, $href->{$reason});
+  }
+
+  $row++ if $xls_fh;
+  print $txt_fh "\n" if $txt_fh;
+
+  if ($htm_fh) {
+    print $htm_fh "</tr></table></td><td>";
+    if ($HAVE_GD_Graph_pie && $charts && ($#chartdatavals_count > 0)) {
+      # calculate the graph
+      my @data = (
+         \@chartdatanames,
+         \@chartdatavals_count
+      );
+      my $graph = GD::Graph::pie->new(200, 200);
+      $graph->set(
+          x_label           => 'Rejection Reasons',
+          y_label           => 'Messages',
+          title             => 'By count',
+      );
+      my $gd = $graph->plot(\@data) or warn($graph->error);
+      if ($gd) {
+        open(IMG, ">$chartdir/rejections_count.png") or die "Could not write $chartdir/rejections_count.png: $!\n";
+        binmode IMG;
+        print IMG $gd->png;
+        close IMG;
+        print $htm_fh "<img src=\"$chartrel/rejections_count.png\">";
+      }
+    }
+    print $htm_fh "</td></tr></table>\n\n";
+  }
+}
+
+
+
+
+
+#######################################################################
+# print_transport();
+#
+#  print_transport();
+#
+# Print totals by transport.
+#######################################################################
+sub print_transport {
+  my(@chartdatanames);
+  my(@chartdatavals_count);
+  my(@chartdatavals_vol);
+  no integer;                 #Lose this for charting the data.
+
+  my $txt_format1 = "  %-18s  %6s      %6d";
+  my $htm_format1 = "<tr><td>%s</td><td align=\"right\">%s</td><td align=\"right\">%d</td>";
+
+  if ($txt_fh) {
+    print $txt_fh "Deliveries by transport\n";
+    print $txt_fh "-----------------------";
+    print $txt_fh "\n                      Volume    Messages\n";
+  }
+  if ($htm_fh) {
+    print $htm_fh "<hr><a name=\"Transport\"></a><h2>Deliveries by Transport</h2>\n";
+    print $htm_fh "<table border=0 width=\"100%\"><tr><td><table border=1>\n";
+    print $htm_fh "<tr><th>&nbsp;</th><th>Volume</th><th>Messages</th>\n";
+  }
+  if ($xls_fh) {
+    $ws_global->write(++$row, $col, "Deliveries by transport", $f_header2);
+    $ws_global->write(++$row, 1, ["Volume", "Messages"], $f_headertab);
+  }
+
+  my($key);
+  if ($merge_reports) {
+    # We are getting our data from previous reports.
+    foreach $key (sort keys %{$report_totals{transport}}) {
+      my $count = get_report_total($report_totals{transport}{$key},'Messages');
+      my @content=($key, volume_rounded($report_totals{transport}{$key}{Volume},
+        $report_totals{transport}{$key}{'Volume-gigs'}), $count);
+      push(@chartdatanames, $key);
+      push(@chartdatavals_count, $count);
+      push(@chartdatavals_vol, $report_totals{transport}{$key}{'Volume-gigs'}*$gig + $report_totals{transport}{$key}{Volume} );
+      printf $txt_fh ("$txt_format1\n", @content) if $txt_fh;
+      printf $htm_fh ("$htm_format1\n", @content) if $htm_fh;
+      $ws_global->write(++$row, 0, \@content) if $xls_fh;
+    }
+  }
+  else {
+    # We are getting our data from mainlog files.
+    foreach $key (sort keys %transported_data) {
+      my @content=($key, volume_rounded($transported_data{$key},$transported_data_gigs{$key}),
+        $transported_count{$key});
+      push(@chartdatanames, $key);
+      push(@chartdatavals_count, $transported_count{$key});
+      push(@chartdatavals_vol, $transported_data_gigs{$key}*$gig + $transported_data{$key});
+      printf $txt_fh ("$txt_format1\n", @content) if $txt_fh;
+      printf $htm_fh ("$htm_format1\n", @content) if $htm_fh;
+      $ws_global->write(++$row, 0, \@content) if $xls_fh;
+    }
+  }
+  print $txt_fh "\n" if $txt_fh;
+  if ($htm_fh) {
+    print $htm_fh "</tr></table></td><td>";
+
+    if ($HAVE_GD_Graph_pie && $charts && ($#chartdatavals_count > 0))
+      {
+      # calculate the graph
+      my @data = (
+         \@chartdatanames,
+         \@chartdatavals_count
+      );
+      my $graph = GD::Graph::pie->new(200, 200);
+      $graph->set(
+          x_label           => 'Transport',
+          y_label           => 'Messages',
+          title             => 'By count',
+      );
+      my $gd = $graph->plot(\@data) or warn($graph->error);
+      if ($gd) {
+        open(IMG, ">$chartdir/transports_count.png") or die "Could not write $chartdir/transports_count.png: $!\n";
+        binmode IMG;
+        print IMG $gd->png;
+        close IMG;
+        print $htm_fh "<img src=\"$chartrel/transports_count.png\">";
+      }
+    }
+    print $htm_fh "</td><td>";
+
+    if ($HAVE_GD_Graph_pie && $charts && ($#chartdatavals_vol > 0)) {
+      my @data = (
+         \@chartdatanames,
+         \@chartdatavals_vol
+      );
+      my $graph = GD::Graph::pie->new(200, 200);
+      $graph->set(
+          title             => 'By volume',
+      );
+      my $gd = $graph->plot(\@data) or warn($graph->error);
+      if ($gd) {
+        open(IMG, ">$chartdir/transports_vol.png") or die "Could not write $chartdir/transports_vol.png: $!\n";
+        binmode IMG;
+        print IMG $gd->png;
+        close IMG;
+        print $htm_fh "<img src=\"$chartrel/transports_vol.png\">";
+      }
+    }
+
+    print $htm_fh "</td></tr></table>\n\n";
+  }
+}
+
+
+
+#######################################################################
+# print_relay();
+#
+#  print_relay();
+#
+# Print our totals by relay.
+#######################################################################
+sub print_relay {
+  my $row_print_relay=1;
+  my $temp = "Relayed messages";
+  print $htm_fh "<hr><a name=\"$temp\"></a><h2>$temp</h2>\n" if $htm_fh;
+  if (scalar(keys %relayed) > 0 || $relayed_unshown > 0) {
+    my $shown = 0;
+    my $spacing = "";
+    my $txt_format = "%7d %s\n      => %s\n";
+    my $htm_format = "<tr><td align=\"right\">%d</td><td>%s</td><td>%s</td>\n";
+
+    printf $txt_fh ("%s\n%s\n\n", $temp, "-" x length($temp)) if $txt_fh;
+    if ($htm_fh) {
+      print $htm_fh "<table border=1>\n";
+      print $htm_fh "<tr><th>Count</th><th>From</th><th>To</th>\n";
+    }
+    if ($xls_fh) {
+      $ws_relayed->write($row_print_relay++, $col, $temp, $f_header2);
+      &set_worksheet_line($ws_relayed, $row_print_relay++, 0, ["Count", "From", "To"], $f_headertab);
+    }
+
+
+    my($key);
+    foreach $key (sort keys %relayed) {
+      my $count = $relayed{$key};
+      $shown += $count;
+      $key =~ s/[HA]=//g;
+      my($one,$two) = split(/=> /, $key);
+      my @content=($count, $one, $two);
+      printf $txt_fh ($txt_format, @content) if $txt_fh;
+      printf $htm_fh ($htm_format, @content) if $htm_fh;
+      if ($xls_fh)
+      {
+        &set_worksheet_line($ws_relayed, $row_print_relay++, 0, \@content);
+      }
+      $spacing = "\n";
+    }
+
+    print $htm_fh "</table>\n<p>\n" if $htm_fh;
+    print $txt_fh "${spacing}Total: $shown (plus $relayed_unshown unshown)\n\n" if $txt_fh;
+    print $htm_fh "${spacing}Total: $shown (plus $relayed_unshown unshown)\n\n" if $htm_fh;
+    if ($xls_fh)
+    {
+       &set_worksheet_line($ws_relayed, $row_print_relay++, 0, [$shown, "Sum of shown" ]);
+       &set_worksheet_line($ws_relayed, $row_print_relay++, 0, [$relayed_unshown, "unshown"]);
+       $row_print_relay++;
+    }
+  }
+  else {
+    print $txt_fh "No relayed messages\n-------------------\n\n" if $txt_fh;
+    print $htm_fh "No relayed messages\n\n" if $htm_fh;
+    if ($xls_fh)
+    {
+      $row_print_relay++;
+    }
+  }
+}
+
+
+
+#######################################################################
+# print_errors();
+#
+#  print_errors();
+#
+# Print our errors. In HTML, we display them as a list rather than a table -
+# Netscape doesn't like large tables!
+#######################################################################
+sub print_errors {
+  my $total_errors = 0;
+  $row=1;
+
+  if (scalar(keys %errors_count) != 0) {
+    my $temp = "List of errors";
+    my $htm_format = "<li>%d - %s\n";
+
+    printf $txt_fh ("%s\n%s\n\n", $temp, "-" x length($temp)) if $txt_fh;
+    if ($htm_fh) {
+      print $htm_fh "<hr><a name=\"errors\"></a><h2>$temp</h2>\n";
+      print $htm_fh "<ul><li><b>Count - Error</b>\n";
+    }
+    if ($xls_fh)
+    {
+      $ws_errors->write($row++, 0, $temp, $f_header2);
+      &set_worksheet_line($ws_errors, $row++, 0, ["Count", "Error"], $f_headertab);
+    }
+
+
+    my($key);
+    foreach $key (sort keys %errors_count) {
+      my $text = $key;
+      chomp($text);
+      $text =~ s/\s\s+/ /g;   #Convert multiple spaces to a single space.
+      $total_errors += $errors_count{$key};
+
+      if ($txt_fh) {
+        printf $txt_fh ("%5d ", $errors_count{$key});
+        my $text_remaining = $text;
+        while (length($text_remaining) > 65) {
+          my($first,$rest) = $text_remaining =~ /(.{50}\S*)\s+(.+)/;
+          last if !$first;
+          printf $txt_fh ("%s\n\t    ", $first);
+          $text_remaining = $rest;
+        }
+        printf $txt_fh ("%s\n\n", $text_remaining);
+      }
+
+      if ($htm_fh) {
+
+        #Translate HTML tag characters. Sergey Sholokh.
+        $text =~ s/\</\&lt\;/g;
+        $text =~ s/\>/\&gt\;/g;
+
+        printf $htm_fh ($htm_format,$errors_count{$key},$text);
+      }
+      if ($xls_fh)
+      {
+        &set_worksheet_line($ws_errors, $row++, 0, [$errors_count{$key},$text]);
+      }
+    }
+
+    $temp = "Errors encountered: $total_errors";
+
+    if ($txt_fh) {
+      print $txt_fh $temp, "\n";
+      print $txt_fh "-" x length($temp),"\n";
+    }
+    if ($htm_fh) {
+      print $htm_fh "</ul>\n<p>\n";
+      print $htm_fh $temp, "\n";
+    }
+    if ($xls_fh)
+    {
+        &set_worksheet_line($ws_errors, $row++, 0, [$total_errors, "Sum of Errors encountered"]);
+    }
+  }
+
+}
+
+
+#######################################################################
+# parse_old_eximstat_reports();
+#
+#  parse_old_eximstat_reports($fh);
+#
+# Parse old eximstat output so we can merge daily stats to weekly stats and weekly to monthly etc.
+#
+# To test that the merging still works after changes, do something like the following.
+# All the diffs should produce no output.
+#
+#  options='-bydomain -byemail -byhost -byedomain'
+#  options="$options -show_rt1,2,4 -show_dt 1,2,4"
+#  options="$options -pattern 'Completed Messages' /Completed/"
+#  options="$options -pattern 'Received Messages' /<=/"
+#
+#  ./eximstats $options mainlog > mainlog.txt
+#  ./eximstats $options -merge mainlog.txt > mainlog.2.txt
+#  diff mainlog.txt mainlog.2.txt
+#
+#  ./eximstats $options -html mainlog > mainlog.html
+#  ./eximstats $options -merge -html mainlog.txt  > mainlog.2.html
+#  diff mainlog.html mainlog.2.html
+#
+#  ./eximstats $options -merge mainlog.html > mainlog.3.txt
+#  diff mainlog.txt mainlog.3.txt
+#
+#  ./eximstats $options -merge -html mainlog.html > mainlog.3.html
+#  diff mainlog.html mainlog.3.html
+#
+#  ./eximstats $options -nvr   mainlog > mainlog.nvr.txt
+#  ./eximstats $options -merge mainlog.nvr.txt > mainlog.4.txt
+#  diff mainlog.txt mainlog.4.txt
+#
+#  # double_mainlog.txt should have twice the values that mainlog.txt has.
+#  ./eximstats $options mainlog mainlog > double_mainlog.txt
+#######################################################################
+sub parse_old_eximstat_reports {
+  my($fh) = @_;
+
+  my(%league_table_value_entered, %league_table_value_was_zero, %table_order);
+
+  my(%user_pattern_index);
+  my $user_pattern_index = 0;
+  map {$user_pattern_index{$_} = $user_pattern_index++} @user_descriptions;
+  my $user_pattern_keys = join('|', @user_descriptions);
+
+  while (<$fh>) {
+    PARSE_OLD_REPORT_LINE:
+    if (/Exim statistics from ([\d\-]+ [\d:]+(\s+[\+\-]\d+)?) to ([\d\-]+ [\d:]+(\s+[\+\-]\d+)?)/) {
+      $begin = $1 if ($1 lt $begin);
+      $end   = $3 if ($3 gt $end);
+    }
+    elsif (/Grand total summary/) {
+      # Fill in $report_totals{Received|Delivered}{Volume|Messages|Addresses|Hosts|Domains|...|Delayed|DelayedPercent|Failed|FailedPercent}
+      my(@fields, @delivered_fields);
+      my $doing_table = 0;
+      while (<$fh>) {
+        $_ = html2txt($_);       #Convert general HTML markup to text.
+        s/At least one addr//g;  #Another part of the HTML output we don't want.
+
+#  TOTAL               Volume    Messages Addresses   Hosts Domains      Delayed       Failed
+#  Received              26MB         237               177      23       8  3.4%     28 11.8%
+#  Delivered             13MB         233       250      99      88
+        if (/TOTAL\s+(.*?)\s*$/) {
+          $doing_table = 1;
+          @delivered_fields = split(/\s+/,$1);
+
+          #Delayed and Failed have two columns each, so add the extra field names in.
+          splice(@delivered_fields,-1,1,'DelayedPercent','Failed','FailedPercent');
+
+          # Addresses only figure in the Delivered row, so remove them from the
+          # normal fields.
+          @fields = grep !/Addresses/, @delivered_fields;
+        }
+        elsif (/(Received)\s+(.*?)\s*$/) {
+          print STDERR "Parsing $_" if $debug;
+          add_to_totals($report_totals{$1},\@fields,$2);
+        }
+        elsif (/(Delivered)\s+(.*?)\s*$/) {
+          print STDERR "Parsing $_" if $debug;
+          add_to_totals($report_totals{$1},\@delivered_fields,$2);
+          my $data = $2;
+          # If we're merging an old report which doesn't include addresses,
+          # then use the Messages field instead.
+          unless (grep(/Addresses/, @delivered_fields)) {
+            my %tmp;
+            line_to_hash(\%tmp,\@delivered_fields,$data);
+            add_to_totals($report_totals{Delivered},['Addresses'],$tmp{Messages});
+          }
+        }
+        elsif (/(Temp Rejects|Rejects|Ham|Spam)\s+(.*?)\s*$/) {
+          print STDERR "Parsing $_" if $debug;
+          add_to_totals($report_totals{$1},['Messages','Hosts'],$2);
+        }
+        else {
+          last if $doing_table;
+        }
+      }
+    }
+
+    elsif (/User Specified Patterns/i) {
+#User Specified Patterns
+#-----------------------
+#                       Total
+#  Description             85
+
+      while (<$fh>) { last if (/Total/); }  #Wait until we get the table headers.
+      while (<$fh>) {
+        print STDERR "Parsing $_" if $debug;
+        $_ = html2txt($_);              #Convert general HTML markup to text.
+        if (/^\s*(.*?)\s+(\d+)\s*$/) {
+          $report_totals{patterns}{$1} = {} unless (defined $report_totals{patterns}{$1});
+          add_to_totals($report_totals{patterns}{$1},['Total'],$2);
+        }
+        last if (/^\s*$/);              #Finished if we have a blank line.
+      }
+    }
+
+    elsif (/(^|<h2>)($user_pattern_keys) per /o) {
+      # Parse User defined pattern histograms if they exist.
+      parse_histogram($fh, $user_pattern_interval_count[$user_pattern_index{$2}] );
+    }
+
+
+    elsif (/Deliveries by transport/i) {
+#Deliveries by transport
+#-----------------------
+#                      Volume    Messages
+#  :blackhole:           70KB          51
+#  address_pipe         655KB           1
+#  smtp                  11MB         151
+
+      while (<$fh>) { last if (/Volume/); }  #Wait until we get the table headers.
+      while (<$fh>) {
+        print STDERR "Parsing $_" if $debug;
+        $_ = html2txt($_);              #Convert general HTML markup to text.
+        if (/(\S+)\s+(\d+\S*\s+\d+)/) {
+          $report_totals{transport}{$1} = {} unless (defined $report_totals{transport}{$1});
+          add_to_totals($report_totals{transport}{$1},['Volume','Messages'],$2);
+        }
+        last if (/^\s*$/);              #Finished if we have a blank line.
+      }
+    }
+    elsif (/Messages received per/) {
+      parse_histogram($fh, \@received_interval_count);
+    }
+    elsif (/Deliveries per/) {
+      parse_histogram($fh, \@delivered_interval_count);
+    }
+
+    #elsif (/Time spent on the queue: (all messages|messages with at least one remote delivery)/) {
+    elsif (/(Time spent on the queue|Delivery times|Receipt times): ((\S+) messages|messages with at least one remote delivery)((<[^>]*>)*\s*)$/) {
+#Time spent on the queue: all messages
+#-------------------------------------
+#
+#Under   1m      217  91.9%   91.9%
+#        5m        2   0.8%   92.8%
+#        3h        8   3.4%   96.2%
+#        6h        7   3.0%   99.2%
+#       12h        2   0.8%  100.0%
+
+      # Set a pointer to the queue bin so we can use the same code
+      # block for both all messages and remote deliveries.
+      #my $bin_aref = ($1 eq 'all messages') ? \@qt_all_bin : \@qt_remote_bin;
+      my($bin_aref, $times_aref, $overflow_sref);
+      if ($1 eq 'Time spent on the queue') {
+        $times_aref = \@queue_times;
+        if ($2 eq 'all messages') {
+          $bin_aref = \@qt_all_bin;
+          $overflow_sref = \$qt_all_overflow;
+        }
+        else {
+          $bin_aref = \@qt_remote_bin;
+          $overflow_sref = \$qt_remote_overflow;
+        }
+      }
+      elsif ($1 eq 'Delivery times') {
+        $times_aref = \@delivery_times;
+        if ($2 eq 'all messages') {
+          $bin_aref = \@dt_all_bin;
+          $overflow_sref = \$dt_all_overflow;
+        }
+        else {
+          $bin_aref = \@dt_remote_bin;
+          $overflow_sref = \$dt_remote_overflow;
+        }
+      }
+      else {
+        unless (exists $rcpt_times_bin{$3}) {
+          initialise_rcpt_times($3);
+        }
+        $bin_aref = $rcpt_times_bin{$3};
+        $times_aref = \@rcpt_times;
+        $overflow_sref = \$rcpt_times_overflow{$3};
+      }
+
+
+      my ($blank_lines, $reached_table) = (0,0);
+      while (<$fh>) {
+        $_ = html2txt($_);              #Convert general HTML markup to text.
+        # The table is preceded by one blank line, and has one blank line
+        # following it. As the table may be empty, the best way to determine
+        # that we've finished it is to look for the second blank line.
+        ++$blank_lines if /^\s*$/;
+        last if ($blank_lines >=2);     #Finished the table ?
+        $reached_table = 1 if (/\d/);
+        next unless $reached_table;
+        my $previous_seconds_on_queue = 0;
+        if (/^\s*(Under|Over|)\s+(\d+[smhdw])\s+(\d+)/) {
+          print STDERR "Parsing $_" if $debug;
+          my($modifier,$formated_time,$count) = ($1,$2,$3);
+          my $seconds = unformat_time($formated_time);
+          my $time_on_queue = ($seconds + $previous_seconds_on_queue) / 2;
+          $previous_seconds_on_queue = $seconds;
+          $time_on_queue = $seconds * 2 if ($modifier eq 'Over');
+          my($i);
+          for ($i = 0; $i <= $#$times_aref; $i++) {
+            if ($time_on_queue < $times_aref->[$i]) {
+              $$bin_aref[$i] += $count;
+              last;
+            }
+          }
+          $$overflow_sref += $count if ($i > $#$times_aref);
+
+        }
+      }
+    }
+
+    elsif (/Relayed messages/) {
+#Relayed messages
+#----------------
+#
+#      1 addr.domain.com [1.2.3.4] a.user@domain.com
+#      => addr2.domain2.com [5.6.7.8] a2.user2@domain2.com
+#
+#<tr><td align="right">1</td><td>addr.domain.com [1.2.3.4] a.user@domain.com </td><td>addr2.domain2.com [5.6.7.8] a2.user2@domain2.com</td>
+
+      my $reached_table = 0;
+      my($count,$sender);
+      while (<$fh>) {
+        unless ($reached_table) {
+          last if (/No relayed messages/);
+          $reached_table = 1 if (/^\s*\d/ || />\d+</);
+          next unless $reached_table;
+        }
+        if (/>(\d+)<.td><td>(.*?) ?<.td><td>(.*?)</) {
+          update_relayed($1,$2,$3);
+        }
+        elsif (/^\s*(\d+)\s+(.*?)\s*$/) {
+          ($count,$sender) = ($1,$2);
+        }
+        elsif (/=>\s+(.*?)\s*$/) {
+          update_relayed($count,$sender,$1);
+        }
+        else {
+          last;                           #Finished the table ?
+        }
+      }
+    }
+
+    elsif (/Top (.*?) by (message count|volume)/) {
+#Top 50 sending hosts by message count
+#-------------------------------------
+#
+#     48     1468KB   local
+# Could also have average values for HTML output.
+#     48     1468KB   30KB  local
+
+      my($category,$by_count_or_volume) = ($1,$2);
+
+      #As we show 2 views of each table (by count and by volume),
+      #most (but not all) entries will appear in both tables.
+      #Set up a hash to record which entries we have already seen
+      #and one to record which ones we are seeing for the first time.
+      if ($by_count_or_volume =~ /count/) {
+        undef %league_table_value_entered;
+        undef %league_table_value_was_zero;
+        undef %table_order;
+      }
+
+      #As this section processes multiple different table categories,
+      #set up pointers to the hashes to be updated.
+      my($messages_href,$addresses_href,$data_href,$data_gigs_href);
+      if ($category =~ /local sender/) {
+        $messages_href   = \%received_count_user;
+        $addresses_href  = undef;
+        $data_href       = \%received_data_user;
+        $data_gigs_href  = \%received_data_gigs_user;
+      }
+      elsif ($category =~ /sending (\S+?)s?\b/) {
+        #Top 50 sending (host|domain|email|edomain)s
+        #Top sending (host|domain|email|edomain)
+        $messages_href   = \%{$received_count{"\u$1"}};
+        $data_href       = \%{$received_data{"\u$1"}};
+        $data_gigs_href  = \%{$received_data_gigs{"\u$1"}};
+      }
+      elsif ($category =~ /local destination/) {
+        $messages_href   = \%delivered_messages_user;
+        $addresses_href  = \%delivered_addresses_user;
+        $data_href       = \%delivered_data_user;
+        $data_gigs_href  = \%delivered_data_gigs_user;
+      }
+      elsif ($category =~ /local domain destination/) {
+        $messages_href   = \%delivered_messages_local_domain;
+        $addresses_href  = \%delivered_addresses_local_domain;
+        $data_href       = \%delivered_data_local_domain;
+        $data_gigs_href  = \%delivered_data_gigs_local_domain;
+      }
+      elsif ($category =~ /(\S+) destination/) {
+        #Top 50 (host|domain|email|edomain) destinations
+        #Top (host|domain|email|edomain) destination
+        $messages_href   = \%{$delivered_messages{"\u$1"}};
+        $addresses_href  = \%{$delivered_addresses{"\u$1"}};
+        $data_href       = \%{$delivered_data{"\u$1"}};
+        $data_gigs_href  = \%{$delivered_data_gigs{"\u$1"}};
+      }
+      elsif ($category =~ /temporarily rejected ips/) {
+        $messages_href      = \%temporarily_rejected_count_by_ip;
+      }
+      elsif ($category =~ /rejected ips/) {
+        $messages_href      = \%rejected_count_by_ip;
+      }
+      elsif ($category =~ /non-rejected spamming ips/) {
+        $messages_href      = \%spam_count_by_ip;
+      }
+      elsif ($category =~ /mail temporary rejection reasons/) {
+        $messages_href      = \%temporarily_rejected_count_by_reason;
+      }
+      elsif ($category =~ /mail rejection reasons/) {
+        $messages_href      = \%rejected_count_by_reason;
+      }
+
+      my $reached_table = 0;
+      my $row_re;
+      while (<$fh>) {
+        # Watch out for empty tables.
+        goto PARSE_OLD_REPORT_LINE if (/<h2>/ or (/^\s*[a-zA-Z]/ && !/^\s*Messages/));
+
+        $_ = html2txt($_);              #Convert general HTML markup to text.
+
+        # Messages      Addresses  Bytes  Average
+        if (/^\s*Messages/) {
+          my $pattern = '^\s*(\d+)';
+          $pattern .= (/Addresses/) ? '\s+(\d+)' : '()';
+          $pattern .= (/Bytes/)     ? '\s+([\dKMGB]+)' : '()';
+          $pattern .= (/Average/)   ? '\s+[\dKMGB]+' : '';
+          $pattern .= '\s+(.*?)\s*$';
+          $row_re = qr/$pattern/;
+          $reached_table = 1;
+          next;
+        }
+        next unless $reached_table;
+
+        my($messages, $addresses, $rounded_volume, $entry);
+
+        if (/$row_re/) {
+          ($messages, $addresses, $rounded_volume, $entry) = ($1, $2, $3, $4);
+        }
+        else {
+          #Else we have finished the table and we may need to do some
+          #kludging to retain the order of the entries.
+
+          if ($by_count_or_volume =~ /volume/) {
+            #Add a few bytes to appropriate entries to preserve the order.
+            foreach $rounded_volume (keys %table_order) {
+              #For each rounded volume, we want to create a list which has things
+              #ordered from the volume table at the front, and additional things
+              #from the count table ordered at the back.
+              @{$table_order{$rounded_volume}{volume}} = () unless defined $table_order{$rounded_volume}{volume};
+              @{$table_order{$rounded_volume}{'message count'}} = () unless defined $table_order{$rounded_volume}{'message count'};
+              my(@order,%mark);
+              map {$mark{$_} = 1} @{$table_order{$rounded_volume}{volume}};
+              @order = @{$table_order{$rounded_volume}{volume}};
+              map {push(@order,$_)} grep(!$mark{$_},@{$table_order{$rounded_volume}{'message count'}});
+
+              my $bonus_bytes = $#order;
+              $bonus_bytes = 511 if ($bonus_bytes > 511);  #Don't go over the half-K boundary!
+              while (@order and ($bonus_bytes > 0)) {
+                my $entry = shift(@order);
+                if ($league_table_value_was_zero{$entry}) {
+                  $$data_href{$entry} += $bonus_bytes;
+                  print STDERR "$category by $by_count_or_volume: added $bonus_bytes bonus bytes to $entry\n" if $debug;
+                }
+                $bonus_bytes--;
+              }
+            }
+          }
+          last;
+        }
+
+        # Store a new table entry.
+
+        # Add the entry into the %table_order hash if it has a rounded
+        # volume (KB/MB/GB).
+        push(@{$table_order{$rounded_volume}{$by_count_or_volume}},$entry) if ($rounded_volume =~ /\D/);
+
+        unless ($league_table_value_entered{$entry}) {
+          $league_table_value_entered{$entry} = 1;
+          unless ($$messages_href{$entry}) {
+            $$messages_href{$entry}  = 0;
+            $$addresses_href{$entry} = 0;
+            $$data_href{$entry}      = 0;
+            $$data_gigs_href{$entry} = 0;
+            $league_table_value_was_zero{$entry} = 1;
+          }
+
+          $$messages_href{$entry} += $messages;
+
+          # When adding the addresses, be aware that we could be merging
+          # an old report which does not include addresses. In this case,
+          # we add the messages instead.
+          $$addresses_href{$entry} += ($addresses) ? $addresses : $messages;
+
+          #Add the rounded value to the data and data_gigs hashes.
+          un_round($rounded_volume,\$$data_href{$entry},\$$data_gigs_href{$entry}) if $rounded_volume;
+          print STDERR "$category by $by_count_or_volume: added $messages,$rounded_volume to $entry\n" if $debug;
+        }
+
+      }
+    }
+    elsif (/List of errors/) {
+#List of errors
+#--------------
+#
+#    1 07904931641@one2one.net R=external T=smtp: SMTP error
+#            from remote mailer after RCPT TO:<07904931641@one2one.net>:
+#            host mail.one2one.net [193.133.192.24]: 550 User unknown
+#
+#<li>1 - ally.dufc@dunbar.org.uk R=external T=smtp: SMTP error from remote mailer after RCPT TO:<ally.dufc@dunbar.org.uk>: host mail.dunbar.org.uk [216.167.89.88]: 550 Unknown local part ally.dufc in <ally.dufc@dunbar.org.uk>
+
+
+      my $reached_table = 0;
+      my($count,$error,$blanks);
+      while (<$fh>) {
+        $reached_table = 1 if (/^( *|<li>)(\d+)/);
+        next unless $reached_table;
+
+        s/^<li>(\d+) -/$1/;     #Convert an HTML line to a text line.
+        $_ = html2txt($_);      #Convert general HTML markup to text.
+
+        if (/\t\s*(.*)/) {
+          $error .= ' ' . $1;   #Join a multiline error.
+        }
+        elsif (/^\s*(\d+)\s+(.*)/) {
+          if ($error) {
+            #Finished with a previous multiline error so save it.
+            $errors_count{$error} = 0 unless $errors_count{$error};
+            $errors_count{$error} += $count;
+          }
+          ($count,$error) = ($1,$2);
+        }
+        elsif (/Errors encountered/) {
+          if ($error) {
+            #Finished the section, so save our stored last error.
+            $errors_count{$error} = 0 unless $errors_count{$error};
+            $errors_count{$error} += $count;
+          }
+          last;
+        }
+      }
+    }
+
+  }
+}
+
+#######################################################################
+# parse_histogram($fh, \@delivered_interval_count);
+# Parse a histogram into the provided array of counters.
+#######################################################################
+sub parse_histogram {
+  my($fh, $counters_aref) = @_;
+
+  #      Messages received per hour (each dot is 2 messages)
+  #---------------------------------------------------
+  #
+  #00-01    106 .....................................................
+  #01-02    103 ...................................................
+
+  my $reached_table = 0;
+  while (<$fh>) {
+    $reached_table = 1 if (/^00/);
+    next unless $reached_table;
+    print STDERR "Parsing $_" if $debug;
+    if (/^(\d+):(\d+)\s+(\d+)/) {           #hh:mm start time format ?
+      $$counters_aref[($1*60 + $2)/$hist_interval] += $3 if $hist_opt;
+    }
+    elsif (/^(\d+)-(\d+)\s+(\d+)/) {        #hh-hh start-end time format ?
+      $$counters_aref[($1*60)/$hist_interval] += $3 if $hist_opt;
+    }
+    else {                                  #Finished the table ?
+      last;
+    }
+  }
+}
+
+
+#######################################################################
+# update_relayed();
+#
+#  update_relayed($count,$sender,$recipient);
+#
+# Adds an entry into the %relayed hash. Currently only used when
+# merging reports.
+#######################################################################
+sub update_relayed {
+  my($count,$sender,$recipient) = @_;
+
+  #When generating the key, put in the 'H=' and 'A=' which can be used
+  #in searches.
+  my $key = "H=$sender => H=$recipient";
+  $key =~ s/ ([^=\s]+\@\S+|<>)/ A=$1/g;
+  if (!defined $relay_pattern || $key !~ /$relay_pattern/o) {
+    $relayed{$key} = 0 if !defined $relayed{$key};
+    $relayed{$key} += $count;
+  }
+  else {
+    $relayed_unshown += $count;
+  }
+}
+
+
+#######################################################################
+# add_to_totals();
+#
+#  add_to_totals(\%totals,\@keys,$values);
+#
+# Given a line of space seperated values, add them into the provided hash using @keys
+# as the hash keys.
+#
+# If the value contains a '%', then the value is set rather than added. Otherwise, we
+# convert the value to bytes and gigs. The gigs get added to I<Key>-gigs.
+#######################################################################
+sub add_to_totals {
+  my($totals_href,$keys_aref,$values) = @_;
+  my(@values) = split(/\s+/,$values);
+
+  for(my $i = 0; $i < @values && $i < @$keys_aref; ++$i) {
+    my $key = $keys_aref->[$i];
+    if ($values[$i] =~ /%/) {
+      $$totals_href{$key} = $values[$i];
+    }
+    else {
+      $$totals_href{$key} = 0 unless ($$totals_href{$key});
+      $$totals_href{"$key-gigs"} = 0 unless ($$totals_href{"$key-gigs"});
+      un_round($values[$i], \$$totals_href{$key}, \$$totals_href{"$key-gigs"});
+      print STDERR "Added $values[$i] to $key - $$totals_href{$key} , " . $$totals_href{"$key-gigs"} . "GB.\n" if $debug;
+    }
+  }
+}
+
+
+#######################################################################
+# line_to_hash();
+#
+#  line_to_hash(\%hash,\@keys,$line);
+#
+# Given a line of space seperated values, set them into the provided hash
+# using @keys as the hash keys.
+#######################################################################
+sub line_to_hash {
+  my($href,$keys_aref,$values) = @_;
+  my(@values) = split(/\s+/,$values);
+  for(my $i = 0; $i < @values && $i < @$keys_aref; ++$i) {
+    $$href{$keys_aref->[$i]} = $values[$i];
+  }
+}
+
+
+#######################################################################
+# get_report_total();
+#
+#  $total = get_report_total(\%hash,$key);
+#
+# If %hash contains values split into Units and Gigs, we calculate and return
+#
+#   $hash{$key} + 1024*1024*1024 * $hash{"${key}-gigs"}
+#######################################################################
+sub get_report_total {
+  no integer;
+  my($hash_ref,$key) = @_;
+  if ($$hash_ref{"${key}-gigs"}) {
+    return $$hash_ref{$key} + $gig * $$hash_ref{"${key}-gigs"};
+  }
+  return $$hash_ref{$key} || 0;
+}
+
+#######################################################################
+# html2txt();
+#
+#  $text_line = html2txt($html_line);
+#
+# Convert a line from html to text. Currently we just convert HTML tags to spaces
+# and convert &gt;, &lt;, and &nbsp; tags back.
+#######################################################################
+sub html2txt {
+  ($_) = @_;
+
+  # Convert HTML tags to spacing. Note that the reports may contain <Userid> and
+  # <Userid@Domain> words, so explicitly specify the HTML tags we will remove
+  # (the ones used by this program). If someone is careless enough to have their
+  # Userid the same as an HTML tag, there's not much we can do about it.
+  s/<\/?(html|head|title|body|h\d|ul|li|a\s+|table|tr|td|th|pre|hr|p|br)\b.*?>/ /g;
+
+  s/\&lt\;/\</og;             #Convert '&lt;' to '<'.
+  s/\&gt\;/\>/og;             #Convert '&gt;' to '>'.
+  s/\&nbsp\;/ /og;            #Convert '&nbsp;' to ' '.
+  return($_);
+}
+
+#######################################################################
+# get_next_arg();
+#
+#  $arg = get_next_arg();
+#
+# Because eximstats arguments are often passed as variables,
+# we can't rely on shell parsing to deal with quotes. This
+# subroutine returns $ARGV[1] and does a shift. If $ARGV[1]
+# starts with a quote (' or "), and doesn't end in one, then
+# we append the next argument to it and shift again. We repeat
+# until we've got all of the argument.
+#
+# This isn't perfect as all white space gets reduced to one space,
+# but it's as good as we can get! If it's esential that spacing
+# be preserved precisely, then you get that by not using shell
+# variables.
+#######################################################################
+sub get_next_arg {
+  my $arg = '';
+  my $matched_pattern = 0;
+  while ($ARGV[1]) {
+    $arg .= ' ' if $arg;
+    $arg .= $ARGV[1]; shift(@ARGV);
+    if ($arg !~ /^['"]/) {
+      $matched_pattern = 1;
+      last;
+    }
+    if ($arg =~ s/^(['"])(.*)\1$/$2/) {
+      $matched_pattern = 1;
+      last;
+    }
+  }
+  die "Mismatched argument quotes - <$arg>.\n" unless $matched_pattern;
+  return $arg;
+}
+
+#######################################################################
+# set_worksheet_line($ws_global, $startrow, $startcol, \@content, $format);
+#
+# set values to a sequence of cells in a row.
+#
+#######################################################################
+sub set_worksheet_line {
+  my ($worksheet, $row, $col, $content, $format) = @_;
+
+  foreach my $token (@$content)
+  {
+     $worksheet->write($row, $col++, $token, $format );
+  }
+
+}
+
+#######################################################################
+# @rcpt_times = parse_time_list($string);
+#
+# Parse a comma seperated list of time values in seconds given by
+# the user and fill an array.
+#
+# Return a default list if $string is undefined.
+# Return () if $string eq '0'.
+#######################################################################
+sub parse_time_list {
+  my($string) = @_;
+  if (! defined $string) {
+    return(60, 5*60, 15*60, 30*60, 60*60, 3*60*60, 6*60*60, 12*60*60, 24*60*60);
+  }
+  my(@times) = split(/,/, $string);
+  foreach my $q (@times) { $q = eval($q) + 0 }
+  @times = sort { $a <=> $b } @times;
+  @times = () if ($#times == 0 && $times[0] == 0);
+  return(@times);
+}
+
+
+#######################################################################
+# initialise_rcpt_times($protocol);
+# Initialise an array of rcpt_times to 0 for the specified protocol.
+#######################################################################
+sub initialise_rcpt_times {
+  my($protocol) = @_;
+  for (my $i = 0; $i <= $#rcpt_times; ++$i) {
+    $rcpt_times_bin{$protocol}[$i] = 0;
+  }
+  $rcpt_times_overflow{$protocol} = 0;
+}
+
+
+##################################################
+#                 Main Program                   #
+##################################################
+
+
+$last_timestamp = '';
+$last_date = '';
+$show_errors = 1;
+$show_relay = 1;
+$show_transport = 1;
+$topcount = 50;
+$local_league_table = 1;
+$include_remote_users = 0;
+$include_original_destination = 0;
+$hist_opt = 1;
+$volume_rounding = 1;
+$localtime_offset = calculate_localtime_offset();    # PH/FANF
+
+$charts = 0;
+$charts_option_specified = 0;
+$chartrel = ".";
+$chartdir = ".";
+
+@queue_times = parse_time_list();
+@rcpt_times = ();
+@delivery_times = ();
+
+$last_offset = '';
+$offset_seconds = 0;
+
+$row=1;
+$col=0;
+$col_hist=0;
+$run_hist=0;
+my(%output_files);     # What output files have been specified?
+
+# Decode options
+
+while (@ARGV > 0 && substr($ARGV[0], 0, 1) eq '-') {
+  if    ($ARGV[0] =~ /^\-h(\d+)$/) { $hist_opt = $1 }
+  elsif ($ARGV[0] =~ /^\-ne$/)     { $show_errors = 0 }
+  elsif ($ARGV[0] =~ /^\-nr(.?)(.*)\1$/) {
+    if ($1 eq "") { $show_relay = 0 } else { $relay_pattern = $2 }
+  }
+  elsif ($ARGV[0] =~ /^\-q([,\d\+\-\*\/]+)$/) { @queue_times = parse_time_list($1) }
+  elsif ($ARGV[0] =~ /^-nt$/)       { $show_transport = 0 }
+  elsif ($ARGV[0] =~ /^\-nt(.?)(.*)\1$/)
+    {
+    if ($1 eq "") { $show_transport = 0 } else { $transport_pattern = $2 }
+    }
+  elsif ($ARGV[0] =~ /^-t(\d+)$/)   { $topcount = $1 }
+  elsif ($ARGV[0] =~ /^-tnl$/)      { $local_league_table = 0 }
+  elsif ($ARGV[0] =~ /^-txt=?(\S*)$/)  { $txt_fh = get_filehandle($1,\%output_files) }
+  elsif ($ARGV[0] =~ /^-html=?(\S*)$/) { $htm_fh = get_filehandle($1,\%output_files) }
+  elsif ($ARGV[0] =~ /^-xls=?(\S*)$/) {
+    if ($HAVE_Spreadsheet_WriteExcel) {
+      $xls_fh = get_filehandle($1,\%output_files);
+    }
+    else {
+      warn "WARNING: CPAN Module Spreadsheet::WriteExcel not installed. Obtain from www.cpan.org\n";
+    }
+  }
+  elsif ($ARGV[0] =~ /^-merge$/)    { $merge_reports = 1 }
+  elsif ($ARGV[0] =~ /^-charts$/)   {
+    $charts = 1;
+    warn "WARNING: CPAN Module GD::Graph::pie not installed. Obtain from www.cpan.org\n" unless $HAVE_GD_Graph_pie;
+    warn "WARNING: CPAN Module GD::Graph::linespoints not installed. Obtain from www.cpan.org\n" unless $HAVE_GD_Graph_linespoints;
+  }
+  elsif ($ARGV[0] =~ /^-chartdir$/) { $chartdir = $ARGV[1]; shift; $charts_option_specified = 1; }
+  elsif ($ARGV[0] =~ /^-chartrel$/) { $chartrel = $ARGV[1]; shift; $charts_option_specified = 1; }
+  elsif ($ARGV[0] =~ /^-include_original_destination$/)    { $include_original_destination = 1 }
+  elsif ($ARGV[0] =~ /^-cache$/)    { } #Not currently used.
+  elsif ($ARGV[0] =~ /^-byhost$/)   { $do_sender{Host} = 1 }
+  elsif ($ARGV[0] =~ /^-bydomain$/) { $do_sender{Domain} = 1 }
+  elsif ($ARGV[0] =~ /^-byemail$/)  { $do_sender{Email} = 1 }
+  elsif ($ARGV[0] =~ /^-byemaildomain$/)  { $do_sender{Edomain} = 1 }
+  elsif ($ARGV[0] =~ /^-byedomain$/)  { $do_sender{Edomain} = 1 }
+  elsif ($ARGV[0] =~ /^-bylocaldomain$/)  { $do_local_domain = 1 }
+  elsif ($ARGV[0] =~ /^-emptyok$/)  { $emptyOK = 1 }
+  elsif ($ARGV[0] =~ /^-nvr$/)      { $volume_rounding = 0 }
+  elsif ($ARGV[0] =~ /^-show_rt([,\d\+\-\*\/]+)?$/) { @rcpt_times = parse_time_list($1) }
+  elsif ($ARGV[0] =~ /^-show_dt([,\d\+\-\*\/]+)?$/) { @delivery_times = parse_time_list($1) }
+  elsif ($ARGV[0] =~ /^-d$/)        { $debug = 1 }
+  elsif ($ARGV[0] =~ /^--?h(elp)?$/){ help() }
+  elsif ($ARGV[0] =~ /^-t_remote_users$/) { $include_remote_users = 1 }
+  elsif ($ARGV[0] =~ /^-pattern$/)
+    {
+    push(@user_descriptions,get_next_arg());
+    push(@user_patterns,get_next_arg());
+    }
+  elsif ($ARGV[0] =~ /^-utc$/)
+    {
+    # We don't need this value if the log is in UTC.
+    $localtime_offset = undef;
+    }
+  else
+    {
+    print STDERR "Eximstats: Unknown or malformed option $ARGV[0]\n";
+    help();
+    }
+  shift;
+  }
+
+  # keep old default behaviour
+  if (! ($xls_fh or $htm_fh or $txt_fh)) {
+    $txt_fh = \*STDOUT;
+  }
+
+  # Check that all the charts options are specified.
+  warn "-charts option not specified. Use -help for help.\n" if ($charts_option_specified && ! $charts);
+
+  # Default to display tables by sending Host.
+  $do_sender{Host} = 1 unless ($do_sender{Domain} || $do_sender{Email} || $do_sender{Edomain});
+
+  # prepare xls Excel Workbook
+  if (defined $xls_fh) {
+
+    # Create a new Excel workbook
+    $workbook  = Spreadsheet::WriteExcel->new($xls_fh);
+
+    # Add worksheets
+    $ws_global = $workbook->addworksheet('Exim Statistik');
+    # show $ws_global as initial sheet
+    $ws_global->set_first_sheet();
+    $ws_global->activate();
+
+    if ($show_relay) {
+      $ws_relayed = $workbook->addworksheet('Relayed Messages');
+      $ws_relayed->set_column(1, 2,  80);
+    }
+    if ($show_errors) {
+      $ws_errors = $workbook->addworksheet('Errors');
+    }
+
+
+    # set column widths
+    $ws_global->set_column(0, 2,  20); # Columns B-D width set to 30
+    $ws_global->set_column(3, 3,  15); # Columns B-D width set to 30
+    $ws_global->set_column(4, 4,  25); # Columns B-D width set to 30
+
+    # Define Formats
+    $f_default = $workbook->add_format();
+
+    $f_header1 = $workbook->add_format();
+    $f_header1->set_bold();
+    #$f_header1->set_color('red');
+    $f_header1->set_size('15');
+    $f_header1->set_valign();
+    # $f_header1->set_align('center');
+    # $ws_global->write($row++, 2, "Testing Headers 1", $f_header1);
+
+    $f_header2 = $workbook->add_format();
+    $f_header2->set_bold();
+    $f_header2->set_size('12');
+    $f_header2->set_valign();
+    # $ws_global->write($row++, 2, "Testing Headers 2", $f_header2);
+
+    # Create another header2 for use in merged cells.
+    $f_header2_m = $workbook->add_format();
+    $f_header2_m->set_bold();
+    $f_header2_m->set_size('8');
+    $f_header2_m->set_valign();
+    $f_header2_m->set_align('center');
+
+    $f_percent = $workbook->add_format();
+    $f_percent->set_num_format('0.0%');
+
+    $f_headertab = $workbook->add_format();
+    $f_headertab->set_bold();
+    $f_headertab->set_valign();
+    # $ws_global->write($row++, 2, "Testing Headers tab", $f_headertab);
+
+  }
+
+
+# Initialise the queue/delivery/rcpt time counters.
+for (my $i = 0; $i <= $#queue_times; $i++) {
+  $qt_all_bin[$i] = 0;
+  $qt_remote_bin[$i] = 0;
+}
+for (my $i = 0; $i <= $#delivery_times; $i++) {
+  $dt_all_bin[$i] = 0;
+  $dt_remote_bin[$i] = 0;
+}
+initialise_rcpt_times('all');
+
+
+# Compute the number of slots for the histogram
+if ($hist_opt > 0)
+  {
+  if ($hist_opt > 60 || 60 % $hist_opt != 0)
+    {
+    print STDERR "Eximstats: -h must specify a factor of 60\n";
+    exit 1;
+    }
+  $hist_interval = 60/$hist_opt;                #Interval in minutes.
+  $hist_number = (24*60)/$hist_interval;        #Number of intervals per day.
+  @received_interval_count = (0) x $hist_number;
+  @delivered_interval_count = (0) x $hist_number;
+  my $user_pattern_index = 0;
+  for (my $user_pattern_index = 0; $user_pattern_index <= $#user_patterns; ++$user_pattern_index) {
+    @{$user_pattern_interval_count[$user_pattern_index]} = (0) x $hist_number;
+  }
+  @dt_all_bin = (0) x $hist_number;
+  @dt_remote_bin = (0) x $hist_number;
+}
+
+#$queue_unknown = 0;
+
+$total_received_data = 0;
+$total_received_data_gigs = 0;
+$total_received_count = 0;
+
+$total_delivered_data = 0;
+$total_delivered_data_gigs = 0;
+$total_delivered_messages = 0;
+$total_delivered_addresses = 0;
+
+$qt_all_overflow = 0;
+$qt_remote_overflow = 0;
+$dt_all_overflow = 0;
+$dt_remote_overflow = 0;
+$delayed_count = 0;
+$relayed_unshown = 0;
+$message_errors = 0;
+$begin = "9999-99-99 99:99:99";
+$end = "0000-00-00 00:00:00";
+my($section,$type);
+foreach $section ('Received','Delivered','Temp Rejects', 'Rejects','Ham','Spam') {
+  foreach $type ('Volume','Messages','Delayed','Failed','Hosts','Domains','Emails','Edomains') {
+    $report_totals{$section}{$type} = 0;
+  }
+}
+
+# Generate our parser.
+my $parser = generate_parser();
+
+
+
+if (@ARGV) {
+  # Scan the input files and collect the data
+  foreach my $file (@ARGV) {
+    if ($file =~ /\.gz/) {
+      unless (open(FILE,"gunzip -c $file |")) {
+        print STDERR "Failed to gunzip -c $file: $!";
+        next;
+      }
+    }
+    elsif ($file =~ /\.Z/) {
+      unless (open(FILE,"uncompress -c $file |")) {
+        print STDERR "Failed to uncompress -c $file: $!";
+        next;
+      }
+    }
+    else {
+      unless (open(FILE,$file)) {
+        print STDERR "Failed to read $file: $!";
+        next;
+      }
+    }
+    #Now parse the filehandle, updating the global variables.
+    parse($parser,\*FILE);
+    close FILE;
+  }
+}
+else {
+  #No files provided. Parse STDIN, updating the global variables.
+  parse($parser,\*STDIN);
+}
+
+
+if ($begin eq "9999-99-99 99:99:99" && ! $emptyOK) {
+  print STDERR "**** No valid log lines read\n";
+  exit 1;
+}
+
+# Output our results.
+print_header();
+print_grandtotals();
+
+# Print counts of user specified patterns if required.
+print_user_patterns() if @user_patterns;
+
+# Print rejection reasons.
+# print_rejects();
+
+# Print totals by transport if required.
+print_transport() if $show_transport;
+
+# Print the deliveries per interval as a histogram, unless configured not to.
+# First find the maximum in one interval and scale accordingly.
+if ($hist_opt > 0) {
+  print_histogram("Messages received", 'message', @received_interval_count);
+  print_histogram("Deliveries", 'delivery', @delivered_interval_count);
+}
+
+# Print times on queue if required.
+if ($#queue_times >= 0) {
+  print_duration_table("Time spent on the queue", "all messages", \@queue_times, \@qt_all_bin,$qt_all_overflow);
+  print_duration_table("Time spent on the queue", "messages with at least one remote delivery", \@queue_times, \@qt_remote_bin,$qt_remote_overflow);
+}
+
+# Print delivery times if required.
+if ($#delivery_times >= 0) {
+  print_duration_table("Delivery times", "all messages", \@delivery_times, \@dt_all_bin,$dt_all_overflow);
+  print_duration_table("Delivery times", "messages with at least one remote delivery", \@delivery_times, \@dt_remote_bin,$dt_remote_overflow);
+}
+
+# Print rcpt times if required.
+if ($#rcpt_times >= 0) {
+  foreach my $protocol ('all', grep(!/^all$/, sort keys %rcpt_times_bin)) {
+    print_duration_table("Receipt times", "$protocol messages", \@rcpt_times, $rcpt_times_bin{$protocol}, $rcpt_times_overflow{$protocol});
+  }
+}
+
+# Print relay information if required.
+print_relay() if $show_relay;
+
+# Print the league tables, if topcount isn't zero.
+if ($topcount > 0) {
+  my($ws_rej, $ws_top50, $ws_rej_row, $ws_top50_row, $ws_temp_rej, $ws_temp_rej_row);
+  $ws_rej_row = $ws_temp_rej_row = $ws_top50_row = 0;
+  if ($xls_fh) {
+    $ws_top50 = $workbook->addworksheet('Deliveries');
+    $ws_rej = $workbook->addworksheet('Rejections') if (%rejected_count_by_reason || %rejected_count_by_ip || %spam_count_by_ip);
+    $ws_temp_rej = $workbook->addworksheet('Temporary Rejections') if (%temporarily_rejected_count_by_reason || %temporarily_rejected_count_by_ip);
+  }
+
+  print_league_table("mail rejection reason", \%rejected_count_by_reason, undef, undef, undef, $ws_rej, \$ws_rej_row) if %rejected_count_by_reason;
+  print_league_table("mail temporary rejection reason", \%temporarily_rejected_count_by_reason, undef, undef, undef, $ws_temp_rej, \$ws_temp_rej_row) if %temporarily_rejected_count_by_reason;
+
+  foreach ('Host','Domain','Email','Edomain') {
+    next unless $do_sender{$_};
+    print_league_table("sending \l$_", $received_count{$_}, undef, $received_data{$_},$received_data_gigs{$_}, $ws_top50, \$ws_top50_row);
+  }
+
+  print_league_table("local sender", \%received_count_user, undef,
+    \%received_data_user,\%received_data_gigs_user, $ws_top50, \$ws_top50_row) if (($local_league_table || $include_remote_users) && %received_count_user);
+  foreach ('Host','Domain','Email','Edomain') {
+    next unless $do_sender{$_};
+    print_league_table("\l$_ destination", $delivered_messages{$_}, $delivered_addresses{$_}, $delivered_data{$_},$delivered_data_gigs{$_}, $ws_top50, \$ws_top50_row);
+  }
+  print_league_table("local destination", \%delivered_messages_user, \%delivered_addresses_user, \%delivered_data_user,\%delivered_data_gigs_user, $ws_top50, \$ws_top50_row) if (($local_league_table || $include_remote_users) && %delivered_messages_user);
+  print_league_table("local domain destination", \%delivered_messages_local_domain, \%delivered_addresses_local_domain, \%delivered_data_local_domain,\%delivered_data_gigs_local_domain, $ws_top50, \$ws_top50_row) if (($local_league_table || $include_remote_users) && %delivered_messages_local_domain);
+
+  print_league_table("rejected ip", \%rejected_count_by_ip, undef, undef, undef, $ws_rej, \$ws_rej_row) if %rejected_count_by_ip;
+  print_league_table("temporarily rejected ip", \%temporarily_rejected_count_by_ip, undef, undef, undef, $ws_rej, \$ws_rej_row) if %temporarily_rejected_count_by_ip;
+  print_league_table("non-rejected spamming ip", \%spam_count_by_ip, undef, undef, undef, $ws_rej, \$ws_rej_row) if %spam_count_by_ip;
+
+}
+
+# Print the error statistics if required.
+print_errors() if $show_errors;
+
+print $htm_fh "</body>\n</html>\n" if $htm_fh;
+
+
+$txt_fh->close if $txt_fh && ref $txt_fh;
+$htm_fh->close if $htm_fh;
+
+if ($xls_fh) {
+  # close Excel Workbook
+  $ws_global->set_first_sheet();
+  # FIXME: whyever - activate does not work :-/
+  $ws_global->activate();
+  $workbook->close();
+}
+
+
+# End of eximstats
diff --git a/src/exinext.src b/src/exinext.src
new file mode 100644 (file)
index 0000000..182e395
--- /dev/null
@@ -0,0 +1,252 @@
+#! /bin/sh
+
+# Copyright (c) University of Cambridge, 1995 - 2007
+# See the file NOTICE for conditions of use and distribution.
+
+# Except when they appear in comments, the following placeholders in this
+# source are replaced when it is turned into a runnable script:
+#
+# CONFIGURE_FILE_USE_NODE
+# CONFIGURE_FILE
+# BIN_DIRECTORY
+
+# PROCESSED_FLAG
+
+# A shell+perl script to fish out the next retry time for a given domain;
+# it first calls exim to find out which hosts are set up for that domain and
+# then fishes out the retry data for each one.
+
+# For testing the selection and formatting logic, and perhaps for use in
+# special cases, the script can have an argument -C <filename> to specify
+# the use of an alternate Exim configuration file. It may also have any number
+# of -D options to set macros that are passed to exim.
+
+config=
+eximmacdef=
+exim_path=
+
+if expr -- $1 : '\-' >/dev/null ; then
+  while expr -- $1 : '\-' >/dev/null ; do
+    if [ "$1" = "-C" ]; then
+      config=$2
+      shift
+      shift
+    elif expr -- $1 : '\-D' >/dev/null ; then
+      eximmacdef="$eximmacdef $1"
+      if expr -- $1 : '\-DEXIM_PATH=' >/dev/null ; then
+        exim_path=`expr -- $1 : '\-DEXIM_PATH=\(.*\)'`
+      fi
+      shift
+    else
+      break
+    fi
+  done
+fi
+
+# We need to save the script's argument because in the absence of -C we need to
+# use shell arguments for sorting out the configuration file name.
+
+argone=$1
+
+# This is the normal case when no config file or macros are specified
+
+if [ "$config" = "" ]; then
+  # See if this installation is using the esoteric "USE_NODE" feature of Exim,
+  # in which it uses the host's name as a suffix for the configuration file name.
+
+  if [ "CONFIGURE_FILE_USE_NODE" = "yes" ]; then
+    hostsuffix=.`uname -n`
+  fi
+
+  # Now find the configuration file name. This has got complicated because
+  # CONFIGURE_FILE may now be a list of files. The one that is used is the first
+  # one that exists. Mimic the code in readconf.c by testing first for the
+  # suffixed file in each case.
+
+  set `awk -F: '{ for (i = 1; i <= NF; i++) print $i }' <<End
+CONFIGURE_FILE
+End
+`
+  while [ "$config" = "" -a $# -gt 0 ] ; do
+    if [ -f "$1$hostsuffix" ] ; then
+      config="$1$hostsuffix"
+    elif [ -f "$1" ] ; then
+      config="$1"
+    fi
+    shift
+  done
+fi
+
+# Determine where the spool directory is. Search for an exim_path setting
+# in the configure file; otherwise use the bin directory. Call that version of
+# Exim to find the spool directory and the qualify domain. BEWARE: a tab
+# character is needed in the command below. It has had a nasty tendency to get
+# lost in the past. Use a variable to hold a space and a tab to keep the tab in
+# one place.
+
+st='     '
+
+if [ "$exim_path" = "" ]; then
+  exim_path=`grep "^[$st]*exim_path" $config | sed "s/.*=[$st]*//"`
+fi
+
+if test "$exim_path" = ""; then exim_path=BIN_DIRECTORY/exim; fi
+spool_directory=`$exim_path $eximmacdef -C $config -bP spool_directory | sed 's/.*=[  ]*//'`
+qualify_domain=`$exim_path $eximmacdef -C $config -bP qualify_domain | sed 's/.*=[  ]*//'`
+
+# Now do the job. Perl uses $ so frequently that we don't want to have to
+# escape them all from the shell, so pass in shell variable values as
+# arguments.
+
+# 16-May-1996  Fixed it to do better if routing fails to complete.
+#              Improved the format of the output.
+# 10-Jun-1996  Complain if no argument given.
+# 02-Aug-1996  Lower case the domain.
+# 14-Jan-1999  Add subject to want list even if remote host found, so as to
+#              pick up routing delays after temporary recipient errors.
+#              Also add unqualified subject if it looks like a message id.
+# 01-Apr-2004  Add the -C feature for testing
+# 22-Dec-2005  Complete the -C feature (!)
+
+if [ "$argone" = "" ]; then
+  echo "Usage: exinext <address>|<domain>|<local-part>"
+  exit 1
+fi
+
+perl - $exim_path "$eximmacdef" $argone $spool_directory $qualify_domain $config <<'End'
+
+  # Name the arguments
+
+  $exim = $ARGV[0];
+  $eximmacdef = $ARGV[1];
+  $subject = $ARGV[2];
+  $spool = $ARGV[3];
+  $qualify = $ARGV[4];
+  $config = $ARGV[5];
+
+  # If the subject doesn't contain an @ then construct an address
+  # for the domain, and ensure that in both cases the domain is
+  # lower cased.
+
+  $address = ($subject =~ /^([^\@]*)\@([^\@]*)$/)?
+    "$1\@\L$2\E" : "User\@\L$subject\E";
+
+  # Run Exim to get a list of hosts for the given domain; for
+  # each one construct the appropriate retry key.
+
+  open(LIST, "$exim -C $config -v -bt $address |") ||
+    die "can't run exim to route $address";
+
+  while (<LIST>)
+    {
+    chop;
+    push(@list, $_) if s/\s*host (\S+)\s+\[(.+)\].*/$1:$2/;
+    print "$_\n" if /cannot be resolved/;
+    }
+  close(LIST);
+
+  # If there were no hosts, assume that what was given was a local
+  # username, unless it contains an @, and construct a suitable retry
+  # key for that. Also, if it looks like a message id, search for that
+  # as well, so as to pick up message-specific retry data.
+
+  if (scalar(@list) == 0)
+    {
+    push(@list, $subject) if $subject =~ /^\w{6}-\w{6}-\w{2}$/;
+
+    if ($subject !~ /\@/ && $subject !~ /\./)
+      {
+      push(@list, "$subject\@$qualify");
+      }
+    else
+      {
+      print "No remote hosts found for $subject\n";
+      }
+    }
+
+  # Always search for the full address, even if hosts are found, in case
+  # there is a routing delay caused by a temporary recipient error.
+
+  push(@list, $subject);
+
+  # Run exim_dumpdb to get out the retry data and pick off what we want
+
+  open(DATA, "${exim}_dumpdb $spool retry |") ||
+    die "can't run exim_dumpdb";
+
+  while (<DATA>)
+    {
+    for ($i = 0; $i <= $#list; $i++)
+      {
+      if (/$list[$i]/)
+        {
+        $printed = 1;
+        if (/^\s*T:[^:\s]*:/)
+          {
+          ($key,$error,$error2,$text) = /^\s*T:(\S+)\s+(\S+)\s+(\S+)\s*(.*)$/;
+
+          # Parsing the keys is a nightmare because of IPv6. The design of the
+          # format for the keys is a complete shambles. All my fault (PH). But
+          # I don't want to change it just for this purpose. If they key
+          # contains more than 3 colons, we have an IPv6 address, because
+          # an IPv6 address must contain at least two colons.
+
+          # Deal with IPv4 addresses (3 colons or fewer)
+
+          if ($key !~ /:([^:]*?:){3}/)
+            {
+            ($host,$ip,$port,$msgid) = $key =~
+              /^([^:]*):([^:]*)(?::([^:]*)(?::(\S*)|)|)/;
+            }
+
+          # Deal with IPv6 addresses; sorting out the colons is a complete
+          # mess. We should be able to find the host name and IP address from
+          # further in the message. That seems the easiest escape plan here. We
+          # can use those to match the rest of the key.
+
+          else
+            {
+            ($host,$ip) = $text =~ /host\s(\S+)\s\[([^]]+)\]/;
+            if (defined $host)
+              {
+              ($port,$msgid) = $key =~
+                /^$host:$ip(?::([^:]*)(?::(\S*)|)|)/;
+              }
+
+           # This will probably be wrong...
+
+           else
+             {
+             ($host,$ip) = $key =~ /([^:]*):(.*)/;
+             }
+            }
+
+          printf("Transport: %s [%s]", $host, $ip);
+          print ":$port" if defined $port;
+          print " $msgid" if defined $msgid;
+          print " error $error: $text\n";
+          }
+
+        else
+          {
+          ($type,$domain,$error,$error2,$text) =
+            /^\s*(\S):(\S+)\s+(\S+)\s+(\S+)\s*(.*)$/;
+          $type = ($type eq 'R')? "Route: " :
+                  ($type eq 'T')? "Transport: " : "";
+          print "$type$domain error $error: $text\n";
+          }
+        $_ = <DATA>;
+        ($first,$last,$next,$expired) =
+          /^(\S+\s+\S+)\s+(\S+\s+\S+)\s+(\S+\s+\S+)\s*(\*?)/;
+        print "  first failed: $first\n";
+        print "  last tried:   $last\n";
+        print "  next try at:  $next\n";
+        print "  past final cutoff time\n" if $expired eq "*";
+        }
+      }
+    }
+
+  close(DATA);
+  print "No retry data found for $subject\n" if !$printed;
+End
+
diff --git a/src/exipick.src b/src/exipick.src
new file mode 100644 (file)
index 0000000..4708ebb
--- /dev/null
@@ -0,0 +1,1824 @@
+#!PERL_COMMAND
+
+# This variable should be set by the building process to Exim's spool directory.
+my $spool = 'SPOOL_DIRECTORY';
+# Need to set this dynamically during build, but it's not used right now anyway.
+my $charset = 'ISO-8859-1';
+
+# use 'exipick --help' to view documentation for this program.
+# Documentation also viewable online at
+#       http://www.exim.org/eximwiki/ToolExipickManPage
+
+use strict;
+use Getopt::Long;
+
+my($p_name)   = $0 =~ m|/?([^/]+)$|;
+my $p_version = "20100323.0";
+my $p_usage   = "Usage: $p_name [--help|--version] (see --help for details)";
+my $p_cp      = <<EOM;
+        Copyright (c) 2003-2010 John Jetmore <jj33\@pobox.com>
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program; if not, write to the Free Software
+    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
+EOM
+ext_usage(); # before we do anything else, check for --help
+
+$| = 1; # unbuffer STDOUT
+
+Getopt::Long::Configure("bundling_override");
+GetOptions(
+  'spool=s'     => \$G::spool,      # exim spool dir
+  'input-dir=s' => \$G::input_dir,  # name of the "input" dir
+  'finput'      => \$G::finput,     # same as "--input-dir Finput"
+  'bp'          => \$G::mailq_bp,   # List the queue (noop - default)
+  'bpa'         => \$G::mailq_bpa,  # ... with generated address as well
+  'bpc'         => \$G::mailq_bpc,  # ... but just show a count of messages
+  'bpr'         => \$G::mailq_bpr,  # ... do not sort
+  'bpra'        => \$G::mailq_bpra, # ... with generated addresses, unsorted
+  'bpru'        => \$G::mailq_bpru, # ... only undelivered addresses, unsorted
+  'bpu'         => \$G::mailq_bpu,  # ... only undelivered addresses
+  'and'         => \$G::and,        # 'and' the criteria (default)
+  'or'          => \$G::or,         # 'or' the criteria
+  'f=s'         => \$G::qgrep_f,    # from regexp
+  'r=s'         => \$G::qgrep_r,    # recipient regexp
+  's=s'         => \$G::qgrep_s,    # match against size field
+  'y=s'         => \$G::qgrep_y,    # message younger than (secs)
+  'o=s'         => \$G::qgrep_o,    # message older than (secs)
+  'z'           => \$G::qgrep_z,    # frozen only
+  'x'           => \$G::qgrep_x,    # non-frozen only
+  'c'           => \$G::qgrep_c,    # display match count
+  'l'           => \$G::qgrep_l,    # long format (default)
+  'i'           => \$G::qgrep_i,    # message ids only
+  'b'           => \$G::qgrep_b,    # brief format
+  'size'        => \$G::size_only,  # sum the size of the matching msgs
+  'not'         => \$G::negate,     # flip every test
+  'R|reverse'   => \$G::reverse,    # reverse output (-R is qgrep option)
+  'sort=s'      => \@G::sort,       # allow you to choose variables to sort by
+  'freeze=s'    => \$G::freeze,     # freeze data in this file
+  'thaw=s'      => \$G::thaw,       # thaw data from this file
+  'unsorted'    => \$G::unsorted,   # unsorted, regardless of output format
+  'random'      => \$G::random,     # (poorly) randomize evaluation order
+  'flatq'       => \$G::flatq,      # brief format
+  'caseful'     => \$G::caseful,    # in '=' criteria, respect case
+  'caseless'    => \$G::caseless,   #   ...ignore case (default)
+  'charset=s'   => \$charset,       # charset for $bh and $h variables
+  'show-vars=s' => \$G::show_vars,  # display the contents of these vars
+  'just-vars'   => \$G::just_vars,  # only display vars, no other info
+  'show-rules'  => \$G::show_rules, # display compiled match rules
+  'show-tests'  => \$G::show_tests  # display tests as applied to each message
+) || exit(1);
+
+# if both freeze and thaw specified, only thaw as it is less desctructive
+$G::freeze = undef               if ($G::freeze && $G::thaw);
+freeze_start()                   if ($G::freeze);
+thaw_start()                     if ($G::thaw);
+
+# massage sort options (make '$var,Var:' be 'var','var')
+for (my $i = scalar(@G::sort)-1; $i >= 0; $i--) {
+  $G::sort[$i] = lc($G::sort[$i]);
+  $G::sort[$i] =~ s/[\$:\s]//g;
+  if ((my @vars = split(/,/, $G::sort[$i])) > 1) {
+    $G::sort[$i] = $vars[0]; shift(@vars); # replace current slot w/ first var
+    splice(@G::sort, $i+1, 0, @vars);      # add other vars after current pos
+  }
+}
+push(@G::sort, "message_exim_id") if (@G::sort);
+die "empty value provided to --sort not allowed, exiting\n"
+    if (grep /^\s*$/, @G::sort);
+
+# massage the qgrep options into standard criteria
+push(@ARGV, "\$sender_address     =~ /$G::qgrep_f/") if ($G::qgrep_f);
+push(@ARGV, "\$recipients         =~ /$G::qgrep_r/") if ($G::qgrep_r);
+push(@ARGV, "\$shown_message_size eq $G::qgrep_s")   if ($G::qgrep_s);
+push(@ARGV, "\$message_age        <  $G::qgrep_y")   if ($G::qgrep_y);
+push(@ARGV, "\$message_age        >  $G::qgrep_o")   if ($G::qgrep_o);
+push(@ARGV, "\$deliver_freeze")                      if ($G::qgrep_z);
+push(@ARGV, "!\$deliver_freeze")                     if ($G::qgrep_x);
+
+$G::mailq_bp        = $G::mailq_bp;        # shut up -w
+$G::and             = $G::and;             # shut up -w
+$G::msg_ids         = {};                  # short circuit when crit is only MID
+$G::caseless        = $G::caseful ? 0 : 1; # nocase by default, case if both
+@G::recipients_crit = ();                  # holds per-recip criteria
+$spool              = $G::spool if ($G::spool);
+my $input_dir       = $G::input_dir || ($G::finput ? "Finput" : "input");
+my $count_only      = 1 if ($G::mailq_bpc  || $G::qgrep_c);
+my $unsorted        = 1 if ($G::mailq_bpr  || $G::mailq_bpra ||
+                            $G::mailq_bpru || $G::unsorted);
+my $msg             = $G::thaw ? thaw_message_list()
+                               : get_all_msgs($spool, $input_dir, $unsorted,
+                                              $G::reverse, $G::random);
+die "Problem accessing thaw file\n" if ($G::thaw && !$msg);
+my $crit            = process_criteria(\@ARGV);
+my $e               = Exim::SpoolFile->new();
+my $tcount          = 0 if ($count_only);  # holds count of all messages
+my $mcount          = 0 if ($count_only);  # holds count of matching messages
+my $total_size      = 0 if ($G::size_only);
+$e->set_undelivered_only(1)      if ($G::mailq_bpru || $G::mailq_bpu);
+$e->set_show_generated(1)        if ($G::mailq_bpra || $G::mailq_bpa);
+$e->output_long()                if ($G::qgrep_l);
+$e->output_idonly()              if ($G::qgrep_i);
+$e->output_brief()               if ($G::qgrep_b);
+$e->output_flatq()               if ($G::flatq);
+$e->output_vars_only()           if ($G::just_vars && $G::show_vars);
+$e->set_show_vars($G::show_vars) if ($G::show_vars);
+$e->set_spool($spool, $input_dir);
+
+MSG:
+foreach my $m (@$msg) {
+  next if (scalar(keys(%$G::msg_ids)) && !$G::or
+                                      && !$G::msg_ids->{$m->{message}});
+  if ($G::thaw) {
+    my $data = thaw_data();
+    if (!$e->restore_state($data)) {
+      warn "Couldn't thaw $data->{_message}: ".$e->error()."\n";
+      next MSG;
+    }
+  } else {
+    if (!$e->parse_message($m->{message}, $m->{path})) {
+      warn "Couldn't parse $m->{message}: ".$e->error()."\n";
+      next MSG;
+    }
+  }
+  $tcount++;
+  my $match = 0;
+  my @local_crit = ();
+  foreach my $c (@G::recipients_crit) {              # handle each_recip* vars
+    foreach my $addr (split(/, /, $e->get_var($c->{var}))) {
+      my %t = ( 'cmp' => $c->{cmp}, 'var' => $c->{var} );
+      $t{cmp} =~ s/"?\$var"?/'$addr'/;
+      push(@local_crit, \%t);
+    }
+  }
+  if ($G::show_tests) { print $e->get_var('message_exim_id'), "\n"; }
+  CRITERIA:
+  foreach my $c (@$crit, @local_crit) {
+    my $var = $e->get_var($c->{var});
+    my $ret = eval($c->{cmp});
+    if ($G::show_tests) {
+      printf "  %25s =  '%s'\n  %25s => $ret\n",$c->{var},$var,$c->{cmp},$ret;
+    }
+    if ($@) {
+      print STDERR "Error in eval '$c->{cmp}': $@\n";
+      next MSG;
+    } elsif ($ret) {
+      $match = 1;
+      if ($G::or) { last CRITERIA; }
+      else        { next CRITERIA; }
+    } else { # no match
+      if ($G::or) { next CRITERIA; }
+      else        { next MSG;      }
+    }
+  }
+
+  # skip this message if any criteria were supplied and it didn't match
+  next MSG if ((scalar(@$crit) || scalar(@local_crit)) && !$match);
+
+  if ($count_only || $G::size_only) {
+    $mcount++;
+    $total_size += $e->get_var('message_size');
+  } else {
+    if (@G::sort) {
+      # if we are defining criteria to sort on, save the message here.  If
+      # we don't save here and do the sort later, we have a chicken/egg
+      # problem
+      push(@G::to_print, { vars => {}, output => "" });
+      foreach my $var (@G::sort) {
+        # save any values we want to sort on.  I don't like doing the internal
+        # struct access here, but calling get_var a bunch can be _slow_ =(
+        $G::sort_type{$var} ||= '<=>';
+        $G::to_print[-1]{vars}{$var} = $e->{_vars}{$var};
+        $G::sort_type{$var} = 'cmp' if ($G::to_print[-1]{vars}{$var} =~ /\D/);
+      }
+      $G::to_print[-1]{output} = $e->format_message();
+    } else {
+      print $e->format_message();
+    }
+  }
+
+  if ($G::freeze) {
+    freeze_data($e->get_state());
+    push(@G::frozen_msgs, $m);
+  }
+}
+
+if (@G::to_print) {
+  msg_sort(\@G::to_print, \@G::sort, $G::reverse);
+  foreach my $msg (@G::to_print) {
+    print $msg->{output};
+  }
+}
+
+if ($G::qgrep_c) {
+  print "$mcount matches out of $tcount messages" .
+        ($G::size_only ? " ($total_size)" : "") . "\n";
+} elsif ($G::mailq_bpc) {
+  print "$mcount" .  ($G::size_only ? " ($total_size)" : "") . "\n";
+} elsif ($G::size_only) {
+  print "$total_size\n";
+}
+
+if ($G::freeze) {
+  freeze_message_list(\@G::frozen_msgs);
+  freeze_end();
+} elsif ($G::thaw) {
+  thaw_end();
+}
+
+exit;
+
+# sender_address_domain,shown_message_size
+sub msg_sort {
+  my $msgs    = shift;
+  my $vars    = shift;
+  my $reverse = shift;
+
+  my @pieces = ();
+  foreach my $v (@G::sort) {
+    push(@pieces, "\$a->{vars}{\"$v\"} $G::sort_type{$v} \$b->{vars}{\"$v\"}");
+  }
+  my $sort_str = join(" || ", @pieces);
+
+  @$msgs = sort { eval $sort_str } (@$msgs);
+  @$msgs = reverse(@$msgs) if ($reverse);
+}
+
+sub try_load {
+  my $mod = shift;
+
+  eval("use $mod");
+  return $@ ? 0 : 1;
+}
+
+# FREEZE FILE FORMAT:
+# message_data_bytes
+# message_data
+# <...>
+# EOM
+# message_list
+# message_list_bytes <- 10 bytes, zero-packed, plus \n
+
+sub freeze_start {
+  eval("use Storable");
+  die "Storable module not found: $@\n" if ($@);
+  open(O, ">$G::freeze") || die "Can't open freeze file $G::freeze: $!\n";
+  $G::freeze_handle = \*O;
+}
+
+sub freeze_end {
+  close($G::freeze_handle);
+}
+
+sub thaw_start {
+  eval("use Storable");
+  die "Storable module not found: $@\n" if ($@);
+  open(I, "<$G::thaw") || die "Can't open freeze file $G::thaw: $!\n";
+  $G::freeze_handle = \*I;
+}
+
+sub thaw_end {
+  close($G::freeze_handle);
+}
+
+sub freeze_data {
+  my $h = Storable::freeze($_[0]);
+  print $G::freeze_handle length($h)+1, "\n$h\n";
+}
+
+sub freeze_message_list {
+  my $h = Storable::freeze($_[0]);
+  my $l = length($h) + 1;
+  printf $G::freeze_handle "EOM\n$l\n$h\n%010d\n", $l+11+length($l)+1;
+}
+
+sub thaw_message_list {
+  my $orig_pos = tell($G::freeze_handle);
+  seek($G::freeze_handle, -11, 2);
+  chomp(my $bytes = <$G::freeze_handle>);
+  seek($G::freeze_handle, $bytes * -1, 2);
+  my $obj = thaw_data();
+  seek($G::freeze_handle, 0, $orig_pos);
+  return($obj);
+}
+
+sub thaw_data {
+  my $obj;
+  chomp(my $bytes = <$G::freeze_handle>);
+  return(undef) if (!$bytes || $bytes eq 'EOM');
+  my $read = read(I, $obj, $bytes);
+  die "Format error in thaw file (expected $bytes bytes, got $read)\n"
+      if ($bytes != $read);
+  chomp($obj);
+  return(Storable::thaw($obj));
+}
+
+sub process_criteria {
+  my $a = shift;
+  my @c = ();
+  my $e = 0;
+
+  foreach (@$a) {
+    foreach my $t ('@') { s/$t/\\$t/g; }
+    if (/^(.*?)\s+(<=|>=|==|!=|<|>)\s+(.*)$/) {
+      #print STDERR "found as integer\n";
+      my $v = $1; my $o = $2; my $n = $3;
+      if    ($n =~ /^(-?[\d\.]+)M$/)  { $n = $1 * 1024 * 1024; }
+      elsif ($n =~ /^(-?[\d\.]+)K$/)  { $n = $1 * 1024; }
+      elsif ($n =~ /^(-?[\d\.]+)B?$/) { $n = $1; }
+      elsif ($n =~ /^(-?[\d\.]+)d$/)  { $n = $1 * 60 * 60 * 24; }
+      elsif ($n =~ /^(-?[\d\.]+)h$/)  { $n = $1 * 60 * 60; }
+      elsif ($n =~ /^(-?[\d\.]+)m$/)  { $n = $1 * 60; }
+      elsif ($n =~ /^(-?[\d\.]+)s?$/) { $n = $1; }
+      else {
+        print STDERR "Expression $_ did not parse: numeric comparison with ",
+                     "non-number\n";
+        $e = 1;
+        next;
+      }
+      push(@c, { var => lc($v), cmp => "(\$var $o $n)" });
+    } elsif (/^(.*?)\s+(=~|!~)\s+(.*)$/) {
+      #print STDERR "found as string regexp\n";
+      push(@c, { var => lc($1), cmp => "(\"\$var\" $2 $3)" });
+    } elsif (/^(.*?)\s+=\s+(.*)$/) {
+      #print STDERR "found as bare string regexp\n";
+      my $case = $G::caseful ? '' : 'i';
+      push(@c, { var => lc($1), cmp => "(\"\$var\" =~ /$2/$case)" });
+      # quote special characters in perl text string
+      #foreach my $t ('@') { $c[-1]{cmp} =~ s/$t/\\$t/g; }
+    } elsif (/^(.*?)\s+(eq|ne)\s+(.*)$/) {
+      #print STDERR "found as string cmp\n";
+      my $var = lc($1); my $op = $2; my $val = $3;
+      $val =~ s|^(['"])(.*)\1$|$2|;
+      push(@c, { var => $var, cmp => "(\"\$var\" $op \"$val\")" });
+      if (($var eq 'message_id' || $var eq 'message_exim_id') && $op eq "eq") {
+        #print STDERR "short circuit @c[-1]->{cmp} $val\n";
+        $G::msg_ids->{$val} = 1;
+      }
+      #foreach my $t ('@') { $c[-1]{cmp} =~ s/$t/\\$t/g; }
+    } elsif (/^(\S+)$/) {
+      #print STDERR "found as boolean\n";
+      push(@c, { var => lc($1), cmp => "(\$var)" });
+    } else {
+      print STDERR "Expression $_ did not parse\n";
+      $e = 1;
+      next;
+    }
+    # assign the results of the cmp test here (handle "!" negation)
+    # also handle global --not negation
+    if ($c[-1]{var} =~ s|^!||) {
+      $c[-1]{cmp} .= $G::negate ? " ? 1 : 0" : " ? 0 : 1";
+    } else {
+      $c[-1]{cmp} .= $G::negate ? " ? 0 : 1" : " ? 1 : 0";
+    }
+    # support the each_* psuedo variables.  Steal the criteria off of the
+    # queue for special processing later
+    if ($c[-1]{var} =~ /^each_(recipients(_(un)?del)?)$/) {
+      my $var = $1;
+      push(@G::recipients_crit,pop(@c));
+      $G::recipients_crit[-1]{var} = $var; # remove each_ from the variable
+    }
+  }
+
+  exit(1) if ($e);
+
+  if ($G::show_rules) { foreach (@c) { print "$_->{var}\t$_->{cmp}\n"; } }
+
+  return(\@c);
+}
+
+sub get_all_msgs {
+  my $d = shift();
+  my $i = shift();
+  my $u = shift; # don't sort
+  my $r = shift; # right before returning, reverse order
+  my $o = shift; # if true, randomize list order before returning
+  my @m = ();
+
+  if ($i =~ m|^/|) { $d = $i; } else { $d = $d . '/' . $i; }
+
+  opendir(D, "$d") || die "Couldn't opendir $d: $!\n";
+  foreach my $e (grep !/^\./, readdir(D)) {
+    if ($e =~ /^[a-zA-Z0-9]$/) {
+      opendir(DD, "$d/$e") || next;
+      foreach my $f (grep !/^\./, readdir(DD)) {
+        push(@m, { message => $1, path => "$d/$e" }) if ($f =~ /^(.{16})-H$/);
+      }
+      closedir(DD);
+    } elsif ($e =~ /^(.{16})-H$/) {
+      push(@m, { message => $1, path => $d });
+    }
+  }
+  closedir(D);
+
+  if ($o) {
+    my $c = scalar(@m);
+    # loop twice to pretend we're doing a good job of mixing things up
+    for (my $i = 0; $i < 2 * $c; $i++) {
+      my $rand = int(rand($c));
+      ($m[$i % $c],$m[$rand]) = ($m[$rand],$m[$i % $c]);
+    }
+  } elsif (!$u) {
+    @m = sort { $a->{message} cmp $b->{message} } @m;
+  }
+  @m = reverse(@m) if ($r);
+
+  return(\@m);
+}
+
+BEGIN {
+
+package Exim::SpoolFile;
+
+# versions 4.61 and higher will not need these variables anymore, but they
+# are left for handling legacy installs
+$Exim::SpoolFile::ACL_C_MAX_LEGACY = 10;
+#$Exim::SpoolFile::ACL_M_MAX _LEGACY= 10;
+
+sub new {
+  my $class = shift;
+  my $self  = {};
+  bless($self, $class);
+
+  $self->{_spool_dir}        = '';
+  $self->{_input_path}       = '';
+  $self->{_undelivered_only} = 0;
+  $self->{_show_generated}   = 0;
+  $self->{_output_long}      = 1;
+  $self->{_output_idonly}    = 0;
+  $self->{_output_brief}     = 0;
+  $self->{_output_flatq}     = 0;
+  $self->{_output_vars_only} = 0;
+  $self->{_show_vars}        = [];
+
+  $self->_reset();
+  return($self);
+}
+
+sub output_long {
+  my $self = shift;
+
+  $self->{_output_long}      = 1;
+  $self->{_output_idonly}    = 0;
+  $self->{_output_brief}     = 0;
+  $self->{_output_flatq}     = 0;
+  $self->{_output_vars_only} = 0;
+}
+
+sub output_idonly {
+  my $self = shift;
+
+  $self->{_output_long}      = 0;
+  $self->{_output_idonly}    = 1;
+  $self->{_output_brief}     = 0;
+  $self->{_output_flatq}     = 0;
+  $self->{_output_vars_only} = 0;
+}
+
+sub output_brief {
+  my $self = shift;
+
+  $self->{_output_long}      = 0;
+  $self->{_output_idonly}    = 0;
+  $self->{_output_brief}     = 1;
+  $self->{_output_flatq}     = 0;
+  $self->{_output_vars_only} = 0;
+}
+
+sub output_flatq {
+  my $self = shift;
+
+  $self->{_output_long}      = 0;
+  $self->{_output_idonly}    = 0;
+  $self->{_output_brief}     = 0;
+  $self->{_output_flatq}     = 1;
+  $self->{_output_vars_only} = 0;
+}
+
+sub output_vars_only {
+  my $self = shift;
+
+  $self->{_output_long}      = 0;
+  $self->{_output_idonly}    = 0;
+  $self->{_output_brief}     = 0;
+  $self->{_output_flatq}     = 0;
+  $self->{_output_vars_only} = 1;
+}
+
+sub set_show_vars {
+  my $self = shift;
+  my $s    = shift;
+
+  foreach my $v (split(/\s*,\s*/, $s)) {
+    push(@{$self->{_show_vars}}, $v);
+  }
+}
+
+sub set_show_generated {
+  my $self = shift;
+  $self->{_show_generated} = shift;
+}
+
+sub set_undelivered_only {
+  my $self = shift;
+  $self->{_undelivered_only} = shift;
+}
+
+sub error {
+  my $self = shift;
+  return $self->{_error};
+}
+
+sub _error {
+  my $self = shift;
+  $self->{_error} = shift;
+  return(undef);
+}
+
+sub _reset {
+  my $self = shift;
+
+  $self->{_error}       = '';
+  $self->{_delivered}   = 0;
+  $self->{_message}     = '';
+  $self->{_path}        = '';
+  $self->{_vars}        = {};
+  $self->{_vars_raw}    = {};
+
+  $self->{_numrecips}   = 0;
+  $self->{_udel_tree}   = {};
+  $self->{_del_tree}    = {};
+  $self->{_recips}      = {};
+
+  return($self);
+}
+
+sub parse_message {
+  my $self = shift;
+
+  $self->_reset();
+  $self->{_message} = shift || return(0);
+  $self->{_path}    = shift; # optional path to message
+  return(0) if (!$self->{_input_path});
+  if (!$self->{_path} && !$self->_find_path()) {
+    # assume the message was delivered from under us and ignore
+    $self->{_delivered} = 1;
+    return(1);
+  }
+  $self->_parse_header() || return(0);
+
+  return(1);
+}
+
+# take the output of get_state() and set up a message internally like
+# parse_message (except from a saved data struct, not by parsing the
+# files on disk).
+sub restore_state {
+  my $self = shift;
+  my $h    = shift;
+
+  return(1) if ($h->{_delivered});
+  $self->_reset();
+  $self->{_message} = $h->{_message} || return(0);
+  return(0) if (!$self->{_input_path});
+
+  $self->{_path}      = $h->{_path};
+  $self->{_vars}      = $h->{_vars};
+  $self->{_numrecips} = $h->{_numrecips};
+  $self->{_udel_tree} = $h->{_udel_tree};
+  $self->{_del_tree}  = $h->{_del_tree};
+  $self->{_recips}    = $h->{_recips};
+
+  $self->{_vars}{message_age} = time() - $self->{_vars}{received_time};
+  return(1);
+}
+
+# This returns the state data for a specific message in a format that can
+# be later frozen back in to regain state
+#
+# after calling this function, this specific state is not expect to be
+# reused.  That's because we're returning direct references to specific
+# internal structures.  We're also modifying the structure ourselves
+# by deleting certain internal message variables.
+sub get_state {
+  my $self = shift;
+  my $h    = {};    # this is the hash ref we'll be returning.
+
+  $h->{_delivered} = $self->{_delivered};
+  $h->{_message}   = $self->{_message};
+  $h->{_path}      = $self->{_path};
+  $h->{_vars}      = $self->{_vars};
+  $h->{_numrecips} = $self->{_numrecips};
+  $h->{_udel_tree} = $self->{_udel_tree};
+  $h->{_del_tree}  = $self->{_del_tree};
+  $h->{_recips}    = $self->{_recips};
+
+  # delete some internal variables that we will rebuild later if needed
+  delete($h->{_vars}{message_body});
+  delete($h->{_vars}{message_age});
+
+  return($h);
+}
+
+# keep this sub as a feature if we ever break this module out, but do away
+# with its use in exipick (pass it in from caller instead)
+sub _find_path {
+  my $self = shift;
+
+  return(0) if (!$self->{_message});
+  return(0) if (!$self->{_input_path});
+
+  # test split spool first on the theory that people concerned about
+  # performance will have split spool set =).
+  foreach my $f (substr($self->{_message}, 5, 1).'/', '') {
+    if (-f "$self->{_input_path}/$f$self->{_message}-H") {
+      $self->{_path} = "$self->{_input_path}}/$f";
+      return(1);
+    }
+  }
+  return(0);
+}
+
+sub set_spool {
+  my $self = shift;
+  $self->{_spool_dir} = shift;
+  $self->{_input_path} = shift;
+  if ($self->{_input_path} !~ m|^/|) {
+    $self->{_input_path} = $self->{_spool_dir} . '/' . $self->{_input_path};
+  }
+}
+
+sub get_matching_vars {
+  my $self = shift;
+  my $e    = shift;
+
+  if ($e =~ /^\^/) {
+    my @r = ();
+    foreach my $v (keys %{$self->{_vars}}) { push(@r, $v) if ($v =~ /$e/); }
+    return(@r);
+  } else {
+    return($e);
+  }
+}
+
+# accepts a variable with or without leading '$' or trailing ':'
+sub get_var {
+  my $self = shift;
+  my $var  = lc(shift); $var =~ s/^\$//; $var =~ s/:$//;
+
+  if ($var eq 'message_body' && !defined($self->{_vars}{message_body})) {
+    $self->_parse_body()
+  } elsif ($var =~ s|^([rb]?h)(eader)?_|${1}eader_| &&
+           exists($self->{_vars}{$var}) && !defined($self->{_vars}{$var}))
+  {
+    if ((my $type = $1) eq 'rh') {
+      $self->{_vars}{$var} = join('', @{$self->{_vars_raw}{$var}{vals}});
+    } else {
+      # both bh_ and h_ build their strings from rh_.  Do common work here
+      my $rh = $var; $rh =~ s|^b?|r|;
+      my $comma = 1 if ($self->{_vars_raw}{$rh}{type} =~ /^[BCFRST]$/);
+      foreach (@{$self->{_vars_raw}{$rh}{vals}}) {
+        my $x = $_; # editing $_ here would change the original, which is bad
+        $x =~ s|^\s+||;
+        $x =~ s|\s+$||;
+        if ($comma) { chomp($x); $self->{_vars}{$var} .= "$x,\n"; }
+        else        { $self->{_vars}{$var} .= $x; }
+      }
+      $self->{_vars}{$var} =~ s|[\s\n]*$||;
+      $self->{_vars}{$var} =~ s|,$|| if ($comma);
+      # ok, that's the preprocessing, not do specific processing for h type
+      if ($type eq 'bh') {
+        $self->{_vars}{$var} = $self->_decode_2047($self->{_vars}{$var});
+      } else {
+        $self->{_vars}{$var} =
+            $self->_decode_2047($self->{_vars}{$var}, $charset);
+      }
+    }
+  }
+  elsif ($var eq 'received_count' && !defined($self->{_vars}{received_count}))
+  {
+    $self->{_vars}{received_count} =
+        scalar(@{$self->{_vars_raw}{rheader_received}{vals}});
+  }
+  elsif ($var eq 'message_headers' && !defined($self->{_vars}{message_headers}))
+  {
+    $self->{_vars}{$var} =
+        $self->_decode_2047($self->{_vars}{message_headers_raw}, $charset);
+    chomp($self->{_vars}{$var});
+  }
+  elsif ($var eq 'reply_address' && !defined($self->{_vars}{reply_address}))
+  {
+    $self->{_vars}{reply_address} = exists($self->{_vars}{"header_reply-to"})
+        ? $self->get_var("header_reply-to") : $self->get_var("header_from");
+  }
+
+  #chomp($self->{_vars}{$var}); # I think this was only for headers, obsolete
+  return $self->{_vars}{$var};
+}
+
+sub _decode_2047 {
+  my $self = shift;
+  my $s    = shift; # string to decode
+  my $c    = shift; # target charset.  If empty, just decode, don't convert
+  my $t    = '';    # the translated string
+  my $e    = 0;     # set to true if we get an error in here anywhere
+
+  return($s) if ($s !~ /=\?/); # don't even bother to look if there's no sign
+
+  my @p = ();
+  foreach my $mw (split(/(=\?[^\?]{3,}\?[BQ]\?[^\?]{1,74}\?=)/i, $s)) {
+    next if ($mw eq '');
+    if ($mw =~ /=\?([^\?]{3,})\?([BQ])\?([^\?]{1,74})\?=/i) {
+      push(@p, { data => $3, encoding => uc($2), charset => uc($1),
+                 is_mime => 1 });
+      if ($p[-1]{encoding} eq 'Q') {
+        my @ow = split('', $p[-1]{data});
+        my @nw = ();
+        for (my $i = 0; $i < @ow; $i++) {
+          if ($ow[$i] eq '_') { push(@nw, ' '); }
+          elsif ($ow[$i] eq '=') {
+            if (scalar(@ow) - ($i+1) < 2) {  # ran out of characters
+              $e = 1; last;
+            } elsif ($ow[$i+1] !~ /[\dA-F]/i || $ow[$i+2] !~ /[\dA-F]/i) {
+              $e = 1; last;
+            } else {
+              #push(@nw, chr('0x'.$ow[$i+1].$ow[$i+2]));
+              push(@nw, pack("C", hex($ow[$i+1].$ow[$i+2])));
+              $i += 2;
+            }
+          }
+          elsif ($ow[$i] =~ /\s/) { # whitspace is illegal
+            $e = 1;
+            last;
+          }
+          else { push(@nw, $ow[$i]); }
+        }
+        $p[-1]{data} = join('', @nw);
+      } elsif ($p[-1]{encoding} eq 'B') {
+        my $x = $p[-1]{data};
+        $x    =~ tr#A-Za-z0-9+/##cd;
+        $x    =~ s|=+$||;
+        $x    =~ tr#A-Za-z0-9+/# -_#;
+        my $r = '';
+        while ($x =~ s/(.{1,60})//s) {
+          $r .= unpack("u", chr(32 + int(length($1)*3/4)) . $1);
+        }
+        $p[-1]{data} = $r;
+      }
+    } else {
+      push(@p, { data => $mw, is_mime => 0,
+                 is_ws => ($mw =~ m|^[\s\n]+|sm) ? 1 : 0 });
+    }
+  }
+
+  for (my $i = 0; $i < @p; $i++) {
+    # mark entities we want to skip (whitespace between consecutive mimewords)
+    if ($p[$i]{is_mime} && $p[$i+1]{is_ws} && $p[$i+2]{is_mime}) {
+      $p[$i+1]{skip} = 1;
+    }
+
+    # if word is a mimeword and we have access to Encode and charset was
+    # specified, try to convert text
+    # XXX _cannot_ get consistent conversion results in perl, can't get them
+    # to return same conversions that exim performs.  Until I can figure this
+    # out, don't attempt any conversions (header_ will return same value as
+    # bheader_).
+    #if ($c && $p[$i]{is_mime} && $self->_try_load('Encode')) {
+    #  # XXX not sure how to catch errors here
+    #  Encode::from_to($p[$i]{data}, $p[$i]{charset}, $c);
+    #}
+
+    # replace binary zeros w/ '?' in decoded text
+    if ($p[$i]{is_mime}) { $p[$i]{data} =~ s|\x00|?|g; }
+  }
+
+  if ($e) {
+    return($s);
+  } else {
+    return(join('', map { $_->{data} } grep { !$_->{skip} } @p));
+  }
+}
+
+# This isn't a class func but I'm tired
+sub _try_load {
+  my $self = shift;
+  my $mod  = shift;
+
+  eval("use $mod");
+  return $@ ? 0 : 1;
+}
+
+sub _parse_body {
+  my $self = shift;
+  my $f    = $self->{_path} . '/' . $self->{_message} . '-D';
+  $self->{_vars}{message_body} = ""; # define var so we only come here once
+
+  open(I, "<$f") || return($self->_error("Couldn't open $f: $!"));
+  chomp($_ = <I>);
+  return(0) if ($self->{_message}.'-D' ne $_);
+
+  $self->{_vars}{message_body} = join('', <I>);
+  close(I);
+  $self->{_vars}{message_body} =~ s/\n/ /g;
+  $self->{_vars}{message_body} =~ s/\000/ /g;
+  return(1);
+}
+
+sub _parse_header {
+  my $self = shift;
+  my $f    = $self->{_path} . '/' . $self->{_message} . '-H';
+  $self->{_vars}{header_path} = $f;
+  $self->{_vars}{data_path}   = $self->{_path} . '/' . $self->{_message} . '-D';
+
+  if (!open(I, "<$f")) {
+    # assume message went away and silently ignore
+    $self->{_delivered} = 1;
+    return(1);
+  }
+
+  # There are a few numeric variables that should explicitly be set to
+  # zero if they aren't found in the header.  Technically an empty value
+  # works just as well, but might as well be pedantic
+  $self->{_vars}{body_zerocount}           = 0;
+  $self->{_vars}{host_lookup_deferred}     = 0;
+  $self->{_vars}{host_lookup_failed}       = 0;
+  $self->{_vars}{tls_certificate_verified} = 0;
+
+  chomp($_ = <I>);
+  return(0) if ($self->{_message}.'-H' ne $_);
+  $self->{_vars}{message_id}       = $self->{_message};
+  $self->{_vars}{message_exim_id}  = $self->{_message};
+
+  # line 2
+  chomp($_ = <I>);
+  return(0) if (!/^(.+)\s(\-?\d+)\s(\-?\d+)$/);
+  $self->{_vars}{originator_login} = $1;
+  $self->{_vars}{originator_uid}   = $2;
+  $self->{_vars}{originator_gid}   = $3;
+
+  # line 3
+  chomp($_ = <I>);
+  return(0) if (!/^<(.*)>$/);
+  $self->{_vars}{sender_address}   = $1;
+  $self->{_vars}{sender_address_domain} = $1;
+  $self->{_vars}{sender_address_local_part} = $1;
+  $self->{_vars}{sender_address_domain} =~ s/^.*\@//;
+  $self->{_vars}{sender_address_local_part} =~ s/^(.*)\@.*$/$1/;
+
+  # line 4
+  chomp($_ = <I>);
+  return(0) if (!/^(\d+)\s(\d+)$/);
+  $self->{_vars}{received_time}    = $1;
+  $self->{_vars}{warning_count}    = $2;
+  $self->{_vars}{message_age}      = time() - $self->{_vars}{received_time};
+
+  while (<I>) {
+    chomp();
+    if (/^(-\S+)\s*(.*$)/) {
+      my $tag = $1;
+      my $arg = $2;
+      if ($tag eq '-acl') {
+        my $t;
+        return(0) if ($arg !~ /^(\d+)\s(\d+)$/);
+        if ($1 < $Exim::SpoolFile::ACL_C_MAX_LEGACY) {
+          $t = "acl_c$1";
+        } else {
+          $t = "acl_m" . ($1 - $Exim::SpoolFile::ACL_C_MAX_LEGACY);
+        }
+        read(I, $self->{_vars}{$t}, $2+1) || return(0);
+        chomp($self->{_vars}{$t});
+      } elsif ($tag eq '-aclc') {
+        #return(0) if ($arg !~ /^(\d+)\s(\d+)$/);
+        return(0) if ($arg !~ /^(\S+)\s(\d+)$/);
+        my $t = "acl_c$1";
+        read(I, $self->{_vars}{$t}, $2+1) || return(0);
+        chomp($self->{_vars}{$t});
+      } elsif ($tag eq '-aclm') {
+        #return(0) if ($arg !~ /^(\d+)\s(\d+)$/);
+        return(0) if ($arg !~ /^(\S+)\s(\d+)$/);
+        my $t = "acl_m$1";
+        read(I, $self->{_vars}{$t}, $2+1) || return(0);
+        chomp($self->{_vars}{$t});
+      } elsif ($tag eq '-local') {
+        $self->{_vars}{sender_local} = 1;
+      } elsif ($tag eq '-localerror') {
+        $self->{_vars}{local_error_message} = 1;
+      } elsif ($tag eq '-local_scan') {
+        $self->{_vars}{local_scan_data} = $arg;
+      } elsif ($tag eq '-spam_score_int') {
+        $self->{_vars}{spam_score_int} = $arg;
+        $self->{_vars}{spam_score}     = $arg / 10;
+      } elsif ($tag eq '-bmi_verdicts') {
+        $self->{_vars}{bmi_verdicts} = $arg;
+      } elsif ($tag eq '-host_lookup_deferred') {
+        $self->{_vars}{host_lookup_deferred} = 1;
+      } elsif ($tag eq '-host_lookup_failed') {
+        $self->{_vars}{host_lookup_failed} = 1;
+      } elsif ($tag eq '-body_linecount') {
+        $self->{_vars}{body_linecount} = $arg;
+      } elsif ($tag eq '-max_received_linelength') {
+        $self->{_vars}{max_received_linelength} = $arg;
+      } elsif ($tag eq '-body_zerocount') {
+        $self->{_vars}{body_zerocount} = $arg;
+      } elsif ($tag eq '-frozen') {
+        $self->{_vars}{deliver_freeze} = 1;
+        $self->{_vars}{deliver_frozen_at} = $arg;
+      } elsif ($tag eq '-allow_unqualified_recipient') {
+        $self->{_vars}{allow_unqualified_recipient} = 1;
+      } elsif ($tag eq '-allow_unqualified_sender') {
+        $self->{_vars}{allow_unqualified_sender} = 1;
+      } elsif ($tag eq '-deliver_firsttime') {
+        $self->{_vars}{deliver_firsttime} = 1;
+        $self->{_vars}{first_delivery} = 1;
+      } elsif ($tag eq '-manual_thaw') {
+        $self->{_vars}{deliver_manual_thaw} = 1;
+        $self->{_vars}{manually_thawed} = 1;
+      } elsif ($tag eq '-auth_id') {
+        $self->{_vars}{authenticated_id} = $arg;
+      } elsif ($tag eq '-auth_sender') {
+        $self->{_vars}{authenticated_sender} = $arg;
+      } elsif ($tag eq '-sender_set_untrusted') {
+        $self->{_vars}{sender_set_untrusted} = 1;
+      } elsif ($tag eq '-tls_certificate_verified') {
+        $self->{_vars}{tls_certificate_verified} = 1;
+      } elsif ($tag eq '-tls_cipher') {
+        $self->{_vars}{tls_cipher} = $arg;
+      } elsif ($tag eq '-tls_peerdn') {
+        $self->{_vars}{tls_peerdn} = $arg;
+      } elsif ($tag eq '-tls_sni') {
+        $self->{_vars}{tls_sni} = $arg;
+      } elsif ($tag eq '-host_address') {
+        $self->{_vars}{sender_host_port} = $self->_get_host_and_port(\$arg);
+        $self->{_vars}{sender_host_address} = $arg;
+      } elsif ($tag eq '-interface_address') {
+        $self->{_vars}{received_port} =
+            $self->{_vars}{interface_port} = $self->_get_host_and_port(\$arg);
+        $self->{_vars}{received_ip_address} =
+            $self->{_vars}{interface_address} = $arg;
+      } elsif ($tag eq '-active_hostname') {
+        $self->{_vars}{smtp_active_hostname} = $arg;
+      } elsif ($tag eq '-host_auth') {
+        $self->{_vars}{sender_host_authenticated} = $arg;
+      } elsif ($tag eq '-host_name') {
+        $self->{_vars}{sender_host_name} = $arg;
+      } elsif ($tag eq '-helo_name') {
+        $self->{_vars}{sender_helo_name} = $arg;
+      } elsif ($tag eq '-ident') {
+        $self->{_vars}{sender_ident} = $arg;
+      } elsif ($tag eq '-received_protocol') {
+        $self->{_vars}{received_protocol} = $arg;
+      } elsif ($tag eq '-N') {
+        $self->{_vars}{dont_deliver} = 1;
+      } else {
+        # unrecognized tag, save it for reference
+        $self->{$tag} = $arg;
+      }
+    } else {
+      last;
+    }
+  }
+
+  # when we drop out of the while loop, we have the first line of the
+  # delivered tree in $_
+  do {
+    if ($_ eq 'XX') {
+      ; # noop
+    } elsif ($_ =~ s/^[YN][YN]\s+//) {
+      $self->{_del_tree}{$_} = 1;
+    } else {
+      return(0);
+    }
+    chomp($_ = <I>);
+  } while ($_ !~ /^\d+$/);
+
+  $self->{_numrecips} = $_;
+  $self->{_vars}{recipients_count} = $self->{_numrecips};
+  for (my $i = 0; $i < $self->{_numrecips}; $i++) {
+    chomp($_ = <I>);
+    return(0) if (/^$/);
+    my $addr = '';
+    if (/^(.*)\s\d+,(\d+),\d+$/) {
+      #print STDERR "exim3 type (untested): $_\n";
+      $self->{_recips}{$1} = { pno => $2 };
+      $addr = $1;
+    } elsif (/^(.*)\s(\d+)$/) {
+      #print STDERR "exim4 original type (untested): $_\n";
+      $self->{_recips}{$1} = { pno => $2 };
+      $addr = $1;
+    } elsif (/^(.*)\s(.*)\s(\d+),(\d+)#1$/) {
+      #print STDERR "exim4 new type #1 (untested): $_\n";
+      return($self->_error("incorrect format: $_")) if (length($2) != $3);
+      $self->{_recips}{$1} = { pno => $4, errors_to => $2 };
+      $addr = $1;
+    } elsif (/^(\S*)\s(\S*)\s(\d+),(\d+)\s(\S*)\s(\d+),(-?\d+)#3$/) {
+      #print STDERR "exim4 new type #3 DSN (untested): $_\n";
+      return($self->_error("incorrect format: $_"))
+        if ((length($2) != $3) || (length($5) != $6));
+      $self->{_recips}{$1} = { pno => $7, errors_to => $5 };
+      $addr = $1;
+    } elsif (/^.*#(\d+)$/) {
+      #print STDERR "exim4 #$1 style (unimplemented): $_\n";
+      $self->_error("exim4 #$1 style (unimplemented): $_");
+    } else {
+      #print STDERR "default type: $_\n";
+      $self->{_recips}{$_} = {};
+      $addr = $_;
+    }
+    $self->{_udel_tree}{$addr} = 1 if (!$self->{_del_tree}{$addr});
+  }
+  $self->{_vars}{recipients}         = join(', ', keys(%{$self->{_recips}}));
+  $self->{_vars}{recipients_del}     = join(', ', keys(%{$self->{_del_tree}}));
+  $self->{_vars}{recipients_undel}   = join(', ', keys(%{$self->{_udel_tree}}));
+  $self->{_vars}{recipients_undel_count} = scalar(keys(%{$self->{_udel_tree}}));
+  $self->{_vars}{recipients_del_count}   = 0;
+  foreach my $r (keys %{$self->{_del_tree}}) {
+    next if (!$self->{_recips}{$r});
+    $self->{_vars}{recipients_del_count}++;
+  }
+
+  # blank line
+  $_ = <I>;
+  return(0) if (!/^$/);
+
+  # start reading headers
+  while (read(I, $_, 3) == 3) {
+    my $t = getc(I);
+    return(0) if (!length($t));
+    while ($t =~ /^\d$/) {
+      $_ .= $t;
+      $t  = getc(I);
+    }
+    my $hdr_flag  = $t;
+    my $hdr_bytes = $_;
+    $t            = getc(I);              # strip the space out of the file
+    return(0) if (read(I, $_, $hdr_bytes) != $hdr_bytes);
+    if ($hdr_flag ne '*') {
+      $self->{_vars}{message_linecount} += (tr/\n//);
+      $self->{_vars}{message_size}      += $hdr_bytes;
+    }
+
+    # mark (rb)?header_ vars as existing and store raw value.  They'll be
+    # processed further in get_var() if needed
+    my($v,$d) = split(/:/, $_, 2);
+    $v = "header_" . lc($v);
+    $self->{_vars}{$v} = $self->{_vars}{"b$v"} = $self->{_vars}{"r$v"} = undef;
+    push(@{$self->{_vars_raw}{"r$v"}{vals}}, $d);
+    $self->{_vars_raw}{"r$v"}{type} = $hdr_flag;
+    $self->{_vars}{message_headers_raw} .= $_;
+  }
+  close(I);
+
+  $self->{_vars}{message_body_size} =
+      (stat($self->{_path}.'/'.$self->{_message}.'-D'))[7] - 19;
+  if ($self->{_vars}{message_body_size} < 0) {
+    $self->{_vars}{message_size} = 0;
+    $self->{_vars}{message_body_missing} = 1;
+  } else {
+    $self->{_vars}{message_size} += $self->{_vars}{message_body_size} + 1;
+  }
+
+  $self->{_vars}{message_linecount} += $self->{_vars}{body_linecount};
+
+  my $i = $self->{_vars}{message_size};
+  if ($i == 0)          { $i = ""; }
+  elsif ($i < 1024)     { $i = sprintf("%d",    $i);                    }
+  elsif ($i < 10240)    { $i = sprintf("%.1fK", $i / 1024);             }
+  elsif ($i < 1048576)  { $i = sprintf("%dK",   ($i+512)/1024);         }
+  elsif ($i < 10485760) { $i = sprintf("%.1fM", $i/1048576);            }
+  else                  { $i = sprintf("%dM",   ($i + 524288)/1048576); }
+  $self->{_vars}{shown_message_size} = $i;
+
+  return(1);
+}
+
+# mimic exim's host_extract_port function - receive a ref to a scalar,
+# strip it of port, return port
+sub _get_host_and_port {
+  my $self = shift;
+  my $host = shift; # scalar ref, be careful
+
+  if ($$host =~ /^\[([^\]]+)\](?:\:(\d+))?$/) {
+    $$host = $1;
+    return($2 || 0);
+  } elsif ($$host =~ /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})(?:\.(\d+))?$/) {
+    $$host = $1;
+    return($2 || 0);
+  } elsif ($$host =~ /^([\d\:]+)(?:\.(\d+))?$/) {
+    $$host = $1;
+    return($2 || 0);
+  }
+  # implicit else
+  return(0);
+}
+
+# honoring all formatting preferences, return a scalar variable of the
+# information for the single message matching what exim -bp would show.
+# We can print later if we want.
+sub format_message {
+  my $self = shift;
+  my $o    = '';
+  return if ($self->{_delivered});
+
+  # define any vars we want to print out for this message.  The requests
+  # can be regexps, and the defined vars can change for each message, so we
+  # have to build this list for each message
+  my @vars = ();
+  if (@{$self->{_show_vars}}) {
+    my %t = ();
+    foreach my $e (@{$self->{_show_vars}}) {
+      foreach my $v ($self->get_matching_vars($e)) {
+        next if ($t{$v}); $t{$v}++; push(@vars, $v);
+      }
+    }
+  }
+
+  if ($self->{_output_idonly}) {
+    $o .= $self->{_message};
+    foreach my $v (@vars) { $o .= " $v='" . $self->get_var($v) . "'"; }
+    $o .= "\n";
+    return $o;
+  } elsif ($self->{_output_vars_only}) {
+    foreach my $v (@vars) { $o .= $self->get_var($v) . "\n"; }
+    return $o;
+  }
+
+  if ($self->{_output_long} || $self->{_output_flatq}) {
+    my $i = int($self->{_vars}{message_age} / 60);
+    if ($i > 90) {
+      $i = int(($i+30)/60);
+      if ($i > 72) { $o .= sprintf "%2dd ", int(($i+12)/24); }
+      else { $o .= sprintf "%2dh ", $i; }
+    } else { $o .= sprintf "%2dm ", $i; }
+
+    if ($self->{_output_flatq} && @vars) {
+        $o .= join(';', map { "$_='".$self->get_var($_)."'" } (@vars)
+                  );
+    } else {
+      $o .= sprintf "%5s", $self->{_vars}{shown_message_size};
+    }
+    $o .= " ";
+  }
+  $o .= "$self->{_message} ";
+  $o .= "From: " if ($self->{_output_brief});
+  $o .= "<$self->{_vars}{sender_address}>";
+
+  if ($self->{_output_long}) {
+    $o .= " ($self->{_vars}{originator_login})"
+        if ($self->{_vars}{sender_set_untrusted});
+
+    # XXX exim contains code here to print spool format errors
+    $o .= " *** frozen ***" if ($self->{_vars}{deliver_freeze});
+    $o .= "\n";
+
+    foreach my $v (@vars) {
+      $o .= sprintf "  %25s = '%s'\n", $v, $self->get_var($v);
+    }
+
+    foreach my $r (keys %{$self->{_recips}}) {
+      next if ($self->{_del_tree}{$r} && $self->{_undelivered_only});
+      $o .= sprintf "        %s %s\n", $self->{_del_tree}{$r} ? "D" : " ", $r;
+    }
+    if ($self->{_show_generated}) {
+      foreach my $r (keys %{$self->{_del_tree}}) {
+        next if ($self->{_recips}{$r});
+        $o .= sprintf "       +D %s\n", $r;
+      }
+    }
+  } elsif ($self->{_output_brief}) {
+    my @r = ();
+    foreach my $r (keys %{$self->{_recips}}) {
+      next if ($self->{_del_tree}{$r});
+      push(@r, $r);
+    }
+    $o .= " To: " . join(';', @r);
+    if (scalar(@vars)) {
+      $o .= " Vars: ".join(';',map { "$_='".$self->get_var($_)."'" } (@vars));
+    }
+  } elsif ($self->{_output_flatq}) {
+    $o .= " *** frozen ***" if ($self->{_vars}{deliver_freeze});
+    my @r = ();
+    foreach my $r (keys %{$self->{_recips}}) {
+      next if ($self->{_del_tree}{$r});
+      push(@r, $r);
+    }
+    $o .= " " . join(' ', @r);
+  }
+
+  $o .= "\n";
+  return($o);
+}
+
+sub print_message {
+  my $self = shift;
+  my $fh   = shift || \*STDOUT;
+  return if ($self->{_delivered});
+
+  print $fh $self->format_message();
+}
+
+sub dump {
+  my $self = shift;
+
+  foreach my $k (sort keys %$self) {
+    my $r = ref($self->{$k});
+    if ($r eq 'ARRAY') {
+      printf "%20s <<EOM\n", $k;
+      print @{$self->{$k}}, "EOM\n";
+    } elsif ($r eq 'HASH') {
+      printf "%20s <<EOM\n", $k;
+      foreach (sort keys %{$self->{$k}}) {
+        printf "%20s %s\n", $_, $self->{$k}{$_};
+      }
+      print "EOM\n";
+    } else {
+      printf "%20s %s\n", $k, $self->{$k};
+    }
+  }
+}
+
+} # BEGIN
+
+sub ext_usage {
+  if ($ARGV[0] =~ /^--help$/i) {
+    require Config;
+    $ENV{PATH} .= ":" unless $ENV{PATH} eq "";
+    $ENV{PATH} = "$ENV{PATH}$Config::Config{'installscript'}";
+    #exec("perldoc", "-F", "-U", $0) || exit 1;
+    $< = $> = 1 if ($> == 0 || $< == 0);
+    exec("perldoc", $0) || exit 1;
+    # make parser happy
+    %Config::Config = ();
+  } elsif ($ARGV[0] =~ /^--version$/i) {
+    print "$p_name version $p_version\n\n$p_cp\n";
+  } else {
+    return;
+  }
+
+  exit(0);
+}
+
+__END__
+
+=head1 NAME
+
+exipick - selectively display messages from an Exim queue
+
+=head1 SYNOPSIS
+
+exipick [<options>] [<criterion> [<criterion> ...]]
+
+=head1 DESCRIPTION
+
+exipick is a tool to display messages in an Exim queue.  It is very similar to exiqgrep and is, in fact, a drop in replacement for exiqgrep.  exipick allows you to select messages to be displayed using any piece of data stored in an Exim spool file.  Matching messages can be displayed in a variety of formats.
+
+=head1 QUICK START
+
+Delete every frozen message from queue:
+    exipick -zi | xargs exim -Mrm
+
+Show only messages which have not yet been virus scanned:
+    exipick '$received_protocol ne virus-scanned'
+
+Run the queue in a semi-random order:
+    exipick -i --random | xargs exim -M
+
+Show the count and total size of all messages which either originated from localhost or have a received protocol of 'local':
+    exipick --or --size --bpc \
+            '$sender_host_address eq 127.0.0.1' \
+            '$received_protocol eq local'
+
+Display all messages received on the MSA port, ordered first by the sender's email domain and then by the size of the emails:
+    exipick --sort sender_address_domain,message_size \
+            '$received_port == 587'
+
+Display only messages whose every recipient is in the example.com domain, also listing the IP address of the sending host:
+    exipick --show-vars sender_host_address \
+            '$each_recipients = example.com'
+
+Same as above, but show values for all defined variables starting with sender_ and the number of recipients:
+    exipick --show-vars ^sender_,recipients_count \
+            '$each_recipients = example.com'
+
+=head1 OPTIONS
+
+=over 4
+
+=item --and
+
+Display messages matching all criteria (default)
+
+=item -b
+
+Display messages in brief format (exiqgrep)
+
+=item -bp
+
+Display messages in standard mailq format (default)
+
+=item -bpa
+
+Same as -bp, show generated addresses also (exim)
+
+=item -bpc
+
+Show a count of matching messages (exim)
+
+=item -bpr
+
+Same as '-bp --unsorted' (exim)
+
+=item -bpra
+
+Same as '-bpa --unsorted' (exim)
+
+=item -bpru
+
+Same as '-bpu --unsorted' (exim)
+
+=item -bpu
+
+Same as -bp, but only show undelivered messages (exim)
+
+=item -c
+
+Show a count of matching messages (exiqgrep)
+
+=item --caseful
+
+Make operators involving '=' honor case
+
+=item --charset
+
+Override the default local character set for $header_ decoding
+
+=item -f <regexp>
+
+Same as '$sender_address =~ /<regexp>/' (exiqgrep).  Note that this preserves the default case sensitivity of exiqgrep's interface.
+
+=item --finput
+
+Same as '--input-dir Finput'.  'Finput' is where exim copies frozen messages when compiled with SUPPORT_MOVE_FROZEN_MESSAGES.
+
+=item --flatq
+
+Use a single-line output format
+
+=item --freeze <cache file>
+
+Save queue information in an quickly retrievable format
+
+=item --help
+
+Display this output
+
+=item -i
+
+Display only the message IDs (exiqgrep)
+
+=item --input-dir <inputname>
+
+Set the name of the directory under the spool directory.  By defaut this is "input".  If this starts with '/', the value of --spool is ignored.  See also --finput.
+
+=item -l
+
+Same as -bp (exiqgrep)
+
+=item --not
+
+Negate all tests.
+
+=item -o <seconds>
+
+Same as '$message_age > <seconds>' (exiqgrep)
+
+=item --or
+
+Display messages matching any criteria
+
+=item -R
+
+Same as --reverse (exiqgrep)
+
+=item -r <regexp>
+
+Same as '$recipients =~ /<regexp>/' (exiqgrep).  Note that this preserves the default case sensitivity of exiqgrep's interface.
+
+=item --random
+
+Display messages in random order
+
+=item --reverse
+
+Display messages in reverse order
+
+=item -s <string>
+
+Same as '$shown_message_size eq <string>' (exiqgrep)
+
+=item --spool <path>
+
+Set the path to the exim spool to use.  This value will have the argument to --input or 'input' appended, or be ignored if --input is a full path.
+
+=item --show-rules
+
+Show the internal representation of each criterion specified
+
+=item --show-tests
+
+Show the result of each criterion on each message
+
+=item --show-vars <variable>[,<variable>...]
+
+Show the value for <variable> for each displayed message.  <variable> will be a regular expression if it begins with a circumflex.
+
+=item --size
+
+Show the total bytes used by each displayed message
+
+=item --thaw <cache file>
+
+Read queue information cached from a previous --freeze run
+
+=item --sort <variable>[,<variable>...]
+
+Display matching messages sorted according to <variable>
+
+=item --unsorted
+
+Do not apply any sorting to output
+
+=item --version
+
+Display the version of this command
+
+=item -x
+
+Same as '!$deliver_freeze' (exiqgrep)
+
+=item -y
+
+Same as '$message_age < <seconds>' (exiqgrep)
+
+=item -z
+
+Same as '$deliver_freeze' (exiqgrep)
+
+=back
+
+=head1 CRITERIA
+
+Exipick decides which messages to display by applying a test against each message.  The rules take the general form of 'VARIABLE OPERATOR VALUE'.  For example, '$message_age > 60'.  When exipick is deciding which messages to display, it checks the $message_age variable for each message.  If a message's age is greater than 60, the message will be displayed.  If the message's age is 60 or less seconds, it will not be displayed.
+
+Multiple criteria can be used.  The order they are specified does not matter.  By default all criteria must evaluate to true for a message to be displayed.  If the --or option is used, a message is displayed as long as any of the criteria evaluate to true.
+
+See the VARIABLES and OPERATORS sections below for more details
+
+=head1 OPERATORS
+
+=over 4
+
+=item BOOLEAN
+
+Boolean variables are checked simply by being true or false.  There is no real operator except negation.  Examples of valid boolean tests:
+  '$deliver_freeze'
+  '!$deliver_freeze'
+
+=item NUMERIC
+
+Valid comparisons are <, <=, >, >=, ==, and !=.  Numbers can be integers or floats.  Any number in a test suffixed with d, h, m, s, M, K, or B will be multiplied by 86400, 3600, 60, 1, 1048576, 1024, or 1 respectively.  Examples of valid numeric tests:
+  '$message_age >= 3d'
+  '$local_interface == 587'
+  '$message_size < 30K'
+
+=item STRING
+
+The string operators are =, eq, ne, =~, and !~.  With the exception of '=', the operators all match the functionality of the like-named perl operators.  eq and ne match a string exactly.  !~, =~, and = apply a perl regular expression to a string.  The '=' operator behaves just like =~ but you are not required to place // around the regular expression.  Examples of valid string tests:
+  '$received_protocol eq esmtp'
+  '$sender_address = example.com'
+  '$each_recipients =~ /^a[a-z]{2,3}@example.com$/'
+
+=item NEGATION
+
+There are many ways to negate tests, each having a reason for existing.  Many tests can be negated using native operators.  For instance, >1 is the opposite of <=1 and eq and ne are opposites.  In addition, each individual test can be negated by adding a ! at the beginning of the test.  For instance, '!$acl_m1 =~ /^DENY$/' is the same as '$acl_m1 !~ /^DENY$/'.  Finally, every test can be specified by using the command line argument --not.  This is functionally equivalent to adding a ! to the beginning of every test.
+
+=back
+
+=head1 VARIABLES
+
+With a few exceptions the available variables match Exim's internal expansion variables in both name and exact contents.  There are a few notable additions and format deviations which are noted below.  Although a brief explanation is offered below, Exim's spec.txt should be consulted for full details.  It is important to remember that not every variable will be defined for every message.  For example, $sender_host_port is not defined for messages not received from a remote host.
+
+Internally, all variables are represented as strings, meaning any operator will work on any variable.  This means that '$sender_host_name > 4' is a legal criterion, even if it does not produce meaningful results.  Variables in the list below are marked with a 'type' to help in choosing which types of operators make sense to use.
+
+  Identifiers
+    B - Boolean variables
+    S - String variables
+    N - Numeric variables
+    . - Standard variable matching Exim's content definition
+    # - Standard variable, contents differ from Exim's definition
+    + - Non-standard variable
+
+=over 4
+
+=item S . $acl_c0-$acl_c9, $acl_m0-$acl_m9
+
+User definable variables.
+
+=item B + $allow_unqualified_recipient
+
+TRUE if unqualified recipient addresses are permitted in header lines.
+
+=item B + $allow_unqualified_sender
+
+TRUE if unqualified sender addresses are permitted in header lines.
+
+=item S . $authenticated_id
+
+Optional saved information from authenticators, or the login name of the calling process for locally submitted messages.
+
+=item S . $authenticated_sender
+
+The value of AUTH= param for smtp messages, or a generated value from the calling processes login and qualify domain for locally submitted messages.
+
+=item S . $bheader_*, $bh_*
+
+Value of the header(s) with the same name with any RFC2047 words decoded if present.  See section 11.5 of Exim's spec.txt for full details.
+
+=item S + $bmi_verdicts
+
+The verdict string provided by a Brightmail content scan
+
+=item N . $body_linecount
+
+The number of lines in the message's body.
+
+=item N . $body_zerocount
+
+The number of binary zero bytes in the message's body.
+
+=item S + $data_path
+
+The path to the body file's location in the filesystem.
+
+=item B + $deliver_freeze
+
+TRUE if the message is currently frozen.
+
+=item N + $deliver_frozen_at
+
+The epoch time at which message was frozen.
+
+=item B + $dont_deliver
+
+TRUE if, under normal circumstances, Exim will not try to deliver the message.
+
+=item S + $each_recipients
+
+This is a psuedo variable which allows you to apply a test against each address in $recipients individually.  Whereas '$recipients =~ /@aol.com/' will match if any recipient address contains aol.com, '$each_recipients =~ /@aol.com$/' will only be true if every recipient matches that pattern.  Note that this obeys --and or --or being set.  Using it with --or is very similar to just matching against $recipients, but with the added benefit of being able to use anchors at the beginning and end of each recipient address.
+
+=item S + $each_recipients_del
+
+Like $each_recipients, but for $recipients_del
+
+=item S + $each_recipients_undel
+
+Like $each_recipients, but for $recipients_undel
+
+=item B . $first_delivery
+
+TRUE if the message has never been deferred.
+
+=item S . $header_*, $h_*
+
+This will always match the contents of the corresponding $bheader_* variable currently (the same behaviour Exim displays when iconv is not installed).
+
+=item S + $header_path
+
+The path to the header file's location in the filesystem.
+
+=item B . $host_lookup_deferred
+
+TRUE if there was an attempt to look up the host's name from its IP address, but an error occurred that during the attempt.
+
+=item B . $host_lookup_failed
+
+TRUE if there was an attempt to look up the host's name from its IP address, but the attempt returned a negative result.
+
+=item B + $local_error_message
+
+TRUE if the message is a locally-generated error message.
+
+=item S . $local_scan_data
+
+The text returned by the local_scan() function when a message is received.
+
+=item B . $manually_thawed
+
+TRUE when the message has been manually thawed.
+
+=item N . $max_received_linelength
+
+The number of bytes in the longest line that was received as part of the message, not counting line termination characters.
+
+=item N . $message_age
+
+The number of seconds since the message was received.
+
+=item S # $message_body
+
+The message's body.  Unlike Exim's variable of the same name, this variable contains the entire message body.  Newlines and nulls are replaced by spaces.
+
+=item B + $message_body_missing
+
+TRUE is a message's spool data file (-D file) is missing or unreadable.
+
+=item N . $message_body_size
+
+The size of the body in bytes.
+
+=item S . $message_exim_id, $message_id
+
+The unique message id that is used by Exim to identify the message.  $message_id is deprecated as of Exim 4.53.
+
+=item S . $message_headers
+
+A concatenation of all the header lines except for lines added by routers or transports.  RFC2047 decoding is performed
+
+=item S . $message_headers_raw
+
+A concatenation of all the header lines except for lines added by routers or transports.  No decoding or translation is performed.
+
+=item N . $message_linecount
+
+The number of lines in the entire message (body and headers).
+
+=item N . $message_size
+
+The size of the message in bytes.
+
+=item N . $originator_gid
+
+The group id under which the process that called Exim was running as when the message was received.
+
+=item S + $originator_login
+
+The login of the process which called Exim.
+
+=item N . $originator_uid
+
+The user id under which the process that called Exim was running as when the message was received.
+
+=item S . $received_ip_address, $interface_address
+
+The address of the local IP interface for network-originated messages.  $interface_address is deprecated as of Exim 4.64
+
+=item N . $received_port, $interface_port
+
+The local port number if network-originated messages.  $interface_port is deprecated as of Exim 4.64
+
+=item N . $received_count
+
+The number of Received: header lines in the message.
+
+=item S . $received_protocol
+
+The name of the protocol by which the message was received.
+
+=item N . $received_time
+
+The epoch time at which the message was received.
+
+=item S # $recipients
+
+The list of envelope recipients for a message.  Unlike Exim's version, this variable always contains every recipient of the message.  The recipients are separated by a comma and a space.  See also $each_recipients.
+
+=item N . $recipients_count
+
+The number of envelope recipients for the message.
+
+=item S + $recipients_del
+
+The list of delivered envelope recipients for a message.  This non-standard variable is in the same format as $recipients and contains the list of already-delivered recipients including any generated addresses.  See also $each_recipients_del.
+
+=item N + $recipients_del_count
+
+The number of envelope recipients for the message which have already been delivered.  Note that this is the count of original recipients to which the message has been delivered.  It does not include generated addresses so it is possible that this number will be less than the number of addresses in the $recipients_del string.
+
+=item S + $recipients_undel
+
+The list of undelivered envelope recipients for a message.  This non-standard variable is in the same format as $recipients and contains the list of undelivered recipients.  See also $each_recipients_undel.
+
+=item N + $recipients_undel_count
+
+The number of envelope recipients for the message which have not yet been delivered.
+
+=item S . $reply_address
+
+The contents of the Reply-To: header line if one exists and it is not empty, or otherwise the contents of the From: header line.
+
+=item S . $rheader_*, $rh_*
+
+The value of the message's header(s) with the same name.  See section 11.5 of Exim's spec.txt for full description.
+
+=item S . $sender_address
+
+The sender's address that was received in the message's envelope.  For bounce messages, the value of this variable is the empty string.
+
+=item S . $sender_address_domain
+
+The domain part of $sender_address.
+
+=item S . $sender_address_local_part
+
+The local part of $sender_address.
+
+=item S . $sender_helo_name
+
+The HELO or EHLO value supplied for smtp or bsmtp messages.
+
+=item S . $sender_host_address
+
+The remote host's IP address.
+
+=item S . $sender_host_authenticated
+
+The name of the authenticator driver which successfully authenticated the client from which the message was received.
+
+=item S . $sender_host_name
+
+The remote host's name as obtained by looking up its IP address.
+
+=item N . $sender_host_port
+
+The port number that was used on the remote host for network-originated messages.
+
+=item S . $sender_ident
+
+The identification received in response to an RFC 1413 request for remote messages, the login name of the user that called Exim for locally generated messages.
+
+=item B + $sender_local
+
+TRUE if the message was locally generated.
+
+=item B + $sender_set_untrusted
+
+TRUE if the envelope sender of this message was set by an untrusted local caller.
+
+=item S + $shown_message_size
+
+This non-standard variable contains the formatted size string.  That is, for a message whose $message_size is 66566 bytes, $shown_message_size is 65K.
+
+=item S . $smtp_active_hostname
+
+The value of the active host name when the message was received, as specified by the "smtp_active_hostname" option.
+
+=item S . $spam_score
+
+The spam score of the message, for example '3.4' or '30.5'.  (Requires exiscan or WITH_CONTENT_SCAN)
+
+=item S . $spam_score_int
+
+The spam score of the message, multiplied by ten, as an integer value.  For instance '34' or '305'.  (Requires exiscan or WITH_CONTENT_SCAN)
+
+=item B . $tls_certificate_verified
+
+TRUE if a TLS certificate was verified when the message was received.
+
+=item S . $tls_cipher
+
+The cipher suite that was negotiated for encrypted SMTP connections.
+
+=item S . $tls_peerdn
+
+The value of the Distinguished Name of the certificate if Exim is configured to request one
+
+=item S . $tls_sni
+
+The value of the Server Name Indication TLS extension sent by a client, if one was sent.
+
+=item N + $warning_count
+
+The number of delay warnings which have been sent for this message.
+
+=back
+
+=head1 CONTACT
+
+=over 4
+
+=item EMAIL: proj-exipick@jetmore.net
+
+=item HOME: jetmore.org/john/code/#exipick
+
+=back
+
+=cut
diff --git a/src/exiqgrep.src b/src/exiqgrep.src
new file mode 100644 (file)
index 0000000..afecbff
--- /dev/null
@@ -0,0 +1,198 @@
+#!PERL_COMMAND
+
+# Utility for searching and displaying queue information.
+# Written by Matt Hubbard 15 August 2002
+
+# Except when they appear in comments, the following placeholders in this
+# source are replaced when it is turned into a runnable script:
+#
+# BIN_DIRECTORY
+# PERL_COMMAND
+
+# PROCESSED_FLAG
+
+
+# Routine for extracting the UTC timestamp from message ID
+# lifted from eximstat utility
+
+# Version 1.2
+
+use strict;
+use Getopt::Std;
+
+# Have this variable point to your exim binary.
+my $exim = 'BIN_DIRECTORY/exim';
+my $eargs = '-bpu';
+my %id;
+my %opt;
+my $count = 0;
+my $mcount = 0;
+my @tab62 =
+  (0,1,2,3,4,5,6,7,8,9,0,0,0,0,0,0,     # 0-9
+   0,10,11,12,13,14,15,16,17,18,19,20,  # A-K
+  21,22,23,24,25,26,27,28,29,30,31,32,  # L-W
+  33,34,35, 0, 0, 0, 0, 0,              # X-Z
+   0,36,37,38,39,40,41,42,43,44,45,46,  # a-k
+  47,48,49,50,51,52,53,54,55,56,57,58,  # l-w
+  59,60,61);                            # x-z
+
+my $base;
+if ($^O eq 'darwin') { # aka MacOS X
+  $base = 36;
+ } else {
+  $base = 62;
+};
+
+getopts('hf:r:y:o:s:C:zxlibRca',\%opt);
+if ($opt{h}) { &help; exit;}
+if ($opt{a}) { $eargs = '-bp'; }
+if ($opt{C} && -e $opt{C} && -f $opt{C} && -R $opt{C}) { $eargs .= ' -C '.$opt{C}; }
+
+# Read message queue output into hash
+&collect();
+# Identify which messages match selection criteria
+&selection();
+# Print matching data according to display option.
+&display();
+exit;
+
+
+sub help() {
+       print <<'EOF'
+Exim message queue display utility.
+
+       -h              This help message.
+       -C              Specify which exim.conf to use.
+
+Selection criteria:
+       -f <regexp>     Match sender address sender (field is "< >" wrapped)
+       -r <regexp>     Match recipient address
+       -s <regexp>     Match against the size field from long output
+       -y <seconds>    Message younger than
+       -o <seconds>    Message older than
+       -z              Frozen messages only (exclude non-frozen)
+       -x              Non-frozen messages only (exclude frozen)
+
+[ NB: for regexps, provided string sits in /<string>/ ]
+
+Display options:
+       -c              Display match count
+       -l              Long Format [Default]
+       -i              Message IDs only
+       -b              Brief Format
+       -R              Reverse order
+       -a              All recipients (including delivered)
+EOF
+}
+
+sub collect() {
+       open(QUEUE,"$exim $eargs |") or die("Error openning pipe: $!\n");
+       while(<QUEUE>) {
+               chomp();
+               my $line = $_;
+               #Should be 1st line of record, if not error.
+               if ($line =~ /^\s*(\w+)\s+((?:\d+(?:\.\d+)?[A-Z]?)?)\s*(\w{6}-\w{6}-\w{2})\s+(<.*?>)/) {
+                       my $msg = $3;
+                       $id{$msg}{age} = $1;
+                       $id{$msg}{size} = $2;
+                       $id{$msg}{from} = $4;
+                       $id{$msg}{birth} = &msg_utc($msg);
+                       $id{$msg}{ages} = time - $id{$msg}{birth};
+                       if ($line =~ /\*\*\* frozen \*\*\*$/) {
+                               $id{$msg}{frozen} = 1;
+                       } else {
+                               $id{$msg}{frozen} = 0;
+                       }
+                       while(<QUEUE> =~ /\s+(.*?\@.*)$/) {
+                               push(@{$id{$msg}{rcpt}},$1);
+                       }
+                       # Increment message counter.
+                       $count++;
+               } else {
+                       print STDERR "Line mismatch: $line\n"; exit 1;
+               }
+       }
+       close(QUEUE) or die("Error closing pipe: $!\n");
+}
+
+sub selection() {
+       foreach my $msg (keys(%id)) {
+               if ($opt{f}) {
+                       # Match sender address
+                       next unless ($id{$msg}{from} =~ /$opt{f}/i);
+               }
+               if ($opt{r}) {
+                       # Match any recipient address
+                       my $match = 0;
+                       foreach my $rcpt (@{$id{$msg}{rcpt}}) {
+                               $match++ if ($rcpt =~ /$opt{r}/i);
+                       }
+                       next unless ($match);
+               }
+               if ($opt{s}) {
+                       # Match against the size string.
+                       next unless ($id{$msg}{size} =~ /$opt{s}/);
+               }
+               if ($opt{y}) {
+                       # Match younger than
+                       next unless ($id{$msg}{ages} < $opt{y});
+               }
+               if ($opt{o}) {
+                       # Match older than
+                       next unless ($id{$msg}{ages} > $opt{o});
+               }
+               if ($opt{z}) {
+                       # Exclude non frozen
+                       next unless ($id{$msg}{frozen});
+               }
+               if ($opt{x}) {
+                       # Exclude frozen
+                       next if ($id{$msg}{frozen});
+               }
+               # Here's what we do to select the record.
+               # Should only get this far if the message passed all of
+               # the active tests.
+               $id{$msg}{d} = 1;
+               # Increment match counter.
+               $mcount++;
+       }
+}
+
+sub display() {
+       if ($opt{c}) {
+               printf("%d matches out of %d messages\n",$mcount,$count);
+               exit;
+       }
+       foreach my $msg (sort { $opt{R} ? $id{$b}{birth} <=> $id{$a}{birth} : $id{$a}{birth} <=> $id{$b}{birth} } keys(%id) ) {
+               if (exists($id{$msg}{d})) {
+                       if ($opt{i}) {
+                               # Just the msg ID
+                               print $msg, "\n";
+                       } elsif ($opt{b}) {
+                               # Brief format
+                               printf("%s From: %s To: %s\n",$msg,$id{$msg}{from},join(';',@{$id{$msg}{rcpt}}))
+                       } else {
+                               # Otherwise Long format attempted duplication of original format.
+                               printf("%3s %5s %s %s%s\n",$id{$msg}{age},$id{$msg}{size},$msg,$id{$msg}{from},$id{$msg}{frozen} ? " *** frozen ***" : "");
+                               foreach my $rcpt (@{$id{$msg}{rcpt}}) {
+                                       printf("          %s\n",$rcpt);
+                               }
+                               print "\n";
+                       }
+               }
+       }
+}
+
+sub report() {
+       foreach my $msg (keys(%id)) {
+               print "$id{$msg}{birth} $msg\tAge: $id{$msg}{age}\tSize: $id{$msg}{size}\tFrom: $id{$msg}{from}\tTo: " . join(" ",@{$id{$msg}{rcpt}}). "\n";
+       }
+}
+
+sub msg_utc() {
+       my $id = substr((pop @_), 0, 6);
+       my $s = 0;
+       my @c = split(//, $id);
+       while($#c >= 0) { $s = $s * $base + $tab62[ord(shift @c) - ord('0')] }
+       return $s;
+}
diff --git a/src/exiqsumm.src b/src/exiqsumm.src
new file mode 100644 (file)
index 0000000..fc5ad26
--- /dev/null
@@ -0,0 +1,167 @@
+#! PERL_COMMAND -w
+
+# Mail Queue Summary
+# Christoph Lameter, 21 May 1997
+# Modified by Philip Hazel, June 1997
+# Bug fix: June 1998 by Philip Hazel
+#   Message sizes not listed by -bp with K or M
+#   suffixes were getting divided by 10.
+# Bug fix: October 1998 by Philip Hazel
+#   Sorting wasn't working right with Perl 5.005
+#   Fix provided by John Horne
+# Bug fix: November 1998 by Philip Hazel
+#   Failing to recognize domain literals in recipient addresses
+#   Fix provided by Malcolm Ray
+# Bug fix: July 2002 by Philip Hazel
+#   Not handling time periods of more than 100 days
+#   Fix provided by Randy Banks
+# Added summary line: September 2002 by Philip Hazel
+#   Code provided by Joachim Wieland
+# June 2003 by Philip Hazel
+#   Initialize $size, $age, $id to avoid warnings when bad
+#   data is provided
+# Bug fix: July 2003 by Philip Hazel
+#   Incorrectly skipping the first lines of messages whose
+#   message ID ends in 'D'! Before Exim 4.14 this didn't
+#   matter because they never did. Looks like an original
+#   typo. Fix provided by Chris Liddiard.
+# November 2006 by Jori Hamalainen
+#   Added feature to separate frozen and bounced messages from queue
+#   Adedd feature to list queue per source - destination pair
+#   Changed regexps to compile once to very minor speed optimization
+#   Short circuit for empty lines
+#
+# Usage: mailq | exiqsumm [-a] [-b] [-c] [-f] [-s]
+#   Default sorting is by domain name
+#   -a sorts by age of oldest message
+#   -b enables bounce message separation
+#   -c sorts by count of message
+#   -f enables frozen message separation
+#   -s enables source destination separation
+
+# Slightly modified sub from eximstats
+
+sub print_volume_rounded {
+my($x) = pop @_;
+if ($x < 10000)
+  {
+  return sprintf("%6d", $x);
+  }
+elsif ($x < 10000000)
+  {
+  return sprintf("%4dKB", ($x + 512)/1024);
+  }
+else
+  {
+  return sprintf("%4dMB", ($x + 512*1024)/(1024*1024));
+  }
+}
+
+sub s_conv {
+  my($x) = @_;
+  my($v,$s) = $x =~ /([\d\.]+)([A-Z]|)/o;
+  if ($s eq "K") { return $v * 1024 };
+  if ($s eq "M") { return $v * 1024 * 1024 };
+  return $v;
+}
+
+sub older {
+  my($x1,$x2) = @_;
+  my($v1,$s1) = $x1 =~ /(\d+)(\w)/o;
+  my($v2,$s2) = $x2 =~ /(\d+)(\w)/o;
+  return $v1 <=> $v2 if ($s1 eq $s2);
+  return (($s2 eq "m") ||
+          ($s2 eq "h" && $s1 eq "d") ||
+          ($s2 eq "d" && $s1 eq "w"))? 1 : -1;
+}
+
+#
+# Main Program
+#
+
+$sort_by_count = 0;
+$sort_by_age = 0;
+
+$size = "0";
+$age = "0d";
+$id = "";
+
+
+while (@ARGV > 0 && substr($ARGV[0], 0, 1) eq "-")
+  {
+  if ($ARGV[0] eq "-a") { $sort_by_age = 1; }
+  if ($ARGV[0] eq "-c") { $sort_by_count = 1; }
+  if ($ARGV[0] eq "-f") { $enable_frozen = 1; }
+  if ($ARGV[0] eq "-b") { $enable_bounces = 1; }
+  if ($ARGV[0] eq "-s") { $enable_source = 1; }
+  shift @ARGV;
+  }
+
+while (<>)
+{
+# Skip empty and already delivered lines
+
+if (/^$/o || /^\s*D\s\S+/o) { next; }
+
+# If it's the first line of a message, pick out the data. Note: it may
+# have text after the final > (e.g. frozen) so don't insist that it ends >.
+
+if (/^([\d\s]{2,3}\w)\s+(\S+)\s(\S+)\s\<(\S*)\>/o)
+  {
+  ($age,$size,$id,$src)=($1,$2,$3,$4);
+  $src =~ s/([^\@]*)\@(.*?)$/$2/o;
+  if (/\*\*\*\sfrozen\s\*\*\*/o) { $frozen=1; } else { $frozen=0; }
+  if ($src eq "") { $bounce=1; $src="<>"; } else { $bounce=0; }
+  }
+
+# Else check for a recipient line: to handle source-routed addresses, just
+# pick off the first domain.
+
+elsif (/^\s+[^@]*\@([\w\.\-]+|\[(\d+\.){3}\d+\])/o)
+  {
+  if ($enable_source) {
+      $domain = "\L$src > $1";
+  } else {
+      $domain = "\L$1";
+  }
+  $domain .= " (b)" if ($bounce && $enable_bounces);
+  $domain .= " (f)" if ($frozen && $enable_frozen);
+  $queue{$domain}++;
+  $q_oldest{$domain} = $age
+    if (!defined $q_oldest{$domain} || &older($age,$q_oldest{$domain}) > 0);
+  $q_recent{$domain} = $age
+    if (!defined $q_recent{$domain} || &older($q_recent{$domain},$age) > 0);
+  $q_size{$domain} = 0 if (!defined $q_size{$domain});
+  $q_size{$domain} += &s_conv($size);
+  }
+}
+
+print "\nCount  Volume  Oldest  Newest  Domain";
+print "\n-----  ------  ------  ------  ------\n\n";
+
+my ($count, $volume, $max_age, $min_age) = (0, 0, "0m", undef);
+
+foreach $id (sort
+            {
+            $sort_by_age? &older($q_oldest{$b}, $q_oldest{$a}) :
+            $sort_by_count? ($queue{$b} <=> $queue{$a}) :
+            $a cmp $b
+            }
+            keys %queue)
+  {
+  printf("%5d  %.6s  %6s  %6s  %.80s\n",
+    $queue{$id}, &print_volume_rounded($q_size{$id}), $q_oldest{$id},
+    $q_recent{$id}, $id);
+    $max_age = $q_oldest{$id} if &older($q_oldest{$id}, $max_age) > 0;
+    $min_age = $q_recent{$id}
+      if (!defined $min_age || &older($min_age, $q_recent{$id}) > 0);
+    $volume += $q_size{$id};
+    $count += $queue{$id};
+  }
+  $min_age ||= "0000d";
+printf("---------------------------------------------------------------\n");
+printf("%5d  %.6s  %6s  %6s  %.80s\n",
+  $count, &print_volume_rounded($volume), $max_age, $min_age, "TOTAL");
+print "\n";
+
+# End
diff --git a/src/exiwhat.src b/src/exiwhat.src
new file mode 100644 (file)
index 0000000..2542b01
--- /dev/null
@@ -0,0 +1,135 @@
+#! /bin/sh
+
+# Copyright (c) University of Cambridge, 1995 - 2007
+# See the file NOTICE for conditions of use and distribution.
+
+# Except when they appear in comments, the following placeholders in this
+# source are replaced when it is turned into a runnable script:
+#
+# CONFIGURE_FILE_USE_NODE
+# CONFIGURE_FILE
+# BIN_DIRECTORY
+# EXIWHAT_PS_CMD
+# EXIWHAT_PS_ARG
+# EXIWHAT_KILL_SIGNAL
+# EXIWHAT_EGREP_ARG
+# EXIWHAT_MULTIKILL_CMD
+# EXIWHAT_MULTIKILL_ARG
+
+# PROCESSED_FLAG
+
+# Shell script for seeing what the exim processes are doing. It gets rid
+# of the old process log, then sends SIGUSR1 to all exim processes to get
+# them to write their state to the log. Then it displays the contents of
+# the log.
+
+# The following lines are generated from Exim's configuration file when
+# this source is built into a script, but you can subsequently edit them
+# without rebuilding things, as long are you are careful not to overwrite
+# the script in the next Exim rebuild/install. However, it's best to
+# arrange your build-time configuration file to get the correct values.
+
+# Some operating systems have a command that finds processes that match
+# certain conditions (by default usually those running specific commands)
+# and sends them signals. If such a command is defined for your OS, the
+# following variables are set and used.
+
+multikill_cmd=EXIWHAT_MULTIKILL_CMD
+multikill_arg=EXIWHAT_MULTIKILL_ARG
+
+# In other operating systems, Exim has to use "ps" and "egrep" to find the
+# processes itself. In those cases, the next three variables are used:
+
+ps_cmd=EXIWHAT_PS_CMD
+ps_arg=EXIWHAT_PS_ARG
+egrep_arg=EXIWHAT_EGREP_ARG
+
+# In both cases, kill_arg is the argument for the (multi)kill command to send
+# SIGUSR1 (at least one OS requires a numeric value).
+
+signal=EXIWHAT_KILL_SIGNAL
+
+# See if this installation is using the esoteric "USE_NODE" feature of Exim,
+# in which it uses the host's name as a suffix for the configuration file name.
+
+if [ "CONFIGURE_FILE_USE_NODE" = "yes" ]; then
+  hostsuffix=.`uname -n`
+fi
+
+# Now find the configuration file name. This has got complicated because
+# CONFIGURE_FILE may now be a list of files. The one that is used is the first
+# one that exists. Mimic the code in readconf.c by testing first for the
+# suffixed file in each case.
+
+set `awk -F: '{ for (i = 1; i <= NF; i++) print $i }' <<End
+CONFIGURE_FILE
+End
+`
+while [ "$config" = "" -a $# -gt 0 ] ; do
+  if [ -f "$1$hostsuffix" ] ; then
+    config="$1$hostsuffix"
+  elif [ -f "$1" ] ; then
+    config="$1"
+  fi
+  shift
+done
+
+# check we have a config file
+if [ "$config" = "" -o ! -f "$config" ]; then
+  echo Config file not found.
+  exit 1
+fi
+
+# Determine where the spool directory is. Search for an exim_path setting
+# in the configure file; otherwise use the bin directory. Call that version of
+# Exim to find the spool directory. BEWARE: a tab character is needed in the
+# first command below. It has had a nasty tendency to get lost in the past. Use
+# a variable to hold a space and a tab. This is less likely to be touched.
+
+st='    '
+exim_path=`grep "^[$st]*exim_path" $config | sed "s/.*=[$st]*//"`
+if test "$exim_path" = ""; then exim_path=BIN_DIRECTORY/exim; fi
+spool_directory=`$exim_path -C $config -bP spool_directory | sed "s/.*=[ ]*//"`
+process_log_path=`$exim_path -C $config -bP process_log_path | sed "s/.*=[ ]*//"`
+
+# The file that Exim writes when sent the SIGUSR1 signal is specified by
+# the process_log_path option. If that is not defined, Exim uses the file
+# called "exim-process.info" in the spool directory.
+
+log=$process_log_path
+if [ "$log" = "" ] ; then
+  log=$spool_directory/exim-process.info
+fi
+
+# Now do the job.
+
+/bin/rm -f ${log}
+if [ -f ${log} ]; then
+  echo "** Failed to remove ${log}"
+  exit 1
+fi
+
+# If there is a multikill command, use it. On some OS this command is called
+# "killall" (Linux, FreeBSD). On Solaris it is called "pkill". Note that on
+# Solaris, "killall" kills ALL processes - this is the System V version of this
+# command, and not what we want!
+
+if [ "$multikill_cmd" != "" ] && type "$multikill_cmd" >/dev/null 2>&1; then
+  $multikill_cmd $signal "$multikill_arg"
+
+# No multikill command; do it the hard way
+
+else
+  $ps_cmd $ps_arg | \
+    egrep "$egrep_arg" | \
+    awk "{print \"kill $signal \"\$1}" | \
+    uniq | sh
+fi
+
+sleep 1
+
+if [ ! -s ${log} ] ; then echo "No exim process data" ;
+  else sort -nu ${log} ; fi
+
+
+# End of exiwhat
diff --git a/src/expand.c b/src/expand.c
new file mode 100644 (file)
index 0000000..70d7c7d
--- /dev/null
@@ -0,0 +1,7107 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2014 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+
+/* Functions for handling string expansion. */
+
+
+#include "exim.h"
+
+/* Recursively called function */
+
+static uschar *expand_string_internal(uschar *, BOOL, uschar **, BOOL, BOOL, BOOL *);
+static int_eximarith_t expanded_string_integer(uschar *, BOOL);
+
+#ifdef STAND_ALONE
+#ifndef SUPPORT_CRYPTEQ
+#define SUPPORT_CRYPTEQ
+#endif
+#endif
+
+#ifdef LOOKUP_LDAP
+#include "lookups/ldap.h"
+#endif
+
+#ifdef SUPPORT_CRYPTEQ
+#ifdef CRYPT_H
+#include <crypt.h>
+#endif
+#ifndef HAVE_CRYPT16
+extern char* crypt16(char*, char*);
+#endif
+#endif
+
+/* The handling of crypt16() is a mess. I will record below the analysis of the
+mess that was sent to me. We decided, however, to make changing this very low
+priority, because in practice people are moving away from the crypt()
+algorithms nowadays, so it doesn't seem worth it.
+
+<quote>
+There is an algorithm named "crypt16" in Ultrix and Tru64.  It crypts
+the first 8 characters of the password using a 20-round version of crypt
+(standard crypt does 25 rounds).  It then crypts the next 8 characters,
+or an empty block if the password is less than 9 characters, using a
+20-round version of crypt and the same salt as was used for the first
+block.  Charaters after the first 16 are ignored.  It always generates
+a 16-byte hash, which is expressed together with the salt as a string
+of 24 base 64 digits.  Here are some links to peruse:
+
+        http://cvs.pld.org.pl/pam/pamcrypt/crypt16.c?rev=1.2
+        http://seclists.org/bugtraq/1999/Mar/0076.html
+
+There's a different algorithm named "bigcrypt" in HP-UX, Digital Unix,
+and OSF/1.  This is the same as the standard crypt if given a password
+of 8 characters or less.  If given more, it first does the same as crypt
+using the first 8 characters, then crypts the next 8 (the 9th to 16th)
+using as salt the first two base 64 digits from the first hash block.
+If the password is more than 16 characters then it crypts the 17th to 24th
+characters using as salt the first two base 64 digits from the second hash
+block.  And so on: I've seen references to it cutting off the password at
+40 characters (5 blocks), 80 (10 blocks), or 128 (16 blocks).  Some links:
+
+        http://cvs.pld.org.pl/pam/pamcrypt/bigcrypt.c?rev=1.2
+        http://seclists.org/bugtraq/1999/Mar/0109.html
+        http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/HTML/AA-Q0R2D-
+             TET1_html/sec.c222.html#no_id_208
+
+Exim has something it calls "crypt16".  It will either use a native
+crypt16 or its own implementation.  A native crypt16 will presumably
+be the one that I called "crypt16" above.  The internal "crypt16"
+function, however, is a two-block-maximum implementation of what I called
+"bigcrypt".  The documentation matches the internal code.
+
+I suspect that whoever did the "crypt16" stuff for Exim didn't realise
+that crypt16 and bigcrypt were different things.
+
+Exim uses the LDAP-style scheme identifier "{crypt16}" to refer
+to whatever it is using under that name.  This unfortunately sets a
+precedent for using "{crypt16}" to identify two incompatible algorithms
+whose output can't be distinguished.  With "{crypt16}" thus rendered
+ambiguous, I suggest you deprecate it and invent two new identifiers
+for the two algorithms.
+
+Both crypt16 and bigcrypt are very poor algorithms, btw.  Hashing parts
+of the password separately means they can be cracked separately, so
+the double-length hash only doubles the cracking effort instead of
+squaring it.  I recommend salted SHA-1 ({SSHA}), or the Blowfish-based
+bcrypt ({CRYPT}$2a$).
+</quote>
+*/
+
+
+
+#ifndef nelements
+# define nelements(arr) (sizeof(arr) / sizeof(*arr))
+#endif
+
+/*************************************************
+*            Local statics and tables            *
+*************************************************/
+
+/* Table of item names, and corresponding switch numbers. The names must be in
+alphabetical order. */
+
+static uschar *item_table[] = {
+  US"acl",
+  US"certextract",
+  US"dlfunc",
+  US"extract",
+  US"filter",
+  US"hash",
+  US"hmac",
+  US"if",
+  US"length",
+  US"listextract",
+  US"lookup",
+  US"map",
+  US"nhash",
+  US"perl",
+  US"prvs",
+  US"prvscheck",
+  US"readfile",
+  US"readsocket",
+  US"reduce",
+  US"run",
+  US"sg",
+  US"substr",
+  US"tr" };
+
+enum {
+  EITEM_ACL,
+  EITEM_CERTEXTRACT,
+  EITEM_DLFUNC,
+  EITEM_EXTRACT,
+  EITEM_FILTER,
+  EITEM_HASH,
+  EITEM_HMAC,
+  EITEM_IF,
+  EITEM_LENGTH,
+  EITEM_LISTEXTRACT,
+  EITEM_LOOKUP,
+  EITEM_MAP,
+  EITEM_NHASH,
+  EITEM_PERL,
+  EITEM_PRVS,
+  EITEM_PRVSCHECK,
+  EITEM_READFILE,
+  EITEM_READSOCK,
+  EITEM_REDUCE,
+  EITEM_RUN,
+  EITEM_SG,
+  EITEM_SUBSTR,
+  EITEM_TR };
+
+/* Tables of operator names, and corresponding switch numbers. The names must be
+in alphabetical order. There are two tables, because underscore is used in some
+cases to introduce arguments, whereas for other it is part of the name. This is
+an historical mis-design. */
+
+static uschar *op_table_underscore[] = {
+  US"from_utf8",
+  US"local_part",
+  US"quote_local_part",
+  US"reverse_ip",
+  US"time_eval",
+  US"time_interval"};
+
+enum {
+  EOP_FROM_UTF8,
+  EOP_LOCAL_PART,
+  EOP_QUOTE_LOCAL_PART,
+  EOP_REVERSE_IP,
+  EOP_TIME_EVAL,
+  EOP_TIME_INTERVAL };
+
+static uschar *op_table_main[] = {
+  US"address",
+  US"addresses",
+  US"base62",
+  US"base62d",
+  US"domain",
+  US"escape",
+  US"eval",
+  US"eval10",
+  US"expand",
+  US"h",
+  US"hash",
+  US"hex2b64",
+  US"hexquote",
+  US"l",
+  US"lc",
+  US"length",
+  US"listcount",
+  US"listnamed",
+  US"mask",
+  US"md5",
+  US"nh",
+  US"nhash",
+  US"quote",
+  US"randint",
+  US"rfc2047",
+  US"rfc2047d",
+  US"rxquote",
+  US"s",
+  US"sha1",
+  US"sha256",
+  US"stat",
+  US"str2b64",
+  US"strlen",
+  US"substr",
+  US"uc",
+  US"utf8clean" };
+
+enum {
+  EOP_ADDRESS =  sizeof(op_table_underscore)/sizeof(uschar *),
+  EOP_ADDRESSES,
+  EOP_BASE62,
+  EOP_BASE62D,
+  EOP_DOMAIN,
+  EOP_ESCAPE,
+  EOP_EVAL,
+  EOP_EVAL10,
+  EOP_EXPAND,
+  EOP_H,
+  EOP_HASH,
+  EOP_HEX2B64,
+  EOP_HEXQUOTE,
+  EOP_L,
+  EOP_LC,
+  EOP_LENGTH,
+  EOP_LISTCOUNT,
+  EOP_LISTNAMED,
+  EOP_MASK,
+  EOP_MD5,
+  EOP_NH,
+  EOP_NHASH,
+  EOP_QUOTE,
+  EOP_RANDINT,
+  EOP_RFC2047,
+  EOP_RFC2047D,
+  EOP_RXQUOTE,
+  EOP_S,
+  EOP_SHA1,
+  EOP_SHA256,
+  EOP_STAT,
+  EOP_STR2B64,
+  EOP_STRLEN,
+  EOP_SUBSTR,
+  EOP_UC,
+  EOP_UTF8CLEAN };
+
+
+/* Table of condition names, and corresponding switch numbers. The names must
+be in alphabetical order. */
+
+static uschar *cond_table[] = {
+  US"<",
+  US"<=",
+  US"=",
+  US"==",     /* Backward compatibility */
+  US">",
+  US">=",
+  US"acl",
+  US"and",
+  US"bool",
+  US"bool_lax",
+  US"crypteq",
+  US"def",
+  US"eq",
+  US"eqi",
+  US"exists",
+  US"first_delivery",
+  US"forall",
+  US"forany",
+  US"ge",
+  US"gei",
+  US"gt",
+  US"gti",
+  US"inlist",
+  US"inlisti",
+  US"isip",
+  US"isip4",
+  US"isip6",
+  US"ldapauth",
+  US"le",
+  US"lei",
+  US"lt",
+  US"lti",
+  US"match",
+  US"match_address",
+  US"match_domain",
+  US"match_ip",
+  US"match_local_part",
+  US"or",
+  US"pam",
+  US"pwcheck",
+  US"queue_running",
+  US"radius",
+  US"saslauthd"
+};
+
+enum {
+  ECOND_NUM_L,
+  ECOND_NUM_LE,
+  ECOND_NUM_E,
+  ECOND_NUM_EE,
+  ECOND_NUM_G,
+  ECOND_NUM_GE,
+  ECOND_ACL,
+  ECOND_AND,
+  ECOND_BOOL,
+  ECOND_BOOL_LAX,
+  ECOND_CRYPTEQ,
+  ECOND_DEF,
+  ECOND_STR_EQ,
+  ECOND_STR_EQI,
+  ECOND_EXISTS,
+  ECOND_FIRST_DELIVERY,
+  ECOND_FORALL,
+  ECOND_FORANY,
+  ECOND_STR_GE,
+  ECOND_STR_GEI,
+  ECOND_STR_GT,
+  ECOND_STR_GTI,
+  ECOND_INLIST,
+  ECOND_INLISTI,
+  ECOND_ISIP,
+  ECOND_ISIP4,
+  ECOND_ISIP6,
+  ECOND_LDAPAUTH,
+  ECOND_STR_LE,
+  ECOND_STR_LEI,
+  ECOND_STR_LT,
+  ECOND_STR_LTI,
+  ECOND_MATCH,
+  ECOND_MATCH_ADDRESS,
+  ECOND_MATCH_DOMAIN,
+  ECOND_MATCH_IP,
+  ECOND_MATCH_LOCAL_PART,
+  ECOND_OR,
+  ECOND_PAM,
+  ECOND_PWCHECK,
+  ECOND_QUEUE_RUNNING,
+  ECOND_RADIUS,
+  ECOND_SASLAUTHD
+};
+
+
+/* Types of table entry */
+
+enum vtypes {
+  vtype_int,            /* value is address of int */
+  vtype_filter_int,     /* ditto, but recognized only when filtering */
+  vtype_ino,            /* value is address of ino_t (not always an int) */
+  vtype_uid,            /* value is address of uid_t (not always an int) */
+  vtype_gid,            /* value is address of gid_t (not always an int) */
+  vtype_bool,           /* value is address of bool */
+  vtype_stringptr,      /* value is address of pointer to string */
+  vtype_msgbody,        /* as stringptr, but read when first required */
+  vtype_msgbody_end,    /* ditto, the end of the message */
+  vtype_msgheaders,     /* the message's headers, processed */
+  vtype_msgheaders_raw, /* the message's headers, unprocessed */
+  vtype_localpart,      /* extract local part from string */
+  vtype_domain,         /* extract domain from string */
+  vtype_string_func,   /* value is string returned by given function */
+  vtype_todbsdin,       /* value not used; generate BSD inbox tod */
+  vtype_tode,           /* value not used; generate tod in epoch format */
+  vtype_todel,          /* value not used; generate tod in epoch/usec format */
+  vtype_todf,           /* value not used; generate full tod */
+  vtype_todl,           /* value not used; generate log tod */
+  vtype_todlf,          /* value not used; generate log file datestamp tod */
+  vtype_todzone,        /* value not used; generate time zone only */
+  vtype_todzulu,        /* value not used; generate zulu tod */
+  vtype_reply,          /* value not used; get reply from headers */
+  vtype_pid,            /* value not used; result is pid */
+  vtype_host_lookup,    /* value not used; get host name */
+  vtype_load_avg,       /* value not used; result is int from os_getloadavg */
+  vtype_pspace,         /* partition space; value is T/F for spool/log */
+  vtype_pinodes,        /* partition inodes; value is T/F for spool/log */
+  vtype_cert           /* SSL certificate */
+  #ifndef DISABLE_DKIM
+  ,vtype_dkim           /* Lookup of value in DKIM signature */
+  #endif
+};
+
+/* Type for main variable table */
+
+typedef struct {
+  const char *name;
+  enum vtypes type;
+  void       *value;
+} var_entry;
+
+/* Type for entries pointing to address/length pairs. Not currently
+in use. */
+
+typedef struct {
+  uschar **address;
+  int  *length;
+} alblock;
+
+static uschar * fn_recipients(void);
+
+/* This table must be kept in alphabetical order. */
+
+static var_entry var_table[] = {
+  /* WARNING: Do not invent variables whose names start acl_c or acl_m because
+     they will be confused with user-creatable ACL variables. */
+  { "acl_arg1",            vtype_stringptr,   &acl_arg[0] },
+  { "acl_arg2",            vtype_stringptr,   &acl_arg[1] },
+  { "acl_arg3",            vtype_stringptr,   &acl_arg[2] },
+  { "acl_arg4",            vtype_stringptr,   &acl_arg[3] },
+  { "acl_arg5",            vtype_stringptr,   &acl_arg[4] },
+  { "acl_arg6",            vtype_stringptr,   &acl_arg[5] },
+  { "acl_arg7",            vtype_stringptr,   &acl_arg[6] },
+  { "acl_arg8",            vtype_stringptr,   &acl_arg[7] },
+  { "acl_arg9",            vtype_stringptr,   &acl_arg[8] },
+  { "acl_narg",            vtype_int,         &acl_narg },
+  { "acl_verify_message",  vtype_stringptr,   &acl_verify_message },
+  { "address_data",        vtype_stringptr,   &deliver_address_data },
+  { "address_file",        vtype_stringptr,   &address_file },
+  { "address_pipe",        vtype_stringptr,   &address_pipe },
+  { "authenticated_fail_id",vtype_stringptr,  &authenticated_fail_id },
+  { "authenticated_id",    vtype_stringptr,   &authenticated_id },
+  { "authenticated_sender",vtype_stringptr,   &authenticated_sender },
+  { "authentication_failed",vtype_int,        &authentication_failed },
+#ifdef WITH_CONTENT_SCAN
+  { "av_failed",           vtype_int,         &av_failed },
+#endif
+#ifdef EXPERIMENTAL_BRIGHTMAIL
+  { "bmi_alt_location",    vtype_stringptr,   &bmi_alt_location },
+  { "bmi_base64_tracker_verdict", vtype_stringptr, &bmi_base64_tracker_verdict },
+  { "bmi_base64_verdict",  vtype_stringptr,   &bmi_base64_verdict },
+  { "bmi_deliver",         vtype_int,         &bmi_deliver },
+#endif
+  { "body_linecount",      vtype_int,         &body_linecount },
+  { "body_zerocount",      vtype_int,         &body_zerocount },
+  { "bounce_recipient",    vtype_stringptr,   &bounce_recipient },
+  { "bounce_return_size_limit", vtype_int,    &bounce_return_size_limit },
+  { "caller_gid",          vtype_gid,         &real_gid },
+  { "caller_uid",          vtype_uid,         &real_uid },
+  { "compile_date",        vtype_stringptr,   &version_date },
+  { "compile_number",      vtype_stringptr,   &version_cnumber },
+  { "csa_status",          vtype_stringptr,   &csa_status },
+#ifdef EXPERIMENTAL_DCC
+  { "dcc_header",          vtype_stringptr,   &dcc_header },
+  { "dcc_result",          vtype_stringptr,   &dcc_result },
+#endif
+#ifdef WITH_OLD_DEMIME
+  { "demime_errorlevel",   vtype_int,         &demime_errorlevel },
+  { "demime_reason",       vtype_stringptr,   &demime_reason },
+#endif
+#ifndef DISABLE_DKIM
+  { "dkim_algo",           vtype_dkim,        (void *)DKIM_ALGO },
+  { "dkim_bodylength",     vtype_dkim,        (void *)DKIM_BODYLENGTH },
+  { "dkim_canon_body",     vtype_dkim,        (void *)DKIM_CANON_BODY },
+  { "dkim_canon_headers",  vtype_dkim,        (void *)DKIM_CANON_HEADERS },
+  { "dkim_copiedheaders",  vtype_dkim,        (void *)DKIM_COPIEDHEADERS },
+  { "dkim_created",        vtype_dkim,        (void *)DKIM_CREATED },
+  { "dkim_cur_signer",     vtype_stringptr,   &dkim_cur_signer },
+  { "dkim_domain",         vtype_stringptr,   &dkim_signing_domain },
+  { "dkim_expires",        vtype_dkim,        (void *)DKIM_EXPIRES },
+  { "dkim_headernames",    vtype_dkim,        (void *)DKIM_HEADERNAMES },
+  { "dkim_identity",       vtype_dkim,        (void *)DKIM_IDENTITY },
+  { "dkim_key_granularity",vtype_dkim,        (void *)DKIM_KEY_GRANULARITY },
+  { "dkim_key_nosubdomains",vtype_dkim,       (void *)DKIM_NOSUBDOMAINS },
+  { "dkim_key_notes",      vtype_dkim,        (void *)DKIM_KEY_NOTES },
+  { "dkim_key_srvtype",    vtype_dkim,        (void *)DKIM_KEY_SRVTYPE },
+  { "dkim_key_testing",    vtype_dkim,        (void *)DKIM_KEY_TESTING },
+  { "dkim_selector",       vtype_stringptr,   &dkim_signing_selector },
+  { "dkim_signers",        vtype_stringptr,   &dkim_signers },
+  { "dkim_verify_reason",  vtype_dkim,        (void *)DKIM_VERIFY_REASON },
+  { "dkim_verify_status",  vtype_dkim,        (void *)DKIM_VERIFY_STATUS},
+#endif
+#ifdef EXPERIMENTAL_DMARC
+  { "dmarc_ar_header",     vtype_stringptr,   &dmarc_ar_header },
+  { "dmarc_domain_policy", vtype_stringptr,   &dmarc_domain_policy },
+  { "dmarc_status",        vtype_stringptr,   &dmarc_status },
+  { "dmarc_status_text",   vtype_stringptr,   &dmarc_status_text },
+  { "dmarc_used_domain",   vtype_stringptr,   &dmarc_used_domain },
+#endif
+  { "dnslist_domain",      vtype_stringptr,   &dnslist_domain },
+  { "dnslist_matched",     vtype_stringptr,   &dnslist_matched },
+  { "dnslist_text",        vtype_stringptr,   &dnslist_text },
+  { "dnslist_value",       vtype_stringptr,   &dnslist_value },
+  { "domain",              vtype_stringptr,   &deliver_domain },
+  { "domain_data",         vtype_stringptr,   &deliver_domain_data },
+  { "exim_gid",            vtype_gid,         &exim_gid },
+  { "exim_path",           vtype_stringptr,   &exim_path },
+  { "exim_uid",            vtype_uid,         &exim_uid },
+#ifdef WITH_OLD_DEMIME
+  { "found_extension",     vtype_stringptr,   &found_extension },
+#endif
+  { "headers_added",       vtype_string_func, &fn_hdrs_added },
+  { "home",                vtype_stringptr,   &deliver_home },
+  { "host",                vtype_stringptr,   &deliver_host },
+  { "host_address",        vtype_stringptr,   &deliver_host_address },
+  { "host_data",           vtype_stringptr,   &host_data },
+  { "host_lookup_deferred",vtype_int,         &host_lookup_deferred },
+  { "host_lookup_failed",  vtype_int,         &host_lookup_failed },
+  { "inode",               vtype_ino,         &deliver_inode },
+  { "interface_address",   vtype_stringptr,   &interface_address },
+  { "interface_port",      vtype_int,         &interface_port },
+  { "item",                vtype_stringptr,   &iterate_item },
+  #ifdef LOOKUP_LDAP
+  { "ldap_dn",             vtype_stringptr,   &eldap_dn },
+  #endif
+  { "load_average",        vtype_load_avg,    NULL },
+  { "local_part",          vtype_stringptr,   &deliver_localpart },
+  { "local_part_data",     vtype_stringptr,   &deliver_localpart_data },
+  { "local_part_prefix",   vtype_stringptr,   &deliver_localpart_prefix },
+  { "local_part_suffix",   vtype_stringptr,   &deliver_localpart_suffix },
+  { "local_scan_data",     vtype_stringptr,   &local_scan_data },
+  { "local_user_gid",      vtype_gid,         &local_user_gid },
+  { "local_user_uid",      vtype_uid,         &local_user_uid },
+  { "localhost_number",    vtype_int,         &host_number },
+  { "log_inodes",          vtype_pinodes,     (void *)FALSE },
+  { "log_space",           vtype_pspace,      (void *)FALSE },
+  { "lookup_dnssec_authenticated",vtype_stringptr,&lookup_dnssec_authenticated},
+  { "mailstore_basename",  vtype_stringptr,   &mailstore_basename },
+#ifdef WITH_CONTENT_SCAN
+  { "malware_name",        vtype_stringptr,   &malware_name },
+#endif
+  { "max_received_linelength", vtype_int,     &max_received_linelength },
+  { "message_age",         vtype_int,         &message_age },
+  { "message_body",        vtype_msgbody,     &message_body },
+  { "message_body_end",    vtype_msgbody_end, &message_body_end },
+  { "message_body_size",   vtype_int,         &message_body_size },
+  { "message_exim_id",     vtype_stringptr,   &message_id },
+  { "message_headers",     vtype_msgheaders,  NULL },
+  { "message_headers_raw", vtype_msgheaders_raw, NULL },
+  { "message_id",          vtype_stringptr,   &message_id },
+  { "message_linecount",   vtype_int,         &message_linecount },
+  { "message_size",        vtype_int,         &message_size },
+#ifdef WITH_CONTENT_SCAN
+  { "mime_anomaly_level",  vtype_int,         &mime_anomaly_level },
+  { "mime_anomaly_text",   vtype_stringptr,   &mime_anomaly_text },
+  { "mime_boundary",       vtype_stringptr,   &mime_boundary },
+  { "mime_charset",        vtype_stringptr,   &mime_charset },
+  { "mime_content_description", vtype_stringptr, &mime_content_description },
+  { "mime_content_disposition", vtype_stringptr, &mime_content_disposition },
+  { "mime_content_id",     vtype_stringptr,   &mime_content_id },
+  { "mime_content_size",   vtype_int,         &mime_content_size },
+  { "mime_content_transfer_encoding",vtype_stringptr, &mime_content_transfer_encoding },
+  { "mime_content_type",   vtype_stringptr,   &mime_content_type },
+  { "mime_decoded_filename", vtype_stringptr, &mime_decoded_filename },
+  { "mime_filename",       vtype_stringptr,   &mime_filename },
+  { "mime_is_coverletter", vtype_int,         &mime_is_coverletter },
+  { "mime_is_multipart",   vtype_int,         &mime_is_multipart },
+  { "mime_is_rfc822",      vtype_int,         &mime_is_rfc822 },
+  { "mime_part_count",     vtype_int,         &mime_part_count },
+#endif
+  { "n0",                  vtype_filter_int,  &filter_n[0] },
+  { "n1",                  vtype_filter_int,  &filter_n[1] },
+  { "n2",                  vtype_filter_int,  &filter_n[2] },
+  { "n3",                  vtype_filter_int,  &filter_n[3] },
+  { "n4",                  vtype_filter_int,  &filter_n[4] },
+  { "n5",                  vtype_filter_int,  &filter_n[5] },
+  { "n6",                  vtype_filter_int,  &filter_n[6] },
+  { "n7",                  vtype_filter_int,  &filter_n[7] },
+  { "n8",                  vtype_filter_int,  &filter_n[8] },
+  { "n9",                  vtype_filter_int,  &filter_n[9] },
+  { "original_domain",     vtype_stringptr,   &deliver_domain_orig },
+  { "original_local_part", vtype_stringptr,   &deliver_localpart_orig },
+  { "originator_gid",      vtype_gid,         &originator_gid },
+  { "originator_uid",      vtype_uid,         &originator_uid },
+  { "parent_domain",       vtype_stringptr,   &deliver_domain_parent },
+  { "parent_local_part",   vtype_stringptr,   &deliver_localpart_parent },
+  { "pid",                 vtype_pid,         NULL },
+  { "primary_hostname",    vtype_stringptr,   &primary_hostname },
+#ifdef EXPERIMENTAL_PROXY
+  { "proxy_host_address",  vtype_stringptr,   &proxy_host_address },
+  { "proxy_host_port",     vtype_int,         &proxy_host_port },
+  { "proxy_session",       vtype_bool,        &proxy_session },
+  { "proxy_target_address",vtype_stringptr,   &proxy_target_address },
+  { "proxy_target_port",   vtype_int,         &proxy_target_port },
+#endif
+  { "prvscheck_address",   vtype_stringptr,   &prvscheck_address },
+  { "prvscheck_keynum",    vtype_stringptr,   &prvscheck_keynum },
+  { "prvscheck_result",    vtype_stringptr,   &prvscheck_result },
+  { "qualify_domain",      vtype_stringptr,   &qualify_domain_sender },
+  { "qualify_recipient",   vtype_stringptr,   &qualify_domain_recipient },
+  { "rcpt_count",          vtype_int,         &rcpt_count },
+  { "rcpt_defer_count",    vtype_int,         &rcpt_defer_count },
+  { "rcpt_fail_count",     vtype_int,         &rcpt_fail_count },
+  { "received_count",      vtype_int,         &received_count },
+  { "received_for",        vtype_stringptr,   &received_for },
+  { "received_ip_address", vtype_stringptr,   &interface_address },
+  { "received_port",       vtype_int,         &interface_port },
+  { "received_protocol",   vtype_stringptr,   &received_protocol },
+  { "received_time",       vtype_int,         &received_time },
+  { "recipient_data",      vtype_stringptr,   &recipient_data },
+  { "recipient_verify_failure",vtype_stringptr,&recipient_verify_failure },
+  { "recipients",          vtype_string_func, &fn_recipients },
+  { "recipients_count",    vtype_int,         &recipients_count },
+#ifdef WITH_CONTENT_SCAN
+  { "regex_match_string",  vtype_stringptr,   &regex_match_string },
+#endif
+  { "reply_address",       vtype_reply,       NULL },
+  { "return_path",         vtype_stringptr,   &return_path },
+  { "return_size_limit",   vtype_int,         &bounce_return_size_limit },
+  { "router_name",         vtype_stringptr,   &router_name },
+  { "runrc",               vtype_int,         &runrc },
+  { "self_hostname",       vtype_stringptr,   &self_hostname },
+  { "sender_address",      vtype_stringptr,   &sender_address },
+  { "sender_address_data", vtype_stringptr,   &sender_address_data },
+  { "sender_address_domain", vtype_domain,    &sender_address },
+  { "sender_address_local_part", vtype_localpart, &sender_address },
+  { "sender_data",         vtype_stringptr,   &sender_data },
+  { "sender_fullhost",     vtype_stringptr,   &sender_fullhost },
+  { "sender_helo_name",    vtype_stringptr,   &sender_helo_name },
+  { "sender_host_address", vtype_stringptr,   &sender_host_address },
+  { "sender_host_authenticated",vtype_stringptr, &sender_host_authenticated },
+  { "sender_host_dnssec",  vtype_bool,        &sender_host_dnssec },
+  { "sender_host_name",    vtype_host_lookup, NULL },
+  { "sender_host_port",    vtype_int,         &sender_host_port },
+  { "sender_ident",        vtype_stringptr,   &sender_ident },
+  { "sender_rate",         vtype_stringptr,   &sender_rate },
+  { "sender_rate_limit",   vtype_stringptr,   &sender_rate_limit },
+  { "sender_rate_period",  vtype_stringptr,   &sender_rate_period },
+  { "sender_rcvhost",      vtype_stringptr,   &sender_rcvhost },
+  { "sender_verify_failure",vtype_stringptr,  &sender_verify_failure },
+  { "sending_ip_address",  vtype_stringptr,   &sending_ip_address },
+  { "sending_port",        vtype_int,         &sending_port },
+  { "smtp_active_hostname", vtype_stringptr,  &smtp_active_hostname },
+  { "smtp_command",        vtype_stringptr,   &smtp_cmd_buffer },
+  { "smtp_command_argument", vtype_stringptr, &smtp_cmd_argument },
+  { "smtp_count_at_connection_start", vtype_int, &smtp_accept_count },
+  { "smtp_notquit_reason", vtype_stringptr,   &smtp_notquit_reason },
+  { "sn0",                 vtype_filter_int,  &filter_sn[0] },
+  { "sn1",                 vtype_filter_int,  &filter_sn[1] },
+  { "sn2",                 vtype_filter_int,  &filter_sn[2] },
+  { "sn3",                 vtype_filter_int,  &filter_sn[3] },
+  { "sn4",                 vtype_filter_int,  &filter_sn[4] },
+  { "sn5",                 vtype_filter_int,  &filter_sn[5] },
+  { "sn6",                 vtype_filter_int,  &filter_sn[6] },
+  { "sn7",                 vtype_filter_int,  &filter_sn[7] },
+  { "sn8",                 vtype_filter_int,  &filter_sn[8] },
+  { "sn9",                 vtype_filter_int,  &filter_sn[9] },
+#ifdef WITH_CONTENT_SCAN
+  { "spam_bar",            vtype_stringptr,   &spam_bar },
+  { "spam_report",         vtype_stringptr,   &spam_report },
+  { "spam_score",          vtype_stringptr,   &spam_score },
+  { "spam_score_int",      vtype_stringptr,   &spam_score_int },
+#endif
+#ifdef EXPERIMENTAL_SPF
+  { "spf_guess",           vtype_stringptr,   &spf_guess },
+  { "spf_header_comment",  vtype_stringptr,   &spf_header_comment },
+  { "spf_received",        vtype_stringptr,   &spf_received },
+  { "spf_result",          vtype_stringptr,   &spf_result },
+  { "spf_smtp_comment",    vtype_stringptr,   &spf_smtp_comment },
+#endif
+  { "spool_directory",     vtype_stringptr,   &spool_directory },
+  { "spool_inodes",        vtype_pinodes,     (void *)TRUE },
+  { "spool_space",         vtype_pspace,      (void *)TRUE },
+#ifdef EXPERIMENTAL_SRS
+  { "srs_db_address",      vtype_stringptr,   &srs_db_address },
+  { "srs_db_key",          vtype_stringptr,   &srs_db_key },
+  { "srs_orig_recipient",  vtype_stringptr,   &srs_orig_recipient },
+  { "srs_orig_sender",     vtype_stringptr,   &srs_orig_sender },
+  { "srs_recipient",       vtype_stringptr,   &srs_recipient },
+  { "srs_status",          vtype_stringptr,   &srs_status },
+#endif
+  { "thisaddress",         vtype_stringptr,   &filter_thisaddress },
+
+  /* The non-(in,out) variables are now deprecated */
+  { "tls_bits",            vtype_int,         &tls_in.bits },
+  { "tls_certificate_verified", vtype_int,    &tls_in.certificate_verified },
+  { "tls_cipher",          vtype_stringptr,   &tls_in.cipher },
+
+  { "tls_in_bits",         vtype_int,         &tls_in.bits },
+  { "tls_in_certificate_verified", vtype_int, &tls_in.certificate_verified },
+  { "tls_in_cipher",       vtype_stringptr,   &tls_in.cipher },
+  { "tls_in_ocsp",         vtype_int,         &tls_in.ocsp },
+  { "tls_in_ourcert",      vtype_cert,        &tls_in.ourcert },
+  { "tls_in_peercert",     vtype_cert,        &tls_in.peercert },
+  { "tls_in_peerdn",       vtype_stringptr,   &tls_in.peerdn },
+#if defined(SUPPORT_TLS)
+  { "tls_in_sni",          vtype_stringptr,   &tls_in.sni },
+#endif
+  { "tls_out_bits",        vtype_int,         &tls_out.bits },
+  { "tls_out_certificate_verified", vtype_int,&tls_out.certificate_verified },
+  { "tls_out_cipher",      vtype_stringptr,   &tls_out.cipher },
+  { "tls_out_ocsp",        vtype_int,         &tls_out.ocsp },
+  { "tls_out_ourcert",     vtype_cert,        &tls_out.ourcert },
+  { "tls_out_peercert",    vtype_cert,        &tls_out.peercert },
+  { "tls_out_peerdn",      vtype_stringptr,   &tls_out.peerdn },
+#if defined(SUPPORT_TLS)
+  { "tls_out_sni",         vtype_stringptr,   &tls_out.sni },
+#endif
+
+  { "tls_peerdn",          vtype_stringptr,   &tls_in.peerdn },        /* mind the alphabetical order! */
+#if defined(SUPPORT_TLS)
+  { "tls_sni",             vtype_stringptr,   &tls_in.sni },   /* mind the alphabetical order! */
+#endif
+
+  { "tod_bsdinbox",        vtype_todbsdin,    NULL },
+  { "tod_epoch",           vtype_tode,        NULL },
+  { "tod_epoch_l",         vtype_todel,       NULL },
+  { "tod_full",            vtype_todf,        NULL },
+  { "tod_log",             vtype_todl,        NULL },
+  { "tod_logfile",         vtype_todlf,       NULL },
+  { "tod_zone",            vtype_todzone,     NULL },
+  { "tod_zulu",            vtype_todzulu,     NULL },
+#ifdef EXPERIMENTAL_TPDA
+  { "tpda_defer_errno",     vtype_int,         &tpda_defer_errno },
+  { "tpda_defer_errstr",    vtype_stringptr,   &tpda_defer_errstr },
+  { "tpda_delivery_confirmation", vtype_stringptr,   &tpda_delivery_confirmation },
+  { "tpda_delivery_domain", vtype_stringptr,   &tpda_delivery_domain },
+  { "tpda_delivery_fqdn",   vtype_stringptr,   &tpda_delivery_fqdn },
+  { "tpda_delivery_ip",     vtype_stringptr,   &tpda_delivery_ip },
+  { "tpda_delivery_local_part",vtype_stringptr,&tpda_delivery_local_part },
+  { "tpda_delivery_port",   vtype_int,         &tpda_delivery_port },
+#endif
+  { "transport_name",      vtype_stringptr,   &transport_name },
+  { "value",               vtype_stringptr,   &lookup_value },
+  { "version_number",      vtype_stringptr,   &version_string },
+  { "warn_message_delay",  vtype_stringptr,   &warnmsg_delay },
+  { "warn_message_recipient",vtype_stringptr, &warnmsg_recipients },
+  { "warn_message_recipients",vtype_stringptr,&warnmsg_recipients },
+  { "warnmsg_delay",       vtype_stringptr,   &warnmsg_delay },
+  { "warnmsg_recipient",   vtype_stringptr,   &warnmsg_recipients },
+  { "warnmsg_recipients",  vtype_stringptr,   &warnmsg_recipients }
+};
+
+static int var_table_size = sizeof(var_table)/sizeof(var_entry);
+static uschar var_buffer[256];
+static BOOL malformed_header;
+
+/* For textual hashes */
+
+static const char *hashcodes = "abcdefghijklmnopqrtsuvwxyz"
+                               "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+                               "0123456789";
+
+enum { HMAC_MD5, HMAC_SHA1 };
+
+/* For numeric hashes */
+
+static unsigned int prime[] = {
+  2,   3,   5,   7,  11,  13,  17,  19,  23,  29,
+ 31,  37,  41,  43,  47,  53,  59,  61,  67,  71,
+ 73,  79,  83,  89,  97, 101, 103, 107, 109, 113};
+
+/* For printing modes in symbolic form */
+
+static uschar *mtable_normal[] =
+  { US"---", US"--x", US"-w-", US"-wx", US"r--", US"r-x", US"rw-", US"rwx" };
+
+static uschar *mtable_setid[] =
+  { US"--S", US"--s", US"-wS", US"-ws", US"r-S", US"r-s", US"rwS", US"rws" };
+
+static uschar *mtable_sticky[] =
+  { US"--T", US"--t", US"-wT", US"-wt", US"r-T", US"r-t", US"rwT", US"rwt" };
+
+
+
+/*************************************************
+*           Tables for UTF-8 support             *
+*************************************************/
+
+/* Table of the number of extra characters, indexed by the first character
+masked with 0x3f. The highest number for a valid UTF-8 character is in fact
+0x3d. */
+
+static uschar utf8_table1[] = {
+  1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
+  1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
+  2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
+  3,3,3,3,3,3,3,3,4,4,4,4,5,5,5,5 };
+
+/* These are the masks for the data bits in the first byte of a character,
+indexed by the number of additional bytes. */
+
+static int utf8_table2[] = { 0xff, 0x1f, 0x0f, 0x07, 0x03, 0x01};
+
+/* Get the next UTF-8 character, advancing the pointer. */
+
+#define GETUTF8INC(c, ptr) \
+  c = *ptr++; \
+  if ((c & 0xc0) == 0xc0) \
+    { \
+    int a = utf8_table1[c & 0x3f];  /* Number of additional bytes */ \
+    int s = 6*a; \
+    c = (c & utf8_table2[a]) << s; \
+    while (a-- > 0) \
+      { \
+      s -= 6; \
+      c |= (*ptr++ & 0x3f) << s; \
+      } \
+    }
+
+
+/*************************************************
+*           Binary chop search on a table        *
+*************************************************/
+
+/* This is used for matching expansion items and operators.
+
+Arguments:
+  name        the name that is being sought
+  table       the table to search
+  table_size  the number of items in the table
+
+Returns:      the offset in the table, or -1
+*/
+
+static int
+chop_match(uschar *name, uschar **table, int table_size)
+{
+uschar **bot = table;
+uschar **top = table + table_size;
+
+while (top > bot)
+  {
+  uschar **mid = bot + (top - bot)/2;
+  int c = Ustrcmp(name, *mid);
+  if (c == 0) return mid - table;
+  if (c > 0) bot = mid + 1; else top = mid;
+  }
+
+return -1;
+}
+
+
+
+/*************************************************
+*          Check a condition string              *
+*************************************************/
+
+/* This function is called to expand a string, and test the result for a "true"
+or "false" value. Failure of the expansion yields FALSE; logged unless it was a
+forced fail or lookup defer.
+
+We used to release all store used, but this is not not safe due
+to ${dlfunc } and ${acl }.  In any case expand_string_internal()
+is reasonably careful to release what it can.
+
+The actual false-value tests should be replicated for ECOND_BOOL_LAX.
+
+Arguments:
+  condition     the condition string
+  m1            text to be incorporated in panic error
+  m2            ditto
+
+Returns:        TRUE if condition is met, FALSE if not
+*/
+
+BOOL
+expand_check_condition(uschar *condition, uschar *m1, uschar *m2)
+{
+int rc;
+uschar *ss = expand_string(condition);
+if (ss == NULL)
+  {
+  if (!expand_string_forcedfail && !search_find_defer)
+    log_write(0, LOG_MAIN|LOG_PANIC, "failed to expand condition \"%s\" "
+      "for %s %s: %s", condition, m1, m2, expand_string_message);
+  return FALSE;
+  }
+rc = ss[0] != 0 && Ustrcmp(ss, "0") != 0 && strcmpic(ss, US"no") != 0 &&
+  strcmpic(ss, US"false") != 0;
+return rc;
+}
+
+
+
+
+/*************************************************
+*        Pseudo-random number generation         *
+*************************************************/
+
+/* Pseudo-random number generation.  The result is not "expected" to be
+cryptographically strong but not so weak that someone will shoot themselves
+in the foot using it as a nonce in some email header scheme or whatever
+weirdness they'll twist this into.  The result should ideally handle fork().
+
+However, if we're stuck unable to provide this, then we'll fall back to
+appallingly bad randomness.
+
+If SUPPORT_TLS is defined then this will not be used except as an emergency
+fallback.
+
+Arguments:
+  max       range maximum
+Returns     a random number in range [0, max-1]
+*/
+
+#ifdef SUPPORT_TLS
+# define vaguely_random_number vaguely_random_number_fallback
+#endif
+int
+vaguely_random_number(int max)
+{
+#ifdef SUPPORT_TLS
+# undef vaguely_random_number
+#endif
+  static pid_t pid = 0;
+  pid_t p2;
+#if defined(HAVE_SRANDOM) && !defined(HAVE_SRANDOMDEV)
+  struct timeval tv;
+#endif
+
+  p2 = getpid();
+  if (p2 != pid)
+    {
+    if (pid != 0)
+      {
+
+#ifdef HAVE_ARC4RANDOM
+      /* cryptographically strong randomness, common on *BSD platforms, not
+      so much elsewhere.  Alas. */
+      arc4random_stir();
+#elif defined(HAVE_SRANDOM) || defined(HAVE_SRANDOMDEV)
+#ifdef HAVE_SRANDOMDEV
+      /* uses random(4) for seeding */
+      srandomdev();
+#else
+      gettimeofday(&tv, NULL);
+      srandom(tv.tv_sec | tv.tv_usec | getpid());
+#endif
+#else
+      /* Poor randomness and no seeding here */
+#endif
+
+      }
+    pid = p2;
+    }
+
+#ifdef HAVE_ARC4RANDOM
+  return arc4random() % max;
+#elif defined(HAVE_SRANDOM) || defined(HAVE_SRANDOMDEV)
+  return random() % max;
+#else
+  /* This one returns a 16-bit number, definitely not crypto-strong */
+  return random_number(max);
+#endif
+}
+
+
+
+
+/*************************************************
+*             Pick out a name from a string      *
+*************************************************/
+
+/* If the name is too long, it is silently truncated.
+
+Arguments:
+  name      points to a buffer into which to put the name
+  max       is the length of the buffer
+  s         points to the first alphabetic character of the name
+  extras    chars other than alphanumerics to permit
+
+Returns:    pointer to the first character after the name
+
+Note: The test for *s != 0 in the while loop is necessary because
+Ustrchr() yields non-NULL if the character is zero (which is not something
+I expected). */
+
+static uschar *
+read_name(uschar *name, int max, uschar *s, uschar *extras)
+{
+int ptr = 0;
+while (*s != 0 && (isalnum(*s) || Ustrchr(extras, *s) != NULL))
+  {
+  if (ptr < max-1) name[ptr++] = *s;
+  s++;
+  }
+name[ptr] = 0;
+return s;
+}
+
+
+
+/*************************************************
+*     Pick out the rest of a header name         *
+*************************************************/
+
+/* A variable name starting $header_ (or just $h_ for those who like
+abbreviations) might not be the complete header name because headers can
+contain any printing characters in their names, except ':'. This function is
+called to read the rest of the name, chop h[eader]_ off the front, and put ':'
+on the end, if the name was terminated by white space.
+
+Arguments:
+  name      points to a buffer in which the name read so far exists
+  max       is the length of the buffer
+  s         points to the first character after the name so far, i.e. the
+            first non-alphameric character after $header_xxxxx
+
+Returns:    a pointer to the first character after the header name
+*/
+
+static uschar *
+read_header_name(uschar *name, int max, uschar *s)
+{
+int prelen = Ustrchr(name, '_') - name + 1;
+int ptr = Ustrlen(name) - prelen;
+if (ptr > 0) memmove(name, name+prelen, ptr);
+while (mac_isgraph(*s) && *s != ':')
+  {
+  if (ptr < max-1) name[ptr++] = *s;
+  s++;
+  }
+if (*s == ':') s++;
+name[ptr++] = ':';
+name[ptr] = 0;
+return s;
+}
+
+
+
+/*************************************************
+*           Pick out a number from a string      *
+*************************************************/
+
+/* Arguments:
+  n     points to an integer into which to put the number
+  s     points to the first digit of the number
+
+Returns:  a pointer to the character after the last digit
+*/
+
+static uschar *
+read_number(int *n, uschar *s)
+{
+*n = 0;
+while (isdigit(*s)) *n = *n * 10 + (*s++ - '0');
+return s;
+}
+
+
+
+/*************************************************
+*        Extract keyed subfield from a string    *
+*************************************************/
+
+/* The yield is in dynamic store; NULL means that the key was not found.
+
+Arguments:
+  key       points to the name of the key
+  s         points to the string from which to extract the subfield
+
+Returns:    NULL if the subfield was not found, or
+            a pointer to the subfield's data
+*/
+
+static uschar *
+expand_getkeyed(uschar *key, uschar *s)
+{
+int length = Ustrlen(key);
+while (isspace(*s)) s++;
+
+/* Loop to search for the key */
+
+while (*s != 0)
+  {
+  int dkeylength;
+  uschar *data;
+  uschar *dkey = s;
+
+  while (*s != 0 && *s != '=' && !isspace(*s)) s++;
+  dkeylength = s - dkey;
+  while (isspace(*s)) s++;
+  if (*s == '=') while (isspace((*(++s))));
+
+  data = string_dequote(&s);
+  if (length == dkeylength && strncmpic(key, dkey, length) == 0)
+    return data;
+
+  while (isspace(*s)) s++;
+  }
+
+return NULL;
+}
+
+
+
+static var_entry *
+find_var_ent(uschar * name)
+{
+int first = 0;
+int last = var_table_size;
+
+while (last > first)
+  {
+  int middle = (first + last)/2;
+  int c = Ustrcmp(name, var_table[middle].name);
+
+  if (c > 0) { first = middle + 1; continue; }
+  if (c < 0) { last = middle; continue; }
+  return &var_table[middle];
+  }
+return NULL;
+}
+
+/*************************************************
+*   Extract numbered subfield from string        *
+*************************************************/
+
+/* Extracts a numbered field from a string that is divided by tokens - for
+example a line from /etc/passwd is divided by colon characters.  First field is
+numbered one.  Negative arguments count from the right. Zero returns the whole
+string. Returns NULL if there are insufficient tokens in the string
+
+***WARNING***
+Modifies final argument - this is a dynamically generated string, so that's OK.
+
+Arguments:
+  field       number of field to be extracted,
+                first field = 1, whole string = 0, last field = -1
+  separators  characters that are used to break string into tokens
+  s           points to the string from which to extract the subfield
+
+Returns:      NULL if the field was not found,
+              a pointer to the field's data inside s (modified to add 0)
+*/
+
+static uschar *
+expand_gettokened (int field, uschar *separators, uschar *s)
+{
+int sep = 1;
+int count;
+uschar *ss = s;
+uschar *fieldtext = NULL;
+
+if (field == 0) return s;
+
+/* Break the line up into fields in place; for field > 0 we stop when we have
+done the number of fields we want. For field < 0 we continue till the end of
+the string, counting the number of fields. */
+
+count = (field > 0)? field : INT_MAX;
+
+while (count-- > 0)
+  {
+  size_t len;
+
+  /* Previous field was the last one in the string. For a positive field
+  number, this means there are not enough fields. For a negative field number,
+  check that there are enough, and scan back to find the one that is wanted. */
+
+  if (sep == 0)
+    {
+    if (field > 0 || (-field) > (INT_MAX - count - 1)) return NULL;
+    if ((-field) == (INT_MAX - count - 1)) return s;
+    while (field++ < 0)
+      {
+      ss--;
+      while (ss[-1] != 0) ss--;
+      }
+    fieldtext = ss;
+    break;
+    }
+
+  /* Previous field was not last in the string; save its start and put a
+  zero at its end. */
+
+  fieldtext = ss;
+  len = Ustrcspn(ss, separators);
+  sep = ss[len];
+  ss[len] = 0;
+  ss += len + 1;
+  }
+
+return fieldtext;
+}
+
+
+static uschar *
+expand_getlistele(int field, uschar * list)
+{
+uschar * tlist= list;
+int sep= 0;
+uschar dummy;
+
+if(field<0)
+{
+  for(field++; string_nextinlist(&tlist, &sep, &dummy, 1); ) field++;
+  sep= 0;
+}
+if(field==0) return NULL;
+while(--field>0 && (string_nextinlist(&list, &sep, &dummy, 1))) ;
+return string_nextinlist(&list, &sep, NULL, 0);
+}
+
+
+/* Certificate fields, by name.  Worry about by-OID later */
+/* Names are chosen to not have common prefixes */
+
+#ifdef SUPPORT_TLS
+typedef struct
+{
+uschar * name;
+int      namelen;
+uschar * (*getfn)(void * cert, uschar * mod);
+} certfield;
+static certfield certfields[] =
+{                      /* linear search; no special order */
+  { US"version",        7,  &tls_cert_version },
+  { US"serial_number",  13, &tls_cert_serial_number },
+  { US"subject",        7,  &tls_cert_subject },
+  { US"notbefore",      9,  &tls_cert_not_before },
+  { US"notafter",       8,  &tls_cert_not_after },
+  { US"issuer",                 6,  &tls_cert_issuer },
+  { US"signature",      9,  &tls_cert_signature },
+  { US"sig_algorithm",  13, &tls_cert_signature_algorithm },
+  { US"subj_altname",    12, &tls_cert_subject_altname },
+  { US"ocsp_uri",       8,  &tls_cert_ocsp_uri },
+  { US"crl_uri",        7,  &tls_cert_crl_uri },
+};
+
+static uschar *
+expand_getcertele(uschar * field, uschar * certvar)
+{
+var_entry * vp;
+certfield * cp;
+
+if (!(vp = find_var_ent(certvar)))
+  {
+  expand_string_message = 
+    string_sprintf("no variable named \"%s\"", certvar);
+  return NULL;          /* Unknown variable name */
+  }
+/* NB this stops us passing certs around in variable.  Might
+want to do that in future */
+if (vp->type != vtype_cert)
+  {
+  expand_string_message = 
+    string_sprintf("\"%s\" is not a certificate", certvar);
+  return NULL;          /* Unknown variable name */
+  }
+if (!*(void **)vp->value)
+  return NULL;
+
+if (*field >= '0' && *field <= '9')
+  return tls_cert_ext_by_oid(*(void **)vp->value, field, 0);
+
+for(cp = certfields;
+    cp < certfields + nelements(certfields);
+    cp++)
+  if (Ustrncmp(cp->name, field, cp->namelen) == 0)
+    {
+    uschar * modifier = *(field += cp->namelen) == ','
+      ? ++field : NULL;
+    return (*cp->getfn)( *(void **)vp->value, modifier );
+    }
+
+expand_string_message = 
+  string_sprintf("bad field selector \"%s\" for certextract", field);
+return NULL;
+}
+#endif /*SUPPORT_TLS*/
+
+/*************************************************
+*        Extract a substring from a string       *
+*************************************************/
+
+/* Perform the ${substr or ${length expansion operations.
+
+Arguments:
+  subject     the input string
+  value1      the offset from the start of the input string to the start of
+                the output string; if negative, count from the right.
+  value2      the length of the output string, or negative (-1) for unset
+                if value1 is positive, unset means "all after"
+                if value1 is negative, unset means "all before"
+  len         set to the length of the returned string
+
+Returns:      pointer to the output string, or NULL if there is an error
+*/
+
+static uschar *
+extract_substr(uschar *subject, int value1, int value2, int *len)
+{
+int sublen = Ustrlen(subject);
+
+if (value1 < 0)    /* count from right */
+  {
+  value1 += sublen;
+
+  /* If the position is before the start, skip to the start, and adjust the
+  length. If the length ends up negative, the substring is null because nothing
+  can precede. This falls out naturally when the length is unset, meaning "all
+  to the left". */
+
+  if (value1 < 0)
+    {
+    value2 += value1;
+    if (value2 < 0) value2 = 0;
+    value1 = 0;
+    }
+
+  /* Otherwise an unset length => characters before value1 */
+
+  else if (value2 < 0)
+    {
+    value2 = value1;
+    value1 = 0;
+    }
+  }
+
+/* For a non-negative offset, if the starting position is past the end of the
+string, the result will be the null string. Otherwise, an unset length means
+"rest"; just set it to the maximum - it will be cut down below if necessary. */
+
+else
+  {
+  if (value1 > sublen)
+    {
+    value1 = sublen;
+    value2 = 0;
+    }
+  else if (value2 < 0) value2 = sublen;
+  }
+
+/* Cut the length down to the maximum possible for the offset value, and get
+the required characters. */
+
+if (value1 + value2 > sublen) value2 = sublen - value1;
+*len = value2;
+return subject + value1;
+}
+
+
+
+
+/*************************************************
+*            Old-style hash of a string          *
+*************************************************/
+
+/* Perform the ${hash expansion operation.
+
+Arguments:
+  subject     the input string (an expanded substring)
+  value1      the length of the output string; if greater or equal to the
+                length of the input string, the input string is returned
+  value2      the number of hash characters to use, or 26 if negative
+  len         set to the length of the returned string
+
+Returns:      pointer to the output string, or NULL if there is an error
+*/
+
+static uschar *
+compute_hash(uschar *subject, int value1, int value2, int *len)
+{
+int sublen = Ustrlen(subject);
+
+if (value2 < 0) value2 = 26;
+else if (value2 > Ustrlen(hashcodes))
+  {
+  expand_string_message =
+    string_sprintf("hash count \"%d\" too big", value2);
+  return NULL;
+  }
+
+/* Calculate the hash text. We know it is shorter than the original string, so
+can safely place it in subject[] (we know that subject is always itself an
+expanded substring). */
+
+if (value1 < sublen)
+  {
+  int c;
+  int i = 0;
+  int j = value1;
+  while ((c = (subject[j])) != 0)
+    {
+    int shift = (c + j++) & 7;
+    subject[i] ^= (c << shift) | (c >> (8-shift));
+    if (++i >= value1) i = 0;
+    }
+  for (i = 0; i < value1; i++)
+    subject[i] = hashcodes[(subject[i]) % value2];
+  }
+else value1 = sublen;
+
+*len = value1;
+return subject;
+}
+
+
+
+
+/*************************************************
+*             Numeric hash of a string           *
+*************************************************/
+
+/* Perform the ${nhash expansion operation. The first characters of the
+string are treated as most important, and get the highest prime numbers.
+
+Arguments:
+  subject     the input string
+  value1      the maximum value of the first part of the result
+  value2      the maximum value of the second part of the result,
+                or negative to produce only a one-part result
+  len         set to the length of the returned string
+
+Returns:  pointer to the output string, or NULL if there is an error.
+*/
+
+static uschar *
+compute_nhash (uschar *subject, int value1, int value2, int *len)
+{
+uschar *s = subject;
+int i = 0;
+unsigned long int total = 0; /* no overflow */
+
+while (*s != 0)
+  {
+  if (i == 0) i = sizeof(prime)/sizeof(int) - 1;
+  total += prime[i--] * (unsigned int)(*s++);
+  }
+
+/* If value2 is unset, just compute one number */
+
+if (value2 < 0)
+  {
+  s = string_sprintf("%d", total % value1);
+  }
+
+/* Otherwise do a div/mod hash */
+
+else
+  {
+  total = total % (value1 * value2);
+  s = string_sprintf("%d/%d", total/value2, total % value2);
+  }
+
+*len = Ustrlen(s);
+return s;
+}
+
+
+
+
+
+/*************************************************
+*     Find the value of a header or headers      *
+*************************************************/
+
+/* Multiple instances of the same header get concatenated, and this function
+can also return a concatenation of all the header lines. When concatenating
+specific headers that contain lists of addresses, a comma is inserted between
+them. Otherwise we use a straight concatenation. Because some messages can have
+pathologically large number of lines, there is a limit on the length that is
+returned. Also, to avoid massive store use which would result from using
+string_cat() as it copies and extends strings, we do a preliminary pass to find
+out exactly how much store will be needed. On "normal" messages this will be
+pretty trivial.
+
+Arguments:
+  name          the name of the header, without the leading $header_ or $h_,
+                or NULL if a concatenation of all headers is required
+  exists_only   TRUE if called from a def: test; don't need to build a string;
+                just return a string that is not "" and not "0" if the header
+                exists
+  newsize       return the size of memory block that was obtained; may be NULL
+                if exists_only is TRUE
+  want_raw      TRUE if called for $rh_ or $rheader_ variables; no processing,
+                other than concatenating, will be done on the header. Also used
+                for $message_headers_raw.
+  charset       name of charset to translate MIME words to; used only if
+                want_raw is false; if NULL, no translation is done (this is
+                used for $bh_ and $bheader_)
+
+Returns:        NULL if the header does not exist, else a pointer to a new
+                store block
+*/
+
+static uschar *
+find_header(uschar *name, BOOL exists_only, int *newsize, BOOL want_raw,
+  uschar *charset)
+{
+BOOL found = name == NULL;
+int comma = 0;
+int len = found? 0 : Ustrlen(name);
+int i;
+uschar *yield = NULL;
+uschar *ptr = NULL;
+
+/* Loop for two passes - saves code repetition */
+
+for (i = 0; i < 2; i++)
+  {
+  int size = 0;
+  header_line *h;
+
+  for (h = header_list; size < header_insert_maxlen && h != NULL; h = h->next)
+    {
+    if (h->type != htype_old && h->text != NULL)  /* NULL => Received: placeholder */
+      {
+      if (name == NULL || (len <= h->slen && strncmpic(name, h->text, len) == 0))
+        {
+        int ilen;
+        uschar *t;
+
+        if (exists_only) return US"1";      /* don't need actual string */
+        found = TRUE;
+        t = h->text + len;                  /* text to insert */
+        if (!want_raw)                      /* unless wanted raw, */
+          while (isspace(*t)) t++;          /* remove leading white space */
+        ilen = h->slen - (t - h->text);     /* length to insert */
+
+        /* Unless wanted raw, remove trailing whitespace, including the
+        newline. */
+
+        if (!want_raw)
+          while (ilen > 0 && isspace(t[ilen-1])) ilen--;
+
+        /* Set comma = 1 if handling a single header and it's one of those
+        that contains an address list, except when asked for raw headers. Only
+        need to do this once. */
+
+        if (!want_raw && name != NULL && comma == 0 &&
+            Ustrchr("BCFRST", h->type) != NULL)
+          comma = 1;
+
+        /* First pass - compute total store needed; second pass - compute
+        total store used, including this header. */
+
+        size += ilen + comma + 1;  /* +1 for the newline */
+
+        /* Second pass - concatentate the data, up to a maximum. Note that
+        the loop stops when size hits the limit. */
+
+        if (i != 0)
+          {
+          if (size > header_insert_maxlen)
+            {
+            ilen -= size - header_insert_maxlen - 1;
+            comma = 0;
+            }
+          Ustrncpy(ptr, t, ilen);
+          ptr += ilen;
+
+          /* For a non-raw header, put in the comma if needed, then add
+          back the newline we removed above, provided there was some text in
+          the header. */
+
+          if (!want_raw && ilen > 0)
+            {
+            if (comma != 0) *ptr++ = ',';
+            *ptr++ = '\n';
+            }
+          }
+        }
+      }
+    }
+
+  /* At end of first pass, return NULL if no header found. Then truncate size
+  if necessary, and get the buffer to hold the data, returning the buffer size.
+  */
+
+  if (i == 0)
+    {
+    if (!found) return NULL;
+    if (size > header_insert_maxlen) size = header_insert_maxlen;
+    *newsize = size + 1;
+    ptr = yield = store_get(*newsize);
+    }
+  }
+
+/* That's all we do for raw header expansion. */
+
+if (want_raw)
+  {
+  *ptr = 0;
+  }
+
+/* Otherwise, remove a final newline and a redundant added comma. Then we do
+RFC 2047 decoding, translating the charset if requested. The rfc2047_decode2()
+function can return an error with decoded data if the charset translation
+fails. If decoding fails, it returns NULL. */
+
+else
+  {
+  uschar *decoded, *error;
+  if (ptr > yield && ptr[-1] == '\n') ptr--;
+  if (ptr > yield && comma != 0 && ptr[-1] == ',') ptr--;
+  *ptr = 0;
+  decoded = rfc2047_decode2(yield, check_rfc2047_length, charset, '?', NULL,
+    newsize, &error);
+  if (error != NULL)
+    {
+    DEBUG(D_any) debug_printf("*** error in RFC 2047 decoding: %s\n"
+      "    input was: %s\n", error, yield);
+    }
+  if (decoded != NULL) yield = decoded;
+  }
+
+return yield;
+}
+
+
+
+
+/*************************************************
+*               Return list of recipients        *
+*************************************************/
+/* A recipients list is available only during system message filtering,
+during ACL processing after DATA, and while expanding pipe commands
+generated from a system filter, but not elsewhere. */
+
+static uschar *
+fn_recipients(void)
+{
+if (!enable_dollar_recipients) return NULL; else
+  {
+  int size = 128;
+  int ptr = 0;
+  int i;
+  uschar * s = store_get(size);
+  for (i = 0; i < recipients_count; i++)
+    {
+    if (i != 0) s = string_cat(s, &size, &ptr, US", ", 2);
+    s = string_cat(s, &size, &ptr, recipients_list[i].address,
+      Ustrlen(recipients_list[i].address));
+    }
+  s[ptr] = 0;     /* string_cat() leaves room */
+  return s;
+  }
+}
+
+
+/*************************************************
+*               Find value of a variable         *
+*************************************************/
+
+/* The table of variables is kept in alphabetic order, so we can search it
+using a binary chop. The "choplen" variable is nothing to do with the binary
+chop.
+
+Arguments:
+  name          the name of the variable being sought
+  exists_only   TRUE if this is a def: test; passed on to find_header()
+  skipping      TRUE => skip any processing evaluation; this is not the same as
+                  exists_only because def: may test for values that are first
+                  evaluated here
+  newsize       pointer to an int which is initially zero; if the answer is in
+                a new memory buffer, *newsize is set to its size
+
+Returns:        NULL if the variable does not exist, or
+                a pointer to the variable's contents, or
+                something non-NULL if exists_only is TRUE
+*/
+
+static uschar *
+find_variable(uschar *name, BOOL exists_only, BOOL skipping, int *newsize)
+{
+var_entry * vp;
+uschar *s, *domain;
+uschar **ss;
+void * val;
+
+/* Handle ACL variables, whose names are of the form acl_cxxx or acl_mxxx.
+Originally, xxx had to be a number in the range 0-9 (later 0-19), but from
+release 4.64 onwards arbitrary names are permitted, as long as the first 5
+characters are acl_c or acl_m and the sixth is either a digit or an underscore
+(this gave backwards compatibility at the changeover). There may be built-in
+variables whose names start acl_ but they should never start in this way. This
+slightly messy specification is a consequence of the history, needless to say.
+
+If an ACL variable does not exist, treat it as empty, unless strict_acl_vars is
+set, in which case give an error. */
+
+if ((Ustrncmp(name, "acl_c", 5) == 0 || Ustrncmp(name, "acl_m", 5) == 0) &&
+     !isalpha(name[5]))
+  {
+  tree_node *node =
+    tree_search((name[4] == 'c')? acl_var_c : acl_var_m, name + 4);
+  return (node == NULL)? (strict_acl_vars? NULL : US"") : node->data.ptr;
+  }
+
+/* Handle $auth<n> variables. */
+
+if (Ustrncmp(name, "auth", 4) == 0)
+  {
+  uschar *endptr;
+  int n = Ustrtoul(name + 4, &endptr, 10);
+  if (*endptr == 0 && n != 0 && n <= AUTH_VARS)
+    return (auth_vars[n-1] == NULL)? US"" : auth_vars[n-1];
+  }
+
+/* For all other variables, search the table */
+
+if (!(vp = find_var_ent(name)))
+  return NULL;          /* Unknown variable name */
+
+/* Found an existing variable. If in skipping state, the value isn't needed,
+and we want to avoid processing (such as looking up the host name). */
+
+if (skipping)
+  return US"";
+
+val = vp->value;
+switch (vp->type)
+  {
+  case vtype_filter_int:
+  if (!filter_running) return NULL;
+  /* Fall through */
+  /* VVVVVVVVVVVV */
+  case vtype_int:
+  sprintf(CS var_buffer, "%d", *(int *)(val)); /* Integer */
+  return var_buffer;
+
+  case vtype_ino:
+  sprintf(CS var_buffer, "%ld", (long int)(*(ino_t *)(val))); /* Inode */
+  return var_buffer;
+
+  case vtype_gid:
+  sprintf(CS var_buffer, "%ld", (long int)(*(gid_t *)(val))); /* gid */
+  return var_buffer;
+
+  case vtype_uid:
+  sprintf(CS var_buffer, "%ld", (long int)(*(uid_t *)(val))); /* uid */
+  return var_buffer;
+
+  case vtype_bool:
+  sprintf(CS var_buffer, "%s", *(BOOL *)(val) ? "yes" : "no"); /* bool */
+  return var_buffer;
+
+  case vtype_stringptr:                      /* Pointer to string */
+  s = *((uschar **)(val));
+  return (s == NULL)? US"" : s;
+
+  case vtype_pid:
+  sprintf(CS var_buffer, "%d", (int)getpid()); /* pid */
+  return var_buffer;
+
+  case vtype_load_avg:
+  sprintf(CS var_buffer, "%d", OS_GETLOADAVG()); /* load_average */
+  return var_buffer;
+
+  case vtype_host_lookup:                    /* Lookup if not done so */
+  if (sender_host_name == NULL && sender_host_address != NULL &&
+      !host_lookup_failed && host_name_lookup() == OK)
+    host_build_sender_fullhost();
+  return (sender_host_name == NULL)? US"" : sender_host_name;
+
+  case vtype_localpart:                      /* Get local part from address */
+  s = *((uschar **)(val));
+  if (s == NULL) return US"";
+  domain = Ustrrchr(s, '@');
+  if (domain == NULL) return s;
+  if (domain - s > sizeof(var_buffer) - 1)
+    log_write(0, LOG_MAIN|LOG_PANIC_DIE, "local part longer than " SIZE_T_FMT
+       " in string expansion", sizeof(var_buffer));
+  Ustrncpy(var_buffer, s, domain - s);
+  var_buffer[domain - s] = 0;
+  return var_buffer;
+
+  case vtype_domain:                         /* Get domain from address */
+  s = *((uschar **)(val));
+  if (s == NULL) return US"";
+  domain = Ustrrchr(s, '@');
+  return (domain == NULL)? US"" : domain + 1;
+
+  case vtype_msgheaders:
+  return find_header(NULL, exists_only, newsize, FALSE, NULL);
+
+  case vtype_msgheaders_raw:
+  return find_header(NULL, exists_only, newsize, TRUE, NULL);
+
+  case vtype_msgbody:                        /* Pointer to msgbody string */
+  case vtype_msgbody_end:                    /* Ditto, the end of the msg */
+  ss = (uschar **)(val);
+  if (*ss == NULL && deliver_datafile >= 0)  /* Read body when needed */
+    {
+    uschar *body;
+    off_t start_offset = SPOOL_DATA_START_OFFSET;
+    int len = message_body_visible;
+    if (len > message_size) len = message_size;
+    *ss = body = store_malloc(len+1);
+    body[0] = 0;
+    if (vp->type == vtype_msgbody_end)
+      {
+      struct stat statbuf;
+      if (fstat(deliver_datafile, &statbuf) == 0)
+       {
+       start_offset = statbuf.st_size - len;
+       if (start_offset < SPOOL_DATA_START_OFFSET)
+         start_offset = SPOOL_DATA_START_OFFSET;
+       }
+      }
+    lseek(deliver_datafile, start_offset, SEEK_SET);
+    len = read(deliver_datafile, body, len);
+    if (len > 0)
+      {
+      body[len] = 0;
+      if (message_body_newlines)   /* Separate loops for efficiency */
+       {
+       while (len > 0)
+         { if (body[--len] == 0) body[len] = ' '; }
+       }
+      else
+       {
+       while (len > 0)
+         { if (body[--len] == '\n' || body[len] == 0) body[len] = ' '; }
+       }
+      }
+    }
+  return (*ss == NULL)? US"" : *ss;
+
+  case vtype_todbsdin:                       /* BSD inbox time of day */
+  return tod_stamp(tod_bsdin);
+
+  case vtype_tode:                           /* Unix epoch time of day */
+  return tod_stamp(tod_epoch);
+
+  case vtype_todel:                          /* Unix epoch/usec time of day */
+  return tod_stamp(tod_epoch_l);
+
+  case vtype_todf:                           /* Full time of day */
+  return tod_stamp(tod_full);
+
+  case vtype_todl:                           /* Log format time of day */
+  return tod_stamp(tod_log_bare);            /* (without timezone) */
+
+  case vtype_todzone:                        /* Time zone offset only */
+  return tod_stamp(tod_zone);
+
+  case vtype_todzulu:                        /* Zulu time */
+  return tod_stamp(tod_zulu);
+
+  case vtype_todlf:                          /* Log file datestamp tod */
+  return tod_stamp(tod_log_datestamp_daily);
+
+  case vtype_reply:                          /* Get reply address */
+  s = find_header(US"reply-to:", exists_only, newsize, TRUE,
+    headers_charset);
+  if (s != NULL) while (isspace(*s)) s++;
+  if (s == NULL || *s == 0)
+    {
+    *newsize = 0;                            /* For the *s==0 case */
+    s = find_header(US"from:", exists_only, newsize, TRUE, headers_charset);
+    }
+  if (s != NULL)
+    {
+    uschar *t;
+    while (isspace(*s)) s++;
+    for (t = s; *t != 0; t++) if (*t == '\n') *t = ' ';
+    while (t > s && isspace(t[-1])) t--;
+    *t = 0;
+    }
+  return (s == NULL)? US"" : s;
+
+  case vtype_string_func:
+    {
+    uschar * (*fn)() = val;
+    return fn();
+    }
+
+  case vtype_pspace:
+    {
+    int inodes;
+    sprintf(CS var_buffer, "%d",
+      receive_statvfs(val == (void *)TRUE, &inodes));
+    }
+  return var_buffer;
+
+  case vtype_pinodes:
+    {
+    int inodes;
+    (void) receive_statvfs(val == (void *)TRUE, &inodes);
+    sprintf(CS var_buffer, "%d", inodes);
+    }
+  return var_buffer;
+
+  case vtype_cert:
+  return *(void **)val ? US"<cert>" : US"";
+
+  #ifndef DISABLE_DKIM
+  case vtype_dkim:
+  return dkim_exim_expand_query((int)(long)val);
+  #endif
+
+  }
+
+return NULL;  /* Unknown variable. Silences static checkers. */
+}
+
+
+
+
+void
+modify_variable(uschar *name, void * value)
+{
+var_entry * vp;
+if ((vp = find_var_ent(name))) vp->value = value;
+return;          /* Unknown variable name, fail silently */
+}
+
+
+
+
+
+/*************************************************
+*           Read and expand substrings           *
+*************************************************/
+
+/* This function is called to read and expand argument substrings for various
+expansion items. Some have a minimum requirement that is less than the maximum;
+in these cases, the first non-present one is set to NULL.
+
+Arguments:
+  sub        points to vector of pointers to set
+  n          maximum number of substrings
+  m          minimum required
+  sptr       points to current string pointer
+  skipping   the skipping flag
+  check_end  if TRUE, check for final '}'
+  name       name of item, for error message
+  resetok    if not NULL, pointer to flag - write FALSE if unsafe to reset
+            the store.
+
+Returns:     0 OK; string pointer updated
+             1 curly bracketing error (too few arguments)
+             2 too many arguments (only if check_end is set); message set
+             3 other error (expansion failure)
+*/
+
+static int
+read_subs(uschar **sub, int n, int m, uschar **sptr, BOOL skipping,
+  BOOL check_end, uschar *name, BOOL *resetok)
+{
+int i;
+uschar *s = *sptr;
+
+while (isspace(*s)) s++;
+for (i = 0; i < n; i++)
+  {
+  if (*s != '{')
+    {
+    if (i < m) return 1;
+    sub[i] = NULL;
+    break;
+    }
+  sub[i] = expand_string_internal(s+1, TRUE, &s, skipping, TRUE, resetok);
+  if (sub[i] == NULL) return 3;
+  if (*s++ != '}') return 1;
+  while (isspace(*s)) s++;
+  }
+if (check_end && *s++ != '}')
+  {
+  if (s[-1] == '{')
+    {
+    expand_string_message = string_sprintf("Too many arguments for \"%s\" "
+      "(max is %d)", name, n);
+    return 2;
+    }
+  return 1;
+  }
+
+*sptr = s;
+return 0;
+}
+
+
+
+
+/*************************************************
+*     Elaborate message for bad variable         *
+*************************************************/
+
+/* For the "unknown variable" message, take a look at the variable's name, and
+give additional information about possible ACL variables. The extra information
+is added on to expand_string_message.
+
+Argument:   the name of the variable
+Returns:    nothing
+*/
+
+static void
+check_variable_error_message(uschar *name)
+{
+if (Ustrncmp(name, "acl_", 4) == 0)
+  expand_string_message = string_sprintf("%s (%s)", expand_string_message,
+    (name[4] == 'c' || name[4] == 'm')?
+      (isalpha(name[5])?
+        US"6th character of a user-defined ACL variable must be a digit or underscore" :
+        US"strict_acl_vars is set"    /* Syntax is OK, it has to be this */
+      ) :
+      US"user-defined ACL variables must start acl_c or acl_m");
+}
+
+
+
+/*
+Load args from sub array to globals, and call acl_check().
+Sub array will be corrupted on return.
+
+Returns:       OK         access is granted by an ACCEPT verb
+               DISCARD    access is granted by a DISCARD verb
+              FAIL       access is denied
+              FAIL_DROP  access is denied; drop the connection
+              DEFER      can't tell at the moment
+              ERROR      disaster
+*/
+static int
+eval_acl(uschar ** sub, int nsub, uschar ** user_msgp)
+{
+int i;
+uschar *tmp;
+int sav_narg = acl_narg;
+int ret;
+extern int acl_where;
+
+if(--nsub > sizeof(acl_arg)/sizeof(*acl_arg)) nsub = sizeof(acl_arg)/sizeof(*acl_arg);
+for (i = 0; i < nsub && sub[i+1]; i++)
+  {
+  tmp = acl_arg[i];
+  acl_arg[i] = sub[i+1];       /* place callers args in the globals */
+  sub[i+1] = tmp;              /* stash the old args using our caller's storage */
+  }
+acl_narg = i;
+while (i < nsub)
+  {
+  sub[i+1] = acl_arg[i];
+  acl_arg[i++] = NULL;
+  }
+
+DEBUG(D_expand)
+  debug_printf("expanding: acl: %s  arg: %s%s\n",
+    sub[0],
+    acl_narg>0 ? acl_arg[0] : US"<none>",
+    acl_narg>1 ? " +more"   : "");
+
+ret = acl_eval(acl_where, sub[0], user_msgp, &tmp);
+
+for (i = 0; i < nsub; i++)
+  acl_arg[i] = sub[i+1];       /* restore old args */
+acl_narg = sav_narg;
+
+return ret;
+}
+
+
+
+
+/*************************************************
+*        Read and evaluate a condition           *
+*************************************************/
+
+/*
+Arguments:
+  s        points to the start of the condition text
+  resetok  points to a BOOL which is written false if it is unsafe to
+          free memory. Certain condition types (acl) may have side-effect
+          allocation which must be preserved.
+  yield    points to a BOOL to hold the result of the condition test;
+           if NULL, we are just reading through a condition that is
+           part of an "or" combination to check syntax, or in a state
+           where the answer isn't required
+
+Returns:   a pointer to the first character after the condition, or
+           NULL after an error
+*/
+
+static uschar *
+eval_condition(uschar *s, BOOL *resetok, BOOL *yield)
+{
+BOOL testfor = TRUE;
+BOOL tempcond, combined_cond;
+BOOL *subcondptr;
+BOOL sub2_honour_dollar = TRUE;
+int i, rc, cond_type, roffset;
+int_eximarith_t num[2];
+struct stat statbuf;
+uschar name[256];
+uschar *sub[10];
+
+const pcre *re;
+const uschar *rerror;
+
+for (;;)
+  {
+  while (isspace(*s)) s++;
+  if (*s == '!') { testfor = !testfor; s++; } else break;
+  }
+
+/* Numeric comparisons are symbolic */
+
+if (*s == '=' || *s == '>' || *s == '<')
+  {
+  int p = 0;
+  name[p++] = *s++;
+  if (*s == '=')
+    {
+    name[p++] = '=';
+    s++;
+    }
+  name[p] = 0;
+  }
+
+/* All other conditions are named */
+
+else s = read_name(name, 256, s, US"_");
+
+/* If we haven't read a name, it means some non-alpha character is first. */
+
+if (name[0] == 0)
+  {
+  expand_string_message = string_sprintf("condition name expected, "
+    "but found \"%.16s\"", s);
+  return NULL;
+  }
+
+/* Find which condition we are dealing with, and switch on it */
+
+cond_type = chop_match(name, cond_table, sizeof(cond_table)/sizeof(uschar *));
+switch(cond_type)
+  {
+  /* def: tests for a non-empty variable, or for the existence of a header. If
+  yield == NULL we are in a skipping state, and don't care about the answer. */
+
+  case ECOND_DEF:
+  if (*s != ':')
+    {
+    expand_string_message = US"\":\" expected after \"def\"";
+    return NULL;
+    }
+
+  s = read_name(name, 256, s+1, US"_");
+
+  /* Test for a header's existence. If the name contains a closing brace
+  character, this may be a user error where the terminating colon has been
+  omitted. Set a flag to adjust a subsequent error message in this case. */
+
+  if (Ustrncmp(name, "h_", 2) == 0 ||
+      Ustrncmp(name, "rh_", 3) == 0 ||
+      Ustrncmp(name, "bh_", 3) == 0 ||
+      Ustrncmp(name, "header_", 7) == 0 ||
+      Ustrncmp(name, "rheader_", 8) == 0 ||
+      Ustrncmp(name, "bheader_", 8) == 0)
+    {
+    s = read_header_name(name, 256, s);
+    /* {-for-text-editors */
+    if (Ustrchr(name, '}') != NULL) malformed_header = TRUE;
+    if (yield != NULL) *yield =
+      (find_header(name, TRUE, NULL, FALSE, NULL) != NULL) == testfor;
+    }
+
+  /* Test for a variable's having a non-empty value. A non-existent variable
+  causes an expansion failure. */
+
+  else
+    {
+    uschar *value = find_variable(name, TRUE, yield == NULL, NULL);
+    if (value == NULL)
+      {
+      expand_string_message = (name[0] == 0)?
+        string_sprintf("variable name omitted after \"def:\"") :
+        string_sprintf("unknown variable \"%s\" after \"def:\"", name);
+      check_variable_error_message(name);
+      return NULL;
+      }
+    if (yield != NULL) *yield = (value[0] != 0) == testfor;
+    }
+
+  return s;
+
+
+  /* first_delivery tests for first delivery attempt */
+
+  case ECOND_FIRST_DELIVERY:
+  if (yield != NULL) *yield = deliver_firsttime == testfor;
+  return s;
+
+
+  /* queue_running tests for any process started by a queue runner */
+
+  case ECOND_QUEUE_RUNNING:
+  if (yield != NULL) *yield = (queue_run_pid != (pid_t)0) == testfor;
+  return s;
+
+
+  /* exists:  tests for file existence
+       isip:  tests for any IP address
+      isip4:  tests for an IPv4 address
+      isip6:  tests for an IPv6 address
+        pam:  does PAM authentication
+     radius:  does RADIUS authentication
+   ldapauth:  does LDAP authentication
+    pwcheck:  does Cyrus SASL pwcheck authentication
+  */
+
+  case ECOND_EXISTS:
+  case ECOND_ISIP:
+  case ECOND_ISIP4:
+  case ECOND_ISIP6:
+  case ECOND_PAM:
+  case ECOND_RADIUS:
+  case ECOND_LDAPAUTH:
+  case ECOND_PWCHECK:
+
+  while (isspace(*s)) s++;
+  if (*s != '{') goto COND_FAILED_CURLY_START;         /* }-for-text-editors */
+
+  sub[0] = expand_string_internal(s+1, TRUE, &s, yield == NULL, TRUE, resetok);
+  if (sub[0] == NULL) return NULL;
+  /* {-for-text-editors */
+  if (*s++ != '}') goto COND_FAILED_CURLY_END;
+
+  if (yield == NULL) return s;   /* No need to run the test if skipping */
+
+  switch(cond_type)
+    {
+    case ECOND_EXISTS:
+    if ((expand_forbid & RDO_EXISTS) != 0)
+      {
+      expand_string_message = US"File existence tests are not permitted";
+      return NULL;
+      }
+    *yield = (Ustat(sub[0], &statbuf) == 0) == testfor;
+    break;
+
+    case ECOND_ISIP:
+    case ECOND_ISIP4:
+    case ECOND_ISIP6:
+    rc = string_is_ip_address(sub[0], NULL);
+    *yield = ((cond_type == ECOND_ISIP)? (rc != 0) :
+             (cond_type == ECOND_ISIP4)? (rc == 4) : (rc == 6)) == testfor;
+    break;
+
+    /* Various authentication tests - all optionally compiled */
+
+    case ECOND_PAM:
+    #ifdef SUPPORT_PAM
+    rc = auth_call_pam(sub[0], &expand_string_message);
+    goto END_AUTH;
+    #else
+    goto COND_FAILED_NOT_COMPILED;
+    #endif  /* SUPPORT_PAM */
+
+    case ECOND_RADIUS:
+    #ifdef RADIUS_CONFIG_FILE
+    rc = auth_call_radius(sub[0], &expand_string_message);
+    goto END_AUTH;
+    #else
+    goto COND_FAILED_NOT_COMPILED;
+    #endif  /* RADIUS_CONFIG_FILE */
+
+    case ECOND_LDAPAUTH:
+    #ifdef LOOKUP_LDAP
+      {
+      /* Just to keep the interface the same */
+      BOOL do_cache;
+      int old_pool = store_pool;
+      store_pool = POOL_SEARCH;
+      rc = eldapauth_find((void *)(-1), NULL, sub[0], Ustrlen(sub[0]), NULL,
+        &expand_string_message, &do_cache);
+      store_pool = old_pool;
+      }
+    goto END_AUTH;
+    #else
+    goto COND_FAILED_NOT_COMPILED;
+    #endif  /* LOOKUP_LDAP */
+
+    case ECOND_PWCHECK:
+    #ifdef CYRUS_PWCHECK_SOCKET
+    rc = auth_call_pwcheck(sub[0], &expand_string_message);
+    goto END_AUTH;
+    #else
+    goto COND_FAILED_NOT_COMPILED;
+    #endif  /* CYRUS_PWCHECK_SOCKET */
+
+    #if defined(SUPPORT_PAM) || defined(RADIUS_CONFIG_FILE) || \
+        defined(LOOKUP_LDAP) || defined(CYRUS_PWCHECK_SOCKET)
+    END_AUTH:
+    if (rc == ERROR || rc == DEFER) return NULL;
+    *yield = (rc == OK) == testfor;
+    #endif
+    }
+  return s;
+
+
+  /* call ACL (in a conditional context).  Accept true, deny false.
+  Defer is a forced-fail.  Anything set by message= goes to $value.
+  Up to ten parameters are used; we use the braces round the name+args
+  like the saslauthd condition does, to permit a variable number of args.
+  See also the expansion-item version EITEM_ACL and the traditional
+  acl modifier ACLC_ACL.
+  Since the ACL may allocate new global variables, tell our caller to not
+  reclaim memory.
+  */
+
+  case ECOND_ACL:
+    /* ${if acl {{name}{arg1}{arg2}...}  {yes}{no}} */
+    {
+    uschar *user_msg;
+    BOOL cond = FALSE;
+    int size = 0;
+    int ptr = 0;
+
+    while (isspace(*s)) s++;
+    if (*s++ != '{') goto COND_FAILED_CURLY_START;     /*}*/
+
+    switch(read_subs(sub, sizeof(sub)/sizeof(*sub), 1,
+      &s, yield == NULL, TRUE, US"acl", resetok))
+      {
+      case 1: expand_string_message = US"too few arguments or bracketing "
+        "error for acl";
+      case 2:
+      case 3: return NULL;
+      }
+
+    *resetok = FALSE;
+    if (yield != NULL) switch(eval_acl(sub, sizeof(sub)/sizeof(*sub), &user_msg))
+       {
+       case OK:
+         cond = TRUE;
+       case FAIL:
+          lookup_value = NULL;
+         if (user_msg)
+           {
+            lookup_value = string_cat(NULL, &size, &ptr, user_msg, Ustrlen(user_msg));
+            lookup_value[ptr] = '\0';
+           }
+         *yield = cond == testfor;
+         break;
+
+       case DEFER:
+          expand_string_forcedfail = TRUE;
+       default:
+          expand_string_message = string_sprintf("error from acl \"%s\"", sub[0]);
+         return NULL;
+       }
+    return s;
+    }
+
+
+  /* saslauthd: does Cyrus saslauthd authentication. Four parameters are used:
+
+     ${if saslauthd {{username}{password}{service}{realm}}  {yes}{no}}
+
+  However, the last two are optional. That is why the whole set is enclosed
+  in their own set of braces. */
+
+  case ECOND_SASLAUTHD:
+  #ifndef CYRUS_SASLAUTHD_SOCKET
+  goto COND_FAILED_NOT_COMPILED;
+  #else
+  while (isspace(*s)) s++;
+  if (*s++ != '{') goto COND_FAILED_CURLY_START;       /* }-for-text-editors */
+  switch(read_subs(sub, 4, 2, &s, yield == NULL, TRUE, US"saslauthd", resetok))
+    {
+    case 1: expand_string_message = US"too few arguments or bracketing "
+      "error for saslauthd";
+    case 2:
+    case 3: return NULL;
+    }
+  if (sub[2] == NULL) sub[3] = NULL;  /* realm if no service */
+  if (yield != NULL)
+    {
+    int rc;
+    rc = auth_call_saslauthd(sub[0], sub[1], sub[2], sub[3],
+      &expand_string_message);
+    if (rc == ERROR || rc == DEFER) return NULL;
+    *yield = (rc == OK) == testfor;
+    }
+  return s;
+  #endif /* CYRUS_SASLAUTHD_SOCKET */
+
+
+  /* symbolic operators for numeric and string comparison, and a number of
+  other operators, all requiring two arguments.
+
+  crypteq:           encrypts plaintext and compares against an encrypted text,
+                       using crypt(), crypt16(), MD5 or SHA-1
+  inlist/inlisti:    checks if first argument is in the list of the second
+  match:             does a regular expression match and sets up the numerical
+                       variables if it succeeds
+  match_address:     matches in an address list
+  match_domain:      matches in a domain list
+  match_ip:          matches a host list that is restricted to IP addresses
+  match_local_part:  matches in a local part list
+  */
+
+  case ECOND_MATCH_ADDRESS:
+  case ECOND_MATCH_DOMAIN:
+  case ECOND_MATCH_IP:
+  case ECOND_MATCH_LOCAL_PART:
+#ifndef EXPAND_LISTMATCH_RHS
+    sub2_honour_dollar = FALSE;
+#endif
+    /* FALLTHROUGH */
+
+  case ECOND_CRYPTEQ:
+  case ECOND_INLIST:
+  case ECOND_INLISTI:
+  case ECOND_MATCH:
+
+  case ECOND_NUM_L:     /* Numerical comparisons */
+  case ECOND_NUM_LE:
+  case ECOND_NUM_E:
+  case ECOND_NUM_EE:
+  case ECOND_NUM_G:
+  case ECOND_NUM_GE:
+
+  case ECOND_STR_LT:    /* String comparisons */
+  case ECOND_STR_LTI:
+  case ECOND_STR_LE:
+  case ECOND_STR_LEI:
+  case ECOND_STR_EQ:
+  case ECOND_STR_EQI:
+  case ECOND_STR_GT:
+  case ECOND_STR_GTI:
+  case ECOND_STR_GE:
+  case ECOND_STR_GEI:
+
+  for (i = 0; i < 2; i++)
+    {
+    /* Sometimes, we don't expand substrings; too many insecure configurations
+    created using match_address{}{} and friends, where the second param
+    includes information from untrustworthy sources. */
+    BOOL honour_dollar = TRUE;
+    if ((i > 0) && !sub2_honour_dollar)
+      honour_dollar = FALSE;
+
+    while (isspace(*s)) s++;
+    if (*s != '{')
+      {
+      if (i == 0) goto COND_FAILED_CURLY_START;
+      expand_string_message = string_sprintf("missing 2nd string in {} "
+        "after \"%s\"", name);
+      return NULL;
+      }
+    sub[i] = expand_string_internal(s+1, TRUE, &s, yield == NULL,
+        honour_dollar, resetok);
+    if (sub[i] == NULL) return NULL;
+    if (*s++ != '}') goto COND_FAILED_CURLY_END;
+
+    /* Convert to numerical if required; we know that the names of all the
+    conditions that compare numbers do not start with a letter. This just saves
+    checking for them individually. */
+
+    if (!isalpha(name[0]) && yield != NULL)
+      {
+      if (sub[i][0] == 0)
+        {
+        num[i] = 0;
+        DEBUG(D_expand)
+          debug_printf("empty string cast to zero for numerical comparison\n");
+        }
+      else
+        {
+        num[i] = expanded_string_integer(sub[i], FALSE);
+        if (expand_string_message != NULL) return NULL;
+        }
+      }
+    }
+
+  /* Result not required */
+
+  if (yield == NULL) return s;
+
+  /* Do an appropriate comparison */
+
+  switch(cond_type)
+    {
+    case ECOND_NUM_E:
+    case ECOND_NUM_EE:
+    tempcond = (num[0] == num[1]);
+    break;
+
+    case ECOND_NUM_G:
+    tempcond = (num[0] > num[1]);
+    break;
+
+    case ECOND_NUM_GE:
+    tempcond = (num[0] >= num[1]);
+    break;
+
+    case ECOND_NUM_L:
+    tempcond = (num[0] < num[1]);
+    break;
+
+    case ECOND_NUM_LE:
+    tempcond = (num[0] <= num[1]);
+    break;
+
+    case ECOND_STR_LT:
+    tempcond = (Ustrcmp(sub[0], sub[1]) < 0);
+    break;
+
+    case ECOND_STR_LTI:
+    tempcond = (strcmpic(sub[0], sub[1]) < 0);
+    break;
+
+    case ECOND_STR_LE:
+    tempcond = (Ustrcmp(sub[0], sub[1]) <= 0);
+    break;
+
+    case ECOND_STR_LEI:
+    tempcond = (strcmpic(sub[0], sub[1]) <= 0);
+    break;
+
+    case ECOND_STR_EQ:
+    tempcond = (Ustrcmp(sub[0], sub[1]) == 0);
+    break;
+
+    case ECOND_STR_EQI:
+    tempcond = (strcmpic(sub[0], sub[1]) == 0);
+    break;
+
+    case ECOND_STR_GT:
+    tempcond = (Ustrcmp(sub[0], sub[1]) > 0);
+    break;
+
+    case ECOND_STR_GTI:
+    tempcond = (strcmpic(sub[0], sub[1]) > 0);
+    break;
+
+    case ECOND_STR_GE:
+    tempcond = (Ustrcmp(sub[0], sub[1]) >= 0);
+    break;
+
+    case ECOND_STR_GEI:
+    tempcond = (strcmpic(sub[0], sub[1]) >= 0);
+    break;
+
+    case ECOND_MATCH:   /* Regular expression match */
+    re = pcre_compile(CS sub[1], PCRE_COPT, (const char **)&rerror, &roffset,
+      NULL);
+    if (re == NULL)
+      {
+      expand_string_message = string_sprintf("regular expression error in "
+        "\"%s\": %s at offset %d", sub[1], rerror, roffset);
+      return NULL;
+      }
+    tempcond = regex_match_and_setup(re, sub[0], 0, -1);
+    break;
+
+    case ECOND_MATCH_ADDRESS:  /* Match in an address list */
+    rc = match_address_list(sub[0], TRUE, FALSE, &(sub[1]), NULL, -1, 0, NULL);
+    goto MATCHED_SOMETHING;
+
+    case ECOND_MATCH_DOMAIN:   /* Match in a domain list */
+    rc = match_isinlist(sub[0], &(sub[1]), 0, &domainlist_anchor, NULL,
+      MCL_DOMAIN + MCL_NOEXPAND, TRUE, NULL);
+    goto MATCHED_SOMETHING;
+
+    case ECOND_MATCH_IP:       /* Match IP address in a host list */
+    if (sub[0][0] != 0 && string_is_ip_address(sub[0], NULL) == 0)
+      {
+      expand_string_message = string_sprintf("\"%s\" is not an IP address",
+        sub[0]);
+      return NULL;
+      }
+    else
+      {
+      unsigned int *nullcache = NULL;
+      check_host_block cb;
+
+      cb.host_name = US"";
+      cb.host_address = sub[0];
+
+      /* If the host address starts off ::ffff: it is an IPv6 address in
+      IPv4-compatible mode. Find the IPv4 part for checking against IPv4
+      addresses. */
+
+      cb.host_ipv4 = (Ustrncmp(cb.host_address, "::ffff:", 7) == 0)?
+        cb.host_address + 7 : cb.host_address;
+
+      rc = match_check_list(
+             &sub[1],                   /* the list */
+             0,                         /* separator character */
+             &hostlist_anchor,          /* anchor pointer */
+             &nullcache,                /* cache pointer */
+             check_host,                /* function for testing */
+             &cb,                       /* argument for function */
+             MCL_HOST,                  /* type of check */
+             sub[0],                    /* text for debugging */
+             NULL);                     /* where to pass back data */
+      }
+    goto MATCHED_SOMETHING;
+
+    case ECOND_MATCH_LOCAL_PART:
+    rc = match_isinlist(sub[0], &(sub[1]), 0, &localpartlist_anchor, NULL,
+      MCL_LOCALPART + MCL_NOEXPAND, TRUE, NULL);
+    /* Fall through */
+    /* VVVVVVVVVVVV */
+    MATCHED_SOMETHING:
+    switch(rc)
+      {
+      case OK:
+      tempcond = TRUE;
+      break;
+
+      case FAIL:
+      tempcond = FALSE;
+      break;
+
+      case DEFER:
+      expand_string_message = string_sprintf("unable to complete match "
+        "against \"%s\": %s", sub[1], search_error_message);
+      return NULL;
+      }
+
+    break;
+
+    /* Various "encrypted" comparisons. If the second string starts with
+    "{" then an encryption type is given. Default to crypt() or crypt16()
+    (build-time choice). */
+    /* }-for-text-editors */
+
+    case ECOND_CRYPTEQ:
+    #ifndef SUPPORT_CRYPTEQ
+    goto COND_FAILED_NOT_COMPILED;
+    #else
+    if (strncmpic(sub[1], US"{md5}", 5) == 0)
+      {
+      int sublen = Ustrlen(sub[1]+5);
+      md5 base;
+      uschar digest[16];
+
+      md5_start(&base);
+      md5_end(&base, (uschar *)sub[0], Ustrlen(sub[0]), digest);
+
+      /* If the length that we are comparing against is 24, the MD5 digest
+      is expressed as a base64 string. This is the way LDAP does it. However,
+      some other software uses a straightforward hex representation. We assume
+      this if the length is 32. Other lengths fail. */
+
+      if (sublen == 24)
+        {
+        uschar *coded = auth_b64encode((uschar *)digest, 16);
+        DEBUG(D_auth) debug_printf("crypteq: using MD5+B64 hashing\n"
+          "  subject=%s\n  crypted=%s\n", coded, sub[1]+5);
+        tempcond = (Ustrcmp(coded, sub[1]+5) == 0);
+        }
+      else if (sublen == 32)
+        {
+        int i;
+        uschar coded[36];
+        for (i = 0; i < 16; i++) sprintf(CS (coded+2*i), "%02X", digest[i]);
+        coded[32] = 0;
+        DEBUG(D_auth) debug_printf("crypteq: using MD5+hex hashing\n"
+          "  subject=%s\n  crypted=%s\n", coded, sub[1]+5);
+        tempcond = (strcmpic(coded, sub[1]+5) == 0);
+        }
+      else
+        {
+        DEBUG(D_auth) debug_printf("crypteq: length for MD5 not 24 or 32: "
+          "fail\n  crypted=%s\n", sub[1]+5);
+        tempcond = FALSE;
+        }
+      }
+
+    else if (strncmpic(sub[1], US"{sha1}", 6) == 0)
+      {
+      int sublen = Ustrlen(sub[1]+6);
+      sha1 base;
+      uschar digest[20];
+
+      sha1_start(&base);
+      sha1_end(&base, (uschar *)sub[0], Ustrlen(sub[0]), digest);
+
+      /* If the length that we are comparing against is 28, assume the SHA1
+      digest is expressed as a base64 string. If the length is 40, assume a
+      straightforward hex representation. Other lengths fail. */
+
+      if (sublen == 28)
+        {
+        uschar *coded = auth_b64encode((uschar *)digest, 20);
+        DEBUG(D_auth) debug_printf("crypteq: using SHA1+B64 hashing\n"
+          "  subject=%s\n  crypted=%s\n", coded, sub[1]+6);
+        tempcond = (Ustrcmp(coded, sub[1]+6) == 0);
+        }
+      else if (sublen == 40)
+        {
+        int i;
+        uschar coded[44];
+        for (i = 0; i < 20; i++) sprintf(CS (coded+2*i), "%02X", digest[i]);
+        coded[40] = 0;
+        DEBUG(D_auth) debug_printf("crypteq: using SHA1+hex hashing\n"
+          "  subject=%s\n  crypted=%s\n", coded, sub[1]+6);
+        tempcond = (strcmpic(coded, sub[1]+6) == 0);
+        }
+      else
+        {
+        DEBUG(D_auth) debug_printf("crypteq: length for SHA-1 not 28 or 40: "
+          "fail\n  crypted=%s\n", sub[1]+6);
+       tempcond = FALSE;
+        }
+      }
+
+    else   /* {crypt} or {crypt16} and non-{ at start */
+           /* }-for-text-editors */
+      {
+      int which = 0;
+      uschar *coded;
+
+      if (strncmpic(sub[1], US"{crypt}", 7) == 0)
+        {
+        sub[1] += 7;
+        which = 1;
+        }
+      else if (strncmpic(sub[1], US"{crypt16}", 9) == 0)
+        {
+        sub[1] += 9;
+        which = 2;
+        }
+      else if (sub[1][0] == '{')               /* }-for-text-editors */
+        {
+        expand_string_message = string_sprintf("unknown encryption mechanism "
+          "in \"%s\"", sub[1]);
+        return NULL;
+        }
+
+      switch(which)
+        {
+        case 0:  coded = US DEFAULT_CRYPT(CS sub[0], CS sub[1]); break;
+        case 1:  coded = US crypt(CS sub[0], CS sub[1]); break;
+        default: coded = US crypt16(CS sub[0], CS sub[1]); break;
+        }
+
+      #define STR(s) # s
+      #define XSTR(s) STR(s)
+      DEBUG(D_auth) debug_printf("crypteq: using %s()\n"
+        "  subject=%s\n  crypted=%s\n",
+        (which == 0)? XSTR(DEFAULT_CRYPT) : (which == 1)? "crypt" : "crypt16",
+        coded, sub[1]);
+      #undef STR
+      #undef XSTR
+
+      /* If the encrypted string contains fewer than two characters (for the
+      salt), force failure. Otherwise we get false positives: with an empty
+      string the yield of crypt() is an empty string! */
+
+      tempcond = (Ustrlen(sub[1]) < 2)? FALSE :
+        (Ustrcmp(coded, sub[1]) == 0);
+      }
+    break;
+    #endif  /* SUPPORT_CRYPTEQ */
+
+    case ECOND_INLIST:
+    case ECOND_INLISTI:
+      {
+      int sep = 0;
+      uschar *save_iterate_item = iterate_item;
+      int (*compare)(const uschar *, const uschar *);
+
+      tempcond = FALSE;
+      if (cond_type == ECOND_INLISTI)
+        compare = strcmpic;
+      else
+        compare = (int (*)(const uschar *, const uschar *)) strcmp;
+
+      while ((iterate_item = string_nextinlist(&sub[1], &sep, NULL, 0)) != NULL)
+        if (compare(sub[0], iterate_item) == 0)
+          {
+          tempcond = TRUE;
+          break;
+          }
+      iterate_item = save_iterate_item;
+      }
+
+    }   /* Switch for comparison conditions */
+
+  *yield = tempcond == testfor;
+  return s;    /* End of comparison conditions */
+
+
+  /* and/or: computes logical and/or of several conditions */
+
+  case ECOND_AND:
+  case ECOND_OR:
+  subcondptr = (yield == NULL)? NULL : &tempcond;
+  combined_cond = (cond_type == ECOND_AND);
+
+  while (isspace(*s)) s++;
+  if (*s++ != '{') goto COND_FAILED_CURLY_START;       /* }-for-text-editors */
+
+  for (;;)
+    {
+    while (isspace(*s)) s++;
+    /* {-for-text-editors */
+    if (*s == '}') break;
+    if (*s != '{')                                     /* }-for-text-editors */
+      {
+      expand_string_message = string_sprintf("each subcondition "
+        "inside an \"%s{...}\" condition must be in its own {}", name);
+      return NULL;
+      }
+
+    if (!(s = eval_condition(s+1, resetok, subcondptr)))
+      {
+      expand_string_message = string_sprintf("%s inside \"%s{...}\" condition",
+        expand_string_message, name);
+      return NULL;
+      }
+    while (isspace(*s)) s++;
+
+    /* {-for-text-editors */
+    if (*s++ != '}')
+      {
+      /* {-for-text-editors */
+      expand_string_message = string_sprintf("missing } at end of condition "
+        "inside \"%s\" group", name);
+      return NULL;
+      }
+
+    if (yield != NULL)
+      {
+      if (cond_type == ECOND_AND)
+        {
+        combined_cond &= tempcond;
+        if (!combined_cond) subcondptr = NULL;  /* once false, don't */
+        }                                       /* evaluate any more */
+      else
+        {
+        combined_cond |= tempcond;
+        if (combined_cond) subcondptr = NULL;   /* once true, don't */
+        }                                       /* evaluate any more */
+      }
+    }
+
+  if (yield != NULL) *yield = (combined_cond == testfor);
+  return ++s;
+
+
+  /* forall/forany: iterates a condition with different values */
+
+  case ECOND_FORALL:
+  case ECOND_FORANY:
+    {
+    int sep = 0;
+    uschar *save_iterate_item = iterate_item;
+
+    while (isspace(*s)) s++;
+    if (*s++ != '{') goto COND_FAILED_CURLY_START;     /* }-for-text-editors */
+    sub[0] = expand_string_internal(s, TRUE, &s, (yield == NULL), TRUE, resetok);
+    if (sub[0] == NULL) return NULL;
+    /* {-for-text-editors */
+    if (*s++ != '}') goto COND_FAILED_CURLY_END;
+
+    while (isspace(*s)) s++;
+    if (*s++ != '{') goto COND_FAILED_CURLY_START;     /* }-for-text-editors */
+
+    sub[1] = s;
+
+    /* Call eval_condition once, with result discarded (as if scanning a
+    "false" part). This allows us to find the end of the condition, because if
+    the list it empty, we won't actually evaluate the condition for real. */
+
+    if (!(s = eval_condition(sub[1], resetok, NULL)))
+      {
+      expand_string_message = string_sprintf("%s inside \"%s\" condition",
+        expand_string_message, name);
+      return NULL;
+      }
+    while (isspace(*s)) s++;
+
+    /* {-for-text-editors */
+    if (*s++ != '}')
+      {
+      /* {-for-text-editors */
+      expand_string_message = string_sprintf("missing } at end of condition "
+        "inside \"%s\"", name);
+      return NULL;
+      }
+
+    if (yield != NULL) *yield = !testfor;
+    while ((iterate_item = string_nextinlist(&sub[0], &sep, NULL, 0)) != NULL)
+      {
+      DEBUG(D_expand) debug_printf("%s: $item = \"%s\"\n", name, iterate_item);
+      if (!eval_condition(sub[1], resetok, &tempcond))
+        {
+        expand_string_message = string_sprintf("%s inside \"%s\" condition",
+          expand_string_message, name);
+        iterate_item = save_iterate_item;
+        return NULL;
+        }
+      DEBUG(D_expand) debug_printf("%s: condition evaluated to %s\n", name,
+        tempcond? "true":"false");
+
+      if (yield != NULL) *yield = (tempcond == testfor);
+      if (tempcond == (cond_type == ECOND_FORANY)) break;
+      }
+
+    iterate_item = save_iterate_item;
+    return s;
+    }
+
+
+  /* The bool{} expansion condition maps a string to boolean.
+  The values supported should match those supported by the ACL condition
+  (acl.c, ACLC_CONDITION) so that we keep to a minimum the different ideas
+  of true/false.  Note that Router "condition" rules have a different
+  interpretation, where general data can be used and only a few values
+  map to FALSE.
+  Note that readconf.c boolean matching, for boolean configuration options,
+  only matches true/yes/false/no.
+  The bool_lax{} condition matches the Router logic, which is much more
+  liberal. */
+  case ECOND_BOOL:
+  case ECOND_BOOL_LAX:
+    {
+    uschar *sub_arg[1];
+    uschar *t, *t2;
+    uschar *ourname;
+    size_t len;
+    BOOL boolvalue = FALSE;
+    while (isspace(*s)) s++;
+    if (*s != '{') goto COND_FAILED_CURLY_START;       /* }-for-text-editors */
+    ourname = cond_type == ECOND_BOOL_LAX ? US"bool_lax" : US"bool";
+    switch(read_subs(sub_arg, 1, 1, &s, yield == NULL, FALSE, ourname, resetok))
+      {
+      case 1: expand_string_message = string_sprintf(
+                  "too few arguments or bracketing error for %s",
+                  ourname);
+      /*FALLTHROUGH*/
+      case 2:
+      case 3: return NULL;
+      }
+    t = sub_arg[0];
+    while (isspace(*t)) t++;
+    len = Ustrlen(t);
+    if (len)
+      {
+      /* trailing whitespace: seems like a good idea to ignore it too */
+      t2 = t + len - 1;
+      while (isspace(*t2)) t2--;
+      if (t2 != (t + len))
+        {
+        *++t2 = '\0';
+        len = t2 - t;
+        }
+      }
+    DEBUG(D_expand)
+      debug_printf("considering %s: %s\n", ourname, len ? t : US"<empty>");
+    /* logic for the lax case from expand_check_condition(), which also does
+    expands, and the logic is both short and stable enough that there should
+    be no maintenance burden from replicating it. */
+    if (len == 0)
+      boolvalue = FALSE;
+    else if (*t == '-'
+            ? Ustrspn(t+1, "0123456789") == len-1
+            : Ustrspn(t,   "0123456789") == len)
+      {
+      boolvalue = (Uatoi(t) == 0) ? FALSE : TRUE;
+      /* expand_check_condition only does a literal string "0" check */
+      if ((cond_type == ECOND_BOOL_LAX) && (len > 1))
+        boolvalue = TRUE;
+      }
+    else if (strcmpic(t, US"true") == 0 || strcmpic(t, US"yes") == 0)
+      boolvalue = TRUE;
+    else if (strcmpic(t, US"false") == 0 || strcmpic(t, US"no") == 0)
+      boolvalue = FALSE;
+    else if (cond_type == ECOND_BOOL_LAX)
+      boolvalue = TRUE;
+    else
+      {
+      expand_string_message = string_sprintf("unrecognised boolean "
+       "value \"%s\"", t);
+      return NULL;
+      }
+    if (yield != NULL) *yield = (boolvalue == testfor);
+    return s;
+    }
+
+  /* Unknown condition */
+
+  default:
+  expand_string_message = string_sprintf("unknown condition \"%s\"", name);
+  return NULL;
+  }   /* End switch on condition type */
+
+/* Missing braces at start and end of data */
+
+COND_FAILED_CURLY_START:
+expand_string_message = string_sprintf("missing { after \"%s\"", name);
+return NULL;
+
+COND_FAILED_CURLY_END:
+expand_string_message = string_sprintf("missing } at end of \"%s\" condition",
+  name);
+return NULL;
+
+/* A condition requires code that is not compiled */
+
+#if !defined(SUPPORT_PAM) || !defined(RADIUS_CONFIG_FILE) || \
+    !defined(LOOKUP_LDAP) || !defined(CYRUS_PWCHECK_SOCKET) || \
+    !defined(SUPPORT_CRYPTEQ) || !defined(CYRUS_SASLAUTHD_SOCKET)
+COND_FAILED_NOT_COMPILED:
+expand_string_message = string_sprintf("support for \"%s\" not compiled",
+  name);
+return NULL;
+#endif
+}
+
+
+
+
+/*************************************************
+*          Save numerical variables              *
+*************************************************/
+
+/* This function is called from items such as "if" that want to preserve and
+restore the numbered variables.
+
+Arguments:
+  save_expand_string    points to an array of pointers to set
+  save_expand_nlength   points to an array of ints for the lengths
+
+Returns:                the value of expand max to save
+*/
+
+static int
+save_expand_strings(uschar **save_expand_nstring, int *save_expand_nlength)
+{
+int i;
+for (i = 0; i <= expand_nmax; i++)
+  {
+  save_expand_nstring[i] = expand_nstring[i];
+  save_expand_nlength[i] = expand_nlength[i];
+  }
+return expand_nmax;
+}
+
+
+
+/*************************************************
+*           Restore numerical variables          *
+*************************************************/
+
+/* This function restored saved values of numerical strings.
+
+Arguments:
+  save_expand_nmax      the number of strings to restore
+  save_expand_string    points to an array of pointers
+  save_expand_nlength   points to an array of ints
+
+Returns:                nothing
+*/
+
+static void
+restore_expand_strings(int save_expand_nmax, uschar **save_expand_nstring,
+  int *save_expand_nlength)
+{
+int i;
+expand_nmax = save_expand_nmax;
+for (i = 0; i <= expand_nmax; i++)
+  {
+  expand_nstring[i] = save_expand_nstring[i];
+  expand_nlength[i] = save_expand_nlength[i];
+  }
+}
+
+
+
+
+
+/*************************************************
+*            Handle yes/no substrings            *
+*************************************************/
+
+/* This function is used by ${if}, ${lookup} and ${extract} to handle the
+alternative substrings that depend on whether or not the condition was true,
+or the lookup or extraction succeeded. The substrings always have to be
+expanded, to check their syntax, but "skipping" is set when the result is not
+needed - this avoids unnecessary nested lookups.
+
+Arguments:
+  skipping       TRUE if we were skipping when this item was reached
+  yes            TRUE if the first string is to be used, else use the second
+  save_lookup    a value to put back into lookup_value before the 2nd expansion
+  sptr           points to the input string pointer
+  yieldptr       points to the output string pointer
+  sizeptr        points to the output string size
+  ptrptr         points to the output string pointer
+  type           "lookup" or "if" or "extract" or "run", for error message
+  resetok       if not NULL, pointer to flag - write FALSE if unsafe to reset
+               the store.
+
+Returns:         0 OK; lookup_value has been reset to save_lookup
+                 1 expansion failed
+                 2 expansion failed because of bracketing error
+*/
+
+static int
+process_yesno(BOOL skipping, BOOL yes, uschar *save_lookup, uschar **sptr,
+  uschar **yieldptr, int *sizeptr, int *ptrptr, uschar *type, BOOL *resetok)
+{
+int rc = 0;
+uschar *s = *sptr;    /* Local value */
+uschar *sub1, *sub2;
+
+/* If there are no following strings, we substitute the contents of $value for
+lookups and for extractions in the success case. For the ${if item, the string
+"true" is substituted. In the fail case, nothing is substituted for all three
+items. */
+
+while (isspace(*s)) s++;
+if (*s == '}')
+  {
+  if (type[0] == 'i')
+    {
+    if (yes) *yieldptr = string_cat(*yieldptr, sizeptr, ptrptr, US"true", 4);
+    }
+  else
+    {
+    if (yes && lookup_value != NULL)
+      *yieldptr = string_cat(*yieldptr, sizeptr, ptrptr, lookup_value,
+        Ustrlen(lookup_value));
+    lookup_value = save_lookup;
+    }
+  s++;
+  goto RETURN;
+  }
+
+/* The first following string must be braced. */
+
+if (*s++ != '{') goto FAILED_CURLY;
+
+/* Expand the first substring. Forced failures are noticed only if we actually
+want this string. Set skipping in the call in the fail case (this will always
+be the case if we were already skipping). */
+
+sub1 = expand_string_internal(s, TRUE, &s, !yes, TRUE, resetok);
+if (sub1 == NULL && (yes || !expand_string_forcedfail)) goto FAILED;
+expand_string_forcedfail = FALSE;
+if (*s++ != '}') goto FAILED_CURLY;
+
+/* If we want the first string, add it to the output */
+
+if (yes)
+  *yieldptr = string_cat(*yieldptr, sizeptr, ptrptr, sub1, Ustrlen(sub1));
+
+/* If this is called from a lookup or an extract, we want to restore $value to
+what it was at the start of the item, so that it has this value during the
+second string expansion. For the call from "if" or "run" to this function,
+save_lookup is set to lookup_value, so that this statement does nothing. */
+
+lookup_value = save_lookup;
+
+/* There now follows either another substring, or "fail", or nothing. This
+time, forced failures are noticed only if we want the second string. We must
+set skipping in the nested call if we don't want this string, or if we were
+already skipping. */
+
+while (isspace(*s)) s++;
+if (*s == '{')
+  {
+  sub2 = expand_string_internal(s+1, TRUE, &s, yes || skipping, TRUE, resetok);
+  if (sub2 == NULL && (!yes || !expand_string_forcedfail)) goto FAILED;
+  expand_string_forcedfail = FALSE;
+  if (*s++ != '}') goto FAILED_CURLY;
+
+  /* If we want the second string, add it to the output */
+
+  if (!yes)
+    *yieldptr = string_cat(*yieldptr, sizeptr, ptrptr, sub2, Ustrlen(sub2));
+  }
+
+/* If there is no second string, but the word "fail" is present when the use of
+the second string is wanted, set a flag indicating it was a forced failure
+rather than a syntactic error. Swallow the terminating } in case this is nested
+inside another lookup or if or extract. */
+
+else if (*s != '}')
+  {
+  uschar name[256];
+  s = read_name(name, sizeof(name), s, US"_");
+  if (Ustrcmp(name, "fail") == 0)
+    {
+    if (!yes && !skipping)
+      {
+      while (isspace(*s)) s++;
+      if (*s++ != '}') goto FAILED_CURLY;
+      expand_string_message =
+        string_sprintf("\"%s\" failed and \"fail\" requested", type);
+      expand_string_forcedfail = TRUE;
+      goto FAILED;
+      }
+    }
+  else
+    {
+    expand_string_message =
+      string_sprintf("syntax error in \"%s\" item - \"fail\" expected", type);
+    goto FAILED;
+    }
+  }
+
+/* All we have to do now is to check on the final closing brace. */
+
+while (isspace(*s)) s++;
+if (*s++ == '}') goto RETURN;
+
+/* Get here if there is a bracketing failure */
+
+FAILED_CURLY:
+rc++;
+
+/* Get here for other failures */
+
+FAILED:
+rc++;
+
+/* Update the input pointer value before returning */
+
+RETURN:
+*sptr = s;
+return rc;
+}
+
+
+
+
+/*************************************************
+*    Handle MD5 or SHA-1 computation for HMAC    *
+*************************************************/
+
+/* These are some wrapping functions that enable the HMAC code to be a bit
+cleaner. A good compiler will spot the tail recursion.
+
+Arguments:
+  type         HMAC_MD5 or HMAC_SHA1
+  remaining    are as for the cryptographic hash functions
+
+Returns:       nothing
+*/
+
+static void
+chash_start(int type, void *base)
+{
+if (type == HMAC_MD5)
+  md5_start((md5 *)base);
+else
+  sha1_start((sha1 *)base);
+}
+
+static void
+chash_mid(int type, void *base, uschar *string)
+{
+if (type == HMAC_MD5)
+  md5_mid((md5 *)base, string);
+else
+  sha1_mid((sha1 *)base, string);
+}
+
+static void
+chash_end(int type, void *base, uschar *string, int length, uschar *digest)
+{
+if (type == HMAC_MD5)
+  md5_end((md5 *)base, string, length, digest);
+else
+  sha1_end((sha1 *)base, string, length, digest);
+}
+
+
+
+
+
+/********************************************************
+* prvs: Get last three digits of days since Jan 1, 1970 *
+********************************************************/
+
+/* This is needed to implement the "prvs" BATV reverse
+   path signing scheme
+
+Argument: integer "days" offset to add or substract to
+          or from the current number of days.
+
+Returns:  pointer to string containing the last three
+          digits of the number of days since Jan 1, 1970,
+          modified by the offset argument, NULL if there
+          was an error in the conversion.
+
+*/
+
+static uschar *
+prvs_daystamp(int day_offset)
+{
+uschar *days = store_get(32);                /* Need at least 24 for cases */
+(void)string_format(days, 32, TIME_T_FMT,    /* where TIME_T_FMT is %lld */
+  (time(NULL) + day_offset*86400)/86400);
+return (Ustrlen(days) >= 3) ? &days[Ustrlen(days)-3] : US"100";
+}
+
+
+
+/********************************************************
+*   prvs: perform HMAC-SHA1 computation of prvs bits    *
+********************************************************/
+
+/* This is needed to implement the "prvs" BATV reverse
+   path signing scheme
+
+Arguments:
+  address RFC2821 Address to use
+      key The key to use (must be less than 64 characters
+          in size)
+  key_num Single-digit key number to use. Defaults to
+          '0' when NULL.
+
+Returns:  pointer to string containing the first three
+          bytes of the final hash in hex format, NULL if
+          there was an error in the process.
+*/
+
+static uschar *
+prvs_hmac_sha1(uschar *address, uschar *key, uschar *key_num, uschar *daystamp)
+{
+uschar *hash_source, *p;
+int size = 0,offset = 0,i;
+sha1 sha1_base;
+void *use_base = &sha1_base;
+uschar innerhash[20];
+uschar finalhash[20];
+uschar innerkey[64];
+uschar outerkey[64];
+uschar *finalhash_hex = store_get(40);
+
+if (key_num == NULL)
+  key_num = US"0";
+
+if (Ustrlen(key) > 64)
+  return NULL;
+
+hash_source = string_cat(NULL,&size,&offset,key_num,1);
+string_cat(hash_source,&size,&offset,daystamp,3);
+string_cat(hash_source,&size,&offset,address,Ustrlen(address));
+hash_source[offset] = '\0';
+
+DEBUG(D_expand) debug_printf("prvs: hash source is '%s'\n", hash_source);
+
+memset(innerkey, 0x36, 64);
+memset(outerkey, 0x5c, 64);
+
+for (i = 0; i < Ustrlen(key); i++)
+  {
+  innerkey[i] ^= key[i];
+  outerkey[i] ^= key[i];
+  }
+
+chash_start(HMAC_SHA1, use_base);
+chash_mid(HMAC_SHA1, use_base, innerkey);
+chash_end(HMAC_SHA1, use_base, hash_source, offset, innerhash);
+
+chash_start(HMAC_SHA1, use_base);
+chash_mid(HMAC_SHA1, use_base, outerkey);
+chash_end(HMAC_SHA1, use_base, innerhash, 20, finalhash);
+
+p = finalhash_hex;
+for (i = 0; i < 3; i++)
+  {
+  *p++ = hex_digits[(finalhash[i] & 0xf0) >> 4];
+  *p++ = hex_digits[finalhash[i] & 0x0f];
+  }
+*p = '\0';
+
+return finalhash_hex;
+}
+
+
+
+
+/*************************************************
+*        Join a file onto the output string      *
+*************************************************/
+
+/* This is used for readfile and after a run expansion. It joins the contents
+of a file onto the output string, globally replacing newlines with a given
+string (optionally). The file is closed at the end.
+
+Arguments:
+  f            the FILE
+  yield        pointer to the expandable string
+  sizep        pointer to the current size
+  ptrp         pointer to the current position
+  eol          newline replacement string, or NULL
+
+Returns:       new value of string pointer
+*/
+
+static uschar *
+cat_file(FILE *f, uschar *yield, int *sizep, int *ptrp, uschar *eol)
+{
+int eollen;
+uschar buffer[1024];
+
+eollen = (eol == NULL)? 0 : Ustrlen(eol);
+
+while (Ufgets(buffer, sizeof(buffer), f) != NULL)
+  {
+  int len = Ustrlen(buffer);
+  if (eol != NULL && buffer[len-1] == '\n') len--;
+  yield = string_cat(yield, sizep, ptrp, buffer, len);
+  if (buffer[len] != 0)
+    yield = string_cat(yield, sizep, ptrp, eol, eollen);
+  }
+
+if (yield != NULL) yield[*ptrp] = 0;
+
+return yield;
+}
+
+
+
+
+/*************************************************
+*          Evaluate numeric expression           *
+*************************************************/
+
+/* This is a set of mutually recursive functions that evaluate an arithmetic
+expression involving + - * / % & | ^ ~ << >> and parentheses. The only one of
+these functions that is called from elsewhere is eval_expr, whose interface is:
+
+Arguments:
+  sptr        pointer to the pointer to the string - gets updated
+  decimal     TRUE if numbers are to be assumed decimal
+  error       pointer to where to put an error message - must be NULL on input
+  endket      TRUE if ')' must terminate - FALSE for external call
+
+Returns:      on success: the value of the expression, with *error still NULL
+              on failure: an undefined value, with *error = a message
+*/
+
+static int_eximarith_t eval_op_or(uschar **, BOOL, uschar **);
+
+
+static int_eximarith_t
+eval_expr(uschar **sptr, BOOL decimal, uschar **error, BOOL endket)
+{
+uschar *s = *sptr;
+int_eximarith_t x = eval_op_or(&s, decimal, error);
+if (*error == NULL)
+  {
+  if (endket)
+    {
+    if (*s != ')')
+      *error = US"expecting closing parenthesis";
+    else
+      while (isspace(*(++s)));
+    }
+  else if (*s != 0) *error = US"expecting operator";
+  }
+*sptr = s;
+return x;
+}
+
+
+static int_eximarith_t
+eval_number(uschar **sptr, BOOL decimal, uschar **error)
+{
+register int c;
+int_eximarith_t n;
+uschar *s = *sptr;
+while (isspace(*s)) s++;
+c = *s;
+if (isdigit(c))
+  {
+  int count;
+  (void)sscanf(CS s, (decimal? SC_EXIM_DEC "%n" : SC_EXIM_ARITH "%n"), &n, &count);
+  s += count;
+  switch (tolower(*s))
+    {
+    default: break;
+    case 'k': n *= 1024; s++; break;
+    case 'm': n *= 1024*1024; s++; break;
+    case 'g': n *= 1024*1024*1024; s++; break;
+    }
+  while (isspace (*s)) s++;
+  }
+else if (c == '(')
+  {
+  s++;
+  n = eval_expr(&s, decimal, error, 1);
+  }
+else
+  {
+  *error = US"expecting number or opening parenthesis";
+  n = 0;
+  }
+*sptr = s;
+return n;
+}
+
+
+static int_eximarith_t
+eval_op_unary(uschar **sptr, BOOL decimal, uschar **error)
+{
+uschar *s = *sptr;
+int_eximarith_t x;
+while (isspace(*s)) s++;
+if (*s == '+' || *s == '-' || *s == '~')
+  {
+  int op = *s++;
+  x = eval_op_unary(&s, decimal, error);
+  if (op == '-') x = -x;
+    else if (op == '~') x = ~x;
+  }
+else
+  {
+  x = eval_number(&s, decimal, error);
+  }
+*sptr = s;
+return x;
+}
+
+
+static int_eximarith_t
+eval_op_mult(uschar **sptr, BOOL decimal, uschar **error)
+{
+uschar *s = *sptr;
+int_eximarith_t x = eval_op_unary(&s, decimal, error);
+if (*error == NULL)
+  {
+  while (*s == '*' || *s == '/' || *s == '%')
+    {
+    int op = *s++;
+    int_eximarith_t y = eval_op_unary(&s, decimal, error);
+    if (*error != NULL) break;
+    /* SIGFPE both on div/mod by zero and on INT_MIN / -1, which would give
+     * a value of INT_MAX+1. Note that INT_MIN * -1 gives INT_MIN for me, which
+     * is a bug somewhere in [gcc 4.2.1, FreeBSD, amd64].  In fact, -N*-M where
+     * -N*M is INT_MIN will yielf INT_MIN.
+     * Since we don't support floating point, this is somewhat simpler.
+     * Ideally, we'd return an error, but since we overflow for all other
+     * arithmetic, consistency suggests otherwise, but what's the correct value
+     * to use?  There is none.
+     * The C standard guarantees overflow for unsigned arithmetic but signed
+     * overflow invokes undefined behaviour; in practice, this is overflow
+     * except for converting INT_MIN to INT_MAX+1.  We also can't guarantee
+     * that long/longlong larger than int are available, or we could just work
+     * with larger types.  We should consider whether to guarantee 32bit eval
+     * and 64-bit working variables, with errors returned.  For now ...
+     * So, the only SIGFPEs occur with a non-shrinking div/mod, thus -1; we
+     * can just let the other invalid results occur otherwise, as they have
+     * until now.  For this one case, we can coerce.
+     */
+    if (y == -1 && x == EXIM_ARITH_MIN && op != '*')
+      {
+      DEBUG(D_expand)
+        debug_printf("Integer exception dodging: " PR_EXIM_ARITH "%c-1 coerced to " PR_EXIM_ARITH "\n",
+            EXIM_ARITH_MIN, op, EXIM_ARITH_MAX);
+      x = EXIM_ARITH_MAX;
+      continue;
+      }
+    if (op == '*')
+      x *= y;
+    else
+      {
+      if (y == 0)
+        {
+        *error = (op == '/') ? US"divide by zero" : US"modulo by zero";
+        x = 0;
+        break;
+        }
+      if (op == '/')
+        x /= y;
+      else
+        x %= y;
+      }
+    }
+  }
+*sptr = s;
+return x;
+}
+
+
+static int_eximarith_t
+eval_op_sum(uschar **sptr, BOOL decimal, uschar **error)
+{
+uschar *s = *sptr;
+int_eximarith_t x = eval_op_mult(&s, decimal, error);
+if (*error == NULL)
+  {
+  while (*s == '+' || *s == '-')
+    {
+    int op = *s++;
+    int_eximarith_t y = eval_op_mult(&s, decimal, error);
+    if (*error != NULL) break;
+    if (op == '+') x += y; else x -= y;
+    }
+  }
+*sptr = s;
+return x;
+}
+
+
+static int_eximarith_t
+eval_op_shift(uschar **sptr, BOOL decimal, uschar **error)
+{
+uschar *s = *sptr;
+int_eximarith_t x = eval_op_sum(&s, decimal, error);
+if (*error == NULL)
+  {
+  while ((*s == '<' || *s == '>') && s[1] == s[0])
+    {
+    int_eximarith_t y;
+    int op = *s++;
+    s++;
+    y = eval_op_sum(&s, decimal, error);
+    if (*error != NULL) break;
+    if (op == '<') x <<= y; else x >>= y;
+    }
+  }
+*sptr = s;
+return x;
+}
+
+
+static int_eximarith_t
+eval_op_and(uschar **sptr, BOOL decimal, uschar **error)
+{
+uschar *s = *sptr;
+int_eximarith_t x = eval_op_shift(&s, decimal, error);
+if (*error == NULL)
+  {
+  while (*s == '&')
+    {
+    int_eximarith_t y;
+    s++;
+    y = eval_op_shift(&s, decimal, error);
+    if (*error != NULL) break;
+    x &= y;
+    }
+  }
+*sptr = s;
+return x;
+}
+
+
+static int_eximarith_t
+eval_op_xor(uschar **sptr, BOOL decimal, uschar **error)
+{
+uschar *s = *sptr;
+int_eximarith_t x = eval_op_and(&s, decimal, error);
+if (*error == NULL)
+  {
+  while (*s == '^')
+    {
+    int_eximarith_t y;
+    s++;
+    y = eval_op_and(&s, decimal, error);
+    if (*error != NULL) break;
+    x ^= y;
+    }
+  }
+*sptr = s;
+return x;
+}
+
+
+static int_eximarith_t
+eval_op_or(uschar **sptr, BOOL decimal, uschar **error)
+{
+uschar *s = *sptr;
+int_eximarith_t x = eval_op_xor(&s, decimal, error);
+if (*error == NULL)
+  {
+  while (*s == '|')
+    {
+    int_eximarith_t y;
+    s++;
+    y = eval_op_xor(&s, decimal, error);
+    if (*error != NULL) break;
+    x |= y;
+    }
+  }
+*sptr = s;
+return x;
+}
+
+
+
+/*************************************************
+*                 Expand string                  *
+*************************************************/
+
+/* Returns either an unchanged string, or the expanded string in stacking pool
+store. Interpreted sequences are:
+
+   \...                    normal escaping rules
+   $name                   substitutes the variable
+   ${name}                 ditto
+   ${op:string}            operates on the expanded string value
+   ${item{arg1}{arg2}...}  expands the args and then does the business
+                             some literal args are not enclosed in {}
+
+There are now far too many operators and item types to make it worth listing
+them here in detail any more.
+
+We use an internal routine recursively to handle embedded substrings. The
+external function follows. The yield is NULL if the expansion failed, and there
+are two cases: if something collapsed syntactically, or if "fail" was given
+as the action on a lookup failure. These can be distinguised by looking at the
+variable expand_string_forcedfail, which is TRUE in the latter case.
+
+The skipping flag is set true when expanding a substring that isn't actually
+going to be used (after "if" or "lookup") and it prevents lookups from
+happening lower down.
+
+Store usage: At start, a store block of the length of the input plus 64
+is obtained. This is expanded as necessary by string_cat(), which might have to
+get a new block, or might be able to expand the original. At the end of the
+function we can release any store above that portion of the yield block that
+was actually used. In many cases this will be optimal.
+
+However: if the first item in the expansion is a variable name or header name,
+we reset the store before processing it; if the result is in fresh store, we
+use that without copying. This is helpful for expanding strings like
+$message_headers which can get very long.
+
+There's a problem if a ${dlfunc item has side-effects that cause allocation,
+since resetting the store at the end of the expansion will free store that was
+allocated by the plugin code as well as the slop after the expanded string. So
+we skip any resets if ${dlfunc } has been used. The same applies for ${acl }
+and, given the acl condition, ${if }. This is an unfortunate consequence of
+string expansion becoming too powerful.
+
+Arguments:
+  string         the string to be expanded
+  ket_ends       true if expansion is to stop at }
+  left           if not NULL, a pointer to the first character after the
+                 expansion is placed here (typically used with ket_ends)
+  skipping       TRUE for recursive calls when the value isn't actually going
+                 to be used (to allow for optimisation)
+  honour_dollar  TRUE if $ is to be expanded,
+                 FALSE if it's just another character
+  resetok_p     if not NULL, pointer to flag - write FALSE if unsafe to reset
+                the store.
+
+Returns:         NULL if expansion fails:
+                   expand_string_forcedfail is set TRUE if failure was forced
+                   expand_string_message contains a textual error message
+                 a pointer to the expanded string on success
+*/
+
+static uschar *
+expand_string_internal(uschar *string, BOOL ket_ends, uschar **left,
+  BOOL skipping, BOOL honour_dollar, BOOL *resetok_p)
+{
+int ptr = 0;
+int size = Ustrlen(string)+ 64;
+int item_type;
+uschar *yield = store_get(size);
+uschar *s = string;
+uschar *save_expand_nstring[EXPAND_MAXN+1];
+int save_expand_nlength[EXPAND_MAXN+1];
+BOOL resetok = TRUE;
+
+expand_string_forcedfail = FALSE;
+expand_string_message = US"";
+
+while (*s != 0)
+  {
+  uschar *value;
+  uschar name[256];
+
+  /* \ escapes the next character, which must exist, or else
+  the expansion fails. There's a special escape, \N, which causes
+  copying of the subject verbatim up to the next \N. Otherwise,
+  the escapes are the standard set. */
+
+  if (*s == '\\')
+    {
+    if (s[1] == 0)
+      {
+      expand_string_message = US"\\ at end of string";
+      goto EXPAND_FAILED;
+      }
+
+    if (s[1] == 'N')
+      {
+      uschar *t = s + 2;
+      for (s = t; *s != 0; s++) if (*s == '\\' && s[1] == 'N') break;
+      yield = string_cat(yield, &size, &ptr, t, s - t);
+      if (*s != 0) s += 2;
+      }
+
+    else
+      {
+      uschar ch[1];
+      ch[0] = string_interpret_escape(&s);
+      s++;
+      yield = string_cat(yield, &size, &ptr, ch, 1);
+      }
+
+    continue;
+    }
+
+  /*{*/
+  /* Anything other than $ is just copied verbatim, unless we are
+  looking for a terminating } character. */
+
+  /*{*/
+  if (ket_ends && *s == '}') break;
+
+  if (*s != '$' || !honour_dollar)
+    {
+    yield = string_cat(yield, &size, &ptr, s++, 1);
+    continue;
+    }
+
+  /* No { after the $ - must be a plain name or a number for string
+  match variable. There has to be a fudge for variables that are the
+  names of header fields preceded by "$header_" because header field
+  names can contain any printing characters except space and colon.
+  For those that don't like typing this much, "$h_" is a synonym for
+  "$header_". A non-existent header yields a NULL value; nothing is
+  inserted. */ /*}*/
+
+  if (isalpha((*(++s))))
+    {
+    int len;
+    int newsize = 0;
+
+    s = read_name(name, sizeof(name), s, US"_");
+
+    /* If this is the first thing to be expanded, release the pre-allocated
+    buffer. */
+
+    if (ptr == 0 && yield != NULL)
+      {
+      if (resetok) store_reset(yield);
+      yield = NULL;
+      size = 0;
+      }
+
+    /* Header */
+
+    if (Ustrncmp(name, "h_", 2) == 0 ||
+        Ustrncmp(name, "rh_", 3) == 0 ||
+        Ustrncmp(name, "bh_", 3) == 0 ||
+        Ustrncmp(name, "header_", 7) == 0 ||
+        Ustrncmp(name, "rheader_", 8) == 0 ||
+        Ustrncmp(name, "bheader_", 8) == 0)
+      {
+      BOOL want_raw = (name[0] == 'r')? TRUE : FALSE;
+      uschar *charset = (name[0] == 'b')? NULL : headers_charset;
+      s = read_header_name(name, sizeof(name), s);
+      value = find_header(name, FALSE, &newsize, want_raw, charset);
+
+      /* If we didn't find the header, and the header contains a closing brace
+      character, this may be a user error where the terminating colon
+      has been omitted. Set a flag to adjust the error message in this case.
+      But there is no error here - nothing gets inserted. */
+
+      if (value == NULL)
+        {
+        if (Ustrchr(name, '}') != NULL) malformed_header = TRUE;
+        continue;
+        }
+      }
+
+    /* Variable */
+
+    else
+      {
+      value = find_variable(name, FALSE, skipping, &newsize);
+      if (value == NULL)
+        {
+        expand_string_message =
+          string_sprintf("unknown variable name \"%s\"", name);
+          check_variable_error_message(name);
+        goto EXPAND_FAILED;
+        }
+      }
+
+    /* If the data is known to be in a new buffer, newsize will be set to the
+    size of that buffer. If this is the first thing in an expansion string,
+    yield will be NULL; just point it at the new store instead of copying. Many
+    expansion strings contain just one reference, so this is a useful
+    optimization, especially for humungous headers. */
+
+    len = Ustrlen(value);
+    if (yield == NULL && newsize != 0)
+      {
+      yield = value;
+      size = newsize;
+      ptr = len;
+      }
+    else yield = string_cat(yield, &size, &ptr, value, len);
+
+    continue;
+    }
+
+  if (isdigit(*s))
+    {
+    int n;
+    s = read_number(&n, s);
+    if (n >= 0 && n <= expand_nmax)
+      yield = string_cat(yield, &size, &ptr, expand_nstring[n],
+        expand_nlength[n]);
+    continue;
+    }
+
+  /* Otherwise, if there's no '{' after $ it's an error. */            /*}*/
+
+  if (*s != '{')                                                       /*}*/
+    {
+    expand_string_message = US"$ not followed by letter, digit, or {"; /*}*/
+    goto EXPAND_FAILED;
+    }
+
+  /* After { there can be various things, but they all start with
+  an initial word, except for a number for a string match variable. */
+
+  if (isdigit((*(++s))))
+    {
+    int n;
+    s = read_number(&n, s);            /*{*/
+    if (*s++ != '}')
+      {                                        /*{*/
+      expand_string_message = US"} expected after number";
+      goto EXPAND_FAILED;
+      }
+    if (n >= 0 && n <= expand_nmax)
+      yield = string_cat(yield, &size, &ptr, expand_nstring[n],
+        expand_nlength[n]);
+    continue;
+    }
+
+  if (!isalpha(*s))
+    {
+    expand_string_message = US"letter or digit expected after ${";     /*}*/
+    goto EXPAND_FAILED;
+    }
+
+  /* Allow "-" in names to cater for substrings with negative
+  arguments. Since we are checking for known names after { this is
+  OK. */
+
+  s = read_name(name, sizeof(name), s, US"_-");
+  item_type = chop_match(name, item_table, sizeof(item_table)/sizeof(uschar *));
+
+  switch(item_type)
+    {
+    /* Call an ACL from an expansion.  We feed data in via $acl_arg1 - $acl_arg9.
+    If the ACL returns accept or reject we return content set by "message ="
+    There is currently no limit on recursion; this would have us call
+    acl_check_internal() directly and get a current level from somewhere.
+    See also the acl expansion condition ECOND_ACL and the traditional
+    acl modifier ACLC_ACL.
+    Assume that the function has side-effects on the store that must be preserved.
+    */
+
+    case EITEM_ACL:
+      /* ${acl {name} {arg1}{arg2}...} */
+      {
+      uschar *sub[10]; /* name + arg1-arg9 (which must match number of acl_arg[]) */
+      uschar *user_msg;
+
+      switch(read_subs(sub, 10, 1, &s, skipping, TRUE, US"acl", &resetok))
+        {
+        case 1: goto EXPAND_FAILED_CURLY;
+        case 2:
+        case 3: goto EXPAND_FAILED;
+        }
+      if (skipping) continue;
+
+      resetok = FALSE;
+      switch(eval_acl(sub, sizeof(sub)/sizeof(*sub), &user_msg))
+       {
+       case OK:
+       case FAIL:
+         DEBUG(D_expand)
+           debug_printf("acl expansion yield: %s\n", user_msg);
+         if (user_msg)
+            yield = string_cat(yield, &size, &ptr, user_msg, Ustrlen(user_msg));
+         continue;
+
+       case DEFER:
+          expand_string_forcedfail = TRUE;
+       default:
+          expand_string_message = string_sprintf("error from acl \"%s\"", sub[0]);
+         goto EXPAND_FAILED;
+       }
+      }
+
+    /* Handle conditionals - preserve the values of the numerical expansion
+    variables in case they get changed by a regular expression match in the
+    condition. If not, they retain their external settings. At the end
+    of this "if" section, they get restored to their previous values. */
+
+    case EITEM_IF:
+      {
+      BOOL cond = FALSE;
+      uschar *next_s;
+      int save_expand_nmax =
+        save_expand_strings(save_expand_nstring, save_expand_nlength);
+
+      while (isspace(*s)) s++;
+      next_s = eval_condition(s, &resetok, skipping? NULL : &cond);
+      if (next_s == NULL) goto EXPAND_FAILED;  /* message already set */
+
+      DEBUG(D_expand)
+        debug_printf("condition: %.*s\n   result: %s\n", (int)(next_s - s), s,
+          cond? "true" : "false");
+
+      s = next_s;
+
+      /* The handling of "yes" and "no" result strings is now in a separate
+      function that is also used by ${lookup} and ${extract} and ${run}. */
+
+      switch(process_yesno(
+               skipping,                     /* were previously skipping */
+               cond,                         /* success/failure indicator */
+               lookup_value,                 /* value to reset for string2 */
+               &s,                           /* input pointer */
+               &yield,                       /* output pointer */
+               &size,                        /* output size */
+               &ptr,                         /* output current point */
+               US"if",                       /* condition type */
+              &resetok))
+        {
+        case 1: goto EXPAND_FAILED;          /* when all is well, the */
+        case 2: goto EXPAND_FAILED_CURLY;    /* returned value is 0 */
+        }
+
+      /* Restore external setting of expansion variables for continuation
+      at this level. */
+
+      restore_expand_strings(save_expand_nmax, save_expand_nstring,
+        save_expand_nlength);
+      continue;
+      }
+
+    /* Handle database lookups unless locked out. If "skipping" is TRUE, we are
+    expanding an internal string that isn't actually going to be used. All we
+    need to do is check the syntax, so don't do a lookup at all. Preserve the
+    values of the numerical expansion variables in case they get changed by a
+    partial lookup. If not, they retain their external settings. At the end
+    of this "lookup" section, they get restored to their previous values. */
+
+    case EITEM_LOOKUP:
+      {
+      int stype, partial, affixlen, starflags;
+      int expand_setup = 0;
+      int nameptr = 0;
+      uschar *key, *filename, *affix;
+      uschar *save_lookup_value = lookup_value;
+      int save_expand_nmax =
+        save_expand_strings(save_expand_nstring, save_expand_nlength);
+
+      if ((expand_forbid & RDO_LOOKUP) != 0)
+        {
+        expand_string_message = US"lookup expansions are not permitted";
+        goto EXPAND_FAILED;
+        }
+
+      /* Get the key we are to look up for single-key+file style lookups.
+      Otherwise set the key NULL pro-tem. */
+
+      while (isspace(*s)) s++;
+      if (*s == '{')                                   /*}*/
+        {
+        key = expand_string_internal(s+1, TRUE, &s, skipping, TRUE, &resetok);
+        if (key == NULL) goto EXPAND_FAILED;           /*{*/
+        if (*s++ != '}') goto EXPAND_FAILED_CURLY;
+        while (isspace(*s)) s++;
+        }
+      else key = NULL;
+
+      /* Find out the type of database */
+
+      if (!isalpha(*s))
+        {
+        expand_string_message = US"missing lookup type";
+        goto EXPAND_FAILED;
+        }
+
+      /* The type is a string that may contain special characters of various
+      kinds. Allow everything except space or { to appear; the actual content
+      is checked by search_findtype_partial. */                /*}*/
+
+      while (*s != 0 && *s != '{' && !isspace(*s))     /*}*/
+        {
+        if (nameptr < sizeof(name) - 1) name[nameptr++] = *s;
+        s++;
+        }
+      name[nameptr] = 0;
+      while (isspace(*s)) s++;
+
+      /* Now check for the individual search type and any partial or default
+      options. Only those types that are actually in the binary are valid. */
+
+      stype = search_findtype_partial(name, &partial, &affix, &affixlen,
+        &starflags);
+      if (stype < 0)
+        {
+        expand_string_message = search_error_message;
+        goto EXPAND_FAILED;
+        }
+
+      /* Check that a key was provided for those lookup types that need it,
+      and was not supplied for those that use the query style. */
+
+      if (!mac_islookup(stype, lookup_querystyle|lookup_absfilequery))
+        {
+        if (key == NULL)
+          {
+          expand_string_message = string_sprintf("missing {key} for single-"
+            "key \"%s\" lookup", name);
+          goto EXPAND_FAILED;
+          }
+        }
+      else
+        {
+        if (key != NULL)
+          {
+          expand_string_message = string_sprintf("a single key was given for "
+            "lookup type \"%s\", which is not a single-key lookup type", name);
+          goto EXPAND_FAILED;
+          }
+        }
+
+      /* Get the next string in brackets and expand it. It is the file name for
+      single-key+file lookups, and the whole query otherwise. In the case of
+      queries that also require a file name (e.g. sqlite), the file name comes
+      first. */
+
+      if (*s != '{') goto EXPAND_FAILED_CURLY;
+      filename = expand_string_internal(s+1, TRUE, &s, skipping, TRUE, &resetok);
+      if (filename == NULL) goto EXPAND_FAILED;
+      if (*s++ != '}') goto EXPAND_FAILED_CURLY;
+      while (isspace(*s)) s++;
+
+      /* If this isn't a single-key+file lookup, re-arrange the variables
+      to be appropriate for the search_ functions. For query-style lookups,
+      there is just a "key", and no file name. For the special query-style +
+      file types, the query (i.e. "key") starts with a file name. */
+
+      if (key == NULL)
+        {
+        while (isspace(*filename)) filename++;
+        key = filename;
+
+        if (mac_islookup(stype, lookup_querystyle))
+          {
+          filename = NULL;
+          }
+        else
+          {
+          if (*filename != '/')
+            {
+            expand_string_message = string_sprintf(
+              "absolute file name expected for \"%s\" lookup", name);
+            goto EXPAND_FAILED;
+            }
+          while (*key != 0 && !isspace(*key)) key++;
+          if (*key != 0) *key++ = 0;
+          }
+        }
+
+      /* If skipping, don't do the next bit - just lookup_value == NULL, as if
+      the entry was not found. Note that there is no search_close() function.
+      Files are left open in case of re-use. At suitable places in higher logic,
+      search_tidyup() is called to tidy all open files. This can save opening
+      the same file several times. However, files may also get closed when
+      others are opened, if too many are open at once. The rule is that a
+      handle should not be used after a second search_open().
+
+      Request that a partial search sets up $1 and maybe $2 by passing
+      expand_setup containing zero. If its value changes, reset expand_nmax,
+      since new variables will have been set. Note that at the end of this
+      "lookup" section, the old numeric variables are restored. */
+
+      if (skipping)
+        lookup_value = NULL;
+      else
+        {
+        void *handle = search_open(filename, stype, 0, NULL, NULL);
+        if (handle == NULL)
+          {
+          expand_string_message = search_error_message;
+          goto EXPAND_FAILED;
+          }
+        lookup_value = search_find(handle, filename, key, partial, affix,
+          affixlen, starflags, &expand_setup);
+        if (search_find_defer)
+          {
+          expand_string_message =
+            string_sprintf("lookup of \"%s\" gave DEFER: %s",
+              string_printing2(key, FALSE), search_error_message);
+          goto EXPAND_FAILED;
+          }
+        if (expand_setup > 0) expand_nmax = expand_setup;
+        }
+
+      /* The handling of "yes" and "no" result strings is now in a separate
+      function that is also used by ${if} and ${extract}. */
+
+      switch(process_yesno(
+               skipping,                     /* were previously skipping */
+               lookup_value != NULL,         /* success/failure indicator */
+               save_lookup_value,            /* value to reset for string2 */
+               &s,                           /* input pointer */
+               &yield,                       /* output pointer */
+               &size,                        /* output size */
+               &ptr,                         /* output current point */
+               US"lookup",                   /* condition type */
+              &resetok))
+        {
+        case 1: goto EXPAND_FAILED;          /* when all is well, the */
+        case 2: goto EXPAND_FAILED_CURLY;    /* returned value is 0 */
+        }
+
+      /* Restore external setting of expansion variables for carrying on
+      at this level, and continue. */
+
+      restore_expand_strings(save_expand_nmax, save_expand_nstring,
+        save_expand_nlength);
+      continue;
+      }
+
+    /* If Perl support is configured, handle calling embedded perl subroutines,
+    unless locked out at this time. Syntax is ${perl{sub}} or ${perl{sub}{arg}}
+    or ${perl{sub}{arg1}{arg2}} or up to a maximum of EXIM_PERL_MAX_ARGS
+    arguments (defined below). */
+
+    #define EXIM_PERL_MAX_ARGS 8
+
+    case EITEM_PERL:
+    #ifndef EXIM_PERL
+    expand_string_message = US"\"${perl\" encountered, but this facility "     /*}*/
+      "is not included in this binary";
+    goto EXPAND_FAILED;
+
+    #else   /* EXIM_PERL */
+      {
+      uschar *sub_arg[EXIM_PERL_MAX_ARGS + 2];
+      uschar *new_yield;
+
+      if ((expand_forbid & RDO_PERL) != 0)
+        {
+        expand_string_message = US"Perl calls are not permitted";
+        goto EXPAND_FAILED;
+        }
+
+      switch(read_subs(sub_arg, EXIM_PERL_MAX_ARGS + 1, 1, &s, skipping, TRUE,
+           US"perl", &resetok))
+        {
+        case 1: goto EXPAND_FAILED_CURLY;
+        case 2:
+        case 3: goto EXPAND_FAILED;
+        }
+
+      /* If skipping, we don't actually do anything */
+
+      if (skipping) continue;
+
+      /* Start the interpreter if necessary */
+
+      if (!opt_perl_started)
+        {
+        uschar *initerror;
+        if (opt_perl_startup == NULL)
+          {
+          expand_string_message = US"A setting of perl_startup is needed when "
+            "using the Perl interpreter";
+          goto EXPAND_FAILED;
+          }
+        DEBUG(D_any) debug_printf("Starting Perl interpreter\n");
+        initerror = init_perl(opt_perl_startup);
+        if (initerror != NULL)
+          {
+          expand_string_message =
+            string_sprintf("error in perl_startup code: %s\n", initerror);
+          goto EXPAND_FAILED;
+          }
+        opt_perl_started = TRUE;
+        }
+
+      /* Call the function */
+
+      sub_arg[EXIM_PERL_MAX_ARGS + 1] = NULL;
+      new_yield = call_perl_cat(yield, &size, &ptr, &expand_string_message,
+        sub_arg[0], sub_arg + 1);
+
+      /* NULL yield indicates failure; if the message pointer has been set to
+      NULL, the yield was undef, indicating a forced failure. Otherwise the
+      message will indicate some kind of Perl error. */
+
+      if (new_yield == NULL)
+        {
+        if (expand_string_message == NULL)
+          {
+          expand_string_message =
+            string_sprintf("Perl subroutine \"%s\" returned undef to force "
+              "failure", sub_arg[0]);
+          expand_string_forcedfail = TRUE;
+          }
+        goto EXPAND_FAILED;
+        }
+
+      /* Yield succeeded. Ensure forcedfail is unset, just in case it got
+      set during a callback from Perl. */
+
+      expand_string_forcedfail = FALSE;
+      yield = new_yield;
+      continue;
+      }
+    #endif /* EXIM_PERL */
+
+    /* Transform email address to "prvs" scheme to use
+       as BATV-signed return path */
+
+    case EITEM_PRVS:
+      {
+      uschar *sub_arg[3];
+      uschar *p,*domain;
+
+      switch(read_subs(sub_arg, 3, 2, &s, skipping, TRUE, US"prvs", &resetok))
+        {
+        case 1: goto EXPAND_FAILED_CURLY;
+        case 2:
+        case 3: goto EXPAND_FAILED;
+        }
+
+      /* If skipping, we don't actually do anything */
+      if (skipping) continue;
+
+      /* sub_arg[0] is the address */
+      domain = Ustrrchr(sub_arg[0],'@');
+      if ( (domain == NULL) || (domain == sub_arg[0]) || (Ustrlen(domain) == 1) )
+        {
+        expand_string_message = US"prvs first argument must be a qualified email address";
+        goto EXPAND_FAILED;
+        }
+
+      /* Calculate the hash. The second argument must be a single-digit
+      key number, or unset. */
+
+      if (sub_arg[2] != NULL &&
+          (!isdigit(sub_arg[2][0]) || sub_arg[2][1] != 0))
+        {
+        expand_string_message = US"prvs second argument must be a single digit";
+        goto EXPAND_FAILED;
+        }
+
+      p = prvs_hmac_sha1(sub_arg[0],sub_arg[1],sub_arg[2],prvs_daystamp(7));
+      if (p == NULL)
+        {
+        expand_string_message = US"prvs hmac-sha1 conversion failed";
+        goto EXPAND_FAILED;
+        }
+
+      /* Now separate the domain from the local part */
+      *domain++ = '\0';
+
+      yield = string_cat(yield,&size,&ptr,US"prvs=",5);
+      string_cat(yield,&size,&ptr,(sub_arg[2] != NULL) ? sub_arg[2] : US"0", 1);
+      string_cat(yield,&size,&ptr,prvs_daystamp(7),3);
+      string_cat(yield,&size,&ptr,p,6);
+      string_cat(yield,&size,&ptr,US"=",1);
+      string_cat(yield,&size,&ptr,sub_arg[0],Ustrlen(sub_arg[0]));
+      string_cat(yield,&size,&ptr,US"@",1);
+      string_cat(yield,&size,&ptr,domain,Ustrlen(domain));
+
+      continue;
+      }
+
+    /* Check a prvs-encoded address for validity */
+
+    case EITEM_PRVSCHECK:
+      {
+      uschar *sub_arg[3];
+      int mysize = 0, myptr = 0;
+      const pcre *re;
+      uschar *p;
+
+      /* TF: Ugliness: We want to expand parameter 1 first, then set
+         up expansion variables that are used in the expansion of
+         parameter 2. So we clone the string for the first
+         expansion, where we only expand parameter 1.
+
+         PH: Actually, that isn't necessary. The read_subs() function is
+         designed to work this way for the ${if and ${lookup expansions. I've
+         tidied the code.
+      */
+
+      /* Reset expansion variables */
+      prvscheck_result = NULL;
+      prvscheck_address = NULL;
+      prvscheck_keynum = NULL;
+
+      switch(read_subs(sub_arg, 1, 1, &s, skipping, FALSE, US"prvs", &resetok))
+        {
+        case 1: goto EXPAND_FAILED_CURLY;
+        case 2:
+        case 3: goto EXPAND_FAILED;
+        }
+
+      re = regex_must_compile(US"^prvs\\=([0-9])([0-9]{3})([A-F0-9]{6})\\=(.+)\\@(.+)$",
+                              TRUE,FALSE);
+
+      if (regex_match_and_setup(re,sub_arg[0],0,-1))
+        {
+        uschar *local_part = string_copyn(expand_nstring[4],expand_nlength[4]);
+        uschar *key_num = string_copyn(expand_nstring[1],expand_nlength[1]);
+        uschar *daystamp = string_copyn(expand_nstring[2],expand_nlength[2]);
+        uschar *hash = string_copyn(expand_nstring[3],expand_nlength[3]);
+        uschar *domain = string_copyn(expand_nstring[5],expand_nlength[5]);
+
+        DEBUG(D_expand) debug_printf("prvscheck localpart: %s\n", local_part);
+        DEBUG(D_expand) debug_printf("prvscheck key number: %s\n", key_num);
+        DEBUG(D_expand) debug_printf("prvscheck daystamp: %s\n", daystamp);
+        DEBUG(D_expand) debug_printf("prvscheck hash: %s\n", hash);
+        DEBUG(D_expand) debug_printf("prvscheck domain: %s\n", domain);
+
+        /* Set up expansion variables */
+        prvscheck_address = string_cat(NULL, &mysize, &myptr, local_part, Ustrlen(local_part));
+        string_cat(prvscheck_address,&mysize,&myptr,US"@",1);
+        string_cat(prvscheck_address,&mysize,&myptr,domain,Ustrlen(domain));
+        prvscheck_address[myptr] = '\0';
+        prvscheck_keynum = string_copy(key_num);
+
+        /* Now expand the second argument */
+        switch(read_subs(sub_arg, 1, 1, &s, skipping, FALSE, US"prvs", &resetok))
+          {
+          case 1: goto EXPAND_FAILED_CURLY;
+          case 2:
+          case 3: goto EXPAND_FAILED;
+          }
+
+        /* Now we have the key and can check the address. */
+
+        p = prvs_hmac_sha1(prvscheck_address, sub_arg[0], prvscheck_keynum,
+          daystamp);
+
+        if (p == NULL)
+          {
+          expand_string_message = US"hmac-sha1 conversion failed";
+          goto EXPAND_FAILED;
+          }
+
+        DEBUG(D_expand) debug_printf("prvscheck: received hash is %s\n", hash);
+        DEBUG(D_expand) debug_printf("prvscheck:      own hash is %s\n", p);
+
+        if (Ustrcmp(p,hash) == 0)
+          {
+          /* Success, valid BATV address. Now check the expiry date. */
+          uschar *now = prvs_daystamp(0);
+          unsigned int inow = 0,iexpire = 1;
+
+          (void)sscanf(CS now,"%u",&inow);
+          (void)sscanf(CS daystamp,"%u",&iexpire);
+
+          /* When "iexpire" is < 7, a "flip" has occured.
+             Adjust "inow" accordingly. */
+          if ( (iexpire < 7) && (inow >= 993) ) inow = 0;
+
+          if (iexpire >= inow)
+            {
+            prvscheck_result = US"1";
+            DEBUG(D_expand) debug_printf("prvscheck: success, $pvrs_result set to 1\n");
+            }
+            else
+            {
+            prvscheck_result = NULL;
+            DEBUG(D_expand) debug_printf("prvscheck: signature expired, $pvrs_result unset\n");
+            }
+          }
+        else
+          {
+          prvscheck_result = NULL;
+          DEBUG(D_expand) debug_printf("prvscheck: hash failure, $pvrs_result unset\n");
+          }
+
+        /* Now expand the final argument. We leave this till now so that
+        it can include $prvscheck_result. */
+
+        switch(read_subs(sub_arg, 1, 0, &s, skipping, TRUE, US"prvs", &resetok))
+          {
+          case 1: goto EXPAND_FAILED_CURLY;
+          case 2:
+          case 3: goto EXPAND_FAILED;
+          }
+
+        if (sub_arg[0] == NULL || *sub_arg[0] == '\0')
+          yield = string_cat(yield,&size,&ptr,prvscheck_address,Ustrlen(prvscheck_address));
+        else
+          yield = string_cat(yield,&size,&ptr,sub_arg[0],Ustrlen(sub_arg[0]));
+
+        /* Reset the "internal" variables afterwards, because they are in
+        dynamic store that will be reclaimed if the expansion succeeded. */
+
+        prvscheck_address = NULL;
+        prvscheck_keynum = NULL;
+        }
+      else
+        {
+        /* Does not look like a prvs encoded address, return the empty string.
+           We need to make sure all subs are expanded first, so as to skip over
+           the entire item. */
+
+        switch(read_subs(sub_arg, 2, 1, &s, skipping, TRUE, US"prvs", &resetok))
+          {
+          case 1: goto EXPAND_FAILED_CURLY;
+          case 2:
+          case 3: goto EXPAND_FAILED;
+          }
+        }
+
+      continue;
+      }
+
+    /* Handle "readfile" to insert an entire file */
+
+    case EITEM_READFILE:
+      {
+      FILE *f;
+      uschar *sub_arg[2];
+
+      if ((expand_forbid & RDO_READFILE) != 0)
+        {
+        expand_string_message = US"file insertions are not permitted";
+        goto EXPAND_FAILED;
+        }
+
+      switch(read_subs(sub_arg, 2, 1, &s, skipping, TRUE, US"readfile", &resetok))
+        {
+        case 1: goto EXPAND_FAILED_CURLY;
+        case 2:
+        case 3: goto EXPAND_FAILED;
+        }
+
+      /* If skipping, we don't actually do anything */
+
+      if (skipping) continue;
+
+      /* Open the file and read it */
+
+      f = Ufopen(sub_arg[0], "rb");
+      if (f == NULL)
+        {
+        expand_string_message = string_open_failed(errno, "%s", sub_arg[0]);
+        goto EXPAND_FAILED;
+        }
+
+      yield = cat_file(f, yield, &size, &ptr, sub_arg[1]);
+      (void)fclose(f);
+      continue;
+      }
+
+    /* Handle "readsocket" to insert data from a Unix domain socket */
+
+    case EITEM_READSOCK:
+      {
+      int fd;
+      int timeout = 5;
+      int save_ptr = ptr;
+      FILE *f;
+      struct sockaddr_un sockun;         /* don't call this "sun" ! */
+      uschar *arg;
+      uschar *sub_arg[4];
+
+      if ((expand_forbid & RDO_READSOCK) != 0)
+        {
+        expand_string_message = US"socket insertions are not permitted";
+        goto EXPAND_FAILED;
+        }
+
+      /* Read up to 4 arguments, but don't do the end of item check afterwards,
+      because there may be a string for expansion on failure. */
+
+      switch(read_subs(sub_arg, 4, 2, &s, skipping, FALSE, US"readsocket", &resetok))
+        {
+        case 1: goto EXPAND_FAILED_CURLY;
+        case 2:                             /* Won't occur: no end check */
+        case 3: goto EXPAND_FAILED;
+        }
+
+      /* Sort out timeout, if given */
+
+      if (sub_arg[2] != NULL)
+        {
+        timeout = readconf_readtime(sub_arg[2], 0, FALSE);
+        if (timeout < 0)
+          {
+          expand_string_message = string_sprintf("bad time value %s",
+            sub_arg[2]);
+          goto EXPAND_FAILED;
+          }
+        }
+      else sub_arg[3] = NULL;                     /* No eol if no timeout */
+
+      /* If skipping, we don't actually do anything. Otherwise, arrange to
+      connect to either an IP or a Unix socket. */
+
+      if (!skipping)
+        {
+        /* Handle an IP (internet) domain */
+
+        if (Ustrncmp(sub_arg[0], "inet:", 5) == 0)
+          {
+          int port;
+          uschar *server_name = sub_arg[0] + 5;
+          uschar *port_name = Ustrrchr(server_name, ':');
+
+          /* Sort out the port */
+
+          if (port_name == NULL)
+            {
+            expand_string_message =
+              string_sprintf("missing port for readsocket %s", sub_arg[0]);
+            goto EXPAND_FAILED;
+            }
+          *port_name++ = 0;           /* Terminate server name */
+
+          if (isdigit(*port_name))
+            {
+            uschar *end;
+            port = Ustrtol(port_name, &end, 0);
+            if (end != port_name + Ustrlen(port_name))
+              {
+              expand_string_message =
+                string_sprintf("invalid port number %s", port_name);
+              goto EXPAND_FAILED;
+              }
+            }
+          else
+            {
+            struct servent *service_info = getservbyname(CS port_name, "tcp");
+            if (service_info == NULL)
+              {
+              expand_string_message = string_sprintf("unknown port \"%s\"",
+                port_name);
+              goto EXPAND_FAILED;
+              }
+            port = ntohs(service_info->s_port);
+            }
+
+         if ((fd = ip_connectedsocket(SOCK_STREAM, server_name, port, port,
+                 timeout, NULL, &expand_string_message)) < 0)
+              goto SOCK_FAIL;
+          }
+
+        /* Handle a Unix domain socket */
+
+        else
+          {
+          int rc;
+          if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) == -1)
+            {
+            expand_string_message = string_sprintf("failed to create socket: %s",
+              strerror(errno));
+            goto SOCK_FAIL;
+            }
+
+          sockun.sun_family = AF_UNIX;
+          sprintf(sockun.sun_path, "%.*s", (int)(sizeof(sockun.sun_path)-1),
+            sub_arg[0]);
+
+          sigalrm_seen = FALSE;
+          alarm(timeout);
+          rc = connect(fd, (struct sockaddr *)(&sockun), sizeof(sockun));
+          alarm(0);
+          if (sigalrm_seen)
+            {
+            expand_string_message = US "socket connect timed out";
+            goto SOCK_FAIL;
+            }
+          if (rc < 0)
+            {
+            expand_string_message = string_sprintf("failed to connect to socket "
+              "%s: %s", sub_arg[0], strerror(errno));
+            goto SOCK_FAIL;
+            }
+          }
+
+        DEBUG(D_expand) debug_printf("connected to socket %s\n", sub_arg[0]);
+
+       /* Allow sequencing of test actions */
+       if (running_in_test_harness) millisleep(100);
+
+        /* Write the request string, if not empty */
+
+        if (sub_arg[1][0] != 0)
+          {
+          int len = Ustrlen(sub_arg[1]);
+          DEBUG(D_expand) debug_printf("writing \"%s\" to socket\n",
+            sub_arg[1]);
+          if (write(fd, sub_arg[1], len) != len)
+            {
+            expand_string_message = string_sprintf("request write to socket "
+              "failed: %s", strerror(errno));
+            goto SOCK_FAIL;
+            }
+          }
+
+        /* Shut down the sending side of the socket. This helps some servers to
+        recognise that it is their turn to do some work. Just in case some
+        system doesn't have this function, make it conditional. */
+
+        #ifdef SHUT_WR
+        shutdown(fd, SHUT_WR);
+        #endif
+
+       if (running_in_test_harness) millisleep(100);
+
+        /* Now we need to read from the socket, under a timeout. The function
+        that reads a file can be used. */
+
+        f = fdopen(fd, "rb");
+        sigalrm_seen = FALSE;
+        alarm(timeout);
+        yield = cat_file(f, yield, &size, &ptr, sub_arg[3]);
+        alarm(0);
+        (void)fclose(f);
+
+        /* After a timeout, we restore the pointer in the result, that is,
+        make sure we add nothing from the socket. */
+
+        if (sigalrm_seen)
+          {
+          ptr = save_ptr;
+          expand_string_message = US "socket read timed out";
+          goto SOCK_FAIL;
+          }
+        }
+
+      /* The whole thing has worked (or we were skipping). If there is a
+      failure string following, we need to skip it. */
+
+      if (*s == '{')
+        {
+        if (expand_string_internal(s+1, TRUE, &s, TRUE, TRUE, &resetok) == NULL)
+          goto EXPAND_FAILED;
+        if (*s++ != '}') goto EXPAND_FAILED_CURLY;
+        while (isspace(*s)) s++;
+        }
+      if (*s++ != '}') goto EXPAND_FAILED_CURLY;
+      continue;
+
+      /* Come here on failure to create socket, connect socket, write to the
+      socket, or timeout on reading. If another substring follows, expand and
+      use it. Otherwise, those conditions give expand errors. */
+
+      SOCK_FAIL:
+      if (*s != '{') goto EXPAND_FAILED;
+      DEBUG(D_any) debug_printf("%s\n", expand_string_message);
+      arg = expand_string_internal(s+1, TRUE, &s, FALSE, TRUE, &resetok);
+      if (arg == NULL) goto EXPAND_FAILED;
+      yield = string_cat(yield, &size, &ptr, arg, Ustrlen(arg));
+      if (*s++ != '}') goto EXPAND_FAILED_CURLY;
+      while (isspace(*s)) s++;
+      if (*s++ != '}') goto EXPAND_FAILED_CURLY;
+      continue;
+      }
+
+    /* Handle "run" to execute a program. */
+
+    case EITEM_RUN:
+      {
+      FILE *f;
+      uschar *arg;
+      uschar **argv;
+      pid_t pid;
+      int fd_in, fd_out;
+      int lsize = 0;
+      int lptr = 0;
+
+      if ((expand_forbid & RDO_RUN) != 0)
+        {
+        expand_string_message = US"running a command is not permitted";
+        goto EXPAND_FAILED;
+        }
+
+      while (isspace(*s)) s++;
+      if (*s != '{') goto EXPAND_FAILED_CURLY;
+      arg = expand_string_internal(s+1, TRUE, &s, skipping, TRUE, &resetok);
+      if (arg == NULL) goto EXPAND_FAILED;
+      while (isspace(*s)) s++;
+      if (*s++ != '}') goto EXPAND_FAILED_CURLY;
+
+      if (skipping)   /* Just pretend it worked when we're skipping */
+        {
+        runrc = 0;
+        }
+      else
+        {
+        if (!transport_set_up_command(&argv,    /* anchor for arg list */
+            arg,                                /* raw command */
+            FALSE,                              /* don't expand the arguments */
+            0,                                  /* not relevant when... */
+            NULL,                               /* no transporting address */
+            US"${run} expansion",               /* for error messages */
+            &expand_string_message))            /* where to put error message */
+          {
+          goto EXPAND_FAILED;
+          }
+
+        /* Create the child process, making it a group leader. */
+
+        pid = child_open(argv, NULL, 0077, &fd_in, &fd_out, TRUE);
+
+        if (pid < 0)
+          {
+          expand_string_message =
+            string_sprintf("couldn't create child process: %s", strerror(errno));
+          goto EXPAND_FAILED;
+          }
+
+        /* Nothing is written to the standard input. */
+
+        (void)close(fd_in);
+
+        /* Read the pipe to get the command's output into $value (which is kept
+        in lookup_value). Read during execution, so that if the output exceeds
+        the OS pipe buffer limit, we don't block forever. */
+
+        f = fdopen(fd_out, "rb");
+        sigalrm_seen = FALSE;
+        alarm(60);
+        lookup_value = cat_file(f, lookup_value, &lsize, &lptr, NULL);
+        alarm(0);
+        (void)fclose(f);
+
+        /* Wait for the process to finish, applying the timeout, and inspect its
+        return code for serious disasters. Simple non-zero returns are passed on.
+        */
+
+        if (sigalrm_seen == TRUE || (runrc = child_close(pid, 30)) < 0)
+          {
+          if (sigalrm_seen == TRUE || runrc == -256)
+            {
+            expand_string_message = string_sprintf("command timed out");
+            killpg(pid, SIGKILL);       /* Kill the whole process group */
+            }
+
+          else if (runrc == -257)
+            expand_string_message = string_sprintf("wait() failed: %s",
+              strerror(errno));
+
+          else
+            expand_string_message = string_sprintf("command killed by signal %d",
+              -runrc);
+
+          goto EXPAND_FAILED;
+          }
+        }
+
+      /* Process the yes/no strings; $value may be useful in both cases */
+
+      switch(process_yesno(
+               skipping,                     /* were previously skipping */
+               runrc == 0,                   /* success/failure indicator */
+               lookup_value,                 /* value to reset for string2 */
+               &s,                           /* input pointer */
+               &yield,                       /* output pointer */
+               &size,                        /* output size */
+               &ptr,                         /* output current point */
+               US"run",                      /* condition type */
+              &resetok))
+        {
+        case 1: goto EXPAND_FAILED;          /* when all is well, the */
+        case 2: goto EXPAND_FAILED_CURLY;    /* returned value is 0 */
+        }
+
+      continue;
+      }
+
+    /* Handle character translation for "tr" */
+
+    case EITEM_TR:
+      {
+      int oldptr = ptr;
+      int o2m;
+      uschar *sub[3];
+
+      switch(read_subs(sub, 3, 3, &s, skipping, TRUE, US"tr", &resetok))
+        {
+        case 1: goto EXPAND_FAILED_CURLY;
+        case 2:
+        case 3: goto EXPAND_FAILED;
+        }
+
+      yield = string_cat(yield, &size, &ptr, sub[0], Ustrlen(sub[0]));
+      o2m = Ustrlen(sub[2]) - 1;
+
+      if (o2m >= 0) for (; oldptr < ptr; oldptr++)
+        {
+        uschar *m = Ustrrchr(sub[1], yield[oldptr]);
+        if (m != NULL)
+          {
+          int o = m - sub[1];
+          yield[oldptr] = sub[2][(o < o2m)? o : o2m];
+          }
+        }
+
+      continue;
+      }
+
+    /* Handle "hash", "length", "nhash", and "substr" when they are given with
+    expanded arguments. */
+
+    case EITEM_HASH:
+    case EITEM_LENGTH:
+    case EITEM_NHASH:
+    case EITEM_SUBSTR:
+      {
+      int i;
+      int len;
+      uschar *ret;
+      int val[2] = { 0, -1 };
+      uschar *sub[3];
+
+      /* "length" takes only 2 arguments whereas the others take 2 or 3.
+      Ensure that sub[2] is set in the ${length } case. */
+
+      sub[2] = NULL;
+      switch(read_subs(sub, (item_type == EITEM_LENGTH)? 2:3, 2, &s, skipping,
+             TRUE, name, &resetok))
+        {
+        case 1: goto EXPAND_FAILED_CURLY;
+        case 2:
+        case 3: goto EXPAND_FAILED;
+        }
+
+      /* Juggle the arguments if there are only two of them: always move the
+      string to the last position and make ${length{n}{str}} equivalent to
+      ${substr{0}{n}{str}}. See the defaults for val[] above. */
+
+      if (sub[2] == NULL)
+        {
+        sub[2] = sub[1];
+        sub[1] = NULL;
+        if (item_type == EITEM_LENGTH)
+          {
+          sub[1] = sub[0];
+          sub[0] = NULL;
+          }
+        }
+
+      for (i = 0; i < 2; i++)
+        {
+        if (sub[i] == NULL) continue;
+        val[i] = (int)Ustrtol(sub[i], &ret, 10);
+        if (*ret != 0 || (i != 0 && val[i] < 0))
+          {
+          expand_string_message = string_sprintf("\"%s\" is not a%s number "
+            "(in \"%s\" expansion)", sub[i], (i != 0)? " positive" : "", name);
+          goto EXPAND_FAILED;
+          }
+        }
+
+      ret =
+        (item_type == EITEM_HASH)?
+          compute_hash(sub[2], val[0], val[1], &len) :
+        (item_type == EITEM_NHASH)?
+          compute_nhash(sub[2], val[0], val[1], &len) :
+          extract_substr(sub[2], val[0], val[1], &len);
+
+      if (ret == NULL) goto EXPAND_FAILED;
+      yield = string_cat(yield, &size, &ptr, ret, len);
+      continue;
+      }
+
+    /* Handle HMAC computation: ${hmac{<algorithm>}{<secret>}{<text>}}
+    This code originally contributed by Steve Haslam. It currently supports
+    the use of MD5 and SHA-1 hashes.
+
+    We need some workspace that is large enough to handle all the supported
+    hash types. Use macros to set the sizes rather than be too elaborate. */
+
+    #define MAX_HASHLEN      20
+    #define MAX_HASHBLOCKLEN 64
+
+    case EITEM_HMAC:
+      {
+      uschar *sub[3];
+      md5 md5_base;
+      sha1 sha1_base;
+      void *use_base;
+      int type, i;
+      int hashlen;      /* Number of octets for the hash algorithm's output */
+      int hashblocklen; /* Number of octets the hash algorithm processes */
+      uschar *keyptr, *p;
+      unsigned int keylen;
+
+      uschar keyhash[MAX_HASHLEN];
+      uschar innerhash[MAX_HASHLEN];
+      uschar finalhash[MAX_HASHLEN];
+      uschar finalhash_hex[2*MAX_HASHLEN];
+      uschar innerkey[MAX_HASHBLOCKLEN];
+      uschar outerkey[MAX_HASHBLOCKLEN];
+
+      switch (read_subs(sub, 3, 3, &s, skipping, TRUE, name, &resetok))
+        {
+        case 1: goto EXPAND_FAILED_CURLY;
+        case 2:
+        case 3: goto EXPAND_FAILED;
+        }
+
+      if (Ustrcmp(sub[0], "md5") == 0)
+        {
+        type = HMAC_MD5;
+        use_base = &md5_base;
+        hashlen = 16;
+        hashblocklen = 64;
+        }
+      else if (Ustrcmp(sub[0], "sha1") == 0)
+        {
+        type = HMAC_SHA1;
+        use_base = &sha1_base;
+        hashlen = 20;
+        hashblocklen = 64;
+        }
+      else
+        {
+        expand_string_message =
+          string_sprintf("hmac algorithm \"%s\" is not recognised", sub[0]);
+        goto EXPAND_FAILED;
+        }
+
+      keyptr = sub[1];
+      keylen = Ustrlen(keyptr);
+
+      /* If the key is longer than the hash block length, then hash the key
+      first */
+
+      if (keylen > hashblocklen)
+        {
+        chash_start(type, use_base);
+        chash_end(type, use_base, keyptr, keylen, keyhash);
+        keyptr = keyhash;
+        keylen = hashlen;
+        }
+
+      /* Now make the inner and outer key values */
+
+      memset(innerkey, 0x36, hashblocklen);
+      memset(outerkey, 0x5c, hashblocklen);
+
+      for (i = 0; i < keylen; i++)
+        {
+        innerkey[i] ^= keyptr[i];
+        outerkey[i] ^= keyptr[i];
+        }
+
+      /* Now do the hashes */
+
+      chash_start(type, use_base);
+      chash_mid(type, use_base, innerkey);
+      chash_end(type, use_base, sub[2], Ustrlen(sub[2]), innerhash);
+
+      chash_start(type, use_base);
+      chash_mid(type, use_base, outerkey);
+      chash_end(type, use_base, innerhash, hashlen, finalhash);
+
+      /* Encode the final hash as a hex string */
+
+      p = finalhash_hex;
+      for (i = 0; i < hashlen; i++)
+        {
+        *p++ = hex_digits[(finalhash[i] & 0xf0) >> 4];
+        *p++ = hex_digits[finalhash[i] & 0x0f];
+        }
+
+      DEBUG(D_any) debug_printf("HMAC[%s](%.*s,%.*s)=%.*s\n", sub[0],
+        (int)keylen, keyptr, Ustrlen(sub[2]), sub[2], hashlen*2, finalhash_hex);
+
+      yield = string_cat(yield, &size, &ptr, finalhash_hex, hashlen*2);
+      }
+
+    continue;
+
+    /* Handle global substitution for "sg" - like Perl's s/xxx/yyy/g operator.
+    We have to save the numerical variables and restore them afterwards. */
+
+    case EITEM_SG:
+      {
+      const pcre *re;
+      int moffset, moffsetextra, slen;
+      int roffset;
+      int emptyopt;
+      const uschar *rerror;
+      uschar *subject;
+      uschar *sub[3];
+      int save_expand_nmax =
+        save_expand_strings(save_expand_nstring, save_expand_nlength);
+
+      switch(read_subs(sub, 3, 3, &s, skipping, TRUE, US"sg", &resetok))
+        {
+        case 1: goto EXPAND_FAILED_CURLY;
+        case 2:
+        case 3: goto EXPAND_FAILED;
+        }
+
+      /* Compile the regular expression */
+
+      re = pcre_compile(CS sub[1], PCRE_COPT, (const char **)&rerror, &roffset,
+        NULL);
+
+      if (re == NULL)
+        {
+        expand_string_message = string_sprintf("regular expression error in "
+          "\"%s\": %s at offset %d", sub[1], rerror, roffset);
+        goto EXPAND_FAILED;
+        }
+
+      /* Now run a loop to do the substitutions as often as necessary. It ends
+      when there are no more matches. Take care over matches of the null string;
+      do the same thing as Perl does. */
+
+      subject = sub[0];
+      slen = Ustrlen(sub[0]);
+      moffset = moffsetextra = 0;
+      emptyopt = 0;
+
+      for (;;)
+        {
+        int ovector[3*(EXPAND_MAXN+1)];
+        int n = pcre_exec(re, NULL, CS subject, slen, moffset + moffsetextra,
+          PCRE_EOPT | emptyopt, ovector, sizeof(ovector)/sizeof(int));
+        int nn;
+        uschar *insert;
+
+        /* No match - if we previously set PCRE_NOTEMPTY after a null match, this
+        is not necessarily the end. We want to repeat the match from one
+        character further along, but leaving the basic offset the same (for
+        copying below). We can't be at the end of the string - that was checked
+        before setting PCRE_NOTEMPTY. If PCRE_NOTEMPTY is not set, we are
+        finished; copy the remaining string and end the loop. */
+
+        if (n < 0)
+          {
+          if (emptyopt != 0)
+            {
+            moffsetextra = 1;
+            emptyopt = 0;
+            continue;
+            }
+          yield = string_cat(yield, &size, &ptr, subject+moffset, slen-moffset);
+          break;
+          }
+
+        /* Match - set up for expanding the replacement. */
+
+        if (n == 0) n = EXPAND_MAXN + 1;
+        expand_nmax = 0;
+        for (nn = 0; nn < n*2; nn += 2)
+          {
+          expand_nstring[expand_nmax] = subject + ovector[nn];
+          expand_nlength[expand_nmax++] = ovector[nn+1] - ovector[nn];
+          }
+        expand_nmax--;
+
+        /* Copy the characters before the match, plus the expanded insertion. */
+
+        yield = string_cat(yield, &size, &ptr, subject + moffset,
+          ovector[0] - moffset);
+        insert = expand_string(sub[2]);
+        if (insert == NULL) goto EXPAND_FAILED;
+        yield = string_cat(yield, &size, &ptr, insert, Ustrlen(insert));
+
+        moffset = ovector[1];
+        moffsetextra = 0;
+        emptyopt = 0;
+
+        /* If we have matched an empty string, first check to see if we are at
+        the end of the subject. If so, the loop is over. Otherwise, mimic
+        what Perl's /g options does. This turns out to be rather cunning. First
+        we set PCRE_NOTEMPTY and PCRE_ANCHORED and try the match a non-empty
+        string at the same point. If this fails (picked up above) we advance to
+        the next character. */
+
+        if (ovector[0] == ovector[1])
+          {
+          if (ovector[0] == slen) break;
+          emptyopt = PCRE_NOTEMPTY | PCRE_ANCHORED;
+          }
+        }
+
+      /* All done - restore numerical variables. */
+
+      restore_expand_strings(save_expand_nmax, save_expand_nstring,
+        save_expand_nlength);
+      continue;
+      }
+
+    /* Handle keyed and numbered substring extraction. If the first argument
+    consists entirely of digits, then a numerical extraction is assumed. */
+
+    case EITEM_EXTRACT:
+      {
+      int i;
+      int j = 2;
+      int field_number = 1;
+      BOOL field_number_set = FALSE;
+      uschar *save_lookup_value = lookup_value;
+      uschar *sub[3];
+      int save_expand_nmax =
+        save_expand_strings(save_expand_nstring, save_expand_nlength);
+
+      /* Read the arguments */
+
+      for (i = 0; i < j; i++)
+        {
+        while (isspace(*s)) s++;
+        if (*s == '{')                                                 /*}*/
+          {
+          sub[i] = expand_string_internal(s+1, TRUE, &s, skipping, TRUE, &resetok);
+          if (sub[i] == NULL) goto EXPAND_FAILED;              /*{*/
+          if (*s++ != '}') goto EXPAND_FAILED_CURLY;
+
+          /* After removal of leading and trailing white space, the first
+          argument must not be empty; if it consists entirely of digits
+          (optionally preceded by a minus sign), this is a numerical
+          extraction, and we expect 3 arguments. */
+
+          if (i == 0)
+            {
+            int len;
+            int x = 0;
+            uschar *p = sub[0];
+
+            while (isspace(*p)) p++;
+            sub[0] = p;
+
+            len = Ustrlen(p);
+            while (len > 0 && isspace(p[len-1])) len--;
+            p[len] = 0;
+
+            if (*p == 0 && !skipping)
+              {
+              expand_string_message = US"first argument of \"extract\" must "
+                "not be empty";
+              goto EXPAND_FAILED;
+              }
+
+            if (*p == '-')
+              {
+              field_number = -1;
+              p++;
+              }
+            while (*p != 0 && isdigit(*p)) x = x * 10 + *p++ - '0';
+            if (*p == 0)
+              {
+              field_number *= x;
+              j = 3;               /* Need 3 args */
+              field_number_set = TRUE;
+              }
+            }
+          }
+        else goto EXPAND_FAILED_CURLY;
+        }
+
+      /* Extract either the numbered or the keyed substring into $value. If
+      skipping, just pretend the extraction failed. */
+
+      lookup_value = skipping? NULL : field_number_set?
+        expand_gettokened(field_number, sub[1], sub[2]) :
+        expand_getkeyed(sub[0], sub[1]);
+
+      /* If no string follows, $value gets substituted; otherwise there can
+      be yes/no strings, as for lookup or if. */
+
+      switch(process_yesno(
+               skipping,                     /* were previously skipping */
+               lookup_value != NULL,         /* success/failure indicator */
+               save_lookup_value,            /* value to reset for string2 */
+               &s,                           /* input pointer */
+               &yield,                       /* output pointer */
+               &size,                        /* output size */
+               &ptr,                         /* output current point */
+               US"extract",                  /* condition type */
+              &resetok))
+        {
+        case 1: goto EXPAND_FAILED;          /* when all is well, the */
+        case 2: goto EXPAND_FAILED_CURLY;    /* returned value is 0 */
+        }
+
+      /* All done - restore numerical variables. */
+
+      restore_expand_strings(save_expand_nmax, save_expand_nstring,
+        save_expand_nlength);
+
+      continue;
+      }
+
+    /* return the Nth item from a list */
+
+    case EITEM_LISTEXTRACT:
+      {
+      int i;
+      int field_number = 1;
+      uschar *save_lookup_value = lookup_value;
+      uschar *sub[2];
+      int save_expand_nmax =
+        save_expand_strings(save_expand_nstring, save_expand_nlength);
+
+      /* Read the field & list arguments */
+
+      for (i = 0; i < 2; i++)
+        {
+        while (isspace(*s)) s++;
+        if (*s != '{')                                 /*}*/
+         goto EXPAND_FAILED_CURLY;
+
+       sub[i] = expand_string_internal(s+1, TRUE, &s, skipping, TRUE, &resetok);
+       if (!sub[i])     goto EXPAND_FAILED;            /*{*/
+       if (*s++ != '}') goto EXPAND_FAILED_CURLY;
+
+       /* After removal of leading and trailing white space, the first
+       argument must be numeric and nonempty. */
+
+       if (i == 0)
+         {
+         int len;
+         int x = 0;
+         uschar *p = sub[0];
+
+         while (isspace(*p)) p++;
+         sub[0] = p;
+
+         len = Ustrlen(p);
+         while (len > 0 && isspace(p[len-1])) len--;
+         p[len] = 0;
+
+         if (!*p && !skipping)
+           {
+           expand_string_message = US"first argument of \"listextract\" must "
+             "not be empty";
+           goto EXPAND_FAILED;
+           }
+
+         if (*p == '-')
+           {
+           field_number = -1;
+           p++;
+           }
+         while (*p && isdigit(*p)) x = x * 10 + *p++ - '0';
+         if (*p)
+           {
+           expand_string_message = US"first argument of \"listextract\" must "
+             "be numeric";
+           goto EXPAND_FAILED;
+           }
+         field_number *= x;
+         }
+        }
+
+      /* Extract the numbered element into $value. If
+      skipping, just pretend the extraction failed. */
+
+      lookup_value = skipping? NULL : expand_getlistele(field_number, sub[1]);
+
+      /* If no string follows, $value gets substituted; otherwise there can
+      be yes/no strings, as for lookup or if. */
+
+      switch(process_yesno(
+               skipping,                     /* were previously skipping */
+               lookup_value != NULL,         /* success/failure indicator */
+               save_lookup_value,            /* value to reset for string2 */
+               &s,                           /* input pointer */
+               &yield,                       /* output pointer */
+               &size,                        /* output size */
+               &ptr,                         /* output current point */
+               US"extract",                  /* condition type */
+              &resetok))
+        {
+        case 1: goto EXPAND_FAILED;          /* when all is well, the */
+        case 2: goto EXPAND_FAILED_CURLY;    /* returned value is 0 */
+        }
+
+      /* All done - restore numerical variables. */
+
+      restore_expand_strings(save_expand_nmax, save_expand_nstring,
+        save_expand_nlength);
+
+      continue;
+      }
+
+#ifdef SUPPORT_TLS
+    case EITEM_CERTEXTRACT:
+      {
+      uschar *save_lookup_value = lookup_value;
+      uschar *sub[2];
+      int save_expand_nmax =
+        save_expand_strings(save_expand_nstring, save_expand_nlength);
+
+      /* Read the field argument */
+      while (isspace(*s)) s++;
+      if (*s != '{')                                   /*}*/
+       goto EXPAND_FAILED_CURLY;
+      sub[0] = expand_string_internal(s+1, TRUE, &s, skipping, TRUE, &resetok);
+      if (!sub[0])     goto EXPAND_FAILED;             /*{*/
+      if (*s++ != '}') goto EXPAND_FAILED_CURLY;
+      /* strip spaces fore & aft */
+      {
+      int len;
+      uschar *p = sub[0];
+
+      while (isspace(*p)) p++;
+      sub[0] = p;
+
+      len = Ustrlen(p);
+      while (len > 0 && isspace(p[len-1])) len--;
+      p[len] = 0;
+      }
+
+      /* inspect the cert argument */
+      while (isspace(*s)) s++;
+      if (*s != '{')                                   /*}*/
+       goto EXPAND_FAILED_CURLY;
+      if (*++s != '$')
+        {
+       expand_string_message = US"second argument of \"certextract\" must "
+         "be a certificate variable";
+       goto EXPAND_FAILED;
+       }
+      sub[1] = expand_string_internal(s+1, TRUE, &s, skipping, FALSE, &resetok);
+      if (!sub[1])     goto EXPAND_FAILED;             /*{*/
+      if (*s++ != '}') goto EXPAND_FAILED_CURLY;
+
+      if (skipping)
+       lookup_value = NULL;
+      else
+       {
+       lookup_value = expand_getcertele(sub[0], sub[1]);
+       if (*expand_string_message) goto EXPAND_FAILED;
+       }
+      switch(process_yesno(
+               skipping,                     /* were previously skipping */
+               lookup_value != NULL,         /* success/failure indicator */
+               save_lookup_value,            /* value to reset for string2 */
+               &s,                           /* input pointer */
+               &yield,                       /* output pointer */
+               &size,                        /* output size */
+               &ptr,                         /* output current point */
+               US"extract",                  /* condition type */
+              &resetok))
+        {
+        case 1: goto EXPAND_FAILED;          /* when all is well, the */
+        case 2: goto EXPAND_FAILED_CURLY;    /* returned value is 0 */
+        }
+
+      restore_expand_strings(save_expand_nmax, save_expand_nstring,
+        save_expand_nlength);
+      continue;
+      }
+#endif /*SUPPORT_TLS*/
+
+    /* Handle list operations */
+
+    case EITEM_FILTER:
+    case EITEM_MAP:
+    case EITEM_REDUCE:
+      {
+      int sep = 0;
+      int save_ptr = ptr;
+      uschar outsep[2] = { '\0', '\0' };
+      uschar *list, *expr, *temp;
+      uschar *save_iterate_item = iterate_item;
+      uschar *save_lookup_value = lookup_value;
+
+      while (isspace(*s)) s++;
+      if (*s++ != '{') goto EXPAND_FAILED_CURLY;
+
+      list = expand_string_internal(s, TRUE, &s, skipping, TRUE, &resetok);
+      if (list == NULL) goto EXPAND_FAILED;
+      if (*s++ != '}') goto EXPAND_FAILED_CURLY;
+
+      if (item_type == EITEM_REDUCE)
+        {
+        while (isspace(*s)) s++;
+        if (*s++ != '{') goto EXPAND_FAILED_CURLY;
+        temp = expand_string_internal(s, TRUE, &s, skipping, TRUE, &resetok);
+        if (temp == NULL) goto EXPAND_FAILED;
+        lookup_value = temp;
+        if (*s++ != '}') goto EXPAND_FAILED_CURLY;
+        }
+
+      while (isspace(*s)) s++;
+      if (*s++ != '{') goto EXPAND_FAILED_CURLY;
+
+      expr = s;
+
+      /* For EITEM_FILTER, call eval_condition once, with result discarded (as
+      if scanning a "false" part). This allows us to find the end of the
+      condition, because if the list is empty, we won't actually evaluate the
+      condition for real. For EITEM_MAP and EITEM_REDUCE, do the same, using
+      the normal internal expansion function. */
+
+      if (item_type == EITEM_FILTER)
+        {
+        temp = eval_condition(expr, &resetok, NULL);
+        if (temp != NULL) s = temp;
+        }
+      else
+        {
+        temp = expand_string_internal(s, TRUE, &s, TRUE, TRUE, &resetok);
+        }
+
+      if (temp == NULL)
+        {
+        expand_string_message = string_sprintf("%s inside \"%s\" item",
+          expand_string_message, name);
+        goto EXPAND_FAILED;
+        }
+
+      while (isspace(*s)) s++;
+      if (*s++ != '}')
+        {                                              /*{*/
+        expand_string_message = string_sprintf("missing } at end of condition "
+          "or expression inside \"%s\"", name);
+        goto EXPAND_FAILED;
+        }
+
+      while (isspace(*s)) s++;                         /*{*/
+      if (*s++ != '}')
+        {                                              /*{*/
+        expand_string_message = string_sprintf("missing } at end of \"%s\"",
+          name);
+        goto EXPAND_FAILED;
+        }
+
+      /* If we are skipping, we can now just move on to the next item. When
+      processing for real, we perform the iteration. */
+
+      if (skipping) continue;
+      while ((iterate_item = string_nextinlist(&list, &sep, NULL, 0)) != NULL)
+        {
+        *outsep = (uschar)sep;      /* Separator as a string */
+
+        DEBUG(D_expand) debug_printf("%s: $item = \"%s\"\n", name, iterate_item);
+
+        if (item_type == EITEM_FILTER)
+          {
+          BOOL condresult;
+          if (eval_condition(expr, &resetok, &condresult) == NULL)
+            {
+            iterate_item = save_iterate_item;
+            lookup_value = save_lookup_value;
+            expand_string_message = string_sprintf("%s inside \"%s\" condition",
+              expand_string_message, name);
+            goto EXPAND_FAILED;
+            }
+          DEBUG(D_expand) debug_printf("%s: condition is %s\n", name,
+            condresult? "true":"false");
+          if (condresult)
+            temp = iterate_item;    /* TRUE => include this item */
+          else
+            continue;               /* FALSE => skip this item */
+          }
+
+        /* EITEM_MAP and EITEM_REDUCE */
+
+        else
+          {
+          temp = expand_string_internal(expr, TRUE, NULL, skipping, TRUE, &resetok);
+          if (temp == NULL)
+            {
+            iterate_item = save_iterate_item;
+            expand_string_message = string_sprintf("%s inside \"%s\" item",
+              expand_string_message, name);
+            goto EXPAND_FAILED;
+            }
+          if (item_type == EITEM_REDUCE)
+            {
+            lookup_value = temp;      /* Update the value of $value */
+            continue;                 /* and continue the iteration */
+            }
+          }
+
+        /* We reach here for FILTER if the condition is true, always for MAP,
+        and never for REDUCE. The value in "temp" is to be added to the output
+        list that is being created, ensuring that any occurrences of the
+        separator character are doubled. Unless we are dealing with the first
+        item of the output list, add in a space if the new item begins with the
+        separator character, or is an empty string. */
+
+        if (ptr != save_ptr && (temp[0] == *outsep || temp[0] == 0))
+          yield = string_cat(yield, &size, &ptr, US" ", 1);
+
+        /* Add the string in "temp" to the output list that we are building,
+        This is done in chunks by searching for the separator character. */
+
+        for (;;)
+          {
+          size_t seglen = Ustrcspn(temp, outsep);
+            yield = string_cat(yield, &size, &ptr, temp, seglen + 1);
+
+          /* If we got to the end of the string we output one character
+          too many; backup and end the loop. Otherwise arrange to double the
+          separator. */
+
+          if (temp[seglen] == '\0') { ptr--; break; }
+          yield = string_cat(yield, &size, &ptr, outsep, 1);
+          temp += seglen + 1;
+          }
+
+        /* Output a separator after the string: we will remove the redundant
+        final one at the end. */
+
+        yield = string_cat(yield, &size, &ptr, outsep, 1);
+        }   /* End of iteration over the list loop */
+
+      /* REDUCE has generated no output above: output the final value of
+      $value. */
+
+      if (item_type == EITEM_REDUCE)
+        {
+        yield = string_cat(yield, &size, &ptr, lookup_value,
+          Ustrlen(lookup_value));
+        lookup_value = save_lookup_value;  /* Restore $value */
+        }
+
+      /* FILTER and MAP generate lists: if they have generated anything, remove
+      the redundant final separator. Even though an empty item at the end of a
+      list does not count, this is tidier. */
+
+      else if (ptr != save_ptr) ptr--;
+
+      /* Restore preserved $item */
+
+      iterate_item = save_iterate_item;
+      continue;
+      }
+
+
+    /* If ${dlfunc } support is configured, handle calling dynamically-loaded
+    functions, unless locked out at this time. Syntax is ${dlfunc{file}{func}}
+    or ${dlfunc{file}{func}{arg}} or ${dlfunc{file}{func}{arg1}{arg2}} or up to
+    a maximum of EXPAND_DLFUNC_MAX_ARGS arguments (defined below). */
+
+    #define EXPAND_DLFUNC_MAX_ARGS 8
+
+    case EITEM_DLFUNC:
+    #ifndef EXPAND_DLFUNC
+    expand_string_message = US"\"${dlfunc\" encountered, but this facility "   /*}*/
+      "is not included in this binary";
+    goto EXPAND_FAILED;
+
+    #else   /* EXPAND_DLFUNC */
+      {
+      tree_node *t;
+      exim_dlfunc_t *func;
+      uschar *result;
+      int status, argc;
+      uschar *argv[EXPAND_DLFUNC_MAX_ARGS + 3];
+
+      if ((expand_forbid & RDO_DLFUNC) != 0)
+        {
+        expand_string_message =
+          US"dynamically-loaded functions are not permitted";
+        goto EXPAND_FAILED;
+        }
+
+      switch(read_subs(argv, EXPAND_DLFUNC_MAX_ARGS + 2, 2, &s, skipping,
+           TRUE, US"dlfunc", &resetok))
+        {
+        case 1: goto EXPAND_FAILED_CURLY;
+        case 2:
+        case 3: goto EXPAND_FAILED;
+        }
+
+      /* If skipping, we don't actually do anything */
+
+      if (skipping) continue;
+
+      /* Look up the dynamically loaded object handle in the tree. If it isn't
+      found, dlopen() the file and put the handle in the tree for next time. */
+
+      t = tree_search(dlobj_anchor, argv[0]);
+      if (t == NULL)
+        {
+        void *handle = dlopen(CS argv[0], RTLD_LAZY);
+        if (handle == NULL)
+          {
+          expand_string_message = string_sprintf("dlopen \"%s\" failed: %s",
+            argv[0], dlerror());
+          log_write(0, LOG_MAIN|LOG_PANIC, "%s", expand_string_message);
+          goto EXPAND_FAILED;
+          }
+        t = store_get_perm(sizeof(tree_node) + Ustrlen(argv[0]));
+        Ustrcpy(t->name, argv[0]);
+        t->data.ptr = handle;
+        (void)tree_insertnode(&dlobj_anchor, t);
+        }
+
+      /* Having obtained the dynamically loaded object handle, look up the
+      function pointer. */
+
+      func = (exim_dlfunc_t *)dlsym(t->data.ptr, CS argv[1]);
+      if (func == NULL)
+        {
+        expand_string_message = string_sprintf("dlsym \"%s\" in \"%s\" failed: "
+          "%s", argv[1], argv[0], dlerror());
+        log_write(0, LOG_MAIN|LOG_PANIC, "%s", expand_string_message);
+        goto EXPAND_FAILED;
+        }
+
+      /* Call the function and work out what to do with the result. If it
+      returns OK, we have a replacement string; if it returns DEFER then
+      expansion has failed in a non-forced manner; if it returns FAIL then
+      failure was forced; if it returns ERROR or any other value there's a
+      problem, so panic slightly. In any case, assume that the function has
+      side-effects on the store that must be preserved. */
+
+      resetok = FALSE;
+      result = NULL;
+      for (argc = 0; argv[argc] != NULL; argc++);
+      status = func(&result, argc - 2, &argv[2]);
+      if(status == OK)
+        {
+        if (result == NULL) result = US"";
+        yield = string_cat(yield, &size, &ptr, result, Ustrlen(result));
+        continue;
+        }
+      else
+        {
+        expand_string_message = result == NULL ? US"(no message)" : result;
+        if(status == FAIL_FORCED) expand_string_forcedfail = TRUE;
+          else if(status != FAIL)
+            log_write(0, LOG_MAIN|LOG_PANIC, "dlfunc{%s}{%s} failed (%d): %s",
+              argv[0], argv[1], status, expand_string_message);
+        goto EXPAND_FAILED;
+        }
+      }
+    #endif /* EXPAND_DLFUNC */
+    }  /* EITEM_* switch */
+
+  /* Control reaches here if the name is not recognized as one of the more
+  complicated expansion items. Check for the "operator" syntax (name terminated
+  by a colon). Some of the operators have arguments, separated by _ from the
+  name. */
+
+  if (*s == ':')
+    {
+    int c;
+    uschar *arg = NULL;
+    uschar *sub;
+    var_entry *vp = NULL;
+
+    /* Owing to an historical mis-design, an underscore may be part of the
+    operator name, or it may introduce arguments.  We therefore first scan the
+    table of names that contain underscores. If there is no match, we cut off
+    the arguments and then scan the main table. */
+
+    if ((c = chop_match(name, op_table_underscore,
+       sizeof(op_table_underscore)/sizeof(uschar *))) < 0)
+      {
+      arg = Ustrchr(name, '_');
+      if (arg != NULL) *arg = 0;
+      c = chop_match(name, op_table_main,
+        sizeof(op_table_main)/sizeof(uschar *));
+      if (c >= 0) c += sizeof(op_table_underscore)/sizeof(uschar *);
+      if (arg != NULL) *arg++ = '_';   /* Put back for error messages */
+      }
+
+    /* Deal specially with operators that might take a certificate variable
+    as we do not want to do the usual expansion. For most, expand the string.*/
+    switch(c)
+      {
+#ifdef SUPPORT_TLS
+      case EOP_MD5:
+      case EOP_SHA1:
+      case EOP_SHA256:
+       if (s[1] == '$')
+         {
+         uschar * s1 = s;
+         sub = expand_string_internal(s+2, TRUE, &s1, skipping,
+                 FALSE, &resetok);
+         if (!sub)       goto EXPAND_FAILED;           /*{*/
+         if (*s1 != '}') goto EXPAND_FAILED_CURLY;
+         if ((vp = find_var_ent(sub)) && vp->type == vtype_cert)
+           {
+           s = s1+1;
+           break;
+           }
+         vp = NULL;
+         }
+        /*FALLTHROUGH*/
+#endif
+      default:
+       sub = expand_string_internal(s+1, TRUE, &s, skipping, TRUE, &resetok);
+       if (!sub) goto EXPAND_FAILED;
+       s++;
+       break;
+      }
+
+    /* If we are skipping, we don't need to perform the operation at all.
+    This matters for operations like "mask", because the data may not be
+    in the correct format when skipping. For example, the expression may test
+    for the existence of $sender_host_address before trying to mask it. For
+    other operations, doing them may not fail, but it is a waste of time. */
+
+    if (skipping && c >= 0) continue;
+
+    /* Otherwise, switch on the operator type */
+
+    switch(c)
+      {
+      case EOP_BASE62:
+        {
+        uschar *t;
+        unsigned long int n = Ustrtoul(sub, &t, 10);
+        if (*t != 0)
+          {
+          expand_string_message = string_sprintf("argument for base62 "
+            "operator is \"%s\", which is not a decimal number", sub);
+          goto EXPAND_FAILED;
+          }
+        t = string_base62(n);
+        yield = string_cat(yield, &size, &ptr, t, Ustrlen(t));
+        continue;
+        }
+
+      /* Note that for Darwin and Cygwin, BASE_62 actually has the value 36 */
+
+      case EOP_BASE62D:
+        {
+        uschar buf[16];
+        uschar *tt = sub;
+        unsigned long int n = 0;
+        while (*tt != 0)
+          {
+          uschar *t = Ustrchr(base62_chars, *tt++);
+          if (t == NULL)
+            {
+            expand_string_message = string_sprintf("argument for base62d "
+              "operator is \"%s\", which is not a base %d number", sub,
+              BASE_62);
+            goto EXPAND_FAILED;
+            }
+          n = n * BASE_62 + (t - base62_chars);
+          }
+        (void)sprintf(CS buf, "%ld", n);
+        yield = string_cat(yield, &size, &ptr, buf, Ustrlen(buf));
+        continue;
+        }
+
+      case EOP_EXPAND:
+        {
+        uschar *expanded = expand_string_internal(sub, FALSE, NULL, skipping, TRUE, &resetok);
+        if (expanded == NULL)
+          {
+          expand_string_message =
+            string_sprintf("internal expansion of \"%s\" failed: %s", sub,
+              expand_string_message);
+          goto EXPAND_FAILED;
+          }
+        yield = string_cat(yield, &size, &ptr, expanded, Ustrlen(expanded));
+        continue;
+        }
+
+      case EOP_LC:
+        {
+        int count = 0;
+        uschar *t = sub - 1;
+        while (*(++t) != 0) { *t = tolower(*t); count++; }
+        yield = string_cat(yield, &size, &ptr, sub, count);
+        continue;
+        }
+
+      case EOP_UC:
+        {
+        int count = 0;
+        uschar *t = sub - 1;
+        while (*(++t) != 0) { *t = toupper(*t); count++; }
+        yield = string_cat(yield, &size, &ptr, sub, count);
+        continue;
+        }
+
+      case EOP_MD5:
+#ifdef SUPPORT_TLS
+       if (vp && *(void **)vp->value)
+         {
+         uschar * cp = tls_cert_fprt_md5(*(void **)vp->value);
+         yield = string_cat(yield, &size, &ptr, cp, Ustrlen(cp));
+         }
+       else
+#endif
+         {
+         md5 base;
+         uschar digest[16];
+         int j;
+         char st[33];
+         md5_start(&base);
+         md5_end(&base, sub, Ustrlen(sub), digest);
+         for(j = 0; j < 16; j++) sprintf(st+2*j, "%02x", digest[j]);
+         yield = string_cat(yield, &size, &ptr, US st, (int)strlen(st));
+         }
+        continue;
+
+      case EOP_SHA1:
+#ifdef SUPPORT_TLS
+       if (vp && *(void **)vp->value)
+         {
+         uschar * cp = tls_cert_fprt_sha1(*(void **)vp->value);
+         yield = string_cat(yield, &size, &ptr, cp, Ustrlen(cp));
+         }
+       else
+#endif
+         {
+         sha1 base;
+         uschar digest[20];
+         int j;
+         char st[41];
+         sha1_start(&base);
+         sha1_end(&base, sub, Ustrlen(sub), digest);
+         for(j = 0; j < 20; j++) sprintf(st+2*j, "%02X", digest[j]);
+         yield = string_cat(yield, &size, &ptr, US st, (int)strlen(st));
+         }
+        continue;
+
+      case EOP_SHA256:
+#ifdef SUPPORT_TLS
+       if (vp && *(void **)vp->value)
+         {
+         uschar * cp = tls_cert_fprt_sha256(*(void **)vp->value);
+         yield = string_cat(yield, &size, &ptr, cp, (int)Ustrlen(cp));
+         }
+       else
+#endif
+         expand_string_message = US"sha256 only supported for certificates";
+        continue;
+
+      /* Convert hex encoding to base64 encoding */
+
+      case EOP_HEX2B64:
+        {
+        int c = 0;
+        int b = -1;
+        uschar *in = sub;
+        uschar *out = sub;
+        uschar *enc;
+
+        for (enc = sub; *enc != 0; enc++)
+          {
+          if (!isxdigit(*enc))
+            {
+            expand_string_message = string_sprintf("\"%s\" is not a hex "
+              "string", sub);
+            goto EXPAND_FAILED;
+            }
+          c++;
+          }
+
+        if ((c & 1) != 0)
+          {
+          expand_string_message = string_sprintf("\"%s\" contains an odd "
+            "number of characters", sub);
+          goto EXPAND_FAILED;
+          }
+
+        while ((c = *in++) != 0)
+          {
+          if (isdigit(c)) c -= '0';
+          else c = toupper(c) - 'A' + 10;
+          if (b == -1)
+            {
+            b = c << 4;
+            }
+          else
+            {
+            *out++ = b | c;
+            b = -1;
+            }
+          }
+
+        enc = auth_b64encode(sub, out - sub);
+        yield = string_cat(yield, &size, &ptr, enc, Ustrlen(enc));
+        continue;
+        }
+
+      /* Convert octets outside 0x21..0x7E to \xXX form */
+
+      case EOP_HEXQUOTE:
+       {
+        uschar *t = sub - 1;
+        while (*(++t) != 0)
+          {
+          if (*t < 0x21 || 0x7E < *t)
+            yield = string_cat(yield, &size, &ptr,
+             string_sprintf("\\x%02x", *t), 4);
+         else
+           yield = string_cat(yield, &size, &ptr, t, 1);
+          }
+       continue;
+       }
+
+      /* count the number of list elements */
+
+      case EOP_LISTCOUNT:
+        {
+       int cnt = 0;
+       int sep = 0;
+       uschar * cp;
+       uschar buffer[256];
+
+       while (string_nextinlist(&sub, &sep, buffer, sizeof(buffer)) != NULL) cnt++;
+       cp = string_sprintf("%d", cnt);
+        yield = string_cat(yield, &size, &ptr, cp, Ustrlen(cp));
+        continue;
+        }
+
+      /* expand a named list given the name */
+      /* handles nested named lists; requotes as colon-sep list */
+
+      case EOP_LISTNAMED:
+       {
+       tree_node *t = NULL;
+       uschar * list;
+       int sep = 0;
+       uschar * item;
+       uschar * suffix = US"";
+       BOOL needsep = FALSE;
+       uschar buffer[256];
+
+       if (*sub == '+') sub++;
+       if (arg == NULL)        /* no-argument version */
+         {
+         if (!(t = tree_search(addresslist_anchor, sub)) &&
+             !(t = tree_search(domainlist_anchor,  sub)) &&
+             !(t = tree_search(hostlist_anchor,    sub)))
+           t = tree_search(localpartlist_anchor, sub);
+         }
+       else switch(*arg)       /* specific list-type version */
+         {
+         case 'a': t = tree_search(addresslist_anchor,   sub); suffix = US"_a"; break;
+         case 'd': t = tree_search(domainlist_anchor,    sub); suffix = US"_d"; break;
+         case 'h': t = tree_search(hostlist_anchor,      sub); suffix = US"_h"; break;
+         case 'l': t = tree_search(localpartlist_anchor, sub); suffix = US"_l"; break;
+         default:
+            expand_string_message = string_sprintf("bad suffix on \"list\" operator");
+           goto EXPAND_FAILED;
+         }
+
+       if(!t)
+         {
+          expand_string_message = string_sprintf("\"%s\" is not a %snamed list",
+            sub, !arg?""
+             : *arg=='a'?"address "
+             : *arg=='d'?"domain "
+             : *arg=='h'?"host "
+             : *arg=='l'?"localpart "
+             : 0);
+         goto EXPAND_FAILED;
+         }
+
+       list = ((namedlist_block *)(t->data.ptr))->string;
+
+       while ((item = string_nextinlist(&list, &sep, buffer, sizeof(buffer))) != NULL)
+         {
+         uschar * buf = US" : ";
+         if (needsep)
+           yield = string_cat(yield, &size, &ptr, buf, 3);
+         else
+           needsep = TRUE;
+
+         if (*item == '+')     /* list item is itself a named list */
+           {
+           uschar * sub = string_sprintf("${listnamed%s:%s}", suffix, item);
+           item = expand_string_internal(sub, FALSE, NULL, FALSE, TRUE, &resetok);
+           }
+         else if (sep != ':')  /* item from non-colon-sep list, re-quote for colon list-separator */
+           {
+           char * cp;
+           char tok[3];
+           tok[0] = sep; tok[1] = ':'; tok[2] = 0;
+           while ((cp= strpbrk((const char *)item, tok)))
+             {
+              yield = string_cat(yield, &size, &ptr, item, cp-(char *)item);
+             if (*cp++ == ':') /* colon in a non-colon-sep list item, needs doubling */
+               {
+                yield = string_cat(yield, &size, &ptr, US"::", 2);
+               item = (uschar *)cp;
+               }
+             else              /* sep in item; should already be doubled; emit once */
+               {
+                yield = string_cat(yield, &size, &ptr, (uschar *)tok, 1);
+               if (*cp == sep) cp++;
+               item = (uschar *)cp;
+               }
+             }
+           }
+          yield = string_cat(yield, &size, &ptr, item, Ustrlen(item));
+         }
+        continue;
+       }
+
+      /* mask applies a mask to an IP address; for example the result of
+      ${mask:131.111.10.206/28} is 131.111.10.192/28. */
+
+      case EOP_MASK:
+        {
+        int count;
+        uschar *endptr;
+        int binary[4];
+        int mask, maskoffset;
+        int type = string_is_ip_address(sub, &maskoffset);
+        uschar buffer[64];
+
+        if (type == 0)
+          {
+          expand_string_message = string_sprintf("\"%s\" is not an IP address",
+           sub);
+          goto EXPAND_FAILED;
+          }
+
+        if (maskoffset == 0)
+          {
+          expand_string_message = string_sprintf("missing mask value in \"%s\"",
+            sub);
+          goto EXPAND_FAILED;
+          }
+
+        mask = Ustrtol(sub + maskoffset + 1, &endptr, 10);
+
+        if (*endptr != 0 || mask < 0 || mask > ((type == 4)? 32 : 128))
+          {
+          expand_string_message = string_sprintf("mask value too big in \"%s\"",
+            sub);
+          goto EXPAND_FAILED;
+          }
+
+        /* Convert the address to binary integer(s) and apply the mask */
+
+        sub[maskoffset] = 0;
+        count = host_aton(sub, binary);
+        host_mask(count, binary, mask);
+
+        /* Convert to masked textual format and add to output. */
+
+        yield = string_cat(yield, &size, &ptr, buffer,
+          host_nmtoa(count, binary, mask, buffer, '.'));
+        continue;
+        }
+
+      case EOP_ADDRESS:
+      case EOP_LOCAL_PART:
+      case EOP_DOMAIN:
+        {
+        uschar *error;
+        int start, end, domain;
+        uschar *t = parse_extract_address(sub, &error, &start, &end, &domain,
+          FALSE);
+        if (t != NULL)
+          {
+          if (c != EOP_DOMAIN)
+            {
+            if (c == EOP_LOCAL_PART && domain != 0) end = start + domain - 1;
+            yield = string_cat(yield, &size, &ptr, sub+start, end-start);
+            }
+          else if (domain != 0)
+            {
+            domain += start;
+            yield = string_cat(yield, &size, &ptr, sub+domain, end-domain);
+            }
+          }
+        continue;
+        }
+
+      case EOP_ADDRESSES:
+        {
+        uschar outsep[2] = { ':', '\0' };
+        uschar *address, *error;
+        int save_ptr = ptr;
+        int start, end, domain;  /* Not really used */
+
+        while (isspace(*sub)) sub++;
+        if (*sub == '>') { *outsep = *++sub; ++sub; }
+        parse_allow_group = TRUE;
+
+        for (;;)
+          {
+          uschar *p = parse_find_address_end(sub, FALSE);
+          uschar saveend = *p;
+          *p = '\0';
+          address = parse_extract_address(sub, &error, &start, &end, &domain,
+            FALSE);
+          *p = saveend;
+
+          /* Add the address to the output list that we are building. This is
+          done in chunks by searching for the separator character. At the
+          start, unless we are dealing with the first address of the output
+          list, add in a space if the new address begins with the separator
+          character, or is an empty string. */
+
+          if (address != NULL)
+            {
+            if (ptr != save_ptr && address[0] == *outsep)
+              yield = string_cat(yield, &size, &ptr, US" ", 1);
+
+            for (;;)
+              {
+              size_t seglen = Ustrcspn(address, outsep);
+              yield = string_cat(yield, &size, &ptr, address, seglen + 1);
+
+              /* If we got to the end of the string we output one character
+              too many. */
+
+              if (address[seglen] == '\0') { ptr--; break; }
+              yield = string_cat(yield, &size, &ptr, outsep, 1);
+              address += seglen + 1;
+              }
+
+            /* Output a separator after the string: we will remove the
+            redundant final one at the end. */
+
+            yield = string_cat(yield, &size, &ptr, outsep, 1);
+            }
+
+          if (saveend == '\0') break;
+          sub = p + 1;
+          }
+
+        /* If we have generated anything, remove the redundant final
+        separator. */
+
+        if (ptr != save_ptr) ptr--;
+        parse_allow_group = FALSE;
+        continue;
+        }
+
+
+      /* quote puts a string in quotes if it is empty or contains anything
+      other than alphamerics, underscore, dot, or hyphen.
+
+      quote_local_part puts a string in quotes if RFC 2821/2822 requires it to
+      be quoted in order to be a valid local part.
+
+      In both cases, newlines and carriage returns are converted into \n and \r
+      respectively */
+
+      case EOP_QUOTE:
+      case EOP_QUOTE_LOCAL_PART:
+      if (arg == NULL)
+        {
+        BOOL needs_quote = (*sub == 0);      /* TRUE for empty string */
+        uschar *t = sub - 1;
+
+        if (c == EOP_QUOTE)
+          {
+          while (!needs_quote && *(++t) != 0)
+            needs_quote = !isalnum(*t) && !strchr("_-.", *t);
+          }
+        else  /* EOP_QUOTE_LOCAL_PART */
+          {
+          while (!needs_quote && *(++t) != 0)
+            needs_quote = !isalnum(*t) &&
+              strchr("!#$%&'*+-/=?^_`{|}~", *t) == NULL &&
+              (*t != '.' || t == sub || t[1] == 0);
+          }
+
+        if (needs_quote)
+          {
+          yield = string_cat(yield, &size, &ptr, US"\"", 1);
+          t = sub - 1;
+          while (*(++t) != 0)
+            {
+            if (*t == '\n')
+              yield = string_cat(yield, &size, &ptr, US"\\n", 2);
+            else if (*t == '\r')
+              yield = string_cat(yield, &size, &ptr, US"\\r", 2);
+            else
+              {
+              if (*t == '\\' || *t == '"')
+                yield = string_cat(yield, &size, &ptr, US"\\", 1);
+              yield = string_cat(yield, &size, &ptr, t, 1);
+              }
+            }
+          yield = string_cat(yield, &size, &ptr, US"\"", 1);
+          }
+        else yield = string_cat(yield, &size, &ptr, sub, Ustrlen(sub));
+        continue;
+        }
+
+      /* quote_lookuptype does lookup-specific quoting */
+
+      else
+        {
+        int n;
+        uschar *opt = Ustrchr(arg, '_');
+
+        if (opt != NULL) *opt++ = 0;
+
+        n = search_findtype(arg, Ustrlen(arg));
+        if (n < 0)
+          {
+          expand_string_message = search_error_message;
+          goto EXPAND_FAILED;
+          }
+
+        if (lookup_list[n]->quote != NULL)
+          sub = (lookup_list[n]->quote)(sub, opt);
+        else if (opt != NULL) sub = NULL;
+
+        if (sub == NULL)
+          {
+          expand_string_message = string_sprintf(
+            "\"%s\" unrecognized after \"${quote_%s\"",
+            opt, arg);
+          goto EXPAND_FAILED;
+          }
+
+        yield = string_cat(yield, &size, &ptr, sub, Ustrlen(sub));
+        continue;
+        }
+
+      /* rx quote sticks in \ before any non-alphameric character so that
+      the insertion works in a regular expression. */
+
+      case EOP_RXQUOTE:
+        {
+        uschar *t = sub - 1;
+        while (*(++t) != 0)
+          {
+          if (!isalnum(*t))
+            yield = string_cat(yield, &size, &ptr, US"\\", 1);
+          yield = string_cat(yield, &size, &ptr, t, 1);
+          }
+        continue;
+        }
+
+      /* RFC 2047 encodes, assuming headers_charset (default ISO 8859-1) as
+      prescribed by the RFC, if there are characters that need to be encoded */
+
+      case EOP_RFC2047:
+        {
+        uschar buffer[2048];
+        uschar *string = parse_quote_2047(sub, Ustrlen(sub), headers_charset,
+          buffer, sizeof(buffer), FALSE);
+        yield = string_cat(yield, &size, &ptr, string, Ustrlen(string));
+        continue;
+        }
+
+      /* RFC 2047 decode */
+
+      case EOP_RFC2047D:
+        {
+        int len;
+        uschar *error;
+        uschar *decoded = rfc2047_decode(sub, check_rfc2047_length,
+          headers_charset, '?', &len, &error);
+        if (error != NULL)
+          {
+          expand_string_message = error;
+          goto EXPAND_FAILED;
+          }
+        yield = string_cat(yield, &size, &ptr, decoded, len);
+        continue;
+        }
+
+      /* from_utf8 converts UTF-8 to 8859-1, turning non-existent chars into
+      underscores */
+
+      case EOP_FROM_UTF8:
+        {
+        while (*sub != 0)
+          {
+          int c;
+          uschar buff[4];
+          GETUTF8INC(c, sub);
+          if (c > 255) c = '_';
+          buff[0] = c;
+          yield = string_cat(yield, &size, &ptr, buff, 1);
+          }
+        continue;
+        }
+
+         /* replace illegal UTF-8 sequences by replacement character  */
+         
+      #define UTF8_REPLACEMENT_CHAR US"?"
+
+      case EOP_UTF8CLEAN:
+        {
+        int seq_len, index = 0;
+        int bytes_left = 0;
+        uschar seq_buff[4];                    /* accumulate utf-8 here */
+        
+        while (*sub != 0)
+         {
+         int complete;
+         long codepoint;
+         uschar c;
+
+         complete = 0;
+         c = *sub++;
+         if (bytes_left)
+           {
+           if ((c & 0xc0) != 0x80)
+             {
+                   /* wrong continuation byte; invalidate all bytes */
+             complete = 1; /* error */
+             }
+           else
+             {
+             codepoint = (codepoint << 6) | (c & 0x3f);
+             seq_buff[index++] = c;
+             if (--bytes_left == 0)            /* codepoint complete */
+               {
+               if(codepoint > 0x10FFFF)        /* is it too large? */
+                 complete = -1;        /* error */
+               else
+                 {             /* finished; output utf-8 sequence */
+                 yield = string_cat(yield, &size, &ptr, seq_buff, seq_len);
+                 index = 0;
+                 }
+               }
+             }
+           }
+         else  /* no bytes left: new sequence */
+           {
+           if((c & 0x80) == 0) /* 1-byte sequence, US-ASCII, keep it */
+             {
+             yield = string_cat(yield, &size, &ptr, &c, 1);
+             continue;
+             }
+           if((c & 0xe0) == 0xc0)              /* 2-byte sequence */
+             {
+             if(c == 0xc0 || c == 0xc1)        /* 0xc0 and 0xc1 are illegal */
+               complete = -1;
+             else
+               {
+                 bytes_left = 1;
+                 codepoint = c & 0x1f;
+               }
+             }
+           else if((c & 0xf0) == 0xe0)         /* 3-byte sequence */
+             {
+             bytes_left = 2;
+             codepoint = c & 0x0f;
+             }
+           else if((c & 0xf8) == 0xf0)         /* 4-byte sequence */
+             {
+             bytes_left = 3;
+             codepoint = c & 0x07;
+             }
+           else        /* invalid or too long (RFC3629 allows only 4 bytes) */
+             complete = -1;
+
+           seq_buff[index++] = c;
+           seq_len = bytes_left + 1;
+           }           /* if(bytes_left) */
+
+         if (complete != 0)
+           {
+           bytes_left = index = 0;
+           yield = string_cat(yield, &size, &ptr, UTF8_REPLACEMENT_CHAR, 1);
+           }
+         if ((complete == 1) && ((c & 0x80) == 0))
+           { /* ASCII character follows incomplete sequence */
+             yield = string_cat(yield, &size, &ptr, &c, 1);
+           }
+         }
+        continue;
+        }
+
+      /* escape turns all non-printing characters into escape sequences. */
+
+      case EOP_ESCAPE:
+        {
+        uschar *t = string_printing(sub);
+        yield = string_cat(yield, &size, &ptr, t, Ustrlen(t));
+        continue;
+        }
+
+      /* Handle numeric expression evaluation */
+
+      case EOP_EVAL:
+      case EOP_EVAL10:
+        {
+        uschar *save_sub = sub;
+        uschar *error = NULL;
+        int_eximarith_t n = eval_expr(&sub, (c == EOP_EVAL10), &error, FALSE);
+        if (error != NULL)
+          {
+          expand_string_message = string_sprintf("error in expression "
+            "evaluation: %s (after processing \"%.*s\")", error, sub-save_sub,
+              save_sub);
+          goto EXPAND_FAILED;
+          }
+        sprintf(CS var_buffer, PR_EXIM_ARITH, n);
+        yield = string_cat(yield, &size, &ptr, var_buffer, Ustrlen(var_buffer));
+        continue;
+        }
+
+      /* Handle time period formating */
+
+      case EOP_TIME_EVAL:
+        {
+        int n = readconf_readtime(sub, 0, FALSE);
+        if (n < 0)
+          {
+          expand_string_message = string_sprintf("string \"%s\" is not an "
+            "Exim time interval in \"%s\" operator", sub, name);
+          goto EXPAND_FAILED;
+          }
+        sprintf(CS var_buffer, "%d", n);
+        yield = string_cat(yield, &size, &ptr, var_buffer, Ustrlen(var_buffer));
+        continue;
+        }
+
+      case EOP_TIME_INTERVAL:
+        {
+        int n;
+        uschar *t = read_number(&n, sub);
+        if (*t != 0) /* Not A Number*/
+          {
+          expand_string_message = string_sprintf("string \"%s\" is not a "
+            "positive number in \"%s\" operator", sub, name);
+          goto EXPAND_FAILED;
+          }
+        t = readconf_printtime(n);
+        yield = string_cat(yield, &size, &ptr, t, Ustrlen(t));
+        continue;
+        }
+
+      /* Convert string to base64 encoding */
+
+      case EOP_STR2B64:
+        {
+        uschar *encstr = auth_b64encode(sub, Ustrlen(sub));
+        yield = string_cat(yield, &size, &ptr, encstr, Ustrlen(encstr));
+        continue;
+        }
+
+      /* strlen returns the length of the string */
+
+      case EOP_STRLEN:
+        {
+        uschar buff[24];
+        (void)sprintf(CS buff, "%d", Ustrlen(sub));
+        yield = string_cat(yield, &size, &ptr, buff, Ustrlen(buff));
+        continue;
+        }
+
+      /* length_n or l_n takes just the first n characters or the whole string,
+      whichever is the shorter;
+
+      substr_m_n, and s_m_n take n characters from offset m; negative m take
+      from the end; l_n is synonymous with s_0_n. If n is omitted in substr it
+      takes the rest, either to the right or to the left.
+
+      hash_n or h_n makes a hash of length n from the string, yielding n
+      characters from the set a-z; hash_n_m makes a hash of length n, but
+      uses m characters from the set a-zA-Z0-9.
+
+      nhash_n returns a single number between 0 and n-1 (in text form), while
+      nhash_n_m returns a div/mod hash as two numbers "a/b". The first lies
+      between 0 and n-1 and the second between 0 and m-1. */
+
+      case EOP_LENGTH:
+      case EOP_L:
+      case EOP_SUBSTR:
+      case EOP_S:
+      case EOP_HASH:
+      case EOP_H:
+      case EOP_NHASH:
+      case EOP_NH:
+        {
+        int sign = 1;
+        int value1 = 0;
+        int value2 = -1;
+        int *pn;
+        int len;
+        uschar *ret;
+
+        if (arg == NULL)
+          {
+          expand_string_message = string_sprintf("missing values after %s",
+            name);
+          goto EXPAND_FAILED;
+          }
+
+        /* "length" has only one argument, effectively being synonymous with
+        substr_0_n. */
+
+        if (c == EOP_LENGTH || c == EOP_L)
+          {
+          pn = &value2;
+          value2 = 0;
+          }
+
+        /* The others have one or two arguments; for "substr" the first may be
+        negative. The second being negative means "not supplied". */
+
+        else
+          {
+          pn = &value1;
+          if (name[0] == 's' && *arg == '-') { sign = -1; arg++; }
+          }
+
+        /* Read up to two numbers, separated by underscores */
+
+        ret = arg;
+        while (*arg != 0)
+          {
+          if (arg != ret && *arg == '_' && pn == &value1)
+            {
+            pn = &value2;
+            value2 = 0;
+            if (arg[1] != 0) arg++;
+            }
+          else if (!isdigit(*arg))
+            {
+            expand_string_message =
+              string_sprintf("non-digit after underscore in \"%s\"", name);
+            goto EXPAND_FAILED;
+            }
+          else *pn = (*pn)*10 + *arg++ - '0';
+          }
+        value1 *= sign;
+
+        /* Perform the required operation */
+
+        ret =
+          (c == EOP_HASH || c == EOP_H)?
+             compute_hash(sub, value1, value2, &len) :
+          (c == EOP_NHASH || c == EOP_NH)?
+             compute_nhash(sub, value1, value2, &len) :
+             extract_substr(sub, value1, value2, &len);
+
+        if (ret == NULL) goto EXPAND_FAILED;
+        yield = string_cat(yield, &size, &ptr, ret, len);
+        continue;
+        }
+
+      /* Stat a path */
+
+      case EOP_STAT:
+        {
+        uschar *s;
+        uschar smode[12];
+        uschar **modetable[3];
+        int i;
+        mode_t mode;
+        struct stat st;
+
+        if ((expand_forbid & RDO_EXISTS) != 0)
+          {
+          expand_string_message = US"Use of the stat() expansion is not permitted";
+          goto EXPAND_FAILED;
+          }
+
+        if (stat(CS sub, &st) < 0)
+          {
+          expand_string_message = string_sprintf("stat(%s) failed: %s",
+            sub, strerror(errno));
+          goto EXPAND_FAILED;
+          }
+        mode = st.st_mode;
+        switch (mode & S_IFMT)
+          {
+          case S_IFIFO: smode[0] = 'p'; break;
+          case S_IFCHR: smode[0] = 'c'; break;
+          case S_IFDIR: smode[0] = 'd'; break;
+          case S_IFBLK: smode[0] = 'b'; break;
+          case S_IFREG: smode[0] = '-'; break;
+          default: smode[0] = '?'; break;
+          }
+
+        modetable[0] = ((mode & 01000) == 0)? mtable_normal : mtable_sticky;
+        modetable[1] = ((mode & 02000) == 0)? mtable_normal : mtable_setid;
+        modetable[2] = ((mode & 04000) == 0)? mtable_normal : mtable_setid;
+
+        for (i = 0; i < 3; i++)
+          {
+          memcpy(CS(smode + 7 - i*3), CS(modetable[i][mode & 7]), 3);
+          mode >>= 3;
+          }
+
+        smode[10] = 0;
+        s = string_sprintf("mode=%04lo smode=%s inode=%ld device=%ld links=%ld "
+          "uid=%ld gid=%ld size=" OFF_T_FMT " atime=%ld mtime=%ld ctime=%ld",
+          (long)(st.st_mode & 077777), smode, (long)st.st_ino,
+          (long)st.st_dev, (long)st.st_nlink, (long)st.st_uid,
+          (long)st.st_gid, st.st_size, (long)st.st_atime,
+          (long)st.st_mtime, (long)st.st_ctime);
+        yield = string_cat(yield, &size, &ptr, s, Ustrlen(s));
+        continue;
+        }
+
+      /* vaguely random number less than N */
+
+      case EOP_RANDINT:
+        {
+        int_eximarith_t max;
+        uschar *s;
+
+        max = expanded_string_integer(sub, TRUE);
+        if (expand_string_message != NULL)
+          goto EXPAND_FAILED;
+        s = string_sprintf("%d", vaguely_random_number((int)max));
+        yield = string_cat(yield, &size, &ptr, s, Ustrlen(s));
+        continue;
+        }
+
+      /* Reverse IP, including IPv6 to dotted-nibble */
+
+      case EOP_REVERSE_IP:
+        {
+        int family, maskptr;
+        uschar reversed[128];
+
+        family = string_is_ip_address(sub, &maskptr);
+        if (family == 0)
+          {
+          expand_string_message = string_sprintf(
+              "reverse_ip() not given an IP address [%s]", sub);
+          goto EXPAND_FAILED;
+          }
+        invert_address(reversed, sub);
+        yield = string_cat(yield, &size, &ptr, reversed, Ustrlen(reversed));
+        continue;
+        }
+
+      /* Unknown operator */
+
+      default:
+      expand_string_message =
+        string_sprintf("unknown expansion operator \"%s\"", name);
+      goto EXPAND_FAILED;
+      }
+    }
+
+  /* Handle a plain name. If this is the first thing in the expansion, release
+  the pre-allocated buffer. If the result data is known to be in a new buffer,
+  newsize will be set to the size of that buffer, and we can just point at that
+  store instead of copying. Many expansion strings contain just one reference,
+  so this is a useful optimization, especially for humungous headers
+  ($message_headers). */
+                                               /*{*/
+  if (*s++ == '}')
+    {
+    int len;
+    int newsize = 0;
+    if (ptr == 0)
+      {
+      if (resetok) store_reset(yield);
+      yield = NULL;
+      size = 0;
+      }
+    value = find_variable(name, FALSE, skipping, &newsize);
+    if (value == NULL)
+      {
+      expand_string_message =
+        string_sprintf("unknown variable in \"${%s}\"", name);
+      check_variable_error_message(name);
+      goto EXPAND_FAILED;
+      }
+    len = Ustrlen(value);
+    if (yield == NULL && newsize != 0)
+      {
+      yield = value;
+      size = newsize;
+      ptr = len;
+      }
+    else yield = string_cat(yield, &size, &ptr, value, len);
+    continue;
+    }
+
+  /* Else there's something wrong */
+
+  expand_string_message =
+    string_sprintf("\"${%s\" is not a known operator (or a } is missing "
+    "in a variable reference)", name);
+  goto EXPAND_FAILED;
+  }
+
+/* If we hit the end of the string when ket_ends is set, there is a missing
+terminating brace. */
+
+if (ket_ends && *s == 0)
+  {
+  expand_string_message = malformed_header?
+    US"missing } at end of string - could be header name not terminated by colon"
+    :
+    US"missing } at end of string";
+  goto EXPAND_FAILED;
+  }
+
+/* Expansion succeeded; yield may still be NULL here if nothing was actually
+added to the string. If so, set up an empty string. Add a terminating zero. If
+left != NULL, return a pointer to the terminator. */
+
+if (yield == NULL) yield = store_get(1);
+yield[ptr] = 0;
+if (left != NULL) *left = s;
+
+/* Any stacking store that was used above the final string is no longer needed.
+In many cases the final string will be the first one that was got and so there
+will be optimal store usage. */
+
+if (resetok) store_reset(yield + ptr + 1);
+else if (resetok_p) *resetok_p = FALSE;
+
+DEBUG(D_expand)
+  {
+  debug_printf("expanding: %.*s\n   result: %s\n", (int)(s - string), string,
+    yield);
+  if (skipping) debug_printf("skipping: result is not used\n");
+  }
+return yield;
+
+/* This is the failure exit: easiest to program with a goto. We still need
+to update the pointer to the terminator, for cases of nested calls with "fail".
+*/
+
+EXPAND_FAILED_CURLY:
+expand_string_message = malformed_header?
+  US"missing or misplaced { or } - could be header name not terminated by colon"
+  :
+  US"missing or misplaced { or }";
+
+/* At one point, Exim reset the store to yield (if yield was not NULL), but
+that is a bad idea, because expand_string_message is in dynamic store. */
+
+EXPAND_FAILED:
+if (left != NULL) *left = s;
+DEBUG(D_expand)
+  {
+  debug_printf("failed to expand: %s\n", string);
+  debug_printf("   error message: %s\n", expand_string_message);
+  if (expand_string_forcedfail) debug_printf("failure was forced\n");
+  }
+if (resetok_p) *resetok_p = resetok;
+return NULL;
+}
+
+
+/* This is the external function call. Do a quick check for any expansion
+metacharacters, and if there are none, just return the input string.
+
+Argument: the string to be expanded
+Returns:  the expanded string, or NULL if expansion failed; if failure was
+          due to a lookup deferring, search_find_defer will be TRUE
+*/
+
+uschar *
+expand_string(uschar *string)
+{
+search_find_defer = FALSE;
+malformed_header = FALSE;
+return (Ustrpbrk(string, "$\\") == NULL)? string :
+  expand_string_internal(string, FALSE, NULL, FALSE, TRUE, NULL);
+}
+
+
+
+/*************************************************
+*              Expand and copy                   *
+*************************************************/
+
+/* Now and again we want to expand a string and be sure that the result is in a
+new bit of store. This function does that.
+
+Argument: the string to be expanded
+Returns:  the expanded string, always in a new bit of store, or NULL
+*/
+
+uschar *
+expand_string_copy(uschar *string)
+{
+uschar *yield = expand_string(string);
+if (yield == string) yield = string_copy(string);
+return yield;
+}
+
+
+
+/*************************************************
+*        Expand and interpret as an integer      *
+*************************************************/
+
+/* Expand a string, and convert the result into an integer.
+
+Arguments:
+  string  the string to be expanded
+  isplus  TRUE if a non-negative number is expected
+
+Returns:  the integer value, or
+          -1 for an expansion error               ) in both cases, message in
+          -2 for an integer interpretation error  ) expand_string_message
+          expand_string_message is set NULL for an OK integer
+*/
+
+int_eximarith_t
+expand_string_integer(uschar *string, BOOL isplus)
+{
+return expanded_string_integer(expand_string(string), isplus);
+}
+
+
+/*************************************************
+ *         Interpret string as an integer        *
+ *************************************************/
+
+/* Convert a string (that has already been expanded) into an integer.
+
+This function is used inside the expansion code.
+
+Arguments:
+  s       the string to be expanded
+  isplus  TRUE if a non-negative number is expected
+
+Returns:  the integer value, or
+          -1 if string is NULL (which implies an expansion error)
+          -2 for an integer interpretation error
+          expand_string_message is set NULL for an OK integer
+*/
+
+static int_eximarith_t
+expanded_string_integer(uschar *s, BOOL isplus)
+{
+int_eximarith_t value;
+uschar *msg = US"invalid integer \"%s\"";
+uschar *endptr;
+
+/* If expansion failed, expand_string_message will be set. */
+
+if (s == NULL) return -1;
+
+/* On an overflow, strtol() returns LONG_MAX or LONG_MIN, and sets errno
+to ERANGE. When there isn't an overflow, errno is not changed, at least on some
+systems, so we set it zero ourselves. */
+
+errno = 0;
+expand_string_message = NULL;               /* Indicates no error */
+
+/* Before Exim 4.64, strings consisting entirely of whitespace compared
+equal to 0.  Unfortunately, people actually relied upon that, so preserve
+the behaviour explicitly.  Stripping leading whitespace is a harmless
+noop change since strtol skips it anyway (provided that there is a number
+to find at all). */
+if (isspace(*s))
+  {
+  while (isspace(*s)) ++s;
+  if (*s == '\0')
+    {
+      DEBUG(D_expand)
+       debug_printf("treating blank string as number 0\n");
+      return 0;
+    }
+  }
+
+value = strtoll(CS s, CSS &endptr, 10);
+
+if (endptr == s)
+  {
+  msg = US"integer expected but \"%s\" found";
+  }
+else if (value < 0 && isplus)
+  {
+  msg = US"non-negative integer expected but \"%s\" found";
+  }
+else
+  {
+  switch (tolower(*endptr))
+    {
+    default:
+      break;
+    case 'k':
+      if (value > EXIM_ARITH_MAX/1024 || value < EXIM_ARITH_MIN/1024) errno = ERANGE;
+      else value *= 1024;
+      endptr++;
+      break;
+    case 'm':
+      if (value > EXIM_ARITH_MAX/(1024*1024) || value < EXIM_ARITH_MIN/(1024*1024)) errno = ERANGE;
+      else value *= 1024*1024;
+      endptr++;
+      break;
+    case 'g':
+      if (value > EXIM_ARITH_MAX/(1024*1024*1024) || value < EXIM_ARITH_MIN/(1024*1024*1024)) errno = ERANGE;
+      else value *= 1024*1024*1024;
+      endptr++;
+      break;
+    }
+  if (errno == ERANGE)
+    msg = US"absolute value of integer \"%s\" is too large (overflow)";
+  else
+    {
+    while (isspace(*endptr)) endptr++;
+    if (*endptr == 0) return value;
+    }
+  }
+
+expand_string_message = string_sprintf(CS msg, s);
+return -2;
+}
+
+
+/*************************************************
+**************************************************
+*             Stand-alone test program           *
+**************************************************
+*************************************************/
+
+#ifdef STAND_ALONE
+
+
+BOOL
+regex_match_and_setup(const pcre *re, uschar *subject, int options, int setup)
+{
+int ovector[3*(EXPAND_MAXN+1)];
+int n = pcre_exec(re, NULL, subject, Ustrlen(subject), 0, PCRE_EOPT|options,
+  ovector, sizeof(ovector)/sizeof(int));
+BOOL yield = n >= 0;
+if (n == 0) n = EXPAND_MAXN + 1;
+if (yield)
+  {
+  int nn;
+  expand_nmax = (setup < 0)? 0 : setup + 1;
+  for (nn = (setup < 0)? 0 : 2; nn < n*2; nn += 2)
+    {
+    expand_nstring[expand_nmax] = subject + ovector[nn];
+    expand_nlength[expand_nmax++] = ovector[nn+1] - ovector[nn];
+    }
+  expand_nmax--;
+  }
+return yield;
+}
+
+
+int main(int argc, uschar **argv)
+{
+int i;
+uschar buffer[1024];
+
+debug_selector = D_v;
+debug_file = stderr;
+debug_fd = fileno(debug_file);
+big_buffer = malloc(big_buffer_size);
+
+for (i = 1; i < argc; i++)
+  {
+  if (argv[i][0] == '+')
+    {
+    debug_trace_memory = 2;
+    argv[i]++;
+    }
+  if (isdigit(argv[i][0]))
+    debug_selector = Ustrtol(argv[i], NULL, 0);
+  else
+    if (Ustrspn(argv[i], "abcdefghijklmnopqrtsuvwxyz0123456789-.:/") ==
+        Ustrlen(argv[i]))
+      {
+      #ifdef LOOKUP_LDAP
+      eldap_default_servers = argv[i];
+      #endif
+      #ifdef LOOKUP_MYSQL
+      mysql_servers = argv[i];
+      #endif
+      #ifdef LOOKUP_PGSQL
+      pgsql_servers = argv[i];
+      #endif
+      #ifdef EXPERIMENTAL_REDIS
+      redis_servers = argv[i];
+      #endif
+      }
+  #ifdef EXIM_PERL
+  else opt_perl_startup = argv[i];
+  #endif
+  }
+
+printf("Testing string expansion: debug_level = %d\n\n", debug_level);
+
+expand_nstring[1] = US"string 1....";
+expand_nlength[1] = 8;
+expand_nmax = 1;
+
+#ifdef EXIM_PERL
+if (opt_perl_startup != NULL)
+  {
+  uschar *errstr;
+  printf("Starting Perl interpreter\n");
+  errstr = init_perl(opt_perl_startup);
+  if (errstr != NULL)
+    {
+    printf("** error in perl_startup code: %s\n", errstr);
+    return EXIT_FAILURE;
+    }
+  }
+#endif /* EXIM_PERL */
+
+while (fgets(buffer, sizeof(buffer), stdin) != NULL)
+  {
+  void *reset_point = store_get(0);
+  uschar *yield = expand_string(buffer);
+  if (yield != NULL)
+    {
+    printf("%s\n", yield);
+    store_reset(reset_point);
+    }
+  else
+    {
+    if (search_find_defer) printf("search_find deferred\n");
+    printf("Failed: %s\n", expand_string_message);
+    if (expand_string_forcedfail) printf("Forced failure\n");
+    printf("\n");
+    }
+  }
+
+search_tidyup();
+
+return 0;
+}
+
+#endif
+
+/* vi: aw ai sw=2
+*/
+/* End of expand.c */
diff --git a/src/filter.c b/src/filter.c
new file mode 100644 (file)
index 0000000..7132d22
--- /dev/null
@@ -0,0 +1,2611 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+
+/* Code for mail filtering functions. */
+
+#include "exim.h"
+
+
+/* Command arguments and left/right points in conditions can contain different
+types of data, depending on the particular command or condition. Originally,
+(void *) was used as "any old type", with casts, but this gives trouble and
+warnings in some environments. So now it is done "properly", with a union. We
+need to declare the structures first because some of them are recursive. */
+
+struct filter_cmd;
+struct condition_block;
+
+union argtypes {
+  struct string_item     *a;
+  BOOL                    b;
+  struct condition_block *c;
+  struct filter_cmd      *f;
+  int                     i;
+  uschar                 *u;
+};
+
+/* Local structures used in this module */
+
+typedef struct filter_cmd {
+  struct filter_cmd *next;
+  int command;
+  BOOL seen;
+  BOOL noerror;
+  union argtypes args[1];
+} filter_cmd;
+
+typedef struct condition_block {
+  struct condition_block *parent;
+  int type;
+  BOOL testfor;
+  union argtypes left;
+  union argtypes right;
+} condition_block;
+
+/* Miscellaneous other declarations */
+
+static uschar **error_pointer;
+static uschar *log_filename;
+static int  filter_options;
+static int  line_number;
+static int  expect_endif;
+static int  had_else_endif;
+static int  log_fd;
+static int  log_mode;
+static int  output_indent;
+static BOOL filter_delivered;
+static BOOL finish_obeyed;
+static BOOL seen_force;
+static BOOL seen_value;
+static BOOL noerror_force;
+
+enum { had_neither, had_else, had_elif, had_endif };
+
+static BOOL read_command_list(uschar **, filter_cmd ***, BOOL);
+
+
+/* The string arguments for the mail command. The header line ones (that are
+permitted to include \n followed by white space) first, and then the body text
+one (it can have \n anywhere). Then the file names and once_repeat, which may
+not contain \n. */
+
+static const char *mailargs[] = {  /* "to" must be first, and */
+  "to",                            /* "cc" and "bcc" must follow */
+  "cc",
+  "bcc",
+  "from",
+  "reply_to",
+  "subject",
+  "extra_headers",           /* miscellaneous added header lines */
+  "text",
+  "file",
+  "log",
+  "once",
+  "once_repeat"
+};
+
+/* The count of string arguments */
+
+#define MAILARGS_STRING_COUNT (sizeof(mailargs)/sizeof(uschar *))
+
+/* The count of string arguments that are actually passed over as strings
+(once_repeat is converted to an int). */
+
+#define mailargs_string_passed (MAILARGS_STRING_COUNT - 1)
+
+/* This defines the offsets for the arguments; first the string ones, and
+then the non-string ones. The order must be as above. */
+
+enum { mailarg_index_to,
+       mailarg_index_cc,
+       mailarg_index_bcc,
+       mailarg_index_from,
+       mailarg_index_reply_to,
+       mailarg_index_subject,
+       mailarg_index_headers,      /* misc headers must be last */
+       mailarg_index_text,         /* text is first after headers */
+       mailarg_index_file,         /* between text and expand are filenames */
+       mailarg_index_log,
+       mailarg_index_once,
+       mailarg_index_once_repeat,  /* a time string */
+       mailarg_index_expand,       /* first non-string argument */
+       mailarg_index_return,
+       mailargs_total              /* total number of arguments */
+       };
+
+/* Offsets in the data structure for the string arguments (note that
+once_repeat isn't a string argument at this point.) */
+
+static int reply_offsets[] = {  /* must be in same order as above */
+  offsetof(reply_item, to),
+  offsetof(reply_item, cc),
+  offsetof(reply_item, bcc),
+  offsetof(reply_item, from),
+  offsetof(reply_item, reply_to),
+  offsetof(reply_item, subject),
+  offsetof(reply_item, headers),
+  offsetof(reply_item, text),
+  offsetof(reply_item, file),
+  offsetof(reply_item, logfile),
+  offsetof(reply_item, oncelog),
+};
+
+/* Condition identities and names, with negated versions for some
+of them. */
+
+enum { cond_and, cond_or, cond_personal, cond_begins, cond_BEGINS,
+       cond_ends, cond_ENDS, cond_is, cond_IS, cond_matches,
+       cond_MATCHES, cond_contains, cond_CONTAINS, cond_delivered,
+       cond_above, cond_below, cond_errormsg, cond_firsttime,
+       cond_manualthaw, cond_foranyaddress };
+
+static const char *cond_names[] = {
+  "and", "or", "personal",
+  "begins", "BEGINS", "ends", "ENDS",
+  "is", "IS", "matches", "MATCHES", "contains",
+  "CONTAINS", "delivered", "above", "below", "error_message",
+  "first_delivery", "manually_thawed", "foranyaddress" };
+
+static const char *cond_not_names[] = {
+  "", "", "not personal",
+  "does not begin", "does not BEGIN",
+  "does not end", "does not END",
+  "is not", "IS not", "does not match",
+  "does not MATCH", "does not contain", "does not CONTAIN",
+  "not delivered", "not above", "not below", "not error_message",
+  "not first_delivery", "not manually_thawed", "not foranyaddress" };
+
+/* Tables of binary condition words and their corresponding types. Not easy
+to amalgamate with the above because of the different variants. */
+
+static const char *cond_words[] = {
+   "BEGIN",
+   "BEGINS",
+   "CONTAIN",
+   "CONTAINS",
+   "END",
+   "ENDS",
+   "IS",
+   "MATCH",
+   "MATCHES",
+   "above",
+   "begin",
+   "begins",
+   "below",
+   "contain",
+   "contains",
+   "end",
+   "ends",
+   "is",
+   "match",
+   "matches"};
+
+static int cond_word_count = (sizeof(cond_words)/sizeof(uschar *));
+
+static int cond_types[] = { cond_BEGINS, cond_BEGINS, cond_CONTAINS,
+  cond_CONTAINS, cond_ENDS, cond_ENDS, cond_IS, cond_MATCHES, cond_MATCHES,
+  cond_above, cond_begins, cond_begins, cond_below, cond_contains,
+  cond_contains, cond_ends, cond_ends, cond_is, cond_matches, cond_matches };
+
+/* Command identities: must be kept in step with the list of command words
+and the list of expanded argument counts which follow. */
+
+enum { add_command, defer_command, deliver_command, elif_command, else_command,
+       endif_command, finish_command, fail_command, freeze_command,
+       headers_command, if_command, logfile_command, logwrite_command,
+       mail_command, noerror_command, pipe_command, save_command, seen_command,
+       testprint_command, unseen_command, vacation_command };
+
+static const char *command_list[] = {
+  "add",     "defer",   "deliver", "elif", "else",      "endif",    "finish",
+  "fail",    "freeze",  "headers", "if",   "logfile",   "logwrite", "mail",
+  "noerror", "pipe",    "save",    "seen", "testprint", "unseen",   "vacation"
+};
+
+static int command_list_count = sizeof(command_list)/sizeof(uschar *);
+
+/* This table contains the number of expanded arguments in the bottom 4 bits.
+If the top bit is set, it means that the default for the command is "seen". */
+
+static uschar command_exparg_count[] = {
+      2, /* add */
+      1, /* defer */
+  128+2, /* deliver */
+      0, /* elif */
+      0, /* else */
+      0, /* endif */
+      0, /* finish */
+      1, /* fail */
+      1, /* freeze */
+      1, /* headers */
+      0, /* if */
+      1, /* logfile */
+      1, /* logwrite */
+      MAILARGS_STRING_COUNT, /* mail */
+      0, /* noerror */
+  128+0, /* pipe */
+  128+1, /* save */
+      0, /* seen */
+      1, /* testprint */
+      0, /* unseen */
+      MAILARGS_STRING_COUNT /* vacation */
+};
+
+
+
+/*************************************************
+*          Find next significant uschar            *
+*************************************************/
+
+/* Function to skip over white space and, optionally, comments.
+
+Arguments:
+  ptr              pointer to next character
+  comment_allowed  if TRUE, comments (# to \n) are skipped
+
+Returns:           pointer to next non-whitespace character
+*/
+
+static uschar *
+nextsigchar(uschar *ptr, BOOL comment_allowed)
+{
+for (;;)
+  {
+  while (isspace(*ptr))
+    {
+    if (*ptr == '\n') line_number++;
+    ptr++;
+    }
+  if (comment_allowed && *ptr == '#')
+    {
+    while (*(++ptr) != '\n' && *ptr != 0);
+    continue;
+    }
+  else break;
+  }
+return ptr;
+}
+
+
+
+/*************************************************
+*                Read one word                   *
+*************************************************/
+
+/* The terminator is white space unless bracket is TRUE, in which
+case ( and ) terminate.
+
+Arguments
+  ptr       pointer to next character
+  buffer    where to put the word
+  size      size of buffer
+  bracket   if TRUE, terminate on ( and ) as well as space
+
+Returns:    pointer to the next significant character after the word
+*/
+
+static uschar *
+nextword(uschar *ptr, uschar *buffer, int size, BOOL bracket)
+{
+uschar *bp = buffer;
+while (*ptr != 0 && !isspace(*ptr) &&
+       (!bracket || (*ptr != '(' && *ptr != ')')))
+  {
+  if (bp - buffer < size - 1) *bp++ = *ptr++; else
+    {
+    *error_pointer = string_sprintf("word is too long in line %d of "
+      "filter file (max = %d chars)", line_number, size);
+    break;
+    }
+  }
+*bp = 0;
+return nextsigchar(ptr, TRUE);
+}
+
+
+
+/*************************************************
+*                Read one item                   *
+*************************************************/
+
+/* Might be a word, or might be a quoted string; in the latter case
+do the escape stuff.
+
+Arguments:
+  ptr        pointer to next character
+  buffer     where to put the item
+  size       size of buffer
+  bracket    if TRUE, terminate non-quoted on ( and ) as well as space
+
+Returns:     the next significant character after the item
+*/
+
+static uschar *
+nextitem(uschar *ptr, uschar *buffer, int size, BOOL bracket)
+{
+uschar *bp = buffer;
+if (*ptr != '\"') return nextword(ptr, buffer, size, bracket);
+
+while (*(++ptr) != 0 && *ptr != '\"' && *ptr != '\n')
+  {
+  if (bp - buffer >= size - 1)
+    {
+    *error_pointer = string_sprintf("string is too long in line %d of "
+      "filter file (max = %d chars)", line_number, size);
+    break;
+    }
+
+  if (*ptr != '\\') *bp++ = *ptr; else
+    {
+    if (isspace(ptr[1]))    /* \<whitespace>NL<whitespace> ignored */
+      {
+      uschar *p = ptr + 1;
+      while (*p != '\n' && isspace(*p)) p++;
+      if (*p == '\n')
+        {
+        line_number++;
+        ptr = p;
+        while (ptr[1] != '\n' && isspace(ptr[1])) ptr++;
+        continue;
+        }
+      }
+
+    *bp++ = string_interpret_escape(&ptr);
+    }
+  }
+
+if (*ptr == '\"') ptr++;
+  else if (*error_pointer == NULL)
+    *error_pointer = string_sprintf("quote missing at end of string "
+      "in line %d", line_number);
+
+*bp = 0;
+return nextsigchar(ptr, TRUE);
+}
+
+
+
+
+/*************************************************
+*          Convert a string + K|M to a number    *
+*************************************************/
+
+/*
+Arguments:
+  s        points to text string
+  OK       set TRUE if a valid number was read
+
+Returns:   the number, or 0 on error (with *OK FALSE)
+*/
+
+static int
+get_number(uschar *s, BOOL *ok)
+{
+int value, count;
+*ok = FALSE;
+if (sscanf(CS s, "%i%n", &value, &count) != 1) return 0;
+if (tolower(s[count]) == 'k') { value *= 1024; count++; }
+if (tolower(s[count]) == 'm') { value *= 1024*1024; count++; }
+while (isspace((s[count]))) count++;
+if (s[count] != 0) return 0;
+*ok = TRUE;
+return value;
+}
+
+
+
+/*************************************************
+*            Read one condition                  *
+*************************************************/
+
+/* A complete condition must be terminated by "then"; bracketed internal
+conditions must be terminated by a closing bracket. They are read by calling
+this function recursively.
+
+Arguments:
+  ptr             points to start of condition
+  condition_block where to hang the created condition block
+  toplevel        TRUE when called at the top level
+
+Returns:          points to next character after "then"
+*/
+
+static uschar *
+read_condition(uschar *ptr, condition_block **cond, BOOL toplevel)
+{
+uschar buffer[1024];
+BOOL testfor = TRUE;
+condition_block *current_parent = NULL;
+condition_block **current = cond;
+
+*current = NULL;
+
+/* Loop to read next condition */
+
+for (;;)
+  {
+  condition_block *c;
+
+  /* reaching the end of the input is an error. */
+
+  if (*ptr == 0)
+    {
+    *error_pointer = string_sprintf("\"then\" missing at end of filter file");
+    break;
+    }
+
+  /* Opening bracket at the start of a condition introduces a nested
+  condition, which must be terminated by a closing bracket. */
+
+  if (*ptr == '(')
+    {
+    ptr = read_condition(nextsigchar(ptr+1, TRUE), &c, FALSE);
+    if (*error_pointer != NULL) break;
+    if (*ptr != ')')
+      {
+      *error_pointer = string_sprintf("expected \")\" in line %d of "
+        "filter file", line_number);
+      break;
+      }
+    if (!testfor)
+      {
+      c->testfor = !c->testfor;
+      testfor = TRUE;
+      }
+    ptr = nextsigchar(ptr+1, TRUE);
+    }
+
+
+  /* Closing bracket at the start of a condition is an error. Give an
+  explicit message, as otherwise "unknown condition" would be confusing. */
+
+  else if (*ptr == ')')
+    {
+    *error_pointer = string_sprintf("unexpected \")\" in line %d of "
+      "filter file", line_number);
+    break;
+    }
+
+  /* Otherwise we expect a word or a string. */
+
+  else
+    {
+    ptr = nextitem(ptr, buffer, sizeof(buffer), TRUE);
+    if (*error_pointer != NULL) break;
+
+    /* "Then" at the start of a condition is an error */
+
+    if (Ustrcmp(buffer, "then") == 0)
+      {
+      *error_pointer = string_sprintf("unexpected \"then\" near line %d of "
+        "filter file", line_number);
+      break;
+      }
+
+    /* "Not" at the start of a condition negates the testing condition. */
+
+    if (Ustrcmp(buffer, "not") == 0)
+      {
+      testfor = !testfor;
+      continue;
+      }
+
+    /* Build a condition block from the specific word. */
+
+    c = store_get(sizeof(condition_block));
+    c->left.u = c->right.u = NULL;
+    c->testfor = testfor;
+    testfor = TRUE;
+
+    /* Check for conditions that start with a keyword */
+
+    if (Ustrcmp(buffer, "delivered") == 0) c->type = cond_delivered;
+    else if (Ustrcmp(buffer, "error_message") == 0) c->type = cond_errormsg;
+    else if (Ustrcmp(buffer, "first_delivery") == 0) c->type = cond_firsttime;
+    else if (Ustrcmp(buffer, "manually_thawed") == 0) c->type = cond_manualthaw;
+
+    /* Personal can be followed by any number of aliases */
+
+    else if (Ustrcmp(buffer, "personal") == 0)
+      {
+      c->type = cond_personal;
+      for (;;)
+        {
+        string_item *aa;
+        uschar *saveptr = ptr;
+        ptr = nextword(ptr, buffer, sizeof(buffer), TRUE);
+        if (*error_pointer != NULL) break;
+        if (Ustrcmp(buffer, "alias") != 0)
+          {
+          ptr = saveptr;
+          break;
+          }
+        ptr = nextitem(ptr, buffer, sizeof(buffer), TRUE);
+        if (*error_pointer != NULL) break;
+        aa = store_get(sizeof(string_item));
+        aa->text = string_copy(buffer);
+        aa->next = c->left.a;
+        c->left.a = aa;
+        }
+      }
+
+    /* Foranyaddress must be followed by a string and a condition enclosed
+    in parentheses, which is handled as a subcondition. */
+
+    else if (Ustrcmp(buffer, "foranyaddress") == 0)
+      {
+      ptr = nextitem(ptr, buffer, sizeof(buffer), TRUE);
+      if (*error_pointer != NULL) break;
+      if (*ptr != '(')
+        {
+        *error_pointer = string_sprintf("\"(\" expected after \"foranyaddress\" "
+          "near line %d of filter file", line_number);
+        break;
+        }
+
+      c->type = cond_foranyaddress;
+      c->left.u = string_copy(buffer);
+
+      ptr = read_condition(nextsigchar(ptr+1, TRUE), &(c->right.c), FALSE);
+      if (*error_pointer != NULL) break;
+      if (*ptr != ')')
+        {
+        *error_pointer = string_sprintf("expected \")\" in line %d of "
+          "filter file", line_number);
+        break;
+        }
+      if (!testfor)
+        {
+        c->testfor = !c->testfor;
+        testfor = TRUE;
+        }
+      ptr = nextsigchar(ptr+1, TRUE);
+      }
+
+    /* If it's not a word we recognize, then it must be the lefthand
+    operand of one of the comparison words. */
+
+    else
+      {
+      int i;
+      uschar *isptr = NULL;
+
+      c->left.u = string_copy(buffer);
+      ptr = nextword(ptr, buffer, sizeof(buffer), TRUE);
+      if (*error_pointer != NULL) break;
+
+      /* Handle "does|is [not]", preserving the pointer after "is" in
+      case it isn't that, but the form "is <string>". */
+
+      if (strcmpic(buffer, US"does") == 0 || strcmpic(buffer, US"is") == 0)
+        {
+        if (buffer[0] == 'i') { c->type = cond_is; isptr = ptr; }
+        if (buffer[0] == 'I') { c->type = cond_IS; isptr = ptr; }
+
+        ptr = nextword(ptr, buffer, sizeof(buffer), TRUE);
+        if (*error_pointer != NULL) break;
+        if (strcmpic(buffer, US"not") == 0)
+          {
+          c->testfor = !c->testfor;
+          if (isptr != NULL) isptr = ptr;
+          ptr = nextword(ptr, buffer, sizeof(buffer), TRUE);
+          if (*error_pointer != NULL) break;
+          }
+        }
+
+      for (i = 0; i < cond_word_count; i++)
+        {
+        if (Ustrcmp(buffer, cond_words[i]) == 0)
+          {
+          c->type = cond_types[i];
+          break;
+          }
+        }
+
+      /* If an unknown word follows "is" or "is not"
+      it's actually the argument. Reset to read it. */
+
+      if (i >= cond_word_count)
+        {
+        if (isptr != NULL)
+          {
+          ptr = isptr;
+          }
+        else
+          {
+          *error_pointer = string_sprintf("unrecognized condition word \"%s\" "
+            "near line %d of filter file", buffer, line_number);
+          break;
+          }
+        }
+
+      /* Get the RH argument. */
+
+      ptr = nextitem(ptr, buffer, sizeof(buffer), TRUE);
+      if (*error_pointer != NULL) break;
+      c->right.u = string_copy(buffer);
+      }
+    }
+
+  /* We have read some new condition and set it up in the condition block
+  c; point the current pointer at it, and then deal with what follows. */
+
+  *current = c;
+
+  /* Closing bracket terminates if this is a lower-level condition. Otherwise
+  it is unexpected. */
+
+  if (*ptr == ')')
+    {
+    if (toplevel)
+      *error_pointer = string_sprintf("unexpected \")\" in line %d of "
+        "filter file", line_number);
+    break;
+    }
+
+  /* Opening bracket following a condition is an error; give an explicit
+  message to make it clearer what is wrong. */
+
+  else if (*ptr == '(')
+    {
+    *error_pointer = string_sprintf("unexpected \"(\" in line %d of "
+      "filter file", line_number);
+    break;
+    }
+
+  /* Otherwise the next thing must be one of the words "and", "or" or "then" */
+
+  else
+    {
+    uschar *saveptr = ptr;
+    ptr = nextword(ptr, buffer, sizeof(buffer), FALSE);
+    if (*error_pointer != NULL) break;
+
+    /* "Then" terminates a toplevel condition; otherwise a closing bracket
+    has been omitted. Put a string terminator at the start of "then" so
+    that reflecting the condition can be done when testing. */
+
+    if (Ustrcmp(buffer, "then") == 0)
+      {
+      if (toplevel) *saveptr = 0;
+        else *error_pointer = string_sprintf("missing \")\" at end of "
+          "condition near line %d of filter file", line_number);
+      break;
+      }
+
+    /* "And" causes a new condition block to replace the one we have
+    just read, which becomes the left sub-condition. The current pointer
+    is reset to the pointer for the right sub-condition. We have to keep
+    track of the tree of sequential "ands", so as to traverse back up it
+    if an "or" is met. */
+
+    else if (Ustrcmp(buffer, "and") == 0)
+      {
+      condition_block *andc = store_get(sizeof(condition_block));
+      andc->parent = current_parent;
+      andc->type = cond_and;
+      andc->testfor = TRUE;
+      andc->left.c = c;
+      andc->right.u = NULL;    /* insurance */
+      *current = andc;
+      current = &(andc->right.c);
+      current_parent = andc;
+      }
+
+    /* "Or" is similar, but has to be done a bit more carefully to
+    ensure that "and" is more binding. If there's a parent set, we
+    are following a sequence of "and"s and must track back to their
+    start. */
+
+    else if (Ustrcmp(buffer, "or") == 0)
+      {
+      condition_block *orc = store_get(sizeof(condition_block));
+      condition_block *or_parent = NULL;
+
+      if (current_parent != NULL)
+        {
+        while (current_parent->parent != NULL &&
+               current_parent->parent->type == cond_and)
+          current_parent = current_parent->parent;
+
+        /* If the parent has a parent, it must be an "or" parent. */
+
+        if (current_parent->parent != NULL)
+          or_parent = current_parent->parent;
+        }
+
+      orc->parent = or_parent;
+      if (or_parent == NULL) *cond = orc; else
+        or_parent->right.c = orc;
+      orc->type = cond_or;
+      orc->testfor = TRUE;
+      orc->left.c = (current_parent == NULL)? c : current_parent;
+      orc->right.c = NULL;   /* insurance */
+      current = &(orc->right.c);
+      current_parent = orc;
+      }
+
+    /* Otherwise there is a disaster */
+
+    else
+      {
+      *error_pointer = string_sprintf("\"and\" or \"or\" or \"%s\" "
+        "expected near line %d of filter file, but found \"%s\"",
+          toplevel? "then" : ")", line_number, buffer);
+      break;
+      }
+    }
+  }
+
+return nextsigchar(ptr, TRUE);
+}
+
+
+
+/*************************************************
+*             Ouput the current indent           *
+*************************************************/
+
+static void
+indent(void)
+{
+int i;
+for (i = 0; i < output_indent; i++) debug_printf(" ");
+}
+
+
+
+/*************************************************
+*          Condition printer: for debugging      *
+*************************************************/
+
+/*
+Arguments:
+  c           the block at the top of the tree
+  toplevel    TRUE at toplevel - stops overall brackets
+
+Returns:      nothing
+*/
+
+static void
+print_condition(condition_block *c, BOOL toplevel)
+{
+const char *name = (c->testfor)? cond_names[c->type] : cond_not_names[c->type];
+switch(c->type)
+  {
+  case cond_personal:
+  case cond_delivered:
+  case cond_errormsg:
+  case cond_firsttime:
+  case cond_manualthaw:
+  debug_printf("%s", name);
+  break;
+
+  case cond_is:
+  case cond_IS:
+  case cond_matches:
+  case cond_MATCHES:
+  case cond_contains:
+  case cond_CONTAINS:
+  case cond_begins:
+  case cond_BEGINS:
+  case cond_ends:
+  case cond_ENDS:
+  case cond_above:
+  case cond_below:
+  debug_printf("%s %s %s", c->left.u, name, c->right.u);
+  break;
+
+  case cond_and:
+  if (!c->testfor) debug_printf("not (");
+  print_condition(c->left.c, FALSE);
+  debug_printf(" %s ", cond_names[c->type]);
+  print_condition(c->right.c, FALSE);
+  if (!c->testfor) debug_printf(")");
+  break;
+
+  case cond_or:
+  if (!c->testfor) debug_printf("not (");
+  else if (!toplevel) debug_printf("(");
+  print_condition(c->left.c, FALSE);
+  debug_printf(" %s ", cond_names[c->type]);
+  print_condition(c->right.c, FALSE);
+  if (!toplevel || !c->testfor) debug_printf(")");
+  break;
+
+  case cond_foranyaddress:
+  debug_printf("%s %s (", name, c->left.u);
+  print_condition(c->right.c, FALSE);
+  debug_printf(")");
+  break;
+  }
+}
+
+
+
+
+/*************************************************
+*            Read one filtering command          *
+*************************************************/
+
+/*
+Arguments:
+   pptr        points to pointer to first character of command; the pointer
+                 is updated to point after the last character read
+   lastcmdptr  points to pointer to pointer to last command; used for hanging
+                 on the newly read command
+
+Returns:       TRUE if command successfully read, else FALSE
+*/
+
+static BOOL
+read_command(uschar **pptr, filter_cmd ***lastcmdptr)
+{
+int command, i, cmd_bit;
+filter_cmd *new, **newlastcmdptr;
+BOOL yield = TRUE;
+BOOL was_seen_or_unseen = FALSE;
+BOOL was_noerror = FALSE;
+uschar buffer[1024];
+uschar *ptr = *pptr;
+uschar *saveptr;
+uschar *fmsg = NULL;
+
+/* Read the next word and find which command it is. Command words are normally
+terminated by white space, but there are two exceptions, which are the "if" and
+"elif" commands. We must allow for them to be terminated by an opening bracket,
+as brackets are allowed in conditions and users will expect not to require
+white space here. */
+
+if (Ustrncmp(ptr, "if(", 3) == 0)
+  {
+  Ustrcpy(buffer, "if");
+  ptr += 2;
+  }
+else if (Ustrncmp(ptr, "elif(", 5) == 0)
+  {
+  Ustrcpy(buffer, "elif");
+  ptr += 4;
+  }
+else
+  {
+  ptr = nextword(ptr, buffer, sizeof(buffer), FALSE);
+  if (*error_pointer != NULL) return FALSE;
+  }
+
+for (command = 0; command < command_list_count; command++)
+  if (Ustrcmp(buffer, command_list[command]) == 0) break;
+
+/* Handle the individual commands */
+
+switch (command)
+  {
+  /* Add takes two arguments, separated by the word "to". Headers has two
+  arguments, but the first must be "add", "remove", or "charset", and it gets
+  stored in the second argument slot. Neither may be preceded by seen, unseen
+  or noerror. */
+
+  case add_command:
+  case headers_command:
+  if (seen_force || noerror_force)
+    {
+    *error_pointer = string_sprintf("\"seen\", \"unseen\", or \"noerror\" "
+      "found before an \"%s\" command near line %d",
+        command_list[command], line_number);
+    yield = FALSE;
+    }
+  /* Fall through */
+
+  /* Logwrite, logfile, pipe, and testprint all take a single argument, save
+  and logfile can have an option second argument for the mode, and deliver can
+  have "errors_to <address>" in a system filter, or in a user filter if the
+  address is the current one. */
+
+  case deliver_command:
+  case logfile_command:
+  case logwrite_command:
+  case pipe_command:
+  case save_command:
+  case testprint_command:
+
+  ptr = nextitem(ptr, buffer, sizeof(buffer), FALSE);
+  if (*buffer == 0)
+    *error_pointer = string_sprintf("\"%s\" requires an argument "
+      "near line %d of filter file", command_list[command], line_number);
+
+  if (*error_pointer != NULL) yield = FALSE; else
+    {
+    union argtypes argument, second_argument;
+
+    argument.u = second_argument.u = NULL;
+
+    if (command == add_command)
+      {
+      argument.u = string_copy(buffer);
+      ptr = nextitem(ptr, buffer, sizeof(buffer), FALSE);
+      if (*buffer == 0 || Ustrcmp(buffer, "to") != 0)
+        *error_pointer = string_sprintf("\"to\" expected in \"add\" command "
+          "near line %d of filter file", line_number);
+      else
+        {
+        ptr = nextitem(ptr, buffer, sizeof(buffer), FALSE);
+        if (*buffer == 0)
+          *error_pointer = string_sprintf("value missing after \"to\" "
+            "near line %d of filter file", line_number);
+        else second_argument.u = string_copy(buffer);
+        }
+      }
+
+    else if (command == headers_command)
+      {
+      if (Ustrcmp(buffer, "add") == 0)
+        second_argument.b = TRUE;
+      else
+        if (Ustrcmp(buffer, "remove") == 0) second_argument.b = FALSE;
+      else
+        if (Ustrcmp(buffer, "charset") == 0)
+          second_argument.b = TRUE_UNSET;
+      else
+        {
+        *error_pointer = string_sprintf("\"add\", \"remove\", or \"charset\" "
+          "expected after \"headers\" near line %d of filter file",
+            line_number);
+        yield = FALSE;
+        }
+
+      if (!system_filtering && second_argument.b != TRUE_UNSET)
+        {
+        *error_pointer = string_sprintf("header addition and removal is "
+          "available only in system filters: near line %d of filter file",
+          line_number);
+        yield = FALSE;
+        break;
+        }
+
+      if (yield)
+        {
+        ptr = nextitem(ptr, buffer, sizeof(buffer), FALSE);
+        if (*buffer == 0)
+          *error_pointer = string_sprintf("value missing after \"add\", "
+            "\"remove\", or \"charset\" near line %d of filter file",
+              line_number);
+        else argument.u = string_copy(buffer);
+        }
+      }
+
+    /* The argument for the logwrite command must end in a newline, and the save
+    and logfile commands can have an optional mode argument. The deliver
+    command can have an optional "errors_to <address>" for a system filter,
+    or for a user filter if the address is the user's address. Accept the
+    syntax here - the check is later. */
+
+    else
+      {
+      if (command == logwrite_command)
+        {
+        int len = Ustrlen(buffer);
+        if (len == 0 || buffer[len-1] != '\n') Ustrcat(buffer, "\n");
+        }
+
+      argument.u = string_copy(buffer);
+
+      if (command == save_command || command == logfile_command)
+        {
+        if (isdigit(*ptr))
+          {
+          ptr = nextword(ptr, buffer, sizeof(buffer), FALSE);
+          second_argument.i = (int)Ustrtol(buffer, NULL, 8);
+          }
+        else second_argument.i = -1;
+        }
+
+      else if (command == deliver_command)
+        {
+        uschar *save_ptr = ptr;
+        ptr = nextword(ptr, buffer, sizeof(buffer), FALSE);
+        if (Ustrcmp(buffer, "errors_to") == 0)
+          {
+          ptr = nextword(ptr, buffer, sizeof(buffer), FALSE);
+          second_argument.u = string_copy(buffer);
+          }
+        else ptr = save_ptr;
+        }
+      }
+
+    /* Set up the command block. Seen defaults TRUE for delivery commands,
+    FALSE for logging commands, and it doesn't matter for testprint, as
+    that doesn't change the "delivered" status. */
+
+    if (*error_pointer != NULL) yield = FALSE; else
+      {
+      new = store_get(sizeof(filter_cmd) + sizeof(union argtypes));
+      new->next = NULL;
+      **lastcmdptr = new;
+      *lastcmdptr = &(new->next);
+      new->command = command;
+      new->seen = seen_force? seen_value : command_exparg_count[command] >= 128;
+      new->noerror = noerror_force;
+      new->args[0] = argument;
+      new->args[1] = second_argument;
+      }
+    }
+  break;
+
+
+  /* Elif, else and endif just set a flag if expected. */
+
+  case elif_command:
+  case else_command:
+  case endif_command:
+  if (seen_force || noerror_force)
+    {
+    *error_pointer = string_sprintf("\"seen\", \"unseen\", or \"noerror\" "
+      "near line %d is not followed by a command", line_number);
+    yield = FALSE;
+    }
+
+  if (expect_endif > 0)
+    had_else_endif = (command == elif_command)? had_elif :
+                     (command == else_command)? had_else : had_endif;
+  else
+    {
+    *error_pointer = string_sprintf("unexpected \"%s\" command near "
+      "line %d of filter file", buffer, line_number);
+    yield = FALSE;
+    }
+  break;
+
+
+  /* Defer, freeze, and fail are available only if permitted. */
+
+  case defer_command:
+  cmd_bit = RDO_DEFER;
+  goto DEFER_FREEZE_FAIL;
+
+  case fail_command:
+  cmd_bit = RDO_FAIL;
+  goto DEFER_FREEZE_FAIL;
+
+  case freeze_command:
+  cmd_bit = RDO_FREEZE;
+
+  DEFER_FREEZE_FAIL:
+  if ((filter_options & cmd_bit) == 0)
+    {
+    *error_pointer = string_sprintf("filtering command \"%s\" is disabled: "
+      "near line %d of filter file", buffer, line_number);
+    yield = FALSE;
+    break;
+    }
+
+  /* A text message can be provided after the "text" keyword, or
+  as a string in quotes. */
+
+  saveptr = ptr;
+  ptr = nextitem(ptr, buffer, sizeof(buffer), FALSE);
+  if (*saveptr != '\"' && (*buffer == 0 || Ustrcmp(buffer, "text") != 0))
+    {
+    ptr = saveptr;
+    fmsg = US"";
+    }
+  else
+    {
+    if (*saveptr != '\"')
+      ptr = nextitem(ptr, buffer, sizeof(buffer), FALSE);
+    fmsg = string_copy(buffer);
+    }
+
+  /* Drop through and treat as "finish", but never set "seen". */
+
+  seen_value = FALSE;
+
+  /* Finish has no arguments; fmsg defaults to NULL */
+
+  case finish_command:
+  new = store_get(sizeof(filter_cmd));
+  new->next = NULL;
+  **lastcmdptr = new;
+  *lastcmdptr = &(new->next);
+  new->command = command;
+  new->seen = seen_force? seen_value : FALSE;
+  new->args[0].u = fmsg;
+  break;
+
+
+  /* Seen, unseen, and noerror are not allowed before if, which takes a
+  condition argument and then and else sub-commands. */
+
+  case if_command:
+  if (seen_force || noerror_force)
+    {
+    *error_pointer = string_sprintf("\"seen\", \"unseen\", or \"noerror\" "
+      "found before an \"if\" command near line %d",
+        line_number);
+    yield = FALSE;
+    }
+
+  /* Set up the command block for if */
+
+  new = store_get(sizeof(filter_cmd) + 4 * sizeof(union argtypes));
+  new->next = NULL;
+  **lastcmdptr = new;
+  *lastcmdptr = &(new->next);
+  new->command = command;
+  new->seen = FALSE;
+  new->args[0].u = NULL;
+  new->args[1].u = new->args[2].u = NULL;
+  new->args[3].u = ptr;
+
+  /* Read the condition */
+
+  ptr = read_condition(ptr, &(new->args[0].c), TRUE);
+  if (*error_pointer != NULL) { yield = FALSE; break; }
+
+  /* Read the commands to be obeyed if the condition is true */
+
+  newlastcmdptr = &(new->args[1].f);
+  if (!read_command_list(&ptr, &newlastcmdptr, TRUE)) yield = FALSE;
+
+  /* If commands were successfully read, handle the various possible
+  terminators. There may be a number of successive "elif" sections. */
+
+  else
+    {
+    while (had_else_endif == had_elif)
+      {
+      filter_cmd *newnew =
+        store_get(sizeof(filter_cmd) + 4 * sizeof(union argtypes));
+      new->args[2].f = newnew;
+      new = newnew;
+      new->next = NULL;
+      new->command = command;
+      new->seen = FALSE;
+      new->args[0].u = NULL;
+      new->args[1].u = new->args[2].u = NULL;
+      new->args[3].u = ptr;
+
+      ptr = read_condition(ptr, &(new->args[0].c), TRUE);
+      if (*error_pointer != NULL) { yield = FALSE; break; }
+      newlastcmdptr = &(new->args[1].f);
+      if (!read_command_list(&ptr, &newlastcmdptr, TRUE))
+        yield = FALSE;
+      }
+
+    if (yield == FALSE) break;
+
+    /* Handle termination by "else", possibly following one or more
+    "elsif" sections. */
+
+    if (had_else_endif == had_else)
+      {
+      newlastcmdptr = &(new->args[2].f);
+      if (!read_command_list(&ptr, &newlastcmdptr, TRUE))
+        yield = FALSE;
+      else if (had_else_endif != had_endif)
+        {
+        *error_pointer = string_sprintf("\"endif\" missing near line %d of "
+          "filter file", line_number);
+        yield = FALSE;
+        }
+      }
+
+    /* Otherwise the terminator was "endif" - this is checked by
+    read_command_list(). The pointer is already set to NULL. */
+    }
+
+  /* Reset the terminator flag. */
+
+  had_else_endif = had_neither;
+  break;
+
+
+  /* The mail & vacation commands have a whole slew of keyworded arguments.
+  The final argument values are the file expand and return message booleans,
+  whose offsets are defined in mailarg_index_{expand,return}. Although they
+  are logically booleans, because they are stored in a uschar * value, we use
+  NULL and not FALSE, to keep 64-bit compilers happy. */
+
+  case mail_command:
+  case vacation_command:
+  new = store_get(sizeof(filter_cmd) + mailargs_total * sizeof(union argtypes));
+  new->next = NULL;
+  new->command = command;
+  new->seen = seen_force? seen_value : FALSE;
+  new->noerror = noerror_force;
+  for (i = 0; i < mailargs_total; i++) new->args[i].u = NULL;
+
+  /* Read keyword/value pairs until we hit one that isn't. The data
+  must contain only printing chars plus tab, though the "text" value
+  can also contain newlines. The "file" keyword can be preceded by the
+  word "expand", and "return message" has no data. */
+
+  for (;;)
+    {
+    uschar *saveptr = ptr;
+    ptr = nextword(ptr, buffer, sizeof(buffer), FALSE);
+    if (*error_pointer != NULL)
+      {
+      yield = FALSE;
+      break;
+      }
+
+    /* Ensure "return" is followed by "message"; that's a complete option */
+
+    if (Ustrcmp(buffer, "return") == 0)
+      {
+      new->args[mailarg_index_return].u = US"";  /* not NULL => TRUE */
+      ptr = nextword(ptr, buffer, sizeof(buffer), FALSE);
+      if (Ustrcmp(buffer, "message") != 0)
+        {
+        *error_pointer = string_sprintf("\"return\" not followed by \"message\" "
+          " near line %d of filter file", line_number);
+        yield = FALSE;
+        break;
+        }
+      continue;
+      }
+
+    /* Ensure "expand" is followed by "file", then fall through to process the
+    file keyword. */
+
+    if (Ustrcmp(buffer, "expand") == 0)
+      {
+      new->args[mailarg_index_expand].u = US"";  /* not NULL => TRUE */
+      ptr = nextword(ptr, buffer, sizeof(buffer), FALSE);
+      if (Ustrcmp(buffer, "file") != 0)
+        {
+        *error_pointer = string_sprintf("\"expand\" not followed by \"file\" "
+          " near line %d of filter file", line_number);
+        yield = FALSE;
+        break;
+        }
+      }
+
+    /* Scan for the keyword */
+
+    for (i = 0; i < MAILARGS_STRING_COUNT; i++)
+      if (Ustrcmp(buffer, mailargs[i]) == 0) break;
+
+    /* Not found keyword; assume end of this command */
+
+    if (i >= MAILARGS_STRING_COUNT)
+      {
+      ptr = saveptr;
+      break;
+      }
+
+    /* Found keyword, read the data item */
+
+    ptr = nextitem(ptr, buffer, sizeof(buffer), FALSE);
+    if (*error_pointer != NULL)
+      {
+      yield = FALSE;
+      break;
+      }
+    else new->args[i].u = string_copy(buffer);
+    }
+
+  /* If this is the vacation command, apply some default settings to
+  some of the arguments. */
+
+  if (command == vacation_command)
+    {
+    if (new->args[mailarg_index_file].u == NULL)
+      {
+      new->args[mailarg_index_file].u = string_copy(US".vacation.msg");
+      new->args[mailarg_index_expand].u = US"";   /* not NULL => TRUE */
+      }
+    if (new->args[mailarg_index_log].u == NULL)
+      new->args[mailarg_index_log].u = string_copy(US".vacation.log");
+    if (new->args[mailarg_index_once].u == NULL)
+      new->args[mailarg_index_once].u = string_copy(US".vacation");
+    if (new->args[mailarg_index_once_repeat].u == NULL)
+      new->args[mailarg_index_once_repeat].u = string_copy(US"7d");
+    if (new->args[mailarg_index_subject].u == NULL)
+      new->args[mailarg_index_subject].u = string_copy(US"On vacation");
+    }
+
+  /* Join the address on to the chain of generated addresses */
+
+  **lastcmdptr = new;
+  *lastcmdptr = &(new->next);
+  break;
+
+
+  /* Seen and unseen just set flags */
+
+  case seen_command:
+  case unseen_command:
+  if (*ptr == 0)
+    {
+    *error_pointer = string_sprintf("\"seen\" or \"unseen\" "
+      "near line %d is not followed by a command", line_number);
+    yield = FALSE;
+    }
+  if (seen_force)
+    {
+    *error_pointer = string_sprintf("\"seen\" or \"unseen\" repeated "
+      "near line %d", line_number);
+    yield = FALSE;
+    }
+  seen_value = (command == seen_command);
+  seen_force = TRUE;
+  was_seen_or_unseen = TRUE;
+  break;
+
+
+  /* So does noerror */
+
+  case noerror_command:
+  if (*ptr == 0)
+    {
+    *error_pointer = string_sprintf("\"noerror\" "
+      "near line %d is not followed by a command", line_number);
+    yield = FALSE;
+    }
+  noerror_force = TRUE;
+  was_noerror = TRUE;
+  break;
+
+
+  /* Oops */
+
+  default:
+  *error_pointer = string_sprintf("unknown filtering command \"%s\" "
+    "near line %d of filter file", buffer, line_number);
+  yield = FALSE;
+  break;
+  }
+
+if (!was_seen_or_unseen && !was_noerror)
+  {
+  seen_force = FALSE;
+  noerror_force = FALSE;
+  }
+
+*pptr = ptr;
+return yield;
+}
+
+
+
+/*************************************************
+*              Read a list of commands           *
+*************************************************/
+
+/* If condional is TRUE, the list must be terminated
+by the words "else" or "endif".
+
+Arguments:
+  pptr        points to pointer to next character; the pointer is updated
+  lastcmdptr  points to pointer to pointer to previously-read command; used
+                for hanging on the new command
+  conditional TRUE if this command is the subject of a condition
+
+Returns:      TRUE on success
+*/
+
+static BOOL
+read_command_list(uschar **pptr, filter_cmd ***lastcmdptr, BOOL conditional)
+{
+if (conditional) expect_endif++;
+had_else_endif = had_neither;
+while (**pptr != 0 && had_else_endif == had_neither)
+  {
+  if (!read_command(pptr, lastcmdptr)) return FALSE;
+  *pptr = nextsigchar(*pptr, TRUE);
+  }
+if (conditional)
+  {
+  expect_endif--;
+  if (had_else_endif == had_neither)
+    {
+    *error_pointer = US"\"endif\" missing at end of filter file";
+    return FALSE;
+    }
+  }
+return TRUE;
+}
+
+
+
+
+/*************************************************
+*             Test a condition                   *
+*************************************************/
+
+/*
+Arguments:
+  c              points to the condition block; c->testfor indicated whether
+                   it's a positive or negative condition
+  toplevel       TRUE if called from "if" directly; FALSE otherwise
+
+Returns:         TRUE if the condition is met
+*/
+
+static BOOL
+test_condition(condition_block *c, BOOL toplevel)
+{
+BOOL yield = FALSE;
+const pcre *re;
+uschar *exp[2], *p, *pp;
+const uschar *regcomp_error = NULL;
+int regcomp_error_offset;
+int val[2];
+int i;
+
+if (c == NULL) return TRUE;  /* does this ever occur? */
+
+switch (c->type)
+  {
+  case cond_and:
+  yield = test_condition(c->left.c, FALSE) &&
+          *error_pointer == NULL &&
+          test_condition(c->right.c, FALSE);
+  break;
+
+  case cond_or:
+  yield = test_condition(c->left.c, FALSE) ||
+          (*error_pointer == NULL &&
+          test_condition(c->right.c, FALSE));
+  break;
+
+  /* The personal test is meaningless in a system filter. The tests are now in
+  a separate function (so Sieve can use them). However, an Exim filter does not
+  scan Cc: (hence the FALSE argument). */
+
+  case cond_personal:
+  yield = system_filtering? FALSE : filter_personal(c->left.a, FALSE);
+  break;
+
+  case cond_delivered:
+  yield = filter_delivered;
+  break;
+
+  /* Only TRUE if a message is actually being processed; FALSE for address
+  testing and verification. */
+
+  case cond_errormsg:
+  yield = message_id[0] != 0 &&
+    (sender_address == NULL || sender_address[0] == 0);
+  break;
+
+  /* Only FALSE if a message is actually being processed; TRUE for address
+  and filter testing and verification. */
+
+  case cond_firsttime:
+  yield = filter_test != FTEST_NONE || message_id[0] == 0 || deliver_firsttime;
+  break;
+
+  /* Only TRUE if a message is actually being processed; FALSE for address
+  testing and verification. */
+
+  case cond_manualthaw:
+  yield = message_id[0] != 0 && deliver_manual_thaw;
+  break;
+
+  /* The foranyaddress condition loops through a list of addresses */
+
+  case cond_foranyaddress:
+  p = c->left.u;
+  pp = expand_string(p);
+  if (pp == NULL)
+    {
+    *error_pointer = string_sprintf("failed to expand \"%s\" in "
+      "filter file: %s", p, expand_string_message);
+    return FALSE;
+    }
+
+  yield = FALSE;
+  parse_allow_group = TRUE;     /* Allow group syntax */
+
+  while (*pp != 0)
+    {
+    uschar *error;
+    int start, end, domain;
+    int saveend;
+
+    p = parse_find_address_end(pp, FALSE);
+    saveend = *p;
+
+    *p = 0;
+    filter_thisaddress =
+      parse_extract_address(pp, &error, &start, &end, &domain, FALSE);
+    *p = saveend;
+
+    if (filter_thisaddress != NULL)
+      {
+      if ((filter_test != FTEST_NONE && debug_selector != 0) ||
+          (debug_selector & D_filter) != 0)
+        {
+        indent();
+        debug_printf("Extracted address %s\n", filter_thisaddress);
+        }
+      yield = test_condition(c->right.c, FALSE);
+      }
+
+    if (yield) break;
+    if (saveend == 0) break;
+    pp = p + 1;
+    }
+
+  parse_allow_group = FALSE;      /* Reset group syntax flags */
+  parse_found_group = FALSE;
+  break;
+
+  /* All other conditions have left and right values that need expanding;
+  on error, it doesn't matter what value is returned. */
+
+  default:
+  p = c->left.u;
+  for (i = 0; i < 2; i++)
+    {
+    exp[i] = expand_string(p);
+    if (exp[i] == NULL)
+      {
+      *error_pointer = string_sprintf("failed to expand \"%s\" in "
+        "filter file: %s", p, expand_string_message);
+      return FALSE;
+      }
+    p = c->right.u;
+    }
+
+  /* Inner switch for the different cases */
+
+  switch(c->type)
+    {
+    case cond_is:
+    yield = strcmpic(exp[0], exp[1]) == 0;
+    break;
+
+    case cond_IS:
+    yield = Ustrcmp(exp[0], exp[1]) == 0;
+    break;
+
+    case cond_contains:
+    yield = strstric(exp[0], exp[1], FALSE) != NULL;
+    break;
+
+    case cond_CONTAINS:
+    yield = Ustrstr(exp[0], exp[1]) != NULL;
+    break;
+
+    case cond_begins:
+    yield = strncmpic(exp[0], exp[1], Ustrlen(exp[1])) == 0;
+    break;
+
+    case cond_BEGINS:
+    yield = Ustrncmp(exp[0], exp[1], Ustrlen(exp[1])) == 0;
+    break;
+
+    case cond_ends:
+    case cond_ENDS:
+      {
+      int len = Ustrlen(exp[1]);
+      uschar *s = exp[0] + Ustrlen(exp[0]) - len;
+      yield = (s < exp[0])? FALSE :
+        ((c->type == cond_ends)? strcmpic(s, exp[1]) : Ustrcmp(s, exp[1])) == 0;
+      }
+    break;
+
+    case cond_matches:
+    case cond_MATCHES:
+    if ((filter_test != FTEST_NONE && debug_selector != 0) ||
+        (debug_selector & D_filter) != 0)
+      {
+      debug_printf("Match expanded arguments:\n");
+      debug_printf("  Subject = %s\n", exp[0]);
+      debug_printf("  Pattern = %s\n", exp[1]);
+      }
+
+    re = pcre_compile(CS exp[1],
+      PCRE_COPT | ((c->type == cond_matches)? PCRE_CASELESS : 0),
+      (const char **)&regcomp_error, &regcomp_error_offset, NULL);
+
+    if (re == NULL)
+      {
+      *error_pointer = string_sprintf("error while compiling "
+        "regular expression \"%s\": %s at offset %d",
+        exp[1], regcomp_error, regcomp_error_offset);
+      return FALSE;
+      }
+
+    yield = regex_match_and_setup(re, exp[0], PCRE_EOPT, -1);
+    break;
+
+    /* For above and below, convert the strings to numbers */
+
+    case cond_above:
+    case cond_below:
+    for (i = 0; i < 2; i++)
+      {
+      val[i] = get_number(exp[i], &yield);
+      if (!yield)
+        {
+        *error_pointer = string_sprintf("malformed numerical string \"%s\"",
+          exp[i]);
+        return FALSE;
+        }
+      }
+    yield = (c->type == cond_above)? (val[0] > val[1]) : (val[0] < val[1]);
+    break;
+    }
+  break;
+  }
+
+if ((filter_test != FTEST_NONE && debug_selector != 0) ||
+    (debug_selector & D_filter) != 0)
+  {
+  indent();
+  debug_printf("%sondition is %s: ",
+    toplevel? "C" : "Sub-c",
+    (yield == c->testfor)? "true" : "false");
+  print_condition(c, TRUE);
+  debug_printf("\n");
+  }
+
+return yield == c->testfor;
+}
+
+
+
+/*************************************************
+*          Interpret chain of commands           *
+*************************************************/
+
+/* In testing state, just say what would be done rather than doing it. The
+testprint command just expands and outputs its argument in testing state, and
+does nothing otherwise.
+
+Arguments:
+  commands    points to chain of commands to interpret
+  generated   where to hang newly-generated addresses
+
+Returns:      FF_DELIVERED     success, a significant action was taken
+              FF_NOTDELIVERED  success, no significant action
+              FF_DEFER         defer requested
+              FF_FAIL          fail requested
+              FF_FREEZE        freeze requested
+              FF_ERROR         there was a problem
+*/
+
+static int
+interpret_commands(filter_cmd *commands, address_item **generated)
+{
+uschar *s;
+int mode;
+address_item *addr;
+BOOL condition_value;
+
+while (commands != NULL)
+  {
+  int ff_ret;
+  uschar *fmsg, *ff_name;
+  uschar *expargs[MAILARGS_STRING_COUNT];
+
+  int i, n[2];
+
+  /* Expand the relevant number of arguments for the command that are
+  not NULL. */
+
+  for (i = 0; i < (command_exparg_count[commands->command] & 15); i++)
+    {
+    uschar *ss = commands->args[i].u;
+    if (ss == NULL)
+      {
+      expargs[i] = NULL;
+      }
+    else
+      {
+      expargs[i] = expand_string(ss);
+      if (expargs[i] == NULL)
+        {
+        *error_pointer = string_sprintf("failed to expand \"%s\" in "
+          "%s command: %s", ss, command_list[commands->command],
+          expand_string_message);
+        return FF_ERROR;
+        }
+      }
+    }
+
+  /* Now switch for each command, setting the "delivered" flag if any of them
+  have "seen" set. */
+
+  if (commands->seen) filter_delivered = TRUE;
+
+  switch(commands->command)
+    {
+    case add_command:
+    for (i = 0; i < 2; i++)
+      {
+      uschar *ss = expargs[i];
+      uschar *end;
+
+      if (i == 1 && (*ss++ != 'n' || ss[1] != 0))
+        {
+        *error_pointer = string_sprintf("unknown variable \"%s\" in \"add\" "
+          "command", expargs[i]);
+        return FF_ERROR;
+        }
+
+      /* Allow for "--" at the start of the value (from -$n0) for example */
+      if (i == 0) while (ss[0] == '-' && ss[1] == '-') ss += 2;
+
+      n[i] = (int)Ustrtol(ss, &end, 0);
+      if (*end != 0)
+        {
+        *error_pointer = string_sprintf("malformed number \"%s\" in \"add\" "
+          "command", ss);
+        return FF_ERROR;
+        }
+      }
+
+    filter_n[n[1]] += n[0];
+    if (filter_test != FTEST_NONE) printf("Add %d to n%d\n", n[0], n[1]);
+    break;
+
+    /* A deliver command's argument must be a valid address. Its optional
+    second argument (system filter only) must also be a valid address. */
+
+    case deliver_command:
+    for (i = 0; i < 2; i++)
+      {
+      s = expargs[i];
+      if (s != NULL)
+        {
+        int start, end, domain;
+        uschar *error;
+        uschar *ss = parse_extract_address(s, &error, &start, &end, &domain,
+          FALSE);
+        if (ss != NULL)
+          expargs[i] = ((filter_options & RDO_REWRITE) != 0)?
+            rewrite_address(ss, TRUE, FALSE, global_rewrite_rules,
+              rewrite_existflags) :
+            rewrite_address_qualify(ss, TRUE);
+        else
+          {
+          *error_pointer = string_sprintf("malformed address \"%s\" in "
+            "filter file: %s", s, error);
+          return FF_ERROR;
+          }
+        }
+      }
+
+    /* Stick the errors address into a simple variable, as it will
+    be referenced a few times. Check that the caller is permitted to
+    specify it. */
+
+    s = expargs[1];
+
+    if (s != NULL && !system_filtering)
+      {
+      uschar *ownaddress = expand_string(US"$local_part@$domain");
+      if (strcmpic(ownaddress, s) != 0)
+        {
+        *error_pointer = US"errors_to must point to the caller's address";
+        return FF_ERROR;
+        }
+      }
+
+    /* Test case: report what would happen */
+
+    if (filter_test != FTEST_NONE)
+      {
+      indent();
+      printf("%seliver message to: %s%s%s%s\n",
+        (commands->seen)? "D" : "Unseen d",
+        expargs[0],
+        commands->noerror? " (noerror)" : "",
+        (s != NULL)? " errors_to " : "",
+        (s != NULL)? s : US"");
+      }
+
+    /* Real case. */
+
+    else
+      {
+      DEBUG(D_filter) debug_printf("Filter: %sdeliver message to: %s%s%s%s\n",
+        (commands->seen)? "" : "unseen ",
+        expargs[0],
+        commands->noerror? " (noerror)" : "",
+        (s != NULL)? " errors_to " : "",
+        (s != NULL)? s : US"");
+
+      /* Create the new address and add it to the chain, setting the
+      af_ignore_error flag if necessary, and the errors address, which can be
+      set in a system filter and to the local address in user filters. */
+
+      addr = deliver_make_addr(expargs[0], TRUE);  /* TRUE => copy s */
+      addr->p.errors_address = (s == NULL)?
+        s : string_copy(s);                        /* Default is NULL */
+      if (commands->noerror) setflag(addr, af_ignore_error);
+      addr->next = *generated;
+      *generated = addr;
+      }
+    break;
+
+    case save_command:
+    s = expargs[0];
+    mode = commands->args[1].i;
+
+    /* Test case: report what would happen */
+
+    if (filter_test != FTEST_NONE)
+      {
+      indent();
+      if (mode < 0)
+        printf("%save message to: %s%s\n", (commands->seen)?
+          "S" : "Unseen s", s, commands->noerror? " (noerror)" : "");
+      else
+        printf("%save message to: %s %04o%s\n", (commands->seen)?
+          "S" : "Unseen s", s, mode, commands->noerror? " (noerror)" : "");
+      }
+
+    /* Real case: Ensure save argument starts with / if there is a home
+    directory to prepend. */
+
+    else
+      {
+      if (s[0] != '/' && (filter_options & RDO_PREPEND_HOME) != 0 &&
+          deliver_home != NULL && deliver_home[0] != 0)
+        s = string_sprintf("%s/%s", deliver_home, s);
+      DEBUG(D_filter) debug_printf("Filter: %ssave message to: %s%s\n",
+        (commands->seen)? "" : "unseen ", s,
+        commands->noerror? " (noerror)" : "");
+
+      /* Create the new address and add it to the chain, setting the
+      af_pfr and af_file flags, the af_ignore_error flag if necessary, and the
+      mode value. */
+
+      addr = deliver_make_addr(s, TRUE);  /* TRUE => copy s */
+      setflag(addr, af_pfr|af_file);
+      if (commands->noerror) setflag(addr, af_ignore_error);
+      addr->mode = mode;
+      addr->next = *generated;
+      *generated = addr;
+      }
+    break;
+
+    case pipe_command:
+    s = string_copy(commands->args[0].u);
+    if (filter_test != FTEST_NONE)
+      {
+      indent();
+      printf("%sipe message to: %s%s\n", (commands->seen)?
+        "P" : "Unseen p", s, commands->noerror? " (noerror)" : "");
+      }
+    else /* Ensure pipe command starts with | */
+      {
+      DEBUG(D_filter) debug_printf("Filter: %spipe message to: %s%s\n",
+        (commands->seen)? "" : "unseen ", s,
+        commands->noerror? " (noerror)" : "");
+      if (s[0] != '|') s = string_sprintf("|%s", s);
+
+      /* Create the new address and add it to the chain, setting the
+      af_ignore_error flag if necessary. Set the af_expand_pipe flag so that
+      each command argument is expanded in the transport after the command
+      has been split up into separate arguments. */
+
+      addr = deliver_make_addr(s, TRUE);  /* TRUE => copy s */
+      setflag(addr, af_pfr|af_expand_pipe);
+      if (commands->noerror) setflag(addr, af_ignore_error);
+      addr->next = *generated;
+      *generated = addr;
+
+      /* If there are any numeric variables in existence (e.g. after a regex
+      condition), or if $thisaddress is set, take a copy for use in the
+      expansion. Note that we can't pass NULL for filter_thisaddress, because
+      NULL terminates the list. */
+
+      if (expand_nmax >= 0 || filter_thisaddress != NULL)
+        {
+        int i;
+        int ecount = (expand_nmax >= 0)? expand_nmax : -1;
+        uschar **ss = store_get(sizeof(uschar *) * (ecount + 3));
+        addr->pipe_expandn = ss;
+        if (filter_thisaddress == NULL) filter_thisaddress = US"";
+        *ss++ = string_copy(filter_thisaddress);
+        for (i = 0; i <= expand_nmax; i++)
+          *ss++ = string_copyn(expand_nstring[i], expand_nlength[i]);
+        *ss = NULL;
+        }
+      }
+    break;
+
+    /* Set up the file name and mode, and close any previously open
+    file. */
+
+    case logfile_command:
+    log_mode = commands->args[1].i;
+    if (log_mode == -1) log_mode = 0600;
+    if (log_fd >= 0)
+      {
+      (void)close(log_fd);
+      log_fd = -1;
+      }
+    log_filename = expargs[0];
+    if (filter_test != FTEST_NONE)
+      {
+      indent();
+      printf("%sogfile %s\n", (commands->seen)? "Seen l" : "L", log_filename);
+      }
+    break;
+
+    case logwrite_command:
+    s = expargs[0];
+
+    if (filter_test != FTEST_NONE)
+      {
+      indent();
+      printf("%sogwrite \"%s\"\n", (commands->seen)? "Seen l" : "L",
+        string_printing(s));
+      }
+
+    /* Attempt to write to a log file only if configured as permissible.
+    Logging may be forcibly skipped for verifying or testing. */
+
+    else if ((filter_options & RDO_LOG) != 0)   /* Locked out */
+      {
+      DEBUG(D_filter)
+        debug_printf("filter log command aborted: euid=%ld\n",
+        (long int)geteuid());
+      *error_pointer = US"logwrite command forbidden";
+      return FF_ERROR;
+      }
+    else if ((filter_options & RDO_REALLOG) != 0)
+      {
+      int len;
+      DEBUG(D_filter) debug_printf("writing filter log as euid %ld\n",
+        (long int)geteuid());
+      if (log_fd < 0)
+        {
+        if (log_filename == NULL)
+          {
+          *error_pointer = US"attempt to obey \"logwrite\" command "
+            "without a previous \"logfile\"";
+          return FF_ERROR;
+          }
+        log_fd = Uopen(log_filename, O_CREAT|O_APPEND|O_WRONLY, log_mode);
+        if (log_fd < 0)
+          {
+          *error_pointer = string_open_failed(errno, "filter log file \"%s\"",
+            log_filename);
+          return FF_ERROR;
+          }
+        }
+      len = Ustrlen(s);
+      if (write(log_fd, s, len) != len)
+        {
+        *error_pointer = string_sprintf("write error on file \"%s\": %s",
+          log_filename, strerror(errno));
+        return FF_ERROR;
+        }
+      }
+    else
+      {
+      DEBUG(D_filter) debug_printf("skipping logwrite (verifying or testing)\n");
+      }
+    break;
+
+    /* Header addition and removal is available only in the system filter. The
+    command is rejected at parse time otherwise. However "headers charset" is
+    always permitted. */
+
+    case headers_command:
+      {
+      int subtype = commands->args[1].i;
+      s = expargs[0];
+
+      if (filter_test != FTEST_NONE)
+        printf("Headers %s \"%s\"\n", (subtype == TRUE)? "add" :
+          (subtype == FALSE)? "remove" : "charset", string_printing(s));
+
+      if (subtype == TRUE)
+        {
+        while (isspace(*s)) s++;
+        if (s[0] != 0)
+          {
+          header_add(htype_other, "%s%s", s, (s[Ustrlen(s)-1] == '\n')?
+            "" : "\n");
+          header_last->type = header_checkname(header_last, FALSE);
+          if (header_last->type >= 'a') header_last->type = htype_other;
+          }
+        }
+
+      else if (subtype == FALSE)
+        {
+        int sep = 0;
+        uschar *ss;
+        uschar *list = s;
+        uschar buffer[128];
+        while ((ss = string_nextinlist(&list, &sep, buffer, sizeof(buffer)))
+               != NULL)
+          header_remove(0, ss);
+        }
+
+      /* This setting lasts only while the filter is running; on exit, the
+      variable is reset to the previous value. */
+
+      else headers_charset = s;
+      }
+    break;
+
+    /* Defer, freeze, and fail are available only when explicitly permitted.
+    These commands are rejected at parse time otherwise. The message can get
+    very long by the inclusion of message headers; truncate if it is, and also
+    ensure printing characters so as not to mess up log files. */
+
+    case defer_command:
+    ff_name = US"defer";
+    ff_ret = FF_DEFER;
+    goto DEFERFREEZEFAIL;
+
+    case fail_command:
+    ff_name = US"fail";
+    ff_ret = FF_FAIL;
+    goto DEFERFREEZEFAIL;
+
+    case freeze_command:
+    ff_name = US"freeze";
+    ff_ret = FF_FREEZE;
+
+    DEFERFREEZEFAIL:
+    fmsg = expargs[0];
+    if (Ustrlen(fmsg) > 1024) Ustrcpy(fmsg + 1000, " ... (truncated)");
+    fmsg = string_printing(fmsg);
+    *error_pointer = fmsg;
+
+    if (filter_test != FTEST_NONE)
+      {
+      indent();
+      printf("%c%s text \"%s\"\n", toupper(ff_name[0]), ff_name+1, fmsg);
+      }
+    else DEBUG(D_filter) debug_printf("Filter: %s \"%s\"\n", ff_name, fmsg);
+    return ff_ret;
+
+    case finish_command:
+    if (filter_test != FTEST_NONE)
+      {
+      indent();
+      printf("%sinish\n", (commands->seen)? "Seen f" : "F");
+      }
+    else
+      {
+      DEBUG(D_filter) debug_printf("Filter: %sfinish\n",
+        (commands->seen)? " Seen " : "");
+      }
+    finish_obeyed = TRUE;
+    return filter_delivered? FF_DELIVERED : FF_NOTDELIVERED;
+
+    case if_command:
+      {
+      uschar *save_address = filter_thisaddress;
+      int ok = FF_DELIVERED;
+      condition_value = test_condition(commands->args[0].c, TRUE);
+      if (*error_pointer != NULL) ok = FF_ERROR; else
+        {
+        output_indent += 2;
+        ok = interpret_commands(commands->args[condition_value? 1:2].f,
+          generated);
+        output_indent -= 2;
+        }
+      filter_thisaddress = save_address;
+      if (finish_obeyed || (ok != FF_DELIVERED && ok != FF_NOTDELIVERED))
+        return ok;
+      }
+    break;
+
+
+    /* To try to catch runaway loops, do not generate mail if the
+    return path is unset or if a non-trusted user supplied -f <>
+    as the return path. */
+
+    case mail_command:
+    case vacation_command:
+    if (return_path == NULL || return_path[0] == 0)
+      {
+      if (filter_test != FTEST_NONE)
+        printf("%s command ignored because return_path is empty\n",
+          command_list[commands->command]);
+      else DEBUG(D_filter) debug_printf("%s command ignored because return_path "
+        "is empty\n", command_list[commands->command]);
+      break;
+      }
+
+    /* Check the contents of the strings. The type of string can be deduced
+    from the value of i.
+
+    . If i is equal to mailarg_index_text it's a text string for the body,
+      where anything goes.
+
+    . If i is > mailarg_index_text, we are dealing with a file name, which
+      cannot contain non-printing characters.
+
+    . If i is less than mailarg_index_headers we are dealing with something
+      that will go in a single message header line, where newlines must be
+      followed by white space.
+
+    . If i is equal to mailarg_index_headers, we have a string that contains
+      one or more headers. Newlines that are not followed by white space must
+      be followed by a header name.
+    */
+
+    for (i = 0; i < MAILARGS_STRING_COUNT; i++)
+      {
+      uschar *p;
+      uschar *s = expargs[i];
+
+      if (s == NULL) continue;
+
+      if (i != mailarg_index_text) for (p = s; *p != 0; p++)
+        {
+        int c = *p;
+        if (i > mailarg_index_text)
+          {
+          if (!mac_isprint(c))
+            {
+            *error_pointer = string_sprintf("non-printing character in \"%s\" "
+              "in %s command", string_printing(s),
+              command_list[commands->command]);
+            return FF_ERROR;
+            }
+          }
+
+        /* i < mailarg_index_text */
+
+        else if (c == '\n' && !isspace(p[1]))
+          {
+          if (i < mailarg_index_headers)
+            {
+            *error_pointer = string_sprintf("\\n not followed by space in "
+              "\"%.1024s\" in %s command", string_printing(s),
+              command_list[commands->command]);
+            return FF_ERROR;
+            }
+
+          /* Check for the start of a new header line within the string */
+
+          else
+            {
+            uschar *pp;
+            for (pp = p + 1;; pp++)
+              {
+              c = *pp;
+              if (c == ':' && pp != p + 1) break;
+              if (c == 0 || c == ':' || isspace(*pp))
+                {
+                *error_pointer = string_sprintf("\\n not followed by space or "
+                  "valid header name in \"%.1024s\" in %s command",
+                  string_printing(s), command_list[commands->command]);
+                return FF_ERROR;
+                }
+              }
+            p = pp;
+            }
+          }
+        }       /* Loop to scan the string */
+
+      /* The string is OK */
+
+      commands->args[i].u = s;
+      }
+
+    /* Proceed with mail or vacation command */
+
+    if (filter_test != FTEST_NONE)
+      {
+      uschar *to = commands->args[mailarg_index_to].u;
+      indent();
+      printf("%sail to: %s%s%s\n", (commands->seen)? "Seen m" : "M",
+        (to == NULL)? US"<default>" : to,
+        (commands->command == vacation_command)? " (vacation)" : "",
+        (commands->noerror)? " (noerror)" : "");
+      for (i = 1; i < MAILARGS_STRING_COUNT; i++)
+        {
+        uschar *arg = commands->args[i].u;
+        if (arg != NULL)
+          {
+          int len = Ustrlen(mailargs[i]);
+          int indent = (debug_selector != 0)? output_indent : 0;
+          while (len++ < 7 + indent) printf(" ");
+          printf("%s: %s%s\n", mailargs[i], string_printing(arg),
+            (commands->args[mailarg_index_expand].u != NULL &&
+              Ustrcmp(mailargs[i], "file") == 0)? " (expanded)" : "");
+          }
+        }
+      if (commands->args[mailarg_index_return].u != NULL)
+        printf("Return original message\n");
+      }
+    else
+      {
+      uschar *tt;
+      uschar *log_addr = NULL;
+      uschar *to = commands->args[mailarg_index_to].u;
+      int size = 0;
+      int ptr = 0;
+      int badflag = 0;
+
+      if (to == NULL) to = expand_string(US"$reply_address");
+      while (isspace(*to)) to++;
+
+      for (tt = to; *tt != 0; tt++)     /* Get rid of newlines */
+        if (*tt == '\n') *tt = ' ';
+
+      DEBUG(D_filter)
+        {
+        debug_printf("Filter: %smail to: %s%s%s\n",
+          (commands->seen)? "seen " : "",
+          to,
+          (commands->command == vacation_command)? " (vacation)" : "",
+          (commands->noerror)? " (noerror)" : "");
+        for (i = 1; i < MAILARGS_STRING_COUNT; i++)
+          {
+          uschar *arg = commands->args[i].u;
+          if (arg != NULL)
+            {
+            int len = Ustrlen(mailargs[i]);
+            while (len++ < 15) debug_printf(" ");
+            debug_printf("%s: %s%s\n", mailargs[i], string_printing(arg),
+              (commands->args[mailarg_index_expand].u != NULL &&
+                Ustrcmp(mailargs[i], "file") == 0)? " (expanded)" : "");
+            }
+          }
+        }
+
+      /* Create the "address" for the autoreply. This is used only for logging,
+      as the actual recipients are extracted from the To: line by -t. We use the
+      same logic here to extract the working addresses (there may be more than
+      one). Just in case there are a vast number of addresses, stop when the
+      string gets too long. */
+
+      tt = to;
+      while (*tt != 0)
+        {
+        uschar *ss = parse_find_address_end(tt, FALSE);
+        uschar *recipient, *errmess;
+        int start, end, domain;
+        int temp = *ss;
+
+        *ss = 0;
+        recipient = parse_extract_address(tt, &errmess, &start, &end, &domain,
+          FALSE);
+        *ss = temp;
+
+        /* Ignore empty addresses and errors; an error will occur later if
+        there's something really bad. */
+
+        if (recipient != NULL)
+          {
+          log_addr = string_cat(log_addr, &size, &ptr,
+            (log_addr == NULL)? US">" : US",", 1);
+          log_addr = string_cat(log_addr, &size, &ptr, recipient,
+            Ustrlen(recipient));
+          }
+
+        /* Check size */
+
+        if (ptr > 256)
+          {
+          log_addr = string_cat(log_addr, &size, &ptr, US", ...", 5);
+          break;
+          }
+
+        /* Move on past this address */
+
+        tt = ss + (*ss? 1:0);
+        while (isspace(*tt)) tt++;
+        }
+
+      if (log_addr == NULL)
+        {
+        log_addr = string_sprintf(">**bad-reply**");
+        badflag = af_bad_reply;
+        }
+      else log_addr[ptr] = 0;
+
+      addr = deliver_make_addr(log_addr, FALSE);
+      setflag(addr, (af_pfr|badflag));
+      if (commands->noerror) setflag(addr, af_ignore_error);
+      addr->next = *generated;
+      *generated = addr;
+      addr->reply = store_get(sizeof(reply_item));
+      addr->reply->from = NULL;
+      addr->reply->to = string_copy(to);
+      addr->reply->file_expand =
+        commands->args[mailarg_index_expand].u != NULL;
+      addr->reply->expand_forbid = expand_forbid;
+      addr->reply->return_message =
+        commands->args[mailarg_index_return].u != NULL;
+      addr->reply->once_repeat = 0;
+
+      if (commands->args[mailarg_index_once_repeat].u != NULL)
+        {
+        addr->reply->once_repeat =
+          readconf_readtime(commands->args[mailarg_index_once_repeat].u, 0,
+            FALSE);
+        if (addr->reply->once_repeat < 0)
+          {
+          *error_pointer = string_sprintf("Bad time value for \"once_repeat\" "
+            "in mail or vacation command: %s",
+            commands->args[mailarg_index_once_repeat]);
+          return FF_ERROR;
+          }
+        }
+
+      /* Set up all the remaining string arguments (those other than "to") */
+
+      for (i = 1; i < mailargs_string_passed; i++)
+        {
+        uschar *ss = commands->args[i].u;
+        *((uschar **)(((uschar *)(addr->reply)) + reply_offsets[i])) =
+          (ss == NULL)? NULL : string_copy(ss);
+        }
+      }
+    break;
+
+    case testprint_command:
+    if (filter_test != FTEST_NONE || (debug_selector & D_filter) != 0)
+      {
+      uschar *s = string_printing(expargs[0]);
+      if (filter_test == FTEST_NONE)
+        debug_printf("Filter: testprint: %s\n", s);
+      else
+        printf("Testprint: %s\n", s);
+      }
+    }
+
+  commands = commands->next;
+  }
+
+return filter_delivered? FF_DELIVERED : FF_NOTDELIVERED;
+}
+
+
+
+/*************************************************
+*        Test for a personal message             *
+*************************************************/
+
+/* This function is global so that it can also be called from the code that
+implements Sieve filters.
+
+Arguments:
+  aliases    a chain of aliases
+  scan_cc    TRUE if Cc: and Bcc: are to be scanned (Exim filters do not)
+
+Returns:     TRUE if the message is deemed to be personal
+*/
+
+BOOL
+filter_personal(string_item *aliases, BOOL scan_cc)
+{
+uschar *self, *self_from, *self_to;
+uschar *psself = NULL, *psself_from = NULL, *psself_to = NULL;
+void *reset_point = store_get(0);
+BOOL yield;
+header_line *h;
+int to_count = 2;
+int from_count = 9;
+
+/* If any header line in the message is a defined "List-" header field, it is
+not a personal message. We used to check for any header line that started with
+"List-", but this was tightened up for release 4.54. The check is now for
+"List-Id", defined in RFC 2929, or "List-Help", "List-Subscribe", "List-
+Unsubscribe", "List-Post", "List-Owner" or "List-Archive", all of which are
+defined in RFC 2369. We also scan for "Auto-Submitted"; if it is found to
+contain any value other than "no", the message is not personal (RFC 3834).
+Previously the test was for "auto-". */
+
+for (h = header_list; h != NULL; h = h->next)
+  {
+  uschar *s;
+  if (h->type == htype_old) continue;
+
+  if (strncmpic(h->text, US"List-", 5) == 0)
+    {
+    s = h->text + 5;
+    if (strncmpic(s, US"Id:", 3) == 0 ||
+        strncmpic(s, US"Help:", 5) == 0 ||
+        strncmpic(s, US"Subscribe:", 10) == 0 ||
+        strncmpic(s, US"Unsubscribe:", 12) == 0 ||
+        strncmpic(s, US"Post:", 5) == 0 ||
+        strncmpic(s, US"Owner:", 6) == 0 ||
+        strncmpic(s, US"Archive:", 8) == 0)
+      return FALSE;
+    }
+
+  else if (strncmpic(h->text, US"Auto-submitted:", 15) == 0)
+    {
+    s = h->text + 15;
+    while (isspace(*s)) s++;
+    if (strncmpic(s, US"no", 2) != 0) return FALSE;
+    s += 2;
+    while (isspace(*s)) s++;
+    if (*s != 0) return FALSE;
+    }
+  }
+
+/* Set up "my" address */
+
+self = string_sprintf("%s@%s", deliver_localpart, deliver_domain);
+self_from = rewrite_one(self, rewrite_from, NULL, FALSE, US"",
+  global_rewrite_rules);
+self_to   = rewrite_one(self, rewrite_to, NULL, FALSE, US"",
+  global_rewrite_rules);
+
+
+if (self_from == NULL) self_from = self;
+if (self_to == NULL) self_to = self;
+
+/* If there's a prefix or suffix set, we must include the prefixed/
+suffixed version of the local part in the tests. */
+
+if (deliver_localpart_prefix != NULL || deliver_localpart_suffix != NULL)
+  {
+  psself = string_sprintf("%s%s%s@%s",
+    (deliver_localpart_prefix == NULL)? US"" : deliver_localpart_prefix,
+    deliver_localpart,
+    (deliver_localpart_suffix == NULL)? US"" : deliver_localpart_suffix,
+    deliver_domain);
+  psself_from = rewrite_one(psself, rewrite_from, NULL, FALSE, US"",
+    global_rewrite_rules);
+  psself_to   = rewrite_one(psself, rewrite_to, NULL, FALSE, US"",
+    global_rewrite_rules);
+  if (psself_from == NULL) psself_from = psself;
+  if (psself_to == NULL) psself_to = psself;
+  to_count += 2;
+  from_count += 2;
+  }
+
+/* Do all the necessary tests; the counts are adjusted for {pre,suf}fix */
+
+yield =
+  (
+  header_match(US"to:", TRUE, TRUE, aliases, to_count, self, self_to, psself,
+               psself_to) ||
+    (scan_cc &&
+       (
+       header_match(US"cc:", TRUE, TRUE, aliases, to_count, self, self_to,
+                             psself, psself_to)
+       ||
+       header_match(US"bcc:", TRUE, TRUE, aliases, to_count, self, self_to,
+                              psself, psself_to)
+       )
+    )
+  ) &&
+
+  header_match(US"from:", TRUE, FALSE, aliases, from_count, "^server@",
+    "^daemon@", "^root@", "^listserv@", "^majordomo@", "^.*?-request@",
+    "^owner-[^@]+@", self, self_from, psself, psself_from) &&
+
+  header_match(US"precedence:", FALSE, FALSE, NULL, 3, "bulk","list","junk") &&
+
+  (sender_address == NULL || sender_address[0] != 0);
+
+store_reset(reset_point);
+return yield;
+}
+
+
+
+/*************************************************
+*            Interpret a mail filter file        *
+*************************************************/
+
+/*
+Arguments:
+  filter      points to the entire file, read into store as a single string
+  options     controls whether various special things are allowed, and requests
+              special actions
+  generated   where to hang newly-generated addresses
+  error       where to pass back an error text
+
+Returns:      FF_DELIVERED     success, a significant action was taken
+              FF_NOTDELIVERED  success, no significant action
+              FF_DEFER         defer requested
+              FF_FAIL          fail requested
+              FF_FREEZE        freeze requested
+              FF_ERROR         there was a problem
+*/
+
+int
+filter_interpret(uschar *filter, int options, address_item **generated,
+  uschar **error)
+{
+int i;
+int yield = FF_ERROR;
+uschar *ptr = filter;
+uschar *save_headers_charset = headers_charset;
+filter_cmd *commands = NULL;
+filter_cmd **lastcmdptr = &commands;
+
+DEBUG(D_route) debug_printf("Filter: start of processing\n");
+
+/* Initialize "not in an if command", set the global flag that is always TRUE
+while filtering, and zero the variables. */
+
+expect_endif = 0;
+output_indent = 0;
+filter_running = TRUE;
+for (i = 0; i < FILTER_VARIABLE_COUNT; i++) filter_n[i] = 0;
+
+/* To save having to pass certain values about all the time, make them static.
+Also initialize the line number, for error messages, and the log file
+variables. */
+
+filter_options = options;
+filter_delivered = FALSE;
+finish_obeyed = FALSE;
+error_pointer = error;
+*error_pointer = NULL;
+line_number = 1;
+log_fd = -1;
+log_mode = 0600;
+log_filename = NULL;
+
+/* Scan filter file for syntax and build up an interpretation thereof, and
+interpret the compiled commands, and if testing, say whether we ended up
+delivered or not, unless something went wrong. */
+
+seen_force = FALSE;
+ptr = nextsigchar(ptr, TRUE);
+
+if (read_command_list(&ptr, &lastcmdptr, FALSE))
+  yield = interpret_commands(commands, generated);
+
+if (filter_test != FTEST_NONE || (debug_selector & D_filter) != 0)
+  {
+  uschar *s = US"";
+  switch(yield)
+    {
+    case FF_DEFER:
+    s = US"Filtering ended by \"defer\".";
+    break;
+
+    case FF_FREEZE:
+    s = US"Filtering ended by \"freeze\".";
+    break;
+
+    case FF_FAIL:
+    s = US"Filtering ended by \"fail\".";
+    break;
+
+    case FF_DELIVERED:
+    s = US"Filtering set up at least one significant delivery "
+           "or other action.\n"
+           "No other deliveries will occur.";
+    break;
+
+    case FF_NOTDELIVERED:
+    s = US"Filtering did not set up a significant delivery.\n"
+           "Normal delivery will occur.";
+    break;
+
+    case FF_ERROR:
+    s = string_sprintf("Filter error: %s", *error);
+    break;
+    }
+
+  if (filter_test != FTEST_NONE) printf("%s\n", CS s);
+    else debug_printf("%s\n", s);
+  }
+
+/* Close the log file if it was opened, and kill off any numerical variables
+before returning. Reset the header decoding charset. */
+
+if (log_fd >= 0) (void)close(log_fd);
+expand_nmax = -1;
+filter_running = FALSE;
+headers_charset = save_headers_charset;
+
+DEBUG(D_route) debug_printf("Filter: end of processing\n");
+return yield;
+}
+
+
+/* End of filter.c */
diff --git a/src/filtertest.c b/src/filtertest.c
new file mode 100644 (file)
index 0000000..7245608
--- /dev/null
@@ -0,0 +1,281 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+
+/* Code for the filter test function. */
+
+#include "exim.h"
+
+
+
+/*************************************************
+*    Read message and set body/size variables    *
+*************************************************/
+
+/* We have to read the remainder of the message in order to find its size, so
+we can set up the message_body variables at the same time (in normal use, the
+message_body variables are not set up unless needed). The reading code is
+written out here rather than having options in read_message_data, in order to
+keep that function as efficient as possible. (Later: this function is now
+global because it is also used by the -bem testing option.) Handling
+message_body_end is somewhat more tedious. Pile it all into a circular buffer
+and sort out at the end.
+
+Arguments:
+  dot_ended   TRUE if message already terminated by '.'
+
+Returns:      nothing
+*/
+
+void
+read_message_body(BOOL dot_ended)
+{
+register int ch;
+int body_len, body_end_len, header_size;
+uschar *s;
+
+message_body = store_malloc(message_body_visible + 1);
+message_body_end = store_malloc(message_body_visible + 1);
+s = message_body_end;
+body_len = 0;
+body_linecount = 0;
+header_size = message_size;
+
+if (!dot_ended && !feof(stdin))
+  {
+  if (!dot_ends)
+    {
+    while ((ch = getc(stdin)) != EOF)
+      {
+      if (ch == 0) body_zerocount++;
+      if (ch == '\n') body_linecount++;
+      if (body_len < message_body_visible) message_body[body_len++] = ch;
+      *s++ = ch;
+      if (s > message_body_end + message_body_visible) s = message_body_end;
+      message_size++;
+      }
+    }
+  else
+    {
+    int ch_state = 1;
+    while ((ch = getc(stdin)) != EOF)
+      {
+      if (ch == 0) body_zerocount++;
+      switch (ch_state)
+        {
+        case 0:                         /* Normal state */
+        if (ch == '\n') { body_linecount++; ch_state = 1; }
+        break;
+
+        case 1:                         /* After "\n" */
+        if (ch == '.')
+          {
+          ch_state = 2;
+          continue;
+          }
+        if (ch != '\n') ch_state = 0;
+        break;
+
+        case 2:                         /* After "\n." */
+        if (ch == '\n') goto READ_END;
+        if (body_len < message_body_visible) message_body[body_len++] = '.';
+        *s++ = '.';
+        if (s > message_body_end + message_body_visible)
+          s = message_body_end;
+        message_size++;
+        ch_state = 0;
+        break;
+        }
+      if (body_len < message_body_visible) message_body[body_len++] = ch;
+      *s++ = ch;
+      if (s > message_body_end + message_body_visible) s = message_body_end;
+      message_size++;
+      }
+    READ_END: ch = ch;  /* Some compilers don't like null statements */
+    }
+  if (s == message_body_end || s[-1] != '\n') body_linecount++;
+  }
+
+message_body[body_len] = 0;
+message_body_size = message_size - header_size;
+
+/* body_len stops at message_body_visible; it if got there, we may have
+wrapped round in message_body_end. */
+
+if (body_len >= message_body_visible)
+  {
+  int below = s - message_body_end;
+  int above = message_body_visible - below;
+  if (above > 0)
+    {
+    uschar *temp = store_get(below);
+    memcpy(temp, message_body_end, below);
+    memmove(message_body_end, s+1, above);
+    memcpy(message_body_end + above, temp, below);
+    s = message_body_end + message_body_visible;
+    }
+  }
+
+*s = 0;
+body_end_len = s - message_body_end;
+
+/* Convert newlines and nulls in the body variables to spaces */
+
+while (body_len > 0)
+  {
+  if (message_body[--body_len] == '\n' || message_body[body_len] == 0)
+    message_body[body_len] = ' ';
+  }
+
+while (body_end_len > 0)
+  {
+  if (message_body_end[--body_end_len] == '\n' ||
+      message_body_end[body_end_len] == 0)
+    message_body_end[body_end_len] = ' ';
+  }
+}
+
+
+
+/*************************************************
+*            Test a mail filter                  *
+*************************************************/
+
+/* This is called when exim is run with the -bf option. At this point it is
+running under an unprivileged uid/gid. A test message's headers have been read
+into store, and the body of the message is still accessible on the standard
+input if this is the first time this function has been called. It may be called
+twice if both system and user filters are being tested.
+
+Argument:
+  fd          an fd containing the filter file
+  filename    the name of the filter file
+  is_system   TRUE if testing is to be as a system filter
+  dot_ended   TRUE if message already terminated by '.'
+
+Returns:      TRUE if no errors
+*/
+
+BOOL
+filter_runtest(int fd, uschar *filename, BOOL is_system, BOOL dot_ended)
+{
+int rc, filter_type;
+BOOL yield;
+struct stat statbuf;
+address_item *generated = NULL;
+uschar *error, *filebuf;
+
+/* Read the filter file into store as will be done by the router in a real
+case. */
+
+if (fstat(fd, &statbuf) != 0)
+  {
+  printf("exim: failed to get size of %s: %s\n", filename, strerror(errno));
+  return FALSE;
+  }
+
+filebuf = store_get(statbuf.st_size + 1);
+rc = read(fd, filebuf, statbuf.st_size);
+(void)close(fd);
+
+if (rc != statbuf.st_size)
+  {
+  printf("exim: error while reading %s: %s\n", filename, strerror(errno));
+  return FALSE;
+  }
+
+filebuf[statbuf.st_size] = 0;
+
+/* Check the filter type. User filters start with "# Exim filter" or "# Sieve
+filter". If the filter type is not recognized, the file is treated as an
+ordinary .forward file. System filters do not need the "# Exim filter" in order
+to be recognized as Exim filters. */
+
+filter_type = rda_is_filter(filebuf);
+if (is_system && filter_type == FILTER_FORWARD) filter_type = FILTER_EXIM;
+
+printf("Testing %s file \"%s\"\n\n",
+  (filter_type == FILTER_EXIM)? "Exim filter" :
+  (filter_type == FILTER_SIEVE)? "Sieve filter" :
+  "forward file",
+  filename);
+
+/* Handle a plain .forward file */
+
+if (filter_type == FILTER_FORWARD)
+  {
+  yield = parse_forward_list(filebuf,
+    RDO_REWRITE,
+    &generated,                     /* for generated addresses */
+    &error,                         /* for errors */
+    deliver_domain,                 /* incoming domain for \name */
+    NULL,                           /* no check on includes */
+    NULL);                          /* fail on syntax errors */
+
+  switch(yield)
+    {
+    case FF_FAIL:
+    printf("exim: forward file contains \":fail:\"\n");
+    break;
+
+    case FF_BLACKHOLE:
+    printf("exim: forwardfile contains \":blackhole:\"\n");
+    break;
+
+    case FF_ERROR:
+    printf("exim: error in forward file: %s\n", error);
+    return FALSE;
+    }
+
+  if (generated == NULL)
+    printf("exim: no addresses generated from forward file\n");
+
+  else
+    {
+    printf("exim: forward file generated:\n");
+    while (generated != NULL)
+      {
+      printf("  %s\n", generated->address);
+      generated = generated->next;
+      }
+    }
+
+  return TRUE;
+  }
+
+/* For a filter, set up the message_body variables and the message size if this
+is the first time this function has been called. */
+
+if (message_body == NULL) read_message_body(dot_ended);
+
+/* Now pass the filter file to the function that interprets it. Because
+filter_test is not FILTER_NONE, the interpreter will output comments about what
+it is doing. No need to clean up store. Indeed, we must not, because we may be
+testing a system filter that is going to be followed by a user filter test. */
+
+if (is_system)
+  {
+  system_filtering = TRUE;
+  enable_dollar_recipients = TRUE; /* Permit $recipients in system filter */
+  yield = filter_interpret
+    (filebuf,
+    RDO_DEFER|RDO_FAIL|RDO_FILTER|RDO_FREEZE|RDO_REWRITE, &generated, &error);
+  enable_dollar_recipients = FALSE;
+  system_filtering = FALSE;
+  }
+else
+  {
+  yield = (filter_type == FILTER_SIEVE)?
+    sieve_interpret(filebuf, RDO_REWRITE, NULL, NULL, NULL, NULL, &generated, &error)
+    :
+    filter_interpret(filebuf, RDO_REWRITE, &generated, &error);
+  }
+
+return yield != FF_ERROR;
+}
+
+/* End of filtertest.c */
diff --git a/src/functions.h b/src/functions.h
new file mode 100644 (file)
index 0000000..a6257a9
--- /dev/null
@@ -0,0 +1,447 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2014 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+
+/* Prototypes for functions that appear in various modules. Gathered together
+to avoid having a lot of tiddly little headers with only a couple of lines in
+them. However, some functions that are used (or not used) by utility programs
+are in in fact in separate headers. */
+
+
+#ifdef EXIM_PERL
+extern uschar *call_perl_cat(uschar *, int *, int *, uschar **, uschar *,
+                 uschar **);
+extern void    cleanup_perl(void);
+extern uschar *init_perl(uschar *);
+#endif
+
+
+#ifdef SUPPORT_TLS
+extern const char *
+               std_dh_prime_default(void);
+extern const char *
+               std_dh_prime_named(const uschar *);
+
+extern uschar * tls_cert_crl_uri(void *, uschar * mod);
+extern uschar * tls_cert_ext_by_oid(void *, uschar *, int);
+extern uschar * tls_cert_issuer(void *, uschar * mod);
+extern uschar * tls_cert_not_before(void *, uschar * mod);
+extern uschar * tls_cert_not_after(void *, uschar * mod);
+extern uschar * tls_cert_ocsp_uri(void *, uschar * mod);
+extern uschar * tls_cert_serial_number(void *, uschar * mod);
+extern uschar * tls_cert_signature(void *, uschar * mod);
+extern uschar * tls_cert_signature_algorithm(void *, uschar * mod);
+extern uschar * tls_cert_subject(void *, uschar * mod);
+extern uschar * tls_cert_subject_altname(void *, uschar * mod);
+extern uschar * tls_cert_version(void *, uschar * mod);
+
+extern uschar * tls_cert_fprt_md5(void *);
+extern uschar * tls_cert_fprt_sha1(void *);
+extern uschar * tls_cert_fprt_sha256(void *);
+
+extern int     tls_client_start(int, host_item *, address_item *,
+                void *);
+extern void    tls_close(BOOL, BOOL);
+extern int     tls_export_cert(uschar *, size_t, void *);
+extern int     tls_feof(void);
+extern int     tls_ferror(void);
+extern void    tls_free_cert(void *);
+extern int     tls_getc(void);
+extern int     tls_import_cert(const uschar *, void **);
+extern int     tls_read(BOOL, uschar *, size_t);
+extern int     tls_server_start(const uschar *);
+extern BOOL    tls_smtp_buffered(void);
+extern int     tls_ungetc(int);
+extern int     tls_write(BOOL, const uschar *, size_t);
+extern uschar *tls_validate_require_cipher(void);
+extern void    tls_version_report(FILE *);
+# ifndef USE_GNUTLS
+extern BOOL    tls_openssl_options_parse(uschar *, long *);
+# endif
+extern uschar * tls_field_from_dn(uschar *, uschar *);
+# ifdef EXPERIMENTAL_CERTNAMES
+extern BOOL    tls_is_name_for_cert(uschar *, void *);
+# endif
+#endif /*SUPPORT_TLS*/
+
+
+/* Everything else... */
+
+extern acl_block *acl_read(uschar *(*)(void), uschar **);
+extern int     acl_check(int, uschar *, uschar *, uschar **, uschar **);
+extern int     acl_eval(int, uschar *, uschar **, uschar **);
+
+extern tree_node *acl_var_create(uschar *);
+extern void    acl_var_write(uschar *, uschar *, void *);
+extern uschar *auth_b64encode(uschar *, int);
+extern int     auth_b64decode(uschar *, uschar **);
+extern int     auth_call_pam(uschar *, uschar **);
+extern int     auth_call_pwcheck(uschar *, uschar **);
+extern int     auth_call_radius(uschar *, uschar **);
+extern int     auth_call_saslauthd(uschar *, uschar *, uschar *, uschar *,
+                 uschar **);
+extern int     auth_check_serv_cond(auth_instance *);
+extern int     auth_check_some_cond(auth_instance *, uschar *, uschar *, int);
+
+extern int     auth_get_data(uschar **, uschar *, int);
+extern int     auth_get_no64_data(uschar **, uschar *);
+extern uschar *auth_xtextencode(uschar *, int);
+extern int     auth_xtextdecode(uschar *, uschar **);
+
+extern void    cancel_cutthrough_connection(const char *);
+extern int     check_host(void *, uschar *, uschar **, uschar **);
+extern uschar **child_exec_exim(int, BOOL, int *, BOOL, int, ...);
+extern pid_t   child_open_uid(uschar **, uschar **, int, uid_t *, gid_t *,
+                 int *, int *, uschar *, BOOL);
+extern uschar *cutthrough_finaldot(void);
+extern BOOL    cutthrough_flush_send(void);
+extern BOOL    cutthrough_headers_send(void);
+extern BOOL    cutthrough_predata(void);
+extern BOOL    cutthrough_puts(uschar *, int);
+extern BOOL    cutthrough_put_nl(void);
+
+extern void    daemon_go(void);
+
+#ifdef EXPERIMENTAL_DCC
+extern int     dcc_process(uschar **);
+#endif
+
+extern void    debug_logging_activate(uschar *, uschar *);
+extern void    debug_print_argv(uschar **);
+extern void    debug_print_ids(uschar *);
+extern void    debug_print_string(uschar *);
+extern void    debug_print_tree(tree_node *);
+extern void    debug_vprintf(const char *, va_list);
+extern void    decode_bits(unsigned int *, unsigned int *,
+                  int, int, uschar *, bit_table *, int, uschar *, int);
+extern address_item *deliver_make_addr(uschar *, BOOL);
+extern void    delivery_log(int, address_item *, int, uschar *);
+extern int     deliver_message(uschar *, BOOL, BOOL);
+extern void    deliver_msglog(const char *, ...) PRINTF_FUNCTION(1,2);
+extern void    deliver_set_expansions(address_item *);
+extern int     deliver_split_address(address_item *);
+extern void    deliver_succeeded(address_item *);
+#ifdef WITH_OLD_DEMIME
+extern int     demime(uschar **);
+#endif
+extern BOOL    directory_make(const uschar *, const uschar *, int, BOOL);
+#ifndef DISABLE_DKIM
+extern BOOL    dkim_transport_write_message(address_item *, int, int,
+                   int, uschar *, uschar *, uschar *, uschar *, rewrite_rule *,
+                   int, uschar *, uschar *, uschar *, uschar *, uschar *, uschar *);
+#endif
+extern dns_address *dns_address_from_rr(dns_answer *, dns_record *);
+extern void    dns_build_reverse(uschar *, uschar *);
+extern void    dns_init(BOOL, BOOL, BOOL);
+extern int     dns_basic_lookup(dns_answer *, uschar *, int);
+extern BOOL    dns_is_secure(dns_answer *);
+extern int     dns_lookup(dns_answer *, uschar *, int, uschar **);
+extern int     dns_special_lookup(dns_answer *, uschar *, int, uschar **);
+extern dns_record *dns_next_rr(dns_answer *, dns_scan *, int);
+extern uschar *dns_text_type(int);
+extern void    dscp_list_to_stream(FILE *);
+extern BOOL    dscp_lookup(const uschar *, int, int *, int *, int *);
+
+extern void    enq_end(uschar *);
+extern BOOL    enq_start(uschar *);
+extern void    exim_exit(int);
+extern void    exim_nullstd(void);
+extern void    exim_setugid(uid_t, gid_t, BOOL, uschar *);
+extern int     exim_tvcmp(struct timeval *, struct timeval *);
+extern void    exim_wait_tick(struct timeval *, int);
+extern BOOL    expand_check_condition(uschar *, uschar *, uschar *);
+extern uschar *expand_string(uschar *);
+extern uschar *expand_string_copy(uschar *);
+extern int_eximarith_t expand_string_integer(uschar *, BOOL);
+extern void    modify_variable(uschar *, void *);
+
+extern int     filter_interpret(uschar *, int, address_item **, uschar **);
+extern BOOL    filter_personal(string_item *, BOOL);
+extern BOOL    filter_runtest(int, uschar *, BOOL, BOOL);
+extern BOOL    filter_system_interpret(address_item **, uschar **);
+
+extern uschar * fn_hdrs_added(void);
+
+extern void    header_add(int, const char *, ...);
+extern int     header_checkname(header_line *, BOOL);
+extern BOOL    header_match(uschar *, BOOL, BOOL, string_item *, int, ...);
+extern int     host_address_extract_port(uschar *);
+extern uschar *host_and_ident(BOOL);
+extern int     host_aton(uschar *, int *);
+extern void    host_build_hostlist(host_item **, uschar *, BOOL);
+extern ip_address_item *host_build_ifacelist(uschar *, uschar *);
+extern void    host_build_log_info(void);
+extern void    host_build_sender_fullhost(void);
+extern BOOL    host_find_byname(host_item *, uschar *, int, uschar **, BOOL);
+extern int     host_find_bydns(host_item *, uschar *, int, uschar *, uschar *,
+                 uschar *, uschar *, uschar *, uschar **, BOOL *);
+extern ip_address_item *host_find_interfaces(void);
+extern BOOL    host_is_in_net(uschar *, uschar *, int);
+extern BOOL    host_is_tls_on_connect_port(int);
+extern int     host_item_get_port(host_item *);
+extern void    host_mask(int, int *, int);
+extern int     host_name_lookup(void);
+extern int     host_nmtoa(int, int *, int, uschar *, int);
+extern uschar *host_ntoa(int, const void *, uschar *, int *);
+extern int     host_scan_for_local_hosts(host_item *, host_item **, BOOL *);
+
+extern void    invert_address(uschar *, uschar *);
+extern int     ip_bind(int, int, uschar *, int);
+extern int     ip_connect(int, int, uschar *, int, int);
+extern int     ip_connectedsocket(int, const uschar *, int, int,
+                 int, host_item *, uschar **);
+extern int     ip_get_address_family(int);
+extern void    ip_keepalive(int, uschar *, BOOL);
+extern int     ip_recv(int, uschar *, int, int);
+extern int     ip_socket(int, int);
+
+extern uschar *local_part_quote(uschar *);
+extern int     log_create(uschar *);
+extern int     log_create_as_exim(uschar *);
+extern void    log_close_all(void);
+
+#ifdef WITH_CONTENT_SCAN
+extern int     malware(uschar **);
+extern int     malware_in_file(uschar *);
+#endif
+extern int     match_address_list(uschar *, BOOL, BOOL, uschar **,
+                 unsigned int *, int, int, uschar **);
+extern int     match_check_list(uschar **, int, tree_node **, unsigned int **,
+                 int(*)(void *, uschar *, uschar **, uschar **), void *, int,
+                 uschar *, uschar **);
+extern int     match_isinlist(uschar *, uschar **, int, tree_node **,
+                 unsigned int *, int, BOOL, uschar **);
+extern int     match_check_string(uschar *, uschar *, int, BOOL, BOOL, BOOL,
+                 uschar **);
+extern void    md5_end(md5 *, const uschar *, int, uschar *);
+extern void    md5_mid(md5 *, const uschar *);
+extern void    md5_start(md5 *);
+extern void    millisleep(int);
+#ifdef WITH_CONTENT_SCAN
+struct mime_boundary_context;
+extern int     mime_acl_check(uschar *acl, FILE *f,
+                 struct mime_boundary_context *, uschar **, uschar **);
+extern int     mime_decode(uschar **);
+extern int     mime_regex(uschar **);
+#endif
+extern uschar *moan_check_errorcopy(uschar *);
+extern BOOL    moan_skipped_syntax_errors(uschar *, error_block *, uschar *,
+                 BOOL, uschar *);
+extern void    moan_smtp_batch(uschar *, const char *, ...) PRINTF_FUNCTION(2,3);
+extern void    moan_tell_someone(uschar *, address_item *,
+                 const uschar *, const char *, ...) PRINTF_FUNCTION(4,5);
+extern BOOL    moan_to_sender(int, error_block *, header_line *, FILE *, BOOL);
+extern void    moan_write_from(FILE *);
+extern FILE   *modefopen(const uschar *, const char *, mode_t);
+
+extern void   open_cutthrough_connection( address_item * addr );
+
+extern uschar *parse_extract_address(uschar *, uschar **, int *, int *, int *,
+                 BOOL);
+extern int     parse_forward_list(uschar *, int, address_item **, uschar **,
+                 uschar *, uschar *, error_block **);
+extern uschar *parse_find_address_end(uschar *, BOOL);
+extern uschar *parse_find_at(uschar *);
+extern uschar *parse_fix_phrase(uschar *, int, uschar *, int);
+extern uschar *parse_message_id(uschar *, uschar **, uschar **);
+extern uschar *parse_quote_2047(uschar *, int, uschar *, uschar *, int, BOOL);
+extern uschar *parse_date_time(uschar *str, time_t *t);
+extern int     vaguely_random_number(int);
+#ifdef SUPPORT_TLS
+extern int     vaguely_random_number_fallback(int);
+#endif
+
+extern BOOL    queue_action(uschar *, int, uschar **, int, int);
+extern void    queue_check_only(void);
+extern void    queue_list(int, uschar **, int);
+extern void    queue_count(void);
+extern void    queue_run(uschar *, uschar *, BOOL);
+
+extern int     random_number(int);
+#ifdef WITH_CONTENT_SCAN
+extern int     recv_line(int, uschar *, int);
+#endif
+extern int     rda_interpret(redirect_block *, int, uschar *, uschar *,
+                 uschar *, uschar *, uschar *, ugid_block *, address_item **,
+                 uschar **, error_block **, int *, uschar *);
+extern int     rda_is_filter(const uschar *);
+extern BOOL    readconf_depends(driver_instance *, uschar *);
+extern void    readconf_driver_init(uschar *, driver_instance **,
+                 driver_info *, int, void *, int, optionlist *, int);
+extern uschar *readconf_find_option(void *);
+extern void    readconf_main(void);
+extern void    readconf_print(uschar *, uschar *, BOOL);
+extern uschar *readconf_printtime(int);
+extern uschar *readconf_readname(uschar *, int, uschar *);
+extern int     readconf_readtime(uschar *, int, BOOL);
+extern void    readconf_rest();
+extern uschar *readconf_retry_error(uschar *, uschar *, int *, int *);
+extern void    read_message_body(BOOL);
+extern void    receive_bomb_out(uschar *, uschar *);
+extern BOOL    receive_check_fs(int);
+extern BOOL    receive_check_set_sender(uschar *);
+extern BOOL    receive_msg(BOOL);
+extern int     receive_statvfs(BOOL, int *);
+extern void    receive_swallow_smtp(void);
+#ifdef WITH_CONTENT_SCAN
+extern int     regex(uschar **);
+#endif
+extern BOOL    regex_match_and_setup(const pcre *, uschar *, int, int);
+extern const pcre *regex_must_compile(uschar *, BOOL, BOOL);
+extern void    retry_add_item(address_item *, uschar *, int);
+extern BOOL    retry_check_address(uschar *, host_item *, uschar *, BOOL,
+                 uschar **, uschar **);
+extern retry_config *retry_find_config(uschar *, uschar *, int, int);
+extern BOOL    retry_ultimate_address_timeout(uschar *, uschar *,
+                 dbdata_retry *, time_t);
+extern void    retry_update(address_item **, address_item **, address_item **);
+extern uschar *rewrite_address(uschar *, BOOL, BOOL, rewrite_rule *, int);
+extern uschar *rewrite_address_qualify(uschar *, BOOL);
+extern header_line *rewrite_header(header_line *, uschar *, uschar *,
+               rewrite_rule *, int, BOOL);
+extern uschar *rewrite_one(uschar *, int, BOOL *, BOOL, uschar *,
+                 rewrite_rule *);
+extern void    rewrite_test(uschar *);
+extern uschar *rfc2047_decode2(uschar *, BOOL, uschar *, int, int *, int *,
+                 uschar **);
+extern int     route_address(address_item *, address_item **, address_item **,
+                 address_item **, address_item **, int);
+extern int     route_check_prefix(uschar *, uschar *);
+extern int     route_check_suffix(uschar *, uschar *);
+extern BOOL    route_findgroup(uschar *, gid_t *);
+extern BOOL    route_finduser(uschar *, struct passwd **, uid_t *);
+extern BOOL    route_find_expanded_group(uschar *, uschar *, uschar *, gid_t *,
+                 uschar **);
+extern BOOL    route_find_expanded_user(uschar *, uschar *, uschar *,
+                 struct passwd **, uid_t *, uschar **);
+extern void    route_init(void);
+extern void    route_tidyup(void);
+
+extern uschar *search_find(void *, uschar *, uschar *, int, uschar *, int,
+                 int, int *);
+extern int     search_findtype(uschar *, int);
+extern int     search_findtype_partial(uschar *, int *, uschar **, int *,
+                 int *);
+extern void   *search_open(uschar *, int, int, uid_t *, gid_t *);
+extern void    search_tidyup(void);
+extern void    set_process_info(const char *, ...) PRINTF_FUNCTION(1,2);
+extern void    sha1_end(sha1 *, const uschar *, int, uschar *);
+extern void    sha1_mid(sha1 *, const uschar *);
+extern void    sha1_start(sha1 *);
+extern int     sieve_interpret(uschar *, int, uschar *, uschar *, uschar *,
+                 uschar *, address_item **, uschar **);
+extern void    sigalrm_handler(int);
+extern BOOL    smtp_buffered(void);
+extern void    smtp_closedown(uschar *);
+extern int     smtp_connect(host_item *, int, int, uschar *, int, BOOL, const uschar *);
+extern int     smtp_feof(void);
+extern int     smtp_ferror(void);
+extern uschar *smtp_get_connection_info(void);
+extern BOOL    smtp_get_interface(uschar *, int, address_item *, BOOL *,
+                 uschar **, uschar *);
+extern BOOL    smtp_get_port(uschar *, address_item *, int *, uschar *);
+extern int     smtp_getc(void);
+extern int     smtp_handle_acl_fail(int, int, uschar *, uschar *);
+extern void    smtp_log_no_mail(void);
+extern void    smtp_message_code(uschar **, int *, uschar **, uschar **);
+extern BOOL    smtp_read_response(smtp_inblock *, uschar *, int, int, int);
+extern void    smtp_respond(uschar *, int, BOOL, uschar *);
+extern void    smtp_notquit_exit(uschar *, uschar *, uschar *, ...);
+extern void    smtp_send_prohibition_message(int, uschar *);
+extern int     smtp_setup_msg(void);
+extern BOOL    smtp_start_session(void);
+extern int     smtp_ungetc(int);
+extern BOOL    smtp_verify_helo(void);
+extern int     smtp_write_command(smtp_outblock *, BOOL, const char *, ...) PRINTF_FUNCTION(3,4);
+#ifdef WITH_CONTENT_SCAN
+extern int     spam(uschar **);
+extern FILE   *spool_mbox(unsigned long *, uschar *);
+#endif
+extern BOOL    spool_move_message(uschar *, uschar *, uschar *, uschar *);
+extern BOOL    spool_open_datafile(uschar *);
+extern int     spool_open_temp(uschar *);
+extern int     spool_read_header(uschar *, BOOL, BOOL);
+extern int     spool_write_header(uschar *, int, uschar **);
+extern int     stdin_getc(void);
+extern int     stdin_feof(void);
+extern int     stdin_ferror(void);
+extern int     stdin_ungetc(int);
+extern uschar *string_append(uschar *, int *, int *, int, ...);
+extern uschar *string_append_listele(uschar *, uschar, const uschar *);
+extern uschar *string_base62(unsigned long int);
+extern uschar *string_cat(uschar *, int *, int *, const uschar *, int);
+extern uschar *string_copy_dnsdomain(uschar *);
+extern uschar *string_copy_malloc(uschar *);
+extern uschar *string_copylc(uschar *);
+extern uschar *string_copynlc(uschar *, int);
+extern uschar *string_dequote(uschar **);
+extern BOOL    string_format(uschar *, int, const char *, ...) ALMOST_PRINTF(3,4);
+extern uschar *string_format_size(int, uschar *);
+extern int     string_interpret_escape(uschar **);
+extern int     string_is_ip_address(const uschar *, int *);
+extern uschar *string_log_address(address_item *, BOOL, BOOL);
+extern uschar *string_nextinlist(uschar **, int *, uschar *, int);
+extern uschar *string_open_failed(int, const char *, ...) PRINTF_FUNCTION(2,3);
+extern uschar *string_printing2(uschar *, BOOL);
+extern uschar *string_split_message(uschar *);
+extern uschar *string_unprinting(uschar *);
+extern BOOL    string_vformat(uschar *, int, const char *, va_list);
+extern int     strcmpic(const uschar *, const uschar *);
+extern int     strncmpic(const uschar *, const uschar *, int);
+extern uschar *strstric(uschar *, uschar *, BOOL);
+
+extern uschar *tod_stamp(int);
+extern void    tls_modify_variables(tls_support *);
+extern BOOL    transport_check_waiting(uschar *, uschar *, int, uschar *,
+                 BOOL *);
+extern void    transport_init(void);
+extern BOOL    transport_pass_socket(uschar *, uschar *, uschar *, uschar *,
+                 int);
+extern uschar *transport_rcpt_address(address_item *, BOOL);
+extern BOOL    transport_set_up_command(uschar ***, uschar *, BOOL, int,
+                 address_item *, uschar *, uschar **);
+extern void    transport_update_waiting(host_item *, uschar *);
+extern BOOL    transport_write_block(int, uschar *, int);
+extern BOOL    transport_write_string(int, const char *, ...);
+extern BOOL    transport_headers_send(address_item *, int, uschar *, uschar *,
+                 BOOL (*)(int, uschar *, int, BOOL), BOOL, rewrite_rule *, int);
+extern BOOL    transport_write_message(address_item *, int, int, int, uschar *,
+                 uschar *, uschar *, uschar *, rewrite_rule *, int);
+extern void    tree_add_duplicate(uschar *, address_item *);
+extern void    tree_add_nonrecipient(uschar *);
+extern void    tree_add_unusable(host_item *);
+extern int     tree_insertnode(tree_node **, tree_node *);
+extern tree_node *tree_search(tree_node *, uschar *);
+extern void    tree_write(tree_node *, FILE *);
+extern void    tree_walk(tree_node *, void (*)(uschar*, uschar*, void*), void *);
+
+#ifdef WITH_CONTENT_SCAN
+extern void    unspool_mbox(void);
+#endif
+
+extern int     verify_address(address_item *, FILE *, int, int, int, int,
+                 uschar *, uschar *, BOOL *);
+extern int     verify_check_dnsbl(uschar **);
+extern int     verify_check_header_address(uschar **, uschar **, int, int, int,
+                 uschar *, uschar *, int, int *);
+extern int     verify_check_headers(uschar **);
+extern int     verify_check_header_names_ascii(uschar **);
+extern int     verify_check_host(uschar **);
+extern int     verify_check_notblind(void);
+extern int     verify_check_this_host(uschar **, unsigned int *, uschar*,
+                 uschar *, uschar **);
+extern address_item *verify_checked_sender(uschar *);
+extern void    verify_get_ident(int);
+extern BOOL    verify_sender(int *, uschar **);
+extern BOOL    verify_sender_preliminary(int *, uschar **);
+extern void    version_init(void);
+
+extern ssize_t write_to_fd_buf(int, const uschar *, size_t);
+
+/* vi: aw
+*/
+/* End of functions.h */
diff --git a/src/globals.c b/src/globals.c
new file mode 100644 (file)
index 0000000..d3f9987
--- /dev/null
@@ -0,0 +1,1457 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2014 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* All the global variables are defined together in this one module, so
+that they are easy to find. */
+
+#include "exim.h"
+
+
+/* Generic options for auths, all of which live inside auth_instance
+data blocks and hence have the opt_public flag set. */
+
+optionlist optionlist_auths[] = {
+  { "client_condition", opt_stringptr | opt_public,
+                 (void *)(offsetof(auth_instance, client_condition)) },
+  { "client_set_id", opt_stringptr | opt_public,
+                 (void *)(offsetof(auth_instance, set_client_id)) },
+  { "driver",        opt_stringptr | opt_public,
+                 (void *)(offsetof(auth_instance, driver_name)) },
+  { "public_name",   opt_stringptr | opt_public,
+                 (void *)(offsetof(auth_instance, public_name)) },
+  { "server_advertise_condition", opt_stringptr | opt_public,
+                 (void *)(offsetof(auth_instance, advertise_condition))},
+  { "server_condition", opt_stringptr | opt_public,
+                 (void *)(offsetof(auth_instance, server_condition)) },
+  { "server_debug_print", opt_stringptr | opt_public,
+                 (void *)(offsetof(auth_instance, server_debug_string)) },
+  { "server_mail_auth_condition", opt_stringptr | opt_public,
+                 (void *)(offsetof(auth_instance, mail_auth_condition)) },
+  { "server_set_id", opt_stringptr | opt_public,
+                 (void *)(offsetof(auth_instance, set_id)) }
+};
+
+int     optionlist_auths_size = sizeof(optionlist_auths)/sizeof(optionlist);
+
+/* An empty host aliases list. */
+
+uschar *no_aliases             = NULL;
+
+
+/* For comments on these variables, see globals.h. I'm too idle to
+duplicate them here... */
+
+#ifdef EXIM_PERL
+uschar *opt_perl_startup       = NULL;
+BOOL    opt_perl_at_start      = FALSE;
+BOOL    opt_perl_started       = FALSE;
+#endif
+
+#ifdef EXPAND_DLFUNC
+tree_node *dlobj_anchor        = NULL;
+#endif
+
+#ifdef LOOKUP_IBASE
+uschar *ibase_servers          = NULL;
+#endif
+
+#ifdef LOOKUP_LDAP
+uschar *eldap_ca_cert_dir      = NULL;
+uschar *eldap_ca_cert_file     = NULL;
+uschar *eldap_cert_file        = NULL;
+uschar *eldap_cert_key         = NULL;
+uschar *eldap_cipher_suite     = NULL;
+uschar *eldap_default_servers  = NULL;
+uschar *eldap_require_cert     = NULL;
+int     eldap_version          = -1;
+BOOL    eldap_start_tls        = FALSE;
+#endif
+
+#ifdef LOOKUP_MYSQL
+uschar *mysql_servers          = NULL;
+#endif
+
+#ifdef LOOKUP_ORACLE
+uschar *oracle_servers         = NULL;
+#endif
+
+#ifdef LOOKUP_PGSQL
+uschar *pgsql_servers          = NULL;
+#endif
+
+#ifdef EXPERIMENTAL_REDIS
+uschar *redis_servers          = NULL;
+#endif
+
+#ifdef LOOKUP_SQLITE
+int     sqlite_lock_timeout    = 5;
+#endif
+
+#ifdef SUPPORT_MOVE_FROZEN_MESSAGES
+BOOL    move_frozen_messages   = FALSE;
+#endif
+
+/* These variables are outside the #ifdef because it keeps the code less
+cluttered in several places (e.g. during logging) if we can always refer to
+them. Also, the tls_ variables are now always visible. */
+
+tls_support tls_in = {
+ -1,   /* tls_active */
+ 0,    /* tls_bits */
+ FALSE,/* tls_certificate_verified */
+ NULL, /* tls_cipher */
+ FALSE,/* tls_on_connect */
+ NULL, /* tls_on_connect_ports */
+ NULL, /* tls_ourcert */
+ NULL, /* tls_peercert */
+ NULL, /* tls_peerdn */
+ NULL, /* tls_sni */
+ 0     /* tls_ocsp */
+};
+tls_support tls_out = {
+ -1,   /* tls_active */
+ 0,    /* tls_bits */
+ FALSE,/* tls_certificate_verified */
+ NULL, /* tls_cipher */
+ FALSE,/* tls_on_connect */
+ NULL, /* tls_on_connect_ports */
+ NULL, /* tls_ourcert */
+ NULL, /* tls_peercert */
+ NULL, /* tls_peerdn */
+ NULL, /* tls_sni */
+ 0     /* tls_ocsp */
+};
+
+#ifdef EXPERIMENTAL_DSN
+uschar *dsn_envid              = NULL;
+int     dsn_ret                = 0;
+const pcre  *regex_DSN         = NULL;
+BOOL    smtp_use_dsn           = FALSE;
+uschar *dsn_advertise_hosts    = NULL;
+#endif
+
+#ifdef SUPPORT_TLS
+BOOL    gnutls_compat_mode     = FALSE;
+BOOL    gnutls_allow_auto_pkcs11 = FALSE;
+uschar *gnutls_require_mac     = NULL;
+uschar *gnutls_require_kx      = NULL;
+uschar *gnutls_require_proto   = NULL;
+uschar *openssl_options        = NULL;
+const pcre *regex_STARTTLS     = NULL;
+uschar *tls_advertise_hosts    = NULL;    /* This is deliberate */
+uschar *tls_certificate        = NULL;
+uschar *tls_crl                = NULL;
+/* This default matches NSS DH_MAX_P_BITS value at current time (2012), because
+that's the interop problem which has been observed: GnuTLS suggesting a higher
+bit-count as "NORMAL" (2432) and Thunderbird dropping connection. */
+int     tls_dh_max_bits        = 2236;
+uschar *tls_dhparam            = NULL;
+#ifndef DISABLE_OCSP
+uschar *tls_ocsp_file          = NULL;
+#endif
+BOOL    tls_offered            = FALSE;
+uschar *tls_privatekey         = NULL;
+BOOL    tls_remember_esmtp     = FALSE;
+uschar *tls_require_ciphers    = NULL;
+uschar *tls_try_verify_hosts   = NULL;
+uschar *tls_verify_certificates= NULL;
+uschar *tls_verify_hosts       = NULL;
+#endif
+
+#ifndef DISABLE_PRDR
+/* Per Recipient Data Response variables */
+BOOL    prdr_enable            = FALSE;
+BOOL    prdr_requested         = FALSE;
+const pcre *regex_PRDR         = NULL;
+#endif
+
+/* Input-reading functions for messages, so we can use special ones for
+incoming TCP/IP. The defaults use stdin. We never need these for any
+stand-alone tests. */
+
+#ifndef STAND_ALONE
+int (*receive_getc)(void)      = stdin_getc;
+int (*receive_ungetc)(int)     = stdin_ungetc;
+int (*receive_feof)(void)      = stdin_feof;
+int (*receive_ferror)(void)    = stdin_ferror;
+BOOL (*receive_smtp_buffered)(void) = NULL;   /* Only used for SMTP */
+#endif
+
+
+/* List of per-address expansion variables for clearing and saving/restoring
+when verifying one address while routing/verifying another. We have to have
+the size explicit, because it is referenced from more than one module. */
+
+uschar **address_expansions[ADDRESS_EXPANSIONS_COUNT] = {
+  &deliver_address_data,
+  &deliver_domain,
+  &deliver_domain_data,
+  &deliver_domain_orig,
+  &deliver_domain_parent,
+  &deliver_localpart,
+  &deliver_localpart_data,
+  &deliver_localpart_orig,
+  &deliver_localpart_parent,
+  &deliver_localpart_prefix,
+  &deliver_localpart_suffix,
+  (uschar **)(&deliver_recipients),
+  &deliver_host,
+  &deliver_home,
+  &address_file,
+  &address_pipe,
+  &self_hostname,
+  NULL };
+
+int address_expansions_count = sizeof(address_expansions)/sizeof(uschar **);
+
+/* General global variables */
+
+header_line *acl_added_headers = NULL;
+tree_node *acl_anchor          = NULL;
+uschar *acl_arg[9]             = {NULL, NULL, NULL, NULL, NULL,
+                                  NULL, NULL, NULL, NULL};
+int     acl_narg               = 0;
+
+uschar *acl_not_smtp           = NULL;
+#ifdef WITH_CONTENT_SCAN
+uschar *acl_not_smtp_mime      = NULL;
+#endif
+uschar *acl_not_smtp_start     = NULL;
+uschar *acl_removed_headers    = NULL;
+uschar *acl_smtp_auth          = NULL;
+uschar *acl_smtp_connect       = NULL;
+uschar *acl_smtp_data          = NULL;
+#ifndef DISABLE_PRDR
+uschar *acl_smtp_data_prdr     = NULL;
+#endif
+#ifndef DISABLE_DKIM
+uschar *acl_smtp_dkim          = NULL;
+#endif
+uschar *acl_smtp_etrn          = NULL;
+uschar *acl_smtp_expn          = NULL;
+uschar *acl_smtp_helo          = NULL;
+uschar *acl_smtp_mail          = NULL;
+uschar *acl_smtp_mailauth      = NULL;
+#ifdef WITH_CONTENT_SCAN
+uschar *acl_smtp_mime          = NULL;
+#endif
+uschar *acl_smtp_notquit       = NULL;
+uschar *acl_smtp_predata       = NULL;
+uschar *acl_smtp_quit          = NULL;
+uschar *acl_smtp_rcpt          = NULL;
+uschar *acl_smtp_starttls      = NULL;
+uschar *acl_smtp_vrfy          = NULL;
+
+BOOL    acl_temp_details       = FALSE;
+tree_node *acl_var_c           = NULL;
+tree_node *acl_var_m           = NULL;
+uschar *acl_verify_message     = NULL;
+string_item *acl_warn_logged   = NULL;
+
+/* Names of SMTP places for use in ACL error messages, and corresponding SMTP
+error codes - keep in step with definitions of ACL_WHERE_xxxx in macros.h. */
+
+uschar *acl_wherenames[]       = { US"RCPT",
+                                   US"MAIL",
+                                   US"PREDATA",
+                                   US"MIME",
+                                   US"DKIM",
+                                   US"DATA",
+#ifndef DISABLE_PRDR
+                                   US"PRDR",
+#endif
+                                   US"non-SMTP",
+                                   US"AUTH",
+                                   US"connection",
+                                   US"ETRN",
+                                   US"EXPN",
+                                   US"EHLO or HELO",
+                                   US"MAILAUTH",
+                                   US"non-SMTP-start",
+                                   US"NOTQUIT",
+                                   US"QUIT",
+                                   US"STARTTLS",
+                                   US"VRFY",
+                                  US"delivery",
+                                  US"unknown"
+                                 };
+
+uschar *acl_wherecodes[]       = { US"550",     /* RCPT */
+                                   US"550",     /* MAIL */
+                                   US"550",     /* PREDATA */
+                                   US"550",     /* MIME */
+                                   US"550",     /* DKIM */
+                                   US"550",     /* DATA */
+#ifndef DISABLE_PRDR
+                                   US"550",    /* RCPT PRDR */
+#endif
+                                   US"0",       /* not SMTP; not relevant */
+                                   US"503",     /* AUTH */
+                                   US"550",     /* connect */
+                                   US"458",     /* ETRN */
+                                   US"550",     /* EXPN */
+                                   US"550",     /* HELO/EHLO */
+                                   US"0",       /* MAILAUTH; not relevant */
+                                   US"0",       /* not SMTP; not relevant */
+                                   US"0",       /* NOTQUIT; not relevant */
+                                   US"0",       /* QUIT; not relevant */
+                                   US"550",     /* STARTTLS */
+                                   US"252",     /* VRFY */
+                                  US"0",       /* delivery; not relevant */
+                                  US"0"        /* unknown; not relevant */
+                                 };
+
+BOOL    active_local_from_check = FALSE;
+BOOL    active_local_sender_retain = FALSE;
+int     body_8bitmime = 0;
+BOOL    accept_8bitmime        = TRUE; /* deliberately not RFC compliant */
+address_item  *addr_duplicate  = NULL;
+
+address_item address_defaults = {
+  NULL,                 /* next */
+  NULL,                 /* parent */
+  NULL,                 /* first */
+  NULL,                 /* dupof */
+  NULL,                 /* start_router */
+  NULL,                 /* router */
+  NULL,                 /* transport */
+  NULL,                 /* host_list */
+  NULL,                 /* host_used */
+  NULL,                 /* fallback_hosts */
+  NULL,                 /* reply */
+  NULL,                 /* retries */
+  NULL,                 /* address */
+  NULL,                 /* unique */
+  NULL,                 /* cc_local_part */
+  NULL,                 /* lc_local_part */
+  NULL,                 /* local_part */
+  NULL,                 /* prefix */
+  NULL,                 /* suffix */
+  NULL,                 /* domain */
+  NULL,                 /* address_retry_key */
+  NULL,                 /* domain_retry_key */
+  NULL,                 /* current_dir */
+  NULL,                 /* home_dir */
+  NULL,                 /* message */
+  NULL,                 /* user_message */
+  NULL,                 /* onetime_parent */
+  NULL,                 /* pipe_expandn */
+  NULL,                 /* return_filename */
+  NULL,                 /* self_hostname */
+  NULL,                 /* shadow_message */
+  #ifdef SUPPORT_TLS
+  NULL,                 /* cipher */
+  NULL,                        /* ourcert */
+  NULL,                        /* peercert */
+  NULL,                 /* peerdn */
+  OCSP_NOT_REQ,         /* ocsp */
+  #endif
+  NULL,                        /* authenticator */
+  NULL,                        /* auth_id */
+  NULL,                        /* auth_sndr */
+  #ifdef EXPERIMENTAL_DSN
+  NULL,                 /* dsn_orcpt */
+  0,                    /* dsn_flags */
+  0,                    /* dsn_aware */
+  #endif
+  (uid_t)(-1),          /* uid */
+  (gid_t)(-1),          /* gid */
+  0,                    /* flags */
+  { 0 },                /* domain_cache - any larger array should be zeroed */
+  { 0 },                /* localpart_cache - ditto */
+  -1,                   /* mode */
+  0,                    /* more_errno */
+  ERRNO_UNKNOWNERROR,   /* basic_errno */
+  0,                    /* child_count */
+  -1,                   /* return_file */
+  SPECIAL_NONE,         /* special_action */
+  DEFER,                /* transport_return */
+  {                     /* fields that are propagated to children */
+    NULL,               /* address_data */
+    NULL,               /* domain_data */
+    NULL,               /* localpart_data */
+    NULL,               /* errors_address */
+    NULL,               /* extra_headers */
+    NULL,               /* remove_headers */
+#ifdef EXPERIMENTAL_SRS
+    NULL,               /* srs_sender */
+#endif
+  }
+};
+
+uschar *address_file           = NULL;
+uschar *address_pipe           = NULL;
+BOOL    address_test_mode      = FALSE;
+tree_node *addresslist_anchor  = NULL;
+int     addresslist_count      = 0;
+gid_t  *admin_groups           = NULL;
+BOOL    admin_user             = FALSE;
+BOOL    allow_auth_unadvertised= FALSE;
+BOOL    allow_domain_literals  = FALSE;
+BOOL    allow_mx_to_ip         = FALSE;
+BOOL    allow_unqualified_recipient = TRUE;    /* For local messages */
+BOOL    allow_unqualified_sender = TRUE;       /* Reset for SMTP */
+BOOL    allow_utf8_domains     = FALSE;
+uschar *authenticated_fail_id  = NULL;
+uschar *authenticated_id       = NULL;
+uschar *authenticated_sender   = NULL;
+BOOL    authentication_failed  = FALSE;
+auth_instance  *auths          = NULL;
+uschar *auth_advertise_hosts   = US"*";
+auth_instance auth_defaults    = {
+    NULL,                      /* chain pointer */
+    NULL,                      /* name */
+    NULL,                      /* info */
+    NULL,                      /* private options block pointer */
+    NULL,                      /* driver_name */
+    NULL,                      /* advertise_condition */
+    NULL,                      /* client_condition */
+    NULL,                      /* public_name */
+    NULL,                      /* set_id */
+    NULL,                      /* set_client_id */
+    NULL,                      /* server_mail_auth_condition */
+    NULL,                      /* server_debug_string */
+    NULL,                      /* server_condition */
+    FALSE,                     /* client */
+    FALSE,                     /* server */
+    FALSE                      /* advertised */
+};
+
+uschar *auth_defer_msg         = US"reason not recorded";
+uschar *auth_defer_user_msg    = US"";
+uschar *auth_vars[AUTH_VARS];
+int     auto_thaw              = 0;
+#ifdef WITH_CONTENT_SCAN
+BOOL    av_failed              = FALSE;
+uschar *av_scanner             = US"sophie:/var/run/sophie";  /* AV scanner */
+#endif
+
+BOOL    background_daemon      = TRUE;
+
+#if BASE_62 == 62
+uschar *base62_chars=
+    US"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
+#else
+uschar *base62_chars= US"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
+#endif
+
+uschar *bi_command             = NULL;
+uschar *big_buffer             = NULL;
+int     big_buffer_size        = BIG_BUFFER_SIZE;
+#ifdef EXPERIMENTAL_BRIGHTMAIL
+uschar *bmi_alt_location       = NULL;
+uschar *bmi_base64_tracker_verdict = NULL;
+uschar *bmi_base64_verdict     = NULL;
+uschar *bmi_config_file        = US"/opt/brightmail/etc/brightmail.cfg";
+int     bmi_deliver            = 1;
+int     bmi_run                = 0;
+uschar *bmi_verdicts           = NULL;
+#endif
+int     body_linecount         = 0;
+int     body_zerocount         = 0;
+uschar *bounce_message_file    = NULL;
+uschar *bounce_message_text    = NULL;
+uschar *bounce_recipient       = NULL;
+BOOL    bounce_return_body     = TRUE;
+BOOL    bounce_return_message  = TRUE;
+int     bounce_return_size_limit = 100*1024;
+uschar *bounce_sender_authentication = NULL;
+int     bsmtp_transaction_linecount = 0;
+
+int     callout_cache_domain_positive_expire = 7*24*60*60;
+int     callout_cache_domain_negative_expire = 3*60*60;
+int     callout_cache_positive_expire = 24*60*60;
+int     callout_cache_negative_expire = 2*60*60;
+uschar *callout_random_local_part = US"$primary_hostname-$tod_epoch-testing";
+uschar *check_dns_names_pattern= US"(?i)^(?>(?(1)\\.|())[^\\W](?>[a-z0-9/_-]*[^\\W])?)+(\\.?)$";
+int     check_log_inodes       = 0;
+int     check_log_space        = 0;
+BOOL    check_rfc2047_length   = TRUE;
+int     check_spool_inodes     = 0;
+int     check_spool_space      = 0;
+uschar *client_authenticator  = NULL;
+uschar *client_authenticated_id = NULL;
+uschar *client_authenticated_sender = NULL;
+int     clmacro_count          = 0;
+uschar *clmacros[MAX_CLMACROS];
+BOOL    config_changed         = FALSE;
+FILE   *config_file            = NULL;
+uschar *config_filename        = NULL;
+int     config_lineno          = 0;
+#ifdef CONFIGURE_GROUP
+gid_t   config_gid             = CONFIGURE_GROUP;
+#endif
+uschar *config_main_filelist   = US CONFIGURE_FILE
+                         "\0<-----------Space to patch configure_filename->";
+uschar *config_main_filename   = NULL;
+
+#ifdef CONFIGURE_OWNER
+uid_t   config_uid             = CONFIGURE_OWNER;
+#endif
+
+int     connection_max_messages= -1;
+uschar *continue_hostname      = NULL;
+uschar *continue_host_address  = NULL;
+BOOL    continue_more          = FALSE;
+int     continue_sequence      = 1;
+uschar *continue_transport     = NULL;
+
+uschar *csa_status             = NULL;
+BOOL    cutthrough_delivery    = FALSE;
+int     cutthrough_fd          = -1;
+
+BOOL    daemon_listen          = FALSE;
+uschar *daemon_smtp_port       = US"smtp";
+int     daemon_startup_retries = 9;
+int     daemon_startup_sleep   = 30;
+
+#ifdef EXPERIMENTAL_DCC
+BOOL    dcc_direct_add_header  = FALSE;
+uschar *dcc_header             = NULL;
+uschar *dcc_result             = NULL;
+uschar *dccifd_address         = US"/usr/local/dcc/var/dccifd";
+uschar *dccifd_options         = US"header";
+#endif
+
+BOOL    debug_daemon           = FALSE;
+int     debug_fd               = -1;
+FILE   *debug_file             = NULL;
+bit_table debug_options[]      = {
+  { US"acl",            D_acl },
+  { US"all",            D_all },
+  { US"auth",           D_auth },
+  { US"deliver",        D_deliver },
+  { US"dns",            D_dns },
+  { US"dnsbl",          D_dnsbl },
+  { US"exec",           D_exec },
+  { US"expand",         D_expand },
+  { US"filter",         D_filter },
+  { US"hints_lookup",   D_hints_lookup },
+  { US"host_lookup",    D_host_lookup },
+  { US"ident",          D_ident },
+  { US"interface",      D_interface },
+  { US"lists",          D_lists },
+  { US"load",           D_load },
+  { US"local_scan",     D_local_scan },
+  { US"lookup",         D_lookup },
+  { US"memory",         D_memory },
+  { US"pid",            D_pid },
+  { US"process_info",   D_process_info },
+  { US"queue_run",      D_queue_run },
+  { US"receive",        D_receive },
+  { US"resolver",       D_resolver },
+  { US"retry",          D_retry },
+  { US"rewrite",        D_rewrite },
+  { US"route",          D_route },
+  { US"timestamp",      D_timestamp },
+  { US"tls",            D_tls },
+  { US"transport",      D_transport },
+  { US"uid",            D_uid },
+  { US"verify",         D_verify }
+};
+int     debug_options_count    = sizeof(debug_options)/sizeof(bit_table);
+unsigned int debug_selector    = 0;
+int     delay_warning[DELAY_WARNING_SIZE] = { DELAY_WARNING_SIZE, 1, 24*60*60 };
+uschar *delay_warning_condition=
+  US"${if or {"
+            "{ !eq{$h_list-id:$h_list-post:$h_list-subscribe:}{} }"
+            "{ match{$h_precedence:}{(?i)bulk|list|junk} }"
+            "{ match{$h_auto-submitted:}{(?i)auto-generated|auto-replied} }"
+            "} {no}{yes}}";
+BOOL    delivery_date_remove   = TRUE;
+uschar *deliver_address_data   = NULL;
+int     deliver_datafile       = -1;
+uschar *deliver_domain         = NULL;
+uschar *deliver_domain_data    = NULL;
+uschar *deliver_domain_orig    = NULL;
+uschar *deliver_domain_parent  = NULL;
+BOOL    deliver_drop_privilege = FALSE;
+BOOL    deliver_firsttime      = FALSE;
+BOOL    deliver_force          = FALSE;
+BOOL    deliver_freeze         = FALSE;
+time_t  deliver_frozen_at      = 0;
+uschar *deliver_home           = NULL;
+uschar *deliver_host           = NULL;
+uschar *deliver_host_address   = NULL;
+uschar *deliver_in_buffer      = NULL;
+ino_t   deliver_inode          = 0;
+uschar *deliver_localpart      = NULL;
+uschar *deliver_localpart_data = NULL;
+uschar *deliver_localpart_orig = NULL;
+uschar *deliver_localpart_parent = NULL;
+uschar *deliver_localpart_prefix = NULL;
+uschar *deliver_localpart_suffix = NULL;
+BOOL    deliver_force_thaw     = FALSE;
+BOOL    deliver_manual_thaw    = FALSE;
+uschar *deliver_out_buffer     = NULL;
+int     deliver_queue_load_max = -1;
+address_item  *deliver_recipients = NULL;
+uschar *deliver_selectstring   = NULL;
+BOOL    deliver_selectstring_regex = FALSE;
+uschar *deliver_selectstring_sender = NULL;
+BOOL    deliver_selectstring_sender_regex = FALSE;
+#ifdef WITH_OLD_DEMIME
+int     demime_errorlevel      = 0;
+int     demime_ok              = 0;
+uschar *demime_reason          = NULL;
+#endif
+BOOL    disable_callout_flush  = FALSE;
+BOOL    disable_delay_flush    = FALSE;
+#ifdef ENABLE_DISABLE_FSYNC
+BOOL    disable_fsync          = FALSE;
+#endif
+BOOL    disable_ipv6           = FALSE;
+BOOL    disable_logging        = FALSE;
+
+#ifndef DISABLE_DKIM
+uschar *dkim_cur_signer          = NULL;
+uschar *dkim_signers             = NULL;
+uschar *dkim_signing_domain      = NULL;
+uschar *dkim_signing_selector    = NULL;
+uschar *dkim_verify_signers      = US"$dkim_signers";
+BOOL    dkim_collect_input       = FALSE;
+BOOL    dkim_disable_verify      = FALSE;
+#endif
+#ifdef EXPERIMENTAL_DMARC
+BOOL    dmarc_has_been_checked  = FALSE;
+uschar *dmarc_ar_header         = NULL;
+uschar *dmarc_domain_policy     = NULL;
+uschar *dmarc_forensic_sender   = NULL;
+uschar *dmarc_history_file      = NULL;
+uschar *dmarc_status            = NULL;
+uschar *dmarc_status_text       = NULL;
+uschar *dmarc_tld_file          = NULL;
+uschar *dmarc_used_domain       = NULL;
+BOOL    dmarc_disable_verify    = FALSE;
+BOOL    dmarc_enable_forensic   = FALSE;
+#endif
+
+uschar *dns_again_means_nonexist = NULL;
+int     dns_csa_search_limit   = 5;
+BOOL    dns_csa_use_reverse    = TRUE;
+uschar *dns_ipv4_lookup        = NULL;
+int     dns_retrans            = 0;
+int     dns_retry              = 0;
+int     dns_dnssec_ok          = -1; /* <0 = not coerced */
+int     dns_use_edns0          = -1; /* <0 = not coerced */
+uschar *dnslist_domain         = NULL;
+uschar *dnslist_matched        = NULL;
+uschar *dnslist_text           = NULL;
+uschar *dnslist_value          = NULL;
+tree_node *domainlist_anchor   = NULL;
+int     domainlist_count       = 0;
+BOOL    dont_deliver           = FALSE;
+BOOL    dot_ends               = TRUE;
+BOOL    drop_cr                = FALSE;         /* No longer used */
+uschar *dsn_from               = US DEFAULT_DSN_FROM;
+
+BOOL    enable_dollar_recipients = FALSE;
+BOOL    envelope_to_remove     = TRUE;
+int     errno_quota            = ERRNO_QUOTA;
+uschar *errors_copy            = NULL;
+int     error_handling         = ERRORS_SENDER;
+uschar *errors_reply_to        = NULL;
+int     errors_sender_rc       = EXIT_FAILURE;
+
+gid_t   exim_gid               = EXIM_GID;
+BOOL    exim_gid_set           = TRUE;          /* This gid is always set */
+uschar *exim_path              = US BIN_DIRECTORY "/exim"
+                        "\0<---------------Space to patch exim_path->";
+uid_t   exim_uid               = EXIM_UID;
+BOOL    exim_uid_set           = TRUE;          /* This uid is always set */
+int     expand_forbid          = 0;
+int     expand_nlength[EXPAND_MAXN+1];
+int     expand_nmax            = -1;
+uschar *expand_nstring[EXPAND_MAXN+1];
+BOOL    expand_string_forcedfail = FALSE;
+uschar *expand_string_message;
+BOOL    extract_addresses_remove_arguments = TRUE;
+uschar *extra_local_interfaces = NULL;
+
+int     fake_response          = OK;
+uschar *fake_response_text     = US"Your message has been rejected but is "
+                                   "being kept for evaluation.\nIf it was a "
+                                   "legitimate message, it may still be "
+                                   "delivered to the target recipient(s).";
+int     filter_n[FILTER_VARIABLE_COUNT];
+BOOL    filter_running         = FALSE;
+int     filter_sn[FILTER_VARIABLE_COUNT];
+int     filter_test            = FTEST_NONE;
+uschar *filter_test_sfile      = NULL;
+uschar *filter_test_ufile      = NULL;
+uschar *filter_thisaddress     = NULL;
+int     finduser_retries       = 0;
+#ifdef WITH_OLD_DEMIME
+uschar *found_extension        = NULL;
+#endif
+uid_t   fixed_never_users[]    = { FIXED_NEVER_USERS };
+uschar *freeze_tell            = NULL;
+uschar *freeze_tell_config     = NULL;
+uschar *fudged_queue_times     = US"";
+
+uschar *gecos_name             = NULL;
+uschar *gecos_pattern          = NULL;
+rewrite_rule  *global_rewrite_rules = NULL;
+
+uschar *headers_charset        = US HEADERS_CHARSET;
+int     header_insert_maxlen   = 64 * 1024;
+header_line  *header_last      = NULL;
+header_line  *header_list      = NULL;
+int     header_maxsize         = HEADER_MAXSIZE;
+int     header_line_maxsize    = 0;
+
+header_name header_names[] = {
+  { US"bcc",            3, TRUE,  htype_bcc },
+  { US"cc",             2, TRUE,  htype_cc },
+  { US"date",           4, TRUE,  htype_date },
+  { US"delivery-date", 13, FALSE, htype_delivery_date },
+  { US"envelope-to",   11, FALSE, htype_envelope_to },
+  { US"from",           4, TRUE,  htype_from },
+  { US"message-id",    10, TRUE,  htype_id },
+  { US"received",       8, FALSE, htype_received },
+  { US"reply-to",       8, FALSE, htype_reply_to },
+  { US"return-path",   11, FALSE, htype_return_path },
+  { US"sender",         6, TRUE,  htype_sender },
+  { US"subject",        7, FALSE, htype_subject },
+  { US"to",             2, TRUE,  htype_to }
+};
+
+int header_names_size          = sizeof(header_names)/sizeof(header_name);
+
+BOOL    header_rewritten       = FALSE;
+uschar *helo_accept_junk_hosts = NULL;
+uschar *helo_allow_chars       = US"";
+uschar *helo_lookup_domains    = US"@ : @[]";
+uschar *helo_try_verify_hosts  = NULL;
+BOOL    helo_verified          = FALSE;
+BOOL    helo_verify_failed     = FALSE;
+uschar *helo_verify_hosts      = NULL;
+const uschar *hex_digits       = CUS"0123456789abcdef";
+uschar *hold_domains           = NULL;
+BOOL    host_checking          = FALSE;
+BOOL    host_checking_callout  = FALSE;
+uschar *host_data              = NULL;
+BOOL    host_find_failed_syntax= FALSE;
+uschar *host_lookup            = NULL;
+BOOL    host_lookup_deferred   = FALSE;
+BOOL    host_lookup_failed     = FALSE;
+uschar *host_lookup_order      = US"bydns:byaddr";
+uschar *host_lookup_msg        = US"";
+int     host_number            = 0;
+uschar *host_number_string     = NULL;
+uschar *host_reject_connection = NULL;
+tree_node *hostlist_anchor     = NULL;
+int     hostlist_count         = 0;
+uschar *hosts_treat_as_local   = NULL;
+uschar *hosts_connection_nolog = NULL;
+
+int     ignore_bounce_errors_after = 10*7*24*60*60;  /* 10 weeks */
+BOOL    ignore_fromline_local  = FALSE;
+uschar *ignore_fromline_hosts  = NULL;
+BOOL    inetd_wait_mode        = FALSE;
+int     inetd_wait_timeout     = -1;
+uschar *interface_address      = NULL;
+int     interface_port         = -1;
+BOOL    is_inetd               = FALSE;
+uschar *iterate_item           = NULL;
+
+int     journal_fd             = -1;
+
+int     keep_malformed         = 4*24*60*60;    /* 4 days */
+
+uschar *eldap_dn               = NULL;
+int     load_average           = -2;
+BOOL    local_error_message    = FALSE;
+BOOL    local_from_check       = TRUE;
+uschar *local_from_prefix      = NULL;
+uschar *local_from_suffix      = NULL;
+
+#if HAVE_IPV6
+uschar *local_interfaces       = US"<; ::0 ; 0.0.0.0";
+#else
+uschar *local_interfaces       = US"0.0.0.0";
+#endif
+
+uschar *local_scan_data        = NULL;
+int     local_scan_timeout     = 5*60;
+BOOL    local_sender_retain    = FALSE;
+gid_t   local_user_gid         = (gid_t)(-1);
+uid_t   local_user_uid         = (uid_t)(-1);
+
+tree_node *localpartlist_anchor= NULL;
+int     localpartlist_count    = 0;
+uschar *log_buffer             = NULL;
+unsigned int log_extra_selector = LX_default;
+uschar *log_file_path          = US LOG_FILE_PATH
+                           "\0<--------------Space to patch log_file_path->";
+
+/* Those log options with L_xxx identifiers have values less than 0x800000 and
+are the ones that get put into log_write_selector. They can be used in calls to
+log_write() to test for the bit. The options with LX_xxx identifiers have
+values greater than 0x80000000 and are put into log_extra_selector (without the
+top bit). They are never used in calls to log_write(), but are tested
+independently. This separation became necessary when the number of log
+selectors was getting close to filling a 32-bit word. */
+
+/* Note that this list must be in alphabetical order. */
+
+bit_table log_options[]        = {
+  { US"8bitmime",                     LX_8bitmime },
+  { US"acl_warn_skipped",             LX_acl_warn_skipped },
+  { US"address_rewrite",              L_address_rewrite },
+  { US"all",                          L_all },
+  { US"all_parents",                  L_all_parents },
+  { US"arguments",                    LX_arguments },
+  { US"connection_reject",            L_connection_reject },
+  { US"delay_delivery",               L_delay_delivery },
+  { US"deliver_time",                 LX_deliver_time },
+  { US"delivery_size",                LX_delivery_size },
+  { US"dnslist_defer",                L_dnslist_defer },
+  { US"etrn",                         L_etrn },
+  { US"host_lookup_failed",           L_host_lookup_failed },
+  { US"ident_timeout",                LX_ident_timeout },
+  { US"incoming_interface",           LX_incoming_interface },
+  { US"incoming_port",                LX_incoming_port },
+  { US"lost_incoming_connection",     L_lost_incoming_connection },
+  { US"outgoing_port",                LX_outgoing_port },
+  { US"pid",                          LX_pid },
+#ifdef EXPERIMENTAL_PROXY
+  { US"proxy",                        LX_proxy },
+#endif
+  { US"queue_run",                    L_queue_run },
+  { US"queue_time",                   LX_queue_time },
+  { US"queue_time_overall",           LX_queue_time_overall },
+  { US"received_recipients",          LX_received_recipients },
+  { US"received_sender",              LX_received_sender },
+  { US"rejected_header",              LX_rejected_header },
+  { US"rejected_headers",             LX_rejected_header },
+  { US"retry_defer",                  L_retry_defer },
+  { US"return_path_on_delivery",      LX_return_path_on_delivery },
+  { US"sender_on_delivery",           LX_sender_on_delivery },
+  { US"sender_verify_fail",           LX_sender_verify_fail },
+  { US"size_reject",                  L_size_reject },
+  { US"skip_delivery",                L_skip_delivery },
+  { US"smtp_confirmation",            LX_smtp_confirmation },
+  { US"smtp_connection",              L_smtp_connection },
+  { US"smtp_incomplete_transaction",  L_smtp_incomplete_transaction },
+  { US"smtp_mailauth",                LX_smtp_mailauth },
+  { US"smtp_no_mail",                 LX_smtp_no_mail },
+  { US"smtp_protocol_error",          L_smtp_protocol_error },
+  { US"smtp_syntax_error",            L_smtp_syntax_error },
+  { US"subject",                      LX_subject },
+  { US"tls_certificate_verified",     LX_tls_certificate_verified },
+  { US"tls_cipher",                   LX_tls_cipher },
+  { US"tls_peerdn",                   LX_tls_peerdn },
+  { US"tls_sni",                      LX_tls_sni },
+  { US"unknown_in_list",              LX_unknown_in_list }
+};
+
+int     log_options_count      = sizeof(log_options)/sizeof(bit_table);
+int     log_reject_target      = 0;
+uschar *log_selector_string    = NULL;
+FILE   *log_stderr             = NULL;
+BOOL    log_testing_mode       = FALSE;
+BOOL    log_timezone           = FALSE;
+unsigned int log_write_selector= L_default;
+uschar *login_sender_address   = NULL;
+uschar *lookup_dnssec_authenticated = NULL;
+int     lookup_open_max        = 25;
+uschar *lookup_value           = NULL;
+
+macro_item  *macros            = NULL;
+uschar *mailstore_basename     = NULL;
+#ifdef WITH_CONTENT_SCAN
+uschar *malware_name           = NULL;  /* Virus Name */
+#endif
+int     max_received_linelength= 0;
+int     max_username_length    = 0;
+int     message_age            = 0;
+uschar *message_body           = NULL;
+uschar *message_body_end       = NULL;
+BOOL    message_body_newlines  = FALSE;
+int     message_body_size      = 0;
+int     message_body_visible   = 500;
+int     message_ended          = END_NOTSTARTED;
+uschar *message_headers        = NULL;
+uschar *message_id;
+uschar *message_id_domain      = NULL;
+uschar *message_id_text        = NULL;
+struct timeval message_id_tv   = { 0, 0 };
+uschar  message_id_option[MESSAGE_ID_LENGTH + 3];
+uschar *message_id_external;
+int     message_linecount      = 0;
+BOOL    message_logs           = TRUE;
+int     message_size           = 0;
+uschar *message_size_limit     = US"50M";
+uschar  message_subdir[2]      = { 0, 0 };
+uschar *message_reference      = NULL;
+
+/* MIME ACL expandables */
+#ifdef WITH_CONTENT_SCAN
+int     mime_anomaly_level     = 0;
+const uschar *mime_anomaly_text      = NULL;
+uschar *mime_boundary          = NULL;
+uschar *mime_charset           = NULL;
+uschar *mime_content_description = NULL;
+uschar *mime_content_disposition = NULL;
+uschar *mime_content_id        = NULL;
+unsigned int mime_content_size = 0;
+uschar *mime_content_transfer_encoding = NULL;
+uschar *mime_content_type      = NULL;
+uschar *mime_decoded_filename  = NULL;
+uschar *mime_filename          = NULL;
+int     mime_is_multipart      = 0;
+int     mime_is_coverletter    = 0;
+int     mime_is_rfc822         = 0;
+int     mime_part_count        = -1;
+#endif
+
+BOOL    mua_wrapper            = FALSE;
+
+uid_t  *never_users            = NULL;
+#ifdef WITH_CONTENT_SCAN
+BOOL    no_mbox_unspool        = FALSE;
+#endif
+BOOL    no_multiline_responses = FALSE;
+
+uid_t   original_euid;
+gid_t   originator_gid;
+uschar *originator_login       = NULL;
+uschar *originator_name        = NULL;
+uid_t   originator_uid;
+uschar *override_local_interfaces = NULL;
+uschar *override_pid_file_path = NULL;
+
+BOOL    parse_allow_group      = FALSE;
+BOOL    parse_found_group      = FALSE;
+uschar *percent_hack_domains   = NULL;
+uschar *pid_file_path          = US PID_FILE_PATH
+                           "\0<--------------Space to patch pid_file_path->";
+BOOL    pipelining_enable      = TRUE;
+uschar *pipelining_advertise_hosts = US"*";
+BOOL    preserve_message_logs  = FALSE;
+uschar *primary_hostname       = NULL;
+BOOL    print_topbitchars      = FALSE;
+uschar  process_info[PROCESS_INFO_SIZE];
+int     process_info_len       = 0;
+uschar *process_log_path       = NULL;
+BOOL    prod_requires_admin    = TRUE;
+
+#ifdef EXPERIMENTAL_PROXY
+uschar *proxy_host_address     = US"";
+int     proxy_host_port        = 0;
+uschar *proxy_required_hosts   = US"";
+BOOL    proxy_session          = FALSE;
+BOOL    proxy_session_failed   = FALSE;
+uschar *proxy_target_address   = US"";
+int     proxy_target_port      = 0;
+#endif
+
+uschar *prvscheck_address      = NULL;
+uschar *prvscheck_keynum       = NULL;
+uschar *prvscheck_result       = NULL;
+
+
+uschar *qualify_domain_recipient = NULL;
+uschar *qualify_domain_sender  = NULL;
+BOOL    queue_2stage           = FALSE;
+uschar *queue_domains          = NULL;
+int     queue_interval         = -1;
+BOOL    queue_list_requires_admin = TRUE;
+BOOL    queue_only             = FALSE;
+uschar *queue_only_file        = NULL;
+int     queue_only_load        = -1;
+BOOL    queue_only_load_latch  = TRUE;
+BOOL    queue_only_override    = TRUE;
+BOOL    queue_only_policy      = FALSE;
+BOOL    queue_run_first_delivery = FALSE;
+BOOL    queue_run_force        = FALSE;
+BOOL    queue_run_in_order     = FALSE;
+BOOL    queue_run_local        = FALSE;
+int     queue_run_max          = 5;
+pid_t   queue_run_pid          = (pid_t)0;
+int     queue_run_pipe         = -1;
+BOOL    queue_running          = FALSE;
+BOOL    queue_smtp             = FALSE;
+uschar *queue_smtp_domains     = NULL;
+
+unsigned int random_seed       = 0;
+tree_node *ratelimiters_cmd    = NULL;
+tree_node *ratelimiters_conn   = NULL;
+tree_node *ratelimiters_mail   = NULL;
+uschar *raw_active_hostname    = NULL;
+uschar *raw_sender             = NULL;
+uschar **raw_recipients        = NULL;
+int     raw_recipients_count   = 0;
+
+int     rcpt_count             = 0;
+int     rcpt_fail_count        = 0;
+int     rcpt_defer_count       = 0;
+gid_t   real_gid;
+uid_t   real_uid;
+BOOL    really_exim            = TRUE;
+BOOL    receive_call_bombout   = FALSE;
+int     receive_linecount      = 0;
+int     receive_messagecount   = 0;
+int     receive_timeout        = 0;
+int     received_count         = 0;
+uschar *received_for           = NULL;
+
+/*  This is the default text for Received headers generated by Exim. The
+date  will be automatically added on the end. */
+
+uschar *received_header_text   = US
+     "Received: "
+     "${if def:sender_rcvhost {from $sender_rcvhost\n\t}"
+     "{${if def:sender_ident {from ${quote_local_part:$sender_ident} }}"
+     "${if def:sender_helo_name {(helo=$sender_helo_name)\n\t}}}}"
+     "by $primary_hostname "
+     "${if def:received_protocol {with $received_protocol}} "
+     #ifdef SUPPORT_TLS
+     "${if def:tls_cipher {($tls_cipher)\n\t}}"
+     #endif
+     "(Exim $version_number)\n\t"
+     "${if def:sender_address {(envelope-from <$sender_address>)\n\t}}"
+     "id $message_exim_id"
+     "${if def:received_for {\n\tfor $received_for}}"
+     "\0<---------------Space to patch received_header_text->";
+
+int     received_headers_max   = 30;
+uschar *received_protocol      = NULL;
+int     received_time          = 0;
+uschar *recipient_data         = NULL;
+uschar *recipient_unqualified_hosts = NULL;
+uschar *recipient_verify_failure = NULL;
+int     recipients_count       = 0;
+BOOL    recipients_discarded   = FALSE;
+recipient_item  *recipients_list = NULL;
+int     recipients_list_max    = 0;
+int     recipients_max         = 0;
+BOOL    recipients_max_reject  = FALSE;
+const pcre *regex_AUTH         = NULL;
+const pcre *regex_check_dns_names = NULL;
+const pcre *regex_From         = NULL;
+const pcre *regex_IGNOREQUOTA  = NULL;
+const pcre *regex_PIPELINING   = NULL;
+const pcre *regex_SIZE         = NULL;
+const pcre *regex_smtp_code    = NULL;
+const pcre *regex_ismsgid      = NULL;
+#ifdef WHITELIST_D_MACROS
+const pcre *regex_whitelisted_macro = NULL;
+#endif
+#ifdef WITH_CONTENT_SCAN
+uschar *regex_match_string     = NULL;
+#endif
+int     remote_delivery_count  = 0;
+int     remote_max_parallel    = 2;
+uschar *remote_sort_domains    = NULL;
+int     retry_data_expire      = 7*24*60*60;
+int     retry_interval_max     = 24*60*60;
+int     retry_maximum_timeout  = 0;        /* set from retry config */
+retry_config  *retries         = NULL;
+uschar *return_path            = NULL;
+BOOL    return_path_remove     = TRUE;
+int     rewrite_existflags     = 0;
+uschar *rfc1413_hosts          = US"*";
+int     rfc1413_query_timeout  = 5;
+/* BOOL    rfc821_domains         = FALSE;  <<< on the way out */
+uid_t   root_gid               = ROOT_GID;
+uid_t   root_uid               = ROOT_UID;
+
+router_instance  *routers  = NULL;
+router_instance  router_defaults = {
+    NULL,                      /* chain pointer */
+    NULL,                      /* name */
+    NULL,                      /* info */
+    NULL,                      /* private options block pointer */
+    NULL,                      /* driver name */
+
+    NULL,                      /* address_data */
+#ifdef EXPERIMENTAL_BRIGHTMAIL
+    NULL,                      /* bmi_rule */
+#endif
+    NULL,                      /* cannot_route_message */
+    NULL,                      /* condition */
+    NULL,                      /* current_directory */
+    NULL,                      /* debug_string */
+    NULL,                      /* domains */
+    NULL,                      /* errors_to */
+    NULL,                      /* expand_gid */
+    NULL,                      /* expand_uid */
+    NULL,                      /* expand_more */
+    NULL,                      /* expand_unseen */
+    NULL,                      /* extra_headers */
+    NULL,                      /* fallback_hosts */
+    NULL,                      /* home_directory */
+    NULL,                      /* ignore_target_hosts */
+    NULL,                      /* local_parts */
+    NULL,                      /* pass_router_name */
+    NULL,                      /* prefix */
+    NULL,                      /* redirect_router_name */
+    NULL,                      /* remove_headers */
+    NULL,                      /* require_files */
+    NULL,                      /* router_home_directory */
+    US"freeze",                /* self */
+    NULL,                      /* senders */
+    NULL,                      /* suffix */
+    NULL,                      /* translate_ip_address */
+    NULL,                      /* transport_name */
+
+    TRUE,                      /* address_test */
+#ifdef EXPERIMENTAL_BRIGHTMAIL
+    FALSE,                     /* bmi_deliver_alternate */
+    FALSE,                     /* bmi_deliver_default */
+    FALSE,                     /* bmi_dont_deliver */
+#endif
+    TRUE,                      /* expn */
+    FALSE,                     /* caseful_local_part */
+    FALSE,                     /* check_local_user */
+    FALSE,                     /* disable_logging */
+    FALSE,                     /* fail_verify_recipient */
+    FALSE,                     /* fail_verify_sender */
+    FALSE,                     /* gid_set */
+    FALSE,                     /* initgroups */
+    TRUE_UNSET,                /* log_as_local */
+    TRUE,                      /* more */
+    FALSE,                     /* pass_on_timeout */
+    FALSE,                     /* prefix_optional */
+    TRUE,                      /* repeat_use */
+    TRUE_UNSET,                /* retry_use_local_part - fudge "unset" */
+    FALSE,                     /* same_domain_copy_routing */
+    FALSE,                     /* self_rewrite */
+    FALSE,                     /* suffix_optional */
+    FALSE,                     /* verify_only */
+    TRUE,                      /* verify_recipient */
+    TRUE,                      /* verify_sender */
+    FALSE,                     /* uid_set */
+    FALSE,                     /* unseen */
+#ifdef EXPERIMENTAL_DSN
+    FALSE,                     /* dsn_lasthop */
+#endif
+
+    self_freeze,               /* self_code */
+    (uid_t)(-1),               /* uid */
+    (gid_t)(-1),               /* gid */
+
+    NULL,                      /* fallback_hostlist */
+    NULL,                      /* transport instance */
+    NULL,                      /* pass_router */
+    NULL                       /* redirect_router */
+};
+
+uschar *router_name            = NULL;
+
+ip_address_item *running_interfaces = NULL;
+BOOL    running_in_test_harness = FALSE;
+
+/* This is a weird one. The following string gets patched in the binary by the
+script that sets up a copy of Exim for running in the test harness. It seems
+that compilers are now clever, and share constant strings if they can.
+Elsewhere in Exim the string "<" is used. The compiler optimization seems to
+make use of the end of this string in order to save space. So the patching then
+wrecks this. We defeat this optimization by adding some additional characters
+onto the end of the string. */
+
+uschar *running_status         = US">>>running<<<" "\0EXTRA";
+
+int     runrc                  = 0;
+
+uschar *search_error_message   = NULL;
+BOOL    search_find_defer      = FALSE;
+uschar *self_hostname          = NULL;
+uschar *sender_address         = NULL;
+unsigned int sender_address_cache[(MAX_NAMED_LIST * 2)/32];
+uschar *sender_address_data    = NULL;
+BOOL    sender_address_forced  = FALSE;
+uschar *sender_address_unrewritten = NULL;
+uschar *sender_data            = NULL;
+unsigned int sender_domain_cache[(MAX_NAMED_LIST * 2)/32];
+uschar *sender_fullhost        = NULL;
+uschar *sender_helo_name       = NULL;
+uschar **sender_host_aliases   = &no_aliases;
+uschar *sender_host_address    = NULL;
+uschar *sender_host_authenticated = NULL;
+unsigned int sender_host_cache[(MAX_NAMED_LIST * 2)/32];
+BOOL    sender_host_dnssec     = FALSE;
+uschar *sender_host_name       = NULL;
+int     sender_host_port       = 0;
+BOOL    sender_host_notsocket  = FALSE;
+BOOL    sender_host_unknown    = FALSE;
+uschar *sender_ident           = NULL;
+BOOL    sender_local           = FALSE;
+BOOL    sender_name_forced     = FALSE;
+uschar *sender_rate            = NULL;
+uschar *sender_rate_limit      = NULL;
+uschar *sender_rate_period     = NULL;
+uschar *sender_rcvhost         = NULL;
+BOOL    sender_set_untrusted   = FALSE;
+uschar *sender_unqualified_hosts = NULL;
+uschar *sender_verify_failure = NULL;
+address_item *sender_verified_list  = NULL;
+address_item *sender_verified_failed = NULL;
+int     sender_verified_rc     = -1;
+BOOL    sender_verified_responded = FALSE;
+uschar *sending_ip_address     = NULL;
+int     sending_port           = -1;
+SIGNAL_BOOL sigalrm_seen       = FALSE;
+uschar **sighup_argv           = NULL;
+int     smtp_accept_count      = 0;
+BOOL    smtp_accept_keepalive  = TRUE;
+int     smtp_accept_max        = 20;
+int     smtp_accept_max_nonmail= 10;
+uschar *smtp_accept_max_nonmail_hosts = US"*";
+int     smtp_accept_max_per_connection = 1000;
+uschar *smtp_accept_max_per_host = NULL;
+int     smtp_accept_queue      = 0;
+int     smtp_accept_queue_per_connection = 10;
+int     smtp_accept_reserve    = 0;
+uschar *smtp_active_hostname   = NULL;
+BOOL    smtp_authenticated     = FALSE;
+uschar *smtp_banner            = US"$smtp_active_hostname ESMTP "
+                             "Exim $version_number $tod_full"
+                             "\0<---------------Space to patch smtp_banner->";
+BOOL    smtp_batched_input     = FALSE;
+BOOL    smtp_check_spool_space = TRUE;
+int     smtp_ch_index          = 0;
+uschar *smtp_cmd_argument      = NULL;
+uschar *smtp_cmd_buffer        = NULL;
+time_t  smtp_connection_start  = 0;
+uschar  smtp_connection_had[SMTP_HBUFF_SIZE];
+int     smtp_connect_backlog   = 20;
+double  smtp_delay_mail        = 0.0;
+double  smtp_delay_rcpt        = 0.0;
+BOOL    smtp_enforce_sync      = TRUE;
+FILE   *smtp_in                = NULL;
+BOOL    smtp_input             = FALSE;
+int     smtp_load_reserve      = -1;
+int     smtp_mailcmd_count     = 0;
+FILE   *smtp_out               = NULL;
+uschar *smtp_etrn_command      = NULL;
+BOOL    smtp_etrn_serialize    = TRUE;
+int     smtp_max_synprot_errors= 3;
+int     smtp_max_unknown_commands = 3;
+uschar *smtp_notquit_reason    = NULL;
+uschar *smtp_ratelimit_hosts   = NULL;
+uschar *smtp_ratelimit_mail    = NULL;
+uschar *smtp_ratelimit_rcpt    = NULL;
+uschar *smtp_read_error        = US"";
+int     smtp_receive_timeout   = 5*60;
+uschar *smtp_reserve_hosts     = NULL;
+BOOL    smtp_return_error_details = FALSE;
+int     smtp_rlm_base          = 0;
+double  smtp_rlm_factor        = 0.0;
+int     smtp_rlm_limit         = 0;
+int     smtp_rlm_threshold     = INT_MAX;
+int     smtp_rlr_base          = 0;
+double  smtp_rlr_factor        = 0.0;
+int     smtp_rlr_limit         = 0;
+int     smtp_rlr_threshold     = INT_MAX;
+BOOL    smtp_use_pipelining    = FALSE;
+BOOL    smtp_use_size          = FALSE;
+
+#ifdef WITH_CONTENT_SCAN
+uschar *spamd_address          = US"127.0.0.1 783";
+uschar *spam_bar               = NULL;
+uschar *spam_report            = NULL;
+uschar *spam_score             = NULL;
+uschar *spam_score_int         = NULL;
+#endif
+#ifdef EXPERIMENTAL_SPF
+uschar *spf_guess              = US"v=spf1 a/24 mx/24 ptr ?all";
+uschar *spf_header_comment     = NULL;
+uschar *spf_received           = NULL;
+uschar *spf_result             = NULL;
+uschar *spf_smtp_comment       = NULL;
+#endif
+
+BOOL    split_spool_directory  = FALSE;
+uschar *spool_directory        = US SPOOL_DIRECTORY
+                           "\0<--------------Space to patch spool_directory->";
+#ifdef EXPERIMENTAL_SRS
+uschar *srs_config             = NULL;
+uschar *srs_db_address         = NULL;
+uschar *srs_db_key             = NULL;
+int     srs_hashlength         = 6;
+int     srs_hashmin            = -1;
+int     srs_maxage             = 31;
+uschar *srs_orig_recipient     = NULL;
+uschar *srs_orig_sender        = NULL;
+uschar *srs_recipient          = NULL;
+uschar *srs_secrets            = NULL;
+uschar *srs_status             = NULL;
+BOOL    srs_usehash            = TRUE;
+BOOL    srs_usetimestamp       = TRUE;
+#endif
+BOOL    strict_acl_vars        = FALSE;
+int     string_datestamp_offset= -1;
+int     string_datestamp_length= 0;
+int     string_datestamp_type  = -1;
+BOOL    strip_excess_angle_brackets = FALSE;
+BOOL    strip_trailing_dot     = FALSE;
+uschar *submission_domain      = NULL;
+BOOL    submission_mode        = FALSE;
+uschar *submission_name        = NULL;
+BOOL    suppress_local_fixups  = FALSE;
+BOOL    suppress_local_fixups_default = FALSE;
+BOOL    synchronous_delivery   = FALSE;
+BOOL    syslog_duplication     = TRUE;
+int     syslog_facility        = LOG_MAIL;
+uschar *syslog_processname     = US"exim";
+BOOL    syslog_timestamp       = TRUE;
+uschar *system_filter          = NULL;
+
+uschar *system_filter_directory_transport = NULL;
+uschar *system_filter_file_transport = NULL;
+uschar *system_filter_pipe_transport = NULL;
+uschar *system_filter_reply_transport = NULL;
+
+gid_t   system_filter_gid      = 0;
+BOOL    system_filter_gid_set  = FALSE;
+uid_t   system_filter_uid      = (uid_t)-1;
+BOOL    system_filter_uid_set  = FALSE;
+BOOL    system_filtering       = FALSE;
+
+BOOL    tcp_nodelay            = TRUE;
+#ifdef USE_TCP_WRAPPERS
+uschar *tcp_wrappers_daemon_name = US TCP_WRAPPERS_DAEMON_NAME;
+#endif
+int     test_harness_load_avg  = 0;
+int     thismessage_size_limit = 0;
+int     timeout_frozen_after   = 0;
+BOOL    timestamps_utc         = FALSE;
+
+#ifdef EXPERIMENTAL_TPDA
+int     tpda_defer_errno        = 0;
+uschar *tpda_defer_errstr       = NULL;
+uschar *tpda_delivery_ip        = NULL;
+int     tpda_delivery_port      = 0;
+uschar *tpda_delivery_fqdn      = NULL;
+uschar *tpda_delivery_local_part= NULL;
+uschar *tpda_delivery_domain    = NULL;
+uschar *tpda_delivery_confirmation = NULL;
+#endif
+
+transport_instance  *transports = NULL;
+
+transport_instance  transport_defaults = {
+    NULL,                     /* chain pointer */
+    NULL,                     /* name */
+    NULL,                     /* info */
+    NULL,                     /* private options block pointer */
+    NULL,                     /* driver name */
+    NULL,                     /* setup entry point */
+    1,                        /* batch_max */
+    NULL,                     /* batch_id */
+    NULL,                     /* home_dir */
+    NULL,                     /* current_dir */
+    TRUE,                     /* multi-domain */
+    FALSE,                    /* overrides_hosts */
+    100,                      /* max_addresses */
+    500,                      /* connection_max_messages */
+    FALSE,                    /* deliver_as_creator */
+    FALSE,                    /* disable_logging */
+    FALSE,                    /* initgroups */
+    FALSE,                    /* uid_set */
+    FALSE,                    /* gid_set */
+    (uid_t)(-1),              /* uid */
+    (gid_t)(-1),              /* gid */
+    NULL,                     /* expand_uid */
+    NULL,                     /* expand_gid */
+    NULL,                     /* warn_message */
+    NULL,                     /* shadow */
+    NULL,                     /* shadow_condition */
+    NULL,                     /* filter_command */
+    NULL,                     /* add_headers */
+    NULL,                     /* remove_headers */
+    NULL,                     /* return_path */
+    NULL,                     /* debug_string */
+    NULL,                     /* message_size_limit */
+    NULL,                     /* headers_rewrite */
+    NULL,                     /* rewrite_rules */
+    0,                        /* rewrite_existflags */
+    300,                      /* filter_timeout */
+    FALSE,                    /* body_only */
+    FALSE,                    /* delivery_date_add */
+    FALSE,                    /* envelope_to_add */
+    FALSE,                    /* headers_only */
+    FALSE,                    /* rcpt_include_affixes */
+    FALSE,                    /* return_path_add */
+    FALSE,                    /* return_output */
+    FALSE,                    /* return_fail_output */
+    FALSE,                    /* log_output */
+    FALSE,                    /* log_fail_output */
+    FALSE,                    /* log_defer_output */
+    TRUE_UNSET                /* retry_use_local_part: BOOL, but set neither
+                                 1 nor 0 so can detect unset */
+#ifdef EXPERIMENTAL_TPDA
+   ,NULL                     /* tpda_delivery_action */
+#endif
+};
+
+int     transport_count;
+uschar *transport_name          = NULL;
+int     transport_newlines;
+uschar **transport_filter_argv  = NULL;
+int     transport_filter_timeout;
+BOOL    transport_filter_timed_out = FALSE;
+int     transport_write_timeout= 0;
+
+tree_node  *tree_dns_fails     = NULL;
+tree_node  *tree_duplicates    = NULL;
+tree_node  *tree_nonrecipients = NULL;
+tree_node  *tree_unusable      = NULL;
+
+BOOL    trusted_caller         = FALSE;
+BOOL    trusted_config         = TRUE;
+gid_t  *trusted_groups         = NULL;
+uid_t  *trusted_users          = NULL;
+uschar *timezone_string        = US TIMEZONE_DEFAULT;
+
+uschar *unknown_login          = NULL;
+uschar *unknown_username       = NULL;
+uschar *untrusted_set_sender   = NULL;
+
+/*  A regex for matching a "From_" line in an incoming message, in the form
+
+    From ph10 Fri Jan  5 12:35 GMT 1996
+
+which  the "mail" commands send to the MTA (undocumented, of course), or in
+the  form
+
+    From ph10 Fri, 7 Jan 97 14:00:00 GMT
+
+which  is apparently used by some UUCPs, despite it not being in RFC 976.
+Because  of variations in time formats, just match up to the minutes. That
+should  be sufficient. Examples have been seen of time fields like 12:1:03,
+so  just require one digit for hours and minutes. The weekday is also absent
+in  some forms. */
+
+uschar *uucp_from_pattern      = US
+   "^From\\s+(\\S+)\\s+(?:[a-zA-Z]{3},?\\s+)?"    /* Common start */
+   "(?:"                                          /* Non-extracting bracket */
+   "[a-zA-Z]{3}\\s+\\d?\\d|"                      /* First form */
+   "\\d?\\d\\s+[a-zA-Z]{3}\\s+\\d\\d(?:\\d\\d)?"  /* Second form */
+   ")"                                            /* End alternation */
+   "\\s+\\d\\d?:\\d\\d?";                         /* Start of time */
+
+uschar *uucp_from_sender       = US"$1";
+
+uschar *warn_message_file      = NULL;
+uschar *warnmsg_delay          = NULL;
+uschar *warnmsg_recipients     = NULL;
+BOOL    write_rejectlog        = TRUE;
+
+uschar *version_copyright      =
+ US"Copyright (c) University of Cambridge, 1995 - 2014\n"
+   "(c) The Exim Maintainers and contributors in ACKNOWLEDGMENTS file, 2007 - 2014";
+uschar *version_date           = US"?";
+uschar *version_cnumber        = US"????";
+uschar *version_string         = US"?";
+
+int     warning_count          = 0;
+
+/*  End of globals.c */
diff --git a/src/globals.h b/src/globals.h
new file mode 100644 (file)
index 0000000..2bedcf5
--- /dev/null
@@ -0,0 +1,924 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2014 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Almost all the global variables are defined together in this one header, so
+that they are easy to find. However, those that are visible during the
+compilation of the local_scan() function are defined separately in the
+local_scan.h header file. */
+
+/* First put any specials that are required for some operating systems. */
+
+#ifdef NEED_H_ERRNO
+extern int h_errno;
+#endif
+
+/* We need to be careful about width of int and atomicity in signal handlers,
+especially with the rise of 64-bit systems breaking older assumptions.  But
+sig_atomic_t comes from signal.h so can't go into mytypes.h without including
+signal support in local_scan, which seems precipitous. */
+typedef volatile sig_atomic_t SIGNAL_BOOL;
+
+/* Now things that are present only when configured. */
+
+#ifdef EXIM_PERL
+extern uschar *opt_perl_startup;       /* Startup code for Perl interpreter */
+extern BOOL    opt_perl_at_start;      /* Start Perl interpreter at start */
+extern BOOL    opt_perl_started;       /* Set once interpreter started */
+#endif
+
+#ifdef EXPAND_DLFUNC
+extern tree_node *dlobj_anchor;        /* Tree of dynamically-loaded objects */
+#endif
+
+#ifdef LOOKUP_IBASE
+extern uschar *ibase_servers;
+#endif
+
+#ifdef LOOKUP_LDAP
+extern uschar *eldap_ca_cert_dir;      /* Directory with CA certificates */
+extern uschar *eldap_ca_cert_file;     /* CA certificate file */
+extern uschar *eldap_cert_file;        /* Certificate file */
+extern uschar *eldap_cert_key;         /* Certificate key file */
+extern uschar *eldap_cipher_suite;     /* Allowed cipher suite */
+extern uschar *eldap_default_servers;  /* List of default servers */
+extern uschar *eldap_require_cert;     /* Peer certificate checking strategy */
+extern BOOL    eldap_start_tls;        /* Use STARTTLS */
+extern int     eldap_version;          /* LDAP version */
+#endif
+
+#ifdef LOOKUP_MYSQL
+extern uschar *mysql_servers;          /* List of servers and connect info */
+#endif
+
+#ifdef LOOKUP_ORACLE
+extern uschar *oracle_servers;         /* List of servers and connect info */
+#endif
+
+#ifdef LOOKUP_PGSQL
+extern uschar *pgsql_servers;          /* List of servers and connect info */
+#endif
+
+#ifdef EXPERIMENTAL_REDIS
+extern uschar *redis_servers;          /* List of servers and connect info */
+#endif
+
+#ifdef LOOKUP_SQLITE
+extern int     sqlite_lock_timeout;    /* Internal lock waiting timeout */
+#endif
+
+#ifdef SUPPORT_MOVE_FROZEN_MESSAGES
+extern BOOL    move_frozen_messages;   /* Get them out of the normal directory */
+#endif
+
+/* These variables are outside the #ifdef because it keeps the code less
+cluttered in several places (e.g. during logging) if we can always refer to
+them. Also, the tls_ variables are now always visible. */
+
+typedef struct {
+  int     active;             /* fd/socket when in a TLS session */
+  int     bits;               /* bits used in TLS session */
+  BOOL    certificate_verified; /* Client certificate verified */
+  uschar *cipher;             /* Cipher used */
+  BOOL    on_connect;         /* For older MTAs that don't STARTTLS */
+  uschar *on_connect_ports;   /* Ports always tls-on-connect */
+  void   *ourcert;            /* Certificate we presented, binary */
+  void  *peercert;           /* Certificate of peer, binary */
+  uschar *peerdn;             /* DN from peer */
+  uschar *sni;                /* Server Name Indication */
+  enum {
+    OCSP_NOT_REQ=0,            /* not requested */
+    OCSP_NOT_RESP,             /* no response to request */
+    OCSP_VFY_NOT_TRIED,                /* response not verified */
+    OCSP_FAILED,               /* verify failed */
+    OCSP_VFIED                 /* verified */
+    }     ocsp;                      /* Stapled OCSP status */
+} tls_support;
+extern tls_support tls_in;
+extern tls_support tls_out;
+
+#ifdef SUPPORT_TLS
+extern BOOL    gnutls_compat_mode;     /* Less security, more compatibility */
+extern BOOL    gnutls_allow_auto_pkcs11; /* Let GnuTLS autoload PKCS11 modules */
+extern uschar *gnutls_require_mac;     /* So some can be avoided */
+extern uschar *gnutls_require_kx;      /* So some can be avoided */
+extern uschar *gnutls_require_proto;   /* So some can be avoided */
+extern uschar *openssl_options;        /* OpenSSL compatibility options */
+extern const pcre *regex_STARTTLS;     /* For recognizing STARTTLS settings */
+extern uschar *tls_advertise_hosts;    /* host for which TLS is advertised */
+extern uschar *tls_certificate;        /* Certificate file */
+extern uschar *tls_channelbinding_b64; /* string of base64 channel binding */
+extern uschar *tls_crl;                /* CRL File */
+extern int     tls_dh_max_bits;        /* don't accept higher lib suggestions */
+extern uschar *tls_dhparam;            /* DH param file */
+#ifndef DISABLE_OCSP
+extern uschar *tls_ocsp_file;          /* OCSP stapling proof file */
+#endif
+extern BOOL    tls_offered;            /* Server offered TLS */
+extern uschar *tls_privatekey;         /* Private key file */
+extern BOOL    tls_remember_esmtp;     /* For YAEB */
+extern uschar *tls_require_ciphers;    /* So some can be avoided */
+extern uschar *tls_try_verify_hosts;   /* Optional client verification */
+extern uschar *tls_verify_certificates;/* Path for certificates to check */
+extern uschar *tls_verify_hosts;       /* Mandatory client verification */
+#endif
+
+#ifdef EXPERIMENTAL_DSN
+extern uschar  *dsn_envid;             /* DSN envid string */
+extern int      dsn_ret;               /* DSN ret type*/
+extern const pcre  *regex_DSN;         /* For recognizing DSN settings */
+extern BOOL     smtp_use_dsn;          /* Global for passed connections */
+extern uschar  *dsn_advertise_hosts;   /* host for which TLS is advertised */
+#endif
+
+/* Input-reading functions for messages, so we can use special ones for
+incoming TCP/IP. */
+
+extern int (*receive_getc)(void);
+extern int (*receive_ungetc)(int);
+extern int (*receive_feof)(void);
+extern int (*receive_ferror)(void);
+extern BOOL (*receive_smtp_buffered)(void);
+
+
+/* For clearing, saving, restoring address expansion variables. We have to have
+the size of this vector set explicitly, because it is referenced from more than
+one module. */
+
+extern uschar **address_expansions[ADDRESS_EXPANSIONS_COUNT];
+
+/* General global variables */
+
+extern BOOL    accept_8bitmime;        /* Allow *BITMIME incoming */
+extern int     body_8bitmime;          /* sender declared BODY= ; 7=7BIT, 8=8BITMIME */
+extern header_line *acl_added_headers; /* Headers added by an ACL */
+extern tree_node *acl_anchor;          /* Tree of named ACLs */
+extern uschar *acl_arg[9];             /* Argument to ACL call */
+extern int     acl_narg;               /* Number of arguments to ACL call */
+extern uschar *acl_not_smtp;           /* ACL run for non-SMTP messages */
+#ifdef WITH_CONTENT_SCAN
+extern uschar *acl_not_smtp_mime;      /* For MIME parts of ditto */
+#endif
+extern uschar *acl_not_smtp_start;     /* ACL run at the beginning of a non-SMTP session */
+extern uschar *acl_removed_headers;    /* Headers deleted by an ACL */
+extern uschar *acl_smtp_auth;          /* ACL run for AUTH */
+extern uschar *acl_smtp_connect;       /* ACL run on SMTP connection */
+extern uschar *acl_smtp_data;          /* ACL run after DATA received */
+#ifndef DISABLE_PRDR
+extern uschar *acl_smtp_data_prdr;     /* ACL run after DATA received if in PRDR mode*/
+const extern pcre *regex_PRDR;         /* For recognizing PRDR settings */
+#endif
+#ifndef DISABLE_DKIM
+extern uschar *acl_smtp_dkim;          /* ACL run for DKIM signatures / domains */
+#endif
+extern uschar *acl_smtp_etrn;          /* ACL run for ETRN */
+extern uschar *acl_smtp_expn;          /* ACL run for EXPN */
+extern uschar *acl_smtp_helo;          /* ACL run for HELO/EHLO */
+extern uschar *acl_smtp_mail;          /* ACL run for MAIL */
+extern uschar *acl_smtp_mailauth;      /* ACL run for MAIL AUTH */
+#ifdef WITH_CONTENT_SCAN
+extern uschar *acl_smtp_mime;          /* ACL run after DATA, before acl_smtp_data, for each MIME part */
+#endif
+extern uschar *acl_smtp_notquit;       /* ACL run for disconnects */
+extern uschar *acl_smtp_predata;       /* ACL run for DATA command */
+extern uschar *acl_smtp_quit;          /* ACL run for QUIT */
+extern uschar *acl_smtp_rcpt;          /* ACL run for RCPT */
+extern uschar *acl_smtp_starttls;      /* ACL run for STARTTLS */
+extern uschar *acl_smtp_vrfy;          /* ACL run for VRFY */
+extern BOOL    acl_temp_details;       /* TRUE to give details for 4xx error */
+extern tree_node *acl_var_c;           /* ACL connection variables */
+extern tree_node *acl_var_m;           /* ACL messsage variables */
+extern uschar *acl_verify_message;     /* User message for verify failure */
+extern string_item *acl_warn_logged;   /* Logged lines */
+extern uschar *acl_wherecodes[];       /* Response codes for ACL fails */
+extern uschar *acl_wherenames[];       /* Names for messages */
+extern BOOL    active_local_from_check;/* For adding Sender: (switchable) */
+extern BOOL    active_local_sender_retain; /* For keeping Sender: (switchable) */
+extern address_item *addr_duplicate;   /* Duplicate address list */
+extern address_item address_defaults;  /* Default data for address item */
+extern uschar *address_file;           /* Name of file when delivering to one */
+extern uschar *address_pipe;           /* Pipe command when delivering to one */
+extern BOOL    address_test_mode;      /* True for -bt */
+extern tree_node *addresslist_anchor;  /* Tree of defined address lists */
+extern int     addresslist_count;      /* Number defined */
+extern gid_t  *admin_groups;           /* List of admin groups */
+extern BOOL    admin_user;             /* True if caller can do admin */
+extern BOOL    allow_auth_unadvertised;/* As it says */
+extern BOOL    allow_domain_literals;  /* As it says */
+extern BOOL    allow_mx_to_ip;         /* Allow MX records to -> ip address */
+extern BOOL    allow_unqualified_recipient; /* As it says */
+extern BOOL    allow_unqualified_sender; /* Ditto */
+extern BOOL    allow_utf8_domains;     /* For experimenting */
+extern uschar *authenticated_fail_id;  /* ID that failed authentication */
+extern uschar *authenticated_id;       /* ID that was authenticated */
+extern uschar *authenticated_sender;   /* From AUTH on MAIL */
+extern BOOL    authentication_failed;  /* TRUE if AUTH was tried and failed */
+extern uschar *auth_advertise_hosts;   /* Only advertise to these */
+extern auth_info auths_available[];    /* Vector of available auth mechanisms */
+extern auth_instance *auths;           /* Chain of instantiated auths */
+extern auth_instance auth_defaults;    /* Default values */
+extern uschar *auth_defer_msg;         /* Error message for log */
+extern uschar *auth_defer_user_msg;    /* Error message for user */
+extern uschar *auth_vars[];            /* $authn variables */
+extern int     auto_thaw;              /* Auto-thaw interval */
+#ifdef WITH_CONTENT_SCAN
+extern BOOL    av_failed;              /* TRUE if the AV process failed */
+extern uschar *av_scanner;             /* AntiVirus scanner to use for the malware condition */
+#endif
+
+extern BOOL    background_daemon;      /* Set FALSE to keep in foreground */
+extern uschar *base62_chars;           /* Table of base-62 characters */
+extern uschar *bi_command;             /* Command for -bi option */
+extern uschar *big_buffer;             /* Used for various temp things */
+extern int     big_buffer_size;        /* Current size (can expand) */
+#ifdef EXPERIMENTAL_BRIGHTMAIL
+extern uschar *bmi_alt_location;       /* expansion variable that contains the alternate location for the rcpt (available during routing) */
+extern uschar *bmi_base64_tracker_verdict; /* expansion variable with base-64 encoded OLD verdict string (available during routing) */
+extern uschar *bmi_base64_verdict;     /* expansion variable with base-64 encoded verdict string (available during routing) */
+extern uschar *bmi_config_file;        /* Brightmail config file */
+extern int     bmi_deliver;            /* Flag that determines if the message should be delivered to the rcpt (available during routing) */
+extern int     bmi_run;                /* Flag that determines if message should be run through Brightmail server */
+extern uschar *bmi_verdicts;           /* BASE64-encoded verdicts with recipient lists */
+#endif
+extern uschar *bounce_message_file;    /* Template file */
+extern uschar *bounce_message_text;    /* One-liner */
+extern uschar *bounce_recipient;       /* When writing an errmsg */
+extern BOOL    bounce_return_body;     /* Include body in returned message */
+extern BOOL    bounce_return_message;  /* Include message in bounce */
+extern int     bounce_return_size_limit; /* Max amount to return */
+extern uschar *bounce_sender_authentication; /* AUTH address for bounces */
+extern int     bsmtp_transaction_linecount; /* Start of last transaction */
+
+extern int     callout_cache_domain_positive_expire; /* Time for positive domain callout cache records to expire */
+extern int     callout_cache_domain_negative_expire; /* Time for negative domain callout cache records to expire */
+extern int     callout_cache_positive_expire; /* Time for positive callout cache records to expire */
+extern int     callout_cache_negative_expire; /* Time for negative callout cache records to expire */
+extern uschar *callout_random_local_part; /* Local part to be used to check if server called will accept any local part */
+extern uschar *check_dns_names_pattern;/* Regex for syntax check */
+extern int     check_log_inodes;       /* Minimum for message acceptance */
+extern int     check_log_space;        /* Minimum for message acceptance */
+extern BOOL    check_rfc2047_length;   /* Check RFC 2047 encoded string length */
+extern int     check_spool_inodes;     /* Minimum for message acceptance */
+extern int     check_spool_space;      /* Minimum for message acceptance */
+extern uschar *client_authenticator;        /* Authenticator name used for smtp delivery */
+extern uschar *client_authenticated_id;     /* "login" name used for SMTP AUTH */
+extern uschar *client_authenticated_sender; /* AUTH option to SMTP MAIL FROM (not yet used) */
+extern int     clmacro_count;          /* Number of command line macros */
+extern uschar *clmacros[];             /* Copy of them, for re-exec */
+extern int     connection_max_messages;/* Max down one SMTP connection */
+extern BOOL    config_changed;         /* True if -C used */
+extern FILE   *config_file;            /* Configuration file */
+extern uschar *config_filename;        /* Configuration file name */
+#ifdef CONFIGURE_GROUP
+extern gid_t   config_gid;             /* Additional group owner */
+#endif
+extern int     config_lineno;          /* Line number */
+extern uschar *config_main_filelist;   /* List of possible config files */
+extern uschar *config_main_filename;   /* File name actually used */
+#ifdef CONFIGURE_OWNER
+extern uid_t   config_uid;             /* Additional owner */
+#endif
+extern uschar *continue_hostname;      /* Host for continued delivery */
+extern uschar *continue_host_address;  /* IP address for ditto */
+extern BOOL    continue_more;          /* Flag more addresses waiting */
+extern int     continue_sequence;      /* Sequence num for continued delivery */
+extern uschar *continue_transport;     /* Transport for continued delivery */
+
+extern uschar *csa_status;             /* Client SMTP Authorization result */
+extern BOOL    cutthrough_delivery;    /* Deliver in foreground */
+extern int     cutthrough_fd;          /* Connection for ditto */
+
+extern BOOL    daemon_listen;          /* True if listening required */
+extern uschar *daemon_smtp_port;       /* Can be a list of ports */
+extern int     daemon_startup_retries; /* Number of times to retry */
+extern int     daemon_startup_sleep;   /* Sleep between retries */
+
+#ifdef EXPERIMENTAL_DCC
+extern BOOL    dcc_direct_add_header;  /* directly add header */
+extern uschar *dcc_header;             /* dcc header */
+extern uschar *dcc_result;             /* dcc result */
+extern uschar *dccifd_address;         /* address of the dccifd daemon */
+extern uschar *dccifd_options;         /* options for the dccifd daemon */
+#endif
+
+extern BOOL    debug_daemon;           /* Debug the daemon process only */
+extern int     debug_fd;               /* The fd for debug_file */
+extern FILE   *debug_file;             /* Where to write debugging info */
+extern bit_table debug_options[];      /* Table of debug options */
+extern int     debug_options_count;    /* Size of table */
+extern int     delay_warning[];        /* Times between warnings */
+extern uschar *delay_warning_condition; /* Condition string for warnings */
+extern BOOL    delivery_date_remove;   /* Remove delivery-date headers */
+
+extern uschar *deliver_address_data;   /* Arbitrary data for an address */
+extern int     deliver_datafile;       /* FD for data part of message */
+extern uschar *deliver_domain;         /* The local domain for delivery */
+extern uschar *deliver_domain_data;    /* From domain lookup */
+extern uschar *deliver_domain_orig;    /* The original local domain for delivery */
+extern uschar *deliver_domain_parent;  /* The parent domain for delivery */
+extern BOOL    deliver_drop_privilege; /* TRUE for unprivileged delivery */
+extern BOOL    deliver_firsttime;      /* True for first delivery attempt */
+extern BOOL    deliver_force;          /* TRUE if delivery was forced */
+extern BOOL    deliver_freeze;         /* TRUE if delivery is frozen */
+extern time_t  deliver_frozen_at;      /* Time of freezing */
+extern uschar *deliver_home;           /* Home directory for pipes */
+extern uschar *deliver_host;           /* (First) host for routed local deliveries */
+                                       /* Remote host for filter */
+extern uschar *deliver_host_address;   /* Address for remote delivery filter */
+extern uschar *deliver_in_buffer;      /* Buffer for copying file */
+extern ino_t   deliver_inode;          /* Inode for appendfile */
+extern uschar *deliver_localpart;      /* The local part for delivery */
+extern uschar *deliver_localpart_data; /* From local part lookup */
+extern uschar *deliver_localpart_orig; /* The original local part for delivery */
+extern uschar *deliver_localpart_parent; /* The parent local part for delivery */
+extern uschar *deliver_localpart_prefix; /* The stripped prefix, if any */
+extern uschar *deliver_localpart_suffix; /* The stripped suffix, if any */
+extern BOOL    deliver_force_thaw;     /* TRUE to force thaw in queue run */
+extern BOOL    deliver_manual_thaw;    /* TRUE if manually thawed */
+extern uschar *deliver_out_buffer;     /* Buffer for copying file */
+extern int     deliver_queue_load_max; /* Different value for queue running */
+extern address_item *deliver_recipients; /* Current set of addresses */
+extern uschar *deliver_selectstring;   /* For selecting by recipient */
+extern BOOL    deliver_selectstring_regex; /* String is regex */
+extern uschar *deliver_selectstring_sender; /* For selecting by sender */
+extern BOOL    deliver_selectstring_sender_regex; /* String is regex */
+#ifdef WITH_OLD_DEMIME
+extern int     demime_errorlevel;      /* Severity of MIME error */
+extern int     demime_ok;              /* Nonzero if message has been demimed */
+extern uschar *demime_reason;          /* Reason for broken MIME container */
+#endif
+extern BOOL    disable_callout_flush;  /* Don't flush before callouts */
+extern BOOL    disable_delay_flush;    /* Don't flush before "delay" in ACL */
+#ifdef ENABLE_DISABLE_FSYNC
+extern BOOL    disable_fsync;          /* Not for normal use */
+#endif
+extern BOOL    disable_ipv6;           /* Don't do any IPv6 things */
+extern BOOL    disable_logging;        /* Disables log writing when TRUE */
+
+#ifndef DISABLE_DKIM
+extern uschar *dkim_cur_signer;        /* Expansion variable, holds the current "signer" domain or identity during a acl_smtp_dkim run */
+extern uschar *dkim_signers;           /* Expansion variable, holds colon-separated list of domains and identities that have signed a message */
+extern uschar *dkim_signing_domain;    /* Expansion variable, domain used for signing a message. */
+extern uschar *dkim_signing_selector;  /* Expansion variable, selector used for signing a message. */
+extern uschar *dkim_verify_signers;    /* Colon-separated list of domains for each of which we call the DKIM ACL */
+extern BOOL    dkim_collect_input;     /* Runtime flag that tracks wether SMTP input is fed to DKIM validation */
+extern BOOL    dkim_disable_verify;    /* Set via ACL control statement. When set, DKIM verification is disabled for the current message */
+#endif
+#ifdef EXPERIMENTAL_DMARC
+extern BOOL    dmarc_has_been_checked; /* Global variable to check if test has been called yet */
+extern uschar *dmarc_ar_header;        /* Expansion variable, suggested header for dmarc auth results */
+extern uschar *dmarc_domain_policy;    /* Expansion for declared policy of used domain */
+extern uschar *dmarc_forensic_sender;  /* Set sender address for forensic reports */
+extern uschar *dmarc_history_file;     /* Expansion variable, file to store dmarc results */
+extern uschar *dmarc_status;           /* Expansion variable, one word value */
+extern uschar *dmarc_status_text;      /* Expansion variable, human readable value */
+extern uschar *dmarc_tld_file;         /* Mozilla TLDs text file */
+extern uschar *dmarc_used_domain;      /* Expansion variable, domain libopendmarc chose for DMARC policy lookup */
+extern BOOL    dmarc_disable_verify;   /* Set via ACL control statement. When set, DMARC verification is disabled for the current message */
+extern BOOL    dmarc_enable_forensic;  /* Set via ACL control statement. When set, DMARC forensic reports are enabled for the current message */
+#endif
+
+extern uschar *dns_again_means_nonexist; /* Domains that are badly set up */
+extern int     dns_csa_search_limit;   /* How deep to search for CSA SRV records */
+extern BOOL    dns_csa_use_reverse;    /* Check CSA in reverse DNS? (non-standard) */
+extern uschar *dns_ipv4_lookup;        /* For these domains, don't look for AAAA (or A6) */
+extern int     dns_retrans;            /* Retransmission time setting */
+extern int     dns_retry;              /* Number of retries */
+extern int     dns_dnssec_ok;          /* When constructing DNS query, set DO flag */
+extern int     dns_use_edns0;          /* Coerce EDNS0 support on/off in resolver. */
+extern uschar *dnslist_domain;         /* DNS (black) list domain */
+extern uschar *dnslist_matched;        /* DNS (black) list matched key */
+extern uschar *dnslist_text;           /* DNS (black) list text message */
+extern uschar *dnslist_value;          /* DNS (black) list IP address */
+extern tree_node *domainlist_anchor;   /* Tree of defined domain lists */
+extern int     domainlist_count;       /* Number defined */
+extern BOOL    dont_deliver;           /* TRUE for -N option */
+extern BOOL    dot_ends;               /* TRUE if "." ends non-SMTP input */
+
+/* This option is now a no-opt, retained for compatibility */
+extern BOOL    drop_cr;                /* For broken local MUAs */
+
+extern uschar *dsn_from;               /* From: string for DSNs */
+
+extern BOOL    enable_dollar_recipients; /* Make $recipients available */
+extern int     envelope_to_remove;     /* Remove envelope_to_headers */
+extern int     errno_quota;            /* Quota errno in this OS */
+extern int     error_handling;         /* Error handling style */
+extern uschar *errors_copy;            /* For taking copies of errors */
+extern uschar *errors_reply_to;        /* Reply-to for error messages */
+extern int     errors_sender_rc;       /* Return after message to sender*/
+extern gid_t   exim_gid;               /* To be used with exim_uid */
+extern BOOL    exim_gid_set;           /* TRUE if exim_gid set */
+extern uschar *exim_path;              /* Path to exec exim */
+extern const uschar *exim_sieve_extension_list[]; /* list of sieve extensions */
+extern uid_t   exim_uid;               /* Non-root uid for exim */
+extern BOOL    exim_uid_set;           /* TRUE if exim_uid set */
+extern int     expand_forbid;          /* RDO flags for forbidding things */
+extern int     expand_nlength[];       /* Lengths of numbered strings */
+extern int     expand_nmax;            /* Max numerical value */
+extern uschar *expand_nstring[];       /* Numbered strings */
+extern BOOL    expand_string_forcedfail; /* TRUE if failure was "expected" */
+extern BOOL    extract_addresses_remove_arguments; /* Controls -t behaviour */
+extern uschar *extra_local_interfaces; /* Local, non-listen interfaces */
+
+extern int     fake_response;          /* Fake FAIL or DEFER response to data */
+extern uschar *fake_response_text;     /* User defined message for the above. Default is in globals.c. */
+extern int     filter_n[FILTER_VARIABLE_COUNT]; /* filter variables */
+extern BOOL    filter_running;         /* TRUE while running a filter */
+extern int     filter_sn[FILTER_VARIABLE_COUNT]; /* variables set by system filter */
+extern int     filter_test;            /* Filter test type */
+extern uschar *filter_test_sfile;      /* System filter test file */
+extern uschar *filter_test_ufile;      /* User filter test file */
+extern uschar *filter_thisaddress;     /* For address looping */
+extern int     finduser_retries;       /* Retry count for getpwnam() */
+extern uid_t   fixed_never_users[];    /* Can't be overridden */
+#ifdef WITH_OLD_DEMIME
+extern uschar *found_extension;        /* demime acl condition: file extension found */
+#endif
+extern uschar *freeze_tell;            /* Message on (some) freezings */
+extern uschar *freeze_tell_config;     /* The configured setting */
+extern uschar *fudged_queue_times;     /* For use in test harness */
+
+extern uschar *gecos_name;             /* To be expanded when pattern matches */
+extern uschar *gecos_pattern;          /* Pattern to match */
+extern rewrite_rule *global_rewrite_rules;  /* Chain of rewriting rules */
+
+extern int     header_insert_maxlen;   /* Max for inserting headers */
+extern int     header_maxsize;         /* Max total length for header */
+extern int     header_line_maxsize;    /* Max for an individual line */
+extern header_name header_names[];     /* Table of header names */
+extern int     header_names_size;      /* Number of entries */
+extern BOOL    header_rewritten;       /* TRUE if header changed by router */
+extern uschar *helo_accept_junk_hosts; /* Allowed to use junk arg */
+extern uschar *helo_allow_chars;       /* Rogue chars to allow in HELO/EHLO */
+extern uschar *helo_lookup_domains;    /* If these given, lookup host name */
+extern uschar *helo_try_verify_hosts;  /* Soft check HELO argument for these */
+extern BOOL    helo_verified;          /* True if HELO verified */
+extern BOOL    helo_verify_failed;     /* True if attempt failed */
+extern uschar *helo_verify_hosts;      /* Hard check HELO argument for these */
+extern const uschar *hex_digits;             /* Used in several places */
+extern uschar *hold_domains;           /* Hold up deliveries to these */
+extern BOOL    host_find_failed_syntax;/* DNS syntax check failure */
+extern BOOL    host_checking_callout;  /* TRUE if real callout wanted */
+extern uschar *host_data;              /* Obtained from lookup in ACL */
+extern uschar *host_lookup;            /* For which IP addresses are always looked up */
+extern BOOL    host_lookup_deferred;   /* TRUE if lookup deferred */
+extern BOOL    host_lookup_failed;     /* TRUE if lookup failed */
+extern uschar *host_lookup_order;      /* Order of host lookup types */
+extern uschar *host_lookup_msg;        /* Text for why it failed */
+extern int     host_number;            /* For sharing spools */
+extern uschar *host_number_string;     /* For expanding */
+extern uschar *host_reject_connection; /* Reject these hosts */
+extern tree_node *hostlist_anchor;     /* Tree of defined host lists */
+extern int     hostlist_count;         /* Number defined */
+extern uschar *hosts_connection_nolog; /* Limits the logging option */
+extern uschar *hosts_treat_as_local;   /* For routing */
+
+extern int     ignore_bounce_errors_after; /* Keep them for this time. */
+extern BOOL    ignore_fromline_local;  /* Local SMTP ignore fromline */
+extern uschar *ignore_fromline_hosts;  /* Hosts permitted to send "From " */
+extern BOOL    inetd_wait_mode;        /* Whether running in inetd wait mode */
+extern int     inetd_wait_timeout;     /* Timeout for inetd wait mode */
+extern BOOL    is_inetd;               /* True for inetd calls */
+extern uschar *iterate_item;           /* Item from iterate list */
+
+extern int     journal_fd;             /* Fd for journal file */
+
+extern int     keep_malformed;         /* Time to keep malformed messages */
+
+extern uschar *eldap_dn;               /* Where LDAP DNs are left */
+extern int     load_average;           /* Most recently read load average */
+extern BOOL    local_error_message;    /* True if handling one of these */
+extern BOOL    local_from_check;       /* For adding Sender: (global value) */
+extern uschar *local_from_prefix;      /* Permitted prefixes */
+extern uschar *local_from_suffix;      /* Permitted suffixes */
+extern uschar *local_interfaces;       /* For forcing specific interfaces */
+extern uschar *local_scan_data;        /* Text returned by local_scan() */
+extern optionlist local_scan_options[];/* Option list for local_scan() */
+extern int     local_scan_options_count; /* Size of the list */
+extern int     local_scan_timeout;     /* Timeout for local_scan() */
+extern BOOL    local_sender_retain;    /* Retain Sender: (with no From: check) */
+extern gid_t   local_user_gid;         /* As it says; may be set in routers */
+extern uid_t   local_user_uid;         /* As it says; may be set in routers */
+extern tree_node *localpartlist_anchor;/* Tree of defined localpart lists */
+extern int     localpartlist_count;    /* Number defined */
+extern uschar *log_buffer;             /* For constructing log entries */
+extern unsigned int log_extra_selector;/* Bit map of logging options other than used by log_write() */
+extern uschar *log_file_path;          /* If unset, use default */
+extern bit_table log_options[];        /* Table of options */
+extern int     log_options_count;      /* Size of table */
+extern int     log_reject_target;      /* Target log for ACL rejections */
+extern uschar *log_selector_string;    /* As supplied in the config */
+extern FILE   *log_stderr;             /* Copy of stderr for log use, or NULL */
+extern BOOL    log_testing_mode;       /* TRUE in various testing modes */
+extern BOOL    log_timezone;           /* TRUE to include the timezone in log lines */
+extern unsigned int log_write_selector;/* Bit map of logging options for log_write() */
+extern uschar *login_sender_address;   /* The actual sender address */
+extern lookup_info **lookup_list;      /* Array of pointers to available lookups */
+extern int     lookup_list_count;      /* Number of entries in the list */
+extern uschar *lookup_dnssec_authenticated; /* AD status of dns lookup */
+extern int     lookup_open_max;        /* Max lookup files to cache */
+extern uschar *lookup_value;           /* Value looked up from file */
+
+extern macro_item *macros;             /* Configuration macros */
+extern uschar *mailstore_basename;     /* For mailstore deliveries */
+#ifdef WITH_CONTENT_SCAN
+extern uschar *malware_name;           /* Name of virus or malware ("W32/Klez-H") */
+#endif
+extern int     max_received_linelength;/* What it says */
+extern int     max_username_length;    /* For systems with broken getpwnam() */
+extern int     message_age;            /* In seconds */
+extern uschar *message_body;           /* Start of message body for filter */
+extern uschar *message_body_end;       /* End of message body for filter */
+extern BOOL    message_body_newlines;  /* FALSE => remove newlines */
+extern int     message_body_size;      /* Sic */
+extern int     message_body_visible;   /* Amount visible in message_body */
+extern int     message_ended;          /* State of message reading and how ended */
+extern uschar *message_headers;        /* When built */
+extern uschar  message_id_option[];    /* -E<message-id> for use as option */
+extern uschar *message_id_external;    /* External form of following */
+extern uschar *message_id_domain;      /* Expanded to form domain-part of message_id */
+extern uschar *message_id_text;        /* Expanded to form message_id */
+extern struct timeval message_id_tv;   /* Time used to create last message_id */
+extern int     message_linecount;      /* As it says */
+extern BOOL    message_logs;           /* TRUE to write message logs */
+extern int     message_size;           /* Size of message */
+extern uschar *message_size_limit;     /* As it says */
+extern uschar  message_subdir[];       /* Subdirectory for messages */
+extern uschar *message_reference;      /* Reference for error messages */
+
+/* MIME ACL expandables */
+#ifdef WITH_CONTENT_SCAN
+extern int     mime_anomaly_level;
+extern const uschar *mime_anomaly_text;
+extern uschar *mime_boundary;
+extern uschar *mime_charset;
+extern uschar *mime_content_description;
+extern uschar *mime_content_disposition;
+extern uschar *mime_content_id;
+extern unsigned int mime_content_size;
+extern uschar *mime_content_transfer_encoding;
+extern uschar *mime_content_type;
+extern uschar *mime_decoded_filename;
+extern uschar *mime_filename;
+extern int     mime_is_multipart;
+extern int     mime_is_coverletter;
+extern int     mime_is_rfc822;
+extern int     mime_part_count;
+#endif
+
+extern BOOL    mua_wrapper;            /* TRUE when Exim is wrapping an MUA */
+
+extern uid_t  *never_users;            /* List of uids never to be used */
+#ifdef WITH_CONTENT_SCAN
+extern BOOL    no_mbox_unspool;        /* don't unlink files in /scan directory */
+#endif
+extern BOOL    no_multiline_responses; /* For broken clients */
+
+extern optionlist optionlist_auths[];      /* These option lists are made */
+extern int     optionlist_auths_size;      /* global so that readconf can */
+extern optionlist optionlist_routers[];    /* see them for printing out   */
+extern int     optionlist_routers_size;    /* the options.                */
+extern optionlist optionlist_transports[];
+extern int     optionlist_transports_size;
+
+extern uid_t   original_euid;          /* Original effective uid */
+extern gid_t   originator_gid;         /* Gid of whoever wrote spool file */
+extern uschar *originator_login;       /* Login of same */
+extern uschar *originator_name;        /* Full name of same */
+extern uid_t   originator_uid;         /* Uid of ditto */
+extern uschar *override_local_interfaces; /* Value of -oX argument */
+extern uschar *override_pid_file_path; /* Value of -oP argument */
+
+extern BOOL    parse_allow_group;      /* Allow group syntax */
+extern BOOL    parse_found_group;      /* In the middle of a group */
+extern uschar *percent_hack_domains;   /* Local domains for which '% operates */
+extern uschar *pid_file_path;          /* For writing daemon pids */
+extern uschar *pipelining_advertise_hosts; /* As it says */
+extern BOOL    pipelining_enable;      /* As it says */
+#ifndef DISABLE_PRDR
+extern BOOL    prdr_enable;            /* As it says */
+extern BOOL    prdr_requested;         /* Connecting mail server wants PRDR */
+#endif
+extern BOOL    preserve_message_logs;  /* Save msglog files */
+extern uschar *primary_hostname;       /* Primary name of this computer */
+extern BOOL    print_topbitchars;      /* Topbit chars are printing chars */
+extern uschar  process_info[];         /* For SIGUSR1 output */
+extern int     process_info_len;
+extern uschar *process_log_path;       /* Alternate path */
+extern BOOL    prod_requires_admin;    /* TRUE if prodding requires admin */
+
+#ifdef EXPERIMENTAL_PROXY
+extern uschar *proxy_host_address;     /* IP of host being proxied */
+extern int     proxy_host_port;        /* Port of host being proxied */
+extern uschar *proxy_required_hosts;   /* Hostlist which (require) use proxy protocol */
+extern BOOL    proxy_session;          /* TRUE if receiving mail from valid proxy  */
+extern BOOL    proxy_session_failed;   /* TRUE if required proxy negotiation failed */
+extern uschar *proxy_target_address;   /* IP of proxy server inbound */
+extern int     proxy_target_port;      /* Port of proxy server inbound */
+#endif
+
+extern uschar *prvscheck_address;      /* Set during prvscheck expansion item */
+extern uschar *prvscheck_keynum;       /* Set during prvscheck expansion item */
+extern uschar *prvscheck_result;       /* Set during prvscheck expansion item */
+
+extern uschar *qualify_domain_recipient; /* Domain to qualify recipients with */
+extern uschar *qualify_domain_sender;  /* Domain to qualify senders with */
+extern BOOL    queue_2stage;           /* Run queue in 2-stage manner */
+extern uschar *queue_domains;          /* Queue these domains */
+extern BOOL    queue_list_requires_admin; /* TRUE if -bp requires admin */
+extern BOOL    queue_run_first_delivery; /* If TRUE, first deliveries only */
+extern BOOL    queue_run_force;        /* TRUE to force during queue run */
+extern BOOL    queue_run_local;        /* Local deliveries only in queue run */
+extern BOOL    queue_running;          /* TRUE for queue running process and */
+                                       /*   immediate children */
+extern pid_t   queue_run_pid;          /* PID of the queue running process or 0 */
+extern int     queue_run_pipe;         /* Pipe for synchronizing */
+extern int     queue_interval;         /* Queue running interval */
+extern BOOL    queue_only;             /* TRUE to disable immediate delivery */
+extern int     queue_only_load;        /* Max load before auto-queue */
+extern BOOL    queue_only_load_latch;  /* Latch queue_only_load TRUE */
+extern uschar *queue_only_file;        /* Queue if file exists/not-exists */
+extern BOOL    queue_only_override;    /* Allow override from command line */
+extern BOOL    queue_only_policy;      /* ACL or local_scan wants queue_only */
+extern BOOL    queue_run_in_order;     /* As opposed to random */
+extern int     queue_run_max;          /* Max queue runners */
+extern BOOL    queue_smtp;             /* Disable all immediate STMP (-odqs)*/
+extern uschar *queue_smtp_domains;     /* Ditto, for these domains */
+
+extern unsigned int random_seed;       /* Seed for random numbers */
+extern tree_node *ratelimiters_cmd;    /* Results of command ratelimit checks */
+extern tree_node *ratelimiters_conn;   /* Results of connection ratelimit checks */
+extern tree_node *ratelimiters_mail;   /* Results of per-mail ratelimit checks */
+extern uschar *raw_active_hostname;    /* Pre-expansion */
+extern uschar *raw_sender;             /* Before rewriting */
+extern uschar **raw_recipients;        /* Before rewriting */
+extern int     raw_recipients_count;
+extern int     rcpt_count;             /* Count of RCPT commands in a message */
+extern int     rcpt_fail_count;        /* Those that got 5xx */
+extern int     rcpt_defer_count;       /* Those that got 4xx */
+extern gid_t   real_gid;               /* Real gid */
+extern uid_t   real_uid;               /* Real user running program */
+extern BOOL    really_exim;            /* FALSE in utilities */
+extern BOOL    receive_call_bombout;   /* Flag for crashing log */
+extern int     receive_linecount;      /* Mainly for BSMTP errors */
+extern int     receive_messagecount;   /* Mainly for BSMTP errors */
+extern int     receive_timeout;        /* For non-SMTP acceptance */
+extern int     received_count;         /* Count of Received: headers */
+extern uschar *received_for;           /* For "for" field */
+extern uschar *received_header_text;   /* Definition of Received: header */
+extern int     received_headers_max;   /* Max count of Received: headers */
+extern int     received_time;          /* Time the message was received */
+extern uschar *recipient_data;         /* lookup data for recipients */
+extern uschar *recipient_unqualified_hosts; /* Permitted unqualified recipients */
+extern uschar *recipient_verify_failure; /* What went wrong */
+extern BOOL    recipients_discarded;   /* By an ACL */
+extern int     recipients_list_max;    /* Maximum number fitting in list */
+extern int     recipients_max;         /* Max permitted */
+extern int     recipients_max_reject;  /* If TRUE, reject whole message */
+extern const pcre *regex_AUTH;         /* For recognizing AUTH settings */
+extern const pcre  *regex_check_dns_names; /* For DNS name checking */
+extern const pcre  *regex_From;        /* For recognizing "From_" lines */
+extern const pcre  *regex_IGNOREQUOTA; /* For recognizing IGNOREQUOTA (LMTP) */
+extern const pcre  *regex_PIPELINING;  /* For recognizing PIPELINING */
+extern const pcre  *regex_SIZE;        /* For recognizing SIZE settings */
+extern const pcre  *regex_smtp_code;   /* For recognizing SMTP codes */
+extern const pcre  *regex_ismsgid;     /* Compiled r.e. for message it */
+#ifdef WHITELIST_D_MACROS
+extern const pcre  *regex_whitelisted_macro; /* For -D macro values */
+#endif
+#ifdef WITH_CONTENT_SCAN
+extern uschar *regex_match_string;     /* regex that matched a line (regex ACL condition) */
+#endif
+extern int     remote_delivery_count;  /* Number of remote addresses */
+extern int     remote_max_parallel;    /* Maximum parallel delivery */
+extern uschar *remote_sort_domains;    /* Remote domain sorting order */
+extern retry_config *retries;          /* Chain of retry config information */
+extern int     retry_data_expire;      /* When to expire retry data */
+extern int     retry_interval_max;     /* Absolute maximum */
+extern int     retry_maximum_timeout;  /* The maximum timeout */
+extern uschar *return_path;            /* Return path for a message */
+extern BOOL    return_path_remove;     /* Remove return-path headers */
+extern int     rewrite_existflags;     /* Indicate which headers have rewrites */
+extern uschar *rfc1413_hosts;          /* RFC hosts */
+extern int     rfc1413_query_timeout;  /* Timeout on RFC 1413 calls */
+/* extern BOOL    rfc821_domains;  */       /* If set, syntax is 821, not 822 => being abolished */
+extern uid_t   root_gid;               /* The gid for root */
+extern uid_t   root_uid;               /* The uid for root */
+extern router_info routers_available[];/* Vector of available routers */
+extern router_instance *routers;       /* Chain of instantiated routers */
+extern router_instance router_defaults;/* Default values */
+extern uschar *router_name;            /* Name of router last started */
+extern BOOL    running_in_test_harness; /*TRUE when running_status is patched */
+extern ip_address_item *running_interfaces; /* Host's running interfaces */
+extern uschar *running_status;         /* Flag string for testing */
+extern int     runrc;                  /* rc from ${run} */
+
+extern uschar *search_error_message;   /* Details of lookup problem */
+extern BOOL    search_find_defer;      /* Set TRUE if lookup deferred */
+extern uschar *self_hostname;          /* Self host after routing->directors */
+extern unsigned int sender_address_cache[(MAX_NAMED_LIST * 2)/32]; /* Cache bits for sender */
+extern uschar *sender_address_data;    /* address_data from sender verify */
+extern BOOL    sender_address_forced;  /* Set by -f */
+extern uschar *sender_address_unrewritten; /* Set if rewritten by verify */
+extern uschar *sender_data;            /* lookup result for senders */
+extern unsigned int sender_domain_cache[(MAX_NAMED_LIST * 2)/32]; /* Cache bits for sender domain */
+extern uschar *sender_fullhost;        /* Sender host name + address */
+extern uschar *sender_helo_name;       /* Host name from HELO/EHLO */
+extern uschar **sender_host_aliases;   /* Points to list of alias names */
+extern unsigned int sender_host_cache[(MAX_NAMED_LIST * 2)/32]; /* Cache bits for incoming host */
+extern BOOL    sender_host_dnssec;     /* true if sender_host_name verified in DNSSEC */
+extern BOOL    sender_host_notsocket;  /* Set for -bs and -bS */
+extern BOOL    sender_host_unknown;    /* TRUE for -bs and -bS except inetd */
+extern uschar *sender_ident;           /* Sender identity via RFC 1413 */
+extern BOOL    sender_local;           /* TRUE for local senders */
+extern BOOL    sender_name_forced;     /* Set by -F */
+extern uschar *sender_rate;            /* Sender rate computed by ACL */
+extern uschar *sender_rate_limit;      /* Configured rate limit */
+extern uschar *sender_rate_period;     /* Configured smoothing period */
+extern uschar *sender_rcvhost;         /* Host data for Received: */
+extern BOOL    sender_set_untrusted;   /* Sender set by untrusted caller */
+extern uschar *sender_unqualified_hosts; /* Permitted unqualified senders */
+extern uschar *sender_verify_failure;  /* What went wrong */
+extern address_item *sender_verified_list; /* Saved chain of sender verifies */
+extern address_item *sender_verified_failed; /* The one that caused denial */
+extern uschar *sending_ip_address;     /* Address of outgoing (SMTP) interface */
+extern int     sending_port;           /* Port of outgoing interface */
+extern SIGNAL_BOOL sigalrm_seen;       /* Flag for sigalrm_handler */
+extern uschar **sighup_argv;           /* Args for re-execing after SIGHUP */
+extern int     smtp_accept_count;      /* Count of connections */
+extern BOOL    smtp_accept_keepalive;  /* Set keepalive on incoming */
+extern int     smtp_accept_max;        /* Max SMTP connections */
+extern int     smtp_accept_max_nonmail;/* Max non-mail commands in one con */
+extern uschar *smtp_accept_max_nonmail_hosts; /* Limit non-mail cmds from these hosts */
+extern int     smtp_accept_max_per_connection; /* Max msgs per connection */
+extern uschar *smtp_accept_max_per_host; /* Max SMTP cons from one IP addr */
+extern int     smtp_accept_queue;      /* Queue after so many connections */
+extern int     smtp_accept_queue_per_connection; /* Queue after so many msgs */
+extern int     smtp_accept_reserve;    /* Reserve these SMTP connections */
+extern uschar *smtp_active_hostname;   /* Hostname for this message */
+extern BOOL    smtp_authenticated;     /* Sending client has authenticated */
+extern uschar *smtp_banner;            /* Banner string (to be expanded) */
+extern BOOL    smtp_check_spool_space; /* TRUE to check SMTP SIZE value */
+extern int     smtp_ch_index;          /* Index in smtp_connection_had */
+extern uschar *smtp_cmd_argument;      /* For all SMTP commands */
+extern uschar *smtp_cmd_buffer;        /* SMTP command buffer */
+extern time_t  smtp_connection_start;  /* Start time of SMTP connection */
+extern uschar  smtp_connection_had[];  /* Recent SMTP commands */
+extern int     smtp_connect_backlog;   /* Max backlog permitted */
+extern double  smtp_delay_mail;        /* Current MAIL delay */
+extern double  smtp_delay_rcpt;        /* Current RCPT delay */
+extern BOOL    smtp_enforce_sync;      /* Enforce sync rules */
+extern uschar *smtp_etrn_command;      /* Command to run */
+extern BOOL    smtp_etrn_serialize;    /* Only one at once */
+extern FILE   *smtp_in;                /* Incoming SMTP input file */
+extern int     smtp_load_reserve;      /* Only from reserved if load > this */
+extern int     smtp_mailcmd_count;     /* Count of MAIL commands */
+extern int     smtp_max_synprot_errors;/* Max syntax/protocol errors */
+extern int     smtp_max_unknown_commands; /* As it says */
+extern uschar *smtp_notquit_reason;    /* Global for disconnect reason */
+extern FILE   *smtp_out;               /* Incoming SMTP output file */
+extern uschar *smtp_ratelimit_hosts;   /* Rate limit these hosts */
+extern uschar *smtp_ratelimit_mail;    /* Parameters for MAIL limiting */
+extern uschar *smtp_ratelimit_rcpt;    /* Parameters for RCPT limiting */
+extern uschar *smtp_read_error;        /* Message for SMTP input error */
+extern int     smtp_receive_timeout;   /* Applies to each received line */
+extern uschar *smtp_reserve_hosts;     /* Hosts for reserved slots */
+extern BOOL    smtp_return_error_details; /* TRUE to return full info */
+extern int     smtp_rlm_base;          /* Base interval for MAIL rate limit */
+extern double  smtp_rlm_factor;        /* Factor for MAIL rate limit */
+extern int     smtp_rlm_limit;         /* Max delay */
+extern int     smtp_rlm_threshold;     /* Threshold for RCPT rate limit */
+extern int     smtp_rlr_base;          /* Base interval for RCPT rate limit */
+extern double  smtp_rlr_factor;        /* Factor for RCPT rate limit */
+extern int     smtp_rlr_limit;         /* Max delay */
+extern int     smtp_rlr_threshold;     /* Threshold for RCPT rate limit */
+extern BOOL    smtp_use_pipelining;    /* Global for passed connections */
+extern BOOL    smtp_use_size;          /* Global for passed connections */
+
+#ifdef WITH_CONTENT_SCAN
+extern uschar *spamd_address;          /* address for the spamassassin daemon */
+extern uschar *spam_bar;               /* the spam "bar" (textual representation of spam_score) */
+extern uschar *spam_report;            /* the spamd report (multiline) */
+extern uschar *spam_score;             /* the spam score (float) */
+extern uschar *spam_score_int;         /* spam_score * 10 (int) */
+#endif
+#ifdef EXPERIMENTAL_SPF
+extern uschar *spf_guess;              /* spf best-guess record */
+extern uschar *spf_header_comment;     /* spf header comment */
+extern uschar *spf_received;           /* Received-SPF: header */
+extern uschar *spf_result;             /* spf result in string form */
+extern uschar *spf_smtp_comment;       /* spf comment to include in SMTP reply */
+#endif
+extern BOOL    split_spool_directory;  /* TRUE to use multiple subdirs */
+extern uschar *spool_directory;        /* Name of spool directory */
+#ifdef EXPERIMENTAL_SRS
+extern uschar *srs_config;             /* SRS config secret:max age:hash length:use timestamp:use hash */
+extern uschar *srs_db_address;         /* SRS db address */
+extern uschar *srs_db_key;             /* SRS db key */
+extern int     srs_hashlength;         /* SRS hash length */
+extern int     srs_hashmin;            /* SRS minimum hash length */
+extern int     srs_maxage;             /* SRS max age */
+extern uschar *srs_orig_sender;        /* SRS original sender */
+extern uschar *srs_orig_recipient;     /* SRS original recipient */
+extern uschar *srs_recipient;          /* SRS recipient */
+extern uschar *srs_secrets;            /* SRS secrets list */
+extern uschar *srs_status;             /* SRS staus */
+extern BOOL    srs_usehash;            /* SRS use hash flag */
+extern BOOL    srs_usetimestamp;       /* SRS use timestamp flag */
+#endif
+extern BOOL    strict_acl_vars;        /* ACL variables have to be set before being used */
+extern int     string_datestamp_offset;/* After insertion by string_format */
+extern int     string_datestamp_length;/* After insertion by string_format */
+extern int     string_datestamp_type;  /* After insertion by string_format */
+extern BOOL    strip_excess_angle_brackets; /* Surrounding route-addrs */
+extern BOOL    strip_trailing_dot;     /* Remove dots at ends of domains */
+extern uschar *submission_domain;      /* Domain for submission mode */
+extern BOOL    submission_mode;        /* Can be forced from ACL */
+extern uschar *submission_name;        /* User name set from ACL */
+extern BOOL    suppress_local_fixups;  /* Can be forced from ACL */
+extern BOOL    suppress_local_fixups_default; /* former is reset to this; override with -G */
+extern BOOL    synchronous_delivery;   /* TRUE if -odi is set */
+extern BOOL    syslog_duplication;     /* FALSE => no duplicate logging */
+extern int     syslog_facility;        /* As defined by Syslog.h */
+extern uschar *syslog_processname;     /* 'ident' param to openlog() */
+extern BOOL    syslog_timestamp;       /* TRUE if time on syslogs */
+extern uschar *system_filter;          /* Name of system filter file */
+
+extern uschar *system_filter_directory_transport;  /* Transports for the */
+extern uschar *system_filter_file_transport;       /* system filter */
+extern uschar *system_filter_pipe_transport;
+extern uschar *system_filter_reply_transport;
+
+extern gid_t   system_filter_gid;      /* Gid for running system filter */
+extern BOOL    system_filter_gid_set;  /* TRUE if gid set */
+extern uid_t   system_filter_uid;      /* Uid for running system filter */
+extern BOOL    system_filter_uid_set;  /* TRUE if uid set */
+extern BOOL    system_filtering;       /* TRUE when running system filter */
+
+extern BOOL    tcp_nodelay;            /* Controls TCP_NODELAY on daemon */
+#ifdef USE_TCP_WRAPPERS
+extern uschar *tcp_wrappers_daemon_name; /* tcpwrappers daemon lookup name */
+#endif
+extern int     test_harness_load_avg;  /* For use when testing */
+extern int     thismessage_size_limit; /* Limit for this message */
+extern int     timeout_frozen_after;   /* Max time to keep frozen messages */
+extern BOOL    timestamps_utc;         /* Use UTC for all times */
+
+#ifdef EXPERIMENTAL_TPDA
+extern int     tpda_defer_errno;        /* error number set when a remote delivery is deferred with a host error */
+extern uschar *tpda_defer_errstr;       /* error string set when a remote delivery is deferred with a host error */
+extern uschar *tpda_delivery_ip;        /* IP of host, which has accepted delivery */
+extern int     tpda_delivery_port;       /* port of host, which has accepted delivery */
+extern uschar *tpda_delivery_fqdn;      /* FQDN of host, which has accepted delivery */
+extern uschar *tpda_delivery_local_part;/* local part of address being delivered */
+extern uschar *tpda_delivery_domain;    /* domain part of address being delivered */
+extern uschar *tpda_delivery_confirmation; /* SMTP confirmation message */
+#endif
+
+extern uschar *transport_name;         /* Name of transport last started */
+extern int     transport_count;        /* Count of bytes transported */
+extern int     transport_newlines;     /* Accurate count of number of newline chars transported */
+extern uschar **transport_filter_argv; /* For on-the-fly filtering */
+extern int     transport_filter_timeout; /* Timeout for same */
+extern BOOL    transport_filter_timed_out; /* True if it did */
+
+extern transport_info transports_available[]; /* Vector of available transports */
+extern transport_instance *transports; /* Chain of instantiated transports */
+extern transport_instance transport_defaults; /* Default values */
+
+extern int     transport_write_timeout;/* Set to time out individual writes */
+
+extern tree_node *tree_dns_fails;      /* Tree of DNS lookup failures */
+extern tree_node *tree_duplicates;     /* Tree of duplicate addresses */
+extern tree_node *tree_nonrecipients;  /* Tree of nonrecipient addresses */
+extern tree_node *tree_unusable;       /* Tree of unusable addresses */
+
+extern BOOL    trusted_caller;         /* Caller is trusted */
+extern BOOL    trusted_config;         /* Configuration file is trusted */
+extern gid_t  *trusted_groups;         /* List of trusted groups */
+extern uid_t  *trusted_users;          /* List of trusted users */
+extern uschar *timezone_string;        /* Required timezone setting */
+
+extern uschar *unknown_login;          /* To use when login id unknown */
+extern uschar *unknown_username;       /* Ditto */
+extern uschar *untrusted_set_sender;   /* Let untrusted users set these senders */
+extern uschar *uucp_from_pattern;      /* For recognizing "From " lines */
+extern uschar *uucp_from_sender;       /* For building the sender */
+
+extern uschar *warn_message_file;      /* Template for warning messages */
+extern uschar *warnmsg_delay;          /* String form of delay time */
+extern uschar *warnmsg_recipients;     /* Recipients of warning message */
+extern BOOL    write_rejectlog;        /* Control of reject logging */
+
+extern uschar *version_copyright;      /* Copyright notice */
+extern uschar *version_date;           /* Date of compilation */
+extern uschar *version_cnumber;        /* Compile number */
+extern uschar *version_string;         /* Version string */
+
+extern int     warning_count;          /* Delay warnings sent for this msg */
+
+/* End of globals.h */
diff --git a/src/header.c b/src/header.c
new file mode 100644 (file)
index 0000000..8136c69
--- /dev/null
@@ -0,0 +1,462 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+
+#include "exim.h"
+
+
+/*************************************************
+*         Test a header for matching name        *
+*************************************************/
+
+/* This function tests the name of a header. It is made into a function because
+it isn't just a string comparison: spaces and tabs are permitted between the
+name and the colon. The h->text field should nowadays never be NULL, but check
+it just in case.
+
+Arguments:
+  h         points to the header
+  name      the name to test
+  len       the length of the name
+  notdel    if TRUE, force FALSE for deleted headers
+
+Returns:    TRUE or FALSE
+*/
+
+BOOL
+header_testname(header_line *h, const uschar *name, int len, BOOL notdel)
+{
+uschar *tt;
+if (h->type == '*' && notdel) return FALSE;
+if (h->text == NULL || strncmpic(h->text, name, len) != 0) return FALSE;
+tt = h->text + len;
+while (*tt == ' ' || *tt == '\t') tt++;
+return *tt == ':';
+}
+
+/* This is a copy of the function above, only that it is possible to pass
+   only the beginning of a header name. It simply does a front-anchored
+   substring match. Arguments and Return codes are the same as for
+   header_testname() above. */
+
+BOOL
+header_testname_incomplete(header_line *h, const uschar *name,
+    int len, BOOL notdel)
+{
+if (h->type == '*' && notdel) return FALSE;
+if (h->text == NULL || strncmpic(h->text, name, len) != 0) return FALSE;
+return TRUE;
+}
+
+
+/*************************************************
+*         Add new header backend function        *
+*************************************************/
+
+/* The header_last variable points to the last header during message reception
+and delivery; otherwise it is NULL. We add new headers only when header_last is
+not NULL. The function may get called sometimes when it is NULL (e.g. during
+address verification where rewriting options exist). When called from a filter,
+there may be multiple header lines in a single string.
+
+This is an internal static function that is the common back end to the external
+functions defined below. The general interface allows the header to be inserted
+before or after a given occurrence of a given header.
+
+(a) if "name" is NULL, the header is added at the end of all the existing
+    headers if "after" is true, or at the start if it is false. The "topnot"
+    flag is not used.
+
+(b) If "name" is not NULL, the first existing header with that name is sought.
+    If "after" is false, the new header is added before it. If "after" is true,
+    a check is made for adjacent headers with the same name, and the new header
+    is added after the last of them. If a header of the given name is not
+    found, the new header is added first if "topnot" is true, and at the bottom
+    otherwise.
+
+Arguments:
+  after     TRUE for "after", FALSE for "before"
+  name      name if adding at a specific header, else NULL
+  topnot    TRUE to add at top if no header found
+  type      Exim header type character (htype_something)
+  format    sprintf format
+  ap        va_list value for format arguments
+
+Returns:    nothing
+*/
+
+static void
+header_add_backend(BOOL after, uschar *name, BOOL topnot, int type,
+  const char *format, va_list ap)
+{
+header_line *h, *new;
+header_line **hptr;
+
+uschar *p, *q;
+uschar buffer[HEADER_ADD_BUFFER_SIZE];
+
+if (header_last == NULL) return;
+
+if (!string_vformat(buffer, sizeof(buffer), format, ap))
+  log_write(0, LOG_MAIN|LOG_PANIC_DIE, "string too long in header_add: "
+    "%.100s ...", buffer);
+
+/* Find where to insert this header */
+
+if (name == NULL)
+  {
+  if (after)
+    {
+    hptr = &(header_last->next);
+    h = NULL;
+    }
+  else
+    {
+    hptr = &header_list;
+
+    /* header_list->text can be NULL if we get here between when the new
+    received header is allocated and when it is acutally filled in. We want
+    that header to be first, so skip it for now. */
+
+    if (header_list->text == NULL)
+      hptr = &header_list->next;
+    h = *hptr;
+    }
+  }
+
+else
+  {
+  int len = Ustrlen(name);
+
+  /* Find the first non-deleted header witht the correct name. */
+
+  for (hptr = &header_list; (h = *hptr) != NULL; hptr = &(h->next))
+    {
+    if (header_testname(h, name, len, TRUE)) break;
+    }
+
+  /* Handle the case where no header is found. To insert at the bottom, nothing
+  needs to be done. */
+
+  if (h == NULL)
+    {
+    if (topnot)
+      {
+      hptr = &header_list;
+      h = header_list;
+      }
+    }
+
+  /* Handle the case where a header is found. Check for more if "after" is
+  true. In this case, we want to include deleted headers in the block. */
+
+  else if (after)
+    {
+    for (;;)
+      {
+      if (h->next == NULL || !header_testname(h, name, len, FALSE)) break;
+      hptr = &(h->next);
+      h = h->next;
+      }
+    }
+  }
+
+/* Loop for multiple header lines, taking care about continuations. At this
+point, we have hptr pointing to the link field that will point to the new
+header, and h containing the following header, or NULL. */
+
+for (p = q = buffer; *p != 0; )
+  {
+  for (;;)
+    {
+    q = Ustrchr(q, '\n');
+    if (q == NULL) q = p + Ustrlen(p);
+    if (*(++q) != ' ' && *q != '\t') break;
+    }
+
+  new = store_get(sizeof(header_line));
+  new->text = string_copyn(p, q - p);
+  new->slen = q - p;
+  new->type = type;
+  new->next = h;
+
+  *hptr = new;
+  hptr = &(new->next);
+
+  if (h == NULL) header_last = new;
+  p = q;
+  }
+}
+
+
+/*************************************************
+*      Add new header anywhere in the chain      *
+*************************************************/
+
+/* This is an external interface to header_add_backend().
+
+Arguments:
+  after     TRUE for "after", FALSE for "before"
+  name      name if adding at a specific header, else NULL
+  topnot    TRUE to add at top if no header found
+  type      Exim header type character (htype_something)
+  format    sprintf format
+  ...       format arguments
+
+Returns:    nothing
+*/
+
+void
+header_add_at_position(BOOL after, uschar *name, BOOL topnot, int type,
+  const char *format, ...)
+{
+va_list ap;
+va_start(ap, format);
+header_add_backend(after, name, topnot, type, format, ap);
+va_end(ap);
+}
+
+
+
+/*************************************************
+*            Add new header on end of chain      *
+*************************************************/
+
+/* This is now a convenience interface to header_add_backend().
+
+Arguments:
+  type      Exim header type character
+  format    sprintf format
+  ...       arguments for the format
+
+Returns:    nothing
+*/
+
+void
+header_add(int type, const char *format, ...)
+{
+va_list ap;
+va_start(ap, format);
+header_add_backend(TRUE, NULL, FALSE, type, format, ap);
+va_end(ap);
+}
+
+
+
+/*************************************************
+*        Remove (mark as old) a header           *
+*************************************************/
+
+/* This function is used by the filter code; it is also exported in the
+local_scan() API. If no header is found, the function does nothing.
+
+Arguments:
+  occ           the occurrence number for multiply-defined headers
+                  <= 0 means "all"; deleted headers are not counted
+  name          the header name
+
+Returns:        nothing
+*/
+
+void
+header_remove(int occ, const uschar *name)
+{
+header_line *h;
+int hcount = 0;
+int len = Ustrlen(name);
+for (h = header_list; h != NULL; h = h->next)
+  {
+  if (header_testname(h, name, len, TRUE) && (occ <= 0 || ++hcount == occ))
+    {
+    h->type = htype_old;
+    if (occ > 0) return;
+    }
+  }
+}
+
+
+
+/*************************************************
+*          Check the name of a header            *
+*************************************************/
+
+/* This function scans a table of header field names that Exim recognizes, and
+returns the identification of a match. If "resent" is true, the header is known
+to start with "resent-". In that case, the function matches only those fields
+that are allowed to appear with resent- in front of them.
+
+Arguments:
+  h             points to the header line
+  is_resent     TRUE if the name starts "Resent-"
+
+Returns:        One of the htype_ enum values, identifying the header
+*/
+
+int
+header_checkname(header_line *h, BOOL is_resent)
+{
+uschar *text = h->text;
+header_name *bot = header_names;
+header_name *top = header_names + header_names_size;
+
+if (is_resent) text += 7;
+
+while (bot < top)
+  {
+  header_name *mid = bot + (top - bot)/2;
+  int c = strncmpic(text, mid->name, mid->len);
+
+  if (c == 0)
+    {
+    uschar *s = text + mid->len;
+    while (isspace(*s)) s++;
+    if (*s == ':')
+      return (!is_resent || mid->allow_resent)? mid->htype : htype_other;
+    c = 1;
+    }
+
+  if (c > 0) bot = mid + 1; else top = mid;
+  }
+
+return htype_other;
+}
+
+
+/*************************************************
+*       Scan a header for certain strings        *
+*************************************************/
+
+/* This function is used for the "personal" test. It scans a particular header
+line for any one of a number of strings, matched caselessly either as plain
+strings, or as regular expressions. If the header line contains a list of
+addresses, each match is applied only to the operative part of each address in
+the header, and non-regular expressions must be exact matches.
+
+The patterns can be provided either as a chain of string_item structures, or
+inline in the argument list, or both. If there is more than one header of the
+same name, they are all searched.
+
+Arguments:
+  name           header name, including the trailing colon
+  has_addresses  TRUE if the header contains a list of addresses
+  cond           value to return if the header contains any of the strings
+  strings        points to a chain of string_item blocks
+  count          number of inline strings
+  ...            the inline strings
+
+Returns:         cond if the header exists and contains one of the strings;
+                   otherwise !cond
+*/
+
+
+/* First we have a local subroutine to handle a single pattern */
+
+static BOOL
+one_pattern_match(uschar *name, int slen, BOOL has_addresses, uschar *pattern)
+{
+BOOL yield = FALSE;
+header_line *h;
+const pcre *re = NULL;
+
+/* If the pattern is a regex, compile it. Bomb out if compiling fails; these
+patterns are all constructed internally and should be valid. */
+
+if (*pattern == '^') re = regex_must_compile(pattern, TRUE, FALSE);
+
+/* Scan for the required header(s) and scan each one */
+
+for (h = header_list; !yield && h != NULL; h = h->next)
+  {
+  if (h->type == htype_old || slen > h->slen ||
+      strncmpic(name, h->text, slen) != 0)
+    continue;
+
+  /* If the header is a list of addresses, extract each one in turn, and scan
+  it. A non-regex scan must be an exact match for the address. */
+
+  if (has_addresses)
+    {
+    uschar *s = h->text + slen;
+
+    while (!yield && *s != 0)
+      {
+      uschar *error, *next;
+      uschar *e = parse_find_address_end(s, FALSE);
+      int terminator = *e;
+      int start, end, domain;
+
+      /* Temporarily terminate the string at the address end while extracting
+      the operative address within. */
+
+      *e = 0;
+      next = parse_extract_address(s, &error, &start, &end, &domain, FALSE);
+      *e = terminator;
+
+      /* Move on, ready for the next address */
+
+      s = e;
+      if (*s == ',') s++;
+
+      /* If there is some kind of syntax error, just give up on this header
+      line. */
+
+      if (next == NULL) break;
+
+      /* Otherwise, test for the pattern; a non-regex must be an exact match */
+
+      yield = (re == NULL)?
+        (strcmpic(next, pattern) == 0)
+        :
+        (pcre_exec(re, NULL, CS next, Ustrlen(next), 0, PCRE_EOPT, NULL, 0)
+          >= 0);
+      }
+    }
+
+  /* For headers that are not lists of addresses, scan the entire header line,
+  and just require "contains" for non-regex patterns. */
+
+  else
+    {
+    yield = (re == NULL)?
+      (strstric(h->text, pattern, FALSE) != NULL)
+      :
+      (pcre_exec(re, NULL, CS h->text, h->slen, 0, PCRE_EOPT, NULL, 0) >= 0);
+    }
+  }
+
+return yield;
+}
+
+
+/* The externally visible interface */
+
+BOOL
+header_match(uschar *name, BOOL has_addresses, BOOL cond, string_item *strings,
+  int count, ...)
+{
+va_list ap;
+string_item *s;
+int i;
+int slen = Ustrlen(name);
+
+for (s = strings; s != NULL; s = s->next)
+  {
+  if (one_pattern_match(name, slen, has_addresses, s->text)) return cond;
+  }
+
+va_start(ap, count);
+for (i = 0; i < count; i++)
+  {
+  if (one_pattern_match(name, slen, has_addresses, va_arg(ap, uschar *)))
+    return cond;
+  }
+va_end(ap);
+
+return !cond;
+}
+
+/* End of header.c */
diff --git a/src/host.c b/src/host.c
new file mode 100644 (file)
index 0000000..00524f4
--- /dev/null
@@ -0,0 +1,3239 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2012 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Functions for finding hosts, either by gethostbyname(), gethostbyaddr(), or
+directly via the DNS. When IPv6 is supported, getipnodebyname() and
+getipnodebyaddr() may be used instead of gethostbyname() and gethostbyaddr(),
+if the newer functions are available. This module also contains various other
+functions concerned with hosts and addresses, and a random number function,
+used for randomizing hosts with equal MXs but available for use in other parts
+of Exim. */
+
+
+#include "exim.h"
+
+
+/* Static variable for preserving the list of interface addresses in case it is
+used more than once. */
+
+static ip_address_item *local_interface_data = NULL;
+
+
+#ifdef USE_INET_NTOA_FIX
+/*************************************************
+*         Replacement for broken inet_ntoa()     *
+*************************************************/
+
+/* On IRIX systems, gcc uses a different structure passing convention to the
+native libraries. This causes inet_ntoa() to always yield 0.0.0.0 or
+255.255.255.255. To get round this, we provide a private version of the
+function here. It is used only if USE_INET_NTOA_FIX is set, which should happen
+only when gcc is in use on an IRIX system. Code send to me by J.T. Breitner,
+with these comments:
+
+  code by Stuart Levy
+  as seen in comp.sys.sgi.admin
+
+August 2005: Apparently this is also needed for AIX systems; USE_INET_NTOA_FIX
+should now be set for them as well.
+
+Arguments:  sa  an in_addr structure
+Returns:        pointer to static text string
+*/
+
+char *
+inet_ntoa(struct in_addr sa)
+{
+static uschar addr[20];
+sprintf(addr, "%d.%d.%d.%d",
+        (US &sa.s_addr)[0],
+        (US &sa.s_addr)[1],
+        (US &sa.s_addr)[2],
+        (US &sa.s_addr)[3]);
+  return addr;
+}
+#endif
+
+
+
+/*************************************************
+*              Random number generator           *
+*************************************************/
+
+/* This is a simple pseudo-random number generator. It does not have to be
+very good for the uses to which it is put. When running the regression tests,
+start with a fixed seed.
+
+If you need better, see vaguely_random_number() which is potentially stronger,
+if a crypto library is available, but might end up just calling this instead.
+
+Arguments:
+  limit:    one more than the largest number required
+
+Returns:    a pseudo-random number in the range 0 to limit-1
+*/
+
+int
+random_number(int limit)
+{
+if (limit < 1)
+  return 0;
+if (random_seed == 0)
+  {
+  if (running_in_test_harness) random_seed = 42; else
+    {
+    int p = (int)getpid();
+    random_seed = (int)time(NULL) ^ ((p << 16) | p);
+    }
+  }
+random_seed = 1103515245 * random_seed + 12345;
+return (unsigned int)(random_seed >> 16) % limit;
+}
+
+
+
+/*************************************************
+*       Replace gethostbyname() when testing     *
+*************************************************/
+
+/* This function is called instead of gethostbyname(), gethostbyname2(), or
+getipnodebyname() when running in the test harness. It recognizes the name
+"manyhome.test.ex" and generates a humungous number of IP addresses. It also
+recognizes an unqualified "localhost" and forces it to the appropriate loopback
+address. IP addresses are treated as literals. For other names, it uses the DNS
+to find the host name. In the test harness, this means it will access only the
+fake DNS resolver.
+
+Arguments:
+  name          the host name or a textual IP address
+  af            AF_INET or AF_INET6
+  error_num     where to put an error code:
+                HOST_NOT_FOUND/TRY_AGAIN/NO_RECOVERY/NO_DATA
+
+Returns:        a hostent structure or NULL for an error
+*/
+
+static struct hostent *
+host_fake_gethostbyname(uschar *name, int af, int *error_num)
+{
+#if HAVE_IPV6
+int alen = (af == AF_INET)? sizeof(struct in_addr):sizeof(struct in6_addr);
+#else
+int alen = sizeof(struct in_addr);
+#endif
+
+int ipa;
+uschar *lname = name;
+uschar *adds;
+uschar **alist;
+struct hostent *yield;
+dns_answer dnsa;
+dns_scan dnss;
+dns_record *rr;
+
+DEBUG(D_host_lookup)
+  debug_printf("using host_fake_gethostbyname for %s (%s)\n", name,
+    (af == AF_INET)? "IPv4" : "IPv6");
+
+/* Handle the name that needs a vast number of IP addresses */
+
+if (Ustrcmp(name, "manyhome.test.ex") == 0 && af == AF_INET)
+  {
+  int i, j;
+  yield = store_get(sizeof(struct hostent));
+  alist = store_get(2049 * sizeof(char *));
+  adds  = store_get(2048 * alen);
+  yield->h_name = CS name;
+  yield->h_aliases = NULL;
+  yield->h_addrtype = af;
+  yield->h_length = alen;
+  yield->h_addr_list = CSS alist;
+  for (i = 104; i <= 111; i++)
+    {
+    for (j = 0; j <= 255; j++)
+      {
+      *alist++ = adds;
+      *adds++ = 10;
+      *adds++ = 250;
+      *adds++ = i;
+      *adds++ = j;
+      }
+    }
+  *alist = NULL;
+  return yield;
+  }
+
+/* Handle unqualified "localhost" */
+
+if (Ustrcmp(name, "localhost") == 0)
+  lname = (af == AF_INET)? US"127.0.0.1" : US"::1";
+
+/* Handle a literal IP address */
+
+ipa = string_is_ip_address(lname, NULL);
+if (ipa != 0)
+  {
+  if ((ipa == 4 && af == AF_INET) ||
+      (ipa == 6 && af == AF_INET6))
+    {
+    int i, n;
+    int x[4];
+    yield = store_get(sizeof(struct hostent));
+    alist = store_get(2 * sizeof(char *));
+    adds  = store_get(alen);
+    yield->h_name = CS name;
+    yield->h_aliases = NULL;
+    yield->h_addrtype = af;
+    yield->h_length = alen;
+    yield->h_addr_list = CSS alist;
+    *alist++ = adds;
+    n = host_aton(lname, x);
+    for (i = 0; i < n; i++)
+      {
+      int y = x[i];
+      *adds++ = (y >> 24) & 255;
+      *adds++ = (y >> 16) & 255;
+      *adds++ = (y >> 8) & 255;
+      *adds++ = y & 255;
+      }
+    *alist = NULL;
+    }
+
+  /* Wrong kind of literal address */
+
+  else
+    {
+    *error_num = HOST_NOT_FOUND;
+    return NULL;
+    }
+  }
+
+/* Handle a host name */
+
+else
+  {
+  int type = (af == AF_INET)? T_A:T_AAAA;
+  int rc = dns_lookup(&dnsa, lname, type, NULL);
+  int count = 0;
+
+  lookup_dnssec_authenticated = NULL;
+
+  switch(rc)
+    {
+    case DNS_SUCCEED: break;
+    case DNS_NOMATCH: *error_num = HOST_NOT_FOUND; return NULL;
+    case DNS_NODATA:  *error_num = NO_DATA; return NULL;
+    case DNS_AGAIN:   *error_num = TRY_AGAIN; return NULL;
+    default:
+    case DNS_FAIL:    *error_num = NO_RECOVERY; return NULL;
+    }
+
+  for (rr = dns_next_rr(&dnsa, &dnss, RESET_ANSWERS);
+       rr != NULL;
+       rr = dns_next_rr(&dnsa, &dnss, RESET_NEXT))
+    {
+    if (rr->type == type) count++;
+    }
+
+  yield = store_get(sizeof(struct hostent));
+  alist = store_get((count + 1) * sizeof(char **));
+  adds  = store_get(count *alen);
+
+  yield->h_name = CS name;
+  yield->h_aliases = NULL;
+  yield->h_addrtype = af;
+  yield->h_length = alen;
+  yield->h_addr_list = CSS alist;
+
+  for (rr = dns_next_rr(&dnsa, &dnss, RESET_ANSWERS);
+       rr != NULL;
+       rr = dns_next_rr(&dnsa, &dnss, RESET_NEXT))
+    {
+    int i, n;
+    int x[4];
+    dns_address *da;
+    if (rr->type != type) continue;
+    da = dns_address_from_rr(&dnsa, rr);
+    *alist++ = adds;
+    n = host_aton(da->address, x);
+    for (i = 0; i < n; i++)
+      {
+      int y = x[i];
+      *adds++ = (y >> 24) & 255;
+      *adds++ = (y >> 16) & 255;
+      *adds++ = (y >> 8) & 255;
+      *adds++ = y & 255;
+      }
+    }
+  *alist = NULL;
+  }
+
+return yield;
+}
+
+
+
+/*************************************************
+*       Build chain of host items from list      *
+*************************************************/
+
+/* This function builds a chain of host items from a textual list of host
+names. It does not do any lookups. If randomize is true, the chain is build in
+a randomized order. There may be multiple groups of independently randomized
+hosts; they are delimited by a host name consisting of just "+".
+
+Arguments:
+  anchor      anchor for the chain
+  list        text list
+  randomize   TRUE for randomizing
+
+Returns:      nothing
+*/
+
+void
+host_build_hostlist(host_item **anchor, uschar *list, BOOL randomize)
+{
+int sep = 0;
+int fake_mx = MX_NONE;          /* This value is actually -1 */
+uschar *name;
+uschar buffer[1024];
+
+if (list == NULL) return;
+if (randomize) fake_mx--;       /* Start at -2 for randomizing */
+
+*anchor = NULL;
+
+while ((name = string_nextinlist(&list, &sep, buffer, sizeof(buffer))) != NULL)
+  {
+  host_item *h;
+
+  if (name[0] == '+' && name[1] == 0)   /* "+" delimits a randomized group */
+    {                                   /* ignore if not randomizing */
+    if (randomize) fake_mx--;
+    continue;
+    }
+
+  h = store_get(sizeof(host_item));
+  h->name = string_copy(name);
+  h->address = NULL;
+  h->port = PORT_NONE;
+  h->mx = fake_mx;
+  h->sort_key = randomize? (-fake_mx)*1000 + random_number(1000) : 0;
+  h->status = hstatus_unknown;
+  h->why = hwhy_unknown;
+  h->last_try = 0;
+
+  if (*anchor == NULL)
+    {
+    h->next = NULL;
+    *anchor = h;
+    }
+  else
+    {
+    host_item *hh = *anchor;
+    if (h->sort_key < hh->sort_key)
+      {
+      h->next = hh;
+      *anchor = h;
+      }
+    else
+      {
+      while (hh->next != NULL && h->sort_key >= (hh->next)->sort_key)
+        hh = hh->next;
+      h->next = hh->next;
+      hh->next = h;
+      }
+    }
+  }
+}
+
+
+
+
+
+/*************************************************
+*        Extract port from address string        *
+*************************************************/
+
+/* In the spool file, and in the -oMa and -oMi options, a host plus port is
+given as an IP address followed by a dot and a port number. This function
+decodes this.
+
+An alternative format for the -oMa and -oMi options is [ip address]:port which
+is what Exim 4 uses for output, because it seems to becoming commonly used,
+whereas the dot form confuses some programs/people. So we recognize that form
+too.
+
+Argument:
+  address    points to the string; if there is a port, the '.' in the string
+             is overwritten with zero to terminate the address; if the string
+             is in the [xxx]:ppp format, the address is shifted left and the
+             brackets are removed
+
+Returns:     0 if there is no port, else the port number. If there's a syntax
+             error, leave the incoming address alone, and return 0.
+*/
+
+int
+host_address_extract_port(uschar *address)
+{
+int port = 0;
+uschar *endptr;
+
+/* Handle the "bracketed with colon on the end" format */
+
+if (*address == '[')
+  {
+  uschar *rb = address + 1;
+  while (*rb != 0 && *rb != ']') rb++;
+  if (*rb++ == 0) return 0;        /* Missing ]; leave invalid address */
+  if (*rb == ':')
+    {
+    port = Ustrtol(rb + 1, &endptr, 10);
+    if (*endptr != 0) return 0;    /* Invalid port; leave invalid address */
+    }
+  else if (*rb != 0) return 0;     /* Bad syntax; leave invalid address */
+  memmove(address, address + 1, rb - address - 2);
+  rb[-2] = 0;
+  }
+
+/* Handle the "dot on the end" format */
+
+else
+  {
+  int skip = -3;                   /* Skip 3 dots in IPv4 addresses */
+  address--;
+  while (*(++address) != 0)
+    {
+    int ch = *address;
+    if (ch == ':') skip = 0;       /* Skip 0 dots in IPv6 addresses */
+      else if (ch == '.' && skip++ >= 0) break;
+    }
+  if (*address == 0) return 0;
+  port = Ustrtol(address + 1, &endptr, 10);
+  if (*endptr != 0) return 0;      /* Invalid port; leave invalid address */
+  *address = 0;
+  }
+
+return port;
+}
+
+
+/*************************************************
+*         Get port from a host item's name       *
+*************************************************/
+
+/* This function is called when finding the IP address for a host that is in a
+list of hosts explicitly configured, such as in the manualroute router, or in a
+fallback hosts list. We see if there is a port specification at the end of the
+host name, and if so, remove it. A minimum length of 3 is required for the
+original name; nothing shorter is recognized as having a port.
+
+We test for a name ending with a sequence of digits; if preceded by colon we
+have a port if the character before the colon is ] and the name starts with [
+or if there are no other colons in the name (i.e. it's not an IPv6 address).
+
+Arguments:  pointer to the host item
+Returns:    a port number or PORT_NONE
+*/
+
+int
+host_item_get_port(host_item *h)
+{
+uschar *p;
+int port, x;
+int len = Ustrlen(h->name);
+
+if (len < 3 || (p = h->name + len - 1, !isdigit(*p))) return PORT_NONE;
+
+/* Extract potential port number */
+
+port = *p-- - '0';
+x = 10;
+
+while (p > h->name + 1 && isdigit(*p))
+  {
+  port += (*p-- - '0') * x;
+  x *= 10;
+  }
+
+/* The smallest value of p at this point is h->name + 1. */
+
+if (*p != ':') return PORT_NONE;
+
+if (p[-1] == ']' && h->name[0] == '[')
+  h->name = string_copyn(h->name + 1, p - h->name - 2);
+else if (Ustrchr(h->name, ':') == p)
+  h->name = string_copyn(h->name, p - h->name);
+else return PORT_NONE;
+
+DEBUG(D_route|D_host_lookup) debug_printf("host=%s port=%d\n", h->name, port);
+return port;
+}
+
+
+
+#ifndef STAND_ALONE    /* Omit when standalone testing */
+
+/*************************************************
+*     Build sender_fullhost and sender_rcvhost   *
+*************************************************/
+
+/* This function is called when sender_host_name and/or sender_helo_name
+have been set. Or might have been set - for a local message read off the spool
+they won't be. In that case, do nothing. Otherwise, set up the fullhost string
+as follows:
+
+(a) No sender_host_name or sender_helo_name: "[ip address]"
+(b) Just sender_host_name: "host_name [ip address]"
+(c) Just sender_helo_name: "(helo_name) [ip address]" unless helo is IP
+            in which case: "[ip address}"
+(d) The two are identical: "host_name [ip address]" includes helo = IP
+(e) The two are different: "host_name (helo_name) [ip address]"
+
+If log_incoming_port is set, the sending host's port number is added to the IP
+address.
+
+This function also builds sender_rcvhost for use in Received: lines, whose
+syntax is a bit different. This value also includes the RFC 1413 identity.
+There wouldn't be two different variables if I had got all this right in the
+first place.
+
+Because this data may survive over more than one incoming SMTP message, it has
+to be in permanent store.
+
+Arguments:  none
+Returns:    nothing
+*/
+
+void
+host_build_sender_fullhost(void)
+{
+BOOL show_helo = TRUE;
+uschar *address;
+int len;
+int old_pool = store_pool;
+
+if (sender_host_address == NULL) return;
+
+store_pool = POOL_PERM;
+
+/* Set up address, with or without the port. After discussion, it seems that
+the only format that doesn't cause trouble is [aaaa]:pppp. However, we can't
+use this directly as the first item for Received: because it ain't an RFC 2822
+domain. Sigh. */
+
+address = string_sprintf("[%s]:%d", sender_host_address, sender_host_port);
+if ((log_extra_selector & LX_incoming_port) == 0 || sender_host_port <= 0)
+  *(Ustrrchr(address, ':')) = 0;
+
+/* If there's no EHLO/HELO data, we can't show it. */
+
+if (sender_helo_name == NULL) show_helo = FALSE;
+
+/* If HELO/EHLO was followed by an IP literal, it's messy because of two
+features of IPv6. Firstly, there's the "IPv6:" prefix (Exim is liberal and
+doesn't require this, for historical reasons). Secondly, IPv6 addresses may not
+be given in canonical form, so we have to canonicize them before comparing. As
+it happens, the code works for both IPv4 and IPv6. */
+
+else if (sender_helo_name[0] == '[' &&
+         sender_helo_name[(len=Ustrlen(sender_helo_name))-1] == ']')
+  {
+  int offset = 1;
+  uschar *helo_ip;
+
+  if (strncmpic(sender_helo_name + 1, US"IPv6:", 5) == 0) offset += 5;
+  if (strncmpic(sender_helo_name + 1, US"IPv4:", 5) == 0) offset += 5;
+
+  helo_ip = string_copyn(sender_helo_name + offset, len - offset - 1);
+
+  if (string_is_ip_address(helo_ip, NULL) != 0)
+    {
+    int x[4], y[4];
+    int sizex, sizey;
+    uschar ipx[48], ipy[48];    /* large enough for full IPv6 */
+
+    sizex = host_aton(helo_ip, x);
+    sizey = host_aton(sender_host_address, y);
+
+    (void)host_nmtoa(sizex, x, -1, ipx, ':');
+    (void)host_nmtoa(sizey, y, -1, ipy, ':');
+
+    if (strcmpic(ipx, ipy) == 0) show_helo = FALSE;
+    }
+  }
+
+/* Host name is not verified */
+
+if (sender_host_name == NULL)
+  {
+  uschar *portptr = Ustrstr(address, "]:");
+  int size = 0;
+  int ptr = 0;
+  int adlen;    /* Sun compiler doesn't like ++ in initializers */
+
+  adlen = (portptr == NULL)? Ustrlen(address) : (++portptr - address);
+  sender_fullhost = (sender_helo_name == NULL)? address :
+    string_sprintf("(%s) %s", sender_helo_name, address);
+
+  sender_rcvhost = string_cat(NULL, &size, &ptr, address, adlen);
+
+  if (sender_ident != NULL || show_helo || portptr != NULL)
+    {
+    int firstptr;
+    sender_rcvhost = string_cat(sender_rcvhost, &size, &ptr, US" (", 2);
+    firstptr = ptr;
+
+    if (portptr != NULL)
+      sender_rcvhost = string_append(sender_rcvhost, &size, &ptr, 2, US"port=",
+        portptr + 1);
+
+    if (show_helo)
+      sender_rcvhost = string_append(sender_rcvhost, &size, &ptr, 2,
+        (firstptr == ptr)? US"helo=" : US" helo=", sender_helo_name);
+
+    if (sender_ident != NULL)
+      sender_rcvhost = string_append(sender_rcvhost, &size, &ptr, 2,
+        (firstptr == ptr)? US"ident=" : US" ident=", sender_ident);
+
+    sender_rcvhost = string_cat(sender_rcvhost, &size, &ptr, US")", 1);
+    }
+
+  sender_rcvhost[ptr] = 0;   /* string_cat() always leaves room */
+
+  /* Release store, because string_cat allocated a minimum of 100 bytes that
+  are rarely completely used. */
+
+  store_reset(sender_rcvhost + ptr + 1);
+  }
+
+/* Host name is known and verified. Unless we've already found that the HELO
+data matches the IP address, compare it with the name. */
+
+else
+  {
+  if (show_helo && strcmpic(sender_host_name, sender_helo_name) == 0)
+    show_helo = FALSE;
+
+  if (show_helo)
+    {
+    sender_fullhost = string_sprintf("%s (%s) %s", sender_host_name,
+      sender_helo_name, address);
+    sender_rcvhost = (sender_ident == NULL)?
+      string_sprintf("%s (%s helo=%s)", sender_host_name,
+        address, sender_helo_name) :
+      string_sprintf("%s\n\t(%s helo=%s ident=%s)", sender_host_name,
+        address, sender_helo_name, sender_ident);
+    }
+  else
+    {
+    sender_fullhost = string_sprintf("%s %s", sender_host_name, address);
+    sender_rcvhost = (sender_ident == NULL)?
+      string_sprintf("%s (%s)", sender_host_name, address) :
+      string_sprintf("%s (%s ident=%s)", sender_host_name, address,
+        sender_ident);
+    }
+  }
+
+store_pool = old_pool;
+
+DEBUG(D_host_lookup) debug_printf("sender_fullhost = %s\n", sender_fullhost);
+DEBUG(D_host_lookup) debug_printf("sender_rcvhost = %s\n", sender_rcvhost);
+}
+
+
+
+/*************************************************
+*          Build host+ident message              *
+*************************************************/
+
+/* Used when logging rejections and various ACL and SMTP incidents. The text
+return depends on whether sender_fullhost and sender_ident are set or not:
+
+  no ident, no host   => U=unknown
+  no ident, host set  => H=sender_fullhost
+  ident set, no host  => U=ident
+  ident set, host set => H=sender_fullhost U=ident
+
+Arguments:
+  useflag   TRUE if first item to be flagged (H= or U=); if there are two
+              items, the second is always flagged
+
+Returns:    pointer to a string in big_buffer
+*/
+
+uschar *
+host_and_ident(BOOL useflag)
+{
+if (sender_fullhost == NULL)
+  {
+  (void)string_format(big_buffer, big_buffer_size, "%s%s", useflag? "U=" : "",
+     (sender_ident == NULL)? US"unknown" : sender_ident);
+  }
+else
+  {
+  uschar *flag = useflag? US"H=" : US"";
+  uschar *iface = US"";
+  if ((log_extra_selector & LX_incoming_interface) != 0 &&
+       interface_address != NULL)
+    iface = string_sprintf(" I=[%s]:%d", interface_address, interface_port);
+  if (sender_ident == NULL)
+    (void)string_format(big_buffer, big_buffer_size, "%s%s%s",
+      flag, sender_fullhost, iface);
+  else
+    (void)string_format(big_buffer, big_buffer_size, "%s%s%s U=%s",
+      flag, sender_fullhost, iface, sender_ident);
+  }
+return big_buffer;
+}
+
+#endif   /* STAND_ALONE */
+
+
+
+
+/*************************************************
+*         Build list of local interfaces         *
+*************************************************/
+
+/* This function interprets the contents of the local_interfaces or
+extra_local_interfaces options, and creates an ip_address_item block for each
+item on the list. There is no special interpretation of any IP addresses; in
+particular, 0.0.0.0 and ::0 are returned without modification. If any address
+includes a port, it is set in the block. Otherwise the port value is set to
+zero.
+
+Arguments:
+  list        the list
+  name        the name of the option being expanded
+
+Returns:      a chain of ip_address_items, each containing to a textual
+              version of an IP address, and a port number (host order) or
+              zero if no port was given with the address
+*/
+
+ip_address_item *
+host_build_ifacelist(uschar *list, uschar *name)
+{
+int sep = 0;
+uschar *s;
+uschar buffer[64];
+ip_address_item *yield = NULL;
+ip_address_item *last = NULL;
+ip_address_item *next;
+
+while ((s = string_nextinlist(&list, &sep, buffer, sizeof(buffer))) != NULL)
+  {
+  int ipv;
+  int port = host_address_extract_port(s);            /* Leaves just the IP address */
+  if ((ipv = string_is_ip_address(s, NULL)) == 0)
+    log_write(0, LOG_MAIN|LOG_PANIC_DIE, "Malformed IP address \"%s\" in %s",
+      s, name);
+
+  /* Skip IPv6 addresses if IPv6 is disabled. */
+
+  if (disable_ipv6 && ipv == 6) continue;
+
+  /* This use of strcpy() is OK because we have checked that s is a valid IP
+  address above. The field in the ip_address_item is large enough to hold an
+  IPv6 address. */
+
+  next = store_get(sizeof(ip_address_item));
+  next->next = NULL;
+  Ustrcpy(next->address, s);
+  next->port = port;
+  next->v6_include_v4 = FALSE;
+
+  if (yield == NULL) yield = last = next; else
+    {
+    last->next = next;
+    last = next;
+    }
+  }
+
+return yield;
+}
+
+
+
+
+
+/*************************************************
+*         Find addresses on local interfaces     *
+*************************************************/
+
+/* This function finds the addresses of local IP interfaces. These are used
+when testing for routing to the local host. As the function may be called more
+than once, the list is preserved in permanent store, pointed to by a static
+variable, to save doing the work more than once per process.
+
+The generic list of interfaces is obtained by calling host_build_ifacelist()
+for local_interfaces and extra_local_interfaces. This list scanned to remove
+duplicates (which may exist with different ports - not relevant here). If
+either of the wildcard IP addresses (0.0.0.0 and ::0) are encountered, they are
+replaced by the appropriate (IPv4 or IPv6) list of actual local interfaces,
+obtained from os_find_running_interfaces().
+
+Arguments:    none
+Returns:      a chain of ip_address_items, each containing to a textual
+              version of an IP address; the port numbers are not relevant
+*/
+
+
+/* First, a local subfunction to add an interface to a list in permanent store,
+but only if there isn't a previous copy of that address on the list. */
+
+static ip_address_item *
+add_unique_interface(ip_address_item *list, ip_address_item *ipa)
+{
+ip_address_item *ipa2;
+for (ipa2 = list; ipa2 != NULL; ipa2 = ipa2->next)
+  if (Ustrcmp(ipa2->address, ipa->address) == 0) return list;
+ipa2 = store_get_perm(sizeof(ip_address_item));
+*ipa2 = *ipa;
+ipa2->next = list;
+return ipa2;
+}
+
+
+/* This is the globally visible function */
+
+ip_address_item *
+host_find_interfaces(void)
+{
+ip_address_item *running_interfaces = NULL;
+
+if (local_interface_data == NULL)
+  {
+  void *reset_item = store_get(0);
+  ip_address_item *dlist = host_build_ifacelist(local_interfaces,
+    US"local_interfaces");
+  ip_address_item *xlist = host_build_ifacelist(extra_local_interfaces,
+    US"extra_local_interfaces");
+  ip_address_item *ipa;
+
+  if (dlist == NULL) dlist = xlist; else
+    {
+    for (ipa = dlist; ipa->next != NULL; ipa = ipa->next);
+    ipa->next = xlist;
+    }
+
+  for (ipa = dlist; ipa != NULL; ipa = ipa->next)
+    {
+    if (Ustrcmp(ipa->address, "0.0.0.0") == 0 ||
+        Ustrcmp(ipa->address, "::0") == 0)
+      {
+      ip_address_item *ipa2;
+      BOOL ipv6 = ipa->address[0] == ':';
+      if (running_interfaces == NULL)
+        running_interfaces = os_find_running_interfaces();
+      for (ipa2 = running_interfaces; ipa2 != NULL; ipa2 = ipa2->next)
+        {
+        if ((Ustrchr(ipa2->address, ':') != NULL) == ipv6)
+          local_interface_data = add_unique_interface(local_interface_data,
+          ipa2);
+        }
+      }
+    else
+      {
+      local_interface_data = add_unique_interface(local_interface_data, ipa);
+      DEBUG(D_interface)
+        {
+        debug_printf("Configured local interface: address=%s", ipa->address);
+        if (ipa->port != 0) debug_printf(" port=%d", ipa->port);
+        debug_printf("\n");
+        }
+      }
+    }
+  store_reset(reset_item);
+  }
+
+return local_interface_data;
+}
+
+
+
+
+
+/*************************************************
+*        Convert network IP address to text      *
+*************************************************/
+
+/* Given an IPv4 or IPv6 address in binary, convert it to a text
+string and return the result in a piece of new store. The address can
+either be given directly, or passed over in a sockaddr structure. Note
+that this isn't the converse of host_aton() because of byte ordering
+differences. See host_nmtoa() below.
+
+Arguments:
+  type       if < 0 then arg points to a sockaddr, else
+             either AF_INET or AF_INET6
+  arg        points to a sockaddr if type is < 0, or
+             points to an IPv4 address (32 bits), or
+             points to an IPv6 address (128 bits),
+             in both cases, in network byte order
+  buffer     if NULL, the result is returned in gotten store;
+             else points to a buffer to hold the answer
+  portptr    points to where to put the port number, if non NULL; only
+             used when type < 0
+
+Returns:     pointer to character string
+*/
+
+uschar *
+host_ntoa(int type, const void *arg, uschar *buffer, int *portptr)
+{
+uschar *yield;
+
+/* The new world. It is annoying that we have to fish out the address from
+different places in the block, depending on what kind of address it is. It
+is also a pain that inet_ntop() returns a const uschar *, whereas the IPv4
+function inet_ntoa() returns just uschar *, and some picky compilers insist
+on warning if one assigns a const uschar * to a uschar *. Hence the casts. */
+
+#if HAVE_IPV6
+uschar addr_buffer[46];
+if (type < 0)
+  {
+  int family = ((struct sockaddr *)arg)->sa_family;
+  if (family == AF_INET6)
+    {
+    struct sockaddr_in6 *sk = (struct sockaddr_in6 *)arg;
+    yield = (uschar *)inet_ntop(family, &(sk->sin6_addr), CS addr_buffer,
+      sizeof(addr_buffer));
+    if (portptr != NULL) *portptr = ntohs(sk->sin6_port);
+    }
+  else
+    {
+    struct sockaddr_in *sk = (struct sockaddr_in *)arg;
+    yield = (uschar *)inet_ntop(family, &(sk->sin_addr), CS addr_buffer,
+      sizeof(addr_buffer));
+    if (portptr != NULL) *portptr = ntohs(sk->sin_port);
+    }
+  }
+else
+  {
+  yield = (uschar *)inet_ntop(type, arg, CS addr_buffer, sizeof(addr_buffer));
+  }
+
+/* If the result is a mapped IPv4 address, show it in V4 format. */
+
+if (Ustrncmp(yield, "::ffff:", 7) == 0) yield += 7;
+
+#else  /* HAVE_IPV6 */
+
+/* The old world */
+
+if (type < 0)
+  {
+  yield = US inet_ntoa(((struct sockaddr_in *)arg)->sin_addr);
+  if (portptr != NULL) *portptr = ntohs(((struct sockaddr_in *)arg)->sin_port);
+  }
+else
+  yield = US inet_ntoa(*((struct in_addr *)arg));
+#endif
+
+/* If there is no buffer, put the string into some new store. */
+
+if (buffer == NULL) return string_copy(yield);
+
+/* Callers of this function with a non-NULL buffer must ensure that it is
+large enough to hold an IPv6 address, namely, at least 46 bytes. That's what
+makes this use of strcpy() OK. */
+
+Ustrcpy(buffer, yield);
+return buffer;
+}
+
+
+
+
+/*************************************************
+*         Convert address text to binary         *
+*************************************************/
+
+/* Given the textual form of an IP address, convert it to binary in an
+array of ints. IPv4 addresses occupy one int; IPv6 addresses occupy 4 ints.
+The result has the first byte in the most significant byte of the first int. In
+other words, the result is not in network byte order, but in host byte order.
+As a result, this is not the converse of host_ntoa(), which expects network
+byte order. See host_nmtoa() below.
+
+Arguments:
+  address    points to the textual address, checked for syntax
+  bin        points to an array of 4 ints
+
+Returns:     the number of ints used
+*/
+
+int
+host_aton(uschar *address, int *bin)
+{
+int x[4];
+int v4offset = 0;
+
+/* Handle IPv6 address, which may end with an IPv4 address. It may also end
+with a "scope", introduced by a percent sign. This code is NOT enclosed in #if
+HAVE_IPV6 in order that IPv6 addresses are recognized even if IPv6 is not
+supported. */
+
+if (Ustrchr(address, ':') != NULL)
+  {
+  uschar *p = address;
+  uschar *component[8];
+  BOOL ipv4_ends = FALSE;
+  int ci = 0;
+  int nulloffset = 0;
+  int v6count = 8;
+  int i;
+
+  /* If the address starts with a colon, it will start with two colons.
+  Just lose the first one, which will leave a null first component. */
+
+  if (*p == ':') p++;
+
+  /* Split the address into components separated by colons. The input address
+  is supposed to be checked for syntax. There was a case where this was
+  overlooked; to guard against that happening again, check here and crash if
+  there are too many components. */
+
+  while (*p != 0 && *p != '%')
+    {
+    int len = Ustrcspn(p, ":%");
+    if (len == 0) nulloffset = ci;
+    if (ci > 7) log_write(0, LOG_MAIN|LOG_PANIC_DIE,
+      "Internal error: invalid IPv6 address \"%s\" passed to host_aton()",
+      address);
+    component[ci++] = p;
+    p += len;
+    if (*p == ':') p++;
+    }
+
+  /* If the final component contains a dot, it is a trailing v4 address.
+  As the syntax is known to be checked, just set up for a trailing
+  v4 address and restrict the v6 part to 6 components. */
+
+  if (Ustrchr(component[ci-1], '.') != NULL)
+    {
+    address = component[--ci];
+    ipv4_ends = TRUE;
+    v4offset = 3;
+    v6count = 6;
+    }
+
+  /* If there are fewer than 6 or 8 components, we have to insert some
+  more empty ones in the middle. */
+
+  if (ci < v6count)
+    {
+    int insert_count = v6count - ci;
+    for (i = v6count-1; i > nulloffset + insert_count; i--)
+      component[i] = component[i - insert_count];
+    while (i > nulloffset) component[i--] = US"";
+    }
+
+  /* Now turn the components into binary in pairs and bung them
+  into the vector of ints. */
+
+  for (i = 0; i < v6count; i += 2)
+    bin[i/2] = (Ustrtol(component[i], NULL, 16) << 16) +
+      Ustrtol(component[i+1], NULL, 16);
+
+  /* If there was no terminating v4 component, we are done. */
+
+  if (!ipv4_ends) return 4;
+  }
+
+/* Handle IPv4 address */
+
+(void)sscanf(CS address, "%d.%d.%d.%d", x, x+1, x+2, x+3);
+bin[v4offset] = (x[0] << 24) + (x[1] << 16) + (x[2] << 8) + x[3];
+return v4offset+1;
+}
+
+
+/*************************************************
+*           Apply mask to an IP address          *
+*************************************************/
+
+/* Mask an address held in 1 or 4 ints, with the ms bit in the ms bit of the
+first int, etc.
+
+Arguments:
+  count        the number of ints
+  binary       points to the ints to be masked
+  mask         the count of ms bits to leave, or -1 if no masking
+
+Returns:       nothing
+*/
+
+void
+host_mask(int count, int *binary, int mask)
+{
+int i;
+if (mask < 0) mask = 99999;
+for (i = 0; i < count; i++)
+  {
+  int wordmask;
+  if (mask == 0) wordmask = 0;
+  else if (mask < 32)
+    {
+    wordmask = (-1) << (32 - mask);
+    mask = 0;
+    }
+  else
+    {
+    wordmask = -1;
+    mask -= 32;
+    }
+  binary[i] &= wordmask;
+  }
+}
+
+
+
+
+/*************************************************
+*     Convert masked IP address in ints to text  *
+*************************************************/
+
+/* We can't use host_ntoa() because it assumes the binary values are in network
+byte order, and these are the result of host_aton(), which puts them in ints in
+host byte order. Also, we really want IPv6 addresses to be in a canonical
+format, so we output them with no abbreviation. In a number of cases we can't
+use the normal colon separator in them because it terminates keys in lsearch
+files, so we want to use dot instead. There's an argument that specifies what
+to use for IPv6 addresses.
+
+Arguments:
+  count       1 or 4 (number of ints)
+  binary      points to the ints
+  mask        mask value; if < 0 don't add to result
+  buffer      big enough to hold the result
+  sep         component separator character for IPv6 addresses
+
+Returns:      the number of characters placed in buffer, not counting
+              the final nul.
+*/
+
+int
+host_nmtoa(int count, int *binary, int mask, uschar *buffer, int sep)
+{
+int i, j;
+uschar *tt = buffer;
+
+if (count == 1)
+  {
+  j = binary[0];
+  for (i = 24; i >= 0; i -= 8)
+    {
+    sprintf(CS tt, "%d.", (j >> i) & 255);
+    while (*tt) tt++;
+    }
+  }
+else
+  {
+  for (i = 0; i < 4; i++)
+    {
+    j = binary[i];
+    sprintf(CS tt, "%04x%c%04x%c", (j >> 16) & 0xffff, sep, j & 0xffff, sep);
+    while (*tt) tt++;
+    }
+  }
+
+tt--;   /* lose final separator */
+
+if (mask < 0)
+  *tt = 0;
+else
+  {
+  sprintf(CS tt, "/%d", mask);
+  while (*tt) tt++;
+  }
+
+return tt - buffer;
+}
+
+
+
+/*************************************************
+*        Check port for tls_on_connect           *
+*************************************************/
+
+/* This function checks whether a given incoming port is configured for tls-
+on-connect. It is called from the daemon and from inetd handling. If the global
+option tls_on_connect is already set, all ports operate this way. Otherwise, we
+check the tls_on_connect_ports option for a list of ports.
+
+Argument:  a port number
+Returns:   TRUE or FALSE
+*/
+
+BOOL
+host_is_tls_on_connect_port(int port)
+{
+int sep = 0;
+uschar buffer[32];
+uschar *list = tls_in.on_connect_ports;
+uschar *s;
+uschar *end;
+
+if (tls_in.on_connect) return TRUE;
+
+while ((s = string_nextinlist(&list, &sep, buffer, sizeof(buffer))))
+  if (Ustrtol(s, &end, 10) == port)
+    return TRUE;
+
+return FALSE;
+}
+
+
+
+/*************************************************
+*        Check whether host is in a network      *
+*************************************************/
+
+/* This function checks whether a given IP address matches a pattern that
+represents either a single host, or a network (using CIDR notation). The caller
+of this function must check the syntax of the arguments before calling it.
+
+Arguments:
+  host        string representation of the ip-address to check
+  net         string representation of the network, with optional CIDR mask
+  maskoffset  offset to the / that introduces the mask in the key
+              zero if there is no mask
+
+Returns:
+  TRUE   the host is inside the network
+  FALSE  the host is NOT inside the network
+*/
+
+BOOL
+host_is_in_net(uschar *host, uschar *net, int maskoffset)
+{
+int i;
+int address[4];
+int incoming[4];
+int mlen;
+int size = host_aton(net, address);
+int insize;
+
+/* No mask => all bits to be checked */
+
+if (maskoffset == 0) mlen = 99999;    /* Big number */
+  else mlen = Uatoi(net + maskoffset + 1);
+
+/* Convert the incoming address to binary. */
+
+insize = host_aton(host, incoming);
+
+/* Convert IPv4 addresses given in IPv6 compatible mode, which represent
+   connections from IPv4 hosts to IPv6 hosts, that is, addresses of the form
+   ::ffff:<v4address>, to IPv4 format. */
+
+if (insize == 4 && incoming[0] == 0 && incoming[1] == 0 &&
+    incoming[2] == 0xffff)
+  {
+  insize = 1;
+  incoming[0] = incoming[3];
+  }
+
+/* No match if the sizes don't agree. */
+
+if (insize != size) return FALSE;
+
+/* Else do the masked comparison. */
+
+for (i = 0; i < size; i++)
+  {
+  int mask;
+  if (mlen == 0) mask = 0;
+  else if (mlen < 32)
+    {
+    mask = (-1) << (32 - mlen);
+    mlen = 0;
+    }
+  else
+    {
+    mask = -1;
+    mlen -= 32;
+    }
+  if ((incoming[i] & mask) != (address[i] & mask)) return FALSE;
+  }
+
+return TRUE;
+}
+
+
+
+/*************************************************
+*       Scan host list for local hosts           *
+*************************************************/
+
+/* Scan through a chain of addresses and check whether any of them is the
+address of an interface on the local machine. If so, remove that address and
+any previous ones with the same MX value, and all subsequent ones (which will
+have greater or equal MX values) from the chain. Note: marking them as unusable
+is NOT the right thing to do because it causes the hosts not to be used for
+other domains, for which they may well be correct.
+
+The hosts may be part of a longer chain; we only process those between the
+initial pointer and the "last" pointer.
+
+There is also a list of "pseudo-local" host names which are checked against the
+host names. Any match causes that host item to be treated the same as one which
+matches a local IP address.
+
+If the very first host is a local host, then all MX records had a precedence
+greater than or equal to that of the local host. Either there's a problem in
+the DNS, or an apparently remote name turned out to be an abbreviation for the
+local host. Give a specific return code, and let the caller decide what to do.
+Otherwise, give a success code if at least one host address has been found.
+
+Arguments:
+  host        pointer to the first host in the chain
+  lastptr     pointer to pointer to the last host in the chain (may be updated)
+  removed     if not NULL, set TRUE if some local addresses were removed
+                from the list
+
+Returns:
+  HOST_FOUND       if there is at least one host with an IP address on the chain
+                     and an MX value less than any MX value associated with the
+                     local host
+  HOST_FOUND_LOCAL if a local host is among the lowest-numbered MX hosts; when
+                     the host addresses were obtained from A records or
+                     gethostbyname(), the MX values are set to -1.
+  HOST_FIND_FAILED if no valid hosts with set IP addresses were found
+*/
+
+int
+host_scan_for_local_hosts(host_item *host, host_item **lastptr, BOOL *removed)
+{
+int yield = HOST_FIND_FAILED;
+host_item *last = *lastptr;
+host_item *prev = NULL;
+host_item *h;
+
+if (removed != NULL) *removed = FALSE;
+
+if (local_interface_data == NULL) local_interface_data = host_find_interfaces();
+
+for (h = host; h != last->next; h = h->next)
+  {
+  #ifndef STAND_ALONE
+  if (hosts_treat_as_local != NULL)
+    {
+    int rc;
+    uschar *save = deliver_domain;
+    deliver_domain = h->name;   /* set $domain */
+    rc = match_isinlist(string_copylc(h->name), &hosts_treat_as_local, 0,
+      &domainlist_anchor, NULL, MCL_DOMAIN, TRUE, NULL);
+    deliver_domain = save;
+    if (rc == OK) goto FOUND_LOCAL;
+    }
+  #endif
+
+  /* It seems that on many operating systems, 0.0.0.0 is treated as a synonym
+  for 127.0.0.1 and refers to the local host. We therefore force it always to
+  be treated as local. */
+
+  if (h->address != NULL)
+    {
+    ip_address_item *ip;
+    if (Ustrcmp(h->address, "0.0.0.0") == 0) goto FOUND_LOCAL;
+    for (ip = local_interface_data; ip != NULL; ip = ip->next)
+      if (Ustrcmp(h->address, ip->address) == 0) goto FOUND_LOCAL;
+    yield = HOST_FOUND;  /* At least one remote address has been found */
+    }
+
+  /* Update prev to point to the last host item before any that have
+  the same MX value as the one we have just considered. */
+
+  if (h->next == NULL || h->next->mx != h->mx) prev = h;
+  }
+
+return yield;  /* No local hosts found: return HOST_FOUND or HOST_FIND_FAILED */
+
+/* A host whose IP address matches a local IP address, or whose name matches
+something in hosts_treat_as_local has been found. */
+
+FOUND_LOCAL:
+
+if (prev == NULL)
+  {
+  HDEBUG(D_host_lookup) debug_printf((h->mx >= 0)?
+    "local host has lowest MX\n" :
+    "local host found for non-MX address\n");
+  return HOST_FOUND_LOCAL;
+  }
+
+HDEBUG(D_host_lookup)
+  {
+  debug_printf("local host in host list - removed hosts:\n");
+  for (h = prev->next; h != last->next; h = h->next)
+    debug_printf("  %s %s %d\n", h->name, h->address, h->mx);
+  }
+
+if (removed != NULL) *removed = TRUE;
+prev->next = last->next;
+*lastptr = prev;
+return yield;
+}
+
+
+
+
+/*************************************************
+*        Remove duplicate IPs in host list       *
+*************************************************/
+
+/* You would think that administrators could set up their DNS records so that
+one ended up with a list of unique IP addresses after looking up A or MX
+records, but apparently duplication is common. So we scan such lists and
+remove the later duplicates. Note that we may get lists in which some host
+addresses are not set.
+
+Arguments:
+  host        pointer to the first host in the chain
+  lastptr     pointer to pointer to the last host in the chain (may be updated)
+
+Returns:      nothing
+*/
+
+static void
+host_remove_duplicates(host_item *host, host_item **lastptr)
+{
+while (host != *lastptr)
+  {
+  if (host->address != NULL)
+    {
+    host_item *h = host;
+    while (h != *lastptr)
+      {
+      if (h->next->address != NULL &&
+          Ustrcmp(h->next->address, host->address) == 0)
+        {
+        DEBUG(D_host_lookup) debug_printf("duplicate IP address %s (MX=%d) "
+          "removed\n", host->address, h->next->mx);
+        if (h->next == *lastptr) *lastptr = h;
+        h->next = h->next->next;
+        }
+      else h = h->next;
+      }
+    }
+  /* If the last item was removed, host may have become == *lastptr */
+  if (host != *lastptr) host = host->next;
+  }
+}
+
+
+
+
+/*************************************************
+*    Find sender host name by gethostbyaddr()    *
+*************************************************/
+
+/* This used to be the only way it was done, but it turns out that not all
+systems give aliases for calls to gethostbyaddr() - or one of the modern
+equivalents like getipnodebyaddr(). Fortunately, multiple PTR records are rare,
+but they can still exist. This function is now used only when a DNS lookup of
+the IP address fails, in order to give access to /etc/hosts.
+
+Arguments:   none
+Returns:     OK, DEFER, FAIL
+*/
+
+static int
+host_name_lookup_byaddr(void)
+{
+int len;
+uschar *s, *t;
+struct hostent *hosts;
+struct in_addr addr;
+
+/* Lookup on IPv6 system */
+
+#if HAVE_IPV6
+if (Ustrchr(sender_host_address, ':') != NULL)
+  {
+  struct in6_addr addr6;
+  if (inet_pton(AF_INET6, CS sender_host_address, &addr6) != 1)
+    log_write(0, LOG_MAIN|LOG_PANIC_DIE, "unable to parse \"%s\" as an "
+      "IPv6 address", sender_host_address);
+  #if HAVE_GETIPNODEBYADDR
+  hosts = getipnodebyaddr(CS &addr6, sizeof(addr6), AF_INET6, &h_errno);
+  #else
+  hosts = gethostbyaddr(CS &addr6, sizeof(addr6), AF_INET6);
+  #endif
+  }
+else
+  {
+  if (inet_pton(AF_INET, CS sender_host_address, &addr) != 1)
+    log_write(0, LOG_MAIN|LOG_PANIC_DIE, "unable to parse \"%s\" as an "
+      "IPv4 address", sender_host_address);
+  #if HAVE_GETIPNODEBYADDR
+  hosts = getipnodebyaddr(CS &addr, sizeof(addr), AF_INET, &h_errno);
+  #else
+  hosts = gethostbyaddr(CS &addr, sizeof(addr), AF_INET);
+  #endif
+  }
+
+/* Do lookup on IPv4 system */
+
+#else
+addr.s_addr = (S_ADDR_TYPE)inet_addr(CS sender_host_address);
+hosts = gethostbyaddr(CS(&addr), sizeof(addr), AF_INET);
+#endif
+
+/* Failed to look up the host. */
+
+if (hosts == NULL)
+  {
+  HDEBUG(D_host_lookup) debug_printf("IP address lookup failed: h_errno=%d\n",
+    h_errno);
+  return (h_errno == TRY_AGAIN || h_errno == NO_RECOVERY) ? DEFER : FAIL;
+  }
+
+/* It seems there are some records in the DNS that yield an empty name. We
+treat this as non-existent. In some operating systems, this is returned as an
+empty string; in others as a single dot. */
+
+if (hosts->h_name == NULL || hosts->h_name[0] == 0 || hosts->h_name[0] == '.')
+  {
+  HDEBUG(D_host_lookup) debug_printf("IP address lookup yielded an empty name: "
+    "treated as non-existent host name\n");
+  return FAIL;
+  }
+
+/* Copy and lowercase the name, which is in static storage in many systems.
+Put it in permanent memory. */
+
+s = (uschar *)hosts->h_name;
+len = Ustrlen(s) + 1;
+t = sender_host_name = store_get_perm(len);
+while (*s != 0) *t++ = tolower(*s++);
+*t = 0;
+
+/* If the host has aliases, build a copy of the alias list */
+
+if (hosts->h_aliases != NULL)
+  {
+  int count = 1;
+  uschar **aliases, **ptr;
+  for (aliases = USS hosts->h_aliases; *aliases != NULL; aliases++) count++;
+  ptr = sender_host_aliases = store_get_perm(count * sizeof(uschar *));
+  for (aliases = USS hosts->h_aliases; *aliases != NULL; aliases++)
+    {
+    uschar *s = *aliases;
+    int len = Ustrlen(s) + 1;
+    uschar *t = *ptr++ = store_get_perm(len);
+    while (*s != 0) *t++ = tolower(*s++);
+    *t = 0;
+    }
+  *ptr = NULL;
+  }
+
+return OK;
+}
+
+
+
+/*************************************************
+*        Find host name for incoming call        *
+*************************************************/
+
+/* Put the name in permanent store, pointed to by sender_host_name. We also set
+up a list of alias names, pointed to by sender_host_alias. The list is
+NULL-terminated. The incoming address is in sender_host_address, either in
+dotted-quad form for IPv4 or in colon-separated form for IPv6.
+
+This function does a thorough check that the names it finds point back to the
+incoming IP address. Any that do not are discarded. Note that this is relied on
+by the ACL reverse_host_lookup check.
+
+On some systems, get{host,ipnode}byaddr() appears to do this internally, but
+this it not universally true. Also, for release 4.30, this function was changed
+to do a direct DNS lookup first, by default[1], because it turns out that that
+is the only guaranteed way to find all the aliases on some systems. My
+experiments indicate that Solaris gethostbyaddr() gives the aliases for but
+Linux does not.
+
+[1] The actual order is controlled by the host_lookup_order option.
+
+Arguments:    none
+Returns:      OK on success, the answer being placed in the global variable
+                sender_host_name, with any aliases in a list hung off
+                sender_host_aliases
+              FAIL if no host name can be found
+              DEFER if a temporary error was encountered
+
+The variable host_lookup_msg is set to an empty string on sucess, or to a
+reason for the failure otherwise, in a form suitable for tagging onto an error
+message, and also host_lookup_failed is set TRUE if the lookup failed. If there
+was a defer, host_lookup_deferred is set TRUE.
+
+Any dynamically constructed string for host_lookup_msg must be in permanent
+store, because it might be used for several incoming messages on the same SMTP
+connection. */
+
+int
+host_name_lookup(void)
+{
+int old_pool, rc;
+int sep = 0;
+uschar *hname, *save_hostname;
+uschar **aliases;
+uschar buffer[256];
+uschar *ordername;
+uschar *list = host_lookup_order;
+dns_record *rr;
+dns_answer dnsa;
+dns_scan dnss;
+
+sender_host_dnssec = host_lookup_deferred = host_lookup_failed = FALSE;
+
+HDEBUG(D_host_lookup)
+  debug_printf("looking up host name for %s\n", sender_host_address);
+
+/* For testing the case when a lookup does not complete, we have a special
+reserved IP address. */
+
+if (running_in_test_harness &&
+    Ustrcmp(sender_host_address, "99.99.99.99") == 0)
+  {
+  HDEBUG(D_host_lookup)
+    debug_printf("Test harness: host name lookup returns DEFER\n");
+  host_lookup_deferred = TRUE;
+  return DEFER;
+  }
+
+/* Do lookups directly in the DNS or via gethostbyaddr() (or equivalent), in
+the order specified by the host_lookup_order option. */
+
+while ((ordername = string_nextinlist(&list, &sep, buffer, sizeof(buffer)))
+        != NULL)
+  {
+  if (strcmpic(ordername, US"bydns") == 0)
+    {
+    dns_init(FALSE, FALSE, FALSE);    /* dnssec ctrl by dns_dnssec_ok glbl */
+    dns_build_reverse(sender_host_address, buffer);
+    rc = dns_lookup(&dnsa, buffer, T_PTR, NULL);
+
+    /* The first record we come across is used for the name; others are
+    considered to be aliases. We have to scan twice, in order to find out the
+    number of aliases. However, if all the names are empty, we will behave as
+    if failure. (PTR records that yield empty names have been encountered in
+    the DNS.) */
+
+    if (rc == DNS_SUCCEED)
+      {
+      uschar **aptr = NULL;
+      int ssize = 264;
+      int count = 0;
+      int old_pool = store_pool;
+
+      /* Ideally we'd check DNSSEC both forward and reverse, but we use the
+      gethost* routines for forward, so can't do that unless/until we rewrite. */
+      sender_host_dnssec = dns_is_secure(&dnsa);
+      DEBUG(D_dns)
+        debug_printf("Reverse DNS security status: %s\n",
+            sender_host_dnssec ? "DNSSEC verified (AD)" : "unverified");
+
+      store_pool = POOL_PERM;        /* Save names in permanent storage */
+
+      for (rr = dns_next_rr(&dnsa, &dnss, RESET_ANSWERS);
+           rr != NULL;
+           rr = dns_next_rr(&dnsa, &dnss, RESET_NEXT))
+        {
+        if (rr->type == T_PTR) count++;
+        }
+
+      /* Get store for the list of aliases. For compatibility with
+      gethostbyaddr, we make an empty list if there are none. */
+
+      aptr = sender_host_aliases = store_get(count * sizeof(uschar *));
+
+      /* Re-scan and extract the names */
+
+      for (rr = dns_next_rr(&dnsa, &dnss, RESET_ANSWERS);
+           rr != NULL;
+           rr = dns_next_rr(&dnsa, &dnss, RESET_NEXT))
+        {
+        uschar *s = NULL;
+        if (rr->type != T_PTR) continue;
+        s = store_get(ssize);
+
+        /* If an overlong response was received, the data will have been
+        truncated and dn_expand may fail. */
+
+        if (dn_expand(dnsa.answer, dnsa.answer + dnsa.answerlen,
+             (uschar *)(rr->data), (DN_EXPAND_ARG4_TYPE)(s), ssize) < 0)
+          {
+          log_write(0, LOG_MAIN, "host name alias list truncated for %s",
+            sender_host_address);
+          break;
+          }
+
+        store_reset(s + Ustrlen(s) + 1);
+        if (s[0] == 0)
+          {
+          HDEBUG(D_host_lookup) debug_printf("IP address lookup yielded an "
+            "empty name: treated as non-existent host name\n");
+          continue;
+          }
+        if (sender_host_name == NULL) sender_host_name = s;
+          else *aptr++ = s;
+        while (*s != 0) { *s = tolower(*s); s++; }
+        }
+
+      *aptr = NULL;            /* End of alias list */
+      store_pool = old_pool;   /* Reset store pool */
+
+      /* If we've found a names, break out of the "order" loop */
+
+      if (sender_host_name != NULL) break;
+      }
+
+    /* If the DNS lookup deferred, we must also defer. */
+
+    if (rc == DNS_AGAIN)
+      {
+      HDEBUG(D_host_lookup)
+        debug_printf("IP address PTR lookup gave temporary error\n");
+      host_lookup_deferred = TRUE;
+      return DEFER;
+      }
+    }
+
+  /* Do a lookup using gethostbyaddr() - or equivalent */
+
+  else if (strcmpic(ordername, US"byaddr") == 0)
+    {
+    HDEBUG(D_host_lookup)
+      debug_printf("IP address lookup using gethostbyaddr()\n");
+    rc = host_name_lookup_byaddr();
+    if (rc == DEFER)
+      {
+      host_lookup_deferred = TRUE;
+      return rc;                       /* Can't carry on */
+      }
+    if (rc == OK) break;               /* Found a name */
+    }
+  }      /* Loop for bydns/byaddr scanning */
+
+/* If we have failed to find a name, return FAIL and log when required.
+NB host_lookup_msg must be in permanent store.  */
+
+if (sender_host_name == NULL)
+  {
+  if (host_checking || !log_testing_mode)
+    log_write(L_host_lookup_failed, LOG_MAIN, "no host name found for IP "
+      "address %s", sender_host_address);
+  host_lookup_msg = US" (failed to find host name from IP address)";
+  host_lookup_failed = TRUE;
+  return FAIL;
+  }
+
+HDEBUG(D_host_lookup)
+  {
+  uschar **aliases = sender_host_aliases;
+  debug_printf("IP address lookup yielded %s\n", sender_host_name);
+  while (*aliases != NULL) debug_printf("  alias %s\n", *aliases++);
+  }
+
+/* We need to verify that a forward lookup on the name we found does indeed
+correspond to the address. This is for security: in principle a malefactor who
+happened to own a reverse zone could set it to point to any names at all.
+
+This code was present in versions of Exim before 3.20. At that point I took it
+out because I thought that gethostbyaddr() did the check anyway. It turns out
+that this isn't always the case, so it's coming back in at 4.01. This version
+is actually better, because it also checks aliases.
+
+The code was made more robust at release 4.21. Prior to that, it accepted all
+the names if any of them had the correct IP address. Now the code checks all
+the names, and accepts only those that have the correct IP address. */
+
+save_hostname = sender_host_name;   /* Save for error messages */
+aliases = sender_host_aliases;
+for (hname = sender_host_name; hname != NULL; hname = *aliases++)
+  {
+  int rc;
+  BOOL ok = FALSE;
+  host_item h;
+  h.next = NULL;
+  h.name = hname;
+  h.mx = MX_NONE;
+  h.address = NULL;
+
+  /* When called with the last argument FALSE, host_find_byname() won't return
+  HOST_FOUND_LOCAL. If the incoming address is an IPv4 address expressed in
+  IPv6 format, we must compare the IPv4 part to any IPv4 addresses. */
+
+  if ((rc = host_find_byname(&h, NULL, 0, NULL, FALSE)) == HOST_FOUND)
+    {
+    host_item *hh;
+    HDEBUG(D_host_lookup) debug_printf("checking addresses for %s\n", hname);
+    for (hh = &h; hh != NULL; hh = hh->next)
+      {
+      if (host_is_in_net(hh->address, sender_host_address, 0))
+        {
+        HDEBUG(D_host_lookup) debug_printf("  %s OK\n", hh->address);
+        ok = TRUE;
+        break;
+        }
+      else
+        {
+        HDEBUG(D_host_lookup) debug_printf("  %s\n", hh->address);
+        }
+      }
+    if (!ok) HDEBUG(D_host_lookup)
+      debug_printf("no IP address for %s matched %s\n", hname,
+        sender_host_address);
+    }
+  else if (rc == HOST_FIND_AGAIN)
+    {
+    HDEBUG(D_host_lookup) debug_printf("temporary error for host name lookup\n");
+    host_lookup_deferred = TRUE;
+    sender_host_name = NULL;
+    return DEFER;
+    }
+  else
+    {
+    HDEBUG(D_host_lookup) debug_printf("no IP addresses found for %s\n", hname);
+    }
+
+  /* If this name is no good, and it's the sender name, set it null pro tem;
+  if it's an alias, just remove it from the list. */
+
+  if (!ok)
+    {
+    if (hname == sender_host_name) sender_host_name = NULL; else
+      {
+      uschar **a;                              /* Don't amalgamate - some */
+      a = --aliases;                           /* compilers grumble */
+      while (*a != NULL) { *a = a[1]; a++; }
+      }
+    }
+  }
+
+/* If sender_host_name == NULL, it means we didn't like the name. Replace
+it with the first alias, if there is one. */
+
+if (sender_host_name == NULL && *sender_host_aliases != NULL)
+  sender_host_name = *sender_host_aliases++;
+
+/* If we now have a main name, all is well. */
+
+if (sender_host_name != NULL) return OK;
+
+/* We have failed to find an address that matches. */
+
+HDEBUG(D_host_lookup)
+  debug_printf("%s does not match any IP address for %s\n",
+    sender_host_address, save_hostname);
+
+/* This message must be in permanent store */
+
+old_pool = store_pool;
+store_pool = POOL_PERM;
+host_lookup_msg = string_sprintf(" (%s does not match any IP address for %s)",
+  sender_host_address, save_hostname);
+store_pool = old_pool;
+host_lookup_failed = TRUE;
+return FAIL;
+}
+
+
+
+
+/*************************************************
+*    Find IP address(es) for host by name        *
+*************************************************/
+
+/* The input is a host_item structure with the name filled in and the address
+field set to NULL. We use gethostbyname() or getipnodebyname() or
+gethostbyname2(), as appropriate. Of course, these functions may use the DNS,
+but they do not do MX processing. It appears, however, that in some systems the
+current setting of resolver options is used when one of these functions calls
+the resolver. For this reason, we call dns_init() at the start, with arguments
+influenced by bits in "flags", just as we do for host_find_bydns().
+
+The second argument provides a host list (usually an IP list) of hosts to
+ignore. This makes it possible to ignore IPv6 link-local addresses or loopback
+addresses in unreasonable places.
+
+The lookup may result in a change of name. For compatibility with the dns
+lookup, return this via fully_qualified_name as well as updating the host item.
+The lookup may also yield more than one IP address, in which case chain on
+subsequent host_item structures.
+
+Arguments:
+  host                   a host item with the name and MX filled in;
+                           the address is to be filled in;
+                           multiple IP addresses cause other host items to be
+                             chained on.
+  ignore_target_hosts    a list of hosts to ignore
+  flags                  HOST_FIND_QUALIFY_SINGLE   ) passed to
+                         HOST_FIND_SEARCH_PARENTS   )   dns_init()
+  fully_qualified_name   if not NULL, set to point to host name for
+                         compatibility with host_find_bydns
+  local_host_check       TRUE if a check for the local host is wanted
+
+Returns:                 HOST_FIND_FAILED  Failed to find the host or domain
+                         HOST_FIND_AGAIN   Try again later
+                         HOST_FOUND        Host found - data filled in
+                         HOST_FOUND_LOCAL  Host found and is the local host
+*/
+
+int
+host_find_byname(host_item *host, uschar *ignore_target_hosts, int flags,
+  uschar **fully_qualified_name, BOOL local_host_check)
+{
+int i, yield, times;
+uschar **addrlist;
+host_item *last = NULL;
+BOOL temp_error = FALSE;
+#if HAVE_IPV6
+int af;
+#endif
+
+/* If we are in the test harness, a name ending in .test.again.dns always
+forces a temporary error response, unless the name is in
+dns_again_means_nonexist. */
+
+if (running_in_test_harness)
+  {
+  uschar *endname = host->name + Ustrlen(host->name);
+  if (Ustrcmp(endname - 14, "test.again.dns") == 0) goto RETURN_AGAIN;
+  }
+
+/* Make sure DNS options are set as required. This appears to be necessary in
+some circumstances when the get..byname() function actually calls the DNS. */
+
+dns_init((flags & HOST_FIND_QUALIFY_SINGLE) != 0,
+         (flags & HOST_FIND_SEARCH_PARENTS) != 0,
+        FALSE);        /*XXX dnssec? */
+
+/* In an IPv6 world, unless IPv6 has been disabled, we need to scan for both
+kinds of address, so go round the loop twice. Note that we have ensured that
+AF_INET6 is defined even in an IPv4 world, which makes for slightly tidier
+code. However, if dns_ipv4_lookup matches the domain, we also just do IPv4
+lookups here (except when testing standalone). */
+
+#if HAVE_IPV6
+  #ifdef STAND_ALONE
+  if (disable_ipv6)
+  #else
+  if (disable_ipv6 ||
+    (dns_ipv4_lookup != NULL &&
+        match_isinlist(host->name, &dns_ipv4_lookup, 0, NULL, NULL, MCL_DOMAIN,
+          TRUE, NULL) == OK))
+  #endif
+
+    { af = AF_INET; times = 1; }
+  else
+    { af = AF_INET6; times = 2; }
+
+/* No IPv6 support */
+
+#else   /* HAVE_IPV6 */
+  times = 1;
+#endif  /* HAVE_IPV6 */
+
+/* Initialize the flag that gets set for DNS syntax check errors, so that the
+interface to this function can be similar to host_find_bydns. */
+
+host_find_failed_syntax = FALSE;
+
+/* Loop to look up both kinds of address in an IPv6 world */
+
+for (i = 1; i <= times;
+     #if HAVE_IPV6
+       af = AF_INET,     /* If 2 passes, IPv4 on the second */
+     #endif
+     i++)
+  {
+  BOOL ipv4_addr;
+  int error_num = 0;
+  struct hostent *hostdata;
+
+  #ifdef STAND_ALONE
+  printf("Looking up: %s\n", host->name);
+  #endif
+
+  #if HAVE_IPV6
+  if (running_in_test_harness)
+    hostdata = host_fake_gethostbyname(host->name, af, &error_num);
+  else
+    {
+    #if HAVE_GETIPNODEBYNAME
+    hostdata = getipnodebyname(CS host->name, af, 0, &error_num);
+    #else
+    hostdata = gethostbyname2(CS host->name, af);
+    error_num = h_errno;
+    #endif
+    }
+
+  #else    /* not HAVE_IPV6 */
+  if (running_in_test_harness)
+    hostdata = host_fake_gethostbyname(host->name, AF_INET, &error_num);
+  else
+    {
+    hostdata = gethostbyname(CS host->name);
+    error_num = h_errno;
+    }
+  #endif   /* HAVE_IPV6 */
+
+  if (hostdata == NULL)
+    {
+    uschar *error;
+    switch (error_num)
+      {
+      case HOST_NOT_FOUND: error = US"HOST_NOT_FOUND"; break;
+      case TRY_AGAIN: error = US"TRY_AGAIN"; break;
+      case NO_RECOVERY: error = US"NO_RECOVERY"; break;
+      case NO_DATA: error = US"NO_DATA"; break;
+      #if NO_DATA != NO_ADDRESS
+      case NO_ADDRESS: error = US"NO_ADDRESS"; break;
+      #endif
+      default: error = US"?"; break;
+      }
+
+    DEBUG(D_host_lookup) debug_printf("%s returned %d (%s)\n",
+      #if HAVE_IPV6
+        #if HAVE_GETIPNODEBYNAME
+        (af == AF_INET6)? "getipnodebyname(af=inet6)" : "getipnodebyname(af=inet)",
+        #else
+        (af == AF_INET6)? "gethostbyname2(af=inet6)" : "gethostbyname2(af=inet)",
+        #endif
+      #else
+      "gethostbyname",
+      #endif
+      error_num, error);
+
+    if (error_num == TRY_AGAIN || error_num == NO_RECOVERY) temp_error = TRUE;
+    continue;
+    }
+  if ((hostdata->h_addr_list)[0] == NULL) continue;
+
+  /* Replace the name with the fully qualified one if necessary, and fill in
+  the fully_qualified_name pointer. */
+
+  if (hostdata->h_name[0] != 0 &&
+      Ustrcmp(host->name, hostdata->h_name) != 0)
+    host->name = string_copy_dnsdomain((uschar *)hostdata->h_name);
+  if (fully_qualified_name != NULL) *fully_qualified_name = host->name;
+
+  /* Get the list of addresses. IPv4 and IPv6 addresses can be distinguished
+  by their different lengths. Scan the list, ignoring any that are to be
+  ignored, and build a chain from the rest. */
+
+  ipv4_addr = hostdata->h_length == sizeof(struct in_addr);
+
+  for (addrlist = USS hostdata->h_addr_list; *addrlist != NULL; addrlist++)
+    {
+    uschar *text_address =
+      host_ntoa(ipv4_addr? AF_INET:AF_INET6, *addrlist, NULL, NULL);
+
+    #ifndef STAND_ALONE
+    if (ignore_target_hosts != NULL &&
+        verify_check_this_host(&ignore_target_hosts, NULL, host->name,
+          text_address, NULL) == OK)
+      {
+      DEBUG(D_host_lookup)
+        debug_printf("ignored host %s [%s]\n", host->name, text_address);
+      continue;
+      }
+    #endif
+
+    /* If this is the first address, last == NULL and we put the data in the
+    original block. */
+
+    if (last == NULL)
+      {
+      host->address = text_address;
+      host->port = PORT_NONE;
+      host->status = hstatus_unknown;
+      host->why = hwhy_unknown;
+      host->dnssec = DS_UNK;
+      last = host;
+      }
+
+    /* Else add further host item blocks for any other addresses, keeping
+    the order. */
+
+    else
+      {
+      host_item *next = store_get(sizeof(host_item));
+      next->name = host->name;
+      next->mx = host->mx;
+      next->address = text_address;
+      next->port = PORT_NONE;
+      next->status = hstatus_unknown;
+      next->why = hwhy_unknown;
+      next->dnssec = DS_UNK;
+      next->last_try = 0;
+      next->next = last->next;
+      last->next = next;
+      last = next;
+      }
+    }
+  }
+
+/* If no hosts were found, the address field in the original host block will be
+NULL. If temp_error is set, at least one of the lookups gave a temporary error,
+so we pass that back. */
+
+if (host->address == NULL)
+  {
+  uschar *msg =
+    #ifndef STAND_ALONE
+    (message_id[0] == 0 && smtp_in != NULL)?
+      string_sprintf("no IP address found for host %s (during %s)", host->name,
+          smtp_get_connection_info()) :
+    #endif
+    string_sprintf("no IP address found for host %s", host->name);
+
+  HDEBUG(D_host_lookup) debug_printf("%s\n", msg);
+  if (temp_error) goto RETURN_AGAIN;
+  if (host_checking || !log_testing_mode)
+    log_write(L_host_lookup_failed, LOG_MAIN, "%s", msg);
+  return HOST_FIND_FAILED;
+  }
+
+/* Remove any duplicate IP addresses, then check to see if this is the local
+host if required. */
+
+host_remove_duplicates(host, &last);
+yield = local_host_check?
+  host_scan_for_local_hosts(host, &last, NULL) : HOST_FOUND;
+
+HDEBUG(D_host_lookup)
+  {
+  host_item *h;
+  if (fully_qualified_name != NULL)
+    debug_printf("fully qualified name = %s\n", *fully_qualified_name);
+  debug_printf("%s looked up these IP addresses:\n",
+    #if HAVE_IPV6
+      #if HAVE_GETIPNODEBYNAME
+      "getipnodebyname"
+      #else
+      "gethostbyname2"
+      #endif
+    #else
+    "gethostbyname"
+    #endif
+    );
+  for (h = host; h != last->next; h = h->next)
+    debug_printf("  name=%s address=%s\n", h->name,
+      (h->address == NULL)? US"<null>" : h->address);
+  }
+
+/* Return the found status. */
+
+return yield;
+
+/* Handle the case when there is a temporary error. If the name matches
+dns_again_means_nonexist, return permanent rather than temporary failure. */
+
+RETURN_AGAIN:
+  {
+  #ifndef STAND_ALONE
+  int rc;
+  uschar *save = deliver_domain;
+  deliver_domain = host->name;  /* set $domain */
+  rc = match_isinlist(host->name, &dns_again_means_nonexist, 0, NULL, NULL,
+    MCL_DOMAIN, TRUE, NULL);
+  deliver_domain = save;
+  if (rc == OK)
+    {
+    DEBUG(D_host_lookup) debug_printf("%s is in dns_again_means_nonexist: "
+      "returning HOST_FIND_FAILED\n", host->name);
+    return HOST_FIND_FAILED;
+    }
+  #endif
+  return HOST_FIND_AGAIN;
+  }
+}
+
+
+
+/*************************************************
+*        Fill in a host address from the DNS     *
+*************************************************/
+
+/* Given a host item, with its name, port and mx fields set, and its address
+field set to NULL, fill in its IP address from the DNS. If it is multi-homed,
+create additional host items for the additional addresses, copying all the
+other fields, and randomizing the order.
+
+On IPv6 systems, A6 records are sought first (but only if support for A6 is
+configured - they may never become mainstream), then AAAA records are sought,
+and finally A records are sought as well.
+
+The host name may be changed if the DNS returns a different name - e.g. fully
+qualified or changed via CNAME. If fully_qualified_name is not NULL, dns_lookup
+ensures that it points to the fully qualified name. However, this is the fully
+qualified version of the original name; if a CNAME is involved, the actual
+canonical host name may be different again, and so we get it directly from the
+relevant RR. Note that we do NOT change the mx field of the host item in this
+function as it may be called to set the addresses of hosts taken from MX
+records.
+
+Arguments:
+  host                  points to the host item we're filling in
+  lastptr               points to pointer to last host item in a chain of
+                          host items (may be updated if host is last and gets
+                          extended because multihomed)
+  ignore_target_hosts   list of hosts to ignore
+  allow_ip              if TRUE, recognize an IP address and return it
+  fully_qualified_name  if not NULL, return fully qualified name here if
+                          the contents are different (i.e. it must be preset
+                          to something)
+  dnnssec_require      if TRUE check the DNS result AD bit
+
+Returns:       HOST_FIND_FAILED     couldn't find A record
+               HOST_FIND_AGAIN      try again later
+               HOST_FOUND           found AAAA and/or A record(s)
+               HOST_IGNORED         found, but all IPs ignored
+*/
+
+static int
+set_address_from_dns(host_item *host, host_item **lastptr,
+  uschar *ignore_target_hosts, BOOL allow_ip, uschar **fully_qualified_name,
+  BOOL dnssec_requested, BOOL dnssec_require)
+{
+dns_record *rr;
+host_item *thishostlast = NULL;    /* Indicates not yet filled in anything */
+BOOL v6_find_again = FALSE;
+int i;
+
+/* If allow_ip is set, a name which is an IP address returns that value
+as its address. This is used for MX records when allow_mx_to_ip is set, for
+those sites that feel they have to flaunt the RFC rules. */
+
+if (allow_ip && string_is_ip_address(host->name, NULL) != 0)
+  {
+  #ifndef STAND_ALONE
+  if (ignore_target_hosts != NULL &&
+        verify_check_this_host(&ignore_target_hosts, NULL, host->name,
+        host->name, NULL) == OK)
+    return HOST_IGNORED;
+  #endif
+
+  host->address = host->name;
+  return HOST_FOUND;
+  }
+
+/* On an IPv6 system, unless IPv6 is disabled, go round the loop up to three
+times, looking for A6 and AAAA records the first two times. However, unless
+doing standalone testing, we force an IPv4 lookup if the domain matches
+dns_ipv4_lookup is set. Since A6 records look like being abandoned, support
+them only if explicitly configured to do so. On an IPv4 system, go round the
+loop once only, looking only for A records. */
+
+#if HAVE_IPV6
+  #ifndef STAND_ALONE
+    if (disable_ipv6 || (dns_ipv4_lookup != NULL &&
+        match_isinlist(host->name, &dns_ipv4_lookup, 0, NULL, NULL, MCL_DOMAIN,
+        TRUE, NULL) == OK))
+      i = 0;    /* look up A records only */
+    else
+  #endif        /* STAND_ALONE */
+
+  #ifdef SUPPORT_A6
+  i = 2;        /* look up A6 and AAAA and A records */
+  #else
+  i = 1;        /* look up AAAA and A records */
+  #endif        /* SUPPORT_A6 */
+
+/* The IPv4 world */
+
+#else           /* HAVE_IPV6 */
+  i = 0;        /* look up A records only */
+#endif          /* HAVE_IPV6 */
+
+for (; i >= 0; i--)
+  {
+  static int types[] = { T_A, T_AAAA, T_A6 };
+  int type = types[i];
+  int randoffset = (i == 0)? 500 : 0;  /* Ensures v6 sorts before v4 */
+  dns_answer dnsa;
+  dns_scan dnss;
+
+  int rc = dns_lookup(&dnsa, host->name, type, fully_qualified_name);
+  lookup_dnssec_authenticated = !dnssec_requested ? NULL
+    : dns_is_secure(&dnsa) ? US"yes" : US"no";
+
+  /* We want to return HOST_FIND_AGAIN if one of the A, A6, or AAAA lookups
+  fails or times out, but not if another one succeeds. (In the early
+  IPv6 days there are name servers that always fail on AAAA, but are happy
+  to give out an A record. We want to proceed with that A record.) */
+
+  if (rc != DNS_SUCCEED)
+    {
+    if (i == 0)  /* Just tried for an A record, i.e. end of loop */
+      {
+      if (host->address != NULL) return HOST_FOUND;  /* A6 or AAAA was found */
+      if (rc == DNS_AGAIN || rc == DNS_FAIL || v6_find_again)
+        return HOST_FIND_AGAIN;
+      return HOST_FIND_FAILED;    /* DNS_NOMATCH or DNS_NODATA */
+      }
+
+    /* Tried for an A6 or AAAA record: remember if this was a temporary
+    error, and look for the next record type. */
+
+    if (rc != DNS_NOMATCH && rc != DNS_NODATA) v6_find_again = TRUE;
+    continue;
+    }
+  if (dnssec_require && !dns_is_secure(&dnsa))
+    {
+    log_write(L_host_lookup_failed, LOG_MAIN, "dnssec fail on %s for %.256s",
+               i>1 ? "A6" : i>0 ? "AAAA" : "A", host->name);
+    continue;
+    }
+
+  /* Lookup succeeded: fill in the given host item with the first non-ignored
+  address found; create additional items for any others. A single A6 record
+  may generate more than one address. */
+
+  for (rr = dns_next_rr(&dnsa, &dnss, RESET_ANSWERS);
+       rr != NULL;
+       rr = dns_next_rr(&dnsa, &dnss, RESET_NEXT))
+    {
+    if (rr->type == type)
+      {
+      /* dns_address *da = dns_address_from_rr(&dnsa, rr); */
+
+      dns_address *da;
+      da = dns_address_from_rr(&dnsa, rr);
+
+      DEBUG(D_host_lookup)
+        {
+        if (da == NULL)
+          debug_printf("no addresses extracted from A6 RR for %s\n",
+            host->name);
+        }
+
+      /* This loop runs only once for A and AAAA records, but may run
+      several times for an A6 record that generated multiple addresses. */
+
+      for (; da != NULL; da = da->next)
+        {
+        #ifndef STAND_ALONE
+        if (ignore_target_hosts != NULL &&
+              verify_check_this_host(&ignore_target_hosts, NULL,
+                host->name, da->address, NULL) == OK)
+          {
+          DEBUG(D_host_lookup)
+            debug_printf("ignored host %s [%s]\n", host->name, da->address);
+          continue;
+          }
+        #endif
+
+        /* If this is the first address, stick it in the given host block,
+        and change the name if the returned RR has a different name. */
+
+        if (thishostlast == NULL)
+          {
+          if (strcmpic(host->name, rr->name) != 0)
+            host->name = string_copy_dnsdomain(rr->name);
+          host->address = da->address;
+          host->sort_key = host->mx * 1000 + random_number(500) + randoffset;
+          host->status = hstatus_unknown;
+          host->why = hwhy_unknown;
+          thishostlast = host;
+          }
+
+        /* Not the first address. Check for, and ignore, duplicates. Then
+        insert in the chain at a random point. */
+
+        else
+          {
+          int new_sort_key;
+          host_item *next;
+
+          /* End of our local chain is specified by "thishostlast". */
+
+          for (next = host;; next = next->next)
+            {
+            if (Ustrcmp(CS da->address, next->address) == 0) break;
+            if (next == thishostlast) { next = NULL; break; }
+            }
+          if (next != NULL) continue;  /* With loop for next address */
+
+          /* Not a duplicate */
+
+          new_sort_key = host->mx * 1000 + random_number(500) + randoffset;
+          next = store_get(sizeof(host_item));
+
+          /* New address goes first: insert the new block after the first one
+          (so as not to disturb the original pointer) but put the new address
+          in the original block. */
+
+          if (new_sort_key < host->sort_key)
+            {
+            *next = *host;                                  /* Copies port */
+            host->next = next;
+            host->address = da->address;
+            host->sort_key = new_sort_key;
+            if (thishostlast == host) thishostlast = next;  /* Local last */
+            if (*lastptr == host) *lastptr = next;          /* Global last */
+            }
+
+          /* Otherwise scan down the addresses for this host to find the
+          one to insert after. */
+
+          else
+            {
+            host_item *h = host;
+            while (h != thishostlast)
+              {
+              if (new_sort_key < h->next->sort_key) break;
+              h = h->next;
+              }
+            *next = *h;                                 /* Copies port */
+            h->next = next;
+            next->address = da->address;
+            next->sort_key = new_sort_key;
+            if (h == thishostlast) thishostlast = next; /* Local last */
+            if (h == *lastptr) *lastptr = next;         /* Global last */
+            }
+          }
+        }
+      }
+    }
+  }
+
+/* Control gets here only if the third lookup (the A record) succeeded.
+However, the address may not be filled in if it was ignored. */
+
+return (host->address == NULL)? HOST_IGNORED : HOST_FOUND;
+}
+
+
+
+
+/*************************************************
+*    Find IP addresses and host names via DNS    *
+*************************************************/
+
+/* The input is a host_item structure with the name field filled in and the
+address field set to NULL. This may be in a chain of other host items. The
+lookup may result in more than one IP address, in which case we must created
+new host blocks for the additional addresses, and insert them into the chain.
+The original name may not be fully qualified. Use the fully_qualified_name
+argument to return the official name, as returned by the resolver.
+
+Arguments:
+  host                  point to initial host item
+  ignore_target_hosts   a list of hosts to ignore
+  whichrrs              flags indicating which RRs to look for:
+                          HOST_FIND_BY_SRV  => look for SRV
+                          HOST_FIND_BY_MX   => look for MX
+                          HOST_FIND_BY_A    => look for A or AAAA
+                        also flags indicating how the lookup is done
+                          HOST_FIND_QUALIFY_SINGLE   ) passed to the
+                          HOST_FIND_SEARCH_PARENTS   )   resolver
+  srv_service           when SRV used, the service name
+  srv_fail_domains      DNS errors for these domains => assume nonexist
+  mx_fail_domains       DNS errors for these domains => assume nonexist
+  dnssec_request_domains => make dnssec request
+  dnssec_require_domains => ditto and nonexist failures
+  fully_qualified_name  if not NULL, return fully-qualified name
+  removed               set TRUE if local host was removed from the list
+
+Returns:                HOST_FIND_FAILED  Failed to find the host or domain;
+                                          if there was a syntax error,
+                                          host_find_failed_syntax is set.
+                        HOST_FIND_AGAIN   Could not resolve at this time
+                        HOST_FOUND        Host found
+                        HOST_FOUND_LOCAL  The lowest MX record points to this
+                                          machine, if MX records were found, or
+                                          an A record that was found contains
+                                          an address of the local host
+*/
+
+int
+host_find_bydns(host_item *host, uschar *ignore_target_hosts, int whichrrs,
+  uschar *srv_service, uschar *srv_fail_domains, uschar *mx_fail_domains,
+  uschar *dnssec_request_domains, uschar *dnssec_require_domains,
+  uschar **fully_qualified_name, BOOL *removed)
+{
+host_item *h, *last;
+dns_record *rr;
+int rc = DNS_FAIL;
+int ind_type = 0;
+int yield;
+dns_answer dnsa;
+dns_scan dnss;
+BOOL dnssec_require = match_isinlist(host->name, &dnssec_require_domains,
+                                   0, NULL, NULL, MCL_DOMAIN, TRUE, NULL) == OK;
+BOOL dnssec_request = dnssec_require
+                   || match_isinlist(host->name, &dnssec_request_domains,
+                                   0, NULL, NULL, MCL_DOMAIN, TRUE, NULL) == OK;
+dnssec_status_t dnssec;
+
+/* Set the default fully qualified name to the incoming name, initialize the
+resolver if necessary, set up the relevant options, and initialize the flag
+that gets set for DNS syntax check errors. */
+
+if (fully_qualified_name != NULL) *fully_qualified_name = host->name;
+dns_init((whichrrs & HOST_FIND_QUALIFY_SINGLE) != 0,
+         (whichrrs & HOST_FIND_SEARCH_PARENTS) != 0,
+        dnssec_request
+        );
+host_find_failed_syntax = FALSE;
+
+/* First, if requested, look for SRV records. The service name is given; we
+assume TCP progocol. DNS domain names are constrained to a maximum of 256
+characters, so the code below should be safe. */
+
+if ((whichrrs & HOST_FIND_BY_SRV) != 0)
+  {
+  uschar buffer[300];
+  uschar *temp_fully_qualified_name = buffer;
+  int prefix_length;
+
+  (void)sprintf(CS buffer, "_%s._tcp.%n%.256s", srv_service, &prefix_length,
+    host->name);
+  ind_type = T_SRV;
+
+  /* Search for SRV records. If the fully qualified name is different to
+  the input name, pass back the new original domain, without the prepended
+  magic. */
+
+  dnssec = DS_UNK;
+  lookup_dnssec_authenticated = NULL;
+  rc = dns_lookup(&dnsa, buffer, ind_type, &temp_fully_qualified_name);
+
+  if (dnssec_request)
+    {
+    if (dns_is_secure(&dnsa))
+      { dnssec = DS_YES; lookup_dnssec_authenticated = US"yes"; }
+    else
+      { dnssec = DS_NO; lookup_dnssec_authenticated = US"no"; }
+    }
+
+  if (temp_fully_qualified_name != buffer && fully_qualified_name != NULL)
+    *fully_qualified_name = temp_fully_qualified_name + prefix_length;
+
+  /* On DNS failures, we give the "try again" error unless the domain is
+  listed as one for which we continue. */
+
+  if (rc == DNS_SUCCEED && dnssec_require && !dns_is_secure(&dnsa))
+    {
+    log_write(L_host_lookup_failed, LOG_MAIN,
+               "dnssec fail on SRV for %.256s", host->name);
+    rc = DNS_FAIL;
+    }
+  if (rc == DNS_FAIL || rc == DNS_AGAIN)
+    {
+    #ifndef STAND_ALONE
+    if (match_isinlist(host->name, &srv_fail_domains, 0, NULL, NULL, MCL_DOMAIN,
+        TRUE, NULL) != OK)
+    #endif
+      { yield = HOST_FIND_AGAIN; goto out; }
+    DEBUG(D_host_lookup) debug_printf("DNS_%s treated as DNS_NODATA "
+      "(domain in srv_fail_domains)\n", (rc == DNS_FAIL)? "FAIL":"AGAIN");
+    }
+  }
+
+/* If we did not find any SRV records, search the DNS for MX records, if
+requested to do so. If the result is DNS_NOMATCH, it means there is no such
+domain, and there's no point in going on to look for address records with the
+same domain. The result will be DNS_NODATA if the domain exists but has no MX
+records. On DNS failures, we give the "try again" error unless the domain is
+listed as one for which we continue. */
+
+if (rc != DNS_SUCCEED && (whichrrs & HOST_FIND_BY_MX) != 0)
+  {
+  ind_type = T_MX;
+  dnssec = DS_UNK;
+  lookup_dnssec_authenticated = NULL;
+  rc = dns_lookup(&dnsa, host->name, ind_type, fully_qualified_name);
+
+  if (dnssec_request)
+    {
+    if (dns_is_secure(&dnsa))
+      { dnssec = DS_YES; lookup_dnssec_authenticated = US"yes"; }
+    else
+      { dnssec = DS_NO; lookup_dnssec_authenticated = US"no"; }
+    }
+
+  switch (rc)
+    {
+    case DNS_NOMATCH:
+      yield = HOST_FIND_FAILED; goto out;
+
+    case DNS_SUCCEED:
+      if (!dnssec_require || dns_is_secure(&dnsa))
+       break;
+      log_write(L_host_lookup_failed, LOG_MAIN,
+                 "dnssec fail on MX for %.256s", host->name);
+      rc = DNS_FAIL;
+      /*FALLTRHOUGH*/
+
+    case DNS_FAIL:
+    case DNS_AGAIN:
+      #ifndef STAND_ALONE
+      if (match_isinlist(host->name, &mx_fail_domains, 0, NULL, NULL, MCL_DOMAIN,
+         TRUE, NULL) != OK)
+      #endif
+       { yield = HOST_FIND_AGAIN; goto out; }
+      DEBUG(D_host_lookup) debug_printf("DNS_%s treated as DNS_NODATA "
+       "(domain in mx_fail_domains)\n", (rc == DNS_FAIL)? "FAIL":"AGAIN");
+      break;
+    }
+  }
+
+/* If we haven't found anything yet, and we are requested to do so, try for an
+A or AAAA record. If we find it (or them) check to see that it isn't the local
+host. */
+
+if (rc != DNS_SUCCEED)
+  {
+  if ((whichrrs & HOST_FIND_BY_A) == 0)
+    {
+    DEBUG(D_host_lookup) debug_printf("Address records are not being sought\n");
+    yield = HOST_FIND_FAILED;
+    goto out;
+    }
+
+  last = host;        /* End of local chainlet */
+  host->mx = MX_NONE;
+  host->port = PORT_NONE;
+  dnssec = DS_UNK;
+  lookup_dnssec_authenticated = NULL;
+  rc = set_address_from_dns(host, &last, ignore_target_hosts, FALSE,
+    fully_qualified_name, dnssec_request, dnssec_require);
+
+  if (dnssec_request)
+    {
+    if (dns_is_secure(&dnsa))
+      { dnssec = DS_YES; lookup_dnssec_authenticated = US"yes"; }
+    else
+      { dnssec = DS_NO; lookup_dnssec_authenticated = US"no"; }
+    }
+
+  /* If one or more address records have been found, check that none of them
+  are local. Since we know the host items all have their IP addresses
+  inserted, host_scan_for_local_hosts() can only return HOST_FOUND or
+  HOST_FOUND_LOCAL. We do not need to scan for duplicate IP addresses here,
+  because set_address_from_dns() removes them. */
+
+  if (rc == HOST_FOUND)
+    rc = host_scan_for_local_hosts(host, &last, removed);
+  else
+    if (rc == HOST_IGNORED) rc = HOST_FIND_FAILED;  /* No special action */
+
+  DEBUG(D_host_lookup)
+    {
+    host_item *h;
+    if (host->address != NULL)
+      {
+      if (fully_qualified_name != NULL)
+        debug_printf("fully qualified name = %s\n", *fully_qualified_name);
+      for (h = host; h != last->next; h = h->next)
+        debug_printf("%s %s mx=%d sort=%d %s\n", h->name,
+          (h->address == NULL)? US"<null>" : h->address, h->mx, h->sort_key,
+          (h->status >= hstatus_unusable)? US"*" : US"");
+      }
+    }
+
+  yield = rc;
+  goto out;
+  }
+
+/* We have found one or more MX or SRV records. Sort them according to
+precedence. Put the data for the first one into the existing host block, and
+insert new host_item blocks into the chain for the remainder. For equal
+precedences one is supposed to randomize the order. To make this happen, the
+sorting is actually done on the MX value * 1000 + a random number. This is put
+into a host field called sort_key.
+
+In the case of hosts with both IPv6 and IPv4 addresses, we want to choose the
+IPv6 address in preference. At this stage, we don't know what kind of address
+the host has. We choose a random number < 500; if later we find an A record
+first, we add 500 to the random number. Then for any other address records, we
+use random numbers in the range 0-499 for AAAA records and 500-999 for A
+records.
+
+At this point we remove any duplicates that point to the same host, retaining
+only the one with the lowest precedence. We cannot yet check for precedence
+greater than that of the local host, because that test cannot be properly done
+until the addresses have been found - an MX record may point to a name for this
+host which is not the primary hostname. */
+
+last = NULL;    /* Indicates that not even the first item is filled yet */
+
+for (rr = dns_next_rr(&dnsa, &dnss, RESET_ANSWERS);
+     rr != NULL;
+     rr = dns_next_rr(&dnsa, &dnss, RESET_NEXT))
+  {
+  int precedence;
+  int weight = 0;        /* For SRV records */
+  int port = PORT_NONE;
+  uschar *s;             /* MUST be unsigned for GETSHORT */
+  uschar data[256];
+
+  if (rr->type != ind_type) continue;
+  s = rr->data;
+  GETSHORT(precedence, s);      /* Pointer s is advanced */
+
+  /* For MX records, we use a random "weight" which causes multiple records of
+  the same precedence to sort randomly. */
+
+  if (ind_type == T_MX)
+    weight = random_number(500);
+
+  /* SRV records are specified with a port and a weight. The weight is used
+  in a special algorithm. However, to start with, we just use it to order the
+  records of equal priority (precedence). */
+
+  else
+    {
+    GETSHORT(weight, s);
+    GETSHORT(port, s);
+    }
+
+  /* Get the name of the host pointed to. */
+
+  (void)dn_expand(dnsa.answer, dnsa.answer + dnsa.answerlen, s,
+    (DN_EXPAND_ARG4_TYPE)data, sizeof(data));
+
+  /* Check that we haven't already got this host on the chain; if we have,
+  keep only the lower precedence. This situation shouldn't occur, but you
+  never know what junk might get into the DNS (and this case has been seen on
+  more than one occasion). */
+
+  if (last != NULL)       /* This is not the first record */
+    {
+    host_item *prev = NULL;
+
+    for (h = host; h != last->next; prev = h, h = h->next)
+      {
+      if (strcmpic(h->name, data) == 0)
+        {
+        DEBUG(D_host_lookup)
+          debug_printf("discarded duplicate host %s (MX=%d)\n", data,
+            (precedence > h->mx)? precedence : h->mx);
+        if (precedence >= h->mx) goto NEXT_MX_RR; /* Skip greater precedence */
+        if (h == host)                            /* Override first item */
+          {
+          h->mx = precedence;
+          host->sort_key = precedence * 1000 + weight;
+          goto NEXT_MX_RR;
+          }
+
+        /* Unwanted host item is not the first in the chain, so we can get
+        get rid of it by cutting it out. */
+
+        prev->next = h->next;
+        if (h == last) last = prev;
+        break;
+        }
+      }
+    }
+
+  /* If this is the first MX or SRV record, put the data into the existing host
+  block. Otherwise, add a new block in the correct place; if it has to be
+  before the first block, copy the first block's data to a new second block. */
+
+  if (last == NULL)
+    {
+    host->name = string_copy_dnsdomain(data);
+    host->address = NULL;
+    host->port = port;
+    host->mx = precedence;
+    host->sort_key = precedence * 1000 + weight;
+    host->status = hstatus_unknown;
+    host->why = hwhy_unknown;
+    host->dnssec = dnssec;
+    last = host;
+    }
+
+  /* Make a new host item and seek the correct insertion place */
+
+  else
+    {
+    int sort_key = precedence * 1000 + weight;
+    host_item *next = store_get(sizeof(host_item));
+    next->name = string_copy_dnsdomain(data);
+    next->address = NULL;
+    next->port = port;
+    next->mx = precedence;
+    next->sort_key = sort_key;
+    next->status = hstatus_unknown;
+    next->why = hwhy_unknown;
+    next->dnssec = dnssec;
+    next->last_try = 0;
+
+    /* Handle the case when we have to insert before the first item. */
+
+    if (sort_key < host->sort_key)
+      {
+      host_item htemp;
+      htemp = *host;
+      *host = *next;
+      *next = htemp;
+      host->next = next;
+      if (last == host) last = next;
+      }
+
+    /* Else scan down the items we have inserted as part of this exercise;
+    don't go further. */
+
+    else
+      {
+      for (h = host; h != last; h = h->next)
+        {
+        if (sort_key < h->next->sort_key)
+          {
+          next->next = h->next;
+          h->next = next;
+          break;
+          }
+        }
+
+      /* Join on after the last host item that's part of this
+      processing if we haven't stopped sooner. */
+
+      if (h == last)
+        {
+        next->next = last->next;
+        last->next = next;
+        last = next;
+        }
+      }
+    }
+
+  NEXT_MX_RR: continue;
+  }
+
+/* If the list of hosts was obtained from SRV records, there are two things to
+do. First, if there is only one host, and it's name is ".", it means there is
+no SMTP service at this domain. Otherwise, we have to sort the hosts of equal
+priority according to their weights, using an algorithm that is defined in RFC
+2782. The hosts are currently sorted by priority and weight. For each priority
+group we have to pick off one host and put it first, and then repeat for any
+remaining in the same priority group. */
+
+if (ind_type == T_SRV)
+  {
+  host_item **pptr;
+
+  if (host == last && host->name[0] == 0)
+    {
+    DEBUG(D_host_lookup) debug_printf("the single SRV record is \".\"\n");
+    yield = HOST_FIND_FAILED;
+    goto out;
+    }
+
+  DEBUG(D_host_lookup)
+    {
+    debug_printf("original ordering of hosts from SRV records:\n");
+    for (h = host; h != last->next; h = h->next)
+      debug_printf("  %s P=%d W=%d\n", h->name, h->mx, h->sort_key % 1000);
+    }
+
+  for (pptr = &host, h = host; h != last; pptr = &(h->next), h = h->next)
+    {
+    int sum = 0;
+    host_item *hh;
+
+    /* Find the last following host that has the same precedence. At the same
+    time, compute the sum of the weights and the running totals. These can be
+    stored in the sort_key field. */
+
+    for (hh = h; hh != last; hh = hh->next)
+      {
+      int weight = hh->sort_key % 1000;   /* was precedence * 1000 + weight */
+      sum += weight;
+      hh->sort_key = sum;
+      if (hh->mx != hh->next->mx) break;
+      }
+
+    /* If there's more than one host at this precedence (priority), we need to
+    pick one to go first. */
+
+    if (hh != h)
+      {
+      host_item *hhh;
+      host_item **ppptr;
+      int randomizer = random_number(sum + 1);
+
+      for (ppptr = pptr, hhh = h;
+           hhh != hh;
+           ppptr = &(hhh->next), hhh = hhh->next)
+        {
+        if (hhh->sort_key >= randomizer) break;
+        }
+
+      /* hhh now points to the host that should go first; ppptr points to the
+      place that points to it. Unfortunately, if the start of the minilist is
+      the start of the entire list, we can't just swap the items over, because
+      we must not change the value of host, since it is passed in from outside.
+      One day, this could perhaps be changed.
+
+      The special case is fudged by putting the new item *second* in the chain,
+      and then transferring the data between the first and second items. We
+      can't just swap the first and the chosen item, because that would mean
+      that an item with zero weight might no longer be first. */
+
+      if (hhh != h)
+        {
+        *ppptr = hhh->next;          /* Cuts it out of the chain */
+
+        if (h == host)
+          {
+          host_item temp = *h;
+          *h = *hhh;
+          *hhh = temp;
+          hhh->next = temp.next;
+          h->next = hhh;
+          }
+
+        else
+          {
+          hhh->next = h;               /* The rest of the chain follows it */
+          *pptr = hhh;                 /* It takes the place of h */
+          h = hhh;                     /* It's now the start of this minilist */
+          }
+        }
+      }
+
+    /* A host has been chosen to be first at this priority and h now points
+    to this host. There may be others at the same priority, or others at a
+    different priority. Before we leave this host, we need to put back a sort
+    key of the traditional MX kind, in case this host is multihomed, because
+    the sort key is used for ordering the multiple IP addresses. We do not need
+    to ensure that these new sort keys actually reflect the order of the hosts,
+    however. */
+
+    h->sort_key = h->mx * 1000 + random_number(500);
+    }   /* Move on to the next host */
+  }
+
+/* Now we have to find IP addresses for all the hosts. We have ensured above
+that the names in all the host items are unique. Before release 4.61 we used to
+process records from the additional section in the DNS packet that returned the
+MX or SRV records. However, a DNS name server is free to drop any resource
+records from the additional section. In theory, this has always been a
+potential problem, but it is exacerbated by the advent of IPv6. If a host had
+several IPv4 addresses and some were not in the additional section, at least
+Exim would try the others. However, if a host had both IPv4 and IPv6 addresses
+and all the IPv4 (say) addresses were absent, Exim would try only for a IPv6
+connection, and never try an IPv4 address. When there was only IPv4
+connectivity, this was a disaster that did in practice occur.
+
+So, from release 4.61 onwards, we always search for A and AAAA records
+explicitly. The names shouldn't point to CNAMES, but we use the general lookup
+function that handles them, just in case. If any lookup gives a soft error,
+change the default yield.
+
+For these DNS lookups, we must disable qualify_single and search_parents;
+otherwise invalid host names obtained from MX or SRV records can cause trouble
+if they happen to match something local. */
+
+yield = HOST_FIND_FAILED;    /* Default yield */
+dns_init(FALSE, FALSE,       /* Disable qualify_single and search_parents */
+        dnssec_request || dnssec_require);
+
+for (h = host; h != last->next; h = h->next)
+  {
+  if (h->address != NULL) continue;  /* Inserted by a multihomed host */
+  rc = set_address_from_dns(h, &last, ignore_target_hosts, allow_mx_to_ip,
+    NULL, dnssec_request, dnssec_require);
+  if (rc != HOST_FOUND)
+    {
+    h->status = hstatus_unusable;
+    if (rc == HOST_FIND_AGAIN)
+      {
+      yield = rc;
+      h->why = hwhy_deferred;
+      }
+    else
+      h->why = (rc == HOST_IGNORED)? hwhy_ignored : hwhy_failed;
+    }
+  }
+
+/* Scan the list for any hosts that are marked unusable because they have
+been explicitly ignored, and remove them from the list, as if they did not
+exist. If we end up with just a single, ignored host, flatten its fields as if
+nothing was found. */
+
+if (ignore_target_hosts != NULL)
+  {
+  host_item *prev = NULL;
+  for (h = host; h != last->next; h = h->next)
+    {
+    REDO:
+    if (h->why != hwhy_ignored)        /* Non ignored host, just continue */
+      prev = h;
+    else if (prev == NULL)             /* First host is ignored */
+      {
+      if (h != last)                   /* First is not last */
+        {
+        if (h->next == last) last = h; /* Overwrite it with next */
+        *h = *(h->next);               /* and reprocess it. */
+        goto REDO;                     /* C should have redo, like Perl */
+        }
+      }
+    else                               /* Ignored host is not first - */
+      {                                /*   cut it out */
+      prev->next = h->next;
+      if (h == last) last = prev;
+      }
+    }
+
+  if (host->why == hwhy_ignored) host->address = NULL;
+  }
+
+/* There is still one complication in the case of IPv6. Although the code above
+arranges that IPv6 addresses take precedence over IPv4 addresses for multihomed
+hosts, it doesn't do this for addresses that apply to different hosts with the
+same MX precedence, because the sorting on MX precedence happens first. So we
+have to make another pass to check for this case. We ensure that, within a
+single MX preference value, IPv6 addresses come first. This can separate the
+addresses of a multihomed host, but that should not matter. */
+
+#if HAVE_IPV6
+if (h != last && !disable_ipv6)
+  {
+  for (h = host; h != last; h = h->next)
+    {
+    host_item temp;
+    host_item *next = h->next;
+    if (h->mx != next->mx ||                   /* If next is different MX */
+        h->address == NULL ||                  /* OR this one is unset */
+        Ustrchr(h->address, ':') != NULL ||    /* OR this one is IPv6 */
+        (next->address != NULL &&
+         Ustrchr(next->address, ':') == NULL)) /* OR next is IPv4 */
+      continue;                                /* move on to next */
+    temp = *h;                                 /* otherwise, swap */
+    temp.next = next->next;
+    *h = *next;
+    h->next = next;
+    *next = temp;
+    }
+  }
+#endif
+
+/* Remove any duplicate IP addresses and then scan the list of hosts for any
+whose IP addresses are on the local host. If any are found, all hosts with the
+same or higher MX values are removed. However, if the local host has the lowest
+numbered MX, then HOST_FOUND_LOCAL is returned. Otherwise, if at least one host
+with an IP address is on the list, HOST_FOUND is returned. Otherwise,
+HOST_FIND_FAILED is returned, but in this case do not update the yield, as it
+might have been set to HOST_FIND_AGAIN just above here. If not, it will already
+be HOST_FIND_FAILED. */
+
+host_remove_duplicates(host, &last);
+rc = host_scan_for_local_hosts(host, &last, removed);
+if (rc != HOST_FIND_FAILED) yield = rc;
+
+DEBUG(D_host_lookup)
+  {
+  if (fully_qualified_name != NULL)
+    debug_printf("fully qualified name = %s\n", *fully_qualified_name);
+  debug_printf("host_find_bydns yield = %s (%d); returned hosts:\n",
+    (yield == HOST_FOUND)? "HOST_FOUND" :
+    (yield == HOST_FOUND_LOCAL)? "HOST_FOUND_LOCAL" :
+    (yield == HOST_FIND_AGAIN)? "HOST_FIND_AGAIN" :
+    (yield == HOST_FIND_FAILED)? "HOST_FIND_FAILED" : "?",
+    yield);
+  for (h = host; h != last->next; h = h->next)
+    {
+    debug_printf("  %s %s MX=%d ", h->name,
+      (h->address == NULL)? US"<null>" : h->address, h->mx);
+    if (h->port != PORT_NONE) debug_printf("port=%d ", h->port);
+    if (h->status >= hstatus_unusable) debug_printf("*");
+    debug_printf("\n");
+    }
+  }
+
+out:
+
+dns_init(FALSE, FALSE, FALSE); /* clear the dnssec bit for getaddrbyname */
+return yield;
+}
+
+
+
+
+/*************************************************
+**************************************************
+*             Stand-alone test program           *
+**************************************************
+*************************************************/
+
+#ifdef STAND_ALONE
+
+int main(int argc, char **cargv)
+{
+host_item h;
+int whichrrs = HOST_FIND_BY_MX | HOST_FIND_BY_A;
+BOOL byname = FALSE;
+BOOL qualify_single = TRUE;
+BOOL search_parents = FALSE;
+BOOL request_dnssec = FALSE;
+BOOL require_dnssec = FALSE;
+uschar **argv = USS cargv;
+uschar buffer[256];
+
+disable_ipv6 = FALSE;
+primary_hostname = US"";
+store_pool = POOL_MAIN;
+debug_selector = D_host_lookup|D_interface;
+debug_file = stdout;
+debug_fd = fileno(debug_file);
+
+printf("Exim stand-alone host functions test\n");
+
+host_find_interfaces();
+debug_selector = D_host_lookup | D_dns;
+
+if (argc > 1) primary_hostname = argv[1];
+
+/* So that debug level changes can be done first */
+
+dns_init(qualify_single, search_parents, FALSE);
+
+printf("Testing host lookup\n");
+printf("> ");
+while (Ufgets(buffer, 256, stdin) != NULL)
+  {
+  int rc;
+  int len = Ustrlen(buffer);
+  uschar *fully_qualified_name;
+
+  while (len > 0 && isspace(buffer[len-1])) len--;
+  buffer[len] = 0;
+
+  if (Ustrcmp(buffer, "q") == 0) break;
+
+  if (Ustrcmp(buffer, "byname") == 0) byname = TRUE;
+  else if (Ustrcmp(buffer, "no_byname") == 0) byname = FALSE;
+  else if (Ustrcmp(buffer, "a_only") == 0) whichrrs = HOST_FIND_BY_A;
+  else if (Ustrcmp(buffer, "mx_only") == 0) whichrrs = HOST_FIND_BY_MX;
+  else if (Ustrcmp(buffer, "srv_only") == 0) whichrrs = HOST_FIND_BY_SRV;
+  else if (Ustrcmp(buffer, "srv+a") == 0)
+    whichrrs = HOST_FIND_BY_SRV | HOST_FIND_BY_A;
+  else if (Ustrcmp(buffer, "srv+mx") == 0)
+    whichrrs = HOST_FIND_BY_SRV | HOST_FIND_BY_MX;
+  else if (Ustrcmp(buffer, "srv+mx+a") == 0)
+    whichrrs = HOST_FIND_BY_SRV | HOST_FIND_BY_MX | HOST_FIND_BY_A;
+  else if (Ustrcmp(buffer, "qualify_single")    == 0) qualify_single = TRUE;
+  else if (Ustrcmp(buffer, "no_qualify_single") == 0) qualify_single = FALSE;
+  else if (Ustrcmp(buffer, "search_parents")    == 0) search_parents = TRUE;
+  else if (Ustrcmp(buffer, "no_search_parents") == 0) search_parents = FALSE;
+  else if (Ustrcmp(buffer, "request_dnssec")    == 0) request_dnssec = TRUE;
+  else if (Ustrcmp(buffer, "no_request_dnssec") == 0) request_dnssec = FALSE;
+  else if (Ustrcmp(buffer, "require_dnssec")    == 0) require_dnssec = TRUE;
+  else if (Ustrcmp(buffer, "no_reqiret_dnssec") == 0) require_dnssec = FALSE;
+  else if (Ustrcmp(buffer, "test_harness") == 0)
+    running_in_test_harness = !running_in_test_harness;
+  else if (Ustrcmp(buffer, "ipv6") == 0) disable_ipv6 = !disable_ipv6;
+  else if (Ustrcmp(buffer, "res_debug") == 0)
+    {
+    _res.options ^= RES_DEBUG;
+    }
+  else if (Ustrncmp(buffer, "retrans", 7) == 0)
+    {
+    (void)sscanf(CS(buffer+8), "%d", &dns_retrans);
+    _res.retrans = dns_retrans;
+    }
+  else if (Ustrncmp(buffer, "retry", 5) == 0)
+    {
+    (void)sscanf(CS(buffer+6), "%d", &dns_retry);
+    _res.retry = dns_retry;
+    }
+  else
+    {
+    int flags = whichrrs;
+
+    h.name = buffer;
+    h.next = NULL;
+    h.mx = MX_NONE;
+    h.port = PORT_NONE;
+    h.status = hstatus_unknown;
+    h.why = hwhy_unknown;
+    h.address = NULL;
+
+    if (qualify_single) flags |= HOST_FIND_QUALIFY_SINGLE;
+    if (search_parents) flags |= HOST_FIND_SEARCH_PARENTS;
+
+    rc = byname
+      ? host_find_byname(&h, NULL, flags, &fully_qualified_name, TRUE)
+      : host_find_bydns(&h, NULL, flags, US"smtp", NULL, NULL,
+                       request_dnssec ? &h.name : NULL,
+                       require_dnssec ? &h.name : NULL,
+                       &fully_qualified_name, NULL);
+
+    if (rc == HOST_FIND_FAILED) printf("Failed\n");
+      else if (rc == HOST_FIND_AGAIN) printf("Again\n");
+        else if (rc == HOST_FOUND_LOCAL) printf("Local\n");
+    }
+
+  printf("\n> ");
+  }
+
+printf("Testing host_aton\n");
+printf("> ");
+while (Ufgets(buffer, 256, stdin) != NULL)
+  {
+  int i;
+  int x[4];
+  int len = Ustrlen(buffer);
+
+  while (len > 0 && isspace(buffer[len-1])) len--;
+  buffer[len] = 0;
+
+  if (Ustrcmp(buffer, "q") == 0) break;
+
+  len = host_aton(buffer, x);
+  printf("length = %d ", len);
+  for (i = 0; i < len; i++)
+    {
+    printf("%04x ", (x[i] >> 16) & 0xffff);
+    printf("%04x ", x[i] & 0xffff);
+    }
+  printf("\n> ");
+  }
+
+printf("\n");
+
+printf("Testing host_name_lookup\n");
+printf("> ");
+while (Ufgets(buffer, 256, stdin) != NULL)
+  {
+  int len = Ustrlen(buffer);
+  while (len > 0 && isspace(buffer[len-1])) len--;
+  buffer[len] = 0;
+  if (Ustrcmp(buffer, "q") == 0) break;
+  sender_host_address = buffer;
+  sender_host_name = NULL;
+  sender_host_aliases = NULL;
+  host_lookup_msg = US"";
+  host_lookup_failed = FALSE;
+  if (host_name_lookup() == FAIL)  /* Debug causes printing */
+    printf("Lookup failed:%s\n", host_lookup_msg);
+  printf("\n> ");
+  }
+
+printf("\n");
+
+return 0;
+}
+#endif  /* STAND_ALONE */
+
+/* vi: aw ai sw=2
+*/
+/* End of host.c */
diff --git a/src/ip.c b/src/ip.c
new file mode 100644 (file)
index 0000000..b492b9d
--- /dev/null
+++ b/src/ip.c
@@ -0,0 +1,648 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2014 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Functions for doing things with sockets. With the advent of IPv6 this has
+got messier, so that it's worth pulling out the code into separate functions
+that other parts of Exim can call, expecially as there are now several
+different places in the code where sockets are used. */
+
+
+#include "exim.h"
+
+
+/*************************************************
+*             Create a socket                    *
+*************************************************/
+
+/* Socket creation happens in a number of places so it's packaged here for
+convenience.
+
+Arguments:
+  type       SOCK_DGRAM or SOCK_STREAM
+  af         AF_INET or AF_INET6
+
+Returns:     socket number or -1 on failure
+*/
+
+int
+ip_socket(int type, int af)
+{
+int sock = socket(af, type, 0);
+if (sock < 0)
+  log_write(0, LOG_MAIN, "IPv%c socket creation failed: %s",
+    (af == AF_INET6)? '6':'4', strerror(errno));
+return sock;
+}
+
+
+
+
+#if HAVE_IPV6
+/*************************************************
+*      Convert printing address to numeric       *
+*************************************************/
+
+/* This function converts the textual form of an IP address into a numeric form
+in an appropriate structure in an IPv6 environment. The getaddrinfo() function
+can (apparently) handle more complicated addresses (e.g. those containing
+scopes) than inet_pton() in some environments. We use hints to tell it that the
+input must be a numeric address.
+
+However, apparently some operating systems (or libraries) don't support
+getaddrinfo(), so there is a build-time option to revert to inet_pton() (which
+does not support scopes).
+
+Arguments:
+  address     textual form of the address
+  addr        where to copy back the answer
+
+Returns:      nothing - failure provokes a panic-die
+*/
+
+static void
+ip_addrinfo(uschar *address, struct sockaddr_in6 *saddr)
+{
+#ifdef IPV6_USE_INET_PTON
+
+  if (inet_pton(AF_INET6, CS address, &saddr->sin6_addr) != 1)
+    log_write(0, LOG_MAIN|LOG_PANIC_DIE, "unable to parse \"%s\" as an "
+      "IP address", address);
+  saddr->sin6_family = AF_INET6;
+
+#else
+
+  int rc;
+  struct addrinfo hints, *res;
+  memset(&hints, 0, sizeof(hints));
+  hints.ai_family = AF_INET6;
+  hints.ai_socktype = SOCK_STREAM;
+  hints.ai_flags = AI_NUMERICHOST;
+  if ((rc = getaddrinfo(CS address, NULL, &hints, &res)) != 0 || res == NULL)
+    log_write(0, LOG_MAIN|LOG_PANIC_DIE, "unable to parse \"%s\" as an "
+      "IP address: %s", address,
+      (rc == 0)? "NULL result returned" : gai_strerror(rc));
+  memcpy(saddr, res->ai_addr, res->ai_addrlen);
+  freeaddrinfo(res);
+
+#endif
+}
+#endif  /* HAVE_IPV6 */
+
+
+/*************************************************
+*         Bind socket to interface and port      *
+*************************************************/
+
+/* This function binds a socket to a local interface address and port. For a
+wildcard IPv6 bind, the address is ":".
+
+Arguments:
+  sock           the socket
+  af             AF_INET or AF_INET6 - the socket type
+  address        the IP address, in text form
+  port           the IP port (host order)
+
+Returns:         the result of bind()
+*/
+
+int
+ip_bind(int sock, int af, uschar *address, int port)
+{
+int s_len;
+union sockaddr_46 sin;
+memset(&sin, 0, sizeof(sin));
+
+/* Setup code when using an IPv6 socket. The wildcard address is ":", to
+ensure an IPv6 socket is used. */
+
+#if HAVE_IPV6
+if (af == AF_INET6)
+  {
+  if (address[0] == ':' && address[1] == 0)
+    {
+    sin.v6.sin6_family = AF_INET6;
+    sin.v6.sin6_addr = in6addr_any;
+    }
+  else
+    {
+    ip_addrinfo(address, &sin.v6);  /* Panic-dies on error */
+    }
+  sin.v6.sin6_port = htons(port);
+  s_len = sizeof(sin.v6);
+  }
+else
+#else     /* HAVE_IPv6 */
+af = af;  /* Avoid compiler warning */
+#endif    /* HAVE_IPV6 */
+
+/* Setup code when using IPv4 socket. The wildcard address is "". */
+
+  {
+  sin.v4.sin_family = AF_INET;
+  sin.v4.sin_port = htons(port);
+  s_len = sizeof(sin.v4);
+  if (address[0] == 0)
+    sin.v4.sin_addr.s_addr = (S_ADDR_TYPE)INADDR_ANY;
+  else
+    sin.v4.sin_addr.s_addr = (S_ADDR_TYPE)inet_addr(CS address);
+  }
+
+/* Now we can call the bind() function */
+
+return bind(sock, (struct sockaddr *)&sin, s_len);
+}
+
+
+
+/*************************************************
+*        Connect socket to remote host           *
+*************************************************/
+
+/* This function connects a socket to a remote address and port. The socket may
+or may not have previously been bound to a local interface. The socket is not
+closed, even in cases of error. It is expected that the calling function, which
+created the socket, will be the one that closes it.
+
+Arguments:
+  sock        the socket
+  af          AF_INET6 or AF_INET for the socket type
+  address     the remote address, in text form
+  port        the remote port
+  timeout     a timeout (zero for indefinite timeout)
+
+Returns:      0 on success; -1 on failure, with errno set
+*/
+
+int
+ip_connect(int sock, int af, uschar *address, int port, int timeout)
+{
+struct sockaddr_in s_in4;
+struct sockaddr *s_ptr;
+int s_len, rc, save_errno;
+
+/* For an IPv6 address, use an IPv6 sockaddr structure. */
+
+#if HAVE_IPV6
+struct sockaddr_in6 s_in6;
+if (af == AF_INET6)
+  {
+  memset(&s_in6, 0, sizeof(s_in6));
+  ip_addrinfo(address, &s_in6);   /* Panic-dies on error */
+  s_in6.sin6_port = htons(port);
+  s_ptr = (struct sockaddr *)&s_in6;
+  s_len = sizeof(s_in6);
+  }
+else
+#else     /* HAVE_IPV6 */
+af = af;  /* Avoid compiler warning */
+#endif    /* HAVE_IPV6 */
+
+/* For an IPv4 address, use an IPv4 sockaddr structure, even on a system with
+IPv6 support. */
+
+  {
+  memset(&s_in4, 0, sizeof(s_in4));
+  s_in4.sin_family = AF_INET;
+  s_in4.sin_port = htons(port);
+  s_in4.sin_addr.s_addr = (S_ADDR_TYPE)inet_addr(CS address);
+  s_ptr = (struct sockaddr *)&s_in4;
+  s_len = sizeof(s_in4);
+  }
+
+/* If no connection timeout is set, just call connect() without setting a
+timer, thereby allowing the inbuilt OS timeout to operate. */
+
+sigalrm_seen = FALSE;
+if (timeout > 0) alarm(timeout);
+rc = connect(sock, s_ptr, s_len);
+save_errno = errno;
+alarm(0);
+
+/* There is a testing facility for simulating a connection timeout, as I
+can't think of any other way of doing this. It converts a connection refused
+into a timeout if the timeout is set to 999999. */
+
+if (running_in_test_harness)
+  {
+  if (save_errno == ECONNREFUSED && timeout == 999999)
+    {
+    rc = -1;
+    save_errno = EINTR;
+    sigalrm_seen = TRUE;
+    }
+  }
+
+/* Success */
+
+if (rc >= 0) return 0;
+
+/* A failure whose error code is "Interrupted system call" is in fact
+an externally applied timeout if the signal handler has been run. */
+
+errno = (save_errno == EINTR && sigalrm_seen)? ETIMEDOUT : save_errno;
+return -1;
+}
+
+
+
+/*************************************************
+*    Create connected socket to remote host      *
+*************************************************/
+
+/* Create a socket and connect to host (name or number, ipv6 ok)
+   at one of port-range.
+
+Arguments:
+  type          SOCK_DGRAM or SOCK_STREAM
+  af            AF_INET6 or AF_INET for the socket type
+  address       the remote address, in text form
+  portlo,porthi the remote port range
+  timeout       a timeout
+  connhost     if not NULL, host_item filled in with connection details
+  errstr        pointer for allocated string on error
+
+Return:
+  socket fd, or -1 on failure (having allocated an error string)
+*/
+int
+ip_connectedsocket(int type, const uschar * hostname, int portlo, int porthi,
+       int timeout, host_item * connhost, uschar ** errstr)
+{
+int namelen, port;
+host_item shost;
+host_item *h;
+int af = 0, fd, fd4 = -1, fd6 = -1;
+
+shost.next = NULL;
+shost.address = NULL;
+shost.port = portlo;
+shost.mx = -1;
+
+namelen = Ustrlen(hostname);
+
+/* Anything enclosed in [] must be an IP address. */
+
+if (hostname[0] == '[' &&
+    hostname[namelen - 1] == ']')
+  {
+  uschar * host = string_copy(hostname);
+  host[namelen - 1] = 0;
+  host++;
+  if (string_is_ip_address(host, NULL) == 0)
+    {
+    *errstr = string_sprintf("malformed IP address \"%s\"", hostname);
+    return -1;
+    }
+  shost.name = shost.address = host;
+  }
+
+/* Otherwise check for an unadorned IP address */
+
+else if (string_is_ip_address(hostname, NULL) != 0)
+  shost.name = shost.address = string_copy(hostname);
+
+/* Otherwise lookup IP address(es) from the name */
+
+else
+  {
+  shost.name = string_copy(hostname);
+  if (host_find_byname(&shost, NULL, HOST_FIND_QUALIFY_SINGLE, NULL,
+      FALSE) != HOST_FOUND)
+    {
+    *errstr = string_sprintf("no IP address found for host %s", shost.name);
+    return -1;
+    }
+  }
+
+/* Try to connect to the server - test each IP till one works */
+
+for (h = &shost; h != NULL; h = h->next)
+  {
+  fd = (Ustrchr(h->address, ':') != 0)
+    ? (fd6 < 0) ? (fd6 = ip_socket(type, af = AF_INET6)) : fd6
+    : (fd4 < 0) ? (fd4 = ip_socket(type, af = AF_INET )) : fd4;
+
+  if (fd < 0)
+    {
+    *errstr = string_sprintf("failed to create socket: %s", strerror(errno));
+    goto bad;
+    }
+
+  for(port = portlo; port <= porthi; port++)
+    if (ip_connect(fd, af, h->address, port, timeout) == 0)
+      {
+      if (fd != fd6) close(fd6);
+      if (fd != fd4) close(fd4);
+      if (connhost) {
+       h->port = port;
+       *connhost = *h;
+       connhost->next = NULL;
+       }
+      return fd;
+      }
+  }
+
+*errstr = string_sprintf("failed to connect to %s: "
+  "couldn't connect to any host: %s", hostname, strerror(errno));
+
+bad:
+  close(fd4); close(fd6); return -1;
+}
+
+
+/*************************************************
+*         Set keepalive on a socket              *
+*************************************************/
+
+/* Can be called for both incoming and outgoing sockets.
+
+Arguments:
+  sock       the socket
+  address    the remote host address, for failure logging
+  torf       true for outgoing connection, false for incoming
+
+Returns:     nothing
+*/
+
+void
+ip_keepalive(int sock, uschar *address, BOOL torf)
+{
+int fodder = 1;
+if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
+    (uschar *)(&fodder), sizeof(fodder)) != 0)
+  log_write(0, LOG_MAIN, "setsockopt(SO_KEEPALIVE) on connection %s %s "
+    "failed: %s", torf? "to":"from", address, strerror(errno));
+}
+
+
+
+/*************************************************
+*         Receive from a socket with timeout     *
+*************************************************/
+
+/* The timeout is implemented using select(), and we loop to cover select()
+getting interrupted, and the possibility of select() returning with a positive
+result but no ready descriptor. Is this in fact possible?
+
+Arguments:
+  sock        the socket
+  buffer      to read into
+  bufsize     the buffer size
+  timeout     the timeout
+
+Returns:      > 0 => that much data read
+              <= 0 on error or EOF; errno set - zero for EOF
+*/
+
+int
+ip_recv(int sock, uschar *buffer, int buffsize, int timeout)
+{
+fd_set select_inset;
+struct timeval tv;
+time_t start_recv = time(NULL);
+int rc;
+
+/* Wait until the socket is ready */
+
+for (;;)
+  {
+  FD_ZERO (&select_inset);
+  FD_SET (sock, &select_inset);
+  tv.tv_sec = timeout;
+  tv.tv_usec = 0;
+
+  DEBUG(D_transport) debug_printf("waiting for data on socket\n");
+  rc = select(sock + 1, (SELECT_ARG2_TYPE *)&select_inset, NULL, NULL, &tv);
+
+  /* If some interrupt arrived, just retry. We presume this to be rare,
+  but it can happen (e.g. the SIGUSR1 signal sent by exiwhat causes
+  select() to exit).
+
+  Aug 2004: Somebody set up a cron job that ran exiwhat every 2 minutes, making
+  the interrupt not at all rare. Since the timeout is typically more than 2
+  minutes, the effect was to block the timeout completely. To prevent this
+  happening again, we do an explicit time test. */
+
+  if (rc < 0 && errno == EINTR)
+    {
+    DEBUG(D_transport) debug_printf("EINTR while waiting for socket data\n");
+    if (time(NULL) - start_recv < timeout) continue;
+    DEBUG(D_transport) debug_printf("total wait time exceeds timeout\n");
+    }
+
+  /* Handle a timeout, and treat any other select error as a timeout, including
+  an EINTR when we have been in this loop for longer than timeout. */
+
+  if (rc <= 0)
+    {
+    errno = ETIMEDOUT;
+    return -1;
+    }
+
+  /* If the socket is ready, break out of the loop. */
+
+  if (FD_ISSET(sock, &select_inset)) break;
+  }
+
+/* The socket is ready, read from it (via TLS if it's active). On EOF (i.e.
+close down of the connection), set errno to zero; otherwise leave it alone. */
+
+#ifdef SUPPORT_TLS
+if (tls_out.active == sock)
+  rc = tls_read(FALSE, buffer, buffsize);
+else if (tls_in.active == sock)
+  rc = tls_read(TRUE, buffer, buffsize);
+else
+#endif
+  rc = recv(sock, buffer, buffsize, 0);
+
+if (rc > 0) return rc;
+if (rc == 0) errno = 0;
+return -1;
+}
+
+
+
+
+/*************************************************
+*    Lookup address family of potential socket   *
+*************************************************/
+
+/* Given a file-descriptor, check to see if it's a socket and, if so,
+return the address family; detects IPv4 vs IPv6.  If not a socket then
+return -1.
+
+The value 0 is typically AF_UNSPEC, which should not be seen on a connected
+fd.  If the return is -1, the errno will be from getsockname(); probably
+ENOTSOCK or ECONNRESET.
+
+Arguments:     socket-or-not fd
+Returns:       address family or -1
+*/
+
+int
+ip_get_address_family(int fd)
+{
+struct sockaddr_storage ss;
+socklen_t sslen = sizeof(ss);
+
+if (getsockname(fd, (struct sockaddr *) &ss, &sslen) < 0)
+  return -1;
+
+return (int) ss.ss_family;
+}
+
+
+
+
+/*************************************************
+*       Lookup DSCP settings for a socket        *
+*************************************************/
+
+struct dscp_name_tableentry {
+  const uschar *name;
+  int value;
+};
+/* Keep both of these tables sorted! */
+static struct dscp_name_tableentry dscp_table[] = {
+#ifdef IPTOS_DSCP_AF11
+    { CUS"af11", IPTOS_DSCP_AF11 },
+    { CUS"af12", IPTOS_DSCP_AF12 },
+    { CUS"af13", IPTOS_DSCP_AF13 },
+    { CUS"af21", IPTOS_DSCP_AF21 },
+    { CUS"af22", IPTOS_DSCP_AF22 },
+    { CUS"af23", IPTOS_DSCP_AF23 },
+    { CUS"af31", IPTOS_DSCP_AF31 },
+    { CUS"af32", IPTOS_DSCP_AF32 },
+    { CUS"af33", IPTOS_DSCP_AF33 },
+    { CUS"af41", IPTOS_DSCP_AF41 },
+    { CUS"af42", IPTOS_DSCP_AF42 },
+    { CUS"af43", IPTOS_DSCP_AF43 },
+    { CUS"ef", IPTOS_DSCP_EF },
+#endif
+#ifdef IPTOS_LOWCOST
+    { CUS"lowcost", IPTOS_LOWCOST },
+#endif
+    { CUS"lowdelay", IPTOS_LOWDELAY },
+#ifdef IPTOS_MINCOST
+    { CUS"mincost", IPTOS_MINCOST },
+#endif
+    { CUS"reliability", IPTOS_RELIABILITY },
+    { CUS"throughput", IPTOS_THROUGHPUT }
+};
+static int dscp_table_size =
+  sizeof(dscp_table) / sizeof(struct dscp_name_tableentry);
+
+/* DSCP values change by protocol family, and so do the options used for
+setsockopt(); this utility does all the lookups.  It takes an unexpanded
+option string, expands it, strips off affix whitespace, then checks if it's
+a number.  If all of what's left is a number, then that's how the option will
+be parsed and success/failure is a range check.  If it's not all a number,
+then it must be a supported keyword.
+
+Arguments:
+  dscp_name   a string, so far unvalidated
+  af          address_family in use
+  level       setsockopt level to use
+  optname     setsockopt name to use
+  dscp_value  value for dscp_name
+
+Returns: TRUE if okay to setsockopt(), else FALSE
+
+*level and *optname may be set even if FALSE is returned
+*/
+
+BOOL
+dscp_lookup(const uschar *dscp_name, int af,
+    int *level, int *optname, int *dscp_value)
+{
+uschar *dscp_lookup, *p;
+int first, last;
+long rawlong;
+
+if (af == AF_INET)
+  {
+  *level = IPPROTO_IP;
+  *optname = IP_TOS;
+  }
+#if HAVE_IPV6 && defined(IPV6_TCLASS)
+else if (af == AF_INET6)
+  {
+  *level = IPPROTO_IPV6;
+  *optname = IPV6_TCLASS;
+  }
+#endif
+else
+  {
+  DEBUG(D_transport)
+    debug_printf("Unhandled address family %d in dscp_lookup()\n", af);
+  return FALSE;
+  }
+if (!dscp_name)
+  {
+  DEBUG(D_transport)
+    debug_printf("[empty DSCP]\n");
+  return FALSE;
+  }
+dscp_lookup = expand_string(US dscp_name);
+if (dscp_lookup == NULL || *dscp_lookup == '\0')
+  return FALSE;
+
+p = dscp_lookup + Ustrlen(dscp_lookup) - 1;
+while (isspace(*p)) *p-- = '\0';
+while (isspace(*dscp_lookup) && dscp_lookup < p) dscp_lookup++;
+if (*dscp_lookup == '\0')
+  return FALSE;
+
+rawlong = Ustrtol(dscp_lookup, &p, 0);
+if (p != dscp_lookup && *p == '\0')
+  {
+  /* We have six bits available, which will end up shifted to fit in 0xFC mask.
+  RFC 2597 defines the values unshifted. */
+  if (rawlong < 0 || rawlong > 0x3F)
+    {
+    DEBUG(D_transport)
+      debug_printf("DSCP value %ld out of range, ignored.\n", rawlong);
+    return FALSE;
+    }
+  *dscp_value = rawlong << 2;
+  return TRUE;
+  }
+
+first = 0;
+last = dscp_table_size;
+while (last > first)
+  {
+  int middle = (first + last)/2;
+  int c = Ustrcmp(dscp_lookup, dscp_table[middle].name);
+  if (c == 0)
+    {
+    *dscp_value = dscp_table[middle].value;
+    return TRUE;
+    }
+  else if (c > 0)
+    {
+    first = middle + 1;
+    }
+  else
+    {
+    last = middle;
+    }
+  }
+return FALSE;
+}
+
+void
+dscp_list_to_stream(FILE *stream)
+{
+int i;
+for (i=0; i < dscp_table_size; ++i)
+  fprintf(stream, "%s\n", dscp_table[i].name);
+}
+
+
+/* End of ip.c */
diff --git a/src/local_scan.c b/src/local_scan.c
new file mode 100644 (file)
index 0000000..3500047
--- /dev/null
@@ -0,0 +1,64 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+
+/******************************************************************************
+This file contains a template local_scan() function that just returns ACCEPT.
+If you want to implement your own version, you should copy this file to, say
+Local/local_scan.c, and edit the copy. To use your version instead of the
+default, you must set
+
+LOCAL_SCAN_SOURCE=Local/local_scan.c
+
+in your Local/Makefile. This makes it easy to copy your version for use with
+subsequent Exim releases.
+
+For a full description of the API to this function, see the Exim specification.
+******************************************************************************/
+
+
+/* This is the only Exim header that you should include. The effect of
+including any other Exim header is not defined, and may change from release to
+release. Use only the documented interface! */
+
+#include "local_scan.h"
+
+
+/* This is a "do-nothing" version of a local_scan() function. The arguments
+are:
+
+  fd             The file descriptor of the open -D file, which contains the
+                   body of the message. The file is open for reading and
+                   writing, but modifying it is dangerous and not recommended.
+
+  return_text    A pointer to an unsigned char* variable which you can set in
+                   order to return a text string. It is initialized to NULL.
+
+The return values of this function are:
+
+  LOCAL_SCAN_ACCEPT
+                 The message is to be accepted. The return_text argument is
+                   saved in $local_scan_data.
+
+  LOCAL_SCAN_REJECT
+                 The message is to be rejected. The returned text is used
+                   in the rejection message.
+
+  LOCAL_SCAN_TEMPREJECT
+                 This specifies a temporary rejection. The returned text
+                   is used in the rejection message.
+*/
+
+int
+local_scan(int fd, uschar **return_text)
+{
+fd = fd;                      /* Keep picky compilers happy */
+return_text = return_text;
+return LOCAL_SCAN_ACCEPT;
+}
+
+/* End of local_scan.c */
diff --git a/src/local_scan.h b/src/local_scan.h
new file mode 100644 (file)
index 0000000..770348a
--- /dev/null
@@ -0,0 +1,197 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2012 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* This file is the header that is the only Exim header to be included in the
+source for the local_scan.c() function. It contains definitions that are made
+available for use in that function, and which are documented.
+
+This API is also used for functions called by the ${dlfunc expansion item. */
+
+
+/* Some basic types that make some things easier, the Exim configuration
+settings, and the store functions. */
+
+#include <stdarg.h>
+#include <sys/types.h>
+#include "config.h"
+#include "mytypes.h"
+#include "store.h"
+
+
+/* The function and its return codes. */
+
+extern int local_scan(int, uschar **);
+
+enum {
+  LOCAL_SCAN_ACCEPT,              /* Accept */
+  LOCAL_SCAN_ACCEPT_FREEZE,       /* Accept, but freeze */
+  LOCAL_SCAN_ACCEPT_QUEUE,        /* Accept, but no immediate delivery */
+  LOCAL_SCAN_REJECT,              /* Permanent rejection */
+  LOCAL_SCAN_REJECT_NOLOGHDR,     /* Permanent rejection, no log header */
+  LOCAL_SCAN_TEMPREJECT,          /* Temporary rejection */
+  LOCAL_SCAN_TEMPREJECT_NOLOGHDR  /* Temporary rejection, no log header */
+};
+
+
+/* Functions called by ${dlfunc{file}{func}{arg}...} return one of the five
+status codes defined immediately below. The function's first argument is either
+the result of expansion, or the error message in case of failure. The second
+and third arguments are standard argument count and vector, comprising the
+{arg} values specified in the expansion item. */
+
+typedef int exim_dlfunc_t(uschar **yield, int argc, uschar *argv[]);
+
+
+/* Return codes from the support functions lss_match_xxx(). These are also the
+codes that dynamically-loaded ${dlfunc functions must return. */
+
+#define  OK            0          /* Successful match */
+#define  DEFER         1          /* Defer - some problem */
+#define  FAIL          2          /* Matching failed */
+#define  ERROR         3          /* Internal or config error */
+
+/* Extra return code for ${dlfunc functions */
+
+#define  FAIL_FORCED   4          /* "Forced" failure */
+
+
+/* Available logging destinations */
+
+#define LOG_MAIN        1    /* Write to the main log */
+#define LOG_PANIC       2    /* Write to the panic log */
+#define LOG_REJECT     16    /* Write to the reject log, with headers */
+
+
+/* Accessible debugging bits */
+
+#define D_v                          0x00000001
+#define D_local_scan                 0x00000002
+
+
+/* Option types that can be used for local_scan_options. The boolean ones
+MUST be last so that they are contiguous with the internal boolean specials. */
+
+enum { opt_stringptr, opt_int, opt_octint, opt_mkint, opt_Kint, opt_fixed,
+  opt_time, opt_bool };
+
+
+/* The length of message identification strings. This is the id used internally
+by exim. The external version for use in Received: strings has a leading 'E'
+added to ensure it starts with a letter. */
+
+#define MESSAGE_ID_LENGTH 16
+
+/* The offset to the start of the data in the data file - this allows for
+the name of the data file to be present in the first line. */
+
+#define SPOOL_DATA_START_OFFSET (MESSAGE_ID_LENGTH+3)
+
+/* Some people (Marc Merlin et al) are maintaining a patch that allows for
+dynamic local_scan() libraries. This code is not yet in Exim proper, but it
+helps the maintainers if we keep their ABI version numbers here. This may
+mutate into more general support later. The major number is increased when the
+ABI is changed in a non backward compatible way. The minor number is increased
+each time a new feature is added (in a way that doesn't break backward
+compatibility). */
+
+#define LOCAL_SCAN_ABI_VERSION_MAJOR 1
+#define LOCAL_SCAN_ABI_VERSION_MINOR 1
+#define LOCAL_SCAN_ABI_VERSION \
+  LOCAL_SCAN_ABI_VERSION_MAJOR.LOCAL_SCAN_ABI_VERSION_MINOR
+
+/* Structure definitions that are documented as visible in the function. */
+
+typedef struct header_line {
+  struct header_line *next;
+  int    type;
+  int    slen;
+  uschar *text;
+} header_line;
+
+/* Entries in lists options are in this form. */
+
+typedef struct {
+  const char   *name;
+  int           type;
+  void         *value;
+} optionlist;
+
+/*Structure for holding information about an envelope address. The errors_to
+field is always NULL except for one_time aliases that had errors_to on the
+routers that generated them. */
+
+typedef struct recipient_item {
+  uschar *address;              /* the recipient address */
+  int     pno;                  /* parent number for "one_time" alias, or -1 */
+  uschar *errors_to;            /* the errors_to address or NULL */
+#ifdef EXPERIMENTAL_DSN
+  uschar *orcpt;                /* DSN orcpt */
+  int     dsn_flags;            /* DSN flags */
+#endif
+#ifdef EXPERIMENTAL_BRIGHTMAIL
+  uschar *bmi_optin;
+#endif
+} recipient_item;
+
+
+/* Global variables that are documented as visible in the function. */
+
+extern unsigned int debug_selector;    /* Debugging bits */
+
+extern int     body_linecount;         /* Line count in body */
+extern int     body_zerocount;         /* Binary zero count in body */
+extern uschar *expand_string_message;  /* Error info for failing expansion */
+extern uschar *headers_charset;        /* Charset for RFC 2047 decoding */
+extern header_line *header_last;       /* Final header */
+extern header_line *header_list;       /* First header */
+extern BOOL    host_checking;          /* Set when checking a host */
+extern uschar *interface_address;      /* Interface for incoming call */
+extern int     interface_port;         /* Port number for incoming call */
+extern uschar *message_id;             /* Internal id of message being handled */
+extern uschar *received_protocol;      /* Name of incoming protocol */
+extern int     recipients_count;       /* Number of recipients */
+extern recipient_item *recipients_list;/* List of recipient addresses */
+extern unsigned char *sender_address;  /* Sender address */
+extern uschar *sender_host_address;    /* IP address of sender, as chars */
+extern uschar *sender_host_authenticated; /* Name of authentication mechanism */
+extern uschar *sender_host_name;       /* Host name from lookup */
+extern int     sender_host_port;       /* Port number of sender */
+extern BOOL    smtp_batched_input;     /* TRUE if SMTP batch (no interaction) */
+extern BOOL    smtp_input;             /* TRUE if input is via SMTP */
+
+
+/* Functions that are documented as visible in local_scan(). */
+
+extern int     child_close(pid_t, int);
+extern pid_t   child_open(uschar **, uschar **, int, int *, int *, BOOL);
+extern pid_t   child_open_exim(int *);
+extern pid_t   child_open_exim2(int *, uschar *, uschar *);
+extern void    debug_printf(const char *, ...) PRINTF_FUNCTION(1,2);
+extern uschar *expand_string(uschar *);
+extern void    header_add(int, const char *, ...);
+extern void    header_add_at_position(BOOL, uschar *, BOOL, int, const char *, ...);
+extern void    header_remove(int, const uschar *);
+extern BOOL    header_testname(header_line *, const uschar *, int, BOOL);
+extern BOOL    header_testname_incomplete(header_line *, const uschar *, int, BOOL);
+extern void    log_write(unsigned int, int, const char *format, ...) PRINTF_FUNCTION(3,4);
+extern int     lss_b64decode(uschar *, uschar **);
+extern uschar *lss_b64encode(uschar *, int);
+extern int     lss_match_domain(uschar *, uschar *);
+extern int     lss_match_local_part(uschar *, uschar *, BOOL);
+extern int     lss_match_address(uschar *, uschar *, BOOL);
+extern int     lss_match_host(uschar *, uschar *, uschar *);
+extern void    receive_add_recipient(uschar *, int);
+extern BOOL    receive_remove_recipient(uschar *);
+extern uschar *rfc2047_decode(uschar *, BOOL, uschar *, int, int *, uschar **);
+extern int     smtp_fflush(void);
+extern void    smtp_printf(const char *, ...) PRINTF_FUNCTION(1,2);
+extern void    smtp_vprintf(const char *, va_list);
+extern uschar *string_copy(const uschar *);
+extern uschar *string_copyn(uschar *, int);
+extern uschar *string_sprintf(const char *, ...) ALMOST_PRINTF(1,2);
+
+/* End of local_scan.h */
diff --git a/src/log.c b/src/log.c
new file mode 100644 (file)
index 0000000..c80c347
--- /dev/null
+++ b/src/log.c
@@ -0,0 +1,1349 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2014 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Functions for writing log files. The code for maintaining datestamped
+log files was originally contributed by Tony Sheen. */
+
+
+#include "exim.h"
+
+#define LOG_NAME_SIZE 256
+#define MAX_SYSLOG_LEN 870
+
+#define LOG_MODE_FILE   1
+#define LOG_MODE_SYSLOG 2
+
+enum { lt_main, lt_reject, lt_panic, lt_debug };
+
+static uschar *log_names[] = { US"main", US"reject", US"panic", US"debug" };
+
+
+
+/*************************************************
+*           Local static variables               *
+*************************************************/
+
+static uschar mainlog_name[LOG_NAME_SIZE];
+static uschar rejectlog_name[LOG_NAME_SIZE];
+static uschar debuglog_name[LOG_NAME_SIZE];
+
+static uschar *mainlog_datestamp = NULL;
+static uschar *rejectlog_datestamp = NULL;
+
+static int    mainlogfd = -1;
+static int    rejectlogfd = -1;
+static ino_t  mainlog_inode = 0;
+static ino_t  rejectlog_inode = 0;
+
+static uschar *panic_save_buffer = NULL;
+static BOOL   panic_recurseflag = FALSE;
+
+static BOOL   syslog_open = FALSE;
+static BOOL   path_inspected = FALSE;
+static int    logging_mode = LOG_MODE_FILE;
+static uschar *file_path = US"";
+
+
+
+
+/*************************************************
+*              Write to syslog                   *
+*************************************************/
+
+/* The given string is split into sections according to length, or at embedded
+newlines, and syslogged as a numbered sequence if it is overlong or if there is
+more than one line. However, if we are running in the test harness, do not do
+anything. (The test harness doesn't use syslog - for obvious reasons - but we
+can get here if there is a failure to open the panic log.)
+
+Arguments:
+  priority       syslog priority
+  s              the string to be written
+
+Returns:         nothing
+*/
+
+static void
+write_syslog(int priority, uschar *s)
+{
+int len, pass;
+int linecount = 0;
+
+if (running_in_test_harness) return;
+
+if (!syslog_timestamp) s += log_timezone? 26 : 20;
+
+len = Ustrlen(s);
+
+#ifndef NO_OPENLOG
+if (!syslog_open)
+  {
+  #ifdef SYSLOG_LOG_PID
+  openlog(CS syslog_processname, LOG_PID|LOG_CONS, syslog_facility);
+  #else
+  openlog(CS syslog_processname, LOG_CONS, syslog_facility);
+  #endif
+  syslog_open = TRUE;
+  }
+#endif
+
+/* First do a scan through the message in order to determine how many lines
+it is going to end up as. Then rescan to output it. */
+
+for (pass = 0; pass < 2; pass++)
+  {
+  int i;
+  int tlen;
+  uschar *ss = s;
+  for (i = 1, tlen = len; tlen > 0; i++)
+    {
+    int plen = tlen;
+    uschar *nlptr = Ustrchr(ss, '\n');
+    if (nlptr != NULL) plen = nlptr - ss;
+    #ifndef SYSLOG_LONG_LINES
+    if (plen > MAX_SYSLOG_LEN) plen = MAX_SYSLOG_LEN;
+    #endif
+    tlen -= plen;
+    if (ss[plen] == '\n') tlen--;    /* chars left */
+
+    if (pass == 0) linecount++; else
+      {
+      if (linecount == 1)
+        syslog(priority, "%.*s", plen, ss);
+      else
+        syslog(priority, "[%d%c%d] %.*s", i,
+          (ss[plen] == '\n' && tlen != 0)? '\\' : '/',
+          linecount, plen, ss);
+      }
+    ss += plen;
+    if (*ss == '\n') ss++;
+    }
+  }
+}
+
+
+
+/*************************************************
+*             Die tidily                         *
+*************************************************/
+
+/* This is called when Exim is dying as a result of something going wrong in
+the logging, or after a log call with LOG_PANIC_DIE set. Optionally write a
+message to debug_file or a stderr file, if they exist. Then, if in the middle
+of accepting a message, throw it away tidily by calling receive_bomb_out();
+this will attempt to send an SMTP response if appropriate. Passing NULL as the
+first argument stops it trying to run the NOTQUIT ACL (which might try further
+logging and thus cause problems). Otherwise, try to close down an outstanding
+SMTP call tidily.
+
+Arguments:
+  s1         Error message to write to debug_file and/or stderr and syslog
+  s2         Error message for any SMTP call that is in progress
+Returns:     The function does not return
+*/
+
+static void
+die(uschar *s1, uschar *s2)
+{
+if (s1 != NULL)
+  {
+  write_syslog(LOG_CRIT, s1);
+  if (debug_file != NULL) debug_printf("%s\n", s1);
+  if (log_stderr != NULL && log_stderr != debug_file)
+    fprintf(log_stderr, "%s\n", s1);
+  }
+if (receive_call_bombout) receive_bomb_out(NULL, s2);  /* does not return */
+if (smtp_input) smtp_closedown(s2);
+exim_exit(EXIT_FAILURE);
+}
+
+
+
+/*************************************************
+*             Create a log file                  *
+*************************************************/
+
+/* This function is called to create and open a log file. It may be called in a
+subprocess when the original process is root.
+
+Arguments:
+  name         the file name
+
+The file name has been build in a working buffer, so it is permissible to
+overwrite it temporarily if it is necessary to create the directory.
+
+Returns:       a file descriptor, or < 0 on failure (errno set)
+*/
+
+int
+log_create(uschar *name)
+{
+int fd = Uopen(name, O_CREAT|O_APPEND|O_WRONLY, LOG_MODE);
+
+/* If creation failed, attempt to build a log directory in case that is the
+problem. */
+
+if (fd < 0 && errno == ENOENT)
+  {
+  BOOL created;
+  uschar *lastslash = Ustrrchr(name, '/');
+  *lastslash = 0;
+  created = directory_make(NULL, name, LOG_DIRECTORY_MODE, FALSE);
+  DEBUG(D_any) debug_printf("%s log directory %s\n",
+    created? "created" : "failed to create", name);
+  *lastslash = '/';
+  if (created) fd = Uopen(name, O_CREAT|O_APPEND|O_WRONLY, LOG_MODE);
+  }
+
+return fd;
+}
+
+
+
+/*************************************************
+*     Create a log file as the exim user         *
+*************************************************/
+
+/* This function is called when we are root to spawn an exim:exim subprocess
+in which we can create a log file. It must be signal-safe since it is called
+by the usr1_handler().
+
+Arguments:
+  name         the file name
+
+Returns:       a file descriptor, or < 0 on failure (errno set)
+*/
+
+int
+log_create_as_exim(uschar *name)
+{
+pid_t pid = fork();
+int status = 1;
+int fd = -1;
+
+/* In the subprocess, change uid/gid and do the creation. Return 0 from the
+subprocess on success. If we don't check for setuid failures, then the file
+can be created as root, so vulnerabilities which cause setuid to fail mean
+that the Exim user can use symlinks to cause a file to be opened/created as
+root. We always open for append, so can't nuke existing content but it would
+still be Rather Bad. */
+
+if (pid == 0)
+  {
+  if (setgid(exim_gid) < 0)
+    die(US"exim: setgid for log-file creation failed, aborting",
+      US"Unexpected log failure, please try later");
+  if (setuid(exim_uid) < 0)
+    die(US"exim: setuid for log-file creation failed, aborting",
+      US"Unexpected log failure, please try later");
+  _exit((log_create(name) < 0)? 1 : 0);
+  }
+
+/* If we created a subprocess, wait for it. If it succeeded, try the open. */
+
+while (pid > 0 && waitpid(pid, &status, 0) != pid);
+if (status == 0) fd = Uopen(name, O_APPEND|O_WRONLY, LOG_MODE);
+
+/* If we failed to create a subprocess, we are in a bad way. We return
+with fd still < 0, and errno set, letting the caller handle the error. */
+
+return fd;
+}
+
+
+
+
+/*************************************************
+*                Open a log file                 *
+*************************************************/
+
+/* This function opens one of a number of logs, creating the log directory if
+it does not exist. This may be called recursively on failure, in order to open
+the panic log.
+
+The directory is in the static variable file_path. This is static so that it
+the work of sorting out the path is done just once per Exim process.
+
+Exim is normally configured to avoid running as root wherever possible, the log
+files must be owned by the non-privileged exim user. To ensure this, first try
+an open without O_CREAT - most of the time this will succeed. If it fails, try
+to create the file; if running as root, this must be done in a subprocess to
+avoid races.
+
+Arguments:
+  fd         where to return the resulting file descriptor
+  type       lt_main, lt_reject, lt_panic, or lt_debug
+  tag        optional tag to include in the name (only hooked up for debug)
+
+Returns:   nothing
+*/
+
+static void
+open_log(int *fd, int type, uschar *tag)
+{
+uid_t euid;
+BOOL ok, ok2;
+uschar buffer[LOG_NAME_SIZE];
+
+/* The names of the log files are controlled by file_path. The panic log is
+written to the same directory as the main and reject logs, but its name does
+not have a datestamp. The use of datestamps is indicated by %D/%M in file_path.
+When opening the panic log, if %D or %M is present, we remove the datestamp
+from the generated name; if it is at the start, remove a following
+non-alphanumeric character as well; otherwise, remove a preceding
+non-alphanumeric character. This is definitely kludgy, but it sort of does what
+people want, I hope. */
+
+ok = string_format(buffer, sizeof(buffer), CS file_path, log_names[type]);
+
+/* Save the name of the mainlog for rollover processing. Without a datestamp,
+it gets statted to see if it has been cycled. With a datestamp, the datestamp
+will be compared. The static slot for saving it is the same size as buffer,
+and the text has been checked above to fit, so this use of strcpy() is OK. */
+
+if (type == lt_main)
+  {
+  Ustrcpy(mainlog_name, buffer);
+  mainlog_datestamp = mainlog_name + string_datestamp_offset;
+  }
+
+/* Ditto for the reject log */
+
+else if (type == lt_reject)
+  {
+  Ustrcpy(rejectlog_name, buffer);
+  rejectlog_datestamp = rejectlog_name + string_datestamp_offset;
+  }
+
+/* and deal with the debug log (which keeps the datestamp, but does not
+update it) */
+
+else if (type == lt_debug)
+  {
+  Ustrcpy(debuglog_name, buffer);
+  if (tag)
+    {
+    /* this won't change the offset of the datestamp */
+    ok2 = string_format(buffer, sizeof(buffer), "%s%s",
+      debuglog_name, tag);
+    if (ok2)
+      Ustrcpy(debuglog_name, buffer);
+    }
+  }
+
+/* Remove any datestamp if this is the panic log. This is rare, so there's no
+need to optimize getting the datestamp length. We remove one non-alphanumeric
+char afterwards if at the start, otherwise one before. */
+
+else if (string_datestamp_offset >= 0)
+  {
+  uschar *from = buffer + string_datestamp_offset;
+  uschar *to = from + string_datestamp_length;
+  if (from == buffer || from[-1] == '/')
+    {
+    if (!isalnum(*to)) to++;
+    }
+  else
+    {
+    if (!isalnum(from[-1])) from--;
+    }
+
+  /* This strcpy is ok, because we know that to is a substring of from. */
+
+  Ustrcpy(from, to);
+  }
+
+/* If the file name is too long, it is an unrecoverable disaster */
+
+if (!ok)
+  {
+  die(US"exim: log file path too long: aborting",
+      US"Logging failure; please try later");
+  }
+
+/* We now have the file name. Try to open an existing file. After a successful
+open, arrange for automatic closure on exec(), and then return. */
+
+*fd = Uopen(buffer, O_APPEND|O_WRONLY, LOG_MODE);
+
+if (*fd >= 0)
+  {
+  (void)fcntl(*fd, F_SETFD, fcntl(*fd, F_GETFD) | FD_CLOEXEC);
+  return;
+  }
+
+/* Open was not successful: try creating the file. If this is a root process,
+we must do the creating in a subprocess set to exim:exim in order to ensure
+that the file is created with the right ownership. Otherwise, there can be a
+race if another Exim process is trying to write to the log at the same time.
+The use of SIGUSR1 by the exiwhat utility can provoke a lot of simultaneous
+writing. */
+
+euid = geteuid();
+
+/* If we are already running as the Exim user (even if that user is root),
+we can go ahead and create in the current process. */
+
+if (euid == exim_uid) *fd = log_create(buffer);
+
+/* Otherwise, if we are root, do the creation in an exim:exim subprocess. If we
+are neither exim nor root, creation is not attempted. */
+
+else if (euid == root_uid) *fd = log_create_as_exim(buffer);
+
+/* If we now have an open file, set the close-on-exec flag and return. */
+
+if (*fd >= 0)
+  {
+  (void)fcntl(*fd, F_SETFD, fcntl(*fd, F_GETFD) | FD_CLOEXEC);
+  return;
+  }
+
+/* Creation failed. There are some circumstances in which we get here when
+the effective uid is not root or exim, which is the problem. (For example, a
+non-setuid binary with log_arguments set, called in certain ways.) Rather than
+just bombing out, force the log to stderr and carry on if stderr is available.
+*/
+
+if (euid != root_uid && euid != exim_uid && log_stderr != NULL)
+  {
+  *fd = fileno(log_stderr);
+  return;
+  }
+
+/* Otherwise this is a disaster. This call is deliberately ONLY to the panic
+log. If possible, save a copy of the original line that was being logged. If we
+are recursing (can't open the panic log either), the pointer will already be
+set. */
+
+if (panic_save_buffer == NULL)
+  {
+  panic_save_buffer = (uschar *)malloc(LOG_BUFFER_SIZE);
+  if (panic_save_buffer != NULL)
+    memcpy(panic_save_buffer, log_buffer, LOG_BUFFER_SIZE);
+  }
+
+log_write(0, LOG_PANIC_DIE, "Cannot open %s log file \"%s\": %s: "
+  "euid=%d egid=%d", log_names[type], buffer, strerror(errno), euid, getegid());
+/* Never returns */
+}
+
+
+
+/*************************************************
+*     Add configuration file info to log line    *
+*************************************************/
+
+/* This is put in a function because it's needed twice (once for debugging,
+once for real).
+
+Arguments:
+  ptr         pointer to the end of the line we are building
+  flags       log flags
+
+Returns:      updated pointer
+*/
+
+static uschar *
+log_config_info(uschar *ptr, int flags)
+{
+Ustrcpy(ptr, "Exim configuration error");
+ptr += 24;
+
+if ((flags & (LOG_CONFIG_FOR & ~LOG_CONFIG)) != 0)
+  {
+  Ustrcpy(ptr, " for ");
+  return ptr + 5;
+  }
+
+if ((flags & (LOG_CONFIG_IN & ~LOG_CONFIG)) != 0)
+  {
+  sprintf(CS ptr, " in line %d of %s", config_lineno, config_filename);
+  while (*ptr) ptr++;
+  }
+
+Ustrcpy(ptr, ":\n  ");
+return ptr + 4;
+}
+
+
+/*************************************************
+*           A write() operation failed           *
+*************************************************/
+
+/* This function is called when write() fails on anything other than the panic
+log, which can happen if a disk gets full or a file gets too large or whatever.
+We try to save the relevant message in the panic_save buffer before crashing
+out.
+
+The potential invoker should probably not call us for EINTR -1 writes.  But
+otherwise, short writes are bad as we don't do non-blocking writes to fds
+subject to flow control.  (If we do, that's new and the logic of this should
+be reconsidered).
+
+Arguments:
+  name      the name of the log being written
+  length    the string length being written
+  rc        the return value from write()
+
+Returns:    does not return
+*/
+
+static void
+log_write_failed(uschar *name, int length, int rc)
+{
+int save_errno = errno;
+
+if (panic_save_buffer == NULL)
+  {
+  panic_save_buffer = (uschar *)malloc(LOG_BUFFER_SIZE);
+  if (panic_save_buffer != NULL)
+    memcpy(panic_save_buffer, log_buffer, LOG_BUFFER_SIZE);
+  }
+
+log_write(0, LOG_PANIC_DIE, "failed to write to %s: length=%d result=%d "
+  "errno=%d (%s)", name, length, rc, save_errno,
+  (save_errno == 0)? "write incomplete" : strerror(save_errno));
+/* Never returns */
+}
+
+
+
+/*************************************************
+*     Write to an fd, retrying after signals     *
+*************************************************/
+
+/* Basic write to fd for logs, handling EINTR.
+
+Arguments:
+  fd        the fd to write to
+  buf       the string to write
+  length    the string length being written
+
+Returns:
+  length actually written, persisting an errno from write()
+*/
+ssize_t
+write_to_fd_buf(int fd, const uschar *buf, size_t length)
+{
+ssize_t wrote;
+size_t total_written = 0;
+const uschar *p = buf;
+size_t left = length;
+
+while (1)
+  {
+  wrote = write(fd, p, left);
+  if (wrote == (ssize_t)-1)
+    {
+    if (errno == EINTR) continue;
+    return wrote;
+    }
+  total_written += wrote;
+  if (wrote == left)
+    break;
+  else
+    {
+    p += wrote;
+    left -= wrote;
+    }
+  }
+return total_written;
+}
+
+
+/*************************************************
+*            Write message to log file           *
+*************************************************/
+
+/* Exim can be configured to log to local files, or use syslog, or both. This
+is controlled by the setting of log_file_path. The following cases are
+recognized:
+
+  log_file_path = ""               write files in the spool/log directory
+  log_file_path = "xxx"            write files in the xxx directory
+  log_file_path = "syslog"         write to syslog
+  log_file_path = "syslog : xxx"   write to syslog and to files (any order)
+
+The message always gets '\n' added on the end of it, since more than one
+process may be writing to the log at once and we don't want intermingling to
+happen in the middle of lines. To be absolutely sure of this we write the data
+into a private buffer and then put it out in a single write() call.
+
+The flags determine which log(s) the message is written to, or for syslogging,
+which priority to use, and in the case of the panic log, whether the process
+should die afterwards.
+
+The variable really_exim is TRUE only when exim is running in privileged state
+(i.e. not with a changed configuration or with testing options such as -brw).
+If it is not, don't try to write to the log because permission will probably be
+denied.
+
+Avoid actually writing to the logs when exim is called with -bv or -bt to
+test an address, but take other actions, such as panicing.
+
+In Exim proper, the buffer for building the message is got at start-up, so that
+nothing gets done if it can't be got. However, some functions that are also
+used in utilities occasionally obey log_write calls in error situations, and it
+is simplest to put a single malloc() here rather than put one in each utility.
+Malloc is used directly because the store functions may call log_write().
+
+If a message_id exists, we include it after the timestamp.
+
+Arguments:
+  selector  write to main log or LOG_INFO only if this value is zero, or if
+              its bit is set in log_write_selector
+  flags     each bit indicates some independent action:
+              LOG_SENDER      add raw sender to the message
+              LOG_RECIPIENTS  add raw recipients list to message
+              LOG_CONFIG      add "Exim configuration error"
+              LOG_CONFIG_FOR  add " for " instead of ":\n  "
+              LOG_CONFIG_IN   add " in line x[ of file y]"
+              LOG_MAIN        write to main log or syslog LOG_INFO
+              LOG_REJECT      write to reject log or syslog LOG_NOTICE
+              LOG_PANIC       write to panic log or syslog LOG_ALERT
+              LOG_PANIC_DIE   write to panic log or LOG_ALERT and then crash
+  format    a printf() format
+  ...       arguments for format
+
+Returns:    nothing
+*/
+
+void
+log_write(unsigned int selector, int flags, const char *format, ...)
+{
+uschar *ptr;
+int length;
+int paniclogfd;
+ssize_t written_len;
+va_list ap;
+
+/* If panic_recurseflag is set, we have failed to open the panic log. This is
+the ultimate disaster. First try to write the message to a debug file and/or
+stderr and also to syslog. If panic_save_buffer is not NULL, it contains the
+original log line that caused the problem. Afterwards, expire. */
+
+if (panic_recurseflag)
+  {
+  uschar *extra = (panic_save_buffer == NULL)? US"" : panic_save_buffer;
+  if (debug_file != NULL) debug_printf("%s%s", extra, log_buffer);
+  if (log_stderr != NULL && log_stderr != debug_file)
+    fprintf(log_stderr, "%s%s", extra, log_buffer);
+  if (*extra != 0) write_syslog(LOG_CRIT, extra);
+  write_syslog(LOG_CRIT, log_buffer);
+  die(US"exim: could not open panic log - aborting: see message(s) above",
+    US"Unexpected log failure, please try later");
+  }
+
+/* Ensure we have a buffer (see comment above); this should never be obeyed
+when running Exim proper, only when running utilities. */
+
+if (log_buffer == NULL)
+  {
+  log_buffer = (uschar *)malloc(LOG_BUFFER_SIZE);
+  if (log_buffer == NULL)
+    {
+    fprintf(stderr, "exim: failed to get store for log buffer\n");
+    exim_exit(EXIT_FAILURE);
+    }
+  }
+
+/* If we haven't already done so, inspect the setting of log_file_path to
+determine whether to log to files and/or to syslog. Bits in logging_mode
+control this, and for file logging, the path must end up in file_path. This
+variable must be in permanent store because it may be required again later in
+the process. */
+
+if (!path_inspected)
+  {
+  BOOL multiple = FALSE;
+  int old_pool = store_pool;
+
+  store_pool = POOL_PERM;
+
+  /* If nothing has been set, don't waste effort... the default values for the
+  statics are file_path="" and logging_mode = LOG_MODE_FILE. */
+
+  if (log_file_path[0] != 0)
+    {
+    int sep = ':';              /* Fixed separator - outside use */
+    uschar *s;
+    uschar *ss = log_file_path;
+    logging_mode = 0;
+    while ((s = string_nextinlist(&ss,&sep,log_buffer,LOG_BUFFER_SIZE)) != NULL)
+      {
+      if (Ustrcmp(s, "syslog") == 0)
+        logging_mode |= LOG_MODE_SYSLOG;
+      else if ((logging_mode & LOG_MODE_FILE) != 0) multiple = TRUE;
+      else
+        {
+        logging_mode |= LOG_MODE_FILE;
+
+        /* If a non-empty path is given, use it */
+
+        if (s[0] != 0)
+          {
+          file_path = string_copy(s);
+          }
+
+        /* If the path is empty, we want to use the first non-empty, non-
+        syslog item in LOG_FILE_PATH, if there is one, since the value of
+        log_file_path may have been set at runtime. If there is no such item,
+        use the ultimate default in the spool directory. */
+
+        else
+          {
+          uschar *t;
+          uschar *tt = US LOG_FILE_PATH;
+          while ((t = string_nextinlist(&tt,&sep,log_buffer,LOG_BUFFER_SIZE))
+                != NULL)
+            {
+            if (Ustrcmp(t, "syslog") == 0 || t[0] == 0) continue;
+            file_path = string_copy(t);
+            break;
+            }
+          }  /* Empty item in log_file_path */
+        }    /* First non-syslog item in log_file_path */
+      }      /* Scan of log_file_path */
+    }
+
+  /* If no modes have been selected, it is a major disaster */
+
+  if (logging_mode == 0)
+    die(US"Neither syslog nor file logging set in log_file_path",
+        US"Unexpected logging failure");
+
+  /* Set up the ultimate default if necessary. Then revert to the old store
+  pool, and record that we've sorted out the path. */
+
+  if ((logging_mode & LOG_MODE_FILE) != 0 && file_path[0] == 0)
+    file_path = string_sprintf("%s/log/%%slog", spool_directory);
+  store_pool = old_pool;
+  path_inspected = TRUE;
+
+  /* If more than one file path was given, log a complaint. This recursive call
+  should work since we have now set up the routing. */
+
+  if (multiple)
+    {
+    log_write(0, LOG_MAIN|LOG_PANIC,
+      "More than one path given in log_file_path: using %s", file_path);
+    }
+  }
+
+/* If debugging, show all log entries, but don't show headers. Do it all
+in one go so that it doesn't get split when multi-processing. */
+
+DEBUG(D_any|D_v)
+  {
+  int i;
+  ptr = log_buffer;
+
+  Ustrcpy(ptr, "LOG:");
+  ptr += 4;
+
+  /* Show the options that were passed into the call. These are those whose
+  flag values do not have the 0x80000000 bit in them. Note that this
+  automatically exclude the "all" setting. */
+
+  for (i = 0; i < log_options_count; i++)
+    {
+    unsigned int bit = log_options[i].bit;
+    if ((bit & 0x80000000) != 0) continue;
+    if ((selector & bit) != 0)
+      {
+      *ptr++ = ' ';
+      Ustrcpy(ptr, log_options[i].name);
+      while (*ptr) ptr++;
+      }
+    }
+
+  sprintf(CS ptr, "%s%s%s%s\n  ",
+    ((flags & LOG_MAIN) != 0)?    " MAIN"   : "",
+    ((flags & LOG_PANIC) != 0)?   " PANIC"  : "",
+    ((flags & LOG_PANIC_DIE) == LOG_PANIC_DIE)? " DIE" : "",
+    ((flags & LOG_REJECT) != 0)?  " REJECT" : "");
+
+  while(*ptr) ptr++;
+  if ((flags & LOG_CONFIG) != 0) ptr = log_config_info(ptr, flags);
+
+  va_start(ap, format);
+  if (!string_vformat(ptr, LOG_BUFFER_SIZE - (ptr-log_buffer)-1, format, ap))
+    Ustrcpy(ptr, "**** log string overflowed log buffer ****");
+  va_end(ap);
+
+  while(*ptr) ptr++;
+  Ustrcat(ptr, "\n");
+  debug_printf("%s", log_buffer);
+  }
+
+/* If no log file is specified, we are in a mess. */
+
+if ((flags & (LOG_MAIN|LOG_PANIC|LOG_REJECT)) == 0)
+  log_write(0, LOG_MAIN|LOG_PANIC_DIE, "log_write called with no log "
+    "flags set");
+
+/* There are some weird circumstances in which logging is disabled. */
+
+if (disable_logging)
+  {
+  DEBUG(D_any) debug_printf("log writing disabled\n");
+  return;
+  }
+
+/* Handle disabled reject log */
+
+if (!write_rejectlog) flags &= ~LOG_REJECT;
+
+/* Create the main message in the log buffer. Do not include the message id
+when called by a utility. */
+
+ptr = log_buffer;
+sprintf(CS ptr, "%s ", tod_stamp(tod_log));
+while(*ptr) ptr++;
+
+if ((log_extra_selector & LX_pid) != 0)
+  {
+  sprintf(CS ptr, "[%d] ", (int)getpid());
+  while (*ptr) ptr++;
+  }
+
+if (really_exim && message_id[0] != 0)
+  {
+  sprintf(CS ptr, "%s ", message_id);
+  while(*ptr) ptr++;
+  }
+
+if ((flags & LOG_CONFIG) != 0) ptr = log_config_info(ptr, flags);
+
+va_start(ap, format);
+if (!string_vformat(ptr, LOG_BUFFER_SIZE - (ptr-log_buffer)-1, format, ap))
+  Ustrcpy(ptr, "**** log string overflowed log buffer ****\n");
+while(*ptr) ptr++;
+va_end(ap);
+
+/* Add the raw, unrewritten, sender to the message if required. This is done
+this way because it kind of fits with LOG_RECIPIENTS. */
+
+if ((flags & LOG_SENDER) != 0 &&
+    ptr < log_buffer + LOG_BUFFER_SIZE - 10 - Ustrlen(raw_sender))
+  {
+  sprintf(CS ptr, " from <%s>", raw_sender);
+  while (*ptr) ptr++;
+  }
+
+/* Add list of recipients to the message if required; the raw list,
+before rewriting, was saved in raw_recipients. There may be none, if an ACL
+discarded them all. */
+
+if ((flags & LOG_RECIPIENTS) != 0 && ptr < log_buffer + LOG_BUFFER_SIZE - 6 &&
+     raw_recipients_count > 0)
+  {
+  int i;
+  sprintf(CS ptr, " for");
+  while (*ptr) ptr++;
+  for (i = 0; i < raw_recipients_count; i++)
+    {
+    uschar *s = raw_recipients[i];
+    if (log_buffer + LOG_BUFFER_SIZE - ptr < Ustrlen(s) + 3) break;
+    sprintf(CS ptr, " %s", s);
+    while (*ptr) ptr++;
+    }
+  }
+
+sprintf(CS  ptr, "\n");
+while(*ptr) ptr++;
+length = ptr - log_buffer;
+
+/* Handle loggable errors when running a utility, or when address testing.
+Write to log_stderr unless debugging (when it will already have been written),
+or unless there is no log_stderr (expn called from daemon, for example). */
+
+if (!really_exim || log_testing_mode)
+  {
+  if (debug_selector == 0 && log_stderr != NULL &&
+      (selector == 0 || (selector & log_write_selector) != 0))
+    {
+    if (host_checking)
+      fprintf(log_stderr, "LOG: %s", CS(log_buffer + 20));  /* no timestamp */
+    else
+      fprintf(log_stderr, "%s", CS log_buffer);
+    }
+  if ((flags & LOG_PANIC_DIE) == LOG_PANIC_DIE) exim_exit(EXIT_FAILURE);
+  return;
+  }
+
+/* Handle the main log. We know that either syslog or file logging (or both) is
+set up. A real file gets left open during reception or delivery once it has
+been opened, but we don't want to keep on writing to it for too long after it
+has been renamed. Therefore, do a stat() and see if the inode has changed, and
+if so, re-open. */
+
+if ((flags & LOG_MAIN) != 0 &&
+    (selector == 0 || (selector & log_write_selector) != 0))
+  {
+  if ((logging_mode & LOG_MODE_SYSLOG) != 0 &&
+      (syslog_duplication || (flags & (LOG_REJECT|LOG_PANIC)) == 0))
+    write_syslog(LOG_INFO, log_buffer);
+
+  if ((logging_mode & LOG_MODE_FILE) != 0)
+    {
+    struct stat statbuf;
+
+    /* Check for a change to the mainlog file name when datestamping is in
+    operation. This happens at midnight, at which point we want to roll over
+    the file. Closing it has the desired effect. */
+
+    if (mainlog_datestamp != NULL)
+      {
+      uschar *nowstamp = tod_stamp(string_datestamp_type);
+      if (Ustrncmp (mainlog_datestamp, nowstamp, Ustrlen(nowstamp)) != 0)
+        {
+        (void)close(mainlogfd);       /* Close the file */
+        mainlogfd = -1;               /* Clear the file descriptor */
+        mainlog_inode = 0;            /* Unset the inode */
+        mainlog_datestamp = NULL;     /* Clear the datestamp */
+        }
+      }
+
+    /* Otherwise, we want to check whether the file has been renamed by a
+    cycling script. This could be "if else", but for safety's sake, leave it as
+    "if" so that renaming the log starts a new file even when datestamping is
+    happening. */
+
+    if (mainlogfd >= 0)
+      {
+      if (Ustat(mainlog_name, &statbuf) < 0 || statbuf.st_ino != mainlog_inode)
+        {
+        (void)close(mainlogfd);
+        mainlogfd = -1;
+        mainlog_inode = 0;
+        }
+      }
+
+    /* If the log is closed, open it. Then write the line. */
+
+    if (mainlogfd < 0)
+      {
+      open_log(&mainlogfd, lt_main, NULL);     /* No return on error */
+      if (fstat(mainlogfd, &statbuf) >= 0) mainlog_inode = statbuf.st_ino;
+      }
+
+    /* Failing to write to the log is disastrous */
+
+    written_len = write_to_fd_buf(mainlogfd, log_buffer, length);
+    if (written_len != length)
+      {
+      log_write_failed(US"main log", length, written_len);
+      /* That function does not return */
+      }
+    }
+  }
+
+/* Handle the log for rejected messages. This can be globally disabled, in
+which case the flags are altered above. If there are any header lines (i.e. if
+the rejection is happening after the DATA phase), log the recipients and the
+headers. */
+
+if ((flags & LOG_REJECT) != 0)
+  {
+  header_line *h;
+
+  if (header_list != NULL && (log_extra_selector & LX_rejected_header) != 0)
+    {
+    if (recipients_count > 0)
+      {
+      int i;
+
+      /* List the sender */
+
+      string_format(ptr, LOG_BUFFER_SIZE - (ptr-log_buffer),
+        "Envelope-from: <%s>\n", sender_address);
+      while (*ptr) ptr++;
+
+      /* List up to 5 recipients */
+
+      string_format(ptr, LOG_BUFFER_SIZE - (ptr-log_buffer),
+        "Envelope-to: <%s>\n", recipients_list[0].address);
+      while (*ptr) ptr++;
+
+      for (i = 1; i < recipients_count && i < 5; i++)
+        {
+        string_format(ptr, LOG_BUFFER_SIZE - (ptr-log_buffer), "    <%s>\n",
+          recipients_list[i].address);
+        while (*ptr) ptr++;
+        }
+
+      if (i < recipients_count)
+        {
+        (void)string_format(ptr, LOG_BUFFER_SIZE - (ptr-log_buffer),
+          "    ...\n");
+        while (*ptr) ptr++;
+        }
+      }
+
+    /* A header with a NULL text is an unfilled in Received: header */
+
+    for (h = header_list; h != NULL; h = h->next)
+      {
+      BOOL fitted;
+      if (h->text == NULL) continue;
+      fitted = string_format(ptr, LOG_BUFFER_SIZE - (ptr-log_buffer),
+        "%c %s", h->type, h->text);
+      while(*ptr) ptr++;
+      if (!fitted)         /* Buffer is full; truncate */
+        {
+        ptr -= 100;        /* For message and separator */
+        if (ptr[-1] == '\n') ptr--;
+        Ustrcpy(ptr, "\n*** truncated ***\n");
+        while (*ptr) ptr++;
+        break;
+        }
+      }
+
+    length = ptr - log_buffer;
+    }
+
+  /* Write to syslog or to a log file */
+
+  if ((logging_mode & LOG_MODE_SYSLOG) != 0 &&
+      (syslog_duplication || (flags & LOG_PANIC) == 0))
+    write_syslog(LOG_NOTICE, log_buffer);
+
+  /* Check for a change to the rejectlog file name when datestamping is in
+  operation. This happens at midnight, at which point we want to roll over
+  the file. Closing it has the desired effect. */
+
+  if ((logging_mode & LOG_MODE_FILE) != 0)
+    {
+    struct stat statbuf;
+
+    if (rejectlog_datestamp != NULL)
+      {
+      uschar *nowstamp = tod_stamp(string_datestamp_type);
+      if (Ustrncmp (rejectlog_datestamp, nowstamp, Ustrlen(nowstamp)) != 0)
+        {
+        (void)close(rejectlogfd);       /* Close the file */
+        rejectlogfd = -1;               /* Clear the file descriptor */
+        rejectlog_inode = 0;            /* Unset the inode */
+        rejectlog_datestamp = NULL;     /* Clear the datestamp */
+        }
+      }
+
+    /* Otherwise, we want to check whether the file has been renamed by a
+    cycling script. This could be "if else", but for safety's sake, leave it as
+    "if" so that renaming the log starts a new file even when datestamping is
+    happening. */
+
+    if (rejectlogfd >= 0)
+      {
+      if (Ustat(rejectlog_name, &statbuf) < 0 ||
+           statbuf.st_ino != rejectlog_inode)
+        {
+        (void)close(rejectlogfd);
+        rejectlogfd = -1;
+        rejectlog_inode = 0;
+        }
+      }
+
+    /* Open the file if necessary, and write the data */
+
+    if (rejectlogfd < 0)
+      {
+      open_log(&rejectlogfd, lt_reject, NULL); /* No return on error */
+      if (fstat(rejectlogfd, &statbuf) >= 0) rejectlog_inode = statbuf.st_ino;
+      }
+
+    written_len = write_to_fd_buf(rejectlogfd, log_buffer, length);
+    if (written_len != length)
+      {
+      log_write_failed(US"reject log", length, written_len);
+      /* That function does not return */
+      }
+    }
+  }
+
+
+/* Handle the panic log, which is not kept open like the others. If it fails to
+open, there will be a recursive call to log_write(). We detect this above and
+attempt to write to the system log as a last-ditch try at telling somebody. In
+all cases except mua_wrapper, try to write to log_stderr. */
+
+if ((flags & LOG_PANIC) != 0)
+  {
+  if (log_stderr != NULL && log_stderr != debug_file && !mua_wrapper)
+    fprintf(log_stderr, "%s", CS log_buffer);
+
+  if ((logging_mode & LOG_MODE_SYSLOG) != 0)
+    {
+    write_syslog(LOG_ALERT, log_buffer);
+    }
+
+  /* If this panic logging was caused by a failure to open the main log,
+  the original log line is in panic_save_buffer. Make an attempt to write it. */
+
+  if ((logging_mode & LOG_MODE_FILE) != 0)
+    {
+    panic_recurseflag = TRUE;
+    open_log(&paniclogfd, lt_panic, NULL);  /* Won't return on failure */
+    panic_recurseflag = FALSE;
+
+    if (panic_save_buffer != NULL)
+      {
+      int i = write(paniclogfd, panic_save_buffer, Ustrlen(panic_save_buffer));
+      i = i;   /* compiler quietening */
+      }
+
+    written_len = write_to_fd_buf(paniclogfd, log_buffer, length);
+    if (written_len != length)
+      {
+      int save_errno = errno;
+      write_syslog(LOG_CRIT, log_buffer);
+      sprintf(CS log_buffer, "write failed on panic log: length=%d result=%d "
+        "errno=%d (%s)", length, (int)written_len, save_errno, strerror(save_errno));
+      write_syslog(LOG_CRIT, log_buffer);
+      flags |= LOG_PANIC_DIE;
+      }
+
+    (void)close(paniclogfd);
+    }
+
+  /* Give up if the DIE flag is set */
+
+  if ((flags & LOG_PANIC_DIE) != LOG_PANIC)
+    die(NULL, US"Unexpected failure, please try later");
+  }
+}
+
+
+
+/*************************************************
+*            Close any open log files            *
+*************************************************/
+
+void
+log_close_all(void)
+{
+if (mainlogfd >= 0)
+  { (void)close(mainlogfd); mainlogfd = -1; }
+if (rejectlogfd >= 0)
+  { (void)close(rejectlogfd); rejectlogfd = -1; }
+closelog();
+syslog_open = FALSE;
+}
+
+
+
+/*************************************************
+*         Decode bit settings for log/debug      *
+*************************************************/
+
+/* This function decodes a string containing bit settings in the form of +name
+and/or -name sequences, and sets/unsets bits in a bit string accordingly. It
+also recognizes a numeric setting of the form =<number>, but this is not
+intended for user use. It's an easy way for Exim to pass the debug settings
+when it is re-exec'ed.
+
+The log options are held in two unsigned ints (because there became too many
+for one). The top bit in the table means "put in 2nd selector". This does not
+yet apply to debug options, so the "=" facility sets only the first selector.
+
+The "all" selector, which must be equal to 0xffffffff, is recognized specially.
+It sets all the bits in both selectors. However, there is a facility for then
+unsetting certain bits, because we want to turn off "memory" in the debug case.
+
+The action taken for bad values varies depending upon why we're here.
+For log messages, or if the debugging is triggered from config, then we write
+to the log on the way out.  For debug setting triggered from the command-line,
+we treat it as an unknown option: error message to stderr and die.
+
+Arguments:
+  selector1      address of the first bit string
+  selector2      address of the second bit string, or NULL
+  notall1        bits to exclude from "all" for selector1
+  notall2        bits to exclude from "all" for selector2
+  string         the configured string
+  options        the table of option names
+  count          size of table
+  which          "log" or "debug"
+  flags          DEBUG_FROM_CONFIG
+
+Returns:         nothing on success - bomb out on failure
+*/
+
+void
+decode_bits(unsigned int *selector1, unsigned int *selector2, int notall1,
+  int notall2, uschar *string, bit_table *options, int count, uschar *which,
+  int flags)
+{
+uschar *errmsg;
+if (string == NULL) return;
+
+if (*string == '=')
+  {
+  char *end;    /* Not uschar */
+  *selector1 = strtoul(CS string+1, &end, 0);
+  if (*end == 0) return;
+  errmsg = string_sprintf("malformed numeric %s_selector setting: %s", which,
+    string);
+  goto ERROR_RETURN;
+  }
+
+/* Handle symbolic setting */
+
+else for(;;)
+  {
+  BOOL adding;
+  uschar *s;
+  int len;
+  bit_table *start, *end;
+
+  while (isspace(*string)) string++;
+  if (*string == 0) return;
+
+  if (*string != '+' && *string != '-')
+    {
+    errmsg = string_sprintf("malformed %s_selector setting: "
+      "+ or - expected but found \"%s\"", which, string);
+    goto ERROR_RETURN;
+    }
+
+  adding = *string++ == '+';
+  s = string;
+  while (isalnum(*string) || *string == '_') string++;
+  len = string - s;
+
+  start = options;
+  end = options + count;
+
+  while (start < end)
+    {
+    bit_table *middle = start + (end - start)/2;
+    int c = Ustrncmp(s, middle->name, len);
+    if (c == 0)
+      {
+      if (middle->name[len] != 0) c = -1; else
+        {
+        unsigned int bit = middle->bit;
+        unsigned int *selector;
+
+        /* The value with all bits set means "force all bits in both selectors"
+        in the case where two are being handled. However, the top bit in the
+        second selector is never set. When setting, some bits can be excluded.
+        */
+
+        if (bit == 0xffffffff)
+          {
+          if (adding)
+            {
+            *selector1 = 0xffffffff ^ notall1;
+            if (selector2 != NULL) *selector2 = 0x7fffffff ^ notall2;
+            }
+          else
+            {
+            *selector1 = 0;
+            if (selector2 != NULL) *selector2 = 0;
+            }
+          }
+
+        /* Otherwise, the 0x80000000 bit means "this value, without the top
+        bit, belongs in the second selector". */
+
+        else
+          {
+          if ((bit & 0x80000000) != 0)
+            {
+            selector = selector2;
+            bit &= 0x7fffffff;
+            }
+          else selector = selector1;
+          if (adding) *selector |= bit; else *selector &= ~bit;
+          }
+        break;  /* Out of loop to match selector name */
+        }
+      }
+    if (c < 0) end = middle; else start = middle + 1;
+    }  /* Loop to match selector name */
+
+  if (start >= end)
+    {
+    errmsg = string_sprintf("unknown %s_selector setting: %c%.*s", which,
+      adding? '+' : '-', len, s);
+    goto ERROR_RETURN;
+    }
+  }    /* Loop for selector names */
+
+/* Handle disasters */
+
+ERROR_RETURN:
+if (Ustrcmp(which, "debug") == 0)
+  {
+  if (flags & DEBUG_FROM_CONFIG)
+    {
+    log_write(0, LOG_CONFIG|LOG_PANIC, "%s", errmsg);
+    return;
+    }
+  fprintf(stderr, "exim: %s\n", errmsg);
+  exit(EXIT_FAILURE);
+  }
+else log_write(0, LOG_CONFIG|LOG_PANIC_DIE, "%s", errmsg);
+}
+
+
+
+/*************************************************
+*        Activate a debug logfile (late)         *
+*************************************************/
+
+/* Normally, debugging is activated from the command-line; it may be useful
+within the configuration to activate debugging later, based on certain
+conditions.  If debugging is already in progress, we return early, no action
+taken (besides debug-logging that we wanted debug-logging).
+
+Failures in options are not fatal but will result in paniclog entries for the
+misconfiguration.
+
+The first use of this is in ACL logic, "control = debug/tag=foo/opts=+expand"
+which can be combined with conditions, etc, to activate extra logging only
+for certain sources. The second use is inetd wait mode debug preservation. */
+
+void
+debug_logging_activate(uschar *tag_name, uschar *opts)
+{
+int fd = -1;
+
+if (debug_file)
+  {
+  debug_printf("DEBUGGING ACTIVATED FROM WITHIN CONFIG.\n"
+      "DEBUG: Tag=\"%s\" Opts=\"%s\"\n", tag_name, opts ? opts : US"");
+  return;
+  }
+
+if (tag_name != NULL && (Ustrchr(tag_name, '/') != NULL))
+  {
+  log_write(0, LOG_MAIN|LOG_PANIC, "debug tag may not contain a '/' in: %s",
+      tag_name);
+  return;
+  }
+
+debug_selector = D_default;
+if (opts)
+  {
+  decode_bits(&debug_selector, NULL, D_memory, 0, opts,
+      debug_options, debug_options_count, US"debug", DEBUG_FROM_CONFIG);
+  }
+
+open_log(&fd, lt_debug, tag_name);
+
+if (fd != -1)
+  debug_file = fdopen(fd, "w");
+else
+  log_write(0, LOG_MAIN|LOG_PANIC, "unable to open debug log");
+}
+
+
+/* End of log.c */
diff --git a/src/lookupapi.h b/src/lookupapi.h
new file mode 100644 (file)
index 0000000..2a27071
--- /dev/null
@@ -0,0 +1,59 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+
+/* The "type" field in each item is a set of bit flags:
+
+  lookup_querystyle     => this is a query-style lookup,
+                             else single-key (+ file) style
+  lookup_absfile        => an absolute file name is required,
+                             (for single-key style only)
+*/
+
+typedef struct lookup_info {
+  uschar *name;                   /* e.g. "lsearch" */
+  int type;                       /* query/singlekey/abs-file */
+  void *(*open)(                  /* open function */
+    uschar *,                     /* file name for those that have one */
+    uschar **);                   /* for error message */
+  BOOL (*check)(                  /* file checking function */
+    void *,                       /* handle */
+    uschar *,                     /* file name */
+    int,                          /* modemask for file checking */
+    uid_t *,                      /* owners for file checking */
+    gid_t *,                      /* owngroups for file checking */
+    uschar **);                   /* for error messages */
+  int (*find)(                    /* find function */
+    void *,                       /* handle */
+    uschar *,                     /* file name or NULL */
+    uschar *,                     /* key or query */
+    int,                          /* length of key or query */
+    uschar **,                    /* for returning answer */
+    uschar **,                    /* for error message */
+    BOOL *);                      /* to request cache cleanup */
+  void (*close)(                  /* close function */
+    void *);                      /* handle */
+  void (*tidy)(void);             /* tidy function */
+  uschar *(*quote)(               /* quoting function */
+    uschar *,                     /* string to quote */
+    uschar *);                    /* additional data from quote name */
+  void (*version_report)(         /* diagnostic function */
+    FILE *);                      /* fh to write to */
+} lookup_info;
+
+/* This magic number is used by the following lookup_module_info structure
+   for checking API compatibility. It's equivalent to the string"LMM2" */
+#define LOOKUP_MODULE_INFO_MAGIC 0x4c4d4d32
+/* Version 2 adds: version_report */
+
+typedef struct lookup_module_info {
+  uint magic;
+  lookup_info **lookups;
+  uint lookupcount;
+} lookup_module_info;
+
+/* End of lookupapi.h */
diff --git a/src/lookups/Makefile b/src/lookups/Makefile
new file mode 100644 (file)
index 0000000..6ba0cb1
--- /dev/null
@@ -0,0 +1,69 @@
+# Make file for building Exim's lookup modules.
+# This is called from the main make file, after cd'ing
+# to the lookups subdirectory.
+
+# nb: at build time, the version of this file used will have had some
+#     extra variable definitions and prepended to it and module build rules
+#     interpolated below.
+
+# MAGIC-TAG-MODS-OBJ-RULES-GO-HERE
+
+
+all:             lookups.a lf_quote.o lf_check_file.o lf_sqlperform.o $(MODS)
+
+lookups.a:       $(OBJ)
+                @$(RM_COMMAND) -f lookups.a
+                @echo "$(AR) lookups.a"
+                @$(AR) lookups.a $(OBJ)
+                $(RANLIB) $@
+
+.SUFFIXES:       .o .c .so
+.c.o:;           @echo "$(CC) $*.c"
+                $(FE)$(CC) -c $(CFLAGS) $(INCLUDE) $*.c
+
+.c.so:;          @echo "$(CC) -shared $*.c"
+                $(FE)$(CC) $(LOOKUP_$*_INCLUDE) $(LOOKUP_$*_LIBS) -DDYNLOOKUP $(CFLAGS_DYNAMIC) $(CFLAGS) $(INCLUDE) $(DLFLAGS) $*.c -o $@
+
+lf_check_file.o: $(PHDRS) lf_check_file.c  lf_functions.h
+lf_quote.o:      $(PHDRS) lf_quote.c       lf_functions.h
+lf_sqlperform.o: $(PHDRS) lf_sqlperform.c  lf_functions.h
+
+cdb.o:           $(PHDRS) cdb.c
+dbmdb.o:         $(PHDRS) dbmdb.c
+dnsdb.o:         $(PHDRS) dnsdb.c
+dsearch.o:       $(PHDRS) dsearch.c
+ibase.o:         $(PHDRS) ibase.c
+ldap.o:          $(PHDRS) ldap.c
+lsearch.o:       $(PHDRS) lsearch.c
+mysql.o:         $(PHDRS) mysql.c
+nis.o:           $(PHDRS) nis.c
+nisplus.o:       $(PHDRS) nisplus.c
+oracle.o:        $(PHDRS) oracle.c
+passwd.o:        $(PHDRS) passwd.c
+pgsql.o:         $(PHDRS) pgsql.c
+redis.o:         $(PHDRS) redis.c
+spf.o:           $(PHDRS) spf.c
+sqlite.o:        $(PHDRS) sqlite.c
+testdb.o:        $(PHDRS) testdb.c
+whoson.o:        $(PHDRS) whoson.c
+
+cdb.so:           $(PHDRS) cdb.c
+dbmdb.so:         $(PHDRS) dbmdb.c
+dnsdb.so:         $(PHDRS) dnsdb.c
+dsearch.so:       $(PHDRS) dsearch.c
+ibase.so:         $(PHDRS) ibase.c
+ldap.so:          $(PHDRS) ldap.c
+lsearch.so:       $(PHDRS) lsearch.c
+mysql.so:         $(PHDRS) mysql.c
+nis.so:           $(PHDRS) nis.c
+nisplus.so:       $(PHDRS) nisplus.c
+oracle.so:        $(PHDRS) oracle.c
+passwd.so:        $(PHDRS) passwd.c
+pgsql.so:         $(PHDRS) pgsql.c
+redis.so:         $(PHDRS) redis.c
+spf.so:           $(PHDRS) spf.c
+sqlite.so:        $(PHDRS) sqlite.c
+testdb.so:        $(PHDRS) testdb.c
+whoson.so:        $(PHDRS) whoson.c
+
+# End
diff --git a/src/lookups/README b/src/lookups/README
new file mode 100644 (file)
index 0000000..98905dc
--- /dev/null
@@ -0,0 +1,172 @@
+LOOKUPS
+-------
+
+Each lookup type is implemented by 6 functions, xxx_open(), xxx_check(),
+xxx_find(), xxx_close(), xxx_tidy(), and xxx_quote(), where xxx is the name of
+the lookup type (e.g. lsearch, dbm, or whatever). In addition, there is
+a version reporting function used to trace compile-vs-runtime conflicts and
+to help administrators ensure that the modules from the correct build are
+in use by the main binary.
+
+The xxx_check(), xxx_close(), xxx_tidy(), and xxx_quote() functions need not
+exist. There is a table in drtables.c which links the lookup names to the
+various sets of functions, with NULL entries for any that don't exist. When
+the code for a lookup type is omitted from the binary, all its entries are
+NULL.
+
+One of the fields in the table contains flags describing the kind of lookup.
+These are
+
+  lookup_querystyle
+
+This is a "query style" lookup without a file name, as opposed to the "single
+key" style, where the key is associated with a "file name".
+
+  lookup_absfile
+
+For single key lookups, this means that the file name must be an absolute path.
+It is set for lsearch and dbm, but not for NIS, for example.
+
+  lookup_absfilequery
+
+This is a query-style lookup that must start with an absolute file name. For
+example, the sqlite lookup is of this type.
+
+When a single-key or absfilequery lookup file is opened, the handle returned by
+the xxx_open() function is saved, along with the file name and lookup type, in
+a tree. The xxx_close() function is not called when the first lookup is
+completed. If there are subsequent lookups of the same type that quote the same
+file name, xxx_open() isn't called; instead the cached handle is re-used.
+
+Exim calls the function search_tidyup() at strategic points in its processing
+(e.g. after all routing and directing has been done) and this function walks
+the tree and calls the xxx_close() functions for all the cached handles.
+
+Query-style lookups don't have the concept of an open file that can be cached
+this way. Of course, the local code for the lookup can manage its own caching
+information in any way it pleases. This means that the xxx_close()
+function, even it it exists, is never called. However, if an xxx_tidy()
+function exists, it is called once whenever Exim calls search_tidyup().
+
+A single-key lookup type may also have an xxx_tidy() function, which is called
+by search_tidyup() after all cached handles have been closed via the
+xxx_close() function.
+
+The lookup functions are wrapped into a special store pool (POOL_SEARCH). You
+can safely use store_get to allocate store for your handle caching. The store
+will be reset after all xxx_tidy() functions are called.
+
+The function interfaces are as follows:
+
+
+xxx_open()
+----------
+
+This function is called to initiate the lookup. For things that involve files
+it should do a real open; for other kinds of lookup it may do nothing at all.
+The arguments are:
+
+  uschar *filename    the name of the "file" to open, for non-query-style
+                        lookups; NULL for query-style lookups
+  uschar **errmsg     where to put an error message if there is a problem
+
+The yield of xxx_open() is a void * value representing the open file or
+database. For real files is is normally the FILE or DBM value. For other
+kinds of lookup, if there is no natural value to use, (-1) is recommended.
+The value should not be NULL (or 0) as that is taken to indicate failure of
+the xxx_open() function. For single-key lookups, the handle is cached along
+with the filename and type, and may be used for several lookups.
+
+
+xxx_check()
+-----------
+
+If this function exists, it is called after a successful open to check on the
+ownership and mode of the file(s). The arguments are:
+
+  void *handle        the handle passed back from xxx_open()
+  uschar *filename    the filename passed to xxx_open()
+  int modemask        mode bits that must not be set
+  int *owners         permitted owners of the file(s)
+  int *owngroups      permitted group owners of the file(s)
+  uschar **errmsg     where to put an error message if there is a problem
+
+In the owners and owngroups vectors, the first element is the count of the
+remaining elements. There is a common function that can be called to check
+a file:
+
+int search_check_file(int fd, char *filename, int modemask, int *owners,
+  int *owngroups, char *type, char **errmsg);
+
+If fd is >= 0, it is checked using fstat(), and filename is used only in
+error messages. If fd is < 0 then filename is checked using stat(). The yield
+is zero if all is well, +1 if the mode or uid or gid is wrong, or -1 if the
+stat() fails.
+
+The yield of xxx_check() is TRUE if all is well, FALSE otherwise. The
+function should not close the file(s) on failure. That is done from outside
+by calling the xxx_close() function.
+
+
+xxx_find()
+----------
+
+This is called to search an open file/database. The result is OK, FAIL, or
+DEFER. The arguments are:
+
+  void *handle        the handle passed back from xxx_open()
+  uschar *filename    the filename passed to xxx_open() (NULL for querystyle)
+  uschar *keyquery    the key to look up, or query to process, zero-terminated
+  int  length         the length of the key
+  uschar **result     point to the yield, in dynamic store, on success
+  uschar **errmsg     where to put an error message on failure;
+                      this is initially set to "", and should be left
+                      as that for a standard "entry not found" error
+  BOOL *do_cache      the lookup should set this to FALSE when it changes data.
+                      This is TRUE by default. When set to FALSE the cache tree
+                      of the current search handle will be cleaned and the
+                      current result will NOT be cached. Currently the mysql
+                      and pgsql lookups use this when UPDATE/INSERT queries are
+                      executed.
+
+Even though the key is zero-terminated, the length is passed because in the
+common case it has been computed already and is often needed.
+
+
+xxx_close()
+-----------
+
+This is called for single-key lookups when a file is finished with. There is no
+yield, and the only argument is the handle that was passed back from
+xxx_open(). It is not called for query style lookups.
+
+
+xxx_tidy()
+----------
+
+This function is called once at the end of search_tidyup() for every lookup
+type for which it exists.
+
+
+xxx_quote()
+-----------
+
+This is called by the string expansion code for expansions of the form
+${quote_xxx:<string>}, if it exists. If not, the expansion code makes no change
+to the string. The function must apply any quoting rules that are specific to
+the lookup, and return a pointer to the revised string. If quoting is not
+needed, it can return its single argument, which is a uschar *. This function
+does NOT use the POOL_SEARCH store, because it's usually never called from any
+lookup code.
+
+xxx_report_version()
+--------------------
+
+This is called to report diagnostic information to a file stream.
+Typically it would report both compile-time and run-time version information.
+The arguments are:
+
+  FILE *stream    where to fprintf() the data to
+
+
+****
diff --git a/src/lookups/cdb.c b/src/lookups/cdb.c
new file mode 100644 (file)
index 0000000..fda14fb
--- /dev/null
@@ -0,0 +1,474 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/*
+ * Exim - CDB database lookup module
+ * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ *
+ * Copyright (c) 1998 Nigel Metheringham, Planet Online Ltd
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * --------------------------------------------------------------
+ * Modified by PH for Exim 4:
+ *   Changed over to using unsigned chars
+ *   Makes use of lf_check_file() for file checking
+ * --------------------------------------------------------------
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
+ * 02111-1307, USA.
+ *
+ *
+ * This code implements Dan Bernstein's Constant DataBase (cdb) spec.
+ * Information, the spec and sample code for cdb can be obtained from
+ *      http://www.pobox.com/~djb/cdb.html
+ *
+ * This implementation borrows some code from Dan Bernstein's
+ * implementation (which has no license restrictions applied to it).
+ * This (read-only) implementation is completely contained within
+ * cdb.[ch] it does *not* link against an external cdb library.
+ *
+ *
+ * There are 2 varients included within this code.  One uses MMAP and
+ * should give better performance especially for multiple lookups on a
+ * modern machine.  The other is the default implementation which is
+ * used in the case where the MMAP fails or if MMAP was not compiled
+ * in.  this implementation is the same as the original reference cdb
+ * implementation.  The MMAP version is compiled in if the HAVE_MMAP
+ * preprocessor define is defined - this should be set in the system
+ * specific os.h file.
+ *
+ */
+
+
+#include "../exim.h"
+#include "lf_functions.h"
+
+#ifdef HAVE_MMAP
+#  include <sys/mman.h>
+/* Not all implementations declare MAP_FAILED */
+#  ifndef MAP_FAILED
+#    define MAP_FAILED ((void *) -1)
+#  endif /* MAP_FAILED */
+#endif /* HAVE_MMAP */
+
+
+#define CDB_HASH_SPLIT 256     /* num pieces the hash table is split into */
+#define CDB_HASH_MASK  255     /* mask to and off split value */
+#define CDB_HASH_ENTRY 8       /* how big each offset it */
+#define CDB_HASH_TABLE (CDB_HASH_SPLIT * CDB_HASH_ENTRY)
+
+/* State information for cdb databases that are open NB while the db
+ * is open its contents will not change (cdb dbs are normally updated
+ * atomically by renaming).  However the lifetime of one of these
+ * state structures should be limited - ie a long running daemon
+ * that opens one may hit problems....
+ */
+
+struct cdb_state {
+  int     fileno;
+  off_t   filelen;
+  uschar *cdb_map;
+  uschar *cdb_offsets;
+};
+
+/* 32 bit unsigned type - this is an int on all modern machines */
+typedef unsigned int uint32;
+
+/*
+ * cdb_hash()
+ * Internal function to make hash value */
+
+static uint32
+cdb_hash(uschar *buf, unsigned int len)
+{
+  uint32 h;
+
+  h = 5381;
+  while (len) {
+    --len;
+    h += (h << 5);
+    h ^= (uint32) *buf++;
+  }
+  return h;
+}
+
+/*
+ * cdb_bread()
+ * Internal function to read len bytes from disk, coping with oddities */
+
+static int
+cdb_bread(int fd,
+         uschar *buf,
+         int len)
+{
+  int r;
+  while (len > 0) {
+    do
+      r = Uread(fd,buf,len);
+    while ((r == -1) && (errno == EINTR));
+    if (r == -1) return -1;
+    if (r == 0) { errno = EIO; return -1; }
+    buf += r;
+    len -= r;
+  }
+  return 0;
+}
+
+/*
+ * cdb_bread()
+ * Internal function to parse 4 byte number (endian independant) */
+
+static uint32
+cdb_unpack(uschar *buf)
+{
+  uint32 num;
+  num =  buf[3]; num <<= 8;
+  num += buf[2]; num <<= 8;
+  num += buf[1]; num <<= 8;
+  num += buf[0];
+  return num;
+}
+
+static void cdb_close(void *handle);
+
+static void *
+cdb_open(uschar *filename,
+         uschar **errmsg)
+{
+  int fileno;
+  struct cdb_state *cdbp;
+  struct stat statbuf;
+  void * mapbuf;
+
+  fileno = Uopen(filename, O_RDONLY, 0);
+  if (fileno == -1) {
+    int save_errno = errno;
+    *errmsg = string_open_failed(errno, "%s for cdb lookup", filename);
+    errno = save_errno;
+    return NULL;
+  }
+
+  if (fstat(fileno, &statbuf) == 0) {
+    /* If this is a valid file, then it *must* be at least
+     * CDB_HASH_TABLE bytes long */
+    if (statbuf.st_size < CDB_HASH_TABLE) {
+      int save_errno = errno;
+      *errmsg = string_open_failed(errno,
+                                  "%s too short for cdb lookup",
+                                  filename);
+      errno = save_errno;
+      return NULL;
+    }
+  } else {
+    int save_errno = errno;
+    *errmsg = string_open_failed(errno,
+                                "fstat(%s) failed - cannot do cdb lookup",
+                                filename);
+    errno = save_errno;
+    return NULL;
+  }
+
+  /* Having got a file open we need the structure to put things in */
+  cdbp = store_get(sizeof(struct cdb_state));
+  /* store_get() does not return if memory was not available... */
+  /* preload the structure.... */
+  cdbp->fileno = fileno;
+  cdbp->filelen = statbuf.st_size;
+  cdbp->cdb_map = NULL;
+  cdbp->cdb_offsets = NULL;
+
+  /* if we are allowed to we use mmap here.... */
+#ifdef HAVE_MMAP
+  mapbuf = mmap(NULL,
+               statbuf.st_size,
+               PROT_READ,
+               MAP_SHARED,
+               fileno,
+               0);
+  if (mapbuf != MAP_FAILED) {
+    /* We have an mmap-ed section.  Now we can just use it */
+    cdbp->cdb_map = mapbuf;
+    /* The offsets can be set to the same value since they should
+     * effectively be cached as well
+     */
+    cdbp->cdb_offsets = mapbuf;
+
+    /* Now return the state struct */
+    return(cdbp);
+  } else {
+    /* If we got here the map failed.  Basically we can ignore
+     * this since we fall back to slower methods....
+     * However lets debug log it...
+     */
+    DEBUG(D_lookup) debug_printf("cdb mmap failed - %d\n", errno);
+  }
+#endif /* HAVE_MMAP */
+
+  /* In this case we have either not got MMAP allowed, or it failed */
+
+  /* get a buffer to stash the basic offsets in - this should speed
+   * things up a lot - especially on multiple lookups */
+  cdbp->cdb_offsets = store_get(CDB_HASH_TABLE);
+
+  /* now fill the buffer up... */
+  if (cdb_bread(fileno, cdbp->cdb_offsets, CDB_HASH_TABLE) == -1) {
+    /* read of hash table failed, oh dear, oh.....
+     * time to give up I think....
+     * call the close routine (deallocs the memory), and return NULL */
+    *errmsg = string_open_failed(errno,
+                                "cannot read header from %s for cdb lookup",
+                                filename);
+    cdb_close(cdbp);
+    return NULL;
+  }
+
+  /* Everything else done - return the cache structure */
+  return cdbp;
+}
+
+
+
+/*************************************************
+*             Check entry point                  *
+*************************************************/
+
+static BOOL
+cdb_check(void *handle,
+         uschar *filename,
+         int modemask,
+         uid_t *owners,
+         gid_t *owngroups,
+         uschar **errmsg)
+{
+  struct cdb_state * cdbp = handle;
+  return lf_check_file(cdbp->fileno,
+                       filename,
+                       S_IFREG,
+                       modemask,
+                       owners,
+                       owngroups,
+                       "cdb",
+                       errmsg) == 0;
+}
+
+
+
+/*************************************************
+*              Find entry point                  *
+*************************************************/
+
+static int
+cdb_find(void *handle,
+        uschar *filename,
+        uschar *keystring,
+        int  key_len,
+        uschar **result,
+        uschar **errmsg,
+        BOOL *do_cache)
+{
+  struct cdb_state * cdbp = handle;
+  uint32 item_key_len,
+    item_dat_len,
+    key_hash,
+    item_hash,
+    item_posn,
+    cur_offset,
+    end_offset,
+    hash_offset_entry,
+    hash_offset,
+    hash_offlen,
+    hash_slotnm;
+  int loop;
+
+  /* Keep picky compilers happy */
+  do_cache = do_cache;
+
+  key_hash = cdb_hash((uschar *)keystring, key_len);
+
+  hash_offset_entry = CDB_HASH_ENTRY * (key_hash & CDB_HASH_MASK);
+  hash_offset = cdb_unpack(cdbp->cdb_offsets + hash_offset_entry);
+  hash_offlen = cdb_unpack(cdbp->cdb_offsets + hash_offset_entry + 4);
+
+  /* If the offset length is zero this key cannot be in the file */
+  if (hash_offlen == 0) {
+    return FAIL;
+  }
+  hash_slotnm = (key_hash >> 8) % hash_offlen;
+
+  /* check to ensure that the file is not corrupt
+   * if the hash_offset + (hash_offlen * CDB_HASH_ENTRY) is longer
+   * than the file, then we have problems.... */
+  if ((hash_offset + (hash_offlen * CDB_HASH_ENTRY)) > cdbp->filelen) {
+    *errmsg = string_sprintf("cdb: corrupt cdb file %s (too short)",
+                            filename);
+    DEBUG(D_lookup) debug_printf("%s\n", *errmsg);
+    return DEFER;
+  }
+
+  cur_offset = hash_offset + (hash_slotnm * CDB_HASH_ENTRY);
+  end_offset = hash_offset + (hash_offlen * CDB_HASH_ENTRY);
+  /* if we are allowed to we use mmap here.... */
+#ifdef HAVE_MMAP
+  /* make sure the mmap was OK */
+  if (cdbp->cdb_map != NULL) {
+    uschar * cur_pos = cur_offset + cdbp->cdb_map;
+    uschar * end_pos = end_offset + cdbp->cdb_map;
+    for (loop = 0; (loop < hash_offlen); ++loop) {
+      item_hash = cdb_unpack(cur_pos);
+      cur_pos += 4;
+      item_posn = cdb_unpack(cur_pos);
+      cur_pos += 4;
+      /* if the position is zero then we have a definite miss */
+      if (item_posn == 0)
+       return FAIL;
+
+      if (item_hash == key_hash) {
+       /* matching hash value */
+       uschar * item_ptr = cdbp->cdb_map + item_posn;
+       item_key_len = cdb_unpack(item_ptr);
+       item_ptr += 4;
+       item_dat_len = cdb_unpack(item_ptr);
+       item_ptr += 4;
+       /* check key length matches */
+       if (item_key_len == key_len) {
+         /* finally check if key matches */
+         if (Ustrncmp(keystring, item_ptr, key_len) == 0) {
+           /* we have a match....
+            * make item_ptr point to data */
+           item_ptr += item_key_len;
+           /* ... and the returned result */
+           *result = store_get(item_dat_len + 1);
+           memcpy(*result, item_ptr, item_dat_len);
+           (*result)[item_dat_len] = 0;
+           return OK;
+         }
+       }
+      }
+      /* handle warp round of table */
+      if (cur_pos == end_pos)
+       cur_pos = cdbp->cdb_map + hash_offset;
+    }
+    /* looks like we failed... */
+    return FAIL;
+  }
+#endif /* HAVE_MMAP */
+  for (loop = 0; (loop < hash_offlen); ++loop) {
+    uschar packbuf[8];
+    if (lseek(cdbp->fileno, (off_t) cur_offset,SEEK_SET) == -1) return DEFER;
+    if (cdb_bread(cdbp->fileno, packbuf,8) == -1) return DEFER;
+    item_hash = cdb_unpack(packbuf);
+    item_posn = cdb_unpack(packbuf + 4);
+    /* if the position is zero then we have a definite miss */
+    if (item_posn == 0)
+      return FAIL;
+
+    if (item_hash == key_hash) {
+      /* matching hash value */
+      if (lseek(cdbp->fileno, (off_t) item_posn, SEEK_SET) == -1) return DEFER;
+      if (cdb_bread(cdbp->fileno, packbuf, 8) == -1) return DEFER;
+      item_key_len = cdb_unpack(packbuf);
+      /* check key length matches */
+      if (item_key_len == key_len) {
+       /* finally check if key matches */
+       uschar * item_key = store_get(key_len);
+       if (cdb_bread(cdbp->fileno, item_key, key_len) == -1) return DEFER;
+       if (Ustrncmp(keystring, item_key, key_len) == 0) {
+         /* Reclaim some store */
+         store_reset(item_key);
+         /* matches - get data length */
+         item_dat_len = cdb_unpack(packbuf + 4);
+         /* then we build a new result string */
+         *result = store_get(item_dat_len + 1);
+         if (cdb_bread(cdbp->fileno, *result, item_dat_len) == -1)
+           return DEFER;
+         (*result)[item_dat_len] = 0;
+         return OK;
+       }
+       /* Reclaim some store */
+       store_reset(item_key);
+      }
+    }
+    cur_offset += 8;
+
+    /* handle warp round of table */
+    if (cur_offset == end_offset)
+      cur_offset = hash_offset;
+  }
+  return FAIL;
+}
+
+
+
+/*************************************************
+*              Close entry point                 *
+*************************************************/
+
+/* See local README for interface description */
+
+static void
+cdb_close(void *handle)
+{
+struct cdb_state * cdbp = handle;
+
+#ifdef HAVE_MMAP
+ if (cdbp->cdb_map) {
+   munmap(CS cdbp->cdb_map, cdbp->filelen);
+   if (cdbp->cdb_map == cdbp->cdb_offsets)
+     cdbp->cdb_offsets = NULL;
+ }
+#endif /* HAVE_MMAP */
+
+ (void)close(cdbp->fileno);
+}
+
+
+
+/*************************************************
+*         Version reporting entry point          *
+*************************************************/
+
+/* See local README for interface description. */
+
+#include "../version.h"
+
+void
+cdb_version_report(FILE *f)
+{
+#ifdef DYNLOOKUP
+fprintf(f, "Library version: CDB: Exim version %s\n", EXIM_VERSION_STR);
+#endif
+}
+
+
+lookup_info cdb_lookup_info = {
+  US"cdb",                       /* lookup name */
+  lookup_absfile,                /* uses absolute file name */
+  cdb_open,                      /* open function */
+  cdb_check,                     /* check function */
+  cdb_find,                      /* find function */
+  cdb_close,                     /* close function */
+  NULL,                          /* no tidy function */
+  NULL,                          /* no quoting function */
+  cdb_version_report             /* version reporting */
+};
+
+#ifdef DYNLOOKUP
+#define cdb_lookup_module_info _lookup_module_info
+#endif
+
+static lookup_info *_lookup_list[] = { &cdb_lookup_info };
+lookup_module_info cdb_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 1 };
+
+/* End of lookups/cdb.c */
diff --git a/src/lookups/dbmdb.c b/src/lookups/dbmdb.c
new file mode 100644 (file)
index 0000000..3dec7a7
--- /dev/null
@@ -0,0 +1,283 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2012 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+#include "../exim.h"
+#include "lf_functions.h"
+
+
+/*************************************************
+*              Open entry point                  *
+*************************************************/
+
+/* See local README for interface description */
+
+static void *
+dbmdb_open(uschar *filename, uschar **errmsg)
+{
+EXIM_DB *yield = NULL;
+EXIM_DBOPEN(filename, O_RDONLY, 0, &yield);
+if (yield == NULL)
+  {
+  int save_errno = errno;
+  *errmsg = string_open_failed(errno, "%s as a %s file", filename, EXIM_DBTYPE);
+  errno = save_errno;
+  }
+return yield;
+}
+
+
+
+/*************************************************
+*             Check entry point                  *
+*************************************************/
+
+/* This needs to know more about the underlying files than is good for it!
+We need to know what the real file names are in order to check the owners and
+modes. If USE_DB is set, we know it is Berkeley DB, which uses an unmodified
+file name. If USE_TDB or USE_GDBM is set, we know it is tdb or gdbm, which do
+the same. Otherwise, for safety, we have to check for x.db or x.dir and x.pag.
+*/
+
+static BOOL
+dbmdb_check(void *handle, uschar *filename, int modemask, uid_t *owners,
+  gid_t *owngroups, uschar **errmsg)
+{
+int rc;
+handle = handle;    /* Keep picky compilers happy */
+
+#if defined(USE_DB) || defined(USE_TDB) || defined(USE_GDBM)
+rc = lf_check_file(-1, filename, S_IFREG, modemask, owners, owngroups,
+  "dbm", errmsg);
+#else
+  {
+  uschar filebuffer[256];
+  (void)sprintf(CS filebuffer, "%.250s.db", filename);
+  rc = lf_check_file(-1, filebuffer, S_IFREG, modemask, owners, owngroups,
+    "dbm", errmsg);
+  if (rc < 0)        /* stat() failed */
+    {
+    (void)sprintf(CS filebuffer, "%.250s.dir", filename);
+    rc = lf_check_file(-1, filebuffer, S_IFREG, modemask, owners, owngroups,
+      "dbm", errmsg);
+    if (rc == 0)     /* x.dir was OK */
+      {
+      (void)sprintf(CS filebuffer, "%.250s.pag", filename);
+      rc = lf_check_file(-1, filebuffer, S_IFREG, modemask, owners, owngroups,
+        "dbm", errmsg);
+      }
+    }
+  }
+#endif
+
+return rc == 0;
+}
+
+
+
+/*************************************************
+*              Find entry point                  *
+*************************************************/
+
+/* See local README for interface description. This function adds 1 to
+the keylength in order to include the terminating zero. */
+
+static int
+dbmdb_find(void *handle, uschar *filename, uschar *keystring, int length,
+  uschar **result, uschar **errmsg, BOOL *do_cache)
+{
+EXIM_DB *d = (EXIM_DB *)handle;
+EXIM_DATUM key, data;
+
+filename = filename;    /* Keep picky compilers happy */
+errmsg = errmsg;
+do_cache = do_cache;
+
+EXIM_DATUM_INIT(key);               /* Some DBM libraries require datums to */
+EXIM_DATUM_INIT(data);              /* be cleared before use. */
+EXIM_DATUM_DATA(key) = CS keystring;
+EXIM_DATUM_SIZE(key) = length + 1;
+
+if (EXIM_DBGET(d, key, data))
+  {
+  *result = string_copyn(US EXIM_DATUM_DATA(data), EXIM_DATUM_SIZE(data));
+  EXIM_DATUM_FREE(data);            /* Some DBM libraries need a free() call */
+  return OK;
+  }
+return FAIL;
+}
+
+
+
+/*************************************************
+*      Find entry point - no zero on key         *
+*************************************************/
+
+/* See local README for interface description */
+
+int
+static dbmnz_find(void *handle, uschar *filename, uschar *keystring, int length,
+  uschar **result, uschar **errmsg, BOOL *do_cache)
+{
+return dbmdb_find(handle, filename, keystring, length-1, result, errmsg,
+  do_cache);
+}
+
+
+
+/*************************************************
+*     Find entry point - zero-joined list key    *
+*************************************************/
+
+/*
+ * The parameter passed as a key is a list in normal Exim list syntax.
+ * The elements of that list are joined together on NUL, with no trailing
+ * NUL, to form the key.
+ */
+
+static int
+dbmjz_find(void *handle, uschar *filename, uschar *keystring, int length,
+  uschar **result, uschar **errmsg, BOOL *do_cache)
+{
+uschar *key_item, *key_buffer, *key_p;
+uschar *key_elems = keystring;
+int buflen, bufleft, key_item_len, sep = 0;
+
+/* To a first approximation, the size of the lookup key needs to be about,
+or less than, the length of the delimited list passed in + 1. */
+
+buflen = length + 3;
+key_buffer = store_get(buflen);
+
+key_buffer[0] = '\0';
+
+key_p = key_buffer;
+bufleft = buflen;
+
+/* In all cases of an empty list item, we can set 1 and advance by 1 and then
+pick up the trailing NUL from the previous list item, EXCEPT when at the
+beginning of the output string, in which case we need to supply that NUL
+ourselves.  */
+while ((key_item = string_nextinlist(&key_elems, &sep, key_p, bufleft)) != NULL)
+  {
+  key_item_len = Ustrlen(key_item) + 1;
+  if (key_item_len == 1)
+    {
+    key_p[0] = '\0';
+    if (key_p == key_buffer)
+      {
+      key_p[1] = '\0';
+      key_item_len += 1;
+      }
+    }
+
+  bufleft -= key_item_len;
+  if (bufleft <= 0)
+    {
+    /* The string_nextinlist() will stop at buffer size, but we should always
+    have at least 1 character extra, so some assumption has failed. */
+    *errmsg = string_copy(US"Ran out of buffer space for joining elements");
+    return DEFER;
+    }
+  key_p += key_item_len;
+  }
+
+if (key_p == key_buffer)
+  {
+  *errmsg = string_copy(US"empty list key");
+  return FAIL;
+  }
+
+/* We do not pass in the final NULL; if needed, the list should include an
+empty element to put one in. Boundary: key length 1, is a NULL */
+key_item_len = key_p - key_buffer - 1;
+
+DEBUG(D_lookup) debug_printf("NUL-joined key length: %d\n", key_item_len);
+
+/* beware that dbmdb_find() adds 1 to length to get back terminating NUL, so
+because we've calculated the real length, we need to subtract one more here */
+return dbmdb_find(handle, filename,
+    key_buffer, key_item_len - 1,
+    result, errmsg, do_cache);
+}
+
+
+
+/*************************************************
+*              Close entry point                 *
+*************************************************/
+
+/* See local README for interface description */
+
+void
+static dbmdb_close(void *handle)
+{
+EXIM_DBCLOSE((EXIM_DB *)handle);
+}
+
+
+
+/*************************************************
+*         Version reporting entry point          *
+*************************************************/
+
+/* See local README for interface description. */
+
+#include "../version.h"
+
+void
+dbm_version_report(FILE *f)
+{
+#ifdef DYNLOOKUP
+fprintf(f, "Library version: DBM: Exim version %s\n", EXIM_VERSION_STR);
+#endif
+}
+
+
+lookup_info dbm_lookup_info = {
+  US"dbm",                       /* lookup name */
+  lookup_absfile,                /* uses absolute file name */
+  dbmdb_open,                    /* open function */
+  dbmdb_check,                   /* check function */
+  dbmdb_find,                    /* find function */
+  dbmdb_close,                   /* close function */
+  NULL,                          /* no tidy function */
+  NULL,                          /* no quoting function */
+  dbm_version_report             /* version reporting */
+};
+
+lookup_info dbmz_lookup_info = {
+  US"dbmnz",                     /* lookup name */
+  lookup_absfile,                /* uses absolute file name */
+  dbmdb_open,      /* sic */     /* open function */
+  dbmdb_check,     /* sic */     /* check function */
+  dbmnz_find,                    /* find function */
+  dbmdb_close,     /* sic */     /* close function */
+  NULL,                          /* no tidy function */
+  NULL,                          /* no quoting function */
+  NULL                           /* no version reporting (redundant) */
+};
+
+lookup_info dbmjz_lookup_info = {
+  US"dbmjz",                     /* lookup name */
+  lookup_absfile,                /* uses absolute file name */
+  dbmdb_open,      /* sic */     /* open function */
+  dbmdb_check,     /* sic */     /* check function */
+  dbmjz_find,                    /* find function */
+  dbmdb_close,     /* sic */     /* close function */
+  NULL,                          /* no tidy function */
+  NULL,                          /* no quoting function */
+  NULL                           /* no version reporting (redundant) */
+};
+
+#ifdef DYNLOOKUP
+#define dbmdb_lookup_module_info _lookup_module_info
+#endif
+
+static lookup_info *_lookup_list[] = { &dbm_lookup_info, &dbmz_lookup_info, &dbmjz_lookup_info };
+lookup_module_info dbmdb_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 3 };
+
+/* End of lookups/dbmdb.c */
diff --git a/src/lookups/dnsdb.c b/src/lookups/dnsdb.c
new file mode 100644 (file)
index 0000000..fde98b9
--- /dev/null
@@ -0,0 +1,589 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2014 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+#include "../exim.h"
+#include "lf_functions.h"
+
+
+
+/* Ancient systems (e.g. SunOS4) don't appear to have T_TXT defined in their
+header files. */
+
+#ifndef T_TXT
+#define T_TXT 16
+#endif
+
+/* Many systems do not have T_SPF. */
+#ifndef T_SPF
+#define T_SPF 99
+#endif
+
+/* New TLSA record for DANE */
+#ifndef T_TLSA
+#define T_TLSA 52
+#endif
+
+/* Table of recognized DNS record types and their integer values. */
+
+static const char *type_names[] = {
+  "a",
+#if HAVE_IPV6
+  "a+",
+  "aaaa",
+  #ifdef SUPPORT_A6
+  "a6",
+  #endif
+#endif
+  "cname",
+  "csa",
+  "mx",
+  "mxh",
+  "ns",
+  "ptr",
+  "spf",
+  "srv",
+  "tlsa",
+  "txt",
+  "zns"
+};
+
+static int type_values[] = {
+  T_A,
+#if HAVE_IPV6
+  T_ADDRESSES,     /* Private type for AAAA + A */
+  T_AAAA,
+  #ifdef SUPPORT_A6
+  T_A6,
+  #endif
+#endif
+  T_CNAME,
+  T_CSA,     /* Private type for "Client SMTP Authorization". */
+  T_MX,
+  T_MXH,     /* Private type for "MX hostnames" */
+  T_NS,
+  T_PTR,
+  T_SPF,
+  T_SRV,
+  T_TLSA,
+  T_TXT,
+  T_ZNS      /* Private type for "zone nameservers" */
+};
+
+
+/*************************************************
+*              Open entry point                  *
+*************************************************/
+
+/* See local README for interface description. */
+
+static void *
+dnsdb_open(uschar *filename, uschar **errmsg)
+{
+filename = filename;   /* Keep picky compilers happy */
+errmsg = errmsg;       /* Ditto */
+return (void *)(-1);   /* Any non-0 value */
+}
+
+
+
+/*************************************************
+*           Find entry point for dnsdb           *
+*************************************************/
+
+/* See local README for interface description. The query in the "keystring" may
+consist of a number of parts.
+
+(a) If the first significant character is '>' then the next character is the
+separator character that is used when multiple records are found. The default
+separator is newline.
+
+(b) If the next character is ',' then the next character is the separator
+character used for multiple items of text in "TXT" records. Alternatively,
+if the next character is ';' then these multiple items are concatenated with
+no separator. With neither of these options specified, only the first item
+is output.  Similarly for "SPF" records, but the default for joining multiple
+items in one SPF record is the empty string, for direct concatenation.
+
+(c) If the next sequence of characters is 'defer_FOO' followed by a comma,
+the defer behaviour is set to FOO. The possible behaviours are: 'strict', where
+any defer causes the whole lookup to defer; 'lax', where a defer causes the
+whole lookup to defer only if none of the DNS queries succeeds; and 'never',
+where all defers are as if the lookup failed. The default is 'lax'.
+
+(d) Another optional comma-sep field: 'dnssec_FOO', with 'strict', 'lax'
+and 'never' (default); can appear before or after (c).  The meanings are
+require, try and don't-try dnssec respectively.
+
+(e) If the next sequence of characters is a sequence of letters and digits
+followed by '=', it is interpreted as the name of the DNS record type. The
+default is "TXT".
+
+(f) Then there follows list of domain names. This is a generalized Exim list,
+which may start with '<' in order to set a specific separator. The default
+separator, as always, is colon. */
+
+static int
+dnsdb_find(void *handle, uschar *filename, uschar *keystring, int length,
+  uschar **result, uschar **errmsg, BOOL *do_cache)
+{
+int rc;
+int size = 256;
+int ptr = 0;
+int sep = 0;
+int defer_mode = PASS;
+int dnssec_mode = OK;
+int type;
+int failrc = FAIL;
+uschar *outsep = US"\n";
+uschar *outsep2 = NULL;
+uschar *equals, *domain, *found;
+uschar buffer[256];
+
+/* Because we're the working in the search pool, we try to reclaim as much
+store as possible later, so we preallocate the result here */
+
+uschar *yield = store_get(size);
+
+dns_record *rr;
+dns_answer dnsa;
+dns_scan dnss;
+
+handle = handle;           /* Keep picky compilers happy */
+filename = filename;
+length = length;
+do_cache = do_cache;
+
+/* If the string starts with '>' we change the output separator.
+If it's followed by ';' or ',' we set the TXT output separator. */
+
+while (isspace(*keystring)) keystring++;
+if (*keystring == '>')
+  {
+  outsep = keystring + 1;
+  keystring += 2;
+  if (*keystring == ',')
+    {
+    outsep2 = keystring + 1;
+    keystring += 2;
+    }
+  else if (*keystring == ';')
+    {
+    outsep2 = US"";
+    keystring++;
+    }
+  while (isspace(*keystring)) keystring++;
+  }
+
+/* Check for a modifier keyword. */
+
+while (  strncmpic(keystring, US"defer_", 6) == 0
+      || strncmpic(keystring, US"dnssec_", 7) == 0
+      )
+  {
+  if (strncmpic(keystring, US"defer_", 6) == 0)
+    {
+    keystring += 6;
+    if (strncmpic(keystring, US"strict", 6) == 0)
+      {
+      defer_mode = DEFER;
+      keystring += 6;
+      }
+    else if (strncmpic(keystring, US"lax", 3) == 0)
+      {
+      defer_mode = PASS;
+      keystring += 3;
+      }
+    else if (strncmpic(keystring, US"never", 5) == 0)
+      {
+      defer_mode = OK;
+      keystring += 5;
+      }
+    else
+      {
+      *errmsg = US"unsupported dnsdb defer behaviour";
+      return DEFER;
+      }
+    }
+  else
+    {
+    keystring += 7;
+    if (strncmpic(keystring, US"strict", 6) == 0)
+      {
+      dnssec_mode = DEFER;
+      keystring += 6;
+      }
+    else if (strncmpic(keystring, US"lax", 3) == 0)
+      {
+      dnssec_mode = PASS;
+      keystring += 3;
+      }
+    else if (strncmpic(keystring, US"never", 5) == 0)
+      {
+      dnssec_mode = OK;
+      keystring += 5;
+      }
+    else
+      {
+      *errmsg = US"unsupported dnsdb dnssec behaviour";
+      return DEFER;
+      }
+    }
+  while (isspace(*keystring)) keystring++;
+  if (*keystring++ != ',')
+    {
+    *errmsg = US"dnsdb modifier syntax error";
+    return DEFER;
+    }
+  while (isspace(*keystring)) keystring++;
+  }
+
+/* Figure out the "type" value if it is not T_TXT.
+If the keystring contains an = this must be preceded by a valid type name. */
+
+type = T_TXT;
+if ((equals = Ustrchr(keystring, '=')) != NULL)
+  {
+  int i, len;
+  uschar *tend = equals;
+
+  while (tend > keystring && isspace(tend[-1])) tend--;
+  len = tend - keystring;
+
+  for (i = 0; i < sizeof(type_names)/sizeof(uschar *); i++)
+    {
+    if (len == Ustrlen(type_names[i]) &&
+        strncmpic(keystring, US type_names[i], len) == 0)
+      {
+      type = type_values[i];
+      break;
+      }
+    }
+
+  if (i >= sizeof(type_names)/sizeof(uschar *))
+    {
+    *errmsg = US"unsupported DNS record type";
+    return DEFER;
+    }
+
+  keystring = equals + 1;
+  while (isspace(*keystring)) keystring++;
+  }
+
+/* Initialize the resolver in case this is the first time it has been used. */
+
+dns_init(FALSE, FALSE, dnssec_mode != OK);
+
+/* The remainder of the string must be a list of domains. As long as the lookup
+for at least one of them succeeds, we return success. Failure means that none
+of them were found.
+
+The original implementation did not support a list of domains. Adding the list
+feature is compatible, except in one case: when PTR records are being looked up
+for a single IPv6 address. Fortunately, we can hack in a compatibility feature
+here: If the type is PTR and no list separator is specified, and the entire
+remaining string is valid as an IP address, set an impossible separator so that
+it is treated as one item. */
+
+if (type == T_PTR && keystring[0] != '<' &&
+    string_is_ip_address(keystring, NULL) != 0)
+  sep = -1;
+
+/* SPF strings should be concatenated without a separator, thus make
+it the default if not defined (see RFC 4408 section 3.1.3).
+Multiple SPF records are forbidden (section 3.1.2) but are currently
+not handled specially, thus they are concatenated with \n by default. */
+
+if (type == T_SPF && outsep2 == NULL)
+  outsep2 = US"";
+
+/* Now scan the list and do a lookup for each item */
+
+while ((domain = string_nextinlist(&keystring, &sep, buffer, sizeof(buffer)))
+        != NULL)
+  {
+  uschar rbuffer[256];
+  int searchtype = (type == T_CSA)? T_SRV :         /* record type we want */
+                   (type == T_MXH)? T_MX :
+                   (type == T_ZNS)? T_NS : type;
+
+  /* If the type is PTR or CSA, we have to construct the relevant magic lookup
+  key if the original is an IP address (some experimental protocols are using
+  PTR records for different purposes where the key string is a host name, and
+  Exim's extended CSA can be keyed by domains or IP addresses). This code for
+  doing the reversal is now in a separate function. */
+
+  if ((type == T_PTR || type == T_CSA) &&
+      string_is_ip_address(domain, NULL) != 0)
+    {
+    dns_build_reverse(domain, rbuffer);
+    domain = rbuffer;
+    }
+
+  do
+    {
+    DEBUG(D_lookup) debug_printf("dnsdb key: %s\n", domain);
+
+    /* Do the lookup and sort out the result. There are four special types that
+    are handled specially: T_CSA, T_ZNS, T_ADDRESSES and T_MXH.
+    The first two are handled in a special lookup function so that the facility
+    could be used from other parts of the Exim code. T_ADDRESSES is handled by looping
+    over the types of A lookup.  T_MXH affects only what happens later on in
+    this function, but for tidiness it is handled by the "special". If the
+    lookup fails, continue with the next domain. In the case of DEFER, adjust
+    the final "nothing found" result, but carry on to the next domain. */
+
+    found = domain;
+#if HAVE_IPV6
+    if (type == T_ADDRESSES)           /* NB cannot happen unless HAVE_IPV6 */
+      {
+      if (searchtype == T_ADDRESSES)
+# if defined(SUPPORT_A6)
+                                     searchtype = T_A6;
+# else
+                                     searchtype = T_AAAA;
+# endif
+      else if (searchtype == T_A6)   searchtype = T_AAAA;
+      else if (searchtype == T_AAAA) searchtype = T_A;
+      rc = dns_special_lookup(&dnsa, domain, searchtype, &found);
+      }
+    else
+#endif
+      rc = dns_special_lookup(&dnsa, domain, type, &found);
+
+    lookup_dnssec_authenticated = dnssec_mode==OK ? NULL
+      : dns_is_secure(&dnsa) ? US"yes" : US"no";
+
+    if (rc == DNS_NOMATCH || rc == DNS_NODATA) continue;
+    if (  rc != DNS_SUCCEED
+       || (dnssec_mode == DEFER && !dns_is_secure(&dnsa))
+       )
+      {
+      if (defer_mode == DEFER)
+       {
+       dns_init(FALSE, FALSE, FALSE);                  /* clr dnssec bit */
+       return DEFER;                                   /* always defer */
+       }
+      if (defer_mode == PASS) failrc = DEFER;         /* defer only if all do */
+      continue;                                       /* treat defer as fail */
+      }
+
+
+    /* Search the returned records */
+
+    for (rr = dns_next_rr(&dnsa, &dnss, RESET_ANSWERS);
+         rr != NULL;
+         rr = dns_next_rr(&dnsa, &dnss, RESET_NEXT))
+      {
+      if (rr->type != searchtype) continue;
+
+      /* There may be several addresses from an A6 record. Put the configured
+      separator between them, just as for between several records. However, A6
+      support is not normally configured these days. */
+
+      if (type == T_A ||
+          #ifdef SUPPORT_A6
+          type == T_A6 ||
+          #endif
+          type == T_AAAA ||
+          type == T_ADDRESSES)
+        {
+        dns_address *da;
+        for (da = dns_address_from_rr(&dnsa, rr); da != NULL; da = da->next)
+          {
+          if (ptr != 0) yield = string_cat(yield, &size, &ptr, outsep, 1);
+          yield = string_cat(yield, &size, &ptr, da->address,
+            Ustrlen(da->address));
+          }
+        continue;
+        }
+
+      /* Other kinds of record just have one piece of data each, but there may be
+      several of them, of course. */
+
+      if (ptr != 0) yield = string_cat(yield, &size, &ptr, outsep, 1);
+
+      if (type == T_TXT || type == T_SPF)
+        {
+        if (outsep2 == NULL)
+          {
+          /* output only the first item of data */
+          yield = string_cat(yield, &size, &ptr, (uschar *)(rr->data+1),
+            (rr->data)[0]);
+          }
+        else
+          {
+          /* output all items */
+          int data_offset = 0;
+          while (data_offset < rr->size)
+            {
+            uschar chunk_len = (rr->data)[data_offset++];
+            if (outsep2[0] != '\0' && data_offset != 1)
+              yield = string_cat(yield, &size, &ptr, outsep2, 1);
+            yield = string_cat(yield, &size, &ptr,
+                             (uschar *)((rr->data)+data_offset), chunk_len);
+            data_offset += chunk_len;
+            }
+          }
+        }
+      else if (type == T_TLSA)
+        {
+        uint8_t usage, selector, matching_type;
+        uint16_t i, payload_length;
+        uschar s[MAX_TLSA_EXPANDED_SIZE];
+       uschar * sp = s;
+        uschar *p = (uschar *)(rr->data);
+
+        usage = *p++;
+        selector = *p++;
+        matching_type = *p++;
+        /* What's left after removing the first 3 bytes above */
+        payload_length = rr->size - 3;
+        sp += sprintf(CS s, "%d %d %d ", usage, selector, matching_type);
+        /* Now append the cert/identifier, one hex char at a time */
+        for (i=0;
+             i < payload_length && sp-s < (MAX_TLSA_EXPANDED_SIZE - 4);
+             i++)
+          {
+          sp += sprintf(CS sp, "%02x", (unsigned char)p[i]);
+          }
+        yield = string_cat(yield, &size, &ptr, s, Ustrlen(s));
+        }
+      else   /* T_CNAME, T_CSA, T_MX, T_MXH, T_NS, T_PTR, T_SRV */
+        {
+        int priority, weight, port;
+        uschar s[264];
+        uschar *p = (uschar *)(rr->data);
+
+        if (type == T_MXH)
+          {
+          /* mxh ignores the priority number and includes only the hostnames */
+          GETSHORT(priority, p);
+          }
+        else if (type == T_MX)
+          {
+          GETSHORT(priority, p);
+          sprintf(CS s, "%d ", priority);
+          yield = string_cat(yield, &size, &ptr, s, Ustrlen(s));
+          }
+        else if (type == T_SRV)
+          {
+          GETSHORT(priority, p);
+          GETSHORT(weight, p);
+          GETSHORT(port, p);
+          sprintf(CS s, "%d %d %d ", priority, weight, port);
+          yield = string_cat(yield, &size, &ptr, s, Ustrlen(s));
+          }
+        else if (type == T_CSA)
+          {
+          /* See acl_verify_csa() for more comments about CSA. */
+
+          GETSHORT(priority, p);
+          GETSHORT(weight, p);
+          GETSHORT(port, p);
+
+          if (priority != 1) continue;      /* CSA version must be 1 */
+
+          /* If the CSA record we found is not the one we asked for, analyse
+          the subdomain assertions in the port field, else analyse the direct
+          authorization status in the weight field. */
+
+          if (found != domain)
+            {
+            if (port & 1) *s = 'X';         /* explicit authorization required */
+            else *s = '?';                  /* no subdomain assertions here */
+            }
+          else
+            {
+            if (weight < 2) *s = 'N';       /* not authorized */
+            else if (weight == 2) *s = 'Y'; /* authorized */
+            else if (weight == 3) *s = '?'; /* unauthorizable */
+            else continue;                  /* invalid */
+            }
+
+          s[1] = ' ';
+          yield = string_cat(yield, &size, &ptr, s, 2);
+          }
+
+        /* GETSHORT() has advanced the pointer to the target domain. */
+
+        rc = dn_expand(dnsa.answer, dnsa.answer + dnsa.answerlen, p,
+          (DN_EXPAND_ARG4_TYPE)(s), sizeof(s));
+
+        /* If an overlong response was received, the data will have been
+        truncated and dn_expand may fail. */
+
+        if (rc < 0)
+          {
+          log_write(0, LOG_MAIN, "host name alias list truncated: type=%s "
+            "domain=%s", dns_text_type(type), domain);
+          break;
+          }
+        else yield = string_cat(yield, &size, &ptr, s, Ustrlen(s));
+        }
+      }    /* Loop for list of returned records */
+
+           /* Loop for set of A-lookupu types */
+    } while (type == T_ADDRESSES && searchtype != T_A);
+
+  }        /* Loop for list of domains */
+
+/* Reclaim unused memory */
+
+store_reset(yield + ptr + 1);
+
+/* If ptr == 0 we have not found anything. Otherwise, insert the terminating
+zero and return the result. */
+
+dns_init(FALSE, FALSE, FALSE); /* clear the dnssec bit for getaddrbyname */
+
+if (ptr == 0) return failrc;
+yield[ptr] = 0;
+*result = yield;
+return OK;
+}
+
+
+
+/*************************************************
+*         Version reporting entry point          *
+*************************************************/
+
+/* See local README for interface description. */
+
+#include "../version.h"
+
+void
+dnsdb_version_report(FILE *f)
+{
+#ifdef DYNLOOKUP
+fprintf(f, "Library version: DNSDB: Exim version %s\n", EXIM_VERSION_STR);
+#endif
+}
+
+
+static lookup_info _lookup_info = {
+  US"dnsdb",                     /* lookup name */
+  lookup_querystyle,             /* query style */
+  dnsdb_open,                    /* open function */
+  NULL,                          /* check function */
+  dnsdb_find,                    /* find function */
+  NULL,                          /* no close function */
+  NULL,                          /* no tidy function */
+  NULL,                          /* no quoting function */
+  dnsdb_version_report           /* version reporting */
+};
+
+#ifdef DYNLOOKUP
+#define dnsdb_lookup_module_info _lookup_module_info
+#endif
+
+static lookup_info *_lookup_list[] = { &_lookup_info };
+lookup_module_info dnsdb_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 1 };
+
+/* vi: aw ai sw=2
+*/
+/* End of lookups/dnsdb.c */
diff --git a/src/lookups/dsearch.c b/src/lookups/dsearch.c
new file mode 100644 (file)
index 0000000..990b69c
--- /dev/null
@@ -0,0 +1,157 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* The idea for this code came from Matthew Byng-Maddick, but his original has
+been heavily reworked a lot for Exim 4 (and it now uses stat() (more precisely:
+lstat()) rather than a directory scan). */
+
+
+#include "../exim.h"
+#include "lf_functions.h"
+
+
+
+/*************************************************
+*              Open entry point                  *
+*************************************************/
+
+/* See local README for interface description. We open the directory to test
+whether it exists and whether it is searchable. However, we don't need to keep
+it open, because the "search" can be done by a call to lstat() rather than
+actually scanning through the list of files. */
+
+static void *
+dsearch_open(uschar *dirname, uschar **errmsg)
+{
+DIR *dp = opendir(CS dirname);
+if (dp == NULL)
+  {
+  int save_errno = errno;
+  *errmsg = string_open_failed(errno, "%s for directory search", dirname);
+  errno = save_errno;
+  return NULL;
+  }
+closedir(dp);
+return (void *)(-1);
+}
+
+
+/*************************************************
+*             Check entry point                  *
+*************************************************/
+
+/* The handle will always be (void *)(-1), but don't try casting it to an
+integer as this gives warnings on 64-bit systems. */
+
+BOOL
+static dsearch_check(void *handle, uschar *filename, int modemask, uid_t *owners,
+  gid_t *owngroups, uschar **errmsg)
+{
+handle = handle;
+return lf_check_file(-1, filename, S_IFDIR, modemask, owners, owngroups,
+  "dsearch", errmsg) == 0;
+}
+
+
+/*************************************************
+*              Find entry point                  *
+*************************************************/
+
+/* See local README for interface description. We use lstat() instead of
+scanning the directory, as it is hopefully faster to let the OS do the scanning
+for us. */
+
+int
+static dsearch_find(void *handle, uschar *dirname, uschar *keystring, int length,
+  uschar **result, uschar **errmsg, BOOL *do_cache)
+{
+struct stat statbuf;
+int save_errno;
+uschar filename[PATH_MAX];
+
+handle = handle;  /* Keep picky compilers happy */
+length = length;
+do_cache = do_cache;
+
+if (Ustrchr(keystring, '/') != 0)
+  {
+  *errmsg = string_sprintf("key for dsearch lookup contains a slash: %s",
+    keystring);
+  return DEFER;
+  }
+
+if (!string_format(filename, sizeof(filename), "%s/%s", dirname, keystring))
+  {
+  *errmsg = US"path name too long";
+  return DEFER;
+  }
+
+if (Ulstat(filename, &statbuf) >= 0)
+  {
+  *result = string_copy(keystring);
+  return OK;
+  }
+
+if (errno == ENOENT) return FAIL;
+
+save_errno = errno;
+*errmsg = string_sprintf("%s: lstat failed", filename);
+errno = save_errno;
+return DEFER;
+}
+
+
+/*************************************************
+*              Close entry point                 *
+*************************************************/
+
+/* See local README for interface description */
+
+void
+static dsearch_close(void *handle)
+{
+handle = handle;   /* Avoid compiler warning */
+}
+
+
+/*************************************************
+*         Version reporting entry point          *
+*************************************************/
+
+/* See local README for interface description. */
+
+#include "../version.h"
+
+void
+dsearch_version_report(FILE *f)
+{
+#ifdef DYNLOOKUP
+fprintf(f, "Library version: dsearch: Exim version %s\n", EXIM_VERSION_STR);
+#endif
+}
+
+
+static lookup_info _lookup_info = {
+  US"dsearch",                   /* lookup name */
+  lookup_absfile,                /* uses absolute file name */
+  dsearch_open,                  /* open function */
+  dsearch_check,                 /* check function */
+  dsearch_find,                  /* find function */
+  dsearch_close,                 /* close function */
+  NULL,                          /* no tidy function */
+  NULL,                          /* no quoting function */
+  dsearch_version_report         /* version reporting */
+};
+
+#ifdef DYNLOOKUP
+#define dsearch_lookup_module_info _lookup_module_info
+#endif
+
+static lookup_info *_lookup_list[] = { &_lookup_info };
+lookup_module_info dsearch_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 1 };
+
+/* End of lookups/dsearch.c */
diff --git a/src/lookups/ibase.c b/src/lookups/ibase.c
new file mode 100644 (file)
index 0000000..23e1dea
--- /dev/null
@@ -0,0 +1,584 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* The code in this module was contributed by Ard Biesheuvel. */
+
+#include "../exim.h"
+#include "lf_functions.h"
+
+#include <ibase.h>              /* The system header */
+
+/* Structure and anchor for caching connections. */
+
+typedef struct ibase_connection {
+    struct ibase_connection *next;
+    uschar *server;
+    isc_db_handle dbh;
+    isc_tr_handle transh;
+} ibase_connection;
+
+static ibase_connection *ibase_connections = NULL;
+
+
+
+/*************************************************
+*              Open entry point                  *
+*************************************************/
+
+/* See local README for interface description. */
+
+static void *ibase_open(uschar * filename, uschar ** errmsg)
+{
+    return (void *) (1);        /* Just return something non-null */
+}
+
+
+
+/*************************************************
+*               Tidy entry point                 *
+*************************************************/
+
+/* See local README for interface description. */
+
+static void ibase_tidy(void)
+{
+    ibase_connection *cn;
+    ISC_STATUS status[20];
+
+    while ((cn = ibase_connections) != NULL) {
+        ibase_connections = cn->next;
+        DEBUG(D_lookup) debug_printf("close Interbase connection: %s\n",
+                                     cn->server);
+        isc_commit_transaction(status, &cn->transh);
+        isc_detach_database(status, &cn->dbh);
+    }
+}
+
+static int fetch_field(char *buffer, int buffer_size, XSQLVAR * var)
+{
+    if (buffer_size < var->sqllen)
+        return 0;
+
+    switch (var->sqltype & ~1) {
+    case SQL_VARYING:
+        strncpy(buffer, &var->sqldata[2], *(short *) var->sqldata);
+        return *(short *) var->sqldata;
+    case SQL_TEXT:
+        strncpy(buffer, var->sqldata, var->sqllen);
+        return var->sqllen;
+    case SQL_SHORT:
+        return sprintf(buffer, "%d", *(short *) var->sqldata);
+    case SQL_LONG:
+        return sprintf(buffer, "%ld", *(ISC_LONG *) var->sqldata);
+#ifdef SQL_INT64
+    case SQL_INT64:
+        return sprintf(buffer, "%lld", *(ISC_INT64 *) var->sqldata);
+#endif
+    default:
+        /* not implemented */
+        return 0;
+    }
+}
+
+/*************************************************
+*        Internal search function                *
+*************************************************/
+
+/* This function is called from the find entry point to do the search for a
+single server.
+
+Arguments:
+  query        the query string
+  server       the server string
+  resultptr    where to store the result
+  errmsg       where to point an error message
+  defer_break  TRUE if no more servers are to be tried after DEFER
+
+The server string is of the form "host:dbname|user|password". The host can be
+host:port. This string is in a nextinlist temporary buffer, so can be
+overwritten.
+
+Returns:       OK, FAIL, or DEFER
+*/
+
+static int
+perform_ibase_search(uschar * query, uschar * server, uschar ** resultptr,
+                     uschar ** errmsg, BOOL * defer_break)
+{
+    isc_stmt_handle stmth = NULL;
+    XSQLDA *out_sqlda;
+    XSQLVAR *var;
+
+    char buffer[256];
+    ISC_STATUS status[20], *statusp = status;
+
+    int i;
+    int ssize = 0;
+    int offset = 0;
+    int yield = DEFER;
+    uschar *result = NULL;
+    ibase_connection *cn;
+    uschar *server_copy = NULL;
+    uschar *sdata[3];
+
+/* Disaggregate the parameters from the server argument. The order is host,
+database, user, password. We can write to the string, since it is in a
+nextinlist temporary buffer. The copy of the string that is used for caching
+has the password removed. This copy is also used for debugging output. */
+
+    for (i = 2; i > 0; i--) {
+        uschar *pp = Ustrrchr(server, '|');
+        if (pp == NULL) {
+            *errmsg =
+                string_sprintf("incomplete Interbase server data: %s",
+                               (i == 3) ? server : server_copy);
+            *defer_break = TRUE;
+            return DEFER;
+        }
+        *pp++ = 0;
+        sdata[i] = pp;
+        if (i == 2)
+            server_copy = string_copy(server);   /* sans password */
+    }
+    sdata[0] = server;          /* What's left at the start */
+
+/* See if we have a cached connection to the server */
+
+    for (cn = ibase_connections; cn != NULL; cn = cn->next) {
+        if (Ustrcmp(cn->server, server_copy) == 0) {
+            break;
+        }
+    }
+
+/* Use a previously cached connection ? */
+
+    if (cn != NULL) {
+        static char db_info_options[] = { isc_info_base_level };
+
+        /* test if the connection is alive */
+        if (isc_database_info
+            (status, &cn->dbh, sizeof(db_info_options), db_info_options,
+             sizeof(buffer), buffer)) {
+            /* error occurred: assume connection is down */
+            DEBUG(D_lookup)
+                debug_printf
+                ("Interbase cleaning up cached connection: %s\n",
+                 cn->server);
+            isc_detach_database(status, &cn->dbh);
+        } else {
+            DEBUG(D_lookup)
+                debug_printf("Interbase using cached connection for %s\n",
+                             server_copy);
+        }
+    } else {
+        cn = store_get(sizeof(ibase_connection));
+        cn->server = server_copy;
+        cn->dbh = NULL;
+        cn->transh = NULL;
+        cn->next = ibase_connections;
+        ibase_connections = cn;
+    }
+
+/* If no cached connection, we must set one up. */
+
+    if (cn->dbh == NULL || cn->transh == NULL) {
+
+        char *dpb, *p;
+        short dpb_length;
+        static char trans_options[] =
+            { isc_tpb_version3, isc_tpb_read, isc_tpb_read_committed,
+            isc_tpb_rec_version
+        };
+
+        /* Construct the database parameter buffer. */
+        dpb = buffer;
+        *dpb++ = isc_dpb_version1;
+        *dpb++ = isc_dpb_user_name;
+        *dpb++ = strlen(sdata[1]);
+        for (p = sdata[1]; *p;)
+            *dpb++ = *p++;
+        *dpb++ = isc_dpb_password;
+        *dpb++ = strlen(sdata[2]);
+        for (p = sdata[2]; *p;)
+            *dpb++ = *p++;
+        dpb_length = dpb - buffer;
+
+        DEBUG(D_lookup)
+            debug_printf("new Interbase connection: database=%s user=%s\n",
+                         sdata[0], sdata[1]);
+
+        /* Connect to the database */
+        if (isc_attach_database
+            (status, 0, sdata[0], &cn->dbh, dpb_length, buffer)) {
+            isc_interprete(buffer, &statusp);
+            *errmsg =
+                string_sprintf("Interbase attach() failed: %s", buffer);
+            *defer_break = FALSE;
+            goto IBASE_EXIT;
+        }
+
+        /* Now start a read-only read-committed transaction */
+        if (isc_start_transaction
+            (status, &cn->transh, 1, &cn->dbh, sizeof(trans_options),
+             trans_options)) {
+            isc_interprete(buffer, &statusp);
+            isc_detach_database(status, &cn->dbh);
+            *errmsg =
+                string_sprintf("Interbase start_transaction() failed: %s",
+                               buffer);
+            *defer_break = FALSE;
+            goto IBASE_EXIT;
+        }
+    }
+
+/* Run the query */
+    if (isc_dsql_allocate_statement(status, &cn->dbh, &stmth)) {
+        isc_interprete(buffer, &statusp);
+        *errmsg =
+            string_sprintf("Interbase alloc_statement() failed: %s",
+                           buffer);
+        *defer_break = FALSE;
+        goto IBASE_EXIT;
+    }
+
+    out_sqlda = store_get(XSQLDA_LENGTH(1));
+    out_sqlda->version = SQLDA_VERSION1;
+    out_sqlda->sqln = 1;
+
+    if (isc_dsql_prepare
+        (status, &cn->transh, &stmth, 0, query, 1, out_sqlda)) {
+        isc_interprete(buffer, &statusp);
+        store_reset(out_sqlda);
+        out_sqlda = NULL;
+        *errmsg =
+            string_sprintf("Interbase prepare_statement() failed: %s",
+                           buffer);
+        *defer_break = FALSE;
+        goto IBASE_EXIT;
+    }
+
+/* re-allocate the output structure if there's more than one field */
+    if (out_sqlda->sqln < out_sqlda->sqld) {
+        XSQLDA *new_sqlda = store_get(XSQLDA_LENGTH(out_sqlda->sqld));
+        if (isc_dsql_describe
+            (status, &stmth, out_sqlda->version, new_sqlda)) {
+            isc_interprete(buffer, &statusp);
+            isc_dsql_free_statement(status, &stmth, DSQL_drop);
+            store_reset(out_sqlda);
+            out_sqlda = NULL;
+            *errmsg =
+                string_sprintf("Interbase describe_statement() failed: %s",
+                               buffer);
+            *defer_break = FALSE;
+            goto IBASE_EXIT;
+        }
+        out_sqlda = new_sqlda;
+    }
+
+/* allocate storage for every returned field */
+    for (i = 0, var = out_sqlda->sqlvar; i < out_sqlda->sqld; i++, var++) {
+        switch (var->sqltype & ~1) {
+        case SQL_VARYING:
+            var->sqldata =
+                (char *) store_get(sizeof(char) * var->sqllen + 2);
+            break;
+        case SQL_TEXT:
+            var->sqldata =
+                (char *) store_get(sizeof(char) * var->sqllen);
+            break;
+        case SQL_SHORT:
+            var->sqldata = (char *) store_get(sizeof(short));
+            break;
+        case SQL_LONG:
+            var->sqldata = (char *) store_get(sizeof(ISC_LONG));
+            break;
+#ifdef SQL_INT64
+        case SQL_INT64:
+            var->sqldata = (char *) store_get(sizeof(ISC_INT64));
+            break;
+#endif
+        case SQL_FLOAT:
+            var->sqldata = (char *) store_get(sizeof(float));
+            break;
+        case SQL_DOUBLE:
+            var->sqldata = (char *) store_get(sizeof(double));
+            break;
+#ifdef SQL_TIMESTAMP
+        case SQL_DATE:
+            var->sqldata = (char *) store_get(sizeof(ISC_QUAD));
+            break;
+#else
+        case SQL_TIMESTAMP:
+            var->sqldata = (char *) store_get(sizeof(ISC_TIMESTAMP));
+            break;
+        case SQL_TYPE_DATE:
+            var->sqldata = (char *) store_get(sizeof(ISC_DATE));
+            break;
+        case SQL_TYPE_TIME:
+            var->sqldata = (char *) store_get(sizeof(ISC_TIME));
+            break;
+#endif
+        }
+        if (var->sqltype & 1) {
+            var->sqlind = (short *) store_get(sizeof(short));
+        }
+    }
+
+    /* finally, we're ready to execute the statement */
+    if (isc_dsql_execute
+        (status, &cn->transh, &stmth, out_sqlda->version, NULL)) {
+        isc_interprete(buffer, &statusp);
+        *errmsg =
+            string_sprintf("Interbase describe_statement() failed: %s",
+                           buffer);
+        isc_dsql_free_statement(status, &stmth, DSQL_drop);
+        *defer_break = FALSE;
+        goto IBASE_EXIT;
+    }
+
+    while (isc_dsql_fetch(status, &stmth, out_sqlda->version, out_sqlda) !=
+           100L) {
+        /* check if an error occurred */
+        if (status[0] & status[1]) {
+            isc_interprete(buffer, &statusp);
+            *errmsg =
+                string_sprintf("Interbase fetch() failed: %s", buffer);
+            isc_dsql_free_statement(status, &stmth, DSQL_drop);
+            *defer_break = FALSE;
+            goto IBASE_EXIT;
+        }
+
+        if (result != NULL)
+            result = string_cat(result, &ssize, &offset, US "\n", 1);
+
+        /* Find the number of fields returned. If this is one, we don't add field
+           names to the data. Otherwise we do. */
+        if (out_sqlda->sqld == 1) {
+            if (out_sqlda->sqlvar[0].sqlind == NULL || *out_sqlda->sqlvar[0].sqlind != -1)     /* NULL value yields nothing */
+                result =
+                    string_cat(result, &ssize, &offset, US buffer,
+                               fetch_field(buffer, sizeof(buffer),
+                                           &out_sqlda->sqlvar[0]));
+        }
+
+        else
+            for (i = 0; i < out_sqlda->sqld; i++) {
+                int len = fetch_field(buffer, sizeof(buffer),
+                                      &out_sqlda->sqlvar[i]);
+
+                result =
+                    string_cat(result, &ssize, &offset,
+                               US out_sqlda->sqlvar[i].aliasname,
+                               out_sqlda->sqlvar[i].aliasname_length);
+                result = string_cat(result, &ssize, &offset, US "=", 1);
+
+                /* Quote the value if it contains spaces or is empty */
+
+                if (*out_sqlda->sqlvar[i].sqlind == -1) {       /* NULL value */
+                    result =
+                        string_cat(result, &ssize, &offset, US "\"\"", 2);
+                }
+
+                else if (buffer[0] == 0 || Ustrchr(buffer, ' ') != NULL) {
+                    int j;
+                    result =
+                        string_cat(result, &ssize, &offset, US "\"", 1);
+                    for (j = 0; j < len; j++) {
+                        if (buffer[j] == '\"' || buffer[j] == '\\')
+                            result =
+                                string_cat(result, &ssize, &offset,
+                                           US "\\", 1);
+                        result =
+                            string_cat(result, &ssize, &offset,
+                                       US buffer + j, 1);
+                    }
+                    result =
+                        string_cat(result, &ssize, &offset, US "\"", 1);
+                } else {
+                    result =
+                        string_cat(result, &ssize, &offset, US buffer,
+                                   len);
+                }
+                result = string_cat(result, &ssize, &offset, US " ", 1);
+            }
+    }
+
+/* If result is NULL then no data has been found and so we return FAIL.
+Otherwise, we must terminate the string which has been built; string_cat()
+always leaves enough room for a terminating zero. */
+
+    if (result == NULL) {
+        yield = FAIL;
+        *errmsg = US "Interbase: no data found";
+    } else {
+        result[offset] = 0;
+        store_reset(result + offset + 1);
+    }
+
+
+/* Get here by goto from various error checks. */
+
+  IBASE_EXIT:
+
+    if (stmth != NULL)
+        isc_dsql_free_statement(status, &stmth, DSQL_drop);
+
+/* Non-NULL result indicates a sucessful result */
+
+    if (result != NULL) {
+        *resultptr = result;
+        return OK;
+    } else {
+        DEBUG(D_lookup) debug_printf("%s\n", *errmsg);
+        return yield;           /* FAIL or DEFER */
+    }
+}
+
+
+
+
+/*************************************************
+*               Find entry point                 *
+*************************************************/
+
+/* See local README for interface description. The handle and filename
+arguments are not used. Loop through a list of servers while the query is
+deferred with a retryable error. */
+
+static int
+ibase_find(void *handle, uschar * filename, uschar * query, int length,
+           uschar ** result, uschar ** errmsg, BOOL *do_cache)
+{
+    int sep = 0;
+    uschar *server;
+    uschar *list = ibase_servers;
+    uschar buffer[512];
+
+    /* Keep picky compilers happy */
+    do_cache = do_cache;
+
+    DEBUG(D_lookup) debug_printf("Interbase query: %s\n", query);
+
+    while ((server =
+            string_nextinlist(&list, &sep, buffer,
+                              sizeof(buffer))) != NULL) {
+        BOOL defer_break = FALSE;
+        int rc = perform_ibase_search(query, server, result, errmsg,
+                                      &defer_break);
+        if (rc != DEFER || defer_break)
+            return rc;
+    }
+
+    if (ibase_servers == NULL)
+        *errmsg = US "no Interbase servers defined (ibase_servers option)";
+
+    return DEFER;
+}
+
+
+
+/*************************************************
+*               Quote entry point                *
+*************************************************/
+
+/* The only characters that need to be quoted (with backslash) are newline,
+tab, carriage return, backspace, backslash itself, and the quote characters.
+Percent, and underscore and not escaped. They are only special in contexts
+where they can be wild cards, and this isn't usually the case for data inserted
+from messages, since that isn't likely to be treated as a pattern of any kind.
+Sadly, MySQL doesn't seem to behave like other programs. If you use something
+like "where id="ab\%cd" it does not treat the string as "ab%cd". So you really
+can't quote "on spec".
+
+Arguments:
+  s          the string to be quoted
+  opt        additional option text or NULL if none
+
+Returns:     the processed string or NULL for a bad option
+*/
+
+static uschar *ibase_quote(uschar * s, uschar * opt)
+{
+    register int c;
+    int count = 0;
+    uschar *t = s;
+    uschar *quoted;
+
+    if (opt != NULL)
+        return NULL;            /* No options recognized */
+
+    while ((c = *t++) != 0)
+        if (Ustrchr("\n\t\r\b\'\"\\", c) != NULL)
+            count++;
+
+    if (count == 0)
+        return s;
+    t = quoted = store_get(Ustrlen(s) + count + 1);
+
+    while ((c = *s++) != 0) {
+        if (Ustrchr("'", c) != NULL) {
+            *t++ = '\'';
+            *t++ = '\'';
+/*    switch(c)
+      {
+      case '\n': *t++ = 'n';
+      break;
+      case '\t': *t++ = 't';
+      break;
+      case '\r': *t++ = 'r';
+      break;
+      case '\b': *t++ = 'b';
+      break;
+      default:   *t++ = c;
+      break;
+      }*/
+        } else
+            *t++ = c;
+    }
+
+    *t = 0;
+    return quoted;
+}
+
+
+/*************************************************
+*         Version reporting entry point          *
+*************************************************/
+
+/* See local README for interface description. */
+
+#include "../version.h"
+
+void
+ibase_version_report(FILE *f)
+{
+#ifdef DYNLOOKUP
+fprintf(f, "Library version: ibase: Exim version %s\n", EXIM_VERSION_STR);
+#endif
+}
+
+
+static lookup_info _lookup_info = {
+  US"ibase",                     /* lookup name */
+  lookup_querystyle,             /* query-style lookup */
+  ibase_open,                    /* open function */
+  NULL,                          /* no check function */
+  ibase_find,                    /* find function */
+  NULL,                          /* no close function */
+  ibase_tidy,                    /* tidy function */
+  ibase_quote,                   /* quoting function */
+  ibase_version_report           /* version reporting */
+};
+
+#ifdef DYNLOOKUP
+#define ibase_lookup_module_info _lookup_module_info
+#endif
+static lookup_info *_lookup_list[] = { &_lookup_info };
+lookup_module_info ibase_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 1 };
+
+/* End of lookups/ibase.c */
diff --git a/src/lookups/ldap.c b/src/lookups/ldap.c
new file mode 100644 (file)
index 0000000..f77229d
--- /dev/null
@@ -0,0 +1,1669 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2014 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Many thanks to Stuart Lynne for contributing the original code for this
+driver. Further contibutions from Michael Haardt, Brian Candler, Barry
+Pederson, Peter Savitch and Christian Kellner. Particular thanks to Brian for
+researching how to handle the different kinds of error. */
+
+
+#include "../exim.h"
+#include "lf_functions.h"
+
+
+/* Include LDAP headers. The code below uses some "old" LDAP interfaces that
+are deprecated in OpenLDAP. I don't know their status in other LDAP
+implementations. LDAP_DEPRECATED causes their prototypes to be defined in
+ldap.h. */
+
+#define LDAP_DEPRECATED 1
+
+#include <lber.h>
+#include <ldap.h>
+
+
+/* Annoyingly, the different LDAP libraries handle errors in different ways,
+and some other things too. There doesn't seem to be an automatic way of
+distinguishing between them. Local/Makefile should contain a setting of
+LDAP_LIB_TYPE, which in turn causes appropriate macros to be defined for the
+different kinds. Those that matter are:
+
+LDAP_LIB_NETSCAPE
+LDAP_LIB_SOLARIS   with synonym LDAP_LIB_SOLARIS7
+LDAP_LIB_OPENLDAP2
+
+These others may be defined, but are in fact the default, so are not tested:
+
+LDAP_LIB_UMICHIGAN
+LDAP_LIB_OPENLDAP1
+*/
+
+#if defined(LDAP_LIB_SOLARIS7) && ! defined(LDAP_LIB_SOLARIS)
+#define LDAP_LIB_SOLARIS
+#endif
+
+
+/* Just in case LDAP_NO_LIMIT is not defined by some of these libraries. */
+
+#ifndef LDAP_NO_LIMIT
+#define LDAP_NO_LIMIT 0
+#endif
+
+
+/* Just in case LDAP_DEREF_NEVER is not defined */
+
+#ifndef LDAP_DEREF_NEVER
+#define LDAP_DEREF_NEVER 0
+#endif
+
+
+/* Four types of LDAP search are implemented */
+
+#define SEARCH_LDAP_MULTIPLE 0       /* Get attributes from multiple entries */
+#define SEARCH_LDAP_SINGLE 1         /* Get attributes from one entry only */
+#define SEARCH_LDAP_DN 2             /* Get just the DN from one entry */
+#define SEARCH_LDAP_AUTH 3           /* Just checking for authentication */
+
+/* In all 4 cases, the DN is left in $ldap_dn (which post-dates the
+SEARCH_LDAP_DN lookup). */
+
+
+/* Structure and anchor for caching connections. */
+
+typedef struct ldap_connection {
+  struct ldap_connection *next;
+  uschar *host;
+  uschar *user;
+  uschar *password;
+  BOOL  bound;
+  int   port;
+  BOOL  is_start_tls_called;
+  LDAP *ld;
+} LDAP_CONNECTION;
+
+static LDAP_CONNECTION *ldap_connections = NULL;
+
+
+
+/*************************************************
+*         Internal search function               *
+*************************************************/
+
+/* This is the function that actually does the work. It is called (indirectly
+via control_ldap_search) from eldap_find(), eldapauth_find(), eldapdn_find(),
+and eldapm_find(), with a difference in the "search_type" argument.
+
+The case of eldapauth_find() is special in that all it does is do
+authentication, returning OK or FAIL as appropriate. This isn't used as a
+lookup. Instead, it is called from expand.c as an expansion condition test.
+
+The DN from a successful lookup is placed in $ldap_dn. This feature postdates
+the provision of the SEARCH_LDAP_DN facility for returning just the DN as the
+data.
+
+Arguments:
+  ldap_url      the URL to be looked up
+  server        server host name, when URL contains none
+  s_port        server port, used when URL contains no name
+  search_type   SEARCH_LDAP_MULTIPLE allows values from multiple entries
+                SEARCH_LDAP_SINGLE allows values from one entry only
+                SEARCH_LDAP_DN gets the DN from one entry
+  res           set to point at the result (not used for ldapauth)
+  errmsg        set to point a message if result is not OK
+  defer_break   set TRUE if no more servers to be tried after a DEFER
+  user          user name for authentication, or NULL
+  password      password for authentication, or NULL
+  sizelimit     max number of entries returned, or 0 for no limit
+  timelimit     max time to wait, or 0 for no limit
+  tcplimit      max time for network activity, e.g. connect, or 0 for OS default
+  deference     the dereference option, which is one of
+                  LDAP_DEREF_{NEVER,SEARCHING,FINDING,ALWAYS}
+  referrals     the referral option, which is LDAP_OPT_ON or LDAP_OPT_OFF
+
+Returns:        OK or FAIL or DEFER
+                FAIL is given only if a lookup was performed successfully, but
+                returned no data.
+*/
+
+static int
+perform_ldap_search(uschar *ldap_url, uschar *server, int s_port, int search_type,
+  uschar **res, uschar **errmsg, BOOL *defer_break, uschar *user, uschar *password,
+  int sizelimit, int timelimit, int tcplimit, int dereference, void *referrals)
+{
+LDAPURLDesc     *ludp = NULL;
+LDAPMessage     *result = NULL;
+BerElement      *ber;
+LDAP_CONNECTION *lcp;
+
+struct timeval timeout;
+struct timeval *timeoutptr = NULL;
+
+uschar *attr;
+uschar **attrp;
+uschar *data = NULL;
+uschar *dn = NULL;
+uschar *host;
+uschar **values;
+uschar **firstval;
+uschar porttext[16];
+
+uschar *error1 = NULL;   /* string representation of errcode (static) */
+uschar *error2 = NULL;   /* error message from the server */
+uschar *matched = NULL;  /* partially matched DN */
+
+int    attr_count = 0;
+int    error_yield = DEFER;
+int    msgid;
+int    rc, ldap_rc, ldap_parse_rc;
+int    port;
+int    ptr = 0;
+int    rescount = 0;
+int    size = 0;
+BOOL   attribute_found = FALSE;
+BOOL   ldapi = FALSE;
+
+DEBUG(D_lookup)
+  debug_printf("perform_ldap_search: ldap%s URL = \"%s\" server=%s port=%d "
+    "sizelimit=%d timelimit=%d tcplimit=%d\n",
+    (search_type == SEARCH_LDAP_MULTIPLE)? "m" :
+    (search_type == SEARCH_LDAP_DN)? "dn" :
+    (search_type == SEARCH_LDAP_AUTH)? "auth" : "",
+    ldap_url, server, s_port, sizelimit, timelimit, tcplimit);
+
+/* Check if LDAP thinks the URL is a valid LDAP URL. We assume that if the LDAP
+library that is in use doesn't recognize, say, "ldapi", it will barf here. */
+
+if (!ldap_is_ldap_url(CS ldap_url))
+  {
+  *errmsg = string_sprintf("ldap_is_ldap_url: not an LDAP url \"%s\"\n",
+    ldap_url);
+  goto RETURN_ERROR_BREAK;
+  }
+
+/* Parse the URL */
+
+if ((rc = ldap_url_parse(CS ldap_url, &ludp)) != 0)
+  {
+  *errmsg = string_sprintf("ldap_url_parse: (error %d) parsing \"%s\"\n", rc,
+    ldap_url);
+  goto RETURN_ERROR_BREAK;
+  }
+
+/* If the host name is empty, take it from the separate argument, if one is
+given. OpenLDAP 2.0.6 sets an unset hostname to "" rather than empty, but
+expects NULL later in ldap_init() to mean "default", annoyingly. In OpenLDAP
+2.0.11 this has changed (it uses NULL). */
+
+if ((ludp->lud_host == NULL || ludp->lud_host[0] == 0) && server != NULL)
+  {
+  host = server;
+  port = s_port;
+  }
+else
+  {
+  host = US ludp->lud_host;
+  if (host != NULL && host[0] == 0) host = NULL;
+  port = ludp->lud_port;
+  }
+
+DEBUG(D_lookup) debug_printf("after ldap_url_parse: host=%s port=%d\n",
+  host, port);
+
+if (port == 0) port = LDAP_PORT;      /* Default if none given */
+sprintf(CS porttext, ":%d", port);    /* For messages */
+
+/* If the "host name" is actually a path, we are going to connect using a Unix
+socket, regardless of whether "ldapi" was actually specified or not. This means
+that a Unix socket can be declared in eldap_default_servers, and "traditional"
+LDAP queries using just "ldap" can be used ("ldaps" is similarly overridden).
+The path may start with "/" or it may already be escaped as "%2F" if it was
+actually declared that way in eldap_default_servers. (I did it that way the
+first time.) If the host name is not a path, the use of "ldapi" causes an
+error, except in the default case. (But lud_scheme doesn't seem to exist in
+older libraries.) */
+
+if (host != NULL)
+  {
+  if ((host[0] == '/' || Ustrncmp(host, "%2F", 3) == 0))
+    {
+    ldapi = TRUE;
+    porttext[0] = 0;    /* Remove port from messages */
+    }
+
+  #if defined LDAP_LIB_OPENLDAP2
+  else if (strncmp(ludp->lud_scheme, "ldapi", 5) == 0)
+    {
+    *errmsg = string_sprintf("ldapi requires an absolute path (\"%s\" given)",
+      host);
+    goto RETURN_ERROR;
+    }
+  #endif
+  }
+
+/* Count the attributes; we need this later to tell us how to format results */
+
+for (attrp = USS ludp->lud_attrs; attrp != NULL && *attrp != NULL; attrp++)
+  attr_count++;
+
+/* See if we can find a cached connection to this host. The port is not
+relevant for ldapi. The host name pointer is set to NULL if no host was given
+(implying the library default), rather than to the empty string. Note that in
+this case, there is no difference between ldap and ldapi. */
+
+for (lcp = ldap_connections; lcp != NULL; lcp = lcp->next)
+  {
+  if ((host == NULL) != (lcp->host == NULL) ||
+      (host != NULL && strcmpic(lcp->host, host) != 0))
+    continue;
+  if (ldapi || port == lcp->port) break;
+  }
+
+/* Use this network timeout in any requests. */
+
+if (tcplimit > 0)
+  {
+  timeout.tv_sec = tcplimit;
+  timeout.tv_usec = 0;
+  timeoutptr = &timeout;
+  }
+
+/* If no cached connection found, we must open a connection to the server. If
+the server name is actually an absolute path, we set ldapi=TRUE above. This
+requests connection via a Unix socket. However, as far as I know, only OpenLDAP
+supports the use of sockets, and the use of ldap_initialize(). */
+
+if (lcp == NULL)
+  {
+  LDAP *ld;
+
+  #ifdef LDAP_OPT_X_TLS_NEWCTX
+  int  am_server = 0;
+  LDAP *ldsetctx;
+  #else
+  LDAP *ldsetctx = NULL;
+  #endif
+
+
+  /* --------------------------- OpenLDAP ------------------------ */
+
+  /* There seems to be a preference under OpenLDAP for ldap_initialize()
+  instead of ldap_init(), though I have as yet been unable to find
+  documentation that says this. (OpenLDAP documentation is sparse to
+  non-existent). So we handle OpenLDAP differently here. Also, support for
+  ldapi seems to be OpenLDAP-only at present. */
+
+  #ifdef LDAP_LIB_OPENLDAP2
+
+  /* We now need an empty string for the default host. Get some store in which
+  to build a URL for ldap_initialize(). In the ldapi case, it can't be bigger
+  than (9 + 3*Ustrlen(shost)), whereas in the other cases it can't be bigger
+  than the host name + "ldaps:///" plus : and a port number, say 20 + the
+  length of the host name. What we get should accommodate both, easily. */
+
+  uschar *shost = (host == NULL)? US"" : host;
+  uschar *init_url = store_get(20 + 3 * Ustrlen(shost));
+  uschar *init_ptr;
+
+  /* Handle connection via Unix socket ("ldapi"). We build a basic LDAP URI to
+  contain the path name, with slashes escaped as %2F. */
+
+  if (ldapi)
+    {
+    int ch;
+    init_ptr = init_url + 8;
+    Ustrcpy(init_url, "ldapi://");
+    while ((ch = *shost++) != 0)
+      {
+      if (ch == '/')
+        {
+        Ustrncpy(init_ptr, "%2F", 3);
+        init_ptr += 3;
+        }
+      else *init_ptr++ = ch;
+      }
+    *init_ptr = 0;
+    }
+
+  /* This is not an ldapi call. Just build a URI with the protocol type, host
+  name, and port. */
+
+  else
+    {
+    init_ptr = Ustrchr(ldap_url, '/');
+    Ustrncpy(init_url, ldap_url, init_ptr - ldap_url);
+    init_ptr = init_url + (init_ptr - ldap_url);
+    sprintf(CS init_ptr, "//%s:%d/", shost, port);
+    }
+
+  /* Call ldap_initialize() and check the result */
+
+  DEBUG(D_lookup) debug_printf("ldap_initialize with URL %s\n", init_url);
+  rc = ldap_initialize(&ld, CS init_url);
+  if (rc != LDAP_SUCCESS)
+    {
+    *errmsg = string_sprintf("ldap_initialize: (error %d) URL \"%s\"\n",
+      rc, init_url);
+    goto RETURN_ERROR;
+    }
+  store_reset(init_url);   /* Might as well save memory when we can */
+
+
+  /* ------------------------- Not OpenLDAP ---------------------- */
+
+  /* For libraries other than OpenLDAP, use ldap_init(). */
+
+  #else   /* LDAP_LIB_OPENLDAP2 */
+  ld = ldap_init(CS host, port);
+  #endif  /* LDAP_LIB_OPENLDAP2 */
+
+  /* -------------------------------------------------------------- */
+
+
+  /* Handle failure to initialize */
+
+  if (ld == NULL)
+    {
+    *errmsg = string_sprintf("failed to initialize for LDAP server %s%s - %s",
+      host, porttext, strerror(errno));
+    goto RETURN_ERROR;
+    }
+
+  #ifdef LDAP_OPT_X_TLS_NEWCTX
+  ldsetctx = ld;
+  #endif
+
+  /* Set the TCP connect time limit if available. This is something that is
+  in Netscape SDK v4.1; I don't know about other libraries. */
+
+  #ifdef LDAP_X_OPT_CONNECT_TIMEOUT
+  if (tcplimit > 0)
+    {
+    int timeout1000 = tcplimit*1000;
+    ldap_set_option(ld, LDAP_X_OPT_CONNECT_TIMEOUT, (void *)&timeout1000);
+    }
+  else
+    {
+    int notimeout = LDAP_X_IO_TIMEOUT_NO_TIMEOUT;
+    ldap_set_option(ld, LDAP_X_OPT_CONNECT_TIMEOUT, (void *)&notimeout);
+    }
+  #endif
+
+  /* Set the TCP connect timeout. This works with OpenLDAP 2.2.14. */
+
+  #ifdef LDAP_OPT_NETWORK_TIMEOUT
+  if (tcplimit > 0)
+    ldap_set_option(ld, LDAP_OPT_NETWORK_TIMEOUT, (void *)timeoutptr);
+  #endif
+
+  /* I could not get TLS to work until I set the version to 3. That version
+  seems to be the default nowadays. The RFC is dated 1997, so I would hope
+  that all the LDAP libraries support it. Therefore, if eldap_version hasn't
+  been set, go for v3 if we can. */
+
+  if (eldap_version < 0)
+    {
+    #ifdef LDAP_VERSION3
+    eldap_version = LDAP_VERSION3;
+    #else
+    eldap_version = 2;
+    #endif
+    }
+
+  #ifdef LDAP_OPT_PROTOCOL_VERSION
+  ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, (void *)&eldap_version);
+  #endif
+
+  DEBUG(D_lookup) debug_printf("initialized for LDAP (v%d) server %s%s\n",
+    eldap_version, host, porttext);
+
+  /* If not using ldapi and TLS is available, set appropriate TLS options: hard
+  for "ldaps" and soft otherwise. */
+
+  #ifdef LDAP_OPT_X_TLS
+  if (!ldapi)
+    {
+    int tls_option;
+    #ifdef LDAP_OPT_X_TLS_REQUIRE_CERT
+    if (eldap_require_cert != NULL)
+      {
+      tls_option = LDAP_OPT_X_TLS_NEVER;
+      if (Ustrcmp(eldap_require_cert, "hard") == 0)
+        {
+        tls_option = LDAP_OPT_X_TLS_HARD;
+        }
+      else if (Ustrcmp(eldap_require_cert, "demand") == 0)
+        {
+        tls_option = LDAP_OPT_X_TLS_DEMAND;
+        }
+      else if (Ustrcmp(eldap_require_cert, "allow") == 0)
+        {
+        tls_option = LDAP_OPT_X_TLS_ALLOW;
+        }
+      else if (Ustrcmp(eldap_require_cert, "try") == 0)
+        {
+        tls_option = LDAP_OPT_X_TLS_TRY;
+        }
+      DEBUG(D_lookup)
+        debug_printf("Require certificate overrides LDAP_OPT_X_TLS option (%d)\n",
+                     tls_option);
+      }
+    else
+    #endif  /* LDAP_OPT_X_TLS_REQUIRE_CERT */
+    if (strncmp(ludp->lud_scheme, "ldaps", 5) == 0)
+      {
+      tls_option = LDAP_OPT_X_TLS_HARD;
+      DEBUG(D_lookup)
+        debug_printf("LDAP_OPT_X_TLS_HARD set due to ldaps:// URI\n");
+      }
+    else
+      {
+      tls_option = LDAP_OPT_X_TLS_TRY;
+      DEBUG(D_lookup)
+        debug_printf("LDAP_OPT_X_TLS_TRY set due to ldap:// URI\n");
+      }
+    ldap_set_option(ld, LDAP_OPT_X_TLS, (void *)&tls_option);
+    }
+  #endif  /* LDAP_OPT_X_TLS */
+
+  #ifdef LDAP_OPT_X_TLS_CACERTFILE
+  if (eldap_ca_cert_file != NULL)
+    {
+    ldap_set_option(ldsetctx, LDAP_OPT_X_TLS_CACERTFILE, eldap_ca_cert_file);
+    }
+  #endif
+  #ifdef LDAP_OPT_X_TLS_CACERTDIR
+  if (eldap_ca_cert_dir != NULL)
+    {
+    ldap_set_option(ldsetctx, LDAP_OPT_X_TLS_CACERTDIR, eldap_ca_cert_dir);
+    }
+  #endif
+  #ifdef LDAP_OPT_X_TLS_CERTFILE
+  if (eldap_cert_file != NULL)
+    {
+    ldap_set_option(ldsetctx, LDAP_OPT_X_TLS_CERTFILE, eldap_cert_file);
+    }
+  #endif
+  #ifdef LDAP_OPT_X_TLS_KEYFILE
+  if (eldap_cert_key != NULL)
+    {
+    ldap_set_option(ldsetctx, LDAP_OPT_X_TLS_KEYFILE, eldap_cert_key);
+    }
+  #endif
+  #ifdef LDAP_OPT_X_TLS_CIPHER_SUITE
+  if (eldap_cipher_suite != NULL)
+    {
+    ldap_set_option(ldsetctx, LDAP_OPT_X_TLS_CIPHER_SUITE, eldap_cipher_suite);
+    }
+  #endif
+  #ifdef LDAP_OPT_X_TLS_REQUIRE_CERT
+  if (eldap_require_cert != NULL)
+    {
+    int cert_option = LDAP_OPT_X_TLS_NEVER;
+    if (Ustrcmp(eldap_require_cert, "hard") == 0)
+      {
+      cert_option = LDAP_OPT_X_TLS_HARD;
+      }
+    else if (Ustrcmp(eldap_require_cert, "demand") == 0)
+      {
+      cert_option = LDAP_OPT_X_TLS_DEMAND;
+      }
+    else if (Ustrcmp(eldap_require_cert, "allow") == 0)
+      {
+      cert_option = LDAP_OPT_X_TLS_ALLOW;
+      }
+    else if (Ustrcmp(eldap_require_cert, "try") == 0)
+      {
+      cert_option = LDAP_OPT_X_TLS_TRY;
+      }
+    /* This ldap handle is set at compile time based on client libs. Older
+     * versions want it to be global and newer versions can force a reload
+     * of the TLS context (to reload these settings we are changing from the
+     * default that loaded at instantiation). */
+    rc = ldap_set_option(ldsetctx, LDAP_OPT_X_TLS_REQUIRE_CERT, &cert_option);
+    if (rc)
+      {
+      DEBUG(D_lookup)
+        debug_printf("Unable to set TLS require cert_option(%d) globally: %s\n",
+          cert_option, ldap_err2string(rc));
+      }
+    }
+  #endif
+  #ifdef LDAP_OPT_X_TLS_NEWCTX
+  rc = ldap_set_option(ldsetctx, LDAP_OPT_X_TLS_NEWCTX, &am_server);
+  if (rc)
+    {
+    DEBUG(D_lookup)
+      debug_printf("Unable to reload TLS context %d: %s\n",
+                   rc, ldap_err2string(rc));
+    }
+  #endif
+
+  /* Now add this connection to the chain of cached connections */
+
+  lcp = store_get(sizeof(LDAP_CONNECTION));
+  lcp->host = (host == NULL)? NULL : string_copy(host);
+  lcp->bound = FALSE;
+  lcp->user = NULL;
+  lcp->password = NULL;
+  lcp->port = port;
+  lcp->ld = ld;
+  lcp->next = ldap_connections;
+  lcp->is_start_tls_called = FALSE;
+  ldap_connections = lcp;
+  }
+
+/* Found cached connection */
+
+else
+  {
+  DEBUG(D_lookup)
+    debug_printf("re-using cached connection to LDAP server %s%s\n",
+      host, porttext);
+  }
+
+/* Bind with the user/password supplied, or an anonymous bind if these values
+are NULL, unless a cached connection is already bound with the same values. */
+
+if (!lcp->bound ||
+    (lcp->user == NULL && user != NULL) ||
+    (lcp->user != NULL && user == NULL) ||
+    (lcp->user != NULL && user != NULL && Ustrcmp(lcp->user, user) != 0) ||
+    (lcp->password == NULL && password != NULL) ||
+    (lcp->password != NULL && password == NULL) ||
+    (lcp->password != NULL && password != NULL &&
+      Ustrcmp(lcp->password, password) != 0))
+  {
+  DEBUG(D_lookup) debug_printf("%sbinding with user=%s password=%s\n",
+    (lcp->bound)? "re-" : "", user, password);
+  if (eldap_start_tls && !lcp->is_start_tls_called)
+    {
+#if defined(LDAP_OPT_X_TLS) && !defined(LDAP_LIB_SOLARIS)
+    /* The Oracle LDAP libraries (LDAP_LIB_TYPE=SOLARIS) don't support this.
+     * Note: moreover, they appear to now define LDAP_OPT_X_TLS and still not
+     *       export an ldap_start_tls_s symbol.
+     */
+    if ( (rc = ldap_start_tls_s(lcp->ld, NULL, NULL)) != LDAP_SUCCESS)
+      {
+      *errmsg = string_sprintf("failed to initiate TLS processing on an "
+          "LDAP session to server %s%s - ldap_start_tls_s() returned %d:"
+          " %s", host, porttext, rc, ldap_err2string(rc));
+      goto RETURN_ERROR;
+      }
+    lcp->is_start_tls_called = TRUE;
+#else
+    DEBUG(D_lookup)
+      debug_printf("TLS initiation not supported with this Exim and your LDAP library.\n");
+#endif
+    }
+  if ((msgid = ldap_bind(lcp->ld, CS user, CS password, LDAP_AUTH_SIMPLE))
+       == -1)
+    {
+    *errmsg = string_sprintf("failed to bind the LDAP connection to server "
+      "%s%s - ldap_bind() returned -1", host, porttext);
+    goto RETURN_ERROR;
+    }
+
+  if ((rc = ldap_result( lcp->ld, msgid, 1, timeoutptr, &result )) <= 0)
+    {
+    *errmsg = string_sprintf("failed to bind the LDAP connection to server "
+      "%s%s - LDAP error: %s", host, porttext,
+      rc == -1 ? "result retrieval failed" : "timeout" );
+    result = NULL;
+    goto RETURN_ERROR;
+    }
+
+  rc = ldap_result2error( lcp->ld, result, 0 );
+
+  /* Invalid credentials when just checking credentials returns FAIL. This
+  stops any further servers being tried. */
+
+  if (search_type == SEARCH_LDAP_AUTH && rc == LDAP_INVALID_CREDENTIALS)
+    {
+    DEBUG(D_lookup)
+      debug_printf("Invalid credentials: ldapauth returns FAIL\n");
+    error_yield = FAIL;
+    goto RETURN_ERROR_NOMSG;
+    }
+
+  /* Otherwise we have a problem that doesn't stop further servers from being
+  tried. */
+
+  if (rc != LDAP_SUCCESS)
+    {
+    *errmsg = string_sprintf("failed to bind the LDAP connection to server "
+      "%s%s - LDAP error %d: %s", host, porttext, rc, ldap_err2string(rc));
+    goto RETURN_ERROR;
+    }
+
+  /* Successful bind */
+
+  lcp->bound = TRUE;
+  lcp->user = (user == NULL)? NULL : string_copy(user);
+  lcp->password = (password == NULL)? NULL : string_copy(password);
+
+  ldap_msgfree(result);
+  result = NULL;
+  }
+
+/* If we are just checking credentials, return OK. */
+
+if (search_type == SEARCH_LDAP_AUTH)
+  {
+  DEBUG(D_lookup) debug_printf("Bind succeeded: ldapauth returns OK\n");
+  goto RETURN_OK;
+  }
+
+/* Before doing the search, set the time and size limits (if given). Here again
+the different implementations of LDAP have chosen to do things differently. */
+
+#if defined(LDAP_OPT_SIZELIMIT)
+ldap_set_option(lcp->ld, LDAP_OPT_SIZELIMIT, (void *)&sizelimit);
+ldap_set_option(lcp->ld, LDAP_OPT_TIMELIMIT, (void *)&timelimit);
+#else
+lcp->ld->ld_sizelimit = sizelimit;
+lcp->ld->ld_timelimit = timelimit;
+#endif
+
+/* Similarly for dereferencing aliases. Don't know if this is possible on
+an LDAP library without LDAP_OPT_DEREF. */
+
+#if defined(LDAP_OPT_DEREF)
+ldap_set_option(lcp->ld, LDAP_OPT_DEREF, (void *)&dereference);
+#endif
+
+/* Similarly for the referral setting; should the library follow referrals that
+the LDAP server returns? The conditional is just in case someone uses a library
+without it. */
+
+#if defined(LDAP_OPT_REFERRALS)
+ldap_set_option(lcp->ld, LDAP_OPT_REFERRALS, referrals);
+#endif
+
+/* Start the search on the server. */
+
+DEBUG(D_lookup) debug_printf("Start search\n");
+
+msgid = ldap_search(lcp->ld, ludp->lud_dn, ludp->lud_scope, ludp->lud_filter,
+  ludp->lud_attrs, 0);
+
+if (msgid == -1)
+  {
+  #if defined LDAP_LIB_SOLARIS || defined LDAP_LIB_OPENLDAP2
+  int err;
+  ldap_get_option(lcp->ld, LDAP_OPT_ERROR_NUMBER, &err);
+  *errmsg = string_sprintf("ldap_search failed: %d, %s", err,
+    ldap_err2string(err));
+
+  #else
+  *errmsg = string_sprintf("ldap_search failed");
+  #endif
+
+  goto RETURN_ERROR;
+  }
+
+/* Loop to pick up results as they come in, setting a timeout if one was
+given. */
+
+while ((rc = ldap_result(lcp->ld, msgid, 0, timeoutptr, &result)) ==
+        LDAP_RES_SEARCH_ENTRY)
+  {
+  LDAPMessage  *e;
+
+  DEBUG(D_lookup) debug_printf("ldap_result loop\n");
+
+  for(e = ldap_first_entry(lcp->ld, result);
+      e != NULL;
+      e = ldap_next_entry(lcp->ld, e))
+    {
+    uschar *new_dn;
+    BOOL insert_space = FALSE;
+
+    DEBUG(D_lookup) debug_printf("LDAP entry loop\n");
+
+    rescount++;   /* Count results */
+
+    /* Results for multiple entries values are separated by newlines. */
+
+    if (data != NULL) data = string_cat(data, &size, &ptr, US"\n", 1);
+
+    /* Get the DN from the last result. */
+
+    new_dn = US ldap_get_dn(lcp->ld, e);
+    if (new_dn != NULL)
+      {
+      if (dn != NULL)
+        {
+        #if defined LDAP_LIB_NETSCAPE || defined LDAP_LIB_OPENLDAP2
+        ldap_memfree(dn);
+        #else   /* OPENLDAP 1, UMich, Solaris */
+        free(dn);
+        #endif
+        }
+      /* Save for later */
+      dn = new_dn;
+      }
+
+    /* If the data we want is actually the DN rather than any attribute values,
+    (an "ldapdn" search) add it to the data string. If there are multiple
+    entries, the DNs will be concatenated, but we test for this case below, as
+    for SEARCH_LDAP_SINGLE, and give an error. */
+
+    if (search_type == SEARCH_LDAP_DN)   /* Do not amalgamate these into one */
+      {                                  /* condition, because of the else */
+      if (new_dn != NULL)                /* below, that's for the first only */
+        {
+        data = string_cat(data, &size, &ptr, new_dn, Ustrlen(new_dn));
+        data[ptr] = 0;
+        attribute_found = TRUE;
+        }
+      }
+
+    /* Otherwise, loop through the entry, grabbing attribute values. If there's
+    only one attribute being retrieved, no attribute name is given, and the
+    result is not quoted. Multiple values are separated by (comma, space).
+    If more than one attribute is being retrieved, the data is given as a
+    sequence of name=value pairs, with the value always in quotes. If there are
+    multiple values, they are given within the quotes, comma separated. */
+
+    else for (attr = US ldap_first_attribute(lcp->ld, e, &ber);
+              attr != NULL;
+              attr = US ldap_next_attribute(lcp->ld, e, ber))
+      {
+      if (attr[0] != 0)
+        {
+        /* Get array of values for this attribute. */
+
+        if ((firstval = values = USS ldap_get_values(lcp->ld, e, CS attr))
+             != NULL)
+          {
+          if (attr_count != 1)
+            {
+            if (insert_space)
+              data = string_cat(data, &size, &ptr, US" ", 1);
+            else
+              insert_space = TRUE;
+            data = string_cat(data, &size, &ptr, attr, Ustrlen(attr));
+            data = string_cat(data, &size, &ptr, US"=\"", 2);
+            }
+
+          while (*values != NULL)
+            {
+            uschar *value = *values;
+            int len = Ustrlen(value);
+
+            DEBUG(D_lookup) debug_printf("LDAP attr loop %s:%s\n", attr, value);
+
+            if (values != firstval)
+              data = string_cat(data, &size, &ptr, US",", 1);
+
+            /* For multiple attributes, the data is in quotes. We must escape
+            internal quotes, backslashes, newlines, and must double commas. */
+
+            if (attr_count != 1)
+              {
+              int j;
+              for (j = 0; j < len; j++)
+                {
+                if (value[j] == '\n')
+                  data = string_cat(data, &size, &ptr, US"\\n", 2);
+                else if (value[j] == ',')
+                  data = string_cat(data, &size, &ptr, US",,", 2);
+                else
+                  {
+                  if (value[j] == '\"' || value[j] == '\\')
+                    data = string_cat(data, &size, &ptr, US"\\", 1);
+                  data = string_cat(data, &size, &ptr, value+j, 1);
+                  }
+                }
+              }
+
+            /* For single attributes, just double commas */
+
+           else
+             {
+             int j;
+             for (j = 0; j < len; j++)
+               {
+               if (value[j] == ',')
+                 data = string_cat(data, &size, &ptr, US",,", 2);
+               else
+                 data = string_cat(data, &size, &ptr, value+j, 1);
+               }
+             }
+
+
+            /* Move on to the next value */
+
+            values++;
+            attribute_found = TRUE;
+            }
+
+          /* Closing quote at the end of the data for a named attribute. */
+
+          if (attr_count != 1)
+            data = string_cat(data, &size, &ptr, US"\"", 1);
+
+          /* Free the values */
+
+          ldap_value_free(CSS firstval);
+          }
+        }
+
+      #if defined LDAP_LIB_NETSCAPE || defined LDAP_LIB_OPENLDAP2
+
+      /* Netscape and OpenLDAP2 LDAP's attrs are dynamically allocated and need
+      to be freed. UMich LDAP stores them in static storage and does not require
+      this. */
+
+      ldap_memfree(attr);
+      #endif
+      }        /* End "for" loop for extracting attributes from an entry */
+    }          /* End "for" loop for extracting entries from a result */
+
+  /* Free the result */
+
+  ldap_msgfree(result);
+  result = NULL;
+  }            /* End "while" loop for multiple results */
+
+/* Terminate the dynamic string that we have built and reclaim unused store */
+
+if (data != NULL)
+  {
+  data[ptr] = 0;
+  store_reset(data + ptr + 1);
+  }
+
+/* Copy the last dn into eldap_dn */
+
+if (dn != NULL)
+  {
+  eldap_dn = string_copy(dn);
+  #if defined LDAP_LIB_NETSCAPE || defined LDAP_LIB_OPENLDAP2
+  ldap_memfree(dn);
+  #else   /* OPENLDAP 1, UMich, Solaris */
+  free(dn);
+  #endif
+  }
+
+DEBUG(D_lookup) debug_printf("search ended by ldap_result yielding %d\n",rc);
+
+if (rc == 0)
+  {
+  *errmsg = US"ldap_result timed out";
+  goto RETURN_ERROR;
+  }
+
+/* A return code of -1 seems to mean "ldap_result failed internally or couldn't
+provide you with a message". Other error states seem to exist where
+ldap_result() didn't give us any message from the server at all, leaving result
+set to NULL. Apparently, "the error parameters of the LDAP session handle will
+be set accordingly". That's the best we can do to retrieve an error status; we
+can't use functions like ldap_result2error because they parse a message from
+the server, which we didn't get.
+
+Annoyingly, the different implementations of LDAP have gone for different
+methods of handling error codes and generating error messages. */
+
+if (rc == -1 || result == NULL)
+  {
+  int err;
+  DEBUG(D_lookup) debug_printf("ldap_result failed\n");
+
+  #if defined LDAP_LIB_SOLARIS || defined LDAP_LIB_OPENLDAP2
+    ldap_get_option(lcp->ld, LDAP_OPT_ERROR_NUMBER, &err);
+    *errmsg = string_sprintf("ldap_result failed: %d, %s",
+      err, ldap_err2string(err));
+
+  #elif defined LDAP_LIB_NETSCAPE
+    /* Dubious (surely 'matched' is spurious here?) */
+    (void)ldap_get_lderrno(lcp->ld, &matched, &error1);
+    *errmsg = string_sprintf("ldap_result failed: %s (%s)", error1, matched);
+
+  #else                             /* UMich LDAP aka OpenLDAP 1.x */
+    *errmsg = string_sprintf("ldap_result failed: %d, %s",
+      lcp->ld->ld_errno, ldap_err2string(lcp->ld->ld_errno));
+  #endif
+
+  goto RETURN_ERROR;
+  }
+
+/* A return code that isn't -1 doesn't necessarily mean there were no problems
+with the search. The message must be an LDAP_RES_SEARCH_RESULT or
+LDAP_RES_SEARCH_REFERENCE or else it's something we can't handle. Some versions
+of LDAP do not define LDAP_RES_SEARCH_REFERENCE (LDAP v1 is one, it seems). So
+we don't provide that functionality when we can't. :-) */
+
+if (rc != LDAP_RES_SEARCH_RESULT
+#ifdef LDAP_RES_SEARCH_REFERENCE
+    && rc != LDAP_RES_SEARCH_REFERENCE
+#endif
+   )
+  {
+  *errmsg = string_sprintf("ldap_result returned unexpected code %d", rc);
+  goto RETURN_ERROR;
+  }
+
+/* We have a result message from the server. This doesn't yet mean all is well.
+We need to parse the message to find out exactly what's happened. */
+
+#if defined LDAP_LIB_SOLARIS || defined LDAP_LIB_OPENLDAP2
+  ldap_rc = rc;
+  ldap_parse_rc = ldap_parse_result(lcp->ld, result, &rc, CSS &matched,
+    CSS &error2, NULL, NULL, 0);
+  DEBUG(D_lookup) debug_printf("ldap_parse_result: %d\n", ldap_parse_rc);
+  if (ldap_parse_rc < 0 &&
+      (ldap_parse_rc != LDAP_NO_RESULTS_RETURNED
+      #ifdef LDAP_RES_SEARCH_REFERENCE
+      || ldap_rc != LDAP_RES_SEARCH_REFERENCE
+      #endif
+     ))
+    {
+    *errmsg = string_sprintf("ldap_parse_result failed %d", ldap_parse_rc);
+    goto RETURN_ERROR;
+    }
+  error1 = US ldap_err2string(rc);
+
+#elif defined LDAP_LIB_NETSCAPE
+  /* Dubious (it doesn't reference 'result' at all!) */
+  rc = ldap_get_lderrno(lcp->ld, &matched, &error1);
+
+#else                             /* UMich LDAP aka OpenLDAP 1.x */
+  rc = ldap_result2error(lcp->ld, result, 0);
+  error1 = ldap_err2string(rc);
+  error2 = lcp->ld->ld_error;
+  matched = lcp->ld->ld_matched;
+#endif
+
+/* Process the status as follows:
+
+  (1) If we get LDAP_SIZELIMIT_EXCEEDED, just carry on, to return the
+      truncated result list.
+
+  (2) If we get LDAP_RES_SEARCH_REFERENCE, also just carry on. This was a
+      submitted patch that is reported to "do the right thing" with Solaris
+      LDAP libraries. (The problem it addresses apparently does not occur with
+      Open LDAP.)
+
+  (3) The range of errors defined by LDAP_NAME_ERROR generally mean "that
+      object does not, or cannot, exist in the database". For those cases we
+      fail the lookup.
+
+  (4) All other non-successes here are treated as some kind of problem with
+      the lookup, so return DEFER (which is the default in error_yield).
+*/
+
+DEBUG(D_lookup) debug_printf("ldap_parse_result yielded %d: %s\n",
+  rc, ldap_err2string(rc));
+
+if (rc != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED
+    #ifdef LDAP_RES_SEARCH_REFERENCE
+    && rc != LDAP_RES_SEARCH_REFERENCE
+    #endif
+    )
+  {
+  *errmsg = string_sprintf("LDAP search failed - error %d: %s%s%s%s%s",
+    rc,
+    (error1 != NULL)?                       error1  : US"",
+    (error2 != NULL && error2[0] != 0)?     US"/"   : US"",
+    (error2 != NULL)?                       error2  : US"",
+    (matched != NULL && matched[0] != 0)?   US"/"   : US"",
+    (matched != NULL)?                      matched : US"");
+
+  #if defined LDAP_NAME_ERROR
+  if (LDAP_NAME_ERROR(rc))
+  #elif defined NAME_ERROR    /* OPENLDAP1 calls it this */
+  if (NAME_ERROR(rc))
+  #else
+  if (rc == LDAP_NO_SUCH_OBJECT)
+  #endif
+
+    {
+    DEBUG(D_lookup) debug_printf("lookup failure forced\n");
+    error_yield = FAIL;
+    }
+  goto RETURN_ERROR;
+  }
+
+/* The search succeeded. Check if we have too many results */
+
+if (search_type != SEARCH_LDAP_MULTIPLE && rescount > 1)
+  {
+  *errmsg = string_sprintf("LDAP search: more than one entry (%d) was returned "
+    "(filter not specific enough?)", rescount);
+  goto RETURN_ERROR_BREAK;
+  }
+
+/* Check if we have too few (zero) entries */
+
+if (rescount < 1)
+  {
+  *errmsg = string_sprintf("LDAP search: no results");
+  error_yield = FAIL;
+  goto RETURN_ERROR_BREAK;
+  }
+
+/* If an entry was found, but it had no attributes, we behave as if no entries
+were found, that is, the lookup failed. */
+
+if (!attribute_found)
+  {
+  *errmsg = US"LDAP search: found no attributes";
+  error_yield = FAIL;
+  goto RETURN_ERROR;
+  }
+
+/* Otherwise, it's all worked */
+
+DEBUG(D_lookup) debug_printf("LDAP search: returning: %s\n", data);
+*res = data;
+
+RETURN_OK:
+if (result != NULL) ldap_msgfree(result);
+ldap_free_urldesc(ludp);
+return OK;
+
+/* Error returns */
+
+RETURN_ERROR_BREAK:
+*defer_break = TRUE;
+
+RETURN_ERROR:
+DEBUG(D_lookup) debug_printf("%s\n", *errmsg);
+
+RETURN_ERROR_NOMSG:
+if (result != NULL) ldap_msgfree(result);
+if (ludp != NULL) ldap_free_urldesc(ludp);
+
+#if defined LDAP_LIB_OPENLDAP2
+  if (error2 != NULL)  ldap_memfree(error2);
+  if (matched != NULL) ldap_memfree(matched);
+#endif
+
+return error_yield;
+}
+
+
+
+/*************************************************
+*        Internal search control function        *
+*************************************************/
+
+/* This function is called from eldap_find(), eldapauth_find(), eldapdn_find(),
+and eldapm_find() with a difference in the "search_type" argument. It controls
+calls to perform_ldap_search() which actually does the work. We call that
+repeatedly for certain types of defer in the case when the URL contains no host
+name and eldap_default_servers is set to a list of servers to try. This gives
+more control than just passing over a list of hosts to ldap_open() because it
+handles other kinds of defer as well as just a failure to open. Note that the
+URL is defined to contain either zero or one "hostport" only.
+
+Parameter data in addition to the URL can be passed as preceding text in the
+string, as items of the form XXX=yyy. The URL itself can be detected because it
+must begin "ldapx://", where x is empty, s, or i.
+
+Arguments:
+  ldap_url      the URL to be looked up, optionally preceded by other parameter
+                settings
+  search_type   SEARCH_LDAP_MULTIPLE allows values from multiple entries
+                SEARCH_LDAP_SINGLE allows values from one entry only
+                SEARCH_LDAP_DN gets the DN from one entry
+  res           set to point at the result
+  errmsg        set to point a message if result is not OK
+
+Returns:        OK or FAIL or DEFER
+*/
+
+static int
+control_ldap_search(uschar *ldap_url, int search_type, uschar **res,
+  uschar **errmsg)
+{
+BOOL defer_break = FALSE;
+int timelimit = LDAP_NO_LIMIT;
+int sizelimit = LDAP_NO_LIMIT;
+int tcplimit = 0;
+int sep = 0;
+int dereference = LDAP_DEREF_NEVER;
+void* referrals = LDAP_OPT_ON;
+uschar *url = ldap_url;
+uschar *p;
+uschar *user = NULL;
+uschar *password = NULL;
+uschar *local_servers = NULL;
+uschar *server, *list;
+uschar buffer[512];
+
+while (isspace(*url)) url++;
+
+/* Until the string begins "ldap", search for the other parameter settings that
+are recognized. They are of the form NAME=VALUE, with the value being
+optionally double-quoted. There must still be a space after it, however. No
+NAME has the value "ldap". */
+
+while (strncmpic(url, US"ldap", 4) != 0)
+  {
+  uschar *name = url;
+  while (*url != 0 && *url != '=') url++;
+  if (*url == '=')
+    {
+    int namelen;
+    uschar *value;
+    namelen = ++url - name;
+    value = string_dequote(&url);
+    if (isspace(*url))
+      {
+      if (strncmpic(name, US"USER=", namelen) == 0) user = value;
+      else if (strncmpic(name, US"PASS=", namelen) == 0) password = value;
+      else if (strncmpic(name, US"SIZE=", namelen) == 0) sizelimit = Uatoi(value);
+      else if (strncmpic(name, US"TIME=", namelen) == 0) timelimit = Uatoi(value);
+      else if (strncmpic(name, US"CONNECT=", namelen) == 0) tcplimit = Uatoi(value);
+      else if (strncmpic(name, US"NETTIME=", namelen) == 0) tcplimit = Uatoi(value);
+      else if (strncmpic(name, US"SERVERS=", namelen) == 0) local_servers = value;
+
+      /* Don't know if all LDAP libraries have LDAP_OPT_DEREF */
+
+      #ifdef LDAP_OPT_DEREF
+      else if (strncmpic(name, US"DEREFERENCE=", namelen) == 0)
+        {
+        if (strcmpic(value, US"never") == 0) dereference = LDAP_DEREF_NEVER;
+        else if (strcmpic(value, US"searching") == 0)
+          dereference = LDAP_DEREF_SEARCHING;
+        else if (strcmpic(value, US"finding") == 0)
+          dereference = LDAP_DEREF_FINDING;
+        if (strcmpic(value, US"always") == 0) dereference = LDAP_DEREF_ALWAYS;
+        }
+      #else
+      else if (strncmpic(name, US"DEREFERENCE=", namelen) == 0)
+        {
+        *errmsg = string_sprintf("LDAP_OP_DEREF not defined in this LDAP "
+          "library - cannot use \"dereference\"");
+        DEBUG(D_lookup) debug_printf("%s\n", *errmsg);
+        return DEFER;
+        }
+      #endif
+
+      #ifdef LDAP_OPT_REFERRALS
+      else if (strncmpic(name, US"REFERRALS=", namelen) == 0)
+        {
+        if (strcmpic(value, US"follow") == 0) referrals = LDAP_OPT_ON;
+        else if (strcmpic(value, US"nofollow") == 0) referrals = LDAP_OPT_OFF;
+        else
+          {
+          *errmsg = string_sprintf("LDAP option REFERRALS is not \"follow\" "
+            "or \"nofollow\"");
+          DEBUG(D_lookup) debug_printf("%s\n", *errmsg);
+          return DEFER;
+          }
+        }
+      #else
+      else if (strncmpic(name, US"REFERRALS=", namelen) == 0)
+        {
+        *errmsg = string_sprintf("LDAP_OP_REFERRALS not defined in this LDAP "
+          "library - cannot use \"referrals\"");
+        DEBUG(D_lookup) debug_printf("%s\n", *errmsg);
+        return DEFER;
+        }
+      #endif
+
+      else
+        {
+        *errmsg =
+          string_sprintf("unknown parameter \"%.*s\" precedes LDAP URL",
+            namelen, name);
+        DEBUG(D_lookup) debug_printf("LDAP query error: %s\n", *errmsg);
+        return DEFER;
+        }
+      while (isspace(*url)) url++;
+      continue;
+      }
+    }
+  *errmsg = US"malformed parameter setting precedes LDAP URL";
+  DEBUG(D_lookup) debug_printf("LDAP query error: %s\n", *errmsg);
+  return DEFER;
+  }
+
+/* If user is set, de-URL-quote it. Some LDAP libraries do this for themselves,
+but it seems that not all behave like this. The DN for the user is often the
+result of ${quote_ldap_dn:...} quoting, which does apply URL quoting, because
+that is needed when the DN is used as a base DN in a query. Sigh. This is all
+far too complicated. */
+
+if (user != NULL)
+  {
+  uschar *s;
+  uschar *t = user;
+  for (s = user; *s != 0; s++)
+    {
+    int c, d;
+    if (*s == '%' && isxdigit(c=s[1]) && isxdigit(d=s[2]))
+      {
+      c = tolower(c);
+      d = tolower(d);
+      *t++ =
+        (((c >= 'a')? (10 + c - 'a') : c - '0') << 4) |
+         ((d >= 'a')? (10 + d - 'a') : d - '0');
+      s += 2;
+      }
+    else *t++ = *s;
+    }
+  *t = 0;
+  }
+
+DEBUG(D_lookup)
+  debug_printf("LDAP parameters: user=%s pass=%s size=%d time=%d connect=%d "
+    "dereference=%d referrals=%s\n", user, password, sizelimit, timelimit,
+    tcplimit, dereference, (referrals == LDAP_OPT_ON)? "on" : "off");
+
+/* If the request is just to check authentication, some credentials must
+be given. The password must not be empty because LDAP binds with an empty
+password are considered anonymous, and will succeed on most installations. */
+
+if (search_type == SEARCH_LDAP_AUTH)
+  {
+  if (user == NULL || password == NULL)
+    {
+    *errmsg = US"ldapauth lookups must specify the username and password";
+    return DEFER;
+    }
+  if (password[0] == 0)
+    {
+    DEBUG(D_lookup) debug_printf("Empty password: ldapauth returns FAIL\n");
+    return FAIL;
+    }
+  }
+
+/* Check for valid ldap url starters */
+
+p = url + 4;
+if (tolower(*p) == 's' || tolower(*p) == 'i') p++;
+if (Ustrncmp(p, "://", 3) != 0)
+  {
+  *errmsg = string_sprintf("LDAP URL does not start with \"ldap://\", "
+    "\"ldaps://\", or \"ldapi://\" (it starts with \"%.16s...\")", url);
+  DEBUG(D_lookup) debug_printf("LDAP query error: %s\n", *errmsg);
+  return DEFER;
+  }
+
+/* No default servers, or URL contains a server name: just one attempt */
+
+if ((eldap_default_servers == NULL && local_servers == NULL) || p[3] != '/')
+  {
+  return perform_ldap_search(url, NULL, 0, search_type, res, errmsg,
+    &defer_break, user, password, sizelimit, timelimit, tcplimit, dereference,
+    referrals);
+  }
+
+/* Loop through the default servers until OK or FAIL. Use local_servers list
+ * if defined in the lookup, otherwise use the global default list */
+list = (local_servers == NULL) ? eldap_default_servers : local_servers;
+while ((server = string_nextinlist(&list, &sep, buffer, sizeof(buffer))) != NULL)
+  {
+  int rc;
+  int port = 0;
+  uschar *colon = Ustrchr(server, ':');
+  if (colon != NULL)
+    {
+    *colon = 0;
+    port = Uatoi(colon+1);
+    }
+  rc = perform_ldap_search(url, server, port, search_type, res, errmsg,
+    &defer_break, user, password, sizelimit, timelimit, tcplimit, dereference,
+    referrals);
+  if (rc != DEFER || defer_break) return rc;
+  }
+
+return DEFER;
+}
+
+
+
+/*************************************************
+*               Find entry point                 *
+*************************************************/
+
+/* See local README for interface description. The different kinds of search
+are handled by a common function, with a flag to differentiate between them.
+The handle and filename arguments are not used. */
+
+static int
+eldap_find(void *handle, uschar *filename, uschar *ldap_url, int length,
+  uschar **result, uschar **errmsg, BOOL *do_cache)
+{
+/* Keep picky compilers happy */
+do_cache = do_cache;
+return(control_ldap_search(ldap_url, SEARCH_LDAP_SINGLE, result, errmsg));
+}
+
+static int
+eldapm_find(void *handle, uschar *filename, uschar *ldap_url, int length,
+  uschar **result, uschar **errmsg, BOOL *do_cache)
+{
+/* Keep picky compilers happy */
+do_cache = do_cache;
+return(control_ldap_search(ldap_url, SEARCH_LDAP_MULTIPLE, result, errmsg));
+}
+
+static int
+eldapdn_find(void *handle, uschar *filename, uschar *ldap_url, int length,
+  uschar **result, uschar **errmsg, BOOL *do_cache)
+{
+/* Keep picky compilers happy */
+do_cache = do_cache;
+return(control_ldap_search(ldap_url, SEARCH_LDAP_DN, result, errmsg));
+}
+
+int
+eldapauth_find(void *handle, uschar *filename, uschar *ldap_url, int length,
+  uschar **result, uschar **errmsg, BOOL *do_cache)
+{
+/* Keep picky compilers happy */
+do_cache = do_cache;
+return(control_ldap_search(ldap_url, SEARCH_LDAP_AUTH, result, errmsg));
+}
+
+
+
+/*************************************************
+*              Open entry point                  *
+*************************************************/
+
+/* See local README for interface description. */
+
+static void *
+eldap_open(uschar *filename, uschar **errmsg)
+{
+return (void *)(1);    /* Just return something non-null */
+}
+
+
+
+/*************************************************
+*               Tidy entry point                 *
+*************************************************/
+
+/* See local README for interface description.
+Make sure that eldap_dn does not refer to reclaimed or worse, freed store */
+
+static void
+eldap_tidy(void)
+{
+LDAP_CONNECTION *lcp = NULL;
+eldap_dn = NULL;
+
+while ((lcp = ldap_connections) != NULL)
+  {
+  DEBUG(D_lookup) debug_printf("unbind LDAP connection to %s:%d\n", lcp->host,
+    lcp->port);
+  if(lcp->bound == TRUE)
+    ldap_unbind(lcp->ld);
+  ldap_connections = lcp->next;
+  }
+}
+
+
+
+/*************************************************
+*               Quote entry point                *
+*************************************************/
+
+/* LDAP quoting is unbelievably messy. For a start, two different levels of
+quoting have to be done: LDAP quoting, and URL quoting. The current
+specification is the result of a suggestion by Brian Candler. It recognizes
+two separate cases:
+
+(1) For text that appears in a search filter, the following escapes are
+    required (see RFC 2254):
+
+      *    ->   \2A
+      (    ->   \28
+      )    ->   \29
+      \    ->   \5C
+     NULL  ->   \00
+
+    Then the entire filter text must be URL-escaped. This kind of quoting is
+    implemented by ${quote_ldap:....}. Note that we can never have a NULL
+    in the input string, because that's a terminator.
+
+(2) For a DN that is part of a URL (i.e. the base DN), the characters
+
+      , + " \ < > ;
+
+    must be quoted by backslashing. See RFC 2253. Leading and trailing spaces
+    must be escaped, as must a leading #. Then the string must be URL-quoted.
+    This type of quoting is implemented by ${quote_ldap_dn:....}.
+
+For URL quoting, the only characters that need not be quoted are the
+alphamerics and
+
+  ! $ ' ( ) * + - . _
+
+All the others must be hexified and preceded by %. This includes the
+backslashes used for LDAP quoting.
+
+For a DN that is given in the USER parameter for authentication, we need the
+same initial quoting as (2) but in this case, the result must NOT be
+URL-escaped, because it isn't a URL. The way this is handled is by
+de-URL-quoting the text when processing the USER parameter in
+control_ldap_search() above. That means that the same quote operator can be
+used. This has the additional advantage that spaces in the DN won't cause
+parsing problems. For example:
+
+  USER=cn=${quote_ldap_dn:$1},%20dc=example,%20dc=com
+
+should be safe if there are spaces in $1.
+
+
+Arguments:
+  s          the string to be quoted
+  opt        additional option text or NULL if none
+             only "dn" is recognized
+
+Returns:     the processed string or NULL for a bad option
+*/
+
+
+
+/* The characters in this string, together with alphanumerics, never need
+quoting in any way. */
+
+#define ALWAYS_LITERAL  "!$'-._"
+
+/* The special characters in this string do not need to be URL-quoted. The set
+is a bit larger than the general literals. */
+
+#define URL_NONQUOTE    ALWAYS_LITERAL "()*+"
+
+/* The following macros define the characters that are quoted by quote_ldap and
+quote_ldap_dn, respectively. */
+
+#define LDAP_QUOTE      "*()\\"
+#define LDAP_DN_QUOTE   ",+\"\\<>;"
+
+
+
+static uschar *
+eldap_quote(uschar *s, uschar *opt)
+{
+register int c;
+int count = 0;
+int len = 0;
+BOOL dn = FALSE;
+uschar *t = s;
+uschar *quoted;
+
+/* Test for a DN quotation. */
+
+if (opt != NULL)
+  {
+  if (Ustrcmp(opt, "dn") != 0) return NULL;    /* No others recognized */
+  dn = TRUE;
+  }
+
+/* Compute how much extra store we need for the string. This doesn't have to be
+exact as long as it isn't an underestimate. The worst case is the addition of 5
+extra bytes for a single character. This occurs for certain characters in DNs,
+where, for example, < turns into %5C%3C. For simplicity, we just add 5 for each
+possibly escaped character. The really fast way would be just to test for
+non-alphanumerics, but it is probably better to spot a few others that are
+never escaped, because if there are no specials at all, we can avoid copying
+the string. */
+
+while ((c = *t++) != 0)
+  {
+  len++;
+  if (!isalnum(c) && Ustrchr(ALWAYS_LITERAL, c) == NULL) count += 5;
+  }
+if (count == 0) return s;
+
+/* Get sufficient store to hold the quoted string */
+
+t = quoted = store_get(len + count + 1);
+
+/* Handle plain quote_ldap */
+
+if (!dn)
+  {
+  while ((c = *s++) != 0)
+    {
+    if (!isalnum(c))
+      {
+      if (Ustrchr(LDAP_QUOTE, c) != NULL)
+        {
+        sprintf(CS t, "%%5C%02X", c);        /* e.g. * => %5C2A */
+        t += 5;
+        continue;
+        }
+      if (Ustrchr(URL_NONQUOTE, c) == NULL)  /* e.g. ] => %5D */
+        {
+        sprintf(CS t, "%%%02X", c);
+        t += 3;
+        continue;
+        }
+      }
+    *t++ = c;                                /* unquoted character */
+    }
+  }
+
+/* Handle quote_ldap_dn */
+
+else
+  {
+  uschar *ss = s + len;
+
+  /* Find the last char before any trailing spaces */
+
+  while (ss > s && ss[-1] == ' ') ss--;
+
+  /* Quote leading spaces and sharps */
+
+  for (; s < ss; s++)
+    {
+    if (*s != ' ' && *s != '#') break;
+    sprintf(CS t, "%%5C%%%02X", *s);
+    t += 6;
+    }
+
+  /* Handle the rest of the string, up to the trailing spaces */
+
+  while (s < ss)
+    {
+    c = *s++;
+    if (!isalnum(c))
+      {
+      if (Ustrchr(LDAP_DN_QUOTE, c) != NULL)
+        {
+        Ustrncpy(t, "%5C", 3);               /* insert \ where needed */
+        t += 3;                              /* fall through to check URL */
+        }
+      if (Ustrchr(URL_NONQUOTE, c) == NULL)  /* e.g. ] => %5D */
+        {
+        sprintf(CS t, "%%%02X", c);
+        t += 3;
+        continue;
+        }
+      }
+    *t++ = c;    /* unquoted character, or non-URL quoted after %5C */
+    }
+
+  /* Handle the trailing spaces */
+
+  while (*ss++ != 0)
+    {
+    Ustrncpy(t, "%5C%20", 6);
+    t += 6;
+    }
+  }
+
+/* Terminate the new string and return */
+
+*t = 0;
+return quoted;
+}
+
+
+
+/*************************************************
+*         Version reporting entry point          *
+*************************************************/
+
+/* See local README for interface description. */
+
+#include "../version.h"
+
+void
+ldap_version_report(FILE *f)
+{
+#ifdef DYNLOOKUP
+fprintf(f, "Library version: LDAP: Exim version %s\n", EXIM_VERSION_STR);
+#endif
+}
+
+
+static lookup_info ldap_lookup_info = {
+  US"ldap",                      /* lookup name */
+  lookup_querystyle,             /* query-style lookup */
+  eldap_open,                    /* open function */
+  NULL,                          /* check function */
+  eldap_find,                    /* find function */
+  NULL,                          /* no close function */
+  eldap_tidy,                    /* tidy function */
+  eldap_quote,                   /* quoting function */
+  ldap_version_report            /* version reporting */
+};
+
+static lookup_info ldapdn_lookup_info = {
+  US"ldapdn",                     /* lookup name */
+  lookup_querystyle,             /* query-style lookup */
+  eldap_open,       /* sic */    /* open function */
+  NULL,                          /* check function */
+  eldapdn_find,                  /* find function */
+  NULL,                          /* no close function */
+  eldap_tidy,       /* sic */    /* tidy function */
+  eldap_quote,      /* sic */    /* quoting function */
+  NULL                           /* no version reporting (redundant) */
+};
+
+static lookup_info ldapm_lookup_info = {
+  US"ldapm",                     /* lookup name */
+  lookup_querystyle,             /* query-style lookup */
+  eldap_open,       /* sic */    /* open function */
+  NULL,                          /* check function */
+  eldapm_find,                   /* find function */
+  NULL,                          /* no close function */
+  eldap_tidy,       /* sic */    /* tidy function */
+  eldap_quote,      /* sic */    /* quoting function */
+  NULL                           /* no version reporting (redundant) */
+};
+
+#ifdef DYNLOOKUP
+#define ldap_lookup_module_info _lookup_module_info
+#endif
+
+static lookup_info *_lookup_list[] = { &ldap_lookup_info, &ldapdn_lookup_info, &ldapm_lookup_info };
+lookup_module_info ldap_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 3 };
+
+/* End of lookups/ldap.c */
diff --git a/src/lookups/ldap.h b/src/lookups/ldap.h
new file mode 100644 (file)
index 0000000..5ce756d
--- /dev/null
@@ -0,0 +1,13 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Header for eldapauth_find */
+
+extern int     eldapauth_find(void *, uschar *, uschar *, int, uschar **,
+                 uschar **, BOOL *);
+
+/* End of lookups/ldap.h */
diff --git a/src/lookups/lf_check_file.c b/src/lookups/lf_check_file.c
new file mode 100644 (file)
index 0000000..43198a8
--- /dev/null
@@ -0,0 +1,113 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+
+#include "../exim.h"
+#include "lf_functions.h"
+
+
+
+/*************************************************
+*         Check a file's credentials             *
+*************************************************/
+
+/* fstat can normally be expected to work on an open file, but there are some
+NFS states where it may not.
+
+Arguments:
+  fd         an open file descriptor or -1
+  filename   a file name if fd is -1
+  s_type     type of file (S_IFREG or S_IFDIR)
+  modemask   a mask specifying mode bits that must *not* be set
+  owners     NULL or a list of of allowable uids, count in the first item
+  owngroups  NULL or a list of allowable gids, count in the first item
+  type       name of lookup type for putting in error message
+  errmsg     where to put an error message
+
+Returns:     -1 stat() or fstat() failed
+              0 OK
+             +1 something didn't match
+
+Side effect: sets errno to ERRNO_BADUGID, ERRNO_NOTREGULAR or ERRNO_BADMODE for
+             bad uid/gid, not a regular file, or bad mode; otherwise leaves it
+             to what fstat set it to.
+*/
+
+int
+lf_check_file(int fd, uschar *filename, int s_type, int modemask, uid_t *owners,
+  gid_t *owngroups, const char *type, uschar **errmsg)
+{
+int i;
+struct stat statbuf;
+
+if ((fd >= 0 && fstat(fd, &statbuf) != 0) ||
+    (fd  < 0 && Ustat(filename, &statbuf) != 0))
+  {
+  int save_errno = errno;
+  *errmsg = string_sprintf("%s: stat failed", filename);
+  errno = save_errno;
+  return -1;
+  }
+
+if ((statbuf.st_mode & S_IFMT) != s_type)
+  {
+  if (s_type == S_IFREG)
+    {
+    *errmsg = string_sprintf("%s is not a regular file (%s lookup)",
+      filename, type);
+    errno = ERRNO_NOTREGULAR;
+    }
+  else
+    {
+    *errmsg = string_sprintf("%s is not a directory (%s lookup)",
+      filename, type);
+    errno = ERRNO_NOTDIRECTORY;
+    }
+  return +1;
+  }
+
+if ((statbuf.st_mode & modemask) != 0)
+  {
+  *errmsg = string_sprintf("%s (%s lookup): file mode %.4o should not contain "
+    "%.4o", filename, type,  statbuf.st_mode & 07777,
+    statbuf.st_mode & modemask);
+  errno = ERRNO_BADMODE;
+  return +1;
+  }
+
+if (owners != NULL)
+  {
+  BOOL uid_ok = FALSE;
+  for (i = 1; i <= (int)owners[0]; i++)
+    if (owners[i] == statbuf.st_uid) { uid_ok = TRUE; break; }
+  if (!uid_ok)
+    {
+    *errmsg = string_sprintf("%s (%s lookup): file has wrong owner", filename,
+      type);
+    errno = ERRNO_BADUGID;
+    return +1;
+    }
+  }
+
+if (owngroups != NULL)
+  {
+  BOOL gid_ok = FALSE;
+  for (i = 1; i <= (int)owngroups[0]; i++)
+    if (owngroups[i] == statbuf.st_gid) { gid_ok = TRUE; break; }
+  if (!gid_ok)
+    {
+    *errmsg = string_sprintf("%s (%s lookup): file has wrong group", filename,
+      type);
+    errno = ERRNO_BADUGID;
+    return +1;
+    }
+  }
+
+return 0;
+}
+
+/* End of lf_check_file.c */
diff --git a/src/lookups/lf_functions.h b/src/lookups/lf_functions.h
new file mode 100644 (file)
index 0000000..9f7c7a2
--- /dev/null
@@ -0,0 +1,17 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Header for the functions that are shared by the lookups */
+
+extern int     lf_check_file(int, uschar *, int, int, uid_t *, gid_t *,
+                 const char *, uschar **);
+extern uschar *lf_quote(uschar *, uschar *, int, uschar *, int *, int *);
+extern int     lf_sqlperform(uschar *, uschar *, uschar *, uschar *, uschar **,
+                 uschar **, BOOL *, int(*)(uschar *, uschar *, uschar **,
+                 uschar **, BOOL *, BOOL *));
+
+/* End of lf_functions.h */
diff --git a/src/lookups/lf_quote.c b/src/lookups/lf_quote.c
new file mode 100644 (file)
index 0000000..60c0a76
--- /dev/null
@@ -0,0 +1,65 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+
+#include "../exim.h"
+#include "lf_functions.h"
+
+
+/*************************************************
+*   Add string to result, quoting if necessary   *
+*************************************************/
+
+/* This function is called by some lookups that create name=value result
+strings, to handle the quoting of the data. It adds "name=" to the result,
+followed by appropriately quoted data, followed by a single space.
+
+Arguments:
+  name           the field name
+  value          the data value
+  vlength        the data length
+  result         the result pointer
+  asize          points to the size variable
+  aoffset        points to the offset variable
+
+Returns:         the result pointer (possibly updated)
+*/
+
+uschar *
+lf_quote(uschar *name, uschar *value, int vlength, uschar *result, int *asize,
+  int *aoffset)
+{
+result = string_append(result, asize, aoffset, 2, name, US"=");
+
+/* NULL is handled as an empty string */
+
+if (value == NULL) value = US"";
+
+/* Quote the value if it is empty, contains white space, or starts with a quote
+character. */
+
+if (value[0] == 0 || Ustrpbrk(value, " \t\n\r") != NULL || value[0] == '\"')
+  {
+  int j;
+  result = string_cat(result, asize, aoffset, US"\"", 1);
+  for (j = 0; j < vlength; j++)
+    {
+    if (value[j] == '\"' || value[j] == '\\')
+      result = string_cat(result, asize, aoffset, US"\\", 1);
+    result = string_cat(result, asize, aoffset, US value+j, 1);
+    }
+  result = string_cat(result, asize, aoffset, US"\"", 1);
+  }
+else
+  {
+  result = string_cat(result, asize, aoffset, US value, vlength);
+  }
+
+return string_cat(result, asize, aoffset, US" ", 1);
+}
+
+/* End of lf_quote.c */
diff --git a/src/lookups/lf_sqlperform.c b/src/lookups/lf_sqlperform.c
new file mode 100644 (file)
index 0000000..d430cd4
--- /dev/null
@@ -0,0 +1,137 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+
+#include "../exim.h"
+#include "lf_functions.h"
+
+
+
+/*************************************************
+*    Call SQL server(s) to run an actual query   *
+*************************************************/
+
+/* All the SQL lookups are of the same form, with a list of servers to try
+until one can be accessed. It is now also possible to provide the server data
+as part of the query. This function manages server selection and looping; each
+lookup has its own function for actually performing the lookup.
+
+Arguments:
+  name           the lookup name, e.g. "MySQL"
+  optionname     the name of the servers option, e.g. "mysql_servers"
+  optserverlist  the value of the servers option
+  query          the query
+  result         where to pass back the result
+  errmsg         where to pass back an error message
+  do_cache       to be set FALSE if data is changed
+  func           the lookup function to call
+
+Returns:         the return from the lookup function, or DEFER
+*/
+
+int
+lf_sqlperform(uschar *name, uschar *optionname, uschar *optserverlist,
+  uschar *query, uschar **result, uschar **errmsg, BOOL *do_cache,
+  int(*fn)(uschar *, uschar *, uschar **, uschar **, BOOL *, BOOL *))
+{
+int sep, rc;
+uschar *server;
+uschar *serverlist;
+uschar buffer[512];
+BOOL defer_break = FALSE;
+
+DEBUG(D_lookup) debug_printf("%s query: %s\n", name, query);
+
+/* Handle queries that do not have server information at the start. */
+
+if (Ustrncmp(query, "servers", 7) != 0)
+  {
+  sep = 0;
+  serverlist = optserverlist;
+  while ((server = string_nextinlist(&serverlist, &sep, buffer,
+          sizeof(buffer))) != NULL)
+    {
+    rc = (*fn)(query, server, result, errmsg, &defer_break, do_cache);
+    if (rc != DEFER || defer_break) return rc;
+    }
+  if (optserverlist == NULL)
+    *errmsg = string_sprintf("no %s servers defined (%s option)", name,
+      optionname);
+  }
+
+/* Handle queries that do have server information at the start. */
+
+else
+  {
+  int qsep;
+  uschar *s, *ss;
+  uschar *qserverlist;
+  uschar *qserver;
+  uschar qbuffer[512];
+
+  s = query + 7;
+  while (isspace(*s)) s++;
+  if (*s++ != '=')
+    {
+    *errmsg = string_sprintf("missing = after \"servers\" in %s lookup", name);
+    return DEFER;
+    }
+  while (isspace(*s)) s++;
+
+  ss = Ustrchr(s, ';');
+  if (ss == NULL)
+    {
+    *errmsg = string_sprintf("missing ; after \"servers=\" in %s lookup",
+      name);
+    return DEFER;
+    }
+
+  if (ss == s)
+    {
+    *errmsg = string_sprintf("\"servers=\" defines no servers in \"%s\"",
+      query);
+    return DEFER;
+    }
+
+  qserverlist = string_sprintf("%.*s", ss - s, s);
+  qsep = 0;
+
+  while ((qserver = string_nextinlist(&qserverlist, &qsep, qbuffer,
+           sizeof(qbuffer))) != NULL)
+    {
+    if (Ustrchr(qserver, '/') != NULL)
+      server = qserver;
+    else
+      {
+      int len = Ustrlen(qserver);
+
+      sep = 0;
+      serverlist = optserverlist;
+      while ((server = string_nextinlist(&serverlist, &sep, buffer,
+              sizeof(buffer))) != NULL)
+        {
+        if (Ustrncmp(server, qserver, len) == 0 && server[len] == '/')
+          break;
+        }
+
+      if (server == NULL)
+        {
+        *errmsg = string_sprintf("%s server \"%s\" not found in %s", name,
+          qserver, optionname);
+        return DEFER;
+        }
+      }
+
+    rc = (*fn)(ss+1, server, result, errmsg, &defer_break, do_cache);
+    if (rc != DEFER || defer_break) return rc;
+    }
+  }
+
+return DEFER;
+}
+
+/* End of lf_sqlperform.c */
diff --git a/src/lookups/lsearch.c b/src/lookups/lsearch.c
new file mode 100644 (file)
index 0000000..537cac7
--- /dev/null
@@ -0,0 +1,488 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+#include "../exim.h"
+#include "lf_functions.h"
+
+/* Codes for the different kinds of lsearch that are supported */
+
+enum {
+  LSEARCH_PLAIN,        /* Literal keys */
+  LSEARCH_WILD,         /* Wild card keys, expanded */
+  LSEARCH_NWILD,        /* Wild card keys, not expanded */
+  LSEARCH_IP            /* IP addresses and networks */
+};
+
+
+
+/*************************************************
+*              Open entry point                  *
+*************************************************/
+
+/* See local README for interface description */
+
+static void *
+lsearch_open(uschar *filename, uschar **errmsg)
+{
+FILE *f = Ufopen(filename, "rb");
+if (f == NULL)
+  {
+  int save_errno = errno;
+  *errmsg = string_open_failed(errno, "%s for linear search", filename);
+  errno = save_errno;
+  return NULL;
+  }
+return f;
+}
+
+
+
+/*************************************************
+*             Check entry point                  *
+*************************************************/
+
+static BOOL
+lsearch_check(void *handle, uschar *filename, int modemask, uid_t *owners,
+  gid_t *owngroups, uschar **errmsg)
+{
+return lf_check_file(fileno((FILE *)handle), filename, S_IFREG, modemask,
+  owners, owngroups, "lsearch", errmsg) == 0;
+}
+
+
+
+/*************************************************
+*  Internal function for the various lsearches   *
+*************************************************/
+
+/* See local README for interface description, plus:
+
+Extra argument:
+
+  type     one of the values LSEARCH_PLAIN, LSEARCH_WILD, LSEARCH_NWILD, or
+           LSEARCH_IP
+
+There is some messy logic in here to cope with very long data lines that do not
+fit into the fixed sized buffer. Most of the time this will never be exercised,
+but people do occasionally do weird things. */
+
+static int
+internal_lsearch_find(void *handle, uschar *filename, uschar *keystring,
+  int length, uschar **result, uschar **errmsg, int type)
+{
+FILE *f = (FILE *)handle;
+BOOL last_was_eol = TRUE;
+BOOL this_is_eol = TRUE;
+int old_pool = store_pool;
+void *reset_point = NULL;
+uschar buffer[4096];
+
+/* Wildcard searches may use up some store, because of expansions. We don't
+want them to fill up our search store. What we do is set the pool to the main
+pool and get a point to reset to later. Wildcard searches could also issue
+lookups, but internal_search_find will take care of that, and the cache will be
+safely stored in the search pool again. */
+
+if(type == LSEARCH_WILD || type == LSEARCH_NWILD)
+  {
+  store_pool = POOL_MAIN;
+  reset_point = store_get(0);
+  }
+
+filename = filename;  /* Keep picky compilers happy */
+errmsg = errmsg;
+
+rewind(f);
+for (last_was_eol = TRUE;
+     Ufgets(buffer, sizeof(buffer), f) != NULL;
+     last_was_eol = this_is_eol)
+  {
+  int ptr, size;
+  int p = Ustrlen(buffer);
+  int linekeylength;
+  BOOL this_is_comment;
+  uschar *yield;
+  uschar *s = buffer;
+
+  /* Check whether this the final segment of a line. If it follows an
+  incomplete part-line, skip it. */
+
+  this_is_eol = p > 0 && buffer[p-1] == '\n';
+  if (!last_was_eol) continue;
+
+  /* We now have the start of a physical line. If this is a final line segment,
+  remove trailing white space. */
+
+  if (this_is_eol)
+    {
+    while (p > 0 && isspace((uschar)buffer[p-1])) p--;
+    buffer[p] = 0;
+    }
+
+  /* If the buffer is empty it might be (a) a complete empty line, or (b) the
+  start of a line that begins with so much white space that it doesn't all fit
+  in the buffer. In both cases we want to skip the entire physical line.
+
+  If the buffer begins with # it is a comment line; if it begins with white
+  space it is a logical continuation; again, we want to skip the entire
+  physical line. */
+
+  if (buffer[0] == 0 || buffer[0] == '#' || isspace(buffer[0])) continue;
+
+  /* We assume that they key will fit in the buffer. If the key starts with ",
+  read it as a quoted string. We don't use string_dequote() because that uses
+  new store for the result, and we may be doing this many times in a long file.
+  We know that the dequoted string must be shorter than the original, because
+  we are removing the quotes, and also any escape sequences always turn two or
+  more characters into one character. Therefore, we can store the new string in
+  the same buffer. */
+
+  if (*s == '\"')
+    {
+    uschar *t = s++;
+    while (*s != 0 && *s != '\"')
+      {
+      if (*s == '\\') *t++ = string_interpret_escape(&s);
+        else *t++ = *s;
+      s++;
+      }
+    if (*s != 0) s++;               /* Past terminating " */
+    linekeylength = t - buffer;
+    }
+
+  /* Otherwise it is terminated by a colon or white space */
+
+  else
+    {
+    while (*s != 0 && *s != ':' && !isspace(*s)) s++;
+    linekeylength = s - buffer;
+    }
+
+  /* The matching test depends on which kind of lsearch we are doing */
+
+  switch(type)
+    {
+    /* A plain lsearch treats each key as a literal */
+
+    case LSEARCH_PLAIN:
+    if (linekeylength != length || strncmpic(buffer, keystring, length) != 0)
+      continue;
+    break;      /* Key matched */
+
+    /* A wild lsearch treats each key as a possible wildcarded string; no
+    expansion is done for nwildlsearch. */
+
+    case LSEARCH_WILD:
+    case LSEARCH_NWILD:
+      {
+      int rc;
+      int save = buffer[linekeylength];
+      uschar *list = buffer;
+      buffer[linekeylength] = 0;
+      rc = match_isinlist(keystring,
+        &list,
+        UCHAR_MAX+1,              /* Single-item list */
+        NULL,                     /* No anchor */
+        NULL,                     /* No caching */
+        MCL_STRING + ((type == LSEARCH_WILD)? 0:MCL_NOEXPAND),
+        TRUE,                     /* Caseless */
+        NULL);
+      buffer[linekeylength] = save;
+      if (rc == FAIL) continue;
+      if (rc == DEFER) return DEFER;
+      }
+
+    /* The key has matched. If the search involved a regular expression, it
+    might have caused numerical variables to be set. However, their values will
+    be in the wrong storage pool for external use. Copying them to the standard
+    pool is not feasible because of the caching of lookup results - a repeated
+    lookup will not match the regular expression again. Therefore, we flatten
+    all numeric variables at this point. */
+
+    expand_nmax = -1;
+    break;
+
+    /* Compare an ip address against a list of network/ip addresses. We have to
+    allow for the "*" case specially. */
+
+    case LSEARCH_IP:
+    if (linekeylength == 1 && buffer[0] == '*')
+      {
+      if (length != 1 || keystring[0] != '*') continue;
+      }
+    else if (length == 1 && keystring[0] == '*') continue;
+    else
+      {
+      int maskoffset;
+      int save = buffer[linekeylength];
+      buffer[linekeylength] = 0;
+      if (string_is_ip_address(buffer, &maskoffset) == 0 ||
+          !host_is_in_net(keystring, buffer, maskoffset)) continue;
+      buffer[linekeylength] = save;
+      }
+    break;      /* Key matched */
+    }
+
+  /* The key has matched. Skip spaces after the key, and allow an optional
+  colon after the spaces. This is an odd specification, but it's for
+  compatibility. */
+
+  while (isspace((uschar)*s)) s++;
+  if (*s == ':')
+    {
+    s++;
+    while (isspace((uschar)*s)) s++;
+    }
+
+  /* Reset dynamic store, if we need to, and revert to the search pool */
+
+  if (reset_point != NULL)
+    {
+    store_reset(reset_point);
+    store_pool = old_pool;
+    }
+
+  /* Now we want to build the result string to contain the data. There can be
+  two kinds of continuation: (a) the physical line may not all have fitted into
+  the buffer, and (b) there may be logical continuation lines, for which we
+  must convert all leading white space into a single blank.
+
+  Initialize, and copy the first segment of data. */
+
+  this_is_comment = FALSE;
+  size = 100;
+  ptr = 0;
+  yield = store_get(size);
+  if (*s != 0)
+    yield = string_cat(yield, &size, &ptr, s, Ustrlen(s));
+
+  /* Now handle continuations */
+
+  for (last_was_eol = this_is_eol;
+       Ufgets(buffer, sizeof(buffer), f) != NULL;
+       last_was_eol = this_is_eol)
+    {
+    s = buffer;
+    p = Ustrlen(buffer);
+    this_is_eol = p > 0 && buffer[p-1] == '\n';
+
+    /* Remove trailing white space from a physical line end */
+
+    if (this_is_eol)
+      {
+      while (p > 0 && isspace((uschar)buffer[p-1])) p--;
+      buffer[p] = 0;
+      }
+
+    /* If this is not a physical line continuation, skip it entirely if it's
+    empty or starts with #. Otherwise, break the loop if it doesn't start with
+    white space. Otherwise, replace leading white space with a single blank. */
+
+    if (last_was_eol)
+      {
+      this_is_comment = (this_is_comment || (buffer[0] == 0 || buffer[0] == '#'));
+      if (this_is_comment) continue;
+      if (!isspace((uschar)buffer[0])) break;
+      while (isspace((uschar)*s)) s++;
+      *(--s) = ' ';
+      }
+    if (this_is_comment) continue;
+
+    /* Join a physical or logical line continuation onto the result string. */
+
+    yield = string_cat(yield, &size, &ptr, s, Ustrlen(s));
+    }
+
+  yield[ptr] = 0;
+  store_reset(yield + ptr + 1);
+  *result = yield;
+  return OK;
+  }
+
+/* Reset dynamic store, if we need to */
+
+if (reset_point != NULL)
+  {
+  store_reset(reset_point);
+  store_pool = old_pool;
+  }
+
+return FAIL;
+}
+
+
+/*************************************************
+*         Find entry point for lsearch           *
+*************************************************/
+
+/* See local README for interface description */
+
+static int
+lsearch_find(void *handle, uschar *filename, uschar *keystring, int length,
+  uschar **result, uschar **errmsg, BOOL *do_cache)
+{
+do_cache = do_cache;  /* Keep picky compilers happy */
+return internal_lsearch_find(handle, filename, keystring, length, result,
+  errmsg, LSEARCH_PLAIN);
+}
+
+
+
+/*************************************************
+*      Find entry point for wildlsearch          *
+*************************************************/
+
+/* See local README for interface description */
+
+static int
+wildlsearch_find(void *handle, uschar *filename, uschar *keystring, int length,
+  uschar **result, uschar **errmsg, BOOL *do_cache)
+{
+do_cache = do_cache;  /* Keep picky compilers happy */
+return internal_lsearch_find(handle, filename, keystring, length, result,
+  errmsg, LSEARCH_WILD);
+}
+
+
+
+/*************************************************
+*      Find entry point for nwildlsearch         *
+*************************************************/
+
+/* See local README for interface description */
+
+static int
+nwildlsearch_find(void *handle, uschar *filename, uschar *keystring, int length,
+  uschar **result, uschar **errmsg, BOOL *do_cache)
+{
+do_cache = do_cache;  /* Keep picky compilers happy */
+return internal_lsearch_find(handle, filename, keystring, length, result,
+  errmsg, LSEARCH_NWILD);
+}
+
+
+
+
+/*************************************************
+*      Find entry point for iplsearch            *
+*************************************************/
+
+/* See local README for interface description */
+
+static int
+iplsearch_find(void *handle, uschar *filename, uschar *keystring, int length,
+  uschar **result, uschar **errmsg, BOOL *do_cache)
+{
+do_cache = do_cache;  /* Keep picky compilers happy */
+if ((length == 1 && keystring[0] == '*') ||
+    string_is_ip_address(keystring, NULL) != 0)
+  {
+  return internal_lsearch_find(handle, filename, keystring, length, result,
+    errmsg, LSEARCH_IP);
+  }
+else
+  {
+  *errmsg = string_sprintf("\"%s\" is not a valid iplsearch key (an IP "
+    "address, with optional CIDR mask, is wanted): "
+    "in a host list, use net-iplsearch as the search type", keystring);
+  return DEFER;
+  }
+}
+
+
+
+
+/*************************************************
+*              Close entry point                 *
+*************************************************/
+
+/* See local README for interface description */
+
+static void
+lsearch_close(void *handle)
+{
+(void)fclose((FILE *)handle);
+}
+
+
+
+/*************************************************
+*         Version reporting entry point          *
+*************************************************/
+
+/* See local README for interface description. */
+
+#include "../version.h"
+
+void
+lsearch_version_report(FILE *f)
+{
+#ifdef DYNLOOKUP
+fprintf(f, "Library version: lsearch: Exim version %s\n", EXIM_VERSION_STR);
+#endif
+}
+
+
+static lookup_info iplsearch_lookup_info = {
+  US"iplsearch",                 /* lookup name */
+  lookup_absfile,                /* uses absolute file name */
+  lsearch_open,                  /* open function */
+  lsearch_check,                 /* check function */
+  iplsearch_find,                /* find function */
+  lsearch_close,                 /* close function */
+  NULL,                          /* no tidy function */
+  NULL,                          /* no quoting function */
+  NULL                           /* no version reporting (redundant) */
+};
+
+static lookup_info lsearch_lookup_info = {
+  US"lsearch",                   /* lookup name */
+  lookup_absfile,                /* uses absolute file name */
+  lsearch_open,                  /* open function */
+  lsearch_check,                 /* check function */
+  lsearch_find,                  /* find function */
+  lsearch_close,                 /* close function */
+  NULL,                          /* no tidy function */
+  NULL,                          /* no quoting function */
+  lsearch_version_report         /* version reporting */
+};
+
+static lookup_info nwildlsearch_lookup_info = {
+  US"nwildlsearch",              /* lookup name */
+  lookup_absfile,                /* uses absolute file name */
+  lsearch_open,                  /* open function */
+  lsearch_check,                 /* check function */
+  nwildlsearch_find,             /* find function */
+  lsearch_close,                 /* close function */
+  NULL,                          /* no tidy function */
+  NULL,                          /* no quoting function */
+  NULL                           /* no version reporting (redundant) */
+};
+
+static lookup_info wildlsearch_lookup_info = {
+  US"wildlsearch",               /* lookup name */
+  lookup_absfile,                /* uses absolute file name */
+  lsearch_open,                  /* open function */
+  lsearch_check,                 /* check function */
+  wildlsearch_find,              /* find function */
+  lsearch_close,                 /* close function */
+  NULL,                          /* no tidy function */
+  NULL,                          /* no quoting function */
+  NULL                           /* no version reporting (redundant) */
+};
+
+#ifdef DYNLOOKUP
+#define lsearch_lookup_module_info _lookup_module_info
+#endif
+
+static lookup_info *_lookup_list[] = { &iplsearch_lookup_info,
+                                       &lsearch_lookup_info,
+                                       &nwildlsearch_lookup_info,
+                                       &wildlsearch_lookup_info };
+lookup_module_info lsearch_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 4 };
+
+/* End of lookups/lsearch.c */
diff --git a/src/lookups/mysql.c b/src/lookups/mysql.c
new file mode 100644 (file)
index 0000000..9511d2a
--- /dev/null
@@ -0,0 +1,456 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Thanks to Paul Kelly for contributing the original code for these
+functions. */
+
+
+#include "../exim.h"
+#include "lf_functions.h"
+
+#include <mysql.h>       /* The system header */
+
+
+/* Structure and anchor for caching connections. */
+
+typedef struct mysql_connection {
+  struct mysql_connection *next;
+  uschar  *server;
+  MYSQL *handle;
+} mysql_connection;
+
+static mysql_connection *mysql_connections = NULL;
+
+
+
+/*************************************************
+*              Open entry point                  *
+*************************************************/
+
+/* See local README for interface description. */
+
+static void *
+mysql_open(uschar *filename, uschar **errmsg)
+{
+return (void *)(1);    /* Just return something non-null */
+}
+
+
+
+/*************************************************
+*               Tidy entry point                 *
+*************************************************/
+
+/* See local README for interface description. */
+
+static void
+mysql_tidy(void)
+{
+mysql_connection *cn;
+while ((cn = mysql_connections) != NULL)
+  {
+  mysql_connections = cn->next;
+  DEBUG(D_lookup) debug_printf("close MYSQL connection: %s\n", cn->server);
+  mysql_close(cn->handle);
+  }
+}
+
+
+
+/*************************************************
+*        Internal search function                *
+*************************************************/
+
+/* This function is called from the find entry point to do the search for a
+single server.
+
+Arguments:
+  query        the query string
+  server       the server string
+  resultptr    where to store the result
+  errmsg       where to point an error message
+  defer_break  TRUE if no more servers are to be tried after DEFER
+  do_cache     set false if data is changed
+
+The server string is of the form "host/dbname/user/password". The host can be
+host:port. This string is in a nextinlist temporary buffer, so can be
+overwritten.
+
+Returns:       OK, FAIL, or DEFER
+*/
+
+static int
+perform_mysql_search(uschar *query, uschar *server, uschar **resultptr,
+  uschar **errmsg, BOOL *defer_break, BOOL *do_cache)
+{
+MYSQL *mysql_handle = NULL;        /* Keep compilers happy */
+MYSQL_RES *mysql_result = NULL;
+MYSQL_ROW mysql_row_data;
+MYSQL_FIELD *fields;
+
+int i;
+int ssize = 0;
+int offset = 0;
+int yield = DEFER;
+unsigned int num_fields;
+uschar *result = NULL;
+mysql_connection *cn;
+uschar *server_copy = NULL;
+uschar *sdata[4];
+
+/* Disaggregate the parameters from the server argument. The order is host,
+database, user, password. We can write to the string, since it is in a
+nextinlist temporary buffer. The copy of the string that is used for caching
+has the password removed. This copy is also used for debugging output. */
+
+for (i = 3; i > 0; i--)
+  {
+  uschar *pp = Ustrrchr(server, '/');
+  if (pp == NULL)
+    {
+    *errmsg = string_sprintf("incomplete MySQL server data: %s",
+      (i == 3)? server : server_copy);
+    *defer_break = TRUE;
+    return DEFER;
+    }
+  *pp++ = 0;
+  sdata[i] = pp;
+  if (i == 3) server_copy = string_copy(server);  /* sans password */
+  }
+sdata[0] = server;   /* What's left at the start */
+
+/* See if we have a cached connection to the server */
+
+for (cn = mysql_connections; cn != NULL; cn = cn->next)
+  {
+  if (Ustrcmp(cn->server, server_copy) == 0)
+    {
+    mysql_handle = cn->handle;
+    break;
+    }
+  }
+
+/* If no cached connection, we must set one up. Mysql allows for a host name
+and port to be specified. It also allows the name of a Unix socket to be used.
+Unfortunately, this contains slashes, but its use is expected to be rare, so
+the rather cumbersome syntax shouldn't inconvenience too many people. We use
+this:  host:port(socket)  where all the parts are optional. */
+
+if (cn == NULL)
+  {
+  uschar *p;
+  uschar *socket = NULL;
+  int port = 0;
+
+  if ((p = Ustrchr(sdata[0], '(')) != NULL)
+    {
+    *p++ = 0;
+    socket = p;
+    while (*p != 0 && *p != ')') p++;
+    *p = 0;
+    }
+
+  if ((p = Ustrchr(sdata[0], ':')) != NULL)
+    {
+    *p++ = 0;
+    port = Uatoi(p);
+    }
+
+  if (Ustrchr(sdata[0], '/') != NULL)
+    {
+    *errmsg = string_sprintf("unexpected slash in MySQL server hostname: %s",
+      sdata[0]);
+    *defer_break = TRUE;
+    return DEFER;
+    }
+
+  /* If the database is the empty string, set it NULL - the query must then
+  define it. */
+
+  if (sdata[1][0] == 0) sdata[1] = NULL;
+
+  DEBUG(D_lookup)
+    debug_printf("MYSQL new connection: host=%s port=%d socket=%s "
+      "database=%s user=%s\n", sdata[0], port, socket, sdata[1], sdata[2]);
+
+  /* Get store for a new handle, initialize it, and connect to the server */
+
+  mysql_handle = store_get(sizeof(MYSQL));
+  mysql_init(mysql_handle);
+  if (mysql_real_connect(mysql_handle,
+      /*  host        user         passwd     database */
+      CS sdata[0], CS sdata[2], CS sdata[3], CS sdata[1],
+      port, CS socket, CLIENT_MULTI_RESULTS) == NULL)
+    {
+    *errmsg = string_sprintf("MYSQL connection failed: %s",
+      mysql_error(mysql_handle));
+    *defer_break = FALSE;
+    goto MYSQL_EXIT;
+    }
+
+  /* Add the connection to the cache */
+
+  cn = store_get(sizeof(mysql_connection));
+  cn->server = server_copy;
+  cn->handle = mysql_handle;
+  cn->next = mysql_connections;
+  mysql_connections = cn;
+  }
+
+/* Else use a previously cached connection */
+
+else
+  {
+  DEBUG(D_lookup)
+    debug_printf("MYSQL using cached connection for %s\n", server_copy);
+  }
+
+/* Run the query */
+
+if (mysql_query(mysql_handle, CS query) != 0)
+  {
+  *errmsg = string_sprintf("MYSQL: query failed: %s\n",
+    mysql_error(mysql_handle));
+  *defer_break = FALSE;
+  goto MYSQL_EXIT;
+  }
+
+/* Pick up the result. If the query was not of the type that returns data,
+namely INSERT, UPDATE, or DELETE, an error occurs here. However, this situation
+can be detected by calling mysql_field_count(). If its result is zero, no data
+was expected (this is all explained clearly in the MySQL manual). In this case,
+we return the number of rows affected by the command. In this event, we do NOT
+want to cache the result; also the whole cache for the handle must be cleaned
+up. Setting do_cache FALSE requests this. */
+
+if ((mysql_result = mysql_use_result(mysql_handle)) == NULL)
+  {
+  if ( mysql_field_count(mysql_handle) == 0 )
+    {
+    DEBUG(D_lookup) debug_printf("MYSQL: query was not one that returns data\n");
+    result = string_sprintf("%d", mysql_affected_rows(mysql_handle));
+    *do_cache = FALSE;
+    goto MYSQL_EXIT;
+    }
+  *errmsg = string_sprintf("MYSQL: lookup result failed: %s\n",
+    mysql_error(mysql_handle));
+  *defer_break = FALSE;
+  goto MYSQL_EXIT;
+  }
+
+/* Find the number of fields returned. If this is one, we don't add field
+names to the data. Otherwise we do. */
+
+num_fields = mysql_num_fields(mysql_result);
+
+/* Get the fields and construct the result string. If there is more than one
+row, we insert '\n' between them. */
+
+fields = mysql_fetch_fields(mysql_result);
+
+while ((mysql_row_data = mysql_fetch_row(mysql_result)) != NULL)
+  {
+  unsigned long *lengths = mysql_fetch_lengths(mysql_result);
+
+  if (result != NULL)
+      result = string_cat(result, &ssize, &offset, US"\n", 1);
+
+  if (num_fields == 1)
+    {
+    if (mysql_row_data[0] != NULL)    /* NULL value yields nothing */
+      result = string_cat(result, &ssize, &offset, US mysql_row_data[0],
+        lengths[0]);
+    }
+
+  else for (i = 0; i < num_fields; i++)
+    {
+    result = lf_quote(US fields[i].name, US mysql_row_data[i], lengths[i],
+      result, &ssize, &offset);
+    }
+  }
+
+/* more results? -1 = no, >0 = error, 0 = yes (keep looping)
+   This is needed because of the CLIENT_MULTI_RESULTS on mysql_real_connect(),
+   we don't expect any more results. */
+
+while((i = mysql_next_result(mysql_handle)) >= 0) {
+   if(i == 0) {   /* Just ignore more results */
+     DEBUG(D_lookup) debug_printf("MYSQL: got unexpected more results\n");
+     continue;
+   }
+
+   *errmsg = string_sprintf("MYSQL: lookup result error when checking for more results: %s\n",
+       mysql_error(mysql_handle));
+   goto MYSQL_EXIT;
+}
+
+/* If result is NULL then no data has been found and so we return FAIL.
+Otherwise, we must terminate the string which has been built; string_cat()
+always leaves enough room for a terminating zero. */
+
+if (result == NULL)
+  {
+  yield = FAIL;
+  *errmsg = US"MYSQL: no data found";
+  }
+else
+  {
+  result[offset] = 0;
+  store_reset(result + offset + 1);
+  }
+
+/* Get here by goto from various error checks and from the case where no data
+was read (e.g. an update query). */
+
+MYSQL_EXIT:
+
+/* Free mysal store for any result that was got; don't close the connection, as
+it is cached. */
+
+if (mysql_result != NULL) mysql_free_result(mysql_result);
+
+/* Non-NULL result indicates a sucessful result */
+
+if (result != NULL)
+  {
+  *resultptr = result;
+  return OK;
+  }
+else
+  {
+  DEBUG(D_lookup) debug_printf("%s\n", *errmsg);
+  return yield;      /* FAIL or DEFER */
+  }
+}
+
+
+
+
+/*************************************************
+*               Find entry point                 *
+*************************************************/
+
+/* See local README for interface description. The handle and filename
+arguments are not used. The code to loop through a list of servers while the
+query is deferred with a retryable error is now in a separate function that is
+shared with other SQL lookups. */
+
+static int
+mysql_find(void *handle, uschar *filename, uschar *query, int length,
+  uschar **result, uschar **errmsg, BOOL *do_cache)
+{
+return lf_sqlperform(US"MySQL", US"mysql_servers", mysql_servers, query,
+  result, errmsg, do_cache, perform_mysql_search);
+}
+
+
+
+/*************************************************
+*               Quote entry point                *
+*************************************************/
+
+/* The only characters that need to be quoted (with backslash) are newline,
+tab, carriage return, backspace, backslash itself, and the quote characters.
+Percent, and underscore and not escaped. They are only special in contexts
+where they can be wild cards, and this isn't usually the case for data inserted
+from messages, since that isn't likely to be treated as a pattern of any kind.
+Sadly, MySQL doesn't seem to behave like other programs. If you use something
+like "where id="ab\%cd" it does not treat the string as "ab%cd". So you really
+can't quote "on spec".
+
+Arguments:
+  s          the string to be quoted
+  opt        additional option text or NULL if none
+
+Returns:     the processed string or NULL for a bad option
+*/
+
+static uschar *
+mysql_quote(uschar *s, uschar *opt)
+{
+register int c;
+int count = 0;
+uschar *t = s;
+uschar *quoted;
+
+if (opt != NULL) return NULL;     /* No options recognized */
+
+while ((c = *t++) != 0)
+  if (Ustrchr("\n\t\r\b\'\"\\", c) != NULL) count++;
+
+if (count == 0) return s;
+t = quoted = store_get(Ustrlen(s) + count + 1);
+
+while ((c = *s++) != 0)
+  {
+  if (Ustrchr("\n\t\r\b\'\"\\", c) != NULL)
+    {
+    *t++ = '\\';
+    switch(c)
+      {
+      case '\n': *t++ = 'n';
+      break;
+      case '\t': *t++ = 't';
+      break;
+      case '\r': *t++ = 'r';
+      break;
+      case '\b': *t++ = 'b';
+      break;
+      default:   *t++ = c;
+      break;
+      }
+    }
+  else *t++ = c;
+  }
+
+*t = 0;
+return quoted;
+}
+
+
+/*************************************************
+*         Version reporting entry point          *
+*************************************************/
+
+/* See local README for interface description. */
+
+#include "../version.h"
+
+void
+mysql_version_report(FILE *f)
+{
+fprintf(f, "Library version: MySQL: Compile: %s [%s]\n"
+           "                        Runtime: %s\n",
+        MYSQL_SERVER_VERSION, MYSQL_COMPILATION_COMMENT,
+        mysql_get_client_info());
+#ifdef DYNLOOKUP
+fprintf(f, "                        Exim version %s\n", EXIM_VERSION_STR);
+#endif
+}
+
+/* These are the lookup_info blocks for this driver */
+
+static lookup_info mysql_lookup_info = {
+  US"mysql",                     /* lookup name */
+  lookup_querystyle,             /* query-style lookup */
+  mysql_open,                    /* open function */
+  NULL,                          /* no check function */
+  mysql_find,                    /* find function */
+  NULL,                          /* no close function */
+  mysql_tidy,                    /* tidy function */
+  mysql_quote,                   /* quoting function */
+  mysql_version_report           /* version reporting */
+};
+
+#ifdef DYNLOOKUP
+#define mysql_lookup_module_info _lookup_module_info
+#endif
+
+static lookup_info *_lookup_list[] = { &mysql_lookup_info };
+lookup_module_info mysql_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 1 };
+
+/* End of lookups/mysql.c */
diff --git a/src/lookups/nis.c b/src/lookups/nis.c
new file mode 100644 (file)
index 0000000..7b012b1
--- /dev/null
@@ -0,0 +1,137 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+#include "../exim.h"
+#include "lf_functions.h"
+
+#include <rpcsvc/ypclnt.h>
+
+
+/*************************************************
+*              Open entry point                  *
+*************************************************/
+
+/* See local README for interface description. This serves for both
+the "nis" and "nis0" lookup types. */
+
+static void *
+nis_open(uschar *filename, uschar **errmsg)
+{
+char *nis_domain;
+if (yp_get_default_domain(&nis_domain) != 0)
+  {
+  *errmsg = string_sprintf("failed to get default NIS domain");
+  return NULL;
+  }
+return nis_domain;
+}
+
+
+
+/*************************************************
+*           Find entry point for nis             *
+*************************************************/
+
+/* See local README for interface description. A separate function is used
+for nis0 because they are so short it isn't worth trying to use any common
+code. */
+
+static int
+nis_find(void *handle, uschar *filename, uschar *keystring, int length,
+  uschar **result, uschar **errmsg, BOOL *do_cache)
+{
+int rc;
+uschar *nis_data;
+int nis_data_length;
+do_cache = do_cache;   /* Placate picky compilers */
+if ((rc = yp_match(CS handle, CS filename, CS keystring, length,
+    CSS &nis_data, &nis_data_length)) == 0)
+  {
+  *result = string_copy(nis_data);
+  (*result)[nis_data_length] = 0;    /* remove final '\n' */
+  return OK;
+  }
+return (rc == YPERR_KEY || rc == YPERR_MAP)? FAIL : DEFER;
+}
+
+
+
+/*************************************************
+*           Find entry point for nis0            *
+*************************************************/
+
+/* See local README for interface description. */
+
+static int
+nis0_find(void *handle, uschar *filename, uschar *keystring, int length,
+  uschar **result, uschar **errmsg, BOOL *do_cache)
+{
+int rc;
+uschar *nis_data;
+int nis_data_length;
+do_cache = do_cache;   /* Placate picky compilers */
+if ((rc = yp_match(CS handle, CS filename, CS keystring, length + 1,
+    CSS &nis_data, &nis_data_length)) == 0)
+  {
+  *result = string_copy(nis_data);
+  (*result)[nis_data_length] = 0;    /* remove final '\n' */
+  return OK;
+  }
+return (rc == YPERR_KEY || rc == YPERR_MAP)? FAIL : DEFER;
+}
+
+
+
+/*************************************************
+*         Version reporting entry point          *
+*************************************************/
+
+/* See local README for interface description. */
+
+#include "../version.h"
+
+void
+nis_version_report(FILE *f)
+{
+#ifdef DYNLOOKUP
+fprintf(f, "Library version: NIS: Exim version %s\n", EXIM_VERSION_STR);
+#endif
+}
+
+
+static lookup_info nis_lookup_info = {
+  US"nis",                       /* lookup name */
+  0,                             /* not abs file, not query style*/
+  nis_open,                      /* open function */
+  NULL,                          /* check function */
+  nis_find,                      /* find function */
+  NULL,                          /* no close function */
+  NULL,                          /* no tidy function */
+  NULL,                          /* no quoting function */
+  nis_version_report             /* version reporting */
+};
+
+static lookup_info nis0_lookup_info = {
+  US"nis0",                      /* lookup name */
+  0,                             /* not absfile, not query style */
+  nis_open,    /* sic */         /* open function */
+  NULL,                          /* check function */
+  nis0_find,                     /* find function */
+  NULL,                          /* no close function */
+  NULL,                          /* no tidy function */
+  NULL,                          /* no quoting function */
+  NULL                           /* no version reporting (redundant) */
+};
+
+#ifdef DYNLOOKUP
+#define nis_lookup_module_info _lookup_module_info
+#endif
+
+static lookup_info *_lookup_list[] = { &nis_lookup_info, &nis0_lookup_info };
+lookup_module_info nis_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 2 };
+
+/* End of lookups/nis.c */
diff --git a/src/lookups/nisplus.c b/src/lookups/nisplus.c
new file mode 100644 (file)
index 0000000..8895cee
--- /dev/null
@@ -0,0 +1,297 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+#include "../exim.h"
+#include "lf_functions.h"
+
+#include <rpcsvc/nis.h>
+
+
+/*************************************************
+*              Open entry point                  *
+*************************************************/
+
+/* See local README for interface description. */
+
+static void *
+nisplus_open(uschar *filename, uschar **errmsg)
+{
+return (void *)(1);    /* Just return something non-null */
+}
+
+
+
+/*************************************************
+*               Find entry point                 *
+*************************************************/
+
+/* See local README for interface description. The format of queries for a
+NIS+ search is
+
+  [field=value,...],table-name
+or
+  [field=value,...],table-name:result-field-name
+
+in other words, a normal NIS+ "indexed name", with an optional result field
+name tagged on the end after a colon. If there is no result-field name, the
+yield is the concatenation of all the fields, preceded by their names and an
+equals sign. */
+
+static int
+nisplus_find(void *handle, uschar *filename, uschar *query, int length,
+  uschar **result, uschar **errmsg, BOOL *do_cache)
+{
+int i;
+int ssize = 0;
+int offset = 0;
+int error_error = FAIL;
+uschar *field_name = NULL;
+nis_result *nrt = NULL;
+nis_result *nre = NULL;
+nis_object *tno, *eno;
+struct entry_obj *eo;
+struct table_obj *ta;
+uschar *p = query + length;
+uschar *yield = NULL;
+
+do_cache = do_cache;   /* Placate picky compilers */
+
+/* Search backwards for a colon to see if a result field name
+has been given. */
+
+while (p > query && p[-1] != ':') p--;
+
+if (p > query)
+  {
+  field_name = p;
+  p[-1] = 0;
+  }
+else p = query + length;
+
+/* Now search backwards to find the comma that starts the
+table name. */
+
+while (p > query && p[-1] != ',') p--;
+if (p <= query)
+  {
+  *errmsg = US"NIS+ query malformed";
+  error_error = DEFER;
+  goto NISPLUS_EXIT;
+  }
+
+/* Look up the data for the table, in order to get the field names,
+check that we got back a table, and set up pointers so the field
+names can be scanned. */
+
+nrt = nis_lookup(CS p, EXPAND_NAME | NO_CACHE);
+if (nrt->status != NIS_SUCCESS)
+  {
+  *errmsg = string_sprintf("NIS+ error accessing %s table: %s", p,
+    nis_sperrno(nrt->status));
+  if (nrt->status != NIS_NOTFOUND && nrt->status != NIS_NOSUCHTABLE)
+    error_error = DEFER;
+  goto NISPLUS_EXIT;
+  }
+tno = nrt->objects.objects_val;
+if (tno->zo_data.zo_type != TABLE_OBJ)
+  {
+  *errmsg = string_sprintf("NIS+ error: %s is not a table", p);
+  goto NISPLUS_EXIT;
+  }
+ta = &(tno->zo_data.objdata_u.ta_data);
+
+/* Now look up the entry in the table, check that we got precisely one
+object and that it is a table entry. */
+
+nre = nis_list(CS query, EXPAND_NAME, NULL, NULL);
+if (nre->status != NIS_SUCCESS)
+  {
+  *errmsg = string_sprintf("NIS+ error accessing entry %s: %s",
+    query, nis_sperrno(nre->status));
+  goto NISPLUS_EXIT;
+  }
+if (nre->objects.objects_len > 1)
+  {
+  *errmsg = string_sprintf("NIS+ returned more than one object for %s",
+    query);
+  goto NISPLUS_EXIT;
+  }
+else if (nre->objects.objects_len < 1)
+  {
+  *errmsg = string_sprintf("NIS+ returned no data for %s", query);
+  goto NISPLUS_EXIT;
+  }
+eno = nre->objects.objects_val;
+if (eno->zo_data.zo_type != ENTRY_OBJ)
+  {
+  *errmsg = string_sprintf("NIS+ error: %s is not an entry", query);
+  goto NISPLUS_EXIT;
+  }
+
+/* Scan the columns in the entry and in the table. If a result field
+was given, look for that field; otherwise concatenate all the fields
+with their names. */
+
+eo = &(eno->zo_data.objdata_u.en_data);
+for (i = 0; i < eo->en_cols.en_cols_len; i++)
+  {
+  table_col *tc = ta->ta_cols.ta_cols_val + i;
+  entry_col *ec = eo->en_cols.en_cols_val + i;
+  int len = ec->ec_value.ec_value_len;
+  uschar *value = US ec->ec_value.ec_value_val;
+
+  /* The value may be NULL for a zero-length field. Turn this into an
+  empty string for consistency. Remove trailing whitespace and zero
+  bytes. */
+
+  if (value == NULL) value = US""; else
+    while (len > 0 && (value[len-1] == 0 || isspace(value[len-1])))
+      len--;
+
+  /* Concatenate all fields if no specific one selected */
+
+  if (field_name == NULL)
+    {
+    yield = string_cat(yield, &ssize, &offset,US  tc->tc_name,
+      Ustrlen(tc->tc_name));
+    yield = string_cat(yield, &ssize, &offset, US"=", 1);
+
+    /* Quote the value if it contains spaces or is empty */
+
+    if (value[0] == 0 || Ustrchr(value, ' ') != NULL)
+      {
+      int j;
+      yield = string_cat(yield, &ssize, &offset, US"\"", 1);
+      for (j = 0; j < len; j++)
+        {
+        if (value[j] == '\"' || value[j] == '\\')
+          yield = string_cat(yield, &ssize, &offset, US"\\", 1);
+        yield = string_cat(yield, &ssize, &offset, value+j, 1);
+        }
+      yield = string_cat(yield, &ssize, &offset, US"\"", 1);
+      }
+    else yield = string_cat(yield, &ssize, &offset, value, len);
+
+    yield = string_cat(yield, &ssize, &offset, US" ", 1);
+    }
+
+  /* When the specified field is found, grab its data and finish */
+
+  else if (Ustrcmp(field_name, tc->tc_name) == 0)
+    {
+    yield = string_copyn(value, len);
+    goto NISPLUS_EXIT;
+    }
+  }
+
+/* Error if a field name was specified and we didn't find it; if no
+field name, ensure the concatenated data is zero-terminated. */
+
+if (field_name != NULL)
+  *errmsg = string_sprintf("NIS+ field %s not found for %s", field_name,
+    query);
+else
+  {
+  yield[offset] = 0;
+  store_reset(yield + offset + 1);
+  }
+
+/* Restore the colon in the query, and free result store before
+finishing. */
+
+NISPLUS_EXIT:
+if (field_name != NULL) field_name[-1] = ':';
+if (nrt != NULL) nis_freeresult(nrt);
+if (nre != NULL) nis_freeresult(nre);
+
+if (yield != NULL)
+  {
+  *result = yield;
+  return OK;
+  }
+
+return error_error;      /* FAIL or DEFER */
+}
+
+
+
+/*************************************************
+*               Quote entry point                *
+*************************************************/
+
+/* The only quoting that is necessary for NIS+ is to double any doublequote
+characters. No options are recognized.
+
+Arguments:
+  s          the string to be quoted
+  opt        additional option text or NULL if none
+
+Returns:     the processed string or NULL for a bad option
+*/
+
+static uschar *
+nisplus_quote(uschar *s, uschar *opt)
+{
+int count = 0;
+uschar *quoted;
+uschar *t = s;
+
+if (opt != NULL) return NULL;    /* No options recognized */
+
+while (*t != 0) if (*t++ == '\"') count++;
+if (count == 0) return s;
+
+t = quoted = store_get(Ustrlen(s) + count + 1);
+
+while (*s != 0)
+  {
+  *t++ = *s;
+  if (*s++ == '\"') *t++ = '\"';
+  }
+
+*t = 0;
+return quoted;
+}
+
+
+/*************************************************
+*         Version reporting entry point          *
+*************************************************/
+
+/* See local README for interface description. */
+
+#include "../version.h"
+
+void
+nisplus_version_report(FILE *f)
+{
+#ifdef DYNLOOKUP
+fprintf(f, "Library version: NIS+: Exim version %s\n", EXIM_VERSION_STR);
+#endif
+}
+
+
+static lookup_info _lookup_info = {
+  US"nisplus",                   /* lookup name */
+  lookup_querystyle,             /* query-style lookup */
+  nisplus_open,                  /* open function */
+  NULL,                          /* check function */
+  nisplus_find,                  /* find function */
+  NULL,                          /* no close function */
+  NULL,                          /* no tidy function */
+  nisplus_quote,                 /* quoting function */
+  nisplus_version_report         /* version reporting */
+};
+
+#ifdef DYNLOOKUP
+#define nisplus_lookup_module_info _lookup_module_info
+#endif
+
+static lookup_info *_lookup_list[] = { &_lookup_info };
+lookup_module_info nisplus_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 1 };
+
+/* End of lookups/nisplus.c */
diff --git a/src/lookups/oracle.c b/src/lookups/oracle.c
new file mode 100644 (file)
index 0000000..1f2520a
--- /dev/null
@@ -0,0 +1,642 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Interface to an Oracle database. This code was originally supplied by
+Paul Kelly, but I have hacked it around for various reasons, and tried to add
+some comments from my position of Oracle ignorance. */
+
+
+#include "../exim.h"
+
+
+/* The Oracle system headers */
+
+#include <oratypes.h>
+#include <ocidfn.h>
+#include <ocikpr.h>
+
+#define PARSE_NO_DEFER           0     /* parse straight away */
+#define PARSE_V7_LNG             2
+#define MAX_ITEM_BUFFER_SIZE  1024     /* largest size of a cell of data */
+#define MAX_SELECT_LIST_SIZE    32     /* maximum number of columns (not rows!) */
+
+/* Paul's comment on this was "change this to 512 for 64bit cpu", but I don't
+understand why. The Oracle manual just asks for 256 bytes.
+
+That was years ago. Jin Choi suggested (March 2007) that this change should
+be made in the source, as at worst it wastes 256 bytes, and it saves people
+having to discover about this for themselves as more and more systems are
+64-bit. So I have changed 256 to 512. */
+
+#define HDA_SIZE 512
+
+/* Internal/external datatype codes */
+
+#define NUMBER_TYPE              2
+#define INT_TYPE                 3
+#define FLOAT_TYPE               4
+#define STRING_TYPE              5
+#define ROWID_TYPE              11
+#define DATE_TYPE               12
+
+/*  ORACLE error codes used in demonstration programs */
+
+#define VAR_NOT_IN_LIST       1007
+#define NO_DATA_FOUND         1403
+
+typedef struct Ora_Describe {
+  sb4   dbsize;
+  sb2   dbtype;
+  sb1   buf[MAX_ITEM_BUFFER_SIZE];
+  sb4   buflen;
+  sb4   dsize;
+  sb2   precision;
+  sb2   scale;
+  sb2   nullok;
+} Ora_Describe;
+
+typedef struct Ora_Define {
+  ub1   buf[MAX_ITEM_BUFFER_SIZE];
+  float flt_buf;
+  sword int_buf;
+  sb2   indp;
+  ub2   col_retlen, col_retcode;
+} Ora_Define;
+
+/* Structure and anchor for caching connections. */
+
+typedef struct oracle_connection {
+  struct oracle_connection *next;
+  uschar *server;
+  struct cda_def *handle;
+  void   *hda_mem;
+} oracle_connection;
+
+static oracle_connection *oracle_connections = NULL;
+
+
+
+
+
+/*************************************************
+*        Set up message after error              *
+*************************************************/
+
+/* Sets up a message from a local string plus whatever Oracle gives.
+
+Arguments:
+  oracle_handle   the handle of the connection
+  rc              the return code
+  msg             local text message
+*/
+
+static uschar *
+oracle_error(struct cda_def *oracle_handle, int rc, uschar *msg)
+{
+uschar tmp[1024];
+oerhms(oracle_handle, rc, tmp, sizeof(tmp));
+return string_sprintf("ORACLE %s: %s", msg, tmp);
+}
+
+
+
+/*************************************************
+*     Describe and define the select list items  *
+*************************************************/
+
+/* Figures out sizes, types, and numbers.
+
+Arguments:
+  cda        the connection
+  def
+  desc       descriptions put here
+
+Returns:     number of fields
+*/
+
+static sword
+describe_define(Cda_Def *cda, Ora_Define *def, Ora_Describe *desc)
+{
+sword col, deflen, deftyp;
+static ub1 *defptr;
+static sword numwidth = 8;
+
+/* Describe the select-list items. */
+
+for (col = 0; col < MAX_SELECT_LIST_SIZE; col++)
+  {
+  desc[col].buflen = MAX_ITEM_BUFFER_SIZE;
+
+  if (odescr(cda, col + 1, &desc[col].dbsize,
+             &desc[col].dbtype, &desc[col].buf[0],
+             &desc[col].buflen, &desc[col].dsize,
+             &desc[col].precision, &desc[col].scale,
+             &desc[col].nullok) != 0)
+    {
+    /* Break on end of select list. */
+    if (cda->rc == VAR_NOT_IN_LIST) break; else return -1;
+    }
+
+  /* Adjust sizes and types for display, handling NUMBER with scale as float. */
+
+  if (desc[col].dbtype == NUMBER_TYPE)
+    {
+    desc[col].dbsize = numwidth;
+    if (desc[col].scale != 0)
+      {
+      defptr = (ub1 *)&def[col].flt_buf;
+      deflen = (sword) sizeof(float);
+      deftyp = FLOAT_TYPE;
+      desc[col].dbtype = FLOAT_TYPE;
+      }
+    else
+      {
+      defptr = (ub1 *)&def[col].int_buf;
+      deflen = (sword) sizeof(sword);
+      deftyp = INT_TYPE;
+      desc[col].dbtype = INT_TYPE;
+      }
+    }
+  else
+    {
+    if (desc[col].dbtype == DATE_TYPE)
+        desc[col].dbsize = 9;
+    if (desc[col].dbtype == ROWID_TYPE)
+        desc[col].dbsize = 18;
+    defptr = def[col].buf;
+    deflen = desc[col].dbsize > MAX_ITEM_BUFFER_SIZE ?
+      MAX_ITEM_BUFFER_SIZE : desc[col].dbsize + 1;
+    deftyp = STRING_TYPE;
+    desc[col].dbtype = STRING_TYPE;
+    }
+
+  /* Define an output variable */
+
+  if (odefin(cda, col + 1,
+             defptr, deflen, deftyp,
+             -1, &def[col].indp, (text *) 0, -1, -1,
+             &def[col].col_retlen,
+             &def[col].col_retcode) != 0)
+    return -1;
+  }  /* Loop for each column */
+
+return col;
+}
+
+
+
+/*************************************************
+*              Open entry point                  *
+*************************************************/
+
+/* See local README for interface description. */
+
+static void *
+oracle_open(uschar *filename, uschar **errmsg)
+{
+return (void *)(1);    /* Just return something non-null */
+}
+
+
+
+/*************************************************
+*               Tidy entry point                 *
+*************************************************/
+
+/* See local README for interface description. */
+
+static void
+oracle_tidy(void)
+{
+oracle_connection *cn;
+while ((cn = oracle_connections) != NULL)
+  {
+  oracle_connections = cn->next;
+  DEBUG(D_lookup) debug_printf("close ORACLE connection: %s\n", cn->server);
+  ologof(cn->handle);
+  }
+}
+
+
+
+/*************************************************
+*        Internal search function                *
+*************************************************/
+
+/* This function is called from the find entry point to do the search for a
+single server.
+
+Arguments:
+  query        the query string
+  server       the server string
+  resultptr    where to store the result
+  errmsg       where to point an error message
+  defer_break  TRUE if no more servers are to be tried after DEFER
+
+The server string is of the form "host/dbname/user/password", for compatibility
+with MySQL and pgsql, but at present, the dbname is not used. This string is in
+a nextinlist temporary buffer, so can be overwritten.
+
+Returns:       OK, FAIL, or DEFER
+*/
+
+static int
+perform_oracle_search(uschar *query, uschar *server, uschar **resultptr,
+  uschar **errmsg, BOOL *defer_break)
+{
+Cda_Def *cda = NULL;
+struct cda_def *oracle_handle = NULL;
+Ora_Describe *desc = NULL;
+Ora_Define *def = NULL;
+void *hda = NULL;
+
+int i;
+int ssize = 0;
+int offset = 0;
+int yield = DEFER;
+unsigned int num_fields = 0;
+uschar *result = NULL;
+oracle_connection *cn = NULL;
+uschar *server_copy = NULL;
+uschar *sdata[4];
+uschar tmp[1024];
+
+/* Disaggregate the parameters from the server argument. The order is host,
+database, user, password. We can write to the string, since it is in a
+nextinlist temporary buffer. The copy of the string that is used for caching
+has the password removed. This copy is also used for debugging output. */
+
+for (i = 3; i > 0; i--)
+  {
+  uschar *pp = Ustrrchr(server, '/');
+  if (pp == NULL)
+    {
+    *errmsg = string_sprintf("incomplete ORACLE server data: %s", server);
+    *defer_break = TRUE;
+    return DEFER;
+    }
+  *pp++ = 0;
+  sdata[i] = pp;
+  if (i == 3) server_copy = string_copy(server);  /* sans password */
+  }
+sdata[0] = server;   /* What's left at the start */
+
+/* If the database is the empty string, set it NULL - the query must then
+define it. */
+
+if (sdata[1][0] == 0) sdata[1] = NULL;
+
+/* See if we have a cached connection to the server */
+
+for (cn = oracle_connections; cn != NULL; cn = cn->next)
+  {
+  if (strcmp(cn->server, server_copy) == 0)
+    {
+    oracle_handle = cn->handle;
+    hda = cn->hda_mem;
+    break;
+    }
+  }
+
+/* If no cached connection, we must set one up */
+
+if (cn == NULL)
+  {
+  DEBUG(D_lookup) debug_printf("ORACLE new connection: host=%s database=%s "
+    "user=%s\n", sdata[0], sdata[1], sdata[2]);
+
+  /* Get store for a new connection, initialize it, and connect to the server */
+
+   oracle_handle = store_get(sizeof(struct cda_def));
+   hda = store_get(HDA_SIZE);
+   memset(hda,'\0',HDA_SIZE);
+
+  /*
+   * Perform a default (blocking) login
+   *
+   * sdata[0] = tnsname (service name - typically host name)
+   * sdata[1] = dbname - not used at present
+   * sdata[2] = username
+   * sdata[3] = passwd
+   */
+
+  if(olog(oracle_handle, hda, sdata[2], -1, sdata[3], -1, sdata[0], -1,
+         (ub4)OCI_LM_DEF) != 0)
+    {
+    *errmsg = oracle_error(oracle_handle, oracle_handle->rc,
+      US"connection failed");
+    *defer_break = FALSE;
+    goto ORACLE_EXIT_NO_VALS;
+    }
+
+  /* Add the connection to the cache */
+
+  cn = store_get(sizeof(oracle_connection));
+  cn->server = server_copy;
+  cn->handle = oracle_handle;
+  cn->next = oracle_connections;
+  cn->hda_mem = hda;
+  oracle_connections = cn;
+  }
+
+/* Else use a previously cached connection - we can write to the server string
+to obliterate the password because it is in a nextinlist temporary buffer. */
+
+else
+  {
+  DEBUG(D_lookup)
+    debug_printf("ORACLE using cached connection for %s\n", server_copy);
+  }
+
+/* We have a connection. Open a cursor and run the query */
+
+cda = store_get(sizeof(Cda_Def));
+
+if (oopen(cda, oracle_handle, (text *)0, -1, -1, (text *)0, -1) != 0)
+  {
+  *errmsg = oracle_error(oracle_handle, cda->rc, "failed to open cursor");
+  *defer_break = FALSE;
+  goto ORACLE_EXIT_NO_VALS;
+  }
+
+if (oparse(cda, (text *)query, (sb4) -1,
+      (sword)PARSE_NO_DEFER, (ub4)PARSE_V7_LNG) != 0)
+  {
+  *errmsg = oracle_error(oracle_handle, cda->rc, "query failed");
+  *defer_break = FALSE;
+  oclose(cda);
+  goto ORACLE_EXIT_NO_VALS;
+  }
+
+/* Find the number of fields returned and sort out their types. If the number
+is one, we don't add field names to the data. Otherwise we do. */
+
+def = store_get(sizeof(Ora_Define)*MAX_SELECT_LIST_SIZE);
+desc = store_get(sizeof(Ora_Describe)*MAX_SELECT_LIST_SIZE);
+
+if ((num_fields = describe_define(cda,def,desc)) == -1)
+  {
+  *errmsg = oracle_error(oracle_handle, cda->rc, "describe_define failed");
+  *defer_break = FALSE;
+  goto ORACLE_EXIT;
+  }
+
+if (oexec(cda)!=0)
+  {
+  *errmsg = oracle_error(oracle_handle, cda->rc, "oexec failed");
+  *defer_break = FALSE;
+  goto ORACLE_EXIT;
+  }
+
+/* Get the fields and construct the result string. If there is more than one
+row, we insert '\n' between them. */
+
+while (cda->rc != NO_DATA_FOUND)  /* Loop for each row */
+  {
+  ofetch(cda);
+  if(cda->rc == NO_DATA_FOUND) break;
+
+  if (result != NULL) result = string_cat(result, &ssize, &offset, "\n", 1);
+
+  /* Single field - just add on the data */
+
+  if (num_fields == 1)
+    result = string_cat(result, &ssize, &offset, def[0].buf, def[0].col_retlen);
+
+  /* Multiple fields - precede by file name, removing {lead,trail}ing WS */
+
+  else for (i = 0; i < num_fields; i++)
+    {
+    int slen;
+    uschar *s = US desc[i].buf;
+
+    while (*s != 0 && isspace(*s)) s++;
+    slen = Ustrlen(s);
+    while (slen > 0 && isspace(s[slen-1])) slen--;
+    result = string_cat(result, &ssize, &offset, s, slen);
+    result = string_cat(result, &ssize, &offset, US"=", 1);
+
+    /* int and float type wont ever need escaping. Otherwise, quote the value
+    if it contains spaces or is empty. */
+
+    if (desc[i].dbtype != INT_TYPE && desc[i].dbtype != FLOAT_TYPE &&
+       (def[i].buf[0] == 0 || strchr(def[i].buf, ' ') != NULL))
+      {
+      int j;
+      result = string_cat(result, &ssize, &offset, "\"", 1);
+      for (j = 0; j < def[i].col_retlen; j++)
+        {
+        if (def[i].buf[j] == '\"' || def[i].buf[j] == '\\')
+          result = string_cat(result, &ssize, &offset, "\\", 1);
+        result = string_cat(result, &ssize, &offset, def[i].buf+j, 1);
+        }
+      result = string_cat(result, &ssize, &offset, "\"", 1);
+      }
+
+    else switch(desc[i].dbtype)
+      {
+      case INT_TYPE:
+      sprintf(CS tmp, "%d", def[i].int_buf);
+      result = string_cat(result, &ssize, &offset, tmp, Ustrlen(tmp));
+      break;
+
+      case FLOAT_TYPE:
+      sprintf(CS tmp, "%f", def[i].flt_buf);
+      result = string_cat(result, &ssize, &offset, tmp, Ustrlen(tmp));
+      break;
+
+      case STRING_TYPE:
+      result = string_cat(result, &ssize, &offset, def[i].buf,
+        def[i].col_retlen);
+      break;
+
+      default:
+      *errmsg = string_sprintf("ORACLE: unknown field type %d", desc[i].dbtype);
+      *defer_break = FALSE;
+      result = NULL;
+      goto ORACLE_EXIT;
+      }
+
+    result = string_cat(result, &ssize, &offset, " ", 1);
+    }
+  }
+
+/* If result is NULL then no data has been found and so we return FAIL.
+Otherwise, we must terminate the string which has been built; string_cat()
+always leaves enough room for a terminating zero. */
+
+if (result == NULL)
+  {
+  yield = FAIL;
+  *errmsg = "ORACLE: no data found";
+  }
+else
+  {
+  result[offset] = 0;
+  store_reset(result + offset + 1);
+  }
+
+/* Get here by goto from various error checks. */
+
+ORACLE_EXIT:
+
+/* Close the cursor; don't close the connection, as it is cached. */
+
+oclose(cda);
+
+ORACLE_EXIT_NO_VALS:
+
+/* Non-NULL result indicates a sucessful result */
+
+if (result != NULL)
+  {
+  *resultptr = result;
+  return OK;
+  }
+else
+  {
+  DEBUG(D_lookup) debug_printf("%s\n", *errmsg);
+  return yield;      /* FAIL or DEFER */
+  }
+}
+
+
+
+
+/*************************************************
+*               Find entry point                 *
+*************************************************/
+
+/* See local README for interface description. The handle and filename
+arguments are not used. Loop through a list of servers while the query is
+deferred with a retryable error. */
+
+static int
+oracle_find(void *handle, uschar *filename, uschar *query, int length,
+  uschar **result, uschar **errmsg, BOOL *do_cache)
+{
+int sep = 0;
+uschar *server;
+uschar *list = oracle_servers;
+uschar buffer[512];
+
+do_cache = do_cache;   /* Placate picky compilers */
+
+DEBUG(D_lookup) debug_printf("ORACLE query: %s\n", query);
+
+while ((server = string_nextinlist(&list, &sep, buffer, sizeof(buffer))) != NULL)
+  {
+  BOOL defer_break;
+  int rc = perform_oracle_search(query, server, result, errmsg, &defer_break);
+  if (rc != DEFER || defer_break) return rc;
+  }
+
+if (oracle_servers == NULL)
+  *errmsg = "no ORACLE servers defined (oracle_servers option)";
+
+return DEFER;
+}
+
+
+
+/*************************************************
+*               Quote entry point                *
+*************************************************/
+
+/* The only characters that need to be quoted (with backslash) are newline,
+tab, carriage return, backspace, backslash itself, and the quote characters.
+Percent and underscore are not escaped. They are only special in contexts where
+they can be wild cards, and this isn't usually the case for data inserted from
+messages, since that isn't likely to be treated as a pattern of any kind.
+
+Arguments:
+  s          the string to be quoted
+  opt        additional option text or NULL if none
+
+Returns:     the processed string or NULL for a bad option
+*/
+
+static uschar *
+oracle_quote(uschar *s, uschar *opt)
+{
+register int c;
+int count = 0;
+uschar *t = s;
+uschar *quoted;
+
+if (opt != NULL) return NULL;    /* No options are recognized */
+
+while ((c = *t++) != 0)
+  if (strchr("\n\t\r\b\'\"\\", c) != NULL) count++;
+
+if (count == 0) return s;
+t = quoted = store_get((int)strlen(s) + count + 1);
+
+while ((c = *s++) != 0)
+  {
+  if (strchr("\n\t\r\b\'\"\\", c) != NULL)
+    {
+    *t++ = '\\';
+    switch(c)
+      {
+      case '\n': *t++ = 'n';
+      break;
+      case '\t': *t++ = 't';
+      break;
+      case '\r': *t++ = 'r';
+      break;
+      case '\b': *t++ = 'b';
+      break;
+      default:   *t++ = c;
+      break;
+      }
+    }
+  else *t++ = c;
+  }
+
+*t = 0;
+return quoted;
+}
+
+
+/*************************************************
+*         Version reporting entry point          *
+*************************************************/
+
+/* See local README for interface description. */
+
+#include "../version.h"
+
+void
+oracle_version_report(FILE *f)
+{
+#ifdef DYNLOOKUP
+fprintf(f, "Library version: Oracle: Exim version %s\n", EXIM_VERSION_STR);
+#endif
+}
+
+
+static lookup_info _lookup_info = {
+  US"oracle",                    /* lookup name */
+  lookup_querystyle,             /* query-style lookup */
+  oracle_open,                   /* open function */
+  NULL,                          /* check function */
+  oracle_find,                   /* find function */
+  NULL,                          /* no close function */
+  oracle_tidy,                   /* tidy function */
+  oracle_quote,                  /* quoting function */
+  oracle_version_report          /* version reporting */
+};
+
+#ifdef DYNLOOKUP
+#define oracle_lookup_module_info _lookup_module_info
+#endif
+
+static lookup_info *_lookup_list[] = { &_lookup_info };
+lookup_module_info oracle_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 1 };
+
+/* End of lookups/oracle.c */
diff --git a/src/lookups/passwd.c b/src/lookups/passwd.c
new file mode 100644 (file)
index 0000000..4690cbf
--- /dev/null
@@ -0,0 +1,90 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+#include "../exim.h"
+
+
+
+/*************************************************
+*              Open entry point                  *
+*************************************************/
+
+/* See local README for interface description */
+
+static void *
+passwd_open(uschar *filename, uschar **errmsg)
+{
+filename = filename;     /* Keep picky compilers happy */
+errmsg = errmsg;
+return (void *)(-1);     /* Just return something non-null */
+}
+
+
+
+
+/*************************************************
+*         Find entry point for passwd           *
+*************************************************/
+
+/* See local README for interface description */
+
+static int
+passwd_find(void *handle, uschar *filename, uschar *keystring, int length,
+  uschar **result, uschar **errmsg, BOOL *do_cache)
+{
+struct passwd *pw;
+
+handle = handle;         /* Keep picky compilers happy */
+filename = filename;
+length = length;
+errmsg = errmsg;
+do_cache = do_cache;
+
+if (!route_finduser(keystring, &pw, NULL)) return FAIL;
+*result = string_sprintf("*:%d:%d:%s:%s:%s", (int)pw->pw_uid, (int)pw->pw_gid,
+  pw->pw_gecos, pw->pw_dir, pw->pw_shell);
+return OK;
+}
+
+
+
+/*************************************************
+*         Version reporting entry point          *
+*************************************************/
+
+/* See local README for interface description. */
+
+#include "../version.h"
+
+void
+passwd_version_report(FILE *f)
+{
+#ifdef DYNLOOKUP
+fprintf(f, "Library version: passwd: Exim version %s\n", EXIM_VERSION_STR);
+#endif
+}
+
+static lookup_info _lookup_info = {
+  US"passwd",                    /* lookup name */
+  lookup_querystyle,             /* query-style lookup */
+  passwd_open,                   /* open function */
+  NULL,                          /* no check function */
+  passwd_find,                   /* find function */
+  NULL,                          /* no close function */
+  NULL,                          /* no tidy function */
+  NULL,                          /* no quoting function */
+  passwd_version_report          /* version reporting */
+};
+
+#ifdef DYNLOOKUP
+#define passwd_lookup_module_info _lookup_module_info
+#endif
+
+static lookup_info *_lookup_list[] = { &_lookup_info };
+lookup_module_info passwd_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 1 };
+
+/* End of lookups/passwd.c */
diff --git a/src/lookups/pgsql.c b/src/lookups/pgsql.c
new file mode 100644 (file)
index 0000000..95b1b8c
--- /dev/null
@@ -0,0 +1,530 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Thanks to Petr Cech for contributing the original code for these
+functions. Thanks to Joachim Wieland for the initial patch for the Unix domain
+socket extension. */
+
+#include "../exim.h"
+#include "lf_functions.h"
+
+#include <libpq-fe.h>       /* The system header */
+
+/* Structure and anchor for caching connections. */
+
+typedef struct pgsql_connection {
+  struct pgsql_connection *next;
+  uschar *server;
+  PGconn *handle;
+} pgsql_connection;
+
+static pgsql_connection *pgsql_connections = NULL;
+
+
+
+/*************************************************
+*              Open entry point                  *
+*************************************************/
+
+/* See local README for interface description. */
+
+static void *
+pgsql_open(uschar *filename, uschar **errmsg)
+{
+return (void *)(1);    /* Just return something non-null */
+}
+
+
+
+/*************************************************
+*               Tidy entry point                 *
+*************************************************/
+
+/* See local README for interface description. */
+
+static void
+pgsql_tidy(void)
+{
+pgsql_connection *cn;
+while ((cn = pgsql_connections) != NULL)
+  {
+  pgsql_connections = cn->next;
+  DEBUG(D_lookup) debug_printf("close PGSQL connection: %s\n", cn->server);
+  PQfinish(cn->handle);
+  }
+}
+
+
+/*************************************************
+*       Notice processor function for pgsql      *
+*************************************************/
+
+/* This function is passed to pgsql below, and called for any PostgreSQL
+"notices". By default they are written to stderr, which is undesirable.
+
+Arguments:
+  arg        an opaque user cookie (not used)
+  message    the notice
+
+Returns:     nothing
+*/
+
+static void
+notice_processor(void *arg, const char *message)
+{
+arg = arg;   /* Keep compiler happy */
+DEBUG(D_lookup) debug_printf("PGSQL: %s\n", message);
+}
+
+
+
+/*************************************************
+*        Internal search function                *
+*************************************************/
+
+/* This function is called from the find entry point to do the search for a
+single server. The server string is of the form "server/dbname/user/password".
+
+PostgreSQL supports connections through Unix domain sockets. This is usually
+faster and costs less cpu time than a TCP/IP connection. However it can only be
+used if the mail server runs on the same machine as the database server. A
+configuration line for PostgreSQL via Unix domain sockets looks like this:
+
+hide pgsql_servers = (/tmp/.s.PGSQL.5432)/db/user/password[:<nextserver>]
+
+We enclose the path name in parentheses so that its slashes aren't visually
+confused with the delimeters for the other pgsql_server settings.
+
+For TCP/IP connections, the server is a host name and optional port (with a
+colon separator).
+
+NOTE:
+ 1) All three '/' must be present.
+ 2) If host is omitted the local unix socket is used.
+
+Arguments:
+  query        the query string
+  server       the server string; this is in dynamic memory and can be updated
+  resultptr    where to store the result
+  errmsg       where to point an error message
+  defer_break  set TRUE if no more servers are to be tried after DEFER
+  do_cache     set FALSE if data is changed
+
+Returns:       OK, FAIL, or DEFER
+*/
+
+static int
+perform_pgsql_search(uschar *query, uschar *server, uschar **resultptr,
+  uschar **errmsg, BOOL *defer_break, BOOL *do_cache)
+{
+PGconn *pg_conn = NULL;
+PGresult *pg_result = NULL;
+
+int i;
+int ssize = 0;
+int offset = 0;
+int yield = DEFER;
+unsigned int num_fields, num_tuples;
+uschar *result = NULL;
+pgsql_connection *cn;
+uschar *server_copy = NULL;
+uschar *sdata[3];
+
+/* Disaggregate the parameters from the server argument. The order is host or
+path, database, user, password. We can write to the string, since it is in a
+nextinlist temporary buffer. The copy of the string that is used for caching
+has the password removed. This copy is also used for debugging output. */
+
+for (i = 2; i >= 0; i--)
+  {
+  uschar *pp = Ustrrchr(server, '/');
+  if (pp == NULL)
+    {
+    *errmsg = string_sprintf("incomplete pgSQL server data: %s",
+      (i == 2)? server : server_copy);
+    *defer_break = TRUE;
+    return DEFER;
+    }
+  *pp++ = 0;
+  sdata[i] = pp;
+  if (i == 2) server_copy = string_copy(server);  /* sans password */
+  }
+
+/* The total server string has now been truncated so that what is left at the
+start is the identification of the server (host or path). See if we have a
+cached connection to the server. */
+
+for (cn = pgsql_connections; cn != NULL; cn = cn->next)
+  {
+  if (Ustrcmp(cn->server, server_copy) == 0)
+    {
+    pg_conn = cn->handle;
+    break;
+    }
+  }
+
+/* If there is no cached connection, we must set one up. */
+
+if (cn == NULL)
+  {
+  uschar *port = US"";
+
+  /* For a Unix domain socket connection, the path is in parentheses */
+
+  if (*server == '(')
+    {
+    uschar *last_slash, *last_dot, *p;
+
+    p = ++server;
+    while (*p != 0 && *p != ')') p++;
+    *p = 0;
+
+    last_slash = Ustrrchr(server, '/');
+    last_dot = Ustrrchr(server, '.');
+
+    DEBUG(D_lookup) debug_printf("PGSQL new connection: socket=%s "
+      "database=%s user=%s\n", server, sdata[0], sdata[1]);
+
+    /* A valid socket name looks like this: /var/run/postgresql/.s.PGSQL.5432
+    We have to call PQsetdbLogin with '/var/run/postgresql' as the hostname
+    argument and put '5432' into the port variable. */
+
+    if (last_slash == NULL || last_dot == NULL)
+      {
+      *errmsg = string_sprintf("PGSQL invalid filename for socket: %s",
+        server);
+      *defer_break = TRUE;
+      return DEFER;
+      }
+
+    /* Terminate the path name and set up the port: we'll have something like
+    server = "/var/run/postgresql" and port = "5432". */
+
+    *last_slash = 0;
+    port = last_dot + 1;
+    }
+
+  /* Host connection; sort out the port */
+
+  else
+    {
+    uschar *p;
+    if ((p = Ustrchr(server, ':')) != NULL)
+      {
+      *p++ = 0;
+      port = p;
+      }
+
+    if (Ustrchr(server, '/') != NULL)
+      {
+      *errmsg = string_sprintf("unexpected slash in pgSQL server hostname: %s",
+        server);
+      *defer_break = TRUE;
+      return DEFER;
+      }
+
+    DEBUG(D_lookup) debug_printf("PGSQL new connection: host=%s port=%s "
+      "database=%s user=%s\n", server, port, sdata[0], sdata[1]);
+    }
+
+  /* If the database is the empty string, set it NULL - the query must then
+  define it. */
+
+  if (sdata[0][0] == 0) sdata[0] = NULL;
+
+  /* Get store for a new handle, initialize it, and connect to the server */
+
+  pg_conn=PQsetdbLogin(
+    /*  host      port  options tty   database       user       passwd */
+    CS server, CS port,  NULL, NULL, CS sdata[0], CS sdata[1], CS sdata[2]);
+
+  if(PQstatus(pg_conn) == CONNECTION_BAD)
+    {
+    store_reset(server_copy);
+    *errmsg = string_sprintf("PGSQL connection failed: %s",
+      PQerrorMessage(pg_conn));
+    PQfinish(pg_conn);
+    goto PGSQL_EXIT;
+    }
+
+  /* Set the client encoding to SQL_ASCII, which means that the server will
+  not try to interpret the query as being in any fancy encoding such as UTF-8
+  or other multibyte code that might cause problems with escaping. */
+
+  PQsetClientEncoding(pg_conn, "SQL_ASCII");
+
+  /* Set the notice processor to prevent notices from being written to stderr
+  (which is what the default does). Our function (above) just produces debug
+  output. */
+
+  PQsetNoticeProcessor(pg_conn, notice_processor, NULL);
+
+  /* Add the connection to the cache */
+
+  cn = store_get(sizeof(pgsql_connection));
+  cn->server = server_copy;
+  cn->handle = pg_conn;
+  cn->next = pgsql_connections;
+  pgsql_connections = cn;
+  }
+
+/* Else use a previously cached connection */
+
+else
+  {
+  DEBUG(D_lookup) debug_printf("PGSQL using cached connection for %s\n",
+    server_copy);
+  }
+
+/* Run the query */
+
+  pg_result = PQexec(pg_conn, CS query);
+  switch(PQresultStatus(pg_result))
+    {
+    case PGRES_EMPTY_QUERY:
+    case PGRES_COMMAND_OK:
+    /* The command was successful but did not return any data since it was
+     * not SELECT but either an INSERT, UPDATE or DELETE statement. Tell the
+     * high level code to not cache this query, and clean the current cache for
+     * this handle by setting *do_cache FALSE. */
+    result = string_copy(US PQcmdTuples(pg_result));
+    offset = Ustrlen(result);
+    *do_cache = FALSE;
+    DEBUG(D_lookup) debug_printf("PGSQL: command does not return any data "
+      "but was successful. Rows affected: %s\n", result);
+
+    case PGRES_TUPLES_OK:
+    break;
+
+    default:
+    /* This was the original code:
+    *errmsg = string_sprintf("PGSQL: query failed: %s\n",
+                             PQresultErrorMessage(pg_result));
+    This was suggested by a user:
+    */
+
+    *errmsg = string_sprintf("PGSQL: query failed: %s (%s) (%s)\n",
+                             PQresultErrorMessage(pg_result),
+                             PQresStatus(PQresultStatus(pg_result)), query);
+    goto PGSQL_EXIT;
+    }
+
+/* Result is in pg_result. Find the number of fields returned. If this is one,
+we don't add field names to the data. Otherwise we do. If the query did not
+return anything we skip the for loop; this also applies to the case
+PGRES_COMMAND_OK. */
+
+num_fields = PQnfields(pg_result);
+num_tuples = PQntuples(pg_result);
+
+/* Get the fields and construct the result string. If there is more than one
+row, we insert '\n' between them. */
+
+for (i = 0; i < num_tuples; i++)
+  {
+  if (result != NULL)
+    result = string_cat(result, &ssize, &offset, US"\n", 1);
+
+   if (num_fields == 1)
+    {
+    result = string_cat(result, &ssize, &offset,
+      US PQgetvalue(pg_result, i, 0), PQgetlength(pg_result, i, 0));
+    }
+
+   else
+    {
+    int j;
+    for (j = 0; j < num_fields; j++)
+      {
+      uschar *tmp = US PQgetvalue(pg_result, i, j);
+      result = lf_quote(US PQfname(pg_result, j), tmp, Ustrlen(tmp), result,
+        &ssize, &offset);
+      }
+    }
+  }
+
+/* If result is NULL then no data has been found and so we return FAIL.
+Otherwise, we must terminate the string which has been built; string_cat()
+always leaves enough room for a terminating zero. */
+
+if (result == NULL)
+  {
+  yield = FAIL;
+  *errmsg = US"PGSQL: no data found";
+  }
+else
+  {
+  result[offset] = 0;
+  store_reset(result + offset + 1);
+  }
+
+/* Get here by goto from various error checks. */
+
+PGSQL_EXIT:
+
+/* Free store for any result that was got; don't close the connection, as
+it is cached. */
+
+if (pg_result != NULL) PQclear(pg_result);
+
+/* Non-NULL result indicates a sucessful result */
+
+if (result != NULL)
+  {
+  *resultptr = result;
+  return OK;
+  }
+else
+  {
+  DEBUG(D_lookup) debug_printf("%s\n", *errmsg);
+  return yield;      /* FAIL or DEFER */
+  }
+}
+
+
+
+
+/*************************************************
+*               Find entry point                 *
+*************************************************/
+
+/* See local README for interface description. The handle and filename
+arguments are not used. The code to loop through a list of servers while the
+query is deferred with a retryable error is now in a separate function that is
+shared with other SQL lookups. */
+
+static int
+pgsql_find(void *handle, uschar *filename, uschar *query, int length,
+  uschar **result, uschar **errmsg, BOOL *do_cache)
+{
+return lf_sqlperform(US"PostgreSQL", US"pgsql_servers", pgsql_servers, query,
+  result, errmsg, do_cache, perform_pgsql_search);
+}
+
+
+
+/*************************************************
+*               Quote entry point                *
+*************************************************/
+
+/* The characters that always need to be quoted (with backslash) are newline,
+tab, carriage return, backspace, backslash itself, and the quote characters.
+Percent and underscore are only special in contexts where they can be wild
+cards, and this isn't usually the case for data inserted from messages, since
+that isn't likely to be treated as a pattern of any kind. However, pgsql seems
+to allow escaping "on spec". If you use something like "where id="ab\%cd" it
+does treat the string as "ab%cd". So we can safely quote percent and
+underscore. [This is different to MySQL, where you can't do this.]
+
+The original code quoted single quotes as \' which is documented as valid in
+the O'Reilly book "Practical PostgreSQL" (first edition) as an alternative to
+the SQL standard '' way of representing a single quote as data. However, in
+June 2006 there was some security issue with using \' and so this has been
+changed.
+
+[Note: There is a function called PQescapeStringConn() that quotes strings.
+This cannot be used because it needs a PGconn argument (the connection handle).
+Why, I don't know. Seems odd for just string escaping...]
+
+Arguments:
+  s          the string to be quoted
+  opt        additional option text or NULL if none
+
+Returns:     the processed string or NULL for a bad option
+*/
+
+static uschar *
+pgsql_quote(uschar *s, uschar *opt)
+{
+register int c;
+int count = 0;
+uschar *t = s;
+uschar *quoted;
+
+if (opt != NULL) return NULL;     /* No options recognized */
+
+while ((c = *t++) != 0)
+  if (Ustrchr("\n\t\r\b\'\"\\%_", c) != NULL) count++;
+
+if (count == 0) return s;
+t = quoted = store_get(Ustrlen(s) + count + 1);
+
+while ((c = *s++) != 0)
+  {
+  if (c == '\'')
+    {
+    *t++ = '\'';
+    *t++ = '\'';
+    }
+  else if (Ustrchr("\n\t\r\b\"\\%_", c) != NULL)
+    {
+    *t++ = '\\';
+    switch(c)
+      {
+      case '\n': *t++ = 'n';
+      break;
+      case '\t': *t++ = 't';
+      break;
+      case '\r': *t++ = 'r';
+      break;
+      case '\b': *t++ = 'b';
+      break;
+      default:   *t++ = c;
+      break;
+      }
+    }
+  else *t++ = c;
+  }
+
+*t = 0;
+return quoted;
+}
+
+
+/*************************************************
+*         Version reporting entry point          *
+*************************************************/
+
+/* See local README for interface description. */
+
+#include "../version.h"
+
+void
+pgsql_version_report(FILE *f)
+{
+#ifdef DYNLOOKUP
+fprintf(f, "Library version: PostgreSQL: Exim version %s\n", EXIM_VERSION_STR);
+#endif
+
+/* Version reporting: there appears to be no available information about
+the client library in libpq-fe.h; once you have a connection object, you
+can access the server version and the chosen protocol version, but those
+aren't really what we want.  It might make sense to debug_printf those
+when the connection is established though? */
+}
+
+
+static lookup_info _lookup_info = {
+  US"pgsql",                     /* lookup name */
+  lookup_querystyle,             /* query-style lookup */
+  pgsql_open,                    /* open function */
+  NULL,                          /* no check function */
+  pgsql_find,                    /* find function */
+  NULL,                          /* no close function */
+  pgsql_tidy,                    /* tidy function */
+  pgsql_quote,                   /* quoting function */
+  pgsql_version_report           /* version reporting */
+};
+
+#ifdef DYNLOOKUP
+#define pgsql_lookup_module_info _lookup_module_info
+#endif
+
+static lookup_info *_lookup_list[] = { &_lookup_info };
+lookup_module_info pgsql_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 1 };
+
+/* End of lookups/pgsql.c */
diff --git a/src/lookups/redis.c b/src/lookups/redis.c
new file mode 100644 (file)
index 0000000..87cc9fd
--- /dev/null
@@ -0,0 +1,349 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+#include "../exim.h"
+
+#ifdef EXPERIMENTAL_REDIS
+
+#include "lf_functions.h"
+
+#include <hiredis/hiredis.h>
+
+/* Structure and anchor for caching connections. */
+typedef struct redis_connection {
+       struct redis_connection *next;
+       uschar  *server;
+       redisContext    *handle;
+} redis_connection;
+
+static redis_connection *redis_connections = NULL;
+
+static void *
+redis_open(uschar *filename, uschar **errmsg)
+{
+       return (void *)(1);
+}
+
+void
+redis_tidy(void)
+{
+       redis_connection *cn;
+
+       /*
+        * XXX: Not sure how often this is called!
+        * Guess its called after every lookup which probably would mean to just
+        * not use the _tidy() function at all and leave with exim exiting to
+        * GC connections!
+        */
+       while ((cn = redis_connections) != NULL) {
+               redis_connections = cn->next;
+               DEBUG(D_lookup) debug_printf("close REDIS connection: %s\n", cn->server);
+               redisFree(cn->handle);
+       }
+}
+
+/* This function is called from the find entry point to do the search for a
+ * single server.
+ *
+ *     Arguments:
+ *       query        the query string
+ *       server       the server string
+ *       resultptr    where to store the result
+ *       errmsg       where to point an error message
+ *       defer_break  TRUE if no more servers are to be tried after DEFER
+ *       do_cache     set false if data is changed
+ *
+ *     The server string is of the form "host/dbnumber/password". The host can be
+ *     host:port. This string is in a nextinlist temporary buffer, so can be
+ *     overwritten.
+ *
+ *     Returns:       OK, FAIL, or DEFER
+ */
+static int
+perform_redis_search(uschar *command, uschar *server, uschar **resultptr,
+  uschar **errmsg, BOOL *defer_break, BOOL *do_cache)
+{
+       redisContext *redis_handle = NULL;        /* Keep compilers happy */
+       redisReply *redis_reply = NULL;
+       redisReply *entry = NULL;
+       redisReply *tentry = NULL;
+       redis_connection *cn;
+       int ssize = 0;
+       int offset = 0;
+       int yield = DEFER;
+       int i, j;
+       uschar *result = NULL;
+       uschar *server_copy = NULL;
+       uschar *tmp, *ttmp;
+       uschar *sdata[3];
+
+       /*
+        * Disaggregate the parameters from the server argument.
+        * The order is host:port(socket)
+        * We can write to the string, since it is in a nextinlist temporary buffer.
+        * This copy is also used for debugging output.
+        */
+        memset(sdata, 0, sizeof(sdata)) /* Set all to NULL */;
+                for (i = 2; i > 0; i--) {
+                        uschar *pp = Ustrrchr(server, '/');
+                        if (pp == NULL) {
+                                *errmsg = string_sprintf("incomplete Redis server data: %s", (i == 2) ? server : server_copy);
+                                *defer_break = TRUE;
+                                return DEFER;
+                        }
+                        *pp++ = 0;
+                        sdata[i] = pp;
+                        if (i == 2) server_copy = string_copy(server);  /* sans password */
+                }
+        sdata[0] = server;   /* What's left at the start */
+
+        /* If the database or password is an empty string, set it NULL */
+        if (sdata[1][0] == 0) sdata[1] = NULL;
+        if (sdata[2][0] == 0) sdata[2] = NULL;
+
+       /* See if we have a cached connection to the server */
+       for (cn = redis_connections; cn != NULL; cn = cn->next) {
+               if (Ustrcmp(cn->server, server_copy) == 0) {
+                       redis_handle = cn->handle;
+                       break;
+               }
+       }
+
+       if (cn == NULL) {
+               uschar *p;
+               uschar *socket = NULL;
+               int port = 0;
+               /* int redis_err = REDIS_OK; */
+
+               if ((p = Ustrchr(sdata[0], '(')) != NULL) {
+                       *p++ = 0;
+                       socket = p;
+                       while (*p != 0 && *p != ')')
+                               p++;
+                       *p = 0;
+               }
+
+               if ((p = Ustrchr(sdata[0], ':')) != NULL) {
+                       *p++ = 0;
+                       port = Uatoi(p);
+               } else {
+                       port = Uatoi("6379");
+               }
+
+               if (Ustrchr(server, '/') != NULL) {
+                       *errmsg = string_sprintf("unexpected slash in Redis server hostname: %s", sdata[0]);
+                       *defer_break = TRUE;
+                       return DEFER;
+               }
+
+               DEBUG(D_lookup)
+               debug_printf("REDIS new connection: host=%s port=%d socket=%s database=%s\n", sdata[0], port, socket, sdata[1]);
+
+               /* Get store for a new handle, initialize it, and connect to the server */
+               /* XXX: Use timeouts ? */
+               if (socket != NULL)
+                       redis_handle = redisConnectUnix(CCS socket);
+               else
+                       redis_handle = redisConnect(CCS server, port);
+               if (redis_handle == NULL) {
+                       *errmsg = string_sprintf("REDIS connection failed");
+                       *defer_break = FALSE;
+                       goto REDIS_EXIT;
+               }
+
+               /* Add the connection to the cache */
+               cn = store_get(sizeof(redis_connection));
+               cn->server = server_copy;
+               cn->handle = redis_handle;
+               cn->next = redis_connections;
+               redis_connections = cn;
+       } else {
+               DEBUG(D_lookup)
+               debug_printf("REDIS using cached connection for %s\n", server_copy);
+       }
+
+       /* Authenticate if there is a password */
+       if(sdata[2] != NULL) {
+               if ((redis_reply = redisCommand(redis_handle, "AUTH %s", sdata[2])) == NULL) {
+                       *errmsg = string_sprintf("REDIS Authentication failed: %s\n", redis_handle->errstr);
+                       *defer_break = FALSE;
+                       goto REDIS_EXIT;
+               }
+       }
+
+       /* Select the database if there is a dbnumber passed */
+       if(sdata[1] != NULL) {
+               if ((redis_reply = redisCommand(redis_handle, "SELECT %s", sdata[1])) == NULL) {
+                       *errmsg = string_sprintf("REDIS: Selecting database=%s failed: %s\n", sdata[1], redis_handle->errstr);
+                       *defer_break = FALSE;
+                       goto REDIS_EXIT;
+               } else {
+                       DEBUG(D_lookup) debug_printf("REDIS: Selecting database=%s\n", sdata[1]);
+               }
+       }
+
+       /* Run the command */
+       if ((redis_reply = redisCommand(redis_handle, CS command)) == NULL) {
+               *errmsg = string_sprintf("REDIS: query failed: %s\n", redis_handle->errstr);
+               *defer_break = FALSE;
+               goto REDIS_EXIT;
+       }
+
+       switch (redis_reply->type) {
+       case REDIS_REPLY_ERROR:
+               *errmsg = string_sprintf("REDIS: lookup result failed: %s\n", redis_reply->str);
+               *defer_break = FALSE;
+               *do_cache = FALSE;
+               goto REDIS_EXIT;
+               /* NOTREACHED */
+
+               break;
+       case REDIS_REPLY_NIL:
+               DEBUG(D_lookup) debug_printf("REDIS: query was not one that returned any data\n");
+               result = string_sprintf("");
+               *do_cache = FALSE;
+               goto REDIS_EXIT;
+               /* NOTREACHED */
+
+               break;
+       case REDIS_REPLY_INTEGER:
+               ttmp = (redis_reply->integer == 1) ? US"true" : US"false";
+               result = string_cat(result, &ssize, &offset, US ttmp, Ustrlen(ttmp));
+               break;
+       case REDIS_REPLY_STRING:
+       case REDIS_REPLY_STATUS:
+               result = string_cat(result, &ssize, &offset, US redis_reply->str, redis_reply->len);
+               break;
+       case REDIS_REPLY_ARRAY:
+
+               /* NOTE: For now support 1 nested array result. If needed a limitless result can be parsed */
+               for (i = 0; i < redis_reply->elements; i++) {
+                       entry = redis_reply->element[i];
+
+                       if (result != NULL)
+                               result = string_cat(result, &ssize, &offset, US"\n", 1);
+
+                       switch (entry->type) {
+                       case REDIS_REPLY_INTEGER:
+                               tmp = string_sprintf("%d", entry->integer);
+                               result = string_cat(result, &ssize, &offset, US tmp, Ustrlen(tmp));
+                               break;
+                       case REDIS_REPLY_STRING:
+                               result = string_cat(result, &ssize, &offset, US entry->str, entry->len);
+                               break;
+                       case REDIS_REPLY_ARRAY:
+                               for (j = 0; j < entry->elements; j++) {
+                                       tentry = entry->element[j];
+
+                                       if (result != NULL)
+                                               result = string_cat(result, &ssize, &offset, US"\n", 1);
+
+                                       switch (tentry->type) {
+                                       case REDIS_REPLY_INTEGER:
+                                               ttmp = string_sprintf("%d", tentry->integer);
+                                               result = string_cat(result, &ssize, &offset, US ttmp, Ustrlen(ttmp));
+                                               break;
+                                       case REDIS_REPLY_STRING:
+                                               result = string_cat(result, &ssize, &offset, US tentry->str, tentry->len);
+                                               break;
+                                       case REDIS_REPLY_ARRAY:
+                                               DEBUG(D_lookup) debug_printf("REDIS: result has nesting of arrays which is not supported. Ignoring!\n");
+                                               break;
+                                       default:
+                                               DEBUG(D_lookup) debug_printf("REDIS: result has unsupported type. Ignoring!\n");
+                                               break;
+                                       }
+                               }
+                               break;
+                       default:
+                               DEBUG(D_lookup) debug_printf("REDIS: query returned unsupported type\n");
+                               break;
+                       }
+               }
+               break;
+       }
+
+
+       if (result == NULL) {
+               yield = FAIL;
+               *errmsg = US"REDIS: no data found";
+       } else {
+               result[offset] = 0;
+               store_reset(result + offset + 1);
+       }
+
+    REDIS_EXIT:
+       /* Free store for any result that was got; don't close the connection, as it is cached. */
+       if (redis_reply != NULL)
+               freeReplyObject(redis_reply);
+
+       /* Non-NULL result indicates a sucessful result */
+       if (result != NULL) {
+               *resultptr = result;
+               return OK;
+       } else {
+               DEBUG(D_lookup) debug_printf("%s\n", *errmsg);
+               /* NOTE: Required to close connection since it needs to be reopened */
+               return yield;      /* FAIL or DEFER */
+       }
+}
+
+/*************************************************
+*               Find entry point                 *
+*************************************************/
+/*
+ * See local README for interface description. The handle and filename
+ * arguments are not used. The code to loop through a list of servers while the
+ * query is deferred with a retryable error is now in a separate function that is
+ * shared with other noSQL lookups.
+ */
+
+static int
+redis_find(void *handle __attribute__((unused)), uschar *filename __attribute__((unused)),
+           uschar *command, int length, uschar **result, uschar **errmsg, BOOL *do_cache)
+{
+       return lf_sqlperform(US"Redis", US"redis_servers", redis_servers, command,
+         result, errmsg, do_cache, perform_redis_search);
+}
+
+/*************************************************
+*         Version reporting entry point          *
+*************************************************/
+#include "../version.h"
+
+void
+redis_version_report(FILE *f)
+{
+       fprintf(f, "Library version: REDIS: Compile: %d [%d]\n",
+               HIREDIS_MAJOR, HIREDIS_MINOR);
+#ifdef DYNLOOKUP
+       fprintf(f, "                        Exim version %s\n", EXIM_VERSION_STR);
+#endif
+}
+
+/* These are the lookup_info blocks for this driver */
+static lookup_info redis_lookup_info = {
+  US"redis",                     /* lookup name */
+  lookup_querystyle,             /* query-style lookup */
+  redis_open,                    /* open function */
+  NULL,                          /* no check function */
+  redis_find,                    /* find function */
+  NULL,                          /* no close function */
+  redis_tidy,                    /* tidy function */
+  NULL,                                /* quoting function */
+  redis_version_report           /* version reporting */
+};
+
+#ifdef DYNLOOKUP
+#define redis_lookup_module_info _lookup_module_info
+#endif /* DYNLOOKUP */
+
+static lookup_info *_lookup_list[] = { &redis_lookup_info };
+lookup_module_info redis_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 1 };
+
+#endif /* EXPERIMENTAL_REDIS */
+/* End of lookups/redis.c */
diff --git a/src/lookups/spf.c b/src/lookups/spf.c
new file mode 100644 (file)
index 0000000..23ad2ad
--- /dev/null
@@ -0,0 +1,114 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/*
+ * Exim - SPF lookup module using libspf2
+ * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ *
+ * Copyright (c) 2005 Chris Webb, Arachsys Internet Services Ltd
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ */
+
+#include "../exim.h"
+
+#ifndef EXPERIMENTAL_SPF
+static void dummy(int x);
+static void dummy2(int x) { dummy(x-1); }
+static void dummy(int x) { dummy2(x-1); }
+#else
+
+#include "lf_functions.h"
+#ifndef HAVE_NS_TYPE
+#define HAVE_NS_TYPE
+#endif
+#include <spf2/spf.h>
+#include <spf2/spf_dns_resolv.h>
+#include <spf2/spf_dns_cache.h>
+
+static void *spf_open(uschar *filename, uschar **errmsg) {
+  SPF_server_t *spf_server = NULL;
+  spf_server = SPF_server_new(SPF_DNS_CACHE, 0);
+  if (spf_server == NULL) {
+    *errmsg = US"SPF_server_new() failed";
+    return NULL;
+  }
+  return (void *) spf_server;
+}
+
+static void spf_close(void *handle) {
+  SPF_server_t *spf_server = handle;
+  if (spf_server) SPF_server_free(spf_server);
+}
+
+static int spf_find(void *handle, uschar *filename, uschar *keystring, int key_len,
+             uschar **result, uschar **errmsg, BOOL *do_cache) {
+  SPF_server_t *spf_server = handle;
+  SPF_request_t *spf_request = NULL;
+  SPF_response_t *spf_response = NULL;
+
+  spf_request = SPF_request_new(spf_server);
+  if (spf_request == NULL) {
+    *errmsg = US"SPF_request_new() failed";
+    return FAIL;
+  }
+
+  if (SPF_request_set_ipv4_str(spf_request, CS filename)) {
+    *errmsg = string_sprintf("invalid IP address '%s'", filename);
+    return FAIL;
+  }
+  if (SPF_request_set_env_from(spf_request, CS keystring)) {
+    *errmsg = string_sprintf("invalid envelope from address '%s'", keystring);
+    return FAIL;
+  }
+
+  SPF_request_query_mailfrom(spf_request, &spf_response);
+  *result = string_copy(US SPF_strresult(SPF_response_result(spf_response)));
+  SPF_response_free(spf_response);
+  SPF_request_free(spf_request);
+  return OK;
+}
+
+
+/*************************************************
+*         Version reporting entry point          *
+*************************************************/
+
+/* See local README for interface description. */
+
+#include "../version.h"
+
+void
+spf_version_report(FILE *f)
+{
+#ifdef DYNLOOKUP
+fprintf(f, "Library version: SPF: Exim version %s\n", EXIM_VERSION_STR);
+#endif
+}
+
+
+static lookup_info _lookup_info = {
+  US"spf",                       /* lookup name */
+  0,                             /* not absfile, not query style */
+  spf_open,                      /* open function */
+  NULL,                          /* no check function */
+  spf_find,                      /* find function */
+  spf_close,                     /* close function */
+  NULL,                          /* no tidy function */
+  NULL,                          /* no quoting function */
+  spf_version_report             /* version reporting */
+};
+
+#ifdef DYNLOOKUP
+#define spf_lookup_module_info _lookup_module_info
+#endif
+
+static lookup_info *_lookup_list[] = { &_lookup_info };
+lookup_module_info spf_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 1 };
+
+#endif /* EXPERIMENTAL_SPF */
diff --git a/src/lookups/sqlite.c b/src/lookups/sqlite.c
new file mode 100644 (file)
index 0000000..c0fc891
--- /dev/null
@@ -0,0 +1,196 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+#include "../exim.h"
+#include "lf_functions.h"
+
+#include <sqlite3.h>
+
+
+/*************************************************
+*              Open entry point                  *
+*************************************************/
+
+/* See local README for interface description. */
+
+static void *
+sqlite_open(uschar *filename, uschar **errmsg)
+{
+sqlite3 *db = NULL;
+int ret;
+
+ret = sqlite3_open((char *)filename, &db);
+if (ret != 0)
+  {
+  *errmsg = (void *)sqlite3_errmsg(db);
+  debug_printf("Error opening database: %s\n", *errmsg);
+  }
+
+sqlite3_busy_timeout(db, 1000 * sqlite_lock_timeout);
+return db;
+}
+
+
+/*************************************************
+*               Find entry point                 *
+*************************************************/
+
+/* See local README for interface description. */
+
+struct strbuf {
+  uschar *string;
+  int size;
+  int len;
+};
+
+static int sqlite_callback(void *arg, int argc, char **argv, char **azColName)
+{
+struct strbuf *res = arg;
+int i;
+
+/* For second and subsequent results, insert \n */
+
+if (res->string != NULL)
+  res->string = string_cat(res->string, &res->size, &res->len, US"\n", 1);
+
+if (argc > 1)
+  {
+  /* For multiple fields, include the field name too */
+  for (i = 0; i < argc; i++)
+    {
+    uschar *value = US((argv[i] != NULL)? argv[i]:"<NULL>");
+    res->string = lf_quote(US azColName[i], value, Ustrlen(value), res->string,
+      &res->size, &res->len);
+    }
+  }
+
+else
+  {
+  res->string = string_append(res->string, &res->size, &res->len, 1,
+    (argv[0] != NULL)? argv[0]:"<NULL>");
+  }
+
+res->string[res->len] = 0;
+return 0;
+}
+
+
+static int
+sqlite_find(void *handle, uschar *filename, uschar *query, int length,
+  uschar **result, uschar **errmsg, BOOL *do_cache)
+{
+int ret;
+struct strbuf res = { NULL, 0, 0 };
+
+ret = sqlite3_exec(handle, (char *)query, sqlite_callback, &res, (char **)errmsg);
+if (ret != SQLITE_OK)
+  {
+  debug_printf("sqlite3_exec failed: %s\n", *errmsg);
+  return FAIL;
+  }
+
+if (res.string == NULL) *do_cache = FALSE;
+
+*result = res.string;
+return OK;
+}
+
+
+
+/*************************************************
+*               Close entry point                *
+*************************************************/
+
+/* See local README for interface description. */
+
+static void sqlite_close(void *handle)
+{
+sqlite3_close(handle);
+}
+
+
+
+/*************************************************
+*               Quote entry point                *
+*************************************************/
+
+/* From what I have found so far, the only character that needs to be quoted
+for sqlite is the single quote, and it is quoted by doubling.
+
+Arguments:
+  s          the string to be quoted
+  opt        additional option text or NULL if none
+
+Returns:     the processed string or NULL for a bad option
+*/
+
+static uschar *
+sqlite_quote(uschar *s, uschar *opt)
+{
+register int c;
+int count = 0;
+uschar *t = s;
+uschar *quoted;
+
+if (opt != NULL) return NULL;     /* No options recognized */
+
+while ((c = *t++) != 0) if (c == '\'') count++;
+
+if (count == 0) return s;
+t = quoted = store_get(Ustrlen(s) + count + 1);
+
+while ((c = *s++) != 0)
+  {
+  if (c == '\'') *t++ = '\'';
+  *t++ = c;
+  }
+
+*t = 0;
+return quoted;
+}
+
+
+
+/*************************************************
+*         Version reporting entry point          *
+*************************************************/
+
+/* See local README for interface description. */
+
+#include "../version.h"
+
+void
+sqlite_version_report(FILE *f)
+{
+fprintf(f, "Library version: SQLite: Compile: %s\n"
+           "                         Runtime: %s\n",
+        SQLITE_VERSION, sqlite3_libversion());
+#ifdef DYNLOOKUP
+fprintf(f, "                         Exim version %s\n", EXIM_VERSION_STR);
+#endif
+}
+
+static lookup_info _lookup_info = {
+  US"sqlite",                    /* lookup name */
+  lookup_absfilequery,           /* query-style lookup, starts with file name */
+  sqlite_open,                   /* open function */
+  NULL,                          /* no check function */
+  sqlite_find,                   /* find function */
+  sqlite_close,                  /* close function */
+  NULL,                          /* no tidy function */
+  sqlite_quote,                  /* quoting function */
+  sqlite_version_report          /* version reporting */
+};
+
+#ifdef DYNLOOKUP
+#define sqlite_lookup_module_info _lookup_module_info
+#endif
+
+static lookup_info *_lookup_list[] = { &_lookup_info };
+lookup_module_info sqlite_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 1 };
+
+/* End of lookups/sqlite.c */
diff --git a/src/lookups/testdb.c b/src/lookups/testdb.c
new file mode 100644 (file)
index 0000000..7a81795
--- /dev/null
@@ -0,0 +1,103 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+#include "../exim.h"
+#include "lf_functions.h"
+
+
+/* These are not real lookup functions; they are just a way of testing the
+rest of Exim by providing an easy way of specifying particular yields from
+the find function. */
+
+
+/*************************************************
+*              Open entry point                  *
+*************************************************/
+
+/* See local README for interface description. */
+
+static void *
+testdb_open(uschar *filename, uschar **errmsg)
+{
+filename = filename;   /* Keep picky compilers happy */
+errmsg = errmsg;
+return (void *)(1);    /* Just return something non-null */
+}
+
+
+
+/*************************************************
+*               Find entry point                 *
+*************************************************/
+
+/* See local README for interface description. */
+
+static int
+testdb_find(void *handle, uschar *filename, uschar *query, int length,
+  uschar **result, uschar **errmsg, BOOL *do_cache)
+{
+handle = handle;          /* Keep picky compilers happy */
+filename = filename;
+length = length;
+
+if (Ustrcmp(query, "fail") == 0)
+  {
+  *errmsg = US"testdb lookup forced FAIL";
+  DEBUG(D_lookup) debug_printf("%s\n", *errmsg);
+  return FAIL;
+  }
+if (Ustrcmp(query, "defer") == 0)
+  {
+  *errmsg = US"testdb lookup forced DEFER";
+  DEBUG(D_lookup) debug_printf("%s\n", *errmsg);
+  return DEFER;
+  }
+
+if (Ustrcmp(query, "nocache") == 0) *do_cache = FALSE;
+
+*result = string_copy(query);
+return OK;
+}
+
+
+/*************************************************
+*         Version reporting entry point          *
+*************************************************/
+
+/* See local README for interface description. */
+
+#include "../version.h"
+
+void
+testdb_version_report(FILE *f)
+{
+#ifdef DYNLOOKUP
+fprintf(f, "Library version: TestDB: Exim version %s\n", EXIM_VERSION_STR);
+#endif
+}
+
+
+static lookup_info _lookup_info = {
+  US"testdb",                    /* lookup name */
+  lookup_querystyle,             /* query-style lookup */
+  testdb_open,                   /* open function */
+  NULL,                          /* check function */
+  testdb_find,                   /* find function */
+  NULL,                          /* no close function */
+  NULL,                          /* no tidy function */
+  NULL,                          /* no quoting function */
+  testdb_version_report          /* version reporting */
+};
+
+#ifdef DYNLOOKUP
+#define testdb_lookup_module_info _lookup_module_info
+#endif
+
+static lookup_info *_lookup_list[] = { &_lookup_info };
+lookup_module_info testdb_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 1 };
+
+/* End of lookups/testdb.c */
diff --git a/src/lookups/whoson.c b/src/lookups/whoson.c
new file mode 100644 (file)
index 0000000..4166089
--- /dev/null
@@ -0,0 +1,101 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* This code originally came from Robert Wal. */
+
+#include "../exim.h"
+
+
+#include <whoson.h>        /* Public header */
+
+
+/*************************************************
+*              Open entry point                  *
+*************************************************/
+
+/* See local README for interface description. */
+
+static void *
+whoson_open(uschar *filename, uschar **errmsg)
+{
+filename = filename;   /* Keep picky compilers happy */
+errmsg = errmsg;
+return (void *)(1);    /* Just return something non-null */
+}
+
+
+/*************************************************
+*               Find entry point                 *
+*************************************************/
+
+/* See local README for interface description. */
+
+static int
+whoson_find(void *handle, uschar *filename, uschar *query, int length,
+  uschar **result, uschar **errmsg, BOOL *do_cache)
+{
+uschar buffer[80];
+handle = handle;          /* Keep picky compilers happy */
+filename = filename;
+length = length;
+errmsg = errmsg;
+do_cache = do_cache;
+
+switch (wso_query(CS query, CS buffer, sizeof(buffer)))
+  {
+  case 0:
+  *result = string_copy(buffer);    /* IP in database; return name of user */
+  return OK;
+
+  case +1:
+  return FAIL;                      /* IP not in database */
+
+  default:
+  *errmsg = string_sprintf("WHOSON: failed to complete: %s", buffer);
+  return DEFER;
+  }
+}
+
+
+
+/*************************************************
+*         Version reporting entry point          *
+*************************************************/
+
+/* See local README for interface description. */
+
+#include "../version.h"
+
+void
+whoson_version_report(FILE *f)
+{
+fprintf(f, "Library version: Whoson: Runtime: %s\n", wso_version());
+#ifdef DYNLOOKUP
+fprintf(f, "                         Exim version %s\n", EXIM_VERSION_STR);
+#endif
+}
+
+static lookup_info _lookup_info = {
+  US"whoson",                    /* lookup name */
+  lookup_querystyle,             /* query-style lookup */
+  whoson_open,                   /* open function */
+  NULL,                          /* check function */
+  whoson_find,                   /* find function */
+  NULL,                          /* no close function */
+  NULL,                          /* no tidy function */
+  NULL,                          /* no quoting function */
+  whoson_version_report          /* version reporting */
+};
+
+#ifdef DYNLOOKUP
+#define whoson_lookup_module_info _lookup_module_info
+#endif
+
+static lookup_info *_lookup_list[] = { &_lookup_info };
+lookup_module_info whoson_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 1 };
+
+/* End of lookups/whoson.c */
diff --git a/src/lss.c b/src/lss.c
new file mode 100644 (file)
index 0000000..ab358d9
--- /dev/null
+++ b/src/lss.c
@@ -0,0 +1,142 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Support functions for calling from local_scan(). These are mostly just
+wrappers for various internal functions. */
+
+
+#include "exim.h"
+
+
+/*************************************************
+*            Match a domain in a list            *
+*************************************************/
+
+/*
+Arguments:
+  domain         the domain we are testing
+  list           the domain list
+
+Returns:         OK/FAIL/DEFER
+*/
+
+int
+lss_match_domain(uschar *domain, uschar *list)
+{
+return match_isinlist(domain, &list, 0, &domainlist_anchor, NULL, MCL_DOMAIN,
+  TRUE, NULL);
+}
+
+
+
+/*************************************************
+*            Match a local part in a list        *
+*************************************************/
+
+/*
+Arguments:
+  local_part     the local part we are testing
+  list           the local part list
+  caseless       TRUE for caseless matching
+
+Returns:         OK/FAIL/DEFER
+*/
+
+int
+lss_match_local_part(uschar *local_part, uschar *list, BOOL caseless)
+{
+return match_isinlist(local_part, &list, 0, &localpartlist_anchor, NULL,
+  MCL_LOCALPART, caseless, NULL);
+}
+
+
+
+/*************************************************
+*            Match an address in a list          *
+*************************************************/
+
+/*
+Arguments:
+  address        the address we are testing
+  list           the address list
+  caseless       TRUE for caseless matching
+
+Returns:         OK/FAIL/DEFER
+*/
+
+int
+lss_match_address(uschar *address, uschar *list, BOOL caseless)
+{
+return match_address_list(address, caseless, TRUE, &list, NULL, -1, 0, NULL);
+}
+
+
+
+/*************************************************
+*            Match a host in a list              *
+*************************************************/
+
+/*
+Arguments:
+  host name      the name of the host we are testing, or NULL if this is the
+                   sender host and its name hasn't yet been looked up
+  host address   the IP address of the host, or an empty string for a local
+                   message
+  list           the host list
+
+Returns:         OK/FAIL/DEFER
+                 ERROR if failed to find host name when needed
+*/
+
+int
+lss_match_host(uschar *host_name, uschar *host_address, uschar *list)
+{
+return verify_check_this_host(&list, NULL, host_name, host_address, NULL);
+}
+
+
+
+/*************************************************
+*           Base 64 encode/decode                *
+*************************************************/
+
+/* These functions just give less "internal" names to the functions.
+
+Arguments:
+  clear       points to the clear text bytes
+  len         the number of bytes to encode
+
+Returns:      a pointer to the zero-terminated base 64 string, which
+              is in working store
+*/
+
+uschar *
+lss_b64encode(uschar *clear, int len)
+{
+return auth_b64encode(clear, len);
+}
+
+/*
+Arguments:
+  code        points to the coded string, zero-terminated
+  ptr         where to put the pointer to the result, which is in
+              dynamic store
+
+Returns:      the number of bytes in the result,
+              or -1 if the input was malformed
+
+A zero is added on to the end to make it easy in cases where the result is to
+be interpreted as text. This is not included in the count. */
+
+int
+lss_b64decode(uschar *code, uschar **ptr)
+{
+return auth_b64decode(code, ptr);
+}
+
+
+/* End of lss.c */
diff --git a/src/macros.h b/src/macros.h
new file mode 100644 (file)
index 0000000..b7dd337
--- /dev/null
@@ -0,0 +1,898 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2014 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+
+/* These two macros make it possible to obtain the result of macro-expanding
+a string as a text string. This is sometimes useful for debugging output. */
+
+#define mac_string(s) # s
+#define mac_expanded_string(s) mac_string(s)
+
+
+/* When running in the test harness, the load average is fudged. */
+
+#define OS_GETLOADAVG() \
+  (running_in_test_harness? (test_harness_load_avg += 10) : os_getloadavg())
+
+
+/* The address_item structure has a word full of 1-bit flags. These macros
+manipulate them. */
+
+#define setflag(addr,flag)    addr->flags |= (flag)
+#define clearflag(addr,flag)  addr->flags &= ~(flag)
+
+#define testflag(addr,flag)       ((addr->flags & (flag)) != 0)
+#define testflagsall(addr,flag)   ((addr->flags & (flag)) == (flag))
+
+#define copyflag(addrnew,addrold,flag) \
+  addrnew->flags = (addrnew->flags & ~(flag)) | (addrold->flags & (flag))
+
+#define orflag(addrnew,addrold,flag) \
+  addrnew->flags |= addrold->flags & (flag)
+
+
+/* For almost all calls to convert things to printing characters, we want to
+allow tabs. A macro just makes life a bit easier. */
+
+#define string_printing(s) string_printing2((s), TRUE)
+
+
+/* We need a special return code for "no recipients and failed to send an error
+message". ANSI C defines only EXIT_FAILURE and EXIT_SUCCESS. On the assumption
+that these are always 1 and 0 on Unix systems ... */
+
+#define EXIT_NORECIPIENTS 2
+
+
+/* Character-handling macros. It seems that the set of standard functions in
+ctype.h aren't actually all that useful. One reason for this is that email is
+international, so the concept of using a locale to vary what they do is not
+helpful. Another problem is that in different operating systems, the libraries
+yield different results, even in the default locale. For example, Linux yields
+TRUE for iscntrl() for all characters > 127, whereas many other systems yield
+FALSE. For these reasons we define our own set of macros for a number of
+character testing functions. Ensure that all these tests treat their arguments
+as unsigned. */
+
+#define mac_iscntrl(c) \
+  ((uschar)(c) < 32 || (uschar)(c) == 127)
+
+#define mac_iscntrl_or_special(c) \
+  ((uschar)(c) < 32 || strchr(" ()<>@,;:\\\".[]\177", (uschar)(c)) != NULL)
+
+#define mac_isgraph(c) \
+  ((uschar)(c) > 32 && (uschar)(c) != 127)
+
+#define mac_isprint(c) \
+  (((uschar)(c) >= 32 && (uschar)(c) <= 126) || c == '\t' || \
+  ((uschar)(c) > 127 && print_topbitchars))
+
+
+/* Convenience for testing strings */
+
+#define streqic(Foo, Bar) (strcmpic(Foo, Bar) == 0)
+
+
+/* When built with TLS support, the act of flushing SMTP output becomes
+a no-op once an SSL session is in progress. */
+
+#ifdef SUPPORT_TLS
+#define mac_smtp_fflush() if (tls_in.active < 0) fflush(smtp_out);
+#else
+#define mac_smtp_fflush() fflush(smtp_out);
+#endif
+
+
+/* Define which ends of pipes are for reading and writing, as some systems
+don't make the file descriptors two-way. */
+
+#define pipe_read  0
+#define pipe_write 1
+
+/* The RFC 1413 ident port */
+
+#define IDENT_PORT 113
+
+/* A macro to simplify testing bits in lookup types */
+
+#define mac_islookup(a,b) ((lookup_list[a]->type & (b)) != 0)
+
+/* Debugging control */
+
+#define DEBUG(x)      if ((debug_selector & (x)) != 0)
+#define HDEBUG(x)     if (host_checking || (debug_selector & (x)) != 0)
+
+/* The default From: text for DSNs */
+
+#define DEFAULT_DSN_FROM "Mail Delivery System <Mailer-Daemon@$qualify_domain>"
+
+/* The size of the vector for saving/restoring address expansion pointers while
+verifying. This has to be explicit because it is referenced in more than one
+source module. */
+
+#define ADDRESS_EXPANSIONS_COUNT 18
+
+/* The maximum permitted number of command-line (-D) macro definitions. We
+need a limit only to make it easier to generate argument vectors for re-exec
+of Exim. */
+
+#define MAX_CLMACROS 10
+
+/* The number of integer variables available in filter files. If this is
+changed, then the tables in expand.c for accessing them must be changed too. */
+
+#define FILTER_VARIABLE_COUNT 10
+
+/* The size of the vector holding delay warning times */
+
+#define DELAY_WARNING_SIZE 12
+
+/* The size of the buffer holding the processing information string. */
+
+#define PROCESS_INFO_SIZE 256
+
+/* The size of buffer to get for constructing log entries. Make it big
+enough to hold all the headers from a normal kind of message. */
+
+#define LOG_BUFFER_SIZE 8192
+
+/* The size of the circular buffer that remembers recent SMTP commands */
+
+#define SMTP_HBUFF_SIZE 20
+
+/* The initial size of a big buffer for use in various places. It gets put
+into big_buffer_size and in some circumstances increased. It should be at least
+as long as the maximum path length. */
+
+#if defined PATH_MAX && PATH_MAX > 16384
+#define BIG_BUFFER_SIZE PATH_MAX
+#elif defined MAXPATHLEN && MAXPATHLEN > 16384
+#define BIG_BUFFER_SIZE MAXPATHLEN
+#else
+#define BIG_BUFFER_SIZE 16384
+#endif
+
+/* This limits the length of data returned by local_scan(). Because it is
+written on the spool, it gets read into big_buffer. */
+
+#define LOCAL_SCAN_MAX_RETURN (BIG_BUFFER_SIZE - 24)
+
+/* A limit to the length of an address. RFC 2821 limits the local part to 64
+and the domain to 255, so this should be adequate, taking into account quotings
+etc. */
+
+#define ADDRESS_MAXLENGTH 512
+
+/* The length of the base names of spool files, which consist of an internal
+message id with a trailing "-H" or "-D" added. */
+
+#define SPOOL_NAME_LENGTH (MESSAGE_ID_LENGTH+2)
+
+/* The maximum number of message ids to store in a waiting database
+record. */
+
+#define WAIT_NAME_MAX 50
+
+/* Wait this long before determining that a Proxy Protocol configured
+host isn't speaking the protocol, and so is disallowed. Can be moved to
+runtime configuration if per site settings become needed. */
+#ifdef EXPERIMENTAL_PROXY
+#define PROXY_NEGOTIATION_TIMEOUT_SEC 3
+#define PROXY_NEGOTIATION_TIMEOUT_USEC 0
+#endif
+
+/* Fixed option values for all PCRE functions */
+
+#define PCRE_COPT 0   /* compile */
+#define PCRE_EOPT 0   /* exec */
+
+/* Macros for trivial functions */
+
+#define mac_ismsgid(s) \
+  (pcre_exec(regex_ismsgid,NULL,CS s,Ustrlen(s),0,PCRE_EOPT,NULL,0) >= 0)
+
+
+/* Options for dns_next_rr */
+
+enum { RESET_NEXT, RESET_ANSWERS, RESET_AUTHORITY, RESET_ADDITIONAL };
+
+/* Argument values for the time-of-day function */
+
+enum { tod_log, tod_log_bare, tod_log_zone, tod_log_datestamp_daily,
+       tod_log_datestamp_monthly, tod_zone, tod_full, tod_bsdin,
+       tod_mbx, tod_epoch, tod_epoch_l, tod_zulu };
+
+/* For identifying types of driver */
+
+enum {
+  DTYPE_NONE,
+  DTYPE_ROUTER,
+  DTYPE_TRANSPORT
+};
+
+/* Error numbers for generating error messages when reading a message on the
+standard input. */
+
+enum {
+  ERRMESS_BADARGADDRESS,    /* Bad address via argument list */
+  ERRMESS_BADADDRESS,       /* Bad address read via -t */
+  ERRMESS_NOADDRESS,        /* Message has no addresses */
+  ERRMESS_IGADDRESS,        /* All -t addresses ignored */
+  ERRMESS_BADNOADDRESS,     /* Bad address via -t, leaving none */
+  ERRMESS_IOERR,            /* I/O error while reading a message */
+  ERRMESS_VLONGHEADER,      /* Excessively long message header */
+  ERRMESS_VLONGHDRLINE,     /* Excessively long single line in header */
+  ERRMESS_TOOBIG,           /* Message too big */
+  ERRMESS_TOOMANYRECIP,     /* Too many recipients */
+  ERRMESS_LOCAL_SCAN,       /* Rejected by local scan */
+  ERRMESS_LOCAL_ACL         /* Rejected by non-SMTP ACL */
+#ifdef EXPERIMENTAL_DMARC
+ ,ERRMESS_DMARC_FORENSIC    /* DMARC Forensic Report */
+#endif
+};
+
+/* Error handling styles - set by option, and apply only when receiving
+a local message not via SMTP. */
+
+enum {
+  ERRORS_SENDER,            /* Return to sender (default) */
+  ERRORS_STDERR             /* Write on stderr */
+};
+
+/* Exec control values when Exim execs itself via child_exec_exim. */
+
+enum {
+  CEE_RETURN_ARGV,          /* Don't exec, just build and return argv */
+  CEE_EXEC_EXIT,            /* Just exit if exec fails */
+  CEE_EXEC_PANIC            /* Panic-die if exec fails */
+};
+
+/* Bit values for filter_test */
+
+#define FTEST_NONE     0    /* Not filter testing */
+#define FTEST_USER     1    /* Testing user filter */
+#define FTEST_SYSTEM   2    /* Testing system filter */
+
+/* Returns from the routing, transport and authentication functions (not all
+apply to all of them). Some other functions also use these convenient values,
+and some additional values are used only by non-driver functions.
+
+OK, FAIL, DEFER, ERROR, and FAIL_FORCED are also declared in local_scan.h for
+use in the local_scan() function and in ${dlfunc loaded functions. Do not
+change them unilaterally. */
+
+#define  OK            0    /* Successful match */
+#define  DEFER         1    /* Defer - some problem */
+#define  FAIL          2    /* Matching failed */
+#define  ERROR         3    /* Internal or config error */
+#define  FAIL_FORCED   4    /* "Forced" failure */
+/***********/
+#define DECLINE        5    /* Declined to handle the address, pass to next
+                                 router unless no_more is set */
+#define PASS           6    /* Pass to next driver, or to pass_router,
+                                 even if no_more is set */
+#define DISCARD        7    /* Address routed to :blackhole: or "seen finish" */
+#define SKIP           8    /* Skip this router (used in route_address only) */
+#define REROUTED       9    /* Address was changed and child created*/
+#define PANIC         10    /* Hard failed with internal error */
+#define BAD64         11    /* Bad base64 data (auth) */
+#define UNEXPECTED    12    /* Unexpected initial auth data */
+#define CANCELLED     13    /* Authentication cancelled */
+#define FAIL_SEND     14    /* send() failed in authenticator */
+#define FAIL_DROP     15    /* Fail and drop connection (used in ACL) */
+
+/* Returns from the deliver_message() function */
+
+#define DELIVER_ATTEMPTED_NORMAL   0  /* Tried a normal delivery */
+#define DELIVER_MUA_SUCCEEDED      1  /* Success when mua_wrapper is set */
+#define DELIVER_MUA_FAILED         2  /* Failure when mua_wrapper is set */
+#define DELIVER_NOT_ATTEMPTED      3  /* Not tried (no msg or is locked */
+
+/* Returns from DNS lookup functions. */
+
+enum { DNS_SUCCEED, DNS_NOMATCH, DNS_NODATA, DNS_AGAIN, DNS_FAIL };
+
+/* Ending states when reading a message. The order is important. The test
+for having to swallow the rest of an SMTP message is whether the value is
+>= END_NOTENDED. */
+
+#define END_NOTSTARTED 0    /* Message not started */
+#define END_DOT        1    /* Message ended with '.' */
+#define END_EOF        2    /* Message ended with EOF (error for SMTP) */
+#define END_NOTENDED   3    /* Message reading not yet ended */
+#define END_SIZE       4    /* Reading ended because message too big */
+#define END_WERROR     5    /* Write error while reading the message */
+
+/* Options bits for debugging; D_v and D_local_scan are also in local_scan.h */
+
+#define D_v                          0x00000001
+#define D_local_scan                 0x00000002
+
+#define D_acl                        0x00000004
+#define D_auth                       0x00000008
+#define D_deliver                    0x00000010
+#define D_dns                        0x00000020
+#define D_dnsbl                      0x00000040
+#define D_exec                       0x00000080
+#define D_expand                     0x00000100
+#define D_filter                     0x00000200
+#define D_hints_lookup               0x00000400
+#define D_host_lookup                0x00000800
+#define D_ident                      0x00001000
+#define D_interface                  0x00002000
+#define D_lists                      0x00004000
+#define D_load                       0x00008000
+#define D_lookup                     0x00010000
+#define D_memory                     0x00020000
+#define D_pid                        0x00040000
+#define D_process_info               0x00080000
+#define D_queue_run                  0x00100000
+#define D_receive                    0x00200000
+#define D_resolver                   0x00400000
+#define D_retry                      0x00800000
+#define D_rewrite                    0x01000000
+#define D_route                      0x02000000
+#define D_timestamp                  0x04000000
+#define D_tls                        0x08000000
+#define D_transport                  0x10000000
+#define D_uid                        0x20000000
+#define D_verify                     0x40000000
+
+/* The D_all value must always have all bits set, as it is recognized specially
+by the function that decodes debug and log selectors. This is to enable it to
+set all the bits in a multi-word selector. Debug doesn't use this yet, but we
+are getting close. In fact, we want to omit "memory" for -d+all, but can't
+handle this here. It is fudged externally. */
+
+#define D_all                        0xffffffff
+
+#define D_any                        (D_all & \
+                                       ~(D_v           | \
+                                         D_pid         | \
+                                         D_timestamp)  )
+
+#define D_default                    (0xffffffff & \
+                                       ~(D_expand      | \
+                                         D_filter      | \
+                                         D_interface   | \
+                                         D_load        | \
+                                         D_local_scan  | \
+                                         D_memory      | \
+                                         D_pid         | \
+                                         D_timestamp   | \
+                                         D_resolver))
+
+/* Options bits for logging. Those that will end up in log_write_selector have
+values < 0x80000000. They can be used in calls to log_write(). The others have
+values > 0x80000000 and are put into log_extra_selector (without the top bit).
+These are only ever tested independently. "All" is a magic value that is used
+only in the name table to set all options in both bit maps. */
+
+/* The L_all value must always have all bits set, as it is recognized specially
+by the function that decodes debug and log selectors. This is to enable it to
+set all the bits in a multi-word selector. */
+
+#define L_all                          0xffffffff
+
+#define L_address_rewrite              0x00000001
+#define L_all_parents                  0x00000002
+#define L_connection_reject            0x00000004
+#define L_delay_delivery               0x00000008
+#define L_dnslist_defer                0x00000010
+#define L_etrn                         0x00000020
+#define L_host_lookup_failed           0x00000040
+#define L_lost_incoming_connection     0x00000080
+#define L_queue_run                    0x00000100
+#define L_retry_defer                  0x00000200
+#define L_size_reject                  0x00000400
+#define L_skip_delivery                0x00000800
+#define L_smtp_connection              0x00001000
+#define L_smtp_incomplete_transaction  0x00002000
+#define L_smtp_protocol_error          0x00004000
+#define L_smtp_syntax_error            0x00008000
+
+#define LX_acl_warn_skipped            0x80000001
+#define LX_arguments                   0x80000002
+#define LX_deliver_time                0x80000004
+#define LX_delivery_size               0x80000008
+#define LX_ident_timeout               0x80000010
+#define LX_incoming_interface          0x80000020
+#define LX_incoming_port               0x80000040
+#define LX_outgoing_port               0x80000080
+#define LX_pid                         0x80000100
+#define LX_queue_time                  0x80000200
+#define LX_queue_time_overall          0x80000400
+#define LX_received_sender             0x80000800
+#define LX_received_recipients         0x80001000
+#define LX_rejected_header             0x80002000
+#define LX_return_path_on_delivery     0x80004000
+#define LX_sender_on_delivery          0x80008000
+#define LX_sender_verify_fail          0x80010000
+#define LX_smtp_confirmation           0x80020000
+#define LX_smtp_no_mail                0x80040000
+#define LX_subject                     0x80080000
+#define LX_tls_certificate_verified    0x80100000
+#define LX_tls_cipher                  0x80200000
+#define LX_tls_peerdn                  0x80400000
+#define LX_tls_sni                     0x80800000
+#define LX_unknown_in_list             0x81000000
+#define LX_8bitmime                    0x82000000
+#define LX_smtp_mailauth               0x84000000
+#define LX_proxy                       0x88000000
+
+#define L_default     (L_connection_reject        | \
+                       L_delay_delivery           | \
+                       L_dnslist_defer            | \
+                       L_etrn                     | \
+                       L_host_lookup_failed       | \
+                       L_lost_incoming_connection | \
+                       L_queue_run                | \
+                       L_retry_defer              | \
+                       L_size_reject              | \
+                       L_skip_delivery)
+
+#define LX_default   ((LX_acl_warn_skipped        | \
+                       LX_rejected_header         | \
+                       LX_sender_verify_fail      | \
+                       LX_smtp_confirmation       | \
+                       LX_tls_cipher) & 0x7fffffff)
+
+/* Private error numbers for delivery failures, set negative so as not
+to conflict with system errno values. */
+
+#define ERRNO_UNKNOWNERROR    (-1)
+#define ERRNO_USERSLASH       (-2)
+#define ERRNO_EXISTRACE       (-3)
+#define ERRNO_NOTREGULAR      (-4)
+#define ERRNO_NOTDIRECTORY    (-5)
+#define ERRNO_BADUGID         (-6)
+#define ERRNO_BADMODE         (-7)
+#define ERRNO_INODECHANGED    (-8)
+#define ERRNO_LOCKFAILED      (-9)
+#define ERRNO_BADADDRESS2    (-10)
+#define ERRNO_FORBIDPIPE     (-11)
+#define ERRNO_FORBIDFILE     (-12)
+#define ERRNO_FORBIDREPLY    (-13)
+#define ERRNO_MISSINGPIPE    (-14)
+#define ERRNO_MISSINGFILE    (-15)
+#define ERRNO_MISSINGREPLY   (-16)
+#define ERRNO_BADREDIRECT    (-17)
+#define ERRNO_SMTPCLOSED     (-18)
+#define ERRNO_SMTPFORMAT     (-19)
+#define ERRNO_SPOOLFORMAT    (-20)
+#define ERRNO_NOTABSOLUTE    (-21)
+#define ERRNO_EXIMQUOTA      (-22)   /* Exim-imposed quota */
+#define ERRNO_HELD           (-23)
+#define ERRNO_FILTER_FAIL    (-24)   /* Delivery filter process failure */
+#define ERRNO_CHHEADER_FAIL  (-25)   /* Delivery add/remove header failure */
+#define ERRNO_WRITEINCOMPLETE (-26)  /* Delivery write incomplete error */
+#define ERRNO_EXPANDFAIL     (-27)   /* Some expansion failed */
+#define ERRNO_GIDFAIL        (-28)   /* Failed to get gid */
+#define ERRNO_UIDFAIL        (-29)   /* Failed to get uid */
+#define ERRNO_BADTRANSPORT   (-30)   /* Unset or non-existent transport */
+#define ERRNO_MBXLENGTH      (-31)   /* MBX length mismatch */
+#define ERRNO_UNKNOWNHOST    (-32)   /* Lookup failed in smtp transport */
+#define ERRNO_FORMATUNKNOWN  (-33)   /* Can't match format in appendfile */
+#define ERRNO_BADCREATE      (-34)   /* Creation outside home in appendfile */
+#define ERRNO_LISTDEFER      (-35)   /* Can't check a list; lookup defer */
+#define ERRNO_DNSDEFER       (-36)   /* DNS lookup defer */
+#define ERRNO_TLSFAILURE     (-37)   /* Failed to start TLS session */
+#define ERRNO_TLSREQUIRED    (-38)   /* Mandatory TLS session not started */
+#define ERRNO_CHOWNFAIL      (-39)   /* Failed to chown a file */
+#define ERRNO_PIPEFAIL       (-40)   /* Failed to create a pipe */
+#define ERRNO_CALLOUTDEFER   (-41)   /* When verifying */
+#define ERRNO_AUTHFAIL       (-42)   /* When required by client */
+#define ERRNO_CONNECTTIMEOUT (-43)   /* Used internally in smtp transport */
+#define ERRNO_RCPT4XX        (-44)   /* RCPT gave 4xx error */
+#define ERRNO_MAIL4XX        (-45)   /* MAIL gave 4xx error */
+#define ERRNO_DATA4XX        (-46)   /* DATA gave 4xx error */
+#define ERRNO_PROXYFAIL      (-47)   /* Negotiation failed for proxy configured host */
+
+/* These must be last, so all retry deferments can easily be identified */
+
+#define ERRNO_RETRY_BASE     (-51)   /* Base to test against */
+#define ERRNO_RRETRY         (-51)   /* Not time for routing */
+#define ERRNO_LRETRY         (-52)   /* Not time for local delivery */
+#define ERRNO_HRETRY         (-53)   /* Not time for any remote host */
+#define ERRNO_LOCAL_ONLY     (-54)   /* Local-only delivery */
+#define ERRNO_QUEUE_DOMAIN   (-55)   /* Domain in queue_domains */
+
+/* Special actions to take after failure or deferment. */
+
+enum {
+  SPECIAL_NONE,             /* No special action */
+  SPECIAL_FREEZE,           /* Freeze message */
+  SPECIAL_FAIL,             /* Fail the delivery */
+  SPECIAL_WARN              /* Send a warning message */
+};
+
+/* Flags that get ORed into the more_errno field of an address to give more
+information about errors for retry purposes. They are greater than 256, because
+the bottom byte contains 'A' or 'M' for remote addresses, to indicate whether
+the name was looked up only via an address record or whether MX records were
+used, respectively. */
+
+#define RTEF_CTOUT     0x0100      /* Connection timed out */
+
+/* Permission and other options for parse_extract_addresses(),
+filter_interpret(), and rda_interpret(), i.e. what special things are allowed
+in redirection operations. Not all apply to all cases. Some of the bits allow
+and some forbid, reflecting the "allow" and "forbid" options in the redirect
+router, which were chosen to represent the standard situation for users'
+.forward files. */
+
+#define RDO_BLACKHOLE    0x00000001  /* Forbid :blackhole: */
+#define RDO_DEFER        0x00000002  /* Allow :defer: or "defer" */
+#define RDO_EACCES       0x00000004  /* Ignore EACCES */
+#define RDO_ENOTDIR      0x00000008  /* Ignore ENOTDIR */
+#define RDO_EXISTS       0x00000010  /* Forbid "exists" in expansion in filter */
+#define RDO_FAIL         0x00000020  /* Allow :fail: or "fail" */
+#define RDO_FILTER       0x00000040  /* Allow a filter script */
+#define RDO_FREEZE       0x00000080  /* Allow "freeze" */
+#define RDO_INCLUDE      0x00000100  /* Forbid :include: */
+#define RDO_LOG          0x00000200  /* Forbid "log" */
+#define RDO_LOOKUP       0x00000400  /* Forbid "lookup" in expansion in filter */
+#define RDO_PERL         0x00000800  /* Forbid "perl" in expansion in filter */
+#define RDO_READFILE     0x00001000  /* Forbid "readfile" in exp in filter */
+#define RDO_READSOCK     0x00002000  /* Forbid "readsocket" in exp in filter */
+#define RDO_RUN          0x00004000  /* Forbid "run" in expansion in filter */
+#define RDO_DLFUNC       0x00008000  /* Forbid "dlfunc" in expansion in filter */
+#define RDO_REALLOG      0x00010000  /* Really do log (not testing/verifying) */
+#define RDO_REWRITE      0x00020000  /* Rewrite generated addresses */
+#define RDO_EXIM_FILTER  0x00040000  /* Forbid Exim filters */
+#define RDO_SIEVE_FILTER 0x00080000  /* Forbid Sieve filters */
+#define RDO_PREPEND_HOME 0x00100000  /* Prepend $home to relative paths in Exim filter save commands */
+
+/* This is the set that apply to expansions in filters */
+
+#define RDO_FILTER_EXPANSIONS \
+  (RDO_EXISTS|RDO_LOOKUP|RDO_PERL|RDO_READFILE|RDO_READSOCK|RDO_RUN|RDO_DLFUNC)
+
+/* As well as the RDO bits themselves, we need the bit numbers in order to
+access (most of) the individual bits as separate options. This could be
+automated, but I haven't bothered. Keep this list in step with the above! */
+
+enum { RDON_BLACKHOLE, RDON_DEFER, RDON_EACCES, RDON_ENOTDIR, RDON_EXISTS,
+  RDON_FAIL, RDON_FILTER, RDON_FREEZE, RDON_INCLUDE, RDON_LOG, RDON_LOOKUP,
+  RDON_PERL, RDON_READFILE, RDON_READSOCK, RDON_RUN, RDON_DLFUNC, RDON_REALLOG,
+  RDON_REWRITE, RDON_EXIM_FILTER, RDON_SIEVE_FILTER, RDON_PREPEND_HOME };
+
+/* Results of filter or forward file processing. Some are only from a filter;
+some are only from a forward file. */
+
+enum {
+  FF_DELIVERED,         /* Success, took significant action */
+  FF_NOTDELIVERED,      /* Success, didn't take significant action */
+  FF_BLACKHOLE,         /* Blackholing requested */
+  FF_DEFER,             /* Defer requested */
+  FF_FAIL,              /* Fail requested */
+  FF_INCLUDEFAIL,       /* :include: failed */
+  FF_NONEXIST,          /* Forward file does not exist */
+  FF_FREEZE,            /* Freeze requested */
+  FF_ERROR              /* We have a problem */
+};
+
+/* Values for identifying particular headers; printing characters are used, so
+they can be read in the spool file for those headers that are permanently
+marked. The lower case values don't get onto the spool; they are used only as
+return values from header_checkname(). */
+
+#define htype_other         ' '   /* Unspecified header */
+#define htype_from          'F'
+#define htype_to            'T'
+#define htype_cc            'C'
+#define htype_bcc           'B'
+#define htype_id            'I'   /* for message-id */
+#define htype_reply_to      'R'
+#define htype_received      'P'   /* P for Postmark */
+#define htype_sender        'S'
+#define htype_old           '*'   /* Replaced header */
+
+#define htype_date          'd'
+#define htype_return_path   'p'
+#define htype_delivery_date 'x'
+#define htype_envelope_to   'e'
+#define htype_subject       's'
+
+/* These values are used only when adding new headers from an ACL; they too
+never get onto the spool. The type of the added header is set by reference
+to the header name, by calling header_checkname(). */
+
+#define htype_add_top       'a'
+#define htype_add_rec       'r'
+#define htype_add_bot       'z'
+#define htype_add_rfc       'f'
+
+/* Types of item in options lists. These are the bottom 8 bits of the "type"
+field, which is an int. The opt_void value is used for entries in tables that
+point to special types of value that are accessed only indirectly (e.g. the
+rewrite data that is built out of a string option.) We need to have some values
+visible in local_scan, so the following are declared there:
+
+  opt_stringptr, opt_int, opt_octint, opt_mkint, opt_Kint, opt_fixed, opt_time,
+  opt_bool
+
+To make sure we don't conflict, the local_scan.h values start from zero, and
+those defined here start from 32. The boolean ones must all be together so they
+can be easily tested as a group. That is the only use of opt_bool_last. */
+
+enum { opt_bit = 32, opt_bool_verify, opt_bool_set, opt_expand_bool,
+  opt_bool_last,
+  opt_rewrite, opt_timelist, opt_uid, opt_gid, opt_uidlist, opt_gidlist,
+  opt_expand_uid, opt_expand_gid, opt_void };
+
+/* There's a high-ish bit which is used to flag duplicate options, kept
+for compatibility, which shouldn't be output. Also used for hidden options
+that are automatically maintained from others. Another high bit is used to
+flag driver options that although private (so as to be settable only on some
+drivers), are stored in the instance block so as to be accessible from outside.
+A third high bit is set when an option is read, so as to be able to give an
+error if any option is set twice. Finally, there's a bit which is set when an
+option is set with the "hide" prefix, to prevent -bP from showing it to
+non-admin callers. The next byte up in the int is used to keep the bit number
+for booleans that are kept in one bit. */
+
+#define opt_hidden  0x100      /* Private to Exim */
+#define opt_public  0x200      /* Stored in the main instance block */
+#define opt_set     0x400      /* Option is set */
+#define opt_secure  0x800      /* "hide" prefix used */
+#define opt_rep_con 0x1000     /* Can be appended to by a repeated line (condition) */
+#define opt_rep_str 0x2000     /* Can be appended to by a repeated line (string) */
+#define opt_mask    0x00ff
+
+/* Verify types when directing and routing */
+
+enum { v_none, v_sender, v_recipient, v_expn };
+
+/* Option flags for verify_address() */
+
+#define vopt_fake_sender          0x0001   /* for verify=sender=<address> */
+#define vopt_is_recipient         0x0002
+#define vopt_qualify              0x0004
+#define vopt_expn                 0x0008
+#define vopt_callout_fullpm       0x0010   /* full postmaster during callout */
+#define vopt_callout_random       0x0020   /* during callout */
+#define vopt_callout_no_cache     0x0040   /* disable callout cache */
+#define vopt_callout_recipsender  0x0080   /* use real sender to verify recip */
+#define vopt_callout_recippmaster 0x0100   /* use postmaster to verify recip */
+#define vopt_success_on_redirect  0x0200
+
+/* Values for fields in callout cache records */
+
+#define ccache_unknown         0       /* test hasn't been done */
+#define ccache_accept          1
+#define ccache_reject          2       /* All rejections except */
+#define ccache_reject_mfnull   3       /* MAIL FROM:<> was rejected */
+
+/* Options for lookup functions */
+
+#define lookup_querystyle      1    /* query-style lookup */
+#define lookup_absfile         2    /* requires absolute file name */
+#define lookup_absfilequery    4    /* query-style starts with file name */
+
+/* Status values for host_item blocks. Require hstatus_unusable and
+hstatus_unusable_expired to be last. */
+
+enum { hstatus_unknown, hstatus_usable, hstatus_unusable,
+       hstatus_unusable_expired };
+
+/* Reasons why a host is unusable (for clearer log messages) */
+
+enum { hwhy_unknown, hwhy_retry, hwhy_failed, hwhy_deferred, hwhy_ignored };
+
+/* Domain lookup types for routers */
+
+enum { lk_default, lk_byname, lk_bydns };
+
+/* Values for the self_code fields */
+
+enum { self_freeze, self_defer, self_send, self_reroute, self_pass, self_fail };
+
+/* Flags for rewrite rules */
+
+#define rewrite_sender       0x0001
+#define rewrite_from         0x0002
+#define rewrite_to           0x0004
+#define rewrite_cc           0x0008
+#define rewrite_bcc          0x0010
+#define rewrite_replyto      0x0020
+#define rewrite_all_headers  0x003F  /* all header flags */
+
+#define rewrite_envfrom      0x0040
+#define rewrite_envto        0x0080
+#define rewrite_all_envelope 0x00C0  /* all envelope flags */
+
+#define rewrite_all      (rewrite_all_headers | rewrite_all_envelope)
+
+#define rewrite_smtp         0x0100  /* rewrite at SMTP time */
+#define rewrite_smtp_sender  0x0200  /* SMTP sender rewrite (allows <>) */
+#define rewrite_qualify      0x0400  /* qualify if necessary */
+#define rewrite_repeat       0x0800  /* repeat rewrite rule */
+
+#define rewrite_whole        0x1000  /* option bit for headers */
+#define rewrite_quit         0x2000  /* "no more" option */
+
+/* Flags for log_write(); LOG_MAIN, LOG_PANIC, and LOG_REJECT are also in
+local_scan.h */
+
+#define LOG_MAIN           1      /* Write to the main log */
+#define LOG_PANIC          2      /* Write to the panic log */
+#define LOG_PANIC_DIE      6      /* Write to the panic log and then die */
+#define LOG_REJECT        16      /* Write to the reject log, with headers */
+#define LOG_SENDER        32      /* Add raw sender to the message */
+#define LOG_RECIPIENTS    64      /* Add raw recipients to the message */
+#define LOG_CONFIG       128      /* Add "Exim configuration error" */
+#define LOG_CONFIG_FOR  (256+128) /* Add " for" instead of ":\n" */
+#define LOG_CONFIG_IN   (512+128) /* Add " in line x[ of file y]" */
+
+/* and for debug_bits() logging action control: */
+#define DEBUG_FROM_CONFIG       0x0001
+
+/* SMTP command identifiers for the smtp_connection_had field that records the
+most recent SMTP commands. Must be kept in step with the list of names in
+smtp_in.c that is used for creating the smtp_no_mail logging action. SCH_NONE
+is "empty". */
+
+enum { SCH_NONE, SCH_AUTH, SCH_DATA, SCH_EHLO, SCH_ETRN, SCH_EXPN, SCH_HELO,
+       SCH_HELP, SCH_MAIL, SCH_NOOP, SCH_QUIT, SCH_RCPT, SCH_RSET, SCH_STARTTLS,
+       SCH_VRFY };
+
+/* Returns from host_find_by{name,dns}() */
+
+enum {
+  HOST_FIND_FAILED,     /* failed to find the host */
+  HOST_FIND_AGAIN,      /* could not resolve at this time */
+  HOST_FOUND,           /* found host */
+  HOST_FOUND_LOCAL,     /* found, but MX points to local host */
+  HOST_IGNORED          /* found but ignored - used internally only */
+};
+
+/* Flags for host_find_bydns() */
+
+#define HOST_FIND_BY_SRV          0x0001
+#define HOST_FIND_BY_MX           0x0002
+#define HOST_FIND_BY_A            0x0004
+#define HOST_FIND_QUALIFY_SINGLE  0x0008
+#define HOST_FIND_SEARCH_PARENTS  0x0010
+
+/* Actions applied to specific messages. */
+
+enum { MSG_DELIVER, MSG_FREEZE, MSG_REMOVE, MSG_THAW, MSG_ADD_RECIPIENT,
+       MSG_MARK_ALL_DELIVERED, MSG_MARK_DELIVERED, MSG_EDIT_SENDER,
+       MSG_SHOW_COPY, MSG_LOAD,
+       /* These ones must be last: a test for >= MSG_SHOW_BODY is used
+       to test for actions that list individual spool files. */
+       MSG_SHOW_BODY, MSG_SHOW_HEADER, MSG_SHOW_LOG };
+
+/* Returns from the spool_read_header() function */
+
+enum {
+  spool_read_OK,        /* success */
+  spool_read_notopen,   /* open failed */
+  spool_read_enverror,  /* error in the envelope */
+  spool_read_hdrerror   /* error in the headers */
+};
+
+/* Options for transport_write_message */
+
+#define topt_add_return_path    0x001
+#define topt_add_delivery_date  0x002
+#define topt_add_envelope_to    0x004
+#define topt_use_crlf           0x008  /* Terminate lines with CRLF */
+#define topt_end_dot            0x010  /* Send terminting dot line */
+#define topt_no_headers         0x020  /* Omit headers */
+#define topt_no_body            0x040  /* Omit body */
+#define topt_escape_headers     0x080  /* Apply escape check to headers */
+
+#ifdef EXPERIMENTAL_DSN
+/* Flags for recipient_block, used in DSN support */
+
+#define rf_dsnlasthop           0x01  /* Do not propagate DSN any further */
+#define rf_notify_never         0x02  /* NOTIFY= settings */
+#define rf_notify_success       0x04
+#define rf_notify_failure       0x08
+#define rf_notify_delay         0x10
+
+#define rf_dsnflags  (rf_notify_never | rf_notify_success | \
+                      rf_notify_failure | rf_notify_delay)
+
+/* DSN RET types */
+
+#define dsn_ret_full            1
+#define dsn_ret_hdrs            2
+
+#define dsn_support_unknown     0
+#define dsn_support_yes         1
+#define dsn_support_no          2
+
+#endif
+
+/* Codes for the host_find_failed and host_all_ignored options. */
+
+#define hff_freeze   0
+#define hff_defer    1
+#define hff_pass     2
+#define hff_decline  3
+#define hff_fail     4
+#define hff_ignore   5
+
+/* Router information flags */
+
+#define ri_yestransport    0x0001    /* Must have a transport */
+#define ri_notransport     0x0002    /* Must not have a transport */
+
+/* Codes for match types in match_check_list; to any of them, MCL_NOEXPAND may
+be added */
+
+#define MCL_NOEXPAND  16
+
+enum { MCL_STRING, MCL_DOMAIN, MCL_HOST, MCL_ADDRESS, MCL_LOCALPART };
+
+/* Codes for the places from which ACLs can be called. These are cunningly
+ordered to make it easy to implement tests for certain ACLs when processing
+"control" modifiers, by means of a maximum "where" value. Do not modify this
+order without checking carefully!
+
+**** IMPORTANT***
+****   Furthermore, remember to keep these in step with the tables
+****   of names and response codes in globals.c.
+**** IMPORTANT ****
+*/
+
+enum { ACL_WHERE_RCPT,       /* Some controls are for RCPT only */
+       ACL_WHERE_MAIL,       /* )                                           */
+       ACL_WHERE_PREDATA,    /* ) There are several tests for "in message", */
+       ACL_WHERE_MIME,       /* ) implemented by <= WHERE_NOTSMTP           */
+       ACL_WHERE_DKIM,       /* )                                           */
+       ACL_WHERE_DATA,       /* )                                           */
+#ifndef DISABLE_PRDR
+       ACL_WHERE_PRDR,       /* )                                           */
+#endif
+       ACL_WHERE_NOTSMTP,    /* )                                           */
+
+       ACL_WHERE_AUTH,       /* These remaining ones are not currently    */
+       ACL_WHERE_CONNECT,    /* required to be in a special order so they */
+       ACL_WHERE_ETRN,       /* are just alphabetical.                    */
+       ACL_WHERE_EXPN,
+       ACL_WHERE_HELO,
+       ACL_WHERE_MAILAUTH,
+       ACL_WHERE_NOTSMTP_START,
+       ACL_WHERE_NOTQUIT,
+       ACL_WHERE_QUIT,
+       ACL_WHERE_STARTTLS,
+       ACL_WHERE_VRFY,
+
+       ACL_WHERE_DELIVERY,
+       ACL_WHERE_UNKNOWN     /* Currently used by a ${acl:name} expansion */
+     };
+
+/* Situations for spool_write_header() */
+
+enum { SW_RECEIVING, SW_DELIVERING, SW_MODIFYING };
+
+/* MX fields for hosts not obtained from MX records are always negative.
+MX_NONE is the default case; lesser values are used when the hosts are
+randomized in batches. */
+
+#define MX_NONE           (-1)
+
+/* host_item.port defaults to PORT_NONE; the only current case where this
+is changed before running the transport is when an dnslookup router sets an
+explicit port number. */
+
+#define PORT_NONE     (-1)
+
+/* Flags for single-key search defaults */
+
+#define SEARCH_STAR       0x01
+#define SEARCH_STARAT     0x02
+
+/* Filter types */
+
+enum { FILTER_UNSET, FILTER_FORWARD, FILTER_EXIM, FILTER_SIEVE };
+
+/* End of macros.h */
diff --git a/src/malware.c b/src/malware.c
new file mode 100644 (file)
index 0000000..04b5887
--- /dev/null
@@ -0,0 +1,1679 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) Tom Kistner <tom@duncanthrax.net> 2003-2014 */
+/* License: GPL */
+
+/* Code for calling virus (malware) scanners. Called from acl.c. */
+
+#include "exim.h"
+#ifdef WITH_CONTENT_SCAN
+
+typedef enum {M_FPROTD, M_DRWEB, M_AVES, M_FSEC, M_KAVD, M_CMDL,
+               M_SOPHIE, M_CLAMD, M_SOCK, M_MKSD} scanner_t;
+typedef enum {MC_NONE, MC_TCP, MC_UNIX, MC_STRM} contype_t;
+static struct scan
+{
+  scanner_t    scancode;
+  const uschar * name;
+  const uschar * options_default;
+  contype_t    conn;
+} m_scans[] =
+{
+  { M_FPROTD,  US"f-protd",    US"localhost 10200-10204",            MC_TCP },
+  { M_DRWEB,   US"drweb",      US"/usr/local/drweb/run/drwebd.sock", MC_STRM },
+  { M_AVES,    US"aveserver",  US"/var/run/aveserver",               MC_UNIX },
+  { M_FSEC,    US"fsecure",    US"/var/run/.fsav",                   MC_UNIX },
+  { M_KAVD,    US"kavdaemon",  US"/var/run/AvpCtl",                  MC_UNIX },
+  { M_CMDL,    US"cmdline",    NULL,                                 MC_NONE },
+  { M_SOPHIE,  US"sophie",     US"/var/run/sophie",                  MC_UNIX },
+  { M_CLAMD,   US"clamd",      US"/tmp/clamd",                       MC_NONE },
+  { M_SOCK,    US"sock",       US"/tmp/malware.sock",                MC_STRM },
+  { M_MKSD,    US"mksd",       NULL,                                 MC_NONE },
+  { -1,                NULL,           NULL, MC_NONE }         /* end-marker */
+};
+
+/* The maximum number of clamd servers that are supported in the configuration */
+#define MAX_CLAMD_SERVERS 32
+#define MAX_CLAMD_SERVERS_S "32"
+/* Maximum length of the hostname that can be specified in the clamd address list */
+#define MAX_CLAMD_ADDRESS_LENGTH 64
+#define MAX_CLAMD_ADDRESS_LENGTH_S "64"
+
+typedef struct clamd_address_container {
+  uschar tcp_addr[MAX_CLAMD_ADDRESS_LENGTH+1];
+  unsigned int tcp_port;
+} clamd_address_container;
+
+/* declaration of private routines */
+static int mksd_scan_packed(struct scan * scanent, int sock, uschar *scan_filename);
+static int malware_internal(uschar **listptr, uschar *eml_filename, BOOL faking);
+
+#ifndef nelements
+# define nelements(arr) (sizeof(arr) / sizeof(arr[0]))
+#endif
+
+
+#define        MALWARE_TIMEOUT             120
+
+
+#define DRWEBD_SCAN_CMD             (1)     /* scan file, buffer or diskfile */
+#define DRWEBD_RETURN_VIRUSES       (1<<0)   /* ask daemon return to us viruses names from report */
+#define DRWEBD_IS_MAIL              (1<<19)  /* say to daemon that format is "archive MAIL" */
+
+#define DERR_READ_ERR               (1<<0)   /* read error */
+#define DERR_NOMEMORY               (1<<2)   /* no memory */
+#define DERR_TIMEOUT                (1<<9)   /* scan timeout has run out */
+#define DERR_BAD_CALL               (1<<15)  /* wrong command */
+
+/* Routine to check whether a system is big- or little-endian.
+   Ripped from http://www.faqs.org/faqs/graphics/fileformats-faq/part4/section-7.html
+   Needed for proper kavdaemon implementation. Sigh. */
+#define BIG_MY_ENDIAN      0
+#define LITTLE_MY_ENDIAN   1
+static int test_byte_order(void);
+static inline int
+test_byte_order()
+{
+  short int word = 0x0001;
+  char *byte = (char *) &word;
+  return(byte[0] ? LITTLE_MY_ENDIAN : BIG_MY_ENDIAN);
+}
+
+BOOL malware_ok = FALSE;
+
+/* Gross hacks for the -bmalware option; perhaps we should just create
+the scan directory normally for that case, but look into rigging up the
+needed header variables if not already set on the command-line? */
+extern int spool_mbox_ok;
+extern uschar spooled_message_id[17];
+
+/*************************************************
+*          Scan an email for malware             *
+*************************************************/
+
+/* This is the normal interface for scanning an email, which doesn't need a
+filename; it's a wrapper around the malware_file function.
+
+Arguments:
+  listptr     the list of options to the "malware = ..." ACL condition
+
+Returns:      Exim message processing code (OK, FAIL, DEFER, ...)
+              where true means malware was found (condition applies)
+*/
+int
+malware(uschar **listptr)
+{
+  uschar * scan_filename;
+  int ret;
+
+  scan_filename = string_sprintf("%s/scan/%s/%s.eml",
+                   spool_directory, message_id, message_id);
+  ret = malware_internal(listptr, scan_filename, FALSE);
+  if (ret == DEFER) av_failed = TRUE;
+
+  return ret;
+}
+
+
+/*************************************************
+*          Scan a file for malware               *
+*************************************************/
+
+/* This is a test wrapper for scanning an email, which is not used in
+normal processing.  Scan any file, using the Exim scanning interface.
+This function tampers with various global variables so is unsafe to use
+in any other context.
+
+Arguments:
+  eml_filename  a file holding the message to be scanned
+
+Returns:        Exim message processing code (OK, FAIL, DEFER, ...)
+                where true means malware was found (condition applies)
+*/
+int
+malware_in_file(uschar *eml_filename)
+{
+  uschar *scan_options[2];
+  uschar message_id_buf[64];
+  int ret;
+
+  scan_options[0] = US"*";
+  scan_options[1] = NULL;
+
+  /* spool_mbox() assumes various parameters exist, when creating
+  the relevant directory and the email within */
+  (void) string_format(message_id_buf, sizeof(message_id_buf),
+      "dummy-%d", vaguely_random_number(INT_MAX));
+  message_id = message_id_buf;
+  sender_address = US"malware-sender@example.net";
+  return_path = US"";
+  recipients_list = NULL;
+  receive_add_recipient(US"malware-victim@example.net", -1);
+  enable_dollar_recipients = TRUE;
+
+  ret = malware_internal(scan_options, eml_filename, TRUE);
+
+  Ustrncpy(spooled_message_id, message_id, sizeof(spooled_message_id));
+  spool_mbox_ok = 1;
+  /* don't set no_mbox_unspool; at present, there's no way for it to become
+  set, but if that changes, then it should apply to these tests too */
+  unspool_mbox();
+
+  /* silence static analysis tools */
+  message_id = NULL;
+
+  return ret;
+}
+
+
+static inline int
+malware_errlog_defer(const uschar * str)
+{
+  log_write(0, LOG_MAIN|LOG_PANIC, "malware acl condition: %s", str);
+  return DEFER;
+}
+
+static int
+m_errlog_defer(struct scan * scanent, const uschar * str)
+{
+  return malware_errlog_defer(string_sprintf("%s: %s", scanent->name, str));
+}
+static int
+m_errlog_defer_3(struct scan * scanent, const uschar * str,
+       int fd_to_close)
+{
+  (void) close(fd_to_close);
+  return m_errlog_defer(scanent, str);
+}
+
+/*************************************************/
+
+/* Only used by the Clamav code, which is working from a list of servers and
+uses the returned in_addr to get a second connection to the same system.
+*/
+static inline int
+m_tcpsocket(const uschar * hostname, unsigned int port,
+       host_item * host, uschar ** errstr)
+{
+  return ip_connectedsocket(SOCK_STREAM, hostname, port, port, 5, host, errstr);
+}
+
+static int
+m_tcpsocket_fromdef(const uschar * hostport, uschar ** errstr)
+{
+  int scan;
+  uschar hostname[256];
+  unsigned int portlow, porthigh;
+
+  /* extract host and port part */
+  scan = sscanf(CS hostport, "%255s %u-%u", hostname, &portlow, &porthigh);
+  if ( scan != 3 ) {
+    if ( scan != 2 ) {
+      *errstr = string_sprintf("invalid socket '%s'", hostport);
+      return -1;
+    }
+    porthigh = portlow;
+  }
+
+  return ip_connectedsocket(SOCK_STREAM, hostname, portlow, porthigh,
+                           5, NULL, errstr);
+}
+
+static int
+m_unixsocket(const uschar * path, uschar ** errstr)
+{
+  int sock;
+  struct sockaddr_un server;
+
+  if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
+    *errstr = US"can't open UNIX socket.";
+    return -1;
+  }
+
+  server.sun_family = AF_UNIX;
+  Ustrncpy(server.sun_path, path, sizeof(server.sun_path)-1);
+  server.sun_path[sizeof(server.sun_path)-1] = '\0';
+  if (connect(sock, (struct sockaddr *) &server, sizeof(server)) < 0) {
+    int err = errno;
+    (void)close(sock);
+    *errstr =  string_sprintf("unable to connect to UNIX socket (%s): %s",
+                 path, strerror(err));
+    return -1;
+    }
+  return sock;
+}
+
+static inline int
+m_streamsocket(const uschar * spec, uschar ** errstr)
+{
+  return *spec == '/'
+    ? m_unixsocket(spec, errstr) : m_tcpsocket_fromdef(spec, errstr);
+}
+
+static int
+m_sock_send(int sock, uschar * buf, int cnt, uschar ** errstr)
+{
+  if (send(sock, buf, cnt, 0) < 0) {
+    int err = errno;
+    (void)close(sock);
+    *errstr = string_sprintf("unable to send to socket (%s): %s",
+          buf, strerror(err));
+    return -1;
+    }
+  return sock;
+}
+
+static const pcre *
+m_pcre_compile(const uschar * re, uschar ** errstr)
+{
+  const uschar * rerror;
+  int roffset;
+  const pcre * cre;
+
+  cre = pcre_compile(CS re, PCRE_COPT, (const char **)&rerror, &roffset, NULL);
+  if (!cre)
+    *errstr= string_sprintf("regular expression error in '%s': %s at offset %d",
+       re, rerror, roffset);
+  return cre;
+}
+
+uschar *
+m_pcre_exec(const pcre * cre, uschar * text)
+{
+  int ovector[10*3];
+  int i = pcre_exec(cre, NULL, CS text, Ustrlen(text), 0, 0,
+               ovector, nelements(ovector));
+  uschar * substr = NULL;
+  if (i >= 2)                          /* Got it */
+    pcre_get_substring(CS text, ovector, i, 1, (const char **) &substr);
+  return substr;
+}
+
+static const pcre *
+m_pcre_nextinlist(uschar ** list, int * sep, char * listerr, uschar ** errstr)
+{
+  const uschar * list_ele;
+  const pcre * cre = NULL;
+
+  if (!(list_ele = string_nextinlist(list, sep, NULL, 0)))
+    *errstr = US listerr;
+  else
+    cre = m_pcre_compile(CUS list_ele, errstr);
+  return cre;
+}
+
+/*************************************************
+*          Scan content for malware              *
+*************************************************/
+
+/* This is an internal interface for scanning an email; the normal interface
+is via malware(), or there's malware_in_file() used for testing/debugging.
+
+Arguments:
+  listptr       the list of options to the "malware = ..." ACL condition
+  eml_filename  the file holding the email to be scanned
+  faking        whether or not we're faking this up for the -bmalware test
+
+Returns:        Exim message processing code (OK, FAIL, DEFER, ...)
+                where true means malware was found (condition applies)
+*/
+static int
+malware_internal(uschar **listptr, uschar *eml_filename, BOOL faking)
+{
+  int sep = 0;
+  uschar *list = *listptr;
+  uschar *av_scanner_work = av_scanner;
+  uschar *scanner_name;
+  uschar *malware_regex;
+  uschar malware_regex_default[] = ".+";
+  unsigned long mbox_size;
+  FILE *mbox_file;
+  const pcre *re;
+  uschar * errstr;
+  struct scan * scanent;
+  const uschar * scanner_options;
+  int sock = -1;
+
+  /* make sure the eml mbox file is spooled up */
+  if (!(mbox_file = spool_mbox(&mbox_size, faking ? eml_filename : NULL)))
+    return malware_errlog_defer(US"error while creating mbox spool file");
+
+  /* none of our current scanners need the mbox
+     file as a stream, so we can close it right away */
+  (void)fclose(mbox_file);
+
+  /* extract the malware regex to match against from the option list */
+  if (!(malware_regex = string_nextinlist(&list, &sep, NULL, 0)))
+    return FAIL;               /* empty means "don't match anything" */
+
+  /* parse 1st option */
+    if ( (strcmpic(malware_regex,US"false") == 0) ||
+       (Ustrcmp(malware_regex,"0") == 0) )
+    return FAIL;               /* explicitly no matching */
+
+  /* special cases (match anything except empty) */
+  if ( (strcmpic(malware_regex,US"true") == 0) ||
+       (Ustrcmp(malware_regex,"*") == 0) ||
+       (Ustrcmp(malware_regex,"1") == 0) )
+    malware_regex = malware_regex_default;
+
+  /* Reset sep that is set by previous string_nextinlist() call */
+  sep = 0;
+
+  /* compile the regex, see if it works */
+  if (!(re = m_pcre_compile(malware_regex, &errstr)))
+    return malware_errlog_defer(errstr);
+
+  /* if av_scanner starts with a dollar, expand it first */
+  if (*av_scanner == '$') {
+    if (!(av_scanner_work = expand_string(av_scanner)))
+      return malware_errlog_defer(
+           string_sprintf("av_scanner starts with $, but expansion failed: %s",
+          expand_string_message));
+
+    debug_printf("Expanded av_scanner global: %s\n", av_scanner_work);
+    /* disable result caching in this case */
+    malware_name = NULL;
+    malware_ok = FALSE;
+  }
+
+  /* Do not scan twice (unless av_scanner is dynamic). */
+  if (!malware_ok) {
+
+    /* find the scanner type from the av_scanner option */
+    if (!(scanner_name = string_nextinlist(&av_scanner_work, &sep, NULL, 0)))
+      return malware_errlog_defer(US"av_scanner configuration variable is empty");
+
+    for (scanent = m_scans; ; scanent++) {
+      if (!scanent->name)
+       return malware_errlog_defer(string_sprintf("unknown scanner type '%s'",
+         scanner_name));
+      if (strcmpic(scanner_name, US scanent->name) != 0)
+       continue;
+      if (!(scanner_options = string_nextinlist(&av_scanner_work, &sep, NULL, 0)))
+       scanner_options = scanent->options_default;
+      if (scanent->conn == MC_NONE)
+       break;
+      switch(scanent->conn)
+      {
+      case MC_TCP:  sock = m_tcpsocket_fromdef(scanner_options, &errstr); break;
+      case MC_UNIX: sock = m_unixsocket(scanner_options, &errstr);       break;
+      case MC_STRM: sock = m_streamsocket(scanner_options, &errstr);     break;
+      default: /* compiler quietening */ break;
+      }
+      if (sock < 0)
+       return m_errlog_defer(scanent, errstr);
+      break;
+    }
+    DEBUG(D_lookup) debug_printf("Malware scan: %s\n", scanner_name);
+
+    switch (scanent->scancode) {
+    case M_FPROTD: /* "f-protd" scanner type -------------------------------- */
+      {
+       uschar *fp_scan_option;
+       unsigned int detected=0, par_count=0;
+       uschar * scanrequest;
+       uschar buf[32768], *strhelper, *strhelper2;
+       uschar * malware_name_internal = NULL;
+
+       DEBUG(D_acl) debug_printf("Malware scan: issuing %s GET\n", scanner_name);
+       scanrequest = string_sprintf("GET %s", eml_filename);
+
+       while ((fp_scan_option = string_nextinlist(&av_scanner_work, &sep,
+                             NULL, 0))) {
+         scanrequest = string_sprintf("%s%s%s", scanrequest,
+                                   par_count ? "%20" : "?", fp_scan_option);
+         par_count++;
+       }
+       scanrequest = string_sprintf("%s HTTP/1.0\r\n\r\n", scanrequest);
+
+       /* send scan request */
+       if (m_sock_send(sock, scanrequest, Ustrlen(scanrequest)+1, &errstr) < 0)
+         return m_errlog_defer(scanent, errstr);
+
+       /* We get a lot of empty lines, so we need this hack to check for any data at all */
+       while( recv(sock, buf, 1, MSG_PEEK) > 0 ) {
+         if ( recv_line(sock, buf, sizeof(buf)) > 0) {
+           if ( Ustrstr(buf, US"<detected type=\"") != NULL )
+             detected = 1;
+           else if ( detected && (strhelper = Ustrstr(buf, US"<name>")) ) {
+             if ((strhelper2 = Ustrstr(buf, US"</name>")) != NULL) {
+               *strhelper2 = '\0';
+               malware_name_internal = string_copy(strhelper+6);
+             }
+           } else if ( Ustrstr(buf, US"<summary code=\"") )
+               malware_name = Ustrstr(buf, US"<summary code=\"11\">")
+                 ? malware_name_internal : NULL;
+         }
+       }
+       break;
+      }        /* f-protd */
+
+    case M_DRWEB: /* "drweb" scanner type ----------------------------------- */
+    /* v0.1 - added support for tcp sockets          */
+    /* v0.0 - initial release -- support for unix sockets      */
+      {
+       int result;
+       off_t fsize;
+       unsigned int fsize_uint;
+       uschar * tmpbuf, *drweb_fbuf;
+       int drweb_rc, drweb_cmd, drweb_flags = 0x0000, drweb_fd,
+           drweb_vnum, drweb_slen, drweb_fin = 0x0000;
+       unsigned long bread;
+       const pcre *drweb_re;
+
+       /* prepare variables */
+       drweb_cmd = htonl(DRWEBD_SCAN_CMD);
+       drweb_flags = htonl(DRWEBD_RETURN_VIRUSES | DRWEBD_IS_MAIL);
+
+       if (*scanner_options != '/') {
+
+         /* calc file size */
+         if ((drweb_fd = open(CS eml_filename, O_RDONLY)) == -1)
+           return m_errlog_defer_3(scanent,
+             string_sprintf("can't open spool file %s: %s",
+               eml_filename, strerror(errno)),
+             sock);
+
+         if ((fsize = lseek(drweb_fd, 0, SEEK_END)) == -1) {
+           int err = errno;
+           (void)close(drweb_fd);
+           return m_errlog_defer_3(scanent,
+             string_sprintf("can't seek spool file %s: %s",
+               eml_filename, strerror(err)),
+             sock);
+         }
+         fsize_uint = (unsigned int) fsize;
+         if ((off_t)fsize_uint != fsize) {
+           (void)close(drweb_fd);
+           return m_errlog_defer_3(scanent,
+             string_sprintf("seeking spool file %s, size overflow",
+               eml_filename),
+             sock);
+         }
+         drweb_slen = htonl(fsize);
+         lseek(drweb_fd, 0, SEEK_SET);
+
+         DEBUG(D_acl) debug_printf("Malware scan: issuing %s remote scan [%s]\n",
+             scanner_name, scanner_options);
+
+         /* send scan request */
+         if ((send(sock, &drweb_cmd, sizeof(drweb_cmd), 0) < 0) ||
+             (send(sock, &drweb_flags, sizeof(drweb_flags), 0) < 0) ||
+             (send(sock, &drweb_fin, sizeof(drweb_fin), 0) < 0) ||
+             (send(sock, &drweb_slen, sizeof(drweb_slen), 0) < 0)) {
+           (void)close(drweb_fd);
+           return m_errlog_defer_3(scanent,
+             string_sprintf("unable to send commands to socket (%s)", scanner_options),
+             sock);
+         }
+
+         if (!(drweb_fbuf = (uschar *) malloc (fsize_uint))) {
+           (void)close(drweb_fd);
+           return m_errlog_defer_3(scanent,
+             string_sprintf("unable to allocate memory %u for file (%s)",
+               fsize_uint, eml_filename),
+             sock);
+         }
+
+         if ((result = read (drweb_fd, drweb_fbuf, fsize)) == -1) {
+           int err = errno;
+           (void)close(drweb_fd);
+           free(drweb_fbuf);
+           return m_errlog_defer_3(scanent,
+             string_sprintf("can't read spool file %s: %s",
+               eml_filename, strerror(err)),
+             sock);
+         }
+         (void)close(drweb_fd);
+
+         /* send file body to socket */
+         if (send(sock, drweb_fbuf, fsize, 0) < 0) {
+           free(drweb_fbuf);
+           return m_errlog_defer_3(scanent,
+             string_sprintf("unable to send file body to socket (%s)", scanner_options),
+           sock);
+         }
+         (void)close(drweb_fd);
+
+       } else {
+
+         drweb_slen = htonl(Ustrlen(eml_filename));
+
+         DEBUG(D_acl) debug_printf("Malware scan: issuing %s local scan [%s]\n",
+             scanner_name, scanner_options);
+
+         /* send scan request */
+         if ((send(sock, &drweb_cmd, sizeof(drweb_cmd), 0) < 0) ||
+             (send(sock, &drweb_flags, sizeof(drweb_flags), 0) < 0) ||
+             (send(sock, &drweb_slen, sizeof(drweb_slen), 0) < 0) ||
+             (send(sock, eml_filename, Ustrlen(eml_filename), 0) < 0) ||
+             (send(sock, &drweb_fin, sizeof(drweb_fin), 0) < 0))
+           return m_errlog_defer_3(scanent,
+             string_sprintf("unable to send commands to socket (%s)", scanner_options),
+             sock);
+       }
+
+       /* wait for result */
+       if ((bread = recv(sock, &drweb_rc, sizeof(drweb_rc), 0) != sizeof(drweb_rc)))
+         return m_errlog_defer_3(scanent,
+                     US"unable to read return code", sock);
+       drweb_rc = ntohl(drweb_rc);
+
+       if ((bread = recv(sock, &drweb_vnum, sizeof(drweb_vnum), 0) != sizeof(drweb_vnum)))
+         return m_errlog_defer_3(scanent,
+                             US"unable to read the number of viruses", sock);
+       drweb_vnum = ntohl(drweb_vnum);
+
+       /* "virus(es) found" if virus number is > 0 */
+       if (drweb_vnum) {
+         int i;
+
+         /* setup default virus name */
+         malware_name = US"unknown";
+
+         /* set up match regex */
+         drweb_re = m_pcre_compile(US"infected\\swith\\s*(.+?)$", &errstr);
+
+         /* read and concatenate virus names into one string */
+         for (i=0;i<drweb_vnum;i++)
+         {
+           int size = 0, off = 0, ovector[10*3];
+           /* read the size of report */
+           if ((bread = recv(sock, &drweb_slen, sizeof(drweb_slen), 0) != sizeof(drweb_slen)))
+             return m_errlog_defer_3(scanent,
+                               US"cannot read report size", sock);
+           drweb_slen = ntohl(drweb_slen);
+           tmpbuf = store_get(drweb_slen);
+
+           /* read report body */
+           if ((bread = recv(sock, tmpbuf, drweb_slen, 0)) != drweb_slen)
+             return m_errlog_defer_3(scanent,
+                               US"cannot read report string", sock);
+           tmpbuf[drweb_slen] = '\0';
+
+           /* try matcher on the line, grab substring */
+           result = pcre_exec(drweb_re, NULL, CS tmpbuf, Ustrlen(tmpbuf), 0, 0,
+                                   ovector, nelements(ovector));
+           if (result >= 2) {
+             const char * pre_malware_nb;
+
+             pcre_get_substring(CS tmpbuf, ovector, result, 1, &pre_malware_nb);
+
+             if (i==0) /* the first name we just copy to malware_name */
+               malware_name = string_append(NULL, &size, &off,
+                                           1, pre_malware_nb);
+
+             else      /* concatenate each new virus name to previous */
+               malware_name = string_append(malware_name, &size, &off,
+                                           2, "/", pre_malware_nb);
+
+             pcre_free_substring(pre_malware_nb);
+           }
+         }
+       }
+       else {
+         const char *drweb_s = NULL;
+
+         if (drweb_rc & DERR_READ_ERR) drweb_s = "read error";
+         if (drweb_rc & DERR_NOMEMORY) drweb_s = "no memory";
+         if (drweb_rc & DERR_TIMEOUT)  drweb_s = "timeout";
+         if (drweb_rc & DERR_BAD_CALL) drweb_s = "wrong command";
+         /* retcodes DERR_SYMLINK, DERR_NO_REGFILE, DERR_SKIPPED.
+          * DERR_TOO_BIG, DERR_TOO_COMPRESSED, DERR_SPAM,
+          * DERR_CRC_ERROR, DERR_READSOCKET, DERR_WRITE_ERR
+          * and others are ignored */
+         if (drweb_s)
+           return m_errlog_defer_3(scanent,
+             string_sprintf("drweb daemon retcode 0x%x (%s)", drweb_rc, drweb_s),
+             sock);
+
+         /* no virus found */
+         malware_name = NULL;
+       }
+       break;
+      }        /* drweb */
+
+    case M_AVES: /* "aveserver" scanner type -------------------------------- */
+      {
+       uschar buf[32768];
+       int result;
+
+       /* read aveserver's greeting and see if it is ready (2xx greeting) */
+       recv_line(sock, buf, sizeof(buf));
+
+       if (buf[0] != '2')              /* aveserver is having problems */
+         return m_errlog_defer_3(scanent,
+           string_sprintf("unavailable (Responded: %s).",
+                           ((buf[0] != 0) ? buf : (uschar *)"nothing") ),
+           sock);
+
+       /* prepare our command */
+       (void)string_format(buf, sizeof(buf), "SCAN bPQRSTUW %s\r\n",
+                                                 eml_filename);
+
+       DEBUG(D_acl) debug_printf("Malware scan: issuing %s SCAN\n", scanner_name);
+
+       /* and send it */
+       if (m_sock_send(sock, buf, Ustrlen(buf), &errstr) < 0)
+         return m_errlog_defer(scanent, errstr);
+
+       malware_name = NULL;
+       result = 0;
+       /* read response lines, find malware name and final response */
+       while (recv_line(sock, buf, sizeof(buf)) > 0) {
+         debug_printf("aveserver: %s\n", buf);
+         if (buf[0] == '2')
+           break;
+         if (buf[0] == '5') {          /* aveserver is having problems */
+           result = m_errlog_defer(scanent,
+              string_sprintf("unable to scan file %s (Responded: %s).",
+                              eml_filename, buf));
+           break;
+         } else if (Ustrncmp(buf,"322",3) == 0) {
+           uschar *p = Ustrchr(&buf[4],' ');
+           *p = '\0';
+           malware_name = string_copy(&buf[4]);
+         }
+       }
+
+       /* and send it */
+       if (m_sock_send(sock, US"quit\r\n", 6, &errstr) < 0)
+         return m_errlog_defer(scanent, errstr);
+
+       /* read aveserver's greeting and see if it is ready (2xx greeting) */
+       recv_line(sock, buf, sizeof(buf));
+
+       if (buf[0] != '2')              /* aveserver is having problems */
+         return m_errlog_defer_3(scanent,
+           string_sprintf("unable to quit dialogue (Responded: %s).",
+                         ((buf[0] != 0) ? buf : (uschar *)"nothing") ),
+           sock);
+
+       if (result == DEFER) {
+         (void)close(sock);
+         return DEFER;
+       }
+       break;
+      }        /* aveserver */
+
+    case M_FSEC: /* "fsecure" scanner type ---------------------------------- */
+      {
+       int i, j, bread = 0;
+       uschar * file_name;
+       uschar av_buffer[1024];
+       const pcre * fs_inf;
+       static uschar *cmdopt[] = { US"CONFIGURE\tARCHIVE\t1\n",
+                                       US"CONFIGURE\tTIMEOUT\t0\n",
+                                       US"CONFIGURE\tMAXARCH\t5\n",
+                                       US"CONFIGURE\tMIME\t1\n" };
+
+       malware_name = NULL;
+
+       DEBUG(D_acl) debug_printf("Malware scan: issuing %s scan [%s]\n",
+           scanner_name, scanner_options);
+
+       /* pass options */
+       memset(av_buffer, 0, sizeof(av_buffer));
+       for (i=0; i != nelements(cmdopt); i++) {
+
+         if (m_sock_send(sock, cmdopt[i], Ustrlen(cmdopt[i]), &errstr) < 0)
+           return m_errlog_defer(scanent, errstr);
+
+         bread = ip_recv(sock, av_buffer, sizeof(av_buffer), MALWARE_TIMEOUT);
+         if (bread >0) av_buffer[bread]='\0';
+         if (bread < 0)
+           return m_errlog_defer_3(scanent,
+             string_sprintf("unable to read answer %d (%s)", i, strerror(errno)),
+             sock);
+         for (j=0;j<bread;j++)
+           if((av_buffer[j]=='\r')||(av_buffer[j]=='\n'))
+             av_buffer[j] ='@';
+       }
+
+       /* pass the mailfile to fsecure */
+       file_name = string_sprintf("SCAN\t%s\n", eml_filename);
+
+       if (m_sock_send(sock, file_name, Ustrlen(file_name), &errstr) < 0)
+         return m_errlog_defer(scanent, errstr);
+
+       /* set up match */
+       /* todo also SUSPICION\t */
+       fs_inf = m_pcre_compile(US"\\S{0,5}INFECTED\\t[^\\t]*\\t([^\\t]+)\\t\\S*$", &errstr);
+
+       /* read report, linewise */
+       do {
+         i = 0;
+         memset(av_buffer, 0, sizeof(av_buffer));
+         do {
+           if ((bread= ip_recv(sock, &av_buffer[i], 1, MALWARE_TIMEOUT)) < 0)
+             return m_errlog_defer_3(scanent,
+               string_sprintf("unable to read result (%s)", strerror(errno)),
+               sock);
+         } while (++i < sizeof(av_buffer)-1  &&  av_buffer[i-1] != '\n');
+         av_buffer[i-1] = '\0';
+
+         /* Really search for virus again? */
+         if (malware_name == NULL)
+           /* try matcher on the line, grab substring */
+           malware_name = m_pcre_exec(fs_inf, av_buffer);
+       }
+       while (Ustrstr(av_buffer, "OK\tScan ok.") == NULL);
+       break;
+      }        /* fsecure */
+
+    case M_KAVD: /* "kavdaemon" scanner type -------------------------------- */
+      {
+       time_t t;
+       uschar tmpbuf[1024];
+       uschar * scanrequest;
+       int kav_rc;
+       unsigned long kav_reportlen, bread;
+       const pcre *kav_re;
+       uschar *p;
+
+       /* get current date and time, build scan request */
+       time(&t);
+       /* pdp note: before the eml_filename parameter, this scanned the
+       directory; not finding documentation, so we'll strip off the directory.
+       The side-effect is that the test framework scanning may end up in
+       scanning more than was requested, but for the normal interface, this is
+       fine. */
+
+       strftime(CS tmpbuf, sizeof(tmpbuf), "%d %b %H:%M:%S", localtime(&t));
+       scanrequest = string_sprintf("<0>%s:%s", CS tmpbuf, eml_filename);
+       p = Ustrrchr(scanrequest, '/');
+       if (p)
+         *p = '\0';
+
+       DEBUG(D_acl) debug_printf("Malware scan: issuing %s scan [%s]\n",
+           scanner_name, scanner_options);
+
+       /* send scan request */
+       if (m_sock_send(sock, scanrequest, Ustrlen(scanrequest)+1, &errstr) < 0)
+         return m_errlog_defer(scanent, errstr);
+
+       /* wait for result */
+       if ((bread = recv(sock, tmpbuf, 2, 0) != 2))
+         return m_errlog_defer_3(scanent,
+                             US"unable to read 2 bytes from socket.", sock);
+
+       /* get errorcode from one nibble */
+       kav_rc = tmpbuf[ test_byte_order()==LITTLE_MY_ENDIAN ? 0 : 1 ] & 0x0F;
+       switch(kav_rc)
+       {
+       case 5: case 6: /* improper kavdaemon configuration */
+         return m_errlog_defer_3(scanent,
+                 US"please reconfigure kavdaemon to NOT disinfect or remove infected files.",
+                 sock);
+       case 1:
+         return m_errlog_defer_3(scanent,
+                 US"reported 'scanning not completed' (code 1).", sock);
+       case 7:
+         return m_errlog_defer_3(scanent,
+                 US"reported 'kavdaemon damaged' (code 7).", sock);
+       }
+
+       /* code 8 is not handled, since it is ambigous. It appears mostly on
+       bounces where part of a file has been cut off */
+
+       /* "virus found" return codes (2-4) */
+       if ((kav_rc > 1) && (kav_rc < 5)) {
+         int report_flag = 0;
+
+         /* setup default virus name */
+         malware_name = US"unknown";
+
+         report_flag = tmpbuf[ test_byte_order() == LITTLE_MY_ENDIAN ? 1 : 0 ];
+
+         /* read the report, if available */
+         if( report_flag == 1 ) {
+           /* read report size */
+           if ((bread = recv(sock, &kav_reportlen, 4, 0)) != 4)
+             return m_errlog_defer_3(scanent,
+                   US"cannot read report size", sock);
+
+           /* it's possible that avp returns av_buffer[1] == 1 but the
+           reportsize is 0 (!?) */
+           if (kav_reportlen > 0) {
+             /* set up match regex, depends on retcode */
+             kav_re = m_pcre_compile( kav_rc == 3
+                                      ? US"suspicion:\\s*(.+?)\\s*$"
+                                      : US"infected:\\s*(.+?)\\s*$",
+                                      &errstr );
+
+             /* read report, linewise */
+             while (kav_reportlen > 0) {
+               bread = 0;
+               while ( recv(sock, &tmpbuf[bread], 1, 0) == 1 ) {
+                 kav_reportlen--;
+                 if ( (tmpbuf[bread] == '\n') || (bread > 1021) ) break;
+                 bread++;
+               }
+               bread++;
+               tmpbuf[bread] = '\0';
+
+               /* try matcher on the line, grab substring */
+               if ((malware_name = m_pcre_exec(kav_re, tmpbuf)))
+                 break;
+             }
+           }
+         }
+       }
+       else /* no virus found */
+         malware_name = NULL;
+
+       break;
+      }
+
+    case M_CMDL: /* "cmdline" scanner type ---------------------------------- */
+      {
+       const uschar *cmdline_scanner = scanner_options;
+       const pcre *cmdline_trigger_re;
+       const pcre *cmdline_regex_re;
+       uschar * file_name;
+       uschar * commandline;
+       void (*eximsigchld)(int);
+       void (*eximsigpipe)(int);
+       FILE *scanner_out = NULL;
+       FILE *scanner_record = NULL;
+       uschar linebuffer[32767];
+       int trigger = 0;
+       uschar *p;
+
+       if (!cmdline_scanner)
+         return m_errlog_defer(scanent, errstr);
+
+       /* find scanner output trigger */
+       cmdline_trigger_re = m_pcre_nextinlist(&av_scanner_work, &sep,
+                                 "missing trigger specification", &errstr);
+       if (!cmdline_trigger_re)
+         return m_errlog_defer(scanent, errstr);
+
+       /* find scanner name regex */
+       cmdline_regex_re = m_pcre_nextinlist(&av_scanner_work, &sep,
+                           "missing virus name regex specification", &errstr);
+       if (!cmdline_regex_re)
+         return m_errlog_defer(scanent, errstr);
+
+       /* prepare scanner call; despite the naming, file_name holds a directory
+       name which is documented as the value given to %s. */
+
+       file_name = string_copy(eml_filename);
+       p = Ustrrchr(file_name, '/');
+       if (p)
+         *p = '\0';
+       commandline = string_sprintf(CS cmdline_scanner, file_name);
+
+       /* redirect STDERR too */
+       commandline = string_sprintf("%s 2>&1", commandline);
+
+       DEBUG(D_acl) debug_printf("Malware scan: issuing %s scan [%s]\n", scanner_name, commandline);
+
+       /* store exims signal handlers */
+       eximsigchld = signal(SIGCHLD,SIG_DFL);
+       eximsigpipe = signal(SIGPIPE,SIG_DFL);
+
+       if (!(scanner_out = popen(CS commandline,"r"))) {
+         int err = errno;
+         signal(SIGCHLD,eximsigchld); signal(SIGPIPE,eximsigpipe);
+         return m_errlog_defer(scanent,
+           string_sprintf("call (%s) failed: %s.", commandline, strerror(err)));
+       }
+
+       file_name = string_sprintf("%s/scan/%s/%s_scanner_output",
+                                 spool_directory, message_id, message_id);
+       scanner_record = modefopen(file_name, "wb", SPOOL_MODE);
+
+       if (scanner_record == NULL) {
+         int err = errno;
+         (void) pclose(scanner_out);
+         signal(SIGCHLD,eximsigchld); signal(SIGPIPE,eximsigpipe);
+         return m_errlog_defer(scanent,
+           string_sprintf("opening scanner output file (%s) failed: %s.",
+             file_name, strerror(err)));
+       }
+
+       /* look for trigger while recording output */
+       while(fgets(CS linebuffer, sizeof(linebuffer), scanner_out)) {
+         if ( Ustrlen(linebuffer) > fwrite(linebuffer, 1, Ustrlen(linebuffer), scanner_record) ) {
+           /* short write */
+           (void) pclose(scanner_out);
+           signal(SIGCHLD,eximsigchld); signal(SIGPIPE,eximsigpipe);
+           return m_errlog_defer(scanent,
+             string_sprintf("short write on scanner output file (%s).", file_name));
+         }
+         /* try trigger match */
+         if (!trigger && regex_match_and_setup(cmdline_trigger_re, linebuffer, 0, -1))
+           trigger = 1;
+       }
+
+       (void)fclose(scanner_record);
+       sep = pclose(scanner_out);
+       signal(SIGCHLD,eximsigchld); signal(SIGPIPE,eximsigpipe);
+       if (sep != 0)
+           return m_errlog_defer(scanent,
+               sep == -1
+               ? string_sprintf("running scanner failed: %s", strerror(sep))
+               : string_sprintf("scanner returned error code: %d", sep));
+
+       if (trigger) {
+         uschar * s;
+         /* setup default virus name */
+         malware_name = US"unknown";
+
+         /* re-open the scanner output file, look for name match */
+         scanner_record = fopen(CS file_name, "rb");
+         while(fgets(CS linebuffer, sizeof(linebuffer), scanner_record)) {
+           /* try match */
+           if ((s = m_pcre_exec(cmdline_regex_re, linebuffer)))
+             malware_name = s;
+         }
+         (void)fclose(scanner_record);
+       }
+       else /* no virus found */
+         malware_name = NULL;
+       break;
+      }        /* cmdline */
+
+    case M_SOPHIE: /* "sophie" scanner type --------------------------------- */
+      {
+       int bread = 0;
+       uschar *p;
+       uschar * file_name;
+       uschar av_buffer[1024];
+
+       /* pass the scan directory to sophie */
+       file_name = string_copy(eml_filename);
+       if ((p = Ustrrchr(file_name, '/')))
+         *p = '\0';
+
+       DEBUG(D_acl) debug_printf("Malware scan: issuing %s scan [%s]\n",
+           scanner_name, scanner_options);
+
+       if (  write(sock, file_name, Ustrlen(file_name)) < 0
+          || write(sock, "\n", 1) != 1
+          )
+         return m_errlog_defer_3(scanent,
+           string_sprintf("unable to write to UNIX socket (%s)", scanner_options),
+           sock);
+
+       /* wait for result */
+       memset(av_buffer, 0, sizeof(av_buffer));
+       if ((!(bread = ip_recv(sock, av_buffer, sizeof(av_buffer), MALWARE_TIMEOUT)) > 0))
+         return m_errlog_defer_3(scanent,
+           string_sprintf("unable to read from UNIX socket (%s)", scanner_options),
+           sock);
+
+       /* infected ? */
+       if (av_buffer[0] == '1') {
+         uschar * s = Ustrchr(av_buffer, '\n');
+         if (s)
+           *s = '\0';
+         malware_name = string_copy(&av_buffer[2]);
+       }
+       else if (!strncmp(CS av_buffer, "-1", 2))
+         return m_errlog_defer_3(scanent, US"scanner reported error", sock);
+       else /* all ok, no virus */
+         malware_name = NULL;
+
+       break;
+      }
+
+    case M_CLAMD: /* "clamd" scanner type ----------------------------------- */
+      {
+      /* This code was originally contributed by David Saez */
+      /* There are three scanning methods available to us:
+       *  (1) Use the SCAN command, pointing to a file in the filesystem
+       *  (2) Use the STREAM command, send the data on a separate port
+       *  (3) Use the zINSTREAM command, send the data inline
+       * The zINSTREAM command was introduced with ClamAV 0.95, which marked
+       * STREAM deprecated; see: http://wiki.clamav.net/bin/view/Main/UpgradeNotes095
+       * In Exim, we use SCAN if using a Unix-domain socket or explicitly told that
+       * the TCP-connected daemon is actually local; otherwise we use zINSTREAM unless
+       * WITH_OLD_CLAMAV_STREAM is defined.
+       * See Exim bug 926 for details.  */
+
+       uschar *p, *vname, *result_tag, *response_end;
+       int bread=0;
+       uschar * file_name;
+       uschar av_buffer[1024];
+       uschar *hostname = US"";
+       host_item connhost;
+       uschar *clamav_fbuf;
+       int clam_fd, result;
+       off_t fsize;
+       unsigned int fsize_uint;
+       BOOL use_scan_command = FALSE;
+       clamd_address_container * clamd_address_vector[MAX_CLAMD_SERVERS];
+       int current_server;
+       int num_servers = 0;
+  #ifdef WITH_OLD_CLAMAV_STREAM
+       unsigned int port;
+       uschar av_buffer2[1024];
+       int sockData;
+  #else
+       uint32_t send_size, send_final_zeroblock;
+  #endif
+
+       if (*scanner_options == '/')
+         /* Local file; so we def want to use_scan_command and don't want to try
+          * passing IP/port combinations */
+         use_scan_command = TRUE;
+       else {
+         const uschar *address = scanner_options;
+         uschar address_buffer[MAX_CLAMD_ADDRESS_LENGTH + 20];
+
+         /* Go through the rest of the list of host/port and construct an array
+          * of servers to try. The first one is the bit we just passed from
+          * scanner_options so process that first and then scan the remainder of
+          * the address buffer */
+         do {
+           clamd_address_container *this_clamd;
+
+           /* The 'local' option means use the SCAN command over the network
+            * socket (ie common file storage in use) */
+           if (strcmpic(address,US"local") == 0) {
+             use_scan_command = TRUE;
+             continue;
+           }
+
+           /* XXX: If unsuccessful we should free this memory */
+           this_clamd =
+               (clamd_address_container *)store_get(sizeof(clamd_address_container));
+
+           /* extract host and port part */
+           if( sscanf(CS address, "%" MAX_CLAMD_ADDRESS_LENGTH_S "s %u",
+                  this_clamd->tcp_addr, &(this_clamd->tcp_port)) != 2 ) {
+             (void) m_errlog_defer(scanent,
+                         string_sprintf("invalid address '%s'", address));
+             continue;
+           }
+
+           clamd_address_vector[num_servers] = this_clamd;
+           num_servers++;
+           if (num_servers >= MAX_CLAMD_SERVERS) {
+             (void) m_errlog_defer(scanent,
+                   US"More than " MAX_CLAMD_SERVERS_S " clamd servers "
+                   "specified; only using the first " MAX_CLAMD_SERVERS_S );
+             break;
+           }
+         } while ((address = string_nextinlist(&av_scanner_work, &sep,
+                                         address_buffer,
+                                         sizeof(address_buffer))) != NULL);
+
+         /* check if we have at least one server */
+         if (!num_servers)
+           return m_errlog_defer(scanent,
+             US"no useable server addresses in malware configuration option.");
+       }
+
+       /* See the discussion of response formats below to see why we really don't
+       like colons in filenames when passing filenames to ClamAV. */
+       if (use_scan_command && Ustrchr(eml_filename, ':'))
+         return m_errlog_defer(scanent,
+           string_sprintf("local/SCAN mode incompatible with" \
+             " : in path to email filename [%s]", eml_filename));
+
+       /* We have some network servers specified */
+       if (num_servers) {
+
+         /* Confirmed in ClamAV source (0.95.3) that the TCPAddr option of clamd
+          * only supports AF_INET, but we should probably be looking to the
+          * future and rewriting this to be protocol-independent anyway. */
+
+         while ( num_servers > 0 ) {
+           /* Randomly pick a server to start with */
+           current_server = random_number( num_servers );
+
+           debug_printf("trying server name %s, port %u\n",
+                        clamd_address_vector[current_server]->tcp_addr,
+                        clamd_address_vector[current_server]->tcp_port);
+
+           /* Lookup the host. This is to ensure that we connect to the same IP
+            * on both connections (as one host could resolve to multiple ips) */
+           sock= m_tcpsocket(clamd_address_vector[current_server]->tcp_addr,
+                               clamd_address_vector[current_server]->tcp_port,
+                               &connhost, &errstr);
+           if (sock >= 0) {
+             /* Connection successfully established with a server */
+             hostname = clamd_address_vector[current_server]->tcp_addr;
+             break;
+           }
+
+           (void) m_errlog_defer(scanent, errstr);
+
+           /* Remove the server from the list. XXX We should free the memory */
+           num_servers--;
+           int i;
+           for( i = current_server; i < num_servers; i++ )
+             clamd_address_vector[i] = clamd_address_vector[i+1];
+         }
+
+         if ( num_servers == 0 )
+           return m_errlog_defer(scanent, US"all servers failed");
+
+       } else {
+         if ((sock = m_unixsocket(scanner_options, &errstr)) < 0)
+           return m_errlog_defer(scanent, errstr);
+       }
+
+       /* have socket in variable "sock"; command to use is semi-independent of
+        * the socket protocol.  We use SCAN if is local (either Unix/local
+        * domain socket, or explicitly told local) else we stream the data.
+        * How we stream the data depends upon how we were built.  */
+
+       if (!use_scan_command) {
+
+  #ifdef WITH_OLD_CLAMAV_STREAM
+         /* "STREAM\n" command, get back a "PORT <N>\n" response, send data to
+          * that port on a second connection; then in the scan-method-neutral
+          * part, read the response back on the original connection. */
+
+         DEBUG(D_acl) debug_printf("Malware scan: issuing %s old-style remote scan (PORT)\n",
+             scanner_name);
+
+         /* Pass the string to ClamAV (7 = "STREAM\n") */
+         if (m_sock_send(sock, US"STREAM\n", 7, &errstr) < 0)
+           return m_errlog_defer(scanent, errstr);
+
+         memset(av_buffer2, 0, sizeof(av_buffer2));
+         bread = ip_recv(sock, av_buffer2, sizeof(av_buffer2), MALWARE_TIMEOUT);
+
+         if (bread < 0)
+           return m_errlog_defer_3(scanent,
+             string_sprintf("unable to read PORT from socket (%s)",
+                 strerror(errno)),
+             sock);
+
+         if (bread == sizeof(av_buffer2))
+           return m_errlog_defer_3(scanent, "buffer too small", sock);
+
+         if (!(*av_buffer2))
+           return m_errlog_defer_3(scanent, "ClamAV returned null", sock);
+
+         av_buffer2[bread] = '\0';
+         if( sscanf(CS av_buffer2, "PORT %u\n", &port) != 1 )
+           return m_errlog_defer_3(scanent,
+             string_sprintf("Expected port information from clamd, got '%s'",
+               av_buffer2),
+             sock);
+
+         sockData = m_tcpsocket(connhost.address, port, NULL, &errstr);
+         if (sockData < 0)
+           return m_errlog_defer_3(scanent, errstr, sock);
+
+  #define CLOSE_SOCKDATA (void)close(sockData)
+  #else /* WITH_OLD_CLAMAV_STREAM not defined */
+         /* New protocol: "zINSTREAM\n" followed by a sequence of <length><data>
+         chunks, <n> a 4-byte number (network order), terminated by a zero-length
+         chunk. */
+
+         DEBUG(D_acl) debug_printf("Malware scan: issuing %s new-style remote scan (zINSTREAM)\n",
+             scanner_name);
+
+         /* Pass the string to ClamAV (10 = "zINSTREAM\0") */
+         if (send(sock, "zINSTREAM", 10, 0) < 0)
+           return m_errlog_defer_3(scanent,
+             string_sprintf("unable to send zINSTREAM to socket (%s)",
+               strerror(errno)),
+             sock);
+
+  #define CLOSE_SOCKDATA /**/
+  #endif
+
+         /* calc file size */
+         if ((clam_fd = open(CS eml_filename, O_RDONLY)) < 0) {
+           int err = errno;
+           CLOSE_SOCKDATA;
+           return m_errlog_defer_3(scanent,
+             string_sprintf("can't open spool file %s: %s",
+               eml_filename, strerror(err)),
+             sock);
+         }
+         if ((fsize = lseek(clam_fd, 0, SEEK_END)) < 0) {
+           int err = errno;
+           CLOSE_SOCKDATA; (void)close(clam_fd);
+           return m_errlog_defer_3(scanent,
+             string_sprintf("can't seek spool file %s: %s",
+               eml_filename, strerror(err)),
+             sock);
+         }
+         fsize_uint = (unsigned int) fsize;
+         if ((off_t)fsize_uint != fsize) {
+           CLOSE_SOCKDATA; (void)close(clam_fd);
+           return m_errlog_defer_3(scanent,
+             string_sprintf("seeking spool file %s, size overflow",
+               eml_filename),
+             sock);
+         }
+         lseek(clam_fd, 0, SEEK_SET);
+
+         if (!(clamav_fbuf = (uschar *) malloc (fsize_uint))) {
+           CLOSE_SOCKDATA; (void)close(clam_fd);
+           return m_errlog_defer_3(scanent,
+             string_sprintf("unable to allocate memory %u for file (%s)",
+               fsize_uint, eml_filename),
+             sock);
+         }
+
+         if ((result = read(clam_fd, clamav_fbuf, fsize_uint)) < 0) {
+           int err = errno;
+           free(clamav_fbuf); CLOSE_SOCKDATA; (void)close(clam_fd);
+           return m_errlog_defer_3(scanent,
+             string_sprintf("can't read spool file %s: %s",
+               eml_filename, strerror(err)),
+             sock);
+         }
+         (void)close(clam_fd);
+
+         /* send file body to socket */
+  #ifdef WITH_OLD_CLAMAV_STREAM
+         if (send(sockData, clamav_fbuf, fsize_uint, 0) < 0) {
+           free(clamav_fbuf); CLOSE_SOCKDATA;
+           return m_errlog_defer_3(scanent,
+             string_sprintf("unable to send file body to socket (%s:%u)",
+               hostname, port),
+             sock);
+         }
+  #else
+         send_size = htonl(fsize_uint);
+         send_final_zeroblock = 0;
+         if ((send(sock, &send_size, sizeof(send_size), 0) < 0) ||
+             (send(sock, clamav_fbuf, fsize_uint, 0) < 0) ||
+             (send(sock, &send_final_zeroblock, sizeof(send_final_zeroblock), 0) < 0))
+           {
+           free(clamav_fbuf);
+           return m_errlog_defer_3(scanent,
+             string_sprintf("unable to send file body to socket (%s)", hostname),
+             sock);
+           }
+  #endif
+
+         free(clamav_fbuf);
+
+         CLOSE_SOCKDATA;
+  #undef CLOSE_SOCKDATA
+
+       } else { /* use scan command */
+         /* Send a SCAN command pointing to a filename; then in the then in the
+          * scan-method-neutral part, read the response back */
+
+  /* ================================================================= */
+
+         /* Prior to the reworking post-Exim-4.72, this scanned a directory,
+         which dates to when ClamAV needed us to break apart the email into the
+         MIME parts (eg, with the now deprecated demime condition coming first).
+         Some time back, ClamAV gained the ability to deconstruct the emails, so
+         doing this would actually have resulted in the mail attachments being
+         scanned twice, in the broken out files and from the original .eml.
+         Since ClamAV now handles emails (and has for quite some time) we can
+         just use the email file itself. */
+         /* Pass the string to ClamAV (7 = "SCAN \n" + \0) */
+         file_name = string_sprintf("SCAN %s\n", eml_filename);
+
+         DEBUG(D_acl) debug_printf("Malware scan: issuing %s local-path scan [%s]\n",
+             scanner_name, scanner_options);
+
+         if (send(sock, file_name, Ustrlen(file_name), 0) < 0)
+           return m_errlog_defer_3(scanent,
+             string_sprintf("unable to write to socket (%s)", strerror(errno)),
+             sock);
+
+         /* Do not shut down the socket for writing; a user report noted that
+          * clamd 0.70 does not react well to this. */
+       }
+       /* Commands have been sent, no matter which scan method or connection
+        * type we're using; now just read the result, independent of method. */
+
+       /* Read the result */
+       memset(av_buffer, 0, sizeof(av_buffer));
+       bread = ip_recv(sock, av_buffer, sizeof(av_buffer), MALWARE_TIMEOUT);
+       (void)close(sock);
+       sock = -1;
+
+       if (!(bread > 0))
+         return m_errlog_defer(scanent,
+           string_sprintf("unable to read from socket (%s)", strerror(errno)));
+
+       if (bread == sizeof(av_buffer))
+         return m_errlog_defer(scanent, US"buffer too small");
+       /* We're now assured of a NULL at the end of av_buffer */
+
+       /* Check the result. ClamAV returns one of two result formats.
+       In the basic mode, the response is of the form:
+         infected: -> "<filename>: <virusname> FOUND"
+         not-infected: -> "<filename>: OK"
+         error: -> "<filename>: <errcode> ERROR
+       If the ExtendedDetectionInfo option has been turned on, then we get:
+         "<filename>: <virusname>(<virushash>:<virussize>) FOUND"
+       for the infected case.  Compare:
+  /tmp/eicar.com: Eicar-Test-Signature FOUND
+  /tmp/eicar.com: Eicar-Test-Signature(44d88612fea8a8f36de82e1278abb02f:68) FOUND
+
+       In the streaming case, clamd uses the filename "stream" which you should
+       be able to verify with { ktrace clamdscan --stream /tmp/eicar.com }.  (The
+       client app will replace "stream" with the original filename before returning
+       results to stdout, but the trace shows the data).
+
+       We will assume that the pathname passed to clamd from Exim does not contain
+       a colon.  We will have whined loudly above if the eml_filename does (and we're
+       passing a filename to clamd). */
+
+       if (!(*av_buffer))
+         return m_errlog_defer(scanent, US"ClamAV returned null");
+
+       /* strip newline at the end (won't be present for zINSTREAM)
+       (also any trailing whitespace, which shouldn't exist, but we depend upon
+       this below, so double-check) */
+       p = av_buffer + Ustrlen(av_buffer) - 1;
+       if (*p == '\n') *p = '\0';
+
+       DEBUG(D_acl) debug_printf("Malware response: %s\n", av_buffer);
+
+       while (isspace(*--p) && (p > av_buffer))
+         *p = '\0';
+       if (*p) ++p;
+       response_end = p;
+
+       /* colon in returned output? */
+       if((p = Ustrchr(av_buffer,':')) == NULL)
+         return m_errlog_defer(scanent,
+           string_sprintf("ClamAV returned malformed result (missing colon): %s",
+                   av_buffer));
+
+       /* strip filename */
+       while (*p && isspace(*++p)) /**/;
+       vname = p;
+
+       /* It would be bad to encounter a virus with "FOUND" in part of the name,
+       but we should at least be resistant to it. */
+       p = Ustrrchr(vname, ' ');
+       result_tag = p ? p+1 : vname;
+
+       if (Ustrcmp(result_tag, "FOUND") == 0) {
+         /* p should still be the whitespace before the result_tag */
+         while (isspace(*p)) --p;
+         *++p = '\0';
+         /* Strip off the extended information too, which will be in parens
+         after the virus name, with no intervening whitespace. */
+         if (*--p == ')') {
+           /* "(hash:size)", so previous '(' will do; if not found, we have
+           a curious virus name, but not an error. */
+           p = Ustrrchr(vname, '(');
+           if (p)
+             *p = '\0';
+         }
+         malware_name = string_copy(vname);
+         DEBUG(D_acl) debug_printf("Malware found, name \"%s\"\n", malware_name);
+
+       } else if (Ustrcmp(result_tag, "ERROR") == 0)
+         return m_errlog_defer(scanent,
+           string_sprintf("ClamAV returned: %s", av_buffer));
+
+       else if (Ustrcmp(result_tag, "OK") == 0) {
+         /* Everything should be OK */
+         malware_name = NULL;
+         DEBUG(D_acl) debug_printf("Malware not found\n");
+
+       } else
+         return m_errlog_defer(scanent,
+           string_sprintf("unparseable response from ClamAV: {%s}", av_buffer));
+
+       break;
+      } /* clamd */
+
+    case M_SOCK: /* "sock" scanner type ------------------------------------- */
+      /* This code was derived by Martin Poole from the clamd code contributed
+         by David Saez and the cmdline code
+      */
+      {
+       int bread;
+       uschar * commandline;
+       uschar av_buffer[1024];
+       uschar * linebuffer;
+       uschar * sockline_scanner;
+       uschar sockline_scanner_default[] = "%s\n";
+       const pcre *sockline_trig_re;
+       const pcre *sockline_name_re;
+
+       /* find scanner command line */
+       if ((sockline_scanner = string_nextinlist(&av_scanner_work, &sep,
+                                           NULL, 0)))
+       {       /* check for no expansions apart from one %s */
+         char * s = index(CS sockline_scanner, '%');
+         if (s++)
+           if ((*s != 's' && *s != '%') || index(s+1, '%'))
+             return m_errlog_defer_3(scanent,
+                                   US"unsafe sock scanner call spec", sock);
+       }
+       else
+         sockline_scanner = sockline_scanner_default;
+
+       /* find scanner output trigger */
+       sockline_trig_re = m_pcre_nextinlist(&av_scanner_work, &sep,
+                                 "missing trigger specification", &errstr);
+       if (!sockline_trig_re)
+         return m_errlog_defer_3(scanent, errstr, sock);
+
+       /* find virus name regex */
+       sockline_name_re = m_pcre_nextinlist(&av_scanner_work, &sep,
+                           "missing virus name regex specification", &errstr);
+       if (!sockline_name_re)
+         return m_errlog_defer_3(scanent, errstr, sock);
+
+       /* prepare scanner call - security depends on expansions check above */
+       commandline = string_sprintf("%s/scan/%s/%s.eml", spool_directory, message_id, message_id);
+       commandline = string_sprintf( CS sockline_scanner, CS commandline);
+
+
+       /* Pass the command string to the socket */
+       if (m_sock_send(sock, commandline, Ustrlen(commandline), &errstr) < 0)
+         return m_errlog_defer(scanent, errstr);
+
+       /* Read the result */
+       memset(av_buffer, 0, sizeof(av_buffer));
+       bread = read(sock, av_buffer, sizeof(av_buffer));
+
+       if (!(bread > 0))
+         return m_errlog_defer_3(scanent,
+           string_sprintf("unable to read from socket (%s)", strerror(errno)),
+           sock);
+
+       if (bread == sizeof(av_buffer))
+         return m_errlog_defer_3(scanent, US"buffer too small", sock);
+       linebuffer = string_copy(av_buffer);
+
+       /* try trigger match */
+       if (regex_match_and_setup(sockline_trig_re, linebuffer, 0, -1)) {
+         if (!(malware_name = m_pcre_exec(sockline_name_re, av_buffer)))
+           malware_name = US "unknown";
+       }
+       else /* no virus found */
+         malware_name = NULL;
+       break;
+      }
+
+    case M_MKSD: /* "mksd" scanner type ------------------------------------- */
+      {
+       char *mksd_options_end;
+       int mksd_maxproc = 1;  /* default, if no option supplied */
+       int sock;
+       int retval;
+
+       if (scanner_options) {
+         mksd_maxproc = (int)strtol(CS scanner_options, &mksd_options_end, 10);
+         if (  *scanner_options == '\0'
+            || *mksd_options_end != '\0'
+            || mksd_maxproc < 1
+            || mksd_maxproc > 32
+            )
+           return m_errlog_defer(scanent,
+             string_sprintf("invalid option '%s'", scanner_options));
+       }
+
+       if((sock = m_unixsocket(US "/var/run/mksd/socket", &errstr)) < 0)
+         return m_errlog_defer(scanent, errstr);
+
+       malware_name = NULL;
+
+       DEBUG(D_acl) debug_printf("Malware scan: issuing %s scan\n", scanner_name);
+
+       if ((retval = mksd_scan_packed(scanent, sock, eml_filename)) != OK) {
+         close (sock);
+         return retval;
+       }
+       break;
+      }
+    }
+
+    if (sock >= 0)
+      (void) close (sock);
+    malware_ok = TRUE;                 /* set "been here, done that" marker */
+  }
+
+  /* match virus name against pattern (caseless ------->----------v) */
+  if ( malware_name && (regex_match_and_setup(re, malware_name, 0, -1)) ) {
+    DEBUG(D_acl) debug_printf("Matched regex to malware [%s] [%s]\n", malware_regex, malware_name);
+    return OK;
+  }
+  else
+    return FAIL;
+}
+
+
+/* simple wrapper for reading lines from sockets */
+int
+recv_line(int sock, uschar *buffer, int size)
+{
+  uschar *p = buffer;
+
+  memset(buffer,0,size);
+  /* read until \n */
+  while(recv(sock,p,1,0) > -1) {
+    if ((p-buffer) > (size-2)) break;
+    if (*p == '\n') break;
+    if (*p != '\r') p++;
+  }
+  *p = '\0';
+
+  return (p-buffer);
+}
+
+
+/* ============= private routines for the "mksd" scanner type ============== */
+
+#include <sys/uio.h>
+
+static inline int
+mksd_writev (int sock, struct iovec *iov, int iovcnt)
+{
+  int i;
+
+  for (;;) {
+    do
+      i = writev (sock, iov, iovcnt);
+    while ((i < 0) && (errno == EINTR));
+    if (i <= 0) {
+      (void) malware_errlog_defer(US"unable to write to mksd UNIX socket (/var/run/mksd/socket)");
+      return -1;
+    }
+
+    for (;;)
+      if (i >= iov->iov_len) {
+        if (--iovcnt == 0)
+          return 0;
+        i -= iov->iov_len;
+        iov++;
+      } else {
+        iov->iov_len -= i;
+        iov->iov_base = CS iov->iov_base + i;
+        break;
+      }
+  }
+}
+
+static inline int
+mksd_read_lines (int sock, uschar *av_buffer, int av_buffer_size)
+{
+  int offset = 0;
+  int i;
+
+  do {
+    if ((i = recv (sock, av_buffer+offset, av_buffer_size-offset, 0)) <= 0) {
+      (void) malware_errlog_defer(US"unable to read from mksd UNIX socket (/var/run/mksd/socket)");
+      return -1;
+    }
+
+    offset += i;
+    /* offset == av_buffer_size -> buffer full */
+    if (offset == av_buffer_size) {
+      (void) malware_errlog_defer(US"malformed reply received from mksd");
+      return -1;
+    }
+  } while (av_buffer[offset-1] != '\n');
+
+  av_buffer[offset] = '\0';
+  return offset;
+}
+
+static inline int
+mksd_parse_line(struct scan * scanent, char *line)
+{
+  char *p;
+
+  switch (*line) {
+    case 'O': /* OK */
+      return OK;
+
+    case 'E':
+    case 'A': /* ERR */
+      if ((p = strchr (line, '\n')) != NULL)
+        *p = '\0';
+      return m_errlog_defer(scanent,
+       string_sprintf("scanner failed: %s", line));
+
+    default: /* VIR */
+      if ((p = strchr (line, '\n')) != NULL) {
+        *p = '\0';
+        if (((p-line) > 5) && (line[3] == ' '))
+          if (((p = strchr (line+4, ' ')) != NULL) && ((p-line) > 4)) {
+            *p = '\0';
+            malware_name = string_copy(US line+4);
+            return OK;
+          }
+      }
+      return m_errlog_defer(scanent,
+       string_sprintf("malformed reply received: %s", line));
+  }
+}
+
+static int
+mksd_scan_packed(struct scan * scanent, int sock, uschar *scan_filename)
+{
+  struct iovec iov[3];
+  const char *cmd = "MSQ\n";
+  uschar av_buffer[1024];
+
+  iov[0].iov_base = (void *) cmd;
+  iov[0].iov_len = 3;
+  iov[1].iov_base = CS scan_filename;
+  iov[1].iov_len = Ustrlen(scan_filename);
+  iov[2].iov_base = (void *) (cmd + 3);
+  iov[2].iov_len = 1;
+
+  if (mksd_writev (sock, iov, 3) < 0)
+    return DEFER;
+
+  if (mksd_read_lines (sock, av_buffer, sizeof (av_buffer)) < 0)
+    return DEFER;
+
+  return mksd_parse_line (scanent, CS av_buffer);
+}
+
+#endif /*WITH_CONTENT_SCAN*/
+/*
+ * vi: aw ai sw=2
+ */
diff --git a/src/match.c b/src/match.c
new file mode 100644 (file)
index 0000000..97a0982
--- /dev/null
@@ -0,0 +1,1327 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Functions for matching strings */
+
+
+#include "exim.h"
+
+
+/* Argument block for the check_string() function. This is used for general
+strings, domains, and local parts. */
+
+typedef struct check_string_block {
+  uschar *origsubject;               /* caseful; keep these two first, in */
+  uschar *subject;                   /* step with the block below */
+  int    expand_setup;
+  BOOL   use_partial;
+  BOOL   caseless;
+  BOOL   at_is_special;
+} check_string_block;
+
+
+/* Argument block for the check_address() function. This is used for whole
+addresses. */
+
+typedef struct check_address_block {
+  uschar *origaddress;               /* caseful; keep these two first, in */
+  uschar *address;                   /* step with the block above */
+  int    expand_setup;
+  BOOL   caseless;
+} check_address_block;
+
+
+
+/*************************************************
+*           Generalized string match             *
+*************************************************/
+
+/* This function does a single match of a subject against a pattern, and
+optionally sets up the numeric variables according to what it matched. It is
+called from match_isinlist() via match_check_list() when scanning a list, and
+from match_check_string() when testing just a single item. The subject and
+options arguments are passed in a check_string_block so as to make it easier to
+pass them through match_check_list.
+
+The possible types of pattern are:
+
+  . regular expression - starts with ^
+  . tail match - starts with *
+  . lookup - starts with search type
+  . if at_is_special is set in the argument block:
+      @              matches the primary host name
+      @[]            matches a local IP address in brackets
+      @mx_any        matches any domain with an MX to the local host
+      @mx_primary    matches any domain with a primary MX to the local host
+      @mx_secondary  matches any domain with a secondary MX to the local host
+  . literal - anything else
+
+Any of the @mx_xxx options can be followed by "/ignore=<list>" where <list> is
+a list of IP addresses that are to be ignored (typically 127.0.0.1).
+
+Arguments:
+  arg            check_string_block pointer - see below
+  pattern        the pattern to be matched
+  valueptr       if not NULL, and a lookup is done, return the result here
+                   instead of discarding it; else set it to point to NULL
+  error          for error messages (not used in this function; it never
+                   returns ERROR)
+
+Contents of the argument block:
+  origsubject    the subject in its original casing
+  subject        the subject string to be checked, lowercased if caseless
+  expand_setup   if < 0, don't set up any numeric expansion variables;
+                 if = 0, set $0 to whole subject, and either
+                   $1 to what matches * or
+                   $1, $2, ... to r.e. bracketed items
+                 if > 0, don't set $0, but do set either
+                   $n to what matches *, or
+                   $n, $n+1, ... to r.e. bracketed items
+                 (where n = expand_setup)
+  use_partial    if FALSE, override any partial- search types
+  caseless       TRUE for caseless matching where possible
+  at_is_special  enable special handling of items starting with @
+
+Returns:       OK    if matched
+               FAIL  if not matched
+               DEFER if lookup deferred
+*/
+
+static int
+check_string(void *arg, uschar *pattern, uschar **valueptr, uschar **error)
+{
+check_string_block *cb = (check_string_block *)arg;
+int search_type, partial, affixlen, starflags;
+int expand_setup = cb->expand_setup;
+uschar *affix;
+uschar *s;
+uschar *filename = NULL;
+uschar *keyquery, *result, *semicolon;
+void *handle;
+
+error = error;  /* Keep clever compilers from complaining */
+
+if (valueptr != NULL) *valueptr = NULL;  /* For non-lookup matches */
+
+/* For regular expressions, use cb->origsubject rather than cb->subject so that
+it works if the pattern uses (?-i) to turn off case-independence, overriding
+"caseless". */
+
+s = (pattern[0] == '^')? cb->origsubject : cb->subject;
+
+/* If required to set up $0, initialize the data but don't turn on by setting
+expand_nmax until the match is assured. */
+
+expand_nmax = -1;
+if (expand_setup == 0)
+  {
+  expand_nstring[0] = s;
+  expand_nlength[0] = Ustrlen(s);
+  }
+else if (expand_setup > 0) expand_setup--;
+
+/* Regular expression match: compile, match, and set up $ variables if
+required. */
+
+if (pattern[0] == '^')
+  {
+  const pcre *re = regex_must_compile(pattern, cb->caseless, FALSE);
+  return ((expand_setup < 0)?
+           pcre_exec(re, NULL, CS s, Ustrlen(s), 0, PCRE_EOPT, NULL, 0) >= 0
+           :
+           regex_match_and_setup(re, s, 0, expand_setup)
+         )?
+         OK : FAIL;
+  }
+
+/* Tail match */
+
+if (pattern[0] == '*')
+  {
+  BOOL yield;
+  int slen = Ustrlen(s);
+  int patlen;    /* Sun compiler doesn't like non-constant initializer */
+
+  patlen = Ustrlen(++pattern);
+  if (patlen > slen) return FAIL;
+  yield = cb->caseless?
+    (strncmpic(s + slen - patlen, pattern, patlen) == 0) :
+    (Ustrncmp(s + slen - patlen, pattern, patlen) == 0);
+  if (yield && expand_setup >= 0)
+    {
+    expand_nstring[++expand_setup] = s;
+    expand_nlength[expand_setup] = slen - patlen;
+    expand_nmax = expand_setup;
+    }
+  return yield? OK : FAIL;
+  }
+
+/* Match a special item starting with @ if so enabled. On its own, "@" matches
+the primary host name - implement this by changing the pattern. For the other
+cases we have to do some more work. If we don't recognize a special pattern,
+just fall through - the match will fail. */
+
+if (cb->at_is_special && pattern[0] == '@')
+  {
+  if (pattern[1] == 0)
+    {
+    pattern = primary_hostname;
+    goto NOT_AT_SPECIAL;               /* Handle as exact string match */
+    }
+
+  if (Ustrcmp(pattern, "@[]") == 0)
+    {
+    ip_address_item *ip;
+    int slen = Ustrlen(s);
+    if (s[0] != '[' && s[slen-1] != ']') return FAIL;
+    for (ip = host_find_interfaces(); ip != NULL; ip = ip->next)
+      if (Ustrncmp(ip->address, s+1, slen - 2) == 0
+            && ip->address[slen - 2] == 0)
+        return OK;
+    return FAIL;
+    }
+
+  if (strncmpic(pattern, US"@mx_", 4) == 0)
+    {
+    int rc;
+    host_item h;
+    BOOL prim = FALSE;
+    BOOL secy = FALSE;
+    BOOL removed = FALSE;
+    uschar *ss = pattern + 4;
+    uschar *ignore_target_hosts = NULL;
+
+    if (strncmpic(ss, US"any", 3) == 0) ss += 3;
+    else if (strncmpic(ss, US"primary", 7) == 0)
+      {
+      ss += 7;
+      prim = TRUE;
+      }
+    else if (strncmpic(ss, US"secondary", 9) == 0)
+      {
+      ss += 9;
+      secy = TRUE;
+      }
+    else goto NOT_AT_SPECIAL;
+
+    if (strncmpic(ss, US"/ignore=", 8) == 0) ignore_target_hosts = ss + 8;
+      else if (*ss != 0) goto NOT_AT_SPECIAL;
+
+    h.next = NULL;
+    h.name = s;
+    h.address = NULL;
+
+    rc = host_find_bydns(&h,
+      ignore_target_hosts,
+      HOST_FIND_BY_MX,     /* search only for MX, not SRV or A */
+      NULL,                /* service name not relevant */
+      NULL,                /* srv_fail_domains not relevant */
+      NULL,                /* mx_fail_domains not relevant */
+      NULL,                /* no dnssec request XXX ? */
+      NULL,                /* no dnssec require XXX ? */
+      NULL,                /* no feedback FQDN */
+      &removed);           /* feedback if local removed */
+
+    if (rc == HOST_FIND_AGAIN)
+      {
+      search_error_message = string_sprintf("DNS lookup of \"%s\" deferred", s);
+      return DEFER;
+      }
+
+    if (rc == HOST_FOUND_LOCAL && !secy) return OK;
+    if (prim) return FAIL;
+    return removed? OK : FAIL;
+
+    /*** The above line used to be the following line, but this is incorrect,
+    because host_find_bydns() may return HOST_NOT_FOUND if it removed some MX
+    hosts, but the remaining ones were non-existent. All we are interested in
+    is whether or not it removed some hosts.
+
+    return (rc == HOST_FOUND && removed)? OK : FAIL;
+    ***/
+    }
+  }
+
+/* Escape point from code for specials that start with "@" */
+
+NOT_AT_SPECIAL:
+
+/* This is an exact string match if there is no semicolon in the pattern. */
+
+if ((semicolon = Ustrchr(pattern, ';')) == NULL)
+  {
+  BOOL yield = cb->caseless?
+    (strcmpic(s, pattern) == 0) : (Ustrcmp(s, pattern) == 0);
+  if (yield && expand_setup >= 0) expand_nmax = expand_setup;
+  return yield? OK : FAIL;
+  }
+
+/* Otherwise we have a lookup item. The lookup type, including partial, etc. is
+the part of the string preceding the semicolon. */
+
+*semicolon = 0;
+search_type = search_findtype_partial(pattern, &partial, &affix, &affixlen,
+  &starflags);
+*semicolon = ';';
+if (search_type < 0) log_write(0, LOG_MAIN|LOG_PANIC_DIE, "%s",
+  search_error_message);
+
+/* Partial matching is not appropriate for certain lookups (e.g. when looking
+up user@domain for sender rejection). There's a flag to disable it. */
+
+if (!cb->use_partial) partial = -1;
+
+/* Set the parameters for the three different kinds of lookup. */
+
+keyquery = semicolon + 1;
+while (isspace(*keyquery)) keyquery++;
+
+if (mac_islookup(search_type, lookup_absfilequery))
+  {
+  filename = keyquery;
+  while (*keyquery != 0 && !isspace(*keyquery)) keyquery++;
+  filename = string_copyn(filename, keyquery - filename);
+  while (isspace(*keyquery)) keyquery++;
+  }
+
+else if (!mac_islookup(search_type, lookup_querystyle))
+  {
+  filename = keyquery;
+  keyquery = s;
+  }
+
+/* Now do the actual lookup; throw away the data returned unless it was asked
+for; partial matching is all handled inside search_find(). Note that there is
+no search_close() because of the caching arrangements. */
+
+handle = search_open(filename, search_type, 0, NULL, NULL);
+if (handle == NULL) log_write(0, LOG_MAIN|LOG_PANIC_DIE, "%s",
+  search_error_message);
+result = search_find(handle, filename, keyquery, partial, affix, affixlen,
+  starflags, &expand_setup);
+
+if (result == NULL) return search_find_defer? DEFER : FAIL;
+if (valueptr != NULL) *valueptr = result;
+
+expand_nmax = expand_setup;
+return OK;
+}
+
+
+
+/*************************************************
+*      Public interface to check_string()        *
+*************************************************/
+
+/* This function is called from several places where is it most convenient to
+pass the arguments individually. It places them in a check_string_block
+structure, and then calls check_string().
+
+Arguments:
+  s            the subject string to be checked
+  pattern      the pattern to check it against
+  expand_setup expansion setup option (see check_string())
+  use_partial  if FALSE, override any partial- search types
+  caseless     TRUE for caseless matching where possible
+  at_is_special TRUE to recognize @, @[], etc.
+  valueptr     if not NULL, and a file lookup was done, return the result
+                 here instead of discarding it; else set it to point to NULL
+
+Returns:       OK    if matched
+               FAIL  if not matched
+               DEFER if lookup deferred
+*/
+
+int
+match_check_string(uschar *s, uschar *pattern, int expand_setup,
+  BOOL use_partial, BOOL caseless, BOOL at_is_special, uschar **valueptr)
+{
+check_string_block cb;
+cb.origsubject = s;
+cb.subject = caseless? string_copylc(s) : string_copy(s);
+cb.expand_setup = expand_setup;
+cb.use_partial = use_partial;
+cb.caseless = caseless;
+cb.at_is_special = at_is_special;
+return check_string(&cb, pattern, valueptr, NULL);
+}
+
+
+
+/*************************************************
+*       Get key string from check block          *
+*************************************************/
+
+/* When caching the data from a lookup for a named list, we have to save the
+key that was found, because other lookups of different keys on the same list
+may occur. This function has knowledge of the different lookup types, and
+extracts the appropriate key.
+
+Arguments:
+  arg          the check block
+  type         MCL_STRING, MCL_DOMAIN, MCL_HOST, MCL_ADDRESS, or MCL_LOCALPART
+*/
+
+static uschar *
+get_check_key(void *arg, int type)
+{
+switch(type)
+  {
+  case MCL_STRING:
+  case MCL_DOMAIN:
+  case MCL_LOCALPART:
+  return ((check_string_block *)arg)->subject;
+
+  case MCL_HOST:
+  return ((check_host_block *)arg)->host_address;
+
+  case MCL_ADDRESS:
+  return ((check_address_block *)arg)->address;
+  }
+return US"";  /* In practice, should never happen */
+}
+
+
+
+/*************************************************
+*       Scan list and run matching function      *
+*************************************************/
+
+/* This function scans a list of patterns, and runs a matching function for
+each item in the list. It is called from the functions that match domains,
+local parts, hosts, and addresses, because its overall structure is the same in
+all cases. However, the details of each particular match is different, so it
+calls back to a given function do perform an actual match.
+
+We can't quite keep the different types anonymous here because they permit
+different special cases. A pity.
+
+If a list item starts with !, that implies negation if the subject matches the
+rest of the item (ignoring white space after the !). The result when the end of
+the list is reached is FALSE unless the last item on the list is negated, in
+which case it is TRUE. A file name in the list causes its lines to be
+interpolated as if items in the list. An item starting with + is a named
+sublist, obtained by searching the tree pointed to by anchorptr, with possible
+cached match results in cache_bits.
+
+Arguments:
+  listptr      pointer to the pointer to the list
+  sep          separator character for string_nextinlist();
+                 normally zero for a standard list;
+                 sometimes UCHAR_MAX+1 for single items;
+  anchorptr    -> tree of named items, or NULL if no named items
+  cache_ptr    pointer to pointer to cache bits for named items, or
+                 pointer to NULL if not caching; may get set NULL if an
+                 uncacheable named list is encountered
+  func         function to call back to do one test
+  arg          pointer to pass to the function; the string to be matched is
+                 in the structure it points to
+  type         MCL_STRING, MCL_DOMAIN, MCL_HOST, MCL_ADDRESS, or MCL_LOCALPART
+                 these are used for some special handling
+               MCL_NOEXPAND (whose value is greater than any of them) may
+                 be added to any value to suppress expansion of the list
+  name         string to use in debugging info
+  valueptr     where to pass back data from a lookup
+
+Returns:       OK    if matched a non-negated item
+               OK    if hit end of list after a negated item
+               FAIL  if expansion force-failed
+               FAIL  if matched a negated item
+               FAIL  if hit end of list after a non-negated item
+               DEFER if a something deferred or expansion failed
+*/
+
+int
+match_check_list(uschar **listptr, int sep, tree_node **anchorptr,
+  unsigned int **cache_ptr, int (*func)(void *,uschar *,uschar **,uschar **),
+  void *arg, int type, uschar *name, uschar **valueptr)
+{
+int yield = OK;
+unsigned int *original_cache_bits = *cache_ptr;
+BOOL include_unknown = FALSE;
+BOOL ignore_unknown = FALSE;
+BOOL include_defer = FALSE;
+BOOL ignore_defer = FALSE;
+uschar *list;
+uschar *sss;
+uschar *ot = NULL;
+uschar buffer[1024];
+
+/* Save time by not scanning for the option name when we don't need it. */
+
+HDEBUG(D_any)
+  {
+  uschar *listname = readconf_find_option(listptr);
+  if (listname[0] != 0) ot = string_sprintf("%s in %s?", name, listname);
+  }
+
+/* If the list is empty, the answer is no. Skip the debugging output for
+an unnamed list. */
+
+if (*listptr == NULL)
+  {
+  HDEBUG(D_lists)
+    {
+    if (ot != NULL) debug_printf("%s no (option unset)\n", ot);
+    }
+  return FAIL;
+  }
+
+/* Expand the list before we scan it. A forced expansion gives the answer
+"not in list"; other expansion errors cause DEFER to be returned. However,
+if the type value is greater than or equal to than MCL_NOEXPAND, do not expand
+the list. */
+
+if (type >= MCL_NOEXPAND)
+  {
+  list = *listptr;
+  type -= MCL_NOEXPAND;       /* Remove the "no expand" flag */
+  }
+else
+  {
+  /* If we are searching a domain list, and $domain is not set, set it to the
+  subject that is being sought for the duration of the expansion. */
+
+  if (type == MCL_DOMAIN && deliver_domain == NULL)
+    {
+    check_string_block *cb = (check_string_block *)arg;
+    deliver_domain = cb->subject;
+    list = expand_string(*listptr);
+    deliver_domain = NULL;
+    }
+
+  else list = expand_string(*listptr);
+
+  if (list == NULL)
+    {
+    if (expand_string_forcedfail)
+      {
+      HDEBUG(D_lists) debug_printf("expansion of \"%s\" forced failure: "
+        "assume not in this list\n", *listptr);
+      return FAIL;
+      }
+    log_write(0, LOG_MAIN|LOG_PANIC, "failed to expand \"%s\" while checking "
+      "a list: %s", *listptr, expand_string_message);
+    return DEFER;
+    }
+  }
+
+/* For an unnamed list, use the expanded version in comments */
+
+HDEBUG(D_any)
+  {
+  if (ot == NULL) ot = string_sprintf("%s in \"%s\"?", name, list);
+  }
+
+/* Now scan the list and process each item in turn, until one of them matches,
+or we hit an error. */
+
+while ((sss = string_nextinlist(&list, &sep, buffer, sizeof(buffer))) != NULL)
+  {
+  uschar *ss = sss;
+
+  /* Address lists may contain +caseful, to restore caseful matching of the
+  local part. We have to know the layout of the control block, unfortunately.
+  The lower cased address is in a temporary buffer, so we just copy the local
+  part back to the start of it (if a local part exists). */
+
+  if (type == MCL_ADDRESS)
+    {
+    if (Ustrcmp(ss, "+caseful") == 0)
+      {
+      check_address_block *cb = (check_address_block *)arg;
+      uschar *at = Ustrrchr(cb->origaddress, '@');
+      if (at != NULL)
+        Ustrncpy(cb->address, cb->origaddress, at - cb->origaddress);
+      cb->caseless = FALSE;
+      continue;
+      }
+    }
+
+  /* Similar processing for local parts */
+
+  else if (type == MCL_LOCALPART)
+    {
+    if (Ustrcmp(ss, "+caseful") == 0)
+      {
+      check_string_block *cb = (check_string_block *)arg;
+      Ustrcpy(cb->subject, cb->origsubject);
+      cb->caseless = FALSE;
+      continue;
+      }
+    }
+
+  /* If the host item is "+include_unknown" or "+ignore_unknown", remember it
+  in case there's a subsequent failed reverse lookup. There is similar
+  processing for "defer". */
+
+  else if (type == MCL_HOST && *ss == '+')
+    {
+    if (Ustrcmp(ss, "+include_unknown") == 0)
+      {
+      include_unknown = TRUE;
+      ignore_unknown = FALSE;
+      continue;
+      }
+    if (Ustrcmp(ss, "+ignore_unknown") == 0)
+      {
+      ignore_unknown = TRUE;
+      include_unknown = FALSE;
+      continue;
+      }
+    if (Ustrcmp(ss, "+include_defer") == 0)
+      {
+      include_defer = TRUE;
+      ignore_defer = FALSE;
+      continue;
+      }
+    if (Ustrcmp(ss, "+ignore_defer") == 0)
+      {
+      ignore_defer = TRUE;
+      include_defer = FALSE;
+      continue;
+      }
+    }
+
+  /* Starting with ! specifies a negative item. It is theoretically possible
+  for a local part to start with !. In that case, a regex has to be used. */
+
+  if (*ss == '!')
+    {
+    yield = FAIL;
+    while (isspace((*(++ss))));
+    }
+  else yield = OK;
+
+  /* If the item does not begin with '/', it might be a + item for a named
+  list. Otherwise, it is just a single list entry that has to be matched.
+  We recognize '+' only when supplied with a tree of named lists. */
+
+  if (*ss != '/')
+    {
+    if (*ss == '+' && anchorptr != NULL)
+      {
+      int bits = 0;
+      int offset = 0;
+      int shift = 0;
+      unsigned int *use_cache_bits = original_cache_bits;
+      uschar *cached = US"";
+      namedlist_block *nb;
+      tree_node *t = tree_search(*anchorptr, ss+1);
+
+      if (t == NULL)
+        log_write(0, LOG_MAIN|LOG_PANIC_DIE, "unknown named%s list \"%s\"",
+          (type == MCL_DOMAIN)?    " domain" :
+          (type == MCL_HOST)?      " host" :
+          (type == MCL_ADDRESS)?   " address" :
+          (type == MCL_LOCALPART)? " local part" : "",
+          ss);
+      nb = t->data.ptr;
+
+      /* If the list number is negative, it means that this list is not
+      cacheable because it contains expansion items. */
+
+      if (nb->number < 0) use_cache_bits = NULL;
+
+      /* If we have got a cache pointer, get the bits. This is not an "else"
+      because the pointer may be NULL from the start if caching is not
+      required. */
+
+      if (use_cache_bits != NULL)
+        {
+        offset = (nb->number)/16;
+        shift = ((nb->number)%16)*2;
+        bits = use_cache_bits[offset] & (3 << shift);
+        }
+
+      /* Not previously tested or no cache - run the full test */
+
+      if (bits == 0)
+        {
+        switch (match_check_list(&(nb->string), 0, anchorptr, &use_cache_bits,
+                func, arg, type, name, valueptr))
+          {
+          case OK:   bits = 1; break;
+          case FAIL: bits = 3; break;
+          case DEFER: goto DEFER_RETURN;
+          }
+
+        /* If this list was uncacheable, or a sublist turned out to be
+        uncacheable, the value of use_cache_bits will now be NULL, even if it
+        wasn't before. Ensure that this is passed up to the next level.
+        Otherwise, remember the result of the search in the cache. */
+
+        if (use_cache_bits == NULL)
+          {
+          *cache_ptr = NULL;
+          }
+        else
+          {
+          use_cache_bits[offset] |= bits << shift;
+
+          if (valueptr != NULL)
+            {
+            int old_pool = store_pool;
+            namedlist_cacheblock *p;
+
+            /* Cached data for hosts persists over more than one message,
+            so we use the permanent store pool */
+
+            store_pool = POOL_PERM;
+            p = store_get(sizeof(namedlist_cacheblock));
+            p->key = string_copy(get_check_key(arg, type));
+
+
+            p->data = (*valueptr == NULL)? NULL : string_copy(*valueptr);
+            store_pool = old_pool;
+
+            p->next = nb->cache_data;
+            nb->cache_data = p;
+            if (*valueptr != NULL)
+              {
+              DEBUG(D_lists) debug_printf("data from lookup saved for "
+                "cache for %s: %s\n", ss, *valueptr);
+              }
+            }
+          }
+        }
+
+       /* Previously cached; to find a lookup value, search a chain of values
+       and compare keys. Typically, there is only one such, but it is possible
+       for different keys to have matched the same named list. */
+
+      else
+        {
+        DEBUG(D_lists) debug_printf("cached %s match for %s\n",
+          ((bits & (-bits)) == bits)? "yes" : "no", ss);
+        cached = US" - cached";
+        if (valueptr != NULL)
+          {
+          uschar *key = get_check_key(arg, type);
+          namedlist_cacheblock *p;
+          for (p = nb->cache_data; p != NULL; p = p->next)
+            {
+            if (Ustrcmp(key, p->key) == 0)
+              {
+              *valueptr = p->data;
+              break;
+              }
+            }
+          DEBUG(D_lists) debug_printf("cached lookup data = %s\n", *valueptr);
+          }
+        }
+
+      /* Result of test is indicated by value in bits. For each test, we
+      have 00 => untested, 01 => tested yes, 11 => tested no. */
+
+      if ((bits & (-bits)) == bits)    /* Only one of the two bits is set */
+        {
+        HDEBUG(D_lists) debug_printf("%s %s (matched \"%s\"%s)\n", ot,
+          (yield == OK)? "yes" : "no", sss, cached);
+        return yield;
+        }
+      }
+
+    /* Run the provided function to do the individual test. */
+
+    else
+      {
+      uschar *error = NULL;
+      switch ((func)(arg, ss, valueptr, &error))
+        {
+        case OK:
+        HDEBUG(D_lists) debug_printf("%s %s (matched \"%s\")\n", ot,
+          (yield == OK)? "yes" : "no", sss);
+        return yield;
+
+        case DEFER:
+        if (error == NULL)
+          error = string_sprintf("DNS lookup of %s deferred", ss);
+        if (ignore_defer)
+          {
+          HDEBUG(D_lists) debug_printf("%s: item ignored by +ignore_defer\n",
+            error);
+          break;
+          }
+        if (include_defer)
+          {
+          log_write(0, LOG_MAIN, "%s: accepted by +include_defer", error);
+          return OK;
+          }
+        goto DEFER_RETURN;
+
+        /* The ERROR return occurs when checking hosts, when either a forward
+        or reverse lookup has failed. It can also occur in a match_ip list if a
+        non-IP address item is encountered. The error string gives details of
+        which it was. */
+
+        case ERROR:
+        if (ignore_unknown)
+          {
+          HDEBUG(D_lists) debug_printf("%s: item ignored by +ignore_unknown\n",
+            error);
+          }
+        else
+          {
+          HDEBUG(D_lists) debug_printf("%s %s (%s)\n", ot,
+            include_unknown? "yes":"no", error);
+          if (!include_unknown)
+            {
+            if ((log_extra_selector & LX_unknown_in_list) != 0)
+              log_write(0, LOG_MAIN, "list matching forced to fail: %s", error);
+            return FAIL;
+            }
+          log_write(0, LOG_MAIN, "%s: accepted by +include_unknown", error);
+          return OK;
+          }
+        }
+      }
+    }
+
+  /* If the item is a file name, we read the file and do a match attempt
+  on each line in the file, including possibly more negation processing. */
+
+  else
+    {
+    int file_yield = yield;       /* In case empty file */
+    uschar *filename = ss;
+    FILE *f = Ufopen(filename, "rb");
+    uschar filebuffer[1024];
+
+    /* ot will be null in non-debugging cases, and anyway, we get better
+    wording by reworking it. */
+
+    if (f == NULL)
+      {
+      uschar *listname = readconf_find_option(listptr);
+      if (listname[0] == 0)
+        listname = string_sprintf("\"%s\"", *listptr);
+      log_write(0, LOG_MAIN|LOG_PANIC_DIE, "%s",
+        string_open_failed(errno, "%s when checking %s", sss, listname));
+      }
+
+    /* Trailing comments are introduced by #, but in an address list or local
+    part list, the # must be preceded by white space or the start of the line,
+    because the # character is a legal character in local parts. */
+
+    while (Ufgets(filebuffer, sizeof(filebuffer), f) != NULL)
+      {
+      uschar *error;
+      uschar *sss = filebuffer;
+
+      while ((ss = Ustrchr(sss, '#')) != NULL)
+        {
+        if ((type != MCL_ADDRESS && type != MCL_LOCALPART) ||
+              ss == filebuffer || isspace(ss[-1]))
+          {
+          *ss = 0;
+          break;
+          }
+        sss = ss + 1;
+        }
+
+      ss = filebuffer + Ustrlen(filebuffer);             /* trailing space */
+      while (ss > filebuffer && isspace(ss[-1])) ss--;
+      *ss = 0;
+
+      ss = filebuffer;
+      while (isspace(*ss)) ss++;                         /* leading space */
+
+      if (*ss == 0) continue;                            /* ignore empty */
+
+      file_yield = yield;                                /* positive yield */
+      sss = ss;                                          /* for debugging */
+
+      if (*ss == '!')                                    /* negation */
+        {
+        file_yield = (file_yield == OK)? FAIL : OK;
+        while (isspace((*(++ss))));
+        }
+
+      switch ((func)(arg, ss, valueptr, &error))
+        {
+        case OK:
+        (void)fclose(f);
+        HDEBUG(D_lists) debug_printf("%s %s (matched \"%s\" in %s)\n", ot,
+          (yield == OK)? "yes" : "no", sss, filename);
+        return file_yield;
+
+        case DEFER:
+        if (error == NULL)
+          error = string_sprintf("DNS lookup of %s deferred", ss);
+        if (ignore_defer)
+          {
+          HDEBUG(D_lists) debug_printf("%s: item ignored by +ignore_defer\n",
+            error);
+          break;
+          }
+        (void)fclose(f);
+        if (include_defer)
+          {
+          log_write(0, LOG_MAIN, "%s: accepted by +include_defer", error);
+          return OK;
+          }
+        goto DEFER_RETURN;
+
+        case ERROR:          /* host name lookup failed - this can only */
+        if (ignore_unknown)  /* be for an incoming host (not outgoing) */
+          {
+          HDEBUG(D_lists) debug_printf("%s: item ignored by +ignore_unknown\n",
+            error);
+          }
+        else
+         {
+          HDEBUG(D_lists) debug_printf("%s %s (%s)\n", ot,
+            include_unknown? "yes":"no", error);
+          (void)fclose(f);
+          if (!include_unknown)
+            {
+            if ((log_extra_selector & LX_unknown_in_list) != 0)
+              log_write(0, LOG_MAIN, "list matching forced to fail: %s", error);
+            return FAIL;
+            }
+          log_write(0, LOG_MAIN, "%s: accepted by +include_unknown", error);
+          return OK;
+          }
+        }
+      }
+
+    /* At the end of the file, leave the yield setting at the final setting
+    for the file, in case this is the last item in the list. */
+
+    yield = file_yield;
+    (void)fclose(f);
+    }
+  }    /* Loop for the next item on the top-level list */
+
+/* End of list reached: if the last item was negated yield OK, else FAIL. */
+
+HDEBUG(D_lists)
+  debug_printf("%s %s (end of list)\n", ot, (yield == OK)? "no":"yes");
+return (yield == OK)? FAIL : OK;
+
+/* Something deferred */
+
+DEFER_RETURN:
+HDEBUG(D_lists) debug_printf("%s list match deferred for %s\n", ot, sss);
+return DEFER;
+}
+
+
+/*************************************************
+*          Match in colon-separated list         *
+*************************************************/
+
+/* This function is used for domain lists and local part lists. It is not used
+for host lists or address lists, which have additional interpretation of the
+patterns. Some calls of it set sep > UCHAR_MAX in order to use its matching
+facilities on single items. When this is done, it arranges to set the numerical
+variables as a result of the match.
+
+This function is now just a short interface to match_check_list(), which does
+list scanning in a general way. A good compiler will optimize the tail
+recursion.
+
+Arguments:
+  s              string to search for
+  listptr        ptr to ptr to colon separated list of patterns, or NULL
+  sep            a separator value for the list (see string_nextinlist())
+  anchorptr      ptr to tree for named items, or NULL if no named items
+  cache_bits     ptr to cache_bits for ditto, or NULL if not caching
+  type           MCL_DOMAIN when matching a domain list
+                 MCL_LOCALPART when matching a local part list (address lists
+                   have their own function)
+                 MCL_STRING for others (e.g. list of ciphers)
+                 MCL_NOEXPAND (whose value is greater than any of them) may
+                   be added to any value to suppress expansion of the list
+  caseless       TRUE for (mostly) caseless matching - passed directly to
+                   match_check_string()
+  valueptr       pointer to where any lookup data is to be passed back,
+                 or NULL (just passed on to match_check_string)
+
+Returns:         OK    if matched a non-negated item
+                 OK    if hit end of list after a negated item
+                 FAIL  if expansion force-failed
+                 FAIL  if matched a negated item
+                 FAIL  if hit end of list after a non-negated item
+                 DEFER if a lookup deferred
+*/
+
+int
+match_isinlist(uschar *s, uschar **listptr, int sep, tree_node **anchorptr,
+  unsigned int *cache_bits, int type, BOOL caseless, uschar **valueptr)
+{
+unsigned int *local_cache_bits = cache_bits;
+check_string_block cb;
+cb.origsubject = s;
+cb.subject = caseless? string_copylc(s) : string_copy(s);
+cb.expand_setup = (sep > UCHAR_MAX)? 0 : -1;
+cb.use_partial = TRUE;
+cb.caseless = caseless;
+cb.at_is_special = (type == MCL_DOMAIN || type == MCL_DOMAIN + MCL_NOEXPAND);
+if (valueptr != NULL) *valueptr = NULL;
+return  match_check_list(listptr, sep, anchorptr, &local_cache_bits,
+  check_string, &cb, type, s, valueptr);
+}
+
+
+
+/*************************************************
+*    Match address to single address-list item   *
+*************************************************/
+
+/* This function matches an address to an item from an address list. It is
+called from match_address_list() via match_check_list(). That is why most of
+its arguments are in an indirect block.
+
+Arguments:
+  arg            the argument block (see below)
+  pattern        the pattern to match
+  valueptr       where to return a value
+  error          for error messages (not used in this function; it never
+                   returns ERROR)
+
+The argument block contains:
+  address        the start of the subject address; when called from retry.c
+                   it may be *@domain if the local part isn't relevant
+  origaddress    the original, un-case-forced address (not used here, but used
+                   in match_check_list() when +caseful is encountered)
+  expand_setup   controls setting up of $n variables
+  caseless       TRUE for caseless local part matching
+
+Returns:         OK     for a match
+                 FAIL   for no match
+                 DEFER  if a lookup deferred
+*/
+
+static int
+check_address(void *arg, uschar *pattern, uschar **valueptr, uschar **error)
+{
+check_address_block *cb = (check_address_block *)arg;
+check_string_block csb;
+int rc;
+int expand_inc = 0;
+unsigned int *null = NULL;
+uschar *listptr;
+uschar *subject = cb->address;
+uschar *s, *pdomain, *sdomain;
+
+error = error;  /* Keep clever compilers from complaining */
+
+DEBUG(D_lists) debug_printf("address match test: subject=%s pattern=%s\n",
+  subject, pattern);
+
+/* Find the subject's domain */
+
+sdomain = Ustrrchr(subject, '@');
+
+/* The only case where a subject may not have a domain is if the subject is
+empty. Otherwise, a subject with no domain is a serious configuration error. */
+
+if (sdomain == NULL && *subject != 0)
+  {
+  log_write(0, LOG_MAIN|LOG_PANIC, "no @ found in the subject of an "
+    "address list match: subject=\"%s\" pattern=\"%s\"", subject, pattern);
+  return FAIL;
+  }
+
+/* Handle a regular expression, which must match the entire incoming address.
+This may be the empty address. */
+
+if (*pattern == '^')
+  return match_check_string(subject, pattern, cb->expand_setup, TRUE,
+    cb->caseless, FALSE, NULL);
+
+/* Handle a pattern that is just a lookup. Skip over possible lookup names
+(letters, digits, hyphens). Skip over a possible * or *@ at the end. Then we
+must have a semicolon for it to be a lookup. */
+
+for (s = pattern; isalnum(*s) || *s == '-'; s++);
+if (*s == '*') s++;
+if (*s == '@') s++;
+
+/* If it is a straight lookup, do a lookup for the whole address. This may be
+the empty address. Partial matching doesn't make sense here, so we ignore it,
+but write a panic log entry. However, *@ matching will be honoured. */
+
+if (*s == ';')
+  {
+  if (Ustrncmp(pattern, "partial-", 8) == 0)
+    log_write(0, LOG_MAIN|LOG_PANIC, "partial matching is not applicable to "
+      "whole-address lookups: ignored \"partial-\" in \"%s\"", pattern);
+  return match_check_string(subject, pattern, -1, FALSE, cb->caseless, FALSE,
+    valueptr);
+  }
+
+/* For the remaining cases, an empty subject matches only an empty pattern,
+because other patterns expect to have a local part and a domain to match
+against. */
+
+if (*subject == 0) return (*pattern == 0)? OK : FAIL;
+
+/* If the pattern starts with "@@" we have a split lookup, where the domain is
+looked up to obtain a list of local parts. If the subject's local part is just
+"*" (called from retry) the match always fails. */
+
+if (pattern[0] == '@' && pattern[1] == '@')
+  {
+  int watchdog = 50;
+  uschar *list, *key, *ss;
+  uschar buffer[1024];
+
+  if (sdomain == subject + 1 && *subject == '*') return FAIL;
+
+  /* Loop for handling chains. The last item in any list may be of the form
+  ">name" in order to chain on to another list. */
+
+  for (key = sdomain + 1; key != NULL && watchdog-- > 0; )
+    {
+    int sep = 0;
+
+    if ((rc = match_check_string(key, pattern + 2, -1, TRUE, FALSE, FALSE,
+      &list)) != OK) return rc;
+
+    /* Check for chaining from the last item; set up the next key if one
+    is found. */
+
+    ss = Ustrrchr(list, ':');
+    if (ss == NULL) ss = list; else ss++;
+    while (isspace(*ss)) ss++;
+    if (*ss == '>')
+      {
+      *ss++ = 0;
+      while (isspace(*ss)) ss++;
+      key = string_copy(ss);
+      }
+    else key = NULL;
+
+    /* Look up the local parts provided by the list; negation is permitted.
+    If a local part has to begin with !, a regex can be used. */
+
+    while ((ss = string_nextinlist(&list, &sep, buffer, sizeof(buffer)))
+           != NULL)
+      {
+      int local_yield;
+
+      if (*ss == '!')
+        {
+        local_yield = FAIL;
+        while (isspace((*(++ss))));
+        }
+      else local_yield = OK;
+
+      *sdomain = 0;
+      rc = match_check_string(subject, ss, -1, TRUE, cb->caseless, FALSE,
+        valueptr);
+      *sdomain = '@';
+
+      switch(rc)
+        {
+        case OK:
+        return local_yield;
+
+        case DEFER:
+        return DEFER;
+        }
+      }
+    }
+
+  /* End of chain loop; panic if too many times */
+
+  if (watchdog <= 0)
+    log_write(0, LOG_MAIN|LOG_PANIC_DIE, "Loop detected in lookup of "
+      "local part of %s in %s", subject, pattern);
+
+  /* Otherwise the local part check has failed, so the whole match
+  fails. */
+
+  return FAIL;
+  }
+
+
+/* We get here if the pattern is not a lookup or a regular expression. If it
+contains an @ there is both a local part and a domain. */
+
+pdomain = Ustrrchr(pattern, '@');
+if (pdomain != NULL)
+  {
+  int pllen, sllen;
+
+  /* If the domain in the pattern is empty or one of the special cases [] or
+  mx_{any,primary,secondary}, and the local part in the pattern ends in "@",
+  we have a pattern of the form <something>@@, <something>@@[], or
+  <something>@@mx_{any,primary,secondary}. These magic "domains" are
+  automatically interpreted in match_check_string. We just need to arrange that
+  the leading @ is included in the domain. */
+
+  if (pdomain > pattern && pdomain[-1] == '@' &&
+       (pdomain[1] == 0 ||
+        Ustrcmp(pdomain+1, "[]") == 0 ||
+        Ustrcmp(pdomain+1, "mx_any") == 0 ||
+        Ustrcmp(pdomain+1, "mx_primary") == 0 ||
+        Ustrcmp(pdomain+1, "mx_secondary") == 0))
+    pdomain--;
+
+  pllen = pdomain - pattern;
+  sllen = sdomain - subject;
+
+  /* Compare the local parts in the subject and the pattern */
+
+  if (*pattern == '*')
+    {
+    int cllen = pllen - 1;
+    if (sllen < cllen) return FAIL;
+    if (cb->caseless)
+      {
+      if (strncmpic(subject+sllen-cllen, pattern + 1, cllen) != 0)
+        return FAIL;
+      }
+    else
+      {
+      if (Ustrncmp(subject+sllen-cllen, pattern + 1, cllen) != 0)
+        return FAIL;
+      }
+    if (cb->expand_setup > 0)
+      {
+      expand_nstring[cb->expand_setup] = subject;
+      expand_nlength[cb->expand_setup] = sllen - cllen;
+      expand_inc = 1;
+      }
+    }
+  else
+    {
+    if (sllen != pllen) return FAIL;
+    if (cb->caseless)
+      {
+      if (strncmpic(subject, pattern, sllen) != 0) return FAIL;
+      }
+    else
+      {
+      if (Ustrncmp(subject, pattern, sllen) != 0) return FAIL;
+      }
+    }
+  }
+
+/* If the local part matched, or was not being checked, check the domain using
+the generalized function, which supports file lookups (which may defer). The
+original code read as follows:
+
+  return match_check_string(sdomain + 1,
+      (pdomain == NULL)? pattern : pdomain + 1,
+      cb->expand_setup + expand_inc, TRUE, cb->caseless, TRUE, NULL);
+
+This supported only literal domains and *.x.y patterns. In order to allow for
+named domain lists (so that you can right, for example, "senders=+xxxx"), it
+was changed to use the list scanning function. */
+
+csb.origsubject = sdomain + 1;
+csb.subject = (cb->caseless)? string_copylc(sdomain+1) : string_copy(sdomain+1);
+csb.expand_setup = cb->expand_setup + expand_inc;
+csb.use_partial = TRUE;
+csb.caseless = cb->caseless;
+csb.at_is_special = TRUE;
+
+listptr = (pdomain == NULL)? pattern : pdomain + 1;
+if (valueptr != NULL) *valueptr = NULL;
+
+return match_check_list(
+  &listptr,                  /* list of one item */
+  UCHAR_MAX+1,               /* impossible separator; single item */
+  &domainlist_anchor,        /* it's a domain list */
+  &null,                     /* ptr to NULL means no caching */
+  check_string,              /* the function to do one test */
+  &csb,                      /* its data */
+  MCL_DOMAIN + MCL_NOEXPAND, /* domain list; don't expand */
+  csb.subject,               /* string for messages */
+  valueptr);                 /* where to pass back lookup data */
+}
+
+
+
+
+/*************************************************
+*    Test whether address matches address list   *
+*************************************************/
+
+/* This function is given an address and a list of things to match it against.
+The list may contain individual addresses, regular expressions, lookup
+specifications, and indirection via bare files. Negation is supported. The
+address to check can consist of just a domain, which will then match only
+domain items or items specified as *@domain.
+
+Domains are always lower cased before the match. Local parts are also lower
+cased unless "caseless" is false. The work of actually scanning the list is
+done by match_check_list(), with an appropriate block of arguments and a
+callback to check_address(). During caseless matching, it will recognize
++caseful and revert to caseful matching.
+
+Arguments:
+  address         address to test
+  caseless        TRUE to start in caseless state
+  expand          TRUE to allow list expansion
+  listptr         list to check against
+  cache_bits      points to cache bits for named address lists, or NULL
+  expand_setup    controls setting up of $n variables - passed through
+                  to check_address (q.v.)
+  sep             separator character for the list;
+                  may be 0 to get separator from the list;
+                  may be UCHAR_MAX+1 for one-item list
+  valueptr        where to return a lookup value, or NULL
+
+Returns:          OK    for a positive match, or end list after a negation;
+                  FAIL  for a negative match, or end list after non-negation;
+                  DEFER if a lookup deferred
+*/
+
+int
+match_address_list(uschar *address, BOOL caseless, BOOL expand,
+  uschar **listptr, unsigned int *cache_bits, int expand_setup, int sep,
+  uschar **valueptr)
+{
+uschar *p;
+check_address_block ab;
+unsigned int *local_cache_bits = cache_bits;
+
+/* RFC 2505 recommends that for spam checking, local parts should be caselessly
+compared. Therefore, Exim now forces the entire address into lower case here,
+provided that "caseless" is set. (It is FALSE for calls for matching rewriting
+patterns.) Otherwise just the domain is lower cases. A magic item "+caseful" in
+the list can be used to restore a caseful copy of the local part from the
+original address. */
+
+sprintf(CS big_buffer, "%.*s", big_buffer_size - 1, address);
+for (p = big_buffer + Ustrlen(big_buffer) - 1; p >= big_buffer; p--)
+  {
+  if (!caseless && *p == '@') break;
+  *p = tolower(*p);
+  }
+
+/* If expand_setup is zero, we need to set up $0 to the whole thing, in
+case there is a match. Can't use the built-in facilities of match_check_string
+(via check_address), as we may just be calling that for part of the address
+(the domain). */
+
+if (expand_setup == 0)
+  {
+  expand_nstring[0] = string_copy(address);
+  expand_nlength[0] = Ustrlen(address);
+  expand_setup++;
+  }
+
+/* Set up the data to be passed ultimately to check_address. */
+
+ab.origaddress = address;
+ab.address = big_buffer;
+ab.expand_setup = expand_setup;
+ab.caseless = caseless;
+
+return match_check_list(listptr, sep, &addresslist_anchor, &local_cache_bits,
+  check_address, &ab, MCL_ADDRESS + (expand? 0:MCL_NOEXPAND), address,
+    valueptr);
+}
+
+/* End of match.c */
diff --git a/src/memcheck.h b/src/memcheck.h
new file mode 100644 (file)
index 0000000..bf95491
--- /dev/null
@@ -0,0 +1,277 @@
+
+/*
+   ----------------------------------------------------------------
+
+   Notice that the following BSD-style license applies to this one
+   file (memcheck.h) only.  The rest of Valgrind is licensed under the
+   terms of the GNU General Public License, version 2, unless
+   otherwise indicated.  See the COPYING file in the source
+   distribution for details.
+
+   ----------------------------------------------------------------
+
+   This file is part of MemCheck, a heavyweight Valgrind tool for
+   detecting memory errors.
+
+   Copyright (C) 2000-2010 Julian Seward.  All rights reserved.
+
+   Redistribution and use in source and binary forms, with or without
+   modification, are permitted provided that the following conditions
+   are met:
+
+   1. Redistributions of source code must retain the above copyright
+      notice, this list of conditions and the following disclaimer.
+
+   2. The origin of this software must not be misrepresented; you must 
+      not claim that you wrote the original software.  If you use this 
+      software in a product, an acknowledgment in the product 
+      documentation would be appreciated but is not required.
+
+   3. Altered source versions must be plainly marked as such, and must
+      not be misrepresented as being the original software.
+
+   4. The name of the author may not be used to endorse or promote 
+      products derived from this software without specific prior written 
+      permission.
+
+   THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
+   OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+   ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
+   DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
+   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+   ----------------------------------------------------------------
+
+   Notice that the above BSD-style license applies to this one file
+   (memcheck.h) only.  The entire rest of Valgrind is licensed under
+   the terms of the GNU General Public License, version 2.  See the
+   COPYING file in the source distribution for details.
+
+   ---------------------------------------------------------------- 
+*/
+
+
+#ifndef __MEMCHECK_H
+#define __MEMCHECK_H
+
+
+/* This file is for inclusion into client (your!) code.
+
+   You can use these macros to manipulate and query memory permissions
+   inside your own programs.
+
+   See comment near the top of valgrind.h on how to use them.
+*/
+
+#include "valgrind.h"
+
+/* !! ABIWARNING !! ABIWARNING !! ABIWARNING !! ABIWARNING !! 
+   This enum comprises an ABI exported by Valgrind to programs
+   which use client requests.  DO NOT CHANGE THE ORDER OF THESE
+   ENTRIES, NOR DELETE ANY -- add new ones at the end. */
+typedef
+   enum { 
+      VG_USERREQ__MAKE_MEM_NOACCESS = VG_USERREQ_TOOL_BASE('M','C'),
+      VG_USERREQ__MAKE_MEM_UNDEFINED,
+      VG_USERREQ__MAKE_MEM_DEFINED,
+      VG_USERREQ__DISCARD,
+      VG_USERREQ__CHECK_MEM_IS_ADDRESSABLE,
+      VG_USERREQ__CHECK_MEM_IS_DEFINED,
+      VG_USERREQ__DO_LEAK_CHECK,
+      VG_USERREQ__COUNT_LEAKS,
+
+      VG_USERREQ__GET_VBITS,
+      VG_USERREQ__SET_VBITS,
+
+      VG_USERREQ__CREATE_BLOCK,
+
+      VG_USERREQ__MAKE_MEM_DEFINED_IF_ADDRESSABLE,
+
+      /* Not next to VG_USERREQ__COUNT_LEAKS because it was added later. */
+      VG_USERREQ__COUNT_LEAK_BLOCKS,
+
+      /* This is just for memcheck's internal use - don't use it */
+      _VG_USERREQ__MEMCHECK_RECORD_OVERLAP_ERROR 
+         = VG_USERREQ_TOOL_BASE('M','C') + 256
+   } Vg_MemCheckClientRequest;
+
+
+
+/* Client-code macros to manipulate the state of memory. */
+
+/* Mark memory at _qzz_addr as unaddressable for _qzz_len bytes. */
+#define VALGRIND_MAKE_MEM_NOACCESS(_qzz_addr,_qzz_len)           \
+    VALGRIND_DO_CLIENT_REQUEST_EXPR(0 /* default return */,      \
+                            VG_USERREQ__MAKE_MEM_NOACCESS,       \
+                            (_qzz_addr), (_qzz_len), 0, 0, 0)
+      
+/* Similarly, mark memory at _qzz_addr as addressable but undefined
+   for _qzz_len bytes. */
+#define VALGRIND_MAKE_MEM_UNDEFINED(_qzz_addr,_qzz_len)          \
+    VALGRIND_DO_CLIENT_REQUEST_EXPR(0 /* default return */,      \
+                            VG_USERREQ__MAKE_MEM_UNDEFINED,      \
+                            (_qzz_addr), (_qzz_len), 0, 0, 0)
+
+/* Similarly, mark memory at _qzz_addr as addressable and defined
+   for _qzz_len bytes. */
+#define VALGRIND_MAKE_MEM_DEFINED(_qzz_addr,_qzz_len)            \
+    VALGRIND_DO_CLIENT_REQUEST_EXPR(0 /* default return */,      \
+                            VG_USERREQ__MAKE_MEM_DEFINED,        \
+                            (_qzz_addr), (_qzz_len), 0, 0, 0)
+
+/* Similar to VALGRIND_MAKE_MEM_DEFINED except that addressability is
+   not altered: bytes which are addressable are marked as defined,
+   but those which are not addressable are left unchanged. */
+#define VALGRIND_MAKE_MEM_DEFINED_IF_ADDRESSABLE(_qzz_addr,_qzz_len)     \
+    VALGRIND_DO_CLIENT_REQUEST_EXPR(0 /* default return */,              \
+                            VG_USERREQ__MAKE_MEM_DEFINED_IF_ADDRESSABLE, \
+                            (_qzz_addr), (_qzz_len), 0, 0, 0)
+
+/* Create a block-description handle.  The description is an ascii
+   string which is included in any messages pertaining to addresses
+   within the specified memory range.  Has no other effect on the
+   properties of the memory range. */
+#define VALGRIND_CREATE_BLOCK(_qzz_addr,_qzz_len, _qzz_desc)      \
+    VALGRIND_DO_CLIENT_REQUEST_EXPR(0 /* default return */,        \
+                            VG_USERREQ__CREATE_BLOCK,              \
+                            (_qzz_addr), (_qzz_len), (_qzz_desc),  \
+                            0, 0)
+
+/* Discard a block-description-handle. Returns 1 for an
+   invalid handle, 0 for a valid handle. */
+#define VALGRIND_DISCARD(_qzz_blkindex)                          \
+    VALGRIND_DO_CLIENT_REQUEST_EXPR(0 /* default return */,      \
+                            VG_USERREQ__DISCARD,                 \
+                            0, (_qzz_blkindex), 0, 0, 0)
+
+
+/* Client-code macros to check the state of memory. */
+
+/* Check that memory at _qzz_addr is addressable for _qzz_len bytes.
+   If suitable addressibility is not established, Valgrind prints an
+   error message and returns the address of the first offending byte.
+   Otherwise it returns zero. */
+#define VALGRIND_CHECK_MEM_IS_ADDRESSABLE(_qzz_addr,_qzz_len)      \
+    VALGRIND_DO_CLIENT_REQUEST_EXPR(0,                             \
+                            VG_USERREQ__CHECK_MEM_IS_ADDRESSABLE,  \
+                            (_qzz_addr), (_qzz_len), 0, 0, 0)
+
+/* Check that memory at _qzz_addr is addressable and defined for
+   _qzz_len bytes.  If suitable addressibility and definedness are not
+   established, Valgrind prints an error message and returns the
+   address of the first offending byte.  Otherwise it returns zero. */
+#define VALGRIND_CHECK_MEM_IS_DEFINED(_qzz_addr,_qzz_len)        \
+    VALGRIND_DO_CLIENT_REQUEST_EXPR(0,                           \
+                            VG_USERREQ__CHECK_MEM_IS_DEFINED,    \
+                            (_qzz_addr), (_qzz_len), 0, 0, 0);
+
+/* Use this macro to force the definedness and addressibility of an
+   lvalue to be checked.  If suitable addressibility and definedness
+   are not established, Valgrind prints an error message and returns
+   the address of the first offending byte.  Otherwise it returns
+   zero. */
+#define VALGRIND_CHECK_VALUE_IS_DEFINED(__lvalue)                \
+   VALGRIND_CHECK_MEM_IS_DEFINED(                                \
+      (volatile unsigned char *)&(__lvalue),                     \
+                      (unsigned long)(sizeof (__lvalue)))
+
+
+/* Do a full memory leak check (like --leak-check=full) mid-execution. */
+#define VALGRIND_DO_LEAK_CHECK                                   \
+   {unsigned long _qzz_res;                                      \
+    VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0,                      \
+                            VG_USERREQ__DO_LEAK_CHECK,           \
+                            0, 0, 0, 0, 0);                      \
+   }
+
+/* Do a summary memory leak check (like --leak-check=summary) mid-execution. */
+#define VALGRIND_DO_QUICK_LEAK_CHECK                            \
+   {unsigned long _qzz_res;                                      \
+    VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0,                      \
+                            VG_USERREQ__DO_LEAK_CHECK,           \
+                            1, 0, 0, 0, 0);                      \
+   }
+
+/* Return number of leaked, dubious, reachable and suppressed bytes found by
+   all previous leak checks.  They must be lvalues.  */
+#define VALGRIND_COUNT_LEAKS(leaked, dubious, reachable, suppressed)     \
+   /* For safety on 64-bit platforms we assign the results to private
+      unsigned long variables, then assign these to the lvalues the user
+      specified, which works no matter what type 'leaked', 'dubious', etc
+      are.  We also initialise '_qzz_leaked', etc because
+      VG_USERREQ__COUNT_LEAKS doesn't mark the values returned as
+      defined. */                                                        \
+   {unsigned long _qzz_res;                                              \
+    unsigned long _qzz_leaked    = 0, _qzz_dubious    = 0;               \
+    unsigned long _qzz_reachable = 0, _qzz_suppressed = 0;               \
+    VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0,                              \
+                               VG_USERREQ__COUNT_LEAKS,                  \
+                               &_qzz_leaked, &_qzz_dubious,              \
+                               &_qzz_reachable, &_qzz_suppressed, 0);    \
+    leaked     = _qzz_leaked;                                            \
+    dubious    = _qzz_dubious;                                           \
+    reachable  = _qzz_reachable;                                         \
+    suppressed = _qzz_suppressed;                                        \
+   }
+
+/* Return number of leaked, dubious, reachable and suppressed bytes found by
+   all previous leak checks.  They must be lvalues.  */
+#define VALGRIND_COUNT_LEAK_BLOCKS(leaked, dubious, reachable, suppressed) \
+   /* For safety on 64-bit platforms we assign the results to private
+      unsigned long variables, then assign these to the lvalues the user
+      specified, which works no matter what type 'leaked', 'dubious', etc
+      are.  We also initialise '_qzz_leaked', etc because
+      VG_USERREQ__COUNT_LEAKS doesn't mark the values returned as
+      defined. */                                                        \
+   {unsigned long _qzz_res;                                              \
+    unsigned long _qzz_leaked    = 0, _qzz_dubious    = 0;               \
+    unsigned long _qzz_reachable = 0, _qzz_suppressed = 0;               \
+    VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0,                              \
+                               VG_USERREQ__COUNT_LEAK_BLOCKS,            \
+                               &_qzz_leaked, &_qzz_dubious,              \
+                               &_qzz_reachable, &_qzz_suppressed, 0);    \
+    leaked     = _qzz_leaked;                                            \
+    dubious    = _qzz_dubious;                                           \
+    reachable  = _qzz_reachable;                                         \
+    suppressed = _qzz_suppressed;                                        \
+   }
+
+
+/* Get the validity data for addresses [zza..zza+zznbytes-1] and copy it
+   into the provided zzvbits array.  Return values:
+      0   if not running on valgrind
+      1   success
+      2   [previously indicated unaligned arrays;  these are now allowed]
+      3   if any parts of zzsrc/zzvbits are not addressable.
+   The metadata is not copied in cases 0, 2 or 3 so it should be
+   impossible to segfault your system by using this call.
+*/
+#define VALGRIND_GET_VBITS(zza,zzvbits,zznbytes)                     \
+    VALGRIND_DO_CLIENT_REQUEST_EXPR(0,                               \
+                                    VG_USERREQ__GET_VBITS,           \
+                                    (char*)(zza), (char*)(zzvbits),  \
+                                    (zznbytes), 0, 0)
+
+/* Set the validity data for addresses [zza..zza+zznbytes-1], copying it
+   from the provided zzvbits array.  Return values:
+      0   if not running on valgrind
+      1   success
+      2   [previously indicated unaligned arrays;  these are now allowed]
+      3   if any parts of zza/zzvbits are not addressable.
+   The metadata is not copied in cases 0, 2 or 3 so it should be
+   impossible to segfault your system by using this call.
+*/
+#define VALGRIND_SET_VBITS(zza,zzvbits,zznbytes)                     \
+    VALGRIND_DO_CLIENT_REQUEST_EXPR(0,                               \
+                                    VG_USERREQ__SET_VBITS,           \
+                                    (char*)(zza), (char*)(zzvbits),  \
+                                    (zznbytes), 0, 0 )
+
+#endif
+
diff --git a/src/mime.c b/src/mime.c
new file mode 100644 (file)
index 0000000..95d3da4
--- /dev/null
@@ -0,0 +1,740 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) Tom Kistner <tom@duncanthrax.net> 2004 */
+/* License: GPL */
+
+#include "exim.h"
+#ifdef WITH_CONTENT_SCAN       /* entire file */
+#include "mime.h"
+#include <sys/stat.h>
+
+FILE *mime_stream = NULL;
+uschar *mime_current_boundary = NULL;
+
+/*************************************************
+* set MIME anomaly level + text                  *
+*************************************************/
+
+/* Small wrapper to set the two expandables which
+   give info on detected "problems" in MIME
+   encodings. Those are defined in mime.h. */
+
+void
+mime_set_anomaly(int level, const char *text)
+{
+  mime_anomaly_level = level;
+  mime_anomaly_text = CUS text;
+}
+
+
+/*************************************************
+* decode quoted-printable chars                  *
+*************************************************/
+
+/* gets called when we hit a =
+   returns: new pointer position
+   result code in c:
+          -2 - decode error
+          -1 - soft line break, no char
+           0-255 - char to write
+*/
+
+uschar *
+mime_decode_qp_char(uschar *qp_p, int *c)
+{
+uschar *initial_pos = qp_p;
+
+/* advance one char */
+qp_p++;
+
+/* Check for two hex digits and decode them */
+if (isxdigit(*qp_p) && isxdigit(qp_p[1]))
+  {
+  /* Do hex conversion */
+  *c = (isdigit(*qp_p) ? *qp_p - '0' : toupper(*qp_p) - 'A' + 10) <<4;
+  qp_p++;
+  *c |= isdigit(*qp_p) ? *qp_p - '0' : toupper(*qp_p) - 'A' + 10;
+  return qp_p + 1;
+  }
+
+/* tab or whitespace may follow just ignore it if it precedes \n */
+while (*qp_p == '\t' || *qp_p == ' ' || *qp_p == '\r')
+  qp_p++;
+
+if (*qp_p == '\n')     /* hit soft line break */
+  {
+  *c = -1;
+  return qp_p;
+  }
+
+/* illegal char here */
+*c = -2;
+return initial_pos;
+}
+
+
+/* just dump MIME part without any decoding */
+static ssize_t
+mime_decode_asis(FILE* in, FILE* out, uschar* boundary)
+{
+  ssize_t len, size = 0;
+  uschar buffer[MIME_MAX_LINE_LENGTH];
+
+  while(fgets(CS buffer, MIME_MAX_LINE_LENGTH, mime_stream) != NULL)
+    {
+    if (boundary != NULL
+       && Ustrncmp(buffer, "--", 2) == 0
+       && Ustrncmp((buffer+2), boundary, Ustrlen(boundary)) == 0
+       )
+      break;
+
+    len = Ustrlen(buffer);
+    if (fwrite(buffer, 1, (size_t)len, out) < len)
+      return -1;
+    size += len;
+    } /* while */
+  return size;
+}
+
+
+/* decode base64 MIME part */
+static ssize_t
+mime_decode_base64(FILE* in, FILE* out, uschar* boundary)
+{
+  uschar ibuf[MIME_MAX_LINE_LENGTH], obuf[MIME_MAX_LINE_LENGTH];
+  uschar *ipos, *opos;
+  ssize_t len, size = 0;
+  int bytestate = 0;
+
+  opos = obuf;
+
+  while (Ufgets(ibuf, MIME_MAX_LINE_LENGTH, in) != NULL)
+    {
+    if (boundary != NULL
+       && Ustrncmp(ibuf, "--", 2) == 0
+       && Ustrncmp((ibuf+2), boundary, Ustrlen(boundary)) == 0
+       )
+      break;
+
+    for (ipos = ibuf ; *ipos != '\r' && *ipos != '\n' && *ipos != 0; ++ipos)
+      {
+      if (*ipos == '=')                        /* skip padding */
+        {
+        ++bytestate;
+        continue;
+       }
+      if (mime_b64[*ipos] == 128)      /* skip bad characters */
+        {
+        mime_set_anomaly(MIME_ANOMALY_BROKEN_BASE64);
+        continue;
+       }
+
+      /* simple state-machine */
+      switch((bytestate++) & 3)
+        {
+        case 0:
+          *opos = mime_b64[*ipos] << 2;
+           break;
+        case 1:
+          *opos |= mime_b64[*ipos] >> 4;
+          ++opos;
+          *opos = mime_b64[*ipos] << 4;
+          break;
+        case 2:
+          *opos |= mime_b64[*ipos] >> 2;
+          ++opos;
+          *opos = mime_b64[*ipos] << 6;
+          break;
+        case 3:
+          *opos |= mime_b64[*ipos];
+          ++opos;
+          break;
+       } /* switch */
+      } /* for */
+
+    /* something to write? */
+    len = opos - obuf;
+    if (len > 0)
+      {
+      if (fwrite(obuf, 1, len, out) != len) return -1; /* error */
+      size += len;
+      /* copy incomplete last byte to start of obuf, where we continue */
+      if ((bytestate & 3) != 0)
+        *obuf = *opos;
+      opos = obuf;
+      }
+    } /* while */
+
+  /* write out last byte if it was incomplete */
+  if (bytestate & 3)
+    {
+    if (fwrite(obuf, 1, 1, out) != 1) return -1;
+    ++size;
+    }
+
+  return size;
+}
+
+
+/* decode quoted-printable MIME part */
+static ssize_t
+mime_decode_qp(FILE* in, FILE* out, uschar* boundary)
+{
+uschar ibuf[MIME_MAX_LINE_LENGTH], obuf[MIME_MAX_LINE_LENGTH];
+uschar *ipos, *opos;
+ssize_t len, size = 0;
+
+while (fgets(CS ibuf, MIME_MAX_LINE_LENGTH, in) != NULL)
+  {
+  if (boundary != NULL
+     && Ustrncmp(ibuf, "--", 2) == 0
+     && Ustrncmp((ibuf+2), boundary, Ustrlen(boundary)) == 0
+     )
+    break; /* todo: check for missing boundary */
+
+  ipos = ibuf;
+  opos = obuf;
+
+  while (*ipos != 0)
+    {
+    if (*ipos == '=')
+      {
+      int decode_qp_result;
+
+      ipos = mime_decode_qp_char(ipos, &decode_qp_result);
+
+      if (decode_qp_result == -2)
+       {
+       /* Error from decoder. ipos is unchanged. */
+       mime_set_anomaly(MIME_ANOMALY_BROKEN_QP);
+       *opos = '=';
+       ++opos;
+       ++ipos;
+       }
+      else if (decode_qp_result == -1)
+       break;
+      else if (decode_qp_result >= 0)
+       {
+       *opos = decode_qp_result;
+       ++opos;
+       }
+      }
+    else
+      {
+      *opos = *ipos;
+      ++opos;
+      ++ipos;
+      }
+    }
+  /* something to write? */
+  len = opos - obuf;
+  if (len > 0)
+    {
+    if (fwrite(obuf, 1, len, out) != len) return -1; /* error */
+    size += len;
+    }
+  }
+return size;
+}
+
+
+FILE *
+mime_get_decode_file(uschar *pname, uschar *fname)
+{
+FILE *f = NULL;
+uschar *filename;
+
+filename = (uschar *)malloc(2048);
+
+if (pname && fname)
+  {
+  (void)string_format(filename, 2048, "%s/%s", pname, fname);
+  f = modefopen(filename,"wb+",SPOOL_MODE);
+  }
+else if (!pname)
+  f = modefopen(fname,"wb+",SPOOL_MODE);
+else if (!fname)
+  {
+  int file_nr = 0;
+  int result = 0;
+
+  /* must find first free sequential filename */
+  do
+    {
+    struct stat mystat;
+    (void)string_format(filename, 2048,
+      "%s/%s-%05u", pname, message_id, file_nr++);
+    /* security break */
+    if (file_nr >= 1024)
+      break;
+    result = stat(CS filename, &mystat);
+    } while(result != -1);
+
+  f = modefopen(filename, "wb+", SPOOL_MODE);
+  }
+
+/* set expansion variable */
+mime_decoded_filename = filename;
+
+return f;
+}
+
+
+int
+mime_decode(uschar **listptr)
+{
+int sep = 0;
+uschar *list = *listptr;
+uschar *option;
+uschar option_buffer[1024];
+uschar decode_path[1024];
+FILE *decode_file = NULL;
+long f_pos = 0;
+ssize_t size_counter = 0;
+ssize_t (*decode_function)(FILE*, FILE*, uschar*);
+
+if (mime_stream == NULL)
+  return FAIL;
+
+f_pos = ftell(mime_stream);
+
+/* build default decode path (will exist since MBOX must be spooled up) */
+(void)string_format(decode_path,1024,"%s/scan/%s",spool_directory,message_id);
+
+/* try to find 1st option */
+if ((option = string_nextinlist(&list, &sep,
+                               option_buffer,
+                               sizeof(option_buffer))) != NULL)
+  {
+  /* parse 1st option */
+  if ( (Ustrcmp(option,"false") == 0) || (Ustrcmp(option,"0") == 0) )
+    /* explicitly no decoding */
+    return FAIL;
+
+  if (Ustrcmp(option,"default") == 0)
+    /* explicit default path + file names */
+    goto DEFAULT_PATH;
+
+  if (option[0] == '/')
+    {
+    struct stat statbuf;
+
+    memset(&statbuf,0,sizeof(statbuf));
+
+    /* assume either path or path+file name */
+    if ( (stat(CS option, &statbuf) == 0) && S_ISDIR(statbuf.st_mode) )
+      /* is directory, use it as decode_path */
+      decode_file = mime_get_decode_file(option, NULL);
+    else
+      /* does not exist or is a file, use as full file name */
+      decode_file = mime_get_decode_file(NULL, option);
+    }
+  else
+    /* assume file name only, use default path */
+    decode_file = mime_get_decode_file(decode_path, option);
+  }
+else
+  {
+  /* no option? patch default path */
+DEFAULT_PATH:
+  decode_file = mime_get_decode_file(decode_path, NULL);
+  }
+
+if (!decode_file)
+  return DEFER;
+
+/* decode according to mime type */
+decode_function =
+  !mime_content_transfer_encoding
+  ? mime_decode_asis   /* no encoding, dump as-is */
+  : Ustrcmp(mime_content_transfer_encoding, "base64") == 0
+  ? mime_decode_base64
+  : Ustrcmp(mime_content_transfer_encoding, "quoted-printable") == 0
+  ? mime_decode_qp
+  : mime_decode_asis;  /* unknown encoding type, just dump as-is */
+
+size_counter = decode_function(mime_stream, decode_file, mime_current_boundary);
+
+clearerr(mime_stream);
+fseek(mime_stream, f_pos, SEEK_SET);
+
+if (fclose(decode_file) != 0 || size_counter < 0)
+  return DEFER;
+
+/* round up to the next KiB */
+mime_content_size = (size_counter + 1023) / 1024;
+
+return OK;
+}
+
+int
+mime_get_header(FILE *f, uschar *header)
+{
+int c = EOF;
+int done = 0;
+int header_value_mode = 0;
+int header_open_brackets = 0;
+int num_copied = 0;
+
+while(!done)
+  {
+  if ((c = fgetc(f)) == EOF) break;
+
+  /* always skip CRs */
+  if (c == '\r') continue;
+
+  if (c == '\n')
+    {
+    if (num_copied > 0)
+      {
+      /* look if next char is '\t' or ' ' */
+      if ((c = fgetc(f)) == EOF) break;
+      if ( (c == '\t') || (c == ' ') ) continue;
+      (void)ungetc(c,f);
+      }
+    /* end of the header, terminate with ';' */
+    c = ';';
+    done = 1;
+    }
+
+  /* skip control characters */
+  if (c < 32) continue;
+
+  if (header_value_mode)
+    {
+    /* --------- value mode ----------- */
+    /* skip leading whitespace */
+    if ( ((c == '\t') || (c == ' ')) && (header_value_mode == 1) )
+      continue;
+
+      /* we have hit a non-whitespace char, start copying value data */
+      header_value_mode = 2;
+
+      if (c == '"')       /* flip "quoted" mode */
+        header_value_mode = header_value_mode==2 ? 3 : 2;
+
+      /* leave value mode on unquoted ';' */
+      if (header_value_mode == 2 && c == ';') {
+        header_value_mode = 0;
+      };
+      /* -------------------------------- */
+    }
+  else
+    {
+    /* -------- non-value mode -------- */
+    /* skip whitespace + tabs */
+    if ( (c == ' ') || (c == '\t') )
+      continue;
+    if (c == '\\')
+      {
+      /* quote next char. can be used
+      to escape brackets. */
+      if ((c = fgetc(f)) == EOF) break;
+      }
+    else if (c == '(')
+      {
+      header_open_brackets++;
+      continue;
+      }
+    else if ((c == ')') && header_open_brackets)
+      {
+      header_open_brackets--;
+      continue;
+      }
+    else if ( (c == '=') && !header_open_brackets ) /* enter value mode */
+      header_value_mode = 1;
+
+    /* skip chars while we are in a comment */
+    if (header_open_brackets > 0)
+      continue;
+    /* -------------------------------- */
+    }
+
+  /* copy the char to the buffer */
+  header[num_copied++] = (uschar)c;
+
+  /* break if header buffer is full */
+  if (num_copied > MIME_MAX_HEADER_SIZE-1)
+    done = 1;
+  }
+
+if ((num_copied > 0) && (header[num_copied-1] != ';'))
+  header[num_copied-1] = ';';
+
+/* 0-terminate */
+header[num_copied] = '\0';
+
+/* return 0 for EOF or empty line */
+if ((c == EOF) || (num_copied == 1))
+  return 0;
+else
+  return 1;
+}
+
+
+int
+mime_acl_check(uschar *acl, FILE *f, struct mime_boundary_context *context,
+                   uschar **user_msgptr, uschar **log_msgptr)
+{
+int rc = OK;
+uschar *header = NULL;
+struct mime_boundary_context nested_context;
+
+/* reserve a line buffer to work in */
+if (!(header = (uschar *)malloc(MIME_MAX_HEADER_SIZE+1)))
+  {
+  log_write(0, LOG_PANIC,
+       "MIME ACL: can't allocate %d bytes of memory.", MIME_MAX_HEADER_SIZE+1);
+  return DEFER;
+  }
+
+/* Not actually used at the moment, but will be vital to fixing
+ * some RFC 2046 nonconformance later... */
+nested_context.parent = context;
+
+/* loop through parts */
+while(1)
+  {
+  /* reset all per-part mime variables */
+  mime_anomaly_level     = 0;
+  mime_anomaly_text      = NULL;
+  mime_boundary          = NULL;
+  mime_charset           = NULL;
+  mime_decoded_filename  = NULL;
+  mime_filename          = NULL;
+  mime_content_description = NULL;
+  mime_content_disposition = NULL;
+  mime_content_id        = NULL;
+  mime_content_transfer_encoding = NULL;
+  mime_content_type      = NULL;
+  mime_is_multipart      = 0;
+  mime_content_size      = 0;
+
+  /*
+  If boundary is null, we assume that *f is positioned on the start of headers (for example,
+  at the very beginning of a message.
+  If a boundary is given, we must first advance to it to reach the start of the next header
+  block.
+  */
+
+  /* NOTE -- there's an error here -- RFC2046 specifically says to
+   * check for outer boundaries.  This code doesn't do that, and
+   * I haven't fixed this.
+   *
+   * (I have moved partway towards adding support, however, by adding
+   * a "parent" field to my new boundary-context structure.)
+   */
+  if (context != NULL)
+    {
+    while(fgets(CS header, MIME_MAX_HEADER_SIZE, f) != NULL)
+      {
+      /* boundary line must start with 2 dashes */
+      if (Ustrncmp(header,"--",2) == 0)
+        {
+       if (Ustrncmp((header+2),context->boundary,Ustrlen(context->boundary)) == 0)
+         {
+         /* found boundary */
+         if (Ustrncmp((header+2+Ustrlen(context->boundary)),"--",2) == 0)
+           {
+           /* END boundary found */
+           debug_printf("End boundary found %s\n", context->boundary);
+           return rc;
+           }
+         else
+           debug_printf("Next part with boundary %s\n", context->boundary);
+
+         /* can't use break here */
+         goto DECODE_HEADERS;
+         }
+       }
+      }
+    /* Hit EOF or read error. Ugh. */
+    debug_printf("Hit EOF ...\n");
+    return rc;
+    }
+
+DECODE_HEADERS:
+  /* parse headers, set up expansion variables */
+  while (mime_get_header(f,header))
+    {
+    int i;
+    /* loop through header list */
+    for (i = 0; i < mime_header_list_size; i++)
+      {
+      uschar *header_value = NULL;
+      int header_value_len = 0;
+
+      /* found an interesting header? */
+      if (strncmpic(mime_header_list[i].name,header,mime_header_list[i].namelen) == 0)
+       {
+       uschar *p = header + mime_header_list[i].namelen;
+       /* yes, grab the value (normalize to lower case)
+          and copy to its corresponding expansion variable */
+       while(*p != ';')
+         {
+         *p = tolower(*p);
+         p++;
+         }
+       header_value_len = (p - (header + mime_header_list[i].namelen));
+       header_value = (uschar *)malloc(header_value_len+1);
+       memset(header_value,0,header_value_len+1);
+       p = header + mime_header_list[i].namelen;
+       Ustrncpy(header_value, p, header_value_len);
+       debug_printf("Found %s MIME header, value is '%s'\n", mime_header_list[i].name, header_value);
+       *((uschar **)(mime_header_list[i].value)) = header_value;
+
+       /* make p point to the next character after the closing ';' */
+       p += (header_value_len+1);
+
+       /* grab all param=value tags on the remaining line, check if they are interesting */
+NEXT_PARAM_SEARCH:
+       while (*p != 0)
+         {
+         mime_parameter * mp;
+         for (mp = mime_parameter_list;
+              mp < &mime_parameter_list[mime_parameter_list_size];
+              mp++)
+           {
+           uschar *param_value = NULL;
+           int param_value_len = 0;
+
+           /* found an interesting parameter? */
+           if (strncmpic(mp->name, p, mp->namelen) == 0)
+             {
+             uschar *q = p + mp->namelen;
+             int size = 0;
+             int ptr = 0;
+
+             /* yes, grab the value and copy to its corresponding expansion variable */
+             while(*q && *q != ';')            /* ; terminates */
+               {
+               if (*q == '"')
+                 {
+                 q++;                          /* skip leading " */
+                 while(*q && *q != '"')        /* which protects ; */
+                   param_value = string_cat(param_value, &size, &ptr, q++, 1);
+                 if (*q) q++;                  /* skip trailing " */
+                 }
+               else
+                 param_value = string_cat(param_value, &size, &ptr, q++, 1);
+               }
+             param_value[ptr++] = '\0';
+             param_value_len = ptr;
+
+             param_value = rfc2047_decode(param_value, check_rfc2047_length, NULL, 32, &param_value_len, &q);
+             debug_printf("Found %s MIME parameter in %s header, value is '%s'\n", mp->name, mime_header_list[i].name, param_value);
+             *((uschar **)(mp->value)) = param_value;
+             p += (mp->namelen + param_value_len + 1);
+             goto NEXT_PARAM_SEARCH;
+           }
+         }
+         /* There is something, but not one of our interesting parameters.
+            Advance to the next semicolon */
+         while(*p != ';') p++;
+         p++;
+       }
+      }
+    }
+  }
+
+  /* set additional flag variables (easier access) */
+  if ( (mime_content_type != NULL) &&
+       (Ustrncmp(mime_content_type,"multipart",9) == 0) )
+    mime_is_multipart = 1;
+
+  /* Make a copy of the boundary pointer.
+     Required since mime_boundary is global
+     and can be overwritten further down in recursion */
+  nested_context.boundary = mime_boundary;
+
+  /* raise global counter */
+  mime_part_count++;
+
+  /* copy current file handle to global variable */
+  mime_stream = f;
+  mime_current_boundary = context ? context->boundary : 0;
+
+  /* Note the context */
+  mime_is_coverletter = !(context && context->context == MBC_ATTACHMENT);
+
+  /* call ACL handling function */
+  rc = acl_check(ACL_WHERE_MIME, NULL, acl, user_msgptr, log_msgptr);
+
+  mime_stream = NULL;
+  mime_current_boundary = NULL;
+
+  if (rc != OK) break;
+
+  /* If we have a multipart entity and a boundary, go recursive */
+  if ( (mime_content_type != NULL) &&
+       (nested_context.boundary != NULL) &&
+       (Ustrncmp(mime_content_type,"multipart",9) == 0) )
+    {
+    debug_printf("Entering multipart recursion, boundary '%s'\n", nested_context.boundary);
+
+    nested_context.context =
+      context && context->context == MBC_ATTACHMENT
+      ? MBC_ATTACHMENT
+      :    Ustrcmp(mime_content_type,"multipart/alternative") == 0
+       || Ustrcmp(mime_content_type,"multipart/related") == 0
+      ? MBC_COVERLETTER_ALL
+      : MBC_COVERLETTER_ONESHOT;
+
+    rc = mime_acl_check(acl, f, &nested_context, user_msgptr, log_msgptr);
+    if (rc != OK) break;
+    }
+  else if ( (mime_content_type != NULL) &&
+         (Ustrncmp(mime_content_type,"message/rfc822",14) == 0) )
+    {
+    uschar *rfc822name = NULL;
+    uschar filename[2048];
+    int file_nr = 0;
+    int result = 0;
+
+    /* must find first free sequential filename */
+    do
+      {
+      struct stat mystat;
+      (void)string_format(filename, 2048,
+       "%s/scan/%s/__rfc822_%05u", spool_directory, message_id, file_nr++);
+      /* security break */
+      if (file_nr >= 128)
+       goto NO_RFC822;
+      result = stat(CS filename,&mystat);
+      } while (result != -1);
+
+    rfc822name = filename;
+
+    /* decode RFC822 attachment */
+    mime_decoded_filename = NULL;
+    mime_stream = f;
+    mime_current_boundary = context ? context->boundary : NULL;
+    mime_decode(&rfc822name);
+    mime_stream = NULL;
+    mime_current_boundary = NULL;
+    if (!mime_decoded_filename)                /* decoding failed */
+      {
+      log_write(0, LOG_MAIN,
+          "mime_regex acl condition warning - could not decode RFC822 MIME part to file.");
+      return DEFER;
+      }
+    mime_decoded_filename = NULL;
+    }
+
+NO_RFC822:
+  /* If the boundary of this instance is NULL, we are finished here */
+  if (context == NULL) break;
+
+  if (context->context == MBC_COVERLETTER_ONESHOT)
+    context->context = MBC_ATTACHMENT;
+  }
+
+return rc;
+}
+
+#endif /*WITH_CONTENT_SCAN*/
+
+/* vi: sw ai sw=2
+*/
diff --git a/src/mime.h b/src/mime.h
new file mode 100644 (file)
index 0000000..abf68da
--- /dev/null
@@ -0,0 +1,83 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) Tom Kistner <tom@duncanthrax.net> 2004 */
+/* License: GPL */
+
+#ifdef WITH_CONTENT_SCAN
+
+#define MIME_MAX_HEADER_SIZE 8192
+#define MIME_MAX_LINE_LENGTH 32768
+
+#define MBC_ATTACHMENT            0
+#define MBC_COVERLETTER_ONESHOT   1
+#define MBC_COVERLETTER_ALL       2
+
+struct mime_boundary_context
+{
+  struct mime_boundary_context *parent;
+  unsigned char *boundary;
+  int context;
+};
+
+typedef struct mime_header {
+  uschar *name;
+  int    namelen;
+  void   *value;
+} mime_header;
+
+static mime_header mime_header_list[] = {
+  { US"content-type:", 13, &mime_content_type },
+  { US"content-disposition:", 20, &mime_content_disposition },
+  { US"content-transfer-encoding:", 26, &mime_content_transfer_encoding },
+  { US"content-id:", 11, &mime_content_id },
+  { US"content-description:", 20 , &mime_content_description }
+};
+
+static int mime_header_list_size = sizeof(mime_header_list)/sizeof(mime_header);
+
+
+
+typedef struct mime_parameter {
+  uschar *name;
+  int    namelen;
+  void   *value;
+} mime_parameter;
+
+static mime_parameter mime_parameter_list[] = {
+  { US"name=", 5, &mime_filename },
+  { US"filename=", 9, &mime_filename },
+  { US"charset=", 8, &mime_charset },
+  { US"boundary=", 9, &mime_boundary }
+};
+
+static int mime_parameter_list_size = sizeof(mime_parameter_list)/sizeof(mime_parameter);
+
+
+/* MIME Anomaly list */
+#define MIME_ANOMALY_BROKEN_BASE64    2, "Broken BASE64 encoding detected"
+#define MIME_ANOMALY_BROKEN_QP        1, "Broken Quoted-Printable encoding detected"
+
+
+/* BASE64 decoder matrix */
+static unsigned char mime_b64[256]={
+/*   0 */  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,
+/*  16 */  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,
+/*  32 */  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,   62,  128,  128,  128,   63,
+/*  48 */   52,   53,   54,   55,   56,   57,   58,   59,   60,   61,  128,  128,  128,  255,  128,  128,
+/*  64 */  128,    0,    1,    2,    3,    4,    5,    6,    7,    8,    9,   10,   11,   12,   13,   14,
+/*  80 */   15,   16,   17,   18,   19,   20,   21,   22,   23,   24,   25,  128,  128,  128,  128,  128,
+/*  96 */  128,   26,   27,   28,   29,   30,   31,   32,   33,   34,   35,   36,   37,   38,   39,   40,
+/* 112 */   41,   42,   43,   44,   45,   46,   47,   48,   49,   50,   51,  128,  128,  128,  128,  128,
+/* 128 */  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,
+/* 144 */  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,
+/* 160 */  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,
+/* 176 */  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,
+/* 192 */  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,
+/* 208 */  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,
+/* 224 */  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,
+/* 240 */  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128,  128
+};
+
+#endif
diff --git a/src/moan.c b/src/moan.c
new file mode 100644 (file)
index 0000000..4d7b51b
--- /dev/null
@@ -0,0 +1,761 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2014 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Functions for sending messages to sender or to mailmaster. */
+
+
+#include "exim.h"
+
+
+
+/*************************************************
+*            Write From: line for DSN            *
+*************************************************/
+
+/* This function is called to write the From: line in automatically generated
+messages - bounces, warnings, etc. It expands a configuration item in order to
+get the text. If the expansion fails, a panic is logged and the default value
+for the option is used.
+
+Argument:   the FILE to write to
+Returns:    nothing
+*/
+
+void
+moan_write_from(FILE *f)
+{
+uschar *s = expand_string(dsn_from);
+if (s == NULL)
+  {
+  log_write(0, LOG_MAIN|LOG_PANIC,
+    "Failed to expand dsn_from (using default): %s", expand_string_message);
+  s = expand_string(US DEFAULT_DSN_FROM);
+  }
+fprintf(f, "From: %s\n", s);
+}
+
+
+
+/*************************************************
+*              Send error message                *
+*************************************************/
+
+/* This function sends an error message by opening a pipe to a new process
+running Exim, and writing a message to it using the "-t" option. This is not
+used for delivery failures, which have their own code for handing failed
+addresses.
+
+Arguments:
+  recipient      addressee for the message
+  ident          identifies the type of error
+  eblock         chain of error_blocks containing data about the error
+  headers        the message's headers
+  message_file   FILE containing the body of the message
+  firstline      contains first line of file, if it was read to check for
+                   "From ", but it turned out not to be
+
+Returns:         TRUE if message successfully sent
+*/
+
+static BOOL
+moan_send_message(uschar *recipient, int ident, error_block *eblock,
+  header_line *headers, FILE *message_file, uschar *firstline)
+{
+int written = 0;
+int fd;
+int status;
+int count = 0;
+int size_limit = bounce_return_size_limit;
+FILE *f;
+int pid = child_open_exim(&fd);
+
+/* Creation of child failed */
+
+if (pid < 0)
+  {
+  DEBUG(D_any) debug_printf("Failed to create child to send message: %s\n",
+    strerror(errno));
+  return FALSE;
+  }
+else DEBUG(D_any) debug_printf("Child process %d for sending message\n", pid);
+
+/* Creation of child succeeded */
+
+f = fdopen(fd, "wb");
+if (errors_reply_to != NULL) fprintf(f, "Reply-To: %s\n", errors_reply_to);
+fprintf(f, "Auto-Submitted: auto-replied\n");
+moan_write_from(f);
+fprintf(f, "To: %s\n", recipient);
+
+switch(ident)
+  {
+  case ERRMESS_BADARGADDRESS:
+  fprintf(f,
+  "Subject: Mail failure - malformed recipient address\n\n");
+  fprintf(f,
+  "A message that you sent contained a recipient address that was incorrectly\n"
+  "constructed:\n\n");
+  fprintf(f, "  %s  %s\n", eblock->text1, eblock->text2);
+  count = Ustrlen(eblock->text1);
+  if (count > 0 && eblock->text1[count-1] == '.')
+    fprintf(f,
+    "\nRecipient addresses must not end with a '.' character.\n");
+  fprintf(f,
+  "\nThe message has not been delivered to any recipients.\n");
+  break;
+
+  case ERRMESS_BADNOADDRESS:
+  case ERRMESS_BADADDRESS:
+  fprintf(f,
+  "Subject: Mail failure - malformed recipient address\n\n");
+  fprintf(f,
+  "A message that you sent contained one or more recipient addresses that were\n"
+  "incorrectly constructed:\n\n");
+
+  while (eblock != NULL)
+    {
+    fprintf(f, "  %s: %s\n", eblock->text1, eblock->text2);
+    count++;
+    eblock = eblock->next;
+    }
+
+  fprintf(f, (count == 1)? "\nThis address has been ignored. " :
+    "\nThese addresses have been ignored. ");
+
+  fprintf(f, (ident == ERRMESS_BADADDRESS)?
+  "The other addresses in the message were\n"
+  "syntactically valid and have been passed on for an attempt at delivery.\n" :
+
+  "There were no other addresses in your\n"
+  "message, and so no attempt at delivery was possible.\n");
+  break;
+
+  case ERRMESS_IGADDRESS:
+  fprintf(f, "Subject: Mail failure - no recipient addresses\n\n");
+  fprintf(f,
+  "A message that you sent using the -t command line option contained no\n"
+  "addresses that were not also on the command line, and were therefore\n"
+  "suppressed. This left no recipient addresses, and so no delivery could\n"
+  "be attempted.\n");
+  break;
+
+  case ERRMESS_NOADDRESS:
+  fprintf(f, "Subject: Mail failure - no recipient addresses\n\n");
+  fprintf(f,
+  "A message that you sent contained no recipient addresses, and therefore no\n"
+  "delivery could be attempted.\n");
+  break;
+
+  case ERRMESS_IOERR:
+  fprintf(f, "Subject: Mail failure - system failure\n\n");
+  fprintf(f,
+  "A system failure was encountered while processing a message that you sent,\n"
+  "so it has not been possible to deliver it. The error was:\n\n%s\n",
+    eblock->text1);
+  break;
+
+  case ERRMESS_VLONGHEADER:
+  fprintf(f, "Subject: Mail failure - overlong header section\n\n");
+  fprintf(f,
+  "A message that you sent contained a header section that was excessively\n"
+  "long and could not be handled by the mail transmission software. The\n"
+  "message has not been delivered to any recipients.\n");
+  break;
+
+  case ERRMESS_VLONGHDRLINE:
+  fprintf(f, "Subject: Mail failure - overlong header line\n\n");
+  fprintf(f,
+  "A message that you sent contained a header line that was excessively\n"
+  "long and could not be handled by the mail transmission software. The\n"
+  "message has not been delivered to any recipients.\n");
+  break;
+
+  case ERRMESS_TOOBIG:
+  fprintf(f, "Subject: Mail failure - message too big\n\n");
+  fprintf(f,
+  "A message that you sent was longer than the maximum size allowed on this\n"
+  "system. It was not delivered to any recipients.\n");
+  break;
+
+  case ERRMESS_TOOMANYRECIP:
+  fprintf(f, "Subject: Mail failure - too many recipients\n\n");
+  fprintf(f,
+  "A message that you sent contained more recipients than allowed on this\n"
+  "system. It was not delivered to any recipients.\n");
+  break;
+
+  case ERRMESS_LOCAL_SCAN:
+  case ERRMESS_LOCAL_ACL:
+  fprintf(f, "Subject: Mail failure - rejected by local scanning code\n\n");
+  fprintf(f,
+  "A message that you sent was rejected by the local scanning code that\n"
+  "checks incoming messages on this system.");
+  if (eblock->text1 != NULL)
+    {
+    fprintf(f,
+    " The following error was given:\n\n  %s", eblock->text1);
+    }
+  fprintf(f, "\n");
+  break;
+
+#ifdef EXPERIMENTAL_DMARC
+  case ERRMESS_DMARC_FORENSIC:
+  bounce_return_message = TRUE;
+  bounce_return_body    = FALSE;
+  fprintf(f,
+          "Subject: DMARC Forensic Report for %s from IP %s\n\n",
+         ((eblock == NULL) ? US"Unknown" : eblock->text2),
+          sender_host_address);
+  fprintf(f,
+  "A message claiming to be from you has failed the published DMARC\n"
+  "policy for your domain.\n\n");
+  while (eblock != NULL)
+    {
+    fprintf(f, "  %s: %s\n", eblock->text1, eblock->text2);
+    count++;
+    eblock = eblock->next;
+    }
+  break;
+#endif
+
+  default:
+  fprintf(f, "Subject: Mail failure\n\n");
+  fprintf(f,
+  "A message that you sent has caused the error routine to be entered with\n"
+  "an unknown error number (%d).\n", ident);
+  break;
+  }
+
+/* Now, if configured, copy the message; first the headers and then the rest of
+the input if available, up to the configured limit, if the option for including
+message bodies in bounces is set. */
+
+if (bounce_return_message)
+  {
+  if (bounce_return_body)
+    {
+    fprintf(f, "\n"
+      "------ This is a copy of your message, including all the headers.");
+    if (size_limit == 0 || size_limit > thismessage_size_limit)
+      size_limit = thismessage_size_limit;
+    if (size_limit > 0 && size_limit < message_size)
+      {
+      int x = size_limit;
+      uschar *k = US"";
+      if ((x & 1023) == 0)
+        {
+        k = US"K";
+        x >>= 10;
+        }
+      fprintf(f, "\n"
+        "------ No more than %d%s characters of the body are included.\n\n",
+          x, k);
+      }
+    else fprintf(f, " ------\n\n");
+    }
+  else
+    {
+    fprintf(f, "\n"
+      "------ This is a copy of the headers that were received before the "
+      "error\n       was detected.\n\n");
+    }
+
+  /* If the error occurred before the Received: header was created, its text
+  field will still be NULL; just omit such a header line. */
+
+  while (headers != NULL)
+    {
+    if (headers->text != NULL) fprintf(f, "%s", CS headers->text);
+    headers = headers->next;
+    }
+
+  if (ident != ERRMESS_VLONGHEADER && ident != ERRMESS_VLONGHDRLINE)
+    fputc('\n', f);
+
+  /* After early detection of an error, the message file may be STDIN,
+  in which case we might have to terminate on a line containing just "."
+  as well as on EOF. We may already have the first line in memory. */
+
+  if (bounce_return_body && message_file != NULL)
+    {
+    int ch;
+    int state = 1;
+    BOOL enddot = dot_ends && message_file == stdin;
+    if (firstline != NULL) fprintf(f, "%s", CS firstline);
+    while ((ch = fgetc(message_file)) != EOF)
+      {
+      fputc(ch, f);
+      if (size_limit > 0 && ++written > size_limit) break;
+      if (enddot)
+        {
+        if (state == 0) { if (ch == '\n') state = 1; }
+        else if (state == 1)
+          { if (ch == '.') state = 2; else if (ch != '\n') state = 0; }
+        else
+          { if (ch == '\n') break; else state = 0; }
+        }
+      }
+    }
+#ifdef EXPERIMENTAL_DMARC
+  /* Overkill, but use exact test in case future code gets inserted */
+  else if (bounce_return_body && message_file == NULL)
+    {
+    /* This doesn't print newlines, disable until can parse and fix
+     * output to be legible.  */
+    fprintf(f, "%s", expand_string(US"$message_body"));
+    }
+#endif
+  }
+/* Close the file, which should send an EOF to the child process
+that is receiving the message. Wait for it to finish, without a timeout. */
+
+(void)fclose(f);
+status = child_close(pid, 0);  /* Waits for child to close */
+if (status != 0)
+  {
+  uschar *msg = US"Child mail process returned status";
+  if (status == -257)
+    log_write(0, LOG_MAIN, "%s %d: errno=%d: %s", msg, status, errno,
+      strerror(errno));
+  else
+    log_write(0, LOG_MAIN, "%s %d", msg, status);
+  return FALSE;
+  }
+
+return TRUE;
+}
+
+
+
+/*************************************************
+*          Send message to sender                *
+*************************************************/
+
+/* This function is called when errors are detected during the receipt of a
+message. Delivery failures are handled separately in deliver.c.
+
+If there is a valid sender_address, and the failing message is not a local
+error message, then this function calls moan_send_message to send a message to
+that person. If the sender's address is null, then an error has occurred with a
+message that was generated by a mailer daemon. All we can do is to write
+information to log files. The same action is taken if local_error_message is
+set - this can happen for non null-senders in certain configurations where exim
+doesn't run setuid root.
+
+Arguments:
+  ident         identifies the particular error
+  eblock        chain of error_blocks containing data about the error
+  headers       message's headers (chain)
+  message_file  a FILE where the body of the message can be read
+  check_sender  if TRUE, read the first line of the file for a possible
+                  "From " sender (if a trusted caller)
+
+Returns:        FALSE if there is no sender_address to send to;
+                else the return from moan_send_message()
+*/
+
+BOOL
+moan_to_sender(int ident, error_block *eblock, header_line *headers,
+  FILE *message_file, BOOL check_sender)
+{
+uschar *firstline = NULL;
+uschar *msg = US"Error while reading message with no usable sender address";
+
+if (message_reference != NULL)
+  msg = string_sprintf("%s (R=%s)", msg, message_reference);
+
+/* Find the sender from a From line if permitted and possible */
+
+if (check_sender && message_file != NULL && trusted_caller &&
+    Ufgets(big_buffer, BIG_BUFFER_SIZE, message_file) != NULL)
+  {
+  uschar *new_sender = NULL;
+  if (regex_match_and_setup(regex_From, big_buffer, 0, -1))
+    new_sender = expand_string(uucp_from_sender);
+  if (new_sender != NULL) sender_address = new_sender;
+    else firstline = big_buffer;
+  }
+
+/* If viable sender address, send a message */
+
+if (sender_address != NULL && sender_address[0] != 0 && !local_error_message)
+  return moan_send_message(sender_address, ident, eblock, headers,
+    message_file, firstline);
+
+/* Otherwise, we can only log */
+
+switch(ident)
+  {
+  case ERRMESS_BADARGADDRESS:
+  case ERRMESS_BADNOADDRESS:
+  case ERRMESS_BADADDRESS:
+  log_write(0, LOG_MAIN, "%s: at least one malformed recipient address: "
+    "%s - %s", msg, eblock->text1, eblock->text2);
+  break;
+
+  case ERRMESS_IGADDRESS:
+  case ERRMESS_NOADDRESS:
+  log_write(0, LOG_MAIN, "%s: no recipient addresses", msg);
+  break;
+
+  /* This error has already been logged. */
+  case ERRMESS_IOERR:
+  break;
+
+  case ERRMESS_VLONGHEADER:
+  log_write(0, LOG_MAIN, "%s: excessively long message header section read "
+    "(more than %d characters)", msg, header_maxsize);
+  break;
+
+  case ERRMESS_VLONGHDRLINE:
+  log_write(0, LOG_MAIN, "%s: excessively long message header line read "
+    "(more than %d characters)", msg, header_line_maxsize);
+  break;
+
+  case ERRMESS_TOOBIG:
+  log_write(0, LOG_MAIN, "%s: message too big (limit set to %d)", msg,
+    thismessage_size_limit);
+  break;
+
+  case ERRMESS_TOOMANYRECIP:
+  log_write(0, LOG_MAIN, "%s: too many recipients (max set to %d)", msg,
+    recipients_max);
+  break;
+
+  case ERRMESS_LOCAL_SCAN:
+  log_write(0, LOG_MAIN, "%s: rejected by local_scan: %s", msg, eblock->text1);
+  break;
+
+  case ERRMESS_LOCAL_ACL:
+  log_write(0, LOG_MAIN, "%s: rejected by non-SMTP ACL: %s", msg, eblock->text1);
+  break;
+
+  default:
+  log_write(0, LOG_MAIN|LOG_PANIC, "%s: unknown error number %d", msg,
+    ident);
+  break;
+  }
+
+return FALSE;
+}
+
+
+
+/*************************************************
+*            Send message to someone             *
+*************************************************/
+
+/* This is called when exim is configured to tell someone (often the
+mailmaster) about some incident.
+
+Arguments:
+  who           address to send mail to
+  addr          chain of deferred addresses whose details are to be included
+  subject       subject text for the message
+  format        a printf() format for the body of the message
+  ...           arguments for the format
+
+Returns:        nothing
+*/
+
+void
+moan_tell_someone(uschar *who, address_item *addr,
+  const uschar *subject, const char *format, ...)
+{
+FILE *f;
+va_list ap;
+int fd;
+int pid = child_open_exim(&fd);
+
+if (pid < 0)
+  {
+  DEBUG(D_any) debug_printf("Failed to create child to send message: %s\n",
+    strerror(errno));
+  return;
+  }
+
+f = fdopen(fd, "wb");
+fprintf(f, "Auto-Submitted: auto-replied\n");
+moan_write_from(f);
+fprintf(f, "To: %s\n", who);
+fprintf(f, "Subject: %s\n\n", subject);
+va_start(ap, format);
+vfprintf(f, format, ap);
+va_end(ap);
+
+if (addr != NULL)
+  {
+  fprintf(f, "\nThe following address(es) have yet to be delivered:\n");
+  for (; addr != NULL; addr = addr->next)
+    {
+    uschar *parent = (addr->parent == NULL)? NULL : addr->parent->address;
+    fprintf(f, "  %s", addr->address);
+    if (parent != NULL) fprintf(f, " <%s>", parent);
+    if (addr->basic_errno > 0) fprintf(f, ": %s", strerror(addr->basic_errno));
+    if (addr->message != NULL) fprintf(f, ": %s", addr->message);
+    fprintf(f, "\n");
+    }
+  }
+
+(void)fclose(f);
+child_close(pid, 0);  /* Waits for child to close; no timeout */
+}
+
+
+
+/*************************************************
+*            Handle SMTP batch error             *
+*************************************************/
+
+/* This is called when something goes wrong in batched (-bS) SMTP input.
+Information is written to stdout and/or stderr, and Exim exits with a non-zero
+completion code. BSMTP is almost always called by some other program, so it is
+up to that program to interpret the return code and do something with the error
+information, and also to preserve the batch input file for human analysis.
+
+Formerly, Exim used to attempt to continue after some errors, but this strategy
+has been abandoned as it can lead to loss of messages.
+
+Arguments:
+  cmd_buffer   the command causing the error, or NULL
+  format       a printf() format
+  ...          arguments for the format
+
+Returns:       does not return; exits from the program
+               exit code = 1 if some messages were accepted
+               exit code = 2 if no messages were accepted
+*/
+
+void
+moan_smtp_batch(uschar *cmd_buffer, const char *format, ...)
+{
+va_list ap;
+int yield = (receive_messagecount > 0)? 1 : 2;
+
+DEBUG(D_any) debug_printf("Handling error in batched SMTP input\n");
+
+/* On stdout, write stuff that a program could parse fairly easily. */
+
+va_start(ap, format);
+vfprintf(stdout, format, ap);
+va_end(ap);
+
+fprintf(stdout, "\nTransaction started in line %d\n",
+  bsmtp_transaction_linecount);
+fprintf(stdout,   "Error detected in line %d\n", receive_linecount);
+if (cmd_buffer != NULL) fprintf(stdout, "%s\n", cmd_buffer);
+
+/* On stderr, write stuff for human consumption */
+
+fprintf(stderr,
+  "An error was detected while processing a file of BSMTP input.\n"
+  "The error message was:\n\n  ");
+
+va_start(ap, format);
+vfprintf(stderr, format, ap);
+va_end(ap);
+
+fprintf(stderr,
+  "\n\nThe SMTP transaction started in line %d.\n"
+      "The error was detected in line %d.\n",
+  bsmtp_transaction_linecount, receive_linecount);
+
+if (cmd_buffer != NULL)
+  {
+  fprintf(stderr, "The SMTP command at fault was:\n\n   %s\n\n",
+    cmd_buffer);
+  }
+
+fprintf(stderr, "%d previous message%s successfully processed.\n",
+  receive_messagecount, (receive_messagecount == 1)? " was" : "s were");
+
+fprintf(stderr, "The rest of the batch was abandoned.\n");
+
+exim_exit(yield);
+}
+
+
+
+
+/*************************************************
+*         Check for error copies                 *
+*************************************************/
+
+/* This function is passed the recipient of an error message, and must check
+the error_copies string to see whether there is an additional recipient list to
+which errors for this recipient must be bcc'd. The incoming recipient is always
+fully qualified.
+
+Argument:   recipient address
+Returns:    additional recipient list or NULL
+*/
+
+uschar *
+moan_check_errorcopy(uschar *recipient)
+{
+uschar *item, *localpart, *domain;
+uschar *listptr = errors_copy;
+uschar *yield = NULL;
+uschar buffer[256];
+int sep = 0;
+int llen;
+
+if (errors_copy == NULL) return NULL;
+
+/* Set up pointer to the local part and domain, and compute the
+length of the local part. */
+
+localpart = recipient;
+domain = Ustrrchr(recipient, '@');
+if (domain == NULL) return NULL;  /* should not occur, but avoid crash */
+llen = domain++ - recipient;
+
+/* Scan through the configured items */
+
+while ((item = string_nextinlist(&listptr, &sep, buffer, sizeof(buffer)))
+       != NULL)
+  {
+  uschar *newaddress = item;
+  uschar *pattern = string_dequote(&newaddress);
+
+  /* If no new address found, just skip this item. */
+
+  while (isspace(*newaddress)) newaddress++;
+  if (*newaddress == 0) continue;
+
+  /* We now have an item to match as an address in item, and the additional
+  address in newaddress. If the pattern matches, expand the new address string
+  and return it. During expansion, make local part and domain available for
+  insertion. This requires a copy to be made; we can't just temporarily
+  terminate it, as the whole address is required for $0. */
+
+  if (match_address_list(recipient, TRUE, TRUE, &pattern, NULL, 0, UCHAR_MAX+1,
+        NULL) == OK)
+    {
+    deliver_localpart = string_copyn(localpart, llen);
+    deliver_domain = domain;
+    yield = expand_string_copy(newaddress);
+    deliver_domain = deliver_localpart = NULL;
+    if (yield == NULL)
+      log_write(0, LOG_MAIN|LOG_PANIC, "Failed to expand %s when processing "
+        "errors_copy: %s", newaddress, expand_string_message);
+    break;
+    }
+  }
+
+DEBUG(D_any) debug_printf("errors_copy check returned %s\n",
+  (yield == NULL)? US"NULL" : yield);
+
+expand_nmax = -1;
+return yield;
+}
+
+
+
+/************************************************
+*        Handle skipped syntax errors           *
+************************************************/
+
+/* This function is called by the redirect router when it has skipped over one
+or more syntax errors in the list of addresses. If there is an address to mail
+to, send a message, and always write the information to the log. In the case of
+a filter file, a "syntax error" might actually be something else, such as the
+inability to open a log file. Thus, the wording of the error message is
+general.
+
+Arguments:
+  rname             the router name
+  eblock            chain of error blocks
+  syntax_errors_to  address to send mail to, or NULL
+  some              TRUE if some addresses were generated; FALSE if none were
+  custom            custom message text
+
+Returns:            FALSE if string expansion failed; TRUE otherwise
+*/
+
+BOOL
+moan_skipped_syntax_errors(uschar *rname, error_block *eblock,
+  uschar *syntax_errors_to, BOOL some, uschar *custom)
+{
+int pid, fd;
+uschar *s, *t;
+FILE *f;
+error_block *e;
+
+for (e = eblock; e != NULL; e = e->next)
+  {
+  if (e->text2 != NULL)
+    log_write(0, LOG_MAIN, "%s router: skipped error: %s in \"%s\"",
+      rname, e->text1, e->text2);
+  else
+    log_write(0, LOG_MAIN, "%s router: skipped error: %s", rname,
+      e->text1);
+  }
+
+if (syntax_errors_to == NULL) return TRUE;
+
+s = expand_string(syntax_errors_to);
+if (s == NULL)
+  {
+  log_write(0, LOG_MAIN, "%s router failed to expand %s: %s", rname,
+    syntax_errors_to, expand_string_message);
+  return FALSE;
+  }
+
+/* If we can't create a process to send the message, just forget about
+it. */
+
+pid = child_open_exim(&fd);
+
+if (pid < 0)
+  {
+  DEBUG(D_any) debug_printf("Failed to create child to send message: %s\n",
+    strerror(errno));
+  return TRUE;
+  }
+
+f = fdopen(fd, "wb");
+fprintf(f, "Auto-Submitted: auto-replied\n");
+moan_write_from(f);
+fprintf(f, "To: %s\n", s);
+fprintf(f, "Subject: error(s) in forwarding or filtering\n\n");
+
+if (custom != NULL)
+  {
+  t = expand_string(custom);
+  if (t == NULL)
+    {
+    log_write(0, LOG_MAIN, "%s router failed to expand %s: %s", rname,
+      custom, expand_string_message);
+    return FALSE;
+    }
+  fprintf(f, "%s\n\n", t);
+  }
+
+fprintf(f, "The %s router encountered the following error(s):\n\n",
+  rname);
+
+for (e = eblock; e != NULL; e = e->next)
+  {
+  fprintf(f, "  %s", e->text1);
+  if (e->text2 != NULL)
+    fprintf(f, " in the address\n  \"%s\"", e->text2);
+  fprintf(f, "\n\n");
+  }
+
+if (some)
+  fprintf(f, "Other addresses were processed normally.\n");
+else
+  fprintf(f, "No valid addresses were generated.\n");
+
+(void)fclose(f);
+child_close(pid, 0);  /* Waits for child to close; no timeout */
+
+return TRUE;
+}
+
+/* End of moan.c */
diff --git a/src/mytypes.h b/src/mytypes.h
new file mode 100644 (file)
index 0000000..964abf8
--- /dev/null
@@ -0,0 +1,117 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2012 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+
+/* This header file contains type definitions and macros that I use as
+"standard" in the code of Exim and its utilities. Make it idempotent because
+local_scan.h includes it and exim.h includes them both (to get this earlier). */
+
+#ifndef MYTYPES_H
+#define MYTYPES_H
+
+#ifndef FALSE
+#define FALSE         0
+#endif
+
+#ifndef TRUE
+#define TRUE          1
+#endif
+
+#ifndef TRUE_UNSET
+#define TRUE_UNSET    2
+#endif
+
+
+/* If gcc is being used to compile Exim, we can use its facility for checking
+the arguments of printf-like functions. This is done by a macro. */
+
+#if defined(__GNUC__) || defined(__clang__)
+#define PRINTF_FUNCTION(A,B)  __attribute__((format(printf,A,B)))
+#define ARG_UNUSED  __attribute__((__unused__))
+#else
+#define PRINTF_FUNCTION(A,B)
+#define ARG_UNUSED  /**/
+#endif
+
+#ifdef WANT_DEEPER_PRINTF_CHECKS
+#define ALMOST_PRINTF(A, B) PRINTF_FUNCTION(A, B)
+#else
+#define ALMOST_PRINTF(A, B)
+#endif
+
+
+/* Some operating systems (naughtily, imo) include a definition for "uchar" in
+the standard header files, so we use "uschar". Solaris has u_char in
+sys/types.h. This is just a typing convenience, of course. */
+
+typedef unsigned char uschar;
+typedef int BOOL;
+/* We also have SIGNAL_BOOL, which requires signal.h be included, so is defined
+elsewhere */
+
+
+/* These macros save typing for the casting that is needed to cope with the
+mess that is "char" in ISO/ANSI C. Having now been bitten enough times by
+systems where "char" is actually signed, I've converted Exim to use entirely
+unsigned chars, except in a few special places such as arguments that are
+almost always literal strings. */
+
+#define CS   (char *)
+#define CCS  (const char *)
+#define CSS  (char **)
+#define US   (unsigned char *)
+#define CUS  (const unsigned char *)
+#define USS  (unsigned char **)
+
+/* The C library string functions expect "char *" arguments. Use macros to
+avoid having to write a cast each time. We do this for string and file
+functions that are called quite often; for other calls to external libraries
+(which are on the whole special-purpose) we just use individual casts. */
+
+#define Uatoi(s)           atoi(CCS(s))
+#define Uatol(s)           atol(CCS(s))
+#define Uchdir(s)          chdir(CCS(s))
+#define Uchmod(s,n)        chmod(CCS(s),n)
+#define Uchown(s,n,m)      chown(CCS(s),n,m)
+#define Ufgets(b,n,f)      fgets(CS(b),n,f)
+#define Ufopen(s,t)        fopen(CCS(s),CCS(t))
+#define Ulink(s,t)         link(CCS(s),CCS(t))
+#define Ulstat(s,t)        lstat(CCS(s),t)
+
+#ifdef O_BINARY                                        /* This is for Cygwin,  */
+#define Uopen(s,n,m)       open(CCS(s),(n)|O_BINARY,m) /* where all files must */
+#else                                                  /* be opened as binary  */
+#define Uopen(s,n,m)       open(CCS(s),n,m)            /* to avoid problems    */
+#endif                                                 /* with CRLF endings.   */
+#define Uread(f,b,l)       read(f,CS(b),l)
+#define Urename(s,t)       rename(CCS(s),CCS(t))
+#define Ustat(s,t)         stat(CCS(s),t)
+#define Ustrcat(s,t)       strcat(CS(s),CCS(t))
+#define Ustrchr(s,n)       US strchr(CCS(s),n)
+#define CUstrchr(s,n)      CUS strchr(CCS(s),n)
+#define CUstrerror(n)      CUS strerror(n)
+#define Ustrcmp(s,t)       strcmp(CCS(s),CCS(t))
+#define Ustrcpy(s,t)       strcpy(CS(s),CCS(t))
+#define Ustrcspn(s,t)      strcspn(CCS(s),CCS(t))
+#define Ustrftime(s,m,f,t) strftime(CS(s),m,f,t)
+#define Ustrlen(s)         (int)strlen(CCS(s))
+#define Ustrncat(s,t,n)    strncat(CS(s),CCS(t),n)
+#define Ustrncmp(s,t,n)    strncmp(CCS(s),CCS(t),n)
+#define Ustrncpy(s,t,n)    strncpy(CS(s),CCS(t),n)
+#define Ustrpbrk(s,t)      strpbrk(CCS(s),CCS(t))
+#define Ustrrchr(s,n)      US strrchr(CCS(s),n)
+#define CUstrrchr(s,n)     CUS strrchr(CCS(s),n)
+#define Ustrspn(s,t)       strspn(CCS(s),CCS(t))
+#define Ustrstr(s,t)       US strstr(CCS(s),CCS(t))
+#define CUstrstr(s,t)      CUS strstr(CCS(s),CCS(t))
+#define Ustrtod(s,t)       strtod(CCS(s),CSS(t))
+#define Ustrtol(s,t,b)     strtol(CCS(s),CSS(t),b)
+#define Ustrtoul(s,t,b)    strtoul(CCS(s),CSS(t),b)
+#define Uunlink(s)         unlink(CCS(s))
+#endif
+
+/* End of mytypes.h */
diff --git a/src/os.c b/src/os.c
new file mode 100644 (file)
index 0000000..6e02b8f
--- /dev/null
+++ b/src/os.c
@@ -0,0 +1,917 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2012 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+#ifdef STAND_ALONE
+#include <signal.h>
+#include <stdio.h>
+#include <time.h>
+#endif
+
+/* This source file contains "default" system-dependent functions which
+provide functionality (or lack of it) in cases where the OS-specific os.c
+file has not. Some of them are tailored by macros defined in os.h files. */
+
+
+#ifndef OS_RESTARTING_SIGNAL
+/*************************************************
+*          Set up restarting signal              *
+*************************************************/
+
+/* This function has the same functionality as the ANSI C signal() function,
+except that it arranges that, if the signal happens during a system call, the
+system call gets restarted. (Also, it doesn't return a result.) Different
+versions of Unix have different defaults, and different ways of setting up a
+restarting signal handler. If the functionality is not available, the signal
+should be set to be ignored. This function is used only for catching SIGUSR1.
+*/
+
+void
+os_restarting_signal(int sig, void (*handler)(int))
+{
+/* Many systems have the SA_RESTART sigaction for specifying that a signal
+should restart system calls. These include SunOS5, AIX, BSDI, IRIX, FreeBSD,
+OSF1, Linux and HP-UX 10 (but *not* HP-UX 9). */
+
+#ifdef SA_RESTART
+struct sigaction act;
+act.sa_handler = handler;
+sigemptyset(&(act.sa_mask));
+act.sa_flags = SA_RESTART;
+sigaction(sig, &act, NULL);
+
+#ifdef STAND_ALONE
+printf("Used SA_RESTART\n");
+#endif
+
+/* SunOS4 and Ultrix default to non-interruptable signals, with SV_INTERRUPT
+for making them interruptable. This seems to be a dying fashion. */
+
+#elif defined SV_INTERRUPT
+signal(sig, handler);
+
+#ifdef STAND_ALONE
+printf("Used default signal()\n");
+#endif
+
+
+/* If neither SA_RESTART nor SV_INTERRUPT is available we don't know how to
+set up a restarting signal, so simply suppress the facility. */
+
+#else
+signal(sig, SIG_IGN);
+
+#ifdef STAND_ALONE
+printf("Used SIG_IGN\n");
+#endif
+
+#endif
+}
+
+#endif  /* OS_RESTARTING_SIGNAL */
+
+
+#ifndef OS_NON_RESTARTING_SIGNAL
+/*************************************************
+*          Set up non-restarting signal          *
+*************************************************/
+
+/* This function has the same functionality as the ANSI C signal() function,
+except that it arranges that, if the signal happens during a system call, the
+system call gets interrupted. (Also, it doesn't return a result.) Different
+versions of Unix have different defaults, and different ways of setting up a
+non-restarting signal handler. For systems for which we don't know what to do,
+just use the normal signal() function and hope for the best. */
+
+void
+os_non_restarting_signal(int sig, void (*handler)(int))
+{
+/* Many systems have the SA_RESTART sigaction for specifying that a signal
+should restart system calls. These include SunOS5, AIX, BSDI, IRIX, FreeBSD,
+OSF1, Linux and HP-UX 10 (but *not* HP-UX 9). */
+
+#ifdef SA_RESTART
+struct sigaction act;
+act.sa_handler = handler;
+sigemptyset(&(act.sa_mask));
+act.sa_flags = 0;
+sigaction(sig, &act, NULL);
+
+#ifdef STAND_ALONE
+printf("Used sigaction() with flags = 0\n");
+#endif
+
+/* SunOS4 and Ultrix default to non-interruptable signals, with SV_INTERRUPT
+for making them interruptable. This seems to be a dying fashion. */
+
+#elif defined SV_INTERRUPT
+struct sigvec sv;
+sv.sv_handler = handler;
+sv.sv_flags = SV_INTERRUPT;
+sv.sv_mask = -1;
+sigvec(sig, &sv, NULL);
+
+#ifdef STAND_ALONE
+printf("Used sigvec() with flags = SV_INTERRUPT\n");
+#endif
+
+/* If neither SA_RESTART nor SV_INTERRUPT is available we don't know how to
+set up a restarting signal, so just use the standard signal() function. */
+
+#else
+signal(sig, handler);
+
+#ifdef STAND_ALONE
+printf("Used default signal()\n");
+#endif
+
+#endif
+}
+
+#endif  /* OS_NON_RESTARTING_SIGNAL */
+
+
+
+#ifdef STRERROR_FROM_ERRLIST
+/*************************************************
+*     Provide strerror() for non-ANSI libraries  *
+*************************************************/
+
+/* Some old-fashioned systems still around (e.g. SunOS4) don't have strerror()
+in their libraries, but can provide the same facility by this simple
+alternative function. */
+
+char *
+strerror(int n)
+{
+if (n < 0 || n >= sys_nerr) return "unknown error number";
+return sys_errlist[n];
+}
+#endif /* STRERROR_FROM_ERRLIST */
+
+
+
+#ifndef OS_STRSIGNAL
+/*************************************************
+*      Provide strsignal() for systems without   *
+*************************************************/
+
+/* Some systems have strsignal() to turn signal numbers into names; others
+may have other means of doing this. This function is used for those systems
+that have nothing. It provides a basic translation for the common standard
+signal numbers. I've been extra cautious with the ifdef's here. Probably more
+than is necessary... */
+
+const char *
+os_strsignal(const int n)
+{
+switch (n)
+  {
+  #ifdef SIGHUP
+  case SIGHUP:  return "hangup";
+  #endif
+
+  #ifdef SIGINT
+  case SIGINT:  return "interrupt";
+  #endif
+
+  #ifdef SIGQUIT
+  case SIGQUIT: return "quit";
+  #endif
+
+  #ifdef SIGILL
+  case SIGILL:  return "illegal instruction";
+  #endif
+
+  #ifdef SIGTRAP
+  case SIGTRAP: return "trace trap";
+  #endif
+
+  #ifdef SIGABRT
+  case SIGABRT: return "abort";
+  #endif
+
+  #ifdef SIGEMT
+  case SIGEMT:  return "EMT instruction";
+  #endif
+
+  #ifdef SIGFPE
+  case SIGFPE:  return "arithmetic exception";
+  #endif
+
+  #ifdef SIGKILL
+  case SIGKILL: return "killed";
+  #endif
+
+  #ifdef SIGBUS
+  case SIGBUS:  return "bus error";
+  #endif
+
+  #ifdef SIGSEGV
+  case SIGSEGV: return "segmentation fault";
+  #endif
+
+  #ifdef SIGSYS
+  case SIGSYS:  return "bad system call";
+  #endif
+
+  #ifdef SIGPIPE
+  case SIGPIPE: return "broken pipe";
+  #endif
+
+  #ifdef SIGALRM
+  case SIGALRM: return "alarm";
+  #endif
+
+  #ifdef SIGTERM
+  case SIGTERM: return "terminated";
+  #endif
+
+  #ifdef SIGUSR1
+  case SIGUSR1: return "user signal 1";
+  #endif
+
+  #ifdef SIGUSR2
+  case SIGUSR2: return "user signal 2";
+  #endif
+
+  #ifdef SIGCHLD
+  case SIGCHLD: return "child stop or exit";
+  #endif
+
+  #ifdef SIGPWR
+  case SIGPWR:  return "power fail/restart";
+  #endif
+
+  #ifdef SIGURG
+  case SIGURG:  return "urgent condition on I/O channel";
+  #endif
+
+  #ifdef SIGSTOP
+  case SIGSTOP: return "stop";
+  #endif
+
+  #ifdef SIGTSTP
+  case SIGTSTP: return "stop from tty";
+  #endif
+
+  #ifdef SIGXCPU
+  case SIGXCPU: return "exceeded CPU limit";
+  #endif
+
+  #ifdef SIGXFSZ
+  case SIGXFSZ: return "exceeded file size limit";
+  #endif
+
+  default:      return "unrecognized signal number";
+  }
+}
+#endif /* OS_STRSIGNAL */
+
+
+
+#ifndef OS_STREXIT
+/*************************************************
+*      Provide strexit() for systems without     *
+*************************************************/
+
+/* Actually, I don't know of any system that has a strexit() function to turn
+exit codes into text, but this function is implemented this way so that if any
+OS does have such a thing, it could be used instead of this build-in one. */
+
+const char *
+os_strexit(const int n)
+{
+switch (n)
+  {
+  /* On systems without sysexits.h we can assume only those exit codes
+  that are given a default value in exim.h. */
+
+  #ifndef NO_SYSEXITS
+  case EX_USAGE:       return "(could mean usage or syntax error)";
+  case EX_DATAERR:     return "(could mean error in input data)";
+  case EX_NOINPUT:     return "(could mean input data missing)";
+  case EX_NOUSER:      return "(could mean user nonexistent)";
+  case EX_NOHOST:      return "(could mean host nonexistent)";
+  case EX_SOFTWARE:    return "(could mean internal software error)";
+  case EX_OSERR:       return "(could mean internal operating system error)";
+  case EX_OSFILE:      return "(could mean system file missing)";
+  case EX_IOERR:       return "(could mean input/output error)";
+  case EX_PROTOCOL:    return "(could mean protocol error)";
+  case EX_NOPERM:      return "(could mean permission denied)";
+  #endif
+
+  case EX_EXECFAILED:  return "(could mean unable to exec or command does not exist)";
+  case EX_UNAVAILABLE: return "(could mean service or program unavailable)";
+  case EX_CANTCREAT:   return "(could mean can't create output file)";
+  case EX_TEMPFAIL:    return "(could mean temporary error)";
+  case EX_CONFIG:      return "(could mean configuration error)";
+  default:             return "";
+  }
+}
+#endif /* OS_STREXIT */
+
+
+
+
+/***********************************************************
+*                   Load average function                  *
+***********************************************************/
+
+/* Although every Unix seems to have a different way of getting the load
+average, a number of them have things in common. Some common variants are
+provided below, but if an OS has unique requirements it can be handled in
+a specific os.c file. What is required is a function called os_getloadavg
+which takes no arguments and passes back the load average * 1000 as an int,
+or -1 if no data is available. */
+
+
+/* ----------------------------------------------------------------------- */
+/* If the OS has got a BSD getloadavg() function, life is very easy. */
+
+#if !defined(OS_LOAD_AVERAGE) && defined(HAVE_BSD_GETLOADAVG)
+#define OS_LOAD_AVERAGE
+
+int
+os_getloadavg(void)
+{
+double avg;
+int loads = getloadavg (&avg, 1);
+if (loads != 1) return -1;
+return (int)(avg * 1000.0);
+}
+#endif
+/* ----------------------------------------------------------------------- */
+
+
+
+/* ----------------------------------------------------------------------- */
+/* Only SunOS5 has the kstat functions as far as I know, but put the code
+here as there is the -hal variant, and other systems might follow this road one
+day. */
+
+#if !defined(OS_LOAD_AVERAGE) && defined(HAVE_KSTAT)
+#define OS_LOAD_AVERAGE
+
+#include <kstat.h>
+
+int
+os_getloadavg(void)
+{
+int avg;
+kstat_ctl_t *kc;
+kstat_t *ksp;
+kstat_named_t *kn;
+
+if ((kc = kstat_open()) == NULL ||
+    (ksp = kstat_lookup(kc, LOAD_AVG_KSTAT_MODULE, 0, LOAD_AVG_KSTAT))
+        == NULL ||
+     kstat_read(kc, ksp, NULL) < 0 ||
+    (kn = kstat_data_lookup(ksp, LOAD_AVG_SYMBOL)) == NULL)
+  return -1;
+
+avg = (int)(((double)(kn->LOAD_AVG_FIELD)/FSCALE) * 1000.0);
+
+kstat_close(kc);
+return avg;
+}
+
+#endif
+/* ----------------------------------------------------------------------- */
+
+
+
+/* ----------------------------------------------------------------------- */
+/* Handle OS where a kernel symbol has to be read from /dev/kmem */
+
+#if !defined(OS_LOAD_AVERAGE) && defined(HAVE_DEV_KMEM)
+#define OS_LOAD_AVERAGE
+
+#include <nlist.h>
+
+static int  avg_kd = -1;
+static long avg_offset;
+
+int
+os_getloadavg(void)
+{
+LOAD_AVG_TYPE avg;
+
+if (avg_kd < 0)
+  {
+  struct nlist nl[2];
+  nl[0].n_name = LOAD_AVG_SYMBOL;
+  nl[1].n_name = "";
+  nlist (KERNEL_PATH, nl);
+  avg_offset = (long)nl[0].n_value;
+  avg_kd = open ("/dev/kmem", 0);
+  if (avg_kd < 0) return -1;
+  (void) fcntl(avg_kd, F_SETFD, FD_CLOEXEC);
+  }
+
+if (lseek (avg_kd, avg_offset, 0) == -1L
+    || read (avg_kd, (char *)(&avg), sizeof (avg)) != sizeof(avg))
+  return -1;
+
+return (int)(((double)avg/FSCALE)*1000.0);
+}
+
+#endif
+/* ----------------------------------------------------------------------- */
+
+
+
+/* ----------------------------------------------------------------------- */
+/* If nothing is known about this OS, then the load average facility is
+not available. */
+
+#ifndef OS_LOAD_AVERAGE
+
+int
+os_getloadavg(void)
+{
+return -1;
+}
+
+#endif
+
+/* ----------------------------------------------------------------------- */
+
+
+
+#if !defined FIND_RUNNING_INTERFACES
+/*************************************************
+*     Find all the running network interfaces    *
+*************************************************/
+
+/* Finding all the running interfaces is something that has os-dependent
+tweaks, even in the IPv4 case, and it gets worse for IPv6, which is why this
+code is now in the os-dependent source file. There is a common function which
+works on most OS (except IRIX) for IPv4 interfaces, and, with some variations
+controlled by macros, on at least one OS for IPv6 and IPv4 interfaces. On Linux
+with IPv6, the common function is used for the IPv4 interfaces and additional
+code used for IPv6. Consequently, the real function is called
+os_common_find_running_interfaces() so that it can be called from the Linux
+function. On non-Linux systems, the macro for os_find_running_interfaces just
+calls the common function; on Linux it calls the Linux function.
+
+This function finds the addresses of all the running interfaces on the machine.
+A chain of blocks containing the textual form of the addresses is returned.
+
+getifaddrs() provides a sane consistent way to query this on modern OSs,
+otherwise fall back to a maze of twisty ioctl() calls
+
+Arguments:    none
+Returns:      a chain of ip_address_items, each pointing to a textual
+              version of an IP address, with the port field set to zero
+*/
+
+
+#ifndef NO_FIND_INTERFACES
+
+#ifdef HAVE_GETIFADDRS
+
+#include <ifaddrs.h>
+
+ip_address_item *
+os_common_find_running_interfaces(void)
+{
+struct ifaddrs *ifalist = NULL;
+ip_address_item *yield = NULL;
+ip_address_item *last = NULL;
+ip_address_item  *next;
+
+if (getifaddrs(&ifalist) != 0)
+  log_write(0, LOG_PANIC_DIE, "Unable to call getifaddrs: %d %s",
+    errno, strerror(errno));
+
+struct ifaddrs *ifa;
+for (ifa = ifalist; ifa != NULL; ifa = ifa->ifa_next)
+  {
+  if (ifa->ifa_addr->sa_family != AF_INET
+#if HAVE_IPV6
+    && ifa->ifa_addr->sa_family != AF_INET6
+#endif /* HAVE_IPV6 */
+    )
+    continue;
+
+  if ( !(ifa->ifa_flags & IFF_UP) ) /* Only want 'UP' interfaces */
+    continue;
+
+  /* Create a data block for the address, fill in the data, and put it on the
+  chain. */
+
+  next = store_get(sizeof(ip_address_item));
+  next->next = NULL;
+  next->port = 0;
+  (void)host_ntoa(-1, ifa->ifa_addr, next->address, NULL);
+
+  if (yield == NULL)
+    yield = last = next;
+  else
+    {
+    last->next = next;
+    last = next;
+    }
+
+  DEBUG(D_interface) debug_printf("Actual local interface address is %s (%s)\n",
+    last->address, ifa->ifa_name);
+  }
+
+/* free the list of addresses, and return the chain of data blocks. */
+
+freeifaddrs (ifalist);
+return yield;
+}
+
+#else /* HAVE_GETIFADDRS */
+
+/*
+Problems:
+
+  (1) Solaris 2 has the SIOGIFNUM call to get the number of interfaces, but
+  other OS (including Solaris 1) appear not to. So just screw in a largeish
+  fixed number, defined by MAX_INTERFACES. This is in the config.h file and
+  can be changed in Local/Makefile. Unfortunately, the www addressing scheme
+  means that some hosts have a very large number of virtual interfaces. Such
+  hosts are recommended to set local_interfaces to avoid problems with this.
+
+  (2) If the standard code is run on IRIX, it does not return any alias
+  interfaces. There is special purpose code for that operating system, which
+  uses the sysctl() function. The code is in OS/os.c-IRIX, and this code isn't
+  used on that OS.
+
+  (3) Some experimental/developing OS (e.g. GNU/Hurd) do not have any means
+  of finding the interfaces. If NO_FIND_INTERFACES is set, a fudge-up is used
+  instead.
+
+  (4) Some operating systems set the IP address in what SIOCGIFCONF returns;
+  others do not, and require SIOCGIFADDR to be called to get it. For most of
+  the former, calling the latter does no harm, but it causes grief on Linux and
+  BSD systems in the case of IP aliasing, so a means of cutting it out is
+  provided.
+*/
+
+/* If there is IPv6 support, and SIOCGLIFCONF is defined, define macros to
+use these new, longer versions of the old IPv4 interfaces. Otherwise, define
+the macros to use the historical versions. */
+
+#if HAVE_IPV6 && defined SIOCGLIFCONF
+#define V_ifconf        lifconf
+#define V_ifreq         lifreq
+#define V_GIFADDR       SIOCGLIFADDR
+#define V_GIFCONF       SIOCGLIFCONF
+#define V_GIFFLAGS      SIOCGLIFFLAGS
+#define V_ifc_buf       lifc_buf
+#define V_ifc_family    lifc_family
+#define V_ifc_flags     lifc_flags
+#define V_ifc_len       lifc_len
+#define V_ifr_addr      lifr_addr
+#define V_ifr_flags     lifr_flags
+#define V_ifr_name      lifr_name
+#define V_FAMILY_QUERY  AF_UNSPEC
+#define V_family        ss_family
+#else
+#define V_ifconf        ifconf
+#define V_ifreq         ifreq
+#define V_GIFADDR       SIOCGIFADDR
+#define V_GIFCONF       SIOCGIFCONF
+#define V_GIFFLAGS      SIOCGIFFLAGS
+#define V_ifc_buf       ifc_buf
+#define V_ifc_family    ifc_family
+#define V_ifc_flags     ifc_flags
+#define V_ifc_len       ifc_len
+#define V_ifr_addr      ifr_addr
+#define V_ifr_flags     ifr_flags
+#define V_ifr_name      ifr_name
+#define V_family        sa_family
+#endif
+
+/* In all cases of IPv6 support, use an IPv6 socket. Otherwise (at least on
+Solaris 8) the call to read the flags doesn't work for IPv6 interfaces. If
+we find we can't actually make an IPv6 socket, the code will revert to trying
+an IPv4 socket. */
+
+#if HAVE_IPV6
+#define FAMILY          AF_INET6
+#else
+#define FAMILY          AF_INET
+#endif
+
+/* OK, after all that preliminary stuff, here's the code. */
+
+ip_address_item *
+os_common_find_running_interfaces(void)
+{
+struct V_ifconf ifc;
+struct V_ifreq ifreq;
+int vs;
+ip_address_item *yield = NULL;
+ip_address_item *last = NULL;
+ip_address_item  *next;
+char *cp;
+char buf[MAX_INTERFACES*sizeof(struct V_ifreq)];
+struct sockaddr *addrp;
+size_t len = 0;
+char addrbuf[512];
+
+/* We have to create a socket in order to do ioctls on it to find out
+what we want to know. */
+
+if ((vs = socket(FAMILY, SOCK_DGRAM, 0)) < 0)
+  {
+  #if HAVE_IPV6
+  DEBUG(D_interface)
+    debug_printf("Unable to create IPv6 socket to find interface addresses:\n  "
+      "error %d %s\nTrying for an IPv4 socket\n", errno, strerror(errno));
+  vs = socket(AF_INET, SOCK_DGRAM, 0);
+  if (vs < 0)
+  #endif
+  log_write(0, LOG_PANIC_DIE, "Unable to create IPv4 socket to find interface "
+    "addresses: %d %s", errno, strerror(errno));
+  }
+
+/* Get the interface configuration. Some additional data is required when the
+new structures are in use. */
+
+ifc.V_ifc_len = sizeof(buf);
+ifc.V_ifc_buf = buf;
+
+#ifdef V_FAMILY_QUERY
+ifc.V_ifc_family = V_FAMILY_QUERY;
+ifc.V_ifc_flags = 0;
+#endif
+
+if (ioctl(vs, V_GIFCONF, (char *)&ifc) < 0)
+  log_write(0, LOG_PANIC_DIE, "Unable to get interface configuration: %d %s",
+    errno, strerror(errno));
+
+/* If the buffer is big enough, the ioctl sets the value of ifc.V_ifc_len to
+the amount actually used. If the buffer isn't big enough, at least on some
+operating systems, ifc.V_ifc_len still gets set to correspond to the total
+number of interfaces, even though they don't all fit in the buffer. */
+
+if (ifc.V_ifc_len > sizeof(buf))
+  {
+  ifc.V_ifc_len = sizeof(buf);
+  DEBUG(D_interface)
+    debug_printf("more than %d interfaces found: remainder not used\n"
+      "(set MAX_INTERFACES in Local/Makefile and rebuild if you want more)\n",
+      MAX_INTERFACES);
+  }
+
+/* For each interface, check it is an IP interface, get its flags, and see if
+it is up; if not, skip.
+
+BSD systems differ from others in what SIOCGIFCONF returns. Other systems
+return a vector of ifreq structures whose size is as defined by the structure.
+BSD systems allow sockaddrs to be longer than their sizeof, which in turn makes
+the ifreq structures longer than their sizeof. The code below has its origins
+in amd and ifconfig; it uses the sa_len field of each sockaddr to determine
+each item's length.
+
+This is complicated by the fact that, at least on BSD systems, the data in the
+buffer is not guaranteed to be aligned. Thus, we must first copy the basic
+struct to some aligned memory before looking at the field in the fixed part to
+find its length, and then recopy the correct length. */
+
+for (cp = buf; cp < buf + ifc.V_ifc_len; cp += len)
+  {
+  memcpy((char *)&ifreq, cp, sizeof(ifreq));
+
+  #ifndef HAVE_SA_LEN
+  len = sizeof(struct V_ifreq);
+
+  #else
+  len = ((ifreq.ifr_addr.sa_len > sizeof(ifreq.ifr_addr))?
+          ifreq.ifr_addr.sa_len : sizeof(ifreq.ifr_addr)) +
+         sizeof(ifreq.V_ifr_name);
+  if (len > sizeof(addrbuf))
+    log_write(0, LOG_PANIC_DIE, "Address for %s interface is absurdly long",
+        ifreq.V_ifr_name);
+
+  #endif
+
+  /* If not an IP interface, skip */
+
+  if (ifreq.V_ifr_addr.V_family != AF_INET
+  #if HAVE_IPV6
+    && ifreq.V_ifr_addr.V_family != AF_INET6
+  #endif
+    ) continue;
+
+  /* Get the interface flags, and if the interface is down, continue. Formerly,
+  we treated the inability to get the flags as a panic-die error. However, it
+  seems that on some OS (Solaris 9 being the case noted), it is possible to
+  have an interface in this list for which this call fails because the
+  interface hasn't been "plumbed" to any protocol (IPv4 or IPv6). Therefore,
+  we now just treat this case as "down" as well. */
+
+  if (ioctl(vs, V_GIFFLAGS, (char *)&ifreq) < 0)
+    {
+    continue;
+    /*************
+    log_write(0, LOG_PANIC_DIE, "Unable to get flags for %s interface: %d %s",
+      ifreq.V_ifr_name, errno, strerror(errno));
+    *************/
+    }
+  if ((ifreq.V_ifr_flags & IFF_UP) == 0) continue;
+
+  /* On some operating systems we have to get the IP address of the interface
+  by another call. On others, it's already there, but we must copy the full
+  length because we only copied the basic length above, and anyway,
+  GIFFLAGS may have wrecked the data. */
+
+  #ifndef SIOCGIFCONF_GIVES_ADDR
+  if (ioctl(vs, V_GIFADDR, (char *)&ifreq) < 0)
+    log_write(0, LOG_PANIC_DIE, "Unable to get IP address for %s interface: "
+      "%d %s", ifreq.V_ifr_name, errno, strerror(errno));
+  addrp = &ifreq.V_ifr_addr;
+
+  #else
+  memcpy(addrbuf, cp + offsetof(struct V_ifreq, V_ifr_addr),
+    len - sizeof(ifreq.V_ifr_name));
+  addrp = (struct sockaddr *)addrbuf;
+  #endif
+
+  /* Create a data block for the address, fill in the data, and put it on the
+  chain. */
+
+  next = store_get(sizeof(ip_address_item));
+  next->next = NULL;
+  next->port = 0;
+  (void)host_ntoa(-1, addrp, next->address, NULL);
+
+  if (yield == NULL) yield = last = next; else
+    {
+    last->next = next;
+    last = next;
+    }
+
+  DEBUG(D_interface) debug_printf("Actual local interface address is %s (%s)\n",
+    last->address, ifreq.V_ifr_name);
+  }
+
+/* Close the socket, and return the chain of data blocks. */
+
+(void)close(vs);
+return yield;
+}
+
+#endif /* HAVE_GETIFADDRS */
+
+#else  /* NO_FIND_INTERFACES */
+
+/* Some experimental or developing OS (e.g. GNU/Hurd) do not have the ioctls,
+and there is no other way to get a list of the (IP addresses of) local
+interfaces. We just return the loopback address(es). */
+
+ip_address_item *
+os_common_find_running_interfaces(void)
+{
+ip_address_item *yield = store_get(sizeof(address_item));
+yield->address = US"127.0.0.1";
+yield->port = 0;
+yield->next = NULL;
+
+#if HAVE_IPV6
+yield->next = store_get(sizeof(address_item));
+yield->next->address = US"::1";
+yield->next->port = 0;
+yield->next->next = NULL;
+#endif
+
+DEBUG(D_interface) debug_printf("Unable to find local interface addresses "
+  "on this OS: returning loopback address(es)\n");
+return yield;
+}
+
+#endif /* NO_FIND_INTERFACES */
+#endif /* FIND_RUNNING_INTERFACES */
+
+
+
+
+/* ----------------------------------------------------------------------- */
+
+/***********************************************************
+*                 DNS Resolver Base Finder                 *
+***********************************************************/
+
+/* We need to be able to set options for the system resolver(5), historically
+made available as _res.  At least one OS (NetBSD) now no longer provides this
+directly, instead making you call a function per thread to get a handle.
+Other OSs handle thread-safe resolver differently, in ways which fail if the
+programmer creates their own structs. */
+
+#if !defined(OS_GET_DNS_RESOLVER_RES) && !defined(COMPILE_UTILITY)
+
+#include <resolv.h>
+
+/* confirmed that res_state is typedef'd as a struct* on BSD and Linux, will
+find out how unportable it is on other OSes, but most resolver implementations
+should be descended from ISC's bind.
+
+Linux and BSD do:
+  define _res (*__res_state())
+identically.  We just can't rely on __foo functions.  It's surprising that use
+of _res has been as portable as it has, for so long.
+
+So, since _res works everywhere, and everything can decode the struct, I'm
+going to gamble that res_state is a typedef everywhere and use that as the
+return type.
+*/
+
+res_state
+os_get_dns_resolver_res(void)
+{
+  return &_res;
+}
+
+#endif /* OS_GET_DNS_RESOLVER_RES */
+
+
+/* ----------------------------------------------------------------------- */
+
+
+
+
+
+/*************************************************
+**************************************************
+*             Stand-alone test program           *
+**************************************************
+*************************************************/
+
+
+#ifdef STAND_ALONE
+
+#ifdef CLOCKS_PER_SEC
+#define REAL_CLOCK_TICK CLOCKS_PER_SEC
+#else
+  #ifdef CLK_TCK
+  #define REAL_CLOCK_TICK CLK_TCK
+  #else
+  #define REAL_CLOCK_TICK 1000000   /* SunOS4 */
+  #endif
+#endif
+
+
+int main(int argc, char **argv)
+{
+char buffer[128];
+int fd = fileno(stdin);
+int rc;
+
+printf("Testing restarting signal; wait for handler message, then type a line\n");
+strcpy(buffer, "*** default ***\n");
+os_restarting_signal(SIGALRM, sigalrm_handler);
+alarm(2);
+if ((rc = read(fd, buffer, sizeof(buffer))) < 0)
+  printf("No data read\n");
+else
+  {
+  buffer[rc] = 0;
+  printf("Read: %s", buffer);
+  }
+alarm(0);
+
+printf("Testing non-restarting signal; should read no data after handler message\n");
+strcpy(buffer, "*** default ***\n");
+os_non_restarting_signal(SIGALRM, sigalrm_handler);
+alarm(2);
+if ((rc = read(fd, buffer, sizeof(buffer))) < 0)
+  printf("No data read\n");
+else
+  {
+  buffer[rc] = 0;
+  printf("Read: %s", buffer);
+  }
+alarm(0);
+
+printf("Testing load averages (last test - ^C to kill)\n");
+for (;;)
+  {
+  int avg;
+  clock_t used;
+  clock_t before = clock();
+  avg = os_getloadavg();
+  used = clock() - before;
+  printf("cpu time = %.2f ", (double)used/REAL_CLOCK_TICK);
+  if (avg < 0)
+    {
+    printf("load average not available\n");
+    break;
+    }
+  printf("load average = %.2f\n", (double)avg/1000.0);
+  sleep(2);
+  }
+return 0;
+}
+
+#endif
+
+/* End of os.c */
diff --git a/src/osfunctions.h b/src/osfunctions.h
new file mode 100644 (file)
index 0000000..5527127
--- /dev/null
@@ -0,0 +1,36 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Prototypes for os-specific functions. For utilities, we don't need the one
+that uses a type that isn't defined for them. */
+
+#ifndef COMPILE_UTILITY
+extern ip_address_item *os_common_find_running_interfaces(void);
+#endif
+
+/* If these exist as a macro, then they're overridden away from us and we
+rely upon the system headers to provide prototype declarations for us.
+Notably, strsignal() is not in the Single Unix Specification (v3) and
+predicting constness is awkward. */
+
+#ifndef os_getloadavg
+extern int           os_getloadavg(void);
+#endif
+#ifndef os_restarting_signal
+extern void          os_restarting_signal(int, void (*)(int));
+#endif
+#ifndef os_non_restarting_signal
+extern void          os_non_restarting_signal(int, void (*)(int));
+#endif
+#ifndef os_strexit
+extern const char   *os_strexit(int);     /* char to match os_strsignal */
+#endif
+#ifndef os_strsignal
+extern const char   *os_strsignal(int);   /* char to match strsignal in some OS */
+#endif
+
+/* End of osfunctions.h */
diff --git a/src/parse.c b/src/parse.c
new file mode 100644 (file)
index 0000000..94e42c1
--- /dev/null
@@ -0,0 +1,2200 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Functions for parsing addresses */
+
+
+#include "exim.h"
+
+
+static uschar *last_comment_position;
+
+
+
+/* In stand-alone mode, provide a replacement for deliver_make_addr()
+and rewrite_address[_qualify]() so as to avoid having to drag in too much
+redundant apparatus. */
+
+#ifdef STAND_ALONE
+
+address_item *deliver_make_addr(uschar *address, BOOL copy)
+{
+address_item *addr = store_get(sizeof(address_item));
+addr->next = NULL;
+addr->parent = NULL;
+addr->address = address;
+return addr;
+}
+
+uschar *rewrite_address(uschar *recipient, BOOL dummy1, BOOL dummy2, rewrite_rule
+  *dummy3, int dummy4)
+{
+return recipient;
+}
+
+uschar *rewrite_address_qualify(uschar *recipient, BOOL dummy1)
+{
+return recipient;
+}
+
+#endif
+
+
+
+
+/*************************************************
+*             Find the end of an address         *
+*************************************************/
+
+/* Scan over a string looking for the termination of an address at a comma,
+or end of the string. It's the source-routed addresses which cause much pain
+here. Although Exim ignores source routes, it must recognize such addresses, so
+we cannot get rid of this logic.
+
+Argument:
+  s        pointer to the start of an address
+  nl_ends  if TRUE, '\n' terminates an address
+
+Returns:   pointer past the end of the address
+           (i.e. points to null or comma)
+*/
+
+uschar *
+parse_find_address_end(uschar *s, BOOL nl_ends)
+{
+BOOL source_routing = *s == '@';
+int no_term = source_routing? 1 : 0;
+
+while (*s != 0 && (*s != ',' || no_term > 0) && (*s != '\n' || !nl_ends))
+  {
+  /* Skip single quoted characters. Strictly these should not occur outside
+  quoted strings in RFC 822 addresses, but they can in RFC 821 addresses. Pity
+  about the lack of consistency, isn't it? */
+
+  if (*s == '\\' && s[1] != 0) s += 2;
+
+  /* Skip quoted items that are not inside brackets. Note that
+  quoted pairs are allowed inside quoted strings. */
+
+  else if (*s == '\"')
+    {
+    while (*(++s) != 0 && (*s != '\n' || !nl_ends))
+      {
+      if (*s == '\\' && s[1] != 0) s++;
+        else if (*s == '\"') { s++; break; }
+      }
+    }
+
+  /* Skip comments, which may include nested brackets, but quotes
+  are not recognized inside comments, though quoted pairs are. */
+
+  else if (*s == '(')
+    {
+    int level = 1;
+    while (*(++s) != 0 && (*s != '\n' || !nl_ends))
+      {
+      if (*s == '\\' && s[1] != 0) s++;
+        else if (*s == '(') level++;
+          else if (*s == ')' && --level <= 0) { s++; break; }
+      }
+    }
+
+  /* Non-special character; just advance. Passing the colon in a source
+  routed address means that any subsequent comma or colon may terminate unless
+  inside angle brackets. */
+
+  else
+    {
+    if (*s == '<')
+      {
+      source_routing = s[1] == '@';
+      no_term = source_routing? 2 : 1;
+      }
+    else if (*s == '>') no_term--;
+    else if (source_routing && *s == ':') no_term--;
+    s++;
+    }
+  }
+
+return s;
+}
+
+
+
+/*************************************************
+*            Find last @ in an address           *
+*************************************************/
+
+/* This function is used when we have something that may not qualified. If we
+know it's qualified, searching for the rightmost '@' is sufficient. Here we
+have to be a bit more clever than just a plain search, in order to handle
+unqualified local parts like "thing@thong" correctly. Since quotes may not
+legally be part of a domain name, we can give up on hitting the first quote
+when searching from the right. Now that the parsing also permits the RFC 821
+form of address, where quoted-pairs are allowed in unquoted local parts, we
+must take care to handle that too.
+
+Argument:  pointer to an address, possibly unqualified
+Returns:   pointer to the last @ in an address, or NULL if none
+*/
+
+uschar *
+parse_find_at(uschar *s)
+{
+uschar *t = s + Ustrlen(s);
+while (--t >= s)
+  {
+  if (*t == '@')
+    {
+    int backslash_count = 0;
+    uschar *tt = t - 1;
+    while (tt > s && *tt-- == '\\') backslash_count++;
+    if ((backslash_count & 1) == 0) return t;
+    }
+  else if (*t == '\"') return NULL;
+  }
+return NULL;
+}
+
+
+
+
+/***************************************************************************
+* In all the functions below that read a particular object type from       *
+* the input, return the new value of the pointer s (the first argument),   *
+* and put the object into the store pointed to by t (the second argument), *
+* adding a terminating zero. If no object is found, t will point to zero   *
+* on return.                                                               *
+***************************************************************************/
+
+
+/*************************************************
+*          Skip white space and comment          *
+*************************************************/
+
+/* Algorithm:
+  (1) Skip spaces.
+  (2) If uschar not '(', return.
+  (3) Skip till matching ')', not counting any characters
+      escaped with '\'.
+  (4) Move past ')' and goto (1).
+
+The start of the last potential comment position is remembered to
+make it possible to ignore comments at the end of compound items.
+
+Argument: current character pointer
+Regurns:  new character pointer
+*/
+
+static uschar *
+skip_comment(uschar *s)
+{
+last_comment_position = s;
+while (*s)
+  {
+  int c, level;
+  while (isspace(*s)) s++;
+  if (*s != '(') break;
+  level = 1;
+  while((c = *(++s)) != 0)
+    {
+    if (c == '(') level++;
+    else if (c == ')') { if (--level <= 0) { s++; break; } }
+    else if (c == '\\' && s[1] != 0) s++;
+    }
+  }
+return s;
+}
+
+
+
+/*************************************************
+*             Read a domain                      *
+*************************************************/
+
+/* A domain is a sequence of subdomains, separated by dots. See comments below
+for detailed syntax of the subdomains.
+
+If allow_domain_literals is TRUE, a "domain" may also be an IP address enclosed
+in []. Make sure the output is set to the null string if there is a syntax
+error as well as if there is no domain at all.
+
+Arguments:
+  s          current character pointer
+  t          where to put the domain
+  errorptr   put error message here on failure (*t will be 0 on exit)
+
+Returns:     new character pointer
+*/
+
+static uschar *
+read_domain(uschar *s, uschar *t, uschar **errorptr)
+{
+uschar *tt = t;
+s = skip_comment(s);
+
+/* Handle domain literals if permitted. An RFC 822 domain literal may contain
+any character except [ ] \, including linear white space, and may contain
+quoted characters. However, RFC 821 restricts literals to being dot-separated
+3-digit numbers, and we make the obvious extension for IPv6. Go for a sequence
+of digits, dots, hex digits, and colons here; later this will be checked for
+being a syntactically valid IP address if it ever gets to a router.
+
+Allow both the formal IPv6 form, with IPV6: at the start, and the informal form
+without it, and accept IPV4: as well, 'cause someone will use it sooner or
+later. */
+
+if (*s == '[')
+  {
+  *t++ = *s++;
+
+  if (strncmpic(s, US"IPv6:", 5) == 0 || strncmpic(s, US"IPv4:", 5) == 0)
+    {
+    memcpy(t, s, 5);
+    t += 5;
+    s += 5;
+    }
+  while (*s == '.' || *s == ':' || isxdigit(*s)) *t++ = *s++;
+
+  if (*s == ']') *t++ = *s++; else
+    {
+    *errorptr = US"malformed domain literal";
+    *tt = 0;
+    }
+
+  if (!allow_domain_literals)
+    {
+    *errorptr = US"domain literals not allowed";
+    *tt = 0;
+    }
+  *t = 0;
+  return skip_comment(s);
+  }
+
+/* Handle a proper domain, which is a sequence of dot-separated atoms. Remove
+trailing dots if strip_trailing_dot is set. A subdomain is an atom.
+
+An atom is a sequence of any characters except specials, space, and controls.
+The specials are ( ) < > @ , ; : \ " . [ and ]. This is the rule for RFC 822
+and its successor (RFC 2822). However, RFC 821 and its successor (RFC 2821) is
+tighter, allowing only letters, digits, and hyphens, not starting with a
+hyphen.
+
+There used to be a global flag that got set when checking addresses that came
+in over SMTP and which should therefore should be checked according to the
+stricter rule. However, it seems silly to make the distinction, because I don't
+suppose anybody ever uses local domains that are 822-compliant and not
+821-compliant. Furthermore, Exim now has additional data on the spool file line
+after an address (after "one_time" processing), and it makes use of a #
+character to delimit it. When I wrote that code, I forgot about this 822-domain
+stuff, and assumed # could never appear in a domain.
+
+So the old code is now cut out for Release 4.11 onwards, on 09-Aug-02. In a few
+years, when we are sure this isn't actually causing trouble, throw it away.
+
+March 2003: the story continues: There is a camp that is arguing for the use of
+UTF-8 in domain names as the way to internationalization, and other MTAs
+support this. Therefore, we now have a flag that permits the use of characters
+with values greater than 127, encoded in UTF-8, in subdomains, so that Exim can
+be used experimentally in this way. */
+
+for (;;)
+  {
+  uschar *tsave = t;
+
+/*********************
+  if (rfc821_domains)
+    {
+    if (*s != '-') while (isalnum(*s) || *s == '-') *t++ = *s++;
+    }
+  else
+    while (!mac_iscntrl_or_special(*s)) *t++ = *s++;
+*********************/
+
+  if (*s != '-')
+    {
+    /* Only letters, digits, and hyphens */
+
+    if (!allow_utf8_domains)
+      {
+      while (isalnum(*s) || *s == '-') *t++ = *s++;
+      }
+
+    /* Permit legal UTF-8 characters to be included */
+
+    else for(;;)
+      {
+      int i, d;
+      if (isalnum(*s) || *s == '-')    /* legal ascii characters */
+        {
+        *t++ = *s++;
+        continue;
+        }
+      if ((*s & 0xc0) != 0xc0) break;  /* not start of UTF-8 character */
+      d = *s << 2;
+      for (i = 1; i < 6; i++)          /* i is the number of additional bytes */
+        {
+        if ((d & 0x80) == 0) break;
+        d <<= 1;
+        }
+      if (i == 6) goto BAD_UTF8;       /* invalid UTF-8 */
+      *t++ = *s++;                     /* leading UTF-8 byte */
+      while (i-- > 0)                  /* copy and check remainder */
+        {
+        if ((*s & 0xc0) != 0x80)
+          {
+          BAD_UTF8:
+          *errorptr = US"invalid UTF-8 byte sequence";
+          *tt = 0;
+          return s;
+          }
+        *t++ = *s++;
+        }
+      }    /* End of loop for UTF-8 character */
+    }      /* End of subdomain */
+
+  s = skip_comment(s);
+  *t = 0;
+
+  if (t == tsave)   /* empty component */
+    {
+    if (strip_trailing_dot && t > tt && *s != '.') t[-1] = 0; else
+      {
+      *errorptr = US"domain missing or malformed";
+      *tt = 0;
+      }
+    return s;
+    }
+
+  if (*s != '.') break;
+  *t++ = *s++;
+  s = skip_comment(s);
+  }
+
+return s;
+}
+
+
+
+/*************************************************
+*            Read a local-part                   *
+*************************************************/
+
+/* A local-part is a sequence of words, separated by periods. A null word
+between dots is not strictly allowed but apparently many mailers permit it,
+so, sigh, better be compatible. Even accept a trailing dot...
+
+A <word> is either a quoted string, or an <atom>, which is a sequence
+of any characters except specials, space, and controls. The specials are
+( ) < > @ , ; : \ " . [ and ]. In RFC 822, a single quoted character, (a
+quoted-pair) is not allowed in a word. However, in RFC 821, it is permitted in
+the local part of an address. Rather than have separate parsing functions for
+the different cases, take the liberal attitude always. At least one MUA is
+happy to recognize this case; I don't know how many other programs do.
+
+Arguments:
+  s           current character pointer
+  t           where to put the local part
+  error       where to point error text
+  allow_null  TRUE if an empty local part is not an error
+
+Returns:   new character pointer
+*/
+
+static uschar *
+read_local_part(uschar *s, uschar *t, uschar **error, BOOL allow_null)
+{
+uschar *tt = t;
+*error = NULL;
+for (;;)
+  {
+  int c;
+  uschar *tsave = t;
+  s = skip_comment(s);
+
+  /* Handle a quoted string */
+
+  if (*s == '\"')
+    {
+    *t++ = '\"';
+    while ((c = *(++s)) != 0 && c != '\"')
+      {
+      *t++ = c;
+      if (c == '\\' && s[1] != 0) *t++ = *(++s);
+      }
+    if (c == '\"')
+      {
+      s++;
+      *t++ = '\"';
+      }
+    else
+      {
+      *error = US"unmatched doublequote in local part";
+      return s;
+      }
+    }
+
+  /* Handle an atom, but allow quoted pairs within it. */
+
+  else while (!mac_iscntrl_or_special(*s) || *s == '\\')
+    {
+    c = *t++ = *s++;
+    if (c == '\\' && *s != 0) *t++ = *s++;
+    }
+
+  /* Terminate the word and skip subsequent comment */
+
+  *t = 0;
+  s = skip_comment(s);
+
+  /* If we have read a null component at this point, give an error unless it is
+  terminated by a dot - an extension to RFC 822 - or if it is the first
+  component of the local part and an empty local part is permitted, in which
+  case just return normally. */
+
+  if (t == tsave && *s != '.')
+    {
+    if (t == tt && !allow_null)
+      *error = US"missing or malformed local part";
+    return s;
+    }
+
+  /* Anything other than a dot terminates the local part. Treat multiple dots
+  as a single dot, as this seems to be a common extension. */
+
+  if (*s != '.') break;
+  do { *t++ = *s++; } while (*s == '.');
+  }
+
+return s;
+}
+
+
+/*************************************************
+*            Read route part of route-addr       *
+*************************************************/
+
+/* The pointer is at the initial "@" on entry. Return it following the
+terminating colon. Exim no longer supports the use of source routes, but it is
+required to accept the syntax.
+
+Arguments:
+  s          current character pointer
+  t          where to put the route
+  errorptr   where to put an error message
+
+Returns:     new character pointer
+*/
+
+static uschar *
+read_route(uschar *s, uschar *t, uschar **errorptr)
+{
+BOOL commas = FALSE;
+*errorptr = NULL;
+
+while (*s == '@')
+  {
+  *t++ = '@';
+  s = read_domain(s+1, t, errorptr);
+  if (*t == 0) return s;
+  t += Ustrlen((const uschar *)t);
+  if (*s != ',') break;
+  *t++ = *s++;
+  commas = TRUE;
+  s = skip_comment(s);
+  }
+
+if (*s == ':') *t++ = *s++;
+
+/* If there is no colon, and there were no commas, the most likely error
+is in fact a missing local part in the address rather than a missing colon
+after the route. */
+
+else *errorptr = commas?
+  US"colon expected after route list" :
+  US"no local part";
+
+/* Terminate the route and return */
+
+*t = 0;
+return skip_comment(s);
+}
+
+
+
+/*************************************************
+*                Read addr-spec                  *
+*************************************************/
+
+/* Addr-spec is local-part@domain. We make the domain optional -
+the expected terminator for the whole thing is passed to check this.
+This function is called only when we know we have a route-addr.
+
+Arguments:
+  s          current character pointer
+  t          where to put the addr-spec
+  term       expected terminator (0 or >)
+  errorptr   where to put an error message
+  domainptr  set to point to the start of the domain
+
+Returns:     new character pointer
+*/
+
+static uschar *
+read_addr_spec(uschar *s, uschar *t, int term, uschar **errorptr,
+  uschar **domainptr)
+{
+s = read_local_part(s, t, errorptr, FALSE);
+if (*errorptr == NULL)
+  {
+  if (*s != term)
+    {
+    if (*s != '@')
+      *errorptr = string_sprintf("\"@\" or \".\" expected after \"%s\"", t);
+    else
+      {
+      t += Ustrlen((const uschar *)t);
+      *t++ = *s++;
+      *domainptr = t;
+      s = read_domain(s, t, errorptr);
+      }
+    }
+  }
+return s;
+}
+
+
+
+/*************************************************
+*         Extract operative address              *
+*************************************************/
+
+/* This function extracts an operative address from a full RFC822 mailbox and
+returns it in a piece of dynamic store. We take the easy way and get a piece
+of store the same size as the input, and then copy into it whatever is
+necessary. If we cannot find a valid address (syntax error), return NULL, and
+point the error pointer to the reason. The arguments "start" and "end" are used
+to return the offsets of the first and one past the last characters in the
+original mailbox of the address that has been extracted, to aid in re-writing.
+The argument "domain" is set to point to the first character after "@" in the
+final part of the returned address, or zero if there is no @.
+
+Exim no longer supports the use of source routed addresses (those of the form
+@domain,...:route_addr). It recognizes the syntax, but collapses such addresses
+down to their final components. Formerly, collapse_source_routes had to be set
+to achieve this effect. RFC 1123 allows collapsing with MAY, while the revision
+of RFC 821 had increased this to SHOULD, so I've gone for it, because it makes
+a lot of code elsewhere in Exim much simpler.
+
+There are some special fudges here for handling RFC 822 group address notation
+which may appear in certain headers. If the flag parse_allow_group is set
+TRUE and parse_found_group is FALSE when this function is called, an address
+which is the start of a group (i.e. preceded by a phrase and a colon) is
+recognized; the phrase is ignored and the flag parse_found_group is set. If
+this flag is TRUE at the end of an address, and if an extraneous semicolon is
+found, it is ignored and the flag is cleared.
+
+This logic is used only when scanning through addresses in headers, either to
+fulfil the -t option, or for rewriting, or for checking header syntax. Because
+the group "state" has to be remembered between multiple calls of this function,
+the variables parse_{allow,found}_group are global. It is important to ensure
+that they are reset to FALSE at the end of scanning a header's list of
+addresses.
+
+Arguments:
+  mailbox     points to the RFC822 mailbox
+  errorptr    where to point an error message
+  start       set to start offset in mailbox
+  end         set to end offset in mailbox
+  domain      set to domain offset in result, or 0 if no domain present
+  allow_null  allow <> if TRUE
+
+Returns:      points to the extracted address, or NULL on error
+*/
+
+#define FAILED(s) { *errorptr = s; goto PARSE_FAILED; }
+
+uschar *
+parse_extract_address(uschar *mailbox, uschar **errorptr, int *start, int *end,
+  int *domain, BOOL allow_null)
+{
+uschar *yield = store_get(Ustrlen(mailbox) + 1);
+uschar *startptr, *endptr;
+uschar *s = (uschar *)mailbox;
+uschar *t = (uschar *)yield;
+
+*domain = 0;
+
+/* At the start of the string we expect either an addr-spec or a phrase
+preceding a <route-addr>. If groups are allowed, we might also find a phrase
+preceding a colon and an address. If we find an initial word followed by
+a dot, strict interpretation of the RFC would cause it to be taken
+as the start of an addr-spec. However, many mailers break the rules
+and use addresses of the form "a.n.other <ano@somewhere>" and so we
+allow this case. */
+
+RESTART:   /* Come back here after passing a group name */
+
+s = skip_comment(s);
+startptr = s;                                 /* In case addr-spec */
+s = read_local_part(s, t, errorptr, TRUE);    /* Dot separated words */
+if (*errorptr != NULL) goto PARSE_FAILED;
+
+/* If the terminator is neither < nor @ then the format of the address
+must either be a bare local-part (we are now at the end), or a phrase
+followed by a route-addr (more words must follow). */
+
+if (*s != '@' && *s != '<')
+  {
+  if (*s == 0 || *s == ';')
+    {
+    if (*t == 0) FAILED(US"empty address");
+    endptr = last_comment_position;
+    goto PARSE_SUCCEEDED;              /* Bare local part */
+    }
+
+  /* Expect phrase route-addr, or phrase : if groups permitted, but allow
+  dots in the phrase; complete the loop only when '<' or ':' is encountered -
+  end of string will produce a null local_part and therefore fail. We don't
+  need to keep updating t, as the phrase isn't to be kept. */
+
+  while (*s != '<' && (!parse_allow_group || *s != ':'))
+    {
+    s = read_local_part(s, t, errorptr, FALSE);
+    if (*errorptr != NULL)
+      {
+      *errorptr = string_sprintf("%s (expected word or \"<\")", *errorptr);
+      goto PARSE_FAILED;
+      }
+    }
+
+  if (*s == ':')
+    {
+    parse_found_group = TRUE;
+    parse_allow_group = FALSE;
+    s++;
+    goto RESTART;
+    }
+
+  /* Assert *s == '<' */
+  }
+
+/* At this point the next character is either '@' or '<'. If it is '@', only a
+single local-part has previously been read. An angle bracket signifies the
+start of an <addr-spec>. Throw away anything we have saved so far before
+processing it. Note that this is "if" rather than "else if" because it's also
+used after reading a preceding phrase.
+
+There are a lot of broken sendmails out there that put additional pairs of <>
+round <route-addr>s. If strip_excess_angle_brackets is set, allow any number of
+them, as long as they match. */
+
+if (*s == '<')
+  {
+  uschar *domainptr = yield;
+  BOOL source_routed = FALSE;
+  int bracket_count = 1;
+
+  s++;
+  if (strip_excess_angle_brackets)
+    while (*s == '<') { bracket_count++; s++; }
+
+  t = yield;
+  startptr = s;
+  s = skip_comment(s);
+
+  /* Read an optional series of routes, each of which is a domain. They
+  are separated by commas and terminated by a colon. However, we totally ignore
+  such routes (RFC 1123 says we MAY, and the revision of RFC 821 says we
+  SHOULD). */
+
+  if (*s == '@')
+    {
+    s = read_route(s, t, errorptr);
+    if (*errorptr != NULL) goto PARSE_FAILED;
+    *t = 0;                  /* Ensure route is ignored - probably overkill */
+    source_routed = TRUE;
+    }
+
+  /* Now an addr-spec, terminated by '>'. If there is no preceding route,
+  we must allow an empty addr-spec if allow_null is TRUE, to permit the
+  address "<>" in some circumstances. A source-routed address MUST have
+  a domain in the final part. */
+
+  if (allow_null && !source_routed && *s == '>')
+    {
+    *t = 0;
+    *errorptr = NULL;
+    }
+  else
+    {
+    s = read_addr_spec(s, t, '>', errorptr, &domainptr);
+    if (*errorptr != NULL) goto PARSE_FAILED;
+    *domain = domainptr - yield;
+    if (source_routed && *domain == 0)
+      FAILED(US"domain missing in source-routed address");
+    }
+
+  endptr = s;
+  if (*errorptr != NULL) goto PARSE_FAILED;
+  while (bracket_count-- > 0) if (*s++ != '>')
+    {
+    *errorptr = (s[-1] == 0)? US"'>' missing at end of address" :
+      string_sprintf("malformed address: %.32s may not follow %.*s",
+        s-1, s - (uschar *)mailbox - 1, mailbox);
+    goto PARSE_FAILED;
+    }
+
+  s = skip_comment(s);
+  }
+
+/* Hitting '@' after the first local-part means we have definitely got an
+addr-spec, on a strict reading of the RFC, and the rest of the string
+should be the domain. However, for flexibility we allow for a route-address
+not enclosed in <> as well, which is indicated by an empty first local
+part preceding '@'. The source routing is, however, ignored. */
+
+else if (*t == 0)
+  {
+  uschar *domainptr = yield;
+  s = read_route(s, t, errorptr);
+  if (*errorptr != NULL) goto PARSE_FAILED;
+  *t = 0;         /* Ensure route is ignored - probably overkill */
+  s = read_addr_spec(s, t, 0, errorptr, &domainptr);
+  if (*errorptr != NULL) goto PARSE_FAILED;
+  *domain = domainptr - yield;
+  endptr = last_comment_position;
+  if (*domain == 0) FAILED(US"domain missing in source-routed address");
+  }
+
+/* This is the strict case of local-part@domain. */
+
+else
+  {
+  t += Ustrlen((const uschar *)t);
+  *t++ = *s++;
+  *domain = t - yield;
+  s = read_domain(s, t, errorptr);
+  if (*t == 0) goto PARSE_FAILED;
+  endptr = last_comment_position;
+  }
+
+/* Use goto to get here from the bare local part case. Arrive by falling
+through for other cases. Endptr may have been moved over whitespace, so
+move it back past white space if necessary. */
+
+PARSE_SUCCEEDED:
+if (*s != 0)
+  {
+  if (parse_found_group && *s == ';')
+    {
+    parse_found_group = FALSE;
+    parse_allow_group = TRUE;
+    }
+  else
+    {
+    *errorptr = string_sprintf("malformed address: %.32s may not follow %.*s",
+      s, s - (uschar *)mailbox, mailbox);
+    goto PARSE_FAILED;
+    }
+  }
+*start = startptr - (uschar *)mailbox;      /* Return offsets */
+while (isspace(endptr[-1])) endptr--;
+*end = endptr - (uschar *)mailbox;
+
+/* Although this code has no limitation on the length of address extracted,
+other parts of Exim may have limits, and in any case, RFC 2821 limits local
+parts to 64 and domains to 255, so we do a check here, giving an error if the
+address is ridiculously long. */
+
+if (*end - *start > ADDRESS_MAXLENGTH)
+  {
+  *errorptr = string_sprintf("address is ridiculously long: %.64s...", yield);
+  return NULL;
+  }
+
+return (uschar *)yield;
+
+/* Use goto (via the macro FAILED) to get to here from a variety of places.
+We might have an empty address in a group - the caller can choose to ignore
+this. We must, however, keep the flags correct. */
+
+PARSE_FAILED:
+if (parse_found_group && *s == ';')
+  {
+  parse_found_group = FALSE;
+  parse_allow_group = TRUE;
+  }
+return NULL;
+}
+
+#undef FAILED
+
+
+
+/*************************************************
+*        Quote according to RFC 2047             *
+*************************************************/
+
+/* This function is used for quoting text in headers according to RFC 2047.
+If the only characters that strictly need quoting are spaces, we return the
+original string, unmodified. If a quoted string is too long for the buffer, it
+is truncated. (This shouldn't happen: this is normally handling short strings.)
+
+Hmmph. As always, things get perverted for other uses. This function was
+originally for the "phrase" part of addresses. Now it is being used for much
+longer texts in ACLs and via the ${rfc2047: expansion item. This means we have
+to check for overlong "encoded-word"s and split them. November 2004.
+
+Arguments:
+  string       the string to quote - already checked to contain non-printing
+                 chars
+  len          the length of the string
+  charset      the name of the character set; NULL => iso-8859-1
+  buffer       the buffer to put the answer in
+  buffer_size  the size of the buffer
+  fold         if TRUE, a newline is inserted before the separating space when
+                 more than one encoded-word is generated
+
+Returns:       pointer to the original string, if no quoting needed, or
+               pointer to buffer containing the quoted string, or
+               a pointer to "String too long" if the buffer can't even hold
+               the introduction
+*/
+
+uschar *
+parse_quote_2047(uschar *string, int len, uschar *charset, uschar *buffer,
+  int buffer_size, BOOL fold)
+{
+uschar *s = string;
+uschar *p, *t;
+int hlen;
+BOOL coded = FALSE;
+BOOL first_byte = FALSE;
+
+if (charset == NULL) charset = US"iso-8859-1";
+
+/* We don't expect this to fail! */
+
+if (!string_format(buffer, buffer_size, "=?%s?Q?", charset))
+  return US"String too long";
+
+hlen = Ustrlen(buffer);
+t = buffer + hlen;
+p = buffer;
+
+for (; len > 0; len--)
+  {
+  int ch = *s++;
+  if (t > buffer + buffer_size - hlen - 8) break;
+
+  if ((t - p > 67) && !first_byte)
+    {
+    *t++ = '?';
+    *t++ = '=';
+    if (fold) *t++ = '\n';
+    *t++ = ' ';
+    p = t;
+    Ustrncpy(p, buffer, hlen);
+    t += hlen;
+    }
+
+  if (ch < 33 || ch > 126 ||
+      Ustrchr("?=()<>@,;:\\\".[]_", ch) != NULL)
+    {
+    if (ch == ' ')
+      {
+      *t++ = '_';
+      first_byte = FALSE;
+      } 
+    else
+      {
+      sprintf(CS t, "=%02X", ch);
+      while (*t != 0) t++;
+      coded = TRUE;
+      first_byte = !first_byte;
+      }
+    }
+  else { *t++ = ch; first_byte = FALSE; }
+  }
+
+*t++ = '?';
+*t++ = '=';
+*t = 0;
+
+return coded? buffer : string;
+}
+
+
+
+
+/*************************************************
+*            Fix up an RFC 822 "phrase"          *
+*************************************************/
+
+/* This function is called to repair any syntactic defects in the "phrase" part
+of an RFC822 address. In particular, it is applied to the user's name as read
+from the passwd file when accepting a local message, and to the data from the
+-F option.
+
+If the string contains existing quoted strings or comments containing
+freestanding quotes, then we just quote those bits that need quoting -
+otherwise it would get awfully messy and probably not look good. If not, we
+quote the whole thing if necessary. Thus
+
+   John Q. Smith            =>  "John Q. Smith"
+   John "Jack" Smith        =>  John "Jack" Smith
+   John "Jack" Q. Smith     =>  John "Jack" "Q." Smith
+   John (Jack) Q. Smith     =>  "John (Jack) Q. Smith"
+   John ("Jack") Q. Smith   =>  John ("Jack") "Q." Smith
+but
+   John (\"Jack\") Q. Smith =>  "John (\"Jack\") Q. Smith"
+
+Sheesh! This is tedious code. It is a great pity that the syntax of RFC822 is
+the way it is...
+
+August 2000: Additional code added:
+
+  Previously, non-printing characters were turned into question marks, which do
+  not need to be quoted.
+
+  Now, a different tactic is used if there are any non-printing ASCII
+  characters. The encoding method from RFC 2047 is used, assuming iso-8859-1 as
+  the character set.
+
+  We *could* use this for all cases, getting rid of the messy original code,
+  but leave it for now. It would complicate simple cases like "John Q. Smith".
+
+The result is passed back in the buffer; it is usually going to be added to
+some other string. In order to be sure there is going to be no overflow,
+restrict the length of the input to 1/4 of the buffer size - this allows for
+every single character to be quoted or encoded without overflowing, and that
+wouldn't happen because of amalgamation. If the phrase is too long, return a
+fixed string.
+
+Arguments:
+  phrase       an RFC822 phrase
+  len          the length of the phrase
+  buffer       a buffer to put the result in
+  buffer_size  the size of the buffer
+
+Returns:       the fixed RFC822 phrase
+*/
+
+uschar *
+parse_fix_phrase(uschar *phrase, int len, uschar *buffer, int buffer_size)
+{
+int ch, i;
+BOOL quoted = FALSE;
+uschar *s, *t, *end, *yield;
+
+while (len > 0 && isspace(*phrase)) { phrase++; len--; }
+if (len > buffer_size/4) return US"Name too long";
+
+/* See if there are any non-printing characters, and if so, use the RFC 2047
+encoding for the whole thing. */
+
+for (i = 0, s = phrase; i < len; i++, s++)
+  if ((*s < 32 && *s != '\t') || *s > 126) break;
+
+if (i < len) return parse_quote_2047(phrase, len, headers_charset, buffer,
+  buffer_size, FALSE);
+
+/* No non-printers; use the RFC 822 quoting rules */
+
+s = phrase;
+end = s + len;
+yield = t = buffer + 1;
+
+while (s < end)
+  {
+  ch = *s++;
+
+  /* Copy over quoted strings, remembering we encountered one */
+
+  if (ch == '\"')
+    {
+    *t++ = '\"';
+    while (s < end && (ch = *s++) != '\"')
+      {
+      *t++ = ch;
+      if (ch == '\\' && s < end) *t++ = *s++;
+      }
+    *t++ = '\"';
+    if (s >= end) break;
+    quoted = TRUE;
+    }
+
+  /* Copy over comments, noting if they contain freestanding quote
+  characters */
+
+  else if (ch == '(')
+    {
+    int level = 1;
+    *t++ = '(';
+    while (s < end)
+      {
+      ch = *s++;
+      *t++ = ch;
+      if (ch == '(') level++;
+      else if (ch == ')') { if (--level <= 0) break; }
+      else if (ch == '\\' && s < end) *t++ = *s++ & 127;
+      else if (ch == '\"') quoted = TRUE;
+      }
+    if (ch == 0)
+      {
+      while (level--) *t++ = ')';
+      break;
+      }
+    }
+
+  /* Handle special characters that need to be quoted */
+
+  else if (Ustrchr(")<>@,;:\\.[]", ch) != NULL)
+    {
+    /* If hit previous quotes just make one quoted "word" */
+
+    if (quoted)
+      {
+      uschar *tt = t++;
+      while (*(--tt) != ' ' && *tt != '\"' && *tt != ')') tt[1] = *tt;
+      tt[1] = '\"';
+      *t++ = ch;
+      while (s < end)
+        {
+        ch = *s++;
+        if (ch == ' ' || ch == '\"') { s--; break; } else *t++ = ch;
+        }
+      *t++ = '\"';
+      }
+
+    /* Else quote the whole string so far, and the rest up to any following
+    quotes. We must treat anything following a backslash as a literal. */
+
+    else
+      {
+      BOOL escaped = (ch == '\\');
+      *(--yield) = '\"';
+      *t++ = ch;
+
+      /* Now look for the end or a quote */
+
+      while (s < end)
+        {
+        ch = *s++;
+
+        /* Handle escaped pairs */
+
+        if (escaped)
+          {
+          *t++ = ch;
+          escaped = FALSE;
+          }
+
+        else if (ch == '\\')
+          {
+          *t++ = ch;
+          escaped = TRUE;
+          }
+
+        /* If hit subsequent quotes, insert our quote before any trailing
+        spaces and back up to re-handle the quote in the outer loop. */
+
+        else if (ch == '\"')
+          {
+          int count = 0;
+          while (t[-1] == ' ') { t--; count++; }
+          *t++ = '\"';
+          while (count-- > 0) *t++ = ' ';
+          s--;
+          break;
+          }
+
+        /* If hit a subsequent comment, check it for unescaped quotes,
+        and if so, end our quote before it. */
+
+        else if (ch == '(')
+          {
+          uschar *ss = s;     /* uschar after '(' */
+          int level = 1;
+          while(ss < end)
+            {
+            ch = *ss++;
+            if (ch == '(') level++;
+            else if (ch == ')') { if (--level <= 0) break; }
+            else if (ch == '\\' && ss+1 < end) ss++;
+            else if (ch == '\"') { quoted = TRUE; break; }
+            }
+
+          /* Comment contains unescaped quotes; end our quote before
+          the start of the comment. */
+
+          if (quoted)
+            {
+            int count = 0;
+            while (t[-1] == ' ') { t--; count++; }
+            *t++ = '\"';
+            while (count-- > 0) *t++ = ' ';
+            break;
+            }
+
+          /* Comment does not contain unescaped quotes; include it in
+          our quote. */
+
+          else
+            {
+            if (ss >= end) ss--;
+            *t++ = '(';
+            Ustrncpy(t, s, ss-s);
+            t += ss-s;
+            s = ss;
+            }
+          }
+
+        /* Not a comment or quote; include this character in our quotes. */
+
+        else *t++ = ch;
+        }
+      }
+
+    /* Add a final quote if we hit the end of the string. */
+
+    if (s >= end) *t++ = '\"';
+    }
+
+  /* Non-special character; just copy it over */
+
+  else *t++ = ch;
+  }
+
+*t = 0;
+return yield;
+}
+
+
+/*************************************************
+*          Extract addresses from a list         *
+*************************************************/
+
+/* This function is called by the redirect router to scan a string containing a
+list of addresses separated by commas (with optional white space) or by
+newlines, and to generate a chain of address items from them. In other words,
+to unpick data from an alias or .forward file.
+
+The SunOS5 documentation for alias files is not very clear on the syntax; it
+does not say that either a comma or a newline can be used for separation.
+However, that is the way Smail does it, so we follow suit.
+
+If a # character is encountered in a white space position, then characters from
+there to the next newline are skipped.
+
+If an unqualified address begins with '\', just skip that character. This gives
+compatibility with Sendmail's use of \ to prevent looping. Exim has its own
+loop prevention scheme which handles other cases too - see the code in
+route_address().
+
+An "address" can be a specification of a file or a pipe; the latter may often
+need to be quoted because it may contain spaces, but we don't want to retain
+the quotes. Quotes may appear in normal addresses too, and should be retained.
+We can distinguish between these cases, because in addresses, quotes are used
+only for parts of the address, not the whole thing. Therefore, we remove quotes
+from items when they entirely enclose them, but not otherwise.
+
+An "address" can also be of the form :include:pathname to include a list of
+addresses contained in the specified file.
+
+Any unqualified addresses are qualified with and rewritten if necessary, via
+the rewrite_address() function.
+
+Arguments:
+  s                the list of addresses (typically a complete
+                     .forward file or a list of entries in an alias file)
+  options          option bits for permitting or denying various special cases;
+                     not all bits are relevant here - some are for filter
+                     files; those we use here are:
+                       RDO_DEFER
+                       RDO_FREEZE
+                       RDO_FAIL
+                       RDO_BLACKHOLE
+                       RDO_REWRITE
+                       RDO_INCLUDE
+  anchor           where to hang the chain of newly-created addresses. This
+                     should be initialized to NULL.
+  error            where to return an error text
+  incoming domain  domain of the incoming address; used to qualify unqualified
+                     local parts preceded by \
+  directory        if NULL, no checks are done on :include: files
+                   otherwise, included file names must start with the given
+                     directory
+  syntax_errors    if not NULL, it carries on after syntax errors in addresses,
+                     building up a list of errors as error blocks chained on
+                     here.
+
+Returns:      FF_DELIVERED      addresses extracted
+              FF_NOTDELIVERED   no addresses extracted, but no errors
+              FF_BLACKHOLE      :blackhole:
+              FF_DEFER          :defer:
+              FF_FAIL           :fail:
+              FF_INCLUDEFAIL    some problem with :include:; *error set
+              FF_ERROR          other problems; *error is set
+*/
+
+int
+parse_forward_list(uschar *s, int options, address_item **anchor,
+  uschar **error, uschar *incoming_domain, uschar *directory,
+  error_block **syntax_errors)
+{
+int count = 0;
+
+DEBUG(D_route) debug_printf("parse_forward_list: %s\n", s);
+
+for (;;)
+  {
+  int len;
+  int special = 0;
+  int specopt = 0;
+  int specbit = 0;
+  uschar *ss, *nexts;
+  address_item *addr;
+  BOOL inquote = FALSE;
+
+  for (;;)
+    {
+    while (isspace(*s) || *s == ',') s++;
+    if (*s == '#') { while (*s != 0 && *s != '\n') s++; } else break;
+    }
+
+  /* When we reach the end of the list, we return FF_DELIVERED if any child
+  addresses have been generated. If nothing has been generated, there are two
+  possibilities: either the list is really empty, or there were syntax errors
+  that are being skipped. (If syntax errors are not being skipped, an FF_ERROR
+  return is generated on hitting a syntax error and we don't get here.) For a
+  truly empty list we return FF_NOTDELIVERED so that the router can decline.
+  However, if the list is empty only because syntax errors were skipped, we
+  return FF_DELIVERED. */
+
+  if (*s == 0)
+    {
+    return (count > 0 || (syntax_errors != NULL && *syntax_errors != NULL))?
+      FF_DELIVERED : FF_NOTDELIVERED;
+
+    /* This previous code returns FF_ERROR if nothing is generated but a
+    syntax error has been skipped. I now think it is the wrong approach, but
+    have left this here just in case, and for the record. */
+
+    #ifdef NEVER
+    if (count > 0) return FF_DELIVERED;   /* Something was generated */
+
+    if (syntax_errors == NULL ||          /* Not skipping syntax errors, or */
+       *syntax_errors == NULL)            /*   we didn't actually skip any */
+      return FF_NOTDELIVERED;
+
+    *error = string_sprintf("no addresses generated: syntax error in %s: %s",
+       (*syntax_errors)->text2, (*syntax_errors)->text1);
+    return FF_ERROR;
+    #endif
+
+    }
+
+  /* Find the end of the next address. Quoted strings in addresses may contain
+  escaped characters; I haven't found a proper specification of .forward or
+  alias files that mentions the quoting properties, but it seems right to do
+  the escaping thing in all cases, so use the function that finds the end of an
+  address. However, don't let a quoted string extend over the end of a line. */
+
+  ss = parse_find_address_end(s, TRUE);
+
+  /* Remember where we finished, for starting the next one. */
+
+  nexts = ss;
+
+  /* Remove any trailing spaces; we know there's at least one non-space. */
+
+  while (isspace((ss[-1]))) ss--;
+
+  /* We now have s->start and ss->end of the next address. Remove quotes
+  if they completely enclose, remembering the address started with a quote
+  for handling pipes and files. Another round of removal of leading and
+  trailing spaces is then required. */
+
+  if (*s == '\"' && ss[-1] == '\"')
+    {
+    s++;
+    ss--;
+    inquote = TRUE;
+    while (s < ss && isspace(*s)) s++;
+    while (ss > s && isspace((ss[-1]))) ss--;
+    }
+
+  /* Set up the length of the address. */
+
+  len = ss - s;
+
+  DEBUG(D_route)
+    {
+    int save = s[len];
+    s[len] = 0;
+    debug_printf("extract item: %s\n", s);
+    s[len] = save;
+    }
+
+  /* Handle special addresses if permitted. If the address is :unknown:
+  ignore it - this is for backward compatibility with old alias files. You
+  don't need to use it nowadays - just generate an empty string. For :defer:,
+  :blackhole:, or :fail: we have to set up the error message and give up right
+  away. */
+
+  if (Ustrncmp(s, ":unknown:", len) == 0)
+    {
+    s = nexts;
+    continue;
+    }
+
+  if      (Ustrncmp(s, ":defer:", 7) == 0)
+    { special = FF_DEFER; specopt = RDO_DEFER; }  /* specbit is 0 */
+  else if (Ustrncmp(s, ":blackhole:", 11) == 0)
+    { special = FF_BLACKHOLE; specopt = specbit = RDO_BLACKHOLE; }
+  else if (Ustrncmp(s, ":fail:", 6) == 0)
+    { special = FF_FAIL; specopt = RDO_FAIL; }  /* specbit is 0 */
+
+  if (special != 0)
+    {
+    uschar *ss = Ustrchr(s+1, ':') + 1;
+    if ((options & specopt) == specbit)
+      {
+      *error = string_sprintf("\"%.*s\" is not permitted", len, s);
+      return FF_ERROR;
+      }
+    while (*ss != 0 && isspace(*ss)) ss++;
+    while (s[len] != 0 && s[len] != '\n') len++;
+    s[len] = 0;
+    *error = string_copy(ss);
+    return special;
+    }
+
+  /* If the address is of the form :include:pathname, read the file, and call
+  this function recursively to extract the addresses from it. If directory is
+  NULL, do no checks. Otherwise, insist that the file name starts with the
+  given directory and is a regular file. */
+
+  if (Ustrncmp(s, ":include:", 9) == 0)
+    {
+    uschar *filebuf;
+    uschar filename[256];
+    uschar *t = s+9;
+    int flen = len - 9;
+    int frc;
+    struct stat statbuf;
+    address_item *last;
+    FILE *f;
+
+    while (flen > 0 && isspace(*t)) { t++; flen--; }
+
+    if (flen <= 0)
+      {
+      *error = string_sprintf("file name missing after :include:");
+      return FF_ERROR;
+      }
+
+    if (flen > 255)
+      {
+      *error = string_sprintf("included file name \"%s\" is too long", t);
+      return FF_ERROR;
+      }
+
+    Ustrncpy(filename, t, flen);
+    filename[flen] = 0;
+
+    /* Insist on absolute path */
+
+    if (filename[0]!= '/')
+      {
+      *error = string_sprintf("included file \"%s\" is not an absolute path",
+        filename);
+      return FF_ERROR;
+      }
+
+    /* Check if include is permitted */
+
+    if ((options & RDO_INCLUDE) != 0)
+      {
+      *error = US"included files not permitted";
+      return FF_ERROR;
+      }
+
+    /* Check file name if required */
+
+    if (directory != NULL)
+      {
+      int len = Ustrlen(directory);
+      uschar *p = filename + len;
+
+      if (Ustrncmp(filename, directory, len) != 0 || *p != '/')
+        {
+        *error = string_sprintf("included file %s is not in directory %s",
+          filename, directory);
+        return FF_ERROR;
+        }
+
+      /* It is necessary to check that every component inside the directory
+      is NOT a symbolic link, in order to keep the file inside the directory.
+      This is mighty tedious. It is also not totally foolproof in that it
+      leaves the possibility of a race attack, but I don't know how to do
+      any better. */
+
+      while (*p != 0)
+        {
+        int temp;
+        while (*(++p) != 0 && *p != '/');
+        temp = *p;
+        *p = 0;
+        if (Ulstat(filename, &statbuf) != 0)
+          {
+          *error = string_sprintf("failed to stat %s (component of included "
+            "file)", filename);
+          *p = temp;
+          return FF_ERROR;
+          }
+
+        *p = temp;
+
+        if ((statbuf.st_mode & S_IFMT) == S_IFLNK)
+          {
+          *error = string_sprintf("included file %s in the %s directory "
+            "involves a symbolic link", filename, directory);
+          return FF_ERROR;
+          }
+        }
+      }
+
+    /* Open and stat the file */
+
+    if ((f = Ufopen(filename, "rb")) == NULL)
+      {
+      *error = string_open_failed(errno, "included file %s", filename);
+      return FF_INCLUDEFAIL;
+      }
+
+    if (fstat(fileno(f), &statbuf) != 0)
+      {
+      *error = string_sprintf("failed to stat included file %s: %s",
+        filename, strerror(errno));
+      (void)fclose(f);
+      return FF_INCLUDEFAIL;
+      }
+
+    /* If directory was checked, double check that we opened a regular file */
+
+    if (directory != NULL && (statbuf.st_mode & S_IFMT) != S_IFREG)
+      {
+      *error = string_sprintf("included file %s is not a regular file in "
+        "the %s directory", filename, directory);
+      return FF_ERROR;
+      }
+
+    /* Get a buffer and read the contents */
+
+    if (statbuf.st_size > MAX_INCLUDE_SIZE)
+      {
+      *error = string_sprintf("included file %s is too big (max %d)",
+        filename, MAX_INCLUDE_SIZE);
+      return FF_ERROR;
+      }
+
+    filebuf = store_get(statbuf.st_size + 1);
+    if (fread(filebuf, 1, statbuf.st_size, f) != statbuf.st_size)
+      {
+      *error = string_sprintf("error while reading included file %s: %s",
+        filename, strerror(errno));
+      (void)fclose(f);
+      return FF_ERROR;
+      }
+    filebuf[statbuf.st_size] = 0;
+    (void)fclose(f);
+
+    addr = NULL;
+    frc = parse_forward_list(filebuf, options, &addr,
+      error, incoming_domain, directory, syntax_errors);
+    if (frc != FF_DELIVERED && frc != FF_NOTDELIVERED) return frc;
+
+    if (addr != NULL)
+      {
+      last = addr;
+      while (last->next != NULL) { count++; last = last->next; }
+      last->next = *anchor;
+      *anchor = addr;
+      count++;
+      }
+    }
+
+  /* Else (not :include:) ensure address is syntactically correct and fully
+  qualified if not a pipe or a file, removing a leading \ if present on an
+  unqualified address. For pipes and files we must handle quoting. It's
+  not quite clear exactly what to do for partially quoted things, but the
+  common case of having the whole thing in quotes is straightforward. If this
+  was the case, inquote will have been set TRUE above and the quotes removed.
+
+  There is a possible ambiguity over addresses whose local parts start with
+  a vertical bar or a slash, and the latter do in fact occur, thanks to X.400.
+  Consider a .forward file that contains the line
+
+     /X=xxx/Y=xxx/OU=xxx/@some.gate.way
+
+  Is this a file or an X.400 address? Does it make any difference if it is in
+  quotes? On the grounds that file names of this type are rare, Exim treats
+  something that parses as an RFC 822 address and has a domain as an address
+  rather than a file or a pipe. This is also how an address such as the above
+  would be treated if it came in from outside. */
+
+  else
+    {
+    int start, end, domain;
+    uschar *recipient = NULL;
+    int save = s[len];
+    s[len] = 0;
+
+    /* If it starts with \ and the rest of it parses as a valid mail address
+    without a domain, carry on with that address, but qualify it with the
+    incoming domain. Otherwise arrange for the address to fall through,
+    causing an error message on the re-parse. */
+
+    if (*s == '\\')
+      {
+      recipient =
+        parse_extract_address(s+1, error, &start, &end, &domain, FALSE);
+      if (recipient != NULL)
+        recipient = (domain != 0)? NULL :
+          string_sprintf("%s@%s", recipient, incoming_domain);
+      }
+
+    /* Try parsing the item as an address. */
+
+    if (recipient == NULL) recipient =
+      parse_extract_address(s, error, &start, &end, &domain, FALSE);
+
+    /* If item starts with / or | and is not a valid address, or there
+    is no domain, treat it as a file or pipe. If it was a quoted item,
+    remove the quoting occurrences of \ within it. */
+
+    if ((*s == '|' || *s == '/') && (recipient == NULL || domain == 0))
+      {
+      uschar *t = store_get(Ustrlen(s) + 1);
+      uschar *p = t;
+      uschar *q = s;
+      while (*q != 0)
+        {
+        if (inquote)
+          {
+          *p++ = (*q == '\\')? *(++q) : *q;
+          q++;
+          }
+        else *p++ = *q++;
+        }
+      *p = 0;
+      addr = deliver_make_addr(t, TRUE);
+      setflag(addr, af_pfr);                   /* indicates pipe/file/reply */
+      if (*s != '|') setflag(addr, af_file);   /* indicates file */
+      }
+
+    /* Item must be an address. Complain if not, else qualify, rewrite and set
+    up the control block. It appears that people are in the habit of using
+    empty addresses but with comments as a way of putting comments into
+    alias and forward files. Therefore, ignore the error "empty address".
+    Mailing lists might want to tolerate syntax errors; there is therefore
+    an option to do so. */
+
+    else
+      {
+      if (recipient == NULL)
+        {
+        if (Ustrcmp(*error, "empty address") == 0)
+          {
+          *error = NULL;
+          s[len] = save;
+          s = nexts;
+          continue;
+          }
+
+        if (syntax_errors != NULL)
+          {
+          error_block *e = store_get(sizeof(error_block));
+          error_block *last = *syntax_errors;
+          if (last == NULL) *syntax_errors = e; else
+            {
+            while (last->next != NULL) last = last->next;
+            last->next = e;
+            }
+          e->next = NULL;
+          e->text1 = *error;
+          e->text2 = string_copy(s);
+          s[len] = save;
+          s = nexts;
+          continue;
+          }
+        else
+          {
+          *error = string_sprintf("%s in \"%s\"", *error, s);
+          s[len] = save;   /* _after_ using it for *error */
+          return FF_ERROR;
+          }
+        }
+
+      /* Address was successfully parsed. Rewrite, and then make an address
+      block. */
+
+      recipient = ((options & RDO_REWRITE) != 0)?
+        rewrite_address(recipient, TRUE, FALSE, global_rewrite_rules,
+          rewrite_existflags) :
+        rewrite_address_qualify(recipient, TRUE);
+      addr = deliver_make_addr(recipient, TRUE);  /* TRUE => copy recipient */
+      }
+
+    /* Restore the final character in the original data, and add to the
+    output chain. */
+
+    s[len] = save;
+    addr->next = *anchor;
+    *anchor = addr;
+    count++;
+    }
+
+  /* Advance pointer for the next address */
+
+  s = nexts;
+  }
+}
+
+
+/*************************************************
+*            Extract a Message-ID                *
+*************************************************/
+
+/* This function is used to extract message ids from In-Reply-To: and
+References: header lines.
+
+Arguments:
+  str          pointer to the start of the message-id
+  yield        put pointer to the message id (in dynamic memory) here
+  error        put error message here on failure
+
+Returns:       points after the processed message-id or NULL on error
+*/
+
+uschar *
+parse_message_id(uschar *str, uschar **yield, uschar **error)
+{
+uschar *domain = NULL;
+uschar *id;
+
+str = skip_comment(str);
+if (*str != '<')
+  {
+  *error = US"Missing '<' before message-id";
+  return NULL;
+  }
+
+/* Getting a block the size of the input string will definitely be sufficient
+for the answer, but it may also be very long if we are processing a header
+line. Therefore, take care to release unwanted store afterwards. */
+
+id = *yield = store_get(Ustrlen(str) + 1);
+*id++ = *str++;
+
+str = read_addr_spec(str, id, '>', error, &domain);
+
+if (*error == NULL)
+  {
+  if (*str != '>') *error = US"Missing '>' after message-id";
+    else if (domain == NULL) *error = US"domain missing in message-id";
+  }
+
+if (*error != NULL)
+  {
+  store_reset(*yield);
+  return NULL;
+  }
+
+while (*id != 0) id++;
+*id++ = *str++;
+*id++ = 0;
+store_reset(id);
+
+str = skip_comment(str);
+return str;
+}
+
+
+/*************************************************
+*        Parse a fixed digit number              *
+*************************************************/
+
+/* Parse a string containing an ASCII encoded fixed digits number
+
+Arguments:
+  str          pointer to the start of the ASCII encoded number
+  n            pointer to the resulting value
+  digits       number of required digits
+
+Returns:       points after the processed date or NULL on error
+*/
+
+static uschar *
+parse_number(uschar *str, int *n, int digits)
+{
+  *n=0;
+  while (digits--)
+  {
+    if (*str<'0' || *str>'9') return NULL;
+    *n=10*(*n)+(*str++-'0');
+  }
+  return str;
+}
+
+
+/*************************************************
+*        Parse a RFC 2822 day of week            *
+*************************************************/
+
+/* Parse the day of the week from a RFC 2822 date, but do not
+   decode it, because it is only for humans.
+
+Arguments:
+  str          pointer to the start of the day of the week
+
+Returns:       points after the parsed day or NULL on error
+*/
+
+static uschar *
+parse_day_of_week(uschar *str)
+{
+/*
+day-of-week     =       ([FWS] day-name) / obs-day-of-week
+
+day-name        =       "Mon" / "Tue" / "Wed" / "Thu" /
+                        "Fri" / "Sat" / "Sun"
+
+obs-day-of-week =       [CFWS] day-name [CFWS]
+*/
+
+static const uschar *day_name[7]={ US"mon", US"tue", US"wed", US"thu", US"fri", US"sat", US"sun" };
+int i;
+uschar day[4];
+
+str=skip_comment(str);
+for (i=0; i<3; ++i)
+  {
+  if ((day[i]=tolower(*str))=='\0') return NULL;
+  ++str;
+  }
+day[3]='\0';
+for (i=0; i<7; ++i) if (Ustrcmp(day,day_name[i])==0) break;
+if (i==7) return NULL;
+str=skip_comment(str);
+return str;
+}
+
+
+/*************************************************
+*            Parse a RFC 2822 date               *
+*************************************************/
+
+/* Parse the date part of a RFC 2822 date-time, extracting the
+   day, month and year.
+
+Arguments:
+  str          pointer to the start of the date
+  d            pointer to the resulting day
+  m            pointer to the resulting month
+  y            pointer to the resulting year
+
+Returns:       points after the processed date or NULL on error
+*/
+
+static uschar *
+parse_date(uschar *str, int *d, int *m, int *y)
+{
+/*
+date            =       day month year
+
+year            =       4*DIGIT / obs-year
+
+obs-year        =       [CFWS] 2*DIGIT [CFWS]
+
+month           =       (FWS month-name FWS) / obs-month
+
+month-name      =       "Jan" / "Feb" / "Mar" / "Apr" /
+                        "May" / "Jun" / "Jul" / "Aug" /
+                        "Sep" / "Oct" / "Nov" / "Dec"
+
+obs-month       =       CFWS month-name CFWS
+
+day             =       ([FWS] 1*2DIGIT) / obs-day
+
+obs-day         =       [CFWS] 1*2DIGIT [CFWS]
+*/
+
+uschar *c,*n;
+static const uschar *month_name[]={ US"jan", US"feb", US"mar", US"apr", US"may", US"jun", US"jul", US"aug", US"sep", US"oct", US"nov", US"dec" };
+int i;
+uschar month[4];
+
+str=skip_comment(str);
+if ((str=parse_number(str,d,1))==NULL) return NULL;
+if (*str>='0' && *str<='9') *d=10*(*d)+(*str++-'0');
+c=skip_comment(str);
+if (c==str) return NULL;
+else str=c;
+for (i=0; i<3; ++i) if ((month[i]=tolower(*(str+i)))=='\0') return NULL;
+month[3]='\0';
+for (i=0; i<12; ++i) if (Ustrcmp(month,month_name[i])==0) break;
+if (i==12) return NULL;
+str+=3;
+*m=i;
+c=skip_comment(str);
+if (c==str) return NULL;
+else str=c;
+if ((n=parse_number(str,y,4)))
+  {
+  str=n;
+  if (*y<1900) return NULL;
+  *y=*y-1900;
+  }
+else if ((n=parse_number(str,y,2)))
+  {
+  str=skip_comment(n);
+  while (*(str-1)==' ' || *(str-1)=='\t') --str; /* match last FWS later */
+  if (*y<50) *y+=100;
+  }
+else return NULL;
+return str;
+}
+
+
+/*************************************************
+*            Parse a RFC 2822 Time               *
+*************************************************/
+
+/* Parse the time part of a RFC 2822 date-time, extracting the
+   hour, minute, second and timezone.
+
+Arguments:
+  str          pointer to the start of the time
+  h            pointer to the resulting hour
+  m            pointer to the resulting minute
+  s            pointer to the resulting second
+  z            pointer to the resulting timezone (offset in seconds)
+
+Returns:       points after the processed time or NULL on error
+*/
+
+static uschar *
+parse_time(uschar *str, int *h, int *m, int *s, int *z)
+{
+/*
+time            =       time-of-day FWS zone
+
+time-of-day     =       hour ":" minute [ ":" second ]
+
+hour            =       2DIGIT / obs-hour
+
+obs-hour        =       [CFWS] 2DIGIT [CFWS]
+
+minute          =       2DIGIT / obs-minute
+
+obs-minute      =       [CFWS] 2DIGIT [CFWS]
+
+second          =       2DIGIT / obs-second
+
+obs-second      =       [CFWS] 2DIGIT [CFWS]
+
+zone            =       (( "+" / "-" ) 4DIGIT) / obs-zone
+
+obs-zone        =       "UT" / "GMT" /          ; Universal Time
+                                                ; North American UT
+                                                ; offsets
+                        "EST" / "EDT" /         ; Eastern:  - 5/ - 4
+                        "CST" / "CDT" /         ; Central:  - 6/ - 5
+                        "MST" / "MDT" /         ; Mountain: - 7/ - 6
+                        "PST" / "PDT" /         ; Pacific:  - 8/ - 7
+
+                        %d65-73 /               ; Military zones - "A"
+                        %d75-90 /               ; through "I" and "K"
+                        %d97-105 /              ; through "Z", both
+                        %d107-122               ; upper and lower case
+*/
+
+uschar *c;
+
+str=skip_comment(str);
+if ((str=parse_number(str,h,2))==NULL) return NULL;
+str=skip_comment(str);
+if (*str!=':') return NULL;
+++str;
+str=skip_comment(str);
+if ((str=parse_number(str,m,2))==NULL) return NULL;
+c=skip_comment(str);
+if (*str==':')
+  {
+  ++str;
+  str=skip_comment(str);
+  if ((str=parse_number(str,s,2))==NULL) return NULL;
+  c=skip_comment(str);
+  }
+if (c==str) return NULL;
+else str=c;
+if (*str=='+' || *str=='-')
+  {
+  int neg;
+
+  neg=(*str=='-');
+  ++str;
+  if ((str=parse_number(str,z,4))==NULL) return NULL;
+  *z=(*z/100)*3600+(*z%100)*60;
+  if (neg) *z=-*z;
+  }
+else
+  {
+  char zone[5];
+  struct { const char *name; int off; } zone_name[10]=
+  { {"gmt",0}, {"ut",0}, {"est",-5}, {"edt",-4}, {"cst",-6}, {"cdt",-5}, {"mst",-7}, {"mdt",-6}, {"pst",-8}, {"pdt",-7}};
+  int i,j;
+
+  for (i=0; i<4; ++i)
+    {
+    zone[i]=tolower(*(str+i));
+    if (zone[i]<'a' || zone[i]>'z') break;
+    }
+  zone[i]='\0';
+  for (j=0; j<10 && strcmp(zone,zone_name[j].name); ++j);
+  /* Besides zones named in the grammar, RFC 2822 says other alphabetic */
+  /* time zones should be treated as unknown offsets. */
+  if (j<10)
+    {
+    *z=zone_name[j].off*3600;
+    str+=i;
+    }
+  else if (zone[0]<'a' || zone[1]>'z') return 0;
+  else
+    {
+    while ((*str>='a' && *str<='z') || (*str>='A' && *str<='Z')) ++str;
+    *z=0;
+    }
+  }
+return str;
+}
+
+
+/*************************************************
+*          Parse a RFC 2822 date-time            *
+*************************************************/
+
+/* Parse a RFC 2822 date-time and return it in seconds since the epoch.
+
+Arguments:
+  str          pointer to the start of the date-time
+  t            pointer to the parsed time
+
+Returns:       points after the processed date-time or NULL on error
+*/
+
+uschar *
+parse_date_time(uschar *str, time_t *t)
+{
+/*
+date-time       =       [ day-of-week "," ] date FWS time [CFWS]
+*/
+
+struct tm tm;
+int zone;
+extern char **environ;
+char **old_environ;
+static char gmt0[]="TZ=GMT0";
+static char *gmt_env[]={ gmt0, (char*)0 };
+uschar *try;
+
+if ((try=parse_day_of_week(str)))
+  {
+  str=try;
+  if (*str!=',') return 0;
+  ++str;
+  }
+if ((str=parse_date(str,&tm.tm_mday,&tm.tm_mon,&tm.tm_year))==NULL) return NULL;
+if (*str!=' ' && *str!='\t') return NULL;
+while (*str==' ' || *str=='\t') ++str;
+if ((str=parse_time(str,&tm.tm_hour,&tm.tm_min,&tm.tm_sec,&zone))==NULL) return NULL;
+tm.tm_isdst=0;
+old_environ=environ;
+environ=gmt_env;
+*t=mktime(&tm);
+environ=old_environ;
+if (*t==-1) return NULL;
+*t-=zone;
+str=skip_comment(str);
+return str;
+}
+
+
+
+
+/*************************************************
+**************************************************
+*             Stand-alone test program           *
+**************************************************
+*************************************************/
+
+#if defined STAND_ALONE
+int main(void)
+{
+int start, end, domain;
+uschar buffer[1024];
+uschar outbuff[1024];
+
+big_buffer = store_malloc(big_buffer_size);
+
+/* strip_trailing_dot = TRUE; */
+allow_domain_literals = TRUE;
+
+printf("Testing parse_fix_phrase\n");
+
+while (Ufgets(buffer, sizeof(buffer), stdin) != NULL)
+  {
+  buffer[Ustrlen(buffer)-1] = 0;
+  if (buffer[0] == 0) break;
+  printf("%s\n", CS parse_fix_phrase(buffer, Ustrlen(buffer), outbuff,
+    sizeof(outbuff)));
+  }
+
+printf("Testing parse_extract_address without group syntax and without UTF-8\n");
+
+while (Ufgets(buffer, sizeof(buffer), stdin) != NULL)
+  {
+  uschar *out;
+  uschar *errmess;
+  buffer[Ustrlen(buffer) - 1] = 0;
+  if (buffer[0] == 0) break;
+  out = parse_extract_address(buffer, &errmess, &start, &end, &domain, FALSE);
+  if (out == NULL) printf("*** bad address: %s\n", errmess); else
+    {
+    uschar extract[1024];
+    Ustrncpy(extract, buffer+start, end-start);
+    extract[end-start] = 0;
+    printf("%s %d %d %d \"%s\"\n", out, start, end, domain, extract);
+    }
+  }
+
+printf("Testing parse_extract_address without group syntax but with UTF-8\n");
+
+allow_utf8_domains = TRUE;
+while (Ufgets(buffer, sizeof(buffer), stdin) != NULL)
+  {
+  uschar *out;
+  uschar *errmess;
+  buffer[Ustrlen(buffer) - 1] = 0;
+  if (buffer[0] == 0) break;
+  out = parse_extract_address(buffer, &errmess, &start, &end, &domain, FALSE);
+  if (out == NULL) printf("*** bad address: %s\n", errmess); else
+    {
+    uschar extract[1024];
+    Ustrncpy(extract, buffer+start, end-start);
+    extract[end-start] = 0;
+    printf("%s %d %d %d \"%s\"\n", out, start, end, domain, extract);
+    }
+  }
+allow_utf8_domains = FALSE;
+
+printf("Testing parse_extract_address with group syntax\n");
+
+parse_allow_group = TRUE;
+while (Ufgets(buffer, sizeof(buffer), stdin) != NULL)
+  {
+  uschar *out;
+  uschar *errmess;
+  uschar *s;
+  buffer[Ustrlen(buffer) - 1] = 0;
+  if (buffer[0] == 0) break;
+  s = buffer;
+  while (*s != 0)
+    {
+    uschar *ss = parse_find_address_end(s, FALSE);
+    int terminator = *ss;
+    *ss = 0;
+    out = parse_extract_address(buffer, &errmess, &start, &end, &domain, FALSE);
+    *ss = terminator;
+
+    if (out == NULL) printf("*** bad address: %s\n", errmess); else
+      {
+      uschar extract[1024];
+      Ustrncpy(extract, buffer+start, end-start);
+      extract[end-start] = 0;
+      printf("%s %d %d %d \"%s\"\n", out, start, end, domain, extract);
+      }
+
+    s = ss + (terminator? 1:0);
+    while (isspace(*s)) s++;
+    }
+  }
+
+printf("Testing parse_find_at\n");
+
+while (Ufgets(buffer, sizeof(buffer), stdin) != NULL)
+  {
+  uschar *s;
+  buffer[Ustrlen(buffer)-1] = 0;
+  if (buffer[0] == 0) break;
+  s = parse_find_at(buffer);
+  if (s == NULL) printf("no @ found\n");
+    else printf("offset = %d\n", s - buffer);
+  }
+
+printf("Testing parse_extract_addresses\n");
+
+while (Ufgets(buffer, sizeof(buffer), stdin) != NULL)
+  {
+  uschar *errmess;
+  int extracted;
+  address_item *anchor = NULL;
+  buffer[Ustrlen(buffer) - 1] = 0;
+  if (buffer[0] == 0) break;
+  if ((extracted = parse_forward_list(buffer, -1, &anchor,
+      &errmess, US"incoming.domain", NULL, NULL)) == FF_DELIVERED)
+    {
+    while (anchor != NULL)
+      {
+      address_item *addr = anchor;
+      anchor = anchor->next;
+      printf("%d %s\n", testflag(addr, af_pfr), addr->address);
+      }
+    }
+  else printf("Failed: %d %s\n", extracted, errmess);
+  }
+
+printf("Testing parse_message_id\n");
+
+while (Ufgets(buffer, sizeof(buffer), stdin) != NULL)
+  {
+  uschar *s, *t, *errmess;
+  buffer[Ustrlen(buffer) - 1] = 0;
+  if (buffer[0] == 0) break;
+  s = buffer;
+  while (*s != 0)
+    {
+    s = parse_message_id(s, &t, &errmess);
+    if (errmess != NULL)
+      {
+      printf("Failed: %s\n", errmess);
+      break;
+      }
+    printf("%s\n", t);
+    }
+  }
+
+return 0;
+}
+
+#endif
+
+/* End of parse.c */
diff --git a/src/pdkim/Makefile b/src/pdkim/Makefile
new file mode 100644 (file)
index 0000000..610c011
--- /dev/null
@@ -0,0 +1,22 @@
+# Make file for building the pdkim library.
+
+OBJ = base64.o bignum.o pdkim.o rsa.o sha1.o sha2.o
+
+pdkim.a:         $(OBJ)
+                @$(RM_COMMAND) -f pdkim.a
+                @echo "$(AR) pdkim.a"
+                $(FE)$(AR) pdkim.a $(OBJ)
+                $(RANLIB) $@
+
+.SUFFIXES:       .o .c
+.c.o:;           @echo "$(CC) $*.c"
+                $(FE)$(CC) -c $(CFLAGS) $(INCLUDE) $*.c
+
+base64.o:           $(HDRS) base64.c
+bignum.o:           $(HDRS) bignum.c
+pdkim.o:            $(HDRS) pdkim.c
+rsa.o:              $(HDRS) rsa.c
+sha1.o:             $(HDRS) sha1.c
+sha2.o:             $(HDRS) sha2.c
+
+# End
diff --git a/src/pdkim/README b/src/pdkim/README
new file mode 100644 (file)
index 0000000..de04cff
--- /dev/null
@@ -0,0 +1,11 @@
+PDKIM - a RFC4871 (DKIM) implementation
+http://duncanthrax.net/pdkim/
+Copyright (C) 2009      Tom Kistner <tom@duncanthrax.net>
+
+Includes code from the PolarSSL project.
+http://polarssl.org
+Copyright (C) 2009      Paul Bakker <polarssl_maintainer@polarssl.org>
+Copyright (C) 2006-2008 Christophe Devine
+
+This copy of PDKIM is included with Exim. For a standalone distribution,
+visit http://duncanthrax.net/pdkim/.
diff --git a/src/pdkim/base64.c b/src/pdkim/base64.c
new file mode 100644 (file)
index 0000000..a82fc2d
--- /dev/null
@@ -0,0 +1,181 @@
+/*
+ *  RFC 1521 base64 encoding/decoding
+ *
+ *  Copyright (C) 2006-2010, Brainspark B.V.
+ *
+ *  This file is part of PolarSSL (http://www.polarssl.org)
+ *  Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
+ *
+ *  All rights reserved.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License along
+ *  with this program; if not, write to the Free Software Foundation, Inc.,
+ *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include "base64.h"
+
+static const unsigned char base64_enc_map[64] =
+{
+    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
+    'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
+    'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
+    'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
+    'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
+    'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7',
+    '8', '9', '+', '/'
+};
+
+static const unsigned char base64_dec_map[128] =
+{
+    127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
+    127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
+    127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
+    127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
+    127, 127, 127,  62, 127, 127, 127,  63,  52,  53,
+     54,  55,  56,  57,  58,  59,  60,  61, 127, 127,
+    127,  64, 127, 127, 127,   0,   1,   2,   3,   4,
+      5,   6,   7,   8,   9,  10,  11,  12,  13,  14,
+     15,  16,  17,  18,  19,  20,  21,  22,  23,  24,
+     25, 127, 127, 127, 127, 127, 127,  26,  27,  28,
+     29,  30,  31,  32,  33,  34,  35,  36,  37,  38,
+     39,  40,  41,  42,  43,  44,  45,  46,  47,  48,
+     49,  50,  51, 127, 127, 127, 127, 127
+};
+
+/*
+ * Encode a buffer into base64 format
+ */
+int base64_encode( unsigned char *dst, int *dlen,
+                   const unsigned char *src, int  slen )
+{
+    int i, n;
+    int C1, C2, C3;
+    unsigned char *p;
+
+    if( slen == 0 )
+        return( 0 );
+
+    n = (slen << 3) / 6;
+
+    switch( (slen << 3) - (n * 6) )
+    {
+        case  2: n += 3; break;
+        case  4: n += 2; break;
+        default: break;
+    }
+
+    if( *dlen < n + 1 )
+    {
+        *dlen = n + 1;
+        return( POLARSSL_ERR_BASE64_BUFFER_TOO_SMALL );
+    }
+
+    n = (slen / 3) * 3;
+
+    for( i = 0, p = dst; i < n; i += 3 )
+    {
+        C1 = *src++;
+        C2 = *src++;
+        C3 = *src++;
+
+        *p++ = base64_enc_map[(C1 >> 2) & 0x3F];
+        *p++ = base64_enc_map[(((C1 &  3) << 4) + (C2 >> 4)) & 0x3F];
+        *p++ = base64_enc_map[(((C2 & 15) << 2) + (C3 >> 6)) & 0x3F];
+        *p++ = base64_enc_map[C3 & 0x3F];
+    }
+
+    if( i < slen )
+    {
+        C1 = *src++;
+        C2 = ((i + 1) < slen) ? *src++ : 0;
+
+        *p++ = base64_enc_map[(C1 >> 2) & 0x3F];
+        *p++ = base64_enc_map[(((C1 & 3) << 4) + (C2 >> 4)) & 0x3F];
+
+        if( (i + 1) < slen )
+             *p++ = base64_enc_map[((C2 & 15) << 2) & 0x3F];
+        else *p++ = '=';
+
+        *p++ = '=';
+    }
+
+    *dlen = p - dst;
+    *p = 0;
+
+    return( 0 );
+}
+
+/*
+ * Decode a base64-formatted buffer
+ */
+int base64_decode( unsigned char *dst, int *dlen,
+                   const unsigned char *src, int  slen )
+{
+    int i, j, n;
+    unsigned long x;
+    unsigned char *p;
+
+    for( i = j = n = 0; i < slen; i++ )
+    {
+        if( ( slen - i ) >= 2 &&
+            src[i] == '\r' && src[i + 1] == '\n' )
+            continue;
+
+        if( src[i] == '\n' )
+            continue;
+
+        if( src[i] == '=' && ++j > 2 )
+            return( POLARSSL_ERR_BASE64_INVALID_CHARACTER );
+
+        if( src[i] > 127 || base64_dec_map[src[i]] == 127 )
+            return( POLARSSL_ERR_BASE64_INVALID_CHARACTER );
+
+        if( base64_dec_map[src[i]] < 64 && j != 0 )
+            return( POLARSSL_ERR_BASE64_INVALID_CHARACTER );
+
+        n++;
+    }
+
+    if( n == 0 )
+        return( 0 );
+
+    n = ((n * 6) + 7) >> 3;
+
+    if( *dlen < n )
+    {
+        *dlen = n;
+        return( POLARSSL_ERR_BASE64_BUFFER_TOO_SMALL );
+    }
+
+   for( j = 3, n = x = 0, p = dst; i > 0; i--, src++ )
+   {
+        if( *src == '\r' || *src == '\n' )
+            continue;
+
+        j -= ( base64_dec_map[*src] == 64 );
+        x  = (x << 6) | ( base64_dec_map[*src] & 0x3F );
+
+        if( ++n == 4 )
+        {
+            n = 0;
+            if( j > 0 ) *p++ = (unsigned char)( x >> 16 );
+            if( j > 1 ) *p++ = (unsigned char)( x >>  8 );
+            if( j > 2 ) *p++ = (unsigned char)( x       );
+        }
+    }
+
+    *dlen = p - dst;
+
+    return( 0 );
+}
diff --git a/src/pdkim/base64.h b/src/pdkim/base64.h
new file mode 100644 (file)
index 0000000..a3fd395
--- /dev/null
@@ -0,0 +1,77 @@
+/**
+ * \file base64.h
+ *
+ *  Copyright (C) 2006-2010, Brainspark B.V.
+ *
+ *  This file is part of PolarSSL (http://www.polarssl.org)
+ *  Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
+ *
+ *  All rights reserved.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License along
+ *  with this program; if not, write to the Free Software Foundation, Inc.,
+ *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef POLARSSL_BASE64_H
+#define POLARSSL_BASE64_H
+
+#define POLARSSL_ERR_BASE64_BUFFER_TOO_SMALL               0x0010
+#define POLARSSL_ERR_BASE64_INVALID_CHARACTER              0x0012
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * \brief          Encode a buffer into base64 format
+ *
+ * \param dst      destination buffer
+ * \param dlen     size of the buffer
+ * \param src      source buffer
+ * \param slen     amount of data to be encoded
+ *
+ * \return         0 if successful, or POLARSSL_ERR_BASE64_BUFFER_TOO_SMALL.
+ *                 *dlen is always updated to reflect the amount
+ *                 of data that has (or would have) been written.
+ *
+ * \note           Call this function with *dlen = 0 to obtain the
+ *                 required buffer size in *dlen
+ */
+int base64_encode( unsigned char *dst, int *dlen,
+                   const unsigned char *src, int  slen );
+
+/**
+ * \brief          Decode a base64-formatted buffer
+ *
+ * \param dst      destination buffer
+ * \param dlen     size of the buffer
+ * \param src      source buffer
+ * \param slen     amount of data to be decoded
+ *
+ * \return         0 if successful, POLARSSL_ERR_BASE64_BUFFER_TOO_SMALL, or
+ *                 POLARSSL_ERR_BASE64_INVALID_DATA if the input data is not
+ *                 correct. *dlen is always updated to reflect the amount
+ *                 of data that has (or would have) been written.
+ *
+ * \note           Call this function with *dlen = 0 to obtain the
+ *                 required buffer size in *dlen
+ */
+int base64_decode( unsigned char *dst, int *dlen,
+                   const unsigned char *src, int  slen );
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* base64.h */
diff --git a/src/pdkim/bignum.c b/src/pdkim/bignum.c
new file mode 100644 (file)
index 0000000..692fe82
--- /dev/null
@@ -0,0 +1,1865 @@
+/*
+ *  Multi-precision integer library
+ *
+ *  Copyright (C) 2006-2010, Brainspark B.V.
+ *
+ *  This file is part of PolarSSL (http://www.polarssl.org)
+ *  Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
+ *
+ *  All rights reserved.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License along
+ *  with this program; if not, write to the Free Software Foundation, Inc.,
+ *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+/*
+ *  This MPI implementation is based on:
+ *
+ *  http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf
+ *  http://www.stillhq.com/extracted/gnupg-api/mpi/
+ *  http://math.libtomcrypt.com/files/tommath.pdf
+ */
+
+#include "bignum.h"
+#include "bn_mul.h"
+
+#include <string.h>
+#include <stdlib.h>
+#include <stdarg.h>
+
+#define ciL    ((int) sizeof(t_int))    /* chars in limb  */
+#define biL    (ciL << 3)               /* bits  in limb  */
+#define biH    (ciL << 2)               /* half limb size */
+
+/*
+ * Convert between bits/chars and number of limbs
+ */
+#define BITS_TO_LIMBS(i)  (((i) + biL - 1) / biL)
+#define CHARS_TO_LIMBS(i) (((i) + ciL - 1) / ciL)
+
+/*
+ * Initialize one or more mpi
+ */
+void mpi_init( mpi *X, ... )
+{
+    va_list args;
+
+    va_start( args, X );
+
+    while( X != NULL )
+    {
+        X->s = 1;
+        X->n = 0;
+        X->p = NULL;
+
+        X = va_arg( args, mpi* );
+    }
+
+    va_end( args );
+}
+
+/*
+ * Unallocate one or more mpi
+ */
+void mpi_free( mpi *X, ... )
+{
+    va_list args;
+
+    va_start( args, X );
+
+    while( X != NULL )
+    {
+        if( X->p != NULL )
+        {
+            memset( X->p, 0, X->n * ciL );
+            free( X->p );
+        }
+
+        X->s = 1;
+        X->n = 0;
+        X->p = NULL;
+
+        X = va_arg( args, mpi* );
+    }
+
+    va_end( args );
+}
+
+/*
+ * Enlarge to the specified number of limbs
+ */
+int mpi_grow( mpi *X, int nblimbs )
+{
+    t_int *p;
+
+    if( X->n < nblimbs )
+    {
+        if( ( p = (t_int *) malloc( nblimbs * ciL ) ) == NULL )
+            return( 1 );
+
+        memset( p, 0, nblimbs * ciL );
+
+        if( X->p != NULL )
+        {
+            memcpy( p, X->p, X->n * ciL );
+            memset( X->p, 0, X->n * ciL );
+            free( X->p );
+        }
+
+        X->n = nblimbs;
+        X->p = p;
+    }
+
+    return( 0 );
+}
+
+/*
+ * Copy the contents of Y into X
+ */
+int mpi_copy( mpi *X, const mpi *Y )
+{
+    int ret, i;
+
+    if( X == Y )
+        return( 0 );
+
+    for( i = Y->n - 1; i > 0; i-- )
+        if( Y->p[i] != 0 )
+            break;
+    i++;
+
+    X->s = Y->s;
+
+    MPI_CHK( mpi_grow( X, i ) );
+
+    memset( X->p, 0, X->n * ciL );
+    memcpy( X->p, Y->p, i * ciL );
+
+cleanup:
+
+    return( ret );
+}
+
+/*
+ * Swap the contents of X and Y
+ */
+void mpi_swap( mpi *X, mpi *Y )
+{
+    mpi T;
+
+    memcpy( &T,  X, sizeof( mpi ) );
+    memcpy(  X,  Y, sizeof( mpi ) );
+    memcpy(  Y, &T, sizeof( mpi ) );
+}
+
+/*
+ * Set value from integer
+ */
+int mpi_lset( mpi *X, int z )
+{
+    int ret;
+
+    MPI_CHK( mpi_grow( X, 1 ) );
+    memset( X->p, 0, X->n * ciL );
+
+    X->p[0] = ( z < 0 ) ? -z : z;
+    X->s    = ( z < 0 ) ? -1 : 1;
+
+cleanup:
+
+    return( ret );
+}
+
+/*
+ * Return the number of least significant bits
+ */
+int mpi_lsb( const mpi *X )
+{
+    int i, j, count = 0;
+
+    for( i = 0; i < X->n; i++ )
+        for( j = 0; j < (int) biL; j++, count++ )
+            if( ( ( X->p[i] >> j ) & 1 ) != 0 )
+                return( count );
+
+    return( 0 );
+}
+
+/*
+ * Return the number of most significant bits
+ */
+int mpi_msb( const mpi *X )
+{
+    int i, j;
+
+    for( i = X->n - 1; i > 0; i-- )
+        if( X->p[i] != 0 )
+            break;
+
+    for( j = biL - 1; j >= 0; j-- )
+        if( ( ( X->p[i] >> j ) & 1 ) != 0 )
+            break;
+
+    return( ( i * biL ) + j + 1 );
+}
+
+/*
+ * Return the total size in bytes
+ */
+int mpi_size( const mpi *X )
+{
+    return( ( mpi_msb( X ) + 7 ) >> 3 );
+}
+
+/*
+ * Convert an ASCII character to digit value
+ */
+static int mpi_get_digit( t_int *d, int radix, char c )
+{
+    *d = 255;
+
+    if( c >= 0x30 && c <= 0x39 ) *d = c - 0x30;
+    if( c >= 0x41 && c <= 0x46 ) *d = c - 0x37;
+    if( c >= 0x61 && c <= 0x66 ) *d = c - 0x57;
+
+    if( *d >= (t_int) radix )
+        return( POLARSSL_ERR_MPI_INVALID_CHARACTER );
+
+    return( 0 );
+}
+
+/*
+ * Import from an ASCII string
+ */
+int mpi_read_string( mpi *X, int radix, const char *s )
+{
+    int ret, i, j, n, slen;
+    t_int d;
+    mpi T;
+
+    if( radix < 2 || radix > 16 )
+        return( POLARSSL_ERR_MPI_BAD_INPUT_DATA );
+
+    mpi_init( &T, NULL );
+
+    slen = strlen( s );
+
+    if( radix == 16 )
+    {
+        n = BITS_TO_LIMBS( slen << 2 );
+
+        MPI_CHK( mpi_grow( X, n ) );
+        MPI_CHK( mpi_lset( X, 0 ) );
+
+        for( i = slen - 1, j = 0; i >= 0; i--, j++ )
+        {
+            if( i == 0 && s[i] == '-' )
+            {
+                X->s = -1;
+                break;
+            }
+
+            MPI_CHK( mpi_get_digit( &d, radix, s[i] ) );
+            X->p[j / (2 * ciL)] |= d << ( (j % (2 * ciL)) << 2 );
+        }
+    }
+    else
+    {
+        MPI_CHK( mpi_lset( X, 0 ) );
+
+        for( i = 0; i < slen; i++ )
+        {
+            if( i == 0 && s[i] == '-' )
+            {
+                X->s = -1;
+                continue;
+            }
+
+            MPI_CHK( mpi_get_digit( &d, radix, s[i] ) );
+            MPI_CHK( mpi_mul_int( &T, X, radix ) );
+
+            if( X->s == 1 )
+            {
+                MPI_CHK( mpi_add_int( X, &T, d ) );
+            }
+            else
+            {
+                MPI_CHK( mpi_sub_int( X, &T, d ) );
+            }
+        }
+    }
+
+cleanup:
+
+    mpi_free( &T, NULL );
+
+    return( ret );
+}
+
+/*
+ * Helper to write the digits high-order first
+ */
+static int mpi_write_hlp( mpi *X, int radix, char **p )
+{
+    int ret;
+    t_int r;
+
+    if( radix < 2 || radix > 16 )
+        return( POLARSSL_ERR_MPI_BAD_INPUT_DATA );
+
+    MPI_CHK( mpi_mod_int( &r, X, radix ) );
+    MPI_CHK( mpi_div_int( X, NULL, X, radix ) );
+
+    if( mpi_cmp_int( X, 0 ) != 0 )
+        MPI_CHK( mpi_write_hlp( X, radix, p ) );
+
+    if( r < 10 )
+        *(*p)++ = (char)( r + 0x30 );
+    else
+        *(*p)++ = (char)( r + 0x37 );
+
+cleanup:
+
+    return( ret );
+}
+
+/*
+ * Export into an ASCII string
+ */
+int mpi_write_string( const mpi *X, int radix, char *s, int *slen )
+{
+    int ret = 0, n;
+    char *p;
+    mpi T;
+
+    if( radix < 2 || radix > 16 )
+        return( POLARSSL_ERR_MPI_BAD_INPUT_DATA );
+
+    n = mpi_msb( X );
+    if( radix >=  4 ) n >>= 1;
+    if( radix >= 16 ) n >>= 1;
+    n += 3;
+
+    if( *slen < n )
+    {
+        *slen = n;
+        return( POLARSSL_ERR_MPI_BUFFER_TOO_SMALL );
+    }
+
+    p = s;
+    mpi_init( &T, NULL );
+
+    if( X->s == -1 )
+        *p++ = '-';
+
+    if( radix == 16 )
+    {
+        int c, i, j, k;
+
+        for( i = X->n - 1, k = 0; i >= 0; i-- )
+        {
+            for( j = ciL - 1; j >= 0; j-- )
+            {
+                c = ( X->p[i] >> (j << 3) ) & 0xFF;
+
+                if( c == 0 && k == 0 && (i + j) != 0 )
+                    continue;
+
+                p += sprintf( p, "%02X", c );
+                k = 1;
+            }
+        }
+    }
+    else
+    {
+        MPI_CHK( mpi_copy( &T, X ) );
+
+        if( T.s == -1 )
+            T.s = 1;
+
+        MPI_CHK( mpi_write_hlp( &T, radix, &p ) );
+    }
+
+    *p++ = '\0';
+    *slen = p - s;
+
+cleanup:
+
+    mpi_free( &T, NULL );
+
+    return( ret );
+}
+
+/*
+ * Read X from an opened file
+ */
+int mpi_read_file( mpi *X, int radix, FILE *fin )
+{
+    t_int d;
+    int slen;
+    char *p;
+    char s[1024];
+
+    memset( s, 0, sizeof( s ) );
+    if( fgets( s, sizeof( s ) - 1, fin ) == NULL )
+        return( POLARSSL_ERR_MPI_FILE_IO_ERROR );
+
+    slen = strlen( s );
+    if( s[slen - 1] == '\n' ) { slen--; s[slen] = '\0'; }
+    if( s[slen - 1] == '\r' ) { slen--; s[slen] = '\0'; }
+
+    p = s + slen;
+    while( --p >= s )
+        if( mpi_get_digit( &d, radix, *p ) != 0 )
+            break;
+
+    return( mpi_read_string( X, radix, p + 1 ) );
+}
+
+/*
+ * Write X into an opened file (or stdout if fout == NULL)
+ */
+int mpi_write_file( const char *p, const mpi *X, int radix, FILE *fout )
+{
+    int n, ret;
+    size_t slen;
+    size_t plen;
+    char s[2048];
+
+    n = sizeof( s );
+    memset( s, 0, n );
+    n -= 2;
+
+    MPI_CHK( mpi_write_string( X, radix, s, (int *) &n ) );
+
+    if( p == NULL ) p = "";
+
+    plen = strlen( p );
+    slen = strlen( s );
+    s[slen++] = '\r';
+    s[slen++] = '\n';
+
+    if( fout != NULL )
+    {
+        if( fwrite( p, 1, plen, fout ) != plen ||
+            fwrite( s, 1, slen, fout ) != slen )
+            return( POLARSSL_ERR_MPI_FILE_IO_ERROR );
+    }
+    else
+        printf( "%s%s", p, s );
+
+cleanup:
+
+    return( ret );
+}
+
+/*
+ * Import X from unsigned binary data, big endian
+ */
+int mpi_read_binary( mpi *X, const unsigned char *buf, int buflen )
+{
+    int ret, i, j, n;
+
+    for( n = 0; n < buflen; n++ )
+        if( buf[n] != 0 )
+            break;
+
+    MPI_CHK( mpi_grow( X, CHARS_TO_LIMBS( buflen - n ) ) );
+    MPI_CHK( mpi_lset( X, 0 ) );
+
+    for( i = buflen - 1, j = 0; i >= n; i--, j++ )
+        X->p[j / ciL] |= ((t_int) buf[i]) << ((j % ciL) << 3);
+
+cleanup:
+
+    return( ret );
+}
+
+/*
+ * Export X into unsigned binary data, big endian
+ */
+int mpi_write_binary( const mpi *X, unsigned char *buf, int buflen )
+{
+    int i, j, n;
+
+    n = mpi_size( X );
+
+    if( buflen < n )
+        return( POLARSSL_ERR_MPI_BUFFER_TOO_SMALL );
+
+    memset( buf, 0, buflen );
+
+    for( i = buflen - 1, j = 0; n > 0; i--, j++, n-- )
+        buf[i] = (unsigned char)( X->p[j / ciL] >> ((j % ciL) << 3) );
+
+    return( 0 );
+}
+
+/*
+ * Left-shift: X <<= count
+ */
+int mpi_shift_l( mpi *X, int count )
+{
+    int ret, i, v0, t1;
+    t_int r0 = 0, r1;
+
+    v0 = count / (biL    );
+    t1 = count & (biL - 1);
+
+    i = mpi_msb( X ) + count;
+
+    if( X->n * (int) biL < i )
+        MPI_CHK( mpi_grow( X, BITS_TO_LIMBS( i ) ) );
+
+    ret = 0;
+
+    /*
+     * shift by count / limb_size
+     */
+    if( v0 > 0 )
+    {
+        for( i = X->n - 1; i >= v0; i-- )
+            X->p[i] = X->p[i - v0];
+
+        for( ; i >= 0; i-- )
+            X->p[i] = 0;
+    }
+
+    /*
+     * shift by count % limb_size
+     */
+    if( t1 > 0 )
+    {
+        for( i = v0; i < X->n; i++ )
+        {
+            r1 = X->p[i] >> (biL - t1);
+            X->p[i] <<= t1;
+            X->p[i] |= r0;
+            r0 = r1;
+        }
+    }
+
+cleanup:
+
+    return( ret );
+}
+
+/*
+ * Right-shift: X >>= count
+ */
+int mpi_shift_r( mpi *X, int count )
+{
+    int i, v0, v1;
+    t_int r0 = 0, r1;
+
+    v0 = count /  biL;
+    v1 = count & (biL - 1);
+
+    /*
+     * shift by count / limb_size
+     */
+    if( v0 > 0 )
+    {
+        for( i = 0; i < X->n - v0; i++ )
+            X->p[i] = X->p[i + v0];
+
+        for( ; i < X->n; i++ )
+            X->p[i] = 0;
+    }
+
+    /*
+     * shift by count % limb_size
+     */
+    if( v1 > 0 )
+    {
+        for( i = X->n - 1; i >= 0; i-- )
+        {
+            r1 = X->p[i] << (biL - v1);
+            X->p[i] >>= v1;
+            X->p[i] |= r0;
+            r0 = r1;
+        }
+    }
+
+    return( 0 );
+}
+
+/*
+ * Compare unsigned values
+ */
+int mpi_cmp_abs( const mpi *X, const mpi *Y )
+{
+    int i, j;
+
+    for( i = X->n - 1; i >= 0; i-- )
+        if( X->p[i] != 0 )
+            break;
+
+    for( j = Y->n - 1; j >= 0; j-- )
+        if( Y->p[j] != 0 )
+            break;
+
+    if( i < 0 && j < 0 )
+        return( 0 );
+
+    if( i > j ) return(  1 );
+    if( j > i ) return( -1 );
+
+    for( ; i >= 0; i-- )
+    {
+        if( X->p[i] > Y->p[i] ) return(  1 );
+        if( X->p[i] < Y->p[i] ) return( -1 );
+    }
+
+    return( 0 );
+}
+
+/*
+ * Compare signed values
+ */
+int mpi_cmp_mpi( const mpi *X, const mpi *Y )
+{
+    int i, j;
+
+    for( i = X->n - 1; i >= 0; i-- )
+        if( X->p[i] != 0 )
+            break;
+
+    for( j = Y->n - 1; j >= 0; j-- )
+        if( Y->p[j] != 0 )
+            break;
+
+    if( i < 0 && j < 0 )
+        return( 0 );
+
+    if( i > j ) return(  X->s );
+    if( j > i ) return( -X->s );
+
+    if( X->s > 0 && Y->s < 0 ) return(  1 );
+    if( Y->s > 0 && X->s < 0 ) return( -1 );
+
+    for( ; i >= 0; i-- )
+    {
+        if( X->p[i] > Y->p[i] ) return(  X->s );
+        if( X->p[i] < Y->p[i] ) return( -X->s );
+    }
+
+    return( 0 );
+}
+
+/*
+ * Compare signed values
+ */
+int mpi_cmp_int( const mpi *X, int z )
+{
+    mpi Y;
+    t_int p[1];
+
+    *p  = ( z < 0 ) ? -z : z;
+    Y.s = ( z < 0 ) ? -1 : 1;
+    Y.n = 1;
+    Y.p = p;
+
+    return( mpi_cmp_mpi( X, &Y ) );
+}
+
+/*
+ * Unsigned addition: X = |A| + |B|  (HAC 14.7)
+ */
+int mpi_add_abs( mpi *X, const mpi *A, const mpi *B )
+{
+    int ret, i, j;
+    t_int *o, *p, c;
+
+    if( X == B )
+    {
+        const mpi *T = A; A = X; B = T;
+    }
+
+    if( X != A )
+        MPI_CHK( mpi_copy( X, A ) );
+
+    /*
+     * X should always be positive as a result of unsigned additions.
+     */
+    X->s = 1;
+
+    for( j = B->n - 1; j >= 0; j-- )
+        if( B->p[j] != 0 )
+            break;
+
+    MPI_CHK( mpi_grow( X, j + 1 ) );
+
+    o = B->p; p = X->p; c = 0;
+
+    for( i = 0; i <= j; i++, o++, p++ )
+    {
+        *p +=  c; c  = ( *p <  c );
+        *p += *o; c += ( *p < *o );
+    }
+
+    while( c != 0 )
+    {
+        if( i >= X->n )
+        {
+            MPI_CHK( mpi_grow( X, i + 1 ) );
+            p = X->p + i;
+        }
+
+        *p += c; c = ( *p < c ); i++;
+    }
+
+cleanup:
+
+    return( ret );
+}
+
+/*
+ * Helper for mpi substraction
+ */
+static void mpi_sub_hlp( int n, t_int *s, t_int *d )
+{
+    int i;
+    t_int c, z;
+
+    for( i = c = 0; i < n; i++, s++, d++ )
+    {
+        z = ( *d <  c );     *d -=  c;
+        c = ( *d < *s ) + z; *d -= *s;
+    }
+
+    while( c != 0 )
+    {
+        z = ( *d < c ); *d -= c;
+        c = z; i++; d++;
+    }
+}
+
+/*
+ * Unsigned substraction: X = |A| - |B|  (HAC 14.9)
+ */
+int mpi_sub_abs( mpi *X, const mpi *A, const mpi *B )
+{
+    mpi TB;
+    int ret, n;
+
+    if( mpi_cmp_abs( A, B ) < 0 )
+        return( POLARSSL_ERR_MPI_NEGATIVE_VALUE );
+
+    mpi_init( &TB, NULL );
+
+    if( X == B )
+    {
+        MPI_CHK( mpi_copy( &TB, B ) );
+        B = &TB;
+    }
+
+    if( X != A )
+        MPI_CHK( mpi_copy( X, A ) );
+
+    /*
+     * X should always be positive as a result of unsigned substractions.
+     */
+    X->s = 1;
+
+    ret = 0;
+
+    for( n = B->n - 1; n >= 0; n-- )
+        if( B->p[n] != 0 )
+            break;
+
+    mpi_sub_hlp( n + 1, B->p, X->p );
+
+cleanup:
+
+    mpi_free( &TB, NULL );
+
+    return( ret );
+}
+
+/*
+ * Signed addition: X = A + B
+ */
+int mpi_add_mpi( mpi *X, const mpi *A, const mpi *B )
+{
+    int ret, s = A->s;
+
+    if( A->s * B->s < 0 )
+    {
+        if( mpi_cmp_abs( A, B ) >= 0 )
+        {
+            MPI_CHK( mpi_sub_abs( X, A, B ) );
+            X->s =  s;
+        }
+        else
+        {
+            MPI_CHK( mpi_sub_abs( X, B, A ) );
+            X->s = -s;
+        }
+    }
+    else
+    {
+        MPI_CHK( mpi_add_abs( X, A, B ) );
+        X->s = s;
+    }
+
+cleanup:
+
+    return( ret );
+}
+
+/*
+ * Signed substraction: X = A - B
+ */
+int mpi_sub_mpi( mpi *X, const mpi *A, const mpi *B )
+{
+    int ret, s = A->s;
+
+    if( A->s * B->s > 0 )
+    {
+        if( mpi_cmp_abs( A, B ) >= 0 )
+        {
+            MPI_CHK( mpi_sub_abs( X, A, B ) );
+            X->s =  s;
+        }
+        else
+        {
+            MPI_CHK( mpi_sub_abs( X, B, A ) );
+            X->s = -s;
+        }
+    }
+    else
+    {
+        MPI_CHK( mpi_add_abs( X, A, B ) );
+        X->s = s;
+    }
+
+cleanup:
+
+    return( ret );
+}
+
+/*
+ * Signed addition: X = A + b
+ */
+int mpi_add_int( mpi *X, const mpi *A, int b )
+{
+    mpi _B;
+    t_int p[1];
+
+    p[0] = ( b < 0 ) ? -b : b;
+    _B.s = ( b < 0 ) ? -1 : 1;
+    _B.n = 1;
+    _B.p = p;
+
+    return( mpi_add_mpi( X, A, &_B ) );
+}
+
+/*
+ * Signed substraction: X = A - b
+ */
+int mpi_sub_int( mpi *X, const mpi *A, int b )
+{
+    mpi _B;
+    t_int p[1];
+
+    p[0] = ( b < 0 ) ? -b : b;
+    _B.s = ( b < 0 ) ? -1 : 1;
+    _B.n = 1;
+    _B.p = p;
+
+    return( mpi_sub_mpi( X, A, &_B ) );
+}
+
+/*
+ * Helper for mpi multiplication
+ */
+static void mpi_mul_hlp( int i, t_int *s, t_int *d, t_int b )
+{
+    t_int c = 0, t = 0;
+
+#if defined(MULADDC_HUIT)
+    for( ; i >= 8; i -= 8 )
+    {
+        MULADDC_INIT
+        MULADDC_HUIT
+        MULADDC_STOP
+    }
+
+    for( ; i > 0; i-- )
+    {
+        MULADDC_INIT
+        MULADDC_CORE
+        MULADDC_STOP
+    }
+#else
+    for( ; i >= 16; i -= 16 )
+    {
+        MULADDC_INIT
+        MULADDC_CORE   MULADDC_CORE
+        MULADDC_CORE   MULADDC_CORE
+        MULADDC_CORE   MULADDC_CORE
+        MULADDC_CORE   MULADDC_CORE
+
+        MULADDC_CORE   MULADDC_CORE
+        MULADDC_CORE   MULADDC_CORE
+        MULADDC_CORE   MULADDC_CORE
+        MULADDC_CORE   MULADDC_CORE
+        MULADDC_STOP
+    }
+
+    for( ; i >= 8; i -= 8 )
+    {
+        MULADDC_INIT
+        MULADDC_CORE   MULADDC_CORE
+        MULADDC_CORE   MULADDC_CORE
+
+        MULADDC_CORE   MULADDC_CORE
+        MULADDC_CORE   MULADDC_CORE
+        MULADDC_STOP
+    }
+
+    for( ; i > 0; i-- )
+    {
+        MULADDC_INIT
+        MULADDC_CORE
+        MULADDC_STOP
+    }
+#endif
+
+    t++;
+
+    do {
+        *d += c; c = ( *d < c ); d++;
+    }
+    while( c != 0 );
+}
+
+/*
+ * Baseline multiplication: X = A * B  (HAC 14.12)
+ */
+int mpi_mul_mpi( mpi *X, const mpi *A, const mpi *B )
+{
+    int ret, i, j;
+    mpi TA, TB;
+
+    mpi_init( &TA, &TB, NULL );
+
+    if( X == A ) { MPI_CHK( mpi_copy( &TA, A ) ); A = &TA; }
+    if( X == B ) { MPI_CHK( mpi_copy( &TB, B ) ); B = &TB; }
+
+    for( i = A->n - 1; i >= 0; i-- )
+        if( A->p[i] != 0 )
+            break;
+
+    for( j = B->n - 1; j >= 0; j-- )
+        if( B->p[j] != 0 )
+            break;
+
+    MPI_CHK( mpi_grow( X, i + j + 2 ) );
+    MPI_CHK( mpi_lset( X, 0 ) );
+
+    for( i++; j >= 0; j-- )
+        mpi_mul_hlp( i, A->p, X->p + j, B->p[j] );
+
+    X->s = A->s * B->s;
+
+cleanup:
+
+    mpi_free( &TB, &TA, NULL );
+
+    return( ret );
+}
+
+/*
+ * Baseline multiplication: X = A * b
+ */
+int mpi_mul_int( mpi *X, const mpi *A, t_int b )
+{
+    mpi _B;
+    t_int p[1];
+
+    _B.s = 1;
+    _B.n = 1;
+    _B.p = p;
+    p[0] = b;
+
+    return( mpi_mul_mpi( X, A, &_B ) );
+}
+
+/*
+ * Division by mpi: A = Q * B + R  (HAC 14.20)
+ */
+int mpi_div_mpi( mpi *Q, mpi *R, const mpi *A, const mpi *B )
+{
+    int ret, i, n, t, k;
+    mpi X, Y, Z, T1, T2;
+
+    if( mpi_cmp_int( B, 0 ) == 0 )
+        return( POLARSSL_ERR_MPI_DIVISION_BY_ZERO );
+
+    mpi_init( &X, &Y, &Z, &T1, &T2, NULL );
+
+    if( mpi_cmp_abs( A, B ) < 0 )
+    {
+        if( Q != NULL ) MPI_CHK( mpi_lset( Q, 0 ) );
+        if( R != NULL ) MPI_CHK( mpi_copy( R, A ) );
+        return( 0 );
+    }
+
+    MPI_CHK( mpi_copy( &X, A ) );
+    MPI_CHK( mpi_copy( &Y, B ) );
+    X.s = Y.s = 1;
+
+    MPI_CHK( mpi_grow( &Z, A->n + 2 ) );
+    MPI_CHK( mpi_lset( &Z,  0 ) );
+    MPI_CHK( mpi_grow( &T1, 2 ) );
+    MPI_CHK( mpi_grow( &T2, 3 ) );
+
+    k = mpi_msb( &Y ) % biL;
+    if( k < (int) biL - 1 )
+    {
+        k = biL - 1 - k;
+        MPI_CHK( mpi_shift_l( &X, k ) );
+        MPI_CHK( mpi_shift_l( &Y, k ) );
+    }
+    else k = 0;
+
+    n = X.n - 1;
+    t = Y.n - 1;
+    mpi_shift_l( &Y, biL * (n - t) );
+
+    while( mpi_cmp_mpi( &X, &Y ) >= 0 )
+    {
+        Z.p[n - t]++;
+        mpi_sub_mpi( &X, &X, &Y );
+    }
+    mpi_shift_r( &Y, biL * (n - t) );
+
+    for( i = n; i > t ; i-- )
+    {
+        if( X.p[i] >= Y.p[t] )
+            Z.p[i - t - 1] = ~0;
+        else
+        {
+#if defined(POLARSSL_HAVE_LONGLONG)
+            t_dbl r;
+
+            r  = (t_dbl) X.p[i] << biL;
+            r |= (t_dbl) X.p[i - 1];
+            r /= Y.p[t];
+            if( r > ((t_dbl) 1 << biL) - 1)
+                r = ((t_dbl) 1 << biL) - 1;
+
+            Z.p[i - t - 1] = (t_int) r;
+#else
+            /*
+             * __udiv_qrnnd_c, from gmp/longlong.h
+             */
+            t_int q0, q1, r0, r1;
+            t_int d0, d1, d, m;
+
+            d  = Y.p[t];
+            d0 = ( d << biH ) >> biH;
+            d1 = ( d >> biH );
+
+            q1 = X.p[i] / d1;
+            r1 = X.p[i] - d1 * q1;
+            r1 <<= biH;
+            r1 |= ( X.p[i - 1] >> biH );
+
+            m = q1 * d0;
+            if( r1 < m )
+            {
+                q1--, r1 += d;
+                while( r1 >= d && r1 < m )
+                    q1--, r1 += d;
+            }
+            r1 -= m;
+
+            q0 = r1 / d1;
+            r0 = r1 - d1 * q0;
+            r0 <<= biH;
+            r0 |= ( X.p[i - 1] << biH ) >> biH;
+
+            m = q0 * d0;
+            if( r0 < m )
+            {
+                q0--, r0 += d;
+                while( r0 >= d && r0 < m )
+                    q0--, r0 += d;
+            }
+            r0 -= m;
+
+            Z.p[i - t - 1] = ( q1 << biH ) | q0;
+#endif
+        }
+
+        Z.p[i - t - 1]++;
+        do
+        {
+            Z.p[i - t - 1]--;
+
+            MPI_CHK( mpi_lset( &T1, 0 ) );
+            T1.p[0] = (t < 1) ? 0 : Y.p[t - 1];
+            T1.p[1] = Y.p[t];
+            MPI_CHK( mpi_mul_int( &T1, &T1, Z.p[i - t - 1] ) );
+
+            MPI_CHK( mpi_lset( &T2, 0 ) );
+            T2.p[0] = (i < 2) ? 0 : X.p[i - 2];
+            T2.p[1] = (i < 1) ? 0 : X.p[i - 1];
+            T2.p[2] = X.p[i];
+        }
+        while( mpi_cmp_mpi( &T1, &T2 ) > 0 );
+
+        MPI_CHK( mpi_mul_int( &T1, &Y, Z.p[i - t - 1] ) );
+        MPI_CHK( mpi_shift_l( &T1,  biL * (i - t - 1) ) );
+        MPI_CHK( mpi_sub_mpi( &X, &X, &T1 ) );
+
+        if( mpi_cmp_int( &X, 0 ) < 0 )
+        {
+            MPI_CHK( mpi_copy( &T1, &Y ) );
+            MPI_CHK( mpi_shift_l( &T1, biL * (i - t - 1) ) );
+            MPI_CHK( mpi_add_mpi( &X, &X, &T1 ) );
+            Z.p[i - t - 1]--;
+        }
+    }
+
+    if( Q != NULL )
+    {
+        mpi_copy( Q, &Z );
+        Q->s = A->s * B->s;
+    }
+
+    if( R != NULL )
+    {
+        mpi_shift_r( &X, k );
+        mpi_copy( R, &X );
+
+        R->s = A->s;
+        if( mpi_cmp_int( R, 0 ) == 0 )
+            R->s = 1;
+    }
+
+cleanup:
+
+    mpi_free( &X, &Y, &Z, &T1, &T2, NULL );
+
+    return( ret );
+}
+
+/*
+ * Division by int: A = Q * b + R
+ *
+ * Returns 0 if successful
+ *         1 if memory allocation failed
+ *         POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0
+ */
+int mpi_div_int( mpi *Q, mpi *R, const mpi *A, int b )
+{
+    mpi _B;
+    t_int p[1];
+
+    p[0] = ( b < 0 ) ? -b : b;
+    _B.s = ( b < 0 ) ? -1 : 1;
+    _B.n = 1;
+    _B.p = p;
+
+    return( mpi_div_mpi( Q, R, A, &_B ) );
+}
+
+/*
+ * Modulo: R = A mod B
+ */
+int mpi_mod_mpi( mpi *R, const mpi *A, const mpi *B )
+{
+    int ret;
+
+    if( mpi_cmp_int( B, 0 ) < 0 )
+        return POLARSSL_ERR_MPI_NEGATIVE_VALUE;
+
+    MPI_CHK( mpi_div_mpi( NULL, R, A, B ) );
+
+    while( mpi_cmp_int( R, 0 ) < 0 )
+      MPI_CHK( mpi_add_mpi( R, R, B ) );
+
+    while( mpi_cmp_mpi( R, B ) >= 0 )
+      MPI_CHK( mpi_sub_mpi( R, R, B ) );
+
+cleanup:
+
+    return( ret );
+}
+
+/*
+ * Modulo: r = A mod b
+ */
+int mpi_mod_int( t_int *r, const mpi *A, int b )
+{
+    int i;
+    t_int x, y, z;
+
+    if( b == 0 )
+        return( POLARSSL_ERR_MPI_DIVISION_BY_ZERO );
+
+    if( b < 0 )
+        return POLARSSL_ERR_MPI_NEGATIVE_VALUE;
+
+    /*
+     * handle trivial cases
+     */
+    if( b == 1 )
+    {
+        *r = 0;
+        return( 0 );
+    }
+
+    if( b == 2 )
+    {
+        *r = A->p[0] & 1;
+        return( 0 );
+    }
+
+    /*
+     * general case
+     */
+    for( i = A->n - 1, y = 0; i >= 0; i-- )
+    {
+        x  = A->p[i];
+        y  = ( y << biH ) | ( x >> biH );
+        z  = y / b;
+        y -= z * b;
+
+        x <<= biH;
+        y  = ( y << biH ) | ( x >> biH );
+        z  = y / b;
+        y -= z * b;
+    }
+
+    /*
+     * If A is negative, then the current y represents a negative value.
+     * Flipping it to the positive side.
+     */
+    if( A->s < 0 && y != 0 )
+        y = b - y;
+
+    *r = y;
+
+    return( 0 );
+}
+
+/*
+ * Fast Montgomery initialization (thanks to Tom St Denis)
+ */
+static void mpi_montg_init( t_int *mm, const mpi *N )
+{
+    t_int x, m0 = N->p[0];
+
+    x  = m0;
+    x += ( ( m0 + 2 ) & 4 ) << 1;
+    x *= ( 2 - ( m0 * x ) );
+
+    if( biL >= 16 ) x *= ( 2 - ( m0 * x ) );
+    if( biL >= 32 ) x *= ( 2 - ( m0 * x ) );
+    if( biL >= 64 ) x *= ( 2 - ( m0 * x ) );
+
+    *mm = ~x + 1;
+}
+
+/*
+ * Montgomery multiplication: A = A * B * R^-1 mod N  (HAC 14.36)
+ */
+static void mpi_montmul( mpi *A, const mpi *B, const mpi *N, t_int mm, const mpi *T )
+{
+    int i, n, m;
+    t_int u0, u1, *d;
+
+    memset( T->p, 0, T->n * ciL );
+
+    d = T->p;
+    n = N->n;
+    m = ( B->n < n ) ? B->n : n;
+
+    for( i = 0; i < n; i++ )
+    {
+        /*
+         * T = (T + u0*B + u1*N) / 2^biL
+         */
+        u0 = A->p[i];
+        u1 = ( d[0] + u0 * B->p[0] ) * mm;
+
+        mpi_mul_hlp( m, B->p, d, u0 );
+        mpi_mul_hlp( n, N->p, d, u1 );
+
+        *d++ = u0; d[n + 1] = 0;
+    }
+
+    memcpy( A->p, d, (n + 1) * ciL );
+
+    if( mpi_cmp_abs( A, N ) >= 0 )
+        mpi_sub_hlp( n, N->p, A->p );
+    else
+        /* prevent timing attacks */
+        mpi_sub_hlp( n, A->p, T->p );
+}
+
+/*
+ * Montgomery reduction: A = A * R^-1 mod N
+ */
+static void mpi_montred( mpi *A, const mpi *N, t_int mm, const mpi *T )
+{
+    t_int z = 1;
+    mpi U;
+
+    U.n = U.s = z;
+    U.p = &z;
+
+    mpi_montmul( A, &U, N, mm, T );
+}
+
+/*
+ * Sliding-window exponentiation: X = A^E mod N  (HAC 14.85)
+ */
+int mpi_exp_mod( mpi *X, const mpi *A, const mpi *E, const mpi *N, mpi *_RR )
+{
+    int ret, i, j, wsize, wbits;
+    int bufsize, nblimbs, nbits;
+    t_int ei, mm, state;
+    mpi RR, T, W[64];
+
+    if( mpi_cmp_int( N, 0 ) < 0 || ( N->p[0] & 1 ) == 0 )
+        return( POLARSSL_ERR_MPI_BAD_INPUT_DATA );
+
+    /*
+     * Init temps and window size
+     */
+    mpi_montg_init( &mm, N );
+    mpi_init( &RR, &T, NULL );
+    memset( W, 0, sizeof( W ) );
+
+    i = mpi_msb( E );
+
+    wsize = ( i > 671 ) ? 6 : ( i > 239 ) ? 5 :
+            ( i >  79 ) ? 4 : ( i >  23 ) ? 3 : 1;
+
+    j = N->n + 1;
+    MPI_CHK( mpi_grow( X, j ) );
+    MPI_CHK( mpi_grow( &W[1],  j ) );
+    MPI_CHK( mpi_grow( &T, j * 2 ) );
+
+    /*
+     * If 1st call, pre-compute R^2 mod N
+     */
+    if( _RR == NULL || _RR->p == NULL )
+    {
+        MPI_CHK( mpi_lset( &RR, 1 ) );
+        MPI_CHK( mpi_shift_l( &RR, N->n * 2 * biL ) );
+        MPI_CHK( mpi_mod_mpi( &RR, &RR, N ) );
+
+        if( _RR != NULL )
+            memcpy( _RR, &RR, sizeof( mpi ) );
+    }
+    else
+        memcpy( &RR, _RR, sizeof( mpi ) );
+
+    /*
+     * W[1] = A * R^2 * R^-1 mod N = A * R mod N
+     */
+    if( mpi_cmp_mpi( A, N ) >= 0 )
+        mpi_mod_mpi( &W[1], A, N );
+    else   mpi_copy( &W[1], A );
+
+    mpi_montmul( &W[1], &RR, N, mm, &T );
+
+    /*
+     * X = R^2 * R^-1 mod N = R mod N
+     */
+    MPI_CHK( mpi_copy( X, &RR ) );
+    mpi_montred( X, N, mm, &T );
+
+    if( wsize > 1 )
+    {
+        /*
+         * W[1 << (wsize - 1)] = W[1] ^ (wsize - 1)
+         */
+        j =  1 << (wsize - 1);
+
+        MPI_CHK( mpi_grow( &W[j], N->n + 1 ) );
+        MPI_CHK( mpi_copy( &W[j], &W[1]    ) );
+
+        for( i = 0; i < wsize - 1; i++ )
+            mpi_montmul( &W[j], &W[j], N, mm, &T );
+
+        /*
+         * W[i] = W[i - 1] * W[1]
+         */
+        for( i = j + 1; i < (1 << wsize); i++ )
+        {
+            MPI_CHK( mpi_grow( &W[i], N->n + 1 ) );
+            MPI_CHK( mpi_copy( &W[i], &W[i - 1] ) );
+
+            mpi_montmul( &W[i], &W[1], N, mm, &T );
+        }
+    }
+
+    nblimbs = E->n;
+    bufsize = 0;
+    nbits   = 0;
+    wbits   = 0;
+    state   = 0;
+
+    while( 1 )
+    {
+        if( bufsize == 0 )
+        {
+            if( nblimbs-- == 0 )
+                break;
+
+            bufsize = sizeof( t_int ) << 3;
+        }
+
+        bufsize--;
+
+        ei = (E->p[nblimbs] >> bufsize) & 1;
+
+        /*
+         * skip leading 0s
+         */
+        if( ei == 0 && state == 0 )
+            continue;
+
+        if( ei == 0 && state == 1 )
+        {
+            /*
+             * out of window, square X
+             */
+            mpi_montmul( X, X, N, mm, &T );
+            continue;
+        }
+
+        /*
+         * add ei to current window
+         */
+        state = 2;
+
+        nbits++;
+        wbits |= (ei << (wsize - nbits));
+
+        if( nbits == wsize )
+        {
+            /*
+             * X = X^wsize R^-1 mod N
+             */
+            for( i = 0; i < wsize; i++ )
+                mpi_montmul( X, X, N, mm, &T );
+
+            /*
+             * X = X * W[wbits] R^-1 mod N
+             */
+            mpi_montmul( X, &W[wbits], N, mm, &T );
+
+            state--;
+            nbits = 0;
+            wbits = 0;
+        }
+    }
+
+    /*
+     * process the remaining bits
+     */
+    for( i = 0; i < nbits; i++ )
+    {
+        mpi_montmul( X, X, N, mm, &T );
+
+        wbits <<= 1;
+
+        if( (wbits & (1 << wsize)) != 0 )
+            mpi_montmul( X, &W[1], N, mm, &T );
+    }
+
+    /*
+     * X = A^E * R * R^-1 mod N = A^E mod N
+     */
+    mpi_montred( X, N, mm, &T );
+
+cleanup:
+
+    for( i = (1 << (wsize - 1)); i < (1 << wsize); i++ )
+        mpi_free( &W[i], NULL );
+
+    if( _RR != NULL )
+         mpi_free( &W[1], &T, NULL );
+    else mpi_free( &W[1], &T, &RR, NULL );
+
+    return( ret );
+}
+
+/*
+ * Greatest common divisor: G = gcd(A, B)  (HAC 14.54)
+ */
+int mpi_gcd( mpi *G, const mpi *A, const mpi *B )
+{
+    int ret, lz, lzt;
+    mpi TG, TA, TB;
+
+    mpi_init( &TG, &TA, &TB, NULL );
+
+    MPI_CHK( mpi_copy( &TA, A ) );
+    MPI_CHK( mpi_copy( &TB, B ) );
+
+    lz = mpi_lsb( &TA );
+    lzt = mpi_lsb( &TB );
+
+    if ( lzt < lz )
+        lz = lzt;
+
+    MPI_CHK( mpi_shift_r( &TA, lz ) );
+    MPI_CHK( mpi_shift_r( &TB, lz ) );
+
+    TA.s = TB.s = 1;
+
+    while( mpi_cmp_int( &TA, 0 ) != 0 )
+    {
+        MPI_CHK( mpi_shift_r( &TA, mpi_lsb( &TA ) ) );
+        MPI_CHK( mpi_shift_r( &TB, mpi_lsb( &TB ) ) );
+
+        if( mpi_cmp_mpi( &TA, &TB ) >= 0 )
+        {
+            MPI_CHK( mpi_sub_abs( &TA, &TA, &TB ) );
+            MPI_CHK( mpi_shift_r( &TA, 1 ) );
+        }
+        else
+        {
+            MPI_CHK( mpi_sub_abs( &TB, &TB, &TA ) );
+            MPI_CHK( mpi_shift_r( &TB, 1 ) );
+        }
+    }
+
+    MPI_CHK( mpi_shift_l( &TB, lz ) );
+    MPI_CHK( mpi_copy( G, &TB ) );
+
+cleanup:
+
+    mpi_free( &TB, &TA, &TG, NULL );
+
+    return( ret );
+}
+
+#if defined(POLARSSL_GENPRIME)
+
+/*
+ * Modular inverse: X = A^-1 mod N  (HAC 14.61 / 14.64)
+ */
+int mpi_inv_mod( mpi *X, const mpi *A, const mpi *N )
+{
+    int ret;
+    mpi G, TA, TU, U1, U2, TB, TV, V1, V2;
+
+    if( mpi_cmp_int( N, 0 ) <= 0 )
+        return( POLARSSL_ERR_MPI_BAD_INPUT_DATA );
+
+    mpi_init( &TA, &TU, &U1, &U2, &G,
+              &TB, &TV, &V1, &V2, NULL );
+
+    MPI_CHK( mpi_gcd( &G, A, N ) );
+
+    if( mpi_cmp_int( &G, 1 ) != 0 )
+    {
+        ret = POLARSSL_ERR_MPI_NOT_ACCEPTABLE;
+        goto cleanup;
+    }
+
+    MPI_CHK( mpi_mod_mpi( &TA, A, N ) );
+    MPI_CHK( mpi_copy( &TU, &TA ) );
+    MPI_CHK( mpi_copy( &TB, N ) );
+    MPI_CHK( mpi_copy( &TV, N ) );
+
+    MPI_CHK( mpi_lset( &U1, 1 ) );
+    MPI_CHK( mpi_lset( &U2, 0 ) );
+    MPI_CHK( mpi_lset( &V1, 0 ) );
+    MPI_CHK( mpi_lset( &V2, 1 ) );
+
+    do
+    {
+        while( ( TU.p[0] & 1 ) == 0 )
+        {
+            MPI_CHK( mpi_shift_r( &TU, 1 ) );
+
+            if( ( U1.p[0] & 1 ) != 0 || ( U2.p[0] & 1 ) != 0 )
+            {
+                MPI_CHK( mpi_add_mpi( &U1, &U1, &TB ) );
+                MPI_CHK( mpi_sub_mpi( &U2, &U2, &TA ) );
+            }
+
+            MPI_CHK( mpi_shift_r( &U1, 1 ) );
+            MPI_CHK( mpi_shift_r( &U2, 1 ) );
+        }
+
+        while( ( TV.p[0] & 1 ) == 0 )
+        {
+            MPI_CHK( mpi_shift_r( &TV, 1 ) );
+
+            if( ( V1.p[0] & 1 ) != 0 || ( V2.p[0] & 1 ) != 0 )
+            {
+                MPI_CHK( mpi_add_mpi( &V1, &V1, &TB ) );
+                MPI_CHK( mpi_sub_mpi( &V2, &V2, &TA ) );
+            }
+
+            MPI_CHK( mpi_shift_r( &V1, 1 ) );
+            MPI_CHK( mpi_shift_r( &V2, 1 ) );
+        }
+
+        if( mpi_cmp_mpi( &TU, &TV ) >= 0 )
+        {
+            MPI_CHK( mpi_sub_mpi( &TU, &TU, &TV ) );
+            MPI_CHK( mpi_sub_mpi( &U1, &U1, &V1 ) );
+            MPI_CHK( mpi_sub_mpi( &U2, &U2, &V2 ) );
+        }
+        else
+        {
+            MPI_CHK( mpi_sub_mpi( &TV, &TV, &TU ) );
+            MPI_CHK( mpi_sub_mpi( &V1, &V1, &U1 ) );
+            MPI_CHK( mpi_sub_mpi( &V2, &V2, &U2 ) );
+        }
+    }
+    while( mpi_cmp_int( &TU, 0 ) != 0 );
+
+    while( mpi_cmp_int( &V1, 0 ) < 0 )
+        MPI_CHK( mpi_add_mpi( &V1, &V1, N ) );
+
+    while( mpi_cmp_mpi( &V1, N ) >= 0 )
+        MPI_CHK( mpi_sub_mpi( &V1, &V1, N ) );
+
+    MPI_CHK( mpi_copy( X, &V1 ) );
+
+cleanup:
+
+    mpi_free( &V2, &V1, &TV, &TB, &G,
+              &U2, &U1, &TU, &TA, NULL );
+
+    return( ret );
+}
+
+static const int small_prime[] =
+{
+        3,    5,    7,   11,   13,   17,   19,   23,
+       29,   31,   37,   41,   43,   47,   53,   59,
+       61,   67,   71,   73,   79,   83,   89,   97,
+      101,  103,  107,  109,  113,  127,  131,  137,
+      139,  149,  151,  157,  163,  167,  173,  179,
+      181,  191,  193,  197,  199,  211,  223,  227,
+      229,  233,  239,  241,  251,  257,  263,  269,
+      271,  277,  281,  283,  293,  307,  311,  313,
+      317,  331,  337,  347,  349,  353,  359,  367,
+      373,  379,  383,  389,  397,  401,  409,  419,
+      421,  431,  433,  439,  443,  449,  457,  461,
+      463,  467,  479,  487,  491,  499,  503,  509,
+      521,  523,  541,  547,  557,  563,  569,  571,
+      577,  587,  593,  599,  601,  607,  613,  617,
+      619,  631,  641,  643,  647,  653,  659,  661,
+      673,  677,  683,  691,  701,  709,  719,  727,
+      733,  739,  743,  751,  757,  761,  769,  773,
+      787,  797,  809,  811,  821,  823,  827,  829,
+      839,  853,  857,  859,  863,  877,  881,  883,
+      887,  907,  911,  919,  929,  937,  941,  947,
+      953,  967,  971,  977,  983,  991,  997, -103
+};
+
+/*
+ * Miller-Rabin primality test  (HAC 4.24)
+ */
+int mpi_is_prime( mpi *X, int (*f_rng)(void *), void *p_rng )
+{
+    int ret, i, j, n, s, xs;
+    mpi W, R, T, A, RR;
+    unsigned char *p;
+
+    if( mpi_cmp_int( X, 0 ) == 0 ||
+        mpi_cmp_int( X, 1 ) == 0 )
+        return( POLARSSL_ERR_MPI_NOT_ACCEPTABLE );
+
+    if( mpi_cmp_int( X, 2 ) == 0 )
+        return( 0 );
+
+    mpi_init( &W, &R, &T, &A, &RR, NULL );
+
+    xs = X->s; X->s = 1;
+
+    /*
+     * test trivial factors first
+     */
+    if( ( X->p[0] & 1 ) == 0 )
+        return( POLARSSL_ERR_MPI_NOT_ACCEPTABLE );
+
+    for( i = 0; small_prime[i] > 0; i++ )
+    {
+        t_int r;
+
+        if( mpi_cmp_int( X, small_prime[i] ) <= 0 )
+            return( 0 );
+
+        MPI_CHK( mpi_mod_int( &r, X, small_prime[i] ) );
+
+        if( r == 0 )
+            return( POLARSSL_ERR_MPI_NOT_ACCEPTABLE );
+    }
+
+    /*
+     * W = |X| - 1
+     * R = W >> lsb( W )
+     */
+    MPI_CHK( mpi_sub_int( &W, X, 1 ) );
+    s = mpi_lsb( &W );
+    MPI_CHK( mpi_copy( &R, &W ) );
+    MPI_CHK( mpi_shift_r( &R, s ) );
+
+    i = mpi_msb( X );
+    /*
+     * HAC, table 4.4
+     */
+    n = ( ( i >= 1300 ) ?  2 : ( i >=  850 ) ?  3 :
+          ( i >=  650 ) ?  4 : ( i >=  350 ) ?  8 :
+          ( i >=  250 ) ? 12 : ( i >=  150 ) ? 18 : 27 );
+
+    for( i = 0; i < n; i++ )
+    {
+        /*
+         * pick a random A, 1 < A < |X| - 1
+         */
+        MPI_CHK( mpi_grow( &A, X->n ) );
+
+        p = (unsigned char *) A.p;
+        for( j = 0; j < A.n * ciL; j++ )
+            *p++ = (unsigned char) f_rng( p_rng );
+
+        j = mpi_msb( &A ) - mpi_msb( &W );
+        MPI_CHK( mpi_shift_r( &A, j + 1 ) );
+        A.p[0] |= 3;
+
+        /*
+         * A = A^R mod |X|
+         */
+        MPI_CHK( mpi_exp_mod( &A, &A, &R, X, &RR ) );
+
+        if( mpi_cmp_mpi( &A, &W ) == 0 ||
+            mpi_cmp_int( &A,  1 ) == 0 )
+            continue;
+
+        j = 1;
+        while( j < s && mpi_cmp_mpi( &A, &W ) != 0 )
+        {
+            /*
+             * A = A * A mod |X|
+             */
+            MPI_CHK( mpi_mul_mpi( &T, &A, &A ) );
+            MPI_CHK( mpi_mod_mpi( &A, &T, X  ) );
+
+            if( mpi_cmp_int( &A, 1 ) == 0 )
+                break;
+
+            j++;
+        }
+
+        /*
+         * not prime if A != |X| - 1 or A == 1
+         */
+        if( mpi_cmp_mpi( &A, &W ) != 0 ||
+            mpi_cmp_int( &A,  1 ) == 0 )
+        {
+            ret = POLARSSL_ERR_MPI_NOT_ACCEPTABLE;
+            break;
+        }
+    }
+
+cleanup:
+
+    X->s = xs;
+
+    mpi_free( &RR, &A, &T, &R, &W, NULL );
+
+    return( ret );
+}
+
+/*
+ * Prime number generation
+ */
+int mpi_gen_prime( mpi *X, int nbits, int dh_flag,
+                   int (*f_rng)(void *), void *p_rng )
+{
+    int ret, k, n;
+    unsigned char *p;
+    mpi Y;
+
+    if( nbits < 3 )
+        return( POLARSSL_ERR_MPI_BAD_INPUT_DATA );
+
+    mpi_init( &Y, NULL );
+
+    n = BITS_TO_LIMBS( nbits );
+
+    MPI_CHK( mpi_grow( X, n ) );
+    MPI_CHK( mpi_lset( X, 0 ) );
+
+    p = (unsigned char *) X->p;
+    for( k = 0; k < X->n * ciL; k++ )
+        *p++ = (unsigned char) f_rng( p_rng );
+
+    k = mpi_msb( X );
+    if( k < nbits ) MPI_CHK( mpi_shift_l( X, nbits - k ) );
+    if( k > nbits ) MPI_CHK( mpi_shift_r( X, k - nbits ) );
+
+    X->p[0] |= 3;
+
+    if( dh_flag == 0 )
+    {
+        while( ( ret = mpi_is_prime( X, f_rng, p_rng ) ) != 0 )
+        {
+            if( ret != POLARSSL_ERR_MPI_NOT_ACCEPTABLE )
+                goto cleanup;
+
+            MPI_CHK( mpi_add_int( X, X, 2 ) );
+        }
+    }
+    else
+    {
+        MPI_CHK( mpi_sub_int( &Y, X, 1 ) );
+        MPI_CHK( mpi_shift_r( &Y, 1 ) );
+
+        while( 1 )
+        {
+            if( ( ret = mpi_is_prime( X, f_rng, p_rng ) ) == 0 )
+            {
+                if( ( ret = mpi_is_prime( &Y, f_rng, p_rng ) ) == 0 )
+                    break;
+
+                if( ret != POLARSSL_ERR_MPI_NOT_ACCEPTABLE )
+                    goto cleanup;
+            }
+
+            if( ret != POLARSSL_ERR_MPI_NOT_ACCEPTABLE )
+                goto cleanup;
+
+            MPI_CHK( mpi_add_int( &Y, X, 1 ) );
+            MPI_CHK( mpi_add_int(  X, X, 2 ) );
+            MPI_CHK( mpi_shift_r( &Y, 1 ) );
+        }
+    }
+
+cleanup:
+
+    mpi_free( &Y, NULL );
+
+    return( ret );
+}
+
+#endif
diff --git a/src/pdkim/bignum.h b/src/pdkim/bignum.h
new file mode 100644 (file)
index 0000000..581f7f2
--- /dev/null
@@ -0,0 +1,527 @@
+/**
+ * \file bignum.h
+ *
+ *  Copyright (C) 2006-2010, Brainspark B.V.
+ *
+ *  This file is part of PolarSSL (http://www.polarssl.org)
+ *  Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
+ *
+ *  All rights reserved.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License along
+ *  with this program; if not, write to the Free Software Foundation, Inc.,
+ *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef POLARSSL_BIGNUM_H
+#define POLARSSL_BIGNUM_H
+
+#include <stdio.h>
+
+#define POLARSSL_ERR_MPI_FILE_IO_ERROR                     0x0002
+#define POLARSSL_ERR_MPI_BAD_INPUT_DATA                    0x0004
+#define POLARSSL_ERR_MPI_INVALID_CHARACTER                 0x0006
+#define POLARSSL_ERR_MPI_BUFFER_TOO_SMALL                  0x0008
+#define POLARSSL_ERR_MPI_NEGATIVE_VALUE                    0x000A
+#define POLARSSL_ERR_MPI_DIVISION_BY_ZERO                  0x000C
+#define POLARSSL_ERR_MPI_NOT_ACCEPTABLE                    0x000E
+
+#define MPI_CHK(f) if( ( ret = f ) != 0 ) goto cleanup
+
+/*
+ * Define the base integer type, architecture-wise
+ */
+#if defined(POLARSSL_HAVE_INT8)
+typedef unsigned char  t_int;
+typedef unsigned short t_dbl;
+#else
+#if defined(POLARSSL_HAVE_INT16)
+typedef unsigned short t_int;
+typedef unsigned long  t_dbl;
+#else
+  typedef unsigned long t_int;
+  #if defined(_MSC_VER) && defined(_M_IX86)
+  typedef unsigned __int64 t_dbl;
+  #else
+    #if defined(__amd64__) || defined(__x86_64__)    || \
+        defined(__ppc64__) || defined(__powerpc64__) || \
+        defined(__ia64__)  || defined(__alpha__)
+    typedef unsigned int t_dbl __attribute__((mode(TI)));
+    #else
+      #if defined(POLARSSL_HAVE_LONGLONG)
+      typedef unsigned long long t_dbl;
+      #endif
+    #endif
+  #endif
+#endif
+#endif
+
+/**
+ * \brief          MPI structure
+ */
+typedef struct
+{
+    int s;              /*!<  integer sign      */
+    int n;              /*!<  total # of limbs  */
+    t_int *p;           /*!<  pointer to limbs  */
+}
+mpi;
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * \brief          Initialize one or more mpi
+ */
+void mpi_init( mpi *X, ... );
+
+/**
+ * \brief          Unallocate one or more mpi
+ */
+void mpi_free( mpi *X, ... );
+
+/**
+ * \brief          Enlarge to the specified number of limbs
+ *
+ * \param X        MPI to grow
+ * \param nblimbs  The target number of limbs
+ *
+ * \return         0 if successful,
+ *                 1 if memory allocation failed
+ */
+int mpi_grow( mpi *X, int nblimbs );
+
+/**
+ * \brief          Copy the contents of Y into X
+ *
+ * \param X        Destination MPI
+ * \param Y        Source MPI
+ *
+ * \return         0 if successful,
+ *                 1 if memory allocation failed
+ */
+int mpi_copy( mpi *X, const mpi *Y );
+
+/**
+ * \brief          Swap the contents of X and Y
+ *
+ * \param X        First MPI value
+ * \param Y        Second MPI value
+ */
+void mpi_swap( mpi *X, mpi *Y );
+
+/**
+ * \brief          Set value from integer
+ *
+ * \param X        MPI to set
+ * \param z        Value to use
+ *
+ * \return         0 if successful,
+ *                 1 if memory allocation failed
+ */
+int mpi_lset( mpi *X, int z );
+
+/**
+ * \brief          Return the number of least significant bits
+ *
+ * \param X        MPI to use
+ */
+int mpi_lsb( const mpi *X );
+
+/**
+ * \brief          Return the number of most significant bits
+ *
+ * \param X        MPI to use
+ */
+int mpi_msb( const mpi *X );
+
+/**
+ * \brief          Return the total size in bytes
+ *
+ * \param X        MPI to use
+ */
+int mpi_size( const mpi *X );
+
+/**
+ * \brief          Import from an ASCII string
+ *
+ * \param X        Destination MPI
+ * \param radix    Input numeric base
+ * \param s        Null-terminated string buffer
+ *
+ * \return         0 if successful, or an POLARSSL_ERR_MPI_XXX error code
+ */
+int mpi_read_string( mpi *X, int radix, const char *s );
+
+/**
+ * \brief          Export into an ASCII string
+ *
+ * \param X        Source MPI
+ * \param radix    Output numeric base
+ * \param s        String buffer
+ * \param slen     String buffer size
+ *
+ * \return         0 if successful, or an POLARSSL_ERR_MPI_XXX error code.
+ *                 *slen is always updated to reflect the amount
+ *                 of data that has (or would have) been written.
+ *
+ * \note           Call this function with *slen = 0 to obtain the
+ *                 minimum required buffer size in *slen.
+ */
+int mpi_write_string( const mpi *X, int radix, char *s, int *slen );
+
+/**
+ * \brief          Read X from an opened file
+ *
+ * \param X        Destination MPI
+ * \param radix    Input numeric base
+ * \param fin      Input file handle
+ *
+ * \return         0 if successful, or an POLARSSL_ERR_MPI_XXX error code
+ */
+int mpi_read_file( mpi *X, int radix, FILE *fin );
+
+/**
+ * \brief          Write X into an opened file, or stdout if fout is NULL
+ *
+ * \param p        Prefix, can be NULL
+ * \param X        Source MPI
+ * \param radix    Output numeric base
+ * \param fout     Output file handle (can be NULL)
+ *
+ * \return         0 if successful, or an POLARSSL_ERR_MPI_XXX error code
+ *
+ * \note           Set fout == NULL to print X on the console.
+ */
+int mpi_write_file( const char *p, const mpi *X, int radix, FILE *fout );
+
+/**
+ * \brief          Import X from unsigned binary data, big endian
+ *
+ * \param X        Destination MPI
+ * \param buf      Input buffer
+ * \param buflen   Input buffer size
+ *
+ * \return         0 if successful,
+ *                 1 if memory allocation failed
+ */
+int mpi_read_binary( mpi *X, const unsigned char *buf, int buflen );
+
+/**
+ * \brief          Export X into unsigned binary data, big endian
+ *
+ * \param X        Source MPI
+ * \param buf      Output buffer
+ * \param buflen   Output buffer size
+ *
+ * \return         0 if successful,
+ *                 POLARSSL_ERR_MPI_BUFFER_TOO_SMALL if buf isn't large enough
+ */
+int mpi_write_binary( const mpi *X, unsigned char *buf, int buflen );
+
+/**
+ * \brief          Left-shift: X <<= count
+ *
+ * \param X        MPI to shift
+ * \param count    Amount to shift
+ *
+ * \return         0 if successful,
+ *                 1 if memory allocation failed
+ */
+int mpi_shift_l( mpi *X, int count );
+
+/**
+ * \brief          Right-shift: X >>= count
+ *
+ * \param X        MPI to shift
+ * \param count    Amount to shift
+ *
+ * \return         0 if successful,
+ *                 1 if memory allocation failed
+ */
+int mpi_shift_r( mpi *X, int count );
+
+/**
+ * \brief          Compare unsigned values
+ *
+ * \param X        Left-hand MPI
+ * \param Y        Right-hand MPI
+ *
+ * \return         1 if |X| is greater than |Y|,
+ *                -1 if |X| is lesser  than |Y| or
+ *                 0 if |X| is equal to |Y|
+ */
+int mpi_cmp_abs( const mpi *X, const mpi *Y );
+
+/**
+ * \brief          Compare signed values
+ *
+ * \param X        Left-hand MPI
+ * \param Y        Right-hand MPI
+ *
+ * \return         1 if X is greater than Y,
+ *                -1 if X is lesser  than Y or
+ *                 0 if X is equal to Y
+ */
+int mpi_cmp_mpi( const mpi *X, const mpi *Y );
+
+/**
+ * \brief          Compare signed values
+ *
+ * \param X        Left-hand MPI
+ * \param z        The integer value to compare to
+ *
+ * \return         1 if X is greater than z,
+ *                -1 if X is lesser  than z or
+ *                 0 if X is equal to z
+ */
+int mpi_cmp_int( const mpi *X, int z );
+
+/**
+ * \brief          Unsigned addition: X = |A| + |B|
+ *
+ * \param X        Destination MPI
+ * \param A        Left-hand MPI
+ * \param B        Right-hand MPI
+ *
+ * \return         0 if successful,
+ *                 1 if memory allocation failed
+ */
+int mpi_add_abs( mpi *X, const mpi *A, const mpi *B );
+
+/**
+ * \brief          Unsigned substraction: X = |A| - |B|
+ *
+ * \param X        Destination MPI
+ * \param A        Left-hand MPI
+ * \param B        Right-hand MPI
+ *
+ * \return         0 if successful,
+ *                 POLARSSL_ERR_MPI_NEGATIVE_VALUE if B is greater than A
+ */
+int mpi_sub_abs( mpi *X, const mpi *A, const mpi *B );
+
+/**
+ * \brief          Signed addition: X = A + B
+ *
+ * \param X        Destination MPI
+ * \param A        Left-hand MPI
+ * \param B        Right-hand MPI
+ *
+ * \return         0 if successful,
+ *                 1 if memory allocation failed
+ */
+int mpi_add_mpi( mpi *X, const mpi *A, const mpi *B );
+
+/**
+ * \brief          Signed substraction: X = A - B
+ *
+ * \param X        Destination MPI
+ * \param A        Left-hand MPI
+ * \param B        Right-hand MPI
+ *
+ * \return         0 if successful,
+ *                 1 if memory allocation failed
+ */
+int mpi_sub_mpi( mpi *X, const mpi *A, const mpi *B );
+
+/**
+ * \brief          Signed addition: X = A + b
+ *
+ * \param X        Destination MPI
+ * \param A        Left-hand MPI
+ * \param b        The integer value to add
+ *
+ * \return         0 if successful,
+ *                 1 if memory allocation failed
+ */
+int mpi_add_int( mpi *X, const mpi *A, int b );
+
+/**
+ * \brief          Signed substraction: X = A - b
+ *
+ * \param X        Destination MPI
+ * \param A        Left-hand MPI
+ * \param b        The integer value to subtract
+ *
+ * \return         0 if successful,
+ *                 1 if memory allocation failed
+ */
+int mpi_sub_int( mpi *X, const mpi *A, int b );
+
+/**
+ * \brief          Baseline multiplication: X = A * B
+ *
+ * \param X        Destination MPI
+ * \param A        Left-hand MPI
+ * \param B        Right-hand MPI
+ *
+ * \return         0 if successful,
+ *                 1 if memory allocation failed
+ */
+int mpi_mul_mpi( mpi *X, const mpi *A, const mpi *B );
+
+/**
+ * \brief          Baseline multiplication: X = A * b
+ *                 Note: b is an unsigned integer type, thus
+ *                 Negative values of b are ignored.
+ *
+ * \param X        Destination MPI
+ * \param A        Left-hand MPI
+ * \param b        The integer value to multiply with
+ *
+ * \return         0 if successful,
+ *                 1 if memory allocation failed
+ */
+int mpi_mul_int( mpi *X, const mpi *A, t_int b );
+
+/**
+ * \brief          Division by mpi: A = Q * B + R
+ *
+ * \param Q        Destination MPI for the quotient
+ * \param R        Destination MPI for the rest value
+ * \param A        Left-hand MPI
+ * \param B        Right-hand MPI
+ *
+ * \return         0 if successful,
+ *                 1 if memory allocation failed,
+ *                 POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0
+ *
+ * \note           Either Q or R can be NULL.
+ */
+int mpi_div_mpi( mpi *Q, mpi *R, const mpi *A, const mpi *B );
+
+/**
+ * \brief          Division by int: A = Q * b + R
+ *
+ * \param Q        Destination MPI for the quotient
+ * \param R        Destination MPI for the rest value
+ * \param A        Left-hand MPI
+ * \param b        Integer to divide by
+ *
+ * \return         0 if successful,
+ *                 1 if memory allocation failed,
+ *                 POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0
+ *
+ * \note           Either Q or R can be NULL.
+ */
+int mpi_div_int( mpi *Q, mpi *R, const mpi *A, int b );
+
+/**
+ * \brief          Modulo: R = A mod B
+ *
+ * \param R        Destination MPI for the rest value
+ * \param A        Left-hand MPI
+ * \param B        Right-hand MPI
+ *
+ * \return         0 if successful,
+ *                 1 if memory allocation failed,
+ *                 POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0,
+ *                 POLARSSL_ERR_MPI_NEGATIVE_VALUE if B < 0
+ */
+int mpi_mod_mpi( mpi *R, const mpi *A, const mpi *B );
+
+/**
+ * \brief          Modulo: r = A mod b
+ *
+ * \param r        Destination t_int
+ * \param A        Left-hand MPI
+ * \param b        Integer to divide by
+ *
+ * \return         0 if successful,
+ *                 1 if memory allocation failed,
+ *                 POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0,
+ *                 POLARSSL_ERR_MPI_NEGATIVE_VALUE if b < 0
+ */
+int mpi_mod_int( t_int *r, const mpi *A, int b );
+
+/**
+ * \brief          Sliding-window exponentiation: X = A^E mod N
+ *
+ * \param X        Destination MPI
+ * \param A        Left-hand MPI
+ * \param E        Exponent MPI
+ * \param N        Modular MPI
+ * \param _RR      Speed-up MPI used for recalculations
+ *
+ * \return         0 if successful,
+ *                 1 if memory allocation failed,
+ *                 POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or even
+ *
+ * \note           _RR is used to avoid re-computing R*R mod N across
+ *                 multiple calls, which speeds up things a bit. It can
+ *                 be set to NULL if the extra performance is unneeded.
+ */
+int mpi_exp_mod( mpi *X, const mpi *A, const mpi *E, const mpi *N, mpi *_RR );
+
+/**
+ * \brief          Greatest common divisor: G = gcd(A, B)
+ *
+ * \param G        Destination MPI
+ * \param A        Left-hand MPI
+ * \param B        Right-hand MPI
+ *
+ * \return         0 if successful,
+ *                 1 if memory allocation failed
+ */
+int mpi_gcd( mpi *G, const mpi *A, const mpi *B );
+
+/**
+ * \brief          Modular inverse: X = A^-1 mod N
+ *
+ * \param X        Destination MPI
+ * \param A        Left-hand MPI
+ * \param N        Right-hand MPI
+ *
+ * \return         0 if successful,
+ *                 1 if memory allocation failed,
+ *                 POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or nil
+                   POLARSSL_ERR_MPI_NOT_ACCEPTABLE if A has no inverse mod N
+ */
+int mpi_inv_mod( mpi *X, const mpi *A, const mpi *N );
+
+/**
+ * \brief          Miller-Rabin primality test
+ *
+ * \param X        MPI to check
+ * \param f_rng    RNG function
+ * \param p_rng    RNG parameter
+ *
+ * \return         0 if successful (probably prime),
+ *                 1 if memory allocation failed,
+ *                 POLARSSL_ERR_MPI_NOT_ACCEPTABLE if X is not prime
+ */
+int mpi_is_prime( mpi *X, int (*f_rng)(void *), void *p_rng );
+
+/**
+ * \brief          Prime number generation
+ *
+ * \param X        Destination MPI
+ * \param nbits    Required size of X in bits
+ * \param dh_flag  If 1, then (X-1)/2 will be prime too
+ * \param f_rng    RNG function
+ * \param p_rng    RNG parameter
+ *
+ * \return         0 if successful (probably prime),
+ *                 1 if memory allocation failed,
+ *                 POLARSSL_ERR_MPI_BAD_INPUT_DATA if nbits is < 3
+ */
+int mpi_gen_prime( mpi *X, int nbits, int dh_flag,
+                   int (*f_rng)(void *), void *p_rng );
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* bignum.h */
diff --git a/src/pdkim/bn_mul.h b/src/pdkim/bn_mul.h
new file mode 100644 (file)
index 0000000..b3c09f0
--- /dev/null
@@ -0,0 +1,735 @@
+/**
+ * \file bn_mul.h
+ *
+ *  Copyright (C) 2006-2010, Brainspark B.V.
+ *
+ *  This file is part of PolarSSL (http://www.polarssl.org)
+ *  Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
+ *
+ *  All rights reserved.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License along
+ *  with this program; if not, write to the Free Software Foundation, Inc.,
+ *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+/*
+ *      Multiply source vector [s] with b, add result
+ *       to destination vector [d] and set carry c.
+ *
+ *      Currently supports:
+ *
+ *         . IA-32 (386+)         . AMD64 / EM64T
+ *         . IA-32 (SSE2)         . Motorola 68000
+ *         . PowerPC, 32-bit      . MicroBlaze
+ *         . PowerPC, 64-bit      . TriCore
+ *         . SPARC v8             . ARM v3+
+ *         . Alpha                . MIPS32
+ *         . C, longlong          . C, generic
+ */
+
+#ifndef POLARSSL_BN_MUL_H
+#define POLARSSL_BN_MUL_H
+
+#if defined(POLARSSL_HAVE_ASM)
+
+#if defined(__GNUC__)
+#if defined(__i386__)
+
+#define MULADDC_INIT                \
+    asm( "                          \
+        movl   %%ebx, %0;           \
+        movl   %5, %%esi;           \
+        movl   %6, %%edi;           \
+        movl   %7, %%ecx;           \
+        movl   %8, %%ebx;           \
+        "
+
+#define MULADDC_CORE                \
+        "                           \
+        lodsl;                      \
+        mull   %%ebx;               \
+        addl   %%ecx,   %%eax;      \
+        adcl   $0,      %%edx;      \
+        addl   (%%edi), %%eax;      \
+        adcl   $0,      %%edx;      \
+        movl   %%edx,   %%ecx;      \
+        stosl;                      \
+        "
+
+#if defined(POLARSSL_HAVE_SSE2)
+
+#define MULADDC_HUIT                    \
+        "                               \
+        movd     %%ecx,     %%mm1;      \
+        movd     %%ebx,     %%mm0;      \
+        movd     (%%edi),   %%mm3;      \
+        paddq    %%mm3,     %%mm1;      \
+        movd     (%%esi),   %%mm2;      \
+        pmuludq  %%mm0,     %%mm2;      \
+        movd     4(%%esi),  %%mm4;      \
+        pmuludq  %%mm0,     %%mm4;      \
+        movd     8(%%esi),  %%mm6;      \
+        pmuludq  %%mm0,     %%mm6;      \
+        movd     12(%%esi), %%mm7;      \
+        pmuludq  %%mm0,     %%mm7;      \
+        paddq    %%mm2,     %%mm1;      \
+        movd     4(%%edi),  %%mm3;      \
+        paddq    %%mm4,     %%mm3;      \
+        movd     8(%%edi),  %%mm5;      \
+        paddq    %%mm6,     %%mm5;      \
+        movd     12(%%edi), %%mm4;      \
+        paddq    %%mm4,     %%mm7;      \
+        movd     %%mm1,     (%%edi);    \
+        movd     16(%%esi), %%mm2;      \
+        pmuludq  %%mm0,     %%mm2;      \
+        psrlq    $32,       %%mm1;      \
+        movd     20(%%esi), %%mm4;      \
+        pmuludq  %%mm0,     %%mm4;      \
+        paddq    %%mm3,     %%mm1;      \
+        movd     24(%%esi), %%mm6;      \
+        pmuludq  %%mm0,     %%mm6;      \
+        movd     %%mm1,     4(%%edi);   \
+        psrlq    $32,       %%mm1;      \
+        movd     28(%%esi), %%mm3;      \
+        pmuludq  %%mm0,     %%mm3;      \
+        paddq    %%mm5,     %%mm1;      \
+        movd     16(%%edi), %%mm5;      \
+        paddq    %%mm5,     %%mm2;      \
+        movd     %%mm1,     8(%%edi);   \
+        psrlq    $32,       %%mm1;      \
+        paddq    %%mm7,     %%mm1;      \
+        movd     20(%%edi), %%mm5;      \
+        paddq    %%mm5,     %%mm4;      \
+        movd     %%mm1,     12(%%edi);  \
+        psrlq    $32,       %%mm1;      \
+        paddq    %%mm2,     %%mm1;      \
+        movd     24(%%edi), %%mm5;      \
+        paddq    %%mm5,     %%mm6;      \
+        movd     %%mm1,     16(%%edi);  \
+        psrlq    $32,       %%mm1;      \
+        paddq    %%mm4,     %%mm1;      \
+        movd     28(%%edi), %%mm5;      \
+        paddq    %%mm5,     %%mm3;      \
+        movd     %%mm1,     20(%%edi);  \
+        psrlq    $32,       %%mm1;      \
+        paddq    %%mm6,     %%mm1;      \
+        movd     %%mm1,     24(%%edi);  \
+        psrlq    $32,       %%mm1;      \
+        paddq    %%mm3,     %%mm1;      \
+        movd     %%mm1,     28(%%edi);  \
+        addl     $32,       %%edi;      \
+        addl     $32,       %%esi;      \
+        psrlq    $32,       %%mm1;      \
+        movd     %%mm1,     %%ecx;      \
+        "
+
+#define MULADDC_STOP            \
+        "                       \
+        emms;                   \
+        movl   %4, %%ebx;       \
+        movl   %%ecx, %1;       \
+        movl   %%edi, %2;       \
+        movl   %%esi, %3;       \
+        "                       \
+        : "=m" (t), "=m" (c), "=m" (d), "=m" (s)        \
+        : "m" (t), "m" (s), "m" (d), "m" (c), "m" (b)   \
+        : "eax", "ecx", "edx", "esi", "edi"             \
+    );
+
+#else
+
+#define MULADDC_STOP            \
+        "                       \
+        movl   %4, %%ebx;       \
+        movl   %%ecx, %1;       \
+        movl   %%edi, %2;       \
+        movl   %%esi, %3;       \
+        "                       \
+        : "=m" (t), "=m" (c), "=m" (d), "=m" (s)        \
+        : "m" (t), "m" (s), "m" (d), "m" (c), "m" (b)   \
+        : "eax", "ecx", "edx", "esi", "edi"             \
+    );
+#endif /* SSE2 */
+#endif /* i386 */
+
+#if defined(__amd64__) || defined (__x86_64__)
+
+#define MULADDC_INIT                            \
+    asm( "movq   %0, %%rsi      " :: "m" (s));  \
+    asm( "movq   %0, %%rdi      " :: "m" (d));  \
+    asm( "movq   %0, %%rcx      " :: "m" (c));  \
+    asm( "movq   %0, %%rbx      " :: "m" (b));  \
+    asm( "xorq   %r8, %r8       " );
+
+#define MULADDC_CORE                            \
+    asm( "movq  (%rsi),%rax     " );            \
+    asm( "mulq   %rbx           " );            \
+    asm( "addq   $8,   %rsi     " );            \
+    asm( "addq   %rcx, %rax     " );            \
+    asm( "movq   %r8,  %rcx     " );            \
+    asm( "adcq   $0,   %rdx     " );            \
+    asm( "nop                   " );            \
+    asm( "addq   %rax, (%rdi)   " );            \
+    asm( "adcq   %rdx, %rcx     " );            \
+    asm( "addq   $8,   %rdi     " );
+
+#define MULADDC_STOP                            \
+    asm( "movq   %%rcx, %0      " : "=m" (c));  \
+    asm( "movq   %%rdi, %0      " : "=m" (d));  \
+    asm( "movq   %%rsi, %0      " : "=m" (s) :: \
+    "rax", "rcx", "rdx", "rbx", "rsi", "rdi", "r8" );
+
+#endif /* AMD64 */
+
+#if defined(__mc68020__) || defined(__mcpu32__)
+
+#define MULADDC_INIT                            \
+    asm( "movl   %0, %%a2       " :: "m" (s));  \
+    asm( "movl   %0, %%a3       " :: "m" (d));  \
+    asm( "movl   %0, %%d3       " :: "m" (c));  \
+    asm( "movl   %0, %%d2       " :: "m" (b));  \
+    asm( "moveq  #0, %d0        " );
+
+#define MULADDC_CORE                            \
+    asm( "movel  %a2@+, %d1     " );            \
+    asm( "mulul  %d2, %d4:%d1   " );            \
+    asm( "addl   %d3, %d1       " );            \
+    asm( "addxl  %d0, %d4       " );            \
+    asm( "moveq  #0,  %d3       " );            \
+    asm( "addl   %d1, %a3@+     " );            \
+    asm( "addxl  %d4, %d3       " );
+
+#define MULADDC_STOP                            \
+    asm( "movl   %%d3, %0       " : "=m" (c));  \
+    asm( "movl   %%a3, %0       " : "=m" (d));  \
+    asm( "movl   %%a2, %0       " : "=m" (s) :: \
+    "d0", "d1", "d2", "d3", "d4", "a2", "a3" );
+
+#define MULADDC_HUIT                            \
+    asm( "movel  %a2@+, %d1     " );            \
+    asm( "mulul  %d2, %d4:%d1   " );            \
+    asm( "addxl  %d3, %d1       " );            \
+    asm( "addxl  %d0, %d4       " );            \
+    asm( "addl   %d1, %a3@+     " );            \
+    asm( "movel  %a2@+, %d1     " );            \
+    asm( "mulul  %d2, %d3:%d1   " );            \
+    asm( "addxl  %d4, %d1       " );            \
+    asm( "addxl  %d0, %d3       " );            \
+    asm( "addl   %d1, %a3@+     " );            \
+    asm( "movel  %a2@+, %d1     " );            \
+    asm( "mulul  %d2, %d4:%d1   " );            \
+    asm( "addxl  %d3, %d1       " );            \
+    asm( "addxl  %d0, %d4       " );            \
+    asm( "addl   %d1, %a3@+     " );            \
+    asm( "movel  %a2@+, %d1     " );            \
+    asm( "mulul  %d2, %d3:%d1   " );            \
+    asm( "addxl  %d4, %d1       " );            \
+    asm( "addxl  %d0, %d3       " );            \
+    asm( "addl   %d1, %a3@+     " );            \
+    asm( "movel  %a2@+, %d1     " );            \
+    asm( "mulul  %d2, %d4:%d1   " );            \
+    asm( "addxl  %d3, %d1       " );            \
+    asm( "addxl  %d0, %d4       " );            \
+    asm( "addl   %d1, %a3@+     " );            \
+    asm( "movel  %a2@+, %d1     " );            \
+    asm( "mulul  %d2, %d3:%d1   " );            \
+    asm( "addxl  %d4, %d1       " );            \
+    asm( "addxl  %d0, %d3       " );            \
+    asm( "addl   %d1, %a3@+     " );            \
+    asm( "movel  %a2@+, %d1     " );            \
+    asm( "mulul  %d2, %d4:%d1   " );            \
+    asm( "addxl  %d3, %d1       " );            \
+    asm( "addxl  %d0, %d4       " );            \
+    asm( "addl   %d1, %a3@+     " );            \
+    asm( "movel  %a2@+, %d1     " );            \
+    asm( "mulul  %d2, %d3:%d1   " );            \
+    asm( "addxl  %d4, %d1       " );            \
+    asm( "addxl  %d0, %d3       " );            \
+    asm( "addl   %d1, %a3@+     " );            \
+    asm( "addxl  %d0, %d3       " );
+
+#endif /* MC68000 */
+
+#if defined(__powerpc__)   || defined(__ppc__)
+#if defined(__powerpc64__) || defined(__ppc64__)
+
+#if defined(__MACH__) && defined(__APPLE__)
+
+#define MULADDC_INIT                            \
+    asm( "ld     r3, %0         " :: "m" (s));  \
+    asm( "ld     r4, %0         " :: "m" (d));  \
+    asm( "ld     r5, %0         " :: "m" (c));  \
+    asm( "ld     r6, %0         " :: "m" (b));  \
+    asm( "addi   r3, r3, -8     " );            \
+    asm( "addi   r4, r4, -8     " );            \
+    asm( "addic  r5, r5,  0     " );
+
+#define MULADDC_CORE                            \
+    asm( "ldu    r7, 8(r3)      " );            \
+    asm( "mulld  r8, r7, r6     " );            \
+    asm( "mulhdu r9, r7, r6     " );            \
+    asm( "adde   r8, r8, r5     " );            \
+    asm( "ld     r7, 8(r4)      " );            \
+    asm( "addze  r5, r9         " );            \
+    asm( "addc   r8, r8, r7     " );            \
+    asm( "stdu   r8, 8(r4)      " );
+
+#define MULADDC_STOP                            \
+    asm( "addze  r5, r5         " );            \
+    asm( "addi   r4, r4, 8      " );            \
+    asm( "addi   r3, r3, 8      " );            \
+    asm( "std    r5, %0         " : "=m" (c));  \
+    asm( "std    r4, %0         " : "=m" (d));  \
+    asm( "std    r3, %0         " : "=m" (s) :: \
+    "r3", "r4", "r5", "r6", "r7", "r8", "r9" );
+
+#else
+
+#define MULADDC_INIT                            \
+    asm( "ld     %%r3, %0       " :: "m" (s));  \
+    asm( "ld     %%r4, %0       " :: "m" (d));  \
+    asm( "ld     %%r5, %0       " :: "m" (c));  \
+    asm( "ld     %%r6, %0       " :: "m" (b));  \
+    asm( "addi   %r3, %r3, -8   " );            \
+    asm( "addi   %r4, %r4, -8   " );            \
+    asm( "addic  %r5, %r5,  0   " );
+
+#define MULADDC_CORE                            \
+    asm( "ldu    %r7, 8(%r3)    " );            \
+    asm( "mulld  %r8, %r7, %r6  " );            \
+    asm( "mulhdu %r9, %r7, %r6  " );            \
+    asm( "adde   %r8, %r8, %r5  " );            \
+    asm( "ld     %r7, 8(%r4)    " );            \
+    asm( "addze  %r5, %r9       " );            \
+    asm( "addc   %r8, %r8, %r7  " );            \
+    asm( "stdu   %r8, 8(%r4)    " );
+
+#define MULADDC_STOP                            \
+    asm( "addze  %r5, %r5       " );            \
+    asm( "addi   %r4, %r4, 8    " );            \
+    asm( "addi   %r3, %r3, 8    " );            \
+    asm( "std    %%r5, %0       " : "=m" (c));  \
+    asm( "std    %%r4, %0       " : "=m" (d));  \
+    asm( "std    %%r3, %0       " : "=m" (s) :: \
+    "r3", "r4", "r5", "r6", "r7", "r8", "r9" );
+
+#endif
+
+#else /* PPC32 */
+
+#if defined(__MACH__) && defined(__APPLE__)
+
+#define MULADDC_INIT                            \
+    asm( "lwz    r3, %0         " :: "m" (s));  \
+    asm( "lwz    r4, %0         " :: "m" (d));  \
+    asm( "lwz    r5, %0         " :: "m" (c));  \
+    asm( "lwz    r6, %0         " :: "m" (b));  \
+    asm( "addi   r3, r3, -4     " );            \
+    asm( "addi   r4, r4, -4     " );            \
+    asm( "addic  r5, r5,  0     " );
+
+#define MULADDC_CORE                            \
+    asm( "lwzu   r7, 4(r3)      " );            \
+    asm( "mullw  r8, r7, r6     " );            \
+    asm( "mulhwu r9, r7, r6     " );            \
+    asm( "adde   r8, r8, r5     " );            \
+    asm( "lwz    r7, 4(r4)      " );            \
+    asm( "addze  r5, r9         " );            \
+    asm( "addc   r8, r8, r7     " );            \
+    asm( "stwu   r8, 4(r4)      " );
+
+#define MULADDC_STOP                            \
+    asm( "addze  r5, r5         " );            \
+    asm( "addi   r4, r4, 4      " );            \
+    asm( "addi   r3, r3, 4      " );            \
+    asm( "stw    r5, %0         " : "=m" (c));  \
+    asm( "stw    r4, %0         " : "=m" (d));  \
+    asm( "stw    r3, %0         " : "=m" (s) :: \
+    "r3", "r4", "r5", "r6", "r7", "r8", "r9" );
+
+#else
+
+#define MULADDC_INIT                            \
+    asm( "lwz    %%r3, %0       " :: "m" (s));  \
+    asm( "lwz    %%r4, %0       " :: "m" (d));  \
+    asm( "lwz    %%r5, %0       " :: "m" (c));  \
+    asm( "lwz    %%r6, %0       " :: "m" (b));  \
+    asm( "addi   %r3, %r3, -4   " );            \
+    asm( "addi   %r4, %r4, -4   " );            \
+    asm( "addic  %r5, %r5,  0   " );
+
+#define MULADDC_CORE                            \
+    asm( "lwzu   %r7, 4(%r3)    " );            \
+    asm( "mullw  %r8, %r7, %r6  " );            \
+    asm( "mulhwu %r9, %r7, %r6  " );            \
+    asm( "adde   %r8, %r8, %r5  " );            \
+    asm( "lwz    %r7, 4(%r4)    " );            \
+    asm( "addze  %r5, %r9       " );            \
+    asm( "addc   %r8, %r8, %r7  " );            \
+    asm( "stwu   %r8, 4(%r4)    " );
+
+#define MULADDC_STOP                            \
+    asm( "addze  %r5, %r5       " );            \
+    asm( "addi   %r4, %r4, 4    " );            \
+    asm( "addi   %r3, %r3, 4    " );            \
+    asm( "stw    %%r5, %0       " : "=m" (c));  \
+    asm( "stw    %%r4, %0       " : "=m" (d));  \
+    asm( "stw    %%r3, %0       " : "=m" (s) :: \
+    "r3", "r4", "r5", "r6", "r7", "r8", "r9" );
+
+#endif
+
+#endif /* PPC32 */
+#endif /* PPC64 */
+
+#if defined(__sparc__)
+
+#define MULADDC_INIT                            \
+    asm( "ld     %0, %%o0       " :: "m" (s));  \
+    asm( "ld     %0, %%o1       " :: "m" (d));  \
+    asm( "ld     %0, %%o2       " :: "m" (c));  \
+    asm( "ld     %0, %%o3       " :: "m" (b));
+
+#define MULADDC_CORE                            \
+    asm( "ld    [%o0], %o4      " );            \
+    asm( "inc      4,  %o0      " );            \
+    asm( "ld    [%o1], %o5      " );            \
+    asm( "umul   %o3,  %o4, %o4 " );            \
+    asm( "addcc  %o4,  %o2, %o4 " );            \
+    asm( "rd      %y,  %g1      " );            \
+    asm( "addx   %g1,    0, %g1 " );            \
+    asm( "addcc  %o4,  %o5, %o4 " );            \
+    asm( "st     %o4, [%o1]     " );            \
+    asm( "addx   %g1,    0, %o2 " );            \
+    asm( "inc      4,  %o1      " );
+
+#define MULADDC_STOP                            \
+    asm( "st     %%o2, %0       " : "=m" (c));  \
+    asm( "st     %%o1, %0       " : "=m" (d));  \
+    asm( "st     %%o0, %0       " : "=m" (s) :: \
+    "g1", "o0", "o1", "o2", "o3", "o4", "o5" );
+
+#endif /* SPARCv8 */
+
+#if defined(__microblaze__) || defined(microblaze)
+
+#define MULADDC_INIT                            \
+    asm( "lwi   r3,   %0        " :: "m" (s));  \
+    asm( "lwi   r4,   %0        " :: "m" (d));  \
+    asm( "lwi   r5,   %0        " :: "m" (c));  \
+    asm( "lwi   r6,   %0        " :: "m" (b));  \
+    asm( "andi  r7,   r6, 0xffff" );            \
+    asm( "bsrli r6,   r6, 16    " );
+
+#define MULADDC_CORE                            \
+    asm( "lhui  r8,   r3,   0   " );            \
+    asm( "addi  r3,   r3,   2   " );            \
+    asm( "lhui  r9,   r3,   0   " );            \
+    asm( "addi  r3,   r3,   2   " );            \
+    asm( "mul   r10,  r9,  r6   " );            \
+    asm( "mul   r11,  r8,  r7   " );            \
+    asm( "mul   r12,  r9,  r7   " );            \
+    asm( "mul   r13,  r8,  r6   " );            \
+    asm( "bsrli  r8, r10,  16   " );            \
+    asm( "bsrli  r9, r11,  16   " );            \
+    asm( "add   r13, r13,  r8   " );            \
+    asm( "add   r13, r13,  r9   " );            \
+    asm( "bslli r10, r10,  16   " );            \
+    asm( "bslli r11, r11,  16   " );            \
+    asm( "add   r12, r12, r10   " );            \
+    asm( "addc  r13, r13,  r0   " );            \
+    asm( "add   r12, r12, r11   " );            \
+    asm( "addc  r13, r13,  r0   " );            \
+    asm( "lwi   r10,  r4,   0   " );            \
+    asm( "add   r12, r12, r10   " );            \
+    asm( "addc  r13, r13,  r0   " );            \
+    asm( "add   r12, r12,  r5   " );            \
+    asm( "addc   r5, r13,  r0   " );            \
+    asm( "swi   r12,  r4,   0   " );            \
+    asm( "addi   r4,  r4,   4   " );
+
+#define MULADDC_STOP                            \
+    asm( "swi   r5,   %0        " : "=m" (c));  \
+    asm( "swi   r4,   %0        " : "=m" (d));  \
+    asm( "swi   r3,   %0        " : "=m" (s) :: \
+     "r3", "r4" , "r5" , "r6" , "r7" , "r8" ,   \
+     "r9", "r10", "r11", "r12", "r13" );
+
+#endif /* MicroBlaze */
+
+#if defined(__tricore__)
+
+#define MULADDC_INIT                            \
+    asm( "ld.a   %%a2, %0       " :: "m" (s));  \
+    asm( "ld.a   %%a3, %0       " :: "m" (d));  \
+    asm( "ld.w   %%d4, %0       " :: "m" (c));  \
+    asm( "ld.w   %%d1, %0       " :: "m" (b));  \
+    asm( "xor    %d5, %d5       " );
+
+#define MULADDC_CORE                            \
+    asm( "ld.w   %d0,   [%a2+]      " );        \
+    asm( "madd.u %e2, %e4, %d0, %d1 " );        \
+    asm( "ld.w   %d0,   [%a3]       " );        \
+    asm( "addx   %d2,    %d2,  %d0  " );        \
+    asm( "addc   %d3,    %d3,    0  " );        \
+    asm( "mov    %d4,    %d3        " );        \
+    asm( "st.w  [%a3+],  %d2        " );
+
+#define MULADDC_STOP                            \
+    asm( "st.w   %0, %%d4       " : "=m" (c));  \
+    asm( "st.a   %0, %%a3       " : "=m" (d));  \
+    asm( "st.a   %0, %%a2       " : "=m" (s) :: \
+    "d0", "d1", "e2", "d4", "a2", "a3" );
+
+#endif /* TriCore */
+
+#if defined(__arm__)
+
+#define MULADDC_INIT                            \
+    asm( "ldr    r0, %0         " :: "m" (s));  \
+    asm( "ldr    r1, %0         " :: "m" (d));  \
+    asm( "ldr    r2, %0         " :: "m" (c));  \
+    asm( "ldr    r3, %0         " :: "m" (b));
+
+#define MULADDC_CORE                            \
+    asm( "ldr    r4, [r0], #4   " );            \
+    asm( "mov    r5, #0         " );            \
+    asm( "ldr    r6, [r1]       " );            \
+    asm( "umlal  r2, r5, r3, r4 " );            \
+    asm( "adds   r7, r6, r2     " );            \
+    asm( "adc    r2, r5, #0     " );            \
+    asm( "str    r7, [r1], #4   " );
+
+#define MULADDC_STOP                            \
+    asm( "str    r2, %0         " : "=m" (c));  \
+    asm( "str    r1, %0         " : "=m" (d));  \
+    asm( "str    r0, %0         " : "=m" (s) :: \
+    "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7" );
+
+#endif /* ARMv3 */
+
+#if defined(__alpha__)
+
+#define MULADDC_INIT                            \
+    asm( "ldq    $1, %0         " :: "m" (s));  \
+    asm( "ldq    $2, %0         " :: "m" (d));  \
+    asm( "ldq    $3, %0         " :: "m" (c));  \
+    asm( "ldq    $4, %0         " :: "m" (b));
+
+#define MULADDC_CORE                            \
+    asm( "ldq    $6,  0($1)     " );            \
+    asm( "addq   $1,  8, $1     " );            \
+    asm( "mulq   $6, $4, $7     " );            \
+    asm( "umulh  $6, $4, $6     " );            \
+    asm( "addq   $7, $3, $7     " );            \
+    asm( "cmpult $7, $3, $3     " );            \
+    asm( "ldq    $5,  0($2)     " );            \
+    asm( "addq   $7, $5, $7     " );            \
+    asm( "cmpult $7, $5, $5     " );            \
+    asm( "stq    $7,  0($2)     " );            \
+    asm( "addq   $2,  8, $2     " );            \
+    asm( "addq   $6, $3, $3     " );            \
+    asm( "addq   $5, $3, $3     " );
+
+#define MULADDC_STOP                            \
+    asm( "stq    $3, %0         " : "=m" (c));  \
+    asm( "stq    $2, %0         " : "=m" (d));  \
+    asm( "stq    $1, %0         " : "=m" (s) :: \
+    "$1", "$2", "$3", "$4", "$5", "$6", "$7" );
+
+#endif /* Alpha */
+
+#if defined(__mips__)
+
+#define MULADDC_INIT                            \
+    asm( "lw     $10, %0        " :: "m" (s));  \
+    asm( "lw     $11, %0        " :: "m" (d));  \
+    asm( "lw     $12, %0        " :: "m" (c));  \
+    asm( "lw     $13, %0        " :: "m" (b));
+
+#define MULADDC_CORE                            \
+    asm( "lw     $14, 0($10)    " );            \
+    asm( "multu  $13, $14       " );            \
+    asm( "addi   $10, $10, 4    " );            \
+    asm( "mflo   $14            " );            \
+    asm( "mfhi   $9             " );            \
+    asm( "addu   $14, $12, $14  " );            \
+    asm( "lw     $15, 0($11)    " );            \
+    asm( "sltu   $12, $14, $12  " );            \
+    asm( "addu   $15, $14, $15  " );            \
+    asm( "sltu   $14, $15, $14  " );            \
+    asm( "addu   $12, $12, $9   " );            \
+    asm( "sw     $15, 0($11)    " );            \
+    asm( "addu   $12, $12, $14  " );            \
+    asm( "addi   $11, $11, 4    " );
+
+#define MULADDC_STOP                            \
+    asm( "sw     $12, %0        " : "=m" (c));  \
+    asm( "sw     $11, %0        " : "=m" (d));  \
+    asm( "sw     $10, %0        " : "=m" (s) :: \
+    "$9", "$10", "$11", "$12", "$13", "$14", "$15" );
+
+#endif /* MIPS */
+#endif /* GNUC */
+
+#if (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
+
+#define MULADDC_INIT                            \
+    __asm   mov     esi, s                      \
+    __asm   mov     edi, d                      \
+    __asm   mov     ecx, c                      \
+    __asm   mov     ebx, b
+
+#define MULADDC_CORE                            \
+    __asm   lodsd                               \
+    __asm   mul     ebx                         \
+    __asm   add     eax, ecx                    \
+    __asm   adc     edx, 0                      \
+    __asm   add     eax, [edi]                  \
+    __asm   adc     edx, 0                      \
+    __asm   mov     ecx, edx                    \
+    __asm   stosd
+
+#if defined(POLARSSL_HAVE_SSE2)
+
+#define EMIT __asm _emit
+
+#define MULADDC_HUIT                            \
+    EMIT 0x0F  EMIT 0x6E  EMIT 0xC9             \
+    EMIT 0x0F  EMIT 0x6E  EMIT 0xC3             \
+    EMIT 0x0F  EMIT 0x6E  EMIT 0x1F             \
+    EMIT 0x0F  EMIT 0xD4  EMIT 0xCB             \
+    EMIT 0x0F  EMIT 0x6E  EMIT 0x16             \
+    EMIT 0x0F  EMIT 0xF4  EMIT 0xD0             \
+    EMIT 0x0F  EMIT 0x6E  EMIT 0x66  EMIT 0x04  \
+    EMIT 0x0F  EMIT 0xF4  EMIT 0xE0             \
+    EMIT 0x0F  EMIT 0x6E  EMIT 0x76  EMIT 0x08  \
+    EMIT 0x0F  EMIT 0xF4  EMIT 0xF0             \
+    EMIT 0x0F  EMIT 0x6E  EMIT 0x7E  EMIT 0x0C  \
+    EMIT 0x0F  EMIT 0xF4  EMIT 0xF8             \
+    EMIT 0x0F  EMIT 0xD4  EMIT 0xCA             \
+    EMIT 0x0F  EMIT 0x6E  EMIT 0x5F  EMIT 0x04  \
+    EMIT 0x0F  EMIT 0xD4  EMIT 0xDC             \
+    EMIT 0x0F  EMIT 0x6E  EMIT 0x6F  EMIT 0x08  \
+    EMIT 0x0F  EMIT 0xD4  EMIT 0xEE             \
+    EMIT 0x0F  EMIT 0x6E  EMIT 0x67  EMIT 0x0C  \
+    EMIT 0x0F  EMIT 0xD4  EMIT 0xFC             \
+    EMIT 0x0F  EMIT 0x7E  EMIT 0x0F             \
+    EMIT 0x0F  EMIT 0x6E  EMIT 0x56  EMIT 0x10  \
+    EMIT 0x0F  EMIT 0xF4  EMIT 0xD0             \
+    EMIT 0x0F  EMIT 0x73  EMIT 0xD1  EMIT 0x20  \
+    EMIT 0x0F  EMIT 0x6E  EMIT 0x66  EMIT 0x14  \
+    EMIT 0x0F  EMIT 0xF4  EMIT 0xE0             \
+    EMIT 0x0F  EMIT 0xD4  EMIT 0xCB             \
+    EMIT 0x0F  EMIT 0x6E  EMIT 0x76  EMIT 0x18  \
+    EMIT 0x0F  EMIT 0xF4  EMIT 0xF0             \
+    EMIT 0x0F  EMIT 0x7E  EMIT 0x4F  EMIT 0x04  \
+    EMIT 0x0F  EMIT 0x73  EMIT 0xD1  EMIT 0x20  \
+    EMIT 0x0F  EMIT 0x6E  EMIT 0x5E  EMIT 0x1C  \
+    EMIT 0x0F  EMIT 0xF4  EMIT 0xD8             \
+    EMIT 0x0F  EMIT 0xD4  EMIT 0xCD             \
+    EMIT 0x0F  EMIT 0x6E  EMIT 0x6F  EMIT 0x10  \
+    EMIT 0x0F  EMIT 0xD4  EMIT 0xD5             \
+    EMIT 0x0F  EMIT 0x7E  EMIT 0x4F  EMIT 0x08  \
+    EMIT 0x0F  EMIT 0x73  EMIT 0xD1  EMIT 0x20  \
+    EMIT 0x0F  EMIT 0xD4  EMIT 0xCF             \
+    EMIT 0x0F  EMIT 0x6E  EMIT 0x6F  EMIT 0x14  \
+    EMIT 0x0F  EMIT 0xD4  EMIT 0xE5             \
+    EMIT 0x0F  EMIT 0x7E  EMIT 0x4F  EMIT 0x0C  \
+    EMIT 0x0F  EMIT 0x73  EMIT 0xD1  EMIT 0x20  \
+    EMIT 0x0F  EMIT 0xD4  EMIT 0xCA             \
+    EMIT 0x0F  EMIT 0x6E  EMIT 0x6F  EMIT 0x18  \
+    EMIT 0x0F  EMIT 0xD4  EMIT 0xF5             \
+    EMIT 0x0F  EMIT 0x7E  EMIT 0x4F  EMIT 0x10  \
+    EMIT 0x0F  EMIT 0x73  EMIT 0xD1  EMIT 0x20  \
+    EMIT 0x0F  EMIT 0xD4  EMIT 0xCC             \
+    EMIT 0x0F  EMIT 0x6E  EMIT 0x6F  EMIT 0x1C  \
+    EMIT 0x0F  EMIT 0xD4  EMIT 0xDD             \
+    EMIT 0x0F  EMIT 0x7E  EMIT 0x4F  EMIT 0x14  \
+    EMIT 0x0F  EMIT 0x73  EMIT 0xD1  EMIT 0x20  \
+    EMIT 0x0F  EMIT 0xD4  EMIT 0xCE             \
+    EMIT 0x0F  EMIT 0x7E  EMIT 0x4F  EMIT 0x18  \
+    EMIT 0x0F  EMIT 0x73  EMIT 0xD1  EMIT 0x20  \
+    EMIT 0x0F  EMIT 0xD4  EMIT 0xCB             \
+    EMIT 0x0F  EMIT 0x7E  EMIT 0x4F  EMIT 0x1C  \
+    EMIT 0x83  EMIT 0xC7  EMIT 0x20             \
+    EMIT 0x83  EMIT 0xC6  EMIT 0x20             \
+    EMIT 0x0F  EMIT 0x73  EMIT 0xD1  EMIT 0x20  \
+    EMIT 0x0F  EMIT 0x7E  EMIT 0xC9
+
+#define MULADDC_STOP                            \
+    EMIT 0x0F  EMIT 0x77                        \
+    __asm   mov     c, ecx                      \
+    __asm   mov     d, edi                      \
+    __asm   mov     s, esi                      \
+
+#else
+
+#define MULADDC_STOP                            \
+    __asm   mov     c, ecx                      \
+    __asm   mov     d, edi                      \
+    __asm   mov     s, esi                      \
+
+#endif /* SSE2 */
+#endif /* MSVC */
+
+#endif /* POLARSSL_HAVE_ASM */
+
+#if !defined(MULADDC_CORE)
+#if defined(POLARSSL_HAVE_LONGLONG)
+
+#define MULADDC_INIT                    \
+{                                       \
+    t_dbl r;                            \
+    t_int r0, r1;
+
+#define MULADDC_CORE                    \
+    r   = *(s++) * (t_dbl) b;           \
+    r0  = r;                            \
+    r1  = r >> biL;                     \
+    r0 += c;  r1 += (r0 <  c);          \
+    r0 += *d; r1 += (r0 < *d);          \
+    c = r1; *(d++) = r0;
+
+#define MULADDC_STOP                    \
+}
+
+#else
+#define MULADDC_INIT                    \
+{                                       \
+    t_int s0, s1, b0, b1;               \
+    t_int r0, r1, rx, ry;               \
+    b0 = ( b << biH ) >> biH;           \
+    b1 = ( b >> biH );
+
+#define MULADDC_CORE                    \
+    s0 = ( *s << biH ) >> biH;          \
+    s1 = ( *s >> biH ); s++;            \
+    rx = s0 * b1; r0 = s0 * b0;         \
+    ry = s1 * b0; r1 = s1 * b1;         \
+    r1 += ( rx >> biH );                \
+    r1 += ( ry >> biH );                \
+    rx <<= biH; ry <<= biH;             \
+    r0 += rx; r1 += (r0 < rx);          \
+    r0 += ry; r1 += (r0 < ry);          \
+    r0 +=  c; r1 += (r0 <  c);          \
+    r0 += *d; r1 += (r0 < *d);          \
+    c = r1; *(d++) = r0;
+
+#define MULADDC_STOP                    \
+}
+
+#endif /* C (generic)  */
+#endif /* C (longlong) */
+
+#endif /* bn_mul.h */
diff --git a/src/pdkim/pdkim.c b/src/pdkim/pdkim.c
new file mode 100644 (file)
index 0000000..4f0da3f
--- /dev/null
@@ -0,0 +1,1773 @@
+/*
+ *  PDKIM - a RFC4871 (DKIM) implementation
+ *
+ *  Copyright (C) 2009 - 2012  Tom Kistner <tom@duncanthrax.net>
+ *
+ *  http://duncanthrax.net/pdkim/
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License along
+ *  with this program; if not, write to the Free Software Foundation, Inc.,
+ *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <ctype.h>
+
+#include "pdkim.h"
+
+#include "sha1.h"
+#include "sha2.h"
+#include "rsa.h"
+#include "base64.h"
+
+#define PDKIM_SIGNATURE_VERSION     "1"
+#define PDKIM_PUB_RECORD_VERSION    "DKIM1"
+
+#define PDKIM_MAX_HEADER_LEN        65536
+#define PDKIM_MAX_HEADERS           512
+#define PDKIM_MAX_BODY_LINE_LEN     16384
+#define PDKIM_DNS_TXT_MAX_NAMELEN   1024
+#define PDKIM_DEFAULT_SIGN_HEADERS "From:Sender:Reply-To:Subject:Date:"\
+                             "Message-ID:To:Cc:MIME-Version:Content-Type:"\
+                             "Content-Transfer-Encoding:Content-ID:"\
+                             "Content-Description:Resent-Date:Resent-From:"\
+                             "Resent-Sender:Resent-To:Resent-Cc:"\
+                             "Resent-Message-ID:In-Reply-To:References:"\
+                             "List-Id:List-Help:List-Unsubscribe:"\
+                             "List-Subscribe:List-Post:List-Owner:List-Archive"
+
+/* -------------------------------------------------------------------------- */
+struct pdkim_stringlist {
+  char *value;
+  int  tag;
+  void *next;
+};
+
+#define PDKIM_STR_ALLOC_FRAG 256
+struct pdkim_str {
+  char         *str;
+  unsigned int  len;
+  unsigned int  allocated;
+};
+
+/* -------------------------------------------------------------------------- */
+/* A bunch of list constants */
+const char *pdkim_querymethods[] = {
+  "dns/txt",
+  NULL
+};
+const char *pdkim_algos[] = {
+  "rsa-sha256",
+  "rsa-sha1",
+  NULL
+};
+const char *pdkim_canons[] = {
+  "simple",
+  "relaxed",
+  NULL
+};
+const char *pdkim_hashes[] = {
+  "sha256",
+  "sha1",
+  NULL
+};
+const char *pdkim_keytypes[] = {
+  "rsa",
+  NULL
+};
+
+typedef struct pdkim_combined_canon_entry {
+  const char *str;
+  int canon_headers;
+  int canon_body;
+} pdkim_combined_canon_entry;
+pdkim_combined_canon_entry pdkim_combined_canons[] = {
+  { "simple/simple",    PDKIM_CANON_SIMPLE,   PDKIM_CANON_SIMPLE },
+  { "simple/relaxed",   PDKIM_CANON_SIMPLE,   PDKIM_CANON_RELAXED },
+  { "relaxed/simple",   PDKIM_CANON_RELAXED,  PDKIM_CANON_SIMPLE },
+  { "relaxed/relaxed",  PDKIM_CANON_RELAXED,  PDKIM_CANON_RELAXED },
+  { "simple",           PDKIM_CANON_SIMPLE,   PDKIM_CANON_SIMPLE },
+  { "relaxed",          PDKIM_CANON_RELAXED,  PDKIM_CANON_SIMPLE },
+  { NULL,               0,                    0 }
+};
+
+
+const char *pdkim_verify_status_str(int status) {
+  switch(status) {
+    case PDKIM_VERIFY_NONE:    return "PDKIM_VERIFY_NONE";
+    case PDKIM_VERIFY_INVALID: return "PDKIM_VERIFY_INVALID";
+    case PDKIM_VERIFY_FAIL:    return "PDKIM_VERIFY_FAIL";
+    case PDKIM_VERIFY_PASS:    return "PDKIM_VERIFY_PASS";
+    default:                   return "PDKIM_VERIFY_UNKNOWN";
+  }
+}
+const char *pdkim_verify_ext_status_str(int ext_status) {
+  switch(ext_status) {
+    case PDKIM_VERIFY_FAIL_BODY: return "PDKIM_VERIFY_FAIL_BODY";
+    case PDKIM_VERIFY_FAIL_MESSAGE: return "PDKIM_VERIFY_FAIL_MESSAGE";
+    case PDKIM_VERIFY_INVALID_PUBKEY_UNAVAILABLE: return "PDKIM_VERIFY_INVALID_PUBKEY_UNAVAILABLE";
+    case PDKIM_VERIFY_INVALID_BUFFER_SIZE: return "PDKIM_VERIFY_INVALID_BUFFER_SIZE";
+    case PDKIM_VERIFY_INVALID_PUBKEY_PARSING: return "PDKIM_VERIFY_INVALID_PUBKEY_PARSING";
+    default: return "PDKIM_VERIFY_UNKNOWN";
+  }
+}
+
+
+/* -------------------------------------------------------------------------- */
+/* Print debugging functions */
+#ifdef PDKIM_DEBUG
+void pdkim_quoteprint(FILE *stream, const char *data, int len, int lf) {
+  int i;
+  const unsigned char *p = (const unsigned char *)data;
+
+  for (i=0;i<len;i++) {
+    const int c = p[i];
+    switch (c) {
+      case ' ' : fprintf(stream,"{SP}"); break;
+      case '\t': fprintf(stream,"{TB}"); break;
+      case '\r': fprintf(stream,"{CR}"); break;
+      case '\n': fprintf(stream,"{LF}"); break;
+      case '{' : fprintf(stream,"{BO}"); break;
+      case '}' : fprintf(stream,"{BC}"); break;
+      default:
+        if ( (c < 32) || (c > 127) )
+          fprintf(stream,"{%02x}",c);
+        else
+          fputc(c,stream);
+      break;
+    }
+  }
+  if (lf)
+    fputc('\n',stream);
+}
+void pdkim_hexprint(FILE *stream, const char *data, int len, int lf) {
+  int i;
+  const unsigned char *p = (const unsigned char *)data;
+
+  for (i=0;i<len;i++) {
+    const int c = p[i];
+    fprintf(stream,"%02x",c);
+  }
+  if (lf)
+    fputc('\n',stream);
+}
+#endif
+
+
+/* -------------------------------------------------------------------------- */
+/* Simple string list implementation for convinience */
+pdkim_stringlist *pdkim_append_stringlist(pdkim_stringlist *base, char *str) {
+  pdkim_stringlist *new_entry = malloc(sizeof(pdkim_stringlist));
+  if (new_entry == NULL) return NULL;
+  memset(new_entry,0,sizeof(pdkim_stringlist));
+  new_entry->value = strdup(str);
+  if (new_entry->value == NULL) return NULL;
+  if (base != NULL) {
+    pdkim_stringlist *last = base;
+    while (last->next != NULL) { last = last->next; }
+    last->next = new_entry;
+    return base;
+  }
+  else return new_entry;
+}
+pdkim_stringlist *pdkim_prepend_stringlist(pdkim_stringlist *base, char *str) {
+  pdkim_stringlist *new_entry = malloc(sizeof(pdkim_stringlist));
+  if (new_entry == NULL) return NULL;
+  memset(new_entry,0,sizeof(pdkim_stringlist));
+  new_entry->value = strdup(str);
+  if (new_entry->value == NULL) return NULL;
+  if (base != NULL) {
+    new_entry->next = base;
+  }
+  return new_entry;
+}
+
+
+/* -------------------------------------------------------------------------- */
+/* A small "growing string" implementation to escape malloc/realloc hell */
+pdkim_str *pdkim_strnew (const char *cstr) {
+  unsigned int len = cstr?strlen(cstr):0;
+  pdkim_str *p = malloc(sizeof(pdkim_str));
+  if (p == NULL) return NULL;
+  memset(p,0,sizeof(pdkim_str));
+  p->str = malloc(len+1);
+  if (p->str == NULL) {
+    free(p);
+    return NULL;
+  }
+  p->allocated=(len+1);
+  p->len=len;
+  if (cstr) strcpy(p->str,cstr);
+  else p->str[p->len] = '\0';
+  return p;
+}
+char *pdkim_strncat(pdkim_str *str, const char *data, int len) {
+  if ((str->allocated - str->len) < (len+1)) {
+    /* Extend the buffer */
+    int num_frags = ((len+1)/PDKIM_STR_ALLOC_FRAG)+1;
+    char *n = realloc(str->str,
+                      (str->allocated+(num_frags*PDKIM_STR_ALLOC_FRAG)));
+    if (n == NULL) return NULL;
+    str->str = n;
+    str->allocated += (num_frags*PDKIM_STR_ALLOC_FRAG);
+  }
+  strncpy(&(str->str[str->len]),data,len);
+  str->len+=len;
+  str->str[str->len] = '\0';
+  return str->str;
+}
+char *pdkim_strcat(pdkim_str *str, const char *cstr) {
+  return pdkim_strncat(str, cstr, strlen(cstr));
+}
+char *pdkim_numcat(pdkim_str *str, unsigned long num) {
+  char minibuf[20];
+  snprintf(minibuf,20,"%lu",num);
+  return pdkim_strcat(str,minibuf);
+}
+char *pdkim_strtrim(pdkim_str *str) {
+  char *p = str->str;
+  char *q = str->str;
+  while ( (*p != '\0') && ((*p == '\t') || (*p == ' ')) ) p++;
+  while (*p != '\0') {*q = *p; q++; p++;}
+  *q = '\0';
+  while ( (q != str->str) && ( (*q == '\0') || (*q == '\t') || (*q == ' ') ) ) {
+    *q = '\0';
+    q--;
+  }
+  str->len = strlen(str->str);
+  return str->str;
+}
+char *pdkim_strclear(pdkim_str *str) {
+  str->str[0] = '\0';
+  str->len = 0;
+  return str->str;
+}
+void pdkim_strfree(pdkim_str *str) {
+  if (str == NULL) return;
+  if (str->str != NULL) free(str->str);
+  free(str);
+}
+
+
+
+/* -------------------------------------------------------------------------- */
+void pdkim_free_pubkey(pdkim_pubkey *pub) {
+  if (pub) {
+    if (pub->version        != NULL) free(pub->version);
+    if (pub->granularity    != NULL) free(pub->granularity);
+    if (pub->hashes         != NULL) free(pub->hashes);
+    if (pub->keytype        != NULL) free(pub->keytype);
+    if (pub->srvtype        != NULL) free(pub->srvtype);
+    if (pub->notes          != NULL) free(pub->notes);
+    if (pub->key            != NULL) free(pub->key);
+    free(pub);
+  }
+}
+
+
+/* -------------------------------------------------------------------------- */
+void pdkim_free_sig(pdkim_signature *sig) {
+  if (sig) {
+    pdkim_signature *next = (pdkim_signature *)sig->next;
+
+    pdkim_stringlist *e = sig->headers;
+    while(e != NULL) {
+      pdkim_stringlist *c = e;
+      if (e->value != NULL) free(e->value);
+      e = e->next;
+      free(c);
+    }
+
+    if (sig->sigdata          != NULL) free(sig->sigdata);
+    if (sig->bodyhash         != NULL) free(sig->bodyhash);
+    if (sig->selector         != NULL) free(sig->selector);
+    if (sig->domain           != NULL) free(sig->domain);
+    if (sig->identity         != NULL) free(sig->identity);
+    if (sig->headernames      != NULL) free(sig->headernames);
+    if (sig->copiedheaders    != NULL) free(sig->copiedheaders);
+    if (sig->rsa_privkey      != NULL) free(sig->rsa_privkey);
+    if (sig->sign_headers     != NULL) free(sig->sign_headers);
+    if (sig->signature_header != NULL) free(sig->signature_header);
+    if (sig->sha1_body        != NULL) free(sig->sha1_body);
+    if (sig->sha2_body        != NULL) free(sig->sha2_body);
+
+    if (sig->pubkey != NULL) pdkim_free_pubkey(sig->pubkey);
+
+    free(sig);
+    if (next != NULL) pdkim_free_sig(next);
+  }
+}
+
+
+/* -------------------------------------------------------------------------- */
+DLLEXPORT void pdkim_free_ctx(pdkim_ctx *ctx) {
+  if (ctx) {
+    pdkim_stringlist *e = ctx->headers;
+    while(e != NULL) {
+      pdkim_stringlist *c = e;
+      if (e->value != NULL) free(e->value);
+      e = e->next;
+      free(c);
+    }
+    pdkim_free_sig(ctx->sig);
+    pdkim_strfree(ctx->cur_header);
+    free(ctx);
+  }
+}
+
+
+/* -------------------------------------------------------------------------- */
+/* Matches the name of the passed raw "header" against
+   the passed colon-separated "list", starting at entry
+   "start". Returns the position of the header name in
+   the list. */
+int header_name_match(const char *header,
+                      char       *tick,
+                      int         do_tick) {
+  char *hname;
+  char *lcopy;
+  char *p;
+  char *q;
+  int rc = PDKIM_FAIL;
+
+  /* Get header name */
+  char *hcolon = strchr(header,':');
+  if (hcolon == NULL) return rc; /* This isn't a header */
+  hname = malloc((hcolon-header)+1);
+  if (hname == NULL) return PDKIM_ERR_OOM;
+  memset(hname,0,(hcolon-header)+1);
+  strncpy(hname,header,(hcolon-header));
+
+  /* Copy tick-off list locally, so we can punch zeroes into it */
+  lcopy = strdup(tick);
+  if (lcopy == NULL) {
+    free(hname);
+    return PDKIM_ERR_OOM;
+  }
+  p = lcopy;
+  q = strchr(p,':');
+  while (q != NULL) {
+    *q = '\0';
+
+    if (strcasecmp(p,hname) == 0) {
+      rc = PDKIM_OK;
+      /* Invalidate header name instance in tick-off list */
+      if (do_tick) tick[p-lcopy] = '_';
+      goto BAIL;
+    }
+
+    p = q+1;
+    q = strchr(p,':');
+  }
+
+  if (strcasecmp(p,hname) == 0) {
+    rc = PDKIM_OK;
+    /* Invalidate header name instance in tick-off list */
+    if (do_tick) tick[p-lcopy] = '_';
+  }
+
+  BAIL:
+  free(hname);
+  free(lcopy);
+  return rc;
+}
+
+
+/* -------------------------------------------------------------------------- */
+/* Performs "relaxed" canonicalization of a header. The returned pointer needs
+   to be free()d. */
+char *pdkim_relax_header (char *header, int crlf) {
+  int past_field_name = 0;
+  int seen_wsp = 0;
+  char *p = header;
+  char *q;
+  char *relaxed = malloc(strlen(header)+3);
+  if (relaxed == NULL) return NULL;
+  q = relaxed;
+  while (*p != '\0') {
+    int c = *p;
+    /* Ignore CR & LF */
+    if ( (c == '\r') || (c == '\n') ) {
+      p++;
+      continue;
+    }
+    if ( (c == '\t') || (c == ' ') ) {
+      c = ' '; /* Turns WSP into SP */
+      if (seen_wsp) {
+        p++;
+        continue;
+      }
+      else seen_wsp = 1;
+    }
+    else {
+      if ( (!past_field_name) && (c == ':') ) {
+        if (seen_wsp) q--;   /* This removes WSP before the colon */
+        seen_wsp = 1;        /* This removes WSP after the colon */
+        past_field_name = 1;
+      }
+      else seen_wsp = 0;
+    }
+    /* Lowercase header name */
+    if (!past_field_name) c = tolower(c);
+    *q = c;
+    p++;
+    q++;
+  }
+  if ((q>relaxed) && (*(q-1) == ' ')) q--; /* Squash eventual trailing SP */
+  *q = '\0';
+  if (crlf) strcat(relaxed,"\r\n");
+  return relaxed;
+}
+
+
+/* -------------------------------------------------------------------------- */
+#define PDKIM_QP_ERROR_DECODE -1
+char *pdkim_decode_qp_char(char *qp_p, int *c) {
+  char *initial_pos = qp_p;
+
+  /* Advance one char */
+  qp_p++;
+
+  /* Check for two hex digits and decode them */
+  if (isxdigit(*qp_p) && isxdigit(qp_p[1])) {
+    /* Do hex conversion */
+    if (isdigit(*qp_p)) {*c = *qp_p - '0';}
+    else {*c = toupper(*qp_p) - 'A' + 10;}
+    *c <<= 4;
+    if (isdigit(qp_p[1])) {*c |= qp_p[1] - '0';}
+    else {*c |= toupper(qp_p[1]) - 'A' + 10;}
+    return qp_p + 2;
+  }
+
+  /* Illegal char here */
+  *c = PDKIM_QP_ERROR_DECODE;
+  return initial_pos;
+}
+
+
+/* -------------------------------------------------------------------------- */
+char *pdkim_decode_qp(char *str) {
+  int nchar = 0;
+  char *q;
+  char *p = str;
+  char *n = malloc(strlen(p)+1);
+  if (n == NULL) return NULL;
+  *n = '\0';
+  q = n;
+  while (*p != '\0') {
+    if (*p == '=') {
+      p = pdkim_decode_qp_char(p,&nchar);
+      if (nchar >= 0) {
+        *q = nchar;
+        q++;
+        continue;
+      }
+    }
+    else {
+      *q = *p;
+      q++;
+    }
+    p++;
+  }
+  *q = '\0';
+  return n;
+}
+
+
+/* -------------------------------------------------------------------------- */
+char *pdkim_decode_base64(char *str, int *num_decoded) {
+  int dlen = 0;
+  char *res;
+
+  base64_decode(NULL, &dlen, (unsigned char *)str, strlen(str));
+  res = malloc(dlen+1);
+  if (res == NULL) return NULL;
+  if (base64_decode((unsigned char *)res,&dlen,(unsigned char *)str,strlen(str)) != 0) {
+    free(res);
+    return NULL;
+  }
+  if (num_decoded != NULL) *num_decoded = dlen;
+  return res;
+}
+
+/* -------------------------------------------------------------------------- */
+char *pdkim_encode_base64(char *str, int num) {
+  int dlen = 0;
+  char *res;
+
+  base64_encode(NULL, &dlen, (unsigned char *)str, num);
+  res = malloc(dlen+1);
+  if (res == NULL) return NULL;
+  if (base64_encode((unsigned char *)res,&dlen,(unsigned char *)str,num) != 0) {
+    free(res);
+    return NULL;
+  }
+  return res;
+}
+
+
+/* -------------------------------------------------------------------------- */
+#define PDKIM_HDR_LIMBO 0
+#define PDKIM_HDR_TAG   1
+#define PDKIM_HDR_VALUE 2
+pdkim_signature *pdkim_parse_sig_header(pdkim_ctx *ctx, char *raw_hdr) {
+  pdkim_signature *sig ;
+  char *p,*q;
+  pdkim_str *cur_tag = NULL;
+  pdkim_str *cur_val = NULL;
+  int past_hname = 0;
+  int in_b_val = 0;
+  int where = PDKIM_HDR_LIMBO;
+  int i;
+
+  sig = malloc(sizeof(pdkim_signature));
+  if (sig == NULL) return NULL;
+  memset(sig,0,sizeof(pdkim_signature));
+  sig->bodylength = -1;
+
+  sig->rawsig_no_b_val = malloc(strlen(raw_hdr)+1);
+  if (sig->rawsig_no_b_val == NULL) {
+    free(sig);
+    return NULL;
+  }
+
+  p = raw_hdr;
+  q = sig->rawsig_no_b_val;
+
+  while (1) {
+
+    /* Ignore FWS */
+    if ( (*p == '\r') || (*p == '\n') )
+      goto NEXT_CHAR;
+
+    /* Fast-forward through header name */
+    if (!past_hname) {
+      if (*p == ':') past_hname = 1;
+      goto NEXT_CHAR;
+    }
+
+    if (where == PDKIM_HDR_LIMBO) {
+      /* In limbo, just wait for a tag-char to appear */
+      if (!((*p >= 'a') && (*p <= 'z')))
+        goto NEXT_CHAR;
+
+      where = PDKIM_HDR_TAG;
+    }
+
+    if (where == PDKIM_HDR_TAG) {
+      if (cur_tag == NULL)
+        cur_tag = pdkim_strnew(NULL);
+
+      if ((*p >= 'a') && (*p <= 'z'))
+        pdkim_strncat(cur_tag,p,1);
+
+      if (*p == '=') {
+        if (strcmp(cur_tag->str,"b") == 0) {
+          *q = '='; q++;
+          in_b_val = 1;
+        }
+        where = PDKIM_HDR_VALUE;
+        goto NEXT_CHAR;
+      }
+    }
+
+    if (where == PDKIM_HDR_VALUE) {
+      if (cur_val == NULL)
+        cur_val = pdkim_strnew(NULL);
+
+      if ( (*p == '\r') || (*p == '\n') || (*p == ' ') || (*p == '\t') )
+        goto NEXT_CHAR;
+
+      if ( (*p == ';') || (*p == '\0') ) {
+        if (cur_tag->len > 0) {
+          pdkim_strtrim(cur_val);
+          #ifdef PDKIM_DEBUG
+          if (ctx->debug_stream)
+            fprintf(ctx->debug_stream, "%s=%s\n", cur_tag->str, cur_val->str);
+          #endif
+          switch (cur_tag->str[0]) {
+            case 'b':
+              switch (cur_tag->str[1]) {
+                case 'h':
+                  sig->bodyhash = pdkim_decode_base64(cur_val->str,&(sig->bodyhash_len));
+                break;
+                default:
+                  sig->sigdata = pdkim_decode_base64(cur_val->str,&(sig->sigdata_len));
+                break;
+              }
+            break;
+            case 'v':
+              if (strcmp(cur_val->str,PDKIM_SIGNATURE_VERSION) == 0) {
+                /* We only support version 1, and that is currently the
+                   only version there is. */
+                sig->version = 1;
+              }
+            break;
+            case 'a':
+              i = 0;
+              while (pdkim_algos[i] != NULL) {
+                if (strcmp(cur_val->str,pdkim_algos[i]) == 0 ) {
+                  sig->algo = i;
+                  break;
+                }
+                i++;
+              }
+            break;
+            case 'c':
+              i = 0;
+              while (pdkim_combined_canons[i].str != NULL) {
+                if (strcmp(cur_val->str,pdkim_combined_canons[i].str) == 0 ) {
+                  sig->canon_headers = pdkim_combined_canons[i].canon_headers;
+                  sig->canon_body    = pdkim_combined_canons[i].canon_body;
+                  break;
+                }
+                i++;
+              }
+            break;
+            case 'q':
+              i = 0;
+              while (pdkim_querymethods[i] != NULL) {
+                if (strcmp(cur_val->str,pdkim_querymethods[i]) == 0 ) {
+                  sig->querymethod = i;
+                  break;
+                }
+                i++;
+              }
+            break;
+            case 's':
+              sig->selector = strdup(cur_val->str);
+            break;
+            case 'd':
+              sig->domain = strdup(cur_val->str);
+            break;
+            case 'i':
+              sig->identity = pdkim_decode_qp(cur_val->str);
+            break;
+            case 't':
+              sig->created = strtoul(cur_val->str,NULL,10);
+            break;
+            case 'x':
+              sig->expires = strtoul(cur_val->str,NULL,10);
+            break;
+            case 'l':
+              sig->bodylength = strtol(cur_val->str,NULL,10);
+            break;
+            case 'h':
+              sig->headernames = strdup(cur_val->str);
+            break;
+            case 'z':
+              sig->copiedheaders = pdkim_decode_qp(cur_val->str);
+            break;
+            default:
+              #ifdef PDKIM_DEBUG
+              if (ctx->debug_stream)
+                fprintf(ctx->debug_stream, "Unknown tag encountered\n");
+              #endif
+            break;
+          }
+        }
+        pdkim_strclear(cur_tag);
+        pdkim_strclear(cur_val);
+        in_b_val = 0;
+        where = PDKIM_HDR_LIMBO;
+        goto NEXT_CHAR;
+      }
+      else pdkim_strncat(cur_val,p,1);
+    }
+
+    NEXT_CHAR:
+    if (*p == '\0') break;
+
+    if (!in_b_val) {
+      *q = *p;
+      q++;
+    }
+    p++;
+  }
+
+  /* Make sure the most important bits are there. */
+  if (!(sig->domain      && (*(sig->domain)      != '\0') &&
+        sig->selector    && (*(sig->selector)    != '\0') &&
+        sig->headernames && (*(sig->headernames) != '\0') &&
+        sig->bodyhash    &&
+        sig->sigdata     &&
+        sig->version)) {
+    pdkim_free_sig(sig);
+    return NULL;
+  }
+
+  *q = '\0';
+  /* Chomp raw header. The final newline must not be added to the signature. */
+  q--;
+  while( (q > sig->rawsig_no_b_val) && ((*q == '\r') || (*q == '\n')) ) {
+    *q = '\0'; q--;
+  }
+
+  #ifdef PDKIM_DEBUG
+  if (ctx->debug_stream) {
+    fprintf(ctx->debug_stream,
+            "PDKIM >> Raw signature w/o b= tag value >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
+    pdkim_quoteprint(ctx->debug_stream,
+                     sig->rawsig_no_b_val,
+                     strlen(sig->rawsig_no_b_val), 1);
+    fprintf(ctx->debug_stream,
+            "PDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
+  }
+  #endif
+
+  sig->sha1_body = malloc(sizeof(sha1_context));
+  if (sig->sha1_body == NULL) {
+    pdkim_free_sig(sig);
+    return NULL;
+  }
+  sig->sha2_body = malloc(sizeof(sha2_context));
+  if (sig->sha2_body == NULL) {
+    pdkim_free_sig(sig);
+    return NULL;
+  }
+
+  sha1_starts(sig->sha1_body);
+  sha2_starts(sig->sha2_body,0);
+
+  return sig;
+}
+
+
+/* -------------------------------------------------------------------------- */
+pdkim_pubkey *pdkim_parse_pubkey_record(pdkim_ctx *ctx, char *raw_record) {
+  pdkim_pubkey *pub ;
+  char *p;
+  pdkim_str *cur_tag = NULL;
+  pdkim_str *cur_val = NULL;
+  int where = PDKIM_HDR_LIMBO;
+
+  pub = malloc(sizeof(pdkim_pubkey));
+  if (pub == NULL) return NULL;
+  memset(pub,0,sizeof(pdkim_pubkey));
+
+  p = raw_record;
+
+  while (1) {
+
+    /* Ignore FWS */
+    if ( (*p == '\r') || (*p == '\n') )
+      goto NEXT_CHAR;
+
+    if (where == PDKIM_HDR_LIMBO) {
+      /* In limbo, just wait for a tag-char to appear */
+      if (!((*p >= 'a') && (*p <= 'z')))
+        goto NEXT_CHAR;
+
+      where = PDKIM_HDR_TAG;
+    }
+
+    if (where == PDKIM_HDR_TAG) {
+      if (cur_tag == NULL)
+        cur_tag = pdkim_strnew(NULL);
+
+      if ((*p >= 'a') && (*p <= 'z'))
+        pdkim_strncat(cur_tag,p,1);
+
+      if (*p == '=') {
+        where = PDKIM_HDR_VALUE;
+        goto NEXT_CHAR;
+      }
+    }
+
+    if (where == PDKIM_HDR_VALUE) {
+      if (cur_val == NULL)
+        cur_val = pdkim_strnew(NULL);
+
+      if ( (*p == '\r') || (*p == '\n') )
+        goto NEXT_CHAR;
+
+      if ( (*p == ';') || (*p == '\0') ) {
+        if (cur_tag->len > 0) {
+          pdkim_strtrim(cur_val);
+          #ifdef PDKIM_DEBUG
+          if (ctx->debug_stream)
+            fprintf(ctx->debug_stream, "%s=%s\n", cur_tag->str, cur_val->str);
+          #endif
+          switch (cur_tag->str[0]) {
+            case 'v':
+              /* This tag isn't evaluated because:
+                 - We only support version DKIM1.
+                 - Which is the default for this value (set below)
+                 - Other versions are currently not specified.      */
+            break;
+            case 'h':
+              pub->hashes = strdup(cur_val->str);
+            break;
+            case 'g':
+              pub->granularity = strdup(cur_val->str);
+            break;
+            case 'n':
+              pub->notes = pdkim_decode_qp(cur_val->str);
+            break;
+            case 'p':
+              pub->key = pdkim_decode_base64(cur_val->str,&(pub->key_len));
+            break;
+            case 'k':
+              pub->hashes = strdup(cur_val->str);
+            break;
+            case 's':
+              pub->srvtype = strdup(cur_val->str);
+            break;
+            case 't':
+              if (strchr(cur_val->str,'y') != NULL) pub->testing = 1;
+              if (strchr(cur_val->str,'s') != NULL) pub->no_subdomaining = 1;
+            break;
+            default:
+              #ifdef PDKIM_DEBUG
+              if (ctx->debug_stream)
+                fprintf(ctx->debug_stream, "Unknown tag encountered\n");
+              #endif
+            break;
+          }
+        }
+        pdkim_strclear(cur_tag);
+        pdkim_strclear(cur_val);
+        where = PDKIM_HDR_LIMBO;
+        goto NEXT_CHAR;
+      }
+      else pdkim_strncat(cur_val,p,1);
+    }
+
+    NEXT_CHAR:
+    if (*p == '\0') break;
+    p++;
+  }
+
+  /* Set fallback defaults */
+  if (pub->version     == NULL) pub->version     = strdup(PDKIM_PUB_RECORD_VERSION);
+  if (pub->granularity == NULL) pub->granularity = strdup("*");
+  if (pub->keytype     == NULL) pub->keytype     = strdup("rsa");
+  if (pub->srvtype     == NULL) pub->srvtype     = strdup("*");
+
+  /* p= is required */
+  if (pub->key == NULL) {
+    pdkim_free_pubkey(pub);
+    return NULL;
+  }
+
+  return pub;
+}
+
+
+/* -------------------------------------------------------------------------- */
+int pdkim_update_bodyhash(pdkim_ctx *ctx, const char *data, int len) {
+  pdkim_signature *sig = ctx->sig;
+  /* Cache relaxed version of data */
+  char *relaxed_data = NULL;
+  int   relaxed_len  = 0;
+
+  /* Traverse all signatures, updating their hashes. */
+  while (sig != NULL) {
+    /* Defaults to simple canon (no further treatment necessary) */
+    const char *canon_data = data;
+    int         canon_len = len;
+
+    if (sig->canon_body == PDKIM_CANON_RELAXED) {
+      /* Relax the line if not done already */
+      if (relaxed_data == NULL) {
+        int seen_wsp = 0;
+        const char *p = data;
+        int q = 0;
+        relaxed_data = malloc(len+1);
+        if (relaxed_data == NULL) return PDKIM_ERR_OOM;
+        while (*p != '\0') {
+          char c = *p;
+          if (c == '\r') {
+            if ( (q > 0) && (relaxed_data[q-1] == ' ') ) q--;
+          }
+          else if ( (c == '\t') || (c == ' ') ) {
+            c = ' '; /* Turns WSP into SP */
+            if (seen_wsp) {
+              p++;
+              continue;
+            }
+            else seen_wsp = 1;
+          }
+          else seen_wsp = 0;
+          relaxed_data[q++] = c;
+          p++;
+        }
+        relaxed_data[q] = '\0';
+        relaxed_len = q;
+      }
+      canon_data = relaxed_data;
+      canon_len  = relaxed_len;
+    }
+
+    /* Make sure we don't exceed the to-be-signed body length */
+    if ((sig->bodylength >= 0) &&
+        ((sig->signed_body_bytes+(unsigned long)canon_len) > sig->bodylength))
+      canon_len = (sig->bodylength - sig->signed_body_bytes);
+
+    if (canon_len > 0) {
+      if (sig->algo == PDKIM_ALGO_RSA_SHA1)
+        sha1_update(sig->sha1_body,(unsigned char *)canon_data,canon_len);
+      else
+        sha2_update(sig->sha2_body,(unsigned char *)canon_data,canon_len);
+      sig->signed_body_bytes += canon_len;
+#ifdef PDKIM_DEBUG
+      if (ctx->debug_stream!=NULL)
+        pdkim_quoteprint(ctx->debug_stream,canon_data,canon_len,0);
+#endif
+    }
+
+    sig = sig->next;
+  }
+
+  if (relaxed_data != NULL) free(relaxed_data);
+  return PDKIM_OK;
+}
+
+
+/* -------------------------------------------------------------------------- */
+int pdkim_finish_bodyhash(pdkim_ctx *ctx) {
+  pdkim_signature *sig = ctx->sig;
+
+  /* Traverse all signatures */
+  while (sig != NULL) {
+
+    /* Finish hashes */
+    unsigned char bh[32]; /* SHA-256 = 32 Bytes,  SHA-1 = 20 Bytes */
+    if (sig->algo == PDKIM_ALGO_RSA_SHA1)
+      sha1_finish(sig->sha1_body,bh);
+    else
+      sha2_finish(sig->sha2_body,bh);
+
+    #ifdef PDKIM_DEBUG
+    if (ctx->debug_stream) {
+      fprintf(ctx->debug_stream, "PDKIM [%s] Body bytes hashed: %lu\n",
+        sig->domain, sig->signed_body_bytes);
+      fprintf(ctx->debug_stream, "PDKIM [%s] bh  computed: ", sig->domain);
+      pdkim_hexprint(ctx->debug_stream, (char *)bh,
+                     (sig->algo == PDKIM_ALGO_RSA_SHA1)?20:32,1);
+    }
+    #endif
+
+    /* SIGNING -------------------------------------------------------------- */
+    if (ctx->mode == PDKIM_MODE_SIGN) {
+      sig->bodyhash_len = (sig->algo == PDKIM_ALGO_RSA_SHA1)?20:32;
+      sig->bodyhash = malloc(sig->bodyhash_len);
+      if (sig->bodyhash == NULL) return PDKIM_ERR_OOM;
+      memcpy(sig->bodyhash,bh,sig->bodyhash_len);
+
+      /* If bodylength limit is set, and we have received less bytes
+         than the requested amount, effectively remove the limit tag. */
+      if (sig->signed_body_bytes < sig->bodylength) sig->bodylength = -1;
+    }
+    /* VERIFICATION --------------------------------------------------------- */
+    else {
+      /* Compare bodyhash */
+      if (memcmp(bh,sig->bodyhash,
+                 (sig->algo == PDKIM_ALGO_RSA_SHA1)?20:32) == 0) {
+        #ifdef PDKIM_DEBUG
+        if (ctx->debug_stream)
+          fprintf(ctx->debug_stream, "PDKIM [%s] Body hash verified OK\n",
+                  sig->domain);
+        #endif
+      }
+      else {
+        #ifdef PDKIM_DEBUG
+        if (ctx->debug_stream) {
+          fprintf(ctx->debug_stream, "PDKIM [%s] Body hash did NOT verify\n",
+                  sig->domain);
+          fprintf(ctx->debug_stream, "PDKIM [%s] bh signature: ", sig->domain);
+          pdkim_hexprint(ctx->debug_stream, sig->bodyhash,
+                           (sig->algo == PDKIM_ALGO_RSA_SHA1)?20:32,1);
+        }
+        #endif
+        sig->verify_status     = PDKIM_VERIFY_FAIL;
+        sig->verify_ext_status = PDKIM_VERIFY_FAIL_BODY;
+      }
+    }
+
+    sig = sig->next;
+  }
+
+  return PDKIM_OK;
+}
+
+
+
+/* -------------------------------------------------------------------------- */
+/* Callback from pdkim_feed below for processing complete body lines */
+int pdkim_bodyline_complete(pdkim_ctx *ctx) {
+  char *p = ctx->linebuf;
+  int   n = ctx->linebuf_offset;
+
+  /* Ignore extra data if we've seen the end-of-data marker */
+  if (ctx->seen_eod) goto BAIL;
+
+  /* We've always got one extra byte to stuff a zero ... */
+  ctx->linebuf[(ctx->linebuf_offset)] = '\0';
+
+  if (ctx->input_mode == PDKIM_INPUT_SMTP) {
+    /* Terminate on EOD marker */
+    if (memcmp(p,".\r\n",3) == 0) {
+      ctx->seen_eod = 1;
+      goto BAIL;
+    }
+    /* Unstuff dots */
+    if (memcmp(p,"..",2) == 0) {
+      p++;
+      n--;
+    }
+  }
+
+  /* Empty lines need to be buffered until we find a non-empty line */
+  if (memcmp(p,"\r\n",2) == 0) {
+    ctx->num_buffered_crlf++;
+    goto BAIL;
+  }
+
+  /* At this point, we have a non-empty line, so release the buffered ones. */
+  while (ctx->num_buffered_crlf) {
+    pdkim_update_bodyhash(ctx,"\r\n",2);
+    ctx->num_buffered_crlf--;
+  }
+
+  pdkim_update_bodyhash(ctx,p,n);
+
+  BAIL:
+  ctx->linebuf_offset = 0;
+  return PDKIM_OK;
+}
+
+
+/* -------------------------------------------------------------------------- */
+/* Callback from pdkim_feed below for processing complete headers */
+#define DKIM_SIGNATURE_HEADERNAME "DKIM-Signature:"
+int pdkim_header_complete(pdkim_ctx *ctx) {
+  pdkim_signature *sig = ctx->sig;
+
+  /* Special case: The last header can have an extra \r appended */
+  if ( (ctx->cur_header->len > 1) &&
+       (ctx->cur_header->str[(ctx->cur_header->len)-1] == '\r') ) {
+    ctx->cur_header->str[(ctx->cur_header->len)-1] = '\0';
+    ctx->cur_header->len--;
+  }
+
+  ctx->num_headers++;
+  if (ctx->num_headers > PDKIM_MAX_HEADERS) goto BAIL;
+
+  /* SIGNING -------------------------------------------------------------- */
+  if (ctx->mode == PDKIM_MODE_SIGN) {
+    /* Traverse all signatures */
+    while (sig != NULL) {
+      pdkim_stringlist *list;
+
+      if (header_name_match(ctx->cur_header->str,
+                            sig->sign_headers?
+                              sig->sign_headers:
+                              PDKIM_DEFAULT_SIGN_HEADERS, 0) != PDKIM_OK) goto NEXT_SIG;
+
+      /* Add header to the signed headers list (in reverse order) */
+      list = pdkim_prepend_stringlist(sig->headers,
+                                      ctx->cur_header->str);
+      if (list == NULL) return PDKIM_ERR_OOM;
+      sig->headers = list;
+  
+      NEXT_SIG:
+      sig = sig->next;
+    }
+  }
+
+  /* DKIM-Signature: headers are added to the verification list */
+  if (ctx->mode == PDKIM_MODE_VERIFY) {
+    if (strncasecmp(ctx->cur_header->str,
+                    DKIM_SIGNATURE_HEADERNAME,
+                    strlen(DKIM_SIGNATURE_HEADERNAME)) == 0) {
+      pdkim_signature *new_sig;
+      /* Create and chain new signature block */
+      #ifdef PDKIM_DEBUG
+      if (ctx->debug_stream)
+        fprintf(ctx->debug_stream,
+          "PDKIM >> Found sig, trying to parse >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
+      #endif
+      new_sig = pdkim_parse_sig_header(ctx, ctx->cur_header->str);
+      if (new_sig != NULL) {
+        pdkim_signature *last_sig = ctx->sig;
+        if (last_sig == NULL) {
+          ctx->sig = new_sig;
+        }
+        else {
+          while (last_sig->next != NULL) { last_sig = last_sig->next; }
+          last_sig->next = new_sig;
+        }
+      }
+      else {
+        #ifdef PDKIM_DEBUG
+        if (ctx->debug_stream) {
+          fprintf(ctx->debug_stream,"Error while parsing signature header\n");
+          fprintf(ctx->debug_stream,
+            "PDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
+        }
+        #endif
+      }
+    }
+    /* every other header is stored for signature verification */
+    else {
+      pdkim_stringlist *list;
+
+      list = pdkim_prepend_stringlist(ctx->headers,
+                                      ctx->cur_header->str);
+      if (list == NULL) return PDKIM_ERR_OOM;
+      ctx->headers = list;
+    }
+  }
+
+  BAIL:
+  pdkim_strclear(ctx->cur_header); /* Re-use existing pdkim_str */
+  return PDKIM_OK;
+}
+
+
+
+/* -------------------------------------------------------------------------- */
+#define HEADER_BUFFER_FRAG_SIZE 256
+DLLEXPORT int pdkim_feed (pdkim_ctx *ctx,
+                char *data,
+                int   len) {
+  int p;
+  for (p=0;p<len;p++) {
+    char c = data[p];
+    if (ctx->past_headers) {
+      /* Processing body byte */
+      ctx->linebuf[(ctx->linebuf_offset)++] = c;
+      if (c == '\n') {
+        int rc = pdkim_bodyline_complete(ctx); /* End of line */
+        if (rc != PDKIM_OK) return rc;
+      }
+      if (ctx->linebuf_offset == (PDKIM_MAX_BODY_LINE_LEN-1))
+        return PDKIM_ERR_LONG_LINE;
+    }
+    else {
+      /* Processing header byte */
+      if (c != '\r') {
+        if (c == '\n') {
+          if (ctx->seen_lf) {
+            int rc = pdkim_header_complete(ctx); /* Seen last header line */
+            if (rc != PDKIM_OK) return rc;
+            ctx->past_headers = 1;
+            ctx->seen_lf = 0;
+#ifdef PDKIM_DEBUG
+            if (ctx->debug_stream)
+              fprintf(ctx->debug_stream,
+                "PDKIM >> Hashed body data, canonicalized >>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
+#endif
+            continue;
+          }
+          else ctx->seen_lf = 1;
+        }
+        else if (ctx->seen_lf) {
+          if (! ((c == '\t') || (c == ' '))) {
+            int rc = pdkim_header_complete(ctx); /* End of header */
+            if (rc != PDKIM_OK) return rc;
+          }
+          ctx->seen_lf = 0;
+        }
+      }
+      if (ctx->cur_header == NULL) {
+        ctx->cur_header = pdkim_strnew(NULL);
+        if (ctx->cur_header == NULL) return PDKIM_ERR_OOM;
+      }
+      if (ctx->cur_header->len < PDKIM_MAX_HEADER_LEN)
+        if (pdkim_strncat(ctx->cur_header,&data[p],1) == NULL)
+          return PDKIM_ERR_OOM;
+    }
+  }
+  return PDKIM_OK;
+}
+
+
+/* -------------------------------------------------------------------------- */
+char *pdkim_create_header(pdkim_signature *sig, int final) {
+  char *rc = NULL;
+  char *base64_bh = NULL;
+  char *base64_b  = NULL;
+  pdkim_str *hdr = pdkim_strnew("DKIM-Signature: v="PDKIM_SIGNATURE_VERSION);
+  if (hdr == NULL) return NULL;
+
+  base64_bh = pdkim_encode_base64(sig->bodyhash, sig->bodyhash_len);
+  if (base64_bh == NULL) goto BAIL;
+
+  /* Required and static bits */
+  if (
+        pdkim_strcat(hdr,"; a=")                                &&
+        pdkim_strcat(hdr,pdkim_algos[sig->algo])                &&
+        pdkim_strcat(hdr,"; q=")                                &&
+        pdkim_strcat(hdr,pdkim_querymethods[sig->querymethod])  &&
+        pdkim_strcat(hdr,"; c=")                                &&
+        pdkim_strcat(hdr,pdkim_canons[sig->canon_headers])      &&
+        pdkim_strcat(hdr,"/")                                   &&
+        pdkim_strcat(hdr,pdkim_canons[sig->canon_body])         &&
+        pdkim_strcat(hdr,"; d=")                                &&
+        pdkim_strcat(hdr,sig->domain)                           &&
+        pdkim_strcat(hdr,"; s=")                                &&
+        pdkim_strcat(hdr,sig->selector)                         &&
+        pdkim_strcat(hdr,";\r\n\th=")                           &&
+        pdkim_strcat(hdr,sig->headernames)                      &&
+        pdkim_strcat(hdr,"; bh=")                               &&
+        pdkim_strcat(hdr,base64_bh)                             &&
+        pdkim_strcat(hdr,";\r\n\t")
+     ) {
+    /* Optional bits */
+    if (sig->identity != NULL) {
+      if (!( pdkim_strcat(hdr,"i=")                             &&
+             pdkim_strcat(hdr,sig->identity)                    &&
+             pdkim_strcat(hdr,";") ) ) {
+        goto BAIL;
+      }
+    }
+    if (sig->created > 0) {
+      if (!( pdkim_strcat(hdr,"t=")                             &&
+             pdkim_numcat(hdr,sig->created)                     &&
+             pdkim_strcat(hdr,";") ) ) {
+        goto BAIL;
+      }
+    }
+    if (sig->expires > 0) {
+      if (!( pdkim_strcat(hdr,"x=")                             &&
+             pdkim_numcat(hdr,sig->expires)                     &&
+             pdkim_strcat(hdr,";") ) ) {
+        goto BAIL;
+      }
+    }
+    if (sig->bodylength >= 0) {
+      if (!( pdkim_strcat(hdr,"l=")                             &&
+             pdkim_numcat(hdr,sig->bodylength)                  &&
+             pdkim_strcat(hdr,";") ) ) {
+        goto BAIL;
+      }
+    }
+    /* Extra linebreak */
+    if (hdr->str[(hdr->len)-1] == ';') {
+      if (!pdkim_strcat(hdr," \r\n\t")) goto BAIL;
+    }
+    /* Preliminary or final version? */
+    if (final) {
+      base64_b = pdkim_encode_base64(sig->sigdata, sig->sigdata_len);
+      if (base64_b == NULL) goto BAIL;
+      if (
+            pdkim_strcat(hdr,"b=")                              &&
+            pdkim_strcat(hdr,base64_b)                          &&
+            pdkim_strcat(hdr,";")
+         ) goto DONE;
+    }
+    else {
+      if (pdkim_strcat(hdr,"b=;")) goto DONE;
+    }
+
+    goto BAIL;
+  }
+
+  DONE:
+  rc = strdup(hdr->str);
+
+  BAIL:
+  pdkim_strfree(hdr);
+  if (base64_bh != NULL) free(base64_bh);
+  if (base64_b  != NULL) free(base64_b);
+  return rc;
+}
+
+
+/* -------------------------------------------------------------------------- */
+DLLEXPORT int pdkim_feed_finish(pdkim_ctx *ctx, pdkim_signature **return_signatures) {
+  pdkim_signature *sig = ctx->sig;
+  pdkim_str *headernames = NULL;             /* Collected signed header names */
+
+  /* Check if we must still flush a (partial) header. If that is the
+     case, the message has no body, and we must compute a body hash
+     out of '<CR><LF>' */
+  if (ctx->cur_header && ctx->cur_header->len) {
+    int rc = pdkim_header_complete(ctx);
+    if (rc != PDKIM_OK) return rc;
+    pdkim_update_bodyhash(ctx,"\r\n",2);
+  }
+  else {
+    /* For non-smtp input, check if there's an unfinished line in the
+       body line buffer. If that is the case, we must add a CRLF to the
+       hash to properly terminate the message. */
+    if ((ctx->input_mode == PDKIM_INPUT_NORMAL) && ctx->linebuf_offset) {
+      pdkim_update_bodyhash(ctx, ctx->linebuf, ctx->linebuf_offset);
+      pdkim_update_bodyhash(ctx,"\r\n",2);
+    }
+    #ifdef PDKIM_DEBUG
+    if (ctx->debug_stream)
+      fprintf(ctx->debug_stream,
+        "PDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
+    #endif
+  }
+
+  /* Build (and/or evaluate) body hash */
+  if (pdkim_finish_bodyhash(ctx) != PDKIM_OK) return PDKIM_ERR_OOM;
+
+  /* SIGNING -------------------------------------------------------------- */
+  if (ctx->mode == PDKIM_MODE_SIGN) {
+    headernames = pdkim_strnew(NULL);
+    if (headernames == NULL) return PDKIM_ERR_OOM;
+  }
+  /* ---------------------------------------------------------------------- */
+
+  while (sig != NULL) {
+    sha1_context sha1_headers;
+    sha2_context sha2_headers;
+    char *sig_hdr;
+    char headerhash[32];
+
+    if (sig->algo == PDKIM_ALGO_RSA_SHA1)
+      sha1_starts(&sha1_headers);
+    else
+      sha2_starts(&sha2_headers,0);
+
+    #ifdef PDKIM_DEBUG
+    if (ctx->debug_stream)
+      fprintf(ctx->debug_stream,
+              "PDKIM >> Hashed header data, canonicalized, in sequence >>>>>>>>>>>>>>\n");
+    #endif
+
+    /* SIGNING ---------------------------------------------------------------- */
+    /* When signing, walk through our header list and add them to the hash. As we
+       go, construct a list of the header's names to use for the h= parameter. */
+    if (ctx->mode == PDKIM_MODE_SIGN) {
+      pdkim_stringlist *p = sig->headers;
+      while (p != NULL) {
+        char *rh = NULL;
+        /* Collect header names (Note: colon presence is guaranteed here) */
+        char *q = strchr(p->value,':');
+        if (pdkim_strncat(headernames, p->value,
+                          (q-(p->value))+((p->next==NULL)?0:1)) == NULL)
+          return PDKIM_ERR_OOM;
+
+        if (sig->canon_headers == PDKIM_CANON_RELAXED)
+          rh = pdkim_relax_header(p->value,1); /* cook header for relaxed canon */
+        else
+          rh = strdup(p->value);               /* just copy it for simple canon */
+
+        if (rh == NULL) return PDKIM_ERR_OOM;
+
+        /* Feed header to the hash algorithm */
+        if (sig->algo == PDKIM_ALGO_RSA_SHA1)
+          sha1_update(&(sha1_headers),(unsigned char *)rh,strlen(rh));
+        else
+          sha2_update(&(sha2_headers),(unsigned char *)rh,strlen(rh));
+        #ifdef PDKIM_DEBUG
+        if (ctx->debug_stream)
+          pdkim_quoteprint(ctx->debug_stream, rh, strlen(rh), 1);
+        #endif
+        free(rh);
+        p = p->next;
+      }
+    }
+    /* VERIFICATION ----------------------------------------------------------- */
+    /* When verifying, walk through the header name list in the h= parameter and
+       add the headers to the hash in that order. */
+    else {
+      char *b = strdup(sig->headernames);
+      char *p = b;
+      char *q = NULL;
+      pdkim_stringlist *hdrs = ctx->headers;
+
+      if (b == NULL) return PDKIM_ERR_OOM;
+
+      /* clear tags */
+      while (hdrs != NULL) {
+        hdrs->tag = 0;
+        hdrs = hdrs->next;
+      }
+
+      while(1) {
+        hdrs = ctx->headers;
+        q = strchr(p,':');
+        if (q != NULL) *q = '\0';
+        while (hdrs != NULL) {
+          if ( (hdrs->tag == 0) &&
+               (strncasecmp(hdrs->value,p,strlen(p)) == 0) &&
+               ((hdrs->value)[strlen(p)] == ':') ) {
+            char *rh = NULL;
+            if (sig->canon_headers == PDKIM_CANON_RELAXED)
+              rh = pdkim_relax_header(hdrs->value,1); /* cook header for relaxed canon */
+            else
+              rh = strdup(hdrs->value);               /* just copy it for simple canon */
+            if (rh == NULL) return PDKIM_ERR_OOM;
+            /* Feed header to the hash algorithm */
+            if (sig->algo == PDKIM_ALGO_RSA_SHA1)
+              sha1_update(&(sha1_headers),(unsigned char *)rh,strlen(rh));
+            else
+              sha2_update(&(sha2_headers),(unsigned char *)rh,strlen(rh));
+            #ifdef PDKIM_DEBUG
+            if (ctx->debug_stream)
+              pdkim_quoteprint(ctx->debug_stream, rh, strlen(rh), 1);
+            #endif
+            free(rh);
+            hdrs->tag = 1;
+            break;
+          }
+          hdrs = hdrs->next;
+        }
+        if (q == NULL) break;
+        p = q+1;
+      }
+      free(b);
+    }
+
+    #ifdef PDKIM_DEBUG
+    if (ctx->debug_stream)
+      fprintf(ctx->debug_stream,
+              "PDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
+    #endif
+
+    /* SIGNING ---------------------------------------------------------------- */
+    if (ctx->mode == PDKIM_MODE_SIGN) {
+      /* Copy headernames to signature struct */
+      sig->headernames = strdup(headernames->str);
+      pdkim_strfree(headernames);
+
+      /* Create signature header with b= omitted */
+      sig_hdr = pdkim_create_header(ctx->sig,0);
+    }
+    /* VERIFICATION ----------------------------------------------------------- */
+    else {
+      sig_hdr = strdup(sig->rawsig_no_b_val);
+    }
+    /* ------------------------------------------------------------------------ */
+
+    if (sig_hdr == NULL) return PDKIM_ERR_OOM;
+
+    /* Relax header if necessary */
+    if (sig->canon_headers == PDKIM_CANON_RELAXED) {
+      char *relaxed_hdr = pdkim_relax_header(sig_hdr,0);
+      free(sig_hdr);
+      if (relaxed_hdr == NULL) return PDKIM_ERR_OOM;
+      sig_hdr = relaxed_hdr;
+    }
+
+    #ifdef PDKIM_DEBUG
+    if (ctx->debug_stream) {
+      fprintf(ctx->debug_stream,
+              "PDKIM >> Signed DKIM-Signature header, canonicalized >>>>>>>>>>>>>>>>>\n");
+      pdkim_quoteprint(ctx->debug_stream, sig_hdr, strlen(sig_hdr), 1);
+      fprintf(ctx->debug_stream,
+              "PDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
+    }
+    #endif
+
+    /* Finalize header hash */
+    if (sig->algo == PDKIM_ALGO_RSA_SHA1) {
+      sha1_update(&(sha1_headers),(unsigned char *)sig_hdr,strlen(sig_hdr));
+      sha1_finish(&(sha1_headers),(unsigned char *)headerhash);
+      #ifdef PDKIM_DEBUG
+      if (ctx->debug_stream) {
+        fprintf(ctx->debug_stream, "PDKIM [%s] hh computed: ", sig->domain);
+        pdkim_hexprint(ctx->debug_stream, headerhash, 20, 1);
+      }
+      #endif
+    }
+    else {
+      sha2_update(&(sha2_headers),(unsigned char *)sig_hdr,strlen(sig_hdr));
+      sha2_finish(&(sha2_headers),(unsigned char *)headerhash);
+      #ifdef PDKIM_DEBUG
+      if (ctx->debug_stream) {
+        fprintf(ctx->debug_stream, "PDKIM [%s] hh computed: ", sig->domain);
+        pdkim_hexprint(ctx->debug_stream, headerhash, 32, 1);
+      }
+      #endif
+    }
+
+    free(sig_hdr);
+
+    /* SIGNING ---------------------------------------------------------------- */
+    if (ctx->mode == PDKIM_MODE_SIGN) {
+      rsa_context rsa;
+
+      rsa_init(&rsa,RSA_PKCS_V15,0);
+
+      /* Perform private key operation */
+      if (rsa_parse_key(&rsa, (unsigned char *)sig->rsa_privkey,
+                        strlen(sig->rsa_privkey), NULL, 0) != 0) {
+        return PDKIM_ERR_RSA_PRIVKEY;
+      }
+
+      sig->sigdata_len = mpi_size(&(rsa.N));
+      sig->sigdata = malloc(sig->sigdata_len);
+      if (sig->sigdata == NULL) return PDKIM_ERR_OOM;
+
+      if (rsa_pkcs1_sign( &rsa, RSA_PRIVATE,
+                          ((sig->algo == PDKIM_ALGO_RSA_SHA1)?
+                             SIG_RSA_SHA1:SIG_RSA_SHA256),
+                          0,
+                          (unsigned char *)headerhash,
+                          (unsigned char *)sig->sigdata ) != 0) {
+        return PDKIM_ERR_RSA_SIGNING;
+      }
+
+      rsa_free(&rsa);
+
+      #ifdef PDKIM_DEBUG
+      if (ctx->debug_stream) {
+        fprintf(ctx->debug_stream, "PDKIM [%s] b computed: ",
+                sig->domain);
+        pdkim_hexprint(ctx->debug_stream, sig->sigdata, sig->sigdata_len, 1);
+      }
+      #endif
+
+      sig->signature_header = pdkim_create_header(ctx->sig,1);
+      if (sig->signature_header == NULL) return PDKIM_ERR_OOM;
+    }
+    /* VERIFICATION ----------------------------------------------------------- */
+    else {
+      rsa_context rsa;
+      char *dns_txt_name, *dns_txt_reply;
+
+      rsa_init(&rsa,RSA_PKCS_V15,0);
+
+      dns_txt_name  = malloc(PDKIM_DNS_TXT_MAX_NAMELEN);
+      if (dns_txt_name == NULL) return PDKIM_ERR_OOM;
+      dns_txt_reply = malloc(PDKIM_DNS_TXT_MAX_RECLEN);
+      if (dns_txt_reply == NULL) {
+        free(dns_txt_name);
+        return PDKIM_ERR_OOM;
+      }
+      memset(dns_txt_reply,0,PDKIM_DNS_TXT_MAX_RECLEN);
+      memset(dns_txt_name ,0,PDKIM_DNS_TXT_MAX_NAMELEN);
+
+      if (snprintf(dns_txt_name,PDKIM_DNS_TXT_MAX_NAMELEN,
+                   "%s._domainkey.%s.",
+                   sig->selector,sig->domain) >= PDKIM_DNS_TXT_MAX_NAMELEN) {
+        sig->verify_status =      PDKIM_VERIFY_INVALID;
+        sig->verify_ext_status =  PDKIM_VERIFY_INVALID_BUFFER_SIZE;
+        goto NEXT_VERIFY;
+      }
+
+      if ((ctx->dns_txt_callback(dns_txt_name, dns_txt_reply) != PDKIM_OK) ||
+          (dns_txt_reply[0] == '\0')) {
+        sig->verify_status =      PDKIM_VERIFY_INVALID;
+        sig->verify_ext_status =  PDKIM_VERIFY_INVALID_PUBKEY_UNAVAILABLE;
+        goto NEXT_VERIFY;
+      }
+
+      #ifdef PDKIM_DEBUG
+      if (ctx->debug_stream) {
+        fprintf(ctx->debug_stream,
+                "PDKIM >> Parsing public key record >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
+        fprintf(ctx->debug_stream,"Raw record: ");
+        pdkim_quoteprint(ctx->debug_stream, dns_txt_reply, strlen(dns_txt_reply), 1);
+      }
+      #endif
+
+      sig->pubkey = pdkim_parse_pubkey_record(ctx,dns_txt_reply);
+      if (sig->pubkey == NULL) {
+        sig->verify_status =      PDKIM_VERIFY_INVALID;
+        sig->verify_ext_status =  PDKIM_VERIFY_INVALID_PUBKEY_PARSING;
+        #ifdef PDKIM_DEBUG
+        if (ctx->debug_stream) {
+          fprintf(ctx->debug_stream,"Error while parsing public key record\n");
+          fprintf(ctx->debug_stream,
+            "PDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
+        }
+        #endif
+        goto NEXT_VERIFY;
+      }
+
+      #ifdef PDKIM_DEBUG
+      if (ctx->debug_stream) {
+        fprintf(ctx->debug_stream,
+          "PDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
+      }
+      #endif
+
+      if (rsa_parse_public_key(&rsa,
+                              (unsigned char *)sig->pubkey->key,
+                               sig->pubkey->key_len) != 0) {
+        sig->verify_status =      PDKIM_VERIFY_INVALID;
+        sig->verify_ext_status =  PDKIM_VERIFY_INVALID_PUBKEY_PARSING;
+        goto NEXT_VERIFY;
+      }
+
+      /* Check the signature */
+      if (rsa_pkcs1_verify(&rsa,
+                        RSA_PUBLIC,
+                        ((sig->algo == PDKIM_ALGO_RSA_SHA1)?
+                             SIG_RSA_SHA1:SIG_RSA_SHA256),
+                        0,
+                        (unsigned char *)headerhash,
+                        (unsigned char *)sig->sigdata) != 0) {
+        sig->verify_status =      PDKIM_VERIFY_FAIL;
+        sig->verify_ext_status =  PDKIM_VERIFY_FAIL_MESSAGE;
+        goto NEXT_VERIFY;
+      }
+
+      /* We have a winner! (if bodydhash was correct earlier) */
+      if (sig->verify_status == PDKIM_VERIFY_NONE) {
+        sig->verify_status = PDKIM_VERIFY_PASS;
+      }
+
+      NEXT_VERIFY:
+
+      #ifdef PDKIM_DEBUG
+      if (ctx->debug_stream) {
+        fprintf(ctx->debug_stream, "PDKIM [%s] signature status: %s",
+                sig->domain, pdkim_verify_status_str(sig->verify_status));
+        if (sig->verify_ext_status > 0) {
+          fprintf(ctx->debug_stream, " (%s)\n",
+                  pdkim_verify_ext_status_str(sig->verify_ext_status));
+        }
+        else {
+          fprintf(ctx->debug_stream, "\n");
+        }
+      }
+      #endif
+
+      rsa_free(&rsa);
+      free(dns_txt_name);
+      free(dns_txt_reply);
+    }
+
+    sig = sig->next;
+  }
+
+  /* If requested, set return pointer to signature(s) */
+  if (return_signatures != NULL) {
+    *return_signatures = ctx->sig;
+  }
+
+  return PDKIM_OK;
+}
+
+
+/* -------------------------------------------------------------------------- */
+DLLEXPORT pdkim_ctx *pdkim_init_verify(int input_mode,
+                             int(*dns_txt_callback)(char *, char *)
+                             ) {
+  pdkim_ctx *ctx = malloc(sizeof(pdkim_ctx));
+  if (ctx == NULL) return NULL;
+  memset(ctx,0,sizeof(pdkim_ctx));
+
+  ctx->linebuf = malloc(PDKIM_MAX_BODY_LINE_LEN);
+  if (ctx->linebuf == NULL) {
+    free(ctx);
+    return NULL;
+  }
+
+  ctx->mode = PDKIM_MODE_VERIFY;
+  ctx->input_mode = input_mode;
+  ctx->dns_txt_callback = dns_txt_callback;
+
+  return ctx;
+}
+
+
+/* -------------------------------------------------------------------------- */
+DLLEXPORT pdkim_ctx *pdkim_init_sign(int input_mode,
+                           char *domain,
+                           char *selector,
+                           char *rsa_privkey) {
+  pdkim_ctx *ctx;
+  pdkim_signature *sig;
+
+  if (!domain || !selector || !rsa_privkey) return NULL;
+
+  ctx = malloc(sizeof(pdkim_ctx));
+  if (ctx == NULL) return NULL;
+  memset(ctx,0,sizeof(pdkim_ctx));
+
+  ctx->linebuf = malloc(PDKIM_MAX_BODY_LINE_LEN);
+  if (ctx->linebuf == NULL) {
+    free(ctx);
+    return NULL;
+  }
+
+  sig = malloc(sizeof(pdkim_signature));
+  if (sig == NULL) {
+    free(ctx->linebuf);
+    free(ctx);
+    return NULL;
+  }
+  memset(sig,0,sizeof(pdkim_signature));
+  sig->bodylength = -1;
+
+  ctx->mode = PDKIM_MODE_SIGN;
+  ctx->input_mode = input_mode;
+  ctx->sig = sig;
+
+  ctx->sig->domain = strdup(domain);
+  ctx->sig->selector = strdup(selector);
+  ctx->sig->rsa_privkey = strdup(rsa_privkey);
+
+  if (!ctx->sig->domain || !ctx->sig->selector || !ctx->sig->rsa_privkey) {
+    pdkim_free_ctx(ctx);
+    return NULL;
+  }
+
+  ctx->sig->sha1_body = malloc(sizeof(sha1_context));
+  if (ctx->sig->sha1_body == NULL) {
+    pdkim_free_ctx(ctx);
+    return NULL;
+  }
+  sha1_starts(ctx->sig->sha1_body);
+
+  ctx->sig->sha2_body = malloc(sizeof(sha2_context));
+  if (ctx->sig->sha2_body == NULL) {
+    pdkim_free_ctx(ctx);
+    return NULL;
+  }
+  sha2_starts(ctx->sig->sha2_body,0);
+
+  return ctx;
+}
+
+#ifdef PDKIM_DEBUG
+/* -------------------------------------------------------------------------- */
+DLLEXPORT void pdkim_set_debug_stream(pdkim_ctx *ctx,
+                            FILE *debug_stream) {
+  ctx->debug_stream = debug_stream;
+}
+#endif
+
+/* -------------------------------------------------------------------------- */
+DLLEXPORT int pdkim_set_optional(pdkim_ctx *ctx,
+                       char *sign_headers,
+                       char *identity,
+                       int canon_headers,
+                       int canon_body,
+                       long bodylength,
+                       int algo,
+                       unsigned long created,
+                       unsigned long expires) {
+
+  if (identity != NULL) {
+    ctx->sig->identity = strdup(identity);
+    if (ctx->sig->identity == NULL) return PDKIM_ERR_OOM;
+  }
+
+  if (sign_headers != NULL) {
+    ctx->sig->sign_headers = strdup(sign_headers);
+    if (ctx->sig->sign_headers == NULL) return PDKIM_ERR_OOM;
+  }
+
+  ctx->sig->canon_headers = canon_headers;
+  ctx->sig->canon_body = canon_body;
+  ctx->sig->bodylength = bodylength;
+  ctx->sig->algo = algo;
+  ctx->sig->created = created;
+  ctx->sig->expires = expires;
+
+  return PDKIM_OK;
+}
diff --git a/src/pdkim/pdkim.h b/src/pdkim/pdkim.h
new file mode 100644 (file)
index 0000000..1d364a3
--- /dev/null
@@ -0,0 +1,320 @@
+/*
+ *  PDKIM - a RFC4871 (DKIM) implementation
+ *
+ *  Copyright (C) 2009 - 2012  Tom Kistner <tom@duncanthrax.net>
+ *
+ *  http://duncanthrax.net/pdkim/
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License along
+ *  with this program; if not, write to the Free Software Foundation, Inc.,
+ *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+/* -------------------------------------------------------------------------- */
+/* Debugging. This can also be enabled/disabled at run-time. I recommend to
+   leave it defined. */
+#define PDKIM_DEBUG
+
+/* -------------------------------------------------------------------------- */
+/* Length of the preallocated buffer for the "answer" from the dns/txt
+   callback function. This should match the maximum RDLENGTH from DNS. */
+#define PDKIM_DNS_TXT_MAX_RECLEN    (1 << 16)
+
+/* -------------------------------------------------------------------------- */
+/* Function success / error codes */
+#define PDKIM_OK                      0
+#define PDKIM_FAIL                   -1
+#define PDKIM_ERR_OOM              -100
+#define PDKIM_ERR_RSA_PRIVKEY      -101
+#define PDKIM_ERR_RSA_SIGNING      -102
+#define PDKIM_ERR_LONG_LINE        -103
+#define PDKIM_ERR_BUFFER_TOO_SMALL -104
+
+/* -------------------------------------------------------------------------- */
+/* Main/Extended verification status */
+#define PDKIM_VERIFY_NONE      0
+#define PDKIM_VERIFY_INVALID   1
+#define PDKIM_VERIFY_FAIL      2
+#define PDKIM_VERIFY_PASS      3
+
+#define PDKIM_VERIFY_FAIL_BODY                  1
+#define PDKIM_VERIFY_FAIL_MESSAGE               2
+#define PDKIM_VERIFY_INVALID_PUBKEY_UNAVAILABLE 3
+#define PDKIM_VERIFY_INVALID_BUFFER_SIZE        4
+#define PDKIM_VERIFY_INVALID_PUBKEY_PARSING     5
+
+/* -------------------------------------------------------------------------- */
+/* Some parameter values */
+#define PDKIM_QUERYMETHOD_DNS_TXT 0
+
+#define PDKIM_ALGO_RSA_SHA256     0
+#define PDKIM_ALGO_RSA_SHA1       1
+
+#define PDKIM_CANON_SIMPLE        0
+#define PDKIM_CANON_RELAXED       1
+
+#define PDKIM_HASH_SHA256         0
+#define PDKIM_HASH_SHA1           1
+
+#define PDKIM_KEYTYPE_RSA         0
+
+/* -------------------------------------------------------------------------- */
+/* Some required forward declarations, please ignore */
+typedef struct pdkim_stringlist pdkim_stringlist;
+typedef struct pdkim_str pdkim_str;
+typedef struct sha1_context sha1_context;
+typedef struct sha2_context sha2_context;
+#define HAVE_SHA1_CONTEXT
+#define HAVE_SHA2_CONTEXT
+
+/* -------------------------------------------------------------------------- */
+/* Some concessions towards Redmond */
+#ifdef WINDOWS
+#define snprintf _snprintf
+#define strcasecmp _stricmp
+#define strncasecmp _strnicmp
+#define DLLEXPORT __declspec(dllexport)
+#else
+#define DLLEXPORT
+#endif
+
+
+/* -------------------------------------------------------------------------- */
+/* Public key as (usually) fetched from DNS */
+typedef struct pdkim_pubkey {
+  char *version;                  /* v=  */
+  char *granularity;              /* g=  */
+
+  char *hashes;                   /* h=  */
+  char *keytype;                  /* k=  */
+  char *srvtype;                  /* s=  */
+  char *notes;                    /* n=  */
+
+  char *key;                      /* p=  */
+  int   key_len;
+
+  int   testing;                  /* t=y */
+  int   no_subdomaining;          /* t=s */
+} pdkim_pubkey;
+
+/* -------------------------------------------------------------------------- */
+/* Signature as it appears in a DKIM-Signature header */
+typedef struct pdkim_signature {
+
+  /* Bits stored in a DKIM signature header --------------------------- */
+
+  /* (v=) The version, as an integer. Currently, always "1" */
+  int version;
+
+  /* (a=) The signature algorithm. Either PDKIM_ALGO_RSA_SHA256
+     or PDKIM_ALGO_RSA_SHA1 */
+  int algo;
+
+  /* (c=x/) Header canonicalization method. Either PDKIM_CANON_SIMPLE
+     or PDKIM_CANON_RELAXED */
+  int canon_headers;
+
+  /* (c=/x) Body canonicalization method. Either PDKIM_CANON_SIMPLE
+     or PDKIM_CANON_RELAXED */
+  int canon_body;
+
+  /* (q=) Query Method. Currently, only PDKIM_QUERYMETHOD_DNS_TXT
+     is specified */
+  int querymethod;
+
+  /* (s=) The selector string as given in the signature */
+  char *selector;
+
+  /* (d=) The domain as given in the signature */
+  char *domain;
+
+  /* (i=) The identity as given in the signature */
+  char *identity;
+
+  /* (t=) Timestamp of signature creation */
+  unsigned long created;
+
+  /* (x=) Timestamp of expiry of signature */
+  unsigned long expires;
+
+  /* (l=) Amount of hashed body bytes (after canonicalization). Default
+     is -1. Note: a value of 0 means that the body is unsigned! */
+  long bodylength;
+
+  /* (h=) Colon-separated list of header names that are included in the
+     signature */
+  char *headernames;
+
+  /* (z=) */
+  char *copiedheaders;
+
+  /* (b=) Raw signature data, along with its length in bytes */
+  char *sigdata;
+  int   sigdata_len;
+
+  /* (bh=) Raw body hash data, along with its length in bytes */
+  char *bodyhash;
+  int   bodyhash_len;
+
+  /* Folded DKIM-Signature: header. Singing only, NULL for verifying.
+     Ready for insertion into the message. Note: Folded using CRLFTB,
+     but final line terminator is NOT included. Note2: This buffer is
+     free()d when you call pdkim_free_ctx(). */
+  char *signature_header;
+
+  /* The main verification status. Verification only. One of:
+
+     PDKIM_VERIFY_NONE      Verification was not attempted. This status
+                            should not appear.
+
+     PDKIM_VERIFY_INVALID   There was an error while trying to verify
+                            the signature. A more precise description
+                            is available in verify_ext_status.
+
+     PDKIM_VERIFY_FAIL      Verification failed because either the body
+                            hash did not match, or the signature verification
+                            failed. This means the message was modified.
+                            Check verify_ext_status for the exact reason.
+
+     PDKIM_VERIFY_PASS      Verification succeeded.
+  */
+  int verify_status;
+
+  /* Extended verification status. Verification only. Depending on the value
+     of verify_status, it can contain:
+
+     For verify_status == PDKIM_VERIFY_INVALID:
+
+        PDKIM_VERIFY_INVALID_PUBKEY_UNAVAILABLE
+          Unable to retrieve a public key container.
+
+        PDKIM_VERIFY_INVALID_BUFFER_SIZE
+          Either the DNS name constructed to retrieve the public key record
+          does not fit into PDKIM_DNS_TXT_MAX_NAMELEN bytes, or the retrieved
+          record is longer than PDKIM_DNS_TXT_MAX_RECLEN bytes.
+
+        PDKIM_VERIFY_INVALID_PUBKEY_PARSING
+          (Syntax) error while parsing the retrieved public key record.
+
+
+     For verify_status == PDKIM_VERIFY_FAIL:
+
+        PDKIM_VERIFY_FAIL_BODY
+          The calculated body hash does not match the advertised body hash
+          from the bh= tag of the signature.
+
+        PDKIM_VERIFY_FAIL_MESSAGE
+          RSA verification of the signature (b= tag) failed.
+  */
+  int verify_ext_status;
+
+  /* Pointer to a public key record that was used to verify the signature.
+     See pdkim_pubkey declaration above for more information.
+     Caution: is NULL if signing or if no record was retrieved. */
+  pdkim_pubkey *pubkey;
+
+  /* Pointer to the next pdkim_signature signature. NULL if signing or if
+     this is the last signature. */
+  void *next;
+
+  /* Properties below this point are used internally only ------------- */
+
+  /* Per-signature helper variables ----------------------------------- */
+  sha1_context *sha1_body; /* SHA1 block                                */
+  sha2_context *sha2_body; /* SHA256 block                              */
+  unsigned long signed_body_bytes; /* How many body bytes we hashed     */
+  pdkim_stringlist *headers; /* Raw headers included in the sig         */
+  /* Signing specific ------------------------------------------------- */
+  char *rsa_privkey;     /* Private RSA key                             */
+  char *sign_headers;    /* To-be-signed header names                   */
+  char *rawsig_no_b_val; /* Original signature header w/o b= tag value. */
+} pdkim_signature;
+
+
+/* -------------------------------------------------------------------------- */
+/* Context to keep state between all operations. */
+#define PDKIM_MODE_SIGN     0
+#define PDKIM_MODE_VERIFY   1
+#define PDKIM_INPUT_NORMAL  0
+#define PDKIM_INPUT_SMTP    1
+typedef struct pdkim_ctx {
+
+  /* PDKIM_MODE_VERIFY or PDKIM_MODE_SIGN */
+  int mode;
+
+  /* PDKIM_INPUT_SMTP or PDKIM_INPUT_NORMAL */
+  int input_mode;
+
+  /* One (signing) or several chained (verification) signatures */
+  pdkim_signature *sig;
+
+  /* Callback for dns/txt query method (verification only) */
+  int(*dns_txt_callback)(char *, char *);
+
+  /* Coder's little helpers */
+  pdkim_str *cur_header;
+  char      *linebuf;
+  int        linebuf_offset;
+  int        seen_lf;
+  int        seen_eod;
+  int        past_headers;
+  int        num_buffered_crlf;
+  int        num_headers;
+  pdkim_stringlist *headers; /* Raw headers for verification         */
+
+#ifdef PDKIM_DEBUG
+  /* A FILE pointer. When not NULL, debug output will be generated
+    and sent to this stream */
+  FILE *debug_stream;
+#endif
+
+} pdkim_ctx;
+
+
+/* -------------------------------------------------------------------------- */
+/* API functions. Please see the sample code in sample/test_sign.c and
+   sample/test_verify.c for documentation.
+*/
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+DLLEXPORT
+pdkim_ctx *pdkim_init_sign    (int, char *, char *, char *);
+
+DLLEXPORT
+pdkim_ctx *pdkim_init_verify  (int, int(*)(char *, char *));
+
+DLLEXPORT
+int        pdkim_set_optional (pdkim_ctx *, char *, char *,int, int,
+                               long, int,
+                               unsigned long,
+                               unsigned long);
+
+DLLEXPORT
+int        pdkim_feed         (pdkim_ctx *, char *, int);
+DLLEXPORT
+int        pdkim_feed_finish  (pdkim_ctx *, pdkim_signature **);
+
+DLLEXPORT
+void       pdkim_free_ctx     (pdkim_ctx *);
+
+#ifdef PDKIM_DEBUG
+DLLEXPORT
+void       pdkim_set_debug_stream(pdkim_ctx *, FILE *);
+#endif
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/src/pdkim/rsa.c b/src/pdkim/rsa.c
new file mode 100644 (file)
index 0000000..68b201d
--- /dev/null
@@ -0,0 +1,957 @@
+/*
+ *  The RSA public-key cryptosystem
+ *
+ *  Copyright (C) 2006-2010, Brainspark B.V.
+ *
+ *  This file is part of PolarSSL (http://www.polarssl.org)
+ *  Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
+ *
+ *  All rights reserved.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License along
+ *  with this program; if not, write to the Free Software Foundation, Inc.,
+ *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+/*
+ *  RSA was designed by Ron Rivest, Adi Shamir and Len Adleman.
+ *
+ *  http://theory.lcs.mit.edu/~rivest/rsapaper.pdf
+ *  http://www.cacr.math.uwaterloo.ca/hac/about/chap8.pdf
+ */
+
+#include "rsa.h"
+#include "base64.h"
+
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+
+/* *************** begin copy from x509parse.c ********************/
+/*
+ * ASN.1 DER decoding routines
+ */
+static int asn1_get_len( unsigned char **p,
+                         const unsigned char *end,
+                         int *len )
+{
+    if( ( end - *p ) < 1 )
+        return( POLARSSL_ERR_ASN1_OUT_OF_DATA );
+
+    if( ( **p & 0x80 ) == 0 )
+        *len = *(*p)++;
+    else
+    {
+        switch( **p & 0x7F )
+        {
+        case 1:
+            if( ( end - *p ) < 2 )
+                return( POLARSSL_ERR_ASN1_OUT_OF_DATA );
+
+            *len = (*p)[1];
+            (*p) += 2;
+            break;
+
+        case 2:
+            if( ( end - *p ) < 3 )
+                return( POLARSSL_ERR_ASN1_OUT_OF_DATA );
+
+            *len = ( (*p)[1] << 8 ) | (*p)[2];
+            (*p) += 3;
+            break;
+
+        default:
+            return( POLARSSL_ERR_ASN1_INVALID_LENGTH );
+            break;
+        }
+    }
+
+    if( *len > (int) ( end - *p ) )
+        return( POLARSSL_ERR_ASN1_OUT_OF_DATA );
+
+    return( 0 );
+}
+
+static int asn1_get_tag( unsigned char **p,
+                         const unsigned char *end,
+                         int *len, int tag )
+{
+    if( ( end - *p ) < 1 )
+        return( POLARSSL_ERR_ASN1_OUT_OF_DATA );
+
+    if( **p != tag )
+        return( POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
+
+    (*p)++;
+
+    return( asn1_get_len( p, end, len ) );
+}
+
+static int asn1_get_int( unsigned char **p,
+                         const unsigned char *end,
+                         int *val )
+{
+    int ret, len;
+
+    if( ( ret = asn1_get_tag( p, end, &len, ASN1_INTEGER ) ) != 0 )
+        return( ret );
+
+    if( len > (int) sizeof( int ) || ( **p & 0x80 ) != 0 )
+        return( POLARSSL_ERR_ASN1_INVALID_LENGTH );
+
+    *val = 0;
+
+    while( len-- > 0 )
+    {
+        *val = ( *val << 8 ) | **p;
+        (*p)++;
+    }
+
+    return( 0 );
+}
+
+static int asn1_get_mpi( unsigned char **p,
+                         const unsigned char *end,
+                         mpi *X )
+{
+    int ret, len;
+
+    if( ( ret = asn1_get_tag( p, end, &len, ASN1_INTEGER ) ) != 0 )
+        return( ret );
+
+    ret = mpi_read_binary( X, *p, len );
+
+    *p += len;
+
+    return( ret );
+}
+/* ***************   end copy from x509parse.c ********************/
+
+
+
+
+/*
+ * Initialize an RSA context
+ */
+void rsa_init( rsa_context *ctx,
+               int padding,
+               int hash_id )
+{
+    memset( ctx, 0, sizeof( rsa_context ) );
+
+    ctx->padding = padding;
+    ctx->hash_id = hash_id;
+}
+
+#if defined(POLARSSL_GENPRIME)
+
+/*
+ * Generate an RSA keypair
+ */
+int rsa_gen_key( rsa_context *ctx,
+        int (*f_rng)(void *),
+        void *p_rng,
+        int nbits, int exponent )
+{
+    int ret;
+    mpi P1, Q1, H, G;
+
+    if( f_rng == NULL || nbits < 128 || exponent < 3 )
+        return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
+
+    mpi_init( &P1, &Q1, &H, &G, NULL );
+
+    /*
+     * find primes P and Q with Q < P so that:
+     * GCD( E, (P-1)*(Q-1) ) == 1
+     */
+    MPI_CHK( mpi_lset( &ctx->E, exponent ) );
+
+    do
+    {
+        MPI_CHK( mpi_gen_prime( &ctx->P, ( nbits + 1 ) >> 1, 0, 
+                                f_rng, p_rng ) );
+
+        MPI_CHK( mpi_gen_prime( &ctx->Q, ( nbits + 1 ) >> 1, 0,
+                                f_rng, p_rng ) );
+
+        if( mpi_cmp_mpi( &ctx->P, &ctx->Q ) < 0 )
+            mpi_swap( &ctx->P, &ctx->Q );
+
+        if( mpi_cmp_mpi( &ctx->P, &ctx->Q ) == 0 )
+            continue;
+
+        MPI_CHK( mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) );
+        if( mpi_msb( &ctx->N ) != nbits )
+            continue;
+
+        MPI_CHK( mpi_sub_int( &P1, &ctx->P, 1 ) );
+        MPI_CHK( mpi_sub_int( &Q1, &ctx->Q, 1 ) );
+        MPI_CHK( mpi_mul_mpi( &H, &P1, &Q1 ) );
+        MPI_CHK( mpi_gcd( &G, &ctx->E, &H  ) );
+    }
+    while( mpi_cmp_int( &G, 1 ) != 0 );
+
+    /*
+     * D  = E^-1 mod ((P-1)*(Q-1))
+     * DP = D mod (P - 1)
+     * DQ = D mod (Q - 1)
+     * QP = Q^-1 mod P
+     */
+    MPI_CHK( mpi_inv_mod( &ctx->D , &ctx->E, &H  ) );
+    MPI_CHK( mpi_mod_mpi( &ctx->DP, &ctx->D, &P1 ) );
+    MPI_CHK( mpi_mod_mpi( &ctx->DQ, &ctx->D, &Q1 ) );
+    MPI_CHK( mpi_inv_mod( &ctx->QP, &ctx->Q, &ctx->P ) );
+
+    ctx->len = ( mpi_msb( &ctx->N ) + 7 ) >> 3;
+
+cleanup:
+
+    mpi_free( &G, &H, &Q1, &P1, NULL );
+
+    if( ret != 0 )
+    {
+        rsa_free( ctx );
+        return( POLARSSL_ERR_RSA_KEY_GEN_FAILED | ret );
+    }
+
+    return( 0 );   
+}
+
+#endif
+
+/*
+ * Check a public RSA key
+ */
+int rsa_check_pubkey( const rsa_context *ctx )
+{
+    if( !ctx->N.p || !ctx->E.p )
+        return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED );
+
+    if( ( ctx->N.p[0] & 1 ) == 0 ||
+        ( ctx->E.p[0] & 1 ) == 0 )
+        return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED );
+
+    if( mpi_msb( &ctx->N ) < 128 ||
+        mpi_msb( &ctx->N ) > 4096 )
+        return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED );
+
+    if( mpi_msb( &ctx->E ) < 2 ||
+        mpi_msb( &ctx->E ) > 64 )
+        return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED );
+
+    return( 0 );
+}
+
+/*
+ * Check a private RSA key
+ */
+int rsa_check_privkey( const rsa_context *ctx )
+{
+    int ret;
+    mpi PQ, DE, P1, Q1, H, I, G, G2, L1, L2;
+
+    if( ( ret = rsa_check_pubkey( ctx ) ) != 0 )
+        return( ret );
+
+    if( !ctx->P.p || !ctx->Q.p || !ctx->D.p )
+        return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED );
+
+    mpi_init( &PQ, &DE, &P1, &Q1, &H, &I, &G, &G2, &L1, &L2, NULL );
+
+    MPI_CHK( mpi_mul_mpi( &PQ, &ctx->P, &ctx->Q ) );
+    MPI_CHK( mpi_mul_mpi( &DE, &ctx->D, &ctx->E ) );
+    MPI_CHK( mpi_sub_int( &P1, &ctx->P, 1 ) );
+    MPI_CHK( mpi_sub_int( &Q1, &ctx->Q, 1 ) );
+    MPI_CHK( mpi_mul_mpi( &H, &P1, &Q1 ) );
+    MPI_CHK( mpi_gcd( &G, &ctx->E, &H  ) );
+
+    MPI_CHK( mpi_gcd( &G2, &P1, &Q1 ) );
+    MPI_CHK( mpi_div_mpi( &L1, &L2, &H, &G2 ) );  
+    MPI_CHK( mpi_mod_mpi( &I, &DE, &L1  ) );
+
+    /*
+     * Check for a valid PKCS1v2 private key
+     */
+    if( mpi_cmp_mpi( &PQ, &ctx->N ) == 0 &&
+        mpi_cmp_int( &L2, 0 ) == 0 &&
+        mpi_cmp_int( &I, 1 ) == 0 &&
+        mpi_cmp_int( &G, 1 ) == 0 )
+    {
+        mpi_free( &G, &I, &H, &Q1, &P1, &DE, &PQ, &G2, &L1, &L2, NULL );
+        return( 0 );
+    }
+
+    
+cleanup:
+
+    mpi_free( &G, &I, &H, &Q1, &P1, &DE, &PQ, &G2, &L1, &L2, NULL );
+    return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED | ret );
+}
+
+/*
+ * Do an RSA public key operation
+ */
+int rsa_public( rsa_context *ctx,
+                const unsigned char *input,
+                unsigned char *output )
+{
+    int ret, olen;
+    mpi T;
+
+    mpi_init( &T, NULL );
+
+    MPI_CHK( mpi_read_binary( &T, input, ctx->len ) );
+
+    if( mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
+    {
+        mpi_free( &T, NULL );
+        return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
+    }
+
+    olen = ctx->len;
+    MPI_CHK( mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
+    MPI_CHK( mpi_write_binary( &T, output, olen ) );
+
+cleanup:
+
+    mpi_free( &T, NULL );
+
+    if( ret != 0 )
+        return( POLARSSL_ERR_RSA_PUBLIC_FAILED | ret );
+
+    return( 0 );
+}
+
+/*
+ * Do an RSA private key operation
+ */
+int rsa_private( rsa_context *ctx,
+                 const unsigned char *input,
+                 unsigned char *output )
+{
+    int ret, olen;
+    mpi T, T1, T2;
+
+    mpi_init( &T, &T1, &T2, NULL );
+
+    MPI_CHK( mpi_read_binary( &T, input, ctx->len ) );
+
+    if( mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
+    {
+        mpi_free( &T, NULL );
+        return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
+    }
+
+#if 0
+    MPI_CHK( mpi_exp_mod( &T, &T, &ctx->D, &ctx->N, &ctx->RN ) );
+#else
+    /*
+     * faster decryption using the CRT
+     *
+     * T1 = input ^ dP mod P
+     * T2 = input ^ dQ mod Q
+     */
+    MPI_CHK( mpi_exp_mod( &T1, &T, &ctx->DP, &ctx->P, &ctx->RP ) );
+    MPI_CHK( mpi_exp_mod( &T2, &T, &ctx->DQ, &ctx->Q, &ctx->RQ ) );
+
+    /*
+     * T = (T1 - T2) * (Q^-1 mod P) mod P
+     */
+    MPI_CHK( mpi_sub_mpi( &T, &T1, &T2 ) );
+    MPI_CHK( mpi_mul_mpi( &T1, &T, &ctx->QP ) );
+    MPI_CHK( mpi_mod_mpi( &T, &T1, &ctx->P ) );
+
+    /*
+     * output = T2 + T * Q
+     */
+    MPI_CHK( mpi_mul_mpi( &T1, &T, &ctx->Q ) );
+    MPI_CHK( mpi_add_mpi( &T, &T2, &T1 ) );
+#endif
+
+    olen = ctx->len;
+    MPI_CHK( mpi_write_binary( &T, output, olen ) );
+
+cleanup:
+
+    mpi_free( &T, &T1, &T2, NULL );
+
+    if( ret != 0 )
+        return( POLARSSL_ERR_RSA_PRIVATE_FAILED | ret );
+
+    return( 0 );
+}
+
+/*
+ * Add the message padding, then do an RSA operation
+ */
+int rsa_pkcs1_encrypt( rsa_context *ctx,
+                       int (*f_rng)(void *),
+                       void *p_rng,
+                       int mode, int  ilen,
+                       const unsigned char *input,
+                       unsigned char *output )
+{
+    int nb_pad, olen;
+    unsigned char *p = output;
+
+    olen = ctx->len;
+
+    switch( ctx->padding )
+    {
+        case RSA_PKCS_V15:
+
+            if( ilen < 0 || olen < ilen + 11 || f_rng == NULL )
+                return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
+
+            nb_pad = olen - 3 - ilen;
+
+            *p++ = 0;
+            *p++ = RSA_CRYPT;
+
+            while( nb_pad-- > 0 )
+            {
+                int rng_dl = 100;
+
+                do {
+                    *p = (unsigned char) f_rng( p_rng );
+                } while( *p == 0 && --rng_dl );
+
+                // Check if RNG failed to generate data
+                //
+                if( rng_dl == 0 )
+                    return POLARSSL_ERR_RSA_RNG_FAILED;
+
+                p++;
+            }
+            *p++ = 0;
+            memcpy( p, input, ilen );
+            break;
+
+        default:
+
+            return( POLARSSL_ERR_RSA_INVALID_PADDING );
+    }
+
+    return( ( mode == RSA_PUBLIC )
+            ? rsa_public(  ctx, output, output )
+            : rsa_private( ctx, output, output ) );
+}
+
+/*
+ * Do an RSA operation, then remove the message padding
+ */
+int rsa_pkcs1_decrypt( rsa_context *ctx,
+                       int mode, int *olen,
+                       const unsigned char *input,
+                       unsigned char *output,
+                       int output_max_len)
+{
+    int ret, ilen;
+    unsigned char *p;
+    unsigned char buf[1024];
+
+    ilen = ctx->len;
+
+    if( ilen < 16 || ilen > (int) sizeof( buf ) )
+        return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
+
+    ret = ( mode == RSA_PUBLIC )
+          ? rsa_public(  ctx, input, buf )
+          : rsa_private( ctx, input, buf );
+
+    if( ret != 0 )
+        return( ret );
+
+    p = buf;
+
+    switch( ctx->padding )
+    {
+        case RSA_PKCS_V15:
+
+            if( *p++ != 0 || *p++ != RSA_CRYPT )
+                return( POLARSSL_ERR_RSA_INVALID_PADDING );
+
+            while( *p != 0 )
+            {
+                if( p >= buf + ilen - 1 )
+                    return( POLARSSL_ERR_RSA_INVALID_PADDING );
+                p++;
+            }
+            p++;
+            break;
+
+        default:
+
+            return( POLARSSL_ERR_RSA_INVALID_PADDING );
+    }
+
+    if (ilen - (int)(p - buf) > output_max_len)
+      return( POLARSSL_ERR_RSA_OUTPUT_TOO_LARGE );
+
+    *olen = ilen - (int)(p - buf);
+    memcpy( output, p, *olen );
+
+    return( 0 );
+}
+
+/*
+ * Do an RSA operation to sign the message digest
+ */
+int rsa_pkcs1_sign( rsa_context *ctx,
+                    int mode,
+                    int hash_id,
+                    int hashlen,
+                    const unsigned char *hash,
+                    unsigned char *sig )
+{
+    int nb_pad, olen;
+    unsigned char *p = sig;
+
+    olen = ctx->len;
+
+    switch( ctx->padding )
+    {
+        case RSA_PKCS_V15:
+
+            switch( hash_id )
+            {
+                case SIG_RSA_RAW:
+                    nb_pad = olen - 3 - hashlen;
+                    break;
+
+                case SIG_RSA_MD2:
+                case SIG_RSA_MD4:
+                case SIG_RSA_MD5:
+                    nb_pad = olen - 3 - 34;
+                    break;
+
+                case SIG_RSA_SHA1:
+                    nb_pad = olen - 3 - 35;
+                    break;
+
+                case SIG_RSA_SHA224:
+                    nb_pad = olen - 3 - 47;
+                    break;
+
+                case SIG_RSA_SHA256:
+                    nb_pad = olen - 3 - 51;
+                    break;
+
+                case SIG_RSA_SHA384:
+                    nb_pad = olen - 3 - 67;
+                    break;
+
+                case SIG_RSA_SHA512:
+                    nb_pad = olen - 3 - 83;
+                    break;
+
+
+                default:
+                    return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
+            }
+
+            if( nb_pad < 8 )
+                return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
+
+            *p++ = 0;
+            *p++ = RSA_SIGN;
+            memset( p, 0xFF, nb_pad );
+            p += nb_pad;
+            *p++ = 0;
+            break;
+
+        default:
+
+            return( POLARSSL_ERR_RSA_INVALID_PADDING );
+    }
+
+    switch( hash_id )
+    {
+        case SIG_RSA_RAW:
+            memcpy( p, hash, hashlen );
+            break;
+
+        case SIG_RSA_MD2:
+            memcpy( p, ASN1_HASH_MDX, 18 );
+            memcpy( p + 18, hash, 16 );
+            p[13] = 2; break;
+
+        case SIG_RSA_MD4:
+            memcpy( p, ASN1_HASH_MDX, 18 );
+            memcpy( p + 18, hash, 16 );
+            p[13] = 4; break;
+
+        case SIG_RSA_MD5:
+            memcpy( p, ASN1_HASH_MDX, 18 );
+            memcpy( p + 18, hash, 16 );
+            p[13] = 5; break;
+
+        case SIG_RSA_SHA1:
+            memcpy( p, ASN1_HASH_SHA1, 15 );
+            memcpy( p + 15, hash, 20 );
+            break;
+
+        case SIG_RSA_SHA224:
+            memcpy( p, ASN1_HASH_SHA2X, 19 );
+            memcpy( p + 19, hash, 28 );
+            p[1] += 28; p[14] = 4; p[18] += 28; break;
+
+        case SIG_RSA_SHA256:
+            memcpy( p, ASN1_HASH_SHA2X, 19 );
+            memcpy( p + 19, hash, 32 );
+            p[1] += 32; p[14] = 1; p[18] += 32; break;
+
+        case SIG_RSA_SHA384:
+            memcpy( p, ASN1_HASH_SHA2X, 19 );
+            memcpy( p + 19, hash, 48 );
+            p[1] += 48; p[14] = 2; p[18] += 48; break;
+
+        case SIG_RSA_SHA512:
+            memcpy( p, ASN1_HASH_SHA2X, 19 );
+            memcpy( p + 19, hash, 64 );
+            p[1] += 64; p[14] = 3; p[18] += 64; break;
+
+        default:
+            return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
+    }
+
+    return( ( mode == RSA_PUBLIC )
+            ? rsa_public(  ctx, sig, sig )
+            : rsa_private( ctx, sig, sig ) );
+}
+
+/*
+ * Do an RSA operation and check the message digest
+ */
+int rsa_pkcs1_verify( rsa_context *ctx,
+                      int mode,
+                      int hash_id,
+                      int hashlen,
+                      const unsigned char *hash,
+                      unsigned char *sig )
+{
+    int ret, len, siglen;
+    unsigned char *p, c;
+    unsigned char buf[1024];
+
+    siglen = ctx->len;
+
+    if( siglen < 16 || siglen > (int) sizeof( buf ) )
+        return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
+
+    ret = ( mode == RSA_PUBLIC )
+          ? rsa_public(  ctx, sig, buf )
+          : rsa_private( ctx, sig, buf );
+
+    if( ret != 0 )
+        return( ret );
+
+    p = buf;
+
+    switch( ctx->padding )
+    {
+        case RSA_PKCS_V15:
+
+            if( *p++ != 0 || *p++ != RSA_SIGN )
+                return( POLARSSL_ERR_RSA_INVALID_PADDING );
+
+            while( *p != 0 )
+            {
+                if( p >= buf + siglen - 1 || *p != 0xFF )
+                    return( POLARSSL_ERR_RSA_INVALID_PADDING );
+                p++;
+            }
+            p++;
+            break;
+
+        default:
+
+            return( POLARSSL_ERR_RSA_INVALID_PADDING );
+    }
+
+    len = siglen - (int)( p - buf );
+
+    if( len == 34 )
+    {
+        c = p[13];
+        p[13] = 0;
+
+        if( memcmp( p, ASN1_HASH_MDX, 18 ) != 0 )
+            return( POLARSSL_ERR_RSA_VERIFY_FAILED );
+
+        if( ( c == 2 && hash_id == SIG_RSA_MD2 ) ||
+            ( c == 4 && hash_id == SIG_RSA_MD4 ) ||
+            ( c == 5 && hash_id == SIG_RSA_MD5 ) )
+        {
+            if( memcmp( p + 18, hash, 16 ) == 0 )
+                return( 0 );
+            else
+                return( POLARSSL_ERR_RSA_VERIFY_FAILED );
+        }
+    }
+
+    if( len == 35 && hash_id == SIG_RSA_SHA1 )
+    {
+        if( memcmp( p, ASN1_HASH_SHA1, 15 ) == 0 &&
+            memcmp( p + 15, hash, 20 ) == 0 )
+            return( 0 );
+        else
+            return( POLARSSL_ERR_RSA_VERIFY_FAILED );
+    }
+    if( ( len == 19 + 28 && p[14] == 4 && hash_id == SIG_RSA_SHA224 ) ||
+        ( len == 19 + 32 && p[14] == 1 && hash_id == SIG_RSA_SHA256 ) ||
+        ( len == 19 + 48 && p[14] == 2 && hash_id == SIG_RSA_SHA384 ) ||
+        ( len == 19 + 64 && p[14] == 3 && hash_id == SIG_RSA_SHA512 ) )
+    {
+        c = p[1] - 17;
+        p[1] = 17;
+        p[14] = 0;
+
+        if( p[18] == c &&
+                memcmp( p, ASN1_HASH_SHA2X, 18 ) == 0 &&
+                memcmp( p + 19, hash, c ) == 0 )
+            return( 0 );
+        else
+            return( POLARSSL_ERR_RSA_VERIFY_FAILED );
+    }
+
+    if( len == hashlen && hash_id == SIG_RSA_RAW )
+    {
+        if( memcmp( p, hash, hashlen ) == 0 )
+            return( 0 );
+        else
+            return( POLARSSL_ERR_RSA_VERIFY_FAILED );
+    }
+
+    return( POLARSSL_ERR_RSA_INVALID_PADDING );
+}
+
+/*
+ * Free the components of an RSA key
+ */
+void rsa_free( rsa_context *ctx )
+{
+    mpi_free( &ctx->RQ, &ctx->RP, &ctx->RN,
+              &ctx->QP, &ctx->DQ, &ctx->DP,
+              &ctx->Q,  &ctx->P,  &ctx->D,
+              &ctx->E,  &ctx->N,  NULL );
+}
+
+
+/* PDKIM code (not copied from polarssl) */
+/*
+ * Parse a public RSA key
+
+OpenSSL RSA public key ASN1 container
+  0:d=0  hl=3 l= 159 cons: SEQUENCE
+  3:d=1  hl=2 l=  13 cons: SEQUENCE
+  5:d=2  hl=2 l=   9 prim: OBJECT:rsaEncryption
+ 16:d=2  hl=2 l=   0 prim: NULL
+ 18:d=1  hl=3 l= 141 prim: BIT STRING:RSAPublicKey (below)
+
+RSAPublicKey ASN1 container
+  0:d=0  hl=3 l= 137 cons: SEQUENCE
+  3:d=1  hl=3 l= 129 prim: INTEGER:Public modulus
+135:d=1  hl=2 l=   3 prim: INTEGER:Public exponent
+*/
+
+int rsa_parse_public_key( rsa_context *rsa, unsigned char *buf, int buflen )
+{
+    unsigned char *p, *end;
+    int ret, len;
+
+    p = buf;
+    end = buf+buflen;
+
+    if( ( ret = asn1_get_tag( &p, end, &len,
+            ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 ) {
+        return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT | ret );
+    }
+
+    if( ( ret = asn1_get_tag( &p, end, &len,
+            ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) == 0 ) {
+        /* Skip over embedded rsaEncryption Object */
+        p+=len;
+
+        /* The RSAPublicKey ASN1 container is wrapped in a BIT STRING */
+        if( ( ret = asn1_get_tag( &p, end, &len,
+                ASN1_BIT_STRING ) ) != 0 ) {
+            return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT | ret );
+        }
+
+        /* Limit range to that BIT STRING */
+        end = p + len;
+        p++;
+
+        if( ( ret = asn1_get_tag( &p, end, &len,
+                ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 ) {
+            return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT | ret );
+        }
+    }
+
+    if ( ( ( ret = asn1_get_mpi( &p, end, &(rsa->N)  ) ) == 0 ) &&
+         ( ( ret = asn1_get_mpi( &p, end, &(rsa->E)  ) ) == 0 ) ) {
+        rsa->len = mpi_size( &rsa->N );
+        return 0;
+    }
+
+    return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT | ret );
+}
+
+/*
+ * Parse a private RSA key
+ */
+int rsa_parse_key( rsa_context *rsa, unsigned char *buf, int buflen,
+                                     unsigned char *pwd, int pwdlen )
+{
+    int ret, len, enc;
+    unsigned char *s1, *s2;
+    unsigned char *p, *end;
+
+    s1 = (unsigned char *) strstr( (char *) buf,
+        "-----BEGIN RSA PRIVATE KEY-----" );
+
+    if( s1 != NULL )
+    {
+        s2 = (unsigned char *) strstr( (char *) buf,
+            "-----END RSA PRIVATE KEY-----" );
+
+        if( s2 == NULL || s2 <= s1 )
+            return( POLARSSL_ERR_X509_KEY_INVALID_PEM );
+
+        s1 += 31;
+        if( *s1 == '\r' ) s1++;
+        if( *s1 == '\n' ) s1++;
+            else return( POLARSSL_ERR_X509_KEY_INVALID_PEM );
+
+        enc = 0;
+
+        if( memcmp( s1, "Proc-Type: 4,ENCRYPTED", 22 ) == 0 )
+        {
+            return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
+        }
+
+        len = 0;
+        ret = base64_decode( NULL, &len, s1, s2 - s1 );
+
+        if( ret == POLARSSL_ERR_BASE64_INVALID_CHARACTER )
+            return( ret | POLARSSL_ERR_X509_KEY_INVALID_PEM );
+
+        if( ( buf = (unsigned char *) malloc( len ) ) == NULL )
+            return( 1 );
+
+        if( ( ret = base64_decode( buf, &len, s1, s2 - s1 ) ) != 0 )
+        {
+            free( buf );
+            return( ret | POLARSSL_ERR_X509_KEY_INVALID_PEM );
+        }
+
+        buflen = len;
+
+        if( enc != 0 )
+        {
+            return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
+        }
+    }
+
+    memset( rsa, 0, sizeof( rsa_context ) );
+
+    p = buf;
+    end = buf + buflen;
+
+    /*
+     *  RSAPrivateKey ::= SEQUENCE {
+     *      version           Version,
+     *      modulus           INTEGER,  -- n
+     *      publicExponent    INTEGER,  -- e
+     *      privateExponent   INTEGER,  -- d
+     *      prime1            INTEGER,  -- p
+     *      prime2            INTEGER,  -- q
+     *      exponent1         INTEGER,  -- d mod (p-1)
+     *      exponent2         INTEGER,  -- d mod (q-1)
+     *      coefficient       INTEGER,  -- (inverse of q) mod p
+     *      otherPrimeInfos   OtherPrimeInfos OPTIONAL
+     *  }
+     */
+    if( ( ret = asn1_get_tag( &p, end, &len,
+            ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
+    {
+        if( s1 != NULL )
+            free( buf );
+
+        rsa_free( rsa );
+        return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT | ret );
+    }
+
+    end = p + len;
+
+    if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
+    {
+        if( s1 != NULL )
+            free( buf );
+
+        rsa_free( rsa );
+        return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT | ret );
+    }
+
+    if( rsa->ver != 0 )
+    {
+        if( s1 != NULL )
+            free( buf );
+
+        rsa_free( rsa );
+        return( ret | POLARSSL_ERR_X509_KEY_INVALID_VERSION );
+    }
+
+    if( ( ret = asn1_get_mpi( &p, end, &rsa->N  ) ) != 0 ||
+        ( ret = asn1_get_mpi( &p, end, &rsa->E  ) ) != 0 ||
+        ( ret = asn1_get_mpi( &p, end, &rsa->D  ) ) != 0 ||
+        ( ret = asn1_get_mpi( &p, end, &rsa->P  ) ) != 0 ||
+        ( ret = asn1_get_mpi( &p, end, &rsa->Q  ) ) != 0 ||
+        ( ret = asn1_get_mpi( &p, end, &rsa->DP ) ) != 0 ||
+        ( ret = asn1_get_mpi( &p, end, &rsa->DQ ) ) != 0 ||
+        ( ret = asn1_get_mpi( &p, end, &rsa->QP ) ) != 0 )
+    {
+        if( s1 != NULL )
+            free( buf );
+
+        rsa_free( rsa );
+        return( ret | POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
+    }
+
+    rsa->len = mpi_size( &rsa->N );
+
+    if( p != end )
+    {
+        if( s1 != NULL )
+            free( buf );
+
+        rsa_free( rsa );
+        return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT |
+                POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
+    }
+
+    if( ( ret = rsa_check_privkey( rsa ) ) != 0 )
+    {
+        if( s1 != NULL )
+            free( buf );
+
+        rsa_free( rsa );
+        return( ret );
+    }
+
+    if( s1 != NULL )
+        free( buf );
+
+    return( 0 );
+}
diff --git a/src/pdkim/rsa.h b/src/pdkim/rsa.h
new file mode 100644 (file)
index 0000000..af6823b
--- /dev/null
@@ -0,0 +1,419 @@
+/**
+ * \file rsa.h
+ *
+ *  Copyright (C) 2006-2010, Brainspark B.V.
+ *
+ *  This file is part of PolarSSL (http://www.polarssl.org)
+ *  Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
+ *
+ *  All rights reserved.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License along
+ *  with this program; if not, write to the Free Software Foundation, Inc.,
+ *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef POLARSSL_RSA_H
+#define POLARSSL_RSA_H
+
+#include "bignum.h"
+
+/*
+ * RSA Error codes
+ */
+#define POLARSSL_ERR_RSA_BAD_INPUT_DATA                    -0x0400
+#define POLARSSL_ERR_RSA_INVALID_PADDING                   -0x0410
+#define POLARSSL_ERR_RSA_KEY_GEN_FAILED                    -0x0420
+#define POLARSSL_ERR_RSA_KEY_CHECK_FAILED                  -0x0430
+#define POLARSSL_ERR_RSA_PUBLIC_FAILED                     -0x0440
+#define POLARSSL_ERR_RSA_PRIVATE_FAILED                    -0x0450
+#define POLARSSL_ERR_RSA_VERIFY_FAILED                     -0x0460
+#define POLARSSL_ERR_RSA_OUTPUT_TOO_LARGE                  -0x0470
+#define POLARSSL_ERR_RSA_RNG_FAILED                        -0x0480
+
+/* *************** begin copy from x509.h  ************************/
+/*
+ * ASN1 Error codes
+ *
+ * These error codes will be OR'ed to X509 error codes for
+ * higher error granularity.
+ */
+#define POLARSSL_ERR_ASN1_OUT_OF_DATA                      0x0014
+#define POLARSSL_ERR_ASN1_UNEXPECTED_TAG                   0x0016
+#define POLARSSL_ERR_ASN1_INVALID_LENGTH                   0x0018
+#define POLARSSL_ERR_ASN1_LENGTH_MISMATCH                  0x001A
+#define POLARSSL_ERR_ASN1_INVALID_DATA                     0x001C
+
+/*
+ * X509 Error codes
+ */
+#define POLARSSL_ERR_X509_FEATURE_UNAVAILABLE              -0x0020
+#define POLARSSL_ERR_X509_CERT_INVALID_PEM                 -0x0040
+#define POLARSSL_ERR_X509_CERT_INVALID_FORMAT              -0x0060
+#define POLARSSL_ERR_X509_CERT_INVALID_VERSION             -0x0080
+#define POLARSSL_ERR_X509_CERT_INVALID_SERIAL              -0x00A0
+#define POLARSSL_ERR_X509_CERT_INVALID_ALG                 -0x00C0
+#define POLARSSL_ERR_X509_CERT_INVALID_NAME                -0x00E0
+#define POLARSSL_ERR_X509_CERT_INVALID_DATE                -0x0100
+#define POLARSSL_ERR_X509_CERT_INVALID_PUBKEY              -0x0120
+#define POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE           -0x0140
+#define POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS          -0x0160
+#define POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION             -0x0180
+#define POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG             -0x01A0
+#define POLARSSL_ERR_X509_CERT_UNKNOWN_PK_ALG              -0x01C0
+#define POLARSSL_ERR_X509_CERT_SIG_MISMATCH                -0x01E0
+#define POLARSSL_ERR_X509_CERT_VERIFY_FAILED               -0x0200
+#define POLARSSL_ERR_X509_KEY_INVALID_PEM                  -0x0220
+#define POLARSSL_ERR_X509_KEY_INVALID_VERSION              -0x0240
+#define POLARSSL_ERR_X509_KEY_INVALID_FORMAT               -0x0260
+#define POLARSSL_ERR_X509_KEY_INVALID_ENC_IV               -0x0280
+#define POLARSSL_ERR_X509_KEY_UNKNOWN_ENC_ALG              -0x02A0
+#define POLARSSL_ERR_X509_KEY_PASSWORD_REQUIRED            -0x02C0
+#define POLARSSL_ERR_X509_KEY_PASSWORD_MISMATCH            -0x02E0
+#define POLARSSL_ERR_X509_POINT_ERROR                      -0x0300
+#define POLARSSL_ERR_X509_VALUE_TO_LENGTH                  -0x0320
+
+/*
+ * DER constants
+ */
+#define ASN1_BOOLEAN                 0x01
+#define ASN1_INTEGER                 0x02
+#define ASN1_BIT_STRING              0x03
+#define ASN1_OCTET_STRING            0x04
+#define ASN1_NULL                    0x05
+#define ASN1_OID                     0x06
+#define ASN1_UTF8_STRING             0x0C
+#define ASN1_SEQUENCE                0x10
+#define ASN1_SET                     0x11
+#define ASN1_PRINTABLE_STRING        0x13
+#define ASN1_T61_STRING              0x14
+#define ASN1_IA5_STRING              0x16
+#define ASN1_UTC_TIME                0x17
+#define ASN1_GENERALIZED_TIME        0x18
+#define ASN1_UNIVERSAL_STRING        0x1C
+#define ASN1_BMP_STRING              0x1E
+#define ASN1_PRIMITIVE               0x00
+#define ASN1_CONSTRUCTED             0x20
+#define ASN1_CONTEXT_SPECIFIC        0x80
+/* ***************   end copy from x509.h  ************************/
+
+/*
+ * PKCS#1 constants
+ */
+#define SIG_RSA_RAW     0
+#define SIG_RSA_MD2     2
+#define SIG_RSA_MD4     3
+#define SIG_RSA_MD5     4
+#define SIG_RSA_SHA1    5
+#define SIG_RSA_SHA224  14
+#define SIG_RSA_SHA256  11
+#define SIG_RSA_SHA384  12
+#define SIG_RSA_SHA512  13
+
+#define RSA_PUBLIC      0
+#define RSA_PRIVATE     1
+
+#define RSA_PKCS_V15    0
+#define RSA_PKCS_V21    1
+
+#define RSA_SIGN        1
+#define RSA_CRYPT       2
+
+#define ASN1_STR_CONSTRUCTED_SEQUENCE "\x30"
+#define ASN1_STR_NULL                 "\x05"
+#define ASN1_STR_OID                  "\x06"
+#define ASN1_STR_OCTET_STRING         "\x04"
+
+#define OID_DIGEST_ALG_MDX            "\x2A\x86\x48\x86\xF7\x0D\x02\x00"
+#define OID_HASH_ALG_SHA1             "\x2b\x0e\x03\x02\x1a"
+#define OID_HASH_ALG_SHA2X            "\x60\x86\x48\x01\x65\x03\x04\x02\x00"
+
+#define OID_ISO_MEMBER_BODIES         "\x2a"
+#define OID_ISO_IDENTIFIED_ORG        "\x2b"
+
+/*
+ * ISO Member bodies OID parts
+ */
+#define OID_COUNTRY_US                "\x86\x48"
+#define OID_RSA_DATA_SECURITY         "\x86\xf7\x0d"
+
+/*
+ * ISO Identified organization OID parts
+ */
+#define OID_OIW_SECSIG_SHA1           "\x0e\x03\x02\x1a"
+
+/*
+ * DigestInfo ::= SEQUENCE {
+ *   digestAlgorithm DigestAlgorithmIdentifier,
+ *   digest Digest }
+ *
+ * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
+ *
+ * Digest ::= OCTET STRING
+ */
+#define ASN1_HASH_MDX \
+( \
+    ASN1_STR_CONSTRUCTED_SEQUENCE "\x20" \
+    ASN1_STR_CONSTRUCTED_SEQUENCE "\x0C" \
+    ASN1_STR_OID "\x08" \
+    OID_DIGEST_ALG_MDX \
+    ASN1_STR_NULL "\x00" \
+    ASN1_STR_OCTET_STRING "\x10" \
+)
+
+#define ASN1_HASH_SHA1 \
+    ASN1_STR_CONSTRUCTED_SEQUENCE "\x21" \
+    ASN1_STR_CONSTRUCTED_SEQUENCE "\x09" \
+    ASN1_STR_OID "\x05" \
+    OID_HASH_ALG_SHA1 \
+    ASN1_STR_NULL "\x00" \
+    ASN1_STR_OCTET_STRING "\x14"
+
+#define ASN1_HASH_SHA2X \
+    ASN1_STR_CONSTRUCTED_SEQUENCE "\x11" \
+    ASN1_STR_CONSTRUCTED_SEQUENCE "\x0d" \
+    ASN1_STR_OID "\x09" \
+    OID_HASH_ALG_SHA2X \
+    ASN1_STR_NULL "\x00" \
+    ASN1_STR_OCTET_STRING "\x00"
+
+/**
+ * \brief          RSA context structure
+ */
+typedef struct
+{
+    int ver;                    /*!<  always 0          */
+    int len;                    /*!<  size(N) in chars  */
+
+    mpi N;                      /*!<  public modulus    */
+    mpi E;                      /*!<  public exponent   */
+
+    mpi D;                      /*!<  private exponent  */
+    mpi P;                      /*!<  1st prime factor  */
+    mpi Q;                      /*!<  2nd prime factor  */
+    mpi DP;                     /*!<  D % (P - 1)       */
+    mpi DQ;                     /*!<  D % (Q - 1)       */
+    mpi QP;                     /*!<  1 / (Q % P)       */
+
+    mpi RN;                     /*!<  cached R^2 mod N  */
+    mpi RP;                     /*!<  cached R^2 mod P  */
+    mpi RQ;                     /*!<  cached R^2 mod Q  */
+
+    int padding;                /*!<  1.5 or OAEP/PSS   */
+    int hash_id;                /*!<  hash identifier   */
+}
+rsa_context;
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * \brief          Initialize an RSA context
+ *
+ * \param ctx      RSA context to be initialized
+ * \param padding  RSA_PKCS_V15 or RSA_PKCS_V21
+ * \param hash_id  RSA_PKCS_V21 hash identifier
+ *
+ * \note           The hash_id parameter is actually ignored
+ *                 when using RSA_PKCS_V15 padding.
+ *
+ * \note           Currently, RSA_PKCS_V21 padding
+ *                 is not supported.
+ */
+void rsa_init( rsa_context *ctx,
+               int padding,
+               int hash_id);
+
+/**
+ * \brief          Generate an RSA keypair
+ *
+ * \param ctx      RSA context that will hold the key
+ * \param f_rng    RNG function
+ * \param p_rng    RNG parameter
+ * \param nbits    size of the public key in bits
+ * \param exponent public exponent (e.g., 65537)
+ *
+ * \note           rsa_init() must be called beforehand to setup
+ *                 the RSA context.
+ *
+ * \return         0 if successful, or an POLARSSL_ERR_RSA_XXX error code
+ */
+int rsa_gen_key( rsa_context *ctx,
+                 int (*f_rng)(void *),
+                 void *p_rng,
+                 int nbits, int exponent );
+
+/**
+ * \brief          Check a public RSA key
+ *
+ * \param ctx      RSA context to be checked
+ *
+ * \return         0 if successful, or an POLARSSL_ERR_RSA_XXX error code
+ */
+int rsa_check_pubkey( const rsa_context *ctx );
+
+/**
+ * \brief          Check a private RSA key
+ *
+ * \param ctx      RSA context to be checked
+ *
+ * \return         0 if successful, or an POLARSSL_ERR_RSA_XXX error code
+ */
+int rsa_check_privkey( const rsa_context *ctx );
+
+/**
+ * \brief          Do an RSA public key operation
+ *
+ * \param ctx      RSA context
+ * \param input    input buffer
+ * \param output   output buffer
+ *
+ * \return         0 if successful, or an POLARSSL_ERR_RSA_XXX error code
+ *
+ * \note           This function does NOT take care of message
+ *                 padding. Also, be sure to set input[0] = 0 or assure that
+ *                 input is smaller than N.
+ *
+ * \note           The input and output buffers must be large
+ *                 enough (eg. 128 bytes if RSA-1024 is used).
+ */
+int rsa_public( rsa_context *ctx,
+                const unsigned char *input,
+                unsigned char *output );
+
+/**
+ * \brief          Do an RSA private key operation
+ *
+ * \param ctx      RSA context
+ * \param input    input buffer
+ * \param output   output buffer
+ *
+ * \return         0 if successful, or an POLARSSL_ERR_RSA_XXX error code
+ *
+ * \note           The input and output buffers must be large
+ *                 enough (eg. 128 bytes if RSA-1024 is used).
+ */
+int rsa_private( rsa_context *ctx,
+                 const unsigned char *input,
+                 unsigned char *output );
+
+/**
+ * \brief          Add the message padding, then do an RSA operation
+ *
+ * \param ctx      RSA context
+ * \param f_rng    RNG function
+ * \param p_rng    RNG parameter
+ * \param mode     RSA_PUBLIC or RSA_PRIVATE
+ * \param ilen     contains the plaintext length
+ * \param input    buffer holding the data to be encrypted
+ * \param output   buffer that will hold the ciphertext
+ *
+ * \return         0 if successful, or an POLARSSL_ERR_RSA_XXX error code
+ *
+ * \note           The output buffer must be as large as the size
+ *                 of ctx->N (eg. 128 bytes if RSA-1024 is used).
+ */
+int rsa_pkcs1_encrypt( rsa_context *ctx,
+                       int (*f_rng)(void *),
+                       void *p_rng,
+                       int mode, int  ilen,
+                       const unsigned char *input,
+                       unsigned char *output );
+
+/**
+ * \brief          Do an RSA operation, then remove the message padding
+ *
+ * \param ctx      RSA context
+ * \param mode     RSA_PUBLIC or RSA_PRIVATE
+ * \param input    buffer holding the encrypted data
+ * \param output   buffer that will hold the plaintext
+ * \param olen     will contain the plaintext length
+ * \param output_max_len  maximum length of the output buffer
+ *
+ * \return         0 if successful, or an POLARSSL_ERR_RSA_XXX error code
+ *
+ * \note           The output buffer must be as large as the size
+ *                 of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise
+ *                 an error is thrown.
+ */
+int rsa_pkcs1_decrypt( rsa_context *ctx,
+                       int mode, int *olen,
+                       const unsigned char *input,
+                       unsigned char *output,
+                       int output_max_len );
+
+/**
+ * \brief          Do a private RSA to sign a message digest
+ *
+ * \param ctx      RSA context
+ * \param mode     RSA_PUBLIC or RSA_PRIVATE
+ * \param hash_id  SIG_RSA_RAW, SIG_RSA_MD{2,4,5} or SIG_RSA_SHA{1,224,256,384,512}
+ * \param hashlen  message digest length (for SIG_RSA_RAW only)
+ * \param hash     buffer holding the message digest
+ * \param sig      buffer that will hold the ciphertext
+ *
+ * \return         0 if the signing operation was successful,
+ *                 or an POLARSSL_ERR_RSA_XXX error code
+ *
+ * \note           The "sig" buffer must be as large as the size
+ *                 of ctx->N (eg. 128 bytes if RSA-1024 is used).
+ */
+int rsa_pkcs1_sign( rsa_context *ctx,
+                    int mode,
+                    int hash_id,
+                    int hashlen,
+                    const unsigned char *hash,
+                    unsigned char *sig );
+
+/**
+ * \brief          Do a public RSA and check the message digest
+ *
+ * \param ctx      points to an RSA public key
+ * \param mode     RSA_PUBLIC or RSA_PRIVATE
+ * \param hash_id  SIG_RSA_RAW, SIG_RSA_MD{2,4,5} or SIG_RSA_SHA{1,224,256,384,512}
+ * \param hashlen  message digest length (for SIG_RSA_RAW only)
+ * \param hash     buffer holding the message digest
+ * \param sig      buffer holding the ciphertext
+ *
+ * \return         0 if the verify operation was successful,
+ *                 or an POLARSSL_ERR_RSA_XXX error code
+ *
+ * \note           The "sig" buffer must be as large as the size
+ *                 of ctx->N (eg. 128 bytes if RSA-1024 is used).
+ */
+int rsa_pkcs1_verify( rsa_context *ctx,
+                      int mode,
+                      int hash_id,
+                      int hashlen,
+                      const unsigned char *hash,
+                      unsigned char *sig );
+
+/**
+ * \brief          Free the components of an RSA key
+ *
+ * \param ctx      RSA Context to free
+ */
+void rsa_free( rsa_context *ctx );
+
+/* PDKIM declarations (not part of polarssl) */
+int rsa_parse_public_key( rsa_context *rsa, unsigned char *buf, int buflen );
+int rsa_parse_key( rsa_context *rsa, unsigned char *buf, int buflen,
+                                     unsigned char *pwd, int pwdlen );
+
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* rsa.h */
diff --git a/src/pdkim/sha1.c b/src/pdkim/sha1.c
new file mode 100644 (file)
index 0000000..81b862f
--- /dev/null
@@ -0,0 +1,434 @@
+/*
+ *  FIPS-180-1 compliant SHA-1 implementation
+ *
+ *  Copyright (C) 2006-2010, Brainspark B.V.
+ *
+ *  This file is part of PolarSSL (http://www.polarssl.org)
+ *  Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
+ *
+ *  All rights reserved.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License along
+ *  with this program; if not, write to the Free Software Foundation, Inc.,
+ *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+/*
+ *  The SHA-1 standard was published by NIST in 1993.
+ *
+ *  http://www.itl.nist.gov/fipspubs/fip180-1.htm
+ */
+
+#include "sha1.h"
+
+#include <string.h>
+#include <stdio.h>
+
+/*
+ * 32-bit integer manipulation macros (big endian)
+ */
+#ifndef GET_ULONG_BE
+#define GET_ULONG_BE(n,b,i)                             \
+{                                                       \
+    (n) = ( (unsigned long) (b)[(i)    ] << 24 )        \
+        | ( (unsigned long) (b)[(i) + 1] << 16 )        \
+        | ( (unsigned long) (b)[(i) + 2] <<  8 )        \
+        | ( (unsigned long) (b)[(i) + 3]       );       \
+}
+#endif
+
+#ifndef PUT_ULONG_BE
+#define PUT_ULONG_BE(n,b,i)                             \
+{                                                       \
+    (b)[(i)    ] = (unsigned char) ( (n) >> 24 );       \
+    (b)[(i) + 1] = (unsigned char) ( (n) >> 16 );       \
+    (b)[(i) + 2] = (unsigned char) ( (n) >>  8 );       \
+    (b)[(i) + 3] = (unsigned char) ( (n)       );       \
+}
+#endif
+
+/*
+ * SHA-1 context setup
+ */
+void sha1_starts( sha1_context *ctx )
+{
+    ctx->total[0] = 0;
+    ctx->total[1] = 0;
+
+    ctx->state[0] = 0x67452301;
+    ctx->state[1] = 0xEFCDAB89;
+    ctx->state[2] = 0x98BADCFE;
+    ctx->state[3] = 0x10325476;
+    ctx->state[4] = 0xC3D2E1F0;
+}
+
+static void sha1_process( sha1_context *ctx, const unsigned char data[64] )
+{
+    unsigned long temp, W[16], A, B, C, D, E;
+
+    GET_ULONG_BE( W[ 0], data,  0 );
+    GET_ULONG_BE( W[ 1], data,  4 );
+    GET_ULONG_BE( W[ 2], data,  8 );
+    GET_ULONG_BE( W[ 3], data, 12 );
+    GET_ULONG_BE( W[ 4], data, 16 );
+    GET_ULONG_BE( W[ 5], data, 20 );
+    GET_ULONG_BE( W[ 6], data, 24 );
+    GET_ULONG_BE( W[ 7], data, 28 );
+    GET_ULONG_BE( W[ 8], data, 32 );
+    GET_ULONG_BE( W[ 9], data, 36 );
+    GET_ULONG_BE( W[10], data, 40 );
+    GET_ULONG_BE( W[11], data, 44 );
+    GET_ULONG_BE( W[12], data, 48 );
+    GET_ULONG_BE( W[13], data, 52 );
+    GET_ULONG_BE( W[14], data, 56 );
+    GET_ULONG_BE( W[15], data, 60 );
+
+#define S(x,n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n)))
+
+#define R(t)                                            \
+(                                                       \
+    temp = W[(t -  3) & 0x0F] ^ W[(t - 8) & 0x0F] ^     \
+           W[(t - 14) & 0x0F] ^ W[ t      & 0x0F],      \
+    ( W[t & 0x0F] = S(temp,1) )                         \
+)
+
+#define P(a,b,c,d,e,x)                                  \
+{                                                       \
+    e += S(a,5) + F(b,c,d) + K + x; b = S(b,30);        \
+}
+
+    A = ctx->state[0];
+    B = ctx->state[1];
+    C = ctx->state[2];
+    D = ctx->state[3];
+    E = ctx->state[4];
+
+#define F(x,y,z) (z ^ (x & (y ^ z)))
+#define K 0x5A827999
+
+    P( A, B, C, D, E, W[0]  );
+    P( E, A, B, C, D, W[1]  );
+    P( D, E, A, B, C, W[2]  );
+    P( C, D, E, A, B, W[3]  );
+    P( B, C, D, E, A, W[4]  );
+    P( A, B, C, D, E, W[5]  );
+    P( E, A, B, C, D, W[6]  );
+    P( D, E, A, B, C, W[7]  );
+    P( C, D, E, A, B, W[8]  );
+    P( B, C, D, E, A, W[9]  );
+    P( A, B, C, D, E, W[10] );
+    P( E, A, B, C, D, W[11] );
+    P( D, E, A, B, C, W[12] );
+    P( C, D, E, A, B, W[13] );
+    P( B, C, D, E, A, W[14] );
+    P( A, B, C, D, E, W[15] );
+    P( E, A, B, C, D, R(16) );
+    P( D, E, A, B, C, R(17) );
+    P( C, D, E, A, B, R(18) );
+    P( B, C, D, E, A, R(19) );
+
+#undef K
+#undef F
+
+#define F(x,y,z) (x ^ y ^ z)
+#define K 0x6ED9EBA1
+
+    P( A, B, C, D, E, R(20) );
+    P( E, A, B, C, D, R(21) );
+    P( D, E, A, B, C, R(22) );
+    P( C, D, E, A, B, R(23) );
+    P( B, C, D, E, A, R(24) );
+    P( A, B, C, D, E, R(25) );
+    P( E, A, B, C, D, R(26) );
+    P( D, E, A, B, C, R(27) );
+    P( C, D, E, A, B, R(28) );
+    P( B, C, D, E, A, R(29) );
+    P( A, B, C, D, E, R(30) );
+    P( E, A, B, C, D, R(31) );
+    P( D, E, A, B, C, R(32) );
+    P( C, D, E, A, B, R(33) );
+    P( B, C, D, E, A, R(34) );
+    P( A, B, C, D, E, R(35) );
+    P( E, A, B, C, D, R(36) );
+    P( D, E, A, B, C, R(37) );
+    P( C, D, E, A, B, R(38) );
+    P( B, C, D, E, A, R(39) );
+
+#undef K
+#undef F
+
+#define F(x,y,z) ((x & y) | (z & (x | y)))
+#define K 0x8F1BBCDC
+
+    P( A, B, C, D, E, R(40) );
+    P( E, A, B, C, D, R(41) );
+    P( D, E, A, B, C, R(42) );
+    P( C, D, E, A, B, R(43) );
+    P( B, C, D, E, A, R(44) );
+    P( A, B, C, D, E, R(45) );
+    P( E, A, B, C, D, R(46) );
+    P( D, E, A, B, C, R(47) );
+    P( C, D, E, A, B, R(48) );
+    P( B, C, D, E, A, R(49) );
+    P( A, B, C, D, E, R(50) );
+    P( E, A, B, C, D, R(51) );
+    P( D, E, A, B, C, R(52) );
+    P( C, D, E, A, B, R(53) );
+    P( B, C, D, E, A, R(54) );
+    P( A, B, C, D, E, R(55) );
+    P( E, A, B, C, D, R(56) );
+    P( D, E, A, B, C, R(57) );
+    P( C, D, E, A, B, R(58) );
+    P( B, C, D, E, A, R(59) );
+
+#undef K
+#undef F
+
+#define F(x,y,z) (x ^ y ^ z)
+#define K 0xCA62C1D6
+
+    P( A, B, C, D, E, R(60) );
+    P( E, A, B, C, D, R(61) );
+    P( D, E, A, B, C, R(62) );
+    P( C, D, E, A, B, R(63) );
+    P( B, C, D, E, A, R(64) );
+    P( A, B, C, D, E, R(65) );
+    P( E, A, B, C, D, R(66) );
+    P( D, E, A, B, C, R(67) );
+    P( C, D, E, A, B, R(68) );
+    P( B, C, D, E, A, R(69) );
+    P( A, B, C, D, E, R(70) );
+    P( E, A, B, C, D, R(71) );
+    P( D, E, A, B, C, R(72) );
+    P( C, D, E, A, B, R(73) );
+    P( B, C, D, E, A, R(74) );
+    P( A, B, C, D, E, R(75) );
+    P( E, A, B, C, D, R(76) );
+    P( D, E, A, B, C, R(77) );
+    P( C, D, E, A, B, R(78) );
+    P( B, C, D, E, A, R(79) );
+
+#undef K
+#undef F
+
+    ctx->state[0] += A;
+    ctx->state[1] += B;
+    ctx->state[2] += C;
+    ctx->state[3] += D;
+    ctx->state[4] += E;
+}
+
+/*
+ * SHA-1 process buffer
+ */
+void sha1_update( sha1_context *ctx, const unsigned char *input, int ilen )
+{
+    int fill;
+    unsigned long left;
+
+    if( ilen <= 0 )
+        return;
+
+    left = ctx->total[0] & 0x3F;
+    fill = 64 - left;
+
+    ctx->total[0] += ilen;
+    ctx->total[0] &= 0xFFFFFFFF;
+
+    if( ctx->total[0] < (unsigned long) ilen )
+        ctx->total[1]++;
+
+    if( left && ilen >= fill )
+    {
+        memcpy( (void *) (ctx->buffer + left),
+                (void *) input, fill );
+        sha1_process( ctx, ctx->buffer );
+        input += fill;
+        ilen  -= fill;
+        left = 0;
+    }
+
+    while( ilen >= 64 )
+    {
+        sha1_process( ctx, input );
+        input += 64;
+        ilen  -= 64;
+    }
+
+    if( ilen > 0 )
+    {
+        memcpy( (void *) (ctx->buffer + left),
+                (void *) input, ilen );
+    }
+}
+
+static const unsigned char sha1_padding[64] =
+{
+ 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
+};
+
+/*
+ * SHA-1 final digest
+ */
+void sha1_finish( sha1_context *ctx, unsigned char output[20] )
+{
+    unsigned long last, padn;
+    unsigned long high, low;
+    unsigned char msglen[8];
+
+    high = ( ctx->total[0] >> 29 )
+         | ( ctx->total[1] <<  3 );
+    low  = ( ctx->total[0] <<  3 );
+
+    PUT_ULONG_BE( high, msglen, 0 );
+    PUT_ULONG_BE( low,  msglen, 4 );
+
+    last = ctx->total[0] & 0x3F;
+    padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last );
+
+    sha1_update( ctx, (unsigned char *) sha1_padding, padn );
+    sha1_update( ctx, msglen, 8 );
+
+    PUT_ULONG_BE( ctx->state[0], output,  0 );
+    PUT_ULONG_BE( ctx->state[1], output,  4 );
+    PUT_ULONG_BE( ctx->state[2], output,  8 );
+    PUT_ULONG_BE( ctx->state[3], output, 12 );
+    PUT_ULONG_BE( ctx->state[4], output, 16 );
+}
+
+/*
+ * output = SHA-1( input buffer )
+ */
+void sha1( const unsigned char *input, int ilen, unsigned char output[20] )
+{
+    sha1_context ctx;
+
+    sha1_starts( &ctx );
+    sha1_update( &ctx, input, ilen );
+    sha1_finish( &ctx, output );
+
+    memset( &ctx, 0, sizeof( sha1_context ) );
+}
+
+/*
+ * output = SHA-1( file contents )
+ */
+int sha1_file( const char *path, unsigned char output[20] )
+{
+    FILE *f;
+    size_t n;
+    sha1_context ctx;
+    unsigned char buf[1024];
+
+    if( ( f = fopen( path, "rb" ) ) == NULL )
+        return( 1 );
+
+    sha1_starts( &ctx );
+
+    while( ( n = fread( buf, 1, sizeof( buf ), f ) ) > 0 )
+        sha1_update( &ctx, buf, (int) n );
+
+    sha1_finish( &ctx, output );
+
+    memset( &ctx, 0, sizeof( sha1_context ) );
+
+    if( ferror( f ) != 0 )
+    {
+        fclose( f );
+        return( 2 );
+    }
+
+    fclose( f );
+    return( 0 );
+}
+
+/*
+ * SHA-1 HMAC context setup
+ */
+void sha1_hmac_starts( sha1_context *ctx, const unsigned char *key, int keylen )
+{
+    int i;
+    unsigned char sum[20];
+
+    if( keylen > 64 )
+    {
+        sha1( key, keylen, sum );
+        keylen = 20;
+        key = sum;
+    }
+
+    memset( ctx->ipad, 0x36, 64 );
+    memset( ctx->opad, 0x5C, 64 );
+
+    for( i = 0; i < keylen; i++ )
+    {
+        ctx->ipad[i] = (unsigned char)( ctx->ipad[i] ^ key[i] );
+        ctx->opad[i] = (unsigned char)( ctx->opad[i] ^ key[i] );
+    }
+
+    sha1_starts( ctx );
+    sha1_update( ctx, ctx->ipad, 64 );
+
+    memset( sum, 0, sizeof( sum ) );
+}
+
+/*
+ * SHA-1 HMAC process buffer
+ */
+void sha1_hmac_update( sha1_context *ctx, const unsigned char *input, int ilen )
+{
+    sha1_update( ctx, input, ilen );
+}
+
+/*
+ * SHA-1 HMAC final digest
+ */
+void sha1_hmac_finish( sha1_context *ctx, unsigned char output[20] )
+{
+    unsigned char tmpbuf[20];
+
+    sha1_finish( ctx, tmpbuf );
+    sha1_starts( ctx );
+    sha1_update( ctx, ctx->opad, 64 );
+    sha1_update( ctx, tmpbuf, 20 );
+    sha1_finish( ctx, output );
+
+    memset( tmpbuf, 0, sizeof( tmpbuf ) );
+}
+
+/*
+ * SHA1 HMAC context reset
+ */
+void sha1_hmac_reset( sha1_context *ctx )
+{
+    sha1_starts( ctx );
+    sha1_update( ctx, ctx->ipad, 64 );
+}
+
+/*
+ * output = HMAC-SHA-1( hmac key, input buffer )
+ */
+void sha1_hmac( const unsigned char *key, int keylen,
+                const unsigned char *input, int ilen,
+                unsigned char output[20] )
+{
+    sha1_context ctx;
+
+    sha1_hmac_starts( &ctx, key, keylen );
+    sha1_hmac_update( &ctx, input, ilen );
+    sha1_hmac_finish( &ctx, output );
+
+    memset( &ctx, 0, sizeof( sha1_context ) );
+}
diff --git a/src/pdkim/sha1.h b/src/pdkim/sha1.h
new file mode 100644 (file)
index 0000000..29a9070
--- /dev/null
@@ -0,0 +1,145 @@
+/**
+ * \file sha1.h
+ *
+ *  Copyright (C) 2006-2010, Brainspark B.V.
+ *
+ *  This file is part of PolarSSL (http://www.polarssl.org)
+ *  Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
+ *
+ *  All rights reserved.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License along
+ *  with this program; if not, write to the Free Software Foundation, Inc.,
+ *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef POLARSSL_SHA1_H
+#define POLARSSL_SHA1_H
+
+/**
+ * \brief          SHA-1 context structure
+ */
+#ifndef HAVE_SHA1_CONTEXT
+#define HAVE_SHA1_CONTEXT
+typedef struct sha1_context sha1_context;
+#endif
+
+struct sha1_context
+{
+    unsigned long total[2];     /*!< number of bytes processed  */
+    unsigned long state[5];     /*!< intermediate digest state  */
+    unsigned char buffer[64];   /*!< data block being processed */
+
+    unsigned char ipad[64];     /*!< HMAC: inner padding        */
+    unsigned char opad[64];     /*!< HMAC: outer padding        */
+};
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * \brief          SHA-1 context setup
+ *
+ * \param ctx      context to be initialized
+ */
+void sha1_starts( sha1_context *ctx );
+
+/**
+ * \brief          SHA-1 process buffer
+ *
+ * \param ctx      SHA-1 context
+ * \param input    buffer holding the  data
+ * \param ilen     length of the input data
+ */
+void sha1_update( sha1_context *ctx, const unsigned char *input, int ilen );
+
+/**
+ * \brief          SHA-1 final digest
+ *
+ * \param ctx      SHA-1 context
+ * \param output   SHA-1 checksum result
+ */
+void sha1_finish( sha1_context *ctx, unsigned char output[20] );
+
+/**
+ * \brief          Output = SHA-1( input buffer )
+ *
+ * \param input    buffer holding the  data
+ * \param ilen     length of the input data
+ * \param output   SHA-1 checksum result
+ */
+void sha1( const unsigned char *input, int ilen, unsigned char output[20] );
+
+/**
+ * \brief          Output = SHA-1( file contents )
+ *
+ * \param path     input file name
+ * \param output   SHA-1 checksum result
+ *
+ * \return         0 if successful, 1 if fopen failed,
+ *                 or 2 if fread failed
+ */
+int sha1_file( const char *path, unsigned char output[20] );
+
+/**
+ * \brief          SHA-1 HMAC context setup
+ *
+ * \param ctx      HMAC context to be initialized
+ * \param key      HMAC secret key
+ * \param keylen   length of the HMAC key
+ */
+void sha1_hmac_starts( sha1_context *ctx, const unsigned char *key, int keylen );
+
+/**
+ * \brief          SHA-1 HMAC process buffer
+ *
+ * \param ctx      HMAC context
+ * \param input    buffer holding the  data
+ * \param ilen     length of the input data
+ */
+void sha1_hmac_update( sha1_context *ctx, const unsigned char *input, int ilen );
+
+/**
+ * \brief          SHA-1 HMAC final digest
+ *
+ * \param ctx      HMAC context
+ * \param output   SHA-1 HMAC checksum result
+ */
+void sha1_hmac_finish( sha1_context *ctx, unsigned char output[20] );
+
+/**
+ * \brief          SHA-1 HMAC context reset
+ *
+ * \param ctx      HMAC context to be reset
+ */
+void sha1_hmac_reset( sha1_context *ctx );
+
+/**
+ * \brief          Output = HMAC-SHA-1( hmac key, input buffer )
+ *
+ * \param key      HMAC secret key
+ * \param keylen   length of the HMAC key
+ * \param input    buffer holding the  data
+ * \param ilen     length of the input data
+ * \param output   HMAC-SHA-1 result
+ */
+void sha1_hmac( const unsigned char *key, int keylen,
+                const unsigned char *input, int ilen,
+                unsigned char output[20] );
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* sha1.h */
diff --git a/src/pdkim/sha2.c b/src/pdkim/sha2.c
new file mode 100644 (file)
index 0000000..e0beaf9
--- /dev/null
@@ -0,0 +1,441 @@
+/*
+ *  FIPS-180-2 compliant SHA-256 implementation
+ *
+ *  Copyright (C) 2006-2010, Brainspark B.V.
+ *
+ *  This file is part of PolarSSL (http://www.polarssl.org)
+ *  Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
+ *
+ *  All rights reserved.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License along
+ *  with this program; if not, write to the Free Software Foundation, Inc.,
+ *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+/*
+ *  The SHA-256 Secure Hash Standard was published by NIST in 2002.
+ *
+ *  http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf
+ */
+
+#include "sha2.h"
+
+#include <string.h>
+#include <stdio.h>
+
+/*
+ * 32-bit integer manipulation macros (big endian)
+ */
+#ifndef GET_ULONG_BE
+#define GET_ULONG_BE(n,b,i)                             \
+{                                                       \
+    (n) = ( (unsigned long) (b)[(i)    ] << 24 )        \
+        | ( (unsigned long) (b)[(i) + 1] << 16 )        \
+        | ( (unsigned long) (b)[(i) + 2] <<  8 )        \
+        | ( (unsigned long) (b)[(i) + 3]       );       \
+}
+#endif
+
+#ifndef PUT_ULONG_BE
+#define PUT_ULONG_BE(n,b,i)                             \
+{                                                       \
+    (b)[(i)    ] = (unsigned char) ( (n) >> 24 );       \
+    (b)[(i) + 1] = (unsigned char) ( (n) >> 16 );       \
+    (b)[(i) + 2] = (unsigned char) ( (n) >>  8 );       \
+    (b)[(i) + 3] = (unsigned char) ( (n)       );       \
+}
+#endif
+
+/*
+ * SHA-256 context setup
+ */
+void sha2_starts( sha2_context *ctx, int is224 )
+{
+    ctx->total[0] = 0;
+    ctx->total[1] = 0;
+
+    if( is224 == 0 )
+    {
+        /* SHA-256 */
+        ctx->state[0] = 0x6A09E667;
+        ctx->state[1] = 0xBB67AE85;
+        ctx->state[2] = 0x3C6EF372;
+        ctx->state[3] = 0xA54FF53A;
+        ctx->state[4] = 0x510E527F;
+        ctx->state[5] = 0x9B05688C;
+        ctx->state[6] = 0x1F83D9AB;
+        ctx->state[7] = 0x5BE0CD19;
+    }
+    else
+    {
+        /* SHA-224 */
+        ctx->state[0] = 0xC1059ED8;
+        ctx->state[1] = 0x367CD507;
+        ctx->state[2] = 0x3070DD17;
+        ctx->state[3] = 0xF70E5939;
+        ctx->state[4] = 0xFFC00B31;
+        ctx->state[5] = 0x68581511;
+        ctx->state[6] = 0x64F98FA7;
+        ctx->state[7] = 0xBEFA4FA4;
+    }
+
+    ctx->is224 = is224;
+}
+
+static void sha2_process( sha2_context *ctx, const unsigned char data[64] )
+{
+    unsigned long temp1, temp2, W[64];
+    unsigned long A, B, C, D, E, F, G, H;
+
+    GET_ULONG_BE( W[ 0], data,  0 );
+    GET_ULONG_BE( W[ 1], data,  4 );
+    GET_ULONG_BE( W[ 2], data,  8 );
+    GET_ULONG_BE( W[ 3], data, 12 );
+    GET_ULONG_BE( W[ 4], data, 16 );
+    GET_ULONG_BE( W[ 5], data, 20 );
+    GET_ULONG_BE( W[ 6], data, 24 );
+    GET_ULONG_BE( W[ 7], data, 28 );
+    GET_ULONG_BE( W[ 8], data, 32 );
+    GET_ULONG_BE( W[ 9], data, 36 );
+    GET_ULONG_BE( W[10], data, 40 );
+    GET_ULONG_BE( W[11], data, 44 );
+    GET_ULONG_BE( W[12], data, 48 );
+    GET_ULONG_BE( W[13], data, 52 );
+    GET_ULONG_BE( W[14], data, 56 );
+    GET_ULONG_BE( W[15], data, 60 );
+
+#define  SHR(x,n) ((x & 0xFFFFFFFF) >> n)
+#define ROTR(x,n) (SHR(x,n) | (x << (32 - n)))
+
+#define S0(x) (ROTR(x, 7) ^ ROTR(x,18) ^  SHR(x, 3))
+#define S1(x) (ROTR(x,17) ^ ROTR(x,19) ^  SHR(x,10))
+
+#define S2(x) (ROTR(x, 2) ^ ROTR(x,13) ^ ROTR(x,22))
+#define S3(x) (ROTR(x, 6) ^ ROTR(x,11) ^ ROTR(x,25))
+
+#define F0(x,y,z) ((x & y) | (z & (x | y)))
+#define F1(x,y,z) (z ^ (x & (y ^ z)))
+
+#define R(t)                                    \
+(                                               \
+    W[t] = S1(W[t -  2]) + W[t -  7] +          \
+           S0(W[t - 15]) + W[t - 16]            \
+)
+
+#define P(a,b,c,d,e,f,g,h,x,K)                  \
+{                                               \
+    temp1 = h + S3(e) + F1(e,f,g) + K + x;      \
+    temp2 = S2(a) + F0(a,b,c);                  \
+    d += temp1; h = temp1 + temp2;              \
+}
+
+    A = ctx->state[0];
+    B = ctx->state[1];
+    C = ctx->state[2];
+    D = ctx->state[3];
+    E = ctx->state[4];
+    F = ctx->state[5];
+    G = ctx->state[6];
+    H = ctx->state[7];
+
+    P( A, B, C, D, E, F, G, H, W[ 0], 0x428A2F98 );
+    P( H, A, B, C, D, E, F, G, W[ 1], 0x71374491 );
+    P( G, H, A, B, C, D, E, F, W[ 2], 0xB5C0FBCF );
+    P( F, G, H, A, B, C, D, E, W[ 3], 0xE9B5DBA5 );
+    P( E, F, G, H, A, B, C, D, W[ 4], 0x3956C25B );
+    P( D, E, F, G, H, A, B, C, W[ 5], 0x59F111F1 );
+    P( C, D, E, F, G, H, A, B, W[ 6], 0x923F82A4 );
+    P( B, C, D, E, F, G, H, A, W[ 7], 0xAB1C5ED5 );
+    P( A, B, C, D, E, F, G, H, W[ 8], 0xD807AA98 );
+    P( H, A, B, C, D, E, F, G, W[ 9], 0x12835B01 );
+    P( G, H, A, B, C, D, E, F, W[10], 0x243185BE );
+    P( F, G, H, A, B, C, D, E, W[11], 0x550C7DC3 );
+    P( E, F, G, H, A, B, C, D, W[12], 0x72BE5D74 );
+    P( D, E, F, G, H, A, B, C, W[13], 0x80DEB1FE );
+    P( C, D, E, F, G, H, A, B, W[14], 0x9BDC06A7 );
+    P( B, C, D, E, F, G, H, A, W[15], 0xC19BF174 );
+    P( A, B, C, D, E, F, G, H, R(16), 0xE49B69C1 );
+    P( H, A, B, C, D, E, F, G, R(17), 0xEFBE4786 );
+    P( G, H, A, B, C, D, E, F, R(18), 0x0FC19DC6 );
+    P( F, G, H, A, B, C, D, E, R(19), 0x240CA1CC );
+    P( E, F, G, H, A, B, C, D, R(20), 0x2DE92C6F );
+    P( D, E, F, G, H, A, B, C, R(21), 0x4A7484AA );
+    P( C, D, E, F, G, H, A, B, R(22), 0x5CB0A9DC );
+    P( B, C, D, E, F, G, H, A, R(23), 0x76F988DA );
+    P( A, B, C, D, E, F, G, H, R(24), 0x983E5152 );
+    P( H, A, B, C, D, E, F, G, R(25), 0xA831C66D );
+    P( G, H, A, B, C, D, E, F, R(26), 0xB00327C8 );
+    P( F, G, H, A, B, C, D, E, R(27), 0xBF597FC7 );
+    P( E, F, G, H, A, B, C, D, R(28), 0xC6E00BF3 );
+    P( D, E, F, G, H, A, B, C, R(29), 0xD5A79147 );
+    P( C, D, E, F, G, H, A, B, R(30), 0x06CA6351 );
+    P( B, C, D, E, F, G, H, A, R(31), 0x14292967 );
+    P( A, B, C, D, E, F, G, H, R(32), 0x27B70A85 );
+    P( H, A, B, C, D, E, F, G, R(33), 0x2E1B2138 );
+    P( G, H, A, B, C, D, E, F, R(34), 0x4D2C6DFC );
+    P( F, G, H, A, B, C, D, E, R(35), 0x53380D13 );
+    P( E, F, G, H, A, B, C, D, R(36), 0x650A7354 );
+    P( D, E, F, G, H, A, B, C, R(37), 0x766A0ABB );
+    P( C, D, E, F, G, H, A, B, R(38), 0x81C2C92E );
+    P( B, C, D, E, F, G, H, A, R(39), 0x92722C85 );
+    P( A, B, C, D, E, F, G, H, R(40), 0xA2BFE8A1 );
+    P( H, A, B, C, D, E, F, G, R(41), 0xA81A664B );
+    P( G, H, A, B, C, D, E, F, R(42), 0xC24B8B70 );
+    P( F, G, H, A, B, C, D, E, R(43), 0xC76C51A3 );
+    P( E, F, G, H, A, B, C, D, R(44), 0xD192E819 );
+    P( D, E, F, G, H, A, B, C, R(45), 0xD6990624 );
+    P( C, D, E, F, G, H, A, B, R(46), 0xF40E3585 );
+    P( B, C, D, E, F, G, H, A, R(47), 0x106AA070 );
+    P( A, B, C, D, E, F, G, H, R(48), 0x19A4C116 );
+    P( H, A, B, C, D, E, F, G, R(49), 0x1E376C08 );
+    P( G, H, A, B, C, D, E, F, R(50), 0x2748774C );
+    P( F, G, H, A, B, C, D, E, R(51), 0x34B0BCB5 );
+    P( E, F, G, H, A, B, C, D, R(52), 0x391C0CB3 );
+    P( D, E, F, G, H, A, B, C, R(53), 0x4ED8AA4A );
+    P( C, D, E, F, G, H, A, B, R(54), 0x5B9CCA4F );
+    P( B, C, D, E, F, G, H, A, R(55), 0x682E6FF3 );
+    P( A, B, C, D, E, F, G, H, R(56), 0x748F82EE );
+    P( H, A, B, C, D, E, F, G, R(57), 0x78A5636F );
+    P( G, H, A, B, C, D, E, F, R(58), 0x84C87814 );
+    P( F, G, H, A, B, C, D, E, R(59), 0x8CC70208 );
+    P( E, F, G, H, A, B, C, D, R(60), 0x90BEFFFA );
+    P( D, E, F, G, H, A, B, C, R(61), 0xA4506CEB );
+    P( C, D, E, F, G, H, A, B, R(62), 0xBEF9A3F7 );
+    P( B, C, D, E, F, G, H, A, R(63), 0xC67178F2 );
+
+    ctx->state[0] += A;
+    ctx->state[1] += B;
+    ctx->state[2] += C;
+    ctx->state[3] += D;
+    ctx->state[4] += E;
+    ctx->state[5] += F;
+    ctx->state[6] += G;
+    ctx->state[7] += H;
+}
+
+/*
+ * SHA-256 process buffer
+ */
+void sha2_update( sha2_context *ctx, const unsigned char *input, int ilen )
+{
+    int fill;
+    unsigned long left;
+
+    if( ilen <= 0 )
+        return;
+
+    left = ctx->total[0] & 0x3F;
+    fill = 64 - left;
+
+    ctx->total[0] += ilen;
+    ctx->total[0] &= 0xFFFFFFFF;
+
+    if( ctx->total[0] < (unsigned long) ilen )
+        ctx->total[1]++;
+
+    if( left && ilen >= fill )
+    {
+        memcpy( (void *) (ctx->buffer + left),
+                (void *) input, fill );
+        sha2_process( ctx, ctx->buffer );
+        input += fill;
+        ilen  -= fill;
+        left = 0;
+    }
+
+    while( ilen >= 64 )
+    {
+        sha2_process( ctx, input );
+        input += 64;
+        ilen  -= 64;
+    }
+
+    if( ilen > 0 )
+    {
+        memcpy( (void *) (ctx->buffer + left),
+                (void *) input, ilen );
+    }
+}
+
+static const unsigned char sha2_padding[64] =
+{
+ 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
+};
+
+/*
+ * SHA-256 final digest
+ */
+void sha2_finish( sha2_context *ctx, unsigned char output[32] )
+{
+    unsigned long last, padn;
+    unsigned long high, low;
+    unsigned char msglen[8];
+
+    high = ( ctx->total[0] >> 29 )
+         | ( ctx->total[1] <<  3 );
+    low  = ( ctx->total[0] <<  3 );
+
+    PUT_ULONG_BE( high, msglen, 0 );
+    PUT_ULONG_BE( low,  msglen, 4 );
+
+    last = ctx->total[0] & 0x3F;
+    padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last );
+
+    sha2_update( ctx, (unsigned char *) sha2_padding, padn );
+    sha2_update( ctx, msglen, 8 );
+
+    PUT_ULONG_BE( ctx->state[0], output,  0 );
+    PUT_ULONG_BE( ctx->state[1], output,  4 );
+    PUT_ULONG_BE( ctx->state[2], output,  8 );
+    PUT_ULONG_BE( ctx->state[3], output, 12 );
+    PUT_ULONG_BE( ctx->state[4], output, 16 );
+    PUT_ULONG_BE( ctx->state[5], output, 20 );
+    PUT_ULONG_BE( ctx->state[6], output, 24 );
+
+    if( ctx->is224 == 0 )
+        PUT_ULONG_BE( ctx->state[7], output, 28 );
+}
+
+/*
+ * output = SHA-256( input buffer )
+ */
+void sha2( const unsigned char *input, int ilen,
+           unsigned char output[32], int is224 )
+{
+    sha2_context ctx;
+
+    sha2_starts( &ctx, is224 );
+    sha2_update( &ctx, input, ilen );
+    sha2_finish( &ctx, output );
+
+    memset( &ctx, 0, sizeof( sha2_context ) );
+}
+
+/*
+ * output = SHA-256( file contents )
+ */
+int sha2_file( const char *path, unsigned char output[32], int is224 )
+{
+    FILE *f;
+    size_t n;
+    sha2_context ctx;
+    unsigned char buf[1024];
+
+    if( ( f = fopen( path, "rb" ) ) == NULL )
+        return( 1 );
+
+    sha2_starts( &ctx, is224 );
+
+    while( ( n = fread( buf, 1, sizeof( buf ), f ) ) > 0 )
+        sha2_update( &ctx, buf, (int) n );
+
+    sha2_finish( &ctx, output );
+
+    memset( &ctx, 0, sizeof( sha2_context ) );
+
+    if( ferror( f ) != 0 )
+    {
+        fclose( f );
+        return( 2 );
+    }
+
+    fclose( f );
+    return( 0 );
+}
+
+/*
+ * SHA-256 HMAC context setup
+ */
+void sha2_hmac_starts( sha2_context *ctx, const unsigned char *key, int keylen,
+                       int is224 )
+{
+    int i;
+    unsigned char sum[32];
+
+    if( keylen > 64 )
+    {
+        sha2( key, keylen, sum, is224 );
+        keylen = ( is224 ) ? 28 : 32;
+        key = sum;
+    }
+
+    memset( ctx->ipad, 0x36, 64 );
+    memset( ctx->opad, 0x5C, 64 );
+
+    for( i = 0; i < keylen; i++ )
+    {
+        ctx->ipad[i] = (unsigned char)( ctx->ipad[i] ^ key[i] );
+        ctx->opad[i] = (unsigned char)( ctx->opad[i] ^ key[i] );
+    }
+
+    sha2_starts( ctx, is224 );
+    sha2_update( ctx, ctx->ipad, 64 );
+
+    memset( sum, 0, sizeof( sum ) );
+}
+
+/*
+ * SHA-256 HMAC process buffer
+ */
+void sha2_hmac_update( sha2_context *ctx, const unsigned char *input, int ilen )
+{
+    sha2_update( ctx, input, ilen );
+}
+
+/*
+ * SHA-256 HMAC final digest
+ */
+void sha2_hmac_finish( sha2_context *ctx, unsigned char output[32] )
+{
+    int is224, hlen;
+    unsigned char tmpbuf[32];
+
+    is224 = ctx->is224;
+    hlen = ( is224 == 0 ) ? 32 : 28;
+
+    sha2_finish( ctx, tmpbuf );
+    sha2_starts( ctx, is224 );
+    sha2_update( ctx, ctx->opad, 64 );
+    sha2_update( ctx, tmpbuf, hlen );
+    sha2_finish( ctx, output );
+
+    memset( tmpbuf, 0, sizeof( tmpbuf ) );
+}
+
+/*
+ * SHA-256 HMAC context reset
+ */
+void sha2_hmac_reset( sha2_context *ctx )
+{
+    sha2_starts( ctx, ctx->is224 );
+    sha2_update( ctx, ctx->ipad, 64 );
+}
+
+/*
+ * output = HMAC-SHA-256( hmac key, input buffer )
+ */
+void sha2_hmac( const unsigned char *key, int keylen,
+                const unsigned char *input, int ilen,
+                unsigned char output[32], int is224 )
+{
+    sha2_context ctx;
+
+    sha2_hmac_starts( &ctx, key, keylen, is224 );
+    sha2_hmac_update( &ctx, input, ilen );
+    sha2_hmac_finish( &ctx, output );
+
+    memset( &ctx, 0, sizeof( sha2_context ) );
+}
diff --git a/src/pdkim/sha2.h b/src/pdkim/sha2.h
new file mode 100644 (file)
index 0000000..ec2acfa
--- /dev/null
@@ -0,0 +1,153 @@
+/**
+ * \file sha2.h
+ *
+ *  Copyright (C) 2006-2010, Brainspark B.V.
+ *
+ *  This file is part of PolarSSL (http://www.polarssl.org)
+ *  Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
+ *
+ *  All rights reserved.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License along
+ *  with this program; if not, write to the Free Software Foundation, Inc.,
+ *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef POLARSSL_SHA2_H
+#define POLARSSL_SHA2_H
+
+/**
+ * \brief          SHA-256 context structure
+ */
+#ifndef HAVE_SHA2_CONTEXT
+#define HAVE_SHA2_CONTEXT
+typedef struct sha2_context sha2_context;
+#endif
+
+struct sha2_context
+{
+    unsigned long total[2];     /*!< number of bytes processed  */
+    unsigned long state[8];     /*!< intermediate digest state  */
+    unsigned char buffer[64];   /*!< data block being processed */
+
+    unsigned char ipad[64];     /*!< HMAC: inner padding        */
+    unsigned char opad[64];     /*!< HMAC: outer padding        */
+    int is224;                  /*!< 0 => SHA-256, else SHA-224 */
+};
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * \brief          SHA-256 context setup
+ *
+ * \param ctx      context to be initialized
+ * \param is224    0 = use SHA256, 1 = use SHA224
+ */
+void sha2_starts( sha2_context *ctx, int is224 );
+
+/**
+ * \brief          SHA-256 process buffer
+ *
+ * \param ctx      SHA-256 context
+ * \param input    buffer holding the  data
+ * \param ilen     length of the input data
+ */
+void sha2_update( sha2_context *ctx, const unsigned char *input, int ilen );
+
+/**
+ * \brief          SHA-256 final digest
+ *
+ * \param ctx      SHA-256 context
+ * \param output   SHA-224/256 checksum result
+ */
+void sha2_finish( sha2_context *ctx, unsigned char output[32] );
+
+/**
+ * \brief          Output = SHA-256( input buffer )
+ *
+ * \param input    buffer holding the  data
+ * \param ilen     length of the input data
+ * \param output   SHA-224/256 checksum result
+ * \param is224    0 = use SHA256, 1 = use SHA224
+ */
+void sha2( const unsigned char *input, int ilen,
+           unsigned char output[32], int is224 );
+
+/**
+ * \brief          Output = SHA-256( file contents )
+ *
+ * \param path     input file name
+ * \param output   SHA-224/256 checksum result
+ * \param is224    0 = use SHA256, 1 = use SHA224
+ *
+ * \return         0 if successful, 1 if fopen failed,
+ *                 or 2 if fread failed
+ */
+int sha2_file( const char *path, unsigned char output[32], int is224 );
+
+/**
+ * \brief          SHA-256 HMAC context setup
+ *
+ * \param ctx      HMAC context to be initialized
+ * \param key      HMAC secret key
+ * \param keylen   length of the HMAC key
+ * \param is224    0 = use SHA256, 1 = use SHA224
+ */
+void sha2_hmac_starts( sha2_context *ctx, const unsigned char *key, int keylen,
+                       int is224 );
+
+/**
+ * \brief          SHA-256 HMAC process buffer
+ *
+ * \param ctx      HMAC context
+ * \param input    buffer holding the  data
+ * \param ilen     length of the input data
+ */
+void sha2_hmac_update( sha2_context *ctx, const unsigned char *input, int ilen );
+
+/**
+ * \brief          SHA-256 HMAC final digest
+ *
+ * \param ctx      HMAC context
+ * \param output   SHA-224/256 HMAC checksum result
+ */
+void sha2_hmac_finish( sha2_context *ctx, unsigned char output[32] );
+
+/**
+ * \brief          SHA-256 HMAC context reset
+ *
+ * \param ctx      HMAC context to be reset
+ */
+void sha2_hmac_reset( sha2_context *ctx );
+
+/**
+ * \brief          Output = HMAC-SHA-256( hmac key, input buffer )
+ *
+ * \param key      HMAC secret key
+ * \param keylen   length of the HMAC key
+ * \param input    buffer holding the  data
+ * \param ilen     length of the input data
+ * \param output   HMAC-SHA-224/256 result
+ * \param is224    0 = use SHA256, 1 = use SHA224
+ */
+void sha2_hmac( const unsigned char *key, int keylen,
+                const unsigned char *input, int ilen,
+                unsigned char output[32], int is224 );
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* sha2.h */
diff --git a/src/perl.c b/src/perl.c
new file mode 100644 (file)
index 0000000..543b5d2
--- /dev/null
@@ -0,0 +1,190 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) 1998 Malcolm Beattie */
+
+/* Modified by PH to get rid of the "na" usage, March 1999.
+   Modified further by PH for general tidying for Exim 4.
+   Threaded Perl support added by Stefan Traby, Nov 2002
+*/
+
+
+/* This Perl add-on can be distributed under the same terms as Exim itself. */
+/* See the file NOTICE for conditions of use and distribution. */
+
+#include "exim.h"
+
+#define EXIM_TRUE TRUE
+#undef TRUE
+
+#define EXIM_FALSE FALSE
+#undef FALSE
+
+#define EXIM_DEBUG DEBUG
+#undef DEBUG
+
+#include <EXTERN.h>
+#include <perl.h>
+#include <XSUB.h>
+
+#ifndef ERRSV
+#define ERRSV (GvSV(errgv))
+#endif
+
+/* Some people like very old perl versions, so avoid any build side-effects. */
+
+#ifndef pTHX
+# define pTHX
+# define pTHX_
+#endif
+#ifndef EXTERN_C
+# define EXTERN_C extern
+#endif
+
+EXTERN_C void boot_DynaLoader(pTHX_ CV *cv);
+
+
+static PerlInterpreter *interp_perl = 0;
+
+XS(xs_expand_string)
+{
+  dXSARGS;
+  uschar *str;
+  STRLEN len;
+
+  if (items != 1)
+    croak("Usage: Exim::expand_string(string)");
+
+  str = expand_string(US SvPV(ST(0), len));
+  ST(0) = sv_newmortal();
+  if (str != NULL)
+    sv_setpv(ST(0), (const char *) str);
+  else if (!expand_string_forcedfail)
+    croak("syntax error in Exim::expand_string argument: %s",
+      expand_string_message);
+}
+
+XS(xs_debug_write)
+{
+  dXSARGS;
+  STRLEN len;
+  if (items != 1)
+    croak("Usage: Exim::debug_write(string)");
+  debug_printf("%s", US SvPV(ST(0), len));
+}
+
+XS(xs_log_write)
+{
+  dXSARGS;
+  STRLEN len;
+  if (items != 1)
+    croak("Usage: Exim::log_write(string)");
+  log_write(0, LOG_MAIN, "%s", US SvPV(ST(0), len));
+}
+
+static void  xs_init(pTHX)
+{
+  char *file = __FILE__;
+  newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
+  newXS("Exim::expand_string", xs_expand_string, file);
+  newXS("Exim::debug_write", xs_debug_write, file);
+  newXS("Exim::log_write", xs_log_write, file);
+}
+
+uschar *
+init_perl(uschar *startup_code)
+{
+  static int argc = 2;
+  static char *argv[3] = { "exim-perl", "/dev/null", 0 };
+  SV *sv;
+  STRLEN len;
+
+  if (interp_perl) return 0;
+  interp_perl = perl_alloc();
+  perl_construct(interp_perl);
+  perl_parse(interp_perl, xs_init, argc, argv, 0);
+  perl_run(interp_perl);
+    {
+    dSP;
+
+    /*********************************************************************/
+    /* These lines by PH added to make "warn" output go to the Exim log; I
+    hope this doesn't break anything. */
+
+    sv = newSVpv(
+      "$SIG{__WARN__} = sub { my($s) = $_[0];"
+      "$s =~ s/\\n$//;"
+      "Exim::log_write($s) };", 0);
+    PUSHMARK(SP);
+    perl_eval_sv(sv, G_SCALAR|G_DISCARD|G_KEEPERR);
+    SvREFCNT_dec(sv);
+    if (SvTRUE(ERRSV)) return US SvPV(ERRSV, len);
+    /*********************************************************************/
+
+    sv = newSVpv(CS startup_code, 0);
+    PUSHMARK(SP);
+    perl_eval_sv(sv, G_SCALAR|G_DISCARD|G_KEEPERR);
+    SvREFCNT_dec(sv);
+    if (SvTRUE(ERRSV)) return US SvPV(ERRSV, len);
+
+    setlocale(LC_ALL, "C");    /* In case it got changed */
+    return NULL;
+    }
+}
+
+void
+cleanup_perl(void)
+{
+  if (!interp_perl)
+    return;
+  perl_destruct(interp_perl);
+  perl_free(interp_perl);
+  interp_perl = 0;
+}
+
+uschar *
+call_perl_cat(uschar *yield, int *sizep, int *ptrp, uschar **errstrp,
+  uschar *name, uschar **arg)
+{
+  dSP;
+  SV *sv;
+  STRLEN len;
+  uschar *str;
+  int items;
+
+  if (!interp_perl)
+    {
+    *errstrp = US"the Perl interpreter has not been started";
+    return 0;
+    }
+
+  ENTER;
+  SAVETMPS;
+  PUSHMARK(SP);
+  while (*arg != NULL) XPUSHs(newSVpv(CS (*arg++), 0));
+  PUTBACK;
+  items = perl_call_pv(CS name, G_SCALAR|G_EVAL);
+  SPAGAIN;
+  sv = POPs;
+  PUTBACK;
+  if (SvTRUE(ERRSV))
+    {
+    *errstrp = US SvPV(ERRSV, len);
+    return NULL;
+    }
+  if (!SvOK(sv))
+    {
+    *errstrp = 0;
+    return NULL;
+    }
+  str = US SvPV(sv, len);
+  yield = string_cat(yield, sizep, ptrp, str, (int)len);
+  FREETMPS;
+  LEAVE;
+
+  setlocale(LC_ALL, "C");    /* In case it got changed */
+  return yield;
+}
+
+/* End of perl.c */
diff --git a/src/queue.c b/src/queue.c
new file mode 100644 (file)
index 0000000..8876e09
--- /dev/null
@@ -0,0 +1,1376 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Functions that operate on the input queue. */
+
+
+#include "exim.h"
+
+
+
+/* The number of nodes to use for the bottom-up merge sort when a list of queue
+items is to be ordered. The code for this sort was contributed as a patch by
+Michael Haardt. */
+
+#define LOG2_MAXNODES 32
+
+
+
+/*************************************************
+*  Helper sort function for queue_get_spool_list *
+*************************************************/
+
+/* This function is used when sorting the queue list in the function
+queue_get_spool_list() below.
+
+Arguments:
+  a            points to an ordered list of queue_filename items
+  b            points to another ordered list
+
+Returns:       a pointer to a merged ordered list
+*/
+
+static queue_filename *
+merge_queue_lists(queue_filename *a, queue_filename *b)
+{
+queue_filename *first = NULL;
+queue_filename **append = &first;
+
+while (a != NULL && b != NULL)
+  {
+  if (Ustrcmp(a->text, b->text) < 0)
+    {
+    *append = a;
+    append= &a->next;
+    a = a->next;
+    }
+  else
+    {
+    *append = b;
+    append= &b->next;
+    b = b->next;
+    }
+  }
+
+*append=((a != NULL)? a : b);
+return first;
+}
+
+
+
+
+
+/*************************************************
+*             Get list of spool files            *
+*************************************************/
+
+/* Scan the spool directory and return a list of the relevant file names
+therein. Single-character sub-directories are handled as follows:
+
+  If the first argument is > 0, a sub-directory is scanned; the letter is
+  taken from the nth entry in subdirs.
+
+  If the first argument is 0, sub-directories are not scanned. However, a
+  list of them is returned.
+
+  If the first argument is < 0, sub-directories are scanned for messages,
+  and a single, unified list is created. The returned data blocks contain the
+  identifying character of the subdirectory, if any. The subdirs vector is
+  still required as an argument.
+
+If the randomize argument is TRUE, messages are returned in "randomized" order.
+Actually, the order is anything but random, but the algorithm is cheap, and the
+point is simply to ensure that the same order doesn't occur every time, in case
+a particular message is causing a remote MTA to barf - we would like to try
+other messages to that MTA first.
+
+If the randomize argument is FALSE, sort the list according to the file name.
+This should give the order in which the messages arrived. It is normally used
+only for presentation to humans, in which case the (possibly expensive) sort
+that it does is not part of the normal operational code. However, if
+queue_run_in_order is set, sorting has to take place for queue runs as well.
+When randomize is FALSE, the first argument is normally -1, so all messages are
+included.
+
+Arguments:
+  subdiroffset   sub-directory character offset, or 0 or -1 (see above)
+  subdirs        vector to store list of subdirchars
+  subcount       pointer to int in which to store count of subdirs
+  randomize      TRUE if the order of the list is to be unpredictable
+
+Returns:         pointer to a chain of queue name items
+*/
+
+static queue_filename *
+queue_get_spool_list(int subdiroffset, uschar *subdirs, int *subcount,
+  BOOL randomize)
+{
+int i;
+int flags = 0;
+int resetflags = -1;
+int subptr;
+queue_filename *yield = NULL;
+queue_filename *last = NULL;
+struct dirent *ent;
+DIR *dd;
+uschar buffer[256];
+queue_filename *root[LOG2_MAXNODES];
+
+/* When randomizing, the file names are added to the start or end of the list
+according to the bits of the flags variable. Get a collection of bits from the
+current time. Use the bottom 16 and just keep re-using them if necessary. When
+not randomizing, initialize the sublists for the bottom-up merge sort. */
+
+if (randomize) resetflags = time(NULL) & 0xFFFF;
+  else for (i = 0; i < LOG2_MAXNODES; i++) root[i] = NULL;
+
+/* If processing the full queue, or just the top-level, start at the base
+directory, and initialize the first subdirectory name (as none). Otherwise,
+start at the sub-directory offset. */
+
+if (subdiroffset <= 0)
+  {
+  i = 0;
+  subdirs[0] = 0;
+  *subcount = 0;
+  }
+else i = subdiroffset;
+
+/* Set up prototype for the directory name. */
+
+sprintf(CS buffer, "%s/input", spool_directory);
+subptr = Ustrlen(buffer);
+buffer[subptr+2] = 0;               /* terminator for lengthened name */
+
+/* This loop runs at least once, for the main or given directory, and then as
+many times as necessary to scan any subdirectories encountered in the main
+directory, if they are to be scanned at this time. */
+
+for (; i <= *subcount; i++)
+  {
+  int count = 0;
+  int subdirchar = subdirs[i];      /* 0 for main directory */
+
+  if (subdirchar != 0)
+    {
+    buffer[subptr] = '/';
+    buffer[subptr+1] = subdirchar;
+    }
+
+  dd = opendir(CS buffer);
+  if (dd == NULL) continue;
+
+  /* Now scan the directory. */
+
+  while ((ent = readdir(dd)) != NULL)
+    {
+    uschar *name = US ent->d_name;
+    int len = Ustrlen(name);
+
+    /* Count entries */
+
+    count++;
+
+    /* If we find a single alphameric sub-directory in the base directory,
+    add it to the list for subsequent scans. */
+
+    if (i == 0 && len == 1 && isalnum(*name))
+      {
+      *subcount = *subcount + 1;
+      subdirs[*subcount] = *name;
+      continue;
+      }
+
+    /* Otherwise, if it is a header spool file, add it to the list */
+
+    if (len == SPOOL_NAME_LENGTH &&
+        Ustrcmp(name + SPOOL_NAME_LENGTH - 2, "-H") == 0)
+      {
+      queue_filename *next =
+        store_get(sizeof(queue_filename) + Ustrlen(name));
+      Ustrcpy(next->text, name);
+      next->dir_uschar = subdirchar;
+
+      /* Handle the creation of a randomized list. The first item becomes both
+      the top and bottom of the list. Subsequent items are inserted either at
+      the top or the bottom, randomly. This is, I argue, faster than doing a
+      sort by allocating a random number to each item, and it also saves having
+      to store the number with each item. */
+
+      if (randomize)
+        {
+        if (yield == NULL)
+          {
+          next->next = NULL;
+          yield = last = next;
+          }
+        else
+          {
+          if (flags == 0) flags = resetflags;
+          if ((flags & 1) == 0)
+            {
+            next->next = yield;
+            yield = next;
+            }
+          else
+            {
+            next->next = NULL;
+            last->next = next;
+            last = next;
+            }
+          flags = flags >> 1;
+          }
+        }
+
+      /* Otherwise do a bottom-up merge sort based on the name. */
+
+      else
+        {
+        int j;
+        next->next = NULL;
+        for (j = 0; j < LOG2_MAXNODES; j++)
+          {
+          if (root[j] != NULL)
+            {
+            next = merge_queue_lists(next, root[j]);
+            root[j] = (j == LOG2_MAXNODES - 1)? next : NULL;
+            }
+          else
+            {
+            root[j] = next;
+            break;
+            }
+          }
+        }
+      }
+    }
+
+  /* Finished with this directory */
+
+  closedir(dd);
+
+  /* If we have just scanned a sub-directory, and it was empty (count == 2
+  implies just "." and ".." entries), and Exim is no longer configured to
+  use sub-directories, attempt to get rid of it. At the same time, try to
+  get rid of any corresponding msglog subdirectory. These are just cosmetic
+  tidying actions, so just ignore failures. If we are scanning just a single
+  sub-directory, break the loop. */
+
+  if (i != 0)
+    {
+    if (!split_spool_directory && count <= 2)
+      {
+      rmdir(CS buffer);
+      sprintf(CS big_buffer, "%s/msglog/%c", spool_directory, subdirchar);
+      rmdir(CS big_buffer);
+      }
+    if (subdiroffset > 0) break;    /* Single sub-directory */
+    }
+
+  /* If we have just scanned the base directory, and subdiroffset is 0,
+  we do not want to continue scanning the sub-directories. */
+
+  else
+    {
+    if (subdiroffset == 0) break;
+    }
+  }    /* Loop for multiple subdirectories */
+
+/* When using a bottom-up merge sort, do the final merging of the sublists.
+Then pass back the final list of file items. */
+
+if (!randomize)
+  for (i = 0; i < LOG2_MAXNODES; ++i)
+    yield = merge_queue_lists(yield, root[i]);
+
+return yield;
+}
+
+
+
+
+/*************************************************
+*              Perform a queue run               *
+*************************************************/
+
+/* The arguments give the messages to start and stop at; NULL means start at
+the beginning or stop at the end. If the given start message doesn't exist, we
+start at the next lexically greater one, and likewise we stop at the after the
+previous lexically lesser one if the given stop message doesn't exist. Because
+a queue run can take some time, stat each file before forking, in case it has
+been delivered in the meantime by some other means.
+
+The global variables queue_run_force and queue_run_local may be set to cause
+forced deliveries or local-only deliveries, respectively.
+
+If deliver_selectstring[_sender] is not NULL, skip messages whose recipients do
+not contain the string. As this option is typically used when a machine comes
+back online, we want to ensure that at least one delivery attempt takes place,
+so force the first one. The selecting string can optionally be a regex, or
+refer to the sender instead of recipients.
+
+If queue_2stage is set, the queue is scanned twice. The first time, queue_smtp
+is set so that routing is done for all messages. Thus in the second run those
+that are routed to the same host should go down the same SMTP connection.
+
+Arguments:
+  start_id   message id to start at, or NULL for all
+  stop_id    message id to end at, or NULL for all
+  recurse    TRUE if recursing for 2-stage run
+
+Returns:     nothing
+*/
+
+void
+queue_run(uschar *start_id, uschar *stop_id, BOOL recurse)
+{
+BOOL force_delivery = queue_run_force || deliver_selectstring != NULL ||
+  deliver_selectstring_sender != NULL;
+const pcre *selectstring_regex = NULL;
+const pcre *selectstring_regex_sender = NULL;
+uschar *log_detail = NULL;
+int subcount = 0;
+int i;
+uschar subdirs[64];
+
+/* Cancel any specific queue domains. Turn off the flag that causes SMTP
+deliveries not to happen, unless doing a 2-stage queue run, when the SMTP flag
+gets set. Save the queue_runner's pid and the flag that indicates any
+deliveries run directly from this process. Deliveries that are run by handing
+on TCP/IP channels have queue_run_pid set, but not queue_running. */
+
+queue_domains = NULL;
+queue_smtp_domains = NULL;
+queue_smtp = queue_2stage;
+
+queue_run_pid = getpid();
+queue_running = TRUE;
+
+/* Log the true start of a queue run, and fancy options */
+
+if (!recurse)
+  {
+  uschar extras[8];
+  uschar *p = extras;
+
+  if (queue_2stage) *p++ = 'q';
+  if (queue_run_first_delivery) *p++ = 'i';
+  if (queue_run_force) *p++ = 'f';
+  if (deliver_force_thaw) *p++ = 'f';
+  if (queue_run_local) *p++ = 'l';
+  *p = 0;
+
+  p = big_buffer;
+  sprintf(CS p, "pid=%d", (int)queue_run_pid);
+  while (*p != 0) p++;
+
+  if (extras[0] != 0)
+    {
+    sprintf(CS p, " -q%s", extras);
+    while (*p != 0) p++;
+    }
+
+  if (deliver_selectstring != NULL)
+    {
+    sprintf(CS p, " -R%s %s", deliver_selectstring_regex? "r" : "",
+      deliver_selectstring);
+    while (*p != 0) p++;
+    }
+
+  if (deliver_selectstring_sender != NULL)
+    {
+    sprintf(CS p, " -S%s %s", deliver_selectstring_sender_regex? "r" : "",
+      deliver_selectstring_sender);
+    while (*p != 0) p++;
+    }
+
+  log_detail = string_copy(big_buffer);
+  log_write(L_queue_run, LOG_MAIN, "Start queue run: %s", log_detail);
+  }
+
+/* If deliver_selectstring is a regex, compile it. */
+
+if (deliver_selectstring != NULL && deliver_selectstring_regex)
+  selectstring_regex = regex_must_compile(deliver_selectstring, TRUE, FALSE);
+
+if (deliver_selectstring_sender != NULL && deliver_selectstring_sender_regex)
+  selectstring_regex_sender =
+    regex_must_compile(deliver_selectstring_sender, TRUE, FALSE);
+
+/* If the spool is split into subdirectories, we want to process it one
+directory at a time, so as to spread out the directory scanning and the
+delivering when there are lots of messages involved, except when
+queue_run_in_order is set.
+
+In the random order case, this loop runs once for the main directory (handling
+any messages therein), and then repeats for any subdirectories that were found.
+When the first argument of queue_get_spool_list() is 0, it scans the top
+directory, fills in subdirs, and sets subcount. The order of the directories is
+then randomized after the first time through, before they are scanned in
+subsqeuent iterations.
+
+When the first argument of queue_get_spool_list() is -1 (for queue_run_in_
+order), it scans all directories and makes a single message list. */
+
+for (i  = (queue_run_in_order? -1 : 0);
+     i <= (queue_run_in_order? -1 : subcount);
+     i++)
+  {
+  queue_filename *f;
+  void *reset_point1 = store_get(0);
+
+  DEBUG(D_queue_run)
+    {
+    if (i == 0)
+      debug_printf("queue running main directory\n");
+    else if (i == -1)
+      debug_printf("queue running combined directories\n");
+    else
+      debug_printf("queue running subdirectory '%c'\n", subdirs[i]);
+    }
+
+  for (f = queue_get_spool_list(i, subdirs, &subcount, !queue_run_in_order);
+       f != NULL;
+       f = f->next)
+    {
+    pid_t pid;
+    int status;
+    int pfd[2];
+    struct stat statbuf;
+    uschar buffer[256];
+
+    /* Unless deliveries are forced, if deliver_queue_load_max is non-negative,
+    check that the load average is low enough to permit deliveries. */
+
+    if (!queue_run_force && deliver_queue_load_max >= 0)
+      {
+      load_average = os_getloadavg();
+      if (load_average > deliver_queue_load_max)
+        {
+        log_write(L_queue_run, LOG_MAIN, "Abandon queue run: %s (load %.2f, max %.2f)",
+          log_detail,
+          (double)load_average/1000.0,
+          (double)deliver_queue_load_max/1000.0);
+        i = subcount;                 /* Don't process other directories */
+        break;
+        }
+      else
+        {
+        DEBUG(D_load) debug_printf("load average = %.2f max = %.2f\n",
+          (double)load_average/1000.0,
+          (double)deliver_queue_load_max/1000.0);
+        }
+      }
+
+    /* Skip this message unless it's within the ID limits */
+
+    if (stop_id != NULL && Ustrncmp(f->text, stop_id, MESSAGE_ID_LENGTH) > 0)
+      continue;
+    if (start_id != NULL && Ustrncmp(f->text, start_id, MESSAGE_ID_LENGTH) < 0)
+      continue;
+
+    /* Check that the message still exists */
+
+    message_subdir[0] = f->dir_uschar;
+    sprintf(CS buffer, "%s/input/%s/%s", spool_directory, message_subdir,
+      f->text);
+    if (Ustat(buffer, &statbuf) < 0) continue;
+
+    /* There are some tests that require the reading of the header file. Ensure
+    the store used is scavenged afterwards so that this process doesn't keep
+    growing its store. We have to read the header file again when actually
+    delivering, but it's cheaper than forking a delivery process for each
+    message when many are not going to be delivered. */
+
+    if (deliver_selectstring != NULL || deliver_selectstring_sender != NULL ||
+        queue_run_first_delivery)
+      {
+      BOOL wanted = TRUE;
+      BOOL orig_dont_deliver = dont_deliver;
+      void *reset_point2 = store_get(0);
+
+      /* Restore the original setting of dont_deliver after reading the header,
+      so that a setting for a particular message doesn't force it for any that
+      follow. If the message is chosen for delivery, the header is read again
+      in the deliver_message() function, in a subprocess. */
+
+      if (spool_read_header(f->text, FALSE, TRUE) != spool_read_OK) continue;
+      dont_deliver = orig_dont_deliver;
+
+      /* Now decide if we want to deliver this message. As we have read the
+      header file, we might as well do the freeze test now, and save forking
+      another process. */
+
+      if (deliver_freeze && !deliver_force_thaw)
+        {
+        log_write(L_skip_delivery, LOG_MAIN, "Message is frozen");
+        wanted = FALSE;
+        }
+
+      /* Check first_delivery in the case when there are no message logs. */
+
+      else if (queue_run_first_delivery && !deliver_firsttime)
+        {
+        DEBUG(D_queue_run) debug_printf("%s: not first delivery\n", f->text);
+        wanted = FALSE;
+        }
+
+      /* Check for a matching address if deliver_selectstring[_sender} is set.
+      If so, we do a fully delivery - don't want to omit other addresses since
+      their routing might trigger re-writing etc. */
+
+      /* Sender matching */
+
+      else if (deliver_selectstring_sender != NULL &&
+              !(deliver_selectstring_sender_regex?
+                (pcre_exec(selectstring_regex_sender, NULL, CS sender_address,
+                  Ustrlen(sender_address), 0, PCRE_EOPT, NULL, 0) >= 0)
+                :
+                (strstric(sender_address, deliver_selectstring_sender, FALSE)
+                  != NULL)))
+        {
+        DEBUG(D_queue_run) debug_printf("%s: sender address did not match %s\n",
+          f->text, deliver_selectstring_sender);
+        wanted = FALSE;
+        }
+
+      /* Recipient matching */
+
+      else if (deliver_selectstring != NULL)
+        {
+        int i;
+        for (i = 0; i < recipients_count; i++)
+          {
+          uschar *address = recipients_list[i].address;
+          if ((deliver_selectstring_regex?
+               (pcre_exec(selectstring_regex, NULL, CS address,
+                 Ustrlen(address), 0, PCRE_EOPT, NULL, 0) >= 0)
+               :
+               (strstric(address, deliver_selectstring, FALSE) != NULL))
+              &&
+              tree_search(tree_nonrecipients, address) == NULL)
+            break;
+          }
+
+        if (i >= recipients_count)
+          {
+          DEBUG(D_queue_run)
+            debug_printf("%s: no recipient address matched %s\n",
+              f->text, deliver_selectstring);
+          wanted = FALSE;
+          }
+        }
+
+      /* Recover store used when reading the header */
+
+      store_reset(reset_point2);
+      if (!wanted) continue;      /* With next message */
+      }
+
+    /* OK, got a message we want to deliver. Create a pipe which will
+    serve as a means of detecting when all the processes created by the
+    delivery process are finished. This is relevant when the delivery
+    process passes one or more SMTP channels on to its own children. The
+    pipe gets passed down; by reading on it here we detect when the last
+    descendent dies by the unblocking of the read. It's a pity that for
+    most of the time the pipe isn't used, but creating a pipe should be
+    pretty cheap. */
+
+    if (pipe(pfd) < 0)
+      {
+      log_write(0, LOG_MAIN|LOG_PANIC_DIE, "failed to create pipe in queue "
+        "runner process %d: %s", queue_run_pid, strerror(errno));
+      }
+    queue_run_pipe = pfd[pipe_write];  /* To ensure it gets passed on. */
+
+    /* Make sure it isn't stdin. This seems unlikely, but just to be on the
+    safe side... */
+
+    if (queue_run_pipe == 0)
+      {
+      queue_run_pipe = dup(queue_run_pipe);
+      (void)close(0);
+      }
+
+    /* Before forking to deliver the message, ensure any open and cached
+    lookup files or databases are closed. Otherwise, closing in the subprocess
+    can make the next subprocess have problems. There won't often be anything
+    open here, but it is possible (e.g. if spool_directory is an expanded
+    string). A single call before this loop would probably suffice, but just in
+    case expansions get inserted at some point, I've taken the heavy-handed
+    approach. When nothing is open, the call should be cheap. */
+
+    search_tidyup();
+
+    /* Now deliver the message; get the id by cutting the -H off the file
+    name. The return of the process is zero if a delivery was attempted. */
+
+    set_process_info("running queue: %s", f->text);
+    f->text[SPOOL_NAME_LENGTH-2] = 0;
+    if ((pid = fork()) == 0)
+      {
+      int rc;
+      if (running_in_test_harness) millisleep(100);
+      (void)close(pfd[pipe_read]);
+      rc = deliver_message(f->text, force_delivery, FALSE);
+      _exit(rc == DELIVER_NOT_ATTEMPTED);
+      }
+    if (pid < 0)
+      log_write(0, LOG_MAIN|LOG_PANIC_DIE, "fork of delivery process from "
+        "queue runner %d failed\n", queue_run_pid);
+
+    /* Close the writing end of the synchronizing pipe in this process,
+    then wait for the first level process to terminate. */
+
+    (void)close(pfd[pipe_write]);
+    set_process_info("running queue: waiting for %s (%d)", f->text, pid);
+    while (wait(&status) != pid);
+
+    /* A zero return means a delivery was attempted; turn off the force flag
+    for any subsequent calls unless queue_force is set. */
+
+    if ((status & 0xffff) == 0) force_delivery = queue_run_force;
+
+    /* If the process crashed, tell somebody */
+
+    else if ((status & 0x00ff) != 0)
+      {
+      log_write(0, LOG_MAIN|LOG_PANIC,
+        "queue run: process %d crashed with signal %d while delivering %s",
+        (int)pid, status & 0x00ff, f->text);
+      }
+
+    /* Before continuing, wait till the pipe gets closed at the far end. This
+    tells us that any children created by the delivery to re-use any SMTP
+    channels have all finished. Since no process actually writes to the pipe,
+    the mere fact that read() unblocks is enough. */
+
+    set_process_info("running queue: waiting for children of %d", pid);
+    if (read(pfd[pipe_read], buffer, sizeof(buffer)) > 0)
+      log_write(0, LOG_MAIN|LOG_PANIC, "queue run: unexpected data on pipe");
+    (void)close(pfd[pipe_read]);
+    set_process_info("running queue");
+
+    /* If we are in the test harness, and this is not the first of a 2-stage
+    queue run, update fudged queue times. */
+
+    if (running_in_test_harness && !queue_2stage)
+      {
+      uschar *fqtnext = Ustrchr(fudged_queue_times, '/');
+      if (fqtnext != NULL) fudged_queue_times = fqtnext + 1;
+      }
+    }                                  /* End loop for list of messages */
+
+  store_reset(reset_point1);           /* Scavenge list of messages */
+
+  /* If this was the first time through for random order processing, and
+  sub-directories have been found, randomize their order if necessary. */
+
+  if (i == 0 && subcount > 1 && !queue_run_in_order)
+    {
+    int j;
+    for (j = 1; j <= subcount; j++)
+      {
+      int r = random_number(100);
+      if (r >= 50)
+        {
+        int k = (r % subcount) + 1;
+        int x = subdirs[j];
+        subdirs[j] = subdirs[k];
+        subdirs[k] = x;
+        }
+      }
+    }
+  }                                    /* End loop for multiple directories */
+
+/* If queue_2stage is true, we do it all again, with the 2stage flag
+turned off. */
+
+if (queue_2stage)
+  {
+  queue_2stage = FALSE;
+  queue_run(start_id, stop_id, TRUE);
+  }
+
+/* At top level, log the end of the run. */
+
+if (!recurse) log_write(L_queue_run, LOG_MAIN, "End queue run: %s", log_detail);
+}
+
+
+
+
+/************************************************
+*         Count messages on the queue           *
+************************************************/
+
+/* Called as a result of -bpc
+
+Arguments:  none
+Returns:    nothing
+*/
+
+void
+queue_count(void)
+{
+int subcount;
+int count = 0;
+queue_filename *f = NULL;
+uschar subdirs[64];
+f = queue_get_spool_list(
+        -1,             /* entire queue */
+        subdirs,        /* for holding sub list */
+        &subcount,      /* for subcount */
+        FALSE);         /* not random */
+for (; f != NULL; f = f->next) count++;
+fprintf(stdout, "%d\n", count);
+}
+
+
+
+/************************************************
+*          List extra deliveries                *
+************************************************/
+
+/* This is called from queue_list below to print out all addresses that
+have received a message but which were not primary addresses. That is, all
+the addresses in the tree of non-recipients that are not primary addresses.
+The tree has been scanned and the data field filled in for those that are
+primary addresses.
+
+Argument:    points to the tree node
+Returns:     nothing
+*/
+
+static void queue_list_extras(tree_node *p)
+{
+if (p->left != NULL) queue_list_extras(p->left);
+if (!p->data.val) printf("       +D %s\n", p->name);
+if (p->right != NULL) queue_list_extras(p->right);
+}
+
+
+
+/************************************************
+*          List messages on the queue           *
+************************************************/
+
+/* Or a given list of messages. In the "all" case, we get a list of file names
+as quickly as possible, then scan each one for information to output. If any
+disappear while we are processing, just leave them out, but give an error if an
+explicit list was given. This function is a top-level function that is obeyed
+as a result of the -bp argument. As there may be a lot of messages on the
+queue, we must tidy up the store after reading the headers for each one.
+
+Arguments:
+   option     0 => list top-level recipients, with "D" for those delivered
+              1 => list only undelivered top-level recipients
+              2 => as 0, plus any generated delivered recipients
+              If 8 is added to any of these values, the queue is listed in
+                random order.
+   list       => first of any message ids to list
+   count      count of message ids; 0 => all
+
+Returns:      nothing
+*/
+
+void
+queue_list(int option, uschar **list, int count)
+{
+int i;
+int subcount;
+int now = (int)time(NULL);
+void *reset_point;
+queue_filename *f = NULL;
+uschar subdirs[64];
+
+/* If given a list of messages, build a chain containing their ids. */
+
+if (count > 0)
+  {
+  queue_filename *last = NULL;
+  for (i = 0; i < count; i++)
+    {
+    queue_filename *next =
+      store_get(sizeof(queue_filename) + Ustrlen(list[i]) + 2);
+    sprintf(CS next->text, "%s-H", list[i]);
+    next->dir_uschar = '*';
+    next->next = NULL;
+    if (i == 0) f = next; else last->next = next;
+    last = next;
+    }
+  }
+
+/* Otherwise get a list of the entire queue, in order if necessary. */
+
+else
+  f = queue_get_spool_list(
+          -1,             /* entire queue */
+          subdirs,        /* for holding sub list */
+          &subcount,      /* for subcount */
+          option >= 8);   /* randomize if required */
+
+if (option >= 8) option -= 8;
+
+/* Now scan the chain and print information, resetting store used
+each time. */
+
+reset_point = store_get(0);
+
+for (; f != NULL; f = f->next)
+  {
+  int rc, save_errno;
+  int size = 0;
+  BOOL env_read;
+
+  store_reset(reset_point);
+  message_size = 0;
+  message_subdir[0] = f->dir_uschar;
+  rc = spool_read_header(f->text, FALSE, count <= 0);
+  if (rc == spool_read_notopen && errno == ENOENT && count <= 0) continue;
+  save_errno = errno;
+
+  env_read = (rc == spool_read_OK || rc == spool_read_hdrerror);
+
+  if (env_read)
+    {
+    int ptr;
+    FILE *jread;
+    struct stat statbuf;
+
+    sprintf(CS big_buffer, "%s/input/%s/%s", spool_directory, message_subdir,
+      f->text);
+    ptr = Ustrlen(big_buffer)-1;
+    big_buffer[ptr] = 'D';
+
+    /* Add the data size to the header size; don't count the file name
+    at the start of the data file, but add one for the notional blank line
+    that precedes the data. */
+
+    if (Ustat(big_buffer, &statbuf) == 0)
+      size = message_size + statbuf.st_size - SPOOL_DATA_START_OFFSET + 1;
+    i = (now - received_time)/60;  /* minutes on queue */
+    if (i > 90)
+      {
+      i = (i + 30)/60;
+      if (i > 72) printf("%2dd ", (i + 12)/24); else printf("%2dh ", i);
+      }
+    else printf("%2dm ", i);
+
+    /* Collect delivered addresses from any J file */
+
+    big_buffer[ptr] = 'J';
+    jread = Ufopen(big_buffer, "rb");
+    if (jread != NULL)
+      {
+      while (Ufgets(big_buffer, big_buffer_size, jread) != NULL)
+        {
+        int n = Ustrlen(big_buffer);
+        big_buffer[n-1] = 0;
+        tree_add_nonrecipient(big_buffer);
+        }
+      (void)fclose(jread);
+      }
+    }
+
+  fprintf(stdout, "%s ", string_format_size(size, big_buffer));
+  for (i = 0; i < 16; i++) fputc(f->text[i], stdout);
+
+  if (env_read && sender_address != NULL)
+    {
+    printf(" <%s>", sender_address);
+    if (sender_set_untrusted) printf(" (%s)", originator_login);
+    }
+
+  if (rc != spool_read_OK)
+    {
+    printf("\n    ");
+    if (save_errno == ERRNO_SPOOLFORMAT)
+      {
+      struct stat statbuf;
+      sprintf(CS big_buffer, "%s/input/%s/%s", spool_directory, message_subdir,
+        f->text);
+      if (Ustat(big_buffer, &statbuf) == 0)
+        printf("*** spool format error: size=" OFF_T_FMT " ***",
+          statbuf.st_size);
+      else printf("*** spool format error ***");
+      }
+    else printf("*** spool read error: %s ***", strerror(save_errno));
+    if (rc != spool_read_hdrerror)
+      {
+      printf("\n\n");
+      continue;
+      }
+    }
+
+  if (deliver_freeze) printf(" *** frozen ***");
+
+  printf("\n");
+
+  if (recipients_list != NULL)
+    {
+    for (i = 0; i < recipients_count; i++)
+      {
+      tree_node *delivered =
+        tree_search(tree_nonrecipients, recipients_list[i].address);
+      if (!delivered || option != 1)
+        printf("        %s %s\n", (delivered != NULL)? "D":" ",
+          recipients_list[i].address);
+      if (delivered != NULL) delivered->data.val = TRUE;
+      }
+    if (option == 2 && tree_nonrecipients != NULL)
+      queue_list_extras(tree_nonrecipients);
+    printf("\n");
+    }
+  }
+}
+
+
+
+/*************************************************
+*             Act on a specific message          *
+*************************************************/
+
+/* Actions that require a list of addresses make use of argv/argc/
+recipients_arg. Other actions do not. This function does its own
+authority checking.
+
+Arguments:
+  id              id of the message to work on
+  action          which action is required (MSG_xxx)
+  argv            the original argv for Exim
+  argc            the original argc for Exim
+  recipients_arg  offset to the list of recipients in argv
+
+Returns:          FALSE if there was any problem
+*/
+
+BOOL
+queue_action(uschar *id, int action, uschar **argv, int argc, int recipients_arg)
+{
+int i, j;
+BOOL yield = TRUE;
+BOOL removed = FALSE;
+struct passwd *pw;
+uschar *doing = NULL;
+uschar *username;
+uschar *errmsg;
+uschar spoolname[256];
+
+/* Set the global message_id variable, used when re-writing spool files. This
+also causes message ids to be added to log messages. */
+
+Ustrcpy(message_id, id);
+
+/* The "actions" that just list the files do not require any locking to be
+done. Only admin users may read the spool files. */
+
+if (action >= MSG_SHOW_BODY)
+  {
+  int fd, i, rc;
+  uschar *subdirectory, *suffix;
+
+  if (!admin_user)
+    {
+    printf("Permission denied\n");
+    return FALSE;
+    }
+
+  if (recipients_arg < argc)
+    {
+    printf("*** Only one message can be listed at once\n");
+    return FALSE;
+    }
+
+  if (action == MSG_SHOW_BODY)
+    {
+    subdirectory = US"input";
+    suffix = US"-D";
+    }
+  else if (action == MSG_SHOW_HEADER)
+    {
+    subdirectory = US"input";
+    suffix = US"-H";
+    }
+  else
+    {
+    subdirectory = US"msglog";
+    suffix = US"";
+    }
+
+  for (i = 0; i < 2; i++)
+    {
+    message_subdir[0] = (split_spool_directory == (i == 0))? id[5] : 0;
+    sprintf(CS spoolname, "%s/%s/%s/%s%s", spool_directory, subdirectory,
+      message_subdir, id, suffix);
+    fd = Uopen(spoolname, O_RDONLY, 0);
+    if (fd >= 0) break;
+    if (i == 0) continue;
+    printf("Failed to open %s file for %s%s: %s\n", subdirectory, id, suffix,
+      strerror(errno));
+    if (action == MSG_SHOW_LOG && !message_logs)
+      printf("(No message logs are being created because the message_logs "
+        "option is false.)\n");
+    return FALSE;
+    }
+
+  while((rc = read(fd, big_buffer, big_buffer_size)) > 0)
+    rc = write(fileno(stdout), big_buffer, rc);
+
+  (void)close(fd);
+  return TRUE;
+  }
+
+/* For actions that actually act, open and lock the data file to ensure that no
+other process is working on this message. If the file does not exist, continue
+only if the action is remove and the user is an admin user, to allow for
+tidying up broken states. */
+
+if (!spool_open_datafile(id))
+  {
+  if (errno == ENOENT)
+    {
+    yield = FALSE;
+    printf("Spool data file for %s does not exist\n", id);
+    if (action != MSG_REMOVE || !admin_user) return FALSE;
+    printf("Continuing, to ensure all files removed\n");
+    }
+  else
+    {
+    if (errno == 0) printf("Message %s is locked\n", id);
+      else printf("Couldn't open spool file for %s: %s\n", id,
+        strerror(errno));
+    return FALSE;
+    }
+  }
+
+/* Read the spool header file for the message. Again, continue after an
+error only in the case of deleting by an administrator. Setting the third
+argument false causes it to look both in the main spool directory and in
+the appropriate subdirectory, and set message_subdir according to where it
+found the message. */
+
+sprintf(CS spoolname, "%s-H", id);
+if (spool_read_header(spoolname, TRUE, FALSE) != spool_read_OK)
+  {
+  yield = FALSE;
+  if (errno != ERRNO_SPOOLFORMAT)
+    printf("Spool read error for %s: %s\n", spoolname, strerror(errno));
+  else
+    printf("Spool format error for %s\n", spoolname);
+  if (action != MSG_REMOVE || !admin_user)
+    {
+    (void)close(deliver_datafile);
+    deliver_datafile = -1;
+    return FALSE;
+    }
+  printf("Continuing to ensure all files removed\n");
+  }
+
+/* Check that the user running this process is entitled to operate on this
+message. Only admin users may freeze/thaw, add/cancel recipients, or otherwise
+mess about, but the original sender is permitted to remove a message. That's
+why we leave this check until after the headers are read. */
+
+if (!admin_user && (action != MSG_REMOVE || real_uid != originator_uid))
+  {
+  printf("Permission denied\n");
+  (void)close(deliver_datafile);
+  deliver_datafile = -1;
+  return FALSE;
+  }
+
+/* Set up the user name for logging. */
+
+pw = getpwuid(real_uid);
+username = (pw != NULL)?
+  US pw->pw_name : string_sprintf("uid %ld", (long int)real_uid);
+
+/* Take the necessary action. */
+
+if (action != MSG_SHOW_COPY) printf("Message %s ", id);
+
+switch(action)
+  {
+  case MSG_SHOW_COPY:
+  deliver_in_buffer = store_malloc(DELIVER_IN_BUFFER_SIZE);
+  deliver_out_buffer = store_malloc(DELIVER_OUT_BUFFER_SIZE);
+  transport_write_message(NULL, 1, 0, 0, NULL, NULL, NULL, NULL, NULL, 0);
+  break;
+
+
+  case MSG_FREEZE:
+  if (deliver_freeze)
+    {
+    yield = FALSE;
+    printf("is already frozen\n");
+    }
+  else
+    {
+    deliver_freeze = TRUE;
+    deliver_manual_thaw = FALSE;
+    deliver_frozen_at = time(NULL);
+    if (spool_write_header(id, SW_MODIFYING, &errmsg) >= 0)
+      {
+      printf("is now frozen\n");
+      log_write(0, LOG_MAIN, "frozen by %s", username);
+      }
+    else
+      {
+      yield = FALSE;
+      printf("could not be frozen: %s\n", errmsg);
+      }
+    }
+  break;
+
+
+  case MSG_THAW:
+  if (!deliver_freeze)
+    {
+    yield = FALSE;
+    printf("is not frozen\n");
+    }
+  else
+    {
+    deliver_freeze = FALSE;
+    deliver_manual_thaw = TRUE;
+    if (spool_write_header(id, SW_MODIFYING, &errmsg) >= 0)
+      {
+      printf("is no longer frozen\n");
+      log_write(0, LOG_MAIN, "unfrozen by %s", username);
+      }
+    else
+      {
+      yield = FALSE;
+      printf("could not be unfrozen: %s\n", errmsg);
+      }
+    }
+  break;
+
+
+  /* We must ensure all files are removed from both the input directory
+  and the appropriate subdirectory, to clean up cases when there are odd
+  files left lying around in odd places. In the normal case message_subdir
+  will have been set correctly by spool_read_header, but as this is a rare
+  operation, just run everything twice. */
+
+  case MSG_REMOVE:
+  message_subdir[0] = id[5];
+  for (j = 0; j < 2; message_subdir[0] = 0, j++)
+    {
+    sprintf(CS spoolname, "%s/msglog/%s/%s", spool_directory, message_subdir, id);
+    if (Uunlink(spoolname) < 0)
+      {
+      if (errno != ENOENT)
+        {
+        yield = FALSE;
+        printf("Error while removing %s: %s\n", spoolname,
+          strerror(errno));
+        }
+      }
+    else removed = TRUE;
+
+    for (i = 0; i < 3; i++)
+      {
+      sprintf(CS spoolname, "%s/input/%s/%s-%c", spool_directory, message_subdir,
+        id, "DHJ"[i]);
+      if (Uunlink(spoolname) < 0)
+        {
+        if (errno != ENOENT)
+          {
+          yield = FALSE;
+          printf("Error while removing %s: %s\n", spoolname,
+            strerror(errno));
+          }
+        }
+      else removed = TRUE;
+      }
+    }
+
+  /* In the common case, the datafile is open (and locked), so give the
+  obvious message. Otherwise be more specific. */
+
+  if (deliver_datafile >= 0) printf("has been removed\n");
+    else printf("has been removed or did not exist\n");
+  if (removed)
+    {
+    log_write(0, LOG_MAIN, "removed by %s", username);
+    log_write(0, LOG_MAIN, "Completed");
+    }
+  break;
+
+
+  case MSG_MARK_ALL_DELIVERED:
+  for (i = 0; i < recipients_count; i++)
+    {
+    tree_add_nonrecipient(recipients_list[i].address);
+    }
+  if (spool_write_header(id, SW_MODIFYING, &errmsg) >= 0)
+    {
+    printf("has been modified\n");
+    for (i = 0; i < recipients_count; i++)
+      log_write(0, LOG_MAIN, "address <%s> marked delivered by %s",
+        recipients_list[i].address, username);
+    }
+  else
+    {
+    yield = FALSE;
+    printf("- could not mark all delivered: %s\n", errmsg);
+    }
+  break;
+
+
+  case MSG_EDIT_SENDER:
+  if (recipients_arg < argc - 1)
+    {
+    yield = FALSE;
+    printf("- only one sender address can be specified\n");
+    break;
+    }
+  doing = US"editing sender";
+  /* Fall through */
+
+  case MSG_ADD_RECIPIENT:
+  if (doing == NULL) doing = US"adding recipient";
+  /* Fall through */
+
+  case MSG_MARK_DELIVERED:
+  if (doing == NULL) doing = US"marking as delivered";
+
+  /* Common code for EDIT_SENDER, ADD_RECIPIENT, & MARK_DELIVERED */
+
+  if (recipients_arg >= argc)
+    {
+    yield = FALSE;
+    printf("- error while %s: no address given\n", doing);
+    break;
+    }
+
+  for (; recipients_arg < argc; recipients_arg++)
+    {
+    int start, end, domain;
+    uschar *errmess;
+    uschar *recipient =
+      parse_extract_address(argv[recipients_arg], &errmess, &start, &end,
+        &domain, (action == MSG_EDIT_SENDER));
+
+    if (recipient == NULL)
+      {
+      yield = FALSE;
+      printf("- error while %s:\n  bad address %s: %s\n",
+        doing, argv[recipients_arg], errmess);
+      }
+    else if (recipient[0] != 0 && domain == 0)
+      {
+      yield = FALSE;
+      printf("- error while %s:\n  bad address %s: "
+        "domain missing\n", doing, argv[recipients_arg]);
+      }
+    else
+      {
+      if (action == MSG_ADD_RECIPIENT)
+        {
+        receive_add_recipient(recipient, -1);
+        log_write(0, LOG_MAIN, "recipient <%s> added by %s",
+          recipient, username);
+        }
+      else if (action == MSG_MARK_DELIVERED)
+        {
+        for (i = 0; i < recipients_count; i++)
+          if (Ustrcmp(recipients_list[i].address, recipient) == 0) break;
+        if (i >= recipients_count)
+          {
+          printf("- error while %s:\n  %s is not a recipient:"
+            " message not updated\n", doing, recipient);
+          yield = FALSE;
+          }
+        else
+          {
+          tree_add_nonrecipient(recipients_list[i].address);
+          log_write(0, LOG_MAIN, "address <%s> marked delivered by %s",
+            recipient, username);
+          }
+        }
+      else  /* MSG_EDIT_SENDER */
+        {
+        sender_address = recipient;
+        log_write(0, LOG_MAIN, "sender address changed to <%s> by %s",
+          recipient, username);
+        }
+      }
+    }
+
+  if (yield)
+    {
+    if (spool_write_header(id, SW_MODIFYING, &errmsg) >= 0)
+      printf("has been modified\n");
+    else
+      {
+      yield = FALSE;
+      printf("- while %s: %s\n", doing, errmsg);
+      }
+    }
+
+  break;
+  }
+
+/* Closing the datafile releases the lock and permits other processes
+to operate on the message (if it still exists). */
+
+(void)close(deliver_datafile);
+deliver_datafile = -1;
+return yield;
+}
+
+
+
+/*************************************************
+*       Check the queue_only_file condition      *
+*************************************************/
+
+/* The queue_only_file option forces certain kinds of queueing if a given file
+exists.
+
+Arguments:  none
+Returns:    nothing
+*/
+
+void
+queue_check_only(void)
+{
+BOOL *set;
+int sep = 0;
+struct stat statbuf;
+uschar *s, *ss, *name;
+uschar buffer[1024];
+
+if (queue_only_file == NULL) return;
+
+s = queue_only_file;
+while ((ss = string_nextinlist(&s, &sep, buffer, sizeof(buffer))) != NULL)
+  {
+  if (Ustrncmp(ss, "smtp", 4) == 0)
+    {
+    name = US"queue_smtp";
+    set = &queue_smtp;
+    ss += 4;
+    }
+  else
+    {
+    name = US"queue_only";
+    set = &queue_only;
+    }
+
+  if (Ustat(ss, &statbuf) == 0)
+    {
+    *set = TRUE;
+    DEBUG(D_receive) debug_printf("%s set because %s exists\n", name, ss);
+    }
+  }
+}
+
+/* End of queue.c */
diff --git a/src/rda.c b/src/rda.c
new file mode 100644 (file)
index 0000000..f915ce7
--- /dev/null
+++ b/src/rda.c
@@ -0,0 +1,1005 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* This module contains code for extracting addresses from a forwarding list
+(from an alias or forward file) or by running the filter interpreter. It may do
+this in a sub-process if a uid/gid are supplied. */
+
+
+#include "exim.h"
+
+enum { FILE_EXIST, FILE_NOT_EXIST, FILE_EXIST_UNCLEAR };
+
+#define REPLY_EXISTS    0x01
+#define REPLY_EXPAND    0x02
+#define REPLY_RETURN    0x04
+
+
+/*************************************************
+*         Check string for filter program        *
+*************************************************/
+
+/* This function checks whether a string is actually a filter program. The rule
+is that it must start with "# Exim filter ..." (any capitalization, spaces
+optional). It is envisaged that in future, other kinds of filter may be
+implemented. That's why it is implemented the way it is. The function is global
+because it is also called from filter.c when checking filters.
+
+Argument:  the string
+
+Returns:   FILTER_EXIM    if it starts with "# Exim filter"
+           FILTER_SIEVE   if it starts with "# Sieve filter"
+           FILTER_FORWARD otherwise
+*/
+
+/* This is an auxiliary function for matching a tag. */
+
+static BOOL
+match_tag(const uschar *s, const uschar *tag)
+{
+for (; *tag != 0; s++, tag++)
+  {
+  if (*tag == ' ')
+    {
+    while (*s == ' ' || *s == '\t') s++;
+    s--;
+    }
+  else if (tolower(*s) != tolower(*tag)) break;
+  }
+return (*tag == 0);
+}
+
+/* This is the real function. It should be easy to add checking different
+tags for other types of filter. */
+
+int
+rda_is_filter(const uschar *s)
+{
+while (isspace(*s)) s++;     /* Skips initial blank lines */
+if (match_tag(s, CUS"# exim filter")) return FILTER_EXIM;
+  else if (match_tag(s, CUS"# sieve filter")) return FILTER_SIEVE;
+    else return FILTER_FORWARD;
+}
+
+
+
+
+/*************************************************
+*         Check for existence of file            *
+*************************************************/
+
+/* First of all, we stat the file. If this fails, we try to stat the enclosing
+directory, because a file in an unmounted NFS directory will look the same as a
+non-existent file. It seems that in Solaris 2.6, statting an entry in an
+indirect map that is currently unmounted does not cause the mount to happen.
+Instead, dummy data is returned, which defeats the whole point of this test.
+However, if a stat() is done on some object inside the directory, such as the
+"." back reference to itself, then the mount does occur. If an NFS host is
+taken offline, it is possible for the stat() to get stuck until it comes back.
+To guard against this, stick a timer round it. If we can't access the "."
+inside the directory, try the plain directory, just in case that helps.
+
+Argument:
+  filename   the file name
+  error      for message on error
+
+Returns:     FILE_EXIST          the file exists
+             FILE_NOT_EXIST      the file does not exist
+             FILE_EXIST_UNCLEAR  cannot determine existence
+*/
+
+static int
+rda_exists(uschar *filename, uschar **error)
+{
+int rc, saved_errno;
+uschar *slash;
+struct stat statbuf;
+
+if ((rc = Ustat(filename, &statbuf)) >= 0) return FILE_EXIST;
+saved_errno = errno;
+
+Ustrncpy(big_buffer, filename, big_buffer_size - 3);
+sigalrm_seen = FALSE;
+
+if (saved_errno == ENOENT)
+  {
+  slash = Ustrrchr(big_buffer, '/');
+  Ustrcpy(slash+1, ".");
+
+  alarm(30);
+  rc = Ustat(big_buffer, &statbuf);
+  if (rc != 0 && errno == EACCES && !sigalrm_seen)
+    {
+    *slash = 0;
+    rc = Ustat(big_buffer, &statbuf);
+    }
+  saved_errno = errno;
+  alarm(0);
+
+  DEBUG(D_route) debug_printf("stat(%s)=%d\n", big_buffer, rc);
+  }
+
+if (sigalrm_seen || rc != 0)
+  {
+  *error = string_sprintf("failed to stat %s (%s)", big_buffer,
+    sigalrm_seen? "timeout" : strerror(saved_errno));
+  return FILE_EXIST_UNCLEAR;
+  }
+
+*error = string_sprintf("%s does not exist", filename);
+DEBUG(D_route) debug_printf("%s\n", *error);
+return FILE_NOT_EXIST;
+}
+
+
+
+/*************************************************
+*     Get forwarding list from a file            *
+*************************************************/
+
+/* Open a file and read its entire contents into a block of memory. Certain
+opening errors are optionally treated the same as "file does not exist".
+
+ENOTDIR means that something along the line is not a directory: there are
+installations that set home directories to be /dev/null for non-login accounts
+but in normal circumstances this indicates some kind of configuration error.
+
+EACCES means there's a permissions failure. Some users turn off read permission
+on a .forward file to suspend forwarding, but this is probably an error in any
+kind of mailing list processing.
+
+The redirect block that contains the file name also contains constraints such
+as who may own the file, and mode bits that must not be set. This function is
+
+Arguments:
+  rdata       rdirect block, containing file name and constraints
+  options     for the RDO_ENOTDIR and RDO_EACCES options
+  error       where to put an error message
+  yield       what to return from rda_interpret on error
+
+Returns:      pointer to string in store; NULL on error
+*/
+
+static uschar *
+rda_get_file_contents(redirect_block *rdata, int options, uschar **error,
+  int *yield)
+{
+FILE *fwd;
+uschar *filebuf;
+uschar *filename = rdata->string;
+BOOL uid_ok = !rdata->check_owner;
+BOOL gid_ok = !rdata->check_group;
+struct stat statbuf;
+
+/* Attempt to open the file. If it appears not to exist, check up on the
+containing directory by statting it. If the directory does not exist, we treat
+this situation as an error (which will cause delivery to defer); otherwise we
+pass back FF_NONEXIST, which causes the redirect router to decline.
+
+However, if the ignore_enotdir option is set (to ignore "something on the
+path is not a directory" errors), the right behaviour seems to be not to do the
+directory test. */
+
+fwd = Ufopen(filename, "rb");
+if (fwd == NULL)
+  {
+  switch(errno)
+    {
+    case ENOENT:          /* File does not exist */
+    DEBUG(D_route) debug_printf("%s does not exist\n%schecking parent directory\n",
+      filename,
+      ((options & RDO_ENOTDIR) != 0)? "ignore_enotdir set => skip " : "");
+    *yield = (((options & RDO_ENOTDIR) != 0) ||
+              rda_exists(filename, error) == FILE_NOT_EXIST)?
+      FF_NONEXIST : FF_ERROR;
+    return NULL;
+
+    case ENOTDIR:         /* Something on the path isn't a directory */
+    if ((options & RDO_ENOTDIR) == 0) goto DEFAULT_ERROR;
+    DEBUG(D_route) debug_printf("non-directory on path %s: file assumed not to "
+      "exist\n", filename);
+    *yield = FF_NONEXIST;
+    return NULL;
+
+    case EACCES:           /* Permission denied */
+    if ((options & RDO_EACCES) == 0) goto DEFAULT_ERROR;
+    DEBUG(D_route) debug_printf("permission denied for %s: file assumed not to "
+      "exist\n", filename);
+    *yield = FF_NONEXIST;
+    return NULL;
+
+    DEFAULT_ERROR:
+    default:
+    *error = string_open_failed(errno, "%s", filename);
+    *yield = FF_ERROR;
+    return NULL;
+    }
+  }
+
+/* Check that we have a regular file. */
+
+if (fstat(fileno(fwd), &statbuf) != 0)
+  {
+  *error = string_sprintf("failed to stat %s: %s", filename, strerror(errno));
+  goto ERROR_RETURN;
+  }
+
+if ((statbuf.st_mode & S_IFMT) != S_IFREG)
+  {
+  *error = string_sprintf("%s is not a regular file", filename);
+  goto ERROR_RETURN;
+  }
+
+/* Check for unwanted mode bits */
+
+if ((statbuf.st_mode & rdata->modemask) != 0)
+  {
+  *error = string_sprintf("bad mode (0%o) for %s: 0%o bit(s) unexpected",
+    statbuf.st_mode, filename, statbuf.st_mode & rdata->modemask);
+  goto ERROR_RETURN;
+  }
+
+/* Check the file owner and file group if required to do so. */
+
+if (!uid_ok)
+  {
+  if (rdata->pw != NULL && statbuf.st_uid == rdata->pw->pw_uid)
+    uid_ok = TRUE;
+  else if (rdata->owners != NULL)
+    {
+    int i;
+    for (i = 1; i <= (int)(rdata->owners[0]); i++)
+      if (rdata->owners[i] == statbuf.st_uid) { uid_ok = TRUE; break; }
+    }
+  }
+
+if (!gid_ok)
+  {
+  if (rdata->pw != NULL && statbuf.st_gid == rdata->pw->pw_gid)
+    gid_ok = TRUE;
+  else if (rdata->owngroups != NULL)
+    {
+    int i;
+    for (i = 1; i <= (int)(rdata->owngroups[0]); i++)
+      if (rdata->owngroups[i] == statbuf.st_gid) { gid_ok = TRUE; break; }
+    }
+  }
+
+if (!uid_ok || !gid_ok)
+  {
+  *error = string_sprintf("bad %s for %s", uid_ok? "group" : "owner", filename);
+  goto ERROR_RETURN;
+  }
+
+/* Put an upper limit on the size of the file, just to stop silly people
+feeding in ridiculously large files, which can easily be created by making
+files that have holes in them. */
+
+if (statbuf.st_size > MAX_FILTER_SIZE)
+  {
+  *error = string_sprintf("%s is too big (max %d)", filename, MAX_FILTER_SIZE);
+  goto ERROR_RETURN;
+  }
+
+/* Read the file in one go in order to minimize the time we have it open. */
+
+filebuf = store_get(statbuf.st_size + 1);
+
+if (fread(filebuf, 1, statbuf.st_size, fwd) != statbuf.st_size)
+  {
+  *error = string_sprintf("error while reading %s: %s",
+    filename, strerror(errno));
+  goto ERROR_RETURN;
+  }
+filebuf[statbuf.st_size] = 0;
+
+DEBUG(D_route)
+  debug_printf(OFF_T_FMT " bytes read from %s\n", statbuf.st_size, filename);
+
+(void)fclose(fwd);
+return filebuf;
+
+/* Return an error: the string is already set up. */
+
+ERROR_RETURN:
+*yield = FF_ERROR;
+(void)fclose(fwd);
+return NULL;
+}
+
+
+
+/*************************************************
+*      Extract info from list or filter          *
+*************************************************/
+
+/* This function calls the appropriate function to extract addresses from a
+forwarding list, or to run a filter file and get addresses from there.
+
+Arguments:
+  rdata                     the redirection block
+  options                   the options bits
+  include_directory         restrain to this directory
+  sieve_vacation_directory  passed to sieve_interpret
+  sieve_enotify_mailto_owner passed to sieve_interpret
+  sieve_useraddress         passed to sieve_interpret
+  sieve_subaddress          passed to sieve_interpret
+  generated                 where to hang generated addresses
+  error                     for error messages
+  eblockp                   for details of skipped syntax errors
+                              (NULL => no skip)
+  filtertype                set to the filter type:
+                              FILTER_FORWARD => a traditional .forward file
+                              FILTER_EXIM    => an Exim filter file
+                              FILTER_SIEVE   => a Sieve filter file
+                            a system filter is always forced to be FILTER_EXIM
+
+Returns:                    a suitable return for rda_interpret()
+*/
+
+static int
+rda_extract(redirect_block *rdata, int options, uschar *include_directory,
+  uschar *sieve_vacation_directory, uschar *sieve_enotify_mailto_owner,
+  uschar *sieve_useraddress, uschar *sieve_subaddress,
+  address_item **generated, uschar **error, error_block **eblockp,
+  int *filtertype)
+{
+uschar *data;
+
+if (rdata->isfile)
+  {
+  int yield = 0;
+  data = rda_get_file_contents(rdata, options, error, &yield);
+  if (data == NULL) return yield;
+  }
+else data = rdata->string;
+
+*filtertype = system_filtering? FILTER_EXIM : rda_is_filter(data);
+
+/* Filter interpretation is done by a general function that is also called from
+the filter testing option (-bf). There are two versions: one for Exim filtering
+and one for Sieve filtering. Several features of string expansion may be locked
+out at sites that don't trust users. This is done by setting flags in
+expand_forbid that the expander inspects. */
+
+if (*filtertype != FILTER_FORWARD)
+  {
+  int frc;
+  int old_expand_forbid = expand_forbid;
+
+  DEBUG(D_route) debug_printf("data is %s filter program\n",
+    (*filtertype == FILTER_EXIM)? "an Exim" : "a Sieve");
+
+  /* RDO_FILTER is an "allow" bit */
+
+  if ((options & RDO_FILTER) == 0)
+    {
+    *error = US"filtering not enabled";
+    return FF_ERROR;
+    }
+
+  expand_forbid =
+    (expand_forbid & ~RDO_FILTER_EXPANSIONS) |
+    (options & RDO_FILTER_EXPANSIONS);
+
+  /* RDO_{EXIM,SIEVE}_FILTER are forbid bits */
+
+  if (*filtertype == FILTER_EXIM)
+    {
+    if ((options & RDO_EXIM_FILTER) != 0)
+      {
+      *error = US"Exim filtering not enabled";
+      return FF_ERROR;
+      }
+    frc = filter_interpret(data, options, generated, error);
+    }
+  else
+    {
+    if ((options & RDO_SIEVE_FILTER) != 0)
+      {
+      *error = US"Sieve filtering not enabled";
+      return FF_ERROR;
+      }
+    frc = sieve_interpret(data, options, sieve_vacation_directory,
+      sieve_enotify_mailto_owner, sieve_useraddress, sieve_subaddress,
+      generated, error);
+    }
+
+  expand_forbid = old_expand_forbid;
+  return frc;
+  }
+
+/* Not a filter script */
+
+DEBUG(D_route) debug_printf("file is not a filter file\n");
+
+return parse_forward_list(data,
+  options,                           /* specials that are allowed */
+  generated,                         /* where to hang them */
+  error,                             /* for errors */
+  deliver_domain,                    /* to qualify \name */
+  include_directory,                 /* restrain to directory */
+  eblockp);                          /* for skipped syntax errors */
+}
+
+
+
+
+/*************************************************
+*         Write string down pipe                 *
+*************************************************/
+
+/* This function is used for transferring a string down a pipe between
+processes. If the pointer is NULL, a length of zero is written.
+
+Arguments:
+  fd         the pipe
+  s          the string
+
+Returns:     -1 on error, else 0
+*/
+
+static int
+rda_write_string(int fd, uschar *s)
+{
+int len = (s == NULL)? 0 : Ustrlen(s) + 1;
+return (  write(fd, &len, sizeof(int)) != sizeof(int)
+       || (s != NULL  &&  write(fd, s, len) != len)
+       )
+       ? -1 : 0;
+}
+
+
+
+/*************************************************
+*          Read string from pipe                 *
+*************************************************/
+
+/* This function is used for receiving a string from a pipe.
+
+Arguments:
+  fd         the pipe
+  sp         where to put the string
+
+Returns:     FALSE if data missing
+*/
+
+static BOOL
+rda_read_string(int fd, uschar **sp)
+{
+int len;
+
+if (read(fd, &len, sizeof(int)) != sizeof(int)) return FALSE;
+if (len == 0) *sp = NULL; else
+  {
+  *sp = store_get(len);
+  if (read(fd, *sp, len) != len) return FALSE;
+  }
+return TRUE;
+}
+
+
+
+/*************************************************
+*         Interpret forward list or filter       *
+*************************************************/
+
+/* This function is passed a forward list string (unexpanded) or the name of a
+file (unexpanded) whose contents are the forwarding list. The list may in fact
+be a filter program if it starts with "#Exim filter" or "#Sieve filter". Other
+types of filter, with different inital tag strings, may be introduced in due
+course.
+
+The job of the function is to process the forwarding list or filter. It is
+pulled out into this separate function, because it is used for system filter
+files as well as from the redirect router.
+
+If the function is given a uid/gid, it runs a subprocess that passes the
+results back via a pipe. This provides security for things like :include:s in
+users' .forward files, and "logwrite" calls in users' filter files. A
+sub-process is NOT used when:
+
+  . No uid/gid is provided
+  . The input is a string which is not a filter string, and does not contain
+    :include:
+  . The input is a file whose non-existence can be detected in the main
+    process (which is usually running as root).
+
+Arguments:
+  rdata                     redirect data (file + constraints, or data string)
+  options                   options to pass to the extraction functions,
+                              plus ENOTDIR and EACCES handling bits
+  include_directory         restrain :include: to this directory
+  sieve_vacation_directory  directory passed to sieve_interpret
+  sieve_enotify_mailto_owner passed to sieve_interpret
+  sieve_useraddress         passed to sieve_interpret
+  sieve_subaddress          passed to sieve_interpret
+  ugid                      uid/gid to run under - if NULL, no change
+  generated                 where to hang generated addresses, initially NULL
+  error                     pointer for error message
+  eblockp                   for skipped syntax errors; NULL if no skipping
+  filtertype                set to the type of file:
+                              FILTER_FORWARD => traditional .forward file
+                              FILTER_EXIM    => an Exim filter file
+                              FILTER_SIEVE   => a Sieve filter file
+                            a system filter is always forced to be FILTER_EXIM
+  rname                     router name for error messages in the format
+                              "xxx router" or "system filter"
+
+Returns:        values from extraction function, or FF_NONEXIST:
+                  FF_DELIVERED     success, a significant action was taken
+                  FF_NOTDELIVERED  success, no significant action
+                  FF_BLACKHOLE     :blackhole:
+                  FF_DEFER         defer requested
+                  FF_FAIL          fail requested
+                  FF_INCLUDEFAIL   some problem with :include:
+                  FF_FREEZE        freeze requested
+                  FF_ERROR         there was a problem
+                  FF_NONEXIST      the file does not exist
+*/
+
+int
+rda_interpret(redirect_block *rdata, int options, uschar *include_directory,
+  uschar *sieve_vacation_directory, uschar *sieve_enotify_mailto_owner,
+  uschar *sieve_useraddress, uschar *sieve_subaddress, ugid_block *ugid,
+  address_item **generated, uschar **error, error_block **eblockp,
+  int *filtertype, uschar *rname)
+{
+int fd, rc, pfd[2];
+int yield, status;
+BOOL had_disaster = FALSE;
+pid_t pid;
+uschar *data;
+uschar *readerror = US"";
+void (*oldsignal)(int);
+
+DEBUG(D_route) debug_printf("rda_interpret (%s): %s\n",
+  (rdata->isfile)? "file" : "string", rdata->string);
+
+/* Do the expansions of the file name or data first, while still privileged. */
+
+data = expand_string(rdata->string);
+if (data == NULL)
+  {
+  if (expand_string_forcedfail) return FF_NOTDELIVERED;
+  *error = string_sprintf("failed to expand \"%s\": %s", rdata->string,
+    expand_string_message);
+  return FF_ERROR;
+  }
+rdata->string = data;
+
+DEBUG(D_route) debug_printf("expanded: %s\n", data);
+
+if (rdata->isfile && data[0] != '/')
+  {
+  *error = string_sprintf("\"%s\" is not an absolute path", data);
+  return FF_ERROR;
+  }
+
+/* If no uid/gid are supplied, or if we have a data string which does not start
+with #Exim filter or #Sieve filter, and does not contain :include:, do all the
+work in this process. Note that for a system filter, we always have a file, so
+the work is done in this process only if no user is supplied. */
+
+if (!ugid->uid_set ||                         /* Either there's no uid, or */
+    (!rdata->isfile &&                        /* We've got the data, and */
+     rda_is_filter(data) == FILTER_FORWARD && /* It's not a filter script, */
+     Ustrstr(data, ":include:") == NULL))     /* and there's no :include: */
+  {
+  return rda_extract(rdata, options, include_directory,
+    sieve_vacation_directory, sieve_enotify_mailto_owner, sieve_useraddress,
+    sieve_subaddress, generated, error, eblockp, filtertype);
+  }
+
+/* We need to run the processing code in a sub-process. However, if we can
+determine the non-existence of a file first, we can decline without having to
+create the sub-process. */
+
+if (rdata->isfile && rda_exists(data, error) == FILE_NOT_EXIST)
+  return FF_NONEXIST;
+
+/* If the file does exist, or we can't tell (non-root mounted NFS directory)
+we have to create the subprocess to do everything as the given user. The
+results of processing are passed back via a pipe. */
+
+if (pipe(pfd) != 0)
+  log_write(0, LOG_MAIN|LOG_PANIC_DIE, "creation of pipe for filter or "
+    ":include: failed for %s: %s", rname, strerror(errno));
+
+/* Ensure that SIGCHLD is set to SIG_DFL before forking, so that the child
+process can be waited for. We sometimes get here with it set otherwise. Save
+the old state for resetting on the wait. Ensure that all cached resources are
+freed so that the subprocess starts with a clean slate and doesn't interfere
+with the parent process. */
+
+oldsignal = signal(SIGCHLD, SIG_DFL);
+search_tidyup();
+
+if ((pid = fork()) == 0)
+  {
+  header_line *waslast = header_last;   /* Save last header */
+
+  fd = pfd[pipe_write];
+  (void)close(pfd[pipe_read]);
+  exim_setugid(ugid->uid, ugid->gid, FALSE, rname);
+
+  /* Addresses can get rewritten in filters; if we are not root or the exim
+  user (and we probably are not), turn off rewrite logging, because we cannot
+  write to the log now. */
+
+  if (ugid->uid != root_uid && ugid->uid != exim_uid)
+    {
+    DEBUG(D_rewrite) debug_printf("turned off address rewrite logging (not "
+      "root or exim in this process)\n");
+    log_write_selector &= ~L_address_rewrite;
+    }
+
+  /* Now do the business */
+
+  yield = rda_extract(rdata, options, include_directory,
+    sieve_vacation_directory, sieve_enotify_mailto_owner, sieve_useraddress,
+    sieve_subaddress, generated, error, eblockp, filtertype);
+
+  /* Pass back whether it was a filter, and the return code and any overall
+  error text via the pipe. */
+
+  if (  write(fd, filtertype, sizeof(int)) != sizeof(int)
+     || write(fd, &yield, sizeof(int)) != sizeof(int)
+     || rda_write_string(fd, *error) != 0
+     )
+    goto bad;
+
+  /* Pass back the contents of any syntax error blocks if we have a pointer */
+
+  if (eblockp != NULL)
+    {
+    error_block *ep;
+    for (ep = *eblockp; ep != NULL; ep = ep->next)
+      if (  rda_write_string(fd, ep->text1) != 0
+         || rda_write_string(fd, ep->text2) != 0
+        )
+       goto bad;
+    if (rda_write_string(fd, NULL) != 0)    /* Indicates end of eblocks */
+      goto bad;
+    }
+
+  /* If this is a system filter, we have to pass back the numbers of any
+  original header lines that were removed, and then any header lines that were
+  added but not subsequently removed. */
+
+  if (system_filtering)
+    {
+    int i = 0;
+    header_line *h;
+    for (h = header_list; h != waslast->next; i++, h = h->next)
+      if (  h->type == htype_old
+         && write(fd, &i, sizeof(i)) != sizeof(i)
+        )
+       goto bad;
+
+    i = -1;
+    if (write(fd, &i, sizeof(i)) != sizeof(i))
+       goto bad;
+
+    while (waslast != header_last)
+      {
+      waslast = waslast->next;
+      if (waslast->type != htype_old)
+       if (  rda_write_string(fd, waslast->text) != 0
+           || write(fd, &(waslast->type), sizeof(waslast->type))
+             != sizeof(waslast->type)
+          )
+         goto bad;
+      }
+    if (rda_write_string(fd, NULL) != 0)    /* Indicates end of added headers */
+      goto bad;
+    }
+
+  /* Write the contents of the $n variables */
+
+  if (write(fd, filter_n, sizeof(filter_n)) != sizeof(filter_n))
+    goto bad;
+
+  /* If the result was DELIVERED or NOTDELIVERED, we pass back the generated
+  addresses, and their associated information, through the pipe. This is
+  just tedious, but it seems to be the only safe way. We do this also for
+  FAIL and FREEZE, because a filter is allowed to set up deliveries that
+  are honoured before freezing or failing. */
+
+  if (yield == FF_DELIVERED || yield == FF_NOTDELIVERED ||
+      yield == FF_FAIL || yield == FF_FREEZE)
+    {
+    address_item *addr;
+    for (addr = *generated; addr != NULL; addr = addr->next)
+      {
+      int reply_options = 0;
+
+      if (  rda_write_string(fd, addr->address) != 0
+         || write(fd, &(addr->mode), sizeof(addr->mode))
+           != sizeof(addr->mode)
+         || write(fd, &(addr->flags), sizeof(addr->flags))
+           != sizeof(addr->flags)
+         || rda_write_string(fd, addr->p.errors_address) != 0
+        )
+       goto bad;
+
+      if (addr->pipe_expandn != NULL)
+        {
+        uschar **pp;
+        for (pp = addr->pipe_expandn; *pp != NULL; pp++)
+          if (rda_write_string(fd, *pp) != 0)
+           goto bad;
+        }
+      if (rda_write_string(fd, NULL) != 0)
+        goto bad;
+
+      if (addr->reply == NULL)
+       {
+        if (write(fd, &reply_options, sizeof(int)) != sizeof(int))    /* 0 means no reply */
+         goto bad;
+       }
+      else
+        {
+        reply_options |= REPLY_EXISTS;
+        if (addr->reply->file_expand) reply_options |= REPLY_EXPAND;
+        if (addr->reply->return_message) reply_options |= REPLY_RETURN;
+        if (  write(fd, &reply_options, sizeof(int)) != sizeof(int)
+           || write(fd, &(addr->reply->expand_forbid), sizeof(int))
+             != sizeof(int)
+           || write(fd, &(addr->reply->once_repeat), sizeof(time_t))
+             != sizeof(time_t)
+           || rda_write_string(fd, addr->reply->to) != 0
+           || rda_write_string(fd, addr->reply->cc) != 0
+           || rda_write_string(fd, addr->reply->bcc) != 0
+           || rda_write_string(fd, addr->reply->from) != 0
+           || rda_write_string(fd, addr->reply->reply_to) != 0
+           || rda_write_string(fd, addr->reply->subject) != 0
+           || rda_write_string(fd, addr->reply->headers) != 0
+           || rda_write_string(fd, addr->reply->text) != 0
+           || rda_write_string(fd, addr->reply->file) != 0
+           || rda_write_string(fd, addr->reply->logfile) != 0
+           || rda_write_string(fd, addr->reply->oncelog) != 0
+          )
+         goto bad;
+        }
+      }
+
+    if (rda_write_string(fd, NULL) != 0)   /* Marks end of addresses */
+      goto bad;
+    }
+
+  /* OK, this process is now done. Free any cached resources. Must use _exit()
+  and not exit() !! */
+
+out:
+  (void)close(fd);
+  search_tidyup();
+  _exit(0);
+
+bad:
+  DEBUG(D_rewrite) debug_printf("rda_interpret: failed write to pipe\n");
+  goto out;
+  }
+
+/* Back in the main process: panic if the fork did not succeed. */
+
+if (pid < 0)
+  log_write(0, LOG_MAIN|LOG_PANIC_DIE, "fork failed for %s", rname);
+
+/* Read the pipe to get the data from the filter/forward. Our copy of the
+writing end must be closed first, as otherwise read() won't return zero on an
+empty pipe. Afterwards, close the reading end. */
+
+(void)close(pfd[pipe_write]);
+
+/* Read initial data, including yield and contents of *error */
+
+fd = pfd[pipe_read];
+if (read(fd, filtertype, sizeof(int)) != sizeof(int) ||
+    read(fd, &yield, sizeof(int)) != sizeof(int) ||
+    !rda_read_string(fd, error)) goto DISASTER;
+
+/* Read the contents of any syntax error blocks if we have a pointer */
+
+if (eblockp != NULL)
+  {
+  uschar *s;
+  error_block *e;
+  error_block **p = eblockp;
+  for (;;)
+    {
+    if (!rda_read_string(fd, &s)) goto DISASTER;
+    if (s == NULL) break;
+    e = store_get(sizeof(error_block));
+    e->next = NULL;
+    e->text1 = s;
+    if (!rda_read_string(fd, &s)) goto DISASTER;
+    e->text2 = s;
+    *p = e;
+    p = &(e->next);
+    }
+  }
+
+/* If this is a system filter, read the identify of any original header lines
+that were removed, and then read data for any new ones that were added. */
+
+if (system_filtering)
+  {
+  int hn = 0;
+  header_line *h = header_list;
+
+  for (;;)
+    {
+    int n;
+    if (read(fd, &n, sizeof(int)) != sizeof(int)) goto DISASTER;
+    if (n < 0) break;
+    while (hn < n)
+      {
+      hn++;
+      h = h->next;
+      if (h == NULL) goto DISASTER_NO_HEADER;
+      }
+    h->type = htype_old;
+    }
+
+  for (;;)
+    {
+    uschar *s;
+    int type;
+    if (!rda_read_string(fd, &s)) goto DISASTER;
+    if (s == NULL) break;
+    if (read(fd, &type, sizeof(type)) != sizeof(type)) goto DISASTER;
+    header_add(type, "%s", s);
+    }
+  }
+
+/* Read the values of the $n variables */
+
+if (read(fd, filter_n, sizeof(filter_n)) != sizeof(filter_n)) goto DISASTER;
+
+/* If the yield is DELIVERED, NOTDELIVERED, FAIL, or FREEZE there may follow
+addresses and data to go with them. Keep them in the same order in the
+generated chain. */
+
+if (yield == FF_DELIVERED || yield == FF_NOTDELIVERED ||
+    yield == FF_FAIL || yield == FF_FREEZE)
+  {
+  address_item **nextp = generated;
+
+  for (;;)
+    {
+    int i, reply_options;
+    address_item *addr;
+    uschar *recipient;
+    uschar *expandn[EXPAND_MAXN + 2];
+
+    /* First string is the address; NULL => end of addresses */
+
+    if (!rda_read_string(fd, &recipient)) goto DISASTER;
+    if (recipient == NULL) break;
+
+    /* Hang on the end of the chain */
+
+    addr = deliver_make_addr(recipient, FALSE);
+    *nextp = addr;
+    nextp = &(addr->next);
+
+    /* Next comes the mode and the flags fields */
+
+    if (read(fd, &(addr->mode), sizeof(addr->mode)) != sizeof(addr->mode) ||
+        read(fd, &(addr->flags), sizeof(addr->flags)) != sizeof(addr->flags) ||
+        !rda_read_string(fd, &(addr->p.errors_address))) goto DISASTER;
+
+    /* Next comes a possible setting for $thisaddress and any numerical
+    variables for pipe expansion, terminated by a NULL string. The maximum
+    number of numericals is EXPAND_MAXN. Note that we put filter_thisaddress
+    into the zeroth item in the vector - this is sorted out inside the pipe
+    transport. */
+
+    for (i = 0; i < EXPAND_MAXN + 1; i++)
+      {
+      uschar *temp;
+      if (!rda_read_string(fd, &temp)) goto DISASTER;
+      if (i == 0) filter_thisaddress = temp;           /* Just in case */
+      expandn[i] = temp;
+      if (temp == NULL) break;
+      }
+
+    if (i > 0)
+      {
+      addr->pipe_expandn = store_get((i+1) * sizeof(uschar **));
+      addr->pipe_expandn[i] = NULL;
+      while (--i >= 0) addr->pipe_expandn[i] = expandn[i];
+      }
+
+    /* Then an int containing reply options; zero => no reply data. */
+
+    if (read(fd, &reply_options, sizeof(int)) != sizeof(int)) goto DISASTER;
+    if ((reply_options & REPLY_EXISTS) != 0)
+      {
+      addr->reply = store_get(sizeof(reply_item));
+
+      addr->reply->file_expand = (reply_options & REPLY_EXPAND) != 0;
+      addr->reply->return_message = (reply_options & REPLY_RETURN) != 0;
+
+      if (read(fd,&(addr->reply->expand_forbid),sizeof(int)) !=
+            sizeof(int) ||
+          read(fd,&(addr->reply->once_repeat),sizeof(time_t)) !=
+            sizeof(time_t) ||
+          !rda_read_string(fd, &(addr->reply->to)) ||
+          !rda_read_string(fd, &(addr->reply->cc)) ||
+          !rda_read_string(fd, &(addr->reply->bcc)) ||
+          !rda_read_string(fd, &(addr->reply->from)) ||
+          !rda_read_string(fd, &(addr->reply->reply_to)) ||
+          !rda_read_string(fd, &(addr->reply->subject)) ||
+          !rda_read_string(fd, &(addr->reply->headers)) ||
+          !rda_read_string(fd, &(addr->reply->text)) ||
+          !rda_read_string(fd, &(addr->reply->file)) ||
+          !rda_read_string(fd, &(addr->reply->logfile)) ||
+          !rda_read_string(fd, &(addr->reply->oncelog)))
+        goto DISASTER;
+      }
+    }
+  }
+
+/* All data has been transferred from the sub-process. Reap it, close the
+reading end of the pipe, and we are done. */
+
+WAIT_EXIT:
+while ((rc = wait(&status)) != pid)
+  {
+  if (rc < 0 && errno == ECHILD)      /* Process has vanished */
+    {
+    log_write(0, LOG_MAIN, "redirection process %d vanished unexpectedly", pid);
+    goto FINAL_EXIT;
+    }
+  }
+
+DEBUG(D_route)
+  debug_printf("rda_interpret: subprocess yield=%d error=%s\n", yield, *error);
+
+if (had_disaster)
+  {
+  *error = string_sprintf("internal problem in %s: failure to transfer "
+    "data from subprocess: status=%04x%s%s%s", rname,
+    status, readerror,
+    (*error == NULL)? US"" : US": error=",
+    (*error == NULL)? US"" : *error);
+  log_write(0, LOG_MAIN|LOG_PANIC, "%s", *error);
+  }
+else if (status != 0)
+  {
+  log_write(0, LOG_MAIN|LOG_PANIC, "internal problem in %s: unexpected status "
+    "%04x from redirect subprocess (but data correctly received)", rname,
+    status);
+  }
+
+FINAL_EXIT:
+(void)close(fd);
+signal(SIGCHLD, oldsignal);   /* restore */
+return yield;
+
+
+/* Come here if the data indicates removal of a header that we can't find */
+
+DISASTER_NO_HEADER:
+readerror = US" readerror=bad header identifier";
+had_disaster = TRUE;
+yield = FF_ERROR;
+goto WAIT_EXIT;
+
+/* Come here is there's a shambles in transferring the data over the pipe. The
+value of errno should still be set. */
+
+DISASTER:
+readerror = string_sprintf(" readerror='%s'", strerror(errno));
+had_disaster = TRUE;
+yield = FF_ERROR;
+goto WAIT_EXIT;
+}
+
+/* End of rda.c */
diff --git a/src/readconf.c b/src/readconf.c
new file mode 100644 (file)
index 0000000..adb538c
--- /dev/null
@@ -0,0 +1,4151 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2014 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Functions for reading the configuration file, and for displaying
+overall configuration values. Thanks to Brian Candler for the original
+implementation of the conditional .ifdef etc. */
+
+#include "exim.h"
+
+#define CSTATE_STACK_SIZE 10
+
+
+/* Structure for chain (stack) of .included files */
+
+typedef struct config_file_item {
+  struct config_file_item *next;
+  uschar *filename;
+  FILE *file;
+  int lineno;
+} config_file_item;
+
+/* Structure of table of conditional words and their state transitions */
+
+typedef struct cond_item {
+  uschar *name;
+  int    namelen;
+  int    action1;
+  int    action2;
+  int    pushpop;
+} cond_item;
+
+/* Structure of table of syslog facility names and values */
+
+typedef struct syslog_fac_item {
+  uschar *name;
+  int    value;
+} syslog_fac_item;
+
+
+/* Static variables */
+
+static config_file_item *config_file_stack = NULL;  /* For includes */
+
+static uschar *syslog_facility_str  = NULL;
+static uschar next_section[24];
+static uschar time_buffer[24];
+
+/* State variables for conditional loading (.ifdef / .else / .endif) */
+
+static int cstate = 0;
+static int cstate_stack_ptr = -1;
+static int cstate_stack[CSTATE_STACK_SIZE];
+
+/* Table of state transitions for handling conditional inclusions. There are
+four possible state transitions:
+
+  .ifdef true
+  .ifdef false
+  .elifdef true  (or .else)
+  .elifdef false
+
+.endif just causes the previous cstate to be popped off the stack */
+
+static int next_cstate[3][4] =
+  {
+  /* State 0: reading from file, or reading until next .else or .endif */
+  { 0, 1, 2, 2 },
+  /* State 1: condition failed, skipping until next .else or .endif */
+  { 2, 2, 0, 1 },
+  /* State 2: skipping until .endif */
+  { 2, 2, 2, 2 },
+  };
+
+/* Table of conditionals and the states to set. For each name, there are four
+values: the length of the name (to save computing it each time), the state to
+set if a macro was found in the line, the state to set if a macro was not found
+in the line, and a stack manipulation setting which is:
+
+  -1   pull state value off the stack
+   0   don't alter the stack
+  +1   push value onto stack, before setting new state
+*/
+
+static cond_item cond_list[] = {
+  { US"ifdef",    5, 0, 1,  1 },
+  { US"ifndef",   6, 1, 0,  1 },
+  { US"elifdef",  7, 2, 3,  0 },
+  { US"elifndef", 8, 3, 2,  0 },
+  { US"else",     4, 2, 2,  0 },
+  { US"endif",    5, 0, 0, -1 }
+};
+
+static int cond_list_size = sizeof(cond_list)/sizeof(cond_item);
+
+/* Table of syslog facility names and their values */
+
+static syslog_fac_item syslog_list[] = {
+  { US"mail",   LOG_MAIL },
+  { US"user",   LOG_USER },
+  { US"news",   LOG_NEWS },
+  { US"uucp",   LOG_UUCP },
+  { US"local0", LOG_LOCAL0 },
+  { US"local1", LOG_LOCAL1 },
+  { US"local2", LOG_LOCAL2 },
+  { US"local3", LOG_LOCAL3 },
+  { US"local4", LOG_LOCAL4 },
+  { US"local5", LOG_LOCAL5 },
+  { US"local6", LOG_LOCAL6 },
+  { US"local7", LOG_LOCAL7 },
+  { US"daemon", LOG_DAEMON }
+};
+
+static int syslog_list_size = sizeof(syslog_list)/sizeof(syslog_fac_item);
+
+
+
+
+/*************************************************
+*           Main configuration options           *
+*************************************************/
+
+/* The list of options that can be set in the main configuration file. This
+must be in alphabetic order because it is searched by binary chop. */
+
+static optionlist optionlist_config[] = {
+  { "*set_exim_group",          opt_bool|opt_hidden, &exim_gid_set },
+  { "*set_exim_user",           opt_bool|opt_hidden, &exim_uid_set },
+  { "*set_system_filter_group", opt_bool|opt_hidden, &system_filter_gid_set },
+  { "*set_system_filter_user",  opt_bool|opt_hidden, &system_filter_uid_set },
+  { "accept_8bitmime",          opt_bool,        &accept_8bitmime },
+  { "acl_not_smtp",             opt_stringptr,   &acl_not_smtp },
+#ifdef WITH_CONTENT_SCAN
+  { "acl_not_smtp_mime",        opt_stringptr,   &acl_not_smtp_mime },
+#endif
+  { "acl_not_smtp_start",       opt_stringptr,   &acl_not_smtp_start },
+  { "acl_smtp_auth",            opt_stringptr,   &acl_smtp_auth },
+  { "acl_smtp_connect",         opt_stringptr,   &acl_smtp_connect },
+  { "acl_smtp_data",            opt_stringptr,   &acl_smtp_data },
+#ifndef DISABLE_PRDR
+  { "acl_smtp_data_prdr",       opt_stringptr,   &acl_smtp_data_prdr },
+#endif
+#ifndef DISABLE_DKIM
+  { "acl_smtp_dkim",            opt_stringptr,   &acl_smtp_dkim },
+#endif
+  { "acl_smtp_etrn",            opt_stringptr,   &acl_smtp_etrn },
+  { "acl_smtp_expn",            opt_stringptr,   &acl_smtp_expn },
+  { "acl_smtp_helo",            opt_stringptr,   &acl_smtp_helo },
+  { "acl_smtp_mail",            opt_stringptr,   &acl_smtp_mail },
+  { "acl_smtp_mailauth",        opt_stringptr,   &acl_smtp_mailauth },
+#ifdef WITH_CONTENT_SCAN
+  { "acl_smtp_mime",            opt_stringptr,   &acl_smtp_mime },
+#endif
+  { "acl_smtp_notquit",         opt_stringptr,   &acl_smtp_notquit },
+  { "acl_smtp_predata",         opt_stringptr,   &acl_smtp_predata },
+  { "acl_smtp_quit",            opt_stringptr,   &acl_smtp_quit },
+  { "acl_smtp_rcpt",            opt_stringptr,   &acl_smtp_rcpt },
+#ifdef SUPPORT_TLS
+  { "acl_smtp_starttls",        opt_stringptr,   &acl_smtp_starttls },
+#endif
+  { "acl_smtp_vrfy",            opt_stringptr,   &acl_smtp_vrfy },
+  { "admin_groups",             opt_gidlist,     &admin_groups },
+  { "allow_domain_literals",    opt_bool,        &allow_domain_literals },
+  { "allow_mx_to_ip",           opt_bool,        &allow_mx_to_ip },
+  { "allow_utf8_domains",       opt_bool,        &allow_utf8_domains },
+  { "auth_advertise_hosts",     opt_stringptr,   &auth_advertise_hosts },
+  { "auto_thaw",                opt_time,        &auto_thaw },
+#ifdef WITH_CONTENT_SCAN
+  { "av_scanner",               opt_stringptr,   &av_scanner },
+#endif
+  { "bi_command",               opt_stringptr,   &bi_command },
+#ifdef EXPERIMENTAL_BRIGHTMAIL
+  { "bmi_config_file",          opt_stringptr,   &bmi_config_file },
+#endif
+  { "bounce_message_file",      opt_stringptr,   &bounce_message_file },
+  { "bounce_message_text",      opt_stringptr,   &bounce_message_text },
+  { "bounce_return_body",       opt_bool,        &bounce_return_body },
+  { "bounce_return_message",    opt_bool,        &bounce_return_message },
+  { "bounce_return_size_limit", opt_mkint,       &bounce_return_size_limit },
+  { "bounce_sender_authentication",opt_stringptr,&bounce_sender_authentication },
+  { "callout_domain_negative_expire", opt_time,  &callout_cache_domain_negative_expire },
+  { "callout_domain_positive_expire", opt_time,  &callout_cache_domain_positive_expire },
+  { "callout_negative_expire",  opt_time,        &callout_cache_negative_expire },
+  { "callout_positive_expire",  opt_time,        &callout_cache_positive_expire },
+  { "callout_random_local_part",opt_stringptr,   &callout_random_local_part },
+  { "check_log_inodes",         opt_int,         &check_log_inodes },
+  { "check_log_space",          opt_Kint,        &check_log_space },
+  { "check_rfc2047_length",     opt_bool,        &check_rfc2047_length },
+  { "check_spool_inodes",       opt_int,         &check_spool_inodes },
+  { "check_spool_space",        opt_Kint,        &check_spool_space },
+  { "daemon_smtp_port",         opt_stringptr|opt_hidden, &daemon_smtp_port },
+  { "daemon_smtp_ports",        opt_stringptr,   &daemon_smtp_port },
+  { "daemon_startup_retries",   opt_int,         &daemon_startup_retries },
+  { "daemon_startup_sleep",     opt_time,        &daemon_startup_sleep },
+#ifdef EXPERIMENTAL_DCC
+  { "dcc_direct_add_header",    opt_bool,        &dcc_direct_add_header },
+  { "dccifd_address",           opt_stringptr,   &dccifd_address },
+  { "dccifd_options",           opt_stringptr,   &dccifd_options },
+#endif
+  { "delay_warning",            opt_timelist,    &delay_warning },
+  { "delay_warning_condition",  opt_stringptr,   &delay_warning_condition },
+  { "deliver_drop_privilege",   opt_bool,        &deliver_drop_privilege },
+  { "deliver_queue_load_max",   opt_fixed,       &deliver_queue_load_max },
+  { "delivery_date_remove",     opt_bool,        &delivery_date_remove },
+#ifdef ENABLE_DISABLE_FSYNC
+  { "disable_fsync",            opt_bool,        &disable_fsync },
+#endif
+  { "disable_ipv6",             opt_bool,        &disable_ipv6 },
+#ifndef DISABLE_DKIM
+  { "dkim_verify_signers",      opt_stringptr,   &dkim_verify_signers },
+#endif
+#ifdef EXPERIMENTAL_DMARC
+  { "dmarc_forensic_sender",    opt_stringptr,   &dmarc_forensic_sender },
+  { "dmarc_history_file",       opt_stringptr,   &dmarc_history_file },
+  { "dmarc_tld_file",           opt_stringptr,   &dmarc_tld_file },
+#endif
+  { "dns_again_means_nonexist", opt_stringptr,   &dns_again_means_nonexist },
+  { "dns_check_names_pattern",  opt_stringptr,   &check_dns_names_pattern },
+  { "dns_csa_search_limit",     opt_int,         &dns_csa_search_limit },
+  { "dns_csa_use_reverse",      opt_bool,        &dns_csa_use_reverse },
+  { "dns_dnssec_ok",            opt_int,         &dns_dnssec_ok },
+  { "dns_ipv4_lookup",          opt_stringptr,   &dns_ipv4_lookup },
+  { "dns_retrans",              opt_time,        &dns_retrans },
+  { "dns_retry",                opt_int,         &dns_retry },
+  { "dns_use_edns0",            opt_int,         &dns_use_edns0 },
+ /* This option is now a no-op, retained for compability */
+  { "drop_cr",                  opt_bool,        &drop_cr },
+/*********************************************************/
+#ifdef EXPERIMENTAL_DSN
+  { "dsn_advertise_hosts",      opt_stringptr,   &dsn_advertise_hosts },
+#endif
+  { "dsn_from",                 opt_stringptr,   &dsn_from },
+  { "envelope_to_remove",       opt_bool,        &envelope_to_remove },
+  { "errors_copy",              opt_stringptr,   &errors_copy },
+  { "errors_reply_to",          opt_stringptr,   &errors_reply_to },
+  { "exim_group",               opt_gid,         &exim_gid },
+  { "exim_path",                opt_stringptr,   &exim_path },
+  { "exim_user",                opt_uid,         &exim_uid },
+  { "extra_local_interfaces",   opt_stringptr,   &extra_local_interfaces },
+  { "extract_addresses_remove_arguments", opt_bool, &extract_addresses_remove_arguments },
+  { "finduser_retries",         opt_int,         &finduser_retries },
+  { "freeze_tell",              opt_stringptr,   &freeze_tell },
+  { "gecos_name",               opt_stringptr,   &gecos_name },
+  { "gecos_pattern",            opt_stringptr,   &gecos_pattern },
+#ifdef SUPPORT_TLS
+  { "gnutls_allow_auto_pkcs11", opt_bool,        &gnutls_allow_auto_pkcs11 },
+  { "gnutls_compat_mode",       opt_bool,        &gnutls_compat_mode },
+  /* These three gnutls_require_* options stopped working in Exim 4.80 */
+  /* From 4.83 we log a warning; a future relase will remove them */
+  { "gnutls_require_kx",        opt_stringptr,   &gnutls_require_kx },
+  { "gnutls_require_mac",       opt_stringptr,   &gnutls_require_mac },
+  { "gnutls_require_protocols", opt_stringptr,   &gnutls_require_proto },
+#endif
+  { "header_line_maxsize",      opt_int,         &header_line_maxsize },
+  { "header_maxsize",           opt_int,         &header_maxsize },
+  { "headers_charset",          opt_stringptr,   &headers_charset },
+  { "helo_accept_junk_hosts",   opt_stringptr,   &helo_accept_junk_hosts },
+  { "helo_allow_chars",         opt_stringptr,   &helo_allow_chars },
+  { "helo_lookup_domains",      opt_stringptr,   &helo_lookup_domains },
+  { "helo_try_verify_hosts",    opt_stringptr,   &helo_try_verify_hosts },
+  { "helo_verify_hosts",        opt_stringptr,   &helo_verify_hosts },
+  { "hold_domains",             opt_stringptr,   &hold_domains },
+  { "host_lookup",              opt_stringptr,   &host_lookup },
+  { "host_lookup_order",        opt_stringptr,   &host_lookup_order },
+  { "host_reject_connection",   opt_stringptr,   &host_reject_connection },
+  { "hosts_connection_nolog",   opt_stringptr,   &hosts_connection_nolog },
+  { "hosts_treat_as_local",     opt_stringptr,   &hosts_treat_as_local },
+#ifdef LOOKUP_IBASE
+  { "ibase_servers",            opt_stringptr,   &ibase_servers },
+#endif
+  { "ignore_bounce_errors_after", opt_time,      &ignore_bounce_errors_after },
+  { "ignore_fromline_hosts",    opt_stringptr,   &ignore_fromline_hosts },
+  { "ignore_fromline_local",    opt_bool,        &ignore_fromline_local },
+  { "keep_malformed",           opt_time,        &keep_malformed },
+#ifdef LOOKUP_LDAP
+  { "ldap_ca_cert_dir",         opt_stringptr,   &eldap_ca_cert_dir },
+  { "ldap_ca_cert_file",        opt_stringptr,   &eldap_ca_cert_file },
+  { "ldap_cert_file",           opt_stringptr,   &eldap_cert_file },
+  { "ldap_cert_key",            opt_stringptr,   &eldap_cert_key },
+  { "ldap_cipher_suite",        opt_stringptr,   &eldap_cipher_suite },
+  { "ldap_default_servers",     opt_stringptr,   &eldap_default_servers },
+  { "ldap_require_cert",        opt_stringptr,   &eldap_require_cert },
+  { "ldap_start_tls",           opt_bool,        &eldap_start_tls },
+  { "ldap_version",             opt_int,         &eldap_version },
+#endif
+  { "local_from_check",         opt_bool,        &local_from_check },
+  { "local_from_prefix",        opt_stringptr,   &local_from_prefix },
+  { "local_from_suffix",        opt_stringptr,   &local_from_suffix },
+  { "local_interfaces",         opt_stringptr,   &local_interfaces },
+  { "local_scan_timeout",       opt_time,        &local_scan_timeout },
+  { "local_sender_retain",      opt_bool,        &local_sender_retain },
+  { "localhost_number",         opt_stringptr,   &host_number_string },
+  { "log_file_path",            opt_stringptr,   &log_file_path },
+  { "log_selector",             opt_stringptr,   &log_selector_string },
+  { "log_timezone",             opt_bool,        &log_timezone },
+  { "lookup_open_max",          opt_int,         &lookup_open_max },
+  { "max_username_length",      opt_int,         &max_username_length },
+  { "message_body_newlines",    opt_bool,        &message_body_newlines },
+  { "message_body_visible",     opt_mkint,       &message_body_visible },
+  { "message_id_header_domain", opt_stringptr,   &message_id_domain },
+  { "message_id_header_text",   opt_stringptr,   &message_id_text },
+  { "message_logs",             opt_bool,        &message_logs },
+  { "message_size_limit",       opt_stringptr,   &message_size_limit },
+#ifdef SUPPORT_MOVE_FROZEN_MESSAGES
+  { "move_frozen_messages",     opt_bool,        &move_frozen_messages },
+#endif
+  { "mua_wrapper",              opt_bool,        &mua_wrapper },
+#ifdef LOOKUP_MYSQL
+  { "mysql_servers",            opt_stringptr,   &mysql_servers },
+#endif
+  { "never_users",              opt_uidlist,     &never_users },
+#ifdef SUPPORT_TLS
+  { "openssl_options",          opt_stringptr,   &openssl_options },
+#endif
+#ifdef LOOKUP_ORACLE
+  { "oracle_servers",           opt_stringptr,   &oracle_servers },
+#endif
+  { "percent_hack_domains",     opt_stringptr,   &percent_hack_domains },
+#ifdef EXIM_PERL
+  { "perl_at_start",            opt_bool,        &opt_perl_at_start },
+  { "perl_startup",             opt_stringptr,   &opt_perl_startup },
+#endif
+#ifdef LOOKUP_PGSQL
+  { "pgsql_servers",            opt_stringptr,   &pgsql_servers },
+#endif
+  { "pid_file_path",            opt_stringptr,   &pid_file_path },
+  { "pipelining_advertise_hosts", opt_stringptr, &pipelining_advertise_hosts },
+#ifndef DISABLE_PRDR
+  { "prdr_enable",              opt_bool,        &prdr_enable },
+#endif
+  { "preserve_message_logs",    opt_bool,        &preserve_message_logs },
+  { "primary_hostname",         opt_stringptr,   &primary_hostname },
+  { "print_topbitchars",        opt_bool,        &print_topbitchars },
+  { "process_log_path",         opt_stringptr,   &process_log_path },
+  { "prod_requires_admin",      opt_bool,        &prod_requires_admin },
+#ifdef EXPERIMENTAL_PROXY
+  { "proxy_required_hosts",     opt_stringptr,   &proxy_required_hosts },
+#endif
+  { "qualify_domain",           opt_stringptr,   &qualify_domain_sender },
+  { "qualify_recipient",        opt_stringptr,   &qualify_domain_recipient },
+  { "queue_domains",            opt_stringptr,   &queue_domains },
+  { "queue_list_requires_admin",opt_bool,        &queue_list_requires_admin },
+  { "queue_only",               opt_bool,        &queue_only },
+  { "queue_only_file",          opt_stringptr,   &queue_only_file },
+  { "queue_only_load",          opt_fixed,       &queue_only_load },
+  { "queue_only_load_latch",    opt_bool,        &queue_only_load_latch },
+  { "queue_only_override",      opt_bool,        &queue_only_override },
+  { "queue_run_in_order",       opt_bool,        &queue_run_in_order },
+  { "queue_run_max",            opt_int,         &queue_run_max },
+  { "queue_smtp_domains",       opt_stringptr,   &queue_smtp_domains },
+  { "receive_timeout",          opt_time,        &receive_timeout },
+  { "received_header_text",     opt_stringptr,   &received_header_text },
+  { "received_headers_max",     opt_int,         &received_headers_max },
+  { "recipient_unqualified_hosts", opt_stringptr, &recipient_unqualified_hosts },
+  { "recipients_max",           opt_int,         &recipients_max },
+  { "recipients_max_reject",    opt_bool,        &recipients_max_reject },
+#ifdef EXPERIMENTAL_REDIS
+  { "redis_servers",            opt_stringptr,   &redis_servers },
+#endif
+  { "remote_max_parallel",      opt_int,         &remote_max_parallel },
+  { "remote_sort_domains",      opt_stringptr,   &remote_sort_domains },
+  { "retry_data_expire",        opt_time,        &retry_data_expire },
+  { "retry_interval_max",       opt_time,        &retry_interval_max },
+  { "return_path_remove",       opt_bool,        &return_path_remove },
+  { "return_size_limit",        opt_mkint|opt_hidden, &bounce_return_size_limit },
+  { "rfc1413_hosts",            opt_stringptr,   &rfc1413_hosts },
+  { "rfc1413_query_timeout",    opt_time,        &rfc1413_query_timeout },
+  { "sender_unqualified_hosts", opt_stringptr,   &sender_unqualified_hosts },
+  { "smtp_accept_keepalive",    opt_bool,        &smtp_accept_keepalive },
+  { "smtp_accept_max",          opt_int,         &smtp_accept_max },
+  { "smtp_accept_max_nonmail",  opt_int,         &smtp_accept_max_nonmail },
+  { "smtp_accept_max_nonmail_hosts", opt_stringptr, &smtp_accept_max_nonmail_hosts },
+  { "smtp_accept_max_per_connection", opt_int,   &smtp_accept_max_per_connection },
+  { "smtp_accept_max_per_host", opt_stringptr,   &smtp_accept_max_per_host },
+  { "smtp_accept_queue",        opt_int,         &smtp_accept_queue },
+  { "smtp_accept_queue_per_connection", opt_int, &smtp_accept_queue_per_connection },
+  { "smtp_accept_reserve",      opt_int,         &smtp_accept_reserve },
+  { "smtp_active_hostname",     opt_stringptr,   &raw_active_hostname },
+  { "smtp_banner",              opt_stringptr,   &smtp_banner },
+  { "smtp_check_spool_space",   opt_bool,        &smtp_check_spool_space },
+  { "smtp_connect_backlog",     opt_int,         &smtp_connect_backlog },
+  { "smtp_enforce_sync",        opt_bool,        &smtp_enforce_sync },
+  { "smtp_etrn_command",        opt_stringptr,   &smtp_etrn_command },
+  { "smtp_etrn_serialize",      opt_bool,        &smtp_etrn_serialize },
+  { "smtp_load_reserve",        opt_fixed,       &smtp_load_reserve },
+  { "smtp_max_synprot_errors",  opt_int,         &smtp_max_synprot_errors },
+  { "smtp_max_unknown_commands",opt_int,         &smtp_max_unknown_commands },
+  { "smtp_ratelimit_hosts",     opt_stringptr,   &smtp_ratelimit_hosts },
+  { "smtp_ratelimit_mail",      opt_stringptr,   &smtp_ratelimit_mail },
+  { "smtp_ratelimit_rcpt",      opt_stringptr,   &smtp_ratelimit_rcpt },
+  { "smtp_receive_timeout",     opt_time,        &smtp_receive_timeout },
+  { "smtp_reserve_hosts",       opt_stringptr,   &smtp_reserve_hosts },
+  { "smtp_return_error_details",opt_bool,        &smtp_return_error_details },
+#ifdef WITH_CONTENT_SCAN
+  { "spamd_address",            opt_stringptr,   &spamd_address },
+#endif
+#ifdef EXPERIMENTAL_SPF
+  { "spf_guess",                opt_stringptr,   &spf_guess },
+#endif
+  { "split_spool_directory",    opt_bool,        &split_spool_directory },
+  { "spool_directory",          opt_stringptr,   &spool_directory },
+#ifdef LOOKUP_SQLITE
+  { "sqlite_lock_timeout",      opt_int,         &sqlite_lock_timeout },
+#endif
+#ifdef EXPERIMENTAL_SRS
+  { "srs_config",               opt_stringptr,   &srs_config },
+  { "srs_hashlength",           opt_int,         &srs_hashlength },
+  { "srs_hashmin",              opt_int,         &srs_hashmin },
+  { "srs_maxage",               opt_int,         &srs_maxage },
+  { "srs_secrets",              opt_stringptr,   &srs_secrets },
+  { "srs_usehash",              opt_bool,        &srs_usehash },
+  { "srs_usetimestamp",         opt_bool,        &srs_usetimestamp },
+#endif
+  { "strict_acl_vars",          opt_bool,        &strict_acl_vars },
+  { "strip_excess_angle_brackets", opt_bool,     &strip_excess_angle_brackets },
+  { "strip_trailing_dot",       opt_bool,        &strip_trailing_dot },
+  { "syslog_duplication",       opt_bool,        &syslog_duplication },
+  { "syslog_facility",          opt_stringptr,   &syslog_facility_str },
+  { "syslog_processname",       opt_stringptr,   &syslog_processname },
+  { "syslog_timestamp",         opt_bool,        &syslog_timestamp },
+  { "system_filter",            opt_stringptr,   &system_filter },
+  { "system_filter_directory_transport", opt_stringptr,&system_filter_directory_transport },
+  { "system_filter_file_transport",opt_stringptr,&system_filter_file_transport },
+  { "system_filter_group",      opt_gid,         &system_filter_gid },
+  { "system_filter_pipe_transport",opt_stringptr,&system_filter_pipe_transport },
+  { "system_filter_reply_transport",opt_stringptr,&system_filter_reply_transport },
+  { "system_filter_user",       opt_uid,         &system_filter_uid },
+  { "tcp_nodelay",              opt_bool,        &tcp_nodelay },
+#ifdef USE_TCP_WRAPPERS
+  { "tcp_wrappers_daemon_name", opt_stringptr,   &tcp_wrappers_daemon_name },
+#endif
+  { "timeout_frozen_after",     opt_time,        &timeout_frozen_after },
+  { "timezone",                 opt_stringptr,   &timezone_string },
+#ifdef SUPPORT_TLS
+  { "tls_advertise_hosts",      opt_stringptr,   &tls_advertise_hosts },
+  { "tls_certificate",          opt_stringptr,   &tls_certificate },
+  { "tls_crl",                  opt_stringptr,   &tls_crl },
+  { "tls_dh_max_bits",          opt_int,         &tls_dh_max_bits },
+  { "tls_dhparam",              opt_stringptr,   &tls_dhparam },
+# ifndef DISABLE_OCSP
+  { "tls_ocsp_file",            opt_stringptr,   &tls_ocsp_file },
+# endif
+  { "tls_on_connect_ports",     opt_stringptr,   &tls_in.on_connect_ports },
+  { "tls_privatekey",           opt_stringptr,   &tls_privatekey },
+  { "tls_remember_esmtp",       opt_bool,        &tls_remember_esmtp },
+  { "tls_require_ciphers",      opt_stringptr,   &tls_require_ciphers },
+  { "tls_try_verify_hosts",     opt_stringptr,   &tls_try_verify_hosts },
+  { "tls_verify_certificates",  opt_stringptr,   &tls_verify_certificates },
+  { "tls_verify_hosts",         opt_stringptr,   &tls_verify_hosts },
+#endif
+  { "trusted_groups",           opt_gidlist,     &trusted_groups },
+  { "trusted_users",            opt_uidlist,     &trusted_users },
+  { "unknown_login",            opt_stringptr,   &unknown_login },
+  { "unknown_username",         opt_stringptr,   &unknown_username },
+  { "untrusted_set_sender",     opt_stringptr,   &untrusted_set_sender },
+  { "uucp_from_pattern",        opt_stringptr,   &uucp_from_pattern },
+  { "uucp_from_sender",         opt_stringptr,   &uucp_from_sender },
+  { "warn_message_file",        opt_stringptr,   &warn_message_file },
+  { "write_rejectlog",          opt_bool,        &write_rejectlog }
+};
+
+static int optionlist_config_size =
+  sizeof(optionlist_config)/sizeof(optionlist);
+
+
+
+/*************************************************
+*         Find the name of an option             *
+*************************************************/
+
+/* This function is to aid debugging. Various functions take arguments that are
+pointer variables in the options table or in option tables for various drivers.
+For debugging output, it is useful to be able to find the name of the option
+which is currently being processed. This function finds it, if it exists, by
+searching the table(s).
+
+Arguments:   a value that is presumed to be in the table above
+Returns:     the option name, or an empty string
+*/
+
+uschar *
+readconf_find_option(void *p)
+{
+int i;
+router_instance *r;
+transport_instance *t;
+
+for (i = 0; i < optionlist_config_size; i++)
+  if (p == optionlist_config[i].value) return US optionlist_config[i].name;
+
+for (r = routers; r != NULL; r = r->next)
+  {
+  router_info *ri = r->info;
+  for (i = 0; i < ri->options_count[0]; i++)
+    {
+    if ((ri->options[i].type & opt_mask) != opt_stringptr) continue;
+    if (p == (char *)(r->options_block) + (long int)(ri->options[i].value))
+      return US ri->options[i].name;
+    }
+  }
+
+for (t = transports; t != NULL; t = t->next)
+  {
+  transport_info *ti = t->info;
+  for (i = 0; i < ti->options_count[0]; i++)
+    {
+    if ((ti->options[i].type & opt_mask) != opt_stringptr) continue;
+    if (p == (char *)(t->options_block) + (long int)(ti->options[i].value))
+      return US ti->options[i].name;
+    }
+  }
+
+return US"";
+}
+
+
+
+
+/*************************************************
+*       Deal with an assignment to a macro       *
+*************************************************/
+
+/* This function is called when a line that starts with an upper case letter is
+encountered. The argument "line" should contain a complete logical line, and
+start with the first letter of the macro name. The macro name and the
+replacement text are extracted and stored. Redefinition of existing,
+non-command line, macros is permitted using '==' instead of '='.
+
+Arguments:
+  s            points to the start of the logical line
+
+Returns:       nothing
+*/
+
+static void
+read_macro_assignment(uschar *s)
+{
+uschar name[64];
+int namelen = 0;
+BOOL redef = FALSE;
+macro_item *m;
+macro_item *mlast = NULL;
+
+while (isalnum(*s) || *s == '_')
+  {
+  if (namelen >= sizeof(name) - 1)
+    log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN,
+      "macro name too long (maximum is " SIZE_T_FMT " characters)", sizeof(name) - 1);
+  name[namelen++] = *s++;
+  }
+name[namelen] = 0;
+
+while (isspace(*s)) s++;
+if (*s++ != '=')
+  log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, "malformed macro definition");
+
+if (*s == '=')
+  {
+  redef = TRUE;
+  s++;
+  }
+while (isspace(*s)) s++;
+
+/* If an existing macro of the same name was defined on the command line, we
+just skip this definition. It's an error to attempt to redefine a macro without
+redef set to TRUE, or to redefine a macro when it hasn't been defined earlier.
+It is also an error to define a macro whose name begins with the name of a
+previously defined macro. Note: it is documented that the other way round
+works. */
+
+for (m = macros; m != NULL; m = m->next)
+  {
+  int len = Ustrlen(m->name);
+
+  if (Ustrcmp(m->name, name) == 0)
+    {
+    if (!m->command_line && !redef)
+      log_write(0, LOG_CONFIG|LOG_PANIC_DIE, "macro \"%s\" is already "
+       "defined (use \"==\" if you want to redefine it", name);
+    break;
+    }
+
+  if (len < namelen && Ustrstr(name, m->name) != NULL)
+    log_write(0, LOG_CONFIG|LOG_PANIC_DIE, "\"%s\" cannot be defined as "
+      "a macro because previously defined macro \"%s\" is a substring",
+      name, m->name);
+
+  /* We cannot have this test, because it is documented that a substring
+  macro is permitted (there is even an example).
+  *
+  * if (len > namelen && Ustrstr(m->name, name) != NULL)
+  *   log_write(0, LOG_CONFIG|LOG_PANIC_DIE, "\"%s\" cannot be defined as "
+  *     "a macro because it is a substring of previously defined macro \"%s\"",
+  *     name, m->name);
+  */
+
+  mlast = m;
+  }
+
+/* Check for an overriding command-line definition. */
+
+if (m != NULL && m->command_line) return;
+
+/* Redefinition must refer to an existing macro. */
+
+if (redef)
+  {
+  if (m == NULL)
+    log_write(0, LOG_CONFIG|LOG_PANIC_DIE, "can't redefine an undefined macro "
+      "\"%s\"", name);
+  }
+
+/* We have a new definition. The macro_item structure includes a final vector
+called "name" which is one byte long. Thus, adding "namelen" gives us enough
+room to store the "name" string. */
+
+else
+  {
+  m = store_get(sizeof(macro_item) + namelen);
+  if (macros == NULL) macros = m; else mlast->next = m;
+  Ustrncpy(m->name, name, namelen);
+  m->name[namelen] = 0;
+  m->next = NULL;
+  m->command_line = FALSE;
+  }
+
+/* Set the value of the new or redefined macro */
+
+m->replacement = string_copy(s);
+}
+
+
+
+
+
+/*************************************************
+*            Read configuration line             *
+*************************************************/
+
+/* A logical line of text is read from the configuration file into the big
+buffer, taking account of macros, .includes, and continuations. The size of
+big_buffer is increased if necessary. The count of configuration lines is
+maintained. Physical input lines starting with # (ignoring leading white space,
+and after macro replacement) and empty logical lines are always ignored.
+Leading and trailing spaces are removed.
+
+If we hit a line of the form "begin xxxx", the xxxx is placed in the
+next_section vector, and the function returns NULL, indicating the end of a
+configuration section. On end-of-file, NULL is returned with next_section
+empty.
+
+Arguments:      none
+
+Returns:        a pointer to the first non-blank in the line,
+                or NULL if eof or end of section is reached
+*/
+
+static uschar *
+get_config_line(void)
+{
+int startoffset = 0;         /* To first non-blank char in logical line */
+int len = 0;                 /* Of logical line so far */
+int newlen;
+uschar *s, *ss;
+macro_item *m;
+BOOL macro_found;
+
+/* Loop for handling continuation lines, skipping comments, and dealing with
+.include files. */
+
+for (;;)
+  {
+  if (Ufgets(big_buffer+len, big_buffer_size-len, config_file) == NULL)
+    {
+    if (config_file_stack != NULL)    /* EOF inside .include */
+      {
+      (void)fclose(config_file);
+      config_file = config_file_stack->file;
+      config_filename = config_file_stack->filename;
+      config_lineno = config_file_stack->lineno;
+      config_file_stack = config_file_stack->next;
+      continue;
+      }
+
+    /* EOF at top level */
+
+    if (cstate_stack_ptr >= 0)
+      log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN,
+        "Unexpected end of configuration file: .endif missing");
+
+    if (len != 0) break;        /* EOF after continuation */
+    next_section[0] = 0;        /* EOF at start of logical line */
+    return NULL;
+    }
+
+  config_lineno++;
+  newlen = len + Ustrlen(big_buffer + len);
+
+  /* Handle pathologically long physical lines - yes, it did happen - by
+  extending big_buffer at this point. The code also copes with very long
+  logical lines. */
+
+  while (newlen == big_buffer_size - 1 && big_buffer[newlen - 1] != '\n')
+    {
+    uschar *newbuffer;
+    big_buffer_size += BIG_BUFFER_SIZE;
+    newbuffer = store_malloc(big_buffer_size);
+
+    /* This use of strcpy is OK because we know that the string in the old
+    buffer is shorter than the new buffer. */
+
+    Ustrcpy(newbuffer, big_buffer);
+    store_free(big_buffer);
+    big_buffer = newbuffer;
+    if (Ufgets(big_buffer+newlen, big_buffer_size-newlen, config_file) == NULL)
+      break;
+    newlen += Ustrlen(big_buffer + newlen);
+    }
+
+  /* Find the true start of the physical line - leading spaces are always
+  ignored. */
+
+  ss = big_buffer + len;
+  while (isspace(*ss)) ss++;
+
+  /* Process the physical line for macros. If this is the start of the logical
+  line, skip over initial text at the start of the line if it starts with an
+  upper case character followed by a sequence of name characters and an equals
+  sign, because that is the definition of a new macro, and we don't do
+  replacement therein. */
+
+  s = ss;
+  if (len == 0 && isupper(*s))
+    {
+    while (isalnum(*s) || *s == '_') s++;
+    while (isspace(*s)) s++;
+    if (*s != '=') s = ss;          /* Not a macro definition */
+    }
+
+  /* For each defined macro, scan the line (from after XXX= if present),
+  replacing all occurrences of the macro. */
+
+  macro_found = FALSE;
+  for (m = macros; m != NULL; m = m->next)
+    {
+    uschar *p, *pp;
+    uschar *t = s;
+
+    while ((p = Ustrstr(t, m->name)) != NULL)
+      {
+      int moveby;
+      int namelen = Ustrlen(m->name);
+      int replen = Ustrlen(m->replacement);
+
+      /* Expand the buffer if necessary */
+
+      while (newlen - namelen + replen + 1 > big_buffer_size)
+        {
+        int newsize = big_buffer_size + BIG_BUFFER_SIZE;
+        uschar *newbuffer = store_malloc(newsize);
+        memcpy(newbuffer, big_buffer, newlen + 1);
+        p = newbuffer  + (p - big_buffer);
+        s = newbuffer  + (s - big_buffer);
+        ss = newbuffer + (ss - big_buffer);
+        t = newbuffer  + (t - big_buffer);
+        big_buffer_size = newsize;
+        store_free(big_buffer);
+        big_buffer = newbuffer;
+        }
+
+      /* Shuffle the remaining characters up or down in the buffer before
+      copying in the replacement text. Don't rescan the replacement for this
+      same macro. */
+
+      pp = p + namelen;
+      moveby = replen - namelen;
+      if (moveby != 0)
+        {
+        memmove(p + replen, pp, (big_buffer + newlen) - pp + 1);
+        newlen += moveby;
+        }
+      Ustrncpy(p, m->replacement, replen);
+      t = p + replen;
+      macro_found = TRUE;
+      }
+    }
+
+  /* An empty macro replacement at the start of a line could mean that ss no
+  longer points to the first non-blank character. */
+
+  while (isspace(*ss)) ss++;
+
+  /* Check for comment lines - these are physical lines. */
+
+  if (*ss == '#') continue;
+
+  /* Handle conditionals, which are also applied to physical lines. Conditions
+  are of the form ".ifdef ANYTEXT" and are treated as true if any macro
+  expansion occured on the rest of the line. A preliminary test for the leading
+  '.' saves effort on most lines. */
+
+  if (*ss == '.')
+    {
+    int i;
+
+    /* Search the list of conditional directives */
+
+    for (i = 0; i < cond_list_size; i++)
+      {
+      int n;
+      cond_item *c = cond_list+i;
+      if (Ustrncmp(ss+1, c->name, c->namelen) != 0) continue;
+
+      /* The following character must be white space or end of string */
+
+      n = ss[1 + c->namelen];
+      if (n != ' ' && n != 't' && n != '\n' && n != 0) break;
+
+      /* .ifdef and .ifndef push the current state onto the stack, then set
+      a new one from the table. Stack overflow is an error */
+
+      if (c->pushpop > 0)
+        {
+        if (cstate_stack_ptr >= CSTATE_STACK_SIZE - 1)
+          log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN,
+            ".%s nested too deeply", c->name);
+        cstate_stack[++cstate_stack_ptr] = cstate;
+        cstate = next_cstate[cstate][macro_found? c->action1 : c->action2];
+        }
+
+      /* For any of the others, stack underflow is an error. The next state
+      comes either from the stack (.endif) or from the table. */
+
+      else
+        {
+        if (cstate_stack_ptr < 0)
+          log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN,
+            ".%s without matching .ifdef", c->name);
+        cstate = (c->pushpop < 0)? cstate_stack[cstate_stack_ptr--] :
+          next_cstate[cstate][macro_found? c->action1 : c->action2];
+        }
+
+      /* Having dealt with a directive, break the loop */
+
+      break;
+      }
+
+    /* If we have handled a conditional directive, continue with the next
+    physical line. Otherwise, fall through. */
+
+    if (i < cond_list_size) continue;
+    }
+
+  /* If the conditional state is not 0 (actively using these lines), ignore
+  this input line. */
+
+  if (cstate != 0) continue;  /* Conditional skip */
+
+  /* Handle .include lines - these are also physical lines. */
+
+  if (Ustrncmp(ss, ".include", 8) == 0 &&
+       (isspace(ss[8]) ||
+         (Ustrncmp(ss+8, "_if_exists", 10) == 0 && isspace(ss[18]))))
+    {
+    uschar *t;
+    int include_if_exists = isspace(ss[8])? 0 : 10;
+    config_file_item *save;
+    struct stat statbuf;
+
+    ss += 9 + include_if_exists;
+    while (isspace(*ss)) ss++;
+    t = ss + Ustrlen(ss);
+    while (t > ss && isspace(t[-1])) t--;
+    if (*ss == '\"' && t[-1] == '\"')
+      {
+      ss++;
+      t--;
+      }
+    *t = 0;
+
+    if (*ss != '/')
+      log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, ".include specifies a non-"
+        "absolute path \"%s\"", ss);
+
+    if (include_if_exists != 0 && (Ustat(ss, &statbuf) != 0)) continue;
+
+    save = store_get(sizeof(config_file_item));
+    save->next = config_file_stack;
+    config_file_stack = save;
+    save->file = config_file;
+    save->filename = config_filename;
+    save->lineno = config_lineno;
+
+    config_file = Ufopen(ss, "rb");
+    if (config_file == NULL)
+      log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, "failed to open included "
+        "configuration file %s", ss);
+    config_filename = string_copy(ss);
+    config_lineno = 0;
+    continue;
+    }
+
+  /* If this is the start of the logical line, remember where the non-blank
+  data starts. Otherwise shuffle down continuation lines to remove leading
+  white space. */
+
+  if (len == 0)
+    startoffset = ss - big_buffer;
+  else
+    {
+    s = big_buffer + len;
+    if (ss > s)
+      {
+      memmove(s, ss, (newlen - len) -  (ss - s) + 1);
+      newlen -= ss - s;
+      }
+    }
+
+  /* Accept the new addition to the line. Remove trailing white space. */
+
+  len = newlen;
+  while (len > 0 && isspace(big_buffer[len-1])) len--;
+  big_buffer[len] = 0;
+
+  /* We are done if the line does not end in backslash and contains some data.
+  Empty logical lines are ignored. For continuations, remove the backslash and
+  go round the loop to read the continuation line. */
+
+  if (len > 0)
+    {
+    if (big_buffer[len-1] != '\\') break;   /* End of logical line */
+    big_buffer[--len] = 0;                  /* Remove backslash */
+    }
+  }     /* Loop for reading multiple physical lines */
+
+/* We now have a logical line. Test for the end of a configuration section (or,
+more accurately, for the start of the next section). Place the name of the next
+section in next_section, and return NULL. If the name given is longer than
+next_section, truncate it. It will be unrecognized later, because all the known
+section names do fit. Leave space for pluralizing. */
+
+s = big_buffer + startoffset;            /* First non-space character */
+if (strncmpic(s, US"begin ", 6) == 0)
+  {
+  s += 6;
+  while (isspace(*s)) s++;
+  if (big_buffer + len - s > sizeof(next_section) - 2)
+    s[sizeof(next_section) - 2] = 0;
+  Ustrcpy(next_section, s);
+  return NULL;
+  }
+
+/* Return the first non-blank character. */
+
+return s;
+}
+
+
+
+/*************************************************
+*             Read a name                        *
+*************************************************/
+
+/* The yield is the pointer to the next uschar. Names longer than the
+output space are silently truncated. This function is also used from acl.c when
+parsing ACLs.
+
+Arguments:
+  name      where to put the name
+  len       length of name
+  s         input pointer
+
+Returns:    new input pointer
+*/
+
+uschar *
+readconf_readname(uschar *name, int len, uschar *s)
+{
+int p = 0;
+while (isspace(*s)) s++;
+if (isalpha(*s))
+  {
+  while (isalnum(*s) || *s == '_')
+    {
+    if (p < len-1) name[p++] = *s;
+    s++;
+    }
+  }
+name[p] = 0;
+while (isspace(*s)) s++;
+return s;
+}
+
+
+
+
+/*************************************************
+*          Read a time value                     *
+*************************************************/
+
+/* This function is also called from outside, to read argument
+time values. The format of a time value is:
+
+  [<n>w][<n>d][<n>h][<n>m][<n>s]
+
+as long as at least one is present. If a format error is encountered,
+return a negative value. The value must be terminated by the given
+terminator.
+
+Arguments:
+  s             input pointer
+  terminator    required terminating character
+  return_msec   if TRUE, allow fractional seconds and return milliseconds
+
+Returns:        the time value, or -1 on syntax error
+                value is seconds if return_msec is FALSE
+                value is milliseconds if return_msec is TRUE
+*/
+
+int
+readconf_readtime(uschar *s, int terminator, BOOL return_msec)
+{
+int yield = 0;
+for (;;)
+  {
+  int value, count;
+  double fraction;
+
+  if (!isdigit(*s)) return -1;
+  (void)sscanf(CS s, "%d%n", &value, &count);
+  s += count;
+
+  switch (*s)
+    {
+    case 'w': value *= 7;
+    case 'd': value *= 24;
+    case 'h': value *= 60;
+    case 'm': value *= 60;
+    case 's': s++;
+    break;
+
+    case '.':
+    if (!return_msec) return -1;
+    (void)sscanf(CS s, "%lf%n", &fraction, &count);
+    s += count;
+    if (*s++ != 's') return -1;
+    yield += (int)(fraction * 1000.0);
+    break;
+
+    default: return -1;
+    }
+
+  if (return_msec) value *= 1000;
+  yield += value;
+  if (*s == terminator) return yield;
+  }
+/* Control never reaches here. */
+}
+
+
+
+/*************************************************
+*          Read a fixed point value              *
+*************************************************/
+
+/* The value is returned *1000
+
+Arguments:
+  s           input pointer
+  terminator  required terminator
+
+Returns:      the value, or -1 on error
+*/
+
+static int
+readconf_readfixed(uschar *s, int terminator)
+{
+int yield = 0;
+int value, count;
+if (!isdigit(*s)) return -1;
+(void)sscanf(CS  s, "%d%n", &value, &count);
+s += count;
+yield = value * 1000;
+if (*s == '.')
+  {
+  int m = 100;
+  while (isdigit((*(++s))))
+    {
+    yield += (*s - '0') * m;
+    m /= 10;
+    }
+  }
+
+return (*s == terminator)? yield : (-1);
+}
+
+
+
+/*************************************************
+*            Find option in list                 *
+*************************************************/
+
+/* The lists are always in order, so binary chop can be used.
+
+Arguments:
+  name      the option name to search for
+  ol        the first entry in the option list
+  last      one more than the offset of the last entry in the option list
+
+Returns:    pointer to an option entry, or NULL if not found
+*/
+
+static optionlist *
+find_option(uschar *name, optionlist *ol, int last)
+{
+int first = 0;
+while (last > first)
+  {
+  int middle = (first + last)/2;
+  int c = Ustrcmp(name, ol[middle].name);
+  if (c == 0) return ol + middle;
+    else if (c > 0) first = middle + 1;
+      else last = middle;
+  }
+return NULL;
+}
+
+
+
+/*************************************************
+*      Find a set flag in option list            *
+*************************************************/
+
+/* Because some versions of Unix make no restrictions on the values of uids and
+gids (even negative ones), we cannot represent "unset" by a special value.
+There is therefore a separate boolean variable for each one indicating whether
+a value is set or not. This function returns a pointer to the boolean, given
+the original option name. It is a major disaster if the flag cannot be found.
+
+Arguments:
+  name          the name of the uid or gid option
+  oltop         points to the start of the relevant option list
+  last          one more than the offset of the last item in the option list
+  data_block    NULL when reading main options => data values in the option
+                  list are absolute addresses; otherwise they are byte offsets
+                  in data_block (used for driver options)
+
+Returns:        a pointer to the boolean flag.
+*/
+
+static BOOL *
+get_set_flag(uschar *name, optionlist *oltop, int last, void *data_block)
+{
+optionlist *ol;
+uschar name2[64];
+sprintf(CS name2, "*set_%.50s", name);
+ol = find_option(name2, oltop, last);
+if (ol == NULL) log_write(0, LOG_MAIN|LOG_PANIC_DIE,
+  "Exim internal error: missing set flag for %s", name);
+return (data_block == NULL)? (BOOL *)(ol->value) :
+  (BOOL *)((uschar *)data_block + (long int)(ol->value));
+}
+
+
+
+
+/*************************************************
+*    Output extra characters message and die     *
+*************************************************/
+
+/* Called when an option line has junk on the end. Sometimes this is because
+the sysadmin thinks comments are permitted.
+
+Arguments:
+  s          points to the extra characters
+  t1..t3     strings to insert in the log message
+
+Returns:     doesn't return; dies
+*/
+
+static void
+extra_chars_error(uschar *s, uschar *t1, uschar *t2, uschar *t3)
+{
+uschar *comment = US"";
+if (*s == '#') comment = US" (# is comment only at line start)";
+log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN,
+  "extra characters follow %s%s%s%s", t1, t2, t3, comment);
+}
+
+
+
+/*************************************************
+*              Read rewrite information          *
+*************************************************/
+
+/* Each line of rewrite information contains:
+
+.  A complete address in the form user@domain, possibly with
+   leading * for each part; or alternatively, a regex.
+
+.  A replacement string (which will be expanded).
+
+.  An optional sequence of one-letter flags, indicating which
+   headers etc. to apply this rule to.
+
+All this is decoded and placed into a control block. The OR of the flags is
+maintained in a common word.
+
+Arguments:
+  p           points to the string that makes up the rule
+  existflags  points to the overall flag word
+  isglobal    TRUE if reading global rewrite rules
+
+Returns:      the control block for the parsed rule.
+*/
+
+static rewrite_rule *
+readconf_one_rewrite(uschar *p, int *existflags, BOOL isglobal)
+{
+rewrite_rule *next = store_get(sizeof(rewrite_rule));
+
+next->next = NULL;
+next->key = string_dequote(&p);
+
+while (isspace(*p)) p++;
+if (*p == 0)
+  log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN,
+    "missing rewrite replacement string");
+
+next->flags = 0;
+next->replacement = string_dequote(&p);
+
+while (*p != 0) switch (*p++)
+  {
+  case ' ': case '\t': break;
+
+  case 'q': next->flags |= rewrite_quit; break;
+  case 'w': next->flags |= rewrite_whole; break;
+
+  case 'h': next->flags |= rewrite_all_headers; break;
+  case 's': next->flags |= rewrite_sender; break;
+  case 'f': next->flags |= rewrite_from; break;
+  case 't': next->flags |= rewrite_to;   break;
+  case 'c': next->flags |= rewrite_cc;   break;
+  case 'b': next->flags |= rewrite_bcc;  break;
+  case 'r': next->flags |= rewrite_replyto; break;
+
+  case 'E': next->flags |= rewrite_all_envelope; break;
+  case 'F': next->flags |= rewrite_envfrom; break;
+  case 'T': next->flags |= rewrite_envto; break;
+
+  case 'Q': next->flags |= rewrite_qualify; break;
+  case 'R': next->flags |= rewrite_repeat; break;
+
+  case 'S':
+  next->flags |= rewrite_smtp;
+  if (next->key[0] != '^' && Ustrncmp(next->key, "\\N^", 3) != 0)
+    log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN,
+      "rewrite rule has the S flag but is not a regular expression");
+  break;
+
+  default:
+  log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN,
+    "unknown rewrite flag character '%c' "
+    "(could be missing quotes round replacement item)", p[-1]);
+  break;
+  }
+
+/* If no action flags are set, set all the "normal" rewrites. */
+
+if ((next->flags & (rewrite_all | rewrite_smtp)) == 0)
+  next->flags |= isglobal? rewrite_all : rewrite_all_headers;
+
+/* Remember which exist, for optimization, and return the rule */
+
+*existflags |= next->flags;
+return next;
+}
+
+
+
+
+/*************************************************
+*          Read global rewrite information       *
+*************************************************/
+
+/* Each line is a single rewrite rule; it is parsed into a control block
+by readconf_one_rewrite(), and its flags are ORed into the global flag
+word rewrite_existflags. */
+
+void
+readconf_rewrites(void)
+{
+rewrite_rule **chain = &global_rewrite_rules;
+uschar *p;
+
+while ((p = get_config_line()) != NULL)
+  {
+  rewrite_rule *next = readconf_one_rewrite(p, &rewrite_existflags, TRUE);
+  *chain = next;
+  chain = &(next->next);
+  }
+}
+
+
+
+/*************************************************
+*               Read a string                    *
+*************************************************/
+
+/* Strings are read into the normal store pool. As long we aren't too
+near the end of the current block, the string will just use what is necessary
+on the top of the stacking pool, because string_cat() uses the extension
+mechanism.
+
+Argument:
+  s         the rest of the input line
+  name      the option name (for errors)
+
+Returns:    pointer to the string
+*/
+
+static uschar *
+read_string(uschar *s, uschar *name)
+{
+uschar *yield;
+uschar *ss;
+
+if (*s != '\"') return string_copy(s);
+
+ss = s;
+yield = string_dequote(&s);
+
+if (s == ss+1 || s[-1] != '\"')
+  log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN,
+    "missing quote at end of string value for %s", name);
+
+if (*s != 0) extra_chars_error(s, US"string value for ", name, US"");
+
+return yield;
+}
+
+
+/*************************************************
+*            Handle option line                  *
+*************************************************/
+
+/* This function is called from several places to process a line containing the
+setting of an option. The first argument is the line to be decoded; it has been
+checked not to be empty and not to start with '#'. Trailing newlines and white
+space have been removed. The second argument is a pointer to the list of
+variable names that are to be recognized, together with their types and
+locations, and the third argument gives the number of entries in the list.
+
+The fourth argument is a pointer to a data block. If it is NULL, then the data
+values in the options list are absolute addresses. Otherwise, they are byte
+offsets in the data block.
+
+String option data may continue onto several lines; this function reads further
+data from config_file if necessary.
+
+The yield of this function is normally zero. If a string continues onto
+multiple lines, then the data value is permitted to be followed by a comma
+or a semicolon (for use in drivers) and the yield is that character.
+
+Arguments:
+  buffer        contains the configuration line to be handled
+  oltop         points to the start of the relevant option list
+  last          one more than the offset of the last item in the option list
+  data_block    NULL when reading main options => data values in the option
+                  list are absolute addresses; otherwise they are byte offsets
+                  in data_block when they have opt_public set; otherwise
+                  they are byte offsets in data_block->options_block.
+  unknown_txt   format string to use in panic message for unknown option;
+                  must contain %s for option name
+                if given as NULL, don't panic on unknown option
+
+Returns:        TRUE if an option was read successfully,
+                FALSE false for an unknown option if unknown_txt == NULL,
+                  otherwise panic and die on an unknown option
+*/
+
+static BOOL
+readconf_handle_option(uschar *buffer, optionlist *oltop, int last,
+  void *data_block, uschar *unknown_txt)
+{
+int ptr = 0;
+int offset = 0;
+int n, count, type, value;
+int issecure = 0;
+uid_t uid;
+gid_t gid;
+BOOL boolvalue = TRUE;
+BOOL freesptr = TRUE;
+optionlist *ol, *ol2;
+struct passwd *pw;
+void *reset_point;
+int intbase = 0;
+uschar *inttype = US"";
+uschar *sptr;
+uschar *s = buffer;
+uschar *saved_condition, *strtemp;
+uschar **str_target;
+uschar name[64];
+uschar name2[64];
+
+/* There may be leading spaces; thereafter, we expect an option name starting
+with a letter. */
+
+while (isspace(*s)) s++;
+if (!isalpha(*s))
+  log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, "option setting expected: %s", s);
+
+/* Read the name of the option, and skip any subsequent white space. If
+it turns out that what we read was "hide", set the flag indicating that
+this is a secure option, and loop to read the next word. */
+
+for (n = 0; n < 2; n++)
+  {
+  while (isalnum(*s) || *s == '_')
+    {
+    if (ptr < sizeof(name)-1) name[ptr++] = *s;
+    s++;
+    }
+  name[ptr] = 0;
+  while (isspace(*s)) s++;
+  if (Ustrcmp(name, "hide") != 0) break;
+  issecure = opt_secure;
+  ptr = 0;
+  }
+
+/* Deal with "no_" or "not_" here for booleans */
+
+if (Ustrncmp(name, "no_", 3) == 0)
+  {
+  boolvalue = FALSE;
+  offset = 3;
+  }
+
+if (Ustrncmp(name, "not_", 4) == 0)
+  {
+  boolvalue = FALSE;
+  offset = 4;
+  }
+
+/* Search the list for the given name. A non-existent name, or an option that
+is set twice, is a disaster. */
+
+ol = find_option(name + offset, oltop, last);
+
+if (ol == NULL)
+  {
+  if (unknown_txt == NULL) return FALSE;
+  log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, CS unknown_txt, name);
+  }
+
+if ((ol->type & opt_set)  && !(ol->type & (opt_rep_con | opt_rep_str)))
+  log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN,
+    "\"%s\" option set for the second time", name);
+
+ol->type |= opt_set | issecure;
+type = ol->type & opt_mask;
+
+/* Types with data values must be followed by '='; the "no[t]_" prefix
+applies only to boolean values. */
+
+if (type < opt_bool || type > opt_bool_last)
+  {
+  if (offset != 0)
+    log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN,
+      "negation prefix applied to a non-boolean option");
+  if (*s == 0)
+    log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN,
+      "unexpected end of line (data missing) after %s", name);
+  if (*s != '=')
+    log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, "missing \"=\" after %s", name);
+  }
+
+/* If a boolean wasn't preceded by "no[t]_" it can be followed by = and
+true/false/yes/no, or, in the case of opt_expanded_bool, a general string that
+ultimately expands to one of those values. */
+
+else if (*s != 0 && (offset != 0 || *s != '='))
+  extra_chars_error(s, US"boolean option ", name, US"");
+
+/* Skip white space after = */
+
+if (*s == '=') while (isspace((*(++s))));
+
+/* If there is a data block and the opt_public flag is not set, change
+the data block pointer to the private options block. */
+
+if (data_block != NULL && (ol->type & opt_public) == 0)
+  data_block = (void *)(((driver_instance *)data_block)->options_block);
+
+/* Now get the data according to the type. */
+
+switch (type)
+  {
+  /* If a string value is not enclosed in quotes, it consists of
+  the rest of the current line, verbatim. Otherwise, string escapes
+  are processed.
+
+  A transport is specified as a string, which is then looked up in the
+  list of transports. A search type is specified as one of a number of
+  known strings.
+
+  A set or rewrite rules for a driver is specified as a string, which is
+  then parsed into a suitable chain of control blocks.
+
+  Uids and gids are specified as strings which are then looked up in the
+  passwd file. Lists of uids and gids are similarly specified as colon-
+  separated strings. */
+
+  case opt_stringptr:
+  case opt_uid:
+  case opt_gid:
+  case opt_expand_uid:
+  case opt_expand_gid:
+  case opt_uidlist:
+  case opt_gidlist:
+  case opt_rewrite:
+
+  reset_point = sptr = read_string(s, name);
+
+  /* Having read a string, we now have several different ways of using it,
+  depending on the data type, so do another switch. If keeping the actual
+  string is not required (because it is interpreted), freesptr is set TRUE,
+  and at the end we reset the pool. */
+
+  switch (type)
+    {
+    /* If this was a string, set the variable to point to the new string,
+    and set the flag so its store isn't reclaimed. If it was a list of rewrite
+    rules, we still keep the string (for printing), and parse the rules into a
+    control block and flags word. */
+
+    case opt_stringptr:
+    if (data_block == NULL)
+      str_target = (uschar **)(ol->value);
+    else
+      str_target = (uschar **)((uschar *)data_block + (long int)(ol->value));
+    if (ol->type & opt_rep_con)
+      {
+      /* We already have a condition, we're conducting a crude hack to let
+      multiple condition rules be chained together, despite storing them in
+      text form. */
+      saved_condition = *str_target;
+      strtemp = string_sprintf("${if and{{bool_lax{%s}}{bool_lax{%s}}}}",
+          saved_condition, sptr);
+      *str_target = string_copy_malloc(strtemp);
+      /* TODO(pdp): there is a memory leak here and just below
+      when we set 3 or more conditions; I still don't
+      understand the store mechanism enough to know
+      what's the safe way to free content from an earlier store.
+      AFAICT, stores stack, so freeing an early stored item also stores
+      all data alloc'd after it.  If we knew conditions were adjacent,
+      we could survive that, but we don't.  So I *think* we need to take
+      another bit from opt_type to indicate "malloced"; this seems like
+      quite a hack, especially for this one case.  It also means that
+      we can't ever reclaim the store from the *first* condition.
+
+      Because we only do this once, near process start-up, I'm prepared to
+      let this slide for the time being, even though it rankles.  */
+      }
+    else if (ol->type & opt_rep_str)
+      {
+      uschar sep = Ustrncmp(name, "headers_add", 11)==0 ? '\n' : ':';
+      uschar * cp;
+
+      /* Strip trailing whitespace and seperators */
+      for (cp = sptr + Ustrlen(sptr) - 1;
+         cp >= sptr && (*cp == '\n' || *cp == '\t' || *cp == ' ' || *cp == sep);
+         cp--) *cp = '\0';
+
+      if (cp >= sptr)
+       *str_target = string_copy_malloc(
+                     *str_target ? string_sprintf("%s%c%s", *str_target, sep, sptr)
+                                 : sptr);
+      }
+    else
+      {
+      *str_target = sptr;
+      freesptr = FALSE;
+      }
+    break;
+
+    case opt_rewrite:
+    if (data_block == NULL)
+      *((uschar **)(ol->value)) = sptr;
+    else
+      *((uschar **)((uschar *)data_block + (long int)(ol->value))) = sptr;
+    freesptr = FALSE;
+    if (type == opt_rewrite)
+      {
+      int sep = 0;
+      int *flagptr;
+      uschar *p = sptr;
+      rewrite_rule **chain;
+      optionlist *ol3;
+
+      sprintf(CS name2, "*%.50s_rules", name);
+      ol2 = find_option(name2, oltop, last);
+      sprintf(CS name2, "*%.50s_flags", name);
+      ol3 = find_option(name2, oltop, last);
+
+      if (ol2 == NULL || ol3 == NULL)
+        log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN,
+          "rewrite rules not available for driver");
+
+      if (data_block == NULL)
+        {
+        chain = (rewrite_rule **)(ol2->value);
+        flagptr = (int *)(ol3->value);
+        }
+      else
+        {
+        chain = (rewrite_rule **)((uschar *)data_block + (long int)(ol2->value));
+        flagptr = (int *)((uschar *)data_block + (long int)(ol3->value));
+        }
+
+      while ((p = string_nextinlist(&sptr, &sep, big_buffer, BIG_BUFFER_SIZE))
+              != NULL)
+        {
+        rewrite_rule *next = readconf_one_rewrite(p, flagptr, FALSE);
+        *chain = next;
+        chain = &(next->next);
+        }
+
+      if ((*flagptr & (rewrite_all_envelope | rewrite_smtp)) != 0)
+        log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, "rewrite rule specifies a "
+          "non-header rewrite - not allowed at transport time -");
+      }
+    break;
+
+    /* If it was an expanded uid, see if there is any expansion to be
+    done by checking for the presence of a $ character. If there is, save it
+    in the corresponding *expand_user option field. Otherwise, fall through
+    to treat it as a fixed uid. Ensure mutual exclusivity of the two kinds
+    of data. */
+
+    case opt_expand_uid:
+    sprintf(CS name2, "*expand_%.50s", name);
+    ol2 = find_option(name2, oltop, last);
+    if (ol2 != NULL)
+      {
+      uschar *ss = (Ustrchr(sptr, '$') != NULL)? sptr : NULL;
+
+      if (data_block == NULL)
+        *((uschar **)(ol2->value)) = ss;
+      else
+        *((uschar **)((uschar *)data_block + (long int)(ol2->value))) = ss;
+
+      if (ss != NULL)
+        {
+        *(get_set_flag(name, oltop, last, data_block)) = FALSE;
+        freesptr = FALSE;
+        break;
+        }
+      }
+
+    /* Look up a fixed uid, and also make use of the corresponding gid
+    if a passwd entry is returned and the gid has not been set. */
+
+    case opt_uid:
+    if (!route_finduser(sptr, &pw, &uid))
+      log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, "user %s was not found", sptr);
+    if (data_block == NULL)
+      *((uid_t *)(ol->value)) = uid;
+    else
+      *((uid_t *)((uschar *)data_block + (long int)(ol->value))) = uid;
+
+    /* Set the flag indicating a fixed value is set */
+
+    *(get_set_flag(name, oltop, last, data_block)) = TRUE;
+
+    /* Handle matching gid if we have a passwd entry: done by finding the
+    same name with terminating "user" changed to "group"; if not found,
+    ignore. Also ignore if the value is already set. */
+
+    if (pw == NULL) break;
+    Ustrcpy(name+Ustrlen(name)-4, "group");
+    ol2 = find_option(name, oltop, last);
+    if (ol2 != NULL && ((ol2->type & opt_mask) == opt_gid ||
+        (ol2->type & opt_mask) == opt_expand_gid))
+      {
+      BOOL *set_flag = get_set_flag(name, oltop, last, data_block);
+      if (! *set_flag)
+        {
+        if (data_block == NULL)
+          *((gid_t *)(ol2->value)) = pw->pw_gid;
+        else
+          *((gid_t *)((uschar *)data_block + (long int)(ol2->value))) = pw->pw_gid;
+        *set_flag = TRUE;
+        }
+      }
+    break;
+
+    /* If it was an expanded gid, see if there is any expansion to be
+    done by checking for the presence of a $ character. If there is, save it
+    in the corresponding *expand_user option field. Otherwise, fall through
+    to treat it as a fixed gid. Ensure mutual exclusivity of the two kinds
+    of data. */
+
+    case opt_expand_gid:
+    sprintf(CS name2, "*expand_%.50s", name);
+    ol2 = find_option(name2, oltop, last);
+    if (ol2 != NULL)
+      {
+      uschar *ss = (Ustrchr(sptr, '$') != NULL)? sptr : NULL;
+
+      if (data_block == NULL)
+        *((uschar **)(ol2->value)) = ss;
+      else
+        *((uschar **)((uschar *)data_block + (long int)(ol2->value))) = ss;
+
+      if (ss != NULL)
+        {
+        *(get_set_flag(name, oltop, last, data_block)) = FALSE;
+        freesptr = FALSE;
+        break;
+        }
+      }
+
+    /* Handle freestanding gid */
+
+    case opt_gid:
+    if (!route_findgroup(sptr, &gid))
+      log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, "group %s was not found", sptr);
+    if (data_block == NULL)
+      *((gid_t *)(ol->value)) = gid;
+    else
+      *((gid_t *)((uschar *)data_block + (long int)(ol->value))) = gid;
+    *(get_set_flag(name, oltop, last, data_block)) = TRUE;
+    break;
+
+    /* If it was a uid list, look up each individual entry, and build
+    a vector of uids, with a count in the first element. Put the vector
+    in malloc store so we can free the string. (We are reading into
+    permanent store already.) */
+
+    case opt_uidlist:
+      {
+      int count = 1;
+      uid_t *list;
+      int ptr = 0;
+      uschar *p;
+      uschar *op = expand_string (sptr);
+
+      if (op == NULL)
+        log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, "failed to expand %s: %s",
+          name, expand_string_message);
+
+      p = op;
+      if (*p != 0) count++;
+      while (*p != 0) if (*p++ == ':' && *p != 0) count++;
+      list = store_malloc(count*sizeof(uid_t));
+      list[ptr++] = (uid_t)(count - 1);
+
+      if (data_block == NULL)
+        *((uid_t **)(ol->value)) = list;
+      else
+        *((uid_t **)((uschar *)data_block + (long int)(ol->value))) = list;
+
+      p = op;
+      while (count-- > 1)
+        {
+        int sep = 0;
+        (void)string_nextinlist(&p, &sep, big_buffer, BIG_BUFFER_SIZE);
+        if (!route_finduser(big_buffer, NULL, &uid))
+          log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, "user %s was not found",
+            big_buffer);
+        list[ptr++] = uid;
+        }
+      }
+    break;
+
+    /* If it was a gid list, look up each individual entry, and build
+    a vector of gids, with a count in the first element. Put the vector
+    in malloc store so we can free the string. (We are reading into permanent
+    store already.) */
+
+    case opt_gidlist:
+      {
+      int count = 1;
+      gid_t *list;
+      int ptr = 0;
+      uschar *p;
+      uschar *op = expand_string (sptr);
+
+      if (op == NULL)
+        log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, "failed to expand %s: %s",
+          name, expand_string_message);
+
+      p = op;
+      if (*p != 0) count++;
+      while (*p != 0) if (*p++ == ':' && *p != 0) count++;
+      list = store_malloc(count*sizeof(gid_t));
+      list[ptr++] = (gid_t)(count - 1);
+
+      if (data_block == NULL)
+        *((gid_t **)(ol->value)) = list;
+      else
+        *((gid_t **)((uschar *)data_block + (long int)(ol->value))) = list;
+
+      p = op;
+      while (count-- > 1)
+        {
+        int sep = 0;
+        (void)string_nextinlist(&p, &sep, big_buffer, BIG_BUFFER_SIZE);
+        if (!route_findgroup(big_buffer, &gid))
+          log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, "group %s was not found",
+            big_buffer);
+        list[ptr++] = gid;
+        }
+      }
+    break;
+    }
+
+  /* Release store if the value of the string doesn't need to be kept. */
+
+  if (freesptr) store_reset(reset_point);
+  break;
+
+  /* Expanded boolean: if no characters follow, or if there are no dollar
+  characters, this is a fixed-valued boolean, and we fall through. Otherwise,
+  save the string for later expansion in the alternate place. */
+
+  case opt_expand_bool:
+  if (*s != 0 && Ustrchr(s, '$') != 0)
+    {
+    sprintf(CS name2, "*expand_%.50s", name);
+    ol2 = find_option(name2, oltop, last);
+    if (ol2 != NULL)
+      {
+      reset_point = sptr = read_string(s, name);
+      if (data_block == NULL)
+        *((uschar **)(ol2->value)) = sptr;
+      else
+        *((uschar **)((uschar *)data_block + (long int)(ol2->value))) = sptr;
+      freesptr = FALSE;
+      break;
+      }
+    }
+  /* Fall through */
+
+  /* Boolean: if no characters follow, the value is boolvalue. Otherwise
+  look for yes/not/true/false. Some booleans are stored in a single bit in
+  a single int. There's a special fudge for verify settings; without a suffix
+  they set both xx_sender and xx_recipient. The table points to the sender
+  value; search subsequently for the recipient. There's another special case:
+  opt_bool_set also notes when a boolean has been set. */
+
+  case opt_bool:
+  case opt_bit:
+  case opt_bool_verify:
+  case opt_bool_set:
+  if (*s != 0)
+    {
+    s = readconf_readname(name2, 64, s);
+    if (strcmpic(name2, US"true") == 0 || strcmpic(name2, US"yes") == 0)
+      boolvalue = TRUE;
+    else if (strcmpic(name2, US"false") == 0 || strcmpic(name2, US"no") == 0)
+      boolvalue = FALSE;
+    else log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN,
+      "\"%s\" is not a valid value for the \"%s\" option", name2, name);
+    if (*s != 0) extra_chars_error(s, string_sprintf("\"%s\" ", name2),
+      US"for boolean option ", name);
+    }
+
+  /* Handle single-bit type. */
+
+  if (type == opt_bit)
+    {
+    int bit = 1 << ((ol->type >> 16) & 31);
+    int *ptr = (data_block == NULL)?
+      (int *)(ol->value) :
+      (int *)((uschar *)data_block + (long int)ol->value);
+    if (boolvalue) *ptr |= bit; else *ptr &= ~bit;
+    break;
+    }
+
+  /* Handle full BOOL types */
+
+  if (data_block == NULL)
+    *((BOOL *)(ol->value)) = boolvalue;
+  else
+    *((BOOL *)((uschar *)data_block + (long int)(ol->value))) = boolvalue;
+
+  /* Verify fudge */
+
+  if (type == opt_bool_verify)
+    {
+    sprintf(CS name2, "%.50s_recipient", name + offset);
+    ol2 = find_option(name2, oltop, last);
+    if (ol2 != NULL)
+      {
+      if (data_block == NULL)
+        *((BOOL *)(ol2->value)) = boolvalue;
+      else
+        *((BOOL *)((uschar *)data_block + (long int)(ol2->value))) = boolvalue;
+      }
+    }
+
+  /* Note that opt_bool_set type is set, if there is somewhere to do so */
+
+  else if (type == opt_bool_set)
+    {
+    sprintf(CS name2, "*set_%.50s", name + offset);
+    ol2 = find_option(name2, oltop, last);
+    if (ol2 != NULL)
+      {
+      if (data_block == NULL)
+        *((BOOL *)(ol2->value)) = TRUE;
+      else
+        *((BOOL *)((uschar *)data_block + (long int)(ol2->value))) = TRUE;
+      }
+    }
+  break;
+
+  /* Octal integer */
+
+  case opt_octint:
+  intbase = 8;
+  inttype = US"octal ";
+
+  /*  Integer: a simple(ish) case; allow octal and hex formats, and
+  suffixes K and M. The different types affect output, not input. */
+
+  case opt_mkint:
+  case opt_int:
+    {
+    uschar *endptr;
+    long int lvalue;
+
+    errno = 0;
+    lvalue = strtol(CS s, CSS &endptr, intbase);
+
+    if (endptr == s)
+      log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, "%sinteger expected for %s",
+        inttype, name);
+
+    if (errno != ERANGE)
+      {
+      if (tolower(*endptr) == 'k')
+        {
+        if (lvalue > INT_MAX/1024 || lvalue < INT_MIN/1024) errno = ERANGE;
+          else lvalue *= 1024;
+        endptr++;
+        }
+      else if (tolower(*endptr) == 'm')
+        {
+        if (lvalue > INT_MAX/(1024*1024) || lvalue < INT_MIN/(1024*1024))
+          errno = ERANGE;
+        else lvalue *= 1024*1024;
+        endptr++;
+        }
+      }
+
+    if (errno == ERANGE || lvalue > INT_MAX || lvalue < INT_MIN)
+      log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN,
+        "absolute value of integer \"%s\" is too large (overflow)", s);
+
+    while (isspace(*endptr)) endptr++;
+    if (*endptr != 0)
+      extra_chars_error(endptr, inttype, US"integer value for ", name);
+
+    value = (int)lvalue;
+    }
+
+  if (data_block == NULL)
+    *((int *)(ol->value)) = value;
+  else
+    *((int *)((uschar *)data_block + (long int)(ol->value))) = value;
+  break;
+
+  /*  Integer held in K: again, allow octal and hex formats, and suffixes K and
+  M. */
+
+  case opt_Kint:
+    {
+    uschar *endptr;
+    errno = 0;
+    value = strtol(CS s, CSS &endptr, intbase);
+
+    if (endptr == s)
+      log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, "%sinteger expected for %s",
+        inttype, name);
+
+    if (errno != ERANGE)
+      {
+      if (tolower(*endptr) == 'm')
+        {
+        if (value > INT_MAX/1024 || value < INT_MIN/1024) errno = ERANGE;
+          else value *= 1024;
+        endptr++;
+        }
+      else if (tolower(*endptr) == 'k')
+        {
+        endptr++;
+        }
+      else
+        {
+        value = (value + 512)/1024;
+        }
+      }
+
+    if (errno == ERANGE) log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN,
+      "absolute value of integer \"%s\" is too large (overflow)", s);
+
+    while (isspace(*endptr)) endptr++;
+    if (*endptr != 0)
+      extra_chars_error(endptr, inttype, US"integer value for ", name);
+    }
+
+  if (data_block == NULL)
+    *((int *)(ol->value)) = value;
+  else
+    *((int *)((uschar *)data_block + (long int)(ol->value))) = value;
+  break;
+
+  /*  Fixed-point number: held to 3 decimal places. */
+
+  case opt_fixed:
+  if (sscanf(CS s, "%d%n", &value, &count) != 1)
+    log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN,
+      "fixed-point number expected for %s", name);
+
+  if (value < 0) log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN,
+    "integer \"%s\" is too large (overflow)", s);
+
+  value *= 1000;
+
+  if (value < 0) log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN,
+    "integer \"%s\" is too large (overflow)", s);
+
+  if (s[count] == '.')
+    {
+    int d = 100;
+    while (isdigit(s[++count]))
+      {
+      value += (s[count] - '0') * d;
+      d /= 10;
+      }
+    }
+
+  while (isspace(s[count])) count++;
+
+  if (s[count] != 0)
+    extra_chars_error(s+count, US"fixed-point value for ", name, US"");
+
+  if (data_block == NULL)
+    *((int *)(ol->value)) = value;
+  else
+    *((int *)((uschar *)data_block + (long int)(ol->value))) = value;
+  break;
+
+  /* There's a special routine to read time values. */
+
+  case opt_time:
+  value = readconf_readtime(s, 0, FALSE);
+  if (value < 0)
+    log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, "invalid time value for %s",
+      name);
+  if (data_block == NULL)
+    *((int *)(ol->value)) = value;
+  else
+    *((int *)((uschar *)data_block + (long int)(ol->value))) = value;
+  break;
+
+  /* A time list is a list of colon-separated times, with the first
+  element holding the size of the list and the second the number of
+  entries used. */
+
+  case opt_timelist:
+    {
+    int count = 0;
+    int *list = (data_block == NULL)?
+      (int *)(ol->value) :
+      (int *)((uschar *)data_block + (long int)(ol->value));
+
+    if (*s != 0) for (count = 1; count <= list[0] - 2; count++)
+      {
+      int terminator = 0;
+      uschar *snext = Ustrchr(s, ':');
+      if (snext != NULL)
+        {
+        uschar *ss = snext;
+        while (ss > s && isspace(ss[-1])) ss--;
+        terminator = *ss;
+        }
+      value = readconf_readtime(s, terminator, FALSE);
+      if (value < 0)
+        log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, "invalid time value for %s",
+          name);
+      if (count > 1 && value <= list[count])
+        log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN,
+          "time value out of order for %s", name);
+      list[count+1] = value;
+      if (snext == NULL) break;
+      s = snext + 1;
+      while (isspace(*s)) s++;
+      }
+
+    if (count > list[0] - 2)
+      log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, "too many time values for %s",
+        name);
+    if (count > 0 && list[2] == 0) count = 0;
+    list[1] = count;
+    }
+
+  break;
+  }
+
+return TRUE;
+}
+
+
+
+/*************************************************
+*               Print a time value               *
+*************************************************/
+
+/*
+Argument:  a time value in seconds
+Returns:   pointer to a fixed buffer containing the time as a string,
+           in readconf_readtime() format
+*/
+
+uschar *
+readconf_printtime(int t)
+{
+int s, m, h, d, w;
+uschar *p = time_buffer;
+
+if (t < 0)
+  {
+  *p++ = '-';
+  t = -t;
+  }
+
+s = t % 60;
+t /= 60;
+m = t % 60;
+t /= 60;
+h = t % 24;
+t /= 24;
+d = t % 7;
+w = t/7;
+
+if (w > 0) { sprintf(CS p, "%dw", w); while (*p) p++; }
+if (d > 0) { sprintf(CS p, "%dd", d); while (*p) p++; }
+if (h > 0) { sprintf(CS p, "%dh", h); while (*p) p++; }
+if (m > 0) { sprintf(CS p, "%dm", m); while (*p) p++; }
+if (s > 0 || p == time_buffer) sprintf(CS p, "%ds", s);
+
+return time_buffer;
+}
+
+
+
+/*************************************************
+*      Print an individual option value          *
+*************************************************/
+
+/* This is used by the -bP option, so prints to the standard output.
+The entire options list is passed in as an argument, because some options come
+in pairs - typically uid/gid settings, which can either be explicit numerical
+values, or strings to be expanded later. If the numerical value is unset,
+search for "*expand_<name>" to see if there is a string equivalent.
+
+Arguments:
+  ol             option entry, or NULL for an unknown option
+  name           option name
+  options_block  NULL for main configuration options; otherwise points to
+                   a driver block; if the option doesn't have opt_public
+                   set, then options_block->options_block is where the item
+                   resides.
+  oltop          points to the option list in which ol exists
+  last           one more than the offset of the last entry in optop
+  no_labels      do not show "foo = " at the start.
+
+Returns:         nothing
+*/
+
+static void
+print_ol(optionlist *ol, uschar *name, void *options_block,
+  optionlist *oltop, int last, BOOL no_labels)
+{
+struct passwd *pw;
+struct group *gr;
+optionlist *ol2;
+void *value;
+uid_t *uidlist;
+gid_t *gidlist;
+uschar *s;
+uschar name2[64];
+
+if (ol == NULL)
+  {
+  printf("%s is not a known option\n", name);
+  return;
+  }
+
+/* Non-admin callers cannot see options that have been flagged secure by the
+"hide" prefix. */
+
+if (!admin_user && (ol->type & opt_secure) != 0)
+  {
+  const char * const hidden = "<value not displayable>";
+  if (no_labels)
+    printf("%s\n", hidden);
+  else
+    printf("%s = %s\n", name, hidden);
+  return;
+  }
+
+/* Else show the value of the option */
+
+value = ol->value;
+if (options_block != NULL)
+  {
+  if ((ol->type & opt_public) == 0)
+    options_block = (void *)(((driver_instance *)options_block)->options_block);
+  value = (void *)((uschar *)options_block + (long int)value);
+  }
+
+switch(ol->type & opt_mask)
+  {
+  case opt_stringptr:
+  case opt_rewrite:        /* Show the text value */
+  s = *((uschar **)value);
+  if (!no_labels) printf("%s = ", name);
+  printf("%s\n", (s == NULL)? US"" : string_printing2(s, FALSE));
+  break;
+
+  case opt_int:
+  if (!no_labels) printf("%s = ", name);
+  printf("%d\n", *((int *)value));
+  break;
+
+  case opt_mkint:
+    {
+    int x = *((int *)value);
+    if (x != 0 && (x & 1023) == 0)
+      {
+      int c = 'K';
+      x >>= 10;
+      if ((x & 1023) == 0)
+        {
+        c = 'M';
+        x >>= 10;
+        }
+      if (!no_labels) printf("%s = ", name);
+      printf("%d%c\n", x, c);
+      }
+    else
+      {
+      if (!no_labels) printf("%s = ", name);
+      printf("%d\n", x);
+      }
+    }
+  break;
+
+  case opt_Kint:
+    {
+    int x = *((int *)value);
+    if (!no_labels) printf("%s = ", name);
+    if (x == 0) printf("0\n");
+      else if ((x & 1023) == 0) printf("%dM\n", x >> 10);
+        else printf("%dK\n", x);
+    }
+  break;
+
+  case opt_octint:
+  if (!no_labels) printf("%s = ", name);
+  printf("%#o\n", *((int *)value));
+  break;
+
+  /* Can be negative only when "unset", in which case integer */
+
+  case opt_fixed:
+    {
+    int x = *((int *)value);
+    int f = x % 1000;
+    int d = 100;
+    if (x < 0) printf("%s =\n", name); else
+      {
+      if (!no_labels) printf("%s = ", name);
+      printf("%d.", x/1000);
+      do
+        {
+        printf("%d", f/d);
+        f %= d;
+        d /= 10;
+        }
+      while (f != 0);
+      printf("\n");
+      }
+    }
+  break;
+
+  /* If the numerical value is unset, try for the string value */
+
+  case opt_expand_uid:
+  if (! *get_set_flag(name, oltop, last, options_block))
+    {
+    sprintf(CS name2, "*expand_%.50s", name);
+    ol2 = find_option(name2, oltop, last);
+    if (ol2 != NULL)
+      {
+      void *value2 = ol2->value;
+      if (options_block != NULL)
+        value2 = (void *)((uschar *)options_block + (long int)value2);
+      s = *((uschar **)value2);
+      if (!no_labels) printf("%s = ", name);
+      printf("%s\n", (s == NULL)? US"" : string_printing(s));
+      break;
+      }
+    }
+
+  /* Else fall through */
+
+  case opt_uid:
+  if (!no_labels) printf("%s = ", name);
+  if (! *get_set_flag(name, oltop, last, options_block))
+    printf("\n");
+  else
+    {
+    pw = getpwuid(*((uid_t *)value));
+    if (pw == NULL)
+      printf("%ld\n", (long int)(*((uid_t *)value)));
+    else printf("%s\n", pw->pw_name);
+    }
+  break;
+
+  /* If the numerical value is unset, try for the string value */
+
+  case opt_expand_gid:
+  if (! *get_set_flag(name, oltop, last, options_block))
+    {
+    sprintf(CS name2, "*expand_%.50s", name);
+    ol2 = find_option(name2, oltop, last);
+    if (ol2 != NULL && (ol2->type & opt_mask) == opt_stringptr)
+      {
+      void *value2 = ol2->value;
+      if (options_block != NULL)
+        value2 = (void *)((uschar *)options_block + (long int)value2);
+      s = *((uschar **)value2);
+      if (!no_labels) printf("%s = ", name);
+      printf("%s\n", (s == NULL)? US"" : string_printing(s));
+      break;
+      }
+    }
+
+  /* Else fall through */
+
+  case opt_gid:
+  if (!no_labels) printf("%s = ", name);
+  if (! *get_set_flag(name, oltop, last, options_block))
+    printf("\n");
+  else
+    {
+    gr = getgrgid(*((int *)value));
+    if (gr == NULL)
+       printf("%ld\n", (long int)(*((int *)value)));
+    else printf("%s\n", gr->gr_name);
+    }
+  break;
+
+  case opt_uidlist:
+  uidlist = *((uid_t **)value);
+  if (!no_labels) printf("%s =", name);
+  if (uidlist != NULL)
+    {
+    int i;
+    uschar sep = ' ';
+    if (no_labels) sep = '\0';
+    for (i = 1; i <= (int)(uidlist[0]); i++)
+      {
+      uschar *name = NULL;
+      pw = getpwuid(uidlist[i]);
+      if (pw != NULL) name = US pw->pw_name;
+      if (sep != '\0') printf("%c", sep);
+      if (name != NULL) printf("%s", name);
+        else printf("%ld", (long int)(uidlist[i]));
+      sep = ':';
+      }
+    }
+  printf("\n");
+  break;
+
+  case opt_gidlist:
+  gidlist = *((gid_t **)value);
+  if (!no_labels) printf("%s =", name);
+  if (gidlist != NULL)
+    {
+    int i;
+    uschar sep = ' ';
+    if (no_labels) sep = '\0';
+    for (i = 1; i <= (int)(gidlist[0]); i++)
+      {
+      uschar *name = NULL;
+      gr = getgrgid(gidlist[i]);
+      if (gr != NULL) name = US gr->gr_name;
+      if (sep != '\0') printf("%c", sep);
+      if (name != NULL) printf("%s", name);
+        else printf("%ld", (long int)(gidlist[i]));
+      sep = ':';
+      }
+    }
+  printf("\n");
+  break;
+
+  case opt_time:
+  if (!no_labels) printf("%s = ", name);
+  printf("%s\n", readconf_printtime(*((int *)value)));
+  break;
+
+  case opt_timelist:
+    {
+    int i;
+    int *list = (int *)value;
+    if (!no_labels) printf("%s = ", name);
+    for (i = 0; i < list[1]; i++)
+      printf("%s%s", (i == 0)? "" : ":", readconf_printtime(list[i+2]));
+    printf("\n");
+    }
+  break;
+
+  case opt_bit:
+  printf("%s%s\n", ((*((int *)value)) & (1 << ((ol->type >> 16) & 31)))?
+    "" : "no_", name);
+  break;
+
+  case opt_expand_bool:
+  sprintf(CS name2, "*expand_%.50s", name);
+  ol2 = find_option(name2, oltop, last);
+  if (ol2 != NULL && ol2->value != NULL)
+    {
+    void *value2 = ol2->value;
+    if (options_block != NULL)
+      value2 = (void *)((uschar *)options_block + (long int)value2);
+    s = *((uschar **)value2);
+    if (s != NULL)
+      {
+      if (!no_labels) printf("%s = ", name);
+      printf("%s\n", string_printing(s));
+      break;
+      }
+    /* s == NULL => string not set; fall through */
+    }
+
+  /* Fall through */
+
+  case opt_bool:
+  case opt_bool_verify:
+  case opt_bool_set:
+  printf("%s%s\n", (*((BOOL *)value))? "" : "no_", name);
+  break;
+  }
+}
+
+
+
+/*************************************************
+*        Print value from main configuration     *
+*************************************************/
+
+/* This function, called as a result of encountering the -bP option,
+causes the value of any main configuration variable to be output if the
+second argument is NULL. There are some special values:
+
+  all                print all main configuration options
+  configure_file     print the name of the configuration file
+  routers            print the routers' configurations
+  transports         print the transports' configuration
+  authenticators     print the authenticators' configuration
+  macros             print the macros' configuration
+  router_list        print a list of router names
+  transport_list     print a list of transport names
+  authenticator_list print a list of authentication mechanism names
+  macro_list         print a list of macro names
+  +name              print a named list item
+  local_scan         print the local_scan options
+
+If the second argument is not NULL, it must be one of "router", "transport",
+"authenticator" or "macro" in which case the first argument identifies the
+driver whose options are to be printed.
+
+Arguments:
+  name        option name if type == NULL; else driver name
+  type        NULL or driver type name, as described above
+  no_labels   avoid the "foo = " at the start of an item
+
+Returns:      nothing
+*/
+
+void
+readconf_print(uschar *name, uschar *type, BOOL no_labels)
+{
+BOOL names_only = FALSE;
+optionlist *ol;
+optionlist *ol2 = NULL;
+driver_instance *d = NULL;
+macro_item *m;
+int size = 0;
+
+if (type == NULL)
+  {
+  if (*name == '+')
+    {
+    int i;
+    tree_node *t;
+    BOOL found = FALSE;
+    static uschar *types[] = { US"address", US"domain", US"host",
+      US"localpart" };
+    static tree_node **anchors[] = { &addresslist_anchor, &domainlist_anchor,
+      &hostlist_anchor, &localpartlist_anchor };
+
+    for (i = 0; i < 4; i++)
+      {
+      t = tree_search(*(anchors[i]), name+1);
+      if (t != NULL)
+        {
+        found = TRUE;
+        if (no_labels)
+          printf("%s\n", ((namedlist_block *)(t->data.ptr))->string);
+        else
+          printf("%slist %s = %s\n", types[i], name+1,
+            ((namedlist_block *)(t->data.ptr))->string);
+        }
+      }
+
+    if (!found)
+      printf("no address, domain, host, or local part list called \"%s\" "
+        "exists\n", name+1);
+
+    return;
+    }
+
+  if (Ustrcmp(name, "configure_file") == 0)
+    {
+    printf("%s\n", CS config_main_filename);
+    return;
+    }
+
+  if (Ustrcmp(name, "all") == 0)
+    {
+    for (ol = optionlist_config;
+         ol < optionlist_config + optionlist_config_size; ol++)
+      {
+      if ((ol->type & opt_hidden) == 0)
+        print_ol(ol, US ol->name, NULL,
+            optionlist_config, optionlist_config_size,
+            no_labels);
+      }
+    return;
+    }
+
+  if (Ustrcmp(name, "local_scan") == 0)
+    {
+    #ifndef LOCAL_SCAN_HAS_OPTIONS
+    printf("local_scan() options are not supported\n");
+    #else
+    for (ol = local_scan_options;
+         ol < local_scan_options + local_scan_options_count; ol++)
+      {
+      print_ol(ol, US ol->name, NULL, local_scan_options,
+        local_scan_options_count, no_labels);
+      }
+    #endif
+    return;
+    }
+
+  if (Ustrcmp(name, "routers") == 0)
+    {
+    type = US"router";
+    name = NULL;
+    }
+  else if (Ustrcmp(name, "transports") == 0)
+    {
+    type = US"transport";
+    name = NULL;
+    }
+
+  else if (Ustrcmp(name, "authenticators") == 0)
+    {
+    type = US"authenticator";
+    name = NULL;
+    }
+
+  else if (Ustrcmp(name, "macros") == 0)
+    {
+    type = US"macro";
+    name = NULL;
+    }
+
+  else if (Ustrcmp(name, "router_list") == 0)
+    {
+    type = US"router";
+    name = NULL;
+    names_only = TRUE;
+    }
+
+  else if (Ustrcmp(name, "transport_list") == 0)
+    {
+    type = US"transport";
+    name = NULL;
+    names_only = TRUE;
+    }
+
+  else if (Ustrcmp(name, "authenticator_list") == 0)
+    {
+    type = US"authenticator";
+    name = NULL;
+    names_only = TRUE;
+    }
+
+  else if (Ustrcmp(name, "macro_list") == 0)
+    {
+    type = US"macro";
+    name = NULL;
+    names_only = TRUE;
+    }
+
+  else
+    {
+    print_ol(find_option(name, optionlist_config, optionlist_config_size),
+      name, NULL, optionlist_config, optionlist_config_size, no_labels);
+    return;
+    }
+  }
+
+/* Handle the options for a router or transport. Skip options that are flagged
+as hidden. Some of these are options with names starting with '*', used for
+internal alternative representations of other options (which the printing
+function will sort out). Others are synonyms kept for backward compatibility.
+*/
+
+if (Ustrcmp(type, "router") == 0)
+  {
+  d = (driver_instance *)routers;
+  ol2 = optionlist_routers;
+  size = optionlist_routers_size;
+  }
+else if (Ustrcmp(type, "transport") == 0)
+  {
+  d = (driver_instance *)transports;
+  ol2 = optionlist_transports;
+  size = optionlist_transports_size;
+  }
+else if (Ustrcmp(type, "authenticator") == 0)
+  {
+  d = (driver_instance *)auths;
+  ol2 = optionlist_auths;
+  size = optionlist_auths_size;
+  }
+
+else if (Ustrcmp(type, "macro") == 0)
+  {
+  /* People store passwords in macros and they were previously not available
+  for printing.  So we have an admin_users restriction. */
+  if (!admin_user)
+    {
+    fprintf(stderr, "exim: permission denied\n");
+    exit(EXIT_FAILURE);
+    }
+  for (m = macros; m != NULL; m = m->next)
+    {
+    if (name == NULL || Ustrcmp(name, m->name) == 0)
+      {
+      if (names_only)
+        printf("%s\n", CS m->name);
+      else
+        printf("%s=%s\n", CS m->name, CS m->replacement);
+      if (name != NULL)
+        return;
+      }
+    }
+  if (name != NULL)
+    printf("%s %s not found\n", type, name);
+  return;
+  }
+
+if (names_only)
+  {
+  for (; d != NULL; d = d->next) printf("%s\n", CS d->name);
+  return;
+  }
+
+/* Either search for a given driver, or print all of them */
+
+for (; d != NULL; d = d->next)
+  {
+  if (name == NULL)
+    printf("\n%s %s:\n", d->name, type);
+  else if (Ustrcmp(d->name, name) != 0) continue;
+
+  for (ol = ol2; ol < ol2 + size; ol++)
+    {
+    if ((ol->type & opt_hidden) == 0)
+      print_ol(ol, US ol->name, d, ol2, size, no_labels);
+    }
+
+  for (ol = d->info->options;
+       ol < d->info->options + *(d->info->options_count); ol++)
+    {
+    if ((ol->type & opt_hidden) == 0)
+      print_ol(ol, US ol->name, d, d->info->options, *(d->info->options_count), no_labels);
+    }
+  if (name != NULL) return;
+  }
+if (name != NULL) printf("%s %s not found\n", type, name);
+}
+
+
+
+/*************************************************
+*          Read a named list item                *
+*************************************************/
+
+/* This function reads a name and a list (i.e. string). The name is used to
+save the list in a tree, sorted by its name. Each entry also has a number,
+which can be used for caching tests, but if the string contains any expansion
+items other than $key, the number is set negative to inhibit caching. This
+mechanism is used for domain, host, and address lists that are referenced by
+the "+name" syntax.
+
+Arguments:
+  anchorp     points to the tree anchor
+  numberp     points to the current number for this tree
+  max         the maximum number permitted
+  s           the text of the option line, starting immediately after the name
+                of the list type
+  tname       the name of the list type, for messages
+
+Returns:      nothing
+*/
+
+static void
+read_named_list(tree_node **anchorp, int *numberp, int max, uschar *s,
+  uschar *tname)
+{
+BOOL forcecache = FALSE;
+uschar *ss;
+tree_node *t;
+namedlist_block *nb = store_get(sizeof(namedlist_block));
+
+if (Ustrncmp(s, "_cache", 6) == 0)
+  {
+  forcecache = TRUE;
+  s += 6;
+  }
+
+if (!isspace(*s))
+  log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, "unrecognized configuration line");
+
+if (*numberp >= max)
+ log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, "too many named %ss (max is %d)\n",
+   tname, max);
+
+while (isspace(*s)) s++;
+ss = s;
+while (isalnum(*s) || *s == '_') s++;
+t = store_get(sizeof(tree_node) + s-ss);
+Ustrncpy(t->name, ss, s-ss);
+t->name[s-ss] = 0;
+while (isspace(*s)) s++;
+
+if (!tree_insertnode(anchorp, t))
+  log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN,
+    "duplicate name \"%s\" for a named %s", t->name, tname);
+
+t->data.ptr = nb;
+nb->number = *numberp;
+*numberp += 1;
+
+if (*s++ != '=') log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN,
+  "missing '=' after \"%s\"", t->name);
+while (isspace(*s)) s++;
+nb->string = read_string(s, t->name);
+nb->cache_data = NULL;
+
+/* Check the string for any expansions; if any are found, mark this list
+uncacheable unless the user has explicited forced caching. */
+
+if (!forcecache && Ustrchr(nb->string, '$') != NULL) nb->number = -1;
+}
+
+
+
+
+/*************************************************
+*        Unpick data for a rate limit            *
+*************************************************/
+
+/* This function is called to unpick smtp_ratelimit_{mail,rcpt} into four
+separate values.
+
+Arguments:
+  s            string, in the form t,b,f,l
+               where t is the threshold (integer)
+               b is the initial delay (time)
+               f is the multiplicative factor (fixed point)
+               k is the maximum time (time)
+  threshold    where to store threshold
+  base         where to store base in milliseconds
+  factor       where to store factor in milliseconds
+  limit        where to store limit
+
+Returns:       nothing (panics on error)
+*/
+
+static void
+unpick_ratelimit(uschar *s, int *threshold, int *base, double *factor,
+  int *limit)
+{
+uschar bstring[16], lstring[16];
+
+if (sscanf(CS s, "%d, %15[0123456789smhdw.], %lf, %15s", threshold, bstring,
+    factor, lstring) == 4)
+  {
+  *base = readconf_readtime(bstring, 0, TRUE);
+  *limit = readconf_readtime(lstring, 0, TRUE);
+  if (*base >= 0 && *limit >= 0) return;
+  }
+log_write(0, LOG_MAIN|LOG_PANIC_DIE, "malformed ratelimit data: %s", s);
+}
+
+
+
+
+/*************************************************
+*       Drop privs for checking TLS config      *
+*************************************************/
+
+/* We want to validate TLS options during readconf, but do not want to be
+root when we call into the TLS library, in case of library linkage errors
+which cause segfaults; before this check, those were always done as the Exim
+runtime user and it makes sense to continue with that.
+
+Assumes:  tls_require_ciphers has been set, if it will be
+          exim_user has been set, if it will be
+          exim_group has been set, if it will be
+
+Returns:  bool for "okay"; false will cause caller to immediately exit.
+*/
+
+#ifdef SUPPORT_TLS
+static BOOL
+tls_dropprivs_validate_require_cipher(void)
+{
+const uschar *errmsg;
+pid_t pid;
+int rc, status;
+void (*oldsignal)(int);
+
+oldsignal = signal(SIGCHLD, SIG_DFL);
+
+fflush(NULL);
+if ((pid = fork()) < 0)
+  log_write(0, LOG_MAIN|LOG_PANIC_DIE, "fork failed for TLS check");
+
+if (pid == 0)
+  {
+  /* in some modes, will have dropped privilege already */
+  if (!geteuid())
+    exim_setugid(exim_uid, exim_gid, FALSE,
+        US"calling tls_validate_require_cipher");
+
+  errmsg = tls_validate_require_cipher();
+  if (errmsg)
+    {
+    log_write(0, LOG_PANIC_DIE|LOG_CONFIG,
+        "tls_require_ciphers invalid: %s", errmsg);
+    }
+  fflush(NULL);
+  _exit(0);
+  }
+
+do {
+  rc = waitpid(pid, &status, 0);
+} while (rc < 0 && errno == EINTR);
+
+DEBUG(D_tls)
+  debug_printf("tls_validate_require_cipher child %d ended: status=0x%x\n",
+      (int)pid, status);
+
+signal(SIGCHLD, oldsignal);
+
+return status == 0;
+}
+#endif /* SUPPORT_TLS */
+
+
+
+
+/*************************************************
+*         Read main configuration options        *
+*************************************************/
+
+/* This function is the first to be called for configuration reading. It
+opens the configuration file and reads general configuration settings until
+it reaches the end of the configuration section. The file is then left open so
+that the remaining configuration data can subsequently be read if needed for
+this run of Exim.
+
+The configuration file must be owned either by root or exim, and be writeable
+only by root or uid/gid exim. The values for Exim's uid and gid can be changed
+in the config file, so the test is done on the compiled in values. A slight
+anomaly, to be carefully documented.
+
+The name of the configuration file is taken from a list that is included in the
+binary of Exim. It can be altered from the command line, but if that is done,
+root privilege is immediately withdrawn unless the caller is root or exim.
+The first file on the list that exists is used.
+
+For use on multiple systems that share file systems, first look for a
+configuration file whose name has the current node name on the end. If that is
+not found, try the generic name. For really contorted configurations, that run
+multiple Exims with different uid settings, first try adding the effective uid
+before the node name. These complications are going to waste resources on most
+systems. Therefore they are available only when requested by compile-time
+options. */
+
+void
+readconf_main(void)
+{
+int sep = 0;
+struct stat statbuf;
+uschar *s, *filename;
+uschar *list = config_main_filelist;
+
+/* Loop through the possible file names */
+
+while((filename = string_nextinlist(&list, &sep, big_buffer, big_buffer_size))
+       != NULL)
+  {
+  /* Cut out all the fancy processing unless specifically wanted */
+
+  #if defined(CONFIGURE_FILE_USE_NODE) || defined(CONFIGURE_FILE_USE_EUID)
+  uschar *suffix = filename + Ustrlen(filename);
+
+  /* Try for the node-specific file if a node name exists */
+
+  #ifdef CONFIGURE_FILE_USE_NODE
+  struct utsname uts;
+  if (uname(&uts) >= 0)
+    {
+    #ifdef CONFIGURE_FILE_USE_EUID
+    sprintf(CS suffix, ".%ld.%.256s", (long int)original_euid, uts.nodename);
+    config_file = Ufopen(filename, "rb");
+    if (config_file == NULL)
+    #endif  /* CONFIGURE_FILE_USE_EUID */
+      {
+      sprintf(CS suffix, ".%.256s", uts.nodename);
+      config_file = Ufopen(filename, "rb");
+      }
+    }
+  #endif  /* CONFIGURE_FILE_USE_NODE */
+
+  /* Otherwise, try the generic name, possibly with the euid added */
+
+  #ifdef CONFIGURE_FILE_USE_EUID
+  if (config_file == NULL)
+    {
+    sprintf(CS suffix, ".%ld", (long int)original_euid);
+    config_file = Ufopen(filename, "rb");
+    }
+  #endif  /* CONFIGURE_FILE_USE_EUID */
+
+  /* Finally, try the unadorned name */
+
+  if (config_file == NULL)
+    {
+    *suffix = 0;
+    config_file = Ufopen(filename, "rb");
+    }
+  #else  /* if neither defined */
+
+  /* This is the common case when the fancy processing is not included. */
+
+  config_file = Ufopen(filename, "rb");
+  #endif
+
+  /* If the file does not exist, continue to try any others. For any other
+  error, break out (and die). */
+
+  if (config_file != NULL || errno != ENOENT) break;
+  }
+
+/* On success, save the name for verification; config_filename is used when
+logging configuration errors (it changes for .included files) whereas
+config_main_filename is the name shown by -bP. Failure to open a configuration
+file is a serious disaster. */
+
+if (config_file != NULL)
+  {
+  config_filename = config_main_filename = string_copy(filename);
+  }
+else
+  {
+  if (filename == NULL)
+    log_write(0, LOG_MAIN|LOG_PANIC_DIE, "non-existent configuration file(s): "
+      "%s", config_main_filelist);
+  else
+    log_write(0, LOG_MAIN|LOG_PANIC_DIE, "%s", string_open_failed(errno,
+      "configuration file %s", filename));
+  }
+
+/* Check the status of the file we have opened, if we have retained root
+privileges and the file isn't /dev/null (which *should* be 0666). */
+
+if (trusted_config && Ustrcmp(filename, US"/dev/null"))
+  {
+  if (fstat(fileno(config_file), &statbuf) != 0)
+    log_write(0, LOG_MAIN|LOG_PANIC_DIE, "failed to stat configuration file %s",
+      big_buffer);
+
+  if ((statbuf.st_uid != root_uid                /* owner not root */
+       #ifdef CONFIGURE_OWNER
+       && statbuf.st_uid != config_uid           /* owner not the special one */
+       #endif
+         ) ||                                    /* or */
+      (statbuf.st_gid != root_gid                /* group not root & */
+       #ifdef CONFIGURE_GROUP
+       && statbuf.st_gid != config_gid           /* group not the special one */
+       #endif
+       && (statbuf.st_mode & 020) != 0) ||       /* group writeable  */
+                                                 /* or */
+      ((statbuf.st_mode & 2) != 0))              /* world writeable  */
+
+    log_write(0, LOG_MAIN|LOG_PANIC_DIE, "Exim configuration file %s has the "
+      "wrong owner, group, or mode", big_buffer);
+  }
+
+/* Process the main configuration settings. They all begin with a lower case
+letter. If we see something starting with an upper case letter, it is taken as
+a macro definition. */
+
+while ((s = get_config_line()) != NULL)
+  {
+  if (isupper(s[0])) read_macro_assignment(s);
+
+  else if (Ustrncmp(s, "domainlist", 10) == 0)
+    read_named_list(&domainlist_anchor, &domainlist_count,
+      MAX_NAMED_LIST, s+10, US"domain list");
+
+  else if (Ustrncmp(s, "hostlist", 8) == 0)
+    read_named_list(&hostlist_anchor, &hostlist_count,
+      MAX_NAMED_LIST, s+8, US"host list");
+
+  else if (Ustrncmp(s, US"addresslist", 11) == 0)
+    read_named_list(&addresslist_anchor, &addresslist_count,
+      MAX_NAMED_LIST, s+11, US"address list");
+
+  else if (Ustrncmp(s, US"localpartlist", 13) == 0)
+    read_named_list(&localpartlist_anchor, &localpartlist_count,
+      MAX_NAMED_LIST, s+13, US"local part list");
+
+  else
+    (void) readconf_handle_option(s, optionlist_config, optionlist_config_size,
+      NULL, US"main option \"%s\" unknown");
+  }
+
+
+/* If local_sender_retain is set, local_from_check must be unset. */
+
+if (local_sender_retain && local_from_check)
+  log_write(0, LOG_MAIN|LOG_PANIC_DIE, "both local_from_check and "
+    "local_sender_retain are set; this combination is not allowed");
+
+/* If the timezone string is empty, set it to NULL, implying no TZ variable
+wanted. */
+
+if (timezone_string != NULL && *timezone_string == 0) timezone_string = NULL;
+
+/* The max retry interval must not be greater than 24 hours. */
+
+if (retry_interval_max > 24*60*60) retry_interval_max = 24*60*60;
+
+/* remote_max_parallel must be > 0 */
+
+if (remote_max_parallel <= 0) remote_max_parallel = 1;
+
+/* Save the configured setting of freeze_tell, so we can re-instate it at the
+start of a new SMTP message. */
+
+freeze_tell_config = freeze_tell;
+
+/* The primary host name may be required for expansion of spool_directory
+and log_file_path, so make sure it is set asap. It is obtained from uname(),
+but if that yields an unqualified value, make a FQDN by using gethostbyname to
+canonize it. Some people like upper case letters in their host names, so we
+don't force the case. */
+
+if (primary_hostname == NULL)
+  {
+  uschar *hostname;
+  struct utsname uts;
+  if (uname(&uts) < 0)
+    log_write(0, LOG_MAIN|LOG_PANIC_DIE, "uname() failed to yield host name");
+  hostname = US uts.nodename;
+
+  if (Ustrchr(hostname, '.') == NULL)
+    {
+    int af = AF_INET;
+    struct hostent *hostdata;
+
+    #if HAVE_IPV6
+    if (!disable_ipv6 && (dns_ipv4_lookup == NULL ||
+         match_isinlist(hostname, &dns_ipv4_lookup, 0, NULL, NULL, MCL_DOMAIN,
+           TRUE, NULL) != OK))
+      af = AF_INET6;
+    #else
+    af = AF_INET;
+    #endif
+
+    for (;;)
+      {
+      #if HAVE_IPV6
+        #if HAVE_GETIPNODEBYNAME
+        int error_num;
+        hostdata = getipnodebyname(CS hostname, af, 0, &error_num);
+        #else
+        hostdata = gethostbyname2(CS hostname, af);
+        #endif
+      #else
+      hostdata = gethostbyname(CS hostname);
+      #endif
+
+      if (hostdata != NULL)
+        {
+        hostname = US hostdata->h_name;
+        break;
+        }
+
+      if (af == AF_INET) break;
+      af = AF_INET;
+      }
+    }
+
+  primary_hostname = string_copy(hostname);
+  }
+
+/* Set up default value for smtp_active_hostname */
+
+smtp_active_hostname = primary_hostname;
+
+/* If spool_directory wasn't set in the build-time configuration, it must have
+got set above. Of course, writing to the log may not work if log_file_path is
+not set, but it will at least get to syslog or somewhere, with any luck. */
+
+if (*spool_directory == 0)
+  log_write(0, LOG_MAIN|LOG_PANIC_DIE, "spool_directory undefined: cannot "
+    "proceed");
+
+/* Expand the spool directory name; it may, for example, contain the primary
+host name. Same comment about failure. */
+
+s = expand_string(spool_directory);
+if (s == NULL)
+  log_write(0, LOG_MAIN|LOG_PANIC_DIE, "failed to expand spool_directory "
+    "\"%s\": %s", spool_directory, expand_string_message);
+spool_directory = s;
+
+/* Expand log_file_path, which must contain "%s" in any component that isn't
+the null string or "syslog". It is also allowed to contain one instance of %D
+or %M. However, it must NOT contain % followed by anything else. */
+
+if (*log_file_path != 0)
+  {
+  uschar *ss, *sss;
+  int sep = ':';                       /* Fixed for log file path */
+  s = expand_string(log_file_path);
+  if (s == NULL)
+    log_write(0, LOG_MAIN|LOG_PANIC_DIE, "failed to expand log_file_path "
+      "\"%s\": %s", log_file_path, expand_string_message);
+
+  ss = s;
+  while ((sss = string_nextinlist(&ss,&sep,big_buffer,big_buffer_size)) != NULL)
+    {
+    uschar *t;
+    if (sss[0] == 0 || Ustrcmp(sss, "syslog") == 0) continue;
+    t = Ustrstr(sss, "%s");
+    if (t == NULL)
+      log_write(0, LOG_MAIN|LOG_PANIC_DIE, "log_file_path \"%s\" does not "
+        "contain \"%%s\"", sss);
+    *t = 'X';
+    t = Ustrchr(sss, '%');
+    if (t != NULL)
+      {
+      if ((t[1] != 'D' && t[1] != 'M') || Ustrchr(t+2, '%') != NULL)
+        log_write(0, LOG_MAIN|LOG_PANIC_DIE, "log_file_path \"%s\" contains "
+          "unexpected \"%%\" character", s);
+      }
+    }
+
+  log_file_path = s;
+  }
+
+/* Interpret syslog_facility into an integer argument for 'ident' param to
+openlog(). Default is LOG_MAIL set in globals.c. Allow the user to omit the
+leading "log_". */
+
+if (syslog_facility_str != NULL)
+  {
+  int i;
+  uschar *s = syslog_facility_str;
+
+  if ((Ustrlen(syslog_facility_str) >= 4) &&
+        (strncmpic(syslog_facility_str, US"log_", 4) == 0))
+    s += 4;
+
+  for (i = 0; i < syslog_list_size; i++)
+    {
+    if (strcmpic(s, syslog_list[i].name) == 0)
+      {
+      syslog_facility = syslog_list[i].value;
+      break;
+      }
+    }
+
+  if (i >= syslog_list_size)
+    {
+    log_write(0, LOG_PANIC_DIE|LOG_CONFIG,
+      "failed to interpret syslog_facility \"%s\"", syslog_facility_str);
+    }
+  }
+
+/* Expand pid_file_path */
+
+if (*pid_file_path != 0)
+  {
+  s = expand_string(pid_file_path);
+  if (s == NULL)
+    log_write(0, LOG_MAIN|LOG_PANIC_DIE, "failed to expand pid_file_path "
+      "\"%s\": %s", pid_file_path, expand_string_message);
+  pid_file_path = s;
+  }
+
+/* Set default value of process_log_path */
+
+if (process_log_path == NULL || *process_log_path =='\0')
+  process_log_path = string_sprintf("%s/exim-process.info", spool_directory);
+
+/* Compile the regex for matching a UUCP-style "From_" line in an incoming
+message. */
+
+regex_From = regex_must_compile(uucp_from_pattern, FALSE, TRUE);
+
+/* Unpick the SMTP rate limiting options, if set */
+
+if (smtp_ratelimit_mail != NULL)
+  {
+  unpick_ratelimit(smtp_ratelimit_mail, &smtp_rlm_threshold,
+    &smtp_rlm_base, &smtp_rlm_factor, &smtp_rlm_limit);
+  }
+
+if (smtp_ratelimit_rcpt != NULL)
+  {
+  unpick_ratelimit(smtp_ratelimit_rcpt, &smtp_rlr_threshold,
+    &smtp_rlr_base, &smtp_rlr_factor, &smtp_rlr_limit);
+  }
+
+/* The qualify domains default to the primary host name */
+
+if (qualify_domain_sender == NULL)
+  qualify_domain_sender = primary_hostname;
+if (qualify_domain_recipient == NULL)
+  qualify_domain_recipient = qualify_domain_sender;
+
+/* Setting system_filter_user in the configuration sets the gid as well if a
+name is given, but a numerical value does not. */
+
+if (system_filter_uid_set && !system_filter_gid_set)
+  {
+  struct passwd *pw = getpwuid(system_filter_uid);
+  if (pw == NULL)
+    log_write(0, LOG_MAIN|LOG_PANIC_DIE, "Failed to look up uid %ld",
+      (long int)system_filter_uid);
+  system_filter_gid = pw->pw_gid;
+  system_filter_gid_set = TRUE;
+  }
+
+/* If the errors_reply_to field is set, check that it is syntactically valid
+and ensure it contains a domain. */
+
+if (errors_reply_to != NULL)
+  {
+  uschar *errmess;
+  int start, end, domain;
+  uschar *recipient = parse_extract_address(errors_reply_to, &errmess,
+    &start, &end, &domain, FALSE);
+
+  if (recipient == NULL)
+    log_write(0, LOG_PANIC_DIE|LOG_CONFIG,
+      "error in errors_reply_to (%s): %s", errors_reply_to, errmess);
+
+  if (domain == 0)
+    log_write(0, LOG_PANIC_DIE|LOG_CONFIG,
+      "errors_reply_to (%s) does not contain a domain", errors_reply_to);
+  }
+
+/* If smtp_accept_queue or smtp_accept_max_per_host is set, then
+smtp_accept_max must also be set. */
+
+if (smtp_accept_max == 0 &&
+    (smtp_accept_queue > 0 || smtp_accept_max_per_host != NULL))
+  log_write(0, LOG_PANIC_DIE|LOG_CONFIG,
+    "smtp_accept_max must be set if smtp_accept_queue or "
+    "smtp_accept_max_per_host is set");
+
+/* Set up the host number if anything is specified. It is an expanded string
+so that it can be computed from the host name, for example. We do this last
+so as to ensure that everything else is set up before the expansion. */
+
+if (host_number_string != NULL)
+  {
+  long int n;
+  uschar *end;
+  uschar *s = expand_string(host_number_string);
+  if (s == NULL)
+    log_write(0, LOG_MAIN|LOG_PANIC_DIE,
+        "failed to expand localhost_number \"%s\": %s",
+        host_number_string, expand_string_message);
+  n = Ustrtol(s, &end, 0);
+  while (isspace(*end)) end++;
+  if (*end != 0)
+    log_write(0, LOG_PANIC_DIE|LOG_CONFIG,
+      "localhost_number value is not a number: %s", s);
+  if (n > LOCALHOST_MAX)
+    log_write(0, LOG_PANIC_DIE|LOG_CONFIG,
+      "localhost_number is greater than the maximum allowed value (%d)",
+        LOCALHOST_MAX);
+  host_number = n;
+  }
+
+#ifdef SUPPORT_TLS
+/* If tls_verify_hosts is set, tls_verify_certificates must also be set */
+
+if ((tls_verify_hosts != NULL || tls_try_verify_hosts != NULL) &&
+     tls_verify_certificates == NULL)
+  log_write(0, LOG_PANIC_DIE|LOG_CONFIG,
+    "tls_%sverify_hosts is set, but tls_verify_certificates is not set",
+    (tls_verify_hosts != NULL)? "" : "try_");
+
+/* This also checks that the library linkage is working and we can call
+routines in it, so call even if tls_require_ciphers is unset */
+if (!tls_dropprivs_validate_require_cipher())
+  exit(1);
+
+/* Magic number: at time of writing, 1024 has been the long-standing value
+used by so many clients, and what Exim used to use always, that it makes
+sense to just min-clamp this max-clamp at that. */
+if (tls_dh_max_bits < 1024)
+  log_write(0, LOG_PANIC_DIE|LOG_CONFIG,
+      "tls_dh_max_bits is too small, must be at least 1024 for interop");
+
+/* If openssl_options is set, validate it */
+if (openssl_options != NULL)
+  {
+# ifdef USE_GNUTLS
+  log_write(0, LOG_PANIC_DIE|LOG_CONFIG,
+    "openssl_options is set but we're using GnuTLS");
+# else
+  long dummy;
+  if (!(tls_openssl_options_parse(openssl_options, &dummy)))
+    log_write(0, LOG_PANIC_DIE|LOG_CONFIG,
+      "openssl_options parse error: %s", openssl_options);
+# endif
+  }
+
+if (gnutls_require_kx || gnutls_require_mac || gnutls_require_proto)
+  log_write(0, LOG_MAIN, "WARNING: main options"
+      " gnutls_require_kx, gnutls_require_mac and gnutls_require_protocols"
+      " are obsolete\n");
+#endif /*SUPPORT_TLS*/
+}
+
+
+
+/*************************************************
+*          Initialize one driver                 *
+*************************************************/
+
+/* This is called once the driver's generic options, if any, have been read.
+We can now find the driver, set up defaults for the private options, and
+unset any "set" bits in the private options table (which might have been
+set by another incarnation of the same driver).
+
+Arguments:
+  d                   pointer to driver instance block, with generic
+                        options filled in
+  drivers_available   vector of available drivers
+  size_of_info        size of each block in drivers_available
+  class               class of driver, for error message
+
+Returns:              pointer to the driver info block
+*/
+
+static driver_info *
+init_driver(driver_instance *d, driver_info *drivers_available,
+  int size_of_info, uschar *class)
+{
+driver_info *dd;
+
+for (dd = drivers_available; dd->driver_name[0] != 0;
+     dd = (driver_info *)(((uschar *)dd) + size_of_info))
+  {
+  if (Ustrcmp(d->driver_name, dd->driver_name) == 0)
+    {
+    int i;
+    int len = dd->options_len;
+    d->info = dd;
+    d->options_block = store_get(len);
+    memcpy(d->options_block, dd->options_block, len);
+    for (i = 0; i < *(dd->options_count); i++)
+      dd->options[i].type &= ~opt_set;
+    return dd;
+    }
+  }
+
+log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN,
+  "%s %s: cannot find %s driver \"%s\"", class, d->name, class, d->driver_name);
+
+return NULL;   /* never obeyed */
+}
+
+
+
+
+/*************************************************
+*             Initialize driver list             *
+*************************************************/
+
+/* This function is called for routers, transports, and authentication
+mechanisms. It reads the data from the current point in the configuration file
+up to the end of the section, and sets up a chain of instance blocks according
+to the file's contents. The file will already have been opened by a call to
+readconf_main, and must be left open for subsequent reading of further data.
+
+Any errors cause a panic crash. Note that the blocks with names driver_info and
+driver_instance must map the first portions of all the _info and _instance
+blocks for this shared code to work.
+
+Arguments:
+  class                      "router", "transport", or "authenticator"
+  anchor                     &routers, &transports, &auths
+  drivers_available          available drivers
+  size_of_info               size of each info block
+  instance_default           points to default data for an instance
+  instance_size              size of instance block
+  driver_optionlist          generic option list
+  driver_optionlist_count    count of generic option list
+
+Returns:                     nothing
+*/
+
+void
+readconf_driver_init(
+  uschar *class,
+  driver_instance **anchor,
+  driver_info *drivers_available,
+  int size_of_info,
+  void *instance_default,
+  int  instance_size,
+  optionlist *driver_optionlist,
+  int  driver_optionlist_count)
+{
+driver_instance **p = anchor;
+driver_instance *d = NULL;
+uschar *buffer;
+
+while ((buffer = get_config_line()) != NULL)
+  {
+  uschar name[64];
+  uschar *s;
+
+  /* Read the first name on the line and test for the start of a new driver. A
+  macro definition indicates the end of the previous driver. If this isn't the
+  start of a new driver, the line will be re-read. */
+
+  s = readconf_readname(name, sizeof(name), buffer);
+
+  /* Handle macro definition, first finishing off the initialization of the
+  previous driver, if any. */
+
+  if (isupper(*name) && *s == '=')
+    {
+    if (d != NULL)
+      {
+      if (d->driver_name == NULL)
+        log_write(0, LOG_PANIC_DIE|LOG_CONFIG,
+          "no driver defined for %s \"%s\"", class, d->name);
+      (d->info->init)(d);
+      d = NULL;
+      }
+    read_macro_assignment(buffer);
+    continue;
+    }
+
+  /* If the line starts with a name terminated by a colon, we are at the
+  start of the definition of a new driver. The rest of the line must be
+  blank. */
+
+  if (*s++ == ':')
+    {
+    int i;
+
+    /* Finish off initializing the previous driver. */
+
+    if (d != NULL)
+      {
+      if (d->driver_name == NULL)
+        log_write(0, LOG_PANIC_DIE|LOG_CONFIG,
+          "no driver defined for %s \"%s\"", class, d->name);
+      (d->info->init)(d);
+      }
+
+    /* Check that we haven't already got a driver of this name */
+
+    for (d = *anchor; d != NULL; d = d->next)
+      if (Ustrcmp(name, d->name) == 0)
+        log_write(0, LOG_PANIC_DIE|LOG_CONFIG,
+          "there are two %ss called \"%s\"", class, name);
+
+    /* Set up a new driver instance data block on the chain, with
+    its default values installed. */
+
+    d = store_get(instance_size);
+    memcpy(d, instance_default, instance_size);
+    *p = d;
+    p = &(d->next);
+    d->name = string_copy(name);
+
+    /* Clear out the "set" bits in the generic options */
+
+    for (i = 0; i < driver_optionlist_count; i++)
+      driver_optionlist[i].type &= ~opt_set;
+
+    /* Check nothing more on this line, then do the next loop iteration. */
+
+    while (isspace(*s)) s++;
+    if (*s != 0) extra_chars_error(s, US"driver name ", name, US"");
+    continue;
+    }
+
+  /* Not the start of a new driver. Give an error if we have not set up a
+  current driver yet. */
+
+  if (d == NULL) log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN,
+    "%s name missing", class);
+
+  /* First look to see if this is a generic option; if it is "driver",
+  initialize the driver. If is it not a generic option, we can look for a
+  private option provided that the driver has been previously set up. */
+
+  if (readconf_handle_option(buffer, driver_optionlist,
+        driver_optionlist_count, d, NULL))
+    {
+    if (d->info == NULL && d->driver_name != NULL)
+      init_driver(d, drivers_available, size_of_info, class);
+    }
+
+  /* Handle private options - pass the generic block because some may
+  live therein. A flag with each option indicates if it is in the public
+  block. */
+
+  else if (d->info != NULL)
+    {
+    readconf_handle_option(buffer, d->info->options,
+      *(d->info->options_count), d, US"option \"%s\" unknown");
+    }
+
+  /* The option is not generic and the driver name has not yet been given. */
+
+  else log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, "option \"%s\" unknown "
+    "(\"driver\" must be specified before any private options)", name);
+  }
+
+/* Run the initialization function for the final driver. */
+
+if (d != NULL)
+  {
+  if (d->driver_name == NULL)
+    log_write(0, LOG_PANIC_DIE|LOG_CONFIG,
+      "no driver defined for %s \"%s\"", class, d->name);
+  (d->info->init)(d);
+  }
+}
+
+
+
+/*************************************************
+*            Check driver dependency             *
+*************************************************/
+
+/* This function is passed a driver instance and a string. It checks whether
+any of the string options for the driver contains the given string as an
+expansion variable.
+
+Arguments:
+  d        points to a driver instance block
+  s        the string to search for
+
+Returns:   TRUE if a dependency is found
+*/
+
+BOOL
+readconf_depends(driver_instance *d, uschar *s)
+{
+int count = *(d->info->options_count);
+optionlist *ol;
+uschar *ss;
+
+for (ol = d->info->options; ol < d->info->options + count; ol++)
+  {
+  void *options_block;
+  uschar *value;
+  int type = ol->type & opt_mask;
+  if (type != opt_stringptr) continue;
+  options_block = ((ol->type & opt_public) == 0)? d->options_block : (void *)d;
+  value = *(uschar **)((uschar *)options_block + (long int)(ol->value));
+  if (value != NULL && (ss = Ustrstr(value, s)) != NULL)
+    {
+    if (ss <= value || (ss[-1] != '$' && ss[-1] != '{') ||
+      isalnum(ss[Ustrlen(s)])) continue;
+    DEBUG(D_transport) debug_printf("driver %s: \"%s\" option depends on %s\n",
+      d->name, ol->name, s);
+    return TRUE;
+    }
+  }
+
+DEBUG(D_transport) debug_printf("driver %s does not depend on %s\n", d->name, s);
+return FALSE;
+}
+
+
+
+
+/*************************************************
+*      Decode an error type for retries          *
+*************************************************/
+
+/* This function is global because it is also called from the main
+program when testing retry information. It decodes strings such as "quota_7d"
+into numerical error codes.
+
+Arguments:
+  pp           points to start of text
+  p            points past end of text
+  basic_errno  points to an int to receive the main error number
+  more_errno   points to an int to receive the secondary error data
+
+Returns:       NULL if decoded correctly; else points to error text
+*/
+
+uschar *
+readconf_retry_error(uschar *pp, uschar *p, int *basic_errno, int *more_errno)
+{
+int len;
+uschar *q = pp;
+while (q < p && *q != '_') q++;
+len = q - pp;
+
+if (len == 5 && strncmpic(pp, US"quota", len) == 0)
+  {
+  *basic_errno = ERRNO_EXIMQUOTA;
+  if (q != p && (*more_errno = readconf_readtime(q+1, *p, FALSE)) < 0)
+      return US"bad time value";
+  }
+
+else if (len == 7 && strncmpic(pp, US"refused", len) == 0)
+  {
+  *basic_errno = ECONNREFUSED;
+  if (q != p)
+    {
+    if (strncmpic(q+1, US"MX", p-q-1) == 0) *more_errno = 'M';
+    else if (strncmpic(q+1, US"A", p-q-1) == 0) *more_errno = 'A';
+    else return US"A or MX expected after \"refused\"";
+    }
+  }
+
+else if (len == 7 && strncmpic(pp, US"timeout", len) == 0)
+  {
+  *basic_errno = ETIMEDOUT;
+  if (q != p)
+    {
+    int i;
+    int xlen = p - q - 1;
+    uschar *x = q + 1;
+
+    static uschar *extras[] =
+      { US"A", US"MX", US"connect", US"connect_A",  US"connect_MX" };
+    static int values[] =
+      { 'A',   'M',    RTEF_CTOUT,  RTEF_CTOUT|'A', RTEF_CTOUT|'M' };
+
+    for (i = 0; i < sizeof(extras)/sizeof(uschar *); i++)
+      {
+      if (strncmpic(x, extras[i], xlen) == 0)
+        {
+        *more_errno = values[i];
+        break;
+        }
+      }
+
+    if (i >= sizeof(extras)/sizeof(uschar *))
+      {
+      if (strncmpic(x, US"DNS", xlen) == 0)
+        {
+        log_write(0, LOG_MAIN|LOG_PANIC, "\"timeout_dns\" is no longer "
+          "available in retry rules (it has never worked) - treated as "
+          "\"timeout\"");
+        }
+      else return US"\"A\", \"MX\", or \"connect\" expected after \"timeout\"";
+      }
+    }
+  }
+
+else if (strncmpic(pp, US"mail_4", 6) == 0 ||
+         strncmpic(pp, US"rcpt_4", 6) == 0 ||
+         strncmpic(pp, US"data_4", 6) == 0)
+  {
+  BOOL bad = FALSE;
+  int x = 255;                           /* means "any 4xx code" */
+  if (p != pp + 8) bad = TRUE; else
+    {
+    int a = pp[6], b = pp[7];
+    if (isdigit(a))
+      {
+      x = (a - '0') * 10;
+      if (isdigit(b)) x += b - '0';
+      else if (b == 'x') x += 100;
+      else bad = TRUE;
+      }
+    else if (a != 'x' || b != 'x') bad = TRUE;
+    }
+
+  if (bad)
+    return string_sprintf("%.4s_4 must be followed by xx, dx, or dd, where "
+      "x is literal and d is any digit", pp);
+
+  *basic_errno = (*pp == 'm')? ERRNO_MAIL4XX :
+                 (*pp == 'r')? ERRNO_RCPT4XX : ERRNO_DATA4XX;
+  *more_errno = x << 8;
+  }
+
+else if (len == 4 && strncmpic(pp, US"auth", len) == 0 &&
+         strncmpic(q+1, US"failed", p-q-1) == 0)
+  *basic_errno = ERRNO_AUTHFAIL;
+
+else if (strncmpic(pp, US"lost_connection", p - pp) == 0)
+  *basic_errno = ERRNO_SMTPCLOSED;
+
+else if (strncmpic(pp, US"tls_required", p - pp) == 0)
+  *basic_errno = ERRNO_TLSREQUIRED;
+
+else if (len != 1 || Ustrncmp(pp, "*", 1) != 0)
+  return string_sprintf("unknown or malformed retry error \"%.*s\"", (int) (p-pp), pp);
+
+return NULL;
+}
+
+
+
+
+/*************************************************
+*                Read retry information          *
+*************************************************/
+
+/* Each line of retry information contains:
+
+.  A domain name pattern or an address pattern;
+
+.  An error name, possibly with additional data, or *;
+
+.  An optional sequence of retry items, each consisting of an identifying
+   letter, a cutoff time, and optional parameters.
+
+All this is decoded and placed into a control block. */
+
+
+/* Subroutine to read an argument, preceded by a comma and terminated
+by comma, semicolon, whitespace, or newline. The types are: 0 = time value,
+1 = fixed point number (returned *1000).
+
+Arguments:
+  paddr     pointer to pointer to current character; updated
+  type      0 => read a time; 1 => read a fixed point number
+
+Returns:    time in seconds or fixed point number * 1000
+*/
+
+static int
+retry_arg(uschar **paddr, int type)
+{
+uschar *p = *paddr;
+uschar *pp;
+
+if (*p++ != ',') log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, "comma expected");
+
+while (isspace(*p)) p++;
+pp = p;
+while (isalnum(*p) || (type == 1 && *p == '.')) p++;
+
+if (*p != 0 && !isspace(*p) && *p != ',' && *p != ';')
+  log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, "comma or semicolon expected");
+
+*paddr = p;
+switch (type)
+  {
+  case 0:
+  return readconf_readtime(pp, *p, FALSE);
+  case 1:
+  return readconf_readfixed(pp, *p);
+  }
+return 0;    /* Keep picky compilers happy */
+}
+
+/* The function proper */
+
+void
+readconf_retries(void)
+{
+retry_config **chain = &retries;
+retry_config *next;
+uschar *p;
+
+while ((p = get_config_line()) != NULL)
+  {
+  retry_rule **rchain;
+  uschar *pp, *error;
+
+  next = store_get(sizeof(retry_config));
+  next->next = NULL;
+  *chain = next;
+  chain = &(next->next);
+  next->basic_errno = next->more_errno = 0;
+  next->senders = NULL;
+  next->rules = NULL;
+  rchain = &(next->rules);
+
+  next->pattern = string_dequote(&p);
+  while (isspace(*p)) p++;
+  pp = p;
+  while (mac_isgraph(*p)) p++;
+  if (p - pp <= 0) log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN,
+    "missing error type in retry rule");
+
+  /* Test error names for things we understand. */
+
+  if ((error = readconf_retry_error(pp, p, &(next->basic_errno),
+       &(next->more_errno))) != NULL)
+    log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, "%s", error);
+
+  /* There may be an optional address list of senders to be used as another
+  constraint on the rule. This was added later, so the syntax is a bit of a
+  fudge. Anything that is not a retry rule starting "F," or "G," is treated as
+  an address list. */
+
+  while (isspace(*p)) p++;
+  if (Ustrncmp(p, "senders", 7) == 0)
+    {
+    p += 7;
+    while (isspace(*p)) p++;
+    if (*p++ != '=') log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN,
+      "\"=\" expected after \"senders\" in retry rule");
+    while (isspace(*p)) p++;
+    next->senders = string_dequote(&p);
+    }
+
+  /* Now the retry rules. Keep the maximum timeout encountered. */
+
+  while (isspace(*p)) p++;
+
+  while (*p != 0)
+    {
+    retry_rule *rule = store_get(sizeof(retry_rule));
+    *rchain = rule;
+    rchain = &(rule->next);
+    rule->next = NULL;
+    rule->rule = toupper(*p++);
+    rule->timeout = retry_arg(&p, 0);
+    if (rule->timeout > retry_maximum_timeout)
+      retry_maximum_timeout = rule->timeout;
+
+    switch (rule->rule)
+      {
+      case 'F':   /* Fixed interval */
+      rule->p1 = retry_arg(&p, 0);
+      break;
+
+      case 'G':   /* Geometrically increasing intervals */
+      case 'H':   /* Ditto, but with randomness */
+      rule->p1 = retry_arg(&p, 0);
+      rule->p2 = retry_arg(&p, 1);
+      break;
+
+      default:
+      log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, "unknown retry rule letter");
+      break;
+      }
+
+    if (rule->timeout <= 0 || rule->p1 <= 0 ||
+          (rule->rule != 'F' && rule->p2 < 1000))
+      log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN,
+        "bad parameters for retry rule");
+
+    while (isspace(*p)) p++;
+    if (*p == ';')
+      {
+      p++;
+      while (isspace(*p)) p++;
+      }
+    else if (*p != 0)
+      log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, "semicolon expected");
+    }
+  }
+}
+
+
+
+/*************************************************
+*         Initialize authenticators              *
+*************************************************/
+
+/* Read the authenticators section of the configuration file.
+
+Arguments:   none
+Returns:     nothing
+*/
+
+static void
+auths_init(void)
+{
+auth_instance *au, *bu;
+readconf_driver_init(US"authenticator",
+  (driver_instance **)(&auths),      /* chain anchor */
+  (driver_info *)auths_available,    /* available drivers */
+  sizeof(auth_info),                 /* size of info block */
+  &auth_defaults,                    /* default values for generic options */
+  sizeof(auth_instance),             /* size of instance block */
+  optionlist_auths,                  /* generic options */
+  optionlist_auths_size);
+
+for (au = auths; au != NULL; au = au->next)
+  {
+  if (au->public_name == NULL)
+    log_write(0, LOG_PANIC_DIE|LOG_CONFIG, "no public name specified for "
+      "the %s authenticator", au->name);
+  for (bu = au->next; bu != NULL; bu = bu->next)
+    {
+    if (strcmpic(au->public_name, bu->public_name) == 0)
+      {
+      if ((au->client && bu->client) || (au->server && bu->server))
+        log_write(0, LOG_PANIC_DIE|LOG_CONFIG, "two %s authenticators "
+          "(%s and %s) have the same public name (%s)",
+          (au->client)? US"client" : US"server", au->name, bu->name,
+          au->public_name);
+      }
+    }
+  }
+}
+
+
+
+
+/*************************************************
+*             Read ACL information               *
+*************************************************/
+
+/* If this run of Exim is not doing something that involves receiving a
+message, we can just skip over the ACL information. No need to parse it.
+
+First, we have a function for acl_read() to call back to get the next line. We
+need to remember the line we passed, because at the end it will contain the
+name of the next ACL. */
+
+static uschar *acl_line;
+
+static uschar *
+acl_callback(void)
+{
+acl_line = get_config_line();
+return acl_line;
+}
+
+
+/* Now the main function:
+
+Arguments:    none
+Returns:      nothing
+*/
+
+static void
+readconf_acl(void)
+{
+uschar *p;
+
+/* Read each ACL and add it into the tree. Macro (re)definitions are allowed
+between ACLs. */
+
+acl_line = get_config_line();
+
+while(acl_line != NULL)
+  {
+  uschar name[64];
+  tree_node *node;
+  uschar *error;
+
+  p = readconf_readname(name, sizeof(name), acl_line);
+  if (isupper(*name) && *p == '=')
+    {
+    read_macro_assignment(acl_line);
+    acl_line = get_config_line();
+    continue;
+    }
+
+  if (*p != ':' || name[0] == 0)
+    log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, "missing or malformed ACL name");
+
+  node = store_get(sizeof(tree_node) + Ustrlen(name));
+  Ustrcpy(node->name, name);
+  if (!tree_insertnode(&acl_anchor, node))
+    log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN,
+      "there are two ACLs called \"%s\"", name);
+
+  node->data.ptr = acl_read(acl_callback, &error);
+
+  if (node->data.ptr == NULL && error != NULL)
+    log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, "error in ACL: %s", error);
+  }
+}
+
+
+
+/*************************************************
+*     Read configuration for local_scan()        *
+*************************************************/
+
+/* This function is called after "begin local_scan" is encountered in the
+configuration file. If the local_scan() function allows for configuration
+options, we can process them. Otherwise, we expire in a panic.
+
+Arguments:  none
+Returns:    nothing
+*/
+
+static void
+local_scan_init(void)
+{
+#ifndef LOCAL_SCAN_HAS_OPTIONS
+log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, "local_scan() options not supported: "
+  "(LOCAL_SCAN_HAS_OPTIONS not defined in Local/Makefile)");
+#else
+
+uschar *p;
+while ((p = get_config_line()) != NULL)
+  {
+  (void) readconf_handle_option(p, local_scan_options, local_scan_options_count,
+    NULL, US"local_scan option \"%s\" unknown");
+  }
+#endif
+}
+
+
+
+/*************************************************
+*     Read rest of configuration (after main)    *
+*************************************************/
+
+/* This function reads the rest of the runtime configuration, after the main
+configuration. It is called only when actually needed. Each subsequent section
+of the configuration starts with a line of the form
+
+  begin name
+
+where the name is "routers", "transports", etc. A section is terminated by
+hitting the next "begin" line, and the next name is left in next_section.
+Because it may confuse people as to whether the names are singular or plural,
+we add "s" if it's missing. There is always enough room in next_section for
+this. This function is basically just a switch.
+
+Arguments:   none
+Returns:     nothing
+*/
+
+static uschar *section_list[] = {
+  US"acls",
+  US"authenticators",
+  US"local_scans",
+  US"retrys",
+  US"rewrites",
+  US"routers",
+  US"transports"};
+
+void
+readconf_rest(void)
+{
+int had = 0;
+
+while(next_section[0] != 0)
+  {
+  int bit;
+  int first = 0;
+  int last = sizeof(section_list) / sizeof(uschar *);
+  int mid = last/2;
+  int n = Ustrlen(next_section);
+
+  if (tolower(next_section[n-1]) != 's') Ustrcpy(next_section+n, "s");
+
+  for (;;)
+    {
+    int c = strcmpic(next_section, section_list[mid]);
+    if (c == 0) break;
+    if (c > 0) first = mid + 1; else last = mid;
+    if (first >= last)
+      log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN,
+        "\"%.*s\" is not a known configuration section name", n, next_section);
+    mid = (last + first)/2;
+    }
+
+  bit = 1 << mid;
+  if (((had ^= bit) & bit) == 0)
+    log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN,
+      "\"%.*s\" section is repeated in the configuration file", n,
+        next_section);
+
+  switch(mid)
+    {
+    case 0: readconf_acl(); break;
+    case 1: auths_init(); break;
+    case 2: local_scan_init(); break;
+    case 3: readconf_retries(); break;
+    case 4: readconf_rewrites(); break;
+    case 5: route_init(); break;
+    case 6: transport_init(); break;
+    }
+  }
+
+(void)fclose(config_file);
+}
+
+/* vi: aw ai sw=2
+*/
+/* End of readconf.c */
diff --git a/src/receive.c b/src/receive.c
new file mode 100644 (file)
index 0000000..f27dc42
--- /dev/null
@@ -0,0 +1,4157 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2014 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Code for receiving a message and setting up spool files. */
+
+#include "exim.h"
+
+#ifdef EXPERIMENTAL_DCC
+extern int dcc_ok;
+#endif
+
+#ifdef EXPERIMENTAL_DMARC
+# include "dmarc.h"
+#endif /* EXPERIMENTAL_DMARC */
+
+/*************************************************
+*                Local static variables          *
+*************************************************/
+
+static FILE   *data_file = NULL;
+static int     data_fd = -1;
+static uschar  spool_name[256];
+
+
+
+/*************************************************
+*      Non-SMTP character reading functions      *
+*************************************************/
+
+/* These are the default functions that are set up in the variables such as
+receive_getc initially. They just call the standard functions, passing stdin as
+the file. (When SMTP input is occurring, different functions are used by
+changing the pointer variables.) */
+
+int
+stdin_getc(void)
+{
+return getc(stdin);
+}
+
+int
+stdin_ungetc(int c)
+{
+return ungetc(c, stdin);
+}
+
+int
+stdin_feof(void)
+{
+return feof(stdin);
+}
+
+int
+stdin_ferror(void)
+{
+return ferror(stdin);
+}
+
+
+
+
+/*************************************************
+*     Check that a set sender is allowed         *
+*************************************************/
+
+/* This function is called when a local caller sets an explicit sender address.
+It checks whether this is permitted, which it is for trusted callers.
+Otherwise, it must match the pattern(s) in untrusted_set_sender.
+
+Arguments:  the proposed sender address
+Returns:    TRUE for a trusted caller
+            TRUE if the address has been set, untrusted_set_sender has been
+              set, and the address matches something in the list
+            FALSE otherwise
+*/
+
+BOOL
+receive_check_set_sender(uschar *newsender)
+{
+uschar *qnewsender;
+if (trusted_caller) return TRUE;
+if (newsender == NULL || untrusted_set_sender == NULL) return FALSE;
+qnewsender = (Ustrchr(newsender, '@') != NULL)?
+  newsender : string_sprintf("%s@%s", newsender, qualify_domain_sender);
+return
+  match_address_list(qnewsender, TRUE, TRUE, &untrusted_set_sender, NULL, -1,
+    0, NULL) == OK;
+}
+
+
+
+
+/*************************************************
+*          Read space info for a partition       *
+*************************************************/
+
+/* This function is called by receive_check_fs() below, and also by string
+expansion for variables such as $spool_space. The field names for the statvfs
+structure are macros, because not all OS have F_FAVAIL and it seems tidier to
+have macros for F_BAVAIL and F_FILES as well. Some kinds of file system do not
+have inodes, and they return -1 for the number available.
+
+Later: It turns out that some file systems that do not have the concept of
+inodes return 0 rather than -1. Such systems should also return 0 for the total
+number of inodes, so we require that to be greater than zero before returning
+an inode count.
+
+Arguments:
+  isspool       TRUE for spool partition, FALSE for log partition
+  inodeptr      address of int to receive inode count; -1 if there isn't one
+
+Returns:        available on-root space, in kilobytes
+                -1 for log partition if there isn't one
+
+All values are -1 if the STATFS functions are not available.
+*/
+
+int
+receive_statvfs(BOOL isspool, int *inodeptr)
+{
+#ifdef HAVE_STATFS
+struct STATVFS statbuf;
+uschar *path;
+uschar *name;
+uschar buffer[1024];
+
+/* The spool directory must always exist. */
+
+if (isspool)
+  {
+  path = spool_directory;
+  name = US"spool";
+  }
+
+/* Need to cut down the log file path to the directory, and to ignore any
+appearance of "syslog" in it. */
+
+else
+  {
+  int sep = ':';              /* Not variable - outside scripts use */
+  uschar *p = log_file_path;
+  name = US"log";
+
+  /* An empty log_file_path means "use the default". This is the same as an
+  empty item in a list. */
+
+  if (*p == 0) p = US":";
+  while ((path = string_nextinlist(&p, &sep, buffer, sizeof(buffer))) != NULL)
+    {
+    if (Ustrcmp(path, "syslog") != 0) break;
+    }
+
+  if (path == NULL)  /* No log files */
+    {
+    *inodeptr = -1;
+    return -1;
+    }
+
+  /* An empty string means use the default, which is in the spool directory.
+  But don't just use the spool directory, as it is possible that the log
+  subdirectory has been symbolically linked elsewhere. */
+
+  if (path[0] == 0)
+    {
+    sprintf(CS buffer, CS"%s/log", CS spool_directory);
+    path = buffer;
+    }
+  else
+    {
+    uschar *cp;
+    if ((cp = Ustrrchr(path, '/')) != NULL) *cp = 0;
+    }
+  }
+
+/* We now have the path; do the business */
+
+memset(&statbuf, 0, sizeof(statbuf));
+
+if (STATVFS(CS path, &statbuf) != 0)
+  {
+  log_write(0, LOG_MAIN|LOG_PANIC, "cannot accept message: failed to stat "
+    "%s directory %s: %s", name, spool_directory, strerror(errno));
+  smtp_closedown(US"spool or log directory problem");
+  exim_exit(EXIT_FAILURE);
+  }
+
+*inodeptr = (statbuf.F_FILES > 0)? statbuf.F_FAVAIL : -1;
+
+/* Disks are getting huge. Take care with computing the size in kilobytes. */
+
+return (int)(((double)statbuf.F_BAVAIL * (double)statbuf.F_FRSIZE)/1024.0);
+
+/* Unable to find partition sizes in this environment. */
+
+#else
+*inodeptr = -1;
+return -1;
+#endif
+}
+
+
+
+
+/*************************************************
+*     Check space on spool and log partitions    *
+*************************************************/
+
+/* This function is called before accepting a message; if any thresholds are
+set, it checks them. If a message_size is supplied, it checks that there is
+enough space for that size plus the threshold - i.e. that the message won't
+reduce the space to the threshold. Not all OS have statvfs(); for those that
+don't, this function always returns TRUE. For some OS the old function and
+struct name statfs is used; that is handled by a macro, defined in exim.h.
+
+Arguments:
+  msg_size     the (estimated) size of an incoming message
+
+Returns:       FALSE if there isn't enough space, or if the information cannot
+                 be obtained
+               TRUE if no check was done or there is enough space
+*/
+
+BOOL
+receive_check_fs(int msg_size)
+{
+int space, inodes;
+
+if (check_spool_space > 0 || msg_size > 0 || check_spool_inodes > 0)
+  {
+  space = receive_statvfs(TRUE, &inodes);
+
+  DEBUG(D_receive)
+    debug_printf("spool directory space = %dK inodes = %d "
+      "check_space = %dK inodes = %d msg_size = %d\n",
+      space, inodes, check_spool_space, check_spool_inodes, msg_size);
+
+  if ((space >= 0 && space < check_spool_space) ||
+      (inodes >= 0 && inodes < check_spool_inodes))
+    {
+    log_write(0, LOG_MAIN, "spool directory space check failed: space=%d "
+      "inodes=%d", space, inodes);
+    return FALSE;
+    }
+  }
+
+if (check_log_space > 0 || check_log_inodes > 0)
+  {
+  space = receive_statvfs(FALSE, &inodes);
+
+  DEBUG(D_receive)
+    debug_printf("log directory space = %dK inodes = %d "
+      "check_space = %dK inodes = %d\n",
+      space, inodes, check_log_space, check_log_inodes);
+
+  if ((space >= 0 && space < check_log_space) ||
+      (inodes >= 0 && inodes < check_log_inodes))
+    {
+    log_write(0, LOG_MAIN, "log directory space check failed: space=%d "
+      "inodes=%d", space, inodes);
+    return FALSE;
+    }
+  }
+
+return TRUE;
+}
+
+
+
+/*************************************************
+*         Bomb out while reading a message       *
+*************************************************/
+
+/* The common case of wanting to bomb out is if a SIGTERM or SIGINT is
+received, or if there is a timeout. A rarer case might be if the log files are
+screwed up and Exim can't open them to record a message's arrival. Handling
+that case is done by setting a flag to cause the log functions to call this
+function if there is an ultimate disaster. That is why it is globally
+accessible.
+
+Arguments:
+  reason     text reason to pass to the not-quit ACL
+  msg        default SMTP response to give if in an SMTP session
+Returns:     it doesn't
+*/
+
+void
+receive_bomb_out(uschar *reason, uschar *msg)
+{
+  static BOOL already_bombing_out;
+/* The smtp_notquit_exit() below can call ACLs which can trigger recursive
+timeouts, if someone has something slow in their quit ACL.  Since the only
+things we should be doing are to close down cleanly ASAP, on the second
+pass we also close down stuff that might be opened again, before bypassing
+the ACL call and exiting. */
+
+/* If spool_name is set, it contains the name of the data file that is being
+written. Unlink it before closing so that it cannot be picked up by a delivery
+process. Ensure that any header file is also removed. */
+
+if (spool_name[0] != '\0')
+  {
+  Uunlink(spool_name);
+  spool_name[Ustrlen(spool_name) - 1] = 'H';
+  Uunlink(spool_name);
+  spool_name[0] = '\0';
+  }
+
+/* Now close the file if it is open, either as a fd or a stream. */
+
+if (data_file != NULL)
+  {
+  (void)fclose(data_file);
+  data_file = NULL;
+} else if (data_fd >= 0) {
+  (void)close(data_fd);
+  data_fd = -1;
+  }
+
+/* Attempt to close down an SMTP connection tidily. For non-batched SMTP, call
+smtp_notquit_exit(), which runs the NOTQUIT ACL, if present, and handles the
+SMTP response. */
+
+if (!already_bombing_out)
+  {
+  already_bombing_out = TRUE;
+  if (smtp_input)
+    {
+    if (smtp_batched_input)
+      moan_smtp_batch(NULL, "421 %s - message abandoned", msg);  /* No return */
+    smtp_notquit_exit(reason, US"421", US"%s %s - closing connection.",
+      smtp_active_hostname, msg);
+    }
+  }
+
+/* Exit from the program (non-BSMTP cases) */
+
+exim_exit(EXIT_FAILURE);
+}
+
+
+/*************************************************
+*              Data read timeout                 *
+*************************************************/
+
+/* Handler function for timeouts that occur while reading the data that
+comprises a message.
+
+Argument:  the signal number
+Returns:   nothing
+*/
+
+static void
+data_timeout_handler(int sig)
+{
+uschar *msg = NULL;
+
+sig = sig;    /* Keep picky compilers happy */
+
+if (smtp_input)
+  {
+  msg = US"SMTP incoming data timeout";
+  log_write(L_lost_incoming_connection,
+            LOG_MAIN, "SMTP data timeout (message abandoned) on connection "
+            "from %s F=<%s>",
+            (sender_fullhost != NULL)? sender_fullhost : US"local process",
+            sender_address);
+  }
+else
+  {
+  fprintf(stderr, "exim: timed out while reading - message abandoned\n");
+  log_write(L_lost_incoming_connection,
+            LOG_MAIN, "timed out while reading local message");
+  }
+
+receive_bomb_out(US"data-timeout", msg);   /* Does not return */
+}
+
+
+
+/*************************************************
+*              local_scan() timeout              *
+*************************************************/
+
+/* Handler function for timeouts that occur while running a local_scan()
+function.
+
+Argument:  the signal number
+Returns:   nothing
+*/
+
+static void
+local_scan_timeout_handler(int sig)
+{
+sig = sig;    /* Keep picky compilers happy */
+log_write(0, LOG_MAIN|LOG_REJECT, "local_scan() function timed out - "
+  "message temporarily rejected (size %d)", message_size);
+/* Does not return */
+receive_bomb_out(US"local-scan-timeout", US"local verification problem");
+}
+
+
+
+/*************************************************
+*            local_scan() crashed                *
+*************************************************/
+
+/* Handler function for signals that occur while running a local_scan()
+function.
+
+Argument:  the signal number
+Returns:   nothing
+*/
+
+static void
+local_scan_crash_handler(int sig)
+{
+log_write(0, LOG_MAIN|LOG_REJECT, "local_scan() function crashed with "
+  "signal %d - message temporarily rejected (size %d)", sig, message_size);
+/* Does not return */
+receive_bomb_out(US"local-scan-error", US"local verification problem");
+}
+
+
+/*************************************************
+*           SIGTERM or SIGINT received           *
+*************************************************/
+
+/* Handler for SIGTERM or SIGINT signals that occur while reading the
+data that comprises a message.
+
+Argument:  the signal number
+Returns:   nothing
+*/
+
+static void
+data_sigterm_sigint_handler(int sig)
+{
+uschar *msg = NULL;
+
+if (smtp_input)
+  {
+  msg = US"Service not available - SIGTERM or SIGINT received";
+  log_write(0, LOG_MAIN, "%s closed after %s", smtp_get_connection_info(),
+    (sig == SIGTERM)? "SIGTERM" : "SIGINT");
+  }
+else
+  {
+  if (filter_test == FTEST_NONE)
+    {
+    fprintf(stderr, "\nexim: %s received - message abandoned\n",
+      (sig == SIGTERM)? "SIGTERM" : "SIGINT");
+    log_write(0, LOG_MAIN, "%s received while reading local message",
+      (sig == SIGTERM)? "SIGTERM" : "SIGINT");
+    }
+  }
+
+receive_bomb_out(US"signal-exit", msg);    /* Does not return */
+}
+
+
+
+/*************************************************
+*          Add new recipient to list             *
+*************************************************/
+
+/* This function builds a list of recipient addresses in argc/argv
+format.
+
+Arguments:
+  recipient   the next address to add to recipients_list
+  pno         parent number for fixed aliases; -1 otherwise
+
+Returns:      nothing
+*/
+
+void
+receive_add_recipient(uschar *recipient, int pno)
+{
+if (recipients_count >= recipients_list_max)
+  {
+  recipient_item *oldlist = recipients_list;
+  int oldmax = recipients_list_max;
+  recipients_list_max = recipients_list_max? 2*recipients_list_max : 50;
+  recipients_list = store_get(recipients_list_max * sizeof(recipient_item));
+  if (oldlist != NULL)
+    memcpy(recipients_list, oldlist, oldmax * sizeof(recipient_item));
+  }
+
+recipients_list[recipients_count].address = recipient;
+recipients_list[recipients_count].pno = pno;
+#ifdef EXPERIMENTAL_BRIGHTMAIL
+recipients_list[recipients_count].bmi_optin = bmi_current_optin;
+/* reset optin string pointer for next recipient */
+bmi_current_optin = NULL;
+#endif
+#ifdef EXPERIMENTAL_DSN
+recipients_list[recipients_count].orcpt = NULL;
+recipients_list[recipients_count].dsn_flags = 0;
+#endif
+recipients_list[recipients_count++].errors_to = NULL;
+}
+
+
+
+
+/*************************************************
+*        Send user response message              *
+*************************************************/
+
+/* This function is passed a default response code and a user message. It calls
+smtp_message_code() to check and possibly modify the response code, and then
+calls smtp_respond() to transmit the response. I put this into a function
+just to avoid a lot of repetition.
+
+Arguments:
+  code         the response code
+  user_msg     the user message
+
+Returns:       nothing
+*/
+
+#ifndef DISABLE_PRDR
+static void
+smtp_user_msg(uschar *code, uschar *user_msg)
+{
+int len = 3;
+smtp_message_code(&code, &len, &user_msg, NULL);
+smtp_respond(code, len, TRUE, user_msg);
+}
+#endif
+
+
+
+
+
+/*************************************************
+*        Remove a recipient from the list        *
+*************************************************/
+
+/* This function is provided for local_scan() to use.
+
+Argument:
+  recipient   address to remove
+
+Returns:      TRUE if it did remove something; FALSE otherwise
+*/
+
+BOOL
+receive_remove_recipient(uschar *recipient)
+{
+int count;
+DEBUG(D_receive) debug_printf("receive_remove_recipient(\"%s\") called\n",
+  recipient);
+for (count = 0; count < recipients_count; count++)
+  {
+  if (Ustrcmp(recipients_list[count].address, recipient) == 0)
+    {
+    if ((--recipients_count - count) > 0)
+      memmove(recipients_list + count, recipients_list + count + 1,
+        (recipients_count - count)*sizeof(recipient_item));
+    return TRUE;
+    }
+  }
+return FALSE;
+}
+
+
+
+
+
+/*************************************************
+*     Read data portion of a non-SMTP message    *
+*************************************************/
+
+/* This function is called to read the remainder of a message (following the
+header) when the input is not from SMTP - we are receiving a local message on
+a standard input stream. The message is always terminated by EOF, and is also
+terminated by a dot on a line by itself if the flag dot_ends is TRUE. Split the
+two cases for maximum efficiency.
+
+Ensure that the body ends with a newline. This will naturally be the case when
+the termination is "\n.\n" but may not be otherwise. The RFC defines messages
+as "sequences of lines" - this of course strictly applies only to SMTP, but
+deliveries into BSD-type mailbox files also require it. Exim used to have a
+flag for doing this at delivery time, but as it was always set for all
+transports, I decided to simplify things by putting the check here instead.
+
+There is at least one MUA (dtmail) that sends CRLF via this interface, and
+other programs are known to do this as well. Exim used to have a option for
+dealing with this: in July 2003, after much discussion, the code has been
+changed to default to treat any of LF, CRLF, and bare CR as line terminators.
+
+However, for the case when a dot on a line by itself terminates a message, the
+only recognized terminating sequences before and after the dot are LF and CRLF.
+Otherwise, having read EOL . CR, you don't know whether to read another
+character or not.
+
+Internally, in messages stored in Exim's spool files, LF is used as the line
+terminator. Under the new regime, bare CRs will no longer appear in these
+files.
+
+Arguments:
+  fout      a FILE to which to write the message
+
+Returns:    One of the END_xxx values indicating why it stopped reading
+*/
+
+static int
+read_message_data(FILE *fout)
+{
+int ch_state;
+register int ch;
+register int linelength = 0;
+
+/* Handle the case when only EOF terminates the message */
+
+if (!dot_ends)
+  {
+  register int last_ch = '\n';
+
+  for (; (ch = (receive_getc)()) != EOF; last_ch = ch)
+    {
+    if (ch == 0) body_zerocount++;
+    if (last_ch == '\r' && ch != '\n')
+      {
+      if (linelength > max_received_linelength)
+        max_received_linelength = linelength;
+      linelength = 0;
+      if (fputc('\n', fout) == EOF) return END_WERROR;
+      message_size++;
+      body_linecount++;
+      }
+    if (ch == '\r') continue;
+
+    if (fputc(ch, fout) == EOF) return END_WERROR;
+    if (ch == '\n')
+      {
+      if (linelength > max_received_linelength)
+        max_received_linelength = linelength;
+      linelength = 0;
+      body_linecount++;
+      }
+    else linelength++;
+    if (++message_size > thismessage_size_limit) return END_SIZE;
+    }
+
+  if (last_ch != '\n')
+    {
+    if (linelength > max_received_linelength)
+      max_received_linelength = linelength;
+    if (fputc('\n', fout) == EOF) return END_WERROR;
+    message_size++;
+    body_linecount++;
+    }
+
+  return END_EOF;
+  }
+
+/* Handle the case when a dot on a line on its own, or EOF, terminates. */
+
+ch_state = 1;
+
+while ((ch = (receive_getc)()) != EOF)
+  {
+  if (ch == 0) body_zerocount++;
+  switch (ch_state)
+    {
+    case 0:                         /* Normal state (previous char written) */
+    if (ch == '\n')
+      {
+      body_linecount++;
+      if (linelength > max_received_linelength)
+        max_received_linelength = linelength;
+      linelength = -1;
+      ch_state = 1;
+      }
+    else if (ch == '\r')
+      { ch_state = 2; continue; }
+    break;
+
+    case 1:                         /* After written "\n" */
+    if (ch == '.') { ch_state = 3; continue; }
+    if (ch == '\r') { ch_state = 2; continue; }
+    if (ch != '\n') ch_state = 0; else linelength = -1;
+    break;
+
+    case 2:
+    body_linecount++;               /* After unwritten "\r" */
+    if (linelength > max_received_linelength)
+      max_received_linelength = linelength;
+    if (ch == '\n')
+      {
+      ch_state = 1;
+      linelength = -1;
+      }
+    else
+      {
+      if (message_size++, fputc('\n', fout) == EOF) return END_WERROR;
+      if (ch == '\r') continue;
+      ch_state = 0;
+      linelength = 0;
+      }
+    break;
+
+    case 3:                         /* After "\n." (\n written, dot not) */
+    if (ch == '\n') return END_DOT;
+    if (ch == '\r') { ch_state = 4; continue; }
+    message_size++;
+    linelength++;
+    if (fputc('.', fout) == EOF) return END_WERROR;
+    ch_state = 0;
+    break;
+
+    case 4:                         /* After "\n.\r" (\n written, rest not) */
+    if (ch == '\n') return END_DOT;
+    message_size += 2;
+    body_linecount++;
+    if (fputs(".\n", fout) == EOF) return END_WERROR;
+    if (ch == '\r') { ch_state = 2; continue; }
+    ch_state = 0;
+    break;
+    }
+
+  linelength++;
+  if (fputc(ch, fout) == EOF) return END_WERROR;
+  if (++message_size > thismessage_size_limit) return END_SIZE;
+  }
+
+/* Get here if EOF read. Unless we have just written "\n", we need to ensure
+the message ends with a newline, and we must also write any characters that
+were saved up while testing for an ending dot. */
+
+if (ch_state != 1)
+  {
+  static uschar *ends[] = { US"\n", NULL, US"\n", US".\n", US".\n" };
+  if (fputs(CS ends[ch_state], fout) == EOF) return END_WERROR;
+  message_size += Ustrlen(ends[ch_state]);
+  body_linecount++;
+  }
+
+return END_EOF;
+}
+
+
+
+
+/*************************************************
+*      Read data portion of an SMTP message      *
+*************************************************/
+
+/* This function is called to read the remainder of an SMTP message (after the
+headers), or to skip over it when an error has occurred. In this case, the
+output file is passed as NULL.
+
+If any line begins with a dot, that character is skipped. The input should only
+be successfully terminated by CR LF . CR LF unless it is local (non-network)
+SMTP, in which case the CRs are optional, but...
+
+FUDGE: It seems that sites on the net send out messages with just LF
+terminators, despite the warnings in the RFCs, and other MTAs handle this. So
+we make the CRs optional in all cases.
+
+July 2003: Bare CRs cause trouble. We now treat them as line terminators as
+well, so that there are no CRs in spooled messages. However, the message
+terminating dot is not recognized between two bare CRs.
+
+Arguments:
+  fout      a FILE to which to write the message; NULL if skipping
+
+Returns:    One of the END_xxx values indicating why it stopped reading
+*/
+
+static int
+read_message_data_smtp(FILE *fout)
+{
+int ch_state = 0;
+int ch;
+register int linelength = 0;
+
+while ((ch = (receive_getc)()) != EOF)
+  {
+  if (ch == 0) body_zerocount++;
+  switch (ch_state)
+    {
+    case 0:                             /* After LF or CRLF */
+    if (ch == '.')
+      {
+      ch_state = 3;
+      continue;                         /* Don't ever write . after LF */
+      }
+    ch_state = 1;
+
+    /* Else fall through to handle as normal uschar. */
+
+    case 1:                             /* Normal state */
+    if (ch == '\n')
+      {
+      ch_state = 0;
+      body_linecount++;
+      if (linelength > max_received_linelength)
+        max_received_linelength = linelength;
+      linelength = -1;
+      }
+    else if (ch == '\r')
+      {
+      ch_state = 2;
+      continue;
+      }
+    break;
+
+    case 2:                             /* After (unwritten) CR */
+    body_linecount++;
+    if (linelength > max_received_linelength)
+      max_received_linelength = linelength;
+    linelength = -1;
+    if (ch == '\n')
+      {
+      ch_state = 0;
+      }
+    else
+      {
+      message_size++;
+      if (fout != NULL && fputc('\n', fout) == EOF) return END_WERROR;
+      (void) cutthrough_put_nl();
+      if (ch != '\r') ch_state = 1; else continue;
+      }
+    break;
+
+    case 3:                             /* After [CR] LF . */
+    if (ch == '\n')
+      return END_DOT;
+    if (ch == '\r')
+      {
+      ch_state = 4;
+      continue;
+      }
+    ch_state = 1;                       /* The dot itself is removed */
+    break;
+
+    case 4:                             /* After [CR] LF . CR */
+    if (ch == '\n') return END_DOT;
+    message_size++;
+    body_linecount++;
+    if (fout != NULL && fputc('\n', fout) == EOF) return END_WERROR;
+    (void) cutthrough_put_nl();
+    if (ch == '\r')
+      {
+      ch_state = 2;
+      continue;
+      }
+    ch_state = 1;
+    break;
+    }
+
+  /* Add the character to the spool file, unless skipping; then loop for the
+  next. */
+
+  message_size++;
+  linelength++;
+  if (fout != NULL)
+    {
+    if (fputc(ch, fout) == EOF) return END_WERROR;
+    if (message_size > thismessage_size_limit) return END_SIZE;
+    }
+  if(ch == '\n')
+    (void) cutthrough_put_nl();
+  else
+    {
+    uschar c= ch;
+    (void) cutthrough_puts(&c, 1);
+    }
+  }
+
+/* Fall through here if EOF encountered. This indicates some kind of error,
+since a correct message is terminated by [CR] LF . [CR] LF. */
+
+return END_EOF;
+}
+
+
+
+
+/*************************************************
+*             Swallow SMTP message               *
+*************************************************/
+
+/* This function is called when there has been some kind of error while reading
+an SMTP message, and the remaining data may need to be swallowed. It is global
+because it is called from smtp_closedown() to shut down an incoming call
+tidily.
+
+Argument:    a FILE from which to read the message
+Returns:     nothing
+*/
+
+void
+receive_swallow_smtp(void)
+{
+if (message_ended >= END_NOTENDED)
+  message_ended = read_message_data_smtp(NULL);
+}
+
+
+
+/*************************************************
+*           Handle lost SMTP connection          *
+*************************************************/
+
+/* This function logs connection loss incidents and generates an appropriate
+SMTP response.
+
+Argument:  additional data for the message
+Returns:   the SMTP response
+*/
+
+static uschar *
+handle_lost_connection(uschar *s)
+{
+log_write(L_lost_incoming_connection | L_smtp_connection, LOG_MAIN,
+  "%s lost while reading message data%s", smtp_get_connection_info(), s);
+return US"421 Lost incoming connection";
+}
+
+
+
+
+/*************************************************
+*         Handle a non-smtp reception error      *
+*************************************************/
+
+/* This function is called for various errors during the reception of non-SMTP
+messages. It either sends a message to the sender of the problem message, or it
+writes to the standard error stream.
+
+Arguments:
+  errcode     code for moan_to_sender(), identifying the error
+  text1       first message text, passed to moan_to_sender()
+  text2       second message text, used only for stderrr
+  error_rc    code to pass to exim_exit if no problem
+  f           FILE containing body of message (may be stdin)
+  hptr        pointer to instore headers or NULL
+
+Returns:      calls exim_exit(), which does not return
+*/
+
+static void
+give_local_error(int errcode, uschar *text1, uschar *text2, int error_rc,
+  FILE *f, header_line *hptr)
+{
+if (error_handling == ERRORS_SENDER)
+  {
+  error_block eblock;
+  eblock.next = NULL;
+  eblock.text1 = text1;
+  if (!moan_to_sender(errcode, &eblock, hptr, f, FALSE))
+    error_rc = EXIT_FAILURE;
+  }
+else fprintf(stderr, "exim: %s%s\n", text2, text1);  /* Sic */
+(void)fclose(f);
+exim_exit(error_rc);
+}
+
+
+
+/*************************************************
+*          Add header lines set up by ACL        *
+*************************************************/
+
+/* This function is called to add the header lines that were set up by
+statements in an ACL to the list of headers in memory. It is done in two stages
+like this, because when the ACL for RCPT is running, the other headers have not
+yet been received. This function is called twice; once just before running the
+DATA ACL, and once after. This is so that header lines added by MAIL or RCPT
+are visible to the DATA ACL.
+
+Originally these header lines were added at the end. Now there is support for
+three different places: top, bottom, and after the Received: header(s). There
+will always be at least one Received: header, even if it is marked deleted, and
+even if something else has been put in front of it.
+
+Arguments:
+  acl_name   text to identify which ACL
+
+Returns:     nothing
+*/
+
+static void
+add_acl_headers(int where, uschar *acl_name)
+{
+header_line *h, *next;
+header_line *last_received = NULL;
+
+switch(where)
+  {
+  case ACL_WHERE_DKIM:
+  case ACL_WHERE_MIME:
+  case ACL_WHERE_DATA:
+    if (cutthrough_fd >= 0 && (acl_removed_headers || acl_added_headers))
+    {
+    log_write(0, LOG_MAIN|LOG_PANIC, "Header modification in data ACLs"
+                       " will not take effect on cutthrough deliveries");
+    return;
+    }
+  }
+
+if (acl_removed_headers != NULL)
+  {
+  DEBUG(D_receive|D_acl) debug_printf(">>Headers removed by %s ACL:\n", acl_name);
+
+  for (h = header_list; h != NULL; h = h->next)
+    {
+    uschar *list;
+    BOOL include_header;
+
+    if (h->type == htype_old) continue;
+
+    include_header = TRUE;
+    list = acl_removed_headers;
+
+    int sep = ':';         /* This is specified as a colon-separated list */
+    uschar *s;
+    uschar buffer[128];
+    while ((s = string_nextinlist(&list, &sep, buffer, sizeof(buffer)))
+            != NULL)
+      {
+      int len = Ustrlen(s);
+      if (header_testname(h, s, len, FALSE))
+       {
+       h->type = htype_old;
+        DEBUG(D_receive|D_acl) debug_printf("  %s", h->text);
+       }
+      }
+    }
+  acl_removed_headers = NULL;
+  DEBUG(D_receive|D_acl) debug_printf(">>\n");
+  }
+
+if (acl_added_headers == NULL) return;
+DEBUG(D_receive|D_acl) debug_printf(">>Headers added by %s ACL:\n", acl_name);
+
+for (h = acl_added_headers; h != NULL; h = next)
+  {
+  next = h->next;
+
+  switch(h->type)
+    {
+    case htype_add_top:
+    h->next = header_list;
+    header_list = h;
+    DEBUG(D_receive|D_acl) debug_printf("  (at top)");
+    break;
+
+    case htype_add_rec:
+    if (last_received == NULL)
+      {
+      last_received = header_list;
+      while (!header_testname(last_received, US"Received", 8, FALSE))
+        last_received = last_received->next;
+      while (last_received->next != NULL &&
+             header_testname(last_received->next, US"Received", 8, FALSE))
+        last_received = last_received->next;
+      }
+    h->next = last_received->next;
+    last_received->next = h;
+    DEBUG(D_receive|D_acl) debug_printf("  (after Received:)");
+    break;
+
+    case htype_add_rfc:
+    /* add header before any header which is NOT Received: or Resent- */
+    last_received = header_list;
+    while ( (last_received->next != NULL) &&
+            ( (header_testname(last_received->next, US"Received", 8, FALSE)) ||
+              (header_testname_incomplete(last_received->next, US"Resent-", 7, FALSE)) ) )
+              last_received = last_received->next;
+    /* last_received now points to the last Received: or Resent-* header
+       in an uninterrupted chain of those header types (seen from the beginning
+       of all headers. Our current header must follow it. */
+    h->next = last_received->next;
+    last_received->next = h;
+    DEBUG(D_receive|D_acl) debug_printf("  (before any non-Received: or Resent-*: header)");
+    break;
+
+    default:
+    h->next = NULL;
+    header_last->next = h;
+    break;
+    }
+
+  if (h->next == NULL) header_last = h;
+
+  /* Check for one of the known header types (From:, To:, etc.) though in
+  practice most added headers are going to be "other". Lower case
+  identification letters are never stored with the header; they are used
+  for existence tests when messages are received. So discard any lower case
+  flag values. */
+
+  h->type = header_checkname(h, FALSE);
+  if (h->type >= 'a') h->type = htype_other;
+
+  DEBUG(D_receive|D_acl) debug_printf("  %s", header_last->text);
+  }
+
+acl_added_headers = NULL;
+DEBUG(D_receive|D_acl) debug_printf(">>\n");
+}
+
+
+
+/*************************************************
+*       Add host information for log line        *
+*************************************************/
+
+/* Called for acceptance and rejecting log lines. This adds information about
+the calling host to a string that is being built dynamically.
+
+Arguments:
+  s           the dynamic string
+  sizeptr     points to the size variable
+  ptrptr      points to the pointer variable
+
+Returns:      the extended string
+*/
+
+static uschar *
+add_host_info_for_log(uschar *s, int *sizeptr, int *ptrptr)
+{
+if (sender_fullhost != NULL)
+  {
+  s = string_append(s, sizeptr, ptrptr, 2, US" H=", sender_fullhost);
+  if ((log_extra_selector & LX_incoming_interface) != 0 &&
+       interface_address != NULL)
+    {
+    uschar *ss = string_sprintf(" I=[%s]:%d", interface_address,
+      interface_port);
+    s = string_cat(s, sizeptr, ptrptr, ss, Ustrlen(ss));
+    }
+  }
+if (sender_ident != NULL)
+  s = string_append(s, sizeptr, ptrptr, 2, US" U=", sender_ident);
+if (received_protocol != NULL)
+  s = string_append(s, sizeptr, ptrptr, 2, US" P=", received_protocol);
+return s;
+}
+
+
+
+#ifdef WITH_CONTENT_SCAN
+
+/*************************************************
+*       Run the MIME ACL on a message            *
+*************************************************/
+
+/* This code is in a subroutine so that it can be used for both SMTP
+and non-SMTP messages. It is called with a non-NULL ACL pointer.
+
+Arguments:
+  acl                The ACL to run (acl_smtp_mime or acl_not_smtp_mime)
+  smtp_yield_ptr     Set FALSE to kill messages after dropped connection
+  smtp_reply_ptr     Where SMTP reply is being built
+  blackholed_by_ptr  Where "blackholed by" message is being built
+
+Returns:             TRUE to carry on; FALSE to abandon the message
+*/
+
+static BOOL
+run_mime_acl(uschar *acl, BOOL *smtp_yield_ptr, uschar **smtp_reply_ptr,
+  uschar **blackholed_by_ptr)
+{
+FILE *mbox_file;
+uschar rfc822_file_path[2048];
+unsigned long mbox_size;
+header_line *my_headerlist;
+uschar *user_msg, *log_msg;
+int mime_part_count_buffer = -1;
+int rc = OK;
+
+memset(CS rfc822_file_path,0,2048);
+
+/* check if it is a MIME message */
+my_headerlist = header_list;
+while (my_headerlist != NULL)
+  {
+  /* skip deleted headers */
+  if (my_headerlist->type == '*')
+    {
+    my_headerlist = my_headerlist->next;
+    continue;
+    }
+  if (strncmpic(my_headerlist->text, US"Content-Type:", 13) == 0)
+    {
+    DEBUG(D_receive) debug_printf("Found Content-Type: header - executing acl_smtp_mime.\n");
+    goto DO_MIME_ACL;
+    }
+  my_headerlist = my_headerlist->next;
+  }
+
+DEBUG(D_receive) debug_printf("No Content-Type: header - presumably not a MIME message.\n");
+return TRUE;
+
+DO_MIME_ACL:
+/* make sure the eml mbox file is spooled up */
+mbox_file = spool_mbox(&mbox_size, NULL);
+if (mbox_file == NULL) {
+  /* error while spooling */
+  log_write(0, LOG_MAIN|LOG_PANIC,
+         "acl_smtp_mime: error while creating mbox spool file, message temporarily rejected.");
+  Uunlink(spool_name);
+  unspool_mbox();
+#ifdef EXPERIMENTAL_DCC
+  dcc_ok = 0;
+#endif
+  smtp_respond(US"451", 3, TRUE, US"temporary local problem");
+  message_id[0] = 0;            /* Indicate no message accepted */
+  *smtp_reply_ptr = US"";       /* Indicate reply already sent */
+  return FALSE;                 /* Indicate skip to end of receive function */
+};
+
+mime_is_rfc822 = 0;
+
+MIME_ACL_CHECK:
+mime_part_count = -1;
+rc = mime_acl_check(acl, mbox_file, NULL, &user_msg, &log_msg);
+(void)fclose(mbox_file);
+
+if (Ustrlen(rfc822_file_path) > 0)
+  {
+  mime_part_count = mime_part_count_buffer;
+
+  if (unlink(CS rfc822_file_path) == -1)
+    {
+    log_write(0, LOG_PANIC,
+         "acl_smtp_mime: can't unlink RFC822 spool file, skipping.");
+      goto END_MIME_ACL;
+    }
+  }
+
+/* check if we must check any message/rfc822 attachments */
+if (rc == OK)
+  {
+  uschar temp_path[1024];
+  int n;
+  struct dirent *entry;
+  DIR *tempdir;
+
+  (void)string_format(temp_path, 1024, "%s/scan/%s", spool_directory,
+    message_id);
+
+  tempdir = opendir(CS temp_path);
+  n = 0;
+  do
+    {
+    entry = readdir(tempdir);
+    if (entry == NULL) break;
+    if (strncmpic(US entry->d_name,US"__rfc822_",9) == 0)
+      {
+      (void)string_format(rfc822_file_path, 2048,"%s/scan/%s/%s", spool_directory, message_id, entry->d_name);
+      debug_printf("RFC822 attachment detected: running MIME ACL for '%s'\n", rfc822_file_path);
+      break;
+      }
+    } while (1);
+  closedir(tempdir);
+
+  if (entry != NULL)
+    {
+    mbox_file = Ufopen(rfc822_file_path,"rb");
+    if (mbox_file == NULL)
+      {
+      log_write(0, LOG_PANIC,
+         "acl_smtp_mime: can't open RFC822 spool file, skipping.");
+      unlink(CS rfc822_file_path);
+      goto END_MIME_ACL;
+      }
+    /* set RFC822 expansion variable */
+    mime_is_rfc822 = 1;
+    mime_part_count_buffer = mime_part_count;
+    goto MIME_ACL_CHECK;
+    }
+  }
+
+END_MIME_ACL:
+add_acl_headers(ACL_WHERE_MIME, US"MIME");
+if (rc == DISCARD)
+  {
+  recipients_count = 0;
+  *blackholed_by_ptr = US"MIME ACL";
+  }
+else if (rc != OK)
+  {
+  Uunlink(spool_name);
+  unspool_mbox();
+#ifdef EXPERIMENTAL_DCC
+  dcc_ok = 0;
+#endif
+  if (smtp_input && smtp_handle_acl_fail(ACL_WHERE_MIME, rc, user_msg, log_msg) != 0) {
+    *smtp_yield_ptr = FALSE;    /* No more messsages after dropped connection */
+    *smtp_reply_ptr = US"";     /* Indicate reply already sent */
+  }
+  message_id[0] = 0;            /* Indicate no message accepted */
+  return FALSE;                 /* Cause skip to end of receive function */
+  }
+
+return TRUE;
+}
+
+#endif  /* WITH_CONTENT_SCAN */
+
+
+
+void
+received_header_gen(void)
+{
+uschar *received;
+uschar *timestamp;
+header_line *received_header= header_list;
+
+timestamp = expand_string(US"${tod_full}");
+if (recipients_count == 1) received_for = recipients_list[0].address;
+received = expand_string(received_header_text);
+received_for = NULL;
+
+if (received == NULL)
+  {
+  if(spool_name[0] != 0)
+    Uunlink(spool_name);           /* Lose the data file */
+  log_write(0, LOG_MAIN|LOG_PANIC_DIE, "Expansion of \"%s\" "
+    "(received_header_text) failed: %s", string_printing(received_header_text),
+      expand_string_message);
+  }
+
+/* The first element on the header chain is reserved for the Received header,
+so all we have to do is fill in the text pointer, and set the type. However, if
+the result of the expansion is an empty string, we leave the header marked as
+"old" so as to refrain from adding a Received header. */
+
+if (received[0] == 0)
+  {
+  received_header->text = string_sprintf("Received: ; %s\n", timestamp);
+  received_header->type = htype_old;
+  }
+else
+  {
+  received_header->text = string_sprintf("%s; %s\n", received, timestamp);
+  received_header->type = htype_received;
+  }
+
+received_header->slen = Ustrlen(received_header->text);
+
+DEBUG(D_receive) debug_printf(">>Generated Received: header line\n%c %s",
+  received_header->type, received_header->text);
+}
+
+
+
+/*************************************************
+*                 Receive message                *
+*************************************************/
+
+/* Receive a message on the given input, and put it into a pair of spool files.
+Either a non-null list of recipients, or the extract flag will be true, or
+both. The flag sender_local is true for locally generated messages. The flag
+submission_mode is true if an ACL has obeyed "control = submission". The flag
+suppress_local_fixups is true if an ACL has obeyed "control =
+suppress_local_fixups" or -G was passed on the command-line.
+The flag smtp_input is true if the message is to be
+handled using SMTP conventions about termination and lines starting with dots.
+For non-SMTP messages, dot_ends is true for dot-terminated messages.
+
+If a message was successfully read, message_id[0] will be non-zero.
+
+The general actions of this function are:
+
+  . Read the headers of the message (if any) into a chain of store
+    blocks.
+
+  . If there is a "sender:" header and the message is locally originated,
+    throw it away, unless the caller is trusted, or unless
+    active_local_sender_retain is set - which can only happen if
+    active_local_from_check is false.
+
+  . If recipients are to be extracted from the message, build the
+    recipients list from the headers, removing any that were on the
+    original recipients list (unless extract_addresses_remove_arguments is
+    false), and at the same time, remove any bcc header that may be present.
+
+  . Get the spool file for the data, sort out its unique name, open
+    and lock it (but don't give it the name yet).
+
+  . Generate a "Message-Id" header if the message doesn't have one, for
+    locally-originated messages.
+
+  . Generate a "Received" header.
+
+  . Ensure the recipients list is fully qualified and rewritten if necessary.
+
+  . If there are any rewriting rules, apply them to the sender address
+    and also to the headers.
+
+  . If there is no from: header, generate one, for locally-generated messages
+    and messages in "submission mode" only.
+
+  . If the sender is local, check that from: is correct, and if not, generate
+    a Sender: header, unless message comes from a trusted caller, or this
+    feature is disabled by active_local_from_check being false.
+
+  . If there is no "date" header, generate one, for locally-originated
+    or submission mode messages only.
+
+  . Copy the rest of the input, or up to a terminating "." if in SMTP or
+    dot_ends mode, to the data file. Leave it open, to hold the lock.
+
+  . Write the envelope and the headers to a new file.
+
+  . Set the name for the header file; close it.
+
+  . Set the name for the data file; close it.
+
+Because this function can potentially be called many times in a single
+SMTP connection, all store should be got by store_get(), so that it will be
+automatically retrieved after the message is accepted.
+
+FUDGE: It seems that sites on the net send out messages with just LF
+terminators, despite the warnings in the RFCs, and other MTAs handle this. So
+we make the CRs optional in all cases.
+
+July 2003: Bare CRs in messages, especially in header lines, cause trouble. A
+new regime is now in place in which bare CRs in header lines are turned into LF
+followed by a space, so as not to terminate the header line.
+
+February 2004: A bare LF in a header line in a message whose first line was
+terminated by CRLF is treated in the same way as a bare CR.
+
+Arguments:
+  extract_recip  TRUE if recipients are to be extracted from the message's
+                   headers
+
+Returns:  TRUE   there are more messages to be read (SMTP input)
+          FALSE  there are no more messages to be read (non-SMTP input
+                 or SMTP connection collapsed, or other failure)
+
+When reading a message for filter testing, the returned value indicates
+whether the headers (which is all that is read) were terminated by '.' or
+not. */
+
+BOOL
+receive_msg(BOOL extract_recip)
+{
+int  i;
+int  rc = FAIL;
+int  msg_size = 0;
+int  process_info_len = Ustrlen(process_info);
+int  error_rc = (error_handling == ERRORS_SENDER)?
+       errors_sender_rc : EXIT_FAILURE;
+int  header_size = 256;
+int  start, end, domain, size, sptr;
+int  id_resolution;
+int  had_zero = 0;
+int  prevlines_length = 0;
+
+register int ptr = 0;
+
+BOOL contains_resent_headers = FALSE;
+BOOL extracted_ignored = FALSE;
+BOOL first_line_ended_crlf = TRUE_UNSET;
+BOOL smtp_yield = TRUE;
+BOOL yield = FALSE;
+
+BOOL resents_exist = FALSE;
+uschar *resent_prefix = US"";
+uschar *blackholed_by = NULL;
+uschar *blackhole_log_msg = US"";
+enum {NOT_TRIED, TMP_REJ, PERM_REJ, ACCEPTED} cutthrough_done = NOT_TRIED;
+
+flock_t lock_data;
+error_block *bad_addresses = NULL;
+
+uschar *frozen_by = NULL;
+uschar *queued_by = NULL;
+
+uschar *errmsg, *s;
+struct stat statbuf;
+
+/* Final message to give to SMTP caller, and messages from ACLs */
+
+uschar *smtp_reply = NULL;
+uschar *user_msg, *log_msg;
+
+/* Working header pointers */
+
+header_line *h, *next;
+
+/* Flags for noting the existence of certain headers (only one left) */
+
+BOOL date_header_exists = FALSE;
+
+/* Pointers to receive the addresses of headers whose contents we need. */
+
+header_line *from_header = NULL;
+header_line *subject_header = NULL;
+header_line *msgid_header = NULL;
+header_line *received_header;
+
+#ifdef EXPERIMENTAL_DMARC
+int dmarc_up = 0;
+#endif /* EXPERIMENTAL_DMARC */
+
+/* Variables for use when building the Received: header. */
+
+uschar *timestamp;
+int tslen;
+
+/* Release any open files that might have been cached while preparing to
+accept the message - e.g. by verifying addresses - because reading a message
+might take a fair bit of real time. */
+
+search_tidyup();
+
+/* Extracting the recipient list from an input file is incompatible with
+cutthrough delivery with the no-spool option.  It shouldn't be possible
+to set up the combination, but just in case kill any ongoing connection. */
+if (extract_recip || !smtp_input)
+  cancel_cutthrough_connection("not smtp input");
+
+/* Initialize the chain of headers by setting up a place-holder for Received:
+header. Temporarily mark it as "old", i.e. not to be used. We keep header_last
+pointing to the end of the chain to make adding headers simple. */
+
+received_header = header_list = header_last = store_get(sizeof(header_line));
+header_list->next = NULL;
+header_list->type = htype_old;
+header_list->text = NULL;
+header_list->slen = 0;
+
+/* Control block for the next header to be read. */
+
+next = store_get(sizeof(header_line));
+next->text = store_get(header_size);
+
+/* Initialize message id to be null (indicating no message read), and the
+header names list to be the normal list. Indicate there is no data file open
+yet, initialize the size and warning count, and deal with no size limit. */
+
+message_id[0] = 0;
+data_file = NULL;
+data_fd = -1;
+spool_name[0] = 0;
+message_size = 0;
+warning_count = 0;
+received_count = 1;            /* For the one we will add */
+
+if (thismessage_size_limit <= 0) thismessage_size_limit = INT_MAX;
+
+/* While reading the message, the following counts are computed. */
+
+message_linecount = body_linecount = body_zerocount =
+  max_received_linelength = 0;
+
+#ifndef DISABLE_DKIM
+/* Call into DKIM to set up the context. */
+if (smtp_input && !smtp_batched_input && !dkim_disable_verify) dkim_exim_verify_init();
+#endif
+
+#ifdef EXPERIMENTAL_DMARC
+/* initialize libopendmarc */
+dmarc_up = dmarc_init();
+#endif
+
+/* Remember the time of reception. Exim uses time+pid for uniqueness of message
+ids, and fractions of a second are required. See the comments that precede the
+message id creation below. */
+
+(void)gettimeofday(&message_id_tv, NULL);
+
+/* For other uses of the received time we can operate with granularity of one
+second, and for that we use the global variable received_time. This is for
+things like ultimate message timeouts. */
+
+received_time = message_id_tv.tv_sec;
+
+/* If SMTP input, set the special handler for timeouts. The alarm() calls
+happen in the smtp_getc() function when it refills its buffer. */
+
+if (smtp_input) os_non_restarting_signal(SIGALRM, data_timeout_handler);
+
+/* If not SMTP input, timeout happens only if configured, and we just set a
+single timeout for the whole message. */
+
+else if (receive_timeout > 0)
+  {
+  os_non_restarting_signal(SIGALRM, data_timeout_handler);
+  alarm(receive_timeout);
+  }
+
+/* SIGTERM and SIGINT are caught always. */
+
+signal(SIGTERM, data_sigterm_sigint_handler);
+signal(SIGINT, data_sigterm_sigint_handler);
+
+/* Header lines in messages are not supposed to be very long, though when
+unfolded, to: and cc: headers can take up a lot of store. We must also cope
+with the possibility of junk being thrown at us. Start by getting 256 bytes for
+storing the header, and extend this as necessary using string_cat().
+
+To cope with total lunacies, impose an upper limit on the length of the header
+section of the message, as otherwise the store will fill up. We must also cope
+with the possibility of binary zeros in the data. Hence we cannot use fgets().
+Folded header lines are joined into one string, leaving the '\n' characters
+inside them, so that writing them out reproduces the input.
+
+Loop for each character of each header; the next structure for chaining the
+header is set up already, with ptr the offset of the next character in
+next->text. */
+
+for (;;)
+  {
+  int ch = (receive_getc)();
+
+  /* If we hit EOF on a SMTP connection, it's an error, since incoming
+  SMTP must have a correct "." terminator. */
+
+  if (ch == EOF && smtp_input /* && !smtp_batched_input */)
+    {
+    smtp_reply = handle_lost_connection(US" (header)");
+    smtp_yield = FALSE;
+    goto TIDYUP;                       /* Skip to end of function */
+    }
+
+  /* See if we are at the current header's size limit - there must be at least
+  four bytes left. This allows for the new character plus a zero, plus two for
+  extra insertions when we are playing games with dots and carriage returns. If
+  we are at the limit, extend the text buffer. This could have been done
+  automatically using string_cat() but because this is a tightish loop storing
+  only one character at a time, we choose to do it inline. Normally
+  store_extend() will be able to extend the block; only at the end of a big
+  store block will a copy be needed. To handle the case of very long headers
+  (and sometimes lunatic messages can have ones that are 100s of K long) we
+  call store_release() for strings that have been copied - if the string is at
+  the start of a block (and therefore the only thing in it, because we aren't
+  doing any other gets), the block gets freed. We can only do this because we
+  know there are no other calls to store_get() going on. */
+
+  if (ptr >= header_size - 4)
+    {
+    int oldsize = header_size;
+    /* header_size += 256; */
+    header_size *= 2;
+    if (!store_extend(next->text, oldsize, header_size))
+      {
+      uschar *newtext = store_get(header_size);
+      memcpy(newtext, next->text, ptr);
+      store_release(next->text);
+      next->text = newtext;
+      }
+    }
+
+  /* Cope with receiving a binary zero. There is dispute about whether
+  these should be allowed in RFC 822 messages. The middle view is that they
+  should not be allowed in headers, at least. Exim takes this attitude at
+  the moment. We can't just stomp on them here, because we don't know that
+  this line is a header yet. Set a flag to cause scanning later. */
+
+  if (ch == 0) had_zero++;
+
+  /* Test for termination. Lines in remote SMTP are terminated by CRLF, while
+  those from data files use just LF. Treat LF in local SMTP input as a
+  terminator too. Treat EOF as a line terminator always. */
+
+  if (ch == EOF) goto EOL;
+
+  /* FUDGE: There are sites out there that don't send CRs before their LFs, and
+  other MTAs accept this. We are therefore forced into this "liberalisation"
+  too, so we accept LF as a line terminator whatever the source of the message.
+  However, if the first line of the message ended with a CRLF, we treat a bare
+  LF specially by inserting a white space after it to ensure that the header
+  line is not terminated. */
+
+  if (ch == '\n')
+    {
+    if (first_line_ended_crlf == TRUE_UNSET) first_line_ended_crlf = FALSE;
+      else if (first_line_ended_crlf) receive_ungetc(' ');
+    goto EOL;
+    }
+
+  /* This is not the end of the line. If this is SMTP input and this is
+  the first character in the line and it is a "." character, ignore it.
+  This implements the dot-doubling rule, though header lines starting with
+  dots aren't exactly common. They are legal in RFC 822, though. If the
+  following is CRLF or LF, this is the line that that terminates the
+  entire message. We set message_ended to indicate this has happened (to
+  prevent further reading), and break out of the loop, having freed the
+  empty header, and set next = NULL to indicate no data line. */
+
+  if (ptr == 0 && ch == '.' && (smtp_input || dot_ends))
+    {
+    ch = (receive_getc)();
+    if (ch == '\r')
+      {
+      ch = (receive_getc)();
+      if (ch != '\n')
+        {
+        receive_ungetc(ch);
+        ch = '\r';              /* Revert to CR */
+        }
+      }
+    if (ch == '\n')
+      {
+      message_ended = END_DOT;
+      store_reset(next);
+      next = NULL;
+      break;                    /* End character-reading loop */
+      }
+
+    /* For non-SMTP input, the dot at the start of the line was really a data
+    character. What is now in ch is the following character. We guaranteed
+    enough space for this above. */
+
+    if (!smtp_input)
+      {
+      next->text[ptr++] = '.';
+      message_size++;
+      }
+    }
+
+  /* If CR is immediately followed by LF, end the line, ignoring the CR, and
+  remember this case if this is the first line ending. */
+
+  if (ch == '\r')
+    {
+    ch = (receive_getc)();
+    if (ch == '\n')
+      {
+      if (first_line_ended_crlf == TRUE_UNSET) first_line_ended_crlf = TRUE;
+      goto EOL;
+      }
+
+    /* Otherwise, put back the character after CR, and turn the bare CR
+    into LF SP. */
+
+    ch = (receive_ungetc)(ch);
+    next->text[ptr++] = '\n';
+    message_size++;
+    ch = ' ';
+    }
+
+  /* We have a data character for the header line. */
+
+  next->text[ptr++] = ch;    /* Add to buffer */
+  message_size++;            /* Total message size so far */
+
+  /* Handle failure due to a humungously long header section. The >= allows
+  for the terminating \n. Add what we have so far onto the headers list so
+  that it gets reflected in any error message, and back up the just-read
+  character. */
+
+  if (message_size >= header_maxsize)
+    {
+    next->text[ptr] = 0;
+    next->slen = ptr;
+    next->type = htype_other;
+    next->next = NULL;
+    header_last->next = next;
+    header_last = next;
+
+    log_write(0, LOG_MAIN, "ridiculously long message header received from "
+      "%s (more than %d characters): message abandoned",
+      sender_host_unknown? sender_ident : sender_fullhost, header_maxsize);
+
+    if (smtp_input)
+      {
+      smtp_reply = US"552 Message header is ridiculously long";
+      receive_swallow_smtp();
+      goto TIDYUP;                             /* Skip to end of function */
+      }
+
+    else
+      {
+      give_local_error(ERRMESS_VLONGHEADER,
+        string_sprintf("message header longer than %d characters received: "
+         "message not accepted", header_maxsize), US"", error_rc, stdin,
+           header_list->next);
+      /* Does not return */
+      }
+    }
+
+  continue;                  /* With next input character */
+
+  /* End of header line reached */
+
+  EOL:
+
+  /* Keep track of lines for BSMTP errors and overall message_linecount. */
+
+  receive_linecount++;
+  message_linecount++;
+
+  /* Keep track of maximum line length */
+
+  if (ptr - prevlines_length > max_received_linelength)
+    max_received_linelength = ptr - prevlines_length;
+  prevlines_length = ptr + 1;
+
+  /* Now put in the terminating newline. There is always space for
+  at least two more characters. */
+
+  next->text[ptr++] = '\n';
+  message_size++;
+
+  /* A blank line signals the end of the headers; release the unwanted
+  space and set next to NULL to indicate this. */
+
+  if (ptr == 1)
+    {
+    store_reset(next);
+    next = NULL;
+    break;
+    }
+
+  /* There is data in the line; see if the next input character is a
+  whitespace character. If it is, we have a continuation of this header line.
+  There is always space for at least one character at this point. */
+
+  if (ch != EOF)
+    {
+    int nextch = (receive_getc)();
+    if (nextch == ' ' || nextch == '\t')
+      {
+      next->text[ptr++] = nextch;
+      message_size++;
+      continue;                      /* Iterate the loop */
+      }
+    else if (nextch != EOF) (receive_ungetc)(nextch);   /* For next time */
+    else ch = EOF;                   /* Cause main loop to exit at end */
+    }
+
+  /* We have got to the real line end. Terminate the string and release store
+  beyond it. If it turns out to be a real header, internal binary zeros will
+  be squashed later. */
+
+  next->text[ptr] = 0;
+  next->slen = ptr;
+  store_reset(next->text + ptr + 1);
+
+  /* Check the running total size against the overall message size limit. We
+  don't expect to fail here, but if the overall limit is set less than MESSAGE_
+  MAXSIZE and a big header is sent, we want to catch it. Just stop reading
+  headers - the code to read the body will then also hit the buffer. */
+
+  if (message_size > thismessage_size_limit) break;
+
+  /* A line that is not syntactically correct for a header also marks
+  the end of the headers. In this case, we leave next containing the
+  first data line. This might actually be several lines because of the
+  continuation logic applied above, but that doesn't matter.
+
+  It turns out that smail, and presumably sendmail, accept leading lines
+  of the form
+
+  From ph10 Fri Jan  5 12:35 GMT 1996
+
+  in messages. The "mail" command on Solaris 2 sends such lines. I cannot
+  find any documentation of this, but for compatibility it had better be
+  accepted. Exim restricts it to the case of non-smtp messages, and
+  treats it as an alternative to the -f command line option. Thus it is
+  ignored except for trusted users or filter testing. Otherwise it is taken
+  as the sender address, unless -f was used (sendmail compatibility).
+
+  It further turns out that some UUCPs generate the From_line in a different
+  format, e.g.
+
+  From ph10 Fri, 7 Jan 97 14:00:00 GMT
+
+  The regex for matching these things is now capable of recognizing both
+  formats (including 2- and 4-digit years in the latter). In fact, the regex
+  is now configurable, as is the expansion string to fish out the sender.
+
+  Even further on it has been discovered that some broken clients send
+  these lines in SMTP messages. There is now an option to ignore them from
+  specified hosts or networks. Sigh. */
+
+  if (header_last == header_list &&
+       (!smtp_input
+         ||
+         (sender_host_address != NULL &&
+           verify_check_host(&ignore_fromline_hosts) == OK)
+         ||
+         (sender_host_address == NULL && ignore_fromline_local)
+       ) &&
+       regex_match_and_setup(regex_From, next->text, 0, -1))
+    {
+    if (!sender_address_forced)
+      {
+      uschar *uucp_sender = expand_string(uucp_from_sender);
+      if (uucp_sender == NULL)
+        {
+        log_write(0, LOG_MAIN|LOG_PANIC,
+          "expansion of \"%s\" failed after matching "
+          "\"From \" line: %s", uucp_from_sender, expand_string_message);
+        }
+      else
+        {
+        int start, end, domain;
+        uschar *errmess;
+        uschar *newsender = parse_extract_address(uucp_sender, &errmess,
+          &start, &end, &domain, TRUE);
+        if (newsender != NULL)
+          {
+          if (domain == 0 && newsender[0] != 0)
+            newsender = rewrite_address_qualify(newsender, FALSE);
+
+          if (filter_test != FTEST_NONE || receive_check_set_sender(newsender))
+            {
+            sender_address = newsender;
+
+            if (trusted_caller || filter_test != FTEST_NONE)
+              {
+              authenticated_sender = NULL;
+              originator_name = US"";
+              sender_local = FALSE;
+              }
+
+            if (filter_test != FTEST_NONE)
+              printf("Sender taken from \"From \" line\n");
+            }
+          }
+        }
+      }
+    }
+
+  /* Not a leading "From " line. Check to see if it is a valid header line.
+  Header names may contain any non-control characters except space and colon,
+  amazingly. */
+
+  else
+    {
+    uschar *p = next->text;
+
+    /* If not a valid header line, break from the header reading loop, leaving
+    next != NULL, indicating that it holds the first line of the body. */
+
+    if (isspace(*p)) break;
+    while (mac_isgraph(*p) && *p != ':') p++;
+    while (isspace(*p)) p++;
+    if (*p != ':')
+      {
+      body_zerocount = had_zero;
+      break;
+      }
+
+    /* We have a valid header line. If there were any binary zeroes in
+    the line, stomp on them here. */
+
+    if (had_zero > 0)
+      for (p = next->text; p < next->text + ptr; p++) if (*p == 0) *p = '?';
+
+    /* It is perfectly legal to have an empty continuation line
+    at the end of a header, but it is confusing to humans
+    looking at such messages, since it looks like a blank line.
+    Reduce confusion by removing redundant white space at the
+    end. We know that there is at least one printing character
+    (the ':' tested for above) so there is no danger of running
+    off the end. */
+
+    p = next->text + ptr - 2;
+    for (;;)
+      {
+      while (*p == ' ' || *p == '\t') p--;
+      if (*p != '\n') break;
+      ptr = (p--) - next->text + 1;
+      message_size -= next->slen - ptr;
+      next->text[ptr] = 0;
+      next->slen = ptr;
+      }
+
+    /* Add the header to the chain */
+
+    next->type = htype_other;
+    next->next = NULL;
+    header_last->next = next;
+    header_last = next;
+
+    /* Check the limit for individual line lengths. This comes after adding to
+    the chain so that the failing line is reflected if a bounce is generated
+    (for a local message). */
+
+    if (header_line_maxsize > 0 && next->slen > header_line_maxsize)
+      {
+      log_write(0, LOG_MAIN, "overlong message header line received from "
+        "%s (more than %d characters): message abandoned",
+        sender_host_unknown? sender_ident : sender_fullhost,
+        header_line_maxsize);
+
+      if (smtp_input)
+        {
+        smtp_reply = US"552 A message header line is too long";
+        receive_swallow_smtp();
+        goto TIDYUP;                             /* Skip to end of function */
+        }
+
+      else
+        {
+        give_local_error(ERRMESS_VLONGHDRLINE,
+          string_sprintf("message header line longer than %d characters "
+           "received: message not accepted", header_line_maxsize), US"",
+           error_rc, stdin, header_list->next);
+        /* Does not return */
+        }
+      }
+
+    /* Note if any resent- fields exist. */
+
+    if (!resents_exist && strncmpic(next->text, US"resent-", 7) == 0)
+      {
+      resents_exist = TRUE;
+      resent_prefix = US"Resent-";
+      }
+    }
+
+  /* The line has been handled. If we have hit EOF, break out of the loop,
+  indicating no pending data line. */
+
+  if (ch == EOF) { next = NULL; break; }
+
+  /* Set up for the next header */
+
+  header_size = 256;
+  next = store_get(sizeof(header_line));
+  next->text = store_get(header_size);
+  ptr = 0;
+  had_zero = 0;
+  prevlines_length = 0;
+  }      /* Continue, starting to read the next header */
+
+/* At this point, we have read all the headers into a data structure in main
+store. The first header is still the dummy placeholder for the Received: header
+we are going to generate a bit later on. If next != NULL, it contains the first
+data line - which terminated the headers before reaching a blank line (not the
+normal case). */
+
+DEBUG(D_receive)
+  {
+  debug_printf(">>Headers received:\n");
+  for (h = header_list->next; h != NULL; h = h->next)
+    debug_printf("%s", h->text);
+  debug_printf("\n");
+  }
+
+/* End of file on any SMTP connection is an error. If an incoming SMTP call
+is dropped immediately after valid headers, the next thing we will see is EOF.
+We must test for this specially, as further down the reading of the data is
+skipped if already at EOF. */
+
+if (smtp_input && (receive_feof)())
+  {
+  smtp_reply = handle_lost_connection(US" (after header)");
+  smtp_yield = FALSE;
+  goto TIDYUP;                       /* Skip to end of function */
+  }
+
+/* If this is a filter test run and no headers were read, output a warning
+in case there is a mistake in the test message. */
+
+if (filter_test != FTEST_NONE && header_list->next == NULL)
+  printf("Warning: no message headers read\n");
+
+
+/* Scan the headers to identify them. Some are merely marked for later
+processing; some are dealt with here. */
+
+for (h = header_list->next; h != NULL; h = h->next)
+  {
+  BOOL is_resent = strncmpic(h->text, US"resent-", 7) == 0;
+  if (is_resent) contains_resent_headers = TRUE;
+
+  switch (header_checkname(h, is_resent))
+    {
+    case htype_bcc:
+    h->type = htype_bcc;        /* Both Bcc: and Resent-Bcc: */
+    break;
+
+    case htype_cc:
+    h->type = htype_cc;         /* Both Cc: and Resent-Cc: */
+    break;
+
+    /* Record whether a Date: or Resent-Date: header exists, as appropriate. */
+
+    case htype_date:
+    if (!resents_exist || is_resent) date_header_exists = TRUE;
+    break;
+
+    /* Same comments as about Return-Path: below. */
+
+    case htype_delivery_date:
+    if (delivery_date_remove) h->type = htype_old;
+    break;
+
+    /* Same comments as about Return-Path: below. */
+
+    case htype_envelope_to:
+    if (envelope_to_remove) h->type = htype_old;
+    break;
+
+    /* Mark all "From:" headers so they get rewritten. Save the one that is to
+    be used for Sender: checking. For Sendmail compatibility, if the "From:"
+    header consists of just the login id of the user who called Exim, rewrite
+    it with the gecos field first. Apply this rule to Resent-From: if there
+    are resent- fields. */
+
+    case htype_from:
+    h->type = htype_from;
+    if (!resents_exist || is_resent)
+      {
+      from_header = h;
+      if (!smtp_input)
+        {
+        int len;
+        uschar *s = Ustrchr(h->text, ':') + 1;
+        while (isspace(*s)) s++;
+        len = h->slen - (s - h->text) - 1;
+        if (Ustrlen(originator_login) == len &&
+           strncmpic(s, originator_login, len) == 0)
+          {
+          uschar *name = is_resent? US"Resent-From" : US"From";
+          header_add(htype_from, "%s: %s <%s@%s>\n", name, originator_name,
+            originator_login, qualify_domain_sender);
+          from_header = header_last;
+          h->type = htype_old;
+          DEBUG(D_receive|D_rewrite)
+            debug_printf("rewrote \"%s:\" header using gecos\n", name);
+         }
+        }
+      }
+    break;
+
+    /* Identify the Message-id: header for generating "in-reply-to" in the
+    autoreply transport. For incoming logging, save any resent- value. In both
+    cases, take just the first of any multiples. */
+
+    case htype_id:
+    if (msgid_header == NULL && (!resents_exist || is_resent))
+      {
+      msgid_header = h;
+      h->type = htype_id;
+      }
+    break;
+
+    /* Flag all Received: headers */
+
+    case htype_received:
+    h->type = htype_received;
+    received_count++;
+    break;
+
+    /* "Reply-to:" is just noted (there is no resent-reply-to field) */
+
+    case htype_reply_to:
+    h->type = htype_reply_to;
+    break;
+
+    /* The Return-path: header is supposed to be added to messages when
+    they leave the SMTP system. We shouldn't receive messages that already
+    contain Return-path. However, since Exim generates Return-path: on
+    local delivery, resent messages may well contain it. We therefore
+    provide an option (which defaults on) to remove any Return-path: headers
+    on input. Removal actually means flagging as "old", which prevents the
+    header being transmitted with the message. */
+
+    case htype_return_path:
+    if (return_path_remove) h->type = htype_old;
+
+    /* If we are testing a mail filter file, use the value of the
+    Return-Path: header to set up the return_path variable, which is not
+    otherwise set. However, remove any <> that surround the address
+    because the variable doesn't have these. */
+
+    if (filter_test != FTEST_NONE)
+      {
+      uschar *start = h->text + 12;
+      uschar *end = start + Ustrlen(start);
+      while (isspace(*start)) start++;
+      while (end > start && isspace(end[-1])) end--;
+      if (*start == '<' && end[-1] == '>')
+        {
+        start++;
+        end--;
+        }
+      return_path = string_copyn(start, end - start);
+      printf("Return-path taken from \"Return-path:\" header line\n");
+      }
+    break;
+
+    /* If there is a "Sender:" header and the message is locally originated,
+    and from an untrusted caller and suppress_local_fixups is not set, or if we
+    are in submission mode for a remote message, mark it "old" so that it will
+    not be transmitted with the message, unless active_local_sender_retain is
+    set. (This can only be true if active_local_from_check is false.) If there
+    are any resent- headers in the message, apply this rule to Resent-Sender:
+    instead of Sender:. Messages with multiple resent- header sets cannot be
+    tidily handled. (For this reason, at least one MUA - Pine - turns old
+    resent- headers into X-resent- headers when resending, leaving just one
+    set.) */
+
+    case htype_sender:
+    h->type = ((!active_local_sender_retain &&
+                (
+                (sender_local && !trusted_caller && !suppress_local_fixups)
+                  || submission_mode
+                )
+               ) &&
+               (!resents_exist||is_resent))?
+      htype_old : htype_sender;
+    break;
+
+    /* Remember the Subject: header for logging. There is no Resent-Subject */
+
+    case htype_subject:
+    subject_header = h;
+    break;
+
+    /* "To:" gets flagged, and the existence of a recipient header is noted,
+    whether it's resent- or not. */
+
+    case htype_to:
+    h->type = htype_to;
+    /****
+    to_or_cc_header_exists = TRUE;
+    ****/
+    break;
+    }
+  }
+
+/* Extract recipients from the headers if that is required (the -t option).
+Note that this is documented as being done *before* any address rewriting takes
+place. There are two possibilities:
+
+(1) According to sendmail documentation for Solaris, IRIX, and HP-UX, any
+recipients already listed are to be REMOVED from the message. Smail 3 works
+like this. We need to build a non-recipients tree for that list, because in
+subsequent processing this data is held in a tree and that's what the
+spool_write_header() function expects. Make sure that non-recipient addresses
+are fully qualified and rewritten if necessary.
+
+(2) According to other sendmail documentation, -t ADDS extracted recipients to
+those in the command line arguments (and it is rumoured some other MTAs do
+this). Therefore, there is an option to make Exim behave this way.
+
+*** Notes on "Resent-" header lines ***
+
+The presence of resent-headers in the message makes -t horribly ambiguous.
+Experiments with sendmail showed that it uses recipients for all resent-
+headers, totally ignoring the concept of "sets of resent- headers" as described
+in RFC 2822 section 3.6.6. Sendmail also amalgamates them into a single set
+with all the addresses in one instance of each header.
+
+This seems to me not to be at all sensible. Before release 4.20, Exim 4 gave an
+error for -t if there were resent- headers in the message. However, after a
+discussion on the mailing list, I've learned that there are MUAs that use
+resent- headers with -t, and also that the stuff about sets of resent- headers
+and their ordering in RFC 2822 is generally ignored. An MUA that submits a
+message with -t and resent- header lines makes sure that only *its* resent-
+headers are present; previous ones are often renamed as X-resent- for example.
+
+Consequently, Exim has been changed so that, if any resent- header lines are
+present, the recipients are taken from all of the appropriate resent- lines,
+and not from the ordinary To:, Cc:, etc. */
+
+if (extract_recip)
+  {
+  int rcount = 0;
+  error_block **bnext = &bad_addresses;
+
+  if (extract_addresses_remove_arguments)
+    {
+    while (recipients_count-- > 0)
+      {
+      uschar *s = rewrite_address(recipients_list[recipients_count].address,
+        TRUE, TRUE, global_rewrite_rules, rewrite_existflags);
+      tree_add_nonrecipient(s);
+      }
+    recipients_list = NULL;
+    recipients_count = recipients_list_max = 0;
+    }
+
+  /* Now scan the headers */
+
+  for (h = header_list->next; h != NULL; h = h->next)
+    {
+    if ((h->type == htype_to || h->type == htype_cc || h->type == htype_bcc) &&
+        (!contains_resent_headers || strncmpic(h->text, US"resent-", 7) == 0))
+      {
+      uschar *s = Ustrchr(h->text, ':') + 1;
+      while (isspace(*s)) s++;
+
+      parse_allow_group = TRUE;          /* Allow address group syntax */
+
+      while (*s != 0)
+        {
+        uschar *ss = parse_find_address_end(s, FALSE);
+        uschar *recipient, *errmess, *p, *pp;
+        int start, end, domain;
+
+        /* Check on maximum */
+
+        if (recipients_max > 0 && ++rcount > recipients_max)
+          {
+          give_local_error(ERRMESS_TOOMANYRECIP, US"too many recipients",
+            US"message rejected: ", error_rc, stdin, NULL);
+          /* Does not return */
+          }
+
+        /* Make a copy of the address, and remove any internal newlines. These
+        may be present as a result of continuations of the header line. The
+        white space that follows the newline must not be removed - it is part
+        of the header. */
+
+        pp = recipient = store_get(ss - s + 1);
+        for (p = s; p < ss; p++) if (*p != '\n') *pp++ = *p;
+        *pp = 0;
+        recipient = parse_extract_address(recipient, &errmess, &start, &end,
+          &domain, FALSE);
+
+        /* Keep a list of all the bad addresses so we can send a single
+        error message at the end. However, an empty address is not an error;
+        just ignore it. This can come from an empty group list like
+
+          To: Recipients of list:;
+
+        If there are no recipients at all, an error will occur later. */
+
+        if (recipient == NULL && Ustrcmp(errmess, "empty address") != 0)
+          {
+          int len = Ustrlen(s);
+          error_block *b = store_get(sizeof(error_block));
+          while (len > 0 && isspace(s[len-1])) len--;
+          b->next = NULL;
+          b->text1 = string_printing(string_copyn(s, len));
+          b->text2 = errmess;
+          *bnext = b;
+          bnext = &(b->next);
+          }
+
+        /* If the recipient is already in the nonrecipients tree, it must
+        have appeared on the command line with the option extract_addresses_
+        remove_arguments set. Do not add it to the recipients, and keep a note
+        that this has happened, in order to give a better error if there are
+        no recipients left. */
+
+        else if (recipient != NULL)
+          {
+          if (tree_search(tree_nonrecipients, recipient) == NULL)
+            receive_add_recipient(recipient, -1);
+          else
+            extracted_ignored = TRUE;
+          }
+
+        /* Move on past this address */
+
+        s = ss + (*ss? 1:0);
+        while (isspace(*s)) s++;
+        }    /* Next address */
+
+      parse_allow_group = FALSE;      /* Reset group syntax flags */
+      parse_found_group = FALSE;
+
+      /* If this was the bcc: header, mark it "old", which means it
+      will be kept on the spool, but not transmitted as part of the
+      message. */
+
+      if (h->type == htype_bcc) h->type = htype_old;
+      }   /* For appropriate header line */
+    }     /* For each header line */
+
+  }
+
+/* Now build the unique message id. This has changed several times over the
+lifetime of Exim. This description was rewritten for Exim 4.14 (February 2003).
+Retaining all the history in the comment has become too unwieldy - read
+previous release sources if you want it.
+
+The message ID has 3 parts: tttttt-pppppp-ss. Each part is a number in base 62.
+The first part is the current time, in seconds. The second part is the current
+pid. Both are large enough to hold 32-bit numbers in base 62. The third part
+can hold a number in the range 0-3843. It used to be a computed sequence
+number, but is now the fractional component of the current time in units of
+1/2000 of a second (i.e. a value in the range 0-1999). After a message has been
+received, Exim ensures that the timer has ticked at the appropriate level
+before proceeding, to avoid duplication if the pid happened to be re-used
+within the same time period. It seems likely that most messages will take at
+least half a millisecond to be received, so no delay will normally be
+necessary. At least for some time...
+
+There is a modification when localhost_number is set. Formerly this was allowed
+to be as large as 255. Now it is restricted to the range 0-16, and the final
+component of the message id becomes (localhost_number * 200) + fractional time
+in units of 1/200 of a second (i.e. a value in the range 0-3399).
+
+Some not-really-Unix operating systems use case-insensitive file names (Darwin,
+Cygwin). For these, we have to use base 36 instead of base 62. Luckily, this
+still allows the tttttt field to hold a large enough number to last for some
+more decades, and the final two-digit field can hold numbers up to 1295, which
+is enough for milliseconds (instead of 1/2000 of a second).
+
+However, the pppppp field cannot hold a 32-bit pid, but it can hold a 31-bit
+pid, so it is probably safe because pids have to be positive. The
+localhost_number is restricted to 0-10 for these hosts, and when it is set, the
+final field becomes (localhost_number * 100) + fractional time in centiseconds.
+
+Note that string_base62() returns its data in a static storage block, so it
+must be copied before calling string_base62() again. It always returns exactly
+6 characters.
+
+There doesn't seem to be anything in the RFC which requires a message id to
+start with a letter, but Smail was changed to ensure this. The external form of
+the message id (as supplied by string expansion) therefore starts with an
+additional leading 'E'. The spool file names do not include this leading
+letter and it is not used internally.
+
+NOTE: If ever the format of message ids is changed, the regular expression for
+checking that a string is in this format must be updated in a corresponding
+way. It appears in the initializing code in exim.c. The macro MESSAGE_ID_LENGTH
+must also be changed to reflect the correct string length. Then, of course,
+other programs that rely on the message id format will need updating too. */
+
+Ustrncpy(message_id, string_base62((long int)(message_id_tv.tv_sec)), 6);
+message_id[6] = '-';
+Ustrncpy(message_id + 7, string_base62((long int)getpid()), 6);
+
+/* Deal with the case where the host number is set. The value of the number was
+checked when it was read, to ensure it isn't too big. The timing granularity is
+left in id_resolution so that an appropriate wait can be done after receiving
+the message, if necessary (we hope it won't be). */
+
+if (host_number_string != NULL)
+  {
+  id_resolution = (BASE_62 == 62)? 5000 : 10000;
+  sprintf(CS(message_id + MESSAGE_ID_LENGTH - 3), "-%2s",
+    string_base62((long int)(
+      host_number * (1000000/id_resolution) +
+        message_id_tv.tv_usec/id_resolution)) + 4);
+  }
+
+/* Host number not set: final field is just the fractional time at an
+appropriate resolution. */
+
+else
+  {
+  id_resolution = (BASE_62 == 62)? 500 : 1000;
+  sprintf(CS(message_id + MESSAGE_ID_LENGTH - 3), "-%2s",
+    string_base62((long int)(message_id_tv.tv_usec/id_resolution)) + 4);
+  }
+
+/* Add the current message id onto the current process info string if
+it will fit. */
+
+(void)string_format(process_info + process_info_len,
+  PROCESS_INFO_SIZE - process_info_len, " id=%s", message_id);
+
+/* If we are using multiple input directories, set up the one for this message
+to be the least significant base-62 digit of the time of arrival. Otherwise
+ensure that it is an empty string. */
+
+message_subdir[0] = split_spool_directory? message_id[5] : 0;
+
+/* Now that we have the message-id, if there is no message-id: header, generate
+one, but only for local (without suppress_local_fixups) or submission mode
+messages. This can be user-configured if required, but we had better flatten
+any illegal characters therein. */
+
+if (msgid_header == NULL &&
+      ((sender_host_address == NULL && !suppress_local_fixups)
+        || submission_mode))
+  {
+  uschar *p;
+  uschar *id_text = US"";
+  uschar *id_domain = primary_hostname;
+
+  /* Permit only letters, digits, dots, and hyphens in the domain */
+
+  if (message_id_domain != NULL)
+    {
+    uschar *new_id_domain = expand_string(message_id_domain);
+    if (new_id_domain == NULL)
+      {
+      if (!expand_string_forcedfail)
+        log_write(0, LOG_MAIN|LOG_PANIC,
+          "expansion of \"%s\" (message_id_header_domain) "
+          "failed: %s", message_id_domain, expand_string_message);
+      }
+    else if (*new_id_domain != 0)
+      {
+      id_domain = new_id_domain;
+      for (p = id_domain; *p != 0; p++)
+        if (!isalnum(*p) && *p != '.') *p = '-';  /* No need to test '-' ! */
+      }
+    }
+
+  /* Permit all characters except controls and RFC 2822 specials in the
+  additional text part. */
+
+  if (message_id_text != NULL)
+    {
+    uschar *new_id_text = expand_string(message_id_text);
+    if (new_id_text == NULL)
+      {
+      if (!expand_string_forcedfail)
+        log_write(0, LOG_MAIN|LOG_PANIC,
+          "expansion of \"%s\" (message_id_header_text) "
+          "failed: %s", message_id_text, expand_string_message);
+      }
+    else if (*new_id_text != 0)
+      {
+      id_text = new_id_text;
+      for (p = id_text; *p != 0; p++)
+        if (mac_iscntrl_or_special(*p)) *p = '-';
+      }
+    }
+
+  /* Add the header line
+   * Resent-* headers are prepended, per RFC 5322 3.6.6.  Non-Resent-* are
+   * appended, to preserve classical expectations of header ordering. */
+
+  header_add_at_position(!resents_exist, NULL, FALSE, htype_id,
+    "%sMessage-Id: <%s%s%s@%s>\n", resent_prefix, message_id_external,
+    (*id_text == 0)? "" : ".", id_text, id_domain);
+  }
+
+/* If we are to log recipients, keep a copy of the raw ones before any possible
+rewriting. Must copy the count, because later ACLs and the local_scan()
+function may mess with the real recipients. */
+
+if ((log_extra_selector & LX_received_recipients) != 0)
+  {
+  raw_recipients = store_get(recipients_count * sizeof(uschar *));
+  for (i = 0; i < recipients_count; i++)
+    raw_recipients[i] = string_copy(recipients_list[i].address);
+  raw_recipients_count = recipients_count;
+  }
+
+/* Ensure the recipients list is fully qualified and rewritten. Unqualified
+recipients will get here only if the conditions were right (allow_unqualified_
+recipient is TRUE). */
+
+for (i = 0; i < recipients_count; i++)
+  recipients_list[i].address =
+    rewrite_address(recipients_list[i].address, TRUE, TRUE,
+      global_rewrite_rules, rewrite_existflags);
+
+/* If there is no From: header, generate one for local (without
+suppress_local_fixups) or submission_mode messages. If there is no sender
+address, but the sender is local or this is a local delivery error, use the
+originator login. This shouldn't happen for genuine bounces, but might happen
+for autoreplies. The addition of From: must be done *before* checking for the
+possible addition of a Sender: header, because untrusted_set_sender allows an
+untrusted user to set anything in the envelope (which might then get info
+From:) but we still want to ensure a valid Sender: if it is required. */
+
+if (from_header == NULL &&
+    ((sender_host_address == NULL && !suppress_local_fixups)
+      || submission_mode))
+  {
+  uschar *oname = US"";
+
+  /* Use the originator_name if this is a locally submitted message and the
+  caller is not trusted. For trusted callers, use it only if -F was used to
+  force its value or if we have a non-SMTP message for which -f was not used
+  to set the sender. */
+
+  if (sender_host_address == NULL)
+    {
+    if (!trusted_caller || sender_name_forced ||
+         (!smtp_input && !sender_address_forced))
+      oname = originator_name;
+    }
+
+  /* For non-locally submitted messages, the only time we use the originator
+  name is when it was forced by the /name= option on control=submission. */
+
+  else
+    {
+    if (submission_name != NULL) oname = submission_name;
+    }
+
+  /* Envelope sender is empty */
+
+  if (sender_address[0] == 0)
+    {
+    uschar *fromstart, *fromend;
+
+    fromstart = string_sprintf("%sFrom: %s%s", resent_prefix,
+      oname, (oname[0] == 0)? "" : " <");
+    fromend = (oname[0] == 0)? US"" : US">";
+
+    if (sender_local || local_error_message)
+      {
+      header_add(htype_from, "%s%s@%s%s\n", fromstart,
+        local_part_quote(originator_login), qualify_domain_sender,
+        fromend);
+      }
+    else if (submission_mode && authenticated_id != NULL)
+      {
+      if (submission_domain == NULL)
+        {
+        header_add(htype_from, "%s%s@%s%s\n", fromstart,
+          local_part_quote(authenticated_id), qualify_domain_sender,
+          fromend);
+        }
+      else if (submission_domain[0] == 0)  /* empty => whole address set */
+        {
+        header_add(htype_from, "%s%s%s\n", fromstart, authenticated_id,
+          fromend);
+        }
+      else
+        {
+        header_add(htype_from, "%s%s@%s%s\n", fromstart,
+          local_part_quote(authenticated_id), submission_domain,
+          fromend);
+        }
+      from_header = header_last;    /* To get it checked for Sender: */
+      }
+    }
+
+  /* There is a non-null envelope sender. Build the header using the original
+  sender address, before any rewriting that might have been done while
+  verifying it. */
+
+  else
+    {
+    header_add(htype_from, "%sFrom: %s%s%s%s\n", resent_prefix,
+      oname,
+      (oname[0] == 0)? "" : " <",
+      (sender_address_unrewritten == NULL)?
+        sender_address : sender_address_unrewritten,
+      (oname[0] == 0)? "" : ">");
+
+    from_header = header_last;    /* To get it checked for Sender: */
+    }
+  }
+
+
+/* If the sender is local (without suppress_local_fixups), or if we are in
+submission mode and there is an authenticated_id, check that an existing From:
+is correct, and if not, generate a Sender: header, unless disabled. Any
+previously-existing Sender: header was removed above. Note that sender_local,
+as well as being TRUE if the caller of exim is not trusted, is also true if a
+trusted caller did not supply a -f argument for non-smtp input. To allow
+trusted callers to forge From: without supplying -f, we have to test explicitly
+here. If the From: header contains more than one address, then the call to
+parse_extract_address fails, and a Sender: header is inserted, as required. */
+
+if (from_header != NULL &&
+     (active_local_from_check &&
+       ((sender_local && !trusted_caller && !suppress_local_fixups) ||
+        (submission_mode && authenticated_id != NULL))
+     ))
+  {
+  BOOL make_sender = TRUE;
+  int start, end, domain;
+  uschar *errmess;
+  uschar *from_address =
+    parse_extract_address(Ustrchr(from_header->text, ':') + 1, &errmess,
+      &start, &end, &domain, FALSE);
+  uschar *generated_sender_address;
+
+  if (submission_mode)
+    {
+    if (submission_domain == NULL)
+      {
+      generated_sender_address = string_sprintf("%s@%s",
+        local_part_quote(authenticated_id), qualify_domain_sender);
+      }
+    else if (submission_domain[0] == 0)  /* empty => full address */
+      {
+      generated_sender_address = string_sprintf("%s",
+        authenticated_id);
+      }
+    else
+      {
+      generated_sender_address = string_sprintf("%s@%s",
+        local_part_quote(authenticated_id), submission_domain);
+      }
+    }
+  else
+    generated_sender_address = string_sprintf("%s@%s",
+      local_part_quote(originator_login), qualify_domain_sender);
+
+  /* Remove permitted prefixes and suffixes from the local part of the From:
+  address before doing the comparison with the generated sender. */
+
+  if (from_address != NULL)
+    {
+    int slen;
+    uschar *at = (domain == 0)? NULL : from_address + domain - 1;
+
+    if (at != NULL) *at = 0;
+    from_address += route_check_prefix(from_address, local_from_prefix);
+    slen = route_check_suffix(from_address, local_from_suffix);
+    if (slen > 0)
+      {
+      memmove(from_address+slen, from_address, Ustrlen(from_address)-slen);
+      from_address += slen;
+      }
+    if (at != NULL) *at = '@';
+
+    if (strcmpic(generated_sender_address, from_address) == 0 ||
+      (domain == 0 && strcmpic(from_address, originator_login) == 0))
+        make_sender = FALSE;
+    }
+
+  /* We have to cause the Sender header to be rewritten if there are
+  appropriate rewriting rules. */
+
+  if (make_sender)
+    {
+    if (submission_mode && submission_name == NULL)
+      header_add(htype_sender, "%sSender: %s\n", resent_prefix,
+        generated_sender_address);
+    else
+      header_add(htype_sender, "%sSender: %s <%s>\n",
+        resent_prefix,
+        submission_mode? submission_name : originator_name,
+        generated_sender_address);
+    }
+
+  /* Ensure that a non-null envelope sender address corresponds to the
+  submission mode sender address. */
+
+  if (submission_mode && sender_address[0] != 0)
+    {
+    if (sender_address_unrewritten == NULL)
+      sender_address_unrewritten = sender_address;
+    sender_address = generated_sender_address;
+    if (Ustrcmp(sender_address_unrewritten, generated_sender_address) != 0)
+      log_write(L_address_rewrite, LOG_MAIN,
+        "\"%s\" from env-from rewritten as \"%s\" by submission mode",
+        sender_address_unrewritten, generated_sender_address);
+    }
+  }
+
+/* If there are any rewriting rules, apply them to the sender address, unless
+it has already been rewritten as part of verification for SMTP input. */
+
+if (global_rewrite_rules != NULL && sender_address_unrewritten == NULL &&
+    sender_address[0] != 0)
+  {
+  sender_address = rewrite_address(sender_address, FALSE, TRUE,
+    global_rewrite_rules, rewrite_existflags);
+  DEBUG(D_receive|D_rewrite)
+    debug_printf("rewritten sender = %s\n", sender_address);
+  }
+
+
+/* The headers must be run through rewrite_header(), because it ensures that
+addresses are fully qualified, as well as applying any rewriting rules that may
+exist.
+
+Qualification of header addresses in a message from a remote host happens only
+if the host is in sender_unqualified_hosts or recipient_unqualified hosts, as
+appropriate. For local messages, qualification always happens, unless -bnq is
+used to explicitly suppress it. No rewriting is done for an unqualified address
+that is left untouched.
+
+We start at the second header, skipping our own Received:. This rewriting is
+documented as happening *after* recipient addresses are taken from the headers
+by the -t command line option. An added Sender: gets rewritten here. */
+
+for (h = header_list->next; h != NULL; h = h->next)
+  {
+  header_line *newh = rewrite_header(h, NULL, NULL, global_rewrite_rules,
+    rewrite_existflags, TRUE);
+  if (newh != NULL) h = newh;
+  }
+
+
+/* An RFC 822 (sic) message is not legal unless it has at least one of "to",
+"cc", or "bcc". Note that although the minimal examples in RFC 822 show just
+"to" or "bcc", the full syntax spec allows "cc" as well. If any resent- header
+exists, this applies to the set of resent- headers rather than the normal set.
+
+The requirement for a recipient header has been removed in RFC 2822. At this
+point in the code, earlier versions of Exim added a To: header for locally
+submitted messages, and an empty Bcc: header for others. In the light of the
+changes in RFC 2822, this was dropped in November 2003. */
+
+
+/* If there is no date header, generate one if the message originates locally
+(i.e. not over TCP/IP) and suppress_local_fixups is not set, or if the
+submission mode flag is set. Messages without Date: are not valid, but it seems
+to be more confusing if Exim adds one to all remotely-originated messages.
+As per Message-Id, we prepend if resending, else append.
+*/
+
+if (!date_header_exists &&
+      ((sender_host_address == NULL && !suppress_local_fixups)
+        || submission_mode))
+  header_add_at_position(!resents_exist, NULL, FALSE, htype_other,
+    "%sDate: %s\n", resent_prefix, tod_stamp(tod_full));
+
+search_tidyup();    /* Free any cached resources */
+
+/* Show the complete set of headers if debugging. Note that the first one (the
+new Received:) has not yet been set. */
+
+DEBUG(D_receive)
+  {
+  debug_printf(">>Headers after rewriting and local additions:\n");
+  for (h = header_list->next; h != NULL; h = h->next)
+    debug_printf("%c %s", h->type, h->text);
+  debug_printf("\n");
+  }
+
+/* The headers are now complete in store. If we are running in filter
+testing mode, that is all this function does. Return TRUE if the message
+ended with a dot. */
+
+if (filter_test != FTEST_NONE)
+  {
+  process_info[process_info_len] = 0;
+  return message_ended == END_DOT;
+  }
+
+/* Cutthrough delivery:
+       We have to create the Received header now rather than at the end of reception,
+       so the timestamp behaviour is a change to the normal case.
+       XXX Ensure this gets documented XXX.
+       Having created it, send the headers to the destination.
+*/
+if (cutthrough_fd >= 0)
+  {
+  if (received_count > received_headers_max)
+    {
+    cancel_cutthrough_connection("too many headers");
+    if (smtp_input) receive_swallow_smtp();  /* Swallow incoming SMTP */
+    log_write(0, LOG_MAIN|LOG_REJECT, "rejected from <%s>%s%s%s%s: "
+      "Too many \"Received\" headers",
+      sender_address,
+      (sender_fullhost == NULL)? "" : " H=",
+      (sender_fullhost == NULL)? US"" : sender_fullhost,
+      (sender_ident == NULL)? "" : " U=",
+      (sender_ident == NULL)? US"" : sender_ident);
+    message_id[0] = 0;                       /* Indicate no message accepted */
+    smtp_reply = US"550 Too many \"Received\" headers - suspected mail loop";
+    goto TIDYUP;                             /* Skip to end of function */
+    }
+  received_header_gen();
+  add_acl_headers(ACL_WHERE_RCPT, US"MAIL or RCPT");
+  (void) cutthrough_headers_send();
+  }
+
+
+/* Open a new spool file for the data portion of the message. We need
+to access it both via a file descriptor and a stream. Try to make the
+directory if it isn't there. Note re use of sprintf: spool_directory
+is checked on input to be < 200 characters long. */
+
+sprintf(CS spool_name, "%s/input/%s/%s-D", spool_directory, message_subdir,
+  message_id);
+data_fd = Uopen(spool_name, O_RDWR|O_CREAT|O_EXCL, SPOOL_MODE);
+if (data_fd < 0)
+  {
+  if (errno == ENOENT)
+    {
+    uschar temp[16];
+    sprintf(CS temp, "input/%s", message_subdir);
+    if (message_subdir[0] == 0) temp[5] = 0;
+    (void)directory_make(spool_directory, temp, INPUT_DIRECTORY_MODE, TRUE);
+    data_fd = Uopen(spool_name, O_RDWR|O_CREAT|O_EXCL, SPOOL_MODE);
+    }
+  if (data_fd < 0)
+    log_write(0, LOG_MAIN|LOG_PANIC_DIE, "Failed to create spool file %s: %s",
+      spool_name, strerror(errno));
+  }
+
+/* Make sure the file's group is the Exim gid, and double-check the mode
+because the group setting doesn't always get set automatically. */
+
+if (fchown(data_fd, exim_uid, exim_gid))
+  log_write(0, LOG_MAIN|LOG_PANIC_DIE,
+    "Failed setting ownership on spool file %s: %s",
+    spool_name, strerror(errno));
+(void)fchmod(data_fd, SPOOL_MODE);
+
+/* We now have data file open. Build a stream for it and lock it. We lock only
+the first line of the file (containing the message ID) because otherwise there
+are problems when Exim is run under Cygwin (I'm told). See comments in
+spool_in.c, where the same locking is done. */
+
+data_file = fdopen(data_fd, "w+");
+lock_data.l_type = F_WRLCK;
+lock_data.l_whence = SEEK_SET;
+lock_data.l_start = 0;
+lock_data.l_len = SPOOL_DATA_START_OFFSET;
+
+if (fcntl(data_fd, F_SETLK, &lock_data) < 0)
+  log_write(0, LOG_MAIN|LOG_PANIC_DIE, "Cannot lock %s (%d): %s", spool_name,
+    errno, strerror(errno));
+
+/* We have an open, locked data file. Write the message id to it to make it
+self-identifying. Then read the remainder of the input of this message and
+write it to the data file. If the variable next != NULL, it contains the first
+data line (which was read as a header but then turned out not to have the right
+format); write it (remembering that it might contain binary zeros). The result
+of fwrite() isn't inspected; instead we call ferror() below. */
+
+fprintf(data_file, "%s-D\n", message_id);
+if (next != NULL)
+  {
+  uschar *s = next->text;
+  int len = next->slen;
+  len = fwrite(s, 1, len, data_file);  len = len; /* compiler quietening */
+  body_linecount++;                 /* Assumes only 1 line */
+  }
+
+/* Note that we might already be at end of file, or the logical end of file
+(indicated by '.'), or might have encountered an error while writing the
+message id or "next" line. */
+
+if (!ferror(data_file) && !(receive_feof)() && message_ended != END_DOT)
+  {
+  if (smtp_input)
+    {
+    message_ended = read_message_data_smtp(data_file);
+    receive_linecount++;                /* The terminating "." line */
+    }
+  else message_ended = read_message_data(data_file);
+
+  receive_linecount += body_linecount;  /* For BSMTP errors mainly */
+  message_linecount += body_linecount;
+
+  /* Handle premature termination of SMTP */
+
+  if (smtp_input && message_ended == END_EOF)
+    {
+    Uunlink(spool_name);                     /* Lose data file when closed */
+    cancel_cutthrough_connection("sender closed connection");
+    message_id[0] = 0;                       /* Indicate no message accepted */
+    smtp_reply = handle_lost_connection(US"");
+    smtp_yield = FALSE;
+    goto TIDYUP;                             /* Skip to end of function */
+    }
+
+  /* Handle message that is too big. Don't use host_or_ident() in the log
+  message; we want to see the ident value even for non-remote messages. */
+
+  if (message_ended == END_SIZE)
+    {
+    Uunlink(spool_name);                /* Lose the data file when closed */
+    cancel_cutthrough_connection("mail too big");
+    if (smtp_input) receive_swallow_smtp();  /* Swallow incoming SMTP */
+
+    log_write(L_size_reject, LOG_MAIN|LOG_REJECT, "rejected from <%s>%s%s%s%s: "
+      "message too big: read=%d max=%d",
+      sender_address,
+      (sender_fullhost == NULL)? "" : " H=",
+      (sender_fullhost == NULL)? US"" : sender_fullhost,
+      (sender_ident == NULL)? "" : " U=",
+      (sender_ident == NULL)? US"" : sender_ident,
+      message_size,
+      thismessage_size_limit);
+
+    if (smtp_input)
+      {
+      smtp_reply = US"552 Message size exceeds maximum permitted";
+      message_id[0] = 0;               /* Indicate no message accepted */
+      goto TIDYUP;                     /* Skip to end of function */
+      }
+    else
+      {
+      fseek(data_file, (long int)SPOOL_DATA_START_OFFSET, SEEK_SET);
+      give_local_error(ERRMESS_TOOBIG,
+        string_sprintf("message too big (max=%d)", thismessage_size_limit),
+        US"message rejected: ", error_rc, data_file, header_list);
+      /* Does not return */
+      }
+    }
+  }
+
+/* Restore the standard SIGALRM handler for any subsequent processing. (For
+example, there may be some expansion in an ACL that uses a timer.) */
+
+os_non_restarting_signal(SIGALRM, sigalrm_handler);
+
+/* The message body has now been read into the data file. Call fflush() to
+empty the buffers in C, and then call fsync() to get the data written out onto
+the disk, as fflush() doesn't do this (or at least, it isn't documented as
+having to do this). If there was an I/O error on either input or output,
+attempt to send an error message, and unlink the spool file. For non-SMTP input
+we can then give up. Note that for SMTP input we must swallow the remainder of
+the input in cases of output errors, since the far end doesn't expect to see
+anything until the terminating dot line is sent. */
+
+if (fflush(data_file) == EOF || ferror(data_file) ||
+    EXIMfsync(fileno(data_file)) < 0 || (receive_ferror)())
+  {
+  uschar *msg_errno = US strerror(errno);
+  BOOL input_error = (receive_ferror)() != 0;
+  uschar *msg = string_sprintf("%s error (%s) while receiving message from %s",
+    input_error? "Input read" : "Spool write",
+    msg_errno,
+    (sender_fullhost != NULL)? sender_fullhost : sender_ident);
+
+  log_write(0, LOG_MAIN, "Message abandoned: %s", msg);
+  Uunlink(spool_name);                /* Lose the data file */
+  cancel_cutthrough_connection("error writing spoolfile");
+
+  if (smtp_input)
+    {
+    if (input_error)
+      smtp_reply = US"451 Error while reading input data";
+    else
+      {
+      smtp_reply = US"451 Error while writing spool file";
+      receive_swallow_smtp();
+      }
+    message_id[0] = 0;               /* Indicate no message accepted */
+    goto TIDYUP;                     /* Skip to end of function */
+    }
+
+  else
+    {
+    fseek(data_file, (long int)SPOOL_DATA_START_OFFSET, SEEK_SET);
+    give_local_error(ERRMESS_IOERR, msg, US"", error_rc, data_file,
+      header_list);
+    /* Does not return */
+    }
+  }
+
+
+/* No I/O errors were encountered while writing the data file. */
+
+DEBUG(D_receive) debug_printf("Data file written for message %s\n", message_id);
+
+
+/* If there were any bad addresses extracted by -t, or there were no recipients
+left after -t, send a message to the sender of this message, or write it to
+stderr if the error handling option is set that way. Note that there may
+legitimately be no recipients for an SMTP message if they have all been removed
+by "discard".
+
+We need to rewind the data file in order to read it. In the case of no
+recipients or stderr error writing, throw the data file away afterwards, and
+exit. (This can't be SMTP, which always ensures there's at least one
+syntactically good recipient address.) */
+
+if (extract_recip && (bad_addresses != NULL || recipients_count == 0))
+  {
+  DEBUG(D_receive)
+    {
+    if (recipients_count == 0) debug_printf("*** No recipients\n");
+    if (bad_addresses != NULL)
+      {
+      error_block *eblock = bad_addresses;
+      debug_printf("*** Bad address(es)\n");
+      while (eblock != NULL)
+        {
+        debug_printf("  %s: %s\n", eblock->text1, eblock->text2);
+        eblock = eblock->next;
+        }
+      }
+    }
+
+  fseek(data_file, (long int)SPOOL_DATA_START_OFFSET, SEEK_SET);
+
+  /* If configured to send errors to the sender, but this fails, force
+  a failure error code. We use a special one for no recipients so that it
+  can be detected by the autoreply transport. Otherwise error_rc is set to
+  errors_sender_rc, which is EXIT_FAILURE unless -oee was given, in which case
+  it is EXIT_SUCCESS. */
+
+  if (error_handling == ERRORS_SENDER)
+    {
+    if (!moan_to_sender(
+          (bad_addresses == NULL)?
+            (extracted_ignored? ERRMESS_IGADDRESS : ERRMESS_NOADDRESS) :
+          (recipients_list == NULL)? ERRMESS_BADNOADDRESS : ERRMESS_BADADDRESS,
+          bad_addresses, header_list, data_file, FALSE))
+      error_rc = (bad_addresses == NULL)? EXIT_NORECIPIENTS : EXIT_FAILURE;
+    }
+  else
+    {
+    if (bad_addresses == NULL)
+      {
+      if (extracted_ignored)
+        fprintf(stderr, "exim: all -t recipients overridden by command line\n");
+      else
+        fprintf(stderr, "exim: no recipients in message\n");
+      }
+    else
+      {
+      fprintf(stderr, "exim: invalid address%s",
+        (bad_addresses->next == NULL)? ":" : "es:\n");
+      while (bad_addresses != NULL)
+        {
+        fprintf(stderr, "  %s: %s\n", bad_addresses->text1,
+          bad_addresses->text2);
+        bad_addresses = bad_addresses->next;
+        }
+      }
+    }
+
+  if (recipients_count == 0 || error_handling == ERRORS_STDERR)
+    {
+    Uunlink(spool_name);
+    (void)fclose(data_file);
+    exim_exit(error_rc);
+    }
+  }
+
+/* Data file successfully written. Generate text for the Received: header by
+expanding the configured string, and adding a timestamp. By leaving this
+operation till now, we ensure that the timestamp is the time that message
+reception was completed. However, this is deliberately done before calling the
+data ACL and local_scan().
+
+This Received: header may therefore be inspected by the data ACL and by code in
+the local_scan() function. When they have run, we update the timestamp to be
+the final time of reception.
+
+If there is just one recipient, set up its value in the $received_for variable
+for use when we generate the Received: header.
+
+Note: the checking for too many Received: headers is handled by the delivery
+code. */
+/*XXX eventually add excess Received: check for cutthrough case back when classifying them */
+
+if (received_header->text == NULL)     /* Non-cutthrough case */
+  {
+  received_header_gen();
+
+  /* Set the value of message_body_size for the DATA ACL and for local_scan() */
+
+  message_body_size = (fstat(data_fd, &statbuf) == 0)?
+    statbuf.st_size - SPOOL_DATA_START_OFFSET : -1;
+
+  /* If an ACL from any RCPT commands set up any warning headers to add, do so
+  now, before running the DATA ACL. */
+
+  add_acl_headers(ACL_WHERE_RCPT, US"MAIL or RCPT");
+  }
+else
+  message_body_size = (fstat(data_fd, &statbuf) == 0)?
+    statbuf.st_size - SPOOL_DATA_START_OFFSET : -1;
+
+/* If an ACL is specified for checking things at this stage of reception of a
+message, run it, unless all the recipients were removed by "discard" in earlier
+ACLs. That is the only case in which recipients_count can be zero at this
+stage. Set deliver_datafile to point to the data file so that $message_body and
+$message_body_end can be extracted if needed. Allow $recipients in expansions.
+*/
+
+deliver_datafile = data_fd;
+user_msg = NULL;
+
+enable_dollar_recipients = TRUE;
+
+if (recipients_count == 0)
+  {
+  blackholed_by = recipients_discarded? US"MAIL ACL" : US"RCPT ACL";
+  }
+else
+  {
+  /* Handle interactive SMTP messages */
+
+  if (smtp_input && !smtp_batched_input)
+    {
+
+#ifndef DISABLE_DKIM
+    if (!dkim_disable_verify)
+      {
+      /* Finish verification, this will log individual signature results to
+         the mainlog */
+      dkim_exim_verify_finish();
+
+      /* Check if we must run the DKIM ACL */
+      if ((acl_smtp_dkim != NULL) &&
+          (dkim_verify_signers != NULL) &&
+          (dkim_verify_signers[0] != '\0'))
+        {
+        uschar *dkim_verify_signers_expanded =
+          expand_string(dkim_verify_signers);
+        if (dkim_verify_signers_expanded == NULL)
+          {
+          log_write(0, LOG_MAIN|LOG_PANIC,
+            "expansion of dkim_verify_signers option failed: %s",
+            expand_string_message);
+          }
+        else
+          {
+          int sep = 0;
+          uschar *ptr = dkim_verify_signers_expanded;
+          uschar *item = NULL;
+          uschar *seen_items = NULL;
+          int     seen_items_size = 0;
+          int     seen_items_offset = 0;
+          uschar itembuf[256];
+          /* Default to OK when no items are present */
+          rc = OK;
+          while ((item = string_nextinlist(&ptr, &sep,
+                                           itembuf,
+                                           sizeof(itembuf))) != NULL)
+            {
+            /* Prevent running ACL for an empty item */
+            if (!item || (item[0] == '\0')) continue;
+            /* Only run ACL once for each domain or identity, no matter how often it
+               appears in the expanded list. */
+            if (seen_items != NULL)
+              {
+              uschar *seen_item = NULL;
+              uschar seen_item_buf[256];
+              uschar *seen_items_list = seen_items;
+              int seen_this_item = 0;
+
+              while ((seen_item = string_nextinlist(&seen_items_list, &sep,
+                                                    seen_item_buf,
+                                                    sizeof(seen_item_buf))) != NULL)
+                {
+                  if (Ustrcmp(seen_item,item) == 0)
+                    {
+                      seen_this_item = 1;
+                      break;
+                    }
+                }
+
+              if (seen_this_item > 0)
+                {
+                DEBUG(D_receive)
+                  debug_printf("acl_smtp_dkim: skipping signer %s, already seen\n", item);
+                continue;
+                }
+
+              seen_items = string_append(seen_items,&seen_items_size,&seen_items_offset,1,":");
+              }
+
+            seen_items = string_append(seen_items,&seen_items_size,&seen_items_offset,1,item);
+            seen_items[seen_items_offset] = '\0';
+
+            DEBUG(D_receive)
+              debug_printf("calling acl_smtp_dkim for dkim_cur_signer=%s\n", item);
+
+            dkim_exim_acl_setup(item);
+            rc = acl_check(ACL_WHERE_DKIM, NULL, acl_smtp_dkim, &user_msg, &log_msg);
+
+            if (rc != OK)
+              {
+                DEBUG(D_receive)
+                  debug_printf("acl_smtp_dkim: acl_check returned %d on %s, skipping remaining items\n", rc, item);
+               cancel_cutthrough_connection("dkim acl not ok");
+                break;
+              }
+            }
+          add_acl_headers(ACL_WHERE_DKIM, US"DKIM");
+          if (rc == DISCARD)
+            {
+            recipients_count = 0;
+            blackholed_by = US"DKIM ACL";
+            if (log_msg != NULL)
+              blackhole_log_msg = string_sprintf(": %s", log_msg);
+            }
+          else if (rc != OK)
+            {
+            Uunlink(spool_name);
+            if (smtp_handle_acl_fail(ACL_WHERE_DKIM, rc, user_msg, log_msg) != 0)
+              smtp_yield = FALSE;    /* No more messsages after dropped connection */
+            smtp_reply = US"";       /* Indicate reply already sent */
+            message_id[0] = 0;       /* Indicate no message accepted */
+            goto TIDYUP;             /* Skip to end of function */
+            }
+          }
+        }
+      }
+#endif /* DISABLE_DKIM */
+
+#ifdef WITH_CONTENT_SCAN
+    if (recipients_count > 0 &&
+        acl_smtp_mime != NULL &&
+        !run_mime_acl(acl_smtp_mime, &smtp_yield, &smtp_reply, &blackholed_by))
+      goto TIDYUP;
+#endif /* WITH_CONTENT_SCAN */
+
+#ifdef EXPERIMENTAL_DMARC
+    dmarc_up = dmarc_store_data(from_header);
+#endif /* EXPERIMENTAL_DMARC */
+
+#ifndef DISABLE_PRDR
+    if (prdr_requested && recipients_count > 1 && acl_smtp_data_prdr)
+      {
+      unsigned int c;
+      int all_pass = OK;
+      int all_fail = FAIL;
+
+      smtp_printf("353 PRDR content analysis beginning\r\n");
+      /* Loop through recipients, responses must be in same order received */
+      for (c = 0; recipients_count > c; c++)
+        {
+       uschar * addr= recipients_list[c].address;
+       uschar * msg= US"PRDR R=<%s> %s";
+       uschar * code;
+        DEBUG(D_receive)
+          debug_printf("PRDR processing recipient %s (%d of %d)\n",
+                       addr, c+1, recipients_count);
+        rc = acl_check(ACL_WHERE_PRDR, addr,
+                       acl_smtp_data_prdr, &user_msg, &log_msg);
+
+        /* If any recipient rejected content, indicate it in final message */
+        all_pass |= rc;
+        /* If all recipients rejected, indicate in final message */
+        all_fail &= rc;
+
+        switch (rc)
+          {
+          case OK: case DISCARD: code = US"250"; break;
+          case DEFER:            code = US"450"; break;
+          default:               code = US"550"; break;
+          }
+       if (user_msg != NULL)
+         smtp_user_msg(code, user_msg);
+       else
+         {
+         switch (rc)
+            {
+            case OK: case DISCARD:
+              msg = string_sprintf(CS msg, addr, "acceptance");        break;
+            case DEFER:
+              msg = string_sprintf(CS msg, addr, "temporary refusal"); break;
+            default:
+              msg = string_sprintf(CS msg, addr, "refusal");           break;
+            }
+          smtp_user_msg(code, msg);
+         }
+       if (log_msg)       log_write(0, LOG_MAIN, "PRDR %s %s", addr, log_msg);
+       else if (user_msg) log_write(0, LOG_MAIN, "PRDR %s %s", addr, user_msg);
+       else               log_write(0, LOG_MAIN, "%s", CS msg);
+
+       if (rc != OK) { receive_remove_recipient(addr); c--; }
+        }
+      /* Set up final message, used if data acl gives OK */
+      smtp_reply = string_sprintf("%s id=%s message %s",
+                      all_fail == FAIL ? US"550" : US"250",
+                      message_id,
+                       all_fail == FAIL
+                        ? US"rejected for all recipients"
+                        : all_pass == OK
+                          ? US"accepted"
+                          : US"accepted for some recipients");
+      if (recipients_count == 0)
+        {
+        message_id[0] = 0;       /* Indicate no message accepted */
+       goto TIDYUP;
+       }
+      }
+    else
+      prdr_requested = FALSE;
+#endif /* !DISABLE_PRDR */
+
+    /* Check the recipients count again, as the MIME ACL might have changed
+    them. */
+
+    if (acl_smtp_data != NULL && recipients_count > 0)
+      {
+      rc = acl_check(ACL_WHERE_DATA, NULL, acl_smtp_data, &user_msg, &log_msg);
+      add_acl_headers(ACL_WHERE_DATA, US"DATA");
+      if (rc == DISCARD)
+        {
+        recipients_count = 0;
+        blackholed_by = US"DATA ACL";
+        if (log_msg != NULL)
+          blackhole_log_msg = string_sprintf(": %s", log_msg);
+       cancel_cutthrough_connection("data acl discard");
+        }
+      else if (rc != OK)
+        {
+        Uunlink(spool_name);
+       cancel_cutthrough_connection("data acl not ok");
+#ifdef WITH_CONTENT_SCAN
+        unspool_mbox();
+#endif
+#ifdef EXPERIMENTAL_DCC
+       dcc_ok = 0;
+#endif
+        if (smtp_handle_acl_fail(ACL_WHERE_DATA, rc, user_msg, log_msg) != 0)
+          smtp_yield = FALSE;    /* No more messsages after dropped connection */
+        smtp_reply = US"";       /* Indicate reply already sent */
+        message_id[0] = 0;       /* Indicate no message accepted */
+        goto TIDYUP;             /* Skip to end of function */
+        }
+      }
+    }
+
+  /* Handle non-SMTP and batch SMTP (i.e. non-interactive) messages. Note that
+  we cannot take different actions for permanent and temporary rejections. */
+
+  else
+    {
+
+#ifdef WITH_CONTENT_SCAN
+    if (acl_not_smtp_mime != NULL &&
+        !run_mime_acl(acl_not_smtp_mime, &smtp_yield, &smtp_reply,
+          &blackholed_by))
+      goto TIDYUP;
+#endif /* WITH_CONTENT_SCAN */
+
+    if (acl_not_smtp != NULL)
+      {
+      uschar *user_msg, *log_msg;
+      rc = acl_check(ACL_WHERE_NOTSMTP, NULL, acl_not_smtp, &user_msg, &log_msg);
+      if (rc == DISCARD)
+        {
+        recipients_count = 0;
+        blackholed_by = US"non-SMTP ACL";
+        if (log_msg != NULL)
+          blackhole_log_msg = string_sprintf(": %s", log_msg);
+        }
+      else if (rc != OK)
+        {
+        Uunlink(spool_name);
+#ifdef WITH_CONTENT_SCAN
+        unspool_mbox();
+#endif
+#ifdef EXPERIMENTAL_DCC
+       dcc_ok = 0;
+#endif
+        /* The ACL can specify where rejections are to be logged, possibly
+        nowhere. The default is main and reject logs. */
+
+        if (log_reject_target != 0)
+          log_write(0, log_reject_target, "F=<%s> rejected by non-SMTP ACL: %s",
+            sender_address, log_msg);
+
+        if (user_msg == NULL) user_msg = US"local configuration problem";
+        if (smtp_batched_input)
+          {
+          moan_smtp_batch(NULL, "%d %s", 550, user_msg);
+          /* Does not return */
+          }
+        else
+          {
+          fseek(data_file, (long int)SPOOL_DATA_START_OFFSET, SEEK_SET);
+          give_local_error(ERRMESS_LOCAL_ACL, user_msg,
+            US"message rejected by non-SMTP ACL: ", error_rc, data_file,
+              header_list);
+          /* Does not return */
+          }
+        }
+      add_acl_headers(ACL_WHERE_NOTSMTP, US"non-SMTP");
+      }
+    }
+
+  /* The applicable ACLs have been run */
+
+  if (deliver_freeze) frozen_by = US"ACL";     /* for later logging */
+  if (queue_only_policy) queued_by = US"ACL";
+  }
+
+#ifdef WITH_CONTENT_SCAN
+unspool_mbox();
+#endif
+
+#ifdef EXPERIMENTAL_DCC
+dcc_ok = 0;
+#endif
+
+
+/* The final check on the message is to run the scan_local() function. The
+version supplied with Exim always accepts, but this is a hook for sysadmins to
+supply their own checking code. The local_scan() function is run even when all
+the recipients have been discarded. */
+
+lseek(data_fd, (long int)SPOOL_DATA_START_OFFSET, SEEK_SET);
+
+/* Arrange to catch crashes in local_scan(), so that the -D file gets
+deleted, and the incident gets logged. */
+
+os_non_restarting_signal(SIGSEGV, local_scan_crash_handler);
+os_non_restarting_signal(SIGFPE, local_scan_crash_handler);
+os_non_restarting_signal(SIGILL, local_scan_crash_handler);
+os_non_restarting_signal(SIGBUS, local_scan_crash_handler);
+
+DEBUG(D_receive) debug_printf("calling local_scan(); timeout=%d\n",
+  local_scan_timeout);
+local_scan_data = NULL;
+
+os_non_restarting_signal(SIGALRM, local_scan_timeout_handler);
+if (local_scan_timeout > 0) alarm(local_scan_timeout);
+rc = local_scan(data_fd, &local_scan_data);
+alarm(0);
+os_non_restarting_signal(SIGALRM, sigalrm_handler);
+
+enable_dollar_recipients = FALSE;
+
+store_pool = POOL_MAIN;   /* In case changed */
+DEBUG(D_receive) debug_printf("local_scan() returned %d %s\n", rc,
+  local_scan_data);
+
+os_non_restarting_signal(SIGSEGV, SIG_DFL);
+os_non_restarting_signal(SIGFPE, SIG_DFL);
+os_non_restarting_signal(SIGILL, SIG_DFL);
+os_non_restarting_signal(SIGBUS, SIG_DFL);
+
+/* The length check is paranoia against some runaway code, and also because
+(for a success return) lines in the spool file are read into big_buffer. */
+
+if (local_scan_data != NULL)
+  {
+  int len = Ustrlen(local_scan_data);
+  if (len > LOCAL_SCAN_MAX_RETURN) len = LOCAL_SCAN_MAX_RETURN;
+  local_scan_data = string_copyn(local_scan_data, len);
+  }
+
+if (rc == LOCAL_SCAN_ACCEPT_FREEZE)
+  {
+  if (!deliver_freeze)         /* ACL might have already frozen */
+    {
+    deliver_freeze = TRUE;
+    deliver_frozen_at = time(NULL);
+    frozen_by = US"local_scan()";
+    }
+  rc = LOCAL_SCAN_ACCEPT;
+  }
+else if (rc == LOCAL_SCAN_ACCEPT_QUEUE)
+  {
+  if (!queue_only_policy)      /* ACL might have already queued */
+    {
+    queue_only_policy = TRUE;
+    queued_by = US"local_scan()";
+    }
+  rc = LOCAL_SCAN_ACCEPT;
+  }
+
+/* Message accepted: remove newlines in local_scan_data because otherwise
+the spool file gets corrupted. Ensure that all recipients are qualified. */
+
+if (rc == LOCAL_SCAN_ACCEPT)
+  {
+  if (local_scan_data != NULL)
+    {
+    uschar *s;
+    for (s = local_scan_data; *s != 0; s++) if (*s == '\n') *s = ' ';
+    }
+  for (i = 0; i < recipients_count; i++)
+    {
+    recipient_item *r = recipients_list + i;
+    r->address = rewrite_address_qualify(r->address, TRUE);
+    if (r->errors_to != NULL)
+      r->errors_to = rewrite_address_qualify(r->errors_to, TRUE);
+    }
+  if (recipients_count == 0 && blackholed_by == NULL)
+    blackholed_by = US"local_scan";
+  }
+
+/* Message rejected: newlines permitted in local_scan_data to generate
+multiline SMTP responses. */
+
+else
+  {
+  uschar *istemp = US"";
+  uschar *s = NULL;
+  uschar *smtp_code;
+  int size = 0;
+  int sptr = 0;
+
+  errmsg = local_scan_data;
+
+  Uunlink(spool_name);          /* Cancel this message */
+  switch(rc)
+    {
+    default:
+    log_write(0, LOG_MAIN, "invalid return %d from local_scan(). Temporary "
+      "rejection given", rc);
+    goto TEMPREJECT;
+
+    case LOCAL_SCAN_REJECT_NOLOGHDR:
+    log_extra_selector &= ~LX_rejected_header;
+    /* Fall through */
+
+    case LOCAL_SCAN_REJECT:
+    smtp_code = US"550";
+    if (errmsg == NULL) errmsg =  US"Administrative prohibition";
+    break;
+
+    case LOCAL_SCAN_TEMPREJECT_NOLOGHDR:
+    log_extra_selector &= ~LX_rejected_header;
+    /* Fall through */
+
+    case LOCAL_SCAN_TEMPREJECT:
+    TEMPREJECT:
+    smtp_code = US"451";
+    if (errmsg == NULL) errmsg = US"Temporary local problem";
+    istemp = US"temporarily ";
+    break;
+    }
+
+  s = string_append(s, &size, &sptr, 2, US"F=",
+    (sender_address[0] == 0)? US"<>" : sender_address);
+  s = add_host_info_for_log(s, &size, &sptr);
+  s[sptr] = 0;
+
+  log_write(0, LOG_MAIN|LOG_REJECT, "%s %srejected by local_scan(): %.256s",
+    s, istemp, string_printing(errmsg));
+
+  if (smtp_input)
+    {
+    if (!smtp_batched_input)
+      {
+      smtp_respond(smtp_code, 3, TRUE, errmsg);
+      message_id[0] = 0;            /* Indicate no message accepted */
+      smtp_reply = US"";            /* Indicate reply already sent */
+      goto TIDYUP;                  /* Skip to end of function */
+      }
+    else
+      {
+      moan_smtp_batch(NULL, "%s %s", smtp_code, errmsg);
+      /* Does not return */
+      }
+    }
+  else
+    {
+    fseek(data_file, (long int)SPOOL_DATA_START_OFFSET, SEEK_SET);
+    give_local_error(ERRMESS_LOCAL_SCAN, errmsg,
+      US"message rejected by local scan code: ", error_rc, data_file,
+        header_list);
+    /* Does not return */
+    }
+  }
+
+/* Reset signal handlers to ignore signals that previously would have caused
+the message to be abandoned. */
+
+signal(SIGTERM, SIG_IGN);
+signal(SIGINT, SIG_IGN);
+
+
+/* Ensure the first time flag is set in the newly-received message. */
+
+deliver_firsttime = TRUE;
+
+#ifdef EXPERIMENTAL_BRIGHTMAIL
+if (bmi_run == 1) {
+  /* rewind data file */
+  lseek(data_fd, (long int)SPOOL_DATA_START_OFFSET, SEEK_SET);
+  bmi_verdicts = bmi_process_message(header_list, data_fd);
+};
+#endif
+
+/* Update the timstamp in our Received: header to account for any time taken by
+an ACL or by local_scan(). The new time is the time that all reception
+processing is complete. */
+
+timestamp = expand_string(US"${tod_full}");
+tslen = Ustrlen(timestamp);
+
+memcpy(received_header->text + received_header->slen - tslen - 1,
+  timestamp, tslen);
+
+/* In MUA wrapper mode, ignore queueing actions set by ACL or local_scan() */
+
+if (mua_wrapper)
+  {
+  deliver_freeze = FALSE;
+  queue_only_policy = FALSE;
+  }
+
+/* Keep the data file open until we have written the header file, in order to
+hold onto the lock. In a -bh run, or if the message is to be blackholed, we
+don't write the header file, and we unlink the data file. If writing the header
+file fails, we have failed to accept this message. */
+
+if (host_checking || blackholed_by != NULL)
+  {
+  header_line *h;
+  Uunlink(spool_name);
+  msg_size = 0;                                  /* Compute size for log line */
+  for (h = header_list; h != NULL; h = h->next)
+    if (h->type != '*') msg_size += h->slen;
+  }
+
+/* Write the -H file */
+
+else
+  {
+  if ((msg_size = spool_write_header(message_id, SW_RECEIVING, &errmsg)) < 0)
+    {
+    log_write(0, LOG_MAIN, "Message abandoned: %s", errmsg);
+    Uunlink(spool_name);           /* Lose the data file */
+
+    if (smtp_input)
+      {
+      smtp_reply = US"451 Error in writing spool file";
+      message_id[0] = 0;          /* Indicate no message accepted */
+      goto TIDYUP;
+      }
+    else
+      {
+      fseek(data_file, (long int)SPOOL_DATA_START_OFFSET, SEEK_SET);
+      give_local_error(ERRMESS_IOERR, errmsg, US"", error_rc, data_file,
+        header_list);
+      /* Does not return */
+      }
+    }
+  }
+
+
+/* The message has now been successfully received. */
+
+receive_messagecount++;
+
+/* In SMTP sessions we may receive several in one connection. After each one,
+we wait for the clock to tick at the level of message-id granularity. This is
+so that the combination of time+pid is unique, even on systems where the pid
+can be re-used within our time interval. We can't shorten the interval without
+re-designing the message-id. See comments above where the message id is
+created. This is Something For The Future. */
+
+message_id_tv.tv_usec = (message_id_tv.tv_usec/id_resolution) * id_resolution;
+exim_wait_tick(&message_id_tv, id_resolution);
+
+/* Add data size to written header size. We do not count the initial file name
+that is in the file, but we do add one extra for the notional blank line that
+precedes the data. This total differs from message_size in that it include the
+added Received: header and any other headers that got created locally. */
+
+fflush(data_file);
+fstat(data_fd, &statbuf);
+
+msg_size += statbuf.st_size - SPOOL_DATA_START_OFFSET + 1;
+
+/* Generate a "message received" log entry. We do this by building up a dynamic
+string as required. Since we commonly want to add two items at a time, use a
+macro to simplify the coding. We log the arrival of a new message while the
+file is still locked, just in case the machine is *really* fast, and delivers
+it first! Include any message id that is in the message - since the syntax of a
+message id is actually an addr-spec, we can use the parse routine to canonicize
+it. */
+
+size = 256;
+sptr = 0;
+s = store_get(size);
+
+s = string_append(s, &size, &sptr, 2, US"<= ",
+  (sender_address[0] == 0)? US"<>" : sender_address);
+if (message_reference != NULL)
+  s = string_append(s, &size, &sptr, 2, US" R=", message_reference);
+
+s = add_host_info_for_log(s, &size, &sptr);
+
+#ifdef SUPPORT_TLS
+if (log_extra_selector & LX_tls_cipher && tls_in.cipher)
+  s = string_append(s, &size, &sptr, 2, US" X=", tls_in.cipher);
+if (log_extra_selector & LX_tls_certificate_verified && tls_in.cipher)
+  s = string_append(s, &size, &sptr, 2, US" CV=",
+    tls_in.certificate_verified? "yes":"no");
+if (log_extra_selector & LX_tls_peerdn && tls_in.peerdn)
+  s = string_append(s, &size, &sptr, 3, US" DN=\"",
+    string_printing(tls_in.peerdn), US"\"");
+if (log_extra_selector & LX_tls_sni && tls_in.sni)
+  s = string_append(s, &size, &sptr, 3, US" SNI=\"",
+    string_printing(tls_in.sni), US"\"");
+#endif
+
+if (sender_host_authenticated)
+  {
+  s = string_append(s, &size, &sptr, 2, US" A=", sender_host_authenticated);
+  if (authenticated_id != NULL)
+    {
+    s = string_append(s, &size, &sptr, 2, US":", authenticated_id);
+    if (log_extra_selector & LX_smtp_mailauth  &&  authenticated_sender != NULL)
+      s = string_append(s, &size, &sptr, 2, US":", authenticated_sender);
+    }
+  }
+
+#ifndef DISABLE_PRDR
+if (prdr_requested)
+  s = string_append(s, &size, &sptr, 1, US" PRDR");
+#endif
+
+#ifdef EXPERIMENTAL_PROXY
+if (proxy_session &&  log_extra_selector & LX_proxy)
+  s = string_append(s, &size, &sptr, 2, US" PRX=", proxy_host_address);
+#endif
+
+sprintf(CS big_buffer, "%d", msg_size);
+s = string_append(s, &size, &sptr, 2, US" S=", big_buffer);
+
+/* log 8BITMIME mode announced in MAIL_FROM
+   0 ... no BODY= used
+   7 ... 7BIT
+   8 ... 8BITMIME */
+if (log_extra_selector & LX_8bitmime)
+  {
+  sprintf(CS big_buffer, "%d", body_8bitmime);
+  s = string_append(s, &size, &sptr, 2, US" M8S=", big_buffer);
+  }
+
+/* If an addr-spec in a message-id contains a quoted string, it can contain
+any characters except " \ and CR and so in particular it can contain NL!
+Therefore, make sure we use a printing-characters only version for the log.
+Also, allow for domain literals in the message id. */
+
+if (msgid_header != NULL)
+  {
+  uschar *old_id;
+  BOOL save_allow_domain_literals = allow_domain_literals;
+  allow_domain_literals = TRUE;
+  old_id = parse_extract_address(Ustrchr(msgid_header->text, ':') + 1,
+    &errmsg, &start, &end, &domain, FALSE);
+  allow_domain_literals = save_allow_domain_literals;
+  if (old_id != NULL)
+    s = string_append(s, &size, &sptr, 2, US" id=", string_printing(old_id));
+  }
+
+/* If subject logging is turned on, create suitable printing-character
+text. By expanding $h_subject: we make use of the MIME decoding. */
+
+if ((log_extra_selector & LX_subject) != 0 && subject_header != NULL)
+  {
+  int i;
+  uschar *p = big_buffer;
+  uschar *ss = expand_string(US"$h_subject:");
+
+  /* Backslash-quote any double quotes or backslashes so as to make a
+  a C-like string, and turn any non-printers into escape sequences. */
+
+  *p++ = '\"';
+  if (*ss != 0) for (i = 0; i < 100 && ss[i] != 0; i++)
+    {
+    if (ss[i] == '\"' || ss[i] == '\\') *p++ = '\\';
+    *p++ = ss[i];
+    }
+  *p++ = '\"';
+  *p = 0;
+  s = string_append(s, &size, &sptr, 2, US" T=", string_printing(big_buffer));
+  }
+
+/* Terminate the string: string_cat() and string_append() leave room, but do
+not put the zero in. */
+
+s[sptr] = 0;
+
+/* Create a message log file if message logs are being used and this message is
+not blackholed. Write the reception stuff to it. We used to leave message log
+creation until the first delivery, but this has proved confusing for some
+people. */
+
+if (message_logs && blackholed_by == NULL)
+  {
+  int fd;
+
+  sprintf(CS spool_name, "%s/msglog/%s/%s", spool_directory, message_subdir,
+    message_id);
+  fd = Uopen(spool_name, O_WRONLY|O_APPEND|O_CREAT, SPOOL_MODE);
+
+  if (fd < 0 && errno == ENOENT)
+    {
+    uschar temp[16];
+    sprintf(CS temp, "msglog/%s", message_subdir);
+    if (message_subdir[0] == 0) temp[6] = 0;
+    (void)directory_make(spool_directory, temp, MSGLOG_DIRECTORY_MODE, TRUE);
+    fd = Uopen(spool_name, O_WRONLY|O_APPEND|O_CREAT, SPOOL_MODE);
+    }
+
+  if (fd < 0)
+    {
+    log_write(0, LOG_MAIN|LOG_PANIC, "Couldn't open message log %s: %s",
+      spool_name, strerror(errno));
+    }
+
+  else
+    {
+    FILE *message_log = fdopen(fd, "a");
+    if (message_log == NULL)
+      {
+      log_write(0, LOG_MAIN|LOG_PANIC, "Couldn't fdopen message log %s: %s",
+        spool_name, strerror(errno));
+      (void)close(fd);
+      }
+    else
+      {
+      uschar *now = tod_stamp(tod_log);
+      fprintf(message_log, "%s Received from %s\n", now, s+3);
+      if (deliver_freeze) fprintf(message_log, "%s frozen by %s\n", now,
+        frozen_by);
+      if (queue_only_policy) fprintf(message_log,
+        "%s no immediate delivery: queued by %s\n", now, queued_by);
+      (void)fclose(message_log);
+      }
+    }
+  }
+
+/* Everything has now been done for a successful message except logging its
+arrival, and outputting an SMTP response. While writing to the log, set a flag
+to cause a call to receive_bomb_out() if the log cannot be opened. */
+
+receive_call_bombout = TRUE;
+
+/* Before sending an SMTP response in a TCP/IP session, we check to see if the
+connection has gone away. This can only be done if there is no unconsumed input
+waiting in the local input buffer. We can test for this by calling
+receive_smtp_buffered(). RFC 2920 (pipelining) explicitly allows for additional
+input to be sent following the final dot, so the presence of following input is
+not an error.
+
+If the connection is still present, but there is no unread input for the
+socket, the result of a select() call will be zero. If, however, the connection
+has gone away, or if there is pending input, the result of select() will be
+non-zero. The two cases can be distinguished by trying to read the next input
+character. If we succeed, we can unread it so that it remains in the local
+buffer for handling later. If not, the connection has been lost.
+
+Of course, since TCP/IP is asynchronous, there is always a chance that the
+connection will vanish between the time of this test and the sending of the
+response, but the chance of this happening should be small. */
+
+if (smtp_input && sender_host_address != NULL && !sender_host_notsocket &&
+    !receive_smtp_buffered())
+  {
+  struct timeval tv;
+  fd_set select_check;
+  FD_ZERO(&select_check);
+  FD_SET(fileno(smtp_in), &select_check);
+  tv.tv_sec = 0;
+  tv.tv_usec = 0;
+
+  if (select(fileno(smtp_in) + 1, &select_check, NULL, NULL, &tv) != 0)
+    {
+    int c = (receive_getc)();
+    if (c != EOF) (receive_ungetc)(c); else
+      {
+      uschar *msg = US"SMTP connection lost after final dot";
+      smtp_reply = US"";    /* No attempt to send a response */
+      smtp_yield = FALSE;   /* Nothing more on this connection */
+
+      /* Re-use the log line workspace */
+
+      sptr = 0;
+      s = string_cat(s, &size, &sptr, msg, Ustrlen(msg));
+      s = add_host_info_for_log(s, &size, &sptr);
+      s[sptr] = 0;
+      log_write(0, LOG_MAIN, "%s", s);
+
+      /* Delete the files for this aborted message. */
+
+      sprintf(CS spool_name, "%s/input/%s/%s-D", spool_directory,
+        message_subdir, message_id);
+      Uunlink(spool_name);
+
+      sprintf(CS spool_name, "%s/input/%s/%s-H", spool_directory,
+        message_subdir, message_id);
+      Uunlink(spool_name);
+
+      sprintf(CS spool_name, "%s/msglog/%s/%s", spool_directory,
+        message_subdir, message_id);
+      Uunlink(spool_name);
+
+      goto TIDYUP;
+      }
+    }
+  }
+
+/* The connection has not gone away; we really are going to take responsibility
+for this message. */
+
+/* Cutthrough - had sender last-dot; assume we've sent (or bufferred) all
+   data onward by now.
+
+   Send dot onward.  If accepted, wipe the spooled files, log as delivered and accept
+   the sender's dot (below).
+   If rejected: copy response to sender, wipe the spooled files, log approriately.
+   If temp-reject: accept to sender, keep the spooled files.
+
+   Having the normal spool files lets us do data-filtering, and store/forward on temp-reject.
+
+   XXX We do not handle queue-only, freezing, or blackholes.
+*/
+if(cutthrough_fd >= 0)
+  {
+  uschar * msg= cutthrough_finaldot(); /* Ask the target system to accept the messsage */
+                                       /* Logging was done in finaldot() */
+  switch(msg[0])
+    {
+    case '2':  /* Accept. Do the same to the source; dump any spoolfiles.   */
+      cutthrough_done = ACCEPTED;
+      break;                                   /* message_id needed for SMTP accept below */
+
+    default:   /* Unknown response, or error.  Treat as temp-reject.         */
+    case '4':  /* Temp-reject. Keep spoolfiles and accept. */
+      cutthrough_done = TMP_REJ;               /* Avoid the usual immediate delivery attempt */
+      break;                                   /* message_id needed for SMTP accept below */
+
+    case '5':  /* Perm-reject.  Do the same to the source.  Dump any spoolfiles */
+      smtp_reply= msg;         /* Pass on the exact error */
+      cutthrough_done = PERM_REJ;
+      break;
+    }
+  }
+
+#ifndef DISABLE_PRDR
+if(!smtp_reply || prdr_requested)
+#else
+if(!smtp_reply)
+#endif
+  {
+  log_write(0, LOG_MAIN |
+    (((log_extra_selector & LX_received_recipients) != 0)? LOG_RECIPIENTS : 0) |
+    (((log_extra_selector & LX_received_sender) != 0)? LOG_SENDER : 0),
+    "%s", s);
+
+  /* Log any control actions taken by an ACL or local_scan(). */
+
+  if (deliver_freeze) log_write(0, LOG_MAIN, "frozen by %s", frozen_by);
+  if (queue_only_policy) log_write(L_delay_delivery, LOG_MAIN,
+    "no immediate delivery: queued by %s", queued_by);
+  }
+receive_call_bombout = FALSE;
+
+store_reset(s);   /* The store for the main log message can be reused */
+
+/* If the message is frozen, and freeze_tell is set, do the telling. */
+
+if (deliver_freeze && freeze_tell != NULL && freeze_tell[0] != 0)
+  {
+  moan_tell_someone(freeze_tell, NULL, US"Message frozen on arrival",
+    "Message %s was frozen on arrival by %s.\nThe sender is <%s>.\n",
+    message_id, frozen_by, sender_address);
+  }
+
+
+/* Either a message has been successfully received and written to the two spool
+files, or an error in writing the spool has occurred for an SMTP message, or
+an SMTP message has been rejected for policy reasons. (For a non-SMTP message
+we will have already given up because there's no point in carrying on!) In
+either event, we must now close (and thereby unlock) the data file. In the
+successful case, this leaves the message on the spool, ready for delivery. In
+the error case, the spool file will be deleted. Then tidy up store, interact
+with an SMTP call if necessary, and return.
+
+A fflush() was done earlier in the expectation that any write errors on the
+data file will be flushed(!) out thereby. Nevertheless, it is theoretically
+possible for fclose() to fail - but what to do? What has happened to the lock
+if this happens? */
+
+
+TIDYUP:
+process_info[process_info_len] = 0;                /* Remove message id */
+if (data_file != NULL) (void)fclose(data_file);    /* Frees the lock */
+
+/* Now reset signal handlers to their defaults */
+
+signal(SIGTERM, SIG_DFL);
+signal(SIGINT, SIG_DFL);
+
+/* Tell an SMTP caller the state of play, and arrange to return the SMTP return
+value, which defaults TRUE - meaning there may be more incoming messages from
+this connection. For non-SMTP callers (where there is only ever one message),
+the default is FALSE. */
+
+if (smtp_input)
+  {
+  yield = smtp_yield;
+
+  /* Handle interactive SMTP callers. After several kinds of error, smtp_reply
+  is set to the response that should be sent. When it is NULL, we generate
+  default responses. After an ACL error or local_scan() error, the response has
+  already been sent, and smtp_reply is an empty string to indicate this. */
+
+  if (!smtp_batched_input)
+    {
+    if (smtp_reply == NULL)
+      {
+      if (fake_response != OK)
+        smtp_respond((fake_response == DEFER)? US"450" : US"550", 3, TRUE,
+          fake_response_text);
+
+      /* An OK response is required; use "message" text if present. */
+
+      else if (user_msg != NULL)
+        {
+        uschar *code = US"250";
+        int len = 3;
+        smtp_message_code(&code, &len, &user_msg, NULL);
+        smtp_respond(code, len, TRUE, user_msg);
+        }
+
+      /* Default OK response */
+
+      else
+        smtp_printf("250 OK id=%s\r\n", message_id);
+      if (host_checking)
+        fprintf(stdout,
+          "\n**** SMTP testing: that is not a real message id!\n\n");
+      }
+
+    /* smtp_reply is set non-empty */
+
+    else if (smtp_reply[0] != 0)
+      {
+      if (fake_response != OK && (smtp_reply[0] == '2'))
+        smtp_respond((fake_response == DEFER)? US"450" : US"550", 3, TRUE,
+          fake_response_text);
+      else
+        smtp_printf("%.1024s\r\n", smtp_reply);
+      }
+
+    switch (cutthrough_done)
+      {
+      case ACCEPTED: log_write(0, LOG_MAIN, "Completed");/* Delivery was done */
+      case PERM_REJ: {                                 /* Delete spool files */
+             sprintf(CS spool_name, "%s/input/%s/%s-D", spool_directory,
+               message_subdir, message_id);
+             Uunlink(spool_name);
+             sprintf(CS spool_name, "%s/input/%s/%s-H", spool_directory,
+               message_subdir, message_id);
+             Uunlink(spool_name);
+             sprintf(CS spool_name, "%s/msglog/%s/%s", spool_directory,
+               message_subdir, message_id);
+             Uunlink(spool_name);
+             }
+      case TMP_REJ: message_id[0] = 0;   /* Prevent a delivery from starting */
+      default:break;
+      }
+    cutthrough_delivery = FALSE;
+    }
+
+  /* For batched SMTP, generate an error message on failure, and do
+  nothing on success. The function moan_smtp_batch() does not return -
+  it exits from the program with a non-zero return code. */
+
+  else if (smtp_reply != NULL) moan_smtp_batch(NULL, "%s", smtp_reply);
+  }
+
+
+/* If blackholing, we can immediately log this message's sad fate. The data
+file has already been unlinked, and the header file was never written to disk.
+We must now indicate that nothing was received, to prevent a delivery from
+starting. */
+
+if (blackholed_by != NULL)
+  {
+  uschar *detail = (local_scan_data != NULL)?
+    string_printing(local_scan_data) :
+    string_sprintf("(%s discarded recipients)", blackholed_by);
+  log_write(0, LOG_MAIN, "=> blackhole %s%s", detail, blackhole_log_msg);
+  log_write(0, LOG_MAIN, "Completed");
+  message_id[0] = 0;
+  }
+
+/* Reset headers so that logging of rejects for a subsequent message doesn't
+include them. It is also important to set header_last = NULL before exiting
+from this function, as this prevents certain rewrites that might happen during
+subsequent verifying (of another incoming message) from trying to add headers
+when they shouldn't. */
+
+header_list = header_last = NULL;
+
+return yield;  /* TRUE if more messages (SMTP only) */
+}
+
+/* End of receive.c */
diff --git a/src/regex.c b/src/regex.c
new file mode 100644 (file)
index 0000000..de8ec68
--- /dev/null
@@ -0,0 +1,245 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) Tom Kistner <tom@duncanthrax.net> 2003-???? */
+/* License: GPL */
+
+/* Code for matching regular expressions against headers and body.
+ Called from acl.c. */
+
+#include "exim.h"
+#ifdef WITH_CONTENT_SCAN
+#include <unistd.h>
+#include <sys/mman.h>
+
+/* Structure to hold a list of Regular expressions */
+typedef struct pcre_list {
+  pcre *re;
+  uschar *pcre_text;
+  struct pcre_list *next;
+} pcre_list;
+
+uschar regex_match_string_buffer[1024];
+
+extern FILE *mime_stream;
+extern uschar *mime_current_boundary;
+
+int regex(uschar **listptr) {
+  int sep = 0;
+  uschar *list = *listptr;
+  uschar *regex_string;
+  uschar regex_string_buffer[1024];
+  unsigned long mbox_size;
+  FILE *mbox_file;
+  pcre *re;
+  pcre_list *re_list_head = NULL;
+  pcre_list *re_list_item;
+  const char *pcre_error;
+  int pcre_erroffset;
+  uschar *linebuffer;
+  long f_pos = 0;
+
+  /* reset expansion variable */
+  regex_match_string = NULL;
+
+  if (mime_stream == NULL) {
+    /* We are in the DATA ACL */
+    mbox_file = spool_mbox(&mbox_size, NULL);
+    if (mbox_file == NULL) {
+      /* error while spooling */
+      log_write(0, LOG_MAIN|LOG_PANIC,
+             "regex acl condition: error while creating mbox spool file");
+      return DEFER;
+    };
+  }
+  else {
+    f_pos = ftell(mime_stream);
+    mbox_file = mime_stream;
+  };
+
+  /* precompile our regexes */
+  while ((regex_string = string_nextinlist(&list, &sep,
+                                           regex_string_buffer,
+                                           sizeof(regex_string_buffer))) != NULL) {
+
+    /* parse option */
+    if ( (strcmpic(regex_string,US"false") == 0) ||
+         (Ustrcmp(regex_string,"0") == 0) ) {
+      /* explicitly no matching */
+      continue;
+    };
+
+    /* compile our regular expression */
+    re = pcre_compile( CS regex_string,
+                       0,
+                       &pcre_error,
+                       &pcre_erroffset,
+                       NULL );
+
+    if (re == NULL) {
+      log_write(0, LOG_MAIN,
+           "regex acl condition warning - error in regex '%s': %s at offset %d, skipped.", regex_string, pcre_error, pcre_erroffset);
+      continue;
+    }
+    else {
+      re_list_item = store_get(sizeof(pcre_list));
+      re_list_item->re = re;
+      re_list_item->pcre_text = string_copy(regex_string);
+      re_list_item->next = re_list_head;
+      re_list_head = re_list_item;
+    };
+  };
+
+  /* no regexes -> nothing to do */
+  if (re_list_head == NULL) {
+    return FAIL;
+  };
+
+  /* match each line against all regexes */
+  linebuffer = store_get(32767);
+  while (fgets(CS linebuffer, 32767, mbox_file) != NULL) {
+    if ( (mime_stream != NULL) && (mime_current_boundary != NULL) ) {
+      /* check boundary */
+      if (Ustrncmp(linebuffer,"--",2) == 0) {
+        if (Ustrncmp((linebuffer+2),mime_current_boundary,Ustrlen(mime_current_boundary)) == 0)
+          /* found boundary */
+          break;
+      };
+    };
+    re_list_item = re_list_head;
+    do {
+      /* try matcher on the line */
+      if (pcre_exec(re_list_item->re, NULL, CS linebuffer,
+      (int)Ustrlen(linebuffer), 0, 0, NULL, 0) >= 0) {
+        Ustrncpy(regex_match_string_buffer, re_list_item->pcre_text, 1023);
+        regex_match_string = regex_match_string_buffer;
+        if (mime_stream == NULL)
+          (void)fclose(mbox_file);
+        else {
+          clearerr(mime_stream);
+          fseek(mime_stream,f_pos,SEEK_SET);
+        };
+        return OK;
+      };
+      re_list_item = re_list_item->next;
+    } while (re_list_item != NULL);
+  };
+
+  if (mime_stream == NULL)
+    (void)fclose(mbox_file);
+  else {
+    clearerr(mime_stream);
+    fseek(mime_stream,f_pos,SEEK_SET);
+  };
+
+  /* no matches ... */
+  return FAIL;
+}
+
+
+int mime_regex(uschar **listptr) {
+  int sep = 0;
+  uschar *list = *listptr;
+  uschar *regex_string;
+  uschar regex_string_buffer[1024];
+  pcre *re;
+  pcre_list *re_list_head = NULL;
+  pcre_list *re_list_item;
+  const char *pcre_error;
+  int pcre_erroffset;
+  FILE *f;
+  uschar *mime_subject = NULL;
+  int mime_subject_len = 0;
+
+  /* reset expansion variable */
+  regex_match_string = NULL;
+
+  /* precompile our regexes */
+  while ((regex_string = string_nextinlist(&list, &sep,
+                                           regex_string_buffer,
+                                           sizeof(regex_string_buffer))) != NULL) {
+
+    /* parse option */
+    if ( (strcmpic(regex_string,US"false") == 0) ||
+         (Ustrcmp(regex_string,"0") == 0) ) {
+      /* explicitly no matching */
+      continue;
+    };
+
+    /* compile our regular expression */
+    re = pcre_compile( CS regex_string,
+                       0,
+                       &pcre_error,
+                       &pcre_erroffset,
+                       NULL );
+
+    if (re == NULL) {
+      log_write(0, LOG_MAIN,
+           "regex acl condition warning - error in regex '%s': %s at offset %d, skipped.", regex_string, pcre_error, pcre_erroffset);
+      continue;
+    }
+    else {
+      re_list_item = store_get(sizeof(pcre_list));
+      re_list_item->re = re;
+      re_list_item->pcre_text = string_copy(regex_string);
+      re_list_item->next = re_list_head;
+      re_list_head = re_list_item;
+    };
+  };
+
+  /* no regexes -> nothing to do */
+  if (re_list_head == NULL) {
+    return FAIL;
+  };
+
+  /* check if the file is already decoded */
+  if (mime_decoded_filename == NULL) {
+    uschar *empty = US"";
+    /* no, decode it first */
+    mime_decode(&empty);
+    if (mime_decoded_filename == NULL) {
+      /* decoding failed */
+      log_write(0, LOG_MAIN,
+           "mime_regex acl condition warning - could not decode MIME part to file.");
+      return DEFER;
+    };
+  };
+
+
+  /* open file */
+  f = fopen(CS mime_decoded_filename, "rb");
+  if (f == NULL) {
+    /* open failed */
+    log_write(0, LOG_MAIN,
+         "mime_regex acl condition warning - can't open '%s' for reading.", mime_decoded_filename);
+    return DEFER;
+  };
+
+  /* get 32k memory */
+  mime_subject = (uschar *)store_get(32767);
+
+  /* read max 32k chars from file */
+  mime_subject_len = fread(mime_subject, 1, 32766, f);
+
+  re_list_item = re_list_head;
+  do {
+    /* try matcher on the mmapped file */
+    debug_printf("Matching '%s'\n", re_list_item->pcre_text);
+    if (pcre_exec(re_list_item->re, NULL, CS mime_subject,
+                  mime_subject_len, 0, 0, NULL, 0) >= 0) {
+      Ustrncpy(regex_match_string_buffer, re_list_item->pcre_text, 1023);
+      regex_match_string = regex_match_string_buffer;
+      (void)fclose(f);
+      return OK;
+    };
+    re_list_item = re_list_item->next;
+  } while (re_list_item != NULL);
+
+  (void)fclose(f);
+
+  /* no matches ... */
+  return FAIL;
+}
+
+#endif
diff --git a/src/retry.c b/src/retry.c
new file mode 100644 (file)
index 0000000..4809335
--- /dev/null
@@ -0,0 +1,943 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Functions concerned with retrying unsuccessful deliveries. */
+
+
+#include "exim.h"
+
+
+
+/*************************************************
+*         Check the ultimate address timeout     *
+*************************************************/
+
+/* This function tests whether a message has been on the queue longer than
+the maximum retry time for a particular host or address.
+
+Arguments:
+  retry_key     the key to look up a retry rule
+  domain        the domain to look up a domain retry rule
+  retry_record  contains error information for finding rule
+  now           the time
+
+Returns:        TRUE if the ultimate timeout has been reached
+*/
+
+BOOL
+retry_ultimate_address_timeout(uschar *retry_key, uschar *domain,
+  dbdata_retry *retry_record, time_t now)
+{
+BOOL address_timeout;
+
+DEBUG(D_retry)
+  {
+  debug_printf("retry time not reached: checking ultimate address timeout\n");
+  debug_printf("  now=%d first_failed=%d next_try=%d expired=%d\n",
+    (int)now, (int)retry_record->first_failed,
+    (int)retry_record->next_try, retry_record->expired);
+  }
+
+retry_config *retry =
+  retry_find_config(retry_key+2, domain,
+    retry_record->basic_errno, retry_record->more_errno);
+
+if (retry != NULL && retry->rules != NULL)
+  {
+  retry_rule *last_rule;
+  for (last_rule = retry->rules;
+       last_rule->next != NULL;
+       last_rule = last_rule->next);
+  DEBUG(D_retry)
+    debug_printf("  received_time=%d diff=%d timeout=%d\n",
+      received_time, (int)(now - received_time), last_rule->timeout);
+  address_timeout = (now - received_time > last_rule->timeout);
+  }
+else
+  {
+  DEBUG(D_retry)
+    debug_printf("no retry rule found: assume timed out\n");
+  address_timeout = TRUE;
+  }
+
+DEBUG(D_retry)
+  if (address_timeout)
+    debug_printf("on queue longer than maximum retry for address - "
+      "allowing delivery\n");
+
+return address_timeout;
+}
+
+
+
+/*************************************************
+*     Set status of a host+address item          *
+*************************************************/
+
+/* This function is passed a host_item which contains a host name and an
+IP address string. Its job is to set the status of the address if it is not
+already set (indicated by hstatus_unknown). The possible values are:
+
+   hstatus_usable    the address is not listed in the unusable tree, and does
+                     not have a retry record, OR the time is past the next
+                     try time, OR the message has been on the queue for more
+                     than the maximum retry time for a failing host
+
+   hstatus_unusable  the address is listed in the unusable tree, or does have
+                     a retry record, and the time is not yet at the next retry
+                     time.
+
+   hstatus_unusable_expired  as above, but also the retry time has expired
+                     for this address.
+
+The reason a delivery is permitted when a message has been around for a very
+long time is to allow the ultimate address timeout to operate after a delivery
+failure. Otherwise some messages may stick around without being tried for too
+long.
+
+If a host retry record is retrieved from the hints database, the time of last
+trying is filled into the last_try field of the host block. If a host is
+generally usable, a check is made to see if there is a retry delay on this
+specific message at this host.
+
+If a non-standard port is being used, it is added to the retry key.
+
+Arguments:
+  domain              the address domain
+  host                pointer to a host item
+  portstring          "" for standard port, ":xxxx" for a non-standard port
+  include_ip_address  TRUE to include the address in the key - this is
+                        usual, but sometimes is not wanted
+  retry_host_key      where to put a pointer to the key for the host-specific
+                        retry record, if one is read and the host is usable
+  retry_message_key   where to put a pointer to the key for the message+host
+                        retry record, if one is read and the host is usable
+
+Returns:    TRUE if the host has expired but is usable because
+             its retry time has come
+*/
+
+BOOL
+retry_check_address(uschar *domain, host_item *host, uschar *portstring,
+  BOOL include_ip_address, uschar **retry_host_key, uschar **retry_message_key)
+{
+BOOL yield = FALSE;
+time_t now = time(NULL);
+uschar *host_key, *message_key;
+open_db dbblock;
+open_db *dbm_file;
+tree_node *node;
+dbdata_retry *host_retry_record, *message_retry_record;
+
+*retry_host_key = *retry_message_key = NULL;
+
+DEBUG(D_transport|D_retry) debug_printf("checking status of %s\n", host->name);
+
+/* Do nothing if status already set; otherwise initialize status as usable. */
+
+if (host->status != hstatus_unknown) return FALSE;
+host->status = hstatus_usable;
+
+/* Generate the host key for the unusable tree and the retry database. Ensure
+host names are lower cased (that's what %S does). */
+
+host_key = include_ip_address?
+  string_sprintf("T:%S:%s%s", host->name, host->address, portstring) :
+  string_sprintf("T:%S%s", host->name, portstring);
+
+/* Generate the message-specific key */
+
+message_key = string_sprintf("%s:%s", host_key, message_id);
+
+/* Search the tree of unusable IP addresses. This is filled in when deliveries
+fail, because the retry database itself is not updated until the end of all
+deliveries (so as to do it all in one go). The tree records addresses that have
+become unusable during this delivery process (i.e. those that will get put into
+the retry database when it is updated). */
+
+node = tree_search(tree_unusable, host_key);
+if (node != NULL)
+  {
+  DEBUG(D_transport|D_retry) debug_printf("found in tree of unusables\n");
+  host->status = (node->data.val > 255)?
+    hstatus_unusable_expired : hstatus_unusable;
+  host->why = node->data.val & 255;
+  return FALSE;
+  }
+
+/* Open the retry database, giving up if there isn't one. Otherwise, search for
+the retry records, and then close the database again. */
+
+if ((dbm_file = dbfn_open(US"retry", O_RDONLY, &dbblock, FALSE)) == NULL)
+  {
+  DEBUG(D_deliver|D_retry|D_hints_lookup)
+    debug_printf("no retry data available\n");
+  return FALSE;
+  }
+host_retry_record = dbfn_read(dbm_file, host_key);
+message_retry_record = dbfn_read(dbm_file, message_key);
+dbfn_close(dbm_file);
+
+/* Ignore the data if it is too old - too long since it was written */
+
+if (host_retry_record == NULL)
+  {
+  DEBUG(D_transport|D_retry) debug_printf("no host retry record\n");
+  }
+else if (now - host_retry_record->time_stamp > retry_data_expire)
+  {
+  host_retry_record = NULL;
+  DEBUG(D_transport|D_retry) debug_printf("host retry record too old\n");
+  }
+
+if (message_retry_record == NULL)
+  {
+  DEBUG(D_transport|D_retry) debug_printf("no message retry record\n");
+  }
+else if (now - message_retry_record->time_stamp > retry_data_expire)
+  {
+  message_retry_record = NULL;
+  DEBUG(D_transport|D_retry) debug_printf("message retry record too old\n");
+  }
+
+/* If there's a host-specific retry record, check for reaching the retry
+time (or forcing). If not, and the host is not expired, check for the message
+having been around for longer than the maximum retry time for this host or
+address. Allow the delivery if it has. Otherwise set the appropriate unusable
+flag and return FALSE. Otherwise arrange to return TRUE if this is an expired
+host. */
+
+if (host_retry_record != NULL)
+  {
+  *retry_host_key = host_key;
+
+  /* We have not reached the next try time. Check for the ultimate address
+  timeout if the host has not expired. */
+
+  if (now < host_retry_record->next_try && !deliver_force)
+    {
+    if (!host_retry_record->expired &&
+        retry_ultimate_address_timeout(host_key, domain,
+          host_retry_record, now))
+      return FALSE;
+
+    /* We have not hit the ultimate address timeout; host is unusable. */
+
+    host->status = (host_retry_record->expired)?
+      hstatus_unusable_expired : hstatus_unusable;
+    host->why = hwhy_retry;
+    host->last_try = host_retry_record->last_try;
+    return FALSE;
+    }
+
+  /* Host is usable; set return TRUE if expired. */
+
+  yield = host_retry_record->expired;
+  }
+
+/* It's OK to try the host. If there's a message-specific retry record, check
+for reaching its retry time (or forcing). If not, mark the host unusable,
+unless the ultimate address timeout has been reached. */
+
+if (message_retry_record != NULL)
+  {
+  *retry_message_key = message_key;
+  if (now < message_retry_record->next_try && !deliver_force)
+    {
+    if (!retry_ultimate_address_timeout(host_key, domain,
+        message_retry_record, now))
+      {
+      host->status = hstatus_unusable;
+      host->why = hwhy_retry;
+      }
+    return FALSE;
+    }
+  }
+
+return yield;
+}
+
+
+
+
+/*************************************************
+*           Add a retry item to an address       *
+*************************************************/
+
+/* Retry items are chained onto an address when it is deferred either by router
+or by a transport, or if it succeeds or fails and there was a previous retry
+item that now needs to be deleted. Sometimes there can be both kinds of item:
+for example, if routing was deferred but then succeeded, and delivery then
+deferred. In that case there is a delete item for the routing retry, and an
+updating item for the delivery.
+
+(But note that that is only visible at the outer level, because in remote
+delivery subprocesses, the address starts "clean", with no retry items carried
+in.)
+
+These items are used at the end of a delivery attempt to update the retry
+database. The keys start R: for routing delays and T: for transport delays.
+
+Arguments:
+  addr    the address block onto which to hang the item
+  key     the retry key
+  flags   delete, host, and message flags, copied into the block
+
+Returns:  nothing
+*/
+
+void
+retry_add_item(address_item *addr, uschar *key, int flags)
+{
+retry_item *rti = store_get(sizeof(retry_item));
+rti->next = addr->retries;
+addr->retries = rti;
+rti->key = key;
+rti->basic_errno = addr->basic_errno;
+rti->more_errno = addr->more_errno;
+rti->message = addr->message;
+rti->flags = flags;
+
+DEBUG(D_transport|D_retry)
+  {
+  int letter = rti->more_errno & 255;
+  debug_printf("added retry item for %s: errno=%d more_errno=", rti->key,
+    rti->basic_errno);
+  if (letter == 'A' || letter == 'M')
+    debug_printf("%d,%c", (rti->more_errno >> 8) & 255, letter);
+  else
+    debug_printf("%d", rti->more_errno);
+  debug_printf(" flags=%d\n", flags);
+  }
+}
+
+
+
+/*************************************************
+*        Find retry configuration data           *
+*************************************************/
+
+/* Search the in-store retry information for the first retry item that applies
+to a given destination. If the key contains an @ we are probably handling a
+local delivery and have a complete address to search for; this happens when
+retry_use_local_part is set on a router. Otherwise, the key is likely to be a
+host name for a remote delivery, or a domain name for a local delivery. We
+prepend *@ on the front of it so that it will match a retry item whose address
+item pattern is independent of the local part. The alternate key, if set, is
+always just a domain, so we treat it likewise.
+
+Arguments:
+  key          key for which retry info is wanted
+  alternate    alternative key, always just a domain
+  basic_errno  specific error predicate on the retry rule, or zero
+  more_errno   additional data for errno predicate
+
+Returns:       pointer to retry rule, or NULL
+*/
+
+retry_config *
+retry_find_config(uschar *key, uschar *alternate, int basic_errno,
+  int more_errno)
+{
+int replace = 0;
+uschar *use_key, *use_alternate;
+uschar *colon = Ustrchr(key, ':');
+retry_config *yield;
+
+/* If there's a colon in the key, there are two possibilities:
+
+(1) This is a key for a host, ip address, and possibly port, in the format
+
+      hostname:ip+port
+
+    In this case, we temporarily replace the colon with a zero, to terminate
+    the string after the host name.
+
+(2) This is a key for a pipe, file, or autoreply delivery, in the format
+
+      pipe-or-file-or-auto:x@y
+
+    where x@y is the original address that provoked the delivery. The pipe or
+    file or auto will start with | or / or >, whereas a host name will start
+    with a letter or a digit. In this case we want to use the original address
+    to search for a retry rule. */
+
+if (colon != NULL)
+  {
+  if (isalnum(*key))
+    replace = ':';
+  else
+    key = Ustrrchr(key, ':') + 1;   /* Take from the last colon */
+  }
+
+if (replace == 0) colon = key + Ustrlen(key);
+*colon = 0;
+
+/* Sort out the keys */
+
+use_key = (Ustrchr(key, '@') != NULL)? key : string_sprintf("*@%s", key);
+use_alternate = (alternate == NULL)? NULL : string_sprintf("*@%s", alternate);
+
+/* Scan the configured retry items. */
+
+for (yield = retries; yield != NULL; yield = yield->next)
+  {
+  uschar *plist = yield->pattern;
+  uschar *slist = yield->senders;
+
+  /* If a specific error is set for this item, check that we are handling that
+  specific error, and if so, check any additional error information if
+  required. */
+
+  if (yield->basic_errno != 0)
+    {
+    /* Special code is required for quota errors, as these can either be system
+    quota errors, or Exim's own quota imposition, which has a different error
+    number. Full partitions are also treated in the same way as quota errors.
+    */
+
+    if (yield->basic_errno == ERRNO_EXIMQUOTA)
+      {
+      if ((basic_errno != ERRNO_EXIMQUOTA && basic_errno != errno_quota &&
+           basic_errno != ENOSPC) ||
+          (yield->more_errno != 0 && yield->more_errno > more_errno))
+        continue;
+      }
+
+    /* The TLSREQUIRED error also covers TLSFAILURE. These are subtly different
+    errors, but not worth separating at this level. */
+
+    else if (yield->basic_errno == ERRNO_TLSREQUIRED)
+      {
+      if (basic_errno != ERRNO_TLSREQUIRED && basic_errno != ERRNO_TLSFAILURE)
+        continue;
+      }
+
+    /* Handle 4xx responses to MAIL, RCPT, or DATA. The code that was received
+    is in the 2nd least significant byte of more_errno (with 400 subtracted).
+    The required value is coded in the 2nd least significant byte of the
+    yield->more_errno field as follows:
+
+      255     => any 4xx code
+      >= 100  => the decade must match the value less 100
+      < 100   => the exact value must match
+    */
+
+    else if (yield->basic_errno == ERRNO_MAIL4XX ||
+             yield->basic_errno == ERRNO_RCPT4XX ||
+             yield->basic_errno == ERRNO_DATA4XX)
+      {
+      int wanted;
+      if (basic_errno != yield->basic_errno) continue;
+      wanted = (yield->more_errno >> 8) & 255;
+      if (wanted != 255)
+        {
+        int evalue = (more_errno >> 8) & 255;
+        if (wanted >= 100)
+          {
+          if ((evalue/10)*10 != wanted - 100) continue;
+          }
+        else if (evalue != wanted) continue;
+        }
+      }
+
+    /* There are some special cases for timeouts */
+
+    else if (yield->basic_errno == ETIMEDOUT)
+      {
+      if (basic_errno != ETIMEDOUT) continue;
+
+      /* Just RTEF_CTOUT in the rule => don't care about 'A'/'M' addresses */
+      if (yield->more_errno == RTEF_CTOUT)
+        {
+        if ((more_errno & RTEF_CTOUT) == 0) continue;
+        }
+
+      else if (yield->more_errno != 0)
+        {
+        int cf_errno = more_errno;
+        if ((yield->more_errno & RTEF_CTOUT) == 0) cf_errno &= ~RTEF_CTOUT;
+        if (yield->more_errno != cf_errno) continue;
+        }
+      }
+
+    /* Default checks for exact match */
+
+    else
+      {
+      if (yield->basic_errno != basic_errno ||
+         (yield->more_errno != 0 && yield->more_errno != more_errno))
+       continue;
+      }
+    }
+
+  /* If the "senders" condition is set, check it. Note that sender_address may
+  be null during -brt checking, in which case we do not use this rule. */
+
+  if (slist != NULL && (sender_address == NULL ||
+      match_address_list(sender_address, TRUE, TRUE, &slist, NULL, -1, 0,
+        NULL) != OK))
+    continue;
+
+  /* Check for a match between the address list item at the start of this retry
+  rule and either the main or alternate keys. */
+
+  if (match_address_list(use_key, TRUE, TRUE, &plist, NULL, -1, UCHAR_MAX+1,
+        NULL) == OK ||
+     (use_alternate != NULL &&
+      match_address_list(use_alternate, TRUE, TRUE, &plist, NULL, -1,
+        UCHAR_MAX+1, NULL) == OK))
+    break;
+  }
+
+*colon = replace;
+return yield;
+}
+
+
+
+
+/*************************************************
+*              Update retry database             *
+*************************************************/
+
+/* Update the retry data for any directing/routing/transporting that was
+deferred, or delete it for those that succeeded after a previous defer. This is
+done all in one go to minimize opening/closing/locking of the database file.
+
+Note that, because SMTP delivery involves a list of destinations to try, there
+may be defer-type retry information for some of them even when the message was
+successfully delivered. Likewise if it eventually failed.
+
+This function may move addresses from the defer to the failed queue if the
+ultimate retry time has expired.
+
+Arguments:
+  addr_defer    queue of deferred addresses
+  addr_failed   queue of failed addresses
+  addr_succeed  queue of successful addresses
+
+Returns:        nothing
+*/
+
+void
+retry_update(address_item **addr_defer, address_item **addr_failed,
+  address_item **addr_succeed)
+{
+open_db dbblock;
+open_db *dbm_file = NULL;
+time_t now = time(NULL);
+int i;
+
+DEBUG(D_retry) debug_printf("Processing retry items\n");
+
+/* Three-times loop to handle succeeded, failed, and deferred addresses.
+Deferred addresses must be handled after failed ones, because some may be moved
+to the failed chain if they have timed out. */
+
+for (i = 0; i < 3; i++)
+  {
+  address_item *endaddr, *addr;
+  address_item *last_first = NULL;
+  address_item **paddr = (i==0)? addr_succeed :
+    (i==1)? addr_failed : addr_defer;
+  address_item **saved_paddr = NULL;
+
+  DEBUG(D_retry) debug_printf("%s addresses:\n", (i == 0)? "Succeeded" :
+    (i == 1)? "Failed" : "Deferred");
+
+  /* Loop for each address on the chain. For deferred addresses, the whole
+  address times out unless one of its retry addresses has a retry rule that
+  hasn't yet timed out. Deferred addresses should not be requesting deletion
+  of retry items, but just in case they do by accident, treat that case
+  as "not timed out".
+
+  As well as handling the addresses themselves, we must also process any
+  retry items for any parent addresses - these are typically "delete" items,
+  because the parent must have succeeded in order to generate the child. */
+
+  while ((endaddr = *paddr) != NULL)
+    {
+    BOOL timed_out = FALSE;
+    retry_item *rti;
+
+    for (addr = endaddr; addr != NULL; addr = addr->parent)
+      {
+      int update_count = 0;
+      int timedout_count = 0;
+
+      DEBUG(D_retry) debug_printf("%s%s\n", addr->address, (addr->retries == NULL)?
+        ": no retry items" : "");
+
+      /* Loop for each retry item. */
+
+      for (rti = addr->retries; rti != NULL; rti = rti->next)
+        {
+        uschar *message;
+        int message_length, message_space, failing_interval, next_try;
+        retry_rule *rule, *final_rule;
+        retry_config *retry;
+        dbdata_retry *retry_record;
+
+        /* Open the retry database if it is not already open; failure to open
+        the file is logged, but otherwise ignored - deferred addresses will
+        get retried at the next opportunity. Not opening earlier than this saves
+        opening if no addresses have retry items - common when none have yet
+        reached their retry next try time. */
+
+        if (dbm_file == NULL)
+          dbm_file = dbfn_open(US"retry", O_RDWR, &dbblock, TRUE);
+
+        if (dbm_file == NULL)
+          {
+          DEBUG(D_deliver|D_retry|D_hints_lookup)
+            debug_printf("retry database not available for updating\n");
+          return;
+          }
+
+        /* If there are no deferred addresses, that is, if this message is
+        completing, and the retry item is for a message-specific SMTP error,
+        force it to be deleted, because there's no point in keeping data for
+        no-longer-existing messages. This situation can occur when a domain has
+        two hosts and a message-specific error occurs for the first of them,
+        but the address gets delivered to the second one. This optimization
+        doesn't succeed in cleaning out all the dead entries, but it helps. */
+
+        if (*addr_defer == NULL && (rti->flags & rf_message) != 0)
+          rti->flags |= rf_delete;
+
+        /* Handle the case of a request to delete the retry info for this
+        destination. */
+
+        if ((rti->flags & rf_delete) != 0)
+          {
+          (void)dbfn_delete(dbm_file, rti->key);
+          DEBUG(D_retry)
+            debug_printf("deleted retry information for %s\n", rti->key);
+          continue;
+          }
+
+        /* Count the number of non-delete retry items. This is so that we
+        can compare it to the count of timed_out ones, to check whether
+        all are timed out. */
+
+        update_count++;
+
+        /* Get the retry information for this destination and error code, if
+        any. If this item is for a remote host with ip address, then pass
+        the domain name as an alternative to search for. If no retry
+        information is found, we can't generate a retry time, so there is
+        no point updating the database. This retry item is timed out. */
+
+        if ((retry = retry_find_config(rti->key + 2,
+             ((rti->flags & rf_host) != 0)? addr->domain : NULL,
+             rti->basic_errno, rti->more_errno)) == NULL)
+          {
+          DEBUG(D_retry) debug_printf("No configured retry item for %s%s%s\n",
+            rti->key,
+            ((rti->flags & rf_host) != 0)? US" or " : US"",
+            ((rti->flags & rf_host) != 0)? addr->domain : US"");
+          if (addr == endaddr) timedout_count++;
+          continue;
+          }
+
+        DEBUG(D_retry)
+          {
+          if ((rti->flags & rf_host) != 0)
+            debug_printf("retry for %s (%s) = %s %d %d\n", rti->key,
+              addr->domain, retry->pattern, retry->basic_errno,
+              retry->more_errno);
+          else
+            debug_printf("retry for %s = %s %d %d\n", rti->key, retry->pattern,
+              retry->basic_errno, retry->more_errno);
+          }
+
+        /* Set up the message for the database retry record. Because DBM
+        records have a maximum data length, we enforce a limit. There isn't
+        much point in keeping a huge message here, anyway. */
+
+        message = (rti->basic_errno > 0)? US strerror(rti->basic_errno) :
+          (rti->message == NULL)?
+          US"unknown error" : string_printing(rti->message);
+        message_length = Ustrlen(message);
+        if (message_length > 150) message_length = 150;
+
+        /* Read a retry record from the database or construct a new one.
+        Ignore an old one if it is too old since it was last updated. */
+
+        retry_record = dbfn_read(dbm_file, rti->key);
+        if (retry_record != NULL &&
+            now - retry_record->time_stamp > retry_data_expire)
+          retry_record = NULL;
+
+        if (retry_record == NULL)
+          {
+          retry_record = store_get(sizeof(dbdata_retry) + message_length);
+          message_space = message_length;
+          retry_record->first_failed = now;
+          retry_record->last_try = now;
+          retry_record->next_try = now;
+          retry_record->expired = FALSE;
+          retry_record->text[0] = 0;      /* just in case */
+          }
+        else message_space = Ustrlen(retry_record->text);
+
+        /* Compute how long this destination has been failing */
+
+        failing_interval = now - retry_record->first_failed;
+        DEBUG(D_retry) debug_printf("failing_interval=%d message_age=%d\n",
+          failing_interval, message_age);
+
+        /* For a non-host error, if the message has been on the queue longer
+        than the recorded time of failure, use the message's age instead. This
+        can happen when some messages can be delivered and others cannot; a
+        successful delivery will reset the first_failed time, and this can lead
+        to a failing message being retried too often. */
+
+        if ((rti->flags & rf_host) == 0 && message_age > failing_interval)
+          failing_interval = message_age;
+
+        /* Search for the current retry rule. The cutoff time of the
+        last rule is handled differently to the others. The rule continues
+        to operate for ever (the global maximum interval will eventually
+        limit the gaps) but its cutoff time determines when an individual
+        destination times out. If there are no retry rules, the destination
+        always times out, but we can't compute a retry time. */
+
+        final_rule = NULL;
+        for (rule = retry->rules; rule != NULL; rule = rule->next)
+          {
+          if (failing_interval <= rule->timeout) break;
+          final_rule = rule;
+          }
+
+        /* If there's an un-timed out rule, the destination has not
+        yet timed out, so the address as a whole has not timed out (but we are
+        interested in this only for the end address). Make sure the expired
+        flag is false (can be forced via fixdb from outside, but ensure it is
+        consistent with the rules whenever we go through here). */
+
+        if (rule != NULL)
+          {
+          retry_record->expired = FALSE;
+          }
+
+        /* Otherwise, set the retry timeout expired, and set the final rule
+        as the one from which to compute the next retry time. Subsequent
+        messages will fail immediately until the retry time is reached (unless
+        there are other, still active, retries). */
+
+        else
+          {
+          rule = final_rule;
+          retry_record->expired = TRUE;
+          if (addr == endaddr) timedout_count++;
+          }
+
+        /* There is a special case to consider when some messages get through
+        to a destination and others don't. This can happen locally when a
+        large message pushes a user over quota, and it can happen remotely
+        when a machine is on a dodgy Internet connection. The messages that
+        get through wipe the retry information, causing those that don't to
+        stay on the queue longer than the final retry time. In order to
+        avoid this, we check, using the time of arrival of the message, to
+        see if it has been on the queue for more than the final cutoff time,
+        and if so, cause this retry item to time out, and the retry time to
+        be set to "now" so that any subsequent messages in the same condition
+        also get tried. We search for the last rule onwards from the one that
+        is in use. If there are no retry rules for the item, rule will be null
+        and timedout_count will already have been updated.
+
+        This implements "timeout this rule if EITHER the host (or routing or
+        directing) has been failing for more than the maximum time, OR if the
+        message has been on the queue for more than the maximum time."
+
+        February 2006: It is possible that this code is no longer needed
+        following the change to the retry calculation to use the message age if
+        it is larger than the time since first failure. It may be that the
+        expired flag is always set when the other conditions are met. However,
+        this is a small bit of code, and it does no harm to leave it in place,
+        just in case. */
+
+        if (received_time <= retry_record->first_failed &&
+            addr == endaddr && !retry_record->expired && rule != NULL)
+          {
+          retry_rule *last_rule;
+          for (last_rule = rule;
+               last_rule->next != NULL;
+               last_rule = last_rule->next);
+          if (now - received_time > last_rule->timeout)
+            {
+            DEBUG(D_retry) debug_printf("on queue longer than maximum retry\n");
+            timedout_count++;
+            rule = NULL;
+            }
+          }
+
+        /* Compute the next try time from the rule, subject to the global
+        maximum, and update the retry database. If rule == NULL it means
+        there were no rules at all (and the timeout will be set expired),
+        or we have a message that is older than the final timeout. In this
+        case set the next retry time to now, so that one delivery attempt
+        happens for subsequent messages. */
+
+        if (rule == NULL) next_try = now; else
+          {
+          if (rule->rule == 'F') next_try = now + rule->p1;
+          else  /* rule = 'G' or 'H' */
+            {
+            int last_predicted_gap =
+              retry_record->next_try - retry_record->last_try;
+            int last_actual_gap = now - retry_record->last_try;
+            int lastgap = (last_predicted_gap < last_actual_gap)?
+              last_predicted_gap : last_actual_gap;
+            int next_gap = (lastgap * rule->p2)/1000;
+            if (rule->rule == 'G')
+              {
+              next_try = now + ((lastgap < rule->p1)? rule->p1 : next_gap);
+              }
+            else  /* The 'H' rule */
+              {
+              next_try = now + rule->p1;
+              if (next_gap > rule->p1)
+                next_try += random_number(next_gap - rule->p1)/2 +
+                  (next_gap - rule->p1)/2;
+              }
+            }
+          }
+
+        /* Impose a global retry max */
+
+        if (next_try - now > retry_interval_max)
+          next_try = now + retry_interval_max;
+
+        /* If the new message length is greater than the previous one, we
+        have to copy the record first. */
+
+        if (message_length > message_space)
+          {
+          dbdata_retry *newr = store_get(sizeof(dbdata_retry) + message_length);
+          memcpy(newr, retry_record, sizeof(dbdata_retry));
+          retry_record = newr;
+          }
+
+        /* Set up the retry record; message_length may be less than the string
+        length for very long error strings. */
+
+        retry_record->last_try = now;
+        retry_record->next_try = next_try;
+        retry_record->basic_errno = rti->basic_errno;
+        retry_record->more_errno = rti->more_errno;
+        Ustrncpy(retry_record->text, message, message_length);
+        retry_record->text[message_length] = 0;
+
+        DEBUG(D_retry)
+          {
+          int letter = retry_record->more_errno & 255;
+          debug_printf("Writing retry data for %s\n", rti->key);
+          debug_printf("  first failed=%d last try=%d next try=%d expired=%d\n",
+            (int)retry_record->first_failed, (int)retry_record->last_try,
+            (int)retry_record->next_try, retry_record->expired);
+          debug_printf("  errno=%d more_errno=", retry_record->basic_errno);
+          if (letter == 'A' || letter == 'M')
+            debug_printf("%d,%c", (retry_record->more_errno >> 8) & 255,
+              letter);
+          else
+            debug_printf("%d", retry_record->more_errno);
+          debug_printf(" %s\n", retry_record->text);
+          }
+
+        (void)dbfn_write(dbm_file, rti->key, retry_record,
+          sizeof(dbdata_retry) + message_length);
+        }                            /* Loop for each retry item */
+
+      /* If all the non-delete retry items are timed out, the address is
+      timed out, provided that we didn't skip any hosts because their retry
+      time was not reached (or because of hosts_max_try). */
+
+      if (update_count > 0 && update_count == timedout_count)
+        {
+        if (!testflag(endaddr, af_retry_skipped))
+          {
+          DEBUG(D_retry) debug_printf("timed out: all retries expired\n");
+          timed_out = TRUE;
+          }
+        else
+          {
+          DEBUG(D_retry)
+            debug_printf("timed out but some hosts were skipped\n");
+          }
+        }
+      }     /* Loop for an address and its parents */
+
+    /* If this is a deferred address, and retry processing was requested by
+    means of one or more retry items, and they all timed out, move the address
+    to the failed queue, and restart this loop without updating paddr.
+
+    If there were several addresses batched in the same remote delivery, only
+    the original top one will have host retry items attached to it, but we want
+    to handle all the same. Each will have a pointer back to its "top" address,
+    and they will now precede the item with the retries because addresses are
+    inverted when added to these final queues. We have saved information about
+    them in passing (below) so they can all be cut out at once. */
+
+    if (i == 2)   /* Handling defers */
+      {
+      if (endaddr->retries != NULL && timed_out)
+        {
+        if (last_first == endaddr) paddr = saved_paddr;
+        addr = *paddr;
+        *paddr = endaddr->next;
+
+        endaddr->next = *addr_failed;
+        *addr_failed = addr;
+
+        for (;; addr = addr->next)
+          {
+          setflag(addr, af_retry_timedout);
+          addr->message = (addr->message == NULL)? US"retry timeout exceeded" :
+            string_sprintf("%s: retry timeout exceeded", addr->message);
+          addr->user_message = (addr->user_message == NULL)?
+            US"retry timeout exceeded" :
+            string_sprintf("%s: retry timeout exceeded", addr->user_message);
+          log_write(0, LOG_MAIN, "** %s%s%s%s: retry timeout exceeded",
+            addr->address,
+           (addr->parent == NULL)? US"" : US" <",
+           (addr->parent == NULL)? US"" : addr->parent->address,
+           (addr->parent == NULL)? US"" : US">");
+
+          if (addr == endaddr) break;
+          }
+
+        continue;                       /* Restart from changed *paddr */
+        }
+
+      /* This address is to remain on the defer chain. If it has a "first"
+      pointer, save the pointer to it in case we want to fail the set of
+      addresses when we get to the first one. */
+
+      if (endaddr->first != last_first)
+        {
+        last_first = endaddr->first;
+        saved_paddr = paddr;
+        }
+      }
+
+    /* All cases (succeed, fail, defer left on queue) */
+
+    paddr = &(endaddr->next);         /* Advance to next address */
+    }                                 /* Loop for all addresses  */
+  }                                   /* Loop for succeed, fail, defer */
+
+/* Close and unlock the database */
+
+if (dbm_file != NULL) dbfn_close(dbm_file);
+
+DEBUG(D_retry) debug_printf("end of retry processing\n");
+}
+
+/* End of retry.c */
diff --git a/src/rewrite.c b/src/rewrite.c
new file mode 100644 (file)
index 0000000..f9d29c0
--- /dev/null
@@ -0,0 +1,828 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Functions concerned with rewriting headers */
+
+
+#include "exim.h"
+
+/* Names for testing rewriting */
+
+static const char *rrname[] = {
+  "  sender",
+  "    from",
+  "      to",
+  "      cc",
+  "     bcc",
+  "reply-to",
+  "env-from",
+  "  env-to"
+};
+
+/* Structure and table for finding source of address for debug printing */
+
+typedef struct where_list_block {
+  int bit;
+  const uschar *string;
+} where_list_block;
+
+static where_list_block where_list[] = {
+  { rewrite_sender,  CUS"sender:" },
+  { rewrite_from,    CUS"from:" },
+  { rewrite_to,      CUS"to:" },
+  { rewrite_cc,      CUS"cc:" },
+  { rewrite_bcc,     CUS"bcc:" },
+  { rewrite_replyto, CUS"reply-to:" },
+  { rewrite_envfrom, CUS"env-from" },
+  { rewrite_envto,   CUS"env-to" },
+  { rewrite_smtp,    CUS"smtp recipient" },
+  { rewrite_smtp|rewrite_smtp_sender, CUS"smtp sender" }
+};
+
+static int where_list_size = sizeof(where_list)/sizeof(where_list_block);
+
+
+
+/*************************************************
+*            Ensure an address is qualified      *
+*************************************************/
+
+/*
+Arguments:
+  s              address to check
+  is_recipient   TRUE if a recipient address; FALSE if a sender address
+
+Returns:         fully-qualified address
+*/
+
+uschar *
+rewrite_address_qualify(uschar *s, BOOL is_recipient)
+{
+return (parse_find_at(s) != NULL)? s :
+  string_sprintf("%s@%s", s,
+    is_recipient? qualify_domain_recipient : qualify_domain_sender);
+}
+
+
+
+/*************************************************
+*               Rewrite a single address         *
+*************************************************/
+
+/* The yield is the input address if there is no rewriting to be done. Assume
+the input is a valid address, except in the case of SMTP-time rewriting, which
+is handled specially. When this function is called while processing filter and
+forward files, the uid may be that of the user. Ensure it is reset while
+expanding a replacement, in case that involves file lookups.
+
+Arguments:
+  s              address to rewrite
+  flag           indicates where this address comes from; it must match the
+                   flags in the rewriting rule
+  whole          if not NULL, set TRUE if any rewriting rule contained the
+                   "whole" bit and it is a header that is being rewritten
+  add_header     if TRUE and rewriting occurs, add an "X-rewrote-xxx" header
+                   if headers are in existence; this should be TRUE only when
+                   a message is being received, not during delivery
+  name           name of header, for use when adding X-rewrote-xxxx
+  rewrite_rules  chain of rewriting rules
+
+Returns:         new address if rewritten; the input address if no change;
+                 for a header rewrite, if the "whole" bit is set, the entire
+                 rewritten address is returned, not just the active bit.
+*/
+
+uschar *
+rewrite_one(uschar *s, int flag, BOOL *whole, BOOL add_header, uschar *name,
+  rewrite_rule *rewrite_rules)
+{
+rewrite_rule *rule;
+uschar *yield = s;
+uschar *subject = s;
+uschar *domain = NULL;
+BOOL done = FALSE;
+int rule_number = 1;
+int yield_start = 0, yield_end = 0;
+
+if (whole != NULL) *whole = FALSE;
+
+/* Scan the rewriting rules */
+
+for (rule = rewrite_rules;
+     rule != NULL && !done;
+     rule_number++, rule = rule->next)
+  {
+  int start, end, pdomain;
+  int count = 0;
+  uschar *save_localpart, *save_domain;
+  uschar *error, *new, *newparsed;
+
+  /* Ensure that the flag matches the flags in the rule. */
+
+  if ((rule->flags & flag) == 0) continue;
+
+  /* Come back here for a repeat after a successful rewrite. We do this
+  only so many times. */
+
+  REPEAT_RULE:
+
+  /* If this is an SMTP-time rewrite, the pattern must be a regex and
+  the subject may have any structure. No local part or domain variables
+  can be set for the expansion. We expand the pattern in order to be consistent
+  with the other kinds of rewrite, where expansion happens inside
+  match_address_list(). */
+
+  if ((flag & rewrite_smtp) != 0)
+    {
+    uschar *key = expand_string(rule->key);
+    if (key == NULL)
+      {
+      if (!expand_string_forcedfail)
+        log_write(0, LOG_MAIN|LOG_PANIC, "failed to expand \"%s\" while "
+          "checking for SMTP rewriting: %s", rule->key, expand_string_message);
+      continue;
+      }
+    if (match_check_string(subject, key, 0, TRUE, FALSE, FALSE, NULL) != OK)
+      continue;
+    new = expand_string(rule->replacement);
+    }
+
+  /* All other rewrites expect the input to be a valid address, so local part
+  and domain variables can be set for expansion. For the first rule, to be
+  applied to this address, domain will be NULL and needs to be set. */
+
+  else
+    {
+    if (domain == NULL) domain = Ustrrchr(subject, '@') + 1;
+
+    /* Use the general function for matching an address against a list (here
+    just one item, so use the "impossible value" separator UCHAR_MAX+1). */
+
+    if (match_address_list(subject, FALSE, TRUE, &(rule->key), NULL, 0,
+        UCHAR_MAX + 1, NULL) != OK)
+      continue;
+
+    /* The source address matches, and numerical variables have been
+    set up. If the replacement string consists of precisely "*" then no
+    rewriting is required for this address - the behaviour is as for "fail"
+    in the replacement expansion, but assuming the quit flag. */
+
+    if (Ustrcmp(rule->replacement, "*") == 0) break;
+
+    /* Otherwise, expand the replacement string. Set $local_part and $domain to
+    the appropriate values, restoring whatever value they previously had
+    afterwards. */
+
+    save_localpart = deliver_localpart;
+    save_domain = deliver_domain;
+
+    /* We have subject pointing to "localpart@domain" and domain pointing to
+    the domain. Temporarily terminate the local part so that it can be
+    set up as an expansion variable */
+
+    domain[-1] = 0;
+    deliver_localpart = subject;
+    deliver_domain = domain;
+
+    new = expand_string(rule->replacement);
+
+    domain[-1] = '@';
+    deliver_localpart = save_localpart;
+    deliver_domain = save_domain;
+    }
+
+  /* If the expansion failed with the "forcedfail" flag, don't generate
+  an error - just give up on this rewriting rule. If the "q" flag is set,
+  give up altogether. For other expansion failures we have a configuration
+  error. */
+
+  if (new == NULL)
+    {
+    if (expand_string_forcedfail)
+      { if ((rule->flags & rewrite_quit) != 0) break; else continue; }
+    log_write(0, LOG_MAIN|LOG_PANIC, "Expansion of %s failed while rewriting: "
+      "%s", rule->replacement, expand_string_message);
+    break;
+    }
+
+  /* Check the what has been generated is a valid RFC 2822 address. Only
+  envelope from or SMTP sender is permitted to be rewritten as <>.*/
+
+  newparsed = parse_extract_address(new, &error, &start, &end, &pdomain,
+    flag == rewrite_envfrom || flag == (rewrite_smtp|rewrite_smtp_sender));
+
+  if (newparsed == NULL)
+    {
+    log_write(0, LOG_MAIN|LOG_PANIC, "Rewrite of %s yielded unparseable "
+      "address: %s in address %s", subject, error, new);
+    break;   /* Give up on this address */
+    }
+
+  /* A non-null unqualified address can be qualified if requested. Otherwise,
+  this is an error unless it's the empty address in circumstances where that is
+  permitted. */
+
+  if (pdomain == 0 && (*newparsed != 0 ||
+      (flag != rewrite_envfrom && flag != (rewrite_smtp|rewrite_smtp_sender))))
+    {
+    if ((rule->flags & rewrite_qualify) != 0)
+      {
+      newparsed = rewrite_address_qualify(newparsed, TRUE);
+      new = string_sprintf("%.*s%s%.*s", start, new, newparsed,
+        Ustrlen(new) - end, new + end);
+      end = start + Ustrlen(newparsed);
+      }
+    else
+      {
+      log_write(0, LOG_MAIN|LOG_PANIC, "Rewrite of %s yielded unqualified "
+        "address \"%s\"", subject, new);
+      break;   /* Give up on this address */
+      }
+    }
+
+  /* We have a validly rewritten address */
+
+  if ((log_write_selector & L_address_rewrite) != 0 ||
+      (debug_selector & D_rewrite) != 0)
+    {
+    int i;
+    const uschar *where = CUS"?";
+
+    for (i = 0; i < where_list_size; i++)
+      {
+      if (flag == where_list[i].bit)
+        {
+        where = where_list[i].string;
+        break;
+        }
+      }
+    log_write(L_address_rewrite,
+           LOG_MAIN, "\"%s\" from %s rewritten as \"%s\" by rule %d",
+           yield, where, new, rule_number);
+    }
+
+  /* A header will only actually be added if header_last is non-NULL,
+  i.e. during message reception or delivery, but add_header should not
+  be set TRUE during delivery, as otherwise multiple instances of the header
+  can fill up the -H file and make it embarrassingly large. We don't need
+  to set header_rewritten because the -H file always gets written at the end
+  of message reception. */
+
+  if (add_header)
+    header_add(htype_old, "X-rewrote-%s: %s\n", name, subject);
+
+  /* Handle the case when replacement of the whole address is possible.
+  This happens only when whole is not NULL and we are rewriting a header.
+  If *whole is already TRUE it means that a previous rule had the w
+  flag set and so we must preserve the non-active portion of the current
+  subject unless the current rule also has the w flag set. */
+
+  if (whole != NULL && (flag & rewrite_all_headers) != 0)
+    {
+    /* Current rule has the w flag set. We must ensure the phrase parts
+    are syntactically valid if they are present. */
+
+    if ((rule->flags & rewrite_whole) != 0)
+      {
+      if (start > 0 && new[start-1] == '<')
+        {
+        uschar *p1 = new + start - 1;
+        uschar *p2 = new + end + 1;
+        uschar *pf1, *pf2;
+        uschar buff1[256], buff2[256];
+
+        while (p1 > new && p1[-1] == ' ') p1--;
+        pf1 = parse_fix_phrase(new, p1 - new, buff1, sizeof(buff1));
+        while (*p2 == ' ') p2++;
+        pf2 = parse_fix_phrase(p2, Ustrlen(p2), buff2, sizeof(buff2));
+
+        /* Note that pf1 and pf2 are NOT necessarily buff1 and buff2. For
+        a non-RFC 2047 phrase that does not need to be RFC 2822 quoted, they
+        will be buff1+1 and buff2+1. */
+
+        start = Ustrlen(pf1) + start + new - p1;
+        end = start + Ustrlen(newparsed);
+        new = string_sprintf("%s%.*s%s", pf1, p2 - p1, p1, pf2);
+        }
+
+      /* Now accept the whole thing */
+
+      yield = new;
+      yield_start = start;
+      yield_end = end;
+      subject = newparsed;
+      *whole = TRUE;
+      }
+
+    /* Current rule does not have the w flag set; if not previously
+    done any whole rewriting, behave in non-whole manner. */
+
+    else if (!*whole) goto NEVER_WHOLE;
+
+    /* Current rule does not have the w flag set, but a previous
+    rule did rewrite the whole address. Thus yield and subject will be
+    different. Preserve the previous non-active part of the address. */
+
+    else
+      {
+      subject = newparsed;
+      new = string_sprintf("%.*s%s%n%s",
+         yield_start, yield, subject, &end, yield + yield_end);
+      yield_end = end;
+      yield = new;
+      }
+    }
+
+  /* Rule just rewrites active part, or handling an envelope. This
+  code is obeyed only when all rules so far have not done "whole"
+  replacement. */
+
+  else
+    {
+    NEVER_WHOLE:
+    subject = yield = newparsed;
+    }
+
+  domain = NULL;    /* Reset for next rule */
+
+  /* If no further rewrites are to be done, set the done flag. This allows
+  repeats of the current rule if configured before breaking the loop. */
+
+  if ((rule->flags & rewrite_quit) != 0) done = TRUE;
+
+  /* Allow the current rule to be applied up to 10 times if
+  requested. */
+
+  if ((rule->flags & rewrite_repeat) != 0)
+    {
+    if (count++ < 10) goto REPEAT_RULE;
+    log_write(0, LOG_MAIN|LOG_PANIC, "rewrite rule repeat ignored after 10 "
+      "times");
+    }
+  }
+
+/* Unset expansion numeric variables, and that's it. */
+
+expand_nmax = -1;
+return yield;
+}
+
+
+
+/*************************************************
+*         Ensure qualification and rewrite       *
+*************************************************/
+
+/* This function is called for envelope addresses, the boolean specifying
+whether a recipient or a sender. It must first of all ensure the address is
+fully qualified, and then apply any relevant re-writing rules. The add-header
+flag causes a header to be added, recording the old address. This is marked
+"old", so that it is never transported anywhere; it exists for local checking
+and debugging purposes.
+
+Arguments:
+  s              the address to be considered
+  is_recipient   TRUE for recipient addresses; FALSE otherwise
+  add_header     add "X-rewrote-xxx" header when rewriting; this is
+                   set TRUE only for calls from the reception functions
+  rewrite_rules  points to chain of rewrite rules
+  existflags     bits indicating which headers there are rewrites for
+                 (just an optimisation)
+
+Returns:         possibly rewritten address
+*/
+
+uschar *
+rewrite_address(uschar *s, BOOL is_recipient, BOOL add_header,
+  rewrite_rule *rewrite_rules, int existflags)
+{
+int flag = is_recipient? rewrite_envto : rewrite_envfrom;
+s = rewrite_address_qualify(s, is_recipient);
+if ((existflags & flag) != 0)
+  {
+  uschar *new = rewrite_one(s, flag, NULL, add_header, is_recipient?
+    US"original-recipient" : US"sender", rewrite_rules);
+  if (new != s) s = new;
+  }
+return s;
+}
+
+
+
+/*************************************************
+*    Qualify and possibly rewrite one header     *
+*************************************************/
+
+/* This is called only from rewrite_header() below, either when reading a
+message. or when routing, in order to rewrite addresses that get changed by a
+router. This is normally the addition of full qualification to a partial
+domain. The first rewriting rule in this case is "change routed_old into
+routed_new", and it applies to all header lines that contain addresses. Then
+header-specific rewriting rules are applied.
+
+Before rewriting can be done, addresses without domains have to be qualified.
+This should only be done for messages from "local" senders. This is a difficult
+concept to pin down, what with the use of SMTP both as a submission and as a
+transmission protocol. Exim normally requires incoming SMTP to contain fully-
+qualified addresses, but there are options to permit unqualified ones from
+certain hosts. For those hosts only, addresses in headers can also be
+qualified. For other hosts, unqualified addresses in headers do not get touched
+in any way. For locally sourced messages, unqualified addresses always get
+qualified, except when -bnq is used to explicitly suppress this.
+
+Arguments:
+  h              pointer to header line block
+  flag           indicates which header this is
+  routed_old     if not NULL, this is a rewrite caused by a router, changing
+                   this domain into routed_new
+  routed_new     new routed domain if routed_old is not NULL
+  rewrite_rules  points to chain of rewriting rules
+  existflags     bits indicating which rewrites exist
+  replace        if TRUE, insert the new header in the chain after the old
+                   one, and mark the old one "replaced"
+
+Returns:         NULL if header unchanged; otherwise the rewritten header
+*/
+
+static header_line *
+rewrite_one_header(header_line *h, int flag, uschar *routed_old,
+  uschar *routed_new, rewrite_rule *rewrite_rules, int existflags, BOOL replace)
+{
+int lastnewline = 0;
+header_line *newh = NULL;
+void *function_reset_point = store_get(0);
+uschar *s = Ustrchr(h->text, ':') + 1;
+while (isspace(*s)) s++;
+
+DEBUG(D_rewrite)
+  debug_printf("rewrite_one_header: type=%c:\n  %s", h->type, h->text);
+
+parse_allow_group = TRUE;     /* Allow group syntax */
+
+/* Loop for multiple addresses in the header. We have to go through them all
+in case any need qualifying, even if there's no rewriting. Pathological headers
+may have thousands of addresses in them, so cause the store to be reset for
+any that don't actually get rewritten. We also play silly games for those that
+_are_ rewritten so as to avoid runaway store usage for these kinds of header.
+We want to avoid keeping store for any intermediate versions. */
+
+while (*s != 0)
+  {
+  uschar *sprev;
+  uschar *ss = parse_find_address_end(s, FALSE);
+  uschar *recipient, *new, *errmess;
+  void *loop_reset_point = store_get(0);
+  BOOL changed = FALSE;
+  int terminator = *ss;
+  int start, end, domain;
+
+  /* Temporarily terminate the string at this point, and extract the
+  operative address within. Then put back the terminator and prepare for
+  the next address, saving the start of the old one. */
+
+  *ss = 0;
+  recipient = parse_extract_address(s,&errmess,&start,&end,&domain,FALSE);
+  *ss = terminator;
+  sprev = s;
+  s = ss + (terminator? 1:0);
+  while (isspace(*s)) s++;
+
+  /* There isn't much we can do for syntactic disasters at this stage.
+  Pro tem (possibly for ever) ignore them. */
+
+  if (recipient == NULL)
+    {
+    store_reset(loop_reset_point);
+    continue;
+    }
+
+  /* If routed_old is not NULL, this is a rewrite caused by a router,
+  consisting of changing routed_old into routed_new, and applying to all
+  headers. If the header address has no domain, it is excluded, since a router
+  rewrite affects domains only. The new value should always be fully qualified,
+  but it may be something that has an explicit re-write rule set, so we need to
+  check the configured rules subsequently as well. (Example: there's an
+  explicit rewrite turning *.foo.com into foo.com, and an address is supplied
+  as abc@xyz, which the DNS lookup turns into abc@xyz.foo.com). However, if no
+  change is made here, don't bother carrying on. */
+
+  if (routed_old != NULL)
+    {
+    if (domain <= 0 || strcmpic(recipient+domain, routed_old) != 0) continue;
+    recipient[domain-1] = 0;
+    new = string_sprintf("%s@%s", recipient, routed_new);
+    DEBUG(D_rewrite)
+      {
+      recipient[domain-1] = '@';
+      debug_printf("%s rewritten by router as %s\n", recipient, new);
+      }
+    recipient = new;
+    changed = TRUE;
+    }
+
+  /* This is not a router-inspired rewrite. Ensure the address is fully
+  qualified if that is permitted. If an unqualified address was received
+  from a host that isn't listed, do not continue rewriting this address.
+  Sender, From or Reply-To headers are treated as senders, the rest as
+  recipients. This matters only when there are different qualify strings. */
+
+  else
+    {
+    BOOL is_recipient =
+      (flag & (rewrite_sender | rewrite_from | rewrite_replyto)) == 0;
+    new = rewrite_address_qualify(recipient, is_recipient);
+    changed = (new != recipient);
+    recipient = new;
+
+    /* Can only qualify if permitted; if not, no rewrite. */
+
+    if (changed && ((is_recipient && !allow_unqualified_recipient) ||
+                    (!is_recipient && !allow_unqualified_sender)))
+      {
+      store_reset(loop_reset_point);
+      continue;
+      }
+    }
+
+  /* If there are rewrite rules for this type of header, apply
+  them. This test is just for efficiency, to save scanning the rules
+  in cases when nothing is going to change. If any rewrite rule had the
+  "whole" flag set, adjust the pointers so that the whole address gets
+  replaced, except possibly a final \n. */
+
+  if ((existflags & flag) != 0)
+    {
+    BOOL whole;
+    new = rewrite_one(recipient, flag, &whole, FALSE, NULL, rewrite_rules);
+    if (new != recipient)
+      {
+      changed = TRUE;
+      if (whole)
+        {
+        start = 0;
+        end = ss - sprev;
+        if (sprev[end-1] == '\n') end--;
+        }
+      }
+    }
+
+  /* If nothing has changed, lose all dynamic store obtained in this loop, and
+  move on to the next address. We can't reset to the function start store
+  point, because we may have a rewritten line from a previous time round the
+  loop. */
+
+  if (!changed) store_reset(loop_reset_point);
+
+  /* If the address has changed, create a new header containing the
+  rewritten address. We do not need to set the chain pointers at this
+  stage. We want to avoid using more and more memory if the header is very long
+  and contains lots and lots of rewritten addresses. Therefore, we build the
+  new text string in malloc store, then at the end we reset dynamic store
+  before copying the new header to a new block (and then freeing the malloc
+  block). The header must end up in dynamic store so that it's freed at the end
+  of receiving a message. */
+
+  else
+    {
+    int remlen;
+    int newlen = Ustrlen(new);
+    int oldlen = end - start;
+
+    header_line *prev = (newh == NULL)? h : newh;
+    uschar *newt = store_malloc(prev->slen - oldlen + newlen + 4);
+    uschar *newtstart = newt;
+
+    int type = prev->type;
+    int slen = prev->slen - oldlen + newlen;
+
+    /* Build the new header text by copying the old and putting in the
+    replacement. This process may make the header substantially longer
+    than it was before - qualification of a list of bare addresses can
+    often do this - so we stick in a newline after the re-written address
+    if it has increased in length and ends more than 40 characters in. In
+    fact, the code is not perfect, since it does not scan for existing
+    newlines in the header, but it doesn't seem worth going to that
+    amount of trouble. */
+
+    Ustrncpy(newt, prev->text, sprev - prev->text + start);
+    newt += sprev - prev->text + start;
+    *newt = 0;
+    Ustrcat(newt, new);
+    newt += newlen;
+    remlen = s - (sprev + end);
+    if (remlen > 0)
+      {
+      Ustrncpy(newt, sprev + end, remlen);
+      newt += remlen;
+      *newt = 0;
+      }
+
+    /* Must check that there isn't a newline here anyway; in particular, there
+    will be one at the very end of the header, where we DON'T want to insert
+    another one! The pointer s has been skipped over white space, so just
+    look back to see if the last non-space-or-tab was a newline. */
+
+    if (newlen > oldlen && newt - newtstart - lastnewline > 40)
+      {
+      uschar *p = s - 1;
+      while (p >= prev->text && (*p == ' ' || *p == '\t')) p--;
+      if (*p != '\n')
+        {
+        lastnewline = newt - newtstart;
+        Ustrcat(newt, "\n\t");
+        slen += 2;
+        }
+      }
+
+    /* Finally, the remaining unprocessed addresses, if any. */
+
+    Ustrcat(newt, s);
+
+    DEBUG(D_rewrite) debug_printf("newlen=%d newtype=%c newtext:\n%s",
+      slen, type, newtstart);
+
+    /* Compute the length of the rest of the header line before we possibly
+    flatten a previously rewritten copy. */
+
+    remlen = (s - prev->text) - oldlen + newlen;
+
+    /* We have the new text in a malloc block. That enables us to release all
+    the memory that has been used, back to the point at which the function was
+    entered. Then set up a new header in dynamic store. This will override a
+    rewritten copy from a previous time round this loop. */
+
+    store_reset(function_reset_point);
+    newh = store_get(sizeof(header_line));
+    newh->type = type;
+    newh->slen = slen;
+    newh->text = string_copyn(newtstart, slen);
+    store_free(newtstart);
+
+    /* Set up for scanning the rest of the header */
+
+    s = newh->text + remlen;
+    DEBUG(D_rewrite) debug_printf("remainder: %s", (*s == 0)? US"\n" : s);
+    }
+  }
+
+parse_allow_group = FALSE;  /* Reset group flags */
+parse_found_group = FALSE;
+
+/* If a rewrite happened and "replace" is true, put the new header into the
+chain following the old one, and mark the old one as replaced. */
+
+if (newh != NULL && replace)
+  {
+  newh->next = h->next;
+  if (newh->next == NULL) header_last = newh;
+  h->type = htype_old;
+  h->next = newh;
+  }
+
+return newh;
+}
+
+
+
+
+/*************************************************
+*              Rewrite a header line             *
+*************************************************/
+
+/* This function may be passed any old header line. It must detect those which
+contain addresses, then then apply any rewriting rules that apply. If
+routed_old is NULL, only the configured rewriting rules are consulted.
+Otherwise, the rewriting rule is "change routed_old into routed_new", and it
+applies to all header lines that contain addresses. Then header-specific
+rewriting rules are applied.
+
+The old header line is flagged as "old". Old headers are saved on the spool for
+debugging but are never sent to any recipients.
+
+Arguments:
+  h              header line to rewrite
+  routed_old     if not NULL, this is a rewrite caused by a router, changing
+                   this domain into routed_new
+  routed_new     new routed domain if routed_old is not NULL
+  rewrite_rules  points to chain of rewrite rules
+  existflags     bits indicating which rewrites exist
+  replace        if TRUE, the new header is inserted into the header chain
+                    after the old one, and the old one is marked replaced
+
+Returns:         NULL if header unchanged; otherwise the rewritten header
+*/
+
+header_line *
+rewrite_header(header_line *h, uschar *routed_old, uschar *routed_new,
+  rewrite_rule *rewrite_rules, int existflags, BOOL replace)
+{
+switch (h->type)
+  {
+  case htype_sender:
+  return rewrite_one_header(h, rewrite_sender, routed_old, routed_new,
+    rewrite_rules, existflags, replace);
+
+  case htype_from:
+  return rewrite_one_header(h, rewrite_from, routed_old, routed_new,
+    rewrite_rules, existflags, replace);
+
+  case htype_to:
+  return rewrite_one_header(h, rewrite_to, routed_old, routed_new,
+    rewrite_rules, existflags, replace);
+
+  case htype_cc:
+  return rewrite_one_header(h, rewrite_cc, routed_old, routed_new,
+    rewrite_rules, existflags, replace);
+
+  case htype_bcc:
+  return rewrite_one_header(h, rewrite_bcc, routed_old, routed_new,
+    rewrite_rules, existflags, replace);
+
+  case htype_reply_to:
+  return rewrite_one_header(h, rewrite_replyto, routed_old, routed_new,
+    rewrite_rules, existflags, replace);
+  }
+
+return NULL;
+}
+
+
+
+/************************************************
+*            Test rewriting rules               *
+************************************************/
+
+/* Called from the mainline as a result of the -brw option. Test the
+address for all possible cases.
+
+Argument: the address to test
+Returns:  nothing
+*/
+
+void rewrite_test(uschar *s)
+{
+uschar *recipient, *error;
+int i, start, end, domain;
+BOOL done_smtp = FALSE;
+
+if (rewrite_existflags == 0)
+  {
+  printf("No rewrite rules are defined\n");
+  return;
+  }
+
+/* Do SMTP rewrite only if a rule with the S flag exists. Allow <> by
+pretending it is a sender. */
+
+if ((rewrite_existflags & rewrite_smtp) != 0)
+  {
+  uschar *new = rewrite_one(s, rewrite_smtp|rewrite_smtp_sender, NULL, FALSE,
+    US"", global_rewrite_rules);
+  if (new != s)
+    {
+    if (*new == 0)
+      printf("    SMTP: <>\n");
+    else
+      printf("    SMTP: %s\n", new);
+    done_smtp = TRUE;
+    }
+  }
+
+/* Do the other rewrites only if a rule without the S flag exists */
+
+if ((rewrite_existflags & ~rewrite_smtp) == 0) return;
+
+/* Qualify if necessary before extracting the address */
+
+if (parse_find_at(s) == NULL)
+  s = string_sprintf("%s@%s", s, qualify_domain_recipient);
+
+recipient = parse_extract_address(s, &error, &start, &end, &domain, FALSE);
+
+if (recipient == NULL)
+  {
+  if (!done_smtp)
+    printf("Syntax error in %s\n%c%s\n", s, toupper(error[0]), error+1);
+  return;
+  }
+
+for (i = 0; i < 8; i++)
+  {
+  BOOL whole = FALSE;
+  int flag = 1 << i;
+  uschar *new = rewrite_one(recipient, flag, &whole, FALSE, US"",
+    global_rewrite_rules);
+  printf("%s: ", rrname[i]);
+  if (*new == 0)
+    printf("<>\n");
+  else if (whole || (flag & rewrite_all_headers) == 0)
+    printf("%s\n", CS new);
+  else printf("%.*s%s%s\n", start, s, new, s+end);
+  }
+}
+
+/* End of rewrite.c */
diff --git a/src/rfc2047.c b/src/rfc2047.c
new file mode 100644 (file)
index 0000000..128e6b9
--- /dev/null
@@ -0,0 +1,349 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* This file contains a function for decoding message header lines that may
+contain encoded "words" according to the rules described in
+
+  RFC-2047 at http://www.ietf.org/rfc/rfc2047.txt
+
+The function is a rewritten version of code created by Norihisa Washitake.
+The original could be used both inside Exim (as part of a patch) or in a
+freestanding form. The original contained some built-in code conversions; I
+have chosen only to do code conversions if iconv() is supported by the OS.
+Because there were quite a lot of hacks to be done, for a variety of reasons,
+I rewrote the code.
+
+You can find the latest version of the original library at
+
+  http://washitake.com/mail/exim/mime/
+
+The code below is almost completely unlike the original. */
+
+
+#include "exim.h"
+
+
+/*************************************************
+*                Do a QP conversion              *
+*************************************************/
+
+/* This function decodes "quoted printable" into bytes.
+
+Arguments:
+  string      the string that includes QP escapes
+  ptrptr      where to return pointer to the decoded string
+
+Returns:      the length of the decoded string, or -1 on failure
+*/
+
+static int
+rfc2047_qpdecode(uschar *string, uschar **ptrptr)
+{
+int len = 0;
+uschar *ptr;
+
+ptr = *ptrptr = store_get(Ustrlen(string) + 1);  /* No longer than this */
+
+while (*string != 0)
+  {
+  register int ch = *string++;
+
+  if (ch == '_') *ptr++ = ' ';
+  else if (ch == '=')
+    {
+    int a = *string;
+    int b = (a == 0)? 0 : string[1];
+    if (!isxdigit(a) || !isxdigit(b)) return -1;  /* Bad QP string */
+    *ptr++ = ((Ustrchr(hex_digits, tolower(a)) - hex_digits) << 4) +
+               Ustrchr(hex_digits, tolower(b)) - hex_digits;
+    string += 2;
+    }
+  else if (ch == ' ' || ch == '\t') return -1;    /* Whitespace is illegal */
+  else *ptr++ = ch;
+
+  len++;
+  }
+
+*ptr = 0;
+return len;
+}
+
+
+
+/*************************************************
+*            Decode next MIME word               *
+*************************************************/
+
+/* Scan a string to see if a MIME word exists; pass back the separator
+points in the string.
+
+Arguments:
+  string     subject string
+  lencheck   TRUE to enforce maximum length check
+  q1ptr      pass back address of first question mark
+  q2ptr      pass back address of second question mark
+  endptr     pass back address of final ?=
+  dlenptr    pass back length of decoded string
+  dptrptr    pass back pointer to decoded string
+
+Returns:     address of =? or NULL if not present
+*/
+
+static uschar *
+decode_mimeword(uschar *string, BOOL lencheck, uschar **q1ptr, uschar **q2ptr,
+  uschar **endptr, size_t *dlenptr, uschar **dptrptr)
+{
+uschar *mimeword;
+for (;; string = mimeword + 2)
+  {
+  int encoding;
+  int dlen = -1;
+
+  if ((mimeword = Ustrstr(string, "=?"))  == NULL ||
+      (*q1ptr = Ustrchr(mimeword+2, '?')) == NULL ||
+      (*q2ptr = Ustrchr(*q1ptr+1, '?')) == NULL ||
+      (*endptr = Ustrstr(*q2ptr+1, "?=")) == NULL) return NULL;
+
+  /* We have found =?xxx?xxx?xxx?= in the string. Optionally check the
+  length, and that the second field is just one character long. If not,
+  continue the loop to search again. We must start just after the initial =?
+  because we might have found =?xxx=?xxx?xxx?xxx?=. */
+
+  if ((lencheck && *endptr - mimeword > 73) || *q2ptr - *q1ptr != 2) continue;
+
+  /* Get the encoding letter, and decode the data string. */
+
+  encoding = toupper((*q1ptr)[1]);
+  **endptr = 0;
+  if (encoding == 'B')
+    dlen = auth_b64decode(*q2ptr+1, dptrptr);
+  else if (encoding == 'Q')
+    dlen = rfc2047_qpdecode(*q2ptr+1, dptrptr);
+  **endptr = '?';   /* restore */
+
+  /* If the decoding succeeded, we are done. Set the length of the decoded
+  string, and pass back the initial pointer. Otherwise, the loop continues. */
+
+  if (dlen >= 0)
+    {
+    *dlenptr = (size_t)dlen;
+    return mimeword;
+    }
+  }
+
+/* Control should never actually get here */
+}
+
+
+
+/*************************************************
+*    Decode and convert an RFC 2047 string       *
+*************************************************/
+
+/* There are two functions defined here. The original one was rfc2047_decode()
+and it was documented in the local_scan() interface. I needed to add an extra
+argument for use by expand_string(), so I created rfc2047_decode2() for that
+purpose. The original function became a stub that just supplies NULL for the
+new argument (sizeptr).
+
+An RFC 2047-encoded string may contain one or more "words", each of the
+form  =?...?.?...?=  with the first ... specifying the character code, the
+second being Q (for quoted printable) or B for Base64 encoding. The third ...
+is the actual data.
+
+This function first decodes each "word" into bytes from the Q or B encoding.
+Then, if provided with the name of a charset encoding, and if iconv() is
+available, it attempts to translate the result to the named character set.
+If this fails, the binary string is returned with an error message.
+
+If a binary zero is encountered in the decoded string, it is replaced by the
+contents of the zeroval argument. For use with Exim headers, the value must not
+be 0 because they are handled as zero-terminated strings. When zeroval==0,
+lenptr should not be NULL.
+
+Arguments:
+    string       the subject string
+    lencheck     TRUE to enforce maximum MIME word length
+    target       the name of the target encoding for MIME words, or NULL for
+                   no charset translation
+    zeroval      the value to use for binary zero bytes
+    lenptr       if not NULL, the length of the result is returned via
+                   this variable
+    sizeptr      if not NULL, the length of a new store block in which the
+                   result is built is placed here; if no new store is obtained,
+                   the value is not changed
+    error        for error messages; NULL if no problem; this can be set
+                   when the yield is non-NULL if there was a charset
+                   translation problem
+
+Returns:         the decoded, converted string, or NULL on error; if there are
+                   no MIME words in the string, the original string is returned
+*/
+
+uschar *
+rfc2047_decode2(uschar *string, BOOL lencheck, uschar *target, int zeroval,
+  int *lenptr, int *sizeptr, uschar **error)
+{
+int ptr = 0;
+int size = Ustrlen(string);
+size_t dlen;
+uschar *dptr, *yield;
+uschar *mimeword, *q1, *q2, *endword;
+
+*error = NULL;
+mimeword = decode_mimeword(string, lencheck, &q1, &q2, &endword, &dlen, &dptr);
+
+if (mimeword == NULL)
+  {
+  if (lenptr != NULL) *lenptr = size;
+  return string;
+  }
+
+/* Scan through the string, decoding MIME words and copying intermediate text,
+building the result as we go. The result may be longer than the input if it is
+translated into a multibyte code such as UTF-8. That's why we use the dynamic
+string building code. */
+
+yield = store_get(++size);
+
+while (mimeword != NULL)
+  {
+
+  #if HAVE_ICONV
+  iconv_t icd = (iconv_t)(-1);
+  #endif
+
+  if (mimeword != string)
+    yield = string_cat(yield, &size, &ptr, string, mimeword - string);
+
+  /* Do a charset translation if required. This is supported only on hosts
+  that have the iconv() function. Translation errors set error, but carry on,
+  using the untranslated data. If there is more than one error, the message
+  passed back refers to the final one. We use a loop to cater for the case
+  of long strings - the RFC puts limits on the length, but it's best to be
+  robust. */
+
+  #if HAVE_ICONV
+  *q1 = 0;
+  if (target != NULL && strcmpic(target, mimeword+2) != 0)
+    {
+    icd = iconv_open(CS target, CS(mimeword+2));
+
+    if (icd == (iconv_t)(-1))
+      {
+      *error = string_sprintf("iconv_open(\"%s\", \"%s\") failed: %s%s",
+        target, mimeword+2, strerror(errno),
+        (errno == EINVAL)? " (maybe unsupported conversion)" : "");
+      }
+    }
+  *q1 = '?';
+  #endif
+
+  while (dlen > 0)
+    {
+    uschar *tptr = NULL;   /* Stops compiler warning */
+    int tlen = -1;
+
+    #if HAVE_ICONV
+    uschar tbuffer[256];
+    uschar *outptr = tbuffer;
+    size_t outleft = sizeof(tbuffer);
+
+    /* If translation is required, go for it. */
+
+    if (icd != (iconv_t)(-1))
+      {
+      (void)iconv(icd, (ICONV_ARG2_TYPE)(&dptr), &dlen, CSS &outptr, &outleft);
+
+      /* If outptr has been adjusted, there is some output. Set up to add it to
+      the output buffer. The function will have adjusted dptr and dlen. If
+      iconv() stopped because of an error, we'll pick it up next time when
+      there's no output.
+
+      If there is no output, we expect there to have been a translation
+      error, because we know there was at least one input byte. We leave the
+      value of tlen as -1, which causes the rest of the input to be copied
+      verbatim. */
+
+      if (outptr > tbuffer)
+        {
+        tptr = tbuffer;
+        tlen = outptr - tbuffer;
+        }
+      else
+        {
+        DEBUG(D_any) debug_printf("iconv error translating \"%.*s\" to %s: "
+        "%s\n", (int)(endword + 2 - mimeword), mimeword, target, strerror(errno));
+        }
+      }
+
+    #endif
+
+    /* No charset translation is happening or there was a translation error;
+    just set up the original as the string to be added, and mark it all used.
+    */
+
+    if (tlen == -1)
+      {
+      tptr = dptr;
+      tlen = dlen;
+      dlen = 0;
+      }
+
+    /* Deal with zero values; convert them if requested. */
+
+    if (zeroval != 0)
+      {
+      int i;
+      for (i = 0; i < tlen; i++)
+        if (tptr[i] == 0) tptr[i] = zeroval;
+      }
+
+    /* Add the new string onto the result */
+
+    yield = string_cat(yield, &size, &ptr, tptr, tlen);
+    }
+
+  #if HAVE_ICONV
+  if (icd != (iconv_t)(-1))  iconv_close(icd);
+  #endif
+
+  /* Update string past the MIME word; skip any white space if the next thing
+  is another MIME word. */
+
+  string = endword + 2;
+  mimeword = decode_mimeword(string, lencheck, &q1, &q2, &endword, &dlen, &dptr);
+  if (mimeword != NULL)
+    {
+    uschar *s = string;
+    while (isspace(*s)) s++;
+    if (s == mimeword) string = s;
+    }
+  }
+
+/* Copy the remaining characters of the string, zero-terminate it, and return
+the length as well if requested. */
+
+yield = string_cat(yield, &size, &ptr, string, Ustrlen(string));
+yield[ptr] = 0;
+if (lenptr != NULL) *lenptr = ptr;
+if (sizeptr != NULL) *sizeptr = size;
+return yield;
+}
+
+
+/* This is the stub that provides the original interface without the sizeptr
+argument. */
+
+uschar *
+rfc2047_decode(uschar *string, BOOL lencheck, uschar *target, int zeroval,
+  int *lenptr, uschar **error)
+{
+return rfc2047_decode2(string, lencheck, target, zeroval, lenptr, NULL, error);
+}
+
+/* End of rfc2047.c */
diff --git a/src/route.c b/src/route.c
new file mode 100644 (file)
index 0000000..6ba1d9f
--- /dev/null
@@ -0,0 +1,2012 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2014 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Functions concerned with routing, and the list of generic router options. */
+
+
+#include "exim.h"
+
+
+
+/* Generic options for routers, all of which live inside router_instance
+data blocks and which therefore have the opt_public flag set. */
+
+optionlist optionlist_routers[] = {
+  { "*expand_group",      opt_stringptr | opt_hidden | opt_public,
+                 (void *)(offsetof(router_instance, expand_gid)) },
+  { "*expand_more",       opt_stringptr | opt_hidden | opt_public,
+                 (void *)(offsetof(router_instance, expand_more)) },
+  { "*expand_unseen",     opt_stringptr | opt_hidden | opt_public,
+                 (void *)(offsetof(router_instance, expand_unseen)) },
+  { "*expand_user",       opt_stringptr | opt_hidden | opt_public,
+                 (void *)(offsetof(router_instance, expand_uid)) },
+  { "*set_group",         opt_bool | opt_hidden | opt_public,
+                 (void *)(offsetof(router_instance, gid_set)) },
+  { "*set_user",          opt_bool | opt_hidden | opt_public,
+                 (void *)(offsetof(router_instance, uid_set)) },
+  { "address_data",       opt_stringptr|opt_public,
+                 (void *)(offsetof(router_instance, address_data)) },
+  { "address_test",       opt_bool|opt_public,
+                 (void *)(offsetof(router_instance, address_test)) },
+#ifdef EXPERIMENTAL_BRIGHTMAIL
+  { "bmi_deliver_alternate",   opt_bool | opt_public,
+                 (void *)(offsetof(router_instance, bmi_deliver_alternate)) },
+  { "bmi_deliver_default",   opt_bool | opt_public,
+                 (void *)(offsetof(router_instance, bmi_deliver_default)) },
+  { "bmi_dont_deliver",   opt_bool | opt_public,
+                 (void *)(offsetof(router_instance, bmi_dont_deliver)) },
+  { "bmi_rule",           opt_stringptr|opt_public,
+                 (void *)(offsetof(router_instance, bmi_rule)) },
+#endif
+  { "cannot_route_message", opt_stringptr | opt_public,
+                 (void *)(offsetof(router_instance, cannot_route_message)) },
+  { "caseful_local_part", opt_bool | opt_public,
+                 (void *)(offsetof(router_instance, caseful_local_part)) },
+  { "check_local_user",   opt_bool | opt_public,
+                 (void *)(offsetof(router_instance, check_local_user)) },
+  { "condition",          opt_stringptr|opt_public|opt_rep_con,
+                 (void *)offsetof(router_instance, condition) },
+  { "debug_print",        opt_stringptr | opt_public,
+                 (void *)offsetof(router_instance, debug_string) },
+  { "disable_logging",    opt_bool | opt_public,
+                 (void *)offsetof(router_instance, disable_logging) },
+  { "domains",            opt_stringptr|opt_public,
+                 (void *)offsetof(router_instance, domains) },
+  { "driver",             opt_stringptr|opt_public,
+                 (void *)offsetof(router_instance, driver_name) },
+  #ifdef EXPERIMENTAL_DSN
+  { "dsn_lasthop",        opt_bool|opt_public,
+                 (void *)offsetof(router_instance, dsn_lasthop) },
+  #endif
+  { "errors_to",          opt_stringptr|opt_public,
+                 (void *)(offsetof(router_instance, errors_to)) },
+  { "expn",               opt_bool|opt_public,
+                 (void *)offsetof(router_instance, expn) },
+  { "fail_verify",        opt_bool_verify|opt_hidden|opt_public,
+                 (void *)offsetof(router_instance, fail_verify_sender) },
+  { "fail_verify_recipient", opt_bool|opt_public,
+                 (void *)offsetof(router_instance, fail_verify_recipient) },
+  { "fail_verify_sender", opt_bool|opt_public,
+                 (void *)offsetof(router_instance, fail_verify_sender) },
+  { "fallback_hosts",     opt_stringptr|opt_public,
+                 (void *)offsetof(router_instance, fallback_hosts) },
+  { "group",              opt_expand_gid | opt_public,
+                 (void *)(offsetof(router_instance, gid)) },
+  { "headers_add",        opt_stringptr|opt_public|opt_rep_str,
+                 (void *)offsetof(router_instance, extra_headers) },
+  { "headers_remove",     opt_stringptr|opt_public|opt_rep_str,
+                 (void *)offsetof(router_instance, remove_headers) },
+  { "ignore_target_hosts",opt_stringptr|opt_public,
+                 (void *)offsetof(router_instance, ignore_target_hosts) },
+  { "initgroups",         opt_bool | opt_public,
+                 (void *)(offsetof(router_instance, initgroups)) },
+  { "local_part_prefix",  opt_stringptr|opt_public,
+                 (void *)offsetof(router_instance, prefix) },
+  { "local_part_prefix_optional",opt_bool|opt_public,
+                 (void *)offsetof(router_instance, prefix_optional) },
+  { "local_part_suffix",  opt_stringptr|opt_public,
+                 (void *)offsetof(router_instance, suffix) },
+  { "local_part_suffix_optional",opt_bool|opt_public,
+                 (void *)offsetof(router_instance, suffix_optional) },
+  { "local_parts",        opt_stringptr|opt_public,
+                 (void *)offsetof(router_instance, local_parts) },
+  { "log_as_local",       opt_bool|opt_public,
+                 (void *)offsetof(router_instance, log_as_local) },
+  { "more",               opt_expand_bool|opt_public,
+                 (void *)offsetof(router_instance, more) },
+  { "pass_on_timeout",    opt_bool|opt_public,
+                 (void *)offsetof(router_instance, pass_on_timeout) },
+  { "pass_router",       opt_stringptr|opt_public,
+                 (void *)offsetof(router_instance, pass_router_name) },
+  { "redirect_router",    opt_stringptr|opt_public,
+                 (void *)offsetof(router_instance, redirect_router_name) },
+  { "require_files",      opt_stringptr|opt_public,
+                 (void *)offsetof(router_instance, require_files) },
+  { "retry_use_local_part", opt_bool|opt_public,
+                 (void *)offsetof(router_instance, retry_use_local_part) },
+  { "router_home_directory", opt_stringptr|opt_public,
+                 (void *)offsetof(router_instance, router_home_directory) },
+  { "self",               opt_stringptr|opt_public,
+                 (void *)(offsetof(router_instance, self)) },
+  { "senders",            opt_stringptr|opt_public,
+                 (void *)offsetof(router_instance, senders) },
+  #ifdef SUPPORT_TRANSLATE_IP_ADDRESS
+  { "translate_ip_address", opt_stringptr|opt_public,
+                 (void *)offsetof(router_instance, translate_ip_address) },
+  #endif
+  { "transport",          opt_stringptr|opt_public,
+                 (void *)offsetof(router_instance, transport_name) },
+  { "transport_current_directory", opt_stringptr|opt_public,
+                 (void *)offsetof(router_instance, current_directory) },
+  { "transport_home_directory", opt_stringptr|opt_public,
+                 (void *)offsetof(router_instance, home_directory) },
+  { "unseen",             opt_expand_bool|opt_public,
+                 (void *)offsetof(router_instance, unseen) },
+  { "user",               opt_expand_uid | opt_public,
+                 (void *)(offsetof(router_instance, uid)) },
+  { "verify",             opt_bool_verify|opt_hidden|opt_public,
+                 (void *)offsetof(router_instance, verify_sender) },
+  { "verify_only",        opt_bool|opt_public,
+                 (void *)offsetof(router_instance, verify_only) },
+  { "verify_recipient",   opt_bool|opt_public,
+                 (void *)offsetof(router_instance, verify_recipient) },
+  { "verify_sender",      opt_bool|opt_public,
+                 (void *)offsetof(router_instance, verify_sender) }
+};
+
+int optionlist_routers_size = sizeof(optionlist_routers)/sizeof(optionlist);
+
+
+
+/*************************************************
+*          Set router pointer from name          *
+*************************************************/
+
+/* This function is used for the redirect_router and pass_router options and
+called from route_init() below.
+
+Arguments:
+  r           the current router
+  name        new router name
+  ptr         where to put the pointer
+  after       TRUE if router must follow this one
+
+Returns:      nothing.
+*/
+
+static void
+set_router(router_instance *r, uschar *name, router_instance **ptr, BOOL after)
+{
+BOOL afterthis = FALSE;
+router_instance *rr;
+
+for (rr = routers; rr != NULL; rr = rr->next)
+  {
+  if (Ustrcmp(name, rr->name) == 0)
+    {
+    *ptr = rr;
+    break;
+    }
+  if (rr == r) afterthis = TRUE;
+  }
+
+if (rr == NULL)
+  log_write(0, LOG_PANIC_DIE|LOG_CONFIG,
+    "new_router \"%s\" not found for \"%s\" router", name, r->name);
+
+if (after && !afterthis)
+  log_write(0, LOG_PANIC_DIE|LOG_CONFIG,
+    "new_router \"%s\" does not follow \"%s\" router", name, r->name);
+}
+
+
+
+/*************************************************
+*             Initialize router list             *
+*************************************************/
+
+/* Read the routers section of the configuration file, and set up a chain of
+router instances according to its contents. Each router has generic options and
+may also have its own private options. This function is only ever called when
+routers == NULL. We use generic code in readconf to do the work. It will set
+values from the configuration file, and then call the driver's initialization
+function. */
+
+void
+route_init(void)
+{
+router_instance *r;
+
+readconf_driver_init(US"router",
+  (driver_instance **)(&routers),     /* chain anchor */
+  (driver_info *)routers_available,   /* available drivers */
+  sizeof(router_info),                /* size of info blocks */
+  &router_defaults,                   /* default values for generic options */
+  sizeof(router_instance),            /* size of instance block */
+  optionlist_routers,                 /* generic options */
+  optionlist_routers_size);
+
+for (r = routers; r != NULL; r = r->next)
+  {
+  uschar *s = r->self;
+
+  /* If log_as_local is unset, its overall default is FALSE. (The accept
+  router defaults it to TRUE.) */
+
+  if (r->log_as_local == TRUE_UNSET) r->log_as_local = FALSE;
+
+  /* Check for transport or no transport on certain routers */
+
+  if ((r->info->ri_flags & ri_yestransport) != 0 &&
+      r->transport_name == NULL &&
+      !r->verify_only)
+    log_write(0, LOG_PANIC_DIE|LOG_CONFIG, "%s router:\n  "
+      "a transport is required for this router", r->name);
+
+  if ((r->info->ri_flags & ri_notransport) != 0 &&
+       r->transport_name != NULL)
+    log_write(0, LOG_PANIC_DIE|LOG_CONFIG, "%s router:\n  "
+      "a transport must not be defined for this router", r->name);
+
+  /* The "self" option needs to be decoded into a code value and possibly a
+  new domain string and a rewrite boolean. */
+
+  if      (Ustrcmp(s, "freeze") == 0)    r->self_code = self_freeze;
+  else if (Ustrcmp(s, "defer") == 0)     r->self_code = self_defer;
+  else if (Ustrcmp(s, "send") == 0)      r->self_code = self_send;
+  else if (Ustrcmp(s, "pass") == 0)      r->self_code = self_pass;
+  else if (Ustrcmp(s, "fail") == 0)      r->self_code = self_fail;
+  else if (Ustrncmp(s, "reroute:", 8) == 0)
+    {
+    s += 8;
+    while (isspace(*s)) s++;
+    if (Ustrncmp(s, "rewrite:", 8) == 0)
+      {
+      r->self_rewrite = TRUE;
+      s += 8;
+      while (isspace(*s)) s++;
+      }
+    r->self = s;
+    r->self_code = self_reroute;
+    }
+
+  else log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s router:\n  "
+      "%s is not valid for the self option", r->name, s);
+
+  /* If any router has check_local_user set, default retry_use_local_part
+  TRUE; otherwise its default is FALSE. */
+
+  if (r->retry_use_local_part == TRUE_UNSET)
+    r->retry_use_local_part = r->check_local_user;
+
+  /* Build a host list if fallback hosts is set. */
+
+  host_build_hostlist(&(r->fallback_hostlist), r->fallback_hosts, FALSE);
+
+  /* Check redirect_router and pass_router are valid */
+
+  if (r->redirect_router_name != NULL)
+    set_router(r, r->redirect_router_name, &(r->redirect_router), FALSE);
+
+  if (r->pass_router_name != NULL)
+    set_router(r, r->pass_router_name, &(r->pass_router), TRUE);
+
+  #ifdef EXPERIMENTAL_DSN
+    DEBUG(D_route) {
+      if (r->dsn_lasthop == FALSE)
+        debug_printf("DSN: %s propagating DSN\n", r->name);
+      else
+        debug_printf("DSN: %s lasthop set\n", r->name);
+      }
+  #endif
+  }
+}
+
+
+
+/*************************************************
+*             Tidy up after routing              *
+*************************************************/
+
+/* Routers are entitled to keep hold of certain resources in their instance
+blocks so as to save setting them up each time. An example is an open file.
+Such routers must provide a tidyup entry point which is called when all routing
+is finished, via this function. */
+
+void
+route_tidyup(void)
+{
+router_instance *r;
+for (r = routers; r != NULL; r = r->next)
+  if (r->info->tidyup != NULL) (r->info->tidyup)(r);
+}
+
+
+
+/*************************************************
+*         Check local part for prefix            *
+*************************************************/
+
+/* This function is handed a local part and a list of possible prefixes; if any
+one matches, return the prefix length. A prefix beginning with '*' is a
+wildcard.
+
+Arguments:
+  local_part    the local part to check
+  prefixes      the list of prefixes
+
+Returns:        length of matching prefix or zero
+*/
+
+int
+route_check_prefix(uschar *local_part, uschar *prefixes)
+{
+int sep = 0;
+uschar *prefix;
+uschar *listptr = prefixes;
+uschar prebuf[64];
+
+while ((prefix = string_nextinlist(&listptr, &sep, prebuf, sizeof(prebuf)))
+       != NULL)
+  {
+  int plen = Ustrlen(prefix);
+  if (prefix[0] == '*')
+    {
+    uschar *p;
+    prefix++;
+    for (p = local_part + Ustrlen(local_part) - (--plen);
+         p >= local_part; p--)
+      if (strncmpic(prefix, p, plen) == 0) return plen + p - local_part;
+    }
+  else
+    if (strncmpic(prefix, local_part, plen) == 0) return plen;
+  }
+
+return 0;
+}
+
+
+
+/*************************************************
+*         Check local part for suffix            *
+*************************************************/
+
+/* This function is handed a local part and a list of possible suffixes;
+if any one matches, return the suffix length. A suffix ending with '*'
+is a wildcard.
+
+Arguments:
+  local_part    the local part to check
+  suffixes      the list of suffixes
+
+Returns:        length of matching suffix or zero
+*/
+
+int
+route_check_suffix(uschar *local_part, uschar *suffixes)
+{
+int sep = 0;
+int alen = Ustrlen(local_part);
+uschar *suffix;
+uschar *listptr = suffixes;
+uschar sufbuf[64];
+
+while ((suffix = string_nextinlist(&listptr, &sep, sufbuf, sizeof(sufbuf)))
+     != NULL)
+  {
+  int slen = Ustrlen(suffix);
+  if (suffix[slen-1] == '*')
+    {
+    uschar *p, *pend;
+    pend = local_part + alen - (--slen) + 1;
+    for (p = local_part; p < pend; p++)
+      if (strncmpic(suffix, p, slen) == 0) return alen - (p - local_part);
+    }
+  else
+    if (alen > slen && strncmpic(suffix, local_part + alen - slen, slen) == 0)
+      return slen;
+  }
+
+return 0;
+}
+
+
+
+
+/*************************************************
+*     Check local part, domain, or sender        *
+*************************************************/
+
+/* The checks in check_router_conditions() require similar code, so we use
+this function to save repetition.
+
+Arguments:
+  rname          router name for error messages
+  type           type of check, for error message
+  list           domains, local_parts, or senders list
+  anchorptr      -> tree for possibly cached items (domains)
+  cache_bits     cached bits pointer
+  listtype       MCL_DOMAIN for domain check
+                 MCL_LOCALPART for local part check
+                 MCL_ADDRESS for sender check
+  domloc         current domain, current local part, or NULL for sender check
+  ldata          where to put lookup data
+  caseless       passed on to match_isinlist()
+  perror         where to put an error message
+
+Returns:         OK     item is in list
+                 SKIP   item is not in list, router is to be skipped
+                 DEFER  lookup or other defer
+*/
+
+static int
+route_check_dls(uschar *rname, uschar *type, uschar *list, tree_node
+  **anchorptr, unsigned int *cache_bits, int listtype, uschar *domloc,
+  uschar **ldata, BOOL caseless, uschar **perror)
+{
+int rc;
+
+if (list == NULL) return OK;   /* Empty list always succeeds */
+
+DEBUG(D_route) debug_printf("checking %s\n", type);
+
+/* The domain and local part use the same matching function, whereas sender
+has its own code. */
+
+if (domloc != NULL)
+  {
+  rc = match_isinlist(domloc, &list, 0, anchorptr, cache_bits, listtype,
+    caseless, ldata);
+  }
+else
+  {
+  uschar *address = (sender_address == NULL)? US"" : sender_address;
+  rc = match_address_list(address, TRUE, TRUE, &list, cache_bits, -1, 0,
+    &sender_data);
+  }
+
+switch(rc)
+  {
+  case OK:
+  return OK;
+
+  case FAIL:
+  *perror = string_sprintf("%s router skipped: %s mismatch", rname, type);
+  DEBUG(D_route) debug_printf("%s\n", *perror);
+  return SKIP;
+
+  default:      /* Paranoia, and keeps compilers happy */
+  case DEFER:
+  *perror = string_sprintf("%s check lookup or other defer", type);
+  DEBUG(D_route) debug_printf("%s\n", *perror);
+  return DEFER;
+  }
+}
+
+
+
+/*************************************************
+*        Check access by a given uid/gid         *
+*************************************************/
+
+/* This function checks whether a given uid/gid has access to a given file or
+directory. It is called only from check_files() below. This is hopefully a
+cheapish check that does the job most of the time. Exim does *not* rely on this
+test when actually accessing any file. The test is used when routing to make it
+possible to take actions such as "if user x can access file y then run this
+router".
+
+During routing, Exim is normally running as root, and so the test will work
+except for NFS non-root mounts. When verifying during message reception, Exim
+is running as "exim", so the test may not work. This is a limitation of the
+Exim design.
+
+Code in check_files() below detects the case when it cannot stat() the file (as
+root), and in that situation it uses a setuid subprocess in which to run this
+test.
+
+Arguments:
+  path          the path to check
+  uid           the user
+  gid           the group
+  bits          the bits required in the final component
+
+Returns:        TRUE
+                FALSE errno=EACCES or ENOENT (or others from realpath or stat)
+*/
+
+static BOOL
+route_check_access(uschar *path, uid_t uid, gid_t gid, int bits)
+{
+struct stat statbuf;
+uschar *slash;
+uschar *rp = US realpath(CS path, CS big_buffer);
+uschar *sp = rp + 1;
+
+DEBUG(D_route) debug_printf("route_check_access(%s,%d,%d,%o)\n", path,
+  (int)uid, (int)gid, bits);
+
+if (rp == NULL) return FALSE;
+
+while ((slash = Ustrchr(sp, '/')) != NULL)
+  {
+  *slash = 0;
+  DEBUG(D_route) debug_printf("stat %s\n", rp);
+  if (Ustat(rp, &statbuf) < 0) return FALSE;
+  if ((statbuf.st_mode &
+       ((statbuf.st_uid == uid)? 0100 : (statbuf.st_gid == gid)? 0010 : 001)
+      ) == 0)
+    {
+    errno = EACCES;
+    return FALSE;
+    }
+  *slash = '/';
+  sp = slash + 1;
+  }
+
+/* Down to the final component */
+
+DEBUG(D_route) debug_printf("stat %s\n", rp);
+
+if (Ustat(rp, &statbuf) < 0) return FALSE;
+
+if (statbuf.st_uid == uid) bits = bits << 6;
+  else if (statbuf.st_gid == gid) bits = bits << 3;
+if ((statbuf.st_mode & bits) != bits)
+  {
+  errno = EACCES;
+  return FALSE;
+  }
+
+DEBUG(D_route) debug_printf("route_check_access() succeeded\n");
+return TRUE;
+}
+
+
+
+/*************************************************
+*           Do file existence tests              *
+*************************************************/
+
+/* This function is given a colon-separated list of file tests, each of which
+is expanded before use. A test consists of a file name, optionally preceded by
+! (require non-existence) and/or + for handling permission denied (+ means
+treat as non-existing).
+
+An item that contains no slashes is interpreted as a username or id, with an
+optional group id, for checking access to the file. This cannot be done
+"perfectly", but it is good enough for a number of applications.
+
+Arguments:
+  s        a colon-separated list of file tests or NULL
+  perror   a pointer to an anchor for an error text in the case of a DEFER
+
+Returns:   OK if s == NULL or all tests are as required
+           DEFER if the existence of at least one of the files is
+             unclear (an error other than non-existence occurred);
+           DEFER if an expansion failed
+           DEFER if a name is not absolute
+           DEFER if problems with user/group
+           SKIP otherwise
+*/
+
+int
+check_files(uschar *s, uschar **perror)
+{
+int sep = 0;              /* List has default separators */
+uid_t uid = 0;            /* For picky compilers */
+gid_t gid = 0;            /* For picky compilers */
+BOOL ugid_set = FALSE;
+uschar *check, *listptr;
+uschar buffer[1024];
+
+if (s == NULL) return OK;
+
+DEBUG(D_route) debug_printf("checking require_files\n");
+
+listptr = s;
+while ((check = string_nextinlist(&listptr, &sep, buffer, sizeof(buffer)))
+        != NULL)
+  {
+  int rc;
+  int eacces_code = 0;
+  BOOL invert = FALSE;
+  struct stat statbuf;
+  uschar *ss = expand_string(check);
+
+  if (ss == NULL)
+    {
+    if (expand_string_forcedfail) continue;
+    *perror = string_sprintf("failed to expand \"%s\" for require_files: %s",
+      check, expand_string_message);
+    goto RETURN_DEFER;
+    }
+
+  /* Empty items are just skipped */
+
+  if (*ss == 0) continue;
+
+  /* If there are no slashes in the string, we have a user name or uid, with
+  optional group/gid. */
+
+  if (Ustrchr(ss, '/') == NULL)
+    {
+    BOOL ok;
+    struct passwd *pw;
+    uschar *comma = Ustrchr(ss, ',');
+
+    /* If there's a comma, temporarily terminate the user name/number
+    at that point. Then set the uid. */
+
+    if (comma != NULL) *comma = 0;
+    ok = route_finduser(ss, &pw, &uid);
+    if (comma != NULL) *comma = ',';
+
+    if (!ok)
+      {
+      *perror = string_sprintf("user \"%s\" for require_files not found", ss);
+      goto RETURN_DEFER;
+      }
+
+    /* If there was no comma, the gid is that associated with the user. */
+
+    if (comma == NULL)
+      {
+      if (pw != NULL) gid = pw->pw_gid; else
+        {
+        *perror = string_sprintf("group missing after numerical uid %d for "
+          "require_files", (int)uid);
+        goto RETURN_DEFER;
+        }
+      }
+    else
+      {
+      if (!route_findgroup(comma + 1, &gid))
+        {
+        *perror = string_sprintf("group \"%s\" for require_files not found\n",
+          comma + 1);
+        goto RETURN_DEFER;
+        }
+      }
+
+    /* Note that we have values set, and proceed to next item */
+
+    DEBUG(D_route)
+      debug_printf("check subsequent files for access by %s\n", ss);
+    ugid_set = TRUE;
+    continue;
+    }
+
+  /* Path, possibly preceded by + and ! */
+
+  if (*ss == '+')
+    {
+    eacces_code = 1;
+    while (isspace((*(++ss))));
+    }
+
+  if (*ss == '!')
+    {
+    invert = TRUE;
+    while (isspace((*(++ss))));
+    }
+
+  if (*ss != '/')
+    {
+    *perror = string_sprintf("require_files: \"%s\" is not absolute", ss);
+    goto RETURN_DEFER;
+    }
+
+  /* Stat the file, either as root (while routing) or as exim (while verifying
+  during message reception). */
+
+  rc = Ustat(ss, &statbuf);
+
+  DEBUG(D_route)
+    {
+    debug_printf("file check: %s\n", check);
+    if (ss != check) debug_printf("expanded file: %s\n", ss);
+    debug_printf("stat() yielded %d\n", rc);
+    }
+
+  /* If permission is denied, and we are running as root (i.e. routing for
+  delivery rather than verifying), and the requirement is to test for access by
+  a particular uid/gid, it must mean that the file is on a non-root-mounted NFS
+  system. In this case, we have to use a subprocess that runs as the relevant
+  uid in order to do the test. */
+
+  if (rc != 0 && errno == EACCES && ugid_set && getuid() == root_uid)
+    {
+    int status;
+    pid_t pid;
+    void (*oldsignal)(int);
+
+    DEBUG(D_route) debug_printf("root is denied access: forking to check "
+      "in subprocess\n");
+
+    /* Before forking, ensure that SIGCHLD is set to SIG_DFL before forking, so
+    that the child process can be waited for, just in case get here with it set
+    otherwise. Save the old state for resetting on the wait. */
+
+    oldsignal = signal(SIGCHLD, SIG_DFL);
+    pid = fork();
+
+    /* If fork() fails, reinstate the original error and behave as if
+    this block of code were not present. This is the same behavious as happens
+    when Exim is not running as root at this point. */
+
+    if (pid < 0)
+      {
+      DEBUG(D_route)
+       debug_printf("require_files: fork failed: %s\n", strerror(errno));
+      errno = EACCES;
+      goto HANDLE_ERROR;
+      }
+
+    /* In the child process, change uid and gid, and then do the check using
+    the route_check_access() function. This does more than just stat the file;
+    it tests permissions as well. Return 0 for OK and 1 for failure. */
+
+    if (pid == 0)
+      {
+      exim_setugid(uid, gid, TRUE,
+        string_sprintf("require_files check, file=%s", ss));
+      if (route_check_access(ss, uid, gid, 4)) _exit(0);
+      DEBUG(D_route) debug_printf("route_check_access() failed\n");
+      _exit(1);
+      }
+
+    /* In the parent, wait for the child to finish */
+
+    while (waitpid(pid, &status, 0) < 0)
+     {
+     if (errno != EINTR)  /* unexpected error, interpret as failure */
+       {
+       status = 1;
+       break;
+       }
+     }
+
+    signal(SIGCHLD, oldsignal);   /* restore */
+    if ((status == 0) == invert) return SKIP;
+    continue;   /* to test the next file */
+    }
+
+  /* Control reaches here if the initial stat() succeeds, or fails with an
+  error other than EACCES, or no uid/gid is set, or we are not running as root.
+  If we know the file exists and uid/gid are set, try to check read access for
+  that uid/gid as best we can. */
+
+  if (rc == 0 && ugid_set && !route_check_access(ss, uid, gid, 4))
+    {
+    DEBUG(D_route) debug_printf("route_check_access() failed\n");
+    rc = -1;
+    }
+
+  /* Handle error returns from stat() or route_check_access(). The EACESS error
+  is handled specially. At present, we can force it to be treated as
+  non-existence. Write the code so that it will be easy to add forcing for
+  existence if required later. */
+
+  HANDLE_ERROR:
+  if (rc < 0)
+    {
+    DEBUG(D_route) debug_printf("errno = %d\n", errno);
+    if (errno == EACCES)
+      {
+      if (eacces_code == 1)
+        {
+        DEBUG(D_route) debug_printf("EACCES => ENOENT\n");
+        errno = ENOENT;   /* Treat as non-existent */
+        }
+      }
+    if (errno != ENOENT)
+      {
+      *perror = string_sprintf("require_files: error for %s: %s", ss,
+        strerror(errno));
+      goto RETURN_DEFER;
+      }
+    }
+
+  /* At this point, rc < 0 => non-existence; rc >= 0 => existence */
+
+  if ((rc >= 0) == invert) return SKIP;
+  }
+
+return OK;
+
+/* Come here on any of the errors that return DEFER. */
+
+RETURN_DEFER:
+DEBUG(D_route) debug_printf("%s\n", *perror);
+return DEFER;
+}
+
+
+
+
+
+/*************************************************
+*             Check for router skipping          *
+*************************************************/
+
+/* This function performs various checks to see whether a router should be
+skipped. The order in which they are performed is important.
+
+Arguments:
+  r            pointer to router instance block
+  addr         address that is being handled
+  verify       the verification type
+  pw           ptr to ptr to passwd structure for local user
+  perror       for lookup errors
+
+Returns:       OK if all the tests succeed
+               SKIP if router is to be skipped
+               DEFER for a lookup defer
+               FAIL for address to be failed
+*/
+
+static BOOL
+check_router_conditions(router_instance *r, address_item *addr, int verify,
+  struct passwd **pw, uschar **perror)
+{
+int rc;
+uschar *check_local_part;
+unsigned int *localpart_cache;
+
+/* Reset variables to hold a home directory and data from lookup of a domain or
+local part, and ensure search_find_defer is unset, in case there aren't any
+actual lookups. */
+
+deliver_home = NULL;
+deliver_domain_data = NULL;
+deliver_localpart_data = NULL;
+sender_data = NULL;
+local_user_gid = (gid_t)(-1);
+local_user_uid = (uid_t)(-1);
+search_find_defer = FALSE;
+
+/* Skip this router if not verifying and it has verify_only set */
+
+if ((verify == v_none || verify == v_expn) && r->verify_only)
+  {
+  DEBUG(D_route) debug_printf("%s router skipped: verify_only set\n", r->name);
+  return SKIP;
+  }
+
+/* Skip this router if testing an address (-bt) and address_test is not set */
+
+if (address_test_mode && !r->address_test)
+  {
+  DEBUG(D_route) debug_printf("%s router skipped: address_test is unset\n",
+    r->name);
+  return SKIP;
+  }
+
+/* Skip this router if verifying and it hasn't got the appropriate verify flag
+set. */
+
+if ((verify == v_sender && !r->verify_sender) ||
+    (verify == v_recipient && !r->verify_recipient))
+  {
+  DEBUG(D_route) debug_printf("%s router skipped: verify %d %d %d\n",
+    r->name, verify, r->verify_sender, r->verify_recipient);
+  return SKIP;
+  }
+
+/* Skip this router if processing EXPN and it doesn't have expn set */
+
+if (verify == v_expn && !r->expn)
+  {
+  DEBUG(D_route) debug_printf("%s router skipped: no_expn set\n", r->name);
+  return SKIP;
+  }
+
+/* Skip this router if there's a domain mismatch. */
+
+if ((rc = route_check_dls(r->name, US"domains", r->domains, &domainlist_anchor,
+     addr->domain_cache, TRUE, addr->domain, &deliver_domain_data, MCL_DOMAIN,
+     perror)) != OK)
+  return rc;
+
+/* Skip this router if there's a local part mismatch. We want to pass over the
+caseful local part, so that +caseful can restore it, even if this router is
+handling local parts caselessly. However, we can't just pass cc_local_part,
+because that doesn't have the prefix or suffix stripped. A bit of massaging is
+required. Also, we only use the match cache for local parts that have not had
+a prefix or suffix stripped. */
+
+if (addr->prefix == NULL && addr->suffix == NULL)
+  {
+  localpart_cache = addr->localpart_cache;
+  check_local_part = addr->cc_local_part;
+  }
+else
+  {
+  localpart_cache = NULL;
+  check_local_part = string_copy(addr->cc_local_part);
+  if (addr->prefix != NULL)
+    check_local_part += Ustrlen(addr->prefix);
+  if (addr->suffix != NULL)
+    check_local_part[Ustrlen(check_local_part) - Ustrlen(addr->suffix)] = 0;
+  }
+
+if ((rc = route_check_dls(r->name, US"local_parts", r->local_parts,
+       &localpartlist_anchor, localpart_cache, MCL_LOCALPART,
+       check_local_part, &deliver_localpart_data, !r->caseful_local_part,
+       perror)) != OK)
+  return rc;
+
+/* If the check_local_user option is set, check that the local_part is the
+login of a local user. Note: the third argument to route_finduser() must be
+NULL here, to prevent a numeric string being taken as a numeric uid. If the
+user is found, set deliver_home to the home directory, and also set
+local_user_{uid,gid}.  */
+
+if (r->check_local_user)
+  {
+  DEBUG(D_route) debug_printf("checking for local user\n");
+  if (!route_finduser(addr->local_part, pw, NULL))
+    {
+    DEBUG(D_route) debug_printf("%s router skipped: %s is not a local user\n",
+      r->name, addr->local_part);
+    return SKIP;
+    }
+  deliver_home = string_copy(US (*pw)->pw_dir);
+  local_user_gid = (*pw)->pw_gid;
+  local_user_uid = (*pw)->pw_uid;
+  }
+
+/* Set (or override in the case of check_local_user) the home directory if
+router_home_directory is set. This is done here so that it overrides $home from
+check_local_user before any subsequent expansions are done. Otherwise, $home
+could mean different things for different options, which would be extremely
+confusing. */
+
+if (r->router_home_directory != NULL)
+  {
+  uschar *router_home = expand_string(r->router_home_directory);
+  if (router_home == NULL)
+    {
+    if (!expand_string_forcedfail)
+      {
+      *perror = string_sprintf("failed to expand \"%s\" for "
+        "router_home_directory: %s", r->router_home_directory,
+        expand_string_message);
+      return DEFER;
+      }
+    }
+  else
+    {
+    setflag(addr, af_home_expanded); /* Note set from router_home_directory */
+    deliver_home = router_home;
+    }
+  }
+
+/* Skip if the sender condition is not met. We leave this one till after the
+local user check so that $home is set - enabling the possiblity of letting
+individual recipients specify lists of acceptable/unacceptable senders. */
+
+if ((rc = route_check_dls(r->name, US"senders", r->senders, NULL,
+     sender_address_cache, MCL_ADDRESS, NULL, NULL, FALSE, perror)) != OK)
+  return rc;
+
+/* This is the point at which we print out the router's debugging string if it
+is set. We wait till here so as to have $home available for local users (and
+anyway, we don't want too much stuff for skipped routers). */
+
+debug_print_string(r->debug_string);
+
+/* Perform file existence tests. */
+
+if ((rc = check_files(r->require_files, perror)) != OK)
+  {
+  DEBUG(D_route) debug_printf("%s router %s: file check\n", r->name,
+    (rc == SKIP)? "skipped" : "deferred");
+  return rc;
+  }
+
+/* Now the general condition test. */
+
+if (r->condition != NULL)
+  {
+  DEBUG(D_route) debug_printf("checking \"condition\"\n");
+  if (!expand_check_condition(r->condition, r->name, US"router"))
+    {
+    if (search_find_defer)
+      {
+      *perror = US"condition check lookup defer";
+      DEBUG(D_route) debug_printf("%s\n", *perror);
+      return DEFER;
+      }
+    DEBUG(D_route)
+      debug_printf("%s router skipped: condition failure\n", r->name);
+    return SKIP;
+    }
+  }
+
+#ifdef EXPERIMENTAL_BRIGHTMAIL
+/* check if a specific Brightmail AntiSpam rule fired on the message */
+if (r->bmi_rule != NULL) {
+  DEBUG(D_route) debug_printf("checking bmi_rule\n");
+  if (bmi_check_rule(bmi_base64_verdict, r->bmi_rule) == 0) {
+    /* none of the rules fired */
+    DEBUG(D_route)
+      debug_printf("%s router skipped: none of bmi_rule rules fired\n", r->name);
+    return SKIP;
+  };
+};
+
+/* check if message should not be delivered */
+if (r->bmi_dont_deliver) {
+  if (bmi_deliver == 1) {
+    DEBUG(D_route)
+      debug_printf("%s router skipped: bmi_dont_deliver is FALSE\n", r->name);
+    return SKIP;
+  };
+};
+
+/* check if message should go to an alternate location */
+if (r->bmi_deliver_alternate) {
+  if ((bmi_deliver == 0) || (bmi_alt_location == NULL)) {
+    DEBUG(D_route)
+      debug_printf("%s router skipped: bmi_deliver_alternate is FALSE\n", r->name);
+    return SKIP;
+  };
+};
+
+/* check if message should go to default location */
+if (r->bmi_deliver_default) {
+  if ((bmi_deliver == 0) || (bmi_alt_location != NULL)) {
+    DEBUG(D_route)
+      debug_printf("%s router skipped: bmi_deliver_default is FALSE\n", r->name);
+    return SKIP;
+  };
+};
+#endif
+
+/* All the checks passed. */
+
+return OK;
+}
+
+
+
+
+/*************************************************
+*           Find a local user                    *
+*************************************************/
+
+/* Try several times (if configured) to find a local user, in case delays in
+NIS or NFS whatever cause an incorrect refusal. It's a pity that getpwnam()
+doesn't have some kind of indication as to why it has failed. If the string
+given consists entirely of digits, and the third argument is not NULL, assume
+the string is the numerical value of the uid. Otherwise it is looked up using
+getpwnam(). The uid is passed back via return_uid, if not NULL, and the
+pointer to a passwd structure, if found, is passed back via pw, if not NULL.
+
+Because this may be called several times in succession for the same user for
+different routers, cache the result of the previous getpwnam call so that it
+can be re-used. Note that we can't just copy the structure, as the store it
+points to can get trashed.
+
+Arguments:
+  s           the login name or textual form of the numerical uid of the user
+  pw          if not NULL, return the result of getpwnam here, or set NULL
+                if no call to getpwnam is made (s numeric, return_uid != NULL)
+  return_uid  if not NULL, return the uid via this address
+
+Returns:      TRUE if s is numerical or was looked up successfully
+
+*/
+
+static struct passwd pwcopy;
+static struct passwd *lastpw = NULL;
+static uschar lastname[48] = { 0 };
+static uschar lastdir[128];
+static uschar lastgecos[128];
+static uschar lastshell[128];
+
+BOOL
+route_finduser(uschar *s, struct passwd **pw, uid_t *return_uid)
+{
+BOOL cache_set = (Ustrcmp(lastname, s) == 0);
+
+DEBUG(D_uid) debug_printf("seeking password data for user \"%s\": %s\n", s,
+  cache_set? "using cached result" : "cache not available");
+
+if (!cache_set)
+  {
+  int i = 0;
+
+  if (return_uid != NULL && (isdigit(*s) || *s == '-') &&
+       s[Ustrspn(s+1, "0123456789")+1] == 0)
+    {
+    *return_uid = (uid_t)Uatoi(s);
+    if (pw != NULL) *pw = NULL;
+    return TRUE;
+    }
+
+  (void)string_format(lastname, sizeof(lastname), "%s", s);
+
+  /* Force failure if string length is greater than given maximum */
+
+  if (max_username_length > 0 && Ustrlen(lastname) > max_username_length)
+    {
+    DEBUG(D_uid) debug_printf("forced failure of finduser(): string "
+      "length of %s is greater than %d\n", lastname, max_username_length);
+    lastpw = NULL;
+    }
+
+  /* Try a few times if so configured; this handles delays in NIS etc. */
+
+  else for (;;)
+    {
+    errno = 0;
+    if ((lastpw = getpwnam(CS s)) != NULL) break;
+    if (++i > finduser_retries) break;
+    sleep(1);
+    }
+
+  if (lastpw != NULL)
+    {
+    pwcopy.pw_uid = lastpw->pw_uid;
+    pwcopy.pw_gid = lastpw->pw_gid;
+    (void)string_format(lastdir, sizeof(lastdir), "%s", lastpw->pw_dir);
+    (void)string_format(lastgecos, sizeof(lastgecos), "%s", lastpw->pw_gecos);
+    (void)string_format(lastshell, sizeof(lastshell), "%s", lastpw->pw_shell);
+    pwcopy.pw_name = CS lastname;
+    pwcopy.pw_dir = CS lastdir;
+    pwcopy.pw_gecos = CS lastgecos;
+    pwcopy.pw_shell = CS lastshell;
+    lastpw = &pwcopy;
+    }
+
+  else DEBUG(D_uid)
+    {
+    if (errno != 0) debug_printf("getpwnam(%s) failed: %s\n", s,
+      strerror(errno));
+    }
+  }
+
+if (lastpw == NULL)
+  {
+  DEBUG(D_uid) debug_printf("getpwnam() returned NULL (user not found)\n");
+  return FALSE;
+  }
+else
+  {
+  DEBUG(D_uid) debug_printf("getpwnam() succeeded uid=%d gid=%d\n",
+    lastpw->pw_uid, lastpw->pw_gid);
+  }
+
+if (return_uid != NULL) *return_uid = lastpw->pw_uid;
+if (pw != NULL) *pw = lastpw;
+
+return TRUE;
+}
+
+
+
+
+/*************************************************
+*           Find a local group                   *
+*************************************************/
+
+/* Try several times (if configured) to find a local group, in case delays in
+NIS or NFS whatever cause an incorrect refusal. It's a pity that getgrnam()
+doesn't have some kind of indication as to why it has failed.
+
+Arguments:
+  s           the group namd or textual form of the numerical gid
+  return_gid  return the gid via this address
+
+Returns:      TRUE if the group was found; FALSE otherwise
+
+*/
+
+BOOL
+route_findgroup(uschar *s, gid_t *return_gid)
+{
+int i = 0;
+struct group *gr;
+
+if ((isdigit(*s) || *s == '-') && s[Ustrspn(s+1, "0123456789")+1] == 0)
+  {
+  *return_gid = (gid_t)Uatoi(s);
+  return TRUE;
+  }
+
+for (;;)
+  {
+  if ((gr = getgrnam(CS s)) != NULL)
+    {
+    *return_gid = gr->gr_gid;
+    return TRUE;
+    }
+  if (++i > finduser_retries) break;
+  sleep(1);
+  }
+
+return FALSE;
+}
+
+
+
+
+/*************************************************
+*          Find user by expanding string         *
+*************************************************/
+
+/* Expands a string, and then looks up the result in the passwd file.
+
+Arguments:
+  string       the string to be expanded, yielding a login name or a numerical
+                 uid value (to be passed to route_finduser())
+  driver_name  caller name for panic error message (only)
+  driver_type  caller type for panic error message (only)
+  pw           return passwd entry via this pointer
+  uid          return uid via this pointer
+  errmsg       where to point a message on failure
+
+Returns:       TRUE if user found, FALSE otherwise
+*/
+
+BOOL
+route_find_expanded_user(uschar *string, uschar *driver_name,
+  uschar *driver_type, struct passwd **pw, uid_t *uid, uschar **errmsg)
+{
+uschar *user = expand_string(string);
+
+if (user == NULL)
+  {
+  *errmsg = string_sprintf("Failed to expand user string \"%s\" for the "
+    "%s %s: %s", string, driver_name, driver_type, expand_string_message);
+  log_write(0, LOG_MAIN|LOG_PANIC, "%s", *errmsg);
+  return FALSE;
+  }
+
+if (route_finduser(user, pw, uid)) return TRUE;
+
+*errmsg = string_sprintf("Failed to find user \"%s\" from expanded string "
+  "\"%s\" for the %s %s", user, string, driver_name, driver_type);
+log_write(0, LOG_MAIN|LOG_PANIC, "%s", *errmsg);
+return FALSE;
+}
+
+
+
+/*************************************************
+*          Find group by expanding string        *
+*************************************************/
+
+/* Expands a string and then looks up the result in the group file.
+
+Arguments:
+  string       the string to be expanded, yielding a group name or a numerical
+                 gid value (to be passed to route_findgroup())
+  driver_name  caller name for panic error message (only)
+  driver_type  caller type for panic error message (only)
+  gid          return gid via this pointer
+  errmsg       return error message via this pointer
+
+Returns:       TRUE if found group, FALSE otherwise
+*/
+
+BOOL
+route_find_expanded_group(uschar *string, uschar *driver_name, uschar *driver_type,
+  gid_t *gid, uschar **errmsg)
+{
+BOOL yield = TRUE;
+uschar *group = expand_string(string);
+
+if (group == NULL)
+  {
+  *errmsg = string_sprintf("Failed to expand group string \"%s\" for the "
+    "%s %s: %s", string, driver_name, driver_type, expand_string_message);
+  log_write(0, LOG_MAIN|LOG_PANIC, "%s", *errmsg);
+  return FALSE;
+  }
+
+if (!route_findgroup(group, gid))
+  {
+  *errmsg = string_sprintf("Failed to find group \"%s\" from expanded string "
+    "\"%s\" for the %s %s", group, string, driver_name, driver_type);
+  log_write(0, LOG_MAIN|LOG_PANIC, "%s", *errmsg);
+  yield = FALSE;
+  }
+
+return yield;
+}
+
+
+
+/*************************************************
+*           Sort out "more" or "unseen"          *
+*************************************************/
+
+/* These values are usually fixed boolean values, but they are permitted to be
+expanded strings.
+
+Arguments:
+  addr       address being routed
+  rname      the router name
+  oname      the option name
+  bvalue     the router's boolean value
+  svalue     the router's string value
+  rvalue     where to put the returned value
+
+Returns:     OK     value placed in rvalue
+             DEFER  expansion failed
+*/
+
+static int
+exp_bool(address_item *addr, uschar *rname, uschar *oname, BOOL bvalue,
+  uschar *svalue, BOOL *rvalue)
+{
+uschar *expanded;
+if (svalue == NULL) { *rvalue = bvalue; return OK; }
+
+expanded = expand_string(svalue);
+if (expanded == NULL)
+  {
+  if (expand_string_forcedfail)
+    {
+    DEBUG(D_route) debug_printf("expansion of \"%s\" forced failure\n", oname);
+    *rvalue = bvalue;
+    return OK;
+    }
+  addr->message = string_sprintf("failed to expand \"%s\" in %s router: %s",
+      oname, rname, expand_string_message);
+  DEBUG(D_route) debug_printf("%s\n", addr->message);
+  return DEFER;
+  }
+
+DEBUG(D_route) debug_printf("expansion of \"%s\" yields \"%s\"\n", oname,
+  expanded);
+
+if (strcmpic(expanded, US"true") == 0 || strcmpic(expanded, US"yes") == 0)
+  *rvalue = TRUE;
+else if (strcmpic(expanded, US"false") == 0 || strcmpic(expanded, US"no") == 0)
+  *rvalue = FALSE;
+else
+  {
+  addr->message = string_sprintf("\"%s\" is not a valid value for the "
+    "\"%s\" option in the %s router", expanded, oname, rname);
+  return DEFER;
+  }
+
+return OK;
+}
+
+
+
+
+/*************************************************
+*            Handle an unseen routing            *
+*************************************************/
+
+/* This function is called when an address is routed by a router with "unseen"
+set. It must make a clone of the address, for handling by subsequent drivers.
+The clone is set to start routing at the next router.
+
+The original address must be replaced by an invented "parent" which has the
+routed address plus the clone as its children. This is necessary in case the
+address is at the top level - we don't want to mark it complete until both
+deliveries have been done.
+
+A new unique field must be made, so that the record of the delivery isn't a
+record of the original address, and checking for already delivered has
+therefore to be done here. If the delivery has happened, then take the base
+address off whichever delivery queue it is on - it will always be the top item.
+
+Arguments:
+  name          router name
+  addr          address that was routed
+  paddr_local   chain of local-delivery addresses
+  paddr_remote  chain of remote-delivery addresses
+  addr_new      chain for newly created addresses
+
+Returns:        nothing
+*/
+
+static void
+route_unseen(uschar *name, address_item *addr, address_item **paddr_local,
+  address_item **paddr_remote, address_item **addr_new)
+{
+address_item *parent = deliver_make_addr(addr->address, TRUE);
+address_item *new = deliver_make_addr(addr->address, TRUE);
+
+/* The invented parent is a copy that replaces the original; note that
+this copies its parent pointer. It has two children, and its errors_address is
+from the original address' parent, if present, otherwise unset. */
+
+*parent = *addr;
+parent->child_count = 2;
+parent->p.errors_address =
+  (addr->parent == NULL)? NULL : addr->parent->p.errors_address;
+
+/* The routed address gets a new parent. */
+
+addr->parent = parent;
+
+/* The clone has this parent too. Set its errors address from the parent. This
+was set from the original parent (or to NULL) - see above. We do NOT want to
+take the errors address from the unseen router. */
+
+new->parent = parent;
+new->p.errors_address = parent->p.errors_address;
+
+/* Copy the propagated flags and address_data from the original. */
+
+copyflag(new, addr, af_propagate);
+new->p.address_data = addr->p.address_data;
+#ifdef EXPERIMENTAL_DSN
+new->dsn_flags = addr->dsn_flags;
+new->dsn_orcpt = addr->dsn_orcpt;
+#endif
+
+
+/* As it has turned out, we haven't set headers_add or headers_remove for the
+ * clone. Thinking about it, it isn't entirely clear whether they should be
+ * copied from the original parent, like errors_address, or taken from the
+ * unseen router, like address_data and the flags. Until somebody brings this
+ * up, I propose to leave the code as it is.
+ */
+
+
+/* Set the cloned address to start at the next router, and put it onto the
+chain of new addresses. */
+
+new->start_router = addr->router->next;
+new->next = *addr_new;
+*addr_new = new;
+
+DEBUG(D_route) debug_printf("\"unseen\" set: replicated %s\n", addr->address);
+
+/* Make a new unique field, to distinguish from the normal one. */
+
+addr->unique = string_sprintf("%s/%s", addr->unique, name);
+
+/* If the address has been routed to a transport, see if it was previously
+delivered. If so, we take it off the relevant queue so that it isn't delivered
+again. Otherwise, it was an alias or something, and the addresses it generated
+are handled in the normal way. */
+
+if (addr->transport != NULL &&
+    tree_search(tree_nonrecipients, addr->unique) != NULL)
+  {
+  DEBUG(D_route)
+    debug_printf("\"unseen\" delivery previously done - discarded\n");
+  parent->child_count--;
+  if (*paddr_remote == addr) *paddr_remote = addr->next;
+  if (*paddr_local == addr) *paddr_local = addr->next;
+  }
+}
+
+
+
+/*************************************************
+*                 Route one address              *
+*************************************************/
+
+/* This function is passed in one address item, for processing by the routers.
+The verify flag is set if this is being called for verification rather than
+delivery. If the router doesn't have its "verify" flag set, it is skipped.
+
+Arguments:
+  addr           address to route
+  paddr_local    chain of local-delivery addresses
+  paddr_remote   chain of remote-delivery addresses
+  addr_new       chain for newly created addresses
+  addr_succeed   chain for completed addresses
+  verify         v_none if not verifying
+                 v_sender if verifying a sender address
+                 v_recipient if verifying a recipient address
+                 v_expn if processing an EXPN address
+
+Returns:         OK      => address successfully routed
+                 DISCARD => address was discarded
+                 FAIL    => address could not be routed
+                 DEFER   => some temporary problem
+                 ERROR   => some major internal or configuration failure
+*/
+
+int
+route_address(address_item *addr, address_item **paddr_local,
+  address_item **paddr_remote, address_item **addr_new,
+  address_item **addr_succeed, int verify)
+{
+int yield = OK;
+BOOL unseen;
+router_instance *r, *nextr;
+uschar *old_domain = addr->domain;
+
+HDEBUG(D_route)
+  {
+  debug_printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
+  debug_printf("routing %s\n", addr->address);
+  }
+
+/* Loop through all router instances until a router succeeds, fails, defers, or
+encounters an error. If the address has start_router set, we begin from there
+instead of at the first router. */
+
+for (r = (addr->start_router == NULL)? routers : addr->start_router;
+     r != NULL; r = nextr)
+  {
+  uschar *error;
+  struct passwd *pw = NULL;
+  struct passwd pwcopy;
+  address_item *parent;
+  BOOL loop_detected = FALSE;
+  BOOL more;
+  int loopcount = 0;
+  int rc;
+
+  DEBUG(D_route) debug_printf("--------> %s router <--------\n", r->name);
+
+  /* Reset any search error message from the previous router. */
+
+  search_error_message = NULL;
+
+  /* There are some weird cases where logging is disabled */
+
+  disable_logging = r->disable_logging;
+
+  /* Record the last router to handle the address, and set the default
+  next router. */
+
+  addr->router = r;
+  nextr = r->next;
+
+  /* Loop protection: If this address has an ancestor with the same address,
+  and that ancestor was routed by this router, we skip this router. This
+  prevents a variety of looping states when a new address is created by
+  redirection or by the use of "unseen" on a router.
+
+  If no_repeat_use is set on the router, we skip if _any_ ancestor was routed
+  by  this router, even if it was different to the current address.
+
+  Just in case someone does put it into a loop (possible with redirection
+  continally adding to an address, for example), put a long stop counter on
+  the number of parents. */
+
+  for (parent = addr->parent; parent != NULL; parent = parent->parent)
+    {
+    if (parent->router == r)
+      {
+      BOOL break_loop = !r->repeat_use;
+
+      /* When repeat_use is set, first check the active addresses caselessly.
+      If they match, we have to do a further caseful check of the local parts
+      when caseful_local_part is set. This is assumed to be rare, which is why
+      the code is written this way. */
+
+      if (!break_loop)
+        {
+        break_loop = strcmpic(parent->address, addr->address) == 0;
+        if (break_loop && r->caseful_local_part)
+          break_loop = Ustrncmp(parent->address, addr->address,
+             Ustrrchr(addr->address, '@') - addr->address) == 0;
+        }
+
+      if (break_loop)
+        {
+        DEBUG(D_route) debug_printf("%s router skipped: previously routed %s\n",
+          r->name, parent->address);
+        loop_detected = TRUE;
+        break;
+        }
+      }
+
+    /* Continue with parents, limiting the size of the dynasty. */
+
+    if (loopcount++ > 100)
+      {
+      log_write(0, LOG_MAIN|LOG_PANIC, "routing loop for %s", addr->address);
+      yield = DEFER;
+      goto ROUTE_EXIT;
+      }
+    }
+
+  if (loop_detected) continue;
+
+  /* Default no affixes and select whether to use a caseful or caseless local
+  part in this router. */
+
+  addr->prefix = addr->suffix = NULL;
+  addr->local_part = r->caseful_local_part?
+    addr->cc_local_part : addr->lc_local_part;
+
+  DEBUG(D_route) debug_printf("local_part=%s domain=%s\n", addr->local_part,
+    addr->domain);
+
+  /* Handle any configured prefix by replacing the local_part address,
+  and setting the prefix. Skip the router if the prefix doesn't match,
+  unless the prefix is optional. */
+
+  if (r->prefix != NULL)
+    {
+    int plen = route_check_prefix(addr->local_part, r->prefix);
+    if (plen > 0)
+      {
+      addr->prefix = string_copyn(addr->local_part, plen);
+      addr->local_part += plen;
+      DEBUG(D_route) debug_printf("stripped prefix %s\n", addr->prefix);
+      }
+    else if (!r->prefix_optional)
+      {
+      DEBUG(D_route) debug_printf("%s router skipped: prefix mismatch\n",
+        r->name);
+      continue;
+      }
+    }
+
+  /* Handle any configured suffix likewise. */
+
+  if (r->suffix != NULL)
+    {
+    int slen = route_check_suffix(addr->local_part, r->suffix);
+    if (slen > 0)
+      {
+      int lplen = Ustrlen(addr->local_part) - slen;
+      addr->suffix = addr->local_part + lplen;
+      addr->local_part = string_copyn(addr->local_part, lplen);
+      DEBUG(D_route) debug_printf("stripped suffix %s\n", addr->suffix);
+      }
+    else if (!r->suffix_optional)
+      {
+      DEBUG(D_route) debug_printf("%s router skipped: suffix mismatch\n",
+        r->name);
+      continue;
+      }
+    }
+
+  /* Set the expansion variables now that we have the affixes and the case of
+  the local part sorted. */
+
+  router_name = r->name;
+  deliver_set_expansions(addr);
+
+  /* For convenience, the pre-router checks are in a separate function, which
+  returns OK, SKIP, FAIL, or DEFER. */
+
+  if ((rc = check_router_conditions(r, addr, verify, &pw, &error)) != OK)
+    {
+    router_name = NULL;
+    if (rc == SKIP) continue;
+    addr->message = error;
+    yield = rc;
+    goto ROUTE_EXIT;
+    }
+
+  /* All pre-conditions have been met. Reset any search error message from
+  pre-condition tests. These can arise in negated tests where the failure of
+  the lookup leads to a TRUE pre-condition. */
+
+  search_error_message = NULL;
+
+  /* Finally, expand the address_data field in the router. Forced failure
+  behaves as if the router declined. Any other failure is more serious. On
+  success, the string is attached to the address for all subsequent processing.
+  */
+
+  if (r->address_data != NULL)
+    {
+    DEBUG(D_route) debug_printf("processing address_data\n");
+    deliver_address_data = expand_string(r->address_data);
+    if (deliver_address_data == NULL)
+      {
+      if (expand_string_forcedfail)
+        {
+        DEBUG(D_route) debug_printf("forced failure in expansion of \"%s\" "
+            "(address_data): decline action taken\n", r->address_data);
+
+        /* Expand "more" if necessary; DEFER => an expansion failed */
+
+        yield = exp_bool(addr, r->name, US"more", r->more, r->expand_more,
+          &more);
+        if (yield != OK) goto ROUTE_EXIT;
+
+        if (!more)
+          {
+          DEBUG(D_route)
+            debug_printf("\"more\"=false: skipping remaining routers\n");
+         router_name = NULL;
+          r = NULL;
+          break;
+          }
+        else continue;    /* With next router */
+        }
+
+      else
+        {
+        addr->message = string_sprintf("expansion of \"%s\" failed "
+          "in %s router: %s", r->address_data, r->name, expand_string_message);
+        yield = DEFER;
+        goto ROUTE_EXIT;
+        }
+      }
+    addr->p.address_data = deliver_address_data;
+    }
+
+  /* We are finally cleared for take-off with this router. Clear the the flag
+  that records that a local host was removed from a routed host list. Make a
+  copy of relevant fields in the password information from check_local_user,
+  because it will be overwritten if check_local_user is invoked again while
+  verifying an errors_address setting. */
+
+  clearflag(addr, af_local_host_removed);
+
+  if (pw != NULL)
+    {
+    pwcopy.pw_name = CS string_copy(US pw->pw_name);
+    pwcopy.pw_uid = pw->pw_uid;
+    pwcopy.pw_gid = pw->pw_gid;
+    pwcopy.pw_gecos = CS string_copy(US pw->pw_gecos);
+    pwcopy.pw_dir = CS string_copy(US pw->pw_dir);
+    pwcopy.pw_shell = CS string_copy(US pw->pw_shell);
+    pw = &pwcopy;
+    }
+
+  /* Run the router, and handle the consequences. */
+
+#ifdef EXPERIMENTAL_DSN
+/* ... but let us check on DSN before. If this should be the last hop for DSN
+   set flag
+*/
+  if ((r->dsn_lasthop == TRUE) && ((addr->dsn_flags & rf_dsnlasthop) == 0))
+  {
+    addr->dsn_flags |= rf_dsnlasthop;
+    HDEBUG(D_route) debug_printf("DSN: last hop for %s\n", addr->address);
+  }
+#endif
+
+  HDEBUG(D_route) debug_printf("calling %s router\n", r->name);
+
+  yield = (r->info->code)(r, addr, pw, verify, paddr_local, paddr_remote,
+    addr_new, addr_succeed);
+
+  router_name = NULL;
+
+  if (yield == FAIL)
+    {
+    HDEBUG(D_route) debug_printf("%s router forced address failure\n", r->name);
+    goto ROUTE_EXIT;
+    }
+
+  /* If succeeded while verifying but fail_verify is set, convert into
+  a failure, and take it off the local or remote delivery list. */
+
+  if (((verify == v_sender && r->fail_verify_sender) ||
+       (verify == v_recipient && r->fail_verify_recipient)) &&
+      (yield == OK || yield == PASS))
+    {
+    addr->message = string_sprintf("%s router forced verify failure", r->name);
+    if (*paddr_remote == addr) *paddr_remote = addr->next;
+    if (*paddr_local == addr) *paddr_local = addr->next;
+    yield = FAIL;
+    goto ROUTE_EXIT;
+    }
+
+  /* PASS and DECLINE are the only two cases where the loop continues. For all
+  other returns, we break the loop and handle the result below. */
+
+  if (yield != PASS && yield != DECLINE) break;
+
+  HDEBUG(D_route)
+    {
+    debug_printf("%s router %s for %s\n", r->name,
+      (yield == PASS)? "passed" : "declined", addr->address);
+    if (Ustrcmp(old_domain, addr->domain) != 0)
+      debug_printf("domain %s rewritten\n", old_domain);
+    }
+
+  /* PASS always continues to another router; DECLINE does so if "more"
+  is true. Initialization insists that pass_router is always a following
+  router. Otherwise, break the loop as if at the end of the routers. */
+
+  if (yield == PASS)
+    {
+    if (r->pass_router != NULL) nextr = r->pass_router;
+    }
+  else
+    {
+    /* Expand "more" if necessary */
+
+    yield = exp_bool(addr, r->name, US"more", r->more, r->expand_more, &more);
+    if (yield != OK) goto ROUTE_EXIT;
+
+    if (!more)
+      {
+      HDEBUG(D_route)
+        debug_printf("\"more\" is false: skipping remaining routers\n");
+      r = NULL;
+      break;
+      }
+    }
+  }                                      /* Loop for all routers */
+
+/* On exit from the routers loop, if r == NULL we have run out of routers,
+either genuinely, or as a result of no_more. Otherwise, the loop ended
+prematurely, either because a router succeeded, or because of some special
+router response. Note that FAIL errors and errors detected before actually
+running a router go direct to ROUTE_EXIT from code above. */
+
+if (r == NULL)
+  {
+  HDEBUG(D_route) debug_printf("no more routers\n");
+  if (addr->message == NULL)
+    {
+    uschar *message = US"Unrouteable address";
+    if (addr->router != NULL && addr->router->cannot_route_message != NULL)
+      {
+      uschar *expmessage = expand_string(addr->router->cannot_route_message);
+      if (expmessage == NULL)
+        {
+        if (!expand_string_forcedfail)
+          log_write(0, LOG_MAIN|LOG_PANIC, "failed to expand "
+            "cannot_route_message in %s router: %s", addr->router->name,
+            expand_string_message);
+        }
+      else message = expmessage;
+      }
+    addr->user_message = addr->message = message;
+    }
+  addr->router = NULL;         /* For logging */
+  yield = FAIL;
+  goto ROUTE_EXIT;
+  }
+
+if (yield == DEFER)
+  {
+  HDEBUG(D_route)
+    {
+    debug_printf("%s router: defer for %s\n", r->name, addr->address);
+    debug_printf("  message: %s\n", (addr->message == NULL)?
+      US"<none>" : addr->message);
+    }
+  goto ROUTE_EXIT;
+  }
+
+if (yield == DISCARD) goto ROUTE_EXIT;
+
+/* The yield must be either OK or REROUTED. */
+
+if (yield != OK && yield != REROUTED)
+  log_write(0, LOG_MAIN|LOG_PANIC_DIE, "%s router returned unknown value %d",
+    r->name, yield);
+
+/* If the yield was REROUTED, the router put a child address on the new chain
+as a result of a domain change of some sort (widening, typically). */
+
+if (yield == REROUTED)
+  {
+  HDEBUG(D_route) debug_printf("re-routed to %s\n", addr->address);
+  yield = OK;
+  goto ROUTE_EXIT;
+  }
+
+/* The only remaining possibility is that the router succeeded. If the
+translate_ip_address options is set and host addresses were associated with the
+address, run them through the translation. This feature is for weird and
+wonderful situations (the amateur packet radio people need it) or very broken
+networking, so it is included in the binary only if requested. */
+
+#ifdef SUPPORT_TRANSLATE_IP_ADDRESS
+
+if (r->translate_ip_address != NULL)
+  {
+  int rc;
+  int old_pool = store_pool;
+  host_item *h;
+  for (h = addr->host_list; h != NULL; h = h->next)
+    {
+    uschar *newaddress;
+    uschar *oldaddress, *oldname;
+
+    if (h->address == NULL) continue;
+
+    deliver_host_address = h->address;
+    newaddress = expand_string(r->translate_ip_address);
+    deliver_host_address = NULL;
+
+    if (newaddress == NULL)
+      {
+      if (expand_string_forcedfail) continue;
+      addr->basic_errno = ERRNO_EXPANDFAIL;
+      addr->message = string_sprintf("translate_ip_address expansion "
+        "failed: %s", expand_string_message);
+      yield = DEFER;
+      goto ROUTE_EXIT;
+      }
+
+    DEBUG(D_route) debug_printf("%s [%s] translated to %s\n",
+      h->name, h->address, newaddress);
+    if (string_is_ip_address(newaddress, NULL) != 0)
+      {
+      h->address = newaddress;
+      continue;
+      }
+
+    oldname = h->name;
+    oldaddress = h->address;
+    h->name = newaddress;
+    h->address = NULL;
+    h->mx = MX_NONE;
+
+    store_pool = POOL_PERM;
+    rc = host_find_byname(h, NULL, HOST_FIND_QUALIFY_SINGLE, NULL, TRUE);
+    store_pool = old_pool;
+
+    if (rc == HOST_FIND_FAILED || rc == HOST_FIND_AGAIN)
+      {
+      addr->basic_errno = ERRNO_UNKNOWNHOST;
+      addr->message = string_sprintf("host %s not found when "
+        "translating %s [%s]", h->name, oldname, oldaddress);
+      yield = DEFER;
+      goto ROUTE_EXIT;
+      }
+    }
+  }
+#endif  /* SUPPORT_TRANSLATE_IP_ADDRESS */
+
+/* See if this is an unseen routing; first expand the option if necessary.
+DEFER can be given if the expansion fails */
+
+yield = exp_bool(addr, r->name, US"unseen", r->unseen, r->expand_unseen,
+  &unseen);
+if (yield != OK) goto ROUTE_EXIT;
+
+/* Debugging output recording a successful routing */
+
+HDEBUG(D_route)
+  {
+  debug_printf("routed by %s router%s\n", r->name,
+    unseen? " (unseen)" : "");
+  }
+
+DEBUG(D_route)
+  {
+  host_item *h;
+
+  debug_printf("  envelope to: %s\n", addr->address);
+  debug_printf("  transport: %s\n", (addr->transport == NULL)?
+    US"<none>" : addr->transport->name);
+
+  if (addr->p.errors_address != NULL)
+    debug_printf("  errors to %s\n", addr->p.errors_address);
+
+  for (h = addr->host_list; h != NULL; h = h->next)
+    {
+    debug_printf("  host %s", h->name);
+    if (h->address != NULL) debug_printf(" [%s]", h->address);
+    if (h->mx >= 0) debug_printf(" MX=%d", h->mx);
+      else if (h->mx != MX_NONE) debug_printf(" rgroup=%d", h->mx);
+    if (h->port != PORT_NONE) debug_printf(" port=%d", h->port);
+    /* if (h->dnssec != DS_UNK) debug_printf(" dnssec=%s", h->dnssec==DS_YES ? "yes" : "no"); */
+    debug_printf("\n");
+    }
+  }
+
+/* Clear any temporary error message set by a router that declined, and handle
+the "unseen" option (ignore if there are no further routers). */
+
+addr->message = NULL;
+if (unseen && r->next != NULL)
+  route_unseen(r->name, addr, paddr_local, paddr_remote, addr_new);
+
+/* Unset the address expansions, and return the final result. */
+
+ROUTE_EXIT:
+if (yield == DEFER) {
+  if (
+    ((Ustrstr(addr->message, "failed to expand") != NULL) || (Ustrstr(addr->message, "expansion of ") != NULL)) &&
+    (
+      Ustrstr(addr->message, "mysql") != NULL ||
+      Ustrstr(addr->message, "pgsql") != NULL ||
+#ifdef EXPERIMENTAL_REDIS
+      Ustrstr(addr->message, "redis") != NULL ||
+#endif
+      Ustrstr(addr->message, "sqlite") != NULL ||
+      Ustrstr(addr->message, "ldap:") != NULL ||
+      Ustrstr(addr->message, "ldapdn:") != NULL ||
+      Ustrstr(addr->message, "ldapm:") != NULL
+    )
+  ) {
+    addr->message = string_sprintf("Temporary internal error");
+  }
+}
+
+deliver_set_expansions(NULL);
+router_name = NULL;
+disable_logging = FALSE;
+return yield;
+}
+
+/* End of route.c */
diff --git a/src/routers/Makefile b/src/routers/Makefile
new file mode 100644 (file)
index 0000000..8f2432d
--- /dev/null
@@ -0,0 +1,43 @@
+# Make file for building a library containing all the available routers and
+# calling it routers.a. This is called from the main make file, after cd'ing
+# to the directors subdirectory. The library also contains functions that
+# are called only from within the individual routers.
+
+OBJ = accept.o dnslookup.o ipliteral.o iplookup.o manualroute.o \
+      queryprogram.o redirect.o \
+      rf_change_domain.o rf_expand_data.o rf_get_errors_address.o \
+      rf_get_munge_headers.o rf_get_transport.o rf_get_ugid.o \
+      rf_lookup_hostlist.o \
+      rf_queue_add.o rf_self_action.o \
+      rf_set_ugid.o
+
+routers.a:       $(OBJ)
+                @$(RM_COMMAND) -f routers.a
+                @echo "$(AR) routers.a"
+                @$(AR) routers.a $(OBJ)
+                $(RANLIB) $@
+
+.SUFFIXES:       .o .c
+.c.o:;           @echo "$(CC) $*.c"
+                $(FE)$(CC) -c $(CFLAGS) $(INCLUDE) $*.c
+
+rf_change_domain.o:      $(HDRS) rf_change_domain.c      rf_functions.h
+rf_expand_data.o:        $(HDRS) rf_expand_data.c        rf_functions.h
+rf_get_errors_address.o: $(HDRS) rf_get_errors_address.c rf_functions.h
+rf_get_munge_headers.o:  $(HDRS) rf_get_munge_headers.c  rf_functions.h
+rf_get_transport.o:      $(HDRS) rf_get_transport.c      rf_functions.h
+rf_get_ugid.o:           $(HDRS) rf_get_ugid.c           rf_functions.h
+rf_lookup_hostlist.o:    $(HDRS) rf_lookup_hostlist.c    rf_functions.h
+rf_queue_add.o:          $(HDRS) rf_queue_add.c          rf_functions.h
+rf_self_action.o:        $(HDRS) rf_self_action.c        rf_functions.h
+rf_set_ugid.o:           $(HDRS) rf_set_ugid.c           rf_functions.h
+
+accept.o:        $(HDRS) accept.c       rf_functions.h accept.h
+dnslookup.o:     $(HDRS) dnslookup.c    rf_functions.h dnslookup.h
+ipliteral.o:     $(HDRS) ipliteral.c    rf_functions.h ipliteral.h
+iplookup.o:      $(HDRS) iplookup.c     rf_functions.h iplookup.h
+manualroute.o:   $(HDRS) manualroute.c  rf_functions.h manualroute.h
+queryprogram.o:  $(HDRS) queryprogram.c rf_functions.h queryprogram.h
+redirect.o:      $(HDRS) redirect.c     rf_functions.h redirect.h
+
+# End
diff --git a/src/routers/README b/src/routers/README
new file mode 100644 (file)
index 0000000..f2a73c8
--- /dev/null
@@ -0,0 +1,57 @@
+ROUTERS:
+
+The yield of a router is one of:
+
+  OK              the address was routed and either added to one of the
+                  addr_local or addr_remote chains, or one or more new
+                  addresses were added to addr_new. The original may be added
+                  to addr_succeed.
+
+  REROUTED        this is used when a child address is created and added to
+                  addr_new as a consequence of a domain widening or because
+                  "self = reroute" was encountered. The only time it is handled
+                  differently from OK is when verifying, to force it to
+                  continue with the child address.
+
+  DECLINE         the address was not routed; pass to next router unless
+                  no_more is set. It is permitted for additional addresses to
+                  have been added to addr_new (or indeed for addresses to have
+                  been put on the other chains).
+
+  PASS            the address was not routed, but might have been modified;
+                  pass to the next router unconditionally.
+
+  DISCARD         the address was discarded (:blackhole: or "seen finish")
+
+  FAIL            the address was not routed; do not pass to any subseqent
+                  routers, i.e. cause routing to fail.
+
+  DEFER           retry this address later.
+
+Both ERROR and DEFER cause the message to be kept on the queue; either may
+request freezing, but nowadays we try not to request freezing from routers
+because it may hold up other addresses in the message.
+
+
+When routing succeeds, the following field in the address can be set:
+
+  transport       points to the transport instance control block
+
+  uid, gid        are the uid and gid under which a local transport is to be
+                  run if the transport does not itself specify them.
+
+  initgroups      is set true if initgroups() is to be called when using the
+                  uid and gid set up by the router.
+
+  fallback_hosts  fallback host list - relevant only if the router sets up
+                  a remote transport for the address.
+
+  errors_address  where to send error messages for this address.
+
+  extra_headers   additional headers to be added to the message for this
+                  address.
+
+  remove_headers  the names of headers to be removed from the message for this
+                  address
+
+****
diff --git a/src/routers/accept.c b/src/routers/accept.c
new file mode 100644 (file)
index 0000000..8049eaf
--- /dev/null
@@ -0,0 +1,128 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+
+#include "../exim.h"
+#include "rf_functions.h"
+#include "accept.h"
+
+
+/* Options specific to the accept router. Because some compilers do not like
+empty declarations ("undefined" in the Standard) we put in a dummy value. */
+
+optionlist accept_router_options[] = {
+  { "", opt_hidden, NULL }
+};
+
+/* Size of the options list. An extern variable has to be used so that its
+address can appear in the tables drtables.c. */
+
+int accept_router_options_count =
+  sizeof(accept_router_options)/sizeof(optionlist);
+
+/* Default private options block for the accept router. Again, a dummy
+value is used. */
+
+accept_router_options_block accept_router_option_defaults = {
+  NULL        /* dummy */
+};
+
+
+
+/*************************************************
+*          Initialization entry point            *
+*************************************************/
+
+/* Called for each instance, after its options have been read, to enable
+consistency checks to be done, or anything else that needs to be set up. */
+
+void accept_router_init(router_instance *rblock)
+{
+/*
+accept_router_options_block *ob =
+  (accept_router_options_block *)(rblock->options_block);
+*/
+
+/* By default, log deliveries via this router as local deliveries. We can't
+just leave it as TRUE_UNSET, because the global default is FALSE. */
+
+if (rblock->log_as_local == TRUE_UNSET) rblock->log_as_local = TRUE;
+}
+
+
+
+/*************************************************
+*              Main entry point                  *
+*************************************************/
+
+/* See local README for interface description. This router returns:
+
+DEFER
+  . verifying the errors address caused a deferment or a big disaster such
+      as an expansion failure (rf_get_errors_address)
+  . expanding a headers_{add,remove} string caused a deferment or another
+      expansion error (rf_get_munge_headers)
+  . a problem in rf_get_transport: no transport when one is needed;
+    failed to expand dynamic transport; failed to find dynamic transport
+  . failure to expand or find a uid/gid (rf_get_ugid via rf_queue_add)
+
+OK
+  added address to addr_local or addr_remote, as appropriate for the
+  type of transport
+*/
+
+int accept_router_entry(
+  router_instance *rblock,        /* data for this instantiation */
+  address_item *addr,             /* address we are working on */
+  struct passwd *pw,              /* passwd entry after check_local_user */
+  int verify,                     /* v_none/v_recipient/v_sender/v_expn */
+  address_item **addr_local,      /* add it to this if it's local */
+  address_item **addr_remote,     /* add it to this if it's remote */
+  address_item **addr_new,        /* put new addresses on here */
+  address_item **addr_succeed)    /* put old address here on success */
+{
+/*
+accept_router_options_block *ob =
+  (accept_router_options_block *)(rblock->options_block);
+*/
+int rc;
+uschar *errors_to;
+uschar *remove_headers;
+header_line *extra_headers;
+
+addr_new = addr_new;  /* Keep picky compilers happy */
+addr_succeed = addr_succeed;
+
+DEBUG(D_route) debug_printf("%s router called for %s\n  domain = %s\n",
+  rblock->name, addr->address, addr->domain);
+
+/* Set up the errors address, if any. */
+
+rc = rf_get_errors_address(addr, rblock, verify, &errors_to);
+if (rc != OK) return rc;
+
+/* Set up the additional and removeable headers for the address. */
+
+rc = rf_get_munge_headers(addr, rblock, &extra_headers, &remove_headers);
+if (rc != OK) return rc;
+
+/* Set the transport and accept the address; update its errors address and
+header munging. Initialization ensures that there is a transport except when
+verifying. */
+
+if (!rf_get_transport(rblock->transport_name, &(rblock->transport),
+  addr, rblock->name, NULL)) return DEFER;
+
+addr->transport = rblock->transport;
+addr->p.errors_address = errors_to;
+addr->p.extra_headers = extra_headers;
+addr->p.remove_headers = remove_headers;
+
+return rf_queue_add(addr, addr_local, addr_remote, rblock, pw)? OK : DEFER;
+}
+
+/* End of routers/accept.c */
diff --git a/src/routers/accept.h b/src/routers/accept.h
new file mode 100644 (file)
index 0000000..43494fb
--- /dev/null
@@ -0,0 +1,31 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Private structure for the private options (there aren't any). */
+
+typedef struct {
+  uschar *dummy;
+} accept_router_options_block;
+
+/* Data for reading the private options. */
+
+extern optionlist accept_router_options[];
+extern int accept_router_options_count;
+
+/* Block containing default values. */
+
+extern accept_router_options_block accept_router_option_defaults;
+
+/* The main and initialization entry points for the router */
+
+extern int accept_router_entry(router_instance *, address_item *,
+  struct passwd *, int, address_item **, address_item **,
+  address_item **, address_item **);
+
+extern void accept_router_init(router_instance *);
+
+/* End of routers/accept.h */
diff --git a/src/routers/dnslookup.c b/src/routers/dnslookup.c
new file mode 100644 (file)
index 0000000..c8fd3f9
--- /dev/null
@@ -0,0 +1,432 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+#include "../exim.h"
+#include "rf_functions.h"
+#include "dnslookup.h"
+
+
+
+/* Options specific to the dnslookup router. */
+
+optionlist dnslookup_router_options[] = {
+  { "check_secondary_mx", opt_bool,
+      (void *)(offsetof(dnslookup_router_options_block, check_secondary_mx)) },
+  { "check_srv",          opt_stringptr,
+      (void *)(offsetof(dnslookup_router_options_block, check_srv)) },
+  { "dnssec_request_domains",         opt_stringptr,
+      (void *)(offsetof(dnslookup_router_options_block, dnssec_request_domains)) },
+  { "dnssec_require_domains",         opt_stringptr,
+      (void *)(offsetof(dnslookup_router_options_block, dnssec_require_domains)) },
+  { "mx_domains",         opt_stringptr,
+      (void *)(offsetof(dnslookup_router_options_block, mx_domains)) },
+  { "mx_fail_domains",    opt_stringptr,
+      (void *)(offsetof(dnslookup_router_options_block, mx_fail_domains)) },
+  { "qualify_single",     opt_bool,
+      (void *)(offsetof(dnslookup_router_options_block, qualify_single)) },
+  { "rewrite_headers",    opt_bool,
+      (void *)(offsetof(dnslookup_router_options_block, rewrite_headers)) },
+  { "same_domain_copy_routing", opt_bool|opt_public,
+      (void *)(offsetof(router_instance, same_domain_copy_routing)) },
+  { "search_parents",     opt_bool,
+      (void *)(offsetof(dnslookup_router_options_block, search_parents)) },
+  { "srv_fail_domains",   opt_stringptr,
+      (void *)(offsetof(dnslookup_router_options_block, srv_fail_domains)) },
+  { "widen_domains",      opt_stringptr,
+      (void *)(offsetof(dnslookup_router_options_block, widen_domains)) }
+};
+
+/* Size of the options list. An extern variable has to be used so that its
+address can appear in the tables drtables.c. */
+
+int dnslookup_router_options_count =
+  sizeof(dnslookup_router_options)/sizeof(optionlist);
+
+/* Default private options block for the dnslookup router. */
+
+dnslookup_router_options_block dnslookup_router_option_defaults = {
+  FALSE,           /* check_secondary_mx */
+  TRUE,            /* qualify_single */
+  FALSE,           /* search_parents */
+  TRUE,            /* rewrite_headers */
+  NULL,            /* widen_domains */
+  NULL,            /* mx_domains */
+  NULL,            /* mx_fail_domains */
+  NULL,            /* srv_fail_domains */
+  NULL,            /* check_srv */
+  NULL,            /* dnssec_request_domains */
+  NULL             /* dnssec_require_domains */
+};
+
+
+
+/*************************************************
+*          Initialization entry point            *
+*************************************************/
+
+/* Called for each instance, after its options have been read, to enable
+consistency checks to be done, or anything else that needs to be set up. */
+
+void
+dnslookup_router_init(router_instance *rblock)
+{
+/*
+dnslookup_router_options_block *ob =
+  (dnslookup_router_options_block *)(rblock->options_block);
+*/
+rblock = rblock;
+}
+
+
+
+/*************************************************
+*              Main entry point                  *
+*************************************************/
+
+/* See local README for interface details. This router returns:
+
+DECLINE
+  . the domain does not exist in the DNS
+  . MX records point to non-existent hosts (including RHS = IP address)
+  . a single SRV record has a host name of "." (=> no service)
+  . syntactically invalid mail domain
+  . check_secondary_mx set, and local host not in host list
+
+DEFER
+  . lookup defer for mx_domains
+  . timeout etc on DNS lookup
+  . verifying the errors address caused a deferment or a big disaster such
+      as an expansion failure (rf_get_errors_address)
+  . expanding a headers_{add,remove} string caused a deferment or another
+      expansion error (rf_get_munge_headers)
+  . a problem in rf_get_transport: no transport when one is needed;
+      failed to expand dynamic transport; failed to find dynamic transport
+  . failure to expand or find a uid/gid (rf_get_ugid via rf_queue_add)
+  . self = "freeze", self = "defer"
+
+PASS
+  . timeout etc on DNS lookup and pass_on_timeout set
+  . self = "pass"
+
+REROUTED
+  . routed to local host, but name was expanded by DNS lookup, so a
+      re-routing should take place
+  . self = "reroute"
+
+  In both cases the new address will have been set up as a child
+
+FAIL
+  . self = "fail"
+
+OK
+  added address to addr_local or addr_remote, as appropriate for the
+  type of transport; this includes the self="send" case.
+*/
+
+int
+dnslookup_router_entry(
+  router_instance *rblock,        /* data for this instantiation */
+  address_item *addr,             /* address we are working on */
+  struct passwd *pw,              /* passwd entry after check_local_user */
+  int verify,                     /* v_none/v_recipient/v_sender/v_expn */
+  address_item **addr_local,      /* add it to this if it's local */
+  address_item **addr_remote,     /* add it to this if it's remote */
+  address_item **addr_new,        /* put new addresses on here */
+  address_item **addr_succeed)    /* put old address here on success */
+{
+host_item h;
+int rc;
+int widen_sep = 0;
+int whichrrs = HOST_FIND_BY_MX | HOST_FIND_BY_A;
+dnslookup_router_options_block *ob =
+  (dnslookup_router_options_block *)(rblock->options_block);
+uschar *srv_service = NULL;
+uschar *widen = NULL;
+uschar *pre_widen = addr->domain;
+uschar *post_widen = NULL;
+uschar *fully_qualified_name;
+uschar *listptr;
+uschar widen_buffer[256];
+
+addr_new = addr_new;          /* Keep picky compilers happy */
+addr_succeed = addr_succeed;
+
+DEBUG(D_route)
+  debug_printf("%s router called for %s\n  domain = %s\n",
+    rblock->name, addr->address, addr->domain);
+
+/* If an SRV check is required, expand the service name */
+
+if (ob->check_srv != NULL)
+  {
+  srv_service = expand_string(ob->check_srv);
+  if (srv_service == NULL && !expand_string_forcedfail)
+    {
+    addr->message = string_sprintf("%s router: failed to expand \"%s\": %s",
+      rblock->name, ob->check_srv, expand_string_message);
+    return DEFER;
+    }
+  else whichrrs |= HOST_FIND_BY_SRV;
+  }
+
+/* Set up the first of any widening domains. The code further down copes with
+either pre- or post-widening, but at present there is no way to turn on
+pre-widening, as actually doing so seems like a rather bad idea, and nobody has
+requested it. Pre-widening would cause local abbreviated names to take
+precedence over global names. For example, if the domain is "xxx.ch" it might
+be something in the "ch" toplevel domain, but it also might be xxx.ch.xyz.com.
+The choice of pre- or post-widening affects which takes precedence. If ever
+somebody comes up with some kind of requirement for pre-widening, presumably
+with some conditions under which it is done, it can be selected here.
+
+The rewrite_headers option works only when routing an address at transport
+time, because the alterations to the headers are not persistent so must be
+worked out immediately before they are used. Sender addresses are routed for
+verification purposes, but never at transport time, so any header changes that
+you might expect as a result of sender domain widening do not occur. Therefore
+we do not perform widening when verifying sender addresses; however, widening
+sender addresses is OK if we do not have to rewrite the headers. A corollary
+of this is that if the current address is not the original address, then it
+does not appear in the message header so it is also OK to widen. The
+suppression of widening for sender addresses is silent because it is the
+normal desirable behaviour. */
+
+if (ob->widen_domains != NULL &&
+    (verify != v_sender || !ob->rewrite_headers || addr->parent != NULL))
+  {
+  listptr = ob->widen_domains;
+  widen = string_nextinlist(&listptr, &widen_sep, widen_buffer,
+    sizeof(widen_buffer));
+
+/****
+  if (some condition requiring pre-widening)
+    {
+    post_widen = pre_widen;
+    pre_widen = NULL;
+    }
+****/
+  }
+
+/* Loop to cope with explicit widening of domains as configured. This code
+copes with widening that may happen before or after the original name. The
+decision as to which is taken above. */
+
+for (;;)
+  {
+  int flags = whichrrs;
+  BOOL removed = FALSE;
+
+  if (pre_widen != NULL)
+    {
+    h.name = pre_widen;
+    pre_widen = NULL;
+    }
+  else if (widen != NULL)
+    {
+    h.name = string_sprintf("%s.%s", addr->domain, widen);
+    widen = string_nextinlist(&listptr, &widen_sep, widen_buffer,
+      sizeof(widen_buffer));
+    DEBUG(D_route) debug_printf("%s router widened %s to %s\n", rblock->name,
+      addr->domain, h.name);
+    }
+  else if (post_widen != NULL)
+    {
+    h.name = post_widen;
+    post_widen = NULL;
+    DEBUG(D_route) debug_printf("%s router trying %s after widening failed\n",
+      rblock->name, h.name);
+    }
+  else return DECLINE;
+
+  /* Set up the rest of the initial host item. Others may get chained on if
+  there is more than one IP address. We set it up here instead of outside the
+  loop so as to re-initialize if a previous try succeeded but was rejected
+  because of not having an MX record. */
+
+  h.next = NULL;
+  h.address = NULL;
+  h.port = PORT_NONE;
+  h.mx = MX_NONE;
+  h.status = hstatus_unknown;
+  h.why = hwhy_unknown;
+  h.last_try = 0;
+
+  /* Unfortunately, we cannot set the mx_only option in advance, because the
+  DNS lookup may extend an unqualified name. Therefore, we must do the test
+  subsequently. We use the same logic as that for widen_domains above to avoid
+  requesting a header rewrite that cannot work. */
+
+  if (verify != v_sender || !ob->rewrite_headers || addr->parent != NULL)
+    {
+    if (ob->qualify_single) flags |= HOST_FIND_QUALIFY_SINGLE;
+    if (ob->search_parents) flags |= HOST_FIND_SEARCH_PARENTS;
+    }
+
+  rc = host_find_bydns(&h, rblock->ignore_target_hosts, flags, srv_service,
+    ob->srv_fail_domains, ob->mx_fail_domains,
+    ob->dnssec_request_domains, ob->dnssec_require_domains,
+    &fully_qualified_name, &removed);
+  if (removed) setflag(addr, af_local_host_removed);
+
+  /* If host found with only address records, test for the domain's being in
+  the mx_domains list. Note that this applies also to SRV records; the name of
+  the option is historical. */
+
+  if ((rc == HOST_FOUND || rc == HOST_FOUND_LOCAL) && h.mx < 0 &&
+       ob->mx_domains != NULL)
+    {
+    switch(match_isinlist(fully_qualified_name, &(ob->mx_domains), 0,
+          &domainlist_anchor, addr->domain_cache, MCL_DOMAIN, TRUE, NULL))
+      {
+      case DEFER:
+      addr->message = US"lookup defer for mx_domains";
+      return DEFER;
+
+      case OK:
+      DEBUG(D_route) debug_printf("%s router rejected %s: no MX record(s)\n",
+        rblock->name, fully_qualified_name);
+      continue;
+      }
+    }
+
+  /* Deferral returns forthwith, and anything other than failure breaks the
+  loop. */
+
+  if (rc == HOST_FIND_AGAIN)
+    {
+    if (rblock->pass_on_timeout)
+      {
+      DEBUG(D_route) debug_printf("%s router timed out, and pass_on_timeout is set\n",
+        rblock->name);
+      return PASS;
+      }
+    addr->message = US"host lookup did not complete";
+    return DEFER;
+    }
+
+  if (rc != HOST_FIND_FAILED) break;
+
+  /* Check to see if the failure is the result of MX records pointing to
+  non-existent domains, and if so, set an appropriate error message; the case
+  of an MX or SRV record pointing to "." is another special case that we can
+  detect. Otherwise "unknown mail domain" is used, which is confusing. Also, in
+  this case don't do the widening. We need check only the first host to see if
+  its MX has been filled in, but there is no address, because if there were any
+  usable addresses returned, we would not have had HOST_FIND_FAILED.
+
+  As a common cause of this problem is MX records with IP addresses on the
+  RHS, give a special message in this case. */
+
+  if (h.mx >= 0 && h.address == NULL)
+    {
+    setflag(addr, af_pass_message);   /* This is not a security risk */
+    if (h.name[0] == 0)
+      addr->message = US"an MX or SRV record indicated no SMTP service";
+    else
+      {
+      addr->message = US"all relevant MX records point to non-existent hosts";
+      if (!allow_mx_to_ip && string_is_ip_address(h.name, NULL) != 0)
+        {
+        addr->user_message =
+          string_sprintf("It appears that the DNS operator for %s\n"
+            "has installed an invalid MX record with an IP address\n"
+            "instead of a domain name on the right hand side.", addr->domain);
+        addr->message = string_sprintf("%s or (invalidly) to IP addresses",
+          addr->message);
+        }
+      }
+    return DECLINE;
+    }
+
+  /* If there's a syntax error, do not continue with any widening, and note
+  the error. */
+
+  if (host_find_failed_syntax)
+    {
+    addr->message = string_sprintf("mail domain \"%s\" is syntactically "
+      "invalid", h.name);
+    return DECLINE;
+    }
+  }
+
+/* If the original domain name has been changed as a result of the host lookup,
+set up a child address for rerouting and request header rewrites if so
+configured. Then yield REROUTED. However, if the only change is a change of
+case in the domain name, which some resolvers yield (others don't), just change
+the domain name in the original address so that the official version is used in
+RCPT commands. */
+
+if (Ustrcmp(addr->domain, fully_qualified_name) != 0)
+  {
+  if (strcmpic(addr->domain, fully_qualified_name) == 0)
+    {
+    uschar *at = Ustrrchr(addr->address, '@');
+    memcpy(at+1, fully_qualified_name, Ustrlen(at+1));
+    }
+  else
+    {
+    rf_change_domain(addr, fully_qualified_name, ob->rewrite_headers, addr_new);
+    return REROUTED;
+    }
+  }
+
+/* If the yield is HOST_FOUND_LOCAL, the remote domain name either found MX
+records with the lowest numbered one pointing to a host with an IP address that
+is set on one of the interfaces of this machine, or found A records or got
+addresses from gethostbyname() that contain one for this machine. This can
+happen quite legitimately if the original name was a shortened form of a
+domain, but we will have picked that up already via the name change test above.
+
+Otherwise, the action to be taken can be configured by the self option, the
+handling of which is in a separate function, as it is also required for other
+routers. */
+
+if (rc == HOST_FOUND_LOCAL)
+  {
+  rc = rf_self_action(addr, &h, rblock->self_code, rblock->self_rewrite,
+    rblock->self, addr_new);
+  if (rc != OK) return rc;
+  }
+
+/* Otherwise, insist on being a secondary MX if so configured */
+
+else if (ob->check_secondary_mx && !testflag(addr, af_local_host_removed))
+  {
+  DEBUG(D_route) debug_printf("check_secondary_mx set and local host not secondary\n");
+  return DECLINE;
+  }
+
+/* Set up the errors address, if any. */
+
+rc = rf_get_errors_address(addr, rblock, verify, &(addr->p.errors_address));
+if (rc != OK) return rc;
+
+/* Set up the additional and removeable headers for this address. */
+
+rc = rf_get_munge_headers(addr, rblock, &(addr->p.extra_headers),
+  &(addr->p.remove_headers));
+if (rc != OK) return rc;
+
+/* Get store in which to preserve the original host item, chained on
+to the address. */
+
+addr->host_list = store_get(sizeof(host_item));
+addr->host_list[0] = h;
+
+/* Fill in the transport and queue the address for delivery. */
+
+if (!rf_get_transport(rblock->transport_name, &(rblock->transport),
+      addr, rblock->name, NULL))
+  return DEFER;
+
+addr->transport = rblock->transport;
+
+return rf_queue_add(addr, addr_local, addr_remote, rblock, pw)?
+  OK : DEFER;
+}
+
+/* End of routers/dnslookup.c */
diff --git a/src/routers/dnslookup.h b/src/routers/dnslookup.h
new file mode 100644 (file)
index 0000000..518b7f4
--- /dev/null
@@ -0,0 +1,41 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Private structure for the private options. */
+
+typedef struct {
+  BOOL check_secondary_mx;
+  BOOL qualify_single;
+  BOOL search_parents;
+  BOOL rewrite_headers;
+  uschar *widen_domains;
+  uschar *mx_domains;
+  uschar *mx_fail_domains;
+  uschar *srv_fail_domains;
+  uschar *check_srv;
+  uschar *dnssec_request_domains;
+  uschar *dnssec_require_domains;
+} dnslookup_router_options_block;
+
+/* Data for reading the private options. */
+
+extern optionlist dnslookup_router_options[];
+extern int dnslookup_router_options_count;
+
+/* Block containing default values. */
+
+extern dnslookup_router_options_block dnslookup_router_option_defaults;
+
+/* The main and initialization entry points for the router */
+
+extern int dnslookup_router_entry(router_instance *, address_item *,
+  struct passwd *, int, address_item **, address_item **,
+  address_item **, address_item **);
+
+extern void dnslookup_router_init(router_instance *);
+
+/* End of routers/dnslookup.h */
diff --git a/src/routers/ipliteral.c b/src/routers/ipliteral.c
new file mode 100644 (file)
index 0000000..7be96e5
--- /dev/null
@@ -0,0 +1,200 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+
+#include "../exim.h"
+#include "rf_functions.h"
+#include "ipliteral.h"
+
+
+/* Options specific to the ipliteral router. Because some compilers do not like
+empty declarations ("undefined" in the Standard) we put in a dummy value. */
+
+optionlist ipliteral_router_options[] = {
+  { "", opt_hidden, NULL }
+};
+
+/* Size of the options list. An extern variable has to be used so that its
+address can appear in the tables drtables.c. */
+
+int ipliteral_router_options_count =
+  sizeof(ipliteral_router_options)/sizeof(optionlist);
+
+/* Default private options block for the ipliteral router. Again, a dummy
+value is present to keep some compilers happy. */
+
+ipliteral_router_options_block ipliteral_router_option_defaults = { 0 };
+
+
+
+/*************************************************
+*          Initialization entry point            *
+*************************************************/
+
+/* Called for each instance, after its options have been read, to enable
+consistency checks to be done, or anything else that needs to be set up. */
+
+void
+ipliteral_router_init(router_instance *rblock)
+{
+/*
+ipliteral_router_options_block *ob =
+  (ipliteral_router_options_block *)(rblock->options_block);
+*/
+rblock = rblock;
+}
+
+
+
+/*************************************************
+*              Main entry point                  *
+*************************************************/
+
+/* See local README for interface details. This router returns:
+
+DECLINE
+  . the domain is not in the form of an IP literal
+
+DEFER
+  . verifying the errors address caused a deferment or a big disaster such
+      as an expansion failure (rf_get_errors_address)
+  . expanding a headers_{add,remove} string caused a deferment or another
+      expansion error (rf_get_munge_headers)
+  . a problem in rf_get_transport: no transport when one is needed;
+      failed to expand dynamic transport; failed to find dynamic transport
+  . failure to expand or find a uid/gid (rf_get_ugid via rf_queue_add)
+  . self = "freeze", self = "defer"
+
+PASS
+  . self = "pass"
+
+REROUTED
+  . self = "reroute"
+
+FAIL
+  . self = "fail"
+
+OK
+  added address to addr_local or addr_remote, as appropriate for the
+  type of transport; this includes the self="send" case.
+*/
+
+int
+ipliteral_router_entry(
+  router_instance *rblock,        /* data for this instantiation */
+  address_item *addr,             /* address we are working on */
+  struct passwd *pw,              /* passwd entry after check_local_user */
+  int verify,                     /* v_none/v_recipient/v_sender/v_expn */
+  address_item **addr_local,      /* add it to this if it's local */
+  address_item **addr_remote,     /* add it to this if it's remote */
+  address_item **addr_new,        /* put new addresses on here */
+  address_item **addr_succeed)    /* put old address here on success */
+{
+/*
+ipliteral_router_options_block *ob =
+  (ipliteral_router_options_block *)(rblock->options_block);
+*/
+host_item *h;
+uschar *domain = addr->domain;
+uschar *ip;
+int len = Ustrlen(domain);
+int rc, ipv;
+
+addr_new = addr_new;         /* Keep picky compilers happy */
+addr_succeed = addr_succeed;
+
+DEBUG(D_route) debug_printf("%s router called for %s: domain = %s\n",
+  rblock->name, addr->address, addr->domain);
+
+/* Check that the domain is an IP address enclosed in square brackets. Remember
+to allow for the "official" form of IPv6 addresses. If not, the router
+declines. Otherwise route to the single IP address, setting the host name to
+"(unnamed)". */
+
+if (domain[0] != '[' || domain[len-1] != ']') return DECLINE;
+domain[len-1] = 0;  /* temporarily */
+
+ip = domain + 1;
+if (strncmpic(ip, US"IPV6:", 5) == 0 || strncmpic(ip, US"IPV4:", 5) == 0)
+  ip += 5;
+
+ipv = string_is_ip_address(ip, NULL);
+if (ipv == 0 || (disable_ipv6 && ipv == 6))
+  {
+  domain[len-1] = ']';
+  return DECLINE;
+  }
+
+/* It seems unlikely that ignore_target_hosts will be used with this router,
+but if it is set, it should probably work. */
+
+if (verify_check_this_host(&(rblock->ignore_target_hosts), NULL, domain,
+      ip, NULL) == OK)
+  {
+  DEBUG(D_route)
+      debug_printf("%s is in ignore_target_hosts\n", ip);
+  addr->message = US"IP literal host explicitly ignored";
+  domain[len-1] = ']';
+  return DECLINE;
+  }
+
+/* Set up a host item */
+
+h = store_get(sizeof(host_item));
+
+h->next = NULL;
+h->address = string_copy(ip);
+h->port = PORT_NONE;
+domain[len-1] = ']';   /* restore */
+h->name = string_copy(domain);
+h->mx = MX_NONE;
+h->status = hstatus_unknown;
+h->why = hwhy_unknown;
+h->last_try = 0;
+
+/* Determine whether the host is the local host, and if so, take action
+according to the configuration. */
+
+if (host_scan_for_local_hosts(h, &h, NULL) == HOST_FOUND_LOCAL)
+  {
+  int rc = rf_self_action(addr, h, rblock->self_code, rblock->self_rewrite,
+    rblock->self, addr_new);
+  if (rc != OK) return rc;
+  }
+
+/* Address is routed to this host */
+
+addr->host_list = h;
+
+/* Set up the errors address, if any. */
+
+rc = rf_get_errors_address(addr, rblock, verify, &(addr->p.errors_address));
+if (rc != OK) return rc;
+
+/* Set up the additional and removeable headers for this address. */
+
+rc = rf_get_munge_headers(addr, rblock, &(addr->p.extra_headers),
+  &(addr->p.remove_headers));
+if (rc != OK) return rc;
+
+/* Fill in the transport, queue the address for local or remote delivery, and
+yield success. For local delivery, of course, the IP address won't be used. If
+just verifying, there need not be a transport, in which case it doesn't matter
+which queue we put the address on. This is all now handled by the route_queue()
+function. */
+
+if (!rf_get_transport(rblock->transport_name, &(rblock->transport),
+      addr, rblock->name, NULL))
+  return DEFER;
+
+addr->transport = rblock->transport;
+
+return rf_queue_add(addr, addr_local, addr_remote, rblock, pw)?
+  OK : DEFER;
+}
+
+/* End of routers/ipliteral.c */
diff --git a/src/routers/ipliteral.h b/src/routers/ipliteral.h
new file mode 100644 (file)
index 0000000..1ddb38b
--- /dev/null
@@ -0,0 +1,34 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+
+/* Private structure for the private options. Some compilers do not like empty
+structures - the Standard, alas, says "undefined behaviour" for an empty
+structure - so we have to put in a dummy value. */
+
+typedef struct {
+  int dummy;
+} ipliteral_router_options_block;
+
+/* Data for reading the private options. */
+
+extern optionlist ipliteral_router_options[];
+extern int ipliteral_router_options_count;
+
+/* Block containing default values. */
+
+extern ipliteral_router_options_block ipliteral_router_option_defaults;
+
+/* The main and initialization entry points for the router */
+
+extern int ipliteral_router_entry(router_instance *, address_item *,
+  struct passwd *, int, address_item **, address_item **,
+  address_item **, address_item **);
+
+extern void ipliteral_router_init(router_instance *);
+
+/* End of routers/ipliteral.h */
diff --git a/src/routers/iplookup.c b/src/routers/iplookup.c
new file mode 100644 (file)
index 0000000..3728007
--- /dev/null
@@ -0,0 +1,401 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+
+#include "../exim.h"
+#include "rf_functions.h"
+#include "iplookup.h"
+
+
+/* IP connection types */
+
+#define ip_udp 0
+#define ip_tcp 1
+
+
+/* Options specific to the iplookup router. */
+
+optionlist iplookup_router_options[] = {
+  { "hosts",    opt_stringptr,
+      (void *)(offsetof(iplookup_router_options_block, hosts)) },
+  { "optional", opt_bool,
+      (void *)(offsetof(iplookup_router_options_block, optional)) },
+  { "port",     opt_int,
+      (void *)(offsetof(iplookup_router_options_block, port)) },
+  { "protocol", opt_stringptr,
+      (void *)(offsetof(iplookup_router_options_block, protocol_name)) },
+  { "query",    opt_stringptr,
+      (void *)(offsetof(iplookup_router_options_block, query)) },
+  { "reroute",  opt_stringptr,
+      (void *)(offsetof(iplookup_router_options_block, reroute)) },
+  { "response_pattern", opt_stringptr,
+      (void *)(offsetof(iplookup_router_options_block, response_pattern)) },
+  { "timeout",  opt_time,
+      (void *)(offsetof(iplookup_router_options_block, timeout)) }
+};
+
+/* Size of the options list. An extern variable has to be used so that its
+address can appear in the tables drtables.c. */
+
+int iplookup_router_options_count =
+  sizeof(iplookup_router_options)/sizeof(optionlist);
+
+/* Default private options block for the iplookup router. */
+
+iplookup_router_options_block iplookup_router_option_defaults = {
+  -1,       /* port */
+  ip_udp,   /* protocol */
+  5,        /* timeout */
+  NULL,     /* protocol_name */
+  NULL,     /* hosts */
+  NULL,     /* query; NULL => local_part@domain */
+  NULL,     /* response_pattern; NULL => don't apply regex */
+  NULL,     /* reroute; NULL => just used returned data */
+  NULL,     /* re_response_pattern; compiled pattern */
+  FALSE     /* optional */
+};
+
+
+
+/*************************************************
+*          Initialization entry point            *
+*************************************************/
+
+/* Called for each instance, after its options have been read, to enable
+consistency checks to be done, or anything else that needs to be set up. */
+
+void
+iplookup_router_init(router_instance *rblock)
+{
+iplookup_router_options_block *ob =
+  (iplookup_router_options_block *)(rblock->options_block);
+
+/* A port and a host list must be given */
+
+if (ob->port < 0)
+  log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s router:\n  "
+    "a port must be specified", rblock->name);
+
+if (ob->hosts == NULL)
+  log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s router:\n  "
+    "a host list must be specified", rblock->name);
+
+/* Translate protocol name into value */
+
+if (ob->protocol_name != NULL)
+  {
+  if (Ustrcmp(ob->protocol_name, "udp") == 0) ob->protocol = ip_udp;
+  else if (Ustrcmp(ob->protocol_name, "tcp") == 0) ob->protocol = ip_tcp;
+  else log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s router:\n  "
+    "protocol not specified as udp or tcp", rblock->name);
+  }
+
+/* If a response pattern is given, compile it now to get the error early. */
+
+if (ob->response_pattern != NULL)
+  ob->re_response_pattern =
+    regex_must_compile(ob->response_pattern, FALSE, TRUE);
+}
+
+
+
+/*************************************************
+*              Main entry point                  *
+*************************************************/
+
+/* See local README for interface details. This router returns:
+
+DECLINE
+  . pattern or identification match on returned data failed
+
+DEFER
+  . failed to expand the query or rerouting string
+  . failed to create socket ("optional" not set)
+  . failed to find a host, failed to connect, timed out ("optional" not set)
+  . rerouting string is not in the form localpart@domain
+  . verifying the errors address caused a deferment or a big disaster such
+      as an expansion failure (rf_get_errors_address)
+  . expanding a headers_{add,remove} string caused a deferment or another
+      expansion error (rf_get_munge_headers)
+
+PASS
+  . failed to create socket ("optional" set)
+  . failed to find a host, failed to connect, timed out ("optional" set)
+
+OK
+  . new address added to addr_new
+*/
+
+int
+iplookup_router_entry(
+  router_instance *rblock,        /* data for this instantiation */
+  address_item *addr,             /* address we are working on */
+  struct passwd *pw,              /* passwd entry after check_local_user */
+  int verify,                     /* v_none/v_recipient/v_sender/v_expn */
+  address_item **addr_local,      /* add it to this if it's local */
+  address_item **addr_remote,     /* add it to this if it's remote */
+  address_item **addr_new,        /* put new addresses on here */
+  address_item **addr_succeed)    /* put old address here on success */
+{
+uschar *query = NULL;
+uschar *reply;
+uschar *hostname, *reroute, *domain, *listptr;
+uschar host_buffer[256];
+host_item *host = store_get(sizeof(host_item));
+address_item *new_addr;
+iplookup_router_options_block *ob =
+  (iplookup_router_options_block *)(rblock->options_block);
+const pcre *re = ob->re_response_pattern;
+int count, query_len, rc;
+int sep = 0;
+
+addr_local = addr_local;    /* Keep picky compilers happy */
+addr_remote = addr_remote;
+addr_succeed = addr_succeed;
+pw = pw;
+
+DEBUG(D_route) debug_printf("%s router called for %s: domain = %s\n",
+  rblock->name, addr->address, addr->domain);
+
+reply = store_get(256);
+
+/* Build the query string to send. If not explicitly given, a default of
+"user@domain user@domain" is used. */
+
+if (ob->query == NULL)
+  query = string_sprintf("%s@%s %s@%s", addr->local_part, addr->domain,
+    addr->local_part, addr->domain);
+else
+  {
+  query = expand_string(ob->query);
+  if (query == NULL)
+    {
+    addr->message = string_sprintf("%s router: failed to expand %s: %s",
+      rblock->name, ob->query, expand_string_message);
+    return DEFER;
+    }
+  }
+
+query_len = Ustrlen(query);
+DEBUG(D_route) debug_printf("%s router query is \"%s\"\n", rblock->name,
+  string_printing(query));
+
+/* Now connect to the required port for each of the hosts in turn, until a
+response it received. Initialization insists on the port being set and there
+being a host list. */
+
+listptr = ob->hosts;
+while ((hostname = string_nextinlist(&listptr, &sep, host_buffer,
+       sizeof(host_buffer))) != NULL)
+  {
+  host_item *h;
+
+  DEBUG(D_route) debug_printf("calling host %s\n", hostname);
+
+  host->name = hostname;
+  host->address = NULL;
+  host->port = PORT_NONE;
+  host->mx = MX_NONE;
+  host->next = NULL;
+
+  if (string_is_ip_address(host->name, NULL) != 0)
+    host->address = host->name;
+  else
+    {
+    int rc = host_find_byname(host, NULL, HOST_FIND_QUALIFY_SINGLE, NULL, TRUE);
+    if (rc == HOST_FIND_FAILED || rc == HOST_FIND_AGAIN) continue;
+    }
+
+  /* Loop for possible multiple IP addresses for the given name. */
+
+  for (h = host; h != NULL; h = h->next)
+    {
+    int host_af, query_socket;
+
+    /* Skip any hosts for which we have no address */
+
+    if (h->address == NULL) continue;
+
+    /* Create a socket, for UDP or TCP, as configured. IPv6 addresses are
+    detected by checking for a colon in the address. */
+
+    host_af = (Ustrchr(h->address, ':') != NULL)? AF_INET6 : AF_INET;
+    query_socket = ip_socket((ob->protocol == ip_udp)? SOCK_DGRAM:SOCK_STREAM,
+      host_af);
+    if (query_socket < 0)
+      {
+      if (ob->optional) return PASS;
+      addr->message = string_sprintf("failed to create socket in %s router",
+        rblock->name);
+      return DEFER;
+      }
+
+    /* Connect to the remote host, under a timeout. In fact, timeouts can occur
+    here only for TCP calls; for a UDP socket, "connect" always works (the
+    router will timeout later on the read call). */
+
+    if (ip_connect(query_socket, host_af, h->address,ob->port, ob->timeout) < 0)
+      {
+      close(query_socket);
+      DEBUG(D_route)
+        debug_printf("connection to %s failed: %s\n", h->address,
+          strerror(errno));
+      continue;
+      }
+
+    /* Send the query. If it fails, just continue with the next address. */
+
+    if (send(query_socket, query, query_len, 0) < 0)
+      {
+      DEBUG(D_route) debug_printf("send to %s failed\n", h->address);
+      (void)close(query_socket);
+      continue;
+      }
+
+    /* Read the response and close the socket. If the read fails, try the
+    next IP address. */
+
+    count = ip_recv(query_socket, reply, sizeof(reply) - 1, ob->timeout);
+    (void)close(query_socket);
+    if (count <= 0)
+      {
+      DEBUG(D_route) debug_printf("%s from %s\n", (errno == ETIMEDOUT)?
+        "timed out" : "recv failed", h->address);
+      *reply = 0;
+      continue;
+      }
+
+    /* Success; break the loop */
+
+    reply[count] = 0;
+    DEBUG(D_route) debug_printf("%s router received \"%s\" from %s\n",
+      rblock->name, string_printing(reply), h->address);
+    break;
+    }
+
+  /* If h == NULL we have tried all the IP addresses and failed on all of them,
+  so we must continue to try more host names. Otherwise we have succeeded. */
+
+  if (h != NULL) break;
+  }
+
+
+/* If hostname is NULL, we have failed to find any host, or failed to
+connect to any of the IP addresses, or timed out while reading or writing to
+those we have connected to. In all cases, we must pass if optional and
+defer otherwise. */
+
+if (hostname == NULL)
+  {
+  DEBUG(D_route) debug_printf("%s router failed to get anything\n", rblock->name);
+  if (ob->optional) return PASS;
+  addr->message = string_sprintf("%s router: failed to communicate with any "
+    "host", rblock->name);
+  return DEFER;
+  }
+
+
+/* If a response pattern was supplied, match the returned string against it. A
+failure to match causes the router to decline. After a successful match, the
+numerical variables for expanding the rerouted address are set up. */
+
+if (re != NULL)
+  {
+  if (!regex_match_and_setup(re, reply, 0, -1))
+    {
+    DEBUG(D_route) debug_printf("%s router: %s failed to match response %s\n",
+      rblock->name, ob->response_pattern, reply);
+    return DECLINE;
+    }
+  }
+
+
+/* If no response pattern was supplied, set up $0 as the response up to the
+first white space (if any). Also, if no query was specified, check that what
+follows the white space matches user@domain. */
+
+else
+  {
+  int n = 0;
+  while (reply[n] != 0 && !isspace(reply[n])) n++;
+  expand_nmax = 0;
+  expand_nstring[0] = reply;
+  expand_nlength[0] = n;
+
+  if (ob->query == NULL)
+    {
+    int nn = n;
+    while (isspace(reply[nn])) nn++;
+    if (Ustrcmp(query + query_len/2 + 1, reply+nn) != 0)
+      {
+      DEBUG(D_route) debug_printf("%s router: failed to match identification "
+        "in response %s\n", rblock->name, reply);
+      return DECLINE;
+      }
+    }
+
+  reply[n] = 0;  /* Terminate for the default case */
+  }
+
+/* If an explicit rerouting string is specified, expand it. Otherwise, use
+what was sent back verbatim. */
+
+if (ob->reroute != NULL)
+  {
+  reroute = expand_string(ob->reroute);
+  expand_nmax = -1;
+  if (reroute == NULL)
+    {
+    addr->message = string_sprintf("%s router: failed to expand %s: %s",
+      rblock->name, ob->reroute, expand_string_message);
+    return DEFER;
+    }
+  }
+else reroute = reply;
+
+/* We should now have a new address in the form user@domain. */
+
+domain = Ustrchr(reroute, '@');
+if (domain == NULL)
+  {
+  log_write(0, LOG_MAIN, "%s router: reroute string %s is not of the form "
+    "user@domain", rblock->name, reroute);
+  addr->message = string_sprintf("%s router: reroute string %s is not of the "
+    "form user@domain", rblock->name, reroute);
+  return DEFER;
+  }
+
+/* Create a child address with the old one as parent. Put the new address on
+the chain of new addressess. */
+
+new_addr = deliver_make_addr(reroute, TRUE);
+new_addr->parent = addr;
+
+copyflag(new_addr, addr, af_propagate);
+new_addr->p = addr->p;
+
+if (addr->child_count == SHRT_MAX)
+  log_write(0, LOG_MAIN|LOG_PANIC_DIE, "%s router generated more than %d "
+    "child addresses for <%s>", rblock->name, SHRT_MAX, addr->address);
+addr->child_count++;
+new_addr->next = *addr_new;
+*addr_new = new_addr;
+
+/* Set up the errors address, if any, and the additional and removeable headers
+for this new address. */
+
+rc = rf_get_errors_address(addr, rblock, verify, &(new_addr->p.errors_address));
+if (rc != OK) return rc;
+
+rc = rf_get_munge_headers(addr, rblock, &(new_addr->p.extra_headers),
+  &(new_addr->p.remove_headers));
+if (rc != OK) return rc;
+
+return OK;
+}
+
+/* End of routers/iplookup.c */
diff --git a/src/routers/iplookup.h b/src/routers/iplookup.h
new file mode 100644 (file)
index 0000000..d5217ef
--- /dev/null
@@ -0,0 +1,41 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+
+/* Private structure for the private options. */
+
+typedef struct {
+  int   port;
+  int   protocol;
+  int   timeout;
+  uschar *protocol_name;
+  uschar *hosts;
+  uschar *query;
+  uschar *response_pattern;
+  uschar *reroute;
+  const pcre *re_response_pattern;
+  BOOL  optional;
+} iplookup_router_options_block;
+
+/* Data for reading the private options. */
+
+extern optionlist iplookup_router_options[];
+extern int iplookup_router_options_count;
+
+/* Block containing default values. */
+
+extern iplookup_router_options_block iplookup_router_option_defaults;
+
+/* The main and initialization entry points for the router */
+
+extern int iplookup_router_entry(router_instance *, address_item *,
+  struct passwd *, int, address_item **, address_item **,
+  address_item **, address_item **);
+
+extern void iplookup_router_init(router_instance *);
+
+/* End of routers/iplookup.h */
diff --git a/src/routers/manualroute.c b/src/routers/manualroute.c
new file mode 100644 (file)
index 0000000..cb047e2
--- /dev/null
@@ -0,0 +1,477 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+
+#include "../exim.h"
+#include "rf_functions.h"
+#include "manualroute.h"
+
+
+/* Options specific to the manualroute router. */
+
+optionlist manualroute_router_options[] = {
+  { "host_all_ignored", opt_stringptr,
+      (void *)(offsetof(manualroute_router_options_block, host_all_ignored)) },
+  { "host_find_failed", opt_stringptr,
+      (void *)(offsetof(manualroute_router_options_block, host_find_failed)) },
+  { "hosts_randomize",  opt_bool,
+      (void *)(offsetof(manualroute_router_options_block, hosts_randomize)) },
+  { "route_data",       opt_stringptr,
+      (void *)(offsetof(manualroute_router_options_block, route_data)) },
+  { "route_list",       opt_stringptr,
+      (void *)(offsetof(manualroute_router_options_block, route_list)) },
+  { "same_domain_copy_routing", opt_bool|opt_public,
+      (void *)(offsetof(router_instance, same_domain_copy_routing)) }
+};
+
+/* Size of the options list. An extern variable has to be used so that its
+address can appear in the tables drtables.c. */
+
+int manualroute_router_options_count =
+  sizeof(manualroute_router_options)/sizeof(optionlist);
+
+/* Default private options block for the manualroute router. */
+
+manualroute_router_options_block manualroute_router_option_defaults = {
+  -1,           /* host_all_ignored code (unset) */
+  -1,           /* host_find_failed code (unset) */
+  FALSE,        /* hosts_randomize */
+  US"defer",    /* host_all_ignored */
+  US"freeze",   /* host_find_failed */
+  NULL,         /* route_data */
+  NULL          /* route_list */
+};
+
+
+/* Names and values for host_find_failed and host_all_ignored.  */
+
+static uschar *hff_names[] = {
+  US"ignore",  /* MUST be first - not valid for host_all_ignored */
+  US"decline",
+  US"defer",
+  US"fail",
+  US"freeze",
+  US"pass" };
+
+static int hff_codes[] = { hff_ignore, hff_decline, hff_defer, hff_fail,
+  hff_freeze, hff_pass };
+
+static int hff_count= sizeof(hff_codes)/sizeof(int);
+
+
+
+/*************************************************
+*          Initialization entry point            *
+*************************************************/
+
+/* Called for each instance, after its options have been read, to enable
+consistency checks to be done, or anything else that needs to be set up. */
+
+void
+manualroute_router_init(router_instance *rblock)
+{
+manualroute_router_options_block *ob =
+  (manualroute_router_options_block *)(rblock->options_block);
+int i;
+
+/* Host_find_failed must be a recognized word */
+
+for (i = 0; i < hff_count; i++)
+  {
+  if (Ustrcmp(ob->host_find_failed, hff_names[i]) == 0)
+    {
+    ob->hff_code = hff_codes[i];
+    break;
+    }
+  }
+if (ob->hff_code < 0)
+  log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s router:\n  "
+    "unrecognized setting for host_find_failed option", rblock->name);
+
+for (i = 1; i < hff_count; i++)   /* NB starts at 1 to skip "ignore" */
+  {
+  if (Ustrcmp(ob->host_all_ignored, hff_names[i]) == 0)
+    {
+    ob->hai_code = hff_codes[i];
+    break;
+    }
+  }
+if (ob->hai_code < 0)
+  log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s router:\n  "
+    "unrecognized setting for host_all_ignored option", rblock->name);
+
+/* One of route_list or route_data must be specified */
+
+if ((ob->route_list == NULL && ob->route_data == NULL) ||
+    (ob->route_list != NULL && ob->route_data != NULL))
+  log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s router:\n  "
+    "route_list or route_data (but not both) must be specified",
+    rblock->name);
+}
+
+
+
+
+/*************************************************
+*               Parse a route item               *
+*************************************************/
+
+/* The format of a route list item is:
+
+  <domain> [<host[list]> [<options>]]
+
+if obtained from route_list. The domain is absent if the string came from
+route_data, in which case domain==NULL. The domain and the host list may be
+enclosed in quotes.
+
+Arguments:
+  s         pointer to route list item
+  domain    if not NULL, where to put the domain pointer
+  hostlist  where to put the host[list] pointer
+  options   where to put the options pointer
+
+Returns:    FALSE if domain expected and string is empty;
+            TRUE otherwise
+*/
+
+static BOOL
+parse_route_item(uschar *s, uschar **domain, uschar **hostlist,
+  uschar **options)
+{
+while (*s != 0 && isspace(*s)) s++;
+
+if (domain != NULL)
+  {
+  if (*s == 0) return FALSE;            /* missing data */
+  *domain = string_dequote(&s);
+  while (*s != 0 && isspace(*s)) s++;
+  }
+
+*hostlist = string_dequote(&s);
+while (*s != 0 && isspace(*s)) s++;
+*options = s;
+return TRUE;
+}
+
+
+
+/*************************************************
+*              Main entry point                  *
+*************************************************/
+
+/* The manualroute router provides a manual routing facility (surprise,
+surprise). The data that defines the routing can either be set in route_data
+(which means it can be found by, for example, looking up the domain in a file),
+or a list of domain patterns and their corresponding data can be provided in
+route_list. */
+
+/* See local README for interface details. This router returns:
+
+DECLINE
+  . no pattern in route_list matched (route_data not set)
+  . route_data was an empty string (route_list not set)
+  . forced expansion failure in route_data (rf_expand_data)
+  . forced expansion of host list
+  . host_find_failed = decline
+
+DEFER
+  . transport not defined when needed
+  . lookup defer in route_list when matching domain pattern
+  . non-forced expansion failure in route_data
+  . non-forced expansion failure in host list
+  . unknown routing option
+  . host list missing for remote transport (not verifying)
+  . timeout etc on host lookup (pass_on_timeout not set)
+  . verifying the errors address caused a deferment or a big disaster such
+      as an expansion failure (rf_get_errors_address)
+  . expanding a headers_{add,remove} string caused a deferment or another
+      expansion error (rf_get_munge_headers)
+  . a problem in rf_get_transport: no transport when one is needed;
+      failed to expand dynamic transport; failed to find dynamic transport
+  . failure to expand or find a uid/gid (rf_get_ugid via rf_queue_add)
+  . host_find_failed = freeze or defer
+  . self = freeze or defer
+
+PASS
+  . timeout etc on host lookup (pass_on_timeout set)
+  . host_find_failed = pass
+  . self = pass
+
+REROUTED
+  . self = reroute
+
+FAIL
+  . host_find_failed = fail
+  . self = fail
+
+OK
+  . added address to addr_local or addr_remote, as appropriate for the
+    type of transport; this includes the self="send" case.
+*/
+
+int
+manualroute_router_entry(
+  router_instance *rblock,        /* data for this instantiation */
+  address_item *addr,             /* address we are working on */
+  struct passwd *pw,              /* passwd entry after check_local_user */
+  int verify,                     /* v_none/v_recipient/v_sender/v_expn */
+  address_item **addr_local,      /* add it to this if it's local */
+  address_item **addr_remote,     /* add it to this if it's remote */
+  address_item **addr_new,        /* put new addresses on here */
+  address_item **addr_succeed)    /* put old address here on success */
+{
+int rc, lookup_type;
+uschar *route_item = NULL;
+uschar *options = NULL;
+uschar *hostlist = NULL;
+uschar *domain, *newhostlist, *listptr;
+manualroute_router_options_block *ob =
+  (manualroute_router_options_block *)(rblock->options_block);
+transport_instance *transport = NULL;
+BOOL individual_transport_set = FALSE;
+BOOL randomize = ob->hosts_randomize;
+
+addr_new = addr_new;           /* Keep picky compilers happy */
+addr_succeed = addr_succeed;
+
+DEBUG(D_route) debug_printf("%s router called for %s\n  domain = %s\n",
+  rblock->name, addr->address, addr->domain);
+
+/* The initialization check ensures that either route_list or route_data is
+set. */
+
+if (ob->route_list != NULL)
+  {
+  int sep = -(';');             /* Default is semicolon */
+  listptr = ob->route_list;
+
+  while ((route_item = string_nextinlist(&listptr, &sep, NULL, 0)) != NULL)
+    {
+    int rc;
+
+    DEBUG(D_route) debug_printf("route_item = %s\n", route_item);
+    if (!parse_route_item(route_item, &domain, &hostlist, &options))
+      continue;     /* Ignore blank items */
+
+    /* Check the current domain; if it matches, break the loop */
+
+    if ((rc = match_isinlist(addr->domain, &domain, UCHAR_MAX+1,
+           &domainlist_anchor, NULL, MCL_DOMAIN, TRUE, &lookup_value)) == OK)
+      break;
+
+    /* If there was a problem doing the check, defer */
+
+    if (rc == DEFER)
+      {
+      addr->message = US"lookup defer in route_list";
+      return DEFER;
+      }
+    }
+
+  if (route_item == NULL) return DECLINE;  /* No pattern in the list matched */
+  }
+
+/* Handle a single routing item in route_data. If it expands to an empty
+string, decline. */
+
+else
+  {
+  route_item = rf_expand_data(addr, ob->route_data, &rc);
+  if (route_item == NULL) return rc;
+  (void) parse_route_item(route_item, NULL, &hostlist, &options);
+  if (hostlist[0] == 0) return DECLINE;
+  }
+
+/* Expand the hostlist item. It may then pointing to an empty string, or to a
+single host or a list of hosts; options is pointing to the rest of the
+routelist item, which is either empty or contains various option words. */
+
+DEBUG(D_route) debug_printf("original list of hosts = \"%s\" options = %s\n",
+  hostlist, options);
+
+newhostlist = expand_string_copy(hostlist);
+lookup_value = NULL;                        /* Finished with */
+expand_nmax = -1;
+
+/* If the expansion was forced to fail, just decline. Otherwise there is a
+configuration problem. */
+
+if (newhostlist == NULL)
+  {
+  if (expand_string_forcedfail) return DECLINE;
+  addr->message = string_sprintf("%s router: failed to expand \"%s\": %s",
+    rblock->name, hostlist, expand_string_message);
+  return DEFER;
+  }
+else hostlist = newhostlist;
+
+DEBUG(D_route) debug_printf("expanded list of hosts = \"%s\" options = %s\n",
+  hostlist, options);
+
+/* Set default lookup type and scan the options */
+
+lookup_type = lk_default;
+
+while (*options != 0)
+  {
+  int term;
+  uschar *s = options;
+  while (*options != 0 && !isspace(*options)) options++;
+  term = *options;
+  *options = 0;
+
+  if (Ustrcmp(s, "randomize") == 0) randomize = TRUE;
+  else if (Ustrcmp(s, "no_randomize") == 0) randomize = FALSE;
+  else if (Ustrcmp(s, "byname") == 0) lookup_type = lk_byname;
+  else if (Ustrcmp(s, "bydns") == 0) lookup_type = lk_bydns;
+  else
+    {
+    transport_instance *t;
+    for (t = transports; t != NULL; t = t->next)
+      {
+      if (Ustrcmp(t->name, s) == 0)
+        {
+        transport = t;
+        individual_transport_set = TRUE;
+        break;
+        }
+      }
+    if (t == NULL)
+      {
+      s = string_sprintf("unknown routing option or transport name \"%s\"", s);
+      log_write(0, LOG_MAIN, "Error in %s router: %s", rblock->name, s);
+      addr->message = string_sprintf("error in router: %s", s);
+      return DEFER;
+      }
+    }
+
+  if (term != 0)
+    {
+    options++;
+    while (*options != 0 && isspace(*options)) options++;
+    }
+  }
+
+/* Set up the errors address, if any. */
+
+rc = rf_get_errors_address(addr, rblock, verify, &(addr->p.errors_address));
+if (rc != OK) return rc;
+
+/* Set up the additional and removeable headers for this address. */
+
+rc = rf_get_munge_headers(addr, rblock, &(addr->p.extra_headers),
+  &(addr->p.remove_headers));
+if (rc != OK) return rc;
+
+/* If an individual transport is not set, get the transport for this router, if
+any. It might be expanded, or it might be unset if this router has verify_only
+set. */
+
+if (!individual_transport_set)
+  {
+  if (!rf_get_transport(rblock->transport_name, &(rblock->transport), addr,
+      rblock->name, NULL))
+    return DEFER;
+  transport = rblock->transport;
+  }
+
+/* Deal with the case of a local transport. The host list is passed over as a
+single text string that ends up in $host. */
+
+if (transport != NULL && transport->info->local)
+  {
+  if (hostlist[0] != 0)
+    {
+    host_item *h;
+    addr->host_list = h = store_get(sizeof(host_item));
+    h->name = string_copy(hostlist);
+    h->address = NULL;
+    h->port = PORT_NONE;
+    h->mx = MX_NONE;
+    h->status = hstatus_unknown;
+    h->why = hwhy_unknown;
+    h->last_try = 0;
+    h->next = NULL;
+    }
+
+  /* There is nothing more to do other than to queue the address for the
+  local transport, filling in any uid/gid. This can be done by the common
+  rf_queue_add() function. */
+
+  addr->transport = transport;
+  return rf_queue_add(addr, addr_local, addr_remote, rblock, pw)?
+    OK : DEFER;
+  }
+
+/* There is either no transport (verify_only) or a remote transport. A host
+list is mandatory in either case, except when verifying, in which case the
+address is just accepted. */
+
+if (hostlist[0] == 0)
+  {
+  if (verify != v_none) goto ROUTED;
+  addr->message = string_sprintf("error in %s router: no host(s) specified "
+    "for domain %s", rblock->name, domain);
+  log_write(0, LOG_MAIN, "%s", addr->message);
+  return DEFER;
+  }
+
+/* Otherwise we finish the routing here by building a chain of host items
+for the list of configured hosts, and then finding their addresses. */
+
+host_build_hostlist(&(addr->host_list), hostlist, randomize);
+rc = rf_lookup_hostlist(rblock, addr, rblock->ignore_target_hosts, lookup_type,
+  ob->hff_code, addr_new);
+if (rc != OK) return rc;
+
+/* If host_find_failed is set to "ignore", it is possible for all the hosts to
+be ignored, in which case we will end up with an empty host list. What happens
+is controlled by host_all_ignored. */
+
+if (addr->host_list == NULL)
+  {
+  int i;
+  DEBUG(D_route) debug_printf("host_find_failed ignored every host\n");
+  if (ob->hai_code == hff_decline) return DECLINE;
+  if (ob->hai_code == hff_pass) return PASS;
+
+  for (i = 0; i < hff_count; i++)
+    if (ob->hai_code == hff_codes[i]) break;
+
+  addr->message = string_sprintf("lookup failed for all hosts in %s router: "
+    "host_find_failed=ignore host_all_ignored=%s", rblock->name, hff_names[i]);
+
+  if (ob->hai_code == hff_defer) return DEFER;
+  if (ob->hai_code == hff_fail) return FAIL;
+
+  addr->special_action = SPECIAL_FREEZE;
+  return DEFER;
+  }
+
+/* Finally, since we have done all the routing here, there must be a transport
+defined for these hosts. It will be a remote one, as a local transport is
+dealt with above. However, we don't need one if verifying only. */
+
+if (transport == NULL && verify == v_none)
+    {
+    log_write(0, LOG_MAIN, "Error in %s router: no transport defined",
+      rblock->name);
+    addr->message = US"error in router: transport missing";
+    return DEFER;
+    }
+
+/* Fill in the transport, queue for remote delivery. The yield of
+rf_queue_add() is always TRUE for a remote transport. */
+
+ROUTED:
+
+addr->transport = transport;
+(void)rf_queue_add(addr, addr_local, addr_remote, rblock, NULL);
+return OK;
+}
+
+/* End of routers/manualroute.c */
diff --git a/src/routers/manualroute.h b/src/routers/manualroute.h
new file mode 100644 (file)
index 0000000..9c20b6f
--- /dev/null
@@ -0,0 +1,39 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Header for the manualroute router */
+
+/* Structure for the private options. */
+
+typedef struct {
+  int   hai_code;
+  int   hff_code;
+  BOOL  hosts_randomize;
+  uschar *host_all_ignored;
+  uschar *host_find_failed;
+  uschar *route_data;
+  uschar *route_list;
+} manualroute_router_options_block;
+
+/* Data for reading the private options. */
+
+extern optionlist manualroute_router_options[];
+extern int manualroute_router_options_count;
+
+/* Block containing default values. */
+
+extern manualroute_router_options_block manualroute_router_option_defaults;
+
+/* The main and initialization entry points for the router */
+
+extern int manualroute_router_entry(router_instance *, address_item *,
+  struct passwd *, int, address_item **, address_item **,
+  address_item **, address_item **);
+
+extern void manualroute_router_init(router_instance *);
+
+/* End of routers/manualroute.h */
diff --git a/src/routers/queryprogram.c b/src/routers/queryprogram.c
new file mode 100644 (file)
index 0000000..11e1fdc
--- /dev/null
@@ -0,0 +1,537 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+#include "../exim.h"
+#include "rf_functions.h"
+#include "queryprogram.h"
+
+
+
+/* Options specific to the queryprogram router. */
+
+optionlist queryprogram_router_options[] = {
+  { "*expand_command_group", opt_bool | opt_hidden,
+      (void *)(offsetof(queryprogram_router_options_block, expand_cmd_gid)) },
+  { "*expand_command_user", opt_bool | opt_hidden,
+      (void *)(offsetof(queryprogram_router_options_block, expand_cmd_uid)) },
+  { "*set_command_group",   opt_bool | opt_hidden,
+      (void *)(offsetof(queryprogram_router_options_block, cmd_gid_set)) },
+  { "*set_command_user",    opt_bool | opt_hidden,
+      (void *)(offsetof(queryprogram_router_options_block, cmd_uid_set)) },
+  { "command",      opt_stringptr,
+      (void *)(offsetof(queryprogram_router_options_block, command)) },
+  { "command_group",opt_expand_gid,
+      (void *)(offsetof(queryprogram_router_options_block, cmd_gid)) },
+  { "command_user", opt_expand_uid,
+      (void *)(offsetof(queryprogram_router_options_block, cmd_uid)) },
+  { "current_directory", opt_stringptr,
+      (void *)(offsetof(queryprogram_router_options_block, current_directory)) },
+  { "timeout",      opt_time,
+      (void *)(offsetof(queryprogram_router_options_block, timeout)) }
+};
+
+/* Size of the options list. An extern variable has to be used so that its
+address can appear in the tables drtables.c. */
+
+int queryprogram_router_options_count =
+  sizeof(queryprogram_router_options)/sizeof(optionlist);
+
+/* Default private options block for the queryprogram router. */
+
+queryprogram_router_options_block queryprogram_router_option_defaults = {
+  NULL,         /* command */
+  60*60,        /* timeout */
+  (uid_t)(-1),  /* cmd_uid */
+  (gid_t)(-1),  /* cmd_gid */
+  FALSE,        /* cmd_uid_set */
+  FALSE,        /* cmd_gid_set */
+  US"/",        /* current_directory */
+  NULL,         /* expand_cmd_gid */
+  NULL          /* expand_cmd_uid */
+};
+
+
+
+/*************************************************
+*          Initialization entry point            *
+*************************************************/
+
+/* Called for each instance, after its options have been read, to enable
+consistency checks to be done, or anything else that needs to be set up. */
+
+void
+queryprogram_router_init(router_instance *rblock)
+{
+queryprogram_router_options_block *ob =
+  (queryprogram_router_options_block *)(rblock->options_block);
+
+/* A command must be given */
+
+if (ob->command == NULL)
+  log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s router:\n  "
+    "a command specification is required", rblock->name);
+
+/* A uid/gid must be supplied */
+
+if (!ob->cmd_uid_set && ob->expand_cmd_uid == NULL)
+  log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s router:\n  "
+    "command_user must be specified", rblock->name);
+}
+
+
+
+/*************************************************
+*    Process a set of generated new addresses    *
+*************************************************/
+
+/* This function sets up a set of newly generated child addresses and puts them
+on the new address chain.
+
+Arguments:
+  rblock                  router block
+  addr_new                new address chain
+  addr                    original address
+  generated               list of generated addresses
+  addr_prop               the propagated data block, containing errors_to,
+                            header change stuff, and address_data
+
+Returns:         nothing
+*/
+
+static void
+add_generated(router_instance *rblock, address_item **addr_new,
+  address_item *addr, address_item *generated,
+  address_item_propagated *addr_prop)
+{
+while (generated != NULL)
+  {
+  address_item *next = generated;
+  generated = next->next;
+
+  next->parent = addr;
+  orflag(next, addr, af_propagate);
+  next->p = *addr_prop;
+  next->start_router = rblock->redirect_router;
+
+  next->next = *addr_new;
+  *addr_new = next;
+
+  if (addr->child_count == SHRT_MAX)
+    log_write(0, LOG_MAIN|LOG_PANIC_DIE, "%s router generated more than %d "
+      "child addresses for <%s>", rblock->name, SHRT_MAX, addr->address);
+  addr->child_count++;
+
+  DEBUG(D_route)
+    debug_printf("%s router generated %s\n", rblock->name, next->address);
+  }
+}
+
+
+
+
+/*************************************************
+*              Main entry point                  *
+*************************************************/
+
+/* See local README for interface details. This router returns:
+
+DECLINE
+  . DECLINE returned
+  . self = DECLINE
+
+PASS
+  . PASS returned
+  . timeout of host lookup and pass_on_timeout set
+  . self = PASS
+
+DEFER
+  . verifying the errors address caused a deferment or a big disaster such
+      as an expansion failure (rf_get_errors_address)
+  . expanding a headers_{add,remove} string caused a deferment or another
+      expansion error (rf_get_munge_headers)
+  . a problem in rf_get_transport: no transport when one is needed;
+      failed to expand dynamic transport; failed to find dynamic transport
+  . bad lookup type
+  . problem looking up host (rf_lookup_hostlist)
+  . self = DEFER or FREEZE
+  . failure to set up uid/gid for running the command
+  . failure of transport_set_up_command: too many arguments, expansion fail
+  . failure to create child process
+  . child process crashed or timed out or didn't return data
+  . :defer: in data
+  . DEFER or FREEZE returned
+  . problem in redirection data
+  . unknown transport name or trouble expanding router transport
+
+FAIL
+  . :fail: in data
+  . FAIL returned
+  . self = FAIL
+
+OK
+  . address added to addr_local or addr_remote for delivery
+  . new addresses added to addr_new
+*/
+
+int
+queryprogram_router_entry(
+  router_instance *rblock,        /* data for this instantiation */
+  address_item *addr,             /* address we are working on */
+  struct passwd *pw,              /* passwd entry after check_local_user */
+  int verify,                     /* v_none/v_recipient/v_sender/v_expn */
+  address_item **addr_local,      /* add it to this if it's local */
+  address_item **addr_remote,     /* add it to this if it's remote */
+  address_item **addr_new,        /* put new addresses on here */
+  address_item **addr_succeed)    /* put old address here on success */
+{
+int fd_in, fd_out, len, rc;
+pid_t pid;
+struct passwd *upw = NULL;
+uschar buffer[1024];
+uschar **argvptr;
+uschar *rword, *rdata, *s;
+address_item_propagated addr_prop;
+queryprogram_router_options_block *ob =
+  (queryprogram_router_options_block *)(rblock->options_block);
+uschar *current_directory = ob->current_directory;
+ugid_block ugid;
+uid_t curr_uid = getuid();
+gid_t curr_gid = getgid();
+uid_t uid = ob->cmd_uid;
+gid_t gid = ob->cmd_gid;
+uid_t *puid = &uid;
+gid_t *pgid = &gid;
+
+DEBUG(D_route) debug_printf("%s router called for %s: domain = %s\n",
+  rblock->name, addr->address, addr->domain);
+
+ugid.uid_set = ugid.gid_set = FALSE;
+
+/* Set up the propagated data block with the current address_data and the
+errors address and extra header stuff. */
+
+addr_prop.address_data = deliver_address_data;
+
+rc = rf_get_errors_address(addr, rblock, verify, &(addr_prop.errors_address));
+if (rc != OK) return rc;
+
+rc = rf_get_munge_headers(addr, rblock, &(addr_prop.extra_headers),
+  &(addr_prop.remove_headers));
+if (rc != OK) return rc;
+
+/* Get the fixed or expanded uid under which the command is to run
+(initialization ensures that one or the other is set). */
+
+if (!ob->cmd_uid_set)
+  {
+  if (!route_find_expanded_user(ob->expand_cmd_uid, rblock->name, US"router",
+      &upw, &uid, &(addr->message)))
+    return DEFER;
+  }
+
+/* Get the fixed or expanded gid, or take the gid from the passwd entry. */
+
+if (!ob->cmd_gid_set)
+  {
+  if (ob->expand_cmd_gid != NULL)
+    {
+    if (route_find_expanded_group(ob->expand_cmd_gid, rblock->name,
+        US"router", &gid, &(addr->message)))
+      return DEFER;
+    }
+  else if (upw != NULL)
+    {
+    gid = upw->pw_gid;
+    }
+  else
+    {
+    addr->message = string_sprintf("command_user set without command_group "
+      "for %s router", rblock->name);
+    return DEFER;
+    }
+  }
+
+DEBUG(D_route) debug_printf("requires uid=%ld gid=%ld current_directory=%s\n",
+  (long int)uid, (long int)gid, current_directory);
+
+/* If we are not running as root, we will not be able to change uid/gid. */
+
+if (curr_uid != root_uid && (uid != curr_uid || gid != curr_gid))
+  {
+  DEBUG(D_route)
+    {
+    debug_printf("not running as root: cannot change uid/gid\n");
+    debug_printf("subprocess will run with uid=%ld gid=%ld\n",
+      (long int)curr_uid, (long int)curr_gid);
+    }
+  puid = pgid = NULL;
+  }
+
+/* Set up the command to run */
+
+if (!transport_set_up_command(&argvptr, /* anchor for arg list */
+    ob->command,                        /* raw command */
+    TRUE,                               /* expand the arguments */
+    0,                                  /* not relevant when... */
+    NULL,                               /* no transporting address */
+    US"queryprogram router",            /* for error messages */
+    &(addr->message)))                  /* where to put error message */
+  {
+  return DEFER;
+  }
+
+/* Create the child process, making it a group leader. */
+
+pid = child_open_uid(argvptr, NULL, 0077, puid, pgid, &fd_in, &fd_out,
+  current_directory, TRUE);
+
+if (pid < 0)
+  {
+  addr->message = string_sprintf("%s router couldn't create child process: %s",
+    rblock->name, strerror(errno));
+  return DEFER;
+  }
+
+/* Nothing is written to the standard input. */
+
+(void)close(fd_in);
+
+/* Wait for the process to finish, applying the timeout, and inspect its return
+code. */
+
+if ((rc = child_close(pid, ob->timeout)) != 0)
+  {
+  if (rc > 0)
+    addr->message = string_sprintf("%s router: command returned non-zero "
+      "code %d", rblock->name, rc);
+
+  else if (rc == -256)
+    {
+    addr->message = string_sprintf("%s router: command timed out",
+      rblock->name);
+    killpg(pid, SIGKILL);       /* Kill the whole process group */
+    }
+
+  else if (rc == -257)
+    addr->message = string_sprintf("%s router: wait() failed: %s",
+      rblock->name, strerror(errno));
+
+  else
+    addr->message = string_sprintf("%s router: command killed by signal %d",
+      rblock->name, -rc);
+
+  return DEFER;
+  }
+
+/* Read the pipe to get the command's output, and then close it. */
+
+len = read(fd_out, buffer, sizeof(buffer) - 1);
+(void)close(fd_out);
+
+/* Failure to return any data is an error. */
+
+if (len <= 0)
+  {
+  addr->message = string_sprintf("%s router: command failed to return data",
+    rblock->name);
+  return DEFER;
+  }
+
+/* Get rid of leading and trailing white space, and pick off the first word of
+the result. */
+
+while (len > 0 && isspace(buffer[len-1])) len--;
+buffer[len] = 0;
+
+DEBUG(D_route) debug_printf("command wrote: %s\n", buffer);
+
+rword = buffer;
+while (isspace(*rword)) rword++;
+rdata = rword;
+while (*rdata != 0 && !isspace(*rdata)) rdata++;
+if (*rdata != 0) *rdata++ = 0;
+
+/* The word must be a known yield name. If it is "REDIRECT", the rest of the
+line is redirection data, as for a .forward file. It may not contain filter
+data, and it may not contain anything other than addresses (no files, no pipes,
+no specials). */
+
+if (strcmpic(rword, US"REDIRECT") == 0)
+  {
+  int filtertype;
+  redirect_block redirect;
+  address_item *generated = NULL;
+
+  redirect.string = rdata;
+  redirect.isfile = FALSE;
+
+  rc = rda_interpret(&redirect,  /* redirection data */
+    RDO_BLACKHOLE |              /* forbid :blackhole: */
+      RDO_FAIL    |              /* forbid :fail: */
+      RDO_INCLUDE |              /* forbid :include: */
+      RDO_REWRITE,               /* rewrite generated addresses */
+    NULL,                        /* :include: directory not relevant */
+    NULL,                        /* sieve vacation directory not relevant */
+    NULL,                        /* sieve enotify mailto owner not relevant */
+    NULL,                        /* sieve useraddress not relevant */
+    NULL,                        /* sieve subaddress not relevant */
+    &ugid,                       /* uid/gid (but not set) */
+    &generated,                  /* where to hang the results */
+    &(addr->message),            /* where to put messages */
+    NULL,                        /* don't skip syntax errors */
+    &filtertype,                 /* not used; will always be FILTER_FORWARD */
+    string_sprintf("%s router", rblock->name));
+
+  switch (rc)
+    {
+    /* FF_DEFER and FF_FAIL can arise only as a result of explicit commands.
+    If a configured message was supplied, allow it to be  included in an SMTP
+    response after verifying. */
+
+    case FF_DEFER:
+    if (addr->message == NULL) addr->message = US"forced defer";
+      else addr->user_message = addr->message;
+    return DEFER;
+
+    case FF_FAIL:
+    add_generated(rblock, addr_new, addr, generated, &addr_prop);
+    if (addr->message == NULL) addr->message = US"forced rejection";
+      else addr->user_message = addr->message;
+    return FAIL;
+
+    case FF_DELIVERED:
+    break;
+
+    case FF_NOTDELIVERED:    /* an empty redirection list is bad */
+    addr->message = US"no addresses supplied";
+    /* Fall through */
+
+    case FF_ERROR:
+    default:
+    addr->basic_errno = ERRNO_BADREDIRECT;
+    addr->message = string_sprintf("error in redirect data: %s", addr->message);
+    return DEFER;
+    }
+
+  /* Handle the generated addresses, if any. */
+
+  add_generated(rblock, addr_new, addr, generated, &addr_prop);
+
+  /* Put the original address onto the succeed queue so that any retry items
+  that get attached to it get processed. */
+
+  addr->next = *addr_succeed;
+  *addr_succeed = addr;
+
+  return OK;
+  }
+
+/* Handle other returns that are not ACCEPT */
+
+if (strcmpic(rword, US"accept") != 0)
+  {
+  if (strcmpic(rword, US"decline") == 0) return DECLINE;
+  if (strcmpic(rword, US"pass") == 0) return PASS;
+  addr->message = string_copy(rdata);                /* data is a message */
+  if (strcmpic(rword, US"fail") == 0)
+    {
+    setflag(addr, af_pass_message);
+    return FAIL;
+    }
+  if (strcmpic(rword, US"freeze") == 0) addr->special_action = SPECIAL_FREEZE;
+  else if (strcmpic(rword, US"defer") != 0)
+    {
+    addr->message = string_sprintf("bad command yield: %s %s", rword, rdata);
+    log_write(0, LOG_PANIC, "%s router: %s", rblock->name, addr->message);
+    }
+  return DEFER;
+  }
+
+/* The command yielded "ACCEPT". The rest of the string is a number of keyed
+fields from which we can fish out values using the "extract" expansion
+function. To use this feature, we must put the string into the $value variable,
+i.e. set lookup_value. */
+
+lookup_value = rdata;
+s = expand_string(US"${extract{data}{$value}}");
+if (*s != 0) addr_prop.address_data = string_copy(s);
+
+s = expand_string(US"${extract{transport}{$value}}");
+lookup_value = NULL;
+
+/* If we found a transport name, find the actual transport */
+
+if (*s != 0)
+  {
+  transport_instance *transport;
+  for (transport = transports; transport != NULL; transport = transport->next)
+    if (Ustrcmp(transport->name, s) == 0) break;
+  if (transport == NULL)
+    {
+    addr->message = string_sprintf("unknown transport name %s yielded by "
+      "command", s);
+    log_write(0, LOG_PANIC, "%s router: %s", rblock->name, addr->message);
+    return DEFER;
+    }
+  addr->transport = transport;
+  }
+
+/* No transport given; get the transport from the router configuration. It may
+be fixed or expanded, but there will be an error if it is unset, requested by
+the last argument not being NULL. */
+
+else
+  {
+  if (!rf_get_transport(rblock->transport_name, &(rblock->transport), addr,
+       rblock->name, US"transport"))
+    return DEFER;
+  addr->transport = rblock->transport;
+  }
+
+/* See if a host list is given, and if so, look up the addresses. */
+
+lookup_value = rdata;
+s = expand_string(US"${extract{hosts}{$value}}");
+
+if (*s != 0)
+  {
+  int lookup_type = lk_default;
+  uschar *ss = expand_string(US"${extract{lookup}{$value}}");
+  lookup_value = NULL;
+
+  if (*ss != 0)
+    {
+    if (Ustrcmp(ss, "byname") == 0) lookup_type = lk_byname;
+    else if (Ustrcmp(ss, "bydns") == 0) lookup_type = lk_bydns;
+    else
+      {
+      addr->message = string_sprintf("bad lookup type \"%s\" yielded by "
+        "command", ss);
+      log_write(0, LOG_PANIC, "%s router: %s", rblock->name, addr->message);
+      return DEFER;
+      }
+    }
+
+  host_build_hostlist(&(addr->host_list), s, FALSE);  /* pro tem no randomize */
+
+  rc = rf_lookup_hostlist(rblock, addr, rblock->ignore_target_hosts,
+    lookup_type, hff_defer, addr_new);
+  if (rc != OK) return rc;
+  }
+lookup_value = NULL;
+
+/* Put the errors address, extra headers, and address_data into this address */
+
+addr->p = addr_prop;
+
+/* Queue the address for local or remote delivery. */
+
+return rf_queue_add(addr, addr_local, addr_remote, rblock, pw)?
+  OK : DEFER;
+}
+
+/* End of routers/queryprogram.c */
diff --git a/src/routers/queryprogram.h b/src/routers/queryprogram.h
new file mode 100644 (file)
index 0000000..93046bd
--- /dev/null
@@ -0,0 +1,40 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+
+/* Private structure for the private options. */
+
+typedef struct {
+  uschar *command;
+  int timeout;
+  uid_t cmd_uid;
+  gid_t cmd_gid;
+  BOOL cmd_uid_set;
+  BOOL cmd_gid_set;
+  uschar *current_directory;
+  uschar *expand_cmd_gid;
+  uschar *expand_cmd_uid;
+} queryprogram_router_options_block;
+
+/* Data for reading the private options. */
+
+extern optionlist queryprogram_router_options[];
+extern int queryprogram_router_options_count;
+
+/* Block containing default values. */
+
+extern queryprogram_router_options_block queryprogram_router_option_defaults;
+
+/* The main and initialization entry points for the router */
+
+extern int queryprogram_router_entry(router_instance *, address_item *,
+  struct passwd *, int, address_item **, address_item **,
+  address_item **, address_item **);
+
+extern void queryprogram_router_init(router_instance *);
+
+/* End of routers/queryprogram.h */
diff --git a/src/routers/redirect.c b/src/routers/redirect.c
new file mode 100644 (file)
index 0000000..c6705e5
--- /dev/null
@@ -0,0 +1,914 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+
+#include "../exim.h"
+#include "rf_functions.h"
+#include "redirect.h"
+
+
+
+/* Options specific to the redirect router. */
+
+optionlist redirect_router_options[] = {
+  { "allow_defer",        opt_bit | (RDON_DEFER << 16),
+      (void *)offsetof(redirect_router_options_block, bit_options) },
+  { "allow_fail",         opt_bit | (RDON_FAIL << 16),
+      (void *)offsetof(redirect_router_options_block, bit_options) },
+  { "allow_filter",       opt_bit | (RDON_FILTER << 16),
+      (void *)offsetof(redirect_router_options_block, bit_options) },
+  { "allow_freeze",       opt_bit | (RDON_FREEZE << 16),
+      (void *)offsetof(redirect_router_options_block, bit_options) },
+  { "check_ancestor",     opt_bool,
+      (void *)offsetof(redirect_router_options_block, check_ancestor) },
+  { "check_group",        opt_bool,
+      (void *)offsetof(redirect_router_options_block, check_group) },
+  { "check_owner",        opt_bool,
+      (void *)offsetof(redirect_router_options_block, check_owner) },
+  { "data",               opt_stringptr,
+      (void *)offsetof(redirect_router_options_block, data) },
+  { "directory_transport",opt_stringptr,
+      (void *)offsetof(redirect_router_options_block, directory_transport_name) },
+  { "file",               opt_stringptr,
+      (void *)offsetof(redirect_router_options_block, file) },
+  { "file_transport",     opt_stringptr,
+      (void *)offsetof(redirect_router_options_block, file_transport_name) },
+  { "filter_prepend_home",opt_bit | (RDON_PREPEND_HOME << 16),
+      (void *)offsetof(redirect_router_options_block, bit_options) },
+  { "forbid_blackhole",   opt_bit | (RDON_BLACKHOLE << 16),
+      (void *)offsetof(redirect_router_options_block, bit_options) },
+  { "forbid_exim_filter", opt_bit | (RDON_EXIM_FILTER << 16),
+      (void *)offsetof(redirect_router_options_block, bit_options) },
+  { "forbid_file",        opt_bool,
+      (void *)offsetof(redirect_router_options_block, forbid_file) },
+  { "forbid_filter_dlfunc", opt_bit | (RDON_DLFUNC << 16),
+      (void *)offsetof(redirect_router_options_block, bit_options) },
+  { "forbid_filter_existstest",  opt_bit | (RDON_EXISTS << 16),
+      (void *)offsetof(redirect_router_options_block, bit_options) },
+  { "forbid_filter_logwrite",opt_bit | (RDON_LOG << 16),
+      (void *)offsetof(redirect_router_options_block, bit_options) },
+  { "forbid_filter_lookup", opt_bit | (RDON_LOOKUP << 16),
+      (void *)offsetof(redirect_router_options_block, bit_options) },
+  { "forbid_filter_perl", opt_bit | (RDON_PERL << 16),
+      (void *)offsetof(redirect_router_options_block, bit_options) },
+  { "forbid_filter_readfile", opt_bit | (RDON_READFILE << 16),
+      (void *)offsetof(redirect_router_options_block, bit_options) },
+  { "forbid_filter_readsocket", opt_bit | (RDON_READSOCK << 16),
+      (void *)offsetof(redirect_router_options_block, bit_options) },
+  { "forbid_filter_reply",opt_bool,
+      (void *)offsetof(redirect_router_options_block, forbid_filter_reply) },
+  { "forbid_filter_run",  opt_bit | (RDON_RUN << 16),
+      (void *)offsetof(redirect_router_options_block, bit_options) },
+  { "forbid_include",     opt_bit | (RDON_INCLUDE << 16),
+      (void *)offsetof(redirect_router_options_block, bit_options) },
+  { "forbid_pipe",        opt_bool,
+      (void *)offsetof(redirect_router_options_block, forbid_pipe) },
+  { "forbid_sieve_filter",opt_bit | (RDON_SIEVE_FILTER << 16),
+      (void *)offsetof(redirect_router_options_block, bit_options) },
+  { "forbid_smtp_code",     opt_bool,
+      (void *)offsetof(redirect_router_options_block, forbid_smtp_code) },
+  { "hide_child_in_errmsg", opt_bool,
+      (void *)offsetof(redirect_router_options_block,  hide_child_in_errmsg) },
+  { "ignore_eacces",      opt_bit | (RDON_EACCES << 16),
+      (void *)offsetof(redirect_router_options_block, bit_options) },
+  { "ignore_enotdir",     opt_bit | (RDON_ENOTDIR << 16),
+      (void *)offsetof(redirect_router_options_block, bit_options) },
+  { "include_directory",  opt_stringptr,
+      (void *)offsetof(redirect_router_options_block,  include_directory) },
+  { "modemask",           opt_octint,
+      (void *)offsetof(redirect_router_options_block, modemask) },
+  { "one_time",           opt_bool,
+      (void *)offsetof(redirect_router_options_block, one_time) },
+  { "owners",             opt_uidlist,
+      (void *)offsetof(redirect_router_options_block, owners) },
+  { "owngroups",          opt_gidlist,
+      (void *)offsetof(redirect_router_options_block, owngroups) },
+  { "pipe_transport",     opt_stringptr,
+      (void *)offsetof(redirect_router_options_block, pipe_transport_name) },
+  { "qualify_domain",     opt_stringptr,
+      (void *)offsetof(redirect_router_options_block, qualify_domain) },
+  { "qualify_preserve_domain", opt_bool,
+      (void *)offsetof(redirect_router_options_block, qualify_preserve_domain) },
+  { "repeat_use",         opt_bool | opt_public,
+      (void *)offsetof(router_instance, repeat_use) },
+  { "reply_transport",    opt_stringptr,
+      (void *)offsetof(redirect_router_options_block, reply_transport_name) },
+  { "rewrite",            opt_bit | (RDON_REWRITE << 16),
+      (void *)offsetof(redirect_router_options_block, bit_options) },
+  { "sieve_enotify_mailto_owner", opt_stringptr,
+      (void *)offsetof(redirect_router_options_block, sieve_enotify_mailto_owner) },
+  { "sieve_subaddress", opt_stringptr,
+      (void *)offsetof(redirect_router_options_block, sieve_subaddress) },
+  { "sieve_useraddress", opt_stringptr,
+      (void *)offsetof(redirect_router_options_block, sieve_useraddress) },
+  { "sieve_vacation_directory", opt_stringptr,
+      (void *)offsetof(redirect_router_options_block, sieve_vacation_directory) },
+  { "skip_syntax_errors", opt_bool,
+      (void *)offsetof(redirect_router_options_block, skip_syntax_errors) },
+#ifdef EXPERIMENTAL_SRS
+  { "srs",                opt_stringptr,
+      (void *)offsetof(redirect_router_options_block, srs) },
+  { "srs_alias",          opt_stringptr,
+      (void *)offsetof(redirect_router_options_block, srs_alias) },
+  { "srs_condition",      opt_stringptr,
+      (void *)offsetof(redirect_router_options_block, srs_condition) },
+  { "srs_dbinsert",       opt_stringptr,
+      (void *)offsetof(redirect_router_options_block, srs_dbinsert) },
+  { "srs_dbselect",       opt_stringptr,
+      (void *)offsetof(redirect_router_options_block, srs_dbselect) },
+#endif
+  { "syntax_errors_text", opt_stringptr,
+      (void *)offsetof(redirect_router_options_block, syntax_errors_text) },
+  { "syntax_errors_to",   opt_stringptr,
+      (void *)offsetof(redirect_router_options_block, syntax_errors_to) }
+};
+
+/* Size of the options list. An extern variable has to be used so that its
+address can appear in the tables drtables.c. */
+
+int redirect_router_options_count =
+  sizeof(redirect_router_options)/sizeof(optionlist);
+
+/* Default private options block for the redirect router. */
+
+redirect_router_options_block redirect_router_option_defaults = {
+  NULL,        /* directory_transport */
+  NULL,        /* file_transport */
+  NULL,        /* pipe_transport */
+  NULL,        /* reply_transport */
+  NULL,        /* data */
+  NULL,        /* directory_transport_name */
+  NULL,        /* file */
+  NULL,        /* file_dir */
+  NULL,        /* file_transport_name */
+  NULL,        /* include_directory */
+  NULL,        /* pipe_transport_name */
+  NULL,        /* reply_transport_name */
+  NULL,        /* sieve_subaddress */
+  NULL,        /* sieve_useraddress */
+  NULL,        /* sieve_vacation_directory */
+  NULL,        /* sieve_enotify_mailto_owner */
+  NULL,        /* syntax_errors_text */
+  NULL,        /* syntax_errors_to */
+  NULL,        /* qualify_domain */
+  NULL,        /* owners */
+  NULL,        /* owngroups */
+#ifdef EXPERIMENTAL_SRS
+  NULL,        /* srs */
+  NULL,        /* srs_alias */
+  NULL,        /* srs_condition */
+  NULL,        /* srs_dbinsert */
+  NULL,        /* srs_dbselect */
+#endif
+  022,         /* modemask */
+  RDO_REWRITE | RDO_PREPEND_HOME, /* bit_options */
+  FALSE,       /* check_ancestor */
+  TRUE_UNSET,  /* check_owner */
+  TRUE_UNSET,  /* check_group */
+  FALSE,       /* forbid_file */
+  FALSE,       /* forbid_filter_reply */
+  FALSE,       /* forbid_pipe */
+  FALSE,       /* forbid_smtp_code */
+  FALSE,       /* hide_child_in_errmsg */
+  FALSE,       /* one_time */
+  FALSE,       /* qualify_preserve_domain */
+  FALSE        /* skip_syntax_errors */
+};
+
+
+
+/*************************************************
+*          Initialization entry point            *
+*************************************************/
+
+/* Called for each instance, after its options have been read, to enable
+consistency checks to be done, or anything else that needs to be set up. */
+
+void redirect_router_init(router_instance *rblock)
+{
+redirect_router_options_block *ob =
+  (redirect_router_options_block *)(rblock->options_block);
+
+/* Either file or data must be set, but not both */
+
+if ((ob->file == NULL) == (ob->data == NULL))
+  log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s router:\n  "
+    "%sone of \"file\" or \"data\" must be specified",
+    rblock->name, (ob->file == NULL)? "" : "only ");
+
+/* Onetime aliases can only be real addresses. Headers can't be manipulated.
+The combination of one_time and unseen is not allowed. We can't check the
+expansion of "unseen" here, but we assume that if it is set to anything other
+than false, there is likely to be a problem. */
+
+if (ob->one_time)
+  {
+  ob->forbid_pipe = ob->forbid_file = ob->forbid_filter_reply = TRUE;
+  if (rblock->extra_headers != NULL || rblock->remove_headers != NULL)
+    log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s router:\n  "
+      "\"headers_add\" and \"headers_remove\" are not permitted with "
+      "\"one_time\"", rblock->name);
+  if (rblock->unseen || rblock->expand_unseen != NULL)
+    log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s router:\n  "
+      "\"unseen\" may not be used with \"one_time\"", rblock->name);
+  }
+
+/* The defaults for check_owner and check_group depend on other settings. The
+defaults are: Check the owner if check_local_user or owners is set; check the
+group if check_local_user is set without a restriction on the group write bit,
+or if owngroups is set. */
+
+if (ob->check_owner == TRUE_UNSET)
+  ob->check_owner = rblock->check_local_user ||
+                    (ob->owners != NULL && ob->owners[0] != 0);
+
+if (ob->check_group == TRUE_UNSET)
+  ob->check_group = (rblock->check_local_user && (ob->modemask & 020) == 0) ||
+                    (ob->owngroups != NULL && ob->owngroups[0] != 0);
+
+/* If explicit qualify domain set, the preserve option is locked out */
+
+if (ob->qualify_domain != NULL && ob->qualify_preserve_domain)
+  log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s router:\n  "
+    "only one of \"qualify_domain\" or \"qualify_preserve_domain\" must be set",
+    rblock->name);
+
+/* If allow_filter is set, either user or check_local_user must be set. */
+
+if (!rblock->check_local_user &&
+    !rblock->uid_set &&
+    rblock->expand_uid == NULL &&
+    (ob->bit_options & RDO_FILTER) != 0)
+  log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s router:\n  "
+    "\"user\" or \"check_local_user\" must be set with \"allow_filter\"",
+    rblock->name);
+}
+
+
+
+/*************************************************
+*       Get errors address and header mods       *
+*************************************************/
+
+/* This function is called when new addresses are generated, in order to
+sort out errors address and header modifications. We put the errors address
+into the parent address (even though it is never used from there because that
+address is never transported) so that it can be retrieved if any of the
+children gets routed by an "unseen" router. The clone of the child that is
+passed on must have the original errors_address value.
+
+Arguments:
+  rblock               the router control block
+  addr                 the address being routed
+  verify               v_none/v_recipient/v_sender/v_expn
+  addr_prop            point to the propagated block, which is where the
+                         new values are to be placed
+
+Returns:    the result of rf_get_errors_address() or rf_get_munge_headers(),
+            which is either OK or DEFER
+*/
+
+static int
+sort_errors_and_headers(router_instance *rblock, address_item *addr,
+  int verify, address_item_propagated *addr_prop)
+{
+int frc = rf_get_errors_address(addr, rblock, verify,
+  &(addr_prop->errors_address));
+if (frc != OK) return frc;
+addr->p.errors_address = addr_prop->errors_address;
+return rf_get_munge_headers(addr, rblock, &(addr_prop->extra_headers),
+  &(addr_prop->remove_headers));
+}
+
+
+
+/*************************************************
+*    Process a set of generated new addresses    *
+*************************************************/
+
+/* This function sets up a set of newly generated child addresses and puts them
+on the new address chain. Copy in the uid, gid and permission flags for use by
+pipes and files, set the parent, and "or" its af_ignore_error flag. Also record
+the setting for any starting router.
+
+If the generated address is the same as one of its ancestors, and the
+check_ancestor flag is set, do not use this generated address, but replace it
+with a copy of the input address. This is to cope with cases where A is aliased
+to B and B has a .forward file pointing to A, though it is usually set on the
+forwardfile rather than the aliasfile. We can't just pass on the old
+address by returning FAIL, because it must act as a general parent for
+generated addresses, and only get marked "done" when all its children are
+delivered.
+
+Arguments:
+  rblock                  router block
+  addr_new                new address chain
+  addr                    original address
+  generated               list of generated addresses
+  addr_prop               the propagated block, containing the errors_address,
+                            header modification stuff, and address_data
+  ugidptr                 points to uid/gid data for files, pipes, autoreplies
+  pw                      password entry, set if ob->check_local_user is TRUE
+
+Returns:         nothing
+*/
+
+static void
+add_generated(router_instance *rblock, address_item **addr_new,
+  address_item *addr, address_item *generated,
+  address_item_propagated *addr_prop, ugid_block *ugidptr, struct passwd *pw)
+{
+redirect_router_options_block *ob =
+  (redirect_router_options_block *)(rblock->options_block);
+
+while (generated != NULL)
+  {
+  address_item *parent;
+  address_item *next = generated;
+  uschar *errors_address = next->p.errors_address;
+
+  generated = next->next;
+  next->parent = addr;
+  orflag(next, addr, af_ignore_error);
+  next->start_router = rblock->redirect_router;
+  if (addr->child_count == SHRT_MAX)
+    log_write(0, LOG_MAIN|LOG_PANIC_DIE, "%s router generated more than %d "
+      "child addresses for <%s>", rblock->name, SHRT_MAX, addr->address);
+  addr->child_count++;
+
+  next->next = *addr_new;
+  *addr_new = next;
+
+  /* Don't do the "one_time" thing for the first pass of a 2-stage queue run. */
+
+  if (ob->one_time && !queue_2stage)
+    {
+    for (parent = addr; parent->parent != NULL; parent = parent->parent);
+    next->onetime_parent = parent->address;
+    }
+
+  if (ob->hide_child_in_errmsg) setflag(next, af_hide_child);
+
+  /* If check_ancestor is set, we want to know if any ancestor of this address
+  is the address we are about to generate. The check must be done caselessly
+  unless the ancestor was routed by a case-sensitive router. */
+
+  if (ob->check_ancestor)
+    {
+    for (parent = addr; parent != NULL; parent = parent->parent)
+      {
+      if (((parent->router != NULL && parent->router->caseful_local_part)?
+           Ustrcmp(next->address, parent->address)
+           :
+           strcmpic(next->address, parent->address)
+          ) == 0)
+        {
+        DEBUG(D_route) debug_printf("generated parent replaced by child\n");
+        next->address = string_copy(addr->address);
+        break;
+        }
+      }
+    }
+
+  /* A user filter may, under some circumstances, set up an errors address.
+  If so, we must take care to re-instate it when we copy in the propagated
+  data so that it overrides any errors_to setting on the router. */
+
+  next->p = *addr_prop;
+  if (errors_address != NULL) next->p.errors_address = errors_address;
+
+  /* For pipes, files, and autoreplies, record this router as handling them,
+  because they don't go through the routing process again. Then set up uid,
+  gid, home and current directories for transporting. */
+
+  if (testflag(next, af_pfr))
+    {
+    next->router = rblock;
+    rf_set_ugid(next, ugidptr);   /* Will contain pw values if not overridden */
+
+    /* When getting the home directory out of the password information, wrap it
+    in \N...\N to avoid expansion later. In Cygwin, home directories can
+    contain $ characters. */
+
+    if (rblock->home_directory != NULL)
+      next->home_dir = rblock->home_directory;
+    else if (rblock->check_local_user)
+      next->home_dir = string_sprintf("\\N%s\\N", pw->pw_dir);
+    else if (rblock->router_home_directory != NULL &&
+             testflag(addr, af_home_expanded))
+      {
+      next->home_dir = deliver_home;
+      setflag(next, af_home_expanded);
+      }
+
+    next->current_dir = rblock->current_directory;
+
+    /* Permission options */
+
+    if (!ob->forbid_pipe) setflag(next, af_allow_pipe);
+    if (!ob->forbid_file) setflag(next, af_allow_file);
+    if (!ob->forbid_filter_reply) setflag(next, af_allow_reply);
+
+    /* If the transport setting fails, the error gets picked up at the outer
+    level from the setting of basic_errno in the address. */
+
+    if (next->address[0] == '|')
+      {
+      address_pipe = next->address;
+      if (rf_get_transport(ob->pipe_transport_name, &(ob->pipe_transport),
+          next, rblock->name, US"pipe_transport"))
+        next->transport = ob->pipe_transport;
+      address_pipe = NULL;
+      }
+    else if (next->address[0] == '>')
+      {
+      if (rf_get_transport(ob->reply_transport_name, &(ob->reply_transport),
+          next, rblock->name, US"reply_transport"))
+        next->transport = ob->reply_transport;
+      }
+    else  /* must be file or directory */
+      {
+      int len = Ustrlen(next->address);
+      address_file = next->address;
+      if (next->address[len-1] == '/')
+        {
+        if (rf_get_transport(ob->directory_transport_name,
+            &(ob->directory_transport), next, rblock->name,
+            US"directory_transport"))
+          next->transport = ob->directory_transport;
+        }
+      else
+        {
+        if (rf_get_transport(ob->file_transport_name, &(ob->file_transport),
+            next, rblock->name, US"file_transport"))
+          next->transport = ob->file_transport;
+        }
+      address_file = NULL;
+      }
+    }
+
+  DEBUG(D_route)
+    {
+    debug_printf("%s router generated %s\n  %serrors_to=%s transport=%s\n",
+      rblock->name,
+      next->address,
+      testflag(next, af_pfr)? "pipe, file, or autoreply\n  " : "",
+      next->p.errors_address,
+      (next->transport == NULL)? US"NULL" : next->transport->name);
+
+    if (testflag(next, af_uid_set))
+      debug_printf("  uid=%ld ", (long int)(next->uid));
+    else
+      debug_printf("  uid=unset ");
+
+    if (testflag(next, af_gid_set))
+      debug_printf("gid=%ld ", (long int)(next->gid));
+    else
+      debug_printf("gid=unset ");
+
+    debug_printf("home=%s\n", next->home_dir);
+    }
+  }
+}
+
+
+/*************************************************
+*              Main entry point                  *
+*************************************************/
+
+/* See local README for interface description. This router returns:
+
+DECLINE
+  . empty address list, or filter did nothing significant
+
+DEFER
+  . verifying the errors address caused a deferment or a big disaster such
+      as an expansion failure (rf_get_errors_address)
+  . expanding a headers_{add,remove} string caused a deferment or another
+      expansion error (rf_get_munge_headers)
+  . :defer: or "freeze" in a filter
+  . error in address list or filter
+  . skipped syntax errors, but failed to send the message
+
+DISCARD
+  . address was :blackhole:d or "seen finish"ed
+
+FAIL
+  . :fail:
+
+OK
+  . new addresses added to addr_new
+*/
+
+int redirect_router_entry(
+  router_instance *rblock,        /* data for this instantiation */
+  address_item *addr,             /* address we are working on */
+  struct passwd *pw,              /* passwd entry after check_local_user */
+  int verify,                     /* v_none/v_recipient/v_sender/v_expn */
+  address_item **addr_local,      /* add it to this if it's local */
+  address_item **addr_remote,     /* add it to this if it's remote */
+  address_item **addr_new,        /* put new addresses on here */
+  address_item **addr_succeed)    /* put old address here on success */
+{
+redirect_router_options_block *ob =
+  (redirect_router_options_block *)(rblock->options_block);
+address_item *generated = NULL;
+uschar *save_qualify_domain_recipient = qualify_domain_recipient;
+uschar *discarded = US"discarded";
+address_item_propagated addr_prop;
+error_block *eblock = NULL;
+ugid_block ugid;
+redirect_block redirect;
+int filtertype = FILTER_UNSET;
+int yield = OK;
+int options = ob->bit_options;
+int frc = 0;
+int xrc = 0;
+
+addr_local = addr_local;     /* Keep picky compilers happy */
+addr_remote = addr_remote;
+
+/* Initialize the data to be propagated to the children */
+
+addr_prop.address_data = deliver_address_data;
+addr_prop.domain_data = deliver_domain_data;
+addr_prop.localpart_data = deliver_localpart_data;
+addr_prop.errors_address = NULL;
+addr_prop.extra_headers = NULL;
+addr_prop.remove_headers = NULL;
+
+#ifdef EXPERIMENTAL_SRS
+addr_prop.srs_sender = NULL;
+#endif
+
+/* When verifying and testing addresses, the "logwrite" command in filters
+must be bypassed. */
+
+if (verify == v_none && !address_test_mode) options |= RDO_REALLOG;
+
+/* Sort out the fixed or dynamic uid/gid. This uid is used (a) for reading the
+file (and interpreting a filter) and (b) for running the transports for
+generated file and pipe addresses. It is not (necessarily) the same as the uids
+that may own the file. Exim panics if an expanded string is not a number and
+can't be found in the password file. Other errors set the freezing bit. */
+
+if (!rf_get_ugid(rblock, addr, &ugid)) return DEFER;
+
+if (!ugid.uid_set && pw != NULL)
+  {
+  ugid.uid = pw->pw_uid;
+  ugid.uid_set = TRUE;
+  }
+
+if (!ugid.gid_set && pw != NULL)
+  {
+  ugid.gid = pw->pw_gid;
+  ugid.gid_set = TRUE;
+  }
+
+#ifdef EXPERIMENTAL_SRS
+  /* Perform SRS on recipient/return-path as required  */
+
+  if(ob->srs != NULL)
+  {
+    BOOL usesrs = TRUE;
+
+    if(ob->srs_condition != NULL)
+      usesrs = expand_check_condition(ob->srs_condition, "srs_condition expansion failed", NULL);
+
+    if(usesrs)
+    {
+      int srs_action = 0, n_srs;
+      uschar *res;
+      uschar *usedomain;
+
+      /* What are we doing? */
+      if(Ustrcmp(ob->srs, "forward") == 0)
+        srs_action = 1;
+      else if(Ustrcmp(ob->srs, "reverseandforward") == 0)
+      {
+        srs_action = 3;
+
+        if((ob->srs_dbinsert == NULL) ^ (ob->srs_dbselect == NULL))
+          return DEFER;
+      }
+      else if(Ustrcmp(ob->srs, "reverse") == 0)
+        srs_action = 2;
+
+      /* Reverse SRS */
+      if(srs_action & 2)
+      {
+        srs_orig_recipient = addr->address;
+
+        eximsrs_init();
+        if(ob->srs_dbselect)
+          eximsrs_db_set(TRUE, ob->srs_dbselect);
+/* Comment this out for now...
+//        else
+//          eximsrs_db_set(TRUE, NULL);
+*/
+
+        if((n_srs = eximsrs_reverse(&res, addr->address)) == OK)
+        {
+          srs_recipient = res;
+          DEBUG(D_any)
+            debug_printf("SRS (reverse): Recipient '%s' rewritten to '%s'\n", srs_orig_recipient, srs_recipient);
+        }
+
+        eximsrs_done();
+
+        if(n_srs != OK)
+          return n_srs;
+      }
+
+      /* Forward SRS */
+      /* No point in actually performing SRS if we are just verifying a recipient */
+      if((srs_action & 1) && verify == v_none &&
+         (sender_address ? sender_address[0] != 0 : FALSE))
+      {
+
+        srs_orig_sender = sender_address;
+        eximsrs_init();
+        if(ob->srs_dbinsert)
+          eximsrs_db_set(FALSE, ob->srs_dbinsert);
+/* Comment this out for now...
+//        else
+//          eximsrs_db_set(FALSE, NULL);
+*/
+
+        if(ob->srs_alias != NULL ? (usedomain = expand_string(ob->srs_alias)) == NULL : 1)
+          usedomain = deliver_domain;
+
+        if((n_srs = eximsrs_forward(&res, sender_address, usedomain)) == OK)
+        {
+          addr_prop.srs_sender = res;
+          DEBUG(D_any)
+            debug_printf("SRS (forward): Sender '%s' rewritten to '%s'\n", srs_orig_sender, res);
+        }
+
+        eximsrs_done();
+
+        if(n_srs != OK)
+          return n_srs;
+      }
+    }
+  }
+#endif
+
+/* Call the function that interprets redirection data, either inline or from a
+file. This is a separate function so that the system filter can use it. It will
+run the function in a subprocess if necessary. If qualify_preserve_domain is
+set, temporarily reset qualify_domain_recipient to the current domain so that
+any unqualified addresses get qualified with the same domain as the incoming
+address. Otherwise, if a local qualify_domain is provided, set that up. */
+
+if (ob->qualify_preserve_domain)
+  qualify_domain_recipient = addr->domain;
+else if (ob->qualify_domain != NULL)
+  {
+  uschar *new_qdr = rf_expand_data(addr, ob->qualify_domain, &xrc);
+  if (new_qdr == NULL) return xrc;
+  qualify_domain_recipient = new_qdr;
+  }
+
+redirect.owners = ob->owners;
+redirect.owngroups = ob->owngroups;
+redirect.modemask = ob->modemask;
+redirect.check_owner = ob->check_owner;
+redirect.check_group = ob->check_group;
+redirect.pw = pw;
+
+if (ob->file != NULL)
+  {
+  redirect.string = ob->file;
+  redirect.isfile = TRUE;
+  }
+else
+  {
+  redirect.string = ob->data;
+  redirect.isfile = FALSE;
+  }
+
+frc = rda_interpret(&redirect, options, ob->include_directory,
+  ob->sieve_vacation_directory, ob->sieve_enotify_mailto_owner,
+  ob->sieve_useraddress, ob->sieve_subaddress, &ugid, &generated,
+  &(addr->message), ob->skip_syntax_errors? &eblock : NULL, &filtertype,
+  string_sprintf("%s router (recipient is %s)", rblock->name, addr->address));
+
+qualify_domain_recipient = save_qualify_domain_recipient;
+
+/* Handle exceptional returns from filtering or processing an address list.
+For FAIL and FREEZE we honour any previously set up deliveries by a filter. */
+
+switch (frc)
+  {
+  case FF_NONEXIST:
+  addr->message = addr->user_message = NULL;
+  return DECLINE;
+
+  case FF_BLACKHOLE:
+  DEBUG(D_route) debug_printf("address :blackhole:d\n");
+  generated = NULL;
+  discarded = US":blackhole:";
+  frc = FF_DELIVERED;
+  break;
+
+  /* FF_DEFER and FF_FAIL can arise only as a result of explicit commands
+  (:defer: or :fail: in an alias file or "fail" in a filter). If a configured
+  message was supplied, allow it to be included in an SMTP response after
+  verifying. Remove any SMTP code if it is not allowed. */
+
+  case FF_DEFER:
+  yield = DEFER;
+  goto SORT_MESSAGE;
+
+  case FF_FAIL:
+  if ((xrc = sort_errors_and_headers(rblock, addr, verify, &addr_prop)) != OK)
+    return xrc;
+  add_generated(rblock, addr_new, addr, generated, &addr_prop, &ugid, pw);
+  yield = FAIL;
+
+  SORT_MESSAGE:
+  if (addr->message == NULL)
+    addr->message = (yield == FAIL)? US"forced rejection" : US"forced defer";
+  else
+    {
+    int ovector[3];
+    if (ob->forbid_smtp_code &&
+        pcre_exec(regex_smtp_code, NULL, CS addr->message,
+          Ustrlen(addr->message), 0, PCRE_EOPT,
+          ovector, sizeof(ovector)/sizeof(int)) >= 0)
+      {
+      DEBUG(D_route) debug_printf("SMTP code at start of error message "
+        "is ignored because forbid_smtp_code is set\n");
+      addr->message += ovector[1];
+      }
+    addr->user_message = addr->message;
+    setflag(addr, af_pass_message);
+    }
+  return yield;
+
+  /* As in the case of a system filter, a freeze does not happen after a manual
+  thaw. In case deliveries were set up by the filter, we set the child count
+  high so that their completion does not mark the original address done. */
+
+  case FF_FREEZE:
+  if (!deliver_manual_thaw)
+    {
+    if ((xrc = sort_errors_and_headers(rblock, addr, verify, &addr_prop))
+      != OK) return xrc;
+    add_generated(rblock, addr_new, addr, generated, &addr_prop, &ugid, pw);
+    if (addr->message == NULL) addr->message = US"frozen by filter";
+    addr->special_action = SPECIAL_FREEZE;
+    addr->child_count = 9999;
+    return DEFER;
+    }
+  frc = FF_NOTDELIVERED;
+  break;
+
+  /* Handle syntax errors and :include: failures and lookup defers */
+
+  case FF_ERROR:
+  case FF_INCLUDEFAIL:
+
+  /* If filtertype is still FILTER_UNSET, it means that the redirection data
+  was never inspected, so the error was an expansion failure or failure to open
+  the file, or whatever. In these cases, the existing error message is probably
+  sufficient. */
+
+  if (filtertype == FILTER_UNSET) return DEFER;
+
+  /* If it was a filter and skip_syntax_errors is set, we want to set up
+  the error message so that it can be logged and mailed to somebody. */
+
+  if (filtertype != FILTER_FORWARD && ob->skip_syntax_errors)
+    {
+    eblock = store_get(sizeof(error_block));
+    eblock->next = NULL;
+    eblock->text1 = addr->message;
+    eblock->text2 = NULL;
+    addr->message = addr->user_message = NULL;
+    }
+
+  /* Otherwise set up the error for the address and defer. */
+
+  else
+    {
+    addr->basic_errno = ERRNO_BADREDIRECT;
+    addr->message = string_sprintf("error in %s %s: %s",
+      (filtertype != FILTER_FORWARD)? "filter" : "redirect",
+      (ob->data == NULL)? "file" : "data",
+      addr->message);
+    return DEFER;
+    }
+  }
+
+
+/* Yield is either FF_DELIVERED (significant action) or FF_NOTDELIVERED (no
+significant action). Before dealing with these, however, we must handle the
+effect of skip_syntax_errors.
+
+If skip_syntax_errors was set and there were syntax errors in an address list,
+error messages will be present in eblock. Log them and send a message if so
+configured. We cannot do this earlier, because the error message must not be
+sent as the local user. If there were no valid addresses, generated will be
+NULL. In this case, the router declines.
+
+For a filter file, the error message has been fudged into an eblock. After
+dealing with it, the router declines. */
+
+if (eblock != NULL)
+  {
+  if (!moan_skipped_syntax_errors(
+        rblock->name,                            /* For message content */
+        eblock,                                  /* Ditto */
+        (verify != v_none || address_test_mode)?
+          NULL : ob->syntax_errors_to,           /* Who to mail */
+        generated != NULL,                       /* True if not all failed */
+        ob->syntax_errors_text))                 /* Custom message */
+    return DEFER;
+
+  if (filtertype != FILTER_FORWARD || generated == NULL)
+    {
+    addr->message = US"syntax error in redirection data";
+    return DECLINE;
+    }
+  }
+
+/* Sort out the errors address and any header modifications, and handle the
+generated addresses, if any. If there are no generated addresses, we must avoid
+calling sort_errors_and_headers() in case this router declines - that function
+may modify the errors_address field in the current address, and we don't want
+to do that for a decline. */
+
+if (generated != NULL)
+  {
+  if ((xrc = sort_errors_and_headers(rblock, addr, verify, &addr_prop)) != OK)
+    return xrc;
+  add_generated(rblock, addr_new, addr, generated, &addr_prop, &ugid, pw);
+  }
+
+/* FF_DELIVERED with no generated addresses is what we get when an address list
+contains :blackhole: or a filter contains "seen finish" without having
+generated anything. Log what happened to this address, and return DISCARD. */
+
+if (frc == FF_DELIVERED)
+  {
+  if (generated == NULL && verify == v_none && !address_test_mode)
+    {
+    log_write(0, LOG_MAIN, "=> %s <%s> R=%s", discarded, addr->address,
+      rblock->name);
+    yield = DISCARD;
+    }
+  }
+
+/* For an address list, FF_NOTDELIVERED always means that no addresses were
+generated. For a filter, addresses may or may not have been generated. If none
+were, it's the same as an empty address list, and the router declines. However,
+if addresses were generated, we can't just decline because successful delivery
+of the base address gets it marked "done", so deferred generated addresses
+never get tried again. We have to generate a new version of the base address,
+as if there were a "deliver" command in the filter file, with the original
+address as parent. */
+
+else
+  {
+  address_item *next;
+
+  if (generated == NULL) return DECLINE;
+
+  next = deliver_make_addr(addr->address, FALSE);
+  next->parent = addr;
+  addr->child_count++;
+  next->next = *addr_new;
+  *addr_new = next;
+
+  /* Copy relevant flags (af_propagate is a name for the set), and set the
+  data that propagates. */
+
+  copyflag(next, addr, af_propagate);
+  next->p = addr_prop;
+
+  DEBUG(D_route) debug_printf("%s router autogenerated %s\n%s%s%s",
+    rblock->name,
+    next->address,
+    (addr_prop.errors_address != NULL)? "  errors to " : "",
+    (addr_prop.errors_address != NULL)? addr_prop.errors_address : US"",
+    (addr_prop.errors_address != NULL)? "\n" : "");
+  }
+
+/* Control gets here only when the address has been completely handled. Put the
+original address onto the succeed queue so that any retry items that get
+attached to it get processed. */
+
+addr->next = *addr_succeed;
+*addr_succeed = addr;
+
+return yield;
+}
+
+/* End of routers/redirect.c */
diff --git a/src/routers/redirect.h b/src/routers/redirect.h
new file mode 100644 (file)
index 0000000..74e14af
--- /dev/null
@@ -0,0 +1,77 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Header for the redirect router */
+
+/* Private structure for the private options. */
+
+typedef struct {
+  transport_instance *directory_transport;
+  transport_instance *file_transport;
+  transport_instance *pipe_transport;
+  transport_instance *reply_transport;
+
+  uschar *data;
+  uschar *directory_transport_name;
+  uschar *file;
+  uschar *file_dir;
+  uschar *file_transport_name;
+  uschar *include_directory;
+  uschar *pipe_transport_name;
+  uschar *reply_transport_name;
+  uschar *sieve_subaddress;
+  uschar *sieve_useraddress;
+  uschar *sieve_vacation_directory;
+  uschar *sieve_enotify_mailto_owner;
+  uschar *syntax_errors_text;
+  uschar *syntax_errors_to;
+  uschar *qualify_domain;
+
+  uid_t  *owners;
+  gid_t  *owngroups;
+
+#ifdef EXPERIMENTAL_SRS
+  uschar *srs;
+  uschar *srs_alias;
+  uschar *srs_condition;
+  uschar *srs_dbinsert;
+  uschar *srs_dbselect;
+#endif
+
+  int   modemask;
+  int   bit_options;
+  BOOL  check_ancestor;
+  BOOL  check_group;
+  BOOL  check_owner;
+  BOOL  forbid_file;
+  BOOL  forbid_filter_reply;
+  BOOL  forbid_pipe;
+  BOOL  forbid_smtp_code;
+  BOOL  hide_child_in_errmsg;
+  BOOL  one_time;
+  BOOL  qualify_preserve_domain;
+  BOOL  skip_syntax_errors;
+} redirect_router_options_block;
+
+/* Data for reading the private options. */
+
+extern optionlist redirect_router_options[];
+extern int redirect_router_options_count;
+
+/* Block containing default values. */
+
+extern redirect_router_options_block redirect_router_option_defaults;
+
+/* The main and initialization entry points for the router */
+
+extern int redirect_router_entry(router_instance *, address_item *,
+  struct passwd *, int, address_item **, address_item **,
+  address_item **, address_item **);
+
+extern void redirect_router_init(router_instance *);
+
+/* End of routers/redirect.h */
diff --git a/src/routers/rf_change_domain.c b/src/routers/rf_change_domain.c
new file mode 100644 (file)
index 0000000..2343e3e
--- /dev/null
@@ -0,0 +1,84 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+
+#include "../exim.h"
+#include "rf_functions.h"
+
+
+
+/*************************************************
+*          Change domain in an address           *
+*************************************************/
+
+/* When a router wants to change the address that is being routed, it is like a
+redirection. We insert a new parent of the current address to hold the original
+information, and change the data in the original address, which is now the
+child. The child address is put onto the addr_new chain. Pick up the local part
+from the "address" field so as to get it in external form - caseful, and with
+any quoting retained.
+
+Arguments:
+  addr        the address block
+  domain      the new domain
+  rewrite     TRUE if headers lines are to be rewritten
+  addr_new    the new address chain
+
+Returns:      nothing
+*/
+
+void
+rf_change_domain(address_item *addr, uschar *domain, BOOL rewrite,
+  address_item **addr_new)
+{
+address_item *parent = store_get(sizeof(address_item));
+uschar *at = Ustrrchr(addr->address, '@');
+uschar *address = string_sprintf("%.*s@%s", at - addr->address, addr->address,
+  domain);
+
+DEBUG(D_route) debug_printf("domain changed to %s\n", domain);
+
+/* The current address item is made into the parent, and a new address is set
+up in the old space. */
+
+*parent = *addr;
+
+/* First copy in initializing values, to wipe out stuff such as the named
+domain cache. Then copy over the propagating fields from the parent. Then set
+up the new fields. */
+
+*addr = address_defaults;
+addr->p = parent->p;
+
+addr->address = address;
+addr->unique = string_copy(address);
+addr->parent = parent;
+
+addr->next = *addr_new;
+*addr_new = addr;
+
+/* Rewrite header lines if requested */
+
+if (rewrite)
+  {
+  header_line *h;
+  DEBUG(D_route|D_rewrite) debug_printf("rewriting header lines\n");
+  for (h = header_list; h != NULL; h = h->next)
+    {
+    header_line *newh =
+      rewrite_header(h, parent->domain, domain,
+        global_rewrite_rules, rewrite_existflags, TRUE);
+    if (newh != NULL)
+      {
+      h = newh;
+      header_rewritten = TRUE;
+      }
+    }
+  }
+}
+
+/* End of rf_change_domain.c */
diff --git a/src/routers/rf_expand_data.c b/src/routers/rf_expand_data.c
new file mode 100644 (file)
index 0000000..cf5b9cb
--- /dev/null
@@ -0,0 +1,48 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+
+#include "../exim.h"
+#include "rf_functions.h"
+
+
+/*************************************************
+*       Expand data string and handle errors     *
+*************************************************/
+
+/* This little function is used by a couple of routers for expanding things. It
+just saves repeating this code too many times. It does an expansion, and
+chooses a suitable return code on error.
+
+Arguments:
+  addr       the address that's being routed
+  s          the string to be expanded
+  prc        pointer to where to put the return code on failure
+
+Returns:     the expanded string, or NULL (with prc set) on failure
+*/
+
+uschar *
+rf_expand_data(address_item *addr, uschar *s, int *prc)
+{
+uschar *yield = expand_string(s);
+if (yield != NULL) return yield;
+if (expand_string_forcedfail)
+  {
+  DEBUG(D_route) debug_printf("forced failure for expansion of \"%s\"\n", s);
+  *prc = DECLINE;
+  }
+else
+  {
+  addr->message = string_sprintf("failed to expand \"%s\": %s", s,
+    expand_string_message);
+  *prc = DEFER;
+  }
+return NULL;
+}
+
+/* End of routers/rf_expand_data.c */
diff --git a/src/routers/rf_functions.h b/src/routers/rf_functions.h
new file mode 100644 (file)
index 0000000..29f37cf
--- /dev/null
@@ -0,0 +1,31 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Header for the functions that are shared by the routers */
+
+
+extern void rf_add_generated(router_instance *, address_item **,
+              address_item *, address_item *, uschar *, header_line *,
+              uschar *, ugid_block *, struct passwd *);
+extern void rf_change_domain(address_item *, uschar *, BOOL, address_item **);
+extern uschar *rf_expand_data(address_item *, uschar *, int *);
+extern int  rf_get_errors_address(address_item *, router_instance *,
+              BOOL, uschar **);
+extern int  rf_get_munge_headers(address_item *, router_instance *,
+              header_line **, uschar **);
+extern BOOL rf_get_transport(uschar *, transport_instance **,  address_item *,
+              uschar *, uschar *);
+extern BOOL rf_get_ugid(router_instance *, address_item *, ugid_block *);
+extern int  rf_lookup_hostlist(router_instance *, address_item *, uschar *,
+              int, int, address_item **);
+extern BOOL rf_queue_add(address_item *, address_item **, address_item **,
+              router_instance *, struct passwd *);
+extern int  rf_self_action(address_item *, host_item *, int, BOOL, uschar *,
+              address_item **);
+extern void rf_set_ugid(address_item *, ugid_block *);
+
+/* End of rf_functions.h */
diff --git a/src/routers/rf_get_errors_address.c b/src/routers/rf_get_errors_address.c
new file mode 100644 (file)
index 0000000..457397a
--- /dev/null
@@ -0,0 +1,131 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+#include "../exim.h"
+#include "rf_functions.h"
+
+
+/*************************************************
+*        Get errors address for a router         *
+*************************************************/
+
+/* This function is called by routers to sort out the errors address for a
+particular address. If there is a setting in the router block, then expand and
+verify it, and if it works, use it. Otherwise use any setting that is in the
+address itself. This might be NULL, meaning unset (the message's sender is then
+used). Verification isn't done when the original address is just being
+verified, as otherwise there might be routing loops if someone sets up a silly
+configuration.
+
+Arguments:
+  addr         the input address
+  rblock       the router instance
+  verify       v_none / v_recipient / v_sender / v_expn
+  errors_to    point the errors address here
+
+Returns:       OK if no problem
+               DEFER if verifying the address caused a deferment
+                 or a big disaster (e.g. expansion failure)
+*/
+
+int
+rf_get_errors_address(address_item *addr, router_instance *rblock,
+  int verify, uschar **errors_to)
+{
+uschar *s;
+
+*errors_to = addr->p.errors_address;
+if (rblock->errors_to == NULL) return OK;
+
+s = expand_string(rblock->errors_to);
+
+if (s == NULL)
+  {
+  if (expand_string_forcedfail)
+    {
+    DEBUG(D_route)
+      debug_printf("forced expansion failure - ignoring errors_to\n");
+    return OK;
+    }
+  addr->message = string_sprintf("%s router failed to expand \"%s\": %s",
+    rblock->name, rblock->errors_to, expand_string_message);
+  return DEFER;
+  }
+
+/* If the errors_to address is empty, it means "ignore errors" */
+
+if (*s == 0)
+  {
+  setflag(addr, af_ignore_error);      /* For locally detected errors */
+  *errors_to = US"";                   /* Return path for SMTP */
+  return OK;
+  }
+
+/* If we are already verifying, do not check the errors address, in order to
+save effort (but we do verify when testing an address). When we do verify, set
+the sender address to null, because that's what it will be when sending an
+error message, and there are now configuration options that control the running
+of routers by checking the sender address. When testing an address, there may
+not be a sender address. We also need to save and restore the expansion values
+associated with an address. */
+
+if (verify != v_none)
+  {
+  *errors_to = s;
+  DEBUG(D_route)
+    debug_printf("skipped verify errors_to address: already verifying\n");
+  }
+else
+  {
+  BOOL save_address_test_mode = address_test_mode;
+  int save1 = 0;
+  int i;
+  uschar ***p;
+  uschar *address_expansions_save[ADDRESS_EXPANSIONS_COUNT];
+  address_item *snew = deliver_make_addr(s, FALSE);
+
+  if (sender_address != NULL)
+    {
+    save1 = sender_address[0];
+    sender_address[0] = 0;
+    }
+
+  for (i = 0, p = address_expansions; *p != NULL;)
+    address_expansions_save[i++] = **p++;
+  address_test_mode = FALSE;
+
+  /* NOTE: the address is verified as a recipient, not a sender. This is
+  perhaps confusing. It isn't immediately obvious what to do: we want to have
+  some confidence that we can deliver to the address, in which case it will be
+  a recipient, but on the other hand, it will be passed on in SMTP deliveries
+  as a sender. However, I think on balance recipient is right because sender
+  verification is really about the *incoming* sender of the message.
+
+  If this code is changed, note that you must set vopt_fake_sender instead of
+  vopt_is_recipient, as otherwise sender_address may be altered because
+  verify_address() thinks it is dealing with *the* sender of the message. */
+
+  DEBUG(D_route|D_verify)
+    debug_printf("------ Verifying errors address %s ------\n", s);
+  if (verify_address(snew, NULL,
+      vopt_is_recipient /* vopt_fake_sender is the alternative */
+      | vopt_qualify, -1, -1, -1, NULL, NULL, NULL) == OK)
+    *errors_to = snew->address;
+  DEBUG(D_route|D_verify)
+    debug_printf("------ End verifying errors address %s ------\n", s);
+
+  address_test_mode = save_address_test_mode;
+  for (i = 0, p = address_expansions; *p != NULL;)
+    **p++ = address_expansions_save[i++];
+
+  if (sender_address != NULL) sender_address[0] = save1;
+  }
+
+return OK;
+}
+
+/* End of rf_get_errors_address.c */
diff --git a/src/routers/rf_get_munge_headers.c b/src/routers/rf_get_munge_headers.c
new file mode 100644 (file)
index 0000000..a4a13b0
--- /dev/null
@@ -0,0 +1,112 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2014 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+#include "../exim.h"
+#include "rf_functions.h"
+
+
+/*************************************************
+*      Get additional headers for a router       *
+*************************************************/
+
+/* This function is called by routers to sort out the additional headers
+and header remove list for a particular address.
+
+Arguments:
+  addr           the input address
+  rblock         the router instance
+  extra_headers  points to where to hang the header chain
+  remove_headers points to where to hang the remove list
+
+Returns:         OK if no problem
+                 DEFER if expanding a string caused a deferment
+                   or a big disaster (e.g. expansion failure)
+*/
+
+int
+rf_get_munge_headers(address_item *addr, router_instance *rblock,
+  header_line **extra_headers, uschar **remove_headers)
+{
+/* Default is to retain existing headers */
+*extra_headers = addr->p.extra_headers;
+
+if (rblock->extra_headers)
+  {
+  uschar * list = rblock->extra_headers;
+  int sep = '\n';
+  uschar * s;
+  int slen;
+
+  while ((s = string_nextinlist(&list, &sep, NULL, 0)))
+    if (!(s = expand_string(s)))
+      {
+      if (!expand_string_forcedfail)
+       {
+       addr->message = string_sprintf("%s router failed to expand \"%s\": %s",
+         rblock->name, rblock->extra_headers, expand_string_message);
+       return DEFER;
+       }
+      }
+    else if ((slen = Ustrlen(s)) > 0)
+      {
+      /* Expand succeeded. Put extra headers at the start of the chain because
+      further down it may point to headers from other routers, which may be
+      shared with other addresses. The output function outputs them in reverse
+      order. */
+
+      header_line *  h = store_get(sizeof(header_line));
+
+      /* We used to use string_sprintf() to add the newline if needed, but that
+      causes problems if the header line is exceedingly long (e.g. adding
+      something to a pathologically long line). So avoid it. */
+
+      if (s[slen-1] == '\n')
+       h->text = s;
+      else
+       {
+       h->text = store_get(slen+2);
+       memcpy(h->text, s, slen);
+       h->text[slen++] = '\n';
+       h->text[slen] = 0;
+       }
+
+      h->next = *extra_headers;
+      h->type = htype_other;
+      h->slen = slen;
+      *extra_headers = h;
+      }
+  }
+
+/* Default is to retain existing removes */
+*remove_headers = addr->p.remove_headers;
+
+/* Expand items from colon-sep list separately, then build new list */
+if (rblock->remove_headers)
+  {
+  uschar * list = rblock->remove_headers;
+  int sep = ':';
+  uschar * s;
+  uschar buffer[128];
+
+  while ((s = string_nextinlist(&list, &sep, buffer, sizeof(buffer))))
+    if (!(s = expand_string(s)))
+      {
+      if (!expand_string_forcedfail)
+       {
+       addr->message = string_sprintf("%s router failed to expand \"%s\": %s",
+         rblock->name, rblock->remove_headers, expand_string_message);
+       return DEFER;
+       }
+      }
+    else if (*s)
+      *remove_headers = string_append_listele(*remove_headers, ':', s);
+  }
+
+return OK;
+}
+
+/* End of rf_get_munge_headers.c */
diff --git a/src/routers/rf_get_transport.c b/src/routers/rf_get_transport.c
new file mode 100644 (file)
index 0000000..ad5cda8
--- /dev/null
@@ -0,0 +1,90 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+#include "../exim.h"
+#include "rf_functions.h"
+
+
+/*************************************************
+*       Get transport for a router               *
+*************************************************/
+
+/* If transport_name contains $, it must be expanded each time and used as a
+transport name. Otherwise, look up the transport only if the destination is not
+already set.
+
+Some routers (e.g. accept) insist that their transport option is set at
+initialization time. However, for some (e.g. file_transport in redirect), there
+is no such check, because the transport may not be required. Calls to this
+function from the former type of router have require_name = NULL, because it
+will never be used. NULL is also used in verify_only cases, where a transport
+is not required.
+
+Arguments:
+  tpname        the text of the transport name
+  tpptr         where to put the transport
+  addr          the address being processed
+  router_name   for error messages
+  require_name  used in the error message if transport is unset
+
+Returns:        TRUE if *tpptr is already set and tpname has no '$' in it;
+                TRUE if a transport has been placed in tpptr;
+                FALSE if there's a problem, in which case
+                addr->message contains a message, and addr->basic_errno has
+                ERRNO_BADTRANSPORT set in it.
+*/
+
+BOOL
+rf_get_transport(uschar *tpname, transport_instance **tpptr, address_item *addr,
+  uschar *router_name, uschar *require_name)
+{
+uschar *ss;
+BOOL expandable;
+transport_instance *tp;
+
+if (tpname == NULL)
+  {
+  if (require_name == NULL) return TRUE;
+  addr->basic_errno = ERRNO_BADTRANSPORT;
+  addr->message = string_sprintf("%s unset in %s router", require_name,
+    router_name);
+  return FALSE;
+  }
+
+expandable = Ustrchr(tpname, '$') != NULL;
+if (*tpptr != NULL && !expandable) return TRUE;
+
+if (expandable)
+  {
+  ss = expand_string(tpname);
+  if (ss == NULL)
+    {
+    addr->basic_errno = ERRNO_BADTRANSPORT;
+    addr->message = string_sprintf("failed to expand transport "
+      "\"%s\" in %s router: %s", tpname, router_name, expand_string_message);
+    return FALSE;
+    }
+  }
+else ss = tpname;
+
+for (tp = transports; tp != NULL; tp = tp->next)
+  {
+  if (Ustrcmp(tp->name, ss) == 0)
+    {
+    DEBUG(D_route) debug_printf("set transport %s\n", ss);
+    *tpptr = tp;
+    return TRUE;
+    }
+  }
+
+addr->basic_errno = ERRNO_BADTRANSPORT;
+addr->message = string_sprintf("transport \"%s\" not found in %s router", ss,
+  router_name);
+return FALSE;
+}
+
+/* End of rf_get_transport.c */
diff --git a/src/routers/rf_get_ugid.c b/src/routers/rf_get_ugid.c
new file mode 100644 (file)
index 0000000..1735e59
--- /dev/null
@@ -0,0 +1,80 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+#include "../exim.h"
+#include "rf_functions.h"
+
+
+/*************************************************
+*            Get uid/gid for a router            *
+*************************************************/
+
+/* This function is called by routers to sort out the uid/gid values which are
+passed with an address for use by local transports.
+
+Arguments:
+  rblock       the router block
+  addr         the address being worked on
+  ugid         pointer to a ugid block to fill in
+
+Returns:       TRUE if all goes well, else FALSE
+*/
+
+BOOL
+rf_get_ugid(router_instance *rblock, address_item *addr, ugid_block *ugid)
+{
+struct passwd *upw = NULL;
+
+/* Initialize from fixed values */
+
+ugid->uid = rblock->uid;
+ugid->gid = rblock->gid;
+ugid->uid_set = rblock->uid_set;
+ugid->gid_set = rblock->gid_set;
+ugid->initgroups = rblock->initgroups;
+
+/* If there is no fixed uid set, see if there's a dynamic one that can
+be expanded and possibly looked up. */
+
+if (!ugid->uid_set && rblock->expand_uid != NULL)
+  {
+  if (route_find_expanded_user(rblock->expand_uid, rblock->name, US"router",
+    &upw, &(ugid->uid), &(addr->message))) ugid->uid_set = TRUE;
+  else return FALSE;
+  }
+
+/* Likewise for the gid */
+
+if (!ugid->gid_set && rblock->expand_gid != NULL)
+  {
+  if (route_find_expanded_group(rblock->expand_gid, rblock->name, US"router",
+    &(ugid->gid), &(addr->message))) ugid->gid_set = TRUE;
+  else return FALSE;
+  }
+
+/* If a uid is set, then a gid must also be available; use one from the passwd
+lookup if it happened. */
+
+if (ugid->uid_set && !ugid->gid_set)
+  {
+  if (upw != NULL)
+    {
+    ugid->gid = upw->pw_gid;
+    ugid->gid_set = TRUE;
+    }
+  else
+    {
+    addr->message = string_sprintf("user set without group for %s router",
+      rblock->name);
+    return FALSE;
+    }
+  }
+
+return TRUE;
+}
+
+/* End of rf_get_ugid.c */
diff --git a/src/routers/rf_lookup_hostlist.c b/src/routers/rf_lookup_hostlist.c
new file mode 100644 (file)
index 0000000..0eae31e
--- /dev/null
@@ -0,0 +1,228 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+
+#include "../exim.h"
+#include "rf_functions.h"
+
+
+
+/*************************************************
+*     Look up IP addresses for a set of hosts    *
+*************************************************/
+
+/* This function is called by a router to fill in the IP addresses for a set of
+hosts that are attached to an address. Each host has its name and MX value set;
+and those that need processing have their address fields set NULL. Multihomed
+hosts cause additional blocks to be inserted into the chain.
+
+This function also supports pseudo-hosts whose names end with "/MX". In this
+case, MX records are looked up for the name, and the list of hosts obtained
+replaces the incoming "host". In other words, "x/MX" is shorthand for "those
+hosts pointed to by x's MX records".
+
+It is also possible for a port to be specified along with the host name or IP
+address. The syntax is to add ":port" on to the end. This doesn't work with
+IPv6 addresses, so we allow IP addresses to be enclosed in [] in order to make
+this work. The specification of the port must come last, that is, after "/MX"
+if that is present.
+
+Arguments:
+  rblock               the router block
+  addr                 the address being routed
+  ignore_target_hosts  list of hosts to ignore
+  lookup_type          lk_default or lk_byname or lk_bydns
+  hff_code             what to do for host find failed
+  addr_new             passed to rf_self_action for self=reroute
+
+Returns:               OK
+                       DEFER host lookup defer
+                       PASS  timeout etc and pass_on_timeout set
+                       self_action: PASS, DECLINE, DEFER, FAIL, FREEZE
+                       hff_code after host find failed
+*/
+
+int
+rf_lookup_hostlist(router_instance *rblock, address_item *addr,
+  uschar *ignore_target_hosts, int lookup_type, int hff_code,
+  address_item **addr_new)
+{
+BOOL self_send = FALSE;
+host_item *h, *next_h, *prev;
+
+/* Look up each host address. A lookup may add additional items into the chain
+if there are multiple addresses. Hence the use of next_h to start each cycle of
+the loop at the next original host. If any host is identified as being the local
+host, omit it and any subsequent hosts - i.e. treat the list like an ordered
+list of MX hosts. If the first host is the local host, act according to the
+"self" option in the configuration. */
+
+prev = NULL;
+for (h = addr->host_list; h != NULL; h = next_h)
+  {
+  uschar *canonical_name;
+  int rc, len, port;
+
+  next_h = h->next;
+  if (h->address != NULL) { prev = h; continue; }
+
+  DEBUG(D_route|D_host_lookup)
+    debug_printf("finding IP address for %s\n", h->name);
+
+  /* Handle any port setting that may be on the name; it will be removed
+  from the end of the name. */
+
+  port = host_item_get_port(h);
+
+  /* If the name ends with "/MX", we interpret it to mean "the list of hosts
+  pointed to by MX records with this name". */
+
+  len = Ustrlen(h->name);
+  if (len > 3 && strcmpic(h->name + len - 3, US"/mx") == 0)
+    {
+    DEBUG(D_route|D_host_lookup)
+      debug_printf("doing DNS MX lookup for %s\n", h->name);
+
+    h->name = string_copyn(h->name, len - 3);
+    rc = host_find_bydns(h,
+        ignore_target_hosts,
+        HOST_FIND_BY_MX,                /* look only for MX records */
+        NULL,                           /* SRV service not relevant */
+        NULL,                           /* failing srv domains not relevant */
+        NULL,                           /* no special mx failing domains */
+       NULL,                           /* no dnssec request XXX ? */
+       NULL,                           /* no dnssec require XXX ? */
+        NULL,                           /* fully_qualified_name */
+        NULL);                          /* indicate local host removed */
+    }
+
+  /* If explicitly configured to look up by name, or if the "host name" is
+  actually an IP address, do a byname lookup. */
+
+  else if (lookup_type == lk_byname || string_is_ip_address(h->name, NULL) != 0)
+    {
+    DEBUG(D_route|D_host_lookup) debug_printf("calling host_find_byname\n");
+    rc = host_find_byname(h, ignore_target_hosts, HOST_FIND_QUALIFY_SINGLE,
+      &canonical_name, TRUE);
+    }
+
+  /* Otherwise, do a DNS lookup. If that yields "host not found", and the
+  lookup type is the default (i.e. "bydns" is not explicitly configured),
+  follow up with a byname lookup, just in case. */
+
+  else
+    {
+    BOOL removed;
+    DEBUG(D_route|D_host_lookup) debug_printf("doing DNS lookup\n");
+    rc = host_find_bydns(h, ignore_target_hosts, HOST_FIND_BY_A, NULL, NULL,
+      NULL,
+      NULL, NULL,      /*XXX dnssec? */
+      &canonical_name, &removed);
+    if (rc == HOST_FOUND)
+      {
+      if (removed) setflag(addr, af_local_host_removed);
+      }
+    else if (rc == HOST_FIND_FAILED)
+      {
+      if (lookup_type == lk_default)
+        {
+        DEBUG(D_route|D_host_lookup)
+          debug_printf("DNS lookup failed: trying getipnodebyname\n");
+        rc = host_find_byname(h, ignore_target_hosts, HOST_FIND_QUALIFY_SINGLE,
+          &canonical_name, TRUE);
+        }
+      }
+    }
+
+  /* Temporary failure defers, unless pass_on_timeout is set */
+
+  if (rc == HOST_FIND_AGAIN)
+    {
+    if (rblock->pass_on_timeout)
+      {
+      DEBUG(D_route)
+        debug_printf("%s router timed out and pass_on_timeout set\n",
+          rblock->name);
+      return PASS;
+      }
+    addr->message = string_sprintf("host lookup for %s did not complete "
+      "(DNS timeout?)", h->name);
+    addr->basic_errno = ERRNO_DNSDEFER;
+    return DEFER;
+    }
+
+  /* Permanent failure is controlled by host_find_failed */
+
+  if (rc == HOST_FIND_FAILED)
+    {
+    if (hff_code == hff_ignore)
+      {
+      if (prev == NULL) addr->host_list = next_h; else prev->next = next_h;
+      continue;   /* With the next host, leave prev unchanged */
+      }
+
+    if (hff_code == hff_pass) return PASS;
+    if (hff_code == hff_decline) return DECLINE;
+
+    addr->message =
+      string_sprintf("lookup of host \"%s\" failed in %s router%s",
+        h->name, rblock->name,
+        host_find_failed_syntax? ": syntax error in name" : "");
+
+    if (hff_code == hff_defer) return DEFER;
+    if (hff_code == hff_fail) return FAIL;
+
+    addr->special_action = SPECIAL_FREEZE;
+    return DEFER;
+    }
+
+  /* Deal with a port setting */
+
+  if (port != PORT_NONE)
+    {
+    host_item *hh;
+    for (hh = h; hh != next_h; hh = hh->next) hh->port = port;
+    }
+
+  /* A local host gets chopped, with its successors, if there are previous
+  hosts. Otherwise the self option is used. If it is set to "send", any
+  subsequent hosts that are also the local host do NOT get chopped. */
+
+  if (rc == HOST_FOUND_LOCAL && !self_send)
+    {
+    if (prev != NULL)
+      {
+      DEBUG(D_route)
+        {
+        debug_printf("Removed from host list:\n");
+        for (; h != NULL; h = h->next) debug_printf("  %s\n", h->name);
+        }
+      prev->next = NULL;
+      setflag(addr, af_local_host_removed);
+      break;
+      }
+    rc = rf_self_action(addr, h, rblock->self_code, rblock->self_rewrite,
+      rblock->self, addr_new);
+    if (rc != OK)
+      {
+      addr->host_list = NULL;   /* Kill the host list for */
+      return rc;                /* anything other than "send" */
+      }
+    self_send = TRUE;
+    }
+
+  /* Ensure that prev is the host before next_h; this will not be h if a lookup
+  found multiple addresses or multiple MX records. */
+
+  prev = h;
+  while (prev->next != next_h) prev = prev->next;
+  }
+
+return OK;
+}
+
+/* End of rf_lookup_hostlist.c */
diff --git a/src/routers/rf_queue_add.c b/src/routers/rf_queue_add.c
new file mode 100644 (file)
index 0000000..06cdb6c
--- /dev/null
@@ -0,0 +1,106 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+#include "../exim.h"
+#include "rf_functions.h"
+
+
+/*************************************************
+*        Queue address for transport             *
+*************************************************/
+
+/* This function is called to put an address onto the local or remote transport
+queue, as appropriate. When the driver is for verifying only, a transport need
+not be set, in which case it doesn't actually matter which queue the address
+gets put on.
+
+The generic uid/gid options are inspected and put into the address if they are
+set. For a remote transport, if there are fallback hosts, they are added to the
+address.
+
+Arguments:
+  addr          the address, with the transport field set (if not verify only)
+  paddr_local   pointer to the anchor of the local transport chain
+  paddr_remote  pointer to the anchor of the remote transport chain
+  rblock        the router block
+  pw            password entry if check_local_user was set, or NULL
+
+Returns:        FALSE on error; the only case is failing to get a uid/gid
+*/
+
+BOOL
+rf_queue_add(address_item *addr, address_item **paddr_local,
+  address_item **paddr_remote, router_instance *rblock, struct passwd *pw)
+{
+addr->p.domain_data = deliver_domain_data;         /* Save these values for */
+addr->p.localpart_data = deliver_localpart_data;   /* use in the transport */
+
+/* Handle a local transport */
+
+if (addr->transport != NULL && addr->transport->info->local)
+  {
+  ugid_block ugid;
+
+  /* Default uid/gid and transport-time home directory are from the passwd file
+  when check_local_user is set, but can be overridden by explicit settings.
+  When getting the home directory out of the password information, set the
+  flag that prevents expansion later. */
+
+  if (pw != NULL)
+    {
+    addr->uid = pw->pw_uid;
+    addr->gid = pw->pw_gid;
+    setflag(addr, af_uid_set|af_gid_set|af_home_expanded);
+    addr->home_dir = string_copy(US pw->pw_dir);
+    }
+
+  if (!rf_get_ugid(rblock, addr, &ugid)) return FALSE;
+  rf_set_ugid(addr, &ugid);
+
+  /* transport_home_directory (in rblock->home_directory) takes priority;
+  otherwise use the expanded value of router_home_directory. The flag also
+  tells the transport not to re-expand it. */
+
+  if (rblock->home_directory != NULL)
+    {
+    addr->home_dir = rblock->home_directory;
+    clearflag(addr, af_home_expanded);
+    }
+  else if (addr->home_dir == NULL && testflag(addr, af_home_expanded))
+    addr->home_dir = deliver_home;
+
+  addr->current_dir = rblock->current_directory;
+
+  addr->next = *paddr_local;
+  *paddr_local = addr;
+  }
+
+/* For a remote transport, set up the fallback host list, and keep a count of
+the total number of addresses routed to remote transports. */
+
+else
+  {
+  addr->fallback_hosts = rblock->fallback_hostlist;
+  addr->next = *paddr_remote;
+  *paddr_remote = addr;
+  remote_delivery_count++;
+  }
+
+DEBUG(D_route)
+  {
+  debug_printf("queued for %s transport: local_part = %s\ndomain = %s\n"
+    "  errors_to=%s\n",
+    (addr->transport == NULL)? US"<unset>" : addr->transport->name,
+    addr->local_part, addr->domain, addr->p.errors_address);
+  debug_printf("  domain_data=%s localpart_data=%s\n", addr->p.domain_data,
+    addr->p.localpart_data);
+  }
+
+return TRUE;
+}
+
+/* End of rf_queue_add.c */
diff --git a/src/routers/rf_self_action.c b/src/routers/rf_self_action.c
new file mode 100644 (file)
index 0000000..7434ddd
--- /dev/null
@@ -0,0 +1,141 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+
+#include "../exim.h"
+#include "rf_functions.h"
+
+
+
+/*************************************************
+*        Decode actions for self reference       *
+*************************************************/
+
+/* This function is called from a number of routers on receiving
+HOST_FOUND_LOCAL when looking up a supposedly remote host. The action is
+controlled by a generic configuration option called "self" on each router,
+which can be one of:
+
+   . freeze:                       Log the incident, freeze, and return DEFER
+
+   . defer:                        Log the incident and return DEFER
+
+   . fail:                         Fail the address
+
+   . send:                         Carry on with the delivery regardless -
+                                   this makes sense only if the SMTP
+                                   listener on this machine is a differently
+                                   configured MTA
+
+   . pass:                         The router passes; the address
+                                   gets passed to the next router, overriding
+                                   the setting of no_more
+
+   . reroute:<new-domain>          Change the domain to the given domain
+                                   and return REROUTE so it gets passed back
+                                   to the routers.
+
+   . reroute:rewrite:<new-domain>  The same, but headers containing the
+                                   old domain get rewritten.
+
+These string values are interpreted earlier on, and passed into this function
+as the values of "code" and "rewrite".
+
+Arguments:
+  addr       the address being routed
+  host       the host that is local, with MX set (or -1 if MX not used)
+  code       the action to be taken (one of the self_xxx enums)
+  rewrite    TRUE if rewriting headers required for REROUTED
+  new        new domain to be used for REROUTED
+  addr_new   child chain for REROUTEED
+
+Returns:   DEFER, REROUTED, PASS, FAIL, or OK, according to the value of code.
+*/
+
+int
+rf_self_action(address_item *addr, host_item *host, int code, BOOL rewrite,
+  uschar *new, address_item **addr_new)
+{
+uschar *msg = (host->mx >= 0)?
+  US"lowest numbered MX record points to local host" :
+  US"remote host address is the local host";
+
+switch (code)
+  {
+  case self_freeze:
+
+  /* If there is no message id, this is happening during an address
+  verification, so give information about the address that is being verified,
+  and where it has come from. Otherwise, during message delivery, the normal
+  logging for the address will be sufficient. */
+
+  if (message_id[0] == 0)
+    {
+    if (sender_fullhost == NULL)
+      {
+      log_write(0, LOG_MAIN, "%s: %s (while routing <%s>)", msg,
+        addr->domain, addr->address);
+      }
+    else
+      {
+      log_write(0, LOG_MAIN, "%s: %s (while verifying <%s> from host %s)",
+        msg, addr->domain, addr->address, sender_fullhost);
+      }
+    }
+  else
+    log_write(0, LOG_MAIN, "%s: %s", msg, addr->domain);
+
+  addr->message = msg;
+  addr->special_action = SPECIAL_FREEZE;
+  return DEFER;
+
+  case self_defer:
+  addr->message = msg;
+  return DEFER;
+
+  case self_reroute:
+  DEBUG(D_route)
+    {
+    debug_printf("%s: %s", msg, addr->domain);
+    debug_printf(": domain changed to %s\n", new);
+    }
+  rf_change_domain(addr, new, rewrite, addr_new);
+  return REROUTED;
+
+  case self_send:
+  DEBUG(D_route)
+    {
+    debug_printf("%s: %s", msg, addr->domain);
+    debug_printf(": configured to try delivery anyway\n");
+    }
+  return OK;
+
+  case self_pass:    /* This is soft failure; pass to next router */
+  DEBUG(D_route)
+    {
+    debug_printf("%s: %s", msg, addr->domain);
+    debug_printf(": passed to next router (self = pass)\n");
+    }
+  addr->message = msg;
+  addr->self_hostname = string_copy(host->name);
+  return PASS;
+
+  case self_fail:
+  DEBUG(D_route)
+    {
+    debug_printf("%s: %s", msg, addr->domain);
+    debug_printf(": address failed (self = fail)\n");
+    }
+  addr->message = msg;
+  setflag(addr, af_pass_message);
+  return FAIL;
+  }
+
+return DEFER;   /* paranoia */
+}
+
+/* End of rf_self_action.c */
diff --git a/src/routers/rf_set_ugid.c b/src/routers/rf_set_ugid.c
new file mode 100644 (file)
index 0000000..e1346b4
--- /dev/null
@@ -0,0 +1,44 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+#include "../exim.h"
+#include "rf_functions.h"
+
+
+/*************************************************
+*      Set uid/gid from block into address       *
+*************************************************/
+
+/* This function copies any set uid or gid from a ugid block into an
+address.
+
+Arguments:
+  addr        the address
+  ugid        the ugid block
+
+Returns:      nothing
+*/
+
+void
+rf_set_ugid(address_item *addr, ugid_block *ugid)
+{
+if (ugid->uid_set)
+  {
+  addr->uid = ugid->uid;
+  setflag(addr, af_uid_set);
+  }
+
+if (ugid->gid_set)
+  {
+  addr->gid = ugid->gid;
+  setflag(addr, af_gid_set);
+  }
+
+if (ugid->initgroups) setflag(addr, af_initgroups);
+}
+
+/* End of rf_set_ugid.c */
diff --git a/src/search.c b/src/search.c
new file mode 100644 (file)
index 0000000..15db52a
--- /dev/null
@@ -0,0 +1,823 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* A set of functions to search databases in various formats. An open
+database is represented by a void * value which is returned from a lookup-
+specific "open" function. These are now all held in individual modules in the
+lookups subdirectory and the functions here form a generic interface.
+
+Caching is used to improve performance. Open files are cached until a tidyup
+function is called, and for each file the result of the last lookup is cached.
+However, if too many files are opened, some of those that are not in use have
+to be closed. Those open items that use real files are kept on a LRU chain to
+help with this.
+
+All the data is held in permanent store so as to be independent of the stacking
+pool that is reset from time to time. In fact, we use malloc'd store so that it
+can be freed when the caches are tidied up. It isn't actually clear whether
+this is a benefit or not, to be honest. */
+
+#include "exim.h"
+
+
+/* Tree in which to cache open files until tidyup called. */
+
+static tree_node *search_tree = NULL;
+
+/* Two-way chain of open databases that use real files. This is maintained in
+recently-used order for the purposes of closing the least recently used when
+too many files are open. */
+
+static tree_node *open_top = NULL;
+static tree_node *open_bot = NULL;
+
+/* Count of open databases that use real files */
+
+static int open_filecount = 0;
+
+/* Allow us to reset store used for lookups and lookup caching */
+
+static void *search_reset_point = NULL;
+
+
+
+/*************************************************
+*      Validate a plain lookup type name         *
+*************************************************/
+
+/* Only those names that are recognized and whose code is included in the
+binary give an OK response. Use a binary chop search now that the list has got
+so long.
+
+Arguments:
+  name       lookup type name - not necessarily zero terminated (e.g. dbm*)
+  len        length of the name
+
+Returns:     +ve => valid lookup name; value is offset in lookup_list
+             -ve => invalid name; message in search_error_message.
+*/
+
+int
+search_findtype(uschar *name, int len)
+{
+int bot = 0;
+int top = lookup_list_count;
+while (top > bot)
+  {
+  int mid = (top + bot)/2;
+  int c = Ustrncmp(name, lookup_list[mid]->name, len);
+
+  /* If c == 0 we have matched the incoming name with the start of the search
+  type name. However, some search types are substrings of others (e.g. nis and
+  nisplus) so we need to check that the lengths are the same. The length of the
+  type name cannot be shorter (else c would not be 0); if it is not equal it
+  must be longer, and in that case, the incoming name comes before the name we
+  are testing. By leaving c == 0 when the lengths are different, and doing a
+  > 0 test below, this all falls out correctly. */
+
+  if (c == 0 && Ustrlen(lookup_list[mid]->name) == len)
+    {
+    if (lookup_list[mid]->find != NULL) return mid;
+    search_error_message  = string_sprintf("lookup type \"%.*s\" is not "
+      "available (not in the binary - check buildtime LOOKUP configuration)",
+      len, name);
+    return -1;
+    }
+
+  if (c > 0) bot = mid + 1; else top = mid;
+  }
+
+search_error_message = string_sprintf("unknown lookup type \"%.*s\"",len,name);
+return -1;
+}
+
+
+
+/*************************************************
+*       Validate a full lookup type name         *
+*************************************************/
+
+/* This function recognizes the "partial-" prefix and also terminating * and *@
+suffixes.
+
+Arguments:
+  name         the full lookup type name
+  ptypeptr     where to put the partial type
+                 after subtraction of 1024 or 2048:
+                   negative     => no partial matching
+                   non-negative => minimum number of non-wild components
+  ptypeaff     where to put a pointer to the affix
+                 the affix is within name if supplied therein
+                 otherwise it's a literal string
+  afflen       the length of the affix
+  starflags    where to put the SEARCH_STAR and SEARCH_STARAT flags
+
+Returns:     +ve => valid lookup name; value is offset in lookup_list
+             -ve => invalid name; message in search_error_message.
+*/
+
+int
+search_findtype_partial(uschar *name, int *ptypeptr, uschar **ptypeaff,
+  int *afflen, int *starflags)
+{
+int len, stype;
+int pv = -1;
+uschar *ss = name;
+
+*starflags = 0;
+*ptypeaff = NULL;
+
+/* Check for a partial matching type. It must start with "partial", optionally
+followed by a sequence of digits. If this is followed by "-", the affix is the
+default "*." string. Otherwise we expect an affix in parentheses. Affixes are a
+limited number of characters, not including parens. */
+
+if (Ustrncmp(name, "partial", 7) == 0)
+  {
+  ss += 7;
+  if (isdigit (*ss))
+    {
+    pv = 0;
+    while (isdigit(*ss)) pv = pv*10 + *ss++ - '0';
+    }
+  else pv = 2;         /* Default number of wild components */
+
+  if (*ss == '(')
+    {
+    *ptypeaff = ++ss;
+    while (ispunct(*ss) && *ss != ')') ss++;
+    if (*ss != ')') goto BAD_TYPE;
+    *afflen = ss++ - *ptypeaff;
+    }
+  else if (*ss++ == '-')
+    {
+    *ptypeaff = US "*.";
+    *afflen = 2;
+    }
+  else
+    {
+    BAD_TYPE:
+    search_error_message = string_sprintf("format error in lookup type \"%s\"",
+      name);
+    return -1;
+    }
+  }
+
+/* Now we are left with a lookup name, possibly followed by * or *@. */
+
+len = Ustrlen(ss);
+if (len >= 2 && Ustrncmp(ss + len - 2, "*@", 2) == 0)
+  {
+  *starflags |= SEARCH_STARAT;
+  len -= 2;
+  }
+else if (len >= 1 && ss[len-1]  == '*')
+  {
+  *starflags |= SEARCH_STAR;
+  len--;
+  }
+
+/* Check for the individual search type. Only those that are actually in the
+binary are valid. For query-style types, "partial" and default types are
+erroneous. */
+
+stype = search_findtype(ss, len);
+if (stype >= 0 && mac_islookup(stype, lookup_querystyle))
+  {
+  if (pv >= 0)
+    {
+    search_error_message = string_sprintf("\"partial\" is not permitted "
+      "for lookup type \"%s\"", ss);
+    return -1;
+    }
+  if ((*starflags & (SEARCH_STAR|SEARCH_STARAT)) != 0)
+    {
+    search_error_message = string_sprintf("defaults using \"*\" or \"*@\" are "
+      "not permitted for lookup type \"%s\"", ss);
+    return -1;
+    }
+  }
+
+*ptypeptr = pv;
+return stype;
+}
+
+
+
+/*************************************************
+*               Release cached resources         *
+*************************************************/
+
+/* When search_open is called it caches the "file" that it opens in
+search_tree. The name of the tree node is a concatenation of the search type
+with the file name. For query-style lookups, the file name is empty. Real files
+are normally closed only when this tidyup routine is called, typically at the
+end of sections of code where a number of lookups might occur. However, if too
+many files are open simultaneously, some get closed beforehand. They can't be
+removed from the tree. There is also a general tidyup function which is called
+for the lookup driver, if it exists.
+
+First, there is an internal, recursive subroutine.
+
+Argument:    a pointer to a search_openfile tree node
+Returns:     nothing
+*/
+
+static void
+tidyup_subtree(tree_node *t)
+{
+search_cache *c = (search_cache *)(t->data.ptr);
+if (t->left != NULL) tidyup_subtree(t->left);
+if (t->right != NULL) tidyup_subtree(t->right);
+if (c != NULL &&
+    c->handle != NULL &&
+    lookup_list[c->search_type]->close != NULL)
+  lookup_list[c->search_type]->close(c->handle);
+}
+
+
+/* The external entry point
+
+Argument: none
+Returns:  nothing
+*/
+
+void
+search_tidyup(void)
+{
+int i;
+int old_pool = store_pool;
+
+DEBUG(D_lookup) debug_printf("search_tidyup called\n");
+
+/* Close individually each cached open file. */
+
+store_pool = POOL_SEARCH;
+if (search_tree != NULL)
+  {
+  tidyup_subtree(search_tree);
+  search_tree = NULL;
+  }
+open_top = open_bot = NULL;
+open_filecount = 0;
+
+/* Call the general tidyup entry for any drivers that have one. */
+
+for (i = 0; i < lookup_list_count; i++)
+  if (lookup_list[i]->tidy != NULL) (lookup_list[i]->tidy)();
+
+if (search_reset_point != NULL) store_reset(search_reset_point);
+search_reset_point = NULL;
+store_pool = old_pool;
+}
+
+
+
+
+/*************************************************
+*             Open search database               *
+*************************************************/
+
+/* A mode, and lists of owners and groups, are passed over for checking in
+the cases where the database is one or more files. Return NULL, with a message
+pointed to by message, in cases of error.
+
+For search types that use a file or files, check up on the mode after
+opening. It is tempting to do a stat before opening the file, and use it as
+an existence check. However, doing that opens a small security loophole in
+that the status could be changed before the file is opened. Can't quite see
+what problems this might lead to, but you can't be too careful where security
+is concerned. Fstat() on an open file can normally be expected to succeed,
+but there are some NFS states where it does not.
+
+There are two styles of query: (1) in the "single-key+file" style, a single
+key string and a file name are given, for example, for linear searches, DBM
+files, or for NIS. (2) In the "query" style, no "filename" is given; instead
+just a single query string is passed. This applies to multiple-key lookup
+types such as NIS+.
+
+Before opening, scan the tree of cached files to see if this file is already
+open for the correct search type. If so, return the saved handle. If not, put
+the handle in the tree for possible subsequent use. See search_tidyup above for
+closing all the cached files.
+
+A count of open databases which use real files is maintained, and if this
+gets too large, we have to close a cached file. Its entry remains in the tree,
+but is marked closed.
+
+Arguments:
+  filename       the name of the file for single-key+file style lookups,
+                 NULL for query-style lookups
+  search_type    the type of search required
+  modemask       if a real single file is used, this specifies mode bits that
+                 must not be set; otherwise it is ignored
+  owners         if a real single file is used, this specifies the possible
+                 owners of the file; otherwise it is ignored
+  owngroups      if a real single file is used, this specifies the possible
+                 group owners of the file; otherwise it is ignored
+
+Returns:         an identifying handle for the open database;
+                 this is the pointer to the tree block in the
+                 cache of open files; return NULL on open failure, with
+                 a message in search_error_message
+*/
+
+void *
+search_open(uschar *filename, int search_type, int modemask, uid_t *owners,
+  gid_t *owngroups)
+{
+void *handle;
+tree_node *t;
+search_cache *c;
+lookup_info *lk = lookup_list[search_type];
+uschar keybuffer[256];
+int old_pool = store_pool;
+
+/* Change to the search store pool and remember our reset point */
+
+store_pool = POOL_SEARCH;
+if (search_reset_point == NULL) search_reset_point = store_get(0);
+
+DEBUG(D_lookup) debug_printf("search_open: %s \"%s\"\n", lk->name,
+  (filename == NULL)? US"NULL" : filename);
+
+/* See if we already have this open for this type of search, and if so,
+pass back the tree block as the handle. The key for the tree node is the search
+type plus '0' concatenated with the file name. There may be entries in the tree
+with closed files if a lot of files have been opened. */
+
+sprintf(CS keybuffer, "%c%.254s", search_type + '0',
+  (filename == NULL)? US"" : filename);
+
+if ((t = tree_search(search_tree, keybuffer)) != NULL)
+  {
+  c = (search_cache *)(t->data.ptr);
+  if (c->handle != NULL)
+    {
+    DEBUG(D_lookup) debug_printf("  cached open\n");
+    store_pool = old_pool;
+    return t;
+    }
+  DEBUG(D_lookup) debug_printf("  cached closed\n");
+  }
+
+/* Otherwise, we need to open the file or database - each search type has its
+own code, which is now split off into separately compiled modules. Before doing
+this, if the search type is one that uses real files, check on the number that
+we are holding open in the cache. If the limit is reached, close the least
+recently used one. */
+
+if (lk->type == lookup_absfile && open_filecount >= lookup_open_max)
+  {
+  if (open_bot == NULL)
+    log_write(0, LOG_MAIN|LOG_PANIC, "too many lookups open, but can't find "
+      "one to close");
+  else
+    {
+    search_cache *c = (search_cache *)(open_bot->data.ptr);
+    DEBUG(D_lookup) debug_printf("Too many lookup files open\n  closing %s\n",
+      open_bot->name);
+    open_bot = c->up;
+    if (open_bot != NULL)
+      ((search_cache *)(open_bot->data.ptr))->down = NULL;
+    else
+      open_top = NULL;
+    ((lookup_list[c->search_type])->close)(c->handle);
+    c->handle = NULL;
+    open_filecount--;
+    }
+  }
+
+/* If opening is successful, call the file-checking function if there is one,
+and if all is still well, enter the open database into the tree. */
+
+handle = (lk->open)(filename, &search_error_message);
+if (handle == NULL)
+  {
+  store_pool = old_pool;
+  return NULL;
+  }
+
+if (lk->check != NULL &&
+   !lk->check(handle, filename, modemask, owners, owngroups,
+     &search_error_message))
+  {
+  lk->close(handle);
+  store_pool = old_pool;
+  return NULL;
+  }
+
+/* If this is a search type that uses real files, keep count. */
+
+if (lk->type == lookup_absfile) open_filecount++;
+
+/* If we found a previously opened entry in the tree, re-use it; otherwise
+insert a new entry. On re-use, leave any cached lookup data and the lookup
+count alone. */
+
+if (t == NULL)
+  {
+  t = store_get(sizeof(tree_node) + Ustrlen(keybuffer));
+  t->data.ptr = c = store_get(sizeof(search_cache));
+  c->item_cache = NULL;
+  Ustrcpy(t->name, keybuffer);
+  tree_insertnode(&search_tree, t);
+  }
+else c = t->data.ptr;
+
+c->handle = handle;
+c->search_type = search_type;
+c->up = c->down = NULL;
+
+store_pool = old_pool;
+return t;
+}
+
+
+
+
+
+/*************************************************
+*  Internal function: Find one item in database  *
+*************************************************/
+
+/* The answer is always put into dynamic store. The last lookup for each handle
+is cached.
+
+Arguments:
+  handle       the handle from search_open; points to tree node
+  filename     the filename that was handed to search_open, or
+               NULL for query-style searches
+  keystring    the keystring for single-key+file lookups, or
+               the querystring for query-style lookups
+
+Returns:       a pointer to a dynamic string containing the answer,
+               or NULL if the query failed or was deferred; in the
+               latter case, search_find_defer is set TRUE; after an unusual
+               failure, there may be a message in search_error_message.
+*/
+
+static uschar *
+internal_search_find(void *handle, uschar *filename, uschar *keystring)
+{
+tree_node *t = (tree_node *)handle;
+search_cache *c = (search_cache *)(t->data.ptr);
+uschar *data = NULL;
+int search_type = t->name[0] - '0';
+int old_pool = store_pool;
+
+/* Lookups that return DEFER may not always set an error message. So that
+the callers don't have to test for NULL, set an empty string. */
+
+search_error_message = US"";
+search_find_defer = FALSE;
+
+DEBUG(D_lookup) debug_printf("internal_search_find: file=\"%s\"\n  "
+  "type=%s key=\"%s\"\n", filename,
+  lookup_list[search_type]->name, keystring);
+
+/* Insurance. If the keystring is empty, just fail. */
+
+if (keystring[0] == 0) return NULL;
+
+/* Use the special store pool for search data */
+
+store_pool = POOL_SEARCH;
+
+/* Look up the data for the key, unless it is already in the cache for this
+file. No need to check c->item_cache for NULL, tree_search will do so. */
+
+if ((t = tree_search(c->item_cache, keystring)) == NULL)
+  {
+  BOOL do_cache = TRUE;
+  int keylength = Ustrlen(keystring);
+
+  DEBUG(D_lookup)
+    {
+    if (filename != NULL)
+      debug_printf("file lookup required for %s\n  in %s\n",
+        keystring, filename);
+    else
+      debug_printf("database lookup required for %s\n", keystring);
+    }
+
+  /* Call the code for the different kinds of search. DEFER is handled
+  like FAIL, except that search_find_defer is set so the caller can
+  distinguish if necessary. */
+
+  if (lookup_list[search_type]->find(c->handle, filename, keystring, keylength,
+      &data, &search_error_message, &do_cache) == DEFER)
+    {
+    search_find_defer = TRUE;
+    }
+
+  /* A record that has been found is now in data, which is either NULL
+  or points to a bit of dynamic store. Cache the result of the lookup if
+  caching is permitted. Lookups can disable caching, when they did something
+  that changes their data. The mysql and pgsql lookups do this when an
+  UPDATE/INSERT query was executed. */
+
+  else if (do_cache)
+    {
+    int len = keylength + 1;
+    t = store_get(sizeof(tree_node) + len);
+    memcpy(t->name, keystring, len);
+    t->data.ptr = data;
+    tree_insertnode(&c->item_cache, t);
+    }
+
+  /* If caching was disabled, empty the cache tree. We just set the cache
+  pointer to NULL here, because we cannot release the store at this stage. */
+
+  else
+    {
+    DEBUG(D_lookup) debug_printf("lookup forced cache cleanup\n");
+    c->item_cache = NULL;
+    }
+  }
+
+/* Data was in the cache already; set the pointer from the tree node */
+
+else
+  {
+  data = US t->data.ptr;
+  DEBUG(D_lookup) debug_printf("cached data used for lookup of %s%s%s\n",
+    keystring,
+    (filename == NULL)? US"" : US"\n  in ",
+    (filename == NULL)? US"" : filename);
+  }
+
+/* Debug: output the answer */
+
+DEBUG(D_lookup)
+  {
+  if (data == NULL)
+    {
+    if (search_find_defer) debug_printf("lookup deferred: %s\n",
+      search_error_message);
+    else debug_printf("lookup failed\n");
+    }
+  else debug_printf("lookup yielded: %s\n", data);
+  }
+
+/* Return it in new dynamic store in the regular pool */
+
+store_pool = old_pool;
+return (data == NULL)? NULL : string_copy(data);
+}
+
+
+
+
+/*************************************************
+* Find one item in database, possibly wildcarded *
+*************************************************/
+
+/* This function calls the internal function above; once only if there
+is no partial matching, but repeatedly when partial matching is requested.
+
+Arguments:
+  handle         the handle from search_open
+  filename       the filename that was handed to search_open, or
+                   NULL for query-style searches
+  keystring      the keystring for single-key+file lookups, or
+                   the querystring for query-style lookups
+  partial        -1 means no partial matching;
+                   otherwise it's the minimum number of components;
+  affix          the affix string for partial matching
+  affixlen       the length of the affix string
+  starflags      SEARCH_STAR and SEARCH_STARAT flags
+  expand_setup   pointer to offset for setting up expansion strings;
+                 don't do any if < 0
+
+Returns:         a pointer to a dynamic string containing the answer,
+                 or NULL if the query failed or was deferred; in the
+                 latter case, search_find_defer is set TRUE
+*/
+
+uschar *
+search_find(void *handle, uschar *filename, uschar *keystring, int partial,
+  uschar *affix, int affixlen, int starflags, int *expand_setup)
+{
+tree_node *t = (tree_node *)handle;
+BOOL set_null_wild = FALSE;
+uschar *yield;
+
+DEBUG(D_lookup)
+  {
+  if (partial < 0) affixlen = 99;   /* So that "NULL" prints */
+  debug_printf("search_find: file=\"%s\"\n  key=\"%s\" "
+    "partial=%d affix=%.*s starflags=%x\n",
+    (filename == NULL)? US"NULL" : filename,
+    keystring, partial, affixlen, affix, starflags);
+  }
+
+/* Arrange to put this database at the top of the LRU chain if it is a type
+that opens real files. */
+
+if (open_top != (tree_node *)handle &&
+    lookup_list[t->name[0]-'0']->type == lookup_absfile)
+  {
+  search_cache *c = (search_cache *)(t->data.ptr);
+  tree_node *up = c->up;
+  tree_node *down = c->down;
+
+  /* Cut it out of the list. A newly opened file will a NULL up pointer.
+  Otherwise there will be a non-NULL up pointer, since we checked above that
+  this block isn't already at the top of the list. */
+
+  if (up != NULL)
+    {
+    ((search_cache *)(up->data.ptr))->down = down;
+    if (down != NULL)
+      ((search_cache *)(down->data.ptr))->up = up;
+    else open_bot = up;
+    }
+
+  /* Now put it at the head of the list. */
+
+  c->up = NULL;
+  c->down = open_top;
+  if (open_top == NULL) open_bot = t; else
+    ((search_cache *)(open_top->data.ptr))->up = t;
+  open_top = t;
+  }
+
+DEBUG(D_lookup)
+  {
+  tree_node *t = open_top;
+  debug_printf("LRU list:\n");
+  while (t != NULL)
+    {
+    search_cache *c = (search_cache *)(t->data.ptr);
+    debug_printf("  %s\n", t->name);
+    if (t == open_bot) debug_printf("  End\n");
+    t = c->down;
+    }
+  }
+
+/* First of all, try to match the key string verbatim. If matched a complete
+entry but could have been partial, flag to set up variables. */
+
+yield = internal_search_find(handle, filename, keystring);
+if (search_find_defer) return NULL;
+if (yield != NULL) { if (partial >= 0) set_null_wild = TRUE; }
+
+/* Not matched a complete entry; handle partial lookups, but only if the full
+search didn't defer. Don't use string_sprintf() to construct the initial key,
+just in case the original key is too long for the string_sprintf() buffer (it
+*has* happened!). The case of a zero-length affix has to be treated specially.
+*/
+
+else if (partial >= 0)
+  {
+  int len = Ustrlen(keystring);
+  uschar *keystring2;
+
+  /* Try with the affix on the front, except for a zero-length affix */
+
+  if (affixlen == 0) keystring2 = keystring; else
+    {
+    keystring2 = store_get(len + affixlen + 1);
+    Ustrncpy(keystring2, affix, affixlen);
+    Ustrcpy(keystring2 + affixlen, keystring);
+    DEBUG(D_lookup) debug_printf("trying partial match %s\n", keystring2);
+    yield = internal_search_find(handle, filename, keystring2);
+    if (search_find_defer) return NULL;
+    }
+
+  /* The key in its entirety did not match a wild entry; try chopping off
+  leading components. */
+
+  if (yield == NULL)
+    {
+    int dotcount = 0;
+    uschar *keystring3 = keystring2 + affixlen;
+    uschar *s = keystring3;
+    while (*s != 0) if (*s++ == '.') dotcount++;
+
+    while (dotcount-- >= partial)
+      {
+      while (*keystring3 != 0 && *keystring3 != '.') keystring3++;
+
+      /* If we get right to the end of the string (which will be the last time
+      through this loop), we've failed if the affix is null. Otherwise do one
+      last lookup for the affix itself, but if it is longer than 1 character,
+      remove the last character if it is ".". */
+
+      if (*keystring3 == 0)
+        {
+        if (affixlen < 1) break;
+        if (affixlen > 1 && affix[affixlen-1] == '.') affixlen--;
+        Ustrncpy(keystring2, affix, affixlen);
+        keystring2[affixlen] = 0;
+        keystring3 = keystring2;
+        }
+      else
+        {
+        keystring3 -= affixlen - 1;
+        if (affixlen > 0) Ustrncpy(keystring3, affix, affixlen);
+        }
+
+      DEBUG(D_lookup) debug_printf("trying partial match %s\n", keystring3);
+      yield = internal_search_find(handle, filename, keystring3);
+      if (search_find_defer) return NULL;
+      if (yield != NULL)
+        {
+        /* First variable is the wild part; second is the fixed part. Take care
+        to get it right when keystring3 is just "*". */
+
+        if (expand_setup != NULL && *expand_setup >= 0)
+          {
+          int fixedlength = Ustrlen(keystring3) - affixlen;
+          int wildlength = Ustrlen(keystring) - fixedlength - 1;
+          *expand_setup += 1;
+          expand_nstring[*expand_setup] = keystring;
+          expand_nlength[*expand_setup] = wildlength;
+          *expand_setup += 1;
+          expand_nstring[*expand_setup] = keystring + wildlength + 1;
+          expand_nlength[*expand_setup] = (fixedlength < 0)? 0 : fixedlength;
+          }
+        break;
+        }
+      keystring3 += affixlen;
+      }
+    }
+
+  else set_null_wild = TRUE; /* Matched a wild entry without any wild part */
+  }
+
+/* If nothing has been matched, but the option to look for "*@" is set, try
+replacing everthing to the left of @ by *. After a match, the wild part
+is set to the string to the left of the @. */
+
+if (yield == NULL && (starflags & SEARCH_STARAT) != 0)
+  {
+  uschar *atat = Ustrrchr(keystring, '@');
+  if (atat != NULL && atat > keystring)
+    {
+    int savechar;
+    savechar = *(--atat);
+    *atat = '*';
+
+    DEBUG(D_lookup) debug_printf("trying default match %s\n", atat);
+    yield = internal_search_find(handle, filename, atat);
+    *atat = savechar;
+    if (search_find_defer) return NULL;
+
+    if (yield != NULL && expand_setup != NULL && *expand_setup >= 0)
+      {
+      *expand_setup += 1;
+      expand_nstring[*expand_setup] = keystring;
+      expand_nlength[*expand_setup] = atat - keystring + 1;
+      *expand_setup += 1;
+      expand_nstring[*expand_setup] = keystring;
+      expand_nlength[*expand_setup] = 0;
+      }
+    }
+  }
+
+/* If we still haven't matched anything, and the option to look for "*" is set,
+try that. If we do match, the first variable (the wild part) is the whole key,
+and the second is empty. */
+
+if (yield == NULL && (starflags & (SEARCH_STAR|SEARCH_STARAT)) != 0)
+  {
+  DEBUG(D_lookup) debug_printf("trying to match *\n");
+  yield = internal_search_find(handle, filename, US"*");
+  if (yield != NULL && expand_setup != NULL && *expand_setup >= 0)
+    {
+    *expand_setup += 1;
+    expand_nstring[*expand_setup] = keystring;
+    expand_nlength[*expand_setup] = Ustrlen(keystring);
+    *expand_setup += 1;
+    expand_nstring[*expand_setup] = keystring;
+    expand_nlength[*expand_setup] = 0;
+    }
+  }
+
+/* If this was a potentially partial lookup, and we matched either a
+complete non-wild domain entry, or we matched a wild-carded entry without
+chopping off any of the domain components, set up the expansion variables
+(if required) so that the first one is empty, and the second one is the
+fixed part of the domain. The set_null_wild flag is set only when yield is not
+NULL. */
+
+if (set_null_wild && expand_setup != NULL && *expand_setup >= 0)
+  {
+  *expand_setup += 1;
+  expand_nstring[*expand_setup] = keystring;
+  expand_nlength[*expand_setup] = 0;
+  *expand_setup += 1;
+  expand_nstring[*expand_setup] = keystring;
+  expand_nlength[*expand_setup] = Ustrlen(keystring);
+  }
+
+return yield;
+}
+
+/* End of search.c */
diff --git a/src/sieve.c b/src/sieve.c
new file mode 100644 (file)
index 0000000..1303646
--- /dev/null
@@ -0,0 +1,3678 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) Michael Haardt 2003-2008 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* This code was contributed by Michael Haardt. */
+
+
+/* Sieve mail filter. */
+
+#include <ctype.h>
+#include <errno.h>
+#include <limits.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "exim.h"
+
+#if HAVE_ICONV
+#include <iconv.h>
+#endif
+
+/* Define this for RFC compliant \r\n end-of-line terminators.      */
+/* Undefine it for UNIX-style \n end-of-line terminators (default). */
+#undef RFC_EOL
+
+/* Define this for development of the Sieve extension "encoded-character". */
+#define ENCODED_CHARACTER
+
+/* Define this for development of the Sieve extension "envelope-auth". */
+#undef ENVELOPE_AUTH
+
+/* Define this for development of the Sieve extension "enotify".    */
+#define ENOTIFY
+
+/* Define this for the Sieve extension "subaddress".                */
+#define SUBADDRESS
+
+/* Define this for the Sieve extension "vacation".                  */
+#define VACATION
+
+/* Must be >= 1                                                     */
+#define VACATION_MIN_DAYS 1
+/* Must be >= VACATION_MIN_DAYS, must be > 7, should be > 30        */
+#define VACATION_MAX_DAYS 31
+
+/* Keep this at 75 to accept only RFC compliant MIME words.         */
+/* Increase it if you want to match headers from buggy MUAs.        */
+#define MIMEWORD_LENGTH 75
+
+struct Sieve
+  {
+  uschar *filter;
+  const uschar *pc;
+  int line;
+  const uschar *errmsg;
+  int keep;
+  int require_envelope;
+  int require_fileinto;
+#ifdef ENCODED_CHARACTER
+  int require_encoded_character;
+#endif
+#ifdef ENVELOPE_AUTH
+  int require_envelope_auth;
+#endif
+#ifdef ENOTIFY
+  int require_enotify;
+  struct Notification *notified;
+#endif
+  uschar *enotify_mailto_owner;
+#ifdef SUBADDRESS
+  int require_subaddress;
+#endif
+#ifdef VACATION
+  int require_vacation;
+  int vacation_ran;
+#endif
+  uschar *vacation_directory;
+  const uschar *subaddress;
+  const uschar *useraddress;
+  int require_copy;
+  int require_iascii_numeric;
+  };
+
+enum Comparator { COMP_OCTET, COMP_EN_ASCII_CASEMAP, COMP_ASCII_NUMERIC };
+enum MatchType { MATCH_IS, MATCH_CONTAINS, MATCH_MATCHES };
+#ifdef SUBADDRESS
+enum AddressPart { ADDRPART_USER, ADDRPART_DETAIL, ADDRPART_LOCALPART, ADDRPART_DOMAIN, ADDRPART_ALL };
+#else
+enum AddressPart { ADDRPART_LOCALPART, ADDRPART_DOMAIN, ADDRPART_ALL };
+#endif
+enum RelOp { LT, LE, EQ, GE, GT, NE };
+
+struct String
+  {
+  uschar *character;
+  int length;
+  };
+
+struct Notification
+  {
+  struct String method;
+  struct String importance;
+  struct String message;
+  struct Notification *next;
+  };
+
+/* This should be a complete list of supported extensions, so that an external
+ManageSieve (RFC 5804) program can interrogate the current Exim binary for the
+list of extensions and provide correct information to a client.
+
+We'll emit the list in the order given here; keep it alphabetically sorted, so
+that callers don't get surprised.
+
+List *MUST* end with a NULL.  Which at least makes ifdef-vs-comma easier. */
+
+const uschar *exim_sieve_extension_list[] = {
+  CUS"comparator-i;ascii-numeric",
+  CUS"copy",
+#ifdef ENCODED_CHARACTER
+  CUS"encoded-character",
+#endif
+#ifdef ENOTIFY
+  CUS"enotify",
+#endif
+  CUS"envelope",
+#ifdef ENVELOPE_AUTH
+  CUS"envelope-auth",
+#endif
+  CUS"fileinto",
+#ifdef SUBADDRESS
+  CUS"subaddress",
+#endif
+#ifdef VACATION
+  CUS"vacation",
+#endif
+  NULL
+};
+
+static int eq_asciicase(const struct String *needle, const struct String *haystack, int match_prefix);
+static int parse_test(struct Sieve *filter, int *cond, int exec);
+static int parse_commands(struct Sieve *filter, int exec, address_item **generated);
+
+static uschar str_from_c[]="From";
+static const struct String str_from={ str_from_c, 4 };
+static uschar str_to_c[]="To";
+static const struct String str_to={ str_to_c, 2 };
+static uschar str_cc_c[]="Cc";
+static const struct String str_cc={ str_cc_c, 2 };
+static uschar str_bcc_c[]="Bcc";
+static const struct String str_bcc={ str_bcc_c, 3 };
+static uschar str_auth_c[]="auth";
+static const struct String str_auth={ str_auth_c, 4 };
+static uschar str_sender_c[]="Sender";
+static const struct String str_sender={ str_sender_c, 6 };
+static uschar str_resent_from_c[]="Resent-From";
+static const struct String str_resent_from={ str_resent_from_c, 11 };
+static uschar str_resent_to_c[]="Resent-To";
+static const struct String str_resent_to={ str_resent_to_c, 9 };
+static uschar str_fileinto_c[]="fileinto";
+static const struct String str_fileinto={ str_fileinto_c, 8 };
+static uschar str_envelope_c[]="envelope";
+static const struct String str_envelope={ str_envelope_c, 8 };
+#ifdef ENCODED_CHARACTER
+static uschar str_encoded_character_c[]="encoded-character";
+static const struct String str_encoded_character={ str_encoded_character_c, 17 };
+#endif
+#ifdef ENVELOPE_AUTH
+static uschar str_envelope_auth_c[]="envelope-auth";
+static const struct String str_envelope_auth={ str_envelope_auth_c, 13 };
+#endif
+#ifdef ENOTIFY
+static uschar str_enotify_c[]="enotify";
+static const struct String str_enotify={ str_enotify_c, 7 };
+static uschar str_online_c[]="online";
+static const struct String str_online={ str_online_c, 6 };
+static uschar str_maybe_c[]="maybe";
+static const struct String str_maybe={ str_maybe_c, 5 };
+static uschar str_auto_submitted_c[]="Auto-Submitted";
+static const struct String str_auto_submitted={ str_auto_submitted_c, 14 };
+#endif
+#ifdef SUBADDRESS
+static uschar str_subaddress_c[]="subaddress";
+static const struct String str_subaddress={ str_subaddress_c, 10 };
+#endif
+#ifdef VACATION
+static uschar str_vacation_c[]="vacation";
+static const struct String str_vacation={ str_vacation_c, 8 };
+static uschar str_subject_c[]="Subject";
+static const struct String str_subject={ str_subject_c, 7 };
+#endif
+static uschar str_copy_c[]="copy";
+static const struct String str_copy={ str_copy_c, 4 };
+static uschar str_iascii_casemap_c[]="i;ascii-casemap";
+static const struct String str_iascii_casemap={ str_iascii_casemap_c, 15 };
+static uschar str_enascii_casemap_c[]="en;ascii-casemap";
+static const struct String str_enascii_casemap={ str_enascii_casemap_c, 16 };
+static uschar str_ioctet_c[]="i;octet";
+static const struct String str_ioctet={ str_ioctet_c, 7 };
+static uschar str_iascii_numeric_c[]="i;ascii-numeric";
+static const struct String str_iascii_numeric={ str_iascii_numeric_c, 15 };
+static uschar str_comparator_iascii_casemap_c[]="comparator-i;ascii-casemap";
+static const struct String str_comparator_iascii_casemap={ str_comparator_iascii_casemap_c, 26 };
+static uschar str_comparator_enascii_casemap_c[]="comparator-en;ascii-casemap";
+static const struct String str_comparator_enascii_casemap={ str_comparator_enascii_casemap_c, 27 };
+static uschar str_comparator_ioctet_c[]="comparator-i;octet";
+static const struct String str_comparator_ioctet={ str_comparator_ioctet_c, 18 };
+static uschar str_comparator_iascii_numeric_c[]="comparator-i;ascii-numeric";
+static const struct String str_comparator_iascii_numeric={ str_comparator_iascii_numeric_c, 26 };
+
+
+/*************************************************
+*          Encode to quoted-printable            *
+*************************************************/
+
+/*
+Arguments:
+  src               UTF-8 string
+  dst               US-ASCII string
+
+Returns
+  dst
+*/
+
+static struct String *quoted_printable_encode(const struct String *src, struct String *dst)
+{
+int pass;
+const uschar *start,*end;
+uschar *new = NULL;
+uschar ch;
+size_t line;
+
+for (pass=0; pass<=1; ++pass)
+  {
+  line=0;
+  if (pass==0)
+    dst->length=0;
+  else
+    {
+    dst->character=store_get(dst->length+1); /* plus one for \0 */
+    new=dst->character;
+    }
+  for (start=src->character,end=start+src->length; start<end; ++start)
+    {
+    ch=*start;
+    if (line>=73)
+      {
+      if (pass==0)
+        dst->length+=2;
+      else
+        {
+        *new++='=';
+        *new++='\n';
+        }
+      line=0;
+      }
+    if
+      (
+      (ch>=33 && ch<=60)
+      || (ch>=62 && ch<=126)
+      ||
+        (
+        (ch==9 || ch==32)
+        && start+2<end
+        && (*(start+1)!='\r' || *(start+2)!='\n')
+        )
+      )
+      {
+      if (pass==0)
+        ++dst->length;
+      else
+        *new++=*start;
+      ++line;
+      }
+    else if (ch=='\r' && start+1<end && *(start+1)=='\n')
+      {
+      if (pass==0)
+        {
+        ++dst->length;
+        line=0;
+        }
+      else
+        *new++='\n';
+        line=0;
+      ++start;
+      }
+    else
+      {
+      if (pass==0)
+        dst->length+=3;
+      else
+        {
+        sprintf(CS new,"=%02X",ch);
+        new+=3;
+        }
+      line+=3;
+      }
+    }
+  }
+  *new='\0'; /* not included in length, but nice */
+  return dst;
+}
+
+
+/*************************************************
+*     Check mail address for correct syntax      *
+*************************************************/
+
+/*
+Check mail address for being syntactically correct.
+
+Arguments:
+  filter      points to the Sieve filter including its state
+  address     String containing one address
+
+Returns
+  1           Mail address is syntactically OK
+ -1           syntax error
+*/
+
+int check_mail_address(struct Sieve *filter, const struct String *address)
+{
+int start, end, domain;
+uschar *error,*ss;
+
+if (address->length>0)
+  {
+  ss = parse_extract_address(address->character, &error, &start, &end, &domain,
+    FALSE);
+  if (ss == NULL)
+    {
+    filter->errmsg=string_sprintf("malformed address \"%s\" (%s)",
+      address->character, error);
+    return -1;
+    }
+  else
+    return 1;
+  }
+else
+  {
+  filter->errmsg=CUS "empty address";
+  return -1;
+  }
+}
+
+
+/*************************************************
+*          Decode URI encoded string             *
+*************************************************/
+
+/*
+Arguments:
+  str               URI encoded string
+
+Returns
+  0                 Decoding successful
+ -1                 Encoding error
+*/
+
+#ifdef ENOTIFY
+static int uri_decode(struct String *str)
+{
+uschar *s,*t,*e;
+
+if (str->length==0) return 0;
+for (s=str->character,t=s,e=s+str->length; s<e; )
+  {
+  if (*s=='%')
+    {
+    if (s+2<e && isxdigit(*(s+1)) && isxdigit(*(s+2)))
+      {
+      *t++=((isdigit(*(s+1)) ? *(s+1)-'0' : tolower(*(s+1))-'a'+10)<<4)
+           | (isdigit(*(s+2)) ? *(s+2)-'0' : tolower(*(s+2))-'a'+10);
+      s+=3;
+      }
+    else return -1;
+    }
+  else
+    *t++=*s++;
+  }
+*t='\0';
+str->length=t-str->character;
+return 0;
+}
+
+
+/*************************************************
+*               Parse mailto URI                 *
+*************************************************/
+
+/*
+Parse mailto-URI.
+
+       mailtoURI   = "mailto:" [ to ] [ headers ]
+       to          = [ addr-spec *("%2C" addr-spec ) ]
+       headers     = "?" header *( "&" header )
+       header      = hname "=" hvalue
+       hname       = *urlc
+       hvalue      = *urlc
+
+Arguments:
+  filter      points to the Sieve filter including its state
+  uri         URI, excluding scheme
+  recipient
+  body
+
+Returns
+  1           URI is syntactically OK
+  0           Unknown URI scheme
+ -1           syntax error
+*/
+
+static int parse_mailto_uri(struct Sieve *filter, const uschar *uri, string_item **recipient, struct String *header, struct String *subject, struct String *body)
+{
+const uschar *start;
+struct String to,hname,hvalue;
+int capacity;
+string_item *new;
+
+if (Ustrncmp(uri,"mailto:",7))
+  {
+  filter->errmsg=US "Unknown URI scheme";
+  return 0;
+  }
+uri+=7;
+if (*uri && *uri!='?')
+  for (;;)
+    {
+    /* match to */
+    for (start=uri; *uri && *uri!='?' && (*uri!='%' || *(uri+1)!='2' || tolower(*(uri+2))!='c'); ++uri);
+    if (uri>start)
+      {
+      capacity=0;
+      to.character=(uschar*)0;
+      to.length=0;
+      to.character=string_cat(to.character,&capacity,&to.length,start,uri-start);
+      to.character[to.length]='\0';
+      if (uri_decode(&to)==-1)
+        {
+        filter->errmsg=US"Invalid URI encoding";
+        return -1;
+        }
+        new=store_get(sizeof(string_item));
+        new->text=store_get(to.length+1);
+        if (to.length) memcpy(new->text,to.character,to.length);
+        new->text[to.length]='\0';
+        new->next=*recipient;
+        *recipient=new;
+      }
+    else
+      {
+      filter->errmsg=US"Missing addr-spec in URI";
+      return -1;
+      }
+    if (*uri=='%') uri+=3;
+    else break;
+    }
+if (*uri=='?')
+  {
+  ++uri;
+  for (;;)
+    {
+    /* match hname */
+    for (start=uri; *uri && (isalnum(*uri) || strchr("$-_.+!*'(),%",*uri)); ++uri);
+    if (uri>start)
+      {
+      capacity=0;
+      hname.character=(uschar*)0;
+      hname.length=0;
+      hname.character=string_cat(hname.character,&capacity,&hname.length,start,uri-start);
+      hname.character[hname.length]='\0';
+      if (uri_decode(&hname)==-1)
+        {
+        filter->errmsg=US"Invalid URI encoding";
+        return -1;
+        }
+      }
+    /* match = */
+    if (*uri=='=')
+      ++uri;
+    else
+      {
+      filter->errmsg=US"Missing equal after hname";
+      return -1;
+      }
+    /* match hvalue */
+    for (start=uri; *uri && (isalnum(*uri) || strchr("$-_.+!*'(),%",*uri)); ++uri);
+    if (uri>start)
+      {
+      capacity=0;
+      hvalue.character=(uschar*)0;
+      hvalue.length=0;
+      hvalue.character=string_cat(hvalue.character,&capacity,&hvalue.length,start,uri-start);
+      hvalue.character[hvalue.length]='\0';
+      if (uri_decode(&hvalue)==-1)
+        {
+        filter->errmsg=US"Invalid URI encoding";
+        return -1;
+        }
+      }
+    if (hname.length==2 && strcmpic(hname.character, US"to")==0)
+      {
+      new=store_get(sizeof(string_item));
+      new->text=store_get(hvalue.length+1);
+      if (hvalue.length) memcpy(new->text,hvalue.character,hvalue.length);
+      new->text[hvalue.length]='\0';
+      new->next=*recipient;
+      *recipient=new;
+      }
+    else if (hname.length==4 && strcmpic(hname.character, US"body")==0)
+      *body=hvalue;
+    else if (hname.length==7 && strcmpic(hname.character, US"subject")==0)
+      *subject=hvalue;
+    else
+      {
+      static struct String ignore[]=
+        {
+        {US"date",4},
+        {US"from",4},
+        {US"message-id",10},
+        {US"received",8},
+        {US"auto-submitted",14}
+        };
+      static struct String *end=ignore+sizeof(ignore)/sizeof(ignore[0]);
+      struct String *i;
+
+      for (i=ignore; i<end && !eq_asciicase(&hname,i,0); ++i);
+      if (i==end)
+        {
+        if (header->length==-1) header->length=0;
+        capacity=header->length;
+        header->character=string_cat(header->character,&capacity,&header->length,hname.character,hname.length);
+        header->character=string_cat(header->character,&capacity,&header->length,CUS ": ",2);
+        header->character=string_cat(header->character,&capacity,&header->length,hvalue.character,hvalue.length);
+        header->character=string_cat(header->character,&capacity,&header->length,CUS "\n",1);
+        header->character[header->length]='\0';
+        }
+      }
+    if (*uri=='&') ++uri;
+    else break;
+    }
+  }
+if (*uri)
+  {
+  filter->errmsg=US"Syntactically invalid URI";
+  return -1;
+  }
+return 1;
+}
+#endif
+
+
+/*************************************************
+*          Octet-wise string comparison          *
+*************************************************/
+
+/*
+Arguments:
+  needle            UTF-8 string to search ...
+  haystack          ... inside the haystack
+  match_prefix      1 to compare if needle is a prefix of haystack
+
+Returns:      0               needle not found in haystack
+              1               needle found
+*/
+
+static int eq_octet(const struct String *needle,
+  const struct String *haystack, int match_prefix)
+{
+size_t nl,hl;
+const uschar *n,*h;
+
+nl=needle->length;
+n=needle->character;
+hl=haystack->length;
+h=haystack->character;
+while (nl>0 && hl>0)
+  {
+#if !HAVE_ICONV
+  if (*n&0x80) return 0;
+  if (*h&0x80) return 0;
+#endif
+  if (*n!=*h) return 0;
+  ++n;
+  ++h;
+  --nl;
+  --hl;
+  }
+return (match_prefix ? nl==0 : nl==0 && hl==0);
+}
+
+
+/*************************************************
+*    ASCII case-insensitive string comparison    *
+*************************************************/
+
+/*
+Arguments:
+  needle            UTF-8 string to search ...
+  haystack          ... inside the haystack
+  match_prefix      1 to compare if needle is a prefix of haystack
+
+Returns:      0               needle not found in haystack
+              1               needle found
+*/
+
+static int eq_asciicase(const struct String *needle,
+  const struct String *haystack, int match_prefix)
+{
+size_t nl,hl;
+const uschar *n,*h;
+uschar nc,hc;
+
+nl=needle->length;
+n=needle->character;
+hl=haystack->length;
+h=haystack->character;
+while (nl>0 && hl>0)
+  {
+  nc=*n;
+  hc=*h;
+#if !HAVE_ICONV
+  if (nc&0x80) return 0;
+  if (hc&0x80) return 0;
+#endif
+  /* tolower depends on the locale and only ASCII case must be insensitive */
+  if ((nc>='A' && nc<='Z' ? nc|0x20 : nc) != (hc>='A' && hc<='Z' ? hc|0x20 : hc)) return 0;
+  ++n;
+  ++h;
+  --nl;
+  --hl;
+  }
+return (match_prefix ? nl==0 : nl==0 && hl==0);
+}
+
+
+/*************************************************
+*              Glob pattern search               *
+*************************************************/
+
+/*
+Arguments:
+  needle          pattern to search ...
+  haystack        ... inside the haystack
+  ascii_caseless  ignore ASCII case
+  match_octet     match octets, not UTF-8 multi-octet characters
+
+Returns:      0               needle not found in haystack
+              1               needle found
+              -1              pattern error
+*/
+
+static int eq_glob(const struct String *needle,
+  const struct String *haystack, int ascii_caseless, int match_octet)
+{
+const uschar *n,*h,*nend,*hend;
+int may_advance=0;
+
+n=needle->character;
+h=haystack->character;
+nend=n+needle->length;
+hend=h+haystack->length;
+while (n<nend)
+  {
+  if (*n=='*')
+    {
+    ++n;
+    may_advance=1;
+    }
+  else
+    {
+    const uschar *npart,*hpart;
+
+    /* Try to match a non-star part of the needle at the current */
+    /* position in the haystack.                                 */
+    match_part:
+    npart=n;
+    hpart=h;
+    while (npart<nend && *npart!='*') switch (*npart)
+      {
+      case '?':
+        {
+        if (hpart==hend) return 0;
+        if (match_octet)
+          ++hpart;
+        else
+          {
+          /* Match one UTF8 encoded character */
+          if ((*hpart&0xc0)==0xc0)
+            {
+            ++hpart;
+            while (hpart<hend && ((*hpart&0xc0)==0x80)) ++hpart;
+            }
+          else
+            ++hpart;
+          }
+        ++npart;
+        break;
+        }
+      case '\\':
+        {
+        ++npart;
+        if (npart==nend) return -1;
+        /* FALLTHROUGH */
+        }
+      default:
+        {
+        if (hpart==hend) return 0;
+        /* tolower depends on the locale, but we need ASCII */
+        if
+          (
+#if !HAVE_ICONV
+          (*hpart&0x80) || (*npart&0x80) ||
+#endif
+          ascii_caseless
+          ? ((*npart>='A' && *npart<='Z' ? *npart|0x20 : *npart) != (*hpart>='A' && *hpart<='Z' ? *hpart|0x20 : *hpart))
+          : *hpart!=*npart
+          )
+          {
+          if (may_advance)
+            /* string match after a star failed, advance and try again */
+            {
+            ++h;
+            goto match_part;
+            }
+          else return 0;
+          }
+        else
+          {
+          ++npart;
+          ++hpart;
+          };
+        }
+      }
+    /* at this point, a part was matched successfully */
+    if (may_advance && npart==nend && hpart<hend)
+      /* needle ends, but haystack does not: if there was a star before, advance and try again */
+      {
+      ++h;
+      goto match_part;
+      }
+    h=hpart;
+    n=npart;
+    may_advance=0;
+    }
+  }
+return (h==hend ? 1 : may_advance);
+}
+
+
+/*************************************************
+*    ASCII numeric comparison                    *
+*************************************************/
+
+/*
+Arguments:
+  a                 first numeric string
+  b                 second numeric string
+  relop             relational operator
+
+Returns:      0               not (a relop b)
+              1               a relop b
+*/
+
+static int eq_asciinumeric(const struct String *a,
+  const struct String *b, enum RelOp relop)
+{
+size_t al,bl;
+const uschar *as,*aend,*bs,*bend;
+int cmp;
+
+as=a->character;
+aend=a->character+a->length;
+bs=b->character;
+bend=b->character+b->length;
+
+while (*as>='0' && *as<='9' && as<aend) ++as;
+al=as-a->character;
+while (*bs>='0' && *bs<='9' && bs<bend) ++bs;
+bl=bs-b->character;
+
+if (al && bl==0) cmp=-1;
+else if (al==0 && bl==0) cmp=0;
+else if (al==0 && bl) cmp=1;
+else
+  {
+  cmp=al-bl;
+  if (cmp==0) cmp=memcmp(a->character,b->character,al);
+  }
+switch (relop)
+  {
+  case LT: return cmp<0;
+  case LE: return cmp<=0;
+  case EQ: return cmp==0;
+  case GE: return cmp>=0;
+  case GT: return cmp>0;
+  case NE: return cmp!=0;
+  }
+  /*NOTREACHED*/
+  return -1;
+}
+
+
+/*************************************************
+*             Compare strings                    *
+*************************************************/
+
+/*
+Arguments:
+  filter      points to the Sieve filter including its state
+  needle      UTF-8 pattern or string to search ...
+  haystack    ... inside the haystack
+  co          comparator to use
+  mt          match type to use
+
+Returns:      0               needle not found in haystack
+              1               needle found
+              -1              comparator does not offer matchtype
+*/
+
+static int compare(struct Sieve *filter, const struct String *needle, const struct String *haystack,
+  enum Comparator co, enum MatchType mt)
+{
+int r=0;
+
+if ((filter_test != FTEST_NONE && debug_selector != 0) ||
+  (debug_selector & D_filter) != 0)
+  {
+  debug_printf("String comparison (match ");
+  switch (mt)
+    {
+    case MATCH_IS: debug_printf(":is"); break;
+    case MATCH_CONTAINS: debug_printf(":contains"); break;
+    case MATCH_MATCHES: debug_printf(":matches"); break;
+    }
+  debug_printf(", comparison \"");
+  switch (co)
+    {
+    case COMP_OCTET: debug_printf("i;octet"); break;
+    case COMP_EN_ASCII_CASEMAP: debug_printf("en;ascii-casemap"); break;
+    case COMP_ASCII_NUMERIC: debug_printf("i;ascii-numeric"); break;
+    }
+  debug_printf("\"):\n");
+  debug_printf("  Search = %s (%d chars)\n", needle->character,needle->length);
+  debug_printf("  Inside = %s (%d chars)\n", haystack->character,haystack->length);
+  }
+switch (mt)
+  {
+  case MATCH_IS:
+    {
+    switch (co)
+      {
+      case COMP_OCTET:
+        {
+        if (eq_octet(needle,haystack,0)) r=1;
+        break;
+        }
+      case COMP_EN_ASCII_CASEMAP:
+        {
+        if (eq_asciicase(needle,haystack,0)) r=1;
+        break;
+        }
+      case COMP_ASCII_NUMERIC:
+        {
+        if (!filter->require_iascii_numeric)
+          {
+          filter->errmsg=CUS "missing previous require \"comparator-i;ascii-numeric\";";
+          return -1;
+          }
+        if (eq_asciinumeric(needle,haystack,EQ)) r=1;
+        break;
+        }
+      }
+    break;
+    }
+  case MATCH_CONTAINS:
+    {
+    struct String h;
+
+    switch (co)
+      {
+      case COMP_OCTET:
+        {
+        for (h=*haystack; h.length; ++h.character,--h.length) if (eq_octet(needle,&h,1)) { r=1; break; }
+        break;
+        }
+      case COMP_EN_ASCII_CASEMAP:
+        {
+        for (h=*haystack; h.length; ++h.character,--h.length) if (eq_asciicase(needle,&h,1)) { r=1; break; }
+        break;
+        }
+      default:
+        {
+        filter->errmsg=CUS "comparator does not offer specified matchtype";
+        return -1;
+        }
+      }
+    break;
+    }
+  case MATCH_MATCHES:
+    {
+    switch (co)
+      {
+      case COMP_OCTET:
+        {
+        if ((r=eq_glob(needle,haystack,0,1))==-1)
+          {
+          filter->errmsg=CUS "syntactically invalid pattern";
+          return -1;
+          }
+        break;
+        }
+      case COMP_EN_ASCII_CASEMAP:
+        {
+        if ((r=eq_glob(needle,haystack,1,1))==-1)
+          {
+          filter->errmsg=CUS "syntactically invalid pattern";
+          return -1;
+          }
+        break;
+        }
+      default:
+        {
+        filter->errmsg=CUS "comparator does not offer specified matchtype";
+        return -1;
+        }
+      }
+    break;
+    }
+  }
+if ((filter_test != FTEST_NONE && debug_selector != 0) ||
+  (debug_selector & D_filter) != 0)
+  debug_printf("  Result %s\n",r?"true":"false");
+return r;
+}
+
+
+/*************************************************
+*         Check header field syntax              *
+*************************************************/
+
+/*
+RFC 2822, section 3.6.8 says:
+
+  field-name      =       1*ftext
+
+  ftext           =       %d33-57 /               ; Any character except
+                          %d59-126                ;  controls, SP, and
+                                                  ;  ":".
+
+That forbids 8-bit header fields.  This implementation accepts them, since
+all of Exim is 8-bit clean, so it adds %d128-%d255.
+
+Arguments:
+  header      header field to quote for suitable use in Exim expansions
+
+Returns:      0               string is not a valid header field
+              1               string is a value header field
+*/
+
+static int is_header(const struct String *header)
+{
+size_t l;
+const uschar *h;
+
+l=header->length;
+h=header->character;
+if (l==0) return 0;
+while (l)
+  {
+  if (((unsigned char)*h)<33 || ((unsigned char)*h)==':' || ((unsigned char)*h)==127) return 0;
+  else
+    {
+    ++h;
+    --l;
+    }
+  }
+return 1;
+}
+
+
+/*************************************************
+*       Quote special characters string          *
+*************************************************/
+
+/*
+Arguments:
+  header      header field to quote for suitable use in Exim expansions
+              or as debug output
+
+Returns:      quoted string
+*/
+
+static const uschar *quote(const struct String *header)
+{
+uschar *quoted=NULL;
+int size=0,ptr=0;
+size_t l;
+const uschar *h;
+
+l=header->length;
+h=header->character;
+while (l)
+  {
+  switch (*h)
+    {
+    case '\0':
+      {
+      quoted=string_cat(quoted,&size,&ptr,CUS "\\0",2);
+      break;
+      }
+    case '$':
+    case '{':
+    case '}':
+      {
+      quoted=string_cat(quoted,&size,&ptr,CUS "\\",1);
+      }
+    default:
+      {
+      quoted=string_cat(quoted,&size,&ptr,h,1);
+      }
+    }
+  ++h;
+  --l;
+  }
+quoted=string_cat(quoted,&size,&ptr,CUS "",1);
+return quoted;
+}
+
+
+/*************************************************
+*   Add address to list of generated addresses   *
+*************************************************/
+
+/*
+According to RFC 5228, duplicate delivery to the same address must
+not happen, so the list is first searched for the address.
+
+Arguments:
+  generated   list of generated addresses
+  addr        new address to add
+  file        address denotes a file
+
+Returns:      nothing
+*/
+
+static void add_addr(address_item **generated, uschar *addr, int file, int maxage, int maxmessages, int maxstorage)
+{
+address_item *new_addr;
+
+for (new_addr=*generated; new_addr; new_addr=new_addr->next)
+  {
+  if (Ustrcmp(new_addr->address,addr)==0 && (file ? testflag(new_addr, af_pfr|af_file) : 1))
+    {
+    if ((filter_test != FTEST_NONE && debug_selector != 0) || (debug_selector & D_filter) != 0)
+      {
+      debug_printf("Repeated %s `%s' ignored.\n",file ? "fileinto" : "redirect", addr);
+      }
+    return;
+    }
+  }
+
+if ((filter_test != FTEST_NONE && debug_selector != 0) || (debug_selector & D_filter) != 0)
+  {
+  debug_printf("%s `%s'\n",file ? "fileinto" : "redirect", addr);
+  }
+new_addr=deliver_make_addr(addr,TRUE);
+if (file)
+  {
+  setflag(new_addr, af_pfr|af_file);
+  new_addr->mode = 0;
+  }
+new_addr->p.errors_address = NULL;
+new_addr->next = *generated;
+*generated = new_addr;
+}
+
+
+/*************************************************
+*         Return decoded header field            *
+*************************************************/
+
+/*
+Unfold the header field as described in RFC 2822 and remove all
+leading and trailing white space, then perform MIME decoding and
+translate the header field to UTF-8.
+
+Arguments:
+  value       returned value of the field
+  header      name of the header field
+
+Returns:      nothing          The expanded string is empty
+                               in case there is no such header
+*/
+
+static void expand_header(struct String *value, const struct String *header)
+{
+uschar *s,*r,*t;
+uschar *errmsg;
+
+value->length=0;
+value->character=(uschar*)0;
+
+t=r=s=expand_string(string_sprintf("$rheader_%s",quote(header)));
+while (*r==' ' || *r=='\t') ++r;
+while (*r)
+  {
+  if (*r=='\n')
+    ++r;
+  else
+    *t++=*r++;
+  }
+while (t>s && (*(t-1)==' ' || *(t-1)=='\t')) --t;
+*t='\0';
+value->character=rfc2047_decode(s,check_rfc2047_length,US"utf-8",'\0',&value->length,&errmsg);
+}
+
+
+/*************************************************
+*        Parse remaining hash comment            *
+*************************************************/
+
+/*
+Token definition:
+  Comment up to terminating CRLF
+
+Arguments:
+  filter      points to the Sieve filter including its state
+
+Returns:      1                success
+              -1               syntax error
+*/
+
+static int parse_hashcomment(struct Sieve *filter)
+{
+++filter->pc;
+while (*filter->pc)
+  {
+#ifdef RFC_EOL
+  if (*filter->pc=='\r' && *(filter->pc+1)=='\n')
+#else
+  if (*filter->pc=='\n')
+#endif
+    {
+#ifdef RFC_EOL
+    filter->pc+=2;
+#else
+    ++filter->pc;
+#endif
+    ++filter->line;
+    return 1;
+    }
+  else ++filter->pc;
+  }
+filter->errmsg=CUS "missing end of comment";
+return -1;
+}
+
+
+/*************************************************
+*       Parse remaining C-style comment          *
+*************************************************/
+
+/*
+Token definition:
+  Everything up to star slash
+
+Arguments:
+  filter      points to the Sieve filter including its state
+
+Returns:      1                success
+              -1               syntax error
+*/
+
+static int parse_comment(struct Sieve *filter)
+{
+  filter->pc+=2;
+  while (*filter->pc)
+  {
+    if (*filter->pc=='*' && *(filter->pc+1)=='/')
+    {
+      filter->pc+=2;
+      return 1;
+    }
+    else ++filter->pc;
+  }
+  filter->errmsg=CUS "missing end of comment";
+  return -1;
+}
+
+
+/*************************************************
+*         Parse optional white space             *
+*************************************************/
+
+/*
+Token definition:
+  Spaces, tabs, CRLFs, hash comments or C-style comments
+
+Arguments:
+  filter      points to the Sieve filter including its state
+
+Returns:      1                success
+              -1               syntax error
+*/
+
+static int parse_white(struct Sieve *filter)
+{
+while (*filter->pc)
+  {
+  if (*filter->pc==' ' || *filter->pc=='\t') ++filter->pc;
+#ifdef RFC_EOL
+  else if (*filter->pc=='\r' && *(filter->pc+1)=='\n')
+#else
+  else if (*filter->pc=='\n')
+#endif
+    {
+#ifdef RFC_EOL
+    filter->pc+=2;
+#else
+    ++filter->pc;
+#endif
+    ++filter->line;
+    }
+  else if (*filter->pc=='#')
+    {
+    if (parse_hashcomment(filter)==-1) return -1;
+    }
+  else if (*filter->pc=='/' && *(filter->pc+1)=='*')
+    {
+    if (parse_comment(filter)==-1) return -1;
+    }
+  else break;
+  }
+return 1;
+}
+
+
+#ifdef ENCODED_CHARACTER
+/*************************************************
+*      Decode hex-encoded-character string       *
+*************************************************/
+
+/*
+Encoding definition:
+   blank                = SP / TAB / CRLF
+   hex-pair-seq         = *blank hex-pair *(1*blank hex-pair) *blank
+   hex-pair             = 1*2HEXDIG
+
+Arguments:
+  src         points to a hex-pair-seq
+  end         points to its end
+  dst         points to the destination of the decoded octets,
+              optionally to (uschar*)0 for checking only
+
+Returns:      >=0              number of decoded octets
+              -1               syntax error
+*/
+
+static int hex_decode(uschar *src, uschar *end, uschar *dst)
+{
+int decoded=0;
+
+while (*src==' ' || *src=='\t' || *src=='\n') ++src;
+do
+  {
+  int x,d,n;
+
+  for (x=0,d=0; d<2 && src<end && isxdigit(n=tolower(*src)); x=(x<<4)|(n>='0' && n<='9' ? n-'0' : 10+(n-'a')),++d,++src);
+  if (d==0) return -1;
+  if (dst) *dst++=x;
+  ++decoded;
+  if (src==end) return decoded;
+  if (*src==' ' || *src=='\t' || *src=='\n')
+    while (*src==' ' || *src=='\t' || *src=='\n') ++src;
+  else
+    return -1;
+  }
+while (src<end);
+return decoded;
+}
+
+
+/*************************************************
+*    Decode unicode-encoded-character string     *
+*************************************************/
+
+/*
+Encoding definition:
+   blank                = SP / TAB / CRLF
+   unicode-hex-seq      = *blank unicode-hex *(blank unicode-hex) *blank
+   unicode-hex          = 1*HEXDIG
+
+   It is an error for a script to use a hexadecimal value that isn't in
+   either the range 0 to D7FF or the range E000 to 10FFFF.
+
+   At this time, strings are already scanned, thus the CRLF is converted
+   to the internally used \n (should RFC_EOL have been used).
+
+Arguments:
+  src         points to a unicode-hex-seq
+  end         points to its end
+  dst         points to the destination of the decoded octets,
+              optionally to (uschar*)0 for checking only
+
+Returns:      >=0              number of decoded octets
+              -1               syntax error
+              -2               semantic error (character range violation)
+*/
+
+static int unicode_decode(uschar *src, uschar *end, uschar *dst)
+{
+int decoded=0;
+
+while (*src==' ' || *src=='\t' || *src=='\n') ++src;
+do
+  {
+  uschar *hex_seq;
+  int c,d,n;
+
+  unicode_hex:
+  for (hex_seq=src; src<end && *src=='0'; ++src);
+  for (c=0,d=0; d<7 && src<end && isxdigit(n=tolower(*src)); c=(c<<4)|(n>='0' && n<='9' ? n-'0' : 10+(n-'a')),++d,++src);
+  if (src==hex_seq) return -1;
+  if (d==7 || (!((c>=0 && c<=0xd7ff) || (c>=0xe000 && c<=0x10ffff)))) return -2;
+  if (c<128)
+    {
+    if (dst) *dst++=c;
+    ++decoded;
+    }
+  else if (c>=0x80 && c<=0x7ff)
+    {
+      if (dst)
+        {
+        *dst++=192+(c>>6);
+        *dst++=128+(c&0x3f);
+        }
+      decoded+=2;
+    }
+  else if (c>=0x800 && c<=0xffff)
+    {
+      if (dst)
+        {
+        *dst++=224+(c>>12);
+        *dst++=128+((c>>6)&0x3f);
+        *dst++=128+(c&0x3f);
+        }
+      decoded+=3;
+    }
+  else if (c>=0x10000 && c<=0x1fffff)
+    {
+      if (dst)
+        {
+        *dst++=240+(c>>18);
+        *dst++=128+((c>>10)&0x3f);
+        *dst++=128+((c>>6)&0x3f);
+        *dst++=128+(c&0x3f);
+        }
+      decoded+=4;
+    }
+  if (*src==' ' || *src=='\t' || *src=='\n')
+    {
+    while (*src==' ' || *src=='\t' || *src=='\n') ++src;
+    if (src==end) return decoded;
+    goto unicode_hex;
+    }
+  }
+while (src<end);
+return decoded;
+}
+
+
+/*************************************************
+*       Decode encoded-character string          *
+*************************************************/
+
+/*
+Encoding definition:
+   encoded-arb-octets   = "${hex:" hex-pair-seq "}"
+   encoded-unicode-char = "${unicode:" unicode-hex-seq "}"
+
+Arguments:
+  encoded     points to an encoded string, returns decoded string
+  filter      points to the Sieve filter including its state
+
+Returns:      1                success
+              -1               syntax error
+*/
+
+static int string_decode(struct Sieve *filter, struct String *data)
+{
+uschar *src,*dst,*end;
+
+src=data->character;
+dst=src;
+end=data->character+data->length;
+while (src<end)
+  {
+  uschar *brace;
+
+  if (
+      strncmpic(src,US "${hex:",6)==0
+      && (brace=Ustrchr(src+6,'}'))!=(uschar*)0
+      && (hex_decode(src+6,brace,(uschar*)0))>=0
+     )
+    {
+    dst+=hex_decode(src+6,brace,dst);
+    src=brace+1;
+    }
+  else if (
+           strncmpic(src,US "${unicode:",10)==0
+           && (brace=Ustrchr(src+10,'}'))!=(uschar*)0
+          )
+    {
+    switch (unicode_decode(src+10,brace,(uschar*)0))
+      {
+      case -2:
+        {
+        filter->errmsg=CUS "unicode character out of range";
+        return -1;
+        }
+      case -1:
+        {
+        *dst++=*src++;
+        break;
+        }
+      default:
+        {
+        dst+=unicode_decode(src+10,brace,dst);
+        src=brace+1;
+        }
+      }
+    }
+  else *dst++=*src++;
+  }
+  data->length=dst-data->character;
+  *dst='\0';
+return 1;
+}
+#endif
+
+
+/*************************************************
+*          Parse an optional string              *
+*************************************************/
+
+/*
+Token definition:
+   quoted-string = DQUOTE *CHAR DQUOTE
+           ;; in general, \ CHAR inside a string maps to CHAR
+           ;; so \" maps to " and \\ maps to \
+           ;; note that newlines and other characters are all allowed
+           ;; in strings
+
+   multi-line          = "text:" *(SP / HTAB) (hash-comment / CRLF)
+                         *(multi-line-literal / multi-line-dotstuff)
+                         "." CRLF
+   multi-line-literal  = [CHAR-NOT-DOT *CHAR-NOT-CRLF] CRLF
+   multi-line-dotstuff = "." 1*CHAR-NOT-CRLF CRLF
+           ;; A line containing only "." ends the multi-line.
+           ;; Remove a leading '.' if followed by another '.'.
+  string           = quoted-string / multi-line
+
+Arguments:
+  filter      points to the Sieve filter including its state
+  id          specifies identifier to match
+
+Returns:      1                success
+              -1               syntax error
+              0                identifier not matched
+*/
+
+static int parse_string(struct Sieve *filter, struct String *data)
+{
+int dataCapacity=0;
+
+data->length=0;
+data->character=(uschar*)0;
+if (*filter->pc=='"') /* quoted string */
+  {
+  ++filter->pc;
+  while (*filter->pc)
+    {
+    if (*filter->pc=='"') /* end of string */
+      {
+      int foo=data->length;
+
+      ++filter->pc;
+      /* that way, there will be at least one character allocated */
+      data->character=string_cat(data->character,&dataCapacity,&foo,CUS "",1);
+#ifdef ENCODED_CHARACTER
+      if (filter->require_encoded_character
+          && string_decode(filter,data)==-1)
+        return -1;
+#endif
+      return 1;
+      }
+    else if (*filter->pc=='\\' && *(filter->pc+1)) /* quoted character */
+      {
+      data->character=string_cat(data->character,&dataCapacity,&data->length,filter->pc+1,1);
+      filter->pc+=2;
+      }
+    else /* regular character */
+      {
+#ifdef RFC_EOL
+      if (*filter->pc=='\r' && *(filter->pc+1)=='\n') ++filter->line;
+#else
+      if (*filter->pc=='\n')
+        {
+        data->character=string_cat(data->character,&dataCapacity,&data->length,US"\r",1);
+        ++filter->line;
+        }
+#endif
+      data->character=string_cat(data->character,&dataCapacity,&data->length,filter->pc,1);
+      filter->pc++;
+      }
+    }
+  filter->errmsg=CUS "missing end of string";
+  return -1;
+  }
+else if (Ustrncmp(filter->pc,CUS "text:",5)==0) /* multiline string */
+  {
+  filter->pc+=5;
+  /* skip optional white space followed by hashed comment or CRLF */
+  while (*filter->pc==' ' || *filter->pc=='\t') ++filter->pc;
+  if (*filter->pc=='#')
+    {
+    if (parse_hashcomment(filter)==-1) return -1;
+    }
+#ifdef RFC_EOL
+  else if (*filter->pc=='\r' && *(filter->pc+1)=='\n')
+#else
+  else if (*filter->pc=='\n')
+#endif
+    {
+#ifdef RFC_EOL
+    filter->pc+=2;
+#else
+    ++filter->pc;
+#endif
+    ++filter->line;
+    }
+  else
+    {
+    filter->errmsg=CUS "syntax error";
+    return -1;
+    }
+  while (*filter->pc)
+    {
+#ifdef RFC_EOL
+    if (*filter->pc=='\r' && *(filter->pc+1)=='\n') /* end of line */
+#else
+    if (*filter->pc=='\n') /* end of line */
+#endif
+      {
+      data->character=string_cat(data->character,&dataCapacity,&data->length,CUS "\r\n",2);
+#ifdef RFC_EOL
+      filter->pc+=2;
+#else
+      ++filter->pc;
+#endif
+      ++filter->line;
+#ifdef RFC_EOL
+      if (*filter->pc=='.' && *(filter->pc+1)=='\r' && *(filter->pc+2)=='\n') /* end of string */
+#else
+      if (*filter->pc=='.' && *(filter->pc+1)=='\n') /* end of string */
+#endif
+        {
+        int foo=data->length;
+
+        /* that way, there will be at least one character allocated */
+        data->character=string_cat(data->character,&dataCapacity,&foo,CUS "",1);
+#ifdef RFC_EOL
+        filter->pc+=3;
+#else
+        filter->pc+=2;
+#endif
+        ++filter->line;
+#ifdef ENCODED_CHARACTER
+        if (filter->require_encoded_character
+            && string_decode(filter,data)==-1)
+          return -1;
+#endif
+        return 1;
+        }
+      else if (*filter->pc=='.' && *(filter->pc+1)=='.') /* remove dot stuffing */
+        {
+        data->character=string_cat(data->character,&dataCapacity,&data->length,CUS ".",1);
+        filter->pc+=2;
+        }
+      }
+    else /* regular character */
+      {
+      data->character=string_cat(data->character,&dataCapacity,&data->length,filter->pc,1);
+      filter->pc++;
+      }
+    }
+  filter->errmsg=CUS "missing end of multi line string";
+  return -1;
+  }
+else return 0;
+}
+
+
+/*************************************************
+*          Parse a specific identifier           *
+*************************************************/
+
+/*
+Token definition:
+  identifier       = (ALPHA / "_") *(ALPHA DIGIT "_")
+
+Arguments:
+  filter      points to the Sieve filter including its state
+  id          specifies identifier to match
+
+Returns:      1                success
+              0                identifier not matched
+*/
+
+static int parse_identifier(struct Sieve *filter, const uschar *id)
+{
+  size_t idlen=Ustrlen(id);
+
+  if (strncmpic(US filter->pc,US id,idlen)==0)
+  {
+    uschar next=filter->pc[idlen];
+
+    if ((next>='A' && next<='Z') || (next>='a' && next<='z') || next=='_' || (next>='0' && next<='9')) return 0;
+    filter->pc+=idlen;
+    return 1;
+  }
+  else return 0;
+}
+
+
+/*************************************************
+*                 Parse a number                 *
+*************************************************/
+
+/*
+Token definition:
+  number           = 1*DIGIT [QUANTIFIER]
+  QUANTIFIER       = "K" / "M" / "G"
+
+Arguments:
+  filter      points to the Sieve filter including its state
+  data        returns value
+
+Returns:      1                success
+              -1               no string list found
+*/
+
+static int parse_number(struct Sieve *filter, unsigned long *data)
+{
+unsigned long d,u;
+
+if (*filter->pc>='0' && *filter->pc<='9')
+  {
+  uschar *e;
+
+  errno=0;
+  d=Ustrtoul(filter->pc,&e,10);
+  if (errno==ERANGE)
+    {
+    filter->errmsg=CUstrerror(ERANGE);
+    return -1;
+    }
+  filter->pc=e;
+  u=1;
+  if (*filter->pc=='K') { u=1024; ++filter->pc; }
+  else if (*filter->pc=='M') { u=1024*1024; ++filter->pc; }
+  else if (*filter->pc=='G') { u=1024*1024*1024; ++filter->pc; }
+  if (d>(ULONG_MAX/u))
+    {
+    filter->errmsg=CUstrerror(ERANGE);
+    return -1;
+    }
+  d*=u;
+  *data=d;
+  return 1;
+  }
+else
+  {
+  filter->errmsg=CUS "missing number";
+  return -1;
+  }
+}
+
+
+/*************************************************
+*              Parse a string list               *
+*************************************************/
+
+/*
+Grammar:
+  string-list      = "[" string *("," string) "]" / string
+
+Arguments:
+  filter      points to the Sieve filter including its state
+  data        returns string list
+
+Returns:      1                success
+              -1               no string list found
+*/
+
+static int parse_stringlist(struct Sieve *filter, struct String **data)
+{
+const uschar *orig=filter->pc;
+int dataCapacity=0;
+int dataLength=0;
+struct String *d=(struct String*)0;
+int m;
+
+if (*filter->pc=='[') /* string list */
+  {
+  ++filter->pc;
+  for (;;)
+    {
+    if (parse_white(filter)==-1) goto error;
+    if ((dataLength+1)>=dataCapacity) /* increase buffer */
+      {
+      struct String *new;
+      int newCapacity;          /* Don't amalgamate with next line; some compilers grumble */
+      newCapacity=dataCapacity?(dataCapacity*=2):(dataCapacity=4);
+      if ((new=(struct String*)store_get(sizeof(struct String)*newCapacity))==(struct String*)0)
+        {
+        filter->errmsg=CUstrerror(errno);
+        goto error;
+        }
+      if (d) memcpy(new,d,sizeof(struct String)*dataLength);
+      d=new;
+      dataCapacity=newCapacity;
+      }
+    m=parse_string(filter,&d[dataLength]);
+    if (m==0)
+      {
+      if (dataLength==0) break;
+      else
+        {
+        filter->errmsg=CUS "missing string";
+        goto error;
+        }
+      }
+    else if (m==-1) goto error;
+    else ++dataLength;
+    if (parse_white(filter)==-1) goto error;
+    if (*filter->pc==',') ++filter->pc;
+    else break;
+    }
+  if (*filter->pc==']')
+    {
+    d[dataLength].character=(uschar*)0;
+    d[dataLength].length=-1;
+    ++filter->pc;
+    *data=d;
+    return 1;
+    }
+  else
+    {
+    filter->errmsg=CUS "missing closing bracket";
+    goto error;
+    }
+  }
+else /* single string */
+  {
+  if ((d=store_get(sizeof(struct String)*2))==(struct String*)0)
+    {
+    return -1;
+    }
+  m=parse_string(filter,&d[0]);
+  if (m==-1)
+    {
+    return -1;
+    }
+  else if (m==0)
+    {
+    filter->pc=orig;
+    return 0;
+    }
+  else
+    {
+    d[1].character=(uschar*)0;
+    d[1].length=-1;
+    *data=d;
+    return 1;
+    }
+  }
+error:
+filter->errmsg=CUS "missing string list";
+return -1;
+}
+
+
+/*************************************************
+*    Parse an optional address part specifier    *
+*************************************************/
+
+/*
+Grammar:
+  address-part     =  ":localpart" / ":domain" / ":all"
+  address-part     =/ ":user" / ":detail"
+
+Arguments:
+  filter      points to the Sieve filter including its state
+  a           returns address part specified
+
+Returns:      1                success
+              0                no comparator found
+              -1               syntax error
+*/
+
+static int parse_addresspart(struct Sieve *filter, enum AddressPart *a)
+{
+#ifdef SUBADDRESS
+if (parse_identifier(filter,CUS ":user")==1)
+  {
+  if (!filter->require_subaddress)
+    {
+    filter->errmsg=CUS "missing previous require \"subaddress\";";
+    return -1;
+    }
+  *a=ADDRPART_USER;
+  return 1;
+  }
+else if (parse_identifier(filter,CUS ":detail")==1)
+  {
+  if (!filter->require_subaddress)
+    {
+    filter->errmsg=CUS "missing previous require \"subaddress\";";
+    return -1;
+    }
+  *a=ADDRPART_DETAIL;
+  return 1;
+  }
+else
+#endif
+if (parse_identifier(filter,CUS ":localpart")==1)
+  {
+  *a=ADDRPART_LOCALPART;
+  return 1;
+  }
+else if (parse_identifier(filter,CUS ":domain")==1)
+  {
+  *a=ADDRPART_DOMAIN;
+  return 1;
+  }
+else if (parse_identifier(filter,CUS ":all")==1)
+  {
+  *a=ADDRPART_ALL;
+  return 1;
+  }
+else return 0;
+}
+
+
+/*************************************************
+*         Parse an optional comparator           *
+*************************************************/
+
+/*
+Grammar:
+  comparator = ":comparator" <comparator-name: string>
+
+Arguments:
+  filter      points to the Sieve filter including its state
+  c           returns comparator
+
+Returns:      1                success
+              0                no comparator found
+              -1               incomplete comparator found
+*/
+
+static int parse_comparator(struct Sieve *filter, enum Comparator *c)
+{
+struct String comparator_name;
+
+if (parse_identifier(filter,CUS ":comparator")==0) return 0;
+if (parse_white(filter)==-1) return -1;
+switch (parse_string(filter,&comparator_name))
+  {
+  case -1: return -1;
+  case 0:
+    {
+    filter->errmsg=CUS "missing comparator";
+    return -1;
+    }
+  default:
+    {
+    int match;
+
+    if (eq_asciicase(&comparator_name,&str_ioctet,0))
+      {
+      *c=COMP_OCTET;
+      match=1;
+      }
+    else if (eq_asciicase(&comparator_name,&str_iascii_casemap,0))
+      {
+      *c=COMP_EN_ASCII_CASEMAP;
+      match=1;
+      }
+    else if (eq_asciicase(&comparator_name,&str_enascii_casemap,0))
+      {
+      *c=COMP_EN_ASCII_CASEMAP;
+      match=1;
+      }
+    else if (eq_asciicase(&comparator_name,&str_iascii_numeric,0))
+      {
+      *c=COMP_ASCII_NUMERIC;
+      match=1;
+      }
+    else
+      {
+      filter->errmsg=CUS "invalid comparator";
+      match=-1;
+      }
+    return match;
+    }
+  }
+}
+
+
+/*************************************************
+*          Parse an optional match type          *
+*************************************************/
+
+/*
+Grammar:
+  match-type = ":is" / ":contains" / ":matches"
+
+Arguments:
+  filter      points to the Sieve filter including its state
+  m           returns match type
+
+Returns:      1                success
+              0                no match type found
+*/
+
+static int parse_matchtype(struct Sieve *filter, enum MatchType *m)
+{
+  if (parse_identifier(filter,CUS ":is")==1)
+  {
+    *m=MATCH_IS;
+    return 1;
+  }
+  else if (parse_identifier(filter,CUS ":contains")==1)
+  {
+    *m=MATCH_CONTAINS;
+    return 1;
+  }
+  else if (parse_identifier(filter,CUS ":matches")==1)
+  {
+    *m=MATCH_MATCHES;
+    return 1;
+  }
+  else return 0;
+}
+
+
+/*************************************************
+*   Parse and interpret an optional test list    *
+*************************************************/
+
+/*
+Grammar:
+  test-list = "(" test *("," test) ")"
+
+Arguments:
+  filter      points to the Sieve filter including its state
+  n           total number of tests
+  num_true    number of passed tests
+  exec        Execute parsed statements
+
+Returns:      1                success
+              0                no test list found
+              -1               syntax or execution error
+*/
+
+static int parse_testlist(struct Sieve *filter, int *n, int *num_true, int exec)
+{
+if (parse_white(filter)==-1) return -1;
+if (*filter->pc=='(')
+  {
+  ++filter->pc;
+  *n=0;
+   *num_true=0;
+  for (;;)
+    {
+    int cond;
+
+    switch (parse_test(filter,&cond,exec))
+      {
+      case -1: return -1;
+      case 0: filter->errmsg=CUS "missing test"; return -1;
+      default: ++*n; if (cond) ++*num_true; break;
+      }
+    if (parse_white(filter)==-1) return -1;
+    if (*filter->pc==',') ++filter->pc;
+    else break;
+    }
+  if (*filter->pc==')')
+    {
+    ++filter->pc;
+    return 1;
+    }
+  else
+    {
+    filter->errmsg=CUS "missing closing paren";
+    return -1;
+    }
+  }
+else return 0;
+}
+
+
+/*************************************************
+*     Parse and interpret an optional test       *
+*************************************************/
+
+/*
+Arguments:
+  filter      points to the Sieve filter including its state
+  cond        returned condition status
+  exec        Execute parsed statements
+
+Returns:      1                success
+              0                no test found
+              -1               syntax or execution error
+*/
+
+static int parse_test(struct Sieve *filter, int *cond, int exec)
+{
+if (parse_white(filter)==-1) return -1;
+if (parse_identifier(filter,CUS "address"))
+  {
+  /*
+  address-test = "address" { [address-part] [comparator] [match-type] }
+                 <header-list: string-list> <key-list: string-list>
+
+  header-list From, To, Cc, Bcc, Sender, Resent-From, Resent-To
+  */
+
+  enum AddressPart addressPart=ADDRPART_ALL;
+  enum Comparator comparator=COMP_EN_ASCII_CASEMAP;
+  enum MatchType matchType=MATCH_IS;
+  struct String *hdr,*h,*key,*k;
+  int m;
+  int ap=0,co=0,mt=0;
+
+  for (;;)
+    {
+    if (parse_white(filter)==-1) return -1;
+    if ((m=parse_addresspart(filter,&addressPart))!=0)
+      {
+      if (m==-1) return -1;
+      if (ap)
+        {
+        filter->errmsg=CUS "address part already specified";
+        return -1;
+        }
+      else ap=1;
+      }
+    else if ((m=parse_comparator(filter,&comparator))!=0)
+      {
+      if (m==-1) return -1;
+      if (co)
+        {
+        filter->errmsg=CUS "comparator already specified";
+        return -1;
+        }
+      else co=1;
+      }
+    else if ((m=parse_matchtype(filter,&matchType))!=0)
+      {
+      if (m==-1) return -1;
+      if (mt)
+        {
+        filter->errmsg=CUS "match type already specified";
+        return -1;
+        }
+      else mt=1;
+      }
+    else break;
+    }
+  if (parse_white(filter)==-1) return -1;
+  if ((m=parse_stringlist(filter,&hdr))!=1)
+    {
+    if (m==0) filter->errmsg=CUS "header string list expected";
+    return -1;
+    }
+  if (parse_white(filter)==-1) return -1;
+  if ((m=parse_stringlist(filter,&key))!=1)
+    {
+    if (m==0) filter->errmsg=CUS "key string list expected";
+    return -1;
+    }
+  *cond=0;
+  for (h=hdr; h->length!=-1 && !*cond; ++h)
+    {
+    uschar *header_value=(uschar*)0,*extracted_addr,*end_addr;
+
+    if
+      (
+      !eq_asciicase(h,&str_from,0)
+      && !eq_asciicase(h,&str_to,0)
+      && !eq_asciicase(h,&str_cc,0)
+      && !eq_asciicase(h,&str_bcc,0)
+      && !eq_asciicase(h,&str_sender,0)
+      && !eq_asciicase(h,&str_resent_from,0)
+      && !eq_asciicase(h,&str_resent_to,0)
+      )
+      {
+      filter->errmsg=CUS "invalid header field";
+      return -1;
+      }
+    if (exec)
+      {
+      /* We are only interested in addresses below, so no MIME decoding */
+      header_value=expand_string(string_sprintf("$rheader_%s",quote(h)));
+      if (header_value == NULL)
+        {
+        filter->errmsg=CUS "header string expansion failed";
+        return -1;
+        }
+      parse_allow_group = TRUE;
+      while (*header_value && !*cond)
+        {
+        uschar *error;
+        int start, end, domain;
+        int saveend;
+        uschar *part=NULL;
+
+        end_addr = parse_find_address_end(header_value, FALSE);
+        saveend = *end_addr;
+        *end_addr = 0;
+        extracted_addr = parse_extract_address(header_value, &error, &start, &end, &domain, FALSE);
+
+        if (extracted_addr) switch (addressPart)
+          {
+          case ADDRPART_ALL: part=extracted_addr; break;
+#ifdef SUBADDRESS
+          case ADDRPART_USER:
+#endif
+          case ADDRPART_LOCALPART: part=extracted_addr; part[domain-1]='\0'; break;
+          case ADDRPART_DOMAIN: part=extracted_addr+domain; break;
+#ifdef SUBADDRESS
+          case ADDRPART_DETAIL: part=NULL; break;
+#endif
+          }
+
+        *end_addr = saveend;
+        if (part)
+          {
+          for (k=key; k->length!=-1; ++k)
+            {
+            struct String partStr;
+
+            partStr.character=part;
+            partStr.length=Ustrlen(part);
+            if (extracted_addr)
+              {
+              *cond=compare(filter,k,&partStr,comparator,matchType);
+              if (*cond==-1) return -1;
+              if (*cond) break;
+              }
+            }
+          }
+        if (saveend == 0) break;
+        header_value = end_addr + 1;
+        }
+      parse_allow_group = FALSE;
+      parse_found_group = FALSE;
+      }
+    }
+  return 1;
+  }
+else if (parse_identifier(filter,CUS "allof"))
+  {
+  /*
+  allof-test   = "allof" <tests: test-list>
+  */
+
+  int n,num_true;
+
+  switch (parse_testlist(filter,&n,&num_true,exec))
+    {
+    case -1: return -1;
+    case 0: filter->errmsg=CUS "missing test list"; return -1;
+    default: *cond=(n==num_true); return 1;
+    }
+  }
+else if (parse_identifier(filter,CUS "anyof"))
+  {
+  /*
+  anyof-test   = "anyof" <tests: test-list>
+  */
+
+  int n,num_true;
+
+  switch (parse_testlist(filter,&n,&num_true,exec))
+    {
+    case -1: return -1;
+    case 0: filter->errmsg=CUS "missing test list"; return -1;
+    default: *cond=(num_true>0); return 1;
+    }
+  }
+else if (parse_identifier(filter,CUS "exists"))
+  {
+  /*
+  exists-test = "exists" <header-names: string-list>
+  */
+
+  struct String *hdr,*h;
+  int m;
+
+  if (parse_white(filter)==-1) return -1;
+  if ((m=parse_stringlist(filter,&hdr))!=1)
+    {
+    if (m==0) filter->errmsg=CUS "header string list expected";
+    return -1;
+    }
+  if (exec)
+    {
+    *cond=1;
+    for (h=hdr; h->length!=-1 && *cond; ++h)
+      {
+      uschar *header_def;
+
+      header_def=expand_string(string_sprintf("${if def:header_%s {true}{false}}",quote(h)));
+      if (header_def == NULL)
+        {
+        filter->errmsg=CUS "header string expansion failed";
+        return -1;
+        }
+      if (Ustrcmp(header_def,"false")==0) *cond=0;
+      }
+    }
+  return 1;
+  }
+else if (parse_identifier(filter,CUS "false"))
+  {
+  /*
+  false-test = "false"
+  */
+
+  *cond=0;
+  return 1;
+  }
+else if (parse_identifier(filter,CUS "header"))
+  {
+  /*
+  header-test = "header" { [comparator] [match-type] }
+                <header-names: string-list> <key-list: string-list>
+  */
+
+  enum Comparator comparator=COMP_EN_ASCII_CASEMAP;
+  enum MatchType matchType=MATCH_IS;
+  struct String *hdr,*h,*key,*k;
+  int m;
+  int co=0,mt=0;
+
+  for (;;)
+    {
+    if (parse_white(filter)==-1) return -1;
+    if ((m=parse_comparator(filter,&comparator))!=0)
+      {
+      if (m==-1) return -1;
+      if (co)
+        {
+        filter->errmsg=CUS "comparator already specified";
+        return -1;
+        }
+      else co=1;
+      }
+    else if ((m=parse_matchtype(filter,&matchType))!=0)
+      {
+      if (m==-1) return -1;
+      if (mt)
+        {
+        filter->errmsg=CUS "match type already specified";
+        return -1;
+        }
+      else mt=1;
+      }
+    else break;
+    }
+  if (parse_white(filter)==-1) return -1;
+  if ((m=parse_stringlist(filter,&hdr))!=1)
+    {
+    if (m==0) filter->errmsg=CUS "header string list expected";
+    return -1;
+    }
+  if (parse_white(filter)==-1) return -1;
+  if ((m=parse_stringlist(filter,&key))!=1)
+    {
+    if (m==0) filter->errmsg=CUS "key string list expected";
+    return -1;
+    }
+  *cond=0;
+  for (h=hdr; h->length!=-1 && !*cond; ++h)
+    {
+    if (!is_header(h))
+      {
+      filter->errmsg=CUS "invalid header field";
+      return -1;
+      }
+    if (exec)
+      {
+      struct String header_value;
+      uschar *header_def;
+
+      expand_header(&header_value,h);
+      header_def=expand_string(string_sprintf("${if def:header_%s {true}{false}}",quote(h)));
+      if (header_value.character == NULL || header_def == NULL)
+        {
+        filter->errmsg=CUS "header string expansion failed";
+        return -1;
+        }
+      for (k=key; k->length!=-1; ++k)
+        {
+        if (Ustrcmp(header_def,"true")==0)
+          {
+          *cond=compare(filter,k,&header_value,comparator,matchType);
+          if (*cond==-1) return -1;
+          if (*cond) break;
+          }
+        }
+      }
+    }
+  return 1;
+  }
+else if (parse_identifier(filter,CUS "not"))
+  {
+  if (parse_white(filter)==-1) return -1;
+  switch (parse_test(filter,cond,exec))
+    {
+    case -1: return -1;
+    case 0: filter->errmsg=CUS "missing test"; return -1;
+    default: *cond=!*cond; return 1;
+    }
+  }
+else if (parse_identifier(filter,CUS "size"))
+  {
+  /*
+  relop = ":over" / ":under"
+  size-test = "size" relop <limit: number>
+  */
+
+  unsigned long limit;
+  int overNotUnder;
+
+  if (parse_white(filter)==-1) return -1;
+  if (parse_identifier(filter,CUS ":over")) overNotUnder=1;
+  else if (parse_identifier(filter,CUS ":under")) overNotUnder=0;
+  else
+    {
+    filter->errmsg=CUS "missing :over or :under";
+    return -1;
+    }
+  if (parse_white(filter)==-1) return -1;
+  if (parse_number(filter,&limit)==-1) return -1;
+  *cond=(overNotUnder ? (message_size>limit) : (message_size<limit));
+  return 1;
+  }
+else if (parse_identifier(filter,CUS "true"))
+  {
+  *cond=1;
+  return 1;
+  }
+else if (parse_identifier(filter,CUS "envelope"))
+  {
+  /*
+  envelope-test = "envelope" { [comparator] [address-part] [match-type] }
+                  <envelope-part: string-list> <key-list: string-list>
+
+  envelope-part is case insensitive "from" or "to"
+#ifdef ENVELOPE_AUTH
+  envelope-part =/ "auth"
+#endif
+  */
+
+  enum Comparator comparator=COMP_EN_ASCII_CASEMAP;
+  enum AddressPart addressPart=ADDRPART_ALL;
+  enum MatchType matchType=MATCH_IS;
+  struct String *env,*e,*key,*k;
+  int m;
+  int co=0,ap=0,mt=0;
+
+  if (!filter->require_envelope)
+    {
+    filter->errmsg=CUS "missing previous require \"envelope\";";
+    return -1;
+    }
+  for (;;)
+    {
+    if (parse_white(filter)==-1) return -1;
+    if ((m=parse_comparator(filter,&comparator))!=0)
+      {
+      if (m==-1) return -1;
+      if (co)
+        {
+        filter->errmsg=CUS "comparator already specified";
+        return -1;
+        }
+      else co=1;
+      }
+    else if ((m=parse_addresspart(filter,&addressPart))!=0)
+      {
+      if (m==-1) return -1;
+      if (ap)
+        {
+        filter->errmsg=CUS "address part already specified";
+        return -1;
+        }
+      else ap=1;
+      }
+    else if ((m=parse_matchtype(filter,&matchType))!=0)
+      {
+      if (m==-1) return -1;
+      if (mt)
+        {
+        filter->errmsg=CUS "match type already specified";
+        return -1;
+        }
+      else mt=1;
+      }
+    else break;
+    }
+  if (parse_white(filter)==-1) return -1;
+  if ((m=parse_stringlist(filter,&env))!=1)
+    {
+    if (m==0) filter->errmsg=CUS "envelope string list expected";
+    return -1;
+    }
+  if (parse_white(filter)==-1) return -1;
+  if ((m=parse_stringlist(filter,&key))!=1)
+    {
+    if (m==0) filter->errmsg=CUS "key string list expected";
+    return -1;
+    }
+  *cond=0;
+  for (e=env; e->length!=-1 && !*cond; ++e)
+    {
+    const uschar *envelopeExpr=CUS 0;
+    uschar *envelope=US 0;
+
+    if (eq_asciicase(e,&str_from,0))
+      {
+      switch (addressPart)
+        {
+        case ADDRPART_ALL: envelopeExpr=CUS "$sender_address"; break;
+#ifdef SUBADDRESS
+        case ADDRPART_USER:
+#endif
+        case ADDRPART_LOCALPART: envelopeExpr=CUS "${local_part:$sender_address}"; break;
+        case ADDRPART_DOMAIN: envelopeExpr=CUS "${domain:$sender_address}"; break;
+#ifdef SUBADDRESS
+        case ADDRPART_DETAIL: envelopeExpr=CUS 0; break;
+#endif
+        }
+      }
+    else if (eq_asciicase(e,&str_to,0))
+      {
+      switch (addressPart)
+        {
+        case ADDRPART_ALL: envelopeExpr=CUS "$local_part_prefix$local_part$local_part_suffix@$domain"; break;
+#ifdef SUBADDRESS
+        case ADDRPART_USER: envelopeExpr=filter->useraddress; break;
+        case ADDRPART_DETAIL: envelopeExpr=filter->subaddress; break;
+#endif
+        case ADDRPART_LOCALPART: envelopeExpr=CUS "$local_part_prefix$local_part$local_part_suffix"; break;
+        case ADDRPART_DOMAIN: envelopeExpr=CUS "$domain"; break;
+        }
+      }
+#ifdef ENVELOPE_AUTH
+    else if (eq_asciicase(e,&str_auth,0))
+      {
+      switch (addressPart)
+        {
+        case ADDRPART_ALL: envelopeExpr=CUS "$authenticated_sender"; break;
+#ifdef SUBADDRESS
+        case ADDRPART_USER:
+#endif
+        case ADDRPART_LOCALPART: envelopeExpr=CUS "${local_part:$authenticated_sender}"; break;
+        case ADDRPART_DOMAIN: envelopeExpr=CUS "${domain:$authenticated_sender}"; break;
+#ifdef SUBADDRESS
+        case ADDRPART_DETAIL: envelopeExpr=CUS 0; break;
+#endif
+        }
+      }
+#endif
+    else
+      {
+      filter->errmsg=CUS "invalid envelope string";
+      return -1;
+      }
+    if (exec && envelopeExpr)
+      {
+      if ((envelope=expand_string(US envelopeExpr)) == NULL)
+        {
+        filter->errmsg=CUS "header string expansion failed";
+        return -1;
+        }
+      for (k=key; k->length!=-1; ++k)
+        {
+        struct String envelopeStr;
+
+        envelopeStr.character=envelope;
+        envelopeStr.length=Ustrlen(envelope);
+        *cond=compare(filter,k,&envelopeStr,comparator,matchType);
+        if (*cond==-1) return -1;
+        if (*cond) break;
+        }
+      }
+    }
+  return 1;
+  }
+#ifdef ENOTIFY
+else if (parse_identifier(filter,CUS "valid_notify_method"))
+  {
+  /*
+  valid_notify_method = "valid_notify_method"
+                        <notification-uris: string-list>
+  */
+
+  struct String *uris,*u;
+  int m;
+
+  if (!filter->require_enotify)
+    {
+    filter->errmsg=CUS "missing previous require \"enotify\";";
+    return -1;
+    }
+  if (parse_white(filter)==-1) return -1;
+  if ((m=parse_stringlist(filter,&uris))!=1)
+    {
+    if (m==0) filter->errmsg=CUS "URI string list expected";
+    return -1;
+    }
+  if (exec)
+    {
+    *cond=1;
+    for (u=uris; u->length!=-1 && *cond; ++u)
+      {
+        string_item *recipient;
+        struct String header,subject,body;
+
+        recipient=NULL;
+        header.length=-1;
+        header.character=(uschar*)0;
+        subject.length=-1;
+        subject.character=(uschar*)0;
+        body.length=-1;
+        body.character=(uschar*)0;
+        if (parse_mailto_uri(filter,u->character,&recipient,&header,&subject,&body)!=1)
+          *cond=0;
+      }
+    }
+  return 1;
+  }
+else if (parse_identifier(filter,CUS "notify_method_capability"))
+  {
+  /*
+  notify_method_capability = "notify_method_capability" [COMPARATOR] [MATCH-TYPE]
+                             <notification-uri: string>
+                             <notification-capability: string>
+                             <key-list: string-list>
+  */
+
+  int m;
+  int co=0,mt=0;
+
+  enum Comparator comparator=COMP_EN_ASCII_CASEMAP;
+  enum MatchType matchType=MATCH_IS;
+  struct String uri,capa,*keys,*k;
+
+  if (!filter->require_enotify)
+    {
+    filter->errmsg=CUS "missing previous require \"enotify\";";
+    return -1;
+    }
+  for (;;)
+    {
+    if (parse_white(filter)==-1) return -1;
+    if ((m=parse_comparator(filter,&comparator))!=0)
+      {
+      if (m==-1) return -1;
+      if (co)
+        {
+        filter->errmsg=CUS "comparator already specified";
+        return -1;
+        }
+      else co=1;
+      }
+    else if ((m=parse_matchtype(filter,&matchType))!=0)
+      {
+      if (m==-1) return -1;
+      if (mt)
+        {
+        filter->errmsg=CUS "match type already specified";
+        return -1;
+        }
+      else mt=1;
+      }
+    else break;
+    }
+    if ((m=parse_string(filter,&uri))!=1)
+      {
+      if (m==0) filter->errmsg=CUS "missing notification URI string";
+      return -1;
+      }
+    if (parse_white(filter)==-1) return -1;
+    if ((m=parse_string(filter,&capa))!=1)
+      {
+      if (m==0) filter->errmsg=CUS "missing notification capability string";
+      return -1;
+      }
+    if (parse_white(filter)==-1) return -1;
+    if ((m=parse_stringlist(filter,&keys))!=1)
+      {
+      if (m==0) filter->errmsg=CUS "missing key string list";
+      return -1;
+      }
+    if (exec)
+      {
+      string_item *recipient;
+      struct String header,subject,body;
+
+      *cond=0;
+      recipient=NULL;
+      header.length=-1;
+      header.character=(uschar*)0;
+      subject.length=-1;
+      subject.character=(uschar*)0;
+      body.length=-1;
+      body.character=(uschar*)0;
+      if (parse_mailto_uri(filter,uri.character,&recipient,&header,&subject,&body)==1)
+        {
+        if (eq_asciicase(&capa,&str_online,0)==1)
+          for (k=keys; k->length!=-1; ++k)
+            {
+            *cond=compare(filter,k,&str_maybe,comparator,matchType);
+            if (*cond==-1) return -1;
+            if (*cond) break;
+            }
+        }
+      }
+    return 1;
+  }
+#endif
+else return 0;
+}
+
+
+/*************************************************
+*     Parse and interpret an optional block      *
+*************************************************/
+
+/*
+Arguments:
+  filter      points to the Sieve filter including its state
+  exec        Execute parsed statements
+  generated   where to hang newly-generated addresses
+
+Returns:      2                success by stop
+              1                other success
+              0                no block command found
+              -1               syntax or execution error
+*/
+
+static int parse_block(struct Sieve *filter, int exec,
+  address_item **generated)
+{
+int r;
+
+if (parse_white(filter)==-1) return -1;
+if (*filter->pc=='{')
+  {
+  ++filter->pc;
+  if ((r=parse_commands(filter,exec,generated))==-1 || r==2) return r;
+  if (*filter->pc=='}')
+    {
+    ++filter->pc;
+    return 1;
+    }
+  else
+    {
+    filter->errmsg=CUS "expecting command or closing brace";
+    return -1;
+    }
+  }
+else return 0;
+}
+
+
+/*************************************************
+*           Match a semicolon                    *
+*************************************************/
+
+/*
+Arguments:
+  filter      points to the Sieve filter including its state
+
+Returns:      1                success
+              -1               syntax error
+*/
+
+static int parse_semicolon(struct Sieve *filter)
+{
+  if (parse_white(filter)==-1) return -1;
+  if (*filter->pc==';')
+  {
+    ++filter->pc;
+    return 1;
+  }
+  else
+  {
+    filter->errmsg=CUS "missing semicolon";
+    return -1;
+  }
+}
+
+
+/*************************************************
+*     Parse and interpret a Sieve command        *
+*************************************************/
+
+/*
+Arguments:
+  filter      points to the Sieve filter including its state
+  exec        Execute parsed statements
+  generated   where to hang newly-generated addresses
+
+Returns:      2                success by stop
+              1                other success
+              -1               syntax or execution error
+*/
+static int parse_commands(struct Sieve *filter, int exec,
+  address_item **generated)
+{
+while (*filter->pc)
+  {
+  if (parse_white(filter)==-1) return -1;
+  if (parse_identifier(filter,CUS "if"))
+    {
+    /*
+    if-command = "if" test block *( "elsif" test block ) [ else block ]
+    */
+
+    int cond,m,unsuccessful;
+
+    /* test block */
+    if (parse_white(filter)==-1) return -1;
+    if ((m=parse_test(filter,&cond,exec))==-1) return -1;
+    if (m==0)
+      {
+      filter->errmsg=CUS "missing test";
+      return -1;
+      }
+    if ((filter_test != FTEST_NONE && debug_selector != 0) ||
+        (debug_selector & D_filter) != 0)
+      {
+      if (exec) debug_printf("if %s\n",cond?"true":"false");
+      }
+    m=parse_block(filter,exec ? cond : 0, generated);
+    if (m==-1 || m==2) return m;
+    if (m==0)
+      {
+      filter->errmsg=CUS "missing block";
+      return -1;
+      }
+    unsuccessful = !cond;
+    for (;;) /* elsif test block */
+      {
+      if (parse_white(filter)==-1) return -1;
+      if (parse_identifier(filter,CUS "elsif"))
+        {
+        if (parse_white(filter)==-1) return -1;
+        m=parse_test(filter,&cond,exec && unsuccessful);
+        if (m==-1 || m==2) return m;
+        if (m==0)
+          {
+          filter->errmsg=CUS "missing test";
+          return -1;
+          }
+        if ((filter_test != FTEST_NONE && debug_selector != 0) ||
+            (debug_selector & D_filter) != 0)
+          {
+          if (exec) debug_printf("elsif %s\n",cond?"true":"false");
+          }
+        m=parse_block(filter,exec && unsuccessful ? cond : 0, generated);
+        if (m==-1 || m==2) return m;
+        if (m==0)
+          {
+          filter->errmsg=CUS "missing block";
+          return -1;
+          }
+        if (exec && unsuccessful && cond) unsuccessful = 0;
+        }
+      else break;
+      }
+    /* else block */
+    if (parse_white(filter)==-1) return -1;
+    if (parse_identifier(filter,CUS "else"))
+      {
+      m=parse_block(filter,exec && unsuccessful, generated);
+      if (m==-1 || m==2) return m;
+      if (m==0)
+        {
+        filter->errmsg=CUS "missing block";
+        return -1;
+        }
+      }
+    }
+  else if (parse_identifier(filter,CUS "stop"))
+    {
+    /*
+    stop-command     =  "stop" { stop-options } ";"
+    stop-options     =
+    */
+
+    if (parse_semicolon(filter)==-1) return -1;
+    if (exec)
+      {
+      filter->pc+=Ustrlen(filter->pc);
+      return 2;
+      }
+    }
+  else if (parse_identifier(filter,CUS "keep"))
+    {
+    /*
+    keep-command     =  "keep" { keep-options } ";"
+    keep-options     =
+    */
+
+    if (parse_semicolon(filter)==-1) return -1;
+    if (exec)
+      {
+      add_addr(generated,US"inbox",1,0,0,0);
+      filter->keep = 0;
+      }
+    }
+  else if (parse_identifier(filter,CUS "discard"))
+    {
+    /*
+    discard-command  =  "discard" { discard-options } ";"
+    discard-options  =
+    */
+
+    if (parse_semicolon(filter)==-1) return -1;
+    if (exec) filter->keep=0;
+    }
+  else if (parse_identifier(filter,CUS "redirect"))
+    {
+    /*
+    redirect-command =  "redirect" redirect-options "string" ";"
+    redirect-options =
+    redirect-options =) ":copy"
+    */
+
+    struct String recipient;
+    int m;
+    int copy=0;
+
+    for (;;)
+      {
+      if (parse_white(filter)==-1) return -1;
+      if (parse_identifier(filter,CUS ":copy")==1)
+        {
+        if (!filter->require_copy)
+          {
+          filter->errmsg=CUS "missing previous require \"copy\";";
+          return -1;
+          }
+          copy=1;
+        }
+      else break;
+      }
+    if (parse_white(filter)==-1) return -1;
+    if ((m=parse_string(filter,&recipient))!=1)
+      {
+      if (m==0) filter->errmsg=CUS "missing redirect recipient string";
+      return -1;
+      }
+    if (strchr(CCS recipient.character,'@')==(char*)0)
+      {
+      filter->errmsg=CUS "unqualified recipient address";
+      return -1;
+      }
+    if (exec)
+      {
+      add_addr(generated,recipient.character,0,0,0,0);
+      if (!copy) filter->keep = 0;
+      }
+    if (parse_semicolon(filter)==-1) return -1;
+    }
+  else if (parse_identifier(filter,CUS "fileinto"))
+    {
+    /*
+    fileinto-command =  "fileinto" { fileinto-options } string ";"
+    fileinto-options =
+    fileinto-options =) [ ":copy" ]
+    */
+
+    struct String folder;
+    uschar *s;
+    int m;
+    unsigned long maxage, maxmessages, maxstorage;
+    int copy=0;
+
+    maxage = maxmessages = maxstorage = 0;
+    if (!filter->require_fileinto)
+      {
+      filter->errmsg=CUS "missing previous require \"fileinto\";";
+      return -1;
+      }
+    for (;;)
+      {
+      if (parse_white(filter)==-1) return -1;
+      if (parse_identifier(filter,CUS ":copy")==1)
+        {
+        if (!filter->require_copy)
+          {
+          filter->errmsg=CUS "missing previous require \"copy\";";
+          return -1;
+          }
+          copy=1;
+        }
+      else break;
+      }
+    if (parse_white(filter)==-1) return -1;
+    if ((m=parse_string(filter,&folder))!=1)
+      {
+      if (m==0) filter->errmsg=CUS "missing fileinto folder string";
+      return -1;
+      }
+    m=0; s=folder.character;
+    if (folder.length==0) m=1;
+    if (Ustrcmp(s,"..")==0 || Ustrncmp(s,"../",3)==0) m=1;
+    else while (*s)
+      {
+      if (Ustrcmp(s,"/..")==0 || Ustrncmp(s,"/../",4)==0) { m=1; break; }
+      ++s;
+      }
+    if (m)
+      {
+      filter->errmsg=CUS "invalid folder";
+      return -1;
+      }
+    if (exec)
+      {
+      add_addr(generated, folder.character, 1, maxage, maxmessages, maxstorage);
+      if (!copy) filter->keep = 0;
+      }
+    if (parse_semicolon(filter)==-1) return -1;
+    }
+#ifdef ENOTIFY
+  else if (parse_identifier(filter,CUS "notify"))
+    {
+    /*
+    notify-command =  "notify" { notify-options } <method: string> ";"
+    notify-options =  [":from" string]
+                      [":importance" <"1" / "2" / "3">]
+                      [":options" 1*(string-list / number)]
+                      [":message" string]
+    */
+
+    int m;
+    struct String from;
+    struct String importance;
+    struct String *options;
+    struct String message;
+    struct String method;
+    struct Notification *already;
+    string_item *recipient;
+    struct String header;
+    struct String subject;
+    struct String body;
+    uschar *envelope_from;
+    struct String auto_submitted_value;
+    uschar *auto_submitted_def;
+
+    if (!filter->require_enotify)
+      {
+      filter->errmsg=CUS "missing previous require \"enotify\";";
+      return -1;
+      }
+    from.character=(uschar*)0;
+    from.length=-1;
+    importance.character=(uschar*)0;
+    importance.length=-1;
+    options=(struct String*)0;
+    message.character=(uschar*)0;
+    message.length=-1;
+    recipient=NULL;
+    header.length=-1;
+    header.character=(uschar*)0;
+    subject.length=-1;
+    subject.character=(uschar*)0;
+    body.length=-1;
+    body.character=(uschar*)0;
+    envelope_from=(sender_address && sender_address[0]) ? expand_string(US"$local_part_prefix$local_part$local_part_suffix@$domain") : US "";
+    for (;;)
+      {
+      if (parse_white(filter)==-1) return -1;
+      if (parse_identifier(filter,CUS ":from")==1)
+        {
+        if (parse_white(filter)==-1) return -1;
+        if ((m=parse_string(filter,&from))!=1)
+          {
+          if (m==0) filter->errmsg=CUS "from string expected";
+          return -1;
+          }
+        }
+      else if (parse_identifier(filter,CUS ":importance")==1)
+        {
+        if (parse_white(filter)==-1) return -1;
+        if ((m=parse_string(filter,&importance))!=1)
+          {
+          if (m==0) filter->errmsg=CUS "importance string expected";
+          return -1;
+          }
+        if (importance.length!=1 || importance.character[0]<'1' || importance.character[0]>'3')
+          {
+          filter->errmsg=CUS "invalid importance";
+          return -1;
+          }
+        }
+      else if (parse_identifier(filter,CUS ":options")==1)
+        {
+        if (parse_white(filter)==-1) return -1;
+        }
+      else if (parse_identifier(filter,CUS ":message")==1)
+        {
+        if (parse_white(filter)==-1) return -1;
+        if ((m=parse_string(filter,&message))!=1)
+          {
+          if (m==0) filter->errmsg=CUS "message string expected";
+          return -1;
+          }
+        }
+      else break;
+      }
+    if (parse_white(filter)==-1) return -1;
+    if ((m=parse_string(filter,&method))!=1)
+      {
+      if (m==0) filter->errmsg=CUS "missing method string";
+      return -1;
+      }
+    if (parse_semicolon(filter)==-1) return -1;
+    if (parse_mailto_uri(filter,method.character,&recipient,&header,&subject,&body)!=1)
+      return -1;
+    if (exec)
+      {
+      if (message.length==-1) message=subject;
+      if (message.length==-1) expand_header(&message,&str_subject);
+      expand_header(&auto_submitted_value,&str_auto_submitted);
+      auto_submitted_def=expand_string(string_sprintf("${if def:header_auto-submitted {true}{false}}"));
+      if (auto_submitted_value.character == NULL || auto_submitted_def == NULL)
+        {
+        filter->errmsg=CUS "header string expansion failed";
+        return -1;
+        }
+        if (Ustrcmp(auto_submitted_def,"true")!=0 || Ustrcmp(auto_submitted_value.character,"no")==0)
+        {
+        for (already=filter->notified; already; already=already->next)
+          {
+          if (already->method.length==method.length
+              && (method.length==-1 || Ustrcmp(already->method.character,method.character)==0)
+              && already->importance.length==importance.length
+              && (importance.length==-1 || Ustrcmp(already->importance.character,importance.character)==0)
+              && already->message.length==message.length
+              && (message.length==-1 || Ustrcmp(already->message.character,message.character)==0))
+            break;
+          }
+        if (already==(struct Notification*)0)
+          /* New notification, process it */
+          {
+          struct Notification *sent;
+          sent=store_get(sizeof(struct Notification));
+          sent->method=method;
+          sent->importance=importance;
+          sent->message=message;
+          sent->next=filter->notified;
+          filter->notified=sent;
+  #ifndef COMPILE_SYNTAX_CHECKER
+          if (filter_test == FTEST_NONE)
+            {
+            string_item *p;
+            int pid,fd;
+
+            if ((pid = child_open_exim2(&fd,envelope_from,envelope_from))>=1)
+              {
+              FILE *f;
+              uschar *buffer;
+              int buffer_capacity;
+
+              f = fdopen(fd, "wb");
+              fprintf(f,"From: %s\n",from.length==-1 ? expand_string(US"$local_part_prefix$local_part$local_part_suffix@$domain") : from.character);
+              for (p=recipient; p; p=p->next) fprintf(f,"To: %s\n",p->text);
+              fprintf(f,"Auto-Submitted: auto-notified; %s\n",filter->enotify_mailto_owner);
+              if (header.length>0) fprintf(f,"%s",header.character);
+              if (message.length==-1)
+                {
+                message.character=US"Notification";
+                message.length=Ustrlen(message.character);
+                }
+              /* Allocation is larger than neccessary, but enough even for split MIME words */
+              buffer_capacity=32+4*message.length;
+              buffer=store_get(buffer_capacity);
+              if (message.length!=-1) fprintf(f,"Subject: %s\n",parse_quote_2047(message.character, message.length, US"utf-8", buffer, buffer_capacity, TRUE));
+              fprintf(f,"\n");
+              if (body.length>0) fprintf(f,"%s\n",body.character);
+              fflush(f);
+              (void)fclose(f);
+              (void)child_close(pid, 0);
+              }
+            }
+          if ((filter_test != FTEST_NONE && debug_selector != 0) || (debug_selector & D_filter) != 0)
+            {
+            debug_printf("Notification to `%s': '%s'.\n",method.character,message.length!=-1 ? message.character : CUS "");
+            }
+#endif
+          }
+        else
+          {
+          if ((filter_test != FTEST_NONE && debug_selector != 0) || (debug_selector & D_filter) != 0)
+            {
+            debug_printf("Repeated notification to `%s' ignored.\n",method.character);
+            }
+          }
+        }
+      else
+        {
+        if ((filter_test != FTEST_NONE && debug_selector != 0) || (debug_selector & D_filter) != 0)
+          {
+          debug_printf("Ignoring notification, triggering message contains Auto-submitted: field.\n");
+          }
+        }
+      }
+    }
+#endif
+#ifdef VACATION
+  else if (parse_identifier(filter,CUS "vacation"))
+    {
+    /*
+    vacation-command =  "vacation" { vacation-options } <reason: string> ";"
+    vacation-options =  [":days" number]
+                        [":subject" string]
+                        [":from" string]
+                        [":addresses" string-list]
+                        [":mime"]
+                        [":handle" string]
+    */
+
+    int m;
+    unsigned long days;
+    struct String subject;
+    struct String from;
+    struct String *addresses;
+    int reason_is_mime;
+    string_item *aliases;
+    struct String handle;
+    struct String reason;
+
+    if (!filter->require_vacation)
+      {
+      filter->errmsg=CUS "missing previous require \"vacation\";";
+      return -1;
+      }
+    if (exec)
+      {
+      if (filter->vacation_ran)
+        {
+        filter->errmsg=CUS "trying to execute vacation more than once";
+        return -1;
+        }
+      filter->vacation_ran=1;
+      }
+    days=VACATION_MIN_DAYS>7 ? VACATION_MIN_DAYS : 7;
+    subject.character=(uschar*)0;
+    subject.length=-1;
+    from.character=(uschar*)0;
+    from.length=-1;
+    addresses=(struct String*)0;
+    aliases=NULL;
+    reason_is_mime=0;
+    handle.character=(uschar*)0;
+    handle.length=-1;
+    for (;;)
+      {
+      if (parse_white(filter)==-1) return -1;
+      if (parse_identifier(filter,CUS ":days")==1)
+        {
+        if (parse_white(filter)==-1) return -1;
+        if (parse_number(filter,&days)==-1) return -1;
+        if (days<VACATION_MIN_DAYS) days=VACATION_MIN_DAYS;
+        else if (days>VACATION_MAX_DAYS) days=VACATION_MAX_DAYS;
+        }
+      else if (parse_identifier(filter,CUS ":subject")==1)
+        {
+        if (parse_white(filter)==-1) return -1;
+        if ((m=parse_string(filter,&subject))!=1)
+          {
+          if (m==0) filter->errmsg=CUS "subject string expected";
+          return -1;
+          }
+        }
+      else if (parse_identifier(filter,CUS ":from")==1)
+        {
+        if (parse_white(filter)==-1) return -1;
+        if ((m=parse_string(filter,&from))!=1)
+          {
+          if (m==0) filter->errmsg=CUS "from string expected";
+          return -1;
+          }
+        if (check_mail_address(filter,&from)!=1)
+          return -1;
+        }
+      else if (parse_identifier(filter,CUS ":addresses")==1)
+        {
+        struct String *a;
+
+        if (parse_white(filter)==-1) return -1;
+        if ((m=parse_stringlist(filter,&addresses))!=1)
+          {
+          if (m==0) filter->errmsg=CUS "addresses string list expected";
+          return -1;
+          }
+        for (a=addresses; a->length!=-1; ++a)
+          {
+          string_item *new;
+
+          new=store_get(sizeof(string_item));
+          new->text=store_get(a->length+1);
+          if (a->length) memcpy(new->text,a->character,a->length);
+          new->text[a->length]='\0';
+          new->next=aliases;
+          aliases=new;
+          }
+        }
+      else if (parse_identifier(filter,CUS ":mime")==1)
+        reason_is_mime=1;
+      else if (parse_identifier(filter,CUS ":handle")==1)
+        {
+        if (parse_white(filter)==-1) return -1;
+        if ((m=parse_string(filter,&from))!=1)
+          {
+          if (m==0) filter->errmsg=CUS "handle string expected";
+          return -1;
+          }
+        }
+      else break;
+      }
+    if (parse_white(filter)==-1) return -1;
+    if ((m=parse_string(filter,&reason))!=1)
+      {
+      if (m==0) filter->errmsg=CUS "missing reason string";
+      return -1;
+      }
+    if (reason_is_mime)
+      {
+      uschar *s,*end;
+
+      for (s=reason.character,end=reason.character+reason.length; s<end && (*s&0x80)==0; ++s);
+      if (s<end)
+        {
+        filter->errmsg=CUS "MIME reason string contains 8bit text";
+        return -1;
+        }
+      }
+    if (parse_semicolon(filter)==-1) return -1;
+
+    if (exec)
+      {
+      address_item *addr;
+      int capacity,start;
+      uschar *buffer;
+      int buffer_capacity;
+      struct String key;
+      md5 base;
+      uschar digest[16];
+      uschar hexdigest[33];
+      int i;
+      uschar *once;
+
+      if (filter_personal(aliases,TRUE))
+        {
+        if (filter_test == FTEST_NONE)
+          {
+          /* ensure oncelog directory exists; failure will be detected later */
+
+          (void)directory_make(NULL, filter->vacation_directory, 0700, FALSE);
+          }
+        /* build oncelog filename */
+
+        key.character=(uschar*)0;
+        key.length=0;
+        capacity=0;
+        if (handle.length==-1)
+          {
+          if (subject.length!=-1) key.character=string_cat(key.character,&capacity,&key.length,subject.character,subject.length);
+          if (from.length!=-1) key.character=string_cat(key.character,&capacity,&key.length,from.character,from.length);
+          key.character=string_cat(key.character,&capacity,&key.length,reason_is_mime?US"1":US"0",1);
+          key.character=string_cat(key.character,&capacity,&key.length,reason.character,reason.length);
+          }
+        else
+          key=handle;
+        md5_start(&base);
+        md5_end(&base, key.character, key.length, digest);
+        for (i = 0; i < 16; i++) sprintf(CS (hexdigest+2*i), "%02X", digest[i]);
+        if ((filter_test != FTEST_NONE && debug_selector != 0) || (debug_selector & D_filter) != 0)
+          {
+          debug_printf("Sieve: mail was personal, vacation file basename: %s\n", hexdigest);
+          }
+        if (filter_test == FTEST_NONE)
+          {
+          capacity=Ustrlen(filter->vacation_directory);
+          start=capacity;
+          once=string_cat(filter->vacation_directory,&capacity,&start,US"/",1);
+          once=string_cat(once,&capacity,&start,hexdigest,33);
+          once[start] = '\0';
+
+          /* process subject */
+
+          if (subject.length==-1)
+            {
+            uschar *subject_def;
+
+            subject_def=expand_string(US"${if def:header_subject {true}{false}}");
+            if (Ustrcmp(subject_def,"true")==0)
+              {
+              expand_header(&subject,&str_subject);
+              capacity=6;
+              start=6;
+              subject.character=string_cat(US"Auto: ",&capacity,&start,subject.character,subject.length);
+              subject.length=start;
+              }
+            else
+              {
+              subject.character=US"Automated reply";
+              subject.length=Ustrlen(subject.character);
+              }
+            }
+
+          /* add address to list of generated addresses */
+
+          addr = deliver_make_addr(string_sprintf(">%.256s", sender_address), FALSE);
+          setflag(addr, af_pfr);
+          setflag(addr, af_ignore_error);
+          addr->next = *generated;
+          *generated = addr;
+          addr->reply = store_get(sizeof(reply_item));
+          memset(addr->reply,0,sizeof(reply_item)); /* XXX */
+          addr->reply->to = string_copy(sender_address);
+          if (from.length==-1)
+            addr->reply->from = expand_string(US"$local_part@$domain");
+          else
+            addr->reply->from = from.character;
+          /* Allocation is larger than neccessary, but enough even for split MIME words */
+          buffer_capacity=32+4*subject.length;
+          buffer=store_get(buffer_capacity);
+          addr->reply->subject=parse_quote_2047(subject.character, subject.length, US"utf-8", buffer, buffer_capacity, TRUE);
+          addr->reply->oncelog=once;
+          addr->reply->once_repeat=days*86400;
+
+          /* build body and MIME headers */
+
+          if (reason_is_mime)
+            {
+            uschar *mime_body,*reason_end;
+            static const uschar nlnl[]="\r\n\r\n";
+
+            for
+              (
+              mime_body=reason.character,reason_end=reason.character+reason.length;
+              mime_body<(reason_end-(sizeof(nlnl)-1)) && memcmp(mime_body,nlnl,(sizeof(nlnl)-1));
+              ++mime_body
+              );
+            capacity = 0;
+            start = 0;
+            addr->reply->headers = string_cat(NULL,&capacity,&start,reason.character,mime_body-reason.character);
+            addr->reply->headers[start] = '\0';
+            capacity = 0;
+            start = 0;
+            if (mime_body+(sizeof(nlnl)-1)<reason_end) mime_body+=(sizeof(nlnl)-1);
+            else mime_body=reason_end-1;
+            addr->reply->text = string_cat(NULL,&capacity,&start,mime_body,reason_end-mime_body);
+            addr->reply->text[start] = '\0';
+            }
+          else
+            {
+            struct String qp = { NULL, 0 };  /* Keep compiler happy (PH) */
+
+            capacity = 0;
+            start = reason.length;
+            addr->reply->headers = US"MIME-Version: 1.0\n"
+                                   "Content-Type: text/plain;\n"
+                                   "\tcharset=\"utf-8\"\n"
+                                   "Content-Transfer-Encoding: quoted-printable";
+            addr->reply->text = quoted_printable_encode(&reason,&qp)->character;
+            }
+          }
+        }
+        else if ((filter_test != FTEST_NONE && debug_selector != 0) || (debug_selector & D_filter) != 0)
+          {
+          debug_printf("Sieve: mail was not personal, vacation would ignore it\n");
+          }
+      }
+    }
+    else break;
+#endif
+  }
+return 1;
+}
+
+
+/*************************************************
+*       Parse and interpret a sieve filter       *
+*************************************************/
+
+/*
+Arguments:
+  filter      points to the Sieve filter including its state
+  exec        Execute parsed statements
+  generated   where to hang newly-generated addresses
+
+Returns:      1                success
+              -1               syntax or execution error
+*/
+
+static int
+parse_start(struct Sieve *filter, int exec, address_item **generated)
+{
+filter->pc=filter->filter;
+filter->line=1;
+filter->keep=1;
+filter->require_envelope=0;
+filter->require_fileinto=0;
+#ifdef ENCODED_CHARACTER
+filter->require_encoded_character=0;
+#endif
+#ifdef ENVELOPE_AUTH
+filter->require_envelope_auth=0;
+#endif
+#ifdef ENOTIFY
+filter->require_enotify=0;
+filter->notified=(struct Notification*)0;
+#endif
+#ifdef SUBADDRESS
+filter->require_subaddress=0;
+#endif
+#ifdef VACATION
+filter->require_vacation=0;
+filter->vacation_ran=0;
+#endif
+filter->require_copy=0;
+filter->require_iascii_numeric=0;
+
+if (parse_white(filter)==-1) return -1;
+
+if (exec && filter->vacation_directory != NULL && filter_test == FTEST_NONE)
+  {
+  DIR *oncelogdir;
+  struct dirent *oncelog;
+  struct stat properties;
+  time_t now;
+
+  /* clean up old vacation log databases */
+
+  oncelogdir=opendir(CS filter->vacation_directory);
+
+  if (oncelogdir ==(DIR*)0 && errno != ENOENT)
+    {
+    filter->errmsg=CUS "unable to open vacation directory";
+    return -1;
+    }
+
+  if (oncelogdir != NULL)
+    {
+    time(&now);
+
+    while ((oncelog=readdir(oncelogdir))!=(struct dirent*)0)
+      {
+      if (strlen(oncelog->d_name)==32)
+        {
+        uschar *s=string_sprintf("%s/%s",filter->vacation_directory,oncelog->d_name);
+        if (Ustat(s,&properties)==0 && (properties.st_mtime+VACATION_MAX_DAYS*86400)<now)
+          Uunlink(s);
+        }
+      }
+    closedir(oncelogdir);
+    }
+  }
+
+while (parse_identifier(filter,CUS "require"))
+  {
+  /*
+  require-command = "require" <capabilities: string-list>
+  */
+
+  struct String *cap,*check;
+  int m;
+
+  if (parse_white(filter)==-1) return -1;
+  if ((m=parse_stringlist(filter,&cap))!=1)
+    {
+    if (m==0) filter->errmsg=CUS "capability string list expected";
+    return -1;
+    }
+  for (check=cap; check->character; ++check)
+    {
+    if (eq_octet(check,&str_envelope,0)) filter->require_envelope=1;
+    else if (eq_octet(check,&str_fileinto,0)) filter->require_fileinto=1;
+#ifdef ENCODED_CHARACTER
+    else if (eq_octet(check,&str_encoded_character,0)) filter->require_encoded_character=1;
+#endif
+#ifdef ENVELOPE_AUTH
+    else if (eq_octet(check,&str_envelope_auth,0)) filter->require_envelope_auth=1;
+#endif
+#ifdef ENOTIFY
+    else if (eq_octet(check,&str_enotify,0))
+      {
+      if (filter->enotify_mailto_owner == NULL)
+        {
+        filter->errmsg=CUS "enotify disabled";
+        return -1;
+        }
+        filter->require_enotify=1;
+      }
+#endif
+#ifdef SUBADDRESS
+    else if (eq_octet(check,&str_subaddress,0)) filter->require_subaddress=1;
+#endif
+#ifdef VACATION
+    else if (eq_octet(check,&str_vacation,0))
+      {
+      if (filter_test == FTEST_NONE && filter->vacation_directory == NULL)
+        {
+        filter->errmsg=CUS "vacation disabled";
+        return -1;
+        }
+      filter->require_vacation=1;
+      }
+#endif
+    else if (eq_octet(check,&str_copy,0)) filter->require_copy=1;
+    else if (eq_octet(check,&str_comparator_ioctet,0)) ;
+    else if (eq_octet(check,&str_comparator_iascii_casemap,0)) ;
+    else if (eq_octet(check,&str_comparator_enascii_casemap,0)) ;
+    else if (eq_octet(check,&str_comparator_iascii_numeric,0)) filter->require_iascii_numeric=1;
+    else
+      {
+      filter->errmsg=CUS "unknown capability";
+      return -1;
+      }
+    }
+    if (parse_semicolon(filter)==-1) return -1;
+  }
+  if (parse_commands(filter,exec,generated)==-1) return -1;
+  if (*filter->pc)
+    {
+    filter->errmsg=CUS "syntax error";
+    return -1;
+    }
+  return 1;
+}
+
+
+/*************************************************
+*            Interpret a sieve filter file       *
+*************************************************/
+
+/*
+Arguments:
+  filter      points to the entire file, read into store as a single string
+  options     controls whether various special things are allowed, and requests
+              special actions (not currently used)
+  vacation_directory    where to store vacation "once" files
+  enotify_mailto_owner  owner of mailto notifications
+  useraddress string expression for :user part of address
+  subaddress  string expression for :subaddress part of address
+  generated   where to hang newly-generated addresses
+  error       where to pass back an error text
+
+Returns:      FF_DELIVERED     success, a significant action was taken
+              FF_NOTDELIVERED  success, no significant action
+              FF_DEFER         defer requested
+              FF_FAIL          fail requested
+              FF_FREEZE        freeze requested
+              FF_ERROR         there was a problem
+*/
+
+int
+sieve_interpret(uschar *filter, int options, uschar *vacation_directory,
+  uschar *enotify_mailto_owner, uschar *useraddress, uschar *subaddress,
+  address_item **generated, uschar **error)
+{
+struct Sieve sieve;
+int r;
+uschar *msg;
+
+options = options; /* Keep picky compilers happy */
+error = error;
+
+DEBUG(D_route) debug_printf("Sieve: start of processing\n");
+sieve.filter=filter;
+
+if (vacation_directory == NULL)
+  sieve.vacation_directory = NULL;
+else
+  {
+  sieve.vacation_directory=expand_string(vacation_directory);
+  if (sieve.vacation_directory == NULL)
+    {
+    *error = string_sprintf("failed to expand \"%s\" "
+      "(sieve_vacation_directory): %s", vacation_directory,
+      expand_string_message);
+    return FF_ERROR;
+    }
+  }
+
+if (enotify_mailto_owner == NULL)
+  sieve.enotify_mailto_owner = NULL;
+else
+  {
+  sieve.enotify_mailto_owner=expand_string(enotify_mailto_owner);
+  if (sieve.enotify_mailto_owner == NULL)
+    {
+    *error = string_sprintf("failed to expand \"%s\" "
+      "(sieve_enotify_mailto_owner): %s", enotify_mailto_owner,
+      expand_string_message);
+    return FF_ERROR;
+    }
+  }
+
+sieve.useraddress = useraddress == NULL ? CUS "$local_part_prefix$local_part$local_part_suffix" : useraddress;
+sieve.subaddress = subaddress;
+
+#ifdef COMPILE_SYNTAX_CHECKER
+if (parse_start(&sieve,0,generated)==1)
+#else
+if (parse_start(&sieve,1,generated)==1)
+#endif
+  {
+  if (sieve.keep)
+    {
+    add_addr(generated,US"inbox",1,0,0,0);
+    msg = string_sprintf("Implicit keep");
+    r = FF_DELIVERED;
+    }
+  else
+    {
+    msg = string_sprintf("No implicit keep");
+    r = FF_DELIVERED;
+    }
+  }
+else
+  {
+  msg = string_sprintf("Sieve error: %s in line %d",sieve.errmsg,sieve.line);
+#ifdef COMPILE_SYNTAX_CHECKER
+  r = FF_ERROR;
+  *error = msg;
+#else
+  add_addr(generated,US"inbox",1,0,0,0);
+  r = FF_DELIVERED;
+#endif
+  }
+
+#ifndef COMPILE_SYNTAX_CHECKER
+if (filter_test != FTEST_NONE) printf("%s\n", (const char*) msg);
+  else debug_printf("%s\n", msg);
+#endif
+
+DEBUG(D_route) debug_printf("Sieve: end of processing\n");
+return r;
+}
diff --git a/src/smtp_in.c b/src/smtp_in.c
new file mode 100644 (file)
index 0000000..dbaa328
--- /dev/null
@@ -0,0 +1,5046 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2014 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Functions for handling an incoming SMTP call. */
+
+
+#include "exim.h"
+
+
+/* Initialize for TCP wrappers if so configured. It appears that the macro
+HAVE_IPV6 is used in some versions of the tcpd.h header, so we unset it before
+including that header, and restore its value afterwards. */
+
+#ifdef USE_TCP_WRAPPERS
+
+  #if HAVE_IPV6
+  #define EXIM_HAVE_IPV6
+  #endif
+  #undef HAVE_IPV6
+  #include <tcpd.h>
+  #undef HAVE_IPV6
+  #ifdef EXIM_HAVE_IPV6
+  #define HAVE_IPV6 TRUE
+  #endif
+
+int allow_severity = LOG_INFO;
+int deny_severity  = LOG_NOTICE;
+uschar *tcp_wrappers_name;
+#endif
+
+
+/* Size of buffer for reading SMTP commands. We used to use 512, as defined
+by RFC 821. However, RFC 1869 specifies that this must be increased for SMTP
+commands that accept arguments, and this in particular applies to AUTH, where
+the data can be quite long.  More recently this value was 2048 in Exim; 
+however, RFC 4954 (circa 2007) recommends 12288 bytes to handle AUTH.  Clients
+such as Thunderbird will send an AUTH with an initial-response for GSSAPI. 
+The maximum size of a Kerberos ticket under Windows 2003 is 12000 bytes, and 
+we need room to handle large base64-encoded AUTHs for GSSAPI.
+*/
+
+#define smtp_cmd_buffer_size  16384
+
+/* Size of buffer for reading SMTP incoming packets */
+
+#define in_buffer_size  8192
+
+/* Structure for SMTP command list */
+
+typedef struct {
+  const char *name;
+  int len;
+  short int cmd;
+  short int has_arg;
+  short int is_mail_cmd;
+} smtp_cmd_list;
+
+/* Codes for identifying commands. We order them so that those that come first
+are those for which synchronization is always required. Checking this can help
+block some spam.  */
+
+enum {
+  /* These commands are required to be synchronized, i.e. to be the last in a
+  block of commands when pipelining. */
+
+  HELO_CMD, EHLO_CMD, DATA_CMD, /* These are listed in the pipelining */
+  VRFY_CMD, EXPN_CMD, NOOP_CMD, /* RFC as requiring synchronization */
+  ETRN_CMD,                     /* This by analogy with TURN from the RFC */
+  STARTTLS_CMD,                 /* Required by the STARTTLS RFC */
+
+  /* This is a dummy to identify the non-sync commands when pipelining */
+
+  NON_SYNC_CMD_PIPELINING,
+
+  /* These commands need not be synchronized when pipelining */
+
+  MAIL_CMD, RCPT_CMD, RSET_CMD,
+
+  /* This is a dummy to identify the non-sync commands when not pipelining */
+
+  NON_SYNC_CMD_NON_PIPELINING,
+
+  /* I have been unable to find a statement about the use of pipelining
+  with AUTH, so to be on the safe side it is here, though I kind of feel
+  it should be up there with the synchronized commands. */
+
+  AUTH_CMD,
+
+  /* I'm not sure about these, but I don't think they matter. */
+
+  QUIT_CMD, HELP_CMD,
+
+#ifdef EXPERIMENTAL_PROXY
+  PROXY_FAIL_IGNORE_CMD,
+#endif
+
+  /* These are specials that don't correspond to actual commands */
+
+  EOF_CMD, OTHER_CMD, BADARG_CMD, BADCHAR_CMD, BADSYN_CMD,
+  TOO_MANY_NONMAIL_CMD };
+
+
+/* This is a convenience macro for adding the identity of an SMTP command
+to the circular buffer that holds a list of the last n received. */
+
+#define HAD(n) \
+    smtp_connection_had[smtp_ch_index++] = n; \
+    if (smtp_ch_index >= SMTP_HBUFF_SIZE) smtp_ch_index = 0
+
+
+/*************************************************
+*                Local static variables          *
+*************************************************/
+
+static auth_instance *authenticated_by;
+static BOOL auth_advertised;
+#ifdef SUPPORT_TLS
+static BOOL tls_advertised;
+#endif
+#ifdef EXPERIMENTAL_DSN
+static BOOL dsn_advertised;
+#endif
+static BOOL esmtp;
+static BOOL helo_required = FALSE;
+static BOOL helo_verify = FALSE;
+static BOOL helo_seen;
+static BOOL helo_accept_junk;
+static BOOL count_nonmail;
+static BOOL pipelining_advertised;
+static BOOL rcpt_smtp_response_same;
+static BOOL rcpt_in_progress;
+static int  nonmail_command_count;
+static BOOL smtp_exit_function_called = 0;
+static int  synprot_error_count;
+static int  unknown_command_count;
+static int  sync_cmd_limit;
+static int  smtp_write_error = 0;
+
+static uschar *rcpt_smtp_response;
+static uschar *smtp_data_buffer;
+static uschar *smtp_cmd_data;
+
+/* We need to know the position of RSET, HELO, EHLO, AUTH, and STARTTLS. Their
+final fields of all except AUTH are forced TRUE at the start of a new message
+setup, to allow one of each between messages that is not counted as a nonmail
+command. (In fact, only one of HELO/EHLO is not counted.) Also, we have to
+allow a new EHLO after starting up TLS.
+
+AUTH is "falsely" labelled as a mail command initially, so that it doesn't get
+counted. However, the flag is changed when AUTH is received, so that multiple
+failing AUTHs will eventually hit the limit. After a successful AUTH, another
+AUTH is already forbidden. After a TLS session is started, AUTH's flag is again
+forced TRUE, to allow for the re-authentication that can happen at that point.
+
+QUIT is also "falsely" labelled as a mail command so that it doesn't up the
+count of non-mail commands and possibly provoke an error. */
+
+static smtp_cmd_list cmd_list[] = {
+  { "rset",       sizeof("rset")-1,       RSET_CMD, FALSE, FALSE },  /* First */
+  { "helo",       sizeof("helo")-1,       HELO_CMD, TRUE,  FALSE },
+  { "ehlo",       sizeof("ehlo")-1,       EHLO_CMD, TRUE,  FALSE },
+  { "auth",       sizeof("auth")-1,       AUTH_CMD, TRUE,  TRUE  },
+  #ifdef SUPPORT_TLS
+  { "starttls",   sizeof("starttls")-1,   STARTTLS_CMD, FALSE, FALSE },
+  #endif
+
+/* If you change anything above here, also fix the definitions below. */
+
+  { "mail from:", sizeof("mail from:")-1, MAIL_CMD, TRUE,  TRUE  },
+  { "rcpt to:",   sizeof("rcpt to:")-1,   RCPT_CMD, TRUE,  TRUE  },
+  { "data",       sizeof("data")-1,       DATA_CMD, FALSE, TRUE  },
+  { "quit",       sizeof("quit")-1,       QUIT_CMD, FALSE, TRUE  },
+  { "noop",       sizeof("noop")-1,       NOOP_CMD, TRUE,  FALSE },
+  { "etrn",       sizeof("etrn")-1,       ETRN_CMD, TRUE,  FALSE },
+  { "vrfy",       sizeof("vrfy")-1,       VRFY_CMD, TRUE,  FALSE },
+  { "expn",       sizeof("expn")-1,       EXPN_CMD, TRUE,  FALSE },
+  { "help",       sizeof("help")-1,       HELP_CMD, TRUE,  FALSE }
+};
+
+static smtp_cmd_list *cmd_list_end =
+  cmd_list + sizeof(cmd_list)/sizeof(smtp_cmd_list);
+
+#define CMD_LIST_RSET      0
+#define CMD_LIST_HELO      1
+#define CMD_LIST_EHLO      2
+#define CMD_LIST_AUTH      3
+#define CMD_LIST_STARTTLS  4
+
+/* This list of names is used for performing the smtp_no_mail logging action.
+It must be kept in step with the SCH_xxx enumerations. */
+
+static uschar *smtp_names[] =
+  {
+  US"NONE", US"AUTH", US"DATA", US"EHLO", US"ETRN", US"EXPN", US"HELO",
+  US"HELP", US"MAIL", US"NOOP", US"QUIT", US"RCPT", US"RSET", US"STARTTLS",
+  US"VRFY" };
+
+static uschar *protocols[] = {
+  US"local-smtp",        /* HELO */
+  US"local-smtps",       /* The rare case EHLO->STARTTLS->HELO */
+  US"local-esmtp",       /* EHLO */
+  US"local-esmtps",      /* EHLO->STARTTLS->EHLO */
+  US"local-esmtpa",      /* EHLO->AUTH */
+  US"local-esmtpsa"      /* EHLO->STARTTLS->EHLO->AUTH */
+  };
+
+#define pnormal  0
+#define pextend  2
+#define pcrpted  1  /* added to pextend or pnormal */
+#define pauthed  2  /* added to pextend */
+#define pnlocal  6  /* offset to remove "local" */
+
+/* Sanity check and validate optional args to MAIL FROM: envelope */
+enum {
+  ENV_MAIL_OPT_SIZE, ENV_MAIL_OPT_BODY, ENV_MAIL_OPT_AUTH,
+#ifndef DISABLE_PRDR
+  ENV_MAIL_OPT_PRDR,
+#endif
+#ifdef EXPERIMENTAL_DSN
+  ENV_MAIL_OPT_RET, ENV_MAIL_OPT_ENVID,
+#endif
+  ENV_MAIL_OPT_NULL
+  };
+typedef struct {
+  uschar *   name;  /* option requested during MAIL cmd */
+  int       value;  /* enum type */
+  BOOL need_value;  /* TRUE requires value (name=value pair format)
+                       FALSE is a singleton */
+  } env_mail_type_t;
+static env_mail_type_t env_mail_type_list[] = {
+    { US"SIZE",   ENV_MAIL_OPT_SIZE,   TRUE  },
+    { US"BODY",   ENV_MAIL_OPT_BODY,   TRUE  },
+    { US"AUTH",   ENV_MAIL_OPT_AUTH,   TRUE  },
+#ifndef DISABLE_PRDR
+    { US"PRDR",   ENV_MAIL_OPT_PRDR,   FALSE },
+#endif
+#ifdef EXPERIMENTAL_DSN
+    { US"RET",    ENV_MAIL_OPT_RET,    TRUE },
+    { US"ENVID",  ENV_MAIL_OPT_ENVID,  TRUE },
+#endif
+    { US"NULL",   ENV_MAIL_OPT_NULL,   FALSE }
+  };
+
+/* When reading SMTP from a remote host, we have to use our own versions of the
+C input-reading functions, in order to be able to flush the SMTP output only
+when about to read more data from the socket. This is the only way to get
+optimal performance when the client is using pipelining. Flushing for every
+command causes a separate packet and reply packet each time; saving all the
+responses up (when pipelining) combines them into one packet and one response.
+
+For simplicity, these functions are used for *all* SMTP input, not only when
+receiving over a socket. However, after setting up a secure socket (SSL), input
+is read via the OpenSSL library, and another set of functions is used instead
+(see tls.c).
+
+These functions are set in the receive_getc etc. variables and called with the
+same interface as the C functions. However, since there can only ever be
+one incoming SMTP call, we just use a single buffer and flags. There is no need
+to implement a complicated private FILE-like structure.*/
+
+static uschar *smtp_inbuffer;
+static uschar *smtp_inptr;
+static uschar *smtp_inend;
+static int     smtp_had_eof;
+static int     smtp_had_error;
+
+
+/*************************************************
+*          SMTP version of getc()                *
+*************************************************/
+
+/* This gets the next byte from the SMTP input buffer. If the buffer is empty,
+it flushes the output, and refills the buffer, with a timeout. The signal
+handler is set appropriately by the calling function. This function is not used
+after a connection has negotated itself into an TLS/SSL state.
+
+Arguments:  none
+Returns:    the next character or EOF
+*/
+
+int
+smtp_getc(void)
+{
+if (smtp_inptr >= smtp_inend)
+  {
+  int rc, save_errno;
+  fflush(smtp_out);
+  if (smtp_receive_timeout > 0) alarm(smtp_receive_timeout);
+  rc = read(fileno(smtp_in), smtp_inbuffer, in_buffer_size);
+  save_errno = errno;
+  alarm(0);
+  if (rc <= 0)
+    {
+    /* Must put the error text in fixed store, because this might be during
+    header reading, where it releases unused store above the header. */
+    if (rc < 0)
+      {
+      smtp_had_error = save_errno;
+      smtp_read_error = string_copy_malloc(
+        string_sprintf(" (error: %s)", strerror(save_errno)));
+      }
+    else smtp_had_eof = 1;
+    return EOF;
+    }
+#ifndef DISABLE_DKIM
+  dkim_exim_verify_feed(smtp_inbuffer, rc);
+#endif
+  smtp_inend = smtp_inbuffer + rc;
+  smtp_inptr = smtp_inbuffer;
+  }
+return *smtp_inptr++;
+}
+
+
+
+/*************************************************
+*          SMTP version of ungetc()              *
+*************************************************/
+
+/* Puts a character back in the input buffer. Only ever
+called once.
+
+Arguments:
+  ch           the character
+
+Returns:       the character
+*/
+
+int
+smtp_ungetc(int ch)
+{
+*(--smtp_inptr) = ch;
+return ch;
+}
+
+
+
+
+/*************************************************
+*          SMTP version of feof()                *
+*************************************************/
+
+/* Tests for a previous EOF
+
+Arguments:     none
+Returns:       non-zero if the eof flag is set
+*/
+
+int
+smtp_feof(void)
+{
+return smtp_had_eof;
+}
+
+
+
+
+/*************************************************
+*          SMTP version of ferror()              *
+*************************************************/
+
+/* Tests for a previous read error, and returns with errno
+restored to what it was when the error was detected.
+
+Arguments:     none
+Returns:       non-zero if the error flag is set
+*/
+
+int
+smtp_ferror(void)
+{
+errno = smtp_had_error;
+return smtp_had_error;
+}
+
+
+
+/*************************************************
+*      Test for characters in the SMTP buffer    *
+*************************************************/
+
+/* Used at the end of a message
+
+Arguments:     none
+Returns:       TRUE/FALSE
+*/
+
+BOOL
+smtp_buffered(void)
+{
+return smtp_inptr < smtp_inend;
+}
+
+
+
+/*************************************************
+*     Write formatted string to SMTP channel     *
+*************************************************/
+
+/* This is a separate function so that we don't have to repeat everything for
+TLS support or debugging. It is global so that the daemon and the
+authentication functions can use it. It does not return any error indication,
+because major problems such as dropped connections won't show up till an output
+flush for non-TLS connections. The smtp_fflush() function is available for
+checking that: for convenience, TLS output errors are remembered here so that
+they are also picked up later by smtp_fflush().
+
+Arguments:
+  format      format string
+  ...         optional arguments
+
+Returns:      nothing
+*/
+
+void
+smtp_printf(const char *format, ...)
+{
+va_list ap;
+
+va_start(ap, format);
+smtp_vprintf(format, ap);
+va_end(ap);
+}
+
+/* This is split off so that verify.c:respond_printf() can, in effect, call
+smtp_printf(), bearing in mind that in C a vararg function can't directly
+call another vararg function, only a function which accepts a va_list. */
+
+void
+smtp_vprintf(const char *format, va_list ap)
+{
+BOOL yield;
+
+yield = string_vformat(big_buffer, big_buffer_size, format, ap);
+
+DEBUG(D_receive)
+  {
+  void *reset_point = store_get(0);
+  uschar *msg_copy, *cr, *end;
+  msg_copy = string_copy(big_buffer);
+  end = msg_copy + Ustrlen(msg_copy);
+  while ((cr = Ustrchr(msg_copy, '\r')) != NULL)   /* lose CRs */
+  memmove(cr, cr + 1, (end--) - cr);
+  debug_printf("SMTP>> %s", msg_copy);
+  store_reset(reset_point);
+  }
+
+if (!yield)
+  {
+  log_write(0, LOG_MAIN|LOG_PANIC, "string too large in smtp_printf()");
+  smtp_closedown(US"Unexpected error");
+  exim_exit(EXIT_FAILURE);
+  }
+
+/* If this is the first output for a (non-batch) RCPT command, see if all RCPTs
+have had the same. Note: this code is also present in smtp_respond(). It would
+be tidier to have it only in one place, but when it was added, it was easier to
+do it that way, so as not to have to mess with the code for the RCPT command,
+which sometimes uses smtp_printf() and sometimes smtp_respond(). */
+
+if (rcpt_in_progress)
+  {
+  if (rcpt_smtp_response == NULL)
+    rcpt_smtp_response = string_copy(big_buffer);
+  else if (rcpt_smtp_response_same &&
+           Ustrcmp(rcpt_smtp_response, big_buffer) != 0)
+    rcpt_smtp_response_same = FALSE;
+  rcpt_in_progress = FALSE;
+  }
+
+/* Now write the string */
+
+#ifdef SUPPORT_TLS
+if (tls_in.active >= 0)
+  {
+  if (tls_write(TRUE, big_buffer, Ustrlen(big_buffer)) < 0)
+    smtp_write_error = -1;
+  }
+else
+#endif
+
+if (fprintf(smtp_out, "%s", big_buffer) < 0) smtp_write_error = -1;
+}
+
+
+
+/*************************************************
+*        Flush SMTP out and check for error      *
+*************************************************/
+
+/* This function isn't currently used within Exim (it detects errors when it
+tries to read the next SMTP input), but is available for use in local_scan().
+For non-TLS connections, it flushes the output and checks for errors. For
+TLS-connections, it checks for a previously-detected TLS write error.
+
+Arguments:  none
+Returns:    0 for no error; -1 after an error
+*/
+
+int
+smtp_fflush(void)
+{
+if (tls_in.active < 0 && fflush(smtp_out) != 0) smtp_write_error = -1;
+return smtp_write_error;
+}
+
+
+
+/*************************************************
+*          SMTP command read timeout             *
+*************************************************/
+
+/* Signal handler for timing out incoming SMTP commands. This attempts to
+finish off tidily.
+
+Argument: signal number (SIGALRM)
+Returns:  nothing
+*/
+
+static void
+command_timeout_handler(int sig)
+{
+sig = sig;    /* Keep picky compilers happy */
+log_write(L_lost_incoming_connection,
+          LOG_MAIN, "SMTP command timeout on%s connection from %s",
+          (tls_in.active >= 0)? " TLS" : "",
+          host_and_ident(FALSE));
+if (smtp_batched_input)
+  moan_smtp_batch(NULL, "421 SMTP command timeout");  /* Does not return */
+smtp_notquit_exit(US"command-timeout", US"421",
+  US"%s: SMTP command timeout - closing connection", smtp_active_hostname);
+exim_exit(EXIT_FAILURE);
+}
+
+
+
+/*************************************************
+*               SIGTERM received                 *
+*************************************************/
+
+/* Signal handler for handling SIGTERM. Again, try to finish tidily.
+
+Argument: signal number (SIGTERM)
+Returns:  nothing
+*/
+
+static void
+command_sigterm_handler(int sig)
+{
+sig = sig;    /* Keep picky compilers happy */
+log_write(0, LOG_MAIN, "%s closed after SIGTERM", smtp_get_connection_info());
+if (smtp_batched_input)
+  moan_smtp_batch(NULL, "421 SIGTERM received");  /* Does not return */
+smtp_notquit_exit(US"signal-exit", US"421",
+  US"%s: Service not available - closing connection", smtp_active_hostname);
+exim_exit(EXIT_FAILURE);
+}
+
+
+
+
+#ifdef EXPERIMENTAL_PROXY
+/*************************************************
+*     Restore socket timeout to previous value   *
+*************************************************/
+/* If the previous value was successfully retrieved, restore
+it before returning control to the non-proxy routines
+
+Arguments: fd     - File descriptor for input
+           get_ok - Successfully retrieved previous values
+           tvtmp  - Time struct with previous values
+           vslen  - Length of time struct
+Returns:   none
+*/
+static void
+restore_socket_timeout(int fd, int get_ok, struct timeval tvtmp, socklen_t vslen)
+{
+if (get_ok == 0)
+  setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (char *)&tvtmp, vslen);
+}
+
+/*************************************************
+*       Check if host is required proxy host     *
+*************************************************/
+/* The function determines if inbound host will be a regular smtp host
+or if it is configured that it must use Proxy Protocol.
+
+Arguments: none
+Returns:   bool
+*/
+
+static BOOL
+check_proxy_protocol_host()
+{
+int rc;
+/* Cannot configure local connection as a proxy inbound */
+if (sender_host_address == NULL) return proxy_session;
+
+rc = verify_check_this_host(&proxy_required_hosts, NULL, NULL,
+                           sender_host_address, NULL);
+if (rc == OK)
+  {
+  DEBUG(D_receive)
+    debug_printf("Detected proxy protocol configured host\n");
+  proxy_session = TRUE;
+  }
+return proxy_session;
+}
+
+
+/*************************************************
+*         Setup host for proxy protocol          *
+*************************************************/
+/* The function configures the connection based on a header from the
+inbound host to use Proxy Protocol. The specification is very exact
+so exit with an error if do not find the exact required pieces. This
+includes an incorrect number of spaces separating args.
+
+Arguments: none
+Returns:   int
+*/
+
+static BOOL
+setup_proxy_protocol_host()
+{
+union {
+  struct {
+    uschar line[108];
+  } v1;
+  struct {
+    uschar sig[12];
+    uint8_t ver_cmd;
+    uint8_t fam;
+    uint16_t len;
+    union {
+      struct { /* TCP/UDP over IPv4, len = 12 */
+        uint32_t src_addr;
+        uint32_t dst_addr;
+        uint16_t src_port;
+        uint16_t dst_port;
+      } ip4;
+      struct { /* TCP/UDP over IPv6, len = 36 */
+        uint8_t  src_addr[16];
+        uint8_t  dst_addr[16];
+        uint16_t src_port;
+        uint16_t dst_port;
+      } ip6;
+      struct { /* AF_UNIX sockets, len = 216 */
+        uschar   src_addr[108];
+        uschar   dst_addr[108];
+      } unx;
+    } addr;
+  } v2;
+} hdr;
+
+/* Temp variables used in PPv2 address:port parsing */
+uint16_t tmpport;
+char tmpip[INET_ADDRSTRLEN];
+struct sockaddr_in tmpaddr;
+char tmpip6[INET6_ADDRSTRLEN];
+struct sockaddr_in6 tmpaddr6;
+
+int get_ok = 0;
+int size, ret, fd;
+const char v2sig[12] = "\x0D\x0A\x0D\x0A\x00\x0D\x0A\x51\x55\x49\x54\x0A";
+uschar *iptype;  /* To display debug info */
+struct timeval tv;
+socklen_t vslen = 0;
+struct timeval tvtmp;
+
+vslen = sizeof(struct timeval);
+
+fd = fileno(smtp_in);
+
+/* Save current socket timeout values */
+get_ok = getsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (char *)&tvtmp,
+                    &vslen);
+
+/* Proxy Protocol host must send header within a short time
+(default 3 seconds) or it's considered invalid */
+tv.tv_sec  = PROXY_NEGOTIATION_TIMEOUT_SEC;
+tv.tv_usec = PROXY_NEGOTIATION_TIMEOUT_USEC;
+setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv,
+           sizeof(struct timeval));
+
+do
+  {
+  /* The inbound host was declared to be a Proxy Protocol host, so
+     don't do a PEEK into the data, actually slurp it up. */
+  ret = recv(fd, &hdr, sizeof(hdr), 0);
+  }
+  while (ret == -1 && errno == EINTR);
+
+if (ret == -1)
+  {
+  restore_socket_timeout(fd, get_ok, tvtmp, vslen);
+  return (errno == EAGAIN) ? 0 : ERRNO_PROXYFAIL;
+  }
+
+if (ret >= 16 &&
+    memcmp(&hdr.v2, v2sig, 12) == 0)
+  {
+  uint8_t ver, cmd;
+
+  /* May 2014: haproxy combined the version and command into one byte to
+     allow two full bytes for the length field in order to proxy SSL
+     connections.  SSL Proxy is not supported in this version of Exim, but
+     must still seperate values here. */
+  ver = (hdr.v2.ver_cmd & 0xf0) >> 4;
+  cmd = (hdr.v2.ver_cmd & 0x0f);
+
+  if (ver != 0x02)
+    {
+    DEBUG(D_receive) debug_printf("Invalid Proxy Protocol version: %d\n", ver);
+    goto proxyfail;
+    }
+  DEBUG(D_receive) debug_printf("Detected PROXYv2 header\n");
+  /* The v2 header will always be 16 bytes per the spec. */
+  size = 16 + hdr.v2.len;
+  if (ret < size)
+    {
+    DEBUG(D_receive) debug_printf("Truncated or too large PROXYv2 header (%d/%d)\n",
+                                  ret, size);
+    goto proxyfail;
+    }
+  switch (cmd)
+    {
+    case 0x01: /* PROXY command */
+      switch (hdr.v2.fam)
+        {
+        case 0x11:  /* TCPv4 address type */
+          iptype = US"IPv4";
+          tmpaddr.sin_addr.s_addr = hdr.v2.addr.ip4.src_addr;
+          inet_ntop(AF_INET, &(tmpaddr.sin_addr), (char *)&tmpip, sizeof(tmpip));
+          if (!string_is_ip_address(US tmpip,NULL))
+            {
+            DEBUG(D_receive) debug_printf("Invalid %s source IP\n", iptype);
+            return ERRNO_PROXYFAIL;
+            }
+          proxy_host_address  = sender_host_address;
+          sender_host_address = string_copy(US tmpip);
+          tmpport             = ntohs(hdr.v2.addr.ip4.src_port);
+          proxy_host_port     = sender_host_port;
+          sender_host_port    = tmpport;
+          /* Save dest ip/port */
+          tmpaddr.sin_addr.s_addr = hdr.v2.addr.ip4.dst_addr;
+          inet_ntop(AF_INET, &(tmpaddr.sin_addr), (char *)&tmpip, sizeof(tmpip));
+          if (!string_is_ip_address(US tmpip,NULL))
+            {
+            DEBUG(D_receive) debug_printf("Invalid %s dest port\n", iptype);
+            return ERRNO_PROXYFAIL;
+            }
+          proxy_target_address = string_copy(US tmpip);
+          tmpport              = ntohs(hdr.v2.addr.ip4.dst_port);
+          proxy_target_port    = tmpport;
+          goto done;
+        case 0x21:  /* TCPv6 address type */
+          iptype = US"IPv6";
+          memmove(tmpaddr6.sin6_addr.s6_addr, hdr.v2.addr.ip6.src_addr, 16);
+          inet_ntop(AF_INET6, &(tmpaddr6.sin6_addr), (char *)&tmpip6, sizeof(tmpip6));
+          if (!string_is_ip_address(US tmpip6,NULL))
+            {
+            DEBUG(D_receive) debug_printf("Invalid %s source IP\n", iptype);
+            return ERRNO_PROXYFAIL;
+            }
+          proxy_host_address  = sender_host_address;
+          sender_host_address = string_copy(US tmpip6);
+          tmpport             = ntohs(hdr.v2.addr.ip6.src_port);
+          proxy_host_port     = sender_host_port;
+          sender_host_port    = tmpport;
+          /* Save dest ip/port */
+          memmove(tmpaddr6.sin6_addr.s6_addr, hdr.v2.addr.ip6.dst_addr, 16);
+          inet_ntop(AF_INET6, &(tmpaddr6.sin6_addr), (char *)&tmpip6, sizeof(tmpip6));
+          if (!string_is_ip_address(US tmpip6,NULL))
+            {
+            DEBUG(D_receive) debug_printf("Invalid %s dest port\n", iptype);
+            return ERRNO_PROXYFAIL;
+            }
+          proxy_target_address = string_copy(US tmpip6);
+          tmpport              = ntohs(hdr.v2.addr.ip6.dst_port);
+          proxy_target_port    = tmpport;
+          goto done;
+        default:
+          DEBUG(D_receive)
+            debug_printf("Unsupported PROXYv2 connection type: 0x%02x\n",
+                         hdr.v2.fam);
+          goto proxyfail;
+        }
+      /* Unsupported protocol, keep local connection address */
+      break;
+    case 0x00: /* LOCAL command */
+      /* Keep local connection address for LOCAL */
+      break;
+    default:
+      DEBUG(D_receive)
+        debug_printf("Unsupported PROXYv2 command: 0x%x\n", cmd);
+      goto proxyfail;
+    }
+  }
+else if (ret >= 8 &&
+         memcmp(hdr.v1.line, "PROXY", 5) == 0)
+  {
+  uschar *p = string_copy(hdr.v1.line);
+  uschar *end = memchr(p, '\r', ret - 1);
+  uschar *sp;     /* Utility variables follow */
+  int     tmp_port;
+  char   *endc;
+
+  if (!end || end[1] != '\n')
+    {
+    DEBUG(D_receive) debug_printf("Partial or invalid PROXY header\n");
+    goto proxyfail;
+    }
+  *end = '\0'; /* Terminate the string */
+  size = end + 2 - hdr.v1.line; /* Skip header + CRLF */
+  DEBUG(D_receive) debug_printf("Detected PROXYv1 header\n");
+  /* Step through the string looking for the required fields. Ensure
+     strict adherance to required formatting, exit for any error. */
+  p += 5;
+  if (!isspace(*(p++)))
+    {
+    DEBUG(D_receive) debug_printf("Missing space after PROXY command\n");
+    goto proxyfail;
+    }
+  if (!Ustrncmp(p, CCS"TCP4", 4))
+    iptype = US"IPv4";
+  else if (!Ustrncmp(p,CCS"TCP6", 4))
+    iptype = US"IPv6";
+  else if (!Ustrncmp(p,CCS"UNKNOWN", 7))
+    {
+    iptype = US"Unknown";
+    goto done;
+    }
+  else
+    {
+    DEBUG(D_receive) debug_printf("Invalid TCP type\n");
+    goto proxyfail;
+    }
+
+  p += Ustrlen(iptype);
+  if (!isspace(*(p++)))
+    {
+    DEBUG(D_receive) debug_printf("Missing space after TCP4/6 command\n");
+    goto proxyfail;
+    }
+  /* Find the end of the arg */
+  if ((sp = Ustrchr(p, ' ')) == NULL)
+    {
+    DEBUG(D_receive)
+      debug_printf("Did not find proxied src %s\n", iptype);
+    goto proxyfail;
+    }
+  *sp = '\0';
+  if(!string_is_ip_address(p,NULL))
+    {
+    DEBUG(D_receive)
+      debug_printf("Proxied src arg is not an %s address\n", iptype);
+    goto proxyfail;
+    }
+  proxy_host_address = sender_host_address;
+  sender_host_address = p;
+  p = sp + 1;
+  if ((sp = Ustrchr(p, ' ')) == NULL)
+    {
+    DEBUG(D_receive)
+      debug_printf("Did not find proxy dest %s\n", iptype);
+    goto proxyfail;
+    }
+  *sp = '\0';
+  if(!string_is_ip_address(p,NULL))
+    {
+    DEBUG(D_receive)
+      debug_printf("Proxy dest arg is not an %s address\n", iptype);
+    goto proxyfail;
+    }
+  proxy_target_address = p;
+  p = sp + 1;
+  if ((sp = Ustrchr(p, ' ')) == NULL)
+    {
+    DEBUG(D_receive) debug_printf("Did not find proxied src port\n");
+    goto proxyfail;
+    }
+  *sp = '\0';
+  tmp_port = strtol(CCS p,&endc,10);
+  if (*endc || tmp_port == 0)
+    {
+    DEBUG(D_receive)
+      debug_printf("Proxied src port '%s' not an integer\n", p);
+    goto proxyfail;
+    }
+  proxy_host_port = sender_host_port;
+  sender_host_port = tmp_port;
+  p = sp + 1;
+  if ((sp = Ustrchr(p, '\0')) == NULL)
+    {
+    DEBUG(D_receive) debug_printf("Did not find proxy dest port\n");
+    goto proxyfail;
+    }
+  tmp_port = strtol(CCS p,&endc,10);
+  if (*endc || tmp_port == 0)
+    {
+    DEBUG(D_receive)
+      debug_printf("Proxy dest port '%s' not an integer\n", p);
+    goto proxyfail;
+    }
+  proxy_target_port = tmp_port;
+  /* Already checked for /r /n above. Good V1 header received. */
+  goto done;
+  }
+else
+  {
+  /* Wrong protocol */
+  DEBUG(D_receive) debug_printf("Invalid proxy protocol version negotiation\n");
+  goto proxyfail;
+  }
+
+proxyfail:
+restore_socket_timeout(fd, get_ok, tvtmp, vslen);
+/* Don't flush any potential buffer contents. Any input should cause a
+   synchronization failure */
+return FALSE;
+
+done:
+restore_socket_timeout(fd, get_ok, tvtmp, vslen);
+DEBUG(D_receive)
+  debug_printf("Valid %s sender from Proxy Protocol header\n", iptype);
+return proxy_session;
+}
+#endif
+
+/*************************************************
+*           Read one command line                *
+*************************************************/
+
+/* Strictly, SMTP commands coming over the net are supposed to end with CRLF.
+There are sites that don't do this, and in any case internal SMTP probably
+should check only for LF. Consequently, we check here for LF only. The line
+ends up with [CR]LF removed from its end. If we get an overlong line, treat as
+an unknown command. The command is read into the global smtp_cmd_buffer so that
+it is available via $smtp_command.
+
+The character reading routine sets up a timeout for each block actually read
+from the input (which may contain more than one command). We set up a special
+signal handler that closes down the session on a timeout. Control does not
+return when it runs.
+
+Arguments:
+  check_sync   if TRUE, check synchronization rules if global option is TRUE
+
+Returns:       a code identifying the command (enumerated above)
+*/
+
+static int
+smtp_read_command(BOOL check_sync)
+{
+int c;
+int ptr = 0;
+smtp_cmd_list *p;
+BOOL hadnull = FALSE;
+
+os_non_restarting_signal(SIGALRM, command_timeout_handler);
+
+while ((c = (receive_getc)()) != '\n' && c != EOF)
+  {
+  if (ptr >= smtp_cmd_buffer_size)
+    {
+    os_non_restarting_signal(SIGALRM, sigalrm_handler);
+    return OTHER_CMD;
+    }
+  if (c == 0)
+    {
+    hadnull = TRUE;
+    c = '?';
+    }
+  smtp_cmd_buffer[ptr++] = c;
+  }
+
+receive_linecount++;    /* For BSMTP errors */
+os_non_restarting_signal(SIGALRM, sigalrm_handler);
+
+/* If hit end of file, return pseudo EOF command. Whether we have a
+part-line already read doesn't matter, since this is an error state. */
+
+if (c == EOF) return EOF_CMD;
+
+/* Remove any CR and white space at the end of the line, and terminate the
+string. */
+
+while (ptr > 0 && isspace(smtp_cmd_buffer[ptr-1])) ptr--;
+smtp_cmd_buffer[ptr] = 0;
+
+DEBUG(D_receive) debug_printf("SMTP<< %s\n", smtp_cmd_buffer);
+
+/* NULLs are not allowed in SMTP commands */
+
+if (hadnull) return BADCHAR_CMD;
+
+/* Scan command list and return identity, having set the data pointer
+to the start of the actual data characters. Check for SMTP synchronization
+if required. */
+
+for (p = cmd_list; p < cmd_list_end; p++)
+  {
+  #ifdef EXPERIMENTAL_PROXY
+  /* Only allow QUIT command if Proxy Protocol parsing failed */
+  if (proxy_session && proxy_session_failed)
+    {
+    if (p->cmd != QUIT_CMD)
+      continue;
+    }
+  #endif
+  if (strncmpic(smtp_cmd_buffer, US p->name, p->len) == 0 &&
+       (smtp_cmd_buffer[p->len-1] == ':' ||   /* "mail from:" or "rcpt to:" */
+        smtp_cmd_buffer[p->len] == 0 ||
+        smtp_cmd_buffer[p->len] == ' '))
+    {
+    if (smtp_inptr < smtp_inend &&                     /* Outstanding input */
+        p->cmd < sync_cmd_limit &&                     /* Command should sync */
+        check_sync &&                                  /* Local flag set */
+        smtp_enforce_sync &&                           /* Global flag set */
+        sender_host_address != NULL &&                 /* Not local input */
+        !sender_host_notsocket)                        /* Really is a socket */
+      return BADSYN_CMD;
+
+    /* The variables $smtp_command and $smtp_command_argument point into the
+    unmodified input buffer. A copy of the latter is taken for actual
+    processing, so that it can be chopped up into separate parts if necessary,
+    for example, when processing a MAIL command options such as SIZE that can
+    follow the sender address. */
+
+    smtp_cmd_argument = smtp_cmd_buffer + p->len;
+    while (isspace(*smtp_cmd_argument)) smtp_cmd_argument++;
+    Ustrcpy(smtp_data_buffer, smtp_cmd_argument);
+    smtp_cmd_data = smtp_data_buffer;
+
+    /* Count non-mail commands from those hosts that are controlled in this
+    way. The default is all hosts. We don't waste effort checking the list
+    until we get a non-mail command, but then cache the result to save checking
+    again. If there's a DEFER while checking the host, assume it's in the list.
+
+    Note that one instance of RSET, EHLO/HELO, and STARTTLS is allowed at the
+    start of each incoming message by fiddling with the value in the table. */
+
+    if (!p->is_mail_cmd)
+      {
+      if (count_nonmail == TRUE_UNSET) count_nonmail =
+        verify_check_host(&smtp_accept_max_nonmail_hosts) != FAIL;
+      if (count_nonmail && ++nonmail_command_count > smtp_accept_max_nonmail)
+        return TOO_MANY_NONMAIL_CMD;
+      }
+
+    /* If there is data for a command that does not expect it, generate the
+    error here. */
+
+    return (p->has_arg || *smtp_cmd_data == 0)? p->cmd : BADARG_CMD;
+    }
+  }
+
+#ifdef EXPERIMENTAL_PROXY
+/* Only allow QUIT command if Proxy Protocol parsing failed */
+if (proxy_session && proxy_session_failed)
+  return PROXY_FAIL_IGNORE_CMD;
+#endif
+
+/* Enforce synchronization for unknown commands */
+
+if (smtp_inptr < smtp_inend &&                     /* Outstanding input */
+    check_sync &&                                  /* Local flag set */
+    smtp_enforce_sync &&                           /* Global flag set */
+    sender_host_address != NULL &&                 /* Not local input */
+    !sender_host_notsocket)                        /* Really is a socket */
+  return BADSYN_CMD;
+
+return OTHER_CMD;
+}
+
+
+
+/*************************************************
+*          Recheck synchronization               *
+*************************************************/
+
+/* Synchronization checks can never be perfect because a packet may be on its
+way but not arrived when the check is done. Such checks can in any case only be
+done when TLS is not in use. Normally, the checks happen when commands are
+read: Exim ensures that there is no more input in the input buffer. In normal
+cases, the response to the command will be fast, and there is no further check.
+
+However, for some commands an ACL is run, and that can include delays. In those
+cases, it is useful to do another check on the input just before sending the
+response. This also applies at the start of a connection. This function does
+that check by means of the select() function, as long as the facility is not
+disabled or inappropriate. A failure of select() is ignored.
+
+When there is unwanted input, we read it so that it appears in the log of the
+error.
+
+Arguments: none
+Returns:   TRUE if all is well; FALSE if there is input pending
+*/
+
+static BOOL
+check_sync(void)
+{
+int fd, rc;
+fd_set fds;
+struct timeval tzero;
+
+if (!smtp_enforce_sync || sender_host_address == NULL ||
+    sender_host_notsocket || tls_in.active >= 0)
+  return TRUE;
+
+fd = fileno(smtp_in);
+FD_ZERO(&fds);
+FD_SET(fd, &fds);
+tzero.tv_sec = 0;
+tzero.tv_usec = 0;
+rc = select(fd + 1, (SELECT_ARG2_TYPE *)&fds, NULL, NULL, &tzero);
+
+if (rc <= 0) return TRUE;     /* Not ready to read */
+rc = smtp_getc();
+if (rc < 0) return TRUE;      /* End of file or error */
+
+smtp_ungetc(rc);
+rc = smtp_inend - smtp_inptr;
+if (rc > 150) rc = 150;
+smtp_inptr[rc] = 0;
+return FALSE;
+}
+
+
+
+/*************************************************
+*          Forced closedown of call              *
+*************************************************/
+
+/* This function is called from log.c when Exim is dying because of a serious
+disaster, and also from some other places. If an incoming non-batched SMTP
+channel is open, it swallows the rest of the incoming message if in the DATA
+phase, sends the reply string, and gives an error to all subsequent commands
+except QUIT. The existence of an SMTP call is detected by the non-NULLness of
+smtp_in.
+
+Arguments:
+  message   SMTP reply string to send, excluding the code
+
+Returns:    nothing
+*/
+
+void
+smtp_closedown(uschar *message)
+{
+if (smtp_in == NULL || smtp_batched_input) return;
+receive_swallow_smtp();
+smtp_printf("421 %s\r\n", message);
+
+for (;;)
+  {
+  switch(smtp_read_command(FALSE))
+    {
+    case EOF_CMD:
+    return;
+
+    case QUIT_CMD:
+    smtp_printf("221 %s closing connection\r\n", smtp_active_hostname);
+    mac_smtp_fflush();
+    return;
+
+    case RSET_CMD:
+    smtp_printf("250 Reset OK\r\n");
+    break;
+
+    default:
+    smtp_printf("421 %s\r\n", message);
+    break;
+    }
+  }
+}
+
+
+
+
+/*************************************************
+*        Set up connection info for logging      *
+*************************************************/
+
+/* This function is called when logging information about an SMTP connection.
+It sets up appropriate source information, depending on the type of connection.
+If sender_fullhost is NULL, we are at a very early stage of the connection;
+just use the IP address.
+
+Argument:    none
+Returns:     a string describing the connection
+*/
+
+uschar *
+smtp_get_connection_info(void)
+{
+uschar *hostname = (sender_fullhost == NULL)?
+  sender_host_address : sender_fullhost;
+
+if (host_checking)
+  return string_sprintf("SMTP connection from %s", hostname);
+
+if (sender_host_unknown || sender_host_notsocket)
+  return string_sprintf("SMTP connection from %s", sender_ident);
+
+if (is_inetd)
+  return string_sprintf("SMTP connection from %s (via inetd)", hostname);
+
+if ((log_extra_selector & LX_incoming_interface) != 0 &&
+     interface_address != NULL)
+  return string_sprintf("SMTP connection from %s I=[%s]:%d", hostname,
+    interface_address, interface_port);
+
+return string_sprintf("SMTP connection from %s", hostname);
+}
+
+
+
+#ifdef SUPPORT_TLS
+/* Append TLS-related information to a log line
+
+Arguments:
+  s            String under construction: allocated string to extend, or NULL
+  sizep                Pointer to current allocation size (update on return), or NULL
+  ptrp         Pointer to index for new entries in string (update on return), or NULL
+
+Returns:       Allocated string or NULL
+*/
+static uschar *
+s_tlslog(uschar * s, int * sizep, int * ptrp)
+{
+  int size = sizep ? *sizep : 0;
+  int ptr = ptrp ? *ptrp : 0;
+
+  if ((log_extra_selector & LX_tls_cipher) != 0 && tls_in.cipher != NULL)
+    s = string_append(s, &size, &ptr, 2, US" X=", tls_in.cipher);
+  if ((log_extra_selector & LX_tls_certificate_verified) != 0 &&
+       tls_in.cipher != NULL)
+    s = string_append(s, &size, &ptr, 2, US" CV=",
+      tls_in.certificate_verified? "yes":"no");
+  if ((log_extra_selector & LX_tls_peerdn) != 0 && tls_in.peerdn != NULL)
+    s = string_append(s, &size, &ptr, 3, US" DN=\"",
+      string_printing(tls_in.peerdn), US"\"");
+  if ((log_extra_selector & LX_tls_sni) != 0 && tls_in.sni != NULL)
+    s = string_append(s, &size, &ptr, 3, US" SNI=\"",
+      string_printing(tls_in.sni), US"\"");
+
+  if (s)
+    {
+    s[ptr] = '\0';
+    if (sizep) *sizep = size;
+    if (ptrp) *ptrp = ptr;
+    }
+  return s;
+}
+#endif
+
+/*************************************************
+*      Log lack of MAIL if so configured         *
+*************************************************/
+
+/* This function is called when an SMTP session ends. If the log selector
+smtp_no_mail is set, write a log line giving some details of what has happened
+in the SMTP session.
+
+Arguments:   none
+Returns:     nothing
+*/
+
+void
+smtp_log_no_mail(void)
+{
+int size, ptr, i;
+uschar *s, *sep;
+
+if (smtp_mailcmd_count > 0 || (log_extra_selector & LX_smtp_no_mail) == 0)
+  return;
+
+s = NULL;
+size = ptr = 0;
+
+if (sender_host_authenticated != NULL)
+  {
+  s = string_append(s, &size, &ptr, 2, US" A=", sender_host_authenticated);
+  if (authenticated_id != NULL)
+    s = string_append(s, &size, &ptr, 2, US":", authenticated_id);
+  }
+
+#ifdef SUPPORT_TLS
+s = s_tlslog(s, &size, &ptr);
+#endif
+
+sep = (smtp_connection_had[SMTP_HBUFF_SIZE-1] != SCH_NONE)?
+  US" C=..." : US" C=";
+for (i = smtp_ch_index; i < SMTP_HBUFF_SIZE; i++)
+  {
+  if (smtp_connection_had[i] != SCH_NONE)
+    {
+    s = string_append(s, &size, &ptr, 2, sep,
+      smtp_names[smtp_connection_had[i]]);
+    sep = US",";
+    }
+  }
+
+for (i = 0; i < smtp_ch_index; i++)
+  {
+  s = string_append(s, &size, &ptr, 2, sep, smtp_names[smtp_connection_had[i]]);
+  sep = US",";
+  }
+
+if (s != NULL) s[ptr] = 0; else s = US"";
+log_write(0, LOG_MAIN, "no MAIL in SMTP connection from %s D=%s%s",
+  host_and_ident(FALSE),
+  readconf_printtime( (int) ((long)time(NULL) - (long)smtp_connection_start)),
+  s);
+}
+
+
+
+/*************************************************
+*   Check HELO line and set sender_helo_name     *
+*************************************************/
+
+/* Check the format of a HELO line. The data for HELO/EHLO is supposed to be
+the domain name of the sending host, or an ip literal in square brackets. The
+arrgument is placed in sender_helo_name, which is in malloc store, because it
+must persist over multiple incoming messages. If helo_accept_junk is set, this
+host is permitted to send any old junk (needed for some broken hosts).
+Otherwise, helo_allow_chars can be used for rogue characters in general
+(typically people want to let in underscores).
+
+Argument:
+  s       the data portion of the line (already past any white space)
+
+Returns:  TRUE or FALSE
+*/
+
+static BOOL
+check_helo(uschar *s)
+{
+uschar *start = s;
+uschar *end = s + Ustrlen(s);
+BOOL yield = helo_accept_junk;
+
+/* Discard any previous helo name */
+
+if (sender_helo_name != NULL)
+  {
+  store_free(sender_helo_name);
+  sender_helo_name = NULL;
+  }
+
+/* Skip tests if junk is permitted. */
+
+if (!yield)
+  {
+  /* Allow the new standard form for IPv6 address literals, namely,
+  [IPv6:....], and because someone is bound to use it, allow an equivalent
+  IPv4 form. Allow plain addresses as well. */
+
+  if (*s == '[')
+    {
+    if (end[-1] == ']')
+      {
+      end[-1] = 0;
+      if (strncmpic(s, US"[IPv6:", 6) == 0)
+        yield = (string_is_ip_address(s+6, NULL) == 6);
+      else if (strncmpic(s, US"[IPv4:", 6) == 0)
+        yield = (string_is_ip_address(s+6, NULL) == 4);
+      else
+        yield = (string_is_ip_address(s+1, NULL) != 0);
+      end[-1] = ']';
+      }
+    }
+
+  /* Non-literals must be alpha, dot, hyphen, plus any non-valid chars
+  that have been configured (usually underscore - sigh). */
+
+  else if (*s != 0)
+    {
+    yield = TRUE;
+    while (*s != 0)
+      {
+      if (!isalnum(*s) && *s != '.' && *s != '-' &&
+          Ustrchr(helo_allow_chars, *s) == NULL)
+        {
+        yield = FALSE;
+        break;
+        }
+      s++;
+      }
+    }
+  }
+
+/* Save argument if OK */
+
+if (yield) sender_helo_name = string_copy_malloc(start);
+return yield;
+}
+
+
+
+
+
+/*************************************************
+*         Extract SMTP command option            *
+*************************************************/
+
+/* This function picks the next option setting off the end of smtp_cmd_data. It
+is called for MAIL FROM and RCPT TO commands, to pick off the optional ESMTP
+things that can appear there.
+
+Arguments:
+   name           point this at the name
+   value          point this at the data string
+
+Returns:          TRUE if found an option
+*/
+
+static BOOL
+extract_option(uschar **name, uschar **value)
+{
+uschar *n;
+uschar *v = smtp_cmd_data + Ustrlen(smtp_cmd_data) - 1;
+while (isspace(*v)) v--;
+v[1] = 0;
+while (v > smtp_cmd_data && *v != '=' && !isspace(*v)) v--;
+
+n = v;
+if (*v == '=')
+{
+  while(isalpha(n[-1])) n--;
+  /* RFC says SP, but TAB seen in wild and other major MTAs accept it */
+  if (!isspace(n[-1])) return FALSE;
+  n[-1] = 0;
+}
+else
+{
+  n++;
+  if (v == smtp_cmd_data) return FALSE;
+}
+*v++ = 0;
+*name = n;
+*value = v;
+return TRUE;
+}
+
+
+
+
+
+/*************************************************
+*         Reset for new message                  *
+*************************************************/
+
+/* This function is called whenever the SMTP session is reset from
+within either of the setup functions.
+
+Argument:   the stacking pool storage reset point
+Returns:    nothing
+*/
+
+static void
+smtp_reset(void *reset_point)
+{
+store_reset(reset_point);
+recipients_list = NULL;
+rcpt_count = rcpt_defer_count = rcpt_fail_count =
+  raw_recipients_count = recipients_count = recipients_list_max = 0;
+cancel_cutthrough_connection("smtp reset");
+message_linecount = 0;
+message_size = -1;
+acl_added_headers = NULL;
+acl_removed_headers = NULL;
+queue_only_policy = FALSE;
+rcpt_smtp_response = NULL;
+rcpt_smtp_response_same = TRUE;
+rcpt_in_progress = FALSE;
+deliver_freeze = FALSE;                              /* Can be set by ACL */
+freeze_tell = freeze_tell_config;                    /* Can be set by ACL */
+fake_response = OK;                                  /* Can be set by ACL */
+#ifdef WITH_CONTENT_SCAN
+no_mbox_unspool = FALSE;                             /* Can be set by ACL */
+#endif
+submission_mode = FALSE;                             /* Can be set by ACL */
+suppress_local_fixups = suppress_local_fixups_default; /* Can be set by ACL */
+active_local_from_check = local_from_check;          /* Can be set by ACL */
+active_local_sender_retain = local_sender_retain;    /* Can be set by ACL */
+sender_address = NULL;
+submission_name = NULL;                              /* Can be set by ACL */
+raw_sender = NULL;                  /* After SMTP rewrite, before qualifying */
+sender_address_unrewritten = NULL;  /* Set only after verify rewrite */
+sender_verified_list = NULL;        /* No senders verified */
+memset(sender_address_cache, 0, sizeof(sender_address_cache));
+memset(sender_domain_cache, 0, sizeof(sender_domain_cache));
+
+#ifdef EXPERIMENTAL_DSN
+/* Reset the DSN flags */
+dsn_ret = 0;
+dsn_envid = NULL;
+#endif
+
+authenticated_sender = NULL;
+#ifdef EXPERIMENTAL_BRIGHTMAIL
+bmi_run = 0;
+bmi_verdicts = NULL;
+#endif
+#ifndef DISABLE_DKIM
+dkim_signers = NULL;
+dkim_disable_verify = FALSE;
+dkim_collect_input = FALSE;
+#endif
+#ifdef EXPERIMENTAL_SPF
+spf_header_comment = NULL;
+spf_received = NULL;
+spf_result = NULL;
+spf_smtp_comment = NULL;
+#endif
+body_linecount = body_zerocount = 0;
+
+sender_rate = sender_rate_limit = sender_rate_period = NULL;
+ratelimiters_mail = NULL;           /* Updated by ratelimit ACL condition */
+                   /* Note that ratelimiters_conn persists across resets. */
+
+/* Reset message ACL variables */
+
+acl_var_m = NULL;
+
+/* The message body variables use malloc store. They may be set if this is
+not the first message in an SMTP session and the previous message caused them
+to be referenced in an ACL. */
+
+if (message_body != NULL)
+  {
+  store_free(message_body);
+  message_body = NULL;
+  }
+
+if (message_body_end != NULL)
+  {
+  store_free(message_body_end);
+  message_body_end = NULL;
+  }
+
+/* Warning log messages are also saved in malloc store. They are saved to avoid
+repetition in the same message, but it seems right to repeat them for different
+messages. */
+
+while (acl_warn_logged != NULL)
+  {
+  string_item *this = acl_warn_logged;
+  acl_warn_logged = acl_warn_logged->next;
+  store_free(this);
+  }
+}
+
+
+
+
+
+/*************************************************
+*  Initialize for incoming batched SMTP message  *
+*************************************************/
+
+/* This function is called from smtp_setup_msg() in the case when
+smtp_batched_input is true. This happens when -bS is used to pass a whole batch
+of messages in one file with SMTP commands between them. All errors must be
+reported by sending a message, and only MAIL FROM, RCPT TO, and DATA are
+relevant. After an error on a sender, or an invalid recipient, the remainder
+of the message is skipped. The value of received_protocol is already set.
+
+Argument: none
+Returns:  > 0 message successfully started (reached DATA)
+          = 0 QUIT read or end of file reached
+          < 0 should not occur
+*/
+
+static int
+smtp_setup_batch_msg(void)
+{
+int done = 0;
+void *reset_point = store_get(0);
+
+/* Save the line count at the start of each transaction - single commands
+like HELO and RSET count as whole transactions. */
+
+bsmtp_transaction_linecount = receive_linecount;
+
+if ((receive_feof)()) return 0;   /* Treat EOF as QUIT */
+
+smtp_reset(reset_point);                /* Reset for start of message */
+
+/* Deal with SMTP commands. This loop is exited by setting done to a POSITIVE
+value. The values are 2 larger than the required yield of the function. */
+
+while (done <= 0)
+  {
+  uschar *errmess;
+  uschar *recipient = NULL;
+  int start, end, sender_domain, recipient_domain;
+
+  switch(smtp_read_command(FALSE))
+    {
+    /* The HELO/EHLO commands set sender_address_helo if they have
+    valid data; otherwise they are ignored, except that they do
+    a reset of the state. */
+
+    case HELO_CMD:
+    case EHLO_CMD:
+
+    check_helo(smtp_cmd_data);
+    /* Fall through */
+
+    case RSET_CMD:
+    smtp_reset(reset_point);
+    bsmtp_transaction_linecount = receive_linecount;
+    break;
+
+
+    /* The MAIL FROM command requires an address as an operand. All we
+    do here is to parse it for syntactic correctness. The form "<>" is
+    a special case which converts into an empty string. The start/end
+    pointers in the original are not used further for this address, as
+    it is the canonical extracted address which is all that is kept. */
+
+    case MAIL_CMD:
+    if (sender_address != NULL)
+      /* The function moan_smtp_batch() does not return. */
+      moan_smtp_batch(smtp_cmd_buffer, "503 Sender already given");
+
+    if (smtp_cmd_data[0] == 0)
+      /* The function moan_smtp_batch() does not return. */
+      moan_smtp_batch(smtp_cmd_buffer, "501 MAIL FROM must have an address operand");
+
+    /* Reset to start of message */
+
+    smtp_reset(reset_point);
+
+    /* Apply SMTP rewrite */
+
+    raw_sender = ((rewrite_existflags & rewrite_smtp) != 0)?
+      rewrite_one(smtp_cmd_data, rewrite_smtp|rewrite_smtp_sender, NULL, FALSE,
+        US"", global_rewrite_rules) : smtp_cmd_data;
+
+    /* Extract the address; the TRUE flag allows <> as valid */
+
+    raw_sender =
+      parse_extract_address(raw_sender, &errmess, &start, &end, &sender_domain,
+        TRUE);
+
+    if (raw_sender == NULL)
+      /* The function moan_smtp_batch() does not return. */
+      moan_smtp_batch(smtp_cmd_buffer, "501 %s", errmess);
+
+    sender_address = string_copy(raw_sender);
+
+    /* Qualify unqualified sender addresses if permitted to do so. */
+
+    if (sender_domain == 0 && sender_address[0] != 0 && sender_address[0] != '@')
+      {
+      if (allow_unqualified_sender)
+        {
+        sender_address = rewrite_address_qualify(sender_address, FALSE);
+        DEBUG(D_receive) debug_printf("unqualified address %s accepted "
+          "and rewritten\n", raw_sender);
+        }
+      /* The function moan_smtp_batch() does not return. */
+      else moan_smtp_batch(smtp_cmd_buffer, "501 sender address must contain "
+        "a domain");
+      }
+    break;
+
+
+    /* The RCPT TO command requires an address as an operand. All we do
+    here is to parse it for syntactic correctness. There may be any number
+    of RCPT TO commands, specifying multiple senders. We build them all into
+    a data structure that is in argc/argv format. The start/end values
+    given by parse_extract_address are not used, as we keep only the
+    extracted address. */
+
+    case RCPT_CMD:
+    if (sender_address == NULL)
+      /* The function moan_smtp_batch() does not return. */
+      moan_smtp_batch(smtp_cmd_buffer, "503 No sender yet given");
+
+    if (smtp_cmd_data[0] == 0)
+      /* The function moan_smtp_batch() does not return. */
+      moan_smtp_batch(smtp_cmd_buffer, "501 RCPT TO must have an address operand");
+
+    /* Check maximum number allowed */
+
+    if (recipients_max > 0 && recipients_count + 1 > recipients_max)
+      /* The function moan_smtp_batch() does not return. */
+      moan_smtp_batch(smtp_cmd_buffer, "%s too many recipients",
+        recipients_max_reject? "552": "452");
+
+    /* Apply SMTP rewrite, then extract address. Don't allow "<>" as a
+    recipient address */
+
+    recipient = ((rewrite_existflags & rewrite_smtp) != 0)?
+      rewrite_one(smtp_cmd_data, rewrite_smtp, NULL, FALSE, US"",
+        global_rewrite_rules) : smtp_cmd_data;
+
+    /* rfc821_domains = TRUE; << no longer needed */
+    recipient = parse_extract_address(recipient, &errmess, &start, &end,
+      &recipient_domain, FALSE);
+    /* rfc821_domains = FALSE; << no longer needed */
+
+    if (recipient == NULL)
+      /* The function moan_smtp_batch() does not return. */
+      moan_smtp_batch(smtp_cmd_buffer, "501 %s", errmess);
+
+    /* If the recipient address is unqualified, qualify it if permitted. Then
+    add it to the list of recipients. */
+
+    if (recipient_domain == 0)
+      {
+      if (allow_unqualified_recipient)
+        {
+        DEBUG(D_receive) debug_printf("unqualified address %s accepted\n",
+          recipient);
+        recipient = rewrite_address_qualify(recipient, TRUE);
+        }
+      /* The function moan_smtp_batch() does not return. */
+      else moan_smtp_batch(smtp_cmd_buffer, "501 recipient address must contain "
+        "a domain");
+      }
+    receive_add_recipient(recipient, -1);
+    break;
+
+
+    /* The DATA command is legal only if it follows successful MAIL FROM
+    and RCPT TO commands. This function is complete when a valid DATA
+    command is encountered. */
+
+    case DATA_CMD:
+    if (sender_address == NULL || recipients_count <= 0)
+      {
+      /* The function moan_smtp_batch() does not return. */
+      if (sender_address == NULL)
+        moan_smtp_batch(smtp_cmd_buffer,
+          "503 MAIL FROM:<sender> command must precede DATA");
+      else
+        moan_smtp_batch(smtp_cmd_buffer,
+          "503 RCPT TO:<recipient> must precede DATA");
+      }
+    else
+      {
+      done = 3;                      /* DATA successfully achieved */
+      message_ended = END_NOTENDED;  /* Indicate in middle of message */
+      }
+    break;
+
+
+    /* The VRFY, EXPN, HELP, ETRN, and NOOP commands are ignored. */
+
+    case VRFY_CMD:
+    case EXPN_CMD:
+    case HELP_CMD:
+    case NOOP_CMD:
+    case ETRN_CMD:
+    bsmtp_transaction_linecount = receive_linecount;
+    break;
+
+
+    case EOF_CMD:
+    case QUIT_CMD:
+    done = 2;
+    break;
+
+
+    case BADARG_CMD:
+    /* The function moan_smtp_batch() does not return. */
+    moan_smtp_batch(smtp_cmd_buffer, "501 Unexpected argument data");
+    break;
+
+
+    case BADCHAR_CMD:
+    /* The function moan_smtp_batch() does not return. */
+    moan_smtp_batch(smtp_cmd_buffer, "501 Unexpected NULL in SMTP command");
+    break;
+
+
+    default:
+    /* The function moan_smtp_batch() does not return. */
+    moan_smtp_batch(smtp_cmd_buffer, "500 Command unrecognized");
+    break;
+    }
+  }
+
+return done - 2;  /* Convert yield values */
+}
+
+
+
+
+/*************************************************
+*          Start an SMTP session                 *
+*************************************************/
+
+/* This function is called at the start of an SMTP session. Thereafter,
+smtp_setup_msg() is called to initiate each separate message. This
+function does host-specific testing, and outputs the banner line.
+
+Arguments:     none
+Returns:       FALSE if the session can not continue; something has
+               gone wrong, or the connection to the host is blocked
+*/
+
+BOOL
+smtp_start_session(void)
+{
+int size = 256;
+int ptr, esclen;
+uschar *user_msg, *log_msg;
+uschar *code, *esc;
+uschar *p, *s, *ss;
+
+smtp_connection_start = time(NULL);
+for (smtp_ch_index = 0; smtp_ch_index < SMTP_HBUFF_SIZE; smtp_ch_index++)
+  smtp_connection_had[smtp_ch_index] = SCH_NONE;
+smtp_ch_index = 0;
+
+/* Default values for certain variables */
+
+helo_seen = esmtp = helo_accept_junk = FALSE;
+smtp_mailcmd_count = 0;
+count_nonmail = TRUE_UNSET;
+synprot_error_count = unknown_command_count = nonmail_command_count = 0;
+smtp_delay_mail = smtp_rlm_base;
+auth_advertised = FALSE;
+pipelining_advertised = FALSE;
+pipelining_enable = TRUE;
+sync_cmd_limit = NON_SYNC_CMD_NON_PIPELINING;
+smtp_exit_function_called = FALSE;    /* For avoiding loop in not-quit exit */
+
+memset(sender_host_cache, 0, sizeof(sender_host_cache));
+
+/* If receiving by -bs from a trusted user, or testing with -bh, we allow
+authentication settings from -oMaa to remain in force. */
+
+if (!host_checking && !sender_host_notsocket) sender_host_authenticated = NULL;
+authenticated_by = NULL;
+
+#ifdef SUPPORT_TLS
+tls_in.cipher = tls_in.peerdn = NULL;
+tls_in.ourcert = tls_in.peercert = NULL;
+tls_in.sni = NULL;
+tls_in.ocsp = OCSP_NOT_REQ;
+tls_advertised = FALSE;
+#endif
+#ifdef EXPERIMENTAL_DSN
+dsn_advertised = FALSE;
+#endif
+
+/* Reset ACL connection variables */
+
+acl_var_c = NULL;
+
+/* Allow for trailing 0 in the command and data buffers. */
+
+smtp_cmd_buffer = (uschar *)malloc(2*smtp_cmd_buffer_size + 2);
+if (smtp_cmd_buffer == NULL)
+  log_write(0, LOG_MAIN|LOG_PANIC_DIE,
+    "malloc() failed for SMTP command buffer");
+smtp_cmd_buffer[0] = 0;
+smtp_data_buffer = smtp_cmd_buffer + smtp_cmd_buffer_size + 1;
+
+/* For batched input, the protocol setting can be overridden from the
+command line by a trusted caller. */
+
+if (smtp_batched_input)
+  {
+  if (received_protocol == NULL) received_protocol = US"local-bsmtp";
+  }
+
+/* For non-batched SMTP input, the protocol setting is forced here. It will be
+reset later if any of EHLO/AUTH/STARTTLS are received. */
+
+else
+  received_protocol =
+    protocols[pnormal] + ((sender_host_address != NULL)? pnlocal : 0);
+
+/* Set up the buffer for inputting using direct read() calls, and arrange to
+call the local functions instead of the standard C ones. */
+
+smtp_inbuffer = (uschar *)malloc(in_buffer_size);
+if (smtp_inbuffer == NULL)
+  log_write(0, LOG_MAIN|LOG_PANIC_DIE, "malloc() failed for SMTP input buffer");
+receive_getc = smtp_getc;
+receive_ungetc = smtp_ungetc;
+receive_feof = smtp_feof;
+receive_ferror = smtp_ferror;
+receive_smtp_buffered = smtp_buffered;
+smtp_inptr = smtp_inend = smtp_inbuffer;
+smtp_had_eof = smtp_had_error = 0;
+
+/* Set up the message size limit; this may be host-specific */
+
+thismessage_size_limit = expand_string_integer(message_size_limit, TRUE);
+if (expand_string_message != NULL)
+  {
+  if (thismessage_size_limit == -1)
+    log_write(0, LOG_MAIN|LOG_PANIC, "unable to expand message_size_limit: "
+      "%s", expand_string_message);
+  else
+    log_write(0, LOG_MAIN|LOG_PANIC, "invalid message_size_limit: "
+      "%s", expand_string_message);
+  smtp_closedown(US"Temporary local problem - please try later");
+  return FALSE;
+  }
+
+/* When a message is input locally via the -bs or -bS options, sender_host_
+unknown is set unless -oMa was used to force an IP address, in which case it
+is checked like a real remote connection. When -bs is used from inetd, this
+flag is not set, causing the sending host to be checked. The code that deals
+with IP source routing (if configured) is never required for -bs or -bS and
+the flag sender_host_notsocket is used to suppress it.
+
+If smtp_accept_max and smtp_accept_reserve are set, keep some connections in
+reserve for certain hosts and/or networks. */
+
+if (!sender_host_unknown)
+  {
+  int rc;
+  BOOL reserved_host = FALSE;
+
+  /* Look up IP options (source routing info) on the socket if this is not an
+  -oMa "host", and if any are found, log them and drop the connection.
+
+  Linux (and others now, see below) is different to everyone else, so there
+  has to be some conditional compilation here. Versions of Linux before 2.1.15
+  used a structure whose name was "options". Somebody finally realized that
+  this name was silly, and it got changed to "ip_options". I use the
+  newer name here, but there is a fudge in the script that sets up os.h
+  to define a macro in older Linux systems.
+
+  Sigh. Linux is a fast-moving target. Another generation of Linux uses
+  glibc 2, which has chosen ip_opts for the structure name. This is now
+  really a glibc thing rather than a Linux thing, so the condition name
+  has been changed to reflect this. It is relevant also to GNU/Hurd.
+
+  Mac OS 10.x (Darwin) is like the later glibc versions, but without the
+  setting of the __GLIBC__ macro, so we can't detect it automatically. There's
+  a special macro defined in the os.h file.
+
+  Some DGUX versions on older hardware appear not to support IP options at
+  all, so there is now a general macro which can be set to cut out this
+  support altogether.
+
+  How to do this properly in IPv6 is not yet known. */
+
+  #if !HAVE_IPV6 && !defined(NO_IP_OPTIONS)
+
+  #ifdef GLIBC_IP_OPTIONS
+    #if (!defined __GLIBC__) || (__GLIBC__ < 2)
+    #define OPTSTYLE 1
+    #else
+    #define OPTSTYLE 2
+    #endif
+  #elif defined DARWIN_IP_OPTIONS
+    #define OPTSTYLE 2
+  #else
+    #define OPTSTYLE 3
+  #endif
+
+  if (!host_checking && !sender_host_notsocket)
+    {
+    #if OPTSTYLE == 1
+    EXIM_SOCKLEN_T optlen = sizeof(struct ip_options) + MAX_IPOPTLEN;
+    struct ip_options *ipopt = store_get(optlen);
+    #elif OPTSTYLE == 2
+    struct ip_opts ipoptblock;
+    struct ip_opts *ipopt = &ipoptblock;
+    EXIM_SOCKLEN_T optlen = sizeof(ipoptblock);
+    #else
+    struct ipoption ipoptblock;
+    struct ipoption *ipopt = &ipoptblock;
+    EXIM_SOCKLEN_T optlen = sizeof(ipoptblock);
+    #endif
+
+    /* Occasional genuine failures of getsockopt() have been seen - for
+    example, "reset by peer". Therefore, just log and give up on this
+    call, unless the error is ENOPROTOOPT. This error is given by systems
+    that have the interfaces but not the mechanism - e.g. GNU/Hurd at the time
+    of writing. So for that error, carry on - we just can't do an IP options
+    check. */
+
+    DEBUG(D_receive) debug_printf("checking for IP options\n");
+
+    if (getsockopt(fileno(smtp_out), IPPROTO_IP, IP_OPTIONS, (uschar *)(ipopt),
+          &optlen) < 0)
+      {
+      if (errno != ENOPROTOOPT)
+        {
+        log_write(0, LOG_MAIN, "getsockopt() failed from %s: %s",
+          host_and_ident(FALSE), strerror(errno));
+        smtp_printf("451 SMTP service not available\r\n");
+        return FALSE;
+        }
+      }
+
+    /* Deal with any IP options that are set. On the systems I have looked at,
+    the value of MAX_IPOPTLEN has been 40, meaning that there should never be
+    more logging data than will fit in big_buffer. Nevertheless, after somebody
+    questioned this code, I've added in some paranoid checking. */
+
+    else if (optlen > 0)
+      {
+      uschar *p = big_buffer;
+      uschar *pend = big_buffer + big_buffer_size;
+      uschar *opt, *adptr;
+      int optcount;
+      struct in_addr addr;
+
+      #if OPTSTYLE == 1
+      uschar *optstart = (uschar *)(ipopt->__data);
+      #elif OPTSTYLE == 2
+      uschar *optstart = (uschar *)(ipopt->ip_opts);
+      #else
+      uschar *optstart = (uschar *)(ipopt->ipopt_list);
+      #endif
+
+      DEBUG(D_receive) debug_printf("IP options exist\n");
+
+      Ustrcpy(p, "IP options on incoming call:");
+      p += Ustrlen(p);
+
+      for (opt = optstart; opt != NULL &&
+           opt < (uschar *)(ipopt) + optlen;)
+        {
+        switch (*opt)
+          {
+          case IPOPT_EOL:
+          opt = NULL;
+          break;
+
+          case IPOPT_NOP:
+          opt++;
+          break;
+
+          case IPOPT_SSRR:
+          case IPOPT_LSRR:
+          if (!string_format(p, pend-p, " %s [@%s",
+               (*opt == IPOPT_SSRR)? "SSRR" : "LSRR",
+               #if OPTSTYLE == 1
+               inet_ntoa(*((struct in_addr *)(&(ipopt->faddr))))))
+               #elif OPTSTYLE == 2
+               inet_ntoa(ipopt->ip_dst)))
+               #else
+               inet_ntoa(ipopt->ipopt_dst)))
+               #endif
+            {
+            opt = NULL;
+            break;
+            }
+
+          p += Ustrlen(p);
+          optcount = (opt[1] - 3) / sizeof(struct in_addr);
+          adptr = opt + 3;
+          while (optcount-- > 0)
+            {
+            memcpy(&addr, adptr, sizeof(addr));
+            if (!string_format(p, pend - p - 1, "%s%s",
+                  (optcount == 0)? ":" : "@", inet_ntoa(addr)))
+              {
+              opt = NULL;
+              break;
+              }
+            p += Ustrlen(p);
+            adptr += sizeof(struct in_addr);
+            }
+          *p++ = ']';
+          opt += opt[1];
+          break;
+
+          default:
+            {
+            int i;
+            if (pend - p < 4 + 3*opt[1]) { opt = NULL; break; }
+            Ustrcat(p, "[ ");
+            p += 2;
+            for (i = 0; i < opt[1]; i++)
+              {
+              sprintf(CS p, "%2.2x ", opt[i]);
+              p += 3;
+              }
+            *p++ = ']';
+            }
+          opt += opt[1];
+          break;
+          }
+        }
+
+      *p = 0;
+      log_write(0, LOG_MAIN, "%s", big_buffer);
+
+      /* Refuse any call with IP options. This is what tcpwrappers 7.5 does. */
+
+      log_write(0, LOG_MAIN|LOG_REJECT,
+        "connection from %s refused (IP options)", host_and_ident(FALSE));
+
+      smtp_printf("554 SMTP service not available\r\n");
+      return FALSE;
+      }
+
+    /* Length of options = 0 => there are no options */
+
+    else DEBUG(D_receive) debug_printf("no IP options found\n");
+    }
+  #endif  /* HAVE_IPV6 && !defined(NO_IP_OPTIONS) */
+
+  /* Set keep-alive in socket options. The option is on by default. This
+  setting is an attempt to get rid of some hanging connections that stick in
+  read() when the remote end (usually a dialup) goes away. */
+
+  if (smtp_accept_keepalive && !sender_host_notsocket)
+    ip_keepalive(fileno(smtp_out), sender_host_address, FALSE);
+
+  /* If the current host matches host_lookup, set the name by doing a
+  reverse lookup. On failure, sender_host_name will be NULL and
+  host_lookup_failed will be TRUE. This may or may not be serious - optional
+  checks later. */
+
+  if (verify_check_host(&host_lookup) == OK)
+    {
+    (void)host_name_lookup();
+    host_build_sender_fullhost();
+    }
+
+  /* Delay this until we have the full name, if it is looked up. */
+
+  set_process_info("handling incoming connection from %s",
+    host_and_ident(FALSE));
+
+  /* Start up TLS if tls_on_connect is set. This is for supporting the legacy
+  smtps port for use with older style SSL MTAs. */
+
+  #ifdef SUPPORT_TLS
+  if (tls_in.on_connect && tls_server_start(tls_require_ciphers) != OK)
+    return FALSE;
+  #endif
+
+  /* Test for explicit connection rejection */
+
+  if (verify_check_host(&host_reject_connection) == OK)
+    {
+    log_write(L_connection_reject, LOG_MAIN|LOG_REJECT, "refused connection "
+      "from %s (host_reject_connection)", host_and_ident(FALSE));
+    smtp_printf("554 SMTP service not available\r\n");
+    return FALSE;
+    }
+
+  /* Test with TCP Wrappers if so configured. There is a problem in that
+  hosts_ctl() returns 0 (deny) under a number of system failure circumstances,
+  such as disks dying. In these cases, it is desirable to reject with a 4xx
+  error instead of a 5xx error. There isn't a "right" way to detect such
+  problems. The following kludge is used: errno is zeroed before calling
+  hosts_ctl(). If the result is "reject", a 5xx error is given only if the
+  value of errno is 0 or ENOENT (which happens if /etc/hosts.{allow,deny} does
+  not exist). */
+
+  #ifdef USE_TCP_WRAPPERS
+  errno = 0;
+  tcp_wrappers_name = expand_string(tcp_wrappers_daemon_name);
+  if (tcp_wrappers_name == NULL)
+    {
+    log_write(0, LOG_MAIN|LOG_PANIC_DIE, "Expansion of \"%s\" "
+      "(tcp_wrappers_name) failed: %s", string_printing(tcp_wrappers_name),
+        expand_string_message);
+    }
+  if (!hosts_ctl(tcp_wrappers_name,
+         (sender_host_name == NULL)? STRING_UNKNOWN : CS sender_host_name,
+         (sender_host_address == NULL)? STRING_UNKNOWN : CS sender_host_address,
+         (sender_ident == NULL)? STRING_UNKNOWN : CS sender_ident))
+    {
+    if (errno == 0 || errno == ENOENT)
+      {
+      HDEBUG(D_receive) debug_printf("tcp wrappers rejection\n");
+      log_write(L_connection_reject,
+                LOG_MAIN|LOG_REJECT, "refused connection from %s "
+                "(tcp wrappers)", host_and_ident(FALSE));
+      smtp_printf("554 SMTP service not available\r\n");
+      }
+    else
+      {
+      int save_errno = errno;
+      HDEBUG(D_receive) debug_printf("tcp wrappers rejected with unexpected "
+        "errno value %d\n", save_errno);
+      log_write(L_connection_reject,
+                LOG_MAIN|LOG_REJECT, "temporarily refused connection from %s "
+                "(tcp wrappers errno=%d)", host_and_ident(FALSE), save_errno);
+      smtp_printf("451 Temporary local problem - please try later\r\n");
+      }
+    return FALSE;
+    }
+  #endif
+
+  /* Check for reserved slots. The value of smtp_accept_count has already been
+  incremented to include this process. */
+
+  if (smtp_accept_max > 0 &&
+      smtp_accept_count > smtp_accept_max - smtp_accept_reserve)
+    {
+    if ((rc = verify_check_host(&smtp_reserve_hosts)) != OK)
+      {
+      log_write(L_connection_reject,
+        LOG_MAIN, "temporarily refused connection from %s: not in "
+        "reserve list: connected=%d max=%d reserve=%d%s",
+        host_and_ident(FALSE), smtp_accept_count - 1, smtp_accept_max,
+        smtp_accept_reserve, (rc == DEFER)? " (lookup deferred)" : "");
+      smtp_printf("421 %s: Too many concurrent SMTP connections; "
+        "please try again later\r\n", smtp_active_hostname);
+      return FALSE;
+      }
+    reserved_host = TRUE;
+    }
+
+  /* If a load level above which only messages from reserved hosts are
+  accepted is set, check the load. For incoming calls via the daemon, the
+  check is done in the superior process if there are no reserved hosts, to
+  save a fork. In all cases, the load average will already be available
+  in a global variable at this point. */
+
+  if (smtp_load_reserve >= 0 &&
+       load_average > smtp_load_reserve &&
+       !reserved_host &&
+       verify_check_host(&smtp_reserve_hosts) != OK)
+    {
+    log_write(L_connection_reject,
+      LOG_MAIN, "temporarily refused connection from %s: not in "
+      "reserve list and load average = %.2f", host_and_ident(FALSE),
+      (double)load_average/1000.0);
+    smtp_printf("421 %s: Too much load; please try again later\r\n",
+      smtp_active_hostname);
+    return FALSE;
+    }
+
+  /* Determine whether unqualified senders or recipients are permitted
+  for this host. Unfortunately, we have to do this every time, in order to
+  set the flags so that they can be inspected when considering qualifying
+  addresses in the headers. For a site that permits no qualification, this
+  won't take long, however. */
+
+  allow_unqualified_sender =
+    verify_check_host(&sender_unqualified_hosts) == OK;
+
+  allow_unqualified_recipient =
+    verify_check_host(&recipient_unqualified_hosts) == OK;
+
+  /* Determine whether HELO/EHLO is required for this host. The requirement
+  can be hard or soft. */
+
+  helo_required = verify_check_host(&helo_verify_hosts) == OK;
+  if (!helo_required)
+    helo_verify = verify_check_host(&helo_try_verify_hosts) == OK;
+
+  /* Determine whether this hosts is permitted to send syntactic junk
+  after a HELO or EHLO command. */
+
+  helo_accept_junk = verify_check_host(&helo_accept_junk_hosts) == OK;
+  }
+
+/* For batch SMTP input we are now done. */
+
+if (smtp_batched_input) return TRUE;
+
+#ifdef EXPERIMENTAL_PROXY
+/* If valid Proxy Protocol source is connecting, set up session.
+ * Failure will not allow any SMTP function other than QUIT. */
+proxy_session = FALSE;
+proxy_session_failed = FALSE;
+if (check_proxy_protocol_host())
+  {
+  if (setup_proxy_protocol_host() == FALSE)
+    {
+    proxy_session_failed = TRUE;
+    DEBUG(D_receive)
+      debug_printf("Failure to extract proxied host, only QUIT allowed\n");
+    }
+  else
+    {
+    sender_host_name = NULL;
+    (void)host_name_lookup();
+    host_build_sender_fullhost();
+    }
+  }
+#endif
+
+/* Run the ACL if it exists */
+
+user_msg = NULL;
+if (acl_smtp_connect != NULL)
+  {
+  int rc;
+  rc = acl_check(ACL_WHERE_CONNECT, NULL, acl_smtp_connect, &user_msg,
+    &log_msg);
+  if (rc != OK)
+    {
+    (void)smtp_handle_acl_fail(ACL_WHERE_CONNECT, rc, user_msg, log_msg);
+    return FALSE;
+    }
+  }
+
+/* Output the initial message for a two-way SMTP connection. It may contain
+newlines, which then cause a multi-line response to be given. */
+
+code = US"220";   /* Default status code */
+esc = US"";       /* Default extended status code */
+esclen = 0;       /* Length of esc */
+
+if (user_msg == NULL)
+  {
+  s = expand_string(smtp_banner);
+  if (s == NULL)
+    log_write(0, LOG_MAIN|LOG_PANIC_DIE, "Expansion of \"%s\" (smtp_banner) "
+      "failed: %s", smtp_banner, expand_string_message);
+  }
+else
+  {
+  int codelen = 3;
+  s = user_msg;
+  smtp_message_code(&code, &codelen, &s, NULL);
+  if (codelen > 4)
+    {
+    esc = code + 4;
+    esclen = codelen - 4;
+    }
+  }
+
+/* Remove any terminating newlines; might as well remove trailing space too */
+
+p = s + Ustrlen(s);
+while (p > s && isspace(p[-1])) p--;
+*p = 0;
+
+/* It seems that CC:Mail is braindead, and assumes that the greeting message
+is all contained in a single IP packet. The original code wrote out the
+greeting using several calls to fprint/fputc, and on busy servers this could
+cause it to be split over more than one packet - which caused CC:Mail to fall
+over when it got the second part of the greeting after sending its first
+command. Sigh. To try to avoid this, build the complete greeting message
+first, and output it in one fell swoop. This gives a better chance of it
+ending up as a single packet. */
+
+ss = store_get(size);
+ptr = 0;
+
+p = s;
+do       /* At least once, in case we have an empty string */
+  {
+  int len;
+  uschar *linebreak = Ustrchr(p, '\n');
+  ss = string_cat(ss, &size, &ptr, code, 3);
+  if (linebreak == NULL)
+    {
+    len = Ustrlen(p);
+    ss = string_cat(ss, &size, &ptr, US" ", 1);
+    }
+  else
+    {
+    len = linebreak - p;
+    ss = string_cat(ss, &size, &ptr, US"-", 1);
+    }
+  ss = string_cat(ss, &size, &ptr, esc, esclen);
+  ss = string_cat(ss, &size, &ptr, p, len);
+  ss = string_cat(ss, &size, &ptr, US"\r\n", 2);
+  p += len;
+  if (linebreak != NULL) p++;
+  }
+while (*p != 0);
+
+ss[ptr] = 0;  /* string_cat leaves room for this */
+
+/* Before we write the banner, check that there is no input pending, unless
+this synchronisation check is disabled. */
+
+if (!check_sync())
+  {
+  log_write(0, LOG_MAIN|LOG_REJECT, "SMTP protocol "
+    "synchronization error (input sent without waiting for greeting): "
+    "rejected connection from %s input=\"%s\"", host_and_ident(TRUE),
+    string_printing(smtp_inptr));
+  smtp_printf("554 SMTP synchronization error\r\n");
+  return FALSE;
+  }
+
+/* Now output the banner */
+
+smtp_printf("%s", ss);
+return TRUE;
+}
+
+
+
+
+
+/*************************************************
+*     Handle SMTP syntax and protocol errors     *
+*************************************************/
+
+/* Write to the log for SMTP syntax errors in incoming commands, if configured
+to do so. Then transmit the error response. The return value depends on the
+number of syntax and protocol errors in this SMTP session.
+
+Arguments:
+  type      error type, given as a log flag bit
+  code      response code; <= 0 means don't send a response
+  data      data to reflect in the response (can be NULL)
+  errmess   the error message
+
+Returns:    -1   limit of syntax/protocol errors NOT exceeded
+            +1   limit of syntax/protocol errors IS exceeded
+
+These values fit in with the values of the "done" variable in the main
+processing loop in smtp_setup_msg(). */
+
+static int
+synprot_error(int type, int code, uschar *data, uschar *errmess)
+{
+int yield = -1;
+
+log_write(type, LOG_MAIN, "SMTP %s error in \"%s\" %s %s",
+  (type == L_smtp_syntax_error)? "syntax" : "protocol",
+  string_printing(smtp_cmd_buffer), host_and_ident(TRUE), errmess);
+
+if (++synprot_error_count > smtp_max_synprot_errors)
+  {
+  yield = 1;
+  log_write(0, LOG_MAIN|LOG_REJECT, "SMTP call from %s dropped: too many "
+    "syntax or protocol errors (last command was \"%s\")",
+    host_and_ident(FALSE), smtp_cmd_buffer);
+  }
+
+if (code > 0)
+  {
+  smtp_printf("%d%c%s%s%s\r\n", code, (yield == 1)? '-' : ' ',
+    (data == NULL)? US"" : data, (data == NULL)? US"" : US": ", errmess);
+  if (yield == 1)
+    smtp_printf("%d Too many syntax or protocol errors\r\n", code);
+  }
+
+return yield;
+}
+
+
+
+
+/*************************************************
+*          Log incomplete transactions           *
+*************************************************/
+
+/* This function is called after a transaction has been aborted by RSET, QUIT,
+connection drops or other errors. It logs the envelope information received
+so far in order to preserve address verification attempts.
+
+Argument:   string to indicate what aborted the transaction
+Returns:    nothing
+*/
+
+static void
+incomplete_transaction_log(uschar *what)
+{
+if (sender_address == NULL ||                 /* No transaction in progress */
+    (log_write_selector & L_smtp_incomplete_transaction) == 0  /* Not logging */
+  ) return;
+
+/* Build list of recipients for logging */
+
+if (recipients_count > 0)
+  {
+  int i;
+  raw_recipients = store_get(recipients_count * sizeof(uschar *));
+  for (i = 0; i < recipients_count; i++)
+    raw_recipients[i] = recipients_list[i].address;
+  raw_recipients_count = recipients_count;
+  }
+
+log_write(L_smtp_incomplete_transaction, LOG_MAIN|LOG_SENDER|LOG_RECIPIENTS,
+  "%s incomplete transaction (%s)", host_and_ident(TRUE), what);
+}
+
+
+
+
+/*************************************************
+*    Send SMTP response, possibly multiline      *
+*************************************************/
+
+/* There are, it seems, broken clients out there that cannot handle multiline
+responses. If no_multiline_responses is TRUE (it can be set from an ACL), we
+output nothing for non-final calls, and only the first line for anything else.
+
+Arguments:
+  code          SMTP code, may involve extended status codes
+  codelen       length of smtp code; if > 4 there's an ESC
+  final         FALSE if the last line isn't the final line
+  msg           message text, possibly containing newlines
+
+Returns:        nothing
+*/
+
+void
+smtp_respond(uschar* code, int codelen, BOOL final, uschar *msg)
+{
+int esclen = 0;
+uschar *esc = US"";
+
+if (!final && no_multiline_responses) return;
+
+if (codelen > 4)
+  {
+  esc = code + 4;
+  esclen = codelen - 4;
+  }
+
+/* If this is the first output for a (non-batch) RCPT command, see if all RCPTs
+have had the same. Note: this code is also present in smtp_printf(). It would
+be tidier to have it only in one place, but when it was added, it was easier to
+do it that way, so as not to have to mess with the code for the RCPT command,
+which sometimes uses smtp_printf() and sometimes smtp_respond(). */
+
+if (rcpt_in_progress)
+  {
+  if (rcpt_smtp_response == NULL)
+    rcpt_smtp_response = string_copy(msg);
+  else if (rcpt_smtp_response_same &&
+           Ustrcmp(rcpt_smtp_response, msg) != 0)
+    rcpt_smtp_response_same = FALSE;
+  rcpt_in_progress = FALSE;
+  }
+
+/* Not output the message, splitting it up into multiple lines if necessary. */
+
+for (;;)
+  {
+  uschar *nl = Ustrchr(msg, '\n');
+  if (nl == NULL)
+    {
+    smtp_printf("%.3s%c%.*s%s\r\n", code, final? ' ':'-', esclen, esc, msg);
+    return;
+    }
+  else if (nl[1] == 0 || no_multiline_responses)
+    {
+    smtp_printf("%.3s%c%.*s%.*s\r\n", code, final? ' ':'-', esclen, esc,
+      (int)(nl - msg), msg);
+    return;
+    }
+  else
+    {
+    smtp_printf("%.3s-%.*s%.*s\r\n", code, esclen, esc, (int)(nl - msg), msg);
+    msg = nl + 1;
+    while (isspace(*msg)) msg++;
+    }
+  }
+}
+
+
+
+
+/*************************************************
+*            Parse user SMTP message             *
+*************************************************/
+
+/* This function allows for user messages overriding the response code details
+by providing a suitable response code string at the start of the message
+user_msg. Check the message for starting with a response code and optionally an
+extended status code. If found, check that the first digit is valid, and if so,
+change the code pointer and length to use the replacement. An invalid code
+causes a panic log; in this case, if the log messages is the same as the user
+message, we must also adjust the value of the log message to show the code that
+is actually going to be used (the original one).
+
+This function is global because it is called from receive.c as well as within
+this module.
+
+Note that the code length returned includes the terminating whitespace
+character, which is always included in the regex match.
+
+Arguments:
+  code          SMTP code, may involve extended status codes
+  codelen       length of smtp code; if > 4 there's an ESC
+  msg           message text
+  log_msg       optional log message, to be adjusted with the new SMTP code
+
+Returns:        nothing
+*/
+
+void
+smtp_message_code(uschar **code, int *codelen, uschar **msg, uschar **log_msg)
+{
+int n;
+int ovector[3];
+
+if (msg == NULL || *msg == NULL) return;
+
+n = pcre_exec(regex_smtp_code, NULL, CS *msg, Ustrlen(*msg), 0,
+  PCRE_EOPT, ovector, sizeof(ovector)/sizeof(int));
+if (n < 0) return;
+
+if ((*msg)[0] != (*code)[0])
+  {
+  log_write(0, LOG_MAIN|LOG_PANIC, "configured error code starts with "
+    "incorrect digit (expected %c) in \"%s\"", (*code)[0], *msg);
+  if (log_msg != NULL && *log_msg == *msg)
+    *log_msg = string_sprintf("%s %s", *code, *log_msg + ovector[1]);
+  }
+else
+  {
+  *code = *msg;
+  *codelen = ovector[1];    /* Includes final space */
+  }
+*msg += ovector[1];         /* Chop the code off the message */
+return;
+}
+
+
+
+
+/*************************************************
+*           Handle an ACL failure                *
+*************************************************/
+
+/* This function is called when acl_check() fails. As well as calls from within
+this module, it is called from receive.c for an ACL after DATA. It sorts out
+logging the incident, and sets up the error response. A message containing
+newlines is turned into a multiline SMTP response, but for logging, only the
+first line is used.
+
+There's a table of default permanent failure response codes to use in
+globals.c, along with the table of names. VFRY is special. Despite RFC1123 it
+defaults disabled in Exim. However, discussion in connection with RFC 821bis
+(aka RFC 2821) has concluded that the response should be 252 in the disabled
+state, because there are broken clients that try VRFY before RCPT. A 5xx
+response should be given only when the address is positively known to be
+undeliverable. Sigh. Also, for ETRN, 458 is given on refusal, and for AUTH,
+503.
+
+From Exim 4.63, it is possible to override the response code details by
+providing a suitable response code string at the start of the message provided
+in user_msg. The code's first digit is checked for validity.
+
+Arguments:
+  where      where the ACL was called from
+  rc         the failure code
+  user_msg   a message that can be included in an SMTP response
+  log_msg    a message for logging
+
+Returns:     0 in most cases
+             2 if the failure code was FAIL_DROP, in which case the
+               SMTP connection should be dropped (this value fits with the
+               "done" variable in smtp_setup_msg() below)
+*/
+
+int
+smtp_handle_acl_fail(int where, int rc, uschar *user_msg, uschar *log_msg)
+{
+BOOL drop = rc == FAIL_DROP;
+int codelen = 3;
+uschar *smtp_code;
+uschar *lognl;
+uschar *sender_info = US"";
+uschar *what =
+#ifdef WITH_CONTENT_SCAN
+  (where == ACL_WHERE_MIME)? US"during MIME ACL checks" :
+#endif
+  (where == ACL_WHERE_PREDATA)? US"DATA" :
+  (where == ACL_WHERE_DATA)? US"after DATA" :
+#ifndef DISABLE_PRDR
+  (where == ACL_WHERE_PRDR)? US"after DATA PRDR" :
+#endif
+  (smtp_cmd_data == NULL)?
+    string_sprintf("%s in \"connect\" ACL", acl_wherenames[where]) :
+    string_sprintf("%s %s", acl_wherenames[where], smtp_cmd_data);
+
+if (drop) rc = FAIL;
+
+/* Set the default SMTP code, and allow a user message to change it. */
+
+smtp_code = (rc != FAIL)? US"451" : acl_wherecodes[where];
+smtp_message_code(&smtp_code, &codelen, &user_msg, &log_msg);
+
+/* We used to have sender_address here; however, there was a bug that was not
+updating sender_address after a rewrite during a verify. When this bug was
+fixed, sender_address at this point became the rewritten address. I'm not sure
+this is what should be logged, so I've changed to logging the unrewritten
+address to retain backward compatibility. */
+
+#ifndef WITH_CONTENT_SCAN
+if (where == ACL_WHERE_RCPT || where == ACL_WHERE_DATA)
+#else
+if (where == ACL_WHERE_RCPT || where == ACL_WHERE_DATA || where == ACL_WHERE_MIME)
+#endif
+  {
+  sender_info = string_sprintf("F=<%s>%s%s%s%s ",
+    sender_address_unrewritten ? sender_address_unrewritten : sender_address,
+    sender_host_authenticated ? US" A="                                    : US"",
+    sender_host_authenticated ? sender_host_authenticated                  : US"",
+    sender_host_authenticated && authenticated_id ? US":"                  : US"",
+    sender_host_authenticated && authenticated_id ? authenticated_id       : US""
+    );
+  }
+
+/* If there's been a sender verification failure with a specific message, and
+we have not sent a response about it yet, do so now, as a preliminary line for
+failures, but not defers. However, always log it for defer, and log it for fail
+unless the sender_verify_fail log selector has been turned off. */
+
+if (sender_verified_failed != NULL &&
+    !testflag(sender_verified_failed, af_sverify_told))
+  {
+  BOOL save_rcpt_in_progress = rcpt_in_progress;
+  rcpt_in_progress = FALSE;  /* So as not to treat these as the error */
+
+  setflag(sender_verified_failed, af_sverify_told);
+
+  if (rc != FAIL || (log_extra_selector & LX_sender_verify_fail) != 0)
+    log_write(0, LOG_MAIN|LOG_REJECT, "%s sender verify %s for <%s>%s",
+      host_and_ident(TRUE),
+      ((sender_verified_failed->special_action & 255) == DEFER)? "defer":"fail",
+      sender_verified_failed->address,
+      (sender_verified_failed->message == NULL)? US"" :
+      string_sprintf(": %s", sender_verified_failed->message));
+
+  if (rc == FAIL && sender_verified_failed->user_message != NULL)
+    smtp_respond(smtp_code, codelen, FALSE, string_sprintf(
+        testflag(sender_verified_failed, af_verify_pmfail)?
+          "Postmaster verification failed while checking <%s>\n%s\n"
+          "Several RFCs state that you are required to have a postmaster\n"
+          "mailbox for each mail domain. This host does not accept mail\n"
+          "from domains whose servers reject the postmaster address."
+          :
+        testflag(sender_verified_failed, af_verify_nsfail)?
+          "Callback setup failed while verifying <%s>\n%s\n"
+          "The initial connection, or a HELO or MAIL FROM:<> command was\n"
+          "rejected. Refusing MAIL FROM:<> does not help fight spam, disregards\n"
+          "RFC requirements, and stops you from receiving standard bounce\n"
+          "messages. This host does not accept mail from domains whose servers\n"
+          "refuse bounces."
+          :
+          "Verification failed for <%s>\n%s",
+        sender_verified_failed->address,
+        sender_verified_failed->user_message));
+
+  rcpt_in_progress = save_rcpt_in_progress;
+  }
+
+/* Sort out text for logging */
+
+log_msg = (log_msg == NULL)? US"" : string_sprintf(": %s", log_msg);
+lognl = Ustrchr(log_msg, '\n');
+if (lognl != NULL) *lognl = 0;
+
+/* Send permanent failure response to the command, but the code used isn't
+always a 5xx one - see comments at the start of this function. If the original
+rc was FAIL_DROP we drop the connection and yield 2. */
+
+if (rc == FAIL) smtp_respond(smtp_code, codelen, TRUE, (user_msg == NULL)?
+  US"Administrative prohibition" : user_msg);
+
+/* Send temporary failure response to the command. Don't give any details,
+unless acl_temp_details is set. This is TRUE for a callout defer, a "defer"
+verb, and for a header verify when smtp_return_error_details is set.
+
+This conditional logic is all somewhat of a mess because of the odd
+interactions between temp_details and return_error_details. One day it should
+be re-implemented in a tidier fashion. */
+
+else
+  {
+  if (acl_temp_details && user_msg != NULL)
+    {
+    if (smtp_return_error_details &&
+        sender_verified_failed != NULL &&
+        sender_verified_failed->message != NULL)
+      {
+      smtp_respond(smtp_code, codelen, FALSE, sender_verified_failed->message);
+      }
+    smtp_respond(smtp_code, codelen, TRUE, user_msg);
+    }
+  else
+    smtp_respond(smtp_code, codelen, TRUE,
+      US"Temporary local problem - please try later");
+  }
+
+/* Log the incident to the logs that are specified by log_reject_target
+(default main, reject). This can be empty to suppress logging of rejections. If
+the connection is not forcibly to be dropped, return 0. Otherwise, log why it
+is closing if required and return 2.  */
+
+if (log_reject_target != 0)
+  {
+#ifdef SUPPORT_TLS
+  uschar * s = s_tlslog(NULL, NULL, NULL);
+  if (!s) s = US"";
+#else
+  uschar * s = US"";
+#endif
+  log_write(0, log_reject_target, "%s%s %s%srejected %s%s",
+    host_and_ident(TRUE), s,
+    sender_info, (rc == FAIL)? US"" : US"temporarily ", what, log_msg);
+  }
+
+if (!drop) return 0;
+
+log_write(L_smtp_connection, LOG_MAIN, "%s closed by DROP in ACL",
+  smtp_get_connection_info());
+
+/* Run the not-quit ACL, but without any custom messages. This should not be a
+problem, because we get here only if some other ACL has issued "drop", and
+in that case, *its* custom messages will have been used above. */
+
+smtp_notquit_exit(US"acl-drop", NULL, NULL);
+return 2;
+}
+
+
+
+
+/*************************************************
+*     Handle SMTP exit when QUIT is not given    *
+*************************************************/
+
+/* This function provides a logging/statistics hook for when an SMTP connection
+is dropped on the floor or the other end goes away. It's a global function
+because it's called from receive.c as well as this module. As well as running
+the NOTQUIT ACL, if there is one, this function also outputs a final SMTP
+response, either with a custom message from the ACL, or using a default. There
+is one case, however, when no message is output - after "drop". In that case,
+the ACL that obeyed "drop" has already supplied the custom message, and NULL is
+passed to this function.
+
+In case things go wrong while processing this function, causing an error that
+may re-enter this funtion, there is a recursion check.
+
+Arguments:
+  reason          What $smtp_notquit_reason will be set to in the ACL;
+                    if NULL, the ACL is not run
+  code            The error code to return as part of the response
+  defaultrespond  The default message if there's no user_msg
+
+Returns:          Nothing
+*/
+
+void
+smtp_notquit_exit(uschar *reason, uschar *code, uschar *defaultrespond, ...)
+{
+int rc;
+uschar *user_msg = NULL;
+uschar *log_msg = NULL;
+
+/* Check for recursive acll */
+
+if (smtp_exit_function_called)
+  {
+  log_write(0, LOG_PANIC, "smtp_notquit_exit() called more than once (%s)",
+    reason);
+  return;
+  }
+smtp_exit_function_called = TRUE;
+
+/* Call the not-QUIT ACL, if there is one, unless no reason is given. */
+
+if (acl_smtp_notquit != NULL && reason != NULL)
+  {
+  smtp_notquit_reason = reason;
+  rc = acl_check(ACL_WHERE_NOTQUIT, NULL, acl_smtp_notquit, &user_msg,
+    &log_msg);
+  if (rc == ERROR)
+    log_write(0, LOG_MAIN|LOG_PANIC, "ACL for not-QUIT returned ERROR: %s",
+      log_msg);
+  }
+
+/* Write an SMTP response if we are expected to give one. As the default
+responses are all internal, they should always fit in the buffer, but code a
+warning, just in case. Note that string_vformat() still leaves a complete
+string, even if it is incomplete. */
+
+if (code != NULL && defaultrespond != NULL)
+  {
+  if (user_msg == NULL)
+    {
+    uschar buffer[128];
+    va_list ap;
+    va_start(ap, defaultrespond);
+    if (!string_vformat(buffer, sizeof(buffer), CS defaultrespond, ap))
+      log_write(0, LOG_MAIN|LOG_PANIC, "string too large in smtp_notquit_exit()");
+    smtp_printf("%s %s\r\n", code, buffer);
+    va_end(ap);
+    }
+  else
+    smtp_respond(code, 3, TRUE, user_msg);
+  mac_smtp_fflush();
+  }
+}
+
+
+
+
+/*************************************************
+*             Verify HELO argument               *
+*************************************************/
+
+/* This function is called if helo_verify_hosts or helo_try_verify_hosts is
+matched. It is also called from ACL processing if verify = helo is used and
+verification was not previously tried (i.e. helo_try_verify_hosts was not
+matched). The result of its processing is to set helo_verified and
+helo_verify_failed. These variables should both be FALSE for this function to
+be called.
+
+Note that EHLO/HELO is legitimately allowed to quote an address literal. Allow
+for IPv6 ::ffff: literals.
+
+Argument:   none
+Returns:    TRUE if testing was completed;
+            FALSE on a temporary failure
+*/
+
+BOOL
+smtp_verify_helo(void)
+{
+BOOL yield = TRUE;
+
+HDEBUG(D_receive) debug_printf("verifying EHLO/HELO argument \"%s\"\n",
+  sender_helo_name);
+
+if (sender_helo_name == NULL)
+  {
+  HDEBUG(D_receive) debug_printf("no EHLO/HELO command was issued\n");
+  }
+
+/* Deal with the case of -bs without an IP address */
+
+else if (sender_host_address == NULL)
+  {
+  HDEBUG(D_receive) debug_printf("no client IP address: assume success\n");
+  helo_verified = TRUE;
+  }
+
+/* Deal with the more common case when there is a sending IP address */
+
+else if (sender_helo_name[0] == '[')
+  {
+  helo_verified = Ustrncmp(sender_helo_name+1, sender_host_address,
+    Ustrlen(sender_host_address)) == 0;
+
+  #if HAVE_IPV6
+  if (!helo_verified)
+    {
+    if (strncmpic(sender_host_address, US"::ffff:", 7) == 0)
+      helo_verified = Ustrncmp(sender_helo_name + 1,
+        sender_host_address + 7, Ustrlen(sender_host_address) - 7) == 0;
+    }
+  #endif
+
+  HDEBUG(D_receive)
+    { if (helo_verified) debug_printf("matched host address\n"); }
+  }
+
+/* Do a reverse lookup if one hasn't already given a positive or negative
+response. If that fails, or the name doesn't match, try checking with a forward
+lookup. */
+
+else
+  {
+  if (sender_host_name == NULL && !host_lookup_failed)
+    yield = host_name_lookup() != DEFER;
+
+  /* If a host name is known, check it and all its aliases. */
+
+  if (sender_host_name != NULL)
+    {
+    helo_verified = strcmpic(sender_host_name, sender_helo_name) == 0;
+
+    if (helo_verified)
+      {
+      HDEBUG(D_receive) debug_printf("matched host name\n");
+      }
+    else
+      {
+      uschar **aliases = sender_host_aliases;
+      while (*aliases != NULL)
+        {
+        helo_verified = strcmpic(*aliases++, sender_helo_name) == 0;
+        if (helo_verified) break;
+        }
+      HDEBUG(D_receive)
+        {
+        if (helo_verified)
+          debug_printf("matched alias %s\n", *(--aliases));
+        }
+      }
+    }
+
+  /* Final attempt: try a forward lookup of the helo name */
+
+  if (!helo_verified)
+    {
+    int rc;
+    host_item h;
+    h.name = sender_helo_name;
+    h.address = NULL;
+    h.mx = MX_NONE;
+    h.next = NULL;
+    HDEBUG(D_receive) debug_printf("getting IP address for %s\n",
+      sender_helo_name);
+    rc = host_find_byname(&h, NULL, 0, NULL, TRUE);
+    if (rc == HOST_FOUND || rc == HOST_FOUND_LOCAL)
+      {
+      host_item *hh = &h;
+      while (hh != NULL)
+        {
+        if (Ustrcmp(hh->address, sender_host_address) == 0)
+          {
+          helo_verified = TRUE;
+          HDEBUG(D_receive)
+            debug_printf("IP address for %s matches calling address\n",
+              sender_helo_name);
+          break;
+          }
+        hh = hh->next;
+        }
+      }
+    }
+  }
+
+if (!helo_verified) helo_verify_failed = TRUE;  /* We've tried ... */
+return yield;
+}
+
+
+
+
+/*************************************************
+*        Send user response message              *
+*************************************************/
+
+/* This function is passed a default response code and a user message. It calls
+smtp_message_code() to check and possibly modify the response code, and then
+calls smtp_respond() to transmit the response. I put this into a function
+just to avoid a lot of repetition.
+
+Arguments:
+  code         the response code
+  user_msg     the user message
+
+Returns:       nothing
+*/
+
+static void
+smtp_user_msg(uschar *code, uschar *user_msg)
+{
+int len = 3;
+smtp_message_code(&code, &len, &user_msg, NULL);
+smtp_respond(code, len, TRUE, user_msg);
+}
+
+
+
+/*************************************************
+*       Initialize for SMTP incoming message     *
+*************************************************/
+
+/* This function conducts the initial dialogue at the start of an incoming SMTP
+message, and builds a list of recipients. However, if the incoming message
+is part of a batch (-bS option) a separate function is called since it would
+be messy having tests splattered about all over this function. This function
+therefore handles the case where interaction is occurring. The input and output
+files are set up in smtp_in and smtp_out.
+
+The global recipients_list is set to point to a vector of recipient_item
+blocks, whose number is given by recipients_count. This is extended by the
+receive_add_recipient() function. The global variable sender_address is set to
+the sender's address. The yield is +1 if a message has been successfully
+started, 0 if a QUIT command was encountered or the connection was refused from
+the particular host, or -1 if the connection was lost.
+
+Argument: none
+
+Returns:  > 0 message successfully started (reached DATA)
+          = 0 QUIT read or end of file reached or call refused
+          < 0 lost connection
+*/
+
+int
+smtp_setup_msg(void)
+{
+int done = 0;
+BOOL toomany = FALSE;
+BOOL discarded = FALSE;
+BOOL last_was_rej_mail = FALSE;
+BOOL last_was_rcpt = FALSE;
+void *reset_point = store_get(0);
+
+DEBUG(D_receive) debug_printf("smtp_setup_msg entered\n");
+
+/* Reset for start of new message. We allow one RSET not to be counted as a
+nonmail command, for those MTAs that insist on sending it between every
+message. Ditto for EHLO/HELO and for STARTTLS, to allow for going in and out of
+TLS between messages (an Exim client may do this if it has messages queued up
+for the host). Note: we do NOT reset AUTH at this point. */
+
+smtp_reset(reset_point);
+message_ended = END_NOTSTARTED;
+
+cmd_list[CMD_LIST_RSET].is_mail_cmd = TRUE;
+cmd_list[CMD_LIST_HELO].is_mail_cmd = TRUE;
+cmd_list[CMD_LIST_EHLO].is_mail_cmd = TRUE;
+#ifdef SUPPORT_TLS
+cmd_list[CMD_LIST_STARTTLS].is_mail_cmd = TRUE;
+#endif
+
+/* Set the local signal handler for SIGTERM - it tries to end off tidily */
+
+os_non_restarting_signal(SIGTERM, command_sigterm_handler);
+
+/* Batched SMTP is handled in a different function. */
+
+if (smtp_batched_input) return smtp_setup_batch_msg();
+
+/* Deal with SMTP commands. This loop is exited by setting done to a POSITIVE
+value. The values are 2 larger than the required yield of the function. */
+
+while (done <= 0)
+  {
+  uschar **argv;
+  uschar *etrn_command;
+  uschar *etrn_serialize_key;
+  uschar *errmess;
+  uschar *log_msg, *smtp_code;
+  uschar *user_msg = NULL;
+  uschar *recipient = NULL;
+  uschar *hello = NULL;
+  uschar *set_id = NULL;
+  uschar *s, *ss;
+  BOOL was_rej_mail = FALSE;
+  BOOL was_rcpt = FALSE;
+  void (*oldsignal)(int);
+  pid_t pid;
+  int start, end, sender_domain, recipient_domain;
+  int ptr, size, rc;
+  int c, i;
+  auth_instance *au;
+#ifdef EXPERIMENTAL_DSN
+  uschar *orcpt = NULL;
+  int flags;
+#endif
+
+  switch(smtp_read_command(TRUE))
+    {
+    /* The AUTH command is not permitted to occur inside a transaction, and may
+    occur successfully only once per connection. Actually, that isn't quite
+    true. When TLS is started, all previous information about a connection must
+    be discarded, so a new AUTH is permitted at that time.
+
+    AUTH may only be used when it has been advertised. However, it seems that
+    there are clients that send AUTH when it hasn't been advertised, some of
+    them even doing this after HELO. And there are MTAs that accept this. Sigh.
+    So there's a get-out that allows this to happen.
+
+    AUTH is initially labelled as a "nonmail command" so that one occurrence
+    doesn't get counted. We change the label here so that multiple failing
+    AUTHS will eventually hit the nonmail threshold. */
+
+    case AUTH_CMD:
+    HAD(SCH_AUTH);
+    authentication_failed = TRUE;
+    cmd_list[CMD_LIST_AUTH].is_mail_cmd = FALSE;
+
+    if (!auth_advertised && !allow_auth_unadvertised)
+      {
+      done = synprot_error(L_smtp_protocol_error, 503, NULL,
+        US"AUTH command used when not advertised");
+      break;
+      }
+    if (sender_host_authenticated != NULL)
+      {
+      done = synprot_error(L_smtp_protocol_error, 503, NULL,
+        US"already authenticated");
+      break;
+      }
+    if (sender_address != NULL)
+      {
+      done = synprot_error(L_smtp_protocol_error, 503, NULL,
+        US"not permitted in mail transaction");
+      break;
+      }
+
+    /* Check the ACL */
+
+    if (acl_smtp_auth != NULL)
+      {
+      rc = acl_check(ACL_WHERE_AUTH, NULL, acl_smtp_auth, &user_msg, &log_msg);
+      if (rc != OK)
+        {
+        done = smtp_handle_acl_fail(ACL_WHERE_AUTH, rc, user_msg, log_msg);
+        break;
+        }
+      }
+
+    /* Find the name of the requested authentication mechanism. */
+
+    s = smtp_cmd_data;
+    while ((c = *smtp_cmd_data) != 0 && !isspace(c))
+      {
+      if (!isalnum(c) && c != '-' && c != '_')
+        {
+        done = synprot_error(L_smtp_syntax_error, 501, NULL,
+          US"invalid character in authentication mechanism name");
+        goto COMMAND_LOOP;
+        }
+      smtp_cmd_data++;
+      }
+
+    /* If not at the end of the line, we must be at white space. Terminate the
+    name and move the pointer on to any data that may be present. */
+
+    if (*smtp_cmd_data != 0)
+      {
+      *smtp_cmd_data++ = 0;
+      while (isspace(*smtp_cmd_data)) smtp_cmd_data++;
+      }
+
+    /* Search for an authentication mechanism which is configured for use
+    as a server and which has been advertised (unless, sigh, allow_auth_
+    unadvertised is set). */
+
+    for (au = auths; au != NULL; au = au->next)
+      {
+      if (strcmpic(s, au->public_name) == 0 && au->server &&
+          (au->advertised || allow_auth_unadvertised)) break;
+      }
+
+    if (au == NULL)
+      {
+      done = synprot_error(L_smtp_protocol_error, 504, NULL,
+        string_sprintf("%s authentication mechanism not supported", s));
+      break;
+      }
+
+    /* Run the checking code, passing the remainder of the command line as
+    data. Initials the $auth<n> variables as empty. Initialize $0 empty and set
+    it as the only set numerical variable. The authenticator may set $auth<n>
+    and also set other numeric variables. The $auth<n> variables are preferred
+    nowadays; the numerical variables remain for backwards compatibility.
+
+    Afterwards, have a go at expanding the set_id string, even if
+    authentication failed - for bad passwords it can be useful to log the
+    userid. On success, require set_id to expand and exist, and put it in
+    authenticated_id. Save this in permanent store, as the working store gets
+    reset at HELO, RSET, etc. */
+
+    for (i = 0; i < AUTH_VARS; i++) auth_vars[i] = NULL;
+    expand_nmax = 0;
+    expand_nlength[0] = 0;   /* $0 contains nothing */
+
+    c = (au->info->servercode)(au, smtp_cmd_data);
+    if (au->set_id != NULL) set_id = expand_string(au->set_id);
+    expand_nmax = -1;        /* Reset numeric variables */
+    for (i = 0; i < AUTH_VARS; i++) auth_vars[i] = NULL;   /* Reset $auth<n> */
+
+    /* The value of authenticated_id is stored in the spool file and printed in
+    log lines. It must not contain binary zeros or newline characters. In
+    normal use, it never will, but when playing around or testing, this error
+    can (did) happen. To guard against this, ensure that the id contains only
+    printing characters. */
+
+    if (set_id != NULL) set_id = string_printing(set_id);
+
+    /* For the non-OK cases, set up additional logging data if set_id
+    is not empty. */
+
+    if (c != OK)
+      {
+      if (set_id != NULL && *set_id != 0)
+        set_id = string_sprintf(" (set_id=%s)", set_id);
+      else set_id = US"";
+      }
+
+    /* Switch on the result */
+
+    switch(c)
+      {
+      case OK:
+      if (au->set_id == NULL || set_id != NULL)    /* Complete success */
+        {
+        if (set_id != NULL) authenticated_id = string_copy_malloc(set_id);
+        sender_host_authenticated = au->name;
+        authentication_failed = FALSE;
+        authenticated_fail_id = NULL;   /* Impossible to already be set? */
+        received_protocol =
+          protocols[pextend + pauthed + ((tls_in.active >= 0)? pcrpted:0)] +
+            ((sender_host_address != NULL)? pnlocal : 0);
+        s = ss = US"235 Authentication succeeded";
+        authenticated_by = au;
+        break;
+        }
+
+      /* Authentication succeeded, but we failed to expand the set_id string.
+      Treat this as a temporary error. */
+
+      auth_defer_msg = expand_string_message;
+      /* Fall through */
+
+      case DEFER:
+      if (set_id != NULL) authenticated_fail_id = string_copy_malloc(set_id);
+      s = string_sprintf("435 Unable to authenticate at present%s",
+        auth_defer_user_msg);
+      ss = string_sprintf("435 Unable to authenticate at present%s: %s",
+        set_id, auth_defer_msg);
+      break;
+
+      case BAD64:
+      s = ss = US"501 Invalid base64 data";
+      break;
+
+      case CANCELLED:
+      s = ss = US"501 Authentication cancelled";
+      break;
+
+      case UNEXPECTED:
+      s = ss = US"553 Initial data not expected";
+      break;
+
+      case FAIL:
+      if (set_id != NULL) authenticated_fail_id = string_copy_malloc(set_id);
+      s = US"535 Incorrect authentication data";
+      ss = string_sprintf("535 Incorrect authentication data%s", set_id);
+      break;
+
+      default:
+      if (set_id != NULL) authenticated_fail_id = string_copy_malloc(set_id);
+      s = US"435 Internal error";
+      ss = string_sprintf("435 Internal error%s: return %d from authentication "
+        "check", set_id, c);
+      break;
+      }
+
+    smtp_printf("%s\r\n", s);
+    if (c != OK)
+      log_write(0, LOG_MAIN|LOG_REJECT, "%s authenticator failed for %s: %s",
+        au->name, host_and_ident(FALSE), ss);
+
+    break;  /* AUTH_CMD */
+
+    /* The HELO/EHLO commands are permitted to appear in the middle of a
+    session as well as at the beginning. They have the effect of a reset in
+    addition to their other functions. Their absence at the start cannot be
+    taken to be an error.
+
+    RFC 2821 says:
+
+      If the EHLO command is not acceptable to the SMTP server, 501, 500,
+      or 502 failure replies MUST be returned as appropriate.  The SMTP
+      server MUST stay in the same state after transmitting these replies
+      that it was in before the EHLO was received.
+
+    Therefore, we do not do the reset until after checking the command for
+    acceptability. This change was made for Exim release 4.11. Previously
+    it did the reset first. */
+
+    case HELO_CMD:
+    HAD(SCH_HELO);
+    hello = US"HELO";
+    esmtp = FALSE;
+    goto HELO_EHLO;
+
+    case EHLO_CMD:
+    HAD(SCH_EHLO);
+    hello = US"EHLO";
+    esmtp = TRUE;
+
+    HELO_EHLO:      /* Common code for HELO and EHLO */
+    cmd_list[CMD_LIST_HELO].is_mail_cmd = FALSE;
+    cmd_list[CMD_LIST_EHLO].is_mail_cmd = FALSE;
+
+    /* Reject the HELO if its argument was invalid or non-existent. A
+    successful check causes the argument to be saved in malloc store. */
+
+    if (!check_helo(smtp_cmd_data))
+      {
+      smtp_printf("501 Syntactically invalid %s argument(s)\r\n", hello);
+
+      log_write(0, LOG_MAIN|LOG_REJECT, "rejected %s from %s: syntactically "
+        "invalid argument(s): %s", hello, host_and_ident(FALSE),
+        (*smtp_cmd_argument == 0)? US"(no argument given)" :
+                           string_printing(smtp_cmd_argument));
+
+      if (++synprot_error_count > smtp_max_synprot_errors)
+        {
+        log_write(0, LOG_MAIN|LOG_REJECT, "SMTP call from %s dropped: too many "
+          "syntax or protocol errors (last command was \"%s\")",
+          host_and_ident(FALSE), smtp_cmd_buffer);
+        done = 1;
+        }
+
+      break;
+      }
+
+    /* If sender_host_unknown is true, we have got here via the -bs interface,
+    not called from inetd. Otherwise, we are running an IP connection and the
+    host address will be set. If the helo name is the primary name of this
+    host and we haven't done a reverse lookup, force one now. If helo_required
+    is set, ensure that the HELO name matches the actual host. If helo_verify
+    is set, do the same check, but softly. */
+
+    if (!sender_host_unknown)
+      {
+      BOOL old_helo_verified = helo_verified;
+      uschar *p = smtp_cmd_data;
+
+      while (*p != 0 && !isspace(*p)) { *p = tolower(*p); p++; }
+      *p = 0;
+
+      /* Force a reverse lookup if HELO quoted something in helo_lookup_domains
+      because otherwise the log can be confusing. */
+
+      if (sender_host_name == NULL &&
+           (deliver_domain = sender_helo_name,  /* set $domain */
+            match_isinlist(sender_helo_name, &helo_lookup_domains, 0,
+              &domainlist_anchor, NULL, MCL_DOMAIN, TRUE, NULL)) == OK)
+        (void)host_name_lookup();
+
+      /* Rebuild the fullhost info to include the HELO name (and the real name
+      if it was looked up.) */
+
+      host_build_sender_fullhost();  /* Rebuild */
+      set_process_info("handling%s incoming connection from %s",
+        (tls_in.active >= 0)? " TLS" : "", host_and_ident(FALSE));
+
+      /* Verify if configured. This doesn't give much security, but it does
+      make some people happy to be able to do it. If helo_required is set,
+      (host matches helo_verify_hosts) failure forces rejection. If helo_verify
+      is set (host matches helo_try_verify_hosts), it does not. This is perhaps
+      now obsolescent, since the verification can now be requested selectively
+      at ACL time. */
+
+      helo_verified = helo_verify_failed = FALSE;
+      if (helo_required || helo_verify)
+        {
+        BOOL tempfail = !smtp_verify_helo();
+        if (!helo_verified)
+          {
+          if (helo_required)
+            {
+            smtp_printf("%d %s argument does not match calling host\r\n",
+              tempfail? 451 : 550, hello);
+            log_write(0, LOG_MAIN|LOG_REJECT, "%srejected \"%s %s\" from %s",
+              tempfail? "temporarily " : "",
+              hello, sender_helo_name, host_and_ident(FALSE));
+            helo_verified = old_helo_verified;
+            break;                   /* End of HELO/EHLO processing */
+            }
+          HDEBUG(D_all) debug_printf("%s verification failed but host is in "
+            "helo_try_verify_hosts\n", hello);
+          }
+        }
+      }
+
+#ifdef EXPERIMENTAL_SPF
+    /* set up SPF context */
+    spf_init(sender_helo_name, sender_host_address);
+#endif
+
+    /* Apply an ACL check if one is defined; afterwards, recheck
+    synchronization in case the client started sending in a delay. */
+
+    if (acl_smtp_helo != NULL)
+      {
+      rc = acl_check(ACL_WHERE_HELO, NULL, acl_smtp_helo, &user_msg, &log_msg);
+      if (rc != OK)
+        {
+        done = smtp_handle_acl_fail(ACL_WHERE_HELO, rc, user_msg, log_msg);
+        sender_helo_name = NULL;
+        host_build_sender_fullhost();  /* Rebuild */
+        break;
+        }
+      else if (!check_sync()) goto SYNC_FAILURE;
+      }
+
+    /* Generate an OK reply. The default string includes the ident if present,
+    and also the IP address if present. Reflecting back the ident is intended
+    as a deterrent to mail forgers. For maximum efficiency, and also because
+    some broken systems expect each response to be in a single packet, arrange
+    that the entire reply is sent in one write(). */
+
+    auth_advertised = FALSE;
+    pipelining_advertised = FALSE;
+    #ifdef SUPPORT_TLS
+    tls_advertised = FALSE;
+    #endif
+    #ifdef EXPERIMENTAL_DSN
+    dsn_advertised = FALSE;
+    #endif
+
+    smtp_code = US"250 ";        /* Default response code plus space*/
+    if (user_msg == NULL)
+      {
+      s = string_sprintf("%.3s %s Hello %s%s%s",
+        smtp_code,
+        smtp_active_hostname,
+        (sender_ident == NULL)?  US"" : sender_ident,
+        (sender_ident == NULL)?  US"" : US" at ",
+        (sender_host_name == NULL)? sender_helo_name : sender_host_name);
+
+      ptr = Ustrlen(s);
+      size = ptr + 1;
+
+      if (sender_host_address != NULL)
+        {
+        s = string_cat(s, &size, &ptr, US" [", 2);
+        s = string_cat(s, &size, &ptr, sender_host_address,
+          Ustrlen(sender_host_address));
+        s = string_cat(s, &size, &ptr, US"]", 1);
+        }
+      }
+
+    /* A user-supplied EHLO greeting may not contain more than one line. Note
+    that the code returned by smtp_message_code() includes the terminating
+    whitespace character. */
+
+    else
+      {
+      char *ss;
+      int codelen = 4;
+      smtp_message_code(&smtp_code, &codelen, &user_msg, NULL);
+      s = string_sprintf("%.*s%s", codelen, smtp_code, user_msg);
+      if ((ss = strpbrk(CS s, "\r\n")) != NULL)
+        {
+        log_write(0, LOG_MAIN|LOG_PANIC, "EHLO/HELO response must not contain "
+          "newlines: message truncated: %s", string_printing(s));
+        *ss = 0;
+        }
+      ptr = Ustrlen(s);
+      size = ptr + 1;
+      }
+
+    s = string_cat(s, &size, &ptr, US"\r\n", 2);
+
+    /* If we received EHLO, we must create a multiline response which includes
+    the functions supported. */
+
+    if (esmtp)
+      {
+      s[3] = '-';
+
+      /* I'm not entirely happy with this, as an MTA is supposed to check
+      that it has enough room to accept a message of maximum size before
+      it sends this. However, there seems little point in not sending it.
+      The actual size check happens later at MAIL FROM time. By postponing it
+      till then, VRFY and EXPN can be used after EHLO when space is short. */
+
+      if (thismessage_size_limit > 0)
+        {
+        sprintf(CS big_buffer, "%.3s-SIZE %d\r\n", smtp_code,
+          thismessage_size_limit);
+        s = string_cat(s, &size, &ptr, big_buffer, Ustrlen(big_buffer));
+        }
+      else
+        {
+        s = string_cat(s, &size, &ptr, smtp_code, 3);
+        s = string_cat(s, &size, &ptr, US"-SIZE\r\n", 7);
+        }
+
+      /* Exim does not do protocol conversion or data conversion. It is 8-bit
+      clean; if it has an 8-bit character in its hand, it just sends it. It
+      cannot therefore specify 8BITMIME and remain consistent with the RFCs.
+      However, some users want this option simply in order to stop MUAs
+      mangling messages that contain top-bit-set characters. It is therefore
+      provided as an option. */
+
+      if (accept_8bitmime)
+        {
+        s = string_cat(s, &size, &ptr, smtp_code, 3);
+        s = string_cat(s, &size, &ptr, US"-8BITMIME\r\n", 11);
+        }
+
+      #ifdef EXPERIMENTAL_DSN
+      /* Advertise DSN support if configured to do so. */
+      if (verify_check_host(&dsn_advertise_hosts) != FAIL) 
+        {
+        s = string_cat(s, &size, &ptr, smtp_code, 3);
+        s = string_cat(s, &size, &ptr, US"-DSN\r\n", 6);
+        dsn_advertised = TRUE;
+        }
+      #endif
+
+      /* Advertise ETRN if there's an ACL checking whether a host is
+      permitted to issue it; a check is made when any host actually tries. */
+
+      if (acl_smtp_etrn != NULL)
+        {
+        s = string_cat(s, &size, &ptr, smtp_code, 3);
+        s = string_cat(s, &size, &ptr, US"-ETRN\r\n", 7);
+        }
+
+      /* Advertise EXPN if there's an ACL checking whether a host is
+      permitted to issue it; a check is made when any host actually tries. */
+
+      if (acl_smtp_expn != NULL)
+        {
+        s = string_cat(s, &size, &ptr, smtp_code, 3);
+        s = string_cat(s, &size, &ptr, US"-EXPN\r\n", 7);
+        }
+
+      /* Exim is quite happy with pipelining, so let the other end know that
+      it is safe to use it, unless advertising is disabled. */
+
+      if (pipelining_enable &&
+          verify_check_host(&pipelining_advertise_hosts) == OK)
+        {
+        s = string_cat(s, &size, &ptr, smtp_code, 3);
+        s = string_cat(s, &size, &ptr, US"-PIPELINING\r\n", 13);
+        sync_cmd_limit = NON_SYNC_CMD_PIPELINING;
+        pipelining_advertised = TRUE;
+        }
+
+
+      /* If any server authentication mechanisms are configured, advertise
+      them if the current host is in auth_advertise_hosts. The problem with
+      advertising always is that some clients then require users to
+      authenticate (and aren't configurable otherwise) even though it may not
+      be necessary (e.g. if the host is in host_accept_relay).
+
+      RFC 2222 states that SASL mechanism names contain only upper case
+      letters, so output the names in upper case, though we actually recognize
+      them in either case in the AUTH command. */
+
+      if (auths != NULL)
+        {
+        if (verify_check_host(&auth_advertise_hosts) == OK)
+          {
+          auth_instance *au;
+          BOOL first = TRUE;
+          for (au = auths; au != NULL; au = au->next)
+            {
+            if (au->server && (au->advertise_condition == NULL ||
+                expand_check_condition(au->advertise_condition, au->name,
+                US"authenticator")))
+              {
+              int saveptr;
+              if (first)
+                {
+                s = string_cat(s, &size, &ptr, smtp_code, 3);
+                s = string_cat(s, &size, &ptr, US"-AUTH", 5);
+                first = FALSE;
+                auth_advertised = TRUE;
+                }
+              saveptr = ptr;
+              s = string_cat(s, &size, &ptr, US" ", 1);
+              s = string_cat(s, &size, &ptr, au->public_name,
+                Ustrlen(au->public_name));
+              while (++saveptr < ptr) s[saveptr] = toupper(s[saveptr]);
+              au->advertised = TRUE;
+              }
+            else au->advertised = FALSE;
+            }
+          if (!first) s = string_cat(s, &size, &ptr, US"\r\n", 2);
+          }
+        }
+
+      /* Advertise TLS (Transport Level Security) aka SSL (Secure Socket Layer)
+      if it has been included in the binary, and the host matches
+      tls_advertise_hosts. We must *not* advertise if we are already in a
+      secure connection. */
+
+      #ifdef SUPPORT_TLS
+      if (tls_in.active < 0 &&
+          verify_check_host(&tls_advertise_hosts) != FAIL)
+        {
+        s = string_cat(s, &size, &ptr, smtp_code, 3);
+        s = string_cat(s, &size, &ptr, US"-STARTTLS\r\n", 11);
+        tls_advertised = TRUE;
+        }
+      #endif
+
+      #ifndef DISABLE_PRDR
+      /* Per Recipient Data Response, draft by Eric A. Hall extending RFC */
+      if (prdr_enable)
+        {
+        s = string_cat(s, &size, &ptr, smtp_code, 3);
+        s = string_cat(s, &size, &ptr, US"-PRDR\r\n", 7);
+       }
+      #endif
+
+      /* Finish off the multiline reply with one that is always available. */
+
+      s = string_cat(s, &size, &ptr, smtp_code, 3);
+      s = string_cat(s, &size, &ptr, US" HELP\r\n", 7);
+      }
+
+    /* Terminate the string (for debug), write it, and note that HELO/EHLO
+    has been seen. */
+
+    s[ptr] = 0;
+
+    #ifdef SUPPORT_TLS
+    if (tls_in.active >= 0) (void)tls_write(TRUE, s, ptr); else
+    #endif
+
+      {
+      int i = fwrite(s, 1, ptr, smtp_out); i = i; /* compiler quietening */
+      }
+    DEBUG(D_receive)
+      {
+      uschar *cr;
+      while ((cr = Ustrchr(s, '\r')) != NULL)   /* lose CRs */
+        memmove(cr, cr + 1, (ptr--) - (cr - s));
+      debug_printf("SMTP>> %s", s);
+      }
+    helo_seen = TRUE;
+
+    /* Reset the protocol and the state, abandoning any previous message. */
+
+    received_protocol = (esmtp?
+      protocols[pextend +
+        ((sender_host_authenticated != NULL)? pauthed : 0) +
+        ((tls_in.active >= 0)? pcrpted : 0)]
+      :
+      protocols[pnormal + ((tls_in.active >= 0)? pcrpted : 0)])
+      +
+      ((sender_host_address != NULL)? pnlocal : 0);
+
+    smtp_reset(reset_point);
+    toomany = FALSE;
+    break;   /* HELO/EHLO */
+
+
+    /* The MAIL command requires an address as an operand. All we do
+    here is to parse it for syntactic correctness. The form "<>" is
+    a special case which converts into an empty string. The start/end
+    pointers in the original are not used further for this address, as
+    it is the canonical extracted address which is all that is kept. */
+
+    case MAIL_CMD:
+    HAD(SCH_MAIL);
+    smtp_mailcmd_count++;              /* Count for limit and ratelimit */
+    was_rej_mail = TRUE;               /* Reset if accepted */
+    env_mail_type_t * mail_args;       /* Sanity check & validate args */
+
+    if (helo_required && !helo_seen)
+      {
+      smtp_printf("503 HELO or EHLO required\r\n");
+      log_write(0, LOG_MAIN|LOG_REJECT, "rejected MAIL from %s: no "
+        "HELO/EHLO given", host_and_ident(FALSE));
+      break;
+      }
+
+    if (sender_address != NULL)
+      {
+      done = synprot_error(L_smtp_protocol_error, 503, NULL,
+        US"sender already given");
+      break;
+      }
+
+    if (smtp_cmd_data[0] == 0)
+      {
+      done = synprot_error(L_smtp_protocol_error, 501, NULL,
+        US"MAIL must have an address operand");
+      break;
+      }
+
+    /* Check to see if the limit for messages per connection would be
+    exceeded by accepting further messages. */
+
+    if (smtp_accept_max_per_connection > 0 &&
+        smtp_mailcmd_count > smtp_accept_max_per_connection)
+      {
+      smtp_printf("421 too many messages in this connection\r\n");
+      log_write(0, LOG_MAIN|LOG_REJECT, "rejected MAIL command %s: too many "
+        "messages in one connection", host_and_ident(TRUE));
+      break;
+      }
+
+    /* Reset for start of message - even if this is going to fail, we
+    obviously need to throw away any previous data. */
+
+    smtp_reset(reset_point);
+    toomany = FALSE;
+    sender_data = recipient_data = NULL;
+
+    /* Loop, checking for ESMTP additions to the MAIL FROM command. */
+
+    if (esmtp) for(;;)
+      {
+      uschar *name, *value, *end;
+      unsigned long int size;
+      BOOL arg_error = FALSE;
+
+      if (!extract_option(&name, &value)) break;
+
+      for (mail_args = env_mail_type_list;
+           (char *)mail_args < (char *)env_mail_type_list + sizeof(env_mail_type_list);
+           mail_args++
+          )
+        {
+        if (strcmpic(name, mail_args->name) == 0)
+          break;
+        }
+      if (mail_args->need_value && strcmpic(value, US"") == 0)
+        break;
+
+      switch(mail_args->value)
+        {
+        /* Handle SIZE= by reading the value. We don't do the check till later,
+        in order to be able to log the sender address on failure. */
+        case ENV_MAIL_OPT_SIZE:
+          if (((size = Ustrtoul(value, &end, 10)), *end == 0))
+            {
+            if ((size == ULONG_MAX && errno == ERANGE) || size > INT_MAX)
+              size = INT_MAX;
+            message_size = (int)size;
+            }
+          else
+            arg_error = TRUE;
+          break;
+
+        /* If this session was initiated with EHLO and accept_8bitmime is set,
+        Exim will have indicated that it supports the BODY=8BITMIME option. In
+        fact, it does not support this according to the RFCs, in that it does not
+        take any special action for forwarding messages containing 8-bit
+        characters. That is why accept_8bitmime is not the default setting, but
+        some sites want the action that is provided. We recognize both "8BITMIME"
+        and "7BIT" as body types, but take no action. */
+        case ENV_MAIL_OPT_BODY:
+          if (accept_8bitmime) {
+            if (strcmpic(value, US"8BITMIME") == 0) {
+              body_8bitmime = 8;
+            } else if (strcmpic(value, US"7BIT") == 0) {
+              body_8bitmime = 7;
+            } else {
+              body_8bitmime = 0;
+              done = synprot_error(L_smtp_syntax_error, 501, NULL,
+                US"invalid data for BODY");
+              goto COMMAND_LOOP;
+            }
+            DEBUG(D_receive) debug_printf("8BITMIME: %d\n", body_8bitmime);
+           break;
+          }
+          arg_error = TRUE;
+          break;
+
+        #ifdef EXPERIMENTAL_DSN
+  
+        /* Handle the two DSN options, but only if configured to do so (which
+        will have caused "DSN" to be given in the EHLO response). The code itself
+        is included only if configured in at build time. */
+
+        case ENV_MAIL_OPT_RET:
+          if (dsn_advertised) {
+            /* Check if RET has already been set */
+            if (dsn_ret > 0) {
+              synprot_error(L_smtp_syntax_error, 501, NULL,
+                US"RET can be specified once only");
+              goto COMMAND_LOOP;
+            }
+            dsn_ret = (strcmpic(value, US"HDRS") == 0)? dsn_ret_hdrs :
+                    (strcmpic(value, US"FULL") == 0)? dsn_ret_full : 0;
+            DEBUG(D_receive) debug_printf("DSN_RET: %d\n", dsn_ret);
+            /* Check for invalid invalid value, and exit with error */
+            if (dsn_ret == 0) {
+              synprot_error(L_smtp_syntax_error, 501, NULL,
+                US"Value for RET is invalid");
+              goto COMMAND_LOOP;
+            }
+          }
+          break;
+        case ENV_MAIL_OPT_ENVID:
+          if (dsn_advertised) {
+            /* Check if the dsn envid has been already set */
+            if (dsn_envid != NULL) {
+              synprot_error(L_smtp_syntax_error, 501, NULL,
+                US"ENVID can be specified once only");
+              goto COMMAND_LOOP;
+            }
+            dsn_envid = string_copy(value);
+            DEBUG(D_receive) debug_printf("DSN_ENVID: %s\n", dsn_envid);
+          }
+          break;
+        #endif
+
+        /* Handle the AUTH extension. If the value given is not "<>" and either
+        the ACL says "yes" or there is no ACL but the sending host is
+        authenticated, we set it up as the authenticated sender. However, if the
+        authenticator set a condition to be tested, we ignore AUTH on MAIL unless
+        the condition is met. The value of AUTH is an xtext, which means that +,
+        = and cntrl chars are coded in hex; however "<>" is unaffected by this
+        coding. */
+        case ENV_MAIL_OPT_AUTH:
+          if (Ustrcmp(value, "<>") != 0)
+            {
+            int rc;
+            uschar *ignore_msg;
+
+            if (auth_xtextdecode(value, &authenticated_sender) < 0)
+              {
+              /* Put back terminator overrides for error message */
+              value[-1] = '=';
+              name[-1] = ' ';
+              done = synprot_error(L_smtp_syntax_error, 501, NULL,
+                US"invalid data for AUTH");
+              goto COMMAND_LOOP;
+              }
+            if (acl_smtp_mailauth == NULL)
+              {
+              ignore_msg = US"client not authenticated";
+              rc = (sender_host_authenticated != NULL)? OK : FAIL;
+              }
+            else
+              {
+              ignore_msg = US"rejected by ACL";
+              rc = acl_check(ACL_WHERE_MAILAUTH, NULL, acl_smtp_mailauth,
+                &user_msg, &log_msg);
+              }
+  
+            switch (rc)
+              {
+              case OK:
+              if (authenticated_by == NULL ||
+                  authenticated_by->mail_auth_condition == NULL ||
+                  expand_check_condition(authenticated_by->mail_auth_condition,
+                      authenticated_by->name, US"authenticator"))
+                break;     /* Accept the AUTH */
+  
+              ignore_msg = US"server_mail_auth_condition failed";
+              if (authenticated_id != NULL)
+                ignore_msg = string_sprintf("%s: authenticated ID=\"%s\"",
+                  ignore_msg, authenticated_id);
+  
+              /* Fall through */
+  
+              case FAIL:
+              authenticated_sender = NULL;
+              log_write(0, LOG_MAIN, "ignoring AUTH=%s from %s (%s)",
+                value, host_and_ident(TRUE), ignore_msg);
+              break;
+  
+              /* Should only get DEFER or ERROR here. Put back terminator
+              overrides for error message */
+  
+              default:
+              value[-1] = '=';
+              name[-1] = ' ';
+              (void)smtp_handle_acl_fail(ACL_WHERE_MAILAUTH, rc, user_msg,
+                log_msg);
+              goto COMMAND_LOOP;
+              }
+            }
+            break;
+
+#ifndef DISABLE_PRDR
+        case ENV_MAIL_OPT_PRDR:
+          if (prdr_enable)
+            prdr_requested = TRUE;
+          break;
+#endif
+
+        /* Unknown option. Stick back the terminator characters and break
+        the loop.  Do the name-terminator second as extract_option sets
+       value==name when it found no equal-sign.
+       An error for a malformed address will occur. */
+        default:
+          value[-1] = '=';
+          name[-1] = ' ';
+          arg_error = TRUE;
+          break;
+        }
+      /* Break out of for loop if switch() had bad argument or
+         when start of the email address is reached */
+      if (arg_error) break;
+      }
+
+    /* If we have passed the threshold for rate limiting, apply the current
+    delay, and update it for next time, provided this is a limited host. */
+
+    if (smtp_mailcmd_count > smtp_rlm_threshold &&
+        verify_check_host(&smtp_ratelimit_hosts) == OK)
+      {
+      DEBUG(D_receive) debug_printf("rate limit MAIL: delay %.3g sec\n",
+        smtp_delay_mail/1000.0);
+      millisleep((int)smtp_delay_mail);
+      smtp_delay_mail *= smtp_rlm_factor;
+      if (smtp_delay_mail > (double)smtp_rlm_limit)
+        smtp_delay_mail = (double)smtp_rlm_limit;
+      }
+
+    /* Now extract the address, first applying any SMTP-time rewriting. The
+    TRUE flag allows "<>" as a sender address. */
+
+    raw_sender = ((rewrite_existflags & rewrite_smtp) != 0)?
+      rewrite_one(smtp_cmd_data, rewrite_smtp, NULL, FALSE, US"",
+        global_rewrite_rules) : smtp_cmd_data;
+
+    /* rfc821_domains = TRUE; << no longer needed */
+    raw_sender =
+      parse_extract_address(raw_sender, &errmess, &start, &end, &sender_domain,
+        TRUE);
+    /* rfc821_domains = FALSE; << no longer needed */
+
+    if (raw_sender == NULL)
+      {
+      done = synprot_error(L_smtp_syntax_error, 501, smtp_cmd_data, errmess);
+      break;
+      }
+
+    sender_address = raw_sender;
+
+    /* If there is a configured size limit for mail, check that this message
+    doesn't exceed it. The check is postponed to this point so that the sender
+    can be logged. */
+
+    if (thismessage_size_limit > 0 && message_size > thismessage_size_limit)
+      {
+      smtp_printf("552 Message size exceeds maximum permitted\r\n");
+      log_write(L_size_reject,
+          LOG_MAIN|LOG_REJECT, "rejected MAIL FROM:<%s> %s: "
+          "message too big: size%s=%d max=%d",
+          sender_address,
+          host_and_ident(TRUE),
+          (message_size == INT_MAX)? ">" : "",
+          message_size,
+          thismessage_size_limit);
+      sender_address = NULL;
+      break;
+      }
+
+    /* Check there is enough space on the disk unless configured not to.
+    When smtp_check_spool_space is set, the check is for thismessage_size_limit
+    plus the current message - i.e. we accept the message only if it won't
+    reduce the space below the threshold. Add 5000 to the size to allow for
+    overheads such as the Received: line and storing of recipients, etc.
+    By putting the check here, even when SIZE is not given, it allow VRFY
+    and EXPN etc. to be used when space is short. */
+
+    if (!receive_check_fs(
+         (smtp_check_spool_space && message_size >= 0)?
+            message_size + 5000 : 0))
+      {
+      smtp_printf("452 Space shortage, please try later\r\n");
+      sender_address = NULL;
+      break;
+      }
+
+    /* If sender_address is unqualified, reject it, unless this is a locally
+    generated message, or the sending host or net is permitted to send
+    unqualified addresses - typically local machines behaving as MUAs -
+    in which case just qualify the address. The flag is set above at the start
+    of the SMTP connection. */
+
+    if (sender_domain == 0 && sender_address[0] != 0)
+      {
+      if (allow_unqualified_sender)
+        {
+        sender_domain = Ustrlen(sender_address) + 1;
+        sender_address = rewrite_address_qualify(sender_address, FALSE);
+        DEBUG(D_receive) debug_printf("unqualified address %s accepted\n",
+          raw_sender);
+        }
+      else
+        {
+        smtp_printf("501 %s: sender address must contain a domain\r\n",
+          smtp_cmd_data);
+        log_write(L_smtp_syntax_error,
+          LOG_MAIN|LOG_REJECT,
+          "unqualified sender rejected: <%s> %s%s",
+          raw_sender,
+          host_and_ident(TRUE),
+          host_lookup_msg);
+        sender_address = NULL;
+        break;
+        }
+      }
+
+    /* Apply an ACL check if one is defined, before responding. Afterwards,
+    when pipelining is not advertised, do another sync check in case the ACL
+    delayed and the client started sending in the meantime. */
+
+    if (acl_smtp_mail)
+      {
+      rc = acl_check(ACL_WHERE_MAIL, NULL, acl_smtp_mail, &user_msg, &log_msg);
+      if (rc == OK && !pipelining_advertised && !check_sync())
+        goto SYNC_FAILURE;
+      }
+    else
+      rc = OK;
+
+    if (rc == OK || rc == DISCARD)
+      {
+      if (!user_msg)
+        smtp_printf("%s%s%s", US"250 OK",
+                  #ifndef DISABLE_PRDR
+                    prdr_requested ? US", PRDR Requested" : US"",
+                 #else
+                    US"",
+                  #endif
+                    US"\r\n");
+      else 
+        {
+      #ifndef DISABLE_PRDR
+        if (prdr_requested)
+           user_msg = string_sprintf("%s%s", user_msg, US", PRDR Requested");
+      #endif
+        smtp_user_msg(US"250", user_msg);
+        }
+      smtp_delay_rcpt = smtp_rlr_base;
+      recipients_discarded = (rc == DISCARD);
+      was_rej_mail = FALSE;
+      }
+    else
+      {
+      done = smtp_handle_acl_fail(ACL_WHERE_MAIL, rc, user_msg, log_msg);
+      sender_address = NULL;
+      }
+    break;
+
+
+    /* The RCPT command requires an address as an operand. There may be any
+    number of RCPT commands, specifying multiple recipients. We build them all
+    into a data structure. The start/end values given by parse_extract_address
+    are not used, as we keep only the extracted address. */
+
+    case RCPT_CMD:
+    HAD(SCH_RCPT);
+    rcpt_count++;
+    was_rcpt = rcpt_in_progress = TRUE;
+
+    /* There must be a sender address; if the sender was rejected and
+    pipelining was advertised, we assume the client was pipelining, and do not
+    count this as a protocol error. Reset was_rej_mail so that further RCPTs
+    get the same treatment. */
+
+    if (sender_address == NULL)
+      {
+      if (pipelining_advertised && last_was_rej_mail)
+        {
+        smtp_printf("503 sender not yet given\r\n");
+        was_rej_mail = TRUE;
+        }
+      else
+        {
+        done = synprot_error(L_smtp_protocol_error, 503, NULL,
+          US"sender not yet given");
+        was_rcpt = FALSE;             /* Not a valid RCPT */
+        }
+      rcpt_fail_count++;
+      break;
+      }
+
+    /* Check for an operand */
+
+    if (smtp_cmd_data[0] == 0)
+      {
+      done = synprot_error(L_smtp_syntax_error, 501, NULL,
+        US"RCPT must have an address operand");
+      rcpt_fail_count++;
+      break;
+      }
+    
+    #ifdef EXPERIMENTAL_DSN
+    /* Set the DSN flags orcpt and dsn_flags from the session*/
+    orcpt = NULL;
+    flags = 0;
+
+    if (esmtp) for(;;)
+      {
+      uschar *name, *value, *end;
+      int size;
+
+      if (!extract_option(&name, &value))
+        {
+        break;
+        }
+
+      if (dsn_advertised && strcmpic(name, US"ORCPT") == 0)
+        {
+        /* Check whether orcpt has been already set */
+        if (orcpt != NULL) {
+          synprot_error(L_smtp_syntax_error, 501, NULL,
+            US"ORCPT can be specified once only");
+          goto COMMAND_LOOP;
+          }
+        orcpt = string_copy(value);
+        DEBUG(D_receive) debug_printf("DSN orcpt: %s\n", orcpt);
+        }
+
+      else if (dsn_advertised && strcmpic(name, US"NOTIFY") == 0)
+        {
+        /* Check if the notify flags have been already set */
+        if (flags > 0) {
+          synprot_error(L_smtp_syntax_error, 501, NULL,
+              US"NOTIFY can be specified once only");
+          goto COMMAND_LOOP;
+          }
+        if (strcmpic(value, US"NEVER") == 0) flags |= rf_notify_never; else
+          {
+          uschar *p = value;
+          while (*p != 0)
+            {
+            uschar *pp = p;
+            while (*pp != 0 && *pp != ',') pp++;
+              if (*pp == ',') *pp++ = 0;
+            if (strcmpic(p, US"SUCCESS") == 0) {
+                DEBUG(D_receive) debug_printf("DSN: Setting notify success\n");
+                flags |= rf_notify_success;
+              }
+            else if (strcmpic(p, US"FAILURE") == 0) {
+                DEBUG(D_receive) debug_printf("DSN: Setting notify failure\n");
+                flags |= rf_notify_failure;
+              }
+            else if (strcmpic(p, US"DELAY") == 0) {
+                DEBUG(D_receive) debug_printf("DSN: Setting notify delay\n");
+                flags |= rf_notify_delay;
+              }
+            else {
+              /* Catch any strange values */
+              synprot_error(L_smtp_syntax_error, 501, NULL,
+                US"Invalid value for NOTIFY parameter");
+              goto COMMAND_LOOP;
+              }
+            p = pp;
+            }
+            DEBUG(D_receive) debug_printf("DSN Flags: %x\n", flags);
+          }
+        }
+
+      /* Unknown option. Stick back the terminator characters and break
+      the loop. An error for a malformed address will occur. */
+
+      else
+        {
+        DEBUG(D_receive) debug_printf("Invalid RCPT option: %s : %s\n", name, value);
+        name[-1] = ' ';
+        value[-1] = '=';
+        break;
+        }
+      }
+    #endif
+
+    /* Apply SMTP rewriting then extract the working address. Don't allow "<>"
+    as a recipient address */
+
+    recipient = ((rewrite_existflags & rewrite_smtp) != 0)?
+      rewrite_one(smtp_cmd_data, rewrite_smtp, NULL, FALSE, US"",
+        global_rewrite_rules) : smtp_cmd_data;
+
+    /* rfc821_domains = TRUE; << no longer needed */
+    recipient = parse_extract_address(recipient, &errmess, &start, &end,
+      &recipient_domain, FALSE);
+    /* rfc821_domains = FALSE; << no longer needed */
+
+    if (recipient == NULL)
+      {
+      done = synprot_error(L_smtp_syntax_error, 501, smtp_cmd_data, errmess);
+      rcpt_fail_count++;
+      break;
+      }
+
+    /* If the recipient address is unqualified, reject it, unless this is a
+    locally generated message. However, unqualified addresses are permitted
+    from a configured list of hosts and nets - typically when behaving as
+    MUAs rather than MTAs. Sad that SMTP is used for both types of traffic,
+    really. The flag is set at the start of the SMTP connection.
+
+    RFC 1123 talks about supporting "the reserved mailbox postmaster"; I always
+    assumed this meant "reserved local part", but the revision of RFC 821 and
+    friends now makes it absolutely clear that it means *mailbox*. Consequently
+    we must always qualify this address, regardless. */
+
+    if (recipient_domain == 0)
+      {
+      if (allow_unqualified_recipient ||
+          strcmpic(recipient, US"postmaster") == 0)
+        {
+        DEBUG(D_receive) debug_printf("unqualified address %s accepted\n",
+          recipient);
+        recipient_domain = Ustrlen(recipient) + 1;
+        recipient = rewrite_address_qualify(recipient, TRUE);
+        }
+      else
+        {
+        rcpt_fail_count++;
+        smtp_printf("501 %s: recipient address must contain a domain\r\n",
+          smtp_cmd_data);
+        log_write(L_smtp_syntax_error,
+          LOG_MAIN|LOG_REJECT, "unqualified recipient rejected: "
+          "<%s> %s%s", recipient, host_and_ident(TRUE),
+          host_lookup_msg);
+        break;
+        }
+      }
+
+    /* Check maximum allowed */
+
+    if (rcpt_count > recipients_max && recipients_max > 0)
+      {
+      if (recipients_max_reject)
+        {
+        rcpt_fail_count++;
+        smtp_printf("552 too many recipients\r\n");
+        if (!toomany)
+          log_write(0, LOG_MAIN|LOG_REJECT, "too many recipients: message "
+            "rejected: sender=<%s> %s", sender_address, host_and_ident(TRUE));
+        }
+      else
+        {
+        rcpt_defer_count++;
+        smtp_printf("452 too many recipients\r\n");
+        if (!toomany)
+          log_write(0, LOG_MAIN|LOG_REJECT, "too many recipients: excess "
+            "temporarily rejected: sender=<%s> %s", sender_address,
+            host_and_ident(TRUE));
+        }
+
+      toomany = TRUE;
+      break;
+      }
+
+    /* If we have passed the threshold for rate limiting, apply the current
+    delay, and update it for next time, provided this is a limited host. */
+
+    if (rcpt_count > smtp_rlr_threshold &&
+        verify_check_host(&smtp_ratelimit_hosts) == OK)
+      {
+      DEBUG(D_receive) debug_printf("rate limit RCPT: delay %.3g sec\n",
+        smtp_delay_rcpt/1000.0);
+      millisleep((int)smtp_delay_rcpt);
+      smtp_delay_rcpt *= smtp_rlr_factor;
+      if (smtp_delay_rcpt > (double)smtp_rlr_limit)
+        smtp_delay_rcpt = (double)smtp_rlr_limit;
+      }
+
+    /* If the MAIL ACL discarded all the recipients, we bypass ACL checking
+    for them. Otherwise, check the access control list for this recipient. As
+    there may be a delay in this, re-check for a synchronization error
+    afterwards, unless pipelining was advertised. */
+
+    if (recipients_discarded) rc = DISCARD; else
+      {
+      rc = acl_check(ACL_WHERE_RCPT, recipient, acl_smtp_rcpt, &user_msg,
+        &log_msg);
+      if (rc == OK && !pipelining_advertised && !check_sync())
+        goto SYNC_FAILURE;
+      }
+
+    /* The ACL was happy */
+
+    if (rc == OK)
+      {
+      if (user_msg == NULL) smtp_printf("250 Accepted\r\n");
+        else smtp_user_msg(US"250", user_msg);
+      receive_add_recipient(recipient, -1);
+      
+      #ifdef EXPERIMENTAL_DSN
+      /* Set the dsn flags in the recipients_list */
+      if (orcpt != NULL)
+        recipients_list[recipients_count-1].orcpt = orcpt;
+      else
+        recipients_list[recipients_count-1].orcpt = NULL;
+
+      if (flags != 0)
+        recipients_list[recipients_count-1].dsn_flags = flags;
+      else
+        recipients_list[recipients_count-1].dsn_flags = 0;
+      DEBUG(D_receive) debug_printf("DSN: orcpt: %s  flags: %d\n", recipients_list[recipients_count-1].orcpt, recipients_list[recipients_count-1].dsn_flags);
+      #endif
+      
+      }
+
+    /* The recipient was discarded */
+
+    else if (rc == DISCARD)
+      {
+      if (user_msg == NULL) smtp_printf("250 Accepted\r\n");
+        else smtp_user_msg(US"250", user_msg);
+      rcpt_fail_count++;
+      discarded = TRUE;
+      log_write(0, LOG_MAIN|LOG_REJECT, "%s F=<%s> rejected RCPT %s: "
+        "discarded by %s ACL%s%s", host_and_ident(TRUE),
+        (sender_address_unrewritten != NULL)?
+        sender_address_unrewritten : sender_address,
+        smtp_cmd_argument, recipients_discarded? "MAIL" : "RCPT",
+        (log_msg == NULL)? US"" : US": ",
+        (log_msg == NULL)? US"" : log_msg);
+      }
+
+    /* Either the ACL failed the address, or it was deferred. */
+
+    else
+      {
+      if (rc == FAIL) rcpt_fail_count++; else rcpt_defer_count++;
+      done = smtp_handle_acl_fail(ACL_WHERE_RCPT, rc, user_msg, log_msg);
+      }
+    break;
+
+
+    /* The DATA command is legal only if it follows successful MAIL FROM
+    and RCPT TO commands. However, if pipelining is advertised, a bad DATA is
+    not counted as a protocol error if it follows RCPT (which must have been
+    rejected if there are no recipients.) This function is complete when a
+    valid DATA command is encountered.
+
+    Note concerning the code used: RFC 2821 says this:
+
+     -  If there was no MAIL, or no RCPT, command, or all such commands
+        were rejected, the server MAY return a "command out of sequence"
+        (503) or "no valid recipients" (554) reply in response to the
+        DATA command.
+
+    The example in the pipelining RFC 2920 uses 554, but I use 503 here
+    because it is the same whether pipelining is in use or not.
+
+    If all the RCPT commands that precede DATA provoked the same error message
+    (often indicating some kind of system error), it is helpful to include it
+    with the DATA rejection (an idea suggested by Tony Finch). */
+
+    case DATA_CMD:
+    HAD(SCH_DATA);
+    if (!discarded && recipients_count <= 0)
+      {
+      if (rcpt_smtp_response_same && rcpt_smtp_response != NULL)
+        {
+        uschar *code = US"503";
+        int len = Ustrlen(rcpt_smtp_response);
+        smtp_respond(code, 3, FALSE, US"All RCPT commands were rejected with "
+          "this error:");
+        /* Responses from smtp_printf() will have \r\n on the end */
+        if (len > 2 && rcpt_smtp_response[len-2] == '\r')
+          rcpt_smtp_response[len-2] = 0;
+        smtp_respond(code, 3, FALSE, rcpt_smtp_response);
+        }
+      if (pipelining_advertised && last_was_rcpt)
+        smtp_printf("503 Valid RCPT command must precede DATA\r\n");
+      else
+        done = synprot_error(L_smtp_protocol_error, 503, NULL,
+          US"valid RCPT command must precede DATA");
+      break;
+      }
+
+    if (toomany && recipients_max_reject)
+      {
+      sender_address = NULL;  /* This will allow a new MAIL without RSET */
+      sender_address_unrewritten = NULL;
+      smtp_printf("554 Too many recipients\r\n");
+      break;
+      }
+
+    /* If there is an ACL, re-check the synchronization afterwards, since the
+    ACL may have delayed.  To handle cutthrough delivery enforce a dummy call
+    to get the DATA command sent. */
+
+    if (acl_smtp_predata == NULL && cutthrough_fd < 0) rc = OK; else
+      {
+      uschar * acl= acl_smtp_predata ? acl_smtp_predata : US"accept";
+      enable_dollar_recipients = TRUE;
+      rc = acl_check(ACL_WHERE_PREDATA, NULL, acl, &user_msg,
+        &log_msg);
+      enable_dollar_recipients = FALSE;
+      if (rc == OK && !check_sync()) goto SYNC_FAILURE;
+      }
+
+    if (rc == OK)
+      {
+      uschar * code;
+      code = US"354";
+      if (user_msg == NULL)
+        smtp_printf("%s Enter message, ending with \".\" on a line by itself\r\n", code);
+      else smtp_user_msg(code, user_msg);
+      done = 3;
+      message_ended = END_NOTENDED;   /* Indicate in middle of data */
+      }
+
+    /* Either the ACL failed the address, or it was deferred. */
+
+    else
+      done = smtp_handle_acl_fail(ACL_WHERE_PREDATA, rc, user_msg, log_msg);
+    break;
+
+
+    case VRFY_CMD:
+    HAD(SCH_VRFY);
+    rc = acl_check(ACL_WHERE_VRFY, NULL, acl_smtp_vrfy, &user_msg, &log_msg);
+    if (rc != OK)
+      done = smtp_handle_acl_fail(ACL_WHERE_VRFY, rc, user_msg, log_msg);
+    else
+      {
+      uschar *address;
+      uschar *s = NULL;
+
+      /* rfc821_domains = TRUE; << no longer needed */
+      address = parse_extract_address(smtp_cmd_data, &errmess, &start, &end,
+        &recipient_domain, FALSE);
+      /* rfc821_domains = FALSE; << no longer needed */
+
+      if (address == NULL)
+        s = string_sprintf("501 %s", errmess);
+      else
+        {
+        address_item *addr = deliver_make_addr(address, FALSE);
+        switch(verify_address(addr, NULL, vopt_is_recipient | vopt_qualify, -1,
+               -1, -1, NULL, NULL, NULL))
+          {
+          case OK:
+          s = string_sprintf("250 <%s> is deliverable", address);
+          break;
+
+          case DEFER:
+          s = (addr->user_message != NULL)?
+            string_sprintf("451 <%s> %s", address, addr->user_message) :
+            string_sprintf("451 Cannot resolve <%s> at this time", address);
+          break;
+
+          case FAIL:
+          s = (addr->user_message != NULL)?
+            string_sprintf("550 <%s> %s", address, addr->user_message) :
+            string_sprintf("550 <%s> is not deliverable", address);
+          log_write(0, LOG_MAIN, "VRFY failed for %s %s",
+            smtp_cmd_argument, host_and_ident(TRUE));
+          break;
+          }
+        }
+
+      smtp_printf("%s\r\n", s);
+      }
+    break;
+
+
+    case EXPN_CMD:
+    HAD(SCH_EXPN);
+    rc = acl_check(ACL_WHERE_EXPN, NULL, acl_smtp_expn, &user_msg, &log_msg);
+    if (rc != OK)
+      done = smtp_handle_acl_fail(ACL_WHERE_EXPN, rc, user_msg, log_msg);
+    else
+      {
+      BOOL save_log_testing_mode = log_testing_mode;
+      address_test_mode = log_testing_mode = TRUE;
+      (void) verify_address(deliver_make_addr(smtp_cmd_data, FALSE),
+        smtp_out, vopt_is_recipient | vopt_qualify | vopt_expn, -1, -1, -1,
+        NULL, NULL, NULL);
+      address_test_mode = FALSE;
+      log_testing_mode = save_log_testing_mode;    /* true for -bh */
+      }
+    break;
+
+
+    #ifdef SUPPORT_TLS
+
+    case STARTTLS_CMD:
+    HAD(SCH_STARTTLS);
+    if (!tls_advertised)
+      {
+      done = synprot_error(L_smtp_protocol_error, 503, NULL,
+        US"STARTTLS command used when not advertised");
+      break;
+      }
+
+    /* Apply an ACL check if one is defined */
+
+    if (acl_smtp_starttls != NULL)
+      {
+      rc = acl_check(ACL_WHERE_STARTTLS, NULL, acl_smtp_starttls, &user_msg,
+        &log_msg);
+      if (rc != OK)
+        {
+        done = smtp_handle_acl_fail(ACL_WHERE_STARTTLS, rc, user_msg, log_msg);
+        break;
+        }
+      }
+
+    /* RFC 2487 is not clear on when this command may be sent, though it
+    does state that all information previously obtained from the client
+    must be discarded if a TLS session is started. It seems reasonble to
+    do an implied RSET when STARTTLS is received. */
+
+    incomplete_transaction_log(US"STARTTLS");
+    smtp_reset(reset_point);
+    toomany = FALSE;
+    cmd_list[CMD_LIST_STARTTLS].is_mail_cmd = FALSE;
+
+    /* There's an attack where more data is read in past the STARTTLS command
+    before TLS is negotiated, then assumed to be part of the secure session
+    when used afterwards; we use segregated input buffers, so are not
+    vulnerable, but we want to note when it happens and, for sheer paranoia,
+    ensure that the buffer is "wiped".
+    Pipelining sync checks will normally have protected us too, unless disabled
+    by configuration. */
+
+    if (receive_smtp_buffered())
+      {
+      DEBUG(D_any)
+        debug_printf("Non-empty input buffer after STARTTLS; naive attack?");
+      if (tls_in.active < 0)
+        smtp_inend = smtp_inptr = smtp_inbuffer;
+      /* and if TLS is already active, tls_server_start() should fail */
+      }
+
+    /* There is nothing we value in the input buffer and if TLS is succesfully
+    negotiated, we won't use this buffer again; if TLS fails, we'll just read
+    fresh content into it.  The buffer contains arbitrary content from an
+    untrusted remote source; eg: NOOP <shellcode>\r\nSTARTTLS\r\n
+    It seems safest to just wipe away the content rather than leave it as a
+    target to jump to. */
+
+    memset(smtp_inbuffer, 0, in_buffer_size);
+
+    /* Attempt to start up a TLS session, and if successful, discard all
+    knowledge that was obtained previously. At least, that's what the RFC says,
+    and that's what happens by default. However, in order to work round YAEB,
+    there is an option to remember the esmtp state. Sigh.
+
+    We must allow for an extra EHLO command and an extra AUTH command after
+    STARTTLS that don't add to the nonmail command count. */
+
+    if ((rc = tls_server_start(tls_require_ciphers)) == OK)
+      {
+      if (!tls_remember_esmtp)
+        helo_seen = esmtp = auth_advertised = pipelining_advertised = FALSE;
+      cmd_list[CMD_LIST_EHLO].is_mail_cmd = TRUE;
+      cmd_list[CMD_LIST_AUTH].is_mail_cmd = TRUE;
+      if (sender_helo_name != NULL)
+        {
+        store_free(sender_helo_name);
+        sender_helo_name = NULL;
+        host_build_sender_fullhost();  /* Rebuild */
+        set_process_info("handling incoming TLS connection from %s",
+          host_and_ident(FALSE));
+        }
+      received_protocol = (esmtp?
+        protocols[pextend + pcrpted +
+          ((sender_host_authenticated != NULL)? pauthed : 0)]
+        :
+        protocols[pnormal + pcrpted])
+        +
+        ((sender_host_address != NULL)? pnlocal : 0);
+
+      sender_host_authenticated = NULL;
+      authenticated_id = NULL;
+      sync_cmd_limit = NON_SYNC_CMD_NON_PIPELINING;
+      DEBUG(D_tls) debug_printf("TLS active\n");
+      break;     /* Successful STARTTLS */
+      }
+
+    /* Some local configuration problem was discovered before actually trying
+    to do a TLS handshake; give a temporary error. */
+
+    else if (rc == DEFER)
+      {
+      smtp_printf("454 TLS currently unavailable\r\n");
+      break;
+      }
+
+    /* Hard failure. Reject everything except QUIT or closed connection. One
+    cause for failure is a nested STARTTLS, in which case tls_in.active remains
+    set, but we must still reject all incoming commands. */
+
+    DEBUG(D_tls) debug_printf("TLS failed to start\n");
+    while (done <= 0)
+      {
+      switch(smtp_read_command(FALSE))
+        {
+        case EOF_CMD:
+        log_write(L_smtp_connection, LOG_MAIN, "%s closed by EOF",
+          smtp_get_connection_info());
+        smtp_notquit_exit(US"tls-failed", NULL, NULL);
+        done = 2;
+        break;
+
+        /* It is perhaps arguable as to which exit ACL should be called here,
+        but as it is probably a situation that almost never arises, it
+        probably doesn't matter. We choose to call the real QUIT ACL, which in
+        some sense is perhaps "right". */
+
+        case QUIT_CMD:
+        user_msg = NULL;
+        if (acl_smtp_quit != NULL)
+          {
+          rc = acl_check(ACL_WHERE_QUIT, NULL, acl_smtp_quit, &user_msg,
+            &log_msg);
+          if (rc == ERROR)
+            log_write(0, LOG_MAIN|LOG_PANIC, "ACL for QUIT returned ERROR: %s",
+              log_msg);
+          }
+        if (user_msg == NULL)
+          smtp_printf("221 %s closing connection\r\n", smtp_active_hostname);
+        else
+          smtp_respond(US"221", 3, TRUE, user_msg);
+        log_write(L_smtp_connection, LOG_MAIN, "%s closed by QUIT",
+          smtp_get_connection_info());
+        done = 2;
+        break;
+
+        default:
+        smtp_printf("554 Security failure\r\n");
+        break;
+        }
+      }
+    tls_close(TRUE, TRUE);
+    break;
+    #endif
+
+
+    /* The ACL for QUIT is provided for gathering statistical information or
+    similar; it does not affect the response code, but it can supply a custom
+    message. */
+
+    case QUIT_CMD:
+    HAD(SCH_QUIT);
+    incomplete_transaction_log(US"QUIT");
+    if (acl_smtp_quit != NULL)
+      {
+      rc = acl_check(ACL_WHERE_QUIT, NULL, acl_smtp_quit, &user_msg, &log_msg);
+      if (rc == ERROR)
+        log_write(0, LOG_MAIN|LOG_PANIC, "ACL for QUIT returned ERROR: %s",
+          log_msg);
+      }
+    if (user_msg == NULL)
+      smtp_printf("221 %s closing connection\r\n", smtp_active_hostname);
+    else
+      smtp_respond(US"221", 3, TRUE, user_msg);
+
+    #ifdef SUPPORT_TLS
+    tls_close(TRUE, TRUE);
+    #endif
+
+    done = 2;
+    log_write(L_smtp_connection, LOG_MAIN, "%s closed by QUIT",
+      smtp_get_connection_info());
+    break;
+
+
+    case RSET_CMD:
+    HAD(SCH_RSET);
+    incomplete_transaction_log(US"RSET");
+    smtp_reset(reset_point);
+    toomany = FALSE;
+    smtp_printf("250 Reset OK\r\n");
+    cmd_list[CMD_LIST_RSET].is_mail_cmd = FALSE;
+    break;
+
+
+    case NOOP_CMD:
+    HAD(SCH_NOOP);
+    smtp_printf("250 OK\r\n");
+    break;
+
+
+    /* Show ETRN/EXPN/VRFY if there's an ACL for checking hosts; if actually
+    used, a check will be done for permitted hosts. Show STARTTLS only if not
+    already in a TLS session and if it would be advertised in the EHLO
+    response. */
+
+    case HELP_CMD:
+    HAD(SCH_HELP);
+    smtp_printf("214-Commands supported:\r\n");
+      {
+      uschar buffer[256];
+      buffer[0] = 0;
+      Ustrcat(buffer, " AUTH");
+      #ifdef SUPPORT_TLS
+      if (tls_in.active < 0 &&
+          verify_check_host(&tls_advertise_hosts) != FAIL)
+        Ustrcat(buffer, " STARTTLS");
+      #endif
+      Ustrcat(buffer, " HELO EHLO MAIL RCPT DATA");
+      Ustrcat(buffer, " NOOP QUIT RSET HELP");
+      if (acl_smtp_etrn != NULL) Ustrcat(buffer, " ETRN");
+      if (acl_smtp_expn != NULL) Ustrcat(buffer, " EXPN");
+      if (acl_smtp_vrfy != NULL) Ustrcat(buffer, " VRFY");
+      smtp_printf("214%s\r\n", buffer);
+      }
+    break;
+
+
+    case EOF_CMD:
+    incomplete_transaction_log(US"connection lost");
+    smtp_notquit_exit(US"connection-lost", US"421",
+      US"%s lost input connection", smtp_active_hostname);
+
+    /* Don't log by default unless in the middle of a message, as some mailers
+    just drop the call rather than sending QUIT, and it clutters up the logs.
+    */
+
+    if (sender_address != NULL || recipients_count > 0)
+      log_write(L_lost_incoming_connection,
+          LOG_MAIN,
+          "unexpected %s while reading SMTP command from %s%s",
+          sender_host_unknown? "EOF" : "disconnection",
+          host_and_ident(FALSE), smtp_read_error);
+
+    else log_write(L_smtp_connection, LOG_MAIN, "%s lost%s",
+      smtp_get_connection_info(), smtp_read_error);
+
+    done = 1;
+    break;
+
+
+    case ETRN_CMD:
+    HAD(SCH_ETRN);
+    if (sender_address != NULL)
+      {
+      done = synprot_error(L_smtp_protocol_error, 503, NULL,
+        US"ETRN is not permitted inside a transaction");
+      break;
+      }
+
+    log_write(L_etrn, LOG_MAIN, "ETRN %s received from %s", smtp_cmd_argument,
+      host_and_ident(FALSE));
+
+    rc = acl_check(ACL_WHERE_ETRN, NULL, acl_smtp_etrn, &user_msg, &log_msg);
+    if (rc != OK)
+      {
+      done = smtp_handle_acl_fail(ACL_WHERE_ETRN, rc, user_msg, log_msg);
+      break;
+      }
+
+    /* Compute the serialization key for this command. */
+
+    etrn_serialize_key = string_sprintf("etrn-%s\n", smtp_cmd_data);
+
+    /* If a command has been specified for running as a result of ETRN, we
+    permit any argument to ETRN. If not, only the # standard form is permitted,
+    since that is strictly the only kind of ETRN that can be implemented
+    according to the RFC. */
+
+    if (smtp_etrn_command != NULL)
+      {
+      uschar *error;
+      BOOL rc;
+      etrn_command = smtp_etrn_command;
+      deliver_domain = smtp_cmd_data;
+      rc = transport_set_up_command(&argv, smtp_etrn_command, TRUE, 0, NULL,
+        US"ETRN processing", &error);
+      deliver_domain = NULL;
+      if (!rc)
+        {
+        log_write(0, LOG_MAIN|LOG_PANIC, "failed to set up ETRN command: %s",
+          error);
+        smtp_printf("458 Internal failure\r\n");
+        break;
+        }
+      }
+
+    /* Else set up to call Exim with the -R option. */
+
+    else
+      {
+      if (*smtp_cmd_data++ != '#')
+        {
+        done = synprot_error(L_smtp_syntax_error, 501, NULL,
+          US"argument must begin with #");
+        break;
+        }
+      etrn_command = US"exim -R";
+      argv = child_exec_exim(CEE_RETURN_ARGV, TRUE, NULL, TRUE, 2, US"-R",
+        smtp_cmd_data);
+      }
+
+    /* If we are host-testing, don't actually do anything. */
+
+    if (host_checking)
+      {
+      HDEBUG(D_any)
+        {
+        debug_printf("ETRN command is: %s\n", etrn_command);
+        debug_printf("ETRN command execution skipped\n");
+        }
+      if (user_msg == NULL) smtp_printf("250 OK\r\n");
+        else smtp_user_msg(US"250", user_msg);
+      break;
+      }
+
+
+    /* If ETRN queue runs are to be serialized, check the database to
+    ensure one isn't already running. */
+
+    if (smtp_etrn_serialize && !enq_start(etrn_serialize_key))
+      {
+      smtp_printf("458 Already processing %s\r\n", smtp_cmd_data);
+      break;
+      }
+
+    /* Fork a child process and run the command. We don't want to have to
+    wait for the process at any point, so set SIGCHLD to SIG_IGN before
+    forking. It should be set that way anyway for external incoming SMTP,
+    but we save and restore to be tidy. If serialization is required, we
+    actually run the command in yet another process, so we can wait for it
+    to complete and then remove the serialization lock. */
+
+    oldsignal = signal(SIGCHLD, SIG_IGN);
+
+    if ((pid = fork()) == 0)
+      {
+      smtp_input = FALSE;       /* This process is not associated with the */
+      (void)fclose(smtp_in);    /* SMTP call any more. */
+      (void)fclose(smtp_out);
+
+      signal(SIGCHLD, SIG_DFL);      /* Want to catch child */
+
+      /* If not serializing, do the exec right away. Otherwise, fork down
+      into another process. */
+
+      if (!smtp_etrn_serialize || (pid = fork()) == 0)
+        {
+        DEBUG(D_exec) debug_print_argv(argv);
+        exim_nullstd();                   /* Ensure std{in,out,err} exist */
+        execv(CS argv[0], (char *const *)argv);
+        log_write(0, LOG_MAIN|LOG_PANIC_DIE, "exec of \"%s\" (ETRN) failed: %s",
+          etrn_command, strerror(errno));
+        _exit(EXIT_FAILURE);         /* paranoia */
+        }
+
+      /* Obey this if smtp_serialize and the 2nd fork yielded non-zero. That
+      is, we are in the first subprocess, after forking again. All we can do
+      for a failing fork is to log it. Otherwise, wait for the 2nd process to
+      complete, before removing the serialization. */
+
+      if (pid < 0)
+        log_write(0, LOG_MAIN|LOG_PANIC, "2nd fork for serialized ETRN "
+          "failed: %s", strerror(errno));
+      else
+        {
+        int status;
+        DEBUG(D_any) debug_printf("waiting for serialized ETRN process %d\n",
+          (int)pid);
+        (void)wait(&status);
+        DEBUG(D_any) debug_printf("serialized ETRN process %d ended\n",
+          (int)pid);
+        }
+
+      enq_end(etrn_serialize_key);
+      _exit(EXIT_SUCCESS);
+      }
+
+    /* Back in the top level SMTP process. Check that we started a subprocess
+    and restore the signal state. */
+
+    if (pid < 0)
+      {
+      log_write(0, LOG_MAIN|LOG_PANIC, "fork of process for ETRN failed: %s",
+        strerror(errno));
+      smtp_printf("458 Unable to fork process\r\n");
+      if (smtp_etrn_serialize) enq_end(etrn_serialize_key);
+      }
+    else
+      {
+      if (user_msg == NULL) smtp_printf("250 OK\r\n");
+        else smtp_user_msg(US"250", user_msg);
+      }
+
+    signal(SIGCHLD, oldsignal);
+    break;
+
+
+    case BADARG_CMD:
+    done = synprot_error(L_smtp_syntax_error, 501, NULL,
+      US"unexpected argument data");
+    break;
+
+
+    /* This currently happens only for NULLs, but could be extended. */
+
+    case BADCHAR_CMD:
+    done = synprot_error(L_smtp_syntax_error, 0, NULL,       /* Just logs */
+      US"NULL character(s) present (shown as '?')");
+    smtp_printf("501 NULL characters are not allowed in SMTP commands\r\n");
+    break;
+
+
+    case BADSYN_CMD:
+    SYNC_FAILURE:
+    if (smtp_inend >= smtp_inbuffer + in_buffer_size)
+      smtp_inend = smtp_inbuffer + in_buffer_size - 1;
+    c = smtp_inend - smtp_inptr;
+    if (c > 150) c = 150;
+    smtp_inptr[c] = 0;
+    incomplete_transaction_log(US"sync failure");
+    log_write(0, LOG_MAIN|LOG_REJECT, "SMTP protocol synchronization error "
+      "(next input sent too soon: pipelining was%s advertised): "
+      "rejected \"%s\" %s next input=\"%s\"",
+      pipelining_advertised? "" : " not",
+      smtp_cmd_buffer, host_and_ident(TRUE),
+      string_printing(smtp_inptr));
+    smtp_notquit_exit(US"synchronization-error", US"554",
+      US"SMTP synchronization error");
+    done = 1;   /* Pretend eof - drops connection */
+    break;
+
+
+    case TOO_MANY_NONMAIL_CMD:
+    s = smtp_cmd_buffer;
+    while (*s != 0 && !isspace(*s)) s++;
+    incomplete_transaction_log(US"too many non-mail commands");
+    log_write(0, LOG_MAIN|LOG_REJECT, "SMTP call from %s dropped: too many "
+      "nonmail commands (last was \"%.*s\")",  host_and_ident(FALSE),
+      (int)(s - smtp_cmd_buffer), smtp_cmd_buffer);
+    smtp_notquit_exit(US"bad-commands", US"554", US"Too many nonmail commands");
+    done = 1;   /* Pretend eof - drops connection */
+    break;
+
+    #ifdef EXPERIMENTAL_PROXY
+    case PROXY_FAIL_IGNORE_CMD:
+    smtp_printf("503 Command refused, required Proxy negotiation failed\r\n");
+    break;
+    #endif
+
+    default:
+    if (unknown_command_count++ >= smtp_max_unknown_commands)
+      {
+      log_write(L_smtp_syntax_error, LOG_MAIN,
+        "SMTP syntax error in \"%s\" %s %s",
+        string_printing(smtp_cmd_buffer), host_and_ident(TRUE),
+        US"unrecognized command");
+      incomplete_transaction_log(US"unrecognized command");
+      smtp_notquit_exit(US"bad-commands", US"500",
+        US"Too many unrecognized commands");
+      done = 2;
+      log_write(0, LOG_MAIN|LOG_REJECT, "SMTP call from %s dropped: too many "
+        "unrecognized commands (last was \"%s\")", host_and_ident(FALSE),
+        smtp_cmd_buffer);
+      }
+    else
+      done = synprot_error(L_smtp_syntax_error, 500, NULL,
+        US"unrecognized command");
+    break;
+    }
+
+  /* This label is used by goto's inside loops that want to break out to
+  the end of the command-processing loop. */
+
+  COMMAND_LOOP:
+  last_was_rej_mail = was_rej_mail;     /* Remember some last commands for */
+  last_was_rcpt = was_rcpt;             /* protocol error handling */
+  continue;
+  }
+
+return done - 2;  /* Convert yield values */
+}
+
+/* vi: aw ai sw=2
+*/
+/* End of smtp_in.c */
diff --git a/src/smtp_out.c b/src/smtp_out.c
new file mode 100644 (file)
index 0000000..b6ff511
--- /dev/null
@@ -0,0 +1,571 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* A number of functions for driving outgoing SMTP calls. */
+
+
+#include "exim.h"
+
+
+
+/*************************************************
+*           Find an outgoing interface           *
+*************************************************/
+
+/* This function is called from the smtp transport and also from the callout
+code in verify.c. Its job is to expand a string to get a list of interfaces,
+and choose a suitable one (IPv4 or IPv6) for the outgoing address.
+
+Arguments:
+  istring    string interface setting, may be NULL, meaning "any", in
+               which case the function does nothing
+  host_af    AF_INET or AF_INET6 for the outgoing IP address
+  addr       the mail address being handled (for setting errors)
+  changed    if not NULL, set TRUE if expansion actually changed istring
+  interface  point this to the interface
+  msg        to add to any error message
+
+Returns:     TRUE on success, FALSE on failure, with error message
+               set in addr and transport_return set to PANIC
+*/
+
+BOOL
+smtp_get_interface(uschar *istring, int host_af, address_item *addr,
+  BOOL *changed, uschar **interface, uschar *msg)
+{
+uschar *expint;
+uschar *iface;
+int sep = 0;
+
+if (istring == NULL) return TRUE;
+
+expint = expand_string(istring);
+if (expint == NULL)
+  {
+  if (expand_string_forcedfail) return TRUE;
+  addr->transport_return = PANIC;
+  addr->message = string_sprintf("failed to expand \"interface\" "
+      "option for %s: %s", msg, expand_string_message);
+  return FALSE;
+  }
+
+if (changed != NULL) *changed = expint != istring;
+
+while (isspace(*expint)) expint++;
+if (*expint == 0) return TRUE;
+
+while ((iface = string_nextinlist(&expint, &sep, big_buffer,
+          big_buffer_size)) != NULL)
+  {
+  if (string_is_ip_address(iface, NULL) == 0)
+    {
+    addr->transport_return = PANIC;
+    addr->message = string_sprintf("\"%s\" is not a valid IP "
+      "address for the \"interface\" option for %s",
+      iface, msg);
+    return FALSE;
+    }
+
+  if (((Ustrchr(iface, ':') == NULL)? AF_INET:AF_INET6) == host_af)
+    break;
+  }
+
+if (iface != NULL) *interface = string_copy(iface);
+return TRUE;
+}
+
+
+
+/*************************************************
+*           Find an outgoing port                *
+*************************************************/
+
+/* This function is called from the smtp transport and also from the callout
+code in verify.c. Its job is to find a port number. Note that getservbyname()
+produces the number in network byte order.
+
+Arguments:
+  rstring     raw (unexpanded) string representation of the port
+  addr        the mail address being handled (for setting errors)
+  port        stick the port in here
+  msg         for adding to error message
+
+Returns:      TRUE on success, FALSE on failure, with error message set
+                in addr, and transport_return set to PANIC
+*/
+
+BOOL
+smtp_get_port(uschar *rstring, address_item *addr, int *port, uschar *msg)
+{
+uschar *pstring = expand_string(rstring);
+
+if (pstring == NULL)
+  {
+  addr->transport_return = PANIC;
+  addr->message = string_sprintf("failed to expand \"%s\" (\"port\" option) "
+    "for %s: %s", rstring, msg, expand_string_message);
+  return FALSE;
+  }
+
+if (isdigit(*pstring))
+  {
+  uschar *end;
+  *port = Ustrtol(pstring, &end, 0);
+  if (end != pstring + Ustrlen(pstring))
+    {
+    addr->transport_return = PANIC;
+    addr->message = string_sprintf("invalid port number for %s: %s", msg,
+      pstring);
+    return FALSE;
+    }
+  }
+
+else
+  {
+  struct servent *smtp_service = getservbyname(CS pstring, "tcp");
+  if (smtp_service == NULL)
+    {
+    addr->transport_return = PANIC;
+    addr->message = string_sprintf("TCP port \"%s\" is not defined for %s",
+      pstring, msg);
+    return FALSE;
+    }
+  *port = ntohs(smtp_service->s_port);
+  }
+
+return TRUE;
+}
+
+
+
+
+/*************************************************
+*           Connect to remote host               *
+*************************************************/
+
+/* Create a socket, and connect it to a remote host. IPv6 addresses are
+detected by checking for a colon in the address. AF_INET6 is defined even on
+non-IPv6 systems, to enable the code to be less messy. However, on such systems
+host->address will always be an IPv4 address.
+
+The port field in the host item is used if it is set (usually router from SRV
+records or elsewhere). In other cases, the default passed as an argument is
+used, and the host item is updated with its value.
+
+Arguments:
+  host        host item containing name and address (and sometimes port)
+  host_af     AF_INET or AF_INET6
+  port        default remote port to connect to, in host byte order, for those
+                hosts whose port setting is PORT_NONE
+  interface   outgoing interface address or NULL
+  timeout     timeout value or 0
+  keepalive   TRUE to use keepalive
+  dscp        DSCP value to assign to socket
+
+Returns:      connected socket number, or -1 with errno set
+*/
+
+int
+smtp_connect(host_item *host, int host_af, int port, uschar *interface,
+  int timeout, BOOL keepalive, const uschar *dscp)
+{
+int on = 1;
+int save_errno = 0;
+int dscp_value;
+int dscp_level;
+int dscp_option;
+int sock;
+
+if (host->port != PORT_NONE)
+  {
+  HDEBUG(D_transport|D_acl|D_v)
+    debug_printf("Transport port=%d replaced by host-specific port=%d\n", port,
+      host->port);
+  port = host->port;
+  }
+else host->port = port;    /* Set the port actually used */
+
+HDEBUG(D_transport|D_acl|D_v)
+  {
+  if (interface == NULL)
+    debug_printf("Connecting to %s [%s]:%d ... ",host->name,host->address,port);
+  else
+    debug_printf("Connecting to %s [%s]:%d from %s ... ", host->name,
+      host->address, port, interface);
+  }
+
+/* Create the socket */
+
+if ((sock = ip_socket(SOCK_STREAM, host_af)) < 0) return -1;
+
+/* Set TCP_NODELAY; Exim does its own buffering. */
+
+setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (uschar *)(&on), sizeof(on));
+
+/* Set DSCP value, if we can. For now, if we fail to set the value, we don't
+bomb out, just log it and continue in default traffic class. */
+
+if (dscp && dscp_lookup(dscp, host_af, &dscp_level, &dscp_option, &dscp_value))
+  {
+  HDEBUG(D_transport|D_acl|D_v)
+    debug_printf("DSCP \"%s\"=%x ", dscp, dscp_value);
+  if (setsockopt(sock, dscp_level, dscp_option, &dscp_value, sizeof(dscp_value)) < 0)
+    HDEBUG(D_transport|D_acl|D_v)
+      debug_printf("failed to set DSCP: %s ", strerror(errno));
+  /* If the kernel supports IPv4 and IPv6 on an IPv6 socket, we need to set the
+  option for both; ignore failures here */
+  if (host_af == AF_INET6 &&
+      dscp_lookup(dscp, AF_INET, &dscp_level, &dscp_option, &dscp_value))
+    {
+    (void) setsockopt(sock, dscp_level, dscp_option, &dscp_value, sizeof(dscp_value));
+    }
+  }
+
+/* Bind to a specific interface if requested. Caller must ensure the interface
+is the same type (IPv4 or IPv6) as the outgoing address. */
+
+if (interface != NULL && ip_bind(sock, host_af, interface, 0) < 0)
+  {
+  save_errno = errno;
+  HDEBUG(D_transport|D_acl|D_v)
+    debug_printf("unable to bind outgoing SMTP call to %s: %s", interface,
+    strerror(errno));
+  }
+
+/* Connect to the remote host, and add keepalive to the socket before returning
+it, if requested. */
+
+else if (ip_connect(sock, host_af, host->address, port, timeout) < 0)
+  save_errno = errno;
+
+/* Either bind() or connect() failed */
+
+if (save_errno != 0)
+  {
+  HDEBUG(D_transport|D_acl|D_v)
+    {
+    debug_printf("failed: %s", CUstrerror(save_errno));
+    if (save_errno == ETIMEDOUT)
+      debug_printf(" (timeout=%s)", readconf_printtime(timeout));
+    debug_printf("\n");
+    }
+  (void)close(sock);
+  errno = save_errno;
+  return -1;
+  }
+
+/* Both bind() and connect() succeeded */
+
+else
+  {
+  union sockaddr_46 interface_sock;
+  EXIM_SOCKLEN_T size = sizeof(interface_sock);
+  HDEBUG(D_transport|D_acl|D_v) debug_printf("connected\n");
+  if (getsockname(sock, (struct sockaddr *)(&interface_sock), &size) == 0)
+    sending_ip_address = host_ntoa(-1, &interface_sock, NULL, &sending_port);
+  else
+    {
+    log_write(0, LOG_MAIN | ((errno == ECONNRESET)? 0 : LOG_PANIC),
+      "getsockname() failed: %s", strerror(errno));
+    close(sock);
+    return -1;
+    }
+  if (keepalive) ip_keepalive(sock, host->address, TRUE);
+  return sock;
+  }
+}
+
+
+/*************************************************
+*        Flush outgoing command buffer           *
+*************************************************/
+
+/* This function is called only from smtp_write_command() below. It flushes
+the buffer of outgoing commands. There is more than one in the buffer only when
+pipelining.
+
+Argument:
+  outblock   the SMTP output block
+
+Returns:     TRUE if OK, FALSE on error, with errno set
+*/
+
+static BOOL
+flush_buffer(smtp_outblock *outblock)
+{
+int rc;
+
+#ifdef SUPPORT_TLS
+if (tls_out.active == outblock->sock)
+  rc = tls_write(FALSE, outblock->buffer, outblock->ptr - outblock->buffer);
+else
+#endif
+
+rc = send(outblock->sock, outblock->buffer, outblock->ptr - outblock->buffer, 0);
+if (rc <= 0)
+  {
+  HDEBUG(D_transport|D_acl) debug_printf("send failed: %s\n", strerror(errno));
+  return FALSE;
+  }
+
+outblock->ptr = outblock->buffer;
+outblock->cmd_count = 0;
+return TRUE;
+}
+
+
+
+/*************************************************
+*             Write SMTP command                 *
+*************************************************/
+
+/* The formatted command is left in big_buffer so that it can be reflected in
+any error message.
+
+Arguments:
+  outblock   contains buffer for pipelining, and socket
+  noflush    if TRUE, save the command in the output buffer, for pipelining
+  format     a format, starting with one of
+             of HELO, MAIL FROM, RCPT TO, DATA, ".", or QUIT.
+  ...        data for the format
+
+Returns:     0 if command added to pipelining buffer, with nothing transmitted
+            +n if n commands transmitted (may still have buffered the new one)
+            -1 on error, with errno set
+*/
+
+int
+smtp_write_command(smtp_outblock *outblock, BOOL noflush, const char *format, ...)
+{
+int count;
+int rc = 0;
+va_list ap;
+
+va_start(ap, format);
+if (!string_vformat(big_buffer, big_buffer_size, CS format, ap))
+  log_write(0, LOG_MAIN|LOG_PANIC_DIE, "overlong write_command in outgoing "
+    "SMTP");
+va_end(ap);
+count = Ustrlen(big_buffer);
+
+if (count > outblock->buffersize)
+  log_write(0, LOG_MAIN|LOG_PANIC_DIE, "overlong write_command in outgoing "
+    "SMTP");
+
+if (count > outblock->buffersize - (outblock->ptr - outblock->buffer))
+  {
+  rc = outblock->cmd_count;                 /* flush resets */
+  if (!flush_buffer(outblock)) return -1;
+  }
+
+Ustrncpy(CS outblock->ptr, big_buffer, count);
+outblock->ptr += count;
+outblock->cmd_count++;
+count -= 2;
+big_buffer[count] = 0;     /* remove \r\n for error message */
+
+/* We want to hide the actual data sent in AUTH transactions from reflections
+and logs. While authenticating, a flag is set in the outblock to enable this.
+The AUTH command itself gets any data flattened. Other lines are flattened
+completely. */
+
+if (outblock->authenticating)
+  {
+  uschar *p = big_buffer;
+  if (Ustrncmp(big_buffer, "AUTH ", 5) == 0)
+    {
+    p += 5;
+    while (isspace(*p)) p++;
+    while (!isspace(*p)) p++;
+    while (isspace(*p)) p++;
+    }
+  while (*p != 0) *p++ = '*';
+  }
+
+HDEBUG(D_transport|D_acl|D_v) debug_printf("  SMTP>> %s\n", big_buffer);
+
+if (!noflush)
+  {
+  rc += outblock->cmd_count;                /* flush resets */
+  if (!flush_buffer(outblock)) return -1;
+  }
+
+return rc;
+}
+
+
+
+/*************************************************
+*          Read one line of SMTP response        *
+*************************************************/
+
+/* This function reads one line of SMTP response from the server host. This may
+not be a complete response - it could be just part of a multiline response. We
+have to use a buffer for incoming packets, because when pipelining or using
+LMTP, there may well be more than one response in a single packet. This
+function is called only from the one that follows.
+
+Arguments:
+  inblock   the SMTP input block (contains holding buffer, socket, etc.)
+  buffer    where to put the line
+  size      space available for the line
+  timeout   the timeout to use when reading a packet
+
+Returns:    length of a line that has been put in the buffer
+            -1 otherwise, with errno set
+*/
+
+static int
+read_response_line(smtp_inblock *inblock, uschar *buffer, int size, int timeout)
+{
+uschar *p = buffer;
+uschar *ptr = inblock->ptr;
+uschar *ptrend = inblock->ptrend;
+int sock = inblock->sock;
+
+/* Loop for reading multiple packets or reading another packet after emptying
+a previously-read one. */
+
+for (;;)
+  {
+  int rc;
+
+  /* If there is data in the input buffer left over from last time, copy
+  characters from it until the end of a line, at which point we can return,
+  having removed any whitespace (which will include CR) at the end of the line.
+  The rules for SMTP say that lines end in CRLF, but there are have been cases
+  of hosts using just LF, and other MTAs are reported to handle this, so we
+  just look for LF. If we run out of characters before the end of a line,
+  carry on to read the next incoming packet. */
+
+  while (ptr < ptrend)
+    {
+    int c = *ptr++;
+    if (c == '\n')
+      {
+      while (p > buffer && isspace(p[-1])) p--;
+      *p = 0;
+      inblock->ptr = ptr;
+      return p - buffer;
+      }
+    *p++ = c;
+    if (--size < 4)
+      {
+      *p = 0;                     /* Leave malformed line for error message */
+      errno = ERRNO_SMTPFORMAT;
+      return -1;
+      }
+    }
+
+  /* Need to read a new input packet. */
+
+  rc = ip_recv(sock, inblock->buffer, inblock->buffersize, timeout);
+  if (rc <= 0) break;
+
+  /* Another block of data has been successfully read. Set up the pointers
+  and let the loop continue. */
+
+  ptrend = inblock->ptrend = inblock->buffer + rc;
+  ptr = inblock->buffer;
+  DEBUG(D_transport|D_acl) debug_printf("read response data: size=%d\n", rc);
+  }
+
+/* Get here if there has been some kind of recv() error; errno is set, but we
+ensure that the result buffer is empty before returning. */
+
+*buffer = 0;
+return -1;
+}
+
+
+
+
+
+/*************************************************
+*              Read SMTP response                *
+*************************************************/
+
+/* This function reads an SMTP response with a timeout, and returns the
+response in the given buffer, as a string. A multiline response will contain
+newline characters between the lines. The function also analyzes the first
+digit of the reply code and returns FALSE if it is not acceptable. FALSE is
+also returned after a reading error. In this case buffer[0] will be zero, and
+the error code will be in errno.
+
+Arguments:
+  inblock   the SMTP input block (contains holding buffer, socket, etc.)
+  buffer    where to put the response
+  size      the size of the buffer
+  okdigit   the expected first digit of the response
+  timeout   the timeout to use
+
+Returns:    TRUE if a valid, non-error response was received; else FALSE
+*/
+
+BOOL
+smtp_read_response(smtp_inblock *inblock, uschar *buffer, int size, int okdigit,
+   int timeout)
+{
+uschar *ptr = buffer;
+int count;
+
+errno = 0;  /* Ensure errno starts out zero */
+
+/* This is a loop to read and concatentate the lines that make up a multi-line
+response. */
+
+for (;;)
+  {
+  if ((count = read_response_line(inblock, ptr, size, timeout)) < 0)
+    return FALSE;
+
+  HDEBUG(D_transport|D_acl|D_v)
+    debug_printf("  %s %s\n", (ptr == buffer)? "SMTP<<" : "      ", ptr);
+
+  /* Check the format of the response: it must start with three digits; if
+  these are followed by a space or end of line, the response is complete. If
+  they are followed by '-' this is a multi-line response and we must look for
+  another line until the final line is reached. The only use made of multi-line
+  responses is to pass them back as error messages. We therefore just
+  concatenate them all within the buffer, which should be large enough to
+  accept any reasonable number of lines. */
+
+  if (count < 3 ||
+     !isdigit(ptr[0]) ||
+     !isdigit(ptr[1]) ||
+     !isdigit(ptr[2]) ||
+     (ptr[3] != '-' && ptr[3] != ' ' && ptr[3] != 0))
+    {
+    errno = ERRNO_SMTPFORMAT;    /* format error */
+    return FALSE;
+    }
+
+  /* If the line we have just read is a terminal line, line, we are done.
+  Otherwise more data has to be read. */
+
+  if (ptr[3] != '-') break;
+
+  /* Move the reading pointer upwards in the buffer and insert \n between the
+  components of a multiline response. Space is left for this by read_response_
+  line(). */
+
+  ptr += count;
+  *ptr++ = '\n';
+  size -= count + 1;
+  }
+
+/* Return a value that depends on the SMTP return code. On some systems a
+non-zero value of errno has been seen at this point, so ensure it is zero,
+because the caller of this function looks at errno when FALSE is returned, to
+distinguish between an unexpected return code and other errors such as
+timeouts, lost connections, etc. */
+
+errno = 0;
+return buffer[0] == okdigit;
+}
+
+/* End of smtp_out.c */
diff --git a/src/spam.c b/src/spam.c
new file mode 100644 (file)
index 0000000..7eb6fbf
--- /dev/null
@@ -0,0 +1,445 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) Tom Kistner <tom@duncanthrax.net> 2003-???? */
+/* License: GPL */
+
+/* Code for calling spamassassin's spamd. Called from acl.c. */
+
+#include "exim.h"
+#ifdef WITH_CONTENT_SCAN
+#include "spam.h"
+
+uschar spam_score_buffer[16];
+uschar spam_score_int_buffer[16];
+uschar spam_bar_buffer[128];
+uschar spam_report_buffer[32600];
+uschar prev_user_name[128] = "";
+int spam_ok = 0;
+int spam_rc = 0;
+uschar *prev_spamd_address_work = NULL;
+
+int
+spam(uschar **listptr)
+{
+  int sep = 0;
+  uschar *list = *listptr;
+  uschar *user_name;
+  uschar user_name_buffer[128];
+  unsigned long mbox_size;
+  FILE *mbox_file;
+  int spamd_sock = -1;
+  uschar spamd_buffer[32600];
+  int i, j, offset, result;
+  uschar spamd_version[8];
+  uschar spamd_score_char;
+  double spamd_threshold, spamd_score;
+  int spamd_report_offset;
+  uschar *p,*q;
+  int override = 0;
+  time_t start;
+  size_t read, wrote;
+  struct sockaddr_un server;
+#ifndef NO_POLL_H
+  struct pollfd pollfd;
+#else                               /* Patch posted by Erik ? for OS X */
+  struct timeval select_tv;         /* and applied by PH */
+  fd_set select_fd;
+#endif
+  uschar *spamd_address_work;
+
+  /* stop compiler warning */
+  result = 0;
+
+  /* find the username from the option list */
+  if ((user_name = string_nextinlist(&list, &sep,
+                                     user_name_buffer,
+                                     sizeof(user_name_buffer))) == NULL) {
+    /* no username given, this means no scanning should be done */
+    return FAIL;
+  };
+
+  /* if username is "0" or "false", do not scan */
+  if ( (Ustrcmp(user_name,"0") == 0) ||
+       (strcmpic(user_name,US"false") == 0) ) {
+    return FAIL;
+  };
+
+  /* if there is an additional option, check if it is "true" */
+  if (strcmpic(list,US"true") == 0) {
+    /* in that case, always return true later */
+    override = 1;
+  };
+
+  /* expand spamd_address if needed */
+  if (*spamd_address == '$') {
+    spamd_address_work = expand_string(spamd_address);
+    if (spamd_address_work == NULL) {
+      log_write(0, LOG_MAIN|LOG_PANIC,
+        "spamassassin acl condition: spamd_address starts with $, but expansion failed: %s", expand_string_message);
+      return DEFER;
+    }
+  }
+  else
+    spamd_address_work = spamd_address;
+
+  /* check if previous spamd_address was expanded and has changed. dump cached results if so */
+  if ( spam_ok && ( prev_spamd_address_work != NULL) && (Ustrcmp(prev_spamd_address_work, spamd_address_work) != 0)) {
+    spam_ok = 0;
+  }
+
+  /* if we scanned for this username last time, just return */
+  if ( spam_ok && ( Ustrcmp(prev_user_name, user_name) == 0 ) ) {
+    if (override)
+      return OK;
+    else
+      return spam_rc;
+  };
+
+  /* make sure the eml mbox file is spooled up */
+  mbox_file = spool_mbox(&mbox_size, NULL);
+
+  if (mbox_file == NULL) {
+    /* error while spooling */
+    log_write(0, LOG_MAIN|LOG_PANIC,
+           "spam acl condition: error while creating mbox spool file");
+    return DEFER;
+  };
+
+  start = time(NULL);
+
+  /* socket does not start with '/' -> network socket */
+  if (*spamd_address_work != '/') {
+    int num_servers = 0;
+    int current_server;
+    uschar *address = NULL;
+    uschar *spamd_address_list_ptr = spamd_address_work;
+    uschar address_buffer[256];
+    spamd_address_container * spamd_address_vector[32];
+
+    /* Check how many spamd servers we have
+       and register their addresses */
+    while ((address = string_nextinlist(&spamd_address_list_ptr, &sep,
+                                        address_buffer,
+                                        sizeof(address_buffer))) != NULL) {
+
+      /* Potential memory leak as we never free the store. */
+      spamd_address_container *this_spamd =
+        (spamd_address_container *)store_get(sizeof(spamd_address_container));
+
+      /* grok spamd address and port */
+      if( sscanf(CS address, "%s %u", this_spamd->tcp_addr, &(this_spamd->tcp_port)) != 2 ) {
+        log_write(0, LOG_MAIN,
+          "spam acl condition: warning - invalid spamd address: '%s'", address);
+        continue;
+      };
+
+      spamd_address_vector[num_servers] = this_spamd;
+      num_servers++;
+      if (num_servers > 31)
+        break;
+    };
+
+    /* check if we have at least one server */
+    if (!num_servers) {
+      log_write(0, LOG_MAIN|LOG_PANIC,
+         "spam acl condition: no useable spamd server addresses in spamd_address configuration option.");
+      (void)fclose(mbox_file);
+      return DEFER;
+    };
+
+    while ( num_servers > 0 ) {
+      int i;
+
+      /* Randomly pick a server to try */
+      current_server = random_number( num_servers );
+
+      debug_printf("trying server %s, port %u\n",
+                   spamd_address_vector[current_server]->tcp_addr,
+                   spamd_address_vector[current_server]->tcp_port);
+
+      /* contact a spamd */
+      if ( (spamd_sock = ip_socket(SOCK_STREAM, AF_INET)) < 0) {
+        log_write(0, LOG_MAIN|LOG_PANIC,
+           "spam acl condition: error creating IP socket for spamd");
+        (void)fclose(mbox_file);
+        return DEFER;
+      };
+
+      if (ip_connect( spamd_sock,
+                      AF_INET,
+                      spamd_address_vector[current_server]->tcp_addr,
+                      spamd_address_vector[current_server]->tcp_port,
+                      5 ) > -1) {
+        /* connection OK */
+        break;
+      };
+
+      log_write(0, LOG_MAIN|LOG_PANIC,
+         "spam acl condition: warning - spamd connection to %s, port %u failed: %s",
+         spamd_address_vector[current_server]->tcp_addr,
+         spamd_address_vector[current_server]->tcp_port,
+         strerror(errno));
+
+      (void)close(spamd_sock);
+
+      /* Remove the server from the list. XXX We should free the memory */
+      num_servers--;
+      for( i = current_server; i < num_servers; i++ )
+        spamd_address_vector[i] = spamd_address_vector[i+1];
+    }
+
+    if ( num_servers == 0 ) {
+      log_write(0, LOG_MAIN|LOG_PANIC, "spam acl condition: all spamd servers failed");
+      (void)fclose(mbox_file);
+      return DEFER;
+    }
+
+  }
+  else {
+    /* open the local socket */
+
+    if ((spamd_sock = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
+      log_write(0, LOG_MAIN|LOG_PANIC,
+                "malware acl condition: spamd: unable to acquire socket (%s)",
+                strerror(errno));
+      (void)fclose(mbox_file);
+      return DEFER;
+    }
+
+    server.sun_family = AF_UNIX;
+    Ustrcpy(server.sun_path, spamd_address_work);
+
+    if (connect(spamd_sock, (struct sockaddr *) &server, sizeof(struct sockaddr_un)) < 0) {
+      log_write(0, LOG_MAIN|LOG_PANIC,
+                "malware acl condition: spamd: unable to connect to UNIX socket %s (%s)",
+                spamd_address_work, strerror(errno) );
+      (void)fclose(mbox_file);
+      (void)close(spamd_sock);
+      return DEFER;
+    }
+
+  }
+
+  if (spamd_sock == -1) {
+    log_write(0, LOG_MAIN|LOG_PANIC,
+        "programming fault, spamd_sock unexpectedly unset");
+    (void)fclose(mbox_file);
+    (void)close(spamd_sock);
+    return DEFER;
+  }
+
+  /* now we are connected to spamd on spamd_sock */
+  (void)string_format(spamd_buffer,
+           sizeof(spamd_buffer),
+           "REPORT SPAMC/1.2\r\nUser: %s\r\nContent-length: %ld\r\n\r\n",
+           user_name,
+           mbox_size);
+
+  /* send our request */
+  if (send(spamd_sock, spamd_buffer, Ustrlen(spamd_buffer), 0) < 0) {
+    (void)close(spamd_sock);
+    log_write(0, LOG_MAIN|LOG_PANIC,
+         "spam acl condition: spamd send failed: %s", strerror(errno));
+    (void)fclose(mbox_file);
+    (void)close(spamd_sock);
+    return DEFER;
+  };
+
+  /* now send the file */
+  /* spamd sometimes accepts conections but doesn't read data off
+   * the connection.  We make the file descriptor non-blocking so
+   * that the write will only write sufficient data without blocking
+   * and we poll the desciptor to make sure that we can write without
+   * blocking.  Short writes are gracefully handled and if the whole
+   * trasaction takes too long it is aborted.
+   * Note: poll() is not supported in OSX 10.2 and is reported to be
+   *       broken in more recent versions (up to 10.4).
+   */
+#ifndef NO_POLL_H
+  pollfd.fd = spamd_sock;
+  pollfd.events = POLLOUT;
+#endif
+  (void)fcntl(spamd_sock, F_SETFL, O_NONBLOCK);
+  do {
+    read = fread(spamd_buffer,1,sizeof(spamd_buffer),mbox_file);
+    if (read > 0) {
+      offset = 0;
+again:
+#ifndef NO_POLL_H
+      result = poll(&pollfd, 1, 1000);
+
+/* Patch posted by Erik ? for OS X and applied by PH */
+#else
+      select_tv.tv_sec = 1;
+      select_tv.tv_usec = 0;
+      FD_ZERO(&select_fd);
+      FD_SET(spamd_sock, &select_fd);
+      result = select(spamd_sock+1, NULL, &select_fd, NULL, &select_tv);
+#endif
+/* End Erik's patch */
+
+      if (result == -1 && errno == EINTR)
+        goto again;
+      else if (result < 1) {
+        if (result == -1)
+          log_write(0, LOG_MAIN|LOG_PANIC,
+            "spam acl condition: %s on spamd socket", strerror(errno));
+        else {
+          if (time(NULL) - start < SPAMD_TIMEOUT)
+            goto again;
+          log_write(0, LOG_MAIN|LOG_PANIC,
+            "spam acl condition: timed out writing spamd socket");
+        }
+        (void)close(spamd_sock);
+        (void)fclose(mbox_file);
+        return DEFER;
+      }
+
+      wrote = send(spamd_sock,spamd_buffer + offset,read - offset,0);
+      if (wrote == -1)
+      {
+          log_write(0, LOG_MAIN|LOG_PANIC,
+            "spam acl condition: %s on spamd socket", strerror(errno));
+        (void)close(spamd_sock);
+        (void)fclose(mbox_file);
+        return DEFER;
+      }
+      if (offset + wrote != read) {
+        offset += wrote;
+        goto again;
+      }
+    }
+  }
+  while (!feof(mbox_file) && !ferror(mbox_file));
+  if (ferror(mbox_file)) {
+    log_write(0, LOG_MAIN|LOG_PANIC,
+      "spam acl condition: error reading spool file: %s", strerror(errno));
+    (void)close(spamd_sock);
+    (void)fclose(mbox_file);
+    return DEFER;
+  }
+
+  (void)fclose(mbox_file);
+
+  /* we're done sending, close socket for writing */
+  shutdown(spamd_sock,SHUT_WR);
+
+  /* read spamd response using what's left of the timeout.
+   */
+  memset(spamd_buffer, 0, sizeof(spamd_buffer));
+  offset = 0;
+  while((i = ip_recv(spamd_sock,
+                     spamd_buffer + offset,
+                     sizeof(spamd_buffer) - offset - 1,
+                     SPAMD_TIMEOUT - time(NULL) + start)) > 0 ) {
+    offset += i;
+  }
+
+  /* error handling */
+  if((i <= 0) && (errno != 0)) {
+    log_write(0, LOG_MAIN|LOG_PANIC,
+         "spam acl condition: error reading from spamd socket: %s", strerror(errno));
+    (void)close(spamd_sock);
+    return DEFER;
+  }
+
+  /* reading done */
+  (void)close(spamd_sock);
+
+  /* dig in the spamd output and put the report in a multiline header, if requested */
+  if( sscanf(CS spamd_buffer,"SPAMD/%7s 0 EX_OK\r\nContent-length: %*u\r\n\r\n%lf/%lf\r\n%n",
+             spamd_version,&spamd_score,&spamd_threshold,&spamd_report_offset) != 3 ) {
+
+    /* try to fall back to pre-2.50 spamd output */
+    if( sscanf(CS spamd_buffer,"SPAMD/%7s 0 EX_OK\r\nSpam: %*s ; %lf / %lf\r\n\r\n%n",
+               spamd_version,&spamd_score,&spamd_threshold,&spamd_report_offset) != 3 ) {
+      log_write(0, LOG_MAIN|LOG_PANIC,
+         "spam acl condition: cannot parse spamd output");
+      return DEFER;
+    };
+  };
+
+  /* Create report. Since this is a multiline string,
+  we must hack it into shape first */
+  p = &spamd_buffer[spamd_report_offset];
+  q = spam_report_buffer;
+  while (*p != '\0') {
+    /* skip \r */
+    if (*p == '\r') {
+      p++;
+      continue;
+    };
+    *q = *p;
+    q++;
+    if (*p == '\n') {
+      /* add an extra space after the newline to ensure
+      that it is treated as a header continuation line */
+      *q = ' ';
+      q++;
+    };
+    p++;
+  };
+  /* NULL-terminate */
+  *q = '\0';
+  q--;
+  /* cut off trailing leftovers */
+  while (*q <= ' ') {
+    *q = '\0';
+    q--;
+  };
+  spam_report = spam_report_buffer;
+
+  /* create spam bar */
+  spamd_score_char = spamd_score > 0 ? '+' : '-';
+  j = abs((int)(spamd_score));
+  i = 0;
+  if( j != 0 ) {
+    while((i < j) && (i <= MAX_SPAM_BAR_CHARS))
+       spam_bar_buffer[i++] = spamd_score_char;
+  }
+  else{
+    spam_bar_buffer[0] = '/';
+    i = 1;
+  }
+  spam_bar_buffer[i] = '\0';
+  spam_bar = spam_bar_buffer;
+
+  /* create "float" spam score */
+  (void)string_format(spam_score_buffer, sizeof(spam_score_buffer),"%.1f", spamd_score);
+  spam_score = spam_score_buffer;
+
+  /* create "int" spam score */
+  j = (int)((spamd_score + 0.001)*10);
+  (void)string_format(spam_score_int_buffer, sizeof(spam_score_int_buffer), "%d", j);
+  spam_score_int = spam_score_int_buffer;
+
+  /* compare threshold against score */
+  if (spamd_score >= spamd_threshold) {
+    /* spam as determined by user's threshold */
+    spam_rc = OK;
+  }
+  else {
+    /* not spam */
+    spam_rc = FAIL;
+  };
+
+  /* remember expanded spamd_address if needed */
+  if (spamd_address_work != spamd_address) {
+    prev_spamd_address_work = string_copy(spamd_address_work);
+  }
+  /* remember user name and "been here" for it */
+  Ustrcpy(prev_user_name, user_name);
+  spam_ok = 1;
+
+  if (override) {
+    /* always return OK, no matter what the score */
+    return OK;
+  }
+  else {
+    return spam_rc;
+  };
+}
+
+#endif
diff --git a/src/spam.h b/src/spam.h
new file mode 100644 (file)
index 0000000..ba700c8
--- /dev/null
@@ -0,0 +1,28 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) Tom Kistner <tom@duncanthrax.net> 2003-???? */
+/* License: GPL */
+
+/* spam defines */
+
+#ifdef WITH_CONTENT_SCAN
+
+/* timeout for reading and writing spamd */
+#define SPAMD_TIMEOUT 120
+
+/* maximum length of the spam bar */
+#define MAX_SPAM_BAR_CHARS 50
+
+/* SHUT_WR seems to be undefined on Unixware ? */
+#ifndef SHUT_WR
+#define SHUT_WR 1
+#endif
+
+typedef struct spamd_address_container {
+  uschar tcp_addr[24];
+  unsigned int tcp_port;
+} spamd_address_container;
+
+#endif
diff --git a/src/spf.c b/src/spf.c
new file mode 100644 (file)
index 0000000..7167f57
--- /dev/null
+++ b/src/spf.c
@@ -0,0 +1,139 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Experimental SPF support.
+   Copyright (c) Tom Kistner <tom@duncanthrax.net> 2004 - 2014
+   License: GPL */
+
+/* Code for calling spf checks via libspf-alt. Called from acl.c. */
+
+#include "exim.h"
+#ifdef EXPERIMENTAL_SPF
+
+/* must be kept in numeric order */
+static spf_result_id spf_result_id_list[] = {
+  { US"invalid", 0},
+  { US"neutral", 1 },
+  { US"pass", 2 },
+  { US"fail", 3 },
+  { US"softfail", 4 },
+  { US"none", 5 },
+  { US"err_temp", 6 },  /* Deprecated Apr 2014 */
+  { US"err_perm", 7 },  /* Deprecated Apr 2014 */
+  { US"temperror", 6 }, /* RFC 4408 defined */
+  { US"permerror", 7 }  /* RFC 4408 defined */
+};
+
+SPF_server_t    *spf_server = NULL;
+SPF_request_t   *spf_request = NULL;
+SPF_response_t  *spf_response = NULL;
+SPF_response_t  *spf_response_2mx = NULL;
+
+/* spf_init sets up a context that can be re-used for several
+   messages on the same SMTP connection (that come from the
+   same host with the same HELO string) */
+
+int spf_init(uschar *spf_helo_domain, uschar *spf_remote_addr) {
+
+  spf_server = SPF_server_new(SPF_DNS_CACHE, 0);
+
+  if ( spf_server == NULL ) {
+    debug_printf("spf: SPF_server_new() failed.\n");
+    return 0;
+  }
+
+  if (SPF_server_set_rec_dom(spf_server, CS primary_hostname)) {
+    debug_printf("spf: SPF_server_set_rec_dom(\"%s\") failed.\n", primary_hostname);
+    spf_server = NULL;
+    return 0;
+  }
+
+  spf_request = SPF_request_new(spf_server);
+
+  if (SPF_request_set_ipv4_str(spf_request, CS spf_remote_addr)
+      && SPF_request_set_ipv6_str(spf_request, CS spf_remote_addr)) {
+    debug_printf("spf: SPF_request_set_ipv4_str() and SPF_request_set_ipv6_str() failed [%s]\n", spf_remote_addr);
+    spf_server = NULL;
+    spf_request = NULL;
+    return 0;
+  }
+
+  if (SPF_request_set_helo_dom(spf_request, CS spf_helo_domain)) {
+    debug_printf("spf: SPF_set_helo_dom(\"%s\") failed.\n", spf_helo_domain);
+    spf_server = NULL;
+    spf_request = NULL;
+    return 0;
+  }
+
+  return 1;
+}
+
+
+/* spf_process adds the envelope sender address to the existing
+   context (if any), retrieves the result, sets up expansion
+   strings and evaluates the condition outcome. */
+
+int spf_process(uschar **listptr, uschar *spf_envelope_sender, int action) {
+  int sep = 0;
+  uschar *list = *listptr;
+  uschar *spf_result_id;
+  uschar spf_result_id_buffer[128];
+  int rc = SPF_RESULT_PERMERROR;
+
+  if (!(spf_server && spf_request)) {
+    /* no global context, assume temp error and skip to evaluation */
+    rc = SPF_RESULT_PERMERROR;
+    goto SPF_EVALUATE;
+  };
+
+  if (SPF_request_set_env_from(spf_request, CS spf_envelope_sender)) {
+    /* Invalid sender address. This should be a real rare occurence */
+    rc = SPF_RESULT_PERMERROR;
+    goto SPF_EVALUATE;
+  }
+
+  /* get SPF result */
+  if (action == SPF_PROCESS_FALLBACK)
+    SPF_request_query_fallback(spf_request, &spf_response, CS spf_guess);
+  else
+    SPF_request_query_mailfrom(spf_request, &spf_response);
+
+  /* set up expansion items */
+  spf_header_comment     = (uschar *)SPF_response_get_header_comment(spf_response);
+  spf_received           = (uschar *)SPF_response_get_received_spf(spf_response);
+  spf_result             = (uschar *)SPF_strresult(SPF_response_result(spf_response));
+  spf_smtp_comment       = (uschar *)SPF_response_get_smtp_comment(spf_response);
+
+  rc = SPF_response_result(spf_response);
+
+  /* We got a result. Now see if we should return OK or FAIL for it */
+  SPF_EVALUATE:
+  debug_printf("SPF result is %s (%d)\n", SPF_strresult(rc), rc);
+
+  if (action == SPF_PROCESS_GUESS && (!strcmp (SPF_strresult(rc), "none")))
+    return spf_process(listptr, spf_envelope_sender, SPF_PROCESS_FALLBACK);
+
+  while ((spf_result_id = string_nextinlist(&list, &sep,
+                                     spf_result_id_buffer,
+                                     sizeof(spf_result_id_buffer))) != NULL) {
+    int negate = 0;
+    int result = 0;
+
+    /* Check for negation */
+    if (spf_result_id[0] == '!') {
+      negate = 1;
+      spf_result_id++;
+    };
+
+    /* Check the result identifier */
+    result = Ustrcmp(spf_result_id, spf_result_id_list[rc].name);
+    if (!negate && result==0) return OK;
+    if (negate && result!=0) return OK;
+  };
+
+  /* no match */
+  return FAIL;
+}
+
+#endif
diff --git a/src/spf.h b/src/spf.h
new file mode 100644 (file)
index 0000000..0ce5d00
--- /dev/null
+++ b/src/spf.h
@@ -0,0 +1,33 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Experimental SPF support.
+   Copyright (c) Tom Kistner <tom@duncanthrax.net> 2004
+   License: GPL */
+
+#ifdef EXPERIMENTAL_SPF
+
+/* Yes, we do have ns_type. spf.h redefines it if we don't set this. Doh */
+#ifndef HAVE_NS_TYPE
+#define HAVE_NS_TYPE
+#endif
+#include <spf2/spf.h>
+
+#include <spf2/spf_dns_resolv.h>
+#include <spf2/spf_dns_cache.h>
+
+typedef struct spf_result_id {
+  uschar *name;
+  int    value;
+} spf_result_id;
+
+/* prototypes */
+int spf_init(uschar *,uschar *);
+int spf_process(uschar **, uschar *, int);
+
+#define SPF_PROCESS_NORMAL  0
+#define SPF_PROCESS_GUESS   1
+#define SPF_PROCESS_FALLBACK    2
+
+#endif
diff --git a/src/spool_in.c b/src/spool_in.c
new file mode 100644 (file)
index 0000000..6dcb512
--- /dev/null
@@ -0,0 +1,884 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2012 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Functions for reading spool files. When compiling for a utility (eximon),
+not all are needed, and some functionality can be cut out. */
+
+
+#include "exim.h"
+
+
+
+#ifndef COMPILE_UTILITY
+/*************************************************
+*           Open and lock data file              *
+*************************************************/
+
+/* The data file is the one that is used for locking, because the header file
+can get replaced during delivery because of header rewriting. The file has
+to opened with write access so that we can get an exclusive lock, but in
+fact it won't be written to. Just in case there's a major disaster (e.g.
+overwriting some other file descriptor with the value of this one), open it
+with append.
+
+Argument: the id of the message
+Returns:  TRUE if file successfully opened and locked
+
+Side effect: deliver_datafile is set to the fd of the open file.
+*/
+
+BOOL
+spool_open_datafile(uschar *id)
+{
+int i;
+struct stat statbuf;
+flock_t lock_data;
+uschar spoolname[256];
+
+/* If split_spool_directory is set, first look for the file in the appropriate
+sub-directory of the input directory. If it is not found there, try the input
+directory itself, to pick up leftovers from before the splitting. If split_
+spool_directory is not set, first look in the main input directory. If it is
+not found there, try the split sub-directory, in case it is left over from a
+splitting state. */
+
+for (i = 0; i < 2; i++)
+  {
+  int save_errno;
+  message_subdir[0] = (split_spool_directory == (i == 0))? id[5] : 0;
+  sprintf(CS spoolname, "%s/input/%s/%s-D", spool_directory, message_subdir, id);
+  deliver_datafile = Uopen(spoolname, O_RDWR | O_APPEND, 0);
+  if (deliver_datafile >= 0) break;
+  save_errno = errno;
+  if (errno == ENOENT)
+    {
+    if (i == 0) continue;
+    if (!queue_running)
+      log_write(0, LOG_MAIN, "Spool file %s-D not found", id);
+    }
+  else log_write(0, LOG_MAIN, "Spool error for %s: %s", spoolname,
+    strerror(errno));
+  errno = save_errno;
+  return FALSE;
+  }
+
+/* File is open and message_subdir is set. Set the close-on-exec flag, and lock
+the file. We lock only the first line of the file (containing the message ID)
+because this apparently is needed for running Exim under Cygwin. If the entire
+file is locked in one process, a sub-process cannot access it, even when passed
+an open file descriptor (at least, I think that's the Cygwin story). On real
+Unix systems it doesn't make any difference as long as Exim is consistent in
+what it locks. */
+
+(void)fcntl(deliver_datafile, F_SETFD, fcntl(deliver_datafile, F_GETFD) |
+  FD_CLOEXEC);
+
+lock_data.l_type = F_WRLCK;
+lock_data.l_whence = SEEK_SET;
+lock_data.l_start = 0;
+lock_data.l_len = SPOOL_DATA_START_OFFSET;
+
+if (fcntl(deliver_datafile, F_SETLK, &lock_data) < 0)
+  {
+  log_write(L_skip_delivery,
+            LOG_MAIN,
+            "Spool file is locked (another process is handling this message)");
+  (void)close(deliver_datafile);
+  deliver_datafile = -1;
+  errno = 0;
+  return FALSE;
+  }
+
+/* Get the size of the data; don't include the leading filename line
+in the count, but add one for the newline before the data. */
+
+if (fstat(deliver_datafile, &statbuf) == 0)
+  {
+  message_body_size = statbuf.st_size - SPOOL_DATA_START_OFFSET;
+  message_size = message_body_size + 1;
+  }
+
+return TRUE;
+}
+#endif  /* COMPILE_UTILITY */
+
+
+
+/*************************************************
+*    Read non-recipients tree from spool file    *
+*************************************************/
+
+/* The tree of non-recipients is written to the spool file in a form that
+makes it easy to read back into a tree. The format is as follows:
+
+   . Each node is preceded by two letter(Y/N) indicating whether it has left
+     or right children. There's one space after the two flags, before the name.
+
+   . The left subtree (if any) then follows, then the right subtree (if any).
+
+This function is entered with the next input line in the buffer. Note we must
+save the right flag before recursing with the same buffer.
+
+Once the tree is read, we re-construct the balance fields by scanning the tree.
+I forgot to write them out originally, and the compatible fix is to do it this
+way. This initial local recursing function does the necessary.
+
+Arguments:
+  node      tree node
+
+Returns:    maximum depth below the node, including the node itself
+*/
+
+static int
+count_below(tree_node *node)
+{
+int nleft, nright;
+if (node == NULL) return 0;
+nleft = count_below(node->left);
+nright = count_below(node->right);
+node->balance = (nleft > nright)? 1 : ((nright > nleft)? 2 : 0);
+return 1 + ((nleft > nright)? nleft : nright);
+}
+
+/* This is the real function...
+
+Arguments:
+  connect      pointer to the root of the tree
+  f            FILE to read data from
+  buffer       contains next input line; further lines read into it
+  buffer_size  size of the buffer
+
+Returns:       FALSE on format error
+*/
+
+static BOOL
+read_nonrecipients_tree(tree_node **connect, FILE *f, uschar *buffer,
+  int buffer_size)
+{
+tree_node *node;
+int n = Ustrlen(buffer);
+BOOL right = buffer[1] == 'Y';
+
+if (n < 5) return FALSE;    /* malformed line */
+buffer[n-1] = 0;            /* Remove \n */
+node = store_get(sizeof(tree_node) + n - 3);
+*connect = node;
+Ustrcpy(node->name, buffer + 3);
+node->data.ptr = NULL;
+
+if (buffer[0] == 'Y')
+  {
+  if (Ufgets(buffer, buffer_size, f) == NULL ||
+    !read_nonrecipients_tree(&node->left, f, buffer, buffer_size))
+      return FALSE;
+  }
+else node->left = NULL;
+
+if (right)
+  {
+  if (Ufgets(buffer, buffer_size, f) == NULL ||
+    !read_nonrecipients_tree(&node->right, f, buffer, buffer_size))
+      return FALSE;
+  }
+else node->right = NULL;
+
+(void) count_below(*connect);
+return TRUE;
+}
+
+
+
+
+/*************************************************
+*             Read spool header file             *
+*************************************************/
+
+/* This function reads a spool header file and places the data into the
+appropriate global variables. The header portion is always read, but header
+structures are built only if read_headers is set true. It isn't, for example,
+while generating -bp output.
+
+It may be possible for blocks of nulls (binary zeroes) to get written on the
+end of a file if there is a system crash during writing. It was observed on an
+earlier version of Exim that omitted to fsync() the files - this is thought to
+have been the cause of that incident, but in any case, this code must be robust
+against such an event, and if such a file is encountered, it must be treated as
+malformed.
+
+Arguments:
+  name          name of the header file, including the -H
+  read_headers  TRUE if in-store header structures are to be built
+  subdir_set    TRUE is message_subdir is already set
+
+Returns:        spool_read_OK        success
+                spool_read_notopen   open failed
+                spool_read_enverror  error in the envelope portion
+                spool_read_hdrdrror  error in the header portion
+*/
+
+int
+spool_read_header(uschar *name, BOOL read_headers, BOOL subdir_set)
+{
+FILE *f = NULL;
+int n;
+int rcount = 0;
+long int uid, gid;
+BOOL inheader = FALSE;
+uschar *p;
+
+/* Reset all the global variables to their default values. However, there is
+one exception. DO NOT change the default value of dont_deliver, because it may
+be forced by an external setting. */
+
+acl_var_c = acl_var_m = NULL;
+authenticated_id = NULL;
+authenticated_sender = NULL;
+allow_unqualified_recipient = FALSE;
+allow_unqualified_sender = FALSE;
+body_linecount = 0;
+body_zerocount = 0;
+deliver_firsttime = FALSE;
+deliver_freeze = FALSE;
+deliver_frozen_at = 0;
+deliver_manual_thaw = FALSE;
+/* dont_deliver must NOT be reset */
+header_list = header_last = NULL;
+host_lookup_deferred = FALSE;
+host_lookup_failed = FALSE;
+interface_address = NULL;
+interface_port = 0;
+local_error_message = FALSE;
+local_scan_data = NULL;
+max_received_linelength = 0;
+message_linecount = 0;
+received_protocol = NULL;
+received_count = 0;
+recipients_list = NULL;
+sender_address = NULL;
+sender_fullhost = NULL;
+sender_helo_name = NULL;
+sender_host_address = NULL;
+sender_host_name = NULL;
+sender_host_port = 0;
+sender_host_authenticated = NULL;
+sender_ident = NULL;
+sender_local = FALSE;
+sender_set_untrusted = FALSE;
+smtp_active_hostname = primary_hostname;
+tree_nonrecipients = NULL;
+
+#ifdef EXPERIMENTAL_BRIGHTMAIL
+bmi_run = 0;
+bmi_verdicts = NULL;
+#endif
+
+#ifndef DISABLE_DKIM
+dkim_signers = NULL;
+dkim_disable_verify = FALSE;
+dkim_collect_input = FALSE;
+#endif
+
+#ifdef SUPPORT_TLS
+tls_in.certificate_verified = FALSE;
+tls_in.cipher = NULL;
+tls_in.ourcert = NULL;
+tls_in.peercert = NULL;
+tls_in.peerdn = NULL;
+tls_in.sni = NULL;
+tls_in.ocsp = OCSP_NOT_REQ;
+#endif
+
+#ifdef WITH_CONTENT_SCAN
+spam_score_int = NULL;
+#endif
+
+#ifdef EXPERIMENTAL_DSN
+dsn_ret = 0;
+dsn_envid = NULL;
+#endif
+
+/* Generate the full name and open the file. If message_subdir is already
+set, just look in the given directory. Otherwise, look in both the split
+and unsplit directories, as for the data file above. */
+
+for (n = 0; n < 2; n++)
+  {
+  if (!subdir_set)
+    message_subdir[0] = (split_spool_directory == (n == 0))? name[5] : 0;
+  sprintf(CS big_buffer, "%s/input/%s/%s", spool_directory, message_subdir,
+    name);
+  f = Ufopen(big_buffer, "rb");
+  if (f != NULL) break;
+  if (n != 0 || subdir_set || errno != ENOENT) return spool_read_notopen;
+  }
+
+errno = 0;
+
+#ifndef COMPILE_UTILITY
+DEBUG(D_deliver) debug_printf("reading spool file %s\n", name);
+#endif  /* COMPILE_UTILITY */
+
+/* The first line of a spool file contains the message id followed by -H (i.e.
+the file name), in order to make the file self-identifying. */
+
+if (Ufgets(big_buffer, big_buffer_size, f) == NULL) goto SPOOL_READ_ERROR;
+if (Ustrlen(big_buffer) != MESSAGE_ID_LENGTH + 3 ||
+    Ustrncmp(big_buffer, name, MESSAGE_ID_LENGTH + 2) != 0)
+  goto SPOOL_FORMAT_ERROR;
+
+/* The next three lines in the header file are in a fixed format. The first
+contains the login, uid, and gid of the user who caused the file to be written.
+There are known cases where a negative gid is used, so we allow for both
+negative uids and gids. The second contains the mail address of the message's
+sender, enclosed in <>. The third contains the time the message was received,
+and the number of warning messages for delivery delays that have been sent. */
+
+if (Ufgets(big_buffer, big_buffer_size, f) == NULL) goto SPOOL_READ_ERROR;
+
+p = big_buffer + Ustrlen(big_buffer);
+while (p > big_buffer && isspace(p[-1])) p--;
+*p = 0;
+if (!isdigit(p[-1])) goto SPOOL_FORMAT_ERROR;
+while (p > big_buffer && (isdigit(p[-1]) || '-' == p[-1])) p--;
+gid = Uatoi(p);
+if (p <= big_buffer || *(--p) != ' ') goto SPOOL_FORMAT_ERROR;
+*p = 0;
+if (!isdigit(p[-1])) goto SPOOL_FORMAT_ERROR;
+while (p > big_buffer && (isdigit(p[-1]) || '-' == p[-1])) p--;
+uid = Uatoi(p);
+if (p <= big_buffer || *(--p) != ' ') goto SPOOL_FORMAT_ERROR;
+*p = 0;
+
+originator_login = string_copy(big_buffer);
+originator_uid = (uid_t)uid;
+originator_gid = (gid_t)gid;
+
+if (Ufgets(big_buffer, big_buffer_size, f) == NULL) goto SPOOL_READ_ERROR;
+n = Ustrlen(big_buffer);
+if (n < 3 || big_buffer[0] != '<' || big_buffer[n-2] != '>')
+  goto SPOOL_FORMAT_ERROR;
+
+sender_address = store_get(n-2);
+Ustrncpy(sender_address, big_buffer+1, n-3);
+sender_address[n-3] = 0;
+
+if (Ufgets(big_buffer, big_buffer_size, f) == NULL) goto SPOOL_READ_ERROR;
+if (sscanf(CS big_buffer, "%d %d", &received_time, &warning_count) != 2)
+  goto SPOOL_FORMAT_ERROR;
+
+message_age = time(NULL) - received_time;
+
+#ifndef COMPILE_UTILITY
+DEBUG(D_deliver) debug_printf("user=%s uid=%ld gid=%ld sender=%s\n",
+  originator_login, (long int)originator_uid, (long int)originator_gid,
+  sender_address);
+#endif  /* COMPILE_UTILITY */
+
+/* Now there may be a number of optional lines, each starting with "-". If you
+add a new setting here, make sure you set the default above.
+
+Because there are now quite a number of different possibilities, we use a
+switch on the first character to avoid too many failing tests. Thanks to Nico
+Erfurth for the patch that implemented this. I have made it even more efficient
+by not re-scanning the first two characters.
+
+To allow new versions of Exim that add additional flags to interwork with older
+versions that do not understand them, just ignore any lines starting with "-"
+that we don't recognize. Otherwise it wouldn't be possible to back off a new
+version that left new-style flags written on the spool. */
+
+p = big_buffer + 2;
+for (;;)
+  {
+  if (Ufgets(big_buffer, big_buffer_size, f) == NULL) goto SPOOL_READ_ERROR;
+  if (big_buffer[0] != '-') break;
+  big_buffer[Ustrlen(big_buffer) - 1] = 0;
+
+  switch(big_buffer[1])
+    {
+    case 'a':
+
+    /* Nowadays we use "-aclc" and "-aclm" for the different types of ACL
+    variable, because Exim allows any number of them, with arbitrary names.
+    The line in the spool file is "-acl[cm] <name> <length>". The name excludes
+    the c or m. */
+
+    if (Ustrncmp(p, "clc ", 4) == 0 ||
+        Ustrncmp(p, "clm ", 4) == 0)
+      {
+      uschar *name, *endptr;
+      int count;
+      tree_node *node;
+      endptr = Ustrchr(big_buffer + 6, ' ');
+      if (endptr == NULL) goto SPOOL_FORMAT_ERROR;
+      name = string_sprintf("%c%.*s", big_buffer[4], endptr - big_buffer - 6,
+        big_buffer + 6);
+      if (sscanf(CS endptr, " %d", &count) != 1) goto SPOOL_FORMAT_ERROR;
+      node = acl_var_create(name);
+      node->data.ptr = store_get(count + 1);
+      if (fread(node->data.ptr, 1, count+1, f) < count) goto SPOOL_READ_ERROR;
+      ((uschar*)node->data.ptr)[count] = 0;
+      }
+
+    else if (Ustrcmp(p, "llow_unqualified_recipient") == 0)
+      allow_unqualified_recipient = TRUE;
+    else if (Ustrcmp(p, "llow_unqualified_sender") == 0)
+      allow_unqualified_sender = TRUE;
+
+    else if (Ustrncmp(p, "uth_id", 6) == 0)
+      authenticated_id = string_copy(big_buffer + 9);
+    else if (Ustrncmp(p, "uth_sender", 10) == 0)
+      authenticated_sender = string_copy(big_buffer + 13);
+    else if (Ustrncmp(p, "ctive_hostname", 14) == 0)
+      smtp_active_hostname = string_copy(big_buffer + 17);
+
+    /* For long-term backward compatibility, we recognize "-acl", which was
+    used before the number of ACL variables changed from 10 to 20. This was
+    before the subsequent change to an arbitrary number of named variables.
+    This code is retained so that upgrades from very old versions can still
+    handle old-format spool files. The value given after "-acl" is a number
+    that is 0-9 for connection variables, and 10-19 for message variables. */
+
+    else if (Ustrncmp(p, "cl ", 3) == 0)
+      {
+      int index, count;
+      uschar name[20];   /* Need plenty of space for %d format */
+      tree_node *node;
+      if (sscanf(CS big_buffer + 5, "%d %d", &index, &count) != 2)
+        goto SPOOL_FORMAT_ERROR;
+      if (index < 10)
+        (void) string_format(name, sizeof(name), "%c%d", 'c', index);
+      else if (index < 20) /* ignore out-of-range index */
+        (void) string_format(name, sizeof(name), "%c%d", 'm', index - 10);
+      node = acl_var_create(name);
+      node->data.ptr = store_get(count + 1);
+      if (fread(node->data.ptr, 1, count+1, f) < count) goto SPOOL_READ_ERROR;
+      ((uschar*)node->data.ptr)[count] = 0;
+      }
+    break;
+
+    case 'b':
+    if (Ustrncmp(p, "ody_linecount", 13) == 0)
+      body_linecount = Uatoi(big_buffer + 15);
+    else if (Ustrncmp(p, "ody_zerocount", 13) == 0)
+      body_zerocount = Uatoi(big_buffer + 15);
+    #ifdef EXPERIMENTAL_BRIGHTMAIL
+    else if (Ustrncmp(p, "mi_verdicts ", 12) == 0)
+      bmi_verdicts = string_copy(big_buffer + 14);
+    #endif
+    break;
+
+    case 'd':
+    if (Ustrcmp(p, "eliver_firsttime") == 0)
+      deliver_firsttime = TRUE;
+    #ifdef EXPERIMENTAL_DSN
+    /* Check if the dsn flags have been set in the header file */
+    else if (Ustrncmp(p, "sn_ret", 6) == 0)
+      {
+      dsn_ret= atoi(big_buffer + 8);
+      }
+    else if (Ustrncmp(p, "sn_envid", 8) == 0)
+      {
+      dsn_envid = string_copy(big_buffer + 11);
+      }
+    #endif
+    break;
+
+    case 'f':
+    if (Ustrncmp(p, "rozen", 5) == 0)
+      {
+      deliver_freeze = TRUE;
+      sscanf(big_buffer+7, TIME_T_FMT, &deliver_frozen_at);
+      }
+    break;
+
+    case 'h':
+    if (Ustrcmp(p, "ost_lookup_deferred") == 0)
+      host_lookup_deferred = TRUE;
+    else if (Ustrcmp(p, "ost_lookup_failed") == 0)
+      host_lookup_failed = TRUE;
+    else if (Ustrncmp(p, "ost_auth", 8) == 0)
+      sender_host_authenticated = string_copy(big_buffer + 11);
+    else if (Ustrncmp(p, "ost_name", 8) == 0)
+      sender_host_name = string_copy(big_buffer + 11);
+    else if (Ustrncmp(p, "elo_name", 8) == 0)
+      sender_helo_name = string_copy(big_buffer + 11);
+
+    /* We now record the port number after the address, separated by a
+    dot. For compatibility during upgrading, do nothing if there
+    isn't a value (it gets left at zero). */
+
+    else if (Ustrncmp(p, "ost_address", 11) == 0)
+      {
+      sender_host_port = host_address_extract_port(big_buffer + 14);
+      sender_host_address = string_copy(big_buffer + 14);
+      }
+    break;
+
+    case 'i':
+    if (Ustrncmp(p, "nterface_address", 16) == 0)
+      {
+      interface_port = host_address_extract_port(big_buffer + 19);
+      interface_address = string_copy(big_buffer + 19);
+      }
+    else if (Ustrncmp(p, "dent", 4) == 0)
+      sender_ident = string_copy(big_buffer + 7);
+    break;
+
+    case 'l':
+    if (Ustrcmp(p, "ocal") == 0) sender_local = TRUE;
+    else if (Ustrcmp(big_buffer, "-localerror") == 0)
+      local_error_message = TRUE;
+    else if (Ustrncmp(p, "ocal_scan ", 10) == 0)
+      local_scan_data = string_copy(big_buffer + 12);
+    break;
+
+    case 'm':
+    if (Ustrcmp(p, "anual_thaw") == 0) deliver_manual_thaw = TRUE;
+    else if (Ustrncmp(p, "ax_received_linelength", 22) == 0)
+      max_received_linelength = Uatoi(big_buffer + 24);
+    break;
+
+    case 'N':
+    if (*p == 0) dont_deliver = TRUE;   /* -N */
+    break;
+
+    case 'r':
+    if (Ustrncmp(p, "eceived_protocol", 16) == 0)
+      received_protocol = string_copy(big_buffer + 19);
+    break;
+
+    case 's':
+    if (Ustrncmp(p, "ender_set_untrusted", 19) == 0)
+      sender_set_untrusted = TRUE;
+    #ifdef WITH_CONTENT_SCAN
+    else if (Ustrncmp(p, "pam_score_int ", 14) == 0)
+      spam_score_int = string_copy(big_buffer + 16);
+    #endif
+    break;
+
+    #ifdef SUPPORT_TLS
+    case 't':
+    if (Ustrncmp(p, "ls_certificate_verified", 23) == 0)
+      tls_in.certificate_verified = TRUE;
+    else if (Ustrncmp(p, "ls_cipher", 9) == 0)
+      tls_in.cipher = string_copy(big_buffer + 12);
+#ifndef COMPILE_UTILITY
+    else if (Ustrncmp(p, "ls_ourcert", 10) == 0)
+      (void) tls_import_cert(big_buffer + 13, &tls_in.ourcert);
+    else if (Ustrncmp(p, "ls_peercert", 11) == 0)
+      (void) tls_import_cert(big_buffer + 14, &tls_in.peercert);
+#endif
+    else if (Ustrncmp(p, "ls_peerdn", 9) == 0)
+      tls_in.peerdn = string_unprinting(string_copy(big_buffer + 12));
+    else if (Ustrncmp(p, "ls_sni", 6) == 0)
+      tls_in.sni = string_unprinting(string_copy(big_buffer + 9));
+    else if (Ustrncmp(p, "ls_ocsp", 7) == 0)
+      tls_in.ocsp = big_buffer[10] - '0';
+    break;
+    #endif
+
+    default:    /* Present because some compilers complain if all */
+    break;      /* possibilities are not covered. */
+    }
+  }
+
+/* Build sender_fullhost if required */
+
+#ifndef COMPILE_UTILITY
+host_build_sender_fullhost();
+#endif  /* COMPILE_UTILITY */
+
+#ifndef COMPILE_UTILITY
+DEBUG(D_deliver)
+  debug_printf("sender_local=%d ident=%s\n", sender_local,
+    (sender_ident == NULL)? US"unset" : sender_ident);
+#endif  /* COMPILE_UTILITY */
+
+/* We now have the tree of addresses NOT to deliver to, or a line
+containing "XX", indicating no tree. */
+
+if (Ustrncmp(big_buffer, "XX\n", 3) != 0 &&
+  !read_nonrecipients_tree(&tree_nonrecipients, f, big_buffer, big_buffer_size))
+    goto SPOOL_FORMAT_ERROR;
+
+#ifndef COMPILE_UTILITY
+DEBUG(D_deliver)
+  {
+  debug_printf("Non-recipients:\n");
+  debug_print_tree(tree_nonrecipients);
+  }
+#endif  /* COMPILE_UTILITY */
+
+/* After reading the tree, the next line has not yet been read into the
+buffer. It contains the count of recipients which follow on separate lines. */
+
+if (Ufgets(big_buffer, big_buffer_size, f) == NULL) goto SPOOL_READ_ERROR;
+if (sscanf(CS big_buffer, "%d", &rcount) != 1) goto SPOOL_FORMAT_ERROR;
+
+#ifndef COMPILE_UTILITY
+DEBUG(D_deliver) debug_printf("recipients_count=%d\n", rcount);
+#endif  /* COMPILE_UTILITY */
+
+recipients_list_max = rcount;
+recipients_list = store_get(rcount * sizeof(recipient_item));
+
+for (recipients_count = 0; recipients_count < rcount; recipients_count++)
+  {
+  int nn;
+  int pno = -1;
+  #ifdef EXPERIMENTAL_DSN
+  int dsn_flags = 0;
+  uschar *orcpt = NULL;
+  #endif
+  uschar *errors_to = NULL;
+  uschar *p;
+
+  if (Ufgets(big_buffer, big_buffer_size, f) == NULL) goto SPOOL_READ_ERROR;
+  nn = Ustrlen(big_buffer);
+  if (nn < 2) goto SPOOL_FORMAT_ERROR;
+
+  /* Remove the newline; this terminates the address if there is no additional
+  data on the line. */
+
+  p = big_buffer + nn - 1;
+  *p-- = 0;
+
+  /* Look back from the end of the line for digits and special terminators.
+  Since an address must end with a domain, we can tell that extra data is
+  present by the presence of the terminator, which is always some character
+  that cannot exist in a domain. (If I'd thought of the need for additional
+  data early on, I'd have put it at the start, with the address at the end. As
+  it is, we have to operate backwards. Addresses are permitted to contain
+  spaces, you see.)
+
+  This code has to cope with various versions of this data that have evolved
+  over time. In all cases, the line might just contain an address, with no
+  additional data. Otherwise, the possibilities are as follows:
+
+  Exim 3 type:       <address><space><digits>,<digits>,<digits>
+
+    The second set of digits is the parent number for one_time addresses. The
+    other values were remnants of earlier experiments that were abandoned.
+
+  Exim 4 first type: <address><space><digits>
+
+    The digits are the parent number for one_time addresses.
+
+  Exim 4 new type:   <address><space><data>#<type bits>
+
+    The type bits indicate what the contents of the data are.
+
+    Bit 01 indicates that, reading from right to left, the data
+      ends with <errors_to address><space><len>,<pno> where pno is
+      the parent number for one_time addresses, and len is the length
+      of the errors_to address (zero meaning none).
+
+    Bit 02 indicates that, again reading from right to left, the data continues
+     with orcpt len(orcpt),dsn_flags
+   */
+
+  while (isdigit(*p)) p--;
+
+  /* Handle Exim 3 spool files */
+
+  if (*p == ',')
+    {
+    int dummy;
+    while (isdigit(*(--p)) || *p == ',');
+    if (*p == ' ')
+      {
+      *p++ = 0;
+      (void)sscanf(CS p, "%d,%d", &dummy, &pno);
+      }
+    }
+
+  /* Handle early Exim 4 spool files */
+
+  else if (*p == ' ')
+    {
+    *p++ = 0;
+    (void)sscanf(CS p, "%d", &pno);
+    }
+
+  /* Handle current format Exim 4 spool files */
+
+  else if (*p == '#')
+    {
+    int flags;
+
+    #ifdef EXPERIMENTAL_DSN
+    #ifndef COMPILE_UTILITY
+      DEBUG(D_deliver) debug_printf("**** SPOOL_IN - Exim 4 standard format spoolfile\n");
+    #endif  /* COMPILE_UTILITY */
+    #endif
+
+    (void)sscanf(CS p+1, "%d", &flags);
+
+    if ((flags & 0x01) != 0)      /* one_time data exists */
+      {
+      int len;
+      while (isdigit(*(--p)) || *p == ',' || *p == '-');
+      (void)sscanf(CS p+1, "%d,%d", &len, &pno);
+      *p = 0;
+      if (len > 0)
+        {
+        p -= len;
+        errors_to = string_copy(p);
+        }      
+      }
+
+    *(--p) = 0;   /* Terminate address */
+#ifdef EXPERIMENTAL_DSN
+    if ((flags & 0x02) != 0)      /* one_time data exists */
+      {
+      int len;
+      while (isdigit(*(--p)) || *p == ',' || *p == '-');
+      (void)sscanf(CS p+1, "%d,%d", &len, &dsn_flags);
+      *p = 0;
+      if (len > 0)
+        {
+        p -= len;
+        orcpt = string_copy(p);
+        }      
+      }
+
+    *(--p) = 0;   /* Terminate address */
+#endif  /* EXPERIMENTAL_DSN */
+    }
+#ifdef EXPERIMENTAL_DSN
+  #ifndef COMPILE_UTILITY
+  else
+    {
+       DEBUG(D_deliver) debug_printf("**** SPOOL_IN - No additional fields\n");
+    }
+
+  if ((orcpt != NULL) || (dsn_flags != 0))
+    {
+    DEBUG(D_deliver) debug_printf("**** SPOOL_IN - address: |%s| orcpt: |%s| dsn_flags: %d\n",
+      big_buffer, orcpt, dsn_flags);
+    }
+  if (errors_to != NULL)
+    {
+    DEBUG(D_deliver) debug_printf("**** SPOOL_IN - address: |%s| errorsto: |%s|\n",
+      big_buffer, errors_to);
+    }
+  #endif  /* COMPILE_UTILITY */
+#endif  /* EXPERIMENTAL_DSN */
+
+  recipients_list[recipients_count].address = string_copy(big_buffer);
+  recipients_list[recipients_count].pno = pno;
+  recipients_list[recipients_count].errors_to = errors_to;
+  #ifdef EXPERIMENTAL_DSN
+  recipients_list[recipients_count].orcpt = orcpt;
+  recipients_list[recipients_count].dsn_flags = dsn_flags;
+  #endif
+  }
+
+/* The remainder of the spool header file contains the headers for the message,
+separated off from the previous data by a blank line. Each header is preceded
+by a count of its length and either a certain letter (for various identified
+headers), space (for a miscellaneous live header) or an asterisk (for a header
+that has been rewritten). Count the Received: headers. We read the headers
+always, in order to check on the format of the file, but only create a header
+list if requested to do so. */
+
+inheader = TRUE;
+if (Ufgets(big_buffer, big_buffer_size, f) == NULL) goto SPOOL_READ_ERROR;
+if (big_buffer[0] != '\n') goto SPOOL_FORMAT_ERROR;
+
+while ((n = fgetc(f)) != EOF)
+  {
+  header_line *h;
+  uschar flag[4];
+  int i;
+
+  if (!isdigit(n)) goto SPOOL_FORMAT_ERROR;
+  if(ungetc(n, f) == EOF  ||  fscanf(f, "%d%c ", &n, flag) == EOF)
+    goto SPOOL_READ_ERROR;
+  if (flag[0] != '*') message_size += n;  /* Omit non-transmitted headers */
+
+  if (read_headers)
+    {
+    h = store_get(sizeof(header_line));
+    h->next = NULL;
+    h->type = flag[0];
+    h->slen = n;
+    h->text = store_get(n+1);
+
+    if (h->type == htype_received) received_count++;
+
+    if (header_list == NULL) header_list = h;
+      else header_last->next = h;
+    header_last = h;
+
+    for (i = 0; i < n; i++)
+      {
+      int c = fgetc(f);
+      if (c == 0 || c == EOF) goto SPOOL_FORMAT_ERROR;
+      if (c == '\n' && h->type != htype_old) message_linecount++;
+      h->text[i] = c;
+      }
+    h->text[i] = 0;
+    }
+
+  /* Not requiring header data, just skip through the bytes */
+
+  else for (i = 0; i < n; i++)
+    {
+    int c = fgetc(f);
+    if (c == 0 || c == EOF) goto SPOOL_FORMAT_ERROR;
+    }
+  }
+
+/* We have successfully read the data in the header file. Update the message
+line count by adding the body linecount to the header linecount. Close the file
+and give a positive response. */
+
+#ifndef COMPILE_UTILITY
+DEBUG(D_deliver) debug_printf("body_linecount=%d message_linecount=%d\n",
+  body_linecount, message_linecount);
+#endif  /* COMPILE_UTILITY */
+
+message_linecount += body_linecount;
+
+fclose(f);
+return spool_read_OK;
+
+
+/* There was an error reading the spool or there was missing data,
+or there was a format error. A "read error" with no errno means an
+unexpected EOF, which we treat as a format error. */
+
+SPOOL_READ_ERROR:
+if (errno != 0)
+  {
+  n = errno;
+
+  #ifndef COMPILE_UTILITY
+  DEBUG(D_any) debug_printf("Error while reading spool file %s\n", name);
+  #endif  /* COMPILE_UTILITY */
+
+  fclose(f);
+  errno = n;
+  return inheader? spool_read_hdrerror : spool_read_enverror;
+  }
+
+SPOOL_FORMAT_ERROR:
+
+#ifndef COMPILE_UTILITY
+DEBUG(D_any) debug_printf("Format error in spool file %s\n", name);
+#endif  /* COMPILE_UTILITY */
+
+fclose(f);
+errno = ERRNO_SPOOLFORMAT;
+return inheader? spool_read_hdrerror : spool_read_enverror;
+}
+
+/* vi: aw ai sw=2
+*/
+/* End of spool_in.c */
diff --git a/src/spool_mbox.c b/src/spool_mbox.c
new file mode 100644 (file)
index 0000000..12cf3d4
--- /dev/null
@@ -0,0 +1,224 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) Tom Kistner <tom@duncanthrax.net> 2003-???? */
+/* License: GPL */
+
+/* Code for setting up a MBOX style spool file inside a /scan/<msgid>
+sub directory of exim's spool directory. */
+
+#include "exim.h"
+#ifdef WITH_CONTENT_SCAN
+
+/* externals, we must reset them on unspooling */
+#ifdef WITH_OLD_DEMIME
+extern int demime_ok;
+extern struct file_extension *file_extensions;
+#endif
+
+extern int malware_ok;
+extern int spam_ok;
+
+int spool_mbox_ok = 0;
+uschar spooled_message_id[17];
+
+/* returns a pointer to the FILE, and puts the size in bytes into mbox_file_size
+ * normally, source_file_override is NULL */
+
+FILE *spool_mbox(unsigned long *mbox_file_size, uschar *source_file_override) {
+  uschar message_subdir[2];
+  uschar buffer[16384];
+  uschar *temp_string;
+  uschar *mbox_path;
+  FILE *mbox_file = NULL;
+  FILE *data_file = NULL;
+  FILE *yield = NULL;
+  header_line *my_headerlist;
+  struct stat statbuf;
+  int i, j;
+  void *reset_point = store_get(0);
+
+  mbox_path = string_sprintf("%s/scan/%s/%s.eml", spool_directory, message_id,
+    message_id);
+
+  /* Skip creation if already spooled out as mbox file */
+  if (!spool_mbox_ok) {
+    /* create temp directory inside scan dir, directory_make works recursively */
+    temp_string = string_sprintf("scan/%s", message_id);
+    if (!directory_make(spool_directory, temp_string, 0750, FALSE)) {
+      log_write(0, LOG_MAIN|LOG_PANIC, "%s", string_open_failed(errno,
+        "scan directory %s/scan/%s", spool_directory, temp_string));
+      goto OUT;
+    };
+
+    /* open [message_id].eml file for writing */
+    mbox_file = modefopen(mbox_path, "wb", SPOOL_MODE);
+    if (mbox_file == NULL) {
+      log_write(0, LOG_MAIN|LOG_PANIC, "%s", string_open_failed(errno,
+        "scan file %s", mbox_path));
+      goto OUT;
+    };
+
+    /* Generate mailbox headers. The $received_for variable is (up to at least
+    Exim 4.64) never set here, because it is only set when expanding the
+    contents of the Received: header line. However, the code below will use it
+    if it should become available in future. */
+
+    temp_string = expand_string(
+      US"From ${if def:return_path{$return_path}{MAILER-DAEMON}} ${tod_bsdinbox}\n"
+      "${if def:sender_address{X-Envelope-From: <${sender_address}>\n}}"
+      "${if def:recipients{X-Envelope-To: ${recipients}\n}}");
+
+    if (temp_string != NULL) {
+      i = fwrite(temp_string, Ustrlen(temp_string), 1, mbox_file);
+      if (i != 1) {
+        log_write(0, LOG_MAIN|LOG_PANIC, "Error/short write while writing \
+            mailbox headers to %s", mbox_path);
+        goto OUT;
+      };
+    };
+
+    /* write all header lines to mbox file */
+    my_headerlist = header_list;
+    for (my_headerlist = header_list; my_headerlist != NULL;
+      my_headerlist = my_headerlist->next)
+    {
+      /* skip deleted headers */
+      if (my_headerlist->type == '*') continue;
+
+      i = fwrite(my_headerlist->text, my_headerlist->slen, 1, mbox_file);
+      if (i != 1) {
+        log_write(0, LOG_MAIN|LOG_PANIC, "Error/short write while writing \
+            message headers to %s", mbox_path);
+        goto OUT;
+      };
+    };
+
+    /* End headers */
+    if (fwrite("\n", 1, 1, mbox_file) != 1) {
+      log_write(0, LOG_MAIN|LOG_PANIC, "Error/short write while writing \
+        message headers to %s", mbox_path);
+      goto OUT;
+    }
+
+    /* copy body file */
+    if (source_file_override == NULL) {
+      message_subdir[1] = '\0';
+      for (i = 0; i < 2; i++) {
+        message_subdir[0] = (split_spool_directory == (i == 0))? message_id[5] : 0;
+        temp_string = string_sprintf("%s/input/%s/%s-D", spool_directory,
+          message_subdir, message_id);
+        data_file = Ufopen(temp_string, "rb");
+        if (data_file != NULL) break;
+      };
+    } else {
+      data_file = Ufopen(source_file_override, "rb");
+    };
+
+    if (data_file == NULL) {
+      log_write(0, LOG_MAIN|LOG_PANIC, "Could not open datafile for message %s",
+        message_id);
+      goto OUT;
+    };
+
+    /* The code used to use this line, but it doesn't work in Cygwin.
+     *
+     *  (void)fread(data_buffer, 1, 18, data_file);
+     *
+     * What's happening is that spool_mbox used to use an fread to jump over the
+     * file header. That fails under Cygwin because the header is locked, but
+     * doing an fseek succeeds. We have to output the leading newline
+     * explicitly, because the one in the file is parted of the locked area.
+     */
+
+    if (!source_file_override)
+      (void)fseek(data_file, SPOOL_DATA_START_OFFSET, SEEK_SET);
+
+    do {
+      j = fread(buffer, 1, sizeof(buffer), data_file);
+
+      if (j > 0) {
+        i = fwrite(buffer, j, 1, mbox_file);
+        if (i != 1) {
+          log_write(0, LOG_MAIN|LOG_PANIC, "Error/short write while writing \
+              message body to %s", mbox_path);
+          goto OUT;
+        };
+      };
+    } while (j > 0);
+
+    (void)fclose(mbox_file);
+    mbox_file = NULL;
+
+    Ustrcpy(spooled_message_id, message_id);
+    spool_mbox_ok = 1;
+  };
+
+  /* get the size of the mbox message and open [message_id].eml file for reading*/
+  if (Ustat(mbox_path, &statbuf) != 0 ||
+      (yield = Ufopen(mbox_path,"rb")) == NULL) {
+    log_write(0, LOG_MAIN|LOG_PANIC, "%s", string_open_failed(errno,
+      "scan file %s", mbox_path));
+    goto OUT;
+  };
+
+  *mbox_file_size = statbuf.st_size;
+
+  OUT:
+  if (data_file) (void)fclose(data_file);
+  if (mbox_file) (void)fclose(mbox_file);
+  store_reset(reset_point);
+  return yield;
+}
+
+/* remove mbox spool file, demimed files and temp directory */
+void unspool_mbox(void) {
+
+  /* reset all exiscan state variables */
+  #ifdef WITH_OLD_DEMIME
+  demime_ok = 0;
+  demime_errorlevel = 0;
+  demime_reason = NULL;
+  file_extensions = NULL;
+  #endif
+
+  spam_ok = 0;
+  malware_ok = 0;
+
+  if (spool_mbox_ok && !no_mbox_unspool) {
+    uschar *mbox_path;
+    uschar *file_path;
+    int n;
+    struct dirent *entry;
+    DIR *tempdir;
+
+    mbox_path = string_sprintf("%s/scan/%s", spool_directory, spooled_message_id);
+
+    tempdir = opendir(CS mbox_path);
+    if (!tempdir) {
+      debug_printf("Unable to opendir(%s): %s\n", mbox_path, strerror(errno));
+      /* Just in case we still can: */
+      rmdir(CS mbox_path);
+      return;
+    }
+    /* loop thru dir & delete entries */
+    while((entry = readdir(tempdir)) != NULL) {
+      uschar *name = US entry->d_name;
+      if (Ustrcmp(name, US".") == 0 || Ustrcmp(name, US"..") == 0) continue;
+
+      file_path = string_sprintf("%s/%s", mbox_path, name);
+      debug_printf("unspool_mbox(): unlinking '%s'\n", file_path);
+      n = unlink(CS file_path);
+    };
+
+    closedir(tempdir);
+
+    /* remove directory */
+    rmdir(CS mbox_path);
+    store_reset(mbox_path);
+  };
+  spool_mbox_ok = 0;
+}
+
+#endif
diff --git a/src/spool_out.c b/src/spool_out.c
new file mode 100644 (file)
index 0000000..67ac8bc
--- /dev/null
@@ -0,0 +1,521 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2012 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Functions for writing spool files, and moving them about. */
+
+
+#include "exim.h"
+
+
+
+/*************************************************
+*       Deal with header writing errors          *
+*************************************************/
+
+/* This function is called immediately after errors in writing the spool, with
+errno still set. It creates and error message, depending on the circumstances.
+If errmsg is NULL, it logs the message and panic-dies. Otherwise errmsg is set
+to point to the message, and -1 is returned. This function makes the code of
+spool_write_header() a bit neater.
+
+Arguments:
+   where      SW_RECEIVING, SW_DELIVERING, or SW_MODIFYING
+   errmsg     where to put the message; NULL => panic-die
+   s          text to add to log string
+   temp_name  name of temp file to unlink
+   f          FILE to close, if not NULL
+
+Returns:      -1 if errmsg is not NULL; otherwise doesn't return
+*/
+
+static int
+spool_write_error(int where, uschar **errmsg, uschar *s, uschar *temp_name,
+  FILE *f)
+{
+uschar *msg = (where == SW_RECEIVING)?
+  string_sprintf("spool file %s error while receiving from %s: %s", s,
+    (sender_fullhost != NULL)? sender_fullhost : sender_ident,
+    strerror(errno))
+  :
+  string_sprintf("spool file %s error while %s: %s", s,
+    (where == SW_DELIVERING)? "delivering" : "modifying",
+    strerror(errno));
+
+if (temp_name != NULL) Uunlink(temp_name);
+if (f != NULL) (void)fclose(f);
+
+if (errmsg == NULL)
+  log_write(0, LOG_MAIN|LOG_PANIC_DIE, "%s", msg);
+else
+  *errmsg = msg;
+
+return -1;
+}
+
+
+
+/*************************************************
+*            Open file under temporary name      *
+*************************************************/
+
+/* This is used for opening spool files under a temporary name,
+with a single attempt at deleting if they already exist.
+
+Argument: temporary name for spool header file
+Returns:  file descriptor of open file, or < 0 on failure, with errno unchanged
+*/
+
+int
+spool_open_temp(uschar *temp_name)
+{
+int fd = Uopen(temp_name, O_RDWR|O_CREAT|O_EXCL, SPOOL_MODE);
+
+/* If the file already exists, something has gone wrong. This process may well
+have previously created the file if it is delivering more than one address, but
+it should have renamed it almost immediately. A file could, however, be left
+around as a result of a system crash, and by coincidence this process might
+have the same pid. We therefore have one go at unlinking it before giving up.
+*/
+
+if (fd < 0 && errno == EEXIST)
+  {
+  DEBUG(D_any) debug_printf("%s exists: unlinking\n", temp_name);
+  Uunlink(temp_name);
+  fd = Uopen(temp_name, O_RDWR|O_CREAT|O_EXCL, SPOOL_MODE);
+  }
+
+/* If the file has been opened, make sure the file's group is the Exim gid, and
+double-check the mode because the group setting doesn't always get set
+automatically. */
+
+if (fd >= 0)
+  if (fchown(fd, exim_uid, exim_gid) || fchmod(fd, SPOOL_MODE))
+    {
+    DEBUG(D_any) debug_printf("failed setting perms on %s\n", temp_name);
+    (void) close(fd); fd = -1;
+    Uunlink(temp_name);
+    }
+
+return fd;
+}
+
+
+
+/*************************************************
+*          Write the header spool file           *
+*************************************************/
+
+/* Returns the size of the file for success; zero for failure. The file is
+written under a temporary name, and then renamed. It's done this way so that it
+works with re-writing the file on message deferral as well as for the initial
+write. Whenever this function is called, the data file for the message should
+be open and locked, thus preventing any other exim process from working on this
+message.
+
+Argument:
+  id      the message id
+  where   SW_RECEIVING, SW_DELIVERING, or SW_MODIFYING
+  errmsg  where to put an error message; if NULL, panic-die on error
+
+Returns:  the size of the header texts on success;
+          negative on writing failure, unless errmsg == NULL
+*/
+
+int
+spool_write_header(uschar *id, int where, uschar **errmsg)
+{
+int fd;
+int i;
+int size_correction;
+FILE *f;
+header_line *h;
+struct stat statbuf;
+uschar name[256];
+uschar temp_name[256];
+
+sprintf(CS temp_name, "%s/input/%s/hdr.%d", spool_directory, message_subdir,
+  (int)getpid());
+fd = spool_open_temp(temp_name);
+if (fd < 0) return spool_write_error(where, errmsg, US"open", NULL, NULL);
+f = fdopen(fd, "wb");
+DEBUG(D_receive|D_deliver) debug_printf("Writing spool header file\n");
+
+/* We now have an open file to which the header data is to be written. Start
+with the file's leaf name, to make the file self-identifying. Continue with the
+identity of the submitting user, followed by the sender's address. The sender's
+address is enclosed in <> because it might be the null address. Then write the
+received time and the number of warning messages that have been sent. */
+
+fprintf(f, "%s-H\n", message_id);
+fprintf(f, "%.63s %ld %ld\n", originator_login, (long int)originator_uid,
+  (long int)originator_gid);
+fprintf(f, "<%s>\n", sender_address);
+fprintf(f, "%d %d\n", received_time, warning_count);
+
+/* If there is information about a sending host, remember it. The HELO
+data can be set for local SMTP as well as remote. */
+
+if (sender_helo_name != NULL)
+  fprintf(f, "-helo_name %s\n", sender_helo_name);
+
+if (sender_host_address != NULL)
+  {
+  fprintf(f, "-host_address %s.%d\n", sender_host_address, sender_host_port);
+  if (sender_host_name != NULL)
+    fprintf(f, "-host_name %s\n", sender_host_name);
+  if (sender_host_authenticated != NULL)
+    fprintf(f, "-host_auth %s\n", sender_host_authenticated);
+  }
+
+/* Also about the interface a message came in on */
+
+if (interface_address != NULL)
+  fprintf(f, "-interface_address %s.%d\n", interface_address, interface_port);
+
+if (smtp_active_hostname != primary_hostname)
+  fprintf(f, "-active_hostname %s\n", smtp_active_hostname);
+
+/* Likewise for any ident information; for local messages this is
+likely to be the same as originator_login, but will be different if
+the originator was root, forcing a different ident. */
+
+if (sender_ident != NULL) fprintf(f, "-ident %s\n", sender_ident);
+
+/* Ditto for the received protocol */
+
+if (received_protocol != NULL)
+  fprintf(f, "-received_protocol %s\n", received_protocol);
+
+/* Preserve any ACL variables that are set. */
+
+tree_walk(acl_var_c, &acl_var_write, f);
+tree_walk(acl_var_m, &acl_var_write, f);
+
+/* Now any other data that needs to be remembered. */
+
+fprintf(f, "-body_linecount %d\n", body_linecount);
+fprintf(f, "-max_received_linelength %d\n", max_received_linelength);
+
+if (body_zerocount > 0) fprintf(f, "-body_zerocount %d\n", body_zerocount);
+
+if (authenticated_id != NULL)
+  fprintf(f, "-auth_id %s\n", authenticated_id);
+if (authenticated_sender != NULL)
+  fprintf(f, "-auth_sender %s\n", authenticated_sender);
+
+if (allow_unqualified_recipient) fprintf(f, "-allow_unqualified_recipient\n");
+if (allow_unqualified_sender) fprintf(f, "-allow_unqualified_sender\n");
+if (deliver_firsttime) fprintf(f, "-deliver_firsttime\n");
+if (deliver_freeze) fprintf(f, "-frozen " TIME_T_FMT "\n", deliver_frozen_at);
+if (dont_deliver) fprintf(f, "-N\n");
+if (host_lookup_deferred) fprintf(f, "-host_lookup_deferred\n");
+if (host_lookup_failed) fprintf(f, "-host_lookup_failed\n");
+if (sender_local) fprintf(f, "-local\n");
+if (local_error_message) fprintf(f, "-localerror\n");
+if (local_scan_data != NULL) fprintf(f, "-local_scan %s\n", local_scan_data);
+#ifdef WITH_CONTENT_SCAN
+if (spam_score_int != NULL) fprintf(f,"-spam_score_int %s\n", spam_score_int);
+#endif
+if (deliver_manual_thaw) fprintf(f, "-manual_thaw\n");
+if (sender_set_untrusted) fprintf(f, "-sender_set_untrusted\n");
+
+#ifdef EXPERIMENTAL_BRIGHTMAIL
+if (bmi_verdicts != NULL) fprintf(f, "-bmi_verdicts %s\n", bmi_verdicts);
+#endif
+
+#ifdef SUPPORT_TLS
+if (tls_in.certificate_verified) fprintf(f, "-tls_certificate_verified\n");
+if (tls_in.cipher)       fprintf(f, "-tls_cipher %s\n", tls_in.cipher);
+if (tls_in.peercert)
+  {
+  (void) tls_export_cert(big_buffer, big_buffer_size, tls_in.peercert);
+  fprintf(f, "-tls_peercert %s\n", CS big_buffer);
+  }
+if (tls_in.peerdn)       fprintf(f, "-tls_peerdn %s\n", string_printing(tls_in.peerdn));
+if (tls_in.sni)                 fprintf(f, "-tls_sni %s\n",    string_printing(tls_in.sni));
+if (tls_in.ourcert)
+  {
+  (void) tls_export_cert(big_buffer, big_buffer_size, tls_in.ourcert);
+  fprintf(f, "-tls_ourcert %s\n", CS big_buffer);
+  }
+if (tls_in.ocsp)        fprintf(f, "-tls_ocsp %d\n",   tls_in.ocsp);
+#endif
+
+#ifdef EXPERIMENTAL_DSN
+/* Write the dsn flags to the spool header file */
+DEBUG(D_deliver) debug_printf("DSN: Write SPOOL :-dsn_envid %s\n", dsn_envid);
+if (dsn_envid != NULL) fprintf(f, "-dsn_envid %s\n", dsn_envid);
+DEBUG(D_deliver) debug_printf("DSN: Write SPOOL :-dsn_ret %d\n", dsn_ret);
+if (dsn_ret != 0) fprintf(f, "-dsn_ret %d\n", dsn_ret);
+#endif
+
+/* To complete the envelope, write out the tree of non-recipients, followed by
+the list of recipients. These won't be disjoint the first time, when no
+checking has been done. If a recipient is a "one-time" alias, it is followed by
+a space and its parent address number (pno). */
+
+tree_write(tree_nonrecipients, f);
+fprintf(f, "%d\n", recipients_count);
+for (i = 0; i < recipients_count; i++)
+  {
+  recipient_item *r = recipients_list + i;
+#ifdef EXPERIMENTAL_DSN
+DEBUG(D_deliver) debug_printf("DSN: Flags :%d\n", r->dsn_flags);
+#endif
+  if (r->pno < 0 && r->errors_to == NULL
+    #ifdef EXPERIMENTAL_DSN
+     && r->dsn_flags == 0
+    #endif
+    )
+    fprintf(f, "%s\n", r->address);
+  else
+    {
+    uschar *errors_to = (r->errors_to == NULL)? US"" : r->errors_to;
+    #ifdef EXPERIMENTAL_DSN
+    /* for DSN SUPPORT extend exim 4 spool in a compatible way by
+       adding new values upfront and add flag 0x02 */
+    uschar *orcpt = (r->orcpt == NULL)? US"" : r->orcpt;
+    fprintf(f, "%s %s %d,%d %s %d,%d#3\n", r->address, orcpt, Ustrlen(orcpt), r->dsn_flags,
+      errors_to, Ustrlen(errors_to), r->pno);
+    #else
+    fprintf(f, "%s %s %d,%d#1\n", r->address, errors_to,
+      Ustrlen(errors_to), r->pno);
+    #endif
+    }
+    
+    #ifdef EXPERIMENTAL_DSN
+      DEBUG(D_deliver) debug_printf("DSN: **** SPOOL_OUT - address: |%s| errorsto: |%s| orcpt: |%s| dsn_flags: %d\n",
+         r->address, r->errors_to, r->orcpt, r->dsn_flags);
+    #endif
+  }
+
+/* Put a blank line before the headers */
+
+fprintf(f, "\n");
+
+/* Save the size of the file so far so we can subtract it from the final length
+to get the actual size of the headers. */
+
+fflush(f);
+fstat(fd, &statbuf);
+size_correction = statbuf.st_size;
+
+/* Finally, write out the message's headers. To make it easier to read them
+in again, precede each one with the count of its length. Make the count fixed
+length to aid human eyes when debugging and arrange for it not be included in
+the size. It is followed by a space for normal headers, a flagging letter for
+various other headers, or an asterisk for old headers that have been rewritten.
+These are saved as a record for debugging. Don't included them in the message's
+size. */
+
+for (h = header_list; h != NULL; h = h->next)
+  {
+  fprintf(f, "%03d%c %s", h->slen, h->type, h->text);
+  size_correction += 5;
+  if (h->type == '*') size_correction += h->slen;
+  }
+
+/* Flush and check for any errors while writing */
+
+if (fflush(f) != 0 || ferror(f))
+  return spool_write_error(where, errmsg, US"write", temp_name, f);
+
+/* Force the file's contents to be written to disk. Note that fflush()
+just pushes it out of C, and fclose() doesn't guarantee to do the write
+either. That's just the way Unix works... */
+
+if (EXIMfsync(fileno(f)) < 0)
+  return spool_write_error(where, errmsg, US"sync", temp_name, f);
+
+/* Get the size of the file, and close it. */
+
+fstat(fd, &statbuf);
+if (fclose(f) != 0)
+  return spool_write_error(where, errmsg, US"close", temp_name, NULL);
+
+/* Rename the file to its correct name, thereby replacing any previous
+incarnation. */
+
+sprintf(CS name, "%s/input/%s/%s-H", spool_directory, message_subdir, id);
+
+if (Urename(temp_name, name) < 0)
+  return spool_write_error(where, errmsg, US"rename", temp_name, NULL);
+
+/* Linux (and maybe other OS?) does not automatically sync a directory after
+an operation like rename. We therefore have to do it forcibly ourselves in
+these cases, to make sure the file is actually accessible on disk, as opposed
+to just the data being accessible from a file in lost+found. Linux also has
+O_DIRECTORY, for opening a directory.
+
+However, it turns out that some file systems (some versions of NFS?) do not
+support directory syncing. It seems safe enough to ignore EINVAL to cope with
+these cases. One hack on top of another... but that's life. */
+
+#ifdef NEED_SYNC_DIRECTORY
+
+sprintf(CS temp_name, "%s/input/%s/.", spool_directory, message_subdir);
+
+#ifndef O_DIRECTORY
+#define O_DIRECTORY 0
+#endif
+
+if ((fd = Uopen(temp_name, O_RDONLY|O_DIRECTORY, 0)) < 0)
+  return spool_write_error(where, errmsg, US"directory open", name, NULL);
+
+if (EXIMfsync(fd) < 0 && errno != EINVAL)
+  return spool_write_error(where, errmsg, US"directory sync", name, NULL);
+
+if (close(fd) < 0)
+  return spool_write_error(where, errmsg, US"directory close", name, NULL);
+
+#endif  /* NEED_SYNC_DIRECTORY */
+
+/* Return the number of characters in the headers, which is the file size, less
+the prelimary stuff, less the additional count fields on the headers. */
+
+DEBUG(D_receive) debug_printf("Size of headers = %d\n",
+  (int)(statbuf.st_size - size_correction));
+
+return statbuf.st_size - size_correction;
+}
+
+
+#ifdef SUPPORT_MOVE_FROZEN_MESSAGES
+
+/************************************************
+*              Make a hard link                 *
+************************************************/
+
+/* Used by spool_move_message() below. Note re the use of sprintf(): the value
+of spool_directory is checked to ensure that it is less than 200 characters at
+start-up time.
+
+Arguments:
+  dir        base directory name
+  subdir     subdirectory name
+  id         message id
+  suffix     suffix to add to id
+  from       source directory prefix
+  to         destination directory prefix
+  noentok    if TRUE, absence of file is not an error
+
+Returns:     TRUE if all went well
+             FALSE, having panic logged if not
+*/
+
+static BOOL
+make_link(uschar *dir, uschar *subdir, uschar *id, uschar *suffix, uschar *from,
+  uschar *to, BOOL noentok)
+{
+uschar f[256], t[256];
+sprintf(CS f, "%s/%s%s/%s/%s%s", spool_directory, from, dir, subdir, id, suffix);
+sprintf(CS t, "%s/%s%s/%s/%s%s", spool_directory, to, dir, subdir, id, suffix);
+if (Ulink(f, t) < 0 && (!noentok || errno != ENOENT))
+  {
+  log_write(0, LOG_MAIN|LOG_PANIC, "link(\"%s\", \"%s\") failed while moving "
+    "message: %s", f, t, strerror(errno));
+  return FALSE;
+  }
+return TRUE;
+}
+
+
+
+/************************************************
+*                Break a link                   *
+************************************************/
+
+/* Used by spool_move_message() below. Note re the use of sprintf(): the value
+of spool_directory is checked to ensure that it is less than 200 characters at
+start-up time.
+
+Arguments:
+  dir        base directory name
+  subdir     subdirectory name
+  id         message id
+  suffix     suffix to add to id
+  from       source directory prefix
+  noentok    if TRUE, absence of file is not an error
+
+Returns:     TRUE if all went well
+             FALSE, having panic logged if not
+*/
+
+static BOOL
+break_link(uschar *dir, uschar *subdir, uschar *id, uschar *suffix, uschar *from,
+  BOOL noentok)
+{
+uschar f[256];
+sprintf(CS f, "%s/%s%s/%s/%s%s", spool_directory, from, dir, subdir, id, suffix);
+if (Uunlink(f) < 0 && (!noentok || errno != ENOENT))
+  {
+  log_write(0, LOG_MAIN|LOG_PANIC, "unlink(\"%s\") failed while moving "
+    "message: %s", f, strerror(errno));
+  return FALSE;
+  }
+return TRUE;
+}
+
+
+
+/************************************************
+*            Move message files                 *
+************************************************/
+
+/* Move the files for a message (-H, -D, and msglog) from one directory (or
+hierarchy) to another. It is assume that there is no -J file in existence when
+this is done. At present, this is used only when move_frozen_messages is set,
+so compile it only when that support is configured.
+
+Arguments:
+  id          the id of the message to be delivered
+  subdir      the subdirectory name, or an empty string
+  from        a prefix for "input" or "msglog" for where the message is now
+  to          a prefix for "input" or "msglog" for where the message is to go
+
+Returns:      TRUE if all is well
+              FALSE if not, with error logged in panic and main logs
+*/
+
+BOOL
+spool_move_message(uschar *id, uschar *subdir, uschar *from, uschar *to)
+{
+/* Create any output directories that do not exist. */
+
+sprintf(CS big_buffer, "%sinput/%s", to, subdir);
+(void)directory_make(spool_directory, big_buffer, INPUT_DIRECTORY_MODE, TRUE);
+sprintf(CS big_buffer, "%smsglog/%s", to, subdir);
+(void)directory_make(spool_directory, big_buffer, INPUT_DIRECTORY_MODE, TRUE);
+
+/* Move the message by first creating new hard links for all the files, and
+then removing the old links. When moving messages onto the main spool, the -H
+file should be set up last, because that's the one that tells Exim there is a
+message to be delivered, so we create its new link last and remove its old link
+first. Programs that look at the alternate directories should follow the same
+rule of waiting for a -H file before doing anything. When moving messsages off
+the mail spool, the -D file should be open and locked at the time, thus keeping
+Exim's hands off. */
+
+if (!make_link(US"msglog", subdir, id, US"", from, to, TRUE) ||
+    !make_link(US"input",  subdir, id, US"-D", from, to, FALSE) ||
+    !make_link(US"input",  subdir, id, US"-H", from, to, FALSE))
+  return FALSE;
+
+if (!break_link(US"input",  subdir, id, US"-H", from, FALSE) ||
+    !break_link(US"input",  subdir, id, US"-D", from, FALSE) ||
+    !break_link(US"msglog", subdir, id, US"", from, TRUE))
+  return FALSE;
+
+log_write(0, LOG_MAIN, "moved from %sinput, %smsglog to %sinput, %smsglog",
+   from, from, to, to);
+
+return TRUE;
+}
+
+#endif
+
+/* End of spool_out.c */
diff --git a/src/srs.c b/src/srs.c
new file mode 100644 (file)
index 0000000..0115c0b
--- /dev/null
+++ b/src/srs.c
@@ -0,0 +1,229 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* SRS - Sender rewriting scheme support
+  (C)2004 Miles Wilton <miles@mirtol.com>
+
+  SRS Support Version: 1.0a
+
+  License: GPL */
+
+#include "exim.h"
+#ifdef EXPERIMENTAL_SRS
+
+#include <srs_alt.h>
+#include "srs.h"
+
+srs_t    *srs                   = NULL;
+uschar   *srs_db_forward        = NULL;
+uschar   *srs_db_reverse        = NULL;
+
+
+/* srs_init just initialises libsrs and creates (if necessary)
+   an srs object to use for all srs calls in this instance */
+
+int eximsrs_init()
+{
+  uschar *list = srs_config;
+  uschar secret_buf[SRS_MAX_SECRET_LENGTH];
+  uschar *secret = NULL;
+  uschar sbuf[4];
+  uschar *sbufp;
+
+  /* Check if this instance of Exim has not initialized SRS */
+  if(srs == NULL)
+  {
+    int co = 0;
+    int hashlen, maxage;
+    BOOL usetimestamp, usehash;
+
+    /* Copy config vars */
+    hashlen = srs_hashlength;
+    maxage = srs_maxage;
+    usetimestamp = srs_usetimestamp;
+    usehash = srs_usehash;
+
+    /* Pass srs_config var (overrides new config vars) */
+    co = 0;
+    if(srs_config != NULL)
+    {
+      secret = string_nextinlist(&list, &co, secret_buf, SRS_MAX_SECRET_LENGTH);
+
+      if((sbufp = string_nextinlist(&list, &co, sbuf, sizeof(sbuf))) != NULL)
+        maxage = atoi(sbuf);
+
+      if((sbufp = string_nextinlist(&list, &co, sbuf, sizeof(sbuf))) != NULL)
+        hashlen = atoi(sbuf);
+
+      if((sbufp = string_nextinlist(&list, &co, sbuf, sizeof(sbuf))) != NULL)
+        usetimestamp = atoi(sbuf);
+
+      if((sbufp = string_nextinlist(&list, &co, sbuf, sizeof(sbuf))) != NULL)
+        usehash = atoi(sbuf);
+    }
+
+    if(srs_hashmin == -1)
+      srs_hashmin = hashlen;
+
+    /* First secret specified in secrets? */
+    co = 0;
+    list = srs_secrets;
+    if(secret == NULL || *secret == '\0')
+    {
+      if((secret = string_nextinlist(&list, &co, secret_buf, SRS_MAX_SECRET_LENGTH)) == NULL)
+      {
+        log_write(0, LOG_MAIN | LOG_PANIC,
+            "SRS Configuration Error: No secret specified");
+        return DEFER;
+      }
+    }
+
+    /* Check config */
+    if(maxage < 0 || maxage > 365)
+    {
+      log_write(0, LOG_MAIN | LOG_PANIC,
+          "SRS Configuration Error: Invalid maximum timestamp age");
+      return DEFER;
+    }
+    if(hashlen < 1 || hashlen > 20 || srs_hashmin < 1 || srs_hashmin > 20)
+    {
+      log_write(0, LOG_MAIN | LOG_PANIC,
+          "SRS Configuration Error: Invalid hash length");
+      return DEFER;
+    }
+
+    if((srs = srs_open(secret, Ustrlen(secret), maxage, hashlen, srs_hashmin)) == NULL)
+    {
+      log_write(0, LOG_MAIN | LOG_PANIC,
+          "Failed to allocate SRS memory");
+      return DEFER;
+    }
+
+    srs_set_option(srs, SRS_OPTION_USETIMESTAMP, usetimestamp);
+    srs_set_option(srs, SRS_OPTION_USEHASH, usehash);
+
+    /* Extra secrets? */
+    while((secret = string_nextinlist(&list, &co, secret_buf, SRS_MAX_SECRET_LENGTH)) != NULL)
+        srs_add_secret(srs, secret, (Ustrlen(secret) > SRS_MAX_SECRET_LENGTH) ? SRS_MAX_SECRET_LENGTH :  Ustrlen(secret));
+
+    DEBUG(D_any)
+      debug_printf("SRS initialized\n");
+  }
+
+  return OK;
+}
+
+
+int eximsrs_done()
+{
+  if(srs != NULL)
+    srs_close(srs);
+
+  srs = NULL;
+
+  return OK;
+}
+
+
+int eximsrs_forward(uschar **result, uschar *orig_sender, uschar *domain)
+{
+  char res[512];
+  int n;
+
+  if((n = srs_forward(srs, orig_sender, domain, res, sizeof(res))) & SRS_RESULT_FAIL)
+  {
+    DEBUG(D_any)
+      debug_printf("srs_forward failed (%s, %s): %s\n", orig_sender, domain, srs_geterrormsg(n));
+    return DEFER;
+  }
+
+  *result = string_copy(res);
+  return OK;
+}
+
+
+int eximsrs_reverse(uschar **result, uschar *address)
+{
+  char res[512];
+  int n;
+
+  if((n = srs_reverse(srs, address, res, sizeof(res))) & SRS_RESULT_FAIL)
+  {
+    DEBUG(D_any)
+      debug_printf("srs_reverse failed (%s): %s\n", address, srs_geterrormsg(n));
+    if(n == SRS_RESULT_NOTSRS || n == SRS_RESULT_BADSRS)
+      return DECLINE;
+    if(n == SRS_RESULT_BADHASH || n == SRS_RESULT_BADTIMESTAMP || n == SRS_RESULT_TIMESTAMPEXPIRED)
+      return FAIL;
+    return DEFER;
+  }
+
+  *result = string_copy(res);
+  return OK;
+}
+
+
+int eximsrs_db_set(BOOL reverse, uschar *srs_db)
+{
+  if(reverse)
+    srs_db_reverse = (srs_db == NULL ? NULL : string_copy(srs_db));
+  else
+    srs_db_forward = (srs_db == NULL ? NULL : string_copy(srs_db));
+
+  if(srs_set_db_functions(srs, (srs_db_forward ? eximsrs_db_insert : NULL),
+                               (srs_db_reverse ? eximsrs_db_lookup : NULL)) & SRS_RESULT_FAIL)
+    return DEFER;
+
+  return OK;
+}
+
+
+srs_result eximsrs_db_insert(srs_t *srs, char *data, uint data_len, char *result, uint result_len)
+{
+  uschar *res;
+  uschar buf[64];
+
+  if(srs_db_forward == NULL)
+    return SRS_RESULT_DBERROR;
+
+  srs_db_address = string_copyn(data, data_len);
+  if(srs_generate_unique_id(srs, srs_db_address, buf, 64) & SRS_RESULT_FAIL)
+    return SRS_RESULT_DBERROR;
+
+  srs_db_key = string_copyn(buf, 16);
+
+  if((res = expand_string(srs_db_forward)) == NULL)
+    return SRS_RESULT_DBERROR;
+
+  if(result_len < 17)
+    return SRS_RESULT_DBERROR;
+
+  Ustrncpy(result, srs_db_key, result_len);
+
+  return SRS_RESULT_OK;
+}
+
+
+srs_result eximsrs_db_lookup(srs_t *srs, char *data, uint data_len, char *result, uint result_len)
+{
+  uschar *res;
+
+  if(srs_db_reverse == NULL)
+    return SRS_RESULT_DBERROR;
+
+  srs_db_key = string_copyn(data, data_len);
+  if((res = expand_string(srs_db_reverse)) == NULL)
+    return SRS_RESULT_DBERROR;
+
+  if(Ustrlen(res) >= result_len)
+    return SRS_RESULT_ADDRESSTOOLONG;
+
+  strncpy(result, res, result_len);
+
+  return SRS_RESULT_OK;
+}
+
+
+#endif
+
diff --git a/src/srs.h b/src/srs.h
new file mode 100644 (file)
index 0000000..99463de
--- /dev/null
+++ b/src/srs.h
@@ -0,0 +1,29 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* SRS - Sender rewriting scheme support
+  Â©2004 Miles Wilton <miles@mirtol.com>
+  License: GPL */
+
+#ifndef __SRS_H__
+
+#define __SRS_H__ 1
+
+#ifdef EXPERIMENTAL_SRS
+
+#include "mytypes.h"
+#include <srs_alt.h>
+
+int eximsrs_init();
+int eximsrs_done();
+int eximsrs_forward(uschar **result, uschar *orig_sender, uschar *domain);
+int eximsrs_reverse(uschar **result, uschar *address);
+int eximsrs_db_set(BOOL reverse, uschar *srs_db);
+
+srs_result eximsrs_db_insert(srs_t *srs, char *data, uint data_len, char *result, uint result_len);
+srs_result eximsrs_db_lookup(srs_t *srs, char *data, uint data_len, char *result, uint result_len);
+
+#endif
+
+#endif
diff --git a/src/std-crypto.c b/src/std-crypto.c
new file mode 100644 (file)
index 0000000..3f0fec8
--- /dev/null
@@ -0,0 +1,521 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) Phil Pennock 2012
+ * But almost everything here is fixed published constants from RFCs, so also:
+ * Copyright (C) The Internet Society (2003)
+ * Copyright (C) The IETF Trust (2008)
+ * Most of the text in RFC referencing comments is copy/paste from RFC,
+ * as is undoubtedly the intention.
+ * The constants are generated from that text using util/gen_pkcs3.c invoked
+ * with the -C option.
+ */
+
+/* See the file NOTICE for conditions of use and distribution. */
+
+#include "exim.h"
+
+#ifndef SUPPORT_TLS
+static void dummy(int x) { dummy(x-1); }
+#else
+
+/* The IETF defines standard primes as "Modular Exponential (MODP) Groups" for
+use in IKE in RFC 2409 and 3526, and then some more, "for Use with IETF
+Standards" in RFC 5114.  These have been thoroughly reviewed as meeting
+certain eligibility criteria, which is more than can be said for primes
+generated quickly on no particular criteria.
+
+Any prime used in TLS is disclosed publicly, and if the security of your
+session depends upon the prime being secret, then one of three situations
+holds:
+ (1) the prime is too small
+ (2) the prime is flawed, use one of these instead
+ (3) you know of fundamental cryptanalytic breaks not currently publicly known
+     to the cryptographic community.
+*/
+
+/* RFC 2409 MODP IKE_id=1 generator=2 bits=768
+   The prime is: 2^768 - 2 ^704 - 1 + 2^64 * { [2^638 pi] + 149686 }
+   Its hexadecimal value is
+         FFFFFFFF FFFFFFFF C90FDAA2 2168C234 C4C6628B 80DC1CD1
+         29024E08 8A67CC74 020BBEA6 3B139B22 514A0879 8E3404DD
+         EF9519B3 CD3A431B 302B0A6D F25F1437 4FE1356D 6D51C245
+         E485B576 625E7EC6 F44C42E9 A63A3620 FFFFFFFF FFFFFFFF
+*/
+static const char dh_ike_1_pem[] =
+"-----BEGIN DH PARAMETERS-----\n"
+"MGYCYQD//////////8kP2qIhaMI0xMZii4DcHNEpAk4IimfMdAILvqY7E5siUUoI\n"
+"eY40BN3vlRmzzTpDGzArCm3yXxQ3T+E1bW1RwkXkhbV2Yl5+xvRMQummOjYg////\n"
+"//////8CAQI=\n"
+"-----END DH PARAMETERS-----\n";
+
+/* RFC 2409 MODP IKE_id=2 generator=2 bits=1024
+   The prime is 2^1024 - 2^960 - 1 + 2^64 * { [2^894 pi] + 129093 }.
+   Its hexadecimal value is
+
+         FFFFFFFF FFFFFFFF C90FDAA2 2168C234 C4C6628B 80DC1CD1
+         29024E08 8A67CC74 020BBEA6 3B139B22 514A0879 8E3404DD
+         EF9519B3 CD3A431B 302B0A6D F25F1437 4FE1356D 6D51C245
+         E485B576 625E7EC6 F44C42E9 A637ED6B 0BFF5CB6 F406B7ED
+         EE386BFB 5A899FA5 AE9F2411 7C4B1FE6 49286651 ECE65381
+         FFFFFFFF FFFFFFFF
+*/
+static const char dh_ike_2_pem[] =
+"-----BEGIN DH PARAMETERS-----\n"
+"MIGHAoGBAP//////////yQ/aoiFowjTExmKLgNwc0SkCTgiKZ8x0Agu+pjsTmyJR\n"
+"Sgh5jjQE3e+VGbPNOkMbMCsKbfJfFDdP4TVtbVHCReSFtXZiXn7G9ExC6aY37WsL\n"
+"/1y29Aa37e44a/taiZ+lrp8kEXxLH+ZJKGZR7OZTgf//////////AgEC\n"
+"-----END DH PARAMETERS-----\n";
+
+/* RFC 2409; id=3 and id=4 are EC2N, not yet supported here */
+
+/* RFC 3526 MODP IKE_id=5 generator=2 bits=1536
+   The prime is: 2^1536 - 2^1472 - 1 + 2^64 * { [2^1406 pi] + 741804 }
+   Its hexadecimal value is:
+
+      FFFFFFFF FFFFFFFF C90FDAA2 2168C234 C4C6628B 80DC1CD1
+      29024E08 8A67CC74 020BBEA6 3B139B22 514A0879 8E3404DD
+      EF9519B3 CD3A431B 302B0A6D F25F1437 4FE1356D 6D51C245
+      E485B576 625E7EC6 F44C42E9 A637ED6B 0BFF5CB6 F406B7ED
+      EE386BFB 5A899FA5 AE9F2411 7C4B1FE6 49286651 ECE45B3D
+      C2007CB8 A163BF05 98DA4836 1C55D39A 69163FA8 FD24CF5F
+      83655D23 DCA3AD96 1C62F356 208552BB 9ED52907 7096966D
+      670C354E 4ABC9804 F1746C08 CA237327 FFFFFFFF FFFFFFFF
+*/
+static const char dh_ike_5_pem[] =
+"-----BEGIN DH PARAMETERS-----\n"
+"MIHHAoHBAP//////////yQ/aoiFowjTExmKLgNwc0SkCTgiKZ8x0Agu+pjsTmyJR\n"
+"Sgh5jjQE3e+VGbPNOkMbMCsKbfJfFDdP4TVtbVHCReSFtXZiXn7G9ExC6aY37WsL\n"
+"/1y29Aa37e44a/taiZ+lrp8kEXxLH+ZJKGZR7ORbPcIAfLihY78FmNpINhxV05pp\n"
+"Fj+o/STPX4NlXSPco62WHGLzViCFUrue1SkHcJaWbWcMNU5KvJgE8XRsCMojcyf/\n"
+"/////////wIBAg==\n"
+"-----END DH PARAMETERS-----\n";
+
+/* RFC 3526 MODP IKE_id=14 generator=2 bits=2048
+   This prime is: 2^2048 - 2^1984 - 1 + 2^64 * { [2^1918 pi] + 124476 }
+   Its hexadecimal value is:
+
+      FFFFFFFF FFFFFFFF C90FDAA2 2168C234 C4C6628B 80DC1CD1
+      29024E08 8A67CC74 020BBEA6 3B139B22 514A0879 8E3404DD
+      EF9519B3 CD3A431B 302B0A6D F25F1437 4FE1356D 6D51C245
+      E485B576 625E7EC6 F44C42E9 A637ED6B 0BFF5CB6 F406B7ED
+      EE386BFB 5A899FA5 AE9F2411 7C4B1FE6 49286651 ECE45B3D
+      C2007CB8 A163BF05 98DA4836 1C55D39A 69163FA8 FD24CF5F
+      83655D23 DCA3AD96 1C62F356 208552BB 9ED52907 7096966D
+      670C354E 4ABC9804 F1746C08 CA18217C 32905E46 2E36CE3B
+      E39E772C 180E8603 9B2783A2 EC07A28F B5C55DF0 6F4C52C9
+      DE2BCBF6 95581718 3995497C EA956AE5 15D22618 98FA0510
+      15728E5A 8AACAA68 FFFFFFFF FFFFFFFF
+*/
+static const char dh_ike_14_pem[] =
+"-----BEGIN DH PARAMETERS-----\n"
+"MIIBCAKCAQEA///////////JD9qiIWjCNMTGYouA3BzRKQJOCIpnzHQCC76mOxOb\n"
+"IlFKCHmONATd75UZs806QxswKwpt8l8UN0/hNW1tUcJF5IW1dmJefsb0TELppjft\n"
+"awv/XLb0Brft7jhr+1qJn6WunyQRfEsf5kkoZlHs5Fs9wgB8uKFjvwWY2kg2HFXT\n"
+"mmkWP6j9JM9fg2VdI9yjrZYcYvNWIIVSu57VKQdwlpZtZww1Tkq8mATxdGwIyhgh\n"
+"fDKQXkYuNs474553LBgOhgObJ4Oi7Aeij7XFXfBvTFLJ3ivL9pVYFxg5lUl86pVq\n"
+"5RXSJhiY+gUQFXKOWoqsqmj//////////wIBAg==\n"
+"-----END DH PARAMETERS-----\n";
+
+/* RFC 3526 MODP IKE_id=15 generator=2 bits=3072
+   This prime is: 2^3072 - 2^3008 - 1 + 2^64 * { [2^2942 pi] + 1690314 }
+   Its hexadecimal value is:
+
+      FFFFFFFF FFFFFFFF C90FDAA2 2168C234 C4C6628B 80DC1CD1
+      29024E08 8A67CC74 020BBEA6 3B139B22 514A0879 8E3404DD
+      EF9519B3 CD3A431B 302B0A6D F25F1437 4FE1356D 6D51C245
+      E485B576 625E7EC6 F44C42E9 A637ED6B 0BFF5CB6 F406B7ED
+      EE386BFB 5A899FA5 AE9F2411 7C4B1FE6 49286651 ECE45B3D
+      C2007CB8 A163BF05 98DA4836 1C55D39A 69163FA8 FD24CF5F
+      83655D23 DCA3AD96 1C62F356 208552BB 9ED52907 7096966D
+      670C354E 4ABC9804 F1746C08 CA18217C 32905E46 2E36CE3B
+      E39E772C 180E8603 9B2783A2 EC07A28F B5C55DF0 6F4C52C9
+      DE2BCBF6 95581718 3995497C EA956AE5 15D22618 98FA0510
+      15728E5A 8AAAC42D AD33170D 04507A33 A85521AB DF1CBA64
+      ECFB8504 58DBEF0A 8AEA7157 5D060C7D B3970F85 A6E1E4C7
+      ABF5AE8C DB0933D7 1E8C94E0 4A25619D CEE3D226 1AD2EE6B
+      F12FFA06 D98A0864 D8760273 3EC86A64 521F2B18 177B200C
+      BBE11757 7A615D6C 770988C0 BAD946E2 08E24FA0 74E5AB31
+      43DB5BFC E0FD108E 4B82D120 A93AD2CA FFFFFFFF FFFFFFFF
+*/
+static const char dh_ike_15_pem[] =
+"-----BEGIN DH PARAMETERS-----\n"
+"MIIBiAKCAYEA///////////JD9qiIWjCNMTGYouA3BzRKQJOCIpnzHQCC76mOxOb\n"
+"IlFKCHmONATd75UZs806QxswKwpt8l8UN0/hNW1tUcJF5IW1dmJefsb0TELppjft\n"
+"awv/XLb0Brft7jhr+1qJn6WunyQRfEsf5kkoZlHs5Fs9wgB8uKFjvwWY2kg2HFXT\n"
+"mmkWP6j9JM9fg2VdI9yjrZYcYvNWIIVSu57VKQdwlpZtZww1Tkq8mATxdGwIyhgh\n"
+"fDKQXkYuNs474553LBgOhgObJ4Oi7Aeij7XFXfBvTFLJ3ivL9pVYFxg5lUl86pVq\n"
+"5RXSJhiY+gUQFXKOWoqqxC2tMxcNBFB6M6hVIavfHLpk7PuFBFjb7wqK6nFXXQYM\n"
+"fbOXD4Wm4eTHq/WujNsJM9cejJTgSiVhnc7j0iYa0u5r8S/6BtmKCGTYdgJzPshq\n"
+"ZFIfKxgXeyAMu+EXV3phXWx3CYjAutlG4gjiT6B05asxQ9tb/OD9EI5LgtEgqTrS\n"
+"yv//////////AgEC\n"
+"-----END DH PARAMETERS-----\n";
+
+/* RFC 3526 MODP IKE_id=16 generator=2 bits=4096
+   This prime is: 2^4096 - 2^4032 - 1 + 2^64 * { [2^3966 pi] + 240904 }
+   Its hexadecimal value is:
+
+      FFFFFFFF FFFFFFFF C90FDAA2 2168C234 C4C6628B 80DC1CD1
+      29024E08 8A67CC74 020BBEA6 3B139B22 514A0879 8E3404DD
+      EF9519B3 CD3A431B 302B0A6D F25F1437 4FE1356D 6D51C245
+      E485B576 625E7EC6 F44C42E9 A637ED6B 0BFF5CB6 F406B7ED
+      EE386BFB 5A899FA5 AE9F2411 7C4B1FE6 49286651 ECE45B3D
+      C2007CB8 A163BF05 98DA4836 1C55D39A 69163FA8 FD24CF5F
+      83655D23 DCA3AD96 1C62F356 208552BB 9ED52907 7096966D
+      670C354E 4ABC9804 F1746C08 CA18217C 32905E46 2E36CE3B
+      E39E772C 180E8603 9B2783A2 EC07A28F B5C55DF0 6F4C52C9
+      DE2BCBF6 95581718 3995497C EA956AE5 15D22618 98FA0510
+      15728E5A 8AAAC42D AD33170D 04507A33 A85521AB DF1CBA64
+      ECFB8504 58DBEF0A 8AEA7157 5D060C7D B3970F85 A6E1E4C7
+      ABF5AE8C DB0933D7 1E8C94E0 4A25619D CEE3D226 1AD2EE6B
+      F12FFA06 D98A0864 D8760273 3EC86A64 521F2B18 177B200C
+      BBE11757 7A615D6C 770988C0 BAD946E2 08E24FA0 74E5AB31
+      43DB5BFC E0FD108E 4B82D120 A9210801 1A723C12 A787E6D7
+      88719A10 BDBA5B26 99C32718 6AF4E23C 1A946834 B6150BDA
+      2583E9CA 2AD44CE8 DBBBC2DB 04DE8EF9 2E8EFC14 1FBECAA6
+      287C5947 4E6BC05D 99B2964F A090C3A2 233BA186 515BE7ED
+      1F612970 CEE2D7AF B81BDD76 2170481C D0069127 D5B05AA9
+      93B4EA98 8D8FDDC1 86FFB7DC 90A6C08F 4DF435C9 34063199
+      FFFFFFFF FFFFFFFF
+*/
+static const char dh_ike_16_pem[] =
+"-----BEGIN DH PARAMETERS-----\n"
+"MIICCAKCAgEA///////////JD9qiIWjCNMTGYouA3BzRKQJOCIpnzHQCC76mOxOb\n"
+"IlFKCHmONATd75UZs806QxswKwpt8l8UN0/hNW1tUcJF5IW1dmJefsb0TELppjft\n"
+"awv/XLb0Brft7jhr+1qJn6WunyQRfEsf5kkoZlHs5Fs9wgB8uKFjvwWY2kg2HFXT\n"
+"mmkWP6j9JM9fg2VdI9yjrZYcYvNWIIVSu57VKQdwlpZtZww1Tkq8mATxdGwIyhgh\n"
+"fDKQXkYuNs474553LBgOhgObJ4Oi7Aeij7XFXfBvTFLJ3ivL9pVYFxg5lUl86pVq\n"
+"5RXSJhiY+gUQFXKOWoqqxC2tMxcNBFB6M6hVIavfHLpk7PuFBFjb7wqK6nFXXQYM\n"
+"fbOXD4Wm4eTHq/WujNsJM9cejJTgSiVhnc7j0iYa0u5r8S/6BtmKCGTYdgJzPshq\n"
+"ZFIfKxgXeyAMu+EXV3phXWx3CYjAutlG4gjiT6B05asxQ9tb/OD9EI5LgtEgqSEI\n"
+"ARpyPBKnh+bXiHGaEL26WyaZwycYavTiPBqUaDS2FQvaJYPpyirUTOjbu8LbBN6O\n"
+"+S6O/BQfvsqmKHxZR05rwF2ZspZPoJDDoiM7oYZRW+ftH2EpcM7i16+4G912IXBI\n"
+"HNAGkSfVsFqpk7TqmI2P3cGG/7fckKbAj030Nck0BjGZ//////////8CAQI=\n"
+"-----END DH PARAMETERS-----\n";
+
+/* RFC 3526 MODP IKE_id=17 generator=2 bits=6144
+   This prime is: 2^6144 - 2^6080 - 1 + 2^64 * { [2^6014 pi] + 929484 }
+   Its hexadecimal value is:
+
+   FFFFFFFF FFFFFFFF C90FDAA2 2168C234 C4C6628B 80DC1CD1 29024E08
+   8A67CC74 020BBEA6 3B139B22 514A0879 8E3404DD EF9519B3 CD3A431B
+   302B0A6D F25F1437 4FE1356D 6D51C245 E485B576 625E7EC6 F44C42E9
+   A637ED6B 0BFF5CB6 F406B7ED EE386BFB 5A899FA5 AE9F2411 7C4B1FE6
+   49286651 ECE45B3D C2007CB8 A163BF05 98DA4836 1C55D39A 69163FA8
+   FD24CF5F 83655D23 DCA3AD96 1C62F356 208552BB 9ED52907 7096966D
+   670C354E 4ABC9804 F1746C08 CA18217C 32905E46 2E36CE3B E39E772C
+   180E8603 9B2783A2 EC07A28F B5C55DF0 6F4C52C9 DE2BCBF6 95581718
+   3995497C EA956AE5 15D22618 98FA0510 15728E5A 8AAAC42D AD33170D
+   04507A33 A85521AB DF1CBA64 ECFB8504 58DBEF0A 8AEA7157 5D060C7D
+   B3970F85 A6E1E4C7 ABF5AE8C DB0933D7 1E8C94E0 4A25619D CEE3D226
+   1AD2EE6B F12FFA06 D98A0864 D8760273 3EC86A64 521F2B18 177B200C
+   BBE11757 7A615D6C 770988C0 BAD946E2 08E24FA0 74E5AB31 43DB5BFC
+   E0FD108E 4B82D120 A9210801 1A723C12 A787E6D7 88719A10 BDBA5B26
+   99C32718 6AF4E23C 1A946834 B6150BDA 2583E9CA 2AD44CE8 DBBBC2DB
+   04DE8EF9 2E8EFC14 1FBECAA6 287C5947 4E6BC05D 99B2964F A090C3A2
+   233BA186 515BE7ED 1F612970 CEE2D7AF B81BDD76 2170481C D0069127
+   D5B05AA9 93B4EA98 8D8FDDC1 86FFB7DC 90A6C08F 4DF435C9 34028492
+   36C3FAB4 D27C7026 C1D4DCB2 602646DE C9751E76 3DBA37BD F8FF9406
+   AD9E530E E5DB382F 413001AE B06A53ED 9027D831 179727B0 865A8918
+   DA3EDBEB CF9B14ED 44CE6CBA CED4BB1B DB7F1447 E6CC254B 33205151
+   2BD7AF42 6FB8F401 378CD2BF 5983CA01 C64B92EC F032EA15 D1721D03
+   F482D7CE 6E74FEF6 D55E702F 46980C82 B5A84031 900B1C9E 59E7C97F
+   BEC7E8F3 23A97A7E 36CC88BE 0F1D45B7 FF585AC5 4BD407B2 2B4154AA
+   CC8F6D7E BF48E1D8 14CC5ED2 0F8037E0 A79715EE F29BE328 06A1D58B
+   B7C5DA76 F550AA3D 8A1FBFF0 EB19CCB1 A313D55C DA56C9EC 2EF29632
+   387FE8D7 6E3C0468 043E8F66 3F4860EE 12BF2D5B 0B7474D6 E694F91E
+   6DCC4024 FFFFFFFF FFFFFFFF
+*/
+static const char dh_ike_17_pem[] =
+"-----BEGIN DH PARAMETERS-----\n"
+"MIIDCAKCAwEA///////////JD9qiIWjCNMTGYouA3BzRKQJOCIpnzHQCC76mOxOb\n"
+"IlFKCHmONATd75UZs806QxswKwpt8l8UN0/hNW1tUcJF5IW1dmJefsb0TELppjft\n"
+"awv/XLb0Brft7jhr+1qJn6WunyQRfEsf5kkoZlHs5Fs9wgB8uKFjvwWY2kg2HFXT\n"
+"mmkWP6j9JM9fg2VdI9yjrZYcYvNWIIVSu57VKQdwlpZtZww1Tkq8mATxdGwIyhgh\n"
+"fDKQXkYuNs474553LBgOhgObJ4Oi7Aeij7XFXfBvTFLJ3ivL9pVYFxg5lUl86pVq\n"
+"5RXSJhiY+gUQFXKOWoqqxC2tMxcNBFB6M6hVIavfHLpk7PuFBFjb7wqK6nFXXQYM\n"
+"fbOXD4Wm4eTHq/WujNsJM9cejJTgSiVhnc7j0iYa0u5r8S/6BtmKCGTYdgJzPshq\n"
+"ZFIfKxgXeyAMu+EXV3phXWx3CYjAutlG4gjiT6B05asxQ9tb/OD9EI5LgtEgqSEI\n"
+"ARpyPBKnh+bXiHGaEL26WyaZwycYavTiPBqUaDS2FQvaJYPpyirUTOjbu8LbBN6O\n"
+"+S6O/BQfvsqmKHxZR05rwF2ZspZPoJDDoiM7oYZRW+ftH2EpcM7i16+4G912IXBI\n"
+"HNAGkSfVsFqpk7TqmI2P3cGG/7fckKbAj030Nck0AoSSNsP6tNJ8cCbB1NyyYCZG\n"
+"3sl1HnY9uje9+P+UBq2eUw7l2zgvQTABrrBqU+2QJ9gxF5cnsIZaiRjaPtvrz5sU\n"
+"7UTObLrO1Lsb238UR+bMJUszIFFRK9evQm+49AE3jNK/WYPKAcZLkuzwMuoV0XId\n"
+"A/SC185udP721V5wL0aYDIK1qEAxkAscnlnnyX++x+jzI6l6fjbMiL4PHUW3/1ha\n"
+"xUvUB7IrQVSqzI9tfr9I4dgUzF7SD4A34KeXFe7ym+MoBqHVi7fF2nb1UKo9ih+/\n"
+"8OsZzLGjE9Vc2lbJ7C7yljI4f+jXbjwEaAQ+j2Y/SGDuEr8tWwt0dNbmlPkebcxA\n"
+"JP//////////AgEC\n"
+"-----END DH PARAMETERS-----\n";
+
+/* RFC 3526 MODP IKE_id=18 generator=2 bits=8192
+   This prime is: 2^8192 - 2^8128 - 1 + 2^64 * { [2^8062 pi] + 4743158 }
+   Its hexadecimal value is:
+
+      FFFFFFFF FFFFFFFF C90FDAA2 2168C234 C4C6628B 80DC1CD1
+      29024E08 8A67CC74 020BBEA6 3B139B22 514A0879 8E3404DD
+      EF9519B3 CD3A431B 302B0A6D F25F1437 4FE1356D 6D51C245
+      E485B576 625E7EC6 F44C42E9 A637ED6B 0BFF5CB6 F406B7ED
+      EE386BFB 5A899FA5 AE9F2411 7C4B1FE6 49286651 ECE45B3D
+      C2007CB8 A163BF05 98DA4836 1C55D39A 69163FA8 FD24CF5F
+      83655D23 DCA3AD96 1C62F356 208552BB 9ED52907 7096966D
+      670C354E 4ABC9804 F1746C08 CA18217C 32905E46 2E36CE3B
+      E39E772C 180E8603 9B2783A2 EC07A28F B5C55DF0 6F4C52C9
+      DE2BCBF6 95581718 3995497C EA956AE5 15D22618 98FA0510
+      15728E5A 8AAAC42D AD33170D 04507A33 A85521AB DF1CBA64
+      ECFB8504 58DBEF0A 8AEA7157 5D060C7D B3970F85 A6E1E4C7
+      ABF5AE8C DB0933D7 1E8C94E0 4A25619D CEE3D226 1AD2EE6B
+      F12FFA06 D98A0864 D8760273 3EC86A64 521F2B18 177B200C
+      BBE11757 7A615D6C 770988C0 BAD946E2 08E24FA0 74E5AB31
+      43DB5BFC E0FD108E 4B82D120 A9210801 1A723C12 A787E6D7
+      88719A10 BDBA5B26 99C32718 6AF4E23C 1A946834 B6150BDA
+      2583E9CA 2AD44CE8 DBBBC2DB 04DE8EF9 2E8EFC14 1FBECAA6
+      287C5947 4E6BC05D 99B2964F A090C3A2 233BA186 515BE7ED
+      1F612970 CEE2D7AF B81BDD76 2170481C D0069127 D5B05AA9
+      93B4EA98 8D8FDDC1 86FFB7DC 90A6C08F 4DF435C9 34028492
+      36C3FAB4 D27C7026 C1D4DCB2 602646DE C9751E76 3DBA37BD
+      F8FF9406 AD9E530E E5DB382F 413001AE B06A53ED 9027D831
+      179727B0 865A8918 DA3EDBEB CF9B14ED 44CE6CBA CED4BB1B
+      DB7F1447 E6CC254B 33205151 2BD7AF42 6FB8F401 378CD2BF
+      5983CA01 C64B92EC F032EA15 D1721D03 F482D7CE 6E74FEF6
+      D55E702F 46980C82 B5A84031 900B1C9E 59E7C97F BEC7E8F3
+      23A97A7E 36CC88BE 0F1D45B7 FF585AC5 4BD407B2 2B4154AA
+      CC8F6D7E BF48E1D8 14CC5ED2 0F8037E0 A79715EE F29BE328
+      06A1D58B B7C5DA76 F550AA3D 8A1FBFF0 EB19CCB1 A313D55C
+      DA56C9EC 2EF29632 387FE8D7 6E3C0468 043E8F66 3F4860EE
+      12BF2D5B 0B7474D6 E694F91E 6DBE1159 74A3926F 12FEE5E4
+      38777CB6 A932DF8C D8BEC4D0 73B931BA 3BC832B6 8D9DD300
+      741FA7BF 8AFC47ED 2576F693 6BA42466 3AAB639C 5AE4F568
+      3423B474 2BF1C978 238F16CB E39D652D E3FDB8BE FC848AD9
+      22222E04 A4037C07 13EB57A8 1A23F0C7 3473FC64 6CEA306B
+      4BCBC886 2F8385DD FA9D4B7F A2C087E8 79683303 ED5BDD3A
+      062B3CF5 B3A278A6 6D2A13F8 3F44F82D DF310EE0 74AB6A36
+      4597E899 A0255DC1 64F31CC5 0846851D F9AB4819 5DED7EA1
+      B1D510BD 7EE74D73 FAF36BC3 1ECFA268 359046F4 EB879F92
+      4009438B 481C6CD7 889A002E D5EE382B C9190DA6 FC026E47
+      9558E447 5677E9AA 9E3050E2 765694DF C81F56E8 80B96E71
+      60C980DD 98EDD3DF FFFFFFFF FFFFFFFF
+*/
+static const char dh_ike_18_pem[] =
+"-----BEGIN DH PARAMETERS-----\n"
+"MIIECAKCBAEA///////////JD9qiIWjCNMTGYouA3BzRKQJOCIpnzHQCC76mOxOb\n"
+"IlFKCHmONATd75UZs806QxswKwpt8l8UN0/hNW1tUcJF5IW1dmJefsb0TELppjft\n"
+"awv/XLb0Brft7jhr+1qJn6WunyQRfEsf5kkoZlHs5Fs9wgB8uKFjvwWY2kg2HFXT\n"
+"mmkWP6j9JM9fg2VdI9yjrZYcYvNWIIVSu57VKQdwlpZtZww1Tkq8mATxdGwIyhgh\n"
+"fDKQXkYuNs474553LBgOhgObJ4Oi7Aeij7XFXfBvTFLJ3ivL9pVYFxg5lUl86pVq\n"
+"5RXSJhiY+gUQFXKOWoqqxC2tMxcNBFB6M6hVIavfHLpk7PuFBFjb7wqK6nFXXQYM\n"
+"fbOXD4Wm4eTHq/WujNsJM9cejJTgSiVhnc7j0iYa0u5r8S/6BtmKCGTYdgJzPshq\n"
+"ZFIfKxgXeyAMu+EXV3phXWx3CYjAutlG4gjiT6B05asxQ9tb/OD9EI5LgtEgqSEI\n"
+"ARpyPBKnh+bXiHGaEL26WyaZwycYavTiPBqUaDS2FQvaJYPpyirUTOjbu8LbBN6O\n"
+"+S6O/BQfvsqmKHxZR05rwF2ZspZPoJDDoiM7oYZRW+ftH2EpcM7i16+4G912IXBI\n"
+"HNAGkSfVsFqpk7TqmI2P3cGG/7fckKbAj030Nck0AoSSNsP6tNJ8cCbB1NyyYCZG\n"
+"3sl1HnY9uje9+P+UBq2eUw7l2zgvQTABrrBqU+2QJ9gxF5cnsIZaiRjaPtvrz5sU\n"
+"7UTObLrO1Lsb238UR+bMJUszIFFRK9evQm+49AE3jNK/WYPKAcZLkuzwMuoV0XId\n"
+"A/SC185udP721V5wL0aYDIK1qEAxkAscnlnnyX++x+jzI6l6fjbMiL4PHUW3/1ha\n"
+"xUvUB7IrQVSqzI9tfr9I4dgUzF7SD4A34KeXFe7ym+MoBqHVi7fF2nb1UKo9ih+/\n"
+"8OsZzLGjE9Vc2lbJ7C7yljI4f+jXbjwEaAQ+j2Y/SGDuEr8tWwt0dNbmlPkebb4R\n"
+"WXSjkm8S/uXkOHd8tqky34zYvsTQc7kxujvIMraNndMAdB+nv4r8R+0ldvaTa6Qk\n"
+"ZjqrY5xa5PVoNCO0dCvxyXgjjxbL451lLeP9uL78hIrZIiIuBKQDfAcT61eoGiPw\n"
+"xzRz/GRs6jBrS8vIhi+Dhd36nUt/osCH6HloMwPtW906Bis89bOieKZtKhP4P0T4\n"
+"Ld8xDuB0q2o2RZfomaAlXcFk8xzFCEaFHfmrSBld7X6hsdUQvX7nTXP682vDHs+i\n"
+"aDWQRvTrh5+SQAlDi0gcbNeImgAu1e44K8kZDab8Am5HlVjkR1Z36aqeMFDidlaU\n"
+"38gfVuiAuW5xYMmA3Zjt09///////////wIBAg==\n"
+"-----END DH PARAMETERS-----\n";
+
+/* RFC 5114 IKE_id=22
+2.1.  1024-bit MODP Group with 160-bit Prime Order Subgroup
+
+   The hexadecimal value of the prime is:
+
+   p = B10B8F96 A080E01D DE92DE5E AE5D54EC 52C99FBC FB06A3C6
+       9A6A9DCA 52D23B61 6073E286 75A23D18 9838EF1E 2EE652C0
+       13ECB4AE A9061123 24975C3C D49B83BF ACCBDD7D 90C4BD70
+       98488E9C 219A7372 4EFFD6FA E5644738 FAA31A4F F55BCCC0
+       A151AF5F 0DC8B4BD 45BF37DF 365C1A65 E68CFDA7 6D4DA708
+       DF1FB2BC 2E4A4371
+
+   The hexadecimal value of the generator is:
+
+   g = A4D1CBD5 C3FD3412 6765A442 EFB99905 F8104DD2 58AC507F
+       D6406CFF 14266D31 266FEA1E 5C41564B 777E690F 5504F213
+       160217B4 B01B886A 5E91547F 9E2749F4 D7FBD7D3 B9A92EE1
+       909D0D22 63F80A76 A6A24C08 7A091F53 1DBF0A01 69B6A28A
+       D662A4D1 8E73AFA3 2D779D59 18D08BC8 858F4DCE F97C2A24
+       855E6EEB 22B3B2E5
+
+   The generator generates a prime-order subgroup of size:
+
+   q = F518AA87 81A8DF27 8ABA4E7D 64B7CB9D 49462353
+*/
+static const char dh_ike_22_pem[] =
+"-----BEGIN DH PARAMETERS-----\n"
+"MIIBCAKBgQCxC4+WoIDgHd6S3l6uXVTsUsmfvPsGo8aaap3KUtI7YWBz4oZ1oj0Y\n"
+"mDjvHi7mUsAT7LSuqQYRIySXXDzUm4O/rMvdfZDEvXCYSI6cIZpzck7/1vrlZEc4\n"
+"+qMaT/VbzMChUa9fDci0vUW/N982XBpl5oz9p21NpwjfH7K8LkpDcQKBgQCk0cvV\n"
+"w/00EmdlpELvuZkF+BBN0lisUH/WQGz/FCZtMSZv6h5cQVZLd35pD1UE8hMWAhe0\n"
+"sBuIal6RVH+eJ0n01/vX07mpLuGQnQ0iY/gKdqaiTAh6CR9THb8KAWm2oorWYqTR\n"
+"jnOvoy13nVkY0IvIhY9Nzvl8KiSFXm7rIrOy5Q==\n"
+"-----END DH PARAMETERS-----\n";
+
+/* RFC 5114 IKE_id=23
+2.2.  2048-bit MODP Group with 224-bit Prime Order Subgroup
+
+   The hexadecimal value of the prime is:
+
+   p =  AD107E1E 9123A9D0 D660FAA7 9559C51F A20D64E5 683B9FD1
+        B54B1597 B61D0A75 E6FA141D F95A56DB AF9A3C40 7BA1DF15
+        EB3D688A 309C180E 1DE6B85A 1274A0A6 6D3F8152 AD6AC212
+        9037C9ED EFDA4DF8 D91E8FEF 55B7394B 7AD5B7D0 B6C12207
+        C9F98D11 ED34DBF6 C6BA0B2C 8BBC27BE 6A00E0A0 B9C49708
+        B3BF8A31 70918836 81286130 BC8985DB 1602E714 415D9330
+        278273C7 DE31EFDC 7310F712 1FD5A074 15987D9A DC0A486D
+        CDF93ACC 44328387 315D75E1 98C641A4 80CD86A1 B9E587E8
+        BE60E69C C928B2B9 C52172E4 13042E9B 23F10B0E 16E79763
+        C9B53DCF 4BA80A29 E3FB73C1 6B8E75B9 7EF363E2 FFA31F71
+        CF9DE538 4E71B81C 0AC4DFFE 0C10E64F
+
+   The hexadecimal value of the generator is:
+
+   g =  AC4032EF 4F2D9AE3 9DF30B5C 8FFDAC50 6CDEBE7B 89998CAF
+        74866A08 CFE4FFE3 A6824A4E 10B9A6F0 DD921F01 A70C4AFA
+        AB739D77 00C29F52 C57DB17C 620A8652 BE5E9001 A8D66AD7
+        C1766910 1999024A F4D02727 5AC1348B B8A762D0 521BC98A
+        E2471504 22EA1ED4 09939D54 DA7460CD B5F6C6B2 50717CBE
+        F180EB34 118E98D1 19529A45 D6F83456 6E3025E3 16A330EF
+        BB77A86F 0C1AB15B 051AE3D4 28C8F8AC B70A8137 150B8EEB
+        10E183ED D19963DD D9E263E4 770589EF 6AA21E7F 5F2FF381
+        B539CCE3 409D13CD 566AFBB4 8D6C0191 81E1BCFE 94B30269
+        EDFE72FE 9B6AA4BD 7B5A0F1C 71CFFF4C 19C418E1 F6EC0179
+        81BC087F 2A7065B3 84B890D3 191F2BFA
+
+   The generator generates a prime-order subgroup of size:
+
+   q =  801C0D34 C58D93FE 99717710 1F80535A 4738CEBC BF389A99
+        B36371EB
+*/
+static const char dh_ike_23_pem[] =
+"-----BEGIN DH PARAMETERS-----\n"
+"MIICCgKCAQEArRB+HpEjqdDWYPqnlVnFH6INZOVoO5/RtUsVl7YdCnXm+hQd+VpW\n"
+"26+aPEB7od8V6z1oijCcGA4d5rhaEnSgpm0/gVKtasISkDfJ7e/aTfjZHo/vVbc5\n"
+"S3rVt9C2wSIHyfmNEe002/bGugssi7wnvmoA4KC5xJcIs7+KMXCRiDaBKGEwvImF\n"
+"2xYC5xRBXZMwJ4Jzx94x79xzEPcSH9WgdBWYfZrcCkhtzfk6zEQyg4cxXXXhmMZB\n"
+"pIDNhqG55YfovmDmnMkosrnFIXLkEwQumyPxCw4W55djybU9z0uoCinj+3PBa451\n"
+"uX7zY+L/ox9xz53lOE5xuBwKxN/+DBDmTwKCAQEArEAy708tmuOd8wtcj/2sUGze\n"
+"vnuJmYyvdIZqCM/k/+OmgkpOELmm8N2SHwGnDEr6q3OddwDCn1LFfbF8YgqGUr5e\n"
+"kAGo1mrXwXZpEBmZAkr00CcnWsE0i7inYtBSG8mK4kcVBCLqHtQJk51U2nRgzbX2\n"
+"xrJQcXy+8YDrNBGOmNEZUppF1vg0Vm4wJeMWozDvu3eobwwasVsFGuPUKMj4rLcK\n"
+"gTcVC47rEOGD7dGZY93Z4mPkdwWJ72qiHn9fL/OBtTnM40CdE81Wavu0jWwBkYHh\n"
+"vP6UswJp7f5y/ptqpL17Wg8ccc//TBnEGOH27AF5gbwIfypwZbOEuJDTGR8r+g==\n"
+"-----END DH PARAMETERS-----\n";
+
+/* RFC 5114 IKE_id=24
+2.3.  2048-bit MODP Group with 256-bit Prime Order Subgroup
+
+   The hexadecimal value of the prime is:
+
+   p = 87A8E61D B4B6663C FFBBD19C 65195999 8CEEF608 660DD0F2
+       5D2CEED4 435E3B00 E00DF8F1 D61957D4 FAF7DF45 61B2AA30
+       16C3D911 34096FAA 3BF4296D 830E9A7C 209E0C64 97517ABD
+       5A8A9D30 6BCF67ED 91F9E672 5B4758C0 22E0B1EF 4275BF7B
+       6C5BFC11 D45F9088 B941F54E B1E59BB8 BC39A0BF 12307F5C
+       4FDB70C5 81B23F76 B63ACAE1 CAA6B790 2D525267 35488A0E
+       F13C6D9A 51BFA4AB 3AD83477 96524D8E F6A167B5 A41825D9
+       67E144E5 14056425 1CCACB83 E6B486F6 B3CA3F79 71506026
+       C0B857F6 89962856 DED4010A BD0BE621 C3A3960A 54E710C3
+       75F26375 D7014103 A4B54330 C198AF12 6116D227 6E11715F
+       693877FA D7EF09CA DB094AE9 1E1A1597
+
+   The hexadecimal value of the generator is:
+
+   g = 3FB32C9B 73134D0B 2E775066 60EDBD48 4CA7B18F 21EF2054
+       07F4793A 1A0BA125 10DBC150 77BE463F FF4FED4A AC0BB555
+       BE3A6C1B 0C6B47B1 BC3773BF 7E8C6F62 901228F8 C28CBB18
+       A55AE313 41000A65 0196F931 C77A57F2 DDF463E5 E9EC144B
+       777DE62A AAB8A862 8AC376D2 82D6ED38 64E67982 428EBC83
+       1D14348F 6F2F9193 B5045AF2 767164E1 DFC967C1 FB3F2E55
+       A4BD1BFF E83B9C80 D052B985 D182EA0A DB2A3B73 13D3FE14
+       C8484B1E 052588B9 B7D2BBD2 DF016199 ECD06E15 57CD0915
+       B3353BBB 64E0EC37 7FD02837 0DF92B52 C7891428 CDC67EB6
+       184B523D 1DB246C3 2F630784 90F00EF8 D647D148 D4795451
+       5E2327CF EF98C582 664B4C0F 6CC41659
+
+   The generator generates a prime-order subgroup of size:
+
+   q = 8CF83642 A709A097 B4479976 40129DA2 99B1A47D 1EB3750B
+       A308B0FE 64F5FBD3
+*/
+static const char dh_ike_24_pem[] =
+"-----BEGIN DH PARAMETERS-----\n"
+"MIICCQKCAQEAh6jmHbS2Zjz/u9GcZRlZmYzu9ghmDdDyXSzu1ENeOwDgDfjx1hlX\n"
+"1Pr330VhsqowFsPZETQJb6o79Cltgw6afCCeDGSXUXq9WoqdMGvPZ+2R+eZyW0dY\n"
+"wCLgse9Cdb97bFv8EdRfkIi5QfVOseWbuLw5oL8SMH9cT9twxYGyP3a2Osrhyqa3\n"
+"kC1SUmc1SIoO8TxtmlG/pKs62DR3llJNjvahZ7WkGCXZZ+FE5RQFZCUcysuD5rSG\n"
+"9rPKP3lxUGAmwLhX9omWKFbe1AEKvQvmIcOjlgpU5xDDdfJjddcBQQOktUMwwZiv\n"
+"EmEW0iduEXFfaTh3+tfvCcrbCUrpHhoVlwKCAQA/syybcxNNCy53UGZg7b1ITKex\n"
+"jyHvIFQH9Hk6GguhJRDbwVB3vkY//0/tSqwLtVW+OmwbDGtHsbw3c79+jG9ikBIo\n"
+"+MKMuxilWuMTQQAKZQGW+THHelfy3fRj5ensFEt3feYqqrioYorDdtKC1u04ZOZ5\n"
+"gkKOvIMdFDSPby+Rk7UEWvJ2cWTh38lnwfs/LlWkvRv/6DucgNBSuYXRguoK2yo7\n"
+"cxPT/hTISEseBSWIubfSu9LfAWGZ7NBuFVfNCRWzNTu7ZODsN3/QKDcN+StSx4kU\n"
+"KM3GfrYYS1I9HbJGwy9jB4SQ8A741kfRSNR5VFFeIyfP75jFgmZLTA9sxBZZ\n"
+"-----END DH PARAMETERS-----\n";
+
+
+/* ========================================================================= */
+
+struct dh_constant {
+  const char *label;
+  const char *pem;
+};
+
+/* KEEP SORTED ALPHABETICALLY;
+ * duplicate PEM are okay, if we want aliases, but names must be alphabetical */
+static struct dh_constant dh_constants[] = {
+    { "default", dh_ike_23_pem },
+    { "ike1", dh_ike_1_pem },
+    { "ike14", dh_ike_14_pem },
+    { "ike15", dh_ike_15_pem },
+    { "ike16", dh_ike_16_pem },
+    { "ike17", dh_ike_17_pem },
+    { "ike18", dh_ike_18_pem },
+    { "ike2", dh_ike_2_pem },
+    { "ike22", dh_ike_22_pem },
+    { "ike23", dh_ike_23_pem },
+    { "ike24", dh_ike_24_pem },
+    { "ike5", dh_ike_5_pem },
+};
+static const int dh_constants_count =
+  sizeof(dh_constants) / sizeof(struct dh_constant);
+
+
+/* A policy decision; in absence of any other data, use a 2048 bit prime,
+ * pick the first one from the latest RFC providing such. */
+const char *
+std_dh_prime_default(void)
+{
+  return dh_ike_23_pem;
+}
+
+
+const char *
+std_dh_prime_named(const uschar *name)
+{
+  int first, last;
+  char *search_name = CS string_copylc(US name);
+
+  first = 0;
+  last = dh_constants_count;
+  while (last > first) {
+    int middle = (first + last)/2;
+    int c = strcmp(search_name, dh_constants[middle].label);
+    if (c == 0)
+      return dh_constants[middle].pem;
+    else if (c > 0)
+      first = middle + 1;
+    else
+      last = middle;
+  }
+  return NULL;
+}
+
+#endif /* SUPPORT_TLS */
+/* EOF */
diff --git a/src/store.c b/src/store.c
new file mode 100644 (file)
index 0000000..790f79d
--- /dev/null
@@ -0,0 +1,563 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2012 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Exim gets and frees all its store through these functions. In the original
+implementation there was a lot of mallocing and freeing of small bits of store.
+The philosophy has now changed to a scheme which includes the concept of
+"stacking pools" of store. For the short-lived processes, there isn't any real
+need to do any garbage collection, but the stack concept allows quick resetting
+in places where this seems sensible.
+
+Obviously the long-running processes (the daemon, the queue runner, and eximon)
+must take care not to eat store.
+
+The following different types of store are recognized:
+
+. Long-lived, large blocks: This is implemented by retaining the original
+  malloc/free functions, and it used for permanent working buffers and for
+  getting blocks to cut up for the other types.
+
+. Long-lived, small blocks: This is used for blocks that have to survive until
+  the process exits. It is implemented as a stacking pool (POOL_PERM). This is
+  functionally the same as store_malloc(), except that the store can't be
+  freed, but I expect it to be more efficient for handling small blocks.
+
+. Short-lived, short blocks: Most of the dynamic store falls into this
+  category. It is implemented as a stacking pool (POOL_MAIN) which is reset
+  after accepting a message when multiple messages are received by a single
+  process. Resetting happens at some other times as well, usually fairly
+  locally after some specific processing that needs working store.
+
+. There is a separate pool (POOL_SEARCH) that is used only for lookup storage.
+  This means it can be freed when search_tidyup() is called to close down all
+  the lookup caching.
+*/
+
+
+#include "exim.h"
+/* keep config.h before memcheck.h, for NVALGRIND */
+#include "config.h"
+
+#include "memcheck.h"
+
+
+/* We need to know how to align blocks of data for general use. I'm not sure
+how to get an alignment factor in general. In the current world, a value of 8
+is probably right, and this is sizeof(double) on some systems and sizeof(void
+*) on others, so take the larger of those. Since everything in this expression
+is a constant, the compiler should optimize it to a simple constant wherever it
+appears (I checked that gcc does do this). */
+
+#define alignment \
+  ((sizeof(void *) > sizeof(double))? sizeof(void *) : sizeof(double))
+
+/* Size of block to get from malloc to carve up into smaller ones. This
+must be a multiple of the alignment. We assume that 8192 is going to be
+suitably aligned. */
+
+#define STORE_BLOCK_SIZE 8192
+
+/* store_reset() will not free the following block if the last used block has
+less than this much left in it. */
+
+#define STOREPOOL_MIN_SIZE 256
+
+/* Structure describing the beginning of each big block. */
+
+typedef struct storeblock {
+  struct storeblock *next;
+  size_t length;
+} storeblock;
+
+/* Just in case we find ourselves on a system where the structure above has a
+length that is not a multiple of the alignment, set up a macro for the padded
+length. */
+
+#define ALIGNED_SIZEOF_STOREBLOCK \
+  (((sizeof(storeblock) + alignment - 1) / alignment) * alignment)
+
+/* Variables holding data for the local pools of store. The current pool number
+is held in store_pool, which is global so that it can be changed from outside.
+Setting the initial length values to -1 forces a malloc for the first call,
+even if the length is zero (which is used for getting a point to reset to). */
+
+int store_pool = POOL_PERM;
+
+static storeblock *chainbase[3] = { NULL, NULL, NULL };
+static storeblock *current_block[3] = { NULL, NULL, NULL };
+static void *next_yield[3] = { NULL, NULL, NULL };
+static int yield_length[3] = { -1, -1, -1 };
+
+/* pool_malloc holds the amount of memory used by the store pools; this goes up
+and down as store is reset or released. nonpool_malloc is the total got by
+malloc from other calls; this doesn't go down because it is just freed by
+pointer. */
+
+static int pool_malloc = 0;
+static int nonpool_malloc = 0;
+
+/* This variable is set by store_get() to its yield, and by store_reset() to
+NULL. This enables string_cat() to optimize its store handling for very long
+strings. That's why the variable is global. */
+
+void *store_last_get[3] = { NULL, NULL, NULL };
+
+
+
+/*************************************************
+*       Get a block from the current pool        *
+*************************************************/
+
+/* Running out of store is a total disaster. This function is called via the
+macro store_get(). It passes back a block of store within the current big
+block, getting a new one if necessary. The address is saved in
+store_last_was_get.
+
+Arguments:
+  size        amount wanted
+  filename    source file from which called
+  linenumber  line number in source file.
+
+Returns:      pointer to store (panic on malloc failure)
+*/
+
+void *
+store_get_3(int size, const char *filename, int linenumber)
+{
+/* Round up the size to a multiple of the alignment. Although this looks a
+messy statement, because "alignment" is a constant expression, the compiler can
+do a reasonable job of optimizing, especially if the value of "alignment" is a
+power of two. I checked this with -O2, and gcc did very well, compiling it to 4
+instructions on a Sparc (alignment = 8). */
+
+if (size % alignment != 0) size += alignment - (size % alignment);
+
+/* If there isn't room in the current block, get a new one. The minimum
+size is STORE_BLOCK_SIZE, and we would expect this to be the norm, since
+these functions are mostly called for small amounts of store. */
+
+if (size > yield_length[store_pool])
+  {
+  int length = (size <= STORE_BLOCK_SIZE)? STORE_BLOCK_SIZE : size;
+  int mlength = length + ALIGNED_SIZEOF_STOREBLOCK;
+  storeblock *newblock = NULL;
+
+  /* Sometimes store_reset() may leave a block for us; check if we can use it */
+
+  if (current_block[store_pool] != NULL &&
+      current_block[store_pool]->next != NULL)
+    {
+    newblock = current_block[store_pool]->next;
+    if (newblock->length < length)
+      {
+      /* Give up on this block, because it's too small */
+      store_free(newblock);
+      newblock = NULL;
+      }
+    }
+
+  /* If there was no free block, get a new one */
+
+  if (newblock == NULL)
+    {
+    pool_malloc += mlength;           /* Used in pools */
+    nonpool_malloc -= mlength;        /* Exclude from overall total */
+    newblock = store_malloc(mlength);
+    newblock->next = NULL;
+    newblock->length = length;
+    if (chainbase[store_pool] == NULL) chainbase[store_pool] = newblock;
+      else current_block[store_pool]->next = newblock;
+    }
+
+  current_block[store_pool] = newblock;
+  yield_length[store_pool] = newblock->length;
+  next_yield[store_pool] =
+    (void *)((char *)current_block[store_pool] + ALIGNED_SIZEOF_STOREBLOCK);
+  VALGRIND_MAKE_MEM_NOACCESS(next_yield[store_pool], yield_length[store_pool]);
+  }
+
+/* There's (now) enough room in the current block; the yield is the next
+pointer. */
+
+store_last_get[store_pool] = next_yield[store_pool];
+
+/* Cut out the debugging stuff for utilities, but stop picky compilers from
+giving warnings. */
+
+#ifdef COMPILE_UTILITY
+filename = filename;
+linenumber = linenumber;
+#else
+DEBUG(D_memory)
+  {
+  if (running_in_test_harness)
+    debug_printf("---%d Get %5d\n", store_pool, size);
+  else
+    debug_printf("---%d Get %6p %5d %-14s %4d\n", store_pool,
+      store_last_get[store_pool], size, filename, linenumber);
+  }
+#endif  /* COMPILE_UTILITY */
+
+VALGRIND_MAKE_MEM_UNDEFINED(store_last_get[store_pool], size);
+/* Update next pointer and number of bytes left in the current block. */
+
+next_yield[store_pool] = (void *)((char *)next_yield[store_pool] + size);
+yield_length[store_pool] -= size;
+
+return store_last_get[store_pool];
+}
+
+
+
+/*************************************************
+*       Get a block from the PERM pool           *
+*************************************************/
+
+/* This is just a convenience function, useful when just a single block is to
+be obtained.
+
+Arguments:
+  size        amount wanted
+  filename    source file from which called
+  linenumber  line number in source file.
+
+Returns:      pointer to store (panic on malloc failure)
+*/
+
+void *
+store_get_perm_3(int size, const char *filename, int linenumber)
+{
+void *yield;
+int old_pool = store_pool;
+store_pool = POOL_PERM;
+yield = store_get_3(size, filename, linenumber);
+store_pool = old_pool;
+return yield;
+}
+
+
+
+/*************************************************
+*      Extend a block if it is at the top        *
+*************************************************/
+
+/* While reading strings of unknown length, it is often the case that the
+string is being read into the block at the top of the stack. If it needs to be
+extended, it is more efficient just to extend the top block rather than
+allocate a new block and then have to copy the data. This function is provided
+for the use of string_cat(), but of course can be used elsewhere too.
+
+Arguments:
+  ptr        pointer to store block
+  oldsize    current size of the block, as requested by user
+  newsize    new size required
+  filename   source file from which called
+  linenumber line number in source file
+
+Returns:     TRUE if the block is at the top of the stack and has been
+             extended; FALSE if it isn't at the top of the stack, or cannot
+             be extended
+*/
+
+BOOL
+store_extend_3(void *ptr, int oldsize, int newsize, const char *filename,
+  int linenumber)
+{
+int inc = newsize - oldsize;
+int rounded_oldsize = oldsize;
+
+if (rounded_oldsize % alignment != 0)
+  rounded_oldsize += alignment - (rounded_oldsize % alignment);
+
+if ((char *)ptr + rounded_oldsize != (char *)(next_yield[store_pool]) ||
+    inc > yield_length[store_pool] + rounded_oldsize - oldsize)
+  return FALSE;
+
+/* Cut out the debugging stuff for utilities, but stop picky compilers from
+giving warnings. */
+
+#ifdef COMPILE_UTILITY
+filename = filename;
+linenumber = linenumber;
+#else
+DEBUG(D_memory)
+  {
+  if (running_in_test_harness)
+    debug_printf("---%d Ext %5d\n", store_pool, newsize);
+  else
+    debug_printf("---%d Ext %6p %5d %-14s %4d\n", store_pool, ptr, newsize,
+      filename, linenumber);
+  }
+#endif  /* COMPILE_UTILITY */
+
+if (newsize % alignment != 0) newsize += alignment - (newsize % alignment);
+next_yield[store_pool] = (char *)ptr + newsize;
+yield_length[store_pool] -= newsize - rounded_oldsize;
+VALGRIND_MAKE_MEM_UNDEFINED(ptr + oldsize, inc);
+return TRUE;
+}
+
+
+
+
+/*************************************************
+*    Back up to a previous point on the stack    *
+*************************************************/
+
+/* This function resets the next pointer, freeing any subsequent whole blocks
+that are now unused. Normally it is given a pointer that was the yield of a
+call to store_get, and is therefore aligned, but it may be given an offset
+after such a pointer in order to release the end of a block and anything that
+follows.
+
+Arguments:
+  ptr         place to back up to
+  filename    source file from which called
+  linenumber  line number in source file
+
+Returns:      nothing
+*/
+
+void
+store_reset_3(void *ptr, const char *filename, int linenumber)
+{
+storeblock *bb;
+storeblock *b = current_block[store_pool];
+char *bc = (char *)b + ALIGNED_SIZEOF_STOREBLOCK;
+int newlength;
+
+/* Last store operation was not a get */
+
+store_last_get[store_pool] = NULL;
+
+/* See if the place is in the current block - as it often will be. Otherwise,
+search for the block in which it lies. */
+
+if ((char *)ptr < bc || (char *)ptr > bc + b->length)
+  {
+  for (b = chainbase[store_pool]; b != NULL; b = b->next)
+    {
+    bc = (char *)b + ALIGNED_SIZEOF_STOREBLOCK;
+    if ((char *)ptr >= bc && (char *)ptr <= bc + b->length) break;
+    }
+  if (b == NULL)
+    log_write(0, LOG_MAIN|LOG_PANIC_DIE, "internal error: store_reset(%p) "
+      "failed: pool=%d %-14s %4d", ptr, store_pool, filename, linenumber);
+  }
+
+/* Back up, rounding to the alignment if necessary. When testing, flatten
+the released memory. */
+
+newlength = bc + b->length - (char *)ptr;
+#ifndef COMPILE_UTILITY
+if (running_in_test_harness) memset(ptr, 0xF0, newlength);
+#endif
+VALGRIND_MAKE_MEM_NOACCESS(ptr, newlength);
+yield_length[store_pool] = newlength - (newlength % alignment);
+next_yield[store_pool] = (char *)ptr + (newlength % alignment);
+current_block[store_pool] = b;
+
+/* Free any subsequent block. Do NOT free the first successor, if our
+current block has less than 256 bytes left. This should prevent us from
+flapping memory. However, keep this block only when it has the default size. */
+
+if (yield_length[store_pool] < STOREPOOL_MIN_SIZE &&
+    b->next != NULL &&
+    b->next->length == STORE_BLOCK_SIZE)
+  {
+  b = b->next;
+  VALGRIND_MAKE_MEM_NOACCESS((char *)b + ALIGNED_SIZEOF_STOREBLOCK, b->length - ALIGNED_SIZEOF_STOREBLOCK);
+  }
+
+bb = b->next;
+b->next = NULL;
+
+while (bb != NULL)
+  {
+  b = bb;
+  bb = bb->next;
+  pool_malloc -= b->length + ALIGNED_SIZEOF_STOREBLOCK;
+  store_free_3(b, filename, linenumber);
+  }
+
+/* Cut out the debugging stuff for utilities, but stop picky compilers from
+giving warnings. */
+
+#ifdef COMPILE_UTILITY
+filename = filename;
+linenumber = linenumber;
+#else
+DEBUG(D_memory)
+  {
+  if (running_in_test_harness)
+    debug_printf("---%d Rst    ** %d\n", store_pool, pool_malloc);
+  else
+    debug_printf("---%d Rst %6p    ** %-14s %4d %d\n", store_pool, ptr,
+      filename, linenumber, pool_malloc);
+  }
+#endif  /* COMPILE_UTILITY */
+}
+
+
+
+
+
+/************************************************
+*             Release store                     *
+************************************************/
+
+/* This function is specifically provided for use when reading very
+long strings, e.g. header lines. When the string gets longer than a
+complete block, it gets copied to a new block. It is helpful to free
+the old block iff the previous copy of the string is at its start,
+and therefore the only thing in it. Otherwise, for very long strings,
+dead store can pile up somewhat disastrously. This function checks that
+the pointer it is given is the first thing in a block, and if so,
+releases that block.
+
+Arguments:
+  block       block of store to consider
+  filename    source file from which called
+  linenumber  line number in source file
+
+Returns:      nothing
+*/
+
+void
+store_release_3(void *block, const char *filename, int linenumber)
+{
+storeblock *b;
+
+/* It will never be the first block, so no need to check that. */
+
+for (b = chainbase[store_pool]; b != NULL; b = b->next)
+  {
+  storeblock *bb = b->next;
+  if (bb != NULL && (char *)block == (char *)bb + ALIGNED_SIZEOF_STOREBLOCK)
+    {
+    b->next = bb->next;
+    pool_malloc -= bb->length + ALIGNED_SIZEOF_STOREBLOCK;
+
+    /* Cut out the debugging stuff for utilities, but stop picky compilers
+    from giving warnings. */
+
+    #ifdef COMPILE_UTILITY
+    filename = filename;
+    linenumber = linenumber;
+    #else
+    DEBUG(D_memory)
+      {
+      if (running_in_test_harness)
+        debug_printf("-Release       %d\n", pool_malloc);
+      else
+        debug_printf("-Release %6p %-20s %4d %d\n", (void *)bb, filename,
+          linenumber, pool_malloc);
+      }
+    if (running_in_test_harness)
+      memset(bb, 0xF0, bb->length+ALIGNED_SIZEOF_STOREBLOCK);
+    #endif  /* COMPILE_UTILITY */
+
+    free(bb);
+    return;
+    }
+  }
+}
+
+
+
+
+/*************************************************
+*                Malloc store                    *
+*************************************************/
+
+/* Running out of store is a total disaster for exim. Some malloc functions
+do not run happily on very small sizes, nor do they document this fact. This
+function is called via the macro store_malloc().
+
+Arguments:
+  size        amount of store wanted
+  filename    source file from which called
+  linenumber  line number in source file
+
+Returns:      pointer to gotten store (panic on failure)
+*/
+
+void *
+store_malloc_3(int size, const char *filename, int linenumber)
+{
+void *yield;
+
+if (size < 16) size = 16;
+yield = malloc((size_t)size);
+
+if (yield == NULL)
+  log_write(0, LOG_MAIN|LOG_PANIC_DIE, "failed to malloc %d bytes of memory: "
+    "called from line %d of %s", size, linenumber, filename);
+
+nonpool_malloc += size;
+
+/* Cut out the debugging stuff for utilities, but stop picky compilers from
+giving warnings. */
+
+#ifdef COMPILE_UTILITY
+filename = filename;
+linenumber = linenumber;
+#else
+
+/* If running in test harness, spend time making sure all the new store
+is not filled with zeros so as to catch problems. */
+
+if (running_in_test_harness)
+  {
+  memset(yield, 0xF0, (size_t)size);
+  DEBUG(D_memory) debug_printf("--Malloc %5d %d %d\n", size, pool_malloc,
+    nonpool_malloc);
+  }
+else
+  {
+  DEBUG(D_memory) debug_printf("--Malloc %6p %5d %-14s %4d %d %d\n", yield,
+    size, filename, linenumber, pool_malloc, nonpool_malloc);
+  }
+#endif  /* COMPILE_UTILITY */
+
+return yield;
+}
+
+
+/************************************************
+*             Free store                        *
+************************************************/
+
+/* This function is called by the macro store_free().
+
+Arguments:
+  block       block of store to free
+  filename    source file from which called
+  linenumber  line number in source file
+
+Returns:      nothing
+*/
+
+void
+store_free_3(void *block, const char *filename, int linenumber)
+{
+#ifdef COMPILE_UTILITY
+filename = filename;
+linenumber = linenumber;
+#else
+DEBUG(D_memory)
+  {
+  if (running_in_test_harness)
+    debug_printf("----Free\n");
+  else
+    debug_printf("----Free %6p %-20s %4d\n", block, filename, linenumber);
+  }
+#endif  /* COMPILE_UTILITY */
+free(block);
+}
+
+/* End of store.c */
diff --git a/src/store.h b/src/store.h
new file mode 100644 (file)
index 0000000..7c860f1
--- /dev/null
@@ -0,0 +1,53 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Header for Exim's memory allocation functions */
+
+#ifndef STORE_H
+#define STORE_H
+
+/* Define symbols for identifying the store pools. */
+
+enum { POOL_MAIN, POOL_PERM, POOL_SEARCH };
+
+/* This variable (the one for the current pool) is set by store_get() to its
+yield, and by store_reset() to NULL. This allows string_cat() to optimize its
+store handling. */
+
+extern void *store_last_get[3];
+
+/* This variable contains the current store pool number. */
+
+extern int store_pool;
+
+/* Macros for calling the memory allocation routines with
+tracing information for debugging. */
+
+#define store_extend(addr,old,new) \
+  store_extend_3(addr, old, new, __FILE__, __LINE__)
+
+#define store_free(addr)     store_free_3(addr, __FILE__, __LINE__)
+#define store_get(size)      store_get_3(size, __FILE__, __LINE__)
+#define store_get_perm(size) store_get_perm_3(size, __FILE__, __LINE__)
+#define store_malloc(size)   store_malloc_3(size, __FILE__, __LINE__)
+#define store_release(addr)  store_release_3(addr, __FILE__, __LINE__)
+#define store_reset(addr)    store_reset_3(addr, __FILE__, __LINE__)
+
+
+/* The real functions */
+
+extern BOOL    store_extend_3(void *, int, int, const char *, int);  /* The */
+extern void    store_free_3(void *, const char *, int);     /* value of the */
+extern void   *store_get_3(int, const char *, int);         /* 2nd arg is   */
+extern void   *store_get_perm_3(int, const char *, int);    /* __FILE__ in  */
+extern void   *store_malloc_3(int, const char *, int);      /* every call,  */
+extern void    store_release_3(void *, const char *, int);  /* so give its  */
+extern void    store_reset_3(void *, const char *, int);    /* correct type */
+
+#endif  /* STORE_H */
+
+/* End of store.h */
diff --git a/src/string.c b/src/string.c
new file mode 100644 (file)
index 0000000..0f657dc
--- /dev/null
@@ -0,0 +1,1783 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2014 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Miscellaneous string-handling functions. Some are not required for
+utilities and tests, and are cut out by the COMPILE_UTILITY macro. */
+
+
+#include "exim.h"
+
+
+#ifndef COMPILE_UTILITY
+/*************************************************
+*            Test for IP address                 *
+*************************************************/
+
+/* This used just to be a regular expression, but with IPv6 things are a bit
+more complicated. If the address contains a colon, it is assumed to be a v6
+address (assuming HAVE_IPV6 is set). If a mask is permitted and one is present,
+and maskptr is not NULL, its offset is placed there.
+
+Arguments:
+  s         a string
+  maskptr   NULL if no mask is permitted to follow
+            otherwise, points to an int where the offset of '/' is placed
+            if there is no / followed by trailing digits, *maskptr is set 0
+
+Returns:    0 if the string is not a textual representation of an IP address
+            4 if it is an IPv4 address
+            6 if it is an IPv6 address
+*/
+
+int
+string_is_ip_address(const uschar *s, int *maskptr)
+{
+int i;
+int yield = 4;
+
+/* If an optional mask is permitted, check for it. If found, pass back the
+offset. */
+
+if (maskptr != NULL)
+  {
+  const uschar *ss = s + Ustrlen(s);
+  *maskptr = 0;
+  if (s != ss && isdigit(*(--ss)))
+    {
+    while (ss > s && isdigit(ss[-1])) ss--;
+    if (ss > s && *(--ss) == '/') *maskptr = ss - s;
+    }
+  }
+
+/* A colon anywhere in the string => IPv6 address */
+
+if (Ustrchr(s, ':') != NULL)
+  {
+  BOOL had_double_colon = FALSE;
+  BOOL v4end = FALSE;
+  int count = 0;
+
+  yield = 6;
+
+  /* An IPv6 address must start with hex digit or double colon. A single
+  colon is invalid. */
+
+  if (*s == ':' && *(++s) != ':') return 0;
+
+  /* Now read up to 8 components consisting of up to 4 hex digits each. There
+  may be one and only one appearance of double colon, which implies any number
+  of binary zero bits. The number of preceding components is held in count. */
+
+  for (count = 0; count < 8; count++)
+    {
+    /* If the end of the string is reached before reading 8 components, the
+    address is valid provided a double colon has been read. This also applies
+    if we hit the / that introduces a mask or the % that introduces the
+    interface specifier (scope id) of a link-local address. */
+
+    if (*s == 0 || *s == '%' || *s == '/') return had_double_colon? yield : 0;
+
+    /* If a component starts with an additional colon, we have hit a double
+    colon. This is permitted to appear once only, and counts as at least
+    one component. The final component may be of this form. */
+
+    if (*s == ':')
+      {
+      if (had_double_colon) return 0;
+      had_double_colon = TRUE;
+      s++;
+      continue;
+      }
+
+    /* If the remainder of the string contains a dot but no colons, we
+    can expect a trailing IPv4 address. This is valid if either there has
+    been no double-colon and this is the 7th component (with the IPv4 address
+    being the 7th & 8th components), OR if there has been a double-colon
+    and fewer than 6 components. */
+
+    if (Ustrchr(s, ':') == NULL && Ustrchr(s, '.') != NULL)
+      {
+      if ((!had_double_colon && count != 6) ||
+          (had_double_colon && count > 6)) return 0;
+      v4end = TRUE;
+      yield = 6;
+      break;
+      }
+
+    /* Check for at least one and not more than 4 hex digits for this
+    component. */
+
+    if (!isxdigit(*s++)) return 0;
+    if (isxdigit(*s) && isxdigit(*(++s)) && isxdigit(*(++s))) s++;
+
+    /* If the component is terminated by colon and there is more to
+    follow, skip over the colon. If there is no more to follow the address is
+    invalid. */
+
+    if (*s == ':' && *(++s) == 0) return 0;
+    }
+
+  /* If about to handle a trailing IPv4 address, drop through. Otherwise
+  all is well if we are at the end of the string or at the mask or at a percent
+  sign, which introduces the interface specifier (scope id) of a link local
+  address. */
+
+  if (!v4end)
+    return (*s == 0 || *s == '%' ||
+           (*s == '/' && maskptr != NULL && *maskptr != 0))? yield : 0;
+  }
+
+/* Test for IPv4 address, which may be the tail-end of an IPv6 address. */
+
+for (i = 0; i < 4; i++)
+  {
+  if (i != 0 && *s++ != '.') return 0;
+  if (!isdigit(*s++)) return 0;
+  if (isdigit(*s) && isdigit(*(++s))) s++;
+  }
+
+return (*s == 0 || (*s == '/' && maskptr != NULL && *maskptr != 0))?
+  yield : 0;
+}
+#endif  /* COMPILE_UTILITY */
+
+
+/*************************************************
+*              Format message size               *
+*************************************************/
+
+/* Convert a message size in bytes to printing form, rounding
+according to the magnitude of the number. A value of zero causes
+a string of spaces to be returned.
+
+Arguments:
+  size        the message size in bytes
+  buffer      where to put the answer
+
+Returns:      pointer to the buffer
+              a string of exactly 5 characters is normally returned
+*/
+
+uschar *
+string_format_size(int size, uschar *buffer)
+{
+if (size == 0) Ustrcpy(CS buffer, "     ");
+else if (size < 1024) sprintf(CS buffer, "%5d", size);
+else if (size < 10*1024)
+  sprintf(CS buffer, "%4.1fK", (double)size / 1024.0);
+else if (size < 1024*1024)
+  sprintf(CS buffer, "%4dK", (size + 512)/1024);
+else if (size < 10*1024*1024)
+  sprintf(CS buffer, "%4.1fM", (double)size / (1024.0 * 1024.0));
+else
+  sprintf(CS buffer, "%4dM", (size + 512 * 1024)/(1024*1024));
+return buffer;
+}
+
+
+
+#ifndef COMPILE_UTILITY
+/*************************************************
+*       Convert a number to base 62 format       *
+*************************************************/
+
+/* Convert a long integer into an ASCII base 62 string. For Cygwin the value of
+BASE_62 is actually 36. Always return exactly 6 characters plus zero, in a
+static area.
+
+Argument: a long integer
+Returns:  pointer to base 62 string
+*/
+
+uschar *
+string_base62(unsigned long int value)
+{
+static uschar yield[7];
+uschar *p = yield + sizeof(yield) - 1;
+*p = 0;
+while (p > yield)
+  {
+  *(--p) = base62_chars[value % BASE_62];
+  value /= BASE_62;
+  }
+return yield;
+}
+#endif  /* COMPILE_UTILITY */
+
+
+
+/*************************************************
+*          Interpret escape sequence             *
+*************************************************/
+
+/* This function is called from several places where escape sequences are to be
+interpreted in strings.
+
+Arguments:
+  pp       points a pointer to the initiating "\" in the string;
+           the pointer gets updated to point to the final character
+Returns:   the value of the character escape
+*/
+
+int
+string_interpret_escape(uschar **pp)
+{
+#ifdef COMPILE_UTILITY
+const uschar *hex_digits= CUS"0123456789abcdef";
+#endif
+int ch;
+uschar *p = *pp;
+ch = *(++p);
+if (isdigit(ch) && ch != '8' && ch != '9')
+  {
+  ch -= '0';
+  if (isdigit(p[1]) && p[1] != '8' && p[1] != '9')
+    {
+    ch = ch * 8 + *(++p) - '0';
+    if (isdigit(p[1]) && p[1] != '8' && p[1] != '9')
+      ch = ch * 8 + *(++p) - '0';
+    }
+  }
+else switch(ch)
+  {
+  case 'b':  ch = '\b'; break;
+  case 'f':  ch = '\f'; break;
+  case 'n':  ch = '\n'; break;
+  case 'r':  ch = '\r'; break;
+  case 't':  ch = '\t'; break;
+  case 'v':  ch = '\v'; break;
+  case 'x':
+  ch = 0;
+  if (isxdigit(p[1]))
+    {
+    ch = ch * 16 +
+      Ustrchr(hex_digits, tolower(*(++p))) - hex_digits;
+    if (isxdigit(p[1])) ch = ch * 16 +
+      Ustrchr(hex_digits, tolower(*(++p))) - hex_digits;
+    }
+  break;
+  }
+*pp = p;
+return ch;
+}
+
+
+
+#ifndef COMPILE_UTILITY
+/*************************************************
+*          Ensure string is printable            *
+*************************************************/
+
+/* This function is called for critical strings. It checks for any
+non-printing characters, and if any are found, it makes a new copy
+of the string with suitable escape sequences. It is most often called by the
+macro string_printing(), which sets allow_tab TRUE.
+
+Arguments:
+  s             the input string
+  allow_tab     TRUE to allow tab as a printing character
+
+Returns:        string with non-printers encoded as printing sequences
+*/
+
+uschar *
+string_printing2(uschar *s, BOOL allow_tab)
+{
+int nonprintcount = 0;
+int length = 0;
+uschar *t = s;
+uschar *ss, *tt;
+
+while (*t != 0)
+  {
+  int c = *t++;
+  if (!mac_isprint(c) || (!allow_tab && c == '\t')) nonprintcount++;
+  length++;
+  }
+
+if (nonprintcount == 0) return s;
+
+/* Get a new block of store guaranteed big enough to hold the
+expanded string. */
+
+ss = store_get(length + nonprintcount * 3 + 1);
+
+/* Copy everying, escaping non printers. */
+
+t = s;
+tt = ss;
+
+while (*t != 0)
+  {
+  int c = *t;
+  if (mac_isprint(c) && (allow_tab || c != '\t')) *tt++ = *t++; else
+    {
+    *tt++ = '\\';
+    switch (*t)
+      {
+      case '\n': *tt++ = 'n'; break;
+      case '\r': *tt++ = 'r'; break;
+      case '\b': *tt++ = 'b'; break;
+      case '\v': *tt++ = 'v'; break;
+      case '\f': *tt++ = 'f'; break;
+      case '\t': *tt++ = 't'; break;
+      default: sprintf(CS tt, "%03o", *t); tt += 3; break;
+      }
+    t++;
+    }
+  }
+*tt = 0;
+return ss;
+}
+#endif  /* COMPILE_UTILITY */
+
+/*************************************************
+*        Undo printing escapes in string         *
+*************************************************/
+
+/* This function is the reverse of string_printing2.  It searches for
+backslash characters and if any are found, it makes a new copy of the
+string with escape sequences parsed.  Otherwise it returns the original
+string.
+
+Arguments:
+  s             the input string
+
+Returns:        string with printing escapes parsed back
+*/
+
+uschar *
+string_unprinting(uschar *s)
+{
+uschar *p, *q, *r, *ss;
+int len, off;
+
+p = Ustrchr(s, '\\');
+if (!p) return s;
+
+len = Ustrlen(s) + 1;
+ss = store_get(len);
+
+q = ss;
+off = p - s;
+if (off)
+  {
+  memcpy(q, s, off);
+  q += off;
+  }
+
+while (*p)
+  {
+  if (*p == '\\')
+    {
+    *q++ = string_interpret_escape(&p);
+    p++;
+    }
+  else
+    {
+    r = Ustrchr(p, '\\');
+    if (!r)
+      {
+      off = Ustrlen(p);
+      memcpy(q, p, off);
+      p += off;
+      q += off;
+      break;
+      }
+    else
+      {
+      off = r - p;
+      memcpy(q, p, off);
+      q += off;
+      p = r;
+      }
+    }
+  }
+*q = '\0';
+
+return ss;
+}
+
+
+
+
+/*************************************************
+*            Copy and save string                *
+*************************************************/
+
+/* This function assumes that memcpy() is faster than strcpy().
+
+Argument: string to copy
+Returns:  copy of string in new store
+*/
+
+uschar *
+string_copy(const uschar *s)
+{
+int len = Ustrlen(s) + 1;
+uschar *ss = store_get(len);
+memcpy(ss, s, len);
+return ss;
+}
+
+
+
+/*************************************************
+*     Copy and save string in malloc'd store     *
+*************************************************/
+
+/* This function assumes that memcpy() is faster than strcpy().
+
+Argument: string to copy
+Returns:  copy of string in new store
+*/
+
+uschar *
+string_copy_malloc(uschar *s)
+{
+int len = Ustrlen(s) + 1;
+uschar *ss = store_malloc(len);
+memcpy(ss, s, len);
+return ss;
+}
+
+
+
+/*************************************************
+*       Copy, lowercase and save string          *
+*************************************************/
+
+/*
+Argument: string to copy
+Returns:  copy of string in new store, with letters lowercased
+*/
+
+uschar *
+string_copylc(uschar *s)
+{
+uschar *ss = store_get(Ustrlen(s) + 1);
+uschar *p = ss;
+while (*s != 0) *p++ = tolower(*s++);
+*p = 0;
+return ss;
+}
+
+
+
+/*************************************************
+*       Copy and save string, given length       *
+*************************************************/
+
+/* It is assumed the data contains no zeros. A zero is added
+onto the end.
+
+Arguments:
+  s         string to copy
+  n         number of characters
+
+Returns:    copy of string in new store
+*/
+
+uschar *
+string_copyn(uschar *s, int n)
+{
+uschar *ss = store_get(n + 1);
+Ustrncpy(ss, s, n);
+ss[n] = 0;
+return ss;
+}
+
+
+/*************************************************
+* Copy, lowercase, and save string, given length *
+*************************************************/
+
+/* It is assumed the data contains no zeros. A zero is added
+onto the end.
+
+Arguments:
+  s         string to copy
+  n         number of characters
+
+Returns:    copy of string in new store, with letters lowercased
+*/
+
+uschar *
+string_copynlc(uschar *s, int n)
+{
+uschar *ss = store_get(n + 1);
+uschar *p = ss;
+while (n-- > 0) *p++ = tolower(*s++);
+*p = 0;
+return ss;
+}
+
+
+
+/*************************************************
+*    Copy string if long, inserting newlines     *
+*************************************************/
+
+/* If the given string is longer than 75 characters, it is copied, and within
+the copy, certain space characters are converted into newlines.
+
+Argument:  pointer to the string
+Returns:   pointer to the possibly altered string
+*/
+
+uschar *
+string_split_message(uschar *msg)
+{
+uschar *s, *ss;
+
+if (msg == NULL || Ustrlen(msg) <= 75) return msg;
+s = ss = msg = string_copy(msg);
+
+for (;;)
+  {
+  int i = 0;
+  while (i < 75 && *ss != 0 && *ss != '\n') ss++, i++;
+  if (*ss == 0) break;
+  if (*ss == '\n')
+    s = ++ss;
+  else
+    {
+    uschar *t = ss + 1;
+    uschar *tt = NULL;
+    while (--t > s + 35)
+      {
+      if (*t == ' ')
+        {
+        if (t[-1] == ':') { tt = t; break; }
+        if (tt == NULL) tt = t;
+        }
+      }
+
+    if (tt == NULL)          /* Can't split behind - try ahead */
+      {
+      t = ss + 1;
+      while (*t != 0)
+        {
+        if (*t == ' ' || *t == '\n')
+          { tt = t; break; }
+        t++;
+        }
+      }
+
+    if (tt == NULL) break;   /* Can't find anywhere to split */
+    *tt = '\n';
+    s = ss = tt+1;
+    }
+  }
+
+return msg;
+}
+
+
+
+/*************************************************
+*   Copy returned DNS domain name, de-escaping   *
+*************************************************/
+
+/* If a domain name contains top-bit characters, some resolvers return
+the fully qualified name with those characters turned into escapes. The
+convention is a backslash followed by _decimal_ digits. We convert these
+back into the original binary values. This will be relevant when
+allow_utf8_domains is set true and UTF-8 characters are used in domain
+names. Backslash can also be used to escape other characters, though we
+shouldn't come across them in domain names.
+
+Argument:   the domain name string
+Returns:    copy of string in new store, de-escaped
+*/
+
+uschar *
+string_copy_dnsdomain(uschar *s)
+{
+uschar *yield;
+uschar *ss = yield = store_get(Ustrlen(s) + 1);
+
+while (*s != 0)
+  {
+  if (*s != '\\')
+    {
+    *ss++ = *s++;
+    }
+  else if (isdigit(s[1]))
+    {
+    *ss++ = (s[1] - '0')*100 + (s[2] - '0')*10 + s[3] - '0';
+    s += 4;
+    }
+  else if (*(++s) != 0)
+    {
+    *ss++ = *s++;
+    }
+  }
+
+*ss = 0;
+return yield;
+}
+
+
+#ifndef COMPILE_UTILITY
+/*************************************************
+*     Copy space-terminated or quoted string     *
+*************************************************/
+
+/* This function copies from a string until its end, or until whitespace is
+encountered, unless the string begins with a double quote, in which case the
+terminating quote is sought, and escaping within the string is done. The length
+of a de-quoted string can be no longer than the original, since escaping always
+turns n characters into 1 character.
+
+Argument:  pointer to the pointer to the first character, which gets updated
+Returns:   the new string
+*/
+
+uschar *
+string_dequote(uschar **sptr)
+{
+uschar *s = *sptr;
+uschar *t, *yield;
+
+/* First find the end of the string */
+
+if (*s != '\"')
+  {
+  while (*s != 0 && !isspace(*s)) s++;
+  }
+else
+  {
+  s++;
+  while (*s != 0 && *s != '\"')
+    {
+    if (*s == '\\') (void)string_interpret_escape(&s);
+    s++;
+    }
+  if (*s != 0) s++;
+  }
+
+/* Get enough store to copy into */
+
+t = yield = store_get(s - *sptr + 1);
+s = *sptr;
+
+/* Do the copy */
+
+if (*s != '\"')
+  {
+  while (*s != 0 && !isspace(*s)) *t++ = *s++;
+  }
+else
+  {
+  s++;
+  while (*s != 0 && *s != '\"')
+    {
+    if (*s == '\\') *t++ = string_interpret_escape(&s);
+      else *t++ = *s;
+    s++;
+    }
+  if (*s != 0) s++;
+  }
+
+/* Update the pointer and return the terminated copy */
+
+*sptr = s;
+*t = 0;
+return yield;
+}
+#endif  /* COMPILE_UTILITY */
+
+
+
+/*************************************************
+*          Format a string and save it           *
+*************************************************/
+
+/* The formatting is done by string_format, which checks the length of
+everything.
+
+Arguments:
+  format    a printf() format - deliberately char * rather than uschar *
+              because it will most usually be a literal string
+  ...       arguments for format
+
+Returns:    pointer to fresh piece of store containing sprintf'ed string
+*/
+
+uschar *
+string_sprintf(const char *format, ...)
+{
+va_list ap;
+uschar buffer[STRING_SPRINTF_BUFFER_SIZE];
+va_start(ap, format);
+if (!string_vformat(buffer, sizeof(buffer), format, ap))
+  log_write(0, LOG_MAIN|LOG_PANIC_DIE,
+    "string_sprintf expansion was longer than " SIZE_T_FMT " (%s)",
+    sizeof(buffer), format);
+va_end(ap);
+return string_copy(buffer);
+}
+
+
+
+/*************************************************
+*         Case-independent strncmp() function    *
+*************************************************/
+
+/*
+Arguments:
+  s         first string
+  t         second string
+  n         number of characters to compare
+
+Returns:    < 0, = 0, or > 0, according to the comparison
+*/
+
+int
+strncmpic(const uschar *s, const uschar *t, int n)
+{
+while (n--)
+  {
+  int c = tolower(*s++) - tolower(*t++);
+  if (c) return c;
+  }
+return 0;
+}
+
+
+/*************************************************
+*         Case-independent strcmp() function     *
+*************************************************/
+
+/*
+Arguments:
+  s         first string
+  t         second string
+
+Returns:    < 0, = 0, or > 0, according to the comparison
+*/
+
+int
+strcmpic(const uschar *s, const uschar *t)
+{
+while (*s != 0)
+  {
+  int c = tolower(*s++) - tolower(*t++);
+  if (c != 0) return c;
+  }
+return *t;
+}
+
+
+/*************************************************
+*         Case-independent strstr() function     *
+*************************************************/
+
+/* The third argument specifies whether whitespace is required
+to follow the matched string.
+
+Arguments:
+  s              string to search
+  t              substring to search for
+  space_follows  if TRUE, match only if whitespace follows
+
+Returns:         pointer to substring in string, or NULL if not found
+*/
+
+uschar *
+strstric(uschar *s, uschar *t, BOOL space_follows)
+{
+uschar *p = t;
+uschar *yield = NULL;
+int cl = tolower(*p);
+int cu = toupper(*p);
+
+while (*s)
+  {
+  if (*s == cl || *s == cu)
+    {
+    if (yield == NULL) yield = s;
+    if (*(++p) == 0)
+      {
+      if (!space_follows || s[1] == ' ' || s[1] == '\n' ) return yield;
+      yield = NULL;
+      p = t;
+      }
+    cl = tolower(*p);
+    cu = toupper(*p);
+    s++;
+    }
+  else if (yield != NULL)
+    {
+    yield = NULL;
+    p = t;
+    cl = tolower(*p);
+    cu = toupper(*p);
+    }
+  else s++;
+  }
+return NULL;
+}
+
+
+
+#ifndef COMPILE_UTILITY
+/*************************************************
+*       Get next string from separated list      *
+*************************************************/
+
+/* Leading and trailing space is removed from each item. The separator in the
+list is controlled by the int pointed to by the separator argument as follows:
+
+  If the value is > 0 it is used as the separator. This is typically used for
+  sublists such as slash-separated options. The value is always a printing
+  character.
+
+    (If the value is actually > UCHAR_MAX there is only one item in the list.
+    This is used for some cases when called via functions that sometimes
+    plough through lists, and sometimes are given single items.)
+
+  If the value is <= 0, the string is inspected for a leading <x, where x is an
+  ispunct() or an iscntrl() character. If found, x is used as the separator. If
+  not found:
+
+      (a) if separator == 0, ':' is used
+      (b) if separator <0, -separator is used
+
+  In all cases the value of the separator that is used is written back to the
+  int so that it is used on subsequent calls as we progress through the list.
+
+A literal ispunct() separator can be represented in an item by doubling, but
+there is no way to include an iscntrl() separator as part of the data.
+
+Arguments:
+  listptr    points to a pointer to the current start of the list; the
+             pointer gets updated to point after the end of the next item
+  separator  a pointer to the separator character in an int (see above)
+  buffer     where to put a copy of the next string in the list; or
+               NULL if the next string is returned in new memory
+  buflen     when buffer is not NULL, the size of buffer; otherwise ignored
+
+Returns:     pointer to buffer, containing the next substring,
+             or NULL if no more substrings
+*/
+
+uschar *
+string_nextinlist(uschar **listptr, int *separator, uschar *buffer, int buflen)
+{
+register int sep = *separator;
+register uschar *s = *listptr;
+BOOL sep_is_special;
+
+if (s == NULL) return NULL;
+
+/* This allows for a fixed specified separator to be an iscntrl() character,
+but at the time of implementation, this is never the case. However, it's best
+to be conservative. */
+
+while (isspace(*s) && *s != sep) s++;
+
+/* A change of separator is permitted, so look for a leading '<' followed by an
+allowed character. */
+
+if (sep <= 0)
+  {
+  if (*s == '<' && (ispunct(s[1]) || iscntrl(s[1])))
+    {
+    sep = s[1];
+    s += 2;
+    while (isspace(*s) && *s != sep) s++;
+    }
+  else
+    {
+    sep = (sep == 0)? ':' : -sep;
+    }
+  *separator = sep;
+  }
+
+/* An empty string has no list elements */
+
+if (*s == 0) return NULL;
+
+/* Note whether whether or not the separator is an iscntrl() character. */
+
+sep_is_special = iscntrl(sep);
+
+/* Handle the case when a buffer is provided. */
+
+if (buffer != NULL)
+  {
+  register int p = 0;
+  for (; *s != 0; s++)
+    {
+    if (*s == sep && (*(++s) != sep || sep_is_special)) break;
+    if (p < buflen - 1) buffer[p++] = *s;
+    }
+  while (p > 0 && isspace(buffer[p-1])) p--;
+  buffer[p] = 0;
+  }
+
+/* Handle the case when a buffer is not provided. */
+
+else
+  {
+  int size = 0;
+  int ptr = 0;
+  uschar *ss;
+
+  /* We know that *s != 0 at this point. However, it might be pointing to a
+  separator, which could indicate an empty string, or (if an ispunct()
+  character) could be doubled to indicate a separator character as data at the
+  start of a string. Avoid getting working memory for an empty item. */
+
+  if (*s == sep)
+    {
+    s++;
+    if (*s != sep || sep_is_special)
+      {
+      *listptr = s;
+      return string_copy(US"");
+      }
+    }
+
+  /* Not an empty string; the first character is guaranteed to be a data
+  character. */
+
+  for (;;)
+    {
+    for (ss = s + 1; *ss != 0 && *ss != sep; ss++);
+    buffer = string_cat(buffer, &size, &ptr, s, ss-s);
+    s = ss;
+    if (*s == 0 || *(++s) != sep || sep_is_special) break;
+    }
+  while (ptr > 0 && isspace(buffer[ptr-1])) ptr--;
+  buffer[ptr] = 0;
+  }
+
+/* Update the current pointer and return the new string */
+
+*listptr = s;
+return buffer;
+}
+#endif  /* COMPILE_UTILITY */
+
+
+#ifndef COMPILE_UTILITY
+/************************************************
+*      Add element to seperated list           *
+************************************************/
+/* This function is used to build a list, returning
+an allocated null-terminated growable string. The
+given element has any embedded seperator characters
+doubled.
+
+Arguments:
+  list points to the start of the list that is being built, or NULL
+       if this is a new list that has no contents yet
+  sep  list seperator charactoer
+  ele  new lement to be appended to the list
+
+Returns:  pointer to the start of the list, changed if copied for expansion.
+*/
+
+uschar *
+string_append_listele(uschar * list, uschar sep, const uschar * ele)
+{
+uschar * new = NULL;
+int sz = 0, off = 0;
+uschar * sp;
+
+if (list)
+  {
+  new = string_cat(new, &sz, &off, list, Ustrlen(list));
+  new = string_cat(new, &sz, &off, &sep, 1);
+  }
+
+while((sp = Ustrchr(ele, sep)))
+  {
+  new = string_cat(new, &sz, &off, ele, sp-ele+1);
+  new = string_cat(new, &sz, &off, &sep, 1);
+  ele = sp+1;
+  }
+new = string_cat(new, &sz, &off, ele, Ustrlen(ele));
+new[off] = '\0';
+return new;
+}
+#endif  /* COMPILE_UTILITY */
+
+
+
+#ifndef COMPILE_UTILITY
+/*************************************************
+*             Add chars to string                *
+*************************************************/
+
+/* This function is used when building up strings of unknown length. Room is
+always left for a terminating zero to be added to the string that is being
+built. This function does not require the string that is being added to be NUL
+terminated, because the number of characters to add is given explicitly. It is
+sometimes called to extract parts of other strings.
+
+Arguments:
+  string   points to the start of the string that is being built, or NULL
+             if this is a new string that has no contents yet
+  size     points to a variable that holds the current capacity of the memory
+             block (updated if changed)
+  ptr      points to a variable that holds the offset at which to add
+             characters, updated to the new offset
+  s        points to characters to add
+  count    count of characters to add; must not exceed the length of s, if s
+             is a C string
+
+If string is given as NULL, *size and *ptr should both be zero.
+
+Returns:   pointer to the start of the string, changed if copied for expansion.
+           Note that a NUL is not added, though space is left for one. This is
+           because string_cat() is often called multiple times to build up a
+           string - there's no point adding the NUL till the end.
+*/
+
+uschar *
+string_cat(uschar *string, int *size, int *ptr, const uschar *s, int count)
+{
+int p = *ptr;
+
+if (p + count >= *size)
+  {
+  int oldsize = *size;
+
+  /* Mostly, string_cat() is used to build small strings of a few hundred
+  characters at most. There are times, however, when the strings are very much
+  longer (for example, a lookup that returns a vast number of alias addresses).
+  To try to keep things reasonable, we use increments whose size depends on the
+  existing length of the string. */
+
+  int inc = (oldsize < 4096)? 100 : 1024;
+  while (*size <= p + count) *size += inc;
+
+  /* New string */
+
+  if (string == NULL) string = store_get(*size);
+
+  /* Try to extend an existing allocation. If the result of calling
+  store_extend() is false, either there isn't room in the current memory block,
+  or this string is not the top item on the dynamic store stack. We then have
+  to get a new chunk of store and copy the old string. When building large
+  strings, it is helpful to call store_release() on the old string, to release
+  memory blocks that have become empty. (The block will be freed if the string
+  is at its start.) However, we can do this only if we know that the old string
+  was the last item on the dynamic memory stack. This is the case if it matches
+  store_last_get. */
+
+  else if (!store_extend(string, oldsize, *size))
+    {
+    BOOL release_ok = store_last_get[store_pool] == string;
+    uschar *newstring = store_get(*size);
+    memcpy(newstring, string, p);
+    if (release_ok) store_release(string);
+    string = newstring;
+    }
+  }
+
+/* Because we always specify the exact number of characters to copy, we can
+use memcpy(), which is likely to be more efficient than strncopy() because the
+latter has to check for zero bytes. */
+
+memcpy(string + p, s, count);
+*ptr = p + count;
+return string;
+}
+#endif  /* COMPILE_UTILITY */
+
+
+
+#ifndef COMPILE_UTILITY
+/*************************************************
+*        Append strings to another string        *
+*************************************************/
+
+/* This function can be used to build a string from many other strings.
+It calls string_cat() to do the dirty work.
+
+Arguments:
+  string   points to the start of the string that is being built, or NULL
+             if this is a new string that has no contents yet
+  size     points to a variable that holds the current capacity of the memory
+             block (updated if changed)
+  ptr      points to a variable that holds the offset at which to add
+             characters, updated to the new offset
+  count    the number of strings to append
+  ...      "count" uschar* arguments, which must be valid zero-terminated
+             C strings
+
+Returns:   pointer to the start of the string, changed if copied for expansion.
+           The string is not zero-terminated - see string_cat() above.
+*/
+
+uschar *
+string_append(uschar *string, int *size, int *ptr, int count, ...)
+{
+va_list ap;
+int i;
+
+va_start(ap, count);
+for (i = 0; i < count; i++)
+  {
+  uschar *t = va_arg(ap, uschar *);
+  string = string_cat(string, size, ptr, t, Ustrlen(t));
+  }
+va_end(ap);
+
+return string;
+}
+#endif
+
+
+
+/*************************************************
+*        Format a string with length checks      *
+*************************************************/
+
+/* This function is used to format a string with checking of the length of the
+output for all conversions. It protects Exim from absent-mindedness when
+calling functions like debug_printf and string_sprintf, and elsewhere. There
+are two different entry points to what is actually the same function, depending
+on whether the variable length list of data arguments are given explicitly or
+as a va_list item.
+
+The formats are the usual printf() ones, with some omissions (never used) and
+two additions for strings: %S forces lower case, and %#s or %#S prints nothing
+for a NULL string. Without the # "NULL" is printed (useful in debugging). There
+is also the addition of %D and %M, which insert the date in the form used for
+datestamped log files.
+
+Arguments:
+  buffer       a buffer in which to put the formatted string
+  buflen       the length of the buffer
+  format       the format string - deliberately char * and not uschar *
+  ... or ap    variable list of supplementary arguments
+
+Returns:       TRUE if the result fitted in the buffer
+*/
+
+BOOL
+string_format(uschar *buffer, int buflen, const char *format, ...)
+{
+BOOL yield;
+va_list ap;
+va_start(ap, format);
+yield = string_vformat(buffer, buflen, format, ap);
+va_end(ap);
+return yield;
+}
+
+
+BOOL
+string_vformat(uschar *buffer, int buflen, const char *format, va_list ap)
+{
+/* We assume numbered ascending order, C does not guarantee that */
+enum { L_NORMAL=1, L_SHORT=2, L_LONG=3, L_LONGLONG=4, L_LONGDOUBLE=5, L_SIZE=6 };
+
+BOOL yield = TRUE;
+int width, precision;
+const char *fp = format;       /* Deliberately not unsigned */
+uschar *p = buffer;
+uschar *last = buffer + buflen - 1;
+
+string_datestamp_offset = -1;  /* Datestamp not inserted */
+string_datestamp_length = 0;   /* Datestamp not inserted */
+string_datestamp_type = 0;     /* Datestamp not inserted */
+
+/* Scan the format and handle the insertions */
+
+while (*fp != 0)
+  {
+  int length = L_NORMAL;
+  int *nptr;
+  int slen;
+  const char *null = "NULL";   /* ) These variables */
+  const char *item_start, *s;  /* ) are deliberately */
+  char newformat[16];          /* ) not unsigned */
+
+  /* Non-% characters just get copied verbatim */
+
+  if (*fp != '%')
+    {
+    if (p >= last) { yield = FALSE; break; }
+    *p++ = (uschar)*fp++;
+    continue;
+    }
+
+  /* Deal with % characters. Pick off the width and precision, for checking
+  strings, skipping over the flag and modifier characters. */
+
+  item_start = fp;
+  width = precision = -1;
+
+  if (strchr("-+ #0", *(++fp)) != NULL)
+    {
+    if (*fp == '#') null = "";
+    fp++;
+    }
+
+  if (isdigit((uschar)*fp))
+    {
+    width = *fp++ - '0';
+    while (isdigit((uschar)*fp)) width = width * 10 + *fp++ - '0';
+    }
+  else if (*fp == '*')
+    {
+    width = va_arg(ap, int);
+    fp++;
+    }
+
+  if (*fp == '.')
+    {
+    if (*(++fp) == '*')
+      {
+      precision = va_arg(ap, int);
+      fp++;
+      }
+    else
+      {
+      precision = 0;
+      while (isdigit((uschar)*fp))
+        precision = precision*10 + *fp++ - '0';
+      }
+    }
+
+  /* Skip over 'h', 'L', 'l', 'll' and 'z', remembering the item length */
+
+  if (*fp == 'h')
+    { fp++; length = L_SHORT; }
+  else if (*fp == 'L')
+    { fp++; length = L_LONGDOUBLE; }
+  else if (*fp == 'l')
+    {
+    if (fp[1] == 'l')
+      {
+      fp += 2;
+      length = L_LONGLONG;
+      }
+    else
+      {
+      fp++;
+      length = L_LONG;
+      }
+    }
+  else if (*fp == 'z')
+    { fp++; length = L_SIZE; }
+
+  /* Handle each specific format type. */
+
+  switch (*fp++)
+    {
+    case 'n':
+    nptr = va_arg(ap, int *);
+    *nptr = p - buffer;
+    break;
+
+    case 'd':
+    case 'o':
+    case 'u':
+    case 'x':
+    case 'X':
+    if (p >= last - ((length > L_LONG)? 24 : 12))
+      { yield = FALSE; goto END_FORMAT; }
+    strncpy(newformat, item_start, fp - item_start);
+    newformat[fp - item_start] = 0;
+
+    /* Short int is promoted to int when passing through ..., so we must use
+    int for va_arg(). */
+
+    switch(length)
+      {
+      case L_SHORT:
+      case L_NORMAL:   sprintf(CS p, newformat, va_arg(ap, int)); break;
+      case L_LONG:     sprintf(CS p, newformat, va_arg(ap, long int)); break;
+      case L_LONGLONG: sprintf(CS p, newformat, va_arg(ap, LONGLONG_T)); break;
+      case L_SIZE:     sprintf(CS p, newformat, va_arg(ap, size_t)); break;
+      }
+    while (*p) p++;
+    break;
+
+    case 'p':
+    if (p >= last - 24) { yield = FALSE; goto END_FORMAT; }
+    strncpy(newformat, item_start, fp - item_start);
+    newformat[fp - item_start] = 0;
+    sprintf(CS p, newformat, va_arg(ap, void *));
+    while (*p) p++;
+    break;
+
+    /* %f format is inherently insecure if the numbers that it may be
+    handed are unknown (e.g. 1e300). However, in Exim, %f is used for
+    printing load averages, and these are actually stored as integers
+    (load average * 1000) so the size of the numbers is constrained.
+    It is also used for formatting sending rates, where the simplicity
+    of the format prevents overflow. */
+
+    case 'f':
+    case 'e':
+    case 'E':
+    case 'g':
+    case 'G':
+    if (precision < 0) precision = 6;
+    if (p >= last - precision - 8) { yield = FALSE; goto END_FORMAT; }
+    strncpy(newformat, item_start, fp - item_start);
+    newformat[fp-item_start] = 0;
+    if (length == L_LONGDOUBLE)
+      sprintf(CS p, newformat, va_arg(ap, long double));
+    else
+      sprintf(CS p, newformat, va_arg(ap, double));
+    while (*p) p++;
+    break;
+
+    /* String types */
+
+    case '%':
+    if (p >= last) { yield = FALSE; goto END_FORMAT; }
+    *p++ = '%';
+    break;
+
+    case 'c':
+    if (p >= last) { yield = FALSE; goto END_FORMAT; }
+    *p++ = va_arg(ap, int);
+    break;
+
+    case 'D':                   /* Insert daily datestamp for log file names */
+    s = CS tod_stamp(tod_log_datestamp_daily);
+    string_datestamp_offset = p - buffer;   /* Passed back via global */
+    string_datestamp_length = Ustrlen(s);   /* Passed back via global */
+    string_datestamp_type = tod_log_datestamp_daily;
+    slen = string_datestamp_length;
+    goto INSERT_STRING;
+
+    case 'M':                   /* Insert monthly datestamp for log file names */
+    s = CS tod_stamp(tod_log_datestamp_monthly);
+    string_datestamp_offset = p - buffer;   /* Passed back via global */
+    string_datestamp_length = Ustrlen(s);   /* Passed back via global */
+    string_datestamp_type = tod_log_datestamp_monthly;
+    slen = string_datestamp_length;
+    goto INSERT_STRING;
+
+    case 's':
+    case 'S':                   /* Forces *lower* case */
+    s = va_arg(ap, char *);
+
+    if (s == NULL) s = null;
+    slen = Ustrlen(s);
+
+    INSERT_STRING:              /* Come to from %D or %M above */
+
+    /* If the width is specified, check that there is a precision
+    set; if not, set it to the width to prevent overruns of long
+    strings. */
+
+    if (width >= 0)
+      {
+      if (precision < 0) precision = width;
+      }
+
+    /* If a width is not specified and the precision is specified, set
+    the width to the precision, or the string length if shorted. */
+
+    else if (precision >= 0)
+      {
+      width = (precision < slen)? precision : slen;
+      }
+
+    /* If neither are specified, set them both to the string length. */
+
+    else width = precision = slen;
+
+    /* Check string space, and add the string to the buffer if ok. If
+    not OK, add part of the string (debugging uses this to show as
+    much as possible). */
+
+    if (p == last)
+      {
+      yield = FALSE;
+      goto END_FORMAT;
+      }
+    if (p >= last - width)
+      {
+      yield = FALSE;
+      width = precision = last - p - 1;
+      if (width < 0) width = 0;
+      if (precision < 0) precision = 0;
+      }
+    sprintf(CS p, "%*.*s", width, precision, s);
+    if (fp[-1] == 'S')
+      while (*p) { *p = tolower(*p); p++; }
+    else
+      while (*p) p++;
+    if (!yield) goto END_FORMAT;
+    break;
+
+    /* Some things are never used in Exim; also catches junk. */
+
+    default:
+    strncpy(newformat, item_start, fp - item_start);
+    newformat[fp-item_start] = 0;
+    log_write(0, LOG_MAIN|LOG_PANIC_DIE, "string_format: unsupported type "
+      "in \"%s\" in \"%s\"", newformat, format);
+    break;
+    }
+  }
+
+/* Ensure string is complete; return TRUE if got to the end of the format */
+
+END_FORMAT:
+
+*p = 0;
+return yield;
+}
+
+
+
+#ifndef COMPILE_UTILITY
+/*************************************************
+*       Generate an "open failed" message        *
+*************************************************/
+
+/* This function creates a message after failure to open a file. It includes a
+string supplied as data, adds the strerror() text, and if the failure was
+"Permission denied", reads and includes the euid and egid.
+
+Arguments:
+  eno           the value of errno after the failure
+  format        a text format string - deliberately not uschar *
+  ...           arguments for the format string
+
+Returns:        a message, in dynamic store
+*/
+
+uschar *
+string_open_failed(int eno, const char *format, ...)
+{
+va_list ap;
+uschar buffer[1024];
+
+Ustrcpy(buffer, "failed to open ");
+va_start(ap, format);
+
+/* Use the checked formatting routine to ensure that the buffer
+does not overflow. It should not, since this is called only for internally
+specified messages. If it does, the message just gets truncated, and there
+doesn't seem much we can do about that. */
+
+(void)string_vformat(buffer+15, sizeof(buffer) - 15, format, ap);
+
+return (eno == EACCES)?
+  string_sprintf("%s: %s (euid=%ld egid=%ld)", buffer, strerror(eno),
+    (long int)geteuid(), (long int)getegid()) :
+  string_sprintf("%s: %s", buffer, strerror(eno));
+}
+#endif  /* COMPILE_UTILITY */
+
+
+
+#ifndef COMPILE_UTILITY
+/*************************************************
+*        Generate local prt for logging          *
+*************************************************/
+
+/* This function is a subroutine for use in string_log_address() below.
+
+Arguments:
+  addr        the address being logged
+  yield       the current dynamic buffer pointer
+  sizeptr     points to current size
+  ptrptr      points to current insert pointer
+
+Returns:      the new value of the buffer pointer
+*/
+
+static uschar *
+string_get_localpart(address_item *addr, uschar *yield, int *sizeptr,
+  int *ptrptr)
+{
+if (testflag(addr, af_include_affixes) && addr->prefix != NULL)
+  yield = string_cat(yield, sizeptr, ptrptr, addr->prefix,
+    Ustrlen(addr->prefix));
+yield = string_cat(yield, sizeptr, ptrptr, addr->local_part,
+  Ustrlen(addr->local_part));
+if (testflag(addr, af_include_affixes) && addr->suffix != NULL)
+  yield = string_cat(yield, sizeptr, ptrptr, addr->suffix,
+    Ustrlen(addr->suffix));
+return yield;
+}
+
+
+/*************************************************
+*          Generate log address list             *
+*************************************************/
+
+/* This function generates a list consisting of an address and its parents, for
+use in logging lines. For saved onetime aliased addresses, the onetime parent
+field is used. If the address was delivered by a transport with rcpt_include_
+affixes set, the af_include_affixes bit will be set in the address. In that
+case, we include the affixes here too.
+
+Arguments:
+  addr          bottom (ultimate) address
+  all_parents   if TRUE, include all parents
+  success       TRUE for successful delivery
+
+Returns:        a string in dynamic store
+*/
+
+uschar *
+string_log_address(address_item *addr, BOOL all_parents, BOOL success)
+{
+int size = 64;
+int ptr = 0;
+BOOL add_topaddr = TRUE;
+uschar *yield = store_get(size);
+address_item *topaddr;
+
+/* Find the ultimate parent */
+
+for (topaddr = addr; topaddr->parent != NULL; topaddr = topaddr->parent);
+
+/* We start with just the local part for pipe, file, and reply deliveries, and
+for successful local deliveries from routers that have the log_as_local flag
+set. File deliveries from filters can be specified as non-absolute paths in
+cases where the transport is goin to complete the path. If there is an error
+before this happens (expansion failure) the local part will not be updated, and
+so won't necessarily look like a path. Add extra text for this case. */
+
+if (testflag(addr, af_pfr) ||
+      (success &&
+       addr->router != NULL && addr->router->log_as_local &&
+       addr->transport != NULL && addr->transport->info->local))
+  {
+  if (testflag(addr, af_file) && addr->local_part[0] != '/')
+    yield = string_cat(yield, &size, &ptr, CUS"save ", 5);
+  yield = string_get_localpart(addr, yield, &size, &ptr);
+  }
+
+/* Other deliveries start with the full address. It we have split it into local
+part and domain, use those fields. Some early failures can happen before the
+splitting is done; in those cases use the original field. */
+
+else
+  {
+  if (addr->local_part != NULL)
+    {
+    yield = string_get_localpart(addr, yield, &size, &ptr);
+    yield = string_cat(yield, &size, &ptr, US"@", 1);
+    yield = string_cat(yield, &size, &ptr, addr->domain,
+      Ustrlen(addr->domain) );
+    }
+  else
+    {
+    yield = string_cat(yield, &size, &ptr, addr->address, Ustrlen(addr->address));
+    }
+  yield[ptr] = 0;
+
+  /* If the address we are going to print is the same as the top address,
+  and all parents are not being included, don't add on the top address. First
+  of all, do a caseless comparison; if this succeeds, do a caseful comparison
+  on the local parts. */
+
+  if (strcmpic(yield, topaddr->address) == 0 &&
+      Ustrncmp(yield, topaddr->address, Ustrchr(yield, '@') - yield) == 0 &&
+      addr->onetime_parent == NULL &&
+      (!all_parents || addr->parent == NULL || addr->parent == topaddr))
+    add_topaddr = FALSE;
+  }
+
+/* If all parents are requested, or this is a local pipe/file/reply, and
+there is at least one intermediate parent, show it in brackets, and continue
+with all of them if all are wanted. */
+
+if ((all_parents || testflag(addr, af_pfr)) &&
+    addr->parent != NULL &&
+    addr->parent != topaddr)
+  {
+  uschar *s = US" (";
+  address_item *addr2;
+  for (addr2 = addr->parent; addr2 != topaddr; addr2 = addr2->parent)
+    {
+    yield = string_cat(yield, &size, &ptr, s, 2);
+    yield = string_cat(yield, &size, &ptr, addr2->address, Ustrlen(addr2->address));
+    if (!all_parents) break;
+    s = US", ";
+    }
+  yield = string_cat(yield, &size, &ptr, US")", 1);
+  }
+
+/* Add the top address if it is required */
+
+if (add_topaddr)
+  {
+  yield = string_cat(yield, &size, &ptr, US" <", 2);
+
+  if (addr->onetime_parent == NULL)
+    yield = string_cat(yield, &size, &ptr, topaddr->address,
+      Ustrlen(topaddr->address));
+  else
+    yield = string_cat(yield, &size, &ptr, addr->onetime_parent,
+      Ustrlen(addr->onetime_parent));
+
+  yield = string_cat(yield, &size, &ptr, US">", 1);
+  }
+
+yield[ptr] = 0;  /* string_cat() leaves space */
+return yield;
+}
+#endif  /* COMPILE_UTILITY */
+
+
+
+
+
+/*************************************************
+**************************************************
+*             Stand-alone test program           *
+**************************************************
+*************************************************/
+
+#ifdef STAND_ALONE
+int main(void)
+{
+uschar buffer[256];
+
+printf("Testing is_ip_address\n");
+
+while (fgets(CS buffer, sizeof(buffer), stdin) != NULL)
+  {
+  int offset;
+  buffer[Ustrlen(buffer) - 1] = 0;
+  printf("%d\n", string_is_ip_address(buffer, NULL));
+  printf("%d %d %s\n", string_is_ip_address(buffer, &offset), offset, buffer);
+  }
+
+printf("Testing string_nextinlist\n");
+
+while (fgets(CS buffer, sizeof(buffer), stdin) != NULL)
+  {
+  uschar *list = buffer;
+  uschar *lp1, *lp2;
+  uschar item[256];
+  int sep1 = 0;
+  int sep2 = 0;
+
+  if (*list == '<')
+    {
+    sep1 = sep2 = list[1];
+    list += 2;
+    }
+
+  lp1 = lp2 = list;
+  for (;;)
+    {
+    uschar *item1 = string_nextinlist(&lp1, &sep1, item, sizeof(item));
+    uschar *item2 = string_nextinlist(&lp2, &sep2, NULL, 0);
+
+    if (item1 == NULL && item2 == NULL) break;
+    if (item == NULL || item2 == NULL || Ustrcmp(item1, item2) != 0)
+      {
+      printf("***ERROR\nitem1=\"%s\"\nitem2=\"%s\"\n",
+        (item1 == NULL)? "NULL" : CS item1,
+        (item2 == NULL)? "NULL" : CS item2);
+      break;
+      }
+    else printf("  \"%s\"\n", CS item1);
+    }
+  }
+
+/* This is a horrible lash-up, but it serves its purpose. */
+
+printf("Testing string_format\n");
+
+while (fgets(CS buffer, sizeof(buffer), stdin) != NULL)
+  {
+  void *args[3];
+  long long llargs[3];
+  double dargs[3];
+  int dflag = 0;
+  int llflag = 0;
+  int n = 0;
+  int count;
+  int countset = 0;
+  uschar format[256];
+  uschar outbuf[256];
+  uschar *s;
+  buffer[Ustrlen(buffer) - 1] = 0;
+
+  s = Ustrchr(buffer, ',');
+  if (s == NULL) s = buffer + Ustrlen(buffer);
+
+  Ustrncpy(format, buffer, s - buffer);
+  format[s-buffer] = 0;
+
+  if (*s == ',') s++;
+
+  while (*s != 0)
+    {
+    uschar *ss = s;
+    s = Ustrchr(ss, ',');
+    if (s == NULL) s = ss + Ustrlen(ss);
+
+    if (isdigit(*ss))
+      {
+      Ustrncpy(outbuf, ss, s-ss);
+      if (Ustrchr(outbuf, '.') != NULL)
+        {
+        dflag = 1;
+        dargs[n++] = Ustrtod(outbuf, NULL);
+        }
+      else if (Ustrstr(outbuf, "ll") != NULL)
+        {
+        llflag = 1;
+        llargs[n++] = strtoull(CS outbuf, NULL, 10);
+        }
+      else
+        {
+        args[n++] = (void *)Uatoi(outbuf);
+        }
+      }
+
+    else if (Ustrcmp(ss, "*") == 0)
+      {
+      args[n++] = (void *)(&count);
+      countset = 1;
+      }
+
+    else
+      {
+      uschar *sss = malloc(s - ss + 1);
+      Ustrncpy(sss, ss, s-ss);
+      args[n++] = sss;
+      }
+
+    if (*s == ',') s++;
+    }
+
+  if (!dflag && !llflag)
+    printf("%s\n", string_format(outbuf, sizeof(outbuf), CS format,
+      args[0], args[1], args[2])? "True" : "False");
+
+  else if (dflag)
+    printf("%s\n", string_format(outbuf, sizeof(outbuf), CS format,
+      dargs[0], dargs[1], dargs[2])? "True" : "False");
+
+  else printf("%s\n", string_format(outbuf, sizeof(outbuf), CS format,
+    llargs[0], llargs[1], llargs[2])? "True" : "False");
+
+  printf("%s\n", CS outbuf);
+  if (countset) printf("count=%d\n", count);
+  }
+
+return 0;
+}
+#endif
+
+/* End of string.c */
diff --git a/src/structs.h b/src/structs.h
new file mode 100644 (file)
index 0000000..71ac5d8
--- /dev/null
@@ -0,0 +1,794 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2014 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+
+/* Definitions of various structures. In addition, those that are visible for
+the compilation of local_scan() are defined in local_scan.h. These are
+
+  header_line
+  optionlist
+  recipient_item
+
+For those declared here, we have to pre-declare some because of mutually
+recursive definitions in the auths, routers, and transports blocks. */
+
+struct address_item;
+struct auth_info;
+struct driver_info;
+struct director_info;
+struct smtp_inblock;
+struct smtp_outblock;
+struct transport_info;
+struct router_info;
+
+/* Structure for remembering macros for the configuration file */
+
+typedef struct macro_item {
+  struct  macro_item *next;
+  BOOL    command_line;
+  uschar *replacement;
+  uschar  name[1];
+} macro_item;
+
+/* Structure for bit tables for debugging and logging */
+
+typedef struct bit_table {
+  uschar *name;
+  unsigned int bit;
+} bit_table;
+
+/* Block for holding a uid and gid, possibly unset, and an initgroups flag. */
+
+typedef struct ugid_block {
+  uid_t   uid;
+  gid_t   gid;
+  BOOL    uid_set;
+  BOOL    gid_set;
+  BOOL    initgroups;
+} ugid_block;
+
+/* Structure for holding information about a host for use mainly by routers,
+but also used when checking lists of hosts and when transporting. Looking up
+host addresses is done using this structure. */
+
+typedef enum {DS_UNK=-1, DS_NO, DS_YES} dnssec_status_t;
+
+typedef struct host_item {
+  struct host_item *next;
+  uschar *name;                   /* Host name */
+  uschar *address;                /* IP address in text form */
+  int     port;                   /* port value in host order (if SRV lookup) */
+  int     mx;                     /* MX value if found via MX records */
+  int     sort_key;               /* MX*1000 plus random "fraction" */
+  int     status;                 /* Usable, unusable, or unknown */
+  int     why;                    /* Why host is unusable */
+  int     last_try;               /* Time of last try if known */
+  dnssec_status_t dnssec;
+} host_item;
+
+/* Chain of rewrite rules, read from the rewrite config, or parsed from the
+rewrite_headers field of a transport. */
+
+typedef struct rewrite_rule {
+  struct rewrite_rule *next;
+  int     flags;
+  uschar *key;
+  uschar *replacement;
+} rewrite_rule;
+
+/* This structure is used to pass back configuration data from the smtp
+transport to the outside world. It is used during callback processing. If ever
+another remote transport were implemented, it could use the same structure. */
+
+typedef struct transport_feedback {
+  uschar *interface;
+  uschar *port;
+  uschar *protocol;
+  uschar *hosts;
+  uschar *helo_data;
+  BOOL   hosts_override;
+  BOOL   hosts_randomize;
+  BOOL   gethostbyname;
+  BOOL   qualify_single;
+  BOOL   search_parents;
+} transport_feedback;
+
+/* Routers, transports, and authenticators have similar data blocks. Each
+driver that is compiled into the code is represented by a xxx_info block; the
+active drivers are represented by a chain of xxx_instance blocks. To make it
+possible to use the same code for reading the configuration files for all
+three, the layout of the start of the blocks is kept the same, and represented
+by the generic structures driver_info and driver_instance. */
+
+typedef struct driver_instance {
+  struct driver_instance *next;
+  uschar *name;                   /* Instance name */
+  struct driver_info *info;       /* Points to info for this driver */
+  void   *options_block;          /* Pointer to private options */
+  uschar *driver_name;            /* All start with this generic option */
+} driver_instance;
+
+typedef struct driver_info {
+  uschar *driver_name;            /* Name of driver */
+  optionlist *options;            /* Table of private options names */
+  int    *options_count;          /* -> Number of entries in table */
+  void   *options_block;          /* Points to default private block */
+  int     options_len;            /* Length of same in bytes */
+  void  (*init)(                  /* Initialization entry point */
+    struct driver_instance *);
+} driver_info;
+
+
+/* Structure for holding information about the configured transports. Some
+of the generally accessible options are set from the configuration file; others
+are set by transport initialization, since they can only be set for certain
+transports. They need to be generally accessible, however, as they are used by
+the main transport code. */
+
+typedef struct transport_instance {
+  struct transport_instance *next;
+  uschar *name;                   /* Instance name */
+  struct transport_info *info;    /* Info for this driver */
+  void *options_block;            /* Pointer to private options */
+  uschar *driver_name;            /* Must be first */
+  int   (*setup)(                 /* Setup entry point */
+    struct transport_instance *,
+    struct address_item *,
+    struct transport_feedback *,  /* For passing back config data */
+    uid_t,                        /* The uid that will be used */
+    gid_t,                        /* The gid that will be used */
+    uschar **);                   /* For an error message */
+                                  /**************************************/
+  int     batch_max;              /* )                                  */
+  uschar *batch_id;               /* )                                  */
+  uschar *home_dir;               /* ) Used only for local transports   */
+  uschar *current_dir;            /* )                                  */
+                                  /**************************************/
+  BOOL    multi_domain;           /* )                                  */
+  BOOL    overrides_hosts;        /* ) Used only for remote transports  */
+  int     max_addresses;          /* )                                  */
+  int     connection_max_messages;/* )                                  */
+                                  /**************************************/
+  BOOL    deliver_as_creator;     /* Used only by pipe at present */
+  BOOL    disable_logging;        /* For very weird requirements */
+  BOOL    initgroups;             /* Initialize groups when setting uid */
+  BOOL    uid_set;                /* uid is set */
+  BOOL    gid_set;                /* gid is set */
+  uid_t   uid;
+  gid_t   gid;
+  uschar *expand_uid;             /* Variable uid */
+  uschar *expand_gid;             /* Variable gid */
+  uschar *warn_message;           /* Used only by appendfile at present */
+  uschar *shadow;                 /* Name of shadow transport */
+  uschar *shadow_condition;       /* Condition for running it */
+  uschar *filter_command;         /* For on-the-fly-filtering */
+  uschar *add_headers;            /* Add these headers */
+  uschar *remove_headers;         /* Remove these headers */
+  uschar *return_path;            /* Overriding (rewriting) return path */
+  uschar *debug_string;           /* Debugging output */
+  uschar *message_size_limit;     /* Biggest message this transport handles */
+  uschar *headers_rewrite;        /* Rules for rewriting headers */
+  rewrite_rule *rewrite_rules;    /* Parsed rewriting rules */
+  int     rewrite_existflags;     /* Bits showing which headers are rewritten */
+  int     filter_timeout;         /* For transport filter timing */
+  BOOL    body_only;              /* Deliver only the body */
+  BOOL    delivery_date_add;      /* Add Delivery-Date header */
+  BOOL    envelope_to_add;        /* Add Envelope-To header */
+  BOOL    headers_only;           /* Deliver only the headers */
+  BOOL    rcpt_include_affixes;   /* TRUE to retain affixes in RCPT commands */
+  BOOL    return_path_add;        /* Add Return-Path header */
+  BOOL    return_output;          /* TRUE if output should always be returned */
+  BOOL    return_fail_output;     /* ditto, but only on failure */
+  BOOL    log_output;             /* Similarly for logging */
+  BOOL    log_fail_output;
+  BOOL    log_defer_output;
+  BOOL    retry_use_local_part;   /* Defaults true for local, false for remote */
+#ifdef EXPERIMENTAL_TPDA
+  uschar  *tpda_delivery_action;  /* String to expand on success */
+#endif
+} transport_instance;
+
+
+/* Structure for holding information about a type of transport. The first six
+fields must match driver_info above. */
+
+typedef struct transport_info {
+  uschar *driver_name;            /* Driver name */
+  optionlist *options;            /* Table of private options names */
+  int    *options_count;          /* -> Number of entries in table */
+  void   *options_block;          /* Points to default private block */
+  int     options_len;            /* Length of same in bytes */
+  void (*init)(                   /* Initialization function */
+    struct transport_instance *);
+/****/
+  BOOL (*code)(                   /* Main entry point */
+    transport_instance *,
+    struct address_item *);
+  void (*tidyup)(                 /* Tidyup function */
+    struct transport_instance *);
+  void  (*closedown)(             /* For closing down a passed channel */
+    struct transport_instance *);
+  BOOL    local;                  /* TRUE for local transports */
+} transport_info;
+
+
+
+/* Structure for holding information about the configured routers. */
+
+typedef struct router_instance {
+  struct router_instance *next;
+  uschar *name;
+  struct router_info *info;
+  void   *options_block;          /* Pointer to private options */
+  uschar *driver_name;            /* Must be first */
+
+  uschar *address_data;           /* Arbitrary data */
+#ifdef EXPERIMENTAL_BRIGHTMAIL
+  uschar *bmi_rule;               /* Brightmail AntiSpam rule checking */
+#endif
+  uschar *cannot_route_message;   /* Used when routing fails */
+  uschar *condition;              /* General condition */
+  uschar *current_directory;      /* For use during delivery */
+  uschar *debug_string;           /* Debugging output */
+  uschar *domains;                /* Specific domains */
+  uschar *errors_to;              /* Errors address */
+  uschar *expand_gid;             /* Expanded gid string */
+  uschar *expand_uid;             /* Expanded uid string */
+  uschar *expand_more;            /* Expanded more string */
+  uschar *expand_unseen;          /* Expanded unseen string */
+  uschar *extra_headers;          /* Additional headers */
+  uschar *fallback_hosts;         /* For remote transports (text list) */
+  uschar *home_directory;         /* For use during delivery */
+  uschar *ignore_target_hosts;    /* Target hosts to ignore */
+  uschar *local_parts;            /* Specific local parts */
+  uschar *pass_router_name;       /* Router for passed address */
+  uschar *prefix;                 /* Address prefix */
+  uschar *redirect_router_name;   /* Router for generated address */
+  uschar *remove_headers;         /* Removed headers */
+  uschar *require_files;          /* File checks before router is run */
+  uschar *router_home_directory;  /* For use while routing */
+  uschar *self;                   /* Text option for handling self reference */
+  uschar *senders;                /* Specific senders */
+  uschar *suffix;                 /* Address suffix */
+  uschar *translate_ip_address;   /* IP address translation fudgery */
+  uschar *transport_name;         /* Transport name */
+
+  BOOL    address_test;           /* Use this router when testing addresses */
+#ifdef EXPERIMENTAL_BRIGHTMAIL
+  BOOL    bmi_deliver_alternate;  /* TRUE => BMI said that message should be delivered to alternate location */
+  BOOL    bmi_deliver_default;    /* TRUE => BMI said that message should be delivered to default location */
+  BOOL    bmi_dont_deliver;       /* TRUE => BMI said that message should not be delivered at all */
+#endif
+  BOOL    expn;                   /* Use this router when processing EXPN */
+  BOOL    caseful_local_part;     /* TRUE => don't lowercase */
+  BOOL    check_local_user;       /* TRUE => check local user */
+  BOOL    disable_logging;        /* For very weird requirements */
+  BOOL    fail_verify_recipient;  /* Fail verify if recipient match this router */
+  BOOL    fail_verify_sender;     /* Fail verify if sender match this router */
+  BOOL    gid_set;                /* Flag to indicate gid is set */
+  BOOL    initgroups;             /* TRUE if initgroups is required */
+  BOOL    log_as_local;           /* TRUE logs as a local delivery */
+  BOOL    more;                   /* If FALSE, do no more if this one fails */
+  BOOL    pass_on_timeout;        /* Treat timeout DEFERs as fails */
+  BOOL    prefix_optional;        /* Just what it says */
+  BOOL    repeat_use;             /* If FALSE, skip if ancestor used it */
+  BOOL    retry_use_local_part;   /* Just what it says */
+  BOOL    same_domain_copy_routing; /* TRUE => copy routing for same domain */
+  BOOL    self_rewrite;           /* TRUE to rewrite headers if making local */
+  BOOL    suffix_optional;        /* As it says */
+  BOOL    verify_only;            /* Skip this router if not verifying */
+  BOOL    verify_recipient;       /* Use this router when verifying a recipient*/
+  BOOL    verify_sender;          /* Use this router when verifying a sender */
+  BOOL    uid_set;                /* Flag to indicate uid is set */
+  BOOL    unseen;                 /* If TRUE carry on, even after success */
+#ifdef EXPERIMENTAL_DSN
+  BOOL    dsn_lasthop;            /* If TRUE, this router is a DSN endpoint */
+#endif
+
+  int     self_code;              /* Encoded version of "self" */
+  uid_t   uid;                    /* Fixed uid value */
+  gid_t   gid;                    /* Fixed gid value */
+
+  host_item *fallback_hostlist;   /* For remote transport (block chain) */
+  transport_instance *transport;  /* Transport block (when found) */
+  struct router_instance *pass_router; /* Actual router for passed address */
+  struct router_instance *redirect_router; /* Actual router for generated address */
+} router_instance;
+
+
+/* Structure for holding information about a type of router. The first six
+fields must match driver_info above. */
+
+typedef struct router_info {
+  uschar *driver_name;
+  optionlist *options;            /* Table of private options names */
+  int    *options_count;          /* -> Number of entries in table */
+  void   *options_block;          /* Points to default private block */
+  int     options_len;            /* Length of same in bytes */
+  void (*init)(                   /* Initialization function */
+    struct router_instance *);
+/****/
+  int (*code)(                    /* Main entry point */
+    router_instance *,
+    struct address_item *,
+    struct passwd *,
+    int,
+    struct address_item **,
+    struct address_item **,
+    struct address_item **,
+    struct address_item **);
+  void (*tidyup)(                 /* Tidyup function */
+    struct router_instance *);
+  int     ri_flags;               /* Descriptive flags */
+} router_info;
+
+
+/* Structure for holding information about a lookup type. */
+
+#include "lookupapi.h"
+
+
+/* Structure for holding information about the configured authentication
+mechanisms */
+
+typedef struct auth_instance {
+  struct auth_instance *next;
+  uschar *name;                   /* Exim instance name */
+  struct auth_info *info;         /* Pointer to driver info block */
+  void   *options_block;          /* Pointer to private options */
+  uschar *driver_name;            /* Must be first */
+  uschar *advertise_condition;    /* Are we going to advertise this?*/
+  uschar *client_condition;       /* Should the client try this? */
+  uschar *public_name;            /* Advertised name */
+  uschar *set_id;                 /* String to set when server as authenticated id */
+  uschar *set_client_id;          /* String to set when client as client_authenticated id */
+  uschar *mail_auth_condition;    /* Condition for AUTH on MAIL command */
+  uschar *server_debug_string;    /* Debugging output */
+  uschar *server_condition;       /* Authorization condition */
+  BOOL    client;                 /* TRUE if client option(s) set */
+  BOOL    server;                 /* TRUE if server options(s) set */
+  BOOL    advertised;             /* Set TRUE when advertised */
+} auth_instance;
+
+
+/* Structure for holding information about an authentication mechanism. The
+first six fields must match driver_info above. */
+
+typedef struct auth_info {
+  uschar *driver_name;            /* e.g. "condition" */
+  optionlist *options;            /* Table of private options names */
+  int    *options_count;          /* -> Number of entries in table */
+  void   *options_block;          /* Points to default private block */
+  int     options_len;            /* Length of same in bytes */
+  void (*init)(                   /* initialization function */
+    struct auth_instance *);
+/****/
+  int (*servercode)(              /* server function */
+    auth_instance *,              /* the instance data */
+    uschar *);                    /* rest of AUTH command */
+  int (*clientcode)(              /* client function */
+    struct auth_instance *,
+    struct smtp_inblock *,        /* socket and input buffer */
+    struct smtp_outblock *,       /* socket and output buffer */
+    int,                          /* command timeout */
+    uschar *,                     /* buffer for reading response */
+    int);                         /* sizeof buffer */
+  void (*version_report)(         /* diagnostic version reporting */
+    FILE *);                      /* I/O stream to print to */
+} auth_info;
+
+
+/* Structure for holding a single IP address and port; used for the chain of
+addresses and ports for the local host. Make the char string large enough to
+hold an IPv6 address. */
+
+typedef struct ip_address_item {
+  struct ip_address_item *next;
+  int    port;
+  BOOL   v6_include_v4;            /* Used in the daemon */
+  uschar address[46];
+} ip_address_item;
+
+/* Structure for chaining together arbitrary strings. */
+
+typedef struct string_item {
+  struct string_item *next;
+  uschar *text;
+} string_item;
+
+/* Information about a soft delivery failure, for use when calculating
+retry information. It's separate from the address block, because there
+can be a chain of them for SMTP deliveries where multiple IP addresses
+can be tried. */
+
+typedef struct retry_item {
+  struct retry_item *next;        /* for chaining */
+  uschar *key;                    /* string identifying host/address/message */
+  int     basic_errno;            /* error code for this destination */
+  int     more_errno;             /* additional error information */
+  uschar *message;                /* local error message */
+  int     flags;                  /* see below */
+} retry_item;
+
+/* Retry data flags */
+
+#define rf_delete   0x0001        /* retry info is to be deleted */
+#define rf_host     0x0002        /* retry info is for a remote host */
+#define rf_message  0x0004        /* retry info is for a host+message */
+
+/* Information about a constructed message that is to be sent using the
+autoreply transport. This is pointed to from the address block. */
+
+typedef struct reply_item {
+  uschar *from;                   /* ) */
+  uschar *reply_to;               /* ) */
+  uschar *to;                     /* ) */
+  uschar *cc;                     /* ) specific header fields */
+  uschar *bcc;                    /* ) */
+  uschar *subject;                /* ) */
+  uschar *headers;                /* misc other headers, concatenated */
+  uschar *text;                   /* text string body */
+  uschar *file;                   /* file body */
+  BOOL    file_expand;            /* expand the body */
+  int     expand_forbid;          /* expansion lockout flags */
+  uschar *logfile;                /* file to keep a log in */
+  uschar *oncelog;                /* file to keep records in for once only */
+  time_t  once_repeat;            /* time to repeat "once only" */
+  BOOL    return_message;         /* send back the original message */
+} reply_item;
+
+
+/* The address_item structure contains many fields which are used at various
+times while delivering a message. Some are used only for remote deliveries;
+some only for local. A particular set of fields is copied whenever a child
+address is created. For convenience, we keep those fields in a separate
+sub-structure so they can be copied in one go. This also means I won't forget
+to edit the various copying places when new to-be-copied fields are added. */
+
+typedef struct address_item_propagated {
+  uschar *address_data;           /* arbitrary data to keep with the address */
+  uschar *domain_data;            /* from "domains" lookup */
+  uschar *localpart_data;         /* from "local_parts" lookup */
+  uschar *errors_address;         /* where to send errors (NULL => sender) */
+  header_line *extra_headers;     /* additional headers */
+  uschar *remove_headers;         /* list of those to remove */
+
+  #ifdef EXPERIMENTAL_SRS
+  uschar *srs_sender;             /* Change return path when delivering */
+  #endif
+} address_item_propagated;
+
+/* Bits for the flags field below */
+
+#define af_allow_file          0x00000001 /* allow file in generated address */
+#define af_allow_pipe          0x00000002 /* allow pipe in generated address */
+#define af_allow_reply         0x00000004 /* allow autoreply in generated address */
+#define af_dr_retry_exists     0x00000008 /* router retry record exists */
+#define af_expand_pipe         0x00000010 /* expand pipe arguments */
+#define af_file                0x00000020 /* file delivery; always with pfr */
+#define af_gid_set             0x00000040 /* gid field is set */
+#define af_home_expanded       0x00000080 /* home_dir is already expanded */
+#define af_ignore_error        0x00000100 /* ignore delivery error */
+#define af_initgroups          0x00000200 /* use initgroups() for local transporting */
+#define af_local_host_removed  0x00000400 /* local host was backup */
+#define af_lt_retry_exists     0x00000800 /* local transport retry exists */
+#define af_pfr                 0x00001000 /* pipe or file or reply delivery */
+#define af_retry_skipped       0x00002000 /* true if retry caused some skipping */
+#define af_retry_timedout      0x00004000 /* true if retry timed out */
+#define af_uid_set             0x00008000 /* uid field is set */
+#define af_hide_child          0x00010000 /* hide child in bounce/defer msgs */
+#define af_sverify_told        0x00020000 /* sender verify failure notified */
+#define af_verify_pmfail       0x00040000 /* verify failure was postmaster callout */
+#define af_verify_nsfail       0x00080000 /* verify failure was null sender callout */
+#define af_homonym             0x00100000 /* an ancestor has same address */
+#define af_verify_routed       0x00200000 /* for cached sender verify: routed OK */
+#define af_verify_callout      0x00400000 /* for cached sender verify: callout was specified */
+#define af_include_affixes     0x00800000 /* delivered with affixes in RCPT */
+#define af_cert_verified       0x01000000 /* delivered with verified TLS cert */
+#define af_pass_message        0x02000000 /* pass message in bounces */
+#define af_bad_reply           0x04000000 /* filter could not generate autoreply */
+#ifndef DISABLE_PRDR
+# define af_prdr_used          0x08000000 /* delivery used SMTP PRDR */
+#endif
+#define af_force_command       0x10000000 /* force_command in pipe transport */
+
+/* These flags must be propagated when a child is created */
+
+#define af_propagate           (af_ignore_error)
+
+/* The main address structure. Note that fields that are to be copied to
+generated addresses should be put in the address_item_propagated structure (see
+above) rather than directly into the address_item structure. */
+
+typedef struct address_item {
+  struct address_item *next;      /* for chaining addresses */
+  struct address_item *parent;    /* parent address */
+  struct address_item *first;     /* points to first after group delivery */
+  struct address_item *dupof;     /* points to address this is a duplicate of */
+
+  router_instance *start_router;  /* generated address starts here */
+  router_instance *router;        /* the router that routed */
+  transport_instance *transport;  /* the transport to use */
+
+  host_item *host_list;           /* host data for the transport */
+  host_item *host_used;           /* host that took delivery or failed hard */
+  host_item *fallback_hosts;      /* to try if delivery defers */
+
+  reply_item *reply;              /* data for autoreply */
+  retry_item *retries;            /* chain of retry information */
+
+  uschar *address;                /* address being delivered or routed */
+  uschar *unique;                 /* used for disambiguating */
+  uschar *cc_local_part;          /* caseful local part */
+  uschar *lc_local_part;          /* lowercased local part */
+  uschar *local_part;             /* points to cc or lc version */
+  uschar *prefix;                 /* stripped prefix of local part */
+  uschar *suffix;                 /* stripped suffix of local part */
+  uschar *domain;                 /* working domain (lower cased) */
+
+  uschar *address_retry_key;      /* retry key including full address */
+  uschar *domain_retry_key;       /* retry key for domain only */
+
+  uschar *current_dir;            /* current directory for transporting */
+  uschar *home_dir;               /* home directory for transporting */
+  uschar *message;                /* error message */
+  uschar *user_message;           /* error message that can be sent over SMTP
+                                     or quoted in bounce message */
+  uschar *onetime_parent;         /* saved original parent for onetime */
+  uschar **pipe_expandn;          /* numeric expansions for pipe from filter */
+  uschar *return_filename;        /* name of return file */
+  uschar *self_hostname;          /* after self=pass */
+  uschar *shadow_message;         /* info about shadow transporting */
+
+  #ifdef SUPPORT_TLS
+  uschar *cipher;                 /* Cipher used for transport */
+  void   *ourcert;                /* Certificate offered to peer, binary */
+  void   *peercert;               /* Certificate from peer, binary */
+  uschar *peerdn;                 /* DN of server's certificate */
+  int    ocsp;                   /* OCSP status of peer cert */
+  #endif
+
+  uschar *authenticator;         /* auth driver name used by transport */
+  uschar *auth_id;               /* auth "login" name used by transport */
+  uschar *auth_sndr;             /* AUTH arg to SMTP MAIL, used by transport */
+
+  #ifdef EXPERIMENTAL_DSN
+  uschar *dsn_orcpt;              /* DSN orcpt value */
+  int     dsn_flags;              /* DSN flags */
+  int     dsn_aware;              /* DSN aware flag */
+  #endif
+
+  uid_t   uid;                    /* uid for transporting */
+  gid_t   gid;                    /* gid for transporting */
+
+  unsigned int flags;             /* a row of bits, defined above */
+  unsigned int domain_cache[(MAX_NAMED_LIST * 2)/32];
+  unsigned int localpart_cache[(MAX_NAMED_LIST * 2)/32];
+  int     mode;                   /* mode for local transporting to a file */
+  int     more_errno;             /* additional error information */
+                                  /* (may need to hold a timestamp) */
+
+  short int basic_errno;          /* status after failure */
+  short int child_count;          /* number of child addresses */
+  short int return_file;          /* fileno of return data file */
+  short int special_action;       /* ( used when when deferred or failed */
+                                  /* (  also  */
+                                  /* ( contains = or - when successful SMTP delivered */
+                                  /* (  also  */
+                                  /* ( contains verify rc in sender verify cache */
+  short int transport_return;     /* result of delivery attempt */
+  address_item_propagated p;      /* fields that are propagated to children */
+} address_item;
+
+/* The table of header names consists of items of this type */
+
+typedef struct {
+  uschar *name;
+  int     len;
+  BOOL    allow_resent;
+  int     htype;
+} header_name;
+
+/* Chain of information about errors (e.g. bad addresses) */
+
+typedef struct error_block {
+  struct error_block *next;
+  uschar *text1;
+  uschar *text2;
+} error_block;
+
+/* Chain of file names when processing the queue */
+
+typedef struct queue_filename {
+  struct queue_filename *next;
+  uschar dir_uschar;
+  uschar text[1];
+} queue_filename;
+
+/* Chain of items of retry information, read from the retry config. */
+
+typedef struct retry_rule {
+  struct retry_rule *next;
+  int    rule;
+  int    timeout;
+  int    p1;
+  int    p2;
+} retry_rule;
+
+typedef struct retry_config {
+  struct retry_config *next;
+  uschar *pattern;
+  int     basic_errno;
+  int     more_errno;
+  uschar *senders;
+  retry_rule *rules;
+} retry_config;
+
+/* Structure for each node in a tree, of which there are various kinds */
+
+typedef struct tree_node {
+  struct tree_node *left;         /* pointer to left child */
+  struct tree_node *right;        /* pointer to right child */
+  union
+    {
+    void  *ptr;                   /* pointer to data */
+    int val;                      /* or integer data */
+    } data;
+  uschar  balance;                /* balancing factor */
+  uschar  name[1];                /* node name - variable length */
+} tree_node;
+
+/* Structure for holding the handle and the cached last lookup for searches.
+This block is pointed to by the tree entry for the file. The file can get
+closed if too many are opened at once. There is a LRU chain for deciding which
+to close. */
+
+typedef struct search_cache {
+  void   *handle;                 /* lookup handle, or NULL if closed */
+  int search_type;                /* search type */
+  tree_node *up;                  /* LRU up pointer */
+  tree_node *down;                /* LRU down pointer */
+  tree_node *item_cache;          /* tree of cached results */
+} search_cache;
+
+/* Structure for holding a partially decoded DNS record; the name has been
+uncompressed, but the data pointer is into the raw data. */
+
+typedef struct {
+  uschar  name[DNS_MAXNAME];      /* domain name */
+  int     type;                   /* record type */
+  int     size;                   /* size of data */
+  uschar *data;                   /* pointer to data */
+} dns_record;
+
+/* Structure for holding the result of a DNS query. */
+
+typedef struct {
+  int     answerlen;              /* length of the answer */
+  uschar  answer[MAXPACKET];      /* the answer itself */
+} dns_answer;
+
+/* Structure for holding the intermediate data while scanning a DNS answer
+block. */
+
+typedef struct {
+  int     rrcount;                /* count of RRs in the answer */
+  uschar *aptr;                   /* pointer in the answer while scanning */
+  dns_record srr;                 /* data from current record in scan */
+} dns_scan;
+
+/* Structure for holding a chain of IP addresses that are extracted from
+an A, AAAA, or A6 record. For the first two, there is only ever one address,
+but the chaining feature of A6 allows for several addresses to be realized from
+a single initial A6 record. The structure defines the address field of length
+1. In use, a suitable sized block is obtained to hold the complete textual
+address. */
+
+typedef struct dns_address {
+  struct dns_address *next;
+  uschar  address[1];
+} dns_address;
+
+/* Structure used for holding intermediate data during MD5 computations. */
+
+typedef struct md5 {
+  unsigned int length;
+  unsigned int abcd[4];
+  }
+md5;
+
+/* Structure used for holding intermediate data during SHA-1 computations. */
+
+typedef struct sha1 {
+  unsigned int H[5];
+  unsigned int length;
+  }
+sha1;
+
+/* Structure used to hold incoming packets of SMTP responses for a specific
+socket. The packets which may contain multiple lines (and in some cases,
+multiple responses). */
+
+typedef struct smtp_inblock {
+  int     sock;                   /* the socket */
+  int     buffersize;             /* the size of the buffer */
+  uschar *ptr;                    /* current position in the buffer */
+  uschar *ptrend;                 /* end of data in the buffer */
+  uschar *buffer;                 /* the buffer itself */
+} smtp_inblock;
+
+/* Structure used to hold buffered outgoing packets of SMTP commands for a
+specific socket. The packets which may contain multiple lines when pipelining
+is in use. */
+
+typedef struct smtp_outblock {
+  int     sock;                   /* the socket */
+  int     cmd_count;              /* count of buffered commands */
+  int     buffersize;             /* the size of the buffer */
+  BOOL    authenticating;         /* TRUE when authenticating */
+  uschar *ptr;                    /* current position in the buffer */
+  uschar *buffer;                 /* the buffer itself */
+} smtp_outblock;
+
+/* Structure to hold information about the source of redirection information */
+
+typedef struct redirect_block {
+  uschar *string;                 /* file name or string */
+  uid_t  *owners;                 /* allowed file owners */
+  gid_t  *owngroups;              /* allowed file groups */
+  struct passwd *pw;              /* possible owner if not NULL */
+  int     modemask;               /* forbidden bits */
+  BOOL    isfile;                 /* TRUE if string is a file name */
+  BOOL    check_owner;            /* TRUE, FALSE, or TRUE_UNSET */
+  BOOL    check_group;            /* TRUE, FALSE, or TRUE_UNSET */
+} redirect_block;
+
+/* Structure for passing arguments to check_host() */
+
+typedef struct check_host_block {
+  uschar *host_name;
+  uschar *host_address;
+  uschar *host_ipv4;
+  BOOL   negative;
+} check_host_block;
+
+/* Structure for remembering lookup data when caching the result of
+a lookup in a named list. */
+
+typedef struct namedlist_cacheblock {
+  struct namedlist_cacheblock *next;
+  uschar *key;
+  uschar *data;
+} namedlist_cacheblock;
+
+/* Structure for holding data for an entry in a named list */
+
+typedef struct namedlist_block {
+  uschar *string;                    /* the list string */
+  namedlist_cacheblock *cache_data;  /* cached domain_data or localpart_data */
+  int number;                        /* the number of the list for caching */
+} namedlist_block;
+
+/* Structures for Access Control Lists */
+
+typedef struct acl_condition_block {
+  struct acl_condition_block *next;
+  uschar *arg;
+  int type;
+  union {
+    BOOL negated;
+    uschar *varname;
+  } u;
+} acl_condition_block;
+
+typedef struct acl_block {
+  struct acl_block *next;
+  acl_condition_block *condition;
+  int verb;
+} acl_block;
+
+/* End of structs.h */
diff --git a/src/tls-gnu.c b/src/tls-gnu.c
new file mode 100644 (file)
index 0000000..266ab89
--- /dev/null
@@ -0,0 +1,2258 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2014 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Copyright (c) Phil Pennock 2012 */
+
+/* This file provides TLS/SSL support for Exim using the GnuTLS library,
+one of the available supported implementations.  This file is #included into
+tls.c when USE_GNUTLS has been set.
+
+The code herein is a revamp of GnuTLS integration using the current APIs; the
+original tls-gnu.c was based on a patch which was contributed by Nikos
+Mavroyanopoulos.  The revamp is partially a rewrite, partially cut&paste as
+appropriate.
+
+APIs current as of GnuTLS 2.12.18; note that the GnuTLS manual is for GnuTLS 3,
+which is not widely deployed by OS vendors.  Will note issues below, which may
+assist in updating the code in the future.  Another sources of hints is
+mod_gnutls for Apache (SNI callback registration and handling).
+
+Keeping client and server variables more split than before and is currently
+the norm, in anticipation of TLS in ACL callouts.
+
+I wanted to switch to gnutls_certificate_set_verify_function() so that
+certificate rejection could happen during handshake where it belongs, rather
+than being dropped afterwards, but that was introduced in 2.10.0 and Debian
+(6.0.5) is still on 2.8.6.  So for now we have to stick with sub-par behaviour.
+
+(I wasn't looking for libraries quite that old, when updating to get rid of
+compiler warnings of deprecated APIs.  If it turns out that a lot of the rest
+require current GnuTLS, then we'll drop support for the ancient libraries).
+*/
+
+#include <gnutls/gnutls.h>
+/* needed for cert checks in verification and DN extraction: */
+#include <gnutls/x509.h>
+/* man-page is incorrect, gnutls_rnd() is not in gnutls.h: */
+#include <gnutls/crypto.h>
+/* needed to disable PKCS11 autoload unless requested */
+#if GNUTLS_VERSION_NUMBER >= 0x020c00
+# include <gnutls/pkcs11.h>
+#endif
+#if GNUTLS_VERSION_NUMBER < 0x030103 && !defined(DISABLE_OCSP)
+# warning "GnuTLS library version too old; define DISABLE_OCSP in Makefile"
+# define DISABLE_OCSP
+#endif
+
+#ifndef DISABLE_OCSP
+# include <gnutls/ocsp.h>
+#endif
+
+/* GnuTLS 2 vs 3
+
+GnuTLS 3 only:
+  gnutls_global_set_audit_log_function()
+
+Changes:
+  gnutls_certificate_verify_peers2(): is new, drop the 2 for old version
+*/
+
+/* Local static variables for GnuTLS */
+
+/* Values for verify_requirement */
+
+enum peer_verify_requirement
+  { VERIFY_NONE, VERIFY_OPTIONAL, VERIFY_REQUIRED
+#ifdef EXPERIMENTAL_CERTNAMES
+    ,VERIFY_WITHHOST
+#endif
+  };
+
+/* This holds most state for server or client; with this, we can set up an
+outbound TLS-enabled connection in an ACL callout, while not stomping all
+over the TLS variables available for expansion.
+
+Some of these correspond to variables in globals.c; those variables will
+be set to point to content in one of these instances, as appropriate for
+the stage of the process lifetime.
+
+Not handled here: global tls_channelbinding_b64.
+*/
+
+typedef struct exim_gnutls_state {
+  gnutls_session_t     session;
+  gnutls_certificate_credentials_t x509_cred;
+  gnutls_priority_t    priority_cache;
+  enum peer_verify_requirement verify_requirement;
+  int                  fd_in;
+  int                  fd_out;
+  BOOL                 peer_cert_verified;
+  BOOL                 trigger_sni_changes;
+  BOOL                 have_set_peerdn;
+  const struct host_item *host;
+  gnutls_x509_crt_t    peercert;
+  uschar               *peerdn;
+  uschar               *ciphersuite;
+  uschar               *received_sni;
+
+  const uschar *tls_certificate;
+  const uschar *tls_privatekey;
+  const uschar *tls_sni; /* client send only, not received */
+  const uschar *tls_verify_certificates;
+  const uschar *tls_crl;
+  const uschar *tls_require_ciphers;
+
+  uschar *exp_tls_certificate;
+  uschar *exp_tls_privatekey;
+  uschar *exp_tls_verify_certificates;
+  uschar *exp_tls_crl;
+  uschar *exp_tls_require_ciphers;
+  uschar *exp_tls_ocsp_file;
+#ifdef EXPERIMENTAL_CERTNAMES
+  uschar *exp_tls_verify_cert_hostnames;
+#endif
+
+  tls_support *tlsp;   /* set in tls_init() */
+
+  uschar *xfer_buffer;
+  int xfer_buffer_lwm;
+  int xfer_buffer_hwm;
+  int xfer_eof;
+  int xfer_error;
+} exim_gnutls_state_st;
+
+static const exim_gnutls_state_st exim_gnutls_state_init = {
+  NULL, NULL, NULL, VERIFY_NONE, -1, -1, FALSE, FALSE, FALSE,
+  NULL, NULL, NULL, NULL,
+  NULL, NULL, NULL, NULL, NULL, NULL,
+  NULL, NULL, NULL, NULL, NULL, NULL, NULL,
+#ifdef EXPERIMENTAL_CERTNAMES
+                                            NULL,
+#endif
+  NULL,
+  NULL, 0, 0, 0, 0,
+};
+
+/* Not only do we have our own APIs which don't pass around state, assuming
+it's held in globals, GnuTLS doesn't appear to let us register callback data
+for callbacks, or as part of the session, so we have to keep a "this is the
+context we're currently dealing with" pointer and rely upon being
+single-threaded to keep from processing data on an inbound TLS connection while
+talking to another TLS connection for an outbound check.  This does mean that
+there's no way for heart-beats to be responded to, for the duration of the
+second connection. */
+
+static exim_gnutls_state_st state_server, state_client;
+
+/* dh_params are initialised once within the lifetime of a process using TLS;
+if we used TLS in a long-lived daemon, we'd have to reconsider this.  But we
+don't want to repeat this. */
+
+static gnutls_dh_params_t dh_server_params = NULL;
+
+/* No idea how this value was chosen; preserving it.  Default is 3600. */
+
+static const int ssl_session_timeout = 200;
+
+static const char * const exim_default_gnutls_priority = "NORMAL";
+
+/* Guard library core initialisation */
+
+static BOOL exim_gnutls_base_init_done = FALSE;
+
+
+/* ------------------------------------------------------------------------ */
+/* macros */
+
+#define MAX_HOST_LEN 255
+
+/* Set this to control gnutls_global_set_log_level(); values 0 to 9 will setup
+the library logging; a value less than 0 disables the calls to set up logging
+callbacks. */
+#ifndef EXIM_GNUTLS_LIBRARY_LOG_LEVEL
+#define EXIM_GNUTLS_LIBRARY_LOG_LEVEL -1
+#endif
+
+#ifndef EXIM_CLIENT_DH_MIN_BITS
+#define EXIM_CLIENT_DH_MIN_BITS 1024
+#endif
+
+/* With GnuTLS 2.12.x+ we have gnutls_sec_param_to_pk_bits() with which we
+can ask for a bit-strength.  Without that, we stick to the constant we had
+before, for now. */
+#ifndef EXIM_SERVER_DH_BITS_PRE2_12
+#define EXIM_SERVER_DH_BITS_PRE2_12 1024
+#endif
+
+#define exim_gnutls_err_check(Label) do { \
+  if (rc != GNUTLS_E_SUCCESS) { return tls_error((Label), gnutls_strerror(rc), host); } } while (0)
+
+#define expand_check_tlsvar(Varname) expand_check(state->Varname, US #Varname, &state->exp_##Varname)
+
+#if GNUTLS_VERSION_NUMBER >= 0x020c00
+# define HAVE_GNUTLS_SESSION_CHANNEL_BINDING
+# define HAVE_GNUTLS_SEC_PARAM_CONSTANTS
+# define HAVE_GNUTLS_RND
+/* The security fix we provide with the gnutls_allow_auto_pkcs11 option
+ * (4.82 PP/09) introduces a compatibility regression. The symbol simply
+ * isn't available sometimes, so this needs to become a conditional
+ * compilation; the sanest way to deal with this being a problem on
+ * older OSes is to block it in the Local/Makefile with this compiler
+ * definition  */
+# ifndef AVOID_GNUTLS_PKCS11
+#  define HAVE_GNUTLS_PKCS11
+# endif /* AVOID_GNUTLS_PKCS11 */
+#endif
+
+
+
+
+/* ------------------------------------------------------------------------ */
+/* Callback declarations */
+
+#if EXIM_GNUTLS_LIBRARY_LOG_LEVEL >= 0
+static void exim_gnutls_logger_cb(int level, const char *message);
+#endif
+
+static int exim_sni_handling_cb(gnutls_session_t session);
+
+#ifndef DISABLE_OCSP
+static int server_ocsp_stapling_cb(gnutls_session_t session, void * ptr,
+  gnutls_datum_t * ocsp_response);
+#endif
+
+
+
+/* ------------------------------------------------------------------------ */
+/* Static functions */
+
+/*************************************************
+*               Handle TLS error                 *
+*************************************************/
+
+/* Called from lots of places when errors occur before actually starting to do
+the TLS handshake, that is, while the session is still in clear. Always returns
+DEFER for a server and FAIL for a client so that most calls can use "return
+tls_error(...)" to do this processing and then give an appropriate return. A
+single function is used for both server and client, because it is called from
+some shared functions.
+
+Argument:
+  prefix    text to include in the logged error
+  msg       additional error string (may be NULL)
+            usually obtained from gnutls_strerror()
+  host      NULL if setting up a server;
+            the connected host if setting up a client
+
+Returns:    OK/DEFER/FAIL
+*/
+
+static int
+tls_error(const uschar *prefix, const char *msg, const host_item *host)
+{
+if (host)
+  {
+  log_write(0, LOG_MAIN, "TLS error on connection to %s [%s] (%s)%s%s",
+      host->name, host->address, prefix, msg ? ": " : "", msg ? msg : "");
+  return FAIL;
+  }
+else
+  {
+  uschar *conn_info = smtp_get_connection_info();
+  if (Ustrncmp(conn_info, US"SMTP ", 5) == 0)
+    conn_info += 5;
+  log_write(0, LOG_MAIN, "TLS error on %s (%s)%s%s",
+      conn_info, prefix, msg ? ": " : "", msg ? msg : "");
+  return DEFER;
+  }
+}
+
+
+
+
+/*************************************************
+*    Deal with logging errors during I/O         *
+*************************************************/
+
+/* We have to get the identity of the peer from saved data.
+
+Argument:
+  state    the current GnuTLS exim state container
+  rc       the GnuTLS error code, or 0 if it's a local error
+  when     text identifying read or write
+  text     local error text when ec is 0
+
+Returns:   nothing
+*/
+
+static void
+record_io_error(exim_gnutls_state_st *state, int rc, uschar *when, uschar *text)
+{
+const char *msg;
+
+if (rc == GNUTLS_E_FATAL_ALERT_RECEIVED)
+  msg = CS string_sprintf("%s: %s", US gnutls_strerror(rc),
+    US gnutls_alert_get_name(gnutls_alert_get(state->session)));
+else
+  msg = gnutls_strerror(rc);
+
+tls_error(when, msg, state->host);
+}
+
+
+
+
+/*************************************************
+*        Set various Exim expansion vars         *
+*************************************************/
+
+#define exim_gnutls_cert_err(Label) \
+  do \
+    { \
+    if (rc != GNUTLS_E_SUCCESS) \
+      { \
+      DEBUG(D_tls) debug_printf("TLS: cert problem: %s: %s\n", \
+       (Label), gnutls_strerror(rc)); \
+      return rc; \
+      } \
+    } while (0)
+
+static int
+import_cert(const gnutls_datum * cert, gnutls_x509_crt_t * crtp)
+{
+int rc;
+
+rc = gnutls_x509_crt_init(crtp);
+exim_gnutls_cert_err(US"gnutls_x509_crt_init (crt)");
+
+rc = gnutls_x509_crt_import(*crtp, cert, GNUTLS_X509_FMT_DER);
+exim_gnutls_cert_err(US"failed to import certificate [gnutls_x509_crt_import(cert)]");
+
+return rc;
+}
+
+#undef exim_gnutls_cert_err
+
+
+/* We set various Exim global variables from the state, once a session has
+been established.  With TLS callouts, may need to change this to stack
+variables, or just re-call it with the server state after client callout
+has finished.
+
+Make sure anything set here is unset in tls_getc().
+
+Sets:
+  tls_active                fd
+  tls_bits                  strength indicator
+  tls_certificate_verified  bool indicator
+  tls_channelbinding_b64    for some SASL mechanisms
+  tls_cipher                a string
+  tls_peercert              pointer to library internal
+  tls_peerdn                a string
+  tls_sni                   a (UTF-8) string
+  tls_ourcert               pointer to library internal
+
+Argument:
+  state      the relevant exim_gnutls_state_st *
+*/
+
+static void
+extract_exim_vars_from_tls_state(exim_gnutls_state_st * state)
+{
+gnutls_cipher_algorithm_t cipher;
+#ifdef HAVE_GNUTLS_SESSION_CHANNEL_BINDING
+int old_pool;
+int rc;
+gnutls_datum_t channel;
+#endif
+tls_support * tlsp = state->tlsp;
+
+tlsp->active = state->fd_out;
+
+cipher = gnutls_cipher_get(state->session);
+/* returns size in "bytes" */
+tlsp->bits = gnutls_cipher_get_key_size(cipher) * 8;
+
+tlsp->cipher = state->ciphersuite;
+
+DEBUG(D_tls) debug_printf("cipher: %s\n", state->ciphersuite);
+
+tlsp->certificate_verified = state->peer_cert_verified;
+
+/* note that tls_channelbinding_b64 is not saved to the spool file, since it's
+only available for use for authenticators while this TLS session is running. */
+
+tls_channelbinding_b64 = NULL;
+#ifdef HAVE_GNUTLS_SESSION_CHANNEL_BINDING
+channel.data = NULL;
+channel.size = 0;
+rc = gnutls_session_channel_binding(state->session, GNUTLS_CB_TLS_UNIQUE, &channel);
+if (rc) {
+  DEBUG(D_tls) debug_printf("Channel binding error: %s\n", gnutls_strerror(rc));
+} else {
+  old_pool = store_pool;
+  store_pool = POOL_PERM;
+  tls_channelbinding_b64 = auth_b64encode(channel.data, (int)channel.size);
+  store_pool = old_pool;
+  DEBUG(D_tls) debug_printf("Have channel bindings cached for possible auth usage.\n");
+}
+#endif
+
+/* peercert is set in peer_status() */
+tlsp->peerdn = state->peerdn;
+tlsp->sni =    state->received_sni;
+
+/* record our certificate */
+  {
+  const gnutls_datum * cert = gnutls_certificate_get_ours(state->session);
+  gnutls_x509_crt_t crt;
+
+  tlsp->ourcert = cert && import_cert(cert, &crt)==0 ? crt : NULL;
+  }
+}
+
+
+
+
+/*************************************************
+*            Setup up DH parameters              *
+*************************************************/
+
+/* Generating the D-H parameters may take a long time. They only need to
+be re-generated every so often, depending on security policy. What we do is to
+keep these parameters in a file in the spool directory. If the file does not
+exist, we generate them. This means that it is easy to cause a regeneration.
+
+The new file is written as a temporary file and renamed, so that an incomplete
+file is never present. If two processes both compute some new parameters, you
+waste a bit of effort, but it doesn't seem worth messing around with locking to
+prevent this.
+
+Returns:     OK/DEFER/FAIL
+*/
+
+static int
+init_server_dh(void)
+{
+int fd, rc;
+unsigned int dh_bits;
+gnutls_datum m;
+uschar filename_buf[PATH_MAX];
+uschar *filename = NULL;
+size_t sz;
+uschar *exp_tls_dhparam;
+BOOL use_file_in_spool = FALSE;
+BOOL use_fixed_file = FALSE;
+host_item *host = NULL; /* dummy for macros */
+
+DEBUG(D_tls) debug_printf("Initialising GnuTLS server params.\n");
+
+rc = gnutls_dh_params_init(&dh_server_params);
+exim_gnutls_err_check(US"gnutls_dh_params_init");
+
+m.data = NULL;
+m.size = 0;
+
+if (!expand_check(tls_dhparam, US"tls_dhparam", &exp_tls_dhparam))
+  return DEFER;
+
+if (!exp_tls_dhparam)
+  {
+  DEBUG(D_tls) debug_printf("Loading default hard-coded DH params\n");
+  m.data = US std_dh_prime_default();
+  m.size = Ustrlen(m.data);
+  }
+else if (Ustrcmp(exp_tls_dhparam, "historic") == 0)
+  use_file_in_spool = TRUE;
+else if (Ustrcmp(exp_tls_dhparam, "none") == 0)
+  {
+  DEBUG(D_tls) debug_printf("Requested no DH parameters.\n");
+  return OK;
+  }
+else if (exp_tls_dhparam[0] != '/')
+  {
+  m.data = US std_dh_prime_named(exp_tls_dhparam);
+  if (m.data == NULL)
+    return tls_error(US"No standard prime named", CS exp_tls_dhparam, NULL);
+  m.size = Ustrlen(m.data);
+  }
+else
+  {
+  use_fixed_file = TRUE;
+  filename = exp_tls_dhparam;
+  }
+
+if (m.data)
+  {
+  rc = gnutls_dh_params_import_pkcs3(dh_server_params, &m, GNUTLS_X509_FMT_PEM);
+  exim_gnutls_err_check(US"gnutls_dh_params_import_pkcs3");
+  DEBUG(D_tls) debug_printf("Loaded fixed standard D-H parameters\n");
+  return OK;
+  }
+
+#ifdef HAVE_GNUTLS_SEC_PARAM_CONSTANTS
+/* If you change this constant, also change dh_param_fn_ext so that we can use a
+different filename and ensure we have sufficient bits. */
+dh_bits = gnutls_sec_param_to_pk_bits(GNUTLS_PK_DH, GNUTLS_SEC_PARAM_NORMAL);
+if (!dh_bits)
+  return tls_error(US"gnutls_sec_param_to_pk_bits() failed", NULL, NULL);
+DEBUG(D_tls)
+  debug_printf("GnuTLS tells us that for D-H PK, NORMAL is %d bits.\n",
+      dh_bits);
+#else
+dh_bits = EXIM_SERVER_DH_BITS_PRE2_12;
+DEBUG(D_tls)
+  debug_printf("GnuTLS lacks gnutls_sec_param_to_pk_bits(), using %d bits.\n",
+      dh_bits);
+#endif
+
+/* Some clients have hard-coded limits. */
+if (dh_bits > tls_dh_max_bits)
+  {
+  DEBUG(D_tls)
+    debug_printf("tls_dh_max_bits clamping override, using %d bits instead.\n",
+        tls_dh_max_bits);
+  dh_bits = tls_dh_max_bits;
+  }
+
+if (use_file_in_spool)
+  {
+  if (!string_format(filename_buf, sizeof(filename_buf),
+        "%s/gnutls-params-%d", spool_directory, dh_bits))
+    return tls_error(US"overlong filename", NULL, NULL);
+  filename = filename_buf;
+  }
+
+/* Open the cache file for reading and if successful, read it and set up the
+parameters. */
+
+fd = Uopen(filename, O_RDONLY, 0);
+if (fd >= 0)
+  {
+  struct stat statbuf;
+  FILE *fp;
+  int saved_errno;
+
+  if (fstat(fd, &statbuf) < 0)  /* EIO */
+    {
+    saved_errno = errno;
+    (void)close(fd);
+    return tls_error(US"TLS cache stat failed", strerror(saved_errno), NULL);
+    }
+  if (!S_ISREG(statbuf.st_mode))
+    {
+    (void)close(fd);
+    return tls_error(US"TLS cache not a file", NULL, NULL);
+    }
+  fp = fdopen(fd, "rb");
+  if (!fp)
+    {
+    saved_errno = errno;
+    (void)close(fd);
+    return tls_error(US"fdopen(TLS cache stat fd) failed",
+        strerror(saved_errno), NULL);
+    }
+
+  m.size = statbuf.st_size;
+  m.data = malloc(m.size);
+  if (m.data == NULL)
+    {
+    fclose(fp);
+    return tls_error(US"malloc failed", strerror(errno), NULL);
+    }
+  sz = fread(m.data, m.size, 1, fp);
+  if (!sz)
+    {
+    saved_errno = errno;
+    fclose(fp);
+    free(m.data);
+    return tls_error(US"fread failed", strerror(saved_errno), NULL);
+    }
+  fclose(fp);
+
+  rc = gnutls_dh_params_import_pkcs3(dh_server_params, &m, GNUTLS_X509_FMT_PEM);
+  free(m.data);
+  exim_gnutls_err_check(US"gnutls_dh_params_import_pkcs3");
+  DEBUG(D_tls) debug_printf("read D-H parameters from file \"%s\"\n", filename);
+  }
+
+/* If the file does not exist, fall through to compute new data and cache it.
+If there was any other opening error, it is serious. */
+
+else if (errno == ENOENT)
+  {
+  rc = -1;
+  DEBUG(D_tls)
+    debug_printf("D-H parameter cache file \"%s\" does not exist\n", filename);
+  }
+else
+  return tls_error(string_open_failed(errno, "\"%s\" for reading", filename),
+      NULL, NULL);
+
+/* If ret < 0, either the cache file does not exist, or the data it contains
+is not useful. One particular case of this is when upgrading from an older
+release of Exim in which the data was stored in a different format. We don't
+try to be clever and support both formats; we just regenerate new data in this
+case. */
+
+if (rc < 0)
+  {
+  uschar *temp_fn;
+  unsigned int dh_bits_gen = dh_bits;
+
+  if ((PATH_MAX - Ustrlen(filename)) < 10)
+    return tls_error(US"Filename too long to generate replacement",
+        CS filename, NULL);
+
+  temp_fn = string_copy(US "%s.XXXXXXX");
+  fd = mkstemp(CS temp_fn); /* modifies temp_fn */
+  if (fd < 0)
+    return tls_error(US"Unable to open temp file", strerror(errno), NULL);
+  (void)fchown(fd, exim_uid, exim_gid);   /* Probably not necessary */
+
+  /* GnuTLS overshoots!
+   * If we ask for 2236, we might get 2237 or more.
+   * But there's no way to ask GnuTLS how many bits there really are.
+   * We can ask how many bits were used in a TLS session, but that's it!
+   * The prime itself is hidden behind too much abstraction.
+   * So we ask for less, and proceed on a wing and a prayer.
+   * First attempt, subtracted 3 for 2233 and got 2240.
+   */
+  if (dh_bits >= EXIM_CLIENT_DH_MIN_BITS + 10)
+    {
+    dh_bits_gen = dh_bits - 10;
+    DEBUG(D_tls)
+      debug_printf("being paranoid about DH generation, make it '%d' bits'\n",
+          dh_bits_gen);
+    }
+
+  DEBUG(D_tls)
+    debug_printf("requesting generation of %d bit Diffie-Hellman prime ...\n",
+        dh_bits_gen);
+  rc = gnutls_dh_params_generate2(dh_server_params, dh_bits_gen);
+  exim_gnutls_err_check(US"gnutls_dh_params_generate2");
+
+  /* gnutls_dh_params_export_pkcs3() will tell us the exact size, every time,
+  and I confirmed that a NULL call to get the size first is how the GnuTLS
+  sample apps handle this. */
+
+  sz = 0;
+  m.data = NULL;
+  rc = gnutls_dh_params_export_pkcs3(dh_server_params, GNUTLS_X509_FMT_PEM,
+      m.data, &sz);
+  if (rc != GNUTLS_E_SHORT_MEMORY_BUFFER)
+    exim_gnutls_err_check(US"gnutls_dh_params_export_pkcs3(NULL) sizing");
+  m.size = sz;
+  m.data = malloc(m.size);
+  if (m.data == NULL)
+    return tls_error(US"memory allocation failed", strerror(errno), NULL);
+  /* this will return a size 1 less than the allocation size above */
+  rc = gnutls_dh_params_export_pkcs3(dh_server_params, GNUTLS_X509_FMT_PEM,
+      m.data, &sz);
+  if (rc != GNUTLS_E_SUCCESS)
+    {
+    free(m.data);
+    exim_gnutls_err_check(US"gnutls_dh_params_export_pkcs3() real");
+    }
+  m.size = sz; /* shrink by 1, probably */
+
+  sz = write_to_fd_buf(fd, m.data, (size_t) m.size);
+  if (sz != m.size)
+    {
+    free(m.data);
+    return tls_error(US"TLS cache write D-H params failed",
+        strerror(errno), NULL);
+    }
+  free(m.data);
+  sz = write_to_fd_buf(fd, US"\n", 1);
+  if (sz != 1)
+    return tls_error(US"TLS cache write D-H params final newline failed",
+        strerror(errno), NULL);
+
+  rc = close(fd);
+  if (rc)
+    return tls_error(US"TLS cache write close() failed",
+        strerror(errno), NULL);
+
+  if (Urename(temp_fn, filename) < 0)
+    return tls_error(string_sprintf("failed to rename \"%s\" as \"%s\"",
+          temp_fn, filename), strerror(errno), NULL);
+
+  DEBUG(D_tls) debug_printf("wrote D-H parameters to file \"%s\"\n", filename);
+  }
+
+DEBUG(D_tls) debug_printf("initialized server D-H parameters\n");
+return OK;
+}
+
+
+
+
+/*************************************************
+*       Variables re-expanded post-SNI           *
+*************************************************/
+
+/* Called from both server and client code, via tls_init(), and also from
+the SNI callback after receiving an SNI, if tls_certificate includes "tls_sni".
+
+We can tell the two apart by state->received_sni being non-NULL in callback.
+
+The callback should not call us unless state->trigger_sni_changes is true,
+which we are responsible for setting on the first pass through.
+
+Arguments:
+  state           exim_gnutls_state_st *
+
+Returns:          OK/DEFER/FAIL
+*/
+
+static int
+tls_expand_session_files(exim_gnutls_state_st *state)
+{
+struct stat statbuf;
+int rc;
+const host_item *host = state->host;  /* macro should be reconsidered? */
+uschar *saved_tls_certificate = NULL;
+uschar *saved_tls_privatekey = NULL;
+uschar *saved_tls_verify_certificates = NULL;
+uschar *saved_tls_crl = NULL;
+int cert_count;
+
+/* We check for tls_sni *before* expansion. */
+if (!host)     /* server */
+  {
+  if (!state->received_sni)
+    {
+    if (state->tls_certificate &&
+        (Ustrstr(state->tls_certificate, US"tls_sni") ||
+         Ustrstr(state->tls_certificate, US"tls_in_sni") ||
+         Ustrstr(state->tls_certificate, US"tls_out_sni")
+       ))
+      {
+      DEBUG(D_tls) debug_printf("We will re-expand TLS session files if we receive SNI.\n");
+      state->trigger_sni_changes = TRUE;
+      }
+    }
+  else
+    {
+    /* useful for debugging */
+    saved_tls_certificate = state->exp_tls_certificate;
+    saved_tls_privatekey = state->exp_tls_privatekey;
+    saved_tls_verify_certificates = state->exp_tls_verify_certificates;
+    saved_tls_crl = state->exp_tls_crl;
+    }
+  }
+
+rc = gnutls_certificate_allocate_credentials(&state->x509_cred);
+exim_gnutls_err_check(US"gnutls_certificate_allocate_credentials");
+
+/* remember: expand_check_tlsvar() is expand_check() but fiddling with
+state members, assuming consistent naming; and expand_check() returns
+false if expansion failed, unless expansion was forced to fail. */
+
+/* check if we at least have a certificate, before doing expensive
+D-H generation. */
+
+if (!expand_check_tlsvar(tls_certificate))
+  return DEFER;
+
+/* certificate is mandatory in server, optional in client */
+
+if ((state->exp_tls_certificate == NULL) ||
+    (*state->exp_tls_certificate == '\0'))
+  {
+  if (!host)
+    return tls_error(US"no TLS server certificate is specified", NULL, NULL);
+  else
+    DEBUG(D_tls) debug_printf("TLS: no client certificate specified; okay\n");
+  }
+
+if (state->tls_privatekey && !expand_check_tlsvar(tls_privatekey))
+  return DEFER;
+
+/* tls_privatekey is optional, defaulting to same file as certificate */
+
+if (state->tls_privatekey == NULL || *state->tls_privatekey == '\0')
+  {
+  state->tls_privatekey = state->tls_certificate;
+  state->exp_tls_privatekey = state->exp_tls_certificate;
+  }
+
+
+if (state->exp_tls_certificate && *state->exp_tls_certificate)
+  {
+  DEBUG(D_tls) debug_printf("certificate file = %s\nkey file = %s\n",
+      state->exp_tls_certificate, state->exp_tls_privatekey);
+
+  if (state->received_sni)
+    {
+    if ((Ustrcmp(state->exp_tls_certificate, saved_tls_certificate) == 0) &&
+        (Ustrcmp(state->exp_tls_privatekey, saved_tls_privatekey) == 0))
+      {
+      DEBUG(D_tls) debug_printf("TLS SNI: cert and key unchanged\n");
+      }
+    else
+      {
+      DEBUG(D_tls) debug_printf("TLS SNI: have a changed cert/key pair.\n");
+      }
+    }
+
+  rc = gnutls_certificate_set_x509_key_file(state->x509_cred,
+      CS state->exp_tls_certificate, CS state->exp_tls_privatekey,
+      GNUTLS_X509_FMT_PEM);
+  exim_gnutls_err_check(
+      string_sprintf("cert/key setup: cert=%s key=%s",
+        state->exp_tls_certificate, state->exp_tls_privatekey));
+  DEBUG(D_tls) debug_printf("TLS: cert/key registered\n");
+  } /* tls_certificate */
+
+
+/* Set the OCSP stapling server info */
+
+#ifndef DISABLE_OCSP
+if (  !host    /* server */
+   && tls_ocsp_file
+   )
+  {
+  if (!expand_check(tls_ocsp_file, US"tls_ocsp_file",
+       &state->exp_tls_ocsp_file))
+    return DEFER;
+
+  /* Use the full callback method for stapling just to get observability.
+  More efficient would be to read the file once only, if it never changed
+  (due to SNI). Would need restart on file update, or watch datestamp.  */
+
+  gnutls_certificate_set_ocsp_status_request_function(state->x509_cred,
+    server_ocsp_stapling_cb, state->exp_tls_ocsp_file);
+
+  DEBUG(D_tls) debug_printf("Set OCSP response file %s\n", &state->exp_tls_ocsp_file);
+  }
+#endif
+
+
+/* Set the trusted CAs file if one is provided, and then add the CRL if one is
+provided. Experiment shows that, if the certificate file is empty, an unhelpful
+error message is provided. However, if we just refrain from setting anything up
+in that case, certificate verification fails, which seems to be the correct
+behaviour. */
+
+if (state->tls_verify_certificates && *state->tls_verify_certificates)
+  {
+  if (!expand_check_tlsvar(tls_verify_certificates))
+    return DEFER;
+  if (state->tls_crl && *state->tls_crl)
+    if (!expand_check_tlsvar(tls_crl))
+      return DEFER;
+
+  if (!(state->exp_tls_verify_certificates &&
+        *state->exp_tls_verify_certificates))
+    {
+    DEBUG(D_tls)
+      debug_printf("TLS: tls_verify_certificates expanded empty, ignoring\n");
+    /* With no tls_verify_certificates, we ignore tls_crl too */
+    return OK;
+    }
+  }
+else
+  {
+  DEBUG(D_tls)
+    debug_printf("TLS: tls_verify_certificates not set or empty, ignoring\n");
+  return OK;
+  }
+
+if (Ustat(state->exp_tls_verify_certificates, &statbuf) < 0)
+  {
+  log_write(0, LOG_MAIN|LOG_PANIC, "could not stat %s "
+      "(tls_verify_certificates): %s", state->exp_tls_verify_certificates,
+      strerror(errno));
+  return DEFER;
+  }
+
+/* The test suite passes in /dev/null; we could check for that path explicitly,
+but who knows if someone has some weird FIFO which always dumps some certs, or
+other weirdness.  The thing we really want to check is that it's not a
+directory, since while OpenSSL supports that, GnuTLS does not.
+So s/!S_ISREG/S_ISDIR/ and change some messsaging ... */
+if (S_ISDIR(statbuf.st_mode))
+  {
+  DEBUG(D_tls)
+    debug_printf("verify certificates path is a dir: \"%s\"\n",
+        state->exp_tls_verify_certificates);
+  log_write(0, LOG_MAIN|LOG_PANIC,
+      "tls_verify_certificates \"%s\" is a directory",
+      state->exp_tls_verify_certificates);
+  return DEFER;
+  }
+
+DEBUG(D_tls) debug_printf("verify certificates = %s size=" OFF_T_FMT "\n",
+        state->exp_tls_verify_certificates, statbuf.st_size);
+
+if (statbuf.st_size == 0)
+  {
+  DEBUG(D_tls)
+    debug_printf("cert file empty, no certs, no verification, ignoring any CRL\n");
+  return OK;
+  }
+
+cert_count = gnutls_certificate_set_x509_trust_file(state->x509_cred,
+    CS state->exp_tls_verify_certificates, GNUTLS_X509_FMT_PEM);
+if (cert_count < 0)
+  {
+  rc = cert_count;
+  exim_gnutls_err_check(US"gnutls_certificate_set_x509_trust_file");
+  }
+DEBUG(D_tls) debug_printf("Added %d certificate authorities.\n", cert_count);
+
+if (state->tls_crl && *state->tls_crl &&
+    state->exp_tls_crl && *state->exp_tls_crl)
+  {
+  DEBUG(D_tls) debug_printf("loading CRL file = %s\n", state->exp_tls_crl);
+  cert_count = gnutls_certificate_set_x509_crl_file(state->x509_cred,
+      CS state->exp_tls_crl, GNUTLS_X509_FMT_PEM);
+  if (cert_count < 0)
+    {
+    rc = cert_count;
+    exim_gnutls_err_check(US"gnutls_certificate_set_x509_crl_file");
+    }
+  DEBUG(D_tls) debug_printf("Processed %d CRLs.\n", cert_count);
+  }
+
+return OK;
+}
+
+
+
+
+/*************************************************
+*          Set X.509 state variables             *
+*************************************************/
+
+/* In GnuTLS, the registered cert/key are not replaced by a later
+set of a cert/key, so for SNI support we need a whole new x509_cred
+structure.  Which means various other non-re-expanded pieces of state
+need to be re-set in the new struct, so the setting logic is pulled
+out to this.
+
+Arguments:
+  state           exim_gnutls_state_st *
+
+Returns:          OK/DEFER/FAIL
+*/
+
+static int
+tls_set_remaining_x509(exim_gnutls_state_st *state)
+{
+int rc;
+const host_item *host = state->host;  /* macro should be reconsidered? */
+
+/* Create D-H parameters, or read them from the cache file. This function does
+its own SMTP error messaging. This only happens for the server, TLS D-H ignores
+client-side params. */
+
+if (!state->host)
+  {
+  if (!dh_server_params)
+    {
+    rc = init_server_dh();
+    if (rc != OK) return rc;
+    }
+  gnutls_certificate_set_dh_params(state->x509_cred, dh_server_params);
+  }
+
+/* Link the credentials to the session. */
+
+rc = gnutls_credentials_set(state->session, GNUTLS_CRD_CERTIFICATE, state->x509_cred);
+exim_gnutls_err_check(US"gnutls_credentials_set");
+
+return OK;
+}
+
+/*************************************************
+*            Initialize for GnuTLS               *
+*************************************************/
+
+/* Called from both server and client code. In the case of a server, errors
+before actual TLS negotiation return DEFER.
+
+Arguments:
+  host            connected host, if client; NULL if server
+  certificate     certificate file
+  privatekey      private key file
+  sni             TLS SNI to send, sometimes when client; else NULL
+  cas             CA certs file
+  crl             CRL file
+  require_ciphers tls_require_ciphers setting
+  caller_state    returned state-info structure
+
+Returns:          OK/DEFER/FAIL
+*/
+
+static int
+tls_init(
+    const host_item *host,
+    const uschar *certificate,
+    const uschar *privatekey,
+    const uschar *sni,
+    const uschar *cas,
+    const uschar *crl,
+    const uschar *require_ciphers,
+    exim_gnutls_state_st **caller_state)
+{
+exim_gnutls_state_st *state;
+int rc;
+size_t sz;
+const char *errpos;
+uschar *p;
+BOOL want_default_priorities;
+
+if (!exim_gnutls_base_init_done)
+  {
+  DEBUG(D_tls) debug_printf("GnuTLS global init required.\n");
+
+#ifdef HAVE_GNUTLS_PKCS11
+  /* By default, gnutls_global_init will init PKCS11 support in auto mode,
+  which loads modules from a config file, which sounds good and may be wanted
+  by some sysadmin, but also means in common configurations that GNOME keyring
+  environment variables are used and so breaks for users calling mailq.
+  To prevent this, we init PKCS11 first, which is the documented approach. */
+  if (!gnutls_allow_auto_pkcs11)
+    {
+    rc = gnutls_pkcs11_init(GNUTLS_PKCS11_FLAG_MANUAL, NULL);
+    exim_gnutls_err_check(US"gnutls_pkcs11_init");
+    }
+#endif
+
+  rc = gnutls_global_init();
+  exim_gnutls_err_check(US"gnutls_global_init");
+
+#if EXIM_GNUTLS_LIBRARY_LOG_LEVEL >= 0
+  DEBUG(D_tls)
+    {
+    gnutls_global_set_log_function(exim_gnutls_logger_cb);
+    /* arbitrarily chosen level; bump upto 9 for more */
+    gnutls_global_set_log_level(EXIM_GNUTLS_LIBRARY_LOG_LEVEL);
+    }
+#endif
+
+  exim_gnutls_base_init_done = TRUE;
+  }
+
+if (host)
+  {
+  state = &state_client;
+  memcpy(state, &exim_gnutls_state_init, sizeof(exim_gnutls_state_init));
+  state->tlsp = &tls_out;
+  DEBUG(D_tls) debug_printf("initialising GnuTLS client session\n");
+  rc = gnutls_init(&state->session, GNUTLS_CLIENT);
+  }
+else
+  {
+  state = &state_server;
+  memcpy(state, &exim_gnutls_state_init, sizeof(exim_gnutls_state_init));
+  state->tlsp = &tls_in;
+  DEBUG(D_tls) debug_printf("initialising GnuTLS server session\n");
+  rc = gnutls_init(&state->session, GNUTLS_SERVER);
+  }
+exim_gnutls_err_check(US"gnutls_init");
+
+state->host = host;
+
+state->tls_certificate = certificate;
+state->tls_privatekey = privatekey;
+state->tls_require_ciphers = require_ciphers;
+state->tls_sni = sni;
+state->tls_verify_certificates = cas;
+state->tls_crl = crl;
+
+/* This handles the variables that might get re-expanded after TLS SNI;
+that's tls_certificate, tls_privatekey, tls_verify_certificates, tls_crl */
+
+DEBUG(D_tls)
+  debug_printf("Expanding various TLS configuration options for session credentials.\n");
+rc = tls_expand_session_files(state);
+if (rc != OK) return rc;
+
+/* These are all other parts of the x509_cred handling, since SNI in GnuTLS
+requires a new structure afterwards. */
+
+rc = tls_set_remaining_x509(state);
+if (rc != OK) return rc;
+
+/* set SNI in client, only */
+if (host)
+  {
+  if (!expand_check(sni, US"tls_out_sni", &state->tlsp->sni))
+    return DEFER;
+  if (state->tlsp->sni && *state->tlsp->sni)
+    {
+    DEBUG(D_tls)
+      debug_printf("Setting TLS client SNI to \"%s\"\n", state->tlsp->sni);
+    sz = Ustrlen(state->tlsp->sni);
+    rc = gnutls_server_name_set(state->session,
+        GNUTLS_NAME_DNS, state->tlsp->sni, sz);
+    exim_gnutls_err_check(US"gnutls_server_name_set");
+    }
+  }
+else if (state->tls_sni)
+  DEBUG(D_tls) debug_printf("*** PROBABLY A BUG *** " \
+      "have an SNI set for a client [%s]\n", state->tls_sni);
+
+/* This is the priority string support,
+http://www.gnutls.org/manual/html_node/Priority-Strings.html
+and replaces gnutls_require_kx, gnutls_require_mac & gnutls_require_protocols.
+This was backwards incompatible, but means Exim no longer needs to track
+all algorithms and provide string forms for them. */
+
+want_default_priorities = TRUE;
+
+if (state->tls_require_ciphers && *state->tls_require_ciphers)
+  {
+  if (!expand_check_tlsvar(tls_require_ciphers))
+    return DEFER;
+  if (state->exp_tls_require_ciphers && *state->exp_tls_require_ciphers)
+    {
+    DEBUG(D_tls) debug_printf("GnuTLS session cipher/priority \"%s\"\n",
+        state->exp_tls_require_ciphers);
+
+    rc = gnutls_priority_init(&state->priority_cache,
+        CS state->exp_tls_require_ciphers, &errpos);
+    want_default_priorities = FALSE;
+    p = state->exp_tls_require_ciphers;
+    }
+  }
+if (want_default_priorities)
+  {
+  DEBUG(D_tls)
+    debug_printf("GnuTLS using default session cipher/priority \"%s\"\n",
+        exim_default_gnutls_priority);
+  rc = gnutls_priority_init(&state->priority_cache,
+      exim_default_gnutls_priority, &errpos);
+  p = US exim_default_gnutls_priority;
+  }
+
+exim_gnutls_err_check(string_sprintf(
+      "gnutls_priority_init(%s) failed at offset %ld, \"%.6s..\"",
+      p, errpos - CS p, errpos));
+
+rc = gnutls_priority_set(state->session, state->priority_cache);
+exim_gnutls_err_check(US"gnutls_priority_set");
+
+gnutls_db_set_cache_expiration(state->session, ssl_session_timeout);
+
+/* Reduce security in favour of increased compatibility, if the admin
+decides to make that trade-off. */
+if (gnutls_compat_mode)
+  {
+#if LIBGNUTLS_VERSION_NUMBER >= 0x020104
+  DEBUG(D_tls) debug_printf("lowering GnuTLS security, compatibility mode\n");
+  gnutls_session_enable_compatibility_mode(state->session);
+#else
+  DEBUG(D_tls) debug_printf("Unable to set gnutls_compat_mode - GnuTLS version too old\n");
+#endif
+  }
+
+*caller_state = state;
+return OK;
+}
+
+
+
+/*************************************************
+*            Extract peer information            *
+*************************************************/
+
+/* Called from both server and client code.
+Only this is allowed to set state->peerdn and state->have_set_peerdn
+and we use that to detect double-calls.
+
+NOTE: the state blocks last while the TLS connection is up, which is fine
+for logging in the server side, but for the client side, we log after teardown
+in src/deliver.c.  While the session is up, we can twist about states and
+repoint tls_* globals, but those variables used for logging or other variable
+expansion that happens _after_ delivery need to have a longer life-time.
+
+So for those, we get the data from POOL_PERM; the re-invoke guard keeps us from
+doing this more than once per generation of a state context.  We set them in
+the state context, and repoint tls_* to them.  After the state goes away, the
+tls_* copies of the pointers remain valid and client delivery logging is happy.
+
+tls_certificate_verified is a BOOL, so the tls_peerdn and tls_cipher issues
+don't apply.
+
+Arguments:
+  state           exim_gnutls_state_st *
+
+Returns:          OK/DEFER/FAIL
+*/
+
+static int
+peer_status(exim_gnutls_state_st *state)
+{
+uschar cipherbuf[256];
+const gnutls_datum *cert_list;
+int old_pool, rc;
+unsigned int cert_list_size = 0;
+gnutls_protocol_t protocol;
+gnutls_cipher_algorithm_t cipher;
+gnutls_kx_algorithm_t kx;
+gnutls_mac_algorithm_t mac;
+gnutls_certificate_type_t ct;
+gnutls_x509_crt_t crt;
+uschar *p, *dn_buf;
+size_t sz;
+
+if (state->have_set_peerdn)
+  return OK;
+state->have_set_peerdn = TRUE;
+
+state->peerdn = NULL;
+
+/* tls_cipher */
+cipher = gnutls_cipher_get(state->session);
+protocol = gnutls_protocol_get_version(state->session);
+mac = gnutls_mac_get(state->session);
+kx = gnutls_kx_get(state->session);
+
+string_format(cipherbuf, sizeof(cipherbuf),
+    "%s:%s:%d",
+    gnutls_protocol_get_name(protocol),
+    gnutls_cipher_suite_get_name(kx, cipher, mac),
+    (int) gnutls_cipher_get_key_size(cipher) * 8);
+
+/* I don't see a way that spaces could occur, in the current GnuTLS
+code base, but it was a concern in the old code and perhaps older GnuTLS
+releases did return "TLS 1.0"; play it safe, just in case. */
+for (p = cipherbuf; *p != '\0'; ++p)
+  if (isspace(*p))
+    *p = '-';
+old_pool = store_pool;
+store_pool = POOL_PERM;
+state->ciphersuite = string_copy(cipherbuf);
+store_pool = old_pool;
+state->tlsp->cipher = state->ciphersuite;
+
+/* tls_peerdn */
+cert_list = gnutls_certificate_get_peers(state->session, &cert_list_size);
+
+if (cert_list == NULL || cert_list_size == 0)
+  {
+  DEBUG(D_tls) debug_printf("TLS: no certificate from peer (%p & %d)\n",
+      cert_list, cert_list_size);
+  if (state->verify_requirement >= VERIFY_REQUIRED)
+    return tls_error(US"certificate verification failed",
+        "no certificate received from peer", state->host);
+  return OK;
+  }
+
+ct = gnutls_certificate_type_get(state->session);
+if (ct != GNUTLS_CRT_X509)
+  {
+  const char *ctn = gnutls_certificate_type_get_name(ct);
+  DEBUG(D_tls)
+    debug_printf("TLS: peer cert not X.509 but instead \"%s\"\n", ctn);
+  if (state->verify_requirement >= VERIFY_REQUIRED)
+    return tls_error(US"certificate verification not possible, unhandled type",
+        ctn, state->host);
+  return OK;
+  }
+
+#define exim_gnutls_peer_err(Label) \
+  do { \
+    if (rc != GNUTLS_E_SUCCESS) \
+      { \
+      DEBUG(D_tls) debug_printf("TLS: peer cert problem: %s: %s\n", \
+       (Label), gnutls_strerror(rc)); \
+      if (state->verify_requirement >= VERIFY_REQUIRED) \
+       return tls_error((Label), gnutls_strerror(rc), state->host); \
+      return OK; \
+      } \
+    } while (0)
+
+rc = import_cert(&cert_list[0], &crt);
+exim_gnutls_peer_err(US"cert 0");
+
+state->tlsp->peercert = state->peercert = crt;
+
+sz = 0;
+rc = gnutls_x509_crt_get_dn(crt, NULL, &sz);
+if (rc != GNUTLS_E_SHORT_MEMORY_BUFFER)
+  {
+  exim_gnutls_peer_err(US"getting size for cert DN failed");
+  return FAIL; /* should not happen */
+  }
+dn_buf = store_get_perm(sz);
+rc = gnutls_x509_crt_get_dn(crt, CS dn_buf, &sz);
+exim_gnutls_peer_err(US"failed to extract certificate DN [gnutls_x509_crt_get_dn(cert 0)]");
+
+state->peerdn = dn_buf;
+
+return OK;
+#undef exim_gnutls_peer_err
+}
+
+
+
+
+/*************************************************
+*            Verify peer certificate             *
+*************************************************/
+
+/* Called from both server and client code.
+*Should* be using a callback registered with
+gnutls_certificate_set_verify_function() to fail the handshake if we dislike
+the peer information, but that's too new for some OSes.
+
+Arguments:
+  state           exim_gnutls_state_st *
+  error           where to put an error message
+
+Returns:
+  FALSE     if the session should be rejected
+  TRUE      if the cert is okay or we just don't care
+*/
+
+static BOOL
+verify_certificate(exim_gnutls_state_st *state, const char **error)
+{
+int rc;
+unsigned int verify;
+
+*error = NULL;
+
+if ((rc = peer_status(state)) != OK)
+  {
+  verify = GNUTLS_CERT_INVALID;
+  *error = "certificate not supplied";
+  }
+else
+  rc = gnutls_certificate_verify_peers2(state->session, &verify);
+
+/* Handle the result of verification. INVALID seems to be set as well
+as REVOKED, but leave the test for both. */
+
+if (rc < 0 ||
+    verify & (GNUTLS_CERT_INVALID|GNUTLS_CERT_REVOKED)
+   )
+  {
+  state->peer_cert_verified = FALSE;
+  if (!*error)
+    *error = verify & GNUTLS_CERT_REVOKED
+      ? "certificate revoked" : "certificate invalid";
+
+  DEBUG(D_tls)
+    debug_printf("TLS certificate verification failed (%s): peerdn=\"%s\"\n",
+        *error, state->peerdn ? state->peerdn : US"<unset>");
+
+  if (state->verify_requirement >= VERIFY_REQUIRED)
+    {
+    gnutls_alert_send(state->session,
+      GNUTLS_AL_FATAL, GNUTLS_A_BAD_CERTIFICATE);
+    return FALSE;
+    }
+  DEBUG(D_tls)
+    debug_printf("TLS verify failure overridden (host in tls_try_verify_hosts)\n");
+  }
+
+else
+  {
+#ifdef EXPERIMENTAL_CERTNAMES
+  if (state->verify_requirement == VERIFY_WITHHOST)
+    {
+    int sep = 0;
+    uschar * list = state->exp_tls_verify_cert_hostnames;
+    uschar * name;
+    while (name = string_nextinlist(&list, &sep, NULL, 0))
+      if (gnutls_x509_crt_check_hostname(state->tlsp->peercert, CS name))
+       break;
+    if (!name)
+      {
+      DEBUG(D_tls)
+       debug_printf("TLS certificate verification failed: cert name mismatch\n");
+      gnutls_alert_send(state->session,
+       GNUTLS_AL_FATAL, GNUTLS_A_BAD_CERTIFICATE);
+      return FALSE;
+      }
+    }
+#endif
+  state->peer_cert_verified = TRUE;
+  DEBUG(D_tls) debug_printf("TLS certificate verified: peerdn=\"%s\"\n",
+      state->peerdn ? state->peerdn : US"<unset>");
+  }
+
+state->tlsp->peerdn = state->peerdn;
+
+return TRUE;
+}
+
+
+
+
+/* ------------------------------------------------------------------------ */
+/* Callbacks */
+
+/* Logging function which can be registered with
+ *   gnutls_global_set_log_function()
+ *   gnutls_global_set_log_level() 0..9
+ */
+#if EXIM_GNUTLS_LIBRARY_LOG_LEVEL >= 0
+static void
+exim_gnutls_logger_cb(int level, const char *message)
+{
+  size_t len = strlen(message);
+  if (len < 1)
+    {
+    DEBUG(D_tls) debug_printf("GnuTLS<%d> empty debug message\n", level);
+    return;
+    }
+  DEBUG(D_tls) debug_printf("GnuTLS<%d>: %s%s", level, message,
+      message[len-1] == '\n' ? "" : "\n");
+}
+#endif
+
+
+/* Called after client hello, should handle SNI work.
+This will always set tls_sni (state->received_sni) if available,
+and may trigger presenting different certificates,
+if state->trigger_sni_changes is TRUE.
+
+Should be registered with
+  gnutls_handshake_set_post_client_hello_function()
+
+"This callback must return 0 on success or a gnutls error code to terminate the
+handshake.".
+
+For inability to get SNI information, we return 0.
+We only return non-zero if re-setup failed.
+Only used for server-side TLS.
+*/
+
+static int
+exim_sni_handling_cb(gnutls_session_t session)
+{
+char sni_name[MAX_HOST_LEN];
+size_t data_len = MAX_HOST_LEN;
+exim_gnutls_state_st *state = &state_server;
+unsigned int sni_type;
+int rc, old_pool;
+
+rc = gnutls_server_name_get(session, sni_name, &data_len, &sni_type, 0);
+if (rc != GNUTLS_E_SUCCESS)
+  {
+  DEBUG(D_tls) {
+    if (rc == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE)
+      debug_printf("TLS: no SNI presented in handshake.\n");
+    else
+      debug_printf("TLS failure: gnutls_server_name_get(): %s [%d]\n",
+        gnutls_strerror(rc), rc);
+  };
+  return 0;
+  }
+
+if (sni_type != GNUTLS_NAME_DNS)
+  {
+  DEBUG(D_tls) debug_printf("TLS: ignoring SNI of unhandled type %u\n", sni_type);
+  return 0;
+  }
+
+/* We now have a UTF-8 string in sni_name */
+old_pool = store_pool;
+store_pool = POOL_PERM;
+state->received_sni = string_copyn(US sni_name, data_len);
+store_pool = old_pool;
+
+/* We set this one now so that variable expansions below will work */
+state->tlsp->sni = state->received_sni;
+
+DEBUG(D_tls) debug_printf("Received TLS SNI \"%s\"%s\n", sni_name,
+    state->trigger_sni_changes ? "" : " (unused for certificate selection)");
+
+if (!state->trigger_sni_changes)
+  return 0;
+
+rc = tls_expand_session_files(state);
+if (rc != OK)
+  {
+  /* If the setup of certs/etc failed before handshake, TLS would not have
+  been offered.  The best we can do now is abort. */
+  return GNUTLS_E_APPLICATION_ERROR_MIN;
+  }
+
+rc = tls_set_remaining_x509(state);
+if (rc != OK) return GNUTLS_E_APPLICATION_ERROR_MIN;
+
+return 0;
+}
+
+
+
+#ifndef DISABLE_OCSP
+
+static int
+server_ocsp_stapling_cb(gnutls_session_t session, void * ptr,
+  gnutls_datum_t * ocsp_response)
+{
+int ret;
+
+if ((ret = gnutls_load_file(ptr, ocsp_response)) < 0)
+  {
+  DEBUG(D_tls) debug_printf("Failed to load ocsp stapling file %s\n",
+                             (char *)ptr);
+  tls_in.ocsp = OCSP_NOT_RESP;
+  return GNUTLS_E_NO_CERTIFICATE_STATUS;
+  }
+
+tls_in.ocsp = OCSP_VFY_NOT_TRIED;
+return 0;
+}
+
+#endif
+
+
+
+
+
+/* ------------------------------------------------------------------------ */
+/* Exported functions */
+
+
+
+
+/*************************************************
+*       Start a TLS session in a server          *
+*************************************************/
+
+/* This is called when Exim is running as a server, after having received
+the STARTTLS command. It must respond to that command, and then negotiate
+a TLS session.
+
+Arguments:
+  require_ciphers  list of allowed ciphers or NULL
+
+Returns:           OK on success
+                   DEFER for errors before the start of the negotiation
+                   FAIL for errors during the negotation; the server can't
+                     continue running.
+*/
+
+int
+tls_server_start(const uschar *require_ciphers)
+{
+int rc;
+const char *error;
+exim_gnutls_state_st *state = NULL;
+
+/* Check for previous activation */
+if (tls_in.active >= 0)
+  {
+  tls_error(US"STARTTLS received after TLS started", "", NULL);
+  smtp_printf("554 Already in TLS\r\n");
+  return FAIL;
+  }
+
+/* Initialize the library. If it fails, it will already have logged the error
+and sent an SMTP response. */
+
+DEBUG(D_tls) debug_printf("initialising GnuTLS as a server\n");
+
+rc = tls_init(NULL, tls_certificate, tls_privatekey,
+    NULL, tls_verify_certificates, tls_crl,
+    require_ciphers, &state);
+if (rc != OK) return rc;
+
+/* If this is a host for which certificate verification is mandatory or
+optional, set up appropriately. */
+
+if (verify_check_host(&tls_verify_hosts) == OK)
+  {
+  DEBUG(D_tls)
+    debug_printf("TLS: a client certificate will be required.\n");
+  state->verify_requirement = VERIFY_REQUIRED;
+  gnutls_certificate_server_set_request(state->session, GNUTLS_CERT_REQUIRE);
+  }
+else if (verify_check_host(&tls_try_verify_hosts) == OK)
+  {
+  DEBUG(D_tls)
+    debug_printf("TLS: a client certificate will be requested but not required.\n");
+  state->verify_requirement = VERIFY_OPTIONAL;
+  gnutls_certificate_server_set_request(state->session, GNUTLS_CERT_REQUEST);
+  }
+else
+  {
+  DEBUG(D_tls)
+    debug_printf("TLS: a client certificate will not be requested.\n");
+  state->verify_requirement = VERIFY_NONE;
+  gnutls_certificate_server_set_request(state->session, GNUTLS_CERT_IGNORE);
+  }
+
+/* Register SNI handling; always, even if not in tls_certificate, so that the
+expansion variable $tls_sni is always available. */
+
+gnutls_handshake_set_post_client_hello_function(state->session,
+    exim_sni_handling_cb);
+
+/* Set context and tell client to go ahead, except in the case of TLS startup
+on connection, where outputting anything now upsets the clients and tends to
+make them disconnect. We need to have an explicit fflush() here, to force out
+the response. Other smtp_printf() calls do not need it, because in non-TLS
+mode, the fflush() happens when smtp_getc() is called. */
+
+if (!state->tlsp->on_connect)
+  {
+  smtp_printf("220 TLS go ahead\r\n");
+  fflush(smtp_out);
+  }
+
+/* Now negotiate the TLS session. We put our own timer on it, since it seems
+that the GnuTLS library doesn't. */
+
+gnutls_transport_set_ptr2(state->session,
+    (gnutls_transport_ptr)(long) fileno(smtp_in),
+    (gnutls_transport_ptr)(long) fileno(smtp_out));
+state->fd_in = fileno(smtp_in);
+state->fd_out = fileno(smtp_out);
+
+sigalrm_seen = FALSE;
+if (smtp_receive_timeout > 0) alarm(smtp_receive_timeout);
+do
+  {
+  rc = gnutls_handshake(state->session);
+  } while ((rc == GNUTLS_E_AGAIN) ||
+      (rc == GNUTLS_E_INTERRUPTED && !sigalrm_seen));
+alarm(0);
+
+if (rc != GNUTLS_E_SUCCESS)
+  {
+  tls_error(US"gnutls_handshake",
+      sigalrm_seen ? "timed out" : gnutls_strerror(rc), NULL);
+  /* It seems that, except in the case of a timeout, we have to close the
+  connection right here; otherwise if the other end is running OpenSSL it hangs
+  until the server times out. */
+
+  if (!sigalrm_seen)
+    {
+    (void)fclose(smtp_out);
+    (void)fclose(smtp_in);
+    }
+
+  return FAIL;
+  }
+
+DEBUG(D_tls) debug_printf("gnutls_handshake was successful\n");
+
+/* Verify after the fact */
+
+if (  state->verify_requirement != VERIFY_NONE
+   && !verify_certificate(state, &error))
+  {
+  if (state->verify_requirement != VERIFY_OPTIONAL)
+    {
+    tls_error(US"certificate verification failed", error, NULL);
+    return FAIL;
+    }
+  DEBUG(D_tls)
+    debug_printf("TLS: continuing on only because verification was optional, after: %s\n",
+       error);
+  }
+
+/* Figure out peer DN, and if authenticated, etc. */
+
+rc = peer_status(state);
+if (rc != OK) return rc;
+
+/* Sets various Exim expansion variables; always safe within server */
+
+extract_exim_vars_from_tls_state(state);
+
+/* TLS has been set up. Adjust the input functions to read via TLS,
+and initialize appropriately. */
+
+state->xfer_buffer = store_malloc(ssl_xfer_buffer_size);
+
+receive_getc = tls_getc;
+receive_ungetc = tls_ungetc;
+receive_feof = tls_feof;
+receive_ferror = tls_ferror;
+receive_smtp_buffered = tls_smtp_buffered;
+
+return OK;
+}
+
+
+
+
+/*************************************************
+*    Start a TLS session in a client             *
+*************************************************/
+
+/* Called from the smtp transport after STARTTLS has been accepted.
+
+Arguments:
+  fd                the fd of the connection
+  host              connected host (for messages)
+  addr              the first address (not used)
+  ob                smtp transport options
+
+Returns:            OK/DEFER/FAIL (because using common functions),
+                    but for a client, DEFER and FAIL have the same meaning
+*/
+
+int
+tls_client_start(int fd, host_item *host,
+    address_item *addr ARG_UNUSED,
+    void *v_ob)
+{
+smtp_transport_options_block *ob = v_ob;
+int rc;
+const char *error;
+exim_gnutls_state_st *state = NULL;
+#ifndef DISABLE_OCSP
+BOOL require_ocsp = verify_check_this_host(&ob->hosts_require_ocsp,
+  NULL, host->name, host->address, NULL) == OK;
+BOOL request_ocsp = require_ocsp ? TRUE
+  : verify_check_this_host(&ob->hosts_request_ocsp,
+      NULL, host->name, host->address, NULL) == OK;
+#endif
+
+DEBUG(D_tls) debug_printf("initialising GnuTLS as a client on fd %d\n", fd);
+
+if ((rc = tls_init(host, ob->tls_certificate, ob->tls_privatekey,
+    ob->tls_sni, ob->tls_verify_certificates, ob->tls_crl,
+    ob->tls_require_ciphers, &state)) != OK)
+  return rc;
+
+  {
+  int dh_min_bits = ob->tls_dh_min_bits;
+  if (dh_min_bits < EXIM_CLIENT_DH_MIN_MIN_BITS)
+    {
+    DEBUG(D_tls)
+      debug_printf("WARNING: tls_dh_min_bits far too low,"
+                   " clamping %d up to %d\n",
+         dh_min_bits, EXIM_CLIENT_DH_MIN_MIN_BITS);
+    dh_min_bits = EXIM_CLIENT_DH_MIN_MIN_BITS;
+    }
+
+  DEBUG(D_tls) debug_printf("Setting D-H prime minimum"
+                   " acceptable bits to %d\n",
+      dh_min_bits);
+  gnutls_dh_set_prime_bits(state->session, dh_min_bits);
+  }
+
+/* Stick to the old behaviour for compatibility if tls_verify_certificates is 
+set but both tls_verify_hosts and tls_try_verify_hosts are unset. Check only
+the specified host patterns if one of them is defined */
+
+if ((  state->exp_tls_verify_certificates
+    && !ob->tls_verify_hosts
+    && !ob->tls_try_verify_hosts
+    )
+    ||
+    verify_check_host(&ob->tls_verify_hosts) == OK
+   )
+  {
+#ifdef EXPERIMENTAL_CERTNAMES
+  if (ob->tls_verify_cert_hostnames)
+    {
+    DEBUG(D_tls)
+      debug_printf("TLS: server cert incl. hostname verification required.\n");
+    state->verify_requirement = VERIFY_WITHHOST;
+    if (!expand_check(ob->tls_verify_cert_hostnames,
+                     US"tls_verify_cert_hostnames",
+                     &state->exp_tls_verify_cert_hostnames))
+      return FAIL;
+    if (state->exp_tls_verify_cert_hostnames)
+      DEBUG(D_tls) debug_printf("Cert hostname to check: \"%s\"\n",
+                     state->exp_tls_verify_cert_hostnames);
+    }
+  else
+#endif
+    {
+    DEBUG(D_tls)
+      debug_printf("TLS: server certificate verification required.\n");
+    state->verify_requirement = VERIFY_REQUIRED;
+    }
+  gnutls_certificate_server_set_request(state->session, GNUTLS_CERT_REQUIRE);
+  }
+else if (verify_check_host(&ob->tls_try_verify_hosts) == OK)
+  {
+  DEBUG(D_tls)
+    debug_printf("TLS: server certificate verification optional.\n");
+  state->verify_requirement = VERIFY_OPTIONAL;
+  gnutls_certificate_server_set_request(state->session, GNUTLS_CERT_REQUEST);
+  }
+else
+  {
+  DEBUG(D_tls)
+    debug_printf("TLS: server certificate verification not required.\n");
+  state->verify_requirement = VERIFY_NONE;
+  gnutls_certificate_server_set_request(state->session, GNUTLS_CERT_IGNORE);
+  }
+
+#ifndef DISABLE_OCSP
+                       /* supported since GnuTLS 3.1.3 */
+if (request_ocsp)
+  {
+  DEBUG(D_tls) debug_printf("TLS: will request OCSP stapling\n");
+  if ((rc = gnutls_ocsp_status_request_enable_client(state->session,
+                   NULL, 0, NULL)) != OK)
+    return tls_error(US"cert-status-req",
+                   gnutls_strerror(rc), state->host);
+  tls_out.ocsp = OCSP_NOT_RESP;
+  }
+#endif
+
+gnutls_transport_set_ptr(state->session, (gnutls_transport_ptr)(long) fd);
+state->fd_in = fd;
+state->fd_out = fd;
+
+DEBUG(D_tls) debug_printf("about to gnutls_handshake\n");
+/* There doesn't seem to be a built-in timeout on connection. */
+
+sigalrm_seen = FALSE;
+alarm(ob->command_timeout);
+do
+  {
+  rc = gnutls_handshake(state->session);
+  } while ((rc == GNUTLS_E_AGAIN) ||
+      (rc == GNUTLS_E_INTERRUPTED && !sigalrm_seen));
+alarm(0);
+
+if (rc != GNUTLS_E_SUCCESS)
+  return tls_error(US"gnutls_handshake",
+      sigalrm_seen ? "timed out" : gnutls_strerror(rc), state->host);
+
+DEBUG(D_tls) debug_printf("gnutls_handshake was successful\n");
+
+/* Verify late */
+
+if (state->verify_requirement != VERIFY_NONE &&
+    !verify_certificate(state, &error))
+  return tls_error(US"certificate verification failed", error, state->host);
+
+#ifndef DISABLE_OCSP
+if (require_ocsp)
+  {
+  DEBUG(D_tls)
+    {
+    gnutls_datum_t stapling;
+    gnutls_ocsp_resp_t resp;
+    gnutls_datum_t printed;
+    if (  (rc= gnutls_ocsp_status_request_get(state->session, &stapling)) == 0
+       && (rc= gnutls_ocsp_resp_init(&resp)) == 0
+       && (rc= gnutls_ocsp_resp_import(resp, &stapling)) == 0
+       && (rc= gnutls_ocsp_resp_print(resp, GNUTLS_OCSP_PRINT_FULL, &printed)) == 0
+       )
+      {
+      debug_printf("%.4096s", printed.data);
+      gnutls_free(printed.data);
+      }
+    else
+      (void) tls_error(US"ocsp decode", gnutls_strerror(rc), state->host);
+    }
+
+  if (gnutls_ocsp_status_request_is_checked(state->session, 0) == 0)
+    {
+    tls_out.ocsp = OCSP_FAILED;
+    return tls_error(US"certificate status check failed", NULL, state->host);
+    }
+  DEBUG(D_tls) debug_printf("Passed OCSP checking\n");
+  tls_out.ocsp = OCSP_VFIED;
+  }
+#endif
+
+/* Figure out peer DN, and if authenticated, etc. */
+
+if ((rc = peer_status(state)) != OK)
+  return rc;
+
+/* Sets various Exim expansion variables; may need to adjust for ACL callouts */
+
+extract_exim_vars_from_tls_state(state);
+
+return OK;
+}
+
+
+
+
+/*************************************************
+*         Close down a TLS session               *
+*************************************************/
+
+/* This is also called from within a delivery subprocess forked from the
+daemon, to shut down the TLS library, without actually doing a shutdown (which
+would tamper with the TLS session in the parent process).
+
+Arguments:   TRUE if gnutls_bye is to be called
+Returns:     nothing
+*/
+
+void
+tls_close(BOOL is_server, BOOL shutdown)
+{
+exim_gnutls_state_st *state = is_server ? &state_server : &state_client;
+
+if (!state->tlsp || state->tlsp->active < 0) return;  /* TLS was not active */
+
+if (shutdown)
+  {
+  DEBUG(D_tls) debug_printf("tls_close(): shutting down TLS\n");
+  gnutls_bye(state->session, GNUTLS_SHUT_WR);
+  }
+
+gnutls_deinit(state->session);
+
+state->tlsp->active = -1;
+memcpy(state, &exim_gnutls_state_init, sizeof(exim_gnutls_state_init));
+
+if ((state_server.session == NULL) && (state_client.session == NULL))
+  {
+  gnutls_global_deinit();
+  exim_gnutls_base_init_done = FALSE;
+  }
+
+}
+
+
+
+
+/*************************************************
+*            TLS version of getc                 *
+*************************************************/
+
+/* This gets the next byte from the TLS input buffer. If the buffer is empty,
+it refills the buffer via the GnuTLS reading function.
+Only used by the server-side TLS.
+
+This feeds DKIM and should be used for all message-body reads.
+
+Arguments:  none
+Returns:    the next character or EOF
+*/
+
+int
+tls_getc(void)
+{
+exim_gnutls_state_st *state = &state_server;
+if (state->xfer_buffer_lwm >= state->xfer_buffer_hwm)
+  {
+  ssize_t inbytes;
+
+  DEBUG(D_tls) debug_printf("Calling gnutls_record_recv(%p, %p, %u)\n",
+    state->session, state->xfer_buffer, ssl_xfer_buffer_size);
+
+  if (smtp_receive_timeout > 0) alarm(smtp_receive_timeout);
+  inbytes = gnutls_record_recv(state->session, state->xfer_buffer,
+    ssl_xfer_buffer_size);
+  alarm(0);
+
+  /* A zero-byte return appears to mean that the TLS session has been
+     closed down, not that the socket itself has been closed down. Revert to
+     non-TLS handling. */
+
+  if (inbytes == 0)
+    {
+    DEBUG(D_tls) debug_printf("Got TLS_EOF\n");
+
+    receive_getc = smtp_getc;
+    receive_ungetc = smtp_ungetc;
+    receive_feof = smtp_feof;
+    receive_ferror = smtp_ferror;
+    receive_smtp_buffered = smtp_buffered;
+
+    gnutls_deinit(state->session);
+    state->session = NULL;
+    state->tlsp->active = -1;
+    state->tlsp->bits = 0;
+    state->tlsp->certificate_verified = FALSE;
+    tls_channelbinding_b64 = NULL;
+    state->tlsp->cipher = NULL;
+    state->tlsp->peercert = NULL;
+    state->tlsp->peerdn = NULL;
+
+    return smtp_getc();
+    }
+
+  /* Handle genuine errors */
+
+  else if (inbytes < 0)
+    {
+    record_io_error(state, (int) inbytes, US"recv", NULL);
+    state->xfer_error = 1;
+    return EOF;
+    }
+#ifndef DISABLE_DKIM
+  dkim_exim_verify_feed(state->xfer_buffer, inbytes);
+#endif
+  state->xfer_buffer_hwm = (int) inbytes;
+  state->xfer_buffer_lwm = 0;
+  }
+
+/* Something in the buffer; return next uschar */
+
+return state->xfer_buffer[state->xfer_buffer_lwm++];
+}
+
+
+
+
+/*************************************************
+*          Read bytes from TLS channel           *
+*************************************************/
+
+/* This does not feed DKIM, so if the caller uses this for reading message body,
+then the caller must feed DKIM.
+
+Arguments:
+  buff      buffer of data
+  len       size of buffer
+
+Returns:    the number of bytes read
+            -1 after a failed read
+*/
+
+int
+tls_read(BOOL is_server, uschar *buff, size_t len)
+{
+exim_gnutls_state_st *state = is_server ? &state_server : &state_client;
+ssize_t inbytes;
+
+if (len > INT_MAX)
+  len = INT_MAX;
+
+if (state->xfer_buffer_lwm < state->xfer_buffer_hwm)
+  DEBUG(D_tls)
+    debug_printf("*** PROBABLY A BUG *** " \
+        "tls_read() called with data in the tls_getc() buffer, %d ignored\n",
+        state->xfer_buffer_hwm - state->xfer_buffer_lwm);
+
+DEBUG(D_tls)
+  debug_printf("Calling gnutls_record_recv(%p, %p, " SIZE_T_FMT ")\n",
+      state->session, buff, len);
+
+inbytes = gnutls_record_recv(state->session, buff, len);
+if (inbytes > 0) return inbytes;
+if (inbytes == 0)
+  {
+  DEBUG(D_tls) debug_printf("Got TLS_EOF\n");
+  }
+else record_io_error(state, (int)inbytes, US"recv", NULL);
+
+return -1;
+}
+
+
+
+
+/*************************************************
+*         Write bytes down TLS channel           *
+*************************************************/
+
+/*
+Arguments:
+  is_server channel specifier
+  buff      buffer of data
+  len       number of bytes
+
+Returns:    the number of bytes after a successful write,
+            -1 after a failed write
+*/
+
+int
+tls_write(BOOL is_server, const uschar *buff, size_t len)
+{
+ssize_t outbytes;
+size_t left = len;
+exim_gnutls_state_st *state = is_server ? &state_server : &state_client;
+
+DEBUG(D_tls) debug_printf("tls_do_write(%p, " SIZE_T_FMT ")\n", buff, left);
+while (left > 0)
+  {
+  DEBUG(D_tls) debug_printf("gnutls_record_send(SSL, %p, " SIZE_T_FMT ")\n",
+      buff, left);
+  outbytes = gnutls_record_send(state->session, buff, left);
+
+  DEBUG(D_tls) debug_printf("outbytes=" SSIZE_T_FMT "\n", outbytes);
+  if (outbytes < 0)
+    {
+    record_io_error(state, outbytes, US"send", NULL);
+    return -1;
+    }
+  if (outbytes == 0)
+    {
+    record_io_error(state, 0, US"send", US"TLS channel closed on write");
+    return -1;
+    }
+
+  left -= outbytes;
+  buff += outbytes;
+  }
+
+if (len > INT_MAX)
+  {
+  DEBUG(D_tls)
+    debug_printf("Whoops!  Wrote more bytes (" SIZE_T_FMT ") than INT_MAX\n",
+        len);
+  len = INT_MAX;
+  }
+
+return (int) len;
+}
+
+
+
+
+/*************************************************
+*            Random number generation            *
+*************************************************/
+
+/* Pseudo-random number generation.  The result is not expected to be
+cryptographically strong but not so weak that someone will shoot themselves
+in the foot using it as a nonce in input in some email header scheme or
+whatever weirdness they'll twist this into.  The result should handle fork()
+and avoid repeating sequences.  OpenSSL handles that for us.
+
+Arguments:
+  max       range maximum
+Returns     a random number in range [0, max-1]
+*/
+
+#ifdef HAVE_GNUTLS_RND
+int
+vaguely_random_number(int max)
+{
+unsigned int r;
+int i, needed_len;
+uschar *p;
+uschar smallbuf[sizeof(r)];
+
+if (max <= 1)
+  return 0;
+
+needed_len = sizeof(r);
+/* Don't take 8 times more entropy than needed if int is 8 octets and we were
+ * asked for a number less than 10. */
+for (r = max, i = 0; r; ++i)
+  r >>= 1;
+i = (i + 7) / 8;
+if (i < needed_len)
+  needed_len = i;
+
+i = gnutls_rnd(GNUTLS_RND_NONCE, smallbuf, needed_len);
+if (i < 0)
+  {
+  DEBUG(D_all) debug_printf("gnutls_rnd() failed, using fallback.\n");
+  return vaguely_random_number_fallback(max);
+  }
+r = 0;
+for (p = smallbuf; needed_len; --needed_len, ++p)
+  {
+  r *= 256;
+  r += *p;
+  }
+
+/* We don't particularly care about weighted results; if someone wants
+ * smooth distribution and cares enough then they should submit a patch then. */
+return r % max;
+}
+#else /* HAVE_GNUTLS_RND */
+int
+vaguely_random_number(int max)
+{
+  return vaguely_random_number_fallback(max);
+}
+#endif /* HAVE_GNUTLS_RND */
+
+
+
+
+/*************************************************
+*  Let tls_require_ciphers be checked at startup *
+*************************************************/
+
+/* The tls_require_ciphers option, if set, must be something which the
+library can parse.
+
+Returns:     NULL on success, or error message
+*/
+
+uschar *
+tls_validate_require_cipher(void)
+{
+int rc;
+uschar *expciphers = NULL;
+gnutls_priority_t priority_cache;
+const char *errpos;
+
+#define validate_check_rc(Label) do { \
+  if (rc != GNUTLS_E_SUCCESS) { if (exim_gnutls_base_init_done) gnutls_global_deinit(); \
+  return string_sprintf("%s failed: %s", (Label), gnutls_strerror(rc)); } } while (0)
+#define return_deinit(Label) do { gnutls_global_deinit(); return (Label); } while (0)
+
+if (exim_gnutls_base_init_done)
+  log_write(0, LOG_MAIN|LOG_PANIC,
+      "already initialised GnuTLS, Exim developer bug");
+
+#ifdef HAVE_GNUTLS_PKCS11
+if (!gnutls_allow_auto_pkcs11)
+  {
+  rc = gnutls_pkcs11_init(GNUTLS_PKCS11_FLAG_MANUAL, NULL);
+  validate_check_rc(US"gnutls_pkcs11_init");
+  }
+#endif
+rc = gnutls_global_init();
+validate_check_rc(US"gnutls_global_init()");
+exim_gnutls_base_init_done = TRUE;
+
+if (!(tls_require_ciphers && *tls_require_ciphers))
+  return_deinit(NULL);
+
+if (!expand_check(tls_require_ciphers, US"tls_require_ciphers", &expciphers))
+  return_deinit(US"failed to expand tls_require_ciphers");
+
+if (!(expciphers && *expciphers))
+  return_deinit(NULL);
+
+DEBUG(D_tls)
+  debug_printf("tls_require_ciphers expands to \"%s\"\n", expciphers);
+
+rc = gnutls_priority_init(&priority_cache, CS expciphers, &errpos);
+validate_check_rc(string_sprintf(
+      "gnutls_priority_init(%s) failed at offset %ld, \"%.8s..\"",
+      expciphers, errpos - CS expciphers, errpos));
+
+#undef return_deinit
+#undef validate_check_rc
+gnutls_global_deinit();
+
+return NULL;
+}
+
+
+
+
+/*************************************************
+*         Report the library versions.           *
+*************************************************/
+
+/* See a description in tls-openssl.c for an explanation of why this exists.
+
+Arguments:   a FILE* to print the results to
+Returns:     nothing
+*/
+
+void
+tls_version_report(FILE *f)
+{
+fprintf(f, "Library version: GnuTLS: Compile: %s\n"
+           "                         Runtime: %s\n",
+           LIBGNUTLS_VERSION,
+           gnutls_check_version(NULL));
+}
+
+/* vi: aw ai sw=2
+*/
+/* End of tls-gnu.c */
diff --git a/src/tls-openssl.c b/src/tls-openssl.c
new file mode 100644 (file)
index 0000000..18994ea
--- /dev/null
@@ -0,0 +1,2349 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2014 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Portions Copyright (c) The OpenSSL Project 1999 */
+
+/* This module provides the TLS (aka SSL) support for Exim using the OpenSSL
+library. It is #included into the tls.c file when that library is used. The
+code herein is based on a patch that was originally contributed by Steve
+Haslam. It was adapted from stunnel, a GPL program by Michal Trojnara.
+
+No cryptographic code is included in Exim. All this module does is to call
+functions from the OpenSSL library. */
+
+
+/* Heading stuff */
+
+#include <openssl/lhash.h>
+#include <openssl/ssl.h>
+#include <openssl/err.h>
+#include <openssl/rand.h>
+#ifndef DISABLE_OCSP
+# include <openssl/ocsp.h>
+#endif
+
+#ifndef DISABLE_OCSP
+# define EXIM_OCSP_SKEW_SECONDS (300L)
+# define EXIM_OCSP_MAX_AGE (-1L)
+#endif
+
+#if OPENSSL_VERSION_NUMBER >= 0x0090806fL && !defined(OPENSSL_NO_TLSEXT)
+# define EXIM_HAVE_OPENSSL_TLSEXT
+#endif
+
+#if !defined(EXIM_HAVE_OPENSSL_TLSEXT) && !defined(DISABLE_OCSP)
+# warning "OpenSSL library version too old; define DISABLE_OCSP in Makefile"
+# define DISABLE_OCSP
+#endif
+
+/* Structure for collecting random data for seeding. */
+
+typedef struct randstuff {
+  struct timeval tv;
+  pid_t          p;
+} randstuff;
+
+/* Local static variables */
+
+static BOOL client_verify_callback_called = FALSE;
+static BOOL server_verify_callback_called = FALSE;
+static const uschar *sid_ctx = US"exim";
+
+/* We have three different contexts to care about.
+
+Simple case: client, `client_ctx`
+ As a client, we can be doing a callout or cut-through delivery while receiving
+ a message.  So we have a client context, which should have options initialised
+ from the SMTP Transport.
+
+Server:
+ There are two cases: with and without ServerNameIndication from the client.
+ Given TLS SNI, we can be using different keys, certs and various other
+ configuration settings, because they're re-expanded with $tls_sni set.  This
+ allows vhosting with TLS.  This SNI is sent in the handshake.
+ A client might not send SNI, so we need a fallback, and an initial setup too.
+ So as a server, we start out using `server_ctx`.
+ If SNI is sent by the client, then we as server, mid-negotiation, try to clone
+ `server_sni` from `server_ctx` and then initialise settings by re-expanding
+ configuration.
+*/
+
+static SSL_CTX *client_ctx = NULL;
+static SSL_CTX *server_ctx = NULL;
+static SSL     *client_ssl = NULL;
+static SSL     *server_ssl = NULL;
+
+#ifdef EXIM_HAVE_OPENSSL_TLSEXT
+static SSL_CTX *server_sni = NULL;
+#endif
+
+static char ssl_errstring[256];
+
+static int  ssl_session_timeout = 200;
+static BOOL client_verify_optional = FALSE;
+static BOOL server_verify_optional = FALSE;
+
+static BOOL reexpand_tls_files_for_sni = FALSE;
+
+
+typedef struct tls_ext_ctx_cb {
+  uschar *certificate;
+  uschar *privatekey;
+#ifndef DISABLE_OCSP
+  BOOL is_server;
+  union {
+    struct {
+      uschar        *file;
+      uschar        *file_expanded;
+      OCSP_RESPONSE *response;
+    } server;
+    struct {
+      X509_STORE    *verify_store;     /* non-null if status requested */
+      BOOL         verify_required;
+    } client;
+  } u_ocsp;
+#endif
+  uschar *dhparam;
+  /* these are cached from first expand */
+  uschar *server_cipher_list;
+  /* only passed down to tls_error: */
+  host_item *host;
+
+#ifdef EXPERIMENTAL_CERTNAMES
+  uschar * verify_cert_hostnames;
+#endif
+} tls_ext_ctx_cb;
+
+/* should figure out a cleanup of API to handle state preserved per
+implementation, for various reasons, which can be void * in the APIs.
+For now, we hack around it. */
+tls_ext_ctx_cb *client_static_cbinfo = NULL;
+tls_ext_ctx_cb *server_static_cbinfo = NULL;
+
+static int
+setup_certs(SSL_CTX *sctx, uschar *certs, uschar *crl, host_item *host, BOOL optional,
+    int (*cert_vfy_cb)(int, X509_STORE_CTX *) );
+
+/* Callbacks */
+#ifdef EXIM_HAVE_OPENSSL_TLSEXT
+static int tls_servername_cb(SSL *s, int *ad ARG_UNUSED, void *arg);
+#endif
+#ifndef DISABLE_OCSP
+static int tls_server_stapling_cb(SSL *s, void *arg);
+#endif
+
+
+/*************************************************
+*               Handle TLS error                 *
+*************************************************/
+
+/* Called from lots of places when errors occur before actually starting to do
+the TLS handshake, that is, while the session is still in clear. Always returns
+DEFER for a server and FAIL for a client so that most calls can use "return
+tls_error(...)" to do this processing and then give an appropriate return. A
+single function is used for both server and client, because it is called from
+some shared functions.
+
+Argument:
+  prefix    text to include in the logged error
+  host      NULL if setting up a server;
+            the connected host if setting up a client
+  msg       error message or NULL if we should ask OpenSSL
+
+Returns:    OK/DEFER/FAIL
+*/
+
+static int
+tls_error(uschar *prefix, host_item *host, uschar *msg)
+{
+if (msg == NULL)
+  {
+  ERR_error_string(ERR_get_error(), ssl_errstring);
+  msg = (uschar *)ssl_errstring;
+  }
+
+if (host == NULL)
+  {
+  uschar *conn_info = smtp_get_connection_info();
+  if (Ustrncmp(conn_info, US"SMTP ", 5) == 0)
+    conn_info += 5;
+  log_write(0, LOG_MAIN, "TLS error on %s (%s): %s",
+    conn_info, prefix, msg);
+  return DEFER;
+  }
+else
+  {
+  log_write(0, LOG_MAIN, "TLS error on connection to %s [%s] (%s): %s",
+    host->name, host->address, prefix, msg);
+  return FAIL;
+  }
+}
+
+
+
+/*************************************************
+*        Callback to generate RSA key            *
+*************************************************/
+
+/*
+Arguments:
+  s          SSL connection
+  export     not used
+  keylength  keylength
+
+Returns:     pointer to generated key
+*/
+
+static RSA *
+rsa_callback(SSL *s, int export, int keylength)
+{
+RSA *rsa_key;
+export = export;     /* Shut picky compilers up */
+DEBUG(D_tls) debug_printf("Generating %d bit RSA key...\n", keylength);
+rsa_key = RSA_generate_key(keylength, RSA_F4, NULL, NULL);
+if (rsa_key == NULL)
+  {
+  ERR_error_string(ERR_get_error(), ssl_errstring);
+  log_write(0, LOG_MAIN|LOG_PANIC, "TLS error (RSA_generate_key): %s",
+    ssl_errstring);
+  return NULL;
+  }
+return rsa_key;
+}
+
+
+
+/* Extreme debug
+#ifndef DISABLE_OCSP
+void
+x509_store_dump_cert_s_names(X509_STORE * store)
+{
+STACK_OF(X509_OBJECT) * roots= store->objs;
+int i;
+static uschar name[256];
+
+for(i= 0; i<sk_X509_OBJECT_num(roots); i++)
+  {
+  X509_OBJECT * tmp_obj= sk_X509_OBJECT_value(roots, i);
+  if(tmp_obj->type == X509_LU_X509)
+    {
+    X509 * current_cert= tmp_obj->data.x509;
+    X509_NAME_oneline(X509_get_subject_name(current_cert), CS name, sizeof(name));
+    debug_printf(" %s\n", name);
+    }
+  }
+}
+#endif
+*/
+
+
+/*************************************************
+*        Callback for verification               *
+*************************************************/
+
+/* The SSL library does certificate verification if set up to do so. This
+callback has the current yes/no state is in "state". If verification succeeded,
+we set up the tls_peerdn string. If verification failed, what happens depends
+on whether the client is required to present a verifiable certificate or not.
+
+If verification is optional, we change the state to yes, but still log the
+verification error. For some reason (it really would help to have proper
+documentation of OpenSSL), this callback function then gets called again, this
+time with state = 1. In fact, that's useful, because we can set up the peerdn
+value, but we must take care not to set the private verified flag on the second
+time through.
+
+Note: this function is not called if the client fails to present a certificate
+when asked. We get here only if a certificate has been received. Handling of
+optional verification for this case is done when requesting SSL to verify, by
+setting SSL_VERIFY_FAIL_IF_NO_PEER_CERT in the non-optional case.
+
+Arguments:
+  state      current yes/no state as 1/0
+  x509ctx    certificate information.
+  client     TRUE for client startup, FALSE for server startup
+
+Returns:     1 if verified, 0 if not
+*/
+
+static int
+verify_callback(int state, X509_STORE_CTX *x509ctx,
+  tls_support *tlsp, BOOL *calledp, BOOL *optionalp)
+{
+X509 * cert = X509_STORE_CTX_get_current_cert(x509ctx);
+static uschar txt[256];
+
+X509_NAME_oneline(X509_get_subject_name(cert), CS txt, sizeof(txt));
+
+if (state == 0)
+  {
+  log_write(0, LOG_MAIN, "SSL verify error: depth=%d error=%s cert=%s",
+    X509_STORE_CTX_get_error_depth(x509ctx),
+    X509_verify_cert_error_string(X509_STORE_CTX_get_error(x509ctx)),
+    txt);
+  tlsp->certificate_verified = FALSE;
+  *calledp = TRUE;
+  if (!*optionalp)
+    {
+    tlsp->peercert = X509_dup(cert);
+    return 0;                      /* reject */
+    }
+  DEBUG(D_tls) debug_printf("SSL verify failure overridden (host in "
+    "tls_try_verify_hosts)\n");
+  }
+
+else if (X509_STORE_CTX_get_error_depth(x509ctx) != 0)
+  {
+  DEBUG(D_tls) debug_printf("SSL verify ok: depth=%d SN=%s\n",
+     X509_STORE_CTX_get_error_depth(x509ctx), txt);
+#ifndef DISABLE_OCSP
+  if (tlsp == &tls_out && client_static_cbinfo->u_ocsp.client.verify_store)
+    {  /* client, wanting stapling  */
+    /* Add the server cert's signing chain as the one
+    for the verification of the OCSP stapled information. */
+  
+    if (!X509_STORE_add_cert(client_static_cbinfo->u_ocsp.client.verify_store,
+                             cert))
+      ERR_clear_error();
+    }
+#endif
+  }
+else
+  {
+#ifdef EXPERIMENTAL_CERTNAMES
+  uschar * verify_cert_hostnames;
+#endif
+
+  tlsp->peerdn = txt;
+  tlsp->peercert = X509_dup(cert);
+
+#ifdef EXPERIMENTAL_CERTNAMES
+  if (  tlsp == &tls_out
+     && ((verify_cert_hostnames = client_static_cbinfo->verify_cert_hostnames)))
+       /* client, wanting hostname check */
+
+# if OPENSSL_VERSION_NUMBER >= 0x010100000L || OPENSSL_VERSION_NUMBER >= 0x010002000L
+#  ifndef X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS
+#   define X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS 0
+#  endif
+    {
+    int sep = 0;
+    uschar * list = verify_cert_hostnames;
+    uschar * name;
+    int rc;
+    while ((name = string_nextinlist(&list, &sep, NULL, 0)))
+      if ((rc = X509_check_host(cert, name, 0,
+                 X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS)))
+       {
+       if (rc < 0)
+         {
+         log_write(0, LOG_MAIN, "SSL verify error: internal error\n");
+         name = NULL;
+         }
+       break;
+       }
+    if (!name)
+      {
+      log_write(0, LOG_MAIN,
+       "SSL verify error: certificate name mismatch: \"%s\"\n", txt);
+      return 0;                                /* reject */
+      }
+    }
+# else
+    if (!tls_is_name_for_cert(verify_cert_hostnames, cert))
+      {
+      log_write(0, LOG_MAIN,
+       "SSL verify error: certificate name mismatch: \"%s\"\n", txt);
+      return 0;                                /* reject */
+      }
+# endif
+#endif
+
+  DEBUG(D_tls) debug_printf("SSL%s verify ok: depth=0 SN=%s\n",
+    *calledp ? "" : " authenticated", txt);
+  if (!*calledp) tlsp->certificate_verified = TRUE;
+  *calledp = TRUE;
+  }
+
+return 1;   /* accept */
+}
+
+static int
+verify_callback_client(int state, X509_STORE_CTX *x509ctx)
+{
+return verify_callback(state, x509ctx, &tls_out, &client_verify_callback_called, &client_verify_optional);
+}
+
+static int
+verify_callback_server(int state, X509_STORE_CTX *x509ctx)
+{
+return verify_callback(state, x509ctx, &tls_in, &server_verify_callback_called, &server_verify_optional);
+}
+
+
+
+/*************************************************
+*           Information callback                 *
+*************************************************/
+
+/* The SSL library functions call this from time to time to indicate what they
+are doing. We copy the string to the debugging output when TLS debugging has
+been requested.
+
+Arguments:
+  s         the SSL connection
+  where
+  ret
+
+Returns:    nothing
+*/
+
+static void
+info_callback(SSL *s, int where, int ret)
+{
+where = where;
+ret = ret;
+DEBUG(D_tls) debug_printf("SSL info: %s\n", SSL_state_string_long(s));
+}
+
+
+
+/*************************************************
+*                Initialize for DH               *
+*************************************************/
+
+/* If dhparam is set, expand it, and load up the parameters for DH encryption.
+
+Arguments:
+  dhparam   DH parameter file or fixed parameter identity string
+  host      connected host, if client; NULL if server
+
+Returns:    TRUE if OK (nothing to set up, or setup worked)
+*/
+
+static BOOL
+init_dh(SSL_CTX *sctx, uschar *dhparam, host_item *host)
+{
+BIO *bio;
+DH *dh;
+uschar *dhexpanded;
+const char *pem;
+
+if (!expand_check(dhparam, US"tls_dhparam", &dhexpanded))
+  return FALSE;
+
+if (!dhexpanded || !*dhexpanded)
+  bio = BIO_new_mem_buf(CS std_dh_prime_default(), -1);
+else if (dhexpanded[0] == '/')
+  {
+  if (!(bio = BIO_new_file(CS dhexpanded, "r")))
+    {
+    tls_error(string_sprintf("could not read dhparams file %s", dhexpanded),
+          host, US strerror(errno));
+    return FALSE;
+    }
+  }
+else
+  {
+  if (Ustrcmp(dhexpanded, "none") == 0)
+    {
+    DEBUG(D_tls) debug_printf("Requested no DH parameters.\n");
+    return TRUE;
+    }
+
+  if (!(pem = std_dh_prime_named(dhexpanded)))
+    {
+    tls_error(string_sprintf("Unknown standard DH prime \"%s\"", dhexpanded),
+        host, US strerror(errno));
+    return FALSE;
+    }
+  bio = BIO_new_mem_buf(CS pem, -1);
+  }
+
+if (!(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL)))
+  {
+  BIO_free(bio);
+  tls_error(string_sprintf("Could not read tls_dhparams \"%s\"", dhexpanded),
+      host, NULL);
+  return FALSE;
+  }
+
+/* Even if it is larger, we silently return success rather than cause things
+ * to fail out, so that a too-large DH will not knock out all TLS; it's a
+ * debatable choice. */
+if ((8*DH_size(dh)) > tls_dh_max_bits)
+  {
+  DEBUG(D_tls)
+    debug_printf("dhparams file %d bits, is > tls_dh_max_bits limit of %d",
+        8*DH_size(dh), tls_dh_max_bits);
+  }
+else
+  {
+  SSL_CTX_set_tmp_dh(sctx, dh);
+  DEBUG(D_tls)
+    debug_printf("Diffie-Hellman initialized from %s with %d-bit prime\n",
+      dhexpanded ? dhexpanded : US"default", 8*DH_size(dh));
+  }
+
+DH_free(dh);
+BIO_free(bio);
+
+return TRUE;
+}
+
+
+
+
+#ifndef DISABLE_OCSP
+/*************************************************
+*       Load OCSP information into state         *
+*************************************************/
+
+/* Called to load the server OCSP response from the given file into memory, once
+caller has determined this is needed.  Checks validity.  Debugs a message
+if invalid.
+
+ASSUMES: single response, for single cert.
+
+Arguments:
+  sctx            the SSL_CTX* to update
+  cbinfo          various parts of session state
+  expanded        the filename putatively holding an OCSP response
+
+*/
+
+static void
+ocsp_load_response(SSL_CTX *sctx, tls_ext_ctx_cb *cbinfo, const uschar *expanded)
+{
+BIO *bio;
+OCSP_RESPONSE *resp;
+OCSP_BASICRESP *basic_response;
+OCSP_SINGLERESP *single_response;
+ASN1_GENERALIZEDTIME *rev, *thisupd, *nextupd;
+X509_STORE *store;
+unsigned long verify_flags;
+int status, reason, i;
+
+cbinfo->u_ocsp.server.file_expanded = string_copy(expanded);
+if (cbinfo->u_ocsp.server.response)
+  {
+  OCSP_RESPONSE_free(cbinfo->u_ocsp.server.response);
+  cbinfo->u_ocsp.server.response = NULL;
+  }
+
+bio = BIO_new_file(CS cbinfo->u_ocsp.server.file_expanded, "rb");
+if (!bio)
+  {
+  DEBUG(D_tls) debug_printf("Failed to open OCSP response file \"%s\"\n",
+      cbinfo->u_ocsp.server.file_expanded);
+  return;
+  }
+
+resp = d2i_OCSP_RESPONSE_bio(bio, NULL);
+BIO_free(bio);
+if (!resp)
+  {
+  DEBUG(D_tls) debug_printf("Error reading OCSP response.\n");
+  return;
+  }
+
+status = OCSP_response_status(resp);
+if (status != OCSP_RESPONSE_STATUS_SUCCESSFUL)
+  {
+  DEBUG(D_tls) debug_printf("OCSP response not valid: %s (%d)\n",
+      OCSP_response_status_str(status), status);
+  goto bad;
+  }
+
+basic_response = OCSP_response_get1_basic(resp);
+if (!basic_response)
+  {
+  DEBUG(D_tls)
+    debug_printf("OCSP response parse error: unable to extract basic response.\n");
+  goto bad;
+  }
+
+store = SSL_CTX_get_cert_store(sctx);
+verify_flags = OCSP_NOVERIFY; /* check sigs, but not purpose */
+
+/* May need to expose ability to adjust those flags?
+OCSP_NOSIGS OCSP_NOVERIFY OCSP_NOCHAIN OCSP_NOCHECKS OCSP_NOEXPLICIT
+OCSP_TRUSTOTHER OCSP_NOINTERN */
+
+i = OCSP_basic_verify(basic_response, NULL, store, verify_flags);
+if (i <= 0)
+  {
+  DEBUG(D_tls) {
+    ERR_error_string(ERR_get_error(), ssl_errstring);
+    debug_printf("OCSP response verify failure: %s\n", US ssl_errstring);
+    }
+  goto bad;
+  }
+
+/* Here's the simplifying assumption: there's only one response, for the
+one certificate we use, and nothing for anything else in a chain.  If this
+proves false, we need to extract a cert id from our issued cert
+(tls_certificate) and use that for OCSP_resp_find_status() (which finds the
+right cert in the stack and then calls OCSP_single_get0_status()).
+
+I'm hoping to avoid reworking a bunch more of how we handle state here. */
+single_response = OCSP_resp_get0(basic_response, 0);
+if (!single_response)
+  {
+  DEBUG(D_tls)
+    debug_printf("Unable to get first response from OCSP basic response.\n");
+  goto bad;
+  }
+
+status = OCSP_single_get0_status(single_response, &reason, &rev, &thisupd, &nextupd);
+if (status != V_OCSP_CERTSTATUS_GOOD)
+  {
+  DEBUG(D_tls) debug_printf("OCSP response bad cert status: %s (%d) %s (%d)\n",
+      OCSP_cert_status_str(status), status,
+      OCSP_crl_reason_str(reason), reason);
+  goto bad;
+  }
+
+if (!OCSP_check_validity(thisupd, nextupd, EXIM_OCSP_SKEW_SECONDS, EXIM_OCSP_MAX_AGE))
+  {
+  DEBUG(D_tls) debug_printf("OCSP status invalid times.\n");
+  goto bad;
+  }
+
+supply_response:
+  cbinfo->u_ocsp.server.response = resp;
+return;
+
+bad:
+  if (running_in_test_harness)
+    {
+    extern char ** environ;
+    uschar ** p;
+    for (p = USS environ; *p != NULL; p++)
+      if (Ustrncmp(*p, "EXIM_TESTHARNESS_DISABLE_OCSPVALIDITYCHECK", 42) == 0)
+       {
+       DEBUG(D_tls) debug_printf("Supplying known bad OCSP response\n");
+       goto supply_response;
+       }
+    }
+return;
+}
+#endif /*!DISABLE_OCSP*/
+
+
+
+
+/*************************************************
+*        Expand key and cert file specs          *
+*************************************************/
+
+/* Called once during tls_init and possibly again during TLS setup, for a
+new context, if Server Name Indication was used and tls_sni was seen in
+the certificate string.
+
+Arguments:
+  sctx            the SSL_CTX* to update
+  cbinfo          various parts of session state
+
+Returns:          OK/DEFER/FAIL
+*/
+
+static int
+tls_expand_session_files(SSL_CTX *sctx, tls_ext_ctx_cb *cbinfo)
+{
+uschar *expanded;
+
+if (cbinfo->certificate == NULL)
+  return OK;
+
+if (Ustrstr(cbinfo->certificate, US"tls_sni") ||
+    Ustrstr(cbinfo->certificate, US"tls_in_sni") ||
+    Ustrstr(cbinfo->certificate, US"tls_out_sni")
+   )
+  reexpand_tls_files_for_sni = TRUE;
+
+if (!expand_check(cbinfo->certificate, US"tls_certificate", &expanded))
+  return DEFER;
+
+if (expanded != NULL)
+  {
+  DEBUG(D_tls) debug_printf("tls_certificate file %s\n", expanded);
+  if (!SSL_CTX_use_certificate_chain_file(sctx, CS expanded))
+    return tls_error(string_sprintf(
+      "SSL_CTX_use_certificate_chain_file file=%s", expanded),
+        cbinfo->host, NULL);
+  }
+
+if (cbinfo->privatekey != NULL &&
+    !expand_check(cbinfo->privatekey, US"tls_privatekey", &expanded))
+  return DEFER;
+
+/* If expansion was forced to fail, key_expanded will be NULL. If the result
+of the expansion is an empty string, ignore it also, and assume the private
+key is in the same file as the certificate. */
+
+if (expanded != NULL && *expanded != 0)
+  {
+  DEBUG(D_tls) debug_printf("tls_privatekey file %s\n", expanded);
+  if (!SSL_CTX_use_PrivateKey_file(sctx, CS expanded, SSL_FILETYPE_PEM))
+    return tls_error(string_sprintf(
+      "SSL_CTX_use_PrivateKey_file file=%s", expanded), cbinfo->host, NULL);
+  }
+
+#ifndef DISABLE_OCSP
+if (cbinfo->is_server &&  cbinfo->u_ocsp.server.file != NULL)
+  {
+  if (!expand_check(cbinfo->u_ocsp.server.file, US"tls_ocsp_file", &expanded))
+    return DEFER;
+
+  if (expanded != NULL && *expanded != 0)
+    {
+    DEBUG(D_tls) debug_printf("tls_ocsp_file %s\n", expanded);
+    if (cbinfo->u_ocsp.server.file_expanded &&
+        (Ustrcmp(expanded, cbinfo->u_ocsp.server.file_expanded) == 0))
+      {
+      DEBUG(D_tls)
+        debug_printf("tls_ocsp_file value unchanged, using existing values.\n");
+      } else {
+        ocsp_load_response(sctx, cbinfo, expanded);
+      }
+    }
+  }
+#endif
+
+return OK;
+}
+
+
+
+
+/*************************************************
+*            Callback to handle SNI              *
+*************************************************/
+
+/* Called when acting as server during the TLS session setup if a Server Name
+Indication extension was sent by the client.
+
+API documentation is OpenSSL s_server.c implementation.
+
+Arguments:
+  s               SSL* of the current session
+  ad              unknown (part of OpenSSL API) (unused)
+  arg             Callback of "our" registered data
+
+Returns:          SSL_TLSEXT_ERR_{OK,ALERT_WARNING,ALERT_FATAL,NOACK}
+*/
+
+#ifdef EXIM_HAVE_OPENSSL_TLSEXT
+static int
+tls_servername_cb(SSL *s, int *ad ARG_UNUSED, void *arg)
+{
+const char *servername = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name);
+tls_ext_ctx_cb *cbinfo = (tls_ext_ctx_cb *) arg;
+int rc;
+int old_pool = store_pool;
+
+if (!servername)
+  return SSL_TLSEXT_ERR_OK;
+
+DEBUG(D_tls) debug_printf("Received TLS SNI \"%s\"%s\n", servername,
+    reexpand_tls_files_for_sni ? "" : " (unused for certificate selection)");
+
+/* Make the extension value available for expansion */
+store_pool = POOL_PERM;
+tls_in.sni = string_copy(US servername);
+store_pool = old_pool;
+
+if (!reexpand_tls_files_for_sni)
+  return SSL_TLSEXT_ERR_OK;
+
+/* Can't find an SSL_CTX_clone() or equivalent, so we do it manually;
+not confident that memcpy wouldn't break some internal reference counting.
+Especially since there's a references struct member, which would be off. */
+
+if (!(server_sni = SSL_CTX_new(SSLv23_server_method())))
+  {
+  ERR_error_string(ERR_get_error(), ssl_errstring);
+  DEBUG(D_tls) debug_printf("SSL_CTX_new() failed: %s\n", ssl_errstring);
+  return SSL_TLSEXT_ERR_NOACK;
+  }
+
+/* Not sure how many of these are actually needed, since SSL object
+already exists.  Might even need this selfsame callback, for reneg? */
+
+SSL_CTX_set_info_callback(server_sni, SSL_CTX_get_info_callback(server_ctx));
+SSL_CTX_set_mode(server_sni, SSL_CTX_get_mode(server_ctx));
+SSL_CTX_set_options(server_sni, SSL_CTX_get_options(server_ctx));
+SSL_CTX_set_timeout(server_sni, SSL_CTX_get_timeout(server_ctx));
+SSL_CTX_set_tlsext_servername_callback(server_sni, tls_servername_cb);
+SSL_CTX_set_tlsext_servername_arg(server_sni, cbinfo);
+if (cbinfo->server_cipher_list)
+  SSL_CTX_set_cipher_list(server_sni, CS cbinfo->server_cipher_list);
+#ifndef DISABLE_OCSP
+if (cbinfo->u_ocsp.server.file)
+  {
+  SSL_CTX_set_tlsext_status_cb(server_sni, tls_server_stapling_cb);
+  SSL_CTX_set_tlsext_status_arg(server_sni, cbinfo);
+  }
+#endif
+
+rc = setup_certs(server_sni, tls_verify_certificates, tls_crl, NULL, FALSE, verify_callback_server);
+if (rc != OK) return SSL_TLSEXT_ERR_NOACK;
+
+/* do this after setup_certs, because this can require the certs for verifying
+OCSP information. */
+rc = tls_expand_session_files(server_sni, cbinfo);
+if (rc != OK) return SSL_TLSEXT_ERR_NOACK;
+
+if (!init_dh(server_sni, cbinfo->dhparam, NULL))
+  return SSL_TLSEXT_ERR_NOACK;
+
+DEBUG(D_tls) debug_printf("Switching SSL context.\n");
+SSL_set_SSL_CTX(s, server_sni);
+
+return SSL_TLSEXT_ERR_OK;
+}
+#endif /* EXIM_HAVE_OPENSSL_TLSEXT */
+
+
+
+
+#ifndef DISABLE_OCSP
+
+/*************************************************
+*        Callback to handle OCSP Stapling        *
+*************************************************/
+
+/* Called when acting as server during the TLS session setup if the client
+requests OCSP information with a Certificate Status Request.
+
+Documentation via openssl s_server.c and the Apache patch from the OpenSSL
+project.
+
+*/
+
+static int
+tls_server_stapling_cb(SSL *s, void *arg)
+{
+const tls_ext_ctx_cb *cbinfo = (tls_ext_ctx_cb *) arg;
+uschar *response_der;
+int response_der_len;
+
+DEBUG(D_tls)
+  debug_printf("Received TLS status request (OCSP stapling); %s response.",
+    cbinfo->u_ocsp.server.response ? "have" : "lack");
+
+tls_in.ocsp = OCSP_NOT_RESP;
+if (!cbinfo->u_ocsp.server.response)
+  return SSL_TLSEXT_ERR_NOACK;
+
+response_der = NULL;
+response_der_len = i2d_OCSP_RESPONSE(cbinfo->u_ocsp.server.response,
+                     &response_der);
+if (response_der_len <= 0)
+  return SSL_TLSEXT_ERR_NOACK;
+
+SSL_set_tlsext_status_ocsp_resp(server_ssl, response_der, response_der_len);
+tls_in.ocsp = OCSP_VFIED;
+return SSL_TLSEXT_ERR_OK;
+}
+
+
+static void
+time_print(BIO * bp, const char * str, ASN1_GENERALIZEDTIME * time)
+{
+BIO_printf(bp, "\t%s: ", str);
+ASN1_GENERALIZEDTIME_print(bp, time);
+BIO_puts(bp, "\n");
+}
+
+static int
+tls_client_stapling_cb(SSL *s, void *arg)
+{
+tls_ext_ctx_cb * cbinfo = arg;
+const unsigned char * p;
+int len;
+OCSP_RESPONSE * rsp;
+OCSP_BASICRESP * bs;
+int i;
+
+DEBUG(D_tls) debug_printf("Received TLS status response (OCSP stapling):");
+len = SSL_get_tlsext_status_ocsp_resp(s, &p);
+if(!p)
+ {
+  /* Expect this when we requested ocsp but got none */
+  if (  cbinfo->u_ocsp.client.verify_required
+     && log_extra_selector & LX_tls_cipher)
+    log_write(0, LOG_MAIN, "Received TLS status callback, null content");
+  else
+    DEBUG(D_tls) debug_printf(" null\n");
+  return cbinfo->u_ocsp.client.verify_required ? 0 : 1;
+ }
+
+if(!(rsp = d2i_OCSP_RESPONSE(NULL, &p, len)))
+ {
+  tls_out.ocsp = OCSP_FAILED;
+  if (log_extra_selector & LX_tls_cipher)
+    log_write(0, LOG_MAIN, "Received TLS status response, parse error");
+  else
+    DEBUG(D_tls) debug_printf(" parse error\n");
+  return 0;
+ }
+
+if(!(bs = OCSP_response_get1_basic(rsp)))
+  {
+  tls_out.ocsp = OCSP_FAILED;
+  if (log_extra_selector & LX_tls_cipher)
+    log_write(0, LOG_MAIN, "Received TLS status response, error parsing response");
+  else
+    DEBUG(D_tls) debug_printf(" error parsing response\n");
+  OCSP_RESPONSE_free(rsp);
+  return 0;
+  }
+
+/* We'd check the nonce here if we'd put one in the request. */
+/* However that would defeat cacheability on the server so we don't. */
+
+/* This section of code reworked from OpenSSL apps source;
+   The OpenSSL Project retains copyright:
+   Copyright (c) 1999 The OpenSSL Project.  All rights reserved.
+*/
+  {
+    BIO * bp = NULL;
+    int status, reason;
+    ASN1_GENERALIZEDTIME *rev, *thisupd, *nextupd;
+
+    DEBUG(D_tls) bp = BIO_new_fp(stderr, BIO_NOCLOSE);
+
+    /*OCSP_RESPONSE_print(bp, rsp, 0);   extreme debug: stapling content */
+
+    /* Use the chain that verified the server cert to verify the stapled info */
+    /* DEBUG(D_tls) x509_store_dump_cert_s_names(cbinfo->u_ocsp.client.verify_store); */
+
+    if ((i = OCSP_basic_verify(bs, NULL,
+             cbinfo->u_ocsp.client.verify_store, 0)) <= 0)
+      {
+      tls_out.ocsp = OCSP_FAILED;
+      BIO_printf(bp, "OCSP response verify failure\n");
+      ERR_print_errors(bp);
+      i = cbinfo->u_ocsp.client.verify_required ? 0 : 1;
+      goto out;
+      }
+
+    BIO_printf(bp, "OCSP response well-formed and signed OK\n");
+
+      {
+      STACK_OF(OCSP_SINGLERESP) * sresp = bs->tbsResponseData->responses;
+      OCSP_SINGLERESP * single;
+
+      if (sk_OCSP_SINGLERESP_num(sresp) != 1)
+        {
+       tls_out.ocsp = OCSP_FAILED;
+        log_write(0, LOG_MAIN, "OCSP stapling "
+           "with multiple responses not handled");
+       i = cbinfo->u_ocsp.client.verify_required ? 0 : 1;
+        goto out;
+        }
+      single = OCSP_resp_get0(bs, 0);
+      status = OCSP_single_get0_status(single, &reason, &rev,
+                 &thisupd, &nextupd);
+      }
+
+    DEBUG(D_tls) time_print(bp, "This OCSP Update", thisupd);
+    DEBUG(D_tls) if(nextupd) time_print(bp, "Next OCSP Update", nextupd);
+    if (!OCSP_check_validity(thisupd, nextupd,
+         EXIM_OCSP_SKEW_SECONDS, EXIM_OCSP_MAX_AGE))
+      {
+      tls_out.ocsp = OCSP_FAILED;
+      DEBUG(D_tls) ERR_print_errors(bp);
+      log_write(0, LOG_MAIN, "Server OSCP dates invalid");
+      i = cbinfo->u_ocsp.client.verify_required ? 0 : 1;
+      }
+    else
+      {
+      DEBUG(D_tls) BIO_printf(bp, "Certificate status: %s\n",
+                   OCSP_cert_status_str(status));
+      switch(status)
+       {
+       case V_OCSP_CERTSTATUS_GOOD:
+         tls_out.ocsp = OCSP_VFIED;
+         i = 1;
+         break;
+       case V_OCSP_CERTSTATUS_REVOKED:
+         tls_out.ocsp = OCSP_FAILED;
+         log_write(0, LOG_MAIN, "Server certificate revoked%s%s",
+             reason != -1 ? "; reason: " : "",
+             reason != -1 ? OCSP_crl_reason_str(reason) : "");
+         DEBUG(D_tls) time_print(bp, "Revocation Time", rev);
+         i = cbinfo->u_ocsp.client.verify_required ? 0 : 1;
+         break;
+       default:
+         tls_out.ocsp = OCSP_FAILED;
+         log_write(0, LOG_MAIN,
+             "Server certificate status unknown, in OCSP stapling");
+         i = cbinfo->u_ocsp.client.verify_required ? 0 : 1;
+         break;
+       }
+      }
+  out:
+    BIO_free(bp);
+  }
+
+OCSP_RESPONSE_free(rsp);
+return i;
+}
+#endif /*!DISABLE_OCSP*/
+
+
+
+/*************************************************
+*            Initialize for TLS                  *
+*************************************************/
+
+/* Called from both server and client code, to do preliminary initialization
+of the library.  We allocate and return a context structure.
+
+Arguments:
+  host            connected host, if client; NULL if server
+  dhparam         DH parameter file
+  certificate     certificate file
+  privatekey      private key
+  ocsp_file       file of stapling info (server); flag for require ocsp (client)
+  addr            address if client; NULL if server (for some randomness)
+  cbp             place to put allocated context
+
+Returns:          OK/DEFER/FAIL
+*/
+
+static int
+tls_init(SSL_CTX **ctxp, host_item *host, uschar *dhparam, uschar *certificate,
+  uschar *privatekey,
+#ifndef DISABLE_OCSP
+  uschar *ocsp_file,
+#endif
+  address_item *addr, tls_ext_ctx_cb ** cbp)
+{
+long init_options;
+int rc;
+BOOL okay;
+tls_ext_ctx_cb *cbinfo;
+
+cbinfo = store_malloc(sizeof(tls_ext_ctx_cb));
+cbinfo->certificate = certificate;
+cbinfo->privatekey = privatekey;
+#ifndef DISABLE_OCSP
+if ((cbinfo->is_server = host==NULL))
+  {
+  cbinfo->u_ocsp.server.file = ocsp_file;
+  cbinfo->u_ocsp.server.file_expanded = NULL;
+  cbinfo->u_ocsp.server.response = NULL;
+  }
+else
+  cbinfo->u_ocsp.client.verify_store = NULL;
+#endif
+cbinfo->dhparam = dhparam;
+cbinfo->server_cipher_list = NULL;
+cbinfo->host = host;
+
+SSL_load_error_strings();          /* basic set up */
+OpenSSL_add_ssl_algorithms();
+
+#if (OPENSSL_VERSION_NUMBER >= 0x0090800fL) && !defined(OPENSSL_NO_SHA256)
+/* SHA256 is becoming ever more popular. This makes sure it gets added to the
+list of available digests. */
+EVP_add_digest(EVP_sha256());
+#endif
+
+/* Create a context.
+The OpenSSL docs in 1.0.1b have not been updated to clarify TLS variant
+negotiation in the different methods; as far as I can tell, the only
+*_{server,client}_method which allows negotiation is SSLv23, which exists even
+when OpenSSL is built without SSLv2 support.
+By disabling with openssl_options, we can let admins re-enable with the
+existing knob. */
+
+*ctxp = SSL_CTX_new((host == NULL)?
+  SSLv23_server_method() : SSLv23_client_method());
+
+if (*ctxp == NULL) return tls_error(US"SSL_CTX_new", host, NULL);
+
+/* It turns out that we need to seed the random number generator this early in
+order to get the full complement of ciphers to work. It took me roughly a day
+of work to discover this by experiment.
+
+On systems that have /dev/urandom, SSL may automatically seed itself from
+there. Otherwise, we have to make something up as best we can. Double check
+afterwards. */
+
+if (!RAND_status())
+  {
+  randstuff r;
+  gettimeofday(&r.tv, NULL);
+  r.p = getpid();
+
+  RAND_seed((uschar *)(&r), sizeof(r));
+  RAND_seed((uschar *)big_buffer, big_buffer_size);
+  if (addr != NULL) RAND_seed((uschar *)addr, sizeof(addr));
+
+  if (!RAND_status())
+    return tls_error(US"RAND_status", host,
+      US"unable to seed random number generator");
+  }
+
+/* Set up the information callback, which outputs if debugging is at a suitable
+level. */
+
+SSL_CTX_set_info_callback(*ctxp, (void (*)())info_callback);
+
+/* Automatically re-try reads/writes after renegotiation. */
+(void) SSL_CTX_set_mode(*ctxp, SSL_MODE_AUTO_RETRY);
+
+/* Apply administrator-supplied work-arounds.
+Historically we applied just one requested option,
+SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS, but when bug 994 requested a second, we
+moved to an administrator-controlled list of options to specify and
+grandfathered in the first one as the default value for "openssl_options".
+
+No OpenSSL version number checks: the options we accept depend upon the
+availability of the option value macros from OpenSSL.  */
+
+okay = tls_openssl_options_parse(openssl_options, &init_options);
+if (!okay)
+  return tls_error(US"openssl_options parsing failed", host, NULL);
+
+if (init_options)
+  {
+  DEBUG(D_tls) debug_printf("setting SSL CTX options: %#lx\n", init_options);
+  if (!(SSL_CTX_set_options(*ctxp, init_options)))
+    return tls_error(string_sprintf(
+          "SSL_CTX_set_option(%#lx)", init_options), host, NULL);
+  }
+else
+  DEBUG(D_tls) debug_printf("no SSL CTX options to set\n");
+
+/* Initialize with DH parameters if supplied */
+
+if (!init_dh(*ctxp, dhparam, host)) return DEFER;
+
+/* Set up certificate and key (and perhaps OCSP info) */
+
+rc = tls_expand_session_files(*ctxp, cbinfo);
+if (rc != OK) return rc;
+
+/* If we need to handle SNI, do so */
+#ifdef EXIM_HAVE_OPENSSL_TLSEXT
+if (host == NULL)              /* server */
+  {
+# ifndef DISABLE_OCSP
+  /* We check u_ocsp.server.file, not server.response, because we care about if
+  the option exists, not what the current expansion might be, as SNI might
+  change the certificate and OCSP file in use between now and the time the
+  callback is invoked. */
+  if (cbinfo->u_ocsp.server.file)
+    {
+    SSL_CTX_set_tlsext_status_cb(server_ctx, tls_server_stapling_cb);
+    SSL_CTX_set_tlsext_status_arg(server_ctx, cbinfo);
+    }
+# endif
+  /* We always do this, so that $tls_sni is available even if not used in
+  tls_certificate */
+  SSL_CTX_set_tlsext_servername_callback(*ctxp, tls_servername_cb);
+  SSL_CTX_set_tlsext_servername_arg(*ctxp, cbinfo);
+  }
+# ifndef DISABLE_OCSP
+else                   /* client */
+  if(ocsp_file)                /* wanting stapling */
+    {
+    if (!(cbinfo->u_ocsp.client.verify_store = X509_STORE_new()))
+      {
+      DEBUG(D_tls) debug_printf("failed to create store for stapling verify\n");
+      return FAIL;
+      }
+    SSL_CTX_set_tlsext_status_cb(*ctxp, tls_client_stapling_cb);
+    SSL_CTX_set_tlsext_status_arg(*ctxp, cbinfo);
+    }
+# endif
+#endif
+
+#ifdef EXPERIMENTAL_CERTNAMES
+cbinfo->verify_cert_hostnames = NULL;
+#endif
+
+/* Set up the RSA callback */
+
+SSL_CTX_set_tmp_rsa_callback(*ctxp, rsa_callback);
+
+/* Finally, set the timeout, and we are done */
+
+SSL_CTX_set_timeout(*ctxp, ssl_session_timeout);
+DEBUG(D_tls) debug_printf("Initialized TLS\n");
+
+*cbp = cbinfo;
+
+return OK;
+}
+
+
+
+
+/*************************************************
+*           Get name of cipher in use            *
+*************************************************/
+
+/*
+Argument:   pointer to an SSL structure for the connection
+            buffer to use for answer
+            size of buffer
+           pointer to number of bits for cipher
+Returns:    nothing
+*/
+
+static void
+construct_cipher_name(SSL *ssl, uschar *cipherbuf, int bsize, int *bits)
+{
+/* With OpenSSL 1.0.0a, this needs to be const but the documentation doesn't
+yet reflect that.  It should be a safe change anyway, even 0.9.8 versions have
+the accessor functions use const in the prototype. */
+const SSL_CIPHER *c;
+const uschar *ver;
+
+ver = (const uschar *)SSL_get_version(ssl);
+
+c = (const SSL_CIPHER *) SSL_get_current_cipher(ssl);
+SSL_CIPHER_get_bits(c, bits);
+
+string_format(cipherbuf, bsize, "%s:%s:%u", ver,
+  SSL_CIPHER_get_name(c), *bits);
+
+DEBUG(D_tls) debug_printf("Cipher: %s\n", cipherbuf);
+}
+
+
+
+
+
+/*************************************************
+*        Set up for verifying certificates       *
+*************************************************/
+
+/* Called by both client and server startup
+
+Arguments:
+  sctx          SSL_CTX* to initialise
+  certs         certs file or NULL
+  crl           CRL file or NULL
+  host          NULL in a server; the remote host in a client
+  optional      TRUE if called from a server for a host in tls_try_verify_hosts;
+                otherwise passed as FALSE
+  cert_vfy_cb  Callback function for certificate verification
+
+Returns:        OK/DEFER/FAIL
+*/
+
+static int
+setup_certs(SSL_CTX *sctx, uschar *certs, uschar *crl, host_item *host, BOOL optional,
+    int (*cert_vfy_cb)(int, X509_STORE_CTX *) )
+{
+uschar *expcerts, *expcrl;
+
+if (!expand_check(certs, US"tls_verify_certificates", &expcerts))
+  return DEFER;
+
+if (expcerts != NULL && *expcerts != '\0')
+  {
+  struct stat statbuf;
+  if (!SSL_CTX_set_default_verify_paths(sctx))
+    return tls_error(US"SSL_CTX_set_default_verify_paths", host, NULL);
+
+  if (Ustat(expcerts, &statbuf) < 0)
+    {
+    log_write(0, LOG_MAIN|LOG_PANIC,
+      "failed to stat %s for certificates", expcerts);
+    return DEFER;
+    }
+  else
+    {
+    uschar *file, *dir;
+    if ((statbuf.st_mode & S_IFMT) == S_IFDIR)
+      { file = NULL; dir = expcerts; }
+    else
+      { file = expcerts; dir = NULL; }
+
+    /* If a certificate file is empty, the next function fails with an
+    unhelpful error message. If we skip it, we get the correct behaviour (no
+    certificates are recognized, but the error message is still misleading (it
+    says no certificate was supplied.) But this is better. */
+
+    if ((file == NULL || statbuf.st_size > 0) &&
+          !SSL_CTX_load_verify_locations(sctx, CS file, CS dir))
+      return tls_error(US"SSL_CTX_load_verify_locations", host, NULL);
+
+    if (file != NULL)
+      {
+      SSL_CTX_set_client_CA_list(sctx, SSL_load_client_CA_file(CS file));
+      }
+    }
+
+  /* Handle a certificate revocation list. */
+
+  #if OPENSSL_VERSION_NUMBER > 0x00907000L
+
+  /* This bit of code is now the version supplied by Lars Mainka. (I have
+   * merely reformatted it into the Exim code style.)
+
+   * "From here I changed the code to add support for multiple crl's
+   * in pem format in one file or to support hashed directory entries in
+   * pem format instead of a file. This method now uses the library function
+   * X509_STORE_load_locations to add the CRL location to the SSL context.
+   * OpenSSL will then handle the verify against CA certs and CRLs by
+   * itself in the verify callback." */
+
+  if (!expand_check(crl, US"tls_crl", &expcrl)) return DEFER;
+  if (expcrl != NULL && *expcrl != 0)
+    {
+    struct stat statbufcrl;
+    if (Ustat(expcrl, &statbufcrl) < 0)
+      {
+      log_write(0, LOG_MAIN|LOG_PANIC,
+        "failed to stat %s for certificates revocation lists", expcrl);
+      return DEFER;
+      }
+    else
+      {
+      /* is it a file or directory? */
+      uschar *file, *dir;
+      X509_STORE *cvstore = SSL_CTX_get_cert_store(sctx);
+      if ((statbufcrl.st_mode & S_IFMT) == S_IFDIR)
+        {
+        file = NULL;
+        dir = expcrl;
+        DEBUG(D_tls) debug_printf("SSL CRL value is a directory %s\n", dir);
+        }
+      else
+        {
+        file = expcrl;
+        dir = NULL;
+        DEBUG(D_tls) debug_printf("SSL CRL value is a file %s\n", file);
+        }
+      if (X509_STORE_load_locations(cvstore, CS file, CS dir) == 0)
+        return tls_error(US"X509_STORE_load_locations", host, NULL);
+
+      /* setting the flags to check against the complete crl chain */
+
+      X509_STORE_set_flags(cvstore,
+        X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL);
+      }
+    }
+
+  #endif  /* OPENSSL_VERSION_NUMBER > 0x00907000L */
+
+  /* If verification is optional, don't fail if no certificate */
+
+  SSL_CTX_set_verify(sctx,
+    SSL_VERIFY_PEER | (optional? 0 : SSL_VERIFY_FAIL_IF_NO_PEER_CERT),
+    cert_vfy_cb);
+  }
+
+return OK;
+}
+
+
+
+/*************************************************
+*       Start a TLS session in a server          *
+*************************************************/
+
+/* This is called when Exim is running as a server, after having received
+the STARTTLS command. It must respond to that command, and then negotiate
+a TLS session.
+
+Arguments:
+  require_ciphers   allowed ciphers
+
+Returns:            OK on success
+                    DEFER for errors before the start of the negotiation
+                    FAIL for errors during the negotation; the server can't
+                      continue running.
+*/
+
+int
+tls_server_start(const uschar *require_ciphers)
+{
+int rc;
+uschar *expciphers;
+tls_ext_ctx_cb *cbinfo;
+static uschar cipherbuf[256];
+
+/* Check for previous activation */
+
+if (tls_in.active >= 0)
+  {
+  tls_error(US"STARTTLS received after TLS started", NULL, US"");
+  smtp_printf("554 Already in TLS\r\n");
+  return FAIL;
+  }
+
+/* Initialize the SSL library. If it fails, it will already have logged
+the error. */
+
+rc = tls_init(&server_ctx, NULL, tls_dhparam, tls_certificate, tls_privatekey,
+#ifndef DISABLE_OCSP
+    tls_ocsp_file,
+#endif
+    NULL, &server_static_cbinfo);
+if (rc != OK) return rc;
+cbinfo = server_static_cbinfo;
+
+if (!expand_check(require_ciphers, US"tls_require_ciphers", &expciphers))
+  return FAIL;
+
+/* In OpenSSL, cipher components are separated by hyphens. In GnuTLS, they
+were historically separated by underscores. So that I can use either form in my
+tests, and also for general convenience, we turn underscores into hyphens here.
+*/
+
+if (expciphers != NULL)
+  {
+  uschar *s = expciphers;
+  while (*s != 0) { if (*s == '_') *s = '-'; s++; }
+  DEBUG(D_tls) debug_printf("required ciphers: %s\n", expciphers);
+  if (!SSL_CTX_set_cipher_list(server_ctx, CS expciphers))
+    return tls_error(US"SSL_CTX_set_cipher_list", NULL, NULL);
+  cbinfo->server_cipher_list = expciphers;
+  }
+
+/* If this is a host for which certificate verification is mandatory or
+optional, set up appropriately. */
+
+tls_in.certificate_verified = FALSE;
+server_verify_callback_called = FALSE;
+
+if (verify_check_host(&tls_verify_hosts) == OK)
+  {
+  rc = setup_certs(server_ctx, tls_verify_certificates, tls_crl, NULL,
+                       FALSE, verify_callback_server);
+  if (rc != OK) return rc;
+  server_verify_optional = FALSE;
+  }
+else if (verify_check_host(&tls_try_verify_hosts) == OK)
+  {
+  rc = setup_certs(server_ctx, tls_verify_certificates, tls_crl, NULL,
+                       TRUE, verify_callback_server);
+  if (rc != OK) return rc;
+  server_verify_optional = TRUE;
+  }
+
+/* Prepare for new connection */
+
+if ((server_ssl = SSL_new(server_ctx)) == NULL) return tls_error(US"SSL_new", NULL, NULL);
+
+/* Warning: we used to SSL_clear(ssl) here, it was removed.
+ *
+ * With the SSL_clear(), we get strange interoperability bugs with
+ * OpenSSL 1.0.1b and TLS1.1/1.2.  It looks as though this may be a bug in
+ * OpenSSL itself, as a clear should not lead to inability to follow protocols.
+ *
+ * The SSL_clear() call is to let an existing SSL* be reused, typically after
+ * session shutdown.  In this case, we have a brand new object and there's no
+ * obvious reason to immediately clear it.  I'm guessing that this was
+ * originally added because of incomplete initialisation which the clear fixed,
+ * in some historic release.
+ */
+
+/* Set context and tell client to go ahead, except in the case of TLS startup
+on connection, where outputting anything now upsets the clients and tends to
+make them disconnect. We need to have an explicit fflush() here, to force out
+the response. Other smtp_printf() calls do not need it, because in non-TLS
+mode, the fflush() happens when smtp_getc() is called. */
+
+SSL_set_session_id_context(server_ssl, sid_ctx, Ustrlen(sid_ctx));
+if (!tls_in.on_connect)
+  {
+  smtp_printf("220 TLS go ahead\r\n");
+  fflush(smtp_out);
+  }
+
+/* Now negotiate the TLS session. We put our own timer on it, since it seems
+that the OpenSSL library doesn't. */
+
+SSL_set_wfd(server_ssl, fileno(smtp_out));
+SSL_set_rfd(server_ssl, fileno(smtp_in));
+SSL_set_accept_state(server_ssl);
+
+DEBUG(D_tls) debug_printf("Calling SSL_accept\n");
+
+sigalrm_seen = FALSE;
+if (smtp_receive_timeout > 0) alarm(smtp_receive_timeout);
+rc = SSL_accept(server_ssl);
+alarm(0);
+
+if (rc <= 0)
+  {
+  tls_error(US"SSL_accept", NULL, sigalrm_seen ? US"timed out" : NULL);
+  if (ERR_get_error() == 0)
+    log_write(0, LOG_MAIN,
+        "TLS client disconnected cleanly (rejected our certificate?)");
+  return FAIL;
+  }
+
+DEBUG(D_tls) debug_printf("SSL_accept was successful\n");
+
+/* TLS has been set up. Adjust the input functions to read via TLS,
+and initialize things. */
+
+construct_cipher_name(server_ssl, cipherbuf, sizeof(cipherbuf), &tls_in.bits);
+tls_in.cipher = cipherbuf;
+
+DEBUG(D_tls)
+  {
+  uschar buf[2048];
+  if (SSL_get_shared_ciphers(server_ssl, CS buf, sizeof(buf)) != NULL)
+    debug_printf("Shared ciphers: %s\n", buf);
+  }
+
+/* Record the certificate we presented */
+  {
+  X509 * crt = SSL_get_certificate(server_ssl);
+  tls_in.ourcert = crt ? X509_dup(crt) : NULL;
+  }
+
+/* Only used by the server-side tls (tls_in), including tls_getc.
+   Client-side (tls_out) reads (seem to?) go via
+   smtp_read_response()/ip_recv().
+   Hence no need to duplicate for _in and _out.
+ */
+ssl_xfer_buffer = store_malloc(ssl_xfer_buffer_size);
+ssl_xfer_buffer_lwm = ssl_xfer_buffer_hwm = 0;
+ssl_xfer_eof = ssl_xfer_error = 0;
+
+receive_getc = tls_getc;
+receive_ungetc = tls_ungetc;
+receive_feof = tls_feof;
+receive_ferror = tls_ferror;
+receive_smtp_buffered = tls_smtp_buffered;
+
+tls_in.active = fileno(smtp_out);
+return OK;
+}
+
+
+
+
+
+/*************************************************
+*    Start a TLS session in a client             *
+*************************************************/
+
+/* Called from the smtp transport after STARTTLS has been accepted.
+
+Argument:
+  fd               the fd of the connection
+  host             connected host (for messages)
+  addr             the first address
+  ob               smtp transport options
+
+Returns:           OK on success
+                   FAIL otherwise - note that tls_error() will not give DEFER
+                     because this is not a server
+*/
+
+int
+tls_client_start(int fd, host_item *host, address_item *addr,
+  void *v_ob)
+{
+smtp_transport_options_block * ob = v_ob;
+static uschar txt[256];
+uschar *expciphers;
+X509* server_cert;
+int rc;
+static uschar cipherbuf[256];
+#ifndef DISABLE_OCSP
+BOOL require_ocsp = verify_check_this_host(&ob->hosts_require_ocsp,
+  NULL, host->name, host->address, NULL) == OK;
+BOOL request_ocsp = require_ocsp ? TRUE
+  : verify_check_this_host(&ob->hosts_request_ocsp,
+      NULL, host->name, host->address, NULL) == OK;
+#endif
+
+rc = tls_init(&client_ctx, host, NULL,
+    ob->tls_certificate, ob->tls_privatekey,
+#ifndef DISABLE_OCSP
+    (void *)(long)request_ocsp,
+#endif
+    addr, &client_static_cbinfo);
+if (rc != OK) return rc;
+
+tls_out.certificate_verified = FALSE;
+client_verify_callback_called = FALSE;
+
+if (!expand_check(ob->tls_require_ciphers, US"tls_require_ciphers",
+    &expciphers))
+  return FAIL;
+
+/* In OpenSSL, cipher components are separated by hyphens. In GnuTLS, they
+are separated by underscores. So that I can use either form in my tests, and
+also for general convenience, we turn underscores into hyphens here. */
+
+if (expciphers != NULL)
+  {
+  uschar *s = expciphers;
+  while (*s != 0) { if (*s == '_') *s = '-'; s++; }
+  DEBUG(D_tls) debug_printf("required ciphers: %s\n", expciphers);
+  if (!SSL_CTX_set_cipher_list(client_ctx, CS expciphers))
+    return tls_error(US"SSL_CTX_set_cipher_list", host, NULL);
+  }
+
+/* stick to the old behaviour for compatibility if tls_verify_certificates is 
+   set but both tls_verify_hosts and tls_try_verify_hosts is not set. Check only
+   the specified host patterns if one of them is defined */
+
+if ((!ob->tls_verify_hosts && !ob->tls_try_verify_hosts) ||
+    (verify_check_host(&ob->tls_verify_hosts) == OK))
+  {
+  if ((rc = setup_certs(client_ctx, ob->tls_verify_certificates,
+       ob->tls_crl, host, FALSE, verify_callback_client)) != OK)
+    return rc;
+  client_verify_optional = FALSE;
+
+#ifdef EXPERIMENTAL_CERTNAMES
+  if (ob->tls_verify_cert_hostnames)
+    {
+    if (!expand_check(ob->tls_verify_cert_hostnames,
+                     US"tls_verify_cert_hostnames",
+                     &client_static_cbinfo->verify_cert_hostnames))
+      return FAIL;
+    if (client_static_cbinfo->verify_cert_hostnames)
+      DEBUG(D_tls) debug_printf("Cert hostname to check: \"%s\"\n",
+                     client_static_cbinfo->verify_cert_hostnames);
+    }
+#endif
+  }
+else if (verify_check_host(&ob->tls_try_verify_hosts) == OK)
+  {
+  if ((rc = setup_certs(client_ctx, ob->tls_verify_certificates,
+       ob->tls_crl, host, TRUE, verify_callback_client)) != OK)
+    return rc;
+  client_verify_optional = TRUE;
+  }
+
+if ((client_ssl = SSL_new(client_ctx)) == NULL)
+  return tls_error(US"SSL_new", host, NULL);
+SSL_set_session_id_context(client_ssl, sid_ctx, Ustrlen(sid_ctx));
+SSL_set_fd(client_ssl, fd);
+SSL_set_connect_state(client_ssl);
+
+if (ob->tls_sni)
+  {
+  if (!expand_check(ob->tls_sni, US"tls_sni", &tls_out.sni))
+    return FAIL;
+  if (tls_out.sni == NULL)
+    {
+    DEBUG(D_tls) debug_printf("Setting TLS SNI forced to fail, not sending\n");
+    }
+  else if (!Ustrlen(tls_out.sni))
+    tls_out.sni = NULL;
+  else
+    {
+#ifdef EXIM_HAVE_OPENSSL_TLSEXT
+    DEBUG(D_tls) debug_printf("Setting TLS SNI \"%s\"\n", tls_out.sni);
+    SSL_set_tlsext_host_name(client_ssl, tls_out.sni);
+#else
+    DEBUG(D_tls)
+      debug_printf("OpenSSL at build-time lacked SNI support, ignoring \"%s\"\n",
+          tls_out.sni);
+#endif
+    }
+  }
+
+#ifndef DISABLE_OCSP
+/* Request certificate status at connection-time.  If the server
+does OCSP stapling we will get the callback (set in tls_init()) */
+if (request_ocsp)
+  {
+  SSL_set_tlsext_status_type(client_ssl, TLSEXT_STATUSTYPE_ocsp);
+  client_static_cbinfo->u_ocsp.client.verify_required = require_ocsp;
+  tls_out.ocsp = OCSP_NOT_RESP;
+  }
+#endif
+
+/* There doesn't seem to be a built-in timeout on connection. */
+
+DEBUG(D_tls) debug_printf("Calling SSL_connect\n");
+sigalrm_seen = FALSE;
+alarm(ob->command_timeout);
+rc = SSL_connect(client_ssl);
+alarm(0);
+
+if (rc <= 0)
+  return tls_error(US"SSL_connect", host, sigalrm_seen ? US"timed out" : NULL);
+
+DEBUG(D_tls) debug_printf("SSL_connect succeeded\n");
+
+/* Beware anonymous ciphers which lead to server_cert being NULL */
+/*XXX server_cert is never freed... use X509_free() */
+server_cert = SSL_get_peer_certificate (client_ssl);
+if (server_cert)
+  {
+  tls_out.peerdn = US X509_NAME_oneline(X509_get_subject_name(server_cert),
+    CS txt, sizeof(txt));
+  tls_out.peerdn = txt;                /*XXX a static buffer... */
+  }
+else
+  tls_out.peerdn = NULL;
+
+construct_cipher_name(client_ssl, cipherbuf, sizeof(cipherbuf), &tls_out.bits);
+tls_out.cipher = cipherbuf;
+
+/* Record the certificate we presented */
+  {
+  X509 * crt = SSL_get_certificate(client_ssl);
+  tls_out.ourcert = crt ? X509_dup(crt) : NULL;
+  }
+
+tls_out.active = fd;
+return OK;
+}
+
+
+
+
+
+/*************************************************
+*            TLS version of getc                 *
+*************************************************/
+
+/* This gets the next byte from the TLS input buffer. If the buffer is empty,
+it refills the buffer via the SSL reading function.
+
+Arguments:  none
+Returns:    the next character or EOF
+
+Only used by the server-side TLS.
+*/
+
+int
+tls_getc(void)
+{
+if (ssl_xfer_buffer_lwm >= ssl_xfer_buffer_hwm)
+  {
+  int error;
+  int inbytes;
+
+  DEBUG(D_tls) debug_printf("Calling SSL_read(%p, %p, %u)\n", server_ssl,
+    ssl_xfer_buffer, ssl_xfer_buffer_size);
+
+  if (smtp_receive_timeout > 0) alarm(smtp_receive_timeout);
+  inbytes = SSL_read(server_ssl, CS ssl_xfer_buffer, ssl_xfer_buffer_size);
+  error = SSL_get_error(server_ssl, inbytes);
+  alarm(0);
+
+  /* SSL_ERROR_ZERO_RETURN appears to mean that the SSL session has been
+  closed down, not that the socket itself has been closed down. Revert to
+  non-SSL handling. */
+
+  if (error == SSL_ERROR_ZERO_RETURN)
+    {
+    DEBUG(D_tls) debug_printf("Got SSL_ERROR_ZERO_RETURN\n");
+
+    receive_getc = smtp_getc;
+    receive_ungetc = smtp_ungetc;
+    receive_feof = smtp_feof;
+    receive_ferror = smtp_ferror;
+    receive_smtp_buffered = smtp_buffered;
+
+    SSL_free(server_ssl);
+    server_ssl = NULL;
+    tls_in.active = -1;
+    tls_in.bits = 0;
+    tls_in.cipher = NULL;
+    tls_in.peerdn = NULL;
+    tls_in.sni = NULL;
+
+    return smtp_getc();
+    }
+
+  /* Handle genuine errors */
+
+  else if (error == SSL_ERROR_SSL)
+    {
+    ERR_error_string(ERR_get_error(), ssl_errstring);
+    log_write(0, LOG_MAIN, "TLS error (SSL_read): %s", ssl_errstring);
+    ssl_xfer_error = 1;
+    return EOF;
+    }
+
+  else if (error != SSL_ERROR_NONE)
+    {
+    DEBUG(D_tls) debug_printf("Got SSL error %d\n", error);
+    ssl_xfer_error = 1;
+    return EOF;
+    }
+
+#ifndef DISABLE_DKIM
+  dkim_exim_verify_feed(ssl_xfer_buffer, inbytes);
+#endif
+  ssl_xfer_buffer_hwm = inbytes;
+  ssl_xfer_buffer_lwm = 0;
+  }
+
+/* Something in the buffer; return next uschar */
+
+return ssl_xfer_buffer[ssl_xfer_buffer_lwm++];
+}
+
+
+
+/*************************************************
+*          Read bytes from TLS channel           *
+*************************************************/
+
+/*
+Arguments:
+  buff      buffer of data
+  len       size of buffer
+
+Returns:    the number of bytes read
+            -1 after a failed read
+
+Only used by the client-side TLS.
+*/
+
+int
+tls_read(BOOL is_server, uschar *buff, size_t len)
+{
+SSL *ssl = is_server ? server_ssl : client_ssl;
+int inbytes;
+int error;
+
+DEBUG(D_tls) debug_printf("Calling SSL_read(%p, %p, %u)\n", ssl,
+  buff, (unsigned int)len);
+
+inbytes = SSL_read(ssl, CS buff, len);
+error = SSL_get_error(ssl, inbytes);
+
+if (error == SSL_ERROR_ZERO_RETURN)
+  {
+  DEBUG(D_tls) debug_printf("Got SSL_ERROR_ZERO_RETURN\n");
+  return -1;
+  }
+else if (error != SSL_ERROR_NONE)
+  {
+  return -1;
+  }
+
+return inbytes;
+}
+
+
+
+
+
+/*************************************************
+*         Write bytes down TLS channel           *
+*************************************************/
+
+/*
+Arguments:
+  is_server channel specifier
+  buff      buffer of data
+  len       number of bytes
+
+Returns:    the number of bytes after a successful write,
+            -1 after a failed write
+
+Used by both server-side and client-side TLS.
+*/
+
+int
+tls_write(BOOL is_server, const uschar *buff, size_t len)
+{
+int outbytes;
+int error;
+int left = len;
+SSL *ssl = is_server ? server_ssl : client_ssl;
+
+DEBUG(D_tls) debug_printf("tls_do_write(%p, %d)\n", buff, left);
+while (left > 0)
+  {
+  DEBUG(D_tls) debug_printf("SSL_write(SSL, %p, %d)\n", buff, left);
+  outbytes = SSL_write(ssl, CS buff, left);
+  error = SSL_get_error(ssl, outbytes);
+  DEBUG(D_tls) debug_printf("outbytes=%d error=%d\n", outbytes, error);
+  switch (error)
+    {
+    case SSL_ERROR_SSL:
+    ERR_error_string(ERR_get_error(), ssl_errstring);
+    log_write(0, LOG_MAIN, "TLS error (SSL_write): %s", ssl_errstring);
+    return -1;
+
+    case SSL_ERROR_NONE:
+    left -= outbytes;
+    buff += outbytes;
+    break;
+
+    case SSL_ERROR_ZERO_RETURN:
+    log_write(0, LOG_MAIN, "SSL channel closed on write");
+    return -1;
+
+    case SSL_ERROR_SYSCALL:
+    log_write(0, LOG_MAIN, "SSL_write: (from %s) syscall: %s",
+      sender_fullhost ? sender_fullhost : US"<unknown>",
+      strerror(errno));
+
+    default:
+    log_write(0, LOG_MAIN, "SSL_write error %d", error);
+    return -1;
+    }
+  }
+return len;
+}
+
+
+
+/*************************************************
+*         Close down a TLS session               *
+*************************************************/
+
+/* This is also called from within a delivery subprocess forked from the
+daemon, to shut down the TLS library, without actually doing a shutdown (which
+would tamper with the SSL session in the parent process).
+
+Arguments:   TRUE if SSL_shutdown is to be called
+Returns:     nothing
+
+Used by both server-side and client-side TLS.
+*/
+
+void
+tls_close(BOOL is_server, BOOL shutdown)
+{
+SSL **sslp = is_server ? &server_ssl : &client_ssl;
+int *fdp = is_server ? &tls_in.active : &tls_out.active;
+
+if (*fdp < 0) return;  /* TLS was not active */
+
+if (shutdown)
+  {
+  DEBUG(D_tls) debug_printf("tls_close(): shutting down SSL\n");
+  SSL_shutdown(*sslp);
+  }
+
+SSL_free(*sslp);
+*sslp = NULL;
+
+*fdp = -1;
+}
+
+
+
+
+/*************************************************
+*  Let tls_require_ciphers be checked at startup *
+*************************************************/
+
+/* The tls_require_ciphers option, if set, must be something which the
+library can parse.
+
+Returns:     NULL on success, or error message
+*/
+
+uschar *
+tls_validate_require_cipher(void)
+{
+SSL_CTX *ctx;
+uschar *s, *expciphers, *err;
+
+/* this duplicates from tls_init(), we need a better "init just global
+state, for no specific purpose" singleton function of our own */
+
+SSL_load_error_strings();
+OpenSSL_add_ssl_algorithms();
+#if (OPENSSL_VERSION_NUMBER >= 0x0090800fL) && !defined(OPENSSL_NO_SHA256)
+/* SHA256 is becoming ever more popular. This makes sure it gets added to the
+list of available digests. */
+EVP_add_digest(EVP_sha256());
+#endif
+
+if (!(tls_require_ciphers && *tls_require_ciphers))
+  return NULL;
+
+if (!expand_check(tls_require_ciphers, US"tls_require_ciphers", &expciphers))
+  return US"failed to expand tls_require_ciphers";
+
+if (!(expciphers && *expciphers))
+  return NULL;
+
+/* normalisation ripped from above */
+s = expciphers;
+while (*s != 0) { if (*s == '_') *s = '-'; s++; }
+
+err = NULL;
+
+ctx = SSL_CTX_new(SSLv23_server_method());
+if (!ctx)
+  {
+  ERR_error_string(ERR_get_error(), ssl_errstring);
+  return string_sprintf("SSL_CTX_new() failed: %s", ssl_errstring);
+  }
+
+DEBUG(D_tls)
+  debug_printf("tls_require_ciphers expands to \"%s\"\n", expciphers);
+
+if (!SSL_CTX_set_cipher_list(ctx, CS expciphers))
+  {
+  ERR_error_string(ERR_get_error(), ssl_errstring);
+  err = string_sprintf("SSL_CTX_set_cipher_list(%s) failed", expciphers);
+  }
+
+SSL_CTX_free(ctx);
+
+return err;
+}
+
+
+
+
+/*************************************************
+*         Report the library versions.           *
+*************************************************/
+
+/* There have historically been some issues with binary compatibility in
+OpenSSL libraries; if Exim (like many other applications) is built against
+one version of OpenSSL but the run-time linker picks up another version,
+it can result in serious failures, including crashing with a SIGSEGV.  So
+report the version found by the compiler and the run-time version.
+
+Note: some OS vendors backport security fixes without changing the version
+number/string, and the version date remains unchanged.  The _build_ date
+will change, so we can more usefully assist with version diagnosis by also
+reporting the build date.
+
+Arguments:   a FILE* to print the results to
+Returns:     nothing
+*/
+
+void
+tls_version_report(FILE *f)
+{
+fprintf(f, "Library version: OpenSSL: Compile: %s\n"
+           "                          Runtime: %s\n"
+           "                                 : %s\n",
+           OPENSSL_VERSION_TEXT,
+           SSLeay_version(SSLEAY_VERSION),
+           SSLeay_version(SSLEAY_BUILT_ON));
+/* third line is 38 characters for the %s and the line is 73 chars long;
+the OpenSSL output includes a "built on: " prefix already. */
+}
+
+
+
+
+/*************************************************
+*            Random number generation            *
+*************************************************/
+
+/* Pseudo-random number generation.  The result is not expected to be
+cryptographically strong but not so weak that someone will shoot themselves
+in the foot using it as a nonce in input in some email header scheme or
+whatever weirdness they'll twist this into.  The result should handle fork()
+and avoid repeating sequences.  OpenSSL handles that for us.
+
+Arguments:
+  max       range maximum
+Returns     a random number in range [0, max-1]
+*/
+
+int
+vaguely_random_number(int max)
+{
+unsigned int r;
+int i, needed_len;
+static pid_t pidlast = 0;
+pid_t pidnow;
+uschar *p;
+uschar smallbuf[sizeof(r)];
+
+if (max <= 1)
+  return 0;
+
+pidnow = getpid();
+if (pidnow != pidlast)
+  {
+  /* Although OpenSSL documents that "OpenSSL makes sure that the PRNG state
+  is unique for each thread", this doesn't apparently apply across processes,
+  so our own warning from vaguely_random_number_fallback() applies here too.
+  Fix per PostgreSQL. */
+  if (pidlast != 0)
+    RAND_cleanup();
+  pidlast = pidnow;
+  }
+
+/* OpenSSL auto-seeds from /dev/random, etc, but this a double-check. */
+if (!RAND_status())
+  {
+  randstuff r;
+  gettimeofday(&r.tv, NULL);
+  r.p = getpid();
+
+  RAND_seed((uschar *)(&r), sizeof(r));
+  }
+/* We're after pseudo-random, not random; if we still don't have enough data
+in the internal PRNG then our options are limited.  We could sleep and hope
+for entropy to come along (prayer technique) but if the system is so depleted
+in the first place then something is likely to just keep taking it.  Instead,
+we'll just take whatever little bit of pseudo-random we can still manage to
+get. */
+
+needed_len = sizeof(r);
+/* Don't take 8 times more entropy than needed if int is 8 octets and we were
+asked for a number less than 10. */
+for (r = max, i = 0; r; ++i)
+  r >>= 1;
+i = (i + 7) / 8;
+if (i < needed_len)
+  needed_len = i;
+
+/* We do not care if crypto-strong */
+i = RAND_pseudo_bytes(smallbuf, needed_len);
+if (i < 0)
+  {
+  DEBUG(D_all)
+    debug_printf("OpenSSL RAND_pseudo_bytes() not supported by RAND method, using fallback.\n");
+  return vaguely_random_number_fallback(max);
+  }
+
+r = 0;
+for (p = smallbuf; needed_len; --needed_len, ++p)
+  {
+  r *= 256;
+  r += *p;
+  }
+
+/* We don't particularly care about weighted results; if someone wants
+smooth distribution and cares enough then they should submit a patch then. */
+return r % max;
+}
+
+
+
+
+/*************************************************
+*        OpenSSL option parse                    *
+*************************************************/
+
+/* Parse one option for tls_openssl_options_parse below
+
+Arguments:
+  name    one option name
+  value   place to store a value for it
+Returns   success or failure in parsing
+*/
+
+struct exim_openssl_option {
+  uschar *name;
+  long    value;
+};
+/* We could use a macro to expand, but we need the ifdef and not all the
+options document which version they were introduced in.  Policylet: include
+all options unless explicitly for DTLS, let the administrator choose which
+to apply.
+
+This list is current as of:
+  ==>  1.0.1b  <==
+Plus SSL_OP_SAFARI_ECDHE_ECDSA_BUG from 2013-June patch/discussion on openssl-dev
+*/
+static struct exim_openssl_option exim_openssl_options[] = {
+/* KEEP SORTED ALPHABETICALLY! */
+#ifdef SSL_OP_ALL
+  { US"all", SSL_OP_ALL },
+#endif
+#ifdef SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION
+  { US"allow_unsafe_legacy_renegotiation", SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION },
+#endif
+#ifdef SSL_OP_CIPHER_SERVER_PREFERENCE
+  { US"cipher_server_preference", SSL_OP_CIPHER_SERVER_PREFERENCE },
+#endif
+#ifdef SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS
+  { US"dont_insert_empty_fragments", SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS },
+#endif
+#ifdef SSL_OP_EPHEMERAL_RSA
+  { US"ephemeral_rsa", SSL_OP_EPHEMERAL_RSA },
+#endif
+#ifdef SSL_OP_LEGACY_SERVER_CONNECT
+  { US"legacy_server_connect", SSL_OP_LEGACY_SERVER_CONNECT },
+#endif
+#ifdef SSL_OP_MICROSOFT_BIG_SSLV3_BUFFER
+  { US"microsoft_big_sslv3_buffer", SSL_OP_MICROSOFT_BIG_SSLV3_BUFFER },
+#endif
+#ifdef SSL_OP_MICROSOFT_SESS_ID_BUG
+  { US"microsoft_sess_id_bug", SSL_OP_MICROSOFT_SESS_ID_BUG },
+#endif
+#ifdef SSL_OP_MSIE_SSLV2_RSA_PADDING
+  { US"msie_sslv2_rsa_padding", SSL_OP_MSIE_SSLV2_RSA_PADDING },
+#endif
+#ifdef SSL_OP_NETSCAPE_CHALLENGE_BUG
+  { US"netscape_challenge_bug", SSL_OP_NETSCAPE_CHALLENGE_BUG },
+#endif
+#ifdef SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG
+  { US"netscape_reuse_cipher_change_bug", SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG },
+#endif
+#ifdef SSL_OP_NO_COMPRESSION
+  { US"no_compression", SSL_OP_NO_COMPRESSION },
+#endif
+#ifdef SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
+  { US"no_session_resumption_on_renegotiation", SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION },
+#endif
+#ifdef SSL_OP_NO_SSLv2
+  { US"no_sslv2", SSL_OP_NO_SSLv2 },
+#endif
+#ifdef SSL_OP_NO_SSLv3
+  { US"no_sslv3", SSL_OP_NO_SSLv3 },
+#endif
+#ifdef SSL_OP_NO_TICKET
+  { US"no_ticket", SSL_OP_NO_TICKET },
+#endif
+#ifdef SSL_OP_NO_TLSv1
+  { US"no_tlsv1", SSL_OP_NO_TLSv1 },
+#endif
+#ifdef SSL_OP_NO_TLSv1_1
+#if SSL_OP_NO_TLSv1_1 == 0x00000400L
+  /* Error in chosen value in 1.0.1a; see first item in CHANGES for 1.0.1b */
+#warning OpenSSL 1.0.1a uses a bad value for SSL_OP_NO_TLSv1_1, ignoring
+#else
+  { US"no_tlsv1_1", SSL_OP_NO_TLSv1_1 },
+#endif
+#endif
+#ifdef SSL_OP_NO_TLSv1_2
+  { US"no_tlsv1_2", SSL_OP_NO_TLSv1_2 },
+#endif
+#ifdef SSL_OP_SAFARI_ECDHE_ECDSA_BUG
+  { US"safari_ecdhe_ecdsa_bug", SSL_OP_SAFARI_ECDHE_ECDSA_BUG },
+#endif
+#ifdef SSL_OP_SINGLE_DH_USE
+  { US"single_dh_use", SSL_OP_SINGLE_DH_USE },
+#endif
+#ifdef SSL_OP_SINGLE_ECDH_USE
+  { US"single_ecdh_use", SSL_OP_SINGLE_ECDH_USE },
+#endif
+#ifdef SSL_OP_SSLEAY_080_CLIENT_DH_BUG
+  { US"ssleay_080_client_dh_bug", SSL_OP_SSLEAY_080_CLIENT_DH_BUG },
+#endif
+#ifdef SSL_OP_SSLREF2_REUSE_CERT_TYPE_BUG
+  { US"sslref2_reuse_cert_type_bug", SSL_OP_SSLREF2_REUSE_CERT_TYPE_BUG },
+#endif
+#ifdef SSL_OP_TLS_BLOCK_PADDING_BUG
+  { US"tls_block_padding_bug", SSL_OP_TLS_BLOCK_PADDING_BUG },
+#endif
+#ifdef SSL_OP_TLS_D5_BUG
+  { US"tls_d5_bug", SSL_OP_TLS_D5_BUG },
+#endif
+#ifdef SSL_OP_TLS_ROLLBACK_BUG
+  { US"tls_rollback_bug", SSL_OP_TLS_ROLLBACK_BUG },
+#endif
+};
+static int exim_openssl_options_size =
+  sizeof(exim_openssl_options)/sizeof(struct exim_openssl_option);
+
+
+static BOOL
+tls_openssl_one_option_parse(uschar *name, long *value)
+{
+int first = 0;
+int last = exim_openssl_options_size;
+while (last > first)
+  {
+  int middle = (first + last)/2;
+  int c = Ustrcmp(name, exim_openssl_options[middle].name);
+  if (c == 0)
+    {
+    *value = exim_openssl_options[middle].value;
+    return TRUE;
+    }
+  else if (c > 0)
+    first = middle + 1;
+  else
+    last = middle;
+  }
+return FALSE;
+}
+
+
+
+
+/*************************************************
+*        OpenSSL option parsing logic            *
+*************************************************/
+
+/* OpenSSL has a number of compatibility options which an administrator might
+reasonably wish to set.  Interpret a list similarly to decode_bits(), so that
+we look like log_selector.
+
+Arguments:
+  option_spec  the administrator-supplied string of options
+  results      ptr to long storage for the options bitmap
+Returns        success or failure
+*/
+
+BOOL
+tls_openssl_options_parse(uschar *option_spec, long *results)
+{
+long result, item;
+uschar *s, *end;
+uschar keep_c;
+BOOL adding, item_parsed;
+
+result = 0L;
+/* Prior to 4.80 we or'd in SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS; removed
+ * from default because it increases BEAST susceptibility. */
+#ifdef SSL_OP_NO_SSLv2
+result |= SSL_OP_NO_SSLv2;
+#endif
+
+if (option_spec == NULL)
+  {
+  *results = result;
+  return TRUE;
+  }
+
+for (s=option_spec; *s != '\0'; /**/)
+  {
+  while (isspace(*s)) ++s;
+  if (*s == '\0')
+    break;
+  if (*s != '+' && *s != '-')
+    {
+    DEBUG(D_tls) debug_printf("malformed openssl option setting: "
+        "+ or - expected but found \"%s\"\n", s);
+    return FALSE;
+    }
+  adding = *s++ == '+';
+  for (end = s; (*end != '\0') && !isspace(*end); ++end) /**/ ;
+  keep_c = *end;
+  *end = '\0';
+  item_parsed = tls_openssl_one_option_parse(s, &item);
+  if (!item_parsed)
+    {
+    DEBUG(D_tls) debug_printf("openssl option setting unrecognised: \"%s\"\n", s);
+    return FALSE;
+    }
+  DEBUG(D_tls) debug_printf("openssl option, %s from %lx: %lx (%s)\n",
+      adding ? "adding" : "removing", result, item, s);
+  if (adding)
+    result |= item;
+  else
+    result &= ~item;
+  *end = keep_c;
+  s = end;
+  }
+
+*results = result;
+return TRUE;
+}
+
+/* vi: aw ai sw=2
+*/
+/* End of tls-openssl.c */
diff --git a/src/tls.c b/src/tls.c
new file mode 100644 (file)
index 0000000..f2ab567
--- /dev/null
+++ b/src/tls.c
@@ -0,0 +1,337 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2012 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* This module provides TLS (aka SSL) support for Exim. The code for OpenSSL is
+based on a patch that was originally contributed by Steve Haslam. It was
+adapted from stunnel, a GPL program by Michal Trojnara. The code for GNU TLS is
+based on a patch contributed by Nikos Mavroyanopoulos. Because these packages
+are so very different, the functions for each are kept in separate files. The
+relevant file is #included as required, after any any common functions.
+
+No cryptographic code is included in Exim. All this module does is to call
+functions from the OpenSSL or GNU TLS libraries. */
+
+
+#include "exim.h"
+#include "transports/smtp.h"
+
+/* This module is compiled only when it is specifically requested in the
+build-time configuration. However, some compilers don't like compiling empty
+modules, so keep them happy with a dummy when skipping the rest. Make it
+reference itself to stop picky compilers complaining that it is unused, and put
+in a dummy argument to stop even pickier compilers complaining about infinite
+loops. */
+
+#ifndef SUPPORT_TLS
+static void dummy(int x) { dummy(x-1); }
+#else
+
+/* Static variables that are used for buffering data by both sets of
+functions and the common functions below.
+
+We're moving away from this; GnuTLS is already using a state, which
+can switch, so we can do TLS callouts during ACLs. */
+
+static const int ssl_xfer_buffer_size = 4096;
+#ifndef USE_GNUTLS
+static uschar *ssl_xfer_buffer = NULL;
+static int ssl_xfer_buffer_lwm = 0;
+static int ssl_xfer_buffer_hwm = 0;
+static int ssl_xfer_eof = 0;
+static int ssl_xfer_error = 0;
+#endif
+
+uschar *tls_channelbinding_b64 = NULL;
+
+
+/*************************************************
+*       Expand string; give error on failure     *
+*************************************************/
+
+/* If expansion is forced to fail, set the result NULL and return TRUE.
+Other failures return FALSE. For a server, an SMTP response is given.
+
+Arguments:
+  s         the string to expand; if NULL just return TRUE
+  name      name of string being expanded (for error)
+  result    where to put the result
+
+Returns:    TRUE if OK; result may still be NULL after forced failure
+*/
+
+static BOOL
+expand_check(const uschar *s, const uschar *name, uschar **result)
+{
+if (s == NULL) *result = NULL; else
+  {
+  *result = expand_string(US s); /* need to clean up const some more */
+  if (*result == NULL && !expand_string_forcedfail)
+    {
+    log_write(0, LOG_MAIN|LOG_PANIC, "expansion of %s failed: %s", name,
+      expand_string_message);
+    return FALSE;
+    }
+  }
+return TRUE;
+}
+
+
+/*************************************************
+*        Many functions are package-specific     *
+*************************************************/
+
+#ifdef USE_GNUTLS
+#include "tls-gnu.c"
+#include "tlscert-gnu.c"
+
+#define ssl_xfer_buffer (state_server.xfer_buffer)
+#define ssl_xfer_buffer_lwm (state_server.xfer_buffer_lwm)
+#define ssl_xfer_buffer_hwm (state_server.xfer_buffer_hwm)
+#define ssl_xfer_eof (state_server.xfer_eof)
+#define ssl_xfer_error (state_server.xfer_error)
+
+#else
+#include "tls-openssl.c"
+#include "tlscert-openssl.c"
+#endif
+
+
+
+/*************************************************
+*           TLS version of ungetc                *
+*************************************************/
+
+/* Puts a character back in the input buffer. Only ever
+called once.
+Only used by the server-side TLS.
+
+Arguments:
+  ch           the character
+
+Returns:       the character
+*/
+
+int
+tls_ungetc(int ch)
+{
+ssl_xfer_buffer[--ssl_xfer_buffer_lwm] = ch;
+return ch;
+}
+
+
+
+/*************************************************
+*           TLS version of feof                  *
+*************************************************/
+
+/* Tests for a previous EOF
+Only used by the server-side TLS.
+
+Arguments:     none
+Returns:       non-zero if the eof flag is set
+*/
+
+int
+tls_feof(void)
+{
+return ssl_xfer_eof;
+}
+
+
+
+/*************************************************
+*              TLS version of ferror             *
+*************************************************/
+
+/* Tests for a previous read error, and returns with errno
+restored to what it was when the error was detected.
+Only used by the server-side TLS.
+
+>>>>> Hmm. Errno not handled yet. Where do we get it from?  >>>>>
+
+Arguments:     none
+Returns:       non-zero if the error flag is set
+*/
+
+int
+tls_ferror(void)
+{
+return ssl_xfer_error;
+}
+
+
+/*************************************************
+*           TLS version of smtp_buffered         *
+*************************************************/
+
+/* Tests for unused chars in the TLS input buffer.
+Only used by the server-side TLS.
+
+Arguments:     none
+Returns:       TRUE/FALSE
+*/
+
+BOOL
+tls_smtp_buffered(void)
+{
+return ssl_xfer_buffer_lwm < ssl_xfer_buffer_hwm;
+}
+
+
+#endif  /* SUPPORT_TLS */
+
+void
+tls_modify_variables(tls_support * dest_tsp)
+{
+modify_variable(US"tls_bits",                 &dest_tsp->bits);
+modify_variable(US"tls_certificate_verified", &dest_tsp->certificate_verified);
+modify_variable(US"tls_cipher",               &dest_tsp->cipher);
+modify_variable(US"tls_peerdn",               &dest_tsp->peerdn);
+#if defined(SUPPORT_TLS) && !defined(USE_GNUTLS)
+modify_variable(US"tls_sni",                  &dest_tsp->sni);
+#endif
+}
+
+
+#ifdef SUPPORT_TLS
+/************************************************
+*      TLS certificate name operations         *
+************************************************/
+
+/* Convert an rfc4514 DN to an exim comma-sep list.
+Backslashed commas need to be replaced by doublecomma
+for Exim's list quoting.  We modify the given string
+inplace.
+*/
+
+static void
+dn_to_list(uschar * dn)
+{
+uschar * cp;
+for (cp = dn; *cp; cp++)
+  if (cp[0] == '\\' && cp[1] == ',')
+    *cp++ = ',';
+}
+
+
+/* Extract fields of a given type from an RFC4514-
+format Distinguished Name.  Return an Exim list.
+NOTE: We modify the supplied dn string during operation.
+
+Arguments:
+       dn      Distinguished Name string
+       mod     string containing optional list-sep and
+               field selector match, comma-separated
+Return:
+       allocated string with list of matching fields,
+       field type stripped
+*/
+
+uschar *
+tls_field_from_dn(uschar * dn, uschar * mod)
+{
+int insep = ',';
+uschar outsep = '\n';
+uschar * ele;
+uschar * match = NULL;
+int len;
+uschar * list = NULL;
+
+while ((ele = string_nextinlist(&mod, &insep, NULL, 0)))
+  if (ele[0] != '>')
+    match = ele;       /* field tag to match */
+  else if (ele[1])
+    outsep = ele[1];   /* nondefault separator */
+
+dn_to_list(dn);
+insep = ',';
+len = Ustrlen(match);
+while ((ele = string_nextinlist(&dn, &insep, NULL, 0)))
+  if (Ustrncmp(ele, match, len) == 0 && ele[len] == '=')
+    list = string_append_listele(list, outsep, ele+len+1);
+return list;
+}
+
+
+# ifdef EXPERIMENTAL_CERTNAMES
+/* Compare a domain name with a possibly-wildcarded name. Wildcards
+are restricted to a single one, as the first element of patterns
+having at least three dot-separated elements.  Case-independent.
+Return TRUE for a match
+*/
+static BOOL
+is_name_match(const uschar * name, const uschar * pat)
+{
+uschar * cp;
+return *pat == '*'             /* possible wildcard match */
+  ?    *++pat == '.'           /* starts star, dot              */
+    && !Ustrchr(++pat, '*')    /* has no more stars             */
+    && Ustrchr(pat, '.')       /* and has another dot.          */
+    && (cp = Ustrchr(name, '.'))/* The name has at least one dot */
+    && strcmpic(++cp, pat) == 0 /* and we only compare after it. */
+  :    !Ustrchr(pat+1, '*')
+    && strcmpic(name, pat) == 0;
+}
+
+/* Compare a list of names with the dnsname elements
+of the Subject Alternate Name, if any, and the
+Subject otherwise.
+
+Arguments:
+       namelist names to compare
+       cert     certificate
+
+Returns:
+       TRUE/FALSE
+*/
+
+BOOL
+tls_is_name_for_cert(uschar * namelist, void * cert)
+{
+uschar * altnames = tls_cert_subject_altname(cert, US"dns");
+uschar * subjdn;
+uschar * certname;
+int cmp_sep = 0;
+uschar * cmpname;
+
+if ((altnames = tls_cert_subject_altname(cert, US"dns")))
+  {
+  int alt_sep = '\n';
+  while ((cmpname = string_nextinlist(&namelist, &cmp_sep, NULL, 0)))
+    {
+    uschar * an = altnames;
+    while ((certname = string_nextinlist(&an, &alt_sep, NULL, 0)))
+      if (is_name_match(cmpname, certname))
+       return TRUE;
+    }
+  }
+
+else if ((subjdn = tls_cert_subject(cert, NULL)))
+  {
+  int sn_sep = ',';
+
+  dn_to_list(subjdn);
+  while ((cmpname = string_nextinlist(&namelist, &cmp_sep, NULL, 0)))
+    {
+    uschar * sn = subjdn;
+    while ((certname = string_nextinlist(&sn, &sn_sep, NULL, 0)))
+      if (  *certname++ == 'C'
+        && *certname++ == 'N'
+        && *certname++ == '='
+        && is_name_match(cmpname, certname)
+        )
+       return TRUE;
+    }
+  }
+return FALSE;
+}
+# endif        /*EXPERIMENTAL_CERTNAMES*/
+#endif /*SUPPORT_TLS*/
+
+/* vi: aw ai sw=2
+*/
+/* End of tls.c */
diff --git a/src/tlscert-gnu.c b/src/tlscert-gnu.c
new file mode 100644 (file)
index 0000000..3261c4e
--- /dev/null
@@ -0,0 +1,451 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) Jeremy Harris 2014 */
+
+/* This file provides TLS/SSL support for Exim using the GnuTLS library,
+one of the available supported implementations.  This file is #included into
+tls.c when USE_GNUTLS has been set.
+*/
+
+#include <gnutls/gnutls.h>
+/* needed for cert checks in verification and DN extraction: */
+#include <gnutls/x509.h>
+/* needed to disable PKCS11 autoload unless requested */
+#if GNUTLS_VERSION_NUMBER >= 0x020c00
+# include <gnutls/pkcs11.h>
+#endif
+
+
+/*****************************************************
+*  Export/import a certificate, binary/printable
+*****************************************************/
+int
+tls_export_cert(uschar * buf, size_t buflen, void * cert)
+{
+size_t sz = buflen;
+void * reset_point = store_get(0);
+int fail;
+uschar * cp;
+
+if ((fail = gnutls_x509_crt_export((gnutls_x509_crt_t)cert,
+    GNUTLS_X509_FMT_PEM, buf, &sz)))
+  {
+  log_write(0, LOG_MAIN, "TLS error in certificate export: %s",
+    gnutls_strerror(fail));
+  return 1;
+  }
+if ((cp = string_printing(buf)) != buf)
+  {
+  Ustrncpy(buf, cp, buflen);
+  if (buf[buflen-1])
+    fail = 1;
+  }
+store_reset(reset_point);
+return fail;
+}
+
+int
+tls_import_cert(const uschar * buf, void ** cert)
+{
+void * reset_point = store_get(0);
+gnutls_datum_t datum;
+gnutls_x509_crt_t crt;
+int fail = 0;
+
+gnutls_global_init();
+gnutls_x509_crt_init(&crt);
+
+datum.data = string_unprinting(US buf);
+datum.size = Ustrlen(datum.data);
+if ((fail = gnutls_x509_crt_import(crt, &datum, GNUTLS_X509_FMT_PEM)))
+  {
+  log_write(0, LOG_MAIN, "TLS error in certificate import: %s",
+    gnutls_strerror(fail));
+  fail = 1;
+  }
+else
+  *cert = (void *)crt;
+
+store_reset(reset_point);
+return fail;
+}
+
+void
+tls_free_cert(void * cert)
+{
+gnutls_x509_crt_deinit((gnutls_x509_crt_t) cert);
+gnutls_global_deinit();
+}
+
+/*****************************************************
+*  Certificate field extraction routines
+*****************************************************/
+
+/* First, some internal service functions */
+
+static uschar *
+g_err(const char * tag, const char * from, int gnutls_err)
+{
+expand_string_message = string_sprintf("%s: %s fail: %s\n",
+  from, tag, gnutls_strerror(gnutls_err));
+return NULL;
+}
+
+
+static uschar *
+time_copy(time_t t, uschar * mod)
+{
+uschar * cp;
+struct tm * tp;
+size_t len;
+
+if (mod && Ustrcmp(mod, "int") == 0)
+  return string_sprintf("%u", (unsigned)t);
+
+cp = store_get(32);
+tp = gmtime(&t);
+len = strftime(CS cp, 32, "%b %e %T %Y %Z", tp);
+return len > 0 ? cp : NULL;
+}
+
+
+/**/
+/* Now the extractors, called from expand.c
+Arguments:
+  cert         The certificate
+  mod          Optional modifiers for the operator
+
+Return:
+  Allocated string with extracted value
+*/
+
+uschar *
+tls_cert_issuer(void * cert, uschar * mod)
+{
+uschar * cp = NULL;
+int ret;
+size_t siz = 0;
+
+if ((ret = gnutls_x509_crt_get_issuer_dn(cert, cp, &siz))
+    != GNUTLS_E_SHORT_MEMORY_BUFFER)
+  return g_err("gi0", __FUNCTION__, ret);
+
+cp = store_get(siz);
+if ((ret = gnutls_x509_crt_get_issuer_dn(cert, cp, &siz)) < 0)
+  return g_err("gi1", __FUNCTION__, ret);
+
+return mod ? tls_field_from_dn(cp, mod) : cp;
+}
+
+uschar *
+tls_cert_not_after(void * cert, uschar * mod)
+{
+return time_copy(
+  gnutls_x509_crt_get_expiration_time((gnutls_x509_crt_t)cert),
+  mod);
+}
+
+uschar *
+tls_cert_not_before(void * cert, uschar * mod)
+{
+return time_copy(
+  gnutls_x509_crt_get_activation_time((gnutls_x509_crt_t)cert),
+  mod);
+}
+
+uschar *
+tls_cert_serial_number(void * cert, uschar * mod)
+{
+uschar bin[50], txt[150];
+size_t sz = sizeof(bin);
+uschar * sp;
+uschar * dp;
+int ret;
+
+if ((ret = gnutls_x509_crt_get_serial((gnutls_x509_crt_t)cert,
+    bin, &sz)))
+  return g_err("gs0", __FUNCTION__, ret);
+
+for(dp = txt, sp = bin; sz; dp += 2, sp++, sz--)
+  sprintf(dp, "%.2x", *sp);
+for(sp = txt; sp[0]=='0' && sp[1]; ) sp++;     /* leading zeroes */
+return string_copy(sp);
+}
+
+uschar *
+tls_cert_signature(void * cert, uschar * mod)
+{
+uschar * cp1;
+uschar * cp2;
+uschar * cp3;
+size_t len = 0;
+int ret;
+
+if ((ret = gnutls_x509_crt_get_signature((gnutls_x509_crt_t)cert, cp1, &len))
+    != GNUTLS_E_SHORT_MEMORY_BUFFER)
+  return g_err("gs0", __FUNCTION__, ret);
+
+cp1 = store_get(len*4+1);
+if (gnutls_x509_crt_get_signature((gnutls_x509_crt_t)cert, cp1, &len) != 0)
+  return g_err("gs1", __FUNCTION__, ret);
+
+for(cp3 = cp2 = cp1+len; cp1 < cp2; cp3 += 3, cp1++)
+  sprintf(cp3, "%.2x ", *cp1);
+cp3[-1]= '\0';
+
+return cp2;
+}
+
+uschar *
+tls_cert_signature_algorithm(void * cert, uschar * mod)
+{
+gnutls_sign_algorithm_t algo =
+  gnutls_x509_crt_get_signature_algorithm((gnutls_x509_crt_t)cert);
+return algo < 0 ? NULL : string_copy(gnutls_sign_get_name(algo));
+}
+
+uschar *
+tls_cert_subject(void * cert, uschar * mod)
+{
+uschar * cp = NULL;
+int ret;
+size_t siz = 0;
+
+if ((ret = gnutls_x509_crt_get_dn(cert, cp, &siz))
+    != GNUTLS_E_SHORT_MEMORY_BUFFER)
+  return g_err("gs0", __FUNCTION__, ret);
+
+cp = store_get(siz);
+if ((ret = gnutls_x509_crt_get_dn(cert, cp, &siz)) < 0)
+  return g_err("gs1", __FUNCTION__, ret);
+
+return mod ? tls_field_from_dn(cp, mod) : cp;
+}
+
+uschar *
+tls_cert_version(void * cert, uschar * mod)
+{
+return string_sprintf("%d", gnutls_x509_crt_get_version(cert));
+}
+
+uschar *
+tls_cert_ext_by_oid(void * cert, uschar * oid, int idx)
+{
+uschar * cp1 = NULL;
+uschar * cp2;
+uschar * cp3;
+size_t siz = 0;
+unsigned int crit;
+int ret;
+
+ret = gnutls_x509_crt_get_extension_by_oid ((gnutls_x509_crt_t)cert,
+  oid, idx, cp1, &siz, &crit);
+if (ret != GNUTLS_E_SHORT_MEMORY_BUFFER)
+  return g_err("ge0", __FUNCTION__, ret);
+
+cp1 = store_get(siz*4 + 1);
+
+ret = gnutls_x509_crt_get_extension_by_oid ((gnutls_x509_crt_t)cert,
+  oid, idx, cp1, &siz, &crit);
+if (ret < 0)
+  return g_err("ge1", __FUNCTION__, ret);
+
+/* binary data, DER encoded */
+
+/* just dump for now */
+for(cp3 = cp2 = cp1+siz; cp1 < cp2; cp3 += 3, cp1++)
+  sprintf(cp3, "%.2x ", *cp1);
+cp3[-1]= '\0';
+
+return cp2;
+}
+
+uschar *
+tls_cert_subject_altname(void * cert, uschar * mod)
+{
+uschar * list = NULL;
+int index;
+size_t siz;
+int ret;
+uschar sep = '\n';
+uschar * tag = US"";
+uschar * ele;
+int match = -1;
+
+while (mod)
+  {
+  if (*mod == '>' && *++mod) sep = *mod++;
+  else if (Ustrcmp(mod, "dns")==0) { match = GNUTLS_SAN_DNSNAME; mod += 3; }
+  else if (Ustrcmp(mod, "uri")==0) { match = GNUTLS_SAN_URI; mod += 3; }
+  else if (Ustrcmp(mod, "mail")==0) { match = GNUTLS_SAN_RFC822NAME; mod += 4; }
+  else continue;
+
+  if (*mod++ != ',')
+    break;
+  }
+
+for(index = 0;; index++)
+  {
+  siz = 0;
+  switch(ret = gnutls_x509_crt_get_subject_alt_name(
+      (gnutls_x509_crt_t)cert, index, NULL, &siz, NULL))
+    {
+    case GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE:
+      return list;     /* no more elements; normal exit */
+
+    case GNUTLS_E_SHORT_MEMORY_BUFFER:
+      break;
+
+    default:
+      return g_err("gs0", __FUNCTION__, ret);
+    }
+
+  ele = store_get(siz+1);
+  if ((ret = gnutls_x509_crt_get_subject_alt_name(
+    (gnutls_x509_crt_t)cert, index, ele, &siz, NULL)) < 0)
+    return g_err("gs1", __FUNCTION__, ret);
+  ele[siz] = '\0';
+
+  if (  match != -1 && match != ret    /* wrong type of SAN */
+     || Ustrlen(ele) != siz)           /* contains a NUL */
+    continue;
+  switch (ret)
+    {
+    case GNUTLS_SAN_DNSNAME:    tag = US"DNS";  break;
+    case GNUTLS_SAN_URI:        tag = US"URI";  break; 
+    case GNUTLS_SAN_RFC822NAME: tag = US"MAIL"; break;
+    default: continue;        /* ignore unrecognised types */
+    }
+  list = string_append_listele(list, sep, 
+          match == -1 ? string_sprintf("%s=%s", tag, ele) : ele);
+  }
+/*NOTREACHED*/
+}
+
+uschar *
+tls_cert_ocsp_uri(void * cert, uschar * mod)
+{
+#if GNUTLS_VERSION_NUMBER >= 0x030000
+gnutls_datum_t uri;
+int ret;
+uschar sep = '\n';
+int index;
+uschar * list = NULL;
+
+if (mod)
+  if (*mod == '>' && *++mod) sep = *mod++;
+
+for(index = 0;; index++)
+  {
+  ret = gnutls_x509_crt_get_authority_info_access((gnutls_x509_crt_t)cert,
+         index, GNUTLS_IA_OCSP_URI, &uri, NULL);
+
+  if (ret == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE)
+    return list;
+  if (ret < 0)
+    return g_err("gai", __FUNCTION__, ret);
+
+  list = string_append_listele(list, sep,
+           string_copyn(uri.data, uri.size));
+  }
+/*NOTREACHED*/
+
+#else
+
+expand_string_message = 
+  string_sprintf("%s: OCSP support with GnuTLS requires version 3.0.0\n",
+    __FUNCTION__);
+return NULL;
+
+#endif
+}
+
+uschar *
+tls_cert_crl_uri(void * cert, uschar * mod)
+{
+int ret;
+size_t siz;
+uschar sep = '\n';
+int index;
+uschar * list = NULL;
+uschar * ele;
+
+if (mod)
+  if (*mod == '>' && *++mod) sep = *mod++;
+
+for(index = 0;; index++)
+  {
+  siz = 0;
+  switch(ret = gnutls_x509_crt_get_crl_dist_points(
+    (gnutls_x509_crt_t)cert, index, NULL, &siz, NULL, NULL))
+    {
+    case GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE:
+      return list;
+    case GNUTLS_E_SHORT_MEMORY_BUFFER:
+      break;
+    default:
+      return g_err("gc0", __FUNCTION__, ret);
+    }
+
+  ele = store_get(siz+1);
+  if ((ret = gnutls_x509_crt_get_crl_dist_points(
+      (gnutls_x509_crt_t)cert, index, ele, &siz, NULL, NULL)) < 0)
+    return g_err("gc1", __FUNCTION__, ret);
+
+  ele[siz] = '\0';
+  list = string_append_listele(list, sep, ele);
+  }
+/*NOTREACHED*/
+}
+
+
+/*****************************************************
+*  Certificate operator routines
+*****************************************************/
+static uschar *
+fingerprint(gnutls_x509_crt_t cert, gnutls_digest_algorithm_t algo)
+{
+int ret;
+size_t siz = 0;
+uschar * cp;
+uschar * cp2;
+uschar * cp3;
+
+if ((ret = gnutls_x509_crt_get_fingerprint(cert, algo, NULL, &siz))
+    != GNUTLS_E_SHORT_MEMORY_BUFFER)
+  return g_err("gf0", __FUNCTION__, ret);
+
+cp = store_get(siz*3+1);
+if ((ret = gnutls_x509_crt_get_fingerprint(cert, algo, cp, &siz)) < 0)
+  return g_err("gf1", __FUNCTION__, ret);
+
+for (cp3 = cp2 = cp+siz; cp < cp2; cp++, cp3+=2)
+  sprintf(cp3, "%02X",*cp);
+return cp2;
+}
+
+
+uschar *
+tls_cert_fprt_md5(void * cert)
+{
+return fingerprint((gnutls_x509_crt_t)cert, GNUTLS_DIG_MD5);
+}
+
+uschar *
+tls_cert_fprt_sha1(void * cert)
+{
+return fingerprint((gnutls_x509_crt_t)cert, GNUTLS_DIG_SHA1);
+}
+
+uschar *
+tls_cert_fprt_sha256(void * cert)
+{
+return fingerprint((gnutls_x509_crt_t)cert, GNUTLS_DIG_SHA256);
+}
+
+
+/* vi: aw ai sw=2
+*/
+/* End of tlscert-gnu.c */
diff --git a/src/tlscert-openssl.c b/src/tlscert-openssl.c
new file mode 100644 (file)
index 0000000..a57980d
--- /dev/null
@@ -0,0 +1,457 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) Jeremy Harris 2014 */
+
+/* This module provides TLS (aka SSL) support for Exim using the OpenSSL
+library. It is #included into the tls.c file when that library is used.
+*/
+
+
+/* Heading stuff */
+
+#include <openssl/lhash.h>
+#include <openssl/ssl.h>
+#include <openssl/err.h>
+#include <openssl/rand.h>
+#include <openssl/x509v3.h>
+
+
+/*****************************************************
+*  Export/import a certificate, binary/printable
+*****************************************************/
+int
+tls_export_cert(uschar * buf, size_t buflen, void * cert)
+{
+BIO * bp = BIO_new(BIO_s_mem());
+int fail;
+
+if ((fail = PEM_write_bio_X509(bp, (X509 *)cert) ? 0 : 1))
+  log_write(0, LOG_MAIN, "TLS error in certificate export: %s",
+    ERR_error_string(ERR_get_error(), NULL));
+else
+  {
+  char * cp = CS buf;
+  int n;
+  buflen -= 2;
+  for(;;)
+    {
+    if ((n = BIO_gets(bp, cp, (int)buflen)) <= 0) break;
+    cp += n+1;
+    buflen -= n+1;
+    cp[-2] = '\\'; cp[-1] = 'n'; /* newline->"\n" */
+    }                           /* compat with string_printing() */
+  *cp = '\0';
+  }
+
+BIO_free(bp);
+return fail;
+}
+
+int
+tls_import_cert(const uschar * buf, void ** cert)
+{
+void * reset_point = store_get(0);
+const uschar * cp = string_unprinting(US buf);
+BIO * bp;
+X509 * x;
+int fail = 0;
+
+bp = BIO_new_mem_buf(US cp, -1);
+if (!(x = PEM_read_bio_X509(bp, NULL, 0, NULL)))
+  {
+  log_write(0, LOG_MAIN, "TLS error in certificate import: %s",
+    ERR_error_string(ERR_get_error(), NULL));
+  fail = 1;
+  }
+else
+  *cert = (void *)x;
+BIO_free(bp);
+store_reset(reset_point);
+return fail;
+}
+
+void
+tls_free_cert(void * cert)
+{
+X509_free((X509 *)cert);
+}
+
+
+/*****************************************************
+*  Certificate field extraction routines
+*****************************************************/
+
+/* First, some internal service functions */
+
+static uschar *
+badalloc(void)
+{
+expand_string_message = US"allocation failure";
+return NULL;
+}
+
+static uschar *
+bio_string_copy(BIO * bp, int len)
+{
+uschar * cp = US"";
+len = len > 0 ? (int) BIO_get_mem_data(bp, &cp) : 0;
+cp = string_copyn(cp, len);
+BIO_free(bp);
+return cp;
+}
+
+static uschar *
+bio_string_time_to_int(BIO * bp, int len)
+{
+uschar * cp = US"";
+struct tm t;
+len = len > 0 ? (int) BIO_get_mem_data(bp, &cp) : 0;
+/*XXX %Z might be glibc-specific? */
+(void) strptime(CS cp, "%b%t%e%t%T%t%Y%t%Z", &t);
+BIO_free(bp);
+/*XXX timegm might not be portable? */
+return string_sprintf("%u", (unsigned) timegm(&t));
+}
+
+static uschar *
+asn1_time_copy(const ASN1_TIME * time, uschar * mod)
+{
+BIO * bp = BIO_new(BIO_s_mem());
+int len;
+
+if (!bp) return badalloc();
+
+len = ASN1_TIME_print(bp, time);
+return mod &&  Ustrcmp(mod, "int") == 0
+  ? bio_string_time_to_int(bp, len)
+  : bio_string_copy(bp, len);
+}
+
+static uschar *
+x509_name_copy(X509_NAME * name)
+{
+BIO * bp = BIO_new(BIO_s_mem());
+int len_good;
+
+if (!bp) return badalloc();
+
+len_good =
+  X509_NAME_print_ex(bp, name, 0, XN_FLAG_RFC2253) >= 0
+  ? 1 : 0;
+return bio_string_copy(bp, len_good);
+}
+
+/**/
+/* Now the extractors, called from expand.c
+Arguments:
+  cert         The certificate
+  mod          Optional modifiers for the operator
+
+Return:
+  Allocated string with extracted value
+*/
+
+uschar *
+tls_cert_issuer(void * cert, uschar * mod)
+{
+uschar * cp = x509_name_copy(X509_get_issuer_name((X509 *)cert));
+return mod ? tls_field_from_dn(cp, mod) : cp;
+}
+
+uschar *
+tls_cert_not_before(void * cert, uschar * mod)
+{
+return asn1_time_copy(X509_get_notBefore((X509 *)cert), mod);
+}
+
+uschar *
+tls_cert_not_after(void * cert, uschar * mod)
+{
+return asn1_time_copy(X509_get_notAfter((X509 *)cert), mod);
+}
+
+uschar *
+tls_cert_serial_number(void * cert, uschar * mod)
+{
+uschar txt[256];
+BIO * bp = BIO_new(BIO_s_mem());
+int len;
+
+if (!bp) return badalloc();
+
+len = i2a_ASN1_INTEGER(bp, X509_get_serialNumber((X509 *)cert));
+if (len < sizeof(txt))
+  BIO_read(bp, txt, len);
+else
+  len = 0;
+BIO_free(bp);
+return string_copynlc(txt, len);       /* lowercase */
+}
+
+uschar *
+tls_cert_signature(void * cert, uschar * mod)
+{
+uschar * cp = NULL;
+BIO * bp = BIO_new(BIO_s_mem());
+
+if (!bp) return badalloc();
+
+if (X509_print_ex(bp, (X509 *)cert, 0,
+  X509_FLAG_NO_HEADER | X509_FLAG_NO_VERSION | X509_FLAG_NO_SERIAL | 
+  X509_FLAG_NO_SIGNAME | X509_FLAG_NO_ISSUER | X509_FLAG_NO_VALIDITY | 
+  X509_FLAG_NO_SUBJECT | X509_FLAG_NO_PUBKEY | X509_FLAG_NO_EXTENSIONS | 
+  /* X509_FLAG_NO_SIGDUMP is the missing one */
+  X509_FLAG_NO_AUX) == 1)
+  {
+  long len = BIO_get_mem_data(bp, &cp);
+
+  /* Strip leading "Signature Algorithm" line */
+  while (*cp && *cp != '\n') { cp++; len--; }
+
+  cp = string_copyn(cp+1, len-1);
+  }
+BIO_free(bp);
+return cp;
+}
+
+uschar *
+tls_cert_signature_algorithm(void * cert, uschar * mod)
+{
+uschar * cp = NULL;
+BIO * bp = BIO_new(BIO_s_mem());
+
+if (!bp) return badalloc();
+
+if (X509_print_ex(bp, (X509 *)cert, 0,
+  X509_FLAG_NO_HEADER | X509_FLAG_NO_VERSION | X509_FLAG_NO_SERIAL | 
+  /* X509_FLAG_NO_SIGNAME is the missing one */
+  X509_FLAG_NO_ISSUER | X509_FLAG_NO_VALIDITY | 
+  X509_FLAG_NO_SUBJECT | X509_FLAG_NO_PUBKEY | X509_FLAG_NO_EXTENSIONS | 
+  X509_FLAG_NO_SIGDUMP | X509_FLAG_NO_AUX) == 1)
+  {
+  long len = BIO_get_mem_data(bp, &cp);
+
+  /* Strip leading "    Signature Algorithm: " and trailing newline */
+  while (*cp && *cp != ':') { cp++; len--; }
+  do { cp++; len--; } while (*cp && *cp == ' ');
+  if (cp[len-1] == '\n') len--;
+
+  cp = string_copyn(cp, len);
+  }
+BIO_free(bp);
+return cp;
+}
+
+uschar *
+tls_cert_subject(void * cert, uschar * mod)
+{
+uschar * cp = x509_name_copy(X509_get_subject_name((X509 *)cert));
+return mod ? tls_field_from_dn(cp, mod) : cp;
+}
+
+uschar *
+tls_cert_version(void * cert, uschar * mod)
+{
+return string_sprintf("%d", X509_get_version((X509 *)cert));
+}
+
+uschar *
+tls_cert_ext_by_oid(void * cert, uschar * oid, int idx)
+{
+int nid = OBJ_create(CS oid, "", "");
+int nidx = X509_get_ext_by_NID((X509 *)cert, nid, idx);
+X509_EXTENSION * ex = X509_get_ext((X509 *)cert, nidx);
+ASN1_OCTET_STRING * adata = X509_EXTENSION_get_data(ex);
+BIO * bp = BIO_new(BIO_s_mem());
+long len;
+uschar * cp1;
+uschar * cp2;
+uschar * cp3;
+
+if (!bp) return badalloc();
+
+M_ASN1_OCTET_STRING_print(bp, adata);
+/* binary data, DER encoded */
+
+/* just dump for now */
+len = BIO_get_mem_data(bp, &cp1);
+cp3 = cp2 = store_get(len*3+1);
+
+while(len)
+  {
+  sprintf(CS cp2, "%.2x ", *cp1++);
+  cp2 += 3;
+  len--;
+  }
+cp2[-1] = '\0';
+
+return cp3;
+}
+
+uschar *
+tls_cert_subject_altname(void * cert, uschar * mod)
+{
+uschar * list = NULL;
+STACK_OF(GENERAL_NAME) * san = (STACK_OF(GENERAL_NAME) *)
+  X509_get_ext_d2i((X509 *)cert, NID_subject_alt_name, NULL, NULL);
+uschar sep = '\n';
+uschar * tag = US"";
+uschar * ele;
+int match = -1;
+int len;
+
+if (!san) return NULL;
+
+while (mod)
+  {
+  if (*mod == '>' && *++mod) sep = *mod++;
+  else if (Ustrcmp(mod, "dns")==0) { match = GEN_DNS; mod += 3; }
+  else if (Ustrcmp(mod, "uri")==0) { match = GEN_URI; mod += 3; }
+  else if (Ustrcmp(mod, "mail")==0) { match = GEN_EMAIL; mod += 4; }
+  else continue;
+
+  if (*mod++ != ',')
+    break;
+  }
+
+while (sk_GENERAL_NAME_num(san) > 0)
+  {
+  GENERAL_NAME * namePart = sk_GENERAL_NAME_pop(san);
+  if (match != -1 && match != namePart->type)
+    continue;
+  switch (namePart->type)
+    {
+    case GEN_DNS:
+      tag = US"DNS";
+      ele = ASN1_STRING_data(namePart->d.dNSName);
+      len = ASN1_STRING_length(namePart->d.dNSName);
+      break;
+    case GEN_URI:
+      tag = US"URI";
+      ele = ASN1_STRING_data(namePart->d.uniformResourceIdentifier);
+      len = ASN1_STRING_length(namePart->d.uniformResourceIdentifier);
+      break;
+    case GEN_EMAIL:
+      tag = US"MAIL";
+      ele = ASN1_STRING_data(namePart->d.rfc822Name);
+      len = ASN1_STRING_length(namePart->d.rfc822Name);
+      break;
+    default:
+      continue;        /* ignore unrecognised types */
+    }
+  if (ele[len])        /* not nul-terminated */
+    ele = string_copyn(ele, len);
+
+  if (strnlen(CS ele, len) == len)     /* ignore any with embedded nul */
+    list = string_append_listele(list, sep,
+         match == -1 ? string_sprintf("%s=%s", tag, ele) : ele);
+  }
+
+sk_GENERAL_NAME_free(san);
+return list;
+}
+
+uschar *
+tls_cert_ocsp_uri(void * cert, uschar * mod)
+{
+STACK_OF(ACCESS_DESCRIPTION) * ads = (STACK_OF(ACCESS_DESCRIPTION) *)
+  X509_get_ext_d2i((X509 *)cert, NID_info_access, NULL, NULL);
+int adsnum = sk_ACCESS_DESCRIPTION_num(ads);
+int i;
+uschar sep = '\n';
+uschar * list = NULL;
+
+if (mod)
+  if (*mod == '>' && *++mod) sep = *mod++;
+
+for (i = 0; i < adsnum; i++)
+  {
+  ACCESS_DESCRIPTION * ad = sk_ACCESS_DESCRIPTION_value(ads, i);
+
+  if (ad && OBJ_obj2nid(ad->method) == NID_ad_OCSP)
+    list = string_append_listele(list, sep,
+             ASN1_STRING_data(ad->location->d.ia5));
+  }
+return list;
+}
+
+uschar *
+tls_cert_crl_uri(void * cert, uschar * mod)
+{
+STACK_OF(DIST_POINT) * dps = (STACK_OF(DIST_POINT) *)
+  X509_get_ext_d2i((X509 *)cert,  NID_crl_distribution_points,
+    NULL, NULL);
+DIST_POINT * dp;
+int dpsnum = sk_DIST_POINT_num(dps);
+int i;
+uschar sep = '\n';
+uschar * list = NULL;
+
+if (mod)
+  if (*mod == '>' && *++mod) sep = *mod++;
+
+if (dps) for (i = 0; i < dpsnum; i++)
+  if ((dp = sk_DIST_POINT_value(dps, i)))
+    {
+    STACK_OF(GENERAL_NAME) * names = dp->distpoint->name.fullname;
+    GENERAL_NAME * np;
+    int nnum = sk_GENERAL_NAME_num(names);
+    int j;
+
+    for (j = 0; j < nnum; j++)
+      if (  (np = sk_GENERAL_NAME_value(names, j))
+        && np->type == GEN_URI
+        )
+       list = string_append_listele(list, sep,
+               ASN1_STRING_data(np->d.uniformResourceIdentifier));
+    }
+return list;
+}
+
+
+
+/*****************************************************
+*  Certificate operator routines
+*****************************************************/
+static uschar *
+fingerprint(X509 * cert, const EVP_MD * fdig)
+{
+int j;
+unsigned int n;
+uschar md[EVP_MAX_MD_SIZE];
+uschar * cp;
+
+if (!X509_digest(cert,fdig,md,&n))
+  {
+  expand_string_message = US"tls_cert_fprt: out of mem\n";
+  return NULL;
+  }
+cp = store_get(n*2+1);
+for (j = 0; j < (int)n; j++) sprintf(CS cp+2*j, "%02X", md[j]);
+return(cp);
+}
+
+uschar * 
+tls_cert_fprt_md5(void * cert)
+{
+return fingerprint((X509 *)cert, EVP_md5());
+}
+
+uschar * 
+tls_cert_fprt_sha1(void * cert)
+{
+return fingerprint((X509 *)cert, EVP_sha1());
+}
+
+uschar * 
+tls_cert_fprt_sha256(void * cert)
+{
+return fingerprint((X509 *)cert, EVP_sha256());
+}
+
+
+/* vi: aw ai sw=2
+*/
+/* End of tlscert-openssl.c */
diff --git a/src/tod.c b/src/tod.c
new file mode 100644 (file)
index 0000000..0297e37
--- /dev/null
+++ b/src/tod.c
@@ -0,0 +1,211 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* A function for returning the time of day in various formats */
+
+
+#include "exim.h"
+
+/* #define TESTING_LOG_DATESTAMP */
+
+
+static uschar timebuf[sizeof("www, dd-mmm-yyyy hh:mm:ss +zzzz")];
+
+
+/*************************************************
+*                Return timestamp                *
+*************************************************/
+
+/* The log timestamp format is dd-mmm-yy so as to be non-confusing on both
+sides of the Atlantic. We calculate an explicit numerical offset from GMT for
+the full datestamp and BSD inbox datestamp. Note that on some systems
+localtime() and gmtime() re-use the same store, so we must save the local time
+values before calling gmtime(). If timestamps_utc is set, don't use
+localtime(); all times are then in UTC (with offset +0000).
+
+There are also some contortions to get the day of the month without
+a leading zero for the full stamp, since Ustrftime() doesn't provide this
+option.
+
+Argument:  type of timestamp required:
+             tod_bsdin                  BSD inbox format
+             tod_epoch                  Unix epoch format
+             tod_epochl                 Unix epoch/usec format
+             tod_full                   full date and time
+             tod_log                    log file data line format,
+                                          with zone if log_timezone is TRUE
+             tod_log_bare               always without zone
+             tod_log_datestamp_daily    for log file names when datestamped daily
+             tod_log_datestamp_monthly  for log file names when datestamped monthly
+             tod_log_zone               always with zone
+             tod_mbx                    MBX inbox format
+             tod_zone                   just the timezone offset
+             tod_zulu                   time in 8601 zulu format
+
+Returns:   pointer to fixed buffer containing the timestamp
+*/
+
+uschar *
+tod_stamp(int type)
+{
+time_t now;
+struct tm *t;
+
+if (type == tod_epoch_l)
+  {
+  struct timeval tv;
+  gettimeofday(&tv, NULL);
+  /* Unix epoch/usec format */
+  (void) sprintf(CS timebuf, "%ld%06ld", tv.tv_sec, (long) tv.tv_usec );
+  return timebuf;
+  }
+
+now = time(NULL);
+
+/* Vary log type according to timezone requirement */
+
+if (type == tod_log) type = log_timezone? tod_log_zone : tod_log_bare;
+
+/* Styles that don't need local time */
+
+else if (type == tod_epoch)
+  {
+  (void) sprintf(CS timebuf, TIME_T_FMT, now);  /* Unix epoch format */
+  return timebuf;      /* NB the above will be wrong if time_t is FP */
+  }
+
+else if (type == tod_zulu)
+  {
+  t = gmtime(&now);
+  (void) sprintf(CS timebuf, "%04d%02d%02d%02d%02d%02dZ",
+    1900 + t->tm_year, 1 + t->tm_mon, t->tm_mday, t->tm_hour, t->tm_min,
+    t->tm_sec);
+  return timebuf;
+  }
+
+/* Convert to local time or UTC */
+
+t = timestamps_utc? gmtime(&now) : localtime(&now);
+
+switch(type)
+  {
+  case tod_log_bare:          /* Format used in logging without timezone */
+  (void) sprintf(CS timebuf, "%04d-%02d-%02d %02d:%02d:%02d",
+    1900 + t->tm_year, 1 + t->tm_mon, t->tm_mday,
+    t->tm_hour, t->tm_min, t->tm_sec);
+  break;
+
+  /* Format used as suffix of log file name when 'log_datestamp' is active. For
+  testing purposes, it changes the file every second. */
+
+  #ifdef TESTING_LOG_DATESTAMP
+  case tod_log_datestamp_daily:
+  case tod_log_datestamp_monthly:
+  (void) sprintf(CS timebuf, "%04d%02d%02d%02d%02d",
+    1900 + t->tm_year, 1 + t->tm_mon, t->tm_mday, t->tm_hour, t->tm_min);
+  break;
+
+  #else
+  case tod_log_datestamp_daily:
+  (void) sprintf(CS timebuf, "%04d%02d%02d",
+    1900 + t->tm_year, 1 + t->tm_mon, t->tm_mday);
+  break;
+
+  case tod_log_datestamp_monthly:
+  (void) sprintf(CS timebuf, "%04d%02d",
+    1900 + t->tm_year, 1 + t->tm_mon);
+  break;
+  #endif
+
+  /* Format used in BSD inbox separator lines. Sort-of documented in RFC 976
+  ("UUCP Mail Interchange Format Standard") but only by example, not by
+  explicit definition. The examples show no timezone offsets, and some MUAs
+  appear to be sensitive to this, so Exim has been changed to remove the
+  timezone offsets that originally appeared. */
+
+  case tod_bsdin:
+    {
+    int len = Ustrftime(timebuf, sizeof(timebuf), "%a %b %d %H:%M:%S", t);
+    Ustrftime(timebuf + len, sizeof(timebuf) - len, " %Y", t);
+    }
+  break;
+
+  /* Other types require the GMT offset to be calculated, or just set up in the
+  case of UTC timestamping. We need to take a copy of the local time first. */
+
+  default:
+    {
+    int diff_hour, diff_min;
+    struct tm local;
+    memcpy(&local, t, sizeof(struct tm));
+
+    if (timestamps_utc)
+      {
+      diff_hour = diff_min = 0;
+      }
+    else
+      {
+      struct tm *gmt = gmtime(&now);
+      diff_min = 60*(local.tm_hour - gmt->tm_hour) + local.tm_min - gmt->tm_min;
+      if (local.tm_year != gmt->tm_year)
+        diff_min += (local.tm_year > gmt->tm_year)? 1440 : -1440;
+      else if (local.tm_yday != gmt->tm_yday)
+        diff_min += (local.tm_yday > gmt->tm_yday)? 1440 : -1440;
+      diff_hour = diff_min/60;
+      diff_min  = abs(diff_min - diff_hour*60);
+      }
+
+    switch(type)
+      {
+      case tod_log_zone:          /* Format used in logging with timezone */
+      (void) sprintf(CS timebuf, "%04d-%02d-%02d %02d:%02d:%02d %+03d%02d",
+        1900 + local.tm_year, 1 + local.tm_mon, local.tm_mday,
+        local.tm_hour, local.tm_min, local.tm_sec,
+        diff_hour, diff_min);
+      break;
+
+      case tod_zone:              /* Just the timezone offset */
+      (void) sprintf(CS timebuf, "%+03d%02d", diff_hour, diff_min);
+      break;
+
+      /* tod_mbx: format used in MBX mailboxes - subtly different to tod_full */
+
+      #ifdef SUPPORT_MBX
+      case tod_mbx:
+        {
+        int len;
+        (void) sprintf(CS timebuf, "%02d-", local.tm_mday);
+        len = Ustrlen(timebuf);
+        len += Ustrftime(timebuf + len, sizeof(timebuf) - len, "%b-%Y %H:%M:%S",
+          &local);
+        (void) sprintf(CS timebuf + len, " %+03d%02d", diff_hour, diff_min);
+        }
+      break;
+      #endif
+
+      /* tod_full: format used in Received: headers (use as default just in case
+      called with a junk type value) */
+
+      default:
+        {
+        int len = Ustrftime(timebuf, sizeof(timebuf), "%a, ", &local);
+        (void) sprintf(CS timebuf + len, "%02d ", local.tm_mday);
+        len += Ustrlen(timebuf + len);
+        len += Ustrftime(timebuf + len, sizeof(timebuf) - len, "%b %Y %H:%M:%S",
+          &local);
+        (void) sprintf(CS timebuf + len, " %+03d%02d", diff_hour, diff_min);
+        }
+      break;
+      }
+    }
+  break;
+  }
+
+return timebuf;
+}
+
+/* End of tod.c */
diff --git a/src/transport-filter.src b/src/transport-filter.src
new file mode 100644 (file)
index 0000000..92a3b9d
--- /dev/null
@@ -0,0 +1,83 @@
+#! PERL_COMMAND -w
+
+# This is a Perl script to demonstrate the possibilities of on-the-fly
+# delivery filtering in Exim. It is presented with a message on its standard
+# input, and must copy it to the standard output, transforming it as it
+# pleases, but of course it must keep to the syntax of RFC 822 for the headers.
+
+# The filter is run before any SMTP-specific processing, such as turning
+# \n into \r\n and escaping lines beginning with a dot.
+#
+# Philip Hazel, May 1997
+#############################################################################
+
+
+# If the filter is called with any arguments, insert them into the message
+# as X-Arg headers, just to verify what they are.
+
+for ($ac = 0; $ac < @ARGV; $ac++)
+  {
+  printf("X-Arg%d: %s\n", $ac, $ARGV[$ac]);
+  }
+
+# Now read the header portion of the message; this is easy to do in Perl
+
+$/ = "";                    # set paragraph mode
+chomp($headers = <STDIN>);  # read a paragraph, remove trailing newlines
+$/ = "\n";                  # unset paragraph mode
+
+# Splitting up a sequence of unique headers is easy to do in Perl, but a
+# message may contain duplicate headers of various kinds. It is better
+# to extract the headers one wants from the whole paragraph, do any appropriate
+# munging, and then put them back (unless removing them altogether). Messing
+# with "Received:" headers is not in any case to be encouraged.
+
+# As a demonstration, we extract the "From:" header, add a textual comment
+# to it, and put it back.
+
+($pre, $from, $post) =
+  $headers =~ /^(|(?:.|\n)+\n)   (?# Stuff preceding the From header,
+                                     which is either null, or any number
+                                     of characters, including \n, ending
+                                     with \n.)
+               From:[\s\t]*      (?# Header name, with optional space or tab.)
+               ((?:.|\n)*?)      (?# Header body, which contains any chars,
+                                     including \n, but we want to make it as
+                                     short as possible so as not to include
+                                     following headers by mistake.)
+               (|\n\S(?:.|\n)*)$ (?# Header terminates at end or at \n followed
+                                     by a non-whitespace character and
+                                     remaining headers.)
+              /ix;                #  case independent, regular expression,
+                                  #  use extended features (ignore whitespace)
+
+# Only do something if there was a From: header, of course. It has been
+# extracted without the final \n, which is on the front of the $post
+# variable.
+
+if ($pre)
+  {
+  $headers = $pre . "From: $from (this is an added comment)" . $post;
+  }
+
+# Add a new header to the end of the headers; remember that the final
+# \n isn't there.
+
+$headers .= "\nX-Comment: Message munged";
+
+# Write out the processed headers, plus a blank line to separate them from
+# the body.
+
+printf(STDOUT "%s\n\n", $headers);
+
+# As a demonstration of munging the body of a message, reverse all the
+# characters in each line.
+
+while (<STDIN>)
+  {
+  chomp;
+  $_ = reverse($_);
+  printf(STDOUT "%s\n", $_);
+  }
+
+# End
diff --git a/src/transport.c b/src/transport.c
new file mode 100644 (file)
index 0000000..3648bfc
--- /dev/null
@@ -0,0 +1,2205 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2014 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* General functions concerned with transportation, and generic options for all
+transports. */
+
+
+#include "exim.h"
+
+#ifdef HAVE_LINUX_SENDFILE
+#include <sys/sendfile.h>
+#endif
+
+/* Structure for keeping list of addresses that have been added to
+Envelope-To:, in order to avoid duplication. */
+
+struct aci {
+  struct aci *next;
+  address_item *ptr;
+  };
+
+
+/* Static data for write_chunk() */
+
+static uschar *chunk_ptr;           /* chunk pointer */
+static uschar *nl_check;            /* string to look for at line start */
+static int     nl_check_length;     /* length of same */
+static uschar *nl_escape;           /* string to insert */
+static int     nl_escape_length;    /* length of same */
+static int     nl_partial_match;    /* length matched at chunk end */
+
+
+/* Generic options for transports, all of which live inside transport_instance
+data blocks and which therefore have the opt_public flag set. Note that there
+are other options living inside this structure which can be set only from
+certain transports. */
+
+optionlist optionlist_transports[] = {
+  { "*expand_group",    opt_stringptr|opt_hidden|opt_public,
+                 (void *)offsetof(transport_instance, expand_gid) },
+  { "*expand_user",     opt_stringptr|opt_hidden|opt_public,
+                 (void *)offsetof(transport_instance, expand_uid) },
+  { "*headers_rewrite_flags", opt_int|opt_public|opt_hidden,
+                 (void *)offsetof(transport_instance, rewrite_existflags) },
+  { "*headers_rewrite_rules", opt_void|opt_public|opt_hidden,
+                 (void *)offsetof(transport_instance, rewrite_rules) },
+  { "*set_group",       opt_bool|opt_hidden|opt_public,
+                 (void *)offsetof(transport_instance, gid_set) },
+  { "*set_user",        opt_bool|opt_hidden|opt_public,
+                 (void *)offsetof(transport_instance, uid_set) },
+  { "body_only",        opt_bool|opt_public,
+                 (void *)offsetof(transport_instance, body_only) },
+  { "current_directory", opt_stringptr|opt_public,
+                 (void *)offsetof(transport_instance, current_dir) },
+  { "debug_print",      opt_stringptr | opt_public,
+                 (void *)offsetof(transport_instance, debug_string) },
+  { "delivery_date_add", opt_bool|opt_public,
+                 (void *)(offsetof(transport_instance, delivery_date_add)) },
+  { "disable_logging",  opt_bool|opt_public,
+                 (void *)(offsetof(transport_instance, disable_logging)) },
+  { "driver",           opt_stringptr|opt_public,
+                 (void *)offsetof(transport_instance, driver_name) },
+  { "envelope_to_add",   opt_bool|opt_public,
+                 (void *)(offsetof(transport_instance, envelope_to_add)) },
+  { "group",             opt_expand_gid|opt_public,
+                 (void *)offsetof(transport_instance, gid) },
+  { "headers_add",      opt_stringptr|opt_public|opt_rep_str,
+                 (void *)offsetof(transport_instance, add_headers) },
+  { "headers_only",     opt_bool|opt_public,
+                 (void *)offsetof(transport_instance, headers_only) },
+  { "headers_remove",   opt_stringptr|opt_public|opt_rep_str,
+                 (void *)offsetof(transport_instance, remove_headers) },
+  { "headers_rewrite",  opt_rewrite|opt_public,
+                 (void *)offsetof(transport_instance, headers_rewrite) },
+  { "home_directory",   opt_stringptr|opt_public,
+                 (void *)offsetof(transport_instance, home_dir) },
+  { "initgroups",       opt_bool|opt_public,
+                 (void *)offsetof(transport_instance, initgroups) },
+  { "message_size_limit", opt_stringptr|opt_public,
+                 (void *)offsetof(transport_instance, message_size_limit) },
+  { "rcpt_include_affixes", opt_bool|opt_public,
+                 (void *)offsetof(transport_instance, rcpt_include_affixes) },
+  { "retry_use_local_part", opt_bool|opt_public,
+                 (void *)offsetof(transport_instance, retry_use_local_part) },
+  { "return_path",      opt_stringptr|opt_public,
+                 (void *)(offsetof(transport_instance, return_path)) },
+  { "return_path_add",   opt_bool|opt_public,
+                 (void *)(offsetof(transport_instance, return_path_add)) },
+  { "shadow_condition", opt_stringptr|opt_public,
+                 (void *)offsetof(transport_instance, shadow_condition) },
+  { "shadow_transport", opt_stringptr|opt_public,
+                 (void *)offsetof(transport_instance, shadow) },
+#ifdef EXPERIMENTAL_TPDA
+  { "tpda_delivery_action",opt_stringptr | opt_public,
+                 (void *)offsetof(transport_instance, tpda_delivery_action) },
+#endif
+  { "transport_filter", opt_stringptr|opt_public,
+                 (void *)offsetof(transport_instance, filter_command) },
+  { "transport_filter_timeout", opt_time|opt_public,
+                 (void *)offsetof(transport_instance, filter_timeout) },
+  { "user",             opt_expand_uid|opt_public,
+                 (void *)offsetof(transport_instance, uid) }
+};
+
+int optionlist_transports_size =
+  sizeof(optionlist_transports)/sizeof(optionlist);
+
+
+/*************************************************
+*             Initialize transport list           *
+*************************************************/
+
+/* Read the transports section of the configuration file, and set up a chain of
+transport instances according to its contents. Each transport has generic
+options and may also have its own private options. This function is only ever
+called when transports == NULL. We use generic code in readconf to do most of
+the work. */
+
+void
+transport_init(void)
+{
+transport_instance *t;
+
+readconf_driver_init(US"transport",
+  (driver_instance **)(&transports),     /* chain anchor */
+  (driver_info *)transports_available,   /* available drivers */
+  sizeof(transport_info),                /* size of info block */
+  &transport_defaults,                   /* default values for generic options */
+  sizeof(transport_instance),            /* size of instance block */
+  optionlist_transports,                 /* generic options */
+  optionlist_transports_size);
+
+/* Now scan the configured transports and check inconsistencies. A shadow
+transport is permitted only for local transports. */
+
+for (t = transports; t != NULL; t = t->next)
+  {
+  if (!t->info->local)
+    {
+    if (t->shadow != NULL)
+      log_write(0, LOG_PANIC_DIE|LOG_CONFIG,
+        "shadow transport not allowed on non-local transport %s", t->name);
+    }
+
+  if (t->body_only && t->headers_only)
+    log_write(0, LOG_PANIC_DIE|LOG_CONFIG,
+      "%s transport: body_only and headers_only are mutually exclusive",
+      t->name);
+  }
+}
+
+
+
+/*************************************************
+*             Write block of data                *
+*************************************************/
+
+/* Subroutine called by write_chunk() and at the end of the message actually
+to write a data block. Also called directly by some transports to write
+additional data to the file descriptor (e.g. prefix, suffix).
+
+If a transport wants data transfers to be timed, it sets a non-zero value in
+transport_write_timeout. A non-zero transport_write_timeout causes a timer to
+be set for each block of data written from here. If time runs out, then write()
+fails and provokes an error return. The caller can then inspect sigalrm_seen to
+check for a timeout.
+
+On some systems, if a quota is exceeded during the write, the yield is the
+number of bytes written rather than an immediate error code. This also happens
+on some systems in other cases, for example a pipe that goes away because the
+other end's process terminates (Linux). On other systems, (e.g. Solaris 2) you
+get the error codes the first time.
+
+The write() function is also interruptible; the Solaris 2.6 man page says:
+
+     If write() is interrupted by a signal before it writes any
+     data, it will return -1 with errno set to EINTR.
+
+     If write() is interrupted by a signal after it successfully
+     writes some data, it will return the number of bytes written.
+
+To handle these cases, we want to restart the write() to output the remainder
+of the data after a non-negative return from write(), except after a timeout.
+In the error cases (EDQUOT, EPIPE) no bytes get written the second time, and a
+proper error then occurs. In principle, after an interruption, the second
+write() could suffer the same fate, but we do not want to continue for
+evermore, so stick a maximum repetition count on the loop to act as a
+longstop.
+
+Arguments:
+  fd        file descriptor to write to
+  block     block of bytes to write
+  len       number of bytes to write
+
+Returns:    TRUE on success, FALSE on failure (with errno preserved);
+              transport_count is incremented by the number of bytes written
+*/
+
+BOOL
+transport_write_block(int fd, uschar *block, int len)
+{
+int i, rc, save_errno;
+int local_timeout = transport_write_timeout;
+
+/* This loop is for handling incomplete writes and other retries. In most
+normal cases, it is only ever executed once. */
+
+for (i = 0; i < 100; i++)
+  {
+  DEBUG(D_transport)
+    debug_printf("writing data block fd=%d size=%d timeout=%d\n",
+      fd, len, local_timeout);
+
+  /* This code makes use of alarm() in order to implement the timeout. This
+  isn't a very tidy way of doing things. Using non-blocking I/O with select()
+  provides a neater approach. However, I don't know how to do this when TLS is
+  in use. */
+
+  if (transport_write_timeout <= 0)   /* No timeout wanted */
+    {
+    #ifdef SUPPORT_TLS
+    if (tls_out.active == fd) rc = tls_write(FALSE, block, len); else
+    #endif
+    rc = write(fd, block, len);
+    save_errno = errno;
+    }
+
+  /* Timeout wanted. */
+
+  else
+    {
+    alarm(local_timeout);
+    #ifdef SUPPORT_TLS
+    if (tls_out.active == fd) rc = tls_write(FALSE, block, len); else
+    #endif
+    rc = write(fd, block, len);
+    save_errno = errno;
+    local_timeout = alarm(0);
+    if (sigalrm_seen)
+      {
+      errno = ETIMEDOUT;
+      return FALSE;
+      }
+    }
+
+  /* Hopefully, the most common case is success, so test that first. */
+
+  if (rc == len) { transport_count += len; return TRUE; }
+
+  /* A non-negative return code is an incomplete write. Try again for the rest
+  of the block. If we have exactly hit the timeout, give up. */
+
+  if (rc >= 0)
+    {
+    len -= rc;
+    block += rc;
+    transport_count += rc;
+    DEBUG(D_transport) debug_printf("write incomplete (%d)\n", rc);
+    goto CHECK_TIMEOUT;   /* A few lines below */
+    }
+
+  /* A negative return code with an EINTR error is another form of
+  incomplete write, zero bytes having been written */
+
+  if (save_errno == EINTR)
+    {
+    DEBUG(D_transport)
+      debug_printf("write interrupted before anything written\n");
+    goto CHECK_TIMEOUT;   /* A few lines below */
+    }
+
+  /* A response of EAGAIN from write() is likely only in the case of writing
+  to a FIFO that is not swallowing the data as fast as Exim is writing it. */
+
+  if (save_errno == EAGAIN)
+    {
+    DEBUG(D_transport)
+      debug_printf("write temporarily locked out, waiting 1 sec\n");
+    sleep(1);
+
+    /* Before continuing to try another write, check that we haven't run out of
+    time. */
+
+    CHECK_TIMEOUT:
+    if (transport_write_timeout > 0 && local_timeout <= 0)
+      {
+      errno = ETIMEDOUT;
+      return FALSE;
+      }
+    continue;
+    }
+
+  /* Otherwise there's been an error */
+
+  DEBUG(D_transport) debug_printf("writing error %d: %s\n", save_errno,
+    strerror(save_errno));
+  errno = save_errno;
+  return FALSE;
+  }
+
+/* We've tried and tried and tried but still failed */
+
+errno = ERRNO_WRITEINCOMPLETE;
+return FALSE;
+}
+
+
+
+
+/*************************************************
+*             Write formatted string             *
+*************************************************/
+
+/* This is called by various transports. It is a convenience function.
+
+Arguments:
+  fd          file descriptor
+  format      string format
+  ...         arguments for format
+
+Returns:      the yield of transport_write_block()
+*/
+
+BOOL
+transport_write_string(int fd, const char *format, ...)
+{
+va_list ap;
+va_start(ap, format);
+if (!string_vformat(big_buffer, big_buffer_size, format, ap))
+  log_write(0, LOG_MAIN|LOG_PANIC_DIE, "overlong formatted string in transport");
+va_end(ap);
+return transport_write_block(fd, big_buffer, Ustrlen(big_buffer));
+}
+
+
+
+
+/*************************************************
+*              Write character chunk             *
+*************************************************/
+
+/* Subroutine used by transport_write_message() to scan character chunks for
+newlines and act appropriately. The object is to minimise the number of writes.
+The output byte stream is buffered up in deliver_out_buffer, which is written
+only when it gets full, thus minimizing write operations and TCP packets.
+
+Static data is used to handle the case when the last character of the previous
+chunk was NL, or matched part of the data that has to be escaped.
+
+Arguments:
+  fd         file descript to write to
+  chunk      pointer to data to write
+  len        length of data to write
+  usr_crlf   TRUE if CR LF is wanted at the end of each line
+
+In addition, the static nl_xxx variables must be set as required.
+
+Returns:     TRUE on success, FALSE on failure (with errno preserved)
+*/
+
+static BOOL
+write_chunk(int fd, uschar *chunk, int len, BOOL use_crlf)
+{
+uschar *start = chunk;
+uschar *end = chunk + len;
+register uschar *ptr;
+int mlen = DELIVER_OUT_BUFFER_SIZE - nl_escape_length - 2;
+
+/* The assumption is made that the check string will never stretch over move
+than one chunk since the only time there are partial matches is when copying
+the body in large buffers. There is always enough room in the buffer for an
+escape string, since the loop below ensures this for each character it
+processes, and it won't have stuck in the escape string if it left a partial
+match. */
+
+if (nl_partial_match >= 0)
+  {
+  if (nl_check_length > 0 && len >= nl_check_length &&
+      Ustrncmp(start, nl_check + nl_partial_match,
+        nl_check_length - nl_partial_match) == 0)
+    {
+    Ustrncpy(chunk_ptr, nl_escape, nl_escape_length);
+    chunk_ptr += nl_escape_length;
+    start += nl_check_length - nl_partial_match;
+    }
+
+  /* The partial match was a false one. Insert the characters carried over
+  from the previous chunk. */
+
+  else if (nl_partial_match > 0)
+    {
+    Ustrncpy(chunk_ptr, nl_check, nl_partial_match);
+    chunk_ptr += nl_partial_match;
+    }
+
+  nl_partial_match = -1;
+  }
+
+/* Now process the characters in the chunk. Whenever we hit a newline we check
+for possible escaping. The code for the non-NL route should be as fast as
+possible. */
+
+for (ptr = start; ptr < end; ptr++)
+  {
+  register int ch;
+
+  /* Flush the buffer if it has reached the threshold - we want to leave enough
+  room for the next uschar, plus a possible extra CR for an LF, plus the escape
+  string. */
+
+  if (chunk_ptr - deliver_out_buffer > mlen)
+    {
+    if (!transport_write_block(fd, deliver_out_buffer,
+          chunk_ptr - deliver_out_buffer))
+      return FALSE;
+    chunk_ptr = deliver_out_buffer;
+    }
+
+  if ((ch = *ptr) == '\n')
+    {
+    int left = end - ptr - 1;  /* count of chars left after NL */
+
+    /* Insert CR before NL if required */
+
+    if (use_crlf) *chunk_ptr++ = '\r';
+    *chunk_ptr++ = '\n';
+    transport_newlines++;
+
+    /* The check_string test (formerly "from hack") replaces the specific
+    string at the start of a line with an escape string (e.g. "From " becomes
+    ">From " or "." becomes "..". It is a case-sensitive test. The length
+    check above ensures there is always enough room to insert this string. */
+
+    if (nl_check_length > 0)
+      {
+      if (left >= nl_check_length &&
+          Ustrncmp(ptr+1, nl_check, nl_check_length) == 0)
+        {
+        Ustrncpy(chunk_ptr, nl_escape, nl_escape_length);
+        chunk_ptr += nl_escape_length;
+        ptr += nl_check_length;
+        }
+
+      /* Handle the case when there isn't enough left to match the whole
+      check string, but there may be a partial match. We remember how many
+      characters matched, and finish processing this chunk. */
+
+      else if (left <= 0) nl_partial_match = 0;
+
+      else if (Ustrncmp(ptr+1, nl_check, left) == 0)
+        {
+        nl_partial_match = left;
+        ptr = end;
+        }
+      }
+    }
+
+  /* Not a NL character */
+
+  else *chunk_ptr++ = ch;
+  }
+
+return TRUE;
+}
+
+
+
+
+/*************************************************
+*        Generate address for RCPT TO            *
+*************************************************/
+
+/* This function puts together an address for RCPT to, using the caseful
+version of the local part and the caseful version of the domain. If there is no
+prefix or suffix, or if affixes are to be retained, we can just use the
+original address. Otherwise, if there is a prefix but no suffix we can use a
+pointer into the original address. If there is a suffix, however, we have to
+build a new string.
+
+Arguments:
+  addr              the address item
+  include_affixes   TRUE if affixes are to be included
+
+Returns:            a string
+*/
+
+uschar *
+transport_rcpt_address(address_item *addr, BOOL include_affixes)
+{
+uschar *at;
+int plen, slen;
+
+if (include_affixes)
+  {
+  setflag(addr, af_include_affixes);  /* Affects logged => line */
+  return addr->address;
+  }
+
+if (addr->suffix == NULL)
+  {
+  if (addr->prefix == NULL) return addr->address;
+  return addr->address + Ustrlen(addr->prefix);
+  }
+
+at = Ustrrchr(addr->address, '@');
+plen = (addr->prefix == NULL)? 0 : Ustrlen(addr->prefix);
+slen = Ustrlen(addr->suffix);
+
+return string_sprintf("%.*s@%s", (at - addr->address - plen - slen),
+   addr->address + plen, at + 1);
+}
+
+
+/*************************************************
+*  Output Envelope-To: address & scan duplicates *
+*************************************************/
+
+/* This function is called from internal_transport_write_message() below, when
+generating an Envelope-To: header line. It checks for duplicates of the given
+address and its ancestors. When one is found, this function calls itself
+recursively, to output the envelope address of the duplicate.
+
+We want to avoid duplication in the list, which can arise for example when
+A->B,C and then both B and C alias to D. This can also happen when there are
+unseen drivers in use. So a list of addresses that have been output is kept in
+the plist variable.
+
+It is also possible to have loops in the address ancestry/duplication graph,
+for example if there are two top level addresses A and B and we have A->B,C and
+B->A. To break the loop, we use a list of processed addresses in the dlist
+variable.
+
+After handling duplication, this function outputs the progenitor of the given
+address.
+
+Arguments:
+  p         the address we are interested in
+  pplist    address of anchor of the list of addresses not to output
+  pdlist    address of anchor of the list of processed addresses
+  first     TRUE if this is the first address; set it FALSE afterwards
+  fd        the file descriptor to write to
+  use_crlf  to be passed on to write_chunk()
+
+Returns:    FALSE if writing failed
+*/
+
+static BOOL
+write_env_to(address_item *p, struct aci **pplist, struct aci **pdlist,
+  BOOL *first, int fd, BOOL use_crlf)
+{
+address_item *pp;
+struct aci *ppp;
+
+/* Do nothing if we have already handled this address. If not, remember it
+so that we don't handle it again. */
+
+for (ppp = *pdlist; ppp != NULL; ppp = ppp->next)
+  { if (p == ppp->ptr) return TRUE; }
+
+ppp = store_get(sizeof(struct aci));
+ppp->next = *pdlist;
+*pdlist = ppp;
+ppp->ptr = p;
+
+/* Now scan up the ancestry, checking for duplicates at each generation. */
+
+for (pp = p;; pp = pp->parent)
+  {
+  address_item *dup;
+  for (dup = addr_duplicate; dup != NULL; dup = dup->next)
+    {
+    if (dup->dupof != pp) continue;   /* Not a dup of our address */
+    if (!write_env_to(dup, pplist, pdlist, first, fd, use_crlf)) return FALSE;
+    }
+  if (pp->parent == NULL) break;
+  }
+
+/* Check to see if we have already output the progenitor. */
+
+for (ppp = *pplist; ppp != NULL; ppp = ppp->next)
+  { if (pp == ppp->ptr) break; }
+if (ppp != NULL) return TRUE;
+
+/* Remember what we have output, and output it. */
+
+ppp = store_get(sizeof(struct aci));
+ppp->next = *pplist;
+*pplist = ppp;
+ppp->ptr = pp;
+
+if (!(*first) && !write_chunk(fd, US",\n ", 3, use_crlf)) return FALSE;
+*first = FALSE;
+return write_chunk(fd, pp->address, Ustrlen(pp->address), use_crlf);
+}
+
+
+
+
+/* Add/remove/rewwrite headers, and send them plus the empty-line sparator.
+
+Globals:
+  header_list
+
+Arguments:
+  addr                  (chain of) addresses (for extra headers), or NULL;
+                          only the first address is used
+  fd                    file descriptor to write the message to
+  sendfn               function for output
+  use_crlf             turn NL into CR LF
+  rewrite_rules         chain of header rewriting rules
+  rewrite_existflags    flags for the rewriting rules
+
+Returns:                TRUE on success; FALSE on failure.
+*/
+BOOL
+transport_headers_send(address_item *addr, int fd, uschar *add_headers, uschar *remove_headers,
+  BOOL (*sendfn)(int fd, uschar * s, int len, BOOL use_crlf),
+  BOOL use_crlf, rewrite_rule *rewrite_rules, int rewrite_existflags)
+{
+header_line *h;
+
+/* Then the message's headers. Don't write any that are flagged as "old";
+that means they were rewritten, or are a record of envelope rewriting, or
+were removed (e.g. Bcc). If remove_headers is not null, skip any headers that
+match any entries therein.  It is a colon-sep list; expand the items
+separately and squash any empty ones.
+Then check addr->p.remove_headers too, provided that addr is not NULL. */
+
+for (h = header_list; h != NULL; h = h->next) if (h->type != htype_old)
+  {
+  int i;
+  uschar *list = remove_headers;
+
+  BOOL include_header = TRUE;
+
+  for (i = 0; i < 2; i++)    /* For remove_headers && addr->p.remove_headers */
+    {
+    if (list)
+      {
+      int sep = ':';         /* This is specified as a colon-separated list */
+      uschar *s, *ss;
+      uschar buffer[128];
+      while ((s = string_nextinlist(&list, &sep, buffer, sizeof(buffer))))
+       {
+       int len;
+
+       if (i == 0)
+         if (!(s = expand_string(s)) && !expand_string_forcedfail)
+           {
+           errno = ERRNO_CHHEADER_FAIL;
+           return FALSE;
+           }
+       len = Ustrlen(s);
+       if (strncmpic(h->text, s, len) != 0) continue;
+       ss = h->text + len;
+       while (*ss == ' ' || *ss == '\t') ss++;
+       if (*ss == ':') break;
+       }
+      if (s != NULL) { include_header = FALSE; break; }
+      }
+    if (addr != NULL) list = addr->p.remove_headers;
+    }
+
+  /* If this header is to be output, try to rewrite it if there are rewriting
+  rules. */
+
+  if (include_header)
+    {
+    if (rewrite_rules)
+      {
+      void *reset_point = store_get(0);
+      header_line *hh;
+
+      if ((hh = rewrite_header(h, NULL, NULL, rewrite_rules, rewrite_existflags, FALSE)))
+       {
+       if (!sendfn(fd, hh->text, hh->slen, use_crlf)) return FALSE;
+       store_reset(reset_point);
+       continue;     /* With the next header line */
+       }
+      }
+
+    /* Either no rewriting rules, or it didn't get rewritten */
+
+    if (!sendfn(fd, h->text, h->slen, use_crlf)) return FALSE;
+    }
+
+  /* Header removed */
+
+  else
+    {
+    DEBUG(D_transport) debug_printf("removed header line:\n%s---\n", h->text);
+    }
+  }
+
+/* Add on any address-specific headers. If there are multiple addresses,
+they will all have the same headers in order to be batched. The headers
+are chained in reverse order of adding (so several addresses from the
+same alias might share some of them) but we want to output them in the
+opposite order. This is a bit tedious, but there shouldn't be very many
+of them. We just walk the list twice, reversing the pointers each time,
+but on the second time, write out the items.
+
+Headers added to an address by a router are guaranteed to end with a newline.
+*/
+
+if (addr)
+  {
+  int i;
+  header_line *hprev = addr->p.extra_headers;
+  header_line *hnext;
+  for (i = 0; i < 2; i++)
+    {
+    for (h = hprev, hprev = NULL; h != NULL; h = hnext)
+      {
+      hnext = h->next;
+      h->next = hprev;
+      hprev = h;
+      if (i == 1)
+       {
+       if (!sendfn(fd, h->text, h->slen, use_crlf)) return FALSE;
+       DEBUG(D_transport)
+         debug_printf("added header line(s):\n%s---\n", h->text);
+       }
+      }
+    }
+  }
+
+/* If a string containing additional headers exists it is a newline-sep
+list.  Expand each item and write out the result.  This is done last so that
+if it (deliberately or accidentally) isn't in header format, it won't mess
+up any other headers. An empty string or a forced expansion failure are
+noops. An added header string from a transport may not end with a newline;
+add one if it does not. */
+
+if (add_headers)
+  {
+  int sep = '\n';
+  uschar * s;
+
+  while ((s = string_nextinlist(&add_headers, &sep, NULL, 0)))
+    if (!(s = expand_string(s)))
+      {
+      if (!expand_string_forcedfail)
+       { errno = ERRNO_CHHEADER_FAIL; return FALSE; }
+      }
+    else
+      {
+      int len = Ustrlen(s);
+      if (len > 0)
+       {
+       if (!sendfn(fd, s, len, use_crlf)) return FALSE;
+       if (s[len-1] != '\n' && !sendfn(fd, US"\n", 1, use_crlf))
+         return FALSE;
+       DEBUG(D_transport)
+         {
+         debug_printf("added header line:\n%s", s);
+         if (s[len-1] != '\n') debug_printf("\n");
+         debug_printf("---\n");
+         }
+       }
+      }
+  }
+
+/* Separate headers from body with a blank line */
+
+return sendfn(fd, US"\n", 1, use_crlf);
+}
+
+
+/*************************************************
+*                Write the message               *
+*************************************************/
+
+/* This function writes the message to the given file descriptor. The headers
+are in the in-store data structure, and the rest of the message is in the open
+file descriptor deliver_datafile. Make sure we start it at the beginning.
+
+. If add_return_path is TRUE, a "return-path:" header is added to the message,
+  containing the envelope sender's address.
+
+. If add_envelope_to is TRUE, a "envelope-to:" header is added to the message,
+  giving the top-level envelope address that caused this delivery to happen.
+
+. If add_delivery_date is TRUE, a "delivery-date:" header is added to the
+  message. It gives the time and date that delivery took place.
+
+. If check_string is not null, the start of each line is checked for that
+  string. If it is found, it is replaced by escape_string. This used to be
+  the "from hack" for files, and "smtp_dots" for escaping SMTP dots.
+
+. If use_crlf is true, newlines are turned into CRLF (SMTP output).
+
+The yield is TRUE if all went well, and FALSE if not. Exit *immediately* after
+any writing or reading error, leaving the code in errno intact. Error exits
+can include timeouts for certain transports, which are requested by setting
+transport_write_timeout non-zero.
+
+Arguments:
+  addr                  (chain of) addresses (for extra headers), or NULL;
+                          only the first address is used
+  fd                    file descriptor to write the message to
+  options               bit-wise options:
+    add_return_path       if TRUE, add a "return-path" header
+    add_envelope_to       if TRUE, add a "envelope-to" header
+    add_delivery_date     if TRUE, add a "delivery-date" header
+    use_crlf              if TRUE, turn NL into CR LF
+    end_dot               if TRUE, send a terminating "." line at the end
+    no_headers            if TRUE, omit the headers
+    no_body               if TRUE, omit the body
+  size_limit            if > 0, this is a limit to the size of message written;
+                          it is used when returning messages to their senders,
+                          and is approximate rather than exact, owing to chunk
+                          buffering
+  add_headers           a string containing one or more headers to add; it is
+                          expanded, and must be in correct RFC 822 format as
+                          it is transmitted verbatim; NULL => no additions,
+                          and so does empty string or forced expansion fail
+  remove_headers        a colon-separated list of headers to remove, or NULL
+  check_string          a string to check for at the start of lines, or NULL
+  escape_string         a string to insert in front of any check string
+  rewrite_rules         chain of header rewriting rules
+  rewrite_existflags    flags for the rewriting rules
+
+Returns:                TRUE on success; FALSE (with errno) on failure.
+                        In addition, the global variable transport_count
+                        is incremented by the number of bytes written.
+*/
+
+static BOOL
+internal_transport_write_message(address_item *addr, int fd, int options,
+  int size_limit, uschar *add_headers, uschar *remove_headers, uschar *check_string,
+  uschar *escape_string, rewrite_rule *rewrite_rules, int rewrite_existflags)
+{
+int written = 0;
+int len;
+BOOL use_crlf  = (options & topt_use_crlf)  != 0;
+
+/* Initialize pointer in output buffer. */
+
+chunk_ptr = deliver_out_buffer;
+
+/* Set up the data for start-of-line data checking and escaping */
+
+nl_partial_match = -1;
+if (check_string != NULL && escape_string != NULL)
+  {
+  nl_check = check_string;
+  nl_check_length = Ustrlen(nl_check);
+  nl_escape = escape_string;
+  nl_escape_length = Ustrlen(nl_escape);
+  }
+else nl_check_length = nl_escape_length = 0;
+
+/* Whether the escaping mechanism is applied to headers or not is controlled by
+an option (set for SMTP, not otherwise). Negate the length if not wanted till
+after the headers. */
+
+if ((options & topt_escape_headers) == 0) nl_check_length = -nl_check_length;
+
+/* Write the headers if required, including any that have to be added. If there
+are header rewriting rules, apply them. */
+
+if ((options & topt_no_headers) == 0)
+  {
+  /* Add return-path: if requested. */
+
+  if ((options & topt_add_return_path) != 0)
+    {
+    uschar buffer[ADDRESS_MAXLENGTH + 20];
+    sprintf(CS buffer, "Return-path: <%.*s>\n", ADDRESS_MAXLENGTH,
+      return_path);
+    if (!write_chunk(fd, buffer, Ustrlen(buffer), use_crlf)) return FALSE;
+    }
+
+  /* Add envelope-to: if requested */
+
+  if ((options & topt_add_envelope_to) != 0)
+    {
+    BOOL first = TRUE;
+    address_item *p;
+    struct aci *plist = NULL;
+    struct aci *dlist = NULL;
+    void *reset_point = store_get(0);
+
+    if (!write_chunk(fd, US"Envelope-to: ", 13, use_crlf)) return FALSE;
+
+    /* Pick up from all the addresses. The plist and dlist variables are
+    anchors for lists of addresses already handled; they have to be defined at
+    this level becuase write_env_to() calls itself recursively. */
+
+    for (p = addr; p != NULL; p = p->next)
+      {
+      if (!write_env_to(p, &plist, &dlist, &first, fd, use_crlf)) return FALSE;
+      }
+
+    /* Add a final newline and reset the store used for tracking duplicates */
+
+    if (!write_chunk(fd, US"\n", 1, use_crlf)) return FALSE;
+    store_reset(reset_point);
+    }
+
+  /* Add delivery-date: if requested. */
+
+  if ((options & topt_add_delivery_date) != 0)
+    {
+    uschar buffer[100];
+    sprintf(CS buffer, "Delivery-date: %s\n", tod_stamp(tod_full));
+    if (!write_chunk(fd, buffer, Ustrlen(buffer), use_crlf)) return FALSE;
+    }
+
+  /* Then the message's headers. Don't write any that are flagged as "old";
+  that means they were rewritten, or are a record of envelope rewriting, or
+  were removed (e.g. Bcc). If remove_headers is not null, skip any headers that
+  match any entries therein. Then check addr->p.remove_headers too, provided that
+  addr is not NULL. */
+  if (!transport_headers_send(addr, fd, add_headers, remove_headers, &write_chunk,
+       use_crlf, rewrite_rules, rewrite_existflags))
+    return FALSE;
+  }
+
+/* If the body is required, ensure that the data for check strings (formerly
+the "from hack") is enabled by negating the length if necessary. (It will be
+negative in cases where it isn't to apply to the headers). Then ensure the body
+is positioned at the start of its file (following the message id), then write
+it, applying the size limit if required. */
+
+if ((options & topt_no_body) == 0)
+  {
+  nl_check_length = abs(nl_check_length);
+  nl_partial_match = 0;
+  lseek(deliver_datafile, SPOOL_DATA_START_OFFSET, SEEK_SET);
+  while ((len = read(deliver_datafile, deliver_in_buffer,
+           DELIVER_IN_BUFFER_SIZE)) > 0)
+    {
+    if (!write_chunk(fd, deliver_in_buffer, len, use_crlf)) return FALSE;
+    if (size_limit > 0)
+      {
+      written += len;
+      if (written > size_limit)
+        {
+        len = 0;    /* Pretend EOF */
+        break;
+        }
+      }
+    }
+
+  /* A read error on the body will have left len == -1 and errno set. */
+
+  if (len != 0) return FALSE;
+  }
+
+/* Finished with the check string */
+
+nl_check_length = nl_escape_length = 0;
+
+/* If requested, add a terminating "." line (SMTP output). */
+
+if ((options & topt_end_dot) != 0 && !write_chunk(fd, US".\n", 2, use_crlf))
+  return FALSE;
+
+/* Write out any remaining data in the buffer before returning. */
+
+return (len = chunk_ptr - deliver_out_buffer) <= 0 ||
+  transport_write_block(fd, deliver_out_buffer, len);
+}
+
+
+#ifndef DISABLE_DKIM
+
+/***************************************************************************************************
+*    External interface to write the message, while signing it with DKIM and/or Domainkeys         *
+***************************************************************************************************/
+
+/* This function is a wrapper around transport_write_message().
+   It is only called from the smtp transport if DKIM or Domainkeys support
+   is compiled in.  The function sets up a replacement fd into a -K file,
+   then calls the normal function. This way, the exact bits that exim would
+   have put "on the wire" will end up in the file (except for TLS
+   encapsulation, which is the very very last thing). When we are done
+   signing the file, send the signed message down the original fd (or TLS fd).
+
+Arguments:
+  as for internal_transport_write_message() above, with additional arguments:
+   uschar *dkim_private_key  DKIM: The private key to use (filename or
+                                   plain data)
+   uschar *dkim_domain       DKIM: The domain to use
+   uschar *dkim_selector     DKIM: The selector to use.
+   uschar *dkim_canon        DKIM: The canonalization scheme to use,
+                                   "simple" or "relaxed"
+   uschar *dkim_strict       DKIM: What to do if signing fails:
+                                 1/true  => throw error
+                                 0/false => send anyway
+   uschar *dkim_sign_headers DKIM: List of headers that should be included
+                                   in signature generation
+
+Returns:       TRUE on success; FALSE (with errno) for any failure
+*/
+
+BOOL
+dkim_transport_write_message(address_item *addr, int fd, int options,
+  int size_limit, uschar *add_headers, uschar *remove_headers,
+  uschar *check_string, uschar *escape_string, rewrite_rule *rewrite_rules,
+  int rewrite_existflags, uschar *dkim_private_key, uschar *dkim_domain,
+  uschar *dkim_selector, uschar *dkim_canon, uschar *dkim_strict, uschar *dkim_sign_headers
+  )
+{
+int dkim_fd;
+int save_errno = 0;
+BOOL rc;
+uschar dkim_spool_name[256];
+char sbuf[2048];
+int sread = 0;
+int wwritten = 0;
+uschar *dkim_signature = NULL;
+off_t size = 0;
+
+/* If we can't sign, just call the original function. */
+
+if (!(dkim_private_key && dkim_domain && dkim_selector))
+  return transport_write_message(addr, fd, options,
+           size_limit, add_headers, remove_headers,
+           check_string, escape_string, rewrite_rules,
+           rewrite_existflags);
+
+(void)string_format(dkim_spool_name, 256, "%s/input/%s/%s-%d-K",
+       spool_directory, message_subdir, message_id, (int)getpid());
+
+if ((dkim_fd = Uopen(dkim_spool_name, O_RDWR|O_CREAT|O_TRUNC, SPOOL_MODE)) < 0)
+  {
+  /* Can't create spool file. Ugh. */
+  rc = FALSE;
+  save_errno = errno;
+  goto CLEANUP;
+  }
+
+/* Call original function to write the -K file */
+
+rc = transport_write_message(addr, dkim_fd, options,
+  size_limit, add_headers, remove_headers,
+  check_string, escape_string, rewrite_rules,
+  rewrite_existflags);
+
+/* Save error state. We must clean up before returning. */
+if (!rc)
+  {
+  save_errno = errno;
+  goto CLEANUP;
+  }
+
+if (dkim_private_key && dkim_domain && dkim_selector)
+  {
+  /* Rewind file and feed it to the goats^W DKIM lib */
+  lseek(dkim_fd, 0, SEEK_SET);
+  dkim_signature = dkim_exim_sign(dkim_fd,
+                                 dkim_private_key,
+                                 dkim_domain,
+                                 dkim_selector,
+                                 dkim_canon,
+                                 dkim_sign_headers);
+  if (!dkim_signature)
+    {
+    if (dkim_strict)
+      {
+      uschar *dkim_strict_result = expand_string(dkim_strict);
+      if (dkim_strict_result)
+       if ( (strcmpic(dkim_strict,US"1") == 0) ||
+            (strcmpic(dkim_strict,US"true") == 0) ) 
+         {
+         /* Set errno to something halfway meaningful */
+         save_errno = EACCES;
+         log_write(0, LOG_MAIN, "DKIM: message could not be signed,"
+           " and dkim_strict is set. Deferring message delivery.");
+         rc = FALSE;
+         goto CLEANUP;
+         }
+      }
+    }
+  else
+    {
+    int siglen = Ustrlen(dkim_signature);
+    while(siglen > 0)
+      {
+#ifdef SUPPORT_TLS
+      wwritten = tls_out.active == fd
+       ? tls_write(FALSE, dkim_signature, siglen)
+       : write(fd, dkim_signature, siglen);
+#else
+      wwritten = write(fd, dkim_signature, siglen);
+#endif
+      if (wwritten == -1)
+        {
+       /* error, bail out */
+       save_errno = errno;
+       rc = FALSE;
+       goto CLEANUP;
+       }
+      siglen -= wwritten;
+      dkim_signature += wwritten;
+      }
+    }
+  }
+
+/* Fetch file size */
+size = lseek(dkim_fd, 0, SEEK_END);
+
+/* Rewind file */
+lseek(dkim_fd, 0, SEEK_SET);
+
+#ifdef HAVE_LINUX_SENDFILE
+/* We can use sendfile() to shove the file contents
+   to the socket. However only if we don't use TLS,
+   as then there's another layer of indirection
+   before the data finally hits the socket. */
+if (tls_out.active != fd)
+  {
+  ssize_t copied = 0;
+  off_t offset = 0;
+  while(copied >= 0 && offset < size)
+    copied = sendfile(fd, dkim_fd, &offset, size - offset);
+  if (copied < 0)
+    {
+    save_errno = errno;
+    rc = FALSE;
+    }
+  goto CLEANUP;
+  }
+#endif
+
+/* Send file down the original fd */
+while((sread = read(dkim_fd, sbuf, 2048)) > 0)
+  {
+  char *p = sbuf;
+  /* write the chunk */
+
+  while (sread)
+    {
+#ifdef SUPPORT_TLS
+    wwritten = tls_out.active == fd
+      ? tls_write(FALSE, US p, sread)
+      : write(fd, p, sread);
+#else
+    wwritten = write(fd, p, sread);
+#endif
+    if (wwritten == -1)
+      {
+      /* error, bail out */
+      save_errno = errno;
+      rc = FALSE;
+      goto CLEANUP;
+      }
+    p += wwritten;
+    sread -= wwritten;
+    }
+  }
+
+if (sread == -1)
+  {
+  save_errno = errno;
+  rc = FALSE;
+  goto CLEANUP;
+  }
+
+CLEANUP:
+/* unlink -K file */
+(void)close(dkim_fd);
+Uunlink(dkim_spool_name);
+errno = save_errno;
+return rc;
+}
+
+#endif
+
+
+
+/*************************************************
+*    External interface to write the message     *
+*************************************************/
+
+/* If there is no filtering required, call the internal function above to do
+the real work, passing over all the arguments from this function. Otherwise,
+set up a filtering process, fork another process to call the internal function
+to write to the filter, and in this process just suck from the filter and write
+down the given fd. At the end, tidy up the pipes and the processes.
+
+Arguments:     as for internal_transport_write_message() above
+
+Returns:       TRUE on success; FALSE (with errno) for any failure
+               transport_count is incremented by the number of bytes written
+*/
+
+BOOL
+transport_write_message(address_item *addr, int fd, int options,
+  int size_limit, uschar *add_headers, uschar *remove_headers,
+  uschar *check_string, uschar *escape_string, rewrite_rule *rewrite_rules,
+  int rewrite_existflags)
+{
+BOOL use_crlf;
+BOOL last_filter_was_NL = TRUE;
+int rc, len, yield, fd_read, fd_write, save_errno;
+int pfd[2];
+pid_t filter_pid, write_pid;
+
+transport_filter_timed_out = FALSE;
+
+/* If there is no filter command set up, call the internal function that does
+the actual work, passing it the incoming fd, and return its result. */
+
+if (transport_filter_argv == NULL)
+  return internal_transport_write_message(addr, fd, options, size_limit,
+    add_headers, remove_headers, check_string, escape_string,
+    rewrite_rules, rewrite_existflags);
+
+/* Otherwise the message must be written to a filter process and read back
+before being written to the incoming fd. First set up the special processing to
+be done during the copying. */
+
+use_crlf  = (options & topt_use_crlf) != 0;
+nl_partial_match = -1;
+
+if (check_string != NULL && escape_string != NULL)
+  {
+  nl_check = check_string;
+  nl_check_length = Ustrlen(nl_check);
+  nl_escape = escape_string;
+  nl_escape_length = Ustrlen(nl_escape);
+  }
+else nl_check_length = nl_escape_length = 0;
+
+/* Start up a subprocess to run the command. Ensure that our main fd will
+be closed when the subprocess execs, but remove the flag afterwards.
+(Otherwise, if this is a TCP/IP socket, it can't get passed on to another
+process to deliver another message.) We get back stdin/stdout file descriptors.
+If the process creation failed, give an error return. */
+
+fd_read = -1;
+fd_write = -1;
+save_errno = 0;
+yield = FALSE;
+write_pid = (pid_t)(-1);
+
+(void)fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
+filter_pid = child_open(transport_filter_argv, NULL, 077, &fd_write, &fd_read,
+  FALSE);
+(void)fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) & ~FD_CLOEXEC);
+if (filter_pid < 0) goto TIDY_UP;      /* errno set */
+
+DEBUG(D_transport)
+  debug_printf("process %d running as transport filter: write=%d read=%d\n",
+    (int)filter_pid, fd_write, fd_read);
+
+/* Fork subprocess to write the message to the filter, and return the result
+via a(nother) pipe. While writing to the filter, we do not do the CRLF,
+smtp dots, or check string processing. */
+
+if (pipe(pfd) != 0) goto TIDY_UP;      /* errno set */
+if ((write_pid = fork()) == 0)
+  {
+  BOOL rc;
+  (void)close(fd_read);
+  (void)close(pfd[pipe_read]);
+  nl_check_length = nl_escape_length = 0;
+  rc = internal_transport_write_message(addr, fd_write,
+    (options & ~(topt_use_crlf | topt_end_dot)),
+    size_limit, add_headers, remove_headers, NULL, NULL,
+    rewrite_rules, rewrite_existflags);
+  save_errno = errno;
+  if (  write(pfd[pipe_write], (void *)&rc, sizeof(BOOL))
+        != sizeof(BOOL)
+     || write(pfd[pipe_write], (void *)&save_errno, sizeof(int))
+        != sizeof(int)
+     || write(pfd[pipe_write], (void *)&(addr->more_errno), sizeof(int))
+        != sizeof(int)
+     )
+    rc = FALSE;        /* compiler quietening */
+  _exit(0);
+  }
+save_errno = errno;
+
+/* Parent process: close our copy of the writing subprocess' pipes. */
+
+(void)close(pfd[pipe_write]);
+(void)close(fd_write);
+fd_write = -1;
+
+/* Writing process creation failed */
+
+if (write_pid < 0)
+  {
+  errno = save_errno;    /* restore */
+  goto TIDY_UP;
+  }
+
+/* When testing, let the subprocess get going */
+
+if (running_in_test_harness) millisleep(250);
+
+DEBUG(D_transport)
+  debug_printf("process %d writing to transport filter\n", (int)write_pid);
+
+/* Copy the message from the filter to the output fd. A read error leaves len
+== -1 and errno set. We need to apply a timeout to the read, to cope with
+the case when the filter gets stuck, but it can be quite a long one. The
+default is 5m, but this is now configurable. */
+
+DEBUG(D_transport) debug_printf("copying from the filter\n");
+
+/* Copy the output of the filter, remembering if the last character was NL. If
+no data is returned, that counts as "ended with NL" (default setting of the
+variable is TRUE). */
+
+chunk_ptr = deliver_out_buffer;
+
+for (;;)
+  {
+  sigalrm_seen = FALSE;
+  alarm(transport_filter_timeout);
+  len = read(fd_read, deliver_in_buffer, DELIVER_IN_BUFFER_SIZE);
+  alarm(0);
+  if (sigalrm_seen)
+    {
+    errno = ETIMEDOUT;
+    transport_filter_timed_out = TRUE;
+    goto TIDY_UP;
+    }
+
+  /* If the read was successful, write the block down the original fd,
+  remembering whether it ends in \n or not. */
+
+  if (len > 0)
+    {
+    if (!write_chunk(fd, deliver_in_buffer, len, use_crlf)) goto TIDY_UP;
+    last_filter_was_NL = (deliver_in_buffer[len-1] == '\n');
+    }
+
+  /* Otherwise, break the loop. If we have hit EOF, set yield = TRUE. */
+
+  else
+    {
+    if (len == 0) yield = TRUE;
+    break;
+    }
+  }
+
+/* Tidying up code. If yield = FALSE there has been an error and errno is set
+to something. Ensure the pipes are all closed and the processes are removed. If
+there has been an error, kill the processes before waiting for them, just to be
+sure. Also apply a paranoia timeout. */
+
+TIDY_UP:
+save_errno = errno;
+
+(void)close(fd_read);
+if (fd_write > 0) (void)close(fd_write);
+
+if (!yield)
+  {
+  if (filter_pid > 0) kill(filter_pid, SIGKILL);
+  if (write_pid > 0)  kill(write_pid, SIGKILL);
+  }
+
+/* Wait for the filter process to complete. */
+
+DEBUG(D_transport) debug_printf("waiting for filter process\n");
+if (filter_pid > 0 && (rc = child_close(filter_pid, 30)) != 0 && yield)
+  {
+  yield = FALSE;
+  save_errno = ERRNO_FILTER_FAIL;
+  addr->more_errno = rc;
+  DEBUG(D_transport) debug_printf("filter process returned %d\n", rc);
+  }
+
+/* Wait for the writing process to complete. If it ends successfully,
+read the results from its pipe, provided we haven't already had a filter
+process failure. */
+
+DEBUG(D_transport) debug_printf("waiting for writing process\n");
+if (write_pid > 0)
+  {
+  rc = child_close(write_pid, 30);
+  if (yield)
+    {
+    if (rc == 0)
+      {
+      BOOL ok;
+      int dummy = read(pfd[pipe_read], (void *)&ok, sizeof(BOOL));
+      if (!ok)
+        {
+        dummy = read(pfd[pipe_read], (void *)&save_errno, sizeof(int));
+        dummy = read(pfd[pipe_read], (void *)&(addr->more_errno), sizeof(int));
+        yield = FALSE;
+        }
+      }
+    else
+      {
+      yield = FALSE;
+      save_errno = ERRNO_FILTER_FAIL;
+      addr->more_errno = rc;
+      DEBUG(D_transport) debug_printf("writing process returned %d\n", rc);
+      }
+    }
+  }
+(void)close(pfd[pipe_read]);
+
+/* If there have been no problems we can now add the terminating "." if this is
+SMTP output, turning off escaping beforehand. If the last character from the
+filter was not NL, insert a NL to make the SMTP protocol work. */
+
+if (yield)
+  {
+  nl_check_length = nl_escape_length = 0;
+  if ((options & topt_end_dot) != 0 && (last_filter_was_NL?
+        !write_chunk(fd, US".\n", 2, use_crlf) :
+        !write_chunk(fd, US"\n.\n", 3, use_crlf)))
+    {
+    yield = FALSE;
+    }
+
+  /* Write out any remaining data in the buffer. */
+
+  else
+    {
+    yield = (len = chunk_ptr - deliver_out_buffer) <= 0 ||
+      transport_write_block(fd, deliver_out_buffer, len);
+    }
+  }
+else errno = save_errno;      /* From some earlier error */
+
+DEBUG(D_transport)
+  {
+  debug_printf("end of filtering transport writing: yield=%d\n", yield);
+  if (!yield)
+    debug_printf("errno=%d more_errno=%d\n", errno, addr->more_errno);
+  }
+
+return yield;
+}
+
+
+
+
+
+/*************************************************
+*            Update waiting database             *
+*************************************************/
+
+/* This is called when an address is deferred by remote transports that are
+capable of sending more than one message over one connection. A database is
+maintained for each transport, keeping track of which messages are waiting for
+which hosts. The transport can then consult this when eventually a successful
+delivery happens, and if it finds that another message is waiting for the same
+host, it can fire up a new process to deal with it using the same connection.
+
+The database records are keyed by host name. They can get full if there are
+lots of messages waiting, and so there is a continuation mechanism for them.
+
+Each record contains a list of message ids, packed end to end without any
+zeros. Each one is MESSAGE_ID_LENGTH bytes long. The count field says how many
+in this record, and the sequence field says if there are any other records for
+this host. If the sequence field is 0, there are none. If it is 1, then another
+record with the name <hostname>:0 exists; if it is 2, then two other records
+with sequence numbers 0 and 1 exist, and so on.
+
+Currently, an exhaustive search of all continuation records has to be done to
+determine whether to add a message id to a given record. This shouldn't be
+too bad except in extreme cases. I can't figure out a *simple* way of doing
+better.
+
+Old records should eventually get swept up by the exim_tidydb utility.
+
+Arguments:
+  hostlist  list of hosts that this message could be sent to
+  tpname    name of the transport
+
+Returns:    nothing
+*/
+
+void
+transport_update_waiting(host_item *hostlist, uschar *tpname)
+{
+uschar buffer[256];
+uschar *prevname = US"";
+host_item *host;
+open_db dbblock;
+open_db *dbm_file;
+
+DEBUG(D_transport) debug_printf("updating wait-%s database\n", tpname);
+
+/* Open the database for this transport */
+
+sprintf(CS buffer, "wait-%.200s", tpname);
+dbm_file = dbfn_open(buffer, O_RDWR, &dbblock, TRUE);
+if (dbm_file == NULL) return;
+
+/* Scan the list of hosts for which this message is waiting, and ensure
+that the message id is in each host record. */
+
+for (host = hostlist; host!= NULL; host = host->next)
+  {
+  BOOL already = FALSE;
+  dbdata_wait *host_record;
+  uschar *s;
+  int i, host_length;
+
+  /* Skip if this is the same host as we just processed; otherwise remember
+  the name for next time. */
+
+  if (Ustrcmp(prevname, host->name) == 0) continue;
+  prevname = host->name;
+
+  /* Look up the host record; if there isn't one, make an empty one. */
+
+  host_record = dbfn_read(dbm_file, host->name);
+  if (host_record == NULL)
+    {
+    host_record = store_get(sizeof(dbdata_wait) + MESSAGE_ID_LENGTH);
+    host_record->count = host_record->sequence = 0;
+    }
+
+  /* Compute the current length */
+
+  host_length = host_record->count * MESSAGE_ID_LENGTH;
+
+  /* Search the record to see if the current message is already in it. */
+
+  for (s = host_record->text; s < host_record->text + host_length;
+       s += MESSAGE_ID_LENGTH)
+    {
+    if (Ustrncmp(s, message_id, MESSAGE_ID_LENGTH) == 0)
+      { already = TRUE; break; }
+    }
+
+  /* If we haven't found this message in the main record, search any
+  continuation records that exist. */
+
+  for (i = host_record->sequence - 1; i >= 0 && !already; i--)
+    {
+    dbdata_wait *cont;
+    sprintf(CS buffer, "%.200s:%d", host->name, i);
+    cont = dbfn_read(dbm_file, buffer);
+    if (cont != NULL)
+      {
+      int clen = cont->count * MESSAGE_ID_LENGTH;
+      for (s = cont->text; s < cont->text + clen; s += MESSAGE_ID_LENGTH)
+        {
+        if (Ustrncmp(s, message_id, MESSAGE_ID_LENGTH) == 0)
+          { already = TRUE; break; }
+        }
+      }
+    }
+
+  /* If this message is already in a record, no need to update. */
+
+  if (already)
+    {
+    DEBUG(D_transport) debug_printf("already listed for %s\n", host->name);
+    continue;
+    }
+
+
+  /* If this record is full, write it out with a new name constructed
+  from the sequence number, increase the sequence number, and empty
+  the record. */
+
+  if (host_record->count >= WAIT_NAME_MAX)
+    {
+    sprintf(CS buffer, "%.200s:%d", host->name, host_record->sequence);
+    dbfn_write(dbm_file, buffer, host_record, sizeof(dbdata_wait) + host_length);
+    host_record->sequence++;
+    host_record->count = 0;
+    host_length = 0;
+    }
+
+  /* If this record is not full, increase the size of the record to
+  allow for one new message id. */
+
+  else
+    {
+    dbdata_wait *newr =
+      store_get(sizeof(dbdata_wait) + host_length + MESSAGE_ID_LENGTH);
+    memcpy(newr, host_record, sizeof(dbdata_wait) + host_length);
+    host_record = newr;
+    }
+
+  /* Now add the new name on the end */
+
+  memcpy(host_record->text + host_length, message_id, MESSAGE_ID_LENGTH);
+  host_record->count++;
+  host_length += MESSAGE_ID_LENGTH;
+
+  /* Update the database */
+
+  dbfn_write(dbm_file, host->name, host_record, sizeof(dbdata_wait) + host_length);
+  DEBUG(D_transport) debug_printf("added to list for %s\n", host->name);
+  }
+
+/* All now done */
+
+dbfn_close(dbm_file);
+}
+
+
+
+
+/*************************************************
+*         Test for waiting messages              *
+*************************************************/
+
+/* This function is called by a remote transport which uses the previous
+function to remember which messages are waiting for which remote hosts. It's
+called after a successful delivery and its job is to check whether there is
+another message waiting for the same host. However, it doesn't do this if the
+current continue sequence is greater than the maximum supplied as an argument,
+or greater than the global connection_max_messages, which, if set, overrides.
+
+Arguments:
+  transport_name     name of the transport
+  hostname           name of the host
+  local_message_max  maximum number of messages down one connection
+                       as set by the caller transport
+  new_message_id     set to the message id of a waiting message
+  more               set TRUE if there are yet more messages waiting
+
+Returns:             TRUE if new_message_id set; FALSE otherwise
+*/
+
+BOOL
+transport_check_waiting(uschar *transport_name, uschar *hostname,
+  int local_message_max, uschar *new_message_id, BOOL *more)
+{
+dbdata_wait *host_record;
+int host_length, path_len;
+open_db dbblock;
+open_db *dbm_file;
+uschar buffer[256];
+
+*more = FALSE;
+
+DEBUG(D_transport)
+  {
+  debug_printf("transport_check_waiting entered\n");
+  debug_printf("  sequence=%d local_max=%d global_max=%d\n",
+    continue_sequence, local_message_max, connection_max_messages);
+  }
+
+/* Do nothing if we have hit the maximum number that can be send down one
+connection. */
+
+if (connection_max_messages >= 0) local_message_max = connection_max_messages;
+if (local_message_max > 0 && continue_sequence >= local_message_max)
+  {
+  DEBUG(D_transport)
+    debug_printf("max messages for one connection reached: returning\n");
+  return FALSE;
+  }
+
+/* Open the waiting information database. */
+
+sprintf(CS buffer, "wait-%.200s", transport_name);
+dbm_file = dbfn_open(buffer, O_RDWR, &dbblock, TRUE);
+if (dbm_file == NULL) return FALSE;
+
+/* See if there is a record for this host; if not, there's nothing to do. */
+
+host_record = dbfn_read(dbm_file, hostname);
+if (host_record == NULL)
+  {
+  dbfn_close(dbm_file);
+  DEBUG(D_transport) debug_printf("no messages waiting for %s\n", hostname);
+  return FALSE;
+  }
+
+/* If the data in the record looks corrupt, just log something and
+don't try to use it. */
+
+if (host_record->count > WAIT_NAME_MAX)
+  {
+  dbfn_close(dbm_file);
+  log_write(0, LOG_MAIN|LOG_PANIC, "smtp-wait database entry for %s has bad "
+    "count=%d (max=%d)", hostname, host_record->count, WAIT_NAME_MAX);
+  return FALSE;
+  }
+
+/* Scan the message ids in the record from the end towards the beginning,
+until one is found for which a spool file actually exists. If the record gets
+emptied, delete it and continue with any continuation records that may exist.
+*/
+
+host_length = host_record->count * MESSAGE_ID_LENGTH;
+
+/* Loop to handle continuation host records in the database */
+
+for (;;)
+  {
+  BOOL found = FALSE;
+
+  sprintf(CS buffer, "%s/input/", spool_directory);
+  path_len = Ustrlen(buffer);
+
+  for (host_length -= MESSAGE_ID_LENGTH; host_length >= 0;
+       host_length -= MESSAGE_ID_LENGTH)
+    {
+    struct stat statbuf;
+    Ustrncpy(new_message_id, host_record->text + host_length,
+      MESSAGE_ID_LENGTH);
+    new_message_id[MESSAGE_ID_LENGTH] = 0;
+
+    if (split_spool_directory)
+      sprintf(CS(buffer + path_len), "%c/%s-D", new_message_id[5], new_message_id);
+    else
+      sprintf(CS(buffer + path_len), "%s-D", new_message_id);
+
+    /* The listed message may be the one we are currently processing. If
+    so, we want to remove it from the list without doing anything else.
+    If not, do a stat to see if it is an existing message. If it is, break
+    the loop to handle it. No need to bother about locks; as this is all
+    "hint" processing, it won't matter if it doesn't exist by the time exim
+    actually tries to deliver it. */
+
+    if (Ustrcmp(new_message_id, message_id) != 0 &&
+        Ustat(buffer, &statbuf) == 0)
+      {
+      found = TRUE;
+      break;
+      }
+    }
+
+  /* If we have removed all the message ids from the record delete the record.
+  If there is a continuation record, fetch it and remove it from the file,
+  as it will be rewritten as the main record. Repeat in the case of an
+  empty continuation. */
+
+  while (host_length <= 0)
+    {
+    int i;
+    dbdata_wait *newr = NULL;
+
+    /* Search for a continuation */
+
+    for (i = host_record->sequence - 1; i >= 0 && newr == NULL; i--)
+      {
+      sprintf(CS buffer, "%.200s:%d", hostname, i);
+      newr = dbfn_read(dbm_file, buffer);
+      }
+
+    /* If no continuation, delete the current and break the loop */
+
+    if (newr == NULL)
+      {
+      dbfn_delete(dbm_file, hostname);
+      break;
+      }
+
+    /* Else replace the current with the continuation */
+
+    dbfn_delete(dbm_file, buffer);
+    host_record = newr;
+    host_length = host_record->count * MESSAGE_ID_LENGTH;
+    }
+
+  /* If we found an existing message, break the continuation loop. */
+
+  if (found) break;
+
+  /* If host_length <= 0 we have emptied a record and not found a good message,
+  and there are no continuation records. Otherwise there is a continuation
+  record to process. */
+
+  if (host_length <= 0)
+    {
+    dbfn_close(dbm_file);
+    DEBUG(D_transport) debug_printf("waiting messages already delivered\n");
+    return FALSE;
+    }
+  }
+
+/* Control gets here when an existing message has been encountered; its
+id is in new_message_id, and host_length is the revised length of the
+host record. If it is zero, the record has been removed. Update the
+record if required, close the database, and return TRUE. */
+
+if (host_length > 0)
+  {
+  host_record->count = host_length/MESSAGE_ID_LENGTH;
+  dbfn_write(dbm_file, hostname, host_record, (int)sizeof(dbdata_wait) + host_length);
+  *more = TRUE;
+  }
+
+dbfn_close(dbm_file);
+return TRUE;
+}
+
+
+
+/*************************************************
+*    Deliver waiting message down same socket    *
+*************************************************/
+
+/* Fork a new exim process to deliver the message, and do a re-exec, both to
+get a clean delivery process, and to regain root privilege in cases where it
+has been given away.
+
+Arguments:
+  transport_name  to pass to the new process
+  hostname        ditto
+  hostaddress     ditto
+  id              the new message to process
+  socket_fd       the connected socket
+
+Returns:          FALSE if fork fails; TRUE otherwise
+*/
+
+BOOL
+transport_pass_socket(uschar *transport_name, uschar *hostname,
+  uschar *hostaddress, uschar *id, int socket_fd)
+{
+pid_t pid;
+int status;
+
+DEBUG(D_transport) debug_printf("transport_pass_socket entered\n");
+
+if ((pid = fork()) == 0)
+  {
+  int i = 16;
+  uschar **argv;
+
+  /* Disconnect entirely from the parent process. If we are running in the
+  test harness, wait for a bit to allow the previous process time to finish,
+  write the log, etc., so that the output is always in the same order for
+  automatic comparison. */
+
+  if ((pid = fork()) != 0) _exit(EXIT_SUCCESS);
+  if (running_in_test_harness) sleep(1);
+
+  /* Set up the calling arguments; use the standard function for the basics,
+  but we have a number of extras that may be added. */
+
+  argv = child_exec_exim(CEE_RETURN_ARGV, TRUE, &i, FALSE, 0);
+
+  #ifdef EXPERIMENTAL_DSN
+  /* Call with the dsn flag */
+  if (smtp_use_dsn) argv[i++] = US"-MCD";
+  #endif
+
+  if (smtp_authenticated) argv[i++] = US"-MCA";
+
+  #ifdef SUPPORT_TLS
+  if (tls_offered) argv[i++] = US"-MCT";
+  #endif
+
+  if (smtp_use_size) argv[i++] = US"-MCS";
+  if (smtp_use_pipelining) argv[i++] = US"-MCP";
+
+  if (queue_run_pid != (pid_t)0)
+    {
+    argv[i++] = US"-MCQ";
+    argv[i++] = string_sprintf("%d", queue_run_pid);
+    argv[i++] = string_sprintf("%d", queue_run_pipe);
+    }
+
+  argv[i++] = US"-MC";
+  argv[i++] = transport_name;
+  argv[i++] = hostname;
+  argv[i++] = hostaddress;
+  argv[i++] = string_sprintf("%d", continue_sequence + 1);
+  argv[i++] = id;
+  argv[i++] = NULL;
+
+  /* Arrange for the channel to be on stdin. */
+
+  if (socket_fd != 0)
+    {
+    (void)dup2(socket_fd, 0);
+    (void)close(socket_fd);
+    }
+
+  DEBUG(D_exec) debug_print_argv(argv);
+  exim_nullstd();                          /* Ensure std{out,err} exist */
+  execv(CS argv[0], (char *const *)argv);
+
+  DEBUG(D_any) debug_printf("execv failed: %s\n", strerror(errno));
+  _exit(errno);         /* Note: must be _exit(), NOT exit() */
+  }
+
+/* If the process creation succeeded, wait for the first-level child, which
+immediately exits, leaving the second level process entirely disconnected from
+this one. */
+
+if (pid > 0)
+  {
+  int rc;
+  while ((rc = wait(&status)) != pid && (rc >= 0 || errno != ECHILD));
+  DEBUG(D_transport) debug_printf("transport_pass_socket succeeded\n");
+  return TRUE;
+  }
+else
+  {
+  DEBUG(D_transport) debug_printf("transport_pass_socket failed to fork: %s\n",
+    strerror(errno));
+  return FALSE;
+  }
+}
+
+
+
+/*************************************************
+*          Set up direct (non-shell) command     *
+*************************************************/
+
+/* This function is called when a command line is to be parsed and executed
+directly, without the use of /bin/sh. It is called by the pipe transport,
+the queryprogram router, and also from the main delivery code when setting up a
+transport filter process. The code for ETRN also makes use of this; in that
+case, no addresses are passed.
+
+Arguments:
+  argvptr            pointer to anchor for argv vector
+  cmd                points to the command string
+  expand_arguments   true if expansion is to occur
+  expand_failed      error value to set if expansion fails; not relevant if
+                     addr == NULL
+  addr               chain of addresses, or NULL
+  etext              text for use in error messages
+  errptr             where to put error message if addr is NULL;
+                     otherwise it is put in the first address
+
+Returns:             TRUE if all went well; otherwise an error will be
+                     set in the first address and FALSE returned
+*/
+
+BOOL
+transport_set_up_command(uschar ***argvptr, uschar *cmd, BOOL expand_arguments,
+  int expand_failed, address_item *addr, uschar *etext, uschar **errptr)
+{
+address_item *ad;
+uschar **argv;
+uschar *s, *ss;
+int address_count = 0;
+int argcount = 0;
+int i, max_args;
+
+/* Get store in which to build an argument list. Count the number of addresses
+supplied, and allow for that many arguments, plus an additional 60, which
+should be enough for anybody. Multiple addresses happen only when the local
+delivery batch option is set. */
+
+for (ad = addr; ad != NULL; ad = ad->next) address_count++;
+max_args = address_count + 60;
+*argvptr = argv = store_get((max_args+1)*sizeof(uschar *));
+
+/* Split the command up into arguments terminated by white space. Lose
+trailing space at the start and end. Double-quoted arguments can contain \\ and
+\" escapes and so can be handled by the standard function; single-quoted
+arguments are verbatim. Copy each argument into a new string. */
+
+s = cmd;
+while (isspace(*s)) s++;
+
+while (*s != 0 && argcount < max_args)
+  {
+  if (*s == '\'')
+    {
+    ss = s + 1;
+    while (*ss != 0 && *ss != '\'') ss++;
+    argv[argcount++] = ss = store_get(ss - s++);
+    while (*s != 0 && *s != '\'') *ss++ = *s++;
+    if (*s != 0) s++;
+    *ss++ = 0;
+    }
+  else argv[argcount++] = string_dequote(&s);
+  while (isspace(*s)) s++;
+  }
+
+argv[argcount] = (uschar *)0;
+
+/* If *s != 0 we have run out of argument slots. */
+
+if (*s != 0)
+  {
+  uschar *msg = string_sprintf("Too many arguments in command \"%s\" in "
+    "%s", cmd, etext);
+  if (addr != NULL)
+    {
+    addr->transport_return = FAIL;
+    addr->message = msg;
+    }
+  else *errptr = msg;
+  return FALSE;
+  }
+
+/* Expand each individual argument if required. Expansion happens for pipes set
+up in filter files and with directly-supplied commands. It does not happen if
+the pipe comes from a traditional .forward file. A failing expansion is a big
+disaster if the command came from Exim's configuration; if it came from a user
+it is just a normal failure. The expand_failed value is used as the error value
+to cater for these two cases.
+
+An argument consisting just of the text "$pipe_addresses" is treated specially.
+It is not passed to the general expansion function. Instead, it is replaced by
+a number of arguments, one for each address. This avoids problems with shell
+metacharacters and spaces in addresses.
+
+If the parent of the top address has an original part of "system-filter", this
+pipe was set up by the system filter, and we can permit the expansion of
+$recipients. */
+
+DEBUG(D_transport)
+  {
+  debug_printf("direct command:\n");
+  for (i = 0; argv[i] != (uschar *)0; i++)
+    debug_printf("  argv[%d] = %s\n", i, string_printing(argv[i]));
+  }
+
+if (expand_arguments)
+  {
+  BOOL allow_dollar_recipients = addr != NULL &&
+    addr->parent != NULL &&
+    Ustrcmp(addr->parent->address, "system-filter") == 0;
+
+  for (i = 0; argv[i] != (uschar *)0; i++)
+    {
+
+    /* Handle special fudge for passing an address list */
+
+    if (addr != NULL &&
+        (Ustrcmp(argv[i], "$pipe_addresses") == 0 ||
+         Ustrcmp(argv[i], "${pipe_addresses}") == 0))
+      {
+      int additional;
+
+      if (argcount + address_count - 1 > max_args)
+        {
+        addr->transport_return = FAIL;
+        addr->message = string_sprintf("Too many arguments to command \"%s\" "
+          "in %s", cmd, etext);
+        return FALSE;
+        }
+
+      additional = address_count - 1;
+      if (additional > 0)
+        memmove(argv + i + 1 + additional, argv + i + 1,
+          (argcount - i)*sizeof(uschar *));
+
+      for (ad = addr; ad != NULL; ad = ad->next) {
+          argv[i++] = ad->address;
+          argcount++;
+      }
+
+      /* Subtract one since we replace $pipe_addresses */
+      argcount--;
+      i--;
+      }
+
+      /* Handle special case of $address_pipe when af_force_command is set */
+
+    else if (addr != NULL && testflag(addr,af_force_command) &&
+        (Ustrcmp(argv[i], "$address_pipe") == 0 ||
+         Ustrcmp(argv[i], "${address_pipe}") == 0))
+      {
+      int address_pipe_i;
+      int address_pipe_argcount = 0;
+      int address_pipe_max_args;
+      uschar **address_pipe_argv;
+
+      /* We can never have more then the argv we will be loading into */
+      address_pipe_max_args = max_args - argcount + 1;
+
+      DEBUG(D_transport)
+        debug_printf("address_pipe_max_args=%d\n", address_pipe_max_args);
+
+      /* We allocate an additional for (uschar *)0 */
+      address_pipe_argv = store_get((address_pipe_max_args+1)*sizeof(uschar *));
+
+      /* +1 because addr->local_part[0] == '|' since af_force_command is set */
+      s = expand_string(addr->local_part + 1);
+
+      if (s == NULL || *s == '\0')
+        {
+        addr->transport_return = FAIL;
+        addr->message = string_sprintf("Expansion of \"%s\" "
+           "from command \"%s\" in %s failed: %s",
+           (addr->local_part + 1), cmd, etext, expand_string_message);
+        return FALSE;
+        }
+
+      while (isspace(*s)) s++; /* strip leading space */
+
+      while (*s != 0 && address_pipe_argcount < address_pipe_max_args)
+        {
+        if (*s == '\'')
+          {
+          ss = s + 1;
+          while (*ss != 0 && *ss != '\'') ss++;
+          address_pipe_argv[address_pipe_argcount++] = ss = store_get(ss - s++);
+          while (*s != 0 && *s != '\'') *ss++ = *s++;
+          if (*s != 0) s++;
+          *ss++ = 0;
+          }
+        else address_pipe_argv[address_pipe_argcount++] = string_dequote(&s);
+        while (isspace(*s)) s++; /* strip space after arg */
+        }
+
+      address_pipe_argv[address_pipe_argcount] = (uschar *)0;
+
+      /* If *s != 0 we have run out of argument slots. */
+      if (*s != 0)
+        {
+        uschar *msg = string_sprintf("Too many arguments in $address_pipe "
+          "\"%s\" in %s", addr->local_part + 1, etext);
+        if (addr != NULL)
+          {
+          addr->transport_return = FAIL;
+          addr->message = msg;
+          }
+        else *errptr = msg;
+        return FALSE;
+        }
+
+      /* address_pipe_argcount - 1
+       * because we are replacing $address_pipe in the argument list
+       * with the first thing it expands to */
+      if (argcount + address_pipe_argcount - 1 > max_args)
+        {
+        addr->transport_return = FAIL;
+        addr->message = string_sprintf("Too many arguments to command "
+          "\"%s\" after expanding $address_pipe in %s", cmd, etext);
+        return FALSE;
+        }
+
+      /* If we are not just able to replace the slot that contained
+       * $address_pipe (address_pipe_argcount == 1)
+       * We have to move the existing argv by address_pipe_argcount - 1
+       * Visually if address_pipe_argcount == 2:
+       * [argv 0][argv 1][argv 2($address_pipe)][argv 3][0]
+       * [argv 0][argv 1][ap_arg0][ap_arg1][old argv 3][0]
+       */
+      if (address_pipe_argcount > 1)
+        memmove(
+          /* current position + additonal args */
+          argv + i + address_pipe_argcount,
+          /* current position + 1 (for the (uschar *)0 at the end) */
+          argv + i + 1,
+          /* -1 for the (uschar *)0 at the end)*/
+          (argcount - i)*sizeof(uschar *)
+        );
+
+      /* Now we fill in the slots we just moved argv out of
+       * [argv 0][argv 1][argv 2=pipeargv[0]][argv 3=pipeargv[1]][old argv 3][0]
+       */
+      for (address_pipe_i = 0;
+           address_pipe_argv[address_pipe_i] != (uschar *)0;
+           address_pipe_i++)
+        {
+        argv[i++] = address_pipe_argv[address_pipe_i];
+        argcount++;
+        }
+
+      /* Subtract one since we replace $address_pipe */
+      argcount--;
+      i--;
+      }
+
+    /* Handle normal expansion string */
+
+    else
+      {
+      uschar *expanded_arg;
+      enable_dollar_recipients = allow_dollar_recipients;
+      expanded_arg = expand_string(argv[i]);
+      enable_dollar_recipients = FALSE;
+
+      if (expanded_arg == NULL)
+        {
+        uschar *msg = string_sprintf("Expansion of \"%s\" "
+          "from command \"%s\" in %s failed: %s",
+          argv[i], cmd, etext, expand_string_message);
+        if (addr != NULL)
+          {
+          addr->transport_return = expand_failed;
+          addr->message = msg;
+          }
+        else *errptr = msg;
+        return FALSE;
+        }
+      argv[i] = expanded_arg;
+      }
+    }
+
+  DEBUG(D_transport)
+    {
+    debug_printf("direct command after expansion:\n");
+    for (i = 0; argv[i] != (uschar *)0; i++)
+      debug_printf("  argv[%d] = %s\n", i, string_printing(argv[i]));
+    }
+  }
+
+return TRUE;
+}
+
+/* vi: aw ai sw=2
+*/
+/* End of transport.c */
diff --git a/src/transports/Makefile b/src/transports/Makefile
new file mode 100644 (file)
index 0000000..02cf0c8
--- /dev/null
@@ -0,0 +1,25 @@
+# Make file for building a library containing all the available transports and
+# calling it transports.a. This is called from the main make file, after cd'ing
+# to the transports subdirectory.
+
+OBJ = appendfile.o autoreply.o lmtp.o pipe.o smtp.o tf_maildir.o
+
+transports.a:    $(OBJ)
+                @$(RM_COMMAND) -f transports.a
+                @echo "$(AR) transports.a"
+                @$(AR) transports.a $(OBJ)
+                $(RANLIB) $@
+
+.SUFFIXES:       .o .c
+.c.o:;           @echo "$(CC) $*.c"
+                $(FE)$(CC) -c $(CFLAGS) $(INCLUDE) $*.c
+
+appendfile.o:    $(HDRS) appendfile.c appendfile.h tf_maildir.h
+autoreply.o:     $(HDRS) autoreply.c autoreply.h
+lmtp.o:          $(HDRS) lmtp.c lmtp.h
+pipe.o:          $(HDRS) pipe.c pipe.h
+smtp.o:          $(HDRS) smtp.c smtp.h
+
+tf_maildir.o:    $(HDRS) tf_maildir.c tf_maildir.h appendfile.h
+
+# End
diff --git a/src/transports/README b/src/transports/README
new file mode 100644 (file)
index 0000000..9ea29fb
--- /dev/null
@@ -0,0 +1,41 @@
+TRANSPORTS:
+
+A delivery attempt results in one of the following values being placed in
+addr->transport_return:
+
+    OK          success
+    DEFER       temporary failure
+    FAIL        permanent failure
+    PANIC       disaster - causes exim to bomb
+
+The field is initialized to DEFER when the address is created, in order that
+unexpected process crashes or other problems don't cause the message to be
+deleted.
+
+For non-OK values, additional information is placed in addr->errno,
+addr->more_errno, and optionally in addr->message. These are inspected only if
+the status is not OK, with one exception (see below).
+
+In addition, the addr->special_action field can be set to request a non-default
+action. The default action after FAIL is to return to sender; the default
+action after DEFER is nothing. The alternatives are:
+
+  SPECIAL_NONE       (default) no special action
+  SPECIAL_FREEZE     freeze the message
+  SPECIAL_WARN       send warning message
+
+The SPECIAL_WARN action is the exception referred to above. It is picked up
+only after a *successful* delivery; it causes a warning message to be sent
+containing the text of warn_message to warn_to. It can be used in appendfile,
+for example, to send a warning message when the mailbox size crosses a given
+threshold.
+
+If the transport is handling a batch of several addresses, it may either put an
+individual value in each address structure, and return TRUE, or it may put a
+common value in the first address, and return FALSE.
+
+Remote transports usually return TRUE and local transports usually return
+FALSE; however, the lmtp transport may return either value, depending on what
+happens inside it.
+
+****
diff --git a/src/transports/appendfile.c b/src/transports/appendfile.c
new file mode 100644 (file)
index 0000000..f56862b
--- /dev/null
@@ -0,0 +1,3371 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+
+#include "../exim.h"
+#include "appendfile.h"
+
+#ifdef SUPPORT_MAILDIR
+#include "tf_maildir.h"
+#endif
+
+
+/* Encodings for mailbox formats, and their names. MBX format is actually
+supported only if SUPPORT_MBX is set. */
+
+enum { mbf_unix, mbf_mbx, mbf_smail, mbf_maildir, mbf_mailstore };
+
+static const char *mailbox_formats[] = {
+  "unix", "mbx", "smail", "maildir", "mailstore" };
+
+
+/* Check warn threshold only if quota size set or not a percentage threshold
+   percentage check should only be done if quota > 0 */
+
+#define THRESHOLD_CHECK  (ob->quota_warn_threshold_value > 0 && \
+  (!ob->quota_warn_threshold_is_percent || ob->quota_value > 0))
+
+
+/* Options specific to the appendfile transport. They must be in alphabetic
+order (note that "_" comes before the lower case letters). Some of them are
+stored in the publicly visible instance block - these are flagged with the
+opt_public flag. */
+
+optionlist appendfile_transport_options[] = {
+#ifdef SUPPORT_MAILDIR
+  { "*expand_maildir_use_size_file", opt_stringptr,
+      (void *)offsetof(appendfile_transport_options_block, expand_maildir_use_size_file) },
+#endif
+  { "*set_use_fcntl_lock",opt_bool | opt_hidden,
+      (void *)offsetof(appendfile_transport_options_block, set_use_fcntl) },
+  { "*set_use_flock_lock",opt_bool | opt_hidden,
+      (void *)offsetof(appendfile_transport_options_block, set_use_flock) },
+  { "*set_use_lockfile", opt_bool | opt_hidden,
+      (void *)offsetof(appendfile_transport_options_block, set_use_lockfile) },
+#ifdef SUPPORT_MBX
+  { "*set_use_mbx_lock", opt_bool | opt_hidden,
+      (void *)offsetof(appendfile_transport_options_block, set_use_mbx_lock) },
+#endif
+  { "allow_fifo",        opt_bool,
+      (void *)offsetof(appendfile_transport_options_block, allow_fifo) },
+  { "allow_symlink",     opt_bool,
+      (void *)offsetof(appendfile_transport_options_block, allow_symlink) },
+  { "batch_id",          opt_stringptr | opt_public,
+      (void *)offsetof(transport_instance, batch_id) },
+  { "batch_max",         opt_int | opt_public,
+      (void *)offsetof(transport_instance, batch_max) },
+  { "check_group",       opt_bool,
+      (void *)offsetof(appendfile_transport_options_block, check_group) },
+  { "check_owner",       opt_bool,
+      (void *)offsetof(appendfile_transport_options_block, check_owner) },
+  { "check_string",      opt_stringptr,
+      (void *)offsetof(appendfile_transport_options_block, check_string) },
+  { "create_directory",  opt_bool,
+      (void *)offsetof(appendfile_transport_options_block, create_directory) },
+  { "create_file",       opt_stringptr,
+      (void *)offsetof(appendfile_transport_options_block, create_file_string) },
+  { "directory",         opt_stringptr,
+      (void *)offsetof(appendfile_transport_options_block, dirname) },
+  { "directory_file",    opt_stringptr,
+      (void *)offsetof(appendfile_transport_options_block, dirfilename) },
+  { "directory_mode",    opt_octint,
+      (void *)offsetof(appendfile_transport_options_block, dirmode) },
+  { "escape_string",     opt_stringptr,
+      (void *)offsetof(appendfile_transport_options_block, escape_string) },
+  { "file",              opt_stringptr,
+      (void *)offsetof(appendfile_transport_options_block, filename) },
+  { "file_format",       opt_stringptr,
+      (void *)offsetof(appendfile_transport_options_block, file_format) },
+  { "file_must_exist",   opt_bool,
+      (void *)offsetof(appendfile_transport_options_block, file_must_exist) },
+  { "lock_fcntl_timeout", opt_time,
+      (void *)offsetof(appendfile_transport_options_block, lock_fcntl_timeout) },
+  { "lock_flock_timeout", opt_time,
+      (void *)offsetof(appendfile_transport_options_block, lock_flock_timeout) },
+  { "lock_interval",     opt_time,
+      (void *)offsetof(appendfile_transport_options_block, lock_interval) },
+  { "lock_retries",      opt_int,
+      (void *)offsetof(appendfile_transport_options_block, lock_retries) },
+  { "lockfile_mode",     opt_octint,
+      (void *)offsetof(appendfile_transport_options_block, lockfile_mode) },
+  { "lockfile_timeout",  opt_time,
+      (void *)offsetof(appendfile_transport_options_block, lockfile_timeout) },
+  { "mailbox_filecount", opt_stringptr,
+      (void *)offsetof(appendfile_transport_options_block, mailbox_filecount_string) },
+  { "mailbox_size",      opt_stringptr,
+      (void *)offsetof(appendfile_transport_options_block, mailbox_size_string) },
+#ifdef SUPPORT_MAILDIR
+  { "maildir_format",    opt_bool,
+      (void *)offsetof(appendfile_transport_options_block, maildir_format ) } ,
+  { "maildir_quota_directory_regex", opt_stringptr,
+      (void *)offsetof(appendfile_transport_options_block, maildir_dir_regex) },
+  { "maildir_retries",   opt_int,
+      (void *)offsetof(appendfile_transport_options_block, maildir_retries) },
+  { "maildir_tag",       opt_stringptr,
+      (void *)offsetof(appendfile_transport_options_block, maildir_tag) },
+  { "maildir_use_size_file", opt_expand_bool,
+      (void *)offsetof(appendfile_transport_options_block, maildir_use_size_file ) } ,
+  { "maildirfolder_create_regex", opt_stringptr,
+      (void *)offsetof(appendfile_transport_options_block, maildirfolder_create_regex ) },
+#endif  /* SUPPORT_MAILDIR */
+#ifdef SUPPORT_MAILSTORE
+  { "mailstore_format",  opt_bool,
+      (void *)offsetof(appendfile_transport_options_block, mailstore_format ) },
+  { "mailstore_prefix",  opt_stringptr,
+      (void *)offsetof(appendfile_transport_options_block, mailstore_prefix ) },
+  { "mailstore_suffix",  opt_stringptr,
+      (void *)offsetof(appendfile_transport_options_block, mailstore_suffix ) },
+#endif  /* SUPPORT_MAILSTORE */
+#ifdef SUPPORT_MBX
+  { "mbx_format",        opt_bool,
+      (void *)offsetof(appendfile_transport_options_block, mbx_format ) } ,
+#endif  /* SUPPORT_MBX */
+  { "message_prefix",    opt_stringptr,
+      (void *)offsetof(appendfile_transport_options_block, message_prefix) },
+  { "message_suffix",    opt_stringptr,
+      (void *)offsetof(appendfile_transport_options_block, message_suffix) },
+  { "mode",              opt_octint,
+      (void *)offsetof(appendfile_transport_options_block, mode) },
+  { "mode_fail_narrower",opt_bool,
+      (void *)offsetof(appendfile_transport_options_block, mode_fail_narrower) },
+  { "notify_comsat",     opt_bool,
+      (void *)offsetof(appendfile_transport_options_block, notify_comsat) },
+  { "quota",             opt_stringptr,
+      (void *)offsetof(appendfile_transport_options_block, quota) },
+  { "quota_directory",   opt_stringptr,
+      (void *)offsetof(appendfile_transport_options_block, quota_directory) },
+  { "quota_filecount",   opt_stringptr,
+      (void *)offsetof(appendfile_transport_options_block, quota_filecount) },
+  { "quota_is_inclusive", opt_bool,
+      (void *)offsetof(appendfile_transport_options_block, quota_is_inclusive) },
+  { "quota_size_regex",   opt_stringptr,
+      (void *)offsetof(appendfile_transport_options_block, quota_size_regex) },
+  { "quota_warn_message", opt_stringptr | opt_public,
+      (void *)offsetof(transport_instance, warn_message) },
+  { "quota_warn_threshold", opt_stringptr,
+      (void *)offsetof(appendfile_transport_options_block, quota_warn_threshold) },
+  { "use_bsmtp",         opt_bool,
+      (void *)offsetof(appendfile_transport_options_block, use_bsmtp) },
+  { "use_crlf",          opt_bool,
+      (void *)offsetof(appendfile_transport_options_block, use_crlf) },
+  { "use_fcntl_lock",    opt_bool_set,
+      (void *)offsetof(appendfile_transport_options_block, use_fcntl) },
+  { "use_flock_lock",    opt_bool_set,
+      (void *)offsetof(appendfile_transport_options_block, use_flock) },
+  { "use_lockfile",      opt_bool_set,
+      (void *)offsetof(appendfile_transport_options_block, use_lockfile) },
+#ifdef SUPPORT_MBX
+  { "use_mbx_lock",      opt_bool_set,
+      (void *)offsetof(appendfile_transport_options_block, use_mbx_lock) },
+#endif  /* SUPPORT_MBX */
+};
+
+/* Size of the options list. An extern variable has to be used so that its
+address can appear in the tables drtables.c. */
+
+int appendfile_transport_options_count =
+  sizeof(appendfile_transport_options)/sizeof(optionlist);
+
+/* Default private options block for the appendfile transport. */
+
+appendfile_transport_options_block appendfile_transport_option_defaults = {
+  NULL,           /* filename */
+  NULL,           /* dirname */
+  US"q${base62:$tod_epoch}-$inode", /* dirfilename */
+  NULL,           /* message_prefix (default reset in init if not bsmtp) */
+  NULL,           /* message_suffix (ditto) */
+  US"anywhere",   /* create_file_string (string value for create_file) */
+  NULL,           /* quota */
+  NULL,           /* quota_directory */
+  NULL,           /* quota_filecount */
+  NULL,           /* quota_size_regex */
+  NULL,           /* quota_warn_threshold */
+  NULL,           /* mailbox_size_string */
+  NULL,           /* mailbox_filecount_string */
+  NULL,           /* expand_maildir_use_size_file */ 
+  US"^(?:cur|new|\\..*)$",  /* maildir_dir_regex */
+  NULL,           /* maildir_tag */
+  NULL,           /* maildirfolder_create_regex */
+  NULL,           /* mailstore_prefix */
+  NULL,           /* mailstore_suffix */
+  NULL,           /* check_string (default changed for non-bsmtp file)*/
+  NULL,           /* escape_string (ditto) */
+  NULL,           /* file_format */
+  0,              /* quota_value */
+  0,              /* quota_warn_threshold_value */
+  -1,             /* mailbox_size_value */
+  -1,             /* mailbox_filecount_value */
+  0,              /* quota_filecount_value */
+  APPENDFILE_MODE,           /* mode */
+  APPENDFILE_DIRECTORY_MODE, /* dirmode */
+  APPENDFILE_LOCKFILE_MODE,  /* lockfile_mode */
+  30*60,          /* lockfile_timeout */
+  0,              /* lock_fcntl_timeout */
+  0,              /* lock_flock_timeout */
+  10,             /* lock_retries */
+   3,             /* lock_interval */
+  10,             /* maildir_retries */
+  create_anywhere,/* create_file */
+  0,              /* options */
+  FALSE,          /* allow_fifo */
+  FALSE,          /* allow_symlink */
+  FALSE,          /* check_group */
+  TRUE,           /* check_owner */
+  TRUE,           /* create_directory */
+  FALSE,          /* notify_comsat */
+  TRUE,           /* use_lockfile */
+  FALSE,          /* set_use_lockfile */
+  TRUE,           /* use_fcntl */
+  FALSE,          /* set_use_fcntl */
+  FALSE,          /* use_flock */
+  FALSE,          /* set_use_flock */
+  FALSE,          /* use_mbx_lock */
+  FALSE,          /* set_use_mbx_lock */
+  FALSE,          /* use_bsmtp */
+  FALSE,          /* use_crlf */
+  FALSE,          /* file_must_exist */
+  TRUE,           /* mode_fail_narrower */
+  FALSE,          /* maildir_format */
+  FALSE,          /* maildir_use_size_file */
+  FALSE,          /* mailstore_format */
+  FALSE,          /* mbx_format */
+  FALSE,          /* quota_warn_threshold_is_percent */
+  TRUE            /* quota_is_inclusive */
+};
+
+
+
+/*************************************************
+*              Setup entry point                 *
+*************************************************/
+
+/* Called for each delivery in the privileged state, just before the uid/gid
+are changed and the main entry point is called. We use this function to
+expand any quota settings, so that it can access files that may not be readable
+by the user. It is also used to pick up external mailbox size information, if
+set.
+
+Arguments:
+  tblock     points to the transport instance
+  addrlist   addresses about to be delivered (not used)
+  dummy      not used (doesn't pass back data)
+  uid        the uid that will be set (not used)
+  gid        the gid that will be set (not used)
+  errmsg     where to put an error message
+
+Returns:     OK, FAIL, or DEFER
+*/
+
+static int
+appendfile_transport_setup(transport_instance *tblock, address_item *addrlist,
+  transport_feedback *dummy, uid_t uid, gid_t gid, uschar **errmsg)
+{
+appendfile_transport_options_block *ob =
+  (appendfile_transport_options_block *)(tblock->options_block);
+uschar *q = ob->quota;
+double default_value = 0.0;
+int i;
+
+addrlist = addrlist;    /* Keep picky compilers happy */
+dummy = dummy;
+uid = uid;
+gid = gid;
+
+if (ob->expand_maildir_use_size_file)
+       ob->maildir_use_size_file = expand_check_condition(ob->expand_maildir_use_size_file, 
+               US"`maildir_use_size_file` in transport", tblock->name);
+
+/* Loop for quota, quota_filecount, quota_warn_threshold, mailbox_size,
+mailbox_filecount */
+
+for (i = 0; i < 5; i++)
+  {
+  double d;
+  uschar *which = NULL;
+
+  if (q == NULL) d = default_value; else
+    {
+    uschar *rest;
+    uschar *s = expand_string(q);
+
+    if (s == NULL)
+      {
+      *errmsg = string_sprintf("Expansion of \"%s\" in %s transport failed: "
+        "%s", q, tblock->name, expand_string_message);
+      return search_find_defer? DEFER : FAIL;
+      }
+
+    d = Ustrtod(s, &rest);
+
+    /* Handle following characters K, M, G, %, the latter being permitted
+    for quota_warn_threshold only. A threshold with no quota setting is
+    just ignored. */
+
+    if (tolower(*rest) == 'k') { d *= 1024.0; rest++; }
+    else if (tolower(*rest) == 'm') { d *= 1024.0*1024.0; rest++; }
+    else if (tolower(*rest) == 'g') { d *= 1024.0*1024.0*1024.0; rest++; }
+    else if (*rest == '%' && i == 2)
+      {
+      if (ob->quota_value <= 0 && !ob->maildir_use_size_file) d = 0;
+      else if ((int)d < 0 || (int)d > 100)
+        {
+        *errmsg = string_sprintf("Invalid quota_warn_threshold percentage (%d)"
+          " for %s transport", (int)d, tblock->name);
+        return FAIL;
+        }
+      ob->quota_warn_threshold_is_percent = TRUE;
+      rest++;
+      }
+
+    while (isspace(*rest)) rest++;
+
+    if (*rest != 0)
+      {
+      *errmsg = string_sprintf("Malformed value \"%s\" (expansion of \"%s\") "
+        "in %s transport", s, q, tblock->name);
+      return FAIL;
+      }
+    }
+
+  /* Set each value, checking for possible overflow. */
+
+  switch (i)
+    {
+    case 0:
+    if (d >= 2.0*1024.0*1024.0*1024.0 && sizeof(off_t) <= 4) which = US"quota";
+    ob->quota_value = (off_t)d;
+    q = ob->quota_filecount;
+    break;
+
+    case 1:
+    if (d >= 2.0*1024.0*1024.0*1024.0) which = US"quota_filecount";
+    ob->quota_filecount_value = (int)d;
+    q = ob->quota_warn_threshold;
+    break;
+
+    case 2:
+    if (d >= 2.0*1024.0*1024.0*1024.0 && sizeof(off_t) <= 4)
+      which = US"quota_warn_threshold";
+    ob->quota_warn_threshold_value = (off_t)d;
+    q = ob->mailbox_size_string;
+    default_value = -1.0;
+    break;
+
+    case 3:
+    if (d >= 2.0*1024.0*1024.0*1024.0 && sizeof(off_t) <= 4)
+      which = US"mailbox_size";;
+    ob->mailbox_size_value = (off_t)d;
+    q = ob->mailbox_filecount_string;
+    break;
+
+    case 4:
+    if (d >= 2.0*1024.0*1024.0*1024.0) which = US"mailbox_filecount";
+    ob->mailbox_filecount_value = (int)d;
+    break;
+    }
+
+  if (which != NULL)
+    {
+    *errmsg = string_sprintf("%s value %.10g is too large (overflow) in "
+      "%s transport", which, d, tblock->name);
+    return FAIL;
+    }
+  }
+
+return OK;
+}
+
+
+
+/*************************************************
+*          Initialization entry point            *
+*************************************************/
+
+/* Called for each instance, after its options have been read, to
+enable consistency checks to be done, or anything else that needs
+to be set up. */
+
+void
+appendfile_transport_init(transport_instance *tblock)
+{
+appendfile_transport_options_block *ob =
+  (appendfile_transport_options_block *)(tblock->options_block);
+
+/* Set up the setup entry point, to be called in the privileged state */
+
+tblock->setup = appendfile_transport_setup;
+
+/* Lock_retries must be greater than zero */
+
+if (ob->lock_retries == 0) ob->lock_retries = 1;
+
+/* Only one of a file name or directory name must be given. */
+
+if (ob->filename != NULL && ob->dirname != NULL)
+  log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s transport:\n  "
+  "only one of \"file\" or \"directory\" can be specified", tblock->name);
+
+/* If a file name was specified, neither quota_filecount nor quota_directory
+must be given. */
+
+if (ob->filename != NULL)
+  {
+  if (ob->quota_filecount != NULL)
+    log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s transport:\n  "
+      "quota_filecount must not be set without \"directory\"", tblock->name);
+  if (ob->quota_directory != NULL)
+    log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s transport:\n  "
+      "quota_directory must not be set without \"directory\"", tblock->name);
+  }
+
+/* The default locking depends on whether MBX is set or not. Change the
+built-in default if none of the lock options has been explicitly set. At least
+one form of locking is required in all cases, but mbx locking changes the
+meaning of fcntl and flock locking. */
+
+/* Not all operating systems provide flock(). For those that do, if flock is
+requested, the default for fcntl is FALSE. */
+
+if (ob->use_flock)
+  {
+  #ifdef NO_FLOCK
+  log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s transport:\n  "
+    "flock() support was not available in the operating system when this "
+    "binary was built", tblock->name);
+  #endif  /* NO_FLOCK */
+  if (!ob->set_use_fcntl) ob->use_fcntl = FALSE;
+  }
+
+#ifdef SUPPORT_MBX
+if (ob->mbx_format)
+  {
+  if (!ob->set_use_lockfile && !ob->set_use_fcntl && !ob->set_use_flock &&
+      !ob->set_use_mbx_lock)
+    {
+    ob->use_lockfile = ob->use_flock = FALSE;
+    ob->use_mbx_lock = ob->use_fcntl = TRUE;
+    }
+  else if (ob->use_mbx_lock)
+    {
+    if (!ob->set_use_lockfile) ob->use_lockfile = FALSE;
+    if (!ob->set_use_fcntl) ob->use_fcntl = FALSE;
+    if (!ob->set_use_flock) ob->use_flock = FALSE;
+    if (!ob->use_fcntl && !ob->use_flock) ob->use_fcntl = TRUE;
+    }
+  }
+#endif  /* SUPPORT_MBX */
+
+if (!ob->use_fcntl && !ob->use_flock && !ob->use_lockfile && !ob->use_mbx_lock)
+  log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s transport:\n  "
+    "no locking configured", tblock->name);
+
+/* Unset timeouts for non-used locking types */
+
+if (!ob->use_fcntl) ob->lock_fcntl_timeout = 0;
+if (!ob->use_flock) ob->lock_flock_timeout = 0;
+
+/* If a directory name was specified, only one of maildir or mailstore may be
+specified, and if quota_filecount or quota_directory is given, quota must
+be set. */
+
+if (ob->dirname != NULL)
+  {
+  if (ob->maildir_format && ob->mailstore_format)
+    log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s transport:\n  "
+      "only one of maildir and mailstore may be specified", tblock->name);
+  if (ob->quota_filecount != NULL && ob->quota == NULL)
+    log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s transport:\n  "
+      "quota must be set if quota_filecount is set", tblock->name);
+  if (ob->quota_directory != NULL && ob->quota == NULL)
+    log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s transport:\n  "
+      "quota must be set if quota_directory is set", tblock->name);
+  }
+
+/* If a fixed uid field is set, then a gid field must also be set. */
+
+if (tblock->uid_set && !tblock->gid_set && tblock->expand_gid == NULL)
+  log_write(0, LOG_PANIC_DIE|LOG_CONFIG,
+    "user set without group for the %s transport", tblock->name);
+
+/* If "create_file" is set, check that a valid option is given, and set the
+integer variable. */
+
+if (ob->create_file_string != NULL)
+  {
+  int value = 0;
+  if (Ustrcmp(ob->create_file_string, "anywhere") == 0) value = create_anywhere;
+  else if (Ustrcmp(ob->create_file_string, "belowhome") == 0) value =
+    create_belowhome;
+  else if (Ustrcmp(ob->create_file_string, "inhome") == 0)
+    value = create_inhome;
+  else
+    log_write(0, LOG_PANIC_DIE|LOG_CONFIG,
+      "invalid value given for \"file_create\" for the %s transport: %s",
+        tblock->name, ob->create_file_string);
+  ob->create_file = value;
+  }
+
+/* If quota_warn_threshold is set, set up default for warn_message. It may
+not be used if the actual threshold for a given delivery ends up as zero,
+of if it's given as a percentage and there's no quota setting. */
+
+if (ob->quota_warn_threshold != NULL)
+  {
+  if (tblock->warn_message == NULL) tblock->warn_message = US
+    "To: $local_part@$domain\n"
+    "Subject: Your mailbox\n\n"
+    "This message is automatically created by mail delivery software (Exim).\n\n"
+    "The size of your mailbox has exceeded a warning threshold that is\n"
+    "set by the system administrator.\n";
+  }
+
+/* If batch SMTP is set, force the check and escape strings, and arrange that
+headers are also escaped. */
+
+if (ob->use_bsmtp)
+  {
+  ob->check_string = US".";
+  ob->escape_string = US"..";
+  ob->options |= topt_escape_headers;
+  }
+
+/* If not batch SMTP, not maildir, not mailstore, and directory is not set,
+insert default values for for the affixes and the check/escape strings. */
+
+else if (ob->dirname == NULL && !ob->maildir_format && !ob->mailstore_format)
+  {
+  if (ob->message_prefix == NULL) ob->message_prefix =
+    US"From ${if def:return_path{$return_path}{MAILER-DAEMON}} ${tod_bsdinbox}\n";
+  if (ob->message_suffix == NULL) ob->message_suffix = US"\n";
+  if (ob->check_string == NULL) ob->check_string = US"From ";
+  if (ob->escape_string == NULL) ob->escape_string = US">From ";
+
+  }
+
+/* Set up the bitwise options for transport_write_message from the various
+driver options. Only one of body_only and headers_only can be set. */
+
+ob->options |=
+  (tblock->body_only? topt_no_headers : 0) |
+  (tblock->headers_only? topt_no_body : 0) |
+  (tblock->return_path_add? topt_add_return_path : 0) |
+  (tblock->delivery_date_add? topt_add_delivery_date : 0) |
+  (tblock->envelope_to_add? topt_add_envelope_to : 0) |
+  ((ob->use_crlf || ob->mbx_format)? topt_use_crlf : 0);
+}
+
+
+
+/*************************************************
+*                  Notify comsat                 *
+*************************************************/
+
+/* The comsat daemon is the thing that provides asynchronous notification of
+the arrival of local messages, if requested by the user by "biff y". It is a
+BSD thing that uses a TCP/IP protocol for communication. A message consisting
+of the text "user@offset" must be sent, where offset is the place in the
+mailbox where new mail starts. There is no scope for telling it which file to
+look at, which makes it a less than useful if mail is being delivered into a
+non-standard place such as the user's home directory. In fact, it doesn't seem
+to pay much attention to the offset.
+
+Arguments:
+  user       user name
+  offset     offset in mailbox
+
+Returns:     nothing
+*/
+
+static void
+notify_comsat(uschar *user, off_t offset)
+{
+struct servent *sp;
+host_item host;
+host_item *h;
+uschar buffer[256];
+
+DEBUG(D_transport) debug_printf("notify_comsat called\n");
+
+sprintf(CS buffer, "%.200s@" OFF_T_FMT "\n", user, offset);
+
+if ((sp = getservbyname("biff", "udp")) == NULL)
+  {
+  DEBUG(D_transport) debug_printf("biff/udp is an unknown service");
+  return;
+  }
+
+host.name = US"localhost";
+host.next = NULL;
+
+
+/* This code is all set up to look up "localhost" and use all its addresses
+until one succeeds. However, it appears that at least on some systems, comsat
+doesn't listen on the ::1 address. So for the moment, just force the address to
+be 127.0.0.1. At some future stage, when IPv6 really is superseding IPv4, this
+can be changed. (But actually, comsat is probably dying out anyway.) */
+
+/******
+if (host_find_byname(&host, NULL, 0, NULL, FALSE) == HOST_FIND_FAILED)
+  {
+  DEBUG(D_transport) debug_printf("\"localhost\" unknown\n");
+  return;
+  }
+******/
+
+host.address = US"127.0.0.1";
+
+
+for (h = &host; h != NULL; h = h->next)
+  {
+  int sock, rc;
+  int host_af = (Ustrchr(h->address, ':') != NULL)? AF_INET6 : AF_INET;
+
+  DEBUG(D_transport) debug_printf("calling comsat on %s\n", h->address);
+
+  sock = ip_socket(SOCK_DGRAM, host_af);
+  if (sock < 0) continue;
+
+  /* Connect never fails for a UDP socket, so don't set a timeout. */
+
+  (void)ip_connect(sock, host_af, h->address, ntohs(sp->s_port), 0);
+  rc = send(sock, buffer, Ustrlen(buffer) + 1, 0);
+  (void)close(sock);
+
+  if (rc >= 0) break;
+  DEBUG(D_transport)
+    debug_printf("send to comsat failed for %s: %s\n", strerror(errno),
+      h->address);
+  }
+}
+
+
+
+/*************************************************
+*     Check the format of a file                 *
+*************************************************/
+
+/* This function is called when file_format is set, to check that an existing
+file has the right format. The format string contains text/transport pairs. The
+string matching is literal. we just read big_buffer_size bytes, because this is
+all about the first few bytes of a file.
+
+Arguments:
+  cfd          the open file
+  tblock       the transport block
+  addr         the address block - for inserting error data
+
+Returns:       pointer to the required transport, or NULL
+*/
+
+transport_instance *
+check_file_format(int cfd, transport_instance *tblock, address_item *addr)
+{
+uschar *format =
+  ((appendfile_transport_options_block *)(tblock->options_block))->file_format;
+uschar data[256];
+int len = read(cfd, data, sizeof(data));
+int sep = 0;
+uschar *s;
+
+DEBUG(D_transport) debug_printf("checking file format\n");
+
+/* An empty file matches the current transport */
+
+if (len == 0) return tblock;
+
+/* Search the formats for a match */
+
+while ((s = string_nextinlist(&format,&sep,big_buffer,big_buffer_size))!= NULL)
+  {
+  int slen = Ustrlen(s);
+  BOOL match = len >= slen && Ustrncmp(data, s, slen) == 0;
+  uschar *tp = string_nextinlist(&format, &sep, big_buffer, big_buffer_size);
+  if (match)
+    {
+    transport_instance *tt;
+    for (tt = transports; tt != NULL; tt = tt->next)
+      if (Ustrcmp(tp, tt->name) == 0)
+        {
+        DEBUG(D_transport)
+          debug_printf("file format -> %s transport\n", tt->name);
+        return tt;
+        }
+    addr->basic_errno = ERRNO_BADTRANSPORT;
+    addr->message = string_sprintf("%s transport (for %.*s format) not found",
+      tp, slen, data);
+    return NULL;
+    }
+  }
+
+/* Failed to find a match */
+
+addr->basic_errno = ERRNO_FORMATUNKNOWN;
+addr->message = US"mailbox file format unrecognized";
+return NULL;
+}
+
+
+
+
+/*************************************************
+*       Check directory's files for quota        *
+*************************************************/
+
+/* This function is called if quota is set for one of the delivery modes that
+delivers into a specific directory. It scans the directory and stats all the
+files in order to get a total size and count. This is an expensive thing to do,
+but some people are prepared to bear the cost. Alternatively, if size_regex is
+set, it is used as a regex to try to extract the size from the file name, a
+strategy that some people use on maildir files on systems where the users have
+no shell access.
+
+The function is global, because it is also called from tf_maildir.c for maildir
+folders (which should contain only regular files).
+
+Note: Any problems can be written to debugging output, but cannot be written to
+the log, because we are running as an unprivileged user here.
+
+Arguments:
+  dirname       the name of the directory
+  countptr      where to add the file count (because this function recurses)
+  regex         a compiled regex to get the size from a name
+
+Returns:        the sum of the sizes of the stattable files
+                zero if the directory cannot be opened
+*/
+
+off_t
+check_dir_size(uschar *dirname, int *countptr, const pcre *regex)
+{
+DIR *dir;
+off_t sum = 0;
+int count = *countptr;
+struct dirent *ent;
+struct stat statbuf;
+
+dir = opendir(CS dirname);
+if (dir == NULL) return 0;
+
+while ((ent = readdir(dir)) != NULL)
+  {
+  uschar *name = US ent->d_name;
+  uschar buffer[1024];
+
+  if (Ustrcmp(name, ".") == 0 || Ustrcmp(name, "..") == 0) continue;
+
+  count++;
+
+  /* If there's a regex, try to find the size using it */
+
+  if (regex != NULL)
+    {
+    int ovector[6];
+    if (pcre_exec(regex, NULL, CS name, Ustrlen(name), 0, 0, ovector,6) >= 2)
+      {
+      uschar *endptr;
+      off_t size = (off_t)Ustrtod(name + ovector[2], &endptr);
+      if (endptr == name + ovector[3])
+        {
+        sum += size;
+        DEBUG(D_transport)
+          debug_printf("check_dir_size: size from %s is " OFF_T_FMT "\n", name,
+            size);
+        continue;
+        }
+      }
+    DEBUG(D_transport)
+      debug_printf("check_dir_size: regex did not match %s\n", name);
+    }
+
+  /* No regex or no match for the regex, or captured non-digits */
+
+  if (!string_format(buffer, sizeof(buffer), "%s/%s", dirname, name))
+    {
+    DEBUG(D_transport)
+      debug_printf("check_dir_size: name too long: dir=%s name=%s\n", dirname,
+        name);
+    continue;
+    }
+
+  if (Ustat(buffer, &statbuf) < 0)
+    {
+    DEBUG(D_transport)
+      debug_printf("check_dir_size: stat error %d for %s: %s\n", errno, buffer,
+        strerror(errno));
+    continue;
+    }
+
+  if ((statbuf.st_mode & S_IFMT) == S_IFREG)
+    sum += statbuf.st_size;
+  else if ((statbuf.st_mode & S_IFMT) == S_IFDIR)
+    sum += check_dir_size(buffer, &count, regex);
+  }
+
+closedir(dir);
+DEBUG(D_transport)
+  debug_printf("check_dir_size: dir=%s sum=" OFF_T_FMT " count=%d\n", dirname,
+    sum, count);
+
+*countptr = count;
+return sum;
+}
+
+
+
+
+/*************************************************
+*         Apply a lock to a file descriptor      *
+*************************************************/
+
+/* This function applies a lock to a file descriptor, using a blocking or
+non-blocking lock, depending on the timeout value. It can apply either or
+both of a fcntl() and a flock() lock. However, not all OS support flock();
+for those that don't, the use_flock option cannot be set.
+
+Arguments:
+  fd          the file descriptor
+  fcntltype   type of lock, specified as F_WRLCK or F_RDLCK (that is, in
+                fcntl() format); the flock() type is deduced if needed
+  dofcntl     do fcntl() locking
+  fcntltime   non-zero to use blocking fcntl()
+  doflock     do flock() locking
+  flocktime   non-zero to use blocking flock()
+
+Returns:      yield of the fcntl() or flock() call, with errno preserved;
+              sigalrm_seen set if there has been a timeout
+*/
+
+static int
+apply_lock(int fd, int fcntltype, BOOL dofcntl, int fcntltime, BOOL doflock,
+    int flocktime)
+{
+int yield = 0;
+int save_errno;
+struct flock lock_data;
+lock_data.l_type = fcntltype;
+lock_data.l_whence = lock_data.l_start = lock_data.l_len = 0;
+
+sigalrm_seen = FALSE;
+
+if (dofcntl)
+  {
+  if (fcntltime > 0)
+    {
+    alarm(fcntltime);
+    yield = fcntl(fd, F_SETLKW, &lock_data);
+    save_errno = errno;
+    alarm(0);
+    errno = save_errno;
+    }
+  else yield = fcntl(fd, F_SETLK, &lock_data);
+  }
+
+#ifndef NO_FLOCK
+if (doflock && (yield >= 0))
+  {
+  int flocktype = (fcntltype == F_WRLCK)? LOCK_EX : LOCK_SH;
+  if (flocktime > 0)
+    {
+    alarm(flocktime);
+    yield = flock(fd, flocktype);
+    save_errno = errno;
+    alarm(0);
+    errno = save_errno;
+    }
+  else yield = flock(fd, flocktype | LOCK_NB);
+  }
+#endif  /* NO_FLOCK */
+
+return yield;
+}
+
+
+
+
+#ifdef SUPPORT_MBX
+/*************************************************
+*         Copy message into MBX mailbox          *
+*************************************************/
+
+/* This function is called when a message intended for a MBX mailbox has been
+written to a temporary file. We can now get the size of the message and then
+copy it in MBX format to the mailbox.
+
+Arguments:
+  to_fd        fd to write to (the real mailbox)
+  from_fd      fd to read from (the temporary file)
+  saved_size   current size of mailbox
+
+Returns:       OK if all went well, DEFER otherwise, with errno preserved
+               the number of bytes written are added to transport_count
+                 by virtue of calling transport_write_block()
+*/
+
+/* Values taken from c-client */
+
+#define MBX_HDRSIZE            2048
+#define MBX_NUSERFLAGS           30
+
+static int
+copy_mbx_message(int to_fd, int from_fd, off_t saved_size)
+{
+int used;
+off_t size;
+struct stat statbuf;
+
+/* If the current mailbox size is zero, write a header block */
+
+if (saved_size == 0)
+  {
+  int i;
+  uschar *s;
+  memset (deliver_out_buffer, '\0', MBX_HDRSIZE);
+  sprintf(CS(s = deliver_out_buffer), "*mbx*\015\012%08lx00000000\015\012",
+    (long int)time(NULL));
+  for (i = 0; i < MBX_NUSERFLAGS; i++)
+    sprintf (CS(s += Ustrlen(s)), "\015\012");
+  if (!transport_write_block (to_fd, deliver_out_buffer, MBX_HDRSIZE))
+    return DEFER;
+  }
+
+DEBUG(D_transport) debug_printf("copying MBX message from temporary file\n");
+
+/* Now construct the message's header from the time and the RFC822 file
+size, including CRLFs, which is the size of the input (temporary) file. */
+
+if (fstat(from_fd, &statbuf) < 0) return DEFER;
+size = statbuf.st_size;
+
+sprintf (CS deliver_out_buffer, "%s," OFF_T_FMT ";%08lx%04x-%08x\015\012",
+  tod_stamp(tod_mbx), size, 0L, 0, 0);
+used = Ustrlen(deliver_out_buffer);
+
+/* Rewind the temporary file, and copy it over in chunks. */
+
+lseek(from_fd, 0 , SEEK_SET);
+
+while (size > 0)
+  {
+  int len = read(from_fd, deliver_out_buffer + used,
+    DELIVER_OUT_BUFFER_SIZE - used);
+  if (len <= 0)
+    {
+    if (len == 0) errno = ERRNO_MBXLENGTH;
+    return DEFER;
+    }
+  if (!transport_write_block(to_fd, deliver_out_buffer, used + len))
+    return DEFER;
+  size -= len;
+  used = 0;
+  }
+
+return OK;
+}
+#endif  /* SUPPORT_MBX */
+
+
+
+/*************************************************
+*            Check creation is permitted         *
+*************************************************/
+
+/* This function checks whether a given file name is permitted to be created,
+as controlled by the create_file option. If no home directory is set, however,
+we can't do any tests.
+
+Arguments:
+  filename     the file name
+  create_file  the ob->create_file option
+
+Returns:       TRUE if creation is permitted
+*/
+
+static BOOL
+check_creation(uschar *filename, int create_file)
+{
+BOOL yield = TRUE;
+
+if (deliver_home != NULL && create_file != create_anywhere)
+  {
+  int len = Ustrlen(deliver_home);
+  uschar *file = filename;
+
+  while (file[0] == '/' && file[1] == '/') file++;
+  if (Ustrncmp(file, deliver_home, len) != 0 || file[len] != '/' ||
+       ( Ustrchr(file+len+2, '/') != NULL &&
+         (
+         create_file != create_belowhome ||
+         Ustrstr(file+len, "/../") != NULL
+         )
+       )
+     ) yield = FALSE;
+
+  /* If yield is TRUE, the file name starts with the home directory, and does
+  not contain any instances of "/../" in the "belowhome" case. However, it may
+  still contain symbolic links. We can check for this by making use of
+  realpath(), which most Unixes seem to have (but make it possible to cut this
+  out). We can't just use realpath() on the whole file name, because we know
+  the file itself doesn't exist, and intermediate directories may also not
+  exist. What we want to know is the real path of the longest existing part of
+  the path. That must match the home directory's beginning, whichever is the
+  shorter. */
+
+  #ifndef NO_REALPATH
+  if (yield && create_file == create_belowhome)
+    {
+    uschar *slash, *next;
+    uschar *rp = NULL;
+    for (slash = Ustrrchr(file, '/');       /* There is known to be one */
+         rp == NULL && slash > file;        /* Stop if reached beginning */
+         slash = next)
+      {
+      *slash = 0;
+      rp = US realpath(CS file, CS big_buffer);
+      next = Ustrrchr(file, '/');
+      *slash = '/';
+      }
+
+    /* If rp == NULL it means that none of the relevant directories exist.
+    This is not a problem here - it means that no symbolic links can exist,
+    which is all we are worried about. Otherwise, we must compare it
+    against the start of the home directory. However, that may itself
+    contain symbolic links, so we have to "realpath" it as well, if
+    possible. */
+
+    if (rp != NULL)
+      {
+      uschar hdbuffer[PATH_MAX+1];
+      uschar *rph = deliver_home;
+      int rlen = Ustrlen(big_buffer);
+
+      rp = US realpath(CS deliver_home, CS hdbuffer);
+      if (rp != NULL)
+        {
+        rph = hdbuffer;
+        len = Ustrlen(rph);
+        }
+
+      if (rlen > len) rlen = len;
+      if (Ustrncmp(rph, big_buffer, rlen) != 0)
+        {
+        yield = FALSE;
+        DEBUG(D_transport) debug_printf("Real path \"%s\" does not match \"%s\"\n",
+          big_buffer, deliver_home);
+        }
+      }
+    }
+  #endif  /* NO_REALPATH */
+  }
+
+return yield;
+}
+
+
+
+/*************************************************
+*              Main entry point                  *
+*************************************************/
+
+/* See local README for general interface details. This transport always
+returns FALSE, indicating that the status which has been placed in the first
+address should be copied to any other addresses in a batch.
+
+Appendfile delivery is tricky and has led to various security problems in other
+mailers. The logic used here is therefore laid out in some detail. When this
+function is called, we are running in a subprocess which has had its gid and
+uid set to the appropriate values. Therefore, we cannot write directly to the
+exim logs. Any errors must be handled by setting appropriate return codes.
+Note that the default setting for addr->transport_return is DEFER, so it need
+not be set unless some other value is required.
+
+The code below calls geteuid() rather than getuid() to get the current uid
+because in weird configurations not running setuid root there may be a
+difference. In the standard configuration, where setuid() has been used in the
+delivery process, there will be no difference between the uid and the euid.
+
+(1)  If the af_file flag is set, this is a delivery to a file after .forward or
+     alias expansion. Otherwise, there must be a configured file name or
+     directory name.
+
+The following items apply in the case when a file name (as opposed to a
+directory name) is given, that is, when appending to a single file:
+
+(2f) Expand the file name.
+
+(3f) If the file name is /dev/null, return success (optimization).
+
+(4f) If the file_format options is set, open the file for reading, and check
+     that the bytes at the start of the file match one of the given strings.
+     If the check indicates a transport other than the current one should be
+     used, pass control to that other transport. Otherwise continue. An empty
+     or non-existent file matches the current transport. The file is closed
+     after the check.
+
+(5f) If a lock file is required, create it (see extensive separate comments
+     below about the algorithm for doing this). It is important to do this
+     before opening the mailbox if NFS is in use.
+
+(6f) Stat the file, using lstat() rather than stat(), in order to pick up
+     details of any symbolic link.
+
+(7f) If the file already exists:
+
+     Check the owner and group if necessary, and defer if they are wrong.
+
+     If it is a symbolic link AND the allow_symlink option is set (NOT the
+     default), go back to (6f) but this time use stat() instead of lstat().
+
+     If it's not a regular file (or FIFO when permitted), defer delivery.
+
+     Check permissions. If the required permissions are *less* than the
+     existing ones, or supplied by the address (often by the user via filter),
+     chmod() the file. Otherwise, defer.
+
+     Save the inode number.
+
+     Open with O_RDRW + O_APPEND, thus failing if the file has vanished.
+
+     If open fails because the file does not exist, go to (6f); on any other
+     failure, defer.
+
+     Check the inode number hasn't changed - I realize this isn't perfect (an
+     inode can be reused) but it's cheap and will catch some of the races.
+
+     Check it's still a regular file (or FIFO if permitted).
+
+     Check that the owner and permissions haven't changed.
+
+     If file_format is set, check that the file still matches the format for
+     the current transport. If not, defer delivery.
+
+(8f) If file does not exist initially:
+
+     Open with O_WRONLY + O_EXCL + O_CREAT with configured mode, unless we know
+     this is via a symbolic link (only possible if allow_symlinks is set), in
+     which case don't use O_EXCL, as it dosn't work.
+
+     If open fails because the file already exists, go to (6f). To avoid
+     looping for ever in a situation where the file is continuously being
+     created and deleted, all of this happens inside a loop that operates
+     lock_retries times and includes the fcntl and flock locking. If the
+     loop completes without the file getting opened, defer and request
+     freezing, because something really weird is happening.
+
+     If open fails for any other reason, defer for subsequent delivery except
+     when this is a file delivery resulting from an alias or forward expansion
+     and the error is EPERM or ENOENT or EACCES, in which case FAIL as this is
+     most likely a user rather than a configuration error.
+
+(9f) We now have the file checked and open for writing. If so configured, lock
+     it using fcntl, flock, or MBX locking rules. If this fails, close the file
+     and goto (6f), up to lock_retries times, after sleeping for a while. If it
+     still fails, give up and defer delivery.
+
+(10f)Save the access time (for subsequent restoration) and the size of the
+     file, for comsat and for re-setting if delivery fails in the middle -
+     e.g. for quota exceeded.
+
+The following items apply in the case when a directory name is given:
+
+(2d) Create a new file in the directory using a temporary name, by opening for
+     writing and with O_CREAT. If maildir format is being used, the file
+     is created in a temporary subdirectory with a prescribed name. If
+     mailstore format is being used, the envelope file is first created with a
+     temporary name, then the data file.
+
+The following items apply in all cases:
+
+(11) We now have the file open for writing, and locked if it was given as a
+     file name. Write the message and flush the file, unless there is a setting
+     of the local quota option, in which case we can check for its excession
+     without doing any writing.
+
+     In the case of MBX format mailboxes, the message is first written to a
+     temporary file, in order to get its correct length. This is then copied to
+     the real file, preceded by an MBX header.
+
+     If there is a quota error on writing, defer the address. Timeout logic
+     will determine for how long retries are attempted. We restore the mailbox
+     to its original length if it's a single file. There doesn't seem to be a
+     uniform error code for quota excession (it even differs between SunOS4
+     and some versions of SunOS5) so a system-dependent macro called
+     ERRNO_QUOTA is used for it, and the value gets put into errno_quota at
+     compile time.
+
+     For any other error (most commonly disk full), do the same.
+
+The following applies after appending to a file:
+
+(12f)Restore the atime; notify_comsat if required; close the file (which
+     unlocks it if it was locked). Delete the lock file if it exists.
+
+The following applies after writing a unique file in a directory:
+
+(12d)For maildir format, rename the file into the new directory. For mailstore
+     format, rename the envelope file to its correct name. Otherwise, generate
+     a unique name from the directory_file option, and rename to that, possibly
+     trying a few times if the file exists and re-expanding the name gives a
+     different string.
+
+This transport yields FAIL only when a file name is generated by an alias or
+forwarding operation and attempting to open it gives EPERM, ENOENT, or EACCES.
+All other failures return DEFER (in addr->transport_return). */
+
+
+BOOL
+appendfile_transport_entry(
+  transport_instance *tblock,      /* data for this instantiation */
+  address_item *addr)              /* address we are working on */
+{
+appendfile_transport_options_block *ob =
+  (appendfile_transport_options_block *)(tblock->options_block);
+struct stat statbuf;
+uschar *fdname = NULL;
+uschar *filename = NULL;
+uschar *hitchname = NULL;
+uschar *dataname = NULL;
+uschar *lockname = NULL;
+uschar *newname = NULL;
+uschar *nametag = NULL;
+uschar *cr = US"";
+uschar *filecount_msg = US"";
+uschar *path;
+struct utimbuf times;
+struct timeval msg_tv;
+BOOL disable_quota = FALSE;
+BOOL isdirectory = FALSE;
+BOOL isfifo = FALSE;
+BOOL wait_for_tick = FALSE;
+uid_t uid = geteuid();     /* See note above */
+gid_t gid = getegid();
+int mbformat;
+int mode = (addr->mode > 0)? addr->mode : ob->mode;
+off_t saved_size = -1;
+off_t mailbox_size = ob->mailbox_size_value;
+int mailbox_filecount = ob->mailbox_filecount_value;
+int hd = -1;
+int fd = -1;
+int yield = FAIL;
+int i;
+
+#ifdef SUPPORT_MBX
+int save_fd = 0;
+int mbx_lockfd = -1;
+uschar mbx_lockname[40];
+FILE *temp_file = NULL;
+#endif  /* SUPPORT_MBX */
+
+#ifdef SUPPORT_MAILDIR
+int maildirsize_fd = -1;      /* fd for maildirsize file */
+int maildir_save_errno;
+#endif
+
+
+DEBUG(D_transport) debug_printf("appendfile transport entered\n");
+
+/* An "address_file" or "address_directory" transport is used to deliver to
+files specified via .forward or an alias file. Prior to release 4.20, the
+"file" and "directory" options were ignored in this case. This has been changed
+to allow the redirection data to specify what is in effect a folder, whose
+location is determined by the options on the transport.
+
+Compatibility with the case when neither option is set is retained by forcing a
+value for the file or directory name. A directory delivery is assumed if the
+last character of the path from the router is '/'.
+
+The file path is in the local part of the address, but not in the $local_part
+variable (that holds the parent local part). It is, however, in the
+$address_file variable. Below, we update the local part in the address if it
+changes by expansion, so that the final path ends up in the log. */
+
+if (testflag(addr, af_file) &&
+    ob->filename == NULL &&
+    ob->dirname == NULL)
+  {
+  fdname = US"$address_file";
+  if (address_file[Ustrlen(address_file)-1] == '/' ||
+      ob->maildir_format ||
+      ob->mailstore_format)
+    isdirectory = TRUE;
+  }
+
+/* Handle (a) an "address file" delivery where "file" or "directory" is
+explicitly set and (b) a non-address_file delivery, where one of "file" or
+"directory" must be set; initialization ensures that they are not both set. */
+
+if (fdname == NULL)
+  {
+  fdname = ob->filename;
+  if (fdname == NULL)
+    {
+    fdname = ob->dirname;
+    isdirectory = TRUE;
+    }
+  if (fdname == NULL)
+    {
+    addr->transport_return = PANIC;
+    addr->message = string_sprintf("Mandatory file or directory option "
+      "missing from %s transport", tblock->name);
+    return FALSE;
+    }
+  }
+
+/* Maildir and mailstore require a directory */
+
+if ((ob->maildir_format || ob->mailstore_format) && !isdirectory)
+  {
+  addr->transport_return = PANIC;
+  addr->message = string_sprintf("mail%s_format requires \"directory\" "
+    "to be specified for the %s transport",
+    ob->maildir_format? "dir" : "store", tblock->name);
+  return FALSE;
+  }
+
+path = expand_string(fdname);
+
+if (path == NULL)
+  {
+  addr->transport_return = PANIC;
+  addr->message = string_sprintf("Expansion of \"%s\" (file or directory "
+    "name for %s transport) failed: %s", fdname, tblock->name,
+    expand_string_message);
+  return FALSE;
+  }
+
+if (path[0] != '/')
+  {
+  addr->message = string_sprintf("appendfile: file or directory name "
+    "\"%s\" is not absolute", path);
+  addr->basic_errno = ERRNO_NOTABSOLUTE;
+  return FALSE;
+  }
+
+/* For a file delivery, make sure the local part in the address(es) is updated
+to the true local part. */
+
+if (testflag(addr, af_file))
+  {
+  address_item *addr2;
+  for (addr2 = addr; addr2 != NULL; addr2 = addr2->next)
+    addr2->local_part = string_copy(path);
+  }
+
+/* The available mailbox formats depend on whether it is a directory or a file
+delivery. */
+
+if (isdirectory)
+  {
+  mbformat =
+  #ifdef SUPPORT_MAILDIR
+    (ob->maildir_format)? mbf_maildir :
+  #endif
+  #ifdef SUPPORT_MAILSTORE
+    (ob->mailstore_format)? mbf_mailstore :
+  #endif
+    mbf_smail;
+  }
+else
+  {
+  mbformat =
+  #ifdef SUPPORT_MBX
+    (ob->mbx_format)? mbf_mbx :
+  #endif
+    mbf_unix;
+  }
+
+DEBUG(D_transport)
+  {
+  debug_printf("appendfile: mode=%o notify_comsat=%d quota=" OFF_T_FMT
+    " warning=" OFF_T_FMT "%s\n"
+    "  %s=%s format=%s\n  message_prefix=%s\n  message_suffix=%s\n  "
+    "maildir_use_size_file=%s\n",
+    mode, ob->notify_comsat, ob->quota_value,
+    ob->quota_warn_threshold_value,
+    ob->quota_warn_threshold_is_percent? "%" : "",
+    isdirectory? "directory" : "file",
+    path, mailbox_formats[mbformat],
+    (ob->message_prefix == NULL)? US"null" : string_printing(ob->message_prefix),
+    (ob->message_suffix == NULL)? US"null" : string_printing(ob->message_suffix),
+    (ob->maildir_use_size_file)? "yes" : "no");
+
+  if (!isdirectory) debug_printf("  locking by %s%s%s%s%s\n",
+    ob->use_lockfile? "lockfile " : "",
+    ob->use_mbx_lock? "mbx locking (" : "",
+    ob->use_fcntl? "fcntl " : "",
+    ob->use_flock? "flock" : "",
+    ob->use_mbx_lock? ")" : "");
+  }
+
+/* If the -N option is set, can't do any more. */
+
+if (dont_deliver)
+  {
+  DEBUG(D_transport)
+    debug_printf("*** delivery by %s transport bypassed by -N option\n",
+      tblock->name);
+  addr->transport_return = OK;
+  return FALSE;
+  }
+
+/* Handle the case of a file name. If the file name is /dev/null, we can save
+ourselves some effort and just give a success return right away. */
+
+if (!isdirectory)
+  {
+  BOOL use_lstat = TRUE;
+  BOOL file_opened = FALSE;
+  BOOL allow_creation_here = TRUE;
+
+  if (Ustrcmp(path, "/dev/null") == 0)
+    {
+    addr->transport_return = OK;
+    return FALSE;
+    }
+
+  /* Set the name of the file to be opened, and the file to which the data
+  is written, and find out if we are permitted to create a non-existent file. */
+
+  dataname = filename = path;
+  allow_creation_here = check_creation(filename, ob->create_file);
+
+  /* If ob->create_directory is set, attempt to create the directories in
+  which this mailbox lives, but only if we are permitted to create the file
+  itself. We know we are dealing with an absolute path, because this was
+  checked above. */
+
+  if (ob->create_directory && allow_creation_here)
+    {
+    uschar *p = Ustrrchr(path, '/');
+    *p = '\0';
+    if (!directory_make(NULL, path, ob->dirmode, FALSE))
+      {
+      addr->basic_errno = errno;
+      addr->message =
+        string_sprintf("failed to create directories for %s: %s", path,
+          strerror(errno));
+      DEBUG(D_transport) debug_printf("%s transport: %s\n", tblock->name, path);
+      return FALSE;
+      }
+    *p = '/';
+    }
+
+  /* If file_format is set we must check that any existing file matches one of
+  the configured formats by checking the bytes it starts with. A match then
+  indicates a specific transport - if it is not this one, pass control to it.
+  Otherwise carry on here. An empty or non-existent file matches the current
+  transport. We don't need to distinguish between non-existence and other open
+  failures because if an existing file fails to open here, it will also fail
+  again later when O_RDWR is used. */
+
+  if (ob->file_format != NULL)
+    {
+    int cfd = Uopen(path, O_RDONLY, 0);
+    if (cfd >= 0)
+      {
+      transport_instance *tt = check_file_format(cfd, tblock, addr);
+      (void)close(cfd);
+
+      /* If another transport is indicated, call it and return; if no transport
+      was found, just return - the error data will have been set up.*/
+
+      if (tt != tblock)
+        {
+        if (tt != NULL)
+          {
+          set_process_info("delivering %s to %s using %s", message_id,
+            addr->local_part, tt->name);
+          debug_print_string(tt->debug_string);
+          addr->transport = tt;
+          (tt->info->code)(tt, addr);
+          }
+        return FALSE;
+        }
+      }
+    }
+
+  /* The locking of mailbox files is worse than the naming of cats, which is
+  known to be "a difficult matter" (T.S. Eliot) and just as cats must have
+  three different names, so several different styles of locking are used.
+
+  Research in other programs that lock mailboxes shows that there is no
+  universally standard method. Having mailboxes NFS-mounted on the system that
+  is delivering mail is not the best thing, but people do run like this,
+  and so the code must do its best to cope.
+
+  Three different locking mechanisms are supported. The initialization function
+  checks that at least one is configured.
+
+  LOCK FILES
+
+  Unless no_use_lockfile is set, we attempt to build a lock file in a way that
+  will work over NFS. Only after that is done do we actually open the mailbox
+  and apply locks to it (if configured).
+
+  Originally, Exim got the file opened before doing anything about locking.
+  However, a very occasional problem was observed on Solaris 2 when delivering
+  over NFS. It is seems that when a file is opened with O_APPEND, the file size
+  gets remembered at open time. If another process on another host (that's
+  important) has the file open and locked and writes to it and then releases
+  the lock while the first process is waiting to get the lock, the first
+  process may fail to write at the new end point of the file - despite the very
+  definite statement about O_APPEND in the man page for write(). Experiments
+  have reproduced this problem, but I do not know any way of forcing a host to
+  update its attribute cache for an open NFS file. It would be nice if it did
+  so when a lock was taken out, but this does not seem to happen. Anyway, to
+  reduce the risk of this problem happening, we now create the lock file
+  (if configured) *before* opening the mailbox. That will prevent two different
+  Exims opening the file simultaneously. It may not prevent clashes with MUAs,
+  however, but Pine at least seems to operate in the same way.
+
+  Lockfiles should normally be used when NFS is involved, because of the above
+  problem.
+
+  The logic for creating the lock file is:
+
+  . The name of the lock file is <mailbox-name>.lock
+
+  . First, create a "hitching post" name by adding the primary host name,
+    current time and pid to the lock file name. This should be unique.
+
+  . Create the hitching post file using WRONLY + CREAT + EXCL.
+
+  . If that fails EACCES, we assume it means that the user is unable to create
+    files in the mail spool directory. Some installations might operate in this
+    manner, so there is a configuration option to allow this state not to be an
+    error - we proceed to lock using fcntl only, after the file is open.
+
+  . Otherwise, an error causes a deferment of the address.
+
+  . Hard link the hitching post to the lock file name.
+
+  . If the link succeeds, we have successfully created the lock file. Simply
+    close and unlink the hitching post file.
+
+  . If the link does not succeed, proceed as follows:
+
+    o Fstat the hitching post file, and then close and unlink it.
+
+    o Now examine the stat data. If the number of links to the file is exactly
+      2, the linking succeeded but for some reason, e.g. an NFS server crash,
+      the return never made it back, so the link() function gave a failure
+      return.
+
+  . This method allows for the lock file to be created by some other process
+    right up to the moment of the attempt to hard link it, and is also robust
+    against NFS server crash-reboots, which would probably result in timeouts
+    in the middle of link().
+
+  . System crashes may cause lock files to get left lying around, and some means
+    of flushing them is required. The approach of writing a pid (used by smail
+    and by elm) into the file isn't useful when NFS may be in use. Pine uses a
+    timeout, which seems a better approach. Since any program that writes to a
+    mailbox using a lock file should complete its task very quickly, Pine
+    removes lock files that are older than 5 minutes. We allow the value to be
+    configurable on the transport.
+
+  FCNTL LOCKING
+
+  If use_fcntl_lock is set, then Exim gets an exclusive fcntl() lock on the
+  mailbox once it is open. This is done by default with a non-blocking lock.
+  Failures to lock cause retries after a sleep, but only for a certain number
+  of tries. A blocking lock is deliberately not used so that we don't hold the
+  mailbox open. This minimizes the possibility of the NFS problem described
+  under LOCK FILES above, if for some reason NFS deliveries are happening
+  without lock files. However, the use of a non-blocking lock and sleep, though
+  the safest approach, does not give the best performance on very busy systems.
+  A blocking lock plus timeout does better. Therefore Exim has an option to
+  allow it to work this way. If lock_fcntl_timeout is set greater than zero, it
+  enables the use of blocking fcntl() calls.
+
+  FLOCK LOCKING
+
+  If use_flock_lock is set, then Exim gets an exclusive flock() lock in the
+  same manner as for fcntl locking above. No-blocking/timeout is also set as
+  above in lock_flock_timeout. Not all operating systems provide or support
+  flock(). For those that don't (as determined by the definition of LOCK_SH in
+  /usr/include/sys/file.h), use_flock_lock may not be set. For some OS, flock()
+  is implemented (not precisely) on top of fcntl(), which means there's no
+  point in actually using it.
+
+  MBX LOCKING
+
+  If use_mbx_lock is set (this is supported only if SUPPORT_MBX is defined)
+  then the rules used for locking in c-client are used. Exim takes out a shared
+  lock on the mailbox file, and an exclusive lock on the file whose name is
+  /tmp/.<device-number>.<inode-number>. The shared lock on the mailbox stops
+  any other MBX client from getting an exclusive lock on it and expunging it.
+  It also stops any other MBX client from unlinking the /tmp lock when it has
+  finished with it.
+
+  The exclusive lock on the /tmp file prevents any other MBX client from
+  updating the mailbox in any way. When writing is finished, if an exclusive
+  lock on the mailbox can be obtained, indicating there are no current sharers,
+  the /tmp file is unlinked.
+
+  MBX locking can use either fcntl() or flock() locking. If neither
+  use_fcntl_lock or use_flock_lock is set, it defaults to using fcntl() only.
+  The calls for getting these locks are by default non-blocking, as for non-mbx
+  locking, but can be made blocking by setting lock_fcntl_timeout and/or
+  lock_flock_timeout as appropriate.  As MBX delivery doesn't work over NFS, it
+  probably makes sense to set timeouts for any MBX deliveries. */
+
+
+  /* Build a lock file if configured to do so - the existence of a lock
+  file is subsequently checked by looking for a non-negative value of the
+  file descriptor hd - even though the file is no longer open. */
+
+  if (ob->use_lockfile)
+    {
+    /* cf. exim_lock.c */
+    lockname = string_sprintf("%s.lock", filename);
+    hitchname = string_sprintf( "%s.%s.%08x.%08x", lockname, primary_hostname,
+      (unsigned int)(time(NULL)), (unsigned int)getpid());
+
+    DEBUG(D_transport) debug_printf("lock name: %s\nhitch name: %s\n", lockname,
+      hitchname);
+
+    /* Lock file creation retry loop */
+
+    for (i = 0; i < ob->lock_retries; sleep(ob->lock_interval), i++)
+      {
+      int rc;
+      hd = Uopen(hitchname, O_WRONLY | O_CREAT | O_EXCL, ob->lockfile_mode);
+
+      if (hd < 0)
+        {
+        addr->basic_errno = errno;
+        addr->message =
+          string_sprintf("creating lock file hitching post %s "
+            "(euid=%ld egid=%ld)", hitchname, (long int)geteuid(),
+            (long int)getegid());
+        return FALSE;
+        }
+
+      /* Attempt to hitch the hitching post to the lock file. If link()
+      succeeds (the common case, we hope) all is well. Otherwise, fstat the
+      file, and get rid of the hitching post. If the number of links was 2,
+      the link was created, despite the failure of link(). If the hitch was
+      not successful, try again, having unlinked the lock file if it is too
+      old.
+
+      There's a version of Linux (2.0.27) which doesn't update its local cache
+      of the inode after link() by default - which many think is a bug - but
+      if the link succeeds, this code will be OK. It just won't work in the
+      case when link() fails after having actually created the link. The Linux
+      NFS person is fixing this; a temporary patch is available if anyone is
+      sufficiently worried. */
+
+      if ((rc = Ulink(hitchname, lockname)) != 0) fstat(hd, &statbuf);
+      (void)close(hd);
+      Uunlink(hitchname);
+      if (rc != 0 && statbuf.st_nlink != 2)
+        {
+        if (ob->lockfile_timeout > 0 && Ustat(lockname, &statbuf) == 0 &&
+            time(NULL) - statbuf.st_ctime > ob->lockfile_timeout)
+          {
+          DEBUG(D_transport) debug_printf("unlinking timed-out lock file\n");
+          Uunlink(lockname);
+          }
+        DEBUG(D_transport) debug_printf("link of hitching post failed - retrying\n");
+        continue;
+        }
+
+      DEBUG(D_transport) debug_printf("lock file created\n");
+      break;
+      }
+
+    /* Check for too many tries at creating the lock file */
+
+    if (i >= ob->lock_retries)
+      {
+      addr->basic_errno = ERRNO_LOCKFAILED;
+      addr->message = string_sprintf("failed to lock mailbox %s (lock file)",
+        filename);
+      return FALSE;
+      }
+    }
+
+
+  /* We now have to get the file open. First, stat() it and act on existence or
+  non-existence. This is in a loop to handle the case of a file's being created
+  or deleted as we watch, and also to handle retries when the locking fails.
+  Rather than holding the file open while waiting for the fcntl() and/or
+  flock() lock, we close and do the whole thing again. This should be safer,
+  especially for NFS files, which might get altered from other hosts, making
+  their cached sizes incorrect.
+
+  With the default settings, no symlinks are permitted, but there is an option
+  to permit symlinks for those sysadmins that know what they are doing.
+  Shudder. However, insist that the initial symlink is owned by the right user.
+  Thus lstat() is used initially; if a symlink is discovered, the loop is
+  repeated such that stat() is used, to look at the end file. */
+
+  for (i = 0; i < ob->lock_retries; i++)
+    {
+    int sleep_before_retry = TRUE;
+    file_opened = FALSE;
+
+    if((use_lstat? Ulstat(filename, &statbuf) : Ustat(filename, &statbuf)) != 0)
+      {
+      /* Let's hope that failure to stat (other than non-existence) is a
+      rare event. */
+
+      if (errno != ENOENT)
+        {
+        addr->basic_errno = errno;
+        addr->message = string_sprintf("attempting to stat mailbox %s",
+          filename);
+        goto RETURN;
+        }
+
+      /* File does not exist. If it is required to pre-exist this state is an
+      error. */
+
+      if (ob->file_must_exist)
+        {
+        addr->basic_errno = errno;
+        addr->message = string_sprintf("mailbox %s does not exist, "
+          "but file_must_exist is set", filename);
+        goto RETURN;
+        }
+
+      /* If not permitted to create this file because it isn't in or below
+      the home directory, generate an error. */
+
+      if (!allow_creation_here)
+        {
+        addr->basic_errno = ERRNO_BADCREATE;
+        addr->message = string_sprintf("mailbox %s does not exist, "
+          "but creation outside the home directory is not permitted",
+          filename);
+        goto RETURN;
+        }
+
+      /* Attempt to create and open the file. If open fails because of
+      pre-existence, go round the loop again. For any other error, defer the
+      address, except for an alias or forward generated file name with EPERM,
+      ENOENT, or EACCES, as those are most likely to be user errors rather
+      than Exim config errors. When a symbolic link is permitted and points
+      to a non-existent file, we get here with use_lstat = FALSE. In this case
+      we mustn't use O_EXCL, since it doesn't work. The file is opened RDRW for
+      consistency and because MBX locking requires it in order to be able to
+      get a shared lock. */
+
+      fd = Uopen(filename, O_RDWR | O_APPEND | O_CREAT |
+        (use_lstat? O_EXCL : 0), mode);
+      if (fd < 0)
+        {
+        if (errno == EEXIST) continue;
+        addr->basic_errno = errno;
+        addr->message = string_sprintf("while creating mailbox %s",
+          filename);
+        if (testflag(addr, af_file) &&
+            (errno == EPERM || errno == ENOENT || errno == EACCES))
+          addr->transport_return = FAIL;
+        goto RETURN;
+        }
+
+      /* We have successfully created and opened the file. Ensure that the group
+      and the mode are correct. */
+
+      if(Uchown(filename, uid, gid) || Uchmod(filename, mode))
+        {
+        addr->basic_errno = errno;
+        addr->message = string_sprintf("while setting perms on mailbox %s",
+          filename);
+        addr->transport_return = FAIL;
+        goto RETURN;
+        }
+      }
+
+
+    /* The file already exists. Test its type, ownership, and permissions, and
+    save the inode for checking later. If symlinks are permitted (not the
+    default or recommended state) it may be a symlink that already exists.
+    Check its ownership and then look for the file at the end of the link(s).
+    This at least prevents one user creating a symlink for another user in
+    a sticky directory. */
+
+    else
+      {
+      int oldmode = (int)statbuf.st_mode;
+      ino_t inode = statbuf.st_ino;
+      BOOL islink = (oldmode & S_IFMT) == S_IFLNK;
+
+      isfifo = FALSE;        /* In case things are changing */
+
+      /* Check owner if required - the default. */
+
+      if (ob->check_owner && statbuf.st_uid != uid)
+        {
+        addr->basic_errno = ERRNO_BADUGID;
+        addr->message = string_sprintf("mailbox %s%s has wrong uid "
+          "(%ld != %ld)", filename,
+          islink? " (symlink)" : "",
+          (long int)(statbuf.st_uid), (long int)uid);
+        goto RETURN;
+        }
+
+      /* Group is checked only if check_group is set. */
+
+      if (ob->check_group && statbuf.st_gid != gid)
+        {
+        addr->basic_errno = ERRNO_BADUGID;
+        addr->message = string_sprintf("mailbox %s%s has wrong gid (%d != %d)",
+          filename, islink? " (symlink)" : "", statbuf.st_gid, gid);
+        goto RETURN;
+        }
+
+      /* Just in case this is a sticky-bit mail directory, we don't want
+      users to be able to create hard links to other users' files. */
+
+      if (statbuf.st_nlink != 1)
+        {
+        addr->basic_errno = ERRNO_NOTREGULAR;
+        addr->message = string_sprintf("mailbox %s%s has too many links (%d)",
+          filename, islink? " (symlink)" : "", statbuf.st_nlink);
+        goto RETURN;
+
+        }
+
+      /* If symlinks are permitted (not recommended), the lstat() above will
+      have found the symlink. Its ownership has just been checked; go round
+      the loop again, using stat() instead of lstat(). That will never yield a
+      mode of S_IFLNK. */
+
+      if (islink && ob->allow_symlink)
+        {
+        use_lstat = FALSE;
+        i--;                   /* Don't count this time round */
+        continue;
+        }
+
+      /* An actual file exists. Check that it is a regular file, or FIFO
+      if permitted. */
+
+      if (ob->allow_fifo && (oldmode & S_IFMT) == S_IFIFO) isfifo = TRUE;
+
+      else if ((oldmode & S_IFMT) != S_IFREG)
+        {
+        addr->basic_errno = ERRNO_NOTREGULAR;
+        addr->message = string_sprintf("mailbox %s is not a regular file%s",
+          filename, ob->allow_fifo? " or named pipe" : "");
+        goto RETURN;
+        }
+
+      /* If the mode is not what it would be for a newly created file, change
+      the permissions if the mode is supplied for the address. Otherwise,
+      reduce but do not extend the permissions. If the newly created
+      permissions are greater than the existing permissions, don't change
+      things when the mode is not from the address. */
+
+      if ((oldmode = (oldmode & 07777)) != mode)
+        {
+        int diffs = oldmode ^ mode;
+        if (addr->mode > 0 || (diffs & oldmode) == diffs)
+          {
+          DEBUG(D_transport) debug_printf("chmod %o %s\n", mode, filename);
+          if (Uchmod(filename, mode) < 0)
+            {
+            addr->basic_errno = errno;
+            addr->message = string_sprintf("attempting to chmod mailbox %s",
+              filename);
+            goto RETURN;
+            }
+          oldmode = mode;
+          }
+
+        /* Mode not from address, and newly-created permissions are greater
+        than existing permissions. Default is to complain, but it can be
+        configured to go ahead and try to deliver anyway if that's what
+        the administration wants. */
+
+        else if (ob->mode_fail_narrower)
+          {
+          addr->basic_errno = ERRNO_BADMODE;
+          addr->message = string_sprintf("mailbox %s has the wrong mode %o "
+            "(%o expected)", filename, oldmode, mode);
+          goto RETURN;
+          }
+        }
+
+      /* We are happy with the existing file. Open it, and then do further
+      tests to ensure that it is the same file that we were just looking at.
+      If the file does not now exist, restart this loop, going back to using
+      lstat again. For an NFS error, just defer; other opening errors are
+      more serious. The file is opened RDWR so that its format can be checked,
+      and also MBX locking requires the use of a shared (read) lock. However,
+      a FIFO is opened WRONLY + NDELAY so that it fails if there is no process
+      reading the pipe. */
+
+      fd = Uopen(filename, isfifo? (O_WRONLY|O_NDELAY) : (O_RDWR|O_APPEND),
+        mode);
+      if (fd < 0)
+        {
+        if (errno == ENOENT)
+          {
+          use_lstat = TRUE;
+          continue;
+          }
+        addr->basic_errno = errno;
+        if (isfifo)
+          {
+          addr->message = string_sprintf("while opening named pipe %s "
+            "(could mean no process is reading it)", filename);
+          }
+        else if (errno != EWOULDBLOCK)
+          {
+          addr->message = string_sprintf("while opening mailbox %s", filename);
+          }
+        goto RETURN;
+        }
+
+      /* This fstat really shouldn't fail, as we have an open file! There's a
+      dilemma here. We use fstat in order to be sure we are peering at the file
+      we have got open. However, that won't tell us if the file was reached
+      via a symbolic link. We checked this above, but there is a race exposure
+      if the link was created between the previous lstat and the open. However,
+      it would have to be created with the same inode in order to pass the
+      check below. If ob->allow_symlink is set, causing the use of stat rather
+      than lstat above, symbolic links may be there anyway, and the checking is
+      weaker. */
+
+      if (fstat(fd, &statbuf) < 0)
+        {
+        addr->basic_errno = errno;
+        addr->message = string_sprintf("attempting to stat open mailbox %s",
+          filename);
+        goto RETURN;
+        }
+
+      /* Check the inode; this is isn't a perfect check, but gives some
+      confidence. */
+
+      if (inode != statbuf.st_ino)
+        {
+        addr->basic_errno = ERRNO_INODECHANGED;
+        addr->message = string_sprintf("opened mailbox %s inode number changed "
+          "from %d to %ld", filename, inode, statbuf.st_ino);
+        addr->special_action = SPECIAL_FREEZE;
+        goto RETURN;
+        }
+
+      /* Check it's still a regular file or FIFO, and the uid, gid, and
+      permissions have not changed. */
+
+      if ((!isfifo && (statbuf.st_mode & S_IFMT) != S_IFREG) ||
+          (isfifo && (statbuf.st_mode & S_IFMT) != S_IFIFO))
+        {
+        addr->basic_errno = ERRNO_NOTREGULAR;
+        addr->message =
+          string_sprintf("opened mailbox %s is no longer a %s", filename,
+            isfifo? "named pipe" : "regular file");
+        addr->special_action = SPECIAL_FREEZE;
+        goto RETURN;
+        }
+
+      if ((ob->check_owner && statbuf.st_uid != uid) ||
+          (ob->check_group && statbuf.st_gid != gid))
+        {
+        addr->basic_errno = ERRNO_BADUGID;
+        addr->message =
+          string_sprintf("opened mailbox %s has wrong uid or gid", filename);
+        addr->special_action = SPECIAL_FREEZE;
+        goto RETURN;
+        }
+
+      if ((statbuf.st_mode & 07777) != oldmode)
+        {
+        addr->basic_errno = ERRNO_BADMODE;
+        addr->message = string_sprintf("opened mailbox %s has wrong mode %o "
+          "(%o expected)", filename, statbuf.st_mode & 07777, mode);
+        addr->special_action = SPECIAL_FREEZE;
+        goto RETURN;
+        }
+
+      /* If file_format is set, check that the format of the file has not
+      changed. Error data is set by the testing function. */
+
+      if (ob->file_format != NULL &&
+          check_file_format(fd, tblock, addr) != tblock)
+        {
+        addr->message = US"open mailbox has changed format";
+        goto RETURN;
+        }
+
+      /* The file is OK. Carry on to do the locking. */
+      }
+
+    /* We now have an open file, and must lock it using fcntl(), flock() or MBX
+    locking rules if configured to do so. If a lock file is also required, it
+    was created above and hd was left >= 0. At least one form of locking is
+    required by the initialization function. If locking fails here, close the
+    file and go round the loop all over again, after waiting for a bit, unless
+    blocking locking was used. */
+
+    file_opened = TRUE;
+    if ((ob->lock_fcntl_timeout > 0) || (ob->lock_flock_timeout > 0))
+      sleep_before_retry = FALSE;
+
+    /* Simple fcntl() and/or flock() locking */
+
+    if (!ob->use_mbx_lock && (ob->use_fcntl || ob->use_flock))
+      {
+      if (apply_lock(fd, F_WRLCK, ob->use_fcntl, ob->lock_fcntl_timeout,
+         ob->use_flock, ob->lock_flock_timeout) >= 0) break;
+      }
+
+    /* MBX locking rules */
+
+    #ifdef SUPPORT_MBX
+    else if (ob->use_mbx_lock)
+      {
+      int mbx_tmp_oflags;
+      struct stat lstatbuf, statbuf2;
+      if (apply_lock(fd, F_RDLCK, ob->use_fcntl, ob->lock_fcntl_timeout,
+           ob->use_flock, ob->lock_flock_timeout) >= 0 &&
+           fstat(fd, &statbuf) >= 0)
+        {
+        sprintf(CS mbx_lockname, "/tmp/.%lx.%lx", (long)statbuf.st_dev,
+          (long)statbuf.st_ino);
+
+        /*
+         * 2010-05-29: SECURITY
+         * Dan Rosenberg reported the presence of a race-condition in the
+         * original code here.  Beware that many systems still allow symlinks
+         * to be followed in /tmp so an attacker can create a symlink pointing
+         * elsewhere between a stat and an open, which we should avoid
+         * following.
+         *
+         * It's unfortunate that we can't just use all the heavily debugged
+         * locking from above.
+         *
+         * Also: remember to mirror changes into exim_lock.c */
+
+        /* first leave the old pre-check in place, it provides better
+         * diagnostics for common cases */
+        if (Ulstat(mbx_lockname, &statbuf) >= 0)
+          {
+          if ((statbuf.st_mode & S_IFMT) == S_IFLNK)
+            {
+            addr->basic_errno = ERRNO_LOCKFAILED;
+            addr->message = string_sprintf("symbolic link on MBX lock file %s",
+              mbx_lockname);
+            goto RETURN;
+            }
+          if (statbuf.st_nlink > 1)
+            {
+            addr->basic_errno = ERRNO_LOCKFAILED;
+            addr->message = string_sprintf("hard link to MBX lock file %s",
+              mbx_lockname);
+            goto RETURN;
+            }
+          }
+
+        /* If we could just declare "we must be the ones who create this
+         * file" then a hitching post in a subdir would work, since a
+         * subdir directly in /tmp/ which we create wouldn't follow links
+         * but this isn't our locking logic, so we can't safely change the
+         * file existence rules. */
+
+        /* On systems which support O_NOFOLLOW, it's the easiest and most
+         * obviously correct security fix */
+        mbx_tmp_oflags = O_RDWR | O_CREAT;
+#ifdef O_NOFOLLOW
+        mbx_tmp_oflags |= O_NOFOLLOW;
+#endif
+        mbx_lockfd = Uopen(mbx_lockname, mbx_tmp_oflags, ob->lockfile_mode);
+        if (mbx_lockfd < 0)
+          {
+          addr->basic_errno = ERRNO_LOCKFAILED;
+          addr->message = string_sprintf("failed to open MBX lock file %s :%s",
+            mbx_lockname, strerror(errno));
+          goto RETURN;
+          }
+
+        if (Ulstat(mbx_lockname, &lstatbuf) < 0)
+          {
+          addr->basic_errno = ERRNO_LOCKFAILED;
+          addr->message = string_sprintf("attempting to lstat open MBX "
+             "lock file %s: %s", mbx_lockname, strerror(errno));
+          goto RETURN;
+          }
+        if (fstat(mbx_lockfd, &statbuf2) < 0)
+          {
+          addr->basic_errno = ERRNO_LOCKFAILED;
+          addr->message = string_sprintf("attempting to stat fd of open MBX "
+              "lock file %s: %s", mbx_lockname, strerror(errno));
+          goto RETURN;
+          }
+
+        /*
+         * At this point:
+         *  statbuf: if exists, is file which existed prior to opening the
+         *           lockfile, might have been replaced since then
+         *  statbuf2: result of stat'ing the open fd, is what was actually
+         *            opened
+         *  lstatbuf: result of lstat'ing the filename immediately after
+         *            the open but there's a race condition again between
+         *            those two steps: before open, symlink to foo, after
+         *            open but before lstat have one of:
+         *             * was no symlink, so is the opened file
+         *               (we created it, no messing possible after that point)
+         *             * hardlink to foo
+         *             * symlink elsewhere
+         *             * hardlink elsewhere
+         *             * new file/other
+         * Don't want to compare to device of /tmp because some modern systems
+         * have regressed to having /tmp be the safe actual filesystem as
+         * valuable data, so is mostly worthless, unless we assume that *only*
+         * Linux systems do this and that all Linux has O_NOFOLLOW.  Something
+         * for further consideration.
+         * No point in doing a readlink on the lockfile as that will always be
+         * at a different point in time from when we open it, so tells us
+         * nothing; attempts to clean up and delete after ourselves would risk
+         * deleting a *third* filename.
+         */
+        if ((statbuf2.st_nlink > 1) ||
+            (lstatbuf.st_nlink > 1) ||
+            (!S_ISREG(lstatbuf.st_mode)) ||
+            (lstatbuf.st_dev != statbuf2.st_dev) ||
+            (lstatbuf.st_ino != statbuf2.st_ino))
+          {
+          addr->basic_errno = ERRNO_LOCKFAILED;
+          addr->message = string_sprintf("RACE CONDITION detected: "
+              "mismatch post-initial-checks between \"%s\" and opened "
+              "fd lead us to abort!", mbx_lockname);
+          goto RETURN;
+          }
+
+        (void)Uchmod(mbx_lockname, ob->lockfile_mode);
+
+        if (apply_lock(mbx_lockfd, F_WRLCK, ob->use_fcntl,
+            ob->lock_fcntl_timeout, ob->use_flock, ob->lock_flock_timeout) >= 0)
+          {
+          struct stat ostatbuf;
+
+          /* This tests for a specific race condition. Ensure that we still
+          have the same file. */
+
+          if (Ulstat(mbx_lockname, &statbuf) == 0 &&
+              fstat(mbx_lockfd, &ostatbuf) == 0 &&
+              statbuf.st_dev == ostatbuf.st_dev &&
+              statbuf.st_ino == ostatbuf.st_ino)
+            break;
+          DEBUG(D_transport) debug_printf("MBX lockfile %s changed "
+            "between creation and locking\n", mbx_lockname);
+          }
+
+        DEBUG(D_transport) debug_printf("failed to lock %s: %s\n", mbx_lockname,
+          strerror(errno));
+        (void)close(mbx_lockfd);
+        mbx_lockfd = -1;
+        }
+      else
+        {
+        DEBUG(D_transport) debug_printf("failed to fstat or get read lock on %s: %s\n",
+          filename, strerror(errno));
+        }
+      }
+    #endif  /* SUPPORT_MBX */
+
+    else break;   /* No on-file locking required; break the open/lock loop */
+
+    DEBUG(D_transport)
+      debug_printf("fcntl(), flock(), or MBX locking failed - retrying\n");
+
+    (void)close(fd);
+    fd = -1;
+    use_lstat = TRUE;             /* Reset to use lstat first */
+
+
+    /* If a blocking call timed out, break the retry loop if the total time
+    so far is not less than than retries * interval. Use the larger of the
+    flock() and fcntl() timeouts. */
+
+    if (sigalrm_seen &&
+        (i+1) * ((ob->lock_fcntl_timeout > ob->lock_flock_timeout)?
+          ob->lock_fcntl_timeout : ob->lock_flock_timeout) >=
+          ob->lock_retries * ob->lock_interval)
+      i = ob->lock_retries;
+
+    /* Wait a bit before retrying, except when it was a blocked fcntl() or
+    flock() that caused the problem. */
+
+    if (i < ob->lock_retries && sleep_before_retry) sleep(ob->lock_interval);
+    }
+
+  /* Test for exceeding the maximum number of tries. Either the file remains
+  locked, or, if we haven't got it open, something is terribly wrong... */
+
+  if (i >= ob->lock_retries)
+    {
+    if (!file_opened)
+      {
+      addr->basic_errno = ERRNO_EXISTRACE;
+      addr->message = string_sprintf("mailbox %s: existence unclear", filename);
+      addr->special_action = SPECIAL_FREEZE;
+      }
+    else
+      {
+      addr->basic_errno = ERRNO_LOCKFAILED;
+      addr->message = string_sprintf("failed to lock mailbox %s (fcntl/flock)",
+        filename);
+      }
+    goto RETURN;
+    }
+
+  DEBUG(D_transport) debug_printf("mailbox %s is locked\n", filename);
+
+  /* Save access time (for subsequent restoration), modification time (for
+  restoration if updating fails), size of file (for comsat and for re-setting if
+  delivery fails in the middle - e.g. for quota exceeded). */
+
+  if (fstat(fd, &statbuf) < 0)
+    {
+    addr->basic_errno = errno;
+    addr->message = string_sprintf("while fstatting opened mailbox %s",
+      filename);
+    goto RETURN;
+    }
+
+  times.actime = statbuf.st_atime;
+  times.modtime = statbuf.st_mtime;
+  saved_size = statbuf.st_size;
+  if (mailbox_size < 0) mailbox_size = saved_size;
+  mailbox_filecount = 0;  /* Not actually relevant for single-file mailbox */
+  }
+
+/* Prepare for writing to a new file (as opposed to appending to an old one).
+There are several different formats, but there is preliminary stuff concerned
+with quotas that applies to all of them. Finding the current size by directory
+scanning is expensive; for maildirs some fudges have been invented:
+
+  (1) A regex can be used to extract a file size from its name;
+  (2) If maildir_use_size is set, a maildirsize file is used to cache the
+      mailbox size.
+*/
+
+else
+  {
+  uschar *check_path = path;    /* Default quota check path */
+  const pcre *regex = NULL;     /* Regex for file size from file name */
+
+  if (!check_creation(string_sprintf("%s/any", path), ob->create_file))
+    {
+    addr->basic_errno = ERRNO_BADCREATE;
+    addr->message = string_sprintf("tried to create file in %s, but "
+      "file creation outside the home directory is not permitted", path);
+    goto RETURN;
+    }
+
+  #ifdef SUPPORT_MAILDIR
+  /* For a maildir delivery, ensure that all the relevant directories exist,
+  and a maildirfolder file if necessary. */
+
+  if (mbformat == mbf_maildir && !maildir_ensure_directories(path, addr,
+    ob->create_directory, ob->dirmode, ob->maildirfolder_create_regex))
+      return FALSE;
+  #endif  /* SUPPORT_MAILDIR */
+
+  /* If we are going to do a quota check, of if maildir_use_size_file is set
+  for a maildir delivery, compile the regular expression if there is one. We
+  may also need to adjust the path that is used. We need to do this for
+  maildir_use_size_file even if the quota is unset, because we still want to
+  create the file. When maildir support is not compiled,
+  ob->maildir_use_size_file is always FALSE. */
+
+  if (ob->quota_value > 0 || THRESHOLD_CHECK || ob->maildir_use_size_file)
+    {
+    const uschar *error;
+    int offset;
+
+    /* Compile the regex if there is one. */
+
+    if (ob->quota_size_regex != NULL)
+      {
+      regex = pcre_compile(CS ob->quota_size_regex, PCRE_COPT,
+        (const char **)&error, &offset, NULL);
+      if (regex == NULL)
+        {
+        addr->message = string_sprintf("appendfile: regular expression "
+          "error: %s at offset %d while compiling %s", error, offset,
+          ob->quota_size_regex);
+        return FALSE;
+        }
+      DEBUG(D_transport) debug_printf("using regex for file sizes: %s\n",
+        ob->quota_size_regex);
+      }
+
+    /* Use an explicitly configured directory if set */
+
+    if (ob->quota_directory != NULL)
+      {
+      check_path = expand_string(ob->quota_directory);
+      if (check_path == NULL)
+        {
+        addr->transport_return = PANIC;
+        addr->message = string_sprintf("Expansion of \"%s\" (quota_directory "
+         "name for %s transport) failed: %s", ob->quota_directory,
+          tblock->name, expand_string_message);
+        return FALSE;
+        }
+
+      if (check_path[0] != '/')
+        {
+        addr->message = string_sprintf("appendfile: quota_directory name "
+          "\"%s\" is not absolute", check_path);
+        addr->basic_errno = ERRNO_NOTABSOLUTE;
+        return FALSE;
+        }
+      }
+
+    #ifdef SUPPORT_MAILDIR
+    /* Otherwise, if we are handling a maildir delivery, and the directory
+    contains a file called maildirfolder, this is a maildir++ feature telling
+    us that this is a sub-directory of the real inbox. We should therefore do
+    the quota check on the parent directory. Beware of the special case when
+    the directory name itself ends in a slash. */
+
+    else if (mbformat == mbf_maildir)
+      {
+      struct stat statbuf;
+      if (Ustat(string_sprintf("%s/maildirfolder", path), &statbuf) >= 0)
+        {
+        uschar *new_check_path = string_copy(check_path);
+        uschar *slash = Ustrrchr(new_check_path, '/');
+        if (slash != NULL)
+          {
+          if (slash[1] == 0)
+            {
+            *slash = 0;
+            slash = Ustrrchr(new_check_path, '/');
+            }
+          if (slash != NULL)
+            {
+            *slash = 0;
+            check_path = new_check_path;
+            DEBUG(D_transport) debug_printf("maildirfolder file exists: "
+              "quota check directory changed to %s\n", check_path);
+            }
+          }
+        }
+      }
+    #endif  /* SUPPORT_MAILDIR */
+    }
+
+  /* If we are using maildirsize files, we need to ensure that such a file
+  exists and, if necessary, recalculate its contents. As a byproduct of this,
+  we obtain the current size of the maildir. If no quota is to be enforced
+  (ob->quota_value == 0), we still need the size if a threshold check will
+  happen later.
+
+  Another regular expression is used to determine which directories inside the
+  maildir are going to be counted. */
+
+  #ifdef SUPPORT_MAILDIR
+  if (ob->maildir_use_size_file)
+    {
+    const pcre *dir_regex = NULL;
+    const uschar *error;
+    int offset;
+
+    if (ob->maildir_dir_regex != NULL)
+      {
+      int check_path_len = Ustrlen(check_path);
+
+      dir_regex = pcre_compile(CS ob->maildir_dir_regex, PCRE_COPT,
+        (const char **)&error, &offset, NULL);
+      if (dir_regex == NULL)
+        {
+        addr->message = string_sprintf("appendfile: regular expression "
+          "error: %s at offset %d while compiling %s", error, offset,
+          ob->maildir_dir_regex);
+        return FALSE;
+        }
+
+      DEBUG(D_transport)
+        debug_printf("using regex for maildir directory selection: %s\n",
+          ob->maildir_dir_regex);
+
+      /* Check to see if we are delivering into an ignored directory, that is,
+      if the delivery path starts with the quota check path, and the rest
+      of the deliver path matches the regex; if so, set a flag to disable quota
+      checking and maildirsize updating. */
+
+      if (Ustrncmp(path, check_path, check_path_len) == 0)
+        {
+        uschar *s = path + check_path_len;
+        while (*s == '/') s++;
+        s = (*s == 0)? US "new" : string_sprintf("%s/new", s);
+        if (pcre_exec(dir_regex, NULL, CS s, Ustrlen(s), 0, 0, NULL, 0) < 0)
+          {
+          disable_quota = TRUE;
+          DEBUG(D_transport) debug_printf("delivery directory does not match "
+            "maildir_quota_directory_regex: disabling quota\n");
+          }
+        }
+      }
+
+    /* Quota enforcement; create and check the file. There is some discussion
+    about whether this should happen if the quota is unset. At present, Exim
+    always creates the file. If we ever want to change this, uncomment
+    appropriate lines below, possibly doing a check on some option. */
+
+/*  if (???? || ob->quota_value > 0) */
+
+    if (!disable_quota)
+      {
+      off_t size;
+      int filecount;
+
+      maildirsize_fd = maildir_ensure_sizefile(check_path, ob, regex, dir_regex,
+        &size, &filecount);
+
+      if (maildirsize_fd == -1)
+        {
+        addr->basic_errno = errno;
+        addr->message = string_sprintf("while opening or reading "
+          "%s/maildirsize", check_path);
+        return FALSE;
+        }
+      /* can also return -2, which means that the file was removed because of
+      raciness; but in this case, the size & filecount will still have been
+      updated. */
+
+      if (mailbox_size < 0) mailbox_size = size;
+      if (mailbox_filecount < 0) mailbox_filecount = filecount;
+      }
+
+    /* No quota enforcement; ensure file does *not* exist; calculate size if
+    needed. */
+
+/*  else
+ *    {
+ *    time_t old_latest;
+ *    (void)unlink(CS string_sprintf("%s/maildirsize", check_path));
+ *    if (THRESHOLD_CHECK)
+ *      mailbox_size = maildir_compute_size(check_path, &mailbox_filecount, &old_latest,
+ *        regex, dir_regex, FALSE);
+ *    }
+*/
+
+    }
+  #endif  /* SUPPORT_MAILDIR */
+
+  /* Otherwise if we are going to do a quota check later on, and the mailbox
+  size is not set, find the current size of the mailbox. Ditto for the file
+  count. Note that ob->quota_filecount_value cannot be set without
+  ob->quota_value being set. */
+
+  if (!disable_quota &&
+      (ob->quota_value > 0 || THRESHOLD_CHECK) &&
+      (mailbox_size < 0 ||
+        (mailbox_filecount < 0 && ob->quota_filecount_value > 0)))
+    {
+    off_t size;
+    int filecount = 0;
+    DEBUG(D_transport)
+      debug_printf("quota checks on directory %s\n", check_path);
+    size = check_dir_size(check_path, &filecount, regex);
+    if (mailbox_size < 0) mailbox_size = size;
+    if (mailbox_filecount < 0) mailbox_filecount = filecount;
+    }
+
+  /* Handle the case of creating a unique file in a given directory (not in
+  maildir or mailstore format - this is how smail did it). A temporary name is
+  used to create the file. Later, when it is written, the name is changed to a
+  unique one. There is no need to lock the file. An attempt is made to create
+  the directory if it does not exist. */
+
+  if (mbformat == mbf_smail)
+    {
+    DEBUG(D_transport)
+      debug_printf("delivering to new file in %s\n", path);
+    filename = dataname =
+      string_sprintf("%s/temp.%d.%s", path, (int)getpid(), primary_hostname);
+    fd = Uopen(filename, O_WRONLY|O_CREAT, mode);
+    if (fd < 0 &&                                 /* failed to open, and */
+        (errno != ENOENT ||                       /* either not non-exist */
+         !ob->create_directory ||                 /* or not allowed to make */
+         !directory_make(NULL, path, ob->dirmode, FALSE) ||  /* or failed to create dir */
+         (fd = Uopen(filename, O_WRONLY|O_CREAT|O_EXCL, mode)) < 0)) /* or then failed to open */
+      {
+      addr->basic_errno = errno;
+      addr->message = string_sprintf("while creating file %s", filename);
+      return FALSE;
+      }
+    }
+
+  #ifdef SUPPORT_MAILDIR
+
+  /* Handle the case of a unique file in maildir format. The file is written to
+  the tmp subdirectory, with a prescribed form of name. */
+
+  else if (mbformat == mbf_maildir)
+    {
+    DEBUG(D_transport)
+      debug_printf("delivering in maildir format in %s\n", path);
+
+    nametag = ob->maildir_tag;
+
+    /* Check that nametag expands successfully; a hard failure causes a panic
+    return. The actual expansion for use happens again later, when
+    $message_size is accurately known. */
+
+    if (nametag != NULL && expand_string(nametag) == NULL &&
+        !expand_string_forcedfail)
+      {
+      addr->transport_return = PANIC;
+      addr->message = string_sprintf("Expansion of \"%s\" (maildir_tag "
+        "for %s transport) failed: %s", nametag, tblock->name,
+        expand_string_message);
+      return FALSE;
+      }
+
+    /* We ensured the existence of all the relevant directories above. Attempt
+    to open the temporary file a limited number of times. I think this rather
+    scary-looking for statement is actually OK. If open succeeds, the loop is
+    broken; if not, there is a test on the value of i. Get the time again
+    afresh each time round the loop. Its value goes into a variable that is
+    checked at the end, to make sure we don't release this process until the
+    clock has ticked. */
+
+    for (i = 1;; i++)
+      {
+      uschar *basename;
+
+      (void)gettimeofday(&msg_tv, NULL);
+      basename = string_sprintf("%lu.H%luP%lu.%s", msg_tv.tv_sec,
+        msg_tv.tv_usec, getpid(), primary_hostname);
+
+      filename = dataname = string_sprintf("tmp/%s", basename);
+      newname = string_sprintf("new/%s", basename);
+
+      if (Ustat(filename, &statbuf) == 0)
+        errno = EEXIST;
+      else if (errno == ENOENT)
+        {
+        fd = Uopen(filename, O_WRONLY | O_CREAT | O_EXCL, mode);
+        if (fd >= 0) break;
+        DEBUG (D_transport) debug_printf ("open failed for %s: %s\n",
+          filename, strerror(errno));
+        }
+
+      /* Too many retries - give up */
+
+      if (i >= ob->maildir_retries)
+        {
+        addr->message = string_sprintf ("failed to open %s (%d tr%s)",
+          filename, i, (i == 1)? "y" : "ies");
+        addr->basic_errno = errno;
+        if (errno == errno_quota || errno == ENOSPC)
+          addr->user_message = US"mailbox is full";
+        return FALSE;
+        }
+
+      /* Open or stat failed but we haven't tried too many times yet. */
+
+      sleep(2);
+      }
+
+    /* Note that we have to ensure the clock has ticked before leaving */
+
+    wait_for_tick = TRUE;
+
+    /* Why are these here? Put in because they are present in the non-maildir
+    directory case above. */
+
+    if(Uchown(filename, uid, gid) || Uchmod(filename, mode))
+      {
+      addr->basic_errno = errno;
+      addr->message = string_sprintf("while setting perms on maildir %s",
+        filename);
+      return FALSE;
+      }
+    }
+
+  #endif  /* SUPPORT_MAILDIR */
+
+  #ifdef SUPPORT_MAILSTORE
+
+  /* Handle the case of a unique file in mailstore format. First write the
+  envelope to a temporary file, then open the main file. The unique base name
+  for the files consists of the message id plus the pid of this delivery
+  process. */
+
+  else
+    {
+    FILE *env_file;
+    address_item *taddr;
+    mailstore_basename = string_sprintf("%s/%s-%s", path, message_id,
+      string_base62((long int)getpid()));
+
+    DEBUG(D_transport)
+      debug_printf("delivering in mailstore format in %s\n", path);
+
+    filename = string_sprintf("%s.tmp", mailstore_basename);
+    newname  = string_sprintf("%s.env", mailstore_basename);
+    dataname = string_sprintf("%s.msg", mailstore_basename);
+
+    fd = Uopen(filename, O_WRONLY|O_CREAT|O_EXCL, mode);
+    if (fd < 0 &&                                 /* failed to open, and */
+        (errno != ENOENT ||                       /* either not non-exist */
+         !ob->create_directory ||                 /* or not allowed to make */
+         !directory_make(NULL, path, ob->dirmode, FALSE) ||  /* or failed to create dir */
+         (fd = Uopen(filename, O_WRONLY|O_CREAT|O_EXCL, mode)) < 0)) /* or then failed to open */
+      {
+      addr->basic_errno = errno;
+      addr->message = string_sprintf("while creating file %s", filename);
+      return FALSE;
+      }
+
+    /* Why are these here? Put in because they are present in the non-maildir
+    directory case above. */
+
+    if(Uchown(filename, uid, gid) || Uchmod(filename, mode))
+      {
+      addr->basic_errno = errno;
+      addr->message = string_sprintf("while setting perms on file %s",
+        filename);
+      return FALSE;
+      }
+
+    /* Built a C stream from the open file descriptor. */
+
+    if ((env_file = fdopen(fd, "wb")) == NULL)
+      {
+      addr->basic_errno = errno;
+      addr->transport_return = PANIC;
+      addr->message = string_sprintf("fdopen of %s ("
+        "for %s transport) failed", filename, tblock->name);
+      (void)close(fd);
+      Uunlink(filename);
+      return FALSE;
+      }
+
+    /* Write the envelope file, then close it. */
+
+    if (ob->mailstore_prefix != NULL)
+      {
+      uschar *s = expand_string(ob->mailstore_prefix);
+      if (s == NULL)
+        {
+        if (!expand_string_forcedfail)
+          {
+          addr->transport_return = PANIC;
+          addr->message = string_sprintf("Expansion of \"%s\" (mailstore "
+            "prefix for %s transport) failed: %s", ob->mailstore_prefix,
+            tblock->name, expand_string_message);
+          (void)fclose(env_file);
+          Uunlink(filename);
+          return FALSE;
+          }
+        }
+      else
+        {
+        int n = Ustrlen(s);
+        fprintf(env_file, "%s", CS s);
+        if (n == 0 || s[n-1] != '\n') fprintf(env_file, "\n");
+        }
+      }
+
+    fprintf(env_file, "%s\n", sender_address);
+
+    for (taddr = addr; taddr!= NULL; taddr = taddr->next)
+      fprintf(env_file, "%s@%s\n", taddr->local_part, taddr->domain);
+
+    if (ob->mailstore_suffix != NULL)
+      {
+      uschar *s = expand_string(ob->mailstore_suffix);
+      if (s == NULL)
+        {
+        if (!expand_string_forcedfail)
+          {
+          addr->transport_return = PANIC;
+          addr->message = string_sprintf("Expansion of \"%s\" (mailstore "
+            "suffix for %s transport) failed: %s", ob->mailstore_suffix,
+            tblock->name, expand_string_message);
+          (void)fclose(env_file);
+          Uunlink(filename);
+          return FALSE;
+          }
+        }
+      else
+        {
+        int n = Ustrlen(s);
+        fprintf(env_file, "%s", CS s);
+        if (n == 0 || s[n-1] != '\n') fprintf(env_file, "\n");
+        }
+      }
+
+    if (fclose(env_file) != 0)
+      {
+      addr->basic_errno = errno;
+      addr->message = string_sprintf("while closing %s", filename);
+      Uunlink(filename);
+      return FALSE;
+      }
+
+    DEBUG(D_transport) debug_printf("Envelope file %s written\n", filename);
+
+    /* Now open the data file, and ensure that it has the correct ownership and
+    mode. */
+
+    fd = Uopen(dataname, O_WRONLY|O_CREAT|O_EXCL, mode);
+    if (fd < 0)
+      {
+      addr->basic_errno = errno;
+      addr->message = string_sprintf("while creating file %s", dataname);
+      Uunlink(filename);
+      return FALSE;
+      }
+    if(Uchown(dataname, uid, gid) || Uchmod(dataname, mode))
+      {
+      addr->basic_errno = errno;
+      addr->message = string_sprintf("while setting perms on file %s",
+        dataname);
+      return FALSE;
+      }
+    }
+
+  #endif  /* SUPPORT_MAILSTORE */
+
+
+  /* In all cases of writing to a new file, ensure that the file which is
+  going to be renamed has the correct ownership and mode. */
+
+  if(Uchown(filename, uid, gid) || Uchmod(filename, mode))
+    {
+    addr->basic_errno = errno;
+    addr->message = string_sprintf("while setting perms on file %s",
+      filename);
+    return FALSE;
+    }
+  }
+
+
+/* At last we can write the message to the file, preceded by any configured
+prefix line, and followed by any configured suffix line. If there are any
+writing errors, we must defer. */
+
+DEBUG(D_transport) debug_printf("writing to file %s\n", dataname);
+
+yield = OK;
+errno = 0;
+
+/* If there is a local quota setting, check that we are not going to exceed it
+with this message if quota_is_inclusive is set; if it is not set, the check
+is for the mailbox already being over quota (i.e. the current message is not
+included in the check). */
+
+if (!disable_quota && ob->quota_value > 0)
+  {
+  DEBUG(D_transport)
+    {
+    debug_printf("Exim quota = " OFF_T_FMT " old size = " OFF_T_FMT
+      " this message = %d (%sincluded)\n",
+      ob->quota_value, mailbox_size, message_size,
+      ob->quota_is_inclusive? "" : "not ");
+    debug_printf("  file count quota = %d count = %d\n",
+      ob->quota_filecount_value, mailbox_filecount);
+    }
+  if (mailbox_size + (ob->quota_is_inclusive? message_size:0) > ob->quota_value)
+    {
+    DEBUG(D_transport) debug_printf("mailbox quota exceeded\n");
+    yield = DEFER;
+    errno = ERRNO_EXIMQUOTA;
+    }
+  else if (ob->quota_filecount_value > 0 &&
+           mailbox_filecount + (ob->quota_is_inclusive ? 1:0) >
+             ob->quota_filecount_value)
+    {
+    DEBUG(D_transport) debug_printf("mailbox file count quota exceeded\n");
+    yield = DEFER;
+    errno = ERRNO_EXIMQUOTA;
+    filecount_msg = US" filecount";
+    }
+  }
+
+/* If we are writing in MBX format, what we actually do is to write the message
+to a temporary file, and then copy it to the real file once we know its size.
+This is the most straightforward way of getting the correct length in the
+separator line. So, what we do here is to save the real file descriptor, and
+replace it with one for a temporary file. The temporary file gets unlinked once
+opened, so that it goes away on closure. */
+
+#ifdef SUPPORT_MBX
+if (yield == OK && ob->mbx_format)
+  {
+  temp_file = tmpfile();
+  if (temp_file == NULL)
+    {
+    addr->basic_errno = errno;
+    addr->message = US"while setting up temporary file";
+    yield = DEFER;
+    goto RETURN;
+    }
+  save_fd = fd;
+  fd = fileno(temp_file);
+  DEBUG(D_transport) debug_printf("writing to temporary file\n");
+  }
+#endif  /* SUPPORT_MBX */
+
+/* Zero the count of bytes written. It is incremented by the transport_xxx()
+functions. */
+
+transport_count = 0;
+transport_newlines = 0;
+
+/* Write any configured prefix text first */
+
+if (yield == OK && ob->message_prefix != NULL && ob->message_prefix[0] != 0)
+  {
+  uschar *prefix = expand_string(ob->message_prefix);
+  if (prefix == NULL)
+    {
+    errno = ERRNO_EXPANDFAIL;
+    addr->transport_return = PANIC;
+    addr->message = string_sprintf("Expansion of \"%s\" (prefix for %s "
+      "transport) failed", ob->message_prefix, tblock->name);
+    yield = DEFER;
+    }
+  else if (!transport_write_string(fd, "%s", prefix)) yield = DEFER;
+  }
+
+/* If the use_bsmtp option is on, we need to write SMTP prefix information. The
+various different values for batching are handled outside; if there is more
+than one address available here, all must be included. If any address is a
+file, use its parent in the RCPT TO. */
+
+if (yield == OK && ob->use_bsmtp)
+  {
+  transport_count = 0;
+  transport_newlines = 0;
+  if (ob->use_crlf) cr = US"\r";
+  if (!transport_write_string(fd, "MAIL FROM:<%s>%s\n", return_path, cr))
+    yield = DEFER;
+  else
+    {
+    address_item *a;
+    transport_newlines++;
+    for (a = addr; a != NULL; a = a->next)
+      {
+      address_item *b = testflag(a, af_pfr)? a->parent: a;
+      if (!transport_write_string(fd, "RCPT TO:<%s>%s\n",
+        transport_rcpt_address(b, tblock->rcpt_include_affixes), cr))
+          { yield = DEFER; break; }
+      transport_newlines++;
+      }
+    if (yield == OK && !transport_write_string(fd, "DATA%s\n", cr))
+      yield = DEFER;
+    else
+      transport_newlines++;
+    }
+  }
+
+/* Now the message itself. The options for transport_write_message were set up
+at initialization time. */
+
+if (yield == OK)
+  {
+  if (!transport_write_message(addr, fd, ob->options, 0, tblock->add_headers,
+      tblock->remove_headers, ob->check_string, ob->escape_string,
+      tblock->rewrite_rules, tblock->rewrite_existflags))
+    yield = DEFER;
+  }
+
+/* Now a configured suffix. */
+
+if (yield == OK && ob->message_suffix != NULL && ob->message_suffix[0] != 0)
+  {
+  uschar *suffix = expand_string(ob->message_suffix);
+  if (suffix == NULL)
+    {
+    errno = ERRNO_EXPANDFAIL;
+    addr->transport_return = PANIC;
+    addr->message = string_sprintf("Expansion of \"%s\" (suffix for %s "
+      "transport) failed", ob->message_suffix, tblock->name);
+    yield = DEFER;
+    }
+  else if (!transport_write_string(fd, "%s", suffix)) yield = DEFER;
+  }
+
+/* If batch smtp, write the terminating dot. */
+
+if (yield == OK && ob->use_bsmtp ) {
+  if(!transport_write_string(fd, ".%s\n", cr)) yield = DEFER;
+  else transport_newlines++;
+}
+
+/* If MBX format is being used, all that writing was to the temporary file.
+However, if there was an earlier failure (Exim quota exceeded, for example),
+the temporary file won't have got opened - and no writing will have been done.
+If writing was OK, we restore the fd, and call a function that copies the
+message in MBX format into the real file. Otherwise use the temporary name in
+any messages. */
+
+#ifdef SUPPORT_MBX
+if (temp_file != NULL && ob->mbx_format)
+  {
+  int mbx_save_errno;
+  fd = save_fd;
+
+  if (yield == OK)
+    {
+    transport_count = 0;   /* Reset transport count for actual write */
+    /* No need to reset transport_newlines as we're just using a block copy
+     * routine so the number won't be affected */
+    yield = copy_mbx_message(fd, fileno(temp_file), saved_size);
+    }
+  else if (errno >= 0) dataname = US"temporary file";
+
+  /* Preserve errno while closing the temporary file. */
+
+  mbx_save_errno = errno;
+  (void)fclose(temp_file);
+  errno = mbx_save_errno;
+  }
+#endif  /* SUPPORT_MBX */
+
+/* Force out the remaining data to check for any errors; some OS don't allow
+fsync() to be called for a FIFO. */
+
+if (yield == OK && !isfifo && EXIMfsync(fd) < 0) yield = DEFER;
+
+/* Update message_size and message_linecount to the accurate count of bytes
+written, including added headers. Note; we subtract 1 from message_linecount as
+this variable doesn't count the new line between the header and the body of the
+message. */
+
+message_size = transport_count;
+message_linecount = transport_newlines - 1;
+
+/* If using a maildir++ quota file, add this message's size to it, and
+close the file descriptor, except when the quota has been disabled because we
+are delivering into an uncounted folder. */
+
+#ifdef SUPPORT_MAILDIR
+if (!disable_quota)
+  {
+  if (yield == OK && maildirsize_fd >= 0)
+    maildir_record_length(maildirsize_fd, message_size);
+  maildir_save_errno = errno;    /* Preserve errno while closing the file */
+  if (maildirsize_fd >= 0)
+    (void)close(maildirsize_fd);
+  errno = maildir_save_errno;
+  }
+#endif  /* SUPPORT_MAILDIR */
+
+/* If there is a quota warning threshold and we are have crossed it with this
+message, set the SPECIAL_WARN flag in the address, to cause a warning message
+to be sent. */
+
+if (!disable_quota && THRESHOLD_CHECK)
+  {
+  off_t threshold = ob->quota_warn_threshold_value;
+  if (ob->quota_warn_threshold_is_percent)
+    threshold = (off_t)(((double)ob->quota_value * threshold) / 100);
+  DEBUG(D_transport)
+    debug_printf("quota = " OFF_T_FMT
+      " threshold = " OFF_T_FMT
+      " old size = " OFF_T_FMT
+      " message size = %d\n",
+      ob->quota_value, threshold, mailbox_size,
+      message_size);
+  if (mailbox_size <= threshold && mailbox_size + message_size > threshold)
+    addr->special_action = SPECIAL_WARN;
+
+  /******* You might think that the test ought to be this:
+  *
+  * if (ob->quota_value > 0 && threshold > 0 && mailbox_size > 0 &&
+  *     mailbox_size <= threshold && mailbox_size + message_size > threshold)
+  *
+  * (indeed, I was sent a patch with that in). However, it is possible to
+  * have a warning threshold without actually imposing a quota, and I have
+  * therefore kept Exim backwards compatible.
+  ********/
+
+  }
+
+/* Handle error while writing the file. Control should come here directly after
+the error, with the reason in errno. In the case of expansion failure in prefix
+or suffix, it will be ERRNO_EXPANDFAIL. */
+
+if (yield != OK)
+  {
+  addr->special_action = SPECIAL_NONE;     /* Cancel any quota warning */
+
+  /* Save the error number. If positive, it will ultimately cause a strerror()
+  call to generate some text. */
+
+  addr->basic_errno = errno;
+
+  /* For system or Exim quota excession, or disk full, set more_errno to the
+  time since the file was last read. If delivery was into a directory, the
+  time since last read logic is not relevant, in general. However, for maildir
+  deliveries we can approximate it by looking at the last modified time of the
+  "new" subdirectory. Since Exim won't be adding new messages, a change to the
+  "new" subdirectory implies that an MUA has moved a message from there to the
+  "cur" directory. */
+
+  if (errno == errno_quota || errno == ERRNO_EXIMQUOTA || errno == ENOSPC)
+    {
+    addr->more_errno = 0;
+    if (!isdirectory) addr->more_errno = (int)(time(NULL) - times.actime);
+
+    #ifdef SUPPORT_MAILDIR
+    else if (mbformat == mbf_maildir)
+      {
+      struct stat statbuf;
+      if (Ustat("new", &statbuf) < 0)
+        {
+        DEBUG(D_transport) debug_printf("maildir quota exceeded: "
+          "stat error %d for \"new\": %s\n", errno, strerror(errno));
+        }
+      else   /* Want a repeatable time when in test harness */
+        {
+        addr->more_errno = running_in_test_harness? 10 :
+          (int)time(NULL) - statbuf.st_mtime;
+        }
+      DEBUG(D_transport)
+        debug_printf("maildir: time since \"new\" directory modified = %s\n",
+        readconf_printtime(addr->more_errno));
+      }
+    #endif /* SUPPORT_MAILDIR */
+    }
+
+  /* Handle system quota excession. Add an explanatory phrase for the error
+  message, since some systems don't have special quota-excession errors,
+  and on those that do, "quota" doesn't always mean anything to the user. */
+
+  if (errno == errno_quota)
+    {
+    #ifndef EDQUOT
+    addr->message = string_sprintf("mailbox is full "
+      "(quota exceeded while writing to file %s)", filename);
+    #else
+    addr->message = string_sprintf("mailbox is full");
+    #endif  /* EDQUOT */
+    addr->user_message = US"mailbox is full";
+    DEBUG(D_transport) debug_printf("System quota exceeded for %s%s%s\n",
+      dataname,
+      isdirectory? US"" : US": time since file read = ",
+      isdirectory? US"" : readconf_printtime(addr->more_errno));
+    }
+
+  /* Handle Exim's own quota-imposition */
+
+  else if (errno == ERRNO_EXIMQUOTA)
+    {
+    addr->message = string_sprintf("mailbox is full "
+      "(MTA-imposed%s quota exceeded while writing to %s)", filecount_msg,
+        dataname);
+    addr->user_message = US"mailbox is full";
+    DEBUG(D_transport) debug_printf("Exim%s quota exceeded for %s%s%s\n",
+      filecount_msg, dataname,
+      isdirectory? US"" : US": time since file read = ",
+      isdirectory? US"" : readconf_printtime(addr->more_errno));
+    }
+
+  /* Handle a process failure while writing via a filter; the return
+  from child_close() is in more_errno. */
+
+  else if (errno == ERRNO_FILTER_FAIL)
+    {
+    yield = PANIC;
+    addr->message = string_sprintf("transport filter process failed (%d) "
+      "while writing to %s%s", addr->more_errno, dataname,
+      (addr->more_errno == EX_EXECFAILED)? ": unable to execute command" : "");
+    }
+
+  /* Handle failure to expand header changes */
+
+  else if (errno == ERRNO_CHHEADER_FAIL)
+    {
+    yield = PANIC;
+    addr->message =
+      string_sprintf("failed to expand headers_add or headers_remove while "
+        "writing to %s: %s", dataname, expand_string_message);
+    }
+
+  /* Handle failure to complete writing of a data block */
+
+  else if (errno == ERRNO_WRITEINCOMPLETE)
+    {
+    addr->message = string_sprintf("failed to write data block while "
+      "writing to %s", dataname);
+    }
+
+  /* Handle length mismatch on MBX copying */
+
+  #ifdef SUPPORT_MBX
+  else if (errno == ERRNO_MBXLENGTH)
+    {
+    addr->message = string_sprintf("length mismatch while copying MBX "
+      "temporary file to %s", dataname);
+    }
+  #endif  /* SUPPORT_MBX */
+
+  /* For other errors, a general-purpose explanation, if the message is
+  not already set. */
+
+  else if (addr->message == NULL)
+    addr->message = string_sprintf("error while writing to %s", dataname);
+
+  /* For a file, reset the file size to what it was before we started, leaving
+  the last modification time unchanged, so it will get reset also. All systems
+  investigated so far have ftruncate(), whereas not all have the F_FREESP
+  fcntl() call (BSDI & FreeBSD do not). */
+
+  if (!isdirectory && ftruncate(fd, saved_size))
+    DEBUG(D_transport) debug_printf("Error restting file size\n");
+  }
+
+/* Handle successful writing - we want the modification time to be now for
+appended files. Remove the default backstop error number. For a directory, now
+is the time to rename the file with a unique name. As soon as such a name
+appears it may get used by another process, so we close the file first and
+check that all is well. */
+
+else
+  {
+  times.modtime = time(NULL);
+  addr->basic_errno = 0;
+
+  /* Handle the case of writing to a new file in a directory. This applies
+  to all single-file formats - maildir, mailstore, and "smail format". */
+
+  if (isdirectory)
+    {
+    if (fstat(fd, &statbuf) < 0)
+      {
+      addr->basic_errno = errno;
+      addr->message = string_sprintf("while fstatting opened message file %s",
+        filename);
+      yield = DEFER;
+      }
+
+    else if (close(fd) < 0)
+      {
+      addr->basic_errno = errno;
+      addr->message = string_sprintf("close() error for %s",
+        (ob->mailstore_format)? dataname : filename);
+      yield = DEFER;
+      }
+
+    /* File is successfully written and closed. Arrange to rename it. For the
+    different kinds of single-file delivery, some games can be played with the
+    name. The message size is by this time set to the accurate value so that
+    its value can be used in expansions. */
+
+    else
+      {
+      uschar *renamename = newname;
+      fd = -1;
+
+      DEBUG(D_transport) debug_printf("renaming temporary file\n");
+
+      /* If there is no rename name set, we are in a non-maildir, non-mailstore
+      situation. The name is built by expanding the directory_file option, and
+      we make the inode number available for use in this. The expansion was
+      checked for syntactic validity above, before we wrote the file.
+
+      We have to be careful here, in case the file name exists. (In the other
+      cases, the names used are constructed to be unique.) The rename()
+      function just replaces an existing file - we don't want that! So instead
+      of calling rename(), we must use link() and unlink().
+
+      In this case, if the link fails because of an existing file, we wait
+      for one second and try the expansion again, to see if it produces a
+      different value. Do this up to 5 times unless the name stops changing.
+      This makes it possible to build values that are based on the time, and
+      still cope with races from multiple simultaneous deliveries. */
+
+      if (newname == NULL)
+        {
+        int i;
+        uschar *renameleaf;
+        uschar *old_renameleaf = US"";
+
+        for (i = 0; ; sleep(1), i++)
+          {
+          deliver_inode = statbuf.st_ino;
+          renameleaf = expand_string(ob->dirfilename);
+          deliver_inode = 0;
+
+          if (renameleaf == NULL)
+            {
+            addr->transport_return = PANIC;
+            addr->message = string_sprintf("Expansion of \"%s\" "
+              "(directory_file for %s transport) failed: %s",
+              ob->dirfilename, tblock->name, expand_string_message);
+            goto RETURN;
+            }
+
+          renamename = string_sprintf("%s/%s", path, renameleaf);
+          if (Ulink(filename, renamename) < 0)
+            {
+            DEBUG(D_transport) debug_printf("link failed: %s\n",
+              strerror(errno));
+            if (errno != EEXIST || i >= 4 ||
+                Ustrcmp(renameleaf, old_renameleaf) == 0)
+              {
+              addr->basic_errno = errno;
+              addr->message = string_sprintf("while renaming %s as %s",
+                filename, renamename);
+              yield = DEFER;
+              break;
+              }
+            old_renameleaf = renameleaf;
+            DEBUG(D_transport) debug_printf("%s exists - trying again\n",
+              renamename);
+            }
+          else
+            {
+            Uunlink(filename);
+            filename = NULL;
+            break;
+            }
+          }        /* re-expand loop */
+        }          /* not mailstore or maildir */
+
+      /* For maildir and mailstore formats, the new name was created earlier,
+      except that for maildir, there is the possibility of adding a "tag" on
+      the end of the name by expanding the value of nametag. This usually
+      includes a reference to the message size. The expansion of nametag was
+      checked above, before the file was opened. It either succeeded, or
+      provoked a soft failure. So any failure here can be treated as soft.
+      Ignore non-printing characters and / and put a colon at the start if the
+      first character is alphanumeric. */
+
+      else
+        {
+        if (nametag != NULL)
+          {
+          uschar *iptr = expand_string(nametag);
+          if (iptr != NULL)
+            {
+            uschar *etag = store_get(Ustrlen(iptr) + 2);
+            uschar *optr = etag;
+            while (*iptr != 0)
+              {
+              if (mac_isgraph(*iptr) && *iptr != '/')
+                {
+                if (optr == etag && isalnum(*iptr)) *optr++ = ':';
+                *optr++ = *iptr;
+                }
+              iptr++;
+              }
+            *optr = 0;
+            renamename = string_sprintf("%s%s", newname, etag);
+            }
+          }
+
+        /* Do the rename. If the name is too long and a tag exists, try again
+        without the tag. */
+
+        if (Urename(filename, renamename) < 0 &&
+               (nametag == NULL || errno != ENAMETOOLONG ||
+               (renamename = newname, Urename(filename, renamename) < 0)))
+          {
+          addr->basic_errno = errno;
+          addr->message = string_sprintf("while renaming %s as %s",
+            filename, renamename);
+          yield = DEFER;
+          }
+
+        /* Rename succeeded */
+
+        else
+          {
+          DEBUG(D_transport) debug_printf("renamed %s as %s\n", filename,
+            renamename);
+          filename = dataname = NULL;   /* Prevents attempt to unlink at end */
+          }
+        }        /* maildir or mailstore */
+      }          /* successful write + close */
+    }            /* isdirectory */
+  }              /* write success */
+
+
+/* For a file, restore the last access time (atime), and set the modification
+time as required - changed if write succeeded, unchanged if not. */
+
+if (!isdirectory) utime(CS filename, &times);
+
+/* Notify comsat if configured to do so. It only makes sense if the configured
+file is the one that the comsat daemon knows about. */
+
+if (ob->notify_comsat && yield == OK && deliver_localpart != NULL)
+  notify_comsat(deliver_localpart, saved_size);
+
+/* Pass back the final return code in the address structure */
+
+DEBUG(D_transport)
+  debug_printf("appendfile yields %d with errno=%d more_errno=%d\n",
+    yield, addr->basic_errno, addr->more_errno);
+
+addr->transport_return = yield;
+
+/* Close the file, which will release the fcntl lock. For a directory write it
+is closed above, except in cases of error which goto RETURN, when we also need
+to remove the original file(s). For MBX locking, if all has gone well, before
+closing the file, see if we can get an exclusive lock on it, in which case we
+can unlink the /tmp lock file before closing it. This is always a non-blocking
+lock; there's no need to wait if we can't get it. If everything has gone right
+but close fails, defer the message. Then unlink the lock file, if present. This
+point in the code is jumped to from a number of places when errors are
+detected, in order to get the file closed and the lock file tidied away. */
+
+RETURN:
+
+#ifdef SUPPORT_MBX
+if (mbx_lockfd >= 0)
+  {
+  if (yield == OK && apply_lock(fd, F_WRLCK, ob->use_fcntl, 0,
+      ob->use_flock, 0) >= 0)
+    {
+    DEBUG(D_transport)
+      debug_printf("unlinking MBX lock file %s\n", mbx_lockname);
+    Uunlink(mbx_lockname);
+    }
+  (void)close(mbx_lockfd);
+  }
+#endif  /* SUPPORT_MBX */
+
+if (fd >= 0 && close(fd) < 0 && yield == OK)
+  {
+  addr->basic_errno = errno;
+  addr->message = string_sprintf("while closing %s", filename);
+  addr->transport_return = DEFER;
+  }
+
+if (hd >= 0) Uunlink(lockname);
+
+/* We get here with isdirectory and filename set only in error situations. */
+
+if (isdirectory && filename != NULL)
+  {
+  Uunlink(filename);
+  if (dataname != filename) Uunlink(dataname);
+  }
+
+/* If wait_for_tick is TRUE, we have done a delivery where the uniqueness of a
+file name relies on time + pid. We must not allow the process to finish until
+the clock has move on by at least one microsecond. Usually we expect this
+already to be the case, but machines keep getting faster... */
+
+if (wait_for_tick) exim_wait_tick(&msg_tv, 1);
+
+/* A return of FALSE means that if there was an error, a common error was
+put in the first address of a batch. */
+
+return FALSE;
+}
+
+/* End of transport/appendfile.c */
diff --git a/src/transports/appendfile.h b/src/transports/appendfile.h
new file mode 100644 (file)
index 0000000..52dc3ba
--- /dev/null
@@ -0,0 +1,97 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Private structure for the private options. */
+
+typedef struct {
+  uschar *filename;
+  uschar *dirname;
+  uschar *dirfilename;
+  uschar *message_prefix;
+  uschar *message_suffix;
+  uschar *create_file_string;
+  uschar *quota;
+  uschar *quota_directory;
+  uschar *quota_filecount;
+  uschar *quota_size_regex;
+  uschar *quota_warn_threshold;
+  uschar *mailbox_size_string;
+  uschar *mailbox_filecount_string;
+  uschar *expand_maildir_use_size_file;
+  uschar *maildir_dir_regex;
+  uschar *maildir_tag;
+  uschar *maildirfolder_create_regex;
+  uschar *mailstore_prefix;
+  uschar *mailstore_suffix;
+  uschar *check_string;
+  uschar *escape_string;
+  uschar *file_format;
+  off_t quota_value;
+  off_t quota_warn_threshold_value;
+  off_t mailbox_size_value;
+  int   mailbox_filecount_value;
+  int   quota_filecount_value;
+  int   mode;
+  int   dirmode;
+  int   lockfile_mode;
+  int   lockfile_timeout;
+  int   lock_fcntl_timeout;
+  int   lock_flock_timeout;
+  int   lock_retries;
+  int   lock_interval;
+  int   maildir_retries;
+  int   create_file;
+  int   options;
+  BOOL  allow_fifo;
+  BOOL  allow_symlink;
+  BOOL  check_group;
+  BOOL  check_owner;
+  BOOL  create_directory;
+  BOOL  notify_comsat;
+  BOOL  use_lockfile;
+  BOOL  set_use_lockfile;
+  BOOL  use_fcntl;
+  BOOL  set_use_fcntl;
+  BOOL  use_flock;
+  BOOL  set_use_flock;
+  BOOL  use_mbx_lock;
+  BOOL  set_use_mbx_lock;
+  BOOL  use_bsmtp;
+  BOOL  use_crlf;
+  BOOL  file_must_exist;
+  BOOL  mode_fail_narrower;
+  BOOL  maildir_format;
+  BOOL  maildir_use_size_file;
+  BOOL  mailstore_format;
+  BOOL  mbx_format;
+  BOOL  quota_warn_threshold_is_percent;
+  BOOL  quota_is_inclusive;
+} appendfile_transport_options_block;
+
+/* Restricted creation options */
+
+enum { create_anywhere, create_belowhome, create_inhome };
+
+/* Data for reading the private options. */
+
+extern optionlist appendfile_transport_options[];
+extern int appendfile_transport_options_count;
+
+/* Block containing default values. */
+
+extern appendfile_transport_options_block appendfile_transport_option_defaults;
+
+/* The main and init entry points for the transport */
+
+extern BOOL appendfile_transport_entry(transport_instance *, address_item *);
+extern void appendfile_transport_init(transport_instance *);
+
+/* Function that is shared with tf_maildir.c */
+
+extern off_t  check_dir_size(uschar *, int *, const pcre *);
+
+/* End of transports/appendfile.h */
diff --git a/src/transports/autoreply.c b/src/transports/autoreply.c
new file mode 100644 (file)
index 0000000..4e391b8
--- /dev/null
@@ -0,0 +1,874 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+
+#include "../exim.h"
+#include "autoreply.h"
+
+
+
+/* Options specific to the autoreply transport. They must be in alphabetic
+order (note that "_" comes before the lower case letters). Those starting
+with "*" are not settable by the user but are used by the option-reading
+software for alternative value types. Some options are publicly visible and so
+are stored in the driver instance block. These are flagged with opt_public. */
+
+optionlist autoreply_transport_options[] = {
+  { "bcc",               opt_stringptr,
+      (void *)offsetof(autoreply_transport_options_block, bcc) },
+  { "cc",                opt_stringptr,
+      (void *)offsetof(autoreply_transport_options_block, cc) },
+  { "file",              opt_stringptr,
+      (void *)offsetof(autoreply_transport_options_block, file) },
+  { "file_expand",     opt_bool,
+      (void *)offsetof(autoreply_transport_options_block, file_expand) },
+  { "file_optional",     opt_bool,
+      (void *)offsetof(autoreply_transport_options_block, file_optional) },
+  { "from",              opt_stringptr,
+      (void *)offsetof(autoreply_transport_options_block, from) },
+  { "headers",           opt_stringptr,
+      (void *)offsetof(autoreply_transport_options_block, headers) },
+  { "log",               opt_stringptr,
+      (void *)offsetof(autoreply_transport_options_block, logfile) },
+  { "mode",              opt_octint,
+      (void *)offsetof(autoreply_transport_options_block, mode) },
+  { "never_mail",        opt_stringptr,
+      (void *)offsetof(autoreply_transport_options_block, never_mail) },
+  { "once",              opt_stringptr,
+      (void *)offsetof(autoreply_transport_options_block, oncelog) },
+  { "once_file_size",    opt_int,
+      (void *)offsetof(autoreply_transport_options_block, once_file_size) },
+  { "once_repeat",       opt_stringptr,
+      (void *)offsetof(autoreply_transport_options_block, once_repeat) },
+  { "reply_to",          opt_stringptr,
+      (void *)offsetof(autoreply_transport_options_block, reply_to) },
+  { "return_message",    opt_bool,
+      (void *)offsetof(autoreply_transport_options_block, return_message) },
+  { "subject",           opt_stringptr,
+      (void *)offsetof(autoreply_transport_options_block, subject) },
+  { "text",              opt_stringptr,
+      (void *)offsetof(autoreply_transport_options_block, text) },
+  { "to",                opt_stringptr,
+      (void *)offsetof(autoreply_transport_options_block, to) },
+};
+
+/* Size of the options list. An extern variable has to be used so that its
+address can appear in the tables drtables.c. */
+
+int autoreply_transport_options_count =
+  sizeof(autoreply_transport_options)/sizeof(optionlist);
+
+/* Default private options block for the autoreply transport. */
+
+autoreply_transport_options_block autoreply_transport_option_defaults = {
+  NULL,           /* from */
+  NULL,           /* reply_to */
+  NULL,           /* to */
+  NULL,           /* cc */
+  NULL,           /* bcc */
+  NULL,           /* subject */
+  NULL,           /* headers */
+  NULL,           /* text */
+  NULL,           /* file */
+  NULL,           /* logfile */
+  NULL,           /* oncelog */
+  NULL,           /* once_repeat */
+  NULL,           /* never_mail */
+  0600,           /* mode */
+  0,              /* once_file_size */
+  FALSE,          /* file_expand */
+  FALSE,          /* file_optional */
+  FALSE           /* return message */
+};
+
+
+
+/* Type of text for the checkexpand() function */
+
+enum { cke_text, cke_hdr, cke_file };
+
+
+
+/*************************************************
+*          Initialization entry point            *
+*************************************************/
+
+/* Called for each instance, after its options have been read, to
+enable consistency checks to be done, or anything else that needs
+to be set up. */
+
+void
+autoreply_transport_init(transport_instance *tblock)
+{
+/*
+autoreply_transport_options_block *ob =
+  (autoreply_transport_options_block *)(tblock->options_block);
+*/
+
+/* If a fixed uid field is set, then a gid field must also be set. */
+
+if (tblock->uid_set && !tblock->gid_set && tblock->expand_gid == NULL)
+  log_write(0, LOG_PANIC_DIE|LOG_CONFIG,
+    "user set without group for the %s transport", tblock->name);
+}
+
+
+
+
+/*************************************************
+*          Expand string and check               *
+*************************************************/
+
+/* If the expansion fails, the error is set up in the address. Expanded
+strings must be checked to ensure they contain only printing characters
+and white space. If not, the function fails.
+
+Arguments:
+   s         string to expand
+   addr      address that is being worked on
+   name      transport name, for error text
+   type      type, for checking content:
+               cke_text => no check
+               cke_hdr  => header, allow \n + whitespace
+               cke_file => file name, no non-printers allowed
+
+Returns:     expanded string if expansion succeeds;
+             NULL otherwise
+*/
+
+static uschar *
+checkexpand(uschar *s, address_item *addr, uschar *name, int type)
+{
+uschar *t;
+uschar *ss = expand_string(s);
+
+if (ss == NULL)
+  {
+  addr->transport_return = FAIL;
+  addr->message = string_sprintf("Expansion of \"%s\" failed in %s transport: "
+    "%s", s, name, expand_string_message);
+  return NULL;
+  }
+
+if (type != cke_text) for (t = ss; *t != 0; t++)
+  {
+  int c = *t;
+  if (mac_isprint(c)) continue;
+  if (type == cke_hdr && c == '\n' && (t[1] == ' ' || t[1] == '\t')) continue;
+  s = string_printing(s);
+  addr->transport_return = FAIL;
+  addr->message = string_sprintf("Expansion of \"%s\" in %s transport "
+    "contains non-printing character %d", s, name, c);
+  return NULL;
+  }
+
+return ss;
+}
+
+
+
+
+/*************************************************
+*          Check a header line for never_mail    *
+*************************************************/
+
+/* This is called to check to, cc, and bcc for addresses in the never_mail
+list. Any that are found are removed.
+
+Arguments:
+  listptr     points to the list of addresses
+  never_mail  an address list, already expanded
+
+Returns:      nothing
+*/
+
+static void
+check_never_mail(uschar **listptr, uschar *never_mail)
+{
+uschar *s = *listptr;
+
+while (*s != 0)
+  {
+  uschar *error, *next;
+  uschar *e = parse_find_address_end(s, FALSE);
+  int terminator = *e;
+  int start, end, domain, rc;
+
+  /* Temporarily terminate the string at the address end while extracting
+  the operative address within. */
+
+  *e = 0;
+  next = parse_extract_address(s, &error, &start, &end, &domain, FALSE);
+  *e = terminator;
+
+  /* If there is some kind of syntax error, just give up on this header
+  line. */
+
+  if (next == NULL) break;
+
+  /* See if the address is on the never_mail list */
+
+  rc = match_address_list(next,         /* address to check */
+                          TRUE,         /* start caseless */
+                          FALSE,        /* don't expand the list */
+                          &never_mail,  /* the list */
+                          NULL,         /* no caching */
+                          -1,           /* no expand setup */
+                          0,            /* separator from list */
+                          NULL);        /* no lookup value return */
+
+  if (rc == OK)                         /* Remove this address */
+    {
+    DEBUG(D_transport)
+      debug_printf("discarding recipient %s (matched never_mail)\n", next);
+    if (terminator == ',') e++;
+    memmove(s, e, Ustrlen(e) + 1);
+    }
+  else                                  /* Skip over this address */
+    {
+    s = e;
+    if (terminator == ',') s++;
+    }
+  }
+
+/* Check to see if we removed the last address, leaving a terminating comma
+that needs to be removed */
+
+s = *listptr + Ustrlen(*listptr);
+while (s > *listptr && (isspace(s[-1]) || s[-1] == ',')) s--;
+*s = 0;
+
+/* Check to see if there any addresses left; if not, set NULL */
+
+s = *listptr;
+while (s != 0 && isspace(*s)) s++;
+if (*s == 0) *listptr = NULL;
+}
+
+
+
+/*************************************************
+*              Main entry point                  *
+*************************************************/
+
+/* See local README for interface details. This transport always returns
+FALSE, indicating that the top address has the status for all - though in fact
+this transport can handle only one address at at time anyway. */
+
+BOOL
+autoreply_transport_entry(
+  transport_instance *tblock,      /* data for this instantiation */
+  address_item *addr)              /* address we are working on */
+{
+int fd, pid, rc;
+int cache_fd = -1;
+int log_fd = -1;
+int cache_size = 0;
+int add_size = 0;
+EXIM_DB *dbm_file = NULL;
+BOOL file_expand, return_message;
+uschar *from, *reply_to, *to, *cc, *bcc, *subject, *headers, *text, *file;
+uschar *logfile, *oncelog;
+uschar *cache_buff = NULL;
+uschar *cache_time = NULL;
+uschar *message_id = NULL;
+header_line *h;
+time_t now = time(NULL);
+time_t once_repeat_sec = 0;
+FILE *f;
+FILE *ff = NULL;
+
+autoreply_transport_options_block *ob =
+  (autoreply_transport_options_block *)(tblock->options_block);
+
+DEBUG(D_transport) debug_printf("%s transport entered\n", tblock->name);
+
+/* Set up for the good case */
+
+addr->transport_return = OK;
+addr->basic_errno = 0;
+
+/* If the address is pointing to a reply block, then take all the data
+from that block. It has typically been set up by a mail filter processing
+router. Otherwise, the data must be supplied by this transport, and
+it has to be expanded here. */
+
+if (addr->reply != NULL)
+  {
+  DEBUG(D_transport) debug_printf("taking data from address\n");
+  from = addr->reply->from;
+  reply_to = addr->reply->reply_to;
+  to = addr->reply->to;
+  cc = addr->reply->cc;
+  bcc = addr->reply->bcc;
+  subject = addr->reply->subject;
+  headers = addr->reply->headers;
+  text = addr->reply->text;
+  file = addr->reply->file;
+  logfile = addr->reply->logfile;
+  oncelog = addr->reply->oncelog;
+  once_repeat_sec = addr->reply->once_repeat;
+  file_expand = addr->reply->file_expand;
+  expand_forbid = addr->reply->expand_forbid;
+  return_message = addr->reply->return_message;
+  }
+else
+  {
+  uschar *oncerepeat = ob->once_repeat;
+
+  DEBUG(D_transport) debug_printf("taking data from transport\n");
+  from = ob->from;
+  reply_to = ob->reply_to;
+  to = ob->to;
+  cc = ob->cc;
+  bcc = ob->bcc;
+  subject = ob->subject;
+  headers = ob->headers;
+  text = ob->text;
+  file = ob->file;
+  logfile = ob->logfile;
+  oncelog = ob->oncelog;
+  file_expand = ob->file_expand;
+  return_message = ob->return_message;
+
+  if ((from  != NULL &&
+        (from = checkexpand(from, addr, tblock->name, cke_hdr)) == NULL) ||
+      (reply_to    != NULL &&
+        (reply_to = checkexpand(reply_to, addr, tblock->name, cke_hdr)) == NULL) ||
+      (to    != NULL &&
+        (to = checkexpand(to, addr, tblock->name, cke_hdr)) == NULL) ||
+      (cc    != NULL &&
+        (cc = checkexpand(cc, addr, tblock->name, cke_hdr)) == NULL) ||
+      (bcc   != NULL &&
+        (bcc = checkexpand(bcc, addr, tblock->name, cke_hdr)) == NULL) ||
+      (subject   != NULL &&
+        (subject = checkexpand(subject, addr, tblock->name, cke_hdr)) == NULL) ||
+      (headers != NULL &&
+        (headers = checkexpand(headers, addr, tblock->name, cke_text)) == NULL) ||
+      (text  != NULL &&
+        (text = checkexpand(text, addr, tblock->name, cke_text)) == NULL) ||
+      (file  != NULL &&
+        (file = checkexpand(file, addr, tblock->name, cke_file)) == NULL) ||
+      (logfile != NULL &&
+        (logfile = checkexpand(logfile, addr, tblock->name, cke_file)) == NULL) ||
+      (oncelog != NULL &&
+        (oncelog = checkexpand(oncelog, addr, tblock->name, cke_file)) == NULL) ||
+      (oncerepeat != NULL &&
+        (oncerepeat = checkexpand(oncerepeat, addr, tblock->name, cke_file)) == NULL))
+    return FALSE;
+
+  if (oncerepeat != NULL)
+    {
+    once_repeat_sec = readconf_readtime(oncerepeat, 0, FALSE);
+    if (once_repeat_sec < 0)
+      {
+      addr->transport_return = FAIL;
+      addr->message = string_sprintf("Invalid time value \"%s\" for "
+        "\"once_repeat\" in %s transport", oncerepeat, tblock->name);
+      return FALSE;
+      }
+    }
+  }
+
+/* If the never_mail option is set, we have to scan all the recipients and
+remove those that match. */
+
+if (ob->never_mail != NULL)
+  {
+  uschar *never_mail = expand_string(ob->never_mail);
+
+  if (never_mail == NULL)
+    {
+    addr->transport_return = FAIL;
+    addr->message = string_sprintf("Failed to expand \"%s\" for "
+      "\"never_mail\" in %s transport", ob->never_mail, tblock->name);
+    return FALSE;
+    }
+
+  if (to != NULL) check_never_mail(&to, never_mail);
+  if (cc != NULL) check_never_mail(&cc, never_mail);
+  if (bcc != NULL) check_never_mail(&bcc, never_mail);
+
+  if (to == NULL && cc == NULL && bcc == NULL)
+    {
+    DEBUG(D_transport)
+      debug_printf("*** all recipients removed by never_mail\n");
+    return OK;
+    }
+  }
+
+/* If the -N option is set, can't do any more. */
+
+if (dont_deliver)
+  {
+  DEBUG(D_transport)
+    debug_printf("*** delivery by %s transport bypassed by -N option\n",
+      tblock->name);
+  return FALSE;
+  }
+
+
+/* If the oncelog field is set, we send want to send only one message to the
+given recipient(s). This works only on the "To" field. If there is no "To"
+field, the message is always sent. If the To: field contains more than one
+recipient, the effect might not be quite as envisaged. If once_file_size is
+set, instead of a dbm file, we use a regular file containing a circular buffer
+recipient cache. */
+
+if (oncelog != NULL && *oncelog != 0 && to != NULL)
+  {
+  time_t then = 0;
+
+  /* Handle fixed-size cache file. */
+
+  if (ob->once_file_size > 0)
+    {
+    uschar *p;
+    struct stat statbuf;
+    cache_fd = Uopen(oncelog, O_CREAT|O_RDWR, ob->mode);
+
+    if (cache_fd < 0 || fstat(cache_fd, &statbuf) != 0)
+      {
+      addr->transport_return = DEFER;
+      addr->message = string_sprintf("Failed to %s \"once\" file %s when "
+        "sending message from %s transport: %s",
+        (cache_fd < 0)? "open" : "stat", oncelog, tblock->name,
+          strerror(errno));
+      goto END_OFF;
+      }
+
+    /* Get store in the temporary pool and read the entire file into it. We get
+    an amount of store that is big enough to add the new entry on the end if we
+    need to do that. */
+
+    cache_size = statbuf.st_size;
+    add_size = sizeof(time_t) + Ustrlen(to) + 1;
+    cache_buff = store_get(cache_size + add_size);
+
+    if (read(cache_fd, cache_buff, cache_size) != cache_size)
+      {
+      addr->transport_return = DEFER;
+      addr->basic_errno = errno;
+      addr->message = US"error while reading \"once\" file";
+      goto END_OFF;
+      }
+
+    DEBUG(D_transport) debug_printf("%d bytes read from %s\n", cache_size, oncelog);
+
+    /* Scan the data for this recipient. Each entry in the file starts with
+    a time_t sized time value, followed by the address, followed by a binary
+    zero. If we find a match, put the time into "then", and the place where it
+    was found into "cache_time". Otherwise, "then" is left at zero. */
+
+    p = cache_buff;
+    while (p < cache_buff + cache_size)
+      {
+      uschar *s = p + sizeof(time_t);
+      uschar *nextp = s + Ustrlen(s) + 1;
+      if (Ustrcmp(to, s) == 0)
+        {
+        memcpy(&then, p, sizeof(time_t));
+        cache_time = p;
+        break;
+        }
+      p = nextp;
+      }
+    }
+
+  /* Use a DBM file for the list of previous recipients. */
+
+  else
+    {
+    EXIM_DATUM key_datum, result_datum;
+    EXIM_DBOPEN(oncelog, O_RDWR|O_CREAT, ob->mode, &dbm_file);
+    if (dbm_file == NULL)
+      {
+      addr->transport_return = DEFER;
+      addr->message = string_sprintf("Failed to open %s file %s when sending "
+        "message from %s transport: %s", EXIM_DBTYPE, oncelog, tblock->name,
+        strerror(errno));
+      goto END_OFF;
+      }
+
+    EXIM_DATUM_INIT(key_datum);        /* Some DBM libraries need datums */
+    EXIM_DATUM_INIT(result_datum);     /* to be cleared */
+    EXIM_DATUM_DATA(key_datum) = CS to;
+    EXIM_DATUM_SIZE(key_datum) = Ustrlen(to) + 1;
+
+    if (EXIM_DBGET(dbm_file, key_datum, result_datum))
+      {
+      /* If the datum size is that of a binary time, we are in the new world
+      where messages are sent periodically. Otherwise the file is an old one,
+      where the datum was filled with a tod_log time, which is assumed to be
+      different in size. For that, only one message is ever sent. This change
+      introduced at Exim 3.00. In a couple of years' time the test on the size
+      can be abolished. */
+
+      if (EXIM_DATUM_SIZE(result_datum) == sizeof(time_t))
+        {
+        memcpy(&then, EXIM_DATUM_DATA(result_datum), sizeof(time_t));
+        }
+      else then = now;
+      }
+    }
+
+  /* Either "then" is set zero, if no message has yet been sent, or it
+  is set to the time of the last sending. */
+
+  if (then != 0 && (once_repeat_sec <= 0 || now - then < once_repeat_sec))
+    {
+    DEBUG(D_transport) debug_printf("message previously sent to %s%s\n", to,
+      (once_repeat_sec > 0)? " and repeat time not reached" : "");
+    log_fd = Uopen(logfile, O_WRONLY|O_APPEND|O_CREAT, ob->mode);
+    if (log_fd >= 0)
+      {
+      uschar *ptr = log_buffer;
+      sprintf(CS ptr, "%s\n  previously sent to %.200s\n", tod_stamp(tod_log), to);
+      while(*ptr) ptr++;
+      if(write(log_fd, log_buffer, ptr - log_buffer) != ptr-log_buffer
+        || close(log_fd))
+        DEBUG(D_transport) debug_printf("Problem writing log file %s for %s "
+          "transport\n", logfile, tblock->name);
+      }
+    goto END_OFF;
+    }
+
+  DEBUG(D_transport) debug_printf("%s %s\n", (then <= 0)?
+    "no previous message sent to" : "repeat time reached for", to);
+  }
+
+/* We are going to send a message. Ensure any requested file is available. */
+
+if (file != NULL)
+  {
+  ff = Ufopen(file, "rb");
+  if (ff == NULL && !ob->file_optional)
+    {
+    addr->transport_return = DEFER;
+    addr->message = string_sprintf("Failed to open file %s when sending "
+      "message from %s transport: %s", file, tblock->name, strerror(errno));
+    return FALSE;
+    }
+  }
+
+/* Make a subprocess to send the message */
+
+pid = child_open_exim(&fd);
+
+/* Creation of child failed; defer this delivery. */
+
+if (pid < 0)
+  {
+  addr->transport_return = DEFER;
+  addr->message = string_sprintf("Failed to create child process to send "
+    "message from %s transport: %s", tblock->name, strerror(errno));
+  DEBUG(D_transport) debug_printf("%s\n", addr->message);
+  return FALSE;
+  }
+
+/* Create the message to be sent - recipients are taken from the headers,
+as the -t option is used. The "headers" stuff *must* be last in case there
+are newlines in it which might, if placed earlier, screw up other headers. */
+
+f = fdopen(fd, "wb");
+
+if (from != NULL) fprintf(f, "From: %s\n", from);
+if (reply_to != NULL) fprintf(f, "Reply-To: %s\n", reply_to);
+if (to != NULL) fprintf(f, "To: %s\n", to);
+if (cc != NULL) fprintf(f, "Cc: %s\n", cc);
+if (bcc != NULL) fprintf(f, "Bcc: %s\n", bcc);
+if (subject != NULL) fprintf(f, "Subject: %s\n", subject);
+
+/* Generate In-Reply-To from the message_id header; there should
+always be one, but code defensively. */
+
+for (h = header_list; h != NULL; h = h->next)
+  if (h->type == htype_id) break;
+
+if (h != NULL)
+  {
+  message_id = Ustrchr(h->text, ':') + 1;
+  while (isspace(*message_id)) message_id++;
+  fprintf(f, "In-Reply-To: %s", message_id);
+  }
+
+/* Generate a References header if there is at least one of Message-ID:,
+References:, or In-Reply-To: (see RFC 2822). */
+
+for (h = header_list; h != NULL; h = h->next)
+  if (h->type != htype_old && strncmpic(US"References:", h->text, 11) == 0)
+    break;
+
+if (h == NULL)
+  for (h = header_list; h != NULL; h = h->next)
+    if (h->type != htype_old && strncmpic(US"In-Reply-To:", h->text, 12) == 0)
+      break;
+
+/* We limit the total length of references.  Although there is no fixed
+limit, some systems do not like headers growing beyond recognition.
+Keep the first message ID for the thread root and the last few for
+the position inside the thread, up to a maximum of 12 altogether. */
+
+if (h != NULL || message_id != NULL)
+  {
+  fprintf(f, "References:");
+  if (h != NULL)
+    {
+    uschar *s, *id, *error;
+    uschar *referenced_ids[12];
+    int reference_count = 0;
+    int i;
+
+    s = Ustrchr(h->text, ':') + 1;
+    parse_allow_group = FALSE;
+    while (*s != 0 && (s = parse_message_id(s, &id, &error)) != NULL)
+      {
+      if (reference_count == sizeof(referenced_ids)/sizeof(uschar *))
+        {
+        memmove(referenced_ids + 1, referenced_ids + 2,
+           sizeof(referenced_ids) - 2*sizeof(uschar *));
+        referenced_ids[reference_count - 1] = id;
+        }
+      else referenced_ids[reference_count++] = id;
+      }
+    for (i = 0; i < reference_count; ++i) fprintf(f, " %s", referenced_ids[i]);
+    }
+
+  /* The message id will have a newline on the end of it. */
+
+  if (message_id != NULL) fprintf(f, " %s", message_id);
+    else fprintf(f, "\n");
+  }
+
+/* Add an Auto-Submitted: header */
+
+fprintf(f, "Auto-Submitted: auto-replied\n");
+
+/* Add any specially requested headers */
+
+if (headers != NULL) fprintf(f, "%s\n", headers);
+fprintf(f, "\n");
+
+if (text != NULL)
+  {
+  fprintf(f, "%s", CS text);
+  if (text[Ustrlen(text)-1] != '\n') fprintf(f, "\n");
+  }
+
+if (ff != NULL)
+  {
+  while (Ufgets(big_buffer, big_buffer_size, ff) != NULL)
+    {
+    if (file_expand)
+      {
+      uschar *s = expand_string(big_buffer);
+      DEBUG(D_transport)
+        {
+        if (s == NULL)
+          debug_printf("error while expanding line from file:\n  %s\n  %s\n",
+            big_buffer, expand_string_message);
+        }
+      fprintf(f, "%s", (s == NULL)? CS big_buffer : CS s);
+      }
+    else fprintf(f, "%s", CS big_buffer);
+    }
+  }
+
+/* Copy the original message if required, observing the return size
+limit if we are returning the body. */
+
+if (return_message)
+  {
+  uschar *rubric = (tblock->headers_only)?
+    US"------ This is a copy of the message's header lines.\n"
+    : (tblock->body_only)?
+    US"------ This is a copy of the body of the message, without the headers.\n"
+    :
+    US"------ This is a copy of the message, including all the headers.\n";
+
+  if (bounce_return_size_limit > 0 && !tblock->headers_only)
+    {
+    struct stat statbuf;
+    int max = (bounce_return_size_limit/DELIVER_IN_BUFFER_SIZE + 1) *
+      DELIVER_IN_BUFFER_SIZE;
+    if (fstat(deliver_datafile, &statbuf) == 0 && statbuf.st_size > max)
+      {
+      fprintf(f, "\n%s"
+"------ The body of the message is " OFF_T_FMT " characters long; only the first\n"
+"------ %d or so are included here.\n\n", rubric, statbuf.st_size,
+        (max/1000)*1000);
+      }
+    else fprintf(f, "\n%s\n", rubric);
+    }
+  else fprintf(f, "\n%s\n", rubric);
+
+  fflush(f);
+  transport_count = 0;
+  transport_write_message(addr, fileno(f),
+    (tblock->body_only? topt_no_headers : 0) |
+    (tblock->headers_only? topt_no_body : 0) |
+    (tblock->return_path_add? topt_add_return_path : 0) |
+    (tblock->delivery_date_add? topt_add_delivery_date : 0) |
+    (tblock->envelope_to_add? topt_add_envelope_to : 0),
+    bounce_return_size_limit, tblock->add_headers, tblock->remove_headers,
+    NULL, NULL, tblock->rewrite_rules, tblock->rewrite_existflags);
+  }
+
+/* End the message and wait for the child process to end; no timeout. */
+
+(void)fclose(f);
+rc = child_close(pid, 0);
+
+/* Update the "sent to" log whatever the yield. This errs on the side of
+missing out a message rather than risking sending more than one. We either have
+cache_fd set to a fixed size, circular buffer file, or dbm_file set to an open
+DBM file (or neither, if "once" is not set). */
+
+/* Update fixed-size cache file. If cache_time is set, we found a previous
+entry; that is the spot into which to put the current time. Otherwise we have
+to add a new record; remove the first one in the file if the file is too big.
+We always rewrite the entire file in a single write operation. This is
+(hopefully) going to be the safest thing because there is no interlocking
+between multiple simultaneous deliveries. */
+
+if (cache_fd >= 0)
+  {
+  uschar *from = cache_buff;
+  int size = cache_size;
+  (void)lseek(cache_fd, 0, SEEK_SET);
+
+  if (cache_time == NULL)
+    {
+    cache_time = from + size;
+    memcpy(cache_time + sizeof(time_t), to, add_size - sizeof(time_t));
+    size += add_size;
+
+    if (cache_size > 0 && size > ob->once_file_size)
+      {
+      from += sizeof(time_t) + Ustrlen(from + sizeof(time_t)) + 1;
+      size -= (from - cache_buff);
+      }
+    }
+
+  memcpy(cache_time, &now, sizeof(time_t));
+  if(write(cache_fd, from, size) != size)
+    DEBUG(D_transport) debug_printf("Problem writing cache file %s for %s "
+      "transport\n", oncelog, tblock->name);
+  }
+
+/* Update DBM file */
+
+else if (dbm_file != NULL)
+  {
+  EXIM_DATUM key_datum, value_datum;
+  EXIM_DATUM_INIT(key_datum);          /* Some DBM libraries need to have */
+  EXIM_DATUM_INIT(value_datum);        /* cleared datums. */
+  EXIM_DATUM_DATA(key_datum) = CS to;
+  EXIM_DATUM_SIZE(key_datum) = Ustrlen(to) + 1;
+
+  /* Many OS define the datum value, sensibly, as a void *. However, there
+  are some which still have char *. By casting this address to a char * we
+  can avoid warning messages from the char * systems. */
+
+  EXIM_DATUM_DATA(value_datum) = CS (&now);
+  EXIM_DATUM_SIZE(value_datum) = (int)sizeof(time_t);
+  EXIM_DBPUT(dbm_file, key_datum, value_datum);
+  }
+
+/* If sending failed, defer to try again - but if once is set the next
+try will skip, of course. However, if there were no recipients in the
+message, we do not fail. */
+
+if (rc != 0)
+  {
+  if (rc == EXIT_NORECIPIENTS)
+    {
+    DEBUG(D_any) debug_printf("%s transport: message contained no recipients\n",
+      tblock->name);
+    }
+  else
+    {
+    addr->transport_return = DEFER;
+    addr->message = string_sprintf("Failed to send message from %s "
+      "transport (%d)", tblock->name, rc);
+    goto END_OFF;
+    }
+  }
+
+/* Log the sending of the message if successful and required. If the file
+fails to open, it's hard to know what to do. We cannot write to the Exim
+log from here, since we may be running under an unprivileged uid. We don't
+want to fail the delivery, since the message has been successfully sent. For
+the moment, ignore open failures. Write the log entry as a single write() to a
+file opened for appending, in order to avoid interleaving of output from
+different processes. The log_buffer can be used exactly as for main log
+writing. */
+
+if (logfile != NULL)
+  {
+  int log_fd = Uopen(logfile, O_WRONLY|O_APPEND|O_CREAT, ob->mode);
+  if (log_fd >= 0)
+    {
+    uschar *ptr = log_buffer;
+    DEBUG(D_transport) debug_printf("logging message details\n");
+    sprintf(CS ptr, "%s\n", tod_stamp(tod_log));
+    while(*ptr) ptr++;
+    if (from != NULL)
+      {
+      (void)string_format(ptr, LOG_BUFFER_SIZE - (ptr-log_buffer),
+        "  From: %s\n", from);
+      while(*ptr) ptr++;
+      }
+    if (to != NULL)
+      {
+      (void)string_format(ptr, LOG_BUFFER_SIZE - (ptr-log_buffer),
+        "  To: %s\n", to);
+      while(*ptr) ptr++;
+      }
+    if (cc != NULL)
+      {
+      (void)string_format(ptr, LOG_BUFFER_SIZE - (ptr-log_buffer),
+        "  Cc: %s\n", cc);
+      while(*ptr) ptr++;
+      }
+    if (bcc != NULL)
+      {
+      (void)string_format(ptr, LOG_BUFFER_SIZE - (ptr-log_buffer),
+        "  Bcc: %s\n", bcc);
+      while(*ptr) ptr++;
+      }
+    if (subject != NULL)
+      {
+      (void)string_format(ptr, LOG_BUFFER_SIZE - (ptr-log_buffer),
+        "  Subject: %s\n", subject);
+      while(*ptr) ptr++;
+      }
+    if (headers != NULL)
+      {
+      (void)string_format(ptr, LOG_BUFFER_SIZE - (ptr-log_buffer),
+        "  %s\n", headers);
+      while(*ptr) ptr++;
+      }
+    if(write(log_fd, log_buffer, ptr - log_buffer) != ptr-log_buffer
+      || close(log_fd))
+      DEBUG(D_transport) debug_printf("Problem writing log file %s for %s "
+        "transport\n", logfile, tblock->name);
+    }
+  else DEBUG(D_transport) debug_printf("Failed to open log file %s for %s "
+    "transport: %s\n", logfile, tblock->name, strerror(errno));
+  }
+
+END_OFF:
+if (dbm_file != NULL) EXIM_DBCLOSE(dbm_file);
+if (cache_fd > 0) (void)close(cache_fd);
+
+DEBUG(D_transport) debug_printf("%s transport succeeded\n", tblock->name);
+
+return FALSE;
+}
+
+/* End of transport/autoreply.c */
diff --git a/src/transports/autoreply.h b/src/transports/autoreply.h
new file mode 100644 (file)
index 0000000..fcfd981
--- /dev/null
@@ -0,0 +1,45 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Private structure for the private options. */
+
+typedef struct {
+  uschar *from;
+  uschar *reply_to;
+  uschar *to;
+  uschar *cc;
+  uschar *bcc;
+  uschar *subject;
+  uschar *headers;
+  uschar *text;
+  uschar *file;
+  uschar *logfile;
+  uschar *oncelog;
+  uschar *once_repeat;
+  uschar *never_mail;
+  int   mode;
+  off_t once_file_size;
+  BOOL  file_expand;
+  BOOL  file_optional;
+  BOOL  return_message;
+} autoreply_transport_options_block;
+
+/* Data for reading the private options. */
+
+extern optionlist autoreply_transport_options[];
+extern int autoreply_transport_options_count;
+
+/* Block containing default values. */
+
+extern autoreply_transport_options_block autoreply_transport_option_defaults;
+
+/* The main and init entry points for the transport */
+
+extern BOOL autoreply_transport_entry(transport_instance *, address_item *);
+extern void autoreply_transport_init(transport_instance *);
+
+/* End of transports/autoreply.h */
diff --git a/src/transports/lmtp.c b/src/transports/lmtp.c
new file mode 100644 (file)
index 0000000..84bbeb9
--- /dev/null
@@ -0,0 +1,789 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2014 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+
+#include "../exim.h"
+#include "lmtp.h"
+
+#define PENDING_OK 256
+
+
+/* Options specific to the lmtp transport. They must be in alphabetic
+order (note that "_" comes before the lower case letters). Those starting
+with "*" are not settable by the user but are used by the option-reading
+software for alternative value types. Some options are stored in the transport
+instance block so as to be publicly visible; these are flagged with opt_public.
+*/
+
+optionlist lmtp_transport_options[] = {
+  { "batch_id",          opt_stringptr | opt_public,
+      (void *)offsetof(transport_instance, batch_id) },
+  { "batch_max",         opt_int | opt_public,
+      (void *)offsetof(transport_instance, batch_max) },
+  { "command",           opt_stringptr,
+      (void *)offsetof(lmtp_transport_options_block, cmd) },
+  { "ignore_quota",      opt_bool,
+      (void *)offsetof(lmtp_transport_options_block, ignore_quota) },
+  { "socket",            opt_stringptr,
+      (void *)offsetof(lmtp_transport_options_block, skt) },
+  { "timeout",           opt_time,
+      (void *)offsetof(lmtp_transport_options_block, timeout) }
+};
+
+/* Size of the options list. An extern variable has to be used so that its
+address can appear in the tables drtables.c. */
+
+int lmtp_transport_options_count =
+  sizeof(lmtp_transport_options)/sizeof(optionlist);
+
+/* Default private options block for the lmtp transport. */
+
+lmtp_transport_options_block lmtp_transport_option_defaults = {
+  NULL,           /* cmd */
+  NULL,           /* skt */
+  5*60,           /* timeout */
+  0,              /* options */
+  FALSE           /* ignore_quota */
+};
+
+
+
+/*************************************************
+*          Initialization entry point            *
+*************************************************/
+
+/* Called for each instance, after its options have been read, to
+enable consistency checks to be done, or anything else that needs
+to be set up. */
+
+void
+lmtp_transport_init(transport_instance *tblock)
+{
+lmtp_transport_options_block *ob =
+  (lmtp_transport_options_block *)(tblock->options_block);
+
+/* Either the command field or the socket field must be set */
+
+if ((ob->cmd == NULL) == (ob->skt == NULL))
+  log_write(0, LOG_PANIC_DIE|LOG_CONFIG,
+    "one (and only one) of command or socket must be set for the %s transport",
+    tblock->name);
+
+/* If a fixed uid field is set, then a gid field must also be set. */
+
+if (tblock->uid_set && !tblock->gid_set && tblock->expand_gid == NULL)
+  log_write(0, LOG_PANIC_DIE|LOG_CONFIG,
+    "user set without group for the %s transport", tblock->name);
+
+/* Set up the bitwise options for transport_write_message from the various
+driver options. Only one of body_only and headers_only can be set. */
+
+ob->options |=
+  (tblock->body_only? topt_no_headers : 0) |
+  (tblock->headers_only? topt_no_body : 0) |
+  (tblock->return_path_add? topt_add_return_path : 0) |
+  (tblock->delivery_date_add? topt_add_delivery_date : 0) |
+  (tblock->envelope_to_add? topt_add_envelope_to : 0) |
+  topt_use_crlf | topt_end_dot;
+}
+
+
+/*************************************************
+*          Check an LMTP response                *
+*************************************************/
+
+/* This function is given an errno code and the LMTP response buffer to
+analyse. It sets an appropriate message and puts the first digit of the
+response code into the yield variable. If no response was actually read, a
+suitable digit is chosen.
+
+Arguments:
+  errno_value  pointer to the errno value
+  more_errno   from the top address for use with ERRNO_FILTER_FAIL
+  buffer       the LMTP response buffer
+  yield        where to put a one-digit LMTP response code
+  message      where to put an errror message
+
+Returns:       TRUE if a "QUIT" command should be sent, else FALSE
+*/
+
+static BOOL check_response(int *errno_value, int more_errno, uschar *buffer,
+  int *yield, uschar **message)
+{
+*yield = '4';    /* Default setting is to give a temporary error */
+
+/* Handle response timeout */
+
+if (*errno_value == ETIMEDOUT)
+  {
+  *message = string_sprintf("LMTP timeout after %s", big_buffer);
+  if (transport_count > 0)
+    *message = string_sprintf("%s (%d bytes written)", *message,
+      transport_count);
+  *errno_value = 0;
+  return FALSE;
+  }
+
+/* Handle malformed LMTP response */
+
+if (*errno_value == ERRNO_SMTPFORMAT)
+  {
+  *message = string_sprintf("Malformed LMTP response after %s: %s",
+    big_buffer, string_printing(buffer));
+  return FALSE;
+  }
+
+/* Handle a failed filter process error; can't send QUIT as we mustn't
+end the DATA. */
+
+if (*errno_value == ERRNO_FILTER_FAIL)
+  {
+  *message = string_sprintf("transport filter process failed (%d)%s",
+    more_errno,
+    (more_errno == EX_EXECFAILED)? ": unable to execute command" : "");
+  return FALSE;
+  }
+
+/* Handle a failed add_headers expansion; can't send QUIT as we mustn't
+end the DATA. */
+
+if (*errno_value == ERRNO_CHHEADER_FAIL)
+  {
+  *message =
+    string_sprintf("failed to expand headers_add or headers_remove: %s",
+      expand_string_message);
+  return FALSE;
+  }
+
+/* Handle failure to write a complete data block */
+
+if (*errno_value == ERRNO_WRITEINCOMPLETE)
+  {
+  *message = string_sprintf("failed to write a data block");
+  return FALSE;
+  }
+
+/* Handle error responses from the remote process. */
+
+if (buffer[0] != 0)
+  {
+  uschar *s = string_printing(buffer);
+  *message = string_sprintf("LMTP error after %s: %s", big_buffer, s);
+  *yield = buffer[0];
+  return TRUE;
+  }
+
+/* No data was read. If there is no errno, this must be the EOF (i.e.
+connection closed) case, which causes deferral. Otherwise, leave the errno
+value to be interpreted. In all cases, we have to assume the connection is now
+dead. */
+
+if (*errno_value == 0)
+  {
+  *errno_value = ERRNO_SMTPCLOSED;
+  *message = string_sprintf("LMTP connection closed after %s", big_buffer);
+  }
+
+return FALSE;
+}
+
+
+
+/*************************************************
+*             Write LMTP command                 *
+*************************************************/
+
+/* The formatted command is left in big_buffer so that it can be reflected in
+any error message.
+
+Arguments:
+  fd         the fd to write to
+  format     a format, starting with one of
+             of HELO, MAIL FROM, RCPT TO, DATA, ".", or QUIT.
+  ...        data for the format
+
+Returns:     TRUE if successful, FALSE if not, with errno set
+*/
+
+static BOOL
+lmtp_write_command(int fd, const char *format, ...)
+{
+int count, rc;
+va_list ap;
+va_start(ap, format);
+if (!string_vformat(big_buffer, big_buffer_size, CS format, ap))
+  {
+  errno = ERRNO_SMTPFORMAT;
+  return FALSE;
+  }
+va_end(ap);
+count = Ustrlen(big_buffer);
+DEBUG(D_transport|D_v) debug_printf("  LMTP>> %s", big_buffer);
+rc = write(fd, big_buffer, count);
+big_buffer[count-2] = 0;     /* remove \r\n for debug and error message */
+if (rc > 0) return TRUE;
+DEBUG(D_transport) debug_printf("write failed: %s\n", strerror(errno));
+return FALSE;
+}
+
+
+
+
+/*************************************************
+*              Read LMTP response                *
+*************************************************/
+
+/* This function reads an LMTP response with a timeout, and returns the
+response in the given buffer. It also analyzes the first digit of the reply
+code and returns FALSE if it is not acceptable.
+
+FALSE is also returned after a reading error. In this case buffer[0] will be
+zero, and the error code will be in errno.
+
+Arguments:
+  f         a file to read from
+  buffer    where to put the response
+  size      the size of the buffer
+  okdigit   the expected first digit of the response
+  timeout   the timeout to use
+
+Returns:    TRUE if a valid, non-error response was received; else FALSE
+*/
+
+static BOOL
+lmtp_read_response(FILE *f, uschar *buffer, int size, int okdigit, int timeout)
+{
+int count;
+uschar *ptr = buffer;
+uschar *readptr = buffer;
+
+/* Ensure errno starts out zero */
+
+errno = 0;
+
+/* Loop for handling LMTP responses that do not all come in one line. */
+
+for (;;)
+  {
+  /* If buffer is too full, something has gone wrong. */
+
+  if (size < 10)
+    {
+    *readptr = 0;
+    errno = ERRNO_SMTPFORMAT;
+    return FALSE;
+    }
+
+  /* Loop to cover the read getting interrupted. */
+
+  for (;;)
+    {
+    char *rc;
+    int save_errno;
+
+    *readptr = 0;           /* In case nothing gets read */
+    sigalrm_seen = FALSE;
+    alarm(timeout);
+    rc = Ufgets(readptr, size-1, f);
+    save_errno = errno;
+    alarm(0);
+    errno = save_errno;
+
+    if (rc != NULL) break;  /* A line has been read */
+
+    /* Handle timeout; must do this first because it uses EINTR */
+
+    if (sigalrm_seen) errno = ETIMEDOUT;
+
+    /* If some other interrupt arrived, just retry. We presume this to be rare,
+    but it can happen (e.g. the SIGUSR1 signal sent by exiwhat causes
+    read() to exit). */
+
+    else if (errno == EINTR)
+      {
+      DEBUG(D_transport) debug_printf("EINTR while reading LMTP response\n");
+      continue;
+      }
+
+    /* Handle other errors, including EOF; ensure buffer is completely empty. */
+
+    buffer[0] = 0;
+    return FALSE;
+    }
+
+  /* Adjust size in case we have to read another line, and adjust the
+  count to be the length of the line we are about to inspect. */
+
+  count = Ustrlen(readptr);
+  size -= count;
+  count += readptr - ptr;
+
+  /* See if the final two characters in the buffer are \r\n. If not, we
+  have to read some more. At least, that is what we should do on a strict
+  interpretation of the RFC. But accept LF as well, as we do for SMTP. */
+
+  if (ptr[count-1] != '\n')
+    {
+    DEBUG(D_transport)
+      {
+      int i;
+      debug_printf("LMTP input line incomplete in one buffer:\n  ");
+      for (i = 0; i < count; i++)
+        {
+        int c = (ptr[i]);
+        if (mac_isprint(c)) debug_printf("%c", c); else debug_printf("<%d>", c);
+        }
+      debug_printf("\n");
+      }
+    readptr = ptr + count;
+    continue;
+    }
+
+  /* Remove any whitespace at the end of the buffer. This gets rid of CR, LF
+  etc. at the end. Show it, if debugging, formatting multi-line responses. */
+
+  while (count > 0 && isspace(ptr[count-1])) count--;
+  ptr[count] = 0;
+
+  DEBUG(D_transport|D_v)
+    {
+    uschar *s = ptr;
+    uschar *t = ptr;
+    while (*t != 0)
+      {
+      while (*t != 0 && *t != '\n') t++;
+      debug_printf("  %s %*s\n", (s == ptr)? "LMTP<<" : "      ",
+        (int)(t-s), s);
+      if (*t == 0) break;
+      s = t = t + 1;
+      }
+    }
+
+  /* Check the format of the response: it must start with three digits; if
+  these are followed by a space or end of line, the response is complete. If
+  they are followed by '-' this is a multi-line response and we must look for
+  another line until the final line is reached. The only use made of multi-line
+  responses is to pass them back as error messages. We therefore just
+  concatenate them all within the buffer, which should be large enough to
+  accept any reasonable number of lines. A multiline response may already
+  have been read in one go - hence the loop here. */
+
+  for(;;)
+    {
+    uschar *p;
+    if (count < 3 ||
+       !isdigit(ptr[0]) ||
+       !isdigit(ptr[1]) ||
+       !isdigit(ptr[2]) ||
+       (ptr[3] != '-' && ptr[3] != ' ' && ptr[3] != 0))
+      {
+      errno = ERRNO_SMTPFORMAT;    /* format error */
+      return FALSE;
+      }
+
+    /* If a single-line response, exit the loop */
+
+    if (ptr[3] != '-') break;
+
+    /* For a multi-line response see if the next line is already read, and if
+    so, stay in this loop to check it. */
+
+    p = ptr + 3;
+    while (*(++p) != 0)
+      {
+      if (*p == '\n')
+        {
+        ptr = ++p;
+        break;
+        }
+      }
+    if (*p == 0) break;   /* No more lines to check */
+    }
+
+  /* End of response. If the last of the lines we are looking at is the final
+  line, we are done. Otherwise more data has to be read. */
+
+  if (ptr[3] != '-') break;
+
+  /* Move the reading pointer upwards in the buffer and insert \n in case this
+  is an error message that subsequently gets printed. Set the scanning pointer
+  to the reading pointer position. */
+
+  ptr += count;
+  *ptr++ = '\n';
+  size--;
+  readptr = ptr;
+  }
+
+/* Return a value that depends on the LMTP return code. Ensure that errno is
+zero, because the caller of this function looks at errno when FALSE is
+returned, to distinguish between an unexpected return code and other errors
+such as timeouts, lost connections, etc. */
+
+errno = 0;
+return buffer[0] == okdigit;
+}
+
+
+
+
+
+
+/*************************************************
+*              Main entry point                  *
+*************************************************/
+
+/* See local README for interface details. For setup-errors, this transport
+returns FALSE, indicating that the first address has the status for all; in
+normal cases it returns TRUE, indicating that each address has its own status
+set. */
+
+BOOL
+lmtp_transport_entry(
+  transport_instance *tblock,      /* data for this instantiation */
+  address_item *addrlist)          /* address(es) we are working on */
+{
+pid_t pid = 0;
+FILE *out;
+lmtp_transport_options_block *ob =
+  (lmtp_transport_options_block *)(tblock->options_block);
+struct sockaddr_un sockun;         /* don't call this "sun" ! */
+int timeout = ob->timeout;
+int fd_in = -1, fd_out = -1;
+int code, save_errno;
+BOOL send_data;
+BOOL yield = FALSE;
+address_item *addr;
+uschar *igquotstr = US"";
+uschar *sockname = NULL;
+uschar **argv;
+uschar buffer[256];
+
+DEBUG(D_transport) debug_printf("%s transport entered\n", tblock->name);
+
+/* Initialization ensures that either a command or a socket is specified, but
+not both. When a command is specified, call the common function for creating an
+argument list and expanding the items. */
+
+if (ob->cmd != NULL)
+  {
+  DEBUG(D_transport) debug_printf("using command %s\n", ob->cmd);
+  sprintf(CS buffer, "%.50s transport", tblock->name);
+  if (!transport_set_up_command(&argv, ob->cmd, TRUE, PANIC, addrlist, buffer,
+       NULL))
+    return FALSE;
+  }
+
+/* When a socket is specified, expand the string and create a socket. */
+
+else
+  {
+  DEBUG(D_transport) debug_printf("using socket %s\n", ob->skt);
+  sockname = expand_string(ob->skt);
+  if (sockname == NULL)
+    {
+    addrlist->message = string_sprintf("Expansion of \"%s\" (socket setting "
+      "for %s transport) failed: %s", ob->skt, tblock->name,
+      expand_string_message);
+    return FALSE;
+    }
+  if ((fd_in = fd_out = socket(PF_UNIX, SOCK_STREAM, 0)) == -1)
+    {
+    addrlist->message = string_sprintf(
+      "Failed to create socket %s for %s transport: %s",
+        ob->skt, tblock->name, strerror(errno));
+    return FALSE;
+    }
+  }
+
+/* If the -N option is set, can't do any more. Presume all has gone well. */
+
+if (dont_deliver)
+  {
+  DEBUG(D_transport)
+    debug_printf("*** delivery by %s transport bypassed by -N option",
+      tblock->name);
+  addrlist->transport_return = OK;
+  return FALSE;
+  }
+
+/* As this is a local transport, we are already running with the required
+uid/gid and current directory. Request that the new process be a process group
+leader, so we can kill it and all its children on an error. */
+
+if (ob->cmd != NULL)
+  {
+  if ((pid = child_open(argv, NULL, 0, &fd_in, &fd_out, TRUE)) < 0)
+    {
+    addrlist->message = string_sprintf(
+      "Failed to create child process for %s transport: %s", tblock->name,
+        strerror(errno));
+    return FALSE;
+    }
+  }
+
+/* For a socket, try to make the connection */
+
+else
+  {
+  sockun.sun_family = AF_UNIX;
+  sprintf(sockun.sun_path, "%.*s", (int)(sizeof(sockun.sun_path)-1), sockname);
+  if(connect(fd_out, (struct sockaddr *)(&sockun), sizeof(sockun)) == -1)
+    {
+    addrlist->message = string_sprintf(
+      "Failed to connect to socket %s for %s transport: %s",
+        sockun.sun_path, tblock->name, strerror(errno));
+    return FALSE;
+    }
+  }
+
+/* Make the output we are going to read into a file. */
+
+out = fdopen(fd_out, "rb");
+
+/* Now we must implement the LMTP protocol. It is like SMTP, except that after
+the end of the message, a return code for every accepted RCPT TO is sent. This
+allows for message+recipient checks after the message has been received. */
+
+/* First thing is to wait for an initial greeting. */
+
+Ustrcpy(big_buffer, "initial connection");
+if (!lmtp_read_response(out, buffer, sizeof(buffer), '2',
+  timeout)) goto RESPONSE_FAILED;
+
+/* Next, we send a LHLO command, and expect a positive response */
+
+if (!lmtp_write_command(fd_in, "%s %s\r\n", "LHLO",
+  primary_hostname)) goto WRITE_FAILED;
+
+if (!lmtp_read_response(out, buffer, sizeof(buffer), '2',
+     timeout)) goto RESPONSE_FAILED;
+
+/* If the ignore_quota option is set, note whether the server supports the
+IGNOREQUOTA option, and if so, set an appropriate addition for RCPT. */
+
+if (ob->ignore_quota)
+  igquotstr = (pcre_exec(regex_IGNOREQUOTA, NULL, CS buffer,
+    Ustrlen(CS buffer), 0, PCRE_EOPT, NULL, 0) >= 0)? US" IGNOREQUOTA" : US"";
+
+/* Now the envelope sender */
+
+if (!lmtp_write_command(fd_in, "MAIL FROM:<%s>\r\n", return_path))
+  goto WRITE_FAILED;
+
+if (!lmtp_read_response(out, buffer, sizeof(buffer), '2', timeout))
+  {
+  if (errno == 0 && buffer[0] == '4')
+    {
+    errno = ERRNO_MAIL4XX;
+    addrlist->more_errno |= ((buffer[1] - '0')*10 + buffer[2] - '0') << 8;
+    }
+  goto RESPONSE_FAILED;
+  }
+
+/* Next, we hand over all the recipients. Some may be permanently or
+temporarily rejected; others may be accepted, for now. */
+
+send_data = FALSE;
+for (addr = addrlist; addr != NULL; addr = addr->next)
+  {
+  if (!lmtp_write_command(fd_in, "RCPT TO:<%s>%s\r\n",
+       transport_rcpt_address(addr, tblock->rcpt_include_affixes), igquotstr))
+    goto WRITE_FAILED;
+  if (lmtp_read_response(out, buffer, sizeof(buffer), '2', timeout))
+    {
+    send_data = TRUE;
+    addr->transport_return = PENDING_OK;
+    }
+  else
+    {
+    if (errno != 0 || buffer[0] == 0) goto RESPONSE_FAILED;
+    addr->message = string_sprintf("LMTP error after %s: %s", big_buffer,
+      string_printing(buffer));
+    setflag(addr, af_pass_message);   /* Allow message to go to user */
+    if (buffer[0] == '5') addr->transport_return = FAIL; else
+      {
+      addr->basic_errno = ERRNO_RCPT4XX;
+      addr->more_errno |= ((buffer[1] - '0')*10 + buffer[2] - '0') << 8;
+      }
+    }
+  }
+
+/* Now send the text of the message if there were any good recipients. */
+
+if (send_data)
+  {
+  BOOL ok;
+
+  if (!lmtp_write_command(fd_in, "DATA\r\n")) goto WRITE_FAILED;
+  if (!lmtp_read_response(out, buffer, sizeof(buffer), '3', timeout))
+    {
+    if (errno == 0 && buffer[0] == '4')
+      {
+      errno = ERRNO_DATA4XX;
+      addrlist->more_errno |= ((buffer[1] - '0')*10 + buffer[2] - '0') << 8;
+      }
+    goto RESPONSE_FAILED;
+    }
+
+  sigalrm_seen = FALSE;
+  transport_write_timeout = timeout;
+  Ustrcpy(big_buffer, "sending data block");   /* For error messages */
+  DEBUG(D_transport|D_v)
+    debug_printf("  LMTP>> writing message and terminating \".\"\n");
+
+  transport_count = 0;
+  ok = transport_write_message(addrlist, fd_in, ob->options, 0,
+        tblock->add_headers, tblock->remove_headers, US".", US"..",
+        tblock->rewrite_rules, tblock->rewrite_existflags);
+
+  /* Failure can either be some kind of I/O disaster (including timeout),
+  or the failure of a transport filter or the expansion of added headers. */
+
+  if (!ok)
+    {
+    buffer[0] = 0;              /* There hasn't been a response */
+    goto RESPONSE_FAILED;
+    }
+
+  Ustrcpy(big_buffer, "end of data");   /* For error messages */
+
+  /* We now expect a response for every address that was accepted above,
+  in the same order. For those that get a response, their status is fixed;
+  any that are accepted have been handed over, even if later responses crash -
+  at least, that's how I read RFC 2033. */
+
+  for (addr = addrlist; addr != NULL; addr = addr->next)
+    {
+    if (addr->transport_return != PENDING_OK) continue;
+
+    if (lmtp_read_response(out, buffer, sizeof(buffer), '2', timeout))
+      {
+      addr->transport_return = OK;
+      if ((log_extra_selector & LX_smtp_confirmation) != 0)
+        {
+        uschar *s = string_printing(buffer);
+        addr->message = (s == buffer)? (uschar *)string_copy(s) : s;
+        }
+      }
+    /* If the response has failed badly, use it for all the remaining pending
+    addresses and give up. */
+
+    else if (errno != 0 || buffer[0] == 0)
+      {
+      address_item *a;
+      save_errno = errno;
+      check_response(&save_errno, addr->more_errno, buffer, &code,
+        &(addr->message));
+      addr->transport_return = (code == '5')? FAIL : DEFER;
+      for (a = addr->next; a != NULL; a = a->next)
+        {
+        if (a->transport_return != PENDING_OK) continue;
+        a->basic_errno = addr->basic_errno;
+        a->message = addr->message;
+        a->transport_return = addr->transport_return;
+        }
+      break;
+      }
+
+    /* Otherwise, it's an LMTP error code return for one address */
+
+    else
+      {
+      if (buffer[0] == '4')
+        {
+        addr->basic_errno = ERRNO_DATA4XX;
+        addr->more_errno |= ((buffer[1] - '0')*10 + buffer[2] - '0') << 8;
+        }
+      addr->message = string_sprintf("LMTP error after %s: %s", big_buffer,
+        string_printing(buffer));
+      addr->transport_return = (buffer[0] == '5')? FAIL : DEFER;
+      setflag(addr, af_pass_message);   /* Allow message to go to user */
+      }
+    }
+  }
+
+/* The message transaction has completed successfully - this doesn't mean that
+all the addresses have necessarily been transferred, but each has its status
+set, so we change the yield to TRUE. */
+
+yield = TRUE;
+(void) lmtp_write_command(fd_in, "QUIT\r\n");
+(void) lmtp_read_response(out, buffer, sizeof(buffer), '2', 1);
+
+goto RETURN;
+
+
+/* Come here if any call to read_response, other than a response after the data
+phase, failed. Put the error in the top address - this will be replicated
+because the yield is still FALSE. (But omit ETIMEDOUT, as there will already be
+a suitable message.) Analyse the error, and if if isn't too bad, send a QUIT
+command. Wait for the response with a short timeout, so we don't wind up this
+process before the far end has had time to read the QUIT. */
+
+RESPONSE_FAILED:
+
+save_errno = errno;
+if (errno != ETIMEDOUT && errno != 0) addrlist->basic_errno = errno;
+addrlist->message = NULL;
+
+if (check_response(&save_errno, addrlist->more_errno,
+    buffer, &code, &(addrlist->message)))
+  {
+  (void) lmtp_write_command(fd_in, "QUIT\r\n");
+  (void) lmtp_read_response(out, buffer, sizeof(buffer), '2', 1);
+  }
+
+addrlist->transport_return = (code == '5')? FAIL : DEFER;
+if (code == '4' && save_errno > 0)
+  addrlist->message = string_sprintf("%s: %s", addrlist->message,
+    strerror(save_errno));
+goto KILL_AND_RETURN;
+
+/* Come here if there are errors during writing of a command or the message
+itself. This error will be applied to all the addresses. */
+
+WRITE_FAILED:
+
+addrlist->transport_return = PANIC;
+addrlist->basic_errno = errno;
+if (errno == ERRNO_CHHEADER_FAIL)
+  addrlist->message =
+    string_sprintf("Failed to expand headers_add or headers_remove: %s",
+      expand_string_message);
+else if (errno == ERRNO_FILTER_FAIL)
+  addrlist->message = string_sprintf("Filter process failure");
+else if (errno == ERRNO_WRITEINCOMPLETE)
+  addrlist->message = string_sprintf("Failed repeatedly to write data");
+else if (errno == ERRNO_SMTPFORMAT)
+  addrlist->message = US"overlong LMTP command generated";
+else
+  addrlist->message = string_sprintf("Error %d", errno);
+
+/* Come here after errors. Kill off the process. */
+
+KILL_AND_RETURN:
+
+if (pid > 0) killpg(pid, SIGKILL);
+
+/* Come here from all paths after the subprocess is created. Wait for the
+process, but with a timeout. */
+
+RETURN:
+
+(void)child_close(pid, timeout);
+
+if (fd_in >= 0) (void)close(fd_in);
+if (fd_out >= 0) (void)fclose(out);
+
+DEBUG(D_transport)
+  debug_printf("%s transport yields %d\n", tblock->name, yield);
+
+return yield;
+}
+
+/* End of transport/lmtp.c */
diff --git a/src/transports/lmtp.h b/src/transports/lmtp.h
new file mode 100644 (file)
index 0000000..93f0f89
--- /dev/null
@@ -0,0 +1,32 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Private structure for the private options. */
+
+typedef struct {
+  uschar *cmd;
+  uschar *skt;
+  int   timeout;
+  int   options;
+  BOOL  ignore_quota;
+} lmtp_transport_options_block;
+
+/* Data for reading the private options. */
+
+extern optionlist lmtp_transport_options[];
+extern int lmtp_transport_options_count;
+
+/* Block containing default values. */
+
+extern lmtp_transport_options_block lmtp_transport_option_defaults;
+
+/* The main and init entry points for the transport */
+
+extern BOOL lmtp_transport_entry(transport_instance *, address_item *);
+extern void lmtp_transport_init(transport_instance *);
+
+/* End of transports/lmtp.h */
diff --git a/src/transports/pipe.c b/src/transports/pipe.c
new file mode 100644 (file)
index 0000000..3366a6d
--- /dev/null
@@ -0,0 +1,1161 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2014 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+
+#include "../exim.h"
+#include "pipe.h"
+
+#ifdef HAVE_SETCLASSRESOURCES
+#include <login_cap.h>
+#endif
+
+
+
+/* Options specific to the pipe transport. They must be in alphabetic
+order (note that "_" comes before the lower case letters). Those starting
+with "*" are not settable by the user but are used by the option-reading
+software for alternative value types. Some options are stored in the transport
+instance block so as to be publicly visible; these are flagged with opt_public.
+*/
+
+optionlist pipe_transport_options[] = {
+  { "allow_commands",    opt_stringptr,
+      (void *)offsetof(pipe_transport_options_block, allow_commands) },
+  { "batch_id",          opt_stringptr | opt_public,
+      (void *)offsetof(transport_instance, batch_id) },
+  { "batch_max",         opt_int | opt_public,
+      (void *)offsetof(transport_instance, batch_max) },
+  { "check_string",      opt_stringptr,
+      (void *)offsetof(pipe_transport_options_block, check_string) },
+  { "command",           opt_stringptr,
+      (void *)offsetof(pipe_transport_options_block, cmd) },
+  { "environment",       opt_stringptr,
+      (void *)offsetof(pipe_transport_options_block, environment) },
+  { "escape_string",     opt_stringptr,
+      (void *)offsetof(pipe_transport_options_block, escape_string) },
+  { "force_command",         opt_bool,
+      (void *)offsetof(pipe_transport_options_block, force_command) },
+  { "freeze_exec_fail",  opt_bool,
+      (void *)offsetof(pipe_transport_options_block, freeze_exec_fail) },
+  { "freeze_signal",     opt_bool,
+      (void *)offsetof(pipe_transport_options_block, freeze_signal) },
+  { "ignore_status",     opt_bool,
+      (void *)offsetof(pipe_transport_options_block, ignore_status) },
+  { "log_defer_output",  opt_bool | opt_public,
+      (void *)offsetof(transport_instance, log_defer_output) },
+  { "log_fail_output",   opt_bool | opt_public,
+      (void *)offsetof(transport_instance, log_fail_output) },
+  { "log_output",        opt_bool | opt_public,
+      (void *)offsetof(transport_instance, log_output) },
+  { "max_output",        opt_mkint,
+      (void *)offsetof(pipe_transport_options_block, max_output) },
+  { "message_prefix",    opt_stringptr,
+      (void *)offsetof(pipe_transport_options_block, message_prefix) },
+  { "message_suffix",    opt_stringptr,
+      (void *)offsetof(pipe_transport_options_block, message_suffix) },
+  { "path",              opt_stringptr,
+      (void *)offsetof(pipe_transport_options_block, path) },
+  { "permit_coredump",   opt_bool,
+      (void *)offsetof(pipe_transport_options_block, permit_coredump) },
+  { "pipe_as_creator",   opt_bool | opt_public,
+      (void *)offsetof(transport_instance, deliver_as_creator) },
+  { "restrict_to_path",  opt_bool,
+      (void *)offsetof(pipe_transport_options_block, restrict_to_path) },
+  { "return_fail_output",opt_bool | opt_public,
+      (void *)offsetof(transport_instance, return_fail_output) },
+  { "return_output",     opt_bool | opt_public,
+      (void *)offsetof(transport_instance, return_output) },
+  { "temp_errors",       opt_stringptr,
+      (void *)offsetof(pipe_transport_options_block, temp_errors) },
+  { "timeout",           opt_time,
+      (void *)offsetof(pipe_transport_options_block, timeout) },
+  { "timeout_defer",     opt_bool,
+      (void *)offsetof(pipe_transport_options_block, timeout_defer) },
+  { "umask",             opt_octint,
+      (void *)offsetof(pipe_transport_options_block, umask) },
+  { "use_bsmtp",         opt_bool,
+      (void *)offsetof(pipe_transport_options_block, use_bsmtp) },
+  #ifdef HAVE_SETCLASSRESOURCES
+  { "use_classresources", opt_bool,
+      (void *)offsetof(pipe_transport_options_block, use_classresources) },
+  #endif
+  { "use_crlf",          opt_bool,
+      (void *)offsetof(pipe_transport_options_block, use_crlf) },
+  { "use_shell",         opt_bool,
+      (void *)offsetof(pipe_transport_options_block, use_shell) },
+};
+
+/* Size of the options list. An extern variable has to be used so that its
+address can appear in the tables drtables.c. */
+
+int pipe_transport_options_count =
+  sizeof(pipe_transport_options)/sizeof(optionlist);
+
+/* Default private options block for the pipe transport. */
+
+pipe_transport_options_block pipe_transport_option_defaults = {
+  NULL,           /* cmd */
+  NULL,           /* allow_commands */
+  NULL,           /* environment */
+  US"/bin:/usr/bin",  /* path */
+  NULL,           /* message_prefix (reset in init if not bsmtp) */
+  NULL,           /* message_suffix (ditto) */
+  US mac_expanded_string(EX_TEMPFAIL) ":"    /* temp_errors */
+     mac_expanded_string(EX_CANTCREAT),
+  NULL,           /* check_string */
+  NULL,           /* escape_string */
+  022,            /* umask */
+  20480,          /* max_output */
+  60*60,          /* timeout */
+  0,              /* options */
+  FALSE,          /* force_command */
+  FALSE,          /* freeze_exec_fail */
+  FALSE,          /* freeze_signal */
+  FALSE,          /* ignore_status */
+  FALSE,          /* permit_coredump */
+  FALSE,          /* restrict_to_path */
+  FALSE,          /* timeout_defer */
+  FALSE,          /* use_shell */
+  FALSE,          /* use_bsmtp */
+  FALSE,          /* use_classresources */
+  FALSE           /* use_crlf */
+};
+
+
+
+/*************************************************
+*              Setup entry point                 *
+*************************************************/
+
+/* Called for each delivery in the privileged state, just before the uid/gid
+are changed and the main entry point is called. In a system that supports the
+login_cap facilities, this function is used to set the class resource limits
+for the user.  It may also re-enable coredumps.
+
+Arguments:
+  tblock     points to the transport instance
+  addrlist   addresses about to be delivered (not used)
+  dummy      not used (doesn't pass back data)
+  uid        the uid that will be set (not used)
+  gid        the gid that will be set (not used)
+  errmsg     where to put an error message
+
+Returns:     OK, FAIL, or DEFER
+*/
+
+static int
+pipe_transport_setup(transport_instance *tblock, address_item *addrlist,
+  transport_feedback *dummy, uid_t uid, gid_t gid, uschar **errmsg)
+{
+pipe_transport_options_block *ob =
+  (pipe_transport_options_block *)(tblock->options_block);
+
+addrlist = addrlist;  /* Keep compiler happy */
+dummy = dummy;
+uid = uid;
+gid = gid;
+errmsg = errmsg;
+ob = ob;
+
+#ifdef HAVE_SETCLASSRESOURCES
+if (ob->use_classresources)
+  {
+  struct passwd *pw = getpwuid(uid);
+  if (pw != NULL)
+    {
+    login_cap_t *lc = login_getpwclass(pw);
+    if (lc != NULL)
+      {
+      setclassresources(lc);
+      login_close(lc);
+      }
+    }
+  }
+#endif
+
+#ifdef RLIMIT_CORE
+if (ob->permit_coredump)
+  {
+  struct rlimit rl;
+  rl.rlim_cur = RLIM_INFINITY;
+  rl.rlim_max = RLIM_INFINITY;
+  if (setrlimit(RLIMIT_CORE, &rl) < 0)
+    {
+#ifdef SETRLIMIT_NOT_SUPPORTED
+    if (errno != ENOSYS && errno != ENOTSUP)
+#endif
+      log_write(0, LOG_MAIN,
+          "delivery setrlimit(RLIMIT_CORE, RLIM_INFINITY) failed: %s",
+          strerror(errno));
+    }
+  }
+#endif
+
+return OK;
+}
+
+
+
+/*************************************************
+*          Initialization entry point            *
+*************************************************/
+
+/* Called for each instance, after its options have been read, to
+enable consistency checks to be done, or anything else that needs
+to be set up. */
+
+void
+pipe_transport_init(transport_instance *tblock)
+{
+pipe_transport_options_block *ob =
+  (pipe_transport_options_block *)(tblock->options_block);
+
+/* Set up the setup entry point, to be called in the privileged state */
+
+tblock->setup = pipe_transport_setup;
+
+/* If pipe_as_creator is set, then uid/gid should not be set. */
+
+if (tblock->deliver_as_creator && (tblock->uid_set || tblock->gid_set ||
+  tblock->expand_uid != NULL || tblock->expand_gid != NULL))
+    log_write(0, LOG_PANIC_DIE|LOG_CONFIG,
+      "both pipe_as_creator and an explicit uid/gid are set for the %s "
+        "transport", tblock->name);
+
+/* If a fixed uid field is set, then a gid field must also be set. */
+
+if (tblock->uid_set && !tblock->gid_set && tblock->expand_gid == NULL)
+  log_write(0, LOG_PANIC_DIE|LOG_CONFIG,
+    "user set without group for the %s transport", tblock->name);
+
+/* Temp_errors must consist only of digits and colons, but there can be
+spaces round the colons, so allow them too. */
+
+if (ob->temp_errors != NULL && Ustrcmp(ob->temp_errors, "*") != 0)
+  {
+  size_t p = Ustrspn(ob->temp_errors, "0123456789: ");
+  if (ob->temp_errors[p] != 0)
+    log_write(0, LOG_PANIC_DIE|LOG_CONFIG,
+      "temp_errors must be a list of numbers or an asterisk for the %s "
+      "transport", tblock->name);
+  }
+
+/* Only one of return_output/return_fail_output or log_output/log_fail_output
+should be set. */
+
+if (tblock->return_output && tblock->return_fail_output)
+  log_write(0, LOG_PANIC_DIE|LOG_CONFIG,
+    "both return_output and return_fail_output set for %s transport",
+    tblock->name);
+
+if (tblock->log_output && tblock->log_fail_output)
+  log_write(0, LOG_PANIC_DIE|LOG_CONFIG,
+    "both log_output and log_fail_output set for the %s transport",
+    tblock->name);
+
+/* If batch SMTP is set, force the check and escape strings, and arrange that
+headers are also escaped. */
+
+if (ob->use_bsmtp)
+  {
+  ob->check_string = US".";
+  ob->escape_string = US"..";
+  ob->options |= topt_escape_headers;
+  }
+
+/* If not batch SMTP, and message_prefix or message_suffix are unset, insert
+default values for them. */
+
+else
+  {
+  if (ob->message_prefix == NULL) ob->message_prefix =
+    US"From ${if def:return_path{$return_path}{MAILER-DAEMON}} ${tod_bsdinbox}\n";
+  if (ob->message_suffix == NULL) ob->message_suffix = US"\n";
+  }
+
+/* The restrict_to_path  and use_shell options are incompatible */
+
+if (ob->restrict_to_path && ob->use_shell)
+  log_write(0, LOG_PANIC_DIE|LOG_CONFIG,
+    "both restrict_to_path and use_shell set for %s transport",
+    tblock->name);
+
+/* The allow_commands and use_shell options are incompatible */
+
+if (ob->allow_commands && ob->use_shell)
+  log_write(0, LOG_PANIC_DIE|LOG_CONFIG,
+    "both allow_commands and use_shell set for %s transport",
+    tblock->name);
+
+/* Set up the bitwise options for transport_write_message from the various
+driver options. Only one of body_only and headers_only can be set. */
+
+ob->options |=
+  (tblock->body_only? topt_no_headers : 0) |
+  (tblock->headers_only? topt_no_body : 0) |
+  (tblock->return_path_add? topt_add_return_path : 0) |
+  (tblock->delivery_date_add? topt_add_delivery_date : 0) |
+  (tblock->envelope_to_add? topt_add_envelope_to : 0) |
+  (ob->use_crlf? topt_use_crlf : 0);
+}
+
+
+
+/*************************************************
+*          Set up direct (non-shell) command     *
+*************************************************/
+
+/* This function is called when a command line is to be parsed by the transport
+and executed directly, without the use of /bin/sh.
+
+Arguments:
+  argvptr            pointer to anchor for argv vector
+  cmd                points to the command string
+  expand_arguments   true if expansion is to occur
+  expand_fail        error if expansion fails
+  addr               chain of addresses
+  tname              the transport name
+  ob                 the transport options block
+
+Returns:             TRUE if all went well; otherwise an error will be
+                     set in the first address and FALSE returned
+*/
+
+static BOOL
+set_up_direct_command(uschar ***argvptr, uschar *cmd, BOOL expand_arguments,
+  int expand_fail, address_item *addr, uschar *tname,
+  pipe_transport_options_block *ob)
+{
+BOOL permitted = FALSE;
+uschar **argv;
+uschar buffer[64];
+
+/* Set up "transport <name>" to be put in any error messages, and then
+call the common function for creating an argument list and expanding
+the items if necessary. If it fails, this function fails (error information
+is in the addresses). */
+
+sprintf(CS buffer, "%.50s transport", tname);
+if (!transport_set_up_command(argvptr, cmd, expand_arguments, expand_fail,
+      addr, buffer, NULL))
+  return FALSE;
+
+/* Point to the set-up arguments. */
+
+argv = *argvptr;
+
+/* If allow_commands is set, see if the command is in the permitted list. */
+
+if (ob->allow_commands != NULL)
+  {
+  int sep = 0;
+  uschar *s, *p;
+  uschar buffer[256];
+
+  s = expand_string(ob->allow_commands);
+  if (s == NULL)
+    {
+    addr->transport_return = DEFER;
+    addr->message = string_sprintf("failed to expand string \"%s\" "
+      "for %s transport: %s", ob->allow_commands, tname, expand_string_message);
+    return FALSE;
+    }
+
+  while ((p = string_nextinlist(&s, &sep, buffer, sizeof(buffer))) != NULL)
+    {
+    if (Ustrcmp(p, argv[0]) == 0) { permitted = TRUE; break; }
+    }
+  }
+
+/* If permitted is TRUE it means the command was found in the allowed list, and
+no further checks are done. If permitted = FALSE, it either means
+allow_commands wasn't set, or that the command didn't match anything in the
+list. In both cases, if restrict_to_path is set, we fail if the command
+contains any slashes, but if restrict_to_path is not set, we must fail the
+command only if allow_commands is set. */
+
+if (!permitted)
+  {
+  if (ob->restrict_to_path)
+    {
+    if (Ustrchr(argv[0], '/') != NULL)
+      {
+      addr->transport_return = FAIL;
+      addr->message = string_sprintf("\"/\" found in \"%s\" (command for %s "
+        "transport) - failed for security reasons", cmd, tname);
+      return FALSE;
+      }
+    }
+
+  else if (ob->allow_commands != NULL)
+    {
+    addr->transport_return = FAIL;
+    addr->message = string_sprintf("\"%s\" command not permitted by %s "
+      "transport", argv[0], tname);
+    return FALSE;
+    }
+  }
+
+/* If the command is not an absolute path, search the PATH directories
+for it. */
+
+if (argv[0][0] != '/')
+  {
+  int sep = 0;
+  uschar *p;
+  uschar *listptr = ob->path;
+  uschar buffer[1024];
+
+  while ((p = string_nextinlist(&listptr, &sep, buffer, sizeof(buffer))) != NULL)
+    {
+    struct stat statbuf;
+    sprintf(CS big_buffer, "%.256s/%.256s", p, argv[0]);
+    if (Ustat(big_buffer, &statbuf) == 0)
+      {
+      argv[0] = string_copy(big_buffer);
+      break;
+      }
+    }
+  if (p == NULL)
+    {
+    addr->transport_return = FAIL;
+    addr->message = string_sprintf("\"%s\" command not found for %s transport",
+      argv[0], tname);
+    return FALSE;
+    }
+  }
+
+return TRUE;
+}
+
+
+/*************************************************
+*               Set up shell command             *
+*************************************************/
+
+/* This function is called when a command line is to be passed to /bin/sh
+without parsing inside the transport.
+
+Arguments:
+  argvptr            pointer to anchor for argv vector
+  cmd                points to the command string
+  expand_arguments   true if expansion is to occur
+  expand_fail        error if expansion fails
+  addr               chain of addresses
+  tname              the transport name
+
+Returns:             TRUE if all went well; otherwise an error will be
+                     set in the first address and FALSE returned
+*/
+
+static BOOL
+set_up_shell_command(uschar ***argvptr, uschar *cmd, BOOL expand_arguments,
+  int expand_fail, address_item *addr, uschar *tname)
+{
+uschar **argv;
+
+*argvptr = argv = store_get((4)*sizeof(uschar *));
+
+argv[0] = US"/bin/sh";
+argv[1] = US"-c";
+
+/* We have to take special action to handle the special "variable" called
+$pipe_addresses, which is not recognized by the normal expansion function. */
+
+DEBUG(D_transport)
+  debug_printf("shell pipe command before expansion:\n  %s\n", cmd);
+
+if (expand_arguments)
+  {
+  uschar *s = cmd;
+  uschar *p = Ustrstr(cmd, "pipe_addresses");
+
+  if (p != NULL && (
+         (p > cmd && p[-1] == '$') ||
+         (p > cmd + 1 && p[-2] == '$' && p[-1] == '{' && p[14] == '}')))
+    {
+    address_item *ad;
+    uschar *q = p + 14;
+    int size = Ustrlen(cmd) + 64;
+    int offset;
+
+    if (p[-1] == '{') { q++; p--; }
+
+    s = store_get(size);
+    offset = p - cmd - 1;
+    Ustrncpy(s, cmd, offset);
+
+    for (ad = addr; ad != NULL; ad = ad->next)
+      {
+      if (ad != addr) string_cat(s, &size, &offset, US" ", 1);
+      string_cat(s, &size, &offset, ad->address, Ustrlen(ad->address));
+      }
+
+    string_cat(s, &size, &offset, q, Ustrlen(q));
+    s[offset] = 0;
+    }
+
+  /* Allow $recipients in the expansion iff it comes from a system filter */
+
+  enable_dollar_recipients = addr != NULL &&
+    addr->parent != NULL &&
+    Ustrcmp(addr->parent->address, "system-filter") == 0;
+  argv[2] = expand_string(s);
+  enable_dollar_recipients = FALSE;
+
+  if (argv[2] == NULL)
+    {
+    addr->transport_return = search_find_defer? DEFER : expand_fail;
+    addr->message = string_sprintf("Expansion of command \"%s\" "
+      "in %s transport failed: %s",
+      cmd, tname, expand_string_message);
+    return FALSE;
+    }
+
+  DEBUG(D_transport)
+    debug_printf("shell pipe command after expansion:\n  %s\n", argv[2]);
+  }
+else argv[2] = cmd;
+
+argv[3] = (uschar *)0;
+return TRUE;
+}
+
+
+
+
+/*************************************************
+*              Main entry point                  *
+*************************************************/
+
+/* See local README for interface details. This transport always returns FALSE,
+indicating that the status in the first address is the status for all addresses
+in a batch. */
+
+BOOL
+pipe_transport_entry(
+  transport_instance *tblock,      /* data for this instantiation */
+  address_item *addr)              /* address(es) we are working on */
+{
+pid_t pid, outpid;
+int fd_in, fd_out, rc;
+int envcount = 0;
+int envsep = 0;
+int expand_fail;
+pipe_transport_options_block *ob =
+  (pipe_transport_options_block *)(tblock->options_block);
+int timeout = ob->timeout;
+BOOL written_ok = FALSE;
+BOOL expand_arguments;
+uschar **argv;
+uschar *envp[50];
+uschar *envlist = ob->environment;
+uschar *cmd, *ss;
+uschar *eol = (ob->use_crlf)? US"\r\n" : US"\n";
+
+DEBUG(D_transport) debug_printf("%s transport entered\n", tblock->name);
+
+/* Set up for the good case */
+
+addr->transport_return = OK;
+addr->basic_errno = 0;
+
+/* Pipes are not accepted as general addresses, but they can be generated from
+.forward files or alias files. In those cases, the pfr flag is set, and the
+command to be obeyed is pointed to by addr->local_part; it starts with the pipe
+symbol. In other cases, the command is supplied as one of the pipe transport's
+options. */
+
+if (testflag(addr, af_pfr) && addr->local_part[0] == '|')
+  {
+  if (ob->force_command)
+    {
+    /* Enables expansion of $address_pipe into seperate arguments */
+    setflag(addr, af_force_command);
+    cmd = ob->cmd;
+    expand_arguments = TRUE;
+    expand_fail = PANIC;
+    }
+  else
+    {
+    cmd = addr->local_part + 1;
+    while (isspace(*cmd)) cmd++;
+    expand_arguments = testflag(addr, af_expand_pipe);
+    expand_fail = FAIL;
+    }
+  }
+else
+  {
+  cmd = ob->cmd;
+  expand_arguments = TRUE;
+  expand_fail = PANIC;
+  }
+
+/* If no command has been supplied, we are in trouble.
+ * We also check for an empty string since it may be
+ * coming from addr->local_part[0] == '|'
+ */
+
+if (cmd == NULL || *cmd == '\0')
+  {
+  addr->transport_return = DEFER;
+  addr->message = string_sprintf("no command specified for %s transport",
+    tblock->name);
+  return FALSE;
+  }
+
+/* When a pipe is set up by a filter file, there may be values for $thisaddress
+and numerical the variables in existence. These are passed in
+addr->pipe_expandn for use here. */
+
+if (expand_arguments && addr->pipe_expandn != NULL)
+  {
+  uschar **ss = addr->pipe_expandn;
+  expand_nmax = -1;
+  if (*ss != NULL) filter_thisaddress = *ss++;
+  while (*ss != NULL)
+    {
+    expand_nstring[++expand_nmax] = *ss;
+    expand_nlength[expand_nmax] = Ustrlen(*ss++);
+    }
+  }
+
+/* The default way of processing the command is to split it up into arguments
+here, and run it directly. This offers some security advantages. However, there
+are installations that want by default to run commands under /bin/sh always, so
+there is an option to do that. */
+
+if (ob->use_shell)
+  {
+  if (!set_up_shell_command(&argv, cmd, expand_arguments, expand_fail, addr,
+    tblock->name)) return FALSE;
+  }
+else if (!set_up_direct_command(&argv, cmd, expand_arguments, expand_fail, addr,
+  tblock->name, ob)) return FALSE;
+
+expand_nmax = -1;           /* Reset */
+filter_thisaddress = NULL;
+
+/* Set up the environment for the command. */
+
+envp[envcount++] = string_sprintf("LOCAL_PART=%s", deliver_localpart);
+envp[envcount++] = string_sprintf("LOGNAME=%s", deliver_localpart);
+envp[envcount++] = string_sprintf("USER=%s", deliver_localpart);
+envp[envcount++] = string_sprintf("LOCAL_PART_PREFIX=%#s",
+  deliver_localpart_prefix);
+envp[envcount++] = string_sprintf("LOCAL_PART_SUFFIX=%#s",
+  deliver_localpart_suffix);
+envp[envcount++] = string_sprintf("DOMAIN=%s", deliver_domain);
+envp[envcount++] = string_sprintf("HOME=%#s", deliver_home);
+envp[envcount++] = string_sprintf("MESSAGE_ID=%s", message_id);
+envp[envcount++] = string_sprintf("PATH=%s", ob->path);
+envp[envcount++] = string_sprintf("RECIPIENT=%#s%#s%#s@%#s",
+  deliver_localpart_prefix, deliver_localpart, deliver_localpart_suffix,
+  deliver_domain);
+envp[envcount++] = string_sprintf("QUALIFY_DOMAIN=%s", qualify_domain_sender);
+envp[envcount++] = string_sprintf("SENDER=%s", sender_address);
+envp[envcount++] = US"SHELL=/bin/sh";
+
+if (addr->host_list != NULL)
+  envp[envcount++] = string_sprintf("HOST=%s", addr->host_list->name);
+
+if (timestamps_utc) envp[envcount++] = US"TZ=UTC";
+else if (timezone_string != NULL && timezone_string[0] != 0)
+  envp[envcount++] = string_sprintf("TZ=%s", timezone_string);
+
+/* Add any requested items */
+
+if (envlist != NULL)
+  {
+  envlist = expand_string(envlist);
+  if (envlist == NULL)
+    {
+    addr->transport_return = DEFER;
+    addr->message = string_sprintf("failed to expand string \"%s\" "
+      "for %s transport: %s", ob->environment, tblock->name,
+      expand_string_message);
+    return FALSE;
+    }
+  }
+
+while ((ss = string_nextinlist(&envlist, &envsep, big_buffer, big_buffer_size))
+       != NULL)
+   {
+   if (envcount > sizeof(envp)/sizeof(uschar *) - 2)
+     {
+     addr->transport_return = DEFER;
+     addr->message = string_sprintf("too many environment settings for "
+       "%s transport", tblock->name);
+     return FALSE;
+     }
+   envp[envcount++] = string_copy(ss);
+   }
+
+envp[envcount] = NULL;
+
+/* If the -N option is set, can't do any more. */
+
+if (dont_deliver)
+  {
+  DEBUG(D_transport)
+    debug_printf("*** delivery by %s transport bypassed by -N option",
+      tblock->name);
+  return FALSE;
+  }
+
+
+/* Handling the output from the pipe is tricky. If a file for catching this
+output is provided, we could in theory just hand that fd over to the process,
+but this isn't very safe because it might loop and carry on writing for
+ever (which is exactly what happened in early versions of Exim). Therefore we
+use the standard child_open() function, which creates pipes. We can then read
+our end of the output pipe and count the number of bytes that come through,
+chopping the sub-process if it exceeds some limit.
+
+However, this means we want to run a sub-process with both its input and output
+attached to pipes. We can't handle that easily from a single parent process
+using straightforward code such as the transport_write_message() function
+because the subprocess might not be reading its input because it is trying to
+write to a full output pipe. The complication of redesigning the world to
+handle this is too great - simpler just to run another process to do the
+reading of the output pipe. */
+
+
+/* As this is a local transport, we are already running with the required
+uid/gid and current directory. Request that the new process be a process group
+leader, so we can kill it and all its children on a timeout. */
+
+if ((pid = child_open(argv, envp, ob->umask, &fd_in, &fd_out, TRUE)) < 0)
+  {
+  addr->transport_return = DEFER;
+  addr->message = string_sprintf(
+    "Failed to create child process for %s transport: %s", tblock->name,
+      strerror(errno));
+  return FALSE;
+  }
+
+/* Now fork a process to handle the output that comes down the pipe. */
+
+if ((outpid = fork()) < 0)
+  {
+  addr->basic_errno = errno;
+  addr->transport_return = DEFER;
+  addr->message = string_sprintf(
+    "Failed to create process for handling output in %s transport",
+      tblock->name);
+  (void)close(fd_in);
+  (void)close(fd_out);
+  return FALSE;
+  }
+
+/* This is the code for the output-handling subprocess. Read from the pipe
+in chunks, and write to the return file if one is provided. Keep track of
+the number of bytes handled. If the limit is exceeded, try to kill the
+subprocess group, and in any case close the pipe and exit, which should cause
+the subprocess to fail. */
+
+if (outpid == 0)
+  {
+  int count = 0;
+  (void)close(fd_in);
+  set_process_info("reading output from |%s", cmd);
+  while ((rc = read(fd_out, big_buffer, big_buffer_size)) > 0)
+    {
+    if (addr->return_file >= 0)
+      if(write(addr->return_file, big_buffer, rc) != rc)
+        DEBUG(D_transport) debug_printf("Problem writing to return_file\n");
+    count += rc;
+    if (count > ob->max_output)
+      {
+      DEBUG(D_transport) debug_printf("Too much output from pipe - killed\n");
+      if (addr->return_file >= 0)
+       {
+        uschar *message = US"\n\n*** Too much output - remainder discarded ***\n";
+        rc = Ustrlen(message);
+        if(write(addr->return_file, message, rc) != rc)
+          DEBUG(D_transport) debug_printf("Problem writing to return_file\n");
+       }
+      killpg(pid, SIGKILL);
+      break;
+      }
+    }
+  (void)close(fd_out);
+  _exit(0);
+  }
+
+(void)close(fd_out);  /* Not used in this process */
+
+
+/* Carrying on now with the main parent process. Attempt to write the message
+to it down the pipe. It is a fallacy to think that you can detect write errors
+when the sub-process fails to read the pipe. The parent process may complete
+writing and close the pipe before the sub-process completes. We could sleep a
+bit here to let the sub-process get going, but it may still not complete. So we
+ignore all writing errors. (When in the test harness, we do do a short sleep so
+any debugging output is likely to be in the same order.) */
+
+if (running_in_test_harness) millisleep(500);
+
+DEBUG(D_transport) debug_printf("Writing message to pipe\n");
+
+/* Arrange to time out writes if there is a timeout set. */
+
+if (timeout > 0)
+  {
+  sigalrm_seen = FALSE;
+  transport_write_timeout = timeout;
+  }
+
+/* Reset the counter of bytes written */
+
+transport_count = 0;
+
+/* First write any configured prefix information */
+
+if (ob->message_prefix != NULL)
+  {
+  uschar *prefix = expand_string(ob->message_prefix);
+  if (prefix == NULL)
+    {
+    addr->transport_return = search_find_defer? DEFER : PANIC;
+    addr->message = string_sprintf("Expansion of \"%s\" (prefix for %s "
+      "transport) failed: %s", ob->message_prefix, tblock->name,
+      expand_string_message);
+    return FALSE;
+    }
+  if (!transport_write_block(fd_in, prefix, Ustrlen(prefix)))
+    goto END_WRITE;
+  }
+
+/* If the use_bsmtp option is set, we need to write SMTP prefix information.
+The various different values for batching are handled outside; if there is more
+than one address available here, all must be included. Force SMTP dot-handling.
+*/
+
+if (ob->use_bsmtp)
+  {
+  address_item *a;
+
+  if (!transport_write_string(fd_in, "MAIL FROM:<%s>%s", return_path, eol))
+    goto END_WRITE;
+
+  for (a = addr; a != NULL; a = a->next)
+    {
+    if (!transport_write_string(fd_in,
+        "RCPT TO:<%s>%s",
+        transport_rcpt_address(a, tblock->rcpt_include_affixes),
+        eol))
+      goto END_WRITE;
+    }
+
+  if (!transport_write_string(fd_in, "DATA%s", eol)) goto END_WRITE;
+  }
+
+/* Now the actual message - the options were set at initialization time */
+
+if (!transport_write_message(addr, fd_in, ob->options, 0, tblock->add_headers,
+  tblock->remove_headers, ob->check_string, ob->escape_string,
+  tblock->rewrite_rules, tblock->rewrite_existflags))
+    goto END_WRITE;
+
+/* Now any configured suffix */
+
+if (ob->message_suffix != NULL)
+  {
+  uschar *suffix = expand_string(ob->message_suffix);
+  if (suffix == NULL)
+    {
+    addr->transport_return = search_find_defer? DEFER : PANIC;
+    addr->message = string_sprintf("Expansion of \"%s\" (suffix for %s "
+      "transport) failed: %s", ob->message_suffix, tblock->name,
+      expand_string_message);
+    return FALSE;
+    }
+  if (!transport_write_block(fd_in, suffix, Ustrlen(suffix)))
+    goto END_WRITE;
+  }
+
+/* If local_smtp, write the terminating dot. */
+
+if (ob->use_bsmtp && !transport_write_string(fd_in, ".%s", eol))
+  goto END_WRITE;
+
+/* Flag all writing completed successfully. */
+
+written_ok = TRUE;
+
+/* Come here if there are errors during writing. */
+
+END_WRITE:
+
+/* OK, the writing is now all done. Close the pipe. */
+
+(void) close(fd_in);
+
+/* Handle errors during writing. For timeouts, set the timeout for waiting for
+the child process to 1 second. If the process at the far end of the pipe died
+without reading all of it, we expect an EPIPE error, which should be ignored.
+We used also to ignore WRITEINCOMPLETE but the writing function is now cleverer
+at handling OS where the death of a pipe doesn't give EPIPE immediately. See
+comments therein. */
+
+if (!written_ok)
+  {
+  if (errno == ETIMEDOUT)
+    {
+    addr->message = string_sprintf("%stimeout while writing to pipe",
+      transport_filter_timed_out? "transport filter " : "");
+    addr->transport_return = ob->timeout_defer? DEFER : FAIL;
+    timeout = 1;
+    }
+  else if (errno == EPIPE)
+    {
+    debug_printf("transport error EPIPE ignored\n");
+    }
+  else
+    {
+    addr->transport_return = PANIC;
+    addr->basic_errno = errno;
+    if (errno == ERRNO_CHHEADER_FAIL)
+      addr->message =
+        string_sprintf("Failed to expand headers_add or headers_remove: %s",
+          expand_string_message);
+    else if (errno == ERRNO_FILTER_FAIL)
+      addr->message = string_sprintf("Transport filter process failed (%d)%s",
+      addr->more_errno,
+      (addr->more_errno == EX_EXECFAILED)? ": unable to execute command" : "");
+    else if (errno == ERRNO_WRITEINCOMPLETE)
+      addr->message = string_sprintf("Failed repeatedly to write data");
+    else
+      addr->message = string_sprintf("Error %d", errno);
+    return FALSE;
+    }
+  }
+
+/* Wait for the child process to complete and take action if the returned
+status is nonzero. The timeout will be just 1 second if any of the writes
+above timed out. */
+
+if ((rc = child_close(pid, timeout)) != 0)
+  {
+  uschar *tmsg = (addr->message == NULL)? US"" :
+    string_sprintf(" (preceded by %s)", addr->message);
+
+  /* The process did not complete in time; kill its process group and fail
+  the delivery. It appears to be necessary to kill the output process too, as
+  otherwise it hangs on for some time if the actual pipe process is sleeping.
+  (At least, that's what I observed on Solaris 2.5.1.) Since we are failing
+  the delivery, that shouldn't cause any problem. */
+
+  if (rc == -256)
+    {
+    killpg(pid, SIGKILL);
+    kill(outpid, SIGKILL);
+    addr->transport_return = ob->timeout_defer? DEFER : FAIL;
+    addr->message = string_sprintf("pipe delivery process timed out%s", tmsg);
+    }
+
+  /* Wait() failed. */
+
+  else if (rc == -257)
+    {
+    addr->transport_return = PANIC;
+    addr->message = string_sprintf("Wait() failed for child process of %s "
+      "transport: %s%s", tblock->name, strerror(errno), tmsg);
+    }
+
+  /* Since the transport_filter timed out we assume it has sent the child process
+  a malformed or incomplete data stream.  Kill off the child process
+  and prevent checking its exit status as it will has probably exited in error.
+  This prevents the transport_filter timeout message from getting overwritten
+  by the exit error which is not the cause of the problem. */
+
+  else if (transport_filter_timed_out)
+    {
+    killpg(pid, SIGKILL);
+    kill(outpid, SIGKILL);
+    }
+
+  /* Either the process completed, but yielded a non-zero (necessarily
+  positive) status, or the process was terminated by a signal (rc will contain
+  the negation of the signal number). Treat killing by signal as failure unless
+  status is being ignored. By default, the message is bounced back, unless
+  freeze_signal is set, in which case it is frozen instead. */
+
+  else if (rc < 0)
+    {
+    if (ob->freeze_signal)
+      {
+      addr->transport_return = DEFER;
+      addr->special_action = SPECIAL_FREEZE;
+      addr->message = string_sprintf("Child process of %s transport (running "
+        "command \"%s\") was terminated by signal %d (%s)%s", tblock->name, cmd,
+        -rc, os_strsignal(-rc), tmsg);
+      }
+    else if (!ob->ignore_status)
+      {
+      addr->transport_return = FAIL;
+      addr->message = string_sprintf("Child process of %s transport (running "
+        "command \"%s\") was terminated by signal %d (%s)%s", tblock->name, cmd,
+        -rc, os_strsignal(-rc), tmsg);
+      }
+    }
+
+  /* For positive values (process terminated with non-zero status), we need a
+  status code to request deferral. A number of systems contain the following
+  line in sysexits.h:
+
+      #define EX_TEMPFAIL 75
+
+  with the description
+
+      EX_TEMPFAIL -- temporary failure, indicating something that
+         is not really an error.  In sendmail, this means
+         that a mailer (e.g.) could not create a connection,
+         and the request should be reattempted later.
+
+  Based on this, we use exit code EX_TEMPFAIL as a default to mean "defer" when
+  not ignoring the returned status. However, there is now an option that
+  contains a list of temporary codes, with TEMPFAIL and CANTCREAT as defaults.
+
+  Another case that needs special treatment is if execve() failed (typically
+  the command that was given is a non-existent path). By default this is
+  treated as just another failure, but if freeze_exec_fail is set, the reaction
+  is to freeze the message rather than bounce the address. Exim used to signal
+  this failure with EX_UNAVAILABLE, which is definined in many systems as
+
+      #define EX_UNAVAILABLE  69
+
+  with the description
+
+      EX_UNAVAILABLE -- A service is unavailable.  This can occur
+            if a support program or file does not exist.  This
+            can also be used as a catchall message when something
+            you wanted to do doesn't work, but you don't know why.
+
+  However, this can be confused with a command that actually returns 69 because
+  something *it* wanted is unavailable. At release 4.21, Exim was changed to
+  use return code 127 instead, because this is what the shell returns when it
+  is unable to exec a command. We define it as EX_EXECFAILED, and use it in
+  child.c to signal execve() failure and other unexpected failures such as
+  setuid() not working - though that won't be the case here because we aren't
+  changing uid. */
+
+  else
+    {
+    /* Always handle execve() failure specially if requested to */
+
+    if (ob->freeze_exec_fail && (rc == EX_EXECFAILED))
+      {
+      addr->transport_return = DEFER;
+      addr->special_action = SPECIAL_FREEZE;
+      addr->message = string_sprintf("pipe process failed to exec \"%s\"%s",
+        cmd, tmsg);
+      }
+
+    /* Otherwise take action only if not ignoring status */
+
+    else if (!ob->ignore_status)
+      {
+      uschar *ss;
+      int size, ptr, i;
+
+      /* If temp_errors is "*" all codes are temporary. Initializion checks
+      that it's either "*" or a list of numbers. If not "*", scan the list of
+      temporary failure codes; if any match, the result is DEFER. */
+
+      if (ob->temp_errors[0] == '*')
+        addr->transport_return = DEFER;
+
+      else
+        {
+        uschar *s = ob->temp_errors;
+        uschar *p;
+        uschar buffer[64];
+        int sep = 0;
+
+        addr->transport_return = FAIL;
+        while ((p = string_nextinlist(&s,&sep,buffer,sizeof(buffer))) != NULL)
+          {
+          if (rc == Uatoi(p)) { addr->transport_return = DEFER; break; }
+          }
+        }
+
+      /* Ensure the message contains the expanded command and arguments. This
+      doesn't have to be brilliantly efficient - it is an error situation. */
+
+      addr->message = string_sprintf("Child process of %s transport returned "
+        "%d", tblock->name, rc);
+
+      ptr = Ustrlen(addr->message);
+      size = ptr + 1;
+
+      /* If the return code is > 128, it often means that a shell command
+      was terminated by a signal. */
+
+      ss = (rc > 128)?
+        string_sprintf("(could mean shell command ended by signal %d (%s))",
+          rc-128, os_strsignal(rc-128)) :
+        US os_strexit(rc);
+
+      if (*ss != 0)
+        {
+        addr->message = string_cat(addr->message, &size, &ptr, US" ", 1);
+        addr->message = string_cat(addr->message, &size, &ptr,
+          ss, Ustrlen(ss));
+        }
+
+      /* Now add the command and arguments */
+
+      addr->message = string_cat(addr->message, &size, &ptr,
+        US" from command:", 14);
+
+      for (i = 0; i < sizeof(argv)/sizeof(int *) && argv[i] != NULL; i++)
+        {
+        BOOL quote = FALSE;
+        addr->message = string_cat(addr->message, &size, &ptr, US" ", 1);
+        if (Ustrpbrk(argv[i], " \t") != NULL)
+          {
+          quote = TRUE;
+          addr->message = string_cat(addr->message, &size, &ptr, US"\"", 1);
+          }
+        addr->message = string_cat(addr->message, &size, &ptr, argv[i],
+          Ustrlen(argv[i]));
+        if (quote)
+          addr->message = string_cat(addr->message, &size, &ptr, US"\"", 1);
+        }
+
+      /* Add previous filter timeout message, if present. */
+
+      if (*tmsg != 0)
+        addr->message = string_cat(addr->message, &size, &ptr, tmsg,
+          Ustrlen(tmsg));
+
+      addr->message[ptr] = 0;  /* Ensure concatenated string terminated */
+      }
+    }
+  }
+
+/* Ensure all subprocesses (in particular, the output handling process)
+are complete before we pass this point. */
+
+while (wait(&rc) >= 0);
+
+DEBUG(D_transport) debug_printf("%s transport yielded %d\n", tblock->name,
+  addr->transport_return);
+
+/* If there has been a problem, the message in addr->message contains details
+of the pipe command. We don't want to expose these to the world, so we set up
+something bland to return to the sender. */
+
+if (addr->transport_return != OK)
+  addr->user_message = US"local delivery failed";
+
+return FALSE;
+}
+
+/* End of transport/pipe.c */
diff --git a/src/transports/pipe.h b/src/transports/pipe.h
new file mode 100644 (file)
index 0000000..ed5c142
--- /dev/null
@@ -0,0 +1,51 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2014 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Private structure for the private options. */
+
+typedef struct {
+  uschar *cmd;
+  uschar *allow_commands;
+  uschar *environment;
+  uschar *path;
+  uschar *message_prefix;
+  uschar *message_suffix;
+  uschar *temp_errors;
+  uschar *check_string;
+  uschar *escape_string;
+  int   umask;
+  int   max_output;
+  int   timeout;
+  int   options;
+  BOOL  force_command;
+  BOOL  freeze_exec_fail;
+  BOOL  freeze_signal;
+  BOOL  ignore_status;
+  BOOL  permit_coredump;
+  BOOL  restrict_to_path;
+  BOOL  timeout_defer;
+  BOOL  use_shell;
+  BOOL  use_bsmtp;
+  BOOL  use_classresources;
+  BOOL  use_crlf;
+} pipe_transport_options_block;
+
+/* Data for reading the private options. */
+
+extern optionlist pipe_transport_options[];
+extern int pipe_transport_options_count;
+
+/* Block containing default values. */
+
+extern pipe_transport_options_block pipe_transport_option_defaults;
+
+/* The main and init entry points for the transport */
+
+extern BOOL pipe_transport_entry(transport_instance *, address_item *);
+extern void pipe_transport_init(transport_instance *);
+
+/* End of transports/pipe.h */
diff --git a/src/transports/smtp.c b/src/transports/smtp.c
new file mode 100644 (file)
index 0000000..40eebe8
--- /dev/null
@@ -0,0 +1,3556 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2014 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+#include "../exim.h"
+#include "smtp.h"
+
+#define PENDING          256
+#define PENDING_DEFER   (PENDING + DEFER)
+#define PENDING_OK      (PENDING + OK)
+
+
+/* Options specific to the smtp transport. This transport also supports LMTP
+over TCP/IP. The options must be in alphabetic order (note that "_" comes
+before the lower case letters). Some live in the transport_instance block so as
+to be publicly visible; these are flagged with opt_public. */
+
+optionlist smtp_transport_options[] = {
+  { "address_retry_include_sender", opt_bool,
+      (void *)offsetof(smtp_transport_options_block, address_retry_include_sender) },
+  { "allow_localhost",      opt_bool,
+      (void *)offsetof(smtp_transport_options_block, allow_localhost) },
+  { "authenticated_sender", opt_stringptr,
+      (void *)offsetof(smtp_transport_options_block, authenticated_sender) },
+  { "authenticated_sender_force", opt_bool,
+      (void *)offsetof(smtp_transport_options_block, authenticated_sender_force) },
+  { "command_timeout",      opt_time,
+      (void *)offsetof(smtp_transport_options_block, command_timeout) },
+  { "connect_timeout",      opt_time,
+      (void *)offsetof(smtp_transport_options_block, connect_timeout) },
+  { "connection_max_messages", opt_int | opt_public,
+      (void *)offsetof(transport_instance, connection_max_messages) },
+  { "data_timeout",         opt_time,
+      (void *)offsetof(smtp_transport_options_block, data_timeout) },
+  { "delay_after_cutoff", opt_bool,
+      (void *)offsetof(smtp_transport_options_block, delay_after_cutoff) },
+#ifndef DISABLE_DKIM
+  { "dkim_canon", opt_stringptr,
+      (void *)offsetof(smtp_transport_options_block, dkim_canon) },
+  { "dkim_domain", opt_stringptr,
+      (void *)offsetof(smtp_transport_options_block, dkim_domain) },
+  { "dkim_private_key", opt_stringptr,
+      (void *)offsetof(smtp_transport_options_block, dkim_private_key) },
+  { "dkim_selector", opt_stringptr,
+      (void *)offsetof(smtp_transport_options_block, dkim_selector) },
+  { "dkim_sign_headers", opt_stringptr,
+      (void *)offsetof(smtp_transport_options_block, dkim_sign_headers) },
+  { "dkim_strict", opt_stringptr,
+      (void *)offsetof(smtp_transport_options_block, dkim_strict) },
+#endif
+  { "dns_qualify_single",   opt_bool,
+      (void *)offsetof(smtp_transport_options_block, dns_qualify_single) },
+  { "dns_search_parents",   opt_bool,
+      (void *)offsetof(smtp_transport_options_block, dns_search_parents) },
+  { "dnssec_request_domains", opt_stringptr,
+      (void *)offsetof(smtp_transport_options_block, dnssec_request_domains) },
+  { "dnssec_require_domains", opt_stringptr,
+      (void *)offsetof(smtp_transport_options_block, dnssec_require_domains) },
+  { "dscp",                 opt_stringptr,
+      (void *)offsetof(smtp_transport_options_block, dscp) },
+  { "fallback_hosts",       opt_stringptr,
+      (void *)offsetof(smtp_transport_options_block, fallback_hosts) },
+  { "final_timeout",        opt_time,
+      (void *)offsetof(smtp_transport_options_block, final_timeout) },
+  { "gethostbyname",        opt_bool,
+      (void *)offsetof(smtp_transport_options_block, gethostbyname) },
+#ifdef SUPPORT_TLS
+  /* These are no longer honoured, as of Exim 4.80; for now, we silently
+  ignore; 4.83 will warn, and a later-still release will remove
+  these options, so that using them becomes an error. */
+  { "gnutls_require_kx",    opt_stringptr,
+      (void *)offsetof(smtp_transport_options_block, gnutls_require_kx) },
+  { "gnutls_require_mac",   opt_stringptr,
+      (void *)offsetof(smtp_transport_options_block, gnutls_require_mac) },
+  { "gnutls_require_protocols", opt_stringptr,
+      (void *)offsetof(smtp_transport_options_block, gnutls_require_proto) },
+#endif
+  { "helo_data",            opt_stringptr,
+      (void *)offsetof(smtp_transport_options_block, helo_data) },
+  { "hosts",                opt_stringptr,
+      (void *)offsetof(smtp_transport_options_block, hosts) },
+  { "hosts_avoid_esmtp",    opt_stringptr,
+      (void *)offsetof(smtp_transport_options_block, hosts_avoid_esmtp) },
+  { "hosts_avoid_pipelining", opt_stringptr,
+      (void *)offsetof(smtp_transport_options_block, hosts_avoid_pipelining) },
+#ifdef SUPPORT_TLS
+  { "hosts_avoid_tls",      opt_stringptr,
+      (void *)offsetof(smtp_transport_options_block, hosts_avoid_tls) },
+#endif
+  { "hosts_max_try",        opt_int,
+      (void *)offsetof(smtp_transport_options_block, hosts_max_try) },
+  { "hosts_max_try_hardlimit", opt_int,
+      (void *)offsetof(smtp_transport_options_block, hosts_max_try_hardlimit) },
+#ifdef SUPPORT_TLS
+  { "hosts_nopass_tls",     opt_stringptr,
+      (void *)offsetof(smtp_transport_options_block, hosts_nopass_tls) },
+#endif
+  { "hosts_override",       opt_bool,
+      (void *)offsetof(smtp_transport_options_block, hosts_override) },
+  { "hosts_randomize",      opt_bool,
+      (void *)offsetof(smtp_transport_options_block, hosts_randomize) },
+#if defined(SUPPORT_TLS) && !defined(DISABLE_OCSP)
+  { "hosts_request_ocsp",   opt_stringptr,
+      (void *)offsetof(smtp_transport_options_block, hosts_request_ocsp) },
+#endif
+  { "hosts_require_auth",   opt_stringptr,
+      (void *)offsetof(smtp_transport_options_block, hosts_require_auth) },
+#ifdef SUPPORT_TLS
+# ifndef DISABLE_OCSP
+  { "hosts_require_ocsp",   opt_stringptr,
+      (void *)offsetof(smtp_transport_options_block, hosts_require_ocsp) },
+# endif
+  { "hosts_require_tls",    opt_stringptr,
+      (void *)offsetof(smtp_transport_options_block, hosts_require_tls) },
+#endif
+  { "hosts_try_auth",       opt_stringptr,
+      (void *)offsetof(smtp_transport_options_block, hosts_try_auth) },
+#ifndef DISABLE_PRDR
+  { "hosts_try_prdr",       opt_stringptr,
+      (void *)offsetof(smtp_transport_options_block, hosts_try_prdr) },
+#endif
+#ifdef SUPPORT_TLS
+  { "hosts_verify_avoid_tls", opt_stringptr,
+      (void *)offsetof(smtp_transport_options_block, hosts_verify_avoid_tls) },
+#endif
+  { "interface",            opt_stringptr,
+      (void *)offsetof(smtp_transport_options_block, interface) },
+  { "keepalive",            opt_bool,
+      (void *)offsetof(smtp_transport_options_block, keepalive) },
+  { "lmtp_ignore_quota",    opt_bool,
+      (void *)offsetof(smtp_transport_options_block, lmtp_ignore_quota) },
+  { "max_rcpt",             opt_int | opt_public,
+      (void *)offsetof(transport_instance, max_addresses) },
+  { "multi_domain",         opt_bool | opt_public,
+      (void *)offsetof(transport_instance, multi_domain) },
+  { "port",                 opt_stringptr,
+      (void *)offsetof(smtp_transport_options_block, port) },
+  { "protocol",             opt_stringptr,
+      (void *)offsetof(smtp_transport_options_block, protocol) },
+  { "retry_include_ip_address", opt_bool,
+      (void *)offsetof(smtp_transport_options_block, retry_include_ip_address) },
+  { "serialize_hosts",      opt_stringptr,
+      (void *)offsetof(smtp_transport_options_block, serialize_hosts) },
+  { "size_addition",        opt_int,
+      (void *)offsetof(smtp_transport_options_block, size_addition) }
+#ifdef SUPPORT_TLS
+ ,{ "tls_certificate",      opt_stringptr,
+      (void *)offsetof(smtp_transport_options_block, tls_certificate) },
+  { "tls_crl",              opt_stringptr,
+      (void *)offsetof(smtp_transport_options_block, tls_crl) },
+  { "tls_dh_min_bits",      opt_int,
+      (void *)offsetof(smtp_transport_options_block, tls_dh_min_bits) },
+  { "tls_privatekey",       opt_stringptr,
+      (void *)offsetof(smtp_transport_options_block, tls_privatekey) },
+  { "tls_require_ciphers",  opt_stringptr,
+      (void *)offsetof(smtp_transport_options_block, tls_require_ciphers) },
+  { "tls_sni",              opt_stringptr,
+      (void *)offsetof(smtp_transport_options_block, tls_sni) },
+  { "tls_tempfail_tryclear", opt_bool,
+      (void *)offsetof(smtp_transport_options_block, tls_tempfail_tryclear) },
+  { "tls_try_verify_hosts", opt_stringptr,
+      (void *)offsetof(smtp_transport_options_block, tls_try_verify_hosts) },
+#ifdef EXPERIMENTAL_CERTNAMES
+  { "tls_verify_cert_hostnames", opt_stringptr,
+      (void *)offsetof(smtp_transport_options_block,tls_verify_cert_hostnames)},
+#endif
+  { "tls_verify_certificates", opt_stringptr,
+      (void *)offsetof(smtp_transport_options_block, tls_verify_certificates) },
+  { "tls_verify_hosts",     opt_stringptr,
+      (void *)offsetof(smtp_transport_options_block, tls_verify_hosts) }
+#endif
+#ifdef EXPERIMENTAL_TPDA
+ ,{ "tpda_host_defer_action", opt_stringptr,
+      (void *)offsetof(smtp_transport_options_block, tpda_host_defer_action) },
+#endif
+};
+
+/* Size of the options list. An extern variable has to be used so that its
+address can appear in the tables drtables.c. */
+
+int smtp_transport_options_count =
+  sizeof(smtp_transport_options)/sizeof(optionlist);
+
+/* Default private options block for the smtp transport. */
+
+smtp_transport_options_block smtp_transport_option_defaults = {
+  NULL,                /* hosts */
+  NULL,                /* fallback_hosts */
+  NULL,                /* hostlist */
+  NULL,                /* fallback_hostlist */
+  NULL,                /* authenticated_sender */
+  US"$primary_hostname", /* helo_data */
+  NULL,                /* interface */
+  NULL,                /* port */
+  US"smtp",            /* protocol */
+  NULL,                /* DSCP */
+  NULL,                /* serialize_hosts */
+  NULL,                /* hosts_try_auth */
+  NULL,                /* hosts_require_auth */
+#ifndef DISABLE_PRDR
+  NULL,                /* hosts_try_prdr */
+#endif
+#ifndef DISABLE_OCSP
+  US"*",               /* hosts_request_ocsp */
+  NULL,                /* hosts_require_ocsp */
+#endif
+  NULL,                /* hosts_require_tls */
+  NULL,                /* hosts_avoid_tls */
+  US"*",               /* hosts_verify_avoid_tls */
+  NULL,                /* hosts_avoid_pipelining */
+  NULL,                /* hosts_avoid_esmtp */
+  NULL,                /* hosts_nopass_tls */
+  5*60,                /* command_timeout */
+  5*60,                /* connect_timeout; shorter system default overrides */
+  5*60,                /* data timeout */
+  10*60,               /* final timeout */
+  1024,                /* size_addition */
+  5,                   /* hosts_max_try */
+  50,                  /* hosts_max_try_hardlimit */
+  TRUE,                /* address_retry_include_sender */
+  FALSE,               /* allow_localhost */
+  FALSE,               /* authenticated_sender_force */
+  FALSE,               /* gethostbyname */
+  TRUE,                /* dns_qualify_single */
+  FALSE,               /* dns_search_parents */
+  NULL,                /* dnssec_request_domains */
+  NULL,                /* dnssec_require_domains */
+  TRUE,                /* delay_after_cutoff */
+  FALSE,               /* hosts_override */
+  FALSE,               /* hosts_randomize */
+  TRUE,                /* keepalive */
+  FALSE,               /* lmtp_ignore_quota */
+  TRUE                 /* retry_include_ip_address */
+#ifdef SUPPORT_TLS
+ ,NULL,                /* tls_certificate */
+  NULL,                /* tls_crl */
+  NULL,                /* tls_privatekey */
+  NULL,                /* tls_require_ciphers */
+  NULL,                /* gnutls_require_kx */
+  NULL,                /* gnutls_require_mac */
+  NULL,                /* gnutls_require_proto */
+  NULL,                /* tls_sni */
+  NULL,                /* tls_verify_certificates */
+  EXIM_CLIENT_DH_DEFAULT_MIN_BITS,
+                       /* tls_dh_min_bits */
+  TRUE,                /* tls_tempfail_tryclear */
+  NULL,                /* tls_verify_hosts */
+  NULL                 /* tls_try_verify_hosts */
+# ifdef EXPERIMENTAL_CERTNAMES
+ ,NULL                 /* tls_verify_cert_hostnames */
+# endif
+#endif
+#ifndef DISABLE_DKIM
+ ,NULL,                /* dkim_canon */
+  NULL,                /* dkim_domain */
+  NULL,                /* dkim_private_key */
+  NULL,                /* dkim_selector */
+  NULL,                /* dkim_sign_headers */
+  NULL                 /* dkim_strict */
+#endif
+#ifdef EXPERIMENTAL_TPDA
+ ,NULL                 /* tpda_host_defer_action */
+#endif
+};
+
+#ifdef EXPERIMENTAL_DSN
+/* some DSN flags for use later */
+
+static int     rf_list[] = {rf_notify_never, rf_notify_success,
+                            rf_notify_failure, rf_notify_delay };
+
+static uschar *rf_names[] = { "NEVER", "SUCCESS", "FAILURE", "DELAY" };
+#endif
+
+
+
+/* Local statics */
+
+static uschar *smtp_command;   /* Points to last cmd for error messages */
+static uschar *mail_command;   /* Points to MAIL cmd for error messages */
+static BOOL    update_waiting; /* TRUE to update the "wait" database */
+
+
+/*************************************************
+*             Setup entry point                  *
+*************************************************/
+
+/* This function is called when the transport is about to be used,
+but before running it in a sub-process. It is used for two things:
+
+  (1) To set the fallback host list in addresses, when delivering.
+  (2) To pass back the interface, port, protocol, and other options, for use
+      during callout verification.
+
+Arguments:
+  tblock    pointer to the transport instance block
+  addrlist  list of addresses about to be transported
+  tf        if not NULL, pointer to block in which to return options
+  uid       the uid that will be set (not used)
+  gid       the gid that will be set (not used)
+  errmsg    place for error message (not used)
+
+Returns:  OK always (FAIL, DEFER not used)
+*/
+
+static int
+smtp_transport_setup(transport_instance *tblock, address_item *addrlist,
+  transport_feedback *tf, uid_t uid, gid_t gid, uschar **errmsg)
+{
+smtp_transport_options_block *ob =
+  (smtp_transport_options_block *)(tblock->options_block);
+
+errmsg = errmsg;    /* Keep picky compilers happy */
+uid = uid;
+gid = gid;
+
+/* Pass back options if required. This interface is getting very messy. */
+
+if (tf != NULL)
+  {
+  tf->interface = ob->interface;
+  tf->port = ob->port;
+  tf->protocol = ob->protocol;
+  tf->hosts = ob->hosts;
+  tf->hosts_override = ob->hosts_override;
+  tf->hosts_randomize = ob->hosts_randomize;
+  tf->gethostbyname = ob->gethostbyname;
+  tf->qualify_single = ob->dns_qualify_single;
+  tf->search_parents = ob->dns_search_parents;
+  tf->helo_data = ob->helo_data;
+  }
+
+/* Set the fallback host list for all the addresses that don't have fallback
+host lists, provided that the local host wasn't present in the original host
+list. */
+
+if (!testflag(addrlist, af_local_host_removed))
+  {
+  for (; addrlist != NULL; addrlist = addrlist->next)
+    if (addrlist->fallback_hosts == NULL)
+      addrlist->fallback_hosts = ob->fallback_hostlist;
+  }
+
+return OK;
+}
+
+
+
+/*************************************************
+*          Initialization entry point            *
+*************************************************/
+
+/* Called for each instance, after its options have been read, to
+enable consistency checks to be done, or anything else that needs
+to be set up.
+
+Argument:   pointer to the transport instance block
+Returns:    nothing
+*/
+
+void
+smtp_transport_init(transport_instance *tblock)
+{
+smtp_transport_options_block *ob =
+  (smtp_transport_options_block *)(tblock->options_block);
+
+/* Retry_use_local_part defaults FALSE if unset */
+
+if (tblock->retry_use_local_part == TRUE_UNSET)
+  tblock->retry_use_local_part = FALSE;
+
+/* Set the default port according to the protocol */
+
+if (ob->port == NULL)
+  ob->port = (strcmpic(ob->protocol, US"lmtp") == 0)? US"lmtp" :
+    (strcmpic(ob->protocol, US"smtps") == 0)? US"smtps" : US"smtp";
+
+/* Set up the setup entry point, to be called before subprocesses for this
+transport. */
+
+tblock->setup = smtp_transport_setup;
+
+/* Complain if any of the timeouts are zero. */
+
+if (ob->command_timeout <= 0 || ob->data_timeout <= 0 ||
+    ob->final_timeout <= 0)
+  log_write(0, LOG_PANIC_DIE|LOG_CONFIG,
+    "command, data, or final timeout value is zero for %s transport",
+      tblock->name);
+
+/* If hosts_override is set and there are local hosts, set the global
+flag that stops verify from showing router hosts. */
+
+if (ob->hosts_override && ob->hosts != NULL) tblock->overrides_hosts = TRUE;
+
+/* If there are any fallback hosts listed, build a chain of host items
+for them, but do not do any lookups at this time. */
+
+host_build_hostlist(&(ob->fallback_hostlist), ob->fallback_hosts, FALSE);
+
+#ifdef SUPPORT_TLS
+if (  ob->gnutls_require_kx
+   || ob->gnutls_require_mac
+   || ob->gnutls_require_proto)
+  log_write(0, LOG_MAIN, "WARNING: smtp transport options"
+    " gnutls_require_kx, gnutls_require_mac and gnutls_require_protocols"
+    " are obsolete\n");
+#endif
+}
+
+
+
+
+
+/*************************************************
+*   Set delivery info into all active addresses  *
+*************************************************/
+
+/* Only addresses whose status is >= PENDING are relevant. A lesser
+status means that an address is not currently being processed.
+
+Arguments:
+  addrlist       points to a chain of addresses
+  errno_value    to put in each address's errno field
+  msg            to put in each address's message field
+  rc             to put in each address's transport_return field
+  pass_message   if TRUE, set the "pass message" flag in the address
+
+If errno_value has the special value ERRNO_CONNECTTIMEOUT, ETIMEDOUT is put in
+the errno field, and RTEF_CTOUT is ORed into the more_errno field, to indicate
+this particular type of timeout.
+
+Returns:       nothing
+*/
+
+static void
+set_errno(address_item *addrlist, int errno_value, uschar *msg, int rc,
+  BOOL pass_message)
+{
+address_item *addr;
+int orvalue = 0;
+if (errno_value == ERRNO_CONNECTTIMEOUT)
+  {
+  errno_value = ETIMEDOUT;
+  orvalue = RTEF_CTOUT;
+  }
+for (addr = addrlist; addr != NULL; addr = addr->next)
+  {
+  if (addr->transport_return < PENDING) continue;
+  addr->basic_errno = errno_value;
+  addr->more_errno |= orvalue;
+  if (msg != NULL)
+    {
+    addr->message = msg;
+    if (pass_message) setflag(addr, af_pass_message);
+    }
+  addr->transport_return = rc;
+  }
+}
+
+
+
+/*************************************************
+*          Check an SMTP response                *
+*************************************************/
+
+/* This function is given an errno code and the SMTP response buffer
+to analyse, together with the host identification for generating messages. It
+sets an appropriate message and puts the first digit of the response code into
+the yield variable. If no response was actually read, a suitable digit is
+chosen.
+
+Arguments:
+  host           the current host, to get its name for messages
+  errno_value    pointer to the errno value
+  more_errno     from the top address for use with ERRNO_FILTER_FAIL
+  buffer         the SMTP response buffer
+  yield          where to put a one-digit SMTP response code
+  message        where to put an errror message
+  pass_message   set TRUE if message is an SMTP response
+
+Returns:         TRUE if an SMTP "QUIT" command should be sent, else FALSE
+*/
+
+static BOOL check_response(host_item *host, int *errno_value, int more_errno,
+  uschar *buffer, int *yield, uschar **message, BOOL *pass_message)
+{
+uschar *pl = US"";
+
+if (smtp_use_pipelining &&
+    (Ustrcmp(smtp_command, "MAIL") == 0 ||
+     Ustrcmp(smtp_command, "RCPT") == 0 ||
+     Ustrcmp(smtp_command, "DATA") == 0))
+  pl = US"pipelined ";
+
+*yield = '4';    /* Default setting is to give a temporary error */
+
+/* Handle response timeout */
+
+if (*errno_value == ETIMEDOUT)
+  {
+  *message = US string_sprintf("SMTP timeout while connected to %s [%s] "
+    "after %s%s", host->name, host->address, pl, smtp_command);
+  if (transport_count > 0)
+    *message = US string_sprintf("%s (%d bytes written)", *message,
+      transport_count);
+  return FALSE;
+  }
+
+/* Handle malformed SMTP response */
+
+if (*errno_value == ERRNO_SMTPFORMAT)
+  {
+  uschar *malfresp = string_printing(buffer);
+  while (isspace(*malfresp)) malfresp++;
+  if (*malfresp == 0)
+    *message = string_sprintf("Malformed SMTP reply (an empty line) from "
+      "%s [%s] in response to %s%s", host->name, host->address, pl,
+      smtp_command);
+  else
+    *message = string_sprintf("Malformed SMTP reply from %s [%s] in response "
+      "to %s%s: %s", host->name, host->address, pl, smtp_command, malfresp);
+  return FALSE;
+  }
+
+/* Handle a failed filter process error; can't send QUIT as we mustn't
+end the DATA. */
+
+if (*errno_value == ERRNO_FILTER_FAIL)
+  {
+  *message = US string_sprintf("transport filter process failed (%d)%s",
+    more_errno,
+    (more_errno == EX_EXECFAILED)? ": unable to execute command" : "");
+  return FALSE;
+  }
+
+/* Handle a failed add_headers expansion; can't send QUIT as we mustn't
+end the DATA. */
+
+if (*errno_value == ERRNO_CHHEADER_FAIL)
+  {
+  *message =
+    US string_sprintf("failed to expand headers_add or headers_remove: %s",
+      expand_string_message);
+  return FALSE;
+  }
+
+/* Handle failure to write a complete data block */
+
+if (*errno_value == ERRNO_WRITEINCOMPLETE)
+  {
+  *message = US string_sprintf("failed to write a data block");
+  return FALSE;
+  }
+
+/* Handle error responses from the remote mailer. */
+
+if (buffer[0] != 0)
+  {
+  uschar *s = string_printing(buffer);
+  *message = US string_sprintf("SMTP error from remote mail server after %s%s: "
+    "host %s [%s]: %s", pl, smtp_command, host->name, host->address, s);
+  *pass_message = TRUE;
+  *yield = buffer[0];
+  return TRUE;
+  }
+
+/* No data was read. If there is no errno, this must be the EOF (i.e.
+connection closed) case, which causes deferral. An explicit connection reset
+error has the same effect. Otherwise, put the host's identity in the message,
+leaving the errno value to be interpreted as well. In all cases, we have to
+assume the connection is now dead. */
+
+if (*errno_value == 0 || *errno_value == ECONNRESET)
+  {
+  *errno_value = ERRNO_SMTPCLOSED;
+  *message = US string_sprintf("Remote host %s [%s] closed connection "
+    "in response to %s%s", host->name, host->address, pl, smtp_command);
+  }
+else *message = US string_sprintf("%s [%s]", host->name, host->address);
+
+return FALSE;
+}
+
+
+
+/*************************************************
+*          Write error message to logs           *
+*************************************************/
+
+/* This writes to the main log and to the message log.
+
+Arguments:
+  addr     the address item containing error information
+  host     the current host
+
+Returns:   nothing
+*/
+
+static void
+write_logs(address_item *addr, host_item *host)
+{
+if (addr->message != NULL)
+  {
+  uschar *message = addr->message;
+  if (addr->basic_errno > 0)
+    message = string_sprintf("%s: %s", message, strerror(addr->basic_errno));
+  log_write(0, LOG_MAIN, "%s", message);
+  deliver_msglog("%s %s\n", tod_stamp(tod_log), message);
+  }
+else
+  {
+  uschar *msg =
+    ((log_extra_selector & LX_outgoing_port) != 0)?
+    string_sprintf("%s [%s]:%d", host->name, host->address,
+      (host->port == PORT_NONE)? 25 : host->port)
+    :
+    string_sprintf("%s [%s]", host->name, host->address);
+  log_write(0, LOG_MAIN, "%s %s", msg, strerror(addr->basic_errno));
+  deliver_msglog("%s %s %s\n", tod_stamp(tod_log), msg,
+    strerror(addr->basic_errno));
+  }
+}
+
+
+
+#ifdef EXPERIMENTAL_TPDA
+/*************************************************
+*   Post-defer action                            *
+*************************************************/
+
+/* This expands an arbitrary per-transport string.
+   It might, for example, be used to write to the database log.
+
+Arguments:
+  ob                    transport options block
+  addr                  the address item containing error information
+  host                  the current host
+
+Returns:   nothing
+*/
+
+static void
+tpda_deferred(smtp_transport_options_block *ob, address_item *addr, host_item *host)
+{
+uschar *action = ob->tpda_host_defer_action;
+if (!action)
+       return;
+
+tpda_delivery_ip =         string_copy(host->address);
+tpda_delivery_port =       (host->port == PORT_NONE)? 25 : host->port;
+tpda_delivery_fqdn =       string_copy(host->name);
+tpda_delivery_local_part = string_copy(addr->local_part);
+tpda_delivery_domain =     string_copy(addr->domain);
+tpda_defer_errno =         addr->basic_errno;
+
+tpda_defer_errstr = addr->message
+  ? addr->basic_errno > 0
+    ? string_sprintf("%s: %s", addr->message, strerror(addr->basic_errno))
+    : string_copy(addr->message)
+  : addr->basic_errno > 0
+    ? string_copy(US strerror(addr->basic_errno))
+    : NULL;
+
+DEBUG(D_transport)
+  debug_printf("  TPDA(host defer): tpda_host_defer_action=|%s| tpda_delivery_IP=%s\n",
+    action, tpda_delivery_ip);
+
+router_name =    addr->router->name;
+transport_name = addr->transport->name;
+if (!expand_string(action) && *expand_string_message)
+  log_write(0, LOG_MAIN|LOG_PANIC, "failed to expand tpda_defer_action in %s: %s\n",
+    transport_name, expand_string_message);
+router_name = transport_name = NULL;
+}
+#endif
+
+
+
+/*************************************************
+*           Synchronize SMTP responses           *
+*************************************************/
+
+/* This function is called from smtp_deliver() to receive SMTP responses from
+the server, and match them up with the commands to which they relate. When
+PIPELINING is not in use, this function is called after every command, and is
+therefore somewhat over-engineered, but it is simpler to use a single scheme
+that works both with and without PIPELINING instead of having two separate sets
+of code.
+
+The set of commands that are buffered up with pipelining may start with MAIL
+and may end with DATA; in between are RCPT commands that correspond to the
+addresses whose status is PENDING_DEFER. All other commands (STARTTLS, AUTH,
+etc.) are never buffered.
+
+Errors after MAIL or DATA abort the whole process leaving the response in the
+buffer. After MAIL, pending responses are flushed, and the original command is
+re-instated in big_buffer for error messages. For RCPT commands, the remote is
+permitted to reject some recipient addresses while accepting others. However
+certain errors clearly abort the whole process. Set the value in
+transport_return to PENDING_OK if the address is accepted. If there is a
+subsequent general error, it will get reset accordingly. If not, it will get
+converted to OK at the end.
+
+Arguments:
+  addrlist          the complete address list
+  include_affixes   TRUE if affixes include in RCPT
+  sync_addr         ptr to the ptr of the one to start scanning at (updated)
+  host              the host we are connected to
+  count             the number of responses to read
+  address_retry_
+    include_sender  true if 4xx retry is to include the sender it its key
+  pending_MAIL      true if the first response is for MAIL
+  pending_DATA      0 if last command sent was not DATA
+                   +1 if previously had a good recipient
+                   -1 if not previously had a good recipient
+  inblock           incoming SMTP block
+  timeout           timeout value
+  buffer            buffer for reading response
+  buffsize          size of buffer
+
+Returns:      3 if at least one address had 2xx and one had 5xx
+              2 if at least one address had 5xx but none had 2xx
+              1 if at least one host had a 2xx response, but none had 5xx
+              0 no address had 2xx or 5xx but no errors (all 4xx, or just DATA)
+             -1 timeout while reading RCPT response
+             -2 I/O or other non-response error for RCPT
+             -3 DATA or MAIL failed - errno and buffer set
+*/
+
+static int
+sync_responses(address_item *addrlist, BOOL include_affixes,
+  address_item **sync_addr, host_item *host, int count,
+  BOOL address_retry_include_sender, BOOL pending_MAIL,
+  int pending_DATA, smtp_inblock *inblock, int timeout, uschar *buffer,
+  int buffsize)
+{
+address_item *addr = *sync_addr;
+int yield = 0;
+
+/* Handle the response for a MAIL command. On error, reinstate the original
+command in big_buffer for error message use, and flush any further pending
+responses before returning, except after I/O errors and timeouts. */
+
+if (pending_MAIL)
+  {
+  count--;
+  if (!smtp_read_response(inblock, buffer, buffsize, '2', timeout))
+    {
+    Ustrcpy(big_buffer, mail_command);  /* Fits, because it came from there! */
+    if (errno == 0 && buffer[0] != 0)
+      {
+      uschar flushbuffer[4096];
+      int save_errno = 0;
+      if (buffer[0] == '4')
+        {
+        save_errno = ERRNO_MAIL4XX;
+        addr->more_errno |= ((buffer[1] - '0')*10 + buffer[2] - '0') << 8;
+        }
+      while (count-- > 0)
+        {
+        if (!smtp_read_response(inblock, flushbuffer, sizeof(flushbuffer),
+                   '2', timeout)
+            && (errno != 0 || flushbuffer[0] == 0))
+          break;
+        }
+      errno = save_errno;
+      }
+    return -3;
+    }
+  }
+
+if (pending_DATA) count--;  /* Number of RCPT responses to come */
+
+/* Read and handle the required number of RCPT responses, matching each one up
+with an address by scanning for the next address whose status is PENDING_DEFER.
+*/
+
+while (count-- > 0)
+  {
+  while (addr->transport_return != PENDING_DEFER) addr = addr->next;
+
+  /* The address was accepted */
+
+  if (smtp_read_response(inblock, buffer, buffsize, '2', timeout))
+    {
+    yield |= 1;
+    addr->transport_return = PENDING_OK;
+
+    /* If af_dr_retry_exists is set, there was a routing delay on this address;
+    ensure that any address-specific retry record is expunged. We do this both
+    for the basic key and for the version that also includes the sender. */
+
+    if (testflag(addr, af_dr_retry_exists))
+      {
+      uschar *altkey = string_sprintf("%s:<%s>", addr->address_retry_key,
+        sender_address);
+      retry_add_item(addr, altkey, rf_delete);
+      retry_add_item(addr, addr->address_retry_key, rf_delete);
+      }
+    }
+
+  /* Timeout while reading the response */
+
+  else if (errno == ETIMEDOUT)
+    {
+    int save_errno = errno;
+    uschar *message = string_sprintf("SMTP timeout while connected to %s [%s] "
+      "after RCPT TO:<%s>", host->name, host->address,
+      transport_rcpt_address(addr, include_affixes));
+    set_errno(addrlist, save_errno, message, DEFER, FALSE);
+    retry_add_item(addr, addr->address_retry_key, 0);
+    update_waiting = FALSE;
+    return -1;
+    }
+
+  /* Handle other errors in obtaining an SMTP response by returning -1. This
+  will cause all the addresses to be deferred. Restore the SMTP command in
+  big_buffer for which we are checking the response, so the error message
+  makes sense. */
+
+  else if (errno != 0 || buffer[0] == 0)
+    {
+    string_format(big_buffer, big_buffer_size, "RCPT TO:<%s>",
+      transport_rcpt_address(addr, include_affixes));
+    return -2;
+    }
+
+  /* Handle SMTP permanent and temporary response codes. */
+
+  else
+    {
+    addr->message =
+      string_sprintf("SMTP error from remote mail server after RCPT TO:<%s>: "
+        "host %s [%s]: %s", transport_rcpt_address(addr, include_affixes),
+        host->name, host->address, string_printing(buffer));
+    setflag(addr, af_pass_message);
+    deliver_msglog("%s %s\n", tod_stamp(tod_log), addr->message);
+
+    /* The response was 5xx */
+
+    if (buffer[0] == '5')
+      {
+      addr->transport_return = FAIL;
+      yield |= 2;
+      }
+
+    /* The response was 4xx */
+
+    else
+      {
+      addr->transport_return = DEFER;
+      addr->basic_errno = ERRNO_RCPT4XX;
+      addr->more_errno |= ((buffer[1] - '0')*10 + buffer[2] - '0') << 8;
+
+      /* Log temporary errors if there are more hosts to be tried. */
+
+      if (host->next != NULL) log_write(0, LOG_MAIN, "%s", addr->message);
+
+      /* Do not put this message on the list of those waiting for specific
+      hosts, as otherwise it is likely to be tried too often. */
+
+      update_waiting = FALSE;
+
+      /* Add a retry item for the address so that it doesn't get tried again
+      too soon. If address_retry_include_sender is true, add the sender address
+      to the retry key. */
+
+      if (address_retry_include_sender)
+        {
+        uschar *altkey = string_sprintf("%s:<%s>", addr->address_retry_key,
+          sender_address);
+        retry_add_item(addr, altkey, 0);
+        }
+      else retry_add_item(addr, addr->address_retry_key, 0);
+      }
+    }
+  }       /* Loop for next RCPT response */
+
+/* Update where to start at for the next block of responses, unless we
+have already handled all the addresses. */
+
+if (addr != NULL) *sync_addr = addr->next;
+
+/* Handle a response to DATA. If we have not had any good recipients, either
+previously or in this block, the response is ignored. */
+
+if (pending_DATA != 0 &&
+    !smtp_read_response(inblock, buffer, buffsize, '3', timeout))
+  {
+  int code;
+  uschar *msg;
+  BOOL pass_message;
+  if (pending_DATA > 0 || (yield & 1) != 0)
+    {
+    if (errno == 0 && buffer[0] == '4')
+      {
+      errno = ERRNO_DATA4XX;
+      addrlist->more_errno |= ((buffer[1] - '0')*10 + buffer[2] - '0') << 8;
+      }
+    return -3;
+    }
+  (void)check_response(host, &errno, 0, buffer, &code, &msg, &pass_message);
+  DEBUG(D_transport) debug_printf("%s\nerror for DATA ignored: pipelining "
+    "is in use and there were no good recipients\n", msg);
+  }
+
+/* All responses read and handled; MAIL (if present) received 2xx and DATA (if
+present) received 3xx. If any RCPTs were handled and yielded anything other
+than 4xx, yield will be set non-zero. */
+
+return yield;
+}
+
+
+
+/* Do the client side of smtp-level authentication */
+/*
+Arguments:
+  buffer       EHLO response from server (gets overwritten)
+  addrlist      chain of potential addresses to deliver
+  host          host to deliver to
+  ob           transport options
+  ibp, obp     comms channel control blocks
+
+Returns:
+  OK                   Success, or failed (but not required): global "smtp_authenticated" set
+  DEFER                        Failed authentication (and was required)
+  ERROR                        Internal problem
+
+  FAIL_SEND            Failed communications - transmit
+  FAIL                 - response
+*/
+
+int
+smtp_auth(uschar *buffer, unsigned bufsize, address_item *addrlist, host_item *host,
+    smtp_transport_options_block *ob, BOOL is_esmtp,
+    smtp_inblock *ibp, smtp_outblock *obp)
+{
+  int require_auth;
+  uschar *fail_reason = US"server did not advertise AUTH support";
+
+  smtp_authenticated = FALSE;
+  client_authenticator = client_authenticated_id = client_authenticated_sender = NULL;
+  require_auth = verify_check_this_host(&(ob->hosts_require_auth), NULL,
+    host->name, host->address, NULL);
+
+  if (is_esmtp && !regex_AUTH) regex_AUTH =
+      regex_must_compile(US"\\n250[\\s\\-]AUTH\\s+([\\-\\w\\s]+)(?:\\n|$)",
+            FALSE, TRUE);
+
+  if (is_esmtp && regex_match_and_setup(regex_AUTH, buffer, 0, -1))
+    {
+    uschar *names = string_copyn(expand_nstring[1], expand_nlength[1]);
+    expand_nmax = -1;                          /* reset */
+
+    /* Must not do this check until after we have saved the result of the
+    regex match above. */
+
+    if (require_auth == OK ||
+        verify_check_this_host(&(ob->hosts_try_auth), NULL, host->name,
+          host->address, NULL) == OK)
+      {
+      auth_instance *au;
+      fail_reason = US"no common mechanisms were found";
+
+      DEBUG(D_transport) debug_printf("scanning authentication mechanisms\n");
+
+      /* Scan the configured authenticators looking for one which is configured
+      for use as a client, which is not suppressed by client_condition, and
+      whose name matches an authentication mechanism supported by the server.
+      If one is found, attempt to authenticate by calling its client function.
+      */
+
+      for (au = auths; !smtp_authenticated && au != NULL; au = au->next)
+        {
+        uschar *p = names;
+        if (!au->client ||
+            (au->client_condition != NULL &&
+             !expand_check_condition(au->client_condition, au->name,
+               US"client authenticator")))
+          {
+          DEBUG(D_transport) debug_printf("skipping %s authenticator: %s\n",
+            au->name,
+            (au->client)? "client_condition is false" :
+                          "not configured as a client");
+          continue;
+          }
+
+        /* Loop to scan supported server mechanisms */
+
+        while (*p != 0)
+          {
+          int rc;
+          int len = Ustrlen(au->public_name);
+          while (isspace(*p)) p++;
+
+          if (strncmpic(au->public_name, p, len) != 0 ||
+              (p[len] != 0 && !isspace(p[len])))
+            {
+            while (*p != 0 && !isspace(*p)) p++;
+            continue;
+            }
+
+          /* Found data for a listed mechanism. Call its client entry. Set
+          a flag in the outblock so that data is overwritten after sending so
+          that reflections don't show it. */
+
+          fail_reason = US"authentication attempt(s) failed";
+          obp->authenticating = TRUE;
+          rc = (au->info->clientcode)(au, ibp, obp,
+            ob->command_timeout, buffer, bufsize);
+          obp->authenticating = FALSE;
+          DEBUG(D_transport) debug_printf("%s authenticator yielded %d\n",
+            au->name, rc);
+
+          /* A temporary authentication failure must hold up delivery to
+          this host. After a permanent authentication failure, we carry on
+          to try other authentication methods. If all fail hard, try to
+          deliver the message unauthenticated unless require_auth was set. */
+
+          switch(rc)
+            {
+            case OK:
+            smtp_authenticated = TRUE;   /* stops the outer loop */
+           client_authenticator = au->name;
+           if (au->set_client_id != NULL)
+             client_authenticated_id = expand_string(au->set_client_id);
+            break;
+
+            /* Failure after writing a command */
+
+            case FAIL_SEND:
+            return FAIL_SEND;
+
+            /* Failure after reading a response */
+
+            case FAIL:
+            if (errno != 0 || buffer[0] != '5') return FAIL;
+            log_write(0, LOG_MAIN, "%s authenticator failed H=%s [%s] %s",
+              au->name, host->name, host->address, buffer);
+            break;
+
+            /* Failure by some other means. In effect, the authenticator
+            decided it wasn't prepared to handle this case. Typically this
+            is the result of "fail" in an expansion string. Do we need to
+            log anything here? Feb 2006: a message is now put in the buffer
+            if logging is required. */
+
+            case CANCELLED:
+            if (*buffer != 0)
+              log_write(0, LOG_MAIN, "%s authenticator cancelled "
+                "authentication H=%s [%s] %s", au->name, host->name,
+                host->address, buffer);
+            break;
+
+            /* Internal problem, message in buffer. */
+
+            case ERROR:
+            set_errno(addrlist, 0, string_copy(buffer), DEFER, FALSE);
+            return ERROR;
+            }
+
+          break;  /* If not authenticated, try next authenticator */
+          }       /* Loop for scanning supported server mechanisms */
+        }         /* Loop for further authenticators */
+      }
+    }
+
+  /* If we haven't authenticated, but are required to, give up. */
+
+  if (require_auth == OK && !smtp_authenticated)
+    {
+    set_errno(addrlist, ERRNO_AUTHFAIL,
+      string_sprintf("authentication required but %s", fail_reason), DEFER,
+      FALSE);
+    return DEFER;
+    }
+
+  return OK;
+}
+
+
+/* Construct AUTH appendix string for MAIL TO */
+/*
+Arguments
+  buffer       to build string
+  addrlist      chain of potential addresses to deliver
+  ob           transport options
+
+Globals                smtp_authenticated
+               client_authenticated_sender
+Return True on error, otherwise buffer has (possibly empty) terminated string
+*/
+
+BOOL
+smtp_mail_auth_str(uschar *buffer, unsigned bufsize, address_item *addrlist,
+                   smtp_transport_options_block *ob)
+{
+uschar *local_authenticated_sender = authenticated_sender;
+
+#ifdef notdef
+  debug_printf("smtp_mail_auth_str: as<%s> os<%s> SA<%s>\n", authenticated_sender, ob->authenticated_sender, smtp_authenticated?"Y":"N");
+#endif
+
+if (ob->authenticated_sender != NULL)
+  {
+  uschar *new = expand_string(ob->authenticated_sender);
+  if (new == NULL)
+    {
+    if (!expand_string_forcedfail)
+      {
+      uschar *message = string_sprintf("failed to expand "
+        "authenticated_sender: %s", expand_string_message);
+      set_errno(addrlist, 0, message, DEFER, FALSE);
+      return TRUE;
+      }
+    }
+  else if (new[0] != 0) local_authenticated_sender = new;
+  }
+
+/* Add the authenticated sender address if present */
+
+if ((smtp_authenticated || ob->authenticated_sender_force) &&
+    local_authenticated_sender != NULL)
+  {
+  string_format(buffer, bufsize, " AUTH=%s",
+    auth_xtextencode(local_authenticated_sender,
+    Ustrlen(local_authenticated_sender)));
+  client_authenticated_sender = string_copy(local_authenticated_sender);
+  }
+else
+  *buffer= 0;
+
+return FALSE;
+}
+
+
+
+/*************************************************
+*       Deliver address list to given host       *
+*************************************************/
+
+/* If continue_hostname is not null, we get here only when continuing to
+deliver down an existing channel. The channel was passed as the standard
+input. TLS is never active on a passed channel; the previous process always
+closes it down before passing the connection on.
+
+Otherwise, we have to make a connection to the remote host, and do the
+initial protocol exchange.
+
+When running as an MUA wrapper, if the sender or any recipient is rejected,
+temporarily or permanently, we force failure for all recipients.
+
+Arguments:
+  addrlist        chain of potential addresses to deliver; only those whose
+                  transport_return field is set to PENDING_DEFER are currently
+                  being processed; others should be skipped - they have either
+                  been delivered to an earlier host or IP address, or been
+                  failed by one of them.
+  host            host to deliver to
+  host_af         AF_INET or AF_INET6
+  port            default TCP/IP port to use, in host byte order
+  interface       interface to bind to, or NULL
+  tblock          transport instance block
+  copy_host       TRUE if host set in addr->host_used must be copied, because
+                    it is specific to this call of the transport
+  message_defer   set TRUE if yield is OK, but all addresses were deferred
+                    because of a non-recipient, non-host failure, that is, a
+                    4xx response to MAIL FROM, DATA, or ".". This is a defer
+                    that is specific to the message.
+  suppress_tls    if TRUE, don't attempt a TLS connection - this is set for
+                    a second attempt after TLS initialization fails
+
+Returns:          OK    - the connection was made and the delivery attempted;
+                          the result for each address is in its data block.
+                  DEFER - the connection could not be made, or something failed
+                          while setting up the SMTP session, or there was a
+                          non-message-specific error, such as a timeout.
+                  ERROR - a filter command is specified for this transport,
+                          and there was a problem setting it up; OR helo_data
+                          or add_headers or authenticated_sender is specified
+                          for this transport, and the string failed to expand
+*/
+
+static int
+smtp_deliver(address_item *addrlist, host_item *host, int host_af, int port,
+  uschar *interface, transport_instance *tblock, BOOL copy_host,
+  BOOL *message_defer, BOOL suppress_tls)
+{
+address_item *addr;
+address_item *sync_addr;
+address_item *first_addr = addrlist;
+int yield = OK;
+int address_count;
+int save_errno;
+int rc;
+time_t start_delivery_time = time(NULL);
+smtp_transport_options_block *ob =
+  (smtp_transport_options_block *)(tblock->options_block);
+BOOL lmtp = strcmpic(ob->protocol, US"lmtp") == 0;
+BOOL smtps = strcmpic(ob->protocol, US"smtps") == 0;
+BOOL ok = FALSE;
+BOOL send_rset = TRUE;
+BOOL send_quit = TRUE;
+BOOL setting_up = TRUE;
+BOOL completed_address = FALSE;
+BOOL esmtp = TRUE;
+BOOL pending_MAIL;
+BOOL pass_message = FALSE;
+#ifndef DISABLE_PRDR
+BOOL prdr_offered = FALSE;
+BOOL prdr_active;
+#endif
+#ifdef EXPERIMENTAL_DSN
+BOOL dsn_all_lasthop = TRUE;
+#endif
+smtp_inblock inblock;
+smtp_outblock outblock;
+int max_rcpt = tblock->max_addresses;
+uschar *igquotstr = US"";
+uschar *helo_data = NULL;
+uschar *message = NULL;
+uschar new_message_id[MESSAGE_ID_LENGTH + 1];
+uschar *p;
+uschar buffer[4096];
+uschar inbuffer[4096];
+uschar outbuffer[4096];
+
+suppress_tls = suppress_tls;  /* stop compiler warning when no TLS support */
+
+*message_defer = FALSE;
+smtp_command = US"initial connection";
+if (max_rcpt == 0) max_rcpt = 999999;
+
+/* Set up the buffer for reading SMTP response packets. */
+
+inblock.buffer = inbuffer;
+inblock.buffersize = sizeof(inbuffer);
+inblock.ptr = inbuffer;
+inblock.ptrend = inbuffer;
+
+/* Set up the buffer for holding SMTP commands while pipelining */
+
+outblock.buffer = outbuffer;
+outblock.buffersize = sizeof(outbuffer);
+outblock.ptr = outbuffer;
+outblock.cmd_count = 0;
+outblock.authenticating = FALSE;
+
+/* Reset the parameters of a TLS session. */
+
+tls_out.bits = 0;
+tls_out.cipher = NULL; /* the one we may use for this transport */
+tls_out.ourcert = NULL;
+tls_out.peercert = NULL;
+tls_out.peerdn = NULL;
+#if defined(SUPPORT_TLS) && !defined(USE_GNUTLS)
+tls_out.sni = NULL;
+#endif
+tls_out.ocsp = OCSP_NOT_REQ;
+
+/* Flip the legacy TLS-related variables over to the outbound set in case
+they're used in the context of the transport.  Don't bother resetting
+afterward as we're in a subprocess. */
+
+tls_modify_variables(&tls_out);
+
+#ifndef SUPPORT_TLS
+if (smtps)
+  {
+  set_errno(addrlist, 0, US"TLS support not available", DEFER, FALSE);
+  return ERROR;
+  }
+#endif
+
+/* Make a connection to the host if this isn't a continued delivery, and handle
+the initial interaction and HELO/EHLO/LHLO. Connect timeout errors are handled
+specially so they can be identified for retries. */
+
+if (continue_hostname == NULL)
+  {
+  inblock.sock = outblock.sock =
+    smtp_connect(host, host_af, port, interface, ob->connect_timeout,
+      ob->keepalive, ob->dscp);   /* This puts port into host->port */
+
+  if (inblock.sock < 0)
+    {
+    set_errno(addrlist, (errno == ETIMEDOUT)? ERRNO_CONNECTTIMEOUT : errno,
+      NULL, DEFER, FALSE);
+    return DEFER;
+    }
+
+  /* Expand the greeting message while waiting for the initial response. (Makes
+  sense if helo_data contains ${lookup dnsdb ...} stuff). The expansion is
+  delayed till here so that $sending_interface and $sending_port are set. */
+
+  helo_data = expand_string(ob->helo_data);
+
+  /* The first thing is to wait for an initial OK response. The dreaded "goto"
+  is nevertheless a reasonably clean way of programming this kind of logic,
+  where you want to escape on any error. */
+
+  if (!smtps)
+    {
+    if (!smtp_read_response(&inblock, buffer, sizeof(buffer), '2',
+      ob->command_timeout)) goto RESPONSE_FAILED;
+
+    /* Now check if the helo_data expansion went well, and sign off cleanly if
+    it didn't. */
+
+    if (helo_data == NULL)
+      {
+      uschar *message = string_sprintf("failed to expand helo_data: %s",
+        expand_string_message);
+      set_errno(addrlist, 0, message, DEFER, FALSE);
+      yield = DEFER;
+      goto SEND_QUIT;
+      }
+    }
+
+/** Debugging without sending a message
+addrlist->transport_return = DEFER;
+goto SEND_QUIT;
+**/
+
+  /* Errors that occur after this point follow an SMTP command, which is
+  left in big_buffer by smtp_write_command() for use in error messages. */
+
+  smtp_command = big_buffer;
+
+  /* Tell the remote who we are...
+
+  February 1998: A convention has evolved that ESMTP-speaking MTAs include the
+  string "ESMTP" in their greeting lines, so make Exim send EHLO if the
+  greeting is of this form. The assumption was that the far end supports it
+  properly... but experience shows that there are some that give 5xx responses,
+  even though the banner includes "ESMTP" (there's a bloody-minded one that
+  says "ESMTP not spoken here"). Cope with that case.
+
+  September 2000: Time has passed, and it seems reasonable now to always send
+  EHLO at the start. It is also convenient to make the change while installing
+  the TLS stuff.
+
+  July 2003: Joachim Wieland met a broken server that advertises "PIPELINING"
+  but times out after sending MAIL FROM, RCPT TO and DATA all together. There
+  would be no way to send out the mails, so there is now a host list
+  "hosts_avoid_esmtp" that disables ESMTP for special hosts and solves the
+  PIPELINING problem as well. Maybe it can also be useful to cure other
+  problems with broken servers.
+
+  Exim originally sent "Helo" at this point and ran for nearly a year that way.
+  Then somebody tried it with a Microsoft mailer... It seems that all other
+  mailers use upper case for some reason (the RFC is quite clear about case
+  independence) so, for peace of mind, I gave in. */
+
+  esmtp = verify_check_this_host(&(ob->hosts_avoid_esmtp), NULL,
+     host->name, host->address, NULL) != OK;
+
+  /* Alas; be careful, since this goto is not an error-out, so conceivably
+  we might set data between here and the target which we assume to exist
+  and be usable.  I can see this coming back to bite us. */
+  #ifdef SUPPORT_TLS
+  if (smtps)
+    {
+    tls_offered = TRUE;
+    suppress_tls = FALSE;
+    ob->tls_tempfail_tryclear = FALSE;
+    smtp_command = US"SSL-on-connect";
+    goto TLS_NEGOTIATE;
+    }
+  #endif
+
+  if (esmtp)
+    {
+    if (smtp_write_command(&outblock, FALSE, "%s %s\r\n",
+         lmtp? "LHLO" : "EHLO", helo_data) < 0)
+      goto SEND_FAILED;
+    if (!smtp_read_response(&inblock, buffer, sizeof(buffer), '2',
+           ob->command_timeout))
+      {
+      if (errno != 0 || buffer[0] == 0 || lmtp) goto RESPONSE_FAILED;
+      esmtp = FALSE;
+      }
+    }
+  else
+    {
+    DEBUG(D_transport)
+      debug_printf("not sending EHLO (host matches hosts_avoid_esmtp)\n");
+    }
+
+  if (!esmtp)
+    {
+    if (smtp_write_command(&outblock, FALSE, "HELO %s\r\n", helo_data) < 0)
+      goto SEND_FAILED;
+    if (!smtp_read_response(&inblock, buffer, sizeof(buffer), '2',
+      ob->command_timeout)) goto RESPONSE_FAILED;
+    }
+
+  /* Set IGNOREQUOTA if the response to LHLO specifies support and the
+  lmtp_ignore_quota option was set. */
+
+  igquotstr = (lmtp && ob->lmtp_ignore_quota &&
+    pcre_exec(regex_IGNOREQUOTA, NULL, CS buffer, Ustrlen(CS buffer), 0,
+      PCRE_EOPT, NULL, 0) >= 0)? US" IGNOREQUOTA" : US"";
+
+  /* Set tls_offered if the response to EHLO specifies support for STARTTLS. */
+
+  #ifdef SUPPORT_TLS
+  tls_offered = esmtp &&
+    pcre_exec(regex_STARTTLS, NULL, CS buffer, Ustrlen(buffer), 0,
+      PCRE_EOPT, NULL, 0) >= 0;
+  #endif
+
+  #ifndef DISABLE_PRDR
+  prdr_offered = esmtp &&
+    (pcre_exec(regex_PRDR, NULL, CS buffer, Ustrlen(buffer), 0,
+      PCRE_EOPT, NULL, 0) >= 0) &&
+    (verify_check_this_host(&(ob->hosts_try_prdr), NULL, host->name,
+      host->address, NULL) == OK);
+
+  if (prdr_offered)
+    {DEBUG(D_transport) debug_printf("PRDR usable\n");}
+  #endif
+  }
+
+/* For continuing deliveries down the same channel, the socket is the standard
+input, and we don't need to redo EHLO here (but may need to do so for TLS - see
+below). Set up the pointer to where subsequent commands will be left, for
+error messages. Note that smtp_use_size and smtp_use_pipelining will have been
+set from the command line if they were set in the process that passed the
+connection on. */
+
+else
+  {
+  inblock.sock = outblock.sock = fileno(stdin);
+  smtp_command = big_buffer;
+  host->port = port;    /* Record the port that was used */
+  }
+
+/* If TLS is available on this connection, whether continued or not, attempt to
+start up a TLS session, unless the host is in hosts_avoid_tls. If successful,
+send another EHLO - the server may give a different answer in secure mode. We
+use a separate buffer for reading the response to STARTTLS so that if it is
+negative, the original EHLO data is available for subsequent analysis, should
+the client not be required to use TLS. If the response is bad, copy the buffer
+for error analysis. */
+
+#ifdef SUPPORT_TLS
+if (tls_offered && !suppress_tls &&
+      verify_check_this_host(&(ob->hosts_avoid_tls), NULL, host->name,
+        host->address, NULL) != OK)
+  {
+  uschar buffer2[4096];
+  if (smtp_write_command(&outblock, FALSE, "STARTTLS\r\n") < 0)
+    goto SEND_FAILED;
+
+  /* If there is an I/O error, transmission of this message is deferred. If
+  there is a temporary rejection of STARRTLS and tls_tempfail_tryclear is
+  false, we also defer. However, if there is a temporary rejection of STARTTLS
+  and tls_tempfail_tryclear is true, or if there is an outright rejection of
+  STARTTLS, we carry on. This means we will try to send the message in clear,
+  unless the host is in hosts_require_tls (tested below). */
+
+  if (!smtp_read_response(&inblock, buffer2, sizeof(buffer2), '2',
+      ob->command_timeout))
+    {
+    if (errno != 0 || buffer2[0] == 0 ||
+         (buffer2[0] == '4' && !ob->tls_tempfail_tryclear))
+      {
+      Ustrncpy(buffer, buffer2, sizeof(buffer));
+      goto RESPONSE_FAILED;
+      }
+    }
+
+  /* STARTTLS accepted: try to negotiate a TLS session. */
+
+  else
+  TLS_NEGOTIATE:
+    {
+    int rc = tls_client_start(inblock.sock, host, addrlist, ob);
+
+    /* TLS negotiation failed; give an error. From outside, this function may
+    be called again to try in clear on a new connection, if the options permit
+    it for this host. */
+
+    if (rc != OK)
+      {
+      save_errno = ERRNO_TLSFAILURE;
+      message = US"failure while setting up TLS session";
+      send_quit = FALSE;
+      goto TLS_FAILED;
+      }
+
+    /* TLS session is set up */
+
+    for (addr = addrlist; addr != NULL; addr = addr->next)
+      {
+      if (addr->transport_return == PENDING_DEFER)
+        {
+        addr->cipher = tls_out.cipher;
+        addr->ourcert = tls_out.ourcert;
+        addr->peercert = tls_out.peercert;
+        addr->peerdn = tls_out.peerdn;
+       addr->ocsp = tls_out.ocsp;
+        }
+      }
+    }
+  }
+
+/* if smtps, we'll have smtp_command set to something else; always safe to
+reset it here. */
+smtp_command = big_buffer;
+
+/* If we started TLS, redo the EHLO/LHLO exchange over the secure channel. If
+helo_data is null, we are dealing with a connection that was passed from
+another process, and so we won't have expanded helo_data above. We have to
+expand it here. $sending_ip_address and $sending_port are set up right at the
+start of the Exim process (in exim.c). */
+
+if (tls_out.active >= 0)
+  {
+  char *greeting_cmd;
+  if (helo_data == NULL)
+    {
+    helo_data = expand_string(ob->helo_data);
+    if (helo_data == NULL)
+      {
+      uschar *message = string_sprintf("failed to expand helo_data: %s",
+        expand_string_message);
+      set_errno(addrlist, 0, message, DEFER, FALSE);
+      yield = DEFER;
+      goto SEND_QUIT;
+      }
+    }
+
+  /* For SMTPS we need to wait for the initial OK response. */
+  if (smtps)
+    {
+    if (!smtp_read_response(&inblock, buffer, sizeof(buffer), '2',
+      ob->command_timeout)) goto RESPONSE_FAILED;
+    }
+
+  if (esmtp)
+    greeting_cmd = "EHLO";
+  else
+    {
+    greeting_cmd = "HELO";
+    DEBUG(D_transport)
+      debug_printf("not sending EHLO (host matches hosts_avoid_esmtp)\n");
+    }
+
+  if (smtp_write_command(&outblock, FALSE, "%s %s\r\n",
+        lmtp? "LHLO" : greeting_cmd, helo_data) < 0)
+    goto SEND_FAILED;
+  if (!smtp_read_response(&inblock, buffer, sizeof(buffer), '2',
+       ob->command_timeout))
+    goto RESPONSE_FAILED;
+  }
+
+/* If the host is required to use a secure channel, ensure that we
+have one. */
+
+else if (verify_check_this_host(&(ob->hosts_require_tls), NULL, host->name,
+          host->address, NULL) == OK)
+  {
+  save_errno = ERRNO_TLSREQUIRED;
+  message = string_sprintf("a TLS session is required for %s [%s], but %s",
+    host->name, host->address,
+    tls_offered? "an attempt to start TLS failed" :
+                 "the server did not offer TLS support");
+  goto TLS_FAILED;
+  }
+#endif
+
+/* If TLS is active, we have just started it up and re-done the EHLO command,
+so its response needs to be analyzed. If TLS is not active and this is a
+continued session down a previously-used socket, we haven't just done EHLO, so
+we skip this. */
+
+if (continue_hostname == NULL
+    #ifdef SUPPORT_TLS
+    || tls_out.active >= 0
+    #endif
+    )
+  {
+  /* Set for IGNOREQUOTA if the response to LHLO specifies support and the
+  lmtp_ignore_quota option was set. */
+
+  igquotstr = (lmtp && ob->lmtp_ignore_quota &&
+    pcre_exec(regex_IGNOREQUOTA, NULL, CS buffer, Ustrlen(CS buffer), 0,
+      PCRE_EOPT, NULL, 0) >= 0)? US" IGNOREQUOTA" : US"";
+
+  /* If the response to EHLO specified support for the SIZE parameter, note
+  this, provided size_addition is non-negative. */
+
+  smtp_use_size = esmtp && ob->size_addition >= 0 &&
+    pcre_exec(regex_SIZE, NULL, CS buffer, Ustrlen(CS buffer), 0,
+      PCRE_EOPT, NULL, 0) >= 0;
+
+  /* Note whether the server supports PIPELINING. If hosts_avoid_esmtp matched
+  the current host, esmtp will be false, so PIPELINING can never be used. If
+  the current host matches hosts_avoid_pipelining, don't do it. */
+
+  smtp_use_pipelining = esmtp &&
+    verify_check_this_host(&(ob->hosts_avoid_pipelining), NULL, host->name,
+      host->address, NULL) != OK &&
+    pcre_exec(regex_PIPELINING, NULL, CS buffer, Ustrlen(CS buffer), 0,
+      PCRE_EOPT, NULL, 0) >= 0;
+
+  DEBUG(D_transport) debug_printf("%susing PIPELINING\n",
+    smtp_use_pipelining? "" : "not ");
+
+#ifndef DISABLE_PRDR
+  prdr_offered = esmtp &&
+    pcre_exec(regex_PRDR, NULL, CS buffer, Ustrlen(CS buffer), 0,
+      PCRE_EOPT, NULL, 0) >= 0 &&
+    verify_check_this_host(&(ob->hosts_try_prdr), NULL, host->name,
+      host->address, NULL) == OK;
+
+  if (prdr_offered)
+    {DEBUG(D_transport) debug_printf("PRDR usable\n");}
+#endif
+
+#ifdef EXPERIMENTAL_DSN
+  /* Note if the server supports DSN */
+  smtp_use_dsn = esmtp && pcre_exec(regex_DSN, NULL, CS buffer, (int)Ustrlen(CS buffer), 0,
+       PCRE_EOPT, NULL, 0) >= 0;
+  DEBUG(D_transport) debug_printf("use_dsn=%d\n", smtp_use_dsn);
+#endif
+
+  /* Note if the response to EHLO specifies support for the AUTH extension.
+  If it has, check that this host is one we want to authenticate to, and do
+  the business. The host name and address must be available when the
+  authenticator's client driver is running. */
+
+  switch (yield = smtp_auth(buffer, sizeof(buffer), addrlist, host,
+                           ob, esmtp, &inblock, &outblock))
+    {
+    default:           goto SEND_QUIT;
+    case OK:           break;
+    case FAIL_SEND:    goto SEND_FAILED;
+    case FAIL:         goto RESPONSE_FAILED;
+    }
+  }
+
+/* The setting up of the SMTP call is now complete. Any subsequent errors are
+message-specific. */
+
+setting_up = FALSE;
+
+/* If there is a filter command specified for this transport, we can now
+set it up. This cannot be done until the identify of the host is known. */
+
+if (tblock->filter_command != NULL)
+  {
+  BOOL rc;
+  uschar buffer[64];
+  sprintf(CS buffer, "%.50s transport", tblock->name);
+  rc = transport_set_up_command(&transport_filter_argv, tblock->filter_command,
+    TRUE, DEFER, addrlist, buffer, NULL);
+  transport_filter_timeout = tblock->filter_timeout;
+
+  /* On failure, copy the error to all addresses, abandon the SMTP call, and
+  yield ERROR. */
+
+  if (!rc)
+    {
+    set_errno(addrlist->next, addrlist->basic_errno, addrlist->message, DEFER,
+      FALSE);
+    yield = ERROR;
+    goto SEND_QUIT;
+    }
+  }
+
+
+/* For messages that have more than the maximum number of envelope recipients,
+we want to send several transactions down the same SMTP connection. (See
+comments in deliver.c as to how this reconciles, heuristically, with
+remote_max_parallel.) This optimization was added to Exim after the following
+code was already working. The simplest way to put it in without disturbing the
+code was to use a goto to jump back to this point when there is another
+transaction to handle. */
+
+SEND_MESSAGE:
+sync_addr = first_addr;
+address_count = 0;
+ok = FALSE;
+send_rset = TRUE;
+completed_address = FALSE;
+
+
+/* Initiate a message transfer. If we know the receiving MTA supports the SIZE
+qualification, send it, adding something to the message size to allow for
+imprecision and things that get added en route. Exim keeps the number of lines
+in a message, so we can give an accurate value for the original message, but we
+need some additional to handle added headers. (Double "." characters don't get
+included in the count.) */
+
+p = buffer;
+*p = 0;
+
+if (smtp_use_size)
+  {
+  sprintf(CS p, " SIZE=%d", message_size+message_linecount+ob->size_addition);
+  while (*p) p++;
+  }
+
+#ifndef DISABLE_PRDR
+prdr_active = FALSE;
+if (prdr_offered)
+  {
+  for (addr = first_addr; addr; addr = addr->next)
+    if (addr->transport_return == PENDING_DEFER)
+      {
+      for (addr = addr->next; addr; addr = addr->next)
+        if (addr->transport_return == PENDING_DEFER)
+         {                     /* at least two recipients to send */
+         prdr_active = TRUE;
+         sprintf(CS p, " PRDR"); p += 5;
+         goto prdr_is_active;
+         }
+      break;
+      }
+  }
+prdr_is_active:
+#endif
+
+#ifdef EXPERIMENTAL_DSN
+/* check if all addresses have lasthop flag */
+/* do not send RET and ENVID if true */
+dsn_all_lasthop = TRUE;
+for (addr = first_addr;
+     address_count < max_rcpt && addr != NULL;
+     addr = addr->next)
+  if ((addr->dsn_flags & rf_dsnlasthop) != 1)
+    dsn_all_lasthop = FALSE;
+
+/* Add any DSN flags to the mail command */
+
+if ((smtp_use_dsn) && (dsn_all_lasthop == FALSE))
+  {
+  if (dsn_ret == dsn_ret_hdrs)
+    {
+    strcpy(p, " RET=HDRS");
+    while (*p) p++;
+    }
+  else if (dsn_ret == dsn_ret_full)
+    {
+    strcpy(p, " RET=FULL");
+    while (*p) p++;
+    }
+  if (dsn_envid != NULL)
+    {
+    string_format(p, sizeof(buffer) - (p-buffer), " ENVID=%s", dsn_envid);
+    while (*p) p++;
+    }
+  }
+#endif
+
+/* If an authenticated_sender override has been specified for this transport
+instance, expand it. If the expansion is forced to fail, and there was already
+an authenticated_sender for this message, the original value will be used.
+Other expansion failures are serious. An empty result is ignored, but there is
+otherwise no check - this feature is expected to be used with LMTP and other
+cases where non-standard addresses (e.g. without domains) might be required. */
+
+if (smtp_mail_auth_str(p, sizeof(buffer) - (p-buffer), addrlist, ob))
+    return ERROR;
+
+/* From here until we send the DATA command, we can make use of PIPELINING
+if the server host supports it. The code has to be able to check the responses
+at any point, for when the buffer fills up, so we write it totally generally.
+When PIPELINING is off, each command written reports that it has flushed the
+buffer. */
+
+pending_MAIL = TRUE;     /* The block starts with MAIL */
+
+rc = smtp_write_command(&outblock, smtp_use_pipelining,
+       "MAIL FROM:<%s>%s\r\n", return_path, buffer);
+mail_command = string_copy(big_buffer);  /* Save for later error message */
+
+switch(rc)
+  {
+  case -1:                /* Transmission error */
+  goto SEND_FAILED;
+
+  case +1:                /* Block was sent */
+  if (!smtp_read_response(&inblock, buffer, sizeof(buffer), '2',
+       ob->command_timeout))
+    {
+    if (errno == 0 && buffer[0] == '4')
+      {
+      errno = ERRNO_MAIL4XX;
+      addrlist->more_errno |= ((buffer[1] - '0')*10 + buffer[2] - '0') << 8;
+      }
+    goto RESPONSE_FAILED;
+    }
+  pending_MAIL = FALSE;
+  break;
+  }
+
+/* Pass over all the relevant recipient addresses for this host, which are the
+ones that have status PENDING_DEFER. If we are using PIPELINING, we can send
+several before we have to read the responses for those seen so far. This
+checking is done by a subroutine because it also needs to be done at the end.
+Send only up to max_rcpt addresses at a time, leaving first_addr pointing to
+the next one if not all are sent.
+
+In the MUA wrapper situation, we want to flush the PIPELINING buffer for the
+last address because we want to abort if any recipients have any kind of
+problem, temporary or permanent. We know that all recipient addresses will have
+the PENDING_DEFER status, because only one attempt is ever made, and we know
+that max_rcpt will be large, so all addresses will be done at once. */
+
+for (addr = first_addr;
+     address_count < max_rcpt && addr != NULL;
+     addr = addr->next)
+  {
+  int count;
+  BOOL no_flush;
+
+  #ifdef EXPERIMENTAL_DSN
+  if(smtp_use_dsn)
+    addr->dsn_aware = dsn_support_yes;
+  else
+    addr->dsn_aware = dsn_support_no;
+  #endif
+
+  if (addr->transport_return != PENDING_DEFER) continue;
+
+  address_count++;
+  no_flush = smtp_use_pipelining && (!mua_wrapper || addr->next != NULL);
+
+  #ifdef EXPERIMENTAL_DSN
+  /* Add any DSN flags to the rcpt command and add to the sent string */
+
+  p = buffer;
+  *p = 0;
+
+  if ((smtp_use_dsn) && ((addr->dsn_flags & rf_dsnlasthop) != 1))
+    {
+    if ((addr->dsn_flags & rf_dsnflags) != 0)
+      {
+      int i;
+      BOOL first = TRUE;
+      strcpy(p, " NOTIFY=");
+      while (*p) p++;
+      for (i = 0; i < 4; i++)
+        {
+        if ((addr->dsn_flags & rf_list[i]) != 0)
+          {
+          if (!first) *p++ = ',';
+          first = FALSE;
+          strcpy(p, rf_names[i]);
+          while (*p) p++;
+          }
+        }
+      }
+
+    if (addr->dsn_orcpt != NULL) {
+      string_format(p, sizeof(buffer) - (p-buffer), " ORCPT=%s",
+        addr->dsn_orcpt);
+      while (*p) p++;
+      }
+    }
+  #endif
+
+
+  /* Now send the RCPT command, and process outstanding responses when
+  necessary. After a timeout on RCPT, we just end the function, leaving the
+  yield as OK, because this error can often mean that there is a problem with
+  just one address, so we don't want to delay the host. */
+
+  #ifdef EXPERIMENTAL_DSN
+  count = smtp_write_command(&outblock, no_flush, "RCPT TO:<%s>%s%s\r\n",
+    transport_rcpt_address(addr, tblock->rcpt_include_affixes), igquotstr, buffer);
+  #else
+  count = smtp_write_command(&outblock, no_flush, "RCPT TO:<%s>%s\r\n",
+    transport_rcpt_address(addr, tblock->rcpt_include_affixes), igquotstr);
+  #endif
+
+  if (count < 0) goto SEND_FAILED;
+  if (count > 0)
+    {
+    switch(sync_responses(first_addr, tblock->rcpt_include_affixes,
+             &sync_addr, host, count, ob->address_retry_include_sender,
+             pending_MAIL, 0, &inblock, ob->command_timeout, buffer,
+             sizeof(buffer)))
+      {
+      case 3: ok = TRUE;                   /* 2xx & 5xx => OK & progress made */
+      case 2: completed_address = TRUE;    /* 5xx (only) => progress made */
+      break;
+
+      case 1: ok = TRUE;                   /* 2xx (only) => OK, but if LMTP, */
+      if (!lmtp) completed_address = TRUE; /* can't tell about progress yet */
+      case 0:                              /* No 2xx or 5xx, but no probs */
+      break;
+
+      case -1: goto END_OFF;               /* Timeout on RCPT */
+      default: goto RESPONSE_FAILED;       /* I/O error, or any MAIL error */
+      }
+    pending_MAIL = FALSE;                  /* Dealt with MAIL */
+    }
+  }      /* Loop for next address */
+
+/* If we are an MUA wrapper, abort if any RCPTs were rejected, either
+permanently or temporarily. We should have flushed and synced after the last
+RCPT. */
+
+if (mua_wrapper)
+  {
+  address_item *badaddr;
+  for (badaddr = first_addr; badaddr != NULL; badaddr = badaddr->next)
+    {
+    if (badaddr->transport_return != PENDING_OK) break;
+    }
+  if (badaddr != NULL)
+    {
+    set_errno(addrlist, 0, badaddr->message, FAIL,
+      testflag(badaddr, af_pass_message));
+    ok = FALSE;
+    }
+  }
+
+/* If ok is TRUE, we know we have got at least one good recipient, and must now
+send DATA, but if it is FALSE (in the normal, non-wrapper case), we may still
+have a good recipient buffered up if we are pipelining. We don't want to waste
+time sending DATA needlessly, so we only send it if either ok is TRUE or if we
+are pipelining. The responses are all handled by sync_responses(). */
+
+if (ok || (smtp_use_pipelining && !mua_wrapper))
+  {
+  int count = smtp_write_command(&outblock, FALSE, "DATA\r\n");
+  if (count < 0) goto SEND_FAILED;
+  switch(sync_responses(first_addr, tblock->rcpt_include_affixes, &sync_addr,
+           host, count, ob->address_retry_include_sender, pending_MAIL,
+           ok? +1 : -1, &inblock, ob->command_timeout, buffer, sizeof(buffer)))
+    {
+    case 3: ok = TRUE;                   /* 2xx & 5xx => OK & progress made */
+    case 2: completed_address = TRUE;    /* 5xx (only) => progress made */
+    break;
+
+    case 1: ok = TRUE;                   /* 2xx (only) => OK, but if LMTP, */
+    if (!lmtp) completed_address = TRUE; /* can't tell about progress yet */
+    case 0: break;                       /* No 2xx or 5xx, but no probs */
+
+    case -1: goto END_OFF;               /* Timeout on RCPT */
+    default: goto RESPONSE_FAILED;       /* I/O error, or any MAIL/DATA error */
+    }
+  }
+
+/* Save the first address of the next batch. */
+
+first_addr = addr;
+
+/* If there were no good recipients (but otherwise there have been no
+problems), just set ok TRUE, since we have handled address-specific errors
+already. Otherwise, it's OK to send the message. Use the check/escape mechanism
+for handling the SMTP dot-handling protocol, flagging to apply to headers as
+well as body. Set the appropriate timeout value to be used for each chunk.
+(Haven't been able to make it work using select() for writing yet.) */
+
+if (!ok) ok = TRUE; else
+  {
+  sigalrm_seen = FALSE;
+  transport_write_timeout = ob->data_timeout;
+  smtp_command = US"sending data block";   /* For error messages */
+  DEBUG(D_transport|D_v)
+    debug_printf("  SMTP>> writing message and terminating \".\"\n");
+  transport_count = 0;
+#ifndef DISABLE_DKIM
+  ok = dkim_transport_write_message(addrlist, inblock.sock,
+    topt_use_crlf | topt_end_dot | topt_escape_headers |
+      (tblock->body_only? topt_no_headers : 0) |
+      (tblock->headers_only? topt_no_body : 0) |
+      (tblock->return_path_add? topt_add_return_path : 0) |
+      (tblock->delivery_date_add? topt_add_delivery_date : 0) |
+      (tblock->envelope_to_add? topt_add_envelope_to : 0),
+    0,            /* No size limit */
+    tblock->add_headers, tblock->remove_headers,
+    US".", US"..",    /* Escaping strings */
+    tblock->rewrite_rules, tblock->rewrite_existflags,
+    ob->dkim_private_key, ob->dkim_domain, ob->dkim_selector,
+    ob->dkim_canon, ob->dkim_strict, ob->dkim_sign_headers
+    );
+#else
+  ok = transport_write_message(addrlist, inblock.sock,
+    topt_use_crlf | topt_end_dot | topt_escape_headers |
+      (tblock->body_only? topt_no_headers : 0) |
+      (tblock->headers_only? topt_no_body : 0) |
+      (tblock->return_path_add? topt_add_return_path : 0) |
+      (tblock->delivery_date_add? topt_add_delivery_date : 0) |
+      (tblock->envelope_to_add? topt_add_envelope_to : 0),
+    0,            /* No size limit */
+    tblock->add_headers, tblock->remove_headers,
+    US".", US"..",    /* Escaping strings */
+    tblock->rewrite_rules, tblock->rewrite_existflags);
+#endif
+
+  /* transport_write_message() uses write() because it is called from other
+  places to write to non-sockets. This means that under some OS (e.g. Solaris)
+  it can exit with "Broken pipe" as its error. This really means that the
+  socket got closed at the far end. */
+
+  transport_write_timeout = 0;   /* for subsequent transports */
+
+  /* Failure can either be some kind of I/O disaster (including timeout),
+  or the failure of a transport filter or the expansion of added headers. */
+
+  if (!ok)
+    {
+    buffer[0] = 0;              /* There hasn't been a response */
+    goto RESPONSE_FAILED;
+    }
+
+  /* We used to send the terminating "." explicitly here, but because of
+  buffering effects at both ends of TCP/IP connections, you don't gain
+  anything by keeping it separate, so it might as well go in the final
+  data buffer for efficiency. This is now done by setting the topt_end_dot
+  flag above. */
+
+  smtp_command = US"end of data";
+
+#ifndef DISABLE_PRDR
+  /* For PRDR we optionally get a partial-responses warning
+   * followed by the individual responses, before going on with
+   * the overall response.  If we don't get the warning then deal
+   * with per non-PRDR. */
+  if(prdr_active)
+    {
+    ok = smtp_read_response(&inblock, buffer, sizeof(buffer), '3',
+      ob->final_timeout);
+    if (!ok && errno == 0)
+      switch(buffer[0])
+        {
+       case '2': prdr_active = FALSE;
+                 ok = TRUE;
+                 break;
+       case '4': errno = ERRNO_DATA4XX;
+                  addrlist->more_errno |= ((buffer[1] - '0')*10 + buffer[2] - '0') << 8;
+                 break;
+        }
+    }
+  else
+#endif
+
+  /* For non-PRDR SMTP, we now read a single response that applies to the
+  whole message.  If it is OK, then all the addresses have been delivered. */
+
+  if (!lmtp)
+    {
+    ok = smtp_read_response(&inblock, buffer, sizeof(buffer), '2',
+      ob->final_timeout);
+    if (!ok && errno == 0 && buffer[0] == '4')
+      {
+      errno = ERRNO_DATA4XX;
+      addrlist->more_errno |= ((buffer[1] - '0')*10 + buffer[2] - '0') << 8;
+      }
+    }
+
+  /* For LMTP, we get back a response for every RCPT command that we sent;
+  some may be accepted and some rejected. For those that get a response, their
+  status is fixed; any that are accepted have been handed over, even if later
+  responses crash - at least, that's how I read RFC 2033.
+
+  If all went well, mark the recipient addresses as completed, record which
+  host/IPaddress they were delivered to, and cut out RSET when sending another
+  message down the same channel. Write the completed addresses to the journal
+  now so that they are recorded in case there is a crash of hardware or
+  software before the spool gets updated. Also record the final SMTP
+  confirmation if needed (for SMTP only). */
+
+  if (ok)
+    {
+    int flag = '=';
+    int delivery_time = (int)(time(NULL) - start_delivery_time);
+    int len;
+    host_item *thost;
+    uschar *conf = NULL;
+    send_rset = FALSE;
+
+    /* Make a copy of the host if it is local to this invocation
+    of the transport. */
+
+    if (copy_host)
+      {
+      thost = store_get(sizeof(host_item));
+      *thost = *host;
+      thost->name = string_copy(host->name);
+      thost->address = string_copy(host->address);
+      }
+    else thost = host;
+
+    /* Set up confirmation if needed - applies only to SMTP */
+
+    if (
+        #ifndef EXPERIMENTAL_TPDA
+          (log_extra_selector & LX_smtp_confirmation) != 0 &&
+        #endif
+          !lmtp
+       )
+      {
+      uschar *s = string_printing(buffer);
+      conf = (s == buffer)? (uschar *)string_copy(s) : s;
+      }
+
+    /* Process all transported addresses - for LMTP or PRDR, read a status for
+    each one. */
+
+    for (addr = addrlist; addr != first_addr; addr = addr->next)
+      {
+      if (addr->transport_return != PENDING_OK) continue;
+
+      /* LMTP - if the response fails badly (e.g. timeout), use it for all the
+      remaining addresses. Otherwise, it's a return code for just the one
+      address. For temporary errors, add a retry item for the address so that
+      it doesn't get tried again too soon. */
+
+#ifndef DISABLE_PRDR
+      if (lmtp || prdr_active)
+#else
+      if (lmtp)
+#endif
+        {
+        if (!smtp_read_response(&inblock, buffer, sizeof(buffer), '2',
+            ob->final_timeout))
+          {
+          if (errno != 0 || buffer[0] == 0) goto RESPONSE_FAILED;
+          addr->message = string_sprintf(
+#ifndef DISABLE_PRDR
+           "%s error after %s: %s", prdr_active ? "PRDR":"LMTP",
+#else
+           "LMTP error after %s: %s",
+#endif
+            big_buffer, string_printing(buffer));
+          setflag(addr, af_pass_message);   /* Allow message to go to user */
+          if (buffer[0] == '5')
+            addr->transport_return = FAIL;
+          else
+            {
+            errno = ERRNO_DATA4XX;
+            addr->more_errno |= ((buffer[1] - '0')*10 + buffer[2] - '0') << 8;
+            addr->transport_return = DEFER;
+#ifndef DISABLE_PRDR
+            if (!prdr_active)
+#endif
+              retry_add_item(addr, addr->address_retry_key, 0);
+            }
+          continue;
+          }
+        completed_address = TRUE;   /* NOW we can set this flag */
+        if ((log_extra_selector & LX_smtp_confirmation) != 0)
+          {
+          uschar *s = string_printing(buffer);
+          conf = (s == buffer)? (uschar *)string_copy(s) : s;
+          }
+        }
+
+      /* SMTP, or success return from LMTP for this address. Pass back the
+      actual host that was used. */
+
+      addr->transport_return = OK;
+      addr->more_errno = delivery_time;
+      addr->host_used = thost;
+      addr->special_action = flag;
+      addr->message = conf;
+#ifndef DISABLE_PRDR
+      if (prdr_active) addr->flags |= af_prdr_used;
+#endif
+      flag = '-';
+
+#ifndef DISABLE_PRDR
+      if (!prdr_active)
+#endif
+        {
+        /* Update the journal. For homonymic addresses, use the base address plus
+        the transport name. See lots of comments in deliver.c about the reasons
+        for the complications when homonyms are involved. Just carry on after
+        write error, as it may prove possible to update the spool file later. */
+
+        if (testflag(addr, af_homonym))
+          sprintf(CS buffer, "%.500s/%s\n", addr->unique + 3, tblock->name);
+        else
+          sprintf(CS buffer, "%.500s\n", addr->unique);
+
+        DEBUG(D_deliver) debug_printf("journalling %s", buffer);
+        len = Ustrlen(CS buffer);
+        if (write(journal_fd, buffer, len) != len)
+          log_write(0, LOG_MAIN|LOG_PANIC, "failed to write journal for "
+            "%s: %s", buffer, strerror(errno));
+        }
+      }
+
+#ifndef DISABLE_PRDR
+      if (prdr_active)
+        {
+       /* PRDR - get the final, overall response.  For any non-success
+       upgrade all the address statuses. */
+        ok = smtp_read_response(&inblock, buffer, sizeof(buffer), '2',
+          ob->final_timeout);
+        if (!ok)
+         {
+         if(errno == 0 && buffer[0] == '4')
+            {
+            errno = ERRNO_DATA4XX;
+            addrlist->more_errno |= ((buffer[1] - '0')*10 + buffer[2] - '0') << 8;
+            }
+         for (addr = addrlist; addr != first_addr; addr = addr->next)
+            if (buffer[0] == '5' || addr->transport_return == OK)
+              addr->transport_return = PENDING_OK; /* allow set_errno action */
+         goto RESPONSE_FAILED;
+         }
+
+       /* Update the journal, or setup retry. */
+        for (addr = addrlist; addr != first_addr; addr = addr->next)
+         if (addr->transport_return == OK)
+         {
+          if (testflag(addr, af_homonym))
+            sprintf(CS buffer, "%.500s/%s\n", addr->unique + 3, tblock->name);
+          else
+            sprintf(CS buffer, "%.500s\n", addr->unique);
+
+          DEBUG(D_deliver) debug_printf("journalling(PRDR) %s", buffer);
+          len = Ustrlen(CS buffer);
+          if (write(journal_fd, buffer, len) != len)
+            log_write(0, LOG_MAIN|LOG_PANIC, "failed to write journal for "
+              "%s: %s", buffer, strerror(errno));
+         }
+       else if (addr->transport_return == DEFER)
+          retry_add_item(addr, addr->address_retry_key, -2);
+       }
+#endif
+
+    /* Ensure the journal file is pushed out to disk. */
+
+    if (EXIMfsync(journal_fd) < 0)
+      log_write(0, LOG_MAIN|LOG_PANIC, "failed to fsync journal: %s",
+        strerror(errno));
+    }
+  }
+
+
+/* Handle general (not specific to one address) failures here. The value of ok
+is used to skip over this code on the falling through case. A timeout causes a
+deferral. Other errors may defer or fail according to the response code, and
+may set up a special errno value, e.g. after connection chopped, which is
+assumed if errno == 0 and there is no text in the buffer. If control reaches
+here during the setting up phase (i.e. before MAIL FROM) then always defer, as
+the problem is not related to this specific message. */
+
+if (!ok)
+  {
+  int code;
+
+  RESPONSE_FAILED:
+  save_errno = errno;
+  message = NULL;
+  send_quit = check_response(host, &save_errno, addrlist->more_errno,
+    buffer, &code, &message, &pass_message);
+  goto FAILED;
+
+  SEND_FAILED:
+  save_errno = errno;
+  code = '4';
+  message = US string_sprintf("send() to %s [%s] failed: %s",
+    host->name, host->address, strerror(save_errno));
+  send_quit = FALSE;
+  goto FAILED;
+
+  /* This label is jumped to directly when a TLS negotiation has failed,
+  or was not done for a host for which it is required. Values will be set
+  in message and save_errno, and setting_up will always be true. Treat as
+  a temporary error. */
+
+  #ifdef SUPPORT_TLS
+  TLS_FAILED:
+  code = '4';
+  #endif
+
+  /* If the failure happened while setting up the call, see if the failure was
+  a 5xx response (this will either be on connection, or following HELO - a 5xx
+  after EHLO causes it to try HELO). If so, fail all addresses, as this host is
+  never going to accept them. For other errors during setting up (timeouts or
+  whatever), defer all addresses, and yield DEFER, so that the host is not
+  tried again for a while. */
+
+  FAILED:
+  ok = FALSE;                /* For when reached by GOTO */
+
+  if (setting_up)
+    {
+    if (code == '5')
+      {
+      set_errno(addrlist, save_errno, message, FAIL, pass_message);
+      }
+    else
+      {
+      set_errno(addrlist, save_errno, message, DEFER, pass_message);
+      yield = DEFER;
+      }
+    }
+
+  /* We want to handle timeouts after MAIL or "." and loss of connection after
+  "." specially. They can indicate a problem with the sender address or with
+  the contents of the message rather than a real error on the connection. These
+  cases are treated in the same way as a 4xx response. This next bit of code
+  does the classification. */
+
+  else
+    {
+    BOOL message_error;
+
+    switch(save_errno)
+      {
+      case 0:
+      case ERRNO_MAIL4XX:
+      case ERRNO_DATA4XX:
+      message_error = TRUE;
+      break;
+
+      case ETIMEDOUT:
+      message_error = Ustrncmp(smtp_command,"MAIL",4) == 0 ||
+                      Ustrncmp(smtp_command,"end ",4) == 0;
+      break;
+
+      case ERRNO_SMTPCLOSED:
+      message_error = Ustrncmp(smtp_command,"end ",4) == 0;
+      break;
+
+      default:
+      message_error = FALSE;
+      break;
+      }
+
+    /* Handle the cases that are treated as message errors. These are:
+
+      (a) negative response or timeout after MAIL
+      (b) negative response after DATA
+      (c) negative response or timeout or dropped connection after "."
+
+    It won't be a negative response or timeout after RCPT, as that is dealt
+    with separately above. The action in all cases is to set an appropriate
+    error code for all the addresses, but to leave yield set to OK because the
+    host itself has not failed. Of course, it might in practice have failed
+    when we've had a timeout, but if so, we'll discover that at the next
+    delivery attempt. For a temporary error, set the message_defer flag, and
+    write to the logs for information if this is not the last host. The error
+    for the last host will be logged as part of the address's log line. */
+
+    if (message_error)
+      {
+      if (mua_wrapper) code = '5';  /* Force hard failure in wrapper mode */
+      set_errno(addrlist, save_errno, message, (code == '5')? FAIL : DEFER,
+        pass_message);
+
+      /* If there's an errno, the message contains just the identity of
+      the host. */
+
+      if (code != '5')     /* Anything other than 5 is treated as temporary */
+        {
+        if (save_errno > 0)
+          message = US string_sprintf("%s: %s", message, strerror(save_errno));
+        if (host->next != NULL) log_write(0, LOG_MAIN, "%s", message);
+        deliver_msglog("%s %s\n", tod_stamp(tod_log), message);
+        *message_defer = TRUE;
+        }
+      }
+
+    /* Otherwise, we have an I/O error or a timeout other than after MAIL or
+    ".", or some other transportation error. We defer all addresses and yield
+    DEFER, except for the case of failed add_headers expansion, or a transport
+    filter failure, when the yield should be ERROR, to stop it trying other
+    hosts. */
+
+    else
+      {
+      yield = (save_errno == ERRNO_CHHEADER_FAIL ||
+               save_errno == ERRNO_FILTER_FAIL)? ERROR : DEFER;
+      set_errno(addrlist, save_errno, message, DEFER, pass_message);
+      }
+    }
+  }
+
+
+/* If all has gone well, send_quit will be set TRUE, implying we can end the
+SMTP session tidily. However, if there were too many addresses to send in one
+message (indicated by first_addr being non-NULL) we want to carry on with the
+rest of them. Also, it is desirable to send more than one message down the SMTP
+connection if there are several waiting, provided we haven't already sent so
+many as to hit the configured limit. The function transport_check_waiting looks
+for a waiting message and returns its id. Then transport_pass_socket tries to
+set up a continued delivery by passing the socket on to another process. The
+variable send_rset is FALSE if a message has just been successfully transfered.
+
+If we are already sending down a continued channel, there may be further
+addresses not yet delivered that are aimed at the same host, but which have not
+been passed in this run of the transport. In this case, continue_more will be
+true, and all we should do is send RSET if necessary, and return, leaving the
+channel open.
+
+However, if no address was disposed of, i.e. all addresses got 4xx errors, we
+do not want to continue with other messages down the same channel, because that
+can lead to looping between two or more messages, all with the same,
+temporarily failing address(es). [The retry information isn't updated yet, so
+new processes keep on trying.] We probably also don't want to try more of this
+message's addresses either.
+
+If we have started a TLS session, we have to end it before passing the
+connection to a new process. However, not all servers can handle this (Exim
+can), so we do not pass such a connection on if the host matches
+hosts_nopass_tls. */
+
+DEBUG(D_transport)
+  debug_printf("ok=%d send_quit=%d send_rset=%d continue_more=%d "
+    "yield=%d first_address is %sNULL\n", ok, send_quit, send_rset,
+    continue_more, yield, (first_addr == NULL)? "":"not ");
+
+if (completed_address && ok && send_quit)
+  {
+  BOOL more;
+  if (first_addr != NULL || continue_more ||
+        (
+           (tls_out.active < 0 ||
+           verify_check_this_host(&(ob->hosts_nopass_tls), NULL, host->name,
+             host->address, NULL) != OK)
+        &&
+           transport_check_waiting(tblock->name, host->name,
+             tblock->connection_max_messages, new_message_id, &more)
+        ))
+    {
+    uschar *msg;
+    BOOL pass_message;
+
+    if (send_rset)
+      {
+      if (! (ok = smtp_write_command(&outblock, FALSE, "RSET\r\n") >= 0))
+        {
+        msg = US string_sprintf("send() to %s [%s] failed: %s", host->name,
+          host->address, strerror(save_errno));
+        send_quit = FALSE;
+        }
+      else if (! (ok = smtp_read_response(&inblock, buffer, sizeof(buffer), '2',
+                  ob->command_timeout)))
+        {
+        int code;
+        send_quit = check_response(host, &errno, 0, buffer, &code, &msg,
+          &pass_message);
+        if (!send_quit)
+          {
+          DEBUG(D_transport) debug_printf("%s\n", msg);
+          }
+        }
+      }
+
+    /* Either RSET was not needed, or it succeeded */
+
+    if (ok)
+      {
+      if (first_addr != NULL)            /* More addresses still to be sent */
+        {                                /*   in this run of the transport */
+        continue_sequence++;             /* Causes * in logging */
+        goto SEND_MESSAGE;
+        }
+      if (continue_more) return yield;   /* More addresses for another run */
+
+      /* Pass the socket to a new Exim process. Before doing so, we must shut
+      down TLS. Not all MTAs allow for the continuation of the SMTP session
+      when TLS is shut down. We test for this by sending a new EHLO. If we
+      don't get a good response, we don't attempt to pass the socket on. */
+
+      #ifdef SUPPORT_TLS
+      if (tls_out.active >= 0)
+        {
+        tls_close(FALSE, TRUE);
+        if (smtps)
+          ok = FALSE;
+        else
+          ok = smtp_write_command(&outblock,FALSE,"EHLO %s\r\n",helo_data) >= 0 &&
+               smtp_read_response(&inblock, buffer, sizeof(buffer), '2',
+                 ob->command_timeout);
+        }
+      #endif
+
+      /* If the socket is successfully passed, we musn't send QUIT (or
+      indeed anything!) from here. */
+
+      if (ok && transport_pass_socket(tblock->name, host->name, host->address,
+            new_message_id, inblock.sock))
+        {
+        send_quit = FALSE;
+        }
+      }
+
+    /* If RSET failed and there are addresses left, they get deferred. */
+
+    else set_errno(first_addr, errno, msg, DEFER, FALSE);
+    }
+  }
+
+/* End off tidily with QUIT unless the connection has died or the socket has
+been passed to another process. There has been discussion on the net about what
+to do after sending QUIT. The wording of the RFC suggests that it is necessary
+to wait for a response, but on the other hand, there isn't anything one can do
+with an error response, other than log it. Exim used to do that. However,
+further discussion suggested that it is positively advantageous not to wait for
+the response, but to close the session immediately. This is supposed to move
+the TCP/IP TIME_WAIT state from the server to the client, thereby removing some
+load from the server. (Hosts that are both servers and clients may not see much
+difference, of course.) Further discussion indicated that this was safe to do
+on Unix systems which have decent implementations of TCP/IP that leave the
+connection around for a while (TIME_WAIT) after the application has gone away.
+This enables the response sent by the server to be properly ACKed rather than
+timed out, as can happen on broken TCP/IP implementations on other OS.
+
+This change is being made on 31-Jul-98. After over a year of trouble-free
+operation, the old commented-out code was removed on 17-Sep-99. */
+
+SEND_QUIT:
+if (send_quit) (void)smtp_write_command(&outblock, FALSE, "QUIT\r\n");
+
+END_OFF:
+
+#ifdef SUPPORT_TLS
+tls_close(FALSE, TRUE);
+#endif
+
+/* Close the socket, and return the appropriate value, first setting
+works because the NULL setting is passed back to the calling process, and
+remote_max_parallel is forced to 1 when delivering over an existing connection,
+
+If all went well and continue_more is set, we shouldn't actually get here if
+there are further addresses, as the return above will be taken. However,
+writing RSET might have failed, or there may be other addresses whose hosts are
+specified in the transports, and therefore not visible at top level, in which
+case continue_more won't get set. */
+
+(void)close(inblock.sock);
+continue_transport = NULL;
+continue_hostname = NULL;
+return yield;
+}
+
+
+
+
+/*************************************************
+*              Closedown entry point             *
+*************************************************/
+
+/* This function is called when exim is passed an open smtp channel
+from another incarnation, but the message which it has been asked
+to deliver no longer exists. The channel is on stdin.
+
+We might do fancy things like looking for another message to send down
+the channel, but if the one we sought has gone, it has probably been
+delivered by some other process that itself will seek further messages,
+so just close down our connection.
+
+Argument:   pointer to the transport instance block
+Returns:    nothing
+*/
+
+void
+smtp_transport_closedown(transport_instance *tblock)
+{
+smtp_transport_options_block *ob =
+  (smtp_transport_options_block *)(tblock->options_block);
+smtp_inblock inblock;
+smtp_outblock outblock;
+uschar buffer[256];
+uschar inbuffer[4096];
+uschar outbuffer[16];
+
+inblock.sock = fileno(stdin);
+inblock.buffer = inbuffer;
+inblock.buffersize = sizeof(inbuffer);
+inblock.ptr = inbuffer;
+inblock.ptrend = inbuffer;
+
+outblock.sock = inblock.sock;
+outblock.buffersize = sizeof(outbuffer);
+outblock.buffer = outbuffer;
+outblock.ptr = outbuffer;
+outblock.cmd_count = 0;
+outblock.authenticating = FALSE;
+
+(void)smtp_write_command(&outblock, FALSE, "QUIT\r\n");
+(void)smtp_read_response(&inblock, buffer, sizeof(buffer), '2',
+  ob->command_timeout);
+(void)close(inblock.sock);
+}
+
+
+
+/*************************************************
+*            Prepare addresses for delivery      *
+*************************************************/
+
+/* This function is called to flush out error settings from previous delivery
+attempts to other hosts. It also records whether we got here via an MX record
+or not in the more_errno field of the address. We are interested only in
+addresses that are still marked DEFER - others may have got delivered to a
+previously considered IP address. Set their status to PENDING_DEFER to indicate
+which ones are relevant this time.
+
+Arguments:
+  addrlist     the list of addresses
+  host         the host we are delivering to
+
+Returns:       the first address for this delivery
+*/
+
+static address_item *
+prepare_addresses(address_item *addrlist, host_item *host)
+{
+address_item *first_addr = NULL;
+address_item *addr;
+for (addr = addrlist; addr != NULL; addr = addr->next)
+  {
+  if (addr->transport_return != DEFER) continue;
+  if (first_addr == NULL) first_addr = addr;
+  addr->transport_return = PENDING_DEFER;
+  addr->basic_errno = 0;
+  addr->more_errno = (host->mx >= 0)? 'M' : 'A';
+  addr->message = NULL;
+  #ifdef SUPPORT_TLS
+  addr->cipher = NULL;
+  addr->ourcert = NULL;
+  addr->peercert = NULL;
+  addr->peerdn = NULL;
+  addr->ocsp = OCSP_NOT_REQ;
+  #endif
+  }
+return first_addr;
+}
+
+
+
+/*************************************************
+*              Main entry point                  *
+*************************************************/
+
+/* See local README for interface details. As this is a remote transport, it is
+given a chain of addresses to be delivered in one connection, if possible. It
+always returns TRUE, indicating that each address has its own independent
+status set, except if there is a setting up problem, in which case it returns
+FALSE. */
+
+BOOL
+smtp_transport_entry(
+  transport_instance *tblock,      /* data for this instantiation */
+  address_item *addrlist)          /* addresses we are working on */
+{
+int cutoff_retry;
+int port;
+int hosts_defer = 0;
+int hosts_fail  = 0;
+int hosts_looked_up = 0;
+int hosts_retry = 0;
+int hosts_serial = 0;
+int hosts_total = 0;
+int total_hosts_tried = 0;
+address_item *addr;
+BOOL expired = TRUE;
+BOOL continuing = continue_hostname != NULL;
+uschar *expanded_hosts = NULL;
+uschar *pistring;
+uschar *tid = string_sprintf("%s transport", tblock->name);
+smtp_transport_options_block *ob =
+  (smtp_transport_options_block *)(tblock->options_block);
+host_item *hostlist = addrlist->host_list;
+host_item *host = NULL;
+
+DEBUG(D_transport)
+  {
+  debug_printf("%s transport entered\n", tblock->name);
+  for (addr = addrlist; addr != NULL; addr = addr->next)
+    debug_printf("  %s\n", addr->address);
+  if (continuing) debug_printf("already connected to %s [%s]\n",
+      continue_hostname, continue_host_address);
+  }
+
+/* Set the flag requesting that these hosts be added to the waiting
+database if the delivery fails temporarily or if we are running with
+queue_smtp or a 2-stage queue run. This gets unset for certain
+kinds of error, typically those that are specific to the message. */
+
+update_waiting =  TRUE;
+
+/* If a host list is not defined for the addresses - they must all have the
+same one in order to be passed to a single transport - or if the transport has
+a host list with hosts_override set, use the host list supplied with the
+transport. It is an error for this not to exist. */
+
+if (hostlist == NULL || (ob->hosts_override && ob->hosts != NULL))
+  {
+  if (ob->hosts == NULL)
+    {
+    addrlist->message = string_sprintf("%s transport called with no hosts set",
+      tblock->name);
+    addrlist->transport_return = PANIC;
+    return FALSE;   /* Only top address has status */
+    }
+
+  DEBUG(D_transport) debug_printf("using the transport's hosts: %s\n",
+    ob->hosts);
+
+  /* If the transport's host list contains no '$' characters, and we are not
+  randomizing, it is fixed and therefore a chain of hosts can be built once
+  and for all, and remembered for subsequent use by other calls to this
+  transport. If, on the other hand, the host list does contain '$', or we are
+  randomizing its order, we have to rebuild it each time. In the fixed case,
+  as the hosts string will never be used again, it doesn't matter that we
+  replace all the : characters with zeros. */
+
+  if (ob->hostlist == NULL)
+    {
+    uschar *s = ob->hosts;
+
+    if (Ustrchr(s, '$') != NULL)
+      {
+      expanded_hosts = expand_string(s);
+      if (expanded_hosts == NULL)
+        {
+        addrlist->message = string_sprintf("failed to expand list of hosts "
+          "\"%s\" in %s transport: %s", s, tblock->name, expand_string_message);
+        addrlist->transport_return = search_find_defer? DEFER : PANIC;
+        return FALSE;     /* Only top address has status */
+        }
+      DEBUG(D_transport) debug_printf("expanded list of hosts \"%s\" to "
+        "\"%s\"\n", s, expanded_hosts);
+      s = expanded_hosts;
+      }
+    else
+      if (ob->hosts_randomize) s = expanded_hosts = string_copy(s);
+
+    host_build_hostlist(&hostlist, s, ob->hosts_randomize);
+
+    /* Check that the expansion yielded something useful. */
+    if (hostlist == NULL)
+      {
+      addrlist->message =
+        string_sprintf("%s transport has empty hosts setting", tblock->name);
+      addrlist->transport_return = PANIC;
+      return FALSE;   /* Only top address has status */
+      }
+
+    /* If there was no expansion of hosts, save the host list for
+    next time. */
+
+    if (expanded_hosts == NULL) ob->hostlist = hostlist;
+    }
+
+  /* This is not the first time this transport has been run in this delivery;
+  the host list was built previously. */
+
+  else hostlist = ob->hostlist;
+  }
+
+/* The host list was supplied with the address. If hosts_randomize is set, we
+must sort it into a random order if it did not come from MX records and has not
+already been randomized (but don't bother if continuing down an existing
+connection). */
+
+else if (ob->hosts_randomize && hostlist->mx == MX_NONE && !continuing)
+  {
+  host_item *newlist = NULL;
+  while (hostlist != NULL)
+    {
+    host_item *h = hostlist;
+    hostlist = hostlist->next;
+
+    h->sort_key = random_number(100);
+
+    if (newlist == NULL)
+      {
+      h->next = NULL;
+      newlist = h;
+      }
+    else if (h->sort_key < newlist->sort_key)
+      {
+      h->next = newlist;
+      newlist = h;
+      }
+    else
+      {
+      host_item *hh = newlist;
+      while (hh->next != NULL)
+        {
+        if (h->sort_key < hh->next->sort_key) break;
+        hh = hh->next;
+        }
+      h->next = hh->next;
+      hh->next = h;
+      }
+    }
+
+  hostlist = addrlist->host_list = newlist;
+  }
+
+
+/* Sort out the default port.  */
+
+if (!smtp_get_port(ob->port, addrlist, &port, tid)) return FALSE;
+
+
+/* For each host-plus-IP-address on the list:
+
+.  If this is a continued delivery and the host isn't the one with the
+   current connection, skip.
+
+.  If the status is unusable (i.e. previously failed or retry checked), skip.
+
+.  If no IP address set, get the address, either by turning the name into
+   an address, calling gethostbyname if gethostbyname is on, or by calling
+   the DNS. The DNS may yield multiple addresses, in which case insert the
+   extra ones into the list.
+
+.  Get the retry data if not previously obtained for this address and set the
+   field which remembers the state of this address. Skip if the retry time is
+   not reached. If not, remember whether retry data was found. The retry string
+   contains both the name and the IP address.
+
+.  Scan the list of addresses and mark those whose status is DEFER as
+   PENDING_DEFER. These are the only ones that will be processed in this cycle
+   of the hosts loop.
+
+.  Make a delivery attempt - addresses marked PENDING_DEFER will be tried.
+   Some addresses may be successfully delivered, others may fail, and yet
+   others may get temporary errors and so get marked DEFER.
+
+.  The return from the delivery attempt is OK if a connection was made and a
+   valid SMTP dialogue was completed. Otherwise it is DEFER.
+
+.  If OK, add a "remove" retry item for this host/IPaddress, if any.
+
+.  If fail to connect, or other defer state, add a retry item.
+
+.  If there are any addresses whose status is still DEFER, carry on to the
+   next host/IPaddress, unless we have tried the number of hosts given
+   by hosts_max_try or hosts_max_try_hardlimit; otherwise return. Note that
+   there is some fancy logic for hosts_max_try that means its limit can be
+   overstepped in some circumstances.
+
+If we get to the end of the list, all hosts have deferred at least one address,
+or not reached their retry times. If delay_after_cutoff is unset, it requests a
+delivery attempt to those hosts whose last try was before the arrival time of
+the current message. To cope with this, we have to go round the loop a second
+time. After that, set the status and error data for any addresses that haven't
+had it set already. */
+
+for (cutoff_retry = 0; expired &&
+     cutoff_retry < ((ob->delay_after_cutoff)? 1 : 2);
+     cutoff_retry++)
+  {
+  host_item *nexthost = NULL;
+  int unexpired_hosts_tried = 0;
+
+  for (host = hostlist;
+       host != NULL &&
+         unexpired_hosts_tried < ob->hosts_max_try &&
+         total_hosts_tried < ob->hosts_max_try_hardlimit;
+       host = nexthost)
+    {
+    int rc;
+    int host_af;
+    uschar *rs;
+    BOOL serialized = FALSE;
+    BOOL host_is_expired = FALSE;
+    BOOL message_defer = FALSE;
+    BOOL ifchanges = FALSE;
+    BOOL some_deferred = FALSE;
+    address_item *first_addr = NULL;
+    uschar *interface = NULL;
+    uschar *retry_host_key = NULL;
+    uschar *retry_message_key = NULL;
+    uschar *serialize_key = NULL;
+
+    /* Default next host is next host. :-) But this can vary if the
+    hosts_max_try limit is hit (see below). It may also be reset if a host
+    address is looked up here (in case the host was multihomed). */
+
+    nexthost = host->next;
+
+    /* If the address hasn't yet been obtained from the host name, look it up
+    now, unless the host is already marked as unusable. If it is marked as
+    unusable, it means that the router was unable to find its IP address (in
+    the DNS or wherever) OR we are in the 2nd time round the cutoff loop, and
+    the lookup failed last time. We don't get this far if *all* MX records
+    point to non-existent hosts; that is treated as a hard error.
+
+    We can just skip this host entirely. When the hosts came from the router,
+    the address will timeout based on the other host(s); when the address is
+    looked up below, there is an explicit retry record added.
+
+    Note that we mustn't skip unusable hosts if the address is not unset; they
+    may be needed as expired hosts on the 2nd time round the cutoff loop. */
+
+    if (host->address == NULL)
+      {
+      int new_port, flags;
+      host_item *hh;
+      uschar *canonical_name;
+
+      if (host->status >= hstatus_unusable)
+        {
+        DEBUG(D_transport) debug_printf("%s has no address and is unusable - skipping\n",
+          host->name);
+        continue;
+        }
+
+      DEBUG(D_transport) debug_printf("getting address for %s\n", host->name);
+
+      /* The host name is permitted to have an attached port. Find it, and
+      strip it from the name. Just remember it for now. */
+
+      new_port = host_item_get_port(host);
+
+      /* Count hosts looked up */
+
+      hosts_looked_up++;
+
+      /* Find by name if so configured, or if it's an IP address. We don't
+      just copy the IP address, because we need the test-for-local to happen. */
+
+      flags = HOST_FIND_BY_A;
+      if (ob->dns_qualify_single) flags |= HOST_FIND_QUALIFY_SINGLE;
+      if (ob->dns_search_parents) flags |= HOST_FIND_SEARCH_PARENTS;
+
+      if (ob->gethostbyname || string_is_ip_address(host->name, NULL) != 0)
+        rc = host_find_byname(host, NULL, flags, &canonical_name, TRUE);
+      else
+        rc = host_find_bydns(host, NULL, flags, NULL, NULL, NULL,
+         ob->dnssec_request_domains, ob->dnssec_require_domains,
+          &canonical_name, NULL);
+
+      /* Update the host (and any additional blocks, resulting from
+      multihoming) with a host-specific port, if any. */
+
+      for (hh = host; hh != nexthost; hh = hh->next) hh->port = new_port;
+
+      /* Failure to find the host at this time (usually DNS temporary failure)
+      is really a kind of routing failure rather than a transport failure.
+      Therefore we add a retry item of the routing kind, not to stop us trying
+      to look this name up here again, but to ensure the address gets timed
+      out if the failures go on long enough. A complete failure at this point
+      commonly points to a configuration error, but the best action is still
+      to carry on for the next host. */
+
+      if (rc == HOST_FIND_AGAIN || rc == HOST_FIND_FAILED)
+        {
+        retry_add_item(addrlist, string_sprintf("R:%s", host->name), 0);
+        expired = FALSE;
+        if (rc == HOST_FIND_AGAIN) hosts_defer++; else hosts_fail++;
+        DEBUG(D_transport) debug_printf("rc = %s for %s\n", (rc == HOST_FIND_AGAIN)?
+          "HOST_FIND_AGAIN" : "HOST_FIND_FAILED", host->name);
+        host->status = hstatus_unusable;
+
+        for (addr = addrlist; addr != NULL; addr = addr->next)
+          {
+          if (addr->transport_return != DEFER) continue;
+          addr->basic_errno = ERRNO_UNKNOWNHOST;
+          addr->message =
+            string_sprintf("failed to lookup IP address for %s", host->name);
+          }
+        continue;
+        }
+
+      /* If the host is actually the local host, we may have a problem, or
+      there may be some cunning configuration going on. In the problem case,
+      log things and give up. The default transport status is already DEFER. */
+
+      if (rc == HOST_FOUND_LOCAL && !ob->allow_localhost)
+        {
+        for (addr = addrlist; addr != NULL; addr = addr->next)
+          {
+          addr->basic_errno = 0;
+          addr->message = string_sprintf("%s transport found host %s to be "
+            "local", tblock->name, host->name);
+          }
+        goto END_TRANSPORT;
+        }
+      }   /* End of block for IP address lookup */
+
+    /* If this is a continued delivery, we are interested only in the host
+    which matches the name of the existing open channel. The check is put
+    here after the local host lookup, in case the name gets expanded as a
+    result of the lookup. Set expired FALSE, to save the outer loop executing
+    twice. */
+
+    if (continuing && (Ustrcmp(continue_hostname, host->name) != 0 ||
+                       Ustrcmp(continue_host_address, host->address) != 0))
+      {
+      expired = FALSE;
+      continue;      /* With next host */
+      }
+
+    /* Reset the default next host in case a multihomed host whose addresses
+    are not looked up till just above added to the host list. */
+
+    nexthost = host->next;
+
+    /* If queue_smtp is set (-odqs or the first part of a 2-stage run), or the
+    domain is in queue_smtp_domains, we don't actually want to attempt any
+    deliveries. When doing a queue run, queue_smtp_domains is always unset. If
+    there is a lookup defer in queue_smtp_domains, proceed as if the domain
+    were not in it. We don't want to hold up all SMTP deliveries! Except when
+    doing a two-stage queue run, don't do this if forcing. */
+
+    if ((!deliver_force || queue_2stage) && (queue_smtp ||
+        match_isinlist(addrlist->domain, &queue_smtp_domains, 0,
+          &domainlist_anchor, NULL, MCL_DOMAIN, TRUE, NULL) == OK))
+      {
+      expired = FALSE;
+      for (addr = addrlist; addr != NULL; addr = addr->next)
+        {
+        if (addr->transport_return != DEFER) continue;
+        addr->message = US"domain matches queue_smtp_domains, or -odqs set";
+        }
+      continue;      /* With next host */
+      }
+
+    /* Count hosts being considered - purely for an intelligent comment
+    if none are usable. */
+
+    hosts_total++;
+
+    /* Set $host and $host address now in case they are needed for the
+    interface expansion or the serialize_hosts check; they remain set if an
+    actual delivery happens. */
+
+    deliver_host = host->name;
+    deliver_host_address = host->address;
+    lookup_dnssec_authenticated = host->dnssec == DS_YES ? US"yes"
+                               : host->dnssec == DS_NO ? US"no"
+                               : US"";
+
+    /* Set up a string for adding to the retry key if the port number is not
+    the standard SMTP port. A host may have its own port setting that overrides
+    the default. */
+
+    pistring = string_sprintf(":%d", (host->port == PORT_NONE)?
+      port : host->port);
+    if (Ustrcmp(pistring, ":25") == 0) pistring = US"";
+
+    /* Select IPv4 or IPv6, and choose an outgoing interface. If the interface
+    string changes upon expansion, we must add it to the key that is used for
+    retries, because connections to the same host from a different interface
+    should be treated separately. */
+
+    host_af = (Ustrchr(host->address, ':') == NULL)? AF_INET : AF_INET6;
+    if (!smtp_get_interface(ob->interface, host_af, addrlist, &ifchanges,
+         &interface, tid))
+      return FALSE;
+    if (ifchanges) pistring = string_sprintf("%s/%s", pistring, interface);
+
+    /* The first time round the outer loop, check the status of the host by
+    inspecting the retry data. The second time round, we are interested only
+    in expired hosts that haven't been tried since this message arrived. */
+
+    if (cutoff_retry == 0)
+      {
+      /* Ensure the status of the address is set by checking retry data if
+      necessary. There maybe host-specific retry data (applicable to all
+      messages) and also data for retries of a specific message at this host.
+      If either of these retry records are actually read, the keys used are
+      returned to save recomputing them later. */
+
+      host_is_expired = retry_check_address(addrlist->domain, host, pistring,
+        ob->retry_include_ip_address, &retry_host_key, &retry_message_key);
+
+      DEBUG(D_transport) debug_printf("%s [%s]%s status = %s\n", host->name,
+        (host->address == NULL)? US"" : host->address, pistring,
+        (host->status == hstatus_usable)? "usable" :
+        (host->status == hstatus_unusable)? "unusable" :
+        (host->status == hstatus_unusable_expired)? "unusable (expired)" : "?");
+
+      /* Skip this address if not usable at this time, noting if it wasn't
+      actually expired, both locally and in the address. */
+
+      switch (host->status)
+        {
+        case hstatus_unusable:
+        expired = FALSE;
+        setflag(addrlist, af_retry_skipped);
+        /* Fall through */
+
+        case hstatus_unusable_expired:
+        switch (host->why)
+          {
+          case hwhy_retry: hosts_retry++; break;
+          case hwhy_failed:  hosts_fail++; break;
+          case hwhy_deferred: hosts_defer++; break;
+          }
+
+        /* If there was a retry message key, implying that previously there
+        was a message-specific defer, we don't want to update the list of
+        messages waiting for these hosts. */
+
+        if (retry_message_key != NULL) update_waiting = FALSE;
+        continue;   /* With the next host or IP address */
+        }
+      }
+
+    /* Second time round the loop: if the address is set but expired, and
+    the message is newer than the last try, let it through. */
+
+    else
+      {
+      if (host->address == NULL ||
+          host->status != hstatus_unusable_expired ||
+          host->last_try > received_time)
+        continue;
+      DEBUG(D_transport)
+        debug_printf("trying expired host %s [%s]%s\n",
+          host->name, host->address, pistring);
+      host_is_expired = TRUE;
+      }
+
+    /* Setting "expired=FALSE" doesn't actually mean not all hosts are expired;
+    it remains TRUE only if all hosts are expired and none are actually tried.
+    */
+
+    expired = FALSE;
+
+    /* If this host is listed as one to which access must be serialized,
+    see if another Exim process has a connection to it, and if so, skip
+    this host. If not, update the database to record our connection to it
+    and remember this for later deletion. Do not do any of this if we are
+    sending the message down a pre-existing connection. */
+
+    if (!continuing &&
+        verify_check_this_host(&(ob->serialize_hosts), NULL, host->name,
+          host->address, NULL) == OK)
+      {
+      serialize_key = string_sprintf("host-serialize-%s", host->name);
+      if (!enq_start(serialize_key))
+        {
+        DEBUG(D_transport)
+          debug_printf("skipping host %s because another Exim process "
+            "is connected to it\n", host->name);
+        hosts_serial++;
+        continue;
+        }
+      serialized = TRUE;
+      }
+
+    /* OK, we have an IP address that is not waiting for its retry time to
+    arrive (it might be expired) OR (second time round the loop) we have an
+    expired host that hasn't been tried since the message arrived. Have a go
+    at delivering the message to it. First prepare the addresses by flushing
+    out the result of previous attempts, and finding the first address that
+    is still to be delivered. */
+
+    first_addr = prepare_addresses(addrlist, host);
+
+    DEBUG(D_transport) debug_printf("delivering %s to %s [%s] (%s%s)\n",
+      message_id, host->name, host->address, addrlist->address,
+      (addrlist->next == NULL)? "" : ", ...");
+
+    set_process_info("delivering %s to %s [%s] (%s%s)",
+      message_id, host->name, host->address, addrlist->address,
+      (addrlist->next == NULL)? "" : ", ...");
+
+    /* This is not for real; don't do the delivery. If there are
+    any remaining hosts, list them. */
+
+    if (dont_deliver)
+      {
+      host_item *host2;
+      set_errno(addrlist, 0, NULL, OK, FALSE);
+      for (addr = addrlist; addr != NULL; addr = addr->next)
+        {
+        addr->host_used = host;
+        addr->special_action = '*';
+        addr->message = US"delivery bypassed by -N option";
+        }
+      DEBUG(D_transport)
+        {
+        debug_printf("*** delivery by %s transport bypassed by -N option\n"
+                     "*** host and remaining hosts:\n", tblock->name);
+        for (host2 = host; host2 != NULL; host2 = host2->next)
+          debug_printf("    %s [%s]\n", host2->name,
+            (host2->address == NULL)? US"unset" : host2->address);
+        }
+      rc = OK;
+      }
+
+    /* This is for real. If the host is expired, we don't count it for
+    hosts_max_retry. This ensures that all hosts must expire before an address
+    is timed out, unless hosts_max_try_hardlimit (which protects against
+    lunatic DNS configurations) is reached.
+
+    If the host is not expired and we are about to hit the hosts_max_retry
+    limit, check to see if there is a subsequent hosts with a different MX
+    value. If so, make that the next host, and don't count this one. This is a
+    heuristic to make sure that different MXs do get tried. With a normal kind
+    of retry rule, they would get tried anyway when the earlier hosts were
+    delayed, but if the domain has a "retry every time" type of rule - as is
+    often used for the the very large ISPs, that won't happen. */
+
+    else
+      {
+      if (!host_is_expired && ++unexpired_hosts_tried >= ob->hosts_max_try)
+        {
+        host_item *h;
+        DEBUG(D_transport)
+          debug_printf("hosts_max_try limit reached with this host\n");
+        for (h = host; h != NULL; h = h->next)
+          if (h->mx != host->mx) break;
+        if (h != NULL)
+          {
+          nexthost = h;
+          unexpired_hosts_tried--;
+          DEBUG(D_transport) debug_printf("however, a higher MX host exists "
+            "and will be tried\n");
+          }
+        }
+
+      /* Attempt the delivery. */
+
+      total_hosts_tried++;
+      rc = smtp_deliver(addrlist, host, host_af, port, interface, tblock,
+        expanded_hosts != NULL, &message_defer, FALSE);
+
+      /* Yield is one of:
+         OK     => connection made, each address contains its result;
+                     message_defer is set for message-specific defers (when all
+                     recipients are marked defer)
+         DEFER  => there was a non-message-specific delivery problem;
+         ERROR  => there was a problem setting up the arguments for a filter,
+                   or there was a problem with expanding added headers
+      */
+
+      /* If the result is not OK, there was a non-message-specific problem.
+      If the result is DEFER, we need to write to the logs saying what happened
+      for this particular host, except in the case of authentication and TLS
+      failures, where the log has already been written. If all hosts defer a
+      general message is written at the end. */
+
+      if (rc == DEFER && first_addr->basic_errno != ERRNO_AUTHFAIL &&
+                         first_addr->basic_errno != ERRNO_TLSFAILURE)
+        write_logs(first_addr, host);
+
+      #ifdef EXPERIMENTAL_TPDA
+      if (rc == DEFER)
+        tpda_deferred(ob, first_addr, host);
+      #endif
+
+      /* If STARTTLS was accepted, but there was a failure in setting up the
+      TLS session (usually a certificate screwup), and the host is not in
+      hosts_require_tls, and tls_tempfail_tryclear is true, try again, with
+      TLS forcibly turned off. We have to start from scratch with a new SMTP
+      connection. That's why the retry is done from here, not from within
+      smtp_deliver(). [Rejections of STARTTLS itself don't screw up the
+      session, so the in-clear transmission after those errors, if permitted,
+      happens inside smtp_deliver().] */
+
+      #ifdef SUPPORT_TLS
+      if (rc == DEFER && first_addr->basic_errno == ERRNO_TLSFAILURE &&
+           ob->tls_tempfail_tryclear &&
+           verify_check_this_host(&(ob->hosts_require_tls), NULL, host->name,
+             host->address, NULL) != OK)
+        {
+        log_write(0, LOG_MAIN, "TLS session failure: delivering unencrypted "
+          "to %s [%s] (not in hosts_require_tls)", host->name, host->address);
+        first_addr = prepare_addresses(addrlist, host);
+        rc = smtp_deliver(addrlist, host, host_af, port, interface, tblock,
+          expanded_hosts != NULL, &message_defer, TRUE);
+        if (rc == DEFER && first_addr->basic_errno != ERRNO_AUTHFAIL)
+          write_logs(first_addr, host);
+        #ifdef EXPERIMENTAL_TPDA
+        if (rc == DEFER)
+          tpda_deferred(ob, first_addr, host);
+        #endif
+        }
+      #endif
+      }
+
+    /* Delivery attempt finished */
+
+    rs = (rc == OK)? US"OK" : (rc == DEFER)? US"DEFER" : (rc == ERROR)?
+      US"ERROR" : US"?";
+
+    set_process_info("delivering %s: just tried %s [%s] for %s%s: result %s",
+      message_id, host->name, host->address, addrlist->address,
+      (addrlist->next == NULL)? "" : " (& others)", rs);
+
+    /* Release serialization if set up */
+
+    if (serialized) enq_end(serialize_key);
+
+    /* If the result is DEFER, or if a host retry record is known to exist, we
+    need to add an item to the retry chain for updating the retry database
+    at the end of delivery. We only need to add the item to the top address,
+    of course. Also, if DEFER, we mark the IP address unusable so as to skip it
+    for any other delivery attempts using the same address. (It is copied into
+    the unusable tree at the outer level, so even if different address blocks
+    contain the same address, it still won't get tried again.) */
+
+    if (rc == DEFER || retry_host_key != NULL)
+      {
+      int delete_flag = (rc != DEFER)? rf_delete : 0;
+      if (retry_host_key == NULL)
+        {
+        retry_host_key = ob->retry_include_ip_address?
+          string_sprintf("T:%S:%s%s", host->name, host->address, pistring) :
+          string_sprintf("T:%S%s", host->name, pistring);
+        }
+
+      /* If a delivery of another message over an existing SMTP connection
+      yields DEFER, we do NOT set up retry data for the host. This covers the
+      case when there are delays in routing the addresses in the second message
+      that are so long that the server times out. This is alleviated by not
+      routing addresses that previously had routing defers when handling an
+      existing connection, but even so, this case may occur (e.g. if a
+      previously happily routed address starts giving routing defers). If the
+      host is genuinely down, another non-continued message delivery will
+      notice it soon enough. */
+
+      if (delete_flag != 0 || !continuing)
+        retry_add_item(first_addr, retry_host_key, rf_host | delete_flag);
+
+      /* We may have tried an expired host, if its retry time has come; ensure
+      the status reflects the expiry for the benefit of any other addresses. */
+
+      if (rc == DEFER)
+        {
+        host->status = (host_is_expired)?
+          hstatus_unusable_expired : hstatus_unusable;
+        host->why = hwhy_deferred;
+        }
+      }
+
+    /* If message_defer is set (host was OK, but every recipient got deferred
+    because of some message-specific problem), or if that had happened
+    previously so that a message retry key exists, add an appropriate item
+    to the retry chain. Note that if there was a message defer but now there is
+    a host defer, the message defer record gets deleted. That seems perfectly
+    reasonable. Also, stop the message from being remembered as waiting
+    for specific hosts. */
+
+    if (message_defer || retry_message_key != NULL)
+      {
+      int delete_flag = message_defer? 0 : rf_delete;
+      if (retry_message_key == NULL)
+        {
+        retry_message_key = ob->retry_include_ip_address?
+          string_sprintf("T:%S:%s%s:%s", host->name, host->address, pistring,
+            message_id) :
+          string_sprintf("T:%S%s:%s", host->name, pistring, message_id);
+        }
+      retry_add_item(addrlist, retry_message_key,
+        rf_message | rf_host | delete_flag);
+      update_waiting = FALSE;
+      }
+
+    /* Any return other than DEFER (that is, OK or ERROR) means that the
+    addresses have got their final statuses filled in for this host. In the OK
+    case, see if any of them are deferred. */
+
+    if (rc == OK)
+      {
+      for (addr = addrlist; addr != NULL; addr = addr->next)
+        {
+        if (addr->transport_return == DEFER)
+          {
+          some_deferred = TRUE;
+          break;
+          }
+        }
+      }
+
+    /* If no addresses deferred or the result was ERROR, return. We do this for
+    ERROR because a failing filter set-up or add_headers expansion is likely to
+    fail for any host we try. */
+
+    if (rc == ERROR || (rc == OK && !some_deferred))
+      {
+      DEBUG(D_transport) debug_printf("Leaving %s transport\n", tblock->name);
+      return TRUE;    /* Each address has its status */
+      }
+
+    /* If the result was DEFER or some individual addresses deferred, let
+    the loop run to try other hosts with the deferred addresses, except for the
+    case when we were trying to deliver down an existing channel and failed.
+    Don't try any other hosts in this case. */
+
+    if (continuing) break;
+
+    /* If the whole delivery, or some individual addresses, were deferred and
+    there are more hosts that could be tried, do not count this host towards
+    the hosts_max_try limit if the age of the message is greater than the
+    maximum retry time for this host. This means we may try try all hosts,
+    ignoring the limit, when messages have been around for some time. This is
+    important because if we don't try all hosts, the address will never time
+    out. NOTE: this does not apply to hosts_max_try_hardlimit. */
+
+    if ((rc == DEFER || some_deferred) && nexthost != NULL)
+      {
+      BOOL timedout;
+      retry_config *retry = retry_find_config(host->name, NULL, 0, 0);
+
+      if (retry != NULL && retry->rules != NULL)
+        {
+        retry_rule *last_rule;
+        for (last_rule = retry->rules;
+             last_rule->next != NULL;
+             last_rule = last_rule->next);
+        timedout = time(NULL) - received_time > last_rule->timeout;
+        }
+      else timedout = TRUE;    /* No rule => timed out */
+
+      if (timedout)
+        {
+        unexpired_hosts_tried--;
+        DEBUG(D_transport) debug_printf("temporary delivery error(s) override "
+          "hosts_max_try (message older than host's retry time)\n");
+        }
+      }
+    }   /* End of loop for trying multiple hosts. */
+
+  /* This is the end of the loop that repeats iff expired is TRUE and
+  ob->delay_after_cutoff is FALSE. The second time round we will
+  try those hosts that haven't been tried since the message arrived. */
+
+  DEBUG(D_transport)
+    {
+    debug_printf("all IP addresses skipped or deferred at least one address\n");
+    if (expired && !ob->delay_after_cutoff && cutoff_retry == 0)
+      debug_printf("retrying IP addresses not tried since message arrived\n");
+    }
+  }
+
+
+/* Get here if all IP addresses are skipped or defer at least one address. In
+MUA wrapper mode, this will happen only for connection or other non-message-
+specific failures. Force the delivery status for all addresses to FAIL. */
+
+if (mua_wrapper)
+  {
+  for (addr = addrlist; addr != NULL; addr = addr->next)
+    addr->transport_return = FAIL;
+  goto END_TRANSPORT;
+  }
+
+/* In the normal, non-wrapper case, add a standard message to each deferred
+address if there hasn't been an error, that is, if it hasn't actually been
+tried this time. The variable "expired" will be FALSE if any deliveries were
+actually tried, or if there was at least one host that was not expired. That
+is, it is TRUE only if no deliveries were tried and all hosts were expired. If
+a delivery has been tried, an error code will be set, and the failing of the
+message is handled by the retry code later.
+
+If queue_smtp is set, or this transport was called to send a subsequent message
+down an existing TCP/IP connection, and something caused the host not to be
+found, we end up here, but can detect these cases and handle them specially. */
+
+for (addr = addrlist; addr != NULL; addr = addr->next)
+  {
+  /* If host is not NULL, it means that we stopped processing the host list
+  because of hosts_max_try or hosts_max_try_hardlimit. In the former case, this
+  means we need to behave as if some hosts were skipped because their retry
+  time had not come. Specifically, this prevents the address from timing out.
+  However, if we have hit hosts_max_try_hardlimit, we want to behave as if all
+  hosts were tried. */
+
+  if (host != NULL)
+    {
+    if (total_hosts_tried >= ob->hosts_max_try_hardlimit)
+      {
+      DEBUG(D_transport)
+        debug_printf("hosts_max_try_hardlimit reached: behave as if all "
+          "hosts were tried\n");
+      }
+    else
+      {
+      DEBUG(D_transport)
+        debug_printf("hosts_max_try limit caused some hosts to be skipped\n");
+      setflag(addr, af_retry_skipped);
+      }
+    }
+
+  if (queue_smtp)    /* no deliveries attempted */
+    {
+    addr->transport_return = DEFER;
+    addr->basic_errno = 0;
+    addr->message = US"SMTP delivery explicitly queued";
+    }
+
+  else if (addr->transport_return == DEFER &&
+       (addr->basic_errno == ERRNO_UNKNOWNERROR || addr->basic_errno == 0) &&
+       addr->message == NULL)
+    {
+    addr->basic_errno = ERRNO_HRETRY;
+    if (continue_hostname != NULL)
+      {
+      addr->message = US"no host found for existing SMTP connection";
+      }
+    else if (expired)
+      {
+      setflag(addr, af_pass_message);   /* This is not a security risk */
+      addr->message = (ob->delay_after_cutoff)?
+        US"retry time not reached for any host after a long failure period" :
+        US"all hosts have been failing for a long time and were last tried "
+          "after this message arrived";
+
+      /* If we are already using fallback hosts, or there are no fallback hosts
+      defined, convert the result to FAIL to cause a bounce. */
+
+      if (addr->host_list == addr->fallback_hosts ||
+          addr->fallback_hosts == NULL)
+        addr->transport_return = FAIL;
+      }
+    else
+      {
+      if (hosts_retry == hosts_total)
+        addr->message = US"retry time not reached for any host";
+      else if (hosts_fail == hosts_total)
+        addr->message = US"all host address lookups failed permanently";
+      else if (hosts_defer == hosts_total)
+        addr->message = US"all host address lookups failed temporarily";
+      else if (hosts_serial == hosts_total)
+        addr->message = US"connection limit reached for all hosts";
+      else if (hosts_fail+hosts_defer == hosts_total)
+        addr->message = US"all host address lookups failed";
+      else addr->message = US"some host address lookups failed and retry time "
+        "not reached for other hosts or connection limit reached";
+      }
+    }
+  }
+
+/* Update the database which keeps information about which messages are waiting
+for which hosts to become available. For some message-specific errors, the
+update_waiting flag is turned off because we don't want follow-on deliveries in
+those cases.  If this transport instance is explicitly limited to one message
+per connection then follow-on deliveries are not possible and there's no need
+to create/update the per-transport wait-<transport_name> database. */
+
+if (update_waiting && tblock->connection_max_messages != 1)
+  transport_update_waiting(hostlist, tblock->name);
+
+END_TRANSPORT:
+
+DEBUG(D_transport) debug_printf("Leaving %s transport\n", tblock->name);
+
+return TRUE;   /* Each address has its status */
+}
+
+/* vi: aw ai sw=2
+*/
+/* End of transport/smtp.c */
diff --git a/src/transports/smtp.h b/src/transports/smtp.h
new file mode 100644 (file)
index 0000000..dd41e1f
--- /dev/null
@@ -0,0 +1,112 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2014 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Private structure for the private options and other private data. */
+
+typedef struct {
+  uschar *hosts;
+  uschar *fallback_hosts;
+  host_item *hostlist;
+  host_item *fallback_hostlist;
+  uschar *authenticated_sender;
+  uschar *helo_data;
+  uschar *interface;
+  uschar *port;
+  uschar *protocol;
+  uschar *dscp;
+  uschar *serialize_hosts;
+  uschar *hosts_try_auth;
+  uschar *hosts_require_auth;
+#ifndef DISABLE_PRDR
+  uschar *hosts_try_prdr;
+#endif
+#ifndef DISABLE_OCSP
+  uschar *hosts_request_ocsp;
+  uschar *hosts_require_ocsp;
+#endif
+  uschar *hosts_require_tls;
+  uschar *hosts_avoid_tls;
+  uschar *hosts_verify_avoid_tls;
+  uschar *hosts_avoid_pipelining;
+  uschar *hosts_avoid_esmtp;
+  uschar *hosts_nopass_tls;
+  int     command_timeout;
+  int     connect_timeout;
+  int     data_timeout;
+  int     final_timeout;
+  int     size_addition;
+  int     hosts_max_try;
+  int     hosts_max_try_hardlimit;
+  BOOL    address_retry_include_sender;
+  BOOL    allow_localhost;
+  BOOL    authenticated_sender_force;
+  BOOL    gethostbyname;
+  BOOL    dns_qualify_single;
+  BOOL    dns_search_parents;
+  uschar *dnssec_request_domains;
+  uschar *dnssec_require_domains;
+  BOOL    delay_after_cutoff;
+  BOOL    hosts_override;
+  BOOL    hosts_randomize;
+  BOOL    keepalive;
+  BOOL    lmtp_ignore_quota;
+  BOOL    retry_include_ip_address;
+#ifdef SUPPORT_TLS
+  uschar *tls_certificate;
+  uschar *tls_crl;
+  uschar *tls_privatekey;
+  uschar *tls_require_ciphers;
+  uschar *gnutls_require_kx;
+  uschar *gnutls_require_mac;
+  uschar *gnutls_require_proto;
+  uschar *tls_sni;
+  uschar *tls_verify_certificates;
+  int     tls_dh_min_bits;
+  BOOL    tls_tempfail_tryclear;
+  uschar *tls_verify_hosts;
+  uschar *tls_try_verify_hosts;
+# ifdef EXPERIMENTAL_CERTNAMES
+  uschar *tls_verify_cert_hostnames;
+# endif
+#endif
+#ifndef DISABLE_DKIM
+  uschar *dkim_domain;
+  uschar *dkim_private_key;
+  uschar *dkim_selector;
+  uschar *dkim_canon;
+  uschar *dkim_sign_headers;
+  uschar *dkim_strict;
+#endif
+#ifdef EXPERIMENTAL_TPDA
+  uschar *tpda_host_defer_action;
+#endif
+} smtp_transport_options_block;
+
+/* Data for reading the private options. */
+
+extern optionlist smtp_transport_options[];
+extern int smtp_transport_options_count;
+
+/* Block containing default values. */
+
+extern smtp_transport_options_block smtp_transport_option_defaults;
+
+/* The main, init, and closedown entry points for the transport */
+
+extern BOOL smtp_transport_entry(transport_instance *, address_item *);
+extern void smtp_transport_init(transport_instance *);
+extern void smtp_transport_closedown(transport_instance *);
+
+
+
+extern int     smtp_auth(uschar *, unsigned, address_item *, host_item *,
+                smtp_transport_options_block *, BOOL,
+                smtp_inblock *, smtp_outblock *);
+extern BOOL    smtp_mail_auth_str(uschar *, unsigned,
+                address_item *, smtp_transport_options_block *);
+
+/* End of transports/smtp.h */
diff --git a/src/transports/tf_maildir.c b/src/transports/tf_maildir.c
new file mode 100644 (file)
index 0000000..cfe7cb2
--- /dev/null
@@ -0,0 +1,598 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2012 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Functions in support of the use of maildirsize files for handling quotas in
+maildir directories. Some of the rules are a bit baroque:
+
+http://www.inter7.com/courierimap/README.maildirquota.html
+
+We try to follow most of that, except that the directories to skip for quota
+calculations are not hard wired in, but are supplied as a regex. */
+
+
+#include "../exim.h"
+#include "appendfile.h"
+#include "tf_maildir.h"
+
+#define MAX_FILE_SIZE  5120
+
+
+
+/*************************************************
+*      Ensure maildir directories exist          *
+*************************************************/
+
+/* This function is called at the start of a maildir delivery, to ensure that
+all the relevant directories exist. It also creates a maildirfolder file if the
+base directory matches a given pattern.
+
+Argument:
+  path              the base directory name
+  addr              the address item (for setting an error message)
+  create_directory  true if we are allowed to create missing directories
+  dirmode           the mode for created directories
+  maildirfolder_create_regex
+                    the pattern to match for maildirfolder creation
+
+Returns:            TRUE on success; FALSE on failure
+*/
+
+BOOL maildir_ensure_directories(uschar *path, address_item *addr,
+  BOOL create_directory, int dirmode, uschar *maildirfolder_create_regex)
+{
+int i;
+struct stat statbuf;
+const char *subdirs[] = { "/tmp", "/new", "/cur" };
+
+DEBUG(D_transport)
+  debug_printf("ensuring maildir directories exist in %s\n", path);
+
+/* First ensure that the path we have is a directory; if it does not exist,
+create it. Then make sure the tmp, new & cur subdirs of the maildir are
+there. If not, fail. This aborts the delivery (even though the cur subdir is
+not actually needed for delivery). Handle all 4 directory tests/creates in a
+loop so that code can be shared. */
+
+for (i = 0; i < 4; i++)
+  {
+  int j;
+  const uschar *dir, *mdir;
+
+  if (i == 0)
+    {
+    mdir = CUS"";
+    dir = path;
+    }
+  else
+    {
+    mdir = CUS subdirs[i-1];
+    dir = mdir + 1;
+    }
+
+  /* Check an existing path is a directory. This is inside a loop because
+  there is a potential race condition when creating the directory - some
+  other process may get there first. Give up after trying several times,
+  though. */
+
+  for (j = 0; j < 10; j++)
+    {
+    if (Ustat(dir, &statbuf) == 0)
+      {
+      if (S_ISDIR(statbuf.st_mode)) break;   /* out of the race loop */
+      addr->message = string_sprintf("%s%s is not a directory", path,
+        mdir);
+      addr->basic_errno = ERRNO_NOTDIRECTORY;
+      return FALSE;
+      }
+
+    /* Try to make if non-existent and configured to do so */
+
+    if (errno == ENOENT && create_directory)
+      {
+      if (!directory_make(NULL, dir, dirmode, FALSE))
+        {
+        if (errno == EEXIST) continue;     /* repeat the race loop */
+        addr->message = string_sprintf("cannot create %s%s", path, mdir);
+        addr->basic_errno = errno;
+        return FALSE;
+        }
+      DEBUG(D_transport)
+        debug_printf("created directory %s%s\n", path, mdir);
+      break;   /* out of the race loop */
+      }
+
+    /* stat() error other than ENOENT, or ENOENT and not creatable */
+
+    addr->message = string_sprintf("stat() error for %s%s: %s", path, mdir,
+      strerror(errno));
+    addr->basic_errno = errno;
+    return FALSE;
+    }
+
+  /* If we went round the loop 10 times, the directory was flickering in
+  and out of existence like someone in a malfunctioning Star Trek
+  transporter. */
+
+  if (j >= 10)
+    {
+    addr->message = string_sprintf("existence of %s%s unclear\n", path,
+      mdir);
+    addr->basic_errno = errno;
+    addr->special_action = SPECIAL_FREEZE;
+    return FALSE;
+    }
+
+  /* First time through the directories loop, cd to the main directory */
+
+  if (i == 0 && Uchdir(path) != 0)
+    {
+    addr->message = string_sprintf ("cannot chdir to %s", path);
+    addr->basic_errno = errno;
+    return FALSE;
+    }
+  }
+
+/* If the basic path matches maildirfolder_create_regex, we are dealing with
+a subfolder, and should ensure that a maildirfolder file exists. */
+
+if (maildirfolder_create_regex != NULL)
+  {
+  const uschar *error;
+  int offset;
+  const pcre *regex;
+
+  DEBUG(D_transport) debug_printf("checking for maildirfolder requirement\n");
+
+  regex = pcre_compile(CS maildirfolder_create_regex, PCRE_COPT,
+    (const char **)&error, &offset, NULL);
+
+  if (regex == NULL)
+    {
+    addr->message = string_sprintf("appendfile: regular expression "
+      "error: %s at offset %d while compiling %s", error, offset,
+      maildirfolder_create_regex);
+    return FALSE;
+    }
+
+  if (pcre_exec(regex, NULL, CS path, Ustrlen(path), 0, 0, NULL, 0) >= 0)
+    {
+    uschar *fname = string_sprintf("%s/maildirfolder", path);
+    if (Ustat(fname, &statbuf) == 0)
+      {
+      DEBUG(D_transport) debug_printf("maildirfolder already exists\n");
+      }
+    else
+      {
+      int fd = Uopen(fname, O_WRONLY|O_APPEND|O_CREAT, 0600);
+      if (fd < 0)
+        {
+        addr->message = string_sprintf("appendfile: failed to create "
+          "maildirfolder file in %s directory: %s", path, strerror(errno));
+        return FALSE;
+        }
+      (void)close(fd);
+      DEBUG(D_transport) debug_printf("created maildirfolder file\n");
+      }
+    }
+  else
+    {
+    DEBUG(D_transport) debug_printf("maildirfolder file not required\n");
+    }
+  }
+
+return TRUE;   /* Everything exists that should exist */
+}
+
+
+
+
+/*************************************************
+*       Update maildirsizefile for new file      *
+*************************************************/
+
+/* This function is called to add a new line to the file, recording the length
+of the newly added message. There isn't much we can do on failure...
+
+Arguments:
+  fd           the open file descriptor
+  size         the size of the message
+
+Returns:       nothing
+*/
+
+void
+maildir_record_length(int fd, int size)
+{
+int len;
+uschar buffer[256];
+sprintf(CS buffer, "%d 1\n", size);
+len = Ustrlen(buffer);
+(void)lseek(fd, 0, SEEK_END);
+len = write(fd, buffer, len);
+DEBUG(D_transport)
+  debug_printf("added '%.*s' to maildirsize file\n", len-1, buffer);
+}
+
+
+
+/*************************************************
+*          Find the size of a maildir            *
+*************************************************/
+
+/* This function is called when we have to recalculate the size of a maildir by
+scanning all the files and directories therein. There are rules and conventions
+about which files or directories are included. We support this by the use of a
+regex to match directories that are to be included.
+
+Maildirs can only be one level deep. However, this function recurses, so it
+might cope with deeper nestings. We use the existing check_dir_size() function
+to add up the sizes of the files in a directory that contains messages.
+
+The function returns the most recent timestamp encountered. It can also be run
+in a dummy mode in which it does not scan for sizes, but just returns the
+timestamp.
+
+Arguments:
+  path            the path to the maildir
+  filecount       where to store the count of messages
+  latest          where to store the latest timestamp encountered
+  regex           a regex for getting files sizes from file names
+  dir_regex       a regex for matching directories to be included
+  timestamp_only  don't actually compute any sizes
+
+Returns:      the sum of the sizes of the messages
+*/
+
+off_t
+maildir_compute_size(uschar *path, int *filecount, time_t *latest,
+  const pcre *regex, const pcre *dir_regex, BOOL timestamp_only)
+{
+DIR *dir;
+off_t sum = 0;
+struct dirent *ent;
+struct stat statbuf;
+
+dir = opendir(CS path);
+if (dir == NULL) return 0;
+
+while ((ent = readdir(dir)) != NULL)
+  {
+  uschar *name = US ent->d_name;
+  uschar buffer[1024];
+
+  if (Ustrcmp(name, ".") == 0 || Ustrcmp(name, "..") == 0) continue;
+
+  /* We are normally supplied with a regex for choosing which directories to
+  scan. We do the regex match first, because that avoids a stat() for names
+  we aren't interested in. */
+
+  if (dir_regex != NULL &&
+      pcre_exec(dir_regex, NULL, CS name, Ustrlen(name), 0, 0, NULL, 0) < 0)
+    {
+    DEBUG(D_transport)
+      debug_printf("skipping %s/%s: dir_regex does not match\n", path, name);
+    continue;
+    }
+
+  /* The name is OK; stat it. */
+
+  if (!string_format(buffer, sizeof(buffer), "%s/%s", path, name))
+    {
+    DEBUG(D_transport)
+      debug_printf("maildir_compute_size: name too long: dir=%s name=%s\n",
+        path, name);
+    continue;
+    }
+
+  if (Ustat(buffer, &statbuf) < 0)
+    {
+    DEBUG(D_transport)
+      debug_printf("maildir_compute_size: stat error %d for %s: %s\n", errno,
+        buffer, strerror(errno));
+    continue;
+    }
+
+  if ((statbuf.st_mode & S_IFMT) != S_IFDIR)
+    {
+    DEBUG(D_transport)
+      debug_printf("skipping %s/%s: not a directory\n", path, name);
+    continue;
+    }
+
+  /* Keep the latest timestamp encountered */
+
+  if (statbuf.st_mtime > *latest) *latest = statbuf.st_mtime;
+
+  /* If this is a maildir folder, call this function recursively. */
+
+  if (name[0] == '.')
+    {
+    sum += maildir_compute_size(buffer, filecount, latest, regex, dir_regex,
+      timestamp_only);
+    }
+
+  /* Otherwise it must be a folder that contains messages (e.g. new or cur), so
+  we need to get its size, unless all we are interested in is the timestamp. */
+
+  else if (!timestamp_only)
+    {
+    sum += check_dir_size(buffer, filecount, regex);
+    }
+  }
+
+closedir(dir);
+DEBUG(D_transport)
+  {
+  if (timestamp_only)
+    debug_printf("maildir_compute_size (timestamp_only): %ld\n",
+    (long int) *latest);
+  else
+    debug_printf("maildir_compute_size: path=%s\n  sum=" OFF_T_FMT
+      " filecount=%d timestamp=%ld\n",
+      path, sum, *filecount, (long int) *latest);
+  }
+return sum;
+}
+
+
+
+/*************************************************
+*        Create or update maildirsizefile        *
+*************************************************/
+
+/* This function is called before a delivery if the option to use
+maildirsizefile is enabled. Its function is to create the file if it does not
+exist, or to update it if that is necessary.
+
+The logic in this function follows the rules that are described in
+
+  http://www.inter7.com/courierimap/README.maildirquota.html
+
+Or, at least, it is supposed to!
+
+Arguments:
+  path             the path to the maildir directory; this is already backed-up
+                     to the parent if the delivery diretory is a maildirfolder
+  ob               the appendfile options block
+  regex            a compiled regex for getting a file's size from its name
+  dir_regex        a compiled regex for selecting maildir directories
+  returned_size    where to return the current size of the maildir, even if
+                     the maildirsizefile is removed because of a race
+
+Returns:           >=0  a file descriptor for an open maildirsize file
+                   -1   there was an error opening or accessing the file
+                   -2   the file was removed because of a race
+*/
+
+int
+maildir_ensure_sizefile(uschar *path, appendfile_transport_options_block *ob,
+  const pcre *regex, const pcre *dir_regex, off_t *returned_size,
+  int *returned_filecount)
+{
+int count, fd;
+off_t cached_quota = 0;
+int cached_quota_filecount = 0;
+int filecount = 0;
+int linecount = 0;
+off_t size = 0;
+uschar *filename;
+uschar buffer[MAX_FILE_SIZE];
+uschar *ptr = buffer;
+uschar *endptr;
+
+/* Try a few times to open or create the file, in case another process is doing
+the same thing. */
+
+filename = string_sprintf("%s/maildirsize", path);
+
+DEBUG(D_transport) debug_printf("looking for maildirsize in %s\n", path);
+fd = Uopen(filename, O_RDWR|O_APPEND, ob->mode ? ob->mode : 0600);
+if (fd < 0)
+  {
+  if (errno != ENOENT) return -1;
+  DEBUG(D_transport)
+    debug_printf("%s does not exist: recalculating\n", filename);
+  goto RECALCULATE;
+  }
+
+/* The file has been successfully opened. Check that the cached quota value is
+still correct, and that the size of the file is still small enough. If so,
+compute the maildir size from the file. */
+
+count = read(fd, buffer, sizeof(buffer));
+if (count >= sizeof(buffer))
+  {
+  DEBUG(D_transport)
+    debug_printf("maildirsize file too big (%d): recalculating\n", count);
+  goto RECALCULATE;
+  }
+buffer[count] = 0;   /* Ensure string terminated */
+
+/* Read the quota parameters from the first line of the data. */
+
+DEBUG(D_transport)
+  debug_printf("reading quota parameters from maildirsize data\n");
+
+for (;;)
+  {
+  off_t n = (off_t)Ustrtod(ptr, &endptr);
+
+  /* Only two data items are currently defined; ignore any others that
+  may be present. The spec is for a number followed by a letter. Anything
+  else we reject and recalculate. */
+
+  if (*endptr == 'S') cached_quota = n;
+    else if (*endptr == 'C') cached_quota_filecount = (int)n;
+  if (!isalpha(*endptr++))
+    {
+    DEBUG(D_transport)
+      debug_printf("quota parameter number not followed by letter in "
+        "\"%.*s\": recalculating maildirsize\n", (int)(endptr - buffer),
+        buffer);
+    goto RECALCULATE;
+    }
+  if (*endptr == '\n' || *endptr == 0) break;
+  if (*endptr++ != ',')
+    {
+    DEBUG(D_transport)
+      debug_printf("quota parameter not followed by comma in "
+        "\"%.*s\": recalculating maildirsize\n", (int)(endptr - buffer),
+        buffer);
+    goto RECALCULATE;
+    }
+  ptr = endptr;
+  }
+
+/* Check the cached values against the current settings */
+
+if (cached_quota != ob->quota_value ||
+    cached_quota_filecount != ob->quota_filecount_value)
+  {
+  DEBUG(D_transport)
+    debug_printf("cached quota is out of date: recalculating\n"
+      "  quota=" OFF_T_FMT " cached_quota=" OFF_T_FMT " filecount_quota=%d "
+      "cached_quota_filecount=%d\n", ob->quota_value,
+      cached_quota, ob->quota_filecount_value, cached_quota_filecount);
+  goto RECALCULATE;
+  }
+
+/* Quota values agree; parse the rest of the data to get the sizes. At this
+stage, *endptr points either to 0 or to '\n'.  */
+
+DEBUG(D_transport)
+  debug_printf("computing maildir size from maildirsize data\n");
+
+while (*endptr++ == '\n')
+  {
+  if (*endptr == 0) break;
+  linecount++;
+  ptr = endptr;
+  size += (off_t)Ustrtod(ptr, &endptr);
+  if (*endptr != ' ') break;
+  ptr = endptr + 1;
+  filecount += Ustrtol(ptr, &endptr, 10);
+  }
+
+/* If *endptr is zero, we have successfully parsed the file, and we now have
+the size of the mailbox as cached in the file. The "rules" say that if this
+value indicates that the mailbox is over quota, we must recalculate if there is
+more than one entry in the file, or if the file is older than 15 minutes. Also,
+just in case there are weird values in the file, recalculate if either of the
+values is negative. */
+
+if (*endptr == 0)
+  {
+  if (size < 0 || filecount < 0)
+    {
+    DEBUG(D_transport) debug_printf("negative value in maildirsize "
+      "(size=" OFF_T_FMT " count=%d): recalculating\n", size, filecount);
+    goto RECALCULATE;
+    }
+
+  if (ob->quota_value > 0 &&
+      (size + (ob->quota_is_inclusive? message_size : 0) > ob->quota_value ||
+        (ob->quota_filecount_value > 0 &&
+          filecount + (ob->quota_is_inclusive ? 1:0) >
+            ob->quota_filecount_value)
+      ))
+    {
+    struct stat statbuf;
+    if (linecount > 1)
+      {
+      DEBUG(D_transport) debug_printf("over quota and maildirsize has "
+        "more than 1 entry: recalculating\n");
+      goto RECALCULATE;
+      }
+
+    if (fstat(fd, &statbuf) < 0) goto RECALCULATE;  /* Should never occur */
+
+    if (time(NULL) - statbuf.st_mtime > 15*60)
+      {
+      DEBUG(D_transport) debug_printf("over quota and maildirsize is older "
+        "than 15 minutes: recalculating\n");
+      goto RECALCULATE;
+      }
+    }
+  }
+
+
+/* If *endptr is not zero, there was a syntax error in the file. */
+
+else
+  {
+  int len;
+  time_t old_latest, new_latest;
+  uschar *tempname;
+  struct timeval tv;
+
+  DEBUG(D_transport)
+    {
+    uschar *p = endptr;
+    while (p > buffer && p[-1] != '\n') p--;
+    endptr[1] = 0;
+
+    debug_printf("error in maildirsizefile: unexpected character %d in "
+      "line %d (starting '%s'): recalculating\n",
+      *endptr, linecount + 1, string_printing(p));
+    }
+
+  /* Either there is no file, or the quota value has changed, or the file has
+  got too big, or there was some format error in the file. Recalculate the size
+  and write new contents to a temporary file; then rename it. After any
+  error, just return -1 as the file descriptor. */
+
+  RECALCULATE:
+
+  if (fd >= 0) (void)close(fd);
+  old_latest = 0;
+  filecount = 0;
+  size = maildir_compute_size(path, &filecount, &old_latest, regex, dir_regex,
+    FALSE);
+
+  (void)gettimeofday(&tv, NULL);
+  tempname = string_sprintf("%s/tmp/%lu.H%luP%lu.%s", path, tv.tv_sec,
+    tv.tv_usec, (long unsigned) getpid(), primary_hostname);
+
+  fd = Uopen(tempname, O_RDWR|O_CREAT|O_EXCL, ob->mode ? ob->mode : 0600);
+  if (fd >= 0)
+    {
+    (void)sprintf(CS buffer, OFF_T_FMT "S,%dC\n" OFF_T_FMT " %d\n",
+      ob->quota_value, ob->quota_filecount_value, size, filecount);
+    len = Ustrlen(buffer);
+    if (write(fd, buffer, len) != len || Urename(tempname, filename) < 0)
+      {
+      (void)close(fd);
+      fd = -1;
+      }
+    }
+
+  /* If any of the directories have been modified since the last timestamp we
+  saw, we have to junk this maildirsize file. */
+
+  DEBUG(D_transport) debug_printf("checking subdirectory timestamps\n");
+  new_latest = 0;
+  (void)maildir_compute_size(path, NULL, &new_latest , NULL, dir_regex, TRUE);
+  if (new_latest > old_latest)
+    {
+    DEBUG(D_transport) debug_printf("abandoning maildirsize because of "
+      "a later subdirectory modification\n");
+    (void)Uunlink(filename);
+    (void)close(fd);
+    fd = -2;
+    }
+  }
+
+/* Return the sizes and the file descriptor, if any */
+
+DEBUG(D_transport) debug_printf("returning maildir size=" OFF_T_FMT
+  " filecount=%d\n", size, filecount);
+*returned_size = size;
+*returned_filecount = filecount;
+return fd;
+}
+
+/* End of tf_maildir.c */
diff --git a/src/transports/tf_maildir.h b/src/transports/tf_maildir.h
new file mode 100644 (file)
index 0000000..0be6bc4
--- /dev/null
@@ -0,0 +1,20 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Header file for the functions that are used to support the use of
+maildirsize files for quota handling in maildir directories. */
+
+extern off_t  maildir_compute_size(uschar *, int *, time_t *, const pcre *,
+                const pcre *, BOOL);
+extern BOOL   maildir_ensure_directories(uschar *, address_item *, BOOL, int,
+                uschar *);
+extern int    maildir_ensure_sizefile(uschar *,
+                appendfile_transport_options_block *, const pcre *,
+                const pcre *, off_t *, int *);
+extern void   maildir_record_length(int, int);
+
+/* End of tf_maildir.h */
diff --git a/src/tree.c b/src/tree.c
new file mode 100644 (file)
index 0000000..2a9d90a
--- /dev/null
@@ -0,0 +1,365 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Functions for maintaining binary balanced trees and some associated
+functions as well. */
+
+
+#include "exim.h"
+
+
+
+
+/*************************************************
+*        Add entry to non-recipients tree        *
+*************************************************/
+
+/* Duplicates are just discarded.
+
+Arguments:
+  s       string to add
+
+Returns:  nothing
+*/
+
+void
+tree_add_nonrecipient(uschar *s)
+{
+tree_node *node = store_get(sizeof(tree_node) + Ustrlen(s));
+Ustrcpy(node->name, s);
+node->data.ptr = NULL;
+if (!tree_insertnode(&tree_nonrecipients, node)) store_reset(node);
+}
+
+
+
+/*************************************************
+*        Add entry to duplicates tree            *
+*************************************************/
+
+/* Duplicates are just discarded.
+
+Argument:
+  s       string to add
+  addr    the address is is a duplicate of
+
+Returns:  nothing
+*/
+
+void
+tree_add_duplicate(uschar *s, address_item *addr)
+{
+tree_node *node = store_get(sizeof(tree_node) + Ustrlen(s));
+Ustrcpy(node->name, s);
+node->data.ptr = addr;
+if (!tree_insertnode(&tree_duplicates, node)) store_reset(node);
+}
+
+
+
+/*************************************************
+*    Add entry to unusable addresses tree        *
+*************************************************/
+
+/* Duplicates are simply discarded.
+
+Argument:    the host item
+Returns:     nothing
+*/
+
+void
+tree_add_unusable(host_item *h)
+{
+tree_node *node;
+uschar s[256];
+sprintf(CS s, "T:%.200s:%s", h->name, h->address);
+node = store_get(sizeof(tree_node) + Ustrlen(s));
+Ustrcpy(node->name, s);
+node->data.val = h->why;
+if (h->status == hstatus_unusable_expired) node->data.val += 256;
+if (!tree_insertnode(&tree_unusable, node)) store_reset(node);
+}
+
+
+
+/*************************************************
+*        Write a tree in re-readable form        *
+*************************************************/
+
+/* This function writes out a tree in a form in which it can
+easily be re-read. It is used for writing out the non-recipients
+tree onto the spool, for retrieval at the next retry time.
+
+The format is as follows:
+
+   . If the tree is empty, write one line containing XX.
+
+   . Otherwise, each node is written, preceded by two letters
+     (Y/N) indicating whether it has left or right children.
+
+   . The left subtree (if any) then follows, then the right subtree.
+
+First, there's an internal recursive subroutine.
+
+Arguments:
+  p          current node
+  f          FILE to write to
+
+Returns:     nothing
+*/
+
+static void
+write_tree(tree_node *p, FILE *f)
+{
+fprintf(f, "%c%c %s\n",
+  (p->left == NULL)? 'N':'Y', (p->right == NULL)? 'N':'Y', p->name);
+if (p->left != NULL) write_tree(p->left, f);
+if (p->right != NULL) write_tree(p->right, f);
+}
+
+/* This is the top-level function, with the same arguments. */
+
+void
+tree_write(tree_node *p, FILE *f)
+{
+if (p == NULL)
+  {
+  fprintf(f, "XX\n");
+  return;
+  }
+write_tree(p, f);
+}
+
+
+
+
+
+/***********************************************************
+*          Binary Balanced Tree Management Routines        *
+***********************************************************/
+
+/* This set of routines maintains a balanced binary tree using
+the algorithm given in Knuth Vol 3 page 455.
+
+The routines make use of uschar * pointers as byte pointers,
+so as to be able to do arithmetic on them, since ANSI Standard
+C does not permit additions and subtractions on void pointers. */
+
+
+/*************************************************
+*              Flags and Parameters              *
+*************************************************/
+
+#define tree_lbal      1         /* left subtree is longer */
+#define tree_rbal      2         /* right subtree is longer */
+#define tree_bmask     3         /* mask for flipping bits */
+
+
+/*************************************************
+*         Insert a new node into a tree          *
+*************************************************/
+
+/* The node->name field must (obviously) be set, but the other
+fields need not be initialized.
+
+Arguments:
+  treebase   pointer to the root of the tree
+  node       the note to insert, with name field set
+
+Returns:     TRUE if node inserted; FALSE if not (duplicate)
+*/
+
+int
+tree_insertnode(tree_node **treebase, tree_node *node)
+{
+tree_node *p = *treebase;
+tree_node **q, *r, *s, **t;
+int a;
+
+node->left = node->right = NULL;
+node->balance = 0;
+
+/* Deal with an empty tree */
+
+if (p == NULL)
+  {
+  *treebase = node;
+  return TRUE;
+  }
+
+/* The tree is not empty. While finding the insertion point,
+q points to the pointer to p, and t points to the pointer to
+the potential re-balancing point. */
+
+q = treebase;
+t = q;
+
+/* Loop to search tree for place to insert new node */
+
+for (;;)
+  {
+  int c = Ustrcmp(node->name, p->name);
+  if (c == 0) return FALSE;              /* Duplicate node encountered */
+
+  /* Deal with climbing down the tree, exiting from the loop
+  when we reach a leaf. */
+
+  q = (c > 0)? &(p->right) : &(p->left);
+  p = *q;
+  if (p == NULL) break;
+
+  /* Save the address of the pointer to the last node en route
+  which has a non-zero balance factor. */
+
+  if (p->balance != 0) t = q;
+  }
+
+/* When the above loop completes, q points to the pointer to NULL;
+that is the place at which the new node must be inserted. */
+
+*q = node;
+
+/* Set up s as the potential re-balancing point, and r as the
+next node after it along the route. */
+
+s = *t;
+r = (Ustrcmp(node->name, s->name) > 0)? s->right : s->left;
+
+/* Adjust balance factors along the route from s to node. */
+
+p = r;
+
+while (p != node)
+  {
+  if (Ustrcmp(node->name, p->name) < 0)
+    {
+    p->balance = tree_lbal;
+    p = p->left;
+    }
+  else
+    {
+    p->balance = tree_rbal;
+    p = p->right;
+    }
+  }
+
+/* Now the World-Famous Balancing Act */
+
+a = (Ustrcmp(node->name, s->name) < 0)? tree_lbal : tree_rbal;
+
+if (s->balance == 0) s->balance = (uschar)a;        /* The tree has grown higher */
+  else if (s->balance != (uschar)a) s->balance = 0; /* It's become more balanced */
+else                                              /* It's got out of balance */
+  {
+  /* Perform a single rotation */
+
+  if (r->balance == (uschar)a)
+    {
+    p = r;
+    if (a == tree_rbal)
+      {
+      s->right = r->left;
+      r->left = s;
+      }
+    else
+      {
+      s->left = r->right;
+      r->right = s;
+      }
+    s->balance = 0;
+    r->balance = 0;
+    }
+
+  /* Perform a double rotation There was an occasion when the balancing
+  factors were screwed up by a bug in the code that reads a tree from
+  the spool. In case this ever happens again, check for changing p to NULL
+  and don't do it. It is better to have an unbalanced tree than a crash. */
+
+  else
+    {
+    if (a == tree_rbal)
+      {
+      if (r->left == NULL) return TRUE;   /* Bail out if tree corrupt */
+      p = r->left;
+      r->left = p->right;
+      p->right = r;
+      s->right = p->left;
+      p->left = s;
+      }
+    else
+      {
+      if (r->right == NULL) return TRUE;  /* Bail out if tree corrupt */
+      p = r->right;
+      r->right = p->left;
+      p->left = r;
+      s->left = p->right;
+      p->right = s;
+      }
+
+    s->balance = (p->balance == (uschar)a)? (uschar)(a^tree_bmask) : 0;
+    r->balance = (p->balance == (uschar)(a^tree_bmask))? (uschar)a : 0;
+    p->balance = 0;
+    }
+
+  /* Finishing touch */
+
+  *t = p;
+  }
+
+return TRUE;     /* Successful insertion */
+}
+
+
+
+/*************************************************
+*          Search tree for node by name          *
+*************************************************/
+
+/*
+Arguments:
+  p         root of tree
+  name      key to search for
+
+Returns:    pointer to node, or NULL if not found
+*/
+
+tree_node *
+tree_search(tree_node *p, uschar *name)
+{
+while (p != NULL)
+  {
+  int c = Ustrcmp(name, p->name);
+  if (c == 0) return p;
+  p = (c < 0)? p->left : p->right;
+  }
+return NULL;
+}
+
+
+
+/*************************************************
+*   Walk tree recursively and execute function   *
+*************************************************/
+
+/*
+Arguments:
+  p       root of the tree
+  f       function to execute for each name-value-pair
+  ctx     context data for f
+*/
+
+void
+tree_walk(tree_node *p, void (*f)(uschar*, uschar*, void*), void *ctx)
+{
+if (p == NULL) return;
+f(p->name, p->data.ptr, ctx);
+if (p->left != NULL) tree_walk(p->left, f, ctx);
+if (p->right != NULL) tree_walk(p->right, f, ctx);
+}
+
+
+/* End of tree.c */
diff --git a/src/valgrind.h b/src/valgrind.h
new file mode 100644 (file)
index 0000000..4d41690
--- /dev/null
@@ -0,0 +1,4797 @@
+/* -*- c -*-
+   ----------------------------------------------------------------
+
+   Notice that the following BSD-style license applies to this one
+   file (valgrind.h) only.  The rest of Valgrind is licensed under the
+   terms of the GNU General Public License, version 2, unless
+   otherwise indicated.  See the COPYING file in the source
+   distribution for details.
+
+   ----------------------------------------------------------------
+
+   This file is part of Valgrind, a dynamic binary instrumentation
+   framework.
+
+   Copyright (C) 2000-2010 Julian Seward.  All rights reserved.
+
+   Redistribution and use in source and binary forms, with or without
+   modification, are permitted provided that the following conditions
+   are met:
+
+   1. Redistributions of source code must retain the above copyright
+      notice, this list of conditions and the following disclaimer.
+
+   2. The origin of this software must not be misrepresented; you must 
+      not claim that you wrote the original software.  If you use this 
+      software in a product, an acknowledgment in the product 
+      documentation would be appreciated but is not required.
+
+   3. Altered source versions must be plainly marked as such, and must
+      not be misrepresented as being the original software.
+
+   4. The name of the author may not be used to endorse or promote 
+      products derived from this software without specific prior written 
+      permission.
+
+   THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
+   OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+   ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
+   DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
+   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+   ----------------------------------------------------------------
+
+   Notice that the above BSD-style license applies to this one file
+   (valgrind.h) only.  The entire rest of Valgrind is licensed under
+   the terms of the GNU General Public License, version 2.  See the
+   COPYING file in the source distribution for details.
+
+   ---------------------------------------------------------------- 
+*/
+
+
+/* This file is for inclusion into client (your!) code.
+
+   You can use these macros to manipulate and query Valgrind's 
+   execution inside your own programs.
+
+   The resulting executables will still run without Valgrind, just a
+   little bit more slowly than they otherwise would, but otherwise
+   unchanged.  When not running on valgrind, each client request
+   consumes very few (eg. 7) instructions, so the resulting performance
+   loss is negligible unless you plan to execute client requests
+   millions of times per second.  Nevertheless, if that is still a
+   problem, you can compile with the NVALGRIND symbol defined (gcc
+   -DNVALGRIND) so that client requests are not even compiled in.  */
+
+#ifndef __VALGRIND_H
+#define __VALGRIND_H
+
+
+/* ------------------------------------------------------------------ */
+/* VERSION NUMBER OF VALGRIND                                         */
+/* ------------------------------------------------------------------ */
+
+/* Specify Valgrind's version number, so that user code can
+   conditionally compile based on our version number.  Note that these
+   were introduced at version 3.6 and so do not exist in version 3.5
+   or earlier.  The recommended way to use them to check for "version
+   X.Y or later" is (eg)
+
+#if defined(__VALGRIND_MAJOR__) && defined(__VALGRIND_MINOR__)   \
+    && (__VALGRIND_MAJOR__ > 3                                   \
+        || (__VALGRIND_MAJOR__ == 3 && __VALGRIND_MINOR__ >= 6))
+*/
+#define __VALGRIND_MAJOR__    3
+#define __VALGRIND_MINOR__    6
+
+
+#include <stdarg.h>
+
+/* Nb: this file might be included in a file compiled with -ansi.  So
+   we can't use C++ style "//" comments nor the "asm" keyword (instead
+   use "__asm__"). */
+
+/* Derive some tags indicating what the target platform is.  Note
+   that in this file we're using the compiler's CPP symbols for
+   identifying architectures, which are different to the ones we use
+   within the rest of Valgrind.  Note, __powerpc__ is active for both
+   32 and 64-bit PPC, whereas __powerpc64__ is only active for the
+   latter (on Linux, that is).
+
+   Misc note: how to find out what's predefined in gcc by default:
+   gcc -Wp,-dM somefile.c
+*/
+#undef PLAT_ppc64_aix5
+#undef PLAT_ppc32_aix5
+#undef PLAT_x86_darwin
+#undef PLAT_amd64_darwin
+#undef PLAT_x86_win32
+#undef PLAT_x86_linux
+#undef PLAT_amd64_linux
+#undef PLAT_ppc32_linux
+#undef PLAT_ppc64_linux
+#undef PLAT_arm_linux
+
+#if defined(_AIX) && defined(__64BIT__)
+#  define PLAT_ppc64_aix5 1
+#elif defined(_AIX) && !defined(__64BIT__)
+#  define PLAT_ppc32_aix5 1
+#elif defined(__APPLE__) && defined(__i386__)
+#  define PLAT_x86_darwin 1
+#elif defined(__APPLE__) && defined(__x86_64__)
+#  define PLAT_amd64_darwin 1
+#elif defined(__MINGW32__) || defined(__CYGWIN32__) || defined(_WIN32) && defined(_M_IX86)
+#  define PLAT_x86_win32 1
+#elif defined(__linux__) && defined(__i386__)
+#  define PLAT_x86_linux 1
+#elif defined(__linux__) && defined(__x86_64__)
+#  define PLAT_amd64_linux 1
+#elif defined(__linux__) && defined(__powerpc__) && !defined(__powerpc64__)
+#  define PLAT_ppc32_linux 1
+#elif defined(__linux__) && defined(__powerpc__) && defined(__powerpc64__)
+#  define PLAT_ppc64_linux 1
+#elif defined(__linux__) && defined(__arm__)
+#  define PLAT_arm_linux 1
+#else
+/* If we're not compiling for our target platform, don't generate
+   any inline asms.  */
+#  if !defined(NVALGRIND)
+#    define NVALGRIND 1
+#  endif
+#endif
+
+
+/* ------------------------------------------------------------------ */
+/* ARCHITECTURE SPECIFICS for SPECIAL INSTRUCTIONS.  There is nothing */
+/* in here of use to end-users -- skip to the next section.           */
+/* ------------------------------------------------------------------ */
+
+#if defined(NVALGRIND)
+
+/* Define NVALGRIND to completely remove the Valgrind magic sequence
+   from the compiled code (analogous to NDEBUG's effects on
+   assert()) */
+#define VALGRIND_DO_CLIENT_REQUEST(                               \
+        _zzq_rlval, _zzq_default, _zzq_request,                   \
+        _zzq_arg1, _zzq_arg2, _zzq_arg3, _zzq_arg4, _zzq_arg5)    \
+   {                                                              \
+      (_zzq_rlval) = (_zzq_default);                              \
+   }
+
+#else  /* ! NVALGRIND */
+
+/* The following defines the magic code sequences which the JITter
+   spots and handles magically.  Don't look too closely at them as
+   they will rot your brain.
+
+   The assembly code sequences for all architectures is in this one
+   file.  This is because this file must be stand-alone, and we don't
+   want to have multiple files.
+
+   For VALGRIND_DO_CLIENT_REQUEST, we must ensure that the default
+   value gets put in the return slot, so that everything works when
+   this is executed not under Valgrind.  Args are passed in a memory
+   block, and so there's no intrinsic limit to the number that could
+   be passed, but it's currently five.
+   
+   The macro args are: 
+      _zzq_rlval    result lvalue
+      _zzq_default  default value (result returned when running on real CPU)
+      _zzq_request  request code
+      _zzq_arg1..5  request params
+
+   The other two macros are used to support function wrapping, and are
+   a lot simpler.  VALGRIND_GET_NR_CONTEXT returns the value of the
+   guest's NRADDR pseudo-register and whatever other information is
+   needed to safely run the call original from the wrapper: on
+   ppc64-linux, the R2 value at the divert point is also needed.  This
+   information is abstracted into a user-visible type, OrigFn.
+
+   VALGRIND_CALL_NOREDIR_* behaves the same as the following on the
+   guest, but guarantees that the branch instruction will not be
+   redirected: x86: call *%eax, amd64: call *%rax, ppc32/ppc64:
+   branch-and-link-to-r11.  VALGRIND_CALL_NOREDIR is just text, not a
+   complete inline asm, since it needs to be combined with more magic
+   inline asm stuff to be useful.
+*/
+
+/* ------------------------- x86-{linux,darwin} ---------------- */
+
+#if defined(PLAT_x86_linux)  ||  defined(PLAT_x86_darwin)  \
+    ||  (defined(PLAT_x86_win32) && defined(__GNUC__))
+
+typedef
+   struct { 
+      unsigned int nraddr; /* where's the code? */
+   }
+   OrigFn;
+
+#define __SPECIAL_INSTRUCTION_PREAMBLE                            \
+                     "roll $3,  %%edi ; roll $13, %%edi\n\t"      \
+                     "roll $29, %%edi ; roll $19, %%edi\n\t"
+
+#define VALGRIND_DO_CLIENT_REQUEST(                               \
+        _zzq_rlval, _zzq_default, _zzq_request,                   \
+        _zzq_arg1, _zzq_arg2, _zzq_arg3, _zzq_arg4, _zzq_arg5)    \
+  { volatile unsigned int _zzq_args[6];                           \
+    volatile unsigned int _zzq_result;                            \
+    _zzq_args[0] = (unsigned int)(_zzq_request);                  \
+    _zzq_args[1] = (unsigned int)(_zzq_arg1);                     \
+    _zzq_args[2] = (unsigned int)(_zzq_arg2);                     \
+    _zzq_args[3] = (unsigned int)(_zzq_arg3);                     \
+    _zzq_args[4] = (unsigned int)(_zzq_arg4);                     \
+    _zzq_args[5] = (unsigned int)(_zzq_arg5);                     \
+    __asm__ volatile(__SPECIAL_INSTRUCTION_PREAMBLE               \
+                     /* %EDX = client_request ( %EAX ) */         \
+                     "xchgl %%ebx,%%ebx"                          \
+                     : "=d" (_zzq_result)                         \
+                     : "a" (&_zzq_args[0]), "0" (_zzq_default)    \
+                     : "cc", "memory"                             \
+                    );                                            \
+    _zzq_rlval = _zzq_result;                                     \
+  }
+
+#define VALGRIND_GET_NR_CONTEXT(_zzq_rlval)                       \
+  { volatile OrigFn* _zzq_orig = &(_zzq_rlval);                   \
+    volatile unsigned int __addr;                                 \
+    __asm__ volatile(__SPECIAL_INSTRUCTION_PREAMBLE               \
+                     /* %EAX = guest_NRADDR */                    \
+                     "xchgl %%ecx,%%ecx"                          \
+                     : "=a" (__addr)                              \
+                     :                                            \
+                     : "cc", "memory"                             \
+                    );                                            \
+    _zzq_orig->nraddr = __addr;                                   \
+  }
+
+#define VALGRIND_CALL_NOREDIR_EAX                                 \
+                     __SPECIAL_INSTRUCTION_PREAMBLE               \
+                     /* call-noredir *%EAX */                     \
+                     "xchgl %%edx,%%edx\n\t"
+#endif /* PLAT_x86_linux || PLAT_x86_darwin || (PLAT_x86_win32 && __GNUC__) */
+
+/* ------------------------- x86-Win32 ------------------------- */
+
+#if defined(PLAT_x86_win32) && !defined(__GNUC__)
+
+typedef
+   struct { 
+      unsigned int nraddr; /* where's the code? */
+   }
+   OrigFn;
+
+#if defined(_MSC_VER)
+
+#define __SPECIAL_INSTRUCTION_PREAMBLE                            \
+                     __asm rol edi, 3  __asm rol edi, 13          \
+                     __asm rol edi, 29 __asm rol edi, 19
+
+#define VALGRIND_DO_CLIENT_REQUEST(                               \
+        _zzq_rlval, _zzq_default, _zzq_request,                   \
+        _zzq_arg1, _zzq_arg2, _zzq_arg3, _zzq_arg4, _zzq_arg5)    \
+  { volatile uintptr_t _zzq_args[6];                              \
+    volatile unsigned int _zzq_result;                            \
+    _zzq_args[0] = (uintptr_t)(_zzq_request);                     \
+    _zzq_args[1] = (uintptr_t)(_zzq_arg1);                        \
+    _zzq_args[2] = (uintptr_t)(_zzq_arg2);                        \
+    _zzq_args[3] = (uintptr_t)(_zzq_arg3);                        \
+    _zzq_args[4] = (uintptr_t)(_zzq_arg4);                        \
+    _zzq_args[5] = (uintptr_t)(_zzq_arg5);                        \
+    __asm { __asm lea eax, _zzq_args __asm mov edx, _zzq_default  \
+            __SPECIAL_INSTRUCTION_PREAMBLE                        \
+            /* %EDX = client_request ( %EAX ) */                  \
+            __asm xchg ebx,ebx                                    \
+            __asm mov _zzq_result, edx                            \
+    }                                                             \
+    _zzq_rlval = _zzq_result;                                     \
+  }
+
+#define VALGRIND_GET_NR_CONTEXT(_zzq_rlval)                       \
+  { volatile OrigFn* _zzq_orig = &(_zzq_rlval);                   \
+    volatile unsigned int __addr;                                 \
+    __asm { __SPECIAL_INSTRUCTION_PREAMBLE                        \
+            /* %EAX = guest_NRADDR */                             \
+            __asm xchg ecx,ecx                                    \
+            __asm mov __addr, eax                                 \
+    }                                                             \
+    _zzq_orig->nraddr = __addr;                                   \
+  }
+
+#define VALGRIND_CALL_NOREDIR_EAX ERROR
+
+#else
+#error Unsupported compiler.
+#endif
+
+#endif /* PLAT_x86_win32 */
+
+/* ------------------------ amd64-{linux,darwin} --------------- */
+
+#if defined(PLAT_amd64_linux)  ||  defined(PLAT_amd64_darwin)
+
+typedef
+   struct { 
+      unsigned long long int nraddr; /* where's the code? */
+   }
+   OrigFn;
+
+#define __SPECIAL_INSTRUCTION_PREAMBLE                            \
+                     "rolq $3,  %%rdi ; rolq $13, %%rdi\n\t"      \
+                     "rolq $61, %%rdi ; rolq $51, %%rdi\n\t"
+
+#define VALGRIND_DO_CLIENT_REQUEST(                               \
+        _zzq_rlval, _zzq_default, _zzq_request,                   \
+        _zzq_arg1, _zzq_arg2, _zzq_arg3, _zzq_arg4, _zzq_arg5)    \
+  { volatile unsigned long long int _zzq_args[6];                 \
+    volatile unsigned long long int _zzq_result;                  \
+    _zzq_args[0] = (unsigned long long int)(_zzq_request);        \
+    _zzq_args[1] = (unsigned long long int)(_zzq_arg1);           \
+    _zzq_args[2] = (unsigned long long int)(_zzq_arg2);           \
+    _zzq_args[3] = (unsigned long long int)(_zzq_arg3);           \
+    _zzq_args[4] = (unsigned long long int)(_zzq_arg4);           \
+    _zzq_args[5] = (unsigned long long int)(_zzq_arg5);           \
+    __asm__ volatile(__SPECIAL_INSTRUCTION_PREAMBLE               \
+                     /* %RDX = client_request ( %RAX ) */         \
+                     "xchgq %%rbx,%%rbx"                          \
+                     : "=d" (_zzq_result)                         \
+                     : "a" (&_zzq_args[0]), "0" (_zzq_default)    \
+                     : "cc", "memory"                             \
+                    );                                            \
+    _zzq_rlval = _zzq_result;                                     \
+  }
+
+#define VALGRIND_GET_NR_CONTEXT(_zzq_rlval)                       \
+  { volatile OrigFn* _zzq_orig = &(_zzq_rlval);                   \
+    volatile unsigned long long int __addr;                       \
+    __asm__ volatile(__SPECIAL_INSTRUCTION_PREAMBLE               \
+                     /* %RAX = guest_NRADDR */                    \
+                     "xchgq %%rcx,%%rcx"                          \
+                     : "=a" (__addr)                              \
+                     :                                            \
+                     : "cc", "memory"                             \
+                    );                                            \
+    _zzq_orig->nraddr = __addr;                                   \
+  }
+
+#define VALGRIND_CALL_NOREDIR_RAX                                 \
+                     __SPECIAL_INSTRUCTION_PREAMBLE               \
+                     /* call-noredir *%RAX */                     \
+                     "xchgq %%rdx,%%rdx\n\t"
+#endif /* PLAT_amd64_linux || PLAT_amd64_darwin */
+
+/* ------------------------ ppc32-linux ------------------------ */
+
+#if defined(PLAT_ppc32_linux)
+
+typedef
+   struct { 
+      unsigned int nraddr; /* where's the code? */
+   }
+   OrigFn;
+
+#define __SPECIAL_INSTRUCTION_PREAMBLE                            \
+                     "rlwinm 0,0,3,0,0  ; rlwinm 0,0,13,0,0\n\t"  \
+                     "rlwinm 0,0,29,0,0 ; rlwinm 0,0,19,0,0\n\t"
+
+#define VALGRIND_DO_CLIENT_REQUEST(                               \
+        _zzq_rlval, _zzq_default, _zzq_request,                   \
+        _zzq_arg1, _zzq_arg2, _zzq_arg3, _zzq_arg4, _zzq_arg5)    \
+                                                                  \
+  {          unsigned int  _zzq_args[6];                          \
+             unsigned int  _zzq_result;                           \
+             unsigned int* _zzq_ptr;                              \
+    _zzq_args[0] = (unsigned int)(_zzq_request);                  \
+    _zzq_args[1] = (unsigned int)(_zzq_arg1);                     \
+    _zzq_args[2] = (unsigned int)(_zzq_arg2);                     \
+    _zzq_args[3] = (unsigned int)(_zzq_arg3);                     \
+    _zzq_args[4] = (unsigned int)(_zzq_arg4);                     \
+    _zzq_args[5] = (unsigned int)(_zzq_arg5);                     \
+    _zzq_ptr = _zzq_args;                                         \
+    __asm__ volatile("mr 3,%1\n\t" /*default*/                    \
+                     "mr 4,%2\n\t" /*ptr*/                        \
+                     __SPECIAL_INSTRUCTION_PREAMBLE               \
+                     /* %R3 = client_request ( %R4 ) */           \
+                     "or 1,1,1\n\t"                               \
+                     "mr %0,3"     /*result*/                     \
+                     : "=b" (_zzq_result)                         \
+                     : "b" (_zzq_default), "b" (_zzq_ptr)         \
+                     : "cc", "memory", "r3", "r4");               \
+    _zzq_rlval = _zzq_result;                                     \
+  }
+
+#define VALGRIND_GET_NR_CONTEXT(_zzq_rlval)                       \
+  { volatile OrigFn* _zzq_orig = &(_zzq_rlval);                   \
+    unsigned int __addr;                                          \
+    __asm__ volatile(__SPECIAL_INSTRUCTION_PREAMBLE               \
+                     /* %R3 = guest_NRADDR */                     \
+                     "or 2,2,2\n\t"                               \
+                     "mr %0,3"                                    \
+                     : "=b" (__addr)                              \
+                     :                                            \
+                     : "cc", "memory", "r3"                       \
+                    );                                            \
+    _zzq_orig->nraddr = __addr;                                   \
+  }
+
+#define VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R11                   \
+                     __SPECIAL_INSTRUCTION_PREAMBLE               \
+                     /* branch-and-link-to-noredir *%R11 */       \
+                     "or 3,3,3\n\t"
+#endif /* PLAT_ppc32_linux */
+
+/* ------------------------ ppc64-linux ------------------------ */
+
+#if defined(PLAT_ppc64_linux)
+
+typedef
+   struct { 
+      unsigned long long int nraddr; /* where's the code? */
+      unsigned long long int r2;  /* what tocptr do we need? */
+   }
+   OrigFn;
+
+#define __SPECIAL_INSTRUCTION_PREAMBLE                            \
+                     "rotldi 0,0,3  ; rotldi 0,0,13\n\t"          \
+                     "rotldi 0,0,61 ; rotldi 0,0,51\n\t"
+
+#define VALGRIND_DO_CLIENT_REQUEST(                               \
+        _zzq_rlval, _zzq_default, _zzq_request,                   \
+        _zzq_arg1, _zzq_arg2, _zzq_arg3, _zzq_arg4, _zzq_arg5)    \
+                                                                  \
+  {          unsigned long long int  _zzq_args[6];                \
+    register unsigned long long int  _zzq_result __asm__("r3");   \
+    register unsigned long long int* _zzq_ptr __asm__("r4");      \
+    _zzq_args[0] = (unsigned long long int)(_zzq_request);        \
+    _zzq_args[1] = (unsigned long long int)(_zzq_arg1);           \
+    _zzq_args[2] = (unsigned long long int)(_zzq_arg2);           \
+    _zzq_args[3] = (unsigned long long int)(_zzq_arg3);           \
+    _zzq_args[4] = (unsigned long long int)(_zzq_arg4);           \
+    _zzq_args[5] = (unsigned long long int)(_zzq_arg5);           \
+    _zzq_ptr = _zzq_args;                                         \
+    __asm__ volatile(__SPECIAL_INSTRUCTION_PREAMBLE               \
+                     /* %R3 = client_request ( %R4 ) */           \
+                     "or 1,1,1"                                   \
+                     : "=r" (_zzq_result)                         \
+                     : "0" (_zzq_default), "r" (_zzq_ptr)         \
+                     : "cc", "memory");                           \
+    _zzq_rlval = _zzq_result;                                     \
+  }
+
+#define VALGRIND_GET_NR_CONTEXT(_zzq_rlval)                       \
+  { volatile OrigFn* _zzq_orig = &(_zzq_rlval);                   \
+    register unsigned long long int __addr __asm__("r3");         \
+    __asm__ volatile(__SPECIAL_INSTRUCTION_PREAMBLE               \
+                     /* %R3 = guest_NRADDR */                     \
+                     "or 2,2,2"                                   \
+                     : "=r" (__addr)                              \
+                     :                                            \
+                     : "cc", "memory"                             \
+                    );                                            \
+    _zzq_orig->nraddr = __addr;                                   \
+    __asm__ volatile(__SPECIAL_INSTRUCTION_PREAMBLE               \
+                     /* %R3 = guest_NRADDR_GPR2 */                \
+                     "or 4,4,4"                                   \
+                     : "=r" (__addr)                              \
+                     :                                            \
+                     : "cc", "memory"                             \
+                    );                                            \
+    _zzq_orig->r2 = __addr;                                       \
+  }
+
+#define VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R11                   \
+                     __SPECIAL_INSTRUCTION_PREAMBLE               \
+                     /* branch-and-link-to-noredir *%R11 */       \
+                     "or 3,3,3\n\t"
+
+#endif /* PLAT_ppc64_linux */
+
+/* ------------------------- arm-linux ------------------------- */
+
+#if defined(PLAT_arm_linux)
+
+typedef
+   struct { 
+      unsigned int nraddr; /* where's the code? */
+   }
+   OrigFn;
+
+#define __SPECIAL_INSTRUCTION_PREAMBLE                            \
+            "mov r12, r12, ror #3  ; mov r12, r12, ror #13 \n\t"  \
+            "mov r12, r12, ror #29 ; mov r12, r12, ror #19 \n\t"
+
+#define VALGRIND_DO_CLIENT_REQUEST(                               \
+        _zzq_rlval, _zzq_default, _zzq_request,                   \
+        _zzq_arg1, _zzq_arg2, _zzq_arg3, _zzq_arg4, _zzq_arg5)    \
+                                                                  \
+  { volatile unsigned int  _zzq_args[6];                          \
+    volatile unsigned int  _zzq_result;                           \
+    _zzq_args[0] = (unsigned int)(_zzq_request);                  \
+    _zzq_args[1] = (unsigned int)(_zzq_arg1);                     \
+    _zzq_args[2] = (unsigned int)(_zzq_arg2);                     \
+    _zzq_args[3] = (unsigned int)(_zzq_arg3);                     \
+    _zzq_args[4] = (unsigned int)(_zzq_arg4);                     \
+    _zzq_args[5] = (unsigned int)(_zzq_arg5);                     \
+    __asm__ volatile("mov r3, %1\n\t" /*default*/                 \
+                     "mov r4, %2\n\t" /*ptr*/                     \
+                     __SPECIAL_INSTRUCTION_PREAMBLE               \
+                     /* R3 = client_request ( R4 ) */             \
+                     "orr r10, r10, r10\n\t"                      \
+                     "mov %0, r3"     /*result*/                  \
+                     : "=r" (_zzq_result)                         \
+                     : "r" (_zzq_default), "r" (&_zzq_args[0])    \
+                     : "cc","memory", "r3", "r4");                \
+    _zzq_rlval = _zzq_result;                                     \
+  }
+
+#define VALGRIND_GET_NR_CONTEXT(_zzq_rlval)                       \
+  { volatile OrigFn* _zzq_orig = &(_zzq_rlval);                   \
+    unsigned int __addr;                                          \
+    __asm__ volatile(__SPECIAL_INSTRUCTION_PREAMBLE               \
+                     /* R3 = guest_NRADDR */                      \
+                     "orr r11, r11, r11\n\t"                      \
+                     "mov %0, r3"                                 \
+                     : "=r" (__addr)                              \
+                     :                                            \
+                     : "cc", "memory", "r3"                       \
+                    );                                            \
+    _zzq_orig->nraddr = __addr;                                   \
+  }
+
+#define VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R4                    \
+                     __SPECIAL_INSTRUCTION_PREAMBLE               \
+                     /* branch-and-link-to-noredir *%R4 */        \
+                     "orr r12, r12, r12\n\t"
+
+#endif /* PLAT_arm_linux */
+
+/* ------------------------ ppc32-aix5 ------------------------- */
+
+#if defined(PLAT_ppc32_aix5)
+
+typedef
+   struct { 
+      unsigned int nraddr; /* where's the code? */
+      unsigned int r2;  /* what tocptr do we need? */
+   }
+   OrigFn;
+
+#define __SPECIAL_INSTRUCTION_PREAMBLE                            \
+                     "rlwinm 0,0,3,0,0  ; rlwinm 0,0,13,0,0\n\t"  \
+                     "rlwinm 0,0,29,0,0 ; rlwinm 0,0,19,0,0\n\t"
+
+#define VALGRIND_DO_CLIENT_REQUEST(                               \
+        _zzq_rlval, _zzq_default, _zzq_request,                   \
+        _zzq_arg1, _zzq_arg2, _zzq_arg3, _zzq_arg4, _zzq_arg5)    \
+                                                                  \
+  {          unsigned int  _zzq_args[7];                          \
+    register unsigned int  _zzq_result;                           \
+    register unsigned int* _zzq_ptr;                              \
+    _zzq_args[0] = (unsigned int)(_zzq_request);                  \
+    _zzq_args[1] = (unsigned int)(_zzq_arg1);                     \
+    _zzq_args[2] = (unsigned int)(_zzq_arg2);                     \
+    _zzq_args[3] = (unsigned int)(_zzq_arg3);                     \
+    _zzq_args[4] = (unsigned int)(_zzq_arg4);                     \
+    _zzq_args[5] = (unsigned int)(_zzq_arg5);                     \
+    _zzq_args[6] = (unsigned int)(_zzq_default);                  \
+    _zzq_ptr = _zzq_args;                                         \
+    __asm__ volatile("mr 4,%1\n\t"                                \
+                     "lwz 3, 24(4)\n\t"                           \
+                     __SPECIAL_INSTRUCTION_PREAMBLE               \
+                     /* %R3 = client_request ( %R4 ) */           \
+                     "or 1,1,1\n\t"                               \
+                     "mr %0,3"                                    \
+                     : "=b" (_zzq_result)                         \
+                     : "b" (_zzq_ptr)                             \
+                     : "r3", "r4", "cc", "memory");               \
+    _zzq_rlval = _zzq_result;                                     \
+  }
+
+#define VALGRIND_GET_NR_CONTEXT(_zzq_rlval)                       \
+  { volatile OrigFn* _zzq_orig = &(_zzq_rlval);                   \
+    register unsigned int __addr;                                 \
+    __asm__ volatile(__SPECIAL_INSTRUCTION_PREAMBLE               \
+                     /* %R3 = guest_NRADDR */                     \
+                     "or 2,2,2\n\t"                               \
+                     "mr %0,3"                                    \
+                     : "=b" (__addr)                              \
+                     :                                            \
+                     : "r3", "cc", "memory"                       \
+                    );                                            \
+    _zzq_orig->nraddr = __addr;                                   \
+    __asm__ volatile(__SPECIAL_INSTRUCTION_PREAMBLE               \
+                     /* %R3 = guest_NRADDR_GPR2 */                \
+                     "or 4,4,4\n\t"                               \
+                     "mr %0,3"                                    \
+                     : "=b" (__addr)                              \
+                     :                                            \
+                     : "r3", "cc", "memory"                       \
+                    );                                            \
+    _zzq_orig->r2 = __addr;                                       \
+  }
+
+#define VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R11                   \
+                     __SPECIAL_INSTRUCTION_PREAMBLE               \
+                     /* branch-and-link-to-noredir *%R11 */       \
+                     "or 3,3,3\n\t"
+
+#endif /* PLAT_ppc32_aix5 */
+
+/* ------------------------ ppc64-aix5 ------------------------- */
+
+#if defined(PLAT_ppc64_aix5)
+
+typedef
+   struct { 
+      unsigned long long int nraddr; /* where's the code? */
+      unsigned long long int r2;  /* what tocptr do we need? */
+   }
+   OrigFn;
+
+#define __SPECIAL_INSTRUCTION_PREAMBLE                            \
+                     "rotldi 0,0,3  ; rotldi 0,0,13\n\t"          \
+                     "rotldi 0,0,61 ; rotldi 0,0,51\n\t"
+
+#define VALGRIND_DO_CLIENT_REQUEST(                               \
+        _zzq_rlval, _zzq_default, _zzq_request,                   \
+        _zzq_arg1, _zzq_arg2, _zzq_arg3, _zzq_arg4, _zzq_arg5)    \
+                                                                  \
+  {          unsigned long long int  _zzq_args[7];                \
+    register unsigned long long int  _zzq_result;                 \
+    register unsigned long long int* _zzq_ptr;                    \
+    _zzq_args[0] = (unsigned int long long)(_zzq_request);        \
+    _zzq_args[1] = (unsigned int long long)(_zzq_arg1);           \
+    _zzq_args[2] = (unsigned int long long)(_zzq_arg2);           \
+    _zzq_args[3] = (unsigned int long long)(_zzq_arg3);           \
+    _zzq_args[4] = (unsigned int long long)(_zzq_arg4);           \
+    _zzq_args[5] = (unsigned int long long)(_zzq_arg5);           \
+    _zzq_args[6] = (unsigned int long long)(_zzq_default);        \
+    _zzq_ptr = _zzq_args;                                         \
+    __asm__ volatile("mr 4,%1\n\t"                                \
+                     "ld 3, 48(4)\n\t"                            \
+                     __SPECIAL_INSTRUCTION_PREAMBLE               \
+                     /* %R3 = client_request ( %R4 ) */           \
+                     "or 1,1,1\n\t"                               \
+                     "mr %0,3"                                    \
+                     : "=b" (_zzq_result)                         \
+                     : "b" (_zzq_ptr)                             \
+                     : "r3", "r4", "cc", "memory");               \
+    _zzq_rlval = _zzq_result;                                     \
+  }
+
+#define VALGRIND_GET_NR_CONTEXT(_zzq_rlval)                       \
+  { volatile OrigFn* _zzq_orig = &(_zzq_rlval);                   \
+    register unsigned long long int __addr;                       \
+    __asm__ volatile(__SPECIAL_INSTRUCTION_PREAMBLE               \
+                     /* %R3 = guest_NRADDR */                     \
+                     "or 2,2,2\n\t"                               \
+                     "mr %0,3"                                    \
+                     : "=b" (__addr)                              \
+                     :                                            \
+                     : "r3", "cc", "memory"                       \
+                    );                                            \
+    _zzq_orig->nraddr = __addr;                                   \
+    __asm__ volatile(__SPECIAL_INSTRUCTION_PREAMBLE               \
+                     /* %R3 = guest_NRADDR_GPR2 */                \
+                     "or 4,4,4\n\t"                               \
+                     "mr %0,3"                                    \
+                     : "=b" (__addr)                              \
+                     :                                            \
+                     : "r3", "cc", "memory"                       \
+                    );                                            \
+    _zzq_orig->r2 = __addr;                                       \
+  }
+
+#define VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R11                   \
+                     __SPECIAL_INSTRUCTION_PREAMBLE               \
+                     /* branch-and-link-to-noredir *%R11 */       \
+                     "or 3,3,3\n\t"
+
+#endif /* PLAT_ppc64_aix5 */
+
+/* Insert assembly code for other platforms here... */
+
+#endif /* NVALGRIND */
+
+
+/* ------------------------------------------------------------------ */
+/* PLATFORM SPECIFICS for FUNCTION WRAPPING.  This is all very        */
+/* ugly.  It's the least-worst tradeoff I can think of.               */
+/* ------------------------------------------------------------------ */
+
+/* This section defines magic (a.k.a appalling-hack) macros for doing
+   guaranteed-no-redirection macros, so as to get from function
+   wrappers to the functions they are wrapping.  The whole point is to
+   construct standard call sequences, but to do the call itself with a
+   special no-redirect call pseudo-instruction that the JIT
+   understands and handles specially.  This section is long and
+   repetitious, and I can't see a way to make it shorter.
+
+   The naming scheme is as follows:
+
+      CALL_FN_{W,v}_{v,W,WW,WWW,WWWW,5W,6W,7W,etc}
+
+   'W' stands for "word" and 'v' for "void".  Hence there are
+   different macros for calling arity 0, 1, 2, 3, 4, etc, functions,
+   and for each, the possibility of returning a word-typed result, or
+   no result.
+*/
+
+/* Use these to write the name of your wrapper.  NOTE: duplicates
+   VG_WRAP_FUNCTION_Z{U,Z} in pub_tool_redir.h. */
+
+/* Use an extra level of macroisation so as to ensure the soname/fnname
+   args are fully macro-expanded before pasting them together. */
+#define VG_CONCAT4(_aa,_bb,_cc,_dd) _aa##_bb##_cc##_dd
+
+#define I_WRAP_SONAME_FNNAME_ZU(soname,fnname)                    \
+   VG_CONCAT4(_vgwZU_,soname,_,fnname)
+
+#define I_WRAP_SONAME_FNNAME_ZZ(soname,fnname)                    \
+   VG_CONCAT4(_vgwZZ_,soname,_,fnname)
+
+/* Use this macro from within a wrapper function to collect the
+   context (address and possibly other info) of the original function.
+   Once you have that you can then use it in one of the CALL_FN_
+   macros.  The type of the argument _lval is OrigFn. */
+#define VALGRIND_GET_ORIG_FN(_lval)  VALGRIND_GET_NR_CONTEXT(_lval)
+
+/* Derivatives of the main macros below, for calling functions
+   returning void. */
+
+#define CALL_FN_v_v(fnptr)                                        \
+   do { volatile unsigned long _junk;                             \
+        CALL_FN_W_v(_junk,fnptr); } while (0)
+
+#define CALL_FN_v_W(fnptr, arg1)                                  \
+   do { volatile unsigned long _junk;                             \
+        CALL_FN_W_W(_junk,fnptr,arg1); } while (0)
+
+#define CALL_FN_v_WW(fnptr, arg1,arg2)                            \
+   do { volatile unsigned long _junk;                             \
+        CALL_FN_W_WW(_junk,fnptr,arg1,arg2); } while (0)
+
+#define CALL_FN_v_WWW(fnptr, arg1,arg2,arg3)                      \
+   do { volatile unsigned long _junk;                             \
+        CALL_FN_W_WWW(_junk,fnptr,arg1,arg2,arg3); } while (0)
+
+#define CALL_FN_v_WWWW(fnptr, arg1,arg2,arg3,arg4)                \
+   do { volatile unsigned long _junk;                             \
+        CALL_FN_W_WWWW(_junk,fnptr,arg1,arg2,arg3,arg4); } while (0)
+
+#define CALL_FN_v_5W(fnptr, arg1,arg2,arg3,arg4,arg5)             \
+   do { volatile unsigned long _junk;                             \
+        CALL_FN_W_5W(_junk,fnptr,arg1,arg2,arg3,arg4,arg5); } while (0)
+
+#define CALL_FN_v_6W(fnptr, arg1,arg2,arg3,arg4,arg5,arg6)        \
+   do { volatile unsigned long _junk;                             \
+        CALL_FN_W_6W(_junk,fnptr,arg1,arg2,arg3,arg4,arg5,arg6); } while (0)
+
+#define CALL_FN_v_7W(fnptr, arg1,arg2,arg3,arg4,arg5,arg6,arg7)   \
+   do { volatile unsigned long _junk;                             \
+        CALL_FN_W_7W(_junk,fnptr,arg1,arg2,arg3,arg4,arg5,arg6,arg7); } while (0)
+
+/* ------------------------- x86-{linux,darwin} ---------------- */
+
+#if defined(PLAT_x86_linux)  ||  defined(PLAT_x86_darwin)
+
+/* These regs are trashed by the hidden call.  No need to mention eax
+   as gcc can already see that, plus causes gcc to bomb. */
+#define __CALLER_SAVED_REGS /*"eax"*/ "ecx", "edx"
+
+/* These CALL_FN_ macros assume that on x86-linux, sizeof(unsigned
+   long) == 4. */
+
+#define CALL_FN_W_v(lval, orig)                                   \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[1];                          \
+      volatile unsigned long _res;                                \
+      _argvec[0] = (unsigned long)_orig.nraddr;                   \
+      __asm__ volatile(                                           \
+         "movl (%%eax), %%eax\n\t"  /* target->%eax */            \
+         VALGRIND_CALL_NOREDIR_EAX                                \
+         : /*out*/   "=a" (_res)                                  \
+         : /*in*/    "a" (&_argvec[0])                            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS          \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_W(lval, orig, arg1)                             \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[2];                          \
+      volatile unsigned long _res;                                \
+      _argvec[0] = (unsigned long)_orig.nraddr;                   \
+      _argvec[1] = (unsigned long)(arg1);                         \
+      __asm__ volatile(                                           \
+         "subl $12, %%esp\n\t"                                    \
+         "pushl 4(%%eax)\n\t"                                     \
+         "movl (%%eax), %%eax\n\t"  /* target->%eax */            \
+         VALGRIND_CALL_NOREDIR_EAX                                \
+         "addl $16, %%esp\n"                                      \
+         : /*out*/   "=a" (_res)                                  \
+         : /*in*/    "a" (&_argvec[0])                            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS          \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_WW(lval, orig, arg1,arg2)                       \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[3];                          \
+      volatile unsigned long _res;                                \
+      _argvec[0] = (unsigned long)_orig.nraddr;                   \
+      _argvec[1] = (unsigned long)(arg1);                         \
+      _argvec[2] = (unsigned long)(arg2);                         \
+      __asm__ volatile(                                           \
+         "subl $8, %%esp\n\t"                                     \
+         "pushl 8(%%eax)\n\t"                                     \
+         "pushl 4(%%eax)\n\t"                                     \
+         "movl (%%eax), %%eax\n\t"  /* target->%eax */            \
+         VALGRIND_CALL_NOREDIR_EAX                                \
+         "addl $16, %%esp\n"                                      \
+         : /*out*/   "=a" (_res)                                  \
+         : /*in*/    "a" (&_argvec[0])                            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS          \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_WWW(lval, orig, arg1,arg2,arg3)                 \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[4];                          \
+      volatile unsigned long _res;                                \
+      _argvec[0] = (unsigned long)_orig.nraddr;                   \
+      _argvec[1] = (unsigned long)(arg1);                         \
+      _argvec[2] = (unsigned long)(arg2);                         \
+      _argvec[3] = (unsigned long)(arg3);                         \
+      __asm__ volatile(                                           \
+         "subl $4, %%esp\n\t"                                     \
+         "pushl 12(%%eax)\n\t"                                    \
+         "pushl 8(%%eax)\n\t"                                     \
+         "pushl 4(%%eax)\n\t"                                     \
+         "movl (%%eax), %%eax\n\t"  /* target->%eax */            \
+         VALGRIND_CALL_NOREDIR_EAX                                \
+         "addl $16, %%esp\n"                                      \
+         : /*out*/   "=a" (_res)                                  \
+         : /*in*/    "a" (&_argvec[0])                            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS          \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_WWWW(lval, orig, arg1,arg2,arg3,arg4)           \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[5];                          \
+      volatile unsigned long _res;                                \
+      _argvec[0] = (unsigned long)_orig.nraddr;                   \
+      _argvec[1] = (unsigned long)(arg1);                         \
+      _argvec[2] = (unsigned long)(arg2);                         \
+      _argvec[3] = (unsigned long)(arg3);                         \
+      _argvec[4] = (unsigned long)(arg4);                         \
+      __asm__ volatile(                                           \
+         "pushl 16(%%eax)\n\t"                                    \
+         "pushl 12(%%eax)\n\t"                                    \
+         "pushl 8(%%eax)\n\t"                                     \
+         "pushl 4(%%eax)\n\t"                                     \
+         "movl (%%eax), %%eax\n\t"  /* target->%eax */            \
+         VALGRIND_CALL_NOREDIR_EAX                                \
+         "addl $16, %%esp\n"                                      \
+         : /*out*/   "=a" (_res)                                  \
+         : /*in*/    "a" (&_argvec[0])                            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS          \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_5W(lval, orig, arg1,arg2,arg3,arg4,arg5)        \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[6];                          \
+      volatile unsigned long _res;                                \
+      _argvec[0] = (unsigned long)_orig.nraddr;                   \
+      _argvec[1] = (unsigned long)(arg1);                         \
+      _argvec[2] = (unsigned long)(arg2);                         \
+      _argvec[3] = (unsigned long)(arg3);                         \
+      _argvec[4] = (unsigned long)(arg4);                         \
+      _argvec[5] = (unsigned long)(arg5);                         \
+      __asm__ volatile(                                           \
+         "subl $12, %%esp\n\t"                                    \
+         "pushl 20(%%eax)\n\t"                                    \
+         "pushl 16(%%eax)\n\t"                                    \
+         "pushl 12(%%eax)\n\t"                                    \
+         "pushl 8(%%eax)\n\t"                                     \
+         "pushl 4(%%eax)\n\t"                                     \
+         "movl (%%eax), %%eax\n\t"  /* target->%eax */            \
+         VALGRIND_CALL_NOREDIR_EAX                                \
+         "addl $32, %%esp\n"                                      \
+         : /*out*/   "=a" (_res)                                  \
+         : /*in*/    "a" (&_argvec[0])                            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS          \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_6W(lval, orig, arg1,arg2,arg3,arg4,arg5,arg6)   \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[7];                          \
+      volatile unsigned long _res;                                \
+      _argvec[0] = (unsigned long)_orig.nraddr;                   \
+      _argvec[1] = (unsigned long)(arg1);                         \
+      _argvec[2] = (unsigned long)(arg2);                         \
+      _argvec[3] = (unsigned long)(arg3);                         \
+      _argvec[4] = (unsigned long)(arg4);                         \
+      _argvec[5] = (unsigned long)(arg5);                         \
+      _argvec[6] = (unsigned long)(arg6);                         \
+      __asm__ volatile(                                           \
+         "subl $8, %%esp\n\t"                                     \
+         "pushl 24(%%eax)\n\t"                                    \
+         "pushl 20(%%eax)\n\t"                                    \
+         "pushl 16(%%eax)\n\t"                                    \
+         "pushl 12(%%eax)\n\t"                                    \
+         "pushl 8(%%eax)\n\t"                                     \
+         "pushl 4(%%eax)\n\t"                                     \
+         "movl (%%eax), %%eax\n\t"  /* target->%eax */            \
+         VALGRIND_CALL_NOREDIR_EAX                                \
+         "addl $32, %%esp\n"                                      \
+         : /*out*/   "=a" (_res)                                  \
+         : /*in*/    "a" (&_argvec[0])                            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS          \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_7W(lval, orig, arg1,arg2,arg3,arg4,arg5,arg6,   \
+                                 arg7)                            \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[8];                          \
+      volatile unsigned long _res;                                \
+      _argvec[0] = (unsigned long)_orig.nraddr;                   \
+      _argvec[1] = (unsigned long)(arg1);                         \
+      _argvec[2] = (unsigned long)(arg2);                         \
+      _argvec[3] = (unsigned long)(arg3);                         \
+      _argvec[4] = (unsigned long)(arg4);                         \
+      _argvec[5] = (unsigned long)(arg5);                         \
+      _argvec[6] = (unsigned long)(arg6);                         \
+      _argvec[7] = (unsigned long)(arg7);                         \
+      __asm__ volatile(                                           \
+         "subl $4, %%esp\n\t"                                     \
+         "pushl 28(%%eax)\n\t"                                    \
+         "pushl 24(%%eax)\n\t"                                    \
+         "pushl 20(%%eax)\n\t"                                    \
+         "pushl 16(%%eax)\n\t"                                    \
+         "pushl 12(%%eax)\n\t"                                    \
+         "pushl 8(%%eax)\n\t"                                     \
+         "pushl 4(%%eax)\n\t"                                     \
+         "movl (%%eax), %%eax\n\t"  /* target->%eax */            \
+         VALGRIND_CALL_NOREDIR_EAX                                \
+         "addl $32, %%esp\n"                                      \
+         : /*out*/   "=a" (_res)                                  \
+         : /*in*/    "a" (&_argvec[0])                            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS          \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_8W(lval, orig, arg1,arg2,arg3,arg4,arg5,arg6,   \
+                                 arg7,arg8)                       \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[9];                          \
+      volatile unsigned long _res;                                \
+      _argvec[0] = (unsigned long)_orig.nraddr;                   \
+      _argvec[1] = (unsigned long)(arg1);                         \
+      _argvec[2] = (unsigned long)(arg2);                         \
+      _argvec[3] = (unsigned long)(arg3);                         \
+      _argvec[4] = (unsigned long)(arg4);                         \
+      _argvec[5] = (unsigned long)(arg5);                         \
+      _argvec[6] = (unsigned long)(arg6);                         \
+      _argvec[7] = (unsigned long)(arg7);                         \
+      _argvec[8] = (unsigned long)(arg8);                         \
+      __asm__ volatile(                                           \
+         "pushl 32(%%eax)\n\t"                                    \
+         "pushl 28(%%eax)\n\t"                                    \
+         "pushl 24(%%eax)\n\t"                                    \
+         "pushl 20(%%eax)\n\t"                                    \
+         "pushl 16(%%eax)\n\t"                                    \
+         "pushl 12(%%eax)\n\t"                                    \
+         "pushl 8(%%eax)\n\t"                                     \
+         "pushl 4(%%eax)\n\t"                                     \
+         "movl (%%eax), %%eax\n\t"  /* target->%eax */            \
+         VALGRIND_CALL_NOREDIR_EAX                                \
+         "addl $32, %%esp\n"                                      \
+         : /*out*/   "=a" (_res)                                  \
+         : /*in*/    "a" (&_argvec[0])                            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS          \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_9W(lval, orig, arg1,arg2,arg3,arg4,arg5,arg6,   \
+                                 arg7,arg8,arg9)                  \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[10];                         \
+      volatile unsigned long _res;                                \
+      _argvec[0] = (unsigned long)_orig.nraddr;                   \
+      _argvec[1] = (unsigned long)(arg1);                         \
+      _argvec[2] = (unsigned long)(arg2);                         \
+      _argvec[3] = (unsigned long)(arg3);                         \
+      _argvec[4] = (unsigned long)(arg4);                         \
+      _argvec[5] = (unsigned long)(arg5);                         \
+      _argvec[6] = (unsigned long)(arg6);                         \
+      _argvec[7] = (unsigned long)(arg7);                         \
+      _argvec[8] = (unsigned long)(arg8);                         \
+      _argvec[9] = (unsigned long)(arg9);                         \
+      __asm__ volatile(                                           \
+         "subl $12, %%esp\n\t"                                    \
+         "pushl 36(%%eax)\n\t"                                    \
+         "pushl 32(%%eax)\n\t"                                    \
+         "pushl 28(%%eax)\n\t"                                    \
+         "pushl 24(%%eax)\n\t"                                    \
+         "pushl 20(%%eax)\n\t"                                    \
+         "pushl 16(%%eax)\n\t"                                    \
+         "pushl 12(%%eax)\n\t"                                    \
+         "pushl 8(%%eax)\n\t"                                     \
+         "pushl 4(%%eax)\n\t"                                     \
+         "movl (%%eax), %%eax\n\t"  /* target->%eax */            \
+         VALGRIND_CALL_NOREDIR_EAX                                \
+         "addl $48, %%esp\n"                                      \
+         : /*out*/   "=a" (_res)                                  \
+         : /*in*/    "a" (&_argvec[0])                            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS          \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_10W(lval, orig, arg1,arg2,arg3,arg4,arg5,arg6,  \
+                                  arg7,arg8,arg9,arg10)           \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[11];                         \
+      volatile unsigned long _res;                                \
+      _argvec[0] = (unsigned long)_orig.nraddr;                   \
+      _argvec[1] = (unsigned long)(arg1);                         \
+      _argvec[2] = (unsigned long)(arg2);                         \
+      _argvec[3] = (unsigned long)(arg3);                         \
+      _argvec[4] = (unsigned long)(arg4);                         \
+      _argvec[5] = (unsigned long)(arg5);                         \
+      _argvec[6] = (unsigned long)(arg6);                         \
+      _argvec[7] = (unsigned long)(arg7);                         \
+      _argvec[8] = (unsigned long)(arg8);                         \
+      _argvec[9] = (unsigned long)(arg9);                         \
+      _argvec[10] = (unsigned long)(arg10);                       \
+      __asm__ volatile(                                           \
+         "subl $8, %%esp\n\t"                                     \
+         "pushl 40(%%eax)\n\t"                                    \
+         "pushl 36(%%eax)\n\t"                                    \
+         "pushl 32(%%eax)\n\t"                                    \
+         "pushl 28(%%eax)\n\t"                                    \
+         "pushl 24(%%eax)\n\t"                                    \
+         "pushl 20(%%eax)\n\t"                                    \
+         "pushl 16(%%eax)\n\t"                                    \
+         "pushl 12(%%eax)\n\t"                                    \
+         "pushl 8(%%eax)\n\t"                                     \
+         "pushl 4(%%eax)\n\t"                                     \
+         "movl (%%eax), %%eax\n\t"  /* target->%eax */            \
+         VALGRIND_CALL_NOREDIR_EAX                                \
+         "addl $48, %%esp\n"                                      \
+         : /*out*/   "=a" (_res)                                  \
+         : /*in*/    "a" (&_argvec[0])                            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS          \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_11W(lval, orig, arg1,arg2,arg3,arg4,arg5,       \
+                                  arg6,arg7,arg8,arg9,arg10,      \
+                                  arg11)                          \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[12];                         \
+      volatile unsigned long _res;                                \
+      _argvec[0] = (unsigned long)_orig.nraddr;                   \
+      _argvec[1] = (unsigned long)(arg1);                         \
+      _argvec[2] = (unsigned long)(arg2);                         \
+      _argvec[3] = (unsigned long)(arg3);                         \
+      _argvec[4] = (unsigned long)(arg4);                         \
+      _argvec[5] = (unsigned long)(arg5);                         \
+      _argvec[6] = (unsigned long)(arg6);                         \
+      _argvec[7] = (unsigned long)(arg7);                         \
+      _argvec[8] = (unsigned long)(arg8);                         \
+      _argvec[9] = (unsigned long)(arg9);                         \
+      _argvec[10] = (unsigned long)(arg10);                       \
+      _argvec[11] = (unsigned long)(arg11);                       \
+      __asm__ volatile(                                           \
+         "subl $4, %%esp\n\t"                                     \
+         "pushl 44(%%eax)\n\t"                                    \
+         "pushl 40(%%eax)\n\t"                                    \
+         "pushl 36(%%eax)\n\t"                                    \
+         "pushl 32(%%eax)\n\t"                                    \
+         "pushl 28(%%eax)\n\t"                                    \
+         "pushl 24(%%eax)\n\t"                                    \
+         "pushl 20(%%eax)\n\t"                                    \
+         "pushl 16(%%eax)\n\t"                                    \
+         "pushl 12(%%eax)\n\t"                                    \
+         "pushl 8(%%eax)\n\t"                                     \
+         "pushl 4(%%eax)\n\t"                                     \
+         "movl (%%eax), %%eax\n\t"  /* target->%eax */            \
+         VALGRIND_CALL_NOREDIR_EAX                                \
+         "addl $48, %%esp\n"                                      \
+         : /*out*/   "=a" (_res)                                  \
+         : /*in*/    "a" (&_argvec[0])                            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS          \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_12W(lval, orig, arg1,arg2,arg3,arg4,arg5,       \
+                                  arg6,arg7,arg8,arg9,arg10,      \
+                                  arg11,arg12)                    \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[13];                         \
+      volatile unsigned long _res;                                \
+      _argvec[0] = (unsigned long)_orig.nraddr;                   \
+      _argvec[1] = (unsigned long)(arg1);                         \
+      _argvec[2] = (unsigned long)(arg2);                         \
+      _argvec[3] = (unsigned long)(arg3);                         \
+      _argvec[4] = (unsigned long)(arg4);                         \
+      _argvec[5] = (unsigned long)(arg5);                         \
+      _argvec[6] = (unsigned long)(arg6);                         \
+      _argvec[7] = (unsigned long)(arg7);                         \
+      _argvec[8] = (unsigned long)(arg8);                         \
+      _argvec[9] = (unsigned long)(arg9);                         \
+      _argvec[10] = (unsigned long)(arg10);                       \
+      _argvec[11] = (unsigned long)(arg11);                       \
+      _argvec[12] = (unsigned long)(arg12);                       \
+      __asm__ volatile(                                           \
+         "pushl 48(%%eax)\n\t"                                    \
+         "pushl 44(%%eax)\n\t"                                    \
+         "pushl 40(%%eax)\n\t"                                    \
+         "pushl 36(%%eax)\n\t"                                    \
+         "pushl 32(%%eax)\n\t"                                    \
+         "pushl 28(%%eax)\n\t"                                    \
+         "pushl 24(%%eax)\n\t"                                    \
+         "pushl 20(%%eax)\n\t"                                    \
+         "pushl 16(%%eax)\n\t"                                    \
+         "pushl 12(%%eax)\n\t"                                    \
+         "pushl 8(%%eax)\n\t"                                     \
+         "pushl 4(%%eax)\n\t"                                     \
+         "movl (%%eax), %%eax\n\t"  /* target->%eax */            \
+         VALGRIND_CALL_NOREDIR_EAX                                \
+         "addl $48, %%esp\n"                                      \
+         : /*out*/   "=a" (_res)                                  \
+         : /*in*/    "a" (&_argvec[0])                            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS          \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#endif /* PLAT_x86_linux || PLAT_x86_darwin */
+
+/* ------------------------ amd64-{linux,darwin} --------------- */
+
+#if defined(PLAT_amd64_linux)  ||  defined(PLAT_amd64_darwin)
+
+/* ARGREGS: rdi rsi rdx rcx r8 r9 (the rest on stack in R-to-L order) */
+
+/* These regs are trashed by the hidden call. */
+#define __CALLER_SAVED_REGS /*"rax",*/ "rcx", "rdx", "rsi",       \
+                            "rdi", "r8", "r9", "r10", "r11"
+
+/* This is all pretty complex.  It's so as to make stack unwinding
+   work reliably.  See bug 243270.  The basic problem is the sub and
+   add of 128 of %rsp in all of the following macros.  If gcc believes
+   the CFA is in %rsp, then unwinding may fail, because what's at the
+   CFA is not what gcc "expected" when it constructs the CFIs for the
+   places where the macros are instantiated.
+
+   But we can't just add a CFI annotation to increase the CFA offset
+   by 128, to match the sub of 128 from %rsp, because we don't know
+   whether gcc has chosen %rsp as the CFA at that point, or whether it
+   has chosen some other register (eg, %rbp).  In the latter case,
+   adding a CFI annotation to change the CFA offset is simply wrong.
+
+   So the solution is to get hold of the CFA using
+   __builtin_dwarf_cfa(), put it in a known register, and add a
+   CFI annotation to say what the register is.  We choose %rbp for
+   this (perhaps perversely), because:
+
+   (1) %rbp is already subject to unwinding.  If a new register was
+       chosen then the unwinder would have to unwind it in all stack
+       traces, which is expensive, and
+
+   (2) %rbp is already subject to precise exception updates in the
+       JIT.  If a new register was chosen, we'd have to have precise
+       exceptions for it too, which reduces performance of the
+       generated code.
+
+   However .. one extra complication.  We can't just whack the result
+   of __builtin_dwarf_cfa() into %rbp and then add %rbp to the
+   list of trashed registers at the end of the inline assembly
+   fragments; gcc won't allow %rbp to appear in that list.  Hence
+   instead we need to stash %rbp in %r15 for the duration of the asm,
+   and say that %r15 is trashed instead.  gcc seems happy to go with
+   that.
+
+   Oh .. and this all needs to be conditionalised so that it is
+   unchanged from before this commit, when compiled with older gccs
+   that don't support __builtin_dwarf_cfa.  Furthermore, since
+   this header file is freestanding, it has to be independent of
+   config.h, and so the following conditionalisation cannot depend on
+   configure time checks.
+
+   Although it's not clear from
+   'defined(__GNUC__) && defined(__GCC_HAVE_DWARF2_CFI_ASM)',
+   this expression excludes Darwin.
+   .cfi directives in Darwin assembly appear to be completely
+   different and I haven't investigated how they work.
+
+   For even more entertainment value, note we have to use the
+   completely undocumented __builtin_dwarf_cfa(), which appears to
+   really compute the CFA, whereas __builtin_frame_address(0) claims
+   to but actually doesn't.  See
+   https://bugs.kde.org/show_bug.cgi?id=243270#c47
+*/
+#if defined(__GNUC__) && defined(__GCC_HAVE_DWARF2_CFI_ASM)
+#  define __FRAME_POINTER                                         \
+      ,"r"(__builtin_dwarf_cfa())
+#  define VALGRIND_CFI_PROLOGUE                                   \
+      "movq %%rbp, %%r15\n\t"                                     \
+      "movq %2, %%rbp\n\t"                                        \
+      ".cfi_remember_state\n\t"                                   \
+      ".cfi_def_cfa rbp, 0\n\t"
+#  define VALGRIND_CFI_EPILOGUE                                   \
+      "movq %%r15, %%rbp\n\t"                                     \
+      ".cfi_restore_state\n\t"
+#else
+#  define __FRAME_POINTER
+#  define VALGRIND_CFI_PROLOGUE
+#  define VALGRIND_CFI_EPILOGUE
+#endif
+
+
+/* These CALL_FN_ macros assume that on amd64-linux, sizeof(unsigned
+   long) == 8. */
+
+/* NB 9 Sept 07.  There is a nasty kludge here in all these CALL_FN_
+   macros.  In order not to trash the stack redzone, we need to drop
+   %rsp by 128 before the hidden call, and restore afterwards.  The
+   nastyness is that it is only by luck that the stack still appears
+   to be unwindable during the hidden call - since then the behaviour
+   of any routine using this macro does not match what the CFI data
+   says.  Sigh.
+
+   Why is this important?  Imagine that a wrapper has a stack
+   allocated local, and passes to the hidden call, a pointer to it.
+   Because gcc does not know about the hidden call, it may allocate
+   that local in the redzone.  Unfortunately the hidden call may then
+   trash it before it comes to use it.  So we must step clear of the
+   redzone, for the duration of the hidden call, to make it safe.
+
+   Probably the same problem afflicts the other redzone-style ABIs too
+   (ppc64-linux, ppc32-aix5, ppc64-aix5); but for those, the stack is
+   self describing (none of this CFI nonsense) so at least messing
+   with the stack pointer doesn't give a danger of non-unwindable
+   stack. */
+
+#define CALL_FN_W_v(lval, orig)                                   \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[1];                          \
+      volatile unsigned long _res;                                \
+      _argvec[0] = (unsigned long)_orig.nraddr;                   \
+      __asm__ volatile(                                           \
+         VALGRIND_CFI_PROLOGUE                                    \
+         "subq $128,%%rsp\n\t"                                    \
+         "movq (%%rax), %%rax\n\t"  /* target->%rax */            \
+         VALGRIND_CALL_NOREDIR_RAX                                \
+         "addq $128,%%rsp\n\t"                                    \
+         VALGRIND_CFI_EPILOGUE                                    \
+         : /*out*/   "=a" (_res)                                  \
+         : /*in*/    "a" (&_argvec[0]) __FRAME_POINTER            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS, "r15"   \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_W(lval, orig, arg1)                             \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[2];                          \
+      volatile unsigned long _res;                                \
+      _argvec[0] = (unsigned long)_orig.nraddr;                   \
+      _argvec[1] = (unsigned long)(arg1);                         \
+      __asm__ volatile(                                           \
+         VALGRIND_CFI_PROLOGUE                                    \
+         "subq $128,%%rsp\n\t"                                    \
+         "movq 8(%%rax), %%rdi\n\t"                               \
+         "movq (%%rax), %%rax\n\t"  /* target->%rax */            \
+         VALGRIND_CALL_NOREDIR_RAX                                \
+         "addq $128,%%rsp\n\t"                                    \
+         VALGRIND_CFI_EPILOGUE                                    \
+         : /*out*/   "=a" (_res)                                  \
+         : /*in*/    "a" (&_argvec[0]) __FRAME_POINTER            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS, "r15"   \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_WW(lval, orig, arg1,arg2)                       \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[3];                          \
+      volatile unsigned long _res;                                \
+      _argvec[0] = (unsigned long)_orig.nraddr;                   \
+      _argvec[1] = (unsigned long)(arg1);                         \
+      _argvec[2] = (unsigned long)(arg2);                         \
+      __asm__ volatile(                                           \
+         VALGRIND_CFI_PROLOGUE                                    \
+         "subq $128,%%rsp\n\t"                                    \
+         "movq 16(%%rax), %%rsi\n\t"                              \
+         "movq 8(%%rax), %%rdi\n\t"                               \
+         "movq (%%rax), %%rax\n\t"  /* target->%rax */            \
+         VALGRIND_CALL_NOREDIR_RAX                                \
+         "addq $128,%%rsp\n\t"                                    \
+         VALGRIND_CFI_EPILOGUE                                    \
+         : /*out*/   "=a" (_res)                                  \
+         : /*in*/    "a" (&_argvec[0]) __FRAME_POINTER            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS, "r15"   \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_WWW(lval, orig, arg1,arg2,arg3)                 \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[4];                          \
+      volatile unsigned long _res;                                \
+      _argvec[0] = (unsigned long)_orig.nraddr;                   \
+      _argvec[1] = (unsigned long)(arg1);                         \
+      _argvec[2] = (unsigned long)(arg2);                         \
+      _argvec[3] = (unsigned long)(arg3);                         \
+      __asm__ volatile(                                           \
+         VALGRIND_CFI_PROLOGUE                                    \
+         "subq $128,%%rsp\n\t"                                    \
+         "movq 24(%%rax), %%rdx\n\t"                              \
+         "movq 16(%%rax), %%rsi\n\t"                              \
+         "movq 8(%%rax), %%rdi\n\t"                               \
+         "movq (%%rax), %%rax\n\t"  /* target->%rax */            \
+         VALGRIND_CALL_NOREDIR_RAX                                \
+         "addq $128,%%rsp\n\t"                                    \
+         VALGRIND_CFI_EPILOGUE                                    \
+         : /*out*/   "=a" (_res)                                  \
+         : /*in*/    "a" (&_argvec[0]) __FRAME_POINTER            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS, "r15"   \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_WWWW(lval, orig, arg1,arg2,arg3,arg4)           \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[5];                          \
+      volatile unsigned long _res;                                \
+      _argvec[0] = (unsigned long)_orig.nraddr;                   \
+      _argvec[1] = (unsigned long)(arg1);                         \
+      _argvec[2] = (unsigned long)(arg2);                         \
+      _argvec[3] = (unsigned long)(arg3);                         \
+      _argvec[4] = (unsigned long)(arg4);                         \
+      __asm__ volatile(                                           \
+         VALGRIND_CFI_PROLOGUE                                    \
+         "subq $128,%%rsp\n\t"                                    \
+         "movq 32(%%rax), %%rcx\n\t"                              \
+         "movq 24(%%rax), %%rdx\n\t"                              \
+         "movq 16(%%rax), %%rsi\n\t"                              \
+         "movq 8(%%rax), %%rdi\n\t"                               \
+         "movq (%%rax), %%rax\n\t"  /* target->%rax */            \
+         VALGRIND_CALL_NOREDIR_RAX                                \
+         "addq $128,%%rsp\n\t"                                    \
+         VALGRIND_CFI_EPILOGUE                                    \
+         : /*out*/   "=a" (_res)                                  \
+         : /*in*/    "a" (&_argvec[0]) __FRAME_POINTER            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS, "r15"   \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_5W(lval, orig, arg1,arg2,arg3,arg4,arg5)        \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[6];                          \
+      volatile unsigned long _res;                                \
+      _argvec[0] = (unsigned long)_orig.nraddr;                   \
+      _argvec[1] = (unsigned long)(arg1);                         \
+      _argvec[2] = (unsigned long)(arg2);                         \
+      _argvec[3] = (unsigned long)(arg3);                         \
+      _argvec[4] = (unsigned long)(arg4);                         \
+      _argvec[5] = (unsigned long)(arg5);                         \
+      __asm__ volatile(                                           \
+         VALGRIND_CFI_PROLOGUE                                    \
+         "subq $128,%%rsp\n\t"                                    \
+         "movq 40(%%rax), %%r8\n\t"                               \
+         "movq 32(%%rax), %%rcx\n\t"                              \
+         "movq 24(%%rax), %%rdx\n\t"                              \
+         "movq 16(%%rax), %%rsi\n\t"                              \
+         "movq 8(%%rax), %%rdi\n\t"                               \
+         "movq (%%rax), %%rax\n\t"  /* target->%rax */            \
+         VALGRIND_CALL_NOREDIR_RAX                                \
+         "addq $128,%%rsp\n\t"                                    \
+         VALGRIND_CFI_EPILOGUE                                    \
+         : /*out*/   "=a" (_res)                                  \
+         : /*in*/    "a" (&_argvec[0]) __FRAME_POINTER            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS, "r15"   \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_6W(lval, orig, arg1,arg2,arg3,arg4,arg5,arg6)   \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[7];                          \
+      volatile unsigned long _res;                                \
+      _argvec[0] = (unsigned long)_orig.nraddr;                   \
+      _argvec[1] = (unsigned long)(arg1);                         \
+      _argvec[2] = (unsigned long)(arg2);                         \
+      _argvec[3] = (unsigned long)(arg3);                         \
+      _argvec[4] = (unsigned long)(arg4);                         \
+      _argvec[5] = (unsigned long)(arg5);                         \
+      _argvec[6] = (unsigned long)(arg6);                         \
+      __asm__ volatile(                                           \
+         VALGRIND_CFI_PROLOGUE                                    \
+         "subq $128,%%rsp\n\t"                                    \
+         "movq 48(%%rax), %%r9\n\t"                               \
+         "movq 40(%%rax), %%r8\n\t"                               \
+         "movq 32(%%rax), %%rcx\n\t"                              \
+         "movq 24(%%rax), %%rdx\n\t"                              \
+         "movq 16(%%rax), %%rsi\n\t"                              \
+         "movq 8(%%rax), %%rdi\n\t"                               \
+         "movq (%%rax), %%rax\n\t"  /* target->%rax */            \
+         VALGRIND_CALL_NOREDIR_RAX                                \
+         "addq $128,%%rsp\n\t"                                    \
+         VALGRIND_CFI_EPILOGUE                                    \
+         : /*out*/   "=a" (_res)                                  \
+         : /*in*/    "a" (&_argvec[0]) __FRAME_POINTER            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS, "r15"   \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_7W(lval, orig, arg1,arg2,arg3,arg4,arg5,arg6,   \
+                                 arg7)                            \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[8];                          \
+      volatile unsigned long _res;                                \
+      _argvec[0] = (unsigned long)_orig.nraddr;                   \
+      _argvec[1] = (unsigned long)(arg1);                         \
+      _argvec[2] = (unsigned long)(arg2);                         \
+      _argvec[3] = (unsigned long)(arg3);                         \
+      _argvec[4] = (unsigned long)(arg4);                         \
+      _argvec[5] = (unsigned long)(arg5);                         \
+      _argvec[6] = (unsigned long)(arg6);                         \
+      _argvec[7] = (unsigned long)(arg7);                         \
+      __asm__ volatile(                                           \
+         VALGRIND_CFI_PROLOGUE                                    \
+         "subq $136,%%rsp\n\t"                                    \
+         "pushq 56(%%rax)\n\t"                                    \
+         "movq 48(%%rax), %%r9\n\t"                               \
+         "movq 40(%%rax), %%r8\n\t"                               \
+         "movq 32(%%rax), %%rcx\n\t"                              \
+         "movq 24(%%rax), %%rdx\n\t"                              \
+         "movq 16(%%rax), %%rsi\n\t"                              \
+         "movq 8(%%rax), %%rdi\n\t"                               \
+         "movq (%%rax), %%rax\n\t"  /* target->%rax */            \
+         VALGRIND_CALL_NOREDIR_RAX                                \
+         "addq $8, %%rsp\n"                                       \
+         "addq $136,%%rsp\n\t"                                    \
+         VALGRIND_CFI_EPILOGUE                                    \
+         : /*out*/   "=a" (_res)                                  \
+         : /*in*/    "a" (&_argvec[0]) __FRAME_POINTER            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS, "r15"   \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_8W(lval, orig, arg1,arg2,arg3,arg4,arg5,arg6,   \
+                                 arg7,arg8)                       \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[9];                          \
+      volatile unsigned long _res;                                \
+      _argvec[0] = (unsigned long)_orig.nraddr;                   \
+      _argvec[1] = (unsigned long)(arg1);                         \
+      _argvec[2] = (unsigned long)(arg2);                         \
+      _argvec[3] = (unsigned long)(arg3);                         \
+      _argvec[4] = (unsigned long)(arg4);                         \
+      _argvec[5] = (unsigned long)(arg5);                         \
+      _argvec[6] = (unsigned long)(arg6);                         \
+      _argvec[7] = (unsigned long)(arg7);                         \
+      _argvec[8] = (unsigned long)(arg8);                         \
+      __asm__ volatile(                                           \
+         VALGRIND_CFI_PROLOGUE                                    \
+         "subq $128,%%rsp\n\t"                                    \
+         "pushq 64(%%rax)\n\t"                                    \
+         "pushq 56(%%rax)\n\t"                                    \
+         "movq 48(%%rax), %%r9\n\t"                               \
+         "movq 40(%%rax), %%r8\n\t"                               \
+         "movq 32(%%rax), %%rcx\n\t"                              \
+         "movq 24(%%rax), %%rdx\n\t"                              \
+         "movq 16(%%rax), %%rsi\n\t"                              \
+         "movq 8(%%rax), %%rdi\n\t"                               \
+         "movq (%%rax), %%rax\n\t"  /* target->%rax */            \
+         VALGRIND_CALL_NOREDIR_RAX                                \
+         "addq $16, %%rsp\n"                                      \
+         "addq $128,%%rsp\n\t"                                    \
+         VALGRIND_CFI_EPILOGUE                                    \
+         : /*out*/   "=a" (_res)                                  \
+         : /*in*/    "a" (&_argvec[0]) __FRAME_POINTER            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS, "r15"   \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_9W(lval, orig, arg1,arg2,arg3,arg4,arg5,arg6,   \
+                                 arg7,arg8,arg9)                  \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[10];                         \
+      volatile unsigned long _res;                                \
+      _argvec[0] = (unsigned long)_orig.nraddr;                   \
+      _argvec[1] = (unsigned long)(arg1);                         \
+      _argvec[2] = (unsigned long)(arg2);                         \
+      _argvec[3] = (unsigned long)(arg3);                         \
+      _argvec[4] = (unsigned long)(arg4);                         \
+      _argvec[5] = (unsigned long)(arg5);                         \
+      _argvec[6] = (unsigned long)(arg6);                         \
+      _argvec[7] = (unsigned long)(arg7);                         \
+      _argvec[8] = (unsigned long)(arg8);                         \
+      _argvec[9] = (unsigned long)(arg9);                         \
+      __asm__ volatile(                                           \
+         VALGRIND_CFI_PROLOGUE                                    \
+         "subq $136,%%rsp\n\t"                                    \
+         "pushq 72(%%rax)\n\t"                                    \
+         "pushq 64(%%rax)\n\t"                                    \
+         "pushq 56(%%rax)\n\t"                                    \
+         "movq 48(%%rax), %%r9\n\t"                               \
+         "movq 40(%%rax), %%r8\n\t"                               \
+         "movq 32(%%rax), %%rcx\n\t"                              \
+         "movq 24(%%rax), %%rdx\n\t"                              \
+         "movq 16(%%rax), %%rsi\n\t"                              \
+         "movq 8(%%rax), %%rdi\n\t"                               \
+         "movq (%%rax), %%rax\n\t"  /* target->%rax */            \
+         VALGRIND_CALL_NOREDIR_RAX                                \
+         "addq $24, %%rsp\n"                                      \
+         "addq $136,%%rsp\n\t"                                    \
+         VALGRIND_CFI_EPILOGUE                                    \
+         : /*out*/   "=a" (_res)                                  \
+         : /*in*/    "a" (&_argvec[0]) __FRAME_POINTER            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS, "r15"   \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_10W(lval, orig, arg1,arg2,arg3,arg4,arg5,arg6,  \
+                                  arg7,arg8,arg9,arg10)           \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[11];                         \
+      volatile unsigned long _res;                                \
+      _argvec[0] = (unsigned long)_orig.nraddr;                   \
+      _argvec[1] = (unsigned long)(arg1);                         \
+      _argvec[2] = (unsigned long)(arg2);                         \
+      _argvec[3] = (unsigned long)(arg3);                         \
+      _argvec[4] = (unsigned long)(arg4);                         \
+      _argvec[5] = (unsigned long)(arg5);                         \
+      _argvec[6] = (unsigned long)(arg6);                         \
+      _argvec[7] = (unsigned long)(arg7);                         \
+      _argvec[8] = (unsigned long)(arg8);                         \
+      _argvec[9] = (unsigned long)(arg9);                         \
+      _argvec[10] = (unsigned long)(arg10);                       \
+      __asm__ volatile(                                           \
+         VALGRIND_CFI_PROLOGUE                                    \
+         "subq $128,%%rsp\n\t"                                    \
+         "pushq 80(%%rax)\n\t"                                    \
+         "pushq 72(%%rax)\n\t"                                    \
+         "pushq 64(%%rax)\n\t"                                    \
+         "pushq 56(%%rax)\n\t"                                    \
+         "movq 48(%%rax), %%r9\n\t"                               \
+         "movq 40(%%rax), %%r8\n\t"                               \
+         "movq 32(%%rax), %%rcx\n\t"                              \
+         "movq 24(%%rax), %%rdx\n\t"                              \
+         "movq 16(%%rax), %%rsi\n\t"                              \
+         "movq 8(%%rax), %%rdi\n\t"                               \
+         "movq (%%rax), %%rax\n\t"  /* target->%rax */            \
+         VALGRIND_CALL_NOREDIR_RAX                                \
+         "addq $32, %%rsp\n"                                      \
+         "addq $128,%%rsp\n\t"                                    \
+         VALGRIND_CFI_EPILOGUE                                    \
+         : /*out*/   "=a" (_res)                                  \
+         : /*in*/    "a" (&_argvec[0]) __FRAME_POINTER            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS, "r15"   \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_11W(lval, orig, arg1,arg2,arg3,arg4,arg5,arg6,  \
+                                  arg7,arg8,arg9,arg10,arg11)     \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[12];                         \
+      volatile unsigned long _res;                                \
+      _argvec[0] = (unsigned long)_orig.nraddr;                   \
+      _argvec[1] = (unsigned long)(arg1);                         \
+      _argvec[2] = (unsigned long)(arg2);                         \
+      _argvec[3] = (unsigned long)(arg3);                         \
+      _argvec[4] = (unsigned long)(arg4);                         \
+      _argvec[5] = (unsigned long)(arg5);                         \
+      _argvec[6] = (unsigned long)(arg6);                         \
+      _argvec[7] = (unsigned long)(arg7);                         \
+      _argvec[8] = (unsigned long)(arg8);                         \
+      _argvec[9] = (unsigned long)(arg9);                         \
+      _argvec[10] = (unsigned long)(arg10);                       \
+      _argvec[11] = (unsigned long)(arg11);                       \
+      __asm__ volatile(                                           \
+         VALGRIND_CFI_PROLOGUE                                    \
+         "subq $136,%%rsp\n\t"                                    \
+         "pushq 88(%%rax)\n\t"                                    \
+         "pushq 80(%%rax)\n\t"                                    \
+         "pushq 72(%%rax)\n\t"                                    \
+         "pushq 64(%%rax)\n\t"                                    \
+         "pushq 56(%%rax)\n\t"                                    \
+         "movq 48(%%rax), %%r9\n\t"                               \
+         "movq 40(%%rax), %%r8\n\t"                               \
+         "movq 32(%%rax), %%rcx\n\t"                              \
+         "movq 24(%%rax), %%rdx\n\t"                              \
+         "movq 16(%%rax), %%rsi\n\t"                              \
+         "movq 8(%%rax), %%rdi\n\t"                               \
+         "movq (%%rax), %%rax\n\t"  /* target->%rax */            \
+         VALGRIND_CALL_NOREDIR_RAX                                \
+         "addq $40, %%rsp\n"                                      \
+         "addq $136,%%rsp\n\t"                                    \
+         VALGRIND_CFI_EPILOGUE                                    \
+         : /*out*/   "=a" (_res)                                  \
+         : /*in*/    "a" (&_argvec[0]) __FRAME_POINTER            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS, "r15"   \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_12W(lval, orig, arg1,arg2,arg3,arg4,arg5,arg6,  \
+                                arg7,arg8,arg9,arg10,arg11,arg12) \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[13];                         \
+      volatile unsigned long _res;                                \
+      _argvec[0] = (unsigned long)_orig.nraddr;                   \
+      _argvec[1] = (unsigned long)(arg1);                         \
+      _argvec[2] = (unsigned long)(arg2);                         \
+      _argvec[3] = (unsigned long)(arg3);                         \
+      _argvec[4] = (unsigned long)(arg4);                         \
+      _argvec[5] = (unsigned long)(arg5);                         \
+      _argvec[6] = (unsigned long)(arg6);                         \
+      _argvec[7] = (unsigned long)(arg7);                         \
+      _argvec[8] = (unsigned long)(arg8);                         \
+      _argvec[9] = (unsigned long)(arg9);                         \
+      _argvec[10] = (unsigned long)(arg10);                       \
+      _argvec[11] = (unsigned long)(arg11);                       \
+      _argvec[12] = (unsigned long)(arg12);                       \
+      __asm__ volatile(                                           \
+         VALGRIND_CFI_PROLOGUE                                    \
+         "subq $128,%%rsp\n\t"                                    \
+         "pushq 96(%%rax)\n\t"                                    \
+         "pushq 88(%%rax)\n\t"                                    \
+         "pushq 80(%%rax)\n\t"                                    \
+         "pushq 72(%%rax)\n\t"                                    \
+         "pushq 64(%%rax)\n\t"                                    \
+         "pushq 56(%%rax)\n\t"                                    \
+         "movq 48(%%rax), %%r9\n\t"                               \
+         "movq 40(%%rax), %%r8\n\t"                               \
+         "movq 32(%%rax), %%rcx\n\t"                              \
+         "movq 24(%%rax), %%rdx\n\t"                              \
+         "movq 16(%%rax), %%rsi\n\t"                              \
+         "movq 8(%%rax), %%rdi\n\t"                               \
+         "movq (%%rax), %%rax\n\t"  /* target->%rax */            \
+         VALGRIND_CALL_NOREDIR_RAX                                \
+         "addq $48, %%rsp\n"                                      \
+         "addq $128,%%rsp\n\t"                                    \
+         VALGRIND_CFI_EPILOGUE                                    \
+         : /*out*/   "=a" (_res)                                  \
+         : /*in*/    "a" (&_argvec[0]) __FRAME_POINTER            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS, "r15"   \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#endif /* PLAT_amd64_linux || PLAT_amd64_darwin */
+
+/* ------------------------ ppc32-linux ------------------------ */
+
+#if defined(PLAT_ppc32_linux)
+
+/* This is useful for finding out about the on-stack stuff:
+
+   extern int f9  ( int,int,int,int,int,int,int,int,int );
+   extern int f10 ( int,int,int,int,int,int,int,int,int,int );
+   extern int f11 ( int,int,int,int,int,int,int,int,int,int,int );
+   extern int f12 ( int,int,int,int,int,int,int,int,int,int,int,int );
+
+   int g9 ( void ) {
+      return f9(11,22,33,44,55,66,77,88,99);
+   }
+   int g10 ( void ) {
+      return f10(11,22,33,44,55,66,77,88,99,110);
+   }
+   int g11 ( void ) {
+      return f11(11,22,33,44,55,66,77,88,99,110,121);
+   }
+   int g12 ( void ) {
+      return f12(11,22,33,44,55,66,77,88,99,110,121,132);
+   }
+*/
+
+/* ARGREGS: r3 r4 r5 r6 r7 r8 r9 r10 (the rest on stack somewhere) */
+
+/* These regs are trashed by the hidden call. */
+#define __CALLER_SAVED_REGS                                       \
+   "lr", "ctr", "xer",                                            \
+   "cr0", "cr1", "cr2", "cr3", "cr4", "cr5", "cr6", "cr7",        \
+   "r0", "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10",   \
+   "r11", "r12", "r13"
+
+/* These CALL_FN_ macros assume that on ppc32-linux, 
+   sizeof(unsigned long) == 4. */
+
+#define CALL_FN_W_v(lval, orig)                                   \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[1];                          \
+      volatile unsigned long _res;                                \
+      _argvec[0] = (unsigned long)_orig.nraddr;                   \
+      __asm__ volatile(                                           \
+         "mr 11,%1\n\t"                                           \
+         "lwz 11,0(11)\n\t"  /* target->r11 */                    \
+         VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R11                  \
+         "mr %0,3"                                                \
+         : /*out*/   "=r" (_res)                                  \
+         : /*in*/    "r" (&_argvec[0])                            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS          \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_W(lval, orig, arg1)                             \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[2];                          \
+      volatile unsigned long _res;                                \
+      _argvec[0] = (unsigned long)_orig.nraddr;                   \
+      _argvec[1] = (unsigned long)arg1;                           \
+      __asm__ volatile(                                           \
+         "mr 11,%1\n\t"                                           \
+         "lwz 3,4(11)\n\t"   /* arg1->r3 */                       \
+         "lwz 11,0(11)\n\t"  /* target->r11 */                    \
+         VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R11                  \
+         "mr %0,3"                                                \
+         : /*out*/   "=r" (_res)                                  \
+         : /*in*/    "r" (&_argvec[0])                            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS          \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_WW(lval, orig, arg1,arg2)                       \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[3];                          \
+      volatile unsigned long _res;                                \
+      _argvec[0] = (unsigned long)_orig.nraddr;                   \
+      _argvec[1] = (unsigned long)arg1;                           \
+      _argvec[2] = (unsigned long)arg2;                           \
+      __asm__ volatile(                                           \
+         "mr 11,%1\n\t"                                           \
+         "lwz 3,4(11)\n\t"   /* arg1->r3 */                       \
+         "lwz 4,8(11)\n\t"                                        \
+         "lwz 11,0(11)\n\t"  /* target->r11 */                    \
+         VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R11                  \
+         "mr %0,3"                                                \
+         : /*out*/   "=r" (_res)                                  \
+         : /*in*/    "r" (&_argvec[0])                            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS          \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_WWW(lval, orig, arg1,arg2,arg3)                 \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[4];                          \
+      volatile unsigned long _res;                                \
+      _argvec[0] = (unsigned long)_orig.nraddr;                   \
+      _argvec[1] = (unsigned long)arg1;                           \
+      _argvec[2] = (unsigned long)arg2;                           \
+      _argvec[3] = (unsigned long)arg3;                           \
+      __asm__ volatile(                                           \
+         "mr 11,%1\n\t"                                           \
+         "lwz 3,4(11)\n\t"   /* arg1->r3 */                       \
+         "lwz 4,8(11)\n\t"                                        \
+         "lwz 5,12(11)\n\t"                                       \
+         "lwz 11,0(11)\n\t"  /* target->r11 */                    \
+         VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R11                  \
+         "mr %0,3"                                                \
+         : /*out*/   "=r" (_res)                                  \
+         : /*in*/    "r" (&_argvec[0])                            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS          \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_WWWW(lval, orig, arg1,arg2,arg3,arg4)           \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[5];                          \
+      volatile unsigned long _res;                                \
+      _argvec[0] = (unsigned long)_orig.nraddr;                   \
+      _argvec[1] = (unsigned long)arg1;                           \
+      _argvec[2] = (unsigned long)arg2;                           \
+      _argvec[3] = (unsigned long)arg3;                           \
+      _argvec[4] = (unsigned long)arg4;                           \
+      __asm__ volatile(                                           \
+         "mr 11,%1\n\t"                                           \
+         "lwz 3,4(11)\n\t"   /* arg1->r3 */                       \
+         "lwz 4,8(11)\n\t"                                        \
+         "lwz 5,12(11)\n\t"                                       \
+         "lwz 6,16(11)\n\t"  /* arg4->r6 */                       \
+         "lwz 11,0(11)\n\t"  /* target->r11 */                    \
+         VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R11                  \
+         "mr %0,3"                                                \
+         : /*out*/   "=r" (_res)                                  \
+         : /*in*/    "r" (&_argvec[0])                            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS          \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_5W(lval, orig, arg1,arg2,arg3,arg4,arg5)        \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[6];                          \
+      volatile unsigned long _res;                                \
+      _argvec[0] = (unsigned long)_orig.nraddr;                   \
+      _argvec[1] = (unsigned long)arg1;                           \
+      _argvec[2] = (unsigned long)arg2;                           \
+      _argvec[3] = (unsigned long)arg3;                           \
+      _argvec[4] = (unsigned long)arg4;                           \
+      _argvec[5] = (unsigned long)arg5;                           \
+      __asm__ volatile(                                           \
+         "mr 11,%1\n\t"                                           \
+         "lwz 3,4(11)\n\t"   /* arg1->r3 */                       \
+         "lwz 4,8(11)\n\t"                                        \
+         "lwz 5,12(11)\n\t"                                       \
+         "lwz 6,16(11)\n\t"  /* arg4->r6 */                       \
+         "lwz 7,20(11)\n\t"                                       \
+         "lwz 11,0(11)\n\t"  /* target->r11 */                    \
+         VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R11                  \
+         "mr %0,3"                                                \
+         : /*out*/   "=r" (_res)                                  \
+         : /*in*/    "r" (&_argvec[0])                            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS          \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_6W(lval, orig, arg1,arg2,arg3,arg4,arg5,arg6)   \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[7];                          \
+      volatile unsigned long _res;                                \
+      _argvec[0] = (unsigned long)_orig.nraddr;                   \
+      _argvec[1] = (unsigned long)arg1;                           \
+      _argvec[2] = (unsigned long)arg2;                           \
+      _argvec[3] = (unsigned long)arg3;                           \
+      _argvec[4] = (unsigned long)arg4;                           \
+      _argvec[5] = (unsigned long)arg5;                           \
+      _argvec[6] = (unsigned long)arg6;                           \
+      __asm__ volatile(                                           \
+         "mr 11,%1\n\t"                                           \
+         "lwz 3,4(11)\n\t"   /* arg1->r3 */                       \
+         "lwz 4,8(11)\n\t"                                        \
+         "lwz 5,12(11)\n\t"                                       \
+         "lwz 6,16(11)\n\t"  /* arg4->r6 */                       \
+         "lwz 7,20(11)\n\t"                                       \
+         "lwz 8,24(11)\n\t"                                       \
+         "lwz 11,0(11)\n\t"  /* target->r11 */                    \
+         VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R11                  \
+         "mr %0,3"                                                \
+         : /*out*/   "=r" (_res)                                  \
+         : /*in*/    "r" (&_argvec[0])                            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS          \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_7W(lval, orig, arg1,arg2,arg3,arg4,arg5,arg6,   \
+                                 arg7)                            \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[8];                          \
+      volatile unsigned long _res;                                \
+      _argvec[0] = (unsigned long)_orig.nraddr;                   \
+      _argvec[1] = (unsigned long)arg1;                           \
+      _argvec[2] = (unsigned long)arg2;                           \
+      _argvec[3] = (unsigned long)arg3;                           \
+      _argvec[4] = (unsigned long)arg4;                           \
+      _argvec[5] = (unsigned long)arg5;                           \
+      _argvec[6] = (unsigned long)arg6;                           \
+      _argvec[7] = (unsigned long)arg7;                           \
+      __asm__ volatile(                                           \
+         "mr 11,%1\n\t"                                           \
+         "lwz 3,4(11)\n\t"   /* arg1->r3 */                       \
+         "lwz 4,8(11)\n\t"                                        \
+         "lwz 5,12(11)\n\t"                                       \
+         "lwz 6,16(11)\n\t"  /* arg4->r6 */                       \
+         "lwz 7,20(11)\n\t"                                       \
+         "lwz 8,24(11)\n\t"                                       \
+         "lwz 9,28(11)\n\t"                                       \
+         "lwz 11,0(11)\n\t"  /* target->r11 */                    \
+         VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R11                  \
+         "mr %0,3"                                                \
+         : /*out*/   "=r" (_res)                                  \
+         : /*in*/    "r" (&_argvec[0])                            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS          \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_8W(lval, orig, arg1,arg2,arg3,arg4,arg5,arg6,   \
+                                 arg7,arg8)                       \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[9];                          \
+      volatile unsigned long _res;                                \
+      _argvec[0] = (unsigned long)_orig.nraddr;                   \
+      _argvec[1] = (unsigned long)arg1;                           \
+      _argvec[2] = (unsigned long)arg2;                           \
+      _argvec[3] = (unsigned long)arg3;                           \
+      _argvec[4] = (unsigned long)arg4;                           \
+      _argvec[5] = (unsigned long)arg5;                           \
+      _argvec[6] = (unsigned long)arg6;                           \
+      _argvec[7] = (unsigned long)arg7;                           \
+      _argvec[8] = (unsigned long)arg8;                           \
+      __asm__ volatile(                                           \
+         "mr 11,%1\n\t"                                           \
+         "lwz 3,4(11)\n\t"   /* arg1->r3 */                       \
+         "lwz 4,8(11)\n\t"                                        \
+         "lwz 5,12(11)\n\t"                                       \
+         "lwz 6,16(11)\n\t"  /* arg4->r6 */                       \
+         "lwz 7,20(11)\n\t"                                       \
+         "lwz 8,24(11)\n\t"                                       \
+         "lwz 9,28(11)\n\t"                                       \
+         "lwz 10,32(11)\n\t" /* arg8->r10 */                      \
+         "lwz 11,0(11)\n\t"  /* target->r11 */                    \
+         VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R11                  \
+         "mr %0,3"                                                \
+         : /*out*/   "=r" (_res)                                  \
+         : /*in*/    "r" (&_argvec[0])                            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS          \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_9W(lval, orig, arg1,arg2,arg3,arg4,arg5,arg6,   \
+                                 arg7,arg8,arg9)                  \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[10];                         \
+      volatile unsigned long _res;                                \
+      _argvec[0] = (unsigned long)_orig.nraddr;                   \
+      _argvec[1] = (unsigned long)arg1;                           \
+      _argvec[2] = (unsigned long)arg2;                           \
+      _argvec[3] = (unsigned long)arg3;                           \
+      _argvec[4] = (unsigned long)arg4;                           \
+      _argvec[5] = (unsigned long)arg5;                           \
+      _argvec[6] = (unsigned long)arg6;                           \
+      _argvec[7] = (unsigned long)arg7;                           \
+      _argvec[8] = (unsigned long)arg8;                           \
+      _argvec[9] = (unsigned long)arg9;                           \
+      __asm__ volatile(                                           \
+         "mr 11,%1\n\t"                                           \
+         "addi 1,1,-16\n\t"                                       \
+         /* arg9 */                                               \
+         "lwz 3,36(11)\n\t"                                       \
+         "stw 3,8(1)\n\t"                                         \
+         /* args1-8 */                                            \
+         "lwz 3,4(11)\n\t"   /* arg1->r3 */                       \
+         "lwz 4,8(11)\n\t"                                        \
+         "lwz 5,12(11)\n\t"                                       \
+         "lwz 6,16(11)\n\t"  /* arg4->r6 */                       \
+         "lwz 7,20(11)\n\t"                                       \
+         "lwz 8,24(11)\n\t"                                       \
+         "lwz 9,28(11)\n\t"                                       \
+         "lwz 10,32(11)\n\t" /* arg8->r10 */                      \
+         "lwz 11,0(11)\n\t"  /* target->r11 */                    \
+         VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R11                  \
+         "addi 1,1,16\n\t"                                        \
+         "mr %0,3"                                                \
+         : /*out*/   "=r" (_res)                                  \
+         : /*in*/    "r" (&_argvec[0])                            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS          \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_10W(lval, orig, arg1,arg2,arg3,arg4,arg5,arg6,  \
+                                  arg7,arg8,arg9,arg10)           \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[11];                         \
+      volatile unsigned long _res;                                \
+      _argvec[0] = (unsigned long)_orig.nraddr;                   \
+      _argvec[1] = (unsigned long)arg1;                           \
+      _argvec[2] = (unsigned long)arg2;                           \
+      _argvec[3] = (unsigned long)arg3;                           \
+      _argvec[4] = (unsigned long)arg4;                           \
+      _argvec[5] = (unsigned long)arg5;                           \
+      _argvec[6] = (unsigned long)arg6;                           \
+      _argvec[7] = (unsigned long)arg7;                           \
+      _argvec[8] = (unsigned long)arg8;                           \
+      _argvec[9] = (unsigned long)arg9;                           \
+      _argvec[10] = (unsigned long)arg10;                         \
+      __asm__ volatile(                                           \
+         "mr 11,%1\n\t"                                           \
+         "addi 1,1,-16\n\t"                                       \
+         /* arg10 */                                              \
+         "lwz 3,40(11)\n\t"                                       \
+         "stw 3,12(1)\n\t"                                        \
+         /* arg9 */                                               \
+         "lwz 3,36(11)\n\t"                                       \
+         "stw 3,8(1)\n\t"                                         \
+         /* args1-8 */                                            \
+         "lwz 3,4(11)\n\t"   /* arg1->r3 */                       \
+         "lwz 4,8(11)\n\t"                                        \
+         "lwz 5,12(11)\n\t"                                       \
+         "lwz 6,16(11)\n\t"  /* arg4->r6 */                       \
+         "lwz 7,20(11)\n\t"                                       \
+         "lwz 8,24(11)\n\t"                                       \
+         "lwz 9,28(11)\n\t"                                       \
+         "lwz 10,32(11)\n\t" /* arg8->r10 */                      \
+         "lwz 11,0(11)\n\t"  /* target->r11 */                    \
+         VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R11                  \
+         "addi 1,1,16\n\t"                                        \
+         "mr %0,3"                                                \
+         : /*out*/   "=r" (_res)                                  \
+         : /*in*/    "r" (&_argvec[0])                            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS          \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_11W(lval, orig, arg1,arg2,arg3,arg4,arg5,arg6,  \
+                                  arg7,arg8,arg9,arg10,arg11)     \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[12];                         \
+      volatile unsigned long _res;                                \
+      _argvec[0] = (unsigned long)_orig.nraddr;                   \
+      _argvec[1] = (unsigned long)arg1;                           \
+      _argvec[2] = (unsigned long)arg2;                           \
+      _argvec[3] = (unsigned long)arg3;                           \
+      _argvec[4] = (unsigned long)arg4;                           \
+      _argvec[5] = (unsigned long)arg5;                           \
+      _argvec[6] = (unsigned long)arg6;                           \
+      _argvec[7] = (unsigned long)arg7;                           \
+      _argvec[8] = (unsigned long)arg8;                           \
+      _argvec[9] = (unsigned long)arg9;                           \
+      _argvec[10] = (unsigned long)arg10;                         \
+      _argvec[11] = (unsigned long)arg11;                         \
+      __asm__ volatile(                                           \
+         "mr 11,%1\n\t"                                           \
+         "addi 1,1,-32\n\t"                                       \
+         /* arg11 */                                              \
+         "lwz 3,44(11)\n\t"                                       \
+         "stw 3,16(1)\n\t"                                        \
+         /* arg10 */                                              \
+         "lwz 3,40(11)\n\t"                                       \
+         "stw 3,12(1)\n\t"                                        \
+         /* arg9 */                                               \
+         "lwz 3,36(11)\n\t"                                       \
+         "stw 3,8(1)\n\t"                                         \
+         /* args1-8 */                                            \
+         "lwz 3,4(11)\n\t"   /* arg1->r3 */                       \
+         "lwz 4,8(11)\n\t"                                        \
+         "lwz 5,12(11)\n\t"                                       \
+         "lwz 6,16(11)\n\t"  /* arg4->r6 */                       \
+         "lwz 7,20(11)\n\t"                                       \
+         "lwz 8,24(11)\n\t"                                       \
+         "lwz 9,28(11)\n\t"                                       \
+         "lwz 10,32(11)\n\t" /* arg8->r10 */                      \
+         "lwz 11,0(11)\n\t"  /* target->r11 */                    \
+         VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R11                  \
+         "addi 1,1,32\n\t"                                        \
+         "mr %0,3"                                                \
+         : /*out*/   "=r" (_res)                                  \
+         : /*in*/    "r" (&_argvec[0])                            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS          \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_12W(lval, orig, arg1,arg2,arg3,arg4,arg5,arg6,  \
+                                arg7,arg8,arg9,arg10,arg11,arg12) \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[13];                         \
+      volatile unsigned long _res;                                \
+      _argvec[0] = (unsigned long)_orig.nraddr;                   \
+      _argvec[1] = (unsigned long)arg1;                           \
+      _argvec[2] = (unsigned long)arg2;                           \
+      _argvec[3] = (unsigned long)arg3;                           \
+      _argvec[4] = (unsigned long)arg4;                           \
+      _argvec[5] = (unsigned long)arg5;                           \
+      _argvec[6] = (unsigned long)arg6;                           \
+      _argvec[7] = (unsigned long)arg7;                           \
+      _argvec[8] = (unsigned long)arg8;                           \
+      _argvec[9] = (unsigned long)arg9;                           \
+      _argvec[10] = (unsigned long)arg10;                         \
+      _argvec[11] = (unsigned long)arg11;                         \
+      _argvec[12] = (unsigned long)arg12;                         \
+      __asm__ volatile(                                           \
+         "mr 11,%1\n\t"                                           \
+         "addi 1,1,-32\n\t"                                       \
+         /* arg12 */                                              \
+         "lwz 3,48(11)\n\t"                                       \
+         "stw 3,20(1)\n\t"                                        \
+         /* arg11 */                                              \
+         "lwz 3,44(11)\n\t"                                       \
+         "stw 3,16(1)\n\t"                                        \
+         /* arg10 */                                              \
+         "lwz 3,40(11)\n\t"                                       \
+         "stw 3,12(1)\n\t"                                        \
+         /* arg9 */                                               \
+         "lwz 3,36(11)\n\t"                                       \
+         "stw 3,8(1)\n\t"                                         \
+         /* args1-8 */                                            \
+         "lwz 3,4(11)\n\t"   /* arg1->r3 */                       \
+         "lwz 4,8(11)\n\t"                                        \
+         "lwz 5,12(11)\n\t"                                       \
+         "lwz 6,16(11)\n\t"  /* arg4->r6 */                       \
+         "lwz 7,20(11)\n\t"                                       \
+         "lwz 8,24(11)\n\t"                                       \
+         "lwz 9,28(11)\n\t"                                       \
+         "lwz 10,32(11)\n\t" /* arg8->r10 */                      \
+         "lwz 11,0(11)\n\t"  /* target->r11 */                    \
+         VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R11                  \
+         "addi 1,1,32\n\t"                                        \
+         "mr %0,3"                                                \
+         : /*out*/   "=r" (_res)                                  \
+         : /*in*/    "r" (&_argvec[0])                            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS          \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#endif /* PLAT_ppc32_linux */
+
+/* ------------------------ ppc64-linux ------------------------ */
+
+#if defined(PLAT_ppc64_linux)
+
+/* ARGREGS: r3 r4 r5 r6 r7 r8 r9 r10 (the rest on stack somewhere) */
+
+/* These regs are trashed by the hidden call. */
+#define __CALLER_SAVED_REGS                                       \
+   "lr", "ctr", "xer",                                            \
+   "cr0", "cr1", "cr2", "cr3", "cr4", "cr5", "cr6", "cr7",        \
+   "r0", "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10",   \
+   "r11", "r12", "r13"
+
+/* These CALL_FN_ macros assume that on ppc64-linux, sizeof(unsigned
+   long) == 8. */
+
+#define CALL_FN_W_v(lval, orig)                                   \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[3+0];                        \
+      volatile unsigned long _res;                                \
+      /* _argvec[0] holds current r2 across the call */           \
+      _argvec[1] = (unsigned long)_orig.r2;                       \
+      _argvec[2] = (unsigned long)_orig.nraddr;                   \
+      __asm__ volatile(                                           \
+         "mr 11,%1\n\t"                                           \
+         "std 2,-16(11)\n\t"  /* save tocptr */                   \
+         "ld   2,-8(11)\n\t"  /* use nraddr's tocptr */           \
+         "ld  11, 0(11)\n\t"  /* target->r11 */                   \
+         VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R11                  \
+         "mr 11,%1\n\t"                                           \
+         "mr %0,3\n\t"                                            \
+         "ld 2,-16(11)" /* restore tocptr */                      \
+         : /*out*/   "=r" (_res)                                  \
+         : /*in*/    "r" (&_argvec[2])                            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS          \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_W(lval, orig, arg1)                             \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[3+1];                        \
+      volatile unsigned long _res;                                \
+      /* _argvec[0] holds current r2 across the call */           \
+      _argvec[1]   = (unsigned long)_orig.r2;                     \
+      _argvec[2]   = (unsigned long)_orig.nraddr;                 \
+      _argvec[2+1] = (unsigned long)arg1;                         \
+      __asm__ volatile(                                           \
+         "mr 11,%1\n\t"                                           \
+         "std 2,-16(11)\n\t"  /* save tocptr */                   \
+         "ld   2,-8(11)\n\t"  /* use nraddr's tocptr */           \
+         "ld   3, 8(11)\n\t"  /* arg1->r3 */                      \
+         "ld  11, 0(11)\n\t"  /* target->r11 */                   \
+         VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R11                  \
+         "mr 11,%1\n\t"                                           \
+         "mr %0,3\n\t"                                            \
+         "ld 2,-16(11)" /* restore tocptr */                      \
+         : /*out*/   "=r" (_res)                                  \
+         : /*in*/    "r" (&_argvec[2])                            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS          \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_WW(lval, orig, arg1,arg2)                       \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[3+2];                        \
+      volatile unsigned long _res;                                \
+      /* _argvec[0] holds current r2 across the call */           \
+      _argvec[1]   = (unsigned long)_orig.r2;                     \
+      _argvec[2]   = (unsigned long)_orig.nraddr;                 \
+      _argvec[2+1] = (unsigned long)arg1;                         \
+      _argvec[2+2] = (unsigned long)arg2;                         \
+      __asm__ volatile(                                           \
+         "mr 11,%1\n\t"                                           \
+         "std 2,-16(11)\n\t"  /* save tocptr */                   \
+         "ld   2,-8(11)\n\t"  /* use nraddr's tocptr */           \
+         "ld   3, 8(11)\n\t"  /* arg1->r3 */                      \
+         "ld   4, 16(11)\n\t" /* arg2->r4 */                      \
+         "ld  11, 0(11)\n\t"  /* target->r11 */                   \
+         VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R11                  \
+         "mr 11,%1\n\t"                                           \
+         "mr %0,3\n\t"                                            \
+         "ld 2,-16(11)" /* restore tocptr */                      \
+         : /*out*/   "=r" (_res)                                  \
+         : /*in*/    "r" (&_argvec[2])                            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS          \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_WWW(lval, orig, arg1,arg2,arg3)                 \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[3+3];                        \
+      volatile unsigned long _res;                                \
+      /* _argvec[0] holds current r2 across the call */           \
+      _argvec[1]   = (unsigned long)_orig.r2;                     \
+      _argvec[2]   = (unsigned long)_orig.nraddr;                 \
+      _argvec[2+1] = (unsigned long)arg1;                         \
+      _argvec[2+2] = (unsigned long)arg2;                         \
+      _argvec[2+3] = (unsigned long)arg3;                         \
+      __asm__ volatile(                                           \
+         "mr 11,%1\n\t"                                           \
+         "std 2,-16(11)\n\t"  /* save tocptr */                   \
+         "ld   2,-8(11)\n\t"  /* use nraddr's tocptr */           \
+         "ld   3, 8(11)\n\t"  /* arg1->r3 */                      \
+         "ld   4, 16(11)\n\t" /* arg2->r4 */                      \
+         "ld   5, 24(11)\n\t" /* arg3->r5 */                      \
+         "ld  11, 0(11)\n\t"  /* target->r11 */                   \
+         VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R11                  \
+         "mr 11,%1\n\t"                                           \
+         "mr %0,3\n\t"                                            \
+         "ld 2,-16(11)" /* restore tocptr */                      \
+         : /*out*/   "=r" (_res)                                  \
+         : /*in*/    "r" (&_argvec[2])                            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS          \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_WWWW(lval, orig, arg1,arg2,arg3,arg4)           \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[3+4];                        \
+      volatile unsigned long _res;                                \
+      /* _argvec[0] holds current r2 across the call */           \
+      _argvec[1]   = (unsigned long)_orig.r2;                     \
+      _argvec[2]   = (unsigned long)_orig.nraddr;                 \
+      _argvec[2+1] = (unsigned long)arg1;                         \
+      _argvec[2+2] = (unsigned long)arg2;                         \
+      _argvec[2+3] = (unsigned long)arg3;                         \
+      _argvec[2+4] = (unsigned long)arg4;                         \
+      __asm__ volatile(                                           \
+         "mr 11,%1\n\t"                                           \
+         "std 2,-16(11)\n\t"  /* save tocptr */                   \
+         "ld   2,-8(11)\n\t"  /* use nraddr's tocptr */           \
+         "ld   3, 8(11)\n\t"  /* arg1->r3 */                      \
+         "ld   4, 16(11)\n\t" /* arg2->r4 */                      \
+         "ld   5, 24(11)\n\t" /* arg3->r5 */                      \
+         "ld   6, 32(11)\n\t" /* arg4->r6 */                      \
+         "ld  11, 0(11)\n\t"  /* target->r11 */                   \
+         VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R11                  \
+         "mr 11,%1\n\t"                                           \
+         "mr %0,3\n\t"                                            \
+         "ld 2,-16(11)" /* restore tocptr */                      \
+         : /*out*/   "=r" (_res)                                  \
+         : /*in*/    "r" (&_argvec[2])                            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS          \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_5W(lval, orig, arg1,arg2,arg3,arg4,arg5)        \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[3+5];                        \
+      volatile unsigned long _res;                                \
+      /* _argvec[0] holds current r2 across the call */           \
+      _argvec[1]   = (unsigned long)_orig.r2;                     \
+      _argvec[2]   = (unsigned long)_orig.nraddr;                 \
+      _argvec[2+1] = (unsigned long)arg1;                         \
+      _argvec[2+2] = (unsigned long)arg2;                         \
+      _argvec[2+3] = (unsigned long)arg3;                         \
+      _argvec[2+4] = (unsigned long)arg4;                         \
+      _argvec[2+5] = (unsigned long)arg5;                         \
+      __asm__ volatile(                                           \
+         "mr 11,%1\n\t"                                           \
+         "std 2,-16(11)\n\t"  /* save tocptr */                   \
+         "ld   2,-8(11)\n\t"  /* use nraddr's tocptr */           \
+         "ld   3, 8(11)\n\t"  /* arg1->r3 */                      \
+         "ld   4, 16(11)\n\t" /* arg2->r4 */                      \
+         "ld   5, 24(11)\n\t" /* arg3->r5 */                      \
+         "ld   6, 32(11)\n\t" /* arg4->r6 */                      \
+         "ld   7, 40(11)\n\t" /* arg5->r7 */                      \
+         "ld  11, 0(11)\n\t"  /* target->r11 */                   \
+         VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R11                  \
+         "mr 11,%1\n\t"                                           \
+         "mr %0,3\n\t"                                            \
+         "ld 2,-16(11)" /* restore tocptr */                      \
+         : /*out*/   "=r" (_res)                                  \
+         : /*in*/    "r" (&_argvec[2])                            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS          \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_6W(lval, orig, arg1,arg2,arg3,arg4,arg5,arg6)   \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[3+6];                        \
+      volatile unsigned long _res;                                \
+      /* _argvec[0] holds current r2 across the call */           \
+      _argvec[1]   = (unsigned long)_orig.r2;                     \
+      _argvec[2]   = (unsigned long)_orig.nraddr;                 \
+      _argvec[2+1] = (unsigned long)arg1;                         \
+      _argvec[2+2] = (unsigned long)arg2;                         \
+      _argvec[2+3] = (unsigned long)arg3;                         \
+      _argvec[2+4] = (unsigned long)arg4;                         \
+      _argvec[2+5] = (unsigned long)arg5;                         \
+      _argvec[2+6] = (unsigned long)arg6;                         \
+      __asm__ volatile(                                           \
+         "mr 11,%1\n\t"                                           \
+         "std 2,-16(11)\n\t"  /* save tocptr */                   \
+         "ld   2,-8(11)\n\t"  /* use nraddr's tocptr */           \
+         "ld   3, 8(11)\n\t"  /* arg1->r3 */                      \
+         "ld   4, 16(11)\n\t" /* arg2->r4 */                      \
+         "ld   5, 24(11)\n\t" /* arg3->r5 */                      \
+         "ld   6, 32(11)\n\t" /* arg4->r6 */                      \
+         "ld   7, 40(11)\n\t" /* arg5->r7 */                      \
+         "ld   8, 48(11)\n\t" /* arg6->r8 */                      \
+         "ld  11, 0(11)\n\t"  /* target->r11 */                   \
+         VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R11                  \
+         "mr 11,%1\n\t"                                           \
+         "mr %0,3\n\t"                                            \
+         "ld 2,-16(11)" /* restore tocptr */                      \
+         : /*out*/   "=r" (_res)                                  \
+         : /*in*/    "r" (&_argvec[2])                            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS          \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_7W(lval, orig, arg1,arg2,arg3,arg4,arg5,arg6,   \
+                                 arg7)                            \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[3+7];                        \
+      volatile unsigned long _res;                                \
+      /* _argvec[0] holds current r2 across the call */           \
+      _argvec[1]   = (unsigned long)_orig.r2;                     \
+      _argvec[2]   = (unsigned long)_orig.nraddr;                 \
+      _argvec[2+1] = (unsigned long)arg1;                         \
+      _argvec[2+2] = (unsigned long)arg2;                         \
+      _argvec[2+3] = (unsigned long)arg3;                         \
+      _argvec[2+4] = (unsigned long)arg4;                         \
+      _argvec[2+5] = (unsigned long)arg5;                         \
+      _argvec[2+6] = (unsigned long)arg6;                         \
+      _argvec[2+7] = (unsigned long)arg7;                         \
+      __asm__ volatile(                                           \
+         "mr 11,%1\n\t"                                           \
+         "std 2,-16(11)\n\t"  /* save tocptr */                   \
+         "ld   2,-8(11)\n\t"  /* use nraddr's tocptr */           \
+         "ld   3, 8(11)\n\t"  /* arg1->r3 */                      \
+         "ld   4, 16(11)\n\t" /* arg2->r4 */                      \
+         "ld   5, 24(11)\n\t" /* arg3->r5 */                      \
+         "ld   6, 32(11)\n\t" /* arg4->r6 */                      \
+         "ld   7, 40(11)\n\t" /* arg5->r7 */                      \
+         "ld   8, 48(11)\n\t" /* arg6->r8 */                      \
+         "ld   9, 56(11)\n\t" /* arg7->r9 */                      \
+         "ld  11, 0(11)\n\t"  /* target->r11 */                   \
+         VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R11                  \
+         "mr 11,%1\n\t"                                           \
+         "mr %0,3\n\t"                                            \
+         "ld 2,-16(11)" /* restore tocptr */                      \
+         : /*out*/   "=r" (_res)                                  \
+         : /*in*/    "r" (&_argvec[2])                            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS          \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_8W(lval, orig, arg1,arg2,arg3,arg4,arg5,arg6,   \
+                                 arg7,arg8)                       \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[3+8];                        \
+      volatile unsigned long _res;                                \
+      /* _argvec[0] holds current r2 across the call */           \
+      _argvec[1]   = (unsigned long)_orig.r2;                     \
+      _argvec[2]   = (unsigned long)_orig.nraddr;                 \
+      _argvec[2+1] = (unsigned long)arg1;                         \
+      _argvec[2+2] = (unsigned long)arg2;                         \
+      _argvec[2+3] = (unsigned long)arg3;                         \
+      _argvec[2+4] = (unsigned long)arg4;                         \
+      _argvec[2+5] = (unsigned long)arg5;                         \
+      _argvec[2+6] = (unsigned long)arg6;                         \
+      _argvec[2+7] = (unsigned long)arg7;                         \
+      _argvec[2+8] = (unsigned long)arg8;                         \
+      __asm__ volatile(                                           \
+         "mr 11,%1\n\t"                                           \
+         "std 2,-16(11)\n\t"  /* save tocptr */                   \
+         "ld   2,-8(11)\n\t"  /* use nraddr's tocptr */           \
+         "ld   3, 8(11)\n\t"  /* arg1->r3 */                      \
+         "ld   4, 16(11)\n\t" /* arg2->r4 */                      \
+         "ld   5, 24(11)\n\t" /* arg3->r5 */                      \
+         "ld   6, 32(11)\n\t" /* arg4->r6 */                      \
+         "ld   7, 40(11)\n\t" /* arg5->r7 */                      \
+         "ld   8, 48(11)\n\t" /* arg6->r8 */                      \
+         "ld   9, 56(11)\n\t" /* arg7->r9 */                      \
+         "ld  10, 64(11)\n\t" /* arg8->r10 */                     \
+         "ld  11, 0(11)\n\t"  /* target->r11 */                   \
+         VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R11                  \
+         "mr 11,%1\n\t"                                           \
+         "mr %0,3\n\t"                                            \
+         "ld 2,-16(11)" /* restore tocptr */                      \
+         : /*out*/   "=r" (_res)                                  \
+         : /*in*/    "r" (&_argvec[2])                            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS          \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_9W(lval, orig, arg1,arg2,arg3,arg4,arg5,arg6,   \
+                                 arg7,arg8,arg9)                  \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[3+9];                        \
+      volatile unsigned long _res;                                \
+      /* _argvec[0] holds current r2 across the call */           \
+      _argvec[1]   = (unsigned long)_orig.r2;                     \
+      _argvec[2]   = (unsigned long)_orig.nraddr;                 \
+      _argvec[2+1] = (unsigned long)arg1;                         \
+      _argvec[2+2] = (unsigned long)arg2;                         \
+      _argvec[2+3] = (unsigned long)arg3;                         \
+      _argvec[2+4] = (unsigned long)arg4;                         \
+      _argvec[2+5] = (unsigned long)arg5;                         \
+      _argvec[2+6] = (unsigned long)arg6;                         \
+      _argvec[2+7] = (unsigned long)arg7;                         \
+      _argvec[2+8] = (unsigned long)arg8;                         \
+      _argvec[2+9] = (unsigned long)arg9;                         \
+      __asm__ volatile(                                           \
+         "mr 11,%1\n\t"                                           \
+         "std 2,-16(11)\n\t"  /* save tocptr */                   \
+         "ld   2,-8(11)\n\t"  /* use nraddr's tocptr */           \
+         "addi 1,1,-128\n\t"  /* expand stack frame */            \
+         /* arg9 */                                               \
+         "ld  3,72(11)\n\t"                                       \
+         "std 3,112(1)\n\t"                                       \
+         /* args1-8 */                                            \
+         "ld   3, 8(11)\n\t"  /* arg1->r3 */                      \
+         "ld   4, 16(11)\n\t" /* arg2->r4 */                      \
+         "ld   5, 24(11)\n\t" /* arg3->r5 */                      \
+         "ld   6, 32(11)\n\t" /* arg4->r6 */                      \
+         "ld   7, 40(11)\n\t" /* arg5->r7 */                      \
+         "ld   8, 48(11)\n\t" /* arg6->r8 */                      \
+         "ld   9, 56(11)\n\t" /* arg7->r9 */                      \
+         "ld  10, 64(11)\n\t" /* arg8->r10 */                     \
+         "ld  11, 0(11)\n\t"  /* target->r11 */                   \
+         VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R11                  \
+         "mr 11,%1\n\t"                                           \
+         "mr %0,3\n\t"                                            \
+         "ld 2,-16(11)\n\t" /* restore tocptr */                  \
+         "addi 1,1,128"     /* restore frame */                   \
+         : /*out*/   "=r" (_res)                                  \
+         : /*in*/    "r" (&_argvec[2])                            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS          \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_10W(lval, orig, arg1,arg2,arg3,arg4,arg5,arg6,  \
+                                  arg7,arg8,arg9,arg10)           \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[3+10];                       \
+      volatile unsigned long _res;                                \
+      /* _argvec[0] holds current r2 across the call */           \
+      _argvec[1]   = (unsigned long)_orig.r2;                     \
+      _argvec[2]   = (unsigned long)_orig.nraddr;                 \
+      _argvec[2+1] = (unsigned long)arg1;                         \
+      _argvec[2+2] = (unsigned long)arg2;                         \
+      _argvec[2+3] = (unsigned long)arg3;                         \
+      _argvec[2+4] = (unsigned long)arg4;                         \
+      _argvec[2+5] = (unsigned long)arg5;                         \
+      _argvec[2+6] = (unsigned long)arg6;                         \
+      _argvec[2+7] = (unsigned long)arg7;                         \
+      _argvec[2+8] = (unsigned long)arg8;                         \
+      _argvec[2+9] = (unsigned long)arg9;                         \
+      _argvec[2+10] = (unsigned long)arg10;                       \
+      __asm__ volatile(                                           \
+         "mr 11,%1\n\t"                                           \
+         "std 2,-16(11)\n\t"  /* save tocptr */                   \
+         "ld   2,-8(11)\n\t"  /* use nraddr's tocptr */           \
+         "addi 1,1,-128\n\t"  /* expand stack frame */            \
+         /* arg10 */                                              \
+         "ld  3,80(11)\n\t"                                       \
+         "std 3,120(1)\n\t"                                       \
+         /* arg9 */                                               \
+         "ld  3,72(11)\n\t"                                       \
+         "std 3,112(1)\n\t"                                       \
+         /* args1-8 */                                            \
+         "ld   3, 8(11)\n\t"  /* arg1->r3 */                      \
+         "ld   4, 16(11)\n\t" /* arg2->r4 */                      \
+         "ld   5, 24(11)\n\t" /* arg3->r5 */                      \
+         "ld   6, 32(11)\n\t" /* arg4->r6 */                      \
+         "ld   7, 40(11)\n\t" /* arg5->r7 */                      \
+         "ld   8, 48(11)\n\t" /* arg6->r8 */                      \
+         "ld   9, 56(11)\n\t" /* arg7->r9 */                      \
+         "ld  10, 64(11)\n\t" /* arg8->r10 */                     \
+         "ld  11, 0(11)\n\t"  /* target->r11 */                   \
+         VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R11                  \
+         "mr 11,%1\n\t"                                           \
+         "mr %0,3\n\t"                                            \
+         "ld 2,-16(11)\n\t" /* restore tocptr */                  \
+         "addi 1,1,128"     /* restore frame */                   \
+         : /*out*/   "=r" (_res)                                  \
+         : /*in*/    "r" (&_argvec[2])                            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS          \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_11W(lval, orig, arg1,arg2,arg3,arg4,arg5,arg6,  \
+                                  arg7,arg8,arg9,arg10,arg11)     \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[3+11];                       \
+      volatile unsigned long _res;                                \
+      /* _argvec[0] holds current r2 across the call */           \
+      _argvec[1]   = (unsigned long)_orig.r2;                     \
+      _argvec[2]   = (unsigned long)_orig.nraddr;                 \
+      _argvec[2+1] = (unsigned long)arg1;                         \
+      _argvec[2+2] = (unsigned long)arg2;                         \
+      _argvec[2+3] = (unsigned long)arg3;                         \
+      _argvec[2+4] = (unsigned long)arg4;                         \
+      _argvec[2+5] = (unsigned long)arg5;                         \
+      _argvec[2+6] = (unsigned long)arg6;                         \
+      _argvec[2+7] = (unsigned long)arg7;                         \
+      _argvec[2+8] = (unsigned long)arg8;                         \
+      _argvec[2+9] = (unsigned long)arg9;                         \
+      _argvec[2+10] = (unsigned long)arg10;                       \
+      _argvec[2+11] = (unsigned long)arg11;                       \
+      __asm__ volatile(                                           \
+         "mr 11,%1\n\t"                                           \
+         "std 2,-16(11)\n\t"  /* save tocptr */                   \
+         "ld   2,-8(11)\n\t"  /* use nraddr's tocptr */           \
+         "addi 1,1,-144\n\t"  /* expand stack frame */            \
+         /* arg11 */                                              \
+         "ld  3,88(11)\n\t"                                       \
+         "std 3,128(1)\n\t"                                       \
+         /* arg10 */                                              \
+         "ld  3,80(11)\n\t"                                       \
+         "std 3,120(1)\n\t"                                       \
+         /* arg9 */                                               \
+         "ld  3,72(11)\n\t"                                       \
+         "std 3,112(1)\n\t"                                       \
+         /* args1-8 */                                            \
+         "ld   3, 8(11)\n\t"  /* arg1->r3 */                      \
+         "ld   4, 16(11)\n\t" /* arg2->r4 */                      \
+         "ld   5, 24(11)\n\t" /* arg3->r5 */                      \
+         "ld   6, 32(11)\n\t" /* arg4->r6 */                      \
+         "ld   7, 40(11)\n\t" /* arg5->r7 */                      \
+         "ld   8, 48(11)\n\t" /* arg6->r8 */                      \
+         "ld   9, 56(11)\n\t" /* arg7->r9 */                      \
+         "ld  10, 64(11)\n\t" /* arg8->r10 */                     \
+         "ld  11, 0(11)\n\t"  /* target->r11 */                   \
+         VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R11                  \
+         "mr 11,%1\n\t"                                           \
+         "mr %0,3\n\t"                                            \
+         "ld 2,-16(11)\n\t" /* restore tocptr */                  \
+         "addi 1,1,144"     /* restore frame */                   \
+         : /*out*/   "=r" (_res)                                  \
+         : /*in*/    "r" (&_argvec[2])                            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS          \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_12W(lval, orig, arg1,arg2,arg3,arg4,arg5,arg6,  \
+                                arg7,arg8,arg9,arg10,arg11,arg12) \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[3+12];                       \
+      volatile unsigned long _res;                                \
+      /* _argvec[0] holds current r2 across the call */           \
+      _argvec[1]   = (unsigned long)_orig.r2;                     \
+      _argvec[2]   = (unsigned long)_orig.nraddr;                 \
+      _argvec[2+1] = (unsigned long)arg1;                         \
+      _argvec[2+2] = (unsigned long)arg2;                         \
+      _argvec[2+3] = (unsigned long)arg3;                         \
+      _argvec[2+4] = (unsigned long)arg4;                         \
+      _argvec[2+5] = (unsigned long)arg5;                         \
+      _argvec[2+6] = (unsigned long)arg6;                         \
+      _argvec[2+7] = (unsigned long)arg7;                         \
+      _argvec[2+8] = (unsigned long)arg8;                         \
+      _argvec[2+9] = (unsigned long)arg9;                         \
+      _argvec[2+10] = (unsigned long)arg10;                       \
+      _argvec[2+11] = (unsigned long)arg11;                       \
+      _argvec[2+12] = (unsigned long)arg12;                       \
+      __asm__ volatile(                                           \
+         "mr 11,%1\n\t"                                           \
+         "std 2,-16(11)\n\t"  /* save tocptr */                   \
+         "ld   2,-8(11)\n\t"  /* use nraddr's tocptr */           \
+         "addi 1,1,-144\n\t"  /* expand stack frame */            \
+         /* arg12 */                                              \
+         "ld  3,96(11)\n\t"                                       \
+         "std 3,136(1)\n\t"                                       \
+         /* arg11 */                                              \
+         "ld  3,88(11)\n\t"                                       \
+         "std 3,128(1)\n\t"                                       \
+         /* arg10 */                                              \
+         "ld  3,80(11)\n\t"                                       \
+         "std 3,120(1)\n\t"                                       \
+         /* arg9 */                                               \
+         "ld  3,72(11)\n\t"                                       \
+         "std 3,112(1)\n\t"                                       \
+         /* args1-8 */                                            \
+         "ld   3, 8(11)\n\t"  /* arg1->r3 */                      \
+         "ld   4, 16(11)\n\t" /* arg2->r4 */                      \
+         "ld   5, 24(11)\n\t" /* arg3->r5 */                      \
+         "ld   6, 32(11)\n\t" /* arg4->r6 */                      \
+         "ld   7, 40(11)\n\t" /* arg5->r7 */                      \
+         "ld   8, 48(11)\n\t" /* arg6->r8 */                      \
+         "ld   9, 56(11)\n\t" /* arg7->r9 */                      \
+         "ld  10, 64(11)\n\t" /* arg8->r10 */                     \
+         "ld  11, 0(11)\n\t"  /* target->r11 */                   \
+         VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R11                  \
+         "mr 11,%1\n\t"                                           \
+         "mr %0,3\n\t"                                            \
+         "ld 2,-16(11)\n\t" /* restore tocptr */                  \
+         "addi 1,1,144"     /* restore frame */                   \
+         : /*out*/   "=r" (_res)                                  \
+         : /*in*/    "r" (&_argvec[2])                            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS          \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#endif /* PLAT_ppc64_linux */
+
+/* ------------------------- arm-linux ------------------------- */
+
+#if defined(PLAT_arm_linux)
+
+/* These regs are trashed by the hidden call. */
+#define __CALLER_SAVED_REGS "r0", "r1", "r2", "r3","r4","r14"
+
+/* These CALL_FN_ macros assume that on arm-linux, sizeof(unsigned
+   long) == 4. */
+
+#define CALL_FN_W_v(lval, orig)                                   \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[1];                          \
+      volatile unsigned long _res;                                \
+      _argvec[0] = (unsigned long)_orig.nraddr;                   \
+      __asm__ volatile(                                           \
+         "ldr r4, [%1] \n\t"  /* target->r4 */                    \
+         VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R4                   \
+         "mov %0, r0\n"                                           \
+         : /*out*/   "=r" (_res)                                  \
+         : /*in*/    "0" (&_argvec[0])                            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS          \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_W(lval, orig, arg1)                             \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[2];                          \
+      volatile unsigned long _res;                                \
+      _argvec[0] = (unsigned long)_orig.nraddr;                   \
+      _argvec[1] = (unsigned long)(arg1);                         \
+      __asm__ volatile(                                           \
+         "ldr r0, [%1, #4] \n\t"                                  \
+         "ldr r4, [%1] \n\t"  /* target->r4 */                    \
+         VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R4                   \
+         "mov %0, r0\n"                                           \
+         : /*out*/   "=r" (_res)                                  \
+         : /*in*/    "0" (&_argvec[0])                            \
+         : /*trash*/ "cc", "memory",  __CALLER_SAVED_REGS         \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_WW(lval, orig, arg1,arg2)                       \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[3];                          \
+      volatile unsigned long _res;                                \
+      _argvec[0] = (unsigned long)_orig.nraddr;                   \
+      _argvec[1] = (unsigned long)(arg1);                         \
+      _argvec[2] = (unsigned long)(arg2);                         \
+      __asm__ volatile(                                           \
+         "ldr r0, [%1, #4] \n\t"                                  \
+         "ldr r1, [%1, #8] \n\t"                                  \
+         "ldr r4, [%1] \n\t"  /* target->r4 */                    \
+         VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R4                   \
+         "mov %0, r0\n"                                           \
+         : /*out*/   "=r" (_res)                                  \
+         : /*in*/    "0" (&_argvec[0])                            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS          \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_WWW(lval, orig, arg1,arg2,arg3)                 \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[4];                          \
+      volatile unsigned long _res;                                \
+      _argvec[0] = (unsigned long)_orig.nraddr;                   \
+      _argvec[1] = (unsigned long)(arg1);                         \
+      _argvec[2] = (unsigned long)(arg2);                         \
+      _argvec[3] = (unsigned long)(arg3);                         \
+      __asm__ volatile(                                           \
+         "ldr r0, [%1, #4] \n\t"                                  \
+         "ldr r1, [%1, #8] \n\t"                                  \
+         "ldr r2, [%1, #12] \n\t"                                 \
+         "ldr r4, [%1] \n\t"  /* target->r4 */                    \
+         VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R4                   \
+         "mov %0, r0\n"                                           \
+         : /*out*/   "=r" (_res)                                  \
+         : /*in*/    "0" (&_argvec[0])                            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS          \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_WWWW(lval, orig, arg1,arg2,arg3,arg4)           \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[5];                          \
+      volatile unsigned long _res;                                \
+      _argvec[0] = (unsigned long)_orig.nraddr;                   \
+      _argvec[1] = (unsigned long)(arg1);                         \
+      _argvec[2] = (unsigned long)(arg2);                         \
+      _argvec[3] = (unsigned long)(arg3);                         \
+      _argvec[4] = (unsigned long)(arg4);                         \
+      __asm__ volatile(                                           \
+         "ldr r0, [%1, #4] \n\t"                                  \
+         "ldr r1, [%1, #8] \n\t"                                  \
+         "ldr r2, [%1, #12] \n\t"                                 \
+         "ldr r3, [%1, #16] \n\t"                                 \
+         "ldr r4, [%1] \n\t"  /* target->r4 */                    \
+         VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R4                   \
+         "mov %0, r0"                                             \
+         : /*out*/   "=r" (_res)                                  \
+         : /*in*/    "0" (&_argvec[0])                            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS          \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_5W(lval, orig, arg1,arg2,arg3,arg4,arg5)        \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[6];                          \
+      volatile unsigned long _res;                                \
+      _argvec[0] = (unsigned long)_orig.nraddr;                   \
+      _argvec[1] = (unsigned long)(arg1);                         \
+      _argvec[2] = (unsigned long)(arg2);                         \
+      _argvec[3] = (unsigned long)(arg3);                         \
+      _argvec[4] = (unsigned long)(arg4);                         \
+      _argvec[5] = (unsigned long)(arg5);                         \
+      __asm__ volatile(                                           \
+         "ldr r0, [%1, #20] \n\t"                                 \
+         "push {r0} \n\t"                                         \
+         "ldr r0, [%1, #4] \n\t"                                  \
+         "ldr r1, [%1, #8] \n\t"                                  \
+         "ldr r2, [%1, #12] \n\t"                                 \
+         "ldr r3, [%1, #16] \n\t"                                 \
+         "ldr r4, [%1] \n\t"  /* target->r4 */                    \
+         VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R4                   \
+         "add sp, sp, #4 \n\t"                                    \
+         "mov %0, r0"                                             \
+         : /*out*/   "=r" (_res)                                  \
+         : /*in*/    "0" (&_argvec[0])                            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS          \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_6W(lval, orig, arg1,arg2,arg3,arg4,arg5,arg6)   \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[7];                          \
+      volatile unsigned long _res;                                \
+      _argvec[0] = (unsigned long)_orig.nraddr;                   \
+      _argvec[1] = (unsigned long)(arg1);                         \
+      _argvec[2] = (unsigned long)(arg2);                         \
+      _argvec[3] = (unsigned long)(arg3);                         \
+      _argvec[4] = (unsigned long)(arg4);                         \
+      _argvec[5] = (unsigned long)(arg5);                         \
+      _argvec[6] = (unsigned long)(arg6);                         \
+      __asm__ volatile(                                           \
+         "ldr r0, [%1, #20] \n\t"                                 \
+         "ldr r1, [%1, #24] \n\t"                                 \
+         "push {r0, r1} \n\t"                                     \
+         "ldr r0, [%1, #4] \n\t"                                  \
+         "ldr r1, [%1, #8] \n\t"                                  \
+         "ldr r2, [%1, #12] \n\t"                                 \
+         "ldr r3, [%1, #16] \n\t"                                 \
+         "ldr r4, [%1] \n\t"  /* target->r4 */                    \
+         VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R4                   \
+         "add sp, sp, #8 \n\t"                                    \
+         "mov %0, r0"                                             \
+         : /*out*/   "=r" (_res)                                  \
+         : /*in*/    "0" (&_argvec[0])                            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS          \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_7W(lval, orig, arg1,arg2,arg3,arg4,arg5,arg6,   \
+                                 arg7)                            \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[8];                          \
+      volatile unsigned long _res;                                \
+      _argvec[0] = (unsigned long)_orig.nraddr;                   \
+      _argvec[1] = (unsigned long)(arg1);                         \
+      _argvec[2] = (unsigned long)(arg2);                         \
+      _argvec[3] = (unsigned long)(arg3);                         \
+      _argvec[4] = (unsigned long)(arg4);                         \
+      _argvec[5] = (unsigned long)(arg5);                         \
+      _argvec[6] = (unsigned long)(arg6);                         \
+      _argvec[7] = (unsigned long)(arg7);                         \
+      __asm__ volatile(                                           \
+         "ldr r0, [%1, #20] \n\t"                                 \
+         "ldr r1, [%1, #24] \n\t"                                 \
+         "ldr r2, [%1, #28] \n\t"                                 \
+         "push {r0, r1, r2} \n\t"                                 \
+         "ldr r0, [%1, #4] \n\t"                                  \
+         "ldr r1, [%1, #8] \n\t"                                  \
+         "ldr r2, [%1, #12] \n\t"                                 \
+         "ldr r3, [%1, #16] \n\t"                                 \
+         "ldr r4, [%1] \n\t"  /* target->r4 */                    \
+         VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R4                   \
+         "add sp, sp, #12 \n\t"                                   \
+         "mov %0, r0"                                             \
+         : /*out*/   "=r" (_res)                                  \
+         : /*in*/    "0" (&_argvec[0])                            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS          \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_8W(lval, orig, arg1,arg2,arg3,arg4,arg5,arg6,   \
+                                 arg7,arg8)                       \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[9];                          \
+      volatile unsigned long _res;                                \
+      _argvec[0] = (unsigned long)_orig.nraddr;                   \
+      _argvec[1] = (unsigned long)(arg1);                         \
+      _argvec[2] = (unsigned long)(arg2);                         \
+      _argvec[3] = (unsigned long)(arg3);                         \
+      _argvec[4] = (unsigned long)(arg4);                         \
+      _argvec[5] = (unsigned long)(arg5);                         \
+      _argvec[6] = (unsigned long)(arg6);                         \
+      _argvec[7] = (unsigned long)(arg7);                         \
+      _argvec[8] = (unsigned long)(arg8);                         \
+      __asm__ volatile(                                           \
+         "ldr r0, [%1, #20] \n\t"                                 \
+         "ldr r1, [%1, #24] \n\t"                                 \
+         "ldr r2, [%1, #28] \n\t"                                 \
+         "ldr r3, [%1, #32] \n\t"                                 \
+         "push {r0, r1, r2, r3} \n\t"                             \
+         "ldr r0, [%1, #4] \n\t"                                  \
+         "ldr r1, [%1, #8] \n\t"                                  \
+         "ldr r2, [%1, #12] \n\t"                                 \
+         "ldr r3, [%1, #16] \n\t"                                 \
+         "ldr r4, [%1] \n\t"  /* target->r4 */                    \
+         VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R4                   \
+         "add sp, sp, #16 \n\t"                                   \
+         "mov %0, r0"                                             \
+         : /*out*/   "=r" (_res)                                  \
+         : /*in*/    "0" (&_argvec[0])                            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS          \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_9W(lval, orig, arg1,arg2,arg3,arg4,arg5,arg6,   \
+                                 arg7,arg8,arg9)                  \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[10];                         \
+      volatile unsigned long _res;                                \
+      _argvec[0] = (unsigned long)_orig.nraddr;                   \
+      _argvec[1] = (unsigned long)(arg1);                         \
+      _argvec[2] = (unsigned long)(arg2);                         \
+      _argvec[3] = (unsigned long)(arg3);                         \
+      _argvec[4] = (unsigned long)(arg4);                         \
+      _argvec[5] = (unsigned long)(arg5);                         \
+      _argvec[6] = (unsigned long)(arg6);                         \
+      _argvec[7] = (unsigned long)(arg7);                         \
+      _argvec[8] = (unsigned long)(arg8);                         \
+      _argvec[9] = (unsigned long)(arg9);                         \
+      __asm__ volatile(                                           \
+         "ldr r0, [%1, #20] \n\t"                                 \
+         "ldr r1, [%1, #24] \n\t"                                 \
+         "ldr r2, [%1, #28] \n\t"                                 \
+         "ldr r3, [%1, #32] \n\t"                                 \
+         "ldr r4, [%1, #36] \n\t"                                 \
+         "push {r0, r1, r2, r3, r4} \n\t"                         \
+         "ldr r0, [%1, #4] \n\t"                                  \
+         "ldr r1, [%1, #8] \n\t"                                  \
+         "ldr r2, [%1, #12] \n\t"                                 \
+         "ldr r3, [%1, #16] \n\t"                                 \
+         "ldr r4, [%1] \n\t"  /* target->r4 */                    \
+         VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R4                   \
+         "add sp, sp, #20 \n\t"                                   \
+         "mov %0, r0"                                             \
+         : /*out*/   "=r" (_res)                                  \
+         : /*in*/    "0" (&_argvec[0])                            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS          \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_10W(lval, orig, arg1,arg2,arg3,arg4,arg5,arg6,  \
+                                  arg7,arg8,arg9,arg10)           \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[11];                         \
+      volatile unsigned long _res;                                \
+      _argvec[0] = (unsigned long)_orig.nraddr;                   \
+      _argvec[1] = (unsigned long)(arg1);                         \
+      _argvec[2] = (unsigned long)(arg2);                         \
+      _argvec[3] = (unsigned long)(arg3);                         \
+      _argvec[4] = (unsigned long)(arg4);                         \
+      _argvec[5] = (unsigned long)(arg5);                         \
+      _argvec[6] = (unsigned long)(arg6);                         \
+      _argvec[7] = (unsigned long)(arg7);                         \
+      _argvec[8] = (unsigned long)(arg8);                         \
+      _argvec[9] = (unsigned long)(arg9);                         \
+      _argvec[10] = (unsigned long)(arg10);                       \
+      __asm__ volatile(                                           \
+         "ldr r0, [%1, #40] \n\t"                                 \
+         "push {r0} \n\t"                                         \
+         "ldr r0, [%1, #20] \n\t"                                 \
+         "ldr r1, [%1, #24] \n\t"                                 \
+         "ldr r2, [%1, #28] \n\t"                                 \
+         "ldr r3, [%1, #32] \n\t"                                 \
+         "ldr r4, [%1, #36] \n\t"                                 \
+         "push {r0, r1, r2, r3, r4} \n\t"                         \
+         "ldr r0, [%1, #4] \n\t"                                  \
+         "ldr r1, [%1, #8] \n\t"                                  \
+         "ldr r2, [%1, #12] \n\t"                                 \
+         "ldr r3, [%1, #16] \n\t"                                 \
+         "ldr r4, [%1] \n\t"  /* target->r4 */                    \
+         VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R4                   \
+         "add sp, sp, #24 \n\t"                                   \
+         "mov %0, r0"                                             \
+         : /*out*/   "=r" (_res)                                  \
+         : /*in*/    "0" (&_argvec[0])                            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS          \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_11W(lval, orig, arg1,arg2,arg3,arg4,arg5,       \
+                                  arg6,arg7,arg8,arg9,arg10,      \
+                                  arg11)                          \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[12];                         \
+      volatile unsigned long _res;                                \
+      _argvec[0] = (unsigned long)_orig.nraddr;                   \
+      _argvec[1] = (unsigned long)(arg1);                         \
+      _argvec[2] = (unsigned long)(arg2);                         \
+      _argvec[3] = (unsigned long)(arg3);                         \
+      _argvec[4] = (unsigned long)(arg4);                         \
+      _argvec[5] = (unsigned long)(arg5);                         \
+      _argvec[6] = (unsigned long)(arg6);                         \
+      _argvec[7] = (unsigned long)(arg7);                         \
+      _argvec[8] = (unsigned long)(arg8);                         \
+      _argvec[9] = (unsigned long)(arg9);                         \
+      _argvec[10] = (unsigned long)(arg10);                       \
+      _argvec[11] = (unsigned long)(arg11);                       \
+      __asm__ volatile(                                           \
+         "ldr r0, [%1, #40] \n\t"                                 \
+         "ldr r1, [%1, #44] \n\t"                                 \
+         "push {r0, r1} \n\t"                                     \
+         "ldr r0, [%1, #20] \n\t"                                 \
+         "ldr r1, [%1, #24] \n\t"                                 \
+         "ldr r2, [%1, #28] \n\t"                                 \
+         "ldr r3, [%1, #32] \n\t"                                 \
+         "ldr r4, [%1, #36] \n\t"                                 \
+         "push {r0, r1, r2, r3, r4} \n\t"                         \
+         "ldr r0, [%1, #4] \n\t"                                  \
+         "ldr r1, [%1, #8] \n\t"                                  \
+         "ldr r2, [%1, #12] \n\t"                                 \
+         "ldr r3, [%1, #16] \n\t"                                 \
+         "ldr r4, [%1] \n\t"  /* target->r4 */                    \
+         VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R4                   \
+         "add sp, sp, #28 \n\t"                                   \
+         "mov %0, r0"                                             \
+         : /*out*/   "=r" (_res)                                  \
+         : /*in*/    "0" (&_argvec[0])                            \
+         : /*trash*/ "cc", "memory",__CALLER_SAVED_REGS           \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_12W(lval, orig, arg1,arg2,arg3,arg4,arg5,       \
+                                  arg6,arg7,arg8,arg9,arg10,      \
+                                  arg11,arg12)                    \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[13];                         \
+      volatile unsigned long _res;                                \
+      _argvec[0] = (unsigned long)_orig.nraddr;                   \
+      _argvec[1] = (unsigned long)(arg1);                         \
+      _argvec[2] = (unsigned long)(arg2);                         \
+      _argvec[3] = (unsigned long)(arg3);                         \
+      _argvec[4] = (unsigned long)(arg4);                         \
+      _argvec[5] = (unsigned long)(arg5);                         \
+      _argvec[6] = (unsigned long)(arg6);                         \
+      _argvec[7] = (unsigned long)(arg7);                         \
+      _argvec[8] = (unsigned long)(arg8);                         \
+      _argvec[9] = (unsigned long)(arg9);                         \
+      _argvec[10] = (unsigned long)(arg10);                       \
+      _argvec[11] = (unsigned long)(arg11);                       \
+      _argvec[12] = (unsigned long)(arg12);                       \
+      __asm__ volatile(                                           \
+         "ldr r0, [%1, #40] \n\t"                                 \
+         "ldr r1, [%1, #44] \n\t"                                 \
+         "ldr r2, [%1, #48] \n\t"                                 \
+         "push {r0, r1, r2} \n\t"                                 \
+         "ldr r0, [%1, #20] \n\t"                                 \
+         "ldr r1, [%1, #24] \n\t"                                 \
+         "ldr r2, [%1, #28] \n\t"                                 \
+         "ldr r3, [%1, #32] \n\t"                                 \
+         "ldr r4, [%1, #36] \n\t"                                 \
+         "push {r0, r1, r2, r3, r4} \n\t"                         \
+         "ldr r0, [%1, #4] \n\t"                                  \
+         "ldr r1, [%1, #8] \n\t"                                  \
+         "ldr r2, [%1, #12] \n\t"                                 \
+         "ldr r3, [%1, #16] \n\t"                                 \
+         "ldr r4, [%1] \n\t"  /* target->r4 */                    \
+         VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R4                   \
+         "add sp, sp, #32 \n\t"                                   \
+         "mov %0, r0"                                             \
+         : /*out*/   "=r" (_res)                                  \
+         : /*in*/    "0" (&_argvec[0])                            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS          \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#endif /* PLAT_arm_linux */
+
+/* ------------------------ ppc32-aix5 ------------------------- */
+
+#if defined(PLAT_ppc32_aix5)
+
+/* ARGREGS: r3 r4 r5 r6 r7 r8 r9 r10 (the rest on stack somewhere) */
+
+/* These regs are trashed by the hidden call. */
+#define __CALLER_SAVED_REGS                                       \
+   "lr", "ctr", "xer",                                            \
+   "cr0", "cr1", "cr2", "cr3", "cr4", "cr5", "cr6", "cr7",        \
+   "r0", "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10",   \
+   "r11", "r12", "r13"
+
+/* Expand the stack frame, copying enough info that unwinding
+   still works.  Trashes r3. */
+
+#define VG_EXPAND_FRAME_BY_trashes_r3(_n_fr)                      \
+         "addi 1,1,-" #_n_fr "\n\t"                               \
+         "lwz  3," #_n_fr "(1)\n\t"                               \
+         "stw  3,0(1)\n\t"
+
+#define VG_CONTRACT_FRAME_BY(_n_fr)                               \
+         "addi 1,1," #_n_fr "\n\t"
+
+/* These CALL_FN_ macros assume that on ppc32-aix5, sizeof(unsigned
+   long) == 4. */
+
+#define CALL_FN_W_v(lval, orig)                                   \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[3+0];                        \
+      volatile unsigned long _res;                                \
+      /* _argvec[0] holds current r2 across the call */           \
+      _argvec[1] = (unsigned long)_orig.r2;                       \
+      _argvec[2] = (unsigned long)_orig.nraddr;                   \
+      __asm__ volatile(                                           \
+         "mr 11,%1\n\t"                                           \
+         VG_EXPAND_FRAME_BY_trashes_r3(512)                       \
+         "stw  2,-8(11)\n\t"  /* save tocptr */                   \
+         "lwz  2,-4(11)\n\t"  /* use nraddr's tocptr */           \
+         "lwz 11, 0(11)\n\t"  /* target->r11 */                   \
+         VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R11                  \
+         "mr 11,%1\n\t"                                           \
+         "mr %0,3\n\t"                                            \
+         "lwz 2,-8(11)\n\t" /* restore tocptr */                  \
+         VG_CONTRACT_FRAME_BY(512)                                \
+         : /*out*/   "=r" (_res)                                  \
+         : /*in*/    "r" (&_argvec[2])                            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS          \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_W(lval, orig, arg1)                             \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[3+1];                        \
+      volatile unsigned long _res;                                \
+      /* _argvec[0] holds current r2 across the call */           \
+      _argvec[1]   = (unsigned long)_orig.r2;                     \
+      _argvec[2]   = (unsigned long)_orig.nraddr;                 \
+      _argvec[2+1] = (unsigned long)arg1;                         \
+      __asm__ volatile(                                           \
+         "mr 11,%1\n\t"                                           \
+         VG_EXPAND_FRAME_BY_trashes_r3(512)                       \
+         "stw  2,-8(11)\n\t"  /* save tocptr */                   \
+         "lwz  2,-4(11)\n\t"  /* use nraddr's tocptr */           \
+         "lwz  3, 4(11)\n\t"  /* arg1->r3 */                      \
+         "lwz 11, 0(11)\n\t"  /* target->r11 */                   \
+         VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R11                  \
+         "mr 11,%1\n\t"                                           \
+         "mr %0,3\n\t"                                            \
+         "lwz 2,-8(11)\n\t" /* restore tocptr */                  \
+         VG_CONTRACT_FRAME_BY(512)                                \
+         : /*out*/   "=r" (_res)                                  \
+         : /*in*/    "r" (&_argvec[2])                            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS          \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_WW(lval, orig, arg1,arg2)                       \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[3+2];                        \
+      volatile unsigned long _res;                                \
+      /* _argvec[0] holds current r2 across the call */           \
+      _argvec[1]   = (unsigned long)_orig.r2;                     \
+      _argvec[2]   = (unsigned long)_orig.nraddr;                 \
+      _argvec[2+1] = (unsigned long)arg1;                         \
+      _argvec[2+2] = (unsigned long)arg2;                         \
+      __asm__ volatile(                                           \
+         "mr 11,%1\n\t"                                           \
+         VG_EXPAND_FRAME_BY_trashes_r3(512)                       \
+         "stw  2,-8(11)\n\t"  /* save tocptr */                   \
+         "lwz  2,-4(11)\n\t"  /* use nraddr's tocptr */           \
+         "lwz  3, 4(11)\n\t"  /* arg1->r3 */                      \
+         "lwz  4, 8(11)\n\t"  /* arg2->r4 */                      \
+         "lwz 11, 0(11)\n\t"  /* target->r11 */                   \
+         VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R11                  \
+         "mr 11,%1\n\t"                                           \
+         "mr %0,3\n\t"                                            \
+         "lwz 2,-8(11)\n\t" /* restore tocptr */                  \
+         VG_CONTRACT_FRAME_BY(512)                                \
+         : /*out*/   "=r" (_res)                                  \
+         : /*in*/    "r" (&_argvec[2])                            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS          \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_WWW(lval, orig, arg1,arg2,arg3)                 \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[3+3];                        \
+      volatile unsigned long _res;                                \
+      /* _argvec[0] holds current r2 across the call */           \
+      _argvec[1]   = (unsigned long)_orig.r2;                     \
+      _argvec[2]   = (unsigned long)_orig.nraddr;                 \
+      _argvec[2+1] = (unsigned long)arg1;                         \
+      _argvec[2+2] = (unsigned long)arg2;                         \
+      _argvec[2+3] = (unsigned long)arg3;                         \
+      __asm__ volatile(                                           \
+         "mr 11,%1\n\t"                                           \
+         VG_EXPAND_FRAME_BY_trashes_r3(512)                       \
+         "stw  2,-8(11)\n\t"  /* save tocptr */                   \
+         "lwz  2,-4(11)\n\t"  /* use nraddr's tocptr */           \
+         "lwz  3, 4(11)\n\t"  /* arg1->r3 */                      \
+         "lwz  4, 8(11)\n\t"  /* arg2->r4 */                      \
+         "lwz  5, 12(11)\n\t" /* arg3->r5 */                      \
+         "lwz 11, 0(11)\n\t"  /* target->r11 */                   \
+         VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R11                  \
+         "mr 11,%1\n\t"                                           \
+         "mr %0,3\n\t"                                            \
+         "lwz 2,-8(11)\n\t" /* restore tocptr */                  \
+         VG_CONTRACT_FRAME_BY(512)                                \
+         : /*out*/   "=r" (_res)                                  \
+         : /*in*/    "r" (&_argvec[2])                            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS          \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_WWWW(lval, orig, arg1,arg2,arg3,arg4)           \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[3+4];                        \
+      volatile unsigned long _res;                                \
+      /* _argvec[0] holds current r2 across the call */           \
+      _argvec[1]   = (unsigned long)_orig.r2;                     \
+      _argvec[2]   = (unsigned long)_orig.nraddr;                 \
+      _argvec[2+1] = (unsigned long)arg1;                         \
+      _argvec[2+2] = (unsigned long)arg2;                         \
+      _argvec[2+3] = (unsigned long)arg3;                         \
+      _argvec[2+4] = (unsigned long)arg4;                         \
+      __asm__ volatile(                                           \
+         "mr 11,%1\n\t"                                           \
+         VG_EXPAND_FRAME_BY_trashes_r3(512)                       \
+         "stw  2,-8(11)\n\t"  /* save tocptr */                   \
+         "lwz  2,-4(11)\n\t"  /* use nraddr's tocptr */           \
+         "lwz  3, 4(11)\n\t"  /* arg1->r3 */                      \
+         "lwz  4, 8(11)\n\t"  /* arg2->r4 */                      \
+         "lwz  5, 12(11)\n\t" /* arg3->r5 */                      \
+         "lwz  6, 16(11)\n\t" /* arg4->r6 */                      \
+         "lwz 11, 0(11)\n\t"  /* target->r11 */                   \
+         VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R11                  \
+         "mr 11,%1\n\t"                                           \
+         "mr %0,3\n\t"                                            \
+         "lwz 2,-8(11)\n\t" /* restore tocptr */                  \
+         VG_CONTRACT_FRAME_BY(512)                                \
+         : /*out*/   "=r" (_res)                                  \
+         : /*in*/    "r" (&_argvec[2])                            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS          \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_5W(lval, orig, arg1,arg2,arg3,arg4,arg5)        \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[3+5];                        \
+      volatile unsigned long _res;                                \
+      /* _argvec[0] holds current r2 across the call */           \
+      _argvec[1]   = (unsigned long)_orig.r2;                     \
+      _argvec[2]   = (unsigned long)_orig.nraddr;                 \
+      _argvec[2+1] = (unsigned long)arg1;                         \
+      _argvec[2+2] = (unsigned long)arg2;                         \
+      _argvec[2+3] = (unsigned long)arg3;                         \
+      _argvec[2+4] = (unsigned long)arg4;                         \
+      _argvec[2+5] = (unsigned long)arg5;                         \
+      __asm__ volatile(                                           \
+         "mr 11,%1\n\t"                                           \
+         VG_EXPAND_FRAME_BY_trashes_r3(512)                       \
+         "stw  2,-8(11)\n\t"  /* save tocptr */                   \
+         "lwz  2,-4(11)\n\t"  /* use nraddr's tocptr */           \
+         "lwz  3, 4(11)\n\t"  /* arg1->r3 */                      \
+         "lwz  4, 8(11)\n\t" /* arg2->r4 */                       \
+         "lwz  5, 12(11)\n\t" /* arg3->r5 */                      \
+         "lwz  6, 16(11)\n\t" /* arg4->r6 */                      \
+         "lwz  7, 20(11)\n\t" /* arg5->r7 */                      \
+         "lwz 11, 0(11)\n\t"  /* target->r11 */                   \
+         VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R11                  \
+         "mr 11,%1\n\t"                                           \
+         "mr %0,3\n\t"                                            \
+         "lwz 2,-8(11)\n\t" /* restore tocptr */                  \
+         VG_CONTRACT_FRAME_BY(512)                                \
+         : /*out*/   "=r" (_res)                                  \
+         : /*in*/    "r" (&_argvec[2])                            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS          \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_6W(lval, orig, arg1,arg2,arg3,arg4,arg5,arg6)   \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[3+6];                        \
+      volatile unsigned long _res;                                \
+      /* _argvec[0] holds current r2 across the call */           \
+      _argvec[1]   = (unsigned long)_orig.r2;                     \
+      _argvec[2]   = (unsigned long)_orig.nraddr;                 \
+      _argvec[2+1] = (unsigned long)arg1;                         \
+      _argvec[2+2] = (unsigned long)arg2;                         \
+      _argvec[2+3] = (unsigned long)arg3;                         \
+      _argvec[2+4] = (unsigned long)arg4;                         \
+      _argvec[2+5] = (unsigned long)arg5;                         \
+      _argvec[2+6] = (unsigned long)arg6;                         \
+      __asm__ volatile(                                           \
+         "mr 11,%1\n\t"                                           \
+         VG_EXPAND_FRAME_BY_trashes_r3(512)                       \
+         "stw  2,-8(11)\n\t"  /* save tocptr */                   \
+         "lwz  2,-4(11)\n\t"  /* use nraddr's tocptr */           \
+         "lwz  3, 4(11)\n\t"  /* arg1->r3 */                      \
+         "lwz  4, 8(11)\n\t"  /* arg2->r4 */                      \
+         "lwz  5, 12(11)\n\t" /* arg3->r5 */                      \
+         "lwz  6, 16(11)\n\t" /* arg4->r6 */                      \
+         "lwz  7, 20(11)\n\t" /* arg5->r7 */                      \
+         "lwz  8, 24(11)\n\t" /* arg6->r8 */                      \
+         "lwz 11, 0(11)\n\t"  /* target->r11 */                   \
+         VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R11                  \
+         "mr 11,%1\n\t"                                           \
+         "mr %0,3\n\t"                                            \
+         "lwz 2,-8(11)\n\t" /* restore tocptr */                  \
+         VG_CONTRACT_FRAME_BY(512)                                \
+         : /*out*/   "=r" (_res)                                  \
+         : /*in*/    "r" (&_argvec[2])                            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS          \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_7W(lval, orig, arg1,arg2,arg3,arg4,arg5,arg6,   \
+                                 arg7)                            \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[3+7];                        \
+      volatile unsigned long _res;                                \
+      /* _argvec[0] holds current r2 across the call */           \
+      _argvec[1]   = (unsigned long)_orig.r2;                     \
+      _argvec[2]   = (unsigned long)_orig.nraddr;                 \
+      _argvec[2+1] = (unsigned long)arg1;                         \
+      _argvec[2+2] = (unsigned long)arg2;                         \
+      _argvec[2+3] = (unsigned long)arg3;                         \
+      _argvec[2+4] = (unsigned long)arg4;                         \
+      _argvec[2+5] = (unsigned long)arg5;                         \
+      _argvec[2+6] = (unsigned long)arg6;                         \
+      _argvec[2+7] = (unsigned long)arg7;                         \
+      __asm__ volatile(                                           \
+         "mr 11,%1\n\t"                                           \
+         VG_EXPAND_FRAME_BY_trashes_r3(512)                       \
+         "stw  2,-8(11)\n\t"  /* save tocptr */                   \
+         "lwz  2,-4(11)\n\t"  /* use nraddr's tocptr */           \
+         "lwz  3, 4(11)\n\t"  /* arg1->r3 */                      \
+         "lwz  4, 8(11)\n\t"  /* arg2->r4 */                      \
+         "lwz  5, 12(11)\n\t" /* arg3->r5 */                      \
+         "lwz  6, 16(11)\n\t" /* arg4->r6 */                      \
+         "lwz  7, 20(11)\n\t" /* arg5->r7 */                      \
+         "lwz  8, 24(11)\n\t" /* arg6->r8 */                      \
+         "lwz  9, 28(11)\n\t" /* arg7->r9 */                      \
+         "lwz 11, 0(11)\n\t"  /* target->r11 */                   \
+         VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R11                  \
+         "mr 11,%1\n\t"                                           \
+         "mr %0,3\n\t"                                            \
+         "lwz 2,-8(11)\n\t" /* restore tocptr */                  \
+         VG_CONTRACT_FRAME_BY(512)                                \
+         : /*out*/   "=r" (_res)                                  \
+         : /*in*/    "r" (&_argvec[2])                            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS          \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_8W(lval, orig, arg1,arg2,arg3,arg4,arg5,arg6,   \
+                                 arg7,arg8)                       \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[3+8];                        \
+      volatile unsigned long _res;                                \
+      /* _argvec[0] holds current r2 across the call */           \
+      _argvec[1]   = (unsigned long)_orig.r2;                     \
+      _argvec[2]   = (unsigned long)_orig.nraddr;                 \
+      _argvec[2+1] = (unsigned long)arg1;                         \
+      _argvec[2+2] = (unsigned long)arg2;                         \
+      _argvec[2+3] = (unsigned long)arg3;                         \
+      _argvec[2+4] = (unsigned long)arg4;                         \
+      _argvec[2+5] = (unsigned long)arg5;                         \
+      _argvec[2+6] = (unsigned long)arg6;                         \
+      _argvec[2+7] = (unsigned long)arg7;                         \
+      _argvec[2+8] = (unsigned long)arg8;                         \
+      __asm__ volatile(                                           \
+         "mr 11,%1\n\t"                                           \
+         VG_EXPAND_FRAME_BY_trashes_r3(512)                       \
+         "stw  2,-8(11)\n\t"  /* save tocptr */                   \
+         "lwz  2,-4(11)\n\t"  /* use nraddr's tocptr */           \
+         "lwz  3, 4(11)\n\t"  /* arg1->r3 */                      \
+         "lwz  4, 8(11)\n\t"  /* arg2->r4 */                      \
+         "lwz  5, 12(11)\n\t" /* arg3->r5 */                      \
+         "lwz  6, 16(11)\n\t" /* arg4->r6 */                      \
+         "lwz  7, 20(11)\n\t" /* arg5->r7 */                      \
+         "lwz  8, 24(11)\n\t" /* arg6->r8 */                      \
+         "lwz  9, 28(11)\n\t" /* arg7->r9 */                      \
+         "lwz 10, 32(11)\n\t" /* arg8->r10 */                     \
+         "lwz 11, 0(11)\n\t"  /* target->r11 */                   \
+         VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R11                  \
+         "mr 11,%1\n\t"                                           \
+         "mr %0,3\n\t"                                            \
+         "lwz 2,-8(11)\n\t" /* restore tocptr */                  \
+         VG_CONTRACT_FRAME_BY(512)                                \
+         : /*out*/   "=r" (_res)                                  \
+         : /*in*/    "r" (&_argvec[2])                            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS          \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_9W(lval, orig, arg1,arg2,arg3,arg4,arg5,arg6,   \
+                                 arg7,arg8,arg9)                  \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[3+9];                        \
+      volatile unsigned long _res;                                \
+      /* _argvec[0] holds current r2 across the call */           \
+      _argvec[1]   = (unsigned long)_orig.r2;                     \
+      _argvec[2]   = (unsigned long)_orig.nraddr;                 \
+      _argvec[2+1] = (unsigned long)arg1;                         \
+      _argvec[2+2] = (unsigned long)arg2;                         \
+      _argvec[2+3] = (unsigned long)arg3;                         \
+      _argvec[2+4] = (unsigned long)arg4;                         \
+      _argvec[2+5] = (unsigned long)arg5;                         \
+      _argvec[2+6] = (unsigned long)arg6;                         \
+      _argvec[2+7] = (unsigned long)arg7;                         \
+      _argvec[2+8] = (unsigned long)arg8;                         \
+      _argvec[2+9] = (unsigned long)arg9;                         \
+      __asm__ volatile(                                           \
+         "mr 11,%1\n\t"                                           \
+         VG_EXPAND_FRAME_BY_trashes_r3(512)                       \
+         "stw  2,-8(11)\n\t"  /* save tocptr */                   \
+         "lwz  2,-4(11)\n\t"  /* use nraddr's tocptr */           \
+         VG_EXPAND_FRAME_BY_trashes_r3(64)                        \
+         /* arg9 */                                               \
+         "lwz 3,36(11)\n\t"                                       \
+         "stw 3,56(1)\n\t"                                        \
+         /* args1-8 */                                            \
+         "lwz  3, 4(11)\n\t"  /* arg1->r3 */                      \
+         "lwz  4, 8(11)\n\t"  /* arg2->r4 */                      \
+         "lwz  5, 12(11)\n\t" /* arg3->r5 */                      \
+         "lwz  6, 16(11)\n\t" /* arg4->r6 */                      \
+         "lwz  7, 20(11)\n\t" /* arg5->r7 */                      \
+         "lwz  8, 24(11)\n\t" /* arg6->r8 */                      \
+         "lwz  9, 28(11)\n\t" /* arg7->r9 */                      \
+         "lwz 10, 32(11)\n\t" /* arg8->r10 */                     \
+         "lwz 11, 0(11)\n\t"  /* target->r11 */                   \
+         VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R11                  \
+         "mr 11,%1\n\t"                                           \
+         "mr %0,3\n\t"                                            \
+         "lwz 2,-8(11)\n\t" /* restore tocptr */                  \
+         VG_CONTRACT_FRAME_BY(64)                                 \
+         VG_CONTRACT_FRAME_BY(512)                                \
+         : /*out*/   "=r" (_res)                                  \
+         : /*in*/    "r" (&_argvec[2])                            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS          \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_10W(lval, orig, arg1,arg2,arg3,arg4,arg5,arg6,  \
+                                  arg7,arg8,arg9,arg10)           \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[3+10];                       \
+      volatile unsigned long _res;                                \
+      /* _argvec[0] holds current r2 across the call */           \
+      _argvec[1]   = (unsigned long)_orig.r2;                     \
+      _argvec[2]   = (unsigned long)_orig.nraddr;                 \
+      _argvec[2+1] = (unsigned long)arg1;                         \
+      _argvec[2+2] = (unsigned long)arg2;                         \
+      _argvec[2+3] = (unsigned long)arg3;                         \
+      _argvec[2+4] = (unsigned long)arg4;                         \
+      _argvec[2+5] = (unsigned long)arg5;                         \
+      _argvec[2+6] = (unsigned long)arg6;                         \
+      _argvec[2+7] = (unsigned long)arg7;                         \
+      _argvec[2+8] = (unsigned long)arg8;                         \
+      _argvec[2+9] = (unsigned long)arg9;                         \
+      _argvec[2+10] = (unsigned long)arg10;                       \
+      __asm__ volatile(                                           \
+         "mr 11,%1\n\t"                                           \
+         VG_EXPAND_FRAME_BY_trashes_r3(512)                       \
+         "stw  2,-8(11)\n\t"  /* save tocptr */                   \
+         "lwz  2,-4(11)\n\t"  /* use nraddr's tocptr */           \
+         VG_EXPAND_FRAME_BY_trashes_r3(64)                        \
+         /* arg10 */                                              \
+         "lwz 3,40(11)\n\t"                                       \
+         "stw 3,60(1)\n\t"                                        \
+         /* arg9 */                                               \
+         "lwz 3,36(11)\n\t"                                       \
+         "stw 3,56(1)\n\t"                                        \
+         /* args1-8 */                                            \
+         "lwz  3, 4(11)\n\t"  /* arg1->r3 */                      \
+         "lwz  4, 8(11)\n\t"  /* arg2->r4 */                      \
+         "lwz  5, 12(11)\n\t" /* arg3->r5 */                      \
+         "lwz  6, 16(11)\n\t" /* arg4->r6 */                      \
+         "lwz  7, 20(11)\n\t" /* arg5->r7 */                      \
+         "lwz  8, 24(11)\n\t" /* arg6->r8 */                      \
+         "lwz  9, 28(11)\n\t" /* arg7->r9 */                      \
+         "lwz 10, 32(11)\n\t" /* arg8->r10 */                     \
+         "lwz 11, 0(11)\n\t"  /* target->r11 */                   \
+         VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R11                  \
+         "mr 11,%1\n\t"                                           \
+         "mr %0,3\n\t"                                            \
+         "lwz 2,-8(11)\n\t" /* restore tocptr */                  \
+         VG_CONTRACT_FRAME_BY(64)                                 \
+         VG_CONTRACT_FRAME_BY(512)                                \
+         : /*out*/   "=r" (_res)                                  \
+         : /*in*/    "r" (&_argvec[2])                            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS          \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_11W(lval, orig, arg1,arg2,arg3,arg4,arg5,arg6,  \
+                                  arg7,arg8,arg9,arg10,arg11)     \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[3+11];                       \
+      volatile unsigned long _res;                                \
+      /* _argvec[0] holds current r2 across the call */           \
+      _argvec[1]   = (unsigned long)_orig.r2;                     \
+      _argvec[2]   = (unsigned long)_orig.nraddr;                 \
+      _argvec[2+1] = (unsigned long)arg1;                         \
+      _argvec[2+2] = (unsigned long)arg2;                         \
+      _argvec[2+3] = (unsigned long)arg3;                         \
+      _argvec[2+4] = (unsigned long)arg4;                         \
+      _argvec[2+5] = (unsigned long)arg5;                         \
+      _argvec[2+6] = (unsigned long)arg6;                         \
+      _argvec[2+7] = (unsigned long)arg7;                         \
+      _argvec[2+8] = (unsigned long)arg8;                         \
+      _argvec[2+9] = (unsigned long)arg9;                         \
+      _argvec[2+10] = (unsigned long)arg10;                       \
+      _argvec[2+11] = (unsigned long)arg11;                       \
+      __asm__ volatile(                                           \
+         "mr 11,%1\n\t"                                           \
+         VG_EXPAND_FRAME_BY_trashes_r3(512)                       \
+         "stw  2,-8(11)\n\t"  /* save tocptr */                   \
+         "lwz  2,-4(11)\n\t"  /* use nraddr's tocptr */           \
+         VG_EXPAND_FRAME_BY_trashes_r3(72)                        \
+         /* arg11 */                                              \
+         "lwz 3,44(11)\n\t"                                       \
+         "stw 3,64(1)\n\t"                                        \
+         /* arg10 */                                              \
+         "lwz 3,40(11)\n\t"                                       \
+         "stw 3,60(1)\n\t"                                        \
+         /* arg9 */                                               \
+         "lwz 3,36(11)\n\t"                                       \
+         "stw 3,56(1)\n\t"                                        \
+         /* args1-8 */                                            \
+         "lwz  3, 4(11)\n\t"  /* arg1->r3 */                      \
+         "lwz  4, 8(11)\n\t"  /* arg2->r4 */                      \
+         "lwz  5, 12(11)\n\t" /* arg3->r5 */                      \
+         "lwz  6, 16(11)\n\t" /* arg4->r6 */                      \
+         "lwz  7, 20(11)\n\t" /* arg5->r7 */                      \
+         "lwz  8, 24(11)\n\t" /* arg6->r8 */                      \
+         "lwz  9, 28(11)\n\t" /* arg7->r9 */                      \
+         "lwz 10, 32(11)\n\t" /* arg8->r10 */                     \
+         "lwz 11, 0(11)\n\t"  /* target->r11 */                   \
+         VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R11                  \
+         "mr 11,%1\n\t"                                           \
+         "mr %0,3\n\t"                                            \
+         "lwz 2,-8(11)\n\t" /* restore tocptr */                  \
+         VG_CONTRACT_FRAME_BY(72)                                 \
+         VG_CONTRACT_FRAME_BY(512)                                \
+         : /*out*/   "=r" (_res)                                  \
+         : /*in*/    "r" (&_argvec[2])                            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS          \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_12W(lval, orig, arg1,arg2,arg3,arg4,arg5,arg6,  \
+                                arg7,arg8,arg9,arg10,arg11,arg12) \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[3+12];                       \
+      volatile unsigned long _res;                                \
+      /* _argvec[0] holds current r2 across the call */           \
+      _argvec[1]   = (unsigned long)_orig.r2;                     \
+      _argvec[2]   = (unsigned long)_orig.nraddr;                 \
+      _argvec[2+1] = (unsigned long)arg1;                         \
+      _argvec[2+2] = (unsigned long)arg2;                         \
+      _argvec[2+3] = (unsigned long)arg3;                         \
+      _argvec[2+4] = (unsigned long)arg4;                         \
+      _argvec[2+5] = (unsigned long)arg5;                         \
+      _argvec[2+6] = (unsigned long)arg6;                         \
+      _argvec[2+7] = (unsigned long)arg7;                         \
+      _argvec[2+8] = (unsigned long)arg8;                         \
+      _argvec[2+9] = (unsigned long)arg9;                         \
+      _argvec[2+10] = (unsigned long)arg10;                       \
+      _argvec[2+11] = (unsigned long)arg11;                       \
+      _argvec[2+12] = (unsigned long)arg12;                       \
+      __asm__ volatile(                                           \
+         "mr 11,%1\n\t"                                           \
+         VG_EXPAND_FRAME_BY_trashes_r3(512)                       \
+         "stw  2,-8(11)\n\t"  /* save tocptr */                   \
+         "lwz  2,-4(11)\n\t"  /* use nraddr's tocptr */           \
+         VG_EXPAND_FRAME_BY_trashes_r3(72)                        \
+         /* arg12 */                                              \
+         "lwz 3,48(11)\n\t"                                       \
+         "stw 3,68(1)\n\t"                                        \
+         /* arg11 */                                              \
+         "lwz 3,44(11)\n\t"                                       \
+         "stw 3,64(1)\n\t"                                        \
+         /* arg10 */                                              \
+         "lwz 3,40(11)\n\t"                                       \
+         "stw 3,60(1)\n\t"                                        \
+         /* arg9 */                                               \
+         "lwz 3,36(11)\n\t"                                       \
+         "stw 3,56(1)\n\t"                                        \
+         /* args1-8 */                                            \
+         "lwz  3, 4(11)\n\t"  /* arg1->r3 */                      \
+         "lwz  4, 8(11)\n\t"  /* arg2->r4 */                      \
+         "lwz  5, 12(11)\n\t" /* arg3->r5 */                      \
+         "lwz  6, 16(11)\n\t" /* arg4->r6 */                      \
+         "lwz  7, 20(11)\n\t" /* arg5->r7 */                      \
+         "lwz  8, 24(11)\n\t" /* arg6->r8 */                      \
+         "lwz  9, 28(11)\n\t" /* arg7->r9 */                      \
+         "lwz 10, 32(11)\n\t" /* arg8->r10 */                     \
+         "lwz 11, 0(11)\n\t"  /* target->r11 */                   \
+         VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R11                  \
+         "mr 11,%1\n\t"                                           \
+         "mr %0,3\n\t"                                            \
+         "lwz 2,-8(11)\n\t" /* restore tocptr */                  \
+         VG_CONTRACT_FRAME_BY(72)                                 \
+         VG_CONTRACT_FRAME_BY(512)                                \
+         : /*out*/   "=r" (_res)                                  \
+         : /*in*/    "r" (&_argvec[2])                            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS          \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#endif /* PLAT_ppc32_aix5 */
+
+/* ------------------------ ppc64-aix5 ------------------------- */
+
+#if defined(PLAT_ppc64_aix5)
+
+/* ARGREGS: r3 r4 r5 r6 r7 r8 r9 r10 (the rest on stack somewhere) */
+
+/* These regs are trashed by the hidden call. */
+#define __CALLER_SAVED_REGS                                       \
+   "lr", "ctr", "xer",                                            \
+   "cr0", "cr1", "cr2", "cr3", "cr4", "cr5", "cr6", "cr7",        \
+   "r0", "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10",   \
+   "r11", "r12", "r13"
+
+/* Expand the stack frame, copying enough info that unwinding
+   still works.  Trashes r3. */
+
+#define VG_EXPAND_FRAME_BY_trashes_r3(_n_fr)                      \
+         "addi 1,1,-" #_n_fr "\n\t"                               \
+         "ld   3," #_n_fr "(1)\n\t"                               \
+         "std  3,0(1)\n\t"
+
+#define VG_CONTRACT_FRAME_BY(_n_fr)                               \
+         "addi 1,1," #_n_fr "\n\t"
+
+/* These CALL_FN_ macros assume that on ppc64-aix5, sizeof(unsigned
+   long) == 8. */
+
+#define CALL_FN_W_v(lval, orig)                                   \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[3+0];                        \
+      volatile unsigned long _res;                                \
+      /* _argvec[0] holds current r2 across the call */           \
+      _argvec[1] = (unsigned long)_orig.r2;                       \
+      _argvec[2] = (unsigned long)_orig.nraddr;                   \
+      __asm__ volatile(                                           \
+         "mr 11,%1\n\t"                                           \
+         VG_EXPAND_FRAME_BY_trashes_r3(512)                       \
+         "std  2,-16(11)\n\t" /* save tocptr */                   \
+         "ld   2,-8(11)\n\t"  /* use nraddr's tocptr */           \
+         "ld  11, 0(11)\n\t"  /* target->r11 */                   \
+         VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R11                  \
+         "mr 11,%1\n\t"                                           \
+         "mr %0,3\n\t"                                            \
+         "ld 2,-16(11)\n\t" /* restore tocptr */                  \
+         VG_CONTRACT_FRAME_BY(512)                                \
+         : /*out*/   "=r" (_res)                                  \
+         : /*in*/    "r" (&_argvec[2])                            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS          \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_W(lval, orig, arg1)                             \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[3+1];                        \
+      volatile unsigned long _res;                                \
+      /* _argvec[0] holds current r2 across the call */           \
+      _argvec[1]   = (unsigned long)_orig.r2;                     \
+      _argvec[2]   = (unsigned long)_orig.nraddr;                 \
+      _argvec[2+1] = (unsigned long)arg1;                         \
+      __asm__ volatile(                                           \
+         "mr 11,%1\n\t"                                           \
+         VG_EXPAND_FRAME_BY_trashes_r3(512)                       \
+         "std  2,-16(11)\n\t" /* save tocptr */                   \
+         "ld   2,-8(11)\n\t"  /* use nraddr's tocptr */           \
+         "ld   3, 8(11)\n\t"  /* arg1->r3 */                      \
+         "ld  11, 0(11)\n\t"  /* target->r11 */                   \
+         VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R11                  \
+         "mr 11,%1\n\t"                                           \
+         "mr %0,3\n\t"                                            \
+         "ld 2,-16(11)\n\t" /* restore tocptr */                  \
+         VG_CONTRACT_FRAME_BY(512)                                \
+         : /*out*/   "=r" (_res)                                  \
+         : /*in*/    "r" (&_argvec[2])                            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS          \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_WW(lval, orig, arg1,arg2)                       \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[3+2];                        \
+      volatile unsigned long _res;                                \
+      /* _argvec[0] holds current r2 across the call */           \
+      _argvec[1]   = (unsigned long)_orig.r2;                     \
+      _argvec[2]   = (unsigned long)_orig.nraddr;                 \
+      _argvec[2+1] = (unsigned long)arg1;                         \
+      _argvec[2+2] = (unsigned long)arg2;                         \
+      __asm__ volatile(                                           \
+         "mr 11,%1\n\t"                                           \
+         VG_EXPAND_FRAME_BY_trashes_r3(512)                       \
+         "std  2,-16(11)\n\t" /* save tocptr */                   \
+         "ld   2,-8(11)\n\t"  /* use nraddr's tocptr */           \
+         "ld   3, 8(11)\n\t"  /* arg1->r3 */                      \
+         "ld   4, 16(11)\n\t" /* arg2->r4 */                      \
+         "ld  11, 0(11)\n\t"  /* target->r11 */                   \
+         VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R11                  \
+         "mr 11,%1\n\t"                                           \
+         "mr %0,3\n\t"                                            \
+         "ld  2,-16(11)\n\t" /* restore tocptr */                 \
+         VG_CONTRACT_FRAME_BY(512)                                \
+         : /*out*/   "=r" (_res)                                  \
+         : /*in*/    "r" (&_argvec[2])                            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS          \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_WWW(lval, orig, arg1,arg2,arg3)                 \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[3+3];                        \
+      volatile unsigned long _res;                                \
+      /* _argvec[0] holds current r2 across the call */           \
+      _argvec[1]   = (unsigned long)_orig.r2;                     \
+      _argvec[2]   = (unsigned long)_orig.nraddr;                 \
+      _argvec[2+1] = (unsigned long)arg1;                         \
+      _argvec[2+2] = (unsigned long)arg2;                         \
+      _argvec[2+3] = (unsigned long)arg3;                         \
+      __asm__ volatile(                                           \
+         "mr 11,%1\n\t"                                           \
+         VG_EXPAND_FRAME_BY_trashes_r3(512)                       \
+         "std  2,-16(11)\n\t" /* save tocptr */                   \
+         "ld   2,-8(11)\n\t"  /* use nraddr's tocptr */           \
+         "ld   3, 8(11)\n\t"  /* arg1->r3 */                      \
+         "ld   4, 16(11)\n\t" /* arg2->r4 */                      \
+         "ld   5, 24(11)\n\t" /* arg3->r5 */                      \
+         "ld  11, 0(11)\n\t"  /* target->r11 */                   \
+         VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R11                  \
+         "mr 11,%1\n\t"                                           \
+         "mr %0,3\n\t"                                            \
+         "ld  2,-16(11)\n\t" /* restore tocptr */                 \
+         VG_CONTRACT_FRAME_BY(512)                                \
+         : /*out*/   "=r" (_res)                                  \
+         : /*in*/    "r" (&_argvec[2])                            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS          \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_WWWW(lval, orig, arg1,arg2,arg3,arg4)           \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[3+4];                        \
+      volatile unsigned long _res;                                \
+      /* _argvec[0] holds current r2 across the call */           \
+      _argvec[1]   = (unsigned long)_orig.r2;                     \
+      _argvec[2]   = (unsigned long)_orig.nraddr;                 \
+      _argvec[2+1] = (unsigned long)arg1;                         \
+      _argvec[2+2] = (unsigned long)arg2;                         \
+      _argvec[2+3] = (unsigned long)arg3;                         \
+      _argvec[2+4] = (unsigned long)arg4;                         \
+      __asm__ volatile(                                           \
+         "mr 11,%1\n\t"                                           \
+         VG_EXPAND_FRAME_BY_trashes_r3(512)                       \
+         "std  2,-16(11)\n\t" /* save tocptr */                   \
+         "ld   2,-8(11)\n\t"  /* use nraddr's tocptr */           \
+         "ld   3, 8(11)\n\t"  /* arg1->r3 */                      \
+         "ld   4, 16(11)\n\t" /* arg2->r4 */                      \
+         "ld   5, 24(11)\n\t" /* arg3->r5 */                      \
+         "ld   6, 32(11)\n\t" /* arg4->r6 */                      \
+         "ld  11, 0(11)\n\t"  /* target->r11 */                   \
+         VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R11                  \
+         "mr 11,%1\n\t"                                           \
+         "mr %0,3\n\t"                                            \
+         "ld  2,-16(11)\n\t" /* restore tocptr */                 \
+         VG_CONTRACT_FRAME_BY(512)                                \
+         : /*out*/   "=r" (_res)                                  \
+         : /*in*/    "r" (&_argvec[2])                            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS          \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_5W(lval, orig, arg1,arg2,arg3,arg4,arg5)        \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[3+5];                        \
+      volatile unsigned long _res;                                \
+      /* _argvec[0] holds current r2 across the call */           \
+      _argvec[1]   = (unsigned long)_orig.r2;                     \
+      _argvec[2]   = (unsigned long)_orig.nraddr;                 \
+      _argvec[2+1] = (unsigned long)arg1;                         \
+      _argvec[2+2] = (unsigned long)arg2;                         \
+      _argvec[2+3] = (unsigned long)arg3;                         \
+      _argvec[2+4] = (unsigned long)arg4;                         \
+      _argvec[2+5] = (unsigned long)arg5;                         \
+      __asm__ volatile(                                           \
+         "mr 11,%1\n\t"                                           \
+         VG_EXPAND_FRAME_BY_trashes_r3(512)                       \
+         "std  2,-16(11)\n\t" /* save tocptr */                   \
+         "ld   2,-8(11)\n\t"  /* use nraddr's tocptr */           \
+         "ld   3, 8(11)\n\t"  /* arg1->r3 */                      \
+         "ld   4, 16(11)\n\t" /* arg2->r4 */                      \
+         "ld   5, 24(11)\n\t" /* arg3->r5 */                      \
+         "ld   6, 32(11)\n\t" /* arg4->r6 */                      \
+         "ld   7, 40(11)\n\t" /* arg5->r7 */                      \
+         "ld  11, 0(11)\n\t"  /* target->r11 */                   \
+         VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R11                  \
+         "mr 11,%1\n\t"                                           \
+         "mr %0,3\n\t"                                            \
+         "ld  2,-16(11)\n\t" /* restore tocptr */                 \
+         VG_CONTRACT_FRAME_BY(512)                                \
+         : /*out*/   "=r" (_res)                                  \
+         : /*in*/    "r" (&_argvec[2])                            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS          \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_6W(lval, orig, arg1,arg2,arg3,arg4,arg5,arg6)   \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[3+6];                        \
+      volatile unsigned long _res;                                \
+      /* _argvec[0] holds current r2 across the call */           \
+      _argvec[1]   = (unsigned long)_orig.r2;                     \
+      _argvec[2]   = (unsigned long)_orig.nraddr;                 \
+      _argvec[2+1] = (unsigned long)arg1;                         \
+      _argvec[2+2] = (unsigned long)arg2;                         \
+      _argvec[2+3] = (unsigned long)arg3;                         \
+      _argvec[2+4] = (unsigned long)arg4;                         \
+      _argvec[2+5] = (unsigned long)arg5;                         \
+      _argvec[2+6] = (unsigned long)arg6;                         \
+      __asm__ volatile(                                           \
+         "mr 11,%1\n\t"                                           \
+         VG_EXPAND_FRAME_BY_trashes_r3(512)                       \
+         "std  2,-16(11)\n\t" /* save tocptr */                   \
+         "ld   2,-8(11)\n\t"  /* use nraddr's tocptr */           \
+         "ld   3, 8(11)\n\t"  /* arg1->r3 */                      \
+         "ld   4, 16(11)\n\t" /* arg2->r4 */                      \
+         "ld   5, 24(11)\n\t" /* arg3->r5 */                      \
+         "ld   6, 32(11)\n\t" /* arg4->r6 */                      \
+         "ld   7, 40(11)\n\t" /* arg5->r7 */                      \
+         "ld   8, 48(11)\n\t" /* arg6->r8 */                      \
+         "ld  11, 0(11)\n\t"  /* target->r11 */                   \
+         VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R11                  \
+         "mr 11,%1\n\t"                                           \
+         "mr %0,3\n\t"                                            \
+         "ld  2,-16(11)\n\t" /* restore tocptr */                 \
+         VG_CONTRACT_FRAME_BY(512)                                \
+         : /*out*/   "=r" (_res)                                  \
+         : /*in*/    "r" (&_argvec[2])                            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS          \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_7W(lval, orig, arg1,arg2,arg3,arg4,arg5,arg6,   \
+                                 arg7)                            \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[3+7];                        \
+      volatile unsigned long _res;                                \
+      /* _argvec[0] holds current r2 across the call */           \
+      _argvec[1]   = (unsigned long)_orig.r2;                     \
+      _argvec[2]   = (unsigned long)_orig.nraddr;                 \
+      _argvec[2+1] = (unsigned long)arg1;                         \
+      _argvec[2+2] = (unsigned long)arg2;                         \
+      _argvec[2+3] = (unsigned long)arg3;                         \
+      _argvec[2+4] = (unsigned long)arg4;                         \
+      _argvec[2+5] = (unsigned long)arg5;                         \
+      _argvec[2+6] = (unsigned long)arg6;                         \
+      _argvec[2+7] = (unsigned long)arg7;                         \
+      __asm__ volatile(                                           \
+         "mr 11,%1\n\t"                                           \
+         VG_EXPAND_FRAME_BY_trashes_r3(512)                       \
+         "std  2,-16(11)\n\t" /* save tocptr */                   \
+         "ld   2,-8(11)\n\t"  /* use nraddr's tocptr */           \
+         "ld   3, 8(11)\n\t"  /* arg1->r3 */                      \
+         "ld   4, 16(11)\n\t" /* arg2->r4 */                      \
+         "ld   5, 24(11)\n\t" /* arg3->r5 */                      \
+         "ld   6, 32(11)\n\t" /* arg4->r6 */                      \
+         "ld   7, 40(11)\n\t" /* arg5->r7 */                      \
+         "ld   8, 48(11)\n\t" /* arg6->r8 */                      \
+         "ld   9, 56(11)\n\t" /* arg7->r9 */                      \
+         "ld  11, 0(11)\n\t"  /* target->r11 */                   \
+         VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R11                  \
+         "mr 11,%1\n\t"                                           \
+         "mr %0,3\n\t"                                            \
+         "ld  2,-16(11)\n\t" /* restore tocptr */                 \
+         VG_CONTRACT_FRAME_BY(512)                                \
+         : /*out*/   "=r" (_res)                                  \
+         : /*in*/    "r" (&_argvec[2])                            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS          \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_8W(lval, orig, arg1,arg2,arg3,arg4,arg5,arg6,   \
+                                 arg7,arg8)                       \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[3+8];                        \
+      volatile unsigned long _res;                                \
+      /* _argvec[0] holds current r2 across the call */           \
+      _argvec[1]   = (unsigned long)_orig.r2;                     \
+      _argvec[2]   = (unsigned long)_orig.nraddr;                 \
+      _argvec[2+1] = (unsigned long)arg1;                         \
+      _argvec[2+2] = (unsigned long)arg2;                         \
+      _argvec[2+3] = (unsigned long)arg3;                         \
+      _argvec[2+4] = (unsigned long)arg4;                         \
+      _argvec[2+5] = (unsigned long)arg5;                         \
+      _argvec[2+6] = (unsigned long)arg6;                         \
+      _argvec[2+7] = (unsigned long)arg7;                         \
+      _argvec[2+8] = (unsigned long)arg8;                         \
+      __asm__ volatile(                                           \
+         "mr 11,%1\n\t"                                           \
+         VG_EXPAND_FRAME_BY_trashes_r3(512)                       \
+         "std  2,-16(11)\n\t" /* save tocptr */                   \
+         "ld   2,-8(11)\n\t"  /* use nraddr's tocptr */           \
+         "ld   3, 8(11)\n\t"  /* arg1->r3 */                      \
+         "ld   4, 16(11)\n\t" /* arg2->r4 */                      \
+         "ld   5, 24(11)\n\t" /* arg3->r5 */                      \
+         "ld   6, 32(11)\n\t" /* arg4->r6 */                      \
+         "ld   7, 40(11)\n\t" /* arg5->r7 */                      \
+         "ld   8, 48(11)\n\t" /* arg6->r8 */                      \
+         "ld   9, 56(11)\n\t" /* arg7->r9 */                      \
+         "ld  10, 64(11)\n\t" /* arg8->r10 */                     \
+         "ld  11, 0(11)\n\t"  /* target->r11 */                   \
+         VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R11                  \
+         "mr 11,%1\n\t"                                           \
+         "mr %0,3\n\t"                                            \
+         "ld  2,-16(11)\n\t" /* restore tocptr */                 \
+         VG_CONTRACT_FRAME_BY(512)                                \
+         : /*out*/   "=r" (_res)                                  \
+         : /*in*/    "r" (&_argvec[2])                            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS          \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_9W(lval, orig, arg1,arg2,arg3,arg4,arg5,arg6,   \
+                                 arg7,arg8,arg9)                  \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[3+9];                        \
+      volatile unsigned long _res;                                \
+      /* _argvec[0] holds current r2 across the call */           \
+      _argvec[1]   = (unsigned long)_orig.r2;                     \
+      _argvec[2]   = (unsigned long)_orig.nraddr;                 \
+      _argvec[2+1] = (unsigned long)arg1;                         \
+      _argvec[2+2] = (unsigned long)arg2;                         \
+      _argvec[2+3] = (unsigned long)arg3;                         \
+      _argvec[2+4] = (unsigned long)arg4;                         \
+      _argvec[2+5] = (unsigned long)arg5;                         \
+      _argvec[2+6] = (unsigned long)arg6;                         \
+      _argvec[2+7] = (unsigned long)arg7;                         \
+      _argvec[2+8] = (unsigned long)arg8;                         \
+      _argvec[2+9] = (unsigned long)arg9;                         \
+      __asm__ volatile(                                           \
+         "mr 11,%1\n\t"                                           \
+         VG_EXPAND_FRAME_BY_trashes_r3(512)                       \
+         "std  2,-16(11)\n\t" /* save tocptr */                   \
+         "ld   2,-8(11)\n\t"  /* use nraddr's tocptr */           \
+         VG_EXPAND_FRAME_BY_trashes_r3(128)                       \
+         /* arg9 */                                               \
+         "ld  3,72(11)\n\t"                                       \
+         "std 3,112(1)\n\t"                                       \
+         /* args1-8 */                                            \
+         "ld   3, 8(11)\n\t"  /* arg1->r3 */                      \
+         "ld   4, 16(11)\n\t" /* arg2->r4 */                      \
+         "ld   5, 24(11)\n\t" /* arg3->r5 */                      \
+         "ld   6, 32(11)\n\t" /* arg4->r6 */                      \
+         "ld   7, 40(11)\n\t" /* arg5->r7 */                      \
+         "ld   8, 48(11)\n\t" /* arg6->r8 */                      \
+         "ld   9, 56(11)\n\t" /* arg7->r9 */                      \
+         "ld  10, 64(11)\n\t" /* arg8->r10 */                     \
+         "ld  11, 0(11)\n\t"  /* target->r11 */                   \
+         VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R11                  \
+         "mr 11,%1\n\t"                                           \
+         "mr %0,3\n\t"                                            \
+         "ld  2,-16(11)\n\t" /* restore tocptr */                 \
+         VG_CONTRACT_FRAME_BY(128)                                \
+         VG_CONTRACT_FRAME_BY(512)                                \
+         : /*out*/   "=r" (_res)                                  \
+         : /*in*/    "r" (&_argvec[2])                            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS          \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_10W(lval, orig, arg1,arg2,arg3,arg4,arg5,arg6,  \
+                                  arg7,arg8,arg9,arg10)           \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[3+10];                       \
+      volatile unsigned long _res;                                \
+      /* _argvec[0] holds current r2 across the call */           \
+      _argvec[1]   = (unsigned long)_orig.r2;                     \
+      _argvec[2]   = (unsigned long)_orig.nraddr;                 \
+      _argvec[2+1] = (unsigned long)arg1;                         \
+      _argvec[2+2] = (unsigned long)arg2;                         \
+      _argvec[2+3] = (unsigned long)arg3;                         \
+      _argvec[2+4] = (unsigned long)arg4;                         \
+      _argvec[2+5] = (unsigned long)arg5;                         \
+      _argvec[2+6] = (unsigned long)arg6;                         \
+      _argvec[2+7] = (unsigned long)arg7;                         \
+      _argvec[2+8] = (unsigned long)arg8;                         \
+      _argvec[2+9] = (unsigned long)arg9;                         \
+      _argvec[2+10] = (unsigned long)arg10;                       \
+      __asm__ volatile(                                           \
+         "mr 11,%1\n\t"                                           \
+         VG_EXPAND_FRAME_BY_trashes_r3(512)                       \
+         "std  2,-16(11)\n\t" /* save tocptr */                   \
+         "ld   2,-8(11)\n\t"  /* use nraddr's tocptr */           \
+         VG_EXPAND_FRAME_BY_trashes_r3(128)                       \
+         /* arg10 */                                              \
+         "ld  3,80(11)\n\t"                                       \
+         "std 3,120(1)\n\t"                                       \
+         /* arg9 */                                               \
+         "ld  3,72(11)\n\t"                                       \
+         "std 3,112(1)\n\t"                                       \
+         /* args1-8 */                                            \
+         "ld   3, 8(11)\n\t"  /* arg1->r3 */                      \
+         "ld   4, 16(11)\n\t" /* arg2->r4 */                      \
+         "ld   5, 24(11)\n\t" /* arg3->r5 */                      \
+         "ld   6, 32(11)\n\t" /* arg4->r6 */                      \
+         "ld   7, 40(11)\n\t" /* arg5->r7 */                      \
+         "ld   8, 48(11)\n\t" /* arg6->r8 */                      \
+         "ld   9, 56(11)\n\t" /* arg7->r9 */                      \
+         "ld  10, 64(11)\n\t" /* arg8->r10 */                     \
+         "ld  11, 0(11)\n\t"  /* target->r11 */                   \
+         VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R11                  \
+         "mr 11,%1\n\t"                                           \
+         "mr %0,3\n\t"                                            \
+         "ld  2,-16(11)\n\t" /* restore tocptr */                 \
+         VG_CONTRACT_FRAME_BY(128)                                \
+         VG_CONTRACT_FRAME_BY(512)                                \
+         : /*out*/   "=r" (_res)                                  \
+         : /*in*/    "r" (&_argvec[2])                            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS          \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_11W(lval, orig, arg1,arg2,arg3,arg4,arg5,arg6,  \
+                                  arg7,arg8,arg9,arg10,arg11)     \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[3+11];                       \
+      volatile unsigned long _res;                                \
+      /* _argvec[0] holds current r2 across the call */           \
+      _argvec[1]   = (unsigned long)_orig.r2;                     \
+      _argvec[2]   = (unsigned long)_orig.nraddr;                 \
+      _argvec[2+1] = (unsigned long)arg1;                         \
+      _argvec[2+2] = (unsigned long)arg2;                         \
+      _argvec[2+3] = (unsigned long)arg3;                         \
+      _argvec[2+4] = (unsigned long)arg4;                         \
+      _argvec[2+5] = (unsigned long)arg5;                         \
+      _argvec[2+6] = (unsigned long)arg6;                         \
+      _argvec[2+7] = (unsigned long)arg7;                         \
+      _argvec[2+8] = (unsigned long)arg8;                         \
+      _argvec[2+9] = (unsigned long)arg9;                         \
+      _argvec[2+10] = (unsigned long)arg10;                       \
+      _argvec[2+11] = (unsigned long)arg11;                       \
+      __asm__ volatile(                                           \
+         "mr 11,%1\n\t"                                           \
+         VG_EXPAND_FRAME_BY_trashes_r3(512)                       \
+         "std  2,-16(11)\n\t" /* save tocptr */                   \
+         "ld   2,-8(11)\n\t"  /* use nraddr's tocptr */           \
+         VG_EXPAND_FRAME_BY_trashes_r3(144)                       \
+         /* arg11 */                                              \
+         "ld  3,88(11)\n\t"                                       \
+         "std 3,128(1)\n\t"                                       \
+         /* arg10 */                                              \
+         "ld  3,80(11)\n\t"                                       \
+         "std 3,120(1)\n\t"                                       \
+         /* arg9 */                                               \
+         "ld  3,72(11)\n\t"                                       \
+         "std 3,112(1)\n\t"                                       \
+         /* args1-8 */                                            \
+         "ld   3, 8(11)\n\t"  /* arg1->r3 */                      \
+         "ld   4, 16(11)\n\t" /* arg2->r4 */                      \
+         "ld   5, 24(11)\n\t" /* arg3->r5 */                      \
+         "ld   6, 32(11)\n\t" /* arg4->r6 */                      \
+         "ld   7, 40(11)\n\t" /* arg5->r7 */                      \
+         "ld   8, 48(11)\n\t" /* arg6->r8 */                      \
+         "ld   9, 56(11)\n\t" /* arg7->r9 */                      \
+         "ld  10, 64(11)\n\t" /* arg8->r10 */                     \
+         "ld  11, 0(11)\n\t"  /* target->r11 */                   \
+         VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R11                  \
+         "mr 11,%1\n\t"                                           \
+         "mr %0,3\n\t"                                            \
+         "ld  2,-16(11)\n\t" /* restore tocptr */                 \
+         VG_CONTRACT_FRAME_BY(144)                                \
+         VG_CONTRACT_FRAME_BY(512)                                \
+         : /*out*/   "=r" (_res)                                  \
+         : /*in*/    "r" (&_argvec[2])                            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS          \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#define CALL_FN_W_12W(lval, orig, arg1,arg2,arg3,arg4,arg5,arg6,  \
+                                arg7,arg8,arg9,arg10,arg11,arg12) \
+   do {                                                           \
+      volatile OrigFn        _orig = (orig);                      \
+      volatile unsigned long _argvec[3+12];                       \
+      volatile unsigned long _res;                                \
+      /* _argvec[0] holds current r2 across the call */           \
+      _argvec[1]   = (unsigned long)_orig.r2;                     \
+      _argvec[2]   = (unsigned long)_orig.nraddr;                 \
+      _argvec[2+1] = (unsigned long)arg1;                         \
+      _argvec[2+2] = (unsigned long)arg2;                         \
+      _argvec[2+3] = (unsigned long)arg3;                         \
+      _argvec[2+4] = (unsigned long)arg4;                         \
+      _argvec[2+5] = (unsigned long)arg5;                         \
+      _argvec[2+6] = (unsigned long)arg6;                         \
+      _argvec[2+7] = (unsigned long)arg7;                         \
+      _argvec[2+8] = (unsigned long)arg8;                         \
+      _argvec[2+9] = (unsigned long)arg9;                         \
+      _argvec[2+10] = (unsigned long)arg10;                       \
+      _argvec[2+11] = (unsigned long)arg11;                       \
+      _argvec[2+12] = (unsigned long)arg12;                       \
+      __asm__ volatile(                                           \
+         "mr 11,%1\n\t"                                           \
+         VG_EXPAND_FRAME_BY_trashes_r3(512)                       \
+         "std  2,-16(11)\n\t" /* save tocptr */                   \
+         "ld   2,-8(11)\n\t"  /* use nraddr's tocptr */           \
+         VG_EXPAND_FRAME_BY_trashes_r3(144)                       \
+         /* arg12 */                                              \
+         "ld  3,96(11)\n\t"                                       \
+         "std 3,136(1)\n\t"                                       \
+         /* arg11 */                                              \
+         "ld  3,88(11)\n\t"                                       \
+         "std 3,128(1)\n\t"                                       \
+         /* arg10 */                                              \
+         "ld  3,80(11)\n\t"                                       \
+         "std 3,120(1)\n\t"                                       \
+         /* arg9 */                                               \
+         "ld  3,72(11)\n\t"                                       \
+         "std 3,112(1)\n\t"                                       \
+         /* args1-8 */                                            \
+         "ld   3, 8(11)\n\t"  /* arg1->r3 */                      \
+         "ld   4, 16(11)\n\t" /* arg2->r4 */                      \
+         "ld   5, 24(11)\n\t" /* arg3->r5 */                      \
+         "ld   6, 32(11)\n\t" /* arg4->r6 */                      \
+         "ld   7, 40(11)\n\t" /* arg5->r7 */                      \
+         "ld   8, 48(11)\n\t" /* arg6->r8 */                      \
+         "ld   9, 56(11)\n\t" /* arg7->r9 */                      \
+         "ld  10, 64(11)\n\t" /* arg8->r10 */                     \
+         "ld  11, 0(11)\n\t"  /* target->r11 */                   \
+         VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R11                  \
+         "mr 11,%1\n\t"                                           \
+         "mr %0,3\n\t"                                            \
+         "ld  2,-16(11)\n\t" /* restore tocptr */                 \
+         VG_CONTRACT_FRAME_BY(144)                                \
+         VG_CONTRACT_FRAME_BY(512)                                \
+         : /*out*/   "=r" (_res)                                  \
+         : /*in*/    "r" (&_argvec[2])                            \
+         : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS          \
+      );                                                          \
+      lval = (__typeof__(lval)) _res;                             \
+   } while (0)
+
+#endif /* PLAT_ppc64_aix5 */
+
+
+/* ------------------------------------------------------------------ */
+/* ARCHITECTURE INDEPENDENT MACROS for CLIENT REQUESTS.               */
+/*                                                                    */
+/* ------------------------------------------------------------------ */
+
+/* Some request codes.  There are many more of these, but most are not
+   exposed to end-user view.  These are the public ones, all of the
+   form 0x1000 + small_number.
+
+   Core ones are in the range 0x00000000--0x0000ffff.  The non-public
+   ones start at 0x2000.
+*/
+
+/* These macros are used by tools -- they must be public, but don't
+   embed them into other programs. */
+#define VG_USERREQ_TOOL_BASE(a,b) \
+   ((unsigned int)(((a)&0xff) << 24 | ((b)&0xff) << 16))
+#define VG_IS_TOOL_USERREQ(a, b, v) \
+   (VG_USERREQ_TOOL_BASE(a,b) == ((v) & 0xffff0000))
+
+/* !! ABIWARNING !! ABIWARNING !! ABIWARNING !! ABIWARNING !! 
+   This enum comprises an ABI exported by Valgrind to programs
+   which use client requests.  DO NOT CHANGE THE ORDER OF THESE
+   ENTRIES, NOR DELETE ANY -- add new ones at the end. */
+typedef
+   enum { VG_USERREQ__RUNNING_ON_VALGRIND  = 0x1001,
+          VG_USERREQ__DISCARD_TRANSLATIONS = 0x1002,
+
+          /* These allow any function to be called from the simulated
+             CPU but run on the real CPU.  Nb: the first arg passed to
+             the function is always the ThreadId of the running
+             thread!  So CLIENT_CALL0 actually requires a 1 arg
+             function, etc. */
+          VG_USERREQ__CLIENT_CALL0 = 0x1101,
+          VG_USERREQ__CLIENT_CALL1 = 0x1102,
+          VG_USERREQ__CLIENT_CALL2 = 0x1103,
+          VG_USERREQ__CLIENT_CALL3 = 0x1104,
+
+          /* Can be useful in regression testing suites -- eg. can
+             send Valgrind's output to /dev/null and still count
+             errors. */
+          VG_USERREQ__COUNT_ERRORS = 0x1201,
+
+          /* These are useful and can be interpreted by any tool that
+             tracks malloc() et al, by using vg_replace_malloc.c. */
+          VG_USERREQ__MALLOCLIKE_BLOCK = 0x1301,
+          VG_USERREQ__FREELIKE_BLOCK   = 0x1302,
+          /* Memory pool support. */
+          VG_USERREQ__CREATE_MEMPOOL   = 0x1303,
+          VG_USERREQ__DESTROY_MEMPOOL  = 0x1304,
+          VG_USERREQ__MEMPOOL_ALLOC    = 0x1305,
+          VG_USERREQ__MEMPOOL_FREE     = 0x1306,
+          VG_USERREQ__MEMPOOL_TRIM     = 0x1307,
+          VG_USERREQ__MOVE_MEMPOOL     = 0x1308,
+          VG_USERREQ__MEMPOOL_CHANGE   = 0x1309,
+          VG_USERREQ__MEMPOOL_EXISTS   = 0x130a,
+
+          /* Allow printfs to valgrind log. */
+          /* The first two pass the va_list argument by value, which
+             assumes it is the same size as or smaller than a UWord,
+             which generally isn't the case.  Hence are deprecated.
+             The second two pass the vargs by reference and so are
+             immune to this problem. */
+          /* both :: char* fmt, va_list vargs (DEPRECATED) */
+          VG_USERREQ__PRINTF           = 0x1401,
+          VG_USERREQ__PRINTF_BACKTRACE = 0x1402,
+          /* both :: char* fmt, va_list* vargs */
+          VG_USERREQ__PRINTF_VALIST_BY_REF = 0x1403,
+          VG_USERREQ__PRINTF_BACKTRACE_VALIST_BY_REF = 0x1404,
+
+          /* Stack support. */
+          VG_USERREQ__STACK_REGISTER   = 0x1501,
+          VG_USERREQ__STACK_DEREGISTER = 0x1502,
+          VG_USERREQ__STACK_CHANGE     = 0x1503,
+
+          /* Wine support */
+          VG_USERREQ__LOAD_PDB_DEBUGINFO = 0x1601,
+
+          /* Querying of debug info. */
+          VG_USERREQ__MAP_IP_TO_SRCLOC = 0x1701
+   } Vg_ClientRequest;
+
+#if !defined(__GNUC__)
+#  define __extension__ /* */
+#endif
+
+
+/*
+ * VALGRIND_DO_CLIENT_REQUEST_EXPR(): a C expression that invokes a Valgrind
+ * client request and whose value equals the client request result.
+ */
+
+#if defined(NVALGRIND)
+
+#define VALGRIND_DO_CLIENT_REQUEST_EXPR(                               \
+        _zzq_default, _zzq_request,                                    \
+        _zzq_arg1, _zzq_arg2, _zzq_arg3, _zzq_arg4, _zzq_arg5)         \
+   (_zzq_default)
+
+#else /*defined(NVALGRIND)*/
+
+#if defined(_MSC_VER)
+
+#define VALGRIND_DO_CLIENT_REQUEST_EXPR(                                \
+        _zzq_default, _zzq_request,                                     \
+        _zzq_arg1, _zzq_arg2, _zzq_arg3, _zzq_arg4, _zzq_arg5)          \
+   (vg_VALGRIND_DO_CLIENT_REQUEST_EXPR((uintptr_t)(_zzq_default),       \
+        (_zzq_request), (uintptr_t)(_zzq_arg1), (uintptr_t)(_zzq_arg2), \
+        (uintptr_t)(_zzq_arg3), (uintptr_t)(_zzq_arg4),                 \
+        (uintptr_t)(_zzq_arg5)))
+
+static __inline unsigned
+vg_VALGRIND_DO_CLIENT_REQUEST_EXPR(uintptr_t _zzq_default,
+                                   unsigned _zzq_request, uintptr_t _zzq_arg1,
+                                   uintptr_t _zzq_arg2, uintptr_t _zzq_arg3,
+                                   uintptr_t _zzq_arg4, uintptr_t _zzq_arg5)
+{
+    unsigned _zzq_rlval;
+    VALGRIND_DO_CLIENT_REQUEST(_zzq_rlval, _zzq_default, _zzq_request,
+                      _zzq_arg1, _zzq_arg2, _zzq_arg3, _zzq_arg4, _zzq_arg5);
+    return _zzq_rlval;
+}
+
+#else /*defined(_MSC_VER)*/
+
+#define VALGRIND_DO_CLIENT_REQUEST_EXPR(                               \
+        _zzq_default, _zzq_request,                                    \
+        _zzq_arg1, _zzq_arg2, _zzq_arg3, _zzq_arg4, _zzq_arg5)         \
+   (__extension__({unsigned int _zzq_rlval;                            \
+    VALGRIND_DO_CLIENT_REQUEST(_zzq_rlval, _zzq_default, _zzq_request, \
+                _zzq_arg1, _zzq_arg2, _zzq_arg3, _zzq_arg4, _zzq_arg5) \
+    _zzq_rlval;                                                        \
+   }))
+
+#endif /*defined(_MSC_VER)*/
+
+#endif /*defined(NVALGRIND)*/
+
+
+/* Returns the number of Valgrinds this code is running under.  That
+   is, 0 if running natively, 1 if running under Valgrind, 2 if
+   running under Valgrind which is running under another Valgrind,
+   etc. */
+#define RUNNING_ON_VALGRIND                                           \
+    VALGRIND_DO_CLIENT_REQUEST_EXPR(0 /* if not */,                   \
+                                    VG_USERREQ__RUNNING_ON_VALGRIND,  \
+                                    0, 0, 0, 0, 0)                    \
+
+
+/* Discard translation of code in the range [_qzz_addr .. _qzz_addr +
+   _qzz_len - 1].  Useful if you are debugging a JITter or some such,
+   since it provides a way to make sure valgrind will retranslate the
+   invalidated area.  Returns no value. */
+#define VALGRIND_DISCARD_TRANSLATIONS(_qzz_addr,_qzz_len)         \
+   {unsigned int _qzz_res;                                        \
+    VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0,                       \
+                               VG_USERREQ__DISCARD_TRANSLATIONS,  \
+                               _qzz_addr, _qzz_len, 0, 0, 0);     \
+   }
+
+
+/* These requests are for getting Valgrind itself to print something.
+   Possibly with a backtrace.  This is a really ugly hack.  The return value
+   is the number of characters printed, excluding the "**<pid>** " part at the
+   start and the backtrace (if present). */
+
+#if defined(NVALGRIND)
+
+/* In Exim the following two lines have been changed from the original
+   version for portability to C89 compilers that don't support variable
+   argument macros. We don't use these macros so it doesn't matter much what
+   we do with them, but the following will work OK in most situations though
+   it may cause complaints about expressions without side-effects. */
+#  define VALGRIND_PRINTF (void)
+#  define VALGRIND_PRINTF_BACKTRACE (void)
+
+#else /* NVALGRIND */
+
+#if !defined(_MSC_VER)
+/* Modern GCC will optimize the static routine out if unused,
+   and unused attribute will shut down warnings about it.  */
+static int VALGRIND_PRINTF(const char *format, ...)
+   __attribute__((format(__printf__, 1, 2), __unused__));
+#endif
+static int
+#if defined(_MSC_VER)
+__inline
+#endif
+VALGRIND_PRINTF(const char *format, ...)
+{
+   unsigned long _qzz_res;
+   va_list vargs;
+   va_start(vargs, format);
+#if defined(_MSC_VER)
+   VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0,
+                              VG_USERREQ__PRINTF_VALIST_BY_REF,
+                              (uintptr_t)format,
+                              (uintptr_t)&vargs,
+                              0, 0, 0);
+#else
+   VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0,
+                              VG_USERREQ__PRINTF_VALIST_BY_REF,
+                              (unsigned long)format,
+                              (unsigned long)&vargs, 
+                              0, 0, 0);
+#endif
+   va_end(vargs);
+   return (int)_qzz_res;
+}
+
+#if !defined(_MSC_VER)
+static int VALGRIND_PRINTF_BACKTRACE(const char *format, ...)
+   __attribute__((format(__printf__, 1, 2), __unused__));
+#endif
+static int
+#if defined(_MSC_VER)
+__inline
+#endif
+VALGRIND_PRINTF_BACKTRACE(const char *format, ...)
+{
+   unsigned long _qzz_res;
+   va_list vargs;
+   va_start(vargs, format);
+#if defined(_MSC_VER)
+   VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0,
+                              VG_USERREQ__PRINTF_BACKTRACE_VALIST_BY_REF,
+                              (uintptr_t)format,
+                              (uintptr_t)&vargs,
+                              0, 0, 0);
+#else
+   VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0,
+                              VG_USERREQ__PRINTF_BACKTRACE_VALIST_BY_REF,
+                              (unsigned long)format,
+                              (unsigned long)&vargs, 
+                              0, 0, 0);
+#endif
+   va_end(vargs);
+   return (int)_qzz_res;
+}
+
+#endif /* NVALGRIND */
+
+
+/* These requests allow control to move from the simulated CPU to the
+   real CPU, calling an arbitary function.
+   
+   Note that the current ThreadId is inserted as the first argument.
+   So this call:
+
+     VALGRIND_NON_SIMD_CALL2(f, arg1, arg2)
+
+   requires f to have this signature:
+
+     Word f(Word tid, Word arg1, Word arg2)
+
+   where "Word" is a word-sized type.
+
+   Note that these client requests are not entirely reliable.  For example,
+   if you call a function with them that subsequently calls printf(),
+   there's a high chance Valgrind will crash.  Generally, your prospects of
+   these working are made higher if the called function does not refer to
+   any global variables, and does not refer to any libc or other functions
+   (printf et al).  Any kind of entanglement with libc or dynamic linking is
+   likely to have a bad outcome, for tricky reasons which we've grappled
+   with a lot in the past.
+*/
+#define VALGRIND_NON_SIMD_CALL0(_qyy_fn)                          \
+   __extension__                                                  \
+   ({unsigned long _qyy_res;                                      \
+    VALGRIND_DO_CLIENT_REQUEST(_qyy_res, 0 /* default return */,  \
+                               VG_USERREQ__CLIENT_CALL0,          \
+                               _qyy_fn,                           \
+                               0, 0, 0, 0);                       \
+    _qyy_res;                                                     \
+   })
+
+#define VALGRIND_NON_SIMD_CALL1(_qyy_fn, _qyy_arg1)               \
+   __extension__                                                  \
+   ({unsigned long _qyy_res;                                      \
+    VALGRIND_DO_CLIENT_REQUEST(_qyy_res, 0 /* default return */,  \
+                               VG_USERREQ__CLIENT_CALL1,          \
+                               _qyy_fn,                           \
+                               _qyy_arg1, 0, 0, 0);               \
+    _qyy_res;                                                     \
+   })
+
+#define VALGRIND_NON_SIMD_CALL2(_qyy_fn, _qyy_arg1, _qyy_arg2)    \
+   __extension__                                                  \
+   ({unsigned long _qyy_res;                                      \
+    VALGRIND_DO_CLIENT_REQUEST(_qyy_res, 0 /* default return */,  \
+                               VG_USERREQ__CLIENT_CALL2,          \
+                               _qyy_fn,                           \
+                               _qyy_arg1, _qyy_arg2, 0, 0);       \
+    _qyy_res;                                                     \
+   })
+
+#define VALGRIND_NON_SIMD_CALL3(_qyy_fn, _qyy_arg1, _qyy_arg2, _qyy_arg3) \
+   __extension__                                                  \
+   ({unsigned long _qyy_res;                                      \
+    VALGRIND_DO_CLIENT_REQUEST(_qyy_res, 0 /* default return */,  \
+                               VG_USERREQ__CLIENT_CALL3,          \
+                               _qyy_fn,                           \
+                               _qyy_arg1, _qyy_arg2,              \
+                               _qyy_arg3, 0);                     \
+    _qyy_res;                                                     \
+   })
+
+
+/* Counts the number of errors that have been recorded by a tool.  Nb:
+   the tool must record the errors with VG_(maybe_record_error)() or
+   VG_(unique_error)() for them to be counted. */
+#define VALGRIND_COUNT_ERRORS                                     \
+   __extension__                                                  \
+   ({unsigned int _qyy_res;                                       \
+    VALGRIND_DO_CLIENT_REQUEST(_qyy_res, 0 /* default return */,  \
+                               VG_USERREQ__COUNT_ERRORS,          \
+                               0, 0, 0, 0, 0);                    \
+    _qyy_res;                                                     \
+   })
+
+/* Several Valgrind tools (Memcheck, Massif, Helgrind, DRD) rely on knowing
+   when heap blocks are allocated in order to give accurate results.  This
+   happens automatically for the standard allocator functions such as
+   malloc(), calloc(), realloc(), memalign(), new, new[], free(), delete,
+   delete[], etc.
+
+   But if your program uses a custom allocator, this doesn't automatically
+   happen, and Valgrind will not do as well.  For example, if you allocate
+   superblocks with mmap() and then allocates chunks of the superblocks, all
+   Valgrind's observations will be at the mmap() level and it won't know that
+   the chunks should be considered separate entities.  In Memcheck's case,
+   that means you probably won't get heap block overrun detection (because
+   there won't be redzones marked as unaddressable) and you definitely won't
+   get any leak detection.
+
+   The following client requests allow a custom allocator to be annotated so
+   that it can be handled accurately by Valgrind.
+
+   VALGRIND_MALLOCLIKE_BLOCK marks a region of memory as having been allocated
+   by a malloc()-like function.  For Memcheck (an illustrative case), this
+   does two things:
+
+   - It records that the block has been allocated.  This means any addresses
+     within the block mentioned in error messages will be
+     identified as belonging to the block.  It also means that if the block
+     isn't freed it will be detected by the leak checker.
+
+   - It marks the block as being addressable and undefined (if 'is_zeroed' is
+     not set), or addressable and defined (if 'is_zeroed' is set).  This
+     controls how accesses to the block by the program are handled.
+   
+   'addr' is the start of the usable block (ie. after any
+   redzone), 'sizeB' is its size.  'rzB' is the redzone size if the allocator
+   can apply redzones -- these are blocks of padding at the start and end of
+   each block.  Adding redzones is recommended as it makes it much more likely
+   Valgrind will spot block overruns.  `is_zeroed' indicates if the memory is
+   zeroed (or filled with another predictable value), as is the case for
+   calloc().
+   
+   VALGRIND_MALLOCLIKE_BLOCK should be put immediately after the point where a
+   heap block -- that will be used by the client program -- is allocated.
+   It's best to put it at the outermost level of the allocator if possible;
+   for example, if you have a function my_alloc() which calls
+   internal_alloc(), and the client request is put inside internal_alloc(),
+   stack traces relating to the heap block will contain entries for both
+   my_alloc() and internal_alloc(), which is probably not what you want.
+
+   For Memcheck users: if you use VALGRIND_MALLOCLIKE_BLOCK to carve out
+   custom blocks from within a heap block, B, that has been allocated with
+   malloc/calloc/new/etc, then block B will be *ignored* during leak-checking
+   -- the custom blocks will take precedence.
+
+   VALGRIND_FREELIKE_BLOCK is the partner to VALGRIND_MALLOCLIKE_BLOCK.  For
+   Memcheck, it does two things:
+
+   - It records that the block has been deallocated.  This assumes that the
+     block was annotated as having been allocated via
+     VALGRIND_MALLOCLIKE_BLOCK.  Otherwise, an error will be issued.
+
+   - It marks the block as being unaddressable.
+
+   VALGRIND_FREELIKE_BLOCK should be put immediately after the point where a
+   heap block is deallocated.
+
+   In many cases, these two client requests will not be enough to get your
+   allocator working well with Memcheck.  More specifically, if your allocator
+   writes to freed blocks in any way then a VALGRIND_MAKE_MEM_UNDEFINED call
+   will be necessary to mark the memory as addressable just before the zeroing
+   occurs, otherwise you'll get a lot of invalid write errors.  For example,
+   you'll need to do this if your allocator recycles freed blocks, but it
+   zeroes them before handing them back out (via VALGRIND_MALLOCLIKE_BLOCK).
+   Alternatively, if your allocator reuses freed blocks for allocator-internal
+   data structures, VALGRIND_MAKE_MEM_UNDEFINED calls will also be necessary.
+
+   Really, what's happening is a blurring of the lines between the client
+   program and the allocator... after VALGRIND_FREELIKE_BLOCK is called, the
+   memory should be considered unaddressable to the client program, but the
+   allocator knows more than the rest of the client program and so may be able
+   to safely access it.  Extra client requests are necessary for Valgrind to
+   understand the distinction between the allocator and the rest of the
+   program.
+
+   Note: there is currently no VALGRIND_REALLOCLIKE_BLOCK client request;  it
+   has to be emulated with MALLOCLIKE/FREELIKE and memory copying.
+   
+   Ignored if addr == 0.
+*/
+#define VALGRIND_MALLOCLIKE_BLOCK(addr, sizeB, rzB, is_zeroed)    \
+   {unsigned int _qzz_res;                                        \
+    VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0,                       \
+                               VG_USERREQ__MALLOCLIKE_BLOCK,      \
+                               addr, sizeB, rzB, is_zeroed, 0);   \
+   }
+
+/* See the comment for VALGRIND_MALLOCLIKE_BLOCK for details.
+   Ignored if addr == 0.
+*/
+#define VALGRIND_FREELIKE_BLOCK(addr, rzB)                        \
+   {unsigned int _qzz_res;                                        \
+    VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0,                       \
+                               VG_USERREQ__FREELIKE_BLOCK,        \
+                               addr, rzB, 0, 0, 0);               \
+   }
+
+/* Create a memory pool. */
+#define VALGRIND_CREATE_MEMPOOL(pool, rzB, is_zeroed)             \
+   {unsigned int _qzz_res;                                        \
+    VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0,                       \
+                               VG_USERREQ__CREATE_MEMPOOL,        \
+                               pool, rzB, is_zeroed, 0, 0);       \
+   }
+
+/* Destroy a memory pool. */
+#define VALGRIND_DESTROY_MEMPOOL(pool)                            \
+   {unsigned int _qzz_res;                                        \
+    VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0,                       \
+                               VG_USERREQ__DESTROY_MEMPOOL,       \
+                               pool, 0, 0, 0, 0);                 \
+   }
+
+/* Associate a piece of memory with a memory pool. */
+#define VALGRIND_MEMPOOL_ALLOC(pool, addr, size)                  \
+   {unsigned int _qzz_res;                                        \
+    VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0,                       \
+                               VG_USERREQ__MEMPOOL_ALLOC,         \
+                               pool, addr, size, 0, 0);           \
+   }
+
+/* Disassociate a piece of memory from a memory pool. */
+#define VALGRIND_MEMPOOL_FREE(pool, addr)                         \
+   {unsigned int _qzz_res;                                        \
+    VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0,                       \
+                               VG_USERREQ__MEMPOOL_FREE,          \
+                               pool, addr, 0, 0, 0);              \
+   }
+
+/* Disassociate any pieces outside a particular range. */
+#define VALGRIND_MEMPOOL_TRIM(pool, addr, size)                   \
+   {unsigned int _qzz_res;                                        \
+    VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0,                       \
+                               VG_USERREQ__MEMPOOL_TRIM,          \
+                               pool, addr, size, 0, 0);           \
+   }
+
+/* Resize and/or move a piece associated with a memory pool. */
+#define VALGRIND_MOVE_MEMPOOL(poolA, poolB)                       \
+   {unsigned int _qzz_res;                                        \
+    VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0,                       \
+                               VG_USERREQ__MOVE_MEMPOOL,          \
+                               poolA, poolB, 0, 0, 0);            \
+   }
+
+/* Resize and/or move a piece associated with a memory pool. */
+#define VALGRIND_MEMPOOL_CHANGE(pool, addrA, addrB, size)         \
+   {unsigned int _qzz_res;                                        \
+    VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0,                       \
+                               VG_USERREQ__MEMPOOL_CHANGE,        \
+                               pool, addrA, addrB, size, 0);      \
+   }
+
+/* Return 1 if a mempool exists, else 0. */
+#define VALGRIND_MEMPOOL_EXISTS(pool)                             \
+   __extension__                                                  \
+   ({unsigned int _qzz_res;                                       \
+    VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0,                       \
+                               VG_USERREQ__MEMPOOL_EXISTS,        \
+                               pool, 0, 0, 0, 0);                 \
+    _qzz_res;                                                     \
+   })
+
+/* Mark a piece of memory as being a stack. Returns a stack id. */
+#define VALGRIND_STACK_REGISTER(start, end)                       \
+   __extension__                                                  \
+   ({unsigned int _qzz_res;                                       \
+    VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0,                       \
+                               VG_USERREQ__STACK_REGISTER,        \
+                               start, end, 0, 0, 0);              \
+    _qzz_res;                                                     \
+   })
+
+/* Unmark the piece of memory associated with a stack id as being a
+   stack. */
+#define VALGRIND_STACK_DEREGISTER(id)                             \
+   {unsigned int _qzz_res;                                        \
+    VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0,                       \
+                               VG_USERREQ__STACK_DEREGISTER,      \
+                               id, 0, 0, 0, 0);                   \
+   }
+
+/* Change the start and end address of the stack id. */
+#define VALGRIND_STACK_CHANGE(id, start, end)                     \
+   {unsigned int _qzz_res;                                        \
+    VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0,                       \
+                               VG_USERREQ__STACK_CHANGE,          \
+                               id, start, end, 0, 0);             \
+   }
+
+/* Load PDB debug info for Wine PE image_map. */
+#define VALGRIND_LOAD_PDB_DEBUGINFO(fd, ptr, total_size, delta)   \
+   {unsigned int _qzz_res;                                        \
+    VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0,                       \
+                               VG_USERREQ__LOAD_PDB_DEBUGINFO,    \
+                               fd, ptr, total_size, delta, 0);    \
+   }
+
+/* Map a code address to a source file name and line number.  buf64
+   must point to a 64-byte buffer in the caller's address space.  The
+   result will be dumped in there and is guaranteed to be zero
+   terminated.  If no info is found, the first byte is set to zero. */
+#define VALGRIND_MAP_IP_TO_SRCLOC(addr, buf64)                    \
+   {unsigned int _qzz_res;                                        \
+    VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0,                       \
+                               VG_USERREQ__MAP_IP_TO_SRCLOC,      \
+                               addr, buf64, 0, 0, 0);             \
+   }
+
+
+#undef PLAT_x86_linux
+#undef PLAT_amd64_linux
+#undef PLAT_ppc32_linux
+#undef PLAT_ppc64_linux
+#undef PLAT_arm_linux
+#undef PLAT_ppc32_aix5
+#undef PLAT_ppc64_aix5
+
+#endif   /* __VALGRIND_H */
diff --git a/src/verify.c b/src/verify.c
new file mode 100644 (file)
index 0000000..b1b9f29
--- /dev/null
@@ -0,0 +1,3729 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2014 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Functions concerned with verifying things. The original code for callout
+caching was contributed by Kevin Fleming (but I hacked it around a bit). */
+
+
+#include "exim.h"
+#include "transports/smtp.h"
+
+#define CUTTHROUGH_CMD_TIMEOUT  30     /* timeout for cutthrough-routing calls */
+#define CUTTHROUGH_DATA_TIMEOUT 60     /* timeout for cutthrough-routing calls */
+address_item cutthrough_addr;
+static smtp_outblock ctblock;
+uschar ctbuffer[8192];
+
+
+/* Structure for caching DNSBL lookups */
+
+typedef struct dnsbl_cache_block {
+  dns_address *rhs;
+  uschar *text;
+  int rc;
+  BOOL text_set;
+} dnsbl_cache_block;
+
+
+/* Anchor for DNSBL cache */
+
+static tree_node *dnsbl_cache = NULL;
+
+
+/* Bits for match_type in one_check_dnsbl() */
+
+#define MT_NOT 1
+#define MT_ALL 2
+
+
+
+/*************************************************
+*          Retrieve a callout cache record       *
+*************************************************/
+
+/* If a record exists, check whether it has expired.
+
+Arguments:
+  dbm_file          an open hints file
+  key               the record key
+  type              "address" or "domain"
+  positive_expire   expire time for positive records
+  negative_expire   expire time for negative records
+
+Returns:            the cache record if a non-expired one exists, else NULL
+*/
+
+static dbdata_callout_cache *
+get_callout_cache_record(open_db *dbm_file, uschar *key, uschar *type,
+  int positive_expire, int negative_expire)
+{
+BOOL negative;
+int length, expire;
+time_t now;
+dbdata_callout_cache *cache_record;
+
+cache_record = dbfn_read_with_length(dbm_file, key, &length);
+
+if (cache_record == NULL)
+  {
+  HDEBUG(D_verify) debug_printf("callout cache: no %s record found\n", type);
+  return NULL;
+  }
+
+/* We treat a record as "negative" if its result field is not positive, or if
+it is a domain record and the postmaster field is negative. */
+
+negative = cache_record->result != ccache_accept ||
+  (type[0] == 'd' && cache_record->postmaster_result == ccache_reject);
+expire = negative? negative_expire : positive_expire;
+now = time(NULL);
+
+if (now - cache_record->time_stamp > expire)
+  {
+  HDEBUG(D_verify) debug_printf("callout cache: %s record expired\n", type);
+  return NULL;
+  }
+
+/* If this is a non-reject domain record, check for the obsolete format version
+that doesn't have the postmaster and random timestamps, by looking at the
+length. If so, copy it to a new-style block, replicating the record's
+timestamp. Then check the additional timestamps. (There's no point wasting
+effort if connections are rejected.) */
+
+if (type[0] == 'd' && cache_record->result != ccache_reject)
+  {
+  if (length == sizeof(dbdata_callout_cache_obs))
+    {
+    dbdata_callout_cache *new = store_get(sizeof(dbdata_callout_cache));
+    memcpy(new, cache_record, length);
+    new->postmaster_stamp = new->random_stamp = new->time_stamp;
+    cache_record = new;
+    }
+
+  if (now - cache_record->postmaster_stamp > expire)
+    cache_record->postmaster_result = ccache_unknown;
+
+  if (now - cache_record->random_stamp > expire)
+    cache_record->random_result = ccache_unknown;
+  }
+
+HDEBUG(D_verify) debug_printf("callout cache: found %s record\n", type);
+return cache_record;
+}
+
+
+
+/*************************************************
+*      Do callout verification for an address    *
+*************************************************/
+
+/* This function is called from verify_address() when the address has routed to
+a host list, and a callout has been requested. Callouts are expensive; that is
+why a cache is used to improve the efficiency.
+
+Arguments:
+  addr              the address that's been routed
+  host_list         the list of hosts to try
+  tf                the transport feedback block
+
+  ifstring          "interface" option from transport, or NULL
+  portstring        "port" option from transport, or NULL
+  protocolstring    "protocol" option from transport, or NULL
+  callout           the per-command callout timeout
+  callout_overall   the overall callout timeout (if < 0 use 4*callout)
+  callout_connect   the callout connection timeout (if < 0 use callout)
+  options           the verification options - these bits are used:
+                      vopt_is_recipient => this is a recipient address
+                      vopt_callout_no_cache => don't use callout cache
+                      vopt_callout_fullpm => if postmaster check, do full one
+                      vopt_callout_random => do the "random" thing
+                      vopt_callout_recipsender => use real sender for recipient
+                      vopt_callout_recippmaster => use postmaster for recipient
+  se_mailfrom         MAIL FROM address for sender verify; NULL => ""
+  pm_mailfrom         if non-NULL, do the postmaster check with this sender
+
+Returns:            OK/FAIL/DEFER
+*/
+
+static int
+do_callout(address_item *addr, host_item *host_list, transport_feedback *tf,
+  int callout, int callout_overall, int callout_connect, int options,
+  uschar *se_mailfrom, uschar *pm_mailfrom)
+{
+BOOL is_recipient = (options & vopt_is_recipient) != 0;
+BOOL callout_no_cache = (options & vopt_callout_no_cache) != 0;
+BOOL callout_random = (options & vopt_callout_random) != 0;
+
+int yield = OK;
+int old_domain_cache_result = ccache_accept;
+BOOL done = FALSE;
+uschar *address_key;
+uschar *from_address;
+uschar *random_local_part = NULL;
+uschar *save_deliver_domain = deliver_domain;
+uschar **failure_ptr = is_recipient?
+  &recipient_verify_failure : &sender_verify_failure;
+open_db dbblock;
+open_db *dbm_file = NULL;
+dbdata_callout_cache new_domain_record;
+dbdata_callout_cache_address new_address_record;
+host_item *host;
+time_t callout_start_time;
+
+new_domain_record.result = ccache_unknown;
+new_domain_record.postmaster_result = ccache_unknown;
+new_domain_record.random_result = ccache_unknown;
+
+memset(&new_address_record, 0, sizeof(new_address_record));
+
+/* For a recipient callout, the key used for the address cache record must
+include the sender address if we are using the real sender in the callout,
+because that may influence the result of the callout. */
+
+address_key = addr->address;
+from_address = US"";
+
+if (is_recipient)
+  {
+  if ((options & vopt_callout_recipsender) != 0)
+    {
+    address_key = string_sprintf("%s/<%s>", addr->address, sender_address);
+    from_address = sender_address;
+    }
+  else if ((options & vopt_callout_recippmaster) != 0)
+    {
+    address_key = string_sprintf("%s/<postmaster@%s>", addr->address,
+      qualify_domain_sender);
+    from_address = string_sprintf("postmaster@%s", qualify_domain_sender);
+    }
+  }
+
+/* For a sender callout, we must adjust the key if the mailfrom address is not
+empty. */
+
+else
+  {
+  from_address = (se_mailfrom == NULL)? US"" : se_mailfrom;
+  if (from_address[0] != 0)
+    address_key = string_sprintf("%s/<%s>", addr->address, from_address);
+  }
+
+/* Open the callout cache database, it it exists, for reading only at this
+stage, unless caching has been disabled. */
+
+if (callout_no_cache)
+  {
+  HDEBUG(D_verify) debug_printf("callout cache: disabled by no_cache\n");
+  }
+else if ((dbm_file = dbfn_open(US"callout", O_RDWR, &dbblock, FALSE)) == NULL)
+  {
+  HDEBUG(D_verify) debug_printf("callout cache: not available\n");
+  }
+
+/* If a cache database is available see if we can avoid the need to do an
+actual callout by making use of previously-obtained data. */
+
+if (dbm_file != NULL)
+  {
+  dbdata_callout_cache_address *cache_address_record;
+  dbdata_callout_cache *cache_record = get_callout_cache_record(dbm_file,
+    addr->domain, US"domain",
+    callout_cache_domain_positive_expire,
+    callout_cache_domain_negative_expire);
+
+  /* If an unexpired cache record was found for this domain, see if the callout
+  process can be short-circuited. */
+
+  if (cache_record != NULL)
+    {
+    /* In most cases, if an early command (up to and including MAIL FROM:<>)
+    was rejected, there is no point carrying on. The callout fails. However, if
+    we are doing a recipient verification with use_sender or use_postmaster
+    set, a previous failure of MAIL FROM:<> doesn't count, because this time we
+    will be using a non-empty sender. We have to remember this situation so as
+    not to disturb the cached domain value if this whole verification succeeds
+    (we don't want it turning into "accept"). */
+
+    old_domain_cache_result = cache_record->result;
+
+    if (cache_record->result == ccache_reject ||
+         (*from_address == 0 && cache_record->result == ccache_reject_mfnull))
+      {
+      setflag(addr, af_verify_nsfail);
+      HDEBUG(D_verify)
+        debug_printf("callout cache: domain gave initial rejection, or "
+          "does not accept HELO or MAIL FROM:<>\n");
+      setflag(addr, af_verify_nsfail);
+      addr->user_message = US"(result of an earlier callout reused).";
+      yield = FAIL;
+      *failure_ptr = US"mail";
+      goto END_CALLOUT;
+      }
+
+    /* If a previous check on a "random" local part was accepted, we assume
+    that the server does not do any checking on local parts. There is therefore
+    no point in doing the callout, because it will always be successful. If a
+    random check previously failed, arrange not to do it again, but preserve
+    the data in the new record. If a random check is required but hasn't been
+    done, skip the remaining cache processing. */
+
+    if (callout_random) switch(cache_record->random_result)
+      {
+      case ccache_accept:
+      HDEBUG(D_verify)
+        debug_printf("callout cache: domain accepts random addresses\n");
+      goto END_CALLOUT;     /* Default yield is OK */
+
+      case ccache_reject:
+      HDEBUG(D_verify)
+        debug_printf("callout cache: domain rejects random addresses\n");
+      callout_random = FALSE;
+      new_domain_record.random_result = ccache_reject;
+      new_domain_record.random_stamp = cache_record->random_stamp;
+      break;
+
+      default:
+      HDEBUG(D_verify)
+        debug_printf("callout cache: need to check random address handling "
+          "(not cached or cache expired)\n");
+      goto END_CACHE;
+      }
+
+    /* If a postmaster check is requested, but there was a previous failure,
+    there is again no point in carrying on. If a postmaster check is required,
+    but has not been done before, we are going to have to do a callout, so skip
+    remaining cache processing. */
+
+    if (pm_mailfrom != NULL)
+      {
+      if (cache_record->postmaster_result == ccache_reject)
+        {
+        setflag(addr, af_verify_pmfail);
+        HDEBUG(D_verify)
+          debug_printf("callout cache: domain does not accept "
+            "RCPT TO:<postmaster@domain>\n");
+        yield = FAIL;
+        *failure_ptr = US"postmaster";
+        setflag(addr, af_verify_pmfail);
+        addr->user_message = US"(result of earlier verification reused).";
+        goto END_CALLOUT;
+        }
+      if (cache_record->postmaster_result == ccache_unknown)
+        {
+        HDEBUG(D_verify)
+          debug_printf("callout cache: need to check RCPT "
+            "TO:<postmaster@domain> (not cached or cache expired)\n");
+        goto END_CACHE;
+        }
+
+      /* If cache says OK, set pm_mailfrom NULL to prevent a redundant
+      postmaster check if the address itself has to be checked. Also ensure
+      that the value in the cache record is preserved (with its old timestamp).
+      */
+
+      HDEBUG(D_verify) debug_printf("callout cache: domain accepts RCPT "
+        "TO:<postmaster@domain>\n");
+      pm_mailfrom = NULL;
+      new_domain_record.postmaster_result = ccache_accept;
+      new_domain_record.postmaster_stamp = cache_record->postmaster_stamp;
+      }
+    }
+
+  /* We can't give a result based on information about the domain. See if there
+  is an unexpired cache record for this specific address (combined with the
+  sender address if we are doing a recipient callout with a non-empty sender).
+  */
+
+  cache_address_record = (dbdata_callout_cache_address *)
+    get_callout_cache_record(dbm_file,
+      address_key, US"address",
+      callout_cache_positive_expire,
+      callout_cache_negative_expire);
+
+  if (cache_address_record != NULL)
+    {
+    if (cache_address_record->result == ccache_accept)
+      {
+      HDEBUG(D_verify)
+        debug_printf("callout cache: address record is positive\n");
+      }
+    else
+      {
+      HDEBUG(D_verify)
+        debug_printf("callout cache: address record is negative\n");
+      addr->user_message = US"Previous (cached) callout verification failure";
+      *failure_ptr = US"recipient";
+      yield = FAIL;
+      }
+    goto END_CALLOUT;
+    }
+
+  /* Close the cache database while we actually do the callout for real. */
+
+  END_CACHE:
+  dbfn_close(dbm_file);
+  dbm_file = NULL;
+  }
+
+if (!addr->transport)
+  {
+  HDEBUG(D_verify) debug_printf("cannot callout via null transport\n");
+  }
+else if (Ustrcmp(addr->transport->driver_name, "smtp") != 0)
+  log_write(0, LOG_MAIN|LOG_PANIC|LOG_CONFIG_FOR, "callout transport '%s': %s is non-smtp",
+    addr->transport->name, addr->transport->driver_name);
+else
+  {
+  smtp_transport_options_block *ob =
+    (smtp_transport_options_block *)addr->transport->options_block;
+
+  /* The information wasn't available in the cache, so we have to do a real
+  callout and save the result in the cache for next time, unless no_cache is set,
+  or unless we have a previously cached negative random result. If we are to test
+  with a random local part, ensure that such a local part is available. If not,
+  log the fact, but carry on without randomming. */
+
+  if (callout_random && callout_random_local_part != NULL)
+    {
+    random_local_part = expand_string(callout_random_local_part);
+    if (random_local_part == NULL)
+      log_write(0, LOG_MAIN|LOG_PANIC, "failed to expand "
+        "callout_random_local_part: %s", expand_string_message);
+    }
+
+  /* Default the connect and overall callout timeouts if not set, and record the
+  time we are starting so that we can enforce it. */
+
+  if (callout_overall < 0) callout_overall = 4 * callout;
+  if (callout_connect < 0) callout_connect = callout;
+  callout_start_time = time(NULL);
+
+  /* Before doing a real callout, if this is an SMTP connection, flush the SMTP
+  output because a callout might take some time. When PIPELINING is active and
+  there are many recipients, the total time for doing lots of callouts can add up
+  and cause the client to time out. So in this case we forgo the PIPELINING
+  optimization. */
+
+  if (smtp_out != NULL && !disable_callout_flush) mac_smtp_fflush();
+
+  /* Now make connections to the hosts and do real callouts. The list of hosts
+  is passed in as an argument. */
+
+  for (host = host_list; host != NULL && !done; host = host->next)
+    {
+    smtp_inblock inblock;
+    smtp_outblock outblock;
+    int host_af;
+    int port = 25;
+    BOOL send_quit = TRUE;
+    uschar *active_hostname = smtp_active_hostname;
+    BOOL lmtp;
+    BOOL smtps;
+    BOOL esmtp;
+    BOOL suppress_tls = FALSE;
+    uschar *interface = NULL;  /* Outgoing interface to use; NULL => any */
+    uschar inbuffer[4096];
+    uschar outbuffer[1024];
+    uschar responsebuffer[4096];
+
+    clearflag(addr, af_verify_pmfail);  /* postmaster callout flag */
+    clearflag(addr, af_verify_nsfail);  /* null sender callout flag */
+
+    /* Skip this host if we don't have an IP address for it. */
+
+    if (host->address == NULL)
+      {
+      DEBUG(D_verify) debug_printf("no IP address for host name %s: skipping\n",
+        host->name);
+      continue;
+      }
+
+    /* Check the overall callout timeout */
+
+    if (time(NULL) - callout_start_time >= callout_overall)
+      {
+      HDEBUG(D_verify) debug_printf("overall timeout for callout exceeded\n");
+      break;
+      }
+
+    /* Set IPv4 or IPv6 */
+
+    host_af = (Ustrchr(host->address, ':') == NULL)? AF_INET:AF_INET6;
+
+    /* Expand and interpret the interface and port strings. The latter will not
+    be used if there is a host-specific port (e.g. from a manualroute router).
+    This has to be delayed till now, because they may expand differently for
+    different hosts. If there's a failure, log it, but carry on with the
+    defaults. */
+
+    deliver_host = host->name;
+    deliver_host_address = host->address;
+    deliver_domain = addr->domain;
+
+    if (!smtp_get_interface(tf->interface, host_af, addr, NULL, &interface,
+            US"callout") ||
+        !smtp_get_port(tf->port, addr, &port, US"callout"))
+      log_write(0, LOG_MAIN|LOG_PANIC, "<%s>: %s", addr->address,
+        addr->message);
+
+    /* Set HELO string according to the protocol */
+    lmtp= Ustrcmp(tf->protocol, "lmtp") == 0;
+    smtps= Ustrcmp(tf->protocol, "smtps") == 0;
+
+
+    HDEBUG(D_verify) debug_printf("interface=%s port=%d\n", interface, port);
+
+    /* Set up the buffer for reading SMTP response packets. */
+
+    inblock.buffer = inbuffer;
+    inblock.buffersize = sizeof(inbuffer);
+    inblock.ptr = inbuffer;
+    inblock.ptrend = inbuffer;
+
+    /* Set up the buffer for holding SMTP commands while pipelining */
+
+    outblock.buffer = outbuffer;
+    outblock.buffersize = sizeof(outbuffer);
+    outblock.ptr = outbuffer;
+    outblock.cmd_count = 0;
+    outblock.authenticating = FALSE;
+
+    /* Reset the parameters of a TLS session */
+    tls_out.cipher = tls_out.peerdn = NULL;
+
+    /* Connect to the host; on failure, just loop for the next one, but we
+    set the error for the last one. Use the callout_connect timeout. */
+
+    tls_retry_connection:
+
+    inblock.sock = outblock.sock =
+      smtp_connect(host, host_af, port, interface, callout_connect, TRUE, NULL);
+    /* reconsider DSCP here */
+    if (inblock.sock < 0)
+      {
+      addr->message = string_sprintf("could not connect to %s [%s]: %s",
+          host->name, host->address, strerror(errno));
+      deliver_host = deliver_host_address = NULL;
+      deliver_domain = save_deliver_domain;
+      continue;
+      }
+
+    /* Expand the helo_data string to find the host name to use. */
+
+    if (tf->helo_data != NULL)
+      {
+      uschar *s = expand_string(tf->helo_data);
+      if (s == NULL)
+        log_write(0, LOG_MAIN|LOG_PANIC, "<%s>: failed to expand transport's "
+          "helo_data value for callout: %s", addr->address,
+          expand_string_message);
+      else active_hostname = s;
+      }
+
+    /* Wait for initial response, and send HELO. The smtp_write_command()
+    function leaves its command in big_buffer. This is used in error responses.
+    Initialize it in case the connection is rejected. */
+
+    Ustrcpy(big_buffer, "initial connection");
+
+    /* Unless ssl-on-connect, wait for the initial greeting */
+    smtps_redo_greeting:
+
+    #ifdef SUPPORT_TLS
+    if (!smtps || (smtps && tls_out.active >= 0))
+    #endif
+      if (!(done= smtp_read_response(&inblock, responsebuffer, sizeof(responsebuffer), '2', callout)))
+        goto RESPONSE_FAILED;
+
+    /* Not worth checking greeting line for ESMTP support */
+    if (!(esmtp = verify_check_this_host(&(ob->hosts_avoid_esmtp), NULL,
+      host->name, host->address, NULL) != OK))
+      DEBUG(D_transport)
+        debug_printf("not sending EHLO (host matches hosts_avoid_esmtp)\n");
+
+    tls_redo_helo:
+
+    #ifdef SUPPORT_TLS
+    if (smtps  &&  tls_out.active < 0) /* ssl-on-connect, first pass */
+      {
+      tls_offered = TRUE;
+      ob->tls_tempfail_tryclear = FALSE;
+      }
+      else                             /* all other cases */
+    #endif
+
+      { esmtp_retry:
+
+      if (!(done= smtp_write_command(&outblock, FALSE, "%s %s\r\n",
+        !esmtp? "HELO" : lmtp? "LHLO" : "EHLO", active_hostname) >= 0))
+        goto SEND_FAILED;
+      if (!smtp_read_response(&inblock, responsebuffer, sizeof(responsebuffer), '2', callout))
+        {
+       if (errno != 0 || responsebuffer[0] == 0 || lmtp || !esmtp || tls_out.active >= 0)
+         {
+         done= FALSE;
+         goto RESPONSE_FAILED;
+         }
+        #ifdef SUPPORT_TLS
+        tls_offered = FALSE;
+        #endif
+        esmtp = FALSE;
+        goto esmtp_retry;                      /* fallback to HELO */
+        }
+
+      /* Set tls_offered if the response to EHLO specifies support for STARTTLS. */
+      #ifdef SUPPORT_TLS
+      if (esmtp && !suppress_tls &&  tls_out.active < 0)
+        {
+          if (regex_STARTTLS == NULL) regex_STARTTLS =
+           regex_must_compile(US"\\n250[\\s\\-]STARTTLS(\\s|\\n|$)", FALSE, TRUE);
+
+          tls_offered = pcre_exec(regex_STARTTLS, NULL, CS responsebuffer,
+                       Ustrlen(responsebuffer), 0, PCRE_EOPT, NULL, 0) >= 0;
+       }
+      else
+        tls_offered = FALSE;
+      #endif
+      }
+
+    /* If TLS is available on this connection attempt to
+    start up a TLS session, unless the host is in hosts_avoid_tls. If successful,
+    send another EHLO - the server may give a different answer in secure mode. We
+    use a separate buffer for reading the response to STARTTLS so that if it is
+    negative, the original EHLO data is available for subsequent analysis, should
+    the client not be required to use TLS. If the response is bad, copy the buffer
+    for error analysis. */
+
+    #ifdef SUPPORT_TLS
+    if (tls_offered &&
+       verify_check_this_host(&(ob->hosts_avoid_tls), NULL, host->name,
+         host->address, NULL) != OK &&
+       verify_check_this_host(&(ob->hosts_verify_avoid_tls), NULL, host->name,
+         host->address, NULL) != OK
+       )
+      {
+      uschar buffer2[4096];
+      if (  !smtps
+         && !(done= smtp_write_command(&outblock, FALSE, "STARTTLS\r\n") >= 0))
+        goto SEND_FAILED;
+
+      /* If there is an I/O error, transmission of this message is deferred. If
+      there is a temporary rejection of STARRTLS and tls_tempfail_tryclear is
+      false, we also defer. However, if there is a temporary rejection of STARTTLS
+      and tls_tempfail_tryclear is true, or if there is an outright rejection of
+      STARTTLS, we carry on. This means we will try to send the message in clear,
+      unless the host is in hosts_require_tls (tested below). */
+
+      if (!smtps && !smtp_read_response(&inblock, buffer2, sizeof(buffer2), '2',
+                       ob->command_timeout))
+        {
+        if (errno != 0 || buffer2[0] == 0 ||
+               (buffer2[0] == '4' && !ob->tls_tempfail_tryclear))
+       {
+       Ustrncpy(responsebuffer, buffer2, sizeof(responsebuffer));
+       done= FALSE;
+       goto RESPONSE_FAILED;
+       }
+        }
+
+       /* STARTTLS accepted or ssl-on-connect: try to negotiate a TLS session. */
+      else
+        {
+       int oldtimeout = ob->command_timeout;
+       int rc;
+
+       ob->command_timeout = callout;
+        rc = tls_client_start(inblock.sock, host, addr, ob);
+       ob->command_timeout = oldtimeout;
+
+        /* TLS negotiation failed; give an error.  Try in clear on a new connection,
+           if the options permit it for this host. */
+        if (rc != OK)
+          {
+       if (rc == DEFER && ob->tls_tempfail_tryclear && !smtps &&
+          verify_check_this_host(&(ob->hosts_require_tls), NULL, host->name,
+            host->address, NULL) != OK)
+         {
+            (void)close(inblock.sock);
+         log_write(0, LOG_MAIN, "TLS session failure: delivering unencrypted "
+           "to %s [%s] (not in hosts_require_tls)", host->name, host->address);
+         suppress_tls = TRUE;
+         goto tls_retry_connection;
+         }
+       /*save_errno = ERRNO_TLSFAILURE;*/
+       /*message = US"failure while setting up TLS session";*/
+       send_quit = FALSE;
+       done= FALSE;
+       goto TLS_FAILED;
+       }
+
+        /* TLS session is set up.  Copy info for logging. */
+        addr->cipher = tls_out.cipher;
+        addr->peerdn = tls_out.peerdn;
+
+        /* For SMTPS we need to wait for the initial OK response, then do HELO. */
+        if (smtps)
+        goto smtps_redo_greeting;
+
+        /* For STARTTLS we need to redo EHLO */
+        goto tls_redo_helo;
+        }
+      }
+
+    /* If the host is required to use a secure channel, ensure that we have one. */
+    if (tls_out.active < 0)
+      if (verify_check_this_host(&(ob->hosts_require_tls), NULL, host->name,
+       host->address, NULL) == OK)
+        {
+        /*save_errno = ERRNO_TLSREQUIRED;*/
+        log_write(0, LOG_MAIN, "a TLS session is required for %s [%s], but %s",
+          host->name, host->address,
+       tls_offered? "an attempt to start TLS failed" : "the server did not offer TLS support");
+        done= FALSE;
+        goto TLS_FAILED;
+        }
+
+    #endif /*SUPPORT_TLS*/
+
+    done = TRUE; /* so far so good; have response to HELO */
+
+    /*XXX the EHLO response would be analyzed here for IGNOREQUOTA, SIZE, PIPELINING */
+
+    /* For now, transport_filter by cutthrough-delivery is not supported */
+    /* Need proper integration with the proper transport mechanism. */
+    if (cutthrough_delivery)
+      {
+      if (addr->transport->filter_command)
+        {
+        cutthrough_delivery= FALSE;
+        HDEBUG(D_acl|D_v) debug_printf("Cutthrough cancelled by presence of transport filter\n");
+        }
+      #ifndef DISABLE_DKIM
+      if (ob->dkim_domain)
+        {
+        cutthrough_delivery= FALSE;
+        HDEBUG(D_acl|D_v) debug_printf("Cutthrough cancelled by presence of DKIM signing\n");
+        }
+      #endif
+      }
+
+    SEND_FAILED:
+    RESPONSE_FAILED:
+    TLS_FAILED:
+    ;
+    /* Clear down of the TLS, SMTP and TCP layers on error is handled below.  */
+
+    /* Failure to accept HELO is cached; this blocks the whole domain for all
+    senders. I/O errors and defer responses are not cached. */
+
+    if (!done)
+      {
+      *failure_ptr = US"mail";     /* At or before MAIL */
+      if (errno == 0 && responsebuffer[0] == '5')
+        {
+        setflag(addr, af_verify_nsfail);
+        new_domain_record.result = ccache_reject;
+        }
+      }
+
+    /* If we haven't authenticated, but are required to, give up. */
+    /* Try to AUTH */
+
+    else done = smtp_auth(responsebuffer, sizeof(responsebuffer),
+       addr, host, ob, esmtp, &inblock, &outblock) == OK  &&
+
+               /* Copy AUTH info for logging */
+      ( (addr->authenticator = client_authenticator),
+        (addr->auth_id = client_authenticated_id),
+
+    /* Build a mail-AUTH string (re-using responsebuffer for convenience */
+        !smtp_mail_auth_str(responsebuffer, sizeof(responsebuffer), addr, ob)
+      )  &&
+
+      ( (addr->auth_sndr = client_authenticated_sender),
+
+    /* Send the MAIL command */
+        (smtp_write_command(&outblock, FALSE, "MAIL FROM:<%s>%s\r\n",
+          from_address, responsebuffer) >= 0)
+      )  &&
+
+      smtp_read_response(&inblock, responsebuffer, sizeof(responsebuffer),
+        '2', callout);
+
+    deliver_host = deliver_host_address = NULL;
+    deliver_domain = save_deliver_domain;
+
+    /* If the host does not accept MAIL FROM:<>, arrange to cache this
+    information, but again, don't record anything for an I/O error or a defer. Do
+    not cache rejections of MAIL when a non-empty sender has been used, because
+    that blocks the whole domain for all senders. */
+
+    if (!done)
+      {
+      *failure_ptr = US"mail";     /* At or before MAIL */
+      if (errno == 0 && responsebuffer[0] == '5')
+        {
+        setflag(addr, af_verify_nsfail);
+        if (from_address[0] == 0)
+          new_domain_record.result = ccache_reject_mfnull;
+        }
+      }
+
+    /* Otherwise, proceed to check a "random" address (if required), then the
+    given address, and the postmaster address (if required). Between each check,
+    issue RSET, because some servers accept only one recipient after MAIL
+    FROM:<>.
+
+    Before doing this, set the result in the domain cache record to "accept",
+    unless its previous value was ccache_reject_mfnull. In that case, the domain
+    rejects MAIL FROM:<> and we want to continue to remember that. When that is
+    the case, we have got here only in the case of a recipient verification with
+    a non-null sender. */
+
+    else
+      {
+      new_domain_record.result =
+        (old_domain_cache_result == ccache_reject_mfnull)?
+          ccache_reject_mfnull: ccache_accept;
+
+      /* Do the random local part check first */
+
+      if (random_local_part != NULL)
+        {
+        uschar randombuffer[1024];
+        BOOL random_ok =
+          smtp_write_command(&outblock, FALSE,
+            "RCPT TO:<%.1000s@%.1000s>\r\n", random_local_part,
+            addr->domain) >= 0 &&
+          smtp_read_response(&inblock, randombuffer,
+            sizeof(randombuffer), '2', callout);
+
+        /* Remember when we last did a random test */
+
+        new_domain_record.random_stamp = time(NULL);
+
+        /* If accepted, we aren't going to do any further tests below. */
+
+        if (random_ok)
+          {
+          new_domain_record.random_result = ccache_accept;
+          }
+
+        /* Otherwise, cache a real negative response, and get back to the right
+        state to send RCPT. Unless there's some problem such as a dropped
+        connection, we expect to succeed, because the commands succeeded above. */
+
+        else if (errno == 0)
+          {
+          if (randombuffer[0] == '5')
+            new_domain_record.random_result = ccache_reject;
+
+          done =
+            smtp_write_command(&outblock, FALSE, "RSET\r\n") >= 0 &&
+            smtp_read_response(&inblock, responsebuffer, sizeof(responsebuffer),
+              '2', callout) &&
+
+            smtp_write_command(&outblock, FALSE, "MAIL FROM:<%s>\r\n",
+              from_address) >= 0 &&
+            smtp_read_response(&inblock, responsebuffer, sizeof(responsebuffer),
+              '2', callout);
+          }
+        else done = FALSE;    /* Some timeout/connection problem */
+        }                     /* Random check */
+
+      /* If the host is accepting all local parts, as determined by the "random"
+      check, we don't need to waste time doing any further checking. */
+
+      if (new_domain_record.random_result != ccache_accept && done)
+        {
+        /* Get the rcpt_include_affixes flag from the transport if there is one,
+        but assume FALSE if there is not. */
+
+        done =
+          smtp_write_command(&outblock, FALSE, "RCPT TO:<%.1000s>\r\n",
+            transport_rcpt_address(addr,
+              (addr->transport == NULL)? FALSE :
+               addr->transport->rcpt_include_affixes)) >= 0 &&
+          smtp_read_response(&inblock, responsebuffer, sizeof(responsebuffer),
+            '2', callout);
+
+        if (done)
+          new_address_record.result = ccache_accept;
+        else if (errno == 0 && responsebuffer[0] == '5')
+          {
+          *failure_ptr = US"recipient";
+          new_address_record.result = ccache_reject;
+          }
+
+        /* Do postmaster check if requested; if a full check is required, we
+        check for RCPT TO:<postmaster> (no domain) in accordance with RFC 821. */
+
+        if (done && pm_mailfrom != NULL)
+          {
+          /*XXX not suitable for cutthrough - sequencing problems */
+       cutthrough_delivery= FALSE;
+       HDEBUG(D_acl|D_v) debug_printf("Cutthrough cancelled by presence of postmaster verify\n");
+
+          done =
+            smtp_write_command(&outblock, FALSE, "RSET\r\n") >= 0 &&
+            smtp_read_response(&inblock, responsebuffer,
+              sizeof(responsebuffer), '2', callout) &&
+
+            smtp_write_command(&outblock, FALSE,
+              "MAIL FROM:<%s>\r\n", pm_mailfrom) >= 0 &&
+            smtp_read_response(&inblock, responsebuffer,
+              sizeof(responsebuffer), '2', callout) &&
+
+            /* First try using the current domain */
+
+            ((
+            smtp_write_command(&outblock, FALSE,
+              "RCPT TO:<postmaster@%.1000s>\r\n", addr->domain) >= 0 &&
+            smtp_read_response(&inblock, responsebuffer,
+              sizeof(responsebuffer), '2', callout)
+            )
+
+            ||
+
+            /* If that doesn't work, and a full check is requested,
+            try without the domain. */
+
+            (
+            (options & vopt_callout_fullpm) != 0 &&
+            smtp_write_command(&outblock, FALSE,
+              "RCPT TO:<postmaster>\r\n") >= 0 &&
+            smtp_read_response(&inblock, responsebuffer,
+              sizeof(responsebuffer), '2', callout)
+            ));
+
+          /* Sort out the cache record */
+
+          new_domain_record.postmaster_stamp = time(NULL);
+
+          if (done)
+            new_domain_record.postmaster_result = ccache_accept;
+          else if (errno == 0 && responsebuffer[0] == '5')
+            {
+            *failure_ptr = US"postmaster";
+            setflag(addr, af_verify_pmfail);
+            new_domain_record.postmaster_result = ccache_reject;
+            }
+          }
+        }           /* Random not accepted */
+      }             /* MAIL FROM: accepted */
+
+    /* For any failure of the main check, other than a negative response, we just
+    close the connection and carry on. We can identify a negative response by the
+    fact that errno is zero. For I/O errors it will be non-zero
+
+    Set up different error texts for logging and for sending back to the caller
+    as an SMTP response. Log in all cases, using a one-line format. For sender
+    callouts, give a full response to the caller, but for recipient callouts,
+    don't give the IP address because this may be an internal host whose identity
+    is not to be widely broadcast. */
+
+    if (!done)
+      {
+      if (errno == ETIMEDOUT)
+        {
+        HDEBUG(D_verify) debug_printf("SMTP timeout\n");
+        send_quit = FALSE;
+        }
+      else if (errno == 0)
+        {
+        if (*responsebuffer == 0) Ustrcpy(responsebuffer, US"connection dropped");
+
+        addr->message =
+          string_sprintf("response to \"%s\" from %s [%s] was: %s",
+            big_buffer, host->name, host->address,
+            string_printing(responsebuffer));
+
+        addr->user_message = is_recipient?
+          string_sprintf("Callout verification failed:\n%s", responsebuffer)
+          :
+          string_sprintf("Called:   %s\nSent:     %s\nResponse: %s",
+            host->address, big_buffer, responsebuffer);
+
+        /* Hard rejection ends the process */
+
+        if (responsebuffer[0] == '5')   /* Address rejected */
+          {
+          yield = FAIL;
+          done = TRUE;
+          }
+        }
+      }
+
+    /* End the SMTP conversation and close the connection. */
+
+    /* Cutthrough - on a successfull connect and recipient-verify with use-sender
+    and we have no cutthrough conn so far
+    here is where we want to leave the conn open */
+    if (  cutthrough_delivery
+       && done
+       && yield == OK
+       && (options & (vopt_callout_recipsender|vopt_callout_recippmaster)) == vopt_callout_recipsender
+       && !random_local_part
+       && !pm_mailfrom
+       && cutthrough_fd < 0
+       )
+      {
+      cutthrough_fd= outblock.sock;    /* We assume no buffer in use in the outblock */
+      cutthrough_addr = *addr;         /* Save the address_item for later logging */
+      cutthrough_addr.next =     NULL;
+      cutthrough_addr.host_used = store_get(sizeof(host_item));
+      cutthrough_addr.host_used->name =    host->name;
+      cutthrough_addr.host_used->address = host->address;
+      cutthrough_addr.host_used->port =    port;
+      if (addr->parent)
+        *(cutthrough_addr.parent = store_get(sizeof(address_item)))= *addr->parent;
+      ctblock.buffer = ctbuffer;
+      ctblock.buffersize = sizeof(ctbuffer);
+      ctblock.ptr = ctbuffer;
+      /* ctblock.cmd_count = 0; ctblock.authenticating = FALSE; */
+      ctblock.sock = cutthrough_fd;
+      }
+    else
+      {
+      /* Ensure no cutthrough on multiple address verifies */
+      if (options & vopt_callout_recipsender)
+        cancel_cutthrough_connection("multiple verify calls");
+      if (send_quit) (void)smtp_write_command(&outblock, FALSE, "QUIT\r\n");
+
+      #ifdef SUPPORT_TLS
+      tls_close(FALSE, TRUE);
+      #endif
+      (void)close(inblock.sock);
+      }
+
+    }    /* Loop through all hosts, while !done */
+  }
+
+/* If we get here with done == TRUE, a successful callout happened, and yield
+will be set OK or FAIL according to the response to the RCPT command.
+Otherwise, we looped through the hosts but couldn't complete the business.
+However, there may be domain-specific information to cache in both cases.
+
+The value of the result field in the new_domain record is ccache_unknown if
+there was an error before or with MAIL FROM:, and errno was not zero,
+implying some kind of I/O error. We don't want to write the cache in that case.
+Otherwise the value is ccache_accept, ccache_reject, or ccache_reject_mfnull. */
+
+if (!callout_no_cache && new_domain_record.result != ccache_unknown)
+  {
+  if ((dbm_file = dbfn_open(US"callout", O_RDWR|O_CREAT, &dbblock, FALSE))
+       == NULL)
+    {
+    HDEBUG(D_verify) debug_printf("callout cache: not available\n");
+    }
+  else
+    {
+    (void)dbfn_write(dbm_file, addr->domain, &new_domain_record,
+      (int)sizeof(dbdata_callout_cache));
+    HDEBUG(D_verify) debug_printf("wrote callout cache domain record:\n"
+      "  result=%d postmaster=%d random=%d\n",
+      new_domain_record.result,
+      new_domain_record.postmaster_result,
+      new_domain_record.random_result);
+    }
+  }
+
+/* If a definite result was obtained for the callout, cache it unless caching
+is disabled. */
+
+if (done)
+  {
+  if (!callout_no_cache && new_address_record.result != ccache_unknown)
+    {
+    if (dbm_file == NULL)
+      dbm_file = dbfn_open(US"callout", O_RDWR|O_CREAT, &dbblock, FALSE);
+    if (dbm_file == NULL)
+      {
+      HDEBUG(D_verify) debug_printf("no callout cache available\n");
+      }
+    else
+      {
+      (void)dbfn_write(dbm_file, address_key, &new_address_record,
+        (int)sizeof(dbdata_callout_cache_address));
+      HDEBUG(D_verify) debug_printf("wrote %s callout cache address record\n",
+        (new_address_record.result == ccache_accept)? "positive" : "negative");
+      }
+    }
+  }    /* done */
+
+/* Failure to connect to any host, or any response other than 2xx or 5xx is a
+temporary error. If there was only one host, and a response was received, leave
+it alone if supplying details. Otherwise, give a generic response. */
+
+else   /* !done */
+  {
+  uschar *dullmsg = string_sprintf("Could not complete %s verify callout",
+    is_recipient? "recipient" : "sender");
+  yield = DEFER;
+
+  if (host_list->next != NULL || addr->message == NULL) addr->message = dullmsg;
+
+  addr->user_message = (!smtp_return_error_details)? dullmsg :
+    string_sprintf("%s for <%s>.\n"
+      "The mail server(s) for the domain may be temporarily unreachable, or\n"
+      "they may be permanently unreachable from this server. In the latter case,\n%s",
+      dullmsg, addr->address,
+      is_recipient?
+        "the address will never be accepted."
+        :
+        "you need to change the address or create an MX record for its domain\n"
+        "if it is supposed to be generally accessible from the Internet.\n"
+        "Talk to your mail administrator for details.");
+
+  /* Force a specific error code */
+
+  addr->basic_errno = ERRNO_CALLOUTDEFER;
+  }
+
+/* Come here from within the cache-reading code on fast-track exit. */
+
+END_CALLOUT:
+if (dbm_file != NULL) dbfn_close(dbm_file);
+return yield;
+}
+
+
+
+/* Called after recipient-acl to get a cutthrough connection open when
+   one was requested and a recipient-verify wasn't subsequently done.
+*/
+void
+open_cutthrough_connection( address_item * addr )
+{
+address_item addr2;
+
+/* Use a recipient-verify-callout to set up the cutthrough connection. */
+/* We must use a copy of the address for verification, because it might
+get rewritten. */
+
+addr2 = *addr;
+HDEBUG(D_acl) debug_printf("----------- start cutthrough setup ------------\n");
+(void) verify_address(&addr2, NULL,
+       vopt_is_recipient | vopt_callout_recipsender | vopt_callout_no_cache,
+       CUTTHROUGH_CMD_TIMEOUT, -1, -1,
+       NULL, NULL, NULL);
+HDEBUG(D_acl) debug_printf("----------- end cutthrough setup ------------\n");
+return;
+}
+
+
+
+/* Send given number of bytes from the buffer */
+static BOOL
+cutthrough_send(int n)
+{
+if(cutthrough_fd < 0)
+  return TRUE;
+
+if(
+#ifdef SUPPORT_TLS
+   (tls_out.active == cutthrough_fd) ? tls_write(FALSE, ctblock.buffer, n) :
+#endif
+   send(cutthrough_fd, ctblock.buffer, n, 0) > 0
+  )
+{
+  transport_count += n;
+  ctblock.ptr= ctblock.buffer;
+  return TRUE;
+}
+
+HDEBUG(D_transport|D_acl) debug_printf("cutthrough_send failed: %s\n", strerror(errno));
+return FALSE;
+}
+
+
+
+static BOOL
+_cutthrough_puts(uschar * cp, int n)
+{
+while(n--)
+ {
+ if(ctblock.ptr >= ctblock.buffer+ctblock.buffersize)
+   if(!cutthrough_send(ctblock.buffersize))
+     return FALSE;
+
+ *ctblock.ptr++ = *cp++;
+ }
+return TRUE;
+}
+
+/* Buffered output of counted data block.   Return boolean success */
+BOOL
+cutthrough_puts(uschar * cp, int n)
+{
+if (cutthrough_fd < 0)       return TRUE;
+if (_cutthrough_puts(cp, n)) return TRUE;
+cancel_cutthrough_connection("transmit failed");
+return FALSE;
+}
+
+
+static BOOL
+_cutthrough_flush_send( void )
+{
+int n= ctblock.ptr-ctblock.buffer;
+
+if(n>0)
+  if(!cutthrough_send(n))
+    return FALSE;
+return TRUE;
+}
+
+
+/* Send out any bufferred output.  Return boolean success. */
+BOOL
+cutthrough_flush_send( void )
+{
+if (_cutthrough_flush_send()) return TRUE;
+cancel_cutthrough_connection("transmit failed");
+return FALSE;
+}
+
+
+BOOL
+cutthrough_put_nl( void )
+{
+return cutthrough_puts(US"\r\n", 2);
+}
+
+
+/* Get and check response from cutthrough target */
+static uschar
+cutthrough_response(char expect, uschar ** copy)
+{
+smtp_inblock inblock;
+uschar inbuffer[4096];
+uschar responsebuffer[4096];
+
+inblock.buffer = inbuffer;
+inblock.buffersize = sizeof(inbuffer);
+inblock.ptr = inbuffer;
+inblock.ptrend = inbuffer;
+inblock.sock = cutthrough_fd;
+/* this relies on (inblock.sock == tls_out.active) */
+if(!smtp_read_response(&inblock, responsebuffer, sizeof(responsebuffer), expect, CUTTHROUGH_DATA_TIMEOUT))
+  cancel_cutthrough_connection("target timeout on read");
+
+if(copy != NULL)
+  {
+  uschar * cp;
+  *copy= cp= string_copy(responsebuffer);
+  /* Trim the trailing end of line */
+  cp += Ustrlen(responsebuffer);
+  if(cp > *copy  &&  cp[-1] == '\n') *--cp = '\0';
+  if(cp > *copy  &&  cp[-1] == '\r') *--cp = '\0';
+  }
+
+return responsebuffer[0];
+}
+
+
+/* Negotiate dataphase with the cutthrough target, returning success boolean */
+BOOL
+cutthrough_predata( void )
+{
+if(cutthrough_fd < 0)
+  return FALSE;
+
+HDEBUG(D_transport|D_acl|D_v) debug_printf("  SMTP>> DATA\n");
+cutthrough_puts(US"DATA\r\n", 6);
+cutthrough_flush_send();
+
+/* Assume nothing buffered.  If it was it gets ignored. */
+return cutthrough_response('3', NULL) == '3';
+}
+
+
+/* fd and use_crlf args only to match write_chunk() */
+static BOOL
+cutthrough_write_chunk(int fd, uschar * s, int len, BOOL use_crlf)
+{
+uschar * s2;
+while(s && (s2 = Ustrchr(s, '\n')))
+ {
+ if(!cutthrough_puts(s, s2-s) || !cutthrough_put_nl())
+  return FALSE;
+ s = s2+1;
+ }
+return TRUE;
+}
+
+
+/* Buffered send of headers.  Return success boolean. */
+/* Expands newlines to wire format (CR,NL).           */
+/* Also sends header-terminating blank line.          */
+BOOL
+cutthrough_headers_send( void )
+{
+if(cutthrough_fd < 0)
+  return FALSE;
+
+/* We share a routine with the mainline transport to handle header add/remove/rewrites,
+   but having a separate buffered-output function (for now)
+*/
+HDEBUG(D_acl) debug_printf("----------- start cutthrough headers send -----------\n");
+
+if (!transport_headers_send(&cutthrough_addr, cutthrough_fd,
+       cutthrough_addr.transport->add_headers, cutthrough_addr.transport->remove_headers,
+       &cutthrough_write_chunk, TRUE,
+       cutthrough_addr.transport->rewrite_rules, cutthrough_addr.transport->rewrite_existflags))
+  return FALSE;
+
+HDEBUG(D_acl) debug_printf("----------- done cutthrough headers send ------------\n");
+return TRUE;
+}
+
+
+static void
+close_cutthrough_connection( const char * why )
+{
+if(cutthrough_fd >= 0)
+  {
+  /* We could be sending this after a bunch of data, but that is ok as
+     the only way to cancel the transfer in dataphase is to drop the tcp
+     conn before the final dot.
+  */
+  ctblock.ptr = ctbuffer;
+  HDEBUG(D_transport|D_acl|D_v) debug_printf("  SMTP>> QUIT\n");
+  _cutthrough_puts(US"QUIT\r\n", 6);   /* avoid recursion */
+  _cutthrough_flush_send();
+  /* No wait for response */
+
+  #ifdef SUPPORT_TLS
+  tls_close(FALSE, TRUE);
+  #endif
+  (void)close(cutthrough_fd);
+  cutthrough_fd= -1;
+  HDEBUG(D_acl) debug_printf("----------- cutthrough shutdown (%s) ------------\n", why);
+  }
+ctblock.ptr = ctbuffer;
+}
+
+void
+cancel_cutthrough_connection( const char * why )
+{
+close_cutthrough_connection(why);
+cutthrough_delivery= FALSE;
+}
+
+
+
+
+/* Have senders final-dot.  Send one to cutthrough target, and grab the response.
+   Log an OK response as a transmission.
+   Close the connection.
+   Return smtp response-class digit.
+*/
+uschar *
+cutthrough_finaldot( void )
+{
+HDEBUG(D_transport|D_acl|D_v) debug_printf("  SMTP>> .\n");
+
+/* Assume data finshed with new-line */
+if(!cutthrough_puts(US".", 1) || !cutthrough_put_nl() || !cutthrough_flush_send())
+  return cutthrough_addr.message;
+
+switch(cutthrough_response('2', &cutthrough_addr.message))
+  {
+  case '2':
+    delivery_log(LOG_MAIN, &cutthrough_addr, (int)'>', NULL);
+    close_cutthrough_connection("delivered");
+    break;
+
+  case '4':
+    delivery_log(LOG_MAIN, &cutthrough_addr, 0, US"tmp-reject from cutthrough after DATA:");
+    break;
+
+  case '5':
+    delivery_log(LOG_MAIN|LOG_REJECT, &cutthrough_addr, 0, US"rejected after DATA:");
+    break;
+
+  default:
+    break;
+  }
+  return cutthrough_addr.message;
+}
+
+
+
+/*************************************************
+*           Copy error to toplevel address       *
+*************************************************/
+
+/* This function is used when a verify fails or defers, to ensure that the
+failure or defer information is in the original toplevel address. This applies
+when an address is redirected to a single new address, and the failure or
+deferral happens to the child address.
+
+Arguments:
+  vaddr       the verify address item
+  addr        the final address item
+  yield       FAIL or DEFER
+
+Returns:      the value of YIELD
+*/
+
+static int
+copy_error(address_item *vaddr, address_item *addr, int yield)
+{
+if (addr != vaddr)
+  {
+  vaddr->message = addr->message;
+  vaddr->user_message = addr->user_message;
+  vaddr->basic_errno = addr->basic_errno;
+  vaddr->more_errno = addr->more_errno;
+  vaddr->p.address_data = addr->p.address_data;
+  copyflag(vaddr, addr, af_pass_message);
+  }
+return yield;
+}
+
+
+
+
+/**************************************************
+* printf that automatically handles TLS if needed *
+***************************************************/
+
+/* This function is used by verify_address() as a substitute for all fprintf()
+calls; a direct fprintf() will not produce output in a TLS SMTP session, such
+as a response to an EXPN command.  smtp_in.c makes smtp_printf available but
+that assumes that we always use the smtp_out FILE* when not using TLS or the
+ssl buffer when we are.  Instead we take a FILE* parameter and check to see if
+that is smtp_out; if so, smtp_printf() with TLS support, otherwise regular
+fprintf().
+
+Arguments:
+  f           the candidate FILE* to write to
+  format      format string
+  ...         optional arguments
+
+Returns:
+              nothing
+*/
+
+static void PRINTF_FUNCTION(2,3)
+respond_printf(FILE *f, const char *format, ...)
+{
+va_list ap;
+
+va_start(ap, format);
+if (smtp_out && (f == smtp_out))
+  smtp_vprintf(format, ap);
+else
+  vfprintf(f, format, ap);
+va_end(ap);
+}
+
+
+
+/*************************************************
+*            Verify an email address             *
+*************************************************/
+
+/* This function is used both for verification (-bv and at other times) and
+address testing (-bt), which is indicated by address_test_mode being set.
+
+Arguments:
+  vaddr            contains the address to verify; the next field in this block
+                     must be NULL
+  f                if not NULL, write the result to this file
+  options          various option bits:
+                     vopt_fake_sender => this sender verify is not for the real
+                       sender (it was verify=sender=xxxx or an address from a
+                       header line) - rewriting must not change sender_address
+                     vopt_is_recipient => this is a recipient address, otherwise
+                       it's a sender address - this affects qualification and
+                       rewriting and messages from callouts
+                     vopt_qualify => qualify an unqualified address; else error
+                     vopt_expn => called from SMTP EXPN command
+                     vopt_success_on_redirect => when a new address is generated
+                       the verification instantly succeeds
+
+                     These ones are used by do_callout() -- the options variable
+                       is passed to it.
+
+                     vopt_callout_fullpm => if postmaster check, do full one
+                     vopt_callout_no_cache => don't use callout cache
+                     vopt_callout_random => do the "random" thing
+                     vopt_callout_recipsender => use real sender for recipient
+                     vopt_callout_recippmaster => use postmaster for recipient
+
+  callout          if > 0, specifies that callout is required, and gives timeout
+                     for individual commands
+  callout_overall  if > 0, gives overall timeout for the callout function;
+                   if < 0, a default is used (see do_callout())
+  callout_connect  the connection timeout for callouts
+  se_mailfrom      when callout is requested to verify a sender, use this
+                     in MAIL FROM; NULL => ""
+  pm_mailfrom      when callout is requested, if non-NULL, do the postmaster
+                     thing and use this as the sender address (may be "")
+
+  routed           if not NULL, set TRUE if routing succeeded, so we can
+                     distinguish between routing failed and callout failed
+
+Returns:           OK      address verified
+                   FAIL    address failed to verify
+                   DEFER   can't tell at present
+*/
+
+int
+verify_address(address_item *vaddr, FILE *f, int options, int callout,
+  int callout_overall, int callout_connect, uschar *se_mailfrom,
+  uschar *pm_mailfrom, BOOL *routed)
+{
+BOOL allok = TRUE;
+BOOL full_info = (f == NULL)? FALSE : (debug_selector != 0);
+BOOL is_recipient = (options & vopt_is_recipient) != 0;
+BOOL expn         = (options & vopt_expn) != 0;
+BOOL success_on_redirect = (options & vopt_success_on_redirect) != 0;
+int i;
+int yield = OK;
+int verify_type = expn? v_expn :
+     address_test_mode? v_none :
+          is_recipient? v_recipient : v_sender;
+address_item *addr_list;
+address_item *addr_new = NULL;
+address_item *addr_remote = NULL;
+address_item *addr_local = NULL;
+address_item *addr_succeed = NULL;
+uschar **failure_ptr = is_recipient?
+  &recipient_verify_failure : &sender_verify_failure;
+uschar *ko_prefix, *cr;
+uschar *address = vaddr->address;
+uschar *save_sender;
+uschar null_sender[] = { 0 };             /* Ensure writeable memory */
+
+/* Clear, just in case */
+
+*failure_ptr = NULL;
+
+/* Set up a prefix and suffix for error message which allow us to use the same
+output statements both in EXPN mode (where an SMTP response is needed) and when
+debugging with an output file. */
+
+if (expn)
+  {
+  ko_prefix = US"553 ";
+  cr = US"\r";
+  }
+else ko_prefix = cr = US"";
+
+/* Add qualify domain if permitted; otherwise an unqualified address fails. */
+
+if (parse_find_at(address) == NULL)
+  {
+  if ((options & vopt_qualify) == 0)
+    {
+    if (f != NULL)
+      respond_printf(f, "%sA domain is required for \"%s\"%s\n",
+        ko_prefix, address, cr);
+    *failure_ptr = US"qualify";
+    return FAIL;
+    }
+  address = rewrite_address_qualify(address, is_recipient);
+  }
+
+DEBUG(D_verify)
+  {
+  debug_printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
+  debug_printf("%s %s\n", address_test_mode? "Testing" : "Verifying", address);
+  }
+
+/* Rewrite and report on it. Clear the domain and local part caches - these
+may have been set by domains and local part tests during an ACL. */
+
+if (global_rewrite_rules != NULL)
+  {
+  uschar *old = address;
+  address = rewrite_address(address, is_recipient, FALSE,
+    global_rewrite_rules, rewrite_existflags);
+  if (address != old)
+    {
+    for (i = 0; i < (MAX_NAMED_LIST * 2)/32; i++) vaddr->localpart_cache[i] = 0;
+    for (i = 0; i < (MAX_NAMED_LIST * 2)/32; i++) vaddr->domain_cache[i] = 0;
+    if (f != NULL && !expn) fprintf(f, "Address rewritten as: %s\n", address);
+    }
+  }
+
+/* If this is the real sender address, we must update sender_address at
+this point, because it may be referred to in the routers. */
+
+if ((options & (vopt_fake_sender|vopt_is_recipient)) == 0)
+  sender_address = address;
+
+/* If the address was rewritten to <> no verification can be done, and we have
+to return OK. This rewriting is permitted only for sender addresses; for other
+addresses, such rewriting fails. */
+
+if (address[0] == 0) return OK;
+
+/* Flip the legacy TLS-related variables over to the outbound set in case
+they're used in the context of a transport used by verification. Reset them
+at exit from this routine. */
+
+tls_modify_variables(&tls_out);
+
+/* Save a copy of the sender address for re-instating if we change it to <>
+while verifying a sender address (a nice bit of self-reference there). */
+
+save_sender = sender_address;
+
+/* Update the address structure with the possibly qualified and rewritten
+address. Set it up as the starting address on the chain of new addresses. */
+
+vaddr->address = address;
+addr_new = vaddr;
+
+/* We need a loop, because an address can generate new addresses. We must also
+cope with generated pipes and files at the top level. (See also the code and
+comment in deliver.c.) However, it is usually the case that the router for
+user's .forward files has its verify flag turned off.
+
+If an address generates more than one child, the loop is used only when
+full_info is set, and this can only be set locally. Remote enquiries just get
+information about the top level address, not anything that it generated. */
+
+while (addr_new != NULL)
+  {
+  int rc;
+  address_item *addr = addr_new;
+
+  addr_new = addr->next;
+  addr->next = NULL;
+
+  DEBUG(D_verify)
+    {
+    debug_printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
+    debug_printf("Considering %s\n", addr->address);
+    }
+
+  /* Handle generated pipe, file or reply addresses. We don't get these
+  when handling EXPN, as it does only one level of expansion. */
+
+  if (testflag(addr, af_pfr))
+    {
+    allok = FALSE;
+    if (f != NULL)
+      {
+      BOOL allow;
+
+      if (addr->address[0] == '>')
+        {
+        allow = testflag(addr, af_allow_reply);
+        fprintf(f, "%s -> mail %s", addr->parent->address, addr->address + 1);
+        }
+      else
+        {
+        allow = (addr->address[0] == '|')?
+          testflag(addr, af_allow_pipe) : testflag(addr, af_allow_file);
+        fprintf(f, "%s -> %s", addr->parent->address, addr->address);
+        }
+
+      if (addr->basic_errno == ERRNO_BADTRANSPORT)
+        fprintf(f, "\n*** Error in setting up pipe, file, or autoreply:\n"
+          "%s\n", addr->message);
+      else if (allow)
+        fprintf(f, "\n  transport = %s\n", addr->transport->name);
+      else
+        fprintf(f, " *** forbidden ***\n");
+      }
+    continue;
+    }
+
+  /* Just in case some router parameter refers to it. */
+
+  return_path = (addr->p.errors_address != NULL)?
+    addr->p.errors_address : sender_address;
+
+  /* Split the address into domain and local part, handling the %-hack if
+  necessary, and then route it. While routing a sender address, set
+  $sender_address to <> because that is what it will be if we were trying to
+  send a bounce to the sender. */
+
+  if (routed != NULL) *routed = FALSE;
+  if ((rc = deliver_split_address(addr)) == OK)
+    {
+    if (!is_recipient) sender_address = null_sender;
+    rc = route_address(addr, &addr_local, &addr_remote, &addr_new,
+      &addr_succeed, verify_type);
+    sender_address = save_sender;     /* Put back the real sender */
+    }
+
+  /* If routing an address succeeded, set the flag that remembers, for use when
+  an ACL cached a sender verify (in case a callout fails). Then if routing set
+  up a list of hosts or the transport has a host list, and the callout option
+  is set, and we aren't in a host checking run, do the callout verification,
+  and set another flag that notes that a callout happened. */
+
+  if (rc == OK)
+    {
+    if (routed != NULL) *routed = TRUE;
+    if (callout > 0)
+      {
+      host_item *host_list = addr->host_list;
+
+      /* Make up some data for use in the case where there is no remote
+      transport. */
+
+      transport_feedback tf = {
+        NULL,                       /* interface (=> any) */
+        US"smtp",                   /* port */
+        US"smtp",                   /* protocol */
+        NULL,                       /* hosts */
+        US"$smtp_active_hostname",  /* helo_data */
+        FALSE,                      /* hosts_override */
+        FALSE,                      /* hosts_randomize */
+        FALSE,                      /* gethostbyname */
+        TRUE,                       /* qualify_single */
+        FALSE                       /* search_parents */
+        };
+
+      /* If verification yielded a remote transport, we want to use that
+      transport's options, so as to mimic what would happen if we were really
+      sending a message to this address. */
+
+      if (addr->transport != NULL && !addr->transport->info->local)
+        {
+        (void)(addr->transport->setup)(addr->transport, addr, &tf, 0, 0, NULL);
+
+        /* If the transport has hosts and the router does not, or if the
+        transport is configured to override the router's hosts, we must build a
+        host list of the transport's hosts, and find the IP addresses */
+
+        if (tf.hosts != NULL && (host_list == NULL || tf.hosts_override))
+          {
+          uschar *s;
+          uschar *save_deliver_domain = deliver_domain;
+          uschar *save_deliver_localpart = deliver_localpart;
+
+          host_list = NULL;    /* Ignore the router's hosts */
+
+          deliver_domain = addr->domain;
+          deliver_localpart = addr->local_part;
+          s = expand_string(tf.hosts);
+          deliver_domain = save_deliver_domain;
+          deliver_localpart = save_deliver_localpart;
+
+          if (s == NULL)
+            {
+            log_write(0, LOG_MAIN|LOG_PANIC, "failed to expand list of hosts "
+              "\"%s\" in %s transport for callout: %s", tf.hosts,
+              addr->transport->name, expand_string_message);
+            }
+          else
+            {
+            int flags;
+            uschar *canonical_name;
+            host_item *host, *nexthost;
+            host_build_hostlist(&host_list, s, tf.hosts_randomize);
+
+            /* Just ignore failures to find a host address. If we don't manage
+            to find any addresses, the callout will defer. Note that more than
+            one address may be found for a single host, which will result in
+            additional host items being inserted into the chain. Hence we must
+            save the next host first. */
+
+            flags = HOST_FIND_BY_A;
+            if (tf.qualify_single) flags |= HOST_FIND_QUALIFY_SINGLE;
+            if (tf.search_parents) flags |= HOST_FIND_SEARCH_PARENTS;
+
+            for (host = host_list; host != NULL; host = nexthost)
+              {
+              nexthost = host->next;
+              if (tf.gethostbyname ||
+                  string_is_ip_address(host->name, NULL) != 0)
+                (void)host_find_byname(host, NULL, flags, &canonical_name, TRUE);
+              else
+               {
+               uschar * d_request = NULL, * d_require = NULL;
+               if (Ustrcmp(addr->transport->driver_name, "smtp") == 0)
+                 {
+                 smtp_transport_options_block * ob =
+                     (smtp_transport_options_block *)
+                       addr->transport->options_block;
+                 d_request = ob->dnssec_request_domains;
+                 d_require = ob->dnssec_require_domains;
+                 }
+
+                (void)host_find_bydns(host, NULL, flags, NULL, NULL, NULL,
+                 d_request, d_require, &canonical_name, NULL);
+               }
+              }
+            }
+          }
+        }
+
+      /* Can only do a callout if we have at least one host! If the callout
+      fails, it will have set ${sender,recipient}_verify_failure. */
+
+      if (host_list != NULL)
+        {
+        HDEBUG(D_verify) debug_printf("Attempting full verification using callout\n");
+        if (host_checking && !host_checking_callout)
+          {
+          HDEBUG(D_verify)
+            debug_printf("... callout omitted by default when host testing\n"
+              "(Use -bhc if you want the callouts to happen.)\n");
+          }
+        else
+          {
+#ifdef SUPPORT_TLS
+         deliver_set_expansions(addr);
+#endif
+          rc = do_callout(addr, host_list, &tf, callout, callout_overall,
+            callout_connect, options, se_mailfrom, pm_mailfrom);
+          }
+        }
+      else
+        {
+        HDEBUG(D_verify) debug_printf("Cannot do callout: neither router nor "
+          "transport provided a host list\n");
+        }
+      }
+    }
+
+  /* Otherwise, any failure is a routing failure */
+
+  else *failure_ptr = US"route";
+
+  /* A router may return REROUTED if it has set up a child address as a result
+  of a change of domain name (typically from widening). In this case we always
+  want to continue to verify the new child. */
+
+  if (rc == REROUTED) continue;
+
+  /* Handle hard failures */
+
+  if (rc == FAIL)
+    {
+    allok = FALSE;
+    if (f != NULL)
+      {
+      address_item *p = addr->parent;
+
+      respond_printf(f, "%s%s %s", ko_prefix,
+        full_info? addr->address : address,
+        address_test_mode? "is undeliverable" : "failed to verify");
+      if (!expn && admin_user)
+        {
+        if (addr->basic_errno > 0)
+          respond_printf(f, ": %s", strerror(addr->basic_errno));
+        if (addr->message != NULL)
+          respond_printf(f, ": %s", addr->message);
+        }
+
+      /* Show parents iff doing full info */
+
+      if (full_info) while (p != NULL)
+        {
+        respond_printf(f, "%s\n    <-- %s", cr, p->address);
+        p = p->parent;
+        }
+      respond_printf(f, "%s\n", cr);
+      }
+    cancel_cutthrough_connection("routing hard fail");
+
+    if (!full_info)
+    {
+      yield = copy_error(vaddr, addr, FAIL);
+      goto out;
+    }
+    else yield = FAIL;
+    }
+
+  /* Soft failure */
+
+  else if (rc == DEFER)
+    {
+    allok = FALSE;
+    if (f != NULL)
+      {
+      address_item *p = addr->parent;
+      respond_printf(f, "%s%s cannot be resolved at this time", ko_prefix,
+        full_info? addr->address : address);
+      if (!expn && admin_user)
+        {
+        if (addr->basic_errno > 0)
+          respond_printf(f, ": %s", strerror(addr->basic_errno));
+        if (addr->message != NULL)
+          respond_printf(f, ": %s", addr->message);
+        else if (addr->basic_errno <= 0)
+          respond_printf(f, ": unknown error");
+        }
+
+      /* Show parents iff doing full info */
+
+      if (full_info) while (p != NULL)
+        {
+        respond_printf(f, "%s\n    <-- %s", cr, p->address);
+        p = p->parent;
+        }
+      respond_printf(f, "%s\n", cr);
+      }
+    cancel_cutthrough_connection("routing soft fail");
+
+    if (!full_info)
+      {
+      yield = copy_error(vaddr, addr, DEFER);
+      goto out;
+      }
+    else if (yield == OK) yield = DEFER;
+    }
+
+  /* If we are handling EXPN, we do not want to continue to route beyond
+  the top level (whose address is in "address"). */
+
+  else if (expn)
+    {
+    uschar *ok_prefix = US"250-";
+    if (addr_new == NULL)
+      {
+      if (addr_local == NULL && addr_remote == NULL)
+        respond_printf(f, "250 mail to <%s> is discarded\r\n", address);
+      else
+        respond_printf(f, "250 <%s>\r\n", address);
+      }
+    else while (addr_new != NULL)
+      {
+      address_item *addr2 = addr_new;
+      addr_new = addr2->next;
+      if (addr_new == NULL) ok_prefix = US"250 ";
+      respond_printf(f, "%s<%s>\r\n", ok_prefix, addr2->address);
+      }
+    yield = OK;
+    goto out;
+    }
+
+  /* Successful routing other than EXPN. */
+
+  else
+    {
+    /* Handle successful routing when short info wanted. Otherwise continue for
+    other (generated) addresses. Short info is the operational case. Full info
+    can be requested only when debug_selector != 0 and a file is supplied.
+
+    There is a conflict between the use of aliasing as an alternate email
+    address, and as a sort of mailing list. If an alias turns the incoming
+    address into just one address (e.g. J.Caesar->jc44) you may well want to
+    carry on verifying the generated address to ensure it is valid when
+    checking incoming mail. If aliasing generates multiple addresses, you
+    probably don't want to do this. Exim therefore treats the generation of
+    just a single new address as a special case, and continues on to verify the
+    generated address. */
+
+    if (!full_info &&                    /* Stop if short info wanted AND */
+         (((addr_new == NULL ||          /* No new address OR */
+           addr_new->next != NULL ||     /* More than one new address OR */
+           testflag(addr_new, af_pfr)))  /* New address is pfr */
+         ||                              /* OR */
+         (addr_new != NULL &&            /* At least one new address AND */
+          success_on_redirect)))         /* success_on_redirect is set */
+      {
+      if (f != NULL) fprintf(f, "%s %s\n", address,
+        address_test_mode? "is deliverable" : "verified");
+
+      /* If we have carried on to verify a child address, we want the value
+      of $address_data to be that of the child */
+
+      vaddr->p.address_data = addr->p.address_data;
+      yield = OK;
+      goto out;
+      }
+    }
+  }     /* Loop for generated addresses */
+
+/* Display the full results of the successful routing, including any generated
+addresses. Control gets here only when full_info is set, which requires f not
+to be NULL, and this occurs only when a top-level verify is called with the
+debugging switch on.
+
+If there are no local and no remote addresses, and there were no pipes, files,
+or autoreplies, and there were no errors or deferments, the message is to be
+discarded, usually because of the use of :blackhole: in an alias file. */
+
+if (allok && addr_local == NULL && addr_remote == NULL)
+  {
+  fprintf(f, "mail to %s is discarded\n", address);
+  goto out;
+  }
+
+for (addr_list = addr_local, i = 0; i < 2; addr_list = addr_remote, i++)
+  {
+  while (addr_list != NULL)
+    {
+    address_item *addr = addr_list;
+    address_item *p = addr->parent;
+    addr_list = addr->next;
+
+    fprintf(f, "%s", CS addr->address);
+#ifdef EXPERIMENTAL_SRS
+    if(addr->p.srs_sender)
+      fprintf(f, "    [srs = %s]", addr->p.srs_sender);
+#endif
+
+    /* If the address is a duplicate, show something about it. */
+
+    if (!testflag(addr, af_pfr))
+      {
+      tree_node *tnode;
+      if ((tnode = tree_search(tree_duplicates, addr->unique)) != NULL)
+        fprintf(f, "   [duplicate, would not be delivered]");
+      else tree_add_duplicate(addr->unique, addr);
+      }
+
+    /* Now show its parents */
+
+    while (p != NULL)
+      {
+      fprintf(f, "\n    <-- %s", p->address);
+      p = p->parent;
+      }
+    fprintf(f, "\n  ");
+
+    /* Show router, and transport */
+
+    fprintf(f, "router = %s, ", addr->router->name);
+    fprintf(f, "transport = %s\n", (addr->transport == NULL)? US"unset" :
+      addr->transport->name);
+
+    /* Show any hosts that are set up by a router unless the transport
+    is going to override them; fiddle a bit to get a nice format. */
+
+    if (addr->host_list != NULL && addr->transport != NULL &&
+        !addr->transport->overrides_hosts)
+      {
+      host_item *h;
+      int maxlen = 0;
+      int maxaddlen = 0;
+      for (h = addr->host_list; h != NULL; h = h->next)
+        {
+        int len = Ustrlen(h->name);
+        if (len > maxlen) maxlen = len;
+        len = (h->address != NULL)? Ustrlen(h->address) : 7;
+        if (len > maxaddlen) maxaddlen = len;
+        }
+      for (h = addr->host_list; h != NULL; h = h->next)
+        {
+        int len = Ustrlen(h->name);
+        fprintf(f, "  host %s ", h->name);
+        while (len++ < maxlen) fprintf(f, " ");
+        if (h->address != NULL)
+          {
+          fprintf(f, "[%s] ", h->address);
+          len = Ustrlen(h->address);
+          }
+        else if (!addr->transport->info->local)  /* Omit [unknown] for local */
+          {
+          fprintf(f, "[unknown] ");
+          len = 7;
+          }
+        else len = -3;
+        while (len++ < maxaddlen) fprintf(f," ");
+        if (h->mx >= 0) fprintf(f, "MX=%d", h->mx);
+        if (h->port != PORT_NONE) fprintf(f, " port=%d", h->port);
+        if (h->status == hstatus_unusable) fprintf(f, " ** unusable **");
+        fprintf(f, "\n");
+        }
+      }
+    }
+  }
+
+/* Yield will be DEFER or FAIL if any one address has, only for full_info (which is
+the -bv or -bt case). */
+
+out:
+tls_modify_variables(&tls_in);
+
+return yield;
+}
+
+
+
+
+/*************************************************
+*      Check headers for syntax errors           *
+*************************************************/
+
+/* This function checks those header lines that contain addresses, and verifies
+that all the addresses therein are syntactially correct.
+
+Arguments:
+  msgptr     where to put an error message
+
+Returns:     OK
+             FAIL
+*/
+
+int
+verify_check_headers(uschar **msgptr)
+{
+header_line *h;
+uschar *colon, *s;
+int yield = OK;
+
+for (h = header_list; h != NULL && yield == OK; h = h->next)
+  {
+  if (h->type != htype_from &&
+      h->type != htype_reply_to &&
+      h->type != htype_sender &&
+      h->type != htype_to &&
+      h->type != htype_cc &&
+      h->type != htype_bcc)
+    continue;
+
+  colon = Ustrchr(h->text, ':');
+  s = colon + 1;
+  while (isspace(*s)) s++;
+
+  /* Loop for multiple addresses in the header, enabling group syntax. Note
+  that we have to reset this after the header has been scanned. */
+
+  parse_allow_group = TRUE;
+
+  while (*s != 0)
+    {
+    uschar *ss = parse_find_address_end(s, FALSE);
+    uschar *recipient, *errmess;
+    int terminator = *ss;
+    int start, end, domain;
+
+    /* Temporarily terminate the string at this point, and extract the
+    operative address within, allowing group syntax. */
+
+    *ss = 0;
+    recipient = parse_extract_address(s,&errmess,&start,&end,&domain,FALSE);
+    *ss = terminator;
+
+    /* Permit an unqualified address only if the message is local, or if the
+    sending host is configured to be permitted to send them. */
+
+    if (recipient != NULL && domain == 0)
+      {
+      if (h->type == htype_from || h->type == htype_sender)
+        {
+        if (!allow_unqualified_sender) recipient = NULL;
+        }
+      else
+        {
+        if (!allow_unqualified_recipient) recipient = NULL;
+        }
+      if (recipient == NULL) errmess = US"unqualified address not permitted";
+      }
+
+    /* It's an error if no address could be extracted, except for the special
+    case of an empty address. */
+
+    if (recipient == NULL && Ustrcmp(errmess, "empty address") != 0)
+      {
+      uschar *verb = US"is";
+      uschar *t = ss;
+      uschar *tt = colon;
+      int len;
+
+      /* Arrange not to include any white space at the end in the
+      error message or the header name. */
+
+      while (t > s && isspace(t[-1])) t--;
+      while (tt > h->text && isspace(tt[-1])) tt--;
+
+      /* Add the address that failed to the error message, since in a
+      header with very many addresses it is sometimes hard to spot
+      which one is at fault. However, limit the amount of address to
+      quote - cases have been seen where, for example, a missing double
+      quote in a humungous To: header creates an "address" that is longer
+      than string_sprintf can handle. */
+
+      len = t - s;
+      if (len > 1024)
+        {
+        len = 1024;
+        verb = US"begins";
+        }
+
+      *msgptr = string_printing(
+        string_sprintf("%s: failing address in \"%.*s:\" header %s: %.*s",
+          errmess, tt - h->text, h->text, verb, len, s));
+
+      yield = FAIL;
+      break;          /* Out of address loop */
+      }
+
+    /* Advance to the next address */
+
+    s = ss + (terminator? 1:0);
+    while (isspace(*s)) s++;
+    }   /* Next address */
+
+  parse_allow_group = FALSE;
+  parse_found_group = FALSE;
+  }     /* Next header unless yield has been set FALSE */
+
+return yield;
+}
+
+
+/*************************************************
+*      Check header names for 8-bit characters   *
+*************************************************/
+
+/* This function checks for invalid charcters in header names. See
+RFC 5322, 2.2. and RFC 6532, 3.
+
+Arguments:
+  msgptr     where to put an error message
+
+Returns:     OK
+             FAIL
+*/
+
+int
+verify_check_header_names_ascii(uschar **msgptr)
+{
+header_line *h;
+uschar *colon, *s;
+
+for (h = header_list; h != NULL; h = h->next)
+  {
+   colon = Ustrchr(h->text, ':');
+   for(s = h->text; s < colon; s++)
+     {
+        if ((*s < 33) || (*s > 126))
+        {
+                *msgptr = string_sprintf("Invalid character in header \"%.*s\" found",
+                                         colon - h->text, h->text);
+                return FAIL;
+        }
+     }
+  }
+return OK;
+}
+
+/*************************************************
+*          Check for blind recipients            *
+*************************************************/
+
+/* This function checks that every (envelope) recipient is mentioned in either
+the To: or Cc: header lines, thus detecting blind carbon copies.
+
+There are two ways of scanning that could be used: either scan the header lines
+and tick off the recipients, or scan the recipients and check the header lines.
+The original proposed patch did the former, but I have chosen to do the latter,
+because (a) it requires no memory and (b) will use fewer resources when there
+are many addresses in To: and/or Cc: and only one or two envelope recipients.
+
+Arguments:   none
+Returns:     OK    if there are no blind recipients
+             FAIL  if there is at least one blind recipient
+*/
+
+int
+verify_check_notblind(void)
+{
+int i;
+for (i = 0; i < recipients_count; i++)
+  {
+  header_line *h;
+  BOOL found = FALSE;
+  uschar *address = recipients_list[i].address;
+
+  for (h = header_list; !found && h != NULL; h = h->next)
+    {
+    uschar *colon, *s;
+
+    if (h->type != htype_to && h->type != htype_cc) continue;
+
+    colon = Ustrchr(h->text, ':');
+    s = colon + 1;
+    while (isspace(*s)) s++;
+
+    /* Loop for multiple addresses in the header, enabling group syntax. Note
+    that we have to reset this after the header has been scanned. */
+
+    parse_allow_group = TRUE;
+
+    while (*s != 0)
+      {
+      uschar *ss = parse_find_address_end(s, FALSE);
+      uschar *recipient,*errmess;
+      int terminator = *ss;
+      int start, end, domain;
+
+      /* Temporarily terminate the string at this point, and extract the
+      operative address within, allowing group syntax. */
+
+      *ss = 0;
+      recipient = parse_extract_address(s,&errmess,&start,&end,&domain,FALSE);
+      *ss = terminator;
+
+      /* If we found a valid recipient that has a domain, compare it with the
+      envelope recipient. Local parts are compared case-sensitively, domains
+      case-insensitively. By comparing from the start with length "domain", we
+      include the "@" at the end, which ensures that we are comparing the whole
+      local part of each address. */
+
+      if (recipient != NULL && domain != 0)
+        {
+        found = Ustrncmp(recipient, address, domain) == 0 &&
+                strcmpic(recipient + domain, address + domain) == 0;
+        if (found) break;
+        }
+
+      /* Advance to the next address */
+
+      s = ss + (terminator? 1:0);
+      while (isspace(*s)) s++;
+      }   /* Next address */
+
+    parse_allow_group = FALSE;
+    parse_found_group = FALSE;
+    }     /* Next header (if found is false) */
+
+  if (!found) return FAIL;
+  }       /* Next recipient */
+
+return OK;
+}
+
+
+
+/*************************************************
+*          Find if verified sender               *
+*************************************************/
+
+/* Usually, just a single address is verified as the sender of the message.
+However, Exim can be made to verify other addresses as well (often related in
+some way), and this is useful in some environments. There may therefore be a
+chain of such addresses that have previously been tested. This function finds
+whether a given address is on the chain.
+
+Arguments:   the address to be verified
+Returns:     pointer to an address item, or NULL
+*/
+
+address_item *
+verify_checked_sender(uschar *sender)
+{
+address_item *addr;
+for (addr = sender_verified_list; addr != NULL; addr = addr->next)
+  if (Ustrcmp(sender, addr->address) == 0) break;
+return addr;
+}
+
+
+
+
+
+/*************************************************
+*             Get valid header address           *
+*************************************************/
+
+/* Scan the originator headers of the message, looking for an address that
+verifies successfully. RFC 822 says:
+
+    o   The "Sender" field mailbox should be sent  notices  of
+        any  problems in transport or delivery of the original
+        messages.  If there is no  "Sender"  field,  then  the
+        "From" field mailbox should be used.
+
+    o   If the "Reply-To" field exists, then the reply  should
+        go to the addresses indicated in that field and not to
+        the address(es) indicated in the "From" field.
+
+So we check a Sender field if there is one, else a Reply_to field, else a From
+field. As some strange messages may have more than one of these fields,
+especially if they are resent- fields, check all of them if there is more than
+one.
+
+Arguments:
+  user_msgptr      points to where to put a user error message
+  log_msgptr       points to where to put a log error message
+  callout          timeout for callout check (passed to verify_address())
+  callout_overall  overall callout timeout (ditto)
+  callout_connect  connect callout timeout (ditto)
+  se_mailfrom      mailfrom for verify; NULL => ""
+  pm_mailfrom      sender for pm callout check (passed to verify_address())
+  options          callout options (passed to verify_address())
+  verrno           where to put the address basic_errno
+
+If log_msgptr is set to something without setting user_msgptr, the caller
+normally uses log_msgptr for both things.
+
+Returns:           result of the verification attempt: OK, FAIL, or DEFER;
+                   FAIL is given if no appropriate headers are found
+*/
+
+int
+verify_check_header_address(uschar **user_msgptr, uschar **log_msgptr,
+  int callout, int callout_overall, int callout_connect, uschar *se_mailfrom,
+  uschar *pm_mailfrom, int options, int *verrno)
+{
+static int header_types[] = { htype_sender, htype_reply_to, htype_from };
+BOOL done = FALSE;
+int yield = FAIL;
+int i;
+
+for (i = 0; i < 3 && !done; i++)
+  {
+  header_line *h;
+  for (h = header_list; h != NULL && !done; h = h->next)
+    {
+    int terminator, new_ok;
+    uschar *s, *ss, *endname;
+
+    if (h->type != header_types[i]) continue;
+    s = endname = Ustrchr(h->text, ':') + 1;
+
+    /* Scan the addresses in the header, enabling group syntax. Note that we
+    have to reset this after the header has been scanned. */
+
+    parse_allow_group = TRUE;
+
+    while (*s != 0)
+      {
+      address_item *vaddr;
+
+      while (isspace(*s) || *s == ',') s++;
+      if (*s == 0) break;        /* End of header */
+
+      ss = parse_find_address_end(s, FALSE);
+
+      /* The terminator is a comma or end of header, but there may be white
+      space preceding it (including newline for the last address). Move back
+      past any white space so we can check against any cached envelope sender
+      address verifications. */
+
+      while (isspace(ss[-1])) ss--;
+      terminator = *ss;
+      *ss = 0;
+
+      HDEBUG(D_verify) debug_printf("verifying %.*s header address %s\n",
+        (int)(endname - h->text), h->text, s);
+
+      /* See if we have already verified this address as an envelope sender,
+      and if so, use the previous answer. */
+
+      vaddr = verify_checked_sender(s);
+
+      if (vaddr != NULL &&                   /* Previously checked */
+           (callout <= 0 ||                  /* No callout needed; OR */
+            vaddr->special_action > 256))    /* Callout was done */
+        {
+        new_ok = vaddr->special_action & 255;
+        HDEBUG(D_verify) debug_printf("previously checked as envelope sender\n");
+        *ss = terminator;  /* Restore shortened string */
+        }
+
+      /* Otherwise we run the verification now. We must restore the shortened
+      string before running the verification, so the headers are correct, in
+      case there is any rewriting. */
+
+      else
+        {
+        int start, end, domain;
+        uschar *address = parse_extract_address(s, log_msgptr, &start, &end,
+          &domain, FALSE);
+
+        *ss = terminator;
+
+        /* If we found an empty address, just carry on with the next one, but
+        kill the message. */
+
+        if (address == NULL && Ustrcmp(*log_msgptr, "empty address") == 0)
+          {
+          *log_msgptr = NULL;
+          s = ss;
+          continue;
+          }
+
+        /* If verification failed because of a syntax error, fail this
+        function, and ensure that the failing address gets added to the error
+        message. */
+
+        if (address == NULL)
+          {
+          new_ok = FAIL;
+          while (ss > s && isspace(ss[-1])) ss--;
+          *log_msgptr = string_sprintf("syntax error in '%.*s' header when "
+            "scanning for sender: %s in \"%.*s\"",
+            endname - h->text, h->text, *log_msgptr, ss - s, s);
+          yield = FAIL;
+          done = TRUE;
+          break;
+          }
+
+        /* Else go ahead with the sender verification. But it isn't *the*
+        sender of the message, so set vopt_fake_sender to stop sender_address
+        being replaced after rewriting or qualification. */
+
+        else
+          {
+          vaddr = deliver_make_addr(address, FALSE);
+          new_ok = verify_address(vaddr, NULL, options | vopt_fake_sender,
+            callout, callout_overall, callout_connect, se_mailfrom,
+            pm_mailfrom, NULL);
+          }
+        }
+
+      /* We now have the result, either newly found, or cached. If we are
+      giving out error details, set a specific user error. This means that the
+      last of these will be returned to the user if all three fail. We do not
+      set a log message - the generic one below will be used. */
+
+      if (new_ok != OK)
+        {
+        *verrno = vaddr->basic_errno;
+        if (smtp_return_error_details)
+          {
+          *user_msgptr = string_sprintf("Rejected after DATA: "
+            "could not verify \"%.*s\" header address\n%s: %s",
+            endname - h->text, h->text, vaddr->address, vaddr->message);
+          }
+        }
+
+      /* Success or defer */
+
+      if (new_ok == OK)
+        {
+        yield = OK;
+        done = TRUE;
+        break;
+        }
+
+      if (new_ok == DEFER) yield = DEFER;
+
+      /* Move on to any more addresses in the header */
+
+      s = ss;
+      }     /* Next address */
+
+    parse_allow_group = FALSE;
+    parse_found_group = FALSE;
+    }       /* Next header, unless done */
+  }         /* Next header type unless done */
+
+if (yield == FAIL && *log_msgptr == NULL)
+  *log_msgptr = US"there is no valid sender in any header line";
+
+if (yield == DEFER && *log_msgptr == NULL)
+  *log_msgptr = US"all attempts to verify a sender in a header line deferred";
+
+return yield;
+}
+
+
+
+
+/*************************************************
+*            Get RFC 1413 identification         *
+*************************************************/
+
+/* Attempt to get an id from the sending machine via the RFC 1413 protocol. If
+the timeout is set to zero, then the query is not done. There may also be lists
+of hosts and nets which are exempt. To guard against malefactors sending
+non-printing characters which could, for example, disrupt a message's headers,
+make sure the string consists of printing characters only.
+
+Argument:
+  port    the port to connect to; usually this is IDENT_PORT (113), but when
+          running in the test harness with -bh a different value is used.
+
+Returns:  nothing
+
+Side effect: any received ident value is put in sender_ident (NULL otherwise)
+*/
+
+void
+verify_get_ident(int port)
+{
+int sock, host_af, qlen;
+int received_sender_port, received_interface_port, n;
+uschar *p;
+uschar buffer[2048];
+
+/* Default is no ident. Check whether we want to do an ident check for this
+host. */
+
+sender_ident = NULL;
+if (rfc1413_query_timeout <= 0 || verify_check_host(&rfc1413_hosts) != OK)
+  return;
+
+DEBUG(D_ident) debug_printf("doing ident callback\n");
+
+/* Set up a connection to the ident port of the remote host. Bind the local end
+to the incoming interface address. If the sender host address is an IPv6
+address, the incoming interface address will also be IPv6. */
+
+host_af = (Ustrchr(sender_host_address, ':') == NULL)? AF_INET : AF_INET6;
+sock = ip_socket(SOCK_STREAM, host_af);
+if (sock < 0) return;
+
+if (ip_bind(sock, host_af, interface_address, 0) < 0)
+  {
+  DEBUG(D_ident) debug_printf("bind socket for ident failed: %s\n",
+    strerror(errno));
+  goto END_OFF;
+  }
+
+if (ip_connect(sock, host_af, sender_host_address, port, rfc1413_query_timeout)
+     < 0)
+  {
+  if (errno == ETIMEDOUT && (log_extra_selector & LX_ident_timeout) != 0)
+    {
+    log_write(0, LOG_MAIN, "ident connection to %s timed out",
+      sender_host_address);
+    }
+  else
+    {
+    DEBUG(D_ident) debug_printf("ident connection to %s failed: %s\n",
+      sender_host_address, strerror(errno));
+    }
+  goto END_OFF;
+  }
+
+/* Construct and send the query. */
+
+sprintf(CS buffer, "%d , %d\r\n", sender_host_port, interface_port);
+qlen = Ustrlen(buffer);
+if (send(sock, buffer, qlen, 0) < 0)
+  {
+  DEBUG(D_ident) debug_printf("ident send failed: %s\n", strerror(errno));
+  goto END_OFF;
+  }
+
+/* Read a response line. We put it into the rest of the buffer, using several
+recv() calls if necessary. */
+
+p = buffer + qlen;
+
+for (;;)
+  {
+  uschar *pp;
+  int count;
+  int size = sizeof(buffer) - (p - buffer);
+
+  if (size <= 0) goto END_OFF;   /* Buffer filled without seeing \n. */
+  count = ip_recv(sock, p, size, rfc1413_query_timeout);
+  if (count <= 0) goto END_OFF;  /* Read error or EOF */
+
+  /* Scan what we just read, to see if we have reached the terminating \r\n. Be
+  generous, and accept a plain \n terminator as well. The only illegal
+  character is 0. */
+
+  for (pp = p; pp < p + count; pp++)
+    {
+    if (*pp == 0) goto END_OFF;   /* Zero octet not allowed */
+    if (*pp == '\n')
+      {
+      if (pp[-1] == '\r') pp--;
+      *pp = 0;
+      goto GOT_DATA;             /* Break out of both loops */
+      }
+    }
+
+  /* Reached the end of the data without finding \n. Let the loop continue to
+  read some more, if there is room. */
+
+  p = pp;
+  }
+
+GOT_DATA:
+
+/* We have received a line of data. Check it carefully. It must start with the
+same two port numbers that we sent, followed by data as defined by the RFC. For
+example,
+
+  12345 , 25 : USERID : UNIX :root
+
+However, the amount of white space may be different to what we sent. In the
+"osname" field there may be several sub-fields, comma separated. The data we
+actually want to save follows the third colon. Some systems put leading spaces
+in it - we discard those. */
+
+if (sscanf(CS buffer + qlen, "%d , %d%n", &received_sender_port,
+      &received_interface_port, &n) != 2 ||
+    received_sender_port != sender_host_port ||
+    received_interface_port != interface_port)
+  goto END_OFF;
+
+p = buffer + qlen + n;
+while(isspace(*p)) p++;
+if (*p++ != ':') goto END_OFF;
+while(isspace(*p)) p++;
+if (Ustrncmp(p, "USERID", 6) != 0) goto END_OFF;
+p += 6;
+while(isspace(*p)) p++;
+if (*p++ != ':') goto END_OFF;
+while (*p != 0 && *p != ':') p++;
+if (*p++ == 0) goto END_OFF;
+while(isspace(*p)) p++;
+if (*p == 0) goto END_OFF;
+
+/* The rest of the line is the data we want. We turn it into printing
+characters when we save it, so that it cannot mess up the format of any logging
+or Received: lines into which it gets inserted. We keep a maximum of 127
+characters. */
+
+sender_ident = string_printing(string_copyn(p, 127));
+DEBUG(D_ident) debug_printf("sender_ident = %s\n", sender_ident);
+
+END_OFF:
+(void)close(sock);
+return;
+}
+
+
+
+
+/*************************************************
+*      Match host to a single host-list item     *
+*************************************************/
+
+/* This function compares a host (name or address) against a single item
+from a host list. The host name gets looked up if it is needed and is not
+already known. The function is called from verify_check_this_host() via
+match_check_list(), which is why most of its arguments are in a single block.
+
+Arguments:
+  arg            the argument block (see below)
+  ss             the host-list item
+  valueptr       where to pass back looked up data, or NULL
+  error          for error message when returning ERROR
+
+The block contains:
+  host_name      (a) the host name, or
+                 (b) NULL, implying use sender_host_name and
+                       sender_host_aliases, looking them up if required, or
+                 (c) the empty string, meaning that only IP address matches
+                       are permitted
+  host_address   the host address
+  host_ipv4      the IPv4 address taken from an IPv6 one
+
+Returns:         OK      matched
+                 FAIL    did not match
+                 DEFER   lookup deferred
+                 ERROR   (a) failed to find the host name or IP address, or
+                         (b) unknown lookup type specified, or
+                         (c) host name encountered when only IP addresses are
+                               being matched
+*/
+
+int
+check_host(void *arg, uschar *ss, uschar **valueptr, uschar **error)
+{
+check_host_block *cb = (check_host_block *)arg;
+int mlen = -1;
+int maskoffset;
+BOOL iplookup = FALSE;
+BOOL isquery = FALSE;
+BOOL isiponly = cb->host_name != NULL && cb->host_name[0] == 0;
+uschar *t;
+uschar *semicolon;
+uschar **aliases;
+
+/* Optimize for the special case when the pattern is "*". */
+
+if (*ss == '*' && ss[1] == 0) return OK;
+
+/* If the pattern is empty, it matches only in the case when there is no host -
+this can occur in ACL checking for SMTP input using the -bs option. In this
+situation, the host address is the empty string. */
+
+if (cb->host_address[0] == 0) return (*ss == 0)? OK : FAIL;
+if (*ss == 0) return FAIL;
+
+/* If the pattern is precisely "@" then match against the primary host name,
+provided that host name matching is permitted; if it's "@[]" match against the
+local host's IP addresses. */
+
+if (*ss == '@')
+  {
+  if (ss[1] == 0)
+    {
+    if (isiponly) return ERROR;
+    ss = primary_hostname;
+    }
+  else if (Ustrcmp(ss, "@[]") == 0)
+    {
+    ip_address_item *ip;
+    for (ip = host_find_interfaces(); ip != NULL; ip = ip->next)
+      if (Ustrcmp(ip->address, cb->host_address) == 0) return OK;
+    return FAIL;
+    }
+  }
+
+/* If the pattern is an IP address, optionally followed by a bitmask count, do
+a (possibly masked) comparision with the current IP address. */
+
+if (string_is_ip_address(ss, &maskoffset) != 0)
+  return (host_is_in_net(cb->host_address, ss, maskoffset)? OK : FAIL);
+
+/* The pattern is not an IP address. A common error that people make is to omit
+one component of an IPv4 address, either by accident, or believing that, for
+example, 1.2.3/24 is the same as 1.2.3.0/24, or 1.2.3 is the same as 1.2.3.0,
+which it isn't. (Those applications that do accept 1.2.3 as an IP address
+interpret it as 1.2.0.3 because the final component becomes 16-bit - this is an
+ancient specification.) To aid in debugging these cases, we give a specific
+error if the pattern contains only digits and dots or contains a slash preceded
+only by digits and dots (a slash at the start indicates a file name and of
+course slashes may be present in lookups, but not preceded only by digits and
+dots). */
+
+for (t = ss; isdigit(*t) || *t == '.'; t++);
+if (*t == 0 || (*t == '/' && t != ss))
+  {
+  *error = US"malformed IPv4 address or address mask";
+  return ERROR;
+  }
+
+/* See if there is a semicolon in the pattern */
+
+semicolon = Ustrchr(ss, ';');
+
+/* If we are doing an IP address only match, then all lookups must be IP
+address lookups, even if there is no "net-". */
+
+if (isiponly)
+  {
+  iplookup = semicolon != NULL;
+  }
+
+/* Otherwise, if the item is of the form net[n]-lookup;<file|query> then it is
+a lookup on a masked IP network, in textual form. We obey this code even if we
+have already set iplookup, so as to skip over the "net-" prefix and to set the
+mask length. The net- stuff really only applies to single-key lookups where the
+key is implicit. For query-style lookups the key is specified in the query.
+From release 4.30, the use of net- for query style is no longer needed, but we
+retain it for backward compatibility. */
+
+if (Ustrncmp(ss, "net", 3) == 0 && semicolon != NULL)
+  {
+  mlen = 0;
+  for (t = ss + 3; isdigit(*t); t++) mlen = mlen * 10 + *t - '0';
+  if (mlen == 0 && t == ss+3) mlen = -1;  /* No mask supplied */
+  iplookup = (*t++ == '-');
+  }
+else t = ss;
+
+/* Do the IP address lookup if that is indeed what we have */
+
+if (iplookup)
+  {
+  int insize;
+  int search_type;
+  int incoming[4];
+  void *handle;
+  uschar *filename, *key, *result;
+  uschar buffer[64];
+
+  /* Find the search type */
+
+  search_type = search_findtype(t, semicolon - t);
+
+  if (search_type < 0) log_write(0, LOG_MAIN|LOG_PANIC_DIE, "%s",
+    search_error_message);
+
+  /* Adjust parameters for the type of lookup. For a query-style lookup, there
+  is no file name, and the "key" is just the query. For query-style with a file
+  name, we have to fish the file off the start of the query. For a single-key
+  lookup, the key is the current IP address, masked appropriately, and
+  reconverted to text form, with the mask appended. For IPv6 addresses, specify
+  dot separators instead of colons, except when the lookup type is "iplsearch".
+  */
+
+  if (mac_islookup(search_type, lookup_absfilequery))
+    {
+    filename = semicolon + 1;
+    key = filename;
+    while (*key != 0 && !isspace(*key)) key++;
+    filename = string_copyn(filename, key - filename);
+    while (isspace(*key)) key++;
+    }
+  else if (mac_islookup(search_type, lookup_querystyle))
+    {
+    filename = NULL;
+    key = semicolon + 1;
+    }
+  else   /* Single-key style */
+    {
+    int sep = (Ustrcmp(lookup_list[search_type]->name, "iplsearch") == 0)?
+      ':' : '.';
+    insize = host_aton(cb->host_address, incoming);
+    host_mask(insize, incoming, mlen);
+    (void)host_nmtoa(insize, incoming, mlen, buffer, sep);
+    key = buffer;
+    filename = semicolon + 1;
+    }
+
+  /* Now do the actual lookup; note that there is no search_close() because
+  of the caching arrangements. */
+
+  handle = search_open(filename, search_type, 0, NULL, NULL);
+  if (handle == NULL) log_write(0, LOG_MAIN|LOG_PANIC_DIE, "%s",
+    search_error_message);
+  result = search_find(handle, filename, key, -1, NULL, 0, 0, NULL);
+  if (valueptr != NULL) *valueptr = result;
+  return (result != NULL)? OK : search_find_defer? DEFER: FAIL;
+  }
+
+/* The pattern is not an IP address or network reference of any kind. That is,
+it is a host name pattern. If this is an IP only match, there's an error in the
+host list. */
+
+if (isiponly)
+  {
+  *error = US"cannot match host name in match_ip list";
+  return ERROR;
+  }
+
+/* Check the characters of the pattern to see if they comprise only letters,
+digits, full stops, and hyphens (the constituents of domain names). Allow
+underscores, as they are all too commonly found. Sigh. Also, if
+allow_utf8_domains is set, allow top-bit characters. */
+
+for (t = ss; *t != 0; t++)
+  if (!isalnum(*t) && *t != '.' && *t != '-' && *t != '_' &&
+      (!allow_utf8_domains || *t < 128)) break;
+
+/* If the pattern is a complete domain name, with no fancy characters, look up
+its IP address and match against that. Note that a multi-homed host will add
+items to the chain. */
+
+if (*t == 0)
+  {
+  int rc;
+  host_item h;
+  h.next = NULL;
+  h.name = ss;
+  h.address = NULL;
+  h.mx = MX_NONE;
+
+  rc = host_find_byname(&h, NULL, HOST_FIND_QUALIFY_SINGLE, NULL, FALSE);
+  if (rc == HOST_FOUND || rc == HOST_FOUND_LOCAL)
+    {
+    host_item *hh;
+    for (hh = &h; hh != NULL; hh = hh->next)
+      {
+      if (host_is_in_net(hh->address, cb->host_address, 0)) return OK;
+      }
+    return FAIL;
+    }
+  if (rc == HOST_FIND_AGAIN) return DEFER;
+  *error = string_sprintf("failed to find IP address for %s", ss);
+  return ERROR;
+  }
+
+/* Almost all subsequent comparisons require the host name, and can be done
+using the general string matching function. When this function is called for
+outgoing hosts, the name is always given explicitly. If it is NULL, it means we
+must use sender_host_name and its aliases, looking them up if necessary. */
+
+if (cb->host_name != NULL)   /* Explicit host name given */
+  return match_check_string(cb->host_name, ss, -1, TRUE, TRUE, TRUE,
+    valueptr);
+
+/* Host name not given; in principle we need the sender host name and its
+aliases. However, for query-style lookups, we do not need the name if the
+query does not contain $sender_host_name. From release 4.23, a reference to
+$sender_host_name causes it to be looked up, so we don't need to do the lookup
+on spec. */
+
+if ((semicolon = Ustrchr(ss, ';')) != NULL)
+  {
+  uschar *affix;
+  int partial, affixlen, starflags, id;
+
+  *semicolon = 0;
+  id = search_findtype_partial(ss, &partial, &affix, &affixlen, &starflags);
+  *semicolon=';';
+
+  if (id < 0)                           /* Unknown lookup type */
+    {
+    log_write(0, LOG_MAIN|LOG_PANIC, "%s in host list item \"%s\"",
+      search_error_message, ss);
+    return DEFER;
+    }
+  isquery = mac_islookup(id, lookup_querystyle|lookup_absfilequery);
+  }
+
+if (isquery)
+  {
+  switch(match_check_string(US"", ss, -1, TRUE, TRUE, TRUE, valueptr))
+    {
+    case OK:    return OK;
+    case DEFER: return DEFER;
+    default:    return FAIL;
+    }
+  }
+
+/* Not a query-style lookup; must ensure the host name is present, and then we
+do a check on the name and all its aliases. */
+
+if (sender_host_name == NULL)
+  {
+  HDEBUG(D_host_lookup)
+    debug_printf("sender host name required, to match against %s\n", ss);
+  if (host_lookup_failed || host_name_lookup() != OK)
+    {
+    *error = string_sprintf("failed to find host name for %s",
+      sender_host_address);;
+    return ERROR;
+    }
+  host_build_sender_fullhost();
+  }
+
+/* Match on the sender host name, using the general matching function */
+
+switch(match_check_string(sender_host_name, ss, -1, TRUE, TRUE, TRUE,
+       valueptr))
+  {
+  case OK:    return OK;
+  case DEFER: return DEFER;
+  }
+
+/* If there are aliases, try matching on them. */
+
+aliases = sender_host_aliases;
+while (*aliases != NULL)
+  {
+  switch(match_check_string(*aliases++, ss, -1, TRUE, TRUE, TRUE, valueptr))
+    {
+    case OK:    return OK;
+    case DEFER: return DEFER;
+    }
+  }
+return FAIL;
+}
+
+
+
+
+/*************************************************
+*    Check a specific host matches a host list   *
+*************************************************/
+
+/* This function is passed a host list containing items in a number of
+different formats and the identity of a host. Its job is to determine whether
+the given host is in the set of hosts defined by the list. The host name is
+passed as a pointer so that it can be looked up if needed and not already
+known. This is commonly the case when called from verify_check_host() to check
+an incoming connection. When called from elsewhere the host name should usually
+be set.
+
+This function is now just a front end to match_check_list(), which runs common
+code for scanning a list. We pass it the check_host() function to perform a
+single test.
+
+Arguments:
+  listptr              pointer to the host list
+  cache_bits           pointer to cache for named lists, or NULL
+  host_name            the host name or NULL, implying use sender_host_name and
+                         sender_host_aliases, looking them up if required
+  host_address         the IP address
+  valueptr             if not NULL, data from a lookup is passed back here
+
+Returns:    OK    if the host is in the defined set
+            FAIL  if the host is not in the defined set,
+            DEFER if a data lookup deferred (not a host lookup)
+
+If the host name was needed in order to make a comparison, and could not be
+determined from the IP address, the result is FAIL unless the item
+"+allow_unknown" was met earlier in the list, in which case OK is returned. */
+
+int
+verify_check_this_host(uschar **listptr, unsigned int *cache_bits,
+  uschar *host_name, uschar *host_address, uschar **valueptr)
+{
+int rc;
+unsigned int *local_cache_bits = cache_bits;
+uschar *save_host_address = deliver_host_address;
+check_host_block cb;
+cb.host_name = host_name;
+cb.host_address = host_address;
+
+if (valueptr != NULL) *valueptr = NULL;
+
+/* If the host address starts off ::ffff: it is an IPv6 address in
+IPv4-compatible mode. Find the IPv4 part for checking against IPv4
+addresses. */
+
+cb.host_ipv4 = (Ustrncmp(host_address, "::ffff:", 7) == 0)?
+  host_address + 7 : host_address;
+
+/* During the running of the check, put the IP address into $host_address. In
+the case of calls from the smtp transport, it will already be there. However,
+in other calls (e.g. when testing ignore_target_hosts), it won't. Just to be on
+the safe side, any existing setting is preserved, though as I write this
+(November 2004) I can't see any cases where it is actually needed. */
+
+deliver_host_address = host_address;
+rc = match_check_list(
+       listptr,                                /* the list */
+       0,                                      /* separator character */
+       &hostlist_anchor,                       /* anchor pointer */
+       &local_cache_bits,                      /* cache pointer */
+       check_host,                             /* function for testing */
+       &cb,                                    /* argument for function */
+       MCL_HOST,                               /* type of check */
+       (host_address == sender_host_address)?
+         US"host" : host_address,              /* text for debugging */
+       valueptr);                              /* where to pass back data */
+deliver_host_address = save_host_address;
+return rc;
+}
+
+
+
+
+/*************************************************
+*      Check the remote host matches a list      *
+*************************************************/
+
+/* This is a front end to verify_check_this_host(), created because checking
+the remote host is a common occurrence. With luck, a good compiler will spot
+the tail recursion and optimize it. If there's no host address, this is
+command-line SMTP input - check against an empty string for the address.
+
+Arguments:
+  listptr              pointer to the host list
+
+Returns:               the yield of verify_check_this_host(),
+                       i.e. OK, FAIL, or DEFER
+*/
+
+int
+verify_check_host(uschar **listptr)
+{
+return verify_check_this_host(listptr, sender_host_cache, NULL,
+  (sender_host_address == NULL)? US"" : sender_host_address, NULL);
+}
+
+
+
+
+
+/*************************************************
+*              Invert an IP address              *
+*************************************************/
+
+/* Originally just used for DNS xBL lists, now also used for the
+reverse_ip expansion operator.
+
+Arguments:
+  buffer         where to put the answer
+  address        the address to invert
+*/
+
+void
+invert_address(uschar *buffer, uschar *address)
+{
+int bin[4];
+uschar *bptr = buffer;
+
+/* If this is an IPv4 address mapped into IPv6 format, adjust the pointer
+to the IPv4 part only. */
+
+if (Ustrncmp(address, "::ffff:", 7) == 0) address += 7;
+
+/* Handle IPv4 address: when HAVE_IPV6 is false, the result of host_aton() is
+always 1. */
+
+if (host_aton(address, bin) == 1)
+  {
+  int i;
+  int x = bin[0];
+  for (i = 0; i < 4; i++)
+    {
+    sprintf(CS bptr, "%d.", x & 255);
+    while (*bptr) bptr++;
+    x >>= 8;
+    }
+  }
+
+/* Handle IPv6 address. Actually, as far as I know, there are no IPv6 addresses
+in any DNS black lists, and the format in which they will be looked up is
+unknown. This is just a guess. */
+
+#if HAVE_IPV6
+else
+  {
+  int i, j;
+  for (j = 3; j >= 0; j--)
+    {
+    int x = bin[j];
+    for (i = 0; i < 8; i++)
+      {
+      sprintf(CS bptr, "%x.", x & 15);
+      while (*bptr) bptr++;
+      x >>= 4;
+      }
+    }
+  }
+#endif
+
+/* Remove trailing period -- this is needed so that both arbitrary
+dnsbl keydomains and inverted addresses may be combined with the
+same format string, "%s.%s" */
+
+*(--bptr) = 0;
+}
+
+
+
+/*************************************************
+*          Perform a single dnsbl lookup         *
+*************************************************/
+
+/* This function is called from verify_check_dnsbl() below. It is also called
+recursively from within itself when domain and domain_txt are different
+pointers, in order to get the TXT record from the alternate domain.
+
+Arguments:
+  domain         the outer dnsbl domain
+  domain_txt     alternate domain to lookup TXT record on success; when the
+                   same domain is to be used, domain_txt == domain (that is,
+                   the pointers must be identical, not just the text)
+  keydomain      the current keydomain (for debug message)
+  prepend        subdomain to lookup (like keydomain, but
+                   reversed if IP address)
+  iplist         the list of matching IP addresses, or NULL for "any"
+  bitmask        true if bitmask matching is wanted
+  match_type     condition for 'succeed' result
+                   0 => Any RR in iplist     (=)
+                   1 => No RR in iplist      (!=)
+                   2 => All RRs in iplist    (==)
+                   3 => Some RRs not in iplist (!==)
+                   the two bits are defined as MT_NOT and MT_ALL
+  defer_return   what to return for a defer
+
+Returns:         OK if lookup succeeded
+                 FAIL if not
+*/
+
+static int
+one_check_dnsbl(uschar *domain, uschar *domain_txt, uschar *keydomain,
+  uschar *prepend, uschar *iplist, BOOL bitmask, int match_type,
+  int defer_return)
+{
+dns_answer dnsa;
+dns_scan dnss;
+tree_node *t;
+dnsbl_cache_block *cb;
+int old_pool = store_pool;
+uschar query[256];         /* DNS domain max length */
+
+/* Construct the specific query domainname */
+
+if (!string_format(query, sizeof(query), "%s.%s", prepend, domain))
+  {
+  log_write(0, LOG_MAIN|LOG_PANIC, "dnslist query is too long "
+    "(ignored): %s...", query);
+  return FAIL;
+  }
+
+/* Look for this query in the cache. */
+
+t = tree_search(dnsbl_cache, query);
+
+/* If not cached from a previous lookup, we must do a DNS lookup, and
+cache the result in permanent memory. */
+
+if (t == NULL)
+  {
+  store_pool = POOL_PERM;
+
+  /* Set up a tree entry to cache the lookup */
+
+  t = store_get(sizeof(tree_node) + Ustrlen(query));
+  Ustrcpy(t->name, query);
+  t->data.ptr = cb = store_get(sizeof(dnsbl_cache_block));
+  (void)tree_insertnode(&dnsbl_cache, t);
+
+  /* Do the DNS loopup . */
+
+  HDEBUG(D_dnsbl) debug_printf("new DNS lookup for %s\n", query);
+  cb->rc = dns_basic_lookup(&dnsa, query, T_A);
+  cb->text_set = FALSE;
+  cb->text = NULL;
+  cb->rhs = NULL;
+
+  /* If the lookup succeeded, cache the RHS address. The code allows for
+  more than one address - this was for complete generality and the possible
+  use of A6 records. However, A6 records have been reduced to experimental
+  status (August 2001) and may die out. So they may never get used at all,
+  let alone in dnsbl records. However, leave the code here, just in case.
+
+  Quite apart from one A6 RR generating multiple addresses, there are DNS
+  lists that return more than one A record, so we must handle multiple
+  addresses generated in that way as well. */
+
+  if (cb->rc == DNS_SUCCEED)
+    {
+    dns_record *rr;
+    dns_address **addrp = &(cb->rhs);
+    for (rr = dns_next_rr(&dnsa, &dnss, RESET_ANSWERS);
+         rr != NULL;
+         rr = dns_next_rr(&dnsa, &dnss, RESET_NEXT))
+      {
+      if (rr->type == T_A)
+        {
+        dns_address *da = dns_address_from_rr(&dnsa, rr);
+        if (da != NULL)
+          {
+          *addrp = da;
+          while (da->next != NULL) da = da->next;
+          addrp = &(da->next);
+          }
+        }
+      }
+
+    /* If we didn't find any A records, change the return code. This can
+    happen when there is a CNAME record but there are no A records for what
+    it points to. */
+
+    if (cb->rhs == NULL) cb->rc = DNS_NODATA;
+    }
+
+  store_pool = old_pool;
+  }
+
+/* Previous lookup was cached */
+
+else
+  {
+  HDEBUG(D_dnsbl) debug_printf("using result of previous DNS lookup\n");
+  cb = t->data.ptr;
+  }
+
+/* We now have the result of the DNS lookup, either newly done, or cached
+from a previous call. If the lookup succeeded, check against the address
+list if there is one. This may be a positive equality list (introduced by
+"="), a negative equality list (introduced by "!="), a positive bitmask
+list (introduced by "&"), or a negative bitmask list (introduced by "!&").*/
+
+if (cb->rc == DNS_SUCCEED)
+  {
+  dns_address *da = NULL;
+  uschar *addlist = cb->rhs->address;
+
+  /* For A and AAAA records, there may be multiple addresses from multiple
+  records. For A6 records (currently not expected to be used) there may be
+  multiple addresses from a single record. */
+
+  for (da = cb->rhs->next; da != NULL; da = da->next)
+    addlist = string_sprintf("%s, %s", addlist, da->address);
+
+  HDEBUG(D_dnsbl) debug_printf("DNS lookup for %s succeeded (yielding %s)\n",
+    query, addlist);
+
+  /* Address list check; this can be either for equality, or via a bitmask.
+  In the latter case, all the bits must match. */
+
+  if (iplist != NULL)
+    {
+    for (da = cb->rhs; da != NULL; da = da->next)
+      {
+      int ipsep = ',';
+      uschar ip[46];
+      uschar *ptr = iplist;
+      uschar *res;
+
+      /* Handle exact matching */
+
+      if (!bitmask)
+        {
+        while ((res = string_nextinlist(&ptr, &ipsep, ip, sizeof(ip))) != NULL)
+          {
+          if (Ustrcmp(CS da->address, ip) == 0) break;
+          }
+        }
+
+      /* Handle bitmask matching */
+
+      else
+        {
+        int address[4];
+        int mask = 0;
+
+        /* At present, all known DNS blocking lists use A records, with
+        IPv4 addresses on the RHS encoding the information they return. I
+        wonder if this will linger on as the last vestige of IPv4 when IPv6
+        is ubiquitous? Anyway, for now we use paranoia code to completely
+        ignore IPv6 addresses. The default mask is 0, which always matches.
+        We change this only for IPv4 addresses in the list. */
+
+        if (host_aton(da->address, address) == 1) mask = address[0];
+
+        /* Scan the returned addresses, skipping any that are IPv6 */
+
+        while ((res = string_nextinlist(&ptr, &ipsep, ip, sizeof(ip))) != NULL)
+          {
+          if (host_aton(ip, address) != 1) continue;
+          if ((address[0] & mask) == address[0]) break;
+          }
+        }
+
+      /* If either
+
+         (a) An IP address in an any ('=') list matched, or
+         (b) No IP address in an all ('==') list matched
+
+      then we're done searching. */
+
+      if (((match_type & MT_ALL) != 0) == (res == NULL)) break;
+      }
+
+    /* If da == NULL, either
+
+       (a) No IP address in an any ('=') list matched, or
+       (b) An IP address in an all ('==') list didn't match
+
+    so behave as if the DNSBL lookup had not succeeded, i.e. the host is not on
+    the list. */
+
+    if ((match_type == MT_NOT || match_type == MT_ALL) != (da == NULL))
+      {
+      HDEBUG(D_dnsbl)
+        {
+        uschar *res = NULL;
+        switch(match_type)
+          {
+          case 0:
+          res = US"was no match";
+          break;
+          case MT_NOT:
+          res = US"was an exclude match";
+          break;
+          case MT_ALL:
+          res = US"was an IP address that did not match";
+          break;
+          case MT_NOT|MT_ALL:
+          res = US"were no IP addresses that did not match";
+          break;
+          }
+        debug_printf("=> but we are not accepting this block class because\n");
+        debug_printf("=> there %s for %s%c%s\n",
+          res,
+          ((match_type & MT_ALL) == 0)? "" : "=",
+          bitmask? '&' : '=', iplist);
+        }
+      return FAIL;
+      }
+    }
+
+  /* Either there was no IP list, or the record matched, implying that the
+  domain is on the list. We now want to find a corresponding TXT record. If an
+  alternate domain is specified for the TXT record, call this function
+  recursively to look that up; this has the side effect of re-checking that
+  there is indeed an A record at the alternate domain. */
+
+  if (domain_txt != domain)
+    return one_check_dnsbl(domain_txt, domain_txt, keydomain, prepend, NULL,
+      FALSE, match_type, defer_return);
+
+  /* If there is no alternate domain, look up a TXT record in the main domain
+  if it has not previously been cached. */
+
+  if (!cb->text_set)
+    {
+    cb->text_set = TRUE;
+    if (dns_basic_lookup(&dnsa, query, T_TXT) == DNS_SUCCEED)
+      {
+      dns_record *rr;
+      for (rr = dns_next_rr(&dnsa, &dnss, RESET_ANSWERS);
+           rr != NULL;
+           rr = dns_next_rr(&dnsa, &dnss, RESET_NEXT))
+        if (rr->type == T_TXT) break;
+      if (rr != NULL)
+        {
+        int len = (rr->data)[0];
+        if (len > 511) len = 127;
+        store_pool = POOL_PERM;
+        cb->text = string_sprintf("%.*s", len, (const uschar *)(rr->data+1));
+        store_pool = old_pool;
+        }
+      }
+    }
+
+  dnslist_value = addlist;
+  dnslist_text = cb->text;
+  return OK;
+  }
+
+/* There was a problem with the DNS lookup */
+
+if (cb->rc != DNS_NOMATCH && cb->rc != DNS_NODATA)
+  {
+  log_write(L_dnslist_defer, LOG_MAIN,
+    "DNS list lookup defer (probably timeout) for %s: %s", query,
+    (defer_return == OK)?   US"assumed in list" :
+    (defer_return == FAIL)? US"assumed not in list" :
+                            US"returned DEFER");
+  return defer_return;
+  }
+
+/* No entry was found in the DNS; continue for next domain */
+
+HDEBUG(D_dnsbl)
+  {
+  debug_printf("DNS lookup for %s failed\n", query);
+  debug_printf("=> that means %s is not listed at %s\n",
+     keydomain, domain);
+  }
+
+return FAIL;
+}
+
+
+
+
+/*************************************************
+*        Check host against DNS black lists      *
+*************************************************/
+
+/* This function runs checks against a list of DNS black lists, until one
+matches. Each item on the list can be of the form
+
+  domain=ip-address/key
+
+The domain is the right-most domain that is used for the query, for example,
+blackholes.mail-abuse.org. If the IP address is present, there is a match only
+if the DNS lookup returns a matching IP address. Several addresses may be
+given, comma-separated, for example: x.y.z=127.0.0.1,127.0.0.2.
+
+If no key is given, what is looked up in the domain is the inverted IP address
+of the current client host. If a key is given, it is used to construct the
+domain for the lookup. For example:
+
+  dsn.rfc-ignorant.org/$sender_address_domain
+
+After finding a match in the DNS, the domain is placed in $dnslist_domain, and
+then we check for a TXT record for an error message, and if found, save its
+value in $dnslist_text. We also cache everything in a tree, to optimize
+multiple lookups.
+
+The TXT record is normally looked up in the same domain as the A record, but
+when many lists are combined in a single DNS domain, this will not be a very
+specific message. It is possible to specify a different domain for looking up
+TXT records; this is given before the main domain, comma-separated. For
+example:
+
+  dnslists = http.dnsbl.sorbs.net,dnsbl.sorbs.net=127.0.0.2 : \
+             socks.dnsbl.sorbs.net,dnsbl.sorbs.net=127.0.0.3
+
+The caching ensures that only one lookup in dnsbl.sorbs.net is done.
+
+Note: an address for testing RBL is 192.203.178.39
+Note: an address for testing DUL is 192.203.178.4
+Note: a domain for testing RFCI is example.tld.dsn.rfc-ignorant.org
+
+Arguments:
+  listptr      the domain/address/data list
+
+Returns:    OK      successful lookup (i.e. the address is on the list), or
+                      lookup deferred after +include_unknown
+            FAIL    name not found, or no data found for the given type, or
+                      lookup deferred after +exclude_unknown (default)
+            DEFER   lookup failure, if +defer_unknown was set
+*/
+
+int
+verify_check_dnsbl(uschar **listptr)
+{
+int sep = 0;
+int defer_return = FAIL;
+uschar *list = *listptr;
+uschar *domain;
+uschar *s;
+uschar buffer[1024];
+uschar revadd[128];        /* Long enough for IPv6 address */
+
+/* Indicate that the inverted IP address is not yet set up */
+
+revadd[0] = 0;
+
+/* In case this is the first time the DNS resolver is being used. */
+
+dns_init(FALSE, FALSE, FALSE); /*XXX dnssec? */
+
+/* Loop through all the domains supplied, until something matches */
+
+while ((domain = string_nextinlist(&list, &sep, buffer, sizeof(buffer))) != NULL)
+  {
+  int rc;
+  BOOL bitmask = FALSE;
+  int match_type = 0;
+  uschar *domain_txt;
+  uschar *comma;
+  uschar *iplist;
+  uschar *key;
+
+  HDEBUG(D_dnsbl) debug_printf("DNS list check: %s\n", domain);
+
+  /* Deal with special values that change the behaviour on defer */
+
+  if (domain[0] == '+')
+    {
+    if      (strcmpic(domain, US"+include_unknown") == 0) defer_return = OK;
+    else if (strcmpic(domain, US"+exclude_unknown") == 0) defer_return = FAIL;
+    else if (strcmpic(domain, US"+defer_unknown") == 0)   defer_return = DEFER;
+    else
+      log_write(0, LOG_MAIN|LOG_PANIC, "unknown item in dnslist (ignored): %s",
+        domain);
+    continue;
+    }
+
+  /* See if there's explicit data to be looked up */
+
+  key = Ustrchr(domain, '/');
+  if (key != NULL) *key++ = 0;
+
+  /* See if there's a list of addresses supplied after the domain name. This is
+  introduced by an = or a & character; if preceded by = we require all matches
+  and if preceded by ! we invert the result. */
+
+  iplist = Ustrchr(domain, '=');
+  if (iplist == NULL)
+    {
+    bitmask = TRUE;
+    iplist = Ustrchr(domain, '&');
+    }
+
+  if (iplist != NULL)                          /* Found either = or & */
+    {
+    if (iplist > domain && iplist[-1] == '!')  /* Handle preceding ! */
+      {
+      match_type |= MT_NOT;
+      iplist[-1] = 0;
+      }
+
+    *iplist++ = 0;                             /* Terminate domain, move on */
+
+    /* If we found = (bitmask == FALSE), check for == or =& */
+
+    if (!bitmask && (*iplist == '=' || *iplist == '&'))
+      {
+      bitmask = *iplist++ == '&';
+      match_type |= MT_ALL;
+      }
+    }
+
+  /* If there is a comma in the domain, it indicates that a second domain for
+  looking up TXT records is provided, before the main domain. Otherwise we must
+  set domain_txt == domain. */
+
+  domain_txt = domain;
+  comma = Ustrchr(domain, ',');
+  if (comma != NULL)
+    {
+    *comma++ = 0;
+    domain = comma;
+    }
+
+  /* Check that what we have left is a sensible domain name. There is no reason
+  why these domains should in fact use the same syntax as hosts and email
+  domains, but in practice they seem to. However, there is little point in
+  actually causing an error here, because that would no doubt hold up incoming
+  mail. Instead, I'll just log it. */
+
+  for (s = domain; *s != 0; s++)
+    {
+    if (!isalnum(*s) && *s != '-' && *s != '.' && *s != '_')
+      {
+      log_write(0, LOG_MAIN, "dnslists domain \"%s\" contains "
+        "strange characters - is this right?", domain);
+      break;
+      }
+    }
+
+  /* Check the alternate domain if present */
+
+  if (domain_txt != domain) for (s = domain_txt; *s != 0; s++)
+    {
+    if (!isalnum(*s) && *s != '-' && *s != '.' && *s != '_')
+      {
+      log_write(0, LOG_MAIN, "dnslists domain \"%s\" contains "
+        "strange characters - is this right?", domain_txt);
+      break;
+      }
+    }
+
+  /* If there is no key string, construct the query by adding the domain name
+  onto the inverted host address, and perform a single DNS lookup. */
+
+  if (key == NULL)
+    {
+    if (sender_host_address == NULL) return FAIL;    /* can never match */
+    if (revadd[0] == 0) invert_address(revadd, sender_host_address);
+    rc = one_check_dnsbl(domain, domain_txt, sender_host_address, revadd,
+      iplist, bitmask, match_type, defer_return);
+    if (rc == OK)
+      {
+      dnslist_domain = string_copy(domain_txt);
+      dnslist_matched = string_copy(sender_host_address);
+      HDEBUG(D_dnsbl) debug_printf("=> that means %s is listed at %s\n",
+        sender_host_address, dnslist_domain);
+      }
+    if (rc != FAIL) return rc;     /* OK or DEFER */
+    }
+
+  /* If there is a key string, it can be a list of domains or IP addresses to
+  be concatenated with the main domain. */
+
+  else
+    {
+    int keysep = 0;
+    BOOL defer = FALSE;
+    uschar *keydomain;
+    uschar keybuffer[256];
+    uschar keyrevadd[128];
+
+    while ((keydomain = string_nextinlist(&key, &keysep, keybuffer,
+            sizeof(keybuffer))) != NULL)
+      {
+      uschar *prepend = keydomain;
+
+      if (string_is_ip_address(keydomain, NULL) != 0)
+        {
+        invert_address(keyrevadd, keydomain);
+        prepend = keyrevadd;
+        }
+
+      rc = one_check_dnsbl(domain, domain_txt, keydomain, prepend, iplist,
+        bitmask, match_type, defer_return);
+
+      if (rc == OK)
+        {
+        dnslist_domain = string_copy(domain_txt);
+        dnslist_matched = string_copy(keydomain);
+        HDEBUG(D_dnsbl) debug_printf("=> that means %s is listed at %s\n",
+          keydomain, dnslist_domain);
+        return OK;
+        }
+
+      /* If the lookup deferred, remember this fact. We keep trying the rest
+      of the list to see if we get a useful result, and if we don't, we return
+      DEFER at the end. */
+
+      if (rc == DEFER) defer = TRUE;
+      }    /* continue with next keystring domain/address */
+
+    if (defer) return DEFER;
+    }
+  }        /* continue with next dnsdb outer domain */
+
+return FAIL;
+}
+
+/* vi: aw ai sw=2
+*/
+/* End of verify.c */
diff --git a/src/version.c b/src/version.c
new file mode 100644 (file)
index 0000000..04a2d07
--- /dev/null
@@ -0,0 +1,56 @@
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Function for setting up the version string. */
+
+#include "exim.h"
+
+#include "version.h"
+
+
+/* The header file cnumber.h contains a single line containing the
+compilation number, making it easy to have it updated automatically.
+Hence the fudgery below to get the number turned into a string, since
+we can't use #include inside a macro argument list */
+
+void
+version_init(void)
+{
+static uschar cnumber_buffer[24];
+static uschar date_buffer[32];
+
+uschar today[20];
+uschar *version_cnumber_format;
+
+int cnumber =
+#include "cnumber.h"
+;
+
+/* The odd magic after each of these is so they can be easily found
+for automatic patching to standard values when running regression tests.
+The reason that version_cnumber_format isn't just written inline in the
+sprintf() call is the gcc -Wall warns about a \0 in a format string. */
+
+version_cnumber = cnumber_buffer;
+version_cnumber_format = US"%d\0<<eximcnumber>>";
+sprintf(CS version_cnumber, CS version_cnumber_format, cnumber);
+version_string = US EXIM_VERSION_STR "\0<<eximversion>>";
+
+Ustrcpy(today, __DATE__);
+if (today[4] == ' ') today[4] = '0';
+today[3] = today[6] = '-';
+
+version_date = date_buffer;
+version_date[0] = 0;
+Ustrncat(version_date, today+4, 3);
+Ustrncat(version_date, today, 4);
+Ustrncat(version_date, today+7, 4);
+Ustrcat(version_date, " ");
+Ustrcat(version_date, __TIME__);
+}
+
+/* End of version.c */
diff --git a/src/version.sh b/src/version.sh
new file mode 100644 (file)
index 0000000..dbf18d7
--- /dev/null
@@ -0,0 +1,4 @@
+# automatically generated file - see ../scripts/reversion
+EXIM_RELEASE_VERSION="4.84"
+EXIM_VARIANT_VERSION=""
+EXIM_COMPILE_NUMBER="1"
diff --git a/util/README b/util/README
new file mode 100644 (file)
index 0000000..bff3378
--- /dev/null
@@ -0,0 +1,40 @@
+The "util" directory in the Exim distribution
+---------------------------------------------
+
+This directory contains some small scripts that people have contributed which
+may be useful to others. They are probably not usable immediately without at
+least some minor editing. Take them as starting points.
+
+cramtest.pl
+-----------
+
+A Perl script to help with debugging CRAM-MD5 authentication.
+
+logargs.sh
+----------
+
+A shell script to interpose between a caller and Exim, to find out what command
+line arguments it is trying to use.
+
+mkcdb.pl
+--------
+
+A Perl script for a converting flat file into a format that is suitable for
+processing by cdbmake into a cdb file. It has some advantages over the
+cdbmake-12 awk script.
+
+ratelimit.pl
+------------
+
+A Perl script for computing peak sending rates from a log file. This is for
+use with the ratelimit ACL condition, so that you can get some idea of what a
+reasonable limit would be before deploying the feature.
+
+unknownuser.sh
+--------------
+
+This is historical, dating to the time when people tried to send back a helpful
+message when an incoming message's recipient was unknown. It recalls a
+different age...
+
+====
diff --git a/util/cramtest.pl b/util/cramtest.pl
new file mode 100755 (executable)
index 0000000..cb70eb4
--- /dev/null
@@ -0,0 +1,58 @@
+#!/usr/bin/perl
+
+# This script is contributed by Vadim Vygonets to aid in debugging CRAM-MD5
+# authentication.
+
+# A patch was contributed by Jon Warbrick to upgrade it to use the Digest::MD5
+# module instead of the deprecated MD5 module.
+
+# The script prompts for three data values: a user name, a password, and the
+# challenge as sent out by an SMTP server. The challenge is a base-64 string.
+# It should be copied (cut-and-pasted) literally as the third data item. The
+# output of the program is the base-64 string that is to be returned as the
+# response to the challenge. Using the example in RFC 2195:
+#
+# User: tim
+# Password: tanstaaftanstaaf
+# Challenge: PDE4OTYuNjk3MTcwOTUyQHBvc3RvZmZpY2UucmVzdG9uLm1jaS5uZXQ+
+# dGltIGI5MTNhNjAyYzdlZGE3YTQ5NWI0ZTZlNzMzNGQzODkw
+#
+# The last line is what you you would send back to the server.
+
+
+# Copyright (c) 2002
+#       Vadim Vygonets <vadik-exim@vygo.net>.  All rights reserved.
+# Public domain is OK with me.
+
+use MIME::Base64;
+use Digest::MD5;
+
+print "User: ";
+chop($user = <>);
+print "Password: ";
+chop($passwd = <>);
+print "Challenge: ";
+chop($chal = <>);
+$chal =~ s/^334 //;
+
+$context = new Digest::MD5;
+if (length($passwd) > 64) {
+        $context->add($passwd);
+        $passwd = $context->digest();
+        $context->reset();
+}
+
+@passwd = unpack("C*", pack("a64", $passwd));
+for ($i = 0; $i < 64; $i++) {
+        $pass_ipad[$i] = $passwd[$i] ^ 0x36;
+        $pass_opad[$i] = $passwd[$i] ^ 0x5C;
+}
+$context->add(pack("C64", @pass_ipad), decode_base64($chal));
+$digest = $context->digest();
+$context->reset();
+$context->add(pack("C64", @pass_opad), $digest);
+$digest = $context->digest();
+
+print encode_base64($user . " " . unpack("H*", $digest));
+
+# End
diff --git a/util/gen_pkcs3.c b/util/gen_pkcs3.c
new file mode 100644 (file)
index 0000000..4be2c58
--- /dev/null
@@ -0,0 +1,239 @@
+/* Copyright (C) 2012 Phil Pennock.
+ * This is distributed as part of Exim and licensed under the GPL.
+ * See the file "NOTICE" for more details.
+ */
+
+/* Build with:
+ * c99 $(pkg-config --cflags openssl) gen_pkcs3.c $(pkg-config --libs openssl)
+ */
+
+/*
+ * Rationale:
+ * The Diffie-Hellman primes which are embedded into Exim as named primes for
+ * the tls_dhparam option are in the std-crypto.c file.  The source for those
+ * comes from various RFCs, where they are given in hexadecimal form.
+ *
+ * This tool provides convenient conversion, to reduce the risk of human
+ * error in transcription.
+ */
+
+#include <ctype.h>
+#include <errno.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <openssl/bio.h>
+#include <openssl/bn.h>
+#include <openssl/dh.h>
+#include <openssl/err.h>
+#include <openssl/pem.h>
+
+extern const char *__progname;
+
+
+void __attribute__((__noreturn__)) __attribute__((__format__(printf, 1, 2)))
+die(const char *fmt, ...)
+{
+  va_list ap;
+
+  fflush(NULL);
+  fprintf(stderr, "%s: ", __progname);
+  va_start(ap, fmt);
+  vfprintf(stderr, fmt, ap);
+  va_end(ap);
+  fprintf(stderr, "\n");
+  fflush(stderr);
+  exit(1);
+}
+
+
+void __attribute__((__noreturn__))
+die_openssl_err(const char *msg)
+{
+  char err_string[250];
+  unsigned long e;
+
+  ERR_error_string_n(ERR_get_error(), err_string, sizeof(err_string));
+  die("%s: %s", msg, err_string);
+}
+
+
+static BIGNUM *
+bn_from_text(const char *text)
+{
+  BIGNUM *b;
+  char *p, *spaceless;
+  const char *q, *end;
+  size_t len;
+  int rc;
+
+  len = strlen(text);
+  spaceless = malloc(len);
+  if (!spaceless)
+    die("malloc(%zu) failed: %s", len, strerror(errno));
+
+  for (p = spaceless, q = text, end = text + len;
+       q < end;
+       ++q) {
+    if (!isspace(*q))
+      *p++ = *q;
+  }
+
+  b = NULL;
+  rc = BN_hex2bn(&b, spaceless);
+
+  if (rc != p - spaceless)
+    die("BN_hex2bn did not convert entire input; took %d of %z bytes",
+        rc, p - spaceless);
+
+  return b;
+}
+
+
+static void
+our_dh_check(DH *dh)
+{
+  int rc, errflags = 0;
+
+  rc = DH_check(dh, &errflags);
+  if (!rc) die_openssl_err("DH_check() could not be performed");;
+
+  /* We ignore DH_UNABLE_TO_CHECK_GENERATOR because some of the invocations
+   * deliberately provide generators other than 2 or 5. */
+
+  if (errflags & DH_CHECK_P_NOT_SAFE_PRIME)
+    die("DH_check(): p not a safe prime");
+  if (errflags & DH_NOT_SUITABLE_GENERATOR)
+    die("DH_check(): g not suitable as generator");
+}
+
+
+static void
+emit_c_format_dh(FILE *stream, DH *dh)
+{
+  BIO *bio;
+  long length;
+  char *data, *end, *p, *nl;
+
+  bio = BIO_new(BIO_s_mem());
+  PEM_write_bio_DHparams(bio, dh);
+  length = BIO_get_mem_data(bio, &data);
+  if (!length)
+    die("no data in memory BIO to format for printing");
+  if (length < 0)
+    die("grr, negative length memory not supported");
+  end = data + length;
+
+  for (p = data; p < end; /**/) {
+    nl = strchr(p, '\n');
+    if (!nl) {
+      fprintf(stream, "\"%s\\n\"\n/* missing final newline */\n", p);
+      break;
+    }
+    *nl = '\0';
+    fprintf(stream, "\"%s\\n\"\n", p);
+    p = nl + 1;
+  }
+}
+
+
+void __attribute__((__noreturn__))
+usage(FILE *stream, int exitcode)
+{
+  fprintf(stream, "Usage: %s [-CPcst] <dh_p> <dh_g>\n"
+"Both dh_p and dh_g should be hex strings representing the numbers\n"
+"They may contain whitespace.\n"
+"\n"
+" -C      show C string form of PEM result\n"
+" -P      do not show PEM\n"
+" -c      run OpenSSL DH_check() on the DH object\n"
+" -s      show the parsed p and g\n"
+" -t      show text form of certificate\n"
+
+      , __progname);
+  exit(exitcode);
+}
+
+
+int
+main(int argc, char *argv[])
+{
+  BIGNUM *p, *g;
+  DH *dh;
+  int ch;
+  bool perform_dh_check = false;
+  bool show_c_form = false;
+  bool show_numbers = false;
+  bool show_pem = true;
+  bool show_text = false;
+
+  while ((ch = getopt(argc, argv, "CPcsth")) != -1) {
+    switch (ch) {
+      case 'C':
+        show_c_form = true;
+        break;
+      case 'P':
+        show_pem = false;
+        break;
+      case 'c':
+        perform_dh_check = true;
+        break;
+      case 's':
+        show_numbers = true;
+        break;
+      case 't':
+        show_text = true;
+        break;
+
+      case 'h':
+        usage(stdout, 0);
+      case '?':
+        die("Unknown option or missing argument -%c", optopt);
+      default:
+        die("Unhandled option -%c", ch);
+    }
+  }
+
+  optind -= 1;
+  argc -= optind;
+  argv += optind;
+
+  if (argc != 3) {
+    fprintf(stderr, "argc: %d\n", argc);
+    usage(stderr, 1);
+  }
+
+  p = bn_from_text(argv[1]);
+  g = bn_from_text(argv[2]);
+
+  if (show_numbers) {
+    printf("p = ");
+    BN_print_fp(stdout, p);
+    printf("\ng = ");
+    BN_print_fp(stdout, g);
+    printf("\n");
+  }
+
+  dh = DH_new();
+  dh->p = p;
+  dh->g = g;
+
+  if (perform_dh_check)
+    our_dh_check(dh);
+
+  if (show_text)
+    DHparams_print_fp(stdout, dh);
+
+  if (show_pem) {
+    if (show_c_form)
+      emit_c_format_dh(stdout, dh);
+    else
+      PEM_write_DHparams(stdout, dh);
+  }
+
+  DH_free(dh); /* should free p & g too */
+  return 0;
+}
diff --git a/util/logargs.sh b/util/logargs.sh
new file mode 100755 (executable)
index 0000000..87369a6
--- /dev/null
@@ -0,0 +1,26 @@
+#! /bin/sh
+
+# This script can be interposed between a calling program and another
+# program, in order to log the arguments which are being used. This can
+# be helpful in finding out what is going on if some program is calling
+# Exim with arguments it doesn't understand.
+
+# Set this to the the path of the program that must ultimately be called.
+
+CALL=exim
+
+# Set this to the name of the file where the data is to be logged. The
+# script writes on the end of it. It must be accessible to the user who
+# runs the script.
+
+LOGFILE=/home/ph10/tmp/zz
+
+# The arguments are copied to the log file
+
+echo $@ >>$LOGFILE
+
+# The real program is now called
+
+exec $CALL $@
+
+# End
diff --git a/util/mkcdb.pl b/util/mkcdb.pl
new file mode 100755 (executable)
index 0000000..2486e8f
--- /dev/null
@@ -0,0 +1,92 @@
+#!/usr/bin/perl -wT
+#
+# Create cdb file from flat alias file. DPC: 15/10/98.
+# Args:      source  (may be relative or absolute)
+#            target  (may be relative or absolute. Default = source)
+# Generates: target.cdb
+#            target.tmp
+#
+# Little Perl script to convert flat file into CDB file. Two advantages over
+# cdbmake-12 awk script that is distributed with CDB:
+#  1) Handles 'dpc22:dpc22@hermes' as well as 'dpc22 dpc22@hermes'
+#  2) Perl works with arbitary length strings: awk chokes at 1,024 chars
+#
+# Cambridge: hermes/src/admin/mkcdb,v 1.9 2005/02/15 18:14:12 fanf2 Exp
+
+use strict;
+
+$ENV{'PATH'} = "";
+umask(022);
+
+my $CDB = '/opt/cdb/bin/cdbmake';
+
+my $prog = $0;
+$prog =~ s|(.*/)?([^/]+)|$2|;
+
+my $source;
+my $target;
+if (@ARGV == 1) {
+    $source = shift(@ARGV);
+    $target = $source;
+} elsif (@ARGV == 2) {
+    $source = shift(@ARGV);
+    $target = shift(@ARGV);
+} else {
+    die("$prog: usage: <source> [<target>]\n");
+}
+# trust the invoker ?!
+$source =~ /(.*)/;
+$source = $1;
+$target =~ /(.*)/;
+$target = $1;
+
+open(SOURCE, "< ${source}")
+    or die("$prog: open < $source: $!\n");
+
+open(PIPE, "| $CDB $target.cdb $target.tmp")
+    or die("$prog: open | $CDB $target: $!\n");
+
+sub add_item ($$) {
+    my $key = shift;
+    my $val = shift;
+    printf PIPE ("+%d,%d:%s->%s\n", length($key), length($val), $key, $val);
+}
+
+sub add_line ($) {
+    my $line = shift;
+    if ($line =~ /^([^\s:]+)\s*:\s*(.*)$/s) {   # key : values
+        add_item($1,$2);
+        return;
+    }
+    if ($line =~ /^(\S+)\s+(.*)$/s) {       # key: values
+        add_item($1,$2);
+        return;
+    }
+    if ($line =~ /^(\S+)$/s) {              # key (empty value)
+        add_item($1,'');
+        return;
+    }
+    warn "$prog: unrecognized item: $line";
+}
+
+my $data;
+while(<SOURCE>) {
+    next if /^#/ or /^\s*$/;
+    m/^(\s*)(\S.*)\s+$/s;
+    if (length($1) == 0) {
+            add_line($data) if defined $data;
+            $data = $2;
+    } else {
+            $data .= " $2";
+    }
+}
+add_line($data) if defined $data;
+print PIPE "\n";
+
+close(SOURCE)
+    or die("$prog: close < $source: $!\n");
+close(PIPE)
+    or die($! ? "$prog: close | $CDB $target: $!\n"
+           : "$prog: close | $CDB $target: exited $?\n");
+
+exit 0;
diff --git a/util/ocsp_fetch.pl b/util/ocsp_fetch.pl
new file mode 100755 (executable)
index 0000000..0d214d6
--- /dev/null
@@ -0,0 +1,83 @@
+#!/usr/bin/perl
+# Copyright (C) 2012 Wizards Internet Ltd
+# License GPLv2: GNU GPL version 2 <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>
+use strict;
+use Getopt::Std;
+$Getopt::Std::STANDARD_HELP_VERSION=1;
+use IO::Handle;
+use Date::Parse;
+my ($o,$i,$s,$f,$t,$u,$VERSION);
+$VERSION='1.0';
+$o={'m'=>10};
+getopts("c:i:u:a:o:m:fv",$o);
+usage('No issuer specified') if ! $o->{'i'} && ! -f $o->{'i'};
+usage('No certificate specified') if ! $o->{'c'} && ! -f $o->{'c'};
+usage('No CA chain specified') if ! $o->{'a'} && ! -f $o->{'a'};
+usage('No OCSP file specified') if ! $o->{'o'};
+usage('No URL specified') if ! $o->{'u'};
+$o->{'t'}=$o->{'o'}.'.tmp';
+
+# check if we need to
+if (     $o->{'f'}
+    || ! -f $o->{'o'}
+    || ( -M $o->{'o'} > 0 )
+   )
+{
+    $i = new IO::Handle;
+    open( $i, "openssl ocsp -issuer $o->{'i'} -cert $o->{'c'} -url $o->{'u'} -CAfile $o->{'a'} -respout $o->{'t'} 2>/dev/null |" ) || die 'Unable to execute ocsp command';
+    $s = <$i> || die 'Unable to read status';
+    $f = <$i> || die 'Unable to read update time';
+    $t = <$i> || die 'Unable to read next update time';
+    close $i;
+    # Status ok ?
+    chomp($s);
+    chomp($f);
+    chomp($t);
+    $s =~ s/[^:]*: //;
+    $f =~ s/[^:]*: //;
+    $t =~ s/[^:]*: //;
+    $t = str2time($t);
+    die "OCSP status is $s" if $s ne 'good';
+    warn "Next Update $t" if $o->{'v'};
+    # response is good, adjust mod time and move into place.
+    $u = $t - $o->{'m'} * (($t - time)/100);
+    utime $u,$u,$o->{'t'};
+    rename $o->{'t'},$o->{'o'};
+}
+exit;
+
+sub
+usage
+{
+    my $m = shift;
+    print STDERR "$m\n" if $m;
+    HELP_MESSAGE(\*STDERR);
+    die;
+}
+sub
+HELP_MESSAGE
+{
+    my $h = shift;
+    print $h <<EOF
+Usage: $0 -i issuer -c certificate -u ocsp_url -a ca_certs -o response [-v] [-f]
+
+For a certificate    "www.example.com.pem"
+  signed by          "signing.example.net.pem"
+  signed by root CA  "ca.example.net.pem"
+  with OCSP server   http://ocsp.example.net/
+
+Ensure there is a file with the signing chain
+
+  cat ca.example.net.pem signing.example.net.pem >chain.pem
+
+The update procedure would be
+
+    ocsp_fetch -i signing.example.net.pem \
+        -c www.example.com.pem \
+    -u http://ocsp.example.net/ \
+    -a chain.pem \
+    -o www.example.com.ocsp.der
+EOF
+}
+# vi: aw ai sw=4
+# End of File
diff --git a/util/proxy_protocol_client.pl b/util/proxy_protocol_client.pl
new file mode 100644 (file)
index 0000000..feae3ca
--- /dev/null
@@ -0,0 +1,250 @@
+#!/usr/bin/perl
+#
+# Copyright (C) 2014 Todd Lyons
+# License GPLv2: GNU GPL version 2
+# <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>
+#
+# This script emulates a proxy which uses Proxy Protocol to communicate
+# to a backend server.  It should be run from an IP which is configured
+# to be a Proxy Protocol connection (or not, if you are testing error
+# scenarios) because Proxy Protocol specs require not to fall back to a
+# non-proxied mode.
+#
+# The script is interactive, so when you run it, you are expected to
+# perform whatever conversation is required for the protocol being
+# tested.  It uses STDIN/STDOUT, so you can also pipe output to/from the
+# script.  It was originally written to test Exim's Proxy Protocol
+# code, and it could be tested like this:
+#
+# swaks --pipe 'perl proxy_protocol_client.pl --server-ip
+#   host.internal.lan' --from user@example.com --to user@example.net
+#
+use strict;
+use warnings;
+use IO::Select;
+use IO::Socket;
+use Getopt::Long;
+use Data::Dumper;
+
+my %opts;
+GetOptions( \%opts,
+  'help',
+  '6|ipv6',
+  'dest-ip:s',
+  'dest-port:i',
+  'source-ip:s',
+  'source-port:i',
+  'server-ip:s',
+  'server-port:i',
+  'version:i'
+);
+&usage() if ($opts{help} || !$opts{'server-ip'});
+
+my ($dest_ip,$source_ip,$dest_port,$source_port);
+my %socket_map;
+my $status_line = "Testing Proxy Protocol Version " .
+                  ($opts{version} ? $opts{version} : '2') .
+                  ":\n";
+
+# All ip's and ports are in network byte order in version 2 mode, but are
+# simple strings when in version 1 mode.  The binary_pack_*() functions
+# return the required data for the Proxy Protocol version being used.
+
+# Use provided source or fall back to www.mrball.net
+$source_ip   = $opts{'source-ip'} ?  binary_pack_ip($opts{'source-ip'}) :
+                 $opts{6} ?
+                 binary_pack_ip("2001:470:d:367::50") :
+                 binary_pack_ip("208.89.139.252");
+$source_port = $opts{'source-port'} ?
+                 binary_pack_port($opts{'source-port'}) :
+                 binary_pack_port(43118);
+
+$status_line .= "-> " if (!$opts{version} || $opts{version} == 2);
+
+# Use provided dest or fall back to mail.exim.org
+$dest_ip   = $opts{'dest-ip'} ?  binary_pack_ip($opts{'dest-ip'}) :
+               $opts{6} ?
+               binary_pack_ip("2001:630:212:8:204:23ff:fed6:b664") :
+               binary_pack_ip("131.111.8.192");
+$dest_port = $opts{'dest-port'} ?
+               binary_pack_port($opts{'dest-port'}) :
+               binary_pack_port(25);
+
+# The IP and port of the Proxy Protocol backend real server being tested,
+# don't binary pack it.
+my $server_ip   = $opts{'server-ip'};
+my $server_port = $opts{'server-port'} ? $opts{'server-port'} : 25;
+
+my $s = IO::Select->new(); # for socket polling
+
+sub generate_preamble {
+  my @preamble;
+  if (!$opts{version} || $opts{version} == 2) {
+    @preamble = (
+      "\x0D\x0A\x0D\x0A\x00\x0D\x0A\x51\x55\x49\x54\x0A", # 12 byte v2 header
+      "\x21",                                             # top 4 bits declares v2
+                                                          # bottom 4 bits is command
+      $opts{6} ? "\x21" : "\x11",                         # inet6/4 and TCP (stream)
+      $opts{6} ? "\x00\x24" : "\x00\x0b",                 # 36 bytes / 12 bytes
+      $source_ip,
+      $dest_ip,
+      $source_port,
+      $dest_port
+    );
+  }
+  else {
+    @preamble = (
+      "PROXY", " ",                                       # Request proxy mode
+      $opts{6} ? "TCP6" : "TCP4", " ",                    # inet6/4 and TCP (stream)
+      $source_ip, " ",                                    
+      $dest_ip, " ",
+      $source_port, " ",
+      $dest_port,
+      "\x0d\x0a"
+    );
+    $status_line .= join "", @preamble;
+  }
+  print "\n", $status_line, "\n";
+  print "\n" if (!$opts{version} || $opts{version} == 2);
+  return @preamble;
+}
+
+sub binary_pack_port {
+  my $port = shift();
+  if ($opts{version} && $opts{version} == 1) {
+    return $port
+      if ($port && $port =~ /^\d+$/ && $port > 0 && $port < 65536);
+    die "Not a valid port: $port";
+  }
+  $status_line .= $port." ";
+  $port = pack "S", $port;
+  return $port;
+}
+
+sub binary_pack_ip {
+  my $ip = shift();
+  if ( $ip =~ m/\./ && !$opts{6}) {
+    if (IP4_valid($ip)) {
+      return $ip if ($opts{version} && $opts{version} == 1);
+      $status_line .= $ip.":";
+      $ip = pack "C*", split /\./, $ip;
+    }
+    else { die "Invalid IPv4: $ip"; }
+  }
+  elsif ($ip =~ m/:/ && $opts{6}) {
+    $ip = pad_ipv6($ip);
+    if (IP6_valid($ip)) {
+      return $ip if ($opts{version} && $opts{version} == 1);
+      $status_line .= $ip.":";
+      $ip = pack "S>*", map hex, split /:/, $ip;
+    }
+    else { die "Invalid IPv6: $ip"; }
+  }
+  else { die "Mismatching IP families passed: $ip"; }
+  return $ip;
+}
+
+sub pad_ipv6 {
+  my $ip = shift();
+  my @ip = split /:/, $ip;
+  my $segments = scalar @ip;
+  return $ip if ($segments == 8);
+  $ip = "";
+  for (my $count=1; $count <= $segments; $count++) {
+    my $block = $ip[$count-1];
+    if ($block) {
+      $ip .= $block;
+      $ip .= ":" unless $count == $segments;
+    }
+    elsif ($count == 1) {
+      # Somebody passed us ::1, fix it, but it's not really valid
+      $ip = "0:";
+    }
+    else {
+      $ip .= join ":", map "0", 0..(8-$segments);
+      $ip .= ":";
+    }
+  }
+  return $ip;
+}
+
+sub IP6_valid {
+  my $ip = shift;
+  $ip = lc($ip);
+  return 0 unless ($ip =~ /^[0-9a-f:]+$/);
+  my @ip = split /:/, $ip;
+  return 0 if (scalar @ip != 8);
+  return 1;
+}
+
+sub IP4_valid {
+  my $ip = shift;
+  $ip =~ /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
+  foreach ($1,$2,$3,$4){
+    if ($_  <256 && $_ >0) {next;}
+    return 0;
+  }
+  return 1;
+}
+
+sub go_interactive {
+  my $continue = 1;
+  while($continue) {
+    # Check for input on both ends, recheck every 5 sec
+    for my $socket ($s->can_read(5)) {
+      my $remote = $socket_map{$socket};
+      my $buffer;
+      my $read = $socket->sysread($buffer, 4096);
+      if ($read) {
+        $remote->syswrite($buffer);
+      }
+      else {
+        $continue = 0;
+      }
+    }
+  }
+}
+
+sub connect_stdin_to_proxy {
+  my $sock = new IO::Socket::INET(
+               PeerAddr => $server_ip,
+               PeerPort => $server_port,
+               Proto    => 'tcp'
+             );
+
+  die "Could not create socket: $!\n" unless $sock;
+  # Add sockets to the Select group
+  $s->add(\*STDIN);
+  $s->add($sock);
+  # Tie the sockets together using this hash
+  $socket_map{\*STDIN} = $sock;
+  $socket_map{$sock} = \*STDOUT;
+  return $sock;
+}
+
+sub usage {
+  chomp(my $prog = `basename $0`);
+  print <<EOF;
+Usage: $prog [required] [optional]
+  Required:
+    --server-ip   IP of server to test proxy configuration,
+                  a hostname is ok, but for only this setting
+  Optional:
+    --server-port Port server is listening on (default 25)
+    --6           IPv6 source/dest (default IPv4), if none specified,
+                  some default, reverse resolvable IP's are used for
+                  the source and dest ip/port
+    --dest-ip     Public IP of the proxy server
+    --dest-port   Port of public IP of proxy server
+    --source-ip   IP connecting to the proxy server
+    --source-port Port of IP connecting to the proxy server
+    --help        This output
+EOF
+  exit;
+}
+
+
+my $sock = connect_stdin_to_proxy();
+my @preamble = generate_preamble();
+print $sock @preamble;
+go_interactive();
diff --git a/util/ratelimit.pl b/util/ratelimit.pl
new file mode 100644 (file)
index 0000000..d7fd43a
--- /dev/null
@@ -0,0 +1,157 @@
+#!/usr/bin/perl -wT
+
+use strict;
+
+sub usage () {
+  print <<END;
+usage: ratelimit.pl [options] <period> <regex> <logfile>
+
+The aim of this script is to compute clients' peak sending rates
+from an Exim log file, using the same formula as Exim's ratelimit
+ACL condition. This is so that you can get an idea of a reasonable
+limit setting before you deploy the restrictions.
+
+options:
+
+-d          Show debugging information to stderr
+-p          Show progress of parse the log to stderr
+
+<period>    The smoothing period in seconds, as defined by the
+            documentation for the ratelimit ACL condition.
+
+            This script isn't perfectly accurate, because the time
+            stamps in Exim's log files are only accurate to a second
+            whereas internally Exim computes sender rates to the
+            accuracy of your computer's clock (typically 10ms).
+
+<regex>     The second argument is a regular expression.
+
+            Each line is matched against the regular expression.
+            Lines that do not match are ignored. The regex may
+            contain 0, 1, or 2 () capturing sub-expressions.
+
+            If there are no () sub-expressions, then every line that
+            matches is used to compute a single rate. Its maximum
+            value is reported when the script finishes.
+
+            If there is one () sub-expression, then the text matched
+            by the sub-expression is used to identify a rate lookup
+            key, similar to the lookup key used by the ratelimit
+            ACL condition. For example, you might write a regex
+            to match the client IP address, or the authenticated
+            username. Separate rates are computed for each different
+            client and the maximum rate for each client is reported
+            when the script finishes.
+
+            If there are two () sub-expressions, then the text matched
+            by the first sub-expression is used to identify a rate
+            lookup key as above, and the second is used to match the
+            message size recorded in the log line, e.g. "S=(\\d+)".
+            In this case the byte rate is computed instead of the
+            message rate, similar to the per_byte option of the
+            ratelimit ACL condition.
+
+<logfile>   The log files to be processed can be specified on the
+            command line after the other arguments; if no filenames
+            are specified the script will read from stdin.
+
+examples:
+
+./ratelimit.pl 1 ' <= .*? \[(.*?)\]' <logfile>
+
+            Compute burst sending rate like ACL condition
+            ratelimit = 0 / 1s / strict / \$sender_host_address
+
+./ratelimit.pl 3600 '<= (.*?) ' <logfile>
+
+            Compute sending rate like ACL condition
+            ratelimit = 0 / 1h / strict / \$sender_address
+
+END
+  exit 1;
+}
+
+sub iso2unix (@) {
+  my ($y,$m,$d,$H,$M,$S,$zs,$zh,$zm) = @_;
+  use integer;
+  $y -= $m < 3;
+  $m += $m < 3 ? 10 : -2;
+  my $z = defined $zs ? "${zs}1" * ($zh * 60 + $zm) : 0;
+  my $t = $y/400 - $y/100 + $y/4 + $y*365
+        + $m*367/12 + $d - 719499;
+  return $t * 86400
+       + $H * 3600
+       + $M * 60
+       + $S
+       - $z;
+}
+
+my $debug = 0;
+my $progress = 0;
+while (@ARGV && $ARGV[0] =~ /^-\w+$/) {
+  $debug = 1    if $ARGV[0] =~ s/(-\w*)d(\w*)/$1$2/;
+  $progress = 1 if $ARGV[0] =~ s/(-\w*)p(\w*)/$1$2/;
+  shift if $ARGV[0] eq "-";
+}
+
+usage if @ARGV < 2;
+
+my $progtime = "";
+
+my $period = shift;
+
+my $re_txt = shift;
+my $re = qr{$re_txt}o;
+
+my %time;
+my %rate;
+my %max;
+
+sub debug ($) {
+  my $key = shift;
+  printf STDERR "%s\t%12d %8s %5.2f %5.2f\n",
+    $_, $time{$key}, $key, $max{$key}, $rate{$key};
+}
+
+while (<>) {
+  next unless $_ =~ $re;
+  my $key = $1 || "";
+  my $size = $2 || 1.0;
+  my $time = iso2unix
+    ($_ =~ m{^(\d{4})-(\d\d)-(\d\d)[ ]
+              (\d\d):(\d\d):(\d\d)[ ]
+              (?:([+-])(\d\d)(\d\d)[ ])?
+            }x);
+  if ($progress) {
+    my $prog_now = substr $_, 0, 14;
+    if ($progtime ne $prog_now) {
+      $progtime = $prog_now;
+      print STDERR "$progtime\n";
+    }
+  }
+  if (not defined $time{$key}) {
+    $time{$key} = $time;
+    $rate{$key} = 0.0;
+    $max{$key} = 0.0;
+    debug $key if $debug;
+    next;
+  }
+  # see acl_ratelimit() for details of the following
+  my $interval = $time - $time{$key};
+  $interval = 1e-9 if $interval <= 0.0;
+  my $i_over_p = $interval / $period;
+  my $a = exp(-$i_over_p);
+  $time{$key} = $time;
+  $rate{$key} = $size * (1.0 - $a) / $i_over_p + $a * $rate{$key};
+  $max{$key} = $rate{$key} if $rate{$key} > $max{$key};
+  debug $key if $debug;
+}
+
+print map {
+  " " x (20 - length) .
+  "$_ : $max{$_}\n"
+} sort {
+  $max{$a} <=> $max{$b}
+} keys %max;
+
+# eof
diff --git a/util/unknownuser.sh b/util/unknownuser.sh
new file mode 100755 (executable)
index 0000000..fe04dc6
--- /dev/null
@@ -0,0 +1,32 @@
+#! /bin/sh
+
+# This is a sample script for demonstrating how to handle unknown users in
+# a more friendly way than just returning a "user unknown" error. It can
+# be called from a pipe transport set up like this:
+
+# unknownuser_pipe:
+#   driver = pipe;
+#   command = "/opt/exim/util/unknownuser.sh",
+#   ignore_status,
+#   return_output,
+#   user = nobody
+
+# which is specified by a smartuser director set up like this:
+
+# unknownuser:
+#   transport = unknownuser_pipe,
+#   no_verify,
+#   driver = smartuser;
+
+# Any output generated by this script is then returned to the sender of
+# the message. You can run any commands you like at this point, for example,
+# to attempt fuzzy matches on the local part of the address. Here we just
+# give a bland message, demonstrating the availability of the variables
+# $LOCAL_PART and $DOMAIN.
+
+cat <<End
+"$LOCAL_PART" is not a known user mailbox in the domain "$DOMAIN".
+End
+
+
+